xref: /freebsd/sys/netinet/tcp_input.c (revision 64807b300fb6711a37fbf34980f7d1bb45c75c8d)
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 
501*64807b30SHiren Panchasara static void inline
502*64807b30SHiren Panchasara cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos)
503*64807b30SHiren Panchasara {
504*64807b30SHiren Panchasara 	INP_WLOCK_ASSERT(tp->t_inpcb);
505*64807b30SHiren Panchasara 
506*64807b30SHiren Panchasara 	if (CC_ALGO(tp)->ecnpkt_handler != NULL) {
507*64807b30SHiren Panchasara 		switch (iptos & IPTOS_ECN_MASK) {
508*64807b30SHiren Panchasara 		case IPTOS_ECN_CE:
509*64807b30SHiren Panchasara 		    tp->ccv->flags |= CCF_IPHDR_CE;
510*64807b30SHiren Panchasara 		    break;
511*64807b30SHiren Panchasara 		case IPTOS_ECN_ECT0:
512*64807b30SHiren Panchasara 		    tp->ccv->flags &= ~CCF_IPHDR_CE;
513*64807b30SHiren Panchasara 		    break;
514*64807b30SHiren Panchasara 		case IPTOS_ECN_ECT1:
515*64807b30SHiren Panchasara 		    tp->ccv->flags &= ~CCF_IPHDR_CE;
516*64807b30SHiren Panchasara 		    break;
517*64807b30SHiren Panchasara 		}
518*64807b30SHiren Panchasara 
519*64807b30SHiren Panchasara 		if (th->th_flags & TH_CWR)
520*64807b30SHiren Panchasara 			tp->ccv->flags |= CCF_TCPHDR_CWR;
521*64807b30SHiren Panchasara 		else
522*64807b30SHiren Panchasara 			tp->ccv->flags &= ~CCF_TCPHDR_CWR;
523*64807b30SHiren Panchasara 
524*64807b30SHiren Panchasara 		if (tp->t_flags & TF_DELACK)
525*64807b30SHiren Panchasara 			tp->ccv->flags |= CCF_DELACK;
526*64807b30SHiren Panchasara 		else
527*64807b30SHiren Panchasara 			tp->ccv->flags &= ~CCF_DELACK;
528*64807b30SHiren Panchasara 
529*64807b30SHiren Panchasara 		CC_ALGO(tp)->ecnpkt_handler(tp->ccv);
530*64807b30SHiren Panchasara 
531*64807b30SHiren Panchasara 		if (tp->ccv->flags & CCF_ACKNOW)
532*64807b30SHiren Panchasara 			tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
533*64807b30SHiren Panchasara 	}
534*64807b30SHiren Panchasara }
535*64807b30SHiren Panchasara 
536df8bae1dSRodney W. Grimes /*
5378d573cc1SAndre Oppermann  * TCP input handling is split into multiple parts:
5388d573cc1SAndre Oppermann  *   tcp6_input is a thin wrapper around tcp_input for the extended
5398d573cc1SAndre Oppermann  *	ip6_protox[] call format in ip6_input
5408d573cc1SAndre Oppermann  *   tcp_input handles primary segment validation, inpcb lookup and
5418d573cc1SAndre Oppermann  *	SYN processing on listen sockets
5428d573cc1SAndre Oppermann  *   tcp_do_segment processes the ACK and text of the segment for
5438d573cc1SAndre Oppermann  *	establishing, established and closing connections
544df8bae1dSRodney W. Grimes  */
545fb59c426SYoshinobu Inoue #ifdef INET6
546fb59c426SYoshinobu Inoue int
547ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto)
548fb59c426SYoshinobu Inoue {
549ad3f9ab3SAndre Oppermann 	struct mbuf *m = *mp;
55033841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
5513e88eb90SAndrey V. Elsukov 	struct ip6_hdr *ip6;
552fb59c426SYoshinobu Inoue 
553fb59c426SYoshinobu Inoue 	IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
554fb59c426SYoshinobu Inoue 
555fb59c426SYoshinobu Inoue 	/*
556fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
557fb59c426SYoshinobu Inoue 	 * better place to put this in?
558fb59c426SYoshinobu Inoue 	 */
5593e88eb90SAndrey V. Elsukov 	ip6 = mtod(m, struct ip6_hdr *);
5603e88eb90SAndrey V. Elsukov 	ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
56133841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
562fb59c426SYoshinobu Inoue 		struct ip6_hdr *ip6;
563fb59c426SYoshinobu Inoue 
5648c0fec80SRobert Watson 		ifa_free(&ia6->ia_ifa);
565fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
566fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
567fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
5688f5a8818SKevin Lo 		return (IPPROTO_DONE);
569fb59c426SYoshinobu Inoue 	}
57077d396fdSMaksim Yevmenkin 	if (ia6)
57177d396fdSMaksim Yevmenkin 		ifa_free(&ia6->ia_ifa);
572fb59c426SYoshinobu Inoue 
5738f5a8818SKevin Lo 	return (tcp_input(mp, offp, proto));
574fb59c426SYoshinobu Inoue }
575b287c6c7SBjoern A. Zeeb #endif /* INET6 */
576fb59c426SYoshinobu Inoue 
5778f5a8818SKevin Lo int
5788f5a8818SKevin Lo tcp_input(struct mbuf **mp, int *offp, int proto)
579df8bae1dSRodney W. Grimes {
5808f5a8818SKevin Lo 	struct mbuf *m = *mp;
581b287c6c7SBjoern A. Zeeb 	struct tcphdr *th = NULL;
582ad3f9ab3SAndre Oppermann 	struct ip *ip = NULL;
583ad3f9ab3SAndre Oppermann 	struct inpcb *inp = NULL;
584302ce8d6SAndre Oppermann 	struct tcpcb *tp = NULL;
585302ce8d6SAndre Oppermann 	struct socket *so = NULL;
586e79adb8eSGarrett Wollman 	u_char *optp = NULL;
5878f5a8818SKevin Lo 	int off0;
58826f9a767SRodney W. Grimes 	int optlen = 0;
589b287c6c7SBjoern A. Zeeb #ifdef INET
590b287c6c7SBjoern A. Zeeb 	int len;
591b287c6c7SBjoern A. Zeeb #endif
592b287c6c7SBjoern A. Zeeb 	int tlen = 0, off;
593fb59c426SYoshinobu Inoue 	int drop_hdrlen;
594ad3f9ab3SAndre Oppermann 	int thflags;
595302ce8d6SAndre Oppermann 	int rstreason = 0;	/* For badport_bandlim accounting purposes */
5962903309aSAttilio Rao #ifdef TCP_SIGNATURE
5972903309aSAttilio Rao 	uint8_t sig_checked = 0;
5982903309aSAttilio Rao #endif
599b287c6c7SBjoern A. Zeeb 	uint8_t iptos = 0;
600c1de64a4SAndrey V. Elsukov 	struct m_tag *fwd_tag = NULL;
601c068736aSJeffrey Hsu #ifdef INET6
602302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6 = NULL;
603c068736aSJeffrey Hsu 	int isipv6;
604c068736aSJeffrey Hsu #else
605a160e630SAndre Oppermann 	const void *ip6 = NULL;
606b287c6c7SBjoern A. Zeeb #endif /* INET6 */
607302ce8d6SAndre Oppermann 	struct tcpopt to;		/* options in this segment */
608a160e630SAndre Oppermann 	char *s = NULL;			/* address and port logging */
609252ca428SRobert Watson 	int ti_locked;
610252ca428SRobert Watson #define	TI_UNLOCKED	1
611fa046d87SRobert Watson #define	TI_WLOCKED	2
612f76fcf6dSJeffrey Hsu 
613610ee2f9SDavid Greenman #ifdef TCPDEBUG
614c068736aSJeffrey Hsu 	/*
615c068736aSJeffrey Hsu 	 * The size of tcp_saveipgen must be the size of the max ip header,
616c068736aSJeffrey Hsu 	 * now IPv6.
617c068736aSJeffrey Hsu 	 */
61814739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
619410bb1bfSLuigi Rizzo 	struct tcphdr tcp_savetcp;
620610ee2f9SDavid Greenman 	short ostate = 0;
621610ee2f9SDavid Greenman #endif
622c068736aSJeffrey Hsu 
623fb59c426SYoshinobu Inoue #ifdef INET6
624fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
625fb59c426SYoshinobu Inoue #endif
626a0292f23SGarrett Wollman 
6278f5a8818SKevin Lo 	off0 = *offp;
6288f5a8818SKevin Lo 	m = *mp;
6298f5a8818SKevin Lo 	*mp = NULL;
630302ce8d6SAndre Oppermann 	to.to_flags = 0;
63178b50714SRobert Watson 	TCPSTAT_INC(tcps_rcvtotal);
632fb59c426SYoshinobu Inoue 
63304d3a452SHajimu UMEMOTO #ifdef INET6
634b287c6c7SBjoern A. Zeeb 	if (isipv6) {
6358d573cc1SAndre Oppermann 		/* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */
63645747ba5SBjoern A. Zeeb 
63745747ba5SBjoern A. Zeeb 		if (m->m_len < (sizeof(*ip6) + sizeof(*th))) {
63845747ba5SBjoern A. Zeeb 			m = m_pullup(m, sizeof(*ip6) + sizeof(*th));
63945747ba5SBjoern A. Zeeb 			if (m == NULL) {
64045747ba5SBjoern A. Zeeb 				TCPSTAT_INC(tcps_rcvshort);
6418f5a8818SKevin Lo 				return (IPPROTO_DONE);
64245747ba5SBjoern A. Zeeb 			}
64345747ba5SBjoern A. Zeeb 		}
64445747ba5SBjoern A. Zeeb 
645fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
64645747ba5SBjoern A. Zeeb 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
647fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
648356ab07eSBjoern A. Zeeb 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) {
64945747ba5SBjoern A. Zeeb 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
65045747ba5SBjoern A. Zeeb 				th->th_sum = m->m_pkthdr.csum_data;
65145747ba5SBjoern A. Zeeb 			else
65245747ba5SBjoern A. Zeeb 				th->th_sum = in6_cksum_pseudo(ip6, tlen,
65345747ba5SBjoern A. Zeeb 				    IPPROTO_TCP, m->m_pkthdr.csum_data);
65445747ba5SBjoern A. Zeeb 			th->th_sum ^= 0xffff;
65545747ba5SBjoern A. Zeeb 		} else
65645747ba5SBjoern A. Zeeb 			th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen);
65745747ba5SBjoern A. Zeeb 		if (th->th_sum) {
65878b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
659fb59c426SYoshinobu Inoue 			goto drop;
660fb59c426SYoshinobu Inoue 		}
66133841545SHajimu UMEMOTO 
66233841545SHajimu UMEMOTO 		/*
66333841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
66433841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
66533841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
66633841545SHajimu UMEMOTO 		 *
66733841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
66833841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
66933841545SHajimu UMEMOTO 		 */
67033841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
67133841545SHajimu UMEMOTO 			/* XXX stat */
67233841545SHajimu UMEMOTO 			goto drop;
67333841545SHajimu UMEMOTO 		}
674b287c6c7SBjoern A. Zeeb 	}
67504d3a452SHajimu UMEMOTO #endif
676b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
677b287c6c7SBjoern A. Zeeb 	else
678b287c6c7SBjoern A. Zeeb #endif
679b287c6c7SBjoern A. Zeeb #ifdef INET
680b287c6c7SBjoern A. Zeeb 	{
681df8bae1dSRodney W. Grimes 		/*
682df8bae1dSRodney W. Grimes 		 * Get IP and TCP header together in first mbuf.
683df8bae1dSRodney W. Grimes 		 * Note: IP leaves IP header in first mbuf.
684df8bae1dSRodney W. Grimes 		 */
685fb59c426SYoshinobu Inoue 		if (off0 > sizeof (struct ip)) {
686105bd211SGleb Smirnoff 			ip_stripoptions(m);
687fb59c426SYoshinobu Inoue 			off0 = sizeof(struct ip);
688fb59c426SYoshinobu Inoue 		}
689df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof (struct tcpiphdr)) {
6904dfdffe9SAndre Oppermann 			if ((m = m_pullup(m, sizeof (struct tcpiphdr)))
6914dfdffe9SAndre Oppermann 			    == NULL) {
69278b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvshort);
6938f5a8818SKevin Lo 				return (IPPROTO_DONE);
694df8bae1dSRodney W. Grimes 			}
695df8bae1dSRodney W. Grimes 		}
696fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
697db4f9cc7SJonathan Lemon 		th = (struct tcphdr *)((caddr_t)ip + off0);
6988ad458a4SGleb Smirnoff 		tlen = ntohs(ip->ip_len) - off0;
699df8bae1dSRodney W. Grimes 
700db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
701db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
702db4f9cc7SJonathan Lemon 				th->th_sum = m->m_pkthdr.csum_data;
703db4f9cc7SJonathan Lemon 			else
704db4f9cc7SJonathan Lemon 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
705c068736aSJeffrey Hsu 				    ip->ip_dst.s_addr,
7068f134647SGleb Smirnoff 				    htonl(m->m_pkthdr.csum_data + tlen +
707c068736aSJeffrey Hsu 				    IPPROTO_TCP));
708db4f9cc7SJonathan Lemon 			th->th_sum ^= 0xffff;
709db4f9cc7SJonathan Lemon 		} else {
7108f134647SGleb Smirnoff 			struct ipovly *ipov = (struct ipovly *)ip;
7118f134647SGleb Smirnoff 
712df8bae1dSRodney W. Grimes 			/*
713df8bae1dSRodney W. Grimes 			 * Checksum extended TCP header and data.
714df8bae1dSRodney W. Grimes 			 */
7158f134647SGleb Smirnoff 			len = off0 + tlen;
716fb59c426SYoshinobu Inoue 			bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
7178f134647SGleb Smirnoff 			ipov->ih_len = htons(tlen);
718fb59c426SYoshinobu Inoue 			th->th_sum = in_cksum(m, len);
71957f60867SMark Johnston 			/* Reset length for SDT probes. */
72057f60867SMark Johnston 			ip->ip_len = htons(tlen + off0);
721db4f9cc7SJonathan Lemon 		}
72257f60867SMark Johnston 
723fb59c426SYoshinobu Inoue 		if (th->th_sum) {
72478b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
725df8bae1dSRodney W. Grimes 			goto drop;
726df8bae1dSRodney W. Grimes 		}
727fb59c426SYoshinobu Inoue 		/* Re-initialization for later version check */
728fb59c426SYoshinobu Inoue 		ip->ip_v = IPVERSION;
729fb59c426SYoshinobu Inoue 	}
730b287c6c7SBjoern A. Zeeb #endif /* INET */
731df8bae1dSRodney W. Grimes 
732f2512ba1SRui Paulo #ifdef INET6
733f2512ba1SRui Paulo 	if (isipv6)
734f2512ba1SRui Paulo 		iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
735b287c6c7SBjoern A. Zeeb #endif
736b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
737f2512ba1SRui Paulo 	else
738f2512ba1SRui Paulo #endif
739b287c6c7SBjoern A. Zeeb #ifdef INET
740f2512ba1SRui Paulo 		iptos = ip->ip_tos;
741b287c6c7SBjoern A. Zeeb #endif
742f2512ba1SRui Paulo 
743df8bae1dSRodney W. Grimes 	/*
744df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
745df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
746df8bae1dSRodney W. Grimes 	 */
747fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
748df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
74978b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvbadoff);
750df8bae1dSRodney W. Grimes 		goto drop;
751df8bae1dSRodney W. Grimes 	}
752fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
753df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
75404d3a452SHajimu UMEMOTO #ifdef INET6
755b287c6c7SBjoern A. Zeeb 		if (isipv6) {
7568f5a8818SKevin Lo 			IP6_EXTHDR_CHECK(m, off0, off, IPPROTO_DONE);
757fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
758fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
759b287c6c7SBjoern A. Zeeb 		}
76004d3a452SHajimu UMEMOTO #endif
761b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
762b287c6c7SBjoern A. Zeeb 		else
763b287c6c7SBjoern A. Zeeb #endif
764b287c6c7SBjoern A. Zeeb #ifdef INET
765b287c6c7SBjoern A. Zeeb 		{
766df8bae1dSRodney W. Grimes 			if (m->m_len < sizeof(struct ip) + off) {
767c068736aSJeffrey Hsu 				if ((m = m_pullup(m, sizeof (struct ip) + off))
7684dfdffe9SAndre Oppermann 				    == NULL) {
76978b50714SRobert Watson 					TCPSTAT_INC(tcps_rcvshort);
7708f5a8818SKevin Lo 					return (IPPROTO_DONE);
771df8bae1dSRodney W. Grimes 				}
772fb59c426SYoshinobu Inoue 				ip = mtod(m, struct ip *);
773fb59c426SYoshinobu Inoue 				th = (struct tcphdr *)((caddr_t)ip + off0);
774fb59c426SYoshinobu Inoue 			}
775df8bae1dSRodney W. Grimes 		}
776b287c6c7SBjoern A. Zeeb #endif
777df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
778fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
779df8bae1dSRodney W. Grimes 	}
780fb59c426SYoshinobu Inoue 	thflags = th->th_flags;
781df8bae1dSRodney W. Grimes 
782e46cd3d4SDag-Erling Smørgrav 	/*
783df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
784df8bae1dSRodney W. Grimes 	 */
7852903309aSAttilio Rao 	tcp_fields_to_host(th);
786df8bae1dSRodney W. Grimes 
787df8bae1dSRodney W. Grimes 	/*
788794235b7SAndre Oppermann 	 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options.
789755c1f07SAndras Olah 	 */
790fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
791755c1f07SAndras Olah 
792755c1f07SAndras Olah 	/*
793fa046d87SRobert Watson 	 * Locate pcb for segment; if we're likely to add or remove a
794a7c7f2a7SJohn Baldwin 	 * connection then first acquire pcbinfo lock.  There are three cases
795fa046d87SRobert Watson 	 * where we might discover later we need a write lock despite the
796a7c7f2a7SJohn Baldwin 	 * flags: ACKs moving a connection out of the syncache, ACKs for a
797a7c7f2a7SJohn Baldwin 	 * connection in TIMEWAIT and SYNs not targeting a listening socket.
798df8bae1dSRodney W. Grimes 	 */
799a7c7f2a7SJohn Baldwin 	if ((thflags & (TH_FIN | TH_RST)) != 0) {
800603724d3SBjoern A. Zeeb 		INP_INFO_WLOCK(&V_tcbinfo);
801252ca428SRobert Watson 		ti_locked = TI_WLOCKED;
802fa046d87SRobert Watson 	} else
803fa046d87SRobert Watson 		ti_locked = TI_UNLOCKED;
804252ca428SRobert Watson 
8058d573cc1SAndre Oppermann 	/*
8068d573cc1SAndre Oppermann 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
8078d573cc1SAndre Oppermann 	 */
808b8056faeSGleb Smirnoff         if (
809b8056faeSGleb Smirnoff #ifdef INET6
810b8056faeSGleb Smirnoff 	    (isipv6 && (m->m_flags & M_IP6_NEXTHOP))
811b8056faeSGleb Smirnoff #ifdef INET
812b8056faeSGleb Smirnoff 	    || (!isipv6 && (m->m_flags & M_IP_NEXTHOP))
813b8056faeSGleb Smirnoff #endif
814b8056faeSGleb Smirnoff #endif
815b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6)
816b8056faeSGleb Smirnoff 	    (m->m_flags & M_IP_NEXTHOP)
817b8056faeSGleb Smirnoff #endif
818b8056faeSGleb Smirnoff 	    )
8199b932e9eSAndre Oppermann 		fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
8209b932e9eSAndre Oppermann 
821ce7ad664SEd Maste findpcb:
822ce7ad664SEd Maste #ifdef INVARIANTS
823ce7ad664SEd Maste 	if (ti_locked == TI_WLOCKED) {
824ce7ad664SEd Maste 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
825ce7ad664SEd Maste 	} else {
826ce7ad664SEd Maste 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
827ce7ad664SEd Maste 	}
828ce7ad664SEd Maste #endif
8298a006adbSBjoern A. Zeeb #ifdef INET6
8308a006adbSBjoern A. Zeeb 	if (isipv6 && fwd_tag != NULL) {
8318a006adbSBjoern A. Zeeb 		struct sockaddr_in6 *next_hop6;
8328a006adbSBjoern A. Zeeb 
8338a006adbSBjoern A. Zeeb 		next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
8348a006adbSBjoern A. Zeeb 		/*
8358a006adbSBjoern A. Zeeb 		 * Transparently forwarded. Pretend to be the destination.
8368a006adbSBjoern A. Zeeb 		 * Already got one like this?
8378a006adbSBjoern A. Zeeb 		 */
8388a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo,
8398a006adbSBjoern A. Zeeb 		    &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport,
8408a006adbSBjoern A. Zeeb 		    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m);
8418a006adbSBjoern A. Zeeb 		if (!inp) {
8428a006adbSBjoern A. Zeeb 			/*
8438a006adbSBjoern A. Zeeb 			 * It's new.  Try to find the ambushing socket.
8448a006adbSBjoern A. Zeeb 			 * Because we've rewritten the destination address,
8458a006adbSBjoern A. Zeeb 			 * any hardware-generated hash is ignored.
8468a006adbSBjoern A. Zeeb 			 */
8478a006adbSBjoern A. Zeeb 			inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src,
8488a006adbSBjoern A. Zeeb 			    th->th_sport, &next_hop6->sin6_addr,
8498a006adbSBjoern A. Zeeb 			    next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) :
8508a006adbSBjoern A. Zeeb 			    th->th_dport, INPLOOKUP_WILDCARD |
8518a006adbSBjoern A. Zeeb 			    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
8528a006adbSBjoern A. Zeeb 		}
853c1de64a4SAndrey V. Elsukov 	} else if (isipv6) {
8548a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src,
8558a006adbSBjoern A. Zeeb 		    th->th_sport, &ip6->ip6_dst, th->th_dport,
8568a006adbSBjoern A. Zeeb 		    INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
8578a006adbSBjoern A. Zeeb 		    m->m_pkthdr.rcvif, m);
8588a006adbSBjoern A. Zeeb 	}
8598a006adbSBjoern A. Zeeb #endif /* INET6 */
8608a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET)
8618a006adbSBjoern A. Zeeb 	else
8628a006adbSBjoern A. Zeeb #endif
8638a006adbSBjoern A. Zeeb #ifdef INET
8648a006adbSBjoern A. Zeeb 	if (fwd_tag != NULL) {
8659b932e9eSAndre Oppermann 		struct sockaddr_in *next_hop;
8669b932e9eSAndre Oppermann 
8679b932e9eSAndre Oppermann 		next_hop = (struct sockaddr_in *)(fwd_tag+1);
868f9e354dfSJulian Elischer 		/*
8692b25acc1SLuigi Rizzo 		 * Transparently forwarded. Pretend to be the destination.
870f9e354dfSJulian Elischer 		 * already got one like this?
871f9e354dfSJulian Elischer 		 */
872d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport,
873fa046d87SRobert Watson 		    ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB,
874d3c1f003SRobert Watson 		    m->m_pkthdr.rcvif, m);
875fa046d87SRobert Watson 		if (!inp) {
876fa046d87SRobert Watson 			/*
877fa046d87SRobert Watson 			 * It's new.  Try to find the ambushing socket.
878d3c1f003SRobert Watson 			 * Because we've rewritten the destination address,
879d3c1f003SRobert Watson 			 * any hardware-generated hash is ignored.
880fa046d87SRobert Watson 			 */
881fa046d87SRobert Watson 			inp = in_pcblookup(&V_tcbinfo, ip->ip_src,
882fa046d87SRobert Watson 			    th->th_sport, next_hop->sin_addr,
883fa046d87SRobert Watson 			    next_hop->sin_port ? ntohs(next_hop->sin_port) :
884fa046d87SRobert Watson 			    th->th_dport, INPLOOKUP_WILDCARD |
885fa046d87SRobert Watson 			    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
886f9e354dfSJulian Elischer 		}
887b10fbdeaSAndre Oppermann 	} else
888d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src,
889fa046d87SRobert Watson 		    th->th_sport, ip->ip_dst, th->th_dport,
890fa046d87SRobert Watson 		    INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
891d3c1f003SRobert Watson 		    m->m_pkthdr.rcvif, m);
8928a006adbSBjoern A. Zeeb #endif /* INET */
893f9e354dfSJulian Elischer 
894df8bae1dSRodney W. Grimes 	/*
895574b6964SAndre Oppermann 	 * If the INPCB does not exist then all data in the incoming
896574b6964SAndre Oppermann 	 * segment is discarded and an appropriate RST is sent back.
8978b07e49aSJulian Elischer 	 * XXX MRT Send RST using which routing table?
898df8bae1dSRodney W. Grimes 	 */
899816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
900574b6964SAndre Oppermann 		/*
901574b6964SAndre Oppermann 		 * Log communication attempts to ports that are not
902574b6964SAndre Oppermann 		 * in use.
903574b6964SAndre Oppermann 		 */
904574b6964SAndre Oppermann 		if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) ||
905574b6964SAndre Oppermann 		    tcp_log_in_vain == 2) {
906b7d747ecSAndre Oppermann 			if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6)))
907df541e5fSAndre Oppermann 				log(LOG_INFO, "%s; %s: Connection attempt "
908df541e5fSAndre Oppermann 				    "to closed port\n", s, __func__);
9092e4e1b4cSGeoff Rehmet 		}
910574b6964SAndre Oppermann 		/*
911574b6964SAndre Oppermann 		 * When blackholing do not respond with a RST but
912574b6964SAndre Oppermann 		 * completely ignore the segment and drop it.
913574b6964SAndre Oppermann 		 */
914603724d3SBjoern A. Zeeb 		if ((V_blackhole == 1 && (thflags & TH_SYN)) ||
915603724d3SBjoern A. Zeeb 		    V_blackhole == 2)
9161929eae1SAndre Oppermann 			goto dropunlock;
917574b6964SAndre Oppermann 
918a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
919a57815efSBosko Milekic 		goto dropwithreset;
920816a3d83SPoul-Henning Kamp 	}
921fa046d87SRobert Watson 	INP_WLOCK_ASSERT(inp);
922c2529042SHans Petter Selasky 	if ((inp->inp_flowtype == M_HASHTYPE_NONE) &&
923c2529042SHans Petter Selasky 	    (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) &&
924c2529042SHans Petter Selasky 	    ((inp->inp_socket == NULL) ||
925c2529042SHans Petter Selasky 	    (inp->inp_socket->so_options & SO_ACCEPTCONN) == 0)) {
92680cb9f21SKip Macy 		inp->inp_flowid = m->m_pkthdr.flowid;
9272f719932SAdrian Chadd 		inp->inp_flowtype = M_HASHTYPE_GET(m);
92880cb9f21SKip Macy 	}
929a2589465SKen Smith #ifdef IPSEC
930a2589465SKen Smith #ifdef INET6
931a2589465SKen Smith 	if (isipv6 && ipsec6_in_reject(m, inp)) {
932a2589465SKen Smith 		goto dropunlock;
933a2589465SKen Smith 	} else
934a2589465SKen Smith #endif /* INET6 */
935a2589465SKen Smith 	if (ipsec4_in_reject(m, inp) != 0) {
936a2589465SKen Smith 		goto dropunlock;
937a2589465SKen Smith 	}
938a2589465SKen Smith #endif /* IPSEC */
939a2589465SKen Smith 
9408d573cc1SAndre Oppermann 	/*
9418d573cc1SAndre Oppermann 	 * Check the minimum TTL for socket.
9428d573cc1SAndre Oppermann 	 */
94334f83c52SGeorge V. Neville-Neil 	if (inp->inp_ip_minttl != 0) {
94434f83c52SGeorge V. Neville-Neil #ifdef INET6
94534f83c52SGeorge V. Neville-Neil 		if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim)
946302ce8d6SAndre Oppermann 			goto dropunlock;
94702707462SAndre Oppermann 		else
94834f83c52SGeorge V. Neville-Neil #endif
94934f83c52SGeorge V. Neville-Neil 		if (inp->inp_ip_minttl > ip->ip_ttl)
950302ce8d6SAndre Oppermann 			goto dropunlock;
95134f83c52SGeorge V. Neville-Neil 	}
952936cd18dSAndre Oppermann 
953340c35deSJonathan Lemon 	/*
954252ca428SRobert Watson 	 * A previous connection in TIMEWAIT state is supposed to catch stray
955252ca428SRobert Watson 	 * or duplicate segments arriving late.  If this segment was a
9560989f56cSRobert Watson 	 * legitimate new connection attempt, the old INPCB gets removed and
957252ca428SRobert Watson 	 * we can try again to find a listening socket.
958252ca428SRobert Watson 	 *
959fa046d87SRobert Watson 	 * At this point, due to earlier optimism, we may hold only an inpcb
960fa046d87SRobert Watson 	 * lock, and not the inpcbinfo write lock.  If so, we need to try to
961fa046d87SRobert Watson 	 * acquire it, or if that fails, acquire a reference on the inpcb,
962fa046d87SRobert Watson 	 * drop all locks, acquire a global write lock, and then re-acquire
963fa046d87SRobert Watson 	 * the inpcb lock.  We may at that point discover that another thread
964fa046d87SRobert Watson 	 * has tried to free the inpcb, in which case we need to loop back
965fa046d87SRobert Watson 	 * and try to find a new inpcb to deliver to.
966fa046d87SRobert Watson 	 *
967fa046d87SRobert Watson 	 * XXXRW: It may be time to rethink timewait locking.
968340c35deSJonathan Lemon 	 */
969883e9bc4SRobert Watson relocked:
970ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
971fa046d87SRobert Watson 		if (ti_locked == TI_UNLOCKED) {
972fa046d87SRobert Watson 			if (INP_INFO_TRY_WLOCK(&V_tcbinfo) == 0) {
973252ca428SRobert Watson 				in_pcbref(inp);
974252ca428SRobert Watson 				INP_WUNLOCK(inp);
975252ca428SRobert Watson 				INP_INFO_WLOCK(&V_tcbinfo);
976252ca428SRobert Watson 				ti_locked = TI_WLOCKED;
977252ca428SRobert Watson 				INP_WLOCK(inp);
978fa046d87SRobert Watson 				if (in_pcbrele_wlocked(inp)) {
979252ca428SRobert Watson 					inp = NULL;
980252ca428SRobert Watson 					goto findpcb;
981252ca428SRobert Watson 				}
982f681a5fdSRobert Watson 			} else
983252ca428SRobert Watson 				ti_locked = TI_WLOCKED;
984252ca428SRobert Watson 		}
985252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
986252ca428SRobert Watson 
987340c35deSJonathan Lemon 		if (thflags & TH_SYN)
988f72167f4SAndre Oppermann 			tcp_dooptions(&to, optp, optlen, TO_SYN);
9898d573cc1SAndre Oppermann 		/*
9908d573cc1SAndre Oppermann 		 * NB: tcp_twcheck unlocks the INP and frees the mbuf.
9918d573cc1SAndre Oppermann 		 */
9922104448fSAndre Oppermann 		if (tcp_twcheck(inp, &to, th, m, tlen))
993340c35deSJonathan Lemon 			goto findpcb;
994603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
9958f5a8818SKevin Lo 		return (IPPROTO_DONE);
996340c35deSJonathan Lemon 	}
997794235b7SAndre Oppermann 	/*
998794235b7SAndre Oppermann 	 * The TCPCB may no longer exist if the connection is winding
999794235b7SAndre Oppermann 	 * down or it is in the CLOSED state.  Either way we drop the
1000794235b7SAndre Oppermann 	 * segment and send an appropriate response.
1001794235b7SAndre Oppermann 	 */
1002df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
1003a160e630SAndre Oppermann 	if (tp == NULL || tp->t_state == TCPS_CLOSED) {
1004a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
1005a57815efSBosko Milekic 		goto dropwithreset;
100609f81a46SBosko Milekic 	}
1007df8bae1dSRodney W. Grimes 
100809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
100909fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
101009fe6320SNavdeep Parhar 		tcp_offload_input(tp, m);
101109fe6320SNavdeep Parhar 		m = NULL;	/* consumed by the TOE driver */
101209fe6320SNavdeep Parhar 		goto dropunlock;
101309fe6320SNavdeep Parhar 	}
101409fe6320SNavdeep Parhar #endif
101509fe6320SNavdeep Parhar 
1016252ca428SRobert Watson 	/*
1017252ca428SRobert Watson 	 * We've identified a valid inpcb, but it could be that we need an
1018fa046d87SRobert Watson 	 * inpcbinfo write lock but don't hold it.  In this case, attempt to
1019fa046d87SRobert Watson 	 * acquire using the same strategy as the TIMEWAIT case above.  If we
1020fa046d87SRobert Watson 	 * relock, we have to jump back to 'relocked' as the connection might
1021fa046d87SRobert Watson 	 * now be in TIMEWAIT.
1022252ca428SRobert Watson 	 */
1023fa046d87SRobert Watson #ifdef INVARIANTS
1024a7c7f2a7SJohn Baldwin 	if ((thflags & (TH_FIN | TH_RST)) != 0)
1025fa046d87SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1026fa046d87SRobert Watson #endif
1027a7c7f2a7SJohn Baldwin 	if (!((tp->t_state == TCPS_ESTABLISHED && (thflags & TH_SYN) == 0) ||
1028a7c7f2a7SJohn Baldwin 	    (tp->t_state == TCPS_LISTEN && (thflags & TH_SYN)))) {
1029fa046d87SRobert Watson 		if (ti_locked == TI_UNLOCKED) {
1030fa046d87SRobert Watson 			if (INP_INFO_TRY_WLOCK(&V_tcbinfo) == 0) {
1031252ca428SRobert Watson 				in_pcbref(inp);
1032252ca428SRobert Watson 				INP_WUNLOCK(inp);
1033252ca428SRobert Watson 				INP_INFO_WLOCK(&V_tcbinfo);
1034252ca428SRobert Watson 				ti_locked = TI_WLOCKED;
1035252ca428SRobert Watson 				INP_WLOCK(inp);
1036fa046d87SRobert Watson 				if (in_pcbrele_wlocked(inp)) {
1037252ca428SRobert Watson 					inp = NULL;
1038252ca428SRobert Watson 					goto findpcb;
1039252ca428SRobert Watson 				}
1040883e9bc4SRobert Watson 				goto relocked;
1041f681a5fdSRobert Watson 			} else
1042252ca428SRobert Watson 				ti_locked = TI_WLOCKED;
1043252ca428SRobert Watson 		}
1044252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1045252ca428SRobert Watson 	}
1046252ca428SRobert Watson 
1047c488362eSRobert Watson #ifdef MAC
10488501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
104930d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, m))
1050302ce8d6SAndre Oppermann 		goto dropunlock;
1051c488362eSRobert Watson #endif
1052a557af22SRobert Watson 	so = inp->inp_socket;
1053302ce8d6SAndre Oppermann 	KASSERT(so != NULL, ("%s: so == NULL", __func__));
1054610ee2f9SDavid Greenman #ifdef TCPDEBUG
1055df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG) {
1056df8bae1dSRodney W. Grimes 		ostate = tp->t_state;
105710fe523eSMaxim Konovalov #ifdef INET6
10586f697424SBjoern A. Zeeb 		if (isipv6) {
1059540e8b7eSJeffrey Hsu 			bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6));
1060d30d90dcSMaxim Konovalov 		} else
10616f697424SBjoern A. Zeeb #endif
1062540e8b7eSJeffrey Hsu 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
1063fb59c426SYoshinobu Inoue 		tcp_savetcp = *th;
1064df8bae1dSRodney W. Grimes 	}
1065b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */
1066db33b3e6SAndre Oppermann 	/*
1067db33b3e6SAndre Oppermann 	 * When the socket is accepting connections (the INPCB is in LISTEN
1068db33b3e6SAndre Oppermann 	 * state) we look into the SYN cache if this is a new connection
1069a7c7f2a7SJohn Baldwin 	 * attempt or the completion of a previous one.
1070db33b3e6SAndre Oppermann 	 */
1071540e8b7eSJeffrey Hsu 	if (so->so_options & SO_ACCEPTCONN) {
1072540e8b7eSJeffrey Hsu 		struct in_conninfo inc;
1073540e8b7eSJeffrey Hsu 
10747824d002SAndre Oppermann 		KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but "
10757824d002SAndre Oppermann 		    "tp not listening", __func__));
10768bfb1918SAndre Oppermann 		bzero(&inc, sizeof(inc));
1077302ce8d6SAndre Oppermann #ifdef INET6
1078be2ac88cSJonathan Lemon 		if (isipv6) {
1079dcdb4371SBjoern A. Zeeb 			inc.inc_flags |= INC_ISIPV6;
1080be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
1081be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
1082302ce8d6SAndre Oppermann 		} else
1083302ce8d6SAndre Oppermann #endif
1084302ce8d6SAndre Oppermann 		{
1085be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
1086be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
1087be2ac88cSJonathan Lemon 		}
1088be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
1089be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
10907973fba3SJulian Elischer 		inc.inc_fibnum = so->so_fibnum;
1091be2ac88cSJonathan Lemon 
1092fb59c426SYoshinobu Inoue 		/*
10938d573cc1SAndre Oppermann 		 * Check for an existing connection attempt in syncache if
10948d573cc1SAndre Oppermann 		 * the flag is only ACK.  A successful lookup creates a new
10958d573cc1SAndre Oppermann 		 * socket appended to the listen queue in SYN_RECEIVED state.
1096fb59c426SYoshinobu Inoue 		 */
1097be2ac88cSJonathan Lemon 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
1098a7c7f2a7SJohn Baldwin 
1099a7c7f2a7SJohn Baldwin 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1100bf6d304aSAndre Oppermann 			/*
1101bf6d304aSAndre Oppermann 			 * Parse the TCP options here because
1102bf6d304aSAndre Oppermann 			 * syncookies need access to the reflected
1103bf6d304aSAndre Oppermann 			 * timestamp.
1104bf6d304aSAndre Oppermann 			 */
1105bf6d304aSAndre Oppermann 			tcp_dooptions(&to, optp, optlen, 0);
11069fa198beSAndre Oppermann 			/*
11079fa198beSAndre Oppermann 			 * NB: syncache_expand() doesn't unlock
11089fa198beSAndre Oppermann 			 * inp and tcpinfo locks.
11099fa198beSAndre Oppermann 			 */
1110bf6d304aSAndre Oppermann 			if (!syncache_expand(&inc, &to, th, &so, m)) {
11114195b4afSPaul Traina 				/*
1112e207f800SAndre Oppermann 				 * No syncache entry or ACK was not
1113be2ac88cSJonathan Lemon 				 * for our SYN/ACK.  Send a RST.
11148d573cc1SAndre Oppermann 				 * NB: syncache did its own logging
11158d573cc1SAndre Oppermann 				 * of the failure cause.
11164195b4afSPaul Traina 				 */
1117be2ac88cSJonathan Lemon 				rstreason = BANDLIM_RST_OPENPORT;
1118be2ac88cSJonathan Lemon 				goto dropwithreset;
1119be2ac88cSJonathan Lemon 			}
1120f76fcf6dSJeffrey Hsu 			if (so == NULL) {
1121be2ac88cSJonathan Lemon 				/*
1122e207f800SAndre Oppermann 				 * We completed the 3-way handshake
1123e207f800SAndre Oppermann 				 * but could not allocate a socket
1124e207f800SAndre Oppermann 				 * either due to memory shortage,
1125e207f800SAndre Oppermann 				 * listen queue length limits or
1126a160e630SAndre Oppermann 				 * global socket limits.  Send RST
1127a160e630SAndre Oppermann 				 * or wait and have the remote end
1128a160e630SAndre Oppermann 				 * retransmit the ACK for another
1129a160e630SAndre Oppermann 				 * try.
1130be2ac88cSJonathan Lemon 				 */
1131a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1132a160e630SAndre Oppermann 					log(LOG_DEBUG, "%s; %s: Listen socket: "
11338d573cc1SAndre Oppermann 					    "Socket allocation failed due to "
11348d573cc1SAndre Oppermann 					    "limits or memory shortage, %s\n",
1135ac957cd2SJulian Elischer 					    s, __func__,
1136ac957cd2SJulian Elischer 					    V_tcp_sc_rst_sock_fail ?
1137ac957cd2SJulian Elischer 					    "sending RST" : "try again");
1138603724d3SBjoern A. Zeeb 				if (V_tcp_sc_rst_sock_fail) {
1139e207f800SAndre Oppermann 					rstreason = BANDLIM_UNLIMITED;
1140e207f800SAndre Oppermann 					goto dropwithreset;
1141a160e630SAndre Oppermann 				} else
1142a160e630SAndre Oppermann 					goto dropunlock;
1143f76fcf6dSJeffrey Hsu 			}
1144be2ac88cSJonathan Lemon 			/*
1145be2ac88cSJonathan Lemon 			 * Socket is created in state SYN_RECEIVED.
11468d573cc1SAndre Oppermann 			 * Unlock the listen socket, lock the newly
11478d573cc1SAndre Oppermann 			 * created socket and update the tp variable.
1148be2ac88cSJonathan Lemon 			 */
11498501a69cSRobert Watson 			INP_WUNLOCK(inp);	/* listen socket */
1150be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
11518501a69cSRobert Watson 			INP_WLOCK(inp);		/* new connection */
1152be2ac88cSJonathan Lemon 			tp = intotcpcb(inp);
11538d573cc1SAndre Oppermann 			KASSERT(tp->t_state == TCPS_SYN_RECEIVED,
11548d573cc1SAndre Oppermann 			    ("%s: ", __func__));
11552903309aSAttilio Rao #ifdef TCP_SIGNATURE
11562903309aSAttilio Rao 			if (sig_checked == 0)  {
11572903309aSAttilio Rao 				tcp_dooptions(&to, optp, optlen,
11582903309aSAttilio Rao 				    (thflags & TH_SYN) ? TO_SYN : 0);
11592903309aSAttilio Rao 				if (!tcp_signature_verify_input(m, off0, tlen,
11602903309aSAttilio Rao 				    optlen, &to, th, tp->t_flags)) {
11612903309aSAttilio Rao 
11622903309aSAttilio Rao 					/*
11632903309aSAttilio Rao 					 * In SYN_SENT state if it receives an
11642903309aSAttilio Rao 					 * RST, it is allowed for further
11652903309aSAttilio Rao 					 * processing.
11662903309aSAttilio Rao 					 */
11672903309aSAttilio Rao 					if ((thflags & TH_RST) == 0 ||
11682903309aSAttilio Rao 					    (tp->t_state == TCPS_SYN_SENT) == 0)
11692903309aSAttilio Rao 						goto dropunlock;
11702903309aSAttilio Rao 				}
11712903309aSAttilio Rao 				sig_checked = 1;
11722903309aSAttilio Rao 			}
11732903309aSAttilio Rao #endif
11742903309aSAttilio Rao 
1175be2ac88cSJonathan Lemon 			/*
1176302ce8d6SAndre Oppermann 			 * Process the segment and the data it
1177302ce8d6SAndre Oppermann 			 * contains.  tcp_do_segment() consumes
1178302ce8d6SAndre Oppermann 			 * the mbuf chain and unlocks the inpcb.
1179302ce8d6SAndre Oppermann 			 */
1180f2512ba1SRui Paulo 			tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen,
1181252ca428SRobert Watson 			    iptos, ti_locked);
1182603724d3SBjoern A. Zeeb 			INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
11838f5a8818SKevin Lo 			return (IPPROTO_DONE);
1184be2ac88cSJonathan Lemon 		}
1185a160e630SAndre Oppermann 		/*
11868d573cc1SAndre Oppermann 		 * Segment flag validation for new connection attempts:
11878d573cc1SAndre Oppermann 		 *
1188a160e630SAndre Oppermann 		 * Our (SYN|ACK) response was rejected.
1189a160e630SAndre Oppermann 		 * Check with syncache and remove entry to prevent
1190a160e630SAndre Oppermann 		 * retransmits.
119119bc77c5SAndre Oppermann 		 *
119219bc77c5SAndre Oppermann 		 * NB: syncache_chkrst does its own logging of failure
119319bc77c5SAndre Oppermann 		 * causes.
1194a160e630SAndre Oppermann 		 */
1195a160e630SAndre Oppermann 		if (thflags & TH_RST) {
119619bc77c5SAndre Oppermann 			syncache_chkrst(&inc, th);
1197a160e630SAndre Oppermann 			goto dropunlock;
1198a160e630SAndre Oppermann 		}
1199a160e630SAndre Oppermann 		/*
1200a160e630SAndre Oppermann 		 * We can't do anything without SYN.
1201a160e630SAndre Oppermann 		 */
1202a160e630SAndre Oppermann 		if ((thflags & TH_SYN) == 0) {
1203a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1204a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1205773673c1SAndre Oppermann 				    "SYN is missing, segment ignored\n",
12068d573cc1SAndre Oppermann 				    s, __func__);
120778b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1208a160e630SAndre Oppermann 			goto dropunlock;
1209a160e630SAndre Oppermann 		}
1210a160e630SAndre Oppermann 		/*
1211a160e630SAndre Oppermann 		 * (SYN|ACK) is bogus on a listen socket.
1212a160e630SAndre Oppermann 		 */
1213fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1214a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1215a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
12168d573cc1SAndre Oppermann 				    "SYN|ACK invalid, segment rejected\n",
12178d573cc1SAndre Oppermann 				    s, __func__);
1218a160e630SAndre Oppermann 			syncache_badack(&inc);	/* XXX: Not needed! */
121978b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1220a57815efSBosko Milekic 			rstreason = BANDLIM_RST_OPENPORT;
1221a57815efSBosko Milekic 			goto dropwithreset;
12224195b4afSPaul Traina 		}
1223a160e630SAndre Oppermann 		/*
1224a160e630SAndre Oppermann 		 * If the drop_synfin option is enabled, drop all
1225a160e630SAndre Oppermann 		 * segments with both the SYN and FIN bits set.
1226a160e630SAndre Oppermann 		 * This prevents e.g. nmap from identifying the
1227a160e630SAndre Oppermann 		 * TCP/IP stack.
1228a160e630SAndre Oppermann 		 * XXX: Poor reasoning.  nmap has other methods
1229a160e630SAndre Oppermann 		 * and is constantly refining its stack detection
1230a160e630SAndre Oppermann 		 * strategies.
12318d573cc1SAndre Oppermann 		 * XXX: This is a violation of the TCP specification
1232a160e630SAndre Oppermann 		 * and was used by RFC1644.
1233a160e630SAndre Oppermann 		 */
1234603724d3SBjoern A. Zeeb 		if ((thflags & TH_FIN) && V_drop_synfin) {
1235a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1236a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1237773673c1SAndre Oppermann 				    "SYN|FIN segment ignored (based on "
12388d573cc1SAndre Oppermann 				    "sysctl setting)\n", s, __func__);
123978b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1240302ce8d6SAndre Oppermann 			goto dropunlock;
1241ebb0cbeaSPaul Traina 		}
1242be2ac88cSJonathan Lemon 		/*
1243be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
1244a160e630SAndre Oppermann 		 *
1245a160e630SAndre Oppermann 		 * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored
1246a160e630SAndre Oppermann 		 * as they do not affect the state of the TCP FSM.
1247a160e630SAndre Oppermann 		 * The data pointed to by TH_URG and th_urp is ignored.
1248be2ac88cSJonathan Lemon 		 */
1249a160e630SAndre Oppermann 		KASSERT((thflags & (TH_RST|TH_ACK)) == 0,
1250a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_RST or TH_ACK set", __func__));
1251a160e630SAndre Oppermann 		KASSERT(thflags & (TH_SYN),
1252a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_SYN not set", __func__));
125333841545SHajimu UMEMOTO #ifdef INET6
125433841545SHajimu UMEMOTO 		/*
125533841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
125633841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
125733841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
125833841545SHajimu UMEMOTO 		 * getting established.
125933841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
126033841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
126133841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
126233841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
126333841545SHajimu UMEMOTO 		 * for the exchange.
126433841545SHajimu UMEMOTO 		 *
126533841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
126633841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
126733841545SHajimu UMEMOTO 		 * SYN in this case.
126833841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
126933841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
127033841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
127133841545SHajimu UMEMOTO 		 *    used"
127233841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
127333841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
127433841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
127533841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
127633841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
127733841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
127833841545SHajimu UMEMOTO 		 *
127933841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
128033841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
128133841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
128233841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
128333841545SHajimu UMEMOTO 		 */
1284603724d3SBjoern A. Zeeb 		if (isipv6 && !V_ip6_use_deprecated) {
128533841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
128633841545SHajimu UMEMOTO 
12873e88eb90SAndrey V. Elsukov 			ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
12888c0fec80SRobert Watson 			if (ia6 != NULL &&
128933841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
12908c0fec80SRobert Watson 				ifa_free(&ia6->ia_ifa);
1291a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1292a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12938d573cc1SAndre Oppermann 					"Connection attempt to deprecated "
12948d573cc1SAndre Oppermann 					"IPv6 address rejected\n",
1295a160e630SAndre Oppermann 					s, __func__);
129633841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
129733841545SHajimu UMEMOTO 				goto dropwithreset;
129833841545SHajimu UMEMOTO 			}
129977d396fdSMaksim Yevmenkin 			if (ia6)
13008c0fec80SRobert Watson 				ifa_free(&ia6->ia_ifa);
130133841545SHajimu UMEMOTO 		}
1302b287c6c7SBjoern A. Zeeb #endif /* INET6 */
130365f28919SJesper Skriver 		/*
1304db33b3e6SAndre Oppermann 		 * Basic sanity checks on incoming SYN requests:
13058d573cc1SAndre Oppermann 		 *   Don't respond if the destination is a link layer
1306db33b3e6SAndre Oppermann 		 *	broadcast according to RFC1122 4.2.3.10, p. 104.
13078d573cc1SAndre Oppermann 		 *   If it is from this socket it must be forged.
13088d573cc1SAndre Oppermann 		 *   Don't respond if the source or destination is a
13098d573cc1SAndre Oppermann 		 *	global or subnet broad- or multicast address.
131093ec91baSCrist J. Clark 		 *   Note that it is quite possible to receive unicast
131193ec91baSCrist J. Clark 		 *	link-layer packets with a broadcast IP address. Use
131293ec91baSCrist J. Clark 		 *	in_broadcast() to find them.
1313be2ac88cSJonathan Lemon 		 */
13148d573cc1SAndre Oppermann 		if (m->m_flags & (M_BCAST|M_MCAST)) {
13158d573cc1SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
13168d573cc1SAndre Oppermann 			    log(LOG_DEBUG, "%s; %s: Listen socket: "
13178d573cc1SAndre Oppermann 				"Connection attempt from broad- or multicast "
1318773673c1SAndre Oppermann 				"link layer address ignored\n", s, __func__);
1319302ce8d6SAndre Oppermann 			goto dropunlock;
13208d573cc1SAndre Oppermann 		}
1321db33b3e6SAndre Oppermann #ifdef INET6
1322b287c6c7SBjoern A. Zeeb 		if (isipv6) {
1323db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1324a160e630SAndre Oppermann 			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) {
1325a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1326a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13278d573cc1SAndre Oppermann 					"Connection attempt to/from self "
1328773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1329302ce8d6SAndre Oppermann 				goto dropunlock;
1330a160e630SAndre Oppermann 			}
1331be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1332a160e630SAndre Oppermann 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
1333a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1334a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13358d573cc1SAndre Oppermann 					"Connection attempt from/to multicast "
1336773673c1SAndre Oppermann 					"address ignored\n", s, __func__);
1337302ce8d6SAndre Oppermann 				goto dropunlock;
1338a160e630SAndre Oppermann 			}
1339b287c6c7SBjoern A. Zeeb 		}
1340db33b3e6SAndre Oppermann #endif
1341b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
1342b287c6c7SBjoern A. Zeeb 		else
1343b287c6c7SBjoern A. Zeeb #endif
1344b287c6c7SBjoern A. Zeeb #ifdef INET
1345b287c6c7SBjoern A. Zeeb 		{
1346db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1347a160e630SAndre Oppermann 			    ip->ip_dst.s_addr == ip->ip_src.s_addr) {
1348a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1349a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13508d573cc1SAndre Oppermann 					"Connection attempt from/to self "
1351773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1352302ce8d6SAndre Oppermann 				goto dropunlock;
1353a160e630SAndre Oppermann 			}
1354be2ac88cSJonathan Lemon 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
1355be2ac88cSJonathan Lemon 			    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
13562ca2159fSCrist J. Clark 			    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
1357a160e630SAndre Oppermann 			    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1358a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1359a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13608d573cc1SAndre Oppermann 					"Connection attempt from/to broad- "
1361773673c1SAndre Oppermann 					"or multicast address ignored\n",
1362a160e630SAndre Oppermann 					s, __func__);
1363302ce8d6SAndre Oppermann 				goto dropunlock;
1364c068736aSJeffrey Hsu 			}
1365a160e630SAndre Oppermann 		}
1366b287c6c7SBjoern A. Zeeb #endif
1367be2ac88cSJonathan Lemon 		/*
1368db33b3e6SAndre Oppermann 		 * SYN appears to be valid.  Create compressed TCP state
1369db33b3e6SAndre Oppermann 		 * for syncache.
1370be2ac88cSJonathan Lemon 		 */
13713c653157SHartmut Brandt #ifdef TCPDEBUG
13723c653157SHartmut Brandt 		if (so->so_options & SO_DEBUG)
13733c653157SHartmut Brandt 			tcp_trace(TA_INPUT, ostate, tp,
13743c653157SHartmut Brandt 			    (void *)tcp_saveipgen, &tcp_savetcp, 0);
13753c653157SHartmut Brandt #endif
1376f72167f4SAndre Oppermann 		tcp_dooptions(&to, optp, optlen, TO_SYN);
137709fe6320SNavdeep Parhar 		syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL);
1378be2ac88cSJonathan Lemon 		/*
13794d6e7130SAndre Oppermann 		 * Entry added to syncache and mbuf consumed.
1380a7c7f2a7SJohn Baldwin 		 * Only the listen socket is unlocked by syncache_add().
1381be2ac88cSJonathan Lemon 		 */
1382a7c7f2a7SJohn Baldwin 		if (ti_locked == TI_WLOCKED) {
1383a7c7f2a7SJohn Baldwin 			INP_INFO_WUNLOCK(&V_tcbinfo);
1384a7c7f2a7SJohn Baldwin 			ti_locked = TI_UNLOCKED;
1385a7c7f2a7SJohn Baldwin 		}
1386603724d3SBjoern A. Zeeb 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
13878f5a8818SKevin Lo 		return (IPPROTO_DONE);
1388982c1675SAndre Oppermann 	} else if (tp->t_state == TCPS_LISTEN) {
1389982c1675SAndre Oppermann 		/*
1390982c1675SAndre Oppermann 		 * When a listen socket is torn down the SO_ACCEPTCONN
1391982c1675SAndre Oppermann 		 * flag is removed first while connections are drained
1392982c1675SAndre Oppermann 		 * from the accept queue in a unlock/lock cycle of the
1393982c1675SAndre Oppermann 		 * ACCEPT_LOCK, opening a race condition allowing a SYN
1394982c1675SAndre Oppermann 		 * attempt go through unhandled.
1395982c1675SAndre Oppermann 		 */
1396982c1675SAndre Oppermann 		goto dropunlock;
1397f76fcf6dSJeffrey Hsu 	}
1398db33b3e6SAndre Oppermann 
13992903309aSAttilio Rao #ifdef TCP_SIGNATURE
14002903309aSAttilio Rao 	if (sig_checked == 0)  {
14012903309aSAttilio Rao 		tcp_dooptions(&to, optp, optlen,
14022903309aSAttilio Rao 		    (thflags & TH_SYN) ? TO_SYN : 0);
14032903309aSAttilio Rao 		if (!tcp_signature_verify_input(m, off0, tlen, optlen, &to,
14042903309aSAttilio Rao 		    th, tp->t_flags)) {
14052903309aSAttilio Rao 
14062903309aSAttilio Rao 			/*
14072903309aSAttilio Rao 			 * In SYN_SENT state if it receives an RST, it is
14082903309aSAttilio Rao 			 * allowed for further processing.
14092903309aSAttilio Rao 			 */
14102903309aSAttilio Rao 			if ((thflags & TH_RST) == 0 ||
14112903309aSAttilio Rao 			    (tp->t_state == TCPS_SYN_SENT) == 0)
14122903309aSAttilio Rao 				goto dropunlock;
14132903309aSAttilio Rao 		}
14142903309aSAttilio Rao 		sig_checked = 1;
14152903309aSAttilio Rao 	}
14162903309aSAttilio Rao #endif
14172903309aSAttilio Rao 
1418fa22ce15SAdrian Chadd 	TCP_PROBE5(receive, NULL, tp, mtod(m, const char *), tp, th);
141957f60867SMark Johnston 
1420302ce8d6SAndre Oppermann 	/*
14218d573cc1SAndre Oppermann 	 * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later
14228d573cc1SAndre Oppermann 	 * state.  tcp_do_segment() always consumes the mbuf chain, unlocks
14238d573cc1SAndre Oppermann 	 * the inpcb, and unlocks pcbinfo.
1424302ce8d6SAndre Oppermann 	 */
1425252ca428SRobert Watson 	tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked);
1426603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
14278f5a8818SKevin Lo 	return (IPPROTO_DONE);
1428be2ac88cSJonathan Lemon 
1429302ce8d6SAndre Oppermann dropwithreset:
1430fa22ce15SAdrian Chadd 	TCP_PROBE5(receive, NULL, tp, mtod(m, const char *), tp, th);
143157f60867SMark Johnston 
1432fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED) {
1433dd8ac7f9SRobert Watson 		INP_INFO_WUNLOCK(&V_tcbinfo);
1434252ca428SRobert Watson 		ti_locked = TI_UNLOCKED;
1435fa046d87SRobert Watson 	}
1436fa046d87SRobert Watson #ifdef INVARIANTS
1437fa046d87SRobert Watson 	else {
1438fa046d87SRobert Watson 		KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropwithreset "
1439fa046d87SRobert Watson 		    "ti_locked: %d", __func__, ti_locked));
1440fa046d87SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1441fa046d87SRobert Watson 	}
1442fa046d87SRobert Watson #endif
1443014ea782SRobert Watson 
1444014ea782SRobert Watson 	if (inp != NULL) {
1445302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
1446014ea782SRobert Watson 		INP_WUNLOCK(inp);
1447dd8ac7f9SRobert Watson 	} else
1448014ea782SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
1449302ce8d6SAndre Oppermann 	m = NULL;	/* mbuf chain got consumed. */
1450014ea782SRobert Watson 	goto drop;
1451014ea782SRobert Watson 
1452302ce8d6SAndre Oppermann dropunlock:
145357f60867SMark Johnston 	if (m != NULL)
1454fa22ce15SAdrian Chadd 		TCP_PROBE5(receive, NULL, tp, mtod(m, const char *), tp, th);
145557f60867SMark Johnston 
1456fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED) {
1457252ca428SRobert Watson 		INP_INFO_WUNLOCK(&V_tcbinfo);
1458252ca428SRobert Watson 		ti_locked = TI_UNLOCKED;
1459fa046d87SRobert Watson 	}
1460fa046d87SRobert Watson #ifdef INVARIANTS
1461fa046d87SRobert Watson 	else {
1462fa046d87SRobert Watson 		KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropunlock "
1463fa046d87SRobert Watson 		    "ti_locked: %d", __func__, ti_locked));
1464fa046d87SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1465fa046d87SRobert Watson 	}
1466fa046d87SRobert Watson #endif
1467252ca428SRobert Watson 
14689fa198beSAndre Oppermann 	if (inp != NULL)
14698501a69cSRobert Watson 		INP_WUNLOCK(inp);
1470014ea782SRobert Watson 
1471302ce8d6SAndre Oppermann drop:
1472603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1473a160e630SAndre Oppermann 	if (s != NULL)
1474a160e630SAndre Oppermann 		free(s, M_TCPLOG);
1475302ce8d6SAndre Oppermann 	if (m != NULL)
1476302ce8d6SAndre Oppermann 		m_freem(m);
14778f5a8818SKevin Lo 	return (IPPROTO_DONE);
1478302ce8d6SAndre Oppermann }
1479302ce8d6SAndre Oppermann 
1480679d9708SAndre Oppermann static void
1481302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
1482252ca428SRobert Watson     struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos,
1483252ca428SRobert Watson     int ti_locked)
1484302ce8d6SAndre Oppermann {
1485302ce8d6SAndre Oppermann 	int thflags, acked, ourfinisacked, needoutput = 0;
1486302ce8d6SAndre Oppermann 	int rstreason, todrop, win;
1487302ce8d6SAndre Oppermann 	u_long tiwin;
148807dacf03SAndre Oppermann 	char *s;
148907dacf03SAndre Oppermann 	struct in_conninfo *inc;
1490c11a15bfSGleb Smirnoff 	struct mbuf *mfree;
1491302ce8d6SAndre Oppermann 	struct tcpopt to;
1492302ce8d6SAndre Oppermann 
149314739780SMaxim Konovalov #ifdef TCPDEBUG
149414739780SMaxim Konovalov 	/*
149514739780SMaxim Konovalov 	 * The size of tcp_saveipgen must be the size of the max ip header,
149614739780SMaxim Konovalov 	 * now IPv6.
149714739780SMaxim Konovalov 	 */
149814739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
149914739780SMaxim Konovalov 	struct tcphdr tcp_savetcp;
150014739780SMaxim Konovalov 	short ostate = 0;
150114739780SMaxim Konovalov #endif
1502302ce8d6SAndre Oppermann 	thflags = th->th_flags;
150307dacf03SAndre Oppermann 	inc = &tp->t_inpcb->inp_inc;
1504d64a46eaSLawrence Stewart 	tp->sackhint.last_sack_ack = 0;
1505302ce8d6SAndre Oppermann 
1506252ca428SRobert Watson 	/*
1507252ca428SRobert Watson 	 * If this is either a state-changing packet or current state isn't
1508252ca428SRobert Watson 	 * established, we require a write lock on tcbinfo.  Otherwise, we
15090989f56cSRobert Watson 	 * allow the tcbinfo to be in either alocked or unlocked, as the
15100989f56cSRobert Watson 	 * caller may have unnecessarily acquired a write lock due to a race.
1511252ca428SRobert Watson 	 */
1512252ca428SRobert Watson 	if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 ||
1513252ca428SRobert Watson 	    tp->t_state != TCPS_ESTABLISHED) {
1514252ca428SRobert Watson 		KASSERT(ti_locked == TI_WLOCKED, ("%s ti_locked %d for "
1515252ca428SRobert Watson 		    "SYN/FIN/RST/!EST", __func__, ti_locked));
1516603724d3SBjoern A. Zeeb 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1517252ca428SRobert Watson 	} else {
1518252ca428SRobert Watson #ifdef INVARIANTS
1519fa046d87SRobert Watson 		if (ti_locked == TI_WLOCKED)
1520252ca428SRobert Watson 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1521fa046d87SRobert Watson 		else {
1522fa046d87SRobert Watson 			KASSERT(ti_locked == TI_UNLOCKED, ("%s: EST "
1523fa046d87SRobert Watson 			    "ti_locked: %d", __func__, ti_locked));
1524fa046d87SRobert Watson 			INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1525fa046d87SRobert Watson 		}
1526252ca428SRobert Watson #endif
1527252ca428SRobert Watson 	}
15288501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1529679d9708SAndre Oppermann 	KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
1530679d9708SAndre Oppermann 	    __func__));
1531679d9708SAndre Oppermann 	KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
1532679d9708SAndre Oppermann 	    __func__));
1533df8bae1dSRodney W. Grimes 
1534df8bae1dSRodney W. Grimes 	/*
1535df8bae1dSRodney W. Grimes 	 * Segment received on connection.
1536df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
1537e8949f74SAndre Oppermann 	 * XXX: This should be done after segment
1538e8949f74SAndre Oppermann 	 * validation to ignore broken/spoofed segs.
1539df8bae1dSRodney W. Grimes 	 */
15409b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
15417ff19458SPaul Traina 	if (TCPS_HAVEESTABLISHED(tp->t_state))
15429077f387SGleb Smirnoff 		tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
1543df8bae1dSRodney W. Grimes 
1544df8bae1dSRodney W. Grimes 	/*
1545464fcfbcSAndre Oppermann 	 * Unscale the window into a 32-bit value.
1546e8949f74SAndre Oppermann 	 * For the SYN_SENT state the scale is zero.
1547464fcfbcSAndre Oppermann 	 */
1548464fcfbcSAndre Oppermann 	tiwin = th->th_win << tp->snd_scale;
1549464fcfbcSAndre Oppermann 
1550464fcfbcSAndre Oppermann 	/*
1551f2512ba1SRui Paulo 	 * TCP ECN processing.
1552f2512ba1SRui Paulo 	 */
1553f2512ba1SRui Paulo 	if (tp->t_flags & TF_ECN_PERMIT) {
15549c251892SRui Paulo 		if (thflags & TH_CWR)
15559c251892SRui Paulo 			tp->t_flags &= ~TF_ECN_SND_ECE;
1556f2512ba1SRui Paulo 		switch (iptos & IPTOS_ECN_MASK) {
1557f2512ba1SRui Paulo 		case IPTOS_ECN_CE:
1558f2512ba1SRui Paulo 			tp->t_flags |= TF_ECN_SND_ECE;
155978b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ce);
1560f2512ba1SRui Paulo 			break;
1561f2512ba1SRui Paulo 		case IPTOS_ECN_ECT0:
156278b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect0);
1563f2512ba1SRui Paulo 			break;
1564f2512ba1SRui Paulo 		case IPTOS_ECN_ECT1:
156578b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect1);
1566f2512ba1SRui Paulo 			break;
1567f2512ba1SRui Paulo 		}
1568*64807b30SHiren Panchasara 
1569*64807b30SHiren Panchasara 		/* Process a packet differently from RFC3168. */
1570*64807b30SHiren Panchasara 		cc_ecnpkt_handler(tp, th, iptos);
1571*64807b30SHiren Panchasara 
1572dbc42409SLawrence Stewart 		/* Congestion experienced. */
1573dbc42409SLawrence Stewart 		if (thflags & TH_ECE) {
1574dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_ECN);
1575f2512ba1SRui Paulo 		}
1576f2512ba1SRui Paulo 	}
1577f2512ba1SRui Paulo 
1578f2512ba1SRui Paulo 	/*
1579f72167f4SAndre Oppermann 	 * Parse options on any incoming segment.
1580f72167f4SAndre Oppermann 	 */
1581302ce8d6SAndre Oppermann 	tcp_dooptions(&to, (u_char *)(th + 1),
1582302ce8d6SAndre Oppermann 	    (th->th_off << 2) - sizeof(struct tcphdr),
1583302ce8d6SAndre Oppermann 	    (thflags & TH_SYN) ? TO_SYN : 0);
1584f72167f4SAndre Oppermann 
1585f72167f4SAndre Oppermann 	/*
1586f72167f4SAndre Oppermann 	 * If echoed timestamp is later than the current time,
1587bf6d304aSAndre Oppermann 	 * fall back to non RFC1323 RTT calculation.  Normalize
1588bf6d304aSAndre Oppermann 	 * timestamp if syncookies were used when this connection
1589bf6d304aSAndre Oppermann 	 * was established.
1590f72167f4SAndre Oppermann 	 */
1591bf6d304aSAndre Oppermann 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
1592e16fa5caSJohn-Mark Gurney 		to.to_tsecr -= tp->ts_offset;
1593d8951c8aSBjoern A. Zeeb 		if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks()))
1594f72167f4SAndre Oppermann 			to.to_tsecr = 0;
1595bf6d304aSAndre Oppermann 	}
159607dacf03SAndre Oppermann 	/*
159707dacf03SAndre Oppermann 	 * If timestamps were negotiated during SYN/ACK they should
159807dacf03SAndre Oppermann 	 * appear on every segment during this session and vice versa.
159907dacf03SAndre Oppermann 	 */
160007dacf03SAndre Oppermann 	if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) {
160107dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
160207dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: Timestamp missing, "
160307dacf03SAndre Oppermann 			    "no action\n", s, __func__);
160407dacf03SAndre Oppermann 			free(s, M_TCPLOG);
160507dacf03SAndre Oppermann 		}
160607dacf03SAndre Oppermann 	}
160707dacf03SAndre Oppermann 	if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) {
160807dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
160907dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: Timestamp not expected, "
161007dacf03SAndre Oppermann 			    "no action\n", s, __func__);
161107dacf03SAndre Oppermann 			free(s, M_TCPLOG);
161207dacf03SAndre Oppermann 		}
161307dacf03SAndre Oppermann 	}
1614f72167f4SAndre Oppermann 
1615f72167f4SAndre Oppermann 	/*
161697d8d152SAndre Oppermann 	 * Process options only when we get SYN/ACK back. The SYN case
161797d8d152SAndre Oppermann 	 * for incoming connections is handled in tcp_syncache.
16185396d0f8SAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
16195396d0f8SAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.
162097d8d152SAndre Oppermann 	 * XXX this is traditional behavior, may need to be cleaned up.
1621df8bae1dSRodney W. Grimes 	 */
16220a389eabSSimon L. B. Nielsen 	if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
1623464fcfbcSAndre Oppermann 		if ((to.to_flags & TOF_SCALE) &&
1624464fcfbcSAndre Oppermann 		    (tp->t_flags & TF_REQ_SCALE)) {
1625be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
162602a1a643SAndre Oppermann 			tp->snd_scale = to.to_wscale;
1627be2ac88cSJonathan Lemon 		}
16285396d0f8SAndre Oppermann 		/*
16295396d0f8SAndre Oppermann 		 * Initial send window.  It will be updated with
16305396d0f8SAndre Oppermann 		 * the next incoming segment to the scaled value.
16315396d0f8SAndre Oppermann 		 */
16325396d0f8SAndre Oppermann 		tp->snd_wnd = th->th_win;
1633be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_TS) {
1634be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
1635be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
1636d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1637be2ac88cSJonathan Lemon 		}
1638be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_MSS)
1639be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
16403529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
16413529149eSAndre Oppermann 		    (to.to_flags & TOF_SACKPERM) == 0)
16423529149eSAndre Oppermann 			tp->t_flags &= ~TF_SACK_PERMIT;
16436d90faf3SPaul Saab 	}
16446d90faf3SPaul Saab 
1645df8bae1dSRodney W. Grimes 	/*
1646df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
1647df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
1648df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
1649df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
1650df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
1651df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
1652df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
1653df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
1654df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
1655df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
1656df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
1657df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
1658a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
1659c5ad39b9SAndre Oppermann 	 * Since we check for TCPS_ESTABLISHED first, it can only
1660a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
1661df8bae1dSRodney W. Grimes 	 */
1662df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
1663c5ad39b9SAndre Oppermann 	    th->th_seq == tp->rcv_nxt &&
1664fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1665c5ad39b9SAndre Oppermann 	    tp->snd_nxt == tp->snd_max &&
1666c5ad39b9SAndre Oppermann 	    tiwin && tiwin == tp->snd_wnd &&
1667a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
1668e407b67bSGleb Smirnoff 	    tp->t_segq == NULL && ((to.to_flags & TOF_TS) == 0 ||
1669c5ad39b9SAndre Oppermann 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) {
1670df8bae1dSRodney W. Grimes 
1671df8bae1dSRodney W. Grimes 		/*
1672df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
1673df8bae1dSRodney W. Grimes 		 * record the timestamp.
1674a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
1675a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1676df8bae1dSRodney W. Grimes 		 */
1677be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
1678fb59c426SYoshinobu Inoue 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1679d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1680a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
1681df8bae1dSRodney W. Grimes 		}
1682df8bae1dSRodney W. Grimes 
1683fb59c426SYoshinobu Inoue 		if (tlen == 0) {
1684fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
1685fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
1686dbc42409SLawrence Stewart 			    !IN_RECOVERY(tp->t_flags) &&
1687fc30a251SAndre Oppermann 			    (to.to_flags & TOF_SACK) == 0 &&
1688dbc42409SLawrence Stewart 			    TAILQ_EMPTY(&tp->snd_holes)) {
1689df8bae1dSRodney W. Grimes 				/*
1690e8949f74SAndre Oppermann 				 * This is a pure ack for outstanding data.
1691df8bae1dSRodney W. Grimes 				 */
1692fa046d87SRobert Watson 				if (ti_locked == TI_WLOCKED)
1693252ca428SRobert Watson 					INP_INFO_WUNLOCK(&V_tcbinfo);
1694252ca428SRobert Watson 				ti_locked = TI_UNLOCKED;
1695252ca428SRobert Watson 
169678b50714SRobert Watson 				TCPSTAT_INC(tcps_predack);
1697252ca428SRobert Watson 
16989b8b58e0SJonathan Lemon 				/*
1699e8949f74SAndre Oppermann 				 * "bad retransmit" recovery.
17009b8b58e0SJonathan Lemon 				 */
17019b8b58e0SJonathan Lemon 				if (tp->t_rxtshift == 1 &&
1702672dc4aeSJohn Baldwin 				    tp->t_flags & TF_PREVVALID &&
17036dfb8b31SJohn Baldwin 				    (int)(ticks - tp->t_badrxtwin) < 0) {
1704dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_RTO_ERR);
17059b8b58e0SJonathan Lemon 				}
1706fa55172bSMatthew Dillon 
1707fa55172bSMatthew Dillon 				/*
1708fa55172bSMatthew Dillon 				 * Recalculate the transmit timer / rtt.
1709fa55172bSMatthew Dillon 				 *
1710fa55172bSMatthew Dillon 				 * Some boxes send broken timestamp replies
1711fa55172bSMatthew Dillon 				 * during the SYN+ACK phase, ignore
1712fa55172bSMatthew Dillon 				 * timestamps of 0 or we could calculate a
1713fa55172bSMatthew Dillon 				 * huge RTT and blow up the retransmit timer.
1714fa55172bSMatthew Dillon 				 */
1715fa55172bSMatthew Dillon 				if ((to.to_flags & TOF_TS) != 0 &&
1716fa55172bSMatthew Dillon 				    to.to_tsecr) {
1717d8951c8aSBjoern A. Zeeb 					u_int t;
1718d8951c8aSBjoern A. Zeeb 
1719d8951c8aSBjoern A. Zeeb 					t = tcp_ts_getticks() - to.to_tsecr;
1720d8951c8aSBjoern A. Zeeb 					if (!tp->t_rttlow || tp->t_rttlow > t)
1721d8951c8aSBjoern A. Zeeb 						tp->t_rttlow = t;
1722a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
1723d8951c8aSBjoern A. Zeeb 					    TCP_TS_TO_TICKS(t) + 1);
1724fa55172bSMatthew Dillon 				} else if (tp->t_rtttime &&
1725fa55172bSMatthew Dillon 				    SEQ_GT(th->th_ack, tp->t_rtseq)) {
1726eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1727eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - tp->t_rtttime)
1728eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - tp->t_rtttime;
1729c068736aSJeffrey Hsu 					tcp_xmit_timer(tp,
1730c068736aSJeffrey Hsu 							ticks - tp->t_rtttime);
1731fa55172bSMatthew Dillon 				}
1732dbc42409SLawrence Stewart 				acked = BYTES_THIS_ACK(tp, th);
173339bc9de5SLawrence Stewart 
173439bc9de5SLawrence Stewart 				/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
173539bc9de5SLawrence Stewart 				hhook_run_tcp_est_in(tp, th, &to);
173639bc9de5SLawrence Stewart 
173778b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvackpack);
173878b50714SRobert Watson 				TCPSTAT_ADD(tcps_rcvackbyte, acked);
1739df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
17409d11646dSJeffrey Hsu 				if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
17419d11646dSJeffrey Hsu 				    SEQ_LEQ(th->th_ack, tp->snd_recover))
17429d11646dSJeffrey Hsu 					tp->snd_recover = th->th_ack - 1;
1743dbc42409SLawrence Stewart 
1744dbc42409SLawrence Stewart 				/*
1745dbc42409SLawrence Stewart 				 * Let the congestion control algorithm update
1746dbc42409SLawrence Stewart 				 * congestion control related information. This
1747dbc42409SLawrence Stewart 				 * typically means increasing the congestion
1748dbc42409SLawrence Stewart 				 * window.
1749dbc42409SLawrence Stewart 				 */
1750dbc42409SLawrence Stewart 				cc_ack_received(tp, th, CC_ACK);
1751dbc42409SLawrence Stewart 
17529d11646dSJeffrey Hsu 				tp->snd_una = th->th_ack;
17531645d090SJeff Roberson 				/*
1754e8949f74SAndre Oppermann 				 * Pull snd_wl2 up to prevent seq wrap relative
17551645d090SJeff Roberson 				 * to th_ack.
17561645d090SJeff Roberson 				 */
1757cb942153SJeffrey Hsu 				tp->snd_wl2 = th->th_ack;
1758b5addd85SJeffrey Hsu 				tp->t_dupacks = 0;
1759df8bae1dSRodney W. Grimes 				m_freem(m);
1760e8949f74SAndre Oppermann 				ND6_HINT(tp); /* Some progress has been made. */
1761df8bae1dSRodney W. Grimes 
1762df8bae1dSRodney W. Grimes 				/*
1763df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
1764df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
1765df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
1766df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
1767df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
1768df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
1769df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
1770e8949f74SAndre Oppermann 				 */
17713c653157SHartmut Brandt #ifdef TCPDEBUG
17723c653157SHartmut Brandt 				if (so->so_options & SO_DEBUG)
17733c653157SHartmut Brandt 					tcp_trace(TA_INPUT, ostate, tp,
17743c653157SHartmut Brandt 					    (void *)tcp_saveipgen,
17753c653157SHartmut Brandt 					    &tcp_savetcp, 0);
17763c653157SHartmut Brandt #endif
1777df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
1778b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
1779b8152ba7SAndre Oppermann 				else if (!tcp_timer_active(tp, TT_PERSIST))
1780b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT,
1781b8152ba7SAndre Oppermann 						      tp->t_rxtcur);
1782df8bae1dSRodney W. Grimes 				sowwakeup(so);
1783cfa6009eSGleb Smirnoff 				if (sbavail(&so->so_snd))
1784df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1785a14c749fSJonathan Lemon 				goto check_delack;
1786df8bae1dSRodney W. Grimes 			}
1787fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1788fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
17896741ecf5SAndre Oppermann 			int newsize = 0;	/* automatic sockbuf scaling */
17906741ecf5SAndre Oppermann 
1791df8bae1dSRodney W. Grimes 			/*
1792252ca428SRobert Watson 			 * This is a pure, in-sequence data packet with
1793252ca428SRobert Watson 			 * nothing on the reassembly queue and we have enough
1794252ca428SRobert Watson 			 * buffer space to take it.
1795df8bae1dSRodney W. Grimes 			 */
1796fa046d87SRobert Watson 			if (ti_locked == TI_WLOCKED)
1797252ca428SRobert Watson 				INP_INFO_WUNLOCK(&V_tcbinfo);
1798252ca428SRobert Watson 			ti_locked = TI_UNLOCKED;
1799252ca428SRobert Watson 
18006d90faf3SPaul Saab 			/* Clean receiver SACK report if present */
18013529149eSAndre Oppermann 			if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks)
18026d90faf3SPaul Saab 				tcp_clean_sackreport(tp);
180378b50714SRobert Watson 			TCPSTAT_INC(tcps_preddat);
1804fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
18051645d090SJeff Roberson 			/*
18061645d090SJeff Roberson 			 * Pull snd_wl1 up to prevent seq wrap relative to
18071645d090SJeff Roberson 			 * th_seq.
18081645d090SJeff Roberson 			 */
180960ee3bb2SAndre Oppermann 			tp->snd_wl1 = th->th_seq;
18101645d090SJeff Roberson 			/*
18111645d090SJeff Roberson 			 * Pull rcv_up up to prevent seq wrap relative to
18121645d090SJeff Roberson 			 * rcv_nxt.
18131645d090SJeff Roberson 			 */
18141645d090SJeff Roberson 			tp->rcv_up = tp->rcv_nxt;
181578b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpack);
181678b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
1817e8949f74SAndre Oppermann 			ND6_HINT(tp);	/* Some progress has been made */
18183c653157SHartmut Brandt #ifdef TCPDEBUG
18193c653157SHartmut Brandt 			if (so->so_options & SO_DEBUG)
18203c653157SHartmut Brandt 				tcp_trace(TA_INPUT, ostate, tp,
18213c653157SHartmut Brandt 				    (void *)tcp_saveipgen, &tcp_savetcp, 0);
18223c653157SHartmut Brandt #endif
18236741ecf5SAndre Oppermann 		/*
18246741ecf5SAndre Oppermann 		 * Automatic sizing of receive socket buffer.  Often the send
18256741ecf5SAndre Oppermann 		 * buffer size is not optimally adjusted to the actual network
18266741ecf5SAndre Oppermann 		 * conditions at hand (delay bandwidth product).  Setting the
18276741ecf5SAndre Oppermann 		 * buffer size too small limits throughput on links with high
18286741ecf5SAndre Oppermann 		 * bandwidth and high delay (eg. trans-continental/oceanic links).
18296741ecf5SAndre Oppermann 		 *
18306741ecf5SAndre Oppermann 		 * On the receive side the socket buffer memory is only rarely
18316741ecf5SAndre Oppermann 		 * used to any significant extent.  This allows us to be much
18326741ecf5SAndre Oppermann 		 * more aggressive in scaling the receive socket buffer.  For
18336741ecf5SAndre Oppermann 		 * the case that the buffer space is actually used to a large
18346741ecf5SAndre Oppermann 		 * extent and we run out of kernel memory we can simply drop
18356741ecf5SAndre Oppermann 		 * the new segments; TCP on the sender will just retransmit it
18366741ecf5SAndre Oppermann 		 * later.  Setting the buffer size too big may only consume too
18376741ecf5SAndre Oppermann 		 * much kernel memory if the application doesn't read() from
18386741ecf5SAndre Oppermann 		 * the socket or packet loss or reordering makes use of the
18396741ecf5SAndre Oppermann 		 * reassembly queue.
18406741ecf5SAndre Oppermann 		 *
18416741ecf5SAndre Oppermann 		 * The criteria to step up the receive buffer one notch are:
1842f7469d3eSHiren Panchasara 		 *  1. Application has not set receive buffer size with
1843f7469d3eSHiren Panchasara 		 *     SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE.
1844f7469d3eSHiren Panchasara 		 *  2. the number of bytes received during the time it takes
18456741ecf5SAndre Oppermann 		 *     one timestamp to be reflected back to us (the RTT);
1846f7469d3eSHiren Panchasara 		 *  3. received bytes per RTT is within seven eighth of the
18476741ecf5SAndre Oppermann 		 *     current socket buffer size;
1848f7469d3eSHiren Panchasara 		 *  4. receive buffer size has not hit maximal automatic size;
18496741ecf5SAndre Oppermann 		 *
18506741ecf5SAndre Oppermann 		 * This algorithm does one step per RTT at most and only if
18516741ecf5SAndre Oppermann 		 * we receive a bulk stream w/o packet losses or reorderings.
18526741ecf5SAndre Oppermann 		 * Shrinking the buffer during idle times is not necessary as
18536741ecf5SAndre Oppermann 		 * it doesn't consume any memory when idle.
18546741ecf5SAndre Oppermann 		 *
18556741ecf5SAndre Oppermann 		 * TODO: Only step up if the application is actually serving
18566741ecf5SAndre Oppermann 		 * the buffer to better manage the socket buffer resources.
1857df8bae1dSRodney W. Grimes 		 */
1858603724d3SBjoern A. Zeeb 			if (V_tcp_do_autorcvbuf &&
18596741ecf5SAndre Oppermann 			    to.to_tsecr &&
18606741ecf5SAndre Oppermann 			    (so->so_rcv.sb_flags & SB_AUTOSIZE)) {
18618502ec25SAndre Oppermann 				if (TSTMP_GT(to.to_tsecr, tp->rfbuf_ts) &&
18626741ecf5SAndre Oppermann 				    to.to_tsecr - tp->rfbuf_ts < hz) {
18636741ecf5SAndre Oppermann 					if (tp->rfbuf_cnt >
18646741ecf5SAndre Oppermann 					    (so->so_rcv.sb_hiwat / 8 * 7) &&
18656741ecf5SAndre Oppermann 					    so->so_rcv.sb_hiwat <
1866603724d3SBjoern A. Zeeb 					    V_tcp_autorcvbuf_max) {
18676741ecf5SAndre Oppermann 						newsize =
18686741ecf5SAndre Oppermann 						    min(so->so_rcv.sb_hiwat +
1869603724d3SBjoern A. Zeeb 						    V_tcp_autorcvbuf_inc,
1870603724d3SBjoern A. Zeeb 						    V_tcp_autorcvbuf_max);
18716741ecf5SAndre Oppermann 					}
18726741ecf5SAndre Oppermann 					/* Start over with next RTT. */
18736741ecf5SAndre Oppermann 					tp->rfbuf_ts = 0;
18746741ecf5SAndre Oppermann 					tp->rfbuf_cnt = 0;
18756741ecf5SAndre Oppermann 				} else
18766741ecf5SAndre Oppermann 					tp->rfbuf_cnt += tlen;	/* add up */
18776741ecf5SAndre Oppermann 			}
18786741ecf5SAndre Oppermann 
18796741ecf5SAndre Oppermann 			/* Add data to socket buffer. */
18801e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
1881c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1882c1c36a2cSMike Silbersack 				m_freem(m);
1883c1c36a2cSMike Silbersack 			} else {
18846741ecf5SAndre Oppermann 				/*
18856741ecf5SAndre Oppermann 				 * Set new socket buffer size.
18866741ecf5SAndre Oppermann 				 * Give up when limit is reached.
18876741ecf5SAndre Oppermann 				 */
18886741ecf5SAndre Oppermann 				if (newsize)
18896741ecf5SAndre Oppermann 					if (!sbreserve_locked(&so->so_rcv,
18906c8286e4SRobert Watson 					    newsize, so, NULL))
18916741ecf5SAndre Oppermann 						so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1892fb59c426SYoshinobu Inoue 				m_adj(m, drop_hdrlen);	/* delayed header drop */
1893651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
1894c1c36a2cSMike Silbersack 			}
1895e8949f74SAndre Oppermann 			/* NB: sorwakeup_locked() does an implicit unlock. */
18961e4d7da7SRobert Watson 			sorwakeup_locked(so);
1897c1e5a6e5SAndre Oppermann 			if (DELAY_ACK(tp, tlen)) {
18983bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
1899f498eeeeSDavid Greenman 			} else {
1900e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
1901e612a582SDavid Greenman 				tcp_output(tp);
1902e612a582SDavid Greenman 			}
1903a14c749fSJonathan Lemon 			goto check_delack;
1904df8bae1dSRodney W. Grimes 		}
1905df8bae1dSRodney W. Grimes 	}
1906df8bae1dSRodney W. Grimes 
1907df8bae1dSRodney W. Grimes 	/*
1908df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1909df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1910df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1911df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1912df8bae1dSRodney W. Grimes 	 */
1913df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1914df8bae1dSRodney W. Grimes 	if (win < 0)
1915df8bae1dSRodney W. Grimes 		win = 0;
191666e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1917df8bae1dSRodney W. Grimes 
19186741ecf5SAndre Oppermann 	/* Reset receive buffer auto scaling when not in bulk receive mode. */
19196741ecf5SAndre Oppermann 	tp->rfbuf_ts = 0;
19206741ecf5SAndre Oppermann 	tp->rfbuf_cnt = 0;
19216741ecf5SAndre Oppermann 
1922df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1923df8bae1dSRodney W. Grimes 
1924df8bae1dSRodney W. Grimes 	/*
1925764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1926764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1927764d8cefSBill Fenner 	 */
1928764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1929fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1930fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1931a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1932a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1933a57815efSBosko Milekic 				goto dropwithreset;
1934a57815efSBosko Milekic 		}
1935764d8cefSBill Fenner 		break;
1936764d8cefSBill Fenner 
1937764d8cefSBill Fenner 	/*
1938df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
1939df8bae1dSRodney W. Grimes 	 *	if seg contains an ACK, but not for our SYN, drop the input.
1940df8bae1dSRodney W. Grimes 	 *	if seg contains a RST, then drop the connection.
1941df8bae1dSRodney W. Grimes 	 *	if seg does not contain SYN, then drop it.
1942df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
1943df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
1944df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
1945f2512ba1SRui Paulo 	 *	if seg contains an ECE and ECN support is enabled, the stream
1946f2512ba1SRui Paulo 	 *	    is ECN capable.
1947df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1948df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
1949df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
1950df8bae1dSRodney W. Grimes 	 */
1951df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
1952fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1953fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
1954fb59c426SYoshinobu Inoue 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1955a57815efSBosko Milekic 			rstreason = BANDLIM_UNLIMITED;
1956a0292f23SGarrett Wollman 			goto dropwithreset;
1957a0292f23SGarrett Wollman 		}
195857f60867SMark Johnston 		if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) {
1959d9fae5abSAndriy Gapon 			TCP_PROBE5(connect__refused, NULL, tp,
1960fa22ce15SAdrian Chadd 			    mtod(m, const char *), tp, th);
1961df8bae1dSRodney W. Grimes 			tp = tcp_drop(tp, ECONNREFUSED);
196257f60867SMark Johnston 		}
19630ca3f933SAndre Oppermann 		if (thflags & TH_RST)
1964df8bae1dSRodney W. Grimes 			goto drop;
19650ca3f933SAndre Oppermann 		if (!(thflags & TH_SYN))
1966df8bae1dSRodney W. Grimes 			goto drop;
1967464fcfbcSAndre Oppermann 
1968fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
1969df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
1970fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
197178b50714SRobert Watson 			TCPSTAT_INC(tcps_connects);
1972845799c1SAndras Olah 			soisconnected(so);
1973c488362eSRobert Watson #ifdef MAC
197430d239bcSRobert Watson 			mac_socketpeer_set_from_mbuf(m, so);
1975c488362eSRobert Watson #endif
1976845799c1SAndras Olah 			/* Do window scaling on this connection? */
1977845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1978845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1979845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
1980845799c1SAndras Olah 			}
1981766282cbSJohn Baldwin 			tp->rcv_adv += imin(tp->rcv_wnd,
1982766282cbSJohn Baldwin 			    TCP_MAXWIN << tp->rcv_scale);
1983a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
1984a0292f23SGarrett Wollman 			/*
1985a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
1986a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
1987a0292f23SGarrett Wollman 			 */
1988c1e5a6e5SAndre Oppermann 			if (DELAY_ACK(tp, tlen) && tlen != 0)
1989b8152ba7SAndre Oppermann 				tcp_timer_activate(tp, TT_DELACK,
1990b8152ba7SAndre Oppermann 				    tcp_delacktime);
1991a0292f23SGarrett Wollman 			else
1992a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
1993f2512ba1SRui Paulo 
1994603724d3SBjoern A. Zeeb 			if ((thflags & TH_ECE) && V_tcp_do_ecn) {
1995f2512ba1SRui Paulo 				tp->t_flags |= TF_ECN_PERMIT;
199678b50714SRobert Watson 				TCPSTAT_INC(tcps_ecn_shs);
1997f2512ba1SRui Paulo 			}
1998f2512ba1SRui Paulo 
1999a0292f23SGarrett Wollman 			/*
2000a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
2001a0292f23SGarrett Wollman 			 * Transitions:
2002a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
2003a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
2004a0292f23SGarrett Wollman 			 */
20059b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
2006a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
200757f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_1);
2008a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
2009fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
20107ff19458SPaul Traina 			} else {
201157f60867SMark Johnston 				tcp_state_change(tp, TCPS_ESTABLISHED);
2012d9fae5abSAndriy Gapon 				TCP_PROBE5(connect__established, NULL, tp,
2013fa22ce15SAdrian Chadd 				    mtod(m, const char *), tp, th);
2014dbc42409SLawrence Stewart 				cc_conn_init(tp);
20159077f387SGleb Smirnoff 				tcp_timer_activate(tp, TT_KEEP,
20169077f387SGleb Smirnoff 				    TP_KEEPIDLE(tp));
20177ff19458SPaul Traina 			}
2018a0292f23SGarrett Wollman 		} else {
2019a0292f23SGarrett Wollman 			/*
2020c068736aSJeffrey Hsu 			 * Received initial SYN in SYN-SENT[*] state =>
2021153edc50SHiren Panchasara 			 * simultaneous open.
2022c068736aSJeffrey Hsu 			 * If it succeeds, connection is * half-synchronized.
2023c068736aSJeffrey Hsu 			 * Otherwise, do 3-way handshake:
2024a0292f23SGarrett Wollman 			 *        SYN-SENT -> SYN-RECEIVED
2025a0292f23SGarrett Wollman 			 *        SYN-SENT* -> SYN-RECEIVED*
2026a0292f23SGarrett Wollman 			 */
20274b8e98d6SQing Li 			tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
2028b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
202957f60867SMark Johnston 			tcp_state_change(tp, TCPS_SYN_RECEIVED);
2030a0292f23SGarrett Wollman 		}
2031df8bae1dSRodney W. Grimes 
2032252ca428SRobert Watson 		KASSERT(ti_locked == TI_WLOCKED, ("%s: trimthenstep6: "
2033252ca428SRobert Watson 		    "ti_locked %d", __func__, ti_locked));
2034252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
20358501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
20367cfc6904SRobert Watson 
2037df8bae1dSRodney W. Grimes 		/*
2038fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
2039df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
2040df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
2041df8bae1dSRodney W. Grimes 		 */
2042fb59c426SYoshinobu Inoue 		th->th_seq++;
2043fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
2044fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
2045df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
2046fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
2047fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
204878b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpackafterwin);
204978b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2050df8bae1dSRodney W. Grimes 		}
2051fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
2052fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
2053a0292f23SGarrett Wollman 		/*
2054a0292f23SGarrett Wollman 		 * Client side of transaction: already sent SYN and data.
2055a0292f23SGarrett Wollman 		 * If the remote host used T/TCP to validate the SYN,
2056a0292f23SGarrett Wollman 		 * our data will be ACK'd; if so, enter normal data segment
2057a0292f23SGarrett Wollman 		 * processing in the middle of step 5, ack processing.
2058a0292f23SGarrett Wollman 		 * Otherwise, goto step 6.
2059a0292f23SGarrett Wollman 		 */
2060fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK)
2061a0292f23SGarrett Wollman 			goto process_ACK;
2062c068736aSJeffrey Hsu 
2063df8bae1dSRodney W. Grimes 		goto step6;
2064c068736aSJeffrey Hsu 
2065a0292f23SGarrett Wollman 	/*
2066a0292f23SGarrett Wollman 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
2067c94c54e4SAndre Oppermann 	 *      do normal processing.
2068a0292f23SGarrett Wollman 	 *
2069c94c54e4SAndre Oppermann 	 * NB: Leftover from RFC1644 T/TCP.  Cases to be reused later.
2070a0292f23SGarrett Wollman 	 */
2071a0292f23SGarrett Wollman 	case TCPS_LAST_ACK:
2072a0292f23SGarrett Wollman 	case TCPS_CLOSING:
2073a0292f23SGarrett Wollman 		break;  /* continue normal processing */
2074df8bae1dSRodney W. Grimes 	}
2075df8bae1dSRodney W. Grimes 
2076df8bae1dSRodney W. Grimes 	/*
2077df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
207880ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
207980ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
208080ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
208180ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
208280ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
208380ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
208480ab7c0eSGarrett Wollman 	 * killing a connection).
208580ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
2086a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
2087df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
2088df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
2089df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
209080ab7c0eSGarrett Wollman 	 */
2091fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
20923220a212SGleb Smirnoff 		/*
20933220a212SGleb Smirnoff 		 * RFC5961 Section 3.2
20943220a212SGleb Smirnoff 		 *
20953220a212SGleb Smirnoff 		 * - RST drops connection only if SEG.SEQ == RCV.NXT.
20963220a212SGleb Smirnoff 		 * - If RST is in window, we send challenge ACK.
20973220a212SGleb Smirnoff 		 *
20983220a212SGleb Smirnoff 		 * Note: to take into account delayed ACKs, we should
20993220a212SGleb Smirnoff 		 *   test against last_ack_sent instead of rcv_nxt.
21003220a212SGleb Smirnoff 		 * Note 2: we handle special case of closed window, not
21013220a212SGleb Smirnoff 		 *   covered by the RFC.
21023220a212SGleb Smirnoff 		 */
21033220a212SGleb Smirnoff 		if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
21043220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
21053220a212SGleb Smirnoff 		    (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) {
210680ab7c0eSGarrett Wollman 
21073220a212SGleb Smirnoff 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
21083220a212SGleb Smirnoff 			KASSERT(ti_locked == TI_WLOCKED,
21093220a212SGleb Smirnoff 			    ("%s: TH_RST ti_locked %d, th %p tp %p",
21103220a212SGleb Smirnoff 			    __func__, ti_locked, th, tp));
21113220a212SGleb Smirnoff 			KASSERT(tp->t_state != TCPS_SYN_SENT,
21123220a212SGleb Smirnoff 			    ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p",
21133220a212SGleb Smirnoff 			    __func__, th, tp));
21143220a212SGleb Smirnoff 
21153220a212SGleb Smirnoff 			if (V_tcp_insecure_rst ||
21163220a212SGleb Smirnoff 			    tp->last_ack_sent == th->th_seq) {
21173220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_drops);
21183220a212SGleb Smirnoff 				/* Drop the connection. */
21193220a212SGleb Smirnoff 				switch (tp->t_state) {
212080ab7c0eSGarrett Wollman 				case TCPS_SYN_RECEIVED:
212180ab7c0eSGarrett Wollman 					so->so_error = ECONNREFUSED;
212280ab7c0eSGarrett Wollman 					goto close;
212380ab7c0eSGarrett Wollman 				case TCPS_ESTABLISHED:
212480ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_1:
212580ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_2:
212680ab7c0eSGarrett Wollman 				case TCPS_CLOSE_WAIT:
212780ab7c0eSGarrett Wollman 					so->so_error = ECONNRESET;
212880ab7c0eSGarrett Wollman 				close:
212957f60867SMark Johnston 					tcp_state_change(tp, TCPS_CLOSED);
21303220a212SGleb Smirnoff 					/* FALLTHROUGH */
21313220a212SGleb Smirnoff 				default:
213280ab7c0eSGarrett Wollman 					tp = tcp_close(tp);
21333220a212SGleb Smirnoff 				}
21343220a212SGleb Smirnoff 			} else {
21353220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_badrst);
21363220a212SGleb Smirnoff 				/* Send challenge ACK. */
21373220a212SGleb Smirnoff 				tcp_respond(tp, mtod(m, void *), th, m,
21383220a212SGleb Smirnoff 				    tp->rcv_nxt, tp->snd_nxt, TH_ACK);
21393220a212SGleb Smirnoff 				tp->last_ack_sent = tp->rcv_nxt;
21403220a212SGleb Smirnoff 				m = NULL;
21413220a212SGleb Smirnoff 			}
21423220a212SGleb Smirnoff 		}
21433220a212SGleb Smirnoff 		goto drop;
21443220a212SGleb Smirnoff 	}
214580ab7c0eSGarrett Wollman 
21463220a212SGleb Smirnoff 	/*
21473220a212SGleb Smirnoff 	 * RFC5961 Section 4.2
21483220a212SGleb Smirnoff 	 * Send challenge ACK for any SYN in synchronized state.
21493220a212SGleb Smirnoff 	 */
21503220a212SGleb Smirnoff 	if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT) {
2151252ca428SRobert Watson 		KASSERT(ti_locked == TI_WLOCKED,
21523220a212SGleb Smirnoff 		    ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked));
2153252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2154252ca428SRobert Watson 
21553220a212SGleb Smirnoff 		TCPSTAT_INC(tcps_badsyn);
21563220a212SGleb Smirnoff 		if (V_tcp_insecure_syn &&
21573220a212SGleb Smirnoff 		    SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
21583220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
21593220a212SGleb Smirnoff 			tp = tcp_drop(tp, ECONNRESET);
21603220a212SGleb Smirnoff 			rstreason = BANDLIM_UNLIMITED;
21613220a212SGleb Smirnoff 		} else {
21623220a212SGleb Smirnoff 			/* Send challenge ACK. */
21633220a212SGleb Smirnoff 			tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt,
21643220a212SGleb Smirnoff 			    tp->snd_nxt, TH_ACK);
21653220a212SGleb Smirnoff 			tp->last_ack_sent = tp->rcv_nxt;
21663220a212SGleb Smirnoff 			m = NULL;
216780ab7c0eSGarrett Wollman 		}
216880ab7c0eSGarrett Wollman 		goto drop;
216980ab7c0eSGarrett Wollman 	}
217080ab7c0eSGarrett Wollman 
217180ab7c0eSGarrett Wollman 	/*
2172df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
2173df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
2174df8bae1dSRodney W. Grimes 	 */
2175be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
217680ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
2177df8bae1dSRodney W. Grimes 
2178df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
2179d8951c8aSBjoern A. Zeeb 		if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
2180df8bae1dSRodney W. Grimes 			/*
2181df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
2182df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
2183df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
2184df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
2185df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
2186df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
2187df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
2188df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
2189df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
2190df8bae1dSRodney W. Grimes 			 */
2191df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
2192df8bae1dSRodney W. Grimes 		} else {
219378b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
219478b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, tlen);
219578b50714SRobert Watson 			TCPSTAT_INC(tcps_pawsdrop);
219607fd333dSMatthew Dillon 			if (tlen)
2197df8bae1dSRodney W. Grimes 				goto dropafterack;
21981ab4789dSMatthew Dillon 			goto drop;
21991ab4789dSMatthew Dillon 		}
2200df8bae1dSRodney W. Grimes 	}
2201df8bae1dSRodney W. Grimes 
2202a0292f23SGarrett Wollman 	/*
220380ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
220480ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
220580ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
220680ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
220780ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
220880ab7c0eSGarrett Wollman 	 */
2209a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
2210a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2211a57815efSBosko Milekic 		goto dropwithreset;
2212a57815efSBosko Milekic 	}
221380ab7c0eSGarrett Wollman 
2214fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
2215df8bae1dSRodney W. Grimes 	if (todrop > 0) {
2216831ad37eSXin LI 		if (thflags & TH_SYN) {
2217fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
2218fb59c426SYoshinobu Inoue 			th->th_seq++;
2219fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
2220fb59c426SYoshinobu Inoue 				th->th_urp--;
2221df8bae1dSRodney W. Grimes 			else
2222fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
2223df8bae1dSRodney W. Grimes 			todrop--;
2224df8bae1dSRodney W. Grimes 		}
2225df8bae1dSRodney W. Grimes 		/*
2226dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
2227df8bae1dSRodney W. Grimes 		 */
2228fb59c426SYoshinobu Inoue 		if (todrop > tlen
2229fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
2230dac20301SGarrett Wollman 			/*
2231dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
2232dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
2233dac20301SGarrett Wollman 			 * of sequence; drop it.
2234dac20301SGarrett Wollman 			 */
2235fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
2236dac20301SGarrett Wollman 
2237df8bae1dSRodney W. Grimes 			/*
2238dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
2239dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
2240df8bae1dSRodney W. Grimes 			 */
2241dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
2242fb59c426SYoshinobu Inoue 			todrop = tlen;
224378b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
224478b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, todrop);
2245df8bae1dSRodney W. Grimes 		} else {
224678b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpartduppack);
224778b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop);
2248df8bae1dSRodney W. Grimes 		}
2249fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
2250fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
2251fb59c426SYoshinobu Inoue 		tlen -= todrop;
2252fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
2253fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
2254df8bae1dSRodney W. Grimes 		else {
2255fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
2256fb59c426SYoshinobu Inoue 			th->th_urp = 0;
2257df8bae1dSRodney W. Grimes 		}
2258df8bae1dSRodney W. Grimes 	}
2259df8bae1dSRodney W. Grimes 
2260df8bae1dSRodney W. Grimes 	/*
2261df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
2262df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
2263df8bae1dSRodney W. Grimes 	 */
2264df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
2265fb59c426SYoshinobu Inoue 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
2266252ca428SRobert Watson 		KASSERT(ti_locked == TI_WLOCKED, ("%s: SS_NOFDEREF && "
2267252ca428SRobert Watson 		    "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked));
2268252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2269252ca428SRobert Watson 
227007dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
227107dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data "
227207dacf03SAndre Oppermann 			    "after socket was closed, "
227307dacf03SAndre Oppermann 			    "sending RST and removing tcpcb\n",
2274e31d8aa3SMike Silbersack 			    s, __func__, tcpstates[tp->t_state], tlen);
2275773673c1SAndre Oppermann 			free(s, M_TCPLOG);
2276773673c1SAndre Oppermann 		}
2277df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
227878b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvafterclose);
2279a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
2280df8bae1dSRodney W. Grimes 		goto dropwithreset;
2281df8bae1dSRodney W. Grimes 	}
2282df8bae1dSRodney W. Grimes 
2283df8bae1dSRodney W. Grimes 	/*
2284df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
2285df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
2286df8bae1dSRodney W. Grimes 	 */
2287fb59c426SYoshinobu Inoue 	todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
2288df8bae1dSRodney W. Grimes 	if (todrop > 0) {
228978b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvpackafterwin);
2290fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
229178b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen);
2292df8bae1dSRodney W. Grimes 			/*
2293df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
2294df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
2295df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
2296df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
2297df8bae1dSRodney W. Grimes 			 * and ack.
2298df8bae1dSRodney W. Grimes 			 */
2299fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
2300df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
230178b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvwinprobe);
2302df8bae1dSRodney W. Grimes 			} else
2303df8bae1dSRodney W. Grimes 				goto dropafterack;
2304df8bae1dSRodney W. Grimes 		} else
230578b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2306df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
2307fb59c426SYoshinobu Inoue 		tlen -= todrop;
2308fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
2309df8bae1dSRodney W. Grimes 	}
2310df8bae1dSRodney W. Grimes 
2311df8bae1dSRodney W. Grimes 	/*
2312df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
2313df8bae1dSRodney W. Grimes 	 * record its timestamp.
2314cf09195bSPaul Saab 	 * NOTE:
2315cf09195bSPaul Saab 	 * 1) That the test incorporates suggestions from the latest
2316a0292f23SGarrett Wollman 	 *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
2317cf09195bSPaul Saab 	 * 2) That updating only on newer timestamps interferes with
2318cf09195bSPaul Saab 	 *    our earlier PAWS tests, so this check should be solely
2319cf09195bSPaul Saab 	 *    predicated on the sequence space of this segment.
2320cf09195bSPaul Saab 	 * 3) That we modify the segment boundary check to be
2321cf09195bSPaul Saab 	 *        Last.ACK.Sent <= SEG.SEQ + SEG.Len
2322cf09195bSPaul Saab 	 *    instead of RFC1323's
2323cf09195bSPaul Saab 	 *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
2324cf09195bSPaul Saab 	 *    This modified check allows us to overcome RFC1323's
2325cf09195bSPaul Saab 	 *    limitations as described in Stevens TCP/IP Illustrated
2326cf09195bSPaul Saab 	 *    Vol. 2 p.869. In such cases, we can still calculate the
2327cf09195bSPaul Saab 	 *    RTT correctly when RCV.NXT == Last.ACK.Sent.
2328df8bae1dSRodney W. Grimes 	 */
2329be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
2330cf09195bSPaul Saab 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
2331cf09195bSPaul Saab 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
2332cf09195bSPaul Saab 		((thflags & (TH_SYN|TH_FIN)) != 0))) {
2333d8951c8aSBjoern A. Zeeb 		tp->ts_recent_age = tcp_ts_getticks();
2334a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
2335df8bae1dSRodney W. Grimes 	}
2336df8bae1dSRodney W. Grimes 
2337df8bae1dSRodney W. Grimes 	/*
2338a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
2339a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
2340a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
2341a0292f23SGarrett Wollman 	 */
2342fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
2343a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
2344a0292f23SGarrett Wollman 		    (tp->t_flags & TF_NEEDSYN))
2345a0292f23SGarrett Wollman 			goto step6;
23465e1aa279SDavid Malone 		else if (tp->t_flags & TF_ACKNOW)
23475e1aa279SDavid Malone 			goto dropafterack;
2348a0292f23SGarrett Wollman 		else
2349a0292f23SGarrett Wollman 			goto drop;
2350a0292f23SGarrett Wollman 	}
2351df8bae1dSRodney W. Grimes 
2352df8bae1dSRodney W. Grimes 	/*
2353df8bae1dSRodney W. Grimes 	 * Ack processing.
2354df8bae1dSRodney W. Grimes 	 */
2355df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2356df8bae1dSRodney W. Grimes 
2357df8bae1dSRodney W. Grimes 	/*
2358764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
2359764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
2360764d8cefSBill Fenner 	 * The ACK was checked above.
2361df8bae1dSRodney W. Grimes 	 */
2362df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2363a0292f23SGarrett Wollman 
236478b50714SRobert Watson 		TCPSTAT_INC(tcps_connects);
2365df8bae1dSRodney W. Grimes 		soisconnected(so);
2366df8bae1dSRodney W. Grimes 		/* Do window scaling? */
2367df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2368df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2369df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
2370464fcfbcSAndre Oppermann 			tp->snd_wnd = tiwin;
2371df8bae1dSRodney W. Grimes 		}
2372a0292f23SGarrett Wollman 		/*
2373a0292f23SGarrett Wollman 		 * Make transitions:
2374a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
2375a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
2376a0292f23SGarrett Wollman 		 */
23779b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
2378a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDFIN) {
237957f60867SMark Johnston 			tcp_state_change(tp, TCPS_FIN_WAIT_1);
2380a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDFIN;
23817ff19458SPaul Traina 		} else {
238257f60867SMark Johnston 			tcp_state_change(tp, TCPS_ESTABLISHED);
2383d9fae5abSAndriy Gapon 			TCP_PROBE5(accept__established, NULL, tp,
2384fa22ce15SAdrian Chadd 			    mtod(m, const char *), tp, th);
2385dbc42409SLawrence Stewart 			cc_conn_init(tp);
23869077f387SGleb Smirnoff 			tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
23877ff19458SPaul Traina 		}
2388a0292f23SGarrett Wollman 		/*
2389a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
2390a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
2391a0292f23SGarrett Wollman 		 */
2392fb59c426SYoshinobu Inoue 		if (tlen == 0 && (thflags & TH_FIN) == 0)
2393fb59c426SYoshinobu Inoue 			(void) tcp_reass(tp, (struct tcphdr *)0, 0,
2394a0292f23SGarrett Wollman 			    (struct mbuf *)0);
239560ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq - 1;
239693b0017fSPhilippe Charnier 		/* FALLTHROUGH */
2397df8bae1dSRodney W. Grimes 
2398df8bae1dSRodney W. Grimes 	/*
2399df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2400df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
2401fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
2402fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
2403df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
2404df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
2405df8bae1dSRodney W. Grimes 	 */
2406df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
2407df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
2408df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
2409df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
2410df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
2411df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
24125a53ca16SPaul Saab 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
241378b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvacktoomuch);
24145a53ca16SPaul Saab 			goto dropafterack;
24155a53ca16SPaul Saab 		}
24163529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
2417fc30a251SAndre Oppermann 		    ((to.to_flags & TOF_SACK) ||
2418fc30a251SAndre Oppermann 		     !TAILQ_EMPTY(&tp->snd_holes)))
24195a53ca16SPaul Saab 			tcp_sack_doack(tp, &to, th->th_ack);
242039bc9de5SLawrence Stewart 
242139bc9de5SLawrence Stewart 		/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
242239bc9de5SLawrence Stewart 		hhook_run_tcp_est_in(tp, th, &to);
242339bc9de5SLawrence Stewart 
2424fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
242562b90589SPeter Wemm 			if (tlen == 0 && tiwin == tp->snd_wnd) {
242662b90589SPeter Wemm 				/*
242762b90589SPeter Wemm 				 * If this is the first time we've seen a
242862b90589SPeter Wemm 				 * FIN from the remote, this is not a
242962b90589SPeter Wemm 				 * duplicate and it needs to be processed
243062b90589SPeter Wemm 				 * normally.  This happens during a
243162b90589SPeter Wemm 				 * simultaneous close.
243262b90589SPeter Wemm 				 */
243362b90589SPeter Wemm 				if ((thflags & TH_FIN) &&
243462b90589SPeter Wemm 				    (TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
243562b90589SPeter Wemm 					tp->t_dupacks = 0;
243662b90589SPeter Wemm 					break;
243762b90589SPeter Wemm 				}
243878b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvdupack);
2439df8bae1dSRodney W. Grimes 				/*
2440df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
2441df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
2442df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
24435f30ec9bSEitan Adler 				 * change and FIN isn't set),
24445f30ec9bSEitan Adler 				 * the ack is the biggest we've
2445df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
2446df8bae1dSRodney W. Grimes 				 * threshhold of them, assume a packet
2447df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
2448df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
2449df8bae1dSRodney W. Grimes 				 * window so we send only this one
2450df8bae1dSRodney W. Grimes 				 * packet.
2451df8bae1dSRodney W. Grimes 				 *
2452df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
2453df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
2454df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
2455df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
2456df8bae1dSRodney W. Grimes 				 * the new ssthresh).
2457df8bae1dSRodney W. Grimes 				 *
2458df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
2459df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
2460df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
2461df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
2462df8bae1dSRodney W. Grimes 				 * network.
2463f2512ba1SRui Paulo 				 *
2464f2512ba1SRui Paulo 				 * When using TCP ECN, notify the peer that
2465f2512ba1SRui Paulo 				 * we reduced the cwnd.
2466df8bae1dSRodney W. Grimes 				 */
2467b8152ba7SAndre Oppermann 				if (!tcp_timer_active(tp, TT_REXMT) ||
2468fb59c426SYoshinobu Inoue 				    th->th_ack != tp->snd_una)
2469df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
2470cb942153SJeffrey Hsu 				else if (++tp->t_dupacks > tcprexmtthresh ||
2471dbc42409SLawrence Stewart 				     IN_FASTRECOVERY(tp->t_flags)) {
2472dbc42409SLawrence Stewart 					cc_ack_received(tp, th, CC_DUPACK);
24733529149eSAndre Oppermann 					if ((tp->t_flags & TF_SACK_PERMIT) &&
2474dbc42409SLawrence Stewart 					    IN_FASTRECOVERY(tp->t_flags)) {
2475808f11b7SPaul Saab 						int awnd;
247625e6f9edSPaul Saab 
247725e6f9edSPaul Saab 						/*
247825e6f9edSPaul Saab 						 * Compute the amount of data in flight first.
247925e6f9edSPaul Saab 						 * We can inject new data into the pipe iff
248025e6f9edSPaul Saab 						 * we have less than 1/2 the original window's
248125e6f9edSPaul Saab 						 * worth of data in flight.
248225e6f9edSPaul Saab 						 */
2483808f11b7SPaul Saab 						awnd = (tp->snd_nxt - tp->snd_fack) +
24840077b016SPaul Saab 							tp->sackhint.sack_bytes_rexmit;
2485808f11b7SPaul Saab 						if (awnd < tp->snd_ssthresh) {
248625e6f9edSPaul Saab 							tp->snd_cwnd += tp->t_maxseg;
248725e6f9edSPaul Saab 							if (tp->snd_cwnd > tp->snd_ssthresh)
248825e6f9edSPaul Saab 								tp->snd_cwnd = tp->snd_ssthresh;
248925e6f9edSPaul Saab 						}
249025e6f9edSPaul Saab 					} else
249146f58482SJonathan Lemon 						tp->snd_cwnd += tp->t_maxseg;
249246f58482SJonathan Lemon 					(void) tcp_output(tp);
249346f58482SJonathan Lemon 					goto drop;
2494cb942153SJeffrey Hsu 				} else if (tp->t_dupacks == tcprexmtthresh) {
2495cb942153SJeffrey Hsu 					tcp_seq onxt = tp->snd_nxt;
2496a0445c2eSJayanth Vijayaraghavan 
2497a0445c2eSJayanth Vijayaraghavan 					/*
2498a0445c2eSJayanth Vijayaraghavan 					 * If we're doing sack, check to
2499a0445c2eSJayanth Vijayaraghavan 					 * see if we're already in sack
2500a0445c2eSJayanth Vijayaraghavan 					 * recovery. If we're not doing sack,
2501a0445c2eSJayanth Vijayaraghavan 					 * check to see if we're in newreno
2502a0445c2eSJayanth Vijayaraghavan 					 * recovery.
2503a0445c2eSJayanth Vijayaraghavan 					 */
25043529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
2505dbc42409SLawrence Stewart 						if (IN_FASTRECOVERY(tp->t_flags)) {
2506a0445c2eSJayanth Vijayaraghavan 							tp->t_dupacks = 0;
2507a0445c2eSJayanth Vijayaraghavan 							break;
2508a0445c2eSJayanth Vijayaraghavan 						}
2509dbc42409SLawrence Stewart 					} else {
2510a0445c2eSJayanth Vijayaraghavan 						if (SEQ_LEQ(th->th_ack,
25119d11646dSJeffrey Hsu 						    tp->snd_recover)) {
2512cb942153SJeffrey Hsu 							tp->t_dupacks = 0;
2513cb942153SJeffrey Hsu 							break;
251446f58482SJonathan Lemon 						}
2515a0445c2eSJayanth Vijayaraghavan 					}
2516dbc42409SLawrence Stewart 					/* Congestion signal before ack. */
2517dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_NDUPACK);
2518dbc42409SLawrence Stewart 					cc_ack_received(tp, th, CC_DUPACK);
2519b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
25209b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
25213529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
252278b50714SRobert Watson 						TCPSTAT_INC(
252378b50714SRobert Watson 						    tcps_sack_recovery_episode);
2524a55db2b6SPaul Saab 						tp->sack_newdata = tp->snd_nxt;
25258db456bfSPaul Saab 						tp->snd_cwnd = tp->t_maxseg;
25266d90faf3SPaul Saab 						(void) tcp_output(tp);
25276d90faf3SPaul Saab 						goto drop;
25286d90faf3SPaul Saab 					}
2529fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
2530df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->t_maxseg;
2531df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
253248d2549cSJeffrey Hsu 					KASSERT(tp->snd_limited <= 2,
2533302ce8d6SAndre Oppermann 					    ("%s: tp->snd_limited too big",
2534302ce8d6SAndre Oppermann 					    __func__));
2535df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
253648d2549cSJeffrey Hsu 					     tp->t_maxseg *
253748d2549cSJeffrey Hsu 					     (tp->t_dupacks - tp->snd_limited);
2538df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
2539df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
2540df8bae1dSRodney W. Grimes 					goto drop;
2541603724d3SBjoern A. Zeeb 				} else if (V_tcp_do_rfc3042) {
2542dbc42409SLawrence Stewart 					cc_ack_received(tp, th, CC_DUPACK);
2543582a954bSJeffrey Hsu 					u_long oldcwnd = tp->snd_cwnd;
254448d2549cSJeffrey Hsu 					tcp_seq oldsndmax = tp->snd_max;
254548d2549cSJeffrey Hsu 					u_int sent;
25465628dd08SAndre Oppermann 					int avail;
254789c02376SJeffrey Hsu 
2548582a954bSJeffrey Hsu 					KASSERT(tp->t_dupacks == 1 ||
2549582a954bSJeffrey Hsu 					    tp->t_dupacks == 2,
2550302ce8d6SAndre Oppermann 					    ("%s: dupacks not 1 or 2",
2551302ce8d6SAndre Oppermann 					    __func__));
255261a36e3dSJeffrey Hsu 					if (tp->t_dupacks == 1)
255348d2549cSJeffrey Hsu 						tp->snd_limited = 0;
255461a36e3dSJeffrey Hsu 					tp->snd_cwnd =
255561a36e3dSJeffrey Hsu 					    (tp->snd_nxt - tp->snd_una) +
255661a36e3dSJeffrey Hsu 					    (tp->t_dupacks - tp->snd_limited) *
255761a36e3dSJeffrey Hsu 					    tp->t_maxseg;
25585628dd08SAndre Oppermann 					/*
25595628dd08SAndre Oppermann 					 * Only call tcp_output when there
25605628dd08SAndre Oppermann 					 * is new data available to be sent.
25615628dd08SAndre Oppermann 					 * Otherwise we would send pure ACKs.
25625628dd08SAndre Oppermann 					 */
25635628dd08SAndre Oppermann 					SOCKBUF_LOCK(&so->so_snd);
2564cfa6009eSGleb Smirnoff 					avail = sbavail(&so->so_snd) -
25655628dd08SAndre Oppermann 					    (tp->snd_nxt - tp->snd_una);
25665628dd08SAndre Oppermann 					SOCKBUF_UNLOCK(&so->so_snd);
25675628dd08SAndre Oppermann 					if (avail > 0)
2568582a954bSJeffrey Hsu 						(void) tcp_output(tp);
256948d2549cSJeffrey Hsu 					sent = tp->snd_max - oldsndmax;
257048d2549cSJeffrey Hsu 					if (sent > tp->t_maxseg) {
257189c02376SJeffrey Hsu 						KASSERT((tp->t_dupacks == 2 &&
257289c02376SJeffrey Hsu 						    tp->snd_limited == 0) ||
257389c02376SJeffrey Hsu 						   (sent == tp->t_maxseg + 1 &&
257489c02376SJeffrey Hsu 						    tp->t_flags & TF_SENTFIN),
2575302ce8d6SAndre Oppermann 						    ("%s: sent too much",
2576302ce8d6SAndre Oppermann 						    __func__));
257748d2549cSJeffrey Hsu 						tp->snd_limited = 2;
257848d2549cSJeffrey Hsu 					} else if (sent > 0)
257948d2549cSJeffrey Hsu 						++tp->snd_limited;
2580582a954bSJeffrey Hsu 					tp->snd_cwnd = oldcwnd;
2581582a954bSJeffrey Hsu 					goto drop;
2582df8bae1dSRodney W. Grimes 				}
2583df8bae1dSRodney W. Grimes 			} else
2584df8bae1dSRodney W. Grimes 				tp->t_dupacks = 0;
2585df8bae1dSRodney W. Grimes 			break;
2586df8bae1dSRodney W. Grimes 		}
2587cb942153SJeffrey Hsu 
2588302ce8d6SAndre Oppermann 		KASSERT(SEQ_GT(th->th_ack, tp->snd_una),
2589302ce8d6SAndre Oppermann 		    ("%s: th_ack <= snd_una", __func__));
2590cb942153SJeffrey Hsu 
2591df8bae1dSRodney W. Grimes 		/*
2592df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
2593df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
2594df8bae1dSRodney W. Grimes 		 */
2595dbc42409SLawrence Stewart 		if (IN_FASTRECOVERY(tp->t_flags)) {
2596cb942153SJeffrey Hsu 			if (SEQ_LT(th->th_ack, tp->snd_recover)) {
25973529149eSAndre Oppermann 				if (tp->t_flags & TF_SACK_PERMIT)
25986d90faf3SPaul Saab 					tcp_sack_partialack(tp, th);
25996d90faf3SPaul Saab 				else
2600c068736aSJeffrey Hsu 					tcp_newreno_partial_ack(tp, th);
2601dbc42409SLawrence Stewart 			} else
2602dbc42409SLawrence Stewart 				cc_post_recovery(tp, th);
260346f58482SJonathan Lemon 		}
2604cb942153SJeffrey Hsu 		tp->t_dupacks = 0;
2605a0292f23SGarrett Wollman 		/*
2606a0292f23SGarrett Wollman 		 * If we reach this point, ACK is not a duplicate,
2607a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
2608a0292f23SGarrett Wollman 		 */
2609a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
2610a0292f23SGarrett Wollman 			/*
2611a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
2612a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
261307e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
261407e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
261507e43e10SAndras Olah 			 * we can do window scaling.
2616a0292f23SGarrett Wollman 			 */
2617a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
2618a0292f23SGarrett Wollman 			tp->snd_una++;
261907e43e10SAndras Olah 			/* Do window scaling? */
262007e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
262107e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
262207e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2623464fcfbcSAndre Oppermann 				/* Send window already scaled. */
262407e43e10SAndras Olah 			}
2625a0292f23SGarrett Wollman 		}
2626a0292f23SGarrett Wollman 
2627a0292f23SGarrett Wollman process_ACK:
26288501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
26297cfc6904SRobert Watson 
2630dbc42409SLawrence Stewart 		acked = BYTES_THIS_ACK(tp, th);
263178b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvackpack);
263278b50714SRobert Watson 		TCPSTAT_ADD(tcps_rcvackbyte, acked);
2633df8bae1dSRodney W. Grimes 
2634df8bae1dSRodney W. Grimes 		/*
26359b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
26369b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
26379b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
26389b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
26399b8b58e0SJonathan Lemon 		 * we left off.
26409b8b58e0SJonathan Lemon 		 */
2641672dc4aeSJohn Baldwin 		if (tp->t_rxtshift == 1 && tp->t_flags & TF_PREVVALID &&
2642672dc4aeSJohn Baldwin 		    (int)(ticks - tp->t_badrxtwin) < 0)
2643dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_RTO_ERR);
26449b8b58e0SJonathan Lemon 
26459b8b58e0SJonathan Lemon 		/*
2646df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
2647df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
2648df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
2649df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
2650df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
2651df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
2652df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
2653fa55172bSMatthew Dillon 		 *
2654fa55172bSMatthew Dillon 		 * Some boxes send broken timestamp replies
2655fa55172bSMatthew Dillon 		 * during the SYN+ACK phase, ignore
2656fa55172bSMatthew Dillon 		 * timestamps of 0 or we could calculate a
2657fa55172bSMatthew Dillon 		 * huge RTT and blow up the retransmit timer.
2658df8bae1dSRodney W. Grimes 		 */
2659d8951c8aSBjoern A. Zeeb 		if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) {
2660d8951c8aSBjoern A. Zeeb 			u_int t;
2661d8951c8aSBjoern A. Zeeb 
2662d8951c8aSBjoern A. Zeeb 			t = tcp_ts_getticks() - to.to_tsecr;
2663d8951c8aSBjoern A. Zeeb 			if (!tp->t_rttlow || tp->t_rttlow > t)
2664d8951c8aSBjoern A. Zeeb 				tp->t_rttlow = t;
2665d8951c8aSBjoern A. Zeeb 			tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1);
2666fa55172bSMatthew Dillon 		} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2667eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
2668eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - tp->t_rtttime;
26699b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2670fa55172bSMatthew Dillon 		}
2671df8bae1dSRodney W. Grimes 
2672df8bae1dSRodney W. Grimes 		/*
2673df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
2674df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
2675df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
2676df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
2677df8bae1dSRodney W. Grimes 		 */
2678fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
2679b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
2680df8bae1dSRodney W. Grimes 			needoutput = 1;
2681b8152ba7SAndre Oppermann 		} else if (!tcp_timer_active(tp, TT_PERSIST))
2682b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
2683a0292f23SGarrett Wollman 
2684a0292f23SGarrett Wollman 		/*
2685a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
2686a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
2687a0292f23SGarrett Wollman 		 */
2688a0292f23SGarrett Wollman 		if (acked == 0)
2689a0292f23SGarrett Wollman 			goto step6;
2690a0292f23SGarrett Wollman 
2691df8bae1dSRodney W. Grimes 		/*
2692dbc42409SLawrence Stewart 		 * Let the congestion control algorithm update congestion
2693dbc42409SLawrence Stewart 		 * control related information. This typically means increasing
2694dbc42409SLawrence Stewart 		 * the congestion window.
2695df8bae1dSRodney W. Grimes 		 */
2696dbc42409SLawrence Stewart 		cc_ack_received(tp, th, CC_ACK);
2697dbc42409SLawrence Stewart 
26985905999bSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
2699cfa6009eSGleb Smirnoff 		if (acked > sbavail(&so->so_snd)) {
2700cfa6009eSGleb Smirnoff 			tp->snd_wnd -= sbavail(&so->so_snd);
2701c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd,
2702cfa6009eSGleb Smirnoff 			    (int)sbavail(&so->so_snd));
2703df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
2704df8bae1dSRodney W. Grimes 		} else {
2705c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd, acked);
270660ee3bb2SAndre Oppermann 			tp->snd_wnd -= acked;
2707df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
2708df8bae1dSRodney W. Grimes 		}
2709564aab1fSAndre Oppermann 		/* NB: sowwakeup_locked() does an implicit unlock. */
27101e4d7da7SRobert Watson 		sowwakeup_locked(so);
2711c11a15bfSGleb Smirnoff 		m_freem(mfree);
2712e8949f74SAndre Oppermann 		/* Detect una wraparound. */
2713dbc42409SLawrence Stewart 		if (!IN_RECOVERY(tp->t_flags) &&
27149d11646dSJeffrey Hsu 		    SEQ_GT(tp->snd_una, tp->snd_recover) &&
27159d11646dSJeffrey Hsu 		    SEQ_LEQ(th->th_ack, tp->snd_recover))
27169d11646dSJeffrey Hsu 			tp->snd_recover = th->th_ack - 1;
2717dbc42409SLawrence Stewart 		/* XXXLAS: Can this be moved up into cc_post_recovery? */
2718dbc42409SLawrence Stewart 		if (IN_RECOVERY(tp->t_flags) &&
271924cb0f22SLawrence Stewart 		    SEQ_GEQ(th->th_ack, tp->snd_recover)) {
2720dbc42409SLawrence Stewart 			EXIT_RECOVERY(tp->t_flags);
272124cb0f22SLawrence Stewart 		}
2722fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
27233529149eSAndre Oppermann 		if (tp->t_flags & TF_SACK_PERMIT) {
27246d90faf3SPaul Saab 			if (SEQ_GT(tp->snd_una, tp->snd_recover))
27256d90faf3SPaul Saab 				tp->snd_recover = tp->snd_una;
27266d90faf3SPaul Saab 		}
2727df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2728df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
2729df8bae1dSRodney W. Grimes 
2730df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2731df8bae1dSRodney W. Grimes 
2732df8bae1dSRodney W. Grimes 		/*
2733df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
2734df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
2735df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
2736df8bae1dSRodney W. Grimes 		 */
2737df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2738df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2739df8bae1dSRodney W. Grimes 				/*
2740df8bae1dSRodney W. Grimes 				 * If we can't receive any more
2741df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
2742df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
2743df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
2744df8bae1dSRodney W. Grimes 				 * we'll hang forever.
2745e8949f74SAndre Oppermann 				 *
2746e8949f74SAndre Oppermann 				 * XXXjl:
2747340c35deSJonathan Lemon 				 * we should release the tp also, and use a
2748340c35deSJonathan Lemon 				 * compressed state.
2749340c35deSJonathan Lemon 				 */
2750c0b99ffaSRobert Watson 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
275103e49181SSeigo Tanimura 					soisdisconnected(so);
27529077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
27539077f387SGleb Smirnoff 					    (tcp_fast_finwait2_recycle ?
27549077f387SGleb Smirnoff 					    tcp_finwait2_timeout :
27559077f387SGleb Smirnoff 					    TP_MAXIDLE(tp)));
27564cc20ab1SSeigo Tanimura 				}
275757f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
2758df8bae1dSRodney W. Grimes 			}
2759df8bae1dSRodney W. Grimes 			break;
2760df8bae1dSRodney W. Grimes 
2761df8bae1dSRodney W. Grimes 		/*
2762df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
2763df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
2764df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
2765df8bae1dSRodney W. Grimes 		 * the segment.
2766df8bae1dSRodney W. Grimes 		 */
2767df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
2768df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2769252ca428SRobert Watson 				INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
277011a20fb8SJeffrey Hsu 				tcp_twstart(tp);
2771603724d3SBjoern A. Zeeb 				INP_INFO_WUNLOCK(&V_tcbinfo);
2772340c35deSJonathan Lemon 				m_freem(m);
2773679d9708SAndre Oppermann 				return;
2774df8bae1dSRodney W. Grimes 			}
2775df8bae1dSRodney W. Grimes 			break;
2776df8bae1dSRodney W. Grimes 
2777df8bae1dSRodney W. Grimes 		/*
2778df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
2779df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
2780df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
2781df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
2782df8bae1dSRodney W. Grimes 		 */
2783df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
2784df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2785252ca428SRobert Watson 				INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2786df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
2787df8bae1dSRodney W. Grimes 				goto drop;
2788df8bae1dSRodney W. Grimes 			}
2789df8bae1dSRodney W. Grimes 			break;
2790df8bae1dSRodney W. Grimes 		}
2791df8bae1dSRodney W. Grimes 	}
2792df8bae1dSRodney W. Grimes 
2793df8bae1dSRodney W. Grimes step6:
27948501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
27957cfc6904SRobert Watson 
2796df8bae1dSRodney W. Grimes 	/*
279760ee3bb2SAndre Oppermann 	 * Update window information.
279860ee3bb2SAndre Oppermann 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2799df8bae1dSRodney W. Grimes 	 */
280060ee3bb2SAndre Oppermann 	if ((thflags & TH_ACK) &&
280160ee3bb2SAndre Oppermann 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
280260ee3bb2SAndre Oppermann 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
280360ee3bb2SAndre Oppermann 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
280460ee3bb2SAndre Oppermann 		/* keep track of pure window updates */
280560ee3bb2SAndre Oppermann 		if (tlen == 0 &&
280660ee3bb2SAndre Oppermann 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
280778b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvwinupd);
2808df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
280960ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq;
281060ee3bb2SAndre Oppermann 		tp->snd_wl2 = th->th_ack;
2811df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
2812df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
281360ee3bb2SAndre Oppermann 		needoutput = 1;
2814df8bae1dSRodney W. Grimes 	}
2815df8bae1dSRodney W. Grimes 
2816df8bae1dSRodney W. Grimes 	/*
2817df8bae1dSRodney W. Grimes 	 * Process segments with URG.
2818df8bae1dSRodney W. Grimes 	 */
2819fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
2820df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2821df8bae1dSRodney W. Grimes 		/*
2822df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
2823df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
2824df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
2825df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
2826df8bae1dSRodney W. Grimes 		 */
282718ad5842SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
2828cfa6009eSGleb Smirnoff 		if (th->th_urp + sbavail(&so->so_rcv) > sb_max) {
2829fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
2830fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
283118ad5842SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);	/* XXX */
2832df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
2833df8bae1dSRodney W. Grimes 		}
2834df8bae1dSRodney W. Grimes 		/*
2835df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
2836df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
2837df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2838df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
2839df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
2840df8bae1dSRodney W. Grimes 		 *
2841df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
2842df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
2843df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
2844df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
2845df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
2846df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
2847df8bae1dSRodney W. Grimes 		 */
2848fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2849fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
2850cfa6009eSGleb Smirnoff 			so->so_oobmark = sbavail(&so->so_rcv) +
2851df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
2852927c5ceaSRobert Watson 			if (so->so_oobmark == 0)
2853c0b99ffaSRobert Watson 				so->so_rcv.sb_state |= SBS_RCVATMARK;
2854df8bae1dSRodney W. Grimes 			sohasoutofband(so);
2855df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2856df8bae1dSRodney W. Grimes 		}
285718ad5842SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
2858df8bae1dSRodney W. Grimes 		/*
2859df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
2860df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
2861df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
2862df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
2863df8bae1dSRodney W. Grimes 		 */
286430613f56SJeffrey Hsu 		if (th->th_urp <= (u_long)tlen &&
286530613f56SJeffrey Hsu 		    !(so->so_options & SO_OOBINLINE)) {
286630613f56SJeffrey Hsu 			/* hdr drop is delayed */
286730613f56SJeffrey Hsu 			tcp_pulloutofband(so, th, m, drop_hdrlen);
286830613f56SJeffrey Hsu 		}
2869c068736aSJeffrey Hsu 	} else {
2870df8bae1dSRodney W. Grimes 		/*
2871df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
2872df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
2873df8bae1dSRodney W. Grimes 		 * with the receive window.
2874df8bae1dSRodney W. Grimes 		 */
2875df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2876df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
2877c068736aSJeffrey Hsu 	}
2878df8bae1dSRodney W. Grimes dodata:							/* XXX */
28798501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
28807cfc6904SRobert Watson 
2881df8bae1dSRodney W. Grimes 	/*
2882df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
2883df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
2884df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
2885df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
2886df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
2887df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
2888df8bae1dSRodney W. Grimes 	 */
2889fb59c426SYoshinobu Inoue 	if ((tlen || (thflags & TH_FIN)) &&
2890df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
289169e03620SPaul Saab 		tcp_seq save_start = th->th_seq;
2892fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
2893e4b64281SJesper Skriver 		/*
2894c068736aSJeffrey Hsu 		 * Insert segment which includes th into TCP reassembly queue
2895c068736aSJeffrey Hsu 		 * with control block tp.  Set thflags to whether reassembly now
2896c068736aSJeffrey Hsu 		 * includes a segment with FIN.  This handles the common case
2897c068736aSJeffrey Hsu 		 * inline (segment is the next to be received on an established
2898c068736aSJeffrey Hsu 		 * connection, and the queue is empty), avoiding linkage into
2899c068736aSJeffrey Hsu 		 * and removal from the queue and repetition of various
2900c068736aSJeffrey Hsu 		 * conversions.
2901c068736aSJeffrey Hsu 		 * Set DELACK for segments received in order, but ack
2902c068736aSJeffrey Hsu 		 * immediately when segments are out of order (so
2903c068736aSJeffrey Hsu 		 * fast retransmit can work).
2904e4b64281SJesper Skriver 		 */
2905e407b67bSGleb Smirnoff 		if (th->th_seq == tp->rcv_nxt && tp->t_segq == NULL &&
2906e4b64281SJesper Skriver 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
2907c1e5a6e5SAndre Oppermann 			if (DELAY_ACK(tp, tlen))
29083bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
2909e4b64281SJesper Skriver 			else
2910e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
2911e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
2912e4b64281SJesper Skriver 			thflags = th->th_flags & TH_FIN;
291378b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpack);
291478b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
2915e4b64281SJesper Skriver 			ND6_HINT(tp);
29161e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
2917c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
2918c1c36a2cSMike Silbersack 				m_freem(m);
2919c1c36a2cSMike Silbersack 			else
2920651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
2921e8949f74SAndre Oppermann 			/* NB: sorwakeup_locked() does an implicit unlock. */
29221e4d7da7SRobert Watson 			sorwakeup_locked(so);
2923e4b64281SJesper Skriver 		} else {
2924e8949f74SAndre Oppermann 			/*
2925e8949f74SAndre Oppermann 			 * XXX: Due to the header drop above "th" is
2926e8949f74SAndre Oppermann 			 * theoretically invalid by now.  Fortunately
2927e8949f74SAndre Oppermann 			 * m_adj() doesn't actually frees any mbufs
2928e8949f74SAndre Oppermann 			 * when trimming from the head.
2929e8949f74SAndre Oppermann 			 */
2930e4b64281SJesper Skriver 			thflags = tcp_reass(tp, th, &tlen, m);
2931e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
2932e4b64281SJesper Skriver 		}
29333529149eSAndre Oppermann 		if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT))
2934f194524fSAndre Oppermann 			tcp_update_sack_list(tp, save_start, save_start + tlen);
2935302ce8d6SAndre Oppermann #if 0
2936df8bae1dSRodney W. Grimes 		/*
2937df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
2938df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
2939df8bae1dSRodney W. Grimes 		 * buffer size.
2940302ce8d6SAndre Oppermann 		 * XXX: Unused.
2941df8bae1dSRodney W. Grimes 		 */
2942f701e30dSJohn Baldwin 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt))
2943df8bae1dSRodney W. Grimes 			len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2944f701e30dSJohn Baldwin 		else
2945f701e30dSJohn Baldwin 			len = so->so_rcv.sb_hiwat;
2946302ce8d6SAndre Oppermann #endif
2947df8bae1dSRodney W. Grimes 	} else {
2948df8bae1dSRodney W. Grimes 		m_freem(m);
2949fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
2950df8bae1dSRodney W. Grimes 	}
2951df8bae1dSRodney W. Grimes 
2952df8bae1dSRodney W. Grimes 	/*
2953df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
2954df8bae1dSRodney W. Grimes 	 * that the connection is closing.
2955df8bae1dSRodney W. Grimes 	 */
2956fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
2957df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2958df8bae1dSRodney W. Grimes 			socantrcvmore(so);
2959a0292f23SGarrett Wollman 			/*
2960a0292f23SGarrett Wollman 			 * If connection is half-synchronized
2961d3eede9dSAndras Olah 			 * (ie NEEDSYN flag on) then delay ACK,
2962a0292f23SGarrett Wollman 			 * so it may be piggybacked when SYN is sent.
2963a0292f23SGarrett Wollman 			 * Otherwise, since we received a FIN then no
2964a0292f23SGarrett Wollman 			 * more input can be expected, send ACK now.
2965a0292f23SGarrett Wollman 			 */
29663bfd6421SJonathan Lemon 			if (tp->t_flags & TF_NEEDSYN)
29673bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
2968a0292f23SGarrett Wollman 			else
2969df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
2970df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
2971df8bae1dSRodney W. Grimes 		}
2972df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2973df8bae1dSRodney W. Grimes 
2974df8bae1dSRodney W. Grimes 		/*
2975df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
2976df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
2977df8bae1dSRodney W. Grimes 		 */
2978df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
29799b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
29809b8b58e0SJonathan Lemon 			/* FALLTHROUGH */
2981df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
298257f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSE_WAIT);
2983df8bae1dSRodney W. Grimes 			break;
2984df8bae1dSRodney W. Grimes 
2985df8bae1dSRodney W. Grimes 		/*
2986df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2987df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
2988df8bae1dSRodney W. Grimes 		 */
2989df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
299057f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSING);
2991df8bae1dSRodney W. Grimes 			break;
2992df8bae1dSRodney W. Grimes 
2993df8bae1dSRodney W. Grimes 		/*
2994df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2995df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
2996df8bae1dSRodney W. Grimes 		 * standard timers.
2997df8bae1dSRodney W. Grimes 		 */
2998df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
2999252ca428SRobert Watson 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
3000252ca428SRobert Watson 			KASSERT(ti_locked == TI_WLOCKED, ("%s: dodata "
3001252ca428SRobert Watson 			    "TCP_FIN_WAIT_2 ti_locked: %d", __func__,
3002252ca428SRobert Watson 			    ti_locked));
3003252ca428SRobert Watson 
3004340c35deSJonathan Lemon 			tcp_twstart(tp);
3005603724d3SBjoern A. Zeeb 			INP_INFO_WUNLOCK(&V_tcbinfo);
3006679d9708SAndre Oppermann 			return;
3007df8bae1dSRodney W. Grimes 		}
3008df8bae1dSRodney W. Grimes 	}
3009fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED)
3010603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
3011252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
3012252ca428SRobert Watson 
3013610ee2f9SDavid Greenman #ifdef TCPDEBUG
30144cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
3015fb59c426SYoshinobu Inoue 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
3016fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3017610ee2f9SDavid Greenman #endif
3018df8bae1dSRodney W. Grimes 
3019df8bae1dSRodney W. Grimes 	/*
3020df8bae1dSRodney W. Grimes 	 * Return any desired output.
3021df8bae1dSRodney W. Grimes 	 */
3022df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
3023df8bae1dSRodney W. Grimes 		(void) tcp_output(tp);
30247792ea27SJeffrey Hsu 
3025a14c749fSJonathan Lemon check_delack:
3026252ca428SRobert Watson 	KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d",
3027252ca428SRobert Watson 	    __func__, ti_locked));
3028603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
30298501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
3030252ca428SRobert Watson 
3031a14c749fSJonathan Lemon 	if (tp->t_flags & TF_DELACK) {
30323bfd6421SJonathan Lemon 		tp->t_flags &= ~TF_DELACK;
3033b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
30343bfd6421SJonathan Lemon 	}
30358501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
3036679d9708SAndre Oppermann 	return;
3037df8bae1dSRodney W. Grimes 
3038df8bae1dSRodney W. Grimes dropafterack:
3039df8bae1dSRodney W. Grimes 	/*
3040df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
3041df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
304280ab7c0eSGarrett Wollman 	 *
304380ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
304480ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
304580ab7c0eSGarrett Wollman 	 * RST have been dropped.
304680ab7c0eSGarrett Wollman 	 *
304780ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
304880ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
304980ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
305080ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
305180ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
305280ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
3053df8bae1dSRodney W. Grimes 	 */
3054fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
3055fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
3056a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
3057a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
3058a57815efSBosko Milekic 		goto dropwithreset;
3059a57815efSBosko Milekic 	}
3060a0292f23SGarrett Wollman #ifdef TCPDEBUG
30614cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
3062fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3063fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3064a0292f23SGarrett Wollman #endif
3065fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED)
3066603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
3067252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
3068252ca428SRobert Watson 
3069df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
3070df8bae1dSRodney W. Grimes 	(void) tcp_output(tp);
30718501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
3072d6915262SRobert Watson 	m_freem(m);
3073679d9708SAndre Oppermann 	return;
3074df8bae1dSRodney W. Grimes 
3075df8bae1dSRodney W. Grimes dropwithreset:
3076fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED)
3077dd8ac7f9SRobert Watson 		INP_INFO_WUNLOCK(&V_tcbinfo);
3078252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
3079a57815efSBosko Milekic 
3080a0ca0871SRobert Watson 	if (tp != NULL) {
3081302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
30828501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
3083dd8ac7f9SRobert Watson 	} else
3084a0ca0871SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
3085679d9708SAndre Oppermann 	return;
3086df8bae1dSRodney W. Grimes 
3087df8bae1dSRodney W. Grimes drop:
3088fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED) {
3089252ca428SRobert Watson 		INP_INFO_WUNLOCK(&V_tcbinfo);
3090fa046d87SRobert Watson 		ti_locked = TI_UNLOCKED;
3091fa046d87SRobert Watson 	}
3092252ca428SRobert Watson #ifdef INVARIANTS
3093252ca428SRobert Watson 	else
3094252ca428SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
3095252ca428SRobert Watson #endif
3096252ca428SRobert Watson 
3097df8bae1dSRodney W. Grimes 	/*
3098df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
3099df8bae1dSRodney W. Grimes 	 */
3100610ee2f9SDavid Greenman #ifdef TCPDEBUG
31011c53f806SRobert Watson 	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
3102fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3103fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3104a0292f23SGarrett Wollman #endif
31051c53f806SRobert Watson 	if (tp != NULL)
31068501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
3107d6915262SRobert Watson 	m_freem(m);
3108302ce8d6SAndre Oppermann }
3109302ce8d6SAndre Oppermann 
3110302ce8d6SAndre Oppermann /*
31110ca3f933SAndre Oppermann  * Issue RST and make ACK acceptable to originator of segment.
31120ca3f933SAndre Oppermann  * The mbuf must still include the original packet header.
31130ca3f933SAndre Oppermann  * tp may be NULL.
3114302ce8d6SAndre Oppermann  */
3115302ce8d6SAndre Oppermann static void
3116302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp,
3117302ce8d6SAndre Oppermann     int tlen, int rstreason)
3118302ce8d6SAndre Oppermann {
3119b287c6c7SBjoern A. Zeeb #ifdef INET
3120302ce8d6SAndre Oppermann 	struct ip *ip;
3121b287c6c7SBjoern A. Zeeb #endif
3122302ce8d6SAndre Oppermann #ifdef INET6
3123302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6;
3124302ce8d6SAndre Oppermann #endif
31257a3244ccSRobert Watson 
31267a3244ccSRobert Watson 	if (tp != NULL) {
31278501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
31287a3244ccSRobert Watson 	}
31297a3244ccSRobert Watson 
31300ca3f933SAndre Oppermann 	/* Don't bother if destination was broadcast/multicast. */
3131302ce8d6SAndre Oppermann 	if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
3132302ce8d6SAndre Oppermann 		goto drop;
3133302ce8d6SAndre Oppermann #ifdef INET6
3134302ce8d6SAndre Oppermann 	if (mtod(m, struct ip *)->ip_v == 6) {
3135302ce8d6SAndre Oppermann 		ip6 = mtod(m, struct ip6_hdr *);
3136302ce8d6SAndre Oppermann 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3137302ce8d6SAndre Oppermann 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3138302ce8d6SAndre Oppermann 			goto drop;
3139302ce8d6SAndre Oppermann 		/* IPv6 anycast check is done at tcp6_input() */
3140b287c6c7SBjoern A. Zeeb 	}
3141302ce8d6SAndre Oppermann #endif
3142b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3143b287c6c7SBjoern A. Zeeb 	else
3144b287c6c7SBjoern A. Zeeb #endif
3145b287c6c7SBjoern A. Zeeb #ifdef INET
3146302ce8d6SAndre Oppermann 	{
3147302ce8d6SAndre Oppermann 		ip = mtod(m, struct ip *);
3148302ce8d6SAndre Oppermann 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3149302ce8d6SAndre Oppermann 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3150302ce8d6SAndre Oppermann 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3151302ce8d6SAndre Oppermann 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
3152302ce8d6SAndre Oppermann 			goto drop;
3153302ce8d6SAndre Oppermann 	}
3154b287c6c7SBjoern A. Zeeb #endif
3155302ce8d6SAndre Oppermann 
3156302ce8d6SAndre Oppermann 	/* Perform bandwidth limiting. */
3157302ce8d6SAndre Oppermann 	if (badport_bandlim(rstreason) < 0)
3158302ce8d6SAndre Oppermann 		goto drop;
3159302ce8d6SAndre Oppermann 
3160302ce8d6SAndre Oppermann 	/* tcp_respond consumes the mbuf chain. */
3161302ce8d6SAndre Oppermann 	if (th->th_flags & TH_ACK) {
3162302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0,
3163302ce8d6SAndre Oppermann 		    th->th_ack, TH_RST);
3164302ce8d6SAndre Oppermann 	} else {
3165302ce8d6SAndre Oppermann 		if (th->th_flags & TH_SYN)
3166302ce8d6SAndre Oppermann 			tlen++;
3167302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
3168302ce8d6SAndre Oppermann 		    (tcp_seq)0, TH_RST|TH_ACK);
3169302ce8d6SAndre Oppermann 	}
3170302ce8d6SAndre Oppermann 	return;
3171302ce8d6SAndre Oppermann drop:
3172302ce8d6SAndre Oppermann 	m_freem(m);
3173df8bae1dSRodney W. Grimes }
3174df8bae1dSRodney W. Grimes 
3175be2ac88cSJonathan Lemon /*
3176be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
3177be2ac88cSJonathan Lemon  */
31780312fbe9SPoul-Henning Kamp static void
3179ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags)
3180df8bae1dSRodney W. Grimes {
3181df8bae1dSRodney W. Grimes 	int opt, optlen;
3182df8bae1dSRodney W. Grimes 
3183be2ac88cSJonathan Lemon 	to->to_flags = 0;
3184df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
3185df8bae1dSRodney W. Grimes 		opt = cp[0];
3186df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
3187df8bae1dSRodney W. Grimes 			break;
3188df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
3189df8bae1dSRodney W. Grimes 			optlen = 1;
3190df8bae1dSRodney W. Grimes 		else {
3191b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
3192b474779fSJun-ichiro itojun Hagino 				break;
3193df8bae1dSRodney W. Grimes 			optlen = cp[1];
3194b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
3195df8bae1dSRodney W. Grimes 				break;
3196df8bae1dSRodney W. Grimes 		}
3197df8bae1dSRodney W. Grimes 		switch (opt) {
3198df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
3199df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
3200df8bae1dSRodney W. Grimes 				continue;
3201f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3202df8bae1dSRodney W. Grimes 				continue;
3203be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
3204be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
3205be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
3206fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
3207df8bae1dSRodney W. Grimes 			break;
3208df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
3209df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
3210df8bae1dSRodney W. Grimes 				continue;
3211f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3212df8bae1dSRodney W. Grimes 				continue;
3213be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
321402a1a643SAndre Oppermann 			to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT);
3215df8bae1dSRodney W. Grimes 			break;
3216df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
3217df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
3218df8bae1dSRodney W. Grimes 				continue;
3219be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
3220a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
3221a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
3222fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
3223a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
3224a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
3225fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
3226df8bae1dSRodney W. Grimes 			break;
32271cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
32281cfd4b53SBruce M Simpson 		/*
32291cfd4b53SBruce M Simpson 		 * XXX In order to reply to a host which has set the
32301cfd4b53SBruce M Simpson 		 * TCP_SIGNATURE option in its initial SYN, we have to
32311cfd4b53SBruce M Simpson 		 * record the fact that the option was observed here
32321cfd4b53SBruce M Simpson 		 * for the syncache code to perform the correct response.
32331cfd4b53SBruce M Simpson 		 */
32341cfd4b53SBruce M Simpson 		case TCPOPT_SIGNATURE:
32351cfd4b53SBruce M Simpson 			if (optlen != TCPOLEN_SIGNATURE)
32361cfd4b53SBruce M Simpson 				continue;
3237df47e437SAndre Oppermann 			to->to_flags |= TOF_SIGNATURE;
3238df47e437SAndre Oppermann 			to->to_signature = cp + 2;
32391cfd4b53SBruce M Simpson 			break;
3240265ed012SBruce M Simpson #endif
32416d90faf3SPaul Saab 		case TCPOPT_SACK_PERMITTED:
3242f72167f4SAndre Oppermann 			if (optlen != TCPOLEN_SACK_PERMITTED)
32436d90faf3SPaul Saab 				continue;
3244f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3245f72167f4SAndre Oppermann 				continue;
3246603724d3SBjoern A. Zeeb 			if (!V_tcp_do_sack)
3247f72167f4SAndre Oppermann 				continue;
3248fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACKPERM;
32496d90faf3SPaul Saab 			break;
32506d90faf3SPaul Saab 		case TCPOPT_SACK:
32515a53ca16SPaul Saab 			if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
32526d90faf3SPaul Saab 				continue;
3253b728e902SAndre Oppermann 			if (flags & TO_SYN)
3254b728e902SAndre Oppermann 				continue;
3255fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACK;
32565a53ca16SPaul Saab 			to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
32575a53ca16SPaul Saab 			to->to_sacks = cp + 2;
325878b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_rcv_blocks);
32596d90faf3SPaul Saab 			break;
3260be2ac88cSJonathan Lemon 		default:
3261be2ac88cSJonathan Lemon 			continue;
3262df8bae1dSRodney W. Grimes 		}
3263df8bae1dSRodney W. Grimes 	}
3264df8bae1dSRodney W. Grimes }
3265df8bae1dSRodney W. Grimes 
3266df8bae1dSRodney W. Grimes /*
3267df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
3268df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
3269df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
3270df8bae1dSRodney W. Grimes  * sequencing purposes.
3271df8bae1dSRodney W. Grimes  */
32720312fbe9SPoul-Henning Kamp static void
3273ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m,
3274ad3f9ab3SAndre Oppermann     int off)
3275df8bae1dSRodney W. Grimes {
3276fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
3277df8bae1dSRodney W. Grimes 
3278df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
3279df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
3280df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
3281df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
3282df8bae1dSRodney W. Grimes 
32838501a69cSRobert Watson 			INP_WLOCK_ASSERT(tp->t_inpcb);
32847a3244ccSRobert Watson 
3285df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
3286df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
3287df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
3288df8bae1dSRodney W. Grimes 			m->m_len--;
328969a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
329069a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
3291df8bae1dSRodney W. Grimes 			return;
3292df8bae1dSRodney W. Grimes 		}
3293df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
3294df8bae1dSRodney W. Grimes 		m = m->m_next;
32954dfdffe9SAndre Oppermann 		if (m == NULL)
3296df8bae1dSRodney W. Grimes 			break;
3297df8bae1dSRodney W. Grimes 	}
3298df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
3299df8bae1dSRodney W. Grimes }
3300df8bae1dSRodney W. Grimes 
3301df8bae1dSRodney W. Grimes /*
3302df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
3303df8bae1dSRodney W. Grimes  * and update averages and current timeout.
3304df8bae1dSRodney W. Grimes  */
33050312fbe9SPoul-Henning Kamp static void
3306ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt)
3307df8bae1dSRodney W. Grimes {
3308ad3f9ab3SAndre Oppermann 	int delta;
3309233e8c18SGarrett Wollman 
33108501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
33112be3bf22SRobert Watson 
331278b50714SRobert Watson 	TCPSTAT_INC(tcps_rttupdated);
3313233e8c18SGarrett Wollman 	tp->t_rttupdated++;
3314233e8c18SGarrett Wollman 	if (tp->t_srtt != 0) {
3315233e8c18SGarrett Wollman 		/*
3316233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
3317233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
3318233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
3319233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
3320233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
3321233e8c18SGarrett Wollman 		 */
3322233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
3323233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
3324233e8c18SGarrett Wollman 
3325233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
3326233e8c18SGarrett Wollman 			tp->t_srtt = 1;
3327233e8c18SGarrett Wollman 
3328233e8c18SGarrett Wollman 		/*
3329233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
3330233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
3331233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
3332233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
3333233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
3334233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
3335233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
3336233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
3337233e8c18SGarrett Wollman 		 */
3338233e8c18SGarrett Wollman 		if (delta < 0)
3339233e8c18SGarrett Wollman 			delta = -delta;
3340233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
3341233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
3342233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
33431fcc99b5SMatthew Dillon 		if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
33441fcc99b5SMatthew Dillon 		    tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3345233e8c18SGarrett Wollman 	} else {
3346233e8c18SGarrett Wollman 		/*
3347233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
3348233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
3349233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
3350233e8c18SGarrett Wollman 		 */
3351233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
3352233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
33531fcc99b5SMatthew Dillon 		tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3354233e8c18SGarrett Wollman 	}
33559b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
3356df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
3357df8bae1dSRodney W. Grimes 
3358df8bae1dSRodney W. Grimes 	/*
3359df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
3360df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
3361df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
3362df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
3363df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
3364df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
3365df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
3366df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
3367df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
3368df8bae1dSRodney W. Grimes 	 */
3369233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
33709e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
3371df8bae1dSRodney W. Grimes 
3372df8bae1dSRodney W. Grimes 	/*
3373df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
3374df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
3375df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
3376df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
3377df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
3378df8bae1dSRodney W. Grimes 	 */
3379df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
3380df8bae1dSRodney W. Grimes }
3381df8bae1dSRodney W. Grimes 
3382df8bae1dSRodney W. Grimes /*
3383df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
3384df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
33854faaea55SAndre Oppermann  * If none, use an mss that can be handled on the outgoing interface
33864faaea55SAndre Oppermann  * without forcing IP to fragment.  If no route is found, route has no mtu,
3387df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
3388df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
3389df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
3390df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
3391df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
3392df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
3393df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
3394a0292f23SGarrett Wollman  *
3395a0292f23SGarrett Wollman  * Also take into account the space needed for options that we
3396a0292f23SGarrett Wollman  * send regularly.  Make maxseg shorter by that amount to assure
3397a0292f23SGarrett Wollman  * that we can send maxseg amount of data even when the options
3398a0292f23SGarrett Wollman  * are present.  Store the upper limit of the length of options plus
3399a0292f23SGarrett Wollman  * data in maxopd.
3400a0292f23SGarrett Wollman  *
340197d8d152SAndre Oppermann  * NOTE that this routine is only called when we process an incoming
3402ef341ee1SGleb Smirnoff  * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS
3403ef341ee1SGleb Smirnoff  * settings are handled in tcp_mssopt().
3404df8bae1dSRodney W. Grimes  */
3405a0292f23SGarrett Wollman void
3406ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer,
34073c914c54SAndre Oppermann     struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap)
3408df8bae1dSRodney W. Grimes {
3409b287c6c7SBjoern A. Zeeb 	int mss = 0;
3410b287c6c7SBjoern A. Zeeb 	u_long maxmtu = 0;
3411c068736aSJeffrey Hsu 	struct inpcb *inp = tp->t_inpcb;
341297d8d152SAndre Oppermann 	struct hc_metrics_lite metrics;
3413ef341ee1SGleb Smirnoff 	int origoffer;
3414fb59c426SYoshinobu Inoue #ifdef INET6
3415c068736aSJeffrey Hsu 	int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
3416c068736aSJeffrey Hsu 	size_t min_protoh = isipv6 ?
3417c068736aSJeffrey Hsu 			    sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
3418c068736aSJeffrey Hsu 			    sizeof (struct tcpiphdr);
3419c068736aSJeffrey Hsu #else
3420c068736aSJeffrey Hsu 	const size_t min_protoh = sizeof(struct tcpiphdr);
3421fb59c426SYoshinobu Inoue #endif
3422df8bae1dSRodney W. Grimes 
34238501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
34247a3244ccSRobert Watson 
3425ef341ee1SGleb Smirnoff 	if (mtuoffer != -1) {
3426ef341ee1SGleb Smirnoff 		KASSERT(offer == -1, ("%s: conflict", __func__));
3427ef341ee1SGleb Smirnoff 		offer = mtuoffer - min_protoh;
3428ef341ee1SGleb Smirnoff 	}
3429ef341ee1SGleb Smirnoff 	origoffer = offer;
3430ef341ee1SGleb Smirnoff 
3431e8949f74SAndre Oppermann 	/* Initialize. */
343297d8d152SAndre Oppermann #ifdef INET6
343397d8d152SAndre Oppermann 	if (isipv6) {
34343c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu6(&inp->inp_inc, cap);
3435603724d3SBjoern A. Zeeb 		tp->t_maxopd = tp->t_maxseg = V_tcp_v6mssdflt;
3436b287c6c7SBjoern A. Zeeb 	}
343797d8d152SAndre Oppermann #endif
3438b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3439b287c6c7SBjoern A. Zeeb 	else
3440b287c6c7SBjoern A. Zeeb #endif
3441b287c6c7SBjoern A. Zeeb #ifdef INET
344297d8d152SAndre Oppermann 	{
34433c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu(&inp->inp_inc, cap);
3444603724d3SBjoern A. Zeeb 		tp->t_maxopd = tp->t_maxseg = V_tcp_mssdflt;
3445df8bae1dSRodney W. Grimes 	}
3446b287c6c7SBjoern A. Zeeb #endif
3447df8bae1dSRodney W. Grimes 
344897d8d152SAndre Oppermann 	/*
3449e8949f74SAndre Oppermann 	 * No route to sender, stay with default mss and return.
345097d8d152SAndre Oppermann 	 */
34516f01cac6SBjoern A. Zeeb 	if (maxmtu == 0) {
34526f01cac6SBjoern A. Zeeb 		/*
34538e5c87f4SBjoern A. Zeeb 		 * In case we return early we need to initialize metrics
34546f01cac6SBjoern A. Zeeb 		 * to a defined state as tcp_hc_get() would do for us
34556f01cac6SBjoern A. Zeeb 		 * if there was no cache hit.
34566f01cac6SBjoern A. Zeeb 		 */
34576f01cac6SBjoern A. Zeeb 		if (metricptr != NULL)
34586f01cac6SBjoern A. Zeeb 			bzero(metricptr, sizeof(struct hc_metrics_lite));
345997d8d152SAndre Oppermann 		return;
34606f01cac6SBjoern A. Zeeb 	}
346197d8d152SAndre Oppermann 
3462e8949f74SAndre Oppermann 	/* What have we got? */
346397d8d152SAndre Oppermann 	switch (offer) {
346497d8d152SAndre Oppermann 		case 0:
346597d8d152SAndre Oppermann 			/*
346697d8d152SAndre Oppermann 			 * Offer == 0 means that there was no MSS on the SYN
3467c3b02504SBjoern A. Zeeb 			 * segment, in this case we use tcp_mssdflt as
3468c3b02504SBjoern A. Zeeb 			 * already assigned to t_maxopd above.
346997d8d152SAndre Oppermann 			 */
3470c3b02504SBjoern A. Zeeb 			offer = tp->t_maxopd;
347197d8d152SAndre Oppermann 			break;
347297d8d152SAndre Oppermann 
347397d8d152SAndre Oppermann 		case -1:
3474a0292f23SGarrett Wollman 			/*
3475c94c54e4SAndre Oppermann 			 * Offer == -1 means that we didn't receive SYN yet.
3476a0292f23SGarrett Wollman 			 */
347797d8d152SAndre Oppermann 			/* FALLTHROUGH */
347897d8d152SAndre Oppermann 
347997d8d152SAndre Oppermann 		default:
3480a0292f23SGarrett Wollman 			/*
348153369ac9SAndre Oppermann 			 * Prevent DoS attack with too small MSS. Round up
348253369ac9SAndre Oppermann 			 * to at least minmss.
348353369ac9SAndre Oppermann 			 */
3484603724d3SBjoern A. Zeeb 			offer = max(offer, V_tcp_minmss);
348597d8d152SAndre Oppermann 	}
3486a0292f23SGarrett Wollman 
3487df8bae1dSRodney W. Grimes 	/*
3488e8949f74SAndre Oppermann 	 * rmx information is now retrieved from tcp_hostcache.
3489df8bae1dSRodney W. Grimes 	 */
349097d8d152SAndre Oppermann 	tcp_hc_get(&inp->inp_inc, &metrics);
34913cee92e0SBjoern A. Zeeb 	if (metricptr != NULL)
34923cee92e0SBjoern A. Zeeb 		bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite));
349397d8d152SAndre Oppermann 
3494df8bae1dSRodney W. Grimes 	/*
3495cc412412SHiren Panchasara 	 * If there's a discovered mtu in tcp hostcache, use it.
3496cc412412SHiren Panchasara 	 * Else, use the link mtu.
3497df8bae1dSRodney W. Grimes 	 */
349897d8d152SAndre Oppermann 	if (metrics.rmx_mtu)
34992d166c02SAndre Oppermann 		mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
3500c068736aSJeffrey Hsu 	else {
350131b3783cSHajimu UMEMOTO #ifdef INET6
3502fb59c426SYoshinobu Inoue 		if (isipv6) {
350397d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3504603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
350597d8d152SAndre Oppermann 			    !in6_localaddr(&inp->in6p_faddr))
3506603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_v6mssdflt);
3507b287c6c7SBjoern A. Zeeb 		}
3508b3399803SHajimu UMEMOTO #endif
3509b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3510b287c6c7SBjoern A. Zeeb 		else
3511b287c6c7SBjoern A. Zeeb #endif
3512b287c6c7SBjoern A. Zeeb #ifdef INET
351397d8d152SAndre Oppermann 		{
351497d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3515603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
351697d8d152SAndre Oppermann 			    !in_localaddr(inp->inp_faddr))
3517603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_mssdflt);
3518a0292f23SGarrett Wollman 		}
3519b287c6c7SBjoern A. Zeeb #endif
35203cee92e0SBjoern A. Zeeb 		/*
35213cee92e0SBjoern A. Zeeb 		 * XXX - The above conditional (mss = maxmtu - min_protoh)
35223cee92e0SBjoern A. Zeeb 		 * probably violates the TCP spec.
35233cee92e0SBjoern A. Zeeb 		 * The problem is that, since we don't know the
35243cee92e0SBjoern A. Zeeb 		 * other end's MSS, we are supposed to use a conservative
35253cee92e0SBjoern A. Zeeb 		 * default.  But, if we do that, then MTU discovery will
35263cee92e0SBjoern A. Zeeb 		 * never actually take place, because the conservative
35273cee92e0SBjoern A. Zeeb 		 * default is much less than the MTUs typically seen
35283cee92e0SBjoern A. Zeeb 		 * on the Internet today.  For the moment, we'll sweep
35293cee92e0SBjoern A. Zeeb 		 * this under the carpet.
35303cee92e0SBjoern A. Zeeb 		 *
35313cee92e0SBjoern A. Zeeb 		 * The conservative default might not actually be a problem
35323cee92e0SBjoern A. Zeeb 		 * if the only case this occurs is when sending an initial
35333cee92e0SBjoern A. Zeeb 		 * SYN with options and data to a host we've never talked
35343cee92e0SBjoern A. Zeeb 		 * to before.  Then, they will reply with an MSS value which
35353cee92e0SBjoern A. Zeeb 		 * will get recorded and the new parameters should get
35363cee92e0SBjoern A. Zeeb 		 * recomputed.  For Further Study.
35373cee92e0SBjoern A. Zeeb 		 */
353897d8d152SAndre Oppermann 	}
3539a0292f23SGarrett Wollman 	mss = min(mss, offer);
354097d8d152SAndre Oppermann 
3541a0292f23SGarrett Wollman 	/*
35423cee92e0SBjoern A. Zeeb 	 * Sanity check: make sure that maxopd will be large
35433cee92e0SBjoern A. Zeeb 	 * enough to allow some data on segments even if the
35443cee92e0SBjoern A. Zeeb 	 * all the option space is used (40bytes).  Otherwise
35453cee92e0SBjoern A. Zeeb 	 * funny things may happen in tcp_output.
35463cee92e0SBjoern A. Zeeb 	 */
35473cee92e0SBjoern A. Zeeb 	mss = max(mss, 64);
35483cee92e0SBjoern A. Zeeb 
35493cee92e0SBjoern A. Zeeb 	/*
3550a0292f23SGarrett Wollman 	 * maxopd stores the maximum length of data AND options
3551a0292f23SGarrett Wollman 	 * in a segment; maxseg is the amount of data in a normal
3552a0292f23SGarrett Wollman 	 * segment.  We need to store this value (maxopd) apart
3553a0292f23SGarrett Wollman 	 * from maxseg, because now every segment carries options
3554a0292f23SGarrett Wollman 	 * and thus we normally have somewhat less data in segments.
3555a0292f23SGarrett Wollman 	 */
3556a0292f23SGarrett Wollman 	tp->t_maxopd = mss;
3557a0292f23SGarrett Wollman 
3558a0292f23SGarrett Wollman 	/*
3559e8949f74SAndre Oppermann 	 * origoffer==-1 indicates that no segments were received yet.
3560c94c54e4SAndre Oppermann 	 * In this case we just guess.
3561a0292f23SGarrett Wollman 	 */
3562a0292f23SGarrett Wollman 	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
3563a0292f23SGarrett Wollman 	    (origoffer == -1 ||
3564a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
3565a0292f23SGarrett Wollman 		mss -= TCPOLEN_TSTAMP_APPA;
3566a0292f23SGarrett Wollman 
356797d8d152SAndre Oppermann 	tp->t_maxseg = mss;
35683cee92e0SBjoern A. Zeeb }
35693cee92e0SBjoern A. Zeeb 
35703cee92e0SBjoern A. Zeeb void
35713cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer)
35723cee92e0SBjoern A. Zeeb {
3573dbc42409SLawrence Stewart 	int mss;
35743cee92e0SBjoern A. Zeeb 	u_long bufsize;
35753cee92e0SBjoern A. Zeeb 	struct inpcb *inp;
35763cee92e0SBjoern A. Zeeb 	struct socket *so;
35773cee92e0SBjoern A. Zeeb 	struct hc_metrics_lite metrics;
35783c914c54SAndre Oppermann 	struct tcp_ifcap cap;
3579dbc42409SLawrence Stewart 
35803cee92e0SBjoern A. Zeeb 	KASSERT(tp != NULL, ("%s: tp == NULL", __func__));
35813cee92e0SBjoern A. Zeeb 
35823c914c54SAndre Oppermann 	bzero(&cap, sizeof(cap));
35833c914c54SAndre Oppermann 	tcp_mss_update(tp, offer, -1, &metrics, &cap);
35843cee92e0SBjoern A. Zeeb 
35853cee92e0SBjoern A. Zeeb 	mss = tp->t_maxseg;
35863cee92e0SBjoern A. Zeeb 	inp = tp->t_inpcb;
358797d8d152SAndre Oppermann 
3588df8bae1dSRodney W. Grimes 	/*
358997d8d152SAndre Oppermann 	 * If there's a pipesize, change the socket buffer to that size,
359097d8d152SAndre Oppermann 	 * don't change if sb_hiwat is different than default (then it
359197d8d152SAndre Oppermann 	 * has been changed on purpose with setsockopt).
359297d8d152SAndre Oppermann 	 * Make the socket buffers an integral number of mss units;
359397d8d152SAndre Oppermann 	 * if the mss is larger than the socket buffer, decrease the mss.
3594df8bae1dSRodney W. Grimes 	 */
3595c3b02504SBjoern A. Zeeb 	so = inp->inp_socket;
35963f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
3597e233e2acSAndre Oppermann 	if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe)
359897d8d152SAndre Oppermann 		bufsize = metrics.rmx_sendpipe;
359997d8d152SAndre Oppermann 	else
3600df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
3601df8bae1dSRodney W. Grimes 	if (bufsize < mss)
3602df8bae1dSRodney W. Grimes 		mss = bufsize;
3603df8bae1dSRodney W. Grimes 	else {
3604df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3605df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3606df8bae1dSRodney W. Grimes 			bufsize = sb_max;
360788c39af3SRuslan Ermilov 		if (bufsize > so->so_snd.sb_hiwat)
36083f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_snd, bufsize, so, NULL);
3609df8bae1dSRodney W. Grimes 	}
36103f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
3611df8bae1dSRodney W. Grimes 	tp->t_maxseg = mss;
3612df8bae1dSRodney W. Grimes 
36133f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
3614e233e2acSAndre Oppermann 	if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe)
361597d8d152SAndre Oppermann 		bufsize = metrics.rmx_recvpipe;
361697d8d152SAndre Oppermann 	else
3617df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
3618df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
3619df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3620df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3621df8bae1dSRodney W. Grimes 			bufsize = sb_max;
362288c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
36233f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL);
3624df8bae1dSRodney W. Grimes 	}
36253f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
362691d6cfa6SBjoern A. Zeeb 
362791d6cfa6SBjoern A. Zeeb 	/* Check the interface for TSO capabilities. */
36283c914c54SAndre Oppermann 	if (cap.ifcap & CSUM_TSO) {
362991d6cfa6SBjoern A. Zeeb 		tp->t_flags |= TF_TSO;
36303c914c54SAndre Oppermann 		tp->t_tsomax = cap.tsomax;
36319fd573c3SHans Petter Selasky 		tp->t_tsomaxsegcount = cap.tsomaxsegcount;
36329fd573c3SHans Petter Selasky 		tp->t_tsomaxsegsize = cap.tsomaxsegsize;
36333c914c54SAndre Oppermann 	}
3634a0292f23SGarrett Wollman }
3635a0292f23SGarrett Wollman 
3636a0292f23SGarrett Wollman /*
3637a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
3638a0292f23SGarrett Wollman  */
3639a0292f23SGarrett Wollman int
3640ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc)
3641a0292f23SGarrett Wollman {
364297d8d152SAndre Oppermann 	int mss = 0;
364397d8d152SAndre Oppermann 	u_long maxmtu = 0;
364497d8d152SAndre Oppermann 	u_long thcmtu = 0;
364597d8d152SAndre Oppermann 	size_t min_protoh;
3646a0292f23SGarrett Wollman 
364797d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3648a0292f23SGarrett Wollman 
364931b3783cSHajimu UMEMOTO #ifdef INET6
3650dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
3651603724d3SBjoern A. Zeeb 		mss = V_tcp_v6mssdflt;
3652233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(inc, NULL);
365397d8d152SAndre Oppermann 		min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
3654b287c6c7SBjoern A. Zeeb 	}
365531b3783cSHajimu UMEMOTO #endif
3656b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3657b287c6c7SBjoern A. Zeeb 	else
3658b287c6c7SBjoern A. Zeeb #endif
3659b287c6c7SBjoern A. Zeeb #ifdef INET
366097d8d152SAndre Oppermann 	{
3661603724d3SBjoern A. Zeeb 		mss = V_tcp_mssdflt;
3662233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(inc, NULL);
366397d8d152SAndre Oppermann 		min_protoh = sizeof(struct tcpiphdr);
366497d8d152SAndre Oppermann 	}
3665b287c6c7SBjoern A. Zeeb #endif
36663a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET)
36673a9391deSBjoern A. Zeeb 	thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
36683a9391deSBjoern A. Zeeb #endif
36693a9391deSBjoern A. Zeeb 
367097d8d152SAndre Oppermann 	if (maxmtu && thcmtu)
367197d8d152SAndre Oppermann 		mss = min(maxmtu, thcmtu) - min_protoh;
367297d8d152SAndre Oppermann 	else if (maxmtu || thcmtu)
367397d8d152SAndre Oppermann 		mss = max(maxmtu, thcmtu) - min_protoh;
367497d8d152SAndre Oppermann 
367597d8d152SAndre Oppermann 	return (mss);
3676df8bae1dSRodney W. Grimes }
367746f58482SJonathan Lemon 
367846f58482SJonathan Lemon 
367946f58482SJonathan Lemon /*
3680c068736aSJeffrey Hsu  * On a partial ack arrives, force the retransmission of the
3681c068736aSJeffrey Hsu  * next unacknowledged segment.  Do not clear tp->t_dupacks.
3682c068736aSJeffrey Hsu  * By setting snd_nxt to ti_ack, this forces retransmission timer to
3683c068736aSJeffrey Hsu  * be started again.
368446f58482SJonathan Lemon  */
3685c068736aSJeffrey Hsu static void
3686ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
368746f58482SJonathan Lemon {
368846f58482SJonathan Lemon 	tcp_seq onxt = tp->snd_nxt;
368946f58482SJonathan Lemon 	u_long  ocwnd = tp->snd_cwnd;
369046f58482SJonathan Lemon 
36918501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
36927a3244ccSRobert Watson 
3693b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_REXMT, 0);
369446f58482SJonathan Lemon 	tp->t_rtttime = 0;
369546f58482SJonathan Lemon 	tp->snd_nxt = th->th_ack;
36966b2a5f92SJayanth Vijayaraghavan 	/*
3697c068736aSJeffrey Hsu 	 * Set snd_cwnd to one segment beyond acknowledged offset.
3698c068736aSJeffrey Hsu 	 * (tp->snd_una has not yet been updated when this function is called.)
36996b2a5f92SJayanth Vijayaraghavan 	 */
3700dbc42409SLawrence Stewart 	tp->snd_cwnd = tp->t_maxseg + BYTES_THIS_ACK(tp, th);
3701a84db8f4SMatthew Dillon 	tp->t_flags |= TF_ACKNOW;
37026b2a5f92SJayanth Vijayaraghavan 	(void) tcp_output(tp);
370346f58482SJonathan Lemon 	tp->snd_cwnd = ocwnd;
370446f58482SJonathan Lemon 	if (SEQ_GT(onxt, tp->snd_nxt))
370546f58482SJonathan Lemon 		tp->snd_nxt = onxt;
370646f58482SJonathan Lemon 	/*
370746f58482SJonathan Lemon 	 * Partial window deflation.  Relies on fact that tp->snd_una
370846f58482SJonathan Lemon 	 * not updated yet.
370946f58482SJonathan Lemon 	 */
3710dbc42409SLawrence Stewart 	if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th))
3711dbc42409SLawrence Stewart 		tp->snd_cwnd -= BYTES_THIS_ACK(tp, th);
3712d7587117SPaul Saab 	else
3713d7587117SPaul Saab 		tp->snd_cwnd = 0;
3714d7587117SPaul Saab 	tp->snd_cwnd += tp->t_maxseg;
371546f58482SJonathan Lemon }
3716