xref: /freebsd/sys/netinet/tcp_input.c (revision 3e88eb903befabe44a72c071cd792f22c5e587bd)
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 
85dbc42409SLawrence Stewart #include <netinet/cc.h>
86df8bae1dSRodney W. Grimes #include <netinet/in.h>
8757f60867SMark Johnston #include <netinet/in_kdtrace.h>
88960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
89df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
90960ed29cSSeigo Tanimura #include <netinet/in_var.h>
91df8bae1dSRodney W. Grimes #include <netinet/ip.h>
928e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h>	/* required for icmp_var.h */
93686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM */
94df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
95ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
96686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
97686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
98686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
99960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h>
100960ed29cSSeigo Tanimura #include <netinet6/nd6.h>
101df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
102df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
103df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
104df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
105fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h>
106df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
107c325962bSMike Silbersack #include <netinet/tcp_syncache.h>
108610ee2f9SDavid Greenman #ifdef TCPDEBUG
109df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
110fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */
11109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
11209fe6320SNavdeep Parhar #include <netinet/tcp_offload.h>
11309fe6320SNavdeep Parhar #endif
114fb59c426SYoshinobu Inoue 
115b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
116b9234fafSSam Leffler #include <netipsec/ipsec.h>
117b9234fafSSam Leffler #include <netipsec/ipsec6.h>
118b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
119b9234fafSSam Leffler 
120db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
121db4f9cc7SJonathan Lemon 
122aed55708SRobert Watson #include <security/mac/mac_framework.h>
123aed55708SRobert Watson 
124dbc42409SLawrence Stewart const int tcprexmtthresh = 3;
1250312fbe9SPoul-Henning Kamp 
126773673c1SAndre Oppermann int tcp_log_in_vain = 0;
127816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
128eddfbb76SRobert Watson     &tcp_log_in_vain, 0,
129eddfbb76SRobert Watson     "Log all incoming TCP segments to closed ports");
130816a3d83SPoul-Henning Kamp 
13182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, blackhole) = 0;
13282cea7e6SBjoern A. Zeeb #define	V_blackhole		VNET(blackhole)
1336df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW,
134eddfbb76SRobert Watson     &VNET_NAME(blackhole), 0,
135eddfbb76SRobert Watson     "Do not send RST on segments to closed ports");
13616f7f31fSGeoff Rehmet 
13782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_delack_enabled) = 1;
1386df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_VNET | CTLFLAG_RW,
139eddfbb76SRobert Watson     &VNET_NAME(tcp_delack_enabled), 0,
1403d177f46SBill Fumerola     "Delay ACK to try and piggyback it onto a data packet");
141f498eeeeSDavid Greenman 
14282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, drop_synfin) = 0;
14382cea7e6SBjoern A. Zeeb #define	V_drop_synfin		VNET(drop_synfin)
1446df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_VNET | CTLFLAG_RW,
145eddfbb76SRobert Watson     &VNET_NAME(drop_synfin), 0,
146eddfbb76SRobert Watson     "Drop TCP packets with SYN+FIN set");
147f8613305SDag-Erling Smørgrav 
14882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1;
14982cea7e6SBjoern A. Zeeb #define	V_tcp_do_rfc3042	VNET(tcp_do_rfc3042)
1506df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_VNET | CTLFLAG_RW,
151eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3042), 0,
152eddfbb76SRobert Watson     "Enable RFC 3042 (Limited Transmit)");
153582a954bSJeffrey Hsu 
15482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1;
1556df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_VNET | CTLFLAG_RW,
156eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3390), 0,
157da3a8a1aSJeffrey Hsu     "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
158da3a8a1aSJeffrey Hsu 
15909440655SAndre Oppermann SYSCTL_NODE(_net_inet_tcp, OID_AUTO, experimental, CTLFLAG_RW, 0,
16009440655SAndre Oppermann     "Experimental TCP extensions");
16109440655SAndre Oppermann 
16209440655SAndre Oppermann VNET_DEFINE(int, tcp_do_initcwnd10) = 1;
1636df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_experimental, OID_AUTO, initcwnd10, CTLFLAG_VNET | CTLFLAG_RW,
16409440655SAndre Oppermann     &VNET_NAME(tcp_do_initcwnd10), 0,
165b8b4cfcdSSergey Kandaurov     "Enable RFC 6928 (Increasing initial CWND to 10)");
16609440655SAndre Oppermann 
16782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1;
1686df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_VNET | CTLFLAG_RW,
169eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3465), 0,
17024cb0f22SLawrence Stewart     "Enable RFC 3465 (Appropriate Byte Counting)");
171eddfbb76SRobert Watson 
17282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2;
1736df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_VNET | CTLFLAG_RW,
174eddfbb76SRobert Watson     &VNET_NAME(tcp_abc_l_var), 2,
17524cb0f22SLawrence Stewart     "Cap the max cwnd increment during slow-start to this number of segments");
17624cb0f22SLawrence Stewart 
1776472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN");
178f2512ba1SRui Paulo 
17982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_ecn) = 0;
1806df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW,
181eddfbb76SRobert Watson     &VNET_NAME(tcp_do_ecn), 0,
182eddfbb76SRobert Watson     "TCP ECN support");
183eddfbb76SRobert Watson 
18482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_ecn_maxretries) = 1;
1856df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_VNET | CTLFLAG_RW,
186eddfbb76SRobert Watson     &VNET_NAME(tcp_ecn_maxretries), 0,
187eddfbb76SRobert Watson     "Max retries before giving up on ECN");
188eddfbb76SRobert Watson 
1893220a212SGleb Smirnoff VNET_DEFINE(int, tcp_insecure_syn) = 0;
1903220a212SGleb Smirnoff #define	V_tcp_insecure_syn	VNET(tcp_insecure_syn)
1916df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_syn, CTLFLAG_VNET | CTLFLAG_RW,
1923220a212SGleb Smirnoff     &VNET_NAME(tcp_insecure_syn), 0,
1933220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting SYN packets");
1943220a212SGleb Smirnoff 
19582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0;
19682cea7e6SBjoern A. Zeeb #define	V_tcp_insecure_rst	VNET(tcp_insecure_rst)
1976df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_VNET | CTLFLAG_RW,
198eddfbb76SRobert Watson     &VNET_NAME(tcp_insecure_rst), 0,
1993220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting RST packets");
200a69968eeSMike Silbersack 
201fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64;
202873789cbSAndre Oppermann #define	V_tcp_recvspace	VNET(tcp_recvspace)
2036df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_VNET | CTLFLAG_RW,
204873789cbSAndre Oppermann     &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size");
205873789cbSAndre Oppermann 
20682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1;
20782cea7e6SBjoern A. Zeeb #define	V_tcp_do_autorcvbuf	VNET(tcp_do_autorcvbuf)
2086df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_VNET | CTLFLAG_RW,
209eddfbb76SRobert Watson     &VNET_NAME(tcp_do_autorcvbuf), 0,
2108b615593SMarko Zec     "Enable automatic receive buffer sizing");
2116741ecf5SAndre Oppermann 
21282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_inc) = 16*1024;
21382cea7e6SBjoern A. Zeeb #define	V_tcp_autorcvbuf_inc	VNET(tcp_autorcvbuf_inc)
2146df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_VNET | CTLFLAG_RW,
215eddfbb76SRobert Watson     &VNET_NAME(tcp_autorcvbuf_inc), 0,
2166489fe65SAndre Oppermann     "Incrementor step size of automatic receive buffer");
2176741ecf5SAndre Oppermann 
218b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024;
21982cea7e6SBjoern A. Zeeb #define	V_tcp_autorcvbuf_max	VNET(tcp_autorcvbuf_max)
2206df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW,
221eddfbb76SRobert Watson     &VNET_NAME(tcp_autorcvbuf_max), 0,
2228b615593SMarko Zec     "Max size of automatic receive buffer");
2236741ecf5SAndre Oppermann 
224eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, tcb);
22544e33a07SMarko Zec #define	tcb6	tcb  /* for KAME src sync over BSD*'s */
22682cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo);
227df8bae1dSRodney W. Grimes 
2285a53ca16SPaul Saab static void	 tcp_dooptions(struct tcpopt *, u_char *, int, int);
229679d9708SAndre Oppermann static void	 tcp_do_segment(struct mbuf *, struct tcphdr *,
230252ca428SRobert Watson 		     struct socket *, struct tcpcb *, int, int, uint8_t,
231252ca428SRobert Watson 		     int);
232302ce8d6SAndre Oppermann static void	 tcp_dropwithreset(struct mbuf *, struct tcphdr *,
233302ce8d6SAndre Oppermann 		     struct tcpcb *, int, int);
2344d77a549SAlfred Perlstein static void	 tcp_pulloutofband(struct socket *,
2354d77a549SAlfred Perlstein 		     struct tcphdr *, struct mbuf *, int);
2364d77a549SAlfred Perlstein static void	 tcp_xmit_timer(struct tcpcb *, int);
237c068736aSJeffrey Hsu static void	 tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *);
238dbc42409SLawrence Stewart static void inline	cc_ack_received(struct tcpcb *tp, struct tcphdr *th,
239dbc42409SLawrence Stewart 			    uint16_t type);
240dbc42409SLawrence Stewart static void inline	cc_conn_init(struct tcpcb *tp);
241dbc42409SLawrence Stewart static void inline	cc_post_recovery(struct tcpcb *tp, struct tcphdr *th);
24239bc9de5SLawrence Stewart static void inline	hhook_run_tcp_est_in(struct tcpcb *tp,
24339bc9de5SLawrence Stewart 			    struct tcphdr *th, struct tcpopt *to);
244f2512ba1SRui Paulo 
245315e3e38SRobert Watson /*
2465da0521fSAndrey V. Elsukov  * TCP statistics are stored in an "array" of counter(9)s.
2475923c293SGleb Smirnoff  */
2485da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat);
2495da0521fSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(tcpstat);
2505da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat,
2515da0521fSAndrey V. Elsukov     tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
2525923c293SGleb Smirnoff 
2535923c293SGleb Smirnoff #ifdef VIMAGE
2545da0521fSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(tcpstat);
2555923c293SGleb Smirnoff #endif /* VIMAGE */
2565923c293SGleb Smirnoff /*
257315e3e38SRobert Watson  * Kernel module interface for updating tcpstat.  The argument is an index
2585923c293SGleb Smirnoff  * into tcpstat treated as an array.
259315e3e38SRobert Watson  */
260315e3e38SRobert Watson void
261315e3e38SRobert Watson kmod_tcpstat_inc(int statnum)
262315e3e38SRobert Watson {
263315e3e38SRobert Watson 
2645da0521fSAndrey V. Elsukov 	counter_u64_add(VNET(tcpstat)[statnum], 1);
265315e3e38SRobert Watson }
266315e3e38SRobert Watson 
267dbc42409SLawrence Stewart /*
26839bc9de5SLawrence Stewart  * Wrapper for the TCP established input helper hook.
26939bc9de5SLawrence Stewart  */
27039bc9de5SLawrence Stewart static void inline
27139bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to)
27239bc9de5SLawrence Stewart {
27339bc9de5SLawrence Stewart 	struct tcp_hhook_data hhook_data;
27439bc9de5SLawrence Stewart 
27539bc9de5SLawrence Stewart 	if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) {
27639bc9de5SLawrence Stewart 		hhook_data.tp = tp;
27739bc9de5SLawrence Stewart 		hhook_data.th = th;
27839bc9de5SLawrence Stewart 		hhook_data.to = to;
27939bc9de5SLawrence Stewart 
28039bc9de5SLawrence Stewart 		hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data,
28139bc9de5SLawrence Stewart 		    tp->osd);
28239bc9de5SLawrence Stewart 	}
28339bc9de5SLawrence Stewart }
28439bc9de5SLawrence Stewart 
28539bc9de5SLawrence Stewart /*
286dbc42409SLawrence Stewart  * CC wrapper hook functions
287dbc42409SLawrence Stewart  */
288f2512ba1SRui Paulo static void inline
289dbc42409SLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t type)
290f2512ba1SRui Paulo {
291dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
292f2512ba1SRui Paulo 
293dbc42409SLawrence Stewart 	tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th);
2945b648e79SLawrence Stewart 	if (tp->snd_cwnd <= tp->snd_wnd)
295dbc42409SLawrence Stewart 		tp->ccv->flags |= CCF_CWND_LIMITED;
296dbc42409SLawrence Stewart 	else
297dbc42409SLawrence Stewart 		tp->ccv->flags &= ~CCF_CWND_LIMITED;
298dbc42409SLawrence Stewart 
299dbc42409SLawrence Stewart 	if (type == CC_ACK) {
300dbc42409SLawrence Stewart 		if (tp->snd_cwnd > tp->snd_ssthresh) {
301dbc42409SLawrence Stewart 			tp->t_bytes_acked += min(tp->ccv->bytes_this_ack,
302dbc42409SLawrence Stewart 			     V_tcp_abc_l_var * tp->t_maxseg);
303dbc42409SLawrence Stewart 			if (tp->t_bytes_acked >= tp->snd_cwnd) {
304dbc42409SLawrence Stewart 				tp->t_bytes_acked -= tp->snd_cwnd;
305dbc42409SLawrence Stewart 				tp->ccv->flags |= CCF_ABC_SENTAWND;
306dbc42409SLawrence Stewart 			}
307dbc42409SLawrence Stewart 		} else {
308dbc42409SLawrence Stewart 				tp->ccv->flags &= ~CCF_ABC_SENTAWND;
309dbc42409SLawrence Stewart 				tp->t_bytes_acked = 0;
310dbc42409SLawrence Stewart 		}
311dbc42409SLawrence Stewart 	}
312dbc42409SLawrence Stewart 
313dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->ack_received != NULL) {
314dbc42409SLawrence Stewart 		/* XXXLAS: Find a way to live without this */
315dbc42409SLawrence Stewart 		tp->ccv->curack = th->th_ack;
316dbc42409SLawrence Stewart 		CC_ALGO(tp)->ack_received(tp->ccv, type);
317dbc42409SLawrence Stewart 	}
318dbc42409SLawrence Stewart }
319dbc42409SLawrence Stewart 
320dbc42409SLawrence Stewart static void inline
321dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp)
322dbc42409SLawrence Stewart {
323dbc42409SLawrence Stewart 	struct hc_metrics_lite metrics;
324dbc42409SLawrence Stewart 	struct inpcb *inp = tp->t_inpcb;
325dbc42409SLawrence Stewart 	int rtt;
326dbc42409SLawrence Stewart 
327dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
328dbc42409SLawrence Stewart 
329dbc42409SLawrence Stewart 	tcp_hc_get(&inp->inp_inc, &metrics);
330dbc42409SLawrence Stewart 
331dbc42409SLawrence Stewart 	if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) {
332dbc42409SLawrence Stewart 		tp->t_srtt = rtt;
333dbc42409SLawrence Stewart 		tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
334dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedrtt);
335dbc42409SLawrence Stewart 		if (metrics.rmx_rttvar) {
336dbc42409SLawrence Stewart 			tp->t_rttvar = metrics.rmx_rttvar;
337dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_usedrttvar);
338dbc42409SLawrence Stewart 		} else {
339dbc42409SLawrence Stewart 			/* default variation is +- 1 rtt */
340dbc42409SLawrence Stewart 			tp->t_rttvar =
341dbc42409SLawrence Stewart 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
342dbc42409SLawrence Stewart 		}
343dbc42409SLawrence Stewart 		TCPT_RANGESET(tp->t_rxtcur,
344dbc42409SLawrence Stewart 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
345dbc42409SLawrence Stewart 		    tp->t_rttmin, TCPTV_REXMTMAX);
346dbc42409SLawrence Stewart 	}
347dbc42409SLawrence Stewart 	if (metrics.rmx_ssthresh) {
348dbc42409SLawrence Stewart 		/*
349dbc42409SLawrence Stewart 		 * There's some sort of gateway or interface
350dbc42409SLawrence Stewart 		 * buffer limit on the path.  Use this to set
351dbc42409SLawrence Stewart 		 * the slow start threshhold, but set the
352dbc42409SLawrence Stewart 		 * threshold to no less than 2*mss.
353dbc42409SLawrence Stewart 		 */
354dbc42409SLawrence Stewart 		tp->snd_ssthresh = max(2 * tp->t_maxseg, metrics.rmx_ssthresh);
355dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedssthresh);
356dbc42409SLawrence Stewart 	}
357dbc42409SLawrence Stewart 
358dbc42409SLawrence Stewart 	/*
3599ec4a4ccSAndre Oppermann 	 * Set the initial slow-start flight size.
360dbc42409SLawrence Stewart 	 *
361cf8f04f4SAndre Oppermann 	 * RFC5681 Section 3.1 specifies the default conservative values.
362cf8f04f4SAndre Oppermann 	 * RFC3390 specifies slightly more aggressive values.
363b8b4cfcdSSergey Kandaurov 	 * RFC6928 increases it to ten segments.
364cf8f04f4SAndre Oppermann 	 *
365cf8f04f4SAndre Oppermann 	 * If a SYN or SYN/ACK was lost and retransmitted, we have to
366cf8f04f4SAndre Oppermann 	 * reduce the initial CWND to one segment as congestion is likely
367cf8f04f4SAndre Oppermann 	 * requiring us to be cautious.
368dbc42409SLawrence Stewart 	 */
369cf8f04f4SAndre Oppermann 	if (tp->snd_cwnd == 1)
370cf8f04f4SAndre Oppermann 		tp->snd_cwnd = tp->t_maxseg;		/* SYN(-ACK) lost */
37109440655SAndre Oppermann 	else if (V_tcp_do_initcwnd10)
37209440655SAndre Oppermann 		tp->snd_cwnd = min(10 * tp->t_maxseg,
37309440655SAndre Oppermann 		    max(2 * tp->t_maxseg, 14600));
374cf8f04f4SAndre Oppermann 	else if (V_tcp_do_rfc3390)
375dbc42409SLawrence Stewart 		tp->snd_cwnd = min(4 * tp->t_maxseg,
376dbc42409SLawrence Stewart 		    max(2 * tp->t_maxseg, 4380));
37722efabd4SAndre Oppermann 	else {
37822efabd4SAndre Oppermann 		/* Per RFC5681 Section 3.1 */
37922efabd4SAndre Oppermann 		if (tp->t_maxseg > 2190)
38022efabd4SAndre Oppermann 			tp->snd_cwnd = 2 * tp->t_maxseg;
38122efabd4SAndre Oppermann 		else if (tp->t_maxseg > 1095)
38222efabd4SAndre Oppermann 			tp->snd_cwnd = 3 * tp->t_maxseg;
383dbc42409SLawrence Stewart 		else
38422efabd4SAndre Oppermann 			tp->snd_cwnd = 4 * tp->t_maxseg;
38522efabd4SAndre Oppermann 	}
386dbc42409SLawrence Stewart 
387dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->conn_init != NULL)
388dbc42409SLawrence Stewart 		CC_ALGO(tp)->conn_init(tp->ccv);
389dbc42409SLawrence Stewart }
390dbc42409SLawrence Stewart 
391dbc42409SLawrence Stewart void inline
392dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type)
393dbc42409SLawrence Stewart {
394dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
395dbc42409SLawrence Stewart 
396dbc42409SLawrence Stewart 	switch(type) {
397dbc42409SLawrence Stewart 	case CC_NDUPACK:
398dbc42409SLawrence Stewart 		if (!IN_FASTRECOVERY(tp->t_flags)) {
399f2512ba1SRui Paulo 			tp->snd_recover = tp->snd_max;
400f2512ba1SRui Paulo 			if (tp->t_flags & TF_ECN_PERMIT)
401f2512ba1SRui Paulo 				tp->t_flags |= TF_ECN_SND_CWR;
402f2512ba1SRui Paulo 		}
403dbc42409SLawrence Stewart 		break;
404dbc42409SLawrence Stewart 	case CC_ECN:
405dbc42409SLawrence Stewart 		if (!IN_CONGRECOVERY(tp->t_flags)) {
406dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_ecn_rcwnd);
407dbc42409SLawrence Stewart 			tp->snd_recover = tp->snd_max;
408dbc42409SLawrence Stewart 			if (tp->t_flags & TF_ECN_PERMIT)
409dbc42409SLawrence Stewart 				tp->t_flags |= TF_ECN_SND_CWR;
410dbc42409SLawrence Stewart 		}
411dbc42409SLawrence Stewart 		break;
412dbc42409SLawrence Stewart 	case CC_RTO:
413dbc42409SLawrence Stewart 		tp->t_dupacks = 0;
414dbc42409SLawrence Stewart 		tp->t_bytes_acked = 0;
415dbc42409SLawrence Stewart 		EXIT_RECOVERY(tp->t_flags);
4166157935fSLawrence Stewart 		tp->snd_ssthresh = max(2, min(tp->snd_wnd, tp->snd_cwnd) / 2 /
4176157935fSLawrence Stewart 		    tp->t_maxseg) * tp->t_maxseg;
418dbc42409SLawrence Stewart 		tp->snd_cwnd = tp->t_maxseg;
419dbc42409SLawrence Stewart 		break;
420dbc42409SLawrence Stewart 	case CC_RTO_ERR:
421dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_sndrexmitbad);
422dbc42409SLawrence Stewart 		/* RTO was unnecessary, so reset everything. */
423dbc42409SLawrence Stewart 		tp->snd_cwnd = tp->snd_cwnd_prev;
424dbc42409SLawrence Stewart 		tp->snd_ssthresh = tp->snd_ssthresh_prev;
425dbc42409SLawrence Stewart 		tp->snd_recover = tp->snd_recover_prev;
426dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASFRECOVERY)
427dbc42409SLawrence Stewart 			ENTER_FASTRECOVERY(tp->t_flags);
428dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASCRECOVERY)
429dbc42409SLawrence Stewart 			ENTER_CONGRECOVERY(tp->t_flags);
430dbc42409SLawrence Stewart 		tp->snd_nxt = tp->snd_max;
431672dc4aeSJohn Baldwin 		tp->t_flags &= ~TF_PREVVALID;
432dbc42409SLawrence Stewart 		tp->t_badrxtwin = 0;
433dbc42409SLawrence Stewart 		break;
434dbc42409SLawrence Stewart 	}
435dbc42409SLawrence Stewart 
436dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->cong_signal != NULL) {
437dbc42409SLawrence Stewart 		if (th != NULL)
438dbc42409SLawrence Stewart 			tp->ccv->curack = th->th_ack;
439dbc42409SLawrence Stewart 		CC_ALGO(tp)->cong_signal(tp->ccv, type);
440dbc42409SLawrence Stewart 	}
441dbc42409SLawrence Stewart }
442dbc42409SLawrence Stewart 
443dbc42409SLawrence Stewart static void inline
444dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th)
445dbc42409SLawrence Stewart {
446dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
447dbc42409SLawrence Stewart 
448dbc42409SLawrence Stewart 	/* XXXLAS: KASSERT that we're in recovery? */
449dbc42409SLawrence Stewart 
450dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->post_recovery != NULL) {
451dbc42409SLawrence Stewart 		tp->ccv->curack = th->th_ack;
452dbc42409SLawrence Stewart 		CC_ALGO(tp)->post_recovery(tp->ccv);
453dbc42409SLawrence Stewart 	}
454dbc42409SLawrence Stewart 	/* XXXLAS: EXIT_RECOVERY ? */
455dbc42409SLawrence Stewart 	tp->t_bytes_acked = 0;
456dbc42409SLawrence Stewart }
4570312fbe9SPoul-Henning Kamp 
4582903309aSAttilio Rao #ifdef TCP_SIGNATURE
4592903309aSAttilio Rao static inline int
4602903309aSAttilio Rao tcp_signature_verify_input(struct mbuf *m, int off0, int tlen, int optlen,
4612903309aSAttilio Rao     struct tcpopt *to, struct tcphdr *th, u_int tcpbflag)
4622903309aSAttilio Rao {
4632903309aSAttilio Rao 	int ret;
4642903309aSAttilio Rao 
4652903309aSAttilio Rao 	tcp_fields_to_net(th);
4662903309aSAttilio Rao 	ret = tcp_signature_verify(m, off0, tlen, optlen, to, th, tcpbflag);
4672903309aSAttilio Rao 	tcp_fields_to_host(th);
4682903309aSAttilio Rao 	return (ret);
4692903309aSAttilio Rao }
4702903309aSAttilio Rao #endif
4712903309aSAttilio Rao 
472fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
473fb59c426SYoshinobu Inoue #ifdef INET6
474fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \
475fb59c426SYoshinobu Inoue do { \
476fb59c426SYoshinobu Inoue 	if ((tp) && (tp)->t_inpcb && \
47797d8d152SAndre Oppermann 	    ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \
47897d8d152SAndre Oppermann 		nd6_nud_hint(NULL, NULL, 0); \
479fb59c426SYoshinobu Inoue } while (0)
480fb59c426SYoshinobu Inoue #else
481fb59c426SYoshinobu Inoue #define ND6_HINT(tp)
482fb59c426SYoshinobu Inoue #endif
483df8bae1dSRodney W. Grimes 
484df8bae1dSRodney W. Grimes /*
485262c1c1aSMatthew Dillon  * Indicate whether this ack should be delayed.  We can delay the ack if
48685536381SHiren Panchasara  * following conditions are met:
48785536381SHiren Panchasara  *	- There is no delayed ack timer in progress.
48885536381SHiren Panchasara  *	- Our last ack wasn't a 0-sized window. We never want to delay
48985536381SHiren Panchasara  *	  the ack that opens up a 0-sized window.
49085536381SHiren Panchasara  *	- LRO wasn't used for this segment. We make sure by checking that the
49185536381SHiren Panchasara  *	  segment size is not larger than the MSS.
49285536381SHiren Panchasara  *	- Delayed acks are enabled or this is a half-synchronized T/TCP
49385536381SHiren Panchasara  *	  connection.
494d8c85a26SJonathan Lemon  */
495c1e5a6e5SAndre Oppermann #define DELAY_ACK(tp, tlen)						\
496b8152ba7SAndre Oppermann 	((!tcp_timer_active(tp, TT_DELACK) &&				\
497a14c749fSJonathan Lemon 	    (tp->t_flags & TF_RXWIN0SENT) == 0) &&			\
498c1e5a6e5SAndre Oppermann 	    (tlen <= tp->t_maxopd) &&					\
499603724d3SBjoern A. Zeeb 	    (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
500d8c85a26SJonathan Lemon 
501df8bae1dSRodney W. Grimes /*
5028d573cc1SAndre Oppermann  * TCP input handling is split into multiple parts:
5038d573cc1SAndre Oppermann  *   tcp6_input is a thin wrapper around tcp_input for the extended
5048d573cc1SAndre Oppermann  *	ip6_protox[] call format in ip6_input
5058d573cc1SAndre Oppermann  *   tcp_input handles primary segment validation, inpcb lookup and
5068d573cc1SAndre Oppermann  *	SYN processing on listen sockets
5078d573cc1SAndre Oppermann  *   tcp_do_segment processes the ACK and text of the segment for
5088d573cc1SAndre Oppermann  *	establishing, established and closing connections
509df8bae1dSRodney W. Grimes  */
510fb59c426SYoshinobu Inoue #ifdef INET6
511fb59c426SYoshinobu Inoue int
512ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto)
513fb59c426SYoshinobu Inoue {
514ad3f9ab3SAndre Oppermann 	struct mbuf *m = *mp;
51533841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
516*3e88eb90SAndrey V. Elsukov 	struct ip6_hdr *ip6;
517fb59c426SYoshinobu Inoue 
518fb59c426SYoshinobu Inoue 	IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
519fb59c426SYoshinobu Inoue 
520fb59c426SYoshinobu Inoue 	/*
521fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
522fb59c426SYoshinobu Inoue 	 * better place to put this in?
523fb59c426SYoshinobu Inoue 	 */
524*3e88eb90SAndrey V. Elsukov 	ip6 = mtod(m, struct ip6_hdr *);
525*3e88eb90SAndrey V. Elsukov 	ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
52633841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
527fb59c426SYoshinobu Inoue 		struct ip6_hdr *ip6;
528fb59c426SYoshinobu Inoue 
5298c0fec80SRobert Watson 		ifa_free(&ia6->ia_ifa);
530fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
531fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
532fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
5338f5a8818SKevin Lo 		return (IPPROTO_DONE);
534fb59c426SYoshinobu Inoue 	}
53577d396fdSMaksim Yevmenkin 	if (ia6)
53677d396fdSMaksim Yevmenkin 		ifa_free(&ia6->ia_ifa);
537fb59c426SYoshinobu Inoue 
5388f5a8818SKevin Lo 	return (tcp_input(mp, offp, proto));
539fb59c426SYoshinobu Inoue }
540b287c6c7SBjoern A. Zeeb #endif /* INET6 */
541fb59c426SYoshinobu Inoue 
5428f5a8818SKevin Lo int
5438f5a8818SKevin Lo tcp_input(struct mbuf **mp, int *offp, int proto)
544df8bae1dSRodney W. Grimes {
5458f5a8818SKevin Lo 	struct mbuf *m = *mp;
546b287c6c7SBjoern A. Zeeb 	struct tcphdr *th = NULL;
547ad3f9ab3SAndre Oppermann 	struct ip *ip = NULL;
548ad3f9ab3SAndre Oppermann 	struct inpcb *inp = NULL;
549302ce8d6SAndre Oppermann 	struct tcpcb *tp = NULL;
550302ce8d6SAndre Oppermann 	struct socket *so = NULL;
551e79adb8eSGarrett Wollman 	u_char *optp = NULL;
5528f5a8818SKevin Lo 	int off0;
55326f9a767SRodney W. Grimes 	int optlen = 0;
554b287c6c7SBjoern A. Zeeb #ifdef INET
555b287c6c7SBjoern A. Zeeb 	int len;
556b287c6c7SBjoern A. Zeeb #endif
557b287c6c7SBjoern A. Zeeb 	int tlen = 0, off;
558fb59c426SYoshinobu Inoue 	int drop_hdrlen;
559ad3f9ab3SAndre Oppermann 	int thflags;
560302ce8d6SAndre Oppermann 	int rstreason = 0;	/* For badport_bandlim accounting purposes */
5612903309aSAttilio Rao #ifdef TCP_SIGNATURE
5622903309aSAttilio Rao 	uint8_t sig_checked = 0;
5632903309aSAttilio Rao #endif
564b287c6c7SBjoern A. Zeeb 	uint8_t iptos = 0;
565c1de64a4SAndrey V. Elsukov 	struct m_tag *fwd_tag = NULL;
566c068736aSJeffrey Hsu #ifdef INET6
567302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6 = NULL;
568c068736aSJeffrey Hsu 	int isipv6;
569c068736aSJeffrey Hsu #else
570a160e630SAndre Oppermann 	const void *ip6 = NULL;
571b287c6c7SBjoern A. Zeeb #endif /* INET6 */
572302ce8d6SAndre Oppermann 	struct tcpopt to;		/* options in this segment */
573a160e630SAndre Oppermann 	char *s = NULL;			/* address and port logging */
574252ca428SRobert Watson 	int ti_locked;
575252ca428SRobert Watson #define	TI_UNLOCKED	1
576fa046d87SRobert Watson #define	TI_WLOCKED	2
577f76fcf6dSJeffrey Hsu 
578610ee2f9SDavid Greenman #ifdef TCPDEBUG
579c068736aSJeffrey Hsu 	/*
580c068736aSJeffrey Hsu 	 * The size of tcp_saveipgen must be the size of the max ip header,
581c068736aSJeffrey Hsu 	 * now IPv6.
582c068736aSJeffrey Hsu 	 */
58314739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
584410bb1bfSLuigi Rizzo 	struct tcphdr tcp_savetcp;
585610ee2f9SDavid Greenman 	short ostate = 0;
586610ee2f9SDavid Greenman #endif
587c068736aSJeffrey Hsu 
588fb59c426SYoshinobu Inoue #ifdef INET6
589fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
590fb59c426SYoshinobu Inoue #endif
591a0292f23SGarrett Wollman 
5928f5a8818SKevin Lo 	off0 = *offp;
5938f5a8818SKevin Lo 	m = *mp;
5948f5a8818SKevin Lo 	*mp = NULL;
595302ce8d6SAndre Oppermann 	to.to_flags = 0;
59678b50714SRobert Watson 	TCPSTAT_INC(tcps_rcvtotal);
597fb59c426SYoshinobu Inoue 
59804d3a452SHajimu UMEMOTO #ifdef INET6
599b287c6c7SBjoern A. Zeeb 	if (isipv6) {
6008d573cc1SAndre Oppermann 		/* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */
60145747ba5SBjoern A. Zeeb 
60245747ba5SBjoern A. Zeeb 		if (m->m_len < (sizeof(*ip6) + sizeof(*th))) {
60345747ba5SBjoern A. Zeeb 			m = m_pullup(m, sizeof(*ip6) + sizeof(*th));
60445747ba5SBjoern A. Zeeb 			if (m == NULL) {
60545747ba5SBjoern A. Zeeb 				TCPSTAT_INC(tcps_rcvshort);
6068f5a8818SKevin Lo 				return (IPPROTO_DONE);
60745747ba5SBjoern A. Zeeb 			}
60845747ba5SBjoern A. Zeeb 		}
60945747ba5SBjoern A. Zeeb 
610fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
61145747ba5SBjoern A. Zeeb 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
612fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
613356ab07eSBjoern A. Zeeb 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) {
61445747ba5SBjoern A. Zeeb 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
61545747ba5SBjoern A. Zeeb 				th->th_sum = m->m_pkthdr.csum_data;
61645747ba5SBjoern A. Zeeb 			else
61745747ba5SBjoern A. Zeeb 				th->th_sum = in6_cksum_pseudo(ip6, tlen,
61845747ba5SBjoern A. Zeeb 				    IPPROTO_TCP, m->m_pkthdr.csum_data);
61945747ba5SBjoern A. Zeeb 			th->th_sum ^= 0xffff;
62045747ba5SBjoern A. Zeeb 		} else
62145747ba5SBjoern A. Zeeb 			th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen);
62245747ba5SBjoern A. Zeeb 		if (th->th_sum) {
62378b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
624fb59c426SYoshinobu Inoue 			goto drop;
625fb59c426SYoshinobu Inoue 		}
62633841545SHajimu UMEMOTO 
62733841545SHajimu UMEMOTO 		/*
62833841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
62933841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
63033841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
63133841545SHajimu UMEMOTO 		 *
63233841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
63333841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
63433841545SHajimu UMEMOTO 		 */
63533841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
63633841545SHajimu UMEMOTO 			/* XXX stat */
63733841545SHajimu UMEMOTO 			goto drop;
63833841545SHajimu UMEMOTO 		}
639b287c6c7SBjoern A. Zeeb 	}
64004d3a452SHajimu UMEMOTO #endif
641b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
642b287c6c7SBjoern A. Zeeb 	else
643b287c6c7SBjoern A. Zeeb #endif
644b287c6c7SBjoern A. Zeeb #ifdef INET
645b287c6c7SBjoern A. Zeeb 	{
646df8bae1dSRodney W. Grimes 		/*
647df8bae1dSRodney W. Grimes 		 * Get IP and TCP header together in first mbuf.
648df8bae1dSRodney W. Grimes 		 * Note: IP leaves IP header in first mbuf.
649df8bae1dSRodney W. Grimes 		 */
650fb59c426SYoshinobu Inoue 		if (off0 > sizeof (struct ip)) {
651105bd211SGleb Smirnoff 			ip_stripoptions(m);
652fb59c426SYoshinobu Inoue 			off0 = sizeof(struct ip);
653fb59c426SYoshinobu Inoue 		}
654df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof (struct tcpiphdr)) {
6554dfdffe9SAndre Oppermann 			if ((m = m_pullup(m, sizeof (struct tcpiphdr)))
6564dfdffe9SAndre Oppermann 			    == NULL) {
65778b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvshort);
6588f5a8818SKevin Lo 				return (IPPROTO_DONE);
659df8bae1dSRodney W. Grimes 			}
660df8bae1dSRodney W. Grimes 		}
661fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
662db4f9cc7SJonathan Lemon 		th = (struct tcphdr *)((caddr_t)ip + off0);
6638ad458a4SGleb Smirnoff 		tlen = ntohs(ip->ip_len) - off0;
664df8bae1dSRodney W. Grimes 
665db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
666db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
667db4f9cc7SJonathan Lemon 				th->th_sum = m->m_pkthdr.csum_data;
668db4f9cc7SJonathan Lemon 			else
669db4f9cc7SJonathan Lemon 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
670c068736aSJeffrey Hsu 				    ip->ip_dst.s_addr,
6718f134647SGleb Smirnoff 				    htonl(m->m_pkthdr.csum_data + tlen +
672c068736aSJeffrey Hsu 				    IPPROTO_TCP));
673db4f9cc7SJonathan Lemon 			th->th_sum ^= 0xffff;
674db4f9cc7SJonathan Lemon 		} else {
6758f134647SGleb Smirnoff 			struct ipovly *ipov = (struct ipovly *)ip;
6768f134647SGleb Smirnoff 
677df8bae1dSRodney W. Grimes 			/*
678df8bae1dSRodney W. Grimes 			 * Checksum extended TCP header and data.
679df8bae1dSRodney W. Grimes 			 */
6808f134647SGleb Smirnoff 			len = off0 + tlen;
681fb59c426SYoshinobu Inoue 			bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
6828f134647SGleb Smirnoff 			ipov->ih_len = htons(tlen);
683fb59c426SYoshinobu Inoue 			th->th_sum = in_cksum(m, len);
68457f60867SMark Johnston 			/* Reset length for SDT probes. */
68557f60867SMark Johnston 			ip->ip_len = htons(tlen + off0);
686db4f9cc7SJonathan Lemon 		}
68757f60867SMark Johnston 
688fb59c426SYoshinobu Inoue 		if (th->th_sum) {
68978b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
690df8bae1dSRodney W. Grimes 			goto drop;
691df8bae1dSRodney W. Grimes 		}
692fb59c426SYoshinobu Inoue 		/* Re-initialization for later version check */
693fb59c426SYoshinobu Inoue 		ip->ip_v = IPVERSION;
694fb59c426SYoshinobu Inoue 	}
695b287c6c7SBjoern A. Zeeb #endif /* INET */
696df8bae1dSRodney W. Grimes 
697f2512ba1SRui Paulo #ifdef INET6
698f2512ba1SRui Paulo 	if (isipv6)
699f2512ba1SRui Paulo 		iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
700b287c6c7SBjoern A. Zeeb #endif
701b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
702f2512ba1SRui Paulo 	else
703f2512ba1SRui Paulo #endif
704b287c6c7SBjoern A. Zeeb #ifdef INET
705f2512ba1SRui Paulo 		iptos = ip->ip_tos;
706b287c6c7SBjoern A. Zeeb #endif
707f2512ba1SRui Paulo 
708df8bae1dSRodney W. Grimes 	/*
709df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
710df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
711df8bae1dSRodney W. Grimes 	 */
712fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
713df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
71478b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvbadoff);
715df8bae1dSRodney W. Grimes 		goto drop;
716df8bae1dSRodney W. Grimes 	}
717fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
718df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
71904d3a452SHajimu UMEMOTO #ifdef INET6
720b287c6c7SBjoern A. Zeeb 		if (isipv6) {
7218f5a8818SKevin Lo 			IP6_EXTHDR_CHECK(m, off0, off, IPPROTO_DONE);
722fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
723fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
724b287c6c7SBjoern A. Zeeb 		}
72504d3a452SHajimu UMEMOTO #endif
726b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
727b287c6c7SBjoern A. Zeeb 		else
728b287c6c7SBjoern A. Zeeb #endif
729b287c6c7SBjoern A. Zeeb #ifdef INET
730b287c6c7SBjoern A. Zeeb 		{
731df8bae1dSRodney W. Grimes 			if (m->m_len < sizeof(struct ip) + off) {
732c068736aSJeffrey Hsu 				if ((m = m_pullup(m, sizeof (struct ip) + off))
7334dfdffe9SAndre Oppermann 				    == NULL) {
73478b50714SRobert Watson 					TCPSTAT_INC(tcps_rcvshort);
7358f5a8818SKevin Lo 					return (IPPROTO_DONE);
736df8bae1dSRodney W. Grimes 				}
737fb59c426SYoshinobu Inoue 				ip = mtod(m, struct ip *);
738fb59c426SYoshinobu Inoue 				th = (struct tcphdr *)((caddr_t)ip + off0);
739fb59c426SYoshinobu Inoue 			}
740df8bae1dSRodney W. Grimes 		}
741b287c6c7SBjoern A. Zeeb #endif
742df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
743fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
744df8bae1dSRodney W. Grimes 	}
745fb59c426SYoshinobu Inoue 	thflags = th->th_flags;
746df8bae1dSRodney W. Grimes 
747e46cd3d4SDag-Erling Smørgrav 	/*
748df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
749df8bae1dSRodney W. Grimes 	 */
7502903309aSAttilio Rao 	tcp_fields_to_host(th);
751df8bae1dSRodney W. Grimes 
752df8bae1dSRodney W. Grimes 	/*
753794235b7SAndre Oppermann 	 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options.
754755c1f07SAndras Olah 	 */
755fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
756755c1f07SAndras Olah 
757755c1f07SAndras Olah 	/*
758fa046d87SRobert Watson 	 * Locate pcb for segment; if we're likely to add or remove a
759a7c7f2a7SJohn Baldwin 	 * connection then first acquire pcbinfo lock.  There are three cases
760fa046d87SRobert Watson 	 * where we might discover later we need a write lock despite the
761a7c7f2a7SJohn Baldwin 	 * flags: ACKs moving a connection out of the syncache, ACKs for a
762a7c7f2a7SJohn Baldwin 	 * connection in TIMEWAIT and SYNs not targeting a listening socket.
763df8bae1dSRodney W. Grimes 	 */
764a7c7f2a7SJohn Baldwin 	if ((thflags & (TH_FIN | TH_RST)) != 0) {
765603724d3SBjoern A. Zeeb 		INP_INFO_WLOCK(&V_tcbinfo);
766252ca428SRobert Watson 		ti_locked = TI_WLOCKED;
767fa046d87SRobert Watson 	} else
768fa046d87SRobert Watson 		ti_locked = TI_UNLOCKED;
769252ca428SRobert Watson 
7708d573cc1SAndre Oppermann 	/*
7718d573cc1SAndre Oppermann 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
7728d573cc1SAndre Oppermann 	 */
773b8056faeSGleb Smirnoff         if (
774b8056faeSGleb Smirnoff #ifdef INET6
775b8056faeSGleb Smirnoff 	    (isipv6 && (m->m_flags & M_IP6_NEXTHOP))
776b8056faeSGleb Smirnoff #ifdef INET
777b8056faeSGleb Smirnoff 	    || (!isipv6 && (m->m_flags & M_IP_NEXTHOP))
778b8056faeSGleb Smirnoff #endif
779b8056faeSGleb Smirnoff #endif
780b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6)
781b8056faeSGleb Smirnoff 	    (m->m_flags & M_IP_NEXTHOP)
782b8056faeSGleb Smirnoff #endif
783b8056faeSGleb Smirnoff 	    )
7849b932e9eSAndre Oppermann 		fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
7859b932e9eSAndre Oppermann 
786ce7ad664SEd Maste findpcb:
787ce7ad664SEd Maste #ifdef INVARIANTS
788ce7ad664SEd Maste 	if (ti_locked == TI_WLOCKED) {
789ce7ad664SEd Maste 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
790ce7ad664SEd Maste 	} else {
791ce7ad664SEd Maste 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
792ce7ad664SEd Maste 	}
793ce7ad664SEd Maste #endif
7948a006adbSBjoern A. Zeeb #ifdef INET6
7958a006adbSBjoern A. Zeeb 	if (isipv6 && fwd_tag != NULL) {
7968a006adbSBjoern A. Zeeb 		struct sockaddr_in6 *next_hop6;
7978a006adbSBjoern A. Zeeb 
7988a006adbSBjoern A. Zeeb 		next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
7998a006adbSBjoern A. Zeeb 		/*
8008a006adbSBjoern A. Zeeb 		 * Transparently forwarded. Pretend to be the destination.
8018a006adbSBjoern A. Zeeb 		 * Already got one like this?
8028a006adbSBjoern A. Zeeb 		 */
8038a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo,
8048a006adbSBjoern A. Zeeb 		    &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport,
8058a006adbSBjoern A. Zeeb 		    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m);
8068a006adbSBjoern A. Zeeb 		if (!inp) {
8078a006adbSBjoern A. Zeeb 			/*
8088a006adbSBjoern A. Zeeb 			 * It's new.  Try to find the ambushing socket.
8098a006adbSBjoern A. Zeeb 			 * Because we've rewritten the destination address,
8108a006adbSBjoern A. Zeeb 			 * any hardware-generated hash is ignored.
8118a006adbSBjoern A. Zeeb 			 */
8128a006adbSBjoern A. Zeeb 			inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src,
8138a006adbSBjoern A. Zeeb 			    th->th_sport, &next_hop6->sin6_addr,
8148a006adbSBjoern A. Zeeb 			    next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) :
8158a006adbSBjoern A. Zeeb 			    th->th_dport, INPLOOKUP_WILDCARD |
8168a006adbSBjoern A. Zeeb 			    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
8178a006adbSBjoern A. Zeeb 		}
818c1de64a4SAndrey V. Elsukov 	} else if (isipv6) {
8198a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src,
8208a006adbSBjoern A. Zeeb 		    th->th_sport, &ip6->ip6_dst, th->th_dport,
8218a006adbSBjoern A. Zeeb 		    INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
8228a006adbSBjoern A. Zeeb 		    m->m_pkthdr.rcvif, m);
8238a006adbSBjoern A. Zeeb 	}
8248a006adbSBjoern A. Zeeb #endif /* INET6 */
8258a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET)
8268a006adbSBjoern A. Zeeb 	else
8278a006adbSBjoern A. Zeeb #endif
8288a006adbSBjoern A. Zeeb #ifdef INET
8298a006adbSBjoern A. Zeeb 	if (fwd_tag != NULL) {
8309b932e9eSAndre Oppermann 		struct sockaddr_in *next_hop;
8319b932e9eSAndre Oppermann 
8329b932e9eSAndre Oppermann 		next_hop = (struct sockaddr_in *)(fwd_tag+1);
833f9e354dfSJulian Elischer 		/*
8342b25acc1SLuigi Rizzo 		 * Transparently forwarded. Pretend to be the destination.
835f9e354dfSJulian Elischer 		 * already got one like this?
836f9e354dfSJulian Elischer 		 */
837d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport,
838fa046d87SRobert Watson 		    ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB,
839d3c1f003SRobert Watson 		    m->m_pkthdr.rcvif, m);
840fa046d87SRobert Watson 		if (!inp) {
841fa046d87SRobert Watson 			/*
842fa046d87SRobert Watson 			 * It's new.  Try to find the ambushing socket.
843d3c1f003SRobert Watson 			 * Because we've rewritten the destination address,
844d3c1f003SRobert Watson 			 * any hardware-generated hash is ignored.
845fa046d87SRobert Watson 			 */
846fa046d87SRobert Watson 			inp = in_pcblookup(&V_tcbinfo, ip->ip_src,
847fa046d87SRobert Watson 			    th->th_sport, next_hop->sin_addr,
848fa046d87SRobert Watson 			    next_hop->sin_port ? ntohs(next_hop->sin_port) :
849fa046d87SRobert Watson 			    th->th_dport, INPLOOKUP_WILDCARD |
850fa046d87SRobert Watson 			    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
851f9e354dfSJulian Elischer 		}
852b10fbdeaSAndre Oppermann 	} else
853d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src,
854fa046d87SRobert Watson 		    th->th_sport, ip->ip_dst, th->th_dport,
855fa046d87SRobert Watson 		    INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
856d3c1f003SRobert Watson 		    m->m_pkthdr.rcvif, m);
8578a006adbSBjoern A. Zeeb #endif /* INET */
858f9e354dfSJulian Elischer 
859df8bae1dSRodney W. Grimes 	/*
860574b6964SAndre Oppermann 	 * If the INPCB does not exist then all data in the incoming
861574b6964SAndre Oppermann 	 * segment is discarded and an appropriate RST is sent back.
8628b07e49aSJulian Elischer 	 * XXX MRT Send RST using which routing table?
863df8bae1dSRodney W. Grimes 	 */
864816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
865574b6964SAndre Oppermann 		/*
866574b6964SAndre Oppermann 		 * Log communication attempts to ports that are not
867574b6964SAndre Oppermann 		 * in use.
868574b6964SAndre Oppermann 		 */
869574b6964SAndre Oppermann 		if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) ||
870574b6964SAndre Oppermann 		    tcp_log_in_vain == 2) {
871b7d747ecSAndre Oppermann 			if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6)))
872df541e5fSAndre Oppermann 				log(LOG_INFO, "%s; %s: Connection attempt "
873df541e5fSAndre Oppermann 				    "to closed port\n", s, __func__);
8742e4e1b4cSGeoff Rehmet 		}
875574b6964SAndre Oppermann 		/*
876574b6964SAndre Oppermann 		 * When blackholing do not respond with a RST but
877574b6964SAndre Oppermann 		 * completely ignore the segment and drop it.
878574b6964SAndre Oppermann 		 */
879603724d3SBjoern A. Zeeb 		if ((V_blackhole == 1 && (thflags & TH_SYN)) ||
880603724d3SBjoern A. Zeeb 		    V_blackhole == 2)
8811929eae1SAndre Oppermann 			goto dropunlock;
882574b6964SAndre Oppermann 
883a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
884a57815efSBosko Milekic 		goto dropwithreset;
885816a3d83SPoul-Henning Kamp 	}
886fa046d87SRobert Watson 	INP_WLOCK_ASSERT(inp);
88780cb9f21SKip Macy 	if (!(inp->inp_flags & INP_HW_FLOWID)
88880cb9f21SKip Macy 	    && (m->m_flags & M_FLOWID)
88980cb9f21SKip Macy 	    && ((inp->inp_socket == NULL)
89080cb9f21SKip Macy 		|| !(inp->inp_socket->so_options & SO_ACCEPTCONN))) {
89180cb9f21SKip Macy 		inp->inp_flags |= INP_HW_FLOWID;
89280cb9f21SKip Macy 		inp->inp_flags &= ~INP_SW_FLOWID;
89380cb9f21SKip Macy 		inp->inp_flowid = m->m_pkthdr.flowid;
8942f719932SAdrian Chadd 		inp->inp_flowtype = M_HASHTYPE_GET(m);
89580cb9f21SKip Macy 	}
896a2589465SKen Smith #ifdef IPSEC
897a2589465SKen Smith #ifdef INET6
898a2589465SKen Smith 	if (isipv6 && ipsec6_in_reject(m, inp)) {
8996794f460SAndrey V. Elsukov 		IPSEC6STAT_INC(ips_in_polvio);
900a2589465SKen Smith 		goto dropunlock;
901a2589465SKen Smith 	} else
902a2589465SKen Smith #endif /* INET6 */
903a2589465SKen Smith 	if (ipsec4_in_reject(m, inp) != 0) {
9046794f460SAndrey V. Elsukov 		IPSECSTAT_INC(ips_in_polvio);
905a2589465SKen Smith 		goto dropunlock;
906a2589465SKen Smith 	}
907a2589465SKen Smith #endif /* IPSEC */
908a2589465SKen Smith 
9098d573cc1SAndre Oppermann 	/*
9108d573cc1SAndre Oppermann 	 * Check the minimum TTL for socket.
9118d573cc1SAndre Oppermann 	 */
91234f83c52SGeorge V. Neville-Neil 	if (inp->inp_ip_minttl != 0) {
91334f83c52SGeorge V. Neville-Neil #ifdef INET6
91434f83c52SGeorge V. Neville-Neil 		if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim)
915302ce8d6SAndre Oppermann 			goto dropunlock;
91602707462SAndre Oppermann 		else
91734f83c52SGeorge V. Neville-Neil #endif
91834f83c52SGeorge V. Neville-Neil 		if (inp->inp_ip_minttl > ip->ip_ttl)
919302ce8d6SAndre Oppermann 			goto dropunlock;
92034f83c52SGeorge V. Neville-Neil 	}
921936cd18dSAndre Oppermann 
922340c35deSJonathan Lemon 	/*
923252ca428SRobert Watson 	 * A previous connection in TIMEWAIT state is supposed to catch stray
924252ca428SRobert Watson 	 * or duplicate segments arriving late.  If this segment was a
9250989f56cSRobert Watson 	 * legitimate new connection attempt, the old INPCB gets removed and
926252ca428SRobert Watson 	 * we can try again to find a listening socket.
927252ca428SRobert Watson 	 *
928fa046d87SRobert Watson 	 * At this point, due to earlier optimism, we may hold only an inpcb
929fa046d87SRobert Watson 	 * lock, and not the inpcbinfo write lock.  If so, we need to try to
930fa046d87SRobert Watson 	 * acquire it, or if that fails, acquire a reference on the inpcb,
931fa046d87SRobert Watson 	 * drop all locks, acquire a global write lock, and then re-acquire
932fa046d87SRobert Watson 	 * the inpcb lock.  We may at that point discover that another thread
933fa046d87SRobert Watson 	 * has tried to free the inpcb, in which case we need to loop back
934fa046d87SRobert Watson 	 * and try to find a new inpcb to deliver to.
935fa046d87SRobert Watson 	 *
936fa046d87SRobert Watson 	 * XXXRW: It may be time to rethink timewait locking.
937340c35deSJonathan Lemon 	 */
938883e9bc4SRobert Watson relocked:
939ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
940fa046d87SRobert Watson 		if (ti_locked == TI_UNLOCKED) {
941fa046d87SRobert Watson 			if (INP_INFO_TRY_WLOCK(&V_tcbinfo) == 0) {
942252ca428SRobert Watson 				in_pcbref(inp);
943252ca428SRobert Watson 				INP_WUNLOCK(inp);
944252ca428SRobert Watson 				INP_INFO_WLOCK(&V_tcbinfo);
945252ca428SRobert Watson 				ti_locked = TI_WLOCKED;
946252ca428SRobert Watson 				INP_WLOCK(inp);
947fa046d87SRobert Watson 				if (in_pcbrele_wlocked(inp)) {
948252ca428SRobert Watson 					inp = NULL;
949252ca428SRobert Watson 					goto findpcb;
950252ca428SRobert Watson 				}
951f681a5fdSRobert Watson 			} else
952252ca428SRobert Watson 				ti_locked = TI_WLOCKED;
953252ca428SRobert Watson 		}
954252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
955252ca428SRobert Watson 
956340c35deSJonathan Lemon 		if (thflags & TH_SYN)
957f72167f4SAndre Oppermann 			tcp_dooptions(&to, optp, optlen, TO_SYN);
9588d573cc1SAndre Oppermann 		/*
9598d573cc1SAndre Oppermann 		 * NB: tcp_twcheck unlocks the INP and frees the mbuf.
9608d573cc1SAndre Oppermann 		 */
9612104448fSAndre Oppermann 		if (tcp_twcheck(inp, &to, th, m, tlen))
962340c35deSJonathan Lemon 			goto findpcb;
963603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
9648f5a8818SKevin Lo 		return (IPPROTO_DONE);
965340c35deSJonathan Lemon 	}
966794235b7SAndre Oppermann 	/*
967794235b7SAndre Oppermann 	 * The TCPCB may no longer exist if the connection is winding
968794235b7SAndre Oppermann 	 * down or it is in the CLOSED state.  Either way we drop the
969794235b7SAndre Oppermann 	 * segment and send an appropriate response.
970794235b7SAndre Oppermann 	 */
971df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
972a160e630SAndre Oppermann 	if (tp == NULL || tp->t_state == TCPS_CLOSED) {
973a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
974a57815efSBosko Milekic 		goto dropwithreset;
97509f81a46SBosko Milekic 	}
976df8bae1dSRodney W. Grimes 
97709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
97809fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
97909fe6320SNavdeep Parhar 		tcp_offload_input(tp, m);
98009fe6320SNavdeep Parhar 		m = NULL;	/* consumed by the TOE driver */
98109fe6320SNavdeep Parhar 		goto dropunlock;
98209fe6320SNavdeep Parhar 	}
98309fe6320SNavdeep Parhar #endif
98409fe6320SNavdeep Parhar 
985252ca428SRobert Watson 	/*
986252ca428SRobert Watson 	 * We've identified a valid inpcb, but it could be that we need an
987fa046d87SRobert Watson 	 * inpcbinfo write lock but don't hold it.  In this case, attempt to
988fa046d87SRobert Watson 	 * acquire using the same strategy as the TIMEWAIT case above.  If we
989fa046d87SRobert Watson 	 * relock, we have to jump back to 'relocked' as the connection might
990fa046d87SRobert Watson 	 * now be in TIMEWAIT.
991252ca428SRobert Watson 	 */
992fa046d87SRobert Watson #ifdef INVARIANTS
993a7c7f2a7SJohn Baldwin 	if ((thflags & (TH_FIN | TH_RST)) != 0)
994fa046d87SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
995fa046d87SRobert Watson #endif
996a7c7f2a7SJohn Baldwin 	if (!((tp->t_state == TCPS_ESTABLISHED && (thflags & TH_SYN) == 0) ||
997a7c7f2a7SJohn Baldwin 	    (tp->t_state == TCPS_LISTEN && (thflags & TH_SYN)))) {
998fa046d87SRobert Watson 		if (ti_locked == TI_UNLOCKED) {
999fa046d87SRobert Watson 			if (INP_INFO_TRY_WLOCK(&V_tcbinfo) == 0) {
1000252ca428SRobert Watson 				in_pcbref(inp);
1001252ca428SRobert Watson 				INP_WUNLOCK(inp);
1002252ca428SRobert Watson 				INP_INFO_WLOCK(&V_tcbinfo);
1003252ca428SRobert Watson 				ti_locked = TI_WLOCKED;
1004252ca428SRobert Watson 				INP_WLOCK(inp);
1005fa046d87SRobert Watson 				if (in_pcbrele_wlocked(inp)) {
1006252ca428SRobert Watson 					inp = NULL;
1007252ca428SRobert Watson 					goto findpcb;
1008252ca428SRobert Watson 				}
1009883e9bc4SRobert Watson 				goto relocked;
1010f681a5fdSRobert Watson 			} else
1011252ca428SRobert Watson 				ti_locked = TI_WLOCKED;
1012252ca428SRobert Watson 		}
1013252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1014252ca428SRobert Watson 	}
1015252ca428SRobert Watson 
1016c488362eSRobert Watson #ifdef MAC
10178501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
101830d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, m))
1019302ce8d6SAndre Oppermann 		goto dropunlock;
1020c488362eSRobert Watson #endif
1021a557af22SRobert Watson 	so = inp->inp_socket;
1022302ce8d6SAndre Oppermann 	KASSERT(so != NULL, ("%s: so == NULL", __func__));
1023610ee2f9SDavid Greenman #ifdef TCPDEBUG
1024df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG) {
1025df8bae1dSRodney W. Grimes 		ostate = tp->t_state;
102610fe523eSMaxim Konovalov #ifdef INET6
10276f697424SBjoern A. Zeeb 		if (isipv6) {
1028540e8b7eSJeffrey Hsu 			bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6));
1029d30d90dcSMaxim Konovalov 		} else
10306f697424SBjoern A. Zeeb #endif
1031540e8b7eSJeffrey Hsu 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
1032fb59c426SYoshinobu Inoue 		tcp_savetcp = *th;
1033df8bae1dSRodney W. Grimes 	}
1034b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */
1035db33b3e6SAndre Oppermann 	/*
1036db33b3e6SAndre Oppermann 	 * When the socket is accepting connections (the INPCB is in LISTEN
1037db33b3e6SAndre Oppermann 	 * state) we look into the SYN cache if this is a new connection
1038a7c7f2a7SJohn Baldwin 	 * attempt or the completion of a previous one.
1039db33b3e6SAndre Oppermann 	 */
1040540e8b7eSJeffrey Hsu 	if (so->so_options & SO_ACCEPTCONN) {
1041540e8b7eSJeffrey Hsu 		struct in_conninfo inc;
1042540e8b7eSJeffrey Hsu 
10437824d002SAndre Oppermann 		KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but "
10447824d002SAndre Oppermann 		    "tp not listening", __func__));
10458bfb1918SAndre Oppermann 		bzero(&inc, sizeof(inc));
1046302ce8d6SAndre Oppermann #ifdef INET6
1047be2ac88cSJonathan Lemon 		if (isipv6) {
1048dcdb4371SBjoern A. Zeeb 			inc.inc_flags |= INC_ISIPV6;
1049be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
1050be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
1051302ce8d6SAndre Oppermann 		} else
1052302ce8d6SAndre Oppermann #endif
1053302ce8d6SAndre Oppermann 		{
1054be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
1055be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
1056be2ac88cSJonathan Lemon 		}
1057be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
1058be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
10597973fba3SJulian Elischer 		inc.inc_fibnum = so->so_fibnum;
1060be2ac88cSJonathan Lemon 
1061fb59c426SYoshinobu Inoue 		/*
10628d573cc1SAndre Oppermann 		 * Check for an existing connection attempt in syncache if
10638d573cc1SAndre Oppermann 		 * the flag is only ACK.  A successful lookup creates a new
10648d573cc1SAndre Oppermann 		 * socket appended to the listen queue in SYN_RECEIVED state.
1065fb59c426SYoshinobu Inoue 		 */
1066be2ac88cSJonathan Lemon 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
1067a7c7f2a7SJohn Baldwin 
1068a7c7f2a7SJohn Baldwin 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1069bf6d304aSAndre Oppermann 			/*
1070bf6d304aSAndre Oppermann 			 * Parse the TCP options here because
1071bf6d304aSAndre Oppermann 			 * syncookies need access to the reflected
1072bf6d304aSAndre Oppermann 			 * timestamp.
1073bf6d304aSAndre Oppermann 			 */
1074bf6d304aSAndre Oppermann 			tcp_dooptions(&to, optp, optlen, 0);
10759fa198beSAndre Oppermann 			/*
10769fa198beSAndre Oppermann 			 * NB: syncache_expand() doesn't unlock
10779fa198beSAndre Oppermann 			 * inp and tcpinfo locks.
10789fa198beSAndre Oppermann 			 */
1079bf6d304aSAndre Oppermann 			if (!syncache_expand(&inc, &to, th, &so, m)) {
10804195b4afSPaul Traina 				/*
1081e207f800SAndre Oppermann 				 * No syncache entry or ACK was not
1082be2ac88cSJonathan Lemon 				 * for our SYN/ACK.  Send a RST.
10838d573cc1SAndre Oppermann 				 * NB: syncache did its own logging
10848d573cc1SAndre Oppermann 				 * of the failure cause.
10854195b4afSPaul Traina 				 */
1086be2ac88cSJonathan Lemon 				rstreason = BANDLIM_RST_OPENPORT;
1087be2ac88cSJonathan Lemon 				goto dropwithreset;
1088be2ac88cSJonathan Lemon 			}
1089f76fcf6dSJeffrey Hsu 			if (so == NULL) {
1090be2ac88cSJonathan Lemon 				/*
1091e207f800SAndre Oppermann 				 * We completed the 3-way handshake
1092e207f800SAndre Oppermann 				 * but could not allocate a socket
1093e207f800SAndre Oppermann 				 * either due to memory shortage,
1094e207f800SAndre Oppermann 				 * listen queue length limits or
1095a160e630SAndre Oppermann 				 * global socket limits.  Send RST
1096a160e630SAndre Oppermann 				 * or wait and have the remote end
1097a160e630SAndre Oppermann 				 * retransmit the ACK for another
1098a160e630SAndre Oppermann 				 * try.
1099be2ac88cSJonathan Lemon 				 */
1100a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1101a160e630SAndre Oppermann 					log(LOG_DEBUG, "%s; %s: Listen socket: "
11028d573cc1SAndre Oppermann 					    "Socket allocation failed due to "
11038d573cc1SAndre Oppermann 					    "limits or memory shortage, %s\n",
1104ac957cd2SJulian Elischer 					    s, __func__,
1105ac957cd2SJulian Elischer 					    V_tcp_sc_rst_sock_fail ?
1106ac957cd2SJulian Elischer 					    "sending RST" : "try again");
1107603724d3SBjoern A. Zeeb 				if (V_tcp_sc_rst_sock_fail) {
1108e207f800SAndre Oppermann 					rstreason = BANDLIM_UNLIMITED;
1109e207f800SAndre Oppermann 					goto dropwithreset;
1110a160e630SAndre Oppermann 				} else
1111a160e630SAndre Oppermann 					goto dropunlock;
1112f76fcf6dSJeffrey Hsu 			}
1113be2ac88cSJonathan Lemon 			/*
1114be2ac88cSJonathan Lemon 			 * Socket is created in state SYN_RECEIVED.
11158d573cc1SAndre Oppermann 			 * Unlock the listen socket, lock the newly
11168d573cc1SAndre Oppermann 			 * created socket and update the tp variable.
1117be2ac88cSJonathan Lemon 			 */
11188501a69cSRobert Watson 			INP_WUNLOCK(inp);	/* listen socket */
1119be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
11208501a69cSRobert Watson 			INP_WLOCK(inp);		/* new connection */
1121be2ac88cSJonathan Lemon 			tp = intotcpcb(inp);
11228d573cc1SAndre Oppermann 			KASSERT(tp->t_state == TCPS_SYN_RECEIVED,
11238d573cc1SAndre Oppermann 			    ("%s: ", __func__));
11242903309aSAttilio Rao #ifdef TCP_SIGNATURE
11252903309aSAttilio Rao 			if (sig_checked == 0)  {
11262903309aSAttilio Rao 				tcp_dooptions(&to, optp, optlen,
11272903309aSAttilio Rao 				    (thflags & TH_SYN) ? TO_SYN : 0);
11282903309aSAttilio Rao 				if (!tcp_signature_verify_input(m, off0, tlen,
11292903309aSAttilio Rao 				    optlen, &to, th, tp->t_flags)) {
11302903309aSAttilio Rao 
11312903309aSAttilio Rao 					/*
11322903309aSAttilio Rao 					 * In SYN_SENT state if it receives an
11332903309aSAttilio Rao 					 * RST, it is allowed for further
11342903309aSAttilio Rao 					 * processing.
11352903309aSAttilio Rao 					 */
11362903309aSAttilio Rao 					if ((thflags & TH_RST) == 0 ||
11372903309aSAttilio Rao 					    (tp->t_state == TCPS_SYN_SENT) == 0)
11382903309aSAttilio Rao 						goto dropunlock;
11392903309aSAttilio Rao 				}
11402903309aSAttilio Rao 				sig_checked = 1;
11412903309aSAttilio Rao 			}
11422903309aSAttilio Rao #endif
11432903309aSAttilio Rao 
1144be2ac88cSJonathan Lemon 			/*
1145302ce8d6SAndre Oppermann 			 * Process the segment and the data it
1146302ce8d6SAndre Oppermann 			 * contains.  tcp_do_segment() consumes
1147302ce8d6SAndre Oppermann 			 * the mbuf chain and unlocks the inpcb.
1148302ce8d6SAndre Oppermann 			 */
1149f2512ba1SRui Paulo 			tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen,
1150252ca428SRobert Watson 			    iptos, ti_locked);
1151603724d3SBjoern A. Zeeb 			INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
11528f5a8818SKevin Lo 			return (IPPROTO_DONE);
1153be2ac88cSJonathan Lemon 		}
1154a160e630SAndre Oppermann 		/*
11558d573cc1SAndre Oppermann 		 * Segment flag validation for new connection attempts:
11568d573cc1SAndre Oppermann 		 *
1157a160e630SAndre Oppermann 		 * Our (SYN|ACK) response was rejected.
1158a160e630SAndre Oppermann 		 * Check with syncache and remove entry to prevent
1159a160e630SAndre Oppermann 		 * retransmits.
116019bc77c5SAndre Oppermann 		 *
116119bc77c5SAndre Oppermann 		 * NB: syncache_chkrst does its own logging of failure
116219bc77c5SAndre Oppermann 		 * causes.
1163a160e630SAndre Oppermann 		 */
1164a160e630SAndre Oppermann 		if (thflags & TH_RST) {
116519bc77c5SAndre Oppermann 			syncache_chkrst(&inc, th);
1166a160e630SAndre Oppermann 			goto dropunlock;
1167a160e630SAndre Oppermann 		}
1168a160e630SAndre Oppermann 		/*
1169a160e630SAndre Oppermann 		 * We can't do anything without SYN.
1170a160e630SAndre Oppermann 		 */
1171a160e630SAndre Oppermann 		if ((thflags & TH_SYN) == 0) {
1172a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1173a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1174773673c1SAndre Oppermann 				    "SYN is missing, segment ignored\n",
11758d573cc1SAndre Oppermann 				    s, __func__);
117678b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1177a160e630SAndre Oppermann 			goto dropunlock;
1178a160e630SAndre Oppermann 		}
1179a160e630SAndre Oppermann 		/*
1180a160e630SAndre Oppermann 		 * (SYN|ACK) is bogus on a listen socket.
1181a160e630SAndre Oppermann 		 */
1182fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1183a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1184a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
11858d573cc1SAndre Oppermann 				    "SYN|ACK invalid, segment rejected\n",
11868d573cc1SAndre Oppermann 				    s, __func__);
1187a160e630SAndre Oppermann 			syncache_badack(&inc);	/* XXX: Not needed! */
118878b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1189a57815efSBosko Milekic 			rstreason = BANDLIM_RST_OPENPORT;
1190a57815efSBosko Milekic 			goto dropwithreset;
11914195b4afSPaul Traina 		}
1192a160e630SAndre Oppermann 		/*
1193a160e630SAndre Oppermann 		 * If the drop_synfin option is enabled, drop all
1194a160e630SAndre Oppermann 		 * segments with both the SYN and FIN bits set.
1195a160e630SAndre Oppermann 		 * This prevents e.g. nmap from identifying the
1196a160e630SAndre Oppermann 		 * TCP/IP stack.
1197a160e630SAndre Oppermann 		 * XXX: Poor reasoning.  nmap has other methods
1198a160e630SAndre Oppermann 		 * and is constantly refining its stack detection
1199a160e630SAndre Oppermann 		 * strategies.
12008d573cc1SAndre Oppermann 		 * XXX: This is a violation of the TCP specification
1201a160e630SAndre Oppermann 		 * and was used by RFC1644.
1202a160e630SAndre Oppermann 		 */
1203603724d3SBjoern A. Zeeb 		if ((thflags & TH_FIN) && V_drop_synfin) {
1204a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1205a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1206773673c1SAndre Oppermann 				    "SYN|FIN segment ignored (based on "
12078d573cc1SAndre Oppermann 				    "sysctl setting)\n", s, __func__);
120878b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1209302ce8d6SAndre Oppermann 			goto dropunlock;
1210ebb0cbeaSPaul Traina 		}
1211be2ac88cSJonathan Lemon 		/*
1212be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
1213a160e630SAndre Oppermann 		 *
1214a160e630SAndre Oppermann 		 * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored
1215a160e630SAndre Oppermann 		 * as they do not affect the state of the TCP FSM.
1216a160e630SAndre Oppermann 		 * The data pointed to by TH_URG and th_urp is ignored.
1217be2ac88cSJonathan Lemon 		 */
1218a160e630SAndre Oppermann 		KASSERT((thflags & (TH_RST|TH_ACK)) == 0,
1219a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_RST or TH_ACK set", __func__));
1220a160e630SAndre Oppermann 		KASSERT(thflags & (TH_SYN),
1221a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_SYN not set", __func__));
122233841545SHajimu UMEMOTO #ifdef INET6
122333841545SHajimu UMEMOTO 		/*
122433841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
122533841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
122633841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
122733841545SHajimu UMEMOTO 		 * getting established.
122833841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
122933841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
123033841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
123133841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
123233841545SHajimu UMEMOTO 		 * for the exchange.
123333841545SHajimu UMEMOTO 		 *
123433841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
123533841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
123633841545SHajimu UMEMOTO 		 * SYN in this case.
123733841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
123833841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
123933841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
124033841545SHajimu UMEMOTO 		 *    used"
124133841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
124233841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
124333841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
124433841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
124533841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
124633841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
124733841545SHajimu UMEMOTO 		 *
124833841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
124933841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
125033841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
125133841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
125233841545SHajimu UMEMOTO 		 */
1253603724d3SBjoern A. Zeeb 		if (isipv6 && !V_ip6_use_deprecated) {
125433841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
125533841545SHajimu UMEMOTO 
1256*3e88eb90SAndrey V. Elsukov 			ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
12578c0fec80SRobert Watson 			if (ia6 != NULL &&
125833841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
12598c0fec80SRobert Watson 				ifa_free(&ia6->ia_ifa);
1260a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1261a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12628d573cc1SAndre Oppermann 					"Connection attempt to deprecated "
12638d573cc1SAndre Oppermann 					"IPv6 address rejected\n",
1264a160e630SAndre Oppermann 					s, __func__);
126533841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
126633841545SHajimu UMEMOTO 				goto dropwithreset;
126733841545SHajimu UMEMOTO 			}
126877d396fdSMaksim Yevmenkin 			if (ia6)
12698c0fec80SRobert Watson 				ifa_free(&ia6->ia_ifa);
127033841545SHajimu UMEMOTO 		}
1271b287c6c7SBjoern A. Zeeb #endif /* INET6 */
127265f28919SJesper Skriver 		/*
1273db33b3e6SAndre Oppermann 		 * Basic sanity checks on incoming SYN requests:
12748d573cc1SAndre Oppermann 		 *   Don't respond if the destination is a link layer
1275db33b3e6SAndre Oppermann 		 *	broadcast according to RFC1122 4.2.3.10, p. 104.
12768d573cc1SAndre Oppermann 		 *   If it is from this socket it must be forged.
12778d573cc1SAndre Oppermann 		 *   Don't respond if the source or destination is a
12788d573cc1SAndre Oppermann 		 *	global or subnet broad- or multicast address.
127993ec91baSCrist J. Clark 		 *   Note that it is quite possible to receive unicast
128093ec91baSCrist J. Clark 		 *	link-layer packets with a broadcast IP address. Use
128193ec91baSCrist J. Clark 		 *	in_broadcast() to find them.
1282be2ac88cSJonathan Lemon 		 */
12838d573cc1SAndre Oppermann 		if (m->m_flags & (M_BCAST|M_MCAST)) {
12848d573cc1SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
12858d573cc1SAndre Oppermann 			    log(LOG_DEBUG, "%s; %s: Listen socket: "
12868d573cc1SAndre Oppermann 				"Connection attempt from broad- or multicast "
1287773673c1SAndre Oppermann 				"link layer address ignored\n", s, __func__);
1288302ce8d6SAndre Oppermann 			goto dropunlock;
12898d573cc1SAndre Oppermann 		}
1290db33b3e6SAndre Oppermann #ifdef INET6
1291b287c6c7SBjoern A. Zeeb 		if (isipv6) {
1292db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1293a160e630SAndre Oppermann 			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) {
1294a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1295a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12968d573cc1SAndre Oppermann 					"Connection attempt to/from self "
1297773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1298302ce8d6SAndre Oppermann 				goto dropunlock;
1299a160e630SAndre Oppermann 			}
1300be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1301a160e630SAndre Oppermann 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
1302a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1303a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13048d573cc1SAndre Oppermann 					"Connection attempt from/to multicast "
1305773673c1SAndre Oppermann 					"address ignored\n", s, __func__);
1306302ce8d6SAndre Oppermann 				goto dropunlock;
1307a160e630SAndre Oppermann 			}
1308b287c6c7SBjoern A. Zeeb 		}
1309db33b3e6SAndre Oppermann #endif
1310b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
1311b287c6c7SBjoern A. Zeeb 		else
1312b287c6c7SBjoern A. Zeeb #endif
1313b287c6c7SBjoern A. Zeeb #ifdef INET
1314b287c6c7SBjoern A. Zeeb 		{
1315db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1316a160e630SAndre Oppermann 			    ip->ip_dst.s_addr == ip->ip_src.s_addr) {
1317a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1318a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13198d573cc1SAndre Oppermann 					"Connection attempt from/to self "
1320773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1321302ce8d6SAndre Oppermann 				goto dropunlock;
1322a160e630SAndre Oppermann 			}
1323be2ac88cSJonathan Lemon 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
1324be2ac88cSJonathan Lemon 			    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
13252ca2159fSCrist J. Clark 			    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
1326a160e630SAndre Oppermann 			    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1327a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1328a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13298d573cc1SAndre Oppermann 					"Connection attempt from/to broad- "
1330773673c1SAndre Oppermann 					"or multicast address ignored\n",
1331a160e630SAndre Oppermann 					s, __func__);
1332302ce8d6SAndre Oppermann 				goto dropunlock;
1333c068736aSJeffrey Hsu 			}
1334a160e630SAndre Oppermann 		}
1335b287c6c7SBjoern A. Zeeb #endif
1336be2ac88cSJonathan Lemon 		/*
1337db33b3e6SAndre Oppermann 		 * SYN appears to be valid.  Create compressed TCP state
1338db33b3e6SAndre Oppermann 		 * for syncache.
1339be2ac88cSJonathan Lemon 		 */
13403c653157SHartmut Brandt #ifdef TCPDEBUG
13413c653157SHartmut Brandt 		if (so->so_options & SO_DEBUG)
13423c653157SHartmut Brandt 			tcp_trace(TA_INPUT, ostate, tp,
13433c653157SHartmut Brandt 			    (void *)tcp_saveipgen, &tcp_savetcp, 0);
13443c653157SHartmut Brandt #endif
1345f72167f4SAndre Oppermann 		tcp_dooptions(&to, optp, optlen, TO_SYN);
134609fe6320SNavdeep Parhar 		syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL);
1347be2ac88cSJonathan Lemon 		/*
13484d6e7130SAndre Oppermann 		 * Entry added to syncache and mbuf consumed.
1349a7c7f2a7SJohn Baldwin 		 * Only the listen socket is unlocked by syncache_add().
1350be2ac88cSJonathan Lemon 		 */
1351a7c7f2a7SJohn Baldwin 		if (ti_locked == TI_WLOCKED) {
1352a7c7f2a7SJohn Baldwin 			INP_INFO_WUNLOCK(&V_tcbinfo);
1353a7c7f2a7SJohn Baldwin 			ti_locked = TI_UNLOCKED;
1354a7c7f2a7SJohn Baldwin 		}
1355603724d3SBjoern A. Zeeb 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
13568f5a8818SKevin Lo 		return (IPPROTO_DONE);
1357982c1675SAndre Oppermann 	} else if (tp->t_state == TCPS_LISTEN) {
1358982c1675SAndre Oppermann 		/*
1359982c1675SAndre Oppermann 		 * When a listen socket is torn down the SO_ACCEPTCONN
1360982c1675SAndre Oppermann 		 * flag is removed first while connections are drained
1361982c1675SAndre Oppermann 		 * from the accept queue in a unlock/lock cycle of the
1362982c1675SAndre Oppermann 		 * ACCEPT_LOCK, opening a race condition allowing a SYN
1363982c1675SAndre Oppermann 		 * attempt go through unhandled.
1364982c1675SAndre Oppermann 		 */
1365982c1675SAndre Oppermann 		goto dropunlock;
1366f76fcf6dSJeffrey Hsu 	}
1367db33b3e6SAndre Oppermann 
13682903309aSAttilio Rao #ifdef TCP_SIGNATURE
13692903309aSAttilio Rao 	if (sig_checked == 0)  {
13702903309aSAttilio Rao 		tcp_dooptions(&to, optp, optlen,
13712903309aSAttilio Rao 		    (thflags & TH_SYN) ? TO_SYN : 0);
13722903309aSAttilio Rao 		if (!tcp_signature_verify_input(m, off0, tlen, optlen, &to,
13732903309aSAttilio Rao 		    th, tp->t_flags)) {
13742903309aSAttilio Rao 
13752903309aSAttilio Rao 			/*
13762903309aSAttilio Rao 			 * In SYN_SENT state if it receives an RST, it is
13772903309aSAttilio Rao 			 * allowed for further processing.
13782903309aSAttilio Rao 			 */
13792903309aSAttilio Rao 			if ((thflags & TH_RST) == 0 ||
13802903309aSAttilio Rao 			    (tp->t_state == TCPS_SYN_SENT) == 0)
13812903309aSAttilio Rao 				goto dropunlock;
13822903309aSAttilio Rao 		}
13832903309aSAttilio Rao 		sig_checked = 1;
13842903309aSAttilio Rao 	}
13852903309aSAttilio Rao #endif
13862903309aSAttilio Rao 
1387fa22ce15SAdrian Chadd 	TCP_PROBE5(receive, NULL, tp, mtod(m, const char *), tp, th);
138857f60867SMark Johnston 
1389302ce8d6SAndre Oppermann 	/*
13908d573cc1SAndre Oppermann 	 * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later
13918d573cc1SAndre Oppermann 	 * state.  tcp_do_segment() always consumes the mbuf chain, unlocks
13928d573cc1SAndre Oppermann 	 * the inpcb, and unlocks pcbinfo.
1393302ce8d6SAndre Oppermann 	 */
1394252ca428SRobert Watson 	tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked);
1395603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
13968f5a8818SKevin Lo 	return (IPPROTO_DONE);
1397be2ac88cSJonathan Lemon 
1398302ce8d6SAndre Oppermann dropwithreset:
1399fa22ce15SAdrian Chadd 	TCP_PROBE5(receive, NULL, tp, mtod(m, const char *), tp, th);
140057f60867SMark Johnston 
1401fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED) {
1402dd8ac7f9SRobert Watson 		INP_INFO_WUNLOCK(&V_tcbinfo);
1403252ca428SRobert Watson 		ti_locked = TI_UNLOCKED;
1404fa046d87SRobert Watson 	}
1405fa046d87SRobert Watson #ifdef INVARIANTS
1406fa046d87SRobert Watson 	else {
1407fa046d87SRobert Watson 		KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropwithreset "
1408fa046d87SRobert Watson 		    "ti_locked: %d", __func__, ti_locked));
1409fa046d87SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1410fa046d87SRobert Watson 	}
1411fa046d87SRobert Watson #endif
1412014ea782SRobert Watson 
1413014ea782SRobert Watson 	if (inp != NULL) {
1414302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
1415014ea782SRobert Watson 		INP_WUNLOCK(inp);
1416dd8ac7f9SRobert Watson 	} else
1417014ea782SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
1418302ce8d6SAndre Oppermann 	m = NULL;	/* mbuf chain got consumed. */
1419014ea782SRobert Watson 	goto drop;
1420014ea782SRobert Watson 
1421302ce8d6SAndre Oppermann dropunlock:
142257f60867SMark Johnston 	if (m != NULL)
1423fa22ce15SAdrian Chadd 		TCP_PROBE5(receive, NULL, tp, mtod(m, const char *), tp, th);
142457f60867SMark Johnston 
1425fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED) {
1426252ca428SRobert Watson 		INP_INFO_WUNLOCK(&V_tcbinfo);
1427252ca428SRobert Watson 		ti_locked = TI_UNLOCKED;
1428fa046d87SRobert Watson 	}
1429fa046d87SRobert Watson #ifdef INVARIANTS
1430fa046d87SRobert Watson 	else {
1431fa046d87SRobert Watson 		KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropunlock "
1432fa046d87SRobert Watson 		    "ti_locked: %d", __func__, ti_locked));
1433fa046d87SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1434fa046d87SRobert Watson 	}
1435fa046d87SRobert Watson #endif
1436252ca428SRobert Watson 
14379fa198beSAndre Oppermann 	if (inp != NULL)
14388501a69cSRobert Watson 		INP_WUNLOCK(inp);
1439014ea782SRobert Watson 
1440302ce8d6SAndre Oppermann drop:
1441603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1442a160e630SAndre Oppermann 	if (s != NULL)
1443a160e630SAndre Oppermann 		free(s, M_TCPLOG);
1444302ce8d6SAndre Oppermann 	if (m != NULL)
1445302ce8d6SAndre Oppermann 		m_freem(m);
14468f5a8818SKevin Lo 	return (IPPROTO_DONE);
1447302ce8d6SAndre Oppermann }
1448302ce8d6SAndre Oppermann 
1449679d9708SAndre Oppermann static void
1450302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
1451252ca428SRobert Watson     struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos,
1452252ca428SRobert Watson     int ti_locked)
1453302ce8d6SAndre Oppermann {
1454302ce8d6SAndre Oppermann 	int thflags, acked, ourfinisacked, needoutput = 0;
1455302ce8d6SAndre Oppermann 	int rstreason, todrop, win;
1456302ce8d6SAndre Oppermann 	u_long tiwin;
145707dacf03SAndre Oppermann 	char *s;
145807dacf03SAndre Oppermann 	struct in_conninfo *inc;
1459c11a15bfSGleb Smirnoff 	struct mbuf *mfree;
1460302ce8d6SAndre Oppermann 	struct tcpopt to;
1461302ce8d6SAndre Oppermann 
146214739780SMaxim Konovalov #ifdef TCPDEBUG
146314739780SMaxim Konovalov 	/*
146414739780SMaxim Konovalov 	 * The size of tcp_saveipgen must be the size of the max ip header,
146514739780SMaxim Konovalov 	 * now IPv6.
146614739780SMaxim Konovalov 	 */
146714739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
146814739780SMaxim Konovalov 	struct tcphdr tcp_savetcp;
146914739780SMaxim Konovalov 	short ostate = 0;
147014739780SMaxim Konovalov #endif
1471302ce8d6SAndre Oppermann 	thflags = th->th_flags;
147207dacf03SAndre Oppermann 	inc = &tp->t_inpcb->inp_inc;
1473d64a46eaSLawrence Stewart 	tp->sackhint.last_sack_ack = 0;
1474302ce8d6SAndre Oppermann 
1475252ca428SRobert Watson 	/*
1476252ca428SRobert Watson 	 * If this is either a state-changing packet or current state isn't
1477252ca428SRobert Watson 	 * established, we require a write lock on tcbinfo.  Otherwise, we
14780989f56cSRobert Watson 	 * allow the tcbinfo to be in either alocked or unlocked, as the
14790989f56cSRobert Watson 	 * caller may have unnecessarily acquired a write lock due to a race.
1480252ca428SRobert Watson 	 */
1481252ca428SRobert Watson 	if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 ||
1482252ca428SRobert Watson 	    tp->t_state != TCPS_ESTABLISHED) {
1483252ca428SRobert Watson 		KASSERT(ti_locked == TI_WLOCKED, ("%s ti_locked %d for "
1484252ca428SRobert Watson 		    "SYN/FIN/RST/!EST", __func__, ti_locked));
1485603724d3SBjoern A. Zeeb 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1486252ca428SRobert Watson 	} else {
1487252ca428SRobert Watson #ifdef INVARIANTS
1488fa046d87SRobert Watson 		if (ti_locked == TI_WLOCKED)
1489252ca428SRobert Watson 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1490fa046d87SRobert Watson 		else {
1491fa046d87SRobert Watson 			KASSERT(ti_locked == TI_UNLOCKED, ("%s: EST "
1492fa046d87SRobert Watson 			    "ti_locked: %d", __func__, ti_locked));
1493fa046d87SRobert Watson 			INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1494fa046d87SRobert Watson 		}
1495252ca428SRobert Watson #endif
1496252ca428SRobert Watson 	}
14978501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1498679d9708SAndre Oppermann 	KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
1499679d9708SAndre Oppermann 	    __func__));
1500679d9708SAndre Oppermann 	KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
1501679d9708SAndre Oppermann 	    __func__));
1502df8bae1dSRodney W. Grimes 
1503df8bae1dSRodney W. Grimes 	/*
1504df8bae1dSRodney W. Grimes 	 * Segment received on connection.
1505df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
1506e8949f74SAndre Oppermann 	 * XXX: This should be done after segment
1507e8949f74SAndre Oppermann 	 * validation to ignore broken/spoofed segs.
1508df8bae1dSRodney W. Grimes 	 */
15099b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
15107ff19458SPaul Traina 	if (TCPS_HAVEESTABLISHED(tp->t_state))
15119077f387SGleb Smirnoff 		tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
1512df8bae1dSRodney W. Grimes 
1513df8bae1dSRodney W. Grimes 	/*
1514464fcfbcSAndre Oppermann 	 * Unscale the window into a 32-bit value.
1515e8949f74SAndre Oppermann 	 * For the SYN_SENT state the scale is zero.
1516464fcfbcSAndre Oppermann 	 */
1517464fcfbcSAndre Oppermann 	tiwin = th->th_win << tp->snd_scale;
1518464fcfbcSAndre Oppermann 
1519464fcfbcSAndre Oppermann 	/*
1520f2512ba1SRui Paulo 	 * TCP ECN processing.
1521f2512ba1SRui Paulo 	 */
1522f2512ba1SRui Paulo 	if (tp->t_flags & TF_ECN_PERMIT) {
15239c251892SRui Paulo 		if (thflags & TH_CWR)
15249c251892SRui Paulo 			tp->t_flags &= ~TF_ECN_SND_ECE;
1525f2512ba1SRui Paulo 		switch (iptos & IPTOS_ECN_MASK) {
1526f2512ba1SRui Paulo 		case IPTOS_ECN_CE:
1527f2512ba1SRui Paulo 			tp->t_flags |= TF_ECN_SND_ECE;
152878b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ce);
1529f2512ba1SRui Paulo 			break;
1530f2512ba1SRui Paulo 		case IPTOS_ECN_ECT0:
153178b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect0);
1532f2512ba1SRui Paulo 			break;
1533f2512ba1SRui Paulo 		case IPTOS_ECN_ECT1:
153478b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect1);
1535f2512ba1SRui Paulo 			break;
1536f2512ba1SRui Paulo 		}
1537dbc42409SLawrence Stewart 		/* Congestion experienced. */
1538dbc42409SLawrence Stewart 		if (thflags & TH_ECE) {
1539dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_ECN);
1540f2512ba1SRui Paulo 		}
1541f2512ba1SRui Paulo 	}
1542f2512ba1SRui Paulo 
1543f2512ba1SRui Paulo 	/*
1544f72167f4SAndre Oppermann 	 * Parse options on any incoming segment.
1545f72167f4SAndre Oppermann 	 */
1546302ce8d6SAndre Oppermann 	tcp_dooptions(&to, (u_char *)(th + 1),
1547302ce8d6SAndre Oppermann 	    (th->th_off << 2) - sizeof(struct tcphdr),
1548302ce8d6SAndre Oppermann 	    (thflags & TH_SYN) ? TO_SYN : 0);
1549f72167f4SAndre Oppermann 
1550f72167f4SAndre Oppermann 	/*
1551f72167f4SAndre Oppermann 	 * If echoed timestamp is later than the current time,
1552bf6d304aSAndre Oppermann 	 * fall back to non RFC1323 RTT calculation.  Normalize
1553bf6d304aSAndre Oppermann 	 * timestamp if syncookies were used when this connection
1554bf6d304aSAndre Oppermann 	 * was established.
1555f72167f4SAndre Oppermann 	 */
1556bf6d304aSAndre Oppermann 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
1557e16fa5caSJohn-Mark Gurney 		to.to_tsecr -= tp->ts_offset;
1558d8951c8aSBjoern A. Zeeb 		if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks()))
1559f72167f4SAndre Oppermann 			to.to_tsecr = 0;
1560bf6d304aSAndre Oppermann 	}
156107dacf03SAndre Oppermann 	/*
156207dacf03SAndre Oppermann 	 * If timestamps were negotiated during SYN/ACK they should
156307dacf03SAndre Oppermann 	 * appear on every segment during this session and vice versa.
156407dacf03SAndre Oppermann 	 */
156507dacf03SAndre Oppermann 	if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) {
156607dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
156707dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: Timestamp missing, "
156807dacf03SAndre Oppermann 			    "no action\n", s, __func__);
156907dacf03SAndre Oppermann 			free(s, M_TCPLOG);
157007dacf03SAndre Oppermann 		}
157107dacf03SAndre Oppermann 	}
157207dacf03SAndre Oppermann 	if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) {
157307dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
157407dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: Timestamp not expected, "
157507dacf03SAndre Oppermann 			    "no action\n", s, __func__);
157607dacf03SAndre Oppermann 			free(s, M_TCPLOG);
157707dacf03SAndre Oppermann 		}
157807dacf03SAndre Oppermann 	}
1579f72167f4SAndre Oppermann 
1580f72167f4SAndre Oppermann 	/*
158197d8d152SAndre Oppermann 	 * Process options only when we get SYN/ACK back. The SYN case
158297d8d152SAndre Oppermann 	 * for incoming connections is handled in tcp_syncache.
15835396d0f8SAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
15845396d0f8SAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.
158597d8d152SAndre Oppermann 	 * XXX this is traditional behavior, may need to be cleaned up.
1586df8bae1dSRodney W. Grimes 	 */
15870a389eabSSimon L. B. Nielsen 	if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
1588464fcfbcSAndre Oppermann 		if ((to.to_flags & TOF_SCALE) &&
1589464fcfbcSAndre Oppermann 		    (tp->t_flags & TF_REQ_SCALE)) {
1590be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
159102a1a643SAndre Oppermann 			tp->snd_scale = to.to_wscale;
1592be2ac88cSJonathan Lemon 		}
15935396d0f8SAndre Oppermann 		/*
15945396d0f8SAndre Oppermann 		 * Initial send window.  It will be updated with
15955396d0f8SAndre Oppermann 		 * the next incoming segment to the scaled value.
15965396d0f8SAndre Oppermann 		 */
15975396d0f8SAndre Oppermann 		tp->snd_wnd = th->th_win;
1598be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_TS) {
1599be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
1600be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
1601d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1602be2ac88cSJonathan Lemon 		}
1603be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_MSS)
1604be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
16053529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
16063529149eSAndre Oppermann 		    (to.to_flags & TOF_SACKPERM) == 0)
16073529149eSAndre Oppermann 			tp->t_flags &= ~TF_SACK_PERMIT;
16086d90faf3SPaul Saab 	}
16096d90faf3SPaul Saab 
1610df8bae1dSRodney W. Grimes 	/*
1611df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
1612df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
1613df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
1614df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
1615df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
1616df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
1617df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
1618df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
1619df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
1620df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
1621df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
1622df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
1623a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
1624c5ad39b9SAndre Oppermann 	 * Since we check for TCPS_ESTABLISHED first, it can only
1625a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
1626df8bae1dSRodney W. Grimes 	 */
1627df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
1628c5ad39b9SAndre Oppermann 	    th->th_seq == tp->rcv_nxt &&
1629fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1630c5ad39b9SAndre Oppermann 	    tp->snd_nxt == tp->snd_max &&
1631c5ad39b9SAndre Oppermann 	    tiwin && tiwin == tp->snd_wnd &&
1632a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
1633e407b67bSGleb Smirnoff 	    tp->t_segq == NULL && ((to.to_flags & TOF_TS) == 0 ||
1634c5ad39b9SAndre Oppermann 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) {
1635df8bae1dSRodney W. Grimes 
1636df8bae1dSRodney W. Grimes 		/*
1637df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
1638df8bae1dSRodney W. Grimes 		 * record the timestamp.
1639a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
1640a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1641df8bae1dSRodney W. Grimes 		 */
1642be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
1643fb59c426SYoshinobu Inoue 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1644d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1645a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
1646df8bae1dSRodney W. Grimes 		}
1647df8bae1dSRodney W. Grimes 
1648fb59c426SYoshinobu Inoue 		if (tlen == 0) {
1649fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
1650fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
1651dbc42409SLawrence Stewart 			    !IN_RECOVERY(tp->t_flags) &&
1652fc30a251SAndre Oppermann 			    (to.to_flags & TOF_SACK) == 0 &&
1653dbc42409SLawrence Stewart 			    TAILQ_EMPTY(&tp->snd_holes)) {
1654df8bae1dSRodney W. Grimes 				/*
1655e8949f74SAndre Oppermann 				 * This is a pure ack for outstanding data.
1656df8bae1dSRodney W. Grimes 				 */
1657fa046d87SRobert Watson 				if (ti_locked == TI_WLOCKED)
1658252ca428SRobert Watson 					INP_INFO_WUNLOCK(&V_tcbinfo);
1659252ca428SRobert Watson 				ti_locked = TI_UNLOCKED;
1660252ca428SRobert Watson 
166178b50714SRobert Watson 				TCPSTAT_INC(tcps_predack);
1662252ca428SRobert Watson 
16639b8b58e0SJonathan Lemon 				/*
1664e8949f74SAndre Oppermann 				 * "bad retransmit" recovery.
16659b8b58e0SJonathan Lemon 				 */
16669b8b58e0SJonathan Lemon 				if (tp->t_rxtshift == 1 &&
1667672dc4aeSJohn Baldwin 				    tp->t_flags & TF_PREVVALID &&
16686dfb8b31SJohn Baldwin 				    (int)(ticks - tp->t_badrxtwin) < 0) {
1669dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_RTO_ERR);
16709b8b58e0SJonathan Lemon 				}
1671fa55172bSMatthew Dillon 
1672fa55172bSMatthew Dillon 				/*
1673fa55172bSMatthew Dillon 				 * Recalculate the transmit timer / rtt.
1674fa55172bSMatthew Dillon 				 *
1675fa55172bSMatthew Dillon 				 * Some boxes send broken timestamp replies
1676fa55172bSMatthew Dillon 				 * during the SYN+ACK phase, ignore
1677fa55172bSMatthew Dillon 				 * timestamps of 0 or we could calculate a
1678fa55172bSMatthew Dillon 				 * huge RTT and blow up the retransmit timer.
1679fa55172bSMatthew Dillon 				 */
1680fa55172bSMatthew Dillon 				if ((to.to_flags & TOF_TS) != 0 &&
1681fa55172bSMatthew Dillon 				    to.to_tsecr) {
1682d8951c8aSBjoern A. Zeeb 					u_int t;
1683d8951c8aSBjoern A. Zeeb 
1684d8951c8aSBjoern A. Zeeb 					t = tcp_ts_getticks() - to.to_tsecr;
1685d8951c8aSBjoern A. Zeeb 					if (!tp->t_rttlow || tp->t_rttlow > t)
1686d8951c8aSBjoern A. Zeeb 						tp->t_rttlow = t;
1687a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
1688d8951c8aSBjoern A. Zeeb 					    TCP_TS_TO_TICKS(t) + 1);
1689fa55172bSMatthew Dillon 				} else if (tp->t_rtttime &&
1690fa55172bSMatthew Dillon 				    SEQ_GT(th->th_ack, tp->t_rtseq)) {
1691eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1692eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - tp->t_rtttime)
1693eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - tp->t_rtttime;
1694c068736aSJeffrey Hsu 					tcp_xmit_timer(tp,
1695c068736aSJeffrey Hsu 							ticks - tp->t_rtttime);
1696fa55172bSMatthew Dillon 				}
1697dbc42409SLawrence Stewart 				acked = BYTES_THIS_ACK(tp, th);
169839bc9de5SLawrence Stewart 
169939bc9de5SLawrence Stewart 				/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
170039bc9de5SLawrence Stewart 				hhook_run_tcp_est_in(tp, th, &to);
170139bc9de5SLawrence Stewart 
170278b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvackpack);
170378b50714SRobert Watson 				TCPSTAT_ADD(tcps_rcvackbyte, acked);
1704df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
17059d11646dSJeffrey Hsu 				if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
17069d11646dSJeffrey Hsu 				    SEQ_LEQ(th->th_ack, tp->snd_recover))
17079d11646dSJeffrey Hsu 					tp->snd_recover = th->th_ack - 1;
1708dbc42409SLawrence Stewart 
1709dbc42409SLawrence Stewart 				/*
1710dbc42409SLawrence Stewart 				 * Let the congestion control algorithm update
1711dbc42409SLawrence Stewart 				 * congestion control related information. This
1712dbc42409SLawrence Stewart 				 * typically means increasing the congestion
1713dbc42409SLawrence Stewart 				 * window.
1714dbc42409SLawrence Stewart 				 */
1715dbc42409SLawrence Stewart 				cc_ack_received(tp, th, CC_ACK);
1716dbc42409SLawrence Stewart 
17179d11646dSJeffrey Hsu 				tp->snd_una = th->th_ack;
17181645d090SJeff Roberson 				/*
1719e8949f74SAndre Oppermann 				 * Pull snd_wl2 up to prevent seq wrap relative
17201645d090SJeff Roberson 				 * to th_ack.
17211645d090SJeff Roberson 				 */
1722cb942153SJeffrey Hsu 				tp->snd_wl2 = th->th_ack;
1723b5addd85SJeffrey Hsu 				tp->t_dupacks = 0;
1724df8bae1dSRodney W. Grimes 				m_freem(m);
1725e8949f74SAndre Oppermann 				ND6_HINT(tp); /* Some progress has been made. */
1726df8bae1dSRodney W. Grimes 
1727df8bae1dSRodney W. Grimes 				/*
1728df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
1729df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
1730df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
1731df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
1732df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
1733df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
1734df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
1735e8949f74SAndre Oppermann 				 */
17363c653157SHartmut Brandt #ifdef TCPDEBUG
17373c653157SHartmut Brandt 				if (so->so_options & SO_DEBUG)
17383c653157SHartmut Brandt 					tcp_trace(TA_INPUT, ostate, tp,
17393c653157SHartmut Brandt 					    (void *)tcp_saveipgen,
17403c653157SHartmut Brandt 					    &tcp_savetcp, 0);
17413c653157SHartmut Brandt #endif
1742df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
1743b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
1744b8152ba7SAndre Oppermann 				else if (!tcp_timer_active(tp, TT_PERSIST))
1745b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT,
1746b8152ba7SAndre Oppermann 						      tp->t_rxtcur);
1747df8bae1dSRodney W. Grimes 				sowwakeup(so);
1748df8bae1dSRodney W. Grimes 				if (so->so_snd.sb_cc)
1749df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1750a14c749fSJonathan Lemon 				goto check_delack;
1751df8bae1dSRodney W. Grimes 			}
1752fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1753fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
17546741ecf5SAndre Oppermann 			int newsize = 0;	/* automatic sockbuf scaling */
17556741ecf5SAndre Oppermann 
1756df8bae1dSRodney W. Grimes 			/*
1757252ca428SRobert Watson 			 * This is a pure, in-sequence data packet with
1758252ca428SRobert Watson 			 * nothing on the reassembly queue and we have enough
1759252ca428SRobert Watson 			 * buffer space to take it.
1760df8bae1dSRodney W. Grimes 			 */
1761fa046d87SRobert Watson 			if (ti_locked == TI_WLOCKED)
1762252ca428SRobert Watson 				INP_INFO_WUNLOCK(&V_tcbinfo);
1763252ca428SRobert Watson 			ti_locked = TI_UNLOCKED;
1764252ca428SRobert Watson 
17656d90faf3SPaul Saab 			/* Clean receiver SACK report if present */
17663529149eSAndre Oppermann 			if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks)
17676d90faf3SPaul Saab 				tcp_clean_sackreport(tp);
176878b50714SRobert Watson 			TCPSTAT_INC(tcps_preddat);
1769fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
17701645d090SJeff Roberson 			/*
17711645d090SJeff Roberson 			 * Pull snd_wl1 up to prevent seq wrap relative to
17721645d090SJeff Roberson 			 * th_seq.
17731645d090SJeff Roberson 			 */
177460ee3bb2SAndre Oppermann 			tp->snd_wl1 = th->th_seq;
17751645d090SJeff Roberson 			/*
17761645d090SJeff Roberson 			 * Pull rcv_up up to prevent seq wrap relative to
17771645d090SJeff Roberson 			 * rcv_nxt.
17781645d090SJeff Roberson 			 */
17791645d090SJeff Roberson 			tp->rcv_up = tp->rcv_nxt;
178078b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpack);
178178b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
1782e8949f74SAndre Oppermann 			ND6_HINT(tp);	/* Some progress has been made */
17833c653157SHartmut Brandt #ifdef TCPDEBUG
17843c653157SHartmut Brandt 			if (so->so_options & SO_DEBUG)
17853c653157SHartmut Brandt 				tcp_trace(TA_INPUT, ostate, tp,
17863c653157SHartmut Brandt 				    (void *)tcp_saveipgen, &tcp_savetcp, 0);
17873c653157SHartmut Brandt #endif
17886741ecf5SAndre Oppermann 		/*
17896741ecf5SAndre Oppermann 		 * Automatic sizing of receive socket buffer.  Often the send
17906741ecf5SAndre Oppermann 		 * buffer size is not optimally adjusted to the actual network
17916741ecf5SAndre Oppermann 		 * conditions at hand (delay bandwidth product).  Setting the
17926741ecf5SAndre Oppermann 		 * buffer size too small limits throughput on links with high
17936741ecf5SAndre Oppermann 		 * bandwidth and high delay (eg. trans-continental/oceanic links).
17946741ecf5SAndre Oppermann 		 *
17956741ecf5SAndre Oppermann 		 * On the receive side the socket buffer memory is only rarely
17966741ecf5SAndre Oppermann 		 * used to any significant extent.  This allows us to be much
17976741ecf5SAndre Oppermann 		 * more aggressive in scaling the receive socket buffer.  For
17986741ecf5SAndre Oppermann 		 * the case that the buffer space is actually used to a large
17996741ecf5SAndre Oppermann 		 * extent and we run out of kernel memory we can simply drop
18006741ecf5SAndre Oppermann 		 * the new segments; TCP on the sender will just retransmit it
18016741ecf5SAndre Oppermann 		 * later.  Setting the buffer size too big may only consume too
18026741ecf5SAndre Oppermann 		 * much kernel memory if the application doesn't read() from
18036741ecf5SAndre Oppermann 		 * the socket or packet loss or reordering makes use of the
18046741ecf5SAndre Oppermann 		 * reassembly queue.
18056741ecf5SAndre Oppermann 		 *
18066741ecf5SAndre Oppermann 		 * The criteria to step up the receive buffer one notch are:
1807f7469d3eSHiren Panchasara 		 *  1. Application has not set receive buffer size with
1808f7469d3eSHiren Panchasara 		 *     SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE.
1809f7469d3eSHiren Panchasara 		 *  2. the number of bytes received during the time it takes
18106741ecf5SAndre Oppermann 		 *     one timestamp to be reflected back to us (the RTT);
1811f7469d3eSHiren Panchasara 		 *  3. received bytes per RTT is within seven eighth of the
18126741ecf5SAndre Oppermann 		 *     current socket buffer size;
1813f7469d3eSHiren Panchasara 		 *  4. receive buffer size has not hit maximal automatic size;
18146741ecf5SAndre Oppermann 		 *
18156741ecf5SAndre Oppermann 		 * This algorithm does one step per RTT at most and only if
18166741ecf5SAndre Oppermann 		 * we receive a bulk stream w/o packet losses or reorderings.
18176741ecf5SAndre Oppermann 		 * Shrinking the buffer during idle times is not necessary as
18186741ecf5SAndre Oppermann 		 * it doesn't consume any memory when idle.
18196741ecf5SAndre Oppermann 		 *
18206741ecf5SAndre Oppermann 		 * TODO: Only step up if the application is actually serving
18216741ecf5SAndre Oppermann 		 * the buffer to better manage the socket buffer resources.
1822df8bae1dSRodney W. Grimes 		 */
1823603724d3SBjoern A. Zeeb 			if (V_tcp_do_autorcvbuf &&
18246741ecf5SAndre Oppermann 			    to.to_tsecr &&
18256741ecf5SAndre Oppermann 			    (so->so_rcv.sb_flags & SB_AUTOSIZE)) {
18268502ec25SAndre Oppermann 				if (TSTMP_GT(to.to_tsecr, tp->rfbuf_ts) &&
18276741ecf5SAndre Oppermann 				    to.to_tsecr - tp->rfbuf_ts < hz) {
18286741ecf5SAndre Oppermann 					if (tp->rfbuf_cnt >
18296741ecf5SAndre Oppermann 					    (so->so_rcv.sb_hiwat / 8 * 7) &&
18306741ecf5SAndre Oppermann 					    so->so_rcv.sb_hiwat <
1831603724d3SBjoern A. Zeeb 					    V_tcp_autorcvbuf_max) {
18326741ecf5SAndre Oppermann 						newsize =
18336741ecf5SAndre Oppermann 						    min(so->so_rcv.sb_hiwat +
1834603724d3SBjoern A. Zeeb 						    V_tcp_autorcvbuf_inc,
1835603724d3SBjoern A. Zeeb 						    V_tcp_autorcvbuf_max);
18366741ecf5SAndre Oppermann 					}
18376741ecf5SAndre Oppermann 					/* Start over with next RTT. */
18386741ecf5SAndre Oppermann 					tp->rfbuf_ts = 0;
18396741ecf5SAndre Oppermann 					tp->rfbuf_cnt = 0;
18406741ecf5SAndre Oppermann 				} else
18416741ecf5SAndre Oppermann 					tp->rfbuf_cnt += tlen;	/* add up */
18426741ecf5SAndre Oppermann 			}
18436741ecf5SAndre Oppermann 
18446741ecf5SAndre Oppermann 			/* Add data to socket buffer. */
18451e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
1846c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1847c1c36a2cSMike Silbersack 				m_freem(m);
1848c1c36a2cSMike Silbersack 			} else {
18496741ecf5SAndre Oppermann 				/*
18506741ecf5SAndre Oppermann 				 * Set new socket buffer size.
18516741ecf5SAndre Oppermann 				 * Give up when limit is reached.
18526741ecf5SAndre Oppermann 				 */
18536741ecf5SAndre Oppermann 				if (newsize)
18546741ecf5SAndre Oppermann 					if (!sbreserve_locked(&so->so_rcv,
18556c8286e4SRobert Watson 					    newsize, so, NULL))
18566741ecf5SAndre Oppermann 						so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1857fb59c426SYoshinobu Inoue 				m_adj(m, drop_hdrlen);	/* delayed header drop */
18581e4d7da7SRobert Watson 				sbappendstream_locked(&so->so_rcv, m);
1859c1c36a2cSMike Silbersack 			}
1860e8949f74SAndre Oppermann 			/* NB: sorwakeup_locked() does an implicit unlock. */
18611e4d7da7SRobert Watson 			sorwakeup_locked(so);
1862c1e5a6e5SAndre Oppermann 			if (DELAY_ACK(tp, tlen)) {
18633bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
1864f498eeeeSDavid Greenman 			} else {
1865e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
1866e612a582SDavid Greenman 				tcp_output(tp);
1867e612a582SDavid Greenman 			}
1868a14c749fSJonathan Lemon 			goto check_delack;
1869df8bae1dSRodney W. Grimes 		}
1870df8bae1dSRodney W. Grimes 	}
1871df8bae1dSRodney W. Grimes 
1872df8bae1dSRodney W. Grimes 	/*
1873df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1874df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1875df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1876df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1877df8bae1dSRodney W. Grimes 	 */
1878df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1879df8bae1dSRodney W. Grimes 	if (win < 0)
1880df8bae1dSRodney W. Grimes 		win = 0;
188166e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1882df8bae1dSRodney W. Grimes 
18836741ecf5SAndre Oppermann 	/* Reset receive buffer auto scaling when not in bulk receive mode. */
18846741ecf5SAndre Oppermann 	tp->rfbuf_ts = 0;
18856741ecf5SAndre Oppermann 	tp->rfbuf_cnt = 0;
18866741ecf5SAndre Oppermann 
1887df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1888df8bae1dSRodney W. Grimes 
1889df8bae1dSRodney W. Grimes 	/*
1890764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1891764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1892764d8cefSBill Fenner 	 */
1893764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1894fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1895fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1896a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1897a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1898a57815efSBosko Milekic 				goto dropwithreset;
1899a57815efSBosko Milekic 		}
1900764d8cefSBill Fenner 		break;
1901764d8cefSBill Fenner 
1902764d8cefSBill Fenner 	/*
1903df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
1904df8bae1dSRodney W. Grimes 	 *	if seg contains an ACK, but not for our SYN, drop the input.
1905df8bae1dSRodney W. Grimes 	 *	if seg contains a RST, then drop the connection.
1906df8bae1dSRodney W. Grimes 	 *	if seg does not contain SYN, then drop it.
1907df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
1908df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
1909df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
1910f2512ba1SRui Paulo 	 *	if seg contains an ECE and ECN support is enabled, the stream
1911f2512ba1SRui Paulo 	 *	    is ECN capable.
1912df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1913df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
1914df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
1915df8bae1dSRodney W. Grimes 	 */
1916df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
1917fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1918fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
1919fb59c426SYoshinobu Inoue 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1920a57815efSBosko Milekic 			rstreason = BANDLIM_UNLIMITED;
1921a0292f23SGarrett Wollman 			goto dropwithreset;
1922a0292f23SGarrett Wollman 		}
192357f60867SMark Johnston 		if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) {
1924d9fae5abSAndriy Gapon 			TCP_PROBE5(connect__refused, NULL, tp,
1925fa22ce15SAdrian Chadd 			    mtod(m, const char *), tp, th);
1926df8bae1dSRodney W. Grimes 			tp = tcp_drop(tp, ECONNREFUSED);
192757f60867SMark Johnston 		}
19280ca3f933SAndre Oppermann 		if (thflags & TH_RST)
1929df8bae1dSRodney W. Grimes 			goto drop;
19300ca3f933SAndre Oppermann 		if (!(thflags & TH_SYN))
1931df8bae1dSRodney W. Grimes 			goto drop;
1932464fcfbcSAndre Oppermann 
1933fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
1934df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
1935fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
193678b50714SRobert Watson 			TCPSTAT_INC(tcps_connects);
1937845799c1SAndras Olah 			soisconnected(so);
1938c488362eSRobert Watson #ifdef MAC
193930d239bcSRobert Watson 			mac_socketpeer_set_from_mbuf(m, so);
1940c488362eSRobert Watson #endif
1941845799c1SAndras Olah 			/* Do window scaling on this connection? */
1942845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1943845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1944845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
1945845799c1SAndras Olah 			}
1946766282cbSJohn Baldwin 			tp->rcv_adv += imin(tp->rcv_wnd,
1947766282cbSJohn Baldwin 			    TCP_MAXWIN << tp->rcv_scale);
1948a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
1949a0292f23SGarrett Wollman 			/*
1950a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
1951a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
1952a0292f23SGarrett Wollman 			 */
1953c1e5a6e5SAndre Oppermann 			if (DELAY_ACK(tp, tlen) && tlen != 0)
1954b8152ba7SAndre Oppermann 				tcp_timer_activate(tp, TT_DELACK,
1955b8152ba7SAndre Oppermann 				    tcp_delacktime);
1956a0292f23SGarrett Wollman 			else
1957a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
1958f2512ba1SRui Paulo 
1959603724d3SBjoern A. Zeeb 			if ((thflags & TH_ECE) && V_tcp_do_ecn) {
1960f2512ba1SRui Paulo 				tp->t_flags |= TF_ECN_PERMIT;
196178b50714SRobert Watson 				TCPSTAT_INC(tcps_ecn_shs);
1962f2512ba1SRui Paulo 			}
1963f2512ba1SRui Paulo 
1964a0292f23SGarrett Wollman 			/*
1965a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
1966a0292f23SGarrett Wollman 			 * Transitions:
1967a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
1968a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
1969a0292f23SGarrett Wollman 			 */
19709b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
1971a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
197257f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_1);
1973a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
1974fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
19757ff19458SPaul Traina 			} else {
197657f60867SMark Johnston 				tcp_state_change(tp, TCPS_ESTABLISHED);
1977d9fae5abSAndriy Gapon 				TCP_PROBE5(connect__established, NULL, tp,
1978fa22ce15SAdrian Chadd 				    mtod(m, const char *), tp, th);
1979dbc42409SLawrence Stewart 				cc_conn_init(tp);
19809077f387SGleb Smirnoff 				tcp_timer_activate(tp, TT_KEEP,
19819077f387SGleb Smirnoff 				    TP_KEEPIDLE(tp));
19827ff19458SPaul Traina 			}
1983a0292f23SGarrett Wollman 		} else {
1984a0292f23SGarrett Wollman 			/*
1985c068736aSJeffrey Hsu 			 * Received initial SYN in SYN-SENT[*] state =>
1986153edc50SHiren Panchasara 			 * simultaneous open.
1987c068736aSJeffrey Hsu 			 * If it succeeds, connection is * half-synchronized.
1988c068736aSJeffrey Hsu 			 * Otherwise, do 3-way handshake:
1989a0292f23SGarrett Wollman 			 *        SYN-SENT -> SYN-RECEIVED
1990a0292f23SGarrett Wollman 			 *        SYN-SENT* -> SYN-RECEIVED*
1991a0292f23SGarrett Wollman 			 */
19924b8e98d6SQing Li 			tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
1993b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
199457f60867SMark Johnston 			tcp_state_change(tp, TCPS_SYN_RECEIVED);
1995a0292f23SGarrett Wollman 		}
1996df8bae1dSRodney W. Grimes 
1997252ca428SRobert Watson 		KASSERT(ti_locked == TI_WLOCKED, ("%s: trimthenstep6: "
1998252ca428SRobert Watson 		    "ti_locked %d", __func__, ti_locked));
1999252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
20008501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
20017cfc6904SRobert Watson 
2002df8bae1dSRodney W. Grimes 		/*
2003fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
2004df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
2005df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
2006df8bae1dSRodney W. Grimes 		 */
2007fb59c426SYoshinobu Inoue 		th->th_seq++;
2008fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
2009fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
2010df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
2011fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
2012fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
201378b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpackafterwin);
201478b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2015df8bae1dSRodney W. Grimes 		}
2016fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
2017fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
2018a0292f23SGarrett Wollman 		/*
2019a0292f23SGarrett Wollman 		 * Client side of transaction: already sent SYN and data.
2020a0292f23SGarrett Wollman 		 * If the remote host used T/TCP to validate the SYN,
2021a0292f23SGarrett Wollman 		 * our data will be ACK'd; if so, enter normal data segment
2022a0292f23SGarrett Wollman 		 * processing in the middle of step 5, ack processing.
2023a0292f23SGarrett Wollman 		 * Otherwise, goto step 6.
2024a0292f23SGarrett Wollman 		 */
2025fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK)
2026a0292f23SGarrett Wollman 			goto process_ACK;
2027c068736aSJeffrey Hsu 
2028df8bae1dSRodney W. Grimes 		goto step6;
2029c068736aSJeffrey Hsu 
2030a0292f23SGarrett Wollman 	/*
2031a0292f23SGarrett Wollman 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
2032c94c54e4SAndre Oppermann 	 *      do normal processing.
2033a0292f23SGarrett Wollman 	 *
2034c94c54e4SAndre Oppermann 	 * NB: Leftover from RFC1644 T/TCP.  Cases to be reused later.
2035a0292f23SGarrett Wollman 	 */
2036a0292f23SGarrett Wollman 	case TCPS_LAST_ACK:
2037a0292f23SGarrett Wollman 	case TCPS_CLOSING:
2038a0292f23SGarrett Wollman 		break;  /* continue normal processing */
2039df8bae1dSRodney W. Grimes 	}
2040df8bae1dSRodney W. Grimes 
2041df8bae1dSRodney W. Grimes 	/*
2042df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
204380ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
204480ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
204580ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
204680ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
204780ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
204880ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
204980ab7c0eSGarrett Wollman 	 * killing a connection).
205080ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
2051a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
2052df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
2053df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
2054df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
205580ab7c0eSGarrett Wollman 	 */
2056fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
20573220a212SGleb Smirnoff 		/*
20583220a212SGleb Smirnoff 		 * RFC5961 Section 3.2
20593220a212SGleb Smirnoff 		 *
20603220a212SGleb Smirnoff 		 * - RST drops connection only if SEG.SEQ == RCV.NXT.
20613220a212SGleb Smirnoff 		 * - If RST is in window, we send challenge ACK.
20623220a212SGleb Smirnoff 		 *
20633220a212SGleb Smirnoff 		 * Note: to take into account delayed ACKs, we should
20643220a212SGleb Smirnoff 		 *   test against last_ack_sent instead of rcv_nxt.
20653220a212SGleb Smirnoff 		 * Note 2: we handle special case of closed window, not
20663220a212SGleb Smirnoff 		 *   covered by the RFC.
20673220a212SGleb Smirnoff 		 */
20683220a212SGleb Smirnoff 		if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
20693220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
20703220a212SGleb Smirnoff 		    (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) {
207180ab7c0eSGarrett Wollman 
20723220a212SGleb Smirnoff 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
20733220a212SGleb Smirnoff 			KASSERT(ti_locked == TI_WLOCKED,
20743220a212SGleb Smirnoff 			    ("%s: TH_RST ti_locked %d, th %p tp %p",
20753220a212SGleb Smirnoff 			    __func__, ti_locked, th, tp));
20763220a212SGleb Smirnoff 			KASSERT(tp->t_state != TCPS_SYN_SENT,
20773220a212SGleb Smirnoff 			    ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p",
20783220a212SGleb Smirnoff 			    __func__, th, tp));
20793220a212SGleb Smirnoff 
20803220a212SGleb Smirnoff 			if (V_tcp_insecure_rst ||
20813220a212SGleb Smirnoff 			    tp->last_ack_sent == th->th_seq) {
20823220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_drops);
20833220a212SGleb Smirnoff 				/* Drop the connection. */
20843220a212SGleb Smirnoff 				switch (tp->t_state) {
208580ab7c0eSGarrett Wollman 				case TCPS_SYN_RECEIVED:
208680ab7c0eSGarrett Wollman 					so->so_error = ECONNREFUSED;
208780ab7c0eSGarrett Wollman 					goto close;
208880ab7c0eSGarrett Wollman 				case TCPS_ESTABLISHED:
208980ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_1:
209080ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_2:
209180ab7c0eSGarrett Wollman 				case TCPS_CLOSE_WAIT:
209280ab7c0eSGarrett Wollman 					so->so_error = ECONNRESET;
209380ab7c0eSGarrett Wollman 				close:
209457f60867SMark Johnston 					tcp_state_change(tp, TCPS_CLOSED);
20953220a212SGleb Smirnoff 					/* FALLTHROUGH */
20963220a212SGleb Smirnoff 				default:
209780ab7c0eSGarrett Wollman 					tp = tcp_close(tp);
20983220a212SGleb Smirnoff 				}
20993220a212SGleb Smirnoff 			} else {
21003220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_badrst);
21013220a212SGleb Smirnoff 				/* Send challenge ACK. */
21023220a212SGleb Smirnoff 				tcp_respond(tp, mtod(m, void *), th, m,
21033220a212SGleb Smirnoff 				    tp->rcv_nxt, tp->snd_nxt, TH_ACK);
21043220a212SGleb Smirnoff 				tp->last_ack_sent = tp->rcv_nxt;
21053220a212SGleb Smirnoff 				m = NULL;
21063220a212SGleb Smirnoff 			}
21073220a212SGleb Smirnoff 		}
21083220a212SGleb Smirnoff 		goto drop;
21093220a212SGleb Smirnoff 	}
211080ab7c0eSGarrett Wollman 
21113220a212SGleb Smirnoff 	/*
21123220a212SGleb Smirnoff 	 * RFC5961 Section 4.2
21133220a212SGleb Smirnoff 	 * Send challenge ACK for any SYN in synchronized state.
21143220a212SGleb Smirnoff 	 */
21153220a212SGleb Smirnoff 	if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT) {
2116252ca428SRobert Watson 		KASSERT(ti_locked == TI_WLOCKED,
21173220a212SGleb Smirnoff 		    ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked));
2118252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2119252ca428SRobert Watson 
21203220a212SGleb Smirnoff 		TCPSTAT_INC(tcps_badsyn);
21213220a212SGleb Smirnoff 		if (V_tcp_insecure_syn &&
21223220a212SGleb Smirnoff 		    SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
21233220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
21243220a212SGleb Smirnoff 			tp = tcp_drop(tp, ECONNRESET);
21253220a212SGleb Smirnoff 			rstreason = BANDLIM_UNLIMITED;
21263220a212SGleb Smirnoff 		} else {
21273220a212SGleb Smirnoff 			/* Send challenge ACK. */
21283220a212SGleb Smirnoff 			tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt,
21293220a212SGleb Smirnoff 			    tp->snd_nxt, TH_ACK);
21303220a212SGleb Smirnoff 			tp->last_ack_sent = tp->rcv_nxt;
21313220a212SGleb Smirnoff 			m = NULL;
213280ab7c0eSGarrett Wollman 		}
213380ab7c0eSGarrett Wollman 		goto drop;
213480ab7c0eSGarrett Wollman 	}
213580ab7c0eSGarrett Wollman 
213680ab7c0eSGarrett Wollman 	/*
2137df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
2138df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
2139df8bae1dSRodney W. Grimes 	 */
2140be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
214180ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
2142df8bae1dSRodney W. Grimes 
2143df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
2144d8951c8aSBjoern A. Zeeb 		if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
2145df8bae1dSRodney W. Grimes 			/*
2146df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
2147df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
2148df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
2149df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
2150df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
2151df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
2152df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
2153df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
2154df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
2155df8bae1dSRodney W. Grimes 			 */
2156df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
2157df8bae1dSRodney W. Grimes 		} else {
215878b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
215978b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, tlen);
216078b50714SRobert Watson 			TCPSTAT_INC(tcps_pawsdrop);
216107fd333dSMatthew Dillon 			if (tlen)
2162df8bae1dSRodney W. Grimes 				goto dropafterack;
21631ab4789dSMatthew Dillon 			goto drop;
21641ab4789dSMatthew Dillon 		}
2165df8bae1dSRodney W. Grimes 	}
2166df8bae1dSRodney W. Grimes 
2167a0292f23SGarrett Wollman 	/*
216880ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
216980ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
217080ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
217180ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
217280ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
217380ab7c0eSGarrett Wollman 	 */
2174a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
2175a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2176a57815efSBosko Milekic 		goto dropwithreset;
2177a57815efSBosko Milekic 	}
217880ab7c0eSGarrett Wollman 
2179fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
2180df8bae1dSRodney W. Grimes 	if (todrop > 0) {
2181831ad37eSXin LI 		if (thflags & TH_SYN) {
2182fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
2183fb59c426SYoshinobu Inoue 			th->th_seq++;
2184fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
2185fb59c426SYoshinobu Inoue 				th->th_urp--;
2186df8bae1dSRodney W. Grimes 			else
2187fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
2188df8bae1dSRodney W. Grimes 			todrop--;
2189df8bae1dSRodney W. Grimes 		}
2190df8bae1dSRodney W. Grimes 		/*
2191dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
2192df8bae1dSRodney W. Grimes 		 */
2193fb59c426SYoshinobu Inoue 		if (todrop > tlen
2194fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
2195dac20301SGarrett Wollman 			/*
2196dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
2197dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
2198dac20301SGarrett Wollman 			 * of sequence; drop it.
2199dac20301SGarrett Wollman 			 */
2200fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
2201dac20301SGarrett Wollman 
2202df8bae1dSRodney W. Grimes 			/*
2203dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
2204dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
2205df8bae1dSRodney W. Grimes 			 */
2206dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
2207fb59c426SYoshinobu Inoue 			todrop = tlen;
220878b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
220978b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, todrop);
2210df8bae1dSRodney W. Grimes 		} else {
221178b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpartduppack);
221278b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop);
2213df8bae1dSRodney W. Grimes 		}
2214fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
2215fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
2216fb59c426SYoshinobu Inoue 		tlen -= todrop;
2217fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
2218fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
2219df8bae1dSRodney W. Grimes 		else {
2220fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
2221fb59c426SYoshinobu Inoue 			th->th_urp = 0;
2222df8bae1dSRodney W. Grimes 		}
2223df8bae1dSRodney W. Grimes 	}
2224df8bae1dSRodney W. Grimes 
2225df8bae1dSRodney W. Grimes 	/*
2226df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
2227df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
2228df8bae1dSRodney W. Grimes 	 */
2229df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
2230fb59c426SYoshinobu Inoue 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
2231252ca428SRobert Watson 		KASSERT(ti_locked == TI_WLOCKED, ("%s: SS_NOFDEREF && "
2232252ca428SRobert Watson 		    "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked));
2233252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2234252ca428SRobert Watson 
223507dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
223607dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data "
223707dacf03SAndre Oppermann 			    "after socket was closed, "
223807dacf03SAndre Oppermann 			    "sending RST and removing tcpcb\n",
2239e31d8aa3SMike Silbersack 			    s, __func__, tcpstates[tp->t_state], tlen);
2240773673c1SAndre Oppermann 			free(s, M_TCPLOG);
2241773673c1SAndre Oppermann 		}
2242df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
224378b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvafterclose);
2244a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
2245df8bae1dSRodney W. Grimes 		goto dropwithreset;
2246df8bae1dSRodney W. Grimes 	}
2247df8bae1dSRodney W. Grimes 
2248df8bae1dSRodney W. Grimes 	/*
2249df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
2250df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
2251df8bae1dSRodney W. Grimes 	 */
2252fb59c426SYoshinobu Inoue 	todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
2253df8bae1dSRodney W. Grimes 	if (todrop > 0) {
225478b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvpackafterwin);
2255fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
225678b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen);
2257df8bae1dSRodney W. Grimes 			/*
2258df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
2259df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
2260df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
2261df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
2262df8bae1dSRodney W. Grimes 			 * and ack.
2263df8bae1dSRodney W. Grimes 			 */
2264fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
2265df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
226678b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvwinprobe);
2267df8bae1dSRodney W. Grimes 			} else
2268df8bae1dSRodney W. Grimes 				goto dropafterack;
2269df8bae1dSRodney W. Grimes 		} else
227078b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2271df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
2272fb59c426SYoshinobu Inoue 		tlen -= todrop;
2273fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
2274df8bae1dSRodney W. Grimes 	}
2275df8bae1dSRodney W. Grimes 
2276df8bae1dSRodney W. Grimes 	/*
2277df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
2278df8bae1dSRodney W. Grimes 	 * record its timestamp.
2279cf09195bSPaul Saab 	 * NOTE:
2280cf09195bSPaul Saab 	 * 1) That the test incorporates suggestions from the latest
2281a0292f23SGarrett Wollman 	 *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
2282cf09195bSPaul Saab 	 * 2) That updating only on newer timestamps interferes with
2283cf09195bSPaul Saab 	 *    our earlier PAWS tests, so this check should be solely
2284cf09195bSPaul Saab 	 *    predicated on the sequence space of this segment.
2285cf09195bSPaul Saab 	 * 3) That we modify the segment boundary check to be
2286cf09195bSPaul Saab 	 *        Last.ACK.Sent <= SEG.SEQ + SEG.Len
2287cf09195bSPaul Saab 	 *    instead of RFC1323's
2288cf09195bSPaul Saab 	 *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
2289cf09195bSPaul Saab 	 *    This modified check allows us to overcome RFC1323's
2290cf09195bSPaul Saab 	 *    limitations as described in Stevens TCP/IP Illustrated
2291cf09195bSPaul Saab 	 *    Vol. 2 p.869. In such cases, we can still calculate the
2292cf09195bSPaul Saab 	 *    RTT correctly when RCV.NXT == Last.ACK.Sent.
2293df8bae1dSRodney W. Grimes 	 */
2294be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
2295cf09195bSPaul Saab 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
2296cf09195bSPaul Saab 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
2297cf09195bSPaul Saab 		((thflags & (TH_SYN|TH_FIN)) != 0))) {
2298d8951c8aSBjoern A. Zeeb 		tp->ts_recent_age = tcp_ts_getticks();
2299a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
2300df8bae1dSRodney W. Grimes 	}
2301df8bae1dSRodney W. Grimes 
2302df8bae1dSRodney W. Grimes 	/*
2303a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
2304a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
2305a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
2306a0292f23SGarrett Wollman 	 */
2307fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
2308a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
2309a0292f23SGarrett Wollman 		    (tp->t_flags & TF_NEEDSYN))
2310a0292f23SGarrett Wollman 			goto step6;
23115e1aa279SDavid Malone 		else if (tp->t_flags & TF_ACKNOW)
23125e1aa279SDavid Malone 			goto dropafterack;
2313a0292f23SGarrett Wollman 		else
2314a0292f23SGarrett Wollman 			goto drop;
2315a0292f23SGarrett Wollman 	}
2316df8bae1dSRodney W. Grimes 
2317df8bae1dSRodney W. Grimes 	/*
2318df8bae1dSRodney W. Grimes 	 * Ack processing.
2319df8bae1dSRodney W. Grimes 	 */
2320df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2321df8bae1dSRodney W. Grimes 
2322df8bae1dSRodney W. Grimes 	/*
2323764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
2324764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
2325764d8cefSBill Fenner 	 * The ACK was checked above.
2326df8bae1dSRodney W. Grimes 	 */
2327df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2328a0292f23SGarrett Wollman 
232978b50714SRobert Watson 		TCPSTAT_INC(tcps_connects);
2330df8bae1dSRodney W. Grimes 		soisconnected(so);
2331df8bae1dSRodney W. Grimes 		/* Do window scaling? */
2332df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2333df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2334df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
2335464fcfbcSAndre Oppermann 			tp->snd_wnd = tiwin;
2336df8bae1dSRodney W. Grimes 		}
2337a0292f23SGarrett Wollman 		/*
2338a0292f23SGarrett Wollman 		 * Make transitions:
2339a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
2340a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
2341a0292f23SGarrett Wollman 		 */
23429b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
2343a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDFIN) {
234457f60867SMark Johnston 			tcp_state_change(tp, TCPS_FIN_WAIT_1);
2345a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDFIN;
23467ff19458SPaul Traina 		} else {
234757f60867SMark Johnston 			tcp_state_change(tp, TCPS_ESTABLISHED);
2348d9fae5abSAndriy Gapon 			TCP_PROBE5(accept__established, NULL, tp,
2349fa22ce15SAdrian Chadd 			    mtod(m, const char *), tp, th);
2350dbc42409SLawrence Stewart 			cc_conn_init(tp);
23519077f387SGleb Smirnoff 			tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
23527ff19458SPaul Traina 		}
2353a0292f23SGarrett Wollman 		/*
2354a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
2355a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
2356a0292f23SGarrett Wollman 		 */
2357fb59c426SYoshinobu Inoue 		if (tlen == 0 && (thflags & TH_FIN) == 0)
2358fb59c426SYoshinobu Inoue 			(void) tcp_reass(tp, (struct tcphdr *)0, 0,
2359a0292f23SGarrett Wollman 			    (struct mbuf *)0);
236060ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq - 1;
236193b0017fSPhilippe Charnier 		/* FALLTHROUGH */
2362df8bae1dSRodney W. Grimes 
2363df8bae1dSRodney W. Grimes 	/*
2364df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2365df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
2366fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
2367fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
2368df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
2369df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
2370df8bae1dSRodney W. Grimes 	 */
2371df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
2372df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
2373df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
2374df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
2375df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
2376df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
23775a53ca16SPaul Saab 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
237878b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvacktoomuch);
23795a53ca16SPaul Saab 			goto dropafterack;
23805a53ca16SPaul Saab 		}
23813529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
2382fc30a251SAndre Oppermann 		    ((to.to_flags & TOF_SACK) ||
2383fc30a251SAndre Oppermann 		     !TAILQ_EMPTY(&tp->snd_holes)))
23845a53ca16SPaul Saab 			tcp_sack_doack(tp, &to, th->th_ack);
238539bc9de5SLawrence Stewart 
238639bc9de5SLawrence Stewart 		/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
238739bc9de5SLawrence Stewart 		hhook_run_tcp_est_in(tp, th, &to);
238839bc9de5SLawrence Stewart 
2389fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
239062b90589SPeter Wemm 			if (tlen == 0 && tiwin == tp->snd_wnd) {
239162b90589SPeter Wemm 				/*
239262b90589SPeter Wemm 				 * If this is the first time we've seen a
239362b90589SPeter Wemm 				 * FIN from the remote, this is not a
239462b90589SPeter Wemm 				 * duplicate and it needs to be processed
239562b90589SPeter Wemm 				 * normally.  This happens during a
239662b90589SPeter Wemm 				 * simultaneous close.
239762b90589SPeter Wemm 				 */
239862b90589SPeter Wemm 				if ((thflags & TH_FIN) &&
239962b90589SPeter Wemm 				    (TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
240062b90589SPeter Wemm 					tp->t_dupacks = 0;
240162b90589SPeter Wemm 					break;
240262b90589SPeter Wemm 				}
240378b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvdupack);
2404df8bae1dSRodney W. Grimes 				/*
2405df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
2406df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
2407df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
24085f30ec9bSEitan Adler 				 * change and FIN isn't set),
24095f30ec9bSEitan Adler 				 * the ack is the biggest we've
2410df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
2411df8bae1dSRodney W. Grimes 				 * threshhold of them, assume a packet
2412df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
2413df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
2414df8bae1dSRodney W. Grimes 				 * window so we send only this one
2415df8bae1dSRodney W. Grimes 				 * packet.
2416df8bae1dSRodney W. Grimes 				 *
2417df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
2418df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
2419df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
2420df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
2421df8bae1dSRodney W. Grimes 				 * the new ssthresh).
2422df8bae1dSRodney W. Grimes 				 *
2423df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
2424df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
2425df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
2426df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
2427df8bae1dSRodney W. Grimes 				 * network.
2428f2512ba1SRui Paulo 				 *
2429f2512ba1SRui Paulo 				 * When using TCP ECN, notify the peer that
2430f2512ba1SRui Paulo 				 * we reduced the cwnd.
2431df8bae1dSRodney W. Grimes 				 */
2432b8152ba7SAndre Oppermann 				if (!tcp_timer_active(tp, TT_REXMT) ||
2433fb59c426SYoshinobu Inoue 				    th->th_ack != tp->snd_una)
2434df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
2435cb942153SJeffrey Hsu 				else if (++tp->t_dupacks > tcprexmtthresh ||
2436dbc42409SLawrence Stewart 				     IN_FASTRECOVERY(tp->t_flags)) {
2437dbc42409SLawrence Stewart 					cc_ack_received(tp, th, CC_DUPACK);
24383529149eSAndre Oppermann 					if ((tp->t_flags & TF_SACK_PERMIT) &&
2439dbc42409SLawrence Stewart 					    IN_FASTRECOVERY(tp->t_flags)) {
2440808f11b7SPaul Saab 						int awnd;
244125e6f9edSPaul Saab 
244225e6f9edSPaul Saab 						/*
244325e6f9edSPaul Saab 						 * Compute the amount of data in flight first.
244425e6f9edSPaul Saab 						 * We can inject new data into the pipe iff
244525e6f9edSPaul Saab 						 * we have less than 1/2 the original window's
244625e6f9edSPaul Saab 						 * worth of data in flight.
244725e6f9edSPaul Saab 						 */
2448808f11b7SPaul Saab 						awnd = (tp->snd_nxt - tp->snd_fack) +
24490077b016SPaul Saab 							tp->sackhint.sack_bytes_rexmit;
2450808f11b7SPaul Saab 						if (awnd < tp->snd_ssthresh) {
245125e6f9edSPaul Saab 							tp->snd_cwnd += tp->t_maxseg;
245225e6f9edSPaul Saab 							if (tp->snd_cwnd > tp->snd_ssthresh)
245325e6f9edSPaul Saab 								tp->snd_cwnd = tp->snd_ssthresh;
245425e6f9edSPaul Saab 						}
245525e6f9edSPaul Saab 					} else
245646f58482SJonathan Lemon 						tp->snd_cwnd += tp->t_maxseg;
245746f58482SJonathan Lemon 					(void) tcp_output(tp);
245846f58482SJonathan Lemon 					goto drop;
2459cb942153SJeffrey Hsu 				} else if (tp->t_dupacks == tcprexmtthresh) {
2460cb942153SJeffrey Hsu 					tcp_seq onxt = tp->snd_nxt;
2461a0445c2eSJayanth Vijayaraghavan 
2462a0445c2eSJayanth Vijayaraghavan 					/*
2463a0445c2eSJayanth Vijayaraghavan 					 * If we're doing sack, check to
2464a0445c2eSJayanth Vijayaraghavan 					 * see if we're already in sack
2465a0445c2eSJayanth Vijayaraghavan 					 * recovery. If we're not doing sack,
2466a0445c2eSJayanth Vijayaraghavan 					 * check to see if we're in newreno
2467a0445c2eSJayanth Vijayaraghavan 					 * recovery.
2468a0445c2eSJayanth Vijayaraghavan 					 */
24693529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
2470dbc42409SLawrence Stewart 						if (IN_FASTRECOVERY(tp->t_flags)) {
2471a0445c2eSJayanth Vijayaraghavan 							tp->t_dupacks = 0;
2472a0445c2eSJayanth Vijayaraghavan 							break;
2473a0445c2eSJayanth Vijayaraghavan 						}
2474dbc42409SLawrence Stewart 					} else {
2475a0445c2eSJayanth Vijayaraghavan 						if (SEQ_LEQ(th->th_ack,
24769d11646dSJeffrey Hsu 						    tp->snd_recover)) {
2477cb942153SJeffrey Hsu 							tp->t_dupacks = 0;
2478cb942153SJeffrey Hsu 							break;
247946f58482SJonathan Lemon 						}
2480a0445c2eSJayanth Vijayaraghavan 					}
2481dbc42409SLawrence Stewart 					/* Congestion signal before ack. */
2482dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_NDUPACK);
2483dbc42409SLawrence Stewart 					cc_ack_received(tp, th, CC_DUPACK);
2484b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
24859b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
24863529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
248778b50714SRobert Watson 						TCPSTAT_INC(
248878b50714SRobert Watson 						    tcps_sack_recovery_episode);
2489a55db2b6SPaul Saab 						tp->sack_newdata = tp->snd_nxt;
24908db456bfSPaul Saab 						tp->snd_cwnd = tp->t_maxseg;
24916d90faf3SPaul Saab 						(void) tcp_output(tp);
24926d90faf3SPaul Saab 						goto drop;
24936d90faf3SPaul Saab 					}
2494fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
2495df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->t_maxseg;
2496df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
249748d2549cSJeffrey Hsu 					KASSERT(tp->snd_limited <= 2,
2498302ce8d6SAndre Oppermann 					    ("%s: tp->snd_limited too big",
2499302ce8d6SAndre Oppermann 					    __func__));
2500df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
250148d2549cSJeffrey Hsu 					     tp->t_maxseg *
250248d2549cSJeffrey Hsu 					     (tp->t_dupacks - tp->snd_limited);
2503df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
2504df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
2505df8bae1dSRodney W. Grimes 					goto drop;
2506603724d3SBjoern A. Zeeb 				} else if (V_tcp_do_rfc3042) {
2507dbc42409SLawrence Stewart 					cc_ack_received(tp, th, CC_DUPACK);
2508582a954bSJeffrey Hsu 					u_long oldcwnd = tp->snd_cwnd;
250948d2549cSJeffrey Hsu 					tcp_seq oldsndmax = tp->snd_max;
251048d2549cSJeffrey Hsu 					u_int sent;
25115628dd08SAndre Oppermann 					int avail;
251289c02376SJeffrey Hsu 
2513582a954bSJeffrey Hsu 					KASSERT(tp->t_dupacks == 1 ||
2514582a954bSJeffrey Hsu 					    tp->t_dupacks == 2,
2515302ce8d6SAndre Oppermann 					    ("%s: dupacks not 1 or 2",
2516302ce8d6SAndre Oppermann 					    __func__));
251761a36e3dSJeffrey Hsu 					if (tp->t_dupacks == 1)
251848d2549cSJeffrey Hsu 						tp->snd_limited = 0;
251961a36e3dSJeffrey Hsu 					tp->snd_cwnd =
252061a36e3dSJeffrey Hsu 					    (tp->snd_nxt - tp->snd_una) +
252161a36e3dSJeffrey Hsu 					    (tp->t_dupacks - tp->snd_limited) *
252261a36e3dSJeffrey Hsu 					    tp->t_maxseg;
25235628dd08SAndre Oppermann 					/*
25245628dd08SAndre Oppermann 					 * Only call tcp_output when there
25255628dd08SAndre Oppermann 					 * is new data available to be sent.
25265628dd08SAndre Oppermann 					 * Otherwise we would send pure ACKs.
25275628dd08SAndre Oppermann 					 */
25285628dd08SAndre Oppermann 					SOCKBUF_LOCK(&so->so_snd);
25295628dd08SAndre Oppermann 					avail = so->so_snd.sb_cc -
25305628dd08SAndre Oppermann 					    (tp->snd_nxt - tp->snd_una);
25315628dd08SAndre Oppermann 					SOCKBUF_UNLOCK(&so->so_snd);
25325628dd08SAndre Oppermann 					if (avail > 0)
2533582a954bSJeffrey Hsu 						(void) tcp_output(tp);
253448d2549cSJeffrey Hsu 					sent = tp->snd_max - oldsndmax;
253548d2549cSJeffrey Hsu 					if (sent > tp->t_maxseg) {
253689c02376SJeffrey Hsu 						KASSERT((tp->t_dupacks == 2 &&
253789c02376SJeffrey Hsu 						    tp->snd_limited == 0) ||
253889c02376SJeffrey Hsu 						   (sent == tp->t_maxseg + 1 &&
253989c02376SJeffrey Hsu 						    tp->t_flags & TF_SENTFIN),
2540302ce8d6SAndre Oppermann 						    ("%s: sent too much",
2541302ce8d6SAndre Oppermann 						    __func__));
254248d2549cSJeffrey Hsu 						tp->snd_limited = 2;
254348d2549cSJeffrey Hsu 					} else if (sent > 0)
254448d2549cSJeffrey Hsu 						++tp->snd_limited;
2545582a954bSJeffrey Hsu 					tp->snd_cwnd = oldcwnd;
2546582a954bSJeffrey Hsu 					goto drop;
2547df8bae1dSRodney W. Grimes 				}
2548df8bae1dSRodney W. Grimes 			} else
2549df8bae1dSRodney W. Grimes 				tp->t_dupacks = 0;
2550df8bae1dSRodney W. Grimes 			break;
2551df8bae1dSRodney W. Grimes 		}
2552cb942153SJeffrey Hsu 
2553302ce8d6SAndre Oppermann 		KASSERT(SEQ_GT(th->th_ack, tp->snd_una),
2554302ce8d6SAndre Oppermann 		    ("%s: th_ack <= snd_una", __func__));
2555cb942153SJeffrey Hsu 
2556df8bae1dSRodney W. Grimes 		/*
2557df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
2558df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
2559df8bae1dSRodney W. Grimes 		 */
2560dbc42409SLawrence Stewart 		if (IN_FASTRECOVERY(tp->t_flags)) {
2561cb942153SJeffrey Hsu 			if (SEQ_LT(th->th_ack, tp->snd_recover)) {
25623529149eSAndre Oppermann 				if (tp->t_flags & TF_SACK_PERMIT)
25636d90faf3SPaul Saab 					tcp_sack_partialack(tp, th);
25646d90faf3SPaul Saab 				else
2565c068736aSJeffrey Hsu 					tcp_newreno_partial_ack(tp, th);
2566dbc42409SLawrence Stewart 			} else
2567dbc42409SLawrence Stewart 				cc_post_recovery(tp, th);
256846f58482SJonathan Lemon 		}
2569cb942153SJeffrey Hsu 		tp->t_dupacks = 0;
2570a0292f23SGarrett Wollman 		/*
2571a0292f23SGarrett Wollman 		 * If we reach this point, ACK is not a duplicate,
2572a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
2573a0292f23SGarrett Wollman 		 */
2574a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
2575a0292f23SGarrett Wollman 			/*
2576a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
2577a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
257807e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
257907e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
258007e43e10SAndras Olah 			 * we can do window scaling.
2581a0292f23SGarrett Wollman 			 */
2582a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
2583a0292f23SGarrett Wollman 			tp->snd_una++;
258407e43e10SAndras Olah 			/* Do window scaling? */
258507e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
258607e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
258707e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2588464fcfbcSAndre Oppermann 				/* Send window already scaled. */
258907e43e10SAndras Olah 			}
2590a0292f23SGarrett Wollman 		}
2591a0292f23SGarrett Wollman 
2592a0292f23SGarrett Wollman process_ACK:
25938501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
25947cfc6904SRobert Watson 
2595dbc42409SLawrence Stewart 		acked = BYTES_THIS_ACK(tp, th);
259678b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvackpack);
259778b50714SRobert Watson 		TCPSTAT_ADD(tcps_rcvackbyte, acked);
2598df8bae1dSRodney W. Grimes 
2599df8bae1dSRodney W. Grimes 		/*
26009b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
26019b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
26029b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
26039b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
26049b8b58e0SJonathan Lemon 		 * we left off.
26059b8b58e0SJonathan Lemon 		 */
2606672dc4aeSJohn Baldwin 		if (tp->t_rxtshift == 1 && tp->t_flags & TF_PREVVALID &&
2607672dc4aeSJohn Baldwin 		    (int)(ticks - tp->t_badrxtwin) < 0)
2608dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_RTO_ERR);
26099b8b58e0SJonathan Lemon 
26109b8b58e0SJonathan Lemon 		/*
2611df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
2612df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
2613df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
2614df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
2615df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
2616df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
2617df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
2618fa55172bSMatthew Dillon 		 *
2619fa55172bSMatthew Dillon 		 * Some boxes send broken timestamp replies
2620fa55172bSMatthew Dillon 		 * during the SYN+ACK phase, ignore
2621fa55172bSMatthew Dillon 		 * timestamps of 0 or we could calculate a
2622fa55172bSMatthew Dillon 		 * huge RTT and blow up the retransmit timer.
2623df8bae1dSRodney W. Grimes 		 */
2624d8951c8aSBjoern A. Zeeb 		if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) {
2625d8951c8aSBjoern A. Zeeb 			u_int t;
2626d8951c8aSBjoern A. Zeeb 
2627d8951c8aSBjoern A. Zeeb 			t = tcp_ts_getticks() - to.to_tsecr;
2628d8951c8aSBjoern A. Zeeb 			if (!tp->t_rttlow || tp->t_rttlow > t)
2629d8951c8aSBjoern A. Zeeb 				tp->t_rttlow = t;
2630d8951c8aSBjoern A. Zeeb 			tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1);
2631fa55172bSMatthew Dillon 		} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2632eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
2633eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - tp->t_rtttime;
26349b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2635fa55172bSMatthew Dillon 		}
2636df8bae1dSRodney W. Grimes 
2637df8bae1dSRodney W. Grimes 		/*
2638df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
2639df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
2640df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
2641df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
2642df8bae1dSRodney W. Grimes 		 */
2643fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
2644b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
2645df8bae1dSRodney W. Grimes 			needoutput = 1;
2646b8152ba7SAndre Oppermann 		} else if (!tcp_timer_active(tp, TT_PERSIST))
2647b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
2648a0292f23SGarrett Wollman 
2649a0292f23SGarrett Wollman 		/*
2650a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
2651a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
2652a0292f23SGarrett Wollman 		 */
2653a0292f23SGarrett Wollman 		if (acked == 0)
2654a0292f23SGarrett Wollman 			goto step6;
2655a0292f23SGarrett Wollman 
2656df8bae1dSRodney W. Grimes 		/*
2657dbc42409SLawrence Stewart 		 * Let the congestion control algorithm update congestion
2658dbc42409SLawrence Stewart 		 * control related information. This typically means increasing
2659dbc42409SLawrence Stewart 		 * the congestion window.
2660df8bae1dSRodney W. Grimes 		 */
2661dbc42409SLawrence Stewart 		cc_ack_received(tp, th, CC_ACK);
2662dbc42409SLawrence Stewart 
26635905999bSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
2664df8bae1dSRodney W. Grimes 		if (acked > so->so_snd.sb_cc) {
266560ee3bb2SAndre Oppermann 			tp->snd_wnd -= so->so_snd.sb_cc;
2666c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd,
2667c11a15bfSGleb Smirnoff 			    (int)so->so_snd.sb_cc);
2668df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
2669df8bae1dSRodney W. Grimes 		} else {
2670c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd, acked);
267160ee3bb2SAndre Oppermann 			tp->snd_wnd -= acked;
2672df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
2673df8bae1dSRodney W. Grimes 		}
2674564aab1fSAndre Oppermann 		/* NB: sowwakeup_locked() does an implicit unlock. */
26751e4d7da7SRobert Watson 		sowwakeup_locked(so);
2676c11a15bfSGleb Smirnoff 		m_freem(mfree);
2677e8949f74SAndre Oppermann 		/* Detect una wraparound. */
2678dbc42409SLawrence Stewart 		if (!IN_RECOVERY(tp->t_flags) &&
26799d11646dSJeffrey Hsu 		    SEQ_GT(tp->snd_una, tp->snd_recover) &&
26809d11646dSJeffrey Hsu 		    SEQ_LEQ(th->th_ack, tp->snd_recover))
26819d11646dSJeffrey Hsu 			tp->snd_recover = th->th_ack - 1;
2682dbc42409SLawrence Stewart 		/* XXXLAS: Can this be moved up into cc_post_recovery? */
2683dbc42409SLawrence Stewart 		if (IN_RECOVERY(tp->t_flags) &&
268424cb0f22SLawrence Stewart 		    SEQ_GEQ(th->th_ack, tp->snd_recover)) {
2685dbc42409SLawrence Stewart 			EXIT_RECOVERY(tp->t_flags);
268624cb0f22SLawrence Stewart 		}
2687fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
26883529149eSAndre Oppermann 		if (tp->t_flags & TF_SACK_PERMIT) {
26896d90faf3SPaul Saab 			if (SEQ_GT(tp->snd_una, tp->snd_recover))
26906d90faf3SPaul Saab 				tp->snd_recover = tp->snd_una;
26916d90faf3SPaul Saab 		}
2692df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2693df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
2694df8bae1dSRodney W. Grimes 
2695df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2696df8bae1dSRodney W. Grimes 
2697df8bae1dSRodney W. Grimes 		/*
2698df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
2699df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
2700df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
2701df8bae1dSRodney W. Grimes 		 */
2702df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2703df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2704df8bae1dSRodney W. Grimes 				/*
2705df8bae1dSRodney W. Grimes 				 * If we can't receive any more
2706df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
2707df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
2708df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
2709df8bae1dSRodney W. Grimes 				 * we'll hang forever.
2710e8949f74SAndre Oppermann 				 *
2711e8949f74SAndre Oppermann 				 * XXXjl:
2712340c35deSJonathan Lemon 				 * we should release the tp also, and use a
2713340c35deSJonathan Lemon 				 * compressed state.
2714340c35deSJonathan Lemon 				 */
2715c0b99ffaSRobert Watson 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
271603e49181SSeigo Tanimura 					soisdisconnected(so);
27179077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
27189077f387SGleb Smirnoff 					    (tcp_fast_finwait2_recycle ?
27199077f387SGleb Smirnoff 					    tcp_finwait2_timeout :
27209077f387SGleb Smirnoff 					    TP_MAXIDLE(tp)));
27214cc20ab1SSeigo Tanimura 				}
272257f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
2723df8bae1dSRodney W. Grimes 			}
2724df8bae1dSRodney W. Grimes 			break;
2725df8bae1dSRodney W. Grimes 
2726df8bae1dSRodney W. Grimes 		/*
2727df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
2728df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
2729df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
2730df8bae1dSRodney W. Grimes 		 * the segment.
2731df8bae1dSRodney W. Grimes 		 */
2732df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
2733df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2734252ca428SRobert Watson 				INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
273511a20fb8SJeffrey Hsu 				tcp_twstart(tp);
2736603724d3SBjoern A. Zeeb 				INP_INFO_WUNLOCK(&V_tcbinfo);
2737340c35deSJonathan Lemon 				m_freem(m);
2738679d9708SAndre Oppermann 				return;
2739df8bae1dSRodney W. Grimes 			}
2740df8bae1dSRodney W. Grimes 			break;
2741df8bae1dSRodney W. Grimes 
2742df8bae1dSRodney W. Grimes 		/*
2743df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
2744df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
2745df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
2746df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
2747df8bae1dSRodney W. Grimes 		 */
2748df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
2749df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2750252ca428SRobert Watson 				INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2751df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
2752df8bae1dSRodney W. Grimes 				goto drop;
2753df8bae1dSRodney W. Grimes 			}
2754df8bae1dSRodney W. Grimes 			break;
2755df8bae1dSRodney W. Grimes 		}
2756df8bae1dSRodney W. Grimes 	}
2757df8bae1dSRodney W. Grimes 
2758df8bae1dSRodney W. Grimes step6:
27598501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
27607cfc6904SRobert Watson 
2761df8bae1dSRodney W. Grimes 	/*
276260ee3bb2SAndre Oppermann 	 * Update window information.
276360ee3bb2SAndre Oppermann 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2764df8bae1dSRodney W. Grimes 	 */
276560ee3bb2SAndre Oppermann 	if ((thflags & TH_ACK) &&
276660ee3bb2SAndre Oppermann 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
276760ee3bb2SAndre Oppermann 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
276860ee3bb2SAndre Oppermann 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
276960ee3bb2SAndre Oppermann 		/* keep track of pure window updates */
277060ee3bb2SAndre Oppermann 		if (tlen == 0 &&
277160ee3bb2SAndre Oppermann 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
277278b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvwinupd);
2773df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
277460ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq;
277560ee3bb2SAndre Oppermann 		tp->snd_wl2 = th->th_ack;
2776df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
2777df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
277860ee3bb2SAndre Oppermann 		needoutput = 1;
2779df8bae1dSRodney W. Grimes 	}
2780df8bae1dSRodney W. Grimes 
2781df8bae1dSRodney W. Grimes 	/*
2782df8bae1dSRodney W. Grimes 	 * Process segments with URG.
2783df8bae1dSRodney W. Grimes 	 */
2784fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
2785df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2786df8bae1dSRodney W. Grimes 		/*
2787df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
2788df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
2789df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
2790df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
2791df8bae1dSRodney W. Grimes 		 */
279218ad5842SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
2793fb59c426SYoshinobu Inoue 		if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
2794fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
2795fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
279618ad5842SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);	/* XXX */
2797df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
2798df8bae1dSRodney W. Grimes 		}
2799df8bae1dSRodney W. Grimes 		/*
2800df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
2801df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
2802df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2803df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
2804df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
2805df8bae1dSRodney W. Grimes 		 *
2806df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
2807df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
2808df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
2809df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
2810df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
2811df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
2812df8bae1dSRodney W. Grimes 		 */
2813fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2814fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
2815df8bae1dSRodney W. Grimes 			so->so_oobmark = so->so_rcv.sb_cc +
2816df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
2817927c5ceaSRobert Watson 			if (so->so_oobmark == 0)
2818c0b99ffaSRobert Watson 				so->so_rcv.sb_state |= SBS_RCVATMARK;
2819df8bae1dSRodney W. Grimes 			sohasoutofband(so);
2820df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2821df8bae1dSRodney W. Grimes 		}
282218ad5842SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
2823df8bae1dSRodney W. Grimes 		/*
2824df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
2825df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
2826df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
2827df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
2828df8bae1dSRodney W. Grimes 		 */
282930613f56SJeffrey Hsu 		if (th->th_urp <= (u_long)tlen &&
283030613f56SJeffrey Hsu 		    !(so->so_options & SO_OOBINLINE)) {
283130613f56SJeffrey Hsu 			/* hdr drop is delayed */
283230613f56SJeffrey Hsu 			tcp_pulloutofband(so, th, m, drop_hdrlen);
283330613f56SJeffrey Hsu 		}
2834c068736aSJeffrey Hsu 	} else {
2835df8bae1dSRodney W. Grimes 		/*
2836df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
2837df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
2838df8bae1dSRodney W. Grimes 		 * with the receive window.
2839df8bae1dSRodney W. Grimes 		 */
2840df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2841df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
2842c068736aSJeffrey Hsu 	}
2843df8bae1dSRodney W. Grimes dodata:							/* XXX */
28448501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
28457cfc6904SRobert Watson 
2846df8bae1dSRodney W. Grimes 	/*
2847df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
2848df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
2849df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
2850df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
2851df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
2852df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
2853df8bae1dSRodney W. Grimes 	 */
2854fb59c426SYoshinobu Inoue 	if ((tlen || (thflags & TH_FIN)) &&
2855df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
285669e03620SPaul Saab 		tcp_seq save_start = th->th_seq;
2857fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
2858e4b64281SJesper Skriver 		/*
2859c068736aSJeffrey Hsu 		 * Insert segment which includes th into TCP reassembly queue
2860c068736aSJeffrey Hsu 		 * with control block tp.  Set thflags to whether reassembly now
2861c068736aSJeffrey Hsu 		 * includes a segment with FIN.  This handles the common case
2862c068736aSJeffrey Hsu 		 * inline (segment is the next to be received on an established
2863c068736aSJeffrey Hsu 		 * connection, and the queue is empty), avoiding linkage into
2864c068736aSJeffrey Hsu 		 * and removal from the queue and repetition of various
2865c068736aSJeffrey Hsu 		 * conversions.
2866c068736aSJeffrey Hsu 		 * Set DELACK for segments received in order, but ack
2867c068736aSJeffrey Hsu 		 * immediately when segments are out of order (so
2868c068736aSJeffrey Hsu 		 * fast retransmit can work).
2869e4b64281SJesper Skriver 		 */
2870e407b67bSGleb Smirnoff 		if (th->th_seq == tp->rcv_nxt && tp->t_segq == NULL &&
2871e4b64281SJesper Skriver 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
2872c1e5a6e5SAndre Oppermann 			if (DELAY_ACK(tp, tlen))
28733bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
2874e4b64281SJesper Skriver 			else
2875e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
2876e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
2877e4b64281SJesper Skriver 			thflags = th->th_flags & TH_FIN;
287878b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpack);
287978b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
2880e4b64281SJesper Skriver 			ND6_HINT(tp);
28811e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
2882c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
2883c1c36a2cSMike Silbersack 				m_freem(m);
2884c1c36a2cSMike Silbersack 			else
28851e4d7da7SRobert Watson 				sbappendstream_locked(&so->so_rcv, m);
2886e8949f74SAndre Oppermann 			/* NB: sorwakeup_locked() does an implicit unlock. */
28871e4d7da7SRobert Watson 			sorwakeup_locked(so);
2888e4b64281SJesper Skriver 		} else {
2889e8949f74SAndre Oppermann 			/*
2890e8949f74SAndre Oppermann 			 * XXX: Due to the header drop above "th" is
2891e8949f74SAndre Oppermann 			 * theoretically invalid by now.  Fortunately
2892e8949f74SAndre Oppermann 			 * m_adj() doesn't actually frees any mbufs
2893e8949f74SAndre Oppermann 			 * when trimming from the head.
2894e8949f74SAndre Oppermann 			 */
2895e4b64281SJesper Skriver 			thflags = tcp_reass(tp, th, &tlen, m);
2896e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
2897e4b64281SJesper Skriver 		}
28983529149eSAndre Oppermann 		if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT))
2899f194524fSAndre Oppermann 			tcp_update_sack_list(tp, save_start, save_start + tlen);
2900302ce8d6SAndre Oppermann #if 0
2901df8bae1dSRodney W. Grimes 		/*
2902df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
2903df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
2904df8bae1dSRodney W. Grimes 		 * buffer size.
2905302ce8d6SAndre Oppermann 		 * XXX: Unused.
2906df8bae1dSRodney W. Grimes 		 */
2907f701e30dSJohn Baldwin 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt))
2908df8bae1dSRodney W. Grimes 			len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2909f701e30dSJohn Baldwin 		else
2910f701e30dSJohn Baldwin 			len = so->so_rcv.sb_hiwat;
2911302ce8d6SAndre Oppermann #endif
2912df8bae1dSRodney W. Grimes 	} else {
2913df8bae1dSRodney W. Grimes 		m_freem(m);
2914fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
2915df8bae1dSRodney W. Grimes 	}
2916df8bae1dSRodney W. Grimes 
2917df8bae1dSRodney W. Grimes 	/*
2918df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
2919df8bae1dSRodney W. Grimes 	 * that the connection is closing.
2920df8bae1dSRodney W. Grimes 	 */
2921fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
2922df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2923df8bae1dSRodney W. Grimes 			socantrcvmore(so);
2924a0292f23SGarrett Wollman 			/*
2925a0292f23SGarrett Wollman 			 * If connection is half-synchronized
2926d3eede9dSAndras Olah 			 * (ie NEEDSYN flag on) then delay ACK,
2927a0292f23SGarrett Wollman 			 * so it may be piggybacked when SYN is sent.
2928a0292f23SGarrett Wollman 			 * Otherwise, since we received a FIN then no
2929a0292f23SGarrett Wollman 			 * more input can be expected, send ACK now.
2930a0292f23SGarrett Wollman 			 */
29313bfd6421SJonathan Lemon 			if (tp->t_flags & TF_NEEDSYN)
29323bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
2933a0292f23SGarrett Wollman 			else
2934df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
2935df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
2936df8bae1dSRodney W. Grimes 		}
2937df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2938df8bae1dSRodney W. Grimes 
2939df8bae1dSRodney W. Grimes 		/*
2940df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
2941df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
2942df8bae1dSRodney W. Grimes 		 */
2943df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
29449b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
29459b8b58e0SJonathan Lemon 			/* FALLTHROUGH */
2946df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
294757f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSE_WAIT);
2948df8bae1dSRodney W. Grimes 			break;
2949df8bae1dSRodney W. Grimes 
2950df8bae1dSRodney W. Grimes 		/*
2951df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2952df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
2953df8bae1dSRodney W. Grimes 		 */
2954df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
295557f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSING);
2956df8bae1dSRodney W. Grimes 			break;
2957df8bae1dSRodney W. Grimes 
2958df8bae1dSRodney W. Grimes 		/*
2959df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2960df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
2961df8bae1dSRodney W. Grimes 		 * standard timers.
2962df8bae1dSRodney W. Grimes 		 */
2963df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
2964252ca428SRobert Watson 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2965252ca428SRobert Watson 			KASSERT(ti_locked == TI_WLOCKED, ("%s: dodata "
2966252ca428SRobert Watson 			    "TCP_FIN_WAIT_2 ti_locked: %d", __func__,
2967252ca428SRobert Watson 			    ti_locked));
2968252ca428SRobert Watson 
2969340c35deSJonathan Lemon 			tcp_twstart(tp);
2970603724d3SBjoern A. Zeeb 			INP_INFO_WUNLOCK(&V_tcbinfo);
2971679d9708SAndre Oppermann 			return;
2972df8bae1dSRodney W. Grimes 		}
2973df8bae1dSRodney W. Grimes 	}
2974fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED)
2975603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
2976252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
2977252ca428SRobert Watson 
2978610ee2f9SDavid Greenman #ifdef TCPDEBUG
29794cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
2980fb59c426SYoshinobu Inoue 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
2981fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2982610ee2f9SDavid Greenman #endif
2983df8bae1dSRodney W. Grimes 
2984df8bae1dSRodney W. Grimes 	/*
2985df8bae1dSRodney W. Grimes 	 * Return any desired output.
2986df8bae1dSRodney W. Grimes 	 */
2987df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
2988df8bae1dSRodney W. Grimes 		(void) tcp_output(tp);
29897792ea27SJeffrey Hsu 
2990a14c749fSJonathan Lemon check_delack:
2991252ca428SRobert Watson 	KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d",
2992252ca428SRobert Watson 	    __func__, ti_locked));
2993603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
29948501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
2995252ca428SRobert Watson 
2996a14c749fSJonathan Lemon 	if (tp->t_flags & TF_DELACK) {
29973bfd6421SJonathan Lemon 		tp->t_flags &= ~TF_DELACK;
2998b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
29993bfd6421SJonathan Lemon 	}
30008501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
3001679d9708SAndre Oppermann 	return;
3002df8bae1dSRodney W. Grimes 
3003df8bae1dSRodney W. Grimes dropafterack:
3004df8bae1dSRodney W. Grimes 	/*
3005df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
3006df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
300780ab7c0eSGarrett Wollman 	 *
300880ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
300980ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
301080ab7c0eSGarrett Wollman 	 * RST have been dropped.
301180ab7c0eSGarrett Wollman 	 *
301280ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
301380ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
301480ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
301580ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
301680ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
301780ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
3018df8bae1dSRodney W. Grimes 	 */
3019fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
3020fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
3021a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
3022a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
3023a57815efSBosko Milekic 		goto dropwithreset;
3024a57815efSBosko Milekic 	}
3025a0292f23SGarrett Wollman #ifdef TCPDEBUG
30264cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
3027fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3028fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3029a0292f23SGarrett Wollman #endif
3030fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED)
3031603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
3032252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
3033252ca428SRobert Watson 
3034df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
3035df8bae1dSRodney W. Grimes 	(void) tcp_output(tp);
30368501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
3037d6915262SRobert Watson 	m_freem(m);
3038679d9708SAndre Oppermann 	return;
3039df8bae1dSRodney W. Grimes 
3040df8bae1dSRodney W. Grimes dropwithreset:
3041fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED)
3042dd8ac7f9SRobert Watson 		INP_INFO_WUNLOCK(&V_tcbinfo);
3043252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
3044a57815efSBosko Milekic 
3045a0ca0871SRobert Watson 	if (tp != NULL) {
3046302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
30478501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
3048dd8ac7f9SRobert Watson 	} else
3049a0ca0871SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
3050679d9708SAndre Oppermann 	return;
3051df8bae1dSRodney W. Grimes 
3052df8bae1dSRodney W. Grimes drop:
3053fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED) {
3054252ca428SRobert Watson 		INP_INFO_WUNLOCK(&V_tcbinfo);
3055fa046d87SRobert Watson 		ti_locked = TI_UNLOCKED;
3056fa046d87SRobert Watson 	}
3057252ca428SRobert Watson #ifdef INVARIANTS
3058252ca428SRobert Watson 	else
3059252ca428SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
3060252ca428SRobert Watson #endif
3061252ca428SRobert Watson 
3062df8bae1dSRodney W. Grimes 	/*
3063df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
3064df8bae1dSRodney W. Grimes 	 */
3065610ee2f9SDavid Greenman #ifdef TCPDEBUG
30661c53f806SRobert Watson 	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
3067fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3068fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3069a0292f23SGarrett Wollman #endif
30701c53f806SRobert Watson 	if (tp != NULL)
30718501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
3072d6915262SRobert Watson 	m_freem(m);
3073302ce8d6SAndre Oppermann }
3074302ce8d6SAndre Oppermann 
3075302ce8d6SAndre Oppermann /*
30760ca3f933SAndre Oppermann  * Issue RST and make ACK acceptable to originator of segment.
30770ca3f933SAndre Oppermann  * The mbuf must still include the original packet header.
30780ca3f933SAndre Oppermann  * tp may be NULL.
3079302ce8d6SAndre Oppermann  */
3080302ce8d6SAndre Oppermann static void
3081302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp,
3082302ce8d6SAndre Oppermann     int tlen, int rstreason)
3083302ce8d6SAndre Oppermann {
3084b287c6c7SBjoern A. Zeeb #ifdef INET
3085302ce8d6SAndre Oppermann 	struct ip *ip;
3086b287c6c7SBjoern A. Zeeb #endif
3087302ce8d6SAndre Oppermann #ifdef INET6
3088302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6;
3089302ce8d6SAndre Oppermann #endif
30907a3244ccSRobert Watson 
30917a3244ccSRobert Watson 	if (tp != NULL) {
30928501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
30937a3244ccSRobert Watson 	}
30947a3244ccSRobert Watson 
30950ca3f933SAndre Oppermann 	/* Don't bother if destination was broadcast/multicast. */
3096302ce8d6SAndre Oppermann 	if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
3097302ce8d6SAndre Oppermann 		goto drop;
3098302ce8d6SAndre Oppermann #ifdef INET6
3099302ce8d6SAndre Oppermann 	if (mtod(m, struct ip *)->ip_v == 6) {
3100302ce8d6SAndre Oppermann 		ip6 = mtod(m, struct ip6_hdr *);
3101302ce8d6SAndre Oppermann 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3102302ce8d6SAndre Oppermann 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3103302ce8d6SAndre Oppermann 			goto drop;
3104302ce8d6SAndre Oppermann 		/* IPv6 anycast check is done at tcp6_input() */
3105b287c6c7SBjoern A. Zeeb 	}
3106302ce8d6SAndre Oppermann #endif
3107b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3108b287c6c7SBjoern A. Zeeb 	else
3109b287c6c7SBjoern A. Zeeb #endif
3110b287c6c7SBjoern A. Zeeb #ifdef INET
3111302ce8d6SAndre Oppermann 	{
3112302ce8d6SAndre Oppermann 		ip = mtod(m, struct ip *);
3113302ce8d6SAndre Oppermann 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3114302ce8d6SAndre Oppermann 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3115302ce8d6SAndre Oppermann 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3116302ce8d6SAndre Oppermann 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
3117302ce8d6SAndre Oppermann 			goto drop;
3118302ce8d6SAndre Oppermann 	}
3119b287c6c7SBjoern A. Zeeb #endif
3120302ce8d6SAndre Oppermann 
3121302ce8d6SAndre Oppermann 	/* Perform bandwidth limiting. */
3122302ce8d6SAndre Oppermann 	if (badport_bandlim(rstreason) < 0)
3123302ce8d6SAndre Oppermann 		goto drop;
3124302ce8d6SAndre Oppermann 
3125302ce8d6SAndre Oppermann 	/* tcp_respond consumes the mbuf chain. */
3126302ce8d6SAndre Oppermann 	if (th->th_flags & TH_ACK) {
3127302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0,
3128302ce8d6SAndre Oppermann 		    th->th_ack, TH_RST);
3129302ce8d6SAndre Oppermann 	} else {
3130302ce8d6SAndre Oppermann 		if (th->th_flags & TH_SYN)
3131302ce8d6SAndre Oppermann 			tlen++;
3132302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
3133302ce8d6SAndre Oppermann 		    (tcp_seq)0, TH_RST|TH_ACK);
3134302ce8d6SAndre Oppermann 	}
3135302ce8d6SAndre Oppermann 	return;
3136302ce8d6SAndre Oppermann drop:
3137302ce8d6SAndre Oppermann 	m_freem(m);
3138df8bae1dSRodney W. Grimes }
3139df8bae1dSRodney W. Grimes 
3140be2ac88cSJonathan Lemon /*
3141be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
3142be2ac88cSJonathan Lemon  */
31430312fbe9SPoul-Henning Kamp static void
3144ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags)
3145df8bae1dSRodney W. Grimes {
3146df8bae1dSRodney W. Grimes 	int opt, optlen;
3147df8bae1dSRodney W. Grimes 
3148be2ac88cSJonathan Lemon 	to->to_flags = 0;
3149df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
3150df8bae1dSRodney W. Grimes 		opt = cp[0];
3151df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
3152df8bae1dSRodney W. Grimes 			break;
3153df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
3154df8bae1dSRodney W. Grimes 			optlen = 1;
3155df8bae1dSRodney W. Grimes 		else {
3156b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
3157b474779fSJun-ichiro itojun Hagino 				break;
3158df8bae1dSRodney W. Grimes 			optlen = cp[1];
3159b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
3160df8bae1dSRodney W. Grimes 				break;
3161df8bae1dSRodney W. Grimes 		}
3162df8bae1dSRodney W. Grimes 		switch (opt) {
3163df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
3164df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
3165df8bae1dSRodney W. Grimes 				continue;
3166f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3167df8bae1dSRodney W. Grimes 				continue;
3168be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
3169be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
3170be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
3171fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
3172df8bae1dSRodney W. Grimes 			break;
3173df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
3174df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
3175df8bae1dSRodney W. Grimes 				continue;
3176f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3177df8bae1dSRodney W. Grimes 				continue;
3178be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
317902a1a643SAndre Oppermann 			to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT);
3180df8bae1dSRodney W. Grimes 			break;
3181df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
3182df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
3183df8bae1dSRodney W. Grimes 				continue;
3184be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
3185a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
3186a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
3187fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
3188a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
3189a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
3190fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
3191df8bae1dSRodney W. Grimes 			break;
31921cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
31931cfd4b53SBruce M Simpson 		/*
31941cfd4b53SBruce M Simpson 		 * XXX In order to reply to a host which has set the
31951cfd4b53SBruce M Simpson 		 * TCP_SIGNATURE option in its initial SYN, we have to
31961cfd4b53SBruce M Simpson 		 * record the fact that the option was observed here
31971cfd4b53SBruce M Simpson 		 * for the syncache code to perform the correct response.
31981cfd4b53SBruce M Simpson 		 */
31991cfd4b53SBruce M Simpson 		case TCPOPT_SIGNATURE:
32001cfd4b53SBruce M Simpson 			if (optlen != TCPOLEN_SIGNATURE)
32011cfd4b53SBruce M Simpson 				continue;
3202df47e437SAndre Oppermann 			to->to_flags |= TOF_SIGNATURE;
3203df47e437SAndre Oppermann 			to->to_signature = cp + 2;
32041cfd4b53SBruce M Simpson 			break;
3205265ed012SBruce M Simpson #endif
32066d90faf3SPaul Saab 		case TCPOPT_SACK_PERMITTED:
3207f72167f4SAndre Oppermann 			if (optlen != TCPOLEN_SACK_PERMITTED)
32086d90faf3SPaul Saab 				continue;
3209f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3210f72167f4SAndre Oppermann 				continue;
3211603724d3SBjoern A. Zeeb 			if (!V_tcp_do_sack)
3212f72167f4SAndre Oppermann 				continue;
3213fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACKPERM;
32146d90faf3SPaul Saab 			break;
32156d90faf3SPaul Saab 		case TCPOPT_SACK:
32165a53ca16SPaul Saab 			if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
32176d90faf3SPaul Saab 				continue;
3218b728e902SAndre Oppermann 			if (flags & TO_SYN)
3219b728e902SAndre Oppermann 				continue;
3220fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACK;
32215a53ca16SPaul Saab 			to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
32225a53ca16SPaul Saab 			to->to_sacks = cp + 2;
322378b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_rcv_blocks);
32246d90faf3SPaul Saab 			break;
3225be2ac88cSJonathan Lemon 		default:
3226be2ac88cSJonathan Lemon 			continue;
3227df8bae1dSRodney W. Grimes 		}
3228df8bae1dSRodney W. Grimes 	}
3229df8bae1dSRodney W. Grimes }
3230df8bae1dSRodney W. Grimes 
3231df8bae1dSRodney W. Grimes /*
3232df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
3233df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
3234df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
3235df8bae1dSRodney W. Grimes  * sequencing purposes.
3236df8bae1dSRodney W. Grimes  */
32370312fbe9SPoul-Henning Kamp static void
3238ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m,
3239ad3f9ab3SAndre Oppermann     int off)
3240df8bae1dSRodney W. Grimes {
3241fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
3242df8bae1dSRodney W. Grimes 
3243df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
3244df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
3245df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
3246df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
3247df8bae1dSRodney W. Grimes 
32488501a69cSRobert Watson 			INP_WLOCK_ASSERT(tp->t_inpcb);
32497a3244ccSRobert Watson 
3250df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
3251df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
3252df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
3253df8bae1dSRodney W. Grimes 			m->m_len--;
325469a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
325569a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
3256df8bae1dSRodney W. Grimes 			return;
3257df8bae1dSRodney W. Grimes 		}
3258df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
3259df8bae1dSRodney W. Grimes 		m = m->m_next;
32604dfdffe9SAndre Oppermann 		if (m == NULL)
3261df8bae1dSRodney W. Grimes 			break;
3262df8bae1dSRodney W. Grimes 	}
3263df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
3264df8bae1dSRodney W. Grimes }
3265df8bae1dSRodney W. Grimes 
3266df8bae1dSRodney W. Grimes /*
3267df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
3268df8bae1dSRodney W. Grimes  * and update averages and current timeout.
3269df8bae1dSRodney W. Grimes  */
32700312fbe9SPoul-Henning Kamp static void
3271ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt)
3272df8bae1dSRodney W. Grimes {
3273ad3f9ab3SAndre Oppermann 	int delta;
3274233e8c18SGarrett Wollman 
32758501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
32762be3bf22SRobert Watson 
327778b50714SRobert Watson 	TCPSTAT_INC(tcps_rttupdated);
3278233e8c18SGarrett Wollman 	tp->t_rttupdated++;
3279233e8c18SGarrett Wollman 	if (tp->t_srtt != 0) {
3280233e8c18SGarrett Wollman 		/*
3281233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
3282233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
3283233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
3284233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
3285233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
3286233e8c18SGarrett Wollman 		 */
3287233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
3288233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
3289233e8c18SGarrett Wollman 
3290233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
3291233e8c18SGarrett Wollman 			tp->t_srtt = 1;
3292233e8c18SGarrett Wollman 
3293233e8c18SGarrett Wollman 		/*
3294233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
3295233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
3296233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
3297233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
3298233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
3299233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
3300233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
3301233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
3302233e8c18SGarrett Wollman 		 */
3303233e8c18SGarrett Wollman 		if (delta < 0)
3304233e8c18SGarrett Wollman 			delta = -delta;
3305233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
3306233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
3307233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
33081fcc99b5SMatthew Dillon 		if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
33091fcc99b5SMatthew Dillon 		    tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3310233e8c18SGarrett Wollman 	} else {
3311233e8c18SGarrett Wollman 		/*
3312233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
3313233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
3314233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
3315233e8c18SGarrett Wollman 		 */
3316233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
3317233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
33181fcc99b5SMatthew Dillon 		tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3319233e8c18SGarrett Wollman 	}
33209b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
3321df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
3322df8bae1dSRodney W. Grimes 
3323df8bae1dSRodney W. Grimes 	/*
3324df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
3325df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
3326df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
3327df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
3328df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
3329df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
3330df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
3331df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
3332df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
3333df8bae1dSRodney W. Grimes 	 */
3334233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
33359e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
3336df8bae1dSRodney W. Grimes 
3337df8bae1dSRodney W. Grimes 	/*
3338df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
3339df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
3340df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
3341df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
3342df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
3343df8bae1dSRodney W. Grimes 	 */
3344df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
3345df8bae1dSRodney W. Grimes }
3346df8bae1dSRodney W. Grimes 
3347df8bae1dSRodney W. Grimes /*
3348df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
3349df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
33504faaea55SAndre Oppermann  * If none, use an mss that can be handled on the outgoing interface
33514faaea55SAndre Oppermann  * without forcing IP to fragment.  If no route is found, route has no mtu,
3352df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
3353df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
3354df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
3355df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
3356df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
3357df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
3358df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
3359a0292f23SGarrett Wollman  *
3360a0292f23SGarrett Wollman  * Also take into account the space needed for options that we
3361a0292f23SGarrett Wollman  * send regularly.  Make maxseg shorter by that amount to assure
3362a0292f23SGarrett Wollman  * that we can send maxseg amount of data even when the options
3363a0292f23SGarrett Wollman  * are present.  Store the upper limit of the length of options plus
3364a0292f23SGarrett Wollman  * data in maxopd.
3365a0292f23SGarrett Wollman  *
336697d8d152SAndre Oppermann  * NOTE that this routine is only called when we process an incoming
3367ef341ee1SGleb Smirnoff  * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS
3368ef341ee1SGleb Smirnoff  * settings are handled in tcp_mssopt().
3369df8bae1dSRodney W. Grimes  */
3370a0292f23SGarrett Wollman void
3371ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer,
33723c914c54SAndre Oppermann     struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap)
3373df8bae1dSRodney W. Grimes {
3374b287c6c7SBjoern A. Zeeb 	int mss = 0;
3375b287c6c7SBjoern A. Zeeb 	u_long maxmtu = 0;
3376c068736aSJeffrey Hsu 	struct inpcb *inp = tp->t_inpcb;
337797d8d152SAndre Oppermann 	struct hc_metrics_lite metrics;
3378ef341ee1SGleb Smirnoff 	int origoffer;
3379fb59c426SYoshinobu Inoue #ifdef INET6
3380c068736aSJeffrey Hsu 	int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
3381c068736aSJeffrey Hsu 	size_t min_protoh = isipv6 ?
3382c068736aSJeffrey Hsu 			    sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
3383c068736aSJeffrey Hsu 			    sizeof (struct tcpiphdr);
3384c068736aSJeffrey Hsu #else
3385c068736aSJeffrey Hsu 	const size_t min_protoh = sizeof(struct tcpiphdr);
3386fb59c426SYoshinobu Inoue #endif
3387df8bae1dSRodney W. Grimes 
33888501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
33897a3244ccSRobert Watson 
3390ef341ee1SGleb Smirnoff 	if (mtuoffer != -1) {
3391ef341ee1SGleb Smirnoff 		KASSERT(offer == -1, ("%s: conflict", __func__));
3392ef341ee1SGleb Smirnoff 		offer = mtuoffer - min_protoh;
3393ef341ee1SGleb Smirnoff 	}
3394ef341ee1SGleb Smirnoff 	origoffer = offer;
3395ef341ee1SGleb Smirnoff 
3396e8949f74SAndre Oppermann 	/* Initialize. */
339797d8d152SAndre Oppermann #ifdef INET6
339897d8d152SAndre Oppermann 	if (isipv6) {
33993c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu6(&inp->inp_inc, cap);
3400603724d3SBjoern A. Zeeb 		tp->t_maxopd = tp->t_maxseg = V_tcp_v6mssdflt;
3401b287c6c7SBjoern A. Zeeb 	}
340297d8d152SAndre Oppermann #endif
3403b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3404b287c6c7SBjoern A. Zeeb 	else
3405b287c6c7SBjoern A. Zeeb #endif
3406b287c6c7SBjoern A. Zeeb #ifdef INET
340797d8d152SAndre Oppermann 	{
34083c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu(&inp->inp_inc, cap);
3409603724d3SBjoern A. Zeeb 		tp->t_maxopd = tp->t_maxseg = V_tcp_mssdflt;
3410df8bae1dSRodney W. Grimes 	}
3411b287c6c7SBjoern A. Zeeb #endif
3412df8bae1dSRodney W. Grimes 
341397d8d152SAndre Oppermann 	/*
3414e8949f74SAndre Oppermann 	 * No route to sender, stay with default mss and return.
341597d8d152SAndre Oppermann 	 */
34166f01cac6SBjoern A. Zeeb 	if (maxmtu == 0) {
34176f01cac6SBjoern A. Zeeb 		/*
34188e5c87f4SBjoern A. Zeeb 		 * In case we return early we need to initialize metrics
34196f01cac6SBjoern A. Zeeb 		 * to a defined state as tcp_hc_get() would do for us
34206f01cac6SBjoern A. Zeeb 		 * if there was no cache hit.
34216f01cac6SBjoern A. Zeeb 		 */
34226f01cac6SBjoern A. Zeeb 		if (metricptr != NULL)
34236f01cac6SBjoern A. Zeeb 			bzero(metricptr, sizeof(struct hc_metrics_lite));
342497d8d152SAndre Oppermann 		return;
34256f01cac6SBjoern A. Zeeb 	}
342697d8d152SAndre Oppermann 
3427e8949f74SAndre Oppermann 	/* What have we got? */
342897d8d152SAndre Oppermann 	switch (offer) {
342997d8d152SAndre Oppermann 		case 0:
343097d8d152SAndre Oppermann 			/*
343197d8d152SAndre Oppermann 			 * Offer == 0 means that there was no MSS on the SYN
3432c3b02504SBjoern A. Zeeb 			 * segment, in this case we use tcp_mssdflt as
3433c3b02504SBjoern A. Zeeb 			 * already assigned to t_maxopd above.
343497d8d152SAndre Oppermann 			 */
3435c3b02504SBjoern A. Zeeb 			offer = tp->t_maxopd;
343697d8d152SAndre Oppermann 			break;
343797d8d152SAndre Oppermann 
343897d8d152SAndre Oppermann 		case -1:
3439a0292f23SGarrett Wollman 			/*
3440c94c54e4SAndre Oppermann 			 * Offer == -1 means that we didn't receive SYN yet.
3441a0292f23SGarrett Wollman 			 */
344297d8d152SAndre Oppermann 			/* FALLTHROUGH */
344397d8d152SAndre Oppermann 
344497d8d152SAndre Oppermann 		default:
3445a0292f23SGarrett Wollman 			/*
344653369ac9SAndre Oppermann 			 * Prevent DoS attack with too small MSS. Round up
344753369ac9SAndre Oppermann 			 * to at least minmss.
344853369ac9SAndre Oppermann 			 */
3449603724d3SBjoern A. Zeeb 			offer = max(offer, V_tcp_minmss);
345097d8d152SAndre Oppermann 	}
3451a0292f23SGarrett Wollman 
3452df8bae1dSRodney W. Grimes 	/*
3453e8949f74SAndre Oppermann 	 * rmx information is now retrieved from tcp_hostcache.
3454df8bae1dSRodney W. Grimes 	 */
345597d8d152SAndre Oppermann 	tcp_hc_get(&inp->inp_inc, &metrics);
34563cee92e0SBjoern A. Zeeb 	if (metricptr != NULL)
34573cee92e0SBjoern A. Zeeb 		bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite));
345897d8d152SAndre Oppermann 
3459df8bae1dSRodney W. Grimes 	/*
3460cc412412SHiren Panchasara 	 * If there's a discovered mtu in tcp hostcache, use it.
3461cc412412SHiren Panchasara 	 * Else, use the link mtu.
3462df8bae1dSRodney W. Grimes 	 */
346397d8d152SAndre Oppermann 	if (metrics.rmx_mtu)
34642d166c02SAndre Oppermann 		mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
3465c068736aSJeffrey Hsu 	else {
346631b3783cSHajimu UMEMOTO #ifdef INET6
3467fb59c426SYoshinobu Inoue 		if (isipv6) {
346897d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3469603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
347097d8d152SAndre Oppermann 			    !in6_localaddr(&inp->in6p_faddr))
3471603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_v6mssdflt);
3472b287c6c7SBjoern A. Zeeb 		}
3473b3399803SHajimu UMEMOTO #endif
3474b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3475b287c6c7SBjoern A. Zeeb 		else
3476b287c6c7SBjoern A. Zeeb #endif
3477b287c6c7SBjoern A. Zeeb #ifdef INET
347897d8d152SAndre Oppermann 		{
347997d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3480603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
348197d8d152SAndre Oppermann 			    !in_localaddr(inp->inp_faddr))
3482603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_mssdflt);
3483a0292f23SGarrett Wollman 		}
3484b287c6c7SBjoern A. Zeeb #endif
34853cee92e0SBjoern A. Zeeb 		/*
34863cee92e0SBjoern A. Zeeb 		 * XXX - The above conditional (mss = maxmtu - min_protoh)
34873cee92e0SBjoern A. Zeeb 		 * probably violates the TCP spec.
34883cee92e0SBjoern A. Zeeb 		 * The problem is that, since we don't know the
34893cee92e0SBjoern A. Zeeb 		 * other end's MSS, we are supposed to use a conservative
34903cee92e0SBjoern A. Zeeb 		 * default.  But, if we do that, then MTU discovery will
34913cee92e0SBjoern A. Zeeb 		 * never actually take place, because the conservative
34923cee92e0SBjoern A. Zeeb 		 * default is much less than the MTUs typically seen
34933cee92e0SBjoern A. Zeeb 		 * on the Internet today.  For the moment, we'll sweep
34943cee92e0SBjoern A. Zeeb 		 * this under the carpet.
34953cee92e0SBjoern A. Zeeb 		 *
34963cee92e0SBjoern A. Zeeb 		 * The conservative default might not actually be a problem
34973cee92e0SBjoern A. Zeeb 		 * if the only case this occurs is when sending an initial
34983cee92e0SBjoern A. Zeeb 		 * SYN with options and data to a host we've never talked
34993cee92e0SBjoern A. Zeeb 		 * to before.  Then, they will reply with an MSS value which
35003cee92e0SBjoern A. Zeeb 		 * will get recorded and the new parameters should get
35013cee92e0SBjoern A. Zeeb 		 * recomputed.  For Further Study.
35023cee92e0SBjoern A. Zeeb 		 */
350397d8d152SAndre Oppermann 	}
3504a0292f23SGarrett Wollman 	mss = min(mss, offer);
350597d8d152SAndre Oppermann 
3506a0292f23SGarrett Wollman 	/*
35073cee92e0SBjoern A. Zeeb 	 * Sanity check: make sure that maxopd will be large
35083cee92e0SBjoern A. Zeeb 	 * enough to allow some data on segments even if the
35093cee92e0SBjoern A. Zeeb 	 * all the option space is used (40bytes).  Otherwise
35103cee92e0SBjoern A. Zeeb 	 * funny things may happen in tcp_output.
35113cee92e0SBjoern A. Zeeb 	 */
35123cee92e0SBjoern A. Zeeb 	mss = max(mss, 64);
35133cee92e0SBjoern A. Zeeb 
35143cee92e0SBjoern A. Zeeb 	/*
3515a0292f23SGarrett Wollman 	 * maxopd stores the maximum length of data AND options
3516a0292f23SGarrett Wollman 	 * in a segment; maxseg is the amount of data in a normal
3517a0292f23SGarrett Wollman 	 * segment.  We need to store this value (maxopd) apart
3518a0292f23SGarrett Wollman 	 * from maxseg, because now every segment carries options
3519a0292f23SGarrett Wollman 	 * and thus we normally have somewhat less data in segments.
3520a0292f23SGarrett Wollman 	 */
3521a0292f23SGarrett Wollman 	tp->t_maxopd = mss;
3522a0292f23SGarrett Wollman 
3523a0292f23SGarrett Wollman 	/*
3524e8949f74SAndre Oppermann 	 * origoffer==-1 indicates that no segments were received yet.
3525c94c54e4SAndre Oppermann 	 * In this case we just guess.
3526a0292f23SGarrett Wollman 	 */
3527a0292f23SGarrett Wollman 	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
3528a0292f23SGarrett Wollman 	    (origoffer == -1 ||
3529a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
3530a0292f23SGarrett Wollman 		mss -= TCPOLEN_TSTAMP_APPA;
3531a0292f23SGarrett Wollman 
353297d8d152SAndre Oppermann 	tp->t_maxseg = mss;
35333cee92e0SBjoern A. Zeeb }
35343cee92e0SBjoern A. Zeeb 
35353cee92e0SBjoern A. Zeeb void
35363cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer)
35373cee92e0SBjoern A. Zeeb {
3538dbc42409SLawrence Stewart 	int mss;
35393cee92e0SBjoern A. Zeeb 	u_long bufsize;
35403cee92e0SBjoern A. Zeeb 	struct inpcb *inp;
35413cee92e0SBjoern A. Zeeb 	struct socket *so;
35423cee92e0SBjoern A. Zeeb 	struct hc_metrics_lite metrics;
35433c914c54SAndre Oppermann 	struct tcp_ifcap cap;
3544dbc42409SLawrence Stewart 
35453cee92e0SBjoern A. Zeeb 	KASSERT(tp != NULL, ("%s: tp == NULL", __func__));
35463cee92e0SBjoern A. Zeeb 
35473c914c54SAndre Oppermann 	bzero(&cap, sizeof(cap));
35483c914c54SAndre Oppermann 	tcp_mss_update(tp, offer, -1, &metrics, &cap);
35493cee92e0SBjoern A. Zeeb 
35503cee92e0SBjoern A. Zeeb 	mss = tp->t_maxseg;
35513cee92e0SBjoern A. Zeeb 	inp = tp->t_inpcb;
355297d8d152SAndre Oppermann 
3553df8bae1dSRodney W. Grimes 	/*
355497d8d152SAndre Oppermann 	 * If there's a pipesize, change the socket buffer to that size,
355597d8d152SAndre Oppermann 	 * don't change if sb_hiwat is different than default (then it
355697d8d152SAndre Oppermann 	 * has been changed on purpose with setsockopt).
355797d8d152SAndre Oppermann 	 * Make the socket buffers an integral number of mss units;
355897d8d152SAndre Oppermann 	 * if the mss is larger than the socket buffer, decrease the mss.
3559df8bae1dSRodney W. Grimes 	 */
3560c3b02504SBjoern A. Zeeb 	so = inp->inp_socket;
35613f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
3562e233e2acSAndre Oppermann 	if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe)
356397d8d152SAndre Oppermann 		bufsize = metrics.rmx_sendpipe;
356497d8d152SAndre Oppermann 	else
3565df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
3566df8bae1dSRodney W. Grimes 	if (bufsize < mss)
3567df8bae1dSRodney W. Grimes 		mss = bufsize;
3568df8bae1dSRodney W. Grimes 	else {
3569df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3570df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3571df8bae1dSRodney W. Grimes 			bufsize = sb_max;
357288c39af3SRuslan Ermilov 		if (bufsize > so->so_snd.sb_hiwat)
35733f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_snd, bufsize, so, NULL);
3574df8bae1dSRodney W. Grimes 	}
35753f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
3576df8bae1dSRodney W. Grimes 	tp->t_maxseg = mss;
3577df8bae1dSRodney W. Grimes 
35783f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
3579e233e2acSAndre Oppermann 	if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe)
358097d8d152SAndre Oppermann 		bufsize = metrics.rmx_recvpipe;
358197d8d152SAndre Oppermann 	else
3582df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
3583df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
3584df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3585df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3586df8bae1dSRodney W. Grimes 			bufsize = sb_max;
358788c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
35883f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL);
3589df8bae1dSRodney W. Grimes 	}
35903f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
359191d6cfa6SBjoern A. Zeeb 
359291d6cfa6SBjoern A. Zeeb 	/* Check the interface for TSO capabilities. */
35933c914c54SAndre Oppermann 	if (cap.ifcap & CSUM_TSO) {
359491d6cfa6SBjoern A. Zeeb 		tp->t_flags |= TF_TSO;
35953c914c54SAndre Oppermann 		tp->t_tsomax = cap.tsomax;
35969fd573c3SHans Petter Selasky 		tp->t_tsomaxsegcount = cap.tsomaxsegcount;
35979fd573c3SHans Petter Selasky 		tp->t_tsomaxsegsize = cap.tsomaxsegsize;
35983c914c54SAndre Oppermann 	}
3599a0292f23SGarrett Wollman }
3600a0292f23SGarrett Wollman 
3601a0292f23SGarrett Wollman /*
3602a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
3603a0292f23SGarrett Wollman  */
3604a0292f23SGarrett Wollman int
3605ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc)
3606a0292f23SGarrett Wollman {
360797d8d152SAndre Oppermann 	int mss = 0;
360897d8d152SAndre Oppermann 	u_long maxmtu = 0;
360997d8d152SAndre Oppermann 	u_long thcmtu = 0;
361097d8d152SAndre Oppermann 	size_t min_protoh;
3611a0292f23SGarrett Wollman 
361297d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3613a0292f23SGarrett Wollman 
361431b3783cSHajimu UMEMOTO #ifdef INET6
3615dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
3616603724d3SBjoern A. Zeeb 		mss = V_tcp_v6mssdflt;
3617233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(inc, NULL);
361897d8d152SAndre Oppermann 		min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
3619b287c6c7SBjoern A. Zeeb 	}
362031b3783cSHajimu UMEMOTO #endif
3621b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3622b287c6c7SBjoern A. Zeeb 	else
3623b287c6c7SBjoern A. Zeeb #endif
3624b287c6c7SBjoern A. Zeeb #ifdef INET
362597d8d152SAndre Oppermann 	{
3626603724d3SBjoern A. Zeeb 		mss = V_tcp_mssdflt;
3627233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(inc, NULL);
362897d8d152SAndre Oppermann 		min_protoh = sizeof(struct tcpiphdr);
362997d8d152SAndre Oppermann 	}
3630b287c6c7SBjoern A. Zeeb #endif
36313a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET)
36323a9391deSBjoern A. Zeeb 	thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
36333a9391deSBjoern A. Zeeb #endif
36343a9391deSBjoern A. Zeeb 
363597d8d152SAndre Oppermann 	if (maxmtu && thcmtu)
363697d8d152SAndre Oppermann 		mss = min(maxmtu, thcmtu) - min_protoh;
363797d8d152SAndre Oppermann 	else if (maxmtu || thcmtu)
363897d8d152SAndre Oppermann 		mss = max(maxmtu, thcmtu) - min_protoh;
363997d8d152SAndre Oppermann 
364097d8d152SAndre Oppermann 	return (mss);
3641df8bae1dSRodney W. Grimes }
364246f58482SJonathan Lemon 
364346f58482SJonathan Lemon 
364446f58482SJonathan Lemon /*
3645c068736aSJeffrey Hsu  * On a partial ack arrives, force the retransmission of the
3646c068736aSJeffrey Hsu  * next unacknowledged segment.  Do not clear tp->t_dupacks.
3647c068736aSJeffrey Hsu  * By setting snd_nxt to ti_ack, this forces retransmission timer to
3648c068736aSJeffrey Hsu  * be started again.
364946f58482SJonathan Lemon  */
3650c068736aSJeffrey Hsu static void
3651ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
365246f58482SJonathan Lemon {
365346f58482SJonathan Lemon 	tcp_seq onxt = tp->snd_nxt;
365446f58482SJonathan Lemon 	u_long  ocwnd = tp->snd_cwnd;
365546f58482SJonathan Lemon 
36568501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
36577a3244ccSRobert Watson 
3658b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_REXMT, 0);
365946f58482SJonathan Lemon 	tp->t_rtttime = 0;
366046f58482SJonathan Lemon 	tp->snd_nxt = th->th_ack;
36616b2a5f92SJayanth Vijayaraghavan 	/*
3662c068736aSJeffrey Hsu 	 * Set snd_cwnd to one segment beyond acknowledged offset.
3663c068736aSJeffrey Hsu 	 * (tp->snd_una has not yet been updated when this function is called.)
36646b2a5f92SJayanth Vijayaraghavan 	 */
3665dbc42409SLawrence Stewart 	tp->snd_cwnd = tp->t_maxseg + BYTES_THIS_ACK(tp, th);
3666a84db8f4SMatthew Dillon 	tp->t_flags |= TF_ACKNOW;
36676b2a5f92SJayanth Vijayaraghavan 	(void) tcp_output(tp);
366846f58482SJonathan Lemon 	tp->snd_cwnd = ocwnd;
366946f58482SJonathan Lemon 	if (SEQ_GT(onxt, tp->snd_nxt))
367046f58482SJonathan Lemon 		tp->snd_nxt = onxt;
367146f58482SJonathan Lemon 	/*
367246f58482SJonathan Lemon 	 * Partial window deflation.  Relies on fact that tp->snd_una
367346f58482SJonathan Lemon 	 * not updated yet.
367446f58482SJonathan Lemon 	 */
3675dbc42409SLawrence Stewart 	if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th))
3676dbc42409SLawrence Stewart 		tp->snd_cwnd -= BYTES_THIS_ACK(tp, th);
3677d7587117SPaul Saab 	else
3678d7587117SPaul Saab 		tp->snd_cwnd = 0;
3679d7587117SPaul Saab 	tp->snd_cwnd += tp->t_maxseg;
368046f58482SJonathan Lemon }
3681