xref: /freebsd/sys/netinet/tcp_input.c (revision 8f134647ca14b930e2ab01b4192ce895127a6e02)
1c398230bSWarner Losh /*-
2e79adb8eSGarrett Wollman  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4dbc42409SLawrence Stewart  * Copyright (c) 2007-2008,2010
5dbc42409SLawrence Stewart  *	Swinburne University of Technology, Melbourne, Australia.
6dbc42409SLawrence Stewart  * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org>
7dbc42409SLawrence Stewart  * Copyright (c) 2010 The FreeBSD Foundation
8fa046d87SRobert Watson  * Copyright (c) 2010-2011 Juniper Networks, Inc.
9dbc42409SLawrence Stewart  * All rights reserved.
10dbc42409SLawrence Stewart  *
11dbc42409SLawrence Stewart  * Portions of this software were developed at the Centre for Advanced Internet
12891b8ed4SLawrence Stewart  * Architectures, Swinburne University of Technology, by Lawrence Stewart,
13891b8ed4SLawrence Stewart  * James Healy and David Hayes, made possible in part by a grant from the Cisco
14891b8ed4SLawrence Stewart  * University Research Program Fund at Community Foundation Silicon Valley.
15dbc42409SLawrence Stewart  *
16dbc42409SLawrence Stewart  * Portions of this software were developed at the Centre for Advanced
17dbc42409SLawrence Stewart  * Internet Architectures, Swinburne University of Technology, Melbourne,
18dbc42409SLawrence Stewart  * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
19df8bae1dSRodney W. Grimes  *
20fa046d87SRobert Watson  * Portions of this software were developed by Robert N. M. Watson under
21fa046d87SRobert Watson  * contract to Juniper Networks, Inc.
22fa046d87SRobert Watson  *
23df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
24df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
25df8bae1dSRodney W. Grimes  * are met:
26df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
27df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
28df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
29df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
30df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
31df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
32df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
33df8bae1dSRodney W. Grimes  *    without specific prior written permission.
34df8bae1dSRodney W. Grimes  *
35df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
46df8bae1dSRodney W. Grimes  *
47e79adb8eSGarrett Wollman  *	@(#)tcp_input.c	8.12 (Berkeley) 5/24/95
48df8bae1dSRodney W. Grimes  */
49df8bae1dSRodney W. Grimes 
504b421e2dSMike Silbersack #include <sys/cdefs.h>
514b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
524b421e2dSMike Silbersack 
53f9e354dfSJulian Elischer #include "opt_ipfw.h"		/* for ipfw_fwd	*/
541cfd4b53SBruce M Simpson #include "opt_inet.h"
55fb59c426SYoshinobu Inoue #include "opt_inet6.h"
563a2a9f79SYoshinobu Inoue #include "opt_ipsec.h"
570cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
580cc12cc5SJoerg Wunsch 
59df8bae1dSRodney W. Grimes #include <sys/param.h>
6098163b98SPoul-Henning Kamp #include <sys/kernel.h>
6139bc9de5SLawrence Stewart #include <sys/hhook.h>
62df8bae1dSRodney W. Grimes #include <sys/malloc.h>
63df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
64a29f300eSGarrett Wollman #include <sys/proc.h>		/* for proc0 declaration */
65df8bae1dSRodney W. Grimes #include <sys/protosw.h>
66960ed29cSSeigo Tanimura #include <sys/signalvar.h>
67df8bae1dSRodney W. Grimes #include <sys/socket.h>
68df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
69960ed29cSSeigo Tanimura #include <sys/sysctl.h>
70816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
71960ed29cSSeigo Tanimura #include <sys/systm.h>
72df8bae1dSRodney W. Grimes 
73e79adb8eSGarrett Wollman #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
74e79adb8eSGarrett Wollman 
7512e2e970SAndre Oppermann #include <vm/uma.h>
7612e2e970SAndre Oppermann 
77df8bae1dSRodney W. Grimes #include <net/if.h>
78df8bae1dSRodney W. Grimes #include <net/route.h>
79530c0060SRobert Watson #include <net/vnet.h>
80df8bae1dSRodney W. Grimes 
81773673c1SAndre Oppermann #define TCPSTATES		/* for logging */
82773673c1SAndre Oppermann 
83dbc42409SLawrence Stewart #include <netinet/cc.h>
84df8bae1dSRodney W. Grimes #include <netinet/in.h>
85960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
86df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
87960ed29cSSeigo Tanimura #include <netinet/in_var.h>
88df8bae1dSRodney W. Grimes #include <netinet/ip.h>
898e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h>	/* required for icmp_var.h */
90686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM */
91df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
92ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
93686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
94686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
95686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
96960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h>
97960ed29cSSeigo Tanimura #include <netinet6/nd6.h>
98df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
99df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
100df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
101df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
102fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h>
103df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
104c325962bSMike Silbersack #include <netinet/tcp_syncache.h>
105610ee2f9SDavid Greenman #ifdef TCPDEBUG
106df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
107fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */
10809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
10909fe6320SNavdeep Parhar #include <netinet/tcp_offload.h>
11009fe6320SNavdeep Parhar #endif
111fb59c426SYoshinobu Inoue 
112b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
113b9234fafSSam Leffler #include <netipsec/ipsec.h>
114b9234fafSSam Leffler #include <netipsec/ipsec6.h>
115b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
116b9234fafSSam Leffler 
117db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
118db4f9cc7SJonathan Lemon 
119aed55708SRobert Watson #include <security/mac/mac_framework.h>
120aed55708SRobert Watson 
121dbc42409SLawrence Stewart const int tcprexmtthresh = 3;
1220312fbe9SPoul-Henning Kamp 
123eddfbb76SRobert Watson VNET_DEFINE(struct tcpstat, tcpstat);
124eddfbb76SRobert Watson SYSCTL_VNET_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW,
125eddfbb76SRobert Watson     &VNET_NAME(tcpstat), tcpstat,
1268b615593SMarko Zec     "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
1270312fbe9SPoul-Henning Kamp 
128773673c1SAndre Oppermann int tcp_log_in_vain = 0;
129816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
130eddfbb76SRobert Watson     &tcp_log_in_vain, 0,
131eddfbb76SRobert Watson     "Log all incoming TCP segments to closed ports");
132816a3d83SPoul-Henning Kamp 
13382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, blackhole) = 0;
13482cea7e6SBjoern A. Zeeb #define	V_blackhole		VNET(blackhole)
135eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW,
136eddfbb76SRobert Watson     &VNET_NAME(blackhole), 0,
137eddfbb76SRobert Watson     "Do not send RST on segments to closed ports");
13816f7f31fSGeoff Rehmet 
13982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_delack_enabled) = 1;
140eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW,
141eddfbb76SRobert Watson     &VNET_NAME(tcp_delack_enabled), 0,
1423d177f46SBill Fumerola     "Delay ACK to try and piggyback it onto a data packet");
143f498eeeeSDavid Greenman 
14482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, drop_synfin) = 0;
14582cea7e6SBjoern A. Zeeb #define	V_drop_synfin		VNET(drop_synfin)
146eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW,
147eddfbb76SRobert Watson     &VNET_NAME(drop_synfin), 0,
148eddfbb76SRobert Watson     "Drop TCP packets with SYN+FIN set");
149f8613305SDag-Erling Smørgrav 
15082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1;
15182cea7e6SBjoern A. Zeeb #define	V_tcp_do_rfc3042	VNET(tcp_do_rfc3042)
152eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW,
153eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3042), 0,
154eddfbb76SRobert Watson     "Enable RFC 3042 (Limited Transmit)");
155582a954bSJeffrey Hsu 
15682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1;
157eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW,
158eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3390), 0,
159da3a8a1aSJeffrey Hsu     "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
160da3a8a1aSJeffrey Hsu 
16182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1;
162eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_RW,
163eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3465), 0,
16424cb0f22SLawrence Stewart     "Enable RFC 3465 (Appropriate Byte Counting)");
165eddfbb76SRobert Watson 
16682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2;
167eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_RW,
168eddfbb76SRobert Watson     &VNET_NAME(tcp_abc_l_var), 2,
16924cb0f22SLawrence Stewart     "Cap the max cwnd increment during slow-start to this number of segments");
17024cb0f22SLawrence Stewart 
1716472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN");
172f2512ba1SRui Paulo 
17382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_ecn) = 0;
174eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_RW,
175eddfbb76SRobert Watson     &VNET_NAME(tcp_do_ecn), 0,
176eddfbb76SRobert Watson     "TCP ECN support");
177eddfbb76SRobert Watson 
17882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_ecn_maxretries) = 1;
179eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_RW,
180eddfbb76SRobert Watson     &VNET_NAME(tcp_ecn_maxretries), 0,
181eddfbb76SRobert Watson     "Max retries before giving up on ECN");
182eddfbb76SRobert Watson 
18382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0;
18482cea7e6SBjoern A. Zeeb #define	V_tcp_insecure_rst	VNET(tcp_insecure_rst)
185eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_RW,
186eddfbb76SRobert Watson     &VNET_NAME(tcp_insecure_rst), 0,
1876489fe65SAndre Oppermann     "Follow the old (insecure) criteria for accepting RST packets");
188a69968eeSMike Silbersack 
189fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64;
190873789cbSAndre Oppermann #define	V_tcp_recvspace	VNET(tcp_recvspace)
191ddd0c4a9SSergey Kandaurov SYSCTL_VNET_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
192873789cbSAndre Oppermann     &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size");
193873789cbSAndre Oppermann 
19482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1;
19582cea7e6SBjoern A. Zeeb #define	V_tcp_do_autorcvbuf	VNET(tcp_do_autorcvbuf)
196eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_RW,
197eddfbb76SRobert Watson     &VNET_NAME(tcp_do_autorcvbuf), 0,
1988b615593SMarko Zec     "Enable automatic receive buffer sizing");
1996741ecf5SAndre Oppermann 
20082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_inc) = 16*1024;
20182cea7e6SBjoern A. Zeeb #define	V_tcp_autorcvbuf_inc	VNET(tcp_autorcvbuf_inc)
202eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_RW,
203eddfbb76SRobert Watson     &VNET_NAME(tcp_autorcvbuf_inc), 0,
2046489fe65SAndre Oppermann     "Incrementor step size of automatic receive buffer");
2056741ecf5SAndre Oppermann 
206b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024;
20782cea7e6SBjoern A. Zeeb #define	V_tcp_autorcvbuf_max	VNET(tcp_autorcvbuf_max)
208eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_RW,
209eddfbb76SRobert Watson     &VNET_NAME(tcp_autorcvbuf_max), 0,
2108b615593SMarko Zec     "Max size of automatic receive buffer");
2116741ecf5SAndre Oppermann 
212eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, tcb);
21344e33a07SMarko Zec #define	tcb6	tcb  /* for KAME src sync over BSD*'s */
21482cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo);
215df8bae1dSRodney W. Grimes 
2165a53ca16SPaul Saab static void	 tcp_dooptions(struct tcpopt *, u_char *, int, int);
217679d9708SAndre Oppermann static void	 tcp_do_segment(struct mbuf *, struct tcphdr *,
218252ca428SRobert Watson 		     struct socket *, struct tcpcb *, int, int, uint8_t,
219252ca428SRobert Watson 		     int);
220302ce8d6SAndre Oppermann static void	 tcp_dropwithreset(struct mbuf *, struct tcphdr *,
221302ce8d6SAndre Oppermann 		     struct tcpcb *, int, int);
2224d77a549SAlfred Perlstein static void	 tcp_pulloutofband(struct socket *,
2234d77a549SAlfred Perlstein 		     struct tcphdr *, struct mbuf *, int);
2244d77a549SAlfred Perlstein static void	 tcp_xmit_timer(struct tcpcb *, int);
225c068736aSJeffrey Hsu static void	 tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *);
2262903309aSAttilio Rao static void inline 	tcp_fields_to_host(struct tcphdr *);
2272903309aSAttilio Rao #ifdef TCP_SIGNATURE
2282903309aSAttilio Rao static void inline 	tcp_fields_to_net(struct tcphdr *);
2292903309aSAttilio Rao static int inline	tcp_signature_verify_input(struct mbuf *, int, int,
2302903309aSAttilio Rao 			    int, struct tcpopt *, struct tcphdr *, u_int);
2312903309aSAttilio Rao #endif
232dbc42409SLawrence Stewart static void inline	cc_ack_received(struct tcpcb *tp, struct tcphdr *th,
233dbc42409SLawrence Stewart 			    uint16_t type);
234dbc42409SLawrence Stewart static void inline	cc_conn_init(struct tcpcb *tp);
235dbc42409SLawrence Stewart static void inline	cc_post_recovery(struct tcpcb *tp, struct tcphdr *th);
23639bc9de5SLawrence Stewart static void inline	hhook_run_tcp_est_in(struct tcpcb *tp,
23739bc9de5SLawrence Stewart 			    struct tcphdr *th, struct tcpopt *to);
238f2512ba1SRui Paulo 
239315e3e38SRobert Watson /*
240315e3e38SRobert Watson  * Kernel module interface for updating tcpstat.  The argument is an index
241315e3e38SRobert Watson  * into tcpstat treated as an array of u_long.  While this encodes the
242315e3e38SRobert Watson  * general layout of tcpstat into the caller, it doesn't encode its location,
243315e3e38SRobert Watson  * so that future changes to add, for example, per-CPU stats support won't
244315e3e38SRobert Watson  * cause binary compatibility problems for kernel modules.
245315e3e38SRobert Watson  */
246315e3e38SRobert Watson void
247315e3e38SRobert Watson kmod_tcpstat_inc(int statnum)
248315e3e38SRobert Watson {
249315e3e38SRobert Watson 
250315e3e38SRobert Watson 	(*((u_long *)&V_tcpstat + statnum))++;
251315e3e38SRobert Watson }
252315e3e38SRobert Watson 
253dbc42409SLawrence Stewart /*
25439bc9de5SLawrence Stewart  * Wrapper for the TCP established input helper hook.
25539bc9de5SLawrence Stewart  */
25639bc9de5SLawrence Stewart static void inline
25739bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to)
25839bc9de5SLawrence Stewart {
25939bc9de5SLawrence Stewart 	struct tcp_hhook_data hhook_data;
26039bc9de5SLawrence Stewart 
26139bc9de5SLawrence Stewart 	if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) {
26239bc9de5SLawrence Stewart 		hhook_data.tp = tp;
26339bc9de5SLawrence Stewart 		hhook_data.th = th;
26439bc9de5SLawrence Stewart 		hhook_data.to = to;
26539bc9de5SLawrence Stewart 
26639bc9de5SLawrence Stewart 		hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data,
26739bc9de5SLawrence Stewart 		    tp->osd);
26839bc9de5SLawrence Stewart 	}
26939bc9de5SLawrence Stewart }
27039bc9de5SLawrence Stewart 
27139bc9de5SLawrence Stewart /*
272dbc42409SLawrence Stewart  * CC wrapper hook functions
273dbc42409SLawrence Stewart  */
274f2512ba1SRui Paulo static void inline
275dbc42409SLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t type)
276f2512ba1SRui Paulo {
277dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
278f2512ba1SRui Paulo 
279dbc42409SLawrence Stewart 	tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th);
280dbc42409SLawrence Stewart 	if (tp->snd_cwnd == min(tp->snd_cwnd, tp->snd_wnd))
281dbc42409SLawrence Stewart 		tp->ccv->flags |= CCF_CWND_LIMITED;
282dbc42409SLawrence Stewart 	else
283dbc42409SLawrence Stewart 		tp->ccv->flags &= ~CCF_CWND_LIMITED;
284dbc42409SLawrence Stewart 
285dbc42409SLawrence Stewart 	if (type == CC_ACK) {
286dbc42409SLawrence Stewart 		if (tp->snd_cwnd > tp->snd_ssthresh) {
287dbc42409SLawrence Stewart 			tp->t_bytes_acked += min(tp->ccv->bytes_this_ack,
288dbc42409SLawrence Stewart 			     V_tcp_abc_l_var * tp->t_maxseg);
289dbc42409SLawrence Stewart 			if (tp->t_bytes_acked >= tp->snd_cwnd) {
290dbc42409SLawrence Stewart 				tp->t_bytes_acked -= tp->snd_cwnd;
291dbc42409SLawrence Stewart 				tp->ccv->flags |= CCF_ABC_SENTAWND;
292dbc42409SLawrence Stewart 			}
293dbc42409SLawrence Stewart 		} else {
294dbc42409SLawrence Stewart 				tp->ccv->flags &= ~CCF_ABC_SENTAWND;
295dbc42409SLawrence Stewart 				tp->t_bytes_acked = 0;
296dbc42409SLawrence Stewart 		}
297dbc42409SLawrence Stewart 	}
298dbc42409SLawrence Stewart 
299dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->ack_received != NULL) {
300dbc42409SLawrence Stewart 		/* XXXLAS: Find a way to live without this */
301dbc42409SLawrence Stewart 		tp->ccv->curack = th->th_ack;
302dbc42409SLawrence Stewart 		CC_ALGO(tp)->ack_received(tp->ccv, type);
303dbc42409SLawrence Stewart 	}
304dbc42409SLawrence Stewart }
305dbc42409SLawrence Stewart 
306dbc42409SLawrence Stewart static void inline
307dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp)
308dbc42409SLawrence Stewart {
309dbc42409SLawrence Stewart 	struct hc_metrics_lite metrics;
310dbc42409SLawrence Stewart 	struct inpcb *inp = tp->t_inpcb;
311dbc42409SLawrence Stewart 	int rtt;
312dbc42409SLawrence Stewart 
313dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
314dbc42409SLawrence Stewart 
315dbc42409SLawrence Stewart 	tcp_hc_get(&inp->inp_inc, &metrics);
316dbc42409SLawrence Stewart 
317dbc42409SLawrence Stewart 	if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) {
318dbc42409SLawrence Stewart 		tp->t_srtt = rtt;
319dbc42409SLawrence Stewart 		tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
320dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedrtt);
321dbc42409SLawrence Stewart 		if (metrics.rmx_rttvar) {
322dbc42409SLawrence Stewart 			tp->t_rttvar = metrics.rmx_rttvar;
323dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_usedrttvar);
324dbc42409SLawrence Stewart 		} else {
325dbc42409SLawrence Stewart 			/* default variation is +- 1 rtt */
326dbc42409SLawrence Stewart 			tp->t_rttvar =
327dbc42409SLawrence Stewart 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
328dbc42409SLawrence Stewart 		}
329dbc42409SLawrence Stewart 		TCPT_RANGESET(tp->t_rxtcur,
330dbc42409SLawrence Stewart 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
331dbc42409SLawrence Stewart 		    tp->t_rttmin, TCPTV_REXMTMAX);
332dbc42409SLawrence Stewart 	}
333dbc42409SLawrence Stewart 	if (metrics.rmx_ssthresh) {
334dbc42409SLawrence Stewart 		/*
335dbc42409SLawrence Stewart 		 * There's some sort of gateway or interface
336dbc42409SLawrence Stewart 		 * buffer limit on the path.  Use this to set
337dbc42409SLawrence Stewart 		 * the slow start threshhold, but set the
338dbc42409SLawrence Stewart 		 * threshold to no less than 2*mss.
339dbc42409SLawrence Stewart 		 */
340dbc42409SLawrence Stewart 		tp->snd_ssthresh = max(2 * tp->t_maxseg, metrics.rmx_ssthresh);
341dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedssthresh);
342dbc42409SLawrence Stewart 	}
343dbc42409SLawrence Stewart 
344dbc42409SLawrence Stewart 	/*
3459ec4a4ccSAndre Oppermann 	 * Set the initial slow-start flight size.
346dbc42409SLawrence Stewart 	 *
347dbc42409SLawrence Stewart 	 * RFC3390 says only do this if SYN or SYN/ACK didn't got lost.
3489ec4a4ccSAndre Oppermann 	 * XXX: We currently check only in syncache_socket for that.
349dbc42409SLawrence Stewart 	 */
350dbc42409SLawrence Stewart 	if (V_tcp_do_rfc3390)
351dbc42409SLawrence Stewart 		tp->snd_cwnd = min(4 * tp->t_maxseg,
352dbc42409SLawrence Stewart 		    max(2 * tp->t_maxseg, 4380));
353dbc42409SLawrence Stewart 	else
3549ec4a4ccSAndre Oppermann 		tp->snd_cwnd = tp->t_maxseg;
355dbc42409SLawrence Stewart 
356dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->conn_init != NULL)
357dbc42409SLawrence Stewart 		CC_ALGO(tp)->conn_init(tp->ccv);
358dbc42409SLawrence Stewart }
359dbc42409SLawrence Stewart 
360dbc42409SLawrence Stewart void inline
361dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type)
362dbc42409SLawrence Stewart {
363dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
364dbc42409SLawrence Stewart 
365dbc42409SLawrence Stewart 	switch(type) {
366dbc42409SLawrence Stewart 	case CC_NDUPACK:
367dbc42409SLawrence Stewart 		if (!IN_FASTRECOVERY(tp->t_flags)) {
368f2512ba1SRui Paulo 			tp->snd_recover = tp->snd_max;
369f2512ba1SRui Paulo 			if (tp->t_flags & TF_ECN_PERMIT)
370f2512ba1SRui Paulo 				tp->t_flags |= TF_ECN_SND_CWR;
371f2512ba1SRui Paulo 		}
372dbc42409SLawrence Stewart 		break;
373dbc42409SLawrence Stewart 	case CC_ECN:
374dbc42409SLawrence Stewart 		if (!IN_CONGRECOVERY(tp->t_flags)) {
375dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_ecn_rcwnd);
376dbc42409SLawrence Stewart 			tp->snd_recover = tp->snd_max;
377dbc42409SLawrence Stewart 			if (tp->t_flags & TF_ECN_PERMIT)
378dbc42409SLawrence Stewart 				tp->t_flags |= TF_ECN_SND_CWR;
379dbc42409SLawrence Stewart 		}
380dbc42409SLawrence Stewart 		break;
381dbc42409SLawrence Stewart 	case CC_RTO:
382dbc42409SLawrence Stewart 		tp->t_dupacks = 0;
383dbc42409SLawrence Stewart 		tp->t_bytes_acked = 0;
384dbc42409SLawrence Stewart 		EXIT_RECOVERY(tp->t_flags);
3856157935fSLawrence Stewart 		tp->snd_ssthresh = max(2, min(tp->snd_wnd, tp->snd_cwnd) / 2 /
3866157935fSLawrence Stewart 		    tp->t_maxseg) * tp->t_maxseg;
387dbc42409SLawrence Stewart 		tp->snd_cwnd = tp->t_maxseg;
388dbc42409SLawrence Stewart 		break;
389dbc42409SLawrence Stewart 	case CC_RTO_ERR:
390dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_sndrexmitbad);
391dbc42409SLawrence Stewart 		/* RTO was unnecessary, so reset everything. */
392dbc42409SLawrence Stewart 		tp->snd_cwnd = tp->snd_cwnd_prev;
393dbc42409SLawrence Stewart 		tp->snd_ssthresh = tp->snd_ssthresh_prev;
394dbc42409SLawrence Stewart 		tp->snd_recover = tp->snd_recover_prev;
395dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASFRECOVERY)
396dbc42409SLawrence Stewart 			ENTER_FASTRECOVERY(tp->t_flags);
397dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASCRECOVERY)
398dbc42409SLawrence Stewart 			ENTER_CONGRECOVERY(tp->t_flags);
399dbc42409SLawrence Stewart 		tp->snd_nxt = tp->snd_max;
400672dc4aeSJohn Baldwin 		tp->t_flags &= ~TF_PREVVALID;
401dbc42409SLawrence Stewart 		tp->t_badrxtwin = 0;
402dbc42409SLawrence Stewart 		break;
403dbc42409SLawrence Stewart 	}
404dbc42409SLawrence Stewart 
405dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->cong_signal != NULL) {
406dbc42409SLawrence Stewart 		if (th != NULL)
407dbc42409SLawrence Stewart 			tp->ccv->curack = th->th_ack;
408dbc42409SLawrence Stewart 		CC_ALGO(tp)->cong_signal(tp->ccv, type);
409dbc42409SLawrence Stewart 	}
410dbc42409SLawrence Stewart }
411dbc42409SLawrence Stewart 
412dbc42409SLawrence Stewart static void inline
413dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th)
414dbc42409SLawrence Stewart {
415dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
416dbc42409SLawrence Stewart 
417dbc42409SLawrence Stewart 	/* XXXLAS: KASSERT that we're in recovery? */
418dbc42409SLawrence Stewart 
419dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->post_recovery != NULL) {
420dbc42409SLawrence Stewart 		tp->ccv->curack = th->th_ack;
421dbc42409SLawrence Stewart 		CC_ALGO(tp)->post_recovery(tp->ccv);
422dbc42409SLawrence Stewart 	}
423dbc42409SLawrence Stewart 	/* XXXLAS: EXIT_RECOVERY ? */
424dbc42409SLawrence Stewart 	tp->t_bytes_acked = 0;
425dbc42409SLawrence Stewart }
4260312fbe9SPoul-Henning Kamp 
4272903309aSAttilio Rao static inline void
4282903309aSAttilio Rao tcp_fields_to_host(struct tcphdr *th)
4292903309aSAttilio Rao {
4302903309aSAttilio Rao 
4312903309aSAttilio Rao 	th->th_seq = ntohl(th->th_seq);
4322903309aSAttilio Rao 	th->th_ack = ntohl(th->th_ack);
4332903309aSAttilio Rao 	th->th_win = ntohs(th->th_win);
4342903309aSAttilio Rao 	th->th_urp = ntohs(th->th_urp);
4352903309aSAttilio Rao }
4362903309aSAttilio Rao 
4372903309aSAttilio Rao #ifdef TCP_SIGNATURE
4382903309aSAttilio Rao static inline void
4392903309aSAttilio Rao tcp_fields_to_net(struct tcphdr *th)
4402903309aSAttilio Rao {
4412903309aSAttilio Rao 
4422903309aSAttilio Rao 	th->th_seq = htonl(th->th_seq);
4432903309aSAttilio Rao 	th->th_ack = htonl(th->th_ack);
4442903309aSAttilio Rao 	th->th_win = htons(th->th_win);
4452903309aSAttilio Rao 	th->th_urp = htons(th->th_urp);
4462903309aSAttilio Rao }
4472903309aSAttilio Rao 
4482903309aSAttilio Rao static inline int
4492903309aSAttilio Rao tcp_signature_verify_input(struct mbuf *m, int off0, int tlen, int optlen,
4502903309aSAttilio Rao     struct tcpopt *to, struct tcphdr *th, u_int tcpbflag)
4512903309aSAttilio Rao {
4522903309aSAttilio Rao 	int ret;
4532903309aSAttilio Rao 
4542903309aSAttilio Rao 	tcp_fields_to_net(th);
4552903309aSAttilio Rao 	ret = tcp_signature_verify(m, off0, tlen, optlen, to, th, tcpbflag);
4562903309aSAttilio Rao 	tcp_fields_to_host(th);
4572903309aSAttilio Rao 	return (ret);
4582903309aSAttilio Rao }
4592903309aSAttilio Rao #endif
4602903309aSAttilio Rao 
461fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
462fb59c426SYoshinobu Inoue #ifdef INET6
463fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \
464fb59c426SYoshinobu Inoue do { \
465fb59c426SYoshinobu Inoue 	if ((tp) && (tp)->t_inpcb && \
46697d8d152SAndre Oppermann 	    ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \
46797d8d152SAndre Oppermann 		nd6_nud_hint(NULL, NULL, 0); \
468fb59c426SYoshinobu Inoue } while (0)
469fb59c426SYoshinobu Inoue #else
470fb59c426SYoshinobu Inoue #define ND6_HINT(tp)
471fb59c426SYoshinobu Inoue #endif
472df8bae1dSRodney W. Grimes 
473df8bae1dSRodney W. Grimes /*
474262c1c1aSMatthew Dillon  * Indicate whether this ack should be delayed.  We can delay the ack if
475262c1c1aSMatthew Dillon  *	- there is no delayed ack timer in progress and
476262c1c1aSMatthew Dillon  *	- our last ack wasn't a 0-sized window.  We never want to delay
4773bfd6421SJonathan Lemon  *	  the ack that opens up a 0-sized window and
4783bfd6421SJonathan Lemon  *		- delayed acks are enabled or
4793bfd6421SJonathan Lemon  *		- this is a half-synchronized T/TCP connection.
480d8c85a26SJonathan Lemon  */
481d8c85a26SJonathan Lemon #define DELAY_ACK(tp)							\
482b8152ba7SAndre Oppermann 	((!tcp_timer_active(tp, TT_DELACK) &&				\
483a14c749fSJonathan Lemon 	    (tp->t_flags & TF_RXWIN0SENT) == 0) &&			\
484603724d3SBjoern A. Zeeb 	    (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
485d8c85a26SJonathan Lemon 
486df8bae1dSRodney W. Grimes /*
4878d573cc1SAndre Oppermann  * TCP input handling is split into multiple parts:
4888d573cc1SAndre Oppermann  *   tcp6_input is a thin wrapper around tcp_input for the extended
4898d573cc1SAndre Oppermann  *	ip6_protox[] call format in ip6_input
4908d573cc1SAndre Oppermann  *   tcp_input handles primary segment validation, inpcb lookup and
4918d573cc1SAndre Oppermann  *	SYN processing on listen sockets
4928d573cc1SAndre Oppermann  *   tcp_do_segment processes the ACK and text of the segment for
4938d573cc1SAndre Oppermann  *	establishing, established and closing connections
494df8bae1dSRodney W. Grimes  */
495fb59c426SYoshinobu Inoue #ifdef INET6
496fb59c426SYoshinobu Inoue int
497ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto)
498fb59c426SYoshinobu Inoue {
499ad3f9ab3SAndre Oppermann 	struct mbuf *m = *mp;
50033841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
501fb59c426SYoshinobu Inoue 
502fb59c426SYoshinobu Inoue 	IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
503fb59c426SYoshinobu Inoue 
504fb59c426SYoshinobu Inoue 	/*
505fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
506fb59c426SYoshinobu Inoue 	 * better place to put this in?
507fb59c426SYoshinobu Inoue 	 */
50833841545SHajimu UMEMOTO 	ia6 = ip6_getdstifaddr(m);
50933841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
510fb59c426SYoshinobu Inoue 		struct ip6_hdr *ip6;
511fb59c426SYoshinobu Inoue 
5128c0fec80SRobert Watson 		ifa_free(&ia6->ia_ifa);
513fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
514fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
515fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
516fb59c426SYoshinobu Inoue 		return IPPROTO_DONE;
517fb59c426SYoshinobu Inoue 	}
51877d396fdSMaksim Yevmenkin 	if (ia6)
51977d396fdSMaksim Yevmenkin 		ifa_free(&ia6->ia_ifa);
520fb59c426SYoshinobu Inoue 
521f0ffb944SJulian Elischer 	tcp_input(m, *offp);
522fb59c426SYoshinobu Inoue 	return IPPROTO_DONE;
523fb59c426SYoshinobu Inoue }
524b287c6c7SBjoern A. Zeeb #endif /* INET6 */
525fb59c426SYoshinobu Inoue 
526df8bae1dSRodney W. Grimes void
527ad3f9ab3SAndre Oppermann tcp_input(struct mbuf *m, int off0)
528df8bae1dSRodney W. Grimes {
529b287c6c7SBjoern A. Zeeb 	struct tcphdr *th = NULL;
530ad3f9ab3SAndre Oppermann 	struct ip *ip = NULL;
531ad3f9ab3SAndre Oppermann 	struct inpcb *inp = NULL;
532302ce8d6SAndre Oppermann 	struct tcpcb *tp = NULL;
533302ce8d6SAndre Oppermann 	struct socket *so = NULL;
534e79adb8eSGarrett Wollman 	u_char *optp = NULL;
53526f9a767SRodney W. Grimes 	int optlen = 0;
536b287c6c7SBjoern A. Zeeb #ifdef INET
537b287c6c7SBjoern A. Zeeb 	int len;
538b287c6c7SBjoern A. Zeeb #endif
539b287c6c7SBjoern A. Zeeb 	int tlen = 0, off;
540fb59c426SYoshinobu Inoue 	int drop_hdrlen;
541ad3f9ab3SAndre Oppermann 	int thflags;
542302ce8d6SAndre Oppermann 	int rstreason = 0;	/* For badport_bandlim accounting purposes */
5432903309aSAttilio Rao #ifdef TCP_SIGNATURE
5442903309aSAttilio Rao 	uint8_t sig_checked = 0;
5452903309aSAttilio Rao #endif
546b287c6c7SBjoern A. Zeeb 	uint8_t iptos = 0;
5479b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
5489b932e9eSAndre Oppermann 	struct m_tag *fwd_tag;
5499b932e9eSAndre Oppermann #endif
550c068736aSJeffrey Hsu #ifdef INET6
551302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6 = NULL;
552c068736aSJeffrey Hsu 	int isipv6;
553c068736aSJeffrey Hsu #else
554a160e630SAndre Oppermann 	const void *ip6 = NULL;
555b287c6c7SBjoern A. Zeeb #endif /* INET6 */
556302ce8d6SAndre Oppermann 	struct tcpopt to;		/* options in this segment */
557a160e630SAndre Oppermann 	char *s = NULL;			/* address and port logging */
558252ca428SRobert Watson 	int ti_locked;
559252ca428SRobert Watson #define	TI_UNLOCKED	1
560fa046d87SRobert Watson #define	TI_WLOCKED	2
561f76fcf6dSJeffrey Hsu 
562610ee2f9SDavid Greenman #ifdef TCPDEBUG
563c068736aSJeffrey Hsu 	/*
564c068736aSJeffrey Hsu 	 * The size of tcp_saveipgen must be the size of the max ip header,
565c068736aSJeffrey Hsu 	 * now IPv6.
566c068736aSJeffrey Hsu 	 */
56714739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
568410bb1bfSLuigi Rizzo 	struct tcphdr tcp_savetcp;
569610ee2f9SDavid Greenman 	short ostate = 0;
570610ee2f9SDavid Greenman #endif
571c068736aSJeffrey Hsu 
572fb59c426SYoshinobu Inoue #ifdef INET6
573fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
574fb59c426SYoshinobu Inoue #endif
575a0292f23SGarrett Wollman 
576302ce8d6SAndre Oppermann 	to.to_flags = 0;
57778b50714SRobert Watson 	TCPSTAT_INC(tcps_rcvtotal);
578fb59c426SYoshinobu Inoue 
57904d3a452SHajimu UMEMOTO #ifdef INET6
580b287c6c7SBjoern A. Zeeb 	if (isipv6) {
5818d573cc1SAndre Oppermann 		/* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */
58245747ba5SBjoern A. Zeeb 
58345747ba5SBjoern A. Zeeb 		if (m->m_len < (sizeof(*ip6) + sizeof(*th))) {
58445747ba5SBjoern A. Zeeb 			m = m_pullup(m, sizeof(*ip6) + sizeof(*th));
58545747ba5SBjoern A. Zeeb 			if (m == NULL) {
58645747ba5SBjoern A. Zeeb 				TCPSTAT_INC(tcps_rcvshort);
58745747ba5SBjoern A. Zeeb 				return;
58845747ba5SBjoern A. Zeeb 			}
58945747ba5SBjoern A. Zeeb 		}
59045747ba5SBjoern A. Zeeb 
591fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
59245747ba5SBjoern A. Zeeb 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
593fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
594356ab07eSBjoern A. Zeeb 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) {
59545747ba5SBjoern A. Zeeb 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
59645747ba5SBjoern A. Zeeb 				th->th_sum = m->m_pkthdr.csum_data;
59745747ba5SBjoern A. Zeeb 			else
59845747ba5SBjoern A. Zeeb 				th->th_sum = in6_cksum_pseudo(ip6, tlen,
59945747ba5SBjoern A. Zeeb 				    IPPROTO_TCP, m->m_pkthdr.csum_data);
60045747ba5SBjoern A. Zeeb 			th->th_sum ^= 0xffff;
60145747ba5SBjoern A. Zeeb 		} else
60245747ba5SBjoern A. Zeeb 			th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen);
60345747ba5SBjoern A. Zeeb 		if (th->th_sum) {
60478b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
605fb59c426SYoshinobu Inoue 			goto drop;
606fb59c426SYoshinobu Inoue 		}
60733841545SHajimu UMEMOTO 
60833841545SHajimu UMEMOTO 		/*
60933841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
61033841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
61133841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
61233841545SHajimu UMEMOTO 		 *
61333841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
61433841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
61533841545SHajimu UMEMOTO 		 */
61633841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
61733841545SHajimu UMEMOTO 			/* XXX stat */
61833841545SHajimu UMEMOTO 			goto drop;
61933841545SHajimu UMEMOTO 		}
620b287c6c7SBjoern A. Zeeb 	}
62104d3a452SHajimu UMEMOTO #endif
622b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
623b287c6c7SBjoern A. Zeeb 	else
624b287c6c7SBjoern A. Zeeb #endif
625b287c6c7SBjoern A. Zeeb #ifdef INET
626b287c6c7SBjoern A. Zeeb 	{
627df8bae1dSRodney W. Grimes 		/*
628df8bae1dSRodney W. Grimes 		 * Get IP and TCP header together in first mbuf.
629df8bae1dSRodney W. Grimes 		 * Note: IP leaves IP header in first mbuf.
630df8bae1dSRodney W. Grimes 		 */
631fb59c426SYoshinobu Inoue 		if (off0 > sizeof (struct ip)) {
632105bd211SGleb Smirnoff 			ip_stripoptions(m);
633fb59c426SYoshinobu Inoue 			off0 = sizeof(struct ip);
634fb59c426SYoshinobu Inoue 		}
635df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof (struct tcpiphdr)) {
6364dfdffe9SAndre Oppermann 			if ((m = m_pullup(m, sizeof (struct tcpiphdr)))
6374dfdffe9SAndre Oppermann 			    == NULL) {
63878b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvshort);
639df8bae1dSRodney W. Grimes 				return;
640df8bae1dSRodney W. Grimes 			}
641df8bae1dSRodney W. Grimes 		}
642fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
643db4f9cc7SJonathan Lemon 		th = (struct tcphdr *)((caddr_t)ip + off0);
644*8f134647SGleb Smirnoff 		tlen = ntohs(ip->ip_len);
645df8bae1dSRodney W. Grimes 
646db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
647db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
648db4f9cc7SJonathan Lemon 				th->th_sum = m->m_pkthdr.csum_data;
649db4f9cc7SJonathan Lemon 			else
650db4f9cc7SJonathan Lemon 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
651c068736aSJeffrey Hsu 				    ip->ip_dst.s_addr,
652*8f134647SGleb Smirnoff 				    htonl(m->m_pkthdr.csum_data + tlen +
653c068736aSJeffrey Hsu 				    IPPROTO_TCP));
654db4f9cc7SJonathan Lemon 			th->th_sum ^= 0xffff;
655db4f9cc7SJonathan Lemon 		} else {
656*8f134647SGleb Smirnoff 			struct ipovly *ipov = (struct ipovly *)ip;
657*8f134647SGleb Smirnoff 
658df8bae1dSRodney W. Grimes 			/*
659df8bae1dSRodney W. Grimes 			 * Checksum extended TCP header and data.
660df8bae1dSRodney W. Grimes 			 */
661*8f134647SGleb Smirnoff 			len = off0 + tlen;
662fb59c426SYoshinobu Inoue 			bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
663*8f134647SGleb Smirnoff 			ipov->ih_len = htons(tlen);
664fb59c426SYoshinobu Inoue 			th->th_sum = in_cksum(m, len);
665db4f9cc7SJonathan Lemon 		}
666fb59c426SYoshinobu Inoue 		if (th->th_sum) {
66778b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
668df8bae1dSRodney W. Grimes 			goto drop;
669df8bae1dSRodney W. Grimes 		}
670fb59c426SYoshinobu Inoue 		/* Re-initialization for later version check */
671fb59c426SYoshinobu Inoue 		ip->ip_v = IPVERSION;
672fb59c426SYoshinobu Inoue 	}
673b287c6c7SBjoern A. Zeeb #endif /* INET */
674df8bae1dSRodney W. Grimes 
675f2512ba1SRui Paulo #ifdef INET6
676f2512ba1SRui Paulo 	if (isipv6)
677f2512ba1SRui Paulo 		iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
678b287c6c7SBjoern A. Zeeb #endif
679b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
680f2512ba1SRui Paulo 	else
681f2512ba1SRui Paulo #endif
682b287c6c7SBjoern A. Zeeb #ifdef INET
683f2512ba1SRui Paulo 		iptos = ip->ip_tos;
684b287c6c7SBjoern A. Zeeb #endif
685f2512ba1SRui Paulo 
686df8bae1dSRodney W. Grimes 	/*
687df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
688df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
689df8bae1dSRodney W. Grimes 	 */
690fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
691df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
69278b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvbadoff);
693df8bae1dSRodney W. Grimes 		goto drop;
694df8bae1dSRodney W. Grimes 	}
695fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
696df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
69704d3a452SHajimu UMEMOTO #ifdef INET6
698b287c6c7SBjoern A. Zeeb 		if (isipv6) {
699fb59c426SYoshinobu Inoue 			IP6_EXTHDR_CHECK(m, off0, off, );
700fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
701fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
702b287c6c7SBjoern A. Zeeb 		}
70304d3a452SHajimu UMEMOTO #endif
704b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
705b287c6c7SBjoern A. Zeeb 		else
706b287c6c7SBjoern A. Zeeb #endif
707b287c6c7SBjoern A. Zeeb #ifdef INET
708b287c6c7SBjoern A. Zeeb 		{
709df8bae1dSRodney W. Grimes 			if (m->m_len < sizeof(struct ip) + off) {
710c068736aSJeffrey Hsu 				if ((m = m_pullup(m, sizeof (struct ip) + off))
7114dfdffe9SAndre Oppermann 				    == NULL) {
71278b50714SRobert Watson 					TCPSTAT_INC(tcps_rcvshort);
713df8bae1dSRodney W. Grimes 					return;
714df8bae1dSRodney W. Grimes 				}
715fb59c426SYoshinobu Inoue 				ip = mtod(m, struct ip *);
716fb59c426SYoshinobu Inoue 				th = (struct tcphdr *)((caddr_t)ip + off0);
717fb59c426SYoshinobu Inoue 			}
718df8bae1dSRodney W. Grimes 		}
719b287c6c7SBjoern A. Zeeb #endif
720df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
721fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
722df8bae1dSRodney W. Grimes 	}
723fb59c426SYoshinobu Inoue 	thflags = th->th_flags;
724df8bae1dSRodney W. Grimes 
725e46cd3d4SDag-Erling Smørgrav 	/*
726df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
727df8bae1dSRodney W. Grimes 	 */
7282903309aSAttilio Rao 	tcp_fields_to_host(th);
729df8bae1dSRodney W. Grimes 
730df8bae1dSRodney W. Grimes 	/*
731794235b7SAndre Oppermann 	 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options.
732755c1f07SAndras Olah 	 */
733fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
734755c1f07SAndras Olah 
735755c1f07SAndras Olah 	/*
736fa046d87SRobert Watson 	 * Locate pcb for segment; if we're likely to add or remove a
737fa046d87SRobert Watson 	 * connection then first acquire pcbinfo lock.  There are two cases
738fa046d87SRobert Watson 	 * where we might discover later we need a write lock despite the
739fa046d87SRobert Watson 	 * flags: ACKs moving a connection out of the syncache, and ACKs for
740fa046d87SRobert Watson 	 * a connection in TIMEWAIT.
741df8bae1dSRodney W. Grimes 	 */
742fa046d87SRobert Watson 	if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0) {
743603724d3SBjoern A. Zeeb 		INP_INFO_WLOCK(&V_tcbinfo);
744252ca428SRobert Watson 		ti_locked = TI_WLOCKED;
745fa046d87SRobert Watson 	} else
746fa046d87SRobert Watson 		ti_locked = TI_UNLOCKED;
747252ca428SRobert Watson 
748df8bae1dSRodney W. Grimes findpcb:
749252ca428SRobert Watson #ifdef INVARIANTS
750fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED) {
751603724d3SBjoern A. Zeeb 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
752fa046d87SRobert Watson 	} else {
753fa046d87SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
754fa046d87SRobert Watson 	}
755252ca428SRobert Watson #endif
756252ca428SRobert Watson 
7579b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
7588d573cc1SAndre Oppermann 	/*
7598d573cc1SAndre Oppermann 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
7608d573cc1SAndre Oppermann 	 */
7619b932e9eSAndre Oppermann 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
7628a006adbSBjoern A. Zeeb #endif /* IPFIREWALL_FORWARD */
7639b932e9eSAndre Oppermann 
7648a006adbSBjoern A. Zeeb #ifdef INET6
7658a006adbSBjoern A. Zeeb #ifdef IPFIREWALL_FORWARD
7668a006adbSBjoern A. Zeeb 	if (isipv6 && fwd_tag != NULL) {
7678a006adbSBjoern A. Zeeb 		struct sockaddr_in6 *next_hop6;
7688a006adbSBjoern A. Zeeb 
7698a006adbSBjoern A. Zeeb 		next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
7708a006adbSBjoern A. Zeeb 		/*
7718a006adbSBjoern A. Zeeb 		 * Transparently forwarded. Pretend to be the destination.
7728a006adbSBjoern A. Zeeb 		 * Already got one like this?
7738a006adbSBjoern A. Zeeb 		 */
7748a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo,
7758a006adbSBjoern A. Zeeb 		    &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport,
7768a006adbSBjoern A. Zeeb 		    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m);
7778a006adbSBjoern A. Zeeb 		if (!inp) {
7788a006adbSBjoern A. Zeeb 			/*
7798a006adbSBjoern A. Zeeb 			 * It's new.  Try to find the ambushing socket.
7808a006adbSBjoern A. Zeeb 			 * Because we've rewritten the destination address,
7818a006adbSBjoern A. Zeeb 			 * any hardware-generated hash is ignored.
7828a006adbSBjoern A. Zeeb 			 */
7838a006adbSBjoern A. Zeeb 			inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src,
7848a006adbSBjoern A. Zeeb 			    th->th_sport, &next_hop6->sin6_addr,
7858a006adbSBjoern A. Zeeb 			    next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) :
7868a006adbSBjoern A. Zeeb 			    th->th_dport, INPLOOKUP_WILDCARD |
7878a006adbSBjoern A. Zeeb 			    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
7888a006adbSBjoern A. Zeeb 		}
7898a006adbSBjoern A. Zeeb 		/* Remove the tag from the packet.  We don't need it anymore. */
7908a006adbSBjoern A. Zeeb 		m_tag_delete(m, fwd_tag);
7918a006adbSBjoern A. Zeeb 	} else
7928a006adbSBjoern A. Zeeb #endif /* IPFIREWALL_FORWARD */
7938a006adbSBjoern A. Zeeb 	if (isipv6) {
7948a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src,
7958a006adbSBjoern A. Zeeb 		    th->th_sport, &ip6->ip6_dst, th->th_dport,
7968a006adbSBjoern A. Zeeb 		    INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
7978a006adbSBjoern A. Zeeb 		    m->m_pkthdr.rcvif, m);
7988a006adbSBjoern A. Zeeb 	}
7998a006adbSBjoern A. Zeeb #endif /* INET6 */
8008a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET)
8018a006adbSBjoern A. Zeeb 	else
8028a006adbSBjoern A. Zeeb #endif
8038a006adbSBjoern A. Zeeb #ifdef INET
8048a006adbSBjoern A. Zeeb #ifdef IPFIREWALL_FORWARD
8058a006adbSBjoern A. Zeeb 	if (fwd_tag != NULL) {
8069b932e9eSAndre Oppermann 		struct sockaddr_in *next_hop;
8079b932e9eSAndre Oppermann 
8089b932e9eSAndre Oppermann 		next_hop = (struct sockaddr_in *)(fwd_tag+1);
809f9e354dfSJulian Elischer 		/*
8102b25acc1SLuigi Rizzo 		 * Transparently forwarded. Pretend to be the destination.
811f9e354dfSJulian Elischer 		 * already got one like this?
812f9e354dfSJulian Elischer 		 */
813d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport,
814fa046d87SRobert Watson 		    ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB,
815d3c1f003SRobert Watson 		    m->m_pkthdr.rcvif, m);
816fa046d87SRobert Watson 		if (!inp) {
817fa046d87SRobert Watson 			/*
818fa046d87SRobert Watson 			 * It's new.  Try to find the ambushing socket.
819d3c1f003SRobert Watson 			 * Because we've rewritten the destination address,
820d3c1f003SRobert Watson 			 * any hardware-generated hash is ignored.
821fa046d87SRobert Watson 			 */
822fa046d87SRobert Watson 			inp = in_pcblookup(&V_tcbinfo, ip->ip_src,
823fa046d87SRobert Watson 			    th->th_sport, next_hop->sin_addr,
824fa046d87SRobert Watson 			    next_hop->sin_port ? ntohs(next_hop->sin_port) :
825fa046d87SRobert Watson 			    th->th_dport, INPLOOKUP_WILDCARD |
826fa046d87SRobert Watson 			    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
827f9e354dfSJulian Elischer 		}
8289b932e9eSAndre Oppermann 		/* Remove the tag from the packet.  We don't need it anymore. */
8299b932e9eSAndre Oppermann 		m_tag_delete(m, fwd_tag);
830b10fbdeaSAndre Oppermann 	} else
8319b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */
832d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src,
833fa046d87SRobert Watson 		    th->th_sport, ip->ip_dst, th->th_dport,
834fa046d87SRobert Watson 		    INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
835d3c1f003SRobert Watson 		    m->m_pkthdr.rcvif, m);
8368a006adbSBjoern A. Zeeb #endif /* INET */
837f9e354dfSJulian Elischer 
838df8bae1dSRodney W. Grimes 	/*
839574b6964SAndre Oppermann 	 * If the INPCB does not exist then all data in the incoming
840574b6964SAndre Oppermann 	 * segment is discarded and an appropriate RST is sent back.
8418b07e49aSJulian Elischer 	 * XXX MRT Send RST using which routing table?
842df8bae1dSRodney W. Grimes 	 */
843816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
844574b6964SAndre Oppermann 		/*
845574b6964SAndre Oppermann 		 * Log communication attempts to ports that are not
846574b6964SAndre Oppermann 		 * in use.
847574b6964SAndre Oppermann 		 */
848574b6964SAndre Oppermann 		if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) ||
849574b6964SAndre Oppermann 		    tcp_log_in_vain == 2) {
850b7d747ecSAndre Oppermann 			if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6)))
851df541e5fSAndre Oppermann 				log(LOG_INFO, "%s; %s: Connection attempt "
852df541e5fSAndre Oppermann 				    "to closed port\n", s, __func__);
8532e4e1b4cSGeoff Rehmet 		}
854574b6964SAndre Oppermann 		/*
855574b6964SAndre Oppermann 		 * When blackholing do not respond with a RST but
856574b6964SAndre Oppermann 		 * completely ignore the segment and drop it.
857574b6964SAndre Oppermann 		 */
858603724d3SBjoern A. Zeeb 		if ((V_blackhole == 1 && (thflags & TH_SYN)) ||
859603724d3SBjoern A. Zeeb 		    V_blackhole == 2)
8601929eae1SAndre Oppermann 			goto dropunlock;
861574b6964SAndre Oppermann 
862a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
863a57815efSBosko Milekic 		goto dropwithreset;
864816a3d83SPoul-Henning Kamp 	}
865fa046d87SRobert Watson 	INP_WLOCK_ASSERT(inp);
86680cb9f21SKip Macy 	if (!(inp->inp_flags & INP_HW_FLOWID)
86780cb9f21SKip Macy 	    && (m->m_flags & M_FLOWID)
86880cb9f21SKip Macy 	    && ((inp->inp_socket == NULL)
86980cb9f21SKip Macy 		|| !(inp->inp_socket->so_options & SO_ACCEPTCONN))) {
87080cb9f21SKip Macy 		inp->inp_flags |= INP_HW_FLOWID;
87180cb9f21SKip Macy 		inp->inp_flags &= ~INP_SW_FLOWID;
87280cb9f21SKip Macy 		inp->inp_flowid = m->m_pkthdr.flowid;
87380cb9f21SKip Macy 	}
874a2589465SKen Smith #ifdef IPSEC
875a2589465SKen Smith #ifdef INET6
876a2589465SKen Smith 	if (isipv6 && ipsec6_in_reject(m, inp)) {
877603724d3SBjoern A. Zeeb 		V_ipsec6stat.in_polvio++;
878a2589465SKen Smith 		goto dropunlock;
879a2589465SKen Smith 	} else
880a2589465SKen Smith #endif /* INET6 */
881a2589465SKen Smith 	if (ipsec4_in_reject(m, inp) != 0) {
882603724d3SBjoern A. Zeeb 		V_ipsec4stat.in_polvio++;
883a2589465SKen Smith 		goto dropunlock;
884a2589465SKen Smith 	}
885a2589465SKen Smith #endif /* IPSEC */
886a2589465SKen Smith 
8878d573cc1SAndre Oppermann 	/*
8888d573cc1SAndre Oppermann 	 * Check the minimum TTL for socket.
8898d573cc1SAndre Oppermann 	 */
89034f83c52SGeorge V. Neville-Neil 	if (inp->inp_ip_minttl != 0) {
89134f83c52SGeorge V. Neville-Neil #ifdef INET6
89234f83c52SGeorge V. Neville-Neil 		if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim)
893302ce8d6SAndre Oppermann 			goto dropunlock;
89402707462SAndre Oppermann 		else
89534f83c52SGeorge V. Neville-Neil #endif
89634f83c52SGeorge V. Neville-Neil 		if (inp->inp_ip_minttl > ip->ip_ttl)
897302ce8d6SAndre Oppermann 			goto dropunlock;
89834f83c52SGeorge V. Neville-Neil 	}
899936cd18dSAndre Oppermann 
900340c35deSJonathan Lemon 	/*
901252ca428SRobert Watson 	 * A previous connection in TIMEWAIT state is supposed to catch stray
902252ca428SRobert Watson 	 * or duplicate segments arriving late.  If this segment was a
9030989f56cSRobert Watson 	 * legitimate new connection attempt, the old INPCB gets removed and
904252ca428SRobert Watson 	 * we can try again to find a listening socket.
905252ca428SRobert Watson 	 *
906fa046d87SRobert Watson 	 * At this point, due to earlier optimism, we may hold only an inpcb
907fa046d87SRobert Watson 	 * lock, and not the inpcbinfo write lock.  If so, we need to try to
908fa046d87SRobert Watson 	 * acquire it, or if that fails, acquire a reference on the inpcb,
909fa046d87SRobert Watson 	 * drop all locks, acquire a global write lock, and then re-acquire
910fa046d87SRobert Watson 	 * the inpcb lock.  We may at that point discover that another thread
911fa046d87SRobert Watson 	 * has tried to free the inpcb, in which case we need to loop back
912fa046d87SRobert Watson 	 * and try to find a new inpcb to deliver to.
913fa046d87SRobert Watson 	 *
914fa046d87SRobert Watson 	 * XXXRW: It may be time to rethink timewait locking.
915340c35deSJonathan Lemon 	 */
916883e9bc4SRobert Watson relocked:
917ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
918fa046d87SRobert Watson 		if (ti_locked == TI_UNLOCKED) {
919fa046d87SRobert Watson 			if (INP_INFO_TRY_WLOCK(&V_tcbinfo) == 0) {
920252ca428SRobert Watson 				in_pcbref(inp);
921252ca428SRobert Watson 				INP_WUNLOCK(inp);
922252ca428SRobert Watson 				INP_INFO_WLOCK(&V_tcbinfo);
923252ca428SRobert Watson 				ti_locked = TI_WLOCKED;
924252ca428SRobert Watson 				INP_WLOCK(inp);
925fa046d87SRobert Watson 				if (in_pcbrele_wlocked(inp)) {
926252ca428SRobert Watson 					inp = NULL;
927252ca428SRobert Watson 					goto findpcb;
928252ca428SRobert Watson 				}
929f681a5fdSRobert Watson 			} else
930252ca428SRobert Watson 				ti_locked = TI_WLOCKED;
931252ca428SRobert Watson 		}
932252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
933252ca428SRobert Watson 
934340c35deSJonathan Lemon 		if (thflags & TH_SYN)
935f72167f4SAndre Oppermann 			tcp_dooptions(&to, optp, optlen, TO_SYN);
9368d573cc1SAndre Oppermann 		/*
9378d573cc1SAndre Oppermann 		 * NB: tcp_twcheck unlocks the INP and frees the mbuf.
9388d573cc1SAndre Oppermann 		 */
9392104448fSAndre Oppermann 		if (tcp_twcheck(inp, &to, th, m, tlen))
940340c35deSJonathan Lemon 			goto findpcb;
941603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
942340c35deSJonathan Lemon 		return;
943340c35deSJonathan Lemon 	}
944794235b7SAndre Oppermann 	/*
945794235b7SAndre Oppermann 	 * The TCPCB may no longer exist if the connection is winding
946794235b7SAndre Oppermann 	 * down or it is in the CLOSED state.  Either way we drop the
947794235b7SAndre Oppermann 	 * segment and send an appropriate response.
948794235b7SAndre Oppermann 	 */
949df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
950a160e630SAndre Oppermann 	if (tp == NULL || tp->t_state == TCPS_CLOSED) {
951a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
952a57815efSBosko Milekic 		goto dropwithreset;
95309f81a46SBosko Milekic 	}
954df8bae1dSRodney W. Grimes 
95509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
95609fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
95709fe6320SNavdeep Parhar 		tcp_offload_input(tp, m);
95809fe6320SNavdeep Parhar 		m = NULL;	/* consumed by the TOE driver */
95909fe6320SNavdeep Parhar 		goto dropunlock;
96009fe6320SNavdeep Parhar 	}
96109fe6320SNavdeep Parhar #endif
96209fe6320SNavdeep Parhar 
963252ca428SRobert Watson 	/*
964252ca428SRobert Watson 	 * We've identified a valid inpcb, but it could be that we need an
965fa046d87SRobert Watson 	 * inpcbinfo write lock but don't hold it.  In this case, attempt to
966fa046d87SRobert Watson 	 * acquire using the same strategy as the TIMEWAIT case above.  If we
967fa046d87SRobert Watson 	 * relock, we have to jump back to 'relocked' as the connection might
968fa046d87SRobert Watson 	 * now be in TIMEWAIT.
969252ca428SRobert Watson 	 */
970fa046d87SRobert Watson #ifdef INVARIANTS
971fa046d87SRobert Watson 	if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0)
972fa046d87SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
973fa046d87SRobert Watson #endif
974fa046d87SRobert Watson 	if (tp->t_state != TCPS_ESTABLISHED) {
975fa046d87SRobert Watson 		if (ti_locked == TI_UNLOCKED) {
976fa046d87SRobert Watson 			if (INP_INFO_TRY_WLOCK(&V_tcbinfo) == 0) {
977252ca428SRobert Watson 				in_pcbref(inp);
978252ca428SRobert Watson 				INP_WUNLOCK(inp);
979252ca428SRobert Watson 				INP_INFO_WLOCK(&V_tcbinfo);
980252ca428SRobert Watson 				ti_locked = TI_WLOCKED;
981252ca428SRobert Watson 				INP_WLOCK(inp);
982fa046d87SRobert Watson 				if (in_pcbrele_wlocked(inp)) {
983252ca428SRobert Watson 					inp = NULL;
984252ca428SRobert Watson 					goto findpcb;
985252ca428SRobert Watson 				}
986883e9bc4SRobert Watson 				goto relocked;
987f681a5fdSRobert Watson 			} else
988252ca428SRobert Watson 				ti_locked = TI_WLOCKED;
989252ca428SRobert Watson 		}
990252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
991252ca428SRobert Watson 	}
992252ca428SRobert Watson 
993c488362eSRobert Watson #ifdef MAC
9948501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
99530d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, m))
996302ce8d6SAndre Oppermann 		goto dropunlock;
997c488362eSRobert Watson #endif
998a557af22SRobert Watson 	so = inp->inp_socket;
999302ce8d6SAndre Oppermann 	KASSERT(so != NULL, ("%s: so == NULL", __func__));
1000610ee2f9SDavid Greenman #ifdef TCPDEBUG
1001df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG) {
1002df8bae1dSRodney W. Grimes 		ostate = tp->t_state;
100310fe523eSMaxim Konovalov #ifdef INET6
10046f697424SBjoern A. Zeeb 		if (isipv6) {
1005540e8b7eSJeffrey Hsu 			bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6));
1006d30d90dcSMaxim Konovalov 		} else
10076f697424SBjoern A. Zeeb #endif
1008540e8b7eSJeffrey Hsu 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
1009fb59c426SYoshinobu Inoue 		tcp_savetcp = *th;
1010df8bae1dSRodney W. Grimes 	}
1011b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */
1012db33b3e6SAndre Oppermann 	/*
1013db33b3e6SAndre Oppermann 	 * When the socket is accepting connections (the INPCB is in LISTEN
1014db33b3e6SAndre Oppermann 	 * state) we look into the SYN cache if this is a new connection
1015fa046d87SRobert Watson 	 * attempt or the completion of a previous one.  Because listen
1016fa046d87SRobert Watson 	 * sockets are never in TCPS_ESTABLISHED, the V_tcbinfo lock will be
1017fa046d87SRobert Watson 	 * held in this case.
1018db33b3e6SAndre Oppermann 	 */
1019540e8b7eSJeffrey Hsu 	if (so->so_options & SO_ACCEPTCONN) {
1020540e8b7eSJeffrey Hsu 		struct in_conninfo inc;
1021540e8b7eSJeffrey Hsu 
10227824d002SAndre Oppermann 		KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but "
10237824d002SAndre Oppermann 		    "tp not listening", __func__));
1024fa046d87SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
10257824d002SAndre Oppermann 
10268bfb1918SAndre Oppermann 		bzero(&inc, sizeof(inc));
1027302ce8d6SAndre Oppermann #ifdef INET6
1028be2ac88cSJonathan Lemon 		if (isipv6) {
1029dcdb4371SBjoern A. Zeeb 			inc.inc_flags |= INC_ISIPV6;
1030be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
1031be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
1032302ce8d6SAndre Oppermann 		} else
1033302ce8d6SAndre Oppermann #endif
1034302ce8d6SAndre Oppermann 		{
1035be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
1036be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
1037be2ac88cSJonathan Lemon 		}
1038be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
1039be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
10407973fba3SJulian Elischer 		inc.inc_fibnum = so->so_fibnum;
1041be2ac88cSJonathan Lemon 
1042fb59c426SYoshinobu Inoue 		/*
10438d573cc1SAndre Oppermann 		 * Check for an existing connection attempt in syncache if
10448d573cc1SAndre Oppermann 		 * the flag is only ACK.  A successful lookup creates a new
10458d573cc1SAndre Oppermann 		 * socket appended to the listen queue in SYN_RECEIVED state.
1046fb59c426SYoshinobu Inoue 		 */
1047be2ac88cSJonathan Lemon 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
1048bf6d304aSAndre Oppermann 			/*
1049bf6d304aSAndre Oppermann 			 * Parse the TCP options here because
1050bf6d304aSAndre Oppermann 			 * syncookies need access to the reflected
1051bf6d304aSAndre Oppermann 			 * timestamp.
1052bf6d304aSAndre Oppermann 			 */
1053bf6d304aSAndre Oppermann 			tcp_dooptions(&to, optp, optlen, 0);
10549fa198beSAndre Oppermann 			/*
10559fa198beSAndre Oppermann 			 * NB: syncache_expand() doesn't unlock
10569fa198beSAndre Oppermann 			 * inp and tcpinfo locks.
10579fa198beSAndre Oppermann 			 */
1058bf6d304aSAndre Oppermann 			if (!syncache_expand(&inc, &to, th, &so, m)) {
10594195b4afSPaul Traina 				/*
1060e207f800SAndre Oppermann 				 * No syncache entry or ACK was not
1061be2ac88cSJonathan Lemon 				 * for our SYN/ACK.  Send a RST.
10628d573cc1SAndre Oppermann 				 * NB: syncache did its own logging
10638d573cc1SAndre Oppermann 				 * of the failure cause.
10644195b4afSPaul Traina 				 */
1065be2ac88cSJonathan Lemon 				rstreason = BANDLIM_RST_OPENPORT;
1066be2ac88cSJonathan Lemon 				goto dropwithreset;
1067be2ac88cSJonathan Lemon 			}
1068f76fcf6dSJeffrey Hsu 			if (so == NULL) {
1069be2ac88cSJonathan Lemon 				/*
1070e207f800SAndre Oppermann 				 * We completed the 3-way handshake
1071e207f800SAndre Oppermann 				 * but could not allocate a socket
1072e207f800SAndre Oppermann 				 * either due to memory shortage,
1073e207f800SAndre Oppermann 				 * listen queue length limits or
1074a160e630SAndre Oppermann 				 * global socket limits.  Send RST
1075a160e630SAndre Oppermann 				 * or wait and have the remote end
1076a160e630SAndre Oppermann 				 * retransmit the ACK for another
1077a160e630SAndre Oppermann 				 * try.
1078be2ac88cSJonathan Lemon 				 */
1079a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1080a160e630SAndre Oppermann 					log(LOG_DEBUG, "%s; %s: Listen socket: "
10818d573cc1SAndre Oppermann 					    "Socket allocation failed due to "
10828d573cc1SAndre Oppermann 					    "limits or memory shortage, %s\n",
1083ac957cd2SJulian Elischer 					    s, __func__,
1084ac957cd2SJulian Elischer 					    V_tcp_sc_rst_sock_fail ?
1085ac957cd2SJulian Elischer 					    "sending RST" : "try again");
1086603724d3SBjoern A. Zeeb 				if (V_tcp_sc_rst_sock_fail) {
1087e207f800SAndre Oppermann 					rstreason = BANDLIM_UNLIMITED;
1088e207f800SAndre Oppermann 					goto dropwithreset;
1089a160e630SAndre Oppermann 				} else
1090a160e630SAndre Oppermann 					goto dropunlock;
1091f76fcf6dSJeffrey Hsu 			}
1092be2ac88cSJonathan Lemon 			/*
1093be2ac88cSJonathan Lemon 			 * Socket is created in state SYN_RECEIVED.
10948d573cc1SAndre Oppermann 			 * Unlock the listen socket, lock the newly
10958d573cc1SAndre Oppermann 			 * created socket and update the tp variable.
1096be2ac88cSJonathan Lemon 			 */
10978501a69cSRobert Watson 			INP_WUNLOCK(inp);	/* listen socket */
1098be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
10998501a69cSRobert Watson 			INP_WLOCK(inp);		/* new connection */
1100be2ac88cSJonathan Lemon 			tp = intotcpcb(inp);
11018d573cc1SAndre Oppermann 			KASSERT(tp->t_state == TCPS_SYN_RECEIVED,
11028d573cc1SAndre Oppermann 			    ("%s: ", __func__));
11032903309aSAttilio Rao #ifdef TCP_SIGNATURE
11042903309aSAttilio Rao 			if (sig_checked == 0)  {
11052903309aSAttilio Rao 				tcp_dooptions(&to, optp, optlen,
11062903309aSAttilio Rao 				    (thflags & TH_SYN) ? TO_SYN : 0);
11072903309aSAttilio Rao 				if (!tcp_signature_verify_input(m, off0, tlen,
11082903309aSAttilio Rao 				    optlen, &to, th, tp->t_flags)) {
11092903309aSAttilio Rao 
11102903309aSAttilio Rao 					/*
11112903309aSAttilio Rao 					 * In SYN_SENT state if it receives an
11122903309aSAttilio Rao 					 * RST, it is allowed for further
11132903309aSAttilio Rao 					 * processing.
11142903309aSAttilio Rao 					 */
11152903309aSAttilio Rao 					if ((thflags & TH_RST) == 0 ||
11162903309aSAttilio Rao 					    (tp->t_state == TCPS_SYN_SENT) == 0)
11172903309aSAttilio Rao 						goto dropunlock;
11182903309aSAttilio Rao 				}
11192903309aSAttilio Rao 				sig_checked = 1;
11202903309aSAttilio Rao 			}
11212903309aSAttilio Rao #endif
11222903309aSAttilio Rao 
1123be2ac88cSJonathan Lemon 			/*
1124302ce8d6SAndre Oppermann 			 * Process the segment and the data it
1125302ce8d6SAndre Oppermann 			 * contains.  tcp_do_segment() consumes
1126302ce8d6SAndre Oppermann 			 * the mbuf chain and unlocks the inpcb.
1127302ce8d6SAndre Oppermann 			 */
1128f2512ba1SRui Paulo 			tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen,
1129252ca428SRobert Watson 			    iptos, ti_locked);
1130603724d3SBjoern A. Zeeb 			INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1131302ce8d6SAndre Oppermann 			return;
1132be2ac88cSJonathan Lemon 		}
1133a160e630SAndre Oppermann 		/*
11348d573cc1SAndre Oppermann 		 * Segment flag validation for new connection attempts:
11358d573cc1SAndre Oppermann 		 *
1136a160e630SAndre Oppermann 		 * Our (SYN|ACK) response was rejected.
1137a160e630SAndre Oppermann 		 * Check with syncache and remove entry to prevent
1138a160e630SAndre Oppermann 		 * retransmits.
113919bc77c5SAndre Oppermann 		 *
114019bc77c5SAndre Oppermann 		 * NB: syncache_chkrst does its own logging of failure
114119bc77c5SAndre Oppermann 		 * causes.
1142a160e630SAndre Oppermann 		 */
1143a160e630SAndre Oppermann 		if (thflags & TH_RST) {
114419bc77c5SAndre Oppermann 			syncache_chkrst(&inc, th);
1145a160e630SAndre Oppermann 			goto dropunlock;
1146a160e630SAndre Oppermann 		}
1147a160e630SAndre Oppermann 		/*
1148a160e630SAndre Oppermann 		 * We can't do anything without SYN.
1149a160e630SAndre Oppermann 		 */
1150a160e630SAndre Oppermann 		if ((thflags & TH_SYN) == 0) {
1151a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1152a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1153773673c1SAndre Oppermann 				    "SYN is missing, segment ignored\n",
11548d573cc1SAndre Oppermann 				    s, __func__);
115578b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1156a160e630SAndre Oppermann 			goto dropunlock;
1157a160e630SAndre Oppermann 		}
1158a160e630SAndre Oppermann 		/*
1159a160e630SAndre Oppermann 		 * (SYN|ACK) is bogus on a listen socket.
1160a160e630SAndre Oppermann 		 */
1161fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1162a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1163a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
11648d573cc1SAndre Oppermann 				    "SYN|ACK invalid, segment rejected\n",
11658d573cc1SAndre Oppermann 				    s, __func__);
1166a160e630SAndre Oppermann 			syncache_badack(&inc);	/* XXX: Not needed! */
116778b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1168a57815efSBosko Milekic 			rstreason = BANDLIM_RST_OPENPORT;
1169a57815efSBosko Milekic 			goto dropwithreset;
11704195b4afSPaul Traina 		}
1171a160e630SAndre Oppermann 		/*
1172a160e630SAndre Oppermann 		 * If the drop_synfin option is enabled, drop all
1173a160e630SAndre Oppermann 		 * segments with both the SYN and FIN bits set.
1174a160e630SAndre Oppermann 		 * This prevents e.g. nmap from identifying the
1175a160e630SAndre Oppermann 		 * TCP/IP stack.
1176a160e630SAndre Oppermann 		 * XXX: Poor reasoning.  nmap has other methods
1177a160e630SAndre Oppermann 		 * and is constantly refining its stack detection
1178a160e630SAndre Oppermann 		 * strategies.
11798d573cc1SAndre Oppermann 		 * XXX: This is a violation of the TCP specification
1180a160e630SAndre Oppermann 		 * and was used by RFC1644.
1181a160e630SAndre Oppermann 		 */
1182603724d3SBjoern A. Zeeb 		if ((thflags & TH_FIN) && V_drop_synfin) {
1183a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1184a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1185773673c1SAndre Oppermann 				    "SYN|FIN segment ignored (based on "
11868d573cc1SAndre Oppermann 				    "sysctl setting)\n", s, __func__);
118778b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1188302ce8d6SAndre Oppermann 			goto dropunlock;
1189ebb0cbeaSPaul Traina 		}
1190be2ac88cSJonathan Lemon 		/*
1191be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
1192a160e630SAndre Oppermann 		 *
1193a160e630SAndre Oppermann 		 * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored
1194a160e630SAndre Oppermann 		 * as they do not affect the state of the TCP FSM.
1195a160e630SAndre Oppermann 		 * The data pointed to by TH_URG and th_urp is ignored.
1196be2ac88cSJonathan Lemon 		 */
1197a160e630SAndre Oppermann 		KASSERT((thflags & (TH_RST|TH_ACK)) == 0,
1198a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_RST or TH_ACK set", __func__));
1199a160e630SAndre Oppermann 		KASSERT(thflags & (TH_SYN),
1200a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_SYN not set", __func__));
120133841545SHajimu UMEMOTO #ifdef INET6
120233841545SHajimu UMEMOTO 		/*
120333841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
120433841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
120533841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
120633841545SHajimu UMEMOTO 		 * getting established.
120733841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
120833841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
120933841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
121033841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
121133841545SHajimu UMEMOTO 		 * for the exchange.
121233841545SHajimu UMEMOTO 		 *
121333841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
121433841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
121533841545SHajimu UMEMOTO 		 * SYN in this case.
121633841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
121733841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
121833841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
121933841545SHajimu UMEMOTO 		 *    used"
122033841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
122133841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
122233841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
122333841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
122433841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
122533841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
122633841545SHajimu UMEMOTO 		 *
122733841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
122833841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
122933841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
123033841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
123133841545SHajimu UMEMOTO 		 */
1232603724d3SBjoern A. Zeeb 		if (isipv6 && !V_ip6_use_deprecated) {
123333841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
123433841545SHajimu UMEMOTO 
12358c0fec80SRobert Watson 			ia6 = ip6_getdstifaddr(m);
12368c0fec80SRobert Watson 			if (ia6 != NULL &&
123733841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
12388c0fec80SRobert Watson 				ifa_free(&ia6->ia_ifa);
1239a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1240a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12418d573cc1SAndre Oppermann 					"Connection attempt to deprecated "
12428d573cc1SAndre Oppermann 					"IPv6 address rejected\n",
1243a160e630SAndre Oppermann 					s, __func__);
124433841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
124533841545SHajimu UMEMOTO 				goto dropwithreset;
124633841545SHajimu UMEMOTO 			}
124777d396fdSMaksim Yevmenkin 			if (ia6)
12488c0fec80SRobert Watson 				ifa_free(&ia6->ia_ifa);
124933841545SHajimu UMEMOTO 		}
1250b287c6c7SBjoern A. Zeeb #endif /* INET6 */
125165f28919SJesper Skriver 		/*
1252db33b3e6SAndre Oppermann 		 * Basic sanity checks on incoming SYN requests:
12538d573cc1SAndre Oppermann 		 *   Don't respond if the destination is a link layer
1254db33b3e6SAndre Oppermann 		 *	broadcast according to RFC1122 4.2.3.10, p. 104.
12558d573cc1SAndre Oppermann 		 *   If it is from this socket it must be forged.
12568d573cc1SAndre Oppermann 		 *   Don't respond if the source or destination is a
12578d573cc1SAndre Oppermann 		 *	global or subnet broad- or multicast address.
125893ec91baSCrist J. Clark 		 *   Note that it is quite possible to receive unicast
125993ec91baSCrist J. Clark 		 *	link-layer packets with a broadcast IP address. Use
126093ec91baSCrist J. Clark 		 *	in_broadcast() to find them.
1261be2ac88cSJonathan Lemon 		 */
12628d573cc1SAndre Oppermann 		if (m->m_flags & (M_BCAST|M_MCAST)) {
12638d573cc1SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
12648d573cc1SAndre Oppermann 			    log(LOG_DEBUG, "%s; %s: Listen socket: "
12658d573cc1SAndre Oppermann 				"Connection attempt from broad- or multicast "
1266773673c1SAndre Oppermann 				"link layer address ignored\n", s, __func__);
1267302ce8d6SAndre Oppermann 			goto dropunlock;
12688d573cc1SAndre Oppermann 		}
1269db33b3e6SAndre Oppermann #ifdef INET6
1270b287c6c7SBjoern A. Zeeb 		if (isipv6) {
1271db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1272a160e630SAndre Oppermann 			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) {
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/from self "
1276773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1277302ce8d6SAndre Oppermann 				goto dropunlock;
1278a160e630SAndre Oppermann 			}
1279be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1280a160e630SAndre Oppermann 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
1281a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1282a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12838d573cc1SAndre Oppermann 					"Connection attempt from/to multicast "
1284773673c1SAndre Oppermann 					"address ignored\n", s, __func__);
1285302ce8d6SAndre Oppermann 				goto dropunlock;
1286a160e630SAndre Oppermann 			}
1287b287c6c7SBjoern A. Zeeb 		}
1288db33b3e6SAndre Oppermann #endif
1289b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
1290b287c6c7SBjoern A. Zeeb 		else
1291b287c6c7SBjoern A. Zeeb #endif
1292b287c6c7SBjoern A. Zeeb #ifdef INET
1293b287c6c7SBjoern A. Zeeb 		{
1294db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1295a160e630SAndre Oppermann 			    ip->ip_dst.s_addr == ip->ip_src.s_addr) {
1296a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1297a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12988d573cc1SAndre Oppermann 					"Connection attempt from/to self "
1299773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1300302ce8d6SAndre Oppermann 				goto dropunlock;
1301a160e630SAndre Oppermann 			}
1302be2ac88cSJonathan Lemon 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
1303be2ac88cSJonathan Lemon 			    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
13042ca2159fSCrist J. Clark 			    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
1305a160e630SAndre Oppermann 			    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1306a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1307a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13088d573cc1SAndre Oppermann 					"Connection attempt from/to broad- "
1309773673c1SAndre Oppermann 					"or multicast address ignored\n",
1310a160e630SAndre Oppermann 					s, __func__);
1311302ce8d6SAndre Oppermann 				goto dropunlock;
1312c068736aSJeffrey Hsu 			}
1313a160e630SAndre Oppermann 		}
1314b287c6c7SBjoern A. Zeeb #endif
1315be2ac88cSJonathan Lemon 		/*
1316db33b3e6SAndre Oppermann 		 * SYN appears to be valid.  Create compressed TCP state
1317db33b3e6SAndre Oppermann 		 * for syncache.
1318be2ac88cSJonathan Lemon 		 */
13193c653157SHartmut Brandt #ifdef TCPDEBUG
13203c653157SHartmut Brandt 		if (so->so_options & SO_DEBUG)
13213c653157SHartmut Brandt 			tcp_trace(TA_INPUT, ostate, tp,
13223c653157SHartmut Brandt 			    (void *)tcp_saveipgen, &tcp_savetcp, 0);
13233c653157SHartmut Brandt #endif
1324f72167f4SAndre Oppermann 		tcp_dooptions(&to, optp, optlen, TO_SYN);
132509fe6320SNavdeep Parhar 		syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL);
1326be2ac88cSJonathan Lemon 		/*
13274d6e7130SAndre Oppermann 		 * Entry added to syncache and mbuf consumed.
1328a160e630SAndre Oppermann 		 * Everything already unlocked by syncache_add().
1329be2ac88cSJonathan Lemon 		 */
1330603724d3SBjoern A. Zeeb 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1331be2ac88cSJonathan Lemon 		return;
1332f76fcf6dSJeffrey Hsu 	}
1333db33b3e6SAndre Oppermann 
13342903309aSAttilio Rao #ifdef TCP_SIGNATURE
13352903309aSAttilio Rao 	if (sig_checked == 0)  {
13362903309aSAttilio Rao 		tcp_dooptions(&to, optp, optlen,
13372903309aSAttilio Rao 		    (thflags & TH_SYN) ? TO_SYN : 0);
13382903309aSAttilio Rao 		if (!tcp_signature_verify_input(m, off0, tlen, optlen, &to,
13392903309aSAttilio Rao 		    th, tp->t_flags)) {
13402903309aSAttilio Rao 
13412903309aSAttilio Rao 			/*
13422903309aSAttilio Rao 			 * In SYN_SENT state if it receives an RST, it is
13432903309aSAttilio Rao 			 * allowed for further processing.
13442903309aSAttilio Rao 			 */
13452903309aSAttilio Rao 			if ((thflags & TH_RST) == 0 ||
13462903309aSAttilio Rao 			    (tp->t_state == TCPS_SYN_SENT) == 0)
13472903309aSAttilio Rao 				goto dropunlock;
13482903309aSAttilio Rao 		}
13492903309aSAttilio Rao 		sig_checked = 1;
13502903309aSAttilio Rao 	}
13512903309aSAttilio Rao #endif
13522903309aSAttilio Rao 
1353302ce8d6SAndre Oppermann 	/*
13548d573cc1SAndre Oppermann 	 * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later
13558d573cc1SAndre Oppermann 	 * state.  tcp_do_segment() always consumes the mbuf chain, unlocks
13568d573cc1SAndre Oppermann 	 * the inpcb, and unlocks pcbinfo.
1357302ce8d6SAndre Oppermann 	 */
1358252ca428SRobert Watson 	tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked);
1359603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1360302ce8d6SAndre Oppermann 	return;
1361be2ac88cSJonathan Lemon 
1362302ce8d6SAndre Oppermann dropwithreset:
1363fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED) {
1364dd8ac7f9SRobert Watson 		INP_INFO_WUNLOCK(&V_tcbinfo);
1365252ca428SRobert Watson 		ti_locked = TI_UNLOCKED;
1366fa046d87SRobert Watson 	}
1367fa046d87SRobert Watson #ifdef INVARIANTS
1368fa046d87SRobert Watson 	else {
1369fa046d87SRobert Watson 		KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropwithreset "
1370fa046d87SRobert Watson 		    "ti_locked: %d", __func__, ti_locked));
1371fa046d87SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1372fa046d87SRobert Watson 	}
1373fa046d87SRobert Watson #endif
1374014ea782SRobert Watson 
1375014ea782SRobert Watson 	if (inp != NULL) {
1376302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
1377014ea782SRobert Watson 		INP_WUNLOCK(inp);
1378dd8ac7f9SRobert Watson 	} else
1379014ea782SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
1380302ce8d6SAndre Oppermann 	m = NULL;	/* mbuf chain got consumed. */
1381014ea782SRobert Watson 	goto drop;
1382014ea782SRobert Watson 
1383302ce8d6SAndre Oppermann dropunlock:
1384fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED) {
1385252ca428SRobert Watson 		INP_INFO_WUNLOCK(&V_tcbinfo);
1386252ca428SRobert Watson 		ti_locked = TI_UNLOCKED;
1387fa046d87SRobert Watson 	}
1388fa046d87SRobert Watson #ifdef INVARIANTS
1389fa046d87SRobert Watson 	else {
1390fa046d87SRobert Watson 		KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropunlock "
1391fa046d87SRobert Watson 		    "ti_locked: %d", __func__, ti_locked));
1392fa046d87SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1393fa046d87SRobert Watson 	}
1394fa046d87SRobert Watson #endif
1395252ca428SRobert Watson 
13969fa198beSAndre Oppermann 	if (inp != NULL)
13978501a69cSRobert Watson 		INP_WUNLOCK(inp);
1398014ea782SRobert Watson 
1399302ce8d6SAndre Oppermann drop:
1400603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1401a160e630SAndre Oppermann 	if (s != NULL)
1402a160e630SAndre Oppermann 		free(s, M_TCPLOG);
1403302ce8d6SAndre Oppermann 	if (m != NULL)
1404302ce8d6SAndre Oppermann 		m_freem(m);
1405302ce8d6SAndre Oppermann }
1406302ce8d6SAndre Oppermann 
1407679d9708SAndre Oppermann static void
1408302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
1409252ca428SRobert Watson     struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos,
1410252ca428SRobert Watson     int ti_locked)
1411302ce8d6SAndre Oppermann {
1412302ce8d6SAndre Oppermann 	int thflags, acked, ourfinisacked, needoutput = 0;
1413302ce8d6SAndre Oppermann 	int rstreason, todrop, win;
1414302ce8d6SAndre Oppermann 	u_long tiwin;
1415302ce8d6SAndre Oppermann 	struct tcpopt to;
1416302ce8d6SAndre Oppermann 
141714739780SMaxim Konovalov #ifdef TCPDEBUG
141814739780SMaxim Konovalov 	/*
141914739780SMaxim Konovalov 	 * The size of tcp_saveipgen must be the size of the max ip header,
142014739780SMaxim Konovalov 	 * now IPv6.
142114739780SMaxim Konovalov 	 */
142214739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
142314739780SMaxim Konovalov 	struct tcphdr tcp_savetcp;
142414739780SMaxim Konovalov 	short ostate = 0;
142514739780SMaxim Konovalov #endif
1426302ce8d6SAndre Oppermann 	thflags = th->th_flags;
1427d64a46eaSLawrence Stewart 	tp->sackhint.last_sack_ack = 0;
1428302ce8d6SAndre Oppermann 
1429252ca428SRobert Watson 	/*
1430252ca428SRobert Watson 	 * If this is either a state-changing packet or current state isn't
1431252ca428SRobert Watson 	 * established, we require a write lock on tcbinfo.  Otherwise, we
14320989f56cSRobert Watson 	 * allow the tcbinfo to be in either alocked or unlocked, as the
14330989f56cSRobert Watson 	 * caller may have unnecessarily acquired a write lock due to a race.
1434252ca428SRobert Watson 	 */
1435252ca428SRobert Watson 	if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 ||
1436252ca428SRobert Watson 	    tp->t_state != TCPS_ESTABLISHED) {
1437252ca428SRobert Watson 		KASSERT(ti_locked == TI_WLOCKED, ("%s ti_locked %d for "
1438252ca428SRobert Watson 		    "SYN/FIN/RST/!EST", __func__, ti_locked));
1439603724d3SBjoern A. Zeeb 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1440252ca428SRobert Watson 	} else {
1441252ca428SRobert Watson #ifdef INVARIANTS
1442fa046d87SRobert Watson 		if (ti_locked == TI_WLOCKED)
1443252ca428SRobert Watson 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1444fa046d87SRobert Watson 		else {
1445fa046d87SRobert Watson 			KASSERT(ti_locked == TI_UNLOCKED, ("%s: EST "
1446fa046d87SRobert Watson 			    "ti_locked: %d", __func__, ti_locked));
1447fa046d87SRobert Watson 			INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1448fa046d87SRobert Watson 		}
1449252ca428SRobert Watson #endif
1450252ca428SRobert Watson 	}
14518501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1452679d9708SAndre Oppermann 	KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
1453679d9708SAndre Oppermann 	    __func__));
1454679d9708SAndre Oppermann 	KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
1455679d9708SAndre Oppermann 	    __func__));
1456df8bae1dSRodney W. Grimes 
1457df8bae1dSRodney W. Grimes 	/*
1458df8bae1dSRodney W. Grimes 	 * Segment received on connection.
1459df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
1460e8949f74SAndre Oppermann 	 * XXX: This should be done after segment
1461e8949f74SAndre Oppermann 	 * validation to ignore broken/spoofed segs.
1462df8bae1dSRodney W. Grimes 	 */
14639b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
14647ff19458SPaul Traina 	if (TCPS_HAVEESTABLISHED(tp->t_state))
14659077f387SGleb Smirnoff 		tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
1466df8bae1dSRodney W. Grimes 
1467df8bae1dSRodney W. Grimes 	/*
1468464fcfbcSAndre Oppermann 	 * Unscale the window into a 32-bit value.
1469e8949f74SAndre Oppermann 	 * For the SYN_SENT state the scale is zero.
1470464fcfbcSAndre Oppermann 	 */
1471464fcfbcSAndre Oppermann 	tiwin = th->th_win << tp->snd_scale;
1472464fcfbcSAndre Oppermann 
1473464fcfbcSAndre Oppermann 	/*
1474f2512ba1SRui Paulo 	 * TCP ECN processing.
1475f2512ba1SRui Paulo 	 */
1476f2512ba1SRui Paulo 	if (tp->t_flags & TF_ECN_PERMIT) {
14779c251892SRui Paulo 		if (thflags & TH_CWR)
14789c251892SRui Paulo 			tp->t_flags &= ~TF_ECN_SND_ECE;
1479f2512ba1SRui Paulo 		switch (iptos & IPTOS_ECN_MASK) {
1480f2512ba1SRui Paulo 		case IPTOS_ECN_CE:
1481f2512ba1SRui Paulo 			tp->t_flags |= TF_ECN_SND_ECE;
148278b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ce);
1483f2512ba1SRui Paulo 			break;
1484f2512ba1SRui Paulo 		case IPTOS_ECN_ECT0:
148578b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect0);
1486f2512ba1SRui Paulo 			break;
1487f2512ba1SRui Paulo 		case IPTOS_ECN_ECT1:
148878b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect1);
1489f2512ba1SRui Paulo 			break;
1490f2512ba1SRui Paulo 		}
1491dbc42409SLawrence Stewart 		/* Congestion experienced. */
1492dbc42409SLawrence Stewart 		if (thflags & TH_ECE) {
1493dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_ECN);
1494f2512ba1SRui Paulo 		}
1495f2512ba1SRui Paulo 	}
1496f2512ba1SRui Paulo 
1497f2512ba1SRui Paulo 	/*
1498f72167f4SAndre Oppermann 	 * Parse options on any incoming segment.
1499f72167f4SAndre Oppermann 	 */
1500302ce8d6SAndre Oppermann 	tcp_dooptions(&to, (u_char *)(th + 1),
1501302ce8d6SAndre Oppermann 	    (th->th_off << 2) - sizeof(struct tcphdr),
1502302ce8d6SAndre Oppermann 	    (thflags & TH_SYN) ? TO_SYN : 0);
1503f72167f4SAndre Oppermann 
1504f72167f4SAndre Oppermann 	/*
1505f72167f4SAndre Oppermann 	 * If echoed timestamp is later than the current time,
1506bf6d304aSAndre Oppermann 	 * fall back to non RFC1323 RTT calculation.  Normalize
1507bf6d304aSAndre Oppermann 	 * timestamp if syncookies were used when this connection
1508bf6d304aSAndre Oppermann 	 * was established.
1509f72167f4SAndre Oppermann 	 */
1510bf6d304aSAndre Oppermann 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
1511e16fa5caSJohn-Mark Gurney 		to.to_tsecr -= tp->ts_offset;
1512d8951c8aSBjoern A. Zeeb 		if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks()))
1513f72167f4SAndre Oppermann 			to.to_tsecr = 0;
1514bf6d304aSAndre Oppermann 	}
1515f72167f4SAndre Oppermann 
1516f72167f4SAndre Oppermann 	/*
151797d8d152SAndre Oppermann 	 * Process options only when we get SYN/ACK back. The SYN case
151897d8d152SAndre Oppermann 	 * for incoming connections is handled in tcp_syncache.
15195396d0f8SAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
15205396d0f8SAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.
152197d8d152SAndre Oppermann 	 * XXX this is traditional behavior, may need to be cleaned up.
1522df8bae1dSRodney W. Grimes 	 */
15230a389eabSSimon L. B. Nielsen 	if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
1524464fcfbcSAndre Oppermann 		if ((to.to_flags & TOF_SCALE) &&
1525464fcfbcSAndre Oppermann 		    (tp->t_flags & TF_REQ_SCALE)) {
1526be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
152702a1a643SAndre Oppermann 			tp->snd_scale = to.to_wscale;
1528be2ac88cSJonathan Lemon 		}
15295396d0f8SAndre Oppermann 		/*
15305396d0f8SAndre Oppermann 		 * Initial send window.  It will be updated with
15315396d0f8SAndre Oppermann 		 * the next incoming segment to the scaled value.
15325396d0f8SAndre Oppermann 		 */
15335396d0f8SAndre Oppermann 		tp->snd_wnd = th->th_win;
1534be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_TS) {
1535be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
1536be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
1537d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1538be2ac88cSJonathan Lemon 		}
1539be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_MSS)
1540be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
15413529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
15423529149eSAndre Oppermann 		    (to.to_flags & TOF_SACKPERM) == 0)
15433529149eSAndre Oppermann 			tp->t_flags &= ~TF_SACK_PERMIT;
15446d90faf3SPaul Saab 	}
15456d90faf3SPaul Saab 
1546df8bae1dSRodney W. Grimes 	/*
1547df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
1548df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
1549df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
1550df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
1551df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
1552df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
1553df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
1554df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
1555df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
1556df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
1557df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
1558df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
1559a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
1560c5ad39b9SAndre Oppermann 	 * Since we check for TCPS_ESTABLISHED first, it can only
1561a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
1562df8bae1dSRodney W. Grimes 	 */
1563df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
1564c5ad39b9SAndre Oppermann 	    th->th_seq == tp->rcv_nxt &&
1565fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1566c5ad39b9SAndre Oppermann 	    tp->snd_nxt == tp->snd_max &&
1567c5ad39b9SAndre Oppermann 	    tiwin && tiwin == tp->snd_wnd &&
1568a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
1569c5ad39b9SAndre Oppermann 	    LIST_EMPTY(&tp->t_segq) &&
1570be2ac88cSJonathan Lemon 	    ((to.to_flags & TOF_TS) == 0 ||
1571c5ad39b9SAndre Oppermann 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) {
1572df8bae1dSRodney W. Grimes 
1573df8bae1dSRodney W. Grimes 		/*
1574df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
1575df8bae1dSRodney W. Grimes 		 * record the timestamp.
1576a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
1577a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1578df8bae1dSRodney W. Grimes 		 */
1579be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
1580fb59c426SYoshinobu Inoue 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1581d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1582a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
1583df8bae1dSRodney W. Grimes 		}
1584df8bae1dSRodney W. Grimes 
1585fb59c426SYoshinobu Inoue 		if (tlen == 0) {
1586fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
1587fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
1588dbc42409SLawrence Stewart 			    !IN_RECOVERY(tp->t_flags) &&
1589fc30a251SAndre Oppermann 			    (to.to_flags & TOF_SACK) == 0 &&
1590dbc42409SLawrence Stewart 			    TAILQ_EMPTY(&tp->snd_holes)) {
1591df8bae1dSRodney W. Grimes 				/*
1592e8949f74SAndre Oppermann 				 * This is a pure ack for outstanding data.
1593df8bae1dSRodney W. Grimes 				 */
1594fa046d87SRobert Watson 				if (ti_locked == TI_WLOCKED)
1595252ca428SRobert Watson 					INP_INFO_WUNLOCK(&V_tcbinfo);
1596252ca428SRobert Watson 				ti_locked = TI_UNLOCKED;
1597252ca428SRobert Watson 
159878b50714SRobert Watson 				TCPSTAT_INC(tcps_predack);
1599252ca428SRobert Watson 
16009b8b58e0SJonathan Lemon 				/*
1601e8949f74SAndre Oppermann 				 * "bad retransmit" recovery.
16029b8b58e0SJonathan Lemon 				 */
16039b8b58e0SJonathan Lemon 				if (tp->t_rxtshift == 1 &&
1604672dc4aeSJohn Baldwin 				    tp->t_flags & TF_PREVVALID &&
16056dfb8b31SJohn Baldwin 				    (int)(ticks - tp->t_badrxtwin) < 0) {
1606dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_RTO_ERR);
16079b8b58e0SJonathan Lemon 				}
1608fa55172bSMatthew Dillon 
1609fa55172bSMatthew Dillon 				/*
1610fa55172bSMatthew Dillon 				 * Recalculate the transmit timer / rtt.
1611fa55172bSMatthew Dillon 				 *
1612fa55172bSMatthew Dillon 				 * Some boxes send broken timestamp replies
1613fa55172bSMatthew Dillon 				 * during the SYN+ACK phase, ignore
1614fa55172bSMatthew Dillon 				 * timestamps of 0 or we could calculate a
1615fa55172bSMatthew Dillon 				 * huge RTT and blow up the retransmit timer.
1616fa55172bSMatthew Dillon 				 */
1617fa55172bSMatthew Dillon 				if ((to.to_flags & TOF_TS) != 0 &&
1618fa55172bSMatthew Dillon 				    to.to_tsecr) {
1619d8951c8aSBjoern A. Zeeb 					u_int t;
1620d8951c8aSBjoern A. Zeeb 
1621d8951c8aSBjoern A. Zeeb 					t = tcp_ts_getticks() - to.to_tsecr;
1622d8951c8aSBjoern A. Zeeb 					if (!tp->t_rttlow || tp->t_rttlow > t)
1623d8951c8aSBjoern A. Zeeb 						tp->t_rttlow = t;
1624a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
1625d8951c8aSBjoern A. Zeeb 					    TCP_TS_TO_TICKS(t) + 1);
1626fa55172bSMatthew Dillon 				} else if (tp->t_rtttime &&
1627fa55172bSMatthew Dillon 				    SEQ_GT(th->th_ack, tp->t_rtseq)) {
1628eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1629eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - tp->t_rtttime)
1630eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - tp->t_rtttime;
1631c068736aSJeffrey Hsu 					tcp_xmit_timer(tp,
1632c068736aSJeffrey Hsu 							ticks - tp->t_rtttime);
1633fa55172bSMatthew Dillon 				}
1634dbc42409SLawrence Stewart 				acked = BYTES_THIS_ACK(tp, th);
163539bc9de5SLawrence Stewart 
163639bc9de5SLawrence Stewart 				/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
163739bc9de5SLawrence Stewart 				hhook_run_tcp_est_in(tp, th, &to);
163839bc9de5SLawrence Stewart 
163978b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvackpack);
164078b50714SRobert Watson 				TCPSTAT_ADD(tcps_rcvackbyte, acked);
1641df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
16429d11646dSJeffrey Hsu 				if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
16439d11646dSJeffrey Hsu 				    SEQ_LEQ(th->th_ack, tp->snd_recover))
16449d11646dSJeffrey Hsu 					tp->snd_recover = th->th_ack - 1;
1645dbc42409SLawrence Stewart 
1646dbc42409SLawrence Stewart 				/*
1647dbc42409SLawrence Stewart 				 * Let the congestion control algorithm update
1648dbc42409SLawrence Stewart 				 * congestion control related information. This
1649dbc42409SLawrence Stewart 				 * typically means increasing the congestion
1650dbc42409SLawrence Stewart 				 * window.
1651dbc42409SLawrence Stewart 				 */
1652dbc42409SLawrence Stewart 				cc_ack_received(tp, th, CC_ACK);
1653dbc42409SLawrence Stewart 
16549d11646dSJeffrey Hsu 				tp->snd_una = th->th_ack;
16551645d090SJeff Roberson 				/*
1656e8949f74SAndre Oppermann 				 * Pull snd_wl2 up to prevent seq wrap relative
16571645d090SJeff Roberson 				 * to th_ack.
16581645d090SJeff Roberson 				 */
1659cb942153SJeffrey Hsu 				tp->snd_wl2 = th->th_ack;
1660b5addd85SJeffrey Hsu 				tp->t_dupacks = 0;
1661df8bae1dSRodney W. Grimes 				m_freem(m);
1662e8949f74SAndre Oppermann 				ND6_HINT(tp); /* Some progress has been made. */
1663df8bae1dSRodney W. Grimes 
1664df8bae1dSRodney W. Grimes 				/*
1665df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
1666df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
1667df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
1668df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
1669df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
1670df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
1671df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
1672e8949f74SAndre Oppermann 				 */
16733c653157SHartmut Brandt #ifdef TCPDEBUG
16743c653157SHartmut Brandt 				if (so->so_options & SO_DEBUG)
16753c653157SHartmut Brandt 					tcp_trace(TA_INPUT, ostate, tp,
16763c653157SHartmut Brandt 					    (void *)tcp_saveipgen,
16773c653157SHartmut Brandt 					    &tcp_savetcp, 0);
16783c653157SHartmut Brandt #endif
1679df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
1680b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
1681b8152ba7SAndre Oppermann 				else if (!tcp_timer_active(tp, TT_PERSIST))
1682b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT,
1683b8152ba7SAndre Oppermann 						      tp->t_rxtcur);
1684df8bae1dSRodney W. Grimes 				sowwakeup(so);
1685df8bae1dSRodney W. Grimes 				if (so->so_snd.sb_cc)
1686df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1687a14c749fSJonathan Lemon 				goto check_delack;
1688df8bae1dSRodney W. Grimes 			}
1689fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1690fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
16916741ecf5SAndre Oppermann 			int newsize = 0;	/* automatic sockbuf scaling */
16926741ecf5SAndre Oppermann 
1693df8bae1dSRodney W. Grimes 			/*
1694252ca428SRobert Watson 			 * This is a pure, in-sequence data packet with
1695252ca428SRobert Watson 			 * nothing on the reassembly queue and we have enough
1696252ca428SRobert Watson 			 * buffer space to take it.
1697df8bae1dSRodney W. Grimes 			 */
1698fa046d87SRobert Watson 			if (ti_locked == TI_WLOCKED)
1699252ca428SRobert Watson 				INP_INFO_WUNLOCK(&V_tcbinfo);
1700252ca428SRobert Watson 			ti_locked = TI_UNLOCKED;
1701252ca428SRobert Watson 
17026d90faf3SPaul Saab 			/* Clean receiver SACK report if present */
17033529149eSAndre Oppermann 			if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks)
17046d90faf3SPaul Saab 				tcp_clean_sackreport(tp);
170578b50714SRobert Watson 			TCPSTAT_INC(tcps_preddat);
1706fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
17071645d090SJeff Roberson 			/*
17081645d090SJeff Roberson 			 * Pull snd_wl1 up to prevent seq wrap relative to
17091645d090SJeff Roberson 			 * th_seq.
17101645d090SJeff Roberson 			 */
17111645d090SJeff Roberson 			tp->snd_wl1 = th->th_seq;
17121645d090SJeff Roberson 			/*
17131645d090SJeff Roberson 			 * Pull rcv_up up to prevent seq wrap relative to
17141645d090SJeff Roberson 			 * rcv_nxt.
17151645d090SJeff Roberson 			 */
17161645d090SJeff Roberson 			tp->rcv_up = tp->rcv_nxt;
171778b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpack);
171878b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
1719e8949f74SAndre Oppermann 			ND6_HINT(tp);	/* Some progress has been made */
17203c653157SHartmut Brandt #ifdef TCPDEBUG
17213c653157SHartmut Brandt 			if (so->so_options & SO_DEBUG)
17223c653157SHartmut Brandt 				tcp_trace(TA_INPUT, ostate, tp,
17233c653157SHartmut Brandt 				    (void *)tcp_saveipgen, &tcp_savetcp, 0);
17243c653157SHartmut Brandt #endif
17256741ecf5SAndre Oppermann 		/*
17266741ecf5SAndre Oppermann 		 * Automatic sizing of receive socket buffer.  Often the send
17276741ecf5SAndre Oppermann 		 * buffer size is not optimally adjusted to the actual network
17286741ecf5SAndre Oppermann 		 * conditions at hand (delay bandwidth product).  Setting the
17296741ecf5SAndre Oppermann 		 * buffer size too small limits throughput on links with high
17306741ecf5SAndre Oppermann 		 * bandwidth and high delay (eg. trans-continental/oceanic links).
17316741ecf5SAndre Oppermann 		 *
17326741ecf5SAndre Oppermann 		 * On the receive side the socket buffer memory is only rarely
17336741ecf5SAndre Oppermann 		 * used to any significant extent.  This allows us to be much
17346741ecf5SAndre Oppermann 		 * more aggressive in scaling the receive socket buffer.  For
17356741ecf5SAndre Oppermann 		 * the case that the buffer space is actually used to a large
17366741ecf5SAndre Oppermann 		 * extent and we run out of kernel memory we can simply drop
17376741ecf5SAndre Oppermann 		 * the new segments; TCP on the sender will just retransmit it
17386741ecf5SAndre Oppermann 		 * later.  Setting the buffer size too big may only consume too
17396741ecf5SAndre Oppermann 		 * much kernel memory if the application doesn't read() from
17406741ecf5SAndre Oppermann 		 * the socket or packet loss or reordering makes use of the
17416741ecf5SAndre Oppermann 		 * reassembly queue.
17426741ecf5SAndre Oppermann 		 *
17436741ecf5SAndre Oppermann 		 * The criteria to step up the receive buffer one notch are:
17446741ecf5SAndre Oppermann 		 *  1. the number of bytes received during the time it takes
17456741ecf5SAndre Oppermann 		 *     one timestamp to be reflected back to us (the RTT);
17466741ecf5SAndre Oppermann 		 *  2. received bytes per RTT is within seven eighth of the
17476741ecf5SAndre Oppermann 		 *     current socket buffer size;
17486741ecf5SAndre Oppermann 		 *  3. receive buffer size has not hit maximal automatic size;
17496741ecf5SAndre Oppermann 		 *
17506741ecf5SAndre Oppermann 		 * This algorithm does one step per RTT at most and only if
17516741ecf5SAndre Oppermann 		 * we receive a bulk stream w/o packet losses or reorderings.
17526741ecf5SAndre Oppermann 		 * Shrinking the buffer during idle times is not necessary as
17536741ecf5SAndre Oppermann 		 * it doesn't consume any memory when idle.
17546741ecf5SAndre Oppermann 		 *
17556741ecf5SAndre Oppermann 		 * TODO: Only step up if the application is actually serving
17566741ecf5SAndre Oppermann 		 * the buffer to better manage the socket buffer resources.
1757df8bae1dSRodney W. Grimes 		 */
1758603724d3SBjoern A. Zeeb 			if (V_tcp_do_autorcvbuf &&
17596741ecf5SAndre Oppermann 			    to.to_tsecr &&
17606741ecf5SAndre Oppermann 			    (so->so_rcv.sb_flags & SB_AUTOSIZE)) {
17618502ec25SAndre Oppermann 				if (TSTMP_GT(to.to_tsecr, tp->rfbuf_ts) &&
17626741ecf5SAndre Oppermann 				    to.to_tsecr - tp->rfbuf_ts < hz) {
17636741ecf5SAndre Oppermann 					if (tp->rfbuf_cnt >
17646741ecf5SAndre Oppermann 					    (so->so_rcv.sb_hiwat / 8 * 7) &&
17656741ecf5SAndre Oppermann 					    so->so_rcv.sb_hiwat <
1766603724d3SBjoern A. Zeeb 					    V_tcp_autorcvbuf_max) {
17676741ecf5SAndre Oppermann 						newsize =
17686741ecf5SAndre Oppermann 						    min(so->so_rcv.sb_hiwat +
1769603724d3SBjoern A. Zeeb 						    V_tcp_autorcvbuf_inc,
1770603724d3SBjoern A. Zeeb 						    V_tcp_autorcvbuf_max);
17716741ecf5SAndre Oppermann 					}
17726741ecf5SAndre Oppermann 					/* Start over with next RTT. */
17736741ecf5SAndre Oppermann 					tp->rfbuf_ts = 0;
17746741ecf5SAndre Oppermann 					tp->rfbuf_cnt = 0;
17756741ecf5SAndre Oppermann 				} else
17766741ecf5SAndre Oppermann 					tp->rfbuf_cnt += tlen;	/* add up */
17776741ecf5SAndre Oppermann 			}
17786741ecf5SAndre Oppermann 
17796741ecf5SAndre Oppermann 			/* Add data to socket buffer. */
17801e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
1781c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1782c1c36a2cSMike Silbersack 				m_freem(m);
1783c1c36a2cSMike Silbersack 			} else {
17846741ecf5SAndre Oppermann 				/*
17856741ecf5SAndre Oppermann 				 * Set new socket buffer size.
17866741ecf5SAndre Oppermann 				 * Give up when limit is reached.
17876741ecf5SAndre Oppermann 				 */
17886741ecf5SAndre Oppermann 				if (newsize)
17896741ecf5SAndre Oppermann 					if (!sbreserve_locked(&so->so_rcv,
17906c8286e4SRobert Watson 					    newsize, so, NULL))
17916741ecf5SAndre Oppermann 						so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1792fb59c426SYoshinobu Inoue 				m_adj(m, drop_hdrlen);	/* delayed header drop */
17931e4d7da7SRobert Watson 				sbappendstream_locked(&so->so_rcv, m);
1794c1c36a2cSMike Silbersack 			}
1795e8949f74SAndre Oppermann 			/* NB: sorwakeup_locked() does an implicit unlock. */
17961e4d7da7SRobert Watson 			sorwakeup_locked(so);
1797d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp)) {
17983bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
1799f498eeeeSDavid Greenman 			} else {
1800e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
1801e612a582SDavid Greenman 				tcp_output(tp);
1802e612a582SDavid Greenman 			}
1803a14c749fSJonathan Lemon 			goto check_delack;
1804df8bae1dSRodney W. Grimes 		}
1805df8bae1dSRodney W. Grimes 	}
1806df8bae1dSRodney W. Grimes 
1807df8bae1dSRodney W. Grimes 	/*
1808df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1809df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1810df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1811df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1812df8bae1dSRodney W. Grimes 	 */
1813df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1814df8bae1dSRodney W. Grimes 	if (win < 0)
1815df8bae1dSRodney W. Grimes 		win = 0;
181666e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1817df8bae1dSRodney W. Grimes 
18186741ecf5SAndre Oppermann 	/* Reset receive buffer auto scaling when not in bulk receive mode. */
18196741ecf5SAndre Oppermann 	tp->rfbuf_ts = 0;
18206741ecf5SAndre Oppermann 	tp->rfbuf_cnt = 0;
18216741ecf5SAndre Oppermann 
1822df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1823df8bae1dSRodney W. Grimes 
1824df8bae1dSRodney W. Grimes 	/*
1825764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1826764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1827764d8cefSBill Fenner 	 */
1828764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1829fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1830fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1831a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1832a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1833a57815efSBosko Milekic 				goto dropwithreset;
1834a57815efSBosko Milekic 		}
1835764d8cefSBill Fenner 		break;
1836764d8cefSBill Fenner 
1837764d8cefSBill Fenner 	/*
1838df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
1839df8bae1dSRodney W. Grimes 	 *	if seg contains an ACK, but not for our SYN, drop the input.
1840df8bae1dSRodney W. Grimes 	 *	if seg contains a RST, then drop the connection.
1841df8bae1dSRodney W. Grimes 	 *	if seg does not contain SYN, then drop it.
1842df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
1843df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
1844df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
1845f2512ba1SRui Paulo 	 *	if seg contains an ECE and ECN support is enabled, the stream
1846f2512ba1SRui Paulo 	 *	    is ECN capable.
1847df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1848df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
1849df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
1850df8bae1dSRodney W. Grimes 	 */
1851df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
1852fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1853fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
1854fb59c426SYoshinobu Inoue 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1855a57815efSBosko Milekic 			rstreason = BANDLIM_UNLIMITED;
1856a0292f23SGarrett Wollman 			goto dropwithreset;
1857a0292f23SGarrett Wollman 		}
18580ca3f933SAndre Oppermann 		if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST))
1859df8bae1dSRodney W. Grimes 			tp = tcp_drop(tp, ECONNREFUSED);
18600ca3f933SAndre Oppermann 		if (thflags & TH_RST)
1861df8bae1dSRodney W. Grimes 			goto drop;
18620ca3f933SAndre Oppermann 		if (!(thflags & TH_SYN))
1863df8bae1dSRodney W. Grimes 			goto drop;
1864464fcfbcSAndre Oppermann 
1865fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
1866df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
1867fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
186878b50714SRobert Watson 			TCPSTAT_INC(tcps_connects);
1869845799c1SAndras Olah 			soisconnected(so);
1870c488362eSRobert Watson #ifdef MAC
187130d239bcSRobert Watson 			mac_socketpeer_set_from_mbuf(m, so);
1872c488362eSRobert Watson #endif
1873845799c1SAndras Olah 			/* Do window scaling on this connection? */
1874845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1875845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1876845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
1877845799c1SAndras Olah 			}
1878766282cbSJohn Baldwin 			tp->rcv_adv += imin(tp->rcv_wnd,
1879766282cbSJohn Baldwin 			    TCP_MAXWIN << tp->rcv_scale);
1880a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
1881a0292f23SGarrett Wollman 			/*
1882a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
1883a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
1884a0292f23SGarrett Wollman 			 */
1885d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp) && tlen != 0)
1886b8152ba7SAndre Oppermann 				tcp_timer_activate(tp, TT_DELACK,
1887b8152ba7SAndre Oppermann 				    tcp_delacktime);
1888a0292f23SGarrett Wollman 			else
1889a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
1890f2512ba1SRui Paulo 
1891603724d3SBjoern A. Zeeb 			if ((thflags & TH_ECE) && V_tcp_do_ecn) {
1892f2512ba1SRui Paulo 				tp->t_flags |= TF_ECN_PERMIT;
189378b50714SRobert Watson 				TCPSTAT_INC(tcps_ecn_shs);
1894f2512ba1SRui Paulo 			}
1895f2512ba1SRui Paulo 
1896a0292f23SGarrett Wollman 			/*
1897a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
1898a0292f23SGarrett Wollman 			 * Transitions:
1899a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
1900a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
1901a0292f23SGarrett Wollman 			 */
19029b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
1903a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
1904a0292f23SGarrett Wollman 				tp->t_state = TCPS_FIN_WAIT_1;
1905a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
1906fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
19077ff19458SPaul Traina 			} else {
1908a0292f23SGarrett Wollman 				tp->t_state = TCPS_ESTABLISHED;
1909dbc42409SLawrence Stewart 				cc_conn_init(tp);
19109077f387SGleb Smirnoff 				tcp_timer_activate(tp, TT_KEEP,
19119077f387SGleb Smirnoff 				    TP_KEEPIDLE(tp));
19127ff19458SPaul Traina 			}
1913a0292f23SGarrett Wollman 		} else {
1914a0292f23SGarrett Wollman 			/*
1915c068736aSJeffrey Hsu 			 * Received initial SYN in SYN-SENT[*] state =>
1916c068736aSJeffrey Hsu 			 * simultaneous open.  If segment contains CC option
1917c068736aSJeffrey Hsu 			 * and there is a cached CC, apply TAO test.
1918c068736aSJeffrey Hsu 			 * If it succeeds, connection is * half-synchronized.
1919c068736aSJeffrey Hsu 			 * Otherwise, do 3-way handshake:
1920a0292f23SGarrett Wollman 			 *        SYN-SENT -> SYN-RECEIVED
1921a0292f23SGarrett Wollman 			 *        SYN-SENT* -> SYN-RECEIVED*
1922a0292f23SGarrett Wollman 			 * If there was no CC option, clear cached CC value.
1923a0292f23SGarrett Wollman 			 */
19244b8e98d6SQing Li 			tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
1925b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
1926df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_SYN_RECEIVED;
1927a0292f23SGarrett Wollman 		}
1928df8bae1dSRodney W. Grimes 
1929252ca428SRobert Watson 		KASSERT(ti_locked == TI_WLOCKED, ("%s: trimthenstep6: "
1930252ca428SRobert Watson 		    "ti_locked %d", __func__, ti_locked));
1931252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
19328501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
19337cfc6904SRobert Watson 
1934df8bae1dSRodney W. Grimes 		/*
1935fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
1936df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
1937df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
1938df8bae1dSRodney W. Grimes 		 */
1939fb59c426SYoshinobu Inoue 		th->th_seq++;
1940fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
1941fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
1942df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
1943fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
1944fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
194578b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpackafterwin);
194678b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
1947df8bae1dSRodney W. Grimes 		}
1948fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
1949fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
1950a0292f23SGarrett Wollman 		/*
1951a0292f23SGarrett Wollman 		 * Client side of transaction: already sent SYN and data.
1952a0292f23SGarrett Wollman 		 * If the remote host used T/TCP to validate the SYN,
1953a0292f23SGarrett Wollman 		 * our data will be ACK'd; if so, enter normal data segment
1954a0292f23SGarrett Wollman 		 * processing in the middle of step 5, ack processing.
1955a0292f23SGarrett Wollman 		 * Otherwise, goto step 6.
1956a0292f23SGarrett Wollman 		 */
1957fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK)
1958a0292f23SGarrett Wollman 			goto process_ACK;
1959c068736aSJeffrey Hsu 
1960df8bae1dSRodney W. Grimes 		goto step6;
1961c068736aSJeffrey Hsu 
1962a0292f23SGarrett Wollman 	/*
1963a0292f23SGarrett Wollman 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
1964c94c54e4SAndre Oppermann 	 *      do normal processing.
1965a0292f23SGarrett Wollman 	 *
1966c94c54e4SAndre Oppermann 	 * NB: Leftover from RFC1644 T/TCP.  Cases to be reused later.
1967a0292f23SGarrett Wollman 	 */
1968a0292f23SGarrett Wollman 	case TCPS_LAST_ACK:
1969a0292f23SGarrett Wollman 	case TCPS_CLOSING:
1970a0292f23SGarrett Wollman 		break;  /* continue normal processing */
1971df8bae1dSRodney W. Grimes 	}
1972df8bae1dSRodney W. Grimes 
1973df8bae1dSRodney W. Grimes 	/*
1974df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
197580ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
197680ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
197780ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
197880ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
197980ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
198080ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
198180ab7c0eSGarrett Wollman 	 * killing a connection).
198280ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
1983a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
1984df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
1985df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
1986df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
1987df8bae1dSRodney W. Grimes 	 *
198880ab7c0eSGarrett Wollman 	 *
198980ab7c0eSGarrett Wollman 	 * If the RST bit is set, check the sequence number to see
199080ab7c0eSGarrett Wollman 	 * if this is a valid reset segment.
199180ab7c0eSGarrett Wollman 	 * RFC 793 page 37:
199280ab7c0eSGarrett Wollman 	 *   In all states except SYN-SENT, all reset (RST) segments
199380ab7c0eSGarrett Wollman 	 *   are validated by checking their SEQ-fields.  A reset is
199480ab7c0eSGarrett Wollman 	 *   valid if its sequence number is in the window.
199580ab7c0eSGarrett Wollman 	 * Note: this does not take into account delayed ACKs, so
199680ab7c0eSGarrett Wollman 	 *   we should test against last_ack_sent instead of rcv_nxt.
19971a244a61SJonathan Lemon 	 *   The sequence number in the reset segment is normally an
19981a244a61SJonathan Lemon 	 *   echo of our outgoing acknowlegement numbers, but some hosts
19991a244a61SJonathan Lemon 	 *   send a reset with the sequence number at the rightmost edge
20001a244a61SJonathan Lemon 	 *   of our receive window, and we have to handle this case.
200180dd2a81SMike Silbersack 	 * Note 2: Paul Watson's paper "Slipping in the Window" has shown
200280dd2a81SMike Silbersack 	 *   that brute force RST attacks are possible.  To combat this,
200380dd2a81SMike Silbersack 	 *   we use a much stricter check while in the ESTABLISHED state,
200480dd2a81SMike Silbersack 	 *   only accepting RSTs where the sequence number is equal to
200580dd2a81SMike Silbersack 	 *   last_ack_sent.  In all other states (the states in which a
200680dd2a81SMike Silbersack 	 *   RST is more likely), the more permissive check is used.
20078e5c87f4SBjoern A. Zeeb 	 * If we have multiple segments in flight, the initial reset
200880ab7c0eSGarrett Wollman 	 * segment sequence numbers will be to the left of last_ack_sent,
200980ab7c0eSGarrett Wollman 	 * but they will eventually catch up.
201080ab7c0eSGarrett Wollman 	 * In any case, it never made sense to trim reset segments to
201180ab7c0eSGarrett Wollman 	 * fit the receive window since RFC 1122 says:
201280ab7c0eSGarrett Wollman 	 *   4.2.2.12  RST Segment: RFC-793 Section 3.4
201380ab7c0eSGarrett Wollman 	 *
201480ab7c0eSGarrett Wollman 	 *    A TCP SHOULD allow a received RST segment to include data.
201580ab7c0eSGarrett Wollman 	 *
201680ab7c0eSGarrett Wollman 	 *    DISCUSSION
201780ab7c0eSGarrett Wollman 	 *         It has been suggested that a RST segment could contain
201880ab7c0eSGarrett Wollman 	 *         ASCII text that encoded and explained the cause of the
201980ab7c0eSGarrett Wollman 	 *         RST.  No standard has yet been established for such
202080ab7c0eSGarrett Wollman 	 *         data.
202180ab7c0eSGarrett Wollman 	 *
202280ab7c0eSGarrett Wollman 	 * If the reset segment passes the sequence number test examine
202380ab7c0eSGarrett Wollman 	 * the state:
202480ab7c0eSGarrett Wollman 	 *    SYN_RECEIVED STATE:
202580ab7c0eSGarrett Wollman 	 *	If passive open, return to LISTEN state.
202680ab7c0eSGarrett Wollman 	 *	If active open, inform user that connection was refused.
2027745bab7fSDima Dorfman 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
202880ab7c0eSGarrett Wollman 	 *	Inform user that connection was reset, and close tcb.
2029e9bd3a37SJonathan M. Bresler 	 *    CLOSING, LAST_ACK STATES:
203080ab7c0eSGarrett Wollman 	 *	Close the tcb.
2031e9bd3a37SJonathan M. Bresler 	 *    TIME_WAIT STATE:
203280ab7c0eSGarrett Wollman 	 *	Drop the segment - see Stevens, vol. 2, p. 964 and
203380ab7c0eSGarrett Wollman 	 *      RFC 1337.
203480ab7c0eSGarrett Wollman 	 */
2035fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
203695ad8418SQing Li 		if (SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) &&
203795ad8418SQing Li 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
203880ab7c0eSGarrett Wollman 			switch (tp->t_state) {
203980ab7c0eSGarrett Wollman 
204080ab7c0eSGarrett Wollman 			case TCPS_SYN_RECEIVED:
204180ab7c0eSGarrett Wollman 				so->so_error = ECONNREFUSED;
204280ab7c0eSGarrett Wollman 				goto close;
204380ab7c0eSGarrett Wollman 
204480ab7c0eSGarrett Wollman 			case TCPS_ESTABLISHED:
2045603724d3SBjoern A. Zeeb 				if (V_tcp_insecure_rst == 0 &&
204695ad8418SQing Li 				    !(SEQ_GEQ(th->th_seq, tp->rcv_nxt - 1) &&
204795ad8418SQing Li 				    SEQ_LEQ(th->th_seq, tp->rcv_nxt + 1)) &&
204895ad8418SQing Li 				    !(SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) &&
204995ad8418SQing Li 				    SEQ_LEQ(th->th_seq, tp->last_ack_sent + 1))) {
205078b50714SRobert Watson 					TCPSTAT_INC(tcps_badrst);
205180dd2a81SMike Silbersack 					goto drop;
205280dd2a81SMike Silbersack 				}
2053564aab1fSAndre Oppermann 				/* FALLTHROUGH */
205480ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_1:
205580ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_2:
205680ab7c0eSGarrett Wollman 			case TCPS_CLOSE_WAIT:
205780ab7c0eSGarrett Wollman 				so->so_error = ECONNRESET;
205880ab7c0eSGarrett Wollman 			close:
2059252ca428SRobert Watson 				KASSERT(ti_locked == TI_WLOCKED,
2060252ca428SRobert Watson 				    ("tcp_do_segment: TH_RST 1 ti_locked %d",
2061252ca428SRobert Watson 				    ti_locked));
2062252ca428SRobert Watson 				INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2063252ca428SRobert Watson 
206480ab7c0eSGarrett Wollman 				tp->t_state = TCPS_CLOSED;
206578b50714SRobert Watson 				TCPSTAT_INC(tcps_drops);
206680ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
206780ab7c0eSGarrett Wollman 				break;
206880ab7c0eSGarrett Wollman 
206980ab7c0eSGarrett Wollman 			case TCPS_CLOSING:
207080ab7c0eSGarrett Wollman 			case TCPS_LAST_ACK:
2071252ca428SRobert Watson 				KASSERT(ti_locked == TI_WLOCKED,
2072252ca428SRobert Watson 				    ("tcp_do_segment: TH_RST 2 ti_locked %d",
2073252ca428SRobert Watson 				    ti_locked));
2074252ca428SRobert Watson 				INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2075252ca428SRobert Watson 
207680ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
207780ab7c0eSGarrett Wollman 				break;
207880ab7c0eSGarrett Wollman 			}
207980ab7c0eSGarrett Wollman 		}
208080ab7c0eSGarrett Wollman 		goto drop;
208180ab7c0eSGarrett Wollman 	}
208280ab7c0eSGarrett Wollman 
208380ab7c0eSGarrett Wollman 	/*
2084df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
2085df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
2086df8bae1dSRodney W. Grimes 	 */
2087be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
208880ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
2089df8bae1dSRodney W. Grimes 
2090df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
2091d8951c8aSBjoern A. Zeeb 		if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
2092df8bae1dSRodney W. Grimes 			/*
2093df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
2094df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
2095df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
2096df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
2097df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
2098df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
2099df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
2100df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
2101df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
2102df8bae1dSRodney W. Grimes 			 */
2103df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
2104df8bae1dSRodney W. Grimes 		} else {
210578b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
210678b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, tlen);
210778b50714SRobert Watson 			TCPSTAT_INC(tcps_pawsdrop);
210807fd333dSMatthew Dillon 			if (tlen)
2109df8bae1dSRodney W. Grimes 				goto dropafterack;
21101ab4789dSMatthew Dillon 			goto drop;
21111ab4789dSMatthew Dillon 		}
2112df8bae1dSRodney W. Grimes 	}
2113df8bae1dSRodney W. Grimes 
2114a0292f23SGarrett Wollman 	/*
211580ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
211680ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
211780ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
211880ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
211980ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
212080ab7c0eSGarrett Wollman 	 */
2121a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
2122a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2123a57815efSBosko Milekic 		goto dropwithreset;
2124a57815efSBosko Milekic 	}
212580ab7c0eSGarrett Wollman 
2126fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
2127df8bae1dSRodney W. Grimes 	if (todrop > 0) {
212881ad7eb0SZachary Loafman 		/*
212981ad7eb0SZachary Loafman 		 * If this is a duplicate SYN for our current connection,
213081ad7eb0SZachary Loafman 		 * advance over it and pretend and it's not a SYN.
213181ad7eb0SZachary Loafman 		 */
213281ad7eb0SZachary Loafman 		if (thflags & TH_SYN && th->th_seq == tp->irs) {
2133fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
2134fb59c426SYoshinobu Inoue 			th->th_seq++;
2135fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
2136fb59c426SYoshinobu Inoue 				th->th_urp--;
2137df8bae1dSRodney W. Grimes 			else
2138fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
2139df8bae1dSRodney W. Grimes 			todrop--;
2140df8bae1dSRodney W. Grimes 		}
2141df8bae1dSRodney W. Grimes 		/*
2142dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
2143df8bae1dSRodney W. Grimes 		 */
2144fb59c426SYoshinobu Inoue 		if (todrop > tlen
2145fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
2146dac20301SGarrett Wollman 			/*
2147dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
2148dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
2149dac20301SGarrett Wollman 			 * of sequence; drop it.
2150dac20301SGarrett Wollman 			 */
2151fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
2152dac20301SGarrett Wollman 
2153df8bae1dSRodney W. Grimes 			/*
2154dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
2155dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
2156df8bae1dSRodney W. Grimes 			 */
2157dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
2158fb59c426SYoshinobu Inoue 			todrop = tlen;
215978b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
216078b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, todrop);
2161df8bae1dSRodney W. Grimes 		} else {
216278b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpartduppack);
216378b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop);
2164df8bae1dSRodney W. Grimes 		}
2165fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
2166fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
2167fb59c426SYoshinobu Inoue 		tlen -= todrop;
2168fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
2169fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
2170df8bae1dSRodney W. Grimes 		else {
2171fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
2172fb59c426SYoshinobu Inoue 			th->th_urp = 0;
2173df8bae1dSRodney W. Grimes 		}
2174df8bae1dSRodney W. Grimes 	}
2175df8bae1dSRodney W. Grimes 
2176df8bae1dSRodney W. Grimes 	/*
2177df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
2178df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
2179df8bae1dSRodney W. Grimes 	 */
2180df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
2181fb59c426SYoshinobu Inoue 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
2182773673c1SAndre Oppermann 		char *s;
2183773673c1SAndre Oppermann 
2184252ca428SRobert Watson 		KASSERT(ti_locked == TI_WLOCKED, ("%s: SS_NOFDEREF && "
2185252ca428SRobert Watson 		    "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked));
2186252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2187252ca428SRobert Watson 
2188773673c1SAndre Oppermann 		if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, NULL))) {
2189e31d8aa3SMike Silbersack 			log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data after socket "
2190773673c1SAndre Oppermann 			    "was closed, sending RST and removing tcpcb\n",
2191e31d8aa3SMike Silbersack 			    s, __func__, tcpstates[tp->t_state], tlen);
2192773673c1SAndre Oppermann 			free(s, M_TCPLOG);
2193773673c1SAndre Oppermann 		}
2194df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
219578b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvafterclose);
2196a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
2197df8bae1dSRodney W. Grimes 		goto dropwithreset;
2198df8bae1dSRodney W. Grimes 	}
2199df8bae1dSRodney W. Grimes 
2200df8bae1dSRodney W. Grimes 	/*
2201df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
2202df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
2203df8bae1dSRodney W. Grimes 	 */
2204fb59c426SYoshinobu Inoue 	todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
2205df8bae1dSRodney W. Grimes 	if (todrop > 0) {
220678b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvpackafterwin);
2207fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
220878b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen);
2209df8bae1dSRodney W. Grimes 			/*
2210df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
2211df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
2212df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
2213df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
2214df8bae1dSRodney W. Grimes 			 * and ack.
2215df8bae1dSRodney W. Grimes 			 */
2216fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
2217df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
221878b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvwinprobe);
2219df8bae1dSRodney W. Grimes 			} else
2220df8bae1dSRodney W. Grimes 				goto dropafterack;
2221df8bae1dSRodney W. Grimes 		} else
222278b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2223df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
2224fb59c426SYoshinobu Inoue 		tlen -= todrop;
2225fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
2226df8bae1dSRodney W. Grimes 	}
2227df8bae1dSRodney W. Grimes 
2228df8bae1dSRodney W. Grimes 	/*
2229df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
2230df8bae1dSRodney W. Grimes 	 * record its timestamp.
2231cf09195bSPaul Saab 	 * NOTE:
2232cf09195bSPaul Saab 	 * 1) That the test incorporates suggestions from the latest
2233a0292f23SGarrett Wollman 	 *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
2234cf09195bSPaul Saab 	 * 2) That updating only on newer timestamps interferes with
2235cf09195bSPaul Saab 	 *    our earlier PAWS tests, so this check should be solely
2236cf09195bSPaul Saab 	 *    predicated on the sequence space of this segment.
2237cf09195bSPaul Saab 	 * 3) That we modify the segment boundary check to be
2238cf09195bSPaul Saab 	 *        Last.ACK.Sent <= SEG.SEQ + SEG.Len
2239cf09195bSPaul Saab 	 *    instead of RFC1323's
2240cf09195bSPaul Saab 	 *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
2241cf09195bSPaul Saab 	 *    This modified check allows us to overcome RFC1323's
2242cf09195bSPaul Saab 	 *    limitations as described in Stevens TCP/IP Illustrated
2243cf09195bSPaul Saab 	 *    Vol. 2 p.869. In such cases, we can still calculate the
2244cf09195bSPaul Saab 	 *    RTT correctly when RCV.NXT == Last.ACK.Sent.
2245df8bae1dSRodney W. Grimes 	 */
2246be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
2247cf09195bSPaul Saab 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
2248cf09195bSPaul Saab 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
2249cf09195bSPaul Saab 		((thflags & (TH_SYN|TH_FIN)) != 0))) {
2250d8951c8aSBjoern A. Zeeb 		tp->ts_recent_age = tcp_ts_getticks();
2251a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
2252df8bae1dSRodney W. Grimes 	}
2253df8bae1dSRodney W. Grimes 
2254df8bae1dSRodney W. Grimes 	/*
2255df8bae1dSRodney W. Grimes 	 * If a SYN is in the window, then this is an
2256df8bae1dSRodney W. Grimes 	 * error and we send an RST and drop the connection.
2257df8bae1dSRodney W. Grimes 	 */
2258fb59c426SYoshinobu Inoue 	if (thflags & TH_SYN) {
2259252ca428SRobert Watson 		KASSERT(ti_locked == TI_WLOCKED,
2260252ca428SRobert Watson 		    ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked));
2261252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2262252ca428SRobert Watson 
2263df8bae1dSRodney W. Grimes 		tp = tcp_drop(tp, ECONNRESET);
2264a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
2265122aad88SAndre Oppermann 		goto drop;
2266df8bae1dSRodney W. Grimes 	}
2267df8bae1dSRodney W. Grimes 
2268a0292f23SGarrett Wollman 	/*
2269a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
2270a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
2271a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
2272a0292f23SGarrett Wollman 	 */
2273fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
2274a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
2275a0292f23SGarrett Wollman 		    (tp->t_flags & TF_NEEDSYN))
2276a0292f23SGarrett Wollman 			goto step6;
22775e1aa279SDavid Malone 		else if (tp->t_flags & TF_ACKNOW)
22785e1aa279SDavid Malone 			goto dropafterack;
2279a0292f23SGarrett Wollman 		else
2280a0292f23SGarrett Wollman 			goto drop;
2281a0292f23SGarrett Wollman 	}
2282df8bae1dSRodney W. Grimes 
2283df8bae1dSRodney W. Grimes 	/*
2284df8bae1dSRodney W. Grimes 	 * Ack processing.
2285df8bae1dSRodney W. Grimes 	 */
2286df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2287df8bae1dSRodney W. Grimes 
2288df8bae1dSRodney W. Grimes 	/*
2289764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
2290764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
2291764d8cefSBill Fenner 	 * The ACK was checked above.
2292df8bae1dSRodney W. Grimes 	 */
2293df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2294a0292f23SGarrett Wollman 
229578b50714SRobert Watson 		TCPSTAT_INC(tcps_connects);
2296df8bae1dSRodney W. Grimes 		soisconnected(so);
2297df8bae1dSRodney W. Grimes 		/* Do window scaling? */
2298df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2299df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2300df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
2301464fcfbcSAndre Oppermann 			tp->snd_wnd = tiwin;
2302df8bae1dSRodney W. Grimes 		}
2303a0292f23SGarrett Wollman 		/*
2304a0292f23SGarrett Wollman 		 * Make transitions:
2305a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
2306a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
2307a0292f23SGarrett Wollman 		 */
23089b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
2309a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDFIN) {
2310a0292f23SGarrett Wollman 			tp->t_state = TCPS_FIN_WAIT_1;
2311a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDFIN;
23127ff19458SPaul Traina 		} else {
2313a0292f23SGarrett Wollman 			tp->t_state = TCPS_ESTABLISHED;
2314dbc42409SLawrence Stewart 			cc_conn_init(tp);
23159077f387SGleb Smirnoff 			tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
23167ff19458SPaul Traina 		}
2317a0292f23SGarrett Wollman 		/*
2318a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
2319a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
2320a0292f23SGarrett Wollman 		 */
2321fb59c426SYoshinobu Inoue 		if (tlen == 0 && (thflags & TH_FIN) == 0)
2322fb59c426SYoshinobu Inoue 			(void) tcp_reass(tp, (struct tcphdr *)0, 0,
2323a0292f23SGarrett Wollman 			    (struct mbuf *)0);
2324fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
232593b0017fSPhilippe Charnier 		/* FALLTHROUGH */
2326df8bae1dSRodney W. Grimes 
2327df8bae1dSRodney W. Grimes 	/*
2328df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2329df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
2330fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
2331fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
2332df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
2333df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
2334df8bae1dSRodney W. Grimes 	 */
2335df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
2336df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
2337df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
2338df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
2339df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
2340df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
23415a53ca16SPaul Saab 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
234278b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvacktoomuch);
23435a53ca16SPaul Saab 			goto dropafterack;
23445a53ca16SPaul Saab 		}
23453529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
2346fc30a251SAndre Oppermann 		    ((to.to_flags & TOF_SACK) ||
2347fc30a251SAndre Oppermann 		     !TAILQ_EMPTY(&tp->snd_holes)))
23485a53ca16SPaul Saab 			tcp_sack_doack(tp, &to, th->th_ack);
234939bc9de5SLawrence Stewart 
235039bc9de5SLawrence Stewart 		/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
235139bc9de5SLawrence Stewart 		hhook_run_tcp_est_in(tp, th, &to);
235239bc9de5SLawrence Stewart 
2353fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
2354fb59c426SYoshinobu Inoue 			if (tlen == 0 && tiwin == tp->snd_wnd) {
235578b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvdupack);
2356df8bae1dSRodney W. Grimes 				/*
2357df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
2358df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
2359df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
2360df8bae1dSRodney W. Grimes 				 * change), the ack is the biggest we've
2361df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
2362df8bae1dSRodney W. Grimes 				 * threshhold of them, assume a packet
2363df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
2364df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
2365df8bae1dSRodney W. Grimes 				 * window so we send only this one
2366df8bae1dSRodney W. Grimes 				 * packet.
2367df8bae1dSRodney W. Grimes 				 *
2368df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
2369df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
2370df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
2371df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
2372df8bae1dSRodney W. Grimes 				 * the new ssthresh).
2373df8bae1dSRodney W. Grimes 				 *
2374df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
2375df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
2376df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
2377df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
2378df8bae1dSRodney W. Grimes 				 * network.
2379f2512ba1SRui Paulo 				 *
2380f2512ba1SRui Paulo 				 * When using TCP ECN, notify the peer that
2381f2512ba1SRui Paulo 				 * we reduced the cwnd.
2382df8bae1dSRodney W. Grimes 				 */
2383b8152ba7SAndre Oppermann 				if (!tcp_timer_active(tp, TT_REXMT) ||
2384fb59c426SYoshinobu Inoue 				    th->th_ack != tp->snd_una)
2385df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
2386cb942153SJeffrey Hsu 				else if (++tp->t_dupacks > tcprexmtthresh ||
2387dbc42409SLawrence Stewart 				     IN_FASTRECOVERY(tp->t_flags)) {
2388dbc42409SLawrence Stewart 					cc_ack_received(tp, th, CC_DUPACK);
23893529149eSAndre Oppermann 					if ((tp->t_flags & TF_SACK_PERMIT) &&
2390dbc42409SLawrence Stewart 					    IN_FASTRECOVERY(tp->t_flags)) {
2391808f11b7SPaul Saab 						int awnd;
239225e6f9edSPaul Saab 
239325e6f9edSPaul Saab 						/*
239425e6f9edSPaul Saab 						 * Compute the amount of data in flight first.
239525e6f9edSPaul Saab 						 * We can inject new data into the pipe iff
239625e6f9edSPaul Saab 						 * we have less than 1/2 the original window's
239725e6f9edSPaul Saab 						 * worth of data in flight.
239825e6f9edSPaul Saab 						 */
2399808f11b7SPaul Saab 						awnd = (tp->snd_nxt - tp->snd_fack) +
24000077b016SPaul Saab 							tp->sackhint.sack_bytes_rexmit;
2401808f11b7SPaul Saab 						if (awnd < tp->snd_ssthresh) {
240225e6f9edSPaul Saab 							tp->snd_cwnd += tp->t_maxseg;
240325e6f9edSPaul Saab 							if (tp->snd_cwnd > tp->snd_ssthresh)
240425e6f9edSPaul Saab 								tp->snd_cwnd = tp->snd_ssthresh;
240525e6f9edSPaul Saab 						}
240625e6f9edSPaul Saab 					} else
240746f58482SJonathan Lemon 						tp->snd_cwnd += tp->t_maxseg;
2408ec03d543SRandall Stewart 					if ((thflags & TH_FIN) &&
2409ec03d543SRandall Stewart 					    (TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
2410ec03d543SRandall Stewart 						/*
2411ec03d543SRandall Stewart 						 * If its a fin we need to process
2412ec03d543SRandall Stewart 						 * it to avoid a race where both
2413ec03d543SRandall Stewart 						 * sides enter FIN-WAIT and send FIN|ACK
2414ec03d543SRandall Stewart 						 * at the same time.
2415ec03d543SRandall Stewart 						 */
2416ec03d543SRandall Stewart 						break;
2417ec03d543SRandall Stewart 					}
241846f58482SJonathan Lemon 					(void) tcp_output(tp);
241946f58482SJonathan Lemon 					goto drop;
2420cb942153SJeffrey Hsu 				} else if (tp->t_dupacks == tcprexmtthresh) {
2421cb942153SJeffrey Hsu 					tcp_seq onxt = tp->snd_nxt;
2422a0445c2eSJayanth Vijayaraghavan 
2423a0445c2eSJayanth Vijayaraghavan 					/*
2424a0445c2eSJayanth Vijayaraghavan 					 * If we're doing sack, check to
2425a0445c2eSJayanth Vijayaraghavan 					 * see if we're already in sack
2426a0445c2eSJayanth Vijayaraghavan 					 * recovery. If we're not doing sack,
2427a0445c2eSJayanth Vijayaraghavan 					 * check to see if we're in newreno
2428a0445c2eSJayanth Vijayaraghavan 					 * recovery.
2429a0445c2eSJayanth Vijayaraghavan 					 */
24303529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
2431dbc42409SLawrence Stewart 						if (IN_FASTRECOVERY(tp->t_flags)) {
2432a0445c2eSJayanth Vijayaraghavan 							tp->t_dupacks = 0;
2433a0445c2eSJayanth Vijayaraghavan 							break;
2434a0445c2eSJayanth Vijayaraghavan 						}
2435dbc42409SLawrence Stewart 					} else {
2436a0445c2eSJayanth Vijayaraghavan 						if (SEQ_LEQ(th->th_ack,
24379d11646dSJeffrey Hsu 						    tp->snd_recover)) {
2438cb942153SJeffrey Hsu 							tp->t_dupacks = 0;
2439cb942153SJeffrey Hsu 							break;
244046f58482SJonathan Lemon 						}
2441a0445c2eSJayanth Vijayaraghavan 					}
2442dbc42409SLawrence Stewart 					/* Congestion signal before ack. */
2443dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_NDUPACK);
2444dbc42409SLawrence Stewart 					cc_ack_received(tp, th, CC_DUPACK);
2445b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
24469b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
24473529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
244878b50714SRobert Watson 						TCPSTAT_INC(
244978b50714SRobert Watson 						    tcps_sack_recovery_episode);
2450a55db2b6SPaul Saab 						tp->sack_newdata = tp->snd_nxt;
24518db456bfSPaul Saab 						tp->snd_cwnd = tp->t_maxseg;
24526d90faf3SPaul Saab 						(void) tcp_output(tp);
24536d90faf3SPaul Saab 						goto drop;
24546d90faf3SPaul Saab 					}
2455fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
2456df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->t_maxseg;
2457ec03d543SRandall Stewart 					if ((thflags & TH_FIN) &&
2458ec03d543SRandall Stewart 					    (TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
2459ec03d543SRandall Stewart 						/*
2460ec03d543SRandall Stewart 						 * If its a fin we need to process
2461ec03d543SRandall Stewart 						 * it to avoid a race where both
2462ec03d543SRandall Stewart 						 * sides enter FIN-WAIT and send FIN|ACK
2463ec03d543SRandall Stewart 						 * at the same time.
2464ec03d543SRandall Stewart 						 */
2465ec03d543SRandall Stewart 						break;
2466ec03d543SRandall Stewart 					}
2467df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
246848d2549cSJeffrey Hsu 					KASSERT(tp->snd_limited <= 2,
2469302ce8d6SAndre Oppermann 					    ("%s: tp->snd_limited too big",
2470302ce8d6SAndre Oppermann 					    __func__));
2471df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
247248d2549cSJeffrey Hsu 					     tp->t_maxseg *
247348d2549cSJeffrey Hsu 					     (tp->t_dupacks - tp->snd_limited);
2474df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
2475df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
2476df8bae1dSRodney W. Grimes 					goto drop;
2477603724d3SBjoern A. Zeeb 				} else if (V_tcp_do_rfc3042) {
2478dbc42409SLawrence Stewart 					cc_ack_received(tp, th, CC_DUPACK);
2479582a954bSJeffrey Hsu 					u_long oldcwnd = tp->snd_cwnd;
248048d2549cSJeffrey Hsu 					tcp_seq oldsndmax = tp->snd_max;
248148d2549cSJeffrey Hsu 					u_int sent;
248289c02376SJeffrey Hsu 
2483582a954bSJeffrey Hsu 					KASSERT(tp->t_dupacks == 1 ||
2484582a954bSJeffrey Hsu 					    tp->t_dupacks == 2,
2485302ce8d6SAndre Oppermann 					    ("%s: dupacks not 1 or 2",
2486302ce8d6SAndre Oppermann 					    __func__));
248761a36e3dSJeffrey Hsu 					if (tp->t_dupacks == 1)
248848d2549cSJeffrey Hsu 						tp->snd_limited = 0;
248961a36e3dSJeffrey Hsu 					tp->snd_cwnd =
249061a36e3dSJeffrey Hsu 					    (tp->snd_nxt - tp->snd_una) +
249161a36e3dSJeffrey Hsu 					    (tp->t_dupacks - tp->snd_limited) *
249261a36e3dSJeffrey Hsu 					    tp->t_maxseg;
2493ec03d543SRandall Stewart 					if ((thflags & TH_FIN) &&
2494ec03d543SRandall Stewart 					    (TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
2495ec03d543SRandall Stewart 						/*
2496ec03d543SRandall Stewart 						 * If its a fin we need to process
2497ec03d543SRandall Stewart 						 * it to avoid a race where both
2498ec03d543SRandall Stewart 						 * sides enter FIN-WAIT and send FIN|ACK
2499ec03d543SRandall Stewart 						 * at the same time.
2500ec03d543SRandall Stewart 						 */
2501ec03d543SRandall Stewart 						break;
2502ec03d543SRandall Stewart 					}
2503582a954bSJeffrey Hsu 					(void) tcp_output(tp);
250448d2549cSJeffrey Hsu 					sent = tp->snd_max - oldsndmax;
250548d2549cSJeffrey Hsu 					if (sent > tp->t_maxseg) {
250689c02376SJeffrey Hsu 						KASSERT((tp->t_dupacks == 2 &&
250789c02376SJeffrey Hsu 						    tp->snd_limited == 0) ||
250889c02376SJeffrey Hsu 						   (sent == tp->t_maxseg + 1 &&
250989c02376SJeffrey Hsu 						    tp->t_flags & TF_SENTFIN),
2510302ce8d6SAndre Oppermann 						    ("%s: sent too much",
2511302ce8d6SAndre Oppermann 						    __func__));
251248d2549cSJeffrey Hsu 						tp->snd_limited = 2;
251348d2549cSJeffrey Hsu 					} else if (sent > 0)
251448d2549cSJeffrey Hsu 						++tp->snd_limited;
2515582a954bSJeffrey Hsu 					tp->snd_cwnd = oldcwnd;
2516582a954bSJeffrey Hsu 					goto drop;
2517df8bae1dSRodney W. Grimes 				}
2518df8bae1dSRodney W. Grimes 			} else
2519df8bae1dSRodney W. Grimes 				tp->t_dupacks = 0;
2520df8bae1dSRodney W. Grimes 			break;
2521df8bae1dSRodney W. Grimes 		}
2522cb942153SJeffrey Hsu 
2523302ce8d6SAndre Oppermann 		KASSERT(SEQ_GT(th->th_ack, tp->snd_una),
2524302ce8d6SAndre Oppermann 		    ("%s: th_ack <= snd_una", __func__));
2525cb942153SJeffrey Hsu 
2526df8bae1dSRodney W. Grimes 		/*
2527df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
2528df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
2529df8bae1dSRodney W. Grimes 		 */
2530dbc42409SLawrence Stewart 		if (IN_FASTRECOVERY(tp->t_flags)) {
2531cb942153SJeffrey Hsu 			if (SEQ_LT(th->th_ack, tp->snd_recover)) {
25323529149eSAndre Oppermann 				if (tp->t_flags & TF_SACK_PERMIT)
25336d90faf3SPaul Saab 					tcp_sack_partialack(tp, th);
25346d90faf3SPaul Saab 				else
2535c068736aSJeffrey Hsu 					tcp_newreno_partial_ack(tp, th);
2536dbc42409SLawrence Stewart 			} else
2537dbc42409SLawrence Stewart 				cc_post_recovery(tp, th);
253846f58482SJonathan Lemon 		}
2539cb942153SJeffrey Hsu 		tp->t_dupacks = 0;
2540a0292f23SGarrett Wollman 		/*
2541a0292f23SGarrett Wollman 		 * If we reach this point, ACK is not a duplicate,
2542a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
2543a0292f23SGarrett Wollman 		 */
2544a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
2545a0292f23SGarrett Wollman 			/*
2546a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
2547a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
254807e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
254907e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
255007e43e10SAndras Olah 			 * we can do window scaling.
2551a0292f23SGarrett Wollman 			 */
2552a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
2553a0292f23SGarrett Wollman 			tp->snd_una++;
255407e43e10SAndras Olah 			/* Do window scaling? */
255507e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
255607e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
255707e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2558464fcfbcSAndre Oppermann 				/* Send window already scaled. */
255907e43e10SAndras Olah 			}
2560a0292f23SGarrett Wollman 		}
2561a0292f23SGarrett Wollman 
2562a0292f23SGarrett Wollman process_ACK:
25638501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
25647cfc6904SRobert Watson 
2565dbc42409SLawrence Stewart 		acked = BYTES_THIS_ACK(tp, th);
256678b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvackpack);
256778b50714SRobert Watson 		TCPSTAT_ADD(tcps_rcvackbyte, acked);
2568df8bae1dSRodney W. Grimes 
2569df8bae1dSRodney W. Grimes 		/*
25709b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
25719b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
25729b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
25739b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
25749b8b58e0SJonathan Lemon 		 * we left off.
25759b8b58e0SJonathan Lemon 		 */
2576672dc4aeSJohn Baldwin 		if (tp->t_rxtshift == 1 && tp->t_flags & TF_PREVVALID &&
2577672dc4aeSJohn Baldwin 		    (int)(ticks - tp->t_badrxtwin) < 0)
2578dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_RTO_ERR);
25799b8b58e0SJonathan Lemon 
25809b8b58e0SJonathan Lemon 		/*
2581df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
2582df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
2583df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
2584df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
2585df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
2586df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
2587df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
2588fa55172bSMatthew Dillon 		 *
2589fa55172bSMatthew Dillon 		 * Some boxes send broken timestamp replies
2590fa55172bSMatthew Dillon 		 * during the SYN+ACK phase, ignore
2591fa55172bSMatthew Dillon 		 * timestamps of 0 or we could calculate a
2592fa55172bSMatthew Dillon 		 * huge RTT and blow up the retransmit timer.
2593df8bae1dSRodney W. Grimes 		 */
2594d8951c8aSBjoern A. Zeeb 		if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) {
2595d8951c8aSBjoern A. Zeeb 			u_int t;
2596d8951c8aSBjoern A. Zeeb 
2597d8951c8aSBjoern A. Zeeb 			t = tcp_ts_getticks() - to.to_tsecr;
2598d8951c8aSBjoern A. Zeeb 			if (!tp->t_rttlow || tp->t_rttlow > t)
2599d8951c8aSBjoern A. Zeeb 				tp->t_rttlow = t;
2600d8951c8aSBjoern A. Zeeb 			tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1);
2601fa55172bSMatthew Dillon 		} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2602eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
2603eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - tp->t_rtttime;
26049b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2605fa55172bSMatthew Dillon 		}
2606df8bae1dSRodney W. Grimes 
2607df8bae1dSRodney W. Grimes 		/*
2608df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
2609df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
2610df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
2611df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
2612df8bae1dSRodney W. Grimes 		 */
2613fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
2614b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
2615df8bae1dSRodney W. Grimes 			needoutput = 1;
2616b8152ba7SAndre Oppermann 		} else if (!tcp_timer_active(tp, TT_PERSIST))
2617b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
2618a0292f23SGarrett Wollman 
2619a0292f23SGarrett Wollman 		/*
2620a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
2621a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
2622a0292f23SGarrett Wollman 		 */
2623a0292f23SGarrett Wollman 		if (acked == 0)
2624a0292f23SGarrett Wollman 			goto step6;
2625a0292f23SGarrett Wollman 
2626df8bae1dSRodney W. Grimes 		/*
2627dbc42409SLawrence Stewart 		 * Let the congestion control algorithm update congestion
2628dbc42409SLawrence Stewart 		 * control related information. This typically means increasing
2629dbc42409SLawrence Stewart 		 * the congestion window.
2630df8bae1dSRodney W. Grimes 		 */
2631dbc42409SLawrence Stewart 		cc_ack_received(tp, th, CC_ACK);
2632dbc42409SLawrence Stewart 
26335905999bSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
2634df8bae1dSRodney W. Grimes 		if (acked > so->so_snd.sb_cc) {
2635df8bae1dSRodney W. Grimes 			tp->snd_wnd -= so->so_snd.sb_cc;
26365905999bSRobert Watson 			sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc);
2637df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
2638df8bae1dSRodney W. Grimes 		} else {
26395905999bSRobert Watson 			sbdrop_locked(&so->so_snd, acked);
2640df8bae1dSRodney W. Grimes 			tp->snd_wnd -= acked;
2641df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
2642df8bae1dSRodney W. Grimes 		}
2643564aab1fSAndre Oppermann 		/* NB: sowwakeup_locked() does an implicit unlock. */
26441e4d7da7SRobert Watson 		sowwakeup_locked(so);
2645e8949f74SAndre Oppermann 		/* Detect una wraparound. */
2646dbc42409SLawrence Stewart 		if (!IN_RECOVERY(tp->t_flags) &&
26479d11646dSJeffrey Hsu 		    SEQ_GT(tp->snd_una, tp->snd_recover) &&
26489d11646dSJeffrey Hsu 		    SEQ_LEQ(th->th_ack, tp->snd_recover))
26499d11646dSJeffrey Hsu 			tp->snd_recover = th->th_ack - 1;
2650dbc42409SLawrence Stewart 		/* XXXLAS: Can this be moved up into cc_post_recovery? */
2651dbc42409SLawrence Stewart 		if (IN_RECOVERY(tp->t_flags) &&
265224cb0f22SLawrence Stewart 		    SEQ_GEQ(th->th_ack, tp->snd_recover)) {
2653dbc42409SLawrence Stewart 			EXIT_RECOVERY(tp->t_flags);
265424cb0f22SLawrence Stewart 		}
2655fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
26563529149eSAndre Oppermann 		if (tp->t_flags & TF_SACK_PERMIT) {
26576d90faf3SPaul Saab 			if (SEQ_GT(tp->snd_una, tp->snd_recover))
26586d90faf3SPaul Saab 				tp->snd_recover = tp->snd_una;
26596d90faf3SPaul Saab 		}
2660df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2661df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
2662df8bae1dSRodney W. Grimes 
2663df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2664df8bae1dSRodney W. Grimes 
2665df8bae1dSRodney W. Grimes 		/*
2666df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
2667df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
2668df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
2669df8bae1dSRodney W. Grimes 		 */
2670df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2671df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2672df8bae1dSRodney W. Grimes 				/*
2673df8bae1dSRodney W. Grimes 				 * If we can't receive any more
2674df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
2675df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
2676df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
2677df8bae1dSRodney W. Grimes 				 * we'll hang forever.
2678e8949f74SAndre Oppermann 				 *
2679e8949f74SAndre Oppermann 				 * XXXjl:
2680340c35deSJonathan Lemon 				 * we should release the tp also, and use a
2681340c35deSJonathan Lemon 				 * compressed state.
2682340c35deSJonathan Lemon 				 */
2683c0b99ffaSRobert Watson 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
268403e49181SSeigo Tanimura 					soisdisconnected(so);
26859077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
26869077f387SGleb Smirnoff 					    (tcp_fast_finwait2_recycle ?
26879077f387SGleb Smirnoff 					    tcp_finwait2_timeout :
26889077f387SGleb Smirnoff 					    TP_MAXIDLE(tp)));
26894cc20ab1SSeigo Tanimura 				}
2690df8bae1dSRodney W. Grimes 				tp->t_state = TCPS_FIN_WAIT_2;
2691df8bae1dSRodney W. Grimes 			}
2692df8bae1dSRodney W. Grimes 			break;
2693df8bae1dSRodney W. Grimes 
2694df8bae1dSRodney W. Grimes 		/*
2695df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
2696df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
2697df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
2698df8bae1dSRodney W. Grimes 		 * the segment.
2699df8bae1dSRodney W. Grimes 		 */
2700df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
2701df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2702252ca428SRobert Watson 				INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
270311a20fb8SJeffrey Hsu 				tcp_twstart(tp);
2704603724d3SBjoern A. Zeeb 				INP_INFO_WUNLOCK(&V_tcbinfo);
2705340c35deSJonathan Lemon 				m_freem(m);
2706679d9708SAndre Oppermann 				return;
2707df8bae1dSRodney W. Grimes 			}
2708df8bae1dSRodney W. Grimes 			break;
2709df8bae1dSRodney W. Grimes 
2710df8bae1dSRodney W. Grimes 		/*
2711df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
2712df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
2713df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
2714df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
2715df8bae1dSRodney W. Grimes 		 */
2716df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
2717df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2718252ca428SRobert Watson 				INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2719df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
2720df8bae1dSRodney W. Grimes 				goto drop;
2721df8bae1dSRodney W. Grimes 			}
2722df8bae1dSRodney W. Grimes 			break;
2723df8bae1dSRodney W. Grimes 		}
2724df8bae1dSRodney W. Grimes 	}
2725df8bae1dSRodney W. Grimes 
2726df8bae1dSRodney W. Grimes step6:
27278501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
27287cfc6904SRobert Watson 
2729df8bae1dSRodney W. Grimes 	/*
2730df8bae1dSRodney W. Grimes 	 * Update window information.
2731df8bae1dSRodney W. Grimes 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2732df8bae1dSRodney W. Grimes 	 */
2733fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) &&
2734fb59c426SYoshinobu Inoue 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
2735fb59c426SYoshinobu Inoue 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
2736fb59c426SYoshinobu Inoue 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
2737df8bae1dSRodney W. Grimes 		/* keep track of pure window updates */
2738fb59c426SYoshinobu Inoue 		if (tlen == 0 &&
2739fb59c426SYoshinobu Inoue 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
274078b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvwinupd);
2741df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
2742fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq;
2743fb59c426SYoshinobu Inoue 		tp->snd_wl2 = th->th_ack;
2744df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
2745df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
2746df8bae1dSRodney W. Grimes 		needoutput = 1;
2747df8bae1dSRodney W. Grimes 	}
2748df8bae1dSRodney W. Grimes 
2749df8bae1dSRodney W. Grimes 	/*
2750df8bae1dSRodney W. Grimes 	 * Process segments with URG.
2751df8bae1dSRodney W. Grimes 	 */
2752fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
2753df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2754df8bae1dSRodney W. Grimes 		/*
2755df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
2756df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
2757df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
2758df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
2759df8bae1dSRodney W. Grimes 		 */
276018ad5842SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
2761fb59c426SYoshinobu Inoue 		if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
2762fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
2763fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
276418ad5842SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);	/* XXX */
2765df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
2766df8bae1dSRodney W. Grimes 		}
2767df8bae1dSRodney W. Grimes 		/*
2768df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
2769df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
2770df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2771df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
2772df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
2773df8bae1dSRodney W. Grimes 		 *
2774df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
2775df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
2776df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
2777df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
2778df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
2779df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
2780df8bae1dSRodney W. Grimes 		 */
2781fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2782fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
2783df8bae1dSRodney W. Grimes 			so->so_oobmark = so->so_rcv.sb_cc +
2784df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
2785927c5ceaSRobert Watson 			if (so->so_oobmark == 0)
2786c0b99ffaSRobert Watson 				so->so_rcv.sb_state |= SBS_RCVATMARK;
2787df8bae1dSRodney W. Grimes 			sohasoutofband(so);
2788df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2789df8bae1dSRodney W. Grimes 		}
279018ad5842SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
2791df8bae1dSRodney W. Grimes 		/*
2792df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
2793df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
2794df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
2795df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
2796df8bae1dSRodney W. Grimes 		 */
279730613f56SJeffrey Hsu 		if (th->th_urp <= (u_long)tlen &&
279830613f56SJeffrey Hsu 		    !(so->so_options & SO_OOBINLINE)) {
279930613f56SJeffrey Hsu 			/* hdr drop is delayed */
280030613f56SJeffrey Hsu 			tcp_pulloutofband(so, th, m, drop_hdrlen);
280130613f56SJeffrey Hsu 		}
2802c068736aSJeffrey Hsu 	} else {
2803df8bae1dSRodney W. Grimes 		/*
2804df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
2805df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
2806df8bae1dSRodney W. Grimes 		 * with the receive window.
2807df8bae1dSRodney W. Grimes 		 */
2808df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2809df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
2810c068736aSJeffrey Hsu 	}
2811df8bae1dSRodney W. Grimes dodata:							/* XXX */
28128501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
28137cfc6904SRobert Watson 
2814df8bae1dSRodney W. Grimes 	/*
2815df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
2816df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
2817df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
2818df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
2819df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
2820df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
2821df8bae1dSRodney W. Grimes 	 */
2822fb59c426SYoshinobu Inoue 	if ((tlen || (thflags & TH_FIN)) &&
2823df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
282469e03620SPaul Saab 		tcp_seq save_start = th->th_seq;
2825fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
2826e4b64281SJesper Skriver 		/*
2827c068736aSJeffrey Hsu 		 * Insert segment which includes th into TCP reassembly queue
2828c068736aSJeffrey Hsu 		 * with control block tp.  Set thflags to whether reassembly now
2829c068736aSJeffrey Hsu 		 * includes a segment with FIN.  This handles the common case
2830c068736aSJeffrey Hsu 		 * inline (segment is the next to be received on an established
2831c068736aSJeffrey Hsu 		 * connection, and the queue is empty), avoiding linkage into
2832c068736aSJeffrey Hsu 		 * and removal from the queue and repetition of various
2833c068736aSJeffrey Hsu 		 * conversions.
2834c068736aSJeffrey Hsu 		 * Set DELACK for segments received in order, but ack
2835c068736aSJeffrey Hsu 		 * immediately when segments are out of order (so
2836c068736aSJeffrey Hsu 		 * fast retransmit can work).
2837e4b64281SJesper Skriver 		 */
2838e4b64281SJesper Skriver 		if (th->th_seq == tp->rcv_nxt &&
2839e4b64281SJesper Skriver 		    LIST_EMPTY(&tp->t_segq) &&
2840e4b64281SJesper Skriver 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
2841e4b64281SJesper Skriver 			if (DELAY_ACK(tp))
28423bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
2843e4b64281SJesper Skriver 			else
2844e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
2845e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
2846e4b64281SJesper Skriver 			thflags = th->th_flags & TH_FIN;
284778b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpack);
284878b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
2849e4b64281SJesper Skriver 			ND6_HINT(tp);
28501e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
2851c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
2852c1c36a2cSMike Silbersack 				m_freem(m);
2853c1c36a2cSMike Silbersack 			else
28541e4d7da7SRobert Watson 				sbappendstream_locked(&so->so_rcv, m);
2855e8949f74SAndre Oppermann 			/* NB: sorwakeup_locked() does an implicit unlock. */
28561e4d7da7SRobert Watson 			sorwakeup_locked(so);
2857e4b64281SJesper Skriver 		} else {
2858e8949f74SAndre Oppermann 			/*
2859e8949f74SAndre Oppermann 			 * XXX: Due to the header drop above "th" is
2860e8949f74SAndre Oppermann 			 * theoretically invalid by now.  Fortunately
2861e8949f74SAndre Oppermann 			 * m_adj() doesn't actually frees any mbufs
2862e8949f74SAndre Oppermann 			 * when trimming from the head.
2863e8949f74SAndre Oppermann 			 */
2864e4b64281SJesper Skriver 			thflags = tcp_reass(tp, th, &tlen, m);
2865e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
2866e4b64281SJesper Skriver 		}
28673529149eSAndre Oppermann 		if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT))
2868f194524fSAndre Oppermann 			tcp_update_sack_list(tp, save_start, save_start + tlen);
2869302ce8d6SAndre Oppermann #if 0
2870df8bae1dSRodney W. Grimes 		/*
2871df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
2872df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
2873df8bae1dSRodney W. Grimes 		 * buffer size.
2874302ce8d6SAndre Oppermann 		 * XXX: Unused.
2875df8bae1dSRodney W. Grimes 		 */
2876f701e30dSJohn Baldwin 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt))
2877df8bae1dSRodney W. Grimes 			len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2878f701e30dSJohn Baldwin 		else
2879f701e30dSJohn Baldwin 			len = so->so_rcv.sb_hiwat;
2880302ce8d6SAndre Oppermann #endif
2881df8bae1dSRodney W. Grimes 	} else {
2882df8bae1dSRodney W. Grimes 		m_freem(m);
2883fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
2884df8bae1dSRodney W. Grimes 	}
2885df8bae1dSRodney W. Grimes 
2886df8bae1dSRodney W. Grimes 	/*
2887df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
2888df8bae1dSRodney W. Grimes 	 * that the connection is closing.
2889df8bae1dSRodney W. Grimes 	 */
2890fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
2891df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2892df8bae1dSRodney W. Grimes 			socantrcvmore(so);
2893a0292f23SGarrett Wollman 			/*
2894a0292f23SGarrett Wollman 			 * If connection is half-synchronized
2895d3eede9dSAndras Olah 			 * (ie NEEDSYN flag on) then delay ACK,
2896a0292f23SGarrett Wollman 			 * so it may be piggybacked when SYN is sent.
2897a0292f23SGarrett Wollman 			 * Otherwise, since we received a FIN then no
2898a0292f23SGarrett Wollman 			 * more input can be expected, send ACK now.
2899a0292f23SGarrett Wollman 			 */
29003bfd6421SJonathan Lemon 			if (tp->t_flags & TF_NEEDSYN)
29013bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
2902a0292f23SGarrett Wollman 			else
2903df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
2904df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
2905df8bae1dSRodney W. Grimes 		}
2906df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2907df8bae1dSRodney W. Grimes 
2908df8bae1dSRodney W. Grimes 		/*
2909df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
2910df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
2911df8bae1dSRodney W. Grimes 		 */
2912df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
29139b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
29149b8b58e0SJonathan Lemon 			/* FALLTHROUGH */
2915df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
2916df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSE_WAIT;
2917df8bae1dSRodney W. Grimes 			break;
2918df8bae1dSRodney W. Grimes 
2919df8bae1dSRodney W. Grimes 		/*
2920df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2921df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
2922df8bae1dSRodney W. Grimes 		 */
2923df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2924df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSING;
2925df8bae1dSRodney W. Grimes 			break;
2926df8bae1dSRodney W. Grimes 
2927df8bae1dSRodney W. Grimes 		/*
2928df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2929df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
2930df8bae1dSRodney W. Grimes 		 * standard timers.
2931df8bae1dSRodney W. Grimes 		 */
2932df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
2933252ca428SRobert Watson 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2934252ca428SRobert Watson 			KASSERT(ti_locked == TI_WLOCKED, ("%s: dodata "
2935252ca428SRobert Watson 			    "TCP_FIN_WAIT_2 ti_locked: %d", __func__,
2936252ca428SRobert Watson 			    ti_locked));
2937252ca428SRobert Watson 
2938340c35deSJonathan Lemon 			tcp_twstart(tp);
2939603724d3SBjoern A. Zeeb 			INP_INFO_WUNLOCK(&V_tcbinfo);
2940679d9708SAndre Oppermann 			return;
2941df8bae1dSRodney W. Grimes 		}
2942df8bae1dSRodney W. Grimes 	}
2943fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED)
2944603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
2945252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
2946252ca428SRobert Watson 
2947610ee2f9SDavid Greenman #ifdef TCPDEBUG
29484cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
2949fb59c426SYoshinobu Inoue 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
2950fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2951610ee2f9SDavid Greenman #endif
2952df8bae1dSRodney W. Grimes 
2953df8bae1dSRodney W. Grimes 	/*
2954df8bae1dSRodney W. Grimes 	 * Return any desired output.
2955df8bae1dSRodney W. Grimes 	 */
2956df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
2957df8bae1dSRodney W. Grimes 		(void) tcp_output(tp);
29587792ea27SJeffrey Hsu 
2959a14c749fSJonathan Lemon check_delack:
2960252ca428SRobert Watson 	KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d",
2961252ca428SRobert Watson 	    __func__, ti_locked));
2962603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
29638501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
2964252ca428SRobert Watson 
2965a14c749fSJonathan Lemon 	if (tp->t_flags & TF_DELACK) {
29663bfd6421SJonathan Lemon 		tp->t_flags &= ~TF_DELACK;
2967b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
29683bfd6421SJonathan Lemon 	}
29698501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
2970679d9708SAndre Oppermann 	return;
2971df8bae1dSRodney W. Grimes 
2972df8bae1dSRodney W. Grimes dropafterack:
2973df8bae1dSRodney W. Grimes 	/*
2974df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
2975df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
297680ab7c0eSGarrett Wollman 	 *
297780ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
297880ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
297980ab7c0eSGarrett Wollman 	 * RST have been dropped.
298080ab7c0eSGarrett Wollman 	 *
298180ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
298280ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
298380ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
298480ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
298580ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
298680ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
2987df8bae1dSRodney W. Grimes 	 */
2988fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
2989fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
2990a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
2991a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2992a57815efSBosko Milekic 		goto dropwithreset;
2993a57815efSBosko Milekic 	}
2994a0292f23SGarrett Wollman #ifdef TCPDEBUG
29954cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
2996fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2997fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2998a0292f23SGarrett Wollman #endif
2999fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED)
3000603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
3001252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
3002252ca428SRobert Watson 
3003df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
3004df8bae1dSRodney W. Grimes 	(void) tcp_output(tp);
30058501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
3006d6915262SRobert Watson 	m_freem(m);
3007679d9708SAndre Oppermann 	return;
3008df8bae1dSRodney W. Grimes 
3009df8bae1dSRodney W. Grimes dropwithreset:
3010fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED)
3011dd8ac7f9SRobert Watson 		INP_INFO_WUNLOCK(&V_tcbinfo);
3012252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
3013a57815efSBosko Milekic 
3014a0ca0871SRobert Watson 	if (tp != NULL) {
3015302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
30168501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
3017dd8ac7f9SRobert Watson 	} else
3018a0ca0871SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
3019679d9708SAndre Oppermann 	return;
3020df8bae1dSRodney W. Grimes 
3021df8bae1dSRodney W. Grimes drop:
3022fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED) {
3023252ca428SRobert Watson 		INP_INFO_WUNLOCK(&V_tcbinfo);
3024fa046d87SRobert Watson 		ti_locked = TI_UNLOCKED;
3025fa046d87SRobert Watson 	}
3026252ca428SRobert Watson #ifdef INVARIANTS
3027252ca428SRobert Watson 	else
3028252ca428SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
3029252ca428SRobert Watson #endif
3030252ca428SRobert Watson 
3031df8bae1dSRodney W. Grimes 	/*
3032df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
3033df8bae1dSRodney W. Grimes 	 */
3034610ee2f9SDavid Greenman #ifdef TCPDEBUG
30351c53f806SRobert Watson 	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
3036fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3037fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3038a0292f23SGarrett Wollman #endif
30391c53f806SRobert Watson 	if (tp != NULL)
30408501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
3041d6915262SRobert Watson 	m_freem(m);
3042302ce8d6SAndre Oppermann }
3043302ce8d6SAndre Oppermann 
3044302ce8d6SAndre Oppermann /*
30450ca3f933SAndre Oppermann  * Issue RST and make ACK acceptable to originator of segment.
30460ca3f933SAndre Oppermann  * The mbuf must still include the original packet header.
30470ca3f933SAndre Oppermann  * tp may be NULL.
3048302ce8d6SAndre Oppermann  */
3049302ce8d6SAndre Oppermann static void
3050302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp,
3051302ce8d6SAndre Oppermann     int tlen, int rstreason)
3052302ce8d6SAndre Oppermann {
3053b287c6c7SBjoern A. Zeeb #ifdef INET
3054302ce8d6SAndre Oppermann 	struct ip *ip;
3055b287c6c7SBjoern A. Zeeb #endif
3056302ce8d6SAndre Oppermann #ifdef INET6
3057302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6;
3058302ce8d6SAndre Oppermann #endif
30597a3244ccSRobert Watson 
30607a3244ccSRobert Watson 	if (tp != NULL) {
30618501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
30627a3244ccSRobert Watson 	}
30637a3244ccSRobert Watson 
30640ca3f933SAndre Oppermann 	/* Don't bother if destination was broadcast/multicast. */
3065302ce8d6SAndre Oppermann 	if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
3066302ce8d6SAndre Oppermann 		goto drop;
3067302ce8d6SAndre Oppermann #ifdef INET6
3068302ce8d6SAndre Oppermann 	if (mtod(m, struct ip *)->ip_v == 6) {
3069302ce8d6SAndre Oppermann 		ip6 = mtod(m, struct ip6_hdr *);
3070302ce8d6SAndre Oppermann 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3071302ce8d6SAndre Oppermann 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3072302ce8d6SAndre Oppermann 			goto drop;
3073302ce8d6SAndre Oppermann 		/* IPv6 anycast check is done at tcp6_input() */
3074b287c6c7SBjoern A. Zeeb 	}
3075302ce8d6SAndre Oppermann #endif
3076b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3077b287c6c7SBjoern A. Zeeb 	else
3078b287c6c7SBjoern A. Zeeb #endif
3079b287c6c7SBjoern A. Zeeb #ifdef INET
3080302ce8d6SAndre Oppermann 	{
3081302ce8d6SAndre Oppermann 		ip = mtod(m, struct ip *);
3082302ce8d6SAndre Oppermann 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3083302ce8d6SAndre Oppermann 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3084302ce8d6SAndre Oppermann 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3085302ce8d6SAndre Oppermann 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
3086302ce8d6SAndre Oppermann 			goto drop;
3087302ce8d6SAndre Oppermann 	}
3088b287c6c7SBjoern A. Zeeb #endif
3089302ce8d6SAndre Oppermann 
3090302ce8d6SAndre Oppermann 	/* Perform bandwidth limiting. */
3091302ce8d6SAndre Oppermann 	if (badport_bandlim(rstreason) < 0)
3092302ce8d6SAndre Oppermann 		goto drop;
3093302ce8d6SAndre Oppermann 
3094302ce8d6SAndre Oppermann 	/* tcp_respond consumes the mbuf chain. */
3095302ce8d6SAndre Oppermann 	if (th->th_flags & TH_ACK) {
3096302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0,
3097302ce8d6SAndre Oppermann 		    th->th_ack, TH_RST);
3098302ce8d6SAndre Oppermann 	} else {
3099302ce8d6SAndre Oppermann 		if (th->th_flags & TH_SYN)
3100302ce8d6SAndre Oppermann 			tlen++;
3101302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
3102302ce8d6SAndre Oppermann 		    (tcp_seq)0, TH_RST|TH_ACK);
3103302ce8d6SAndre Oppermann 	}
3104302ce8d6SAndre Oppermann 	return;
3105302ce8d6SAndre Oppermann drop:
3106302ce8d6SAndre Oppermann 	m_freem(m);
3107df8bae1dSRodney W. Grimes }
3108df8bae1dSRodney W. Grimes 
3109be2ac88cSJonathan Lemon /*
3110be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
3111be2ac88cSJonathan Lemon  */
31120312fbe9SPoul-Henning Kamp static void
3113ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags)
3114df8bae1dSRodney W. Grimes {
3115df8bae1dSRodney W. Grimes 	int opt, optlen;
3116df8bae1dSRodney W. Grimes 
3117be2ac88cSJonathan Lemon 	to->to_flags = 0;
3118df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
3119df8bae1dSRodney W. Grimes 		opt = cp[0];
3120df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
3121df8bae1dSRodney W. Grimes 			break;
3122df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
3123df8bae1dSRodney W. Grimes 			optlen = 1;
3124df8bae1dSRodney W. Grimes 		else {
3125b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
3126b474779fSJun-ichiro itojun Hagino 				break;
3127df8bae1dSRodney W. Grimes 			optlen = cp[1];
3128b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
3129df8bae1dSRodney W. Grimes 				break;
3130df8bae1dSRodney W. Grimes 		}
3131df8bae1dSRodney W. Grimes 		switch (opt) {
3132df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
3133df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
3134df8bae1dSRodney W. Grimes 				continue;
3135f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3136df8bae1dSRodney W. Grimes 				continue;
3137be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
3138be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
3139be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
3140fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
3141df8bae1dSRodney W. Grimes 			break;
3142df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
3143df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
3144df8bae1dSRodney W. Grimes 				continue;
3145f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3146df8bae1dSRodney W. Grimes 				continue;
3147be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
314802a1a643SAndre Oppermann 			to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT);
3149df8bae1dSRodney W. Grimes 			break;
3150df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
3151df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
3152df8bae1dSRodney W. Grimes 				continue;
3153be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
3154a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
3155a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
3156fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
3157a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
3158a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
3159fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
3160df8bae1dSRodney W. Grimes 			break;
31611cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
31621cfd4b53SBruce M Simpson 		/*
31631cfd4b53SBruce M Simpson 		 * XXX In order to reply to a host which has set the
31641cfd4b53SBruce M Simpson 		 * TCP_SIGNATURE option in its initial SYN, we have to
31651cfd4b53SBruce M Simpson 		 * record the fact that the option was observed here
31661cfd4b53SBruce M Simpson 		 * for the syncache code to perform the correct response.
31671cfd4b53SBruce M Simpson 		 */
31681cfd4b53SBruce M Simpson 		case TCPOPT_SIGNATURE:
31691cfd4b53SBruce M Simpson 			if (optlen != TCPOLEN_SIGNATURE)
31701cfd4b53SBruce M Simpson 				continue;
3171df47e437SAndre Oppermann 			to->to_flags |= TOF_SIGNATURE;
3172df47e437SAndre Oppermann 			to->to_signature = cp + 2;
31731cfd4b53SBruce M Simpson 			break;
3174265ed012SBruce M Simpson #endif
31756d90faf3SPaul Saab 		case TCPOPT_SACK_PERMITTED:
3176f72167f4SAndre Oppermann 			if (optlen != TCPOLEN_SACK_PERMITTED)
31776d90faf3SPaul Saab 				continue;
3178f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3179f72167f4SAndre Oppermann 				continue;
3180603724d3SBjoern A. Zeeb 			if (!V_tcp_do_sack)
3181f72167f4SAndre Oppermann 				continue;
3182fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACKPERM;
31836d90faf3SPaul Saab 			break;
31846d90faf3SPaul Saab 		case TCPOPT_SACK:
31855a53ca16SPaul Saab 			if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
31866d90faf3SPaul Saab 				continue;
3187b728e902SAndre Oppermann 			if (flags & TO_SYN)
3188b728e902SAndre Oppermann 				continue;
3189fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACK;
31905a53ca16SPaul Saab 			to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
31915a53ca16SPaul Saab 			to->to_sacks = cp + 2;
319278b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_rcv_blocks);
31936d90faf3SPaul Saab 			break;
3194be2ac88cSJonathan Lemon 		default:
3195be2ac88cSJonathan Lemon 			continue;
3196df8bae1dSRodney W. Grimes 		}
3197df8bae1dSRodney W. Grimes 	}
3198df8bae1dSRodney W. Grimes }
3199df8bae1dSRodney W. Grimes 
3200df8bae1dSRodney W. Grimes /*
3201df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
3202df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
3203df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
3204df8bae1dSRodney W. Grimes  * sequencing purposes.
3205df8bae1dSRodney W. Grimes  */
32060312fbe9SPoul-Henning Kamp static void
3207ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m,
3208ad3f9ab3SAndre Oppermann     int off)
3209df8bae1dSRodney W. Grimes {
3210fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
3211df8bae1dSRodney W. Grimes 
3212df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
3213df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
3214df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
3215df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
3216df8bae1dSRodney W. Grimes 
32178501a69cSRobert Watson 			INP_WLOCK_ASSERT(tp->t_inpcb);
32187a3244ccSRobert Watson 
3219df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
3220df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
3221df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
3222df8bae1dSRodney W. Grimes 			m->m_len--;
322369a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
322469a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
3225df8bae1dSRodney W. Grimes 			return;
3226df8bae1dSRodney W. Grimes 		}
3227df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
3228df8bae1dSRodney W. Grimes 		m = m->m_next;
32294dfdffe9SAndre Oppermann 		if (m == NULL)
3230df8bae1dSRodney W. Grimes 			break;
3231df8bae1dSRodney W. Grimes 	}
3232df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
3233df8bae1dSRodney W. Grimes }
3234df8bae1dSRodney W. Grimes 
3235df8bae1dSRodney W. Grimes /*
3236df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
3237df8bae1dSRodney W. Grimes  * and update averages and current timeout.
3238df8bae1dSRodney W. Grimes  */
32390312fbe9SPoul-Henning Kamp static void
3240ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt)
3241df8bae1dSRodney W. Grimes {
3242ad3f9ab3SAndre Oppermann 	int delta;
3243233e8c18SGarrett Wollman 
32448501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
32452be3bf22SRobert Watson 
324678b50714SRobert Watson 	TCPSTAT_INC(tcps_rttupdated);
3247233e8c18SGarrett Wollman 	tp->t_rttupdated++;
3248233e8c18SGarrett Wollman 	if (tp->t_srtt != 0) {
3249233e8c18SGarrett Wollman 		/*
3250233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
3251233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
3252233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
3253233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
3254233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
3255233e8c18SGarrett Wollman 		 */
3256233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
3257233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
3258233e8c18SGarrett Wollman 
3259233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
3260233e8c18SGarrett Wollman 			tp->t_srtt = 1;
3261233e8c18SGarrett Wollman 
3262233e8c18SGarrett Wollman 		/*
3263233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
3264233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
3265233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
3266233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
3267233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
3268233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
3269233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
3270233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
3271233e8c18SGarrett Wollman 		 */
3272233e8c18SGarrett Wollman 		if (delta < 0)
3273233e8c18SGarrett Wollman 			delta = -delta;
3274233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
3275233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
3276233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
32771fcc99b5SMatthew Dillon 		if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
32781fcc99b5SMatthew Dillon 		    tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3279233e8c18SGarrett Wollman 	} else {
3280233e8c18SGarrett Wollman 		/*
3281233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
3282233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
3283233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
3284233e8c18SGarrett Wollman 		 */
3285233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
3286233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
32871fcc99b5SMatthew Dillon 		tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3288233e8c18SGarrett Wollman 	}
32899b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
3290df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
3291df8bae1dSRodney W. Grimes 
3292df8bae1dSRodney W. Grimes 	/*
3293df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
3294df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
3295df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
3296df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
3297df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
3298df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
3299df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
3300df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
3301df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
3302df8bae1dSRodney W. Grimes 	 */
3303233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
33049e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
3305df8bae1dSRodney W. Grimes 
3306df8bae1dSRodney W. Grimes 	/*
3307df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
3308df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
3309df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
3310df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
3311df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
3312df8bae1dSRodney W. Grimes 	 */
3313df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
3314df8bae1dSRodney W. Grimes }
3315df8bae1dSRodney W. Grimes 
3316df8bae1dSRodney W. Grimes /*
3317df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
3318df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
3319df8bae1dSRodney W. Grimes  * If none, use an mss that can be handled on the outgoing
3320df8bae1dSRodney W. Grimes  * interface without forcing IP to fragment; if bigger than
3321df8bae1dSRodney W. Grimes  * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
3322df8bae1dSRodney W. Grimes  * to utilize large mbufs.  If no route is found, route has no mtu,
3323df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
3324df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
3325df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
3326df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
3327df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
3328df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
3329df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
3330a0292f23SGarrett Wollman  *
3331a0292f23SGarrett Wollman  * Also take into account the space needed for options that we
3332a0292f23SGarrett Wollman  * send regularly.  Make maxseg shorter by that amount to assure
3333a0292f23SGarrett Wollman  * that we can send maxseg amount of data even when the options
3334a0292f23SGarrett Wollman  * are present.  Store the upper limit of the length of options plus
3335a0292f23SGarrett Wollman  * data in maxopd.
3336a0292f23SGarrett Wollman  *
333797d8d152SAndre Oppermann  * NOTE that this routine is only called when we process an incoming
3338ef341ee1SGleb Smirnoff  * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS
3339ef341ee1SGleb Smirnoff  * settings are handled in tcp_mssopt().
3340df8bae1dSRodney W. Grimes  */
3341a0292f23SGarrett Wollman void
3342ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer,
334391d6cfa6SBjoern A. Zeeb     struct hc_metrics_lite *metricptr, int *mtuflags)
3344df8bae1dSRodney W. Grimes {
3345b287c6c7SBjoern A. Zeeb 	int mss = 0;
3346b287c6c7SBjoern A. Zeeb 	u_long maxmtu = 0;
3347c068736aSJeffrey Hsu 	struct inpcb *inp = tp->t_inpcb;
334897d8d152SAndre Oppermann 	struct hc_metrics_lite metrics;
3349ef341ee1SGleb Smirnoff 	int origoffer;
3350fb59c426SYoshinobu Inoue #ifdef INET6
3351c068736aSJeffrey Hsu 	int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
3352c068736aSJeffrey Hsu 	size_t min_protoh = isipv6 ?
3353c068736aSJeffrey Hsu 			    sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
3354c068736aSJeffrey Hsu 			    sizeof (struct tcpiphdr);
3355c068736aSJeffrey Hsu #else
3356c068736aSJeffrey Hsu 	const size_t min_protoh = sizeof(struct tcpiphdr);
3357fb59c426SYoshinobu Inoue #endif
3358df8bae1dSRodney W. Grimes 
33598501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
33607a3244ccSRobert Watson 
3361ef341ee1SGleb Smirnoff 	if (mtuoffer != -1) {
3362ef341ee1SGleb Smirnoff 		KASSERT(offer == -1, ("%s: conflict", __func__));
3363ef341ee1SGleb Smirnoff 		offer = mtuoffer - min_protoh;
3364ef341ee1SGleb Smirnoff 	}
3365ef341ee1SGleb Smirnoff 	origoffer = offer;
3366ef341ee1SGleb Smirnoff 
3367e8949f74SAndre Oppermann 	/* Initialize. */
336897d8d152SAndre Oppermann #ifdef INET6
336997d8d152SAndre Oppermann 	if (isipv6) {
337091d6cfa6SBjoern A. Zeeb 		maxmtu = tcp_maxmtu6(&inp->inp_inc, mtuflags);
3371603724d3SBjoern A. Zeeb 		tp->t_maxopd = tp->t_maxseg = V_tcp_v6mssdflt;
3372b287c6c7SBjoern A. Zeeb 	}
337397d8d152SAndre Oppermann #endif
3374b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3375b287c6c7SBjoern A. Zeeb 	else
3376b287c6c7SBjoern A. Zeeb #endif
3377b287c6c7SBjoern A. Zeeb #ifdef INET
337897d8d152SAndre Oppermann 	{
337991d6cfa6SBjoern A. Zeeb 		maxmtu = tcp_maxmtu(&inp->inp_inc, mtuflags);
3380603724d3SBjoern A. Zeeb 		tp->t_maxopd = tp->t_maxseg = V_tcp_mssdflt;
3381df8bae1dSRodney W. Grimes 	}
3382b287c6c7SBjoern A. Zeeb #endif
3383df8bae1dSRodney W. Grimes 
338497d8d152SAndre Oppermann 	/*
3385e8949f74SAndre Oppermann 	 * No route to sender, stay with default mss and return.
338697d8d152SAndre Oppermann 	 */
33876f01cac6SBjoern A. Zeeb 	if (maxmtu == 0) {
33886f01cac6SBjoern A. Zeeb 		/*
33898e5c87f4SBjoern A. Zeeb 		 * In case we return early we need to initialize metrics
33906f01cac6SBjoern A. Zeeb 		 * to a defined state as tcp_hc_get() would do for us
33916f01cac6SBjoern A. Zeeb 		 * if there was no cache hit.
33926f01cac6SBjoern A. Zeeb 		 */
33936f01cac6SBjoern A. Zeeb 		if (metricptr != NULL)
33946f01cac6SBjoern A. Zeeb 			bzero(metricptr, sizeof(struct hc_metrics_lite));
339597d8d152SAndre Oppermann 		return;
33966f01cac6SBjoern A. Zeeb 	}
339797d8d152SAndre Oppermann 
3398e8949f74SAndre Oppermann 	/* What have we got? */
339997d8d152SAndre Oppermann 	switch (offer) {
340097d8d152SAndre Oppermann 		case 0:
340197d8d152SAndre Oppermann 			/*
340297d8d152SAndre Oppermann 			 * Offer == 0 means that there was no MSS on the SYN
3403c3b02504SBjoern A. Zeeb 			 * segment, in this case we use tcp_mssdflt as
3404c3b02504SBjoern A. Zeeb 			 * already assigned to t_maxopd above.
340597d8d152SAndre Oppermann 			 */
3406c3b02504SBjoern A. Zeeb 			offer = tp->t_maxopd;
340797d8d152SAndre Oppermann 			break;
340897d8d152SAndre Oppermann 
340997d8d152SAndre Oppermann 		case -1:
3410a0292f23SGarrett Wollman 			/*
3411c94c54e4SAndre Oppermann 			 * Offer == -1 means that we didn't receive SYN yet.
3412a0292f23SGarrett Wollman 			 */
341397d8d152SAndre Oppermann 			/* FALLTHROUGH */
341497d8d152SAndre Oppermann 
341597d8d152SAndre Oppermann 		default:
3416a0292f23SGarrett Wollman 			/*
341753369ac9SAndre Oppermann 			 * Prevent DoS attack with too small MSS. Round up
341853369ac9SAndre Oppermann 			 * to at least minmss.
341953369ac9SAndre Oppermann 			 */
3420603724d3SBjoern A. Zeeb 			offer = max(offer, V_tcp_minmss);
342197d8d152SAndre Oppermann 	}
3422a0292f23SGarrett Wollman 
3423df8bae1dSRodney W. Grimes 	/*
3424e8949f74SAndre Oppermann 	 * rmx information is now retrieved from tcp_hostcache.
3425df8bae1dSRodney W. Grimes 	 */
342697d8d152SAndre Oppermann 	tcp_hc_get(&inp->inp_inc, &metrics);
34273cee92e0SBjoern A. Zeeb 	if (metricptr != NULL)
34283cee92e0SBjoern A. Zeeb 		bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite));
342997d8d152SAndre Oppermann 
3430df8bae1dSRodney W. Grimes 	/*
3431e8949f74SAndre Oppermann 	 * If there's a discovered mtu int tcp hostcache, use it
3432fb59c426SYoshinobu Inoue 	 * else, use the link mtu.
3433df8bae1dSRodney W. Grimes 	 */
343497d8d152SAndre Oppermann 	if (metrics.rmx_mtu)
34352d166c02SAndre Oppermann 		mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
3436c068736aSJeffrey Hsu 	else {
343731b3783cSHajimu UMEMOTO #ifdef INET6
3438fb59c426SYoshinobu Inoue 		if (isipv6) {
343997d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3440603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
344197d8d152SAndre Oppermann 			    !in6_localaddr(&inp->in6p_faddr))
3442603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_v6mssdflt);
3443b287c6c7SBjoern A. Zeeb 		}
3444b3399803SHajimu UMEMOTO #endif
3445b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3446b287c6c7SBjoern A. Zeeb 		else
3447b287c6c7SBjoern A. Zeeb #endif
3448b287c6c7SBjoern A. Zeeb #ifdef INET
344997d8d152SAndre Oppermann 		{
345097d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3451603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
345297d8d152SAndre Oppermann 			    !in_localaddr(inp->inp_faddr))
3453603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_mssdflt);
3454a0292f23SGarrett Wollman 		}
3455b287c6c7SBjoern A. Zeeb #endif
34563cee92e0SBjoern A. Zeeb 		/*
34573cee92e0SBjoern A. Zeeb 		 * XXX - The above conditional (mss = maxmtu - min_protoh)
34583cee92e0SBjoern A. Zeeb 		 * probably violates the TCP spec.
34593cee92e0SBjoern A. Zeeb 		 * The problem is that, since we don't know the
34603cee92e0SBjoern A. Zeeb 		 * other end's MSS, we are supposed to use a conservative
34613cee92e0SBjoern A. Zeeb 		 * default.  But, if we do that, then MTU discovery will
34623cee92e0SBjoern A. Zeeb 		 * never actually take place, because the conservative
34633cee92e0SBjoern A. Zeeb 		 * default is much less than the MTUs typically seen
34643cee92e0SBjoern A. Zeeb 		 * on the Internet today.  For the moment, we'll sweep
34653cee92e0SBjoern A. Zeeb 		 * this under the carpet.
34663cee92e0SBjoern A. Zeeb 		 *
34673cee92e0SBjoern A. Zeeb 		 * The conservative default might not actually be a problem
34683cee92e0SBjoern A. Zeeb 		 * if the only case this occurs is when sending an initial
34693cee92e0SBjoern A. Zeeb 		 * SYN with options and data to a host we've never talked
34703cee92e0SBjoern A. Zeeb 		 * to before.  Then, they will reply with an MSS value which
34713cee92e0SBjoern A. Zeeb 		 * will get recorded and the new parameters should get
34723cee92e0SBjoern A. Zeeb 		 * recomputed.  For Further Study.
34733cee92e0SBjoern A. Zeeb 		 */
347497d8d152SAndre Oppermann 	}
3475a0292f23SGarrett Wollman 	mss = min(mss, offer);
347697d8d152SAndre Oppermann 
3477a0292f23SGarrett Wollman 	/*
34783cee92e0SBjoern A. Zeeb 	 * Sanity check: make sure that maxopd will be large
34793cee92e0SBjoern A. Zeeb 	 * enough to allow some data on segments even if the
34803cee92e0SBjoern A. Zeeb 	 * all the option space is used (40bytes).  Otherwise
34813cee92e0SBjoern A. Zeeb 	 * funny things may happen in tcp_output.
34823cee92e0SBjoern A. Zeeb 	 */
34833cee92e0SBjoern A. Zeeb 	mss = max(mss, 64);
34843cee92e0SBjoern A. Zeeb 
34853cee92e0SBjoern A. Zeeb 	/*
3486a0292f23SGarrett Wollman 	 * maxopd stores the maximum length of data AND options
3487a0292f23SGarrett Wollman 	 * in a segment; maxseg is the amount of data in a normal
3488a0292f23SGarrett Wollman 	 * segment.  We need to store this value (maxopd) apart
3489a0292f23SGarrett Wollman 	 * from maxseg, because now every segment carries options
3490a0292f23SGarrett Wollman 	 * and thus we normally have somewhat less data in segments.
3491a0292f23SGarrett Wollman 	 */
3492a0292f23SGarrett Wollman 	tp->t_maxopd = mss;
3493a0292f23SGarrett Wollman 
3494a0292f23SGarrett Wollman 	/*
3495e8949f74SAndre Oppermann 	 * origoffer==-1 indicates that no segments were received yet.
3496c94c54e4SAndre Oppermann 	 * In this case we just guess.
3497a0292f23SGarrett Wollman 	 */
3498a0292f23SGarrett Wollman 	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
3499a0292f23SGarrett Wollman 	    (origoffer == -1 ||
3500a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
3501a0292f23SGarrett Wollman 		mss -= TCPOLEN_TSTAMP_APPA;
3502a0292f23SGarrett Wollman 
3503df8bae1dSRodney W. Grimes #if	(MCLBYTES & (MCLBYTES - 1)) == 0
3504df8bae1dSRodney W. Grimes 	if (mss > MCLBYTES)
3505df8bae1dSRodney W. Grimes 		mss &= ~(MCLBYTES-1);
3506df8bae1dSRodney W. Grimes #else
3507df8bae1dSRodney W. Grimes 	if (mss > MCLBYTES)
3508df8bae1dSRodney W. Grimes 		mss = mss / MCLBYTES * MCLBYTES;
3509df8bae1dSRodney W. Grimes #endif
351097d8d152SAndre Oppermann 	tp->t_maxseg = mss;
35113cee92e0SBjoern A. Zeeb }
35123cee92e0SBjoern A. Zeeb 
35133cee92e0SBjoern A. Zeeb void
35143cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer)
35153cee92e0SBjoern A. Zeeb {
3516dbc42409SLawrence Stewart 	int mss;
35173cee92e0SBjoern A. Zeeb 	u_long bufsize;
35183cee92e0SBjoern A. Zeeb 	struct inpcb *inp;
35193cee92e0SBjoern A. Zeeb 	struct socket *so;
35203cee92e0SBjoern A. Zeeb 	struct hc_metrics_lite metrics;
352191d6cfa6SBjoern A. Zeeb 	int mtuflags = 0;
3522dbc42409SLawrence Stewart 
35233cee92e0SBjoern A. Zeeb 	KASSERT(tp != NULL, ("%s: tp == NULL", __func__));
35243cee92e0SBjoern A. Zeeb 
3525ef341ee1SGleb Smirnoff 	tcp_mss_update(tp, offer, -1, &metrics, &mtuflags);
35263cee92e0SBjoern A. Zeeb 
35273cee92e0SBjoern A. Zeeb 	mss = tp->t_maxseg;
35283cee92e0SBjoern A. Zeeb 	inp = tp->t_inpcb;
352997d8d152SAndre Oppermann 
3530df8bae1dSRodney W. Grimes 	/*
353197d8d152SAndre Oppermann 	 * If there's a pipesize, change the socket buffer to that size,
353297d8d152SAndre Oppermann 	 * don't change if sb_hiwat is different than default (then it
353397d8d152SAndre Oppermann 	 * has been changed on purpose with setsockopt).
353497d8d152SAndre Oppermann 	 * Make the socket buffers an integral number of mss units;
353597d8d152SAndre Oppermann 	 * if the mss is larger than the socket buffer, decrease the mss.
3536df8bae1dSRodney W. Grimes 	 */
3537c3b02504SBjoern A. Zeeb 	so = inp->inp_socket;
35383f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
3539e233e2acSAndre Oppermann 	if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe)
354097d8d152SAndre Oppermann 		bufsize = metrics.rmx_sendpipe;
354197d8d152SAndre Oppermann 	else
3542df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
3543df8bae1dSRodney W. Grimes 	if (bufsize < mss)
3544df8bae1dSRodney W. Grimes 		mss = bufsize;
3545df8bae1dSRodney W. Grimes 	else {
3546df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3547df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3548df8bae1dSRodney W. Grimes 			bufsize = sb_max;
354988c39af3SRuslan Ermilov 		if (bufsize > so->so_snd.sb_hiwat)
35503f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_snd, bufsize, so, NULL);
3551df8bae1dSRodney W. Grimes 	}
35523f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
3553df8bae1dSRodney W. Grimes 	tp->t_maxseg = mss;
3554df8bae1dSRodney W. Grimes 
35553f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
3556e233e2acSAndre Oppermann 	if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe)
355797d8d152SAndre Oppermann 		bufsize = metrics.rmx_recvpipe;
355897d8d152SAndre Oppermann 	else
3559df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
3560df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
3561df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3562df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3563df8bae1dSRodney W. Grimes 			bufsize = sb_max;
356488c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
35653f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL);
3566df8bae1dSRodney W. Grimes 	}
35673f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
356891d6cfa6SBjoern A. Zeeb 
356991d6cfa6SBjoern A. Zeeb 	/* Check the interface for TSO capabilities. */
357091d6cfa6SBjoern A. Zeeb 	if (mtuflags & CSUM_TSO)
357191d6cfa6SBjoern A. Zeeb 		tp->t_flags |= TF_TSO;
3572a0292f23SGarrett Wollman }
3573a0292f23SGarrett Wollman 
3574a0292f23SGarrett Wollman /*
3575a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
3576a0292f23SGarrett Wollman  */
3577a0292f23SGarrett Wollman int
3578ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc)
3579a0292f23SGarrett Wollman {
358097d8d152SAndre Oppermann 	int mss = 0;
358197d8d152SAndre Oppermann 	u_long maxmtu = 0;
358297d8d152SAndre Oppermann 	u_long thcmtu = 0;
358397d8d152SAndre Oppermann 	size_t min_protoh;
3584a0292f23SGarrett Wollman 
358597d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3586a0292f23SGarrett Wollman 
358731b3783cSHajimu UMEMOTO #ifdef INET6
3588dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
3589603724d3SBjoern A. Zeeb 		mss = V_tcp_v6mssdflt;
3590233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(inc, NULL);
359197d8d152SAndre Oppermann 		min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
3592b287c6c7SBjoern A. Zeeb 	}
359331b3783cSHajimu UMEMOTO #endif
3594b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3595b287c6c7SBjoern A. Zeeb 	else
3596b287c6c7SBjoern A. Zeeb #endif
3597b287c6c7SBjoern A. Zeeb #ifdef INET
359897d8d152SAndre Oppermann 	{
3599603724d3SBjoern A. Zeeb 		mss = V_tcp_mssdflt;
3600233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(inc, NULL);
360197d8d152SAndre Oppermann 		min_protoh = sizeof(struct tcpiphdr);
360297d8d152SAndre Oppermann 	}
3603b287c6c7SBjoern A. Zeeb #endif
36043a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET)
36053a9391deSBjoern A. Zeeb 	thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
36063a9391deSBjoern A. Zeeb #endif
36073a9391deSBjoern A. Zeeb 
360897d8d152SAndre Oppermann 	if (maxmtu && thcmtu)
360997d8d152SAndre Oppermann 		mss = min(maxmtu, thcmtu) - min_protoh;
361097d8d152SAndre Oppermann 	else if (maxmtu || thcmtu)
361197d8d152SAndre Oppermann 		mss = max(maxmtu, thcmtu) - min_protoh;
361297d8d152SAndre Oppermann 
361397d8d152SAndre Oppermann 	return (mss);
3614df8bae1dSRodney W. Grimes }
361546f58482SJonathan Lemon 
361646f58482SJonathan Lemon 
361746f58482SJonathan Lemon /*
3618c068736aSJeffrey Hsu  * On a partial ack arrives, force the retransmission of the
3619c068736aSJeffrey Hsu  * next unacknowledged segment.  Do not clear tp->t_dupacks.
3620c068736aSJeffrey Hsu  * By setting snd_nxt to ti_ack, this forces retransmission timer to
3621c068736aSJeffrey Hsu  * be started again.
362246f58482SJonathan Lemon  */
3623c068736aSJeffrey Hsu static void
3624ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
362546f58482SJonathan Lemon {
362646f58482SJonathan Lemon 	tcp_seq onxt = tp->snd_nxt;
362746f58482SJonathan Lemon 	u_long  ocwnd = tp->snd_cwnd;
362846f58482SJonathan Lemon 
36298501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
36307a3244ccSRobert Watson 
3631b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_REXMT, 0);
363246f58482SJonathan Lemon 	tp->t_rtttime = 0;
363346f58482SJonathan Lemon 	tp->snd_nxt = th->th_ack;
36346b2a5f92SJayanth Vijayaraghavan 	/*
3635c068736aSJeffrey Hsu 	 * Set snd_cwnd to one segment beyond acknowledged offset.
3636c068736aSJeffrey Hsu 	 * (tp->snd_una has not yet been updated when this function is called.)
36376b2a5f92SJayanth Vijayaraghavan 	 */
3638dbc42409SLawrence Stewart 	tp->snd_cwnd = tp->t_maxseg + BYTES_THIS_ACK(tp, th);
3639a84db8f4SMatthew Dillon 	tp->t_flags |= TF_ACKNOW;
36406b2a5f92SJayanth Vijayaraghavan 	(void) tcp_output(tp);
364146f58482SJonathan Lemon 	tp->snd_cwnd = ocwnd;
364246f58482SJonathan Lemon 	if (SEQ_GT(onxt, tp->snd_nxt))
364346f58482SJonathan Lemon 		tp->snd_nxt = onxt;
364446f58482SJonathan Lemon 	/*
364546f58482SJonathan Lemon 	 * Partial window deflation.  Relies on fact that tp->snd_una
364646f58482SJonathan Lemon 	 * not updated yet.
364746f58482SJonathan Lemon 	 */
3648dbc42409SLawrence Stewart 	if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th))
3649dbc42409SLawrence Stewart 		tp->snd_cwnd -= BYTES_THIS_ACK(tp, th);
3650d7587117SPaul Saab 	else
3651d7587117SPaul Saab 		tp->snd_cwnd = 0;
3652d7587117SPaul Saab 	tp->snd_cwnd += tp->t_maxseg;
365346f58482SJonathan Lemon }
3654