xref: /freebsd/sys/netinet/tcp_input.c (revision cf8f04f4c0328ef2386b561d705b6a0f6e3129f5)
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>
78c1de64a4SAndrey V. Elsukov #include <net/pfil.h>
79df8bae1dSRodney W. Grimes #include <net/route.h>
80530c0060SRobert Watson #include <net/vnet.h>
81df8bae1dSRodney W. Grimes 
82773673c1SAndre Oppermann #define TCPSTATES		/* for logging */
83773673c1SAndre Oppermann 
84dbc42409SLawrence Stewart #include <netinet/cc.h>
85df8bae1dSRodney W. Grimes #include <netinet/in.h>
86960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
87df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
88960ed29cSSeigo Tanimura #include <netinet/in_var.h>
89df8bae1dSRodney W. Grimes #include <netinet/ip.h>
908e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h>	/* required for icmp_var.h */
91686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM */
92df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
93ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
94686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
95686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
96686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
97960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h>
98960ed29cSSeigo Tanimura #include <netinet6/nd6.h>
99df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
100df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
101df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
102df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
103fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h>
104df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
105c325962bSMike Silbersack #include <netinet/tcp_syncache.h>
106610ee2f9SDavid Greenman #ifdef TCPDEBUG
107df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
108fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */
10909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
11009fe6320SNavdeep Parhar #include <netinet/tcp_offload.h>
11109fe6320SNavdeep Parhar #endif
112fb59c426SYoshinobu Inoue 
113b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
114b9234fafSSam Leffler #include <netipsec/ipsec.h>
115b9234fafSSam Leffler #include <netipsec/ipsec6.h>
116b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
117b9234fafSSam Leffler 
118db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
119db4f9cc7SJonathan Lemon 
120aed55708SRobert Watson #include <security/mac/mac_framework.h>
121aed55708SRobert Watson 
122dbc42409SLawrence Stewart const int tcprexmtthresh = 3;
1230312fbe9SPoul-Henning Kamp 
124eddfbb76SRobert Watson VNET_DEFINE(struct tcpstat, tcpstat);
125eddfbb76SRobert Watson SYSCTL_VNET_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW,
126eddfbb76SRobert Watson     &VNET_NAME(tcpstat), tcpstat,
1278b615593SMarko Zec     "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
1280312fbe9SPoul-Henning Kamp 
129773673c1SAndre Oppermann int tcp_log_in_vain = 0;
130816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
131eddfbb76SRobert Watson     &tcp_log_in_vain, 0,
132eddfbb76SRobert Watson     "Log all incoming TCP segments to closed ports");
133816a3d83SPoul-Henning Kamp 
13482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, blackhole) = 0;
13582cea7e6SBjoern A. Zeeb #define	V_blackhole		VNET(blackhole)
136eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW,
137eddfbb76SRobert Watson     &VNET_NAME(blackhole), 0,
138eddfbb76SRobert Watson     "Do not send RST on segments to closed ports");
13916f7f31fSGeoff Rehmet 
14082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_delack_enabled) = 1;
141eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW,
142eddfbb76SRobert Watson     &VNET_NAME(tcp_delack_enabled), 0,
1433d177f46SBill Fumerola     "Delay ACK to try and piggyback it onto a data packet");
144f498eeeeSDavid Greenman 
14582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, drop_synfin) = 0;
14682cea7e6SBjoern A. Zeeb #define	V_drop_synfin		VNET(drop_synfin)
147eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW,
148eddfbb76SRobert Watson     &VNET_NAME(drop_synfin), 0,
149eddfbb76SRobert Watson     "Drop TCP packets with SYN+FIN set");
150f8613305SDag-Erling Smørgrav 
15182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1;
15282cea7e6SBjoern A. Zeeb #define	V_tcp_do_rfc3042	VNET(tcp_do_rfc3042)
153eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW,
154eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3042), 0,
155eddfbb76SRobert Watson     "Enable RFC 3042 (Limited Transmit)");
156582a954bSJeffrey Hsu 
15782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1;
158eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW,
159eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3390), 0,
160da3a8a1aSJeffrey Hsu     "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
161da3a8a1aSJeffrey Hsu 
16282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1;
163eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_RW,
164eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3465), 0,
16524cb0f22SLawrence Stewart     "Enable RFC 3465 (Appropriate Byte Counting)");
166eddfbb76SRobert Watson 
16782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2;
168eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_RW,
169eddfbb76SRobert Watson     &VNET_NAME(tcp_abc_l_var), 2,
17024cb0f22SLawrence Stewart     "Cap the max cwnd increment during slow-start to this number of segments");
17124cb0f22SLawrence Stewart 
1726472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN");
173f2512ba1SRui Paulo 
17482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_ecn) = 0;
175eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_RW,
176eddfbb76SRobert Watson     &VNET_NAME(tcp_do_ecn), 0,
177eddfbb76SRobert Watson     "TCP ECN support");
178eddfbb76SRobert Watson 
17982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_ecn_maxretries) = 1;
180eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_RW,
181eddfbb76SRobert Watson     &VNET_NAME(tcp_ecn_maxretries), 0,
182eddfbb76SRobert Watson     "Max retries before giving up on ECN");
183eddfbb76SRobert Watson 
18482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0;
18582cea7e6SBjoern A. Zeeb #define	V_tcp_insecure_rst	VNET(tcp_insecure_rst)
186eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_RW,
187eddfbb76SRobert Watson     &VNET_NAME(tcp_insecure_rst), 0,
1886489fe65SAndre Oppermann     "Follow the old (insecure) criteria for accepting RST packets");
189a69968eeSMike Silbersack 
190fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64;
191873789cbSAndre Oppermann #define	V_tcp_recvspace	VNET(tcp_recvspace)
192ddd0c4a9SSergey Kandaurov SYSCTL_VNET_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
193873789cbSAndre Oppermann     &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size");
194873789cbSAndre Oppermann 
19582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1;
19682cea7e6SBjoern A. Zeeb #define	V_tcp_do_autorcvbuf	VNET(tcp_do_autorcvbuf)
197eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_RW,
198eddfbb76SRobert Watson     &VNET_NAME(tcp_do_autorcvbuf), 0,
1998b615593SMarko Zec     "Enable automatic receive buffer sizing");
2006741ecf5SAndre Oppermann 
20182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_inc) = 16*1024;
20282cea7e6SBjoern A. Zeeb #define	V_tcp_autorcvbuf_inc	VNET(tcp_autorcvbuf_inc)
203eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_RW,
204eddfbb76SRobert Watson     &VNET_NAME(tcp_autorcvbuf_inc), 0,
2056489fe65SAndre Oppermann     "Incrementor step size of automatic receive buffer");
2066741ecf5SAndre Oppermann 
207b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024;
20882cea7e6SBjoern A. Zeeb #define	V_tcp_autorcvbuf_max	VNET(tcp_autorcvbuf_max)
209eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_RW,
210eddfbb76SRobert Watson     &VNET_NAME(tcp_autorcvbuf_max), 0,
2118b615593SMarko Zec     "Max size of automatic receive buffer");
2126741ecf5SAndre Oppermann 
213eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, tcb);
21444e33a07SMarko Zec #define	tcb6	tcb  /* for KAME src sync over BSD*'s */
21582cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo);
216df8bae1dSRodney W. Grimes 
2175a53ca16SPaul Saab static void	 tcp_dooptions(struct tcpopt *, u_char *, int, int);
218679d9708SAndre Oppermann static void	 tcp_do_segment(struct mbuf *, struct tcphdr *,
219252ca428SRobert Watson 		     struct socket *, struct tcpcb *, int, int, uint8_t,
220252ca428SRobert Watson 		     int);
221302ce8d6SAndre Oppermann static void	 tcp_dropwithreset(struct mbuf *, struct tcphdr *,
222302ce8d6SAndre Oppermann 		     struct tcpcb *, int, int);
2234d77a549SAlfred Perlstein static void	 tcp_pulloutofband(struct socket *,
2244d77a549SAlfred Perlstein 		     struct tcphdr *, struct mbuf *, int);
2254d77a549SAlfred Perlstein static void	 tcp_xmit_timer(struct tcpcb *, int);
226c068736aSJeffrey Hsu static void	 tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *);
2272903309aSAttilio Rao static void inline 	tcp_fields_to_host(struct tcphdr *);
2282903309aSAttilio Rao #ifdef TCP_SIGNATURE
2292903309aSAttilio Rao static void inline 	tcp_fields_to_net(struct tcphdr *);
2302903309aSAttilio Rao static int inline	tcp_signature_verify_input(struct mbuf *, int, int,
2312903309aSAttilio Rao 			    int, struct tcpopt *, struct tcphdr *, u_int);
2322903309aSAttilio Rao #endif
233dbc42409SLawrence Stewart static void inline	cc_ack_received(struct tcpcb *tp, struct tcphdr *th,
234dbc42409SLawrence Stewart 			    uint16_t type);
235dbc42409SLawrence Stewart static void inline	cc_conn_init(struct tcpcb *tp);
236dbc42409SLawrence Stewart static void inline	cc_post_recovery(struct tcpcb *tp, struct tcphdr *th);
23739bc9de5SLawrence Stewart static void inline	hhook_run_tcp_est_in(struct tcpcb *tp,
23839bc9de5SLawrence Stewart 			    struct tcphdr *th, struct tcpopt *to);
239f2512ba1SRui Paulo 
240315e3e38SRobert Watson /*
241315e3e38SRobert Watson  * Kernel module interface for updating tcpstat.  The argument is an index
242315e3e38SRobert Watson  * into tcpstat treated as an array of u_long.  While this encodes the
243315e3e38SRobert Watson  * general layout of tcpstat into the caller, it doesn't encode its location,
244315e3e38SRobert Watson  * so that future changes to add, for example, per-CPU stats support won't
245315e3e38SRobert Watson  * cause binary compatibility problems for kernel modules.
246315e3e38SRobert Watson  */
247315e3e38SRobert Watson void
248315e3e38SRobert Watson kmod_tcpstat_inc(int statnum)
249315e3e38SRobert Watson {
250315e3e38SRobert Watson 
251315e3e38SRobert Watson 	(*((u_long *)&V_tcpstat + statnum))++;
252315e3e38SRobert Watson }
253315e3e38SRobert Watson 
254dbc42409SLawrence Stewart /*
25539bc9de5SLawrence Stewart  * Wrapper for the TCP established input helper hook.
25639bc9de5SLawrence Stewart  */
25739bc9de5SLawrence Stewart static void inline
25839bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to)
25939bc9de5SLawrence Stewart {
26039bc9de5SLawrence Stewart 	struct tcp_hhook_data hhook_data;
26139bc9de5SLawrence Stewart 
26239bc9de5SLawrence Stewart 	if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) {
26339bc9de5SLawrence Stewart 		hhook_data.tp = tp;
26439bc9de5SLawrence Stewart 		hhook_data.th = th;
26539bc9de5SLawrence Stewart 		hhook_data.to = to;
26639bc9de5SLawrence Stewart 
26739bc9de5SLawrence Stewart 		hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data,
26839bc9de5SLawrence Stewart 		    tp->osd);
26939bc9de5SLawrence Stewart 	}
27039bc9de5SLawrence Stewart }
27139bc9de5SLawrence Stewart 
27239bc9de5SLawrence Stewart /*
273dbc42409SLawrence Stewart  * CC wrapper hook functions
274dbc42409SLawrence Stewart  */
275f2512ba1SRui Paulo static void inline
276dbc42409SLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t type)
277f2512ba1SRui Paulo {
278dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
279f2512ba1SRui Paulo 
280dbc42409SLawrence Stewart 	tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th);
281dbc42409SLawrence Stewart 	if (tp->snd_cwnd == min(tp->snd_cwnd, tp->snd_wnd))
282dbc42409SLawrence Stewart 		tp->ccv->flags |= CCF_CWND_LIMITED;
283dbc42409SLawrence Stewart 	else
284dbc42409SLawrence Stewart 		tp->ccv->flags &= ~CCF_CWND_LIMITED;
285dbc42409SLawrence Stewart 
286dbc42409SLawrence Stewart 	if (type == CC_ACK) {
287dbc42409SLawrence Stewart 		if (tp->snd_cwnd > tp->snd_ssthresh) {
288dbc42409SLawrence Stewart 			tp->t_bytes_acked += min(tp->ccv->bytes_this_ack,
289dbc42409SLawrence Stewart 			     V_tcp_abc_l_var * tp->t_maxseg);
290dbc42409SLawrence Stewart 			if (tp->t_bytes_acked >= tp->snd_cwnd) {
291dbc42409SLawrence Stewart 				tp->t_bytes_acked -= tp->snd_cwnd;
292dbc42409SLawrence Stewart 				tp->ccv->flags |= CCF_ABC_SENTAWND;
293dbc42409SLawrence Stewart 			}
294dbc42409SLawrence Stewart 		} else {
295dbc42409SLawrence Stewart 				tp->ccv->flags &= ~CCF_ABC_SENTAWND;
296dbc42409SLawrence Stewart 				tp->t_bytes_acked = 0;
297dbc42409SLawrence Stewart 		}
298dbc42409SLawrence Stewart 	}
299dbc42409SLawrence Stewart 
300dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->ack_received != NULL) {
301dbc42409SLawrence Stewart 		/* XXXLAS: Find a way to live without this */
302dbc42409SLawrence Stewart 		tp->ccv->curack = th->th_ack;
303dbc42409SLawrence Stewart 		CC_ALGO(tp)->ack_received(tp->ccv, type);
304dbc42409SLawrence Stewart 	}
305dbc42409SLawrence Stewart }
306dbc42409SLawrence Stewart 
307dbc42409SLawrence Stewart static void inline
308dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp)
309dbc42409SLawrence Stewart {
310dbc42409SLawrence Stewart 	struct hc_metrics_lite metrics;
311dbc42409SLawrence Stewart 	struct inpcb *inp = tp->t_inpcb;
312dbc42409SLawrence Stewart 	int rtt;
313dbc42409SLawrence Stewart 
314dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
315dbc42409SLawrence Stewart 
316dbc42409SLawrence Stewart 	tcp_hc_get(&inp->inp_inc, &metrics);
317dbc42409SLawrence Stewart 
318dbc42409SLawrence Stewart 	if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) {
319dbc42409SLawrence Stewart 		tp->t_srtt = rtt;
320dbc42409SLawrence Stewart 		tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
321dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedrtt);
322dbc42409SLawrence Stewart 		if (metrics.rmx_rttvar) {
323dbc42409SLawrence Stewart 			tp->t_rttvar = metrics.rmx_rttvar;
324dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_usedrttvar);
325dbc42409SLawrence Stewart 		} else {
326dbc42409SLawrence Stewart 			/* default variation is +- 1 rtt */
327dbc42409SLawrence Stewart 			tp->t_rttvar =
328dbc42409SLawrence Stewart 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
329dbc42409SLawrence Stewart 		}
330dbc42409SLawrence Stewart 		TCPT_RANGESET(tp->t_rxtcur,
331dbc42409SLawrence Stewart 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
332dbc42409SLawrence Stewart 		    tp->t_rttmin, TCPTV_REXMTMAX);
333dbc42409SLawrence Stewart 	}
334dbc42409SLawrence Stewart 	if (metrics.rmx_ssthresh) {
335dbc42409SLawrence Stewart 		/*
336dbc42409SLawrence Stewart 		 * There's some sort of gateway or interface
337dbc42409SLawrence Stewart 		 * buffer limit on the path.  Use this to set
338dbc42409SLawrence Stewart 		 * the slow start threshhold, but set the
339dbc42409SLawrence Stewart 		 * threshold to no less than 2*mss.
340dbc42409SLawrence Stewart 		 */
341dbc42409SLawrence Stewart 		tp->snd_ssthresh = max(2 * tp->t_maxseg, metrics.rmx_ssthresh);
342dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedssthresh);
343dbc42409SLawrence Stewart 	}
344dbc42409SLawrence Stewart 
345dbc42409SLawrence Stewart 	/*
3469ec4a4ccSAndre Oppermann 	 * Set the initial slow-start flight size.
347dbc42409SLawrence Stewart 	 *
348*cf8f04f4SAndre Oppermann 	 * RFC5681 Section 3.1 specifies the default conservative values.
349*cf8f04f4SAndre Oppermann 	 * RFC3390 specifies slightly more aggressive values.
350*cf8f04f4SAndre Oppermann 	 *
351*cf8f04f4SAndre Oppermann 	 * If a SYN or SYN/ACK was lost and retransmitted, we have to
352*cf8f04f4SAndre Oppermann 	 * reduce the initial CWND to one segment as congestion is likely
353*cf8f04f4SAndre Oppermann 	 * requiring us to be cautious.
354dbc42409SLawrence Stewart 	 */
355*cf8f04f4SAndre Oppermann 	if (tp->snd_cwnd == 1)
356*cf8f04f4SAndre Oppermann 		tp->snd_cwnd = tp->t_maxseg;		/* SYN(-ACK) lost */
357*cf8f04f4SAndre Oppermann 	else if (V_tcp_do_rfc3390)
358dbc42409SLawrence Stewart 		tp->snd_cwnd = min(4 * tp->t_maxseg,
359dbc42409SLawrence Stewart 		    max(2 * tp->t_maxseg, 4380));
36022efabd4SAndre Oppermann 	else {
36122efabd4SAndre Oppermann 		/* Per RFC5681 Section 3.1 */
36222efabd4SAndre Oppermann 		if (tp->t_maxseg > 2190)
36322efabd4SAndre Oppermann 			tp->snd_cwnd = 2 * tp->t_maxseg;
36422efabd4SAndre Oppermann 		else if (tp->t_maxseg > 1095)
36522efabd4SAndre Oppermann 			tp->snd_cwnd = 3 * tp->t_maxseg;
366dbc42409SLawrence Stewart 		else
36722efabd4SAndre Oppermann 			tp->snd_cwnd = 4 * tp->t_maxseg;
36822efabd4SAndre Oppermann 	}
369dbc42409SLawrence Stewart 
370dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->conn_init != NULL)
371dbc42409SLawrence Stewart 		CC_ALGO(tp)->conn_init(tp->ccv);
372dbc42409SLawrence Stewart }
373dbc42409SLawrence Stewart 
374dbc42409SLawrence Stewart void inline
375dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type)
376dbc42409SLawrence Stewart {
377dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
378dbc42409SLawrence Stewart 
379dbc42409SLawrence Stewart 	switch(type) {
380dbc42409SLawrence Stewart 	case CC_NDUPACK:
381dbc42409SLawrence Stewart 		if (!IN_FASTRECOVERY(tp->t_flags)) {
382f2512ba1SRui Paulo 			tp->snd_recover = tp->snd_max;
383f2512ba1SRui Paulo 			if (tp->t_flags & TF_ECN_PERMIT)
384f2512ba1SRui Paulo 				tp->t_flags |= TF_ECN_SND_CWR;
385f2512ba1SRui Paulo 		}
386dbc42409SLawrence Stewart 		break;
387dbc42409SLawrence Stewart 	case CC_ECN:
388dbc42409SLawrence Stewart 		if (!IN_CONGRECOVERY(tp->t_flags)) {
389dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_ecn_rcwnd);
390dbc42409SLawrence Stewart 			tp->snd_recover = tp->snd_max;
391dbc42409SLawrence Stewart 			if (tp->t_flags & TF_ECN_PERMIT)
392dbc42409SLawrence Stewart 				tp->t_flags |= TF_ECN_SND_CWR;
393dbc42409SLawrence Stewart 		}
394dbc42409SLawrence Stewart 		break;
395dbc42409SLawrence Stewart 	case CC_RTO:
396dbc42409SLawrence Stewart 		tp->t_dupacks = 0;
397dbc42409SLawrence Stewart 		tp->t_bytes_acked = 0;
398dbc42409SLawrence Stewart 		EXIT_RECOVERY(tp->t_flags);
3996157935fSLawrence Stewart 		tp->snd_ssthresh = max(2, min(tp->snd_wnd, tp->snd_cwnd) / 2 /
4006157935fSLawrence Stewart 		    tp->t_maxseg) * tp->t_maxseg;
401dbc42409SLawrence Stewart 		tp->snd_cwnd = tp->t_maxseg;
402dbc42409SLawrence Stewart 		break;
403dbc42409SLawrence Stewart 	case CC_RTO_ERR:
404dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_sndrexmitbad);
405dbc42409SLawrence Stewart 		/* RTO was unnecessary, so reset everything. */
406dbc42409SLawrence Stewart 		tp->snd_cwnd = tp->snd_cwnd_prev;
407dbc42409SLawrence Stewart 		tp->snd_ssthresh = tp->snd_ssthresh_prev;
408dbc42409SLawrence Stewart 		tp->snd_recover = tp->snd_recover_prev;
409dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASFRECOVERY)
410dbc42409SLawrence Stewart 			ENTER_FASTRECOVERY(tp->t_flags);
411dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASCRECOVERY)
412dbc42409SLawrence Stewart 			ENTER_CONGRECOVERY(tp->t_flags);
413dbc42409SLawrence Stewart 		tp->snd_nxt = tp->snd_max;
414672dc4aeSJohn Baldwin 		tp->t_flags &= ~TF_PREVVALID;
415dbc42409SLawrence Stewart 		tp->t_badrxtwin = 0;
416dbc42409SLawrence Stewart 		break;
417dbc42409SLawrence Stewart 	}
418dbc42409SLawrence Stewart 
419dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->cong_signal != NULL) {
420dbc42409SLawrence Stewart 		if (th != NULL)
421dbc42409SLawrence Stewart 			tp->ccv->curack = th->th_ack;
422dbc42409SLawrence Stewart 		CC_ALGO(tp)->cong_signal(tp->ccv, type);
423dbc42409SLawrence Stewart 	}
424dbc42409SLawrence Stewart }
425dbc42409SLawrence Stewart 
426dbc42409SLawrence Stewart static void inline
427dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th)
428dbc42409SLawrence Stewart {
429dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
430dbc42409SLawrence Stewart 
431dbc42409SLawrence Stewart 	/* XXXLAS: KASSERT that we're in recovery? */
432dbc42409SLawrence Stewart 
433dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->post_recovery != NULL) {
434dbc42409SLawrence Stewart 		tp->ccv->curack = th->th_ack;
435dbc42409SLawrence Stewart 		CC_ALGO(tp)->post_recovery(tp->ccv);
436dbc42409SLawrence Stewart 	}
437dbc42409SLawrence Stewart 	/* XXXLAS: EXIT_RECOVERY ? */
438dbc42409SLawrence Stewart 	tp->t_bytes_acked = 0;
439dbc42409SLawrence Stewart }
4400312fbe9SPoul-Henning Kamp 
4412903309aSAttilio Rao static inline void
4422903309aSAttilio Rao tcp_fields_to_host(struct tcphdr *th)
4432903309aSAttilio Rao {
4442903309aSAttilio Rao 
4452903309aSAttilio Rao 	th->th_seq = ntohl(th->th_seq);
4462903309aSAttilio Rao 	th->th_ack = ntohl(th->th_ack);
4472903309aSAttilio Rao 	th->th_win = ntohs(th->th_win);
4482903309aSAttilio Rao 	th->th_urp = ntohs(th->th_urp);
4492903309aSAttilio Rao }
4502903309aSAttilio Rao 
4512903309aSAttilio Rao #ifdef TCP_SIGNATURE
4522903309aSAttilio Rao static inline void
4532903309aSAttilio Rao tcp_fields_to_net(struct tcphdr *th)
4542903309aSAttilio Rao {
4552903309aSAttilio Rao 
4562903309aSAttilio Rao 	th->th_seq = htonl(th->th_seq);
4572903309aSAttilio Rao 	th->th_ack = htonl(th->th_ack);
4582903309aSAttilio Rao 	th->th_win = htons(th->th_win);
4592903309aSAttilio Rao 	th->th_urp = htons(th->th_urp);
4602903309aSAttilio Rao }
4612903309aSAttilio Rao 
4622903309aSAttilio Rao static inline int
4632903309aSAttilio Rao tcp_signature_verify_input(struct mbuf *m, int off0, int tlen, int optlen,
4642903309aSAttilio Rao     struct tcpopt *to, struct tcphdr *th, u_int tcpbflag)
4652903309aSAttilio Rao {
4662903309aSAttilio Rao 	int ret;
4672903309aSAttilio Rao 
4682903309aSAttilio Rao 	tcp_fields_to_net(th);
4692903309aSAttilio Rao 	ret = tcp_signature_verify(m, off0, tlen, optlen, to, th, tcpbflag);
4702903309aSAttilio Rao 	tcp_fields_to_host(th);
4712903309aSAttilio Rao 	return (ret);
4722903309aSAttilio Rao }
4732903309aSAttilio Rao #endif
4742903309aSAttilio Rao 
475fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
476fb59c426SYoshinobu Inoue #ifdef INET6
477fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \
478fb59c426SYoshinobu Inoue do { \
479fb59c426SYoshinobu Inoue 	if ((tp) && (tp)->t_inpcb && \
48097d8d152SAndre Oppermann 	    ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \
48197d8d152SAndre Oppermann 		nd6_nud_hint(NULL, NULL, 0); \
482fb59c426SYoshinobu Inoue } while (0)
483fb59c426SYoshinobu Inoue #else
484fb59c426SYoshinobu Inoue #define ND6_HINT(tp)
485fb59c426SYoshinobu Inoue #endif
486df8bae1dSRodney W. Grimes 
487df8bae1dSRodney W. Grimes /*
488262c1c1aSMatthew Dillon  * Indicate whether this ack should be delayed.  We can delay the ack if
489262c1c1aSMatthew Dillon  *	- there is no delayed ack timer in progress and
490262c1c1aSMatthew Dillon  *	- our last ack wasn't a 0-sized window.  We never want to delay
4913bfd6421SJonathan Lemon  *	  the ack that opens up a 0-sized window and
4923bfd6421SJonathan Lemon  *		- delayed acks are enabled or
4933bfd6421SJonathan Lemon  *		- this is a half-synchronized T/TCP connection.
494d8c85a26SJonathan Lemon  */
495d8c85a26SJonathan Lemon #define DELAY_ACK(tp)							\
496b8152ba7SAndre Oppermann 	((!tcp_timer_active(tp, TT_DELACK) &&				\
497a14c749fSJonathan Lemon 	    (tp->t_flags & TF_RXWIN0SENT) == 0) &&			\
498603724d3SBjoern A. Zeeb 	    (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
499d8c85a26SJonathan Lemon 
500df8bae1dSRodney W. Grimes /*
5018d573cc1SAndre Oppermann  * TCP input handling is split into multiple parts:
5028d573cc1SAndre Oppermann  *   tcp6_input is a thin wrapper around tcp_input for the extended
5038d573cc1SAndre Oppermann  *	ip6_protox[] call format in ip6_input
5048d573cc1SAndre Oppermann  *   tcp_input handles primary segment validation, inpcb lookup and
5058d573cc1SAndre Oppermann  *	SYN processing on listen sockets
5068d573cc1SAndre Oppermann  *   tcp_do_segment processes the ACK and text of the segment for
5078d573cc1SAndre Oppermann  *	establishing, established and closing connections
508df8bae1dSRodney W. Grimes  */
509fb59c426SYoshinobu Inoue #ifdef INET6
510fb59c426SYoshinobu Inoue int
511ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto)
512fb59c426SYoshinobu Inoue {
513ad3f9ab3SAndre Oppermann 	struct mbuf *m = *mp;
51433841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
515fb59c426SYoshinobu Inoue 
516fb59c426SYoshinobu Inoue 	IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
517fb59c426SYoshinobu Inoue 
518fb59c426SYoshinobu Inoue 	/*
519fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
520fb59c426SYoshinobu Inoue 	 * better place to put this in?
521fb59c426SYoshinobu Inoue 	 */
52233841545SHajimu UMEMOTO 	ia6 = ip6_getdstifaddr(m);
52333841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
524fb59c426SYoshinobu Inoue 		struct ip6_hdr *ip6;
525fb59c426SYoshinobu Inoue 
5268c0fec80SRobert Watson 		ifa_free(&ia6->ia_ifa);
527fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
528fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
529fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
530fb59c426SYoshinobu Inoue 		return IPPROTO_DONE;
531fb59c426SYoshinobu Inoue 	}
53277d396fdSMaksim Yevmenkin 	if (ia6)
53377d396fdSMaksim Yevmenkin 		ifa_free(&ia6->ia_ifa);
534fb59c426SYoshinobu Inoue 
535f0ffb944SJulian Elischer 	tcp_input(m, *offp);
536fb59c426SYoshinobu Inoue 	return IPPROTO_DONE;
537fb59c426SYoshinobu Inoue }
538b287c6c7SBjoern A. Zeeb #endif /* INET6 */
539fb59c426SYoshinobu Inoue 
540df8bae1dSRodney W. Grimes void
541ad3f9ab3SAndre Oppermann tcp_input(struct mbuf *m, int off0)
542df8bae1dSRodney W. Grimes {
543b287c6c7SBjoern A. Zeeb 	struct tcphdr *th = NULL;
544ad3f9ab3SAndre Oppermann 	struct ip *ip = NULL;
545ad3f9ab3SAndre Oppermann 	struct inpcb *inp = NULL;
546302ce8d6SAndre Oppermann 	struct tcpcb *tp = NULL;
547302ce8d6SAndre Oppermann 	struct socket *so = NULL;
548e79adb8eSGarrett Wollman 	u_char *optp = NULL;
54926f9a767SRodney W. Grimes 	int optlen = 0;
550b287c6c7SBjoern A. Zeeb #ifdef INET
551b287c6c7SBjoern A. Zeeb 	int len;
552b287c6c7SBjoern A. Zeeb #endif
553b287c6c7SBjoern A. Zeeb 	int tlen = 0, off;
554fb59c426SYoshinobu Inoue 	int drop_hdrlen;
555ad3f9ab3SAndre Oppermann 	int thflags;
556302ce8d6SAndre Oppermann 	int rstreason = 0;	/* For badport_bandlim accounting purposes */
5572903309aSAttilio Rao #ifdef TCP_SIGNATURE
5582903309aSAttilio Rao 	uint8_t sig_checked = 0;
5592903309aSAttilio Rao #endif
560b287c6c7SBjoern A. Zeeb 	uint8_t iptos = 0;
561c1de64a4SAndrey V. Elsukov 	struct m_tag *fwd_tag = NULL;
562c068736aSJeffrey Hsu #ifdef INET6
563302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6 = NULL;
564c068736aSJeffrey Hsu 	int isipv6;
565c068736aSJeffrey Hsu #else
566a160e630SAndre Oppermann 	const void *ip6 = NULL;
567b287c6c7SBjoern A. Zeeb #endif /* INET6 */
568302ce8d6SAndre Oppermann 	struct tcpopt to;		/* options in this segment */
569a160e630SAndre Oppermann 	char *s = NULL;			/* address and port logging */
570252ca428SRobert Watson 	int ti_locked;
571252ca428SRobert Watson #define	TI_UNLOCKED	1
572fa046d87SRobert Watson #define	TI_WLOCKED	2
573f76fcf6dSJeffrey Hsu 
574610ee2f9SDavid Greenman #ifdef TCPDEBUG
575c068736aSJeffrey Hsu 	/*
576c068736aSJeffrey Hsu 	 * The size of tcp_saveipgen must be the size of the max ip header,
577c068736aSJeffrey Hsu 	 * now IPv6.
578c068736aSJeffrey Hsu 	 */
57914739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
580410bb1bfSLuigi Rizzo 	struct tcphdr tcp_savetcp;
581610ee2f9SDavid Greenman 	short ostate = 0;
582610ee2f9SDavid Greenman #endif
583c068736aSJeffrey Hsu 
584fb59c426SYoshinobu Inoue #ifdef INET6
585fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
586fb59c426SYoshinobu Inoue #endif
587a0292f23SGarrett Wollman 
588302ce8d6SAndre Oppermann 	to.to_flags = 0;
58978b50714SRobert Watson 	TCPSTAT_INC(tcps_rcvtotal);
590fb59c426SYoshinobu Inoue 
59104d3a452SHajimu UMEMOTO #ifdef INET6
592b287c6c7SBjoern A. Zeeb 	if (isipv6) {
5938d573cc1SAndre Oppermann 		/* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */
59445747ba5SBjoern A. Zeeb 
59545747ba5SBjoern A. Zeeb 		if (m->m_len < (sizeof(*ip6) + sizeof(*th))) {
59645747ba5SBjoern A. Zeeb 			m = m_pullup(m, sizeof(*ip6) + sizeof(*th));
59745747ba5SBjoern A. Zeeb 			if (m == NULL) {
59845747ba5SBjoern A. Zeeb 				TCPSTAT_INC(tcps_rcvshort);
59945747ba5SBjoern A. Zeeb 				return;
60045747ba5SBjoern A. Zeeb 			}
60145747ba5SBjoern A. Zeeb 		}
60245747ba5SBjoern A. Zeeb 
603fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
60445747ba5SBjoern A. Zeeb 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
605fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
606356ab07eSBjoern A. Zeeb 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) {
60745747ba5SBjoern A. Zeeb 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
60845747ba5SBjoern A. Zeeb 				th->th_sum = m->m_pkthdr.csum_data;
60945747ba5SBjoern A. Zeeb 			else
61045747ba5SBjoern A. Zeeb 				th->th_sum = in6_cksum_pseudo(ip6, tlen,
61145747ba5SBjoern A. Zeeb 				    IPPROTO_TCP, m->m_pkthdr.csum_data);
61245747ba5SBjoern A. Zeeb 			th->th_sum ^= 0xffff;
61345747ba5SBjoern A. Zeeb 		} else
61445747ba5SBjoern A. Zeeb 			th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen);
61545747ba5SBjoern A. Zeeb 		if (th->th_sum) {
61678b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
617fb59c426SYoshinobu Inoue 			goto drop;
618fb59c426SYoshinobu Inoue 		}
61933841545SHajimu UMEMOTO 
62033841545SHajimu UMEMOTO 		/*
62133841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
62233841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
62333841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
62433841545SHajimu UMEMOTO 		 *
62533841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
62633841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
62733841545SHajimu UMEMOTO 		 */
62833841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
62933841545SHajimu UMEMOTO 			/* XXX stat */
63033841545SHajimu UMEMOTO 			goto drop;
63133841545SHajimu UMEMOTO 		}
632b287c6c7SBjoern A. Zeeb 	}
63304d3a452SHajimu UMEMOTO #endif
634b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
635b287c6c7SBjoern A. Zeeb 	else
636b287c6c7SBjoern A. Zeeb #endif
637b287c6c7SBjoern A. Zeeb #ifdef INET
638b287c6c7SBjoern A. Zeeb 	{
639df8bae1dSRodney W. Grimes 		/*
640df8bae1dSRodney W. Grimes 		 * Get IP and TCP header together in first mbuf.
641df8bae1dSRodney W. Grimes 		 * Note: IP leaves IP header in first mbuf.
642df8bae1dSRodney W. Grimes 		 */
643fb59c426SYoshinobu Inoue 		if (off0 > sizeof (struct ip)) {
644105bd211SGleb Smirnoff 			ip_stripoptions(m);
645fb59c426SYoshinobu Inoue 			off0 = sizeof(struct ip);
646fb59c426SYoshinobu Inoue 		}
647df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof (struct tcpiphdr)) {
6484dfdffe9SAndre Oppermann 			if ((m = m_pullup(m, sizeof (struct tcpiphdr)))
6494dfdffe9SAndre Oppermann 			    == NULL) {
65078b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvshort);
651df8bae1dSRodney W. Grimes 				return;
652df8bae1dSRodney W. Grimes 			}
653df8bae1dSRodney W. Grimes 		}
654fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
655db4f9cc7SJonathan Lemon 		th = (struct tcphdr *)((caddr_t)ip + off0);
6568ad458a4SGleb Smirnoff 		tlen = ntohs(ip->ip_len) - off0;
657df8bae1dSRodney W. Grimes 
658db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
659db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
660db4f9cc7SJonathan Lemon 				th->th_sum = m->m_pkthdr.csum_data;
661db4f9cc7SJonathan Lemon 			else
662db4f9cc7SJonathan Lemon 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
663c068736aSJeffrey Hsu 				    ip->ip_dst.s_addr,
6648f134647SGleb Smirnoff 				    htonl(m->m_pkthdr.csum_data + tlen +
665c068736aSJeffrey Hsu 				    IPPROTO_TCP));
666db4f9cc7SJonathan Lemon 			th->th_sum ^= 0xffff;
667db4f9cc7SJonathan Lemon 		} else {
6688f134647SGleb Smirnoff 			struct ipovly *ipov = (struct ipovly *)ip;
6698f134647SGleb Smirnoff 
670df8bae1dSRodney W. Grimes 			/*
671df8bae1dSRodney W. Grimes 			 * Checksum extended TCP header and data.
672df8bae1dSRodney W. Grimes 			 */
6738f134647SGleb Smirnoff 			len = off0 + tlen;
674fb59c426SYoshinobu Inoue 			bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
6758f134647SGleb Smirnoff 			ipov->ih_len = htons(tlen);
676fb59c426SYoshinobu Inoue 			th->th_sum = in_cksum(m, len);
677db4f9cc7SJonathan Lemon 		}
678fb59c426SYoshinobu Inoue 		if (th->th_sum) {
67978b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
680df8bae1dSRodney W. Grimes 			goto drop;
681df8bae1dSRodney W. Grimes 		}
682fb59c426SYoshinobu Inoue 		/* Re-initialization for later version check */
683fb59c426SYoshinobu Inoue 		ip->ip_v = IPVERSION;
684fb59c426SYoshinobu Inoue 	}
685b287c6c7SBjoern A. Zeeb #endif /* INET */
686df8bae1dSRodney W. Grimes 
687f2512ba1SRui Paulo #ifdef INET6
688f2512ba1SRui Paulo 	if (isipv6)
689f2512ba1SRui Paulo 		iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
690b287c6c7SBjoern A. Zeeb #endif
691b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
692f2512ba1SRui Paulo 	else
693f2512ba1SRui Paulo #endif
694b287c6c7SBjoern A. Zeeb #ifdef INET
695f2512ba1SRui Paulo 		iptos = ip->ip_tos;
696b287c6c7SBjoern A. Zeeb #endif
697f2512ba1SRui Paulo 
698df8bae1dSRodney W. Grimes 	/*
699df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
700df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
701df8bae1dSRodney W. Grimes 	 */
702fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
703df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
70478b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvbadoff);
705df8bae1dSRodney W. Grimes 		goto drop;
706df8bae1dSRodney W. Grimes 	}
707fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
708df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
70904d3a452SHajimu UMEMOTO #ifdef INET6
710b287c6c7SBjoern A. Zeeb 		if (isipv6) {
711fb59c426SYoshinobu Inoue 			IP6_EXTHDR_CHECK(m, off0, off, );
712fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
713fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
714b287c6c7SBjoern A. Zeeb 		}
71504d3a452SHajimu UMEMOTO #endif
716b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
717b287c6c7SBjoern A. Zeeb 		else
718b287c6c7SBjoern A. Zeeb #endif
719b287c6c7SBjoern A. Zeeb #ifdef INET
720b287c6c7SBjoern A. Zeeb 		{
721df8bae1dSRodney W. Grimes 			if (m->m_len < sizeof(struct ip) + off) {
722c068736aSJeffrey Hsu 				if ((m = m_pullup(m, sizeof (struct ip) + off))
7234dfdffe9SAndre Oppermann 				    == NULL) {
72478b50714SRobert Watson 					TCPSTAT_INC(tcps_rcvshort);
725df8bae1dSRodney W. Grimes 					return;
726df8bae1dSRodney W. Grimes 				}
727fb59c426SYoshinobu Inoue 				ip = mtod(m, struct ip *);
728fb59c426SYoshinobu Inoue 				th = (struct tcphdr *)((caddr_t)ip + off0);
729fb59c426SYoshinobu Inoue 			}
730df8bae1dSRodney W. Grimes 		}
731b287c6c7SBjoern A. Zeeb #endif
732df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
733fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
734df8bae1dSRodney W. Grimes 	}
735fb59c426SYoshinobu Inoue 	thflags = th->th_flags;
736df8bae1dSRodney W. Grimes 
737e46cd3d4SDag-Erling Smørgrav 	/*
738df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
739df8bae1dSRodney W. Grimes 	 */
7402903309aSAttilio Rao 	tcp_fields_to_host(th);
741df8bae1dSRodney W. Grimes 
742df8bae1dSRodney W. Grimes 	/*
743794235b7SAndre Oppermann 	 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options.
744755c1f07SAndras Olah 	 */
745fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
746755c1f07SAndras Olah 
747755c1f07SAndras Olah 	/*
748fa046d87SRobert Watson 	 * Locate pcb for segment; if we're likely to add or remove a
749fa046d87SRobert Watson 	 * connection then first acquire pcbinfo lock.  There are two cases
750fa046d87SRobert Watson 	 * where we might discover later we need a write lock despite the
751fa046d87SRobert Watson 	 * flags: ACKs moving a connection out of the syncache, and ACKs for
752fa046d87SRobert Watson 	 * a connection in TIMEWAIT.
753df8bae1dSRodney W. Grimes 	 */
754fa046d87SRobert Watson 	if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0) {
755603724d3SBjoern A. Zeeb 		INP_INFO_WLOCK(&V_tcbinfo);
756252ca428SRobert Watson 		ti_locked = TI_WLOCKED;
757fa046d87SRobert Watson 	} else
758fa046d87SRobert Watson 		ti_locked = TI_UNLOCKED;
759252ca428SRobert Watson 
760df8bae1dSRodney W. Grimes findpcb:
761252ca428SRobert Watson #ifdef INVARIANTS
762fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED) {
763603724d3SBjoern A. Zeeb 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
764fa046d87SRobert Watson 	} else {
765fa046d87SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
766fa046d87SRobert Watson 	}
767252ca428SRobert Watson #endif
768252ca428SRobert Watson 
7698d573cc1SAndre Oppermann 	/*
7708d573cc1SAndre Oppermann 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
7718d573cc1SAndre Oppermann 	 */
772c1de64a4SAndrey V. Elsukov 	if (V_pfilforward != 0)
7739b932e9eSAndre Oppermann 		fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
7749b932e9eSAndre Oppermann 
7758a006adbSBjoern A. Zeeb #ifdef INET6
7768a006adbSBjoern A. Zeeb 	if (isipv6 && fwd_tag != NULL) {
7778a006adbSBjoern A. Zeeb 		struct sockaddr_in6 *next_hop6;
7788a006adbSBjoern A. Zeeb 
7798a006adbSBjoern A. Zeeb 		next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
7808a006adbSBjoern A. Zeeb 		/*
7818a006adbSBjoern A. Zeeb 		 * Transparently forwarded. Pretend to be the destination.
7828a006adbSBjoern A. Zeeb 		 * Already got one like this?
7838a006adbSBjoern A. Zeeb 		 */
7848a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo,
7858a006adbSBjoern A. Zeeb 		    &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport,
7868a006adbSBjoern A. Zeeb 		    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m);
7878a006adbSBjoern A. Zeeb 		if (!inp) {
7888a006adbSBjoern A. Zeeb 			/*
7898a006adbSBjoern A. Zeeb 			 * It's new.  Try to find the ambushing socket.
7908a006adbSBjoern A. Zeeb 			 * Because we've rewritten the destination address,
7918a006adbSBjoern A. Zeeb 			 * any hardware-generated hash is ignored.
7928a006adbSBjoern A. Zeeb 			 */
7938a006adbSBjoern A. Zeeb 			inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src,
7948a006adbSBjoern A. Zeeb 			    th->th_sport, &next_hop6->sin6_addr,
7958a006adbSBjoern A. Zeeb 			    next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) :
7968a006adbSBjoern A. Zeeb 			    th->th_dport, INPLOOKUP_WILDCARD |
7978a006adbSBjoern A. Zeeb 			    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
7988a006adbSBjoern A. Zeeb 		}
7998a006adbSBjoern A. Zeeb 		/* Remove the tag from the packet.  We don't need it anymore. */
8008a006adbSBjoern A. Zeeb 		m_tag_delete(m, fwd_tag);
801c1de64a4SAndrey V. Elsukov 	} else if (isipv6) {
8028a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src,
8038a006adbSBjoern A. Zeeb 		    th->th_sport, &ip6->ip6_dst, th->th_dport,
8048a006adbSBjoern A. Zeeb 		    INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
8058a006adbSBjoern A. Zeeb 		    m->m_pkthdr.rcvif, m);
8068a006adbSBjoern A. Zeeb 	}
8078a006adbSBjoern A. Zeeb #endif /* INET6 */
8088a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET)
8098a006adbSBjoern A. Zeeb 	else
8108a006adbSBjoern A. Zeeb #endif
8118a006adbSBjoern A. Zeeb #ifdef INET
8128a006adbSBjoern A. Zeeb 	if (fwd_tag != NULL) {
8139b932e9eSAndre Oppermann 		struct sockaddr_in *next_hop;
8149b932e9eSAndre Oppermann 
8159b932e9eSAndre Oppermann 		next_hop = (struct sockaddr_in *)(fwd_tag+1);
816f9e354dfSJulian Elischer 		/*
8172b25acc1SLuigi Rizzo 		 * Transparently forwarded. Pretend to be the destination.
818f9e354dfSJulian Elischer 		 * already got one like this?
819f9e354dfSJulian Elischer 		 */
820d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport,
821fa046d87SRobert Watson 		    ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB,
822d3c1f003SRobert Watson 		    m->m_pkthdr.rcvif, m);
823fa046d87SRobert Watson 		if (!inp) {
824fa046d87SRobert Watson 			/*
825fa046d87SRobert Watson 			 * It's new.  Try to find the ambushing socket.
826d3c1f003SRobert Watson 			 * Because we've rewritten the destination address,
827d3c1f003SRobert Watson 			 * any hardware-generated hash is ignored.
828fa046d87SRobert Watson 			 */
829fa046d87SRobert Watson 			inp = in_pcblookup(&V_tcbinfo, ip->ip_src,
830fa046d87SRobert Watson 			    th->th_sport, next_hop->sin_addr,
831fa046d87SRobert Watson 			    next_hop->sin_port ? ntohs(next_hop->sin_port) :
832fa046d87SRobert Watson 			    th->th_dport, INPLOOKUP_WILDCARD |
833fa046d87SRobert Watson 			    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
834f9e354dfSJulian Elischer 		}
8359b932e9eSAndre Oppermann 		/* Remove the tag from the packet.  We don't need it anymore. */
8369b932e9eSAndre Oppermann 		m_tag_delete(m, fwd_tag);
837b10fbdeaSAndre Oppermann 	} else
838d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src,
839fa046d87SRobert Watson 		    th->th_sport, ip->ip_dst, th->th_dport,
840fa046d87SRobert Watson 		    INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
841d3c1f003SRobert Watson 		    m->m_pkthdr.rcvif, m);
8428a006adbSBjoern A. Zeeb #endif /* INET */
843f9e354dfSJulian Elischer 
844df8bae1dSRodney W. Grimes 	/*
845574b6964SAndre Oppermann 	 * If the INPCB does not exist then all data in the incoming
846574b6964SAndre Oppermann 	 * segment is discarded and an appropriate RST is sent back.
8478b07e49aSJulian Elischer 	 * XXX MRT Send RST using which routing table?
848df8bae1dSRodney W. Grimes 	 */
849816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
850574b6964SAndre Oppermann 		/*
851574b6964SAndre Oppermann 		 * Log communication attempts to ports that are not
852574b6964SAndre Oppermann 		 * in use.
853574b6964SAndre Oppermann 		 */
854574b6964SAndre Oppermann 		if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) ||
855574b6964SAndre Oppermann 		    tcp_log_in_vain == 2) {
856b7d747ecSAndre Oppermann 			if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6)))
857df541e5fSAndre Oppermann 				log(LOG_INFO, "%s; %s: Connection attempt "
858df541e5fSAndre Oppermann 				    "to closed port\n", s, __func__);
8592e4e1b4cSGeoff Rehmet 		}
860574b6964SAndre Oppermann 		/*
861574b6964SAndre Oppermann 		 * When blackholing do not respond with a RST but
862574b6964SAndre Oppermann 		 * completely ignore the segment and drop it.
863574b6964SAndre Oppermann 		 */
864603724d3SBjoern A. Zeeb 		if ((V_blackhole == 1 && (thflags & TH_SYN)) ||
865603724d3SBjoern A. Zeeb 		    V_blackhole == 2)
8661929eae1SAndre Oppermann 			goto dropunlock;
867574b6964SAndre Oppermann 
868a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
869a57815efSBosko Milekic 		goto dropwithreset;
870816a3d83SPoul-Henning Kamp 	}
871fa046d87SRobert Watson 	INP_WLOCK_ASSERT(inp);
87280cb9f21SKip Macy 	if (!(inp->inp_flags & INP_HW_FLOWID)
87380cb9f21SKip Macy 	    && (m->m_flags & M_FLOWID)
87480cb9f21SKip Macy 	    && ((inp->inp_socket == NULL)
87580cb9f21SKip Macy 		|| !(inp->inp_socket->so_options & SO_ACCEPTCONN))) {
87680cb9f21SKip Macy 		inp->inp_flags |= INP_HW_FLOWID;
87780cb9f21SKip Macy 		inp->inp_flags &= ~INP_SW_FLOWID;
87880cb9f21SKip Macy 		inp->inp_flowid = m->m_pkthdr.flowid;
87980cb9f21SKip Macy 	}
880a2589465SKen Smith #ifdef IPSEC
881a2589465SKen Smith #ifdef INET6
882a2589465SKen Smith 	if (isipv6 && ipsec6_in_reject(m, inp)) {
883603724d3SBjoern A. Zeeb 		V_ipsec6stat.in_polvio++;
884a2589465SKen Smith 		goto dropunlock;
885a2589465SKen Smith 	} else
886a2589465SKen Smith #endif /* INET6 */
887a2589465SKen Smith 	if (ipsec4_in_reject(m, inp) != 0) {
888603724d3SBjoern A. Zeeb 		V_ipsec4stat.in_polvio++;
889a2589465SKen Smith 		goto dropunlock;
890a2589465SKen Smith 	}
891a2589465SKen Smith #endif /* IPSEC */
892a2589465SKen Smith 
8938d573cc1SAndre Oppermann 	/*
8948d573cc1SAndre Oppermann 	 * Check the minimum TTL for socket.
8958d573cc1SAndre Oppermann 	 */
89634f83c52SGeorge V. Neville-Neil 	if (inp->inp_ip_minttl != 0) {
89734f83c52SGeorge V. Neville-Neil #ifdef INET6
89834f83c52SGeorge V. Neville-Neil 		if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim)
899302ce8d6SAndre Oppermann 			goto dropunlock;
90002707462SAndre Oppermann 		else
90134f83c52SGeorge V. Neville-Neil #endif
90234f83c52SGeorge V. Neville-Neil 		if (inp->inp_ip_minttl > ip->ip_ttl)
903302ce8d6SAndre Oppermann 			goto dropunlock;
90434f83c52SGeorge V. Neville-Neil 	}
905936cd18dSAndre Oppermann 
906340c35deSJonathan Lemon 	/*
907252ca428SRobert Watson 	 * A previous connection in TIMEWAIT state is supposed to catch stray
908252ca428SRobert Watson 	 * or duplicate segments arriving late.  If this segment was a
9090989f56cSRobert Watson 	 * legitimate new connection attempt, the old INPCB gets removed and
910252ca428SRobert Watson 	 * we can try again to find a listening socket.
911252ca428SRobert Watson 	 *
912fa046d87SRobert Watson 	 * At this point, due to earlier optimism, we may hold only an inpcb
913fa046d87SRobert Watson 	 * lock, and not the inpcbinfo write lock.  If so, we need to try to
914fa046d87SRobert Watson 	 * acquire it, or if that fails, acquire a reference on the inpcb,
915fa046d87SRobert Watson 	 * drop all locks, acquire a global write lock, and then re-acquire
916fa046d87SRobert Watson 	 * the inpcb lock.  We may at that point discover that another thread
917fa046d87SRobert Watson 	 * has tried to free the inpcb, in which case we need to loop back
918fa046d87SRobert Watson 	 * and try to find a new inpcb to deliver to.
919fa046d87SRobert Watson 	 *
920fa046d87SRobert Watson 	 * XXXRW: It may be time to rethink timewait locking.
921340c35deSJonathan Lemon 	 */
922883e9bc4SRobert Watson relocked:
923ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
924fa046d87SRobert Watson 		if (ti_locked == TI_UNLOCKED) {
925fa046d87SRobert Watson 			if (INP_INFO_TRY_WLOCK(&V_tcbinfo) == 0) {
926252ca428SRobert Watson 				in_pcbref(inp);
927252ca428SRobert Watson 				INP_WUNLOCK(inp);
928252ca428SRobert Watson 				INP_INFO_WLOCK(&V_tcbinfo);
929252ca428SRobert Watson 				ti_locked = TI_WLOCKED;
930252ca428SRobert Watson 				INP_WLOCK(inp);
931fa046d87SRobert Watson 				if (in_pcbrele_wlocked(inp)) {
932252ca428SRobert Watson 					inp = NULL;
933252ca428SRobert Watson 					goto findpcb;
934252ca428SRobert Watson 				}
935f681a5fdSRobert Watson 			} else
936252ca428SRobert Watson 				ti_locked = TI_WLOCKED;
937252ca428SRobert Watson 		}
938252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
939252ca428SRobert Watson 
940340c35deSJonathan Lemon 		if (thflags & TH_SYN)
941f72167f4SAndre Oppermann 			tcp_dooptions(&to, optp, optlen, TO_SYN);
9428d573cc1SAndre Oppermann 		/*
9438d573cc1SAndre Oppermann 		 * NB: tcp_twcheck unlocks the INP and frees the mbuf.
9448d573cc1SAndre Oppermann 		 */
9452104448fSAndre Oppermann 		if (tcp_twcheck(inp, &to, th, m, tlen))
946340c35deSJonathan Lemon 			goto findpcb;
947603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
948340c35deSJonathan Lemon 		return;
949340c35deSJonathan Lemon 	}
950794235b7SAndre Oppermann 	/*
951794235b7SAndre Oppermann 	 * The TCPCB may no longer exist if the connection is winding
952794235b7SAndre Oppermann 	 * down or it is in the CLOSED state.  Either way we drop the
953794235b7SAndre Oppermann 	 * segment and send an appropriate response.
954794235b7SAndre Oppermann 	 */
955df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
956a160e630SAndre Oppermann 	if (tp == NULL || tp->t_state == TCPS_CLOSED) {
957a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
958a57815efSBosko Milekic 		goto dropwithreset;
95909f81a46SBosko Milekic 	}
960df8bae1dSRodney W. Grimes 
96109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
96209fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
96309fe6320SNavdeep Parhar 		tcp_offload_input(tp, m);
96409fe6320SNavdeep Parhar 		m = NULL;	/* consumed by the TOE driver */
96509fe6320SNavdeep Parhar 		goto dropunlock;
96609fe6320SNavdeep Parhar 	}
96709fe6320SNavdeep Parhar #endif
96809fe6320SNavdeep Parhar 
969252ca428SRobert Watson 	/*
970252ca428SRobert Watson 	 * We've identified a valid inpcb, but it could be that we need an
971fa046d87SRobert Watson 	 * inpcbinfo write lock but don't hold it.  In this case, attempt to
972fa046d87SRobert Watson 	 * acquire using the same strategy as the TIMEWAIT case above.  If we
973fa046d87SRobert Watson 	 * relock, we have to jump back to 'relocked' as the connection might
974fa046d87SRobert Watson 	 * now be in TIMEWAIT.
975252ca428SRobert Watson 	 */
976fa046d87SRobert Watson #ifdef INVARIANTS
977fa046d87SRobert Watson 	if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0)
978fa046d87SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
979fa046d87SRobert Watson #endif
980fa046d87SRobert Watson 	if (tp->t_state != TCPS_ESTABLISHED) {
981fa046d87SRobert Watson 		if (ti_locked == TI_UNLOCKED) {
982fa046d87SRobert Watson 			if (INP_INFO_TRY_WLOCK(&V_tcbinfo) == 0) {
983252ca428SRobert Watson 				in_pcbref(inp);
984252ca428SRobert Watson 				INP_WUNLOCK(inp);
985252ca428SRobert Watson 				INP_INFO_WLOCK(&V_tcbinfo);
986252ca428SRobert Watson 				ti_locked = TI_WLOCKED;
987252ca428SRobert Watson 				INP_WLOCK(inp);
988fa046d87SRobert Watson 				if (in_pcbrele_wlocked(inp)) {
989252ca428SRobert Watson 					inp = NULL;
990252ca428SRobert Watson 					goto findpcb;
991252ca428SRobert Watson 				}
992883e9bc4SRobert Watson 				goto relocked;
993f681a5fdSRobert Watson 			} else
994252ca428SRobert Watson 				ti_locked = TI_WLOCKED;
995252ca428SRobert Watson 		}
996252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
997252ca428SRobert Watson 	}
998252ca428SRobert Watson 
999c488362eSRobert Watson #ifdef MAC
10008501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
100130d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, m))
1002302ce8d6SAndre Oppermann 		goto dropunlock;
1003c488362eSRobert Watson #endif
1004a557af22SRobert Watson 	so = inp->inp_socket;
1005302ce8d6SAndre Oppermann 	KASSERT(so != NULL, ("%s: so == NULL", __func__));
1006610ee2f9SDavid Greenman #ifdef TCPDEBUG
1007df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG) {
1008df8bae1dSRodney W. Grimes 		ostate = tp->t_state;
100910fe523eSMaxim Konovalov #ifdef INET6
10106f697424SBjoern A. Zeeb 		if (isipv6) {
1011540e8b7eSJeffrey Hsu 			bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6));
1012d30d90dcSMaxim Konovalov 		} else
10136f697424SBjoern A. Zeeb #endif
1014540e8b7eSJeffrey Hsu 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
1015fb59c426SYoshinobu Inoue 		tcp_savetcp = *th;
1016df8bae1dSRodney W. Grimes 	}
1017b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */
1018db33b3e6SAndre Oppermann 	/*
1019db33b3e6SAndre Oppermann 	 * When the socket is accepting connections (the INPCB is in LISTEN
1020db33b3e6SAndre Oppermann 	 * state) we look into the SYN cache if this is a new connection
1021fa046d87SRobert Watson 	 * attempt or the completion of a previous one.  Because listen
1022fa046d87SRobert Watson 	 * sockets are never in TCPS_ESTABLISHED, the V_tcbinfo lock will be
1023fa046d87SRobert Watson 	 * held in this case.
1024db33b3e6SAndre Oppermann 	 */
1025540e8b7eSJeffrey Hsu 	if (so->so_options & SO_ACCEPTCONN) {
1026540e8b7eSJeffrey Hsu 		struct in_conninfo inc;
1027540e8b7eSJeffrey Hsu 
10287824d002SAndre Oppermann 		KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but "
10297824d002SAndre Oppermann 		    "tp not listening", __func__));
1030fa046d87SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
10317824d002SAndre Oppermann 
10328bfb1918SAndre Oppermann 		bzero(&inc, sizeof(inc));
1033302ce8d6SAndre Oppermann #ifdef INET6
1034be2ac88cSJonathan Lemon 		if (isipv6) {
1035dcdb4371SBjoern A. Zeeb 			inc.inc_flags |= INC_ISIPV6;
1036be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
1037be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
1038302ce8d6SAndre Oppermann 		} else
1039302ce8d6SAndre Oppermann #endif
1040302ce8d6SAndre Oppermann 		{
1041be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
1042be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
1043be2ac88cSJonathan Lemon 		}
1044be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
1045be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
10467973fba3SJulian Elischer 		inc.inc_fibnum = so->so_fibnum;
1047be2ac88cSJonathan Lemon 
1048fb59c426SYoshinobu Inoue 		/*
10498d573cc1SAndre Oppermann 		 * Check for an existing connection attempt in syncache if
10508d573cc1SAndre Oppermann 		 * the flag is only ACK.  A successful lookup creates a new
10518d573cc1SAndre Oppermann 		 * socket appended to the listen queue in SYN_RECEIVED state.
1052fb59c426SYoshinobu Inoue 		 */
1053be2ac88cSJonathan Lemon 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
1054bf6d304aSAndre Oppermann 			/*
1055bf6d304aSAndre Oppermann 			 * Parse the TCP options here because
1056bf6d304aSAndre Oppermann 			 * syncookies need access to the reflected
1057bf6d304aSAndre Oppermann 			 * timestamp.
1058bf6d304aSAndre Oppermann 			 */
1059bf6d304aSAndre Oppermann 			tcp_dooptions(&to, optp, optlen, 0);
10609fa198beSAndre Oppermann 			/*
10619fa198beSAndre Oppermann 			 * NB: syncache_expand() doesn't unlock
10629fa198beSAndre Oppermann 			 * inp and tcpinfo locks.
10639fa198beSAndre Oppermann 			 */
1064bf6d304aSAndre Oppermann 			if (!syncache_expand(&inc, &to, th, &so, m)) {
10654195b4afSPaul Traina 				/*
1066e207f800SAndre Oppermann 				 * No syncache entry or ACK was not
1067be2ac88cSJonathan Lemon 				 * for our SYN/ACK.  Send a RST.
10688d573cc1SAndre Oppermann 				 * NB: syncache did its own logging
10698d573cc1SAndre Oppermann 				 * of the failure cause.
10704195b4afSPaul Traina 				 */
1071be2ac88cSJonathan Lemon 				rstreason = BANDLIM_RST_OPENPORT;
1072be2ac88cSJonathan Lemon 				goto dropwithreset;
1073be2ac88cSJonathan Lemon 			}
1074f76fcf6dSJeffrey Hsu 			if (so == NULL) {
1075be2ac88cSJonathan Lemon 				/*
1076e207f800SAndre Oppermann 				 * We completed the 3-way handshake
1077e207f800SAndre Oppermann 				 * but could not allocate a socket
1078e207f800SAndre Oppermann 				 * either due to memory shortage,
1079e207f800SAndre Oppermann 				 * listen queue length limits or
1080a160e630SAndre Oppermann 				 * global socket limits.  Send RST
1081a160e630SAndre Oppermann 				 * or wait and have the remote end
1082a160e630SAndre Oppermann 				 * retransmit the ACK for another
1083a160e630SAndre Oppermann 				 * try.
1084be2ac88cSJonathan Lemon 				 */
1085a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1086a160e630SAndre Oppermann 					log(LOG_DEBUG, "%s; %s: Listen socket: "
10878d573cc1SAndre Oppermann 					    "Socket allocation failed due to "
10888d573cc1SAndre Oppermann 					    "limits or memory shortage, %s\n",
1089ac957cd2SJulian Elischer 					    s, __func__,
1090ac957cd2SJulian Elischer 					    V_tcp_sc_rst_sock_fail ?
1091ac957cd2SJulian Elischer 					    "sending RST" : "try again");
1092603724d3SBjoern A. Zeeb 				if (V_tcp_sc_rst_sock_fail) {
1093e207f800SAndre Oppermann 					rstreason = BANDLIM_UNLIMITED;
1094e207f800SAndre Oppermann 					goto dropwithreset;
1095a160e630SAndre Oppermann 				} else
1096a160e630SAndre Oppermann 					goto dropunlock;
1097f76fcf6dSJeffrey Hsu 			}
1098be2ac88cSJonathan Lemon 			/*
1099be2ac88cSJonathan Lemon 			 * Socket is created in state SYN_RECEIVED.
11008d573cc1SAndre Oppermann 			 * Unlock the listen socket, lock the newly
11018d573cc1SAndre Oppermann 			 * created socket and update the tp variable.
1102be2ac88cSJonathan Lemon 			 */
11038501a69cSRobert Watson 			INP_WUNLOCK(inp);	/* listen socket */
1104be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
11058501a69cSRobert Watson 			INP_WLOCK(inp);		/* new connection */
1106be2ac88cSJonathan Lemon 			tp = intotcpcb(inp);
11078d573cc1SAndre Oppermann 			KASSERT(tp->t_state == TCPS_SYN_RECEIVED,
11088d573cc1SAndre Oppermann 			    ("%s: ", __func__));
11092903309aSAttilio Rao #ifdef TCP_SIGNATURE
11102903309aSAttilio Rao 			if (sig_checked == 0)  {
11112903309aSAttilio Rao 				tcp_dooptions(&to, optp, optlen,
11122903309aSAttilio Rao 				    (thflags & TH_SYN) ? TO_SYN : 0);
11132903309aSAttilio Rao 				if (!tcp_signature_verify_input(m, off0, tlen,
11142903309aSAttilio Rao 				    optlen, &to, th, tp->t_flags)) {
11152903309aSAttilio Rao 
11162903309aSAttilio Rao 					/*
11172903309aSAttilio Rao 					 * In SYN_SENT state if it receives an
11182903309aSAttilio Rao 					 * RST, it is allowed for further
11192903309aSAttilio Rao 					 * processing.
11202903309aSAttilio Rao 					 */
11212903309aSAttilio Rao 					if ((thflags & TH_RST) == 0 ||
11222903309aSAttilio Rao 					    (tp->t_state == TCPS_SYN_SENT) == 0)
11232903309aSAttilio Rao 						goto dropunlock;
11242903309aSAttilio Rao 				}
11252903309aSAttilio Rao 				sig_checked = 1;
11262903309aSAttilio Rao 			}
11272903309aSAttilio Rao #endif
11282903309aSAttilio Rao 
1129be2ac88cSJonathan Lemon 			/*
1130302ce8d6SAndre Oppermann 			 * Process the segment and the data it
1131302ce8d6SAndre Oppermann 			 * contains.  tcp_do_segment() consumes
1132302ce8d6SAndre Oppermann 			 * the mbuf chain and unlocks the inpcb.
1133302ce8d6SAndre Oppermann 			 */
1134f2512ba1SRui Paulo 			tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen,
1135252ca428SRobert Watson 			    iptos, ti_locked);
1136603724d3SBjoern A. Zeeb 			INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1137302ce8d6SAndre Oppermann 			return;
1138be2ac88cSJonathan Lemon 		}
1139a160e630SAndre Oppermann 		/*
11408d573cc1SAndre Oppermann 		 * Segment flag validation for new connection attempts:
11418d573cc1SAndre Oppermann 		 *
1142a160e630SAndre Oppermann 		 * Our (SYN|ACK) response was rejected.
1143a160e630SAndre Oppermann 		 * Check with syncache and remove entry to prevent
1144a160e630SAndre Oppermann 		 * retransmits.
114519bc77c5SAndre Oppermann 		 *
114619bc77c5SAndre Oppermann 		 * NB: syncache_chkrst does its own logging of failure
114719bc77c5SAndre Oppermann 		 * causes.
1148a160e630SAndre Oppermann 		 */
1149a160e630SAndre Oppermann 		if (thflags & TH_RST) {
115019bc77c5SAndre Oppermann 			syncache_chkrst(&inc, th);
1151a160e630SAndre Oppermann 			goto dropunlock;
1152a160e630SAndre Oppermann 		}
1153a160e630SAndre Oppermann 		/*
1154a160e630SAndre Oppermann 		 * We can't do anything without SYN.
1155a160e630SAndre Oppermann 		 */
1156a160e630SAndre Oppermann 		if ((thflags & TH_SYN) == 0) {
1157a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1158a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1159773673c1SAndre Oppermann 				    "SYN is missing, segment ignored\n",
11608d573cc1SAndre Oppermann 				    s, __func__);
116178b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1162a160e630SAndre Oppermann 			goto dropunlock;
1163a160e630SAndre Oppermann 		}
1164a160e630SAndre Oppermann 		/*
1165a160e630SAndre Oppermann 		 * (SYN|ACK) is bogus on a listen socket.
1166a160e630SAndre Oppermann 		 */
1167fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1168a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1169a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
11708d573cc1SAndre Oppermann 				    "SYN|ACK invalid, segment rejected\n",
11718d573cc1SAndre Oppermann 				    s, __func__);
1172a160e630SAndre Oppermann 			syncache_badack(&inc);	/* XXX: Not needed! */
117378b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1174a57815efSBosko Milekic 			rstreason = BANDLIM_RST_OPENPORT;
1175a57815efSBosko Milekic 			goto dropwithreset;
11764195b4afSPaul Traina 		}
1177a160e630SAndre Oppermann 		/*
1178a160e630SAndre Oppermann 		 * If the drop_synfin option is enabled, drop all
1179a160e630SAndre Oppermann 		 * segments with both the SYN and FIN bits set.
1180a160e630SAndre Oppermann 		 * This prevents e.g. nmap from identifying the
1181a160e630SAndre Oppermann 		 * TCP/IP stack.
1182a160e630SAndre Oppermann 		 * XXX: Poor reasoning.  nmap has other methods
1183a160e630SAndre Oppermann 		 * and is constantly refining its stack detection
1184a160e630SAndre Oppermann 		 * strategies.
11858d573cc1SAndre Oppermann 		 * XXX: This is a violation of the TCP specification
1186a160e630SAndre Oppermann 		 * and was used by RFC1644.
1187a160e630SAndre Oppermann 		 */
1188603724d3SBjoern A. Zeeb 		if ((thflags & TH_FIN) && V_drop_synfin) {
1189a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1190a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1191773673c1SAndre Oppermann 				    "SYN|FIN segment ignored (based on "
11928d573cc1SAndre Oppermann 				    "sysctl setting)\n", s, __func__);
119378b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1194302ce8d6SAndre Oppermann 			goto dropunlock;
1195ebb0cbeaSPaul Traina 		}
1196be2ac88cSJonathan Lemon 		/*
1197be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
1198a160e630SAndre Oppermann 		 *
1199a160e630SAndre Oppermann 		 * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored
1200a160e630SAndre Oppermann 		 * as they do not affect the state of the TCP FSM.
1201a160e630SAndre Oppermann 		 * The data pointed to by TH_URG and th_urp is ignored.
1202be2ac88cSJonathan Lemon 		 */
1203a160e630SAndre Oppermann 		KASSERT((thflags & (TH_RST|TH_ACK)) == 0,
1204a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_RST or TH_ACK set", __func__));
1205a160e630SAndre Oppermann 		KASSERT(thflags & (TH_SYN),
1206a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_SYN not set", __func__));
120733841545SHajimu UMEMOTO #ifdef INET6
120833841545SHajimu UMEMOTO 		/*
120933841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
121033841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
121133841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
121233841545SHajimu UMEMOTO 		 * getting established.
121333841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
121433841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
121533841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
121633841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
121733841545SHajimu UMEMOTO 		 * for the exchange.
121833841545SHajimu UMEMOTO 		 *
121933841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
122033841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
122133841545SHajimu UMEMOTO 		 * SYN in this case.
122233841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
122333841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
122433841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
122533841545SHajimu UMEMOTO 		 *    used"
122633841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
122733841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
122833841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
122933841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
123033841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
123133841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
123233841545SHajimu UMEMOTO 		 *
123333841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
123433841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
123533841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
123633841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
123733841545SHajimu UMEMOTO 		 */
1238603724d3SBjoern A. Zeeb 		if (isipv6 && !V_ip6_use_deprecated) {
123933841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
124033841545SHajimu UMEMOTO 
12418c0fec80SRobert Watson 			ia6 = ip6_getdstifaddr(m);
12428c0fec80SRobert Watson 			if (ia6 != NULL &&
124333841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
12448c0fec80SRobert Watson 				ifa_free(&ia6->ia_ifa);
1245a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1246a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12478d573cc1SAndre Oppermann 					"Connection attempt to deprecated "
12488d573cc1SAndre Oppermann 					"IPv6 address rejected\n",
1249a160e630SAndre Oppermann 					s, __func__);
125033841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
125133841545SHajimu UMEMOTO 				goto dropwithreset;
125233841545SHajimu UMEMOTO 			}
125377d396fdSMaksim Yevmenkin 			if (ia6)
12548c0fec80SRobert Watson 				ifa_free(&ia6->ia_ifa);
125533841545SHajimu UMEMOTO 		}
1256b287c6c7SBjoern A. Zeeb #endif /* INET6 */
125765f28919SJesper Skriver 		/*
1258db33b3e6SAndre Oppermann 		 * Basic sanity checks on incoming SYN requests:
12598d573cc1SAndre Oppermann 		 *   Don't respond if the destination is a link layer
1260db33b3e6SAndre Oppermann 		 *	broadcast according to RFC1122 4.2.3.10, p. 104.
12618d573cc1SAndre Oppermann 		 *   If it is from this socket it must be forged.
12628d573cc1SAndre Oppermann 		 *   Don't respond if the source or destination is a
12638d573cc1SAndre Oppermann 		 *	global or subnet broad- or multicast address.
126493ec91baSCrist J. Clark 		 *   Note that it is quite possible to receive unicast
126593ec91baSCrist J. Clark 		 *	link-layer packets with a broadcast IP address. Use
126693ec91baSCrist J. Clark 		 *	in_broadcast() to find them.
1267be2ac88cSJonathan Lemon 		 */
12688d573cc1SAndre Oppermann 		if (m->m_flags & (M_BCAST|M_MCAST)) {
12698d573cc1SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
12708d573cc1SAndre Oppermann 			    log(LOG_DEBUG, "%s; %s: Listen socket: "
12718d573cc1SAndre Oppermann 				"Connection attempt from broad- or multicast "
1272773673c1SAndre Oppermann 				"link layer address ignored\n", s, __func__);
1273302ce8d6SAndre Oppermann 			goto dropunlock;
12748d573cc1SAndre Oppermann 		}
1275db33b3e6SAndre Oppermann #ifdef INET6
1276b287c6c7SBjoern A. Zeeb 		if (isipv6) {
1277db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1278a160e630SAndre Oppermann 			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) {
1279a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1280a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12818d573cc1SAndre Oppermann 					"Connection attempt to/from self "
1282773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1283302ce8d6SAndre Oppermann 				goto dropunlock;
1284a160e630SAndre Oppermann 			}
1285be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1286a160e630SAndre Oppermann 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
1287a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1288a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12898d573cc1SAndre Oppermann 					"Connection attempt from/to multicast "
1290773673c1SAndre Oppermann 					"address ignored\n", s, __func__);
1291302ce8d6SAndre Oppermann 				goto dropunlock;
1292a160e630SAndre Oppermann 			}
1293b287c6c7SBjoern A. Zeeb 		}
1294db33b3e6SAndre Oppermann #endif
1295b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
1296b287c6c7SBjoern A. Zeeb 		else
1297b287c6c7SBjoern A. Zeeb #endif
1298b287c6c7SBjoern A. Zeeb #ifdef INET
1299b287c6c7SBjoern A. Zeeb 		{
1300db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1301a160e630SAndre Oppermann 			    ip->ip_dst.s_addr == ip->ip_src.s_addr) {
1302a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1303a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13048d573cc1SAndre Oppermann 					"Connection attempt from/to self "
1305773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1306302ce8d6SAndre Oppermann 				goto dropunlock;
1307a160e630SAndre Oppermann 			}
1308be2ac88cSJonathan Lemon 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
1309be2ac88cSJonathan Lemon 			    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
13102ca2159fSCrist J. Clark 			    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
1311a160e630SAndre Oppermann 			    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1312a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1313a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13148d573cc1SAndre Oppermann 					"Connection attempt from/to broad- "
1315773673c1SAndre Oppermann 					"or multicast address ignored\n",
1316a160e630SAndre Oppermann 					s, __func__);
1317302ce8d6SAndre Oppermann 				goto dropunlock;
1318c068736aSJeffrey Hsu 			}
1319a160e630SAndre Oppermann 		}
1320b287c6c7SBjoern A. Zeeb #endif
1321be2ac88cSJonathan Lemon 		/*
1322db33b3e6SAndre Oppermann 		 * SYN appears to be valid.  Create compressed TCP state
1323db33b3e6SAndre Oppermann 		 * for syncache.
1324be2ac88cSJonathan Lemon 		 */
13253c653157SHartmut Brandt #ifdef TCPDEBUG
13263c653157SHartmut Brandt 		if (so->so_options & SO_DEBUG)
13273c653157SHartmut Brandt 			tcp_trace(TA_INPUT, ostate, tp,
13283c653157SHartmut Brandt 			    (void *)tcp_saveipgen, &tcp_savetcp, 0);
13293c653157SHartmut Brandt #endif
1330f72167f4SAndre Oppermann 		tcp_dooptions(&to, optp, optlen, TO_SYN);
133109fe6320SNavdeep Parhar 		syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL);
1332be2ac88cSJonathan Lemon 		/*
13334d6e7130SAndre Oppermann 		 * Entry added to syncache and mbuf consumed.
1334a160e630SAndre Oppermann 		 * Everything already unlocked by syncache_add().
1335be2ac88cSJonathan Lemon 		 */
1336603724d3SBjoern A. Zeeb 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1337be2ac88cSJonathan Lemon 		return;
1338f76fcf6dSJeffrey Hsu 	}
1339db33b3e6SAndre Oppermann 
13402903309aSAttilio Rao #ifdef TCP_SIGNATURE
13412903309aSAttilio Rao 	if (sig_checked == 0)  {
13422903309aSAttilio Rao 		tcp_dooptions(&to, optp, optlen,
13432903309aSAttilio Rao 		    (thflags & TH_SYN) ? TO_SYN : 0);
13442903309aSAttilio Rao 		if (!tcp_signature_verify_input(m, off0, tlen, optlen, &to,
13452903309aSAttilio Rao 		    th, tp->t_flags)) {
13462903309aSAttilio Rao 
13472903309aSAttilio Rao 			/*
13482903309aSAttilio Rao 			 * In SYN_SENT state if it receives an RST, it is
13492903309aSAttilio Rao 			 * allowed for further processing.
13502903309aSAttilio Rao 			 */
13512903309aSAttilio Rao 			if ((thflags & TH_RST) == 0 ||
13522903309aSAttilio Rao 			    (tp->t_state == TCPS_SYN_SENT) == 0)
13532903309aSAttilio Rao 				goto dropunlock;
13542903309aSAttilio Rao 		}
13552903309aSAttilio Rao 		sig_checked = 1;
13562903309aSAttilio Rao 	}
13572903309aSAttilio Rao #endif
13582903309aSAttilio Rao 
1359302ce8d6SAndre Oppermann 	/*
13608d573cc1SAndre Oppermann 	 * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later
13618d573cc1SAndre Oppermann 	 * state.  tcp_do_segment() always consumes the mbuf chain, unlocks
13628d573cc1SAndre Oppermann 	 * the inpcb, and unlocks pcbinfo.
1363302ce8d6SAndre Oppermann 	 */
1364252ca428SRobert Watson 	tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked);
1365603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1366302ce8d6SAndre Oppermann 	return;
1367be2ac88cSJonathan Lemon 
1368302ce8d6SAndre Oppermann dropwithreset:
1369fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED) {
1370dd8ac7f9SRobert Watson 		INP_INFO_WUNLOCK(&V_tcbinfo);
1371252ca428SRobert Watson 		ti_locked = TI_UNLOCKED;
1372fa046d87SRobert Watson 	}
1373fa046d87SRobert Watson #ifdef INVARIANTS
1374fa046d87SRobert Watson 	else {
1375fa046d87SRobert Watson 		KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropwithreset "
1376fa046d87SRobert Watson 		    "ti_locked: %d", __func__, ti_locked));
1377fa046d87SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1378fa046d87SRobert Watson 	}
1379fa046d87SRobert Watson #endif
1380014ea782SRobert Watson 
1381014ea782SRobert Watson 	if (inp != NULL) {
1382302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
1383014ea782SRobert Watson 		INP_WUNLOCK(inp);
1384dd8ac7f9SRobert Watson 	} else
1385014ea782SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
1386302ce8d6SAndre Oppermann 	m = NULL;	/* mbuf chain got consumed. */
1387014ea782SRobert Watson 	goto drop;
1388014ea782SRobert Watson 
1389302ce8d6SAndre Oppermann dropunlock:
1390fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED) {
1391252ca428SRobert Watson 		INP_INFO_WUNLOCK(&V_tcbinfo);
1392252ca428SRobert Watson 		ti_locked = TI_UNLOCKED;
1393fa046d87SRobert Watson 	}
1394fa046d87SRobert Watson #ifdef INVARIANTS
1395fa046d87SRobert Watson 	else {
1396fa046d87SRobert Watson 		KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropunlock "
1397fa046d87SRobert Watson 		    "ti_locked: %d", __func__, ti_locked));
1398fa046d87SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1399fa046d87SRobert Watson 	}
1400fa046d87SRobert Watson #endif
1401252ca428SRobert Watson 
14029fa198beSAndre Oppermann 	if (inp != NULL)
14038501a69cSRobert Watson 		INP_WUNLOCK(inp);
1404014ea782SRobert Watson 
1405302ce8d6SAndre Oppermann drop:
1406603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1407a160e630SAndre Oppermann 	if (s != NULL)
1408a160e630SAndre Oppermann 		free(s, M_TCPLOG);
1409302ce8d6SAndre Oppermann 	if (m != NULL)
1410302ce8d6SAndre Oppermann 		m_freem(m);
1411302ce8d6SAndre Oppermann }
1412302ce8d6SAndre Oppermann 
1413679d9708SAndre Oppermann static void
1414302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
1415252ca428SRobert Watson     struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos,
1416252ca428SRobert Watson     int ti_locked)
1417302ce8d6SAndre Oppermann {
1418302ce8d6SAndre Oppermann 	int thflags, acked, ourfinisacked, needoutput = 0;
1419302ce8d6SAndre Oppermann 	int rstreason, todrop, win;
1420302ce8d6SAndre Oppermann 	u_long tiwin;
1421302ce8d6SAndre Oppermann 	struct tcpopt to;
1422302ce8d6SAndre Oppermann 
142314739780SMaxim Konovalov #ifdef TCPDEBUG
142414739780SMaxim Konovalov 	/*
142514739780SMaxim Konovalov 	 * The size of tcp_saveipgen must be the size of the max ip header,
142614739780SMaxim Konovalov 	 * now IPv6.
142714739780SMaxim Konovalov 	 */
142814739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
142914739780SMaxim Konovalov 	struct tcphdr tcp_savetcp;
143014739780SMaxim Konovalov 	short ostate = 0;
143114739780SMaxim Konovalov #endif
1432302ce8d6SAndre Oppermann 	thflags = th->th_flags;
1433d64a46eaSLawrence Stewart 	tp->sackhint.last_sack_ack = 0;
1434302ce8d6SAndre Oppermann 
1435252ca428SRobert Watson 	/*
1436252ca428SRobert Watson 	 * If this is either a state-changing packet or current state isn't
1437252ca428SRobert Watson 	 * established, we require a write lock on tcbinfo.  Otherwise, we
14380989f56cSRobert Watson 	 * allow the tcbinfo to be in either alocked or unlocked, as the
14390989f56cSRobert Watson 	 * caller may have unnecessarily acquired a write lock due to a race.
1440252ca428SRobert Watson 	 */
1441252ca428SRobert Watson 	if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 ||
1442252ca428SRobert Watson 	    tp->t_state != TCPS_ESTABLISHED) {
1443252ca428SRobert Watson 		KASSERT(ti_locked == TI_WLOCKED, ("%s ti_locked %d for "
1444252ca428SRobert Watson 		    "SYN/FIN/RST/!EST", __func__, ti_locked));
1445603724d3SBjoern A. Zeeb 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1446252ca428SRobert Watson 	} else {
1447252ca428SRobert Watson #ifdef INVARIANTS
1448fa046d87SRobert Watson 		if (ti_locked == TI_WLOCKED)
1449252ca428SRobert Watson 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1450fa046d87SRobert Watson 		else {
1451fa046d87SRobert Watson 			KASSERT(ti_locked == TI_UNLOCKED, ("%s: EST "
1452fa046d87SRobert Watson 			    "ti_locked: %d", __func__, ti_locked));
1453fa046d87SRobert Watson 			INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1454fa046d87SRobert Watson 		}
1455252ca428SRobert Watson #endif
1456252ca428SRobert Watson 	}
14578501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1458679d9708SAndre Oppermann 	KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
1459679d9708SAndre Oppermann 	    __func__));
1460679d9708SAndre Oppermann 	KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
1461679d9708SAndre Oppermann 	    __func__));
1462df8bae1dSRodney W. Grimes 
1463df8bae1dSRodney W. Grimes 	/*
1464df8bae1dSRodney W. Grimes 	 * Segment received on connection.
1465df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
1466e8949f74SAndre Oppermann 	 * XXX: This should be done after segment
1467e8949f74SAndre Oppermann 	 * validation to ignore broken/spoofed segs.
1468df8bae1dSRodney W. Grimes 	 */
14699b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
14707ff19458SPaul Traina 	if (TCPS_HAVEESTABLISHED(tp->t_state))
14719077f387SGleb Smirnoff 		tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
1472df8bae1dSRodney W. Grimes 
1473df8bae1dSRodney W. Grimes 	/*
1474464fcfbcSAndre Oppermann 	 * Unscale the window into a 32-bit value.
1475e8949f74SAndre Oppermann 	 * For the SYN_SENT state the scale is zero.
1476464fcfbcSAndre Oppermann 	 */
1477464fcfbcSAndre Oppermann 	tiwin = th->th_win << tp->snd_scale;
1478464fcfbcSAndre Oppermann 
1479464fcfbcSAndre Oppermann 	/*
1480f2512ba1SRui Paulo 	 * TCP ECN processing.
1481f2512ba1SRui Paulo 	 */
1482f2512ba1SRui Paulo 	if (tp->t_flags & TF_ECN_PERMIT) {
14839c251892SRui Paulo 		if (thflags & TH_CWR)
14849c251892SRui Paulo 			tp->t_flags &= ~TF_ECN_SND_ECE;
1485f2512ba1SRui Paulo 		switch (iptos & IPTOS_ECN_MASK) {
1486f2512ba1SRui Paulo 		case IPTOS_ECN_CE:
1487f2512ba1SRui Paulo 			tp->t_flags |= TF_ECN_SND_ECE;
148878b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ce);
1489f2512ba1SRui Paulo 			break;
1490f2512ba1SRui Paulo 		case IPTOS_ECN_ECT0:
149178b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect0);
1492f2512ba1SRui Paulo 			break;
1493f2512ba1SRui Paulo 		case IPTOS_ECN_ECT1:
149478b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect1);
1495f2512ba1SRui Paulo 			break;
1496f2512ba1SRui Paulo 		}
1497dbc42409SLawrence Stewart 		/* Congestion experienced. */
1498dbc42409SLawrence Stewart 		if (thflags & TH_ECE) {
1499dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_ECN);
1500f2512ba1SRui Paulo 		}
1501f2512ba1SRui Paulo 	}
1502f2512ba1SRui Paulo 
1503f2512ba1SRui Paulo 	/*
1504f72167f4SAndre Oppermann 	 * Parse options on any incoming segment.
1505f72167f4SAndre Oppermann 	 */
1506302ce8d6SAndre Oppermann 	tcp_dooptions(&to, (u_char *)(th + 1),
1507302ce8d6SAndre Oppermann 	    (th->th_off << 2) - sizeof(struct tcphdr),
1508302ce8d6SAndre Oppermann 	    (thflags & TH_SYN) ? TO_SYN : 0);
1509f72167f4SAndre Oppermann 
1510f72167f4SAndre Oppermann 	/*
1511f72167f4SAndre Oppermann 	 * If echoed timestamp is later than the current time,
1512bf6d304aSAndre Oppermann 	 * fall back to non RFC1323 RTT calculation.  Normalize
1513bf6d304aSAndre Oppermann 	 * timestamp if syncookies were used when this connection
1514bf6d304aSAndre Oppermann 	 * was established.
1515f72167f4SAndre Oppermann 	 */
1516bf6d304aSAndre Oppermann 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
1517e16fa5caSJohn-Mark Gurney 		to.to_tsecr -= tp->ts_offset;
1518d8951c8aSBjoern A. Zeeb 		if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks()))
1519f72167f4SAndre Oppermann 			to.to_tsecr = 0;
1520bf6d304aSAndre Oppermann 	}
1521f72167f4SAndre Oppermann 
1522f72167f4SAndre Oppermann 	/*
152397d8d152SAndre Oppermann 	 * Process options only when we get SYN/ACK back. The SYN case
152497d8d152SAndre Oppermann 	 * for incoming connections is handled in tcp_syncache.
15255396d0f8SAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
15265396d0f8SAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.
152797d8d152SAndre Oppermann 	 * XXX this is traditional behavior, may need to be cleaned up.
1528df8bae1dSRodney W. Grimes 	 */
15290a389eabSSimon L. B. Nielsen 	if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
1530464fcfbcSAndre Oppermann 		if ((to.to_flags & TOF_SCALE) &&
1531464fcfbcSAndre Oppermann 		    (tp->t_flags & TF_REQ_SCALE)) {
1532be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
153302a1a643SAndre Oppermann 			tp->snd_scale = to.to_wscale;
1534be2ac88cSJonathan Lemon 		}
15355396d0f8SAndre Oppermann 		/*
15365396d0f8SAndre Oppermann 		 * Initial send window.  It will be updated with
15375396d0f8SAndre Oppermann 		 * the next incoming segment to the scaled value.
15385396d0f8SAndre Oppermann 		 */
15395396d0f8SAndre Oppermann 		tp->snd_wnd = th->th_win;
1540be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_TS) {
1541be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
1542be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
1543d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1544be2ac88cSJonathan Lemon 		}
1545be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_MSS)
1546be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
15473529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
15483529149eSAndre Oppermann 		    (to.to_flags & TOF_SACKPERM) == 0)
15493529149eSAndre Oppermann 			tp->t_flags &= ~TF_SACK_PERMIT;
15506d90faf3SPaul Saab 	}
15516d90faf3SPaul Saab 
1552df8bae1dSRodney W. Grimes 	/*
1553df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
1554df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
1555df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
1556df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
1557df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
1558df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
1559df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
1560df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
1561df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
1562df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
1563df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
1564df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
1565a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
1566c5ad39b9SAndre Oppermann 	 * Since we check for TCPS_ESTABLISHED first, it can only
1567a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
1568df8bae1dSRodney W. Grimes 	 */
1569df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
1570c5ad39b9SAndre Oppermann 	    th->th_seq == tp->rcv_nxt &&
1571fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1572c5ad39b9SAndre Oppermann 	    tp->snd_nxt == tp->snd_max &&
1573c5ad39b9SAndre Oppermann 	    tiwin && tiwin == tp->snd_wnd &&
1574a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
1575c5ad39b9SAndre Oppermann 	    LIST_EMPTY(&tp->t_segq) &&
1576be2ac88cSJonathan Lemon 	    ((to.to_flags & TOF_TS) == 0 ||
1577c5ad39b9SAndre Oppermann 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) {
1578df8bae1dSRodney W. Grimes 
1579df8bae1dSRodney W. Grimes 		/*
1580df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
1581df8bae1dSRodney W. Grimes 		 * record the timestamp.
1582a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
1583a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1584df8bae1dSRodney W. Grimes 		 */
1585be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
1586fb59c426SYoshinobu Inoue 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1587d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1588a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
1589df8bae1dSRodney W. Grimes 		}
1590df8bae1dSRodney W. Grimes 
1591fb59c426SYoshinobu Inoue 		if (tlen == 0) {
1592fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
1593fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
1594dbc42409SLawrence Stewart 			    !IN_RECOVERY(tp->t_flags) &&
1595fc30a251SAndre Oppermann 			    (to.to_flags & TOF_SACK) == 0 &&
1596dbc42409SLawrence Stewart 			    TAILQ_EMPTY(&tp->snd_holes)) {
1597df8bae1dSRodney W. Grimes 				/*
1598e8949f74SAndre Oppermann 				 * This is a pure ack for outstanding data.
1599df8bae1dSRodney W. Grimes 				 */
1600fa046d87SRobert Watson 				if (ti_locked == TI_WLOCKED)
1601252ca428SRobert Watson 					INP_INFO_WUNLOCK(&V_tcbinfo);
1602252ca428SRobert Watson 				ti_locked = TI_UNLOCKED;
1603252ca428SRobert Watson 
160478b50714SRobert Watson 				TCPSTAT_INC(tcps_predack);
1605252ca428SRobert Watson 
16069b8b58e0SJonathan Lemon 				/*
1607e8949f74SAndre Oppermann 				 * "bad retransmit" recovery.
16089b8b58e0SJonathan Lemon 				 */
16099b8b58e0SJonathan Lemon 				if (tp->t_rxtshift == 1 &&
1610672dc4aeSJohn Baldwin 				    tp->t_flags & TF_PREVVALID &&
16116dfb8b31SJohn Baldwin 				    (int)(ticks - tp->t_badrxtwin) < 0) {
1612dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_RTO_ERR);
16139b8b58e0SJonathan Lemon 				}
1614fa55172bSMatthew Dillon 
1615fa55172bSMatthew Dillon 				/*
1616fa55172bSMatthew Dillon 				 * Recalculate the transmit timer / rtt.
1617fa55172bSMatthew Dillon 				 *
1618fa55172bSMatthew Dillon 				 * Some boxes send broken timestamp replies
1619fa55172bSMatthew Dillon 				 * during the SYN+ACK phase, ignore
1620fa55172bSMatthew Dillon 				 * timestamps of 0 or we could calculate a
1621fa55172bSMatthew Dillon 				 * huge RTT and blow up the retransmit timer.
1622fa55172bSMatthew Dillon 				 */
1623fa55172bSMatthew Dillon 				if ((to.to_flags & TOF_TS) != 0 &&
1624fa55172bSMatthew Dillon 				    to.to_tsecr) {
1625d8951c8aSBjoern A. Zeeb 					u_int t;
1626d8951c8aSBjoern A. Zeeb 
1627d8951c8aSBjoern A. Zeeb 					t = tcp_ts_getticks() - to.to_tsecr;
1628d8951c8aSBjoern A. Zeeb 					if (!tp->t_rttlow || tp->t_rttlow > t)
1629d8951c8aSBjoern A. Zeeb 						tp->t_rttlow = t;
1630a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
1631d8951c8aSBjoern A. Zeeb 					    TCP_TS_TO_TICKS(t) + 1);
1632fa55172bSMatthew Dillon 				} else if (tp->t_rtttime &&
1633fa55172bSMatthew Dillon 				    SEQ_GT(th->th_ack, tp->t_rtseq)) {
1634eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1635eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - tp->t_rtttime)
1636eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - tp->t_rtttime;
1637c068736aSJeffrey Hsu 					tcp_xmit_timer(tp,
1638c068736aSJeffrey Hsu 							ticks - tp->t_rtttime);
1639fa55172bSMatthew Dillon 				}
1640dbc42409SLawrence Stewart 				acked = BYTES_THIS_ACK(tp, th);
164139bc9de5SLawrence Stewart 
164239bc9de5SLawrence Stewart 				/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
164339bc9de5SLawrence Stewart 				hhook_run_tcp_est_in(tp, th, &to);
164439bc9de5SLawrence Stewart 
164578b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvackpack);
164678b50714SRobert Watson 				TCPSTAT_ADD(tcps_rcvackbyte, acked);
1647df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
16489d11646dSJeffrey Hsu 				if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
16499d11646dSJeffrey Hsu 				    SEQ_LEQ(th->th_ack, tp->snd_recover))
16509d11646dSJeffrey Hsu 					tp->snd_recover = th->th_ack - 1;
1651dbc42409SLawrence Stewart 
1652dbc42409SLawrence Stewart 				/*
1653dbc42409SLawrence Stewart 				 * Let the congestion control algorithm update
1654dbc42409SLawrence Stewart 				 * congestion control related information. This
1655dbc42409SLawrence Stewart 				 * typically means increasing the congestion
1656dbc42409SLawrence Stewart 				 * window.
1657dbc42409SLawrence Stewart 				 */
1658dbc42409SLawrence Stewart 				cc_ack_received(tp, th, CC_ACK);
1659dbc42409SLawrence Stewart 
16609d11646dSJeffrey Hsu 				tp->snd_una = th->th_ack;
16611645d090SJeff Roberson 				/*
1662e8949f74SAndre Oppermann 				 * Pull snd_wl2 up to prevent seq wrap relative
16631645d090SJeff Roberson 				 * to th_ack.
16641645d090SJeff Roberson 				 */
1665cb942153SJeffrey Hsu 				tp->snd_wl2 = th->th_ack;
1666b5addd85SJeffrey Hsu 				tp->t_dupacks = 0;
1667df8bae1dSRodney W. Grimes 				m_freem(m);
1668e8949f74SAndre Oppermann 				ND6_HINT(tp); /* Some progress has been made. */
1669df8bae1dSRodney W. Grimes 
1670df8bae1dSRodney W. Grimes 				/*
1671df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
1672df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
1673df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
1674df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
1675df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
1676df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
1677df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
1678e8949f74SAndre Oppermann 				 */
16793c653157SHartmut Brandt #ifdef TCPDEBUG
16803c653157SHartmut Brandt 				if (so->so_options & SO_DEBUG)
16813c653157SHartmut Brandt 					tcp_trace(TA_INPUT, ostate, tp,
16823c653157SHartmut Brandt 					    (void *)tcp_saveipgen,
16833c653157SHartmut Brandt 					    &tcp_savetcp, 0);
16843c653157SHartmut Brandt #endif
1685df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
1686b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
1687b8152ba7SAndre Oppermann 				else if (!tcp_timer_active(tp, TT_PERSIST))
1688b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT,
1689b8152ba7SAndre Oppermann 						      tp->t_rxtcur);
1690df8bae1dSRodney W. Grimes 				sowwakeup(so);
1691df8bae1dSRodney W. Grimes 				if (so->so_snd.sb_cc)
1692df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1693a14c749fSJonathan Lemon 				goto check_delack;
1694df8bae1dSRodney W. Grimes 			}
1695fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1696fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
16976741ecf5SAndre Oppermann 			int newsize = 0;	/* automatic sockbuf scaling */
16986741ecf5SAndre Oppermann 
1699df8bae1dSRodney W. Grimes 			/*
1700252ca428SRobert Watson 			 * This is a pure, in-sequence data packet with
1701252ca428SRobert Watson 			 * nothing on the reassembly queue and we have enough
1702252ca428SRobert Watson 			 * buffer space to take it.
1703df8bae1dSRodney W. Grimes 			 */
1704fa046d87SRobert Watson 			if (ti_locked == TI_WLOCKED)
1705252ca428SRobert Watson 				INP_INFO_WUNLOCK(&V_tcbinfo);
1706252ca428SRobert Watson 			ti_locked = TI_UNLOCKED;
1707252ca428SRobert Watson 
17086d90faf3SPaul Saab 			/* Clean receiver SACK report if present */
17093529149eSAndre Oppermann 			if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks)
17106d90faf3SPaul Saab 				tcp_clean_sackreport(tp);
171178b50714SRobert Watson 			TCPSTAT_INC(tcps_preddat);
1712fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
17131645d090SJeff Roberson 			/*
17141645d090SJeff Roberson 			 * Pull snd_wl1 up to prevent seq wrap relative to
17151645d090SJeff Roberson 			 * th_seq.
17161645d090SJeff Roberson 			 */
17171645d090SJeff Roberson 			tp->snd_wl1 = th->th_seq;
17181645d090SJeff Roberson 			/*
17191645d090SJeff Roberson 			 * Pull rcv_up up to prevent seq wrap relative to
17201645d090SJeff Roberson 			 * rcv_nxt.
17211645d090SJeff Roberson 			 */
17221645d090SJeff Roberson 			tp->rcv_up = tp->rcv_nxt;
172378b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpack);
172478b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
1725e8949f74SAndre Oppermann 			ND6_HINT(tp);	/* Some progress has been made */
17263c653157SHartmut Brandt #ifdef TCPDEBUG
17273c653157SHartmut Brandt 			if (so->so_options & SO_DEBUG)
17283c653157SHartmut Brandt 				tcp_trace(TA_INPUT, ostate, tp,
17293c653157SHartmut Brandt 				    (void *)tcp_saveipgen, &tcp_savetcp, 0);
17303c653157SHartmut Brandt #endif
17316741ecf5SAndre Oppermann 		/*
17326741ecf5SAndre Oppermann 		 * Automatic sizing of receive socket buffer.  Often the send
17336741ecf5SAndre Oppermann 		 * buffer size is not optimally adjusted to the actual network
17346741ecf5SAndre Oppermann 		 * conditions at hand (delay bandwidth product).  Setting the
17356741ecf5SAndre Oppermann 		 * buffer size too small limits throughput on links with high
17366741ecf5SAndre Oppermann 		 * bandwidth and high delay (eg. trans-continental/oceanic links).
17376741ecf5SAndre Oppermann 		 *
17386741ecf5SAndre Oppermann 		 * On the receive side the socket buffer memory is only rarely
17396741ecf5SAndre Oppermann 		 * used to any significant extent.  This allows us to be much
17406741ecf5SAndre Oppermann 		 * more aggressive in scaling the receive socket buffer.  For
17416741ecf5SAndre Oppermann 		 * the case that the buffer space is actually used to a large
17426741ecf5SAndre Oppermann 		 * extent and we run out of kernel memory we can simply drop
17436741ecf5SAndre Oppermann 		 * the new segments; TCP on the sender will just retransmit it
17446741ecf5SAndre Oppermann 		 * later.  Setting the buffer size too big may only consume too
17456741ecf5SAndre Oppermann 		 * much kernel memory if the application doesn't read() from
17466741ecf5SAndre Oppermann 		 * the socket or packet loss or reordering makes use of the
17476741ecf5SAndre Oppermann 		 * reassembly queue.
17486741ecf5SAndre Oppermann 		 *
17496741ecf5SAndre Oppermann 		 * The criteria to step up the receive buffer one notch are:
17506741ecf5SAndre Oppermann 		 *  1. the number of bytes received during the time it takes
17516741ecf5SAndre Oppermann 		 *     one timestamp to be reflected back to us (the RTT);
17526741ecf5SAndre Oppermann 		 *  2. received bytes per RTT is within seven eighth of the
17536741ecf5SAndre Oppermann 		 *     current socket buffer size;
17546741ecf5SAndre Oppermann 		 *  3. receive buffer size has not hit maximal automatic size;
17556741ecf5SAndre Oppermann 		 *
17566741ecf5SAndre Oppermann 		 * This algorithm does one step per RTT at most and only if
17576741ecf5SAndre Oppermann 		 * we receive a bulk stream w/o packet losses or reorderings.
17586741ecf5SAndre Oppermann 		 * Shrinking the buffer during idle times is not necessary as
17596741ecf5SAndre Oppermann 		 * it doesn't consume any memory when idle.
17606741ecf5SAndre Oppermann 		 *
17616741ecf5SAndre Oppermann 		 * TODO: Only step up if the application is actually serving
17626741ecf5SAndre Oppermann 		 * the buffer to better manage the socket buffer resources.
1763df8bae1dSRodney W. Grimes 		 */
1764603724d3SBjoern A. Zeeb 			if (V_tcp_do_autorcvbuf &&
17656741ecf5SAndre Oppermann 			    to.to_tsecr &&
17666741ecf5SAndre Oppermann 			    (so->so_rcv.sb_flags & SB_AUTOSIZE)) {
17678502ec25SAndre Oppermann 				if (TSTMP_GT(to.to_tsecr, tp->rfbuf_ts) &&
17686741ecf5SAndre Oppermann 				    to.to_tsecr - tp->rfbuf_ts < hz) {
17696741ecf5SAndre Oppermann 					if (tp->rfbuf_cnt >
17706741ecf5SAndre Oppermann 					    (so->so_rcv.sb_hiwat / 8 * 7) &&
17716741ecf5SAndre Oppermann 					    so->so_rcv.sb_hiwat <
1772603724d3SBjoern A. Zeeb 					    V_tcp_autorcvbuf_max) {
17736741ecf5SAndre Oppermann 						newsize =
17746741ecf5SAndre Oppermann 						    min(so->so_rcv.sb_hiwat +
1775603724d3SBjoern A. Zeeb 						    V_tcp_autorcvbuf_inc,
1776603724d3SBjoern A. Zeeb 						    V_tcp_autorcvbuf_max);
17776741ecf5SAndre Oppermann 					}
17786741ecf5SAndre Oppermann 					/* Start over with next RTT. */
17796741ecf5SAndre Oppermann 					tp->rfbuf_ts = 0;
17806741ecf5SAndre Oppermann 					tp->rfbuf_cnt = 0;
17816741ecf5SAndre Oppermann 				} else
17826741ecf5SAndre Oppermann 					tp->rfbuf_cnt += tlen;	/* add up */
17836741ecf5SAndre Oppermann 			}
17846741ecf5SAndre Oppermann 
17856741ecf5SAndre Oppermann 			/* Add data to socket buffer. */
17861e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
1787c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1788c1c36a2cSMike Silbersack 				m_freem(m);
1789c1c36a2cSMike Silbersack 			} else {
17906741ecf5SAndre Oppermann 				/*
17916741ecf5SAndre Oppermann 				 * Set new socket buffer size.
17926741ecf5SAndre Oppermann 				 * Give up when limit is reached.
17936741ecf5SAndre Oppermann 				 */
17946741ecf5SAndre Oppermann 				if (newsize)
17956741ecf5SAndre Oppermann 					if (!sbreserve_locked(&so->so_rcv,
17966c8286e4SRobert Watson 					    newsize, so, NULL))
17976741ecf5SAndre Oppermann 						so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1798fb59c426SYoshinobu Inoue 				m_adj(m, drop_hdrlen);	/* delayed header drop */
17991e4d7da7SRobert Watson 				sbappendstream_locked(&so->so_rcv, m);
1800c1c36a2cSMike Silbersack 			}
1801e8949f74SAndre Oppermann 			/* NB: sorwakeup_locked() does an implicit unlock. */
18021e4d7da7SRobert Watson 			sorwakeup_locked(so);
1803d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp)) {
18043bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
1805f498eeeeSDavid Greenman 			} else {
1806e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
1807e612a582SDavid Greenman 				tcp_output(tp);
1808e612a582SDavid Greenman 			}
1809a14c749fSJonathan Lemon 			goto check_delack;
1810df8bae1dSRodney W. Grimes 		}
1811df8bae1dSRodney W. Grimes 	}
1812df8bae1dSRodney W. Grimes 
1813df8bae1dSRodney W. Grimes 	/*
1814df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1815df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1816df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1817df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1818df8bae1dSRodney W. Grimes 	 */
1819df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1820df8bae1dSRodney W. Grimes 	if (win < 0)
1821df8bae1dSRodney W. Grimes 		win = 0;
182266e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1823df8bae1dSRodney W. Grimes 
18246741ecf5SAndre Oppermann 	/* Reset receive buffer auto scaling when not in bulk receive mode. */
18256741ecf5SAndre Oppermann 	tp->rfbuf_ts = 0;
18266741ecf5SAndre Oppermann 	tp->rfbuf_cnt = 0;
18276741ecf5SAndre Oppermann 
1828df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1829df8bae1dSRodney W. Grimes 
1830df8bae1dSRodney W. Grimes 	/*
1831764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1832764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1833764d8cefSBill Fenner 	 */
1834764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1835fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1836fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1837a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1838a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1839a57815efSBosko Milekic 				goto dropwithreset;
1840a57815efSBosko Milekic 		}
1841764d8cefSBill Fenner 		break;
1842764d8cefSBill Fenner 
1843764d8cefSBill Fenner 	/*
1844df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
1845df8bae1dSRodney W. Grimes 	 *	if seg contains an ACK, but not for our SYN, drop the input.
1846df8bae1dSRodney W. Grimes 	 *	if seg contains a RST, then drop the connection.
1847df8bae1dSRodney W. Grimes 	 *	if seg does not contain SYN, then drop it.
1848df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
1849df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
1850df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
1851f2512ba1SRui Paulo 	 *	if seg contains an ECE and ECN support is enabled, the stream
1852f2512ba1SRui Paulo 	 *	    is ECN capable.
1853df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1854df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
1855df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
1856df8bae1dSRodney W. Grimes 	 */
1857df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
1858fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1859fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
1860fb59c426SYoshinobu Inoue 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1861a57815efSBosko Milekic 			rstreason = BANDLIM_UNLIMITED;
1862a0292f23SGarrett Wollman 			goto dropwithreset;
1863a0292f23SGarrett Wollman 		}
18640ca3f933SAndre Oppermann 		if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST))
1865df8bae1dSRodney W. Grimes 			tp = tcp_drop(tp, ECONNREFUSED);
18660ca3f933SAndre Oppermann 		if (thflags & TH_RST)
1867df8bae1dSRodney W. Grimes 			goto drop;
18680ca3f933SAndre Oppermann 		if (!(thflags & TH_SYN))
1869df8bae1dSRodney W. Grimes 			goto drop;
1870464fcfbcSAndre Oppermann 
1871fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
1872df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
1873fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
187478b50714SRobert Watson 			TCPSTAT_INC(tcps_connects);
1875845799c1SAndras Olah 			soisconnected(so);
1876c488362eSRobert Watson #ifdef MAC
187730d239bcSRobert Watson 			mac_socketpeer_set_from_mbuf(m, so);
1878c488362eSRobert Watson #endif
1879845799c1SAndras Olah 			/* Do window scaling on this connection? */
1880845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1881845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1882845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
1883845799c1SAndras Olah 			}
1884766282cbSJohn Baldwin 			tp->rcv_adv += imin(tp->rcv_wnd,
1885766282cbSJohn Baldwin 			    TCP_MAXWIN << tp->rcv_scale);
1886a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
1887a0292f23SGarrett Wollman 			/*
1888a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
1889a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
1890a0292f23SGarrett Wollman 			 */
1891d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp) && tlen != 0)
1892b8152ba7SAndre Oppermann 				tcp_timer_activate(tp, TT_DELACK,
1893b8152ba7SAndre Oppermann 				    tcp_delacktime);
1894a0292f23SGarrett Wollman 			else
1895a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
1896f2512ba1SRui Paulo 
1897603724d3SBjoern A. Zeeb 			if ((thflags & TH_ECE) && V_tcp_do_ecn) {
1898f2512ba1SRui Paulo 				tp->t_flags |= TF_ECN_PERMIT;
189978b50714SRobert Watson 				TCPSTAT_INC(tcps_ecn_shs);
1900f2512ba1SRui Paulo 			}
1901f2512ba1SRui Paulo 
1902a0292f23SGarrett Wollman 			/*
1903a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
1904a0292f23SGarrett Wollman 			 * Transitions:
1905a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
1906a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
1907a0292f23SGarrett Wollman 			 */
19089b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
1909a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
1910a0292f23SGarrett Wollman 				tp->t_state = TCPS_FIN_WAIT_1;
1911a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
1912fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
19137ff19458SPaul Traina 			} else {
1914a0292f23SGarrett Wollman 				tp->t_state = TCPS_ESTABLISHED;
1915dbc42409SLawrence Stewart 				cc_conn_init(tp);
19169077f387SGleb Smirnoff 				tcp_timer_activate(tp, TT_KEEP,
19179077f387SGleb Smirnoff 				    TP_KEEPIDLE(tp));
19187ff19458SPaul Traina 			}
1919a0292f23SGarrett Wollman 		} else {
1920a0292f23SGarrett Wollman 			/*
1921c068736aSJeffrey Hsu 			 * Received initial SYN in SYN-SENT[*] state =>
1922c068736aSJeffrey Hsu 			 * simultaneous open.  If segment contains CC option
1923c068736aSJeffrey Hsu 			 * and there is a cached CC, apply TAO test.
1924c068736aSJeffrey Hsu 			 * If it succeeds, connection is * half-synchronized.
1925c068736aSJeffrey Hsu 			 * Otherwise, do 3-way handshake:
1926a0292f23SGarrett Wollman 			 *        SYN-SENT -> SYN-RECEIVED
1927a0292f23SGarrett Wollman 			 *        SYN-SENT* -> SYN-RECEIVED*
1928a0292f23SGarrett Wollman 			 * If there was no CC option, clear cached CC value.
1929a0292f23SGarrett Wollman 			 */
19304b8e98d6SQing Li 			tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
1931b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
1932df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_SYN_RECEIVED;
1933a0292f23SGarrett Wollman 		}
1934df8bae1dSRodney W. Grimes 
1935252ca428SRobert Watson 		KASSERT(ti_locked == TI_WLOCKED, ("%s: trimthenstep6: "
1936252ca428SRobert Watson 		    "ti_locked %d", __func__, ti_locked));
1937252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
19388501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
19397cfc6904SRobert Watson 
1940df8bae1dSRodney W. Grimes 		/*
1941fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
1942df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
1943df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
1944df8bae1dSRodney W. Grimes 		 */
1945fb59c426SYoshinobu Inoue 		th->th_seq++;
1946fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
1947fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
1948df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
1949fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
1950fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
195178b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpackafterwin);
195278b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
1953df8bae1dSRodney W. Grimes 		}
1954fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
1955fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
1956a0292f23SGarrett Wollman 		/*
1957a0292f23SGarrett Wollman 		 * Client side of transaction: already sent SYN and data.
1958a0292f23SGarrett Wollman 		 * If the remote host used T/TCP to validate the SYN,
1959a0292f23SGarrett Wollman 		 * our data will be ACK'd; if so, enter normal data segment
1960a0292f23SGarrett Wollman 		 * processing in the middle of step 5, ack processing.
1961a0292f23SGarrett Wollman 		 * Otherwise, goto step 6.
1962a0292f23SGarrett Wollman 		 */
1963fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK)
1964a0292f23SGarrett Wollman 			goto process_ACK;
1965c068736aSJeffrey Hsu 
1966df8bae1dSRodney W. Grimes 		goto step6;
1967c068736aSJeffrey Hsu 
1968a0292f23SGarrett Wollman 	/*
1969a0292f23SGarrett Wollman 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
1970c94c54e4SAndre Oppermann 	 *      do normal processing.
1971a0292f23SGarrett Wollman 	 *
1972c94c54e4SAndre Oppermann 	 * NB: Leftover from RFC1644 T/TCP.  Cases to be reused later.
1973a0292f23SGarrett Wollman 	 */
1974a0292f23SGarrett Wollman 	case TCPS_LAST_ACK:
1975a0292f23SGarrett Wollman 	case TCPS_CLOSING:
1976a0292f23SGarrett Wollman 		break;  /* continue normal processing */
1977df8bae1dSRodney W. Grimes 	}
1978df8bae1dSRodney W. Grimes 
1979df8bae1dSRodney W. Grimes 	/*
1980df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
198180ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
198280ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
198380ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
198480ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
198580ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
198680ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
198780ab7c0eSGarrett Wollman 	 * killing a connection).
198880ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
1989a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
1990df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
1991df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
1992df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
1993df8bae1dSRodney W. Grimes 	 *
199480ab7c0eSGarrett Wollman 	 *
199580ab7c0eSGarrett Wollman 	 * If the RST bit is set, check the sequence number to see
199680ab7c0eSGarrett Wollman 	 * if this is a valid reset segment.
199780ab7c0eSGarrett Wollman 	 * RFC 793 page 37:
199880ab7c0eSGarrett Wollman 	 *   In all states except SYN-SENT, all reset (RST) segments
199980ab7c0eSGarrett Wollman 	 *   are validated by checking their SEQ-fields.  A reset is
200080ab7c0eSGarrett Wollman 	 *   valid if its sequence number is in the window.
200180ab7c0eSGarrett Wollman 	 * Note: this does not take into account delayed ACKs, so
200280ab7c0eSGarrett Wollman 	 *   we should test against last_ack_sent instead of rcv_nxt.
20031a244a61SJonathan Lemon 	 *   The sequence number in the reset segment is normally an
20041a244a61SJonathan Lemon 	 *   echo of our outgoing acknowlegement numbers, but some hosts
20051a244a61SJonathan Lemon 	 *   send a reset with the sequence number at the rightmost edge
20061a244a61SJonathan Lemon 	 *   of our receive window, and we have to handle this case.
200780dd2a81SMike Silbersack 	 * Note 2: Paul Watson's paper "Slipping in the Window" has shown
200880dd2a81SMike Silbersack 	 *   that brute force RST attacks are possible.  To combat this,
200980dd2a81SMike Silbersack 	 *   we use a much stricter check while in the ESTABLISHED state,
201080dd2a81SMike Silbersack 	 *   only accepting RSTs where the sequence number is equal to
201180dd2a81SMike Silbersack 	 *   last_ack_sent.  In all other states (the states in which a
201280dd2a81SMike Silbersack 	 *   RST is more likely), the more permissive check is used.
20138e5c87f4SBjoern A. Zeeb 	 * If we have multiple segments in flight, the initial reset
201480ab7c0eSGarrett Wollman 	 * segment sequence numbers will be to the left of last_ack_sent,
201580ab7c0eSGarrett Wollman 	 * but they will eventually catch up.
201680ab7c0eSGarrett Wollman 	 * In any case, it never made sense to trim reset segments to
201780ab7c0eSGarrett Wollman 	 * fit the receive window since RFC 1122 says:
201880ab7c0eSGarrett Wollman 	 *   4.2.2.12  RST Segment: RFC-793 Section 3.4
201980ab7c0eSGarrett Wollman 	 *
202080ab7c0eSGarrett Wollman 	 *    A TCP SHOULD allow a received RST segment to include data.
202180ab7c0eSGarrett Wollman 	 *
202280ab7c0eSGarrett Wollman 	 *    DISCUSSION
202380ab7c0eSGarrett Wollman 	 *         It has been suggested that a RST segment could contain
202480ab7c0eSGarrett Wollman 	 *         ASCII text that encoded and explained the cause of the
202580ab7c0eSGarrett Wollman 	 *         RST.  No standard has yet been established for such
202680ab7c0eSGarrett Wollman 	 *         data.
202780ab7c0eSGarrett Wollman 	 *
202880ab7c0eSGarrett Wollman 	 * If the reset segment passes the sequence number test examine
202980ab7c0eSGarrett Wollman 	 * the state:
203080ab7c0eSGarrett Wollman 	 *    SYN_RECEIVED STATE:
203180ab7c0eSGarrett Wollman 	 *	If passive open, return to LISTEN state.
203280ab7c0eSGarrett Wollman 	 *	If active open, inform user that connection was refused.
2033745bab7fSDima Dorfman 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
203480ab7c0eSGarrett Wollman 	 *	Inform user that connection was reset, and close tcb.
2035e9bd3a37SJonathan M. Bresler 	 *    CLOSING, LAST_ACK STATES:
203680ab7c0eSGarrett Wollman 	 *	Close the tcb.
2037e9bd3a37SJonathan M. Bresler 	 *    TIME_WAIT STATE:
203880ab7c0eSGarrett Wollman 	 *	Drop the segment - see Stevens, vol. 2, p. 964 and
203980ab7c0eSGarrett Wollman 	 *      RFC 1337.
204080ab7c0eSGarrett Wollman 	 */
2041fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
204295ad8418SQing Li 		if (SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) &&
204395ad8418SQing Li 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
204480ab7c0eSGarrett Wollman 			switch (tp->t_state) {
204580ab7c0eSGarrett Wollman 
204680ab7c0eSGarrett Wollman 			case TCPS_SYN_RECEIVED:
204780ab7c0eSGarrett Wollman 				so->so_error = ECONNREFUSED;
204880ab7c0eSGarrett Wollman 				goto close;
204980ab7c0eSGarrett Wollman 
205080ab7c0eSGarrett Wollman 			case TCPS_ESTABLISHED:
2051603724d3SBjoern A. Zeeb 				if (V_tcp_insecure_rst == 0 &&
205295ad8418SQing Li 				    !(SEQ_GEQ(th->th_seq, tp->rcv_nxt - 1) &&
205395ad8418SQing Li 				    SEQ_LEQ(th->th_seq, tp->rcv_nxt + 1)) &&
205495ad8418SQing Li 				    !(SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) &&
205595ad8418SQing Li 				    SEQ_LEQ(th->th_seq, tp->last_ack_sent + 1))) {
205678b50714SRobert Watson 					TCPSTAT_INC(tcps_badrst);
205780dd2a81SMike Silbersack 					goto drop;
205880dd2a81SMike Silbersack 				}
2059564aab1fSAndre Oppermann 				/* FALLTHROUGH */
206080ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_1:
206180ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_2:
206280ab7c0eSGarrett Wollman 			case TCPS_CLOSE_WAIT:
206380ab7c0eSGarrett Wollman 				so->so_error = ECONNRESET;
206480ab7c0eSGarrett Wollman 			close:
2065252ca428SRobert Watson 				KASSERT(ti_locked == TI_WLOCKED,
2066252ca428SRobert Watson 				    ("tcp_do_segment: TH_RST 1 ti_locked %d",
2067252ca428SRobert Watson 				    ti_locked));
2068252ca428SRobert Watson 				INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2069252ca428SRobert Watson 
207080ab7c0eSGarrett Wollman 				tp->t_state = TCPS_CLOSED;
207178b50714SRobert Watson 				TCPSTAT_INC(tcps_drops);
207280ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
207380ab7c0eSGarrett Wollman 				break;
207480ab7c0eSGarrett Wollman 
207580ab7c0eSGarrett Wollman 			case TCPS_CLOSING:
207680ab7c0eSGarrett Wollman 			case TCPS_LAST_ACK:
2077252ca428SRobert Watson 				KASSERT(ti_locked == TI_WLOCKED,
2078252ca428SRobert Watson 				    ("tcp_do_segment: TH_RST 2 ti_locked %d",
2079252ca428SRobert Watson 				    ti_locked));
2080252ca428SRobert Watson 				INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2081252ca428SRobert Watson 
208280ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
208380ab7c0eSGarrett Wollman 				break;
208480ab7c0eSGarrett Wollman 			}
208580ab7c0eSGarrett Wollman 		}
208680ab7c0eSGarrett Wollman 		goto drop;
208780ab7c0eSGarrett Wollman 	}
208880ab7c0eSGarrett Wollman 
208980ab7c0eSGarrett Wollman 	/*
2090df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
2091df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
2092df8bae1dSRodney W. Grimes 	 */
2093be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
209480ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
2095df8bae1dSRodney W. Grimes 
2096df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
2097d8951c8aSBjoern A. Zeeb 		if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
2098df8bae1dSRodney W. Grimes 			/*
2099df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
2100df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
2101df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
2102df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
2103df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
2104df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
2105df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
2106df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
2107df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
2108df8bae1dSRodney W. Grimes 			 */
2109df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
2110df8bae1dSRodney W. Grimes 		} else {
211178b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
211278b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, tlen);
211378b50714SRobert Watson 			TCPSTAT_INC(tcps_pawsdrop);
211407fd333dSMatthew Dillon 			if (tlen)
2115df8bae1dSRodney W. Grimes 				goto dropafterack;
21161ab4789dSMatthew Dillon 			goto drop;
21171ab4789dSMatthew Dillon 		}
2118df8bae1dSRodney W. Grimes 	}
2119df8bae1dSRodney W. Grimes 
2120a0292f23SGarrett Wollman 	/*
212180ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
212280ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
212380ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
212480ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
212580ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
212680ab7c0eSGarrett Wollman 	 */
2127a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
2128a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2129a57815efSBosko Milekic 		goto dropwithreset;
2130a57815efSBosko Milekic 	}
213180ab7c0eSGarrett Wollman 
2132fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
2133df8bae1dSRodney W. Grimes 	if (todrop > 0) {
213481ad7eb0SZachary Loafman 		/*
213581ad7eb0SZachary Loafman 		 * If this is a duplicate SYN for our current connection,
213681ad7eb0SZachary Loafman 		 * advance over it and pretend and it's not a SYN.
213781ad7eb0SZachary Loafman 		 */
213881ad7eb0SZachary Loafman 		if (thflags & TH_SYN && th->th_seq == tp->irs) {
2139fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
2140fb59c426SYoshinobu Inoue 			th->th_seq++;
2141fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
2142fb59c426SYoshinobu Inoue 				th->th_urp--;
2143df8bae1dSRodney W. Grimes 			else
2144fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
2145df8bae1dSRodney W. Grimes 			todrop--;
2146df8bae1dSRodney W. Grimes 		}
2147df8bae1dSRodney W. Grimes 		/*
2148dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
2149df8bae1dSRodney W. Grimes 		 */
2150fb59c426SYoshinobu Inoue 		if (todrop > tlen
2151fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
2152dac20301SGarrett Wollman 			/*
2153dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
2154dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
2155dac20301SGarrett Wollman 			 * of sequence; drop it.
2156dac20301SGarrett Wollman 			 */
2157fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
2158dac20301SGarrett Wollman 
2159df8bae1dSRodney W. Grimes 			/*
2160dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
2161dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
2162df8bae1dSRodney W. Grimes 			 */
2163dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
2164fb59c426SYoshinobu Inoue 			todrop = tlen;
216578b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
216678b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, todrop);
2167df8bae1dSRodney W. Grimes 		} else {
216878b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpartduppack);
216978b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop);
2170df8bae1dSRodney W. Grimes 		}
2171fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
2172fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
2173fb59c426SYoshinobu Inoue 		tlen -= todrop;
2174fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
2175fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
2176df8bae1dSRodney W. Grimes 		else {
2177fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
2178fb59c426SYoshinobu Inoue 			th->th_urp = 0;
2179df8bae1dSRodney W. Grimes 		}
2180df8bae1dSRodney W. Grimes 	}
2181df8bae1dSRodney W. Grimes 
2182df8bae1dSRodney W. Grimes 	/*
2183df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
2184df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
2185df8bae1dSRodney W. Grimes 	 */
2186df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
2187fb59c426SYoshinobu Inoue 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
2188773673c1SAndre Oppermann 		char *s;
2189773673c1SAndre Oppermann 
2190252ca428SRobert Watson 		KASSERT(ti_locked == TI_WLOCKED, ("%s: SS_NOFDEREF && "
2191252ca428SRobert Watson 		    "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked));
2192252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2193252ca428SRobert Watson 
2194773673c1SAndre Oppermann 		if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, NULL))) {
2195e31d8aa3SMike Silbersack 			log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data after socket "
2196773673c1SAndre Oppermann 			    "was closed, sending RST and removing tcpcb\n",
2197e31d8aa3SMike Silbersack 			    s, __func__, tcpstates[tp->t_state], tlen);
2198773673c1SAndre Oppermann 			free(s, M_TCPLOG);
2199773673c1SAndre Oppermann 		}
2200df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
220178b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvafterclose);
2202a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
2203df8bae1dSRodney W. Grimes 		goto dropwithreset;
2204df8bae1dSRodney W. Grimes 	}
2205df8bae1dSRodney W. Grimes 
2206df8bae1dSRodney W. Grimes 	/*
2207df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
2208df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
2209df8bae1dSRodney W. Grimes 	 */
2210fb59c426SYoshinobu Inoue 	todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
2211df8bae1dSRodney W. Grimes 	if (todrop > 0) {
221278b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvpackafterwin);
2213fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
221478b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen);
2215df8bae1dSRodney W. Grimes 			/*
2216df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
2217df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
2218df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
2219df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
2220df8bae1dSRodney W. Grimes 			 * and ack.
2221df8bae1dSRodney W. Grimes 			 */
2222fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
2223df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
222478b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvwinprobe);
2225df8bae1dSRodney W. Grimes 			} else
2226df8bae1dSRodney W. Grimes 				goto dropafterack;
2227df8bae1dSRodney W. Grimes 		} else
222878b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2229df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
2230fb59c426SYoshinobu Inoue 		tlen -= todrop;
2231fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
2232df8bae1dSRodney W. Grimes 	}
2233df8bae1dSRodney W. Grimes 
2234df8bae1dSRodney W. Grimes 	/*
2235df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
2236df8bae1dSRodney W. Grimes 	 * record its timestamp.
2237cf09195bSPaul Saab 	 * NOTE:
2238cf09195bSPaul Saab 	 * 1) That the test incorporates suggestions from the latest
2239a0292f23SGarrett Wollman 	 *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
2240cf09195bSPaul Saab 	 * 2) That updating only on newer timestamps interferes with
2241cf09195bSPaul Saab 	 *    our earlier PAWS tests, so this check should be solely
2242cf09195bSPaul Saab 	 *    predicated on the sequence space of this segment.
2243cf09195bSPaul Saab 	 * 3) That we modify the segment boundary check to be
2244cf09195bSPaul Saab 	 *        Last.ACK.Sent <= SEG.SEQ + SEG.Len
2245cf09195bSPaul Saab 	 *    instead of RFC1323's
2246cf09195bSPaul Saab 	 *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
2247cf09195bSPaul Saab 	 *    This modified check allows us to overcome RFC1323's
2248cf09195bSPaul Saab 	 *    limitations as described in Stevens TCP/IP Illustrated
2249cf09195bSPaul Saab 	 *    Vol. 2 p.869. In such cases, we can still calculate the
2250cf09195bSPaul Saab 	 *    RTT correctly when RCV.NXT == Last.ACK.Sent.
2251df8bae1dSRodney W. Grimes 	 */
2252be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
2253cf09195bSPaul Saab 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
2254cf09195bSPaul Saab 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
2255cf09195bSPaul Saab 		((thflags & (TH_SYN|TH_FIN)) != 0))) {
2256d8951c8aSBjoern A. Zeeb 		tp->ts_recent_age = tcp_ts_getticks();
2257a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
2258df8bae1dSRodney W. Grimes 	}
2259df8bae1dSRodney W. Grimes 
2260df8bae1dSRodney W. Grimes 	/*
2261df8bae1dSRodney W. Grimes 	 * If a SYN is in the window, then this is an
2262df8bae1dSRodney W. Grimes 	 * error and we send an RST and drop the connection.
2263df8bae1dSRodney W. Grimes 	 */
2264fb59c426SYoshinobu Inoue 	if (thflags & TH_SYN) {
2265252ca428SRobert Watson 		KASSERT(ti_locked == TI_WLOCKED,
2266252ca428SRobert Watson 		    ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked));
2267252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2268252ca428SRobert Watson 
2269df8bae1dSRodney W. Grimes 		tp = tcp_drop(tp, ECONNRESET);
2270a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
2271122aad88SAndre Oppermann 		goto drop;
2272df8bae1dSRodney W. Grimes 	}
2273df8bae1dSRodney W. Grimes 
2274a0292f23SGarrett Wollman 	/*
2275a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
2276a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
2277a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
2278a0292f23SGarrett Wollman 	 */
2279fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
2280a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
2281a0292f23SGarrett Wollman 		    (tp->t_flags & TF_NEEDSYN))
2282a0292f23SGarrett Wollman 			goto step6;
22835e1aa279SDavid Malone 		else if (tp->t_flags & TF_ACKNOW)
22845e1aa279SDavid Malone 			goto dropafterack;
2285a0292f23SGarrett Wollman 		else
2286a0292f23SGarrett Wollman 			goto drop;
2287a0292f23SGarrett Wollman 	}
2288df8bae1dSRodney W. Grimes 
2289df8bae1dSRodney W. Grimes 	/*
2290df8bae1dSRodney W. Grimes 	 * Ack processing.
2291df8bae1dSRodney W. Grimes 	 */
2292df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2293df8bae1dSRodney W. Grimes 
2294df8bae1dSRodney W. Grimes 	/*
2295764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
2296764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
2297764d8cefSBill Fenner 	 * The ACK was checked above.
2298df8bae1dSRodney W. Grimes 	 */
2299df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2300a0292f23SGarrett Wollman 
230178b50714SRobert Watson 		TCPSTAT_INC(tcps_connects);
2302df8bae1dSRodney W. Grimes 		soisconnected(so);
2303df8bae1dSRodney W. Grimes 		/* Do window scaling? */
2304df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2305df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2306df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
2307464fcfbcSAndre Oppermann 			tp->snd_wnd = tiwin;
2308df8bae1dSRodney W. Grimes 		}
2309a0292f23SGarrett Wollman 		/*
2310a0292f23SGarrett Wollman 		 * Make transitions:
2311a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
2312a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
2313a0292f23SGarrett Wollman 		 */
23149b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
2315a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDFIN) {
2316a0292f23SGarrett Wollman 			tp->t_state = TCPS_FIN_WAIT_1;
2317a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDFIN;
23187ff19458SPaul Traina 		} else {
2319a0292f23SGarrett Wollman 			tp->t_state = TCPS_ESTABLISHED;
2320dbc42409SLawrence Stewart 			cc_conn_init(tp);
23219077f387SGleb Smirnoff 			tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
23227ff19458SPaul Traina 		}
2323a0292f23SGarrett Wollman 		/*
2324a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
2325a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
2326a0292f23SGarrett Wollman 		 */
2327fb59c426SYoshinobu Inoue 		if (tlen == 0 && (thflags & TH_FIN) == 0)
2328fb59c426SYoshinobu Inoue 			(void) tcp_reass(tp, (struct tcphdr *)0, 0,
2329a0292f23SGarrett Wollman 			    (struct mbuf *)0);
2330fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
233193b0017fSPhilippe Charnier 		/* FALLTHROUGH */
2332df8bae1dSRodney W. Grimes 
2333df8bae1dSRodney W. Grimes 	/*
2334df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2335df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
2336fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
2337fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
2338df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
2339df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
2340df8bae1dSRodney W. Grimes 	 */
2341df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
2342df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
2343df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
2344df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
2345df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
2346df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
23475a53ca16SPaul Saab 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
234878b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvacktoomuch);
23495a53ca16SPaul Saab 			goto dropafterack;
23505a53ca16SPaul Saab 		}
23513529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
2352fc30a251SAndre Oppermann 		    ((to.to_flags & TOF_SACK) ||
2353fc30a251SAndre Oppermann 		     !TAILQ_EMPTY(&tp->snd_holes)))
23545a53ca16SPaul Saab 			tcp_sack_doack(tp, &to, th->th_ack);
235539bc9de5SLawrence Stewart 
235639bc9de5SLawrence Stewart 		/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
235739bc9de5SLawrence Stewart 		hhook_run_tcp_est_in(tp, th, &to);
235839bc9de5SLawrence Stewart 
2359fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
2360fb59c426SYoshinobu Inoue 			if (tlen == 0 && tiwin == tp->snd_wnd) {
236178b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvdupack);
2362df8bae1dSRodney W. Grimes 				/*
2363df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
2364df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
2365df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
2366df8bae1dSRodney W. Grimes 				 * change), the ack is the biggest we've
2367df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
2368df8bae1dSRodney W. Grimes 				 * threshhold of them, assume a packet
2369df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
2370df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
2371df8bae1dSRodney W. Grimes 				 * window so we send only this one
2372df8bae1dSRodney W. Grimes 				 * packet.
2373df8bae1dSRodney W. Grimes 				 *
2374df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
2375df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
2376df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
2377df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
2378df8bae1dSRodney W. Grimes 				 * the new ssthresh).
2379df8bae1dSRodney W. Grimes 				 *
2380df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
2381df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
2382df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
2383df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
2384df8bae1dSRodney W. Grimes 				 * network.
2385f2512ba1SRui Paulo 				 *
2386f2512ba1SRui Paulo 				 * When using TCP ECN, notify the peer that
2387f2512ba1SRui Paulo 				 * we reduced the cwnd.
2388df8bae1dSRodney W. Grimes 				 */
2389b8152ba7SAndre Oppermann 				if (!tcp_timer_active(tp, TT_REXMT) ||
2390fb59c426SYoshinobu Inoue 				    th->th_ack != tp->snd_una)
2391df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
2392cb942153SJeffrey Hsu 				else if (++tp->t_dupacks > tcprexmtthresh ||
2393dbc42409SLawrence Stewart 				     IN_FASTRECOVERY(tp->t_flags)) {
2394dbc42409SLawrence Stewart 					cc_ack_received(tp, th, CC_DUPACK);
23953529149eSAndre Oppermann 					if ((tp->t_flags & TF_SACK_PERMIT) &&
2396dbc42409SLawrence Stewart 					    IN_FASTRECOVERY(tp->t_flags)) {
2397808f11b7SPaul Saab 						int awnd;
239825e6f9edSPaul Saab 
239925e6f9edSPaul Saab 						/*
240025e6f9edSPaul Saab 						 * Compute the amount of data in flight first.
240125e6f9edSPaul Saab 						 * We can inject new data into the pipe iff
240225e6f9edSPaul Saab 						 * we have less than 1/2 the original window's
240325e6f9edSPaul Saab 						 * worth of data in flight.
240425e6f9edSPaul Saab 						 */
2405808f11b7SPaul Saab 						awnd = (tp->snd_nxt - tp->snd_fack) +
24060077b016SPaul Saab 							tp->sackhint.sack_bytes_rexmit;
2407808f11b7SPaul Saab 						if (awnd < tp->snd_ssthresh) {
240825e6f9edSPaul Saab 							tp->snd_cwnd += tp->t_maxseg;
240925e6f9edSPaul Saab 							if (tp->snd_cwnd > tp->snd_ssthresh)
241025e6f9edSPaul Saab 								tp->snd_cwnd = tp->snd_ssthresh;
241125e6f9edSPaul Saab 						}
241225e6f9edSPaul Saab 					} else
241346f58482SJonathan Lemon 						tp->snd_cwnd += tp->t_maxseg;
2414ec03d543SRandall Stewart 					if ((thflags & TH_FIN) &&
2415ec03d543SRandall Stewart 					    (TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
2416ec03d543SRandall Stewart 						/*
2417ec03d543SRandall Stewart 						 * If its a fin we need to process
2418ec03d543SRandall Stewart 						 * it to avoid a race where both
2419ec03d543SRandall Stewart 						 * sides enter FIN-WAIT and send FIN|ACK
2420ec03d543SRandall Stewart 						 * at the same time.
2421ec03d543SRandall Stewart 						 */
2422ec03d543SRandall Stewart 						break;
2423ec03d543SRandall Stewart 					}
242446f58482SJonathan Lemon 					(void) tcp_output(tp);
242546f58482SJonathan Lemon 					goto drop;
2426cb942153SJeffrey Hsu 				} else if (tp->t_dupacks == tcprexmtthresh) {
2427cb942153SJeffrey Hsu 					tcp_seq onxt = tp->snd_nxt;
2428a0445c2eSJayanth Vijayaraghavan 
2429a0445c2eSJayanth Vijayaraghavan 					/*
2430a0445c2eSJayanth Vijayaraghavan 					 * If we're doing sack, check to
2431a0445c2eSJayanth Vijayaraghavan 					 * see if we're already in sack
2432a0445c2eSJayanth Vijayaraghavan 					 * recovery. If we're not doing sack,
2433a0445c2eSJayanth Vijayaraghavan 					 * check to see if we're in newreno
2434a0445c2eSJayanth Vijayaraghavan 					 * recovery.
2435a0445c2eSJayanth Vijayaraghavan 					 */
24363529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
2437dbc42409SLawrence Stewart 						if (IN_FASTRECOVERY(tp->t_flags)) {
2438a0445c2eSJayanth Vijayaraghavan 							tp->t_dupacks = 0;
2439a0445c2eSJayanth Vijayaraghavan 							break;
2440a0445c2eSJayanth Vijayaraghavan 						}
2441dbc42409SLawrence Stewart 					} else {
2442a0445c2eSJayanth Vijayaraghavan 						if (SEQ_LEQ(th->th_ack,
24439d11646dSJeffrey Hsu 						    tp->snd_recover)) {
2444cb942153SJeffrey Hsu 							tp->t_dupacks = 0;
2445cb942153SJeffrey Hsu 							break;
244646f58482SJonathan Lemon 						}
2447a0445c2eSJayanth Vijayaraghavan 					}
2448dbc42409SLawrence Stewart 					/* Congestion signal before ack. */
2449dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_NDUPACK);
2450dbc42409SLawrence Stewart 					cc_ack_received(tp, th, CC_DUPACK);
2451b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
24529b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
24533529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
245478b50714SRobert Watson 						TCPSTAT_INC(
245578b50714SRobert Watson 						    tcps_sack_recovery_episode);
2456a55db2b6SPaul Saab 						tp->sack_newdata = tp->snd_nxt;
24578db456bfSPaul Saab 						tp->snd_cwnd = tp->t_maxseg;
24586d90faf3SPaul Saab 						(void) tcp_output(tp);
24596d90faf3SPaul Saab 						goto drop;
24606d90faf3SPaul Saab 					}
2461fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
2462df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->t_maxseg;
2463ec03d543SRandall Stewart 					if ((thflags & TH_FIN) &&
2464ec03d543SRandall Stewart 					    (TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
2465ec03d543SRandall Stewart 						/*
2466ec03d543SRandall Stewart 						 * If its a fin we need to process
2467ec03d543SRandall Stewart 						 * it to avoid a race where both
2468ec03d543SRandall Stewart 						 * sides enter FIN-WAIT and send FIN|ACK
2469ec03d543SRandall Stewart 						 * at the same time.
2470ec03d543SRandall Stewart 						 */
2471ec03d543SRandall Stewart 						break;
2472ec03d543SRandall Stewart 					}
2473df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
247448d2549cSJeffrey Hsu 					KASSERT(tp->snd_limited <= 2,
2475302ce8d6SAndre Oppermann 					    ("%s: tp->snd_limited too big",
2476302ce8d6SAndre Oppermann 					    __func__));
2477df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
247848d2549cSJeffrey Hsu 					     tp->t_maxseg *
247948d2549cSJeffrey Hsu 					     (tp->t_dupacks - tp->snd_limited);
2480df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
2481df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
2482df8bae1dSRodney W. Grimes 					goto drop;
2483603724d3SBjoern A. Zeeb 				} else if (V_tcp_do_rfc3042) {
2484dbc42409SLawrence Stewart 					cc_ack_received(tp, th, CC_DUPACK);
2485582a954bSJeffrey Hsu 					u_long oldcwnd = tp->snd_cwnd;
248648d2549cSJeffrey Hsu 					tcp_seq oldsndmax = tp->snd_max;
248748d2549cSJeffrey Hsu 					u_int sent;
248889c02376SJeffrey Hsu 
2489582a954bSJeffrey Hsu 					KASSERT(tp->t_dupacks == 1 ||
2490582a954bSJeffrey Hsu 					    tp->t_dupacks == 2,
2491302ce8d6SAndre Oppermann 					    ("%s: dupacks not 1 or 2",
2492302ce8d6SAndre Oppermann 					    __func__));
249361a36e3dSJeffrey Hsu 					if (tp->t_dupacks == 1)
249448d2549cSJeffrey Hsu 						tp->snd_limited = 0;
249561a36e3dSJeffrey Hsu 					tp->snd_cwnd =
249661a36e3dSJeffrey Hsu 					    (tp->snd_nxt - tp->snd_una) +
249761a36e3dSJeffrey Hsu 					    (tp->t_dupacks - tp->snd_limited) *
249861a36e3dSJeffrey Hsu 					    tp->t_maxseg;
2499ec03d543SRandall Stewart 					if ((thflags & TH_FIN) &&
2500ec03d543SRandall Stewart 					    (TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
2501ec03d543SRandall Stewart 						/*
2502ec03d543SRandall Stewart 						 * If its a fin we need to process
2503ec03d543SRandall Stewart 						 * it to avoid a race where both
2504ec03d543SRandall Stewart 						 * sides enter FIN-WAIT and send FIN|ACK
2505ec03d543SRandall Stewart 						 * at the same time.
2506ec03d543SRandall Stewart 						 */
2507ec03d543SRandall Stewart 						break;
2508ec03d543SRandall Stewart 					}
2509582a954bSJeffrey Hsu 					(void) tcp_output(tp);
251048d2549cSJeffrey Hsu 					sent = tp->snd_max - oldsndmax;
251148d2549cSJeffrey Hsu 					if (sent > tp->t_maxseg) {
251289c02376SJeffrey Hsu 						KASSERT((tp->t_dupacks == 2 &&
251389c02376SJeffrey Hsu 						    tp->snd_limited == 0) ||
251489c02376SJeffrey Hsu 						   (sent == tp->t_maxseg + 1 &&
251589c02376SJeffrey Hsu 						    tp->t_flags & TF_SENTFIN),
2516302ce8d6SAndre Oppermann 						    ("%s: sent too much",
2517302ce8d6SAndre Oppermann 						    __func__));
251848d2549cSJeffrey Hsu 						tp->snd_limited = 2;
251948d2549cSJeffrey Hsu 					} else if (sent > 0)
252048d2549cSJeffrey Hsu 						++tp->snd_limited;
2521582a954bSJeffrey Hsu 					tp->snd_cwnd = oldcwnd;
2522582a954bSJeffrey Hsu 					goto drop;
2523df8bae1dSRodney W. Grimes 				}
2524df8bae1dSRodney W. Grimes 			} else
2525df8bae1dSRodney W. Grimes 				tp->t_dupacks = 0;
2526df8bae1dSRodney W. Grimes 			break;
2527df8bae1dSRodney W. Grimes 		}
2528cb942153SJeffrey Hsu 
2529302ce8d6SAndre Oppermann 		KASSERT(SEQ_GT(th->th_ack, tp->snd_una),
2530302ce8d6SAndre Oppermann 		    ("%s: th_ack <= snd_una", __func__));
2531cb942153SJeffrey Hsu 
2532df8bae1dSRodney W. Grimes 		/*
2533df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
2534df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
2535df8bae1dSRodney W. Grimes 		 */
2536dbc42409SLawrence Stewart 		if (IN_FASTRECOVERY(tp->t_flags)) {
2537cb942153SJeffrey Hsu 			if (SEQ_LT(th->th_ack, tp->snd_recover)) {
25383529149eSAndre Oppermann 				if (tp->t_flags & TF_SACK_PERMIT)
25396d90faf3SPaul Saab 					tcp_sack_partialack(tp, th);
25406d90faf3SPaul Saab 				else
2541c068736aSJeffrey Hsu 					tcp_newreno_partial_ack(tp, th);
2542dbc42409SLawrence Stewart 			} else
2543dbc42409SLawrence Stewart 				cc_post_recovery(tp, th);
254446f58482SJonathan Lemon 		}
2545cb942153SJeffrey Hsu 		tp->t_dupacks = 0;
2546a0292f23SGarrett Wollman 		/*
2547a0292f23SGarrett Wollman 		 * If we reach this point, ACK is not a duplicate,
2548a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
2549a0292f23SGarrett Wollman 		 */
2550a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
2551a0292f23SGarrett Wollman 			/*
2552a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
2553a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
255407e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
255507e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
255607e43e10SAndras Olah 			 * we can do window scaling.
2557a0292f23SGarrett Wollman 			 */
2558a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
2559a0292f23SGarrett Wollman 			tp->snd_una++;
256007e43e10SAndras Olah 			/* Do window scaling? */
256107e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
256207e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
256307e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2564464fcfbcSAndre Oppermann 				/* Send window already scaled. */
256507e43e10SAndras Olah 			}
2566a0292f23SGarrett Wollman 		}
2567a0292f23SGarrett Wollman 
2568a0292f23SGarrett Wollman process_ACK:
25698501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
25707cfc6904SRobert Watson 
2571dbc42409SLawrence Stewart 		acked = BYTES_THIS_ACK(tp, th);
257278b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvackpack);
257378b50714SRobert Watson 		TCPSTAT_ADD(tcps_rcvackbyte, acked);
2574df8bae1dSRodney W. Grimes 
2575df8bae1dSRodney W. Grimes 		/*
25769b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
25779b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
25789b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
25799b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
25809b8b58e0SJonathan Lemon 		 * we left off.
25819b8b58e0SJonathan Lemon 		 */
2582672dc4aeSJohn Baldwin 		if (tp->t_rxtshift == 1 && tp->t_flags & TF_PREVVALID &&
2583672dc4aeSJohn Baldwin 		    (int)(ticks - tp->t_badrxtwin) < 0)
2584dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_RTO_ERR);
25859b8b58e0SJonathan Lemon 
25869b8b58e0SJonathan Lemon 		/*
2587df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
2588df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
2589df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
2590df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
2591df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
2592df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
2593df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
2594fa55172bSMatthew Dillon 		 *
2595fa55172bSMatthew Dillon 		 * Some boxes send broken timestamp replies
2596fa55172bSMatthew Dillon 		 * during the SYN+ACK phase, ignore
2597fa55172bSMatthew Dillon 		 * timestamps of 0 or we could calculate a
2598fa55172bSMatthew Dillon 		 * huge RTT and blow up the retransmit timer.
2599df8bae1dSRodney W. Grimes 		 */
2600d8951c8aSBjoern A. Zeeb 		if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) {
2601d8951c8aSBjoern A. Zeeb 			u_int t;
2602d8951c8aSBjoern A. Zeeb 
2603d8951c8aSBjoern A. Zeeb 			t = tcp_ts_getticks() - to.to_tsecr;
2604d8951c8aSBjoern A. Zeeb 			if (!tp->t_rttlow || tp->t_rttlow > t)
2605d8951c8aSBjoern A. Zeeb 				tp->t_rttlow = t;
2606d8951c8aSBjoern A. Zeeb 			tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1);
2607fa55172bSMatthew Dillon 		} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2608eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
2609eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - tp->t_rtttime;
26109b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2611fa55172bSMatthew Dillon 		}
2612df8bae1dSRodney W. Grimes 
2613df8bae1dSRodney W. Grimes 		/*
2614df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
2615df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
2616df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
2617df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
2618df8bae1dSRodney W. Grimes 		 */
2619fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
2620b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
2621df8bae1dSRodney W. Grimes 			needoutput = 1;
2622b8152ba7SAndre Oppermann 		} else if (!tcp_timer_active(tp, TT_PERSIST))
2623b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
2624a0292f23SGarrett Wollman 
2625a0292f23SGarrett Wollman 		/*
2626a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
2627a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
2628a0292f23SGarrett Wollman 		 */
2629a0292f23SGarrett Wollman 		if (acked == 0)
2630a0292f23SGarrett Wollman 			goto step6;
2631a0292f23SGarrett Wollman 
2632df8bae1dSRodney W. Grimes 		/*
2633dbc42409SLawrence Stewart 		 * Let the congestion control algorithm update congestion
2634dbc42409SLawrence Stewart 		 * control related information. This typically means increasing
2635dbc42409SLawrence Stewart 		 * the congestion window.
2636df8bae1dSRodney W. Grimes 		 */
2637dbc42409SLawrence Stewart 		cc_ack_received(tp, th, CC_ACK);
2638dbc42409SLawrence Stewart 
26395905999bSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
2640df8bae1dSRodney W. Grimes 		if (acked > so->so_snd.sb_cc) {
2641df8bae1dSRodney W. Grimes 			tp->snd_wnd -= so->so_snd.sb_cc;
26425905999bSRobert Watson 			sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc);
2643df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
2644df8bae1dSRodney W. Grimes 		} else {
26455905999bSRobert Watson 			sbdrop_locked(&so->so_snd, acked);
2646df8bae1dSRodney W. Grimes 			tp->snd_wnd -= acked;
2647df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
2648df8bae1dSRodney W. Grimes 		}
2649564aab1fSAndre Oppermann 		/* NB: sowwakeup_locked() does an implicit unlock. */
26501e4d7da7SRobert Watson 		sowwakeup_locked(so);
2651e8949f74SAndre Oppermann 		/* Detect una wraparound. */
2652dbc42409SLawrence Stewart 		if (!IN_RECOVERY(tp->t_flags) &&
26539d11646dSJeffrey Hsu 		    SEQ_GT(tp->snd_una, tp->snd_recover) &&
26549d11646dSJeffrey Hsu 		    SEQ_LEQ(th->th_ack, tp->snd_recover))
26559d11646dSJeffrey Hsu 			tp->snd_recover = th->th_ack - 1;
2656dbc42409SLawrence Stewart 		/* XXXLAS: Can this be moved up into cc_post_recovery? */
2657dbc42409SLawrence Stewart 		if (IN_RECOVERY(tp->t_flags) &&
265824cb0f22SLawrence Stewart 		    SEQ_GEQ(th->th_ack, tp->snd_recover)) {
2659dbc42409SLawrence Stewart 			EXIT_RECOVERY(tp->t_flags);
266024cb0f22SLawrence Stewart 		}
2661fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
26623529149eSAndre Oppermann 		if (tp->t_flags & TF_SACK_PERMIT) {
26636d90faf3SPaul Saab 			if (SEQ_GT(tp->snd_una, tp->snd_recover))
26646d90faf3SPaul Saab 				tp->snd_recover = tp->snd_una;
26656d90faf3SPaul Saab 		}
2666df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2667df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
2668df8bae1dSRodney W. Grimes 
2669df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2670df8bae1dSRodney W. Grimes 
2671df8bae1dSRodney W. Grimes 		/*
2672df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
2673df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
2674df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
2675df8bae1dSRodney W. Grimes 		 */
2676df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2677df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2678df8bae1dSRodney W. Grimes 				/*
2679df8bae1dSRodney W. Grimes 				 * If we can't receive any more
2680df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
2681df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
2682df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
2683df8bae1dSRodney W. Grimes 				 * we'll hang forever.
2684e8949f74SAndre Oppermann 				 *
2685e8949f74SAndre Oppermann 				 * XXXjl:
2686340c35deSJonathan Lemon 				 * we should release the tp also, and use a
2687340c35deSJonathan Lemon 				 * compressed state.
2688340c35deSJonathan Lemon 				 */
2689c0b99ffaSRobert Watson 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
269003e49181SSeigo Tanimura 					soisdisconnected(so);
26919077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
26929077f387SGleb Smirnoff 					    (tcp_fast_finwait2_recycle ?
26939077f387SGleb Smirnoff 					    tcp_finwait2_timeout :
26949077f387SGleb Smirnoff 					    TP_MAXIDLE(tp)));
26954cc20ab1SSeigo Tanimura 				}
2696df8bae1dSRodney W. Grimes 				tp->t_state = TCPS_FIN_WAIT_2;
2697df8bae1dSRodney W. Grimes 			}
2698df8bae1dSRodney W. Grimes 			break;
2699df8bae1dSRodney W. Grimes 
2700df8bae1dSRodney W. Grimes 		/*
2701df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
2702df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
2703df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
2704df8bae1dSRodney W. Grimes 		 * the segment.
2705df8bae1dSRodney W. Grimes 		 */
2706df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
2707df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2708252ca428SRobert Watson 				INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
270911a20fb8SJeffrey Hsu 				tcp_twstart(tp);
2710603724d3SBjoern A. Zeeb 				INP_INFO_WUNLOCK(&V_tcbinfo);
2711340c35deSJonathan Lemon 				m_freem(m);
2712679d9708SAndre Oppermann 				return;
2713df8bae1dSRodney W. Grimes 			}
2714df8bae1dSRodney W. Grimes 			break;
2715df8bae1dSRodney W. Grimes 
2716df8bae1dSRodney W. Grimes 		/*
2717df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
2718df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
2719df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
2720df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
2721df8bae1dSRodney W. Grimes 		 */
2722df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
2723df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2724252ca428SRobert Watson 				INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2725df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
2726df8bae1dSRodney W. Grimes 				goto drop;
2727df8bae1dSRodney W. Grimes 			}
2728df8bae1dSRodney W. Grimes 			break;
2729df8bae1dSRodney W. Grimes 		}
2730df8bae1dSRodney W. Grimes 	}
2731df8bae1dSRodney W. Grimes 
2732df8bae1dSRodney W. Grimes step6:
27338501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
27347cfc6904SRobert Watson 
2735df8bae1dSRodney W. Grimes 	/*
2736df8bae1dSRodney W. Grimes 	 * Update window information.
2737df8bae1dSRodney W. Grimes 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2738df8bae1dSRodney W. Grimes 	 */
2739fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) &&
2740fb59c426SYoshinobu Inoue 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
2741fb59c426SYoshinobu Inoue 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
2742fb59c426SYoshinobu Inoue 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
2743df8bae1dSRodney W. Grimes 		/* keep track of pure window updates */
2744fb59c426SYoshinobu Inoue 		if (tlen == 0 &&
2745fb59c426SYoshinobu Inoue 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
274678b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvwinupd);
2747df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
2748fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq;
2749fb59c426SYoshinobu Inoue 		tp->snd_wl2 = th->th_ack;
2750df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
2751df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
2752df8bae1dSRodney W. Grimes 		needoutput = 1;
2753df8bae1dSRodney W. Grimes 	}
2754df8bae1dSRodney W. Grimes 
2755df8bae1dSRodney W. Grimes 	/*
2756df8bae1dSRodney W. Grimes 	 * Process segments with URG.
2757df8bae1dSRodney W. Grimes 	 */
2758fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
2759df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2760df8bae1dSRodney W. Grimes 		/*
2761df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
2762df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
2763df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
2764df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
2765df8bae1dSRodney W. Grimes 		 */
276618ad5842SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
2767fb59c426SYoshinobu Inoue 		if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
2768fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
2769fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
277018ad5842SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);	/* XXX */
2771df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
2772df8bae1dSRodney W. Grimes 		}
2773df8bae1dSRodney W. Grimes 		/*
2774df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
2775df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
2776df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2777df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
2778df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
2779df8bae1dSRodney W. Grimes 		 *
2780df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
2781df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
2782df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
2783df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
2784df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
2785df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
2786df8bae1dSRodney W. Grimes 		 */
2787fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2788fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
2789df8bae1dSRodney W. Grimes 			so->so_oobmark = so->so_rcv.sb_cc +
2790df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
2791927c5ceaSRobert Watson 			if (so->so_oobmark == 0)
2792c0b99ffaSRobert Watson 				so->so_rcv.sb_state |= SBS_RCVATMARK;
2793df8bae1dSRodney W. Grimes 			sohasoutofband(so);
2794df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2795df8bae1dSRodney W. Grimes 		}
279618ad5842SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
2797df8bae1dSRodney W. Grimes 		/*
2798df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
2799df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
2800df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
2801df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
2802df8bae1dSRodney W. Grimes 		 */
280330613f56SJeffrey Hsu 		if (th->th_urp <= (u_long)tlen &&
280430613f56SJeffrey Hsu 		    !(so->so_options & SO_OOBINLINE)) {
280530613f56SJeffrey Hsu 			/* hdr drop is delayed */
280630613f56SJeffrey Hsu 			tcp_pulloutofband(so, th, m, drop_hdrlen);
280730613f56SJeffrey Hsu 		}
2808c068736aSJeffrey Hsu 	} else {
2809df8bae1dSRodney W. Grimes 		/*
2810df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
2811df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
2812df8bae1dSRodney W. Grimes 		 * with the receive window.
2813df8bae1dSRodney W. Grimes 		 */
2814df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2815df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
2816c068736aSJeffrey Hsu 	}
2817df8bae1dSRodney W. Grimes dodata:							/* XXX */
28188501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
28197cfc6904SRobert Watson 
2820df8bae1dSRodney W. Grimes 	/*
2821df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
2822df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
2823df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
2824df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
2825df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
2826df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
2827df8bae1dSRodney W. Grimes 	 */
2828fb59c426SYoshinobu Inoue 	if ((tlen || (thflags & TH_FIN)) &&
2829df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
283069e03620SPaul Saab 		tcp_seq save_start = th->th_seq;
2831fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
2832e4b64281SJesper Skriver 		/*
2833c068736aSJeffrey Hsu 		 * Insert segment which includes th into TCP reassembly queue
2834c068736aSJeffrey Hsu 		 * with control block tp.  Set thflags to whether reassembly now
2835c068736aSJeffrey Hsu 		 * includes a segment with FIN.  This handles the common case
2836c068736aSJeffrey Hsu 		 * inline (segment is the next to be received on an established
2837c068736aSJeffrey Hsu 		 * connection, and the queue is empty), avoiding linkage into
2838c068736aSJeffrey Hsu 		 * and removal from the queue and repetition of various
2839c068736aSJeffrey Hsu 		 * conversions.
2840c068736aSJeffrey Hsu 		 * Set DELACK for segments received in order, but ack
2841c068736aSJeffrey Hsu 		 * immediately when segments are out of order (so
2842c068736aSJeffrey Hsu 		 * fast retransmit can work).
2843e4b64281SJesper Skriver 		 */
2844e4b64281SJesper Skriver 		if (th->th_seq == tp->rcv_nxt &&
2845e4b64281SJesper Skriver 		    LIST_EMPTY(&tp->t_segq) &&
2846e4b64281SJesper Skriver 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
2847e4b64281SJesper Skriver 			if (DELAY_ACK(tp))
28483bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
2849e4b64281SJesper Skriver 			else
2850e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
2851e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
2852e4b64281SJesper Skriver 			thflags = th->th_flags & TH_FIN;
285378b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpack);
285478b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
2855e4b64281SJesper Skriver 			ND6_HINT(tp);
28561e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
2857c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
2858c1c36a2cSMike Silbersack 				m_freem(m);
2859c1c36a2cSMike Silbersack 			else
28601e4d7da7SRobert Watson 				sbappendstream_locked(&so->so_rcv, m);
2861e8949f74SAndre Oppermann 			/* NB: sorwakeup_locked() does an implicit unlock. */
28621e4d7da7SRobert Watson 			sorwakeup_locked(so);
2863e4b64281SJesper Skriver 		} else {
2864e8949f74SAndre Oppermann 			/*
2865e8949f74SAndre Oppermann 			 * XXX: Due to the header drop above "th" is
2866e8949f74SAndre Oppermann 			 * theoretically invalid by now.  Fortunately
2867e8949f74SAndre Oppermann 			 * m_adj() doesn't actually frees any mbufs
2868e8949f74SAndre Oppermann 			 * when trimming from the head.
2869e8949f74SAndre Oppermann 			 */
2870e4b64281SJesper Skriver 			thflags = tcp_reass(tp, th, &tlen, m);
2871e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
2872e4b64281SJesper Skriver 		}
28733529149eSAndre Oppermann 		if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT))
2874f194524fSAndre Oppermann 			tcp_update_sack_list(tp, save_start, save_start + tlen);
2875302ce8d6SAndre Oppermann #if 0
2876df8bae1dSRodney W. Grimes 		/*
2877df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
2878df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
2879df8bae1dSRodney W. Grimes 		 * buffer size.
2880302ce8d6SAndre Oppermann 		 * XXX: Unused.
2881df8bae1dSRodney W. Grimes 		 */
2882f701e30dSJohn Baldwin 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt))
2883df8bae1dSRodney W. Grimes 			len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2884f701e30dSJohn Baldwin 		else
2885f701e30dSJohn Baldwin 			len = so->so_rcv.sb_hiwat;
2886302ce8d6SAndre Oppermann #endif
2887df8bae1dSRodney W. Grimes 	} else {
2888df8bae1dSRodney W. Grimes 		m_freem(m);
2889fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
2890df8bae1dSRodney W. Grimes 	}
2891df8bae1dSRodney W. Grimes 
2892df8bae1dSRodney W. Grimes 	/*
2893df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
2894df8bae1dSRodney W. Grimes 	 * that the connection is closing.
2895df8bae1dSRodney W. Grimes 	 */
2896fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
2897df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2898df8bae1dSRodney W. Grimes 			socantrcvmore(so);
2899a0292f23SGarrett Wollman 			/*
2900a0292f23SGarrett Wollman 			 * If connection is half-synchronized
2901d3eede9dSAndras Olah 			 * (ie NEEDSYN flag on) then delay ACK,
2902a0292f23SGarrett Wollman 			 * so it may be piggybacked when SYN is sent.
2903a0292f23SGarrett Wollman 			 * Otherwise, since we received a FIN then no
2904a0292f23SGarrett Wollman 			 * more input can be expected, send ACK now.
2905a0292f23SGarrett Wollman 			 */
29063bfd6421SJonathan Lemon 			if (tp->t_flags & TF_NEEDSYN)
29073bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
2908a0292f23SGarrett Wollman 			else
2909df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
2910df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
2911df8bae1dSRodney W. Grimes 		}
2912df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2913df8bae1dSRodney W. Grimes 
2914df8bae1dSRodney W. Grimes 		/*
2915df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
2916df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
2917df8bae1dSRodney W. Grimes 		 */
2918df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
29199b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
29209b8b58e0SJonathan Lemon 			/* FALLTHROUGH */
2921df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
2922df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSE_WAIT;
2923df8bae1dSRodney W. Grimes 			break;
2924df8bae1dSRodney W. Grimes 
2925df8bae1dSRodney W. Grimes 		/*
2926df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2927df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
2928df8bae1dSRodney W. Grimes 		 */
2929df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2930df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSING;
2931df8bae1dSRodney W. Grimes 			break;
2932df8bae1dSRodney W. Grimes 
2933df8bae1dSRodney W. Grimes 		/*
2934df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2935df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
2936df8bae1dSRodney W. Grimes 		 * standard timers.
2937df8bae1dSRodney W. Grimes 		 */
2938df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
2939252ca428SRobert Watson 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2940252ca428SRobert Watson 			KASSERT(ti_locked == TI_WLOCKED, ("%s: dodata "
2941252ca428SRobert Watson 			    "TCP_FIN_WAIT_2 ti_locked: %d", __func__,
2942252ca428SRobert Watson 			    ti_locked));
2943252ca428SRobert Watson 
2944340c35deSJonathan Lemon 			tcp_twstart(tp);
2945603724d3SBjoern A. Zeeb 			INP_INFO_WUNLOCK(&V_tcbinfo);
2946679d9708SAndre Oppermann 			return;
2947df8bae1dSRodney W. Grimes 		}
2948df8bae1dSRodney W. Grimes 	}
2949fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED)
2950603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
2951252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
2952252ca428SRobert Watson 
2953610ee2f9SDavid Greenman #ifdef TCPDEBUG
29544cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
2955fb59c426SYoshinobu Inoue 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
2956fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2957610ee2f9SDavid Greenman #endif
2958df8bae1dSRodney W. Grimes 
2959df8bae1dSRodney W. Grimes 	/*
2960df8bae1dSRodney W. Grimes 	 * Return any desired output.
2961df8bae1dSRodney W. Grimes 	 */
2962df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
2963df8bae1dSRodney W. Grimes 		(void) tcp_output(tp);
29647792ea27SJeffrey Hsu 
2965a14c749fSJonathan Lemon check_delack:
2966252ca428SRobert Watson 	KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d",
2967252ca428SRobert Watson 	    __func__, ti_locked));
2968603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
29698501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
2970252ca428SRobert Watson 
2971a14c749fSJonathan Lemon 	if (tp->t_flags & TF_DELACK) {
29723bfd6421SJonathan Lemon 		tp->t_flags &= ~TF_DELACK;
2973b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
29743bfd6421SJonathan Lemon 	}
29758501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
2976679d9708SAndre Oppermann 	return;
2977df8bae1dSRodney W. Grimes 
2978df8bae1dSRodney W. Grimes dropafterack:
2979df8bae1dSRodney W. Grimes 	/*
2980df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
2981df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
298280ab7c0eSGarrett Wollman 	 *
298380ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
298480ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
298580ab7c0eSGarrett Wollman 	 * RST have been dropped.
298680ab7c0eSGarrett Wollman 	 *
298780ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
298880ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
298980ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
299080ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
299180ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
299280ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
2993df8bae1dSRodney W. Grimes 	 */
2994fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
2995fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
2996a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
2997a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2998a57815efSBosko Milekic 		goto dropwithreset;
2999a57815efSBosko Milekic 	}
3000a0292f23SGarrett Wollman #ifdef TCPDEBUG
30014cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
3002fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3003fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3004a0292f23SGarrett Wollman #endif
3005fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED)
3006603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
3007252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
3008252ca428SRobert Watson 
3009df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
3010df8bae1dSRodney W. Grimes 	(void) tcp_output(tp);
30118501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
3012d6915262SRobert Watson 	m_freem(m);
3013679d9708SAndre Oppermann 	return;
3014df8bae1dSRodney W. Grimes 
3015df8bae1dSRodney W. Grimes dropwithreset:
3016fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED)
3017dd8ac7f9SRobert Watson 		INP_INFO_WUNLOCK(&V_tcbinfo);
3018252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
3019a57815efSBosko Milekic 
3020a0ca0871SRobert Watson 	if (tp != NULL) {
3021302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
30228501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
3023dd8ac7f9SRobert Watson 	} else
3024a0ca0871SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
3025679d9708SAndre Oppermann 	return;
3026df8bae1dSRodney W. Grimes 
3027df8bae1dSRodney W. Grimes drop:
3028fa046d87SRobert Watson 	if (ti_locked == TI_WLOCKED) {
3029252ca428SRobert Watson 		INP_INFO_WUNLOCK(&V_tcbinfo);
3030fa046d87SRobert Watson 		ti_locked = TI_UNLOCKED;
3031fa046d87SRobert Watson 	}
3032252ca428SRobert Watson #ifdef INVARIANTS
3033252ca428SRobert Watson 	else
3034252ca428SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
3035252ca428SRobert Watson #endif
3036252ca428SRobert Watson 
3037df8bae1dSRodney W. Grimes 	/*
3038df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
3039df8bae1dSRodney W. Grimes 	 */
3040610ee2f9SDavid Greenman #ifdef TCPDEBUG
30411c53f806SRobert Watson 	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
3042fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3043fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3044a0292f23SGarrett Wollman #endif
30451c53f806SRobert Watson 	if (tp != NULL)
30468501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
3047d6915262SRobert Watson 	m_freem(m);
3048302ce8d6SAndre Oppermann }
3049302ce8d6SAndre Oppermann 
3050302ce8d6SAndre Oppermann /*
30510ca3f933SAndre Oppermann  * Issue RST and make ACK acceptable to originator of segment.
30520ca3f933SAndre Oppermann  * The mbuf must still include the original packet header.
30530ca3f933SAndre Oppermann  * tp may be NULL.
3054302ce8d6SAndre Oppermann  */
3055302ce8d6SAndre Oppermann static void
3056302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp,
3057302ce8d6SAndre Oppermann     int tlen, int rstreason)
3058302ce8d6SAndre Oppermann {
3059b287c6c7SBjoern A. Zeeb #ifdef INET
3060302ce8d6SAndre Oppermann 	struct ip *ip;
3061b287c6c7SBjoern A. Zeeb #endif
3062302ce8d6SAndre Oppermann #ifdef INET6
3063302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6;
3064302ce8d6SAndre Oppermann #endif
30657a3244ccSRobert Watson 
30667a3244ccSRobert Watson 	if (tp != NULL) {
30678501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
30687a3244ccSRobert Watson 	}
30697a3244ccSRobert Watson 
30700ca3f933SAndre Oppermann 	/* Don't bother if destination was broadcast/multicast. */
3071302ce8d6SAndre Oppermann 	if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
3072302ce8d6SAndre Oppermann 		goto drop;
3073302ce8d6SAndre Oppermann #ifdef INET6
3074302ce8d6SAndre Oppermann 	if (mtod(m, struct ip *)->ip_v == 6) {
3075302ce8d6SAndre Oppermann 		ip6 = mtod(m, struct ip6_hdr *);
3076302ce8d6SAndre Oppermann 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3077302ce8d6SAndre Oppermann 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3078302ce8d6SAndre Oppermann 			goto drop;
3079302ce8d6SAndre Oppermann 		/* IPv6 anycast check is done at tcp6_input() */
3080b287c6c7SBjoern A. Zeeb 	}
3081302ce8d6SAndre Oppermann #endif
3082b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3083b287c6c7SBjoern A. Zeeb 	else
3084b287c6c7SBjoern A. Zeeb #endif
3085b287c6c7SBjoern A. Zeeb #ifdef INET
3086302ce8d6SAndre Oppermann 	{
3087302ce8d6SAndre Oppermann 		ip = mtod(m, struct ip *);
3088302ce8d6SAndre Oppermann 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3089302ce8d6SAndre Oppermann 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3090302ce8d6SAndre Oppermann 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3091302ce8d6SAndre Oppermann 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
3092302ce8d6SAndre Oppermann 			goto drop;
3093302ce8d6SAndre Oppermann 	}
3094b287c6c7SBjoern A. Zeeb #endif
3095302ce8d6SAndre Oppermann 
3096302ce8d6SAndre Oppermann 	/* Perform bandwidth limiting. */
3097302ce8d6SAndre Oppermann 	if (badport_bandlim(rstreason) < 0)
3098302ce8d6SAndre Oppermann 		goto drop;
3099302ce8d6SAndre Oppermann 
3100302ce8d6SAndre Oppermann 	/* tcp_respond consumes the mbuf chain. */
3101302ce8d6SAndre Oppermann 	if (th->th_flags & TH_ACK) {
3102302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0,
3103302ce8d6SAndre Oppermann 		    th->th_ack, TH_RST);
3104302ce8d6SAndre Oppermann 	} else {
3105302ce8d6SAndre Oppermann 		if (th->th_flags & TH_SYN)
3106302ce8d6SAndre Oppermann 			tlen++;
3107302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
3108302ce8d6SAndre Oppermann 		    (tcp_seq)0, TH_RST|TH_ACK);
3109302ce8d6SAndre Oppermann 	}
3110302ce8d6SAndre Oppermann 	return;
3111302ce8d6SAndre Oppermann drop:
3112302ce8d6SAndre Oppermann 	m_freem(m);
3113df8bae1dSRodney W. Grimes }
3114df8bae1dSRodney W. Grimes 
3115be2ac88cSJonathan Lemon /*
3116be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
3117be2ac88cSJonathan Lemon  */
31180312fbe9SPoul-Henning Kamp static void
3119ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags)
3120df8bae1dSRodney W. Grimes {
3121df8bae1dSRodney W. Grimes 	int opt, optlen;
3122df8bae1dSRodney W. Grimes 
3123be2ac88cSJonathan Lemon 	to->to_flags = 0;
3124df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
3125df8bae1dSRodney W. Grimes 		opt = cp[0];
3126df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
3127df8bae1dSRodney W. Grimes 			break;
3128df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
3129df8bae1dSRodney W. Grimes 			optlen = 1;
3130df8bae1dSRodney W. Grimes 		else {
3131b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
3132b474779fSJun-ichiro itojun Hagino 				break;
3133df8bae1dSRodney W. Grimes 			optlen = cp[1];
3134b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
3135df8bae1dSRodney W. Grimes 				break;
3136df8bae1dSRodney W. Grimes 		}
3137df8bae1dSRodney W. Grimes 		switch (opt) {
3138df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
3139df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
3140df8bae1dSRodney W. Grimes 				continue;
3141f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3142df8bae1dSRodney W. Grimes 				continue;
3143be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
3144be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
3145be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
3146fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
3147df8bae1dSRodney W. Grimes 			break;
3148df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
3149df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
3150df8bae1dSRodney W. Grimes 				continue;
3151f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3152df8bae1dSRodney W. Grimes 				continue;
3153be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
315402a1a643SAndre Oppermann 			to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT);
3155df8bae1dSRodney W. Grimes 			break;
3156df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
3157df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
3158df8bae1dSRodney W. Grimes 				continue;
3159be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
3160a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
3161a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
3162fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
3163a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
3164a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
3165fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
3166df8bae1dSRodney W. Grimes 			break;
31671cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
31681cfd4b53SBruce M Simpson 		/*
31691cfd4b53SBruce M Simpson 		 * XXX In order to reply to a host which has set the
31701cfd4b53SBruce M Simpson 		 * TCP_SIGNATURE option in its initial SYN, we have to
31711cfd4b53SBruce M Simpson 		 * record the fact that the option was observed here
31721cfd4b53SBruce M Simpson 		 * for the syncache code to perform the correct response.
31731cfd4b53SBruce M Simpson 		 */
31741cfd4b53SBruce M Simpson 		case TCPOPT_SIGNATURE:
31751cfd4b53SBruce M Simpson 			if (optlen != TCPOLEN_SIGNATURE)
31761cfd4b53SBruce M Simpson 				continue;
3177df47e437SAndre Oppermann 			to->to_flags |= TOF_SIGNATURE;
3178df47e437SAndre Oppermann 			to->to_signature = cp + 2;
31791cfd4b53SBruce M Simpson 			break;
3180265ed012SBruce M Simpson #endif
31816d90faf3SPaul Saab 		case TCPOPT_SACK_PERMITTED:
3182f72167f4SAndre Oppermann 			if (optlen != TCPOLEN_SACK_PERMITTED)
31836d90faf3SPaul Saab 				continue;
3184f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3185f72167f4SAndre Oppermann 				continue;
3186603724d3SBjoern A. Zeeb 			if (!V_tcp_do_sack)
3187f72167f4SAndre Oppermann 				continue;
3188fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACKPERM;
31896d90faf3SPaul Saab 			break;
31906d90faf3SPaul Saab 		case TCPOPT_SACK:
31915a53ca16SPaul Saab 			if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
31926d90faf3SPaul Saab 				continue;
3193b728e902SAndre Oppermann 			if (flags & TO_SYN)
3194b728e902SAndre Oppermann 				continue;
3195fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACK;
31965a53ca16SPaul Saab 			to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
31975a53ca16SPaul Saab 			to->to_sacks = cp + 2;
319878b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_rcv_blocks);
31996d90faf3SPaul Saab 			break;
3200be2ac88cSJonathan Lemon 		default:
3201be2ac88cSJonathan Lemon 			continue;
3202df8bae1dSRodney W. Grimes 		}
3203df8bae1dSRodney W. Grimes 	}
3204df8bae1dSRodney W. Grimes }
3205df8bae1dSRodney W. Grimes 
3206df8bae1dSRodney W. Grimes /*
3207df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
3208df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
3209df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
3210df8bae1dSRodney W. Grimes  * sequencing purposes.
3211df8bae1dSRodney W. Grimes  */
32120312fbe9SPoul-Henning Kamp static void
3213ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m,
3214ad3f9ab3SAndre Oppermann     int off)
3215df8bae1dSRodney W. Grimes {
3216fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
3217df8bae1dSRodney W. Grimes 
3218df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
3219df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
3220df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
3221df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
3222df8bae1dSRodney W. Grimes 
32238501a69cSRobert Watson 			INP_WLOCK_ASSERT(tp->t_inpcb);
32247a3244ccSRobert Watson 
3225df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
3226df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
3227df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
3228df8bae1dSRodney W. Grimes 			m->m_len--;
322969a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
323069a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
3231df8bae1dSRodney W. Grimes 			return;
3232df8bae1dSRodney W. Grimes 		}
3233df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
3234df8bae1dSRodney W. Grimes 		m = m->m_next;
32354dfdffe9SAndre Oppermann 		if (m == NULL)
3236df8bae1dSRodney W. Grimes 			break;
3237df8bae1dSRodney W. Grimes 	}
3238df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
3239df8bae1dSRodney W. Grimes }
3240df8bae1dSRodney W. Grimes 
3241df8bae1dSRodney W. Grimes /*
3242df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
3243df8bae1dSRodney W. Grimes  * and update averages and current timeout.
3244df8bae1dSRodney W. Grimes  */
32450312fbe9SPoul-Henning Kamp static void
3246ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt)
3247df8bae1dSRodney W. Grimes {
3248ad3f9ab3SAndre Oppermann 	int delta;
3249233e8c18SGarrett Wollman 
32508501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
32512be3bf22SRobert Watson 
325278b50714SRobert Watson 	TCPSTAT_INC(tcps_rttupdated);
3253233e8c18SGarrett Wollman 	tp->t_rttupdated++;
3254233e8c18SGarrett Wollman 	if (tp->t_srtt != 0) {
3255233e8c18SGarrett Wollman 		/*
3256233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
3257233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
3258233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
3259233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
3260233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
3261233e8c18SGarrett Wollman 		 */
3262233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
3263233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
3264233e8c18SGarrett Wollman 
3265233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
3266233e8c18SGarrett Wollman 			tp->t_srtt = 1;
3267233e8c18SGarrett Wollman 
3268233e8c18SGarrett Wollman 		/*
3269233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
3270233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
3271233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
3272233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
3273233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
3274233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
3275233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
3276233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
3277233e8c18SGarrett Wollman 		 */
3278233e8c18SGarrett Wollman 		if (delta < 0)
3279233e8c18SGarrett Wollman 			delta = -delta;
3280233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
3281233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
3282233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
32831fcc99b5SMatthew Dillon 		if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
32841fcc99b5SMatthew Dillon 		    tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3285233e8c18SGarrett Wollman 	} else {
3286233e8c18SGarrett Wollman 		/*
3287233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
3288233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
3289233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
3290233e8c18SGarrett Wollman 		 */
3291233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
3292233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
32931fcc99b5SMatthew Dillon 		tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3294233e8c18SGarrett Wollman 	}
32959b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
3296df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
3297df8bae1dSRodney W. Grimes 
3298df8bae1dSRodney W. Grimes 	/*
3299df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
3300df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
3301df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
3302df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
3303df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
3304df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
3305df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
3306df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
3307df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
3308df8bae1dSRodney W. Grimes 	 */
3309233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
33109e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
3311df8bae1dSRodney W. Grimes 
3312df8bae1dSRodney W. Grimes 	/*
3313df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
3314df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
3315df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
3316df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
3317df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
3318df8bae1dSRodney W. Grimes 	 */
3319df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
3320df8bae1dSRodney W. Grimes }
3321df8bae1dSRodney W. Grimes 
3322df8bae1dSRodney W. Grimes /*
3323df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
3324df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
3325df8bae1dSRodney W. Grimes  * If none, use an mss that can be handled on the outgoing
3326df8bae1dSRodney W. Grimes  * interface without forcing IP to fragment; if bigger than
3327df8bae1dSRodney W. Grimes  * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
3328df8bae1dSRodney W. Grimes  * to utilize large mbufs.  If no route is found, route has no mtu,
3329df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
3330df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
3331df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
3332df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
3333df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
3334df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
3335df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
3336a0292f23SGarrett Wollman  *
3337a0292f23SGarrett Wollman  * Also take into account the space needed for options that we
3338a0292f23SGarrett Wollman  * send regularly.  Make maxseg shorter by that amount to assure
3339a0292f23SGarrett Wollman  * that we can send maxseg amount of data even when the options
3340a0292f23SGarrett Wollman  * are present.  Store the upper limit of the length of options plus
3341a0292f23SGarrett Wollman  * data in maxopd.
3342a0292f23SGarrett Wollman  *
334397d8d152SAndre Oppermann  * NOTE that this routine is only called when we process an incoming
3344ef341ee1SGleb Smirnoff  * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS
3345ef341ee1SGleb Smirnoff  * settings are handled in tcp_mssopt().
3346df8bae1dSRodney W. Grimes  */
3347a0292f23SGarrett Wollman void
3348ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer,
334991d6cfa6SBjoern A. Zeeb     struct hc_metrics_lite *metricptr, int *mtuflags)
3350df8bae1dSRodney W. Grimes {
3351b287c6c7SBjoern A. Zeeb 	int mss = 0;
3352b287c6c7SBjoern A. Zeeb 	u_long maxmtu = 0;
3353c068736aSJeffrey Hsu 	struct inpcb *inp = tp->t_inpcb;
335497d8d152SAndre Oppermann 	struct hc_metrics_lite metrics;
3355ef341ee1SGleb Smirnoff 	int origoffer;
3356fb59c426SYoshinobu Inoue #ifdef INET6
3357c068736aSJeffrey Hsu 	int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
3358c068736aSJeffrey Hsu 	size_t min_protoh = isipv6 ?
3359c068736aSJeffrey Hsu 			    sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
3360c068736aSJeffrey Hsu 			    sizeof (struct tcpiphdr);
3361c068736aSJeffrey Hsu #else
3362c068736aSJeffrey Hsu 	const size_t min_protoh = sizeof(struct tcpiphdr);
3363fb59c426SYoshinobu Inoue #endif
3364df8bae1dSRodney W. Grimes 
33658501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
33667a3244ccSRobert Watson 
3367ef341ee1SGleb Smirnoff 	if (mtuoffer != -1) {
3368ef341ee1SGleb Smirnoff 		KASSERT(offer == -1, ("%s: conflict", __func__));
3369ef341ee1SGleb Smirnoff 		offer = mtuoffer - min_protoh;
3370ef341ee1SGleb Smirnoff 	}
3371ef341ee1SGleb Smirnoff 	origoffer = offer;
3372ef341ee1SGleb Smirnoff 
3373e8949f74SAndre Oppermann 	/* Initialize. */
337497d8d152SAndre Oppermann #ifdef INET6
337597d8d152SAndre Oppermann 	if (isipv6) {
337691d6cfa6SBjoern A. Zeeb 		maxmtu = tcp_maxmtu6(&inp->inp_inc, mtuflags);
3377603724d3SBjoern A. Zeeb 		tp->t_maxopd = tp->t_maxseg = V_tcp_v6mssdflt;
3378b287c6c7SBjoern A. Zeeb 	}
337997d8d152SAndre Oppermann #endif
3380b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3381b287c6c7SBjoern A. Zeeb 	else
3382b287c6c7SBjoern A. Zeeb #endif
3383b287c6c7SBjoern A. Zeeb #ifdef INET
338497d8d152SAndre Oppermann 	{
338591d6cfa6SBjoern A. Zeeb 		maxmtu = tcp_maxmtu(&inp->inp_inc, mtuflags);
3386603724d3SBjoern A. Zeeb 		tp->t_maxopd = tp->t_maxseg = V_tcp_mssdflt;
3387df8bae1dSRodney W. Grimes 	}
3388b287c6c7SBjoern A. Zeeb #endif
3389df8bae1dSRodney W. Grimes 
339097d8d152SAndre Oppermann 	/*
3391e8949f74SAndre Oppermann 	 * No route to sender, stay with default mss and return.
339297d8d152SAndre Oppermann 	 */
33936f01cac6SBjoern A. Zeeb 	if (maxmtu == 0) {
33946f01cac6SBjoern A. Zeeb 		/*
33958e5c87f4SBjoern A. Zeeb 		 * In case we return early we need to initialize metrics
33966f01cac6SBjoern A. Zeeb 		 * to a defined state as tcp_hc_get() would do for us
33976f01cac6SBjoern A. Zeeb 		 * if there was no cache hit.
33986f01cac6SBjoern A. Zeeb 		 */
33996f01cac6SBjoern A. Zeeb 		if (metricptr != NULL)
34006f01cac6SBjoern A. Zeeb 			bzero(metricptr, sizeof(struct hc_metrics_lite));
340197d8d152SAndre Oppermann 		return;
34026f01cac6SBjoern A. Zeeb 	}
340397d8d152SAndre Oppermann 
3404e8949f74SAndre Oppermann 	/* What have we got? */
340597d8d152SAndre Oppermann 	switch (offer) {
340697d8d152SAndre Oppermann 		case 0:
340797d8d152SAndre Oppermann 			/*
340897d8d152SAndre Oppermann 			 * Offer == 0 means that there was no MSS on the SYN
3409c3b02504SBjoern A. Zeeb 			 * segment, in this case we use tcp_mssdflt as
3410c3b02504SBjoern A. Zeeb 			 * already assigned to t_maxopd above.
341197d8d152SAndre Oppermann 			 */
3412c3b02504SBjoern A. Zeeb 			offer = tp->t_maxopd;
341397d8d152SAndre Oppermann 			break;
341497d8d152SAndre Oppermann 
341597d8d152SAndre Oppermann 		case -1:
3416a0292f23SGarrett Wollman 			/*
3417c94c54e4SAndre Oppermann 			 * Offer == -1 means that we didn't receive SYN yet.
3418a0292f23SGarrett Wollman 			 */
341997d8d152SAndre Oppermann 			/* FALLTHROUGH */
342097d8d152SAndre Oppermann 
342197d8d152SAndre Oppermann 		default:
3422a0292f23SGarrett Wollman 			/*
342353369ac9SAndre Oppermann 			 * Prevent DoS attack with too small MSS. Round up
342453369ac9SAndre Oppermann 			 * to at least minmss.
342553369ac9SAndre Oppermann 			 */
3426603724d3SBjoern A. Zeeb 			offer = max(offer, V_tcp_minmss);
342797d8d152SAndre Oppermann 	}
3428a0292f23SGarrett Wollman 
3429df8bae1dSRodney W. Grimes 	/*
3430e8949f74SAndre Oppermann 	 * rmx information is now retrieved from tcp_hostcache.
3431df8bae1dSRodney W. Grimes 	 */
343297d8d152SAndre Oppermann 	tcp_hc_get(&inp->inp_inc, &metrics);
34333cee92e0SBjoern A. Zeeb 	if (metricptr != NULL)
34343cee92e0SBjoern A. Zeeb 		bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite));
343597d8d152SAndre Oppermann 
3436df8bae1dSRodney W. Grimes 	/*
3437e8949f74SAndre Oppermann 	 * If there's a discovered mtu int tcp hostcache, use it
3438fb59c426SYoshinobu Inoue 	 * else, use the link mtu.
3439df8bae1dSRodney W. Grimes 	 */
344097d8d152SAndre Oppermann 	if (metrics.rmx_mtu)
34412d166c02SAndre Oppermann 		mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
3442c068736aSJeffrey Hsu 	else {
344331b3783cSHajimu UMEMOTO #ifdef INET6
3444fb59c426SYoshinobu Inoue 		if (isipv6) {
344597d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3446603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
344797d8d152SAndre Oppermann 			    !in6_localaddr(&inp->in6p_faddr))
3448603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_v6mssdflt);
3449b287c6c7SBjoern A. Zeeb 		}
3450b3399803SHajimu UMEMOTO #endif
3451b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3452b287c6c7SBjoern A. Zeeb 		else
3453b287c6c7SBjoern A. Zeeb #endif
3454b287c6c7SBjoern A. Zeeb #ifdef INET
345597d8d152SAndre Oppermann 		{
345697d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3457603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
345897d8d152SAndre Oppermann 			    !in_localaddr(inp->inp_faddr))
3459603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_mssdflt);
3460a0292f23SGarrett Wollman 		}
3461b287c6c7SBjoern A. Zeeb #endif
34623cee92e0SBjoern A. Zeeb 		/*
34633cee92e0SBjoern A. Zeeb 		 * XXX - The above conditional (mss = maxmtu - min_protoh)
34643cee92e0SBjoern A. Zeeb 		 * probably violates the TCP spec.
34653cee92e0SBjoern A. Zeeb 		 * The problem is that, since we don't know the
34663cee92e0SBjoern A. Zeeb 		 * other end's MSS, we are supposed to use a conservative
34673cee92e0SBjoern A. Zeeb 		 * default.  But, if we do that, then MTU discovery will
34683cee92e0SBjoern A. Zeeb 		 * never actually take place, because the conservative
34693cee92e0SBjoern A. Zeeb 		 * default is much less than the MTUs typically seen
34703cee92e0SBjoern A. Zeeb 		 * on the Internet today.  For the moment, we'll sweep
34713cee92e0SBjoern A. Zeeb 		 * this under the carpet.
34723cee92e0SBjoern A. Zeeb 		 *
34733cee92e0SBjoern A. Zeeb 		 * The conservative default might not actually be a problem
34743cee92e0SBjoern A. Zeeb 		 * if the only case this occurs is when sending an initial
34753cee92e0SBjoern A. Zeeb 		 * SYN with options and data to a host we've never talked
34763cee92e0SBjoern A. Zeeb 		 * to before.  Then, they will reply with an MSS value which
34773cee92e0SBjoern A. Zeeb 		 * will get recorded and the new parameters should get
34783cee92e0SBjoern A. Zeeb 		 * recomputed.  For Further Study.
34793cee92e0SBjoern A. Zeeb 		 */
348097d8d152SAndre Oppermann 	}
3481a0292f23SGarrett Wollman 	mss = min(mss, offer);
348297d8d152SAndre Oppermann 
3483a0292f23SGarrett Wollman 	/*
34843cee92e0SBjoern A. Zeeb 	 * Sanity check: make sure that maxopd will be large
34853cee92e0SBjoern A. Zeeb 	 * enough to allow some data on segments even if the
34863cee92e0SBjoern A. Zeeb 	 * all the option space is used (40bytes).  Otherwise
34873cee92e0SBjoern A. Zeeb 	 * funny things may happen in tcp_output.
34883cee92e0SBjoern A. Zeeb 	 */
34893cee92e0SBjoern A. Zeeb 	mss = max(mss, 64);
34903cee92e0SBjoern A. Zeeb 
34913cee92e0SBjoern A. Zeeb 	/*
3492a0292f23SGarrett Wollman 	 * maxopd stores the maximum length of data AND options
3493a0292f23SGarrett Wollman 	 * in a segment; maxseg is the amount of data in a normal
3494a0292f23SGarrett Wollman 	 * segment.  We need to store this value (maxopd) apart
3495a0292f23SGarrett Wollman 	 * from maxseg, because now every segment carries options
3496a0292f23SGarrett Wollman 	 * and thus we normally have somewhat less data in segments.
3497a0292f23SGarrett Wollman 	 */
3498a0292f23SGarrett Wollman 	tp->t_maxopd = mss;
3499a0292f23SGarrett Wollman 
3500a0292f23SGarrett Wollman 	/*
3501e8949f74SAndre Oppermann 	 * origoffer==-1 indicates that no segments were received yet.
3502c94c54e4SAndre Oppermann 	 * In this case we just guess.
3503a0292f23SGarrett Wollman 	 */
3504a0292f23SGarrett Wollman 	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
3505a0292f23SGarrett Wollman 	    (origoffer == -1 ||
3506a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
3507a0292f23SGarrett Wollman 		mss -= TCPOLEN_TSTAMP_APPA;
3508a0292f23SGarrett Wollman 
3509df8bae1dSRodney W. Grimes #if	(MCLBYTES & (MCLBYTES - 1)) == 0
3510df8bae1dSRodney W. Grimes 	if (mss > MCLBYTES)
3511df8bae1dSRodney W. Grimes 		mss &= ~(MCLBYTES-1);
3512df8bae1dSRodney W. Grimes #else
3513df8bae1dSRodney W. Grimes 	if (mss > MCLBYTES)
3514df8bae1dSRodney W. Grimes 		mss = mss / MCLBYTES * MCLBYTES;
3515df8bae1dSRodney W. Grimes #endif
351697d8d152SAndre Oppermann 	tp->t_maxseg = mss;
35173cee92e0SBjoern A. Zeeb }
35183cee92e0SBjoern A. Zeeb 
35193cee92e0SBjoern A. Zeeb void
35203cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer)
35213cee92e0SBjoern A. Zeeb {
3522dbc42409SLawrence Stewart 	int mss;
35233cee92e0SBjoern A. Zeeb 	u_long bufsize;
35243cee92e0SBjoern A. Zeeb 	struct inpcb *inp;
35253cee92e0SBjoern A. Zeeb 	struct socket *so;
35263cee92e0SBjoern A. Zeeb 	struct hc_metrics_lite metrics;
352791d6cfa6SBjoern A. Zeeb 	int mtuflags = 0;
3528dbc42409SLawrence Stewart 
35293cee92e0SBjoern A. Zeeb 	KASSERT(tp != NULL, ("%s: tp == NULL", __func__));
35303cee92e0SBjoern A. Zeeb 
3531ef341ee1SGleb Smirnoff 	tcp_mss_update(tp, offer, -1, &metrics, &mtuflags);
35323cee92e0SBjoern A. Zeeb 
35333cee92e0SBjoern A. Zeeb 	mss = tp->t_maxseg;
35343cee92e0SBjoern A. Zeeb 	inp = tp->t_inpcb;
353597d8d152SAndre Oppermann 
3536df8bae1dSRodney W. Grimes 	/*
353797d8d152SAndre Oppermann 	 * If there's a pipesize, change the socket buffer to that size,
353897d8d152SAndre Oppermann 	 * don't change if sb_hiwat is different than default (then it
353997d8d152SAndre Oppermann 	 * has been changed on purpose with setsockopt).
354097d8d152SAndre Oppermann 	 * Make the socket buffers an integral number of mss units;
354197d8d152SAndre Oppermann 	 * if the mss is larger than the socket buffer, decrease the mss.
3542df8bae1dSRodney W. Grimes 	 */
3543c3b02504SBjoern A. Zeeb 	so = inp->inp_socket;
35443f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
3545e233e2acSAndre Oppermann 	if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe)
354697d8d152SAndre Oppermann 		bufsize = metrics.rmx_sendpipe;
354797d8d152SAndre Oppermann 	else
3548df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
3549df8bae1dSRodney W. Grimes 	if (bufsize < mss)
3550df8bae1dSRodney W. Grimes 		mss = bufsize;
3551df8bae1dSRodney W. Grimes 	else {
3552df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3553df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3554df8bae1dSRodney W. Grimes 			bufsize = sb_max;
355588c39af3SRuslan Ermilov 		if (bufsize > so->so_snd.sb_hiwat)
35563f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_snd, bufsize, so, NULL);
3557df8bae1dSRodney W. Grimes 	}
35583f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
3559df8bae1dSRodney W. Grimes 	tp->t_maxseg = mss;
3560df8bae1dSRodney W. Grimes 
35613f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
3562e233e2acSAndre Oppermann 	if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe)
356397d8d152SAndre Oppermann 		bufsize = metrics.rmx_recvpipe;
356497d8d152SAndre Oppermann 	else
3565df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
3566df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
3567df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3568df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3569df8bae1dSRodney W. Grimes 			bufsize = sb_max;
357088c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
35713f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL);
3572df8bae1dSRodney W. Grimes 	}
35733f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
357491d6cfa6SBjoern A. Zeeb 
357591d6cfa6SBjoern A. Zeeb 	/* Check the interface for TSO capabilities. */
357691d6cfa6SBjoern A. Zeeb 	if (mtuflags & CSUM_TSO)
357791d6cfa6SBjoern A. Zeeb 		tp->t_flags |= TF_TSO;
3578a0292f23SGarrett Wollman }
3579a0292f23SGarrett Wollman 
3580a0292f23SGarrett Wollman /*
3581a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
3582a0292f23SGarrett Wollman  */
3583a0292f23SGarrett Wollman int
3584ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc)
3585a0292f23SGarrett Wollman {
358697d8d152SAndre Oppermann 	int mss = 0;
358797d8d152SAndre Oppermann 	u_long maxmtu = 0;
358897d8d152SAndre Oppermann 	u_long thcmtu = 0;
358997d8d152SAndre Oppermann 	size_t min_protoh;
3590a0292f23SGarrett Wollman 
359197d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3592a0292f23SGarrett Wollman 
359331b3783cSHajimu UMEMOTO #ifdef INET6
3594dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
3595603724d3SBjoern A. Zeeb 		mss = V_tcp_v6mssdflt;
3596233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(inc, NULL);
359797d8d152SAndre Oppermann 		min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
3598b287c6c7SBjoern A. Zeeb 	}
359931b3783cSHajimu UMEMOTO #endif
3600b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3601b287c6c7SBjoern A. Zeeb 	else
3602b287c6c7SBjoern A. Zeeb #endif
3603b287c6c7SBjoern A. Zeeb #ifdef INET
360497d8d152SAndre Oppermann 	{
3605603724d3SBjoern A. Zeeb 		mss = V_tcp_mssdflt;
3606233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(inc, NULL);
360797d8d152SAndre Oppermann 		min_protoh = sizeof(struct tcpiphdr);
360897d8d152SAndre Oppermann 	}
3609b287c6c7SBjoern A. Zeeb #endif
36103a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET)
36113a9391deSBjoern A. Zeeb 	thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
36123a9391deSBjoern A. Zeeb #endif
36133a9391deSBjoern A. Zeeb 
361497d8d152SAndre Oppermann 	if (maxmtu && thcmtu)
361597d8d152SAndre Oppermann 		mss = min(maxmtu, thcmtu) - min_protoh;
361697d8d152SAndre Oppermann 	else if (maxmtu || thcmtu)
361797d8d152SAndre Oppermann 		mss = max(maxmtu, thcmtu) - min_protoh;
361897d8d152SAndre Oppermann 
361997d8d152SAndre Oppermann 	return (mss);
3620df8bae1dSRodney W. Grimes }
362146f58482SJonathan Lemon 
362246f58482SJonathan Lemon 
362346f58482SJonathan Lemon /*
3624c068736aSJeffrey Hsu  * On a partial ack arrives, force the retransmission of the
3625c068736aSJeffrey Hsu  * next unacknowledged segment.  Do not clear tp->t_dupacks.
3626c068736aSJeffrey Hsu  * By setting snd_nxt to ti_ack, this forces retransmission timer to
3627c068736aSJeffrey Hsu  * be started again.
362846f58482SJonathan Lemon  */
3629c068736aSJeffrey Hsu static void
3630ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
363146f58482SJonathan Lemon {
363246f58482SJonathan Lemon 	tcp_seq onxt = tp->snd_nxt;
363346f58482SJonathan Lemon 	u_long  ocwnd = tp->snd_cwnd;
363446f58482SJonathan Lemon 
36358501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
36367a3244ccSRobert Watson 
3637b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_REXMT, 0);
363846f58482SJonathan Lemon 	tp->t_rtttime = 0;
363946f58482SJonathan Lemon 	tp->snd_nxt = th->th_ack;
36406b2a5f92SJayanth Vijayaraghavan 	/*
3641c068736aSJeffrey Hsu 	 * Set snd_cwnd to one segment beyond acknowledged offset.
3642c068736aSJeffrey Hsu 	 * (tp->snd_una has not yet been updated when this function is called.)
36436b2a5f92SJayanth Vijayaraghavan 	 */
3644dbc42409SLawrence Stewart 	tp->snd_cwnd = tp->t_maxseg + BYTES_THIS_ACK(tp, th);
3645a84db8f4SMatthew Dillon 	tp->t_flags |= TF_ACKNOW;
36466b2a5f92SJayanth Vijayaraghavan 	(void) tcp_output(tp);
364746f58482SJonathan Lemon 	tp->snd_cwnd = ocwnd;
364846f58482SJonathan Lemon 	if (SEQ_GT(onxt, tp->snd_nxt))
364946f58482SJonathan Lemon 		tp->snd_nxt = onxt;
365046f58482SJonathan Lemon 	/*
365146f58482SJonathan Lemon 	 * Partial window deflation.  Relies on fact that tp->snd_una
365246f58482SJonathan Lemon 	 * not updated yet.
365346f58482SJonathan Lemon 	 */
3654dbc42409SLawrence Stewart 	if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th))
3655dbc42409SLawrence Stewart 		tp->snd_cwnd -= BYTES_THIS_ACK(tp, th);
3656d7587117SPaul Saab 	else
3657d7587117SPaul Saab 		tp->snd_cwnd = 0;
3658d7587117SPaul Saab 	tp->snd_cwnd += tp->t_maxseg;
365946f58482SJonathan Lemon }
3660