xref: /freebsd/sys/netinet/tcp_input.c (revision c1e5a6e5e8b255c49a1a97065bb381750df77e35)
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"
5757f60867SMark Johnston #include "opt_kdtrace.h"
580cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
590cc12cc5SJoerg Wunsch 
60df8bae1dSRodney W. Grimes #include <sys/param.h>
6198163b98SPoul-Henning Kamp #include <sys/kernel.h>
6239bc9de5SLawrence Stewart #include <sys/hhook.h>
63df8bae1dSRodney W. Grimes #include <sys/malloc.h>
64df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
65a29f300eSGarrett Wollman #include <sys/proc.h>		/* for proc0 declaration */
66df8bae1dSRodney W. Grimes #include <sys/protosw.h>
6757f60867SMark Johnston #include <sys/sdt.h>
68960ed29cSSeigo Tanimura #include <sys/signalvar.h>
69df8bae1dSRodney W. Grimes #include <sys/socket.h>
70df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
71960ed29cSSeigo Tanimura #include <sys/sysctl.h>
72816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
73960ed29cSSeigo Tanimura #include <sys/systm.h>
74df8bae1dSRodney W. Grimes 
75e79adb8eSGarrett Wollman #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
76e79adb8eSGarrett Wollman 
7712e2e970SAndre Oppermann #include <vm/uma.h>
7812e2e970SAndre Oppermann 
79df8bae1dSRodney W. Grimes #include <net/if.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)
133eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, blackhole, 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;
138eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, delayed_ack, 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)
144eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, drop_synfin, 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)
150eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3042, 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;
155eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3390, 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;
16309440655SAndre Oppermann SYSCTL_VNET_INT(_net_inet_tcp_experimental, OID_AUTO, initcwnd10, CTLFLAG_RW,
16409440655SAndre Oppermann     &VNET_NAME(tcp_do_initcwnd10), 0,
16509440655SAndre Oppermann     "Enable draft-ietf-tcpm-initcwnd-05 (Increasing initial CWND to 10)");
16609440655SAndre Oppermann 
16782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1;
168eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3465, 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;
173eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, abc_l_var, 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;
180eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp_ecn, OID_AUTO, enable, 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;
185eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_RW,
186eddfbb76SRobert Watson     &VNET_NAME(tcp_ecn_maxretries), 0,
187eddfbb76SRobert Watson     "Max retries before giving up on ECN");
188eddfbb76SRobert Watson 
18982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0;
19082cea7e6SBjoern A. Zeeb #define	V_tcp_insecure_rst	VNET(tcp_insecure_rst)
191eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_RW,
192eddfbb76SRobert Watson     &VNET_NAME(tcp_insecure_rst), 0,
1936489fe65SAndre Oppermann     "Follow the old (insecure) criteria for accepting RST packets");
194a69968eeSMike Silbersack 
195fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64;
196873789cbSAndre Oppermann #define	V_tcp_recvspace	VNET(tcp_recvspace)
197ddd0c4a9SSergey Kandaurov SYSCTL_VNET_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
198873789cbSAndre Oppermann     &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size");
199873789cbSAndre Oppermann 
20082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1;
20182cea7e6SBjoern A. Zeeb #define	V_tcp_do_autorcvbuf	VNET(tcp_do_autorcvbuf)
202eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_RW,
203eddfbb76SRobert Watson     &VNET_NAME(tcp_do_autorcvbuf), 0,
2048b615593SMarko Zec     "Enable automatic receive buffer sizing");
2056741ecf5SAndre Oppermann 
20682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_inc) = 16*1024;
20782cea7e6SBjoern A. Zeeb #define	V_tcp_autorcvbuf_inc	VNET(tcp_autorcvbuf_inc)
208eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_RW,
209eddfbb76SRobert Watson     &VNET_NAME(tcp_autorcvbuf_inc), 0,
2106489fe65SAndre Oppermann     "Incrementor step size of automatic receive buffer");
2116741ecf5SAndre Oppermann 
212b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024;
21382cea7e6SBjoern A. Zeeb #define	V_tcp_autorcvbuf_max	VNET(tcp_autorcvbuf_max)
214eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_RW,
215eddfbb76SRobert Watson     &VNET_NAME(tcp_autorcvbuf_max), 0,
2168b615593SMarko Zec     "Max size of automatic receive buffer");
2176741ecf5SAndre Oppermann 
218eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, tcb);
21944e33a07SMarko Zec #define	tcb6	tcb  /* for KAME src sync over BSD*'s */
22082cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo);
221df8bae1dSRodney W. Grimes 
2225a53ca16SPaul Saab static void	 tcp_dooptions(struct tcpopt *, u_char *, int, int);
223679d9708SAndre Oppermann static void	 tcp_do_segment(struct mbuf *, struct tcphdr *,
224252ca428SRobert Watson 		     struct socket *, struct tcpcb *, int, int, uint8_t,
225252ca428SRobert Watson 		     int);
226302ce8d6SAndre Oppermann static void	 tcp_dropwithreset(struct mbuf *, struct tcphdr *,
227302ce8d6SAndre Oppermann 		     struct tcpcb *, int, int);
2284d77a549SAlfred Perlstein static void	 tcp_pulloutofband(struct socket *,
2294d77a549SAlfred Perlstein 		     struct tcphdr *, struct mbuf *, int);
2304d77a549SAlfred Perlstein static void	 tcp_xmit_timer(struct tcpcb *, int);
231c068736aSJeffrey Hsu static void	 tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *);
2322903309aSAttilio Rao static void inline 	tcp_fields_to_host(struct tcphdr *);
2332903309aSAttilio Rao #ifdef TCP_SIGNATURE
2342903309aSAttilio Rao static void inline 	tcp_fields_to_net(struct tcphdr *);
2352903309aSAttilio Rao static int inline	tcp_signature_verify_input(struct mbuf *, int, int,
2362903309aSAttilio Rao 			    int, struct tcpopt *, struct tcphdr *, u_int);
2372903309aSAttilio Rao #endif
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.
36309440655SAndre Oppermann 	 * Draft-ietf-tcpm-initcwnd-05 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 static inline void
4592903309aSAttilio Rao tcp_fields_to_host(struct tcphdr *th)
4602903309aSAttilio Rao {
4612903309aSAttilio Rao 
4622903309aSAttilio Rao 	th->th_seq = ntohl(th->th_seq);
4632903309aSAttilio Rao 	th->th_ack = ntohl(th->th_ack);
4642903309aSAttilio Rao 	th->th_win = ntohs(th->th_win);
4652903309aSAttilio Rao 	th->th_urp = ntohs(th->th_urp);
4662903309aSAttilio Rao }
4672903309aSAttilio Rao 
4682903309aSAttilio Rao #ifdef TCP_SIGNATURE
4692903309aSAttilio Rao static inline void
4702903309aSAttilio Rao tcp_fields_to_net(struct tcphdr *th)
4712903309aSAttilio Rao {
4722903309aSAttilio Rao 
4732903309aSAttilio Rao 	th->th_seq = htonl(th->th_seq);
4742903309aSAttilio Rao 	th->th_ack = htonl(th->th_ack);
4752903309aSAttilio Rao 	th->th_win = htons(th->th_win);
4762903309aSAttilio Rao 	th->th_urp = htons(th->th_urp);
4772903309aSAttilio Rao }
4782903309aSAttilio Rao 
4792903309aSAttilio Rao static inline int
4802903309aSAttilio Rao tcp_signature_verify_input(struct mbuf *m, int off0, int tlen, int optlen,
4812903309aSAttilio Rao     struct tcpopt *to, struct tcphdr *th, u_int tcpbflag)
4822903309aSAttilio Rao {
4832903309aSAttilio Rao 	int ret;
4842903309aSAttilio Rao 
4852903309aSAttilio Rao 	tcp_fields_to_net(th);
4862903309aSAttilio Rao 	ret = tcp_signature_verify(m, off0, tlen, optlen, to, th, tcpbflag);
4872903309aSAttilio Rao 	tcp_fields_to_host(th);
4882903309aSAttilio Rao 	return (ret);
4892903309aSAttilio Rao }
4902903309aSAttilio Rao #endif
4912903309aSAttilio Rao 
492fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
493fb59c426SYoshinobu Inoue #ifdef INET6
494fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \
495fb59c426SYoshinobu Inoue do { \
496fb59c426SYoshinobu Inoue 	if ((tp) && (tp)->t_inpcb && \
49797d8d152SAndre Oppermann 	    ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \
49897d8d152SAndre Oppermann 		nd6_nud_hint(NULL, NULL, 0); \
499fb59c426SYoshinobu Inoue } while (0)
500fb59c426SYoshinobu Inoue #else
501fb59c426SYoshinobu Inoue #define ND6_HINT(tp)
502fb59c426SYoshinobu Inoue #endif
503df8bae1dSRodney W. Grimes 
504df8bae1dSRodney W. Grimes /*
505262c1c1aSMatthew Dillon  * Indicate whether this ack should be delayed.  We can delay the ack if
506262c1c1aSMatthew Dillon  *	- there is no delayed ack timer in progress and
507262c1c1aSMatthew Dillon  *	- our last ack wasn't a 0-sized window.  We never want to delay
5083bfd6421SJonathan Lemon  *	  the ack that opens up a 0-sized window and
5093bfd6421SJonathan Lemon  *		- delayed acks are enabled or
5103bfd6421SJonathan Lemon  *		- this is a half-synchronized T/TCP connection.
511*c1e5a6e5SAndre Oppermann  *	- the segment size is not larger than the MSS and LRO wasn't used
512*c1e5a6e5SAndre Oppermann  *	  for this segment.
513d8c85a26SJonathan Lemon  */
514*c1e5a6e5SAndre Oppermann #define DELAY_ACK(tp, tlen)						\
515b8152ba7SAndre Oppermann 	((!tcp_timer_active(tp, TT_DELACK) &&				\
516a14c749fSJonathan Lemon 	    (tp->t_flags & TF_RXWIN0SENT) == 0) &&			\
517*c1e5a6e5SAndre Oppermann 	    (tlen <= tp->t_maxopd) &&					\
518603724d3SBjoern A. Zeeb 	    (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
519d8c85a26SJonathan Lemon 
520df8bae1dSRodney W. Grimes /*
5218d573cc1SAndre Oppermann  * TCP input handling is split into multiple parts:
5228d573cc1SAndre Oppermann  *   tcp6_input is a thin wrapper around tcp_input for the extended
5238d573cc1SAndre Oppermann  *	ip6_protox[] call format in ip6_input
5248d573cc1SAndre Oppermann  *   tcp_input handles primary segment validation, inpcb lookup and
5258d573cc1SAndre Oppermann  *	SYN processing on listen sockets
5268d573cc1SAndre Oppermann  *   tcp_do_segment processes the ACK and text of the segment for
5278d573cc1SAndre Oppermann  *	establishing, established and closing connections
528df8bae1dSRodney W. Grimes  */
529fb59c426SYoshinobu Inoue #ifdef INET6
530fb59c426SYoshinobu Inoue int
531ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto)
532fb59c426SYoshinobu Inoue {
533ad3f9ab3SAndre Oppermann 	struct mbuf *m = *mp;
53433841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
535fb59c426SYoshinobu Inoue 
536fb59c426SYoshinobu Inoue 	IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
537fb59c426SYoshinobu Inoue 
538fb59c426SYoshinobu Inoue 	/*
539fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
540fb59c426SYoshinobu Inoue 	 * better place to put this in?
541fb59c426SYoshinobu Inoue 	 */
54233841545SHajimu UMEMOTO 	ia6 = ip6_getdstifaddr(m);
54333841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
544fb59c426SYoshinobu Inoue 		struct ip6_hdr *ip6;
545fb59c426SYoshinobu Inoue 
5468c0fec80SRobert Watson 		ifa_free(&ia6->ia_ifa);
547fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
548fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
549fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
550fb59c426SYoshinobu Inoue 		return IPPROTO_DONE;
551fb59c426SYoshinobu Inoue 	}
55277d396fdSMaksim Yevmenkin 	if (ia6)
55377d396fdSMaksim Yevmenkin 		ifa_free(&ia6->ia_ifa);
554fb59c426SYoshinobu Inoue 
555f0ffb944SJulian Elischer 	tcp_input(m, *offp);
556fb59c426SYoshinobu Inoue 	return IPPROTO_DONE;
557fb59c426SYoshinobu Inoue }
558b287c6c7SBjoern A. Zeeb #endif /* INET6 */
559fb59c426SYoshinobu Inoue 
560df8bae1dSRodney W. Grimes void
561ad3f9ab3SAndre Oppermann tcp_input(struct mbuf *m, int off0)
562df8bae1dSRodney W. Grimes {
563b287c6c7SBjoern A. Zeeb 	struct tcphdr *th = NULL;
564ad3f9ab3SAndre Oppermann 	struct ip *ip = NULL;
565ad3f9ab3SAndre Oppermann 	struct inpcb *inp = NULL;
566302ce8d6SAndre Oppermann 	struct tcpcb *tp = NULL;
567302ce8d6SAndre Oppermann 	struct socket *so = NULL;
568e79adb8eSGarrett Wollman 	u_char *optp = NULL;
56926f9a767SRodney W. Grimes 	int optlen = 0;
570b287c6c7SBjoern A. Zeeb #ifdef INET
571b287c6c7SBjoern A. Zeeb 	int len;
572b287c6c7SBjoern A. Zeeb #endif
573b287c6c7SBjoern A. Zeeb 	int tlen = 0, off;
574fb59c426SYoshinobu Inoue 	int drop_hdrlen;
575ad3f9ab3SAndre Oppermann 	int thflags;
576302ce8d6SAndre Oppermann 	int rstreason = 0;	/* For badport_bandlim accounting purposes */
5772903309aSAttilio Rao #ifdef TCP_SIGNATURE
5782903309aSAttilio Rao 	uint8_t sig_checked = 0;
5792903309aSAttilio Rao #endif
580b287c6c7SBjoern A. Zeeb 	uint8_t iptos = 0;
581c1de64a4SAndrey V. Elsukov 	struct m_tag *fwd_tag = NULL;
582c068736aSJeffrey Hsu #ifdef INET6
583302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6 = NULL;
584c068736aSJeffrey Hsu 	int isipv6;
585c068736aSJeffrey Hsu #else
586a160e630SAndre Oppermann 	const void *ip6 = NULL;
587b287c6c7SBjoern A. Zeeb #endif /* INET6 */
588302ce8d6SAndre Oppermann 	struct tcpopt to;		/* options in this segment */
589a160e630SAndre Oppermann 	char *s = NULL;			/* address and port logging */
590252ca428SRobert Watson 	int ti_locked;
591252ca428SRobert Watson #define	TI_UNLOCKED	1
592fa046d87SRobert Watson #define	TI_WLOCKED	2
593f76fcf6dSJeffrey Hsu 
594610ee2f9SDavid Greenman #ifdef TCPDEBUG
595c068736aSJeffrey Hsu 	/*
596c068736aSJeffrey Hsu 	 * The size of tcp_saveipgen must be the size of the max ip header,
597c068736aSJeffrey Hsu 	 * now IPv6.
598c068736aSJeffrey Hsu 	 */
59914739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
600410bb1bfSLuigi Rizzo 	struct tcphdr tcp_savetcp;
601610ee2f9SDavid Greenman 	short ostate = 0;
602610ee2f9SDavid Greenman #endif
603c068736aSJeffrey Hsu 
604fb59c426SYoshinobu Inoue #ifdef INET6
605fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
606fb59c426SYoshinobu Inoue #endif
607a0292f23SGarrett Wollman 
608302ce8d6SAndre Oppermann 	to.to_flags = 0;
60978b50714SRobert Watson 	TCPSTAT_INC(tcps_rcvtotal);
610fb59c426SYoshinobu Inoue 
61104d3a452SHajimu UMEMOTO #ifdef INET6
612b287c6c7SBjoern A. Zeeb 	if (isipv6) {
6138d573cc1SAndre Oppermann 		/* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */
61445747ba5SBjoern A. Zeeb 
61545747ba5SBjoern A. Zeeb 		if (m->m_len < (sizeof(*ip6) + sizeof(*th))) {
61645747ba5SBjoern A. Zeeb 			m = m_pullup(m, sizeof(*ip6) + sizeof(*th));
61745747ba5SBjoern A. Zeeb 			if (m == NULL) {
61845747ba5SBjoern A. Zeeb 				TCPSTAT_INC(tcps_rcvshort);
61945747ba5SBjoern A. Zeeb 				return;
62045747ba5SBjoern A. Zeeb 			}
62145747ba5SBjoern A. Zeeb 		}
62245747ba5SBjoern A. Zeeb 
623fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
62445747ba5SBjoern A. Zeeb 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
625fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
626356ab07eSBjoern A. Zeeb 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) {
62745747ba5SBjoern A. Zeeb 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
62845747ba5SBjoern A. Zeeb 				th->th_sum = m->m_pkthdr.csum_data;
62945747ba5SBjoern A. Zeeb 			else
63045747ba5SBjoern A. Zeeb 				th->th_sum = in6_cksum_pseudo(ip6, tlen,
63145747ba5SBjoern A. Zeeb 				    IPPROTO_TCP, m->m_pkthdr.csum_data);
63245747ba5SBjoern A. Zeeb 			th->th_sum ^= 0xffff;
63345747ba5SBjoern A. Zeeb 		} else
63445747ba5SBjoern A. Zeeb 			th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen);
63545747ba5SBjoern A. Zeeb 		if (th->th_sum) {
63678b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
637fb59c426SYoshinobu Inoue 			goto drop;
638fb59c426SYoshinobu Inoue 		}
63933841545SHajimu UMEMOTO 
64033841545SHajimu UMEMOTO 		/*
64133841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
64233841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
64333841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
64433841545SHajimu UMEMOTO 		 *
64533841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
64633841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
64733841545SHajimu UMEMOTO 		 */
64833841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
64933841545SHajimu UMEMOTO 			/* XXX stat */
65033841545SHajimu UMEMOTO 			goto drop;
65133841545SHajimu UMEMOTO 		}
652b287c6c7SBjoern A. Zeeb 	}
65304d3a452SHajimu UMEMOTO #endif
654b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
655b287c6c7SBjoern A. Zeeb 	else
656b287c6c7SBjoern A. Zeeb #endif
657b287c6c7SBjoern A. Zeeb #ifdef INET
658b287c6c7SBjoern A. Zeeb 	{
659df8bae1dSRodney W. Grimes 		/*
660df8bae1dSRodney W. Grimes 		 * Get IP and TCP header together in first mbuf.
661df8bae1dSRodney W. Grimes 		 * Note: IP leaves IP header in first mbuf.
662df8bae1dSRodney W. Grimes 		 */
663fb59c426SYoshinobu Inoue 		if (off0 > sizeof (struct ip)) {
664105bd211SGleb Smirnoff 			ip_stripoptions(m);
665fb59c426SYoshinobu Inoue 			off0 = sizeof(struct ip);
666fb59c426SYoshinobu Inoue 		}
667df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof (struct tcpiphdr)) {
6684dfdffe9SAndre Oppermann 			if ((m = m_pullup(m, sizeof (struct tcpiphdr)))
6694dfdffe9SAndre Oppermann 			    == NULL) {
67078b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvshort);
671df8bae1dSRodney W. Grimes 				return;
672df8bae1dSRodney W. Grimes 			}
673df8bae1dSRodney W. Grimes 		}
674fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
675db4f9cc7SJonathan Lemon 		th = (struct tcphdr *)((caddr_t)ip + off0);
6768ad458a4SGleb Smirnoff 		tlen = ntohs(ip->ip_len) - off0;
677df8bae1dSRodney W. Grimes 
678db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
679db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
680db4f9cc7SJonathan Lemon 				th->th_sum = m->m_pkthdr.csum_data;
681db4f9cc7SJonathan Lemon 			else
682db4f9cc7SJonathan Lemon 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
683c068736aSJeffrey Hsu 				    ip->ip_dst.s_addr,
6848f134647SGleb Smirnoff 				    htonl(m->m_pkthdr.csum_data + tlen +
685c068736aSJeffrey Hsu 				    IPPROTO_TCP));
686db4f9cc7SJonathan Lemon 			th->th_sum ^= 0xffff;
687db4f9cc7SJonathan Lemon 		} else {
6888f134647SGleb Smirnoff 			struct ipovly *ipov = (struct ipovly *)ip;
6898f134647SGleb Smirnoff 
690df8bae1dSRodney W. Grimes 			/*
691df8bae1dSRodney W. Grimes 			 * Checksum extended TCP header and data.
692df8bae1dSRodney W. Grimes 			 */
6938f134647SGleb Smirnoff 			len = off0 + tlen;
694fb59c426SYoshinobu Inoue 			bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
6958f134647SGleb Smirnoff 			ipov->ih_len = htons(tlen);
696fb59c426SYoshinobu Inoue 			th->th_sum = in_cksum(m, len);
69757f60867SMark Johnston 			/* Reset length for SDT probes. */
69857f60867SMark Johnston 			ip->ip_len = htons(tlen + off0);
699db4f9cc7SJonathan Lemon 		}
70057f60867SMark Johnston 
701fb59c426SYoshinobu Inoue 		if (th->th_sum) {
70278b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
703df8bae1dSRodney W. Grimes 			goto drop;
704df8bae1dSRodney W. Grimes 		}
705fb59c426SYoshinobu Inoue 		/* Re-initialization for later version check */
706fb59c426SYoshinobu Inoue 		ip->ip_v = IPVERSION;
707fb59c426SYoshinobu Inoue 	}
708b287c6c7SBjoern A. Zeeb #endif /* INET */
709df8bae1dSRodney W. Grimes 
710f2512ba1SRui Paulo #ifdef INET6
711f2512ba1SRui Paulo 	if (isipv6)
712f2512ba1SRui Paulo 		iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
713b287c6c7SBjoern A. Zeeb #endif
714b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
715f2512ba1SRui Paulo 	else
716f2512ba1SRui Paulo #endif
717b287c6c7SBjoern A. Zeeb #ifdef INET
718f2512ba1SRui Paulo 		iptos = ip->ip_tos;
719b287c6c7SBjoern A. Zeeb #endif
720f2512ba1SRui Paulo 
721df8bae1dSRodney W. Grimes 	/*
722df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
723df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
724df8bae1dSRodney W. Grimes 	 */
725fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
726df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
72778b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvbadoff);
728df8bae1dSRodney W. Grimes 		goto drop;
729df8bae1dSRodney W. Grimes 	}
730fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
731df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
73204d3a452SHajimu UMEMOTO #ifdef INET6
733b287c6c7SBjoern A. Zeeb 		if (isipv6) {
734fb59c426SYoshinobu Inoue 			IP6_EXTHDR_CHECK(m, off0, off, );
735fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
736fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
737b287c6c7SBjoern A. Zeeb 		}
73804d3a452SHajimu UMEMOTO #endif
739b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
740b287c6c7SBjoern A. Zeeb 		else
741b287c6c7SBjoern A. Zeeb #endif
742b287c6c7SBjoern A. Zeeb #ifdef INET
743b287c6c7SBjoern A. Zeeb 		{
744df8bae1dSRodney W. Grimes 			if (m->m_len < sizeof(struct ip) + off) {
745c068736aSJeffrey Hsu 				if ((m = m_pullup(m, sizeof (struct ip) + off))
7464dfdffe9SAndre Oppermann 				    == NULL) {
74778b50714SRobert Watson 					TCPSTAT_INC(tcps_rcvshort);
748df8bae1dSRodney W. Grimes 					return;
749df8bae1dSRodney W. Grimes 				}
750fb59c426SYoshinobu Inoue 				ip = mtod(m, struct ip *);
751fb59c426SYoshinobu Inoue 				th = (struct tcphdr *)((caddr_t)ip + off0);
752fb59c426SYoshinobu Inoue 			}
753df8bae1dSRodney W. Grimes 		}
754b287c6c7SBjoern A. Zeeb #endif
755df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
756fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
757df8bae1dSRodney W. Grimes 	}
758fb59c426SYoshinobu Inoue 	thflags = th->th_flags;
759df8bae1dSRodney W. Grimes 
760e46cd3d4SDag-Erling Smørgrav 	/*
761df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
762df8bae1dSRodney W. Grimes 	 */
7632903309aSAttilio Rao 	tcp_fields_to_host(th);
764df8bae1dSRodney W. Grimes 
765df8bae1dSRodney W. Grimes 	/*
766794235b7SAndre Oppermann 	 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options.
767755c1f07SAndras Olah 	 */
768fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
769755c1f07SAndras Olah 
770755c1f07SAndras Olah 	/*
771fa046d87SRobert Watson 	 * Locate pcb for segment; if we're likely to add or remove a
772fa046d87SRobert Watson 	 * connection then first acquire pcbinfo lock.  There are two cases
773fa046d87SRobert Watson 	 * where we might discover later we need a write lock despite the
774fa046d87SRobert Watson 	 * flags: ACKs moving a connection out of the syncache, and ACKs for
775fa046d87SRobert Watson 	 * a connection in TIMEWAIT.
776df8bae1dSRodney W. Grimes 	 */
777fa046d87SRobert Watson 	if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0) {
778603724d3SBjoern A. Zeeb 		INP_INFO_WLOCK(&V_tcbinfo);
779252ca428SRobert Watson 		ti_locked = TI_WLOCKED;
780fa046d87SRobert Watson 	} else
781fa046d87SRobert Watson 		ti_locked = TI_UNLOCKED;
782252ca428SRobert Watson 
7838d573cc1SAndre Oppermann 	/*
7848d573cc1SAndre Oppermann 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
7858d573cc1SAndre Oppermann 	 */
786b8056faeSGleb Smirnoff         if (
787b8056faeSGleb Smirnoff #ifdef INET6
788b8056faeSGleb Smirnoff 	    (isipv6 && (m->m_flags & M_IP6_NEXTHOP))
789b8056faeSGleb Smirnoff #ifdef INET
790b8056faeSGleb Smirnoff 	    || (!isipv6 && (m->m_flags & M_IP_NEXTHOP))
791b8056faeSGleb Smirnoff #endif
792b8056faeSGleb Smirnoff #endif
793b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6)
794b8056faeSGleb Smirnoff 	    (m->m_flags & M_IP_NEXTHOP)
795b8056faeSGleb Smirnoff #endif
796b8056faeSGleb Smirnoff 	    )
7979b932e9eSAndre Oppermann 		fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
7989b932e9eSAndre Oppermann 
799ce7ad664SEd Maste findpcb:
800ce7ad664SEd Maste #ifdef INVARIANTS
801ce7ad664SEd Maste 	if (ti_locked == TI_WLOCKED) {
802ce7ad664SEd Maste 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
803ce7ad664SEd Maste 	} else {
804ce7ad664SEd Maste 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
805ce7ad664SEd Maste 	}
806ce7ad664SEd Maste #endif
8078a006adbSBjoern A. Zeeb #ifdef INET6
8088a006adbSBjoern A. Zeeb 	if (isipv6 && fwd_tag != NULL) {
8098a006adbSBjoern A. Zeeb 		struct sockaddr_in6 *next_hop6;
8108a006adbSBjoern A. Zeeb 
8118a006adbSBjoern A. Zeeb 		next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
8128a006adbSBjoern A. Zeeb 		/*
8138a006adbSBjoern A. Zeeb 		 * Transparently forwarded. Pretend to be the destination.
8148a006adbSBjoern A. Zeeb 		 * Already got one like this?
8158a006adbSBjoern A. Zeeb 		 */
8168a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo,
8178a006adbSBjoern A. Zeeb 		    &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport,
8188a006adbSBjoern A. Zeeb 		    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m);
8198a006adbSBjoern A. Zeeb 		if (!inp) {
8208a006adbSBjoern A. Zeeb 			/*
8218a006adbSBjoern A. Zeeb 			 * It's new.  Try to find the ambushing socket.
8228a006adbSBjoern A. Zeeb 			 * Because we've rewritten the destination address,
8238a006adbSBjoern A. Zeeb 			 * any hardware-generated hash is ignored.
8248a006adbSBjoern A. Zeeb 			 */
8258a006adbSBjoern A. Zeeb 			inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src,
8268a006adbSBjoern A. Zeeb 			    th->th_sport, &next_hop6->sin6_addr,
8278a006adbSBjoern A. Zeeb 			    next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) :
8288a006adbSBjoern A. Zeeb 			    th->th_dport, INPLOOKUP_WILDCARD |
8298a006adbSBjoern A. Zeeb 			    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
8308a006adbSBjoern A. Zeeb 		}
831c1de64a4SAndrey V. Elsukov 	} else if (isipv6) {
8328a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src,
8338a006adbSBjoern A. Zeeb 		    th->th_sport, &ip6->ip6_dst, th->th_dport,
8348a006adbSBjoern A. Zeeb 		    INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
8358a006adbSBjoern A. Zeeb 		    m->m_pkthdr.rcvif, m);
8368a006adbSBjoern A. Zeeb 	}
8378a006adbSBjoern A. Zeeb #endif /* INET6 */
8388a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET)
8398a006adbSBjoern A. Zeeb 	else
8408a006adbSBjoern A. Zeeb #endif
8418a006adbSBjoern A. Zeeb #ifdef INET
8428a006adbSBjoern A. Zeeb 	if (fwd_tag != NULL) {
8439b932e9eSAndre Oppermann 		struct sockaddr_in *next_hop;
8449b932e9eSAndre Oppermann 
8459b932e9eSAndre Oppermann 		next_hop = (struct sockaddr_in *)(fwd_tag+1);
846f9e354dfSJulian Elischer 		/*
8472b25acc1SLuigi Rizzo 		 * Transparently forwarded. Pretend to be the destination.
848f9e354dfSJulian Elischer 		 * already got one like this?
849f9e354dfSJulian Elischer 		 */
850d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport,
851fa046d87SRobert Watson 		    ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB,
852d3c1f003SRobert Watson 		    m->m_pkthdr.rcvif, m);
853fa046d87SRobert Watson 		if (!inp) {
854fa046d87SRobert Watson 			/*
855fa046d87SRobert Watson 			 * It's new.  Try to find the ambushing socket.
856d3c1f003SRobert Watson 			 * Because we've rewritten the destination address,
857d3c1f003SRobert Watson 			 * any hardware-generated hash is ignored.
858fa046d87SRobert Watson 			 */
859fa046d87SRobert Watson 			inp = in_pcblookup(&V_tcbinfo, ip->ip_src,
860fa046d87SRobert Watson 			    th->th_sport, next_hop->sin_addr,
861fa046d87SRobert Watson 			    next_hop->sin_port ? ntohs(next_hop->sin_port) :
862fa046d87SRobert Watson 			    th->th_dport, INPLOOKUP_WILDCARD |
863fa046d87SRobert Watson 			    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
864f9e354dfSJulian Elischer 		}
865b10fbdeaSAndre Oppermann 	} else
866d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src,
867fa046d87SRobert Watson 		    th->th_sport, ip->ip_dst, th->th_dport,
868fa046d87SRobert Watson 		    INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
869d3c1f003SRobert Watson 		    m->m_pkthdr.rcvif, m);
8708a006adbSBjoern A. Zeeb #endif /* INET */
871f9e354dfSJulian Elischer 
872df8bae1dSRodney W. Grimes 	/*
873574b6964SAndre Oppermann 	 * If the INPCB does not exist then all data in the incoming
874574b6964SAndre Oppermann 	 * segment is discarded and an appropriate RST is sent back.
8758b07e49aSJulian Elischer 	 * XXX MRT Send RST using which routing table?
876df8bae1dSRodney W. Grimes 	 */
877816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
878574b6964SAndre Oppermann 		/*
879574b6964SAndre Oppermann 		 * Log communication attempts to ports that are not
880574b6964SAndre Oppermann 		 * in use.
881574b6964SAndre Oppermann 		 */
882574b6964SAndre Oppermann 		if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) ||
883574b6964SAndre Oppermann 		    tcp_log_in_vain == 2) {
884b7d747ecSAndre Oppermann 			if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6)))
885df541e5fSAndre Oppermann 				log(LOG_INFO, "%s; %s: Connection attempt "
886df541e5fSAndre Oppermann 				    "to closed port\n", s, __func__);
8872e4e1b4cSGeoff Rehmet 		}
888574b6964SAndre Oppermann 		/*
889574b6964SAndre Oppermann 		 * When blackholing do not respond with a RST but
890574b6964SAndre Oppermann 		 * completely ignore the segment and drop it.
891574b6964SAndre Oppermann 		 */
892603724d3SBjoern A. Zeeb 		if ((V_blackhole == 1 && (thflags & TH_SYN)) ||
893603724d3SBjoern A. Zeeb 		    V_blackhole == 2)
8941929eae1SAndre Oppermann 			goto dropunlock;
895574b6964SAndre Oppermann 
896a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
897a57815efSBosko Milekic 		goto dropwithreset;
898816a3d83SPoul-Henning Kamp 	}
899fa046d87SRobert Watson 	INP_WLOCK_ASSERT(inp);
90080cb9f21SKip Macy 	if (!(inp->inp_flags & INP_HW_FLOWID)
90180cb9f21SKip Macy 	    && (m->m_flags & M_FLOWID)
90280cb9f21SKip Macy 	    && ((inp->inp_socket == NULL)
90380cb9f21SKip Macy 		|| !(inp->inp_socket->so_options & SO_ACCEPTCONN))) {
90480cb9f21SKip Macy 		inp->inp_flags |= INP_HW_FLOWID;
90580cb9f21SKip Macy 		inp->inp_flags &= ~INP_SW_FLOWID;
90680cb9f21SKip Macy 		inp->inp_flowid = m->m_pkthdr.flowid;
90780cb9f21SKip Macy 	}
908a2589465SKen Smith #ifdef IPSEC
909a2589465SKen Smith #ifdef INET6
910a2589465SKen Smith 	if (isipv6 && ipsec6_in_reject(m, inp)) {
9116794f460SAndrey V. Elsukov 		IPSEC6STAT_INC(ips_in_polvio);
912a2589465SKen Smith 		goto dropunlock;
913a2589465SKen Smith 	} else
914a2589465SKen Smith #endif /* INET6 */
915a2589465SKen Smith 	if (ipsec4_in_reject(m, inp) != 0) {
9166794f460SAndrey V. Elsukov 		IPSECSTAT_INC(ips_in_polvio);
917a2589465SKen Smith 		goto dropunlock;
918a2589465SKen Smith 	}
919a2589465SKen Smith #endif /* IPSEC */
920a2589465SKen Smith 
9218d573cc1SAndre Oppermann 	/*
9228d573cc1SAndre Oppermann 	 * Check the minimum TTL for socket.
9238d573cc1SAndre Oppermann 	 */
92434f83c52SGeorge V. Neville-Neil 	if (inp->inp_ip_minttl != 0) {
92534f83c52SGeorge V. Neville-Neil #ifdef INET6
92634f83c52SGeorge V. Neville-Neil 		if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim)
927302ce8d6SAndre Oppermann 			goto dropunlock;
92802707462SAndre Oppermann 		else
92934f83c52SGeorge V. Neville-Neil #endif
93034f83c52SGeorge V. Neville-Neil 		if (inp->inp_ip_minttl > ip->ip_ttl)
931302ce8d6SAndre Oppermann 			goto dropunlock;
93234f83c52SGeorge V. Neville-Neil 	}
933936cd18dSAndre Oppermann 
934340c35deSJonathan Lemon 	/*
935252ca428SRobert Watson 	 * A previous connection in TIMEWAIT state is supposed to catch stray
936252ca428SRobert Watson 	 * or duplicate segments arriving late.  If this segment was a
9370989f56cSRobert Watson 	 * legitimate new connection attempt, the old INPCB gets removed and
938252ca428SRobert Watson 	 * we can try again to find a listening socket.
939252ca428SRobert Watson 	 *
940fa046d87SRobert Watson 	 * At this point, due to earlier optimism, we may hold only an inpcb
941fa046d87SRobert Watson 	 * lock, and not the inpcbinfo write lock.  If so, we need to try to
942fa046d87SRobert Watson 	 * acquire it, or if that fails, acquire a reference on the inpcb,
943fa046d87SRobert Watson 	 * drop all locks, acquire a global write lock, and then re-acquire
944fa046d87SRobert Watson 	 * the inpcb lock.  We may at that point discover that another thread
945fa046d87SRobert Watson 	 * has tried to free the inpcb, in which case we need to loop back
946fa046d87SRobert Watson 	 * and try to find a new inpcb to deliver to.
947fa046d87SRobert Watson 	 *
948fa046d87SRobert Watson 	 * XXXRW: It may be time to rethink timewait locking.
949340c35deSJonathan Lemon 	 */
950883e9bc4SRobert Watson relocked:
951ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
952fa046d87SRobert Watson 		if (ti_locked == TI_UNLOCKED) {
953fa046d87SRobert Watson 			if (INP_INFO_TRY_WLOCK(&V_tcbinfo) == 0) {
954252ca428SRobert Watson 				in_pcbref(inp);
955252ca428SRobert Watson 				INP_WUNLOCK(inp);
956252ca428SRobert Watson 				INP_INFO_WLOCK(&V_tcbinfo);
957252ca428SRobert Watson 				ti_locked = TI_WLOCKED;
958252ca428SRobert Watson 				INP_WLOCK(inp);
959fa046d87SRobert Watson 				if (in_pcbrele_wlocked(inp)) {
960252ca428SRobert Watson 					inp = NULL;
961252ca428SRobert Watson 					goto findpcb;
962252ca428SRobert Watson 				}
963f681a5fdSRobert Watson 			} else
964252ca428SRobert Watson 				ti_locked = TI_WLOCKED;
965252ca428SRobert Watson 		}
966252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
967252ca428SRobert Watson 
968340c35deSJonathan Lemon 		if (thflags & TH_SYN)
969f72167f4SAndre Oppermann 			tcp_dooptions(&to, optp, optlen, TO_SYN);
9708d573cc1SAndre Oppermann 		/*
9718d573cc1SAndre Oppermann 		 * NB: tcp_twcheck unlocks the INP and frees the mbuf.
9728d573cc1SAndre Oppermann 		 */
9732104448fSAndre Oppermann 		if (tcp_twcheck(inp, &to, th, m, tlen))
974340c35deSJonathan Lemon 			goto findpcb;
975603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
976340c35deSJonathan Lemon 		return;
977340c35deSJonathan Lemon 	}
978794235b7SAndre Oppermann 	/*
979794235b7SAndre Oppermann 	 * The TCPCB may no longer exist if the connection is winding
980794235b7SAndre Oppermann 	 * down or it is in the CLOSED state.  Either way we drop the
981794235b7SAndre Oppermann 	 * segment and send an appropriate response.
982794235b7SAndre Oppermann 	 */
983df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
984a160e630SAndre Oppermann 	if (tp == NULL || tp->t_state == TCPS_CLOSED) {
985a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
986a57815efSBosko Milekic 		goto dropwithreset;
98709f81a46SBosko Milekic 	}
988df8bae1dSRodney W. Grimes 
98909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
99009fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
99109fe6320SNavdeep Parhar 		tcp_offload_input(tp, m);
99209fe6320SNavdeep Parhar 		m = NULL;	/* consumed by the TOE driver */
99309fe6320SNavdeep Parhar 		goto dropunlock;
99409fe6320SNavdeep Parhar 	}
99509fe6320SNavdeep Parhar #endif
99609fe6320SNavdeep Parhar 
997252ca428SRobert Watson 	/*
998252ca428SRobert Watson 	 * We've identified a valid inpcb, but it could be that we need an
999fa046d87SRobert Watson 	 * inpcbinfo write lock but don't hold it.  In this case, attempt to
1000fa046d87SRobert Watson 	 * acquire using the same strategy as the TIMEWAIT case above.  If we
1001fa046d87SRobert Watson 	 * relock, we have to jump back to 'relocked' as the connection might
1002fa046d87SRobert Watson 	 * now be in TIMEWAIT.
1003252ca428SRobert Watson 	 */
1004fa046d87SRobert Watson #ifdef INVARIANTS
1005fa046d87SRobert Watson 	if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0)
1006fa046d87SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1007fa046d87SRobert Watson #endif
1008fa046d87SRobert Watson 	if (tp->t_state != TCPS_ESTABLISHED) {
1009fa046d87SRobert Watson 		if (ti_locked == TI_UNLOCKED) {
1010fa046d87SRobert Watson 			if (INP_INFO_TRY_WLOCK(&V_tcbinfo) == 0) {
1011252ca428SRobert Watson 				in_pcbref(inp);
1012252ca428SRobert Watson 				INP_WUNLOCK(inp);
1013252ca428SRobert Watson 				INP_INFO_WLOCK(&V_tcbinfo);
1014252ca428SRobert Watson 				ti_locked = TI_WLOCKED;
1015252ca428SRobert Watson 				INP_WLOCK(inp);
1016fa046d87SRobert Watson 				if (in_pcbrele_wlocked(inp)) {
1017252ca428SRobert Watson 					inp = NULL;
1018252ca428SRobert Watson 					goto findpcb;
1019252ca428SRobert Watson 				}
1020883e9bc4SRobert Watson 				goto relocked;
1021f681a5fdSRobert Watson 			} else
1022252ca428SRobert Watson 				ti_locked = TI_WLOCKED;
1023252ca428SRobert Watson 		}
1024252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1025252ca428SRobert Watson 	}
1026252ca428SRobert Watson 
1027c488362eSRobert Watson #ifdef MAC
10288501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
102930d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, m))
1030302ce8d6SAndre Oppermann 		goto dropunlock;
1031c488362eSRobert Watson #endif
1032a557af22SRobert Watson 	so = inp->inp_socket;
1033302ce8d6SAndre Oppermann 	KASSERT(so != NULL, ("%s: so == NULL", __func__));
1034610ee2f9SDavid Greenman #ifdef TCPDEBUG
1035df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG) {
1036df8bae1dSRodney W. Grimes 		ostate = tp->t_state;
103710fe523eSMaxim Konovalov #ifdef INET6
10386f697424SBjoern A. Zeeb 		if (isipv6) {
1039540e8b7eSJeffrey Hsu 			bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6));
1040d30d90dcSMaxim Konovalov 		} else
10416f697424SBjoern A. Zeeb #endif
1042540e8b7eSJeffrey Hsu 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
1043fb59c426SYoshinobu Inoue 		tcp_savetcp = *th;
1044df8bae1dSRodney W. Grimes 	}
1045b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */
1046db33b3e6SAndre Oppermann 	/*
1047db33b3e6SAndre Oppermann 	 * When the socket is accepting connections (the INPCB is in LISTEN
1048db33b3e6SAndre Oppermann 	 * state) we look into the SYN cache if this is a new connection
1049fa046d87SRobert Watson 	 * attempt or the completion of a previous one.  Because listen
1050fa046d87SRobert Watson 	 * sockets are never in TCPS_ESTABLISHED, the V_tcbinfo lock will be
1051fa046d87SRobert Watson 	 * held in this case.
1052db33b3e6SAndre Oppermann 	 */
1053540e8b7eSJeffrey Hsu 	if (so->so_options & SO_ACCEPTCONN) {
1054540e8b7eSJeffrey Hsu 		struct in_conninfo inc;
1055540e8b7eSJeffrey Hsu 
10567824d002SAndre Oppermann 		KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but "
10577824d002SAndre Oppermann 		    "tp not listening", __func__));
1058fa046d87SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
10597824d002SAndre Oppermann 
10608bfb1918SAndre Oppermann 		bzero(&inc, sizeof(inc));
1061302ce8d6SAndre Oppermann #ifdef INET6
1062be2ac88cSJonathan Lemon 		if (isipv6) {
1063dcdb4371SBjoern A. Zeeb 			inc.inc_flags |= INC_ISIPV6;
1064be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
1065be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
1066302ce8d6SAndre Oppermann 		} else
1067302ce8d6SAndre Oppermann #endif
1068302ce8d6SAndre Oppermann 		{
1069be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
1070be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
1071be2ac88cSJonathan Lemon 		}
1072be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
1073be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
10747973fba3SJulian Elischer 		inc.inc_fibnum = so->so_fibnum;
1075be2ac88cSJonathan Lemon 
1076fb59c426SYoshinobu Inoue 		/*
10778d573cc1SAndre Oppermann 		 * Check for an existing connection attempt in syncache if
10788d573cc1SAndre Oppermann 		 * the flag is only ACK.  A successful lookup creates a new
10798d573cc1SAndre Oppermann 		 * socket appended to the listen queue in SYN_RECEIVED state.
1080fb59c426SYoshinobu Inoue 		 */
1081be2ac88cSJonathan Lemon 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
1082bf6d304aSAndre Oppermann 			/*
1083bf6d304aSAndre Oppermann 			 * Parse the TCP options here because
1084bf6d304aSAndre Oppermann 			 * syncookies need access to the reflected
1085bf6d304aSAndre Oppermann 			 * timestamp.
1086bf6d304aSAndre Oppermann 			 */
1087bf6d304aSAndre Oppermann 			tcp_dooptions(&to, optp, optlen, 0);
10889fa198beSAndre Oppermann 			/*
10899fa198beSAndre Oppermann 			 * NB: syncache_expand() doesn't unlock
10909fa198beSAndre Oppermann 			 * inp and tcpinfo locks.
10919fa198beSAndre Oppermann 			 */
1092bf6d304aSAndre Oppermann 			if (!syncache_expand(&inc, &to, th, &so, m)) {
10934195b4afSPaul Traina 				/*
1094e207f800SAndre Oppermann 				 * No syncache entry or ACK was not
1095be2ac88cSJonathan Lemon 				 * for our SYN/ACK.  Send a RST.
10968d573cc1SAndre Oppermann 				 * NB: syncache did its own logging
10978d573cc1SAndre Oppermann 				 * of the failure cause.
10984195b4afSPaul Traina 				 */
1099be2ac88cSJonathan Lemon 				rstreason = BANDLIM_RST_OPENPORT;
1100be2ac88cSJonathan Lemon 				goto dropwithreset;
1101be2ac88cSJonathan Lemon 			}
1102f76fcf6dSJeffrey Hsu 			if (so == NULL) {
1103be2ac88cSJonathan Lemon 				/*
1104e207f800SAndre Oppermann 				 * We completed the 3-way handshake
1105e207f800SAndre Oppermann 				 * but could not allocate a socket
1106e207f800SAndre Oppermann 				 * either due to memory shortage,
1107e207f800SAndre Oppermann 				 * listen queue length limits or
1108a160e630SAndre Oppermann 				 * global socket limits.  Send RST
1109a160e630SAndre Oppermann 				 * or wait and have the remote end
1110a160e630SAndre Oppermann 				 * retransmit the ACK for another
1111a160e630SAndre Oppermann 				 * try.
1112be2ac88cSJonathan Lemon 				 */
1113a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1114a160e630SAndre Oppermann 					log(LOG_DEBUG, "%s; %s: Listen socket: "
11158d573cc1SAndre Oppermann 					    "Socket allocation failed due to "
11168d573cc1SAndre Oppermann 					    "limits or memory shortage, %s\n",
1117ac957cd2SJulian Elischer 					    s, __func__,
1118ac957cd2SJulian Elischer 					    V_tcp_sc_rst_sock_fail ?
1119ac957cd2SJulian Elischer 					    "sending RST" : "try again");
1120603724d3SBjoern A. Zeeb 				if (V_tcp_sc_rst_sock_fail) {
1121e207f800SAndre Oppermann 					rstreason = BANDLIM_UNLIMITED;
1122e207f800SAndre Oppermann 					goto dropwithreset;
1123a160e630SAndre Oppermann 				} else
1124a160e630SAndre Oppermann 					goto dropunlock;
1125f76fcf6dSJeffrey Hsu 			}
1126be2ac88cSJonathan Lemon 			/*
1127be2ac88cSJonathan Lemon 			 * Socket is created in state SYN_RECEIVED.
11288d573cc1SAndre Oppermann 			 * Unlock the listen socket, lock the newly
11298d573cc1SAndre Oppermann 			 * created socket and update the tp variable.
1130be2ac88cSJonathan Lemon 			 */
11318501a69cSRobert Watson 			INP_WUNLOCK(inp);	/* listen socket */
1132be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
11338501a69cSRobert Watson 			INP_WLOCK(inp);		/* new connection */
1134be2ac88cSJonathan Lemon 			tp = intotcpcb(inp);
11358d573cc1SAndre Oppermann 			KASSERT(tp->t_state == TCPS_SYN_RECEIVED,
11368d573cc1SAndre Oppermann 			    ("%s: ", __func__));
11372903309aSAttilio Rao #ifdef TCP_SIGNATURE
11382903309aSAttilio Rao 			if (sig_checked == 0)  {
11392903309aSAttilio Rao 				tcp_dooptions(&to, optp, optlen,
11402903309aSAttilio Rao 				    (thflags & TH_SYN) ? TO_SYN : 0);
11412903309aSAttilio Rao 				if (!tcp_signature_verify_input(m, off0, tlen,
11422903309aSAttilio Rao 				    optlen, &to, th, tp->t_flags)) {
11432903309aSAttilio Rao 
11442903309aSAttilio Rao 					/*
11452903309aSAttilio Rao 					 * In SYN_SENT state if it receives an
11462903309aSAttilio Rao 					 * RST, it is allowed for further
11472903309aSAttilio Rao 					 * processing.
11482903309aSAttilio Rao 					 */
11492903309aSAttilio Rao 					if ((thflags & TH_RST) == 0 ||
11502903309aSAttilio Rao 					    (tp->t_state == TCPS_SYN_SENT) == 0)
11512903309aSAttilio Rao 						goto dropunlock;
11522903309aSAttilio Rao 				}
11532903309aSAttilio Rao 				sig_checked = 1;
11542903309aSAttilio Rao 			}
11552903309aSAttilio Rao #endif
11562903309aSAttilio Rao 
1157be2ac88cSJonathan Lemon 			/*
1158302ce8d6SAndre Oppermann 			 * Process the segment and the data it
1159302ce8d6SAndre Oppermann 			 * contains.  tcp_do_segment() consumes
1160302ce8d6SAndre Oppermann 			 * the mbuf chain and unlocks the inpcb.
1161302ce8d6SAndre Oppermann 			 */
1162f2512ba1SRui Paulo 			tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen,
1163252ca428SRobert Watson 			    iptos, ti_locked);
1164603724d3SBjoern A. Zeeb 			INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1165302ce8d6SAndre Oppermann 			return;
1166be2ac88cSJonathan Lemon 		}
1167a160e630SAndre Oppermann 		/*
11688d573cc1SAndre Oppermann 		 * Segment flag validation for new connection attempts:
11698d573cc1SAndre Oppermann 		 *
1170a160e630SAndre Oppermann 		 * Our (SYN|ACK) response was rejected.
1171a160e630SAndre Oppermann 		 * Check with syncache and remove entry to prevent
1172a160e630SAndre Oppermann 		 * retransmits.
117319bc77c5SAndre Oppermann 		 *
117419bc77c5SAndre Oppermann 		 * NB: syncache_chkrst does its own logging of failure
117519bc77c5SAndre Oppermann 		 * causes.
1176a160e630SAndre Oppermann 		 */
1177a160e630SAndre Oppermann 		if (thflags & TH_RST) {
117819bc77c5SAndre Oppermann 			syncache_chkrst(&inc, th);
1179a160e630SAndre Oppermann 			goto dropunlock;
1180a160e630SAndre Oppermann 		}
1181a160e630SAndre Oppermann 		/*
1182a160e630SAndre Oppermann 		 * We can't do anything without SYN.
1183a160e630SAndre Oppermann 		 */
1184a160e630SAndre Oppermann 		if ((thflags & TH_SYN) == 0) {
1185a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1186a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1187773673c1SAndre Oppermann 				    "SYN is missing, segment ignored\n",
11888d573cc1SAndre Oppermann 				    s, __func__);
118978b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1190a160e630SAndre Oppermann 			goto dropunlock;
1191a160e630SAndre Oppermann 		}
1192a160e630SAndre Oppermann 		/*
1193a160e630SAndre Oppermann 		 * (SYN|ACK) is bogus on a listen socket.
1194a160e630SAndre Oppermann 		 */
1195fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1196a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1197a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
11988d573cc1SAndre Oppermann 				    "SYN|ACK invalid, segment rejected\n",
11998d573cc1SAndre Oppermann 				    s, __func__);
1200a160e630SAndre Oppermann 			syncache_badack(&inc);	/* XXX: Not needed! */
120178b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1202a57815efSBosko Milekic 			rstreason = BANDLIM_RST_OPENPORT;
1203a57815efSBosko Milekic 			goto dropwithreset;
12044195b4afSPaul Traina 		}
1205a160e630SAndre Oppermann 		/*
1206a160e630SAndre Oppermann 		 * If the drop_synfin option is enabled, drop all
1207a160e630SAndre Oppermann 		 * segments with both the SYN and FIN bits set.
1208a160e630SAndre Oppermann 		 * This prevents e.g. nmap from identifying the
1209a160e630SAndre Oppermann 		 * TCP/IP stack.
1210a160e630SAndre Oppermann 		 * XXX: Poor reasoning.  nmap has other methods
1211a160e630SAndre Oppermann 		 * and is constantly refining its stack detection
1212a160e630SAndre Oppermann 		 * strategies.
12138d573cc1SAndre Oppermann 		 * XXX: This is a violation of the TCP specification
1214a160e630SAndre Oppermann 		 * and was used by RFC1644.
1215a160e630SAndre Oppermann 		 */
1216603724d3SBjoern A. Zeeb 		if ((thflags & TH_FIN) && V_drop_synfin) {
1217a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1218a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1219773673c1SAndre Oppermann 				    "SYN|FIN segment ignored (based on "
12208d573cc1SAndre Oppermann 				    "sysctl setting)\n", s, __func__);
122178b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1222302ce8d6SAndre Oppermann 			goto dropunlock;
1223ebb0cbeaSPaul Traina 		}
1224be2ac88cSJonathan Lemon 		/*
1225be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
1226a160e630SAndre Oppermann 		 *
1227a160e630SAndre Oppermann 		 * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored
1228a160e630SAndre Oppermann 		 * as they do not affect the state of the TCP FSM.
1229a160e630SAndre Oppermann 		 * The data pointed to by TH_URG and th_urp is ignored.
1230be2ac88cSJonathan Lemon 		 */
1231a160e630SAndre Oppermann 		KASSERT((thflags & (TH_RST|TH_ACK)) == 0,
1232a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_RST or TH_ACK set", __func__));
1233a160e630SAndre Oppermann 		KASSERT(thflags & (TH_SYN),
1234a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_SYN not set", __func__));
123533841545SHajimu UMEMOTO #ifdef INET6
123633841545SHajimu UMEMOTO 		/*
123733841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
123833841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
123933841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
124033841545SHajimu UMEMOTO 		 * getting established.
124133841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
124233841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
124333841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
124433841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
124533841545SHajimu UMEMOTO 		 * for the exchange.
124633841545SHajimu UMEMOTO 		 *
124733841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
124833841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
124933841545SHajimu UMEMOTO 		 * SYN in this case.
125033841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
125133841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
125233841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
125333841545SHajimu UMEMOTO 		 *    used"
125433841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
125533841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
125633841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
125733841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
125833841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
125933841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
126033841545SHajimu UMEMOTO 		 *
126133841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
126233841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
126333841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
126433841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
126533841545SHajimu UMEMOTO 		 */
1266603724d3SBjoern A. Zeeb 		if (isipv6 && !V_ip6_use_deprecated) {
126733841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
126833841545SHajimu UMEMOTO 
12698c0fec80SRobert Watson 			ia6 = ip6_getdstifaddr(m);
12708c0fec80SRobert Watson 			if (ia6 != NULL &&
127133841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
12728c0fec80SRobert Watson 				ifa_free(&ia6->ia_ifa);
1273a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1274a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12758d573cc1SAndre Oppermann 					"Connection attempt to deprecated "
12768d573cc1SAndre Oppermann 					"IPv6 address rejected\n",
1277a160e630SAndre Oppermann 					s, __func__);
127833841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
127933841545SHajimu UMEMOTO 				goto dropwithreset;
128033841545SHajimu UMEMOTO 			}
128177d396fdSMaksim Yevmenkin 			if (ia6)
12828c0fec80SRobert Watson 				ifa_free(&ia6->ia_ifa);
128333841545SHajimu UMEMOTO 		}
1284b287c6c7SBjoern A. Zeeb #endif /* INET6 */
128565f28919SJesper Skriver 		/*
1286db33b3e6SAndre Oppermann 		 * Basic sanity checks on incoming SYN requests:
12878d573cc1SAndre Oppermann 		 *   Don't respond if the destination is a link layer
1288db33b3e6SAndre Oppermann 		 *	broadcast according to RFC1122 4.2.3.10, p. 104.
12898d573cc1SAndre Oppermann 		 *   If it is from this socket it must be forged.
12908d573cc1SAndre Oppermann 		 *   Don't respond if the source or destination is a
12918d573cc1SAndre Oppermann 		 *	global or subnet broad- or multicast address.
129293ec91baSCrist J. Clark 		 *   Note that it is quite possible to receive unicast
129393ec91baSCrist J. Clark 		 *	link-layer packets with a broadcast IP address. Use
129493ec91baSCrist J. Clark 		 *	in_broadcast() to find them.
1295be2ac88cSJonathan Lemon 		 */
12968d573cc1SAndre Oppermann 		if (m->m_flags & (M_BCAST|M_MCAST)) {
12978d573cc1SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
12988d573cc1SAndre Oppermann 			    log(LOG_DEBUG, "%s; %s: Listen socket: "
12998d573cc1SAndre Oppermann 				"Connection attempt from broad- or multicast "
1300773673c1SAndre Oppermann 				"link layer address ignored\n", s, __func__);
1301302ce8d6SAndre Oppermann 			goto dropunlock;
13028d573cc1SAndre Oppermann 		}
1303db33b3e6SAndre Oppermann #ifdef INET6
1304b287c6c7SBjoern A. Zeeb 		if (isipv6) {
1305db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1306a160e630SAndre Oppermann 			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) {
1307a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1308a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13098d573cc1SAndre Oppermann 					"Connection attempt to/from self "
1310773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1311302ce8d6SAndre Oppermann 				goto dropunlock;
1312a160e630SAndre Oppermann 			}
1313be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1314a160e630SAndre Oppermann 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
1315a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1316a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13178d573cc1SAndre Oppermann 					"Connection attempt from/to multicast "
1318773673c1SAndre Oppermann 					"address ignored\n", s, __func__);
1319302ce8d6SAndre Oppermann 				goto dropunlock;
1320a160e630SAndre Oppermann 			}
1321b287c6c7SBjoern A. Zeeb 		}
1322db33b3e6SAndre Oppermann #endif
1323b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
1324b287c6c7SBjoern A. Zeeb 		else
1325b287c6c7SBjoern A. Zeeb #endif
1326b287c6c7SBjoern A. Zeeb #ifdef INET
1327b287c6c7SBjoern A. Zeeb 		{
1328db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1329a160e630SAndre Oppermann 			    ip->ip_dst.s_addr == ip->ip_src.s_addr) {
1330a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1331a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13328d573cc1SAndre Oppermann 					"Connection attempt from/to self "
1333773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1334302ce8d6SAndre Oppermann 				goto dropunlock;
1335a160e630SAndre Oppermann 			}
1336be2ac88cSJonathan Lemon 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
1337be2ac88cSJonathan Lemon 			    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
13382ca2159fSCrist J. Clark 			    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
1339a160e630SAndre Oppermann 			    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1340a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1341a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13428d573cc1SAndre Oppermann 					"Connection attempt from/to broad- "
1343773673c1SAndre Oppermann 					"or multicast address ignored\n",
1344a160e630SAndre Oppermann 					s, __func__);
1345302ce8d6SAndre Oppermann 				goto dropunlock;
1346c068736aSJeffrey Hsu 			}
1347a160e630SAndre Oppermann 		}
1348b287c6c7SBjoern A. Zeeb #endif
1349be2ac88cSJonathan Lemon 		/*
1350db33b3e6SAndre Oppermann 		 * SYN appears to be valid.  Create compressed TCP state
1351db33b3e6SAndre Oppermann 		 * for syncache.
1352be2ac88cSJonathan Lemon 		 */
13533c653157SHartmut Brandt #ifdef TCPDEBUG
13543c653157SHartmut Brandt 		if (so->so_options & SO_DEBUG)
13553c653157SHartmut Brandt 			tcp_trace(TA_INPUT, ostate, tp,
13563c653157SHartmut Brandt 			    (void *)tcp_saveipgen, &tcp_savetcp, 0);
13573c653157SHartmut Brandt #endif
1358f72167f4SAndre Oppermann 		tcp_dooptions(&to, optp, optlen, TO_SYN);
135909fe6320SNavdeep Parhar 		syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL);
1360be2ac88cSJonathan Lemon 		/*
13614d6e7130SAndre Oppermann 		 * Entry added to syncache and mbuf consumed.
1362a160e630SAndre Oppermann 		 * Everything already unlocked by syncache_add().
1363be2ac88cSJonathan Lemon 		 */
1364603724d3SBjoern A. Zeeb 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1365be2ac88cSJonathan Lemon 		return;
1366982c1675SAndre Oppermann 	} else if (tp->t_state == TCPS_LISTEN) {
1367982c1675SAndre Oppermann 		/*
1368982c1675SAndre Oppermann 		 * When a listen socket is torn down the SO_ACCEPTCONN
1369982c1675SAndre Oppermann 		 * flag is removed first while connections are drained
1370982c1675SAndre Oppermann 		 * from the accept queue in a unlock/lock cycle of the
1371982c1675SAndre Oppermann 		 * ACCEPT_LOCK, opening a race condition allowing a SYN
1372982c1675SAndre Oppermann 		 * attempt go through unhandled.
1373982c1675SAndre Oppermann 		 */
1374982c1675SAndre Oppermann 		goto dropunlock;
1375f76fcf6dSJeffrey Hsu 	}
1376db33b3e6SAndre Oppermann 
13772903309aSAttilio Rao #ifdef TCP_SIGNATURE
13782903309aSAttilio Rao 	if (sig_checked == 0)  {
13792903309aSAttilio Rao 		tcp_dooptions(&to, optp, optlen,
13802903309aSAttilio Rao 		    (thflags & TH_SYN) ? TO_SYN : 0);
13812903309aSAttilio Rao 		if (!tcp_signature_verify_input(m, off0, tlen, optlen, &to,
13822903309aSAttilio Rao 		    th, tp->t_flags)) {
13832903309aSAttilio Rao 
13842903309aSAttilio Rao 			/*
13852903309aSAttilio Rao 			 * In SYN_SENT state if it receives an RST, it is
13862903309aSAttilio Rao 			 * allowed for further processing.
13872903309aSAttilio Rao 			 */
13882903309aSAttilio Rao 			if ((thflags & TH_RST) == 0 ||
13892903309aSAttilio Rao 			    (tp->t_state == TCPS_SYN_SENT) == 0)
13902903309aSAttilio Rao 				goto dropunlock;
13912903309aSAttilio Rao 		}
13922903309aSAttilio Rao 		sig_checked = 1;
13932903309aSAttilio Rao 	}
13942903309aSAttilio Rao #endif
13952903309aSAttilio Rao 
139657f60867SMark Johnston 	TCP_PROBE5(receive, NULL, tp, m->m_data, tp, th);
139757f60867SMark Johnston 
1398302ce8d6SAndre Oppermann 	/*
13998d573cc1SAndre Oppermann 	 * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later
14008d573cc1SAndre Oppermann 	 * state.  tcp_do_segment() always consumes the mbuf chain, unlocks
14018d573cc1SAndre Oppermann 	 * the inpcb, and unlocks pcbinfo.
1402302ce8d6SAndre Oppermann 	 */
1403252ca428SRobert Watson 	tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked);
1404603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1405302ce8d6SAndre Oppermann 	return;
1406be2ac88cSJonathan Lemon 
1407302ce8d6SAndre Oppermann dropwithreset:
140857f60867SMark Johnston 	TCP_PROBE5(receive, NULL, tp, m->m_data, tp, th);
140957f60867SMark Johnston 
1410fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED) {
1411dd8ac7f9SRobert Watson 		INP_INFO_WUNLOCK(&V_tcbinfo);
1412252ca428SRobert Watson 		ti_locked = TI_UNLOCKED;
1413fa046d87SRobert Watson 	}
1414fa046d87SRobert Watson #ifdef INVARIANTS
1415fa046d87SRobert Watson 	else {
1416fa046d87SRobert Watson 		KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropwithreset "
1417fa046d87SRobert Watson 		    "ti_locked: %d", __func__, ti_locked));
1418fa046d87SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1419fa046d87SRobert Watson 	}
1420fa046d87SRobert Watson #endif
1421014ea782SRobert Watson 
1422014ea782SRobert Watson 	if (inp != NULL) {
1423302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
1424014ea782SRobert Watson 		INP_WUNLOCK(inp);
1425dd8ac7f9SRobert Watson 	} else
1426014ea782SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
1427302ce8d6SAndre Oppermann 	m = NULL;	/* mbuf chain got consumed. */
1428014ea782SRobert Watson 	goto drop;
1429014ea782SRobert Watson 
1430302ce8d6SAndre Oppermann dropunlock:
143157f60867SMark Johnston 	if (m != NULL)
143257f60867SMark Johnston 		TCP_PROBE5(receive, NULL, tp, m->m_data, tp, th);
143357f60867SMark Johnston 
1434fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED) {
1435252ca428SRobert Watson 		INP_INFO_WUNLOCK(&V_tcbinfo);
1436252ca428SRobert Watson 		ti_locked = TI_UNLOCKED;
1437fa046d87SRobert Watson 	}
1438fa046d87SRobert Watson #ifdef INVARIANTS
1439fa046d87SRobert Watson 	else {
1440fa046d87SRobert Watson 		KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropunlock "
1441fa046d87SRobert Watson 		    "ti_locked: %d", __func__, ti_locked));
1442fa046d87SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1443fa046d87SRobert Watson 	}
1444fa046d87SRobert Watson #endif
1445252ca428SRobert Watson 
14469fa198beSAndre Oppermann 	if (inp != NULL)
14478501a69cSRobert Watson 		INP_WUNLOCK(inp);
1448014ea782SRobert Watson 
1449302ce8d6SAndre Oppermann drop:
1450603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1451a160e630SAndre Oppermann 	if (s != NULL)
1452a160e630SAndre Oppermann 		free(s, M_TCPLOG);
1453302ce8d6SAndre Oppermann 	if (m != NULL)
1454302ce8d6SAndre Oppermann 		m_freem(m);
1455302ce8d6SAndre Oppermann }
1456302ce8d6SAndre Oppermann 
1457679d9708SAndre Oppermann static void
1458302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
1459252ca428SRobert Watson     struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos,
1460252ca428SRobert Watson     int ti_locked)
1461302ce8d6SAndre Oppermann {
1462302ce8d6SAndre Oppermann 	int thflags, acked, ourfinisacked, needoutput = 0;
1463302ce8d6SAndre Oppermann 	int rstreason, todrop, win;
1464302ce8d6SAndre Oppermann 	u_long tiwin;
146507dacf03SAndre Oppermann 	char *s;
146607dacf03SAndre Oppermann 	struct in_conninfo *inc;
1467c11a15bfSGleb Smirnoff 	struct mbuf *mfree;
1468302ce8d6SAndre Oppermann 	struct tcpopt to;
1469302ce8d6SAndre Oppermann 
147014739780SMaxim Konovalov #ifdef TCPDEBUG
147114739780SMaxim Konovalov 	/*
147214739780SMaxim Konovalov 	 * The size of tcp_saveipgen must be the size of the max ip header,
147314739780SMaxim Konovalov 	 * now IPv6.
147414739780SMaxim Konovalov 	 */
147514739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
147614739780SMaxim Konovalov 	struct tcphdr tcp_savetcp;
147714739780SMaxim Konovalov 	short ostate = 0;
147814739780SMaxim Konovalov #endif
1479302ce8d6SAndre Oppermann 	thflags = th->th_flags;
148007dacf03SAndre Oppermann 	inc = &tp->t_inpcb->inp_inc;
1481d64a46eaSLawrence Stewart 	tp->sackhint.last_sack_ack = 0;
1482302ce8d6SAndre Oppermann 
1483252ca428SRobert Watson 	/*
1484252ca428SRobert Watson 	 * If this is either a state-changing packet or current state isn't
1485252ca428SRobert Watson 	 * established, we require a write lock on tcbinfo.  Otherwise, we
14860989f56cSRobert Watson 	 * allow the tcbinfo to be in either alocked or unlocked, as the
14870989f56cSRobert Watson 	 * caller may have unnecessarily acquired a write lock due to a race.
1488252ca428SRobert Watson 	 */
1489252ca428SRobert Watson 	if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 ||
1490252ca428SRobert Watson 	    tp->t_state != TCPS_ESTABLISHED) {
1491252ca428SRobert Watson 		KASSERT(ti_locked == TI_WLOCKED, ("%s ti_locked %d for "
1492252ca428SRobert Watson 		    "SYN/FIN/RST/!EST", __func__, ti_locked));
1493603724d3SBjoern A. Zeeb 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1494252ca428SRobert Watson 	} else {
1495252ca428SRobert Watson #ifdef INVARIANTS
1496fa046d87SRobert Watson 		if (ti_locked == TI_WLOCKED)
1497252ca428SRobert Watson 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1498fa046d87SRobert Watson 		else {
1499fa046d87SRobert Watson 			KASSERT(ti_locked == TI_UNLOCKED, ("%s: EST "
1500fa046d87SRobert Watson 			    "ti_locked: %d", __func__, ti_locked));
1501fa046d87SRobert Watson 			INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1502fa046d87SRobert Watson 		}
1503252ca428SRobert Watson #endif
1504252ca428SRobert Watson 	}
15058501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1506679d9708SAndre Oppermann 	KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
1507679d9708SAndre Oppermann 	    __func__));
1508679d9708SAndre Oppermann 	KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
1509679d9708SAndre Oppermann 	    __func__));
1510df8bae1dSRodney W. Grimes 
1511df8bae1dSRodney W. Grimes 	/*
1512df8bae1dSRodney W. Grimes 	 * Segment received on connection.
1513df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
1514e8949f74SAndre Oppermann 	 * XXX: This should be done after segment
1515e8949f74SAndre Oppermann 	 * validation to ignore broken/spoofed segs.
1516df8bae1dSRodney W. Grimes 	 */
15179b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
15187ff19458SPaul Traina 	if (TCPS_HAVEESTABLISHED(tp->t_state))
15199077f387SGleb Smirnoff 		tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
1520df8bae1dSRodney W. Grimes 
1521df8bae1dSRodney W. Grimes 	/*
1522464fcfbcSAndre Oppermann 	 * Unscale the window into a 32-bit value.
1523e8949f74SAndre Oppermann 	 * For the SYN_SENT state the scale is zero.
1524464fcfbcSAndre Oppermann 	 */
1525464fcfbcSAndre Oppermann 	tiwin = th->th_win << tp->snd_scale;
1526464fcfbcSAndre Oppermann 
1527464fcfbcSAndre Oppermann 	/*
1528f2512ba1SRui Paulo 	 * TCP ECN processing.
1529f2512ba1SRui Paulo 	 */
1530f2512ba1SRui Paulo 	if (tp->t_flags & TF_ECN_PERMIT) {
15319c251892SRui Paulo 		if (thflags & TH_CWR)
15329c251892SRui Paulo 			tp->t_flags &= ~TF_ECN_SND_ECE;
1533f2512ba1SRui Paulo 		switch (iptos & IPTOS_ECN_MASK) {
1534f2512ba1SRui Paulo 		case IPTOS_ECN_CE:
1535f2512ba1SRui Paulo 			tp->t_flags |= TF_ECN_SND_ECE;
153678b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ce);
1537f2512ba1SRui Paulo 			break;
1538f2512ba1SRui Paulo 		case IPTOS_ECN_ECT0:
153978b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect0);
1540f2512ba1SRui Paulo 			break;
1541f2512ba1SRui Paulo 		case IPTOS_ECN_ECT1:
154278b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect1);
1543f2512ba1SRui Paulo 			break;
1544f2512ba1SRui Paulo 		}
1545dbc42409SLawrence Stewart 		/* Congestion experienced. */
1546dbc42409SLawrence Stewart 		if (thflags & TH_ECE) {
1547dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_ECN);
1548f2512ba1SRui Paulo 		}
1549f2512ba1SRui Paulo 	}
1550f2512ba1SRui Paulo 
1551f2512ba1SRui Paulo 	/*
1552f72167f4SAndre Oppermann 	 * Parse options on any incoming segment.
1553f72167f4SAndre Oppermann 	 */
1554302ce8d6SAndre Oppermann 	tcp_dooptions(&to, (u_char *)(th + 1),
1555302ce8d6SAndre Oppermann 	    (th->th_off << 2) - sizeof(struct tcphdr),
1556302ce8d6SAndre Oppermann 	    (thflags & TH_SYN) ? TO_SYN : 0);
1557f72167f4SAndre Oppermann 
1558f72167f4SAndre Oppermann 	/*
1559f72167f4SAndre Oppermann 	 * If echoed timestamp is later than the current time,
1560bf6d304aSAndre Oppermann 	 * fall back to non RFC1323 RTT calculation.  Normalize
1561bf6d304aSAndre Oppermann 	 * timestamp if syncookies were used when this connection
1562bf6d304aSAndre Oppermann 	 * was established.
1563f72167f4SAndre Oppermann 	 */
1564bf6d304aSAndre Oppermann 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
1565e16fa5caSJohn-Mark Gurney 		to.to_tsecr -= tp->ts_offset;
1566d8951c8aSBjoern A. Zeeb 		if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks()))
1567f72167f4SAndre Oppermann 			to.to_tsecr = 0;
1568bf6d304aSAndre Oppermann 	}
156907dacf03SAndre Oppermann 	/*
157007dacf03SAndre Oppermann 	 * If timestamps were negotiated during SYN/ACK they should
157107dacf03SAndre Oppermann 	 * appear on every segment during this session and vice versa.
157207dacf03SAndre Oppermann 	 */
157307dacf03SAndre Oppermann 	if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) {
157407dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
157507dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: Timestamp missing, "
157607dacf03SAndre Oppermann 			    "no action\n", s, __func__);
157707dacf03SAndre Oppermann 			free(s, M_TCPLOG);
157807dacf03SAndre Oppermann 		}
157907dacf03SAndre Oppermann 	}
158007dacf03SAndre Oppermann 	if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) {
158107dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
158207dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: Timestamp not expected, "
158307dacf03SAndre Oppermann 			    "no action\n", s, __func__);
158407dacf03SAndre Oppermann 			free(s, M_TCPLOG);
158507dacf03SAndre Oppermann 		}
158607dacf03SAndre Oppermann 	}
1587f72167f4SAndre Oppermann 
1588f72167f4SAndre Oppermann 	/*
158997d8d152SAndre Oppermann 	 * Process options only when we get SYN/ACK back. The SYN case
159097d8d152SAndre Oppermann 	 * for incoming connections is handled in tcp_syncache.
15915396d0f8SAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
15925396d0f8SAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.
159397d8d152SAndre Oppermann 	 * XXX this is traditional behavior, may need to be cleaned up.
1594df8bae1dSRodney W. Grimes 	 */
15950a389eabSSimon L. B. Nielsen 	if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
1596464fcfbcSAndre Oppermann 		if ((to.to_flags & TOF_SCALE) &&
1597464fcfbcSAndre Oppermann 		    (tp->t_flags & TF_REQ_SCALE)) {
1598be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
159902a1a643SAndre Oppermann 			tp->snd_scale = to.to_wscale;
1600be2ac88cSJonathan Lemon 		}
16015396d0f8SAndre Oppermann 		/*
16025396d0f8SAndre Oppermann 		 * Initial send window.  It will be updated with
16035396d0f8SAndre Oppermann 		 * the next incoming segment to the scaled value.
16045396d0f8SAndre Oppermann 		 */
16055396d0f8SAndre Oppermann 		tp->snd_wnd = th->th_win;
1606be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_TS) {
1607be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
1608be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
1609d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1610be2ac88cSJonathan Lemon 		}
1611be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_MSS)
1612be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
16133529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
16143529149eSAndre Oppermann 		    (to.to_flags & TOF_SACKPERM) == 0)
16153529149eSAndre Oppermann 			tp->t_flags &= ~TF_SACK_PERMIT;
16166d90faf3SPaul Saab 	}
16176d90faf3SPaul Saab 
1618df8bae1dSRodney W. Grimes 	/*
1619df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
1620df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
1621df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
1622df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
1623df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
1624df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
1625df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
1626df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
1627df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
1628df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
1629df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
1630df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
1631a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
1632c5ad39b9SAndre Oppermann 	 * Since we check for TCPS_ESTABLISHED first, it can only
1633a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
1634df8bae1dSRodney W. Grimes 	 */
1635df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
1636c5ad39b9SAndre Oppermann 	    th->th_seq == tp->rcv_nxt &&
1637fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1638c5ad39b9SAndre Oppermann 	    tp->snd_nxt == tp->snd_max &&
1639c5ad39b9SAndre Oppermann 	    tiwin && tiwin == tp->snd_wnd &&
1640a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
1641c5ad39b9SAndre Oppermann 	    LIST_EMPTY(&tp->t_segq) &&
1642be2ac88cSJonathan Lemon 	    ((to.to_flags & TOF_TS) == 0 ||
1643c5ad39b9SAndre Oppermann 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) {
1644df8bae1dSRodney W. Grimes 
1645df8bae1dSRodney W. Grimes 		/*
1646df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
1647df8bae1dSRodney W. Grimes 		 * record the timestamp.
1648a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
1649a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1650df8bae1dSRodney W. Grimes 		 */
1651be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
1652fb59c426SYoshinobu Inoue 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1653d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1654a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
1655df8bae1dSRodney W. Grimes 		}
1656df8bae1dSRodney W. Grimes 
1657fb59c426SYoshinobu Inoue 		if (tlen == 0) {
1658fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
1659fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
1660dbc42409SLawrence Stewart 			    !IN_RECOVERY(tp->t_flags) &&
1661fc30a251SAndre Oppermann 			    (to.to_flags & TOF_SACK) == 0 &&
1662dbc42409SLawrence Stewart 			    TAILQ_EMPTY(&tp->snd_holes)) {
1663df8bae1dSRodney W. Grimes 				/*
1664e8949f74SAndre Oppermann 				 * This is a pure ack for outstanding data.
1665df8bae1dSRodney W. Grimes 				 */
1666fa046d87SRobert Watson 				if (ti_locked == TI_WLOCKED)
1667252ca428SRobert Watson 					INP_INFO_WUNLOCK(&V_tcbinfo);
1668252ca428SRobert Watson 				ti_locked = TI_UNLOCKED;
1669252ca428SRobert Watson 
167078b50714SRobert Watson 				TCPSTAT_INC(tcps_predack);
1671252ca428SRobert Watson 
16729b8b58e0SJonathan Lemon 				/*
1673e8949f74SAndre Oppermann 				 * "bad retransmit" recovery.
16749b8b58e0SJonathan Lemon 				 */
16759b8b58e0SJonathan Lemon 				if (tp->t_rxtshift == 1 &&
1676672dc4aeSJohn Baldwin 				    tp->t_flags & TF_PREVVALID &&
16776dfb8b31SJohn Baldwin 				    (int)(ticks - tp->t_badrxtwin) < 0) {
1678dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_RTO_ERR);
16799b8b58e0SJonathan Lemon 				}
1680fa55172bSMatthew Dillon 
1681fa55172bSMatthew Dillon 				/*
1682fa55172bSMatthew Dillon 				 * Recalculate the transmit timer / rtt.
1683fa55172bSMatthew Dillon 				 *
1684fa55172bSMatthew Dillon 				 * Some boxes send broken timestamp replies
1685fa55172bSMatthew Dillon 				 * during the SYN+ACK phase, ignore
1686fa55172bSMatthew Dillon 				 * timestamps of 0 or we could calculate a
1687fa55172bSMatthew Dillon 				 * huge RTT and blow up the retransmit timer.
1688fa55172bSMatthew Dillon 				 */
1689fa55172bSMatthew Dillon 				if ((to.to_flags & TOF_TS) != 0 &&
1690fa55172bSMatthew Dillon 				    to.to_tsecr) {
1691d8951c8aSBjoern A. Zeeb 					u_int t;
1692d8951c8aSBjoern A. Zeeb 
1693d8951c8aSBjoern A. Zeeb 					t = tcp_ts_getticks() - to.to_tsecr;
1694d8951c8aSBjoern A. Zeeb 					if (!tp->t_rttlow || tp->t_rttlow > t)
1695d8951c8aSBjoern A. Zeeb 						tp->t_rttlow = t;
1696a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
1697d8951c8aSBjoern A. Zeeb 					    TCP_TS_TO_TICKS(t) + 1);
1698fa55172bSMatthew Dillon 				} else if (tp->t_rtttime &&
1699fa55172bSMatthew Dillon 				    SEQ_GT(th->th_ack, tp->t_rtseq)) {
1700eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1701eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - tp->t_rtttime)
1702eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - tp->t_rtttime;
1703c068736aSJeffrey Hsu 					tcp_xmit_timer(tp,
1704c068736aSJeffrey Hsu 							ticks - tp->t_rtttime);
1705fa55172bSMatthew Dillon 				}
1706dbc42409SLawrence Stewart 				acked = BYTES_THIS_ACK(tp, th);
170739bc9de5SLawrence Stewart 
170839bc9de5SLawrence Stewart 				/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
170939bc9de5SLawrence Stewart 				hhook_run_tcp_est_in(tp, th, &to);
171039bc9de5SLawrence Stewart 
171178b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvackpack);
171278b50714SRobert Watson 				TCPSTAT_ADD(tcps_rcvackbyte, acked);
1713df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
17149d11646dSJeffrey Hsu 				if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
17159d11646dSJeffrey Hsu 				    SEQ_LEQ(th->th_ack, tp->snd_recover))
17169d11646dSJeffrey Hsu 					tp->snd_recover = th->th_ack - 1;
1717dbc42409SLawrence Stewart 
1718dbc42409SLawrence Stewart 				/*
1719dbc42409SLawrence Stewart 				 * Let the congestion control algorithm update
1720dbc42409SLawrence Stewart 				 * congestion control related information. This
1721dbc42409SLawrence Stewart 				 * typically means increasing the congestion
1722dbc42409SLawrence Stewart 				 * window.
1723dbc42409SLawrence Stewart 				 */
1724dbc42409SLawrence Stewart 				cc_ack_received(tp, th, CC_ACK);
1725dbc42409SLawrence Stewart 
17269d11646dSJeffrey Hsu 				tp->snd_una = th->th_ack;
17271645d090SJeff Roberson 				/*
1728e8949f74SAndre Oppermann 				 * Pull snd_wl2 up to prevent seq wrap relative
17291645d090SJeff Roberson 				 * to th_ack.
17301645d090SJeff Roberson 				 */
1731cb942153SJeffrey Hsu 				tp->snd_wl2 = th->th_ack;
1732b5addd85SJeffrey Hsu 				tp->t_dupacks = 0;
1733df8bae1dSRodney W. Grimes 				m_freem(m);
1734e8949f74SAndre Oppermann 				ND6_HINT(tp); /* Some progress has been made. */
1735df8bae1dSRodney W. Grimes 
1736df8bae1dSRodney W. Grimes 				/*
1737df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
1738df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
1739df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
1740df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
1741df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
1742df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
1743df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
1744e8949f74SAndre Oppermann 				 */
17453c653157SHartmut Brandt #ifdef TCPDEBUG
17463c653157SHartmut Brandt 				if (so->so_options & SO_DEBUG)
17473c653157SHartmut Brandt 					tcp_trace(TA_INPUT, ostate, tp,
17483c653157SHartmut Brandt 					    (void *)tcp_saveipgen,
17493c653157SHartmut Brandt 					    &tcp_savetcp, 0);
17503c653157SHartmut Brandt #endif
1751df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
1752b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
1753b8152ba7SAndre Oppermann 				else if (!tcp_timer_active(tp, TT_PERSIST))
1754b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT,
1755b8152ba7SAndre Oppermann 						      tp->t_rxtcur);
1756df8bae1dSRodney W. Grimes 				sowwakeup(so);
1757df8bae1dSRodney W. Grimes 				if (so->so_snd.sb_cc)
1758df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1759a14c749fSJonathan Lemon 				goto check_delack;
1760df8bae1dSRodney W. Grimes 			}
1761fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1762fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
17636741ecf5SAndre Oppermann 			int newsize = 0;	/* automatic sockbuf scaling */
17646741ecf5SAndre Oppermann 
1765df8bae1dSRodney W. Grimes 			/*
1766252ca428SRobert Watson 			 * This is a pure, in-sequence data packet with
1767252ca428SRobert Watson 			 * nothing on the reassembly queue and we have enough
1768252ca428SRobert Watson 			 * buffer space to take it.
1769df8bae1dSRodney W. Grimes 			 */
1770fa046d87SRobert Watson 			if (ti_locked == TI_WLOCKED)
1771252ca428SRobert Watson 				INP_INFO_WUNLOCK(&V_tcbinfo);
1772252ca428SRobert Watson 			ti_locked = TI_UNLOCKED;
1773252ca428SRobert Watson 
17746d90faf3SPaul Saab 			/* Clean receiver SACK report if present */
17753529149eSAndre Oppermann 			if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks)
17766d90faf3SPaul Saab 				tcp_clean_sackreport(tp);
177778b50714SRobert Watson 			TCPSTAT_INC(tcps_preddat);
1778fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
17791645d090SJeff Roberson 			/*
17801645d090SJeff Roberson 			 * Pull snd_wl1 up to prevent seq wrap relative to
17811645d090SJeff Roberson 			 * th_seq.
17821645d090SJeff Roberson 			 */
178360ee3bb2SAndre Oppermann 			tp->snd_wl1 = th->th_seq;
17841645d090SJeff Roberson 			/*
17851645d090SJeff Roberson 			 * Pull rcv_up up to prevent seq wrap relative to
17861645d090SJeff Roberson 			 * rcv_nxt.
17871645d090SJeff Roberson 			 */
17881645d090SJeff Roberson 			tp->rcv_up = tp->rcv_nxt;
178978b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpack);
179078b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
1791e8949f74SAndre Oppermann 			ND6_HINT(tp);	/* Some progress has been made */
17923c653157SHartmut Brandt #ifdef TCPDEBUG
17933c653157SHartmut Brandt 			if (so->so_options & SO_DEBUG)
17943c653157SHartmut Brandt 				tcp_trace(TA_INPUT, ostate, tp,
17953c653157SHartmut Brandt 				    (void *)tcp_saveipgen, &tcp_savetcp, 0);
17963c653157SHartmut Brandt #endif
17976741ecf5SAndre Oppermann 		/*
17986741ecf5SAndre Oppermann 		 * Automatic sizing of receive socket buffer.  Often the send
17996741ecf5SAndre Oppermann 		 * buffer size is not optimally adjusted to the actual network
18006741ecf5SAndre Oppermann 		 * conditions at hand (delay bandwidth product).  Setting the
18016741ecf5SAndre Oppermann 		 * buffer size too small limits throughput on links with high
18026741ecf5SAndre Oppermann 		 * bandwidth and high delay (eg. trans-continental/oceanic links).
18036741ecf5SAndre Oppermann 		 *
18046741ecf5SAndre Oppermann 		 * On the receive side the socket buffer memory is only rarely
18056741ecf5SAndre Oppermann 		 * used to any significant extent.  This allows us to be much
18066741ecf5SAndre Oppermann 		 * more aggressive in scaling the receive socket buffer.  For
18076741ecf5SAndre Oppermann 		 * the case that the buffer space is actually used to a large
18086741ecf5SAndre Oppermann 		 * extent and we run out of kernel memory we can simply drop
18096741ecf5SAndre Oppermann 		 * the new segments; TCP on the sender will just retransmit it
18106741ecf5SAndre Oppermann 		 * later.  Setting the buffer size too big may only consume too
18116741ecf5SAndre Oppermann 		 * much kernel memory if the application doesn't read() from
18126741ecf5SAndre Oppermann 		 * the socket or packet loss or reordering makes use of the
18136741ecf5SAndre Oppermann 		 * reassembly queue.
18146741ecf5SAndre Oppermann 		 *
18156741ecf5SAndre Oppermann 		 * The criteria to step up the receive buffer one notch are:
18166741ecf5SAndre Oppermann 		 *  1. the number of bytes received during the time it takes
18176741ecf5SAndre Oppermann 		 *     one timestamp to be reflected back to us (the RTT);
18186741ecf5SAndre Oppermann 		 *  2. received bytes per RTT is within seven eighth of the
18196741ecf5SAndre Oppermann 		 *     current socket buffer size;
18206741ecf5SAndre Oppermann 		 *  3. receive buffer size has not hit maximal automatic size;
18216741ecf5SAndre Oppermann 		 *
18226741ecf5SAndre Oppermann 		 * This algorithm does one step per RTT at most and only if
18236741ecf5SAndre Oppermann 		 * we receive a bulk stream w/o packet losses or reorderings.
18246741ecf5SAndre Oppermann 		 * Shrinking the buffer during idle times is not necessary as
18256741ecf5SAndre Oppermann 		 * it doesn't consume any memory when idle.
18266741ecf5SAndre Oppermann 		 *
18276741ecf5SAndre Oppermann 		 * TODO: Only step up if the application is actually serving
18286741ecf5SAndre Oppermann 		 * the buffer to better manage the socket buffer resources.
1829df8bae1dSRodney W. Grimes 		 */
1830603724d3SBjoern A. Zeeb 			if (V_tcp_do_autorcvbuf &&
18316741ecf5SAndre Oppermann 			    to.to_tsecr &&
18326741ecf5SAndre Oppermann 			    (so->so_rcv.sb_flags & SB_AUTOSIZE)) {
18338502ec25SAndre Oppermann 				if (TSTMP_GT(to.to_tsecr, tp->rfbuf_ts) &&
18346741ecf5SAndre Oppermann 				    to.to_tsecr - tp->rfbuf_ts < hz) {
18356741ecf5SAndre Oppermann 					if (tp->rfbuf_cnt >
18366741ecf5SAndre Oppermann 					    (so->so_rcv.sb_hiwat / 8 * 7) &&
18376741ecf5SAndre Oppermann 					    so->so_rcv.sb_hiwat <
1838603724d3SBjoern A. Zeeb 					    V_tcp_autorcvbuf_max) {
18396741ecf5SAndre Oppermann 						newsize =
18406741ecf5SAndre Oppermann 						    min(so->so_rcv.sb_hiwat +
1841603724d3SBjoern A. Zeeb 						    V_tcp_autorcvbuf_inc,
1842603724d3SBjoern A. Zeeb 						    V_tcp_autorcvbuf_max);
18436741ecf5SAndre Oppermann 					}
18446741ecf5SAndre Oppermann 					/* Start over with next RTT. */
18456741ecf5SAndre Oppermann 					tp->rfbuf_ts = 0;
18466741ecf5SAndre Oppermann 					tp->rfbuf_cnt = 0;
18476741ecf5SAndre Oppermann 				} else
18486741ecf5SAndre Oppermann 					tp->rfbuf_cnt += tlen;	/* add up */
18496741ecf5SAndre Oppermann 			}
18506741ecf5SAndre Oppermann 
18516741ecf5SAndre Oppermann 			/* Add data to socket buffer. */
18521e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
1853c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1854c1c36a2cSMike Silbersack 				m_freem(m);
1855c1c36a2cSMike Silbersack 			} else {
18566741ecf5SAndre Oppermann 				/*
18576741ecf5SAndre Oppermann 				 * Set new socket buffer size.
18586741ecf5SAndre Oppermann 				 * Give up when limit is reached.
18596741ecf5SAndre Oppermann 				 */
18606741ecf5SAndre Oppermann 				if (newsize)
18616741ecf5SAndre Oppermann 					if (!sbreserve_locked(&so->so_rcv,
18626c8286e4SRobert Watson 					    newsize, so, NULL))
18636741ecf5SAndre Oppermann 						so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1864fb59c426SYoshinobu Inoue 				m_adj(m, drop_hdrlen);	/* delayed header drop */
18651e4d7da7SRobert Watson 				sbappendstream_locked(&so->so_rcv, m);
1866c1c36a2cSMike Silbersack 			}
1867e8949f74SAndre Oppermann 			/* NB: sorwakeup_locked() does an implicit unlock. */
18681e4d7da7SRobert Watson 			sorwakeup_locked(so);
1869*c1e5a6e5SAndre Oppermann 			if (DELAY_ACK(tp, tlen)) {
18703bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
1871f498eeeeSDavid Greenman 			} else {
1872e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
1873e612a582SDavid Greenman 				tcp_output(tp);
1874e612a582SDavid Greenman 			}
1875a14c749fSJonathan Lemon 			goto check_delack;
1876df8bae1dSRodney W. Grimes 		}
1877df8bae1dSRodney W. Grimes 	}
1878df8bae1dSRodney W. Grimes 
1879df8bae1dSRodney W. Grimes 	/*
1880df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1881df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1882df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1883df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1884df8bae1dSRodney W. Grimes 	 */
1885df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1886df8bae1dSRodney W. Grimes 	if (win < 0)
1887df8bae1dSRodney W. Grimes 		win = 0;
188866e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1889df8bae1dSRodney W. Grimes 
18906741ecf5SAndre Oppermann 	/* Reset receive buffer auto scaling when not in bulk receive mode. */
18916741ecf5SAndre Oppermann 	tp->rfbuf_ts = 0;
18926741ecf5SAndre Oppermann 	tp->rfbuf_cnt = 0;
18936741ecf5SAndre Oppermann 
1894df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1895df8bae1dSRodney W. Grimes 
1896df8bae1dSRodney W. Grimes 	/*
1897764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1898764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1899764d8cefSBill Fenner 	 */
1900764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1901fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1902fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1903a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1904a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1905a57815efSBosko Milekic 				goto dropwithreset;
1906a57815efSBosko Milekic 		}
1907764d8cefSBill Fenner 		break;
1908764d8cefSBill Fenner 
1909764d8cefSBill Fenner 	/*
1910df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
1911df8bae1dSRodney W. Grimes 	 *	if seg contains an ACK, but not for our SYN, drop the input.
1912df8bae1dSRodney W. Grimes 	 *	if seg contains a RST, then drop the connection.
1913df8bae1dSRodney W. Grimes 	 *	if seg does not contain SYN, then drop it.
1914df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
1915df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
1916df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
1917f2512ba1SRui Paulo 	 *	if seg contains an ECE and ECN support is enabled, the stream
1918f2512ba1SRui Paulo 	 *	    is ECN capable.
1919df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1920df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
1921df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
1922df8bae1dSRodney W. Grimes 	 */
1923df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
1924fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1925fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
1926fb59c426SYoshinobu Inoue 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1927a57815efSBosko Milekic 			rstreason = BANDLIM_UNLIMITED;
1928a0292f23SGarrett Wollman 			goto dropwithreset;
1929a0292f23SGarrett Wollman 		}
193057f60867SMark Johnston 		if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) {
193157f60867SMark Johnston 			TCP_PROBE5(connect_refused, NULL, tp, m->m_data, tp,
193257f60867SMark Johnston 			    th);
1933df8bae1dSRodney W. Grimes 			tp = tcp_drop(tp, ECONNREFUSED);
193457f60867SMark Johnston 		}
19350ca3f933SAndre Oppermann 		if (thflags & TH_RST)
1936df8bae1dSRodney W. Grimes 			goto drop;
19370ca3f933SAndre Oppermann 		if (!(thflags & TH_SYN))
1938df8bae1dSRodney W. Grimes 			goto drop;
1939464fcfbcSAndre Oppermann 
1940fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
1941df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
1942fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
194378b50714SRobert Watson 			TCPSTAT_INC(tcps_connects);
1944845799c1SAndras Olah 			soisconnected(so);
1945c488362eSRobert Watson #ifdef MAC
194630d239bcSRobert Watson 			mac_socketpeer_set_from_mbuf(m, so);
1947c488362eSRobert Watson #endif
1948845799c1SAndras Olah 			/* Do window scaling on this connection? */
1949845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1950845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1951845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
1952845799c1SAndras Olah 			}
1953766282cbSJohn Baldwin 			tp->rcv_adv += imin(tp->rcv_wnd,
1954766282cbSJohn Baldwin 			    TCP_MAXWIN << tp->rcv_scale);
1955a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
1956a0292f23SGarrett Wollman 			/*
1957a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
1958a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
1959a0292f23SGarrett Wollman 			 */
1960*c1e5a6e5SAndre Oppermann 			if (DELAY_ACK(tp, tlen) && tlen != 0)
1961b8152ba7SAndre Oppermann 				tcp_timer_activate(tp, TT_DELACK,
1962b8152ba7SAndre Oppermann 				    tcp_delacktime);
1963a0292f23SGarrett Wollman 			else
1964a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
1965f2512ba1SRui Paulo 
1966603724d3SBjoern A. Zeeb 			if ((thflags & TH_ECE) && V_tcp_do_ecn) {
1967f2512ba1SRui Paulo 				tp->t_flags |= TF_ECN_PERMIT;
196878b50714SRobert Watson 				TCPSTAT_INC(tcps_ecn_shs);
1969f2512ba1SRui Paulo 			}
1970f2512ba1SRui Paulo 
1971a0292f23SGarrett Wollman 			/*
1972a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
1973a0292f23SGarrett Wollman 			 * Transitions:
1974a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
1975a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
1976a0292f23SGarrett Wollman 			 */
19779b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
1978a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
197957f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_1);
1980a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
1981fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
19827ff19458SPaul Traina 			} else {
198357f60867SMark Johnston 				tcp_state_change(tp, TCPS_ESTABLISHED);
198457f60867SMark Johnston 				TCP_PROBE5(connect_established, NULL, tp,
198557f60867SMark Johnston 				    m->m_data, tp, th);
1986dbc42409SLawrence Stewart 				cc_conn_init(tp);
19879077f387SGleb Smirnoff 				tcp_timer_activate(tp, TT_KEEP,
19889077f387SGleb Smirnoff 				    TP_KEEPIDLE(tp));
19897ff19458SPaul Traina 			}
1990a0292f23SGarrett Wollman 		} else {
1991a0292f23SGarrett Wollman 			/*
1992c068736aSJeffrey Hsu 			 * Received initial SYN in SYN-SENT[*] state =>
1993c068736aSJeffrey Hsu 			 * simultaneous open.  If segment contains CC option
1994c068736aSJeffrey Hsu 			 * and there is a cached CC, apply TAO test.
1995c068736aSJeffrey Hsu 			 * If it succeeds, connection is * half-synchronized.
1996c068736aSJeffrey Hsu 			 * Otherwise, do 3-way handshake:
1997a0292f23SGarrett Wollman 			 *        SYN-SENT -> SYN-RECEIVED
1998a0292f23SGarrett Wollman 			 *        SYN-SENT* -> SYN-RECEIVED*
1999a0292f23SGarrett Wollman 			 * If there was no CC option, clear cached CC value.
2000a0292f23SGarrett Wollman 			 */
20014b8e98d6SQing Li 			tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
2002b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
200357f60867SMark Johnston 			tcp_state_change(tp, TCPS_SYN_RECEIVED);
2004a0292f23SGarrett Wollman 		}
2005df8bae1dSRodney W. Grimes 
2006252ca428SRobert Watson 		KASSERT(ti_locked == TI_WLOCKED, ("%s: trimthenstep6: "
2007252ca428SRobert Watson 		    "ti_locked %d", __func__, ti_locked));
2008252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
20098501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
20107cfc6904SRobert Watson 
2011df8bae1dSRodney W. Grimes 		/*
2012fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
2013df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
2014df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
2015df8bae1dSRodney W. Grimes 		 */
2016fb59c426SYoshinobu Inoue 		th->th_seq++;
2017fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
2018fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
2019df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
2020fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
2021fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
202278b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpackafterwin);
202378b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2024df8bae1dSRodney W. Grimes 		}
2025fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
2026fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
2027a0292f23SGarrett Wollman 		/*
2028a0292f23SGarrett Wollman 		 * Client side of transaction: already sent SYN and data.
2029a0292f23SGarrett Wollman 		 * If the remote host used T/TCP to validate the SYN,
2030a0292f23SGarrett Wollman 		 * our data will be ACK'd; if so, enter normal data segment
2031a0292f23SGarrett Wollman 		 * processing in the middle of step 5, ack processing.
2032a0292f23SGarrett Wollman 		 * Otherwise, goto step 6.
2033a0292f23SGarrett Wollman 		 */
2034fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK)
2035a0292f23SGarrett Wollman 			goto process_ACK;
2036c068736aSJeffrey Hsu 
2037df8bae1dSRodney W. Grimes 		goto step6;
2038c068736aSJeffrey Hsu 
2039a0292f23SGarrett Wollman 	/*
2040a0292f23SGarrett Wollman 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
2041c94c54e4SAndre Oppermann 	 *      do normal processing.
2042a0292f23SGarrett Wollman 	 *
2043c94c54e4SAndre Oppermann 	 * NB: Leftover from RFC1644 T/TCP.  Cases to be reused later.
2044a0292f23SGarrett Wollman 	 */
2045a0292f23SGarrett Wollman 	case TCPS_LAST_ACK:
2046a0292f23SGarrett Wollman 	case TCPS_CLOSING:
2047a0292f23SGarrett Wollman 		break;  /* continue normal processing */
2048df8bae1dSRodney W. Grimes 	}
2049df8bae1dSRodney W. Grimes 
2050df8bae1dSRodney W. Grimes 	/*
2051df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
205280ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
205380ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
205480ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
205580ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
205680ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
205780ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
205880ab7c0eSGarrett Wollman 	 * killing a connection).
205980ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
2060a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
2061df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
2062df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
2063df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
2064df8bae1dSRodney W. Grimes 	 *
206580ab7c0eSGarrett Wollman 	 *
206680ab7c0eSGarrett Wollman 	 * If the RST bit is set, check the sequence number to see
206780ab7c0eSGarrett Wollman 	 * if this is a valid reset segment.
206880ab7c0eSGarrett Wollman 	 * RFC 793 page 37:
206980ab7c0eSGarrett Wollman 	 *   In all states except SYN-SENT, all reset (RST) segments
207080ab7c0eSGarrett Wollman 	 *   are validated by checking their SEQ-fields.  A reset is
207180ab7c0eSGarrett Wollman 	 *   valid if its sequence number is in the window.
207280ab7c0eSGarrett Wollman 	 * Note: this does not take into account delayed ACKs, so
207380ab7c0eSGarrett Wollman 	 *   we should test against last_ack_sent instead of rcv_nxt.
20741a244a61SJonathan Lemon 	 *   The sequence number in the reset segment is normally an
20751a244a61SJonathan Lemon 	 *   echo of our outgoing acknowlegement numbers, but some hosts
20761a244a61SJonathan Lemon 	 *   send a reset with the sequence number at the rightmost edge
20771a244a61SJonathan Lemon 	 *   of our receive window, and we have to handle this case.
207880dd2a81SMike Silbersack 	 * Note 2: Paul Watson's paper "Slipping in the Window" has shown
207980dd2a81SMike Silbersack 	 *   that brute force RST attacks are possible.  To combat this,
208080dd2a81SMike Silbersack 	 *   we use a much stricter check while in the ESTABLISHED state,
208180dd2a81SMike Silbersack 	 *   only accepting RSTs where the sequence number is equal to
208280dd2a81SMike Silbersack 	 *   last_ack_sent.  In all other states (the states in which a
208380dd2a81SMike Silbersack 	 *   RST is more likely), the more permissive check is used.
20848e5c87f4SBjoern A. Zeeb 	 * If we have multiple segments in flight, the initial reset
208580ab7c0eSGarrett Wollman 	 * segment sequence numbers will be to the left of last_ack_sent,
208680ab7c0eSGarrett Wollman 	 * but they will eventually catch up.
208780ab7c0eSGarrett Wollman 	 * In any case, it never made sense to trim reset segments to
208880ab7c0eSGarrett Wollman 	 * fit the receive window since RFC 1122 says:
208980ab7c0eSGarrett Wollman 	 *   4.2.2.12  RST Segment: RFC-793 Section 3.4
209080ab7c0eSGarrett Wollman 	 *
209180ab7c0eSGarrett Wollman 	 *    A TCP SHOULD allow a received RST segment to include data.
209280ab7c0eSGarrett Wollman 	 *
209380ab7c0eSGarrett Wollman 	 *    DISCUSSION
209480ab7c0eSGarrett Wollman 	 *         It has been suggested that a RST segment could contain
209580ab7c0eSGarrett Wollman 	 *         ASCII text that encoded and explained the cause of the
209680ab7c0eSGarrett Wollman 	 *         RST.  No standard has yet been established for such
209780ab7c0eSGarrett Wollman 	 *         data.
209880ab7c0eSGarrett Wollman 	 *
209980ab7c0eSGarrett Wollman 	 * If the reset segment passes the sequence number test examine
210080ab7c0eSGarrett Wollman 	 * the state:
210180ab7c0eSGarrett Wollman 	 *    SYN_RECEIVED STATE:
210280ab7c0eSGarrett Wollman 	 *	If passive open, return to LISTEN state.
210380ab7c0eSGarrett Wollman 	 *	If active open, inform user that connection was refused.
2104745bab7fSDima Dorfman 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
210580ab7c0eSGarrett Wollman 	 *	Inform user that connection was reset, and close tcb.
2106e9bd3a37SJonathan M. Bresler 	 *    CLOSING, LAST_ACK STATES:
210780ab7c0eSGarrett Wollman 	 *	Close the tcb.
2108e9bd3a37SJonathan M. Bresler 	 *    TIME_WAIT STATE:
210980ab7c0eSGarrett Wollman 	 *	Drop the segment - see Stevens, vol. 2, p. 964 and
211080ab7c0eSGarrett Wollman 	 *      RFC 1337.
211180ab7c0eSGarrett Wollman 	 */
2112fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
211395ad8418SQing Li 		if (SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) &&
211495ad8418SQing Li 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
211580ab7c0eSGarrett Wollman 			switch (tp->t_state) {
211680ab7c0eSGarrett Wollman 
211780ab7c0eSGarrett Wollman 			case TCPS_SYN_RECEIVED:
211880ab7c0eSGarrett Wollman 				so->so_error = ECONNREFUSED;
211980ab7c0eSGarrett Wollman 				goto close;
212080ab7c0eSGarrett Wollman 
212180ab7c0eSGarrett Wollman 			case TCPS_ESTABLISHED:
2122603724d3SBjoern A. Zeeb 				if (V_tcp_insecure_rst == 0 &&
212395ad8418SQing Li 				    !(SEQ_GEQ(th->th_seq, tp->rcv_nxt - 1) &&
212495ad8418SQing Li 				    SEQ_LEQ(th->th_seq, tp->rcv_nxt + 1)) &&
212595ad8418SQing Li 				    !(SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) &&
212695ad8418SQing Li 				    SEQ_LEQ(th->th_seq, tp->last_ack_sent + 1))) {
212778b50714SRobert Watson 					TCPSTAT_INC(tcps_badrst);
212880dd2a81SMike Silbersack 					goto drop;
212980dd2a81SMike Silbersack 				}
2130564aab1fSAndre Oppermann 				/* FALLTHROUGH */
213180ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_1:
213280ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_2:
213380ab7c0eSGarrett Wollman 			case TCPS_CLOSE_WAIT:
213480ab7c0eSGarrett Wollman 				so->so_error = ECONNRESET;
213580ab7c0eSGarrett Wollman 			close:
2136252ca428SRobert Watson 				KASSERT(ti_locked == TI_WLOCKED,
2137252ca428SRobert Watson 				    ("tcp_do_segment: TH_RST 1 ti_locked %d",
2138252ca428SRobert Watson 				    ti_locked));
2139252ca428SRobert Watson 				INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2140252ca428SRobert Watson 
214157f60867SMark Johnston 				tcp_state_change(tp, TCPS_CLOSED);
214278b50714SRobert Watson 				TCPSTAT_INC(tcps_drops);
214380ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
214480ab7c0eSGarrett Wollman 				break;
214580ab7c0eSGarrett Wollman 
214680ab7c0eSGarrett Wollman 			case TCPS_CLOSING:
214780ab7c0eSGarrett Wollman 			case TCPS_LAST_ACK:
2148252ca428SRobert Watson 				KASSERT(ti_locked == TI_WLOCKED,
2149252ca428SRobert Watson 				    ("tcp_do_segment: TH_RST 2 ti_locked %d",
2150252ca428SRobert Watson 				    ti_locked));
2151252ca428SRobert Watson 				INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2152252ca428SRobert Watson 
215380ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
215480ab7c0eSGarrett Wollman 				break;
215580ab7c0eSGarrett Wollman 			}
215680ab7c0eSGarrett Wollman 		}
215780ab7c0eSGarrett Wollman 		goto drop;
215880ab7c0eSGarrett Wollman 	}
215980ab7c0eSGarrett Wollman 
216080ab7c0eSGarrett Wollman 	/*
2161df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
2162df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
2163df8bae1dSRodney W. Grimes 	 */
2164be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
216580ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
2166df8bae1dSRodney W. Grimes 
2167df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
2168d8951c8aSBjoern A. Zeeb 		if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
2169df8bae1dSRodney W. Grimes 			/*
2170df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
2171df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
2172df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
2173df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
2174df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
2175df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
2176df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
2177df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
2178df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
2179df8bae1dSRodney W. Grimes 			 */
2180df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
2181df8bae1dSRodney W. Grimes 		} else {
218278b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
218378b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, tlen);
218478b50714SRobert Watson 			TCPSTAT_INC(tcps_pawsdrop);
218507fd333dSMatthew Dillon 			if (tlen)
2186df8bae1dSRodney W. Grimes 				goto dropafterack;
21871ab4789dSMatthew Dillon 			goto drop;
21881ab4789dSMatthew Dillon 		}
2189df8bae1dSRodney W. Grimes 	}
2190df8bae1dSRodney W. Grimes 
2191a0292f23SGarrett Wollman 	/*
219280ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
219380ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
219480ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
219580ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
219680ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
219780ab7c0eSGarrett Wollman 	 */
2198a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
2199a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2200a57815efSBosko Milekic 		goto dropwithreset;
2201a57815efSBosko Milekic 	}
220280ab7c0eSGarrett Wollman 
2203fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
2204df8bae1dSRodney W. Grimes 	if (todrop > 0) {
220581ad7eb0SZachary Loafman 		/*
220681ad7eb0SZachary Loafman 		 * If this is a duplicate SYN for our current connection,
220781ad7eb0SZachary Loafman 		 * advance over it and pretend and it's not a SYN.
220881ad7eb0SZachary Loafman 		 */
220981ad7eb0SZachary Loafman 		if (thflags & TH_SYN && th->th_seq == tp->irs) {
2210fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
2211fb59c426SYoshinobu Inoue 			th->th_seq++;
2212fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
2213fb59c426SYoshinobu Inoue 				th->th_urp--;
2214df8bae1dSRodney W. Grimes 			else
2215fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
2216df8bae1dSRodney W. Grimes 			todrop--;
2217df8bae1dSRodney W. Grimes 		}
2218df8bae1dSRodney W. Grimes 		/*
2219dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
2220df8bae1dSRodney W. Grimes 		 */
2221fb59c426SYoshinobu Inoue 		if (todrop > tlen
2222fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
2223dac20301SGarrett Wollman 			/*
2224dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
2225dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
2226dac20301SGarrett Wollman 			 * of sequence; drop it.
2227dac20301SGarrett Wollman 			 */
2228fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
2229dac20301SGarrett Wollman 
2230df8bae1dSRodney W. Grimes 			/*
2231dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
2232dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
2233df8bae1dSRodney W. Grimes 			 */
2234dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
2235fb59c426SYoshinobu Inoue 			todrop = tlen;
223678b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
223778b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, todrop);
2238df8bae1dSRodney W. Grimes 		} else {
223978b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpartduppack);
224078b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop);
2241df8bae1dSRodney W. Grimes 		}
2242fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
2243fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
2244fb59c426SYoshinobu Inoue 		tlen -= todrop;
2245fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
2246fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
2247df8bae1dSRodney W. Grimes 		else {
2248fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
2249fb59c426SYoshinobu Inoue 			th->th_urp = 0;
2250df8bae1dSRodney W. Grimes 		}
2251df8bae1dSRodney W. Grimes 	}
2252df8bae1dSRodney W. Grimes 
2253df8bae1dSRodney W. Grimes 	/*
2254df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
2255df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
2256df8bae1dSRodney W. Grimes 	 */
2257df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
2258fb59c426SYoshinobu Inoue 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
2259252ca428SRobert Watson 		KASSERT(ti_locked == TI_WLOCKED, ("%s: SS_NOFDEREF && "
2260252ca428SRobert Watson 		    "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked));
2261252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2262252ca428SRobert Watson 
226307dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
226407dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data "
226507dacf03SAndre Oppermann 			    "after socket was closed, "
226607dacf03SAndre Oppermann 			    "sending RST and removing tcpcb\n",
2267e31d8aa3SMike Silbersack 			    s, __func__, tcpstates[tp->t_state], tlen);
2268773673c1SAndre Oppermann 			free(s, M_TCPLOG);
2269773673c1SAndre Oppermann 		}
2270df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
227178b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvafterclose);
2272a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
2273df8bae1dSRodney W. Grimes 		goto dropwithreset;
2274df8bae1dSRodney W. Grimes 	}
2275df8bae1dSRodney W. Grimes 
2276df8bae1dSRodney W. Grimes 	/*
2277df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
2278df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
2279df8bae1dSRodney W. Grimes 	 */
2280fb59c426SYoshinobu Inoue 	todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
2281df8bae1dSRodney W. Grimes 	if (todrop > 0) {
228278b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvpackafterwin);
2283fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
228478b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen);
2285df8bae1dSRodney W. Grimes 			/*
2286df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
2287df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
2288df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
2289df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
2290df8bae1dSRodney W. Grimes 			 * and ack.
2291df8bae1dSRodney W. Grimes 			 */
2292fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
2293df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
229478b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvwinprobe);
2295df8bae1dSRodney W. Grimes 			} else
2296df8bae1dSRodney W. Grimes 				goto dropafterack;
2297df8bae1dSRodney W. Grimes 		} else
229878b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2299df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
2300fb59c426SYoshinobu Inoue 		tlen -= todrop;
2301fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
2302df8bae1dSRodney W. Grimes 	}
2303df8bae1dSRodney W. Grimes 
2304df8bae1dSRodney W. Grimes 	/*
2305df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
2306df8bae1dSRodney W. Grimes 	 * record its timestamp.
2307cf09195bSPaul Saab 	 * NOTE:
2308cf09195bSPaul Saab 	 * 1) That the test incorporates suggestions from the latest
2309a0292f23SGarrett Wollman 	 *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
2310cf09195bSPaul Saab 	 * 2) That updating only on newer timestamps interferes with
2311cf09195bSPaul Saab 	 *    our earlier PAWS tests, so this check should be solely
2312cf09195bSPaul Saab 	 *    predicated on the sequence space of this segment.
2313cf09195bSPaul Saab 	 * 3) That we modify the segment boundary check to be
2314cf09195bSPaul Saab 	 *        Last.ACK.Sent <= SEG.SEQ + SEG.Len
2315cf09195bSPaul Saab 	 *    instead of RFC1323's
2316cf09195bSPaul Saab 	 *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
2317cf09195bSPaul Saab 	 *    This modified check allows us to overcome RFC1323's
2318cf09195bSPaul Saab 	 *    limitations as described in Stevens TCP/IP Illustrated
2319cf09195bSPaul Saab 	 *    Vol. 2 p.869. In such cases, we can still calculate the
2320cf09195bSPaul Saab 	 *    RTT correctly when RCV.NXT == Last.ACK.Sent.
2321df8bae1dSRodney W. Grimes 	 */
2322be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
2323cf09195bSPaul Saab 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
2324cf09195bSPaul Saab 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
2325cf09195bSPaul Saab 		((thflags & (TH_SYN|TH_FIN)) != 0))) {
2326d8951c8aSBjoern A. Zeeb 		tp->ts_recent_age = tcp_ts_getticks();
2327a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
2328df8bae1dSRodney W. Grimes 	}
2329df8bae1dSRodney W. Grimes 
2330df8bae1dSRodney W. Grimes 	/*
2331df8bae1dSRodney W. Grimes 	 * If a SYN is in the window, then this is an
2332df8bae1dSRodney W. Grimes 	 * error and we send an RST and drop the connection.
2333df8bae1dSRodney W. Grimes 	 */
2334fb59c426SYoshinobu Inoue 	if (thflags & TH_SYN) {
2335252ca428SRobert Watson 		KASSERT(ti_locked == TI_WLOCKED,
2336252ca428SRobert Watson 		    ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked));
2337252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2338252ca428SRobert Watson 
2339df8bae1dSRodney W. Grimes 		tp = tcp_drop(tp, ECONNRESET);
2340a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
2341122aad88SAndre Oppermann 		goto drop;
2342df8bae1dSRodney W. Grimes 	}
2343df8bae1dSRodney W. Grimes 
2344a0292f23SGarrett Wollman 	/*
2345a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
2346a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
2347a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
2348a0292f23SGarrett Wollman 	 */
2349fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
2350a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
2351a0292f23SGarrett Wollman 		    (tp->t_flags & TF_NEEDSYN))
2352a0292f23SGarrett Wollman 			goto step6;
23535e1aa279SDavid Malone 		else if (tp->t_flags & TF_ACKNOW)
23545e1aa279SDavid Malone 			goto dropafterack;
2355a0292f23SGarrett Wollman 		else
2356a0292f23SGarrett Wollman 			goto drop;
2357a0292f23SGarrett Wollman 	}
2358df8bae1dSRodney W. Grimes 
2359df8bae1dSRodney W. Grimes 	/*
2360df8bae1dSRodney W. Grimes 	 * Ack processing.
2361df8bae1dSRodney W. Grimes 	 */
2362df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2363df8bae1dSRodney W. Grimes 
2364df8bae1dSRodney W. Grimes 	/*
2365764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
2366764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
2367764d8cefSBill Fenner 	 * The ACK was checked above.
2368df8bae1dSRodney W. Grimes 	 */
2369df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2370a0292f23SGarrett Wollman 
237178b50714SRobert Watson 		TCPSTAT_INC(tcps_connects);
2372df8bae1dSRodney W. Grimes 		soisconnected(so);
2373df8bae1dSRodney W. Grimes 		/* Do window scaling? */
2374df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2375df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2376df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
2377464fcfbcSAndre Oppermann 			tp->snd_wnd = tiwin;
2378df8bae1dSRodney W. Grimes 		}
2379a0292f23SGarrett Wollman 		/*
2380a0292f23SGarrett Wollman 		 * Make transitions:
2381a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
2382a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
2383a0292f23SGarrett Wollman 		 */
23849b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
2385a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDFIN) {
238657f60867SMark Johnston 			tcp_state_change(tp, TCPS_FIN_WAIT_1);
2387a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDFIN;
23887ff19458SPaul Traina 		} else {
238957f60867SMark Johnston 			tcp_state_change(tp, TCPS_ESTABLISHED);
239057f60867SMark Johnston 			TCP_PROBE5(accept_established, NULL, tp, m->m_data, tp,
239157f60867SMark Johnston 			    th);
2392dbc42409SLawrence Stewart 			cc_conn_init(tp);
23939077f387SGleb Smirnoff 			tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
23947ff19458SPaul Traina 		}
2395a0292f23SGarrett Wollman 		/*
2396a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
2397a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
2398a0292f23SGarrett Wollman 		 */
2399fb59c426SYoshinobu Inoue 		if (tlen == 0 && (thflags & TH_FIN) == 0)
2400fb59c426SYoshinobu Inoue 			(void) tcp_reass(tp, (struct tcphdr *)0, 0,
2401a0292f23SGarrett Wollman 			    (struct mbuf *)0);
240260ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq - 1;
240393b0017fSPhilippe Charnier 		/* FALLTHROUGH */
2404df8bae1dSRodney W. Grimes 
2405df8bae1dSRodney W. Grimes 	/*
2406df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2407df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
2408fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
2409fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
2410df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
2411df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
2412df8bae1dSRodney W. Grimes 	 */
2413df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
2414df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
2415df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
2416df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
2417df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
2418df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
24195a53ca16SPaul Saab 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
242078b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvacktoomuch);
24215a53ca16SPaul Saab 			goto dropafterack;
24225a53ca16SPaul Saab 		}
24233529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
2424fc30a251SAndre Oppermann 		    ((to.to_flags & TOF_SACK) ||
2425fc30a251SAndre Oppermann 		     !TAILQ_EMPTY(&tp->snd_holes)))
24265a53ca16SPaul Saab 			tcp_sack_doack(tp, &to, th->th_ack);
242739bc9de5SLawrence Stewart 
242839bc9de5SLawrence Stewart 		/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
242939bc9de5SLawrence Stewart 		hhook_run_tcp_est_in(tp, th, &to);
243039bc9de5SLawrence Stewart 
2431fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
2432fb59c426SYoshinobu Inoue 			if (tlen == 0 && tiwin == tp->snd_wnd) {
243378b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvdupack);
2434df8bae1dSRodney W. Grimes 				/*
2435df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
2436df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
2437df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
2438df8bae1dSRodney W. Grimes 				 * change), the ack is the biggest we've
2439df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
2440df8bae1dSRodney W. Grimes 				 * threshhold of them, assume a packet
2441df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
2442df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
2443df8bae1dSRodney W. Grimes 				 * window so we send only this one
2444df8bae1dSRodney W. Grimes 				 * packet.
2445df8bae1dSRodney W. Grimes 				 *
2446df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
2447df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
2448df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
2449df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
2450df8bae1dSRodney W. Grimes 				 * the new ssthresh).
2451df8bae1dSRodney W. Grimes 				 *
2452df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
2453df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
2454df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
2455df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
2456df8bae1dSRodney W. Grimes 				 * network.
2457f2512ba1SRui Paulo 				 *
2458f2512ba1SRui Paulo 				 * When using TCP ECN, notify the peer that
2459f2512ba1SRui Paulo 				 * we reduced the cwnd.
2460df8bae1dSRodney W. Grimes 				 */
2461b8152ba7SAndre Oppermann 				if (!tcp_timer_active(tp, TT_REXMT) ||
2462fb59c426SYoshinobu Inoue 				    th->th_ack != tp->snd_una)
2463df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
2464cb942153SJeffrey Hsu 				else if (++tp->t_dupacks > tcprexmtthresh ||
2465dbc42409SLawrence Stewart 				     IN_FASTRECOVERY(tp->t_flags)) {
2466dbc42409SLawrence Stewart 					cc_ack_received(tp, th, CC_DUPACK);
24673529149eSAndre Oppermann 					if ((tp->t_flags & TF_SACK_PERMIT) &&
2468dbc42409SLawrence Stewart 					    IN_FASTRECOVERY(tp->t_flags)) {
2469808f11b7SPaul Saab 						int awnd;
247025e6f9edSPaul Saab 
247125e6f9edSPaul Saab 						/*
247225e6f9edSPaul Saab 						 * Compute the amount of data in flight first.
247325e6f9edSPaul Saab 						 * We can inject new data into the pipe iff
247425e6f9edSPaul Saab 						 * we have less than 1/2 the original window's
247525e6f9edSPaul Saab 						 * worth of data in flight.
247625e6f9edSPaul Saab 						 */
2477808f11b7SPaul Saab 						awnd = (tp->snd_nxt - tp->snd_fack) +
24780077b016SPaul Saab 							tp->sackhint.sack_bytes_rexmit;
2479808f11b7SPaul Saab 						if (awnd < tp->snd_ssthresh) {
248025e6f9edSPaul Saab 							tp->snd_cwnd += tp->t_maxseg;
248125e6f9edSPaul Saab 							if (tp->snd_cwnd > tp->snd_ssthresh)
248225e6f9edSPaul Saab 								tp->snd_cwnd = tp->snd_ssthresh;
248325e6f9edSPaul Saab 						}
248425e6f9edSPaul Saab 					} else
248546f58482SJonathan Lemon 						tp->snd_cwnd += tp->t_maxseg;
2486ec03d543SRandall Stewart 					if ((thflags & TH_FIN) &&
2487ec03d543SRandall Stewart 					    (TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
2488ec03d543SRandall Stewart 						/*
2489ec03d543SRandall Stewart 						 * If its a fin we need to process
2490ec03d543SRandall Stewart 						 * it to avoid a race where both
2491ec03d543SRandall Stewart 						 * sides enter FIN-WAIT and send FIN|ACK
2492ec03d543SRandall Stewart 						 * at the same time.
2493ec03d543SRandall Stewart 						 */
2494ec03d543SRandall Stewart 						break;
2495ec03d543SRandall Stewart 					}
249646f58482SJonathan Lemon 					(void) tcp_output(tp);
249746f58482SJonathan Lemon 					goto drop;
2498cb942153SJeffrey Hsu 				} else if (tp->t_dupacks == tcprexmtthresh) {
2499cb942153SJeffrey Hsu 					tcp_seq onxt = tp->snd_nxt;
2500a0445c2eSJayanth Vijayaraghavan 
2501a0445c2eSJayanth Vijayaraghavan 					/*
2502a0445c2eSJayanth Vijayaraghavan 					 * If we're doing sack, check to
2503a0445c2eSJayanth Vijayaraghavan 					 * see if we're already in sack
2504a0445c2eSJayanth Vijayaraghavan 					 * recovery. If we're not doing sack,
2505a0445c2eSJayanth Vijayaraghavan 					 * check to see if we're in newreno
2506a0445c2eSJayanth Vijayaraghavan 					 * recovery.
2507a0445c2eSJayanth Vijayaraghavan 					 */
25083529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
2509dbc42409SLawrence Stewart 						if (IN_FASTRECOVERY(tp->t_flags)) {
2510a0445c2eSJayanth Vijayaraghavan 							tp->t_dupacks = 0;
2511a0445c2eSJayanth Vijayaraghavan 							break;
2512a0445c2eSJayanth Vijayaraghavan 						}
2513dbc42409SLawrence Stewart 					} else {
2514a0445c2eSJayanth Vijayaraghavan 						if (SEQ_LEQ(th->th_ack,
25159d11646dSJeffrey Hsu 						    tp->snd_recover)) {
2516cb942153SJeffrey Hsu 							tp->t_dupacks = 0;
2517cb942153SJeffrey Hsu 							break;
251846f58482SJonathan Lemon 						}
2519a0445c2eSJayanth Vijayaraghavan 					}
2520dbc42409SLawrence Stewart 					/* Congestion signal before ack. */
2521dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_NDUPACK);
2522dbc42409SLawrence Stewart 					cc_ack_received(tp, th, CC_DUPACK);
2523b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
25249b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
25253529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
252678b50714SRobert Watson 						TCPSTAT_INC(
252778b50714SRobert Watson 						    tcps_sack_recovery_episode);
2528a55db2b6SPaul Saab 						tp->sack_newdata = tp->snd_nxt;
25298db456bfSPaul Saab 						tp->snd_cwnd = tp->t_maxseg;
25306d90faf3SPaul Saab 						(void) tcp_output(tp);
25316d90faf3SPaul Saab 						goto drop;
25326d90faf3SPaul Saab 					}
2533fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
2534df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->t_maxseg;
2535ec03d543SRandall Stewart 					if ((thflags & TH_FIN) &&
2536ec03d543SRandall Stewart 					    (TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
2537ec03d543SRandall Stewart 						/*
2538ec03d543SRandall Stewart 						 * If its a fin we need to process
2539ec03d543SRandall Stewart 						 * it to avoid a race where both
2540ec03d543SRandall Stewart 						 * sides enter FIN-WAIT and send FIN|ACK
2541ec03d543SRandall Stewart 						 * at the same time.
2542ec03d543SRandall Stewart 						 */
2543ec03d543SRandall Stewart 						break;
2544ec03d543SRandall Stewart 					}
2545df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
254648d2549cSJeffrey Hsu 					KASSERT(tp->snd_limited <= 2,
2547302ce8d6SAndre Oppermann 					    ("%s: tp->snd_limited too big",
2548302ce8d6SAndre Oppermann 					    __func__));
2549df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
255048d2549cSJeffrey Hsu 					     tp->t_maxseg *
255148d2549cSJeffrey Hsu 					     (tp->t_dupacks - tp->snd_limited);
2552df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
2553df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
2554df8bae1dSRodney W. Grimes 					goto drop;
2555603724d3SBjoern A. Zeeb 				} else if (V_tcp_do_rfc3042) {
2556dbc42409SLawrence Stewart 					cc_ack_received(tp, th, CC_DUPACK);
2557582a954bSJeffrey Hsu 					u_long oldcwnd = tp->snd_cwnd;
255848d2549cSJeffrey Hsu 					tcp_seq oldsndmax = tp->snd_max;
255948d2549cSJeffrey Hsu 					u_int sent;
25605628dd08SAndre Oppermann 					int avail;
256189c02376SJeffrey Hsu 
2562582a954bSJeffrey Hsu 					KASSERT(tp->t_dupacks == 1 ||
2563582a954bSJeffrey Hsu 					    tp->t_dupacks == 2,
2564302ce8d6SAndre Oppermann 					    ("%s: dupacks not 1 or 2",
2565302ce8d6SAndre Oppermann 					    __func__));
256661a36e3dSJeffrey Hsu 					if (tp->t_dupacks == 1)
256748d2549cSJeffrey Hsu 						tp->snd_limited = 0;
256861a36e3dSJeffrey Hsu 					tp->snd_cwnd =
256961a36e3dSJeffrey Hsu 					    (tp->snd_nxt - tp->snd_una) +
257061a36e3dSJeffrey Hsu 					    (tp->t_dupacks - tp->snd_limited) *
257161a36e3dSJeffrey Hsu 					    tp->t_maxseg;
2572ec03d543SRandall Stewart 					if ((thflags & TH_FIN) &&
2573ec03d543SRandall Stewart 					    (TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
2574ec03d543SRandall Stewart 						/*
2575ec03d543SRandall Stewart 						 * If its a fin we need to process
2576ec03d543SRandall Stewart 						 * it to avoid a race where both
2577ec03d543SRandall Stewart 						 * sides enter FIN-WAIT and send FIN|ACK
2578ec03d543SRandall Stewart 						 * at the same time.
2579ec03d543SRandall Stewart 						 */
2580ec03d543SRandall Stewart 						break;
2581ec03d543SRandall Stewart 					}
25825628dd08SAndre Oppermann 					/*
25835628dd08SAndre Oppermann 					 * Only call tcp_output when there
25845628dd08SAndre Oppermann 					 * is new data available to be sent.
25855628dd08SAndre Oppermann 					 * Otherwise we would send pure ACKs.
25865628dd08SAndre Oppermann 					 */
25875628dd08SAndre Oppermann 					SOCKBUF_LOCK(&so->so_snd);
25885628dd08SAndre Oppermann 					avail = so->so_snd.sb_cc -
25895628dd08SAndre Oppermann 					    (tp->snd_nxt - tp->snd_una);
25905628dd08SAndre Oppermann 					SOCKBUF_UNLOCK(&so->so_snd);
25915628dd08SAndre Oppermann 					if (avail > 0)
2592582a954bSJeffrey Hsu 						(void) tcp_output(tp);
259348d2549cSJeffrey Hsu 					sent = tp->snd_max - oldsndmax;
259448d2549cSJeffrey Hsu 					if (sent > tp->t_maxseg) {
259589c02376SJeffrey Hsu 						KASSERT((tp->t_dupacks == 2 &&
259689c02376SJeffrey Hsu 						    tp->snd_limited == 0) ||
259789c02376SJeffrey Hsu 						   (sent == tp->t_maxseg + 1 &&
259889c02376SJeffrey Hsu 						    tp->t_flags & TF_SENTFIN),
2599302ce8d6SAndre Oppermann 						    ("%s: sent too much",
2600302ce8d6SAndre Oppermann 						    __func__));
260148d2549cSJeffrey Hsu 						tp->snd_limited = 2;
260248d2549cSJeffrey Hsu 					} else if (sent > 0)
260348d2549cSJeffrey Hsu 						++tp->snd_limited;
2604582a954bSJeffrey Hsu 					tp->snd_cwnd = oldcwnd;
2605582a954bSJeffrey Hsu 					goto drop;
2606df8bae1dSRodney W. Grimes 				}
2607df8bae1dSRodney W. Grimes 			} else
2608df8bae1dSRodney W. Grimes 				tp->t_dupacks = 0;
2609df8bae1dSRodney W. Grimes 			break;
2610df8bae1dSRodney W. Grimes 		}
2611cb942153SJeffrey Hsu 
2612302ce8d6SAndre Oppermann 		KASSERT(SEQ_GT(th->th_ack, tp->snd_una),
2613302ce8d6SAndre Oppermann 		    ("%s: th_ack <= snd_una", __func__));
2614cb942153SJeffrey Hsu 
2615df8bae1dSRodney W. Grimes 		/*
2616df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
2617df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
2618df8bae1dSRodney W. Grimes 		 */
2619dbc42409SLawrence Stewart 		if (IN_FASTRECOVERY(tp->t_flags)) {
2620cb942153SJeffrey Hsu 			if (SEQ_LT(th->th_ack, tp->snd_recover)) {
26213529149eSAndre Oppermann 				if (tp->t_flags & TF_SACK_PERMIT)
26226d90faf3SPaul Saab 					tcp_sack_partialack(tp, th);
26236d90faf3SPaul Saab 				else
2624c068736aSJeffrey Hsu 					tcp_newreno_partial_ack(tp, th);
2625dbc42409SLawrence Stewart 			} else
2626dbc42409SLawrence Stewart 				cc_post_recovery(tp, th);
262746f58482SJonathan Lemon 		}
2628cb942153SJeffrey Hsu 		tp->t_dupacks = 0;
2629a0292f23SGarrett Wollman 		/*
2630a0292f23SGarrett Wollman 		 * If we reach this point, ACK is not a duplicate,
2631a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
2632a0292f23SGarrett Wollman 		 */
2633a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
2634a0292f23SGarrett Wollman 			/*
2635a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
2636a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
263707e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
263807e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
263907e43e10SAndras Olah 			 * we can do window scaling.
2640a0292f23SGarrett Wollman 			 */
2641a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
2642a0292f23SGarrett Wollman 			tp->snd_una++;
264307e43e10SAndras Olah 			/* Do window scaling? */
264407e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
264507e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
264607e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2647464fcfbcSAndre Oppermann 				/* Send window already scaled. */
264807e43e10SAndras Olah 			}
2649a0292f23SGarrett Wollman 		}
2650a0292f23SGarrett Wollman 
2651a0292f23SGarrett Wollman process_ACK:
26528501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
26537cfc6904SRobert Watson 
2654dbc42409SLawrence Stewart 		acked = BYTES_THIS_ACK(tp, th);
265578b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvackpack);
265678b50714SRobert Watson 		TCPSTAT_ADD(tcps_rcvackbyte, acked);
2657df8bae1dSRodney W. Grimes 
2658df8bae1dSRodney W. Grimes 		/*
26599b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
26609b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
26619b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
26629b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
26639b8b58e0SJonathan Lemon 		 * we left off.
26649b8b58e0SJonathan Lemon 		 */
2665672dc4aeSJohn Baldwin 		if (tp->t_rxtshift == 1 && tp->t_flags & TF_PREVVALID &&
2666672dc4aeSJohn Baldwin 		    (int)(ticks - tp->t_badrxtwin) < 0)
2667dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_RTO_ERR);
26689b8b58e0SJonathan Lemon 
26699b8b58e0SJonathan Lemon 		/*
2670df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
2671df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
2672df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
2673df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
2674df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
2675df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
2676df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
2677fa55172bSMatthew Dillon 		 *
2678fa55172bSMatthew Dillon 		 * Some boxes send broken timestamp replies
2679fa55172bSMatthew Dillon 		 * during the SYN+ACK phase, ignore
2680fa55172bSMatthew Dillon 		 * timestamps of 0 or we could calculate a
2681fa55172bSMatthew Dillon 		 * huge RTT and blow up the retransmit timer.
2682df8bae1dSRodney W. Grimes 		 */
2683d8951c8aSBjoern A. Zeeb 		if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) {
2684d8951c8aSBjoern A. Zeeb 			u_int t;
2685d8951c8aSBjoern A. Zeeb 
2686d8951c8aSBjoern A. Zeeb 			t = tcp_ts_getticks() - to.to_tsecr;
2687d8951c8aSBjoern A. Zeeb 			if (!tp->t_rttlow || tp->t_rttlow > t)
2688d8951c8aSBjoern A. Zeeb 				tp->t_rttlow = t;
2689d8951c8aSBjoern A. Zeeb 			tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1);
2690fa55172bSMatthew Dillon 		} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2691eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
2692eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - tp->t_rtttime;
26939b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2694fa55172bSMatthew Dillon 		}
2695df8bae1dSRodney W. Grimes 
2696df8bae1dSRodney W. Grimes 		/*
2697df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
2698df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
2699df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
2700df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
2701df8bae1dSRodney W. Grimes 		 */
2702fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
2703b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
2704df8bae1dSRodney W. Grimes 			needoutput = 1;
2705b8152ba7SAndre Oppermann 		} else if (!tcp_timer_active(tp, TT_PERSIST))
2706b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
2707a0292f23SGarrett Wollman 
2708a0292f23SGarrett Wollman 		/*
2709a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
2710a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
2711a0292f23SGarrett Wollman 		 */
2712a0292f23SGarrett Wollman 		if (acked == 0)
2713a0292f23SGarrett Wollman 			goto step6;
2714a0292f23SGarrett Wollman 
2715df8bae1dSRodney W. Grimes 		/*
2716dbc42409SLawrence Stewart 		 * Let the congestion control algorithm update congestion
2717dbc42409SLawrence Stewart 		 * control related information. This typically means increasing
2718dbc42409SLawrence Stewart 		 * the congestion window.
2719df8bae1dSRodney W. Grimes 		 */
2720dbc42409SLawrence Stewart 		cc_ack_received(tp, th, CC_ACK);
2721dbc42409SLawrence Stewart 
27225905999bSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
2723df8bae1dSRodney W. Grimes 		if (acked > so->so_snd.sb_cc) {
272460ee3bb2SAndre Oppermann 			tp->snd_wnd -= so->so_snd.sb_cc;
2725c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd,
2726c11a15bfSGleb Smirnoff 			    (int)so->so_snd.sb_cc);
2727df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
2728df8bae1dSRodney W. Grimes 		} else {
2729c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd, acked);
273060ee3bb2SAndre Oppermann 			tp->snd_wnd -= acked;
2731df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
2732df8bae1dSRodney W. Grimes 		}
2733564aab1fSAndre Oppermann 		/* NB: sowwakeup_locked() does an implicit unlock. */
27341e4d7da7SRobert Watson 		sowwakeup_locked(so);
2735c11a15bfSGleb Smirnoff 		m_freem(mfree);
2736e8949f74SAndre Oppermann 		/* Detect una wraparound. */
2737dbc42409SLawrence Stewart 		if (!IN_RECOVERY(tp->t_flags) &&
27389d11646dSJeffrey Hsu 		    SEQ_GT(tp->snd_una, tp->snd_recover) &&
27399d11646dSJeffrey Hsu 		    SEQ_LEQ(th->th_ack, tp->snd_recover))
27409d11646dSJeffrey Hsu 			tp->snd_recover = th->th_ack - 1;
2741dbc42409SLawrence Stewart 		/* XXXLAS: Can this be moved up into cc_post_recovery? */
2742dbc42409SLawrence Stewart 		if (IN_RECOVERY(tp->t_flags) &&
274324cb0f22SLawrence Stewart 		    SEQ_GEQ(th->th_ack, tp->snd_recover)) {
2744dbc42409SLawrence Stewart 			EXIT_RECOVERY(tp->t_flags);
274524cb0f22SLawrence Stewart 		}
2746fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
27473529149eSAndre Oppermann 		if (tp->t_flags & TF_SACK_PERMIT) {
27486d90faf3SPaul Saab 			if (SEQ_GT(tp->snd_una, tp->snd_recover))
27496d90faf3SPaul Saab 				tp->snd_recover = tp->snd_una;
27506d90faf3SPaul Saab 		}
2751df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2752df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
2753df8bae1dSRodney W. Grimes 
2754df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2755df8bae1dSRodney W. Grimes 
2756df8bae1dSRodney W. Grimes 		/*
2757df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
2758df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
2759df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
2760df8bae1dSRodney W. Grimes 		 */
2761df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2762df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2763df8bae1dSRodney W. Grimes 				/*
2764df8bae1dSRodney W. Grimes 				 * If we can't receive any more
2765df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
2766df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
2767df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
2768df8bae1dSRodney W. Grimes 				 * we'll hang forever.
2769e8949f74SAndre Oppermann 				 *
2770e8949f74SAndre Oppermann 				 * XXXjl:
2771340c35deSJonathan Lemon 				 * we should release the tp also, and use a
2772340c35deSJonathan Lemon 				 * compressed state.
2773340c35deSJonathan Lemon 				 */
2774c0b99ffaSRobert Watson 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
277503e49181SSeigo Tanimura 					soisdisconnected(so);
27769077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
27779077f387SGleb Smirnoff 					    (tcp_fast_finwait2_recycle ?
27789077f387SGleb Smirnoff 					    tcp_finwait2_timeout :
27799077f387SGleb Smirnoff 					    TP_MAXIDLE(tp)));
27804cc20ab1SSeigo Tanimura 				}
278157f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
2782df8bae1dSRodney W. Grimes 			}
2783df8bae1dSRodney W. Grimes 			break;
2784df8bae1dSRodney W. Grimes 
2785df8bae1dSRodney W. Grimes 		/*
2786df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
2787df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
2788df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
2789df8bae1dSRodney W. Grimes 		 * the segment.
2790df8bae1dSRodney W. Grimes 		 */
2791df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
2792df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2793252ca428SRobert Watson 				INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
279411a20fb8SJeffrey Hsu 				tcp_twstart(tp);
2795603724d3SBjoern A. Zeeb 				INP_INFO_WUNLOCK(&V_tcbinfo);
2796340c35deSJonathan Lemon 				m_freem(m);
2797679d9708SAndre Oppermann 				return;
2798df8bae1dSRodney W. Grimes 			}
2799df8bae1dSRodney W. Grimes 			break;
2800df8bae1dSRodney W. Grimes 
2801df8bae1dSRodney W. Grimes 		/*
2802df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
2803df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
2804df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
2805df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
2806df8bae1dSRodney W. Grimes 		 */
2807df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
2808df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2809252ca428SRobert Watson 				INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2810df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
2811df8bae1dSRodney W. Grimes 				goto drop;
2812df8bae1dSRodney W. Grimes 			}
2813df8bae1dSRodney W. Grimes 			break;
2814df8bae1dSRodney W. Grimes 		}
2815df8bae1dSRodney W. Grimes 	}
2816df8bae1dSRodney W. Grimes 
2817df8bae1dSRodney W. Grimes step6:
28188501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
28197cfc6904SRobert Watson 
2820df8bae1dSRodney W. Grimes 	/*
282160ee3bb2SAndre Oppermann 	 * Update window information.
282260ee3bb2SAndre Oppermann 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2823df8bae1dSRodney W. Grimes 	 */
282460ee3bb2SAndre Oppermann 	if ((thflags & TH_ACK) &&
282560ee3bb2SAndre Oppermann 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
282660ee3bb2SAndre Oppermann 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
282760ee3bb2SAndre Oppermann 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
282860ee3bb2SAndre Oppermann 		/* keep track of pure window updates */
282960ee3bb2SAndre Oppermann 		if (tlen == 0 &&
283060ee3bb2SAndre Oppermann 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
283178b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvwinupd);
2832df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
283360ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq;
283460ee3bb2SAndre Oppermann 		tp->snd_wl2 = th->th_ack;
2835df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
2836df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
283760ee3bb2SAndre Oppermann 		needoutput = 1;
2838df8bae1dSRodney W. Grimes 	}
2839df8bae1dSRodney W. Grimes 
2840df8bae1dSRodney W. Grimes 	/*
2841df8bae1dSRodney W. Grimes 	 * Process segments with URG.
2842df8bae1dSRodney W. Grimes 	 */
2843fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
2844df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2845df8bae1dSRodney W. Grimes 		/*
2846df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
2847df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
2848df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
2849df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
2850df8bae1dSRodney W. Grimes 		 */
285118ad5842SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
2852fb59c426SYoshinobu Inoue 		if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
2853fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
2854fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
285518ad5842SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);	/* XXX */
2856df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
2857df8bae1dSRodney W. Grimes 		}
2858df8bae1dSRodney W. Grimes 		/*
2859df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
2860df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
2861df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2862df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
2863df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
2864df8bae1dSRodney W. Grimes 		 *
2865df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
2866df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
2867df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
2868df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
2869df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
2870df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
2871df8bae1dSRodney W. Grimes 		 */
2872fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2873fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
2874df8bae1dSRodney W. Grimes 			so->so_oobmark = so->so_rcv.sb_cc +
2875df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
2876927c5ceaSRobert Watson 			if (so->so_oobmark == 0)
2877c0b99ffaSRobert Watson 				so->so_rcv.sb_state |= SBS_RCVATMARK;
2878df8bae1dSRodney W. Grimes 			sohasoutofband(so);
2879df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2880df8bae1dSRodney W. Grimes 		}
288118ad5842SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
2882df8bae1dSRodney W. Grimes 		/*
2883df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
2884df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
2885df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
2886df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
2887df8bae1dSRodney W. Grimes 		 */
288830613f56SJeffrey Hsu 		if (th->th_urp <= (u_long)tlen &&
288930613f56SJeffrey Hsu 		    !(so->so_options & SO_OOBINLINE)) {
289030613f56SJeffrey Hsu 			/* hdr drop is delayed */
289130613f56SJeffrey Hsu 			tcp_pulloutofband(so, th, m, drop_hdrlen);
289230613f56SJeffrey Hsu 		}
2893c068736aSJeffrey Hsu 	} else {
2894df8bae1dSRodney W. Grimes 		/*
2895df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
2896df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
2897df8bae1dSRodney W. Grimes 		 * with the receive window.
2898df8bae1dSRodney W. Grimes 		 */
2899df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2900df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
2901c068736aSJeffrey Hsu 	}
2902df8bae1dSRodney W. Grimes dodata:							/* XXX */
29038501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
29047cfc6904SRobert Watson 
2905df8bae1dSRodney W. Grimes 	/*
2906df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
2907df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
2908df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
2909df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
2910df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
2911df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
2912df8bae1dSRodney W. Grimes 	 */
2913fb59c426SYoshinobu Inoue 	if ((tlen || (thflags & TH_FIN)) &&
2914df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
291569e03620SPaul Saab 		tcp_seq save_start = th->th_seq;
2916fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
2917e4b64281SJesper Skriver 		/*
2918c068736aSJeffrey Hsu 		 * Insert segment which includes th into TCP reassembly queue
2919c068736aSJeffrey Hsu 		 * with control block tp.  Set thflags to whether reassembly now
2920c068736aSJeffrey Hsu 		 * includes a segment with FIN.  This handles the common case
2921c068736aSJeffrey Hsu 		 * inline (segment is the next to be received on an established
2922c068736aSJeffrey Hsu 		 * connection, and the queue is empty), avoiding linkage into
2923c068736aSJeffrey Hsu 		 * and removal from the queue and repetition of various
2924c068736aSJeffrey Hsu 		 * conversions.
2925c068736aSJeffrey Hsu 		 * Set DELACK for segments received in order, but ack
2926c068736aSJeffrey Hsu 		 * immediately when segments are out of order (so
2927c068736aSJeffrey Hsu 		 * fast retransmit can work).
2928e4b64281SJesper Skriver 		 */
2929e4b64281SJesper Skriver 		if (th->th_seq == tp->rcv_nxt &&
2930e4b64281SJesper Skriver 		    LIST_EMPTY(&tp->t_segq) &&
2931e4b64281SJesper Skriver 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
2932*c1e5a6e5SAndre Oppermann 			if (DELAY_ACK(tp, tlen))
29333bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
2934e4b64281SJesper Skriver 			else
2935e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
2936e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
2937e4b64281SJesper Skriver 			thflags = th->th_flags & TH_FIN;
293878b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpack);
293978b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
2940e4b64281SJesper Skriver 			ND6_HINT(tp);
29411e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
2942c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
2943c1c36a2cSMike Silbersack 				m_freem(m);
2944c1c36a2cSMike Silbersack 			else
29451e4d7da7SRobert Watson 				sbappendstream_locked(&so->so_rcv, m);
2946e8949f74SAndre Oppermann 			/* NB: sorwakeup_locked() does an implicit unlock. */
29471e4d7da7SRobert Watson 			sorwakeup_locked(so);
2948e4b64281SJesper Skriver 		} else {
2949e8949f74SAndre Oppermann 			/*
2950e8949f74SAndre Oppermann 			 * XXX: Due to the header drop above "th" is
2951e8949f74SAndre Oppermann 			 * theoretically invalid by now.  Fortunately
2952e8949f74SAndre Oppermann 			 * m_adj() doesn't actually frees any mbufs
2953e8949f74SAndre Oppermann 			 * when trimming from the head.
2954e8949f74SAndre Oppermann 			 */
2955e4b64281SJesper Skriver 			thflags = tcp_reass(tp, th, &tlen, m);
2956e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
2957e4b64281SJesper Skriver 		}
29583529149eSAndre Oppermann 		if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT))
2959f194524fSAndre Oppermann 			tcp_update_sack_list(tp, save_start, save_start + tlen);
2960302ce8d6SAndre Oppermann #if 0
2961df8bae1dSRodney W. Grimes 		/*
2962df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
2963df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
2964df8bae1dSRodney W. Grimes 		 * buffer size.
2965302ce8d6SAndre Oppermann 		 * XXX: Unused.
2966df8bae1dSRodney W. Grimes 		 */
2967f701e30dSJohn Baldwin 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt))
2968df8bae1dSRodney W. Grimes 			len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2969f701e30dSJohn Baldwin 		else
2970f701e30dSJohn Baldwin 			len = so->so_rcv.sb_hiwat;
2971302ce8d6SAndre Oppermann #endif
2972df8bae1dSRodney W. Grimes 	} else {
2973df8bae1dSRodney W. Grimes 		m_freem(m);
2974fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
2975df8bae1dSRodney W. Grimes 	}
2976df8bae1dSRodney W. Grimes 
2977df8bae1dSRodney W. Grimes 	/*
2978df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
2979df8bae1dSRodney W. Grimes 	 * that the connection is closing.
2980df8bae1dSRodney W. Grimes 	 */
2981fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
2982df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2983df8bae1dSRodney W. Grimes 			socantrcvmore(so);
2984a0292f23SGarrett Wollman 			/*
2985a0292f23SGarrett Wollman 			 * If connection is half-synchronized
2986d3eede9dSAndras Olah 			 * (ie NEEDSYN flag on) then delay ACK,
2987a0292f23SGarrett Wollman 			 * so it may be piggybacked when SYN is sent.
2988a0292f23SGarrett Wollman 			 * Otherwise, since we received a FIN then no
2989a0292f23SGarrett Wollman 			 * more input can be expected, send ACK now.
2990a0292f23SGarrett Wollman 			 */
29913bfd6421SJonathan Lemon 			if (tp->t_flags & TF_NEEDSYN)
29923bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
2993a0292f23SGarrett Wollman 			else
2994df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
2995df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
2996df8bae1dSRodney W. Grimes 		}
2997df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2998df8bae1dSRodney W. Grimes 
2999df8bae1dSRodney W. Grimes 		/*
3000df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
3001df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
3002df8bae1dSRodney W. Grimes 		 */
3003df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
30049b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
30059b8b58e0SJonathan Lemon 			/* FALLTHROUGH */
3006df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
300757f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSE_WAIT);
3008df8bae1dSRodney W. Grimes 			break;
3009df8bae1dSRodney W. Grimes 
3010df8bae1dSRodney W. Grimes 		/*
3011df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
3012df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
3013df8bae1dSRodney W. Grimes 		 */
3014df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
301557f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSING);
3016df8bae1dSRodney W. Grimes 			break;
3017df8bae1dSRodney W. Grimes 
3018df8bae1dSRodney W. Grimes 		/*
3019df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
3020df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
3021df8bae1dSRodney W. Grimes 		 * standard timers.
3022df8bae1dSRodney W. Grimes 		 */
3023df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
3024252ca428SRobert Watson 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
3025252ca428SRobert Watson 			KASSERT(ti_locked == TI_WLOCKED, ("%s: dodata "
3026252ca428SRobert Watson 			    "TCP_FIN_WAIT_2 ti_locked: %d", __func__,
3027252ca428SRobert Watson 			    ti_locked));
3028252ca428SRobert Watson 
3029340c35deSJonathan Lemon 			tcp_twstart(tp);
3030603724d3SBjoern A. Zeeb 			INP_INFO_WUNLOCK(&V_tcbinfo);
3031679d9708SAndre Oppermann 			return;
3032df8bae1dSRodney W. Grimes 		}
3033df8bae1dSRodney W. Grimes 	}
3034fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED)
3035603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
3036252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
3037252ca428SRobert Watson 
3038610ee2f9SDavid Greenman #ifdef TCPDEBUG
30394cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
3040fb59c426SYoshinobu Inoue 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
3041fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3042610ee2f9SDavid Greenman #endif
3043df8bae1dSRodney W. Grimes 
3044df8bae1dSRodney W. Grimes 	/*
3045df8bae1dSRodney W. Grimes 	 * Return any desired output.
3046df8bae1dSRodney W. Grimes 	 */
3047df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
3048df8bae1dSRodney W. Grimes 		(void) tcp_output(tp);
30497792ea27SJeffrey Hsu 
3050a14c749fSJonathan Lemon check_delack:
3051252ca428SRobert Watson 	KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d",
3052252ca428SRobert Watson 	    __func__, ti_locked));
3053603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
30548501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
3055252ca428SRobert Watson 
3056a14c749fSJonathan Lemon 	if (tp->t_flags & TF_DELACK) {
30573bfd6421SJonathan Lemon 		tp->t_flags &= ~TF_DELACK;
3058b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
30593bfd6421SJonathan Lemon 	}
30608501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
3061679d9708SAndre Oppermann 	return;
3062df8bae1dSRodney W. Grimes 
3063df8bae1dSRodney W. Grimes dropafterack:
3064df8bae1dSRodney W. Grimes 	/*
3065df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
3066df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
306780ab7c0eSGarrett Wollman 	 *
306880ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
306980ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
307080ab7c0eSGarrett Wollman 	 * RST have been dropped.
307180ab7c0eSGarrett Wollman 	 *
307280ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
307380ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
307480ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
307580ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
307680ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
307780ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
3078df8bae1dSRodney W. Grimes 	 */
3079fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
3080fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
3081a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
3082a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
3083a57815efSBosko Milekic 		goto dropwithreset;
3084a57815efSBosko Milekic 	}
3085a0292f23SGarrett Wollman #ifdef TCPDEBUG
30864cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
3087fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3088fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3089a0292f23SGarrett Wollman #endif
3090fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED)
3091603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
3092252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
3093252ca428SRobert Watson 
3094df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
3095df8bae1dSRodney W. Grimes 	(void) tcp_output(tp);
30968501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
3097d6915262SRobert Watson 	m_freem(m);
3098679d9708SAndre Oppermann 	return;
3099df8bae1dSRodney W. Grimes 
3100df8bae1dSRodney W. Grimes dropwithreset:
3101fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED)
3102dd8ac7f9SRobert Watson 		INP_INFO_WUNLOCK(&V_tcbinfo);
3103252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
3104a57815efSBosko Milekic 
3105a0ca0871SRobert Watson 	if (tp != NULL) {
3106302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
31078501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
3108dd8ac7f9SRobert Watson 	} else
3109a0ca0871SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
3110679d9708SAndre Oppermann 	return;
3111df8bae1dSRodney W. Grimes 
3112df8bae1dSRodney W. Grimes drop:
3113fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED) {
3114252ca428SRobert Watson 		INP_INFO_WUNLOCK(&V_tcbinfo);
3115fa046d87SRobert Watson 		ti_locked = TI_UNLOCKED;
3116fa046d87SRobert Watson 	}
3117252ca428SRobert Watson #ifdef INVARIANTS
3118252ca428SRobert Watson 	else
3119252ca428SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
3120252ca428SRobert Watson #endif
3121252ca428SRobert Watson 
3122df8bae1dSRodney W. Grimes 	/*
3123df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
3124df8bae1dSRodney W. Grimes 	 */
3125610ee2f9SDavid Greenman #ifdef TCPDEBUG
31261c53f806SRobert Watson 	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
3127fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3128fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3129a0292f23SGarrett Wollman #endif
31301c53f806SRobert Watson 	if (tp != NULL)
31318501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
3132d6915262SRobert Watson 	m_freem(m);
3133302ce8d6SAndre Oppermann }
3134302ce8d6SAndre Oppermann 
3135302ce8d6SAndre Oppermann /*
31360ca3f933SAndre Oppermann  * Issue RST and make ACK acceptable to originator of segment.
31370ca3f933SAndre Oppermann  * The mbuf must still include the original packet header.
31380ca3f933SAndre Oppermann  * tp may be NULL.
3139302ce8d6SAndre Oppermann  */
3140302ce8d6SAndre Oppermann static void
3141302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp,
3142302ce8d6SAndre Oppermann     int tlen, int rstreason)
3143302ce8d6SAndre Oppermann {
3144b287c6c7SBjoern A. Zeeb #ifdef INET
3145302ce8d6SAndre Oppermann 	struct ip *ip;
3146b287c6c7SBjoern A. Zeeb #endif
3147302ce8d6SAndre Oppermann #ifdef INET6
3148302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6;
3149302ce8d6SAndre Oppermann #endif
31507a3244ccSRobert Watson 
31517a3244ccSRobert Watson 	if (tp != NULL) {
31528501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
31537a3244ccSRobert Watson 	}
31547a3244ccSRobert Watson 
31550ca3f933SAndre Oppermann 	/* Don't bother if destination was broadcast/multicast. */
3156302ce8d6SAndre Oppermann 	if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
3157302ce8d6SAndre Oppermann 		goto drop;
3158302ce8d6SAndre Oppermann #ifdef INET6
3159302ce8d6SAndre Oppermann 	if (mtod(m, struct ip *)->ip_v == 6) {
3160302ce8d6SAndre Oppermann 		ip6 = mtod(m, struct ip6_hdr *);
3161302ce8d6SAndre Oppermann 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3162302ce8d6SAndre Oppermann 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3163302ce8d6SAndre Oppermann 			goto drop;
3164302ce8d6SAndre Oppermann 		/* IPv6 anycast check is done at tcp6_input() */
3165b287c6c7SBjoern A. Zeeb 	}
3166302ce8d6SAndre Oppermann #endif
3167b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3168b287c6c7SBjoern A. Zeeb 	else
3169b287c6c7SBjoern A. Zeeb #endif
3170b287c6c7SBjoern A. Zeeb #ifdef INET
3171302ce8d6SAndre Oppermann 	{
3172302ce8d6SAndre Oppermann 		ip = mtod(m, struct ip *);
3173302ce8d6SAndre Oppermann 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3174302ce8d6SAndre Oppermann 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3175302ce8d6SAndre Oppermann 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3176302ce8d6SAndre Oppermann 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
3177302ce8d6SAndre Oppermann 			goto drop;
3178302ce8d6SAndre Oppermann 	}
3179b287c6c7SBjoern A. Zeeb #endif
3180302ce8d6SAndre Oppermann 
3181302ce8d6SAndre Oppermann 	/* Perform bandwidth limiting. */
3182302ce8d6SAndre Oppermann 	if (badport_bandlim(rstreason) < 0)
3183302ce8d6SAndre Oppermann 		goto drop;
3184302ce8d6SAndre Oppermann 
3185302ce8d6SAndre Oppermann 	/* tcp_respond consumes the mbuf chain. */
3186302ce8d6SAndre Oppermann 	if (th->th_flags & TH_ACK) {
3187302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0,
3188302ce8d6SAndre Oppermann 		    th->th_ack, TH_RST);
3189302ce8d6SAndre Oppermann 	} else {
3190302ce8d6SAndre Oppermann 		if (th->th_flags & TH_SYN)
3191302ce8d6SAndre Oppermann 			tlen++;
3192302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
3193302ce8d6SAndre Oppermann 		    (tcp_seq)0, TH_RST|TH_ACK);
3194302ce8d6SAndre Oppermann 	}
3195302ce8d6SAndre Oppermann 	return;
3196302ce8d6SAndre Oppermann drop:
3197302ce8d6SAndre Oppermann 	m_freem(m);
3198df8bae1dSRodney W. Grimes }
3199df8bae1dSRodney W. Grimes 
3200be2ac88cSJonathan Lemon /*
3201be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
3202be2ac88cSJonathan Lemon  */
32030312fbe9SPoul-Henning Kamp static void
3204ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags)
3205df8bae1dSRodney W. Grimes {
3206df8bae1dSRodney W. Grimes 	int opt, optlen;
3207df8bae1dSRodney W. Grimes 
3208be2ac88cSJonathan Lemon 	to->to_flags = 0;
3209df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
3210df8bae1dSRodney W. Grimes 		opt = cp[0];
3211df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
3212df8bae1dSRodney W. Grimes 			break;
3213df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
3214df8bae1dSRodney W. Grimes 			optlen = 1;
3215df8bae1dSRodney W. Grimes 		else {
3216b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
3217b474779fSJun-ichiro itojun Hagino 				break;
3218df8bae1dSRodney W. Grimes 			optlen = cp[1];
3219b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
3220df8bae1dSRodney W. Grimes 				break;
3221df8bae1dSRodney W. Grimes 		}
3222df8bae1dSRodney W. Grimes 		switch (opt) {
3223df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
3224df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
3225df8bae1dSRodney W. Grimes 				continue;
3226f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3227df8bae1dSRodney W. Grimes 				continue;
3228be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
3229be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
3230be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
3231fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
3232df8bae1dSRodney W. Grimes 			break;
3233df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
3234df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
3235df8bae1dSRodney W. Grimes 				continue;
3236f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3237df8bae1dSRodney W. Grimes 				continue;
3238be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
323902a1a643SAndre Oppermann 			to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT);
3240df8bae1dSRodney W. Grimes 			break;
3241df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
3242df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
3243df8bae1dSRodney W. Grimes 				continue;
3244be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
3245a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
3246a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
3247fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
3248a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
3249a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
3250fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
3251df8bae1dSRodney W. Grimes 			break;
32521cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
32531cfd4b53SBruce M Simpson 		/*
32541cfd4b53SBruce M Simpson 		 * XXX In order to reply to a host which has set the
32551cfd4b53SBruce M Simpson 		 * TCP_SIGNATURE option in its initial SYN, we have to
32561cfd4b53SBruce M Simpson 		 * record the fact that the option was observed here
32571cfd4b53SBruce M Simpson 		 * for the syncache code to perform the correct response.
32581cfd4b53SBruce M Simpson 		 */
32591cfd4b53SBruce M Simpson 		case TCPOPT_SIGNATURE:
32601cfd4b53SBruce M Simpson 			if (optlen != TCPOLEN_SIGNATURE)
32611cfd4b53SBruce M Simpson 				continue;
3262df47e437SAndre Oppermann 			to->to_flags |= TOF_SIGNATURE;
3263df47e437SAndre Oppermann 			to->to_signature = cp + 2;
32641cfd4b53SBruce M Simpson 			break;
3265265ed012SBruce M Simpson #endif
32666d90faf3SPaul Saab 		case TCPOPT_SACK_PERMITTED:
3267f72167f4SAndre Oppermann 			if (optlen != TCPOLEN_SACK_PERMITTED)
32686d90faf3SPaul Saab 				continue;
3269f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3270f72167f4SAndre Oppermann 				continue;
3271603724d3SBjoern A. Zeeb 			if (!V_tcp_do_sack)
3272f72167f4SAndre Oppermann 				continue;
3273fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACKPERM;
32746d90faf3SPaul Saab 			break;
32756d90faf3SPaul Saab 		case TCPOPT_SACK:
32765a53ca16SPaul Saab 			if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
32776d90faf3SPaul Saab 				continue;
3278b728e902SAndre Oppermann 			if (flags & TO_SYN)
3279b728e902SAndre Oppermann 				continue;
3280fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACK;
32815a53ca16SPaul Saab 			to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
32825a53ca16SPaul Saab 			to->to_sacks = cp + 2;
328378b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_rcv_blocks);
32846d90faf3SPaul Saab 			break;
3285be2ac88cSJonathan Lemon 		default:
3286be2ac88cSJonathan Lemon 			continue;
3287df8bae1dSRodney W. Grimes 		}
3288df8bae1dSRodney W. Grimes 	}
3289df8bae1dSRodney W. Grimes }
3290df8bae1dSRodney W. Grimes 
3291df8bae1dSRodney W. Grimes /*
3292df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
3293df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
3294df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
3295df8bae1dSRodney W. Grimes  * sequencing purposes.
3296df8bae1dSRodney W. Grimes  */
32970312fbe9SPoul-Henning Kamp static void
3298ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m,
3299ad3f9ab3SAndre Oppermann     int off)
3300df8bae1dSRodney W. Grimes {
3301fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
3302df8bae1dSRodney W. Grimes 
3303df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
3304df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
3305df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
3306df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
3307df8bae1dSRodney W. Grimes 
33088501a69cSRobert Watson 			INP_WLOCK_ASSERT(tp->t_inpcb);
33097a3244ccSRobert Watson 
3310df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
3311df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
3312df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
3313df8bae1dSRodney W. Grimes 			m->m_len--;
331469a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
331569a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
3316df8bae1dSRodney W. Grimes 			return;
3317df8bae1dSRodney W. Grimes 		}
3318df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
3319df8bae1dSRodney W. Grimes 		m = m->m_next;
33204dfdffe9SAndre Oppermann 		if (m == NULL)
3321df8bae1dSRodney W. Grimes 			break;
3322df8bae1dSRodney W. Grimes 	}
3323df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
3324df8bae1dSRodney W. Grimes }
3325df8bae1dSRodney W. Grimes 
3326df8bae1dSRodney W. Grimes /*
3327df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
3328df8bae1dSRodney W. Grimes  * and update averages and current timeout.
3329df8bae1dSRodney W. Grimes  */
33300312fbe9SPoul-Henning Kamp static void
3331ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt)
3332df8bae1dSRodney W. Grimes {
3333ad3f9ab3SAndre Oppermann 	int delta;
3334233e8c18SGarrett Wollman 
33358501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
33362be3bf22SRobert Watson 
333778b50714SRobert Watson 	TCPSTAT_INC(tcps_rttupdated);
3338233e8c18SGarrett Wollman 	tp->t_rttupdated++;
3339233e8c18SGarrett Wollman 	if (tp->t_srtt != 0) {
3340233e8c18SGarrett Wollman 		/*
3341233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
3342233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
3343233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
3344233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
3345233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
3346233e8c18SGarrett Wollman 		 */
3347233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
3348233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
3349233e8c18SGarrett Wollman 
3350233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
3351233e8c18SGarrett Wollman 			tp->t_srtt = 1;
3352233e8c18SGarrett Wollman 
3353233e8c18SGarrett Wollman 		/*
3354233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
3355233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
3356233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
3357233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
3358233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
3359233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
3360233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
3361233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
3362233e8c18SGarrett Wollman 		 */
3363233e8c18SGarrett Wollman 		if (delta < 0)
3364233e8c18SGarrett Wollman 			delta = -delta;
3365233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
3366233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
3367233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
33681fcc99b5SMatthew Dillon 		if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
33691fcc99b5SMatthew Dillon 		    tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3370233e8c18SGarrett Wollman 	} else {
3371233e8c18SGarrett Wollman 		/*
3372233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
3373233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
3374233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
3375233e8c18SGarrett Wollman 		 */
3376233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
3377233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
33781fcc99b5SMatthew Dillon 		tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3379233e8c18SGarrett Wollman 	}
33809b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
3381df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
3382df8bae1dSRodney W. Grimes 
3383df8bae1dSRodney W. Grimes 	/*
3384df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
3385df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
3386df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
3387df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
3388df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
3389df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
3390df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
3391df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
3392df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
3393df8bae1dSRodney W. Grimes 	 */
3394233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
33959e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
3396df8bae1dSRodney W. Grimes 
3397df8bae1dSRodney W. Grimes 	/*
3398df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
3399df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
3400df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
3401df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
3402df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
3403df8bae1dSRodney W. Grimes 	 */
3404df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
3405df8bae1dSRodney W. Grimes }
3406df8bae1dSRodney W. Grimes 
3407df8bae1dSRodney W. Grimes /*
3408df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
3409df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
34104faaea55SAndre Oppermann  * If none, use an mss that can be handled on the outgoing interface
34114faaea55SAndre Oppermann  * without forcing IP to fragment.  If no route is found, route has no mtu,
3412df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
3413df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
3414df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
3415df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
3416df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
3417df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
3418df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
3419a0292f23SGarrett Wollman  *
3420a0292f23SGarrett Wollman  * Also take into account the space needed for options that we
3421a0292f23SGarrett Wollman  * send regularly.  Make maxseg shorter by that amount to assure
3422a0292f23SGarrett Wollman  * that we can send maxseg amount of data even when the options
3423a0292f23SGarrett Wollman  * are present.  Store the upper limit of the length of options plus
3424a0292f23SGarrett Wollman  * data in maxopd.
3425a0292f23SGarrett Wollman  *
342697d8d152SAndre Oppermann  * NOTE that this routine is only called when we process an incoming
3427ef341ee1SGleb Smirnoff  * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS
3428ef341ee1SGleb Smirnoff  * settings are handled in tcp_mssopt().
3429df8bae1dSRodney W. Grimes  */
3430a0292f23SGarrett Wollman void
3431ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer,
34323c914c54SAndre Oppermann     struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap)
3433df8bae1dSRodney W. Grimes {
3434b287c6c7SBjoern A. Zeeb 	int mss = 0;
3435b287c6c7SBjoern A. Zeeb 	u_long maxmtu = 0;
3436c068736aSJeffrey Hsu 	struct inpcb *inp = tp->t_inpcb;
343797d8d152SAndre Oppermann 	struct hc_metrics_lite metrics;
3438ef341ee1SGleb Smirnoff 	int origoffer;
3439fb59c426SYoshinobu Inoue #ifdef INET6
3440c068736aSJeffrey Hsu 	int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
3441c068736aSJeffrey Hsu 	size_t min_protoh = isipv6 ?
3442c068736aSJeffrey Hsu 			    sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
3443c068736aSJeffrey Hsu 			    sizeof (struct tcpiphdr);
3444c068736aSJeffrey Hsu #else
3445c068736aSJeffrey Hsu 	const size_t min_protoh = sizeof(struct tcpiphdr);
3446fb59c426SYoshinobu Inoue #endif
3447df8bae1dSRodney W. Grimes 
34488501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
34497a3244ccSRobert Watson 
3450ef341ee1SGleb Smirnoff 	if (mtuoffer != -1) {
3451ef341ee1SGleb Smirnoff 		KASSERT(offer == -1, ("%s: conflict", __func__));
3452ef341ee1SGleb Smirnoff 		offer = mtuoffer - min_protoh;
3453ef341ee1SGleb Smirnoff 	}
3454ef341ee1SGleb Smirnoff 	origoffer = offer;
3455ef341ee1SGleb Smirnoff 
3456e8949f74SAndre Oppermann 	/* Initialize. */
345797d8d152SAndre Oppermann #ifdef INET6
345897d8d152SAndre Oppermann 	if (isipv6) {
34593c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu6(&inp->inp_inc, cap);
3460603724d3SBjoern A. Zeeb 		tp->t_maxopd = tp->t_maxseg = V_tcp_v6mssdflt;
3461b287c6c7SBjoern A. Zeeb 	}
346297d8d152SAndre Oppermann #endif
3463b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3464b287c6c7SBjoern A. Zeeb 	else
3465b287c6c7SBjoern A. Zeeb #endif
3466b287c6c7SBjoern A. Zeeb #ifdef INET
346797d8d152SAndre Oppermann 	{
34683c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu(&inp->inp_inc, cap);
3469603724d3SBjoern A. Zeeb 		tp->t_maxopd = tp->t_maxseg = V_tcp_mssdflt;
3470df8bae1dSRodney W. Grimes 	}
3471b287c6c7SBjoern A. Zeeb #endif
3472df8bae1dSRodney W. Grimes 
347397d8d152SAndre Oppermann 	/*
3474e8949f74SAndre Oppermann 	 * No route to sender, stay with default mss and return.
347597d8d152SAndre Oppermann 	 */
34766f01cac6SBjoern A. Zeeb 	if (maxmtu == 0) {
34776f01cac6SBjoern A. Zeeb 		/*
34788e5c87f4SBjoern A. Zeeb 		 * In case we return early we need to initialize metrics
34796f01cac6SBjoern A. Zeeb 		 * to a defined state as tcp_hc_get() would do for us
34806f01cac6SBjoern A. Zeeb 		 * if there was no cache hit.
34816f01cac6SBjoern A. Zeeb 		 */
34826f01cac6SBjoern A. Zeeb 		if (metricptr != NULL)
34836f01cac6SBjoern A. Zeeb 			bzero(metricptr, sizeof(struct hc_metrics_lite));
348497d8d152SAndre Oppermann 		return;
34856f01cac6SBjoern A. Zeeb 	}
348697d8d152SAndre Oppermann 
3487e8949f74SAndre Oppermann 	/* What have we got? */
348897d8d152SAndre Oppermann 	switch (offer) {
348997d8d152SAndre Oppermann 		case 0:
349097d8d152SAndre Oppermann 			/*
349197d8d152SAndre Oppermann 			 * Offer == 0 means that there was no MSS on the SYN
3492c3b02504SBjoern A. Zeeb 			 * segment, in this case we use tcp_mssdflt as
3493c3b02504SBjoern A. Zeeb 			 * already assigned to t_maxopd above.
349497d8d152SAndre Oppermann 			 */
3495c3b02504SBjoern A. Zeeb 			offer = tp->t_maxopd;
349697d8d152SAndre Oppermann 			break;
349797d8d152SAndre Oppermann 
349897d8d152SAndre Oppermann 		case -1:
3499a0292f23SGarrett Wollman 			/*
3500c94c54e4SAndre Oppermann 			 * Offer == -1 means that we didn't receive SYN yet.
3501a0292f23SGarrett Wollman 			 */
350297d8d152SAndre Oppermann 			/* FALLTHROUGH */
350397d8d152SAndre Oppermann 
350497d8d152SAndre Oppermann 		default:
3505a0292f23SGarrett Wollman 			/*
350653369ac9SAndre Oppermann 			 * Prevent DoS attack with too small MSS. Round up
350753369ac9SAndre Oppermann 			 * to at least minmss.
350853369ac9SAndre Oppermann 			 */
3509603724d3SBjoern A. Zeeb 			offer = max(offer, V_tcp_minmss);
351097d8d152SAndre Oppermann 	}
3511a0292f23SGarrett Wollman 
3512df8bae1dSRodney W. Grimes 	/*
3513e8949f74SAndre Oppermann 	 * rmx information is now retrieved from tcp_hostcache.
3514df8bae1dSRodney W. Grimes 	 */
351597d8d152SAndre Oppermann 	tcp_hc_get(&inp->inp_inc, &metrics);
35163cee92e0SBjoern A. Zeeb 	if (metricptr != NULL)
35173cee92e0SBjoern A. Zeeb 		bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite));
351897d8d152SAndre Oppermann 
3519df8bae1dSRodney W. Grimes 	/*
3520e8949f74SAndre Oppermann 	 * If there's a discovered mtu int tcp hostcache, use it
3521fb59c426SYoshinobu Inoue 	 * else, use the link mtu.
3522df8bae1dSRodney W. Grimes 	 */
352397d8d152SAndre Oppermann 	if (metrics.rmx_mtu)
35242d166c02SAndre Oppermann 		mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
3525c068736aSJeffrey Hsu 	else {
352631b3783cSHajimu UMEMOTO #ifdef INET6
3527fb59c426SYoshinobu Inoue 		if (isipv6) {
352897d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3529603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
353097d8d152SAndre Oppermann 			    !in6_localaddr(&inp->in6p_faddr))
3531603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_v6mssdflt);
3532b287c6c7SBjoern A. Zeeb 		}
3533b3399803SHajimu UMEMOTO #endif
3534b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3535b287c6c7SBjoern A. Zeeb 		else
3536b287c6c7SBjoern A. Zeeb #endif
3537b287c6c7SBjoern A. Zeeb #ifdef INET
353897d8d152SAndre Oppermann 		{
353997d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3540603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
354197d8d152SAndre Oppermann 			    !in_localaddr(inp->inp_faddr))
3542603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_mssdflt);
3543a0292f23SGarrett Wollman 		}
3544b287c6c7SBjoern A. Zeeb #endif
35453cee92e0SBjoern A. Zeeb 		/*
35463cee92e0SBjoern A. Zeeb 		 * XXX - The above conditional (mss = maxmtu - min_protoh)
35473cee92e0SBjoern A. Zeeb 		 * probably violates the TCP spec.
35483cee92e0SBjoern A. Zeeb 		 * The problem is that, since we don't know the
35493cee92e0SBjoern A. Zeeb 		 * other end's MSS, we are supposed to use a conservative
35503cee92e0SBjoern A. Zeeb 		 * default.  But, if we do that, then MTU discovery will
35513cee92e0SBjoern A. Zeeb 		 * never actually take place, because the conservative
35523cee92e0SBjoern A. Zeeb 		 * default is much less than the MTUs typically seen
35533cee92e0SBjoern A. Zeeb 		 * on the Internet today.  For the moment, we'll sweep
35543cee92e0SBjoern A. Zeeb 		 * this under the carpet.
35553cee92e0SBjoern A. Zeeb 		 *
35563cee92e0SBjoern A. Zeeb 		 * The conservative default might not actually be a problem
35573cee92e0SBjoern A. Zeeb 		 * if the only case this occurs is when sending an initial
35583cee92e0SBjoern A. Zeeb 		 * SYN with options and data to a host we've never talked
35593cee92e0SBjoern A. Zeeb 		 * to before.  Then, they will reply with an MSS value which
35603cee92e0SBjoern A. Zeeb 		 * will get recorded and the new parameters should get
35613cee92e0SBjoern A. Zeeb 		 * recomputed.  For Further Study.
35623cee92e0SBjoern A. Zeeb 		 */
356397d8d152SAndre Oppermann 	}
3564a0292f23SGarrett Wollman 	mss = min(mss, offer);
356597d8d152SAndre Oppermann 
3566a0292f23SGarrett Wollman 	/*
35673cee92e0SBjoern A. Zeeb 	 * Sanity check: make sure that maxopd will be large
35683cee92e0SBjoern A. Zeeb 	 * enough to allow some data on segments even if the
35693cee92e0SBjoern A. Zeeb 	 * all the option space is used (40bytes).  Otherwise
35703cee92e0SBjoern A. Zeeb 	 * funny things may happen in tcp_output.
35713cee92e0SBjoern A. Zeeb 	 */
35723cee92e0SBjoern A. Zeeb 	mss = max(mss, 64);
35733cee92e0SBjoern A. Zeeb 
35743cee92e0SBjoern A. Zeeb 	/*
3575a0292f23SGarrett Wollman 	 * maxopd stores the maximum length of data AND options
3576a0292f23SGarrett Wollman 	 * in a segment; maxseg is the amount of data in a normal
3577a0292f23SGarrett Wollman 	 * segment.  We need to store this value (maxopd) apart
3578a0292f23SGarrett Wollman 	 * from maxseg, because now every segment carries options
3579a0292f23SGarrett Wollman 	 * and thus we normally have somewhat less data in segments.
3580a0292f23SGarrett Wollman 	 */
3581a0292f23SGarrett Wollman 	tp->t_maxopd = mss;
3582a0292f23SGarrett Wollman 
3583a0292f23SGarrett Wollman 	/*
3584e8949f74SAndre Oppermann 	 * origoffer==-1 indicates that no segments were received yet.
3585c94c54e4SAndre Oppermann 	 * In this case we just guess.
3586a0292f23SGarrett Wollman 	 */
3587a0292f23SGarrett Wollman 	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
3588a0292f23SGarrett Wollman 	    (origoffer == -1 ||
3589a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
3590a0292f23SGarrett Wollman 		mss -= TCPOLEN_TSTAMP_APPA;
3591a0292f23SGarrett Wollman 
359297d8d152SAndre Oppermann 	tp->t_maxseg = mss;
35933cee92e0SBjoern A. Zeeb }
35943cee92e0SBjoern A. Zeeb 
35953cee92e0SBjoern A. Zeeb void
35963cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer)
35973cee92e0SBjoern A. Zeeb {
3598dbc42409SLawrence Stewart 	int mss;
35993cee92e0SBjoern A. Zeeb 	u_long bufsize;
36003cee92e0SBjoern A. Zeeb 	struct inpcb *inp;
36013cee92e0SBjoern A. Zeeb 	struct socket *so;
36023cee92e0SBjoern A. Zeeb 	struct hc_metrics_lite metrics;
36033c914c54SAndre Oppermann 	struct tcp_ifcap cap;
3604dbc42409SLawrence Stewart 
36053cee92e0SBjoern A. Zeeb 	KASSERT(tp != NULL, ("%s: tp == NULL", __func__));
36063cee92e0SBjoern A. Zeeb 
36073c914c54SAndre Oppermann 	bzero(&cap, sizeof(cap));
36083c914c54SAndre Oppermann 	tcp_mss_update(tp, offer, -1, &metrics, &cap);
36093cee92e0SBjoern A. Zeeb 
36103cee92e0SBjoern A. Zeeb 	mss = tp->t_maxseg;
36113cee92e0SBjoern A. Zeeb 	inp = tp->t_inpcb;
361297d8d152SAndre Oppermann 
3613df8bae1dSRodney W. Grimes 	/*
361497d8d152SAndre Oppermann 	 * If there's a pipesize, change the socket buffer to that size,
361597d8d152SAndre Oppermann 	 * don't change if sb_hiwat is different than default (then it
361697d8d152SAndre Oppermann 	 * has been changed on purpose with setsockopt).
361797d8d152SAndre Oppermann 	 * Make the socket buffers an integral number of mss units;
361897d8d152SAndre Oppermann 	 * if the mss is larger than the socket buffer, decrease the mss.
3619df8bae1dSRodney W. Grimes 	 */
3620c3b02504SBjoern A. Zeeb 	so = inp->inp_socket;
36213f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
3622e233e2acSAndre Oppermann 	if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe)
362397d8d152SAndre Oppermann 		bufsize = metrics.rmx_sendpipe;
362497d8d152SAndre Oppermann 	else
3625df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
3626df8bae1dSRodney W. Grimes 	if (bufsize < mss)
3627df8bae1dSRodney W. Grimes 		mss = bufsize;
3628df8bae1dSRodney W. Grimes 	else {
3629df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3630df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3631df8bae1dSRodney W. Grimes 			bufsize = sb_max;
363288c39af3SRuslan Ermilov 		if (bufsize > so->so_snd.sb_hiwat)
36333f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_snd, bufsize, so, NULL);
3634df8bae1dSRodney W. Grimes 	}
36353f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
3636df8bae1dSRodney W. Grimes 	tp->t_maxseg = mss;
3637df8bae1dSRodney W. Grimes 
36383f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
3639e233e2acSAndre Oppermann 	if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe)
364097d8d152SAndre Oppermann 		bufsize = metrics.rmx_recvpipe;
364197d8d152SAndre Oppermann 	else
3642df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
3643df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
3644df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3645df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3646df8bae1dSRodney W. Grimes 			bufsize = sb_max;
364788c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
36483f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL);
3649df8bae1dSRodney W. Grimes 	}
36503f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
365191d6cfa6SBjoern A. Zeeb 
365291d6cfa6SBjoern A. Zeeb 	/* Check the interface for TSO capabilities. */
36533c914c54SAndre Oppermann 	if (cap.ifcap & CSUM_TSO) {
365491d6cfa6SBjoern A. Zeeb 		tp->t_flags |= TF_TSO;
36553c914c54SAndre Oppermann 		tp->t_tsomax = cap.tsomax;
36563c914c54SAndre Oppermann 	}
3657a0292f23SGarrett Wollman }
3658a0292f23SGarrett Wollman 
3659a0292f23SGarrett Wollman /*
3660a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
3661a0292f23SGarrett Wollman  */
3662a0292f23SGarrett Wollman int
3663ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc)
3664a0292f23SGarrett Wollman {
366597d8d152SAndre Oppermann 	int mss = 0;
366697d8d152SAndre Oppermann 	u_long maxmtu = 0;
366797d8d152SAndre Oppermann 	u_long thcmtu = 0;
366897d8d152SAndre Oppermann 	size_t min_protoh;
3669a0292f23SGarrett Wollman 
367097d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3671a0292f23SGarrett Wollman 
367231b3783cSHajimu UMEMOTO #ifdef INET6
3673dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
3674603724d3SBjoern A. Zeeb 		mss = V_tcp_v6mssdflt;
3675233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(inc, NULL);
367697d8d152SAndre Oppermann 		min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
3677b287c6c7SBjoern A. Zeeb 	}
367831b3783cSHajimu UMEMOTO #endif
3679b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3680b287c6c7SBjoern A. Zeeb 	else
3681b287c6c7SBjoern A. Zeeb #endif
3682b287c6c7SBjoern A. Zeeb #ifdef INET
368397d8d152SAndre Oppermann 	{
3684603724d3SBjoern A. Zeeb 		mss = V_tcp_mssdflt;
3685233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(inc, NULL);
368697d8d152SAndre Oppermann 		min_protoh = sizeof(struct tcpiphdr);
368797d8d152SAndre Oppermann 	}
3688b287c6c7SBjoern A. Zeeb #endif
36893a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET)
36903a9391deSBjoern A. Zeeb 	thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
36913a9391deSBjoern A. Zeeb #endif
36923a9391deSBjoern A. Zeeb 
369397d8d152SAndre Oppermann 	if (maxmtu && thcmtu)
369497d8d152SAndre Oppermann 		mss = min(maxmtu, thcmtu) - min_protoh;
369597d8d152SAndre Oppermann 	else if (maxmtu || thcmtu)
369697d8d152SAndre Oppermann 		mss = max(maxmtu, thcmtu) - min_protoh;
369797d8d152SAndre Oppermann 
369897d8d152SAndre Oppermann 	return (mss);
3699df8bae1dSRodney W. Grimes }
370046f58482SJonathan Lemon 
370146f58482SJonathan Lemon 
370246f58482SJonathan Lemon /*
3703c068736aSJeffrey Hsu  * On a partial ack arrives, force the retransmission of the
3704c068736aSJeffrey Hsu  * next unacknowledged segment.  Do not clear tp->t_dupacks.
3705c068736aSJeffrey Hsu  * By setting snd_nxt to ti_ack, this forces retransmission timer to
3706c068736aSJeffrey Hsu  * be started again.
370746f58482SJonathan Lemon  */
3708c068736aSJeffrey Hsu static void
3709ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
371046f58482SJonathan Lemon {
371146f58482SJonathan Lemon 	tcp_seq onxt = tp->snd_nxt;
371246f58482SJonathan Lemon 	u_long  ocwnd = tp->snd_cwnd;
371346f58482SJonathan Lemon 
37148501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
37157a3244ccSRobert Watson 
3716b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_REXMT, 0);
371746f58482SJonathan Lemon 	tp->t_rtttime = 0;
371846f58482SJonathan Lemon 	tp->snd_nxt = th->th_ack;
37196b2a5f92SJayanth Vijayaraghavan 	/*
3720c068736aSJeffrey Hsu 	 * Set snd_cwnd to one segment beyond acknowledged offset.
3721c068736aSJeffrey Hsu 	 * (tp->snd_una has not yet been updated when this function is called.)
37226b2a5f92SJayanth Vijayaraghavan 	 */
3723dbc42409SLawrence Stewart 	tp->snd_cwnd = tp->t_maxseg + BYTES_THIS_ACK(tp, th);
3724a84db8f4SMatthew Dillon 	tp->t_flags |= TF_ACKNOW;
37256b2a5f92SJayanth Vijayaraghavan 	(void) tcp_output(tp);
372646f58482SJonathan Lemon 	tp->snd_cwnd = ocwnd;
372746f58482SJonathan Lemon 	if (SEQ_GT(onxt, tp->snd_nxt))
372846f58482SJonathan Lemon 		tp->snd_nxt = onxt;
372946f58482SJonathan Lemon 	/*
373046f58482SJonathan Lemon 	 * Partial window deflation.  Relies on fact that tp->snd_una
373146f58482SJonathan Lemon 	 * not updated yet.
373246f58482SJonathan Lemon 	 */
3733dbc42409SLawrence Stewart 	if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th))
3734dbc42409SLawrence Stewart 		tp->snd_cwnd -= BYTES_THIS_ACK(tp, th);
3735d7587117SPaul Saab 	else
3736d7587117SPaul Saab 		tp->snd_cwnd = 0;
3737d7587117SPaul Saab 	tp->snd_cwnd += tp->t_maxseg;
373846f58482SJonathan Lemon }
3739