xref: /freebsd/sys/netinet/tcp_input.c (revision dcdb4371ca97ecfaabf614fb5bcc04dd81df34c8)
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.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29e79adb8eSGarrett Wollman  *	@(#)tcp_input.c	8.12 (Berkeley) 5/24/95
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
324b421e2dSMike Silbersack #include <sys/cdefs.h>
334b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
344b421e2dSMike Silbersack 
35f9e354dfSJulian Elischer #include "opt_ipfw.h"		/* for ipfw_fwd	*/
361cfd4b53SBruce M Simpson #include "opt_inet.h"
37fb59c426SYoshinobu Inoue #include "opt_inet6.h"
383a2a9f79SYoshinobu Inoue #include "opt_ipsec.h"
39c488362eSRobert Watson #include "opt_mac.h"
400cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
410cc12cc5SJoerg Wunsch 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
4398163b98SPoul-Henning Kamp #include <sys/kernel.h>
44df8bae1dSRodney W. Grimes #include <sys/malloc.h>
45df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
46a29f300eSGarrett Wollman #include <sys/proc.h>		/* for proc0 declaration */
47df8bae1dSRodney W. Grimes #include <sys/protosw.h>
48960ed29cSSeigo Tanimura #include <sys/signalvar.h>
49df8bae1dSRodney W. Grimes #include <sys/socket.h>
50df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
51960ed29cSSeigo Tanimura #include <sys/sysctl.h>
52816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
53960ed29cSSeigo Tanimura #include <sys/systm.h>
54603724d3SBjoern A. Zeeb #include <sys/vimage.h>
55df8bae1dSRodney W. Grimes 
56e79adb8eSGarrett Wollman #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
57e79adb8eSGarrett Wollman 
5812e2e970SAndre Oppermann #include <vm/uma.h>
5912e2e970SAndre Oppermann 
60df8bae1dSRodney W. Grimes #include <net/if.h>
61df8bae1dSRodney W. Grimes #include <net/route.h>
62df8bae1dSRodney W. Grimes 
63773673c1SAndre Oppermann #define TCPSTATES		/* for logging */
64773673c1SAndre Oppermann 
65df8bae1dSRodney W. Grimes #include <netinet/in.h>
66960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
67df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
68960ed29cSSeigo Tanimura #include <netinet/in_var.h>
69df8bae1dSRodney W. Grimes #include <netinet/ip.h>
708e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h>	/* required for icmp_var.h */
71686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM */
72df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
73ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
74686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
75686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
76686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
77960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h>
78960ed29cSSeigo Tanimura #include <netinet6/nd6.h>
79df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
80df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
81df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
82df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
83df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
84fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h>
85df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
86c325962bSMike Silbersack #include <netinet/tcp_syncache.h>
87610ee2f9SDavid Greenman #ifdef TCPDEBUG
88df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
89fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */
904b79449eSBjoern A. Zeeb #include <netinet/vinet.h>
914b79449eSBjoern A. Zeeb 
924b79449eSBjoern A. Zeeb #ifdef INET6
934b79449eSBjoern A. Zeeb #include <netinet6/vinet6.h>
944b79449eSBjoern A. Zeeb #endif
95fb59c426SYoshinobu Inoue 
96b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
97b9234fafSSam Leffler #include <netipsec/ipsec.h>
98b9234fafSSam Leffler #include <netipsec/ipsec6.h>
99b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
100b9234fafSSam Leffler 
101db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
102db4f9cc7SJonathan Lemon 
103aed55708SRobert Watson #include <security/mac/mac_framework.h>
104aed55708SRobert Watson 
105c068736aSJeffrey Hsu static const int tcprexmtthresh = 3;
1060312fbe9SPoul-Henning Kamp 
10744e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
1082f96f1f4SGarrett Wollman struct	tcpstat tcpstat;
10944e33a07SMarko Zec int	blackhole;
11044e33a07SMarko Zec int	tcp_delack_enabled;
11144e33a07SMarko Zec int	drop_synfin;
11244e33a07SMarko Zec int	tcp_do_rfc3042;
11344e33a07SMarko Zec int	tcp_do_rfc3390;
11444e33a07SMarko Zec int	tcp_do_ecn;
11544e33a07SMarko Zec int	tcp_ecn_maxretries;
11644e33a07SMarko Zec int	tcp_insecure_rst;
11744e33a07SMarko Zec int	tcp_do_autorcvbuf;
11844e33a07SMarko Zec int	tcp_autorcvbuf_inc;
11944e33a07SMarko Zec int	tcp_autorcvbuf_max;
12044e33a07SMarko Zec #endif
12144e33a07SMarko Zec 
1228b615593SMarko Zec SYSCTL_V_STRUCT(V_NET, vnet_inet, _net_inet_tcp, TCPCTL_STATS, stats,
1238b615593SMarko Zec     CTLFLAG_RW, tcpstat , tcpstat,
1248b615593SMarko Zec     "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
1250312fbe9SPoul-Henning Kamp 
126773673c1SAndre Oppermann int tcp_log_in_vain = 0;
127816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
128574b6964SAndre Oppermann     &tcp_log_in_vain, 0, "Log all incoming TCP segments to closed ports");
129816a3d83SPoul-Henning Kamp 
1308b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW,
1318b615593SMarko Zec     blackhole, 0, "Do not send RST on segments to closed ports");
13216f7f31fSGeoff Rehmet 
1338b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, delayed_ack,
1348b615593SMarko Zec     CTLFLAG_RW, tcp_delack_enabled, 0,
1353d177f46SBill Fumerola     "Delay ACK to try and piggyback it onto a data packet");
136f498eeeeSDavid Greenman 
1378b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, drop_synfin,
1388b615593SMarko Zec     CTLFLAG_RW, drop_synfin, 0, "Drop TCP packets with SYN+FIN set");
139f8613305SDag-Erling Smørgrav 
1408b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW,
1418b615593SMarko Zec     tcp_do_rfc3042, 0, "Enable RFC 3042 (Limited Transmit)");
142582a954bSJeffrey Hsu 
1438b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW,
1448b615593SMarko Zec     tcp_do_rfc3390, 0,
145da3a8a1aSJeffrey Hsu     "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
146da3a8a1aSJeffrey Hsu 
147f2512ba1SRui Paulo SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN");
1488b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_ecn, OID_AUTO, enable,
1498b615593SMarko Zec     CTLFLAG_RW, tcp_do_ecn, 0, "TCP ECN support");
1508b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_ecn, OID_AUTO, maxretries,
1518b615593SMarko Zec     CTLFLAG_RW, tcp_ecn_maxretries, 0, "Max retries before giving up on ECN");
152f2512ba1SRui Paulo 
1538b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, insecure_rst,
1548b615593SMarko Zec     CTLFLAG_RW, tcp_insecure_rst, 0,
1556489fe65SAndre Oppermann     "Follow the old (insecure) criteria for accepting RST packets");
156a69968eeSMike Silbersack 
1578b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, recvbuf_auto,
1588b615593SMarko Zec     CTLFLAG_RW, tcp_do_autorcvbuf, 0,
1598b615593SMarko Zec     "Enable automatic receive buffer sizing");
1606741ecf5SAndre Oppermann 
1618b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, recvbuf_inc,
1628b615593SMarko Zec     CTLFLAG_RW, tcp_autorcvbuf_inc, 0,
1636489fe65SAndre Oppermann     "Incrementor step size of automatic receive buffer");
1646741ecf5SAndre Oppermann 
1658b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, recvbuf_max,
1668b615593SMarko Zec     CTLFLAG_RW, tcp_autorcvbuf_max, 0,
1678b615593SMarko Zec     "Max size of automatic receive buffer");
1686741ecf5SAndre Oppermann 
169252ca428SRobert Watson int	tcp_read_locking = 1;
170252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, read_locking, CTLFLAG_RW,
171252ca428SRobert Watson     &tcp_read_locking, 0, "Enable read locking strategy");
172252ca428SRobert Watson 
173252ca428SRobert Watson int	tcp_rlock_atfirst;
174252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, rlock_atfirst, CTLFLAG_RD,
175252ca428SRobert Watson     &tcp_rlock_atfirst, 0, "");
176252ca428SRobert Watson 
177252ca428SRobert Watson int	tcp_wlock_atfirst;
178252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcp_wlock_atfirst, CTLFLAG_RD,
179252ca428SRobert Watson     &tcp_wlock_atfirst, 0, "");
180252ca428SRobert Watson 
181252ca428SRobert Watson int	tcp_wlock_upgraded;
182252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, wlock_upgraded, CTLFLAG_RD,
183252ca428SRobert Watson     &tcp_wlock_upgraded, 0, "");
184252ca428SRobert Watson 
185252ca428SRobert Watson int	tcp_wlock_relocked;
186252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, wlock_relocked, CTLFLAG_RD,
187252ca428SRobert Watson     &tcp_wlock_relocked, 0, "");
188252ca428SRobert Watson 
189252ca428SRobert Watson int	tcp_wlock_looped;
190252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, wlock_looped, CTLFLAG_RD,
191252ca428SRobert Watson     &tcp_wlock_looped, 0, "");
192252ca428SRobert Watson 
19344e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
19415bd2b43SDavid Greenman struct inpcbhead tcb;
19515bd2b43SDavid Greenman struct inpcbinfo tcbinfo;
19644e33a07SMarko Zec #endif
19744e33a07SMarko Zec #define	tcb6	tcb  /* for KAME src sync over BSD*'s */
198df8bae1dSRodney W. Grimes 
1995a53ca16SPaul Saab static void	 tcp_dooptions(struct tcpopt *, u_char *, int, int);
200679d9708SAndre Oppermann static void	 tcp_do_segment(struct mbuf *, struct tcphdr *,
201252ca428SRobert Watson 		     struct socket *, struct tcpcb *, int, int, uint8_t,
202252ca428SRobert Watson 		     int);
203302ce8d6SAndre Oppermann static void	 tcp_dropwithreset(struct mbuf *, struct tcphdr *,
204302ce8d6SAndre Oppermann 		     struct tcpcb *, int, int);
2054d77a549SAlfred Perlstein static void	 tcp_pulloutofband(struct socket *,
2064d77a549SAlfred Perlstein 		     struct tcphdr *, struct mbuf *, int);
2074d77a549SAlfred Perlstein static void	 tcp_xmit_timer(struct tcpcb *, int);
208c068736aSJeffrey Hsu static void	 tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *);
209f2512ba1SRui Paulo static void inline
210f2512ba1SRui Paulo 		 tcp_congestion_exp(struct tcpcb *);
211f2512ba1SRui Paulo 
212f2512ba1SRui Paulo static void inline
213f2512ba1SRui Paulo tcp_congestion_exp(struct tcpcb *tp)
214f2512ba1SRui Paulo {
215f2512ba1SRui Paulo 	u_int win;
216f2512ba1SRui Paulo 
217f2512ba1SRui Paulo 	win = min(tp->snd_wnd, tp->snd_cwnd) /
218f2512ba1SRui Paulo 	    2 / tp->t_maxseg;
219f2512ba1SRui Paulo 	if (win < 2)
220f2512ba1SRui Paulo 		win = 2;
221f2512ba1SRui Paulo 	tp->snd_ssthresh = win * tp->t_maxseg;
222f2512ba1SRui Paulo 	ENTER_FASTRECOVERY(tp);
223f2512ba1SRui Paulo 	tp->snd_recover = tp->snd_max;
224f2512ba1SRui Paulo 	if (tp->t_flags & TF_ECN_PERMIT)
225f2512ba1SRui Paulo 		tp->t_flags |= TF_ECN_SND_CWR;
226f2512ba1SRui Paulo }
2270312fbe9SPoul-Henning Kamp 
228fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
229fb59c426SYoshinobu Inoue #ifdef INET6
230fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \
231fb59c426SYoshinobu Inoue do { \
232fb59c426SYoshinobu Inoue 	if ((tp) && (tp)->t_inpcb && \
23397d8d152SAndre Oppermann 	    ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \
23497d8d152SAndre Oppermann 		nd6_nud_hint(NULL, NULL, 0); \
235fb59c426SYoshinobu Inoue } while (0)
236fb59c426SYoshinobu Inoue #else
237fb59c426SYoshinobu Inoue #define ND6_HINT(tp)
238fb59c426SYoshinobu Inoue #endif
239df8bae1dSRodney W. Grimes 
240df8bae1dSRodney W. Grimes /*
241262c1c1aSMatthew Dillon  * Indicate whether this ack should be delayed.  We can delay the ack if
242262c1c1aSMatthew Dillon  *	- there is no delayed ack timer in progress and
243262c1c1aSMatthew Dillon  *	- our last ack wasn't a 0-sized window.  We never want to delay
2443bfd6421SJonathan Lemon  *	  the ack that opens up a 0-sized window and
2453bfd6421SJonathan Lemon  *		- delayed acks are enabled or
2463bfd6421SJonathan Lemon  *		- this is a half-synchronized T/TCP connection.
247d8c85a26SJonathan Lemon  */
248d8c85a26SJonathan Lemon #define DELAY_ACK(tp)							\
249b8152ba7SAndre Oppermann 	((!tcp_timer_active(tp, TT_DELACK) &&				\
250a14c749fSJonathan Lemon 	    (tp->t_flags & TF_RXWIN0SENT) == 0) &&			\
251603724d3SBjoern A. Zeeb 	    (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
252d8c85a26SJonathan Lemon 
253df8bae1dSRodney W. Grimes /*
2548d573cc1SAndre Oppermann  * TCP input handling is split into multiple parts:
2558d573cc1SAndre Oppermann  *   tcp6_input is a thin wrapper around tcp_input for the extended
2568d573cc1SAndre Oppermann  *	ip6_protox[] call format in ip6_input
2578d573cc1SAndre Oppermann  *   tcp_input handles primary segment validation, inpcb lookup and
2588d573cc1SAndre Oppermann  *	SYN processing on listen sockets
2598d573cc1SAndre Oppermann  *   tcp_do_segment processes the ACK and text of the segment for
2608d573cc1SAndre Oppermann  *	establishing, established and closing connections
261df8bae1dSRodney W. Grimes  */
262fb59c426SYoshinobu Inoue #ifdef INET6
263fb59c426SYoshinobu Inoue int
264ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto)
265fb59c426SYoshinobu Inoue {
2668b615593SMarko Zec 	INIT_VNET_INET6(curvnet);
267ad3f9ab3SAndre Oppermann 	struct mbuf *m = *mp;
26833841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
269fb59c426SYoshinobu Inoue 
270fb59c426SYoshinobu Inoue 	IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
271fb59c426SYoshinobu Inoue 
272fb59c426SYoshinobu Inoue 	/*
273fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
274fb59c426SYoshinobu Inoue 	 * better place to put this in?
275fb59c426SYoshinobu Inoue 	 */
27633841545SHajimu UMEMOTO 	ia6 = ip6_getdstifaddr(m);
27733841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
278fb59c426SYoshinobu Inoue 		struct ip6_hdr *ip6;
279fb59c426SYoshinobu Inoue 
280fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
281fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
282fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
283fb59c426SYoshinobu Inoue 		return IPPROTO_DONE;
284fb59c426SYoshinobu Inoue 	}
285fb59c426SYoshinobu Inoue 
286f0ffb944SJulian Elischer 	tcp_input(m, *offp);
287fb59c426SYoshinobu Inoue 	return IPPROTO_DONE;
288fb59c426SYoshinobu Inoue }
289fb59c426SYoshinobu Inoue #endif
290fb59c426SYoshinobu Inoue 
291df8bae1dSRodney W. Grimes void
292ad3f9ab3SAndre Oppermann tcp_input(struct mbuf *m, int off0)
293df8bae1dSRodney W. Grimes {
2948b615593SMarko Zec 	INIT_VNET_INET(curvnet);
2958b615593SMarko Zec #ifdef INET6
2968b615593SMarko Zec 	INIT_VNET_INET6(curvnet);
2978b615593SMarko Zec #endif
2988b615593SMarko Zec #ifdef IPSEC
2998b615593SMarko Zec 	INIT_VNET_IPSEC(curvnet);
3008b615593SMarko Zec #endif
301ad3f9ab3SAndre Oppermann 	struct tcphdr *th;
302ad3f9ab3SAndre Oppermann 	struct ip *ip = NULL;
303ad3f9ab3SAndre Oppermann 	struct ipovly *ipov;
304ad3f9ab3SAndre Oppermann 	struct inpcb *inp = NULL;
305302ce8d6SAndre Oppermann 	struct tcpcb *tp = NULL;
306302ce8d6SAndre Oppermann 	struct socket *so = NULL;
307e79adb8eSGarrett Wollman 	u_char *optp = NULL;
30826f9a767SRodney W. Grimes 	int optlen = 0;
309a0194ef1SBruce M Simpson 	int len, tlen, off;
310fb59c426SYoshinobu Inoue 	int drop_hdrlen;
311ad3f9ab3SAndre Oppermann 	int thflags;
312302ce8d6SAndre Oppermann 	int rstreason = 0;	/* For badport_bandlim accounting purposes */
313f2512ba1SRui Paulo 	uint8_t iptos;
3149b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
3159b932e9eSAndre Oppermann 	struct m_tag *fwd_tag;
3169b932e9eSAndre Oppermann #endif
317c068736aSJeffrey Hsu #ifdef INET6
318302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6 = NULL;
319c068736aSJeffrey Hsu 	int isipv6;
320c068736aSJeffrey Hsu #else
321a160e630SAndre Oppermann 	const void *ip6 = NULL;
322c068736aSJeffrey Hsu 	const int isipv6 = 0;
323c068736aSJeffrey Hsu #endif
324302ce8d6SAndre Oppermann 	struct tcpopt to;		/* options in this segment */
325a160e630SAndre Oppermann 	char *s = NULL;			/* address and port logging */
326252ca428SRobert Watson 	int ti_locked;
327252ca428SRobert Watson #define	TI_UNLOCKED	1
328252ca428SRobert Watson #define	TI_RLOCKED	2
329252ca428SRobert Watson #define	TI_WLOCKED	3
330f76fcf6dSJeffrey Hsu 
331610ee2f9SDavid Greenman #ifdef TCPDEBUG
332c068736aSJeffrey Hsu 	/*
333c068736aSJeffrey Hsu 	 * The size of tcp_saveipgen must be the size of the max ip header,
334c068736aSJeffrey Hsu 	 * now IPv6.
335c068736aSJeffrey Hsu 	 */
33614739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
337410bb1bfSLuigi Rizzo 	struct tcphdr tcp_savetcp;
338610ee2f9SDavid Greenman 	short ostate = 0;
339610ee2f9SDavid Greenman #endif
340c068736aSJeffrey Hsu 
341fb59c426SYoshinobu Inoue #ifdef INET6
342fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
343fb59c426SYoshinobu Inoue #endif
344a0292f23SGarrett Wollman 
345302ce8d6SAndre Oppermann 	to.to_flags = 0;
346603724d3SBjoern A. Zeeb 	V_tcpstat.tcps_rcvtotal++;
347fb59c426SYoshinobu Inoue 
348fb59c426SYoshinobu Inoue 	if (isipv6) {
34904d3a452SHajimu UMEMOTO #ifdef INET6
3508d573cc1SAndre Oppermann 		/* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */
351fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
352fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
353fb59c426SYoshinobu Inoue 		if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) {
354603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_rcvbadsum++;
355fb59c426SYoshinobu Inoue 			goto drop;
356fb59c426SYoshinobu Inoue 		}
357fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
35833841545SHajimu UMEMOTO 
35933841545SHajimu UMEMOTO 		/*
36033841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
36133841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
36233841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
36333841545SHajimu UMEMOTO 		 *
36433841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
36533841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
36633841545SHajimu UMEMOTO 		 */
36733841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
36833841545SHajimu UMEMOTO 			/* XXX stat */
36933841545SHajimu UMEMOTO 			goto drop;
37033841545SHajimu UMEMOTO 		}
37104d3a452SHajimu UMEMOTO #else
3728d573cc1SAndre Oppermann 		th = NULL;		/* XXX: Avoid compiler warning. */
37304d3a452SHajimu UMEMOTO #endif
374c068736aSJeffrey Hsu 	} else {
375df8bae1dSRodney W. Grimes 		/*
376df8bae1dSRodney W. Grimes 		 * Get IP and TCP header together in first mbuf.
377df8bae1dSRodney W. Grimes 		 * Note: IP leaves IP header in first mbuf.
378df8bae1dSRodney W. Grimes 		 */
379fb59c426SYoshinobu Inoue 		if (off0 > sizeof (struct ip)) {
380df8bae1dSRodney W. Grimes 			ip_stripoptions(m, (struct mbuf *)0);
381fb59c426SYoshinobu Inoue 			off0 = sizeof(struct ip);
382fb59c426SYoshinobu Inoue 		}
383df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof (struct tcpiphdr)) {
3844dfdffe9SAndre Oppermann 			if ((m = m_pullup(m, sizeof (struct tcpiphdr)))
3854dfdffe9SAndre Oppermann 			    == NULL) {
386603724d3SBjoern A. Zeeb 				V_tcpstat.tcps_rcvshort++;
387df8bae1dSRodney W. Grimes 				return;
388df8bae1dSRodney W. Grimes 			}
389df8bae1dSRodney W. Grimes 		}
390fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
391fb59c426SYoshinobu Inoue 		ipov = (struct ipovly *)ip;
392db4f9cc7SJonathan Lemon 		th = (struct tcphdr *)((caddr_t)ip + off0);
393db4f9cc7SJonathan Lemon 		tlen = ip->ip_len;
394df8bae1dSRodney W. Grimes 
395db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
396db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
397db4f9cc7SJonathan Lemon 				th->th_sum = m->m_pkthdr.csum_data;
398db4f9cc7SJonathan Lemon 			else
399db4f9cc7SJonathan Lemon 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
400c068736aSJeffrey Hsu 						ip->ip_dst.s_addr,
401c068736aSJeffrey Hsu 						htonl(m->m_pkthdr.csum_data +
402c068736aSJeffrey Hsu 							ip->ip_len +
403c068736aSJeffrey Hsu 							IPPROTO_TCP));
404db4f9cc7SJonathan Lemon 			th->th_sum ^= 0xffff;
4053c653157SHartmut Brandt #ifdef TCPDEBUG
4063c653157SHartmut Brandt 			ipov->ih_len = (u_short)tlen;
4073c653157SHartmut Brandt 			ipov->ih_len = htons(ipov->ih_len);
4083c653157SHartmut Brandt #endif
409db4f9cc7SJonathan Lemon 		} else {
410df8bae1dSRodney W. Grimes 			/*
411df8bae1dSRodney W. Grimes 			 * Checksum extended TCP header and data.
412df8bae1dSRodney W. Grimes 			 */
413df8bae1dSRodney W. Grimes 			len = sizeof (struct ip) + tlen;
414fb59c426SYoshinobu Inoue 			bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
415fb59c426SYoshinobu Inoue 			ipov->ih_len = (u_short)tlen;
416fd8e4ebcSMike Barcroft 			ipov->ih_len = htons(ipov->ih_len);
417fb59c426SYoshinobu Inoue 			th->th_sum = in_cksum(m, len);
418db4f9cc7SJonathan Lemon 		}
419fb59c426SYoshinobu Inoue 		if (th->th_sum) {
420603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_rcvbadsum++;
421df8bae1dSRodney W. Grimes 			goto drop;
422df8bae1dSRodney W. Grimes 		}
423fb59c426SYoshinobu Inoue 		/* Re-initialization for later version check */
424fb59c426SYoshinobu Inoue 		ip->ip_v = IPVERSION;
425fb59c426SYoshinobu Inoue 	}
426df8bae1dSRodney W. Grimes 
427f2512ba1SRui Paulo #ifdef INET6
428f2512ba1SRui Paulo 	if (isipv6)
429f2512ba1SRui Paulo 		iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
430f2512ba1SRui Paulo 	else
431f2512ba1SRui Paulo #endif
432f2512ba1SRui Paulo 		iptos = ip->ip_tos;
433f2512ba1SRui Paulo 
434df8bae1dSRodney W. Grimes 	/*
435df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
436df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
437df8bae1dSRodney W. Grimes 	 */
438fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
439df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
440603724d3SBjoern A. Zeeb 		V_tcpstat.tcps_rcvbadoff++;
441df8bae1dSRodney W. Grimes 		goto drop;
442df8bae1dSRodney W. Grimes 	}
443fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
444df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
445fb59c426SYoshinobu Inoue 		if (isipv6) {
44604d3a452SHajimu UMEMOTO #ifdef INET6
447fb59c426SYoshinobu Inoue 			IP6_EXTHDR_CHECK(m, off0, off, );
448fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
449fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
45004d3a452SHajimu UMEMOTO #endif
451c068736aSJeffrey Hsu 		} else {
452df8bae1dSRodney W. Grimes 			if (m->m_len < sizeof(struct ip) + off) {
453c068736aSJeffrey Hsu 				if ((m = m_pullup(m, sizeof (struct ip) + off))
4544dfdffe9SAndre Oppermann 				    == NULL) {
455603724d3SBjoern A. Zeeb 					V_tcpstat.tcps_rcvshort++;
456df8bae1dSRodney W. Grimes 					return;
457df8bae1dSRodney W. Grimes 				}
458fb59c426SYoshinobu Inoue 				ip = mtod(m, struct ip *);
459fb59c426SYoshinobu Inoue 				ipov = (struct ipovly *)ip;
460fb59c426SYoshinobu Inoue 				th = (struct tcphdr *)((caddr_t)ip + off0);
461fb59c426SYoshinobu Inoue 			}
462df8bae1dSRodney W. Grimes 		}
463df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
464fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
465df8bae1dSRodney W. Grimes 	}
466fb59c426SYoshinobu Inoue 	thflags = th->th_flags;
467df8bae1dSRodney W. Grimes 
468e46cd3d4SDag-Erling Smørgrav 	/*
469df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
470df8bae1dSRodney W. Grimes 	 */
471fd8e4ebcSMike Barcroft 	th->th_seq = ntohl(th->th_seq);
472fd8e4ebcSMike Barcroft 	th->th_ack = ntohl(th->th_ack);
473fd8e4ebcSMike Barcroft 	th->th_win = ntohs(th->th_win);
474fd8e4ebcSMike Barcroft 	th->th_urp = ntohs(th->th_urp);
475df8bae1dSRodney W. Grimes 
476df8bae1dSRodney W. Grimes 	/*
477794235b7SAndre Oppermann 	 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options.
478755c1f07SAndras Olah 	 */
479fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
480755c1f07SAndras Olah 
481755c1f07SAndras Olah 	/*
482252ca428SRobert Watson 	 * Locate pcb for segment, which requires a lock on tcbinfo.
483d15fb965SRobert Watson 	 * Optimisticaly acquire a global read lock rather than a write lock
484d15fb965SRobert Watson 	 * unless header flags necessarily imply a state change.  There are
485d15fb965SRobert Watson 	 * two cases where we might discover later we need a write lock
486d15fb965SRobert Watson 	 * despite the flags: ACKs moving a connection out of the syncache,
487d15fb965SRobert Watson 	 * and ACKs for a connection in TIMEWAIT.
488df8bae1dSRodney W. Grimes 	 */
489252ca428SRobert Watson 	if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 ||
490252ca428SRobert Watson 	    tcp_read_locking == 0) {
491603724d3SBjoern A. Zeeb 		INP_INFO_WLOCK(&V_tcbinfo);
492252ca428SRobert Watson 		ti_locked = TI_WLOCKED;
493252ca428SRobert Watson 		tcp_wlock_atfirst++;
494252ca428SRobert Watson 	} else {
495252ca428SRobert Watson 		INP_INFO_RLOCK(&V_tcbinfo);
496252ca428SRobert Watson 		ti_locked = TI_RLOCKED;
497252ca428SRobert Watson 		tcp_rlock_atfirst++;
498252ca428SRobert Watson 	}
499252ca428SRobert Watson 
500df8bae1dSRodney W. Grimes findpcb:
501252ca428SRobert Watson #ifdef INVARIANTS
502252ca428SRobert Watson 	if (ti_locked == TI_RLOCKED)
503252ca428SRobert Watson 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
504252ca428SRobert Watson 	else if (ti_locked == TI_WLOCKED)
505603724d3SBjoern A. Zeeb 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
506252ca428SRobert Watson 	else
507252ca428SRobert Watson 		panic("%s: findpcb ti_locked %d\n", __func__, ti_locked);
508252ca428SRobert Watson #endif
509252ca428SRobert Watson 
5109b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
5118d573cc1SAndre Oppermann 	/*
5128d573cc1SAndre Oppermann 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
5138d573cc1SAndre Oppermann 	 */
5149b932e9eSAndre Oppermann 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
5159b932e9eSAndre Oppermann 
5169b932e9eSAndre Oppermann 	if (fwd_tag != NULL && isipv6 == 0) {	/* IPv6 support is not yet */
5179b932e9eSAndre Oppermann 		struct sockaddr_in *next_hop;
5189b932e9eSAndre Oppermann 
5199b932e9eSAndre Oppermann 		next_hop = (struct sockaddr_in *)(fwd_tag+1);
520f9e354dfSJulian Elischer 		/*
5212b25acc1SLuigi Rizzo 		 * Transparently forwarded. Pretend to be the destination.
522f9e354dfSJulian Elischer 		 * already got one like this?
523f9e354dfSJulian Elischer 		 */
524603724d3SBjoern A. Zeeb 		inp = in_pcblookup_hash(&V_tcbinfo,
5259b932e9eSAndre Oppermann 					ip->ip_src, th->th_sport,
526c068736aSJeffrey Hsu 					ip->ip_dst, th->th_dport,
527c068736aSJeffrey Hsu 					0, m->m_pkthdr.rcvif);
528f9e354dfSJulian Elischer 		if (!inp) {
5299b932e9eSAndre Oppermann 			/* It's new.  Try to find the ambushing socket. */
530603724d3SBjoern A. Zeeb 			inp = in_pcblookup_hash(&V_tcbinfo,
531fb59c426SYoshinobu Inoue 						ip->ip_src, th->th_sport,
5322b25acc1SLuigi Rizzo 						next_hop->sin_addr,
533c068736aSJeffrey Hsu 						next_hop->sin_port ?
534c068736aSJeffrey Hsu 						    ntohs(next_hop->sin_port) :
535c068736aSJeffrey Hsu 						    th->th_dport,
536421d8aa6SBjoern A. Zeeb 						INPLOOKUP_WILDCARD,
537421d8aa6SBjoern A. Zeeb 						m->m_pkthdr.rcvif);
538f9e354dfSJulian Elischer 		}
5399b932e9eSAndre Oppermann 		/* Remove the tag from the packet.  We don't need it anymore. */
5409b932e9eSAndre Oppermann 		m_tag_delete(m, fwd_tag);
541b10fbdeaSAndre Oppermann 	} else
5429b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */
543b10fbdeaSAndre Oppermann 	{
54404d3a452SHajimu UMEMOTO 		if (isipv6) {
54504d3a452SHajimu UMEMOTO #ifdef INET6
546603724d3SBjoern A. Zeeb 			inp = in6_pcblookup_hash(&V_tcbinfo,
547c068736aSJeffrey Hsu 						 &ip6->ip6_src, th->th_sport,
548c068736aSJeffrey Hsu 						 &ip6->ip6_dst, th->th_dport,
549421d8aa6SBjoern A. Zeeb 						 INPLOOKUP_WILDCARD,
550421d8aa6SBjoern A. Zeeb 						 m->m_pkthdr.rcvif);
55104d3a452SHajimu UMEMOTO #endif
55204d3a452SHajimu UMEMOTO 		} else
553603724d3SBjoern A. Zeeb 			inp = in_pcblookup_hash(&V_tcbinfo,
554c068736aSJeffrey Hsu 						ip->ip_src, th->th_sport,
555c068736aSJeffrey Hsu 						ip->ip_dst, th->th_dport,
556421d8aa6SBjoern A. Zeeb 						INPLOOKUP_WILDCARD,
557421d8aa6SBjoern A. Zeeb 						m->m_pkthdr.rcvif);
558fb59c426SYoshinobu Inoue 	}
559f9e354dfSJulian Elischer 
560df8bae1dSRodney W. Grimes 	/*
561574b6964SAndre Oppermann 	 * If the INPCB does not exist then all data in the incoming
562574b6964SAndre Oppermann 	 * segment is discarded and an appropriate RST is sent back.
5638b07e49aSJulian Elischer 	 * XXX MRT Send RST using which routing table?
564df8bae1dSRodney W. Grimes 	 */
565816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
566574b6964SAndre Oppermann 		/*
567574b6964SAndre Oppermann 		 * Log communication attempts to ports that are not
568574b6964SAndre Oppermann 		 * in use.
569574b6964SAndre Oppermann 		 */
570574b6964SAndre Oppermann 		if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) ||
571574b6964SAndre Oppermann 		    tcp_log_in_vain == 2) {
5729fb5d4c0SPeter Wemm 			if ((s = tcp_log_addrs(NULL, th, (void *)ip, ip6)))
573df541e5fSAndre Oppermann 				log(LOG_INFO, "%s; %s: Connection attempt "
574df541e5fSAndre Oppermann 				    "to closed port\n", s, __func__);
5752e4e1b4cSGeoff Rehmet 		}
576574b6964SAndre Oppermann 		/*
577574b6964SAndre Oppermann 		 * When blackholing do not respond with a RST but
578574b6964SAndre Oppermann 		 * completely ignore the segment and drop it.
579574b6964SAndre Oppermann 		 */
580603724d3SBjoern A. Zeeb 		if ((V_blackhole == 1 && (thflags & TH_SYN)) ||
581603724d3SBjoern A. Zeeb 		    V_blackhole == 2)
5821929eae1SAndre Oppermann 			goto dropunlock;
583574b6964SAndre Oppermann 
584a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
585a57815efSBosko Milekic 		goto dropwithreset;
586816a3d83SPoul-Henning Kamp 	}
5878501a69cSRobert Watson 	INP_WLOCK(inp);
588936cd18dSAndre Oppermann 
589a2589465SKen Smith #ifdef IPSEC
590a2589465SKen Smith #ifdef INET6
591a2589465SKen Smith 	if (isipv6 && ipsec6_in_reject(m, inp)) {
592603724d3SBjoern A. Zeeb 		V_ipsec6stat.in_polvio++;
593a2589465SKen Smith 		goto dropunlock;
594a2589465SKen Smith 	} else
595a2589465SKen Smith #endif /* INET6 */
596a2589465SKen Smith 	if (ipsec4_in_reject(m, inp) != 0) {
597603724d3SBjoern A. Zeeb 		V_ipsec4stat.in_polvio++;
598a2589465SKen Smith 		goto dropunlock;
599a2589465SKen Smith 	}
600a2589465SKen Smith #endif /* IPSEC */
601a2589465SKen Smith 
6028d573cc1SAndre Oppermann 	/*
6038d573cc1SAndre Oppermann 	 * Check the minimum TTL for socket.
6048d573cc1SAndre Oppermann 	 */
60534f83c52SGeorge V. Neville-Neil 	if (inp->inp_ip_minttl != 0) {
60634f83c52SGeorge V. Neville-Neil #ifdef INET6
60734f83c52SGeorge V. Neville-Neil 		if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim)
608302ce8d6SAndre Oppermann 			goto dropunlock;
60902707462SAndre Oppermann 		else
61034f83c52SGeorge V. Neville-Neil #endif
61134f83c52SGeorge V. Neville-Neil 		if (inp->inp_ip_minttl > ip->ip_ttl)
612302ce8d6SAndre Oppermann 			goto dropunlock;
61334f83c52SGeorge V. Neville-Neil 	}
614936cd18dSAndre Oppermann 
615340c35deSJonathan Lemon 	/*
616252ca428SRobert Watson 	 * A previous connection in TIMEWAIT state is supposed to catch stray
617252ca428SRobert Watson 	 * or duplicate segments arriving late.  If this segment was a
618252ca428SRobert Watson 	 * legitimate new connection attempt the old INPCB gets removed and
619252ca428SRobert Watson 	 * we can try again to find a listening socket.
620252ca428SRobert Watson 	 *
621252ca428SRobert Watson 	 * At this point, due to earlier optimism, we may hold a read lock on
622252ca428SRobert Watson 	 * the inpcbinfo, rather than a write lock.  If so, we need to
623252ca428SRobert Watson 	 * upgrade, or if that fails, acquire a reference on the inpcb, drop
624252ca428SRobert Watson 	 * all locks, acquire a global write lock, and then re-acquire the
625252ca428SRobert Watson 	 * inpcb lock.  We may at that point discover that another thread has
626252ca428SRobert Watson 	 * tried to free the inpcb, in which case we need to loop back and
627252ca428SRobert Watson 	 * try to find a new inpcb to deliver to.
628340c35deSJonathan Lemon 	 */
629794235b7SAndre Oppermann 	if (inp->inp_vflag & INP_TIMEWAIT) {
630252ca428SRobert Watson 		KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED,
631252ca428SRobert Watson 		    ("%s: INP_TIMEWAIT ti_locked %d", __func__, ti_locked));
632252ca428SRobert Watson 
633252ca428SRobert Watson 		if (ti_locked == TI_RLOCKED) {
634252ca428SRobert Watson 			if (rw_try_upgrade(&V_tcbinfo.ipi_lock) == 0) {
635252ca428SRobert Watson 				in_pcbref(inp);
636252ca428SRobert Watson 				INP_WUNLOCK(inp);
637252ca428SRobert Watson 				INP_INFO_RUNLOCK(&V_tcbinfo);
638252ca428SRobert Watson 				INP_INFO_WLOCK(&V_tcbinfo);
639252ca428SRobert Watson 				ti_locked = TI_WLOCKED;
640252ca428SRobert Watson 				INP_WLOCK(inp);
641252ca428SRobert Watson 				if (in_pcbrele(inp)) {
642252ca428SRobert Watson 					tcp_wlock_looped++;
643252ca428SRobert Watson 					inp = NULL;
644252ca428SRobert Watson 					goto findpcb;
645252ca428SRobert Watson 				}
646252ca428SRobert Watson 				tcp_wlock_relocked++;
647252ca428SRobert Watson 			} else {
648252ca428SRobert Watson 				ti_locked = TI_WLOCKED;
649252ca428SRobert Watson 				tcp_wlock_upgraded++;
650252ca428SRobert Watson 			}
651252ca428SRobert Watson 		}
652252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
653252ca428SRobert Watson 
654340c35deSJonathan Lemon 		if (thflags & TH_SYN)
655f72167f4SAndre Oppermann 			tcp_dooptions(&to, optp, optlen, TO_SYN);
6568d573cc1SAndre Oppermann 		/*
6578d573cc1SAndre Oppermann 		 * NB: tcp_twcheck unlocks the INP and frees the mbuf.
6588d573cc1SAndre Oppermann 		 */
6592104448fSAndre Oppermann 		if (tcp_twcheck(inp, &to, th, m, tlen))
660340c35deSJonathan Lemon 			goto findpcb;
661603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
662340c35deSJonathan Lemon 		return;
663340c35deSJonathan Lemon 	}
664794235b7SAndre Oppermann 	/*
665794235b7SAndre Oppermann 	 * The TCPCB may no longer exist if the connection is winding
666794235b7SAndre Oppermann 	 * down or it is in the CLOSED state.  Either way we drop the
667794235b7SAndre Oppermann 	 * segment and send an appropriate response.
668794235b7SAndre Oppermann 	 */
669df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
670a160e630SAndre Oppermann 	if (tp == NULL || tp->t_state == TCPS_CLOSED) {
671a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
672a57815efSBosko Milekic 		goto dropwithreset;
67309f81a46SBosko Milekic 	}
674df8bae1dSRodney W. Grimes 
675252ca428SRobert Watson 	/*
676252ca428SRobert Watson 	 * We've identified a valid inpcb, but it could be that we need an
677252ca428SRobert Watson 	 * inpcbinfo write lock and have only a read lock.  In this case,
678252ca428SRobert Watson 	 * attempt to upgrade/relock using the same strategy as the TIMEWAIT
679252ca428SRobert Watson 	 * case above.
680252ca428SRobert Watson 	 */
681252ca428SRobert Watson 	if (tp->t_state != TCPS_ESTABLISHED ||
682252ca428SRobert Watson 	    (thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 ||
683252ca428SRobert Watson 	    tcp_read_locking == 0) {
684252ca428SRobert Watson 		KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED,
685252ca428SRobert Watson 		    ("%s: upgrade check ti_locked %d", __func__, ti_locked));
686252ca428SRobert Watson 
687252ca428SRobert Watson 		if (ti_locked == TI_RLOCKED) {
688252ca428SRobert Watson 			if (rw_try_upgrade(&V_tcbinfo.ipi_lock) == 0) {
689252ca428SRobert Watson 				in_pcbref(inp);
690252ca428SRobert Watson 				INP_WUNLOCK(inp);
691252ca428SRobert Watson 				INP_INFO_RUNLOCK(&V_tcbinfo);
692252ca428SRobert Watson 				INP_INFO_WLOCK(&V_tcbinfo);
693252ca428SRobert Watson 				ti_locked = TI_WLOCKED;
694252ca428SRobert Watson 				INP_WLOCK(inp);
695252ca428SRobert Watson 				if (in_pcbrele(inp)) {
696252ca428SRobert Watson 					tcp_wlock_looped++;
697252ca428SRobert Watson 					inp = NULL;
698252ca428SRobert Watson 					goto findpcb;
699252ca428SRobert Watson 				}
700252ca428SRobert Watson 				tcp_wlock_relocked++;
701252ca428SRobert Watson 			} else {
702252ca428SRobert Watson 				ti_locked = TI_WLOCKED;
703252ca428SRobert Watson 				tcp_wlock_upgraded++;
704252ca428SRobert Watson 			}
705252ca428SRobert Watson 		}
706252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
707252ca428SRobert Watson 	}
708252ca428SRobert Watson 
709c488362eSRobert Watson #ifdef MAC
7108501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
71130d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, m))
712302ce8d6SAndre Oppermann 		goto dropunlock;
713c488362eSRobert Watson #endif
714a557af22SRobert Watson 	so = inp->inp_socket;
715302ce8d6SAndre Oppermann 	KASSERT(so != NULL, ("%s: so == NULL", __func__));
716610ee2f9SDavid Greenman #ifdef TCPDEBUG
717df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG) {
718df8bae1dSRodney W. Grimes 		ostate = tp->t_state;
719d30d90dcSMaxim Konovalov 		if (isipv6) {
72010fe523eSMaxim Konovalov #ifdef INET6
721540e8b7eSJeffrey Hsu 			bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6));
722d30d90dcSMaxim Konovalov #endif
723d30d90dcSMaxim Konovalov 		} else
724540e8b7eSJeffrey Hsu 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
725fb59c426SYoshinobu Inoue 		tcp_savetcp = *th;
726df8bae1dSRodney W. Grimes 	}
727610ee2f9SDavid Greenman #endif
728db33b3e6SAndre Oppermann 	/*
729db33b3e6SAndre Oppermann 	 * When the socket is accepting connections (the INPCB is in LISTEN
730db33b3e6SAndre Oppermann 	 * state) we look into the SYN cache if this is a new connection
731db33b3e6SAndre Oppermann 	 * attempt or the completion of a previous one.
732db33b3e6SAndre Oppermann 	 */
733540e8b7eSJeffrey Hsu 	if (so->so_options & SO_ACCEPTCONN) {
734540e8b7eSJeffrey Hsu 		struct in_conninfo inc;
735540e8b7eSJeffrey Hsu 
7367824d002SAndre Oppermann 		KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but "
7377824d002SAndre Oppermann 		    "tp not listening", __func__));
7387824d002SAndre Oppermann 
7398bfb1918SAndre Oppermann 		bzero(&inc, sizeof(inc));
740302ce8d6SAndre Oppermann #ifdef INET6
741be2ac88cSJonathan Lemon 		if (isipv6) {
742dcdb4371SBjoern A. Zeeb 			inc.inc_flags |= INC_ISIPV6;
743be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
744be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
745302ce8d6SAndre Oppermann 		} else
746302ce8d6SAndre Oppermann #endif
747302ce8d6SAndre Oppermann 		{
748be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
749be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
750be2ac88cSJonathan Lemon 		}
751be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
752be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
753be2ac88cSJonathan Lemon 
754fb59c426SYoshinobu Inoue 		/*
7558d573cc1SAndre Oppermann 		 * Check for an existing connection attempt in syncache if
7568d573cc1SAndre Oppermann 		 * the flag is only ACK.  A successful lookup creates a new
7578d573cc1SAndre Oppermann 		 * socket appended to the listen queue in SYN_RECEIVED state.
758fb59c426SYoshinobu Inoue 		 */
759be2ac88cSJonathan Lemon 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
760bf6d304aSAndre Oppermann 			/*
761bf6d304aSAndre Oppermann 			 * Parse the TCP options here because
762bf6d304aSAndre Oppermann 			 * syncookies need access to the reflected
763bf6d304aSAndre Oppermann 			 * timestamp.
764bf6d304aSAndre Oppermann 			 */
765bf6d304aSAndre Oppermann 			tcp_dooptions(&to, optp, optlen, 0);
7669fa198beSAndre Oppermann 			/*
7679fa198beSAndre Oppermann 			 * NB: syncache_expand() doesn't unlock
7689fa198beSAndre Oppermann 			 * inp and tcpinfo locks.
7699fa198beSAndre Oppermann 			 */
770bf6d304aSAndre Oppermann 			if (!syncache_expand(&inc, &to, th, &so, m)) {
7714195b4afSPaul Traina 				/*
772e207f800SAndre Oppermann 				 * No syncache entry or ACK was not
773be2ac88cSJonathan Lemon 				 * for our SYN/ACK.  Send a RST.
7748d573cc1SAndre Oppermann 				 * NB: syncache did its own logging
7758d573cc1SAndre Oppermann 				 * of the failure cause.
7764195b4afSPaul Traina 				 */
777be2ac88cSJonathan Lemon 				rstreason = BANDLIM_RST_OPENPORT;
778be2ac88cSJonathan Lemon 				goto dropwithreset;
779be2ac88cSJonathan Lemon 			}
780f76fcf6dSJeffrey Hsu 			if (so == NULL) {
781be2ac88cSJonathan Lemon 				/*
782e207f800SAndre Oppermann 				 * We completed the 3-way handshake
783e207f800SAndre Oppermann 				 * but could not allocate a socket
784e207f800SAndre Oppermann 				 * either due to memory shortage,
785e207f800SAndre Oppermann 				 * listen queue length limits or
786a160e630SAndre Oppermann 				 * global socket limits.  Send RST
787a160e630SAndre Oppermann 				 * or wait and have the remote end
788a160e630SAndre Oppermann 				 * retransmit the ACK for another
789a160e630SAndre Oppermann 				 * try.
790be2ac88cSJonathan Lemon 				 */
791a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
792a160e630SAndre Oppermann 					log(LOG_DEBUG, "%s; %s: Listen socket: "
7938d573cc1SAndre Oppermann 					    "Socket allocation failed due to "
7948d573cc1SAndre Oppermann 					    "limits or memory shortage, %s\n",
795ac957cd2SJulian Elischer 					    s, __func__,
796ac957cd2SJulian Elischer 					    V_tcp_sc_rst_sock_fail ?
797ac957cd2SJulian Elischer 					    "sending RST" : "try again");
798603724d3SBjoern A. Zeeb 				if (V_tcp_sc_rst_sock_fail) {
799e207f800SAndre Oppermann 					rstreason = BANDLIM_UNLIMITED;
800e207f800SAndre Oppermann 					goto dropwithreset;
801a160e630SAndre Oppermann 				} else
802a160e630SAndre Oppermann 					goto dropunlock;
803f76fcf6dSJeffrey Hsu 			}
804be2ac88cSJonathan Lemon 			/*
805be2ac88cSJonathan Lemon 			 * Socket is created in state SYN_RECEIVED.
8068d573cc1SAndre Oppermann 			 * Unlock the listen socket, lock the newly
8078d573cc1SAndre Oppermann 			 * created socket and update the tp variable.
808be2ac88cSJonathan Lemon 			 */
8098501a69cSRobert Watson 			INP_WUNLOCK(inp);	/* listen socket */
810be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
8118501a69cSRobert Watson 			INP_WLOCK(inp);		/* new connection */
812be2ac88cSJonathan Lemon 			tp = intotcpcb(inp);
8138d573cc1SAndre Oppermann 			KASSERT(tp->t_state == TCPS_SYN_RECEIVED,
8148d573cc1SAndre Oppermann 			    ("%s: ", __func__));
815be2ac88cSJonathan Lemon 			/*
816302ce8d6SAndre Oppermann 			 * Process the segment and the data it
817302ce8d6SAndre Oppermann 			 * contains.  tcp_do_segment() consumes
818302ce8d6SAndre Oppermann 			 * the mbuf chain and unlocks the inpcb.
819302ce8d6SAndre Oppermann 			 */
820f2512ba1SRui Paulo 			tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen,
821252ca428SRobert Watson 			    iptos, ti_locked);
822603724d3SBjoern A. Zeeb 			INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
823302ce8d6SAndre Oppermann 			return;
824be2ac88cSJonathan Lemon 		}
825a160e630SAndre Oppermann 		/*
8268d573cc1SAndre Oppermann 		 * Segment flag validation for new connection attempts:
8278d573cc1SAndre Oppermann 		 *
828a160e630SAndre Oppermann 		 * Our (SYN|ACK) response was rejected.
829a160e630SAndre Oppermann 		 * Check with syncache and remove entry to prevent
830a160e630SAndre Oppermann 		 * retransmits.
83119bc77c5SAndre Oppermann 		 *
83219bc77c5SAndre Oppermann 		 * NB: syncache_chkrst does its own logging of failure
83319bc77c5SAndre Oppermann 		 * causes.
834a160e630SAndre Oppermann 		 */
835a160e630SAndre Oppermann 		if (thflags & TH_RST) {
83619bc77c5SAndre Oppermann 			syncache_chkrst(&inc, th);
837a160e630SAndre Oppermann 			goto dropunlock;
838a160e630SAndre Oppermann 		}
839a160e630SAndre Oppermann 		/*
840a160e630SAndre Oppermann 		 * We can't do anything without SYN.
841a160e630SAndre Oppermann 		 */
842a160e630SAndre Oppermann 		if ((thflags & TH_SYN) == 0) {
843a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
844a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
845773673c1SAndre Oppermann 				    "SYN is missing, segment ignored\n",
8468d573cc1SAndre Oppermann 				    s, __func__);
847603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_badsyn++;
848a160e630SAndre Oppermann 			goto dropunlock;
849a160e630SAndre Oppermann 		}
850a160e630SAndre Oppermann 		/*
851a160e630SAndre Oppermann 		 * (SYN|ACK) is bogus on a listen socket.
852a160e630SAndre Oppermann 		 */
853fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
854a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
855a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
8568d573cc1SAndre Oppermann 				    "SYN|ACK invalid, segment rejected\n",
8578d573cc1SAndre Oppermann 				    s, __func__);
858a160e630SAndre Oppermann 			syncache_badack(&inc);	/* XXX: Not needed! */
859603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_badsyn++;
860a57815efSBosko Milekic 			rstreason = BANDLIM_RST_OPENPORT;
861a57815efSBosko Milekic 			goto dropwithreset;
8624195b4afSPaul Traina 		}
863a160e630SAndre Oppermann 		/*
864a160e630SAndre Oppermann 		 * If the drop_synfin option is enabled, drop all
865a160e630SAndre Oppermann 		 * segments with both the SYN and FIN bits set.
866a160e630SAndre Oppermann 		 * This prevents e.g. nmap from identifying the
867a160e630SAndre Oppermann 		 * TCP/IP stack.
868a160e630SAndre Oppermann 		 * XXX: Poor reasoning.  nmap has other methods
869a160e630SAndre Oppermann 		 * and is constantly refining its stack detection
870a160e630SAndre Oppermann 		 * strategies.
8718d573cc1SAndre Oppermann 		 * XXX: This is a violation of the TCP specification
872a160e630SAndre Oppermann 		 * and was used by RFC1644.
873a160e630SAndre Oppermann 		 */
874603724d3SBjoern A. Zeeb 		if ((thflags & TH_FIN) && V_drop_synfin) {
875a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
876a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
877773673c1SAndre Oppermann 				    "SYN|FIN segment ignored (based on "
8788d573cc1SAndre Oppermann 				    "sysctl setting)\n", s, __func__);
879603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_badsyn++;
880302ce8d6SAndre Oppermann                 	goto dropunlock;
881ebb0cbeaSPaul Traina 		}
882be2ac88cSJonathan Lemon 		/*
883be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
884a160e630SAndre Oppermann 		 *
885a160e630SAndre Oppermann 		 * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored
886a160e630SAndre Oppermann 		 * as they do not affect the state of the TCP FSM.
887a160e630SAndre Oppermann 		 * The data pointed to by TH_URG and th_urp is ignored.
888be2ac88cSJonathan Lemon 		 */
889a160e630SAndre Oppermann 		KASSERT((thflags & (TH_RST|TH_ACK)) == 0,
890a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_RST or TH_ACK set", __func__));
891a160e630SAndre Oppermann 		KASSERT(thflags & (TH_SYN),
892a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_SYN not set", __func__));
89333841545SHajimu UMEMOTO #ifdef INET6
89433841545SHajimu UMEMOTO 		/*
89533841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
89633841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
89733841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
89833841545SHajimu UMEMOTO 		 * getting established.
89933841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
90033841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
90133841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
90233841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
90333841545SHajimu UMEMOTO 		 * for the exchange.
90433841545SHajimu UMEMOTO 		 *
90533841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
90633841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
90733841545SHajimu UMEMOTO 		 * SYN in this case.
90833841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
90933841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
91033841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
91133841545SHajimu UMEMOTO 		 *    used"
91233841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
91333841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
91433841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
91533841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
91633841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
91733841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
91833841545SHajimu UMEMOTO 		 *
91933841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
92033841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
92133841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
92233841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
92333841545SHajimu UMEMOTO 		 */
924603724d3SBjoern A. Zeeb 		if (isipv6 && !V_ip6_use_deprecated) {
92533841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
92633841545SHajimu UMEMOTO 
92733841545SHajimu UMEMOTO 			if ((ia6 = ip6_getdstifaddr(m)) &&
92833841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
929a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
930a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
9318d573cc1SAndre Oppermann 					"Connection attempt to deprecated "
9328d573cc1SAndre Oppermann 					"IPv6 address rejected\n",
933a160e630SAndre Oppermann 					s, __func__);
93433841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
93533841545SHajimu UMEMOTO 				goto dropwithreset;
93633841545SHajimu UMEMOTO 			}
93733841545SHajimu UMEMOTO 		}
93833841545SHajimu UMEMOTO #endif
93965f28919SJesper Skriver 		/*
940db33b3e6SAndre Oppermann 		 * Basic sanity checks on incoming SYN requests:
9418d573cc1SAndre Oppermann 		 *   Don't respond if the destination is a link layer
942db33b3e6SAndre Oppermann 		 *	broadcast according to RFC1122 4.2.3.10, p. 104.
9438d573cc1SAndre Oppermann 		 *   If it is from this socket it must be forged.
9448d573cc1SAndre Oppermann 		 *   Don't respond if the source or destination is a
9458d573cc1SAndre Oppermann 		 *	global or subnet broad- or multicast address.
94693ec91baSCrist J. Clark 		 *   Note that it is quite possible to receive unicast
94793ec91baSCrist J. Clark 		 *	link-layer packets with a broadcast IP address. Use
94893ec91baSCrist J. Clark 		 *	in_broadcast() to find them.
949be2ac88cSJonathan Lemon 		 */
9508d573cc1SAndre Oppermann 		if (m->m_flags & (M_BCAST|M_MCAST)) {
9518d573cc1SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
9528d573cc1SAndre Oppermann 			    log(LOG_DEBUG, "%s; %s: Listen socket: "
9538d573cc1SAndre Oppermann 				"Connection attempt from broad- or multicast "
954773673c1SAndre Oppermann 				"link layer address ignored\n", s, __func__);
955302ce8d6SAndre Oppermann 			goto dropunlock;
9568d573cc1SAndre Oppermann 		}
957be2ac88cSJonathan Lemon 		if (isipv6) {
958db33b3e6SAndre Oppermann #ifdef INET6
959db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
960a160e630SAndre Oppermann 			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) {
961a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
962a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
9638d573cc1SAndre Oppermann 					"Connection attempt to/from self "
964773673c1SAndre Oppermann 					"ignored\n", s, __func__);
965302ce8d6SAndre Oppermann 				goto dropunlock;
966a160e630SAndre Oppermann 			}
967be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
968a160e630SAndre Oppermann 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
969a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
970a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
9718d573cc1SAndre Oppermann 					"Connection attempt from/to multicast "
972773673c1SAndre Oppermann 					"address ignored\n", s, __func__);
973302ce8d6SAndre Oppermann 				goto dropunlock;
974a160e630SAndre Oppermann 			}
975db33b3e6SAndre Oppermann #endif
976c068736aSJeffrey Hsu 		} else {
977db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
978a160e630SAndre Oppermann 			    ip->ip_dst.s_addr == ip->ip_src.s_addr) {
979a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
980a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
9818d573cc1SAndre Oppermann 					"Connection attempt from/to self "
982773673c1SAndre Oppermann 					"ignored\n", s, __func__);
983302ce8d6SAndre Oppermann 				goto dropunlock;
984a160e630SAndre Oppermann 			}
985be2ac88cSJonathan Lemon 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
986be2ac88cSJonathan Lemon 			    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
9872ca2159fSCrist J. Clark 			    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
988a160e630SAndre Oppermann 			    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
989a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
990a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
9918d573cc1SAndre Oppermann 					"Connection attempt from/to broad- "
992773673c1SAndre Oppermann 					"or multicast address ignored\n",
993a160e630SAndre Oppermann 					s, __func__);
994302ce8d6SAndre Oppermann 				goto dropunlock;
995c068736aSJeffrey Hsu 			}
996a160e630SAndre Oppermann 		}
997be2ac88cSJonathan Lemon 		/*
998db33b3e6SAndre Oppermann 		 * SYN appears to be valid.  Create compressed TCP state
999db33b3e6SAndre Oppermann 		 * for syncache.
1000be2ac88cSJonathan Lemon 		 */
10013c653157SHartmut Brandt #ifdef TCPDEBUG
10023c653157SHartmut Brandt 		if (so->so_options & SO_DEBUG)
10033c653157SHartmut Brandt 			tcp_trace(TA_INPUT, ostate, tp,
10043c653157SHartmut Brandt 			    (void *)tcp_saveipgen, &tcp_savetcp, 0);
10053c653157SHartmut Brandt #endif
1006f72167f4SAndre Oppermann 		tcp_dooptions(&to, optp, optlen, TO_SYN);
10074d6e7130SAndre Oppermann 		syncache_add(&inc, &to, th, inp, &so, m);
1008be2ac88cSJonathan Lemon 		/*
10094d6e7130SAndre Oppermann 		 * Entry added to syncache and mbuf consumed.
1010a160e630SAndre Oppermann 		 * Everything already unlocked by syncache_add().
1011be2ac88cSJonathan Lemon 		 */
1012603724d3SBjoern A. Zeeb 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1013be2ac88cSJonathan Lemon 		return;
1014f76fcf6dSJeffrey Hsu 	}
1015db33b3e6SAndre Oppermann 
1016302ce8d6SAndre Oppermann 	/*
10178d573cc1SAndre Oppermann 	 * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later
10188d573cc1SAndre Oppermann 	 * state.  tcp_do_segment() always consumes the mbuf chain, unlocks
10198d573cc1SAndre Oppermann 	 * the inpcb, and unlocks pcbinfo.
1020302ce8d6SAndre Oppermann 	 */
1021252ca428SRobert Watson 	tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked);
1022603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1023302ce8d6SAndre Oppermann 	return;
1024be2ac88cSJonathan Lemon 
1025302ce8d6SAndre Oppermann dropwithreset:
1026252ca428SRobert Watson 	if (ti_locked == TI_RLOCKED)
1027252ca428SRobert Watson 		INP_INFO_RUNLOCK(&V_tcbinfo);
1028252ca428SRobert Watson 	else if (ti_locked == TI_WLOCKED)
1029dd8ac7f9SRobert Watson 		INP_INFO_WUNLOCK(&V_tcbinfo);
1030252ca428SRobert Watson 	else
1031252ca428SRobert Watson 		panic("%s: dropwithreset ti_locked %d", __func__, ti_locked);
1032252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
1033014ea782SRobert Watson 
1034014ea782SRobert Watson 	if (inp != NULL) {
1035302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
1036014ea782SRobert Watson 		INP_WUNLOCK(inp);
1037dd8ac7f9SRobert Watson 	} else
1038014ea782SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
1039302ce8d6SAndre Oppermann 	m = NULL;	/* mbuf chain got consumed. */
1040014ea782SRobert Watson 	goto drop;
1041014ea782SRobert Watson 
1042302ce8d6SAndre Oppermann dropunlock:
1043252ca428SRobert Watson 	if (ti_locked == TI_RLOCKED)
1044252ca428SRobert Watson 		INP_INFO_RUNLOCK(&V_tcbinfo);
1045252ca428SRobert Watson 	else if (ti_locked == TI_WLOCKED)
1046252ca428SRobert Watson 		INP_INFO_WUNLOCK(&V_tcbinfo);
1047252ca428SRobert Watson 	else
1048252ca428SRobert Watson 		panic("%s: dropunlock ti_locked %d", __func__, ti_locked);
1049252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
1050252ca428SRobert Watson 
10519fa198beSAndre Oppermann 	if (inp != NULL)
10528501a69cSRobert Watson 		INP_WUNLOCK(inp);
1053014ea782SRobert Watson 
1054302ce8d6SAndre Oppermann drop:
1055603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1056a160e630SAndre Oppermann 	if (s != NULL)
1057a160e630SAndre Oppermann 		free(s, M_TCPLOG);
1058302ce8d6SAndre Oppermann 	if (m != NULL)
1059302ce8d6SAndre Oppermann 		m_freem(m);
1060302ce8d6SAndre Oppermann }
1061302ce8d6SAndre Oppermann 
1062679d9708SAndre Oppermann static void
1063302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
1064252ca428SRobert Watson     struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos,
1065252ca428SRobert Watson     int ti_locked)
1066302ce8d6SAndre Oppermann {
10678b615593SMarko Zec 	INIT_VNET_INET(tp->t_vnet);
1068302ce8d6SAndre Oppermann 	int thflags, acked, ourfinisacked, needoutput = 0;
1069302ce8d6SAndre Oppermann 	int rstreason, todrop, win;
1070302ce8d6SAndre Oppermann 	u_long tiwin;
1071302ce8d6SAndre Oppermann 	struct tcpopt to;
1072302ce8d6SAndre Oppermann 
107314739780SMaxim Konovalov #ifdef TCPDEBUG
107414739780SMaxim Konovalov 	/*
107514739780SMaxim Konovalov 	 * The size of tcp_saveipgen must be the size of the max ip header,
107614739780SMaxim Konovalov 	 * now IPv6.
107714739780SMaxim Konovalov 	 */
107814739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
107914739780SMaxim Konovalov 	struct tcphdr tcp_savetcp;
108014739780SMaxim Konovalov 	short ostate = 0;
108114739780SMaxim Konovalov #endif
1082302ce8d6SAndre Oppermann 	thflags = th->th_flags;
1083302ce8d6SAndre Oppermann 
1084252ca428SRobert Watson 	/*
1085252ca428SRobert Watson 	 * If this is either a state-changing packet or current state isn't
1086252ca428SRobert Watson 	 * established, we require a write lock on tcbinfo.  Otherwise, we
1087252ca428SRobert Watson 	 * allow either a read lock or a write lock, as we may have acquired
1088252ca428SRobert Watson 	 * a write lock due to a race.
1089252ca428SRobert Watson 	 *
1090d15fb965SRobert Watson 	 * Require a global write lock for SYN/FIN/RST segments or
1091252ca428SRobert Watson 	 * non-established connections; otherwise accept either a read or
1092252ca428SRobert Watson 	 * write lock, as we may have conservatively acquired a write lock in
1093252ca428SRobert Watson 	 * certain cases in tcp_input() (is this still true?).  Currently we
1094252ca428SRobert Watson 	 * will never enter with no lock, so we try to drop it quickly in the
1095252ca428SRobert Watson 	 * common pure ack/pure data cases.
1096252ca428SRobert Watson 	 */
1097252ca428SRobert Watson 	if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 ||
1098252ca428SRobert Watson 	    tp->t_state != TCPS_ESTABLISHED) {
1099252ca428SRobert Watson 		KASSERT(ti_locked == TI_WLOCKED, ("%s ti_locked %d for "
1100252ca428SRobert Watson 		    "SYN/FIN/RST/!EST", __func__, ti_locked));
1101603724d3SBjoern A. Zeeb 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1102252ca428SRobert Watson 	} else {
1103252ca428SRobert Watson #ifdef INVARIANTS
1104252ca428SRobert Watson 		if (ti_locked == TI_RLOCKED)
1105252ca428SRobert Watson 			INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1106252ca428SRobert Watson 		else if (ti_locked == TI_WLOCKED)
1107252ca428SRobert Watson 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1108252ca428SRobert Watson 		else
1109252ca428SRobert Watson 			panic("%s: ti_locked %d for EST", __func__,
1110252ca428SRobert Watson 			    ti_locked);
1111252ca428SRobert Watson #endif
1112252ca428SRobert Watson 	}
11138501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1114679d9708SAndre Oppermann 	KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
1115679d9708SAndre Oppermann 	    __func__));
1116679d9708SAndre Oppermann 	KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
1117679d9708SAndre Oppermann 	    __func__));
1118df8bae1dSRodney W. Grimes 
1119df8bae1dSRodney W. Grimes 	/*
1120df8bae1dSRodney W. Grimes 	 * Segment received on connection.
1121df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
1122e8949f74SAndre Oppermann 	 * XXX: This should be done after segment
1123e8949f74SAndre Oppermann 	 * validation to ignore broken/spoofed segs.
1124df8bae1dSRodney W. Grimes 	 */
11259b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
11267ff19458SPaul Traina 	if (TCPS_HAVEESTABLISHED(tp->t_state))
1127b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_KEEP, tcp_keepidle);
1128df8bae1dSRodney W. Grimes 
1129df8bae1dSRodney W. Grimes 	/*
1130464fcfbcSAndre Oppermann 	 * Unscale the window into a 32-bit value.
1131e8949f74SAndre Oppermann 	 * For the SYN_SENT state the scale is zero.
1132464fcfbcSAndre Oppermann 	 */
1133464fcfbcSAndre Oppermann 	tiwin = th->th_win << tp->snd_scale;
1134464fcfbcSAndre Oppermann 
1135464fcfbcSAndre Oppermann 	/*
1136f2512ba1SRui Paulo 	 * TCP ECN processing.
1137f2512ba1SRui Paulo 	 */
1138f2512ba1SRui Paulo 	if (tp->t_flags & TF_ECN_PERMIT) {
1139f2512ba1SRui Paulo 		switch (iptos & IPTOS_ECN_MASK) {
1140f2512ba1SRui Paulo 		case IPTOS_ECN_CE:
1141f2512ba1SRui Paulo 			tp->t_flags |= TF_ECN_SND_ECE;
1142603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_ecn_ce++;
1143f2512ba1SRui Paulo 			break;
1144f2512ba1SRui Paulo 		case IPTOS_ECN_ECT0:
1145603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_ecn_ect0++;
1146f2512ba1SRui Paulo 			break;
1147f2512ba1SRui Paulo 		case IPTOS_ECN_ECT1:
1148603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_ecn_ect1++;
1149f2512ba1SRui Paulo 			break;
1150f2512ba1SRui Paulo 		}
1151f2512ba1SRui Paulo 
1152f2512ba1SRui Paulo 		if (thflags & TH_CWR)
1153f2512ba1SRui Paulo 			tp->t_flags &= ~TF_ECN_SND_ECE;
1154f2512ba1SRui Paulo 
1155f2512ba1SRui Paulo 		/*
1156f2512ba1SRui Paulo 		 * Congestion experienced.
1157f2512ba1SRui Paulo 		 * Ignore if we are already trying to recover.
1158f2512ba1SRui Paulo 		 */
1159f2512ba1SRui Paulo 		if ((thflags & TH_ECE) &&
1160f2512ba1SRui Paulo 		    SEQ_LEQ(th->th_ack, tp->snd_recover)) {
1161603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_ecn_rcwnd++;
1162f2512ba1SRui Paulo 			tcp_congestion_exp(tp);
1163f2512ba1SRui Paulo 		}
1164f2512ba1SRui Paulo 	}
1165f2512ba1SRui Paulo 
1166f2512ba1SRui Paulo 	/*
1167f72167f4SAndre Oppermann 	 * Parse options on any incoming segment.
1168f72167f4SAndre Oppermann 	 */
1169302ce8d6SAndre Oppermann 	tcp_dooptions(&to, (u_char *)(th + 1),
1170302ce8d6SAndre Oppermann 	    (th->th_off << 2) - sizeof(struct tcphdr),
1171302ce8d6SAndre Oppermann 	    (thflags & TH_SYN) ? TO_SYN : 0);
1172f72167f4SAndre Oppermann 
1173f72167f4SAndre Oppermann 	/*
1174f72167f4SAndre Oppermann 	 * If echoed timestamp is later than the current time,
1175bf6d304aSAndre Oppermann 	 * fall back to non RFC1323 RTT calculation.  Normalize
1176bf6d304aSAndre Oppermann 	 * timestamp if syncookies were used when this connection
1177bf6d304aSAndre Oppermann 	 * was established.
1178f72167f4SAndre Oppermann 	 */
1179bf6d304aSAndre Oppermann 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
1180e16fa5caSJohn-Mark Gurney 		to.to_tsecr -= tp->ts_offset;
1181bf6d304aSAndre Oppermann 		if (TSTMP_GT(to.to_tsecr, ticks))
1182f72167f4SAndre Oppermann 			to.to_tsecr = 0;
1183bf6d304aSAndre Oppermann 	}
1184f72167f4SAndre Oppermann 
1185f72167f4SAndre Oppermann 	/*
118697d8d152SAndre Oppermann 	 * Process options only when we get SYN/ACK back. The SYN case
118797d8d152SAndre Oppermann 	 * for incoming connections is handled in tcp_syncache.
11885396d0f8SAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
11895396d0f8SAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.
119097d8d152SAndre Oppermann 	 * XXX this is traditional behavior, may need to be cleaned up.
1191df8bae1dSRodney W. Grimes 	 */
11920a389eabSSimon L. B. Nielsen 	if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
1193464fcfbcSAndre Oppermann 		if ((to.to_flags & TOF_SCALE) &&
1194464fcfbcSAndre Oppermann 		    (tp->t_flags & TF_REQ_SCALE)) {
1195be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
119602a1a643SAndre Oppermann 			tp->snd_scale = to.to_wscale;
1197be2ac88cSJonathan Lemon 		}
11985396d0f8SAndre Oppermann 		/*
11995396d0f8SAndre Oppermann 		 * Initial send window.  It will be updated with
12005396d0f8SAndre Oppermann 		 * the next incoming segment to the scaled value.
12015396d0f8SAndre Oppermann 		 */
12025396d0f8SAndre Oppermann 		tp->snd_wnd = th->th_win;
1203be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_TS) {
1204be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
1205be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
1206be2ac88cSJonathan Lemon 			tp->ts_recent_age = ticks;
1207be2ac88cSJonathan Lemon 		}
1208be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_MSS)
1209be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
12103529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
12113529149eSAndre Oppermann 		    (to.to_flags & TOF_SACKPERM) == 0)
12123529149eSAndre Oppermann 			tp->t_flags &= ~TF_SACK_PERMIT;
12136d90faf3SPaul Saab 	}
12146d90faf3SPaul Saab 
1215df8bae1dSRodney W. Grimes 	/*
1216df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
1217df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
1218df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
1219df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
1220df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
1221df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
1222df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
1223df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
1224df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
1225df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
1226df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
1227df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
1228a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
1229c5ad39b9SAndre Oppermann 	 * Since we check for TCPS_ESTABLISHED first, it can only
1230a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
1231df8bae1dSRodney W. Grimes 	 */
1232df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
1233c5ad39b9SAndre Oppermann 	    th->th_seq == tp->rcv_nxt &&
1234fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1235c5ad39b9SAndre Oppermann 	    tp->snd_nxt == tp->snd_max &&
1236c5ad39b9SAndre Oppermann 	    tiwin && tiwin == tp->snd_wnd &&
1237a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
1238c5ad39b9SAndre Oppermann 	    LIST_EMPTY(&tp->t_segq) &&
1239be2ac88cSJonathan Lemon 	    ((to.to_flags & TOF_TS) == 0 ||
1240c5ad39b9SAndre Oppermann 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) {
1241df8bae1dSRodney W. Grimes 
1242df8bae1dSRodney W. Grimes 		/*
1243df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
1244df8bae1dSRodney W. Grimes 		 * record the timestamp.
1245a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
1246a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1247df8bae1dSRodney W. Grimes 		 */
1248be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
1249fb59c426SYoshinobu Inoue 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
12509b8b58e0SJonathan Lemon 			tp->ts_recent_age = ticks;
1251a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
1252df8bae1dSRodney W. Grimes 		}
1253df8bae1dSRodney W. Grimes 
1254fb59c426SYoshinobu Inoue 		if (tlen == 0) {
1255fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
1256fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
1257233e8c18SGarrett Wollman 			    tp->snd_cwnd >= tp->snd_wnd &&
1258603724d3SBjoern A. Zeeb 			    ((!V_tcp_do_newreno &&
12593529149eSAndre Oppermann 			      !(tp->t_flags & TF_SACK_PERMIT) &&
1260cb942153SJeffrey Hsu 			      tp->t_dupacks < tcprexmtthresh) ||
1261603724d3SBjoern A. Zeeb 			     ((V_tcp_do_newreno ||
12623529149eSAndre Oppermann 			       (tp->t_flags & TF_SACK_PERMIT)) &&
1263fc30a251SAndre Oppermann 			      !IN_FASTRECOVERY(tp) &&
1264fc30a251SAndre Oppermann 			      (to.to_flags & TOF_SACK) == 0 &&
1265482ac968SPaul Saab 			      TAILQ_EMPTY(&tp->snd_holes)))) {
1266df8bae1dSRodney W. Grimes 				/*
1267e8949f74SAndre Oppermann 				 * This is a pure ack for outstanding data.
1268df8bae1dSRodney W. Grimes 				 */
1269252ca428SRobert Watson 				if (ti_locked == TI_RLOCKED)
1270252ca428SRobert Watson 					INP_INFO_RUNLOCK(&V_tcbinfo);
1271252ca428SRobert Watson 				else if (ti_locked == TI_WLOCKED)
1272252ca428SRobert Watson 					INP_INFO_WUNLOCK(&V_tcbinfo);
1273252ca428SRobert Watson 				else
1274252ca428SRobert Watson 					panic("%s: ti_locked %d on pure ACK",
1275252ca428SRobert Watson 					    __func__, ti_locked);
1276252ca428SRobert Watson 				ti_locked = TI_UNLOCKED;
1277252ca428SRobert Watson 
1278603724d3SBjoern A. Zeeb 				++V_tcpstat.tcps_predack;
1279252ca428SRobert Watson 
12809b8b58e0SJonathan Lemon 				/*
1281e8949f74SAndre Oppermann 				 * "bad retransmit" recovery.
12829b8b58e0SJonathan Lemon 				 */
12839b8b58e0SJonathan Lemon 				if (tp->t_rxtshift == 1 &&
12849b8b58e0SJonathan Lemon 				    ticks < tp->t_badrxtwin) {
1285603724d3SBjoern A. Zeeb 					++V_tcpstat.tcps_sndrexmitbad;
12869b8b58e0SJonathan Lemon 					tp->snd_cwnd = tp->snd_cwnd_prev;
12879b8b58e0SJonathan Lemon 					tp->snd_ssthresh =
12889b8b58e0SJonathan Lemon 					    tp->snd_ssthresh_prev;
12899d11646dSJeffrey Hsu 					tp->snd_recover = tp->snd_recover_prev;
12909d11646dSJeffrey Hsu 					if (tp->t_flags & TF_WASFRECOVERY)
12919d11646dSJeffrey Hsu 					    ENTER_FASTRECOVERY(tp);
12929b8b58e0SJonathan Lemon 					tp->snd_nxt = tp->snd_max;
12939b8b58e0SJonathan Lemon 					tp->t_badrxtwin = 0;
12949b8b58e0SJonathan Lemon 				}
1295fa55172bSMatthew Dillon 
1296fa55172bSMatthew Dillon 				/*
1297fa55172bSMatthew Dillon 				 * Recalculate the transmit timer / rtt.
1298fa55172bSMatthew Dillon 				 *
1299fa55172bSMatthew Dillon 				 * Some boxes send broken timestamp replies
1300fa55172bSMatthew Dillon 				 * during the SYN+ACK phase, ignore
1301fa55172bSMatthew Dillon 				 * timestamps of 0 or we could calculate a
1302fa55172bSMatthew Dillon 				 * huge RTT and blow up the retransmit timer.
1303fa55172bSMatthew Dillon 				 */
1304fa55172bSMatthew Dillon 				if ((to.to_flags & TOF_TS) != 0 &&
1305fa55172bSMatthew Dillon 				    to.to_tsecr) {
1306eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1307eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - to.to_tsecr)
1308eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - to.to_tsecr;
1309a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
13109b8b58e0SJonathan Lemon 					    ticks - to.to_tsecr + 1);
1311fa55172bSMatthew Dillon 				} else if (tp->t_rtttime &&
1312fa55172bSMatthew Dillon 				    SEQ_GT(th->th_ack, tp->t_rtseq)) {
1313eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1314eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - tp->t_rtttime)
1315eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - tp->t_rtttime;
1316c068736aSJeffrey Hsu 					tcp_xmit_timer(tp,
1317c068736aSJeffrey Hsu 							ticks - tp->t_rtttime);
1318fa55172bSMatthew Dillon 				}
13191fcc99b5SMatthew Dillon 				tcp_xmit_bandwidth_limit(tp, th->th_ack);
1320fb59c426SYoshinobu Inoue 				acked = th->th_ack - tp->snd_una;
1321603724d3SBjoern A. Zeeb 				V_tcpstat.tcps_rcvackpack++;
1322603724d3SBjoern A. Zeeb 				V_tcpstat.tcps_rcvackbyte += acked;
1323df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
13249d11646dSJeffrey Hsu 				if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
13259d11646dSJeffrey Hsu 				    SEQ_LEQ(th->th_ack, tp->snd_recover))
13269d11646dSJeffrey Hsu 					tp->snd_recover = th->th_ack - 1;
13279d11646dSJeffrey Hsu 				tp->snd_una = th->th_ack;
13281645d090SJeff Roberson 				/*
1329e8949f74SAndre Oppermann 				 * Pull snd_wl2 up to prevent seq wrap relative
13301645d090SJeff Roberson 				 * to th_ack.
13311645d090SJeff Roberson 				 */
1332cb942153SJeffrey Hsu 				tp->snd_wl2 = th->th_ack;
1333b5addd85SJeffrey Hsu 				tp->t_dupacks = 0;
1334df8bae1dSRodney W. Grimes 				m_freem(m);
1335e8949f74SAndre Oppermann 				ND6_HINT(tp); /* Some progress has been made. */
1336df8bae1dSRodney W. Grimes 
1337df8bae1dSRodney W. Grimes 				/*
1338df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
1339df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
1340df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
1341df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
1342df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
1343df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
1344df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
1345e8949f74SAndre Oppermann 				 */
13463c653157SHartmut Brandt #ifdef TCPDEBUG
13473c653157SHartmut Brandt 				if (so->so_options & SO_DEBUG)
13483c653157SHartmut Brandt 					tcp_trace(TA_INPUT, ostate, tp,
13493c653157SHartmut Brandt 					    (void *)tcp_saveipgen,
13503c653157SHartmut Brandt 					    &tcp_savetcp, 0);
13513c653157SHartmut Brandt #endif
1352df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
1353b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
1354b8152ba7SAndre Oppermann 				else if (!tcp_timer_active(tp, TT_PERSIST))
1355b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT,
1356b8152ba7SAndre Oppermann 						      tp->t_rxtcur);
1357df8bae1dSRodney W. Grimes 				sowwakeup(so);
1358df8bae1dSRodney W. Grimes 				if (so->so_snd.sb_cc)
1359df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1360a14c749fSJonathan Lemon 				goto check_delack;
1361df8bae1dSRodney W. Grimes 			}
1362fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1363fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
13646741ecf5SAndre Oppermann 			int newsize = 0;	/* automatic sockbuf scaling */
13656741ecf5SAndre Oppermann 
1366df8bae1dSRodney W. Grimes 			/*
1367252ca428SRobert Watson 			 * This is a pure, in-sequence data packet with
1368252ca428SRobert Watson 			 * nothing on the reassembly queue and we have enough
1369252ca428SRobert Watson 			 * buffer space to take it.
1370df8bae1dSRodney W. Grimes 			 */
1371252ca428SRobert Watson 			if (ti_locked == TI_RLOCKED)
1372252ca428SRobert Watson 				INP_INFO_RUNLOCK(&V_tcbinfo);
1373252ca428SRobert Watson 			else if (ti_locked == TI_WLOCKED)
1374252ca428SRobert Watson 				INP_INFO_WUNLOCK(&V_tcbinfo);
1375252ca428SRobert Watson 			else
1376252ca428SRobert Watson 				panic("%s: ti_locked %d on pure data "
1377252ca428SRobert Watson 				    "segment", __func__, ti_locked);
1378252ca428SRobert Watson 			ti_locked = TI_UNLOCKED;
1379252ca428SRobert Watson 
13806d90faf3SPaul Saab 			/* Clean receiver SACK report if present */
13813529149eSAndre Oppermann 			if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks)
13826d90faf3SPaul Saab 				tcp_clean_sackreport(tp);
1383603724d3SBjoern A. Zeeb 			++V_tcpstat.tcps_preddat;
1384fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
13851645d090SJeff Roberson 			/*
13861645d090SJeff Roberson 			 * Pull snd_wl1 up to prevent seq wrap relative to
13871645d090SJeff Roberson 			 * th_seq.
13881645d090SJeff Roberson 			 */
13891645d090SJeff Roberson 			tp->snd_wl1 = th->th_seq;
13901645d090SJeff Roberson 			/*
13911645d090SJeff Roberson 			 * Pull rcv_up up to prevent seq wrap relative to
13921645d090SJeff Roberson 			 * rcv_nxt.
13931645d090SJeff Roberson 			 */
13941645d090SJeff Roberson 			tp->rcv_up = tp->rcv_nxt;
1395603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_rcvpack++;
1396603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_rcvbyte += tlen;
1397e8949f74SAndre Oppermann 			ND6_HINT(tp);	/* Some progress has been made */
13983c653157SHartmut Brandt #ifdef TCPDEBUG
13993c653157SHartmut Brandt 			if (so->so_options & SO_DEBUG)
14003c653157SHartmut Brandt 				tcp_trace(TA_INPUT, ostate, tp,
14013c653157SHartmut Brandt 				    (void *)tcp_saveipgen, &tcp_savetcp, 0);
14023c653157SHartmut Brandt #endif
14036741ecf5SAndre Oppermann 		/*
14046741ecf5SAndre Oppermann 		 * Automatic sizing of receive socket buffer.  Often the send
14056741ecf5SAndre Oppermann 		 * buffer size is not optimally adjusted to the actual network
14066741ecf5SAndre Oppermann 		 * conditions at hand (delay bandwidth product).  Setting the
14076741ecf5SAndre Oppermann 		 * buffer size too small limits throughput on links with high
14086741ecf5SAndre Oppermann 		 * bandwidth and high delay (eg. trans-continental/oceanic links).
14096741ecf5SAndre Oppermann 		 *
14106741ecf5SAndre Oppermann 		 * On the receive side the socket buffer memory is only rarely
14116741ecf5SAndre Oppermann 		 * used to any significant extent.  This allows us to be much
14126741ecf5SAndre Oppermann 		 * more aggressive in scaling the receive socket buffer.  For
14136741ecf5SAndre Oppermann 		 * the case that the buffer space is actually used to a large
14146741ecf5SAndre Oppermann 		 * extent and we run out of kernel memory we can simply drop
14156741ecf5SAndre Oppermann 		 * the new segments; TCP on the sender will just retransmit it
14166741ecf5SAndre Oppermann 		 * later.  Setting the buffer size too big may only consume too
14176741ecf5SAndre Oppermann 		 * much kernel memory if the application doesn't read() from
14186741ecf5SAndre Oppermann 		 * the socket or packet loss or reordering makes use of the
14196741ecf5SAndre Oppermann 		 * reassembly queue.
14206741ecf5SAndre Oppermann 		 *
14216741ecf5SAndre Oppermann 		 * The criteria to step up the receive buffer one notch are:
14226741ecf5SAndre Oppermann 		 *  1. the number of bytes received during the time it takes
14236741ecf5SAndre Oppermann 		 *     one timestamp to be reflected back to us (the RTT);
14246741ecf5SAndre Oppermann 		 *  2. received bytes per RTT is within seven eighth of the
14256741ecf5SAndre Oppermann 		 *     current socket buffer size;
14266741ecf5SAndre Oppermann 		 *  3. receive buffer size has not hit maximal automatic size;
14276741ecf5SAndre Oppermann 		 *
14286741ecf5SAndre Oppermann 		 * This algorithm does one step per RTT at most and only if
14296741ecf5SAndre Oppermann 		 * we receive a bulk stream w/o packet losses or reorderings.
14306741ecf5SAndre Oppermann 		 * Shrinking the buffer during idle times is not necessary as
14316741ecf5SAndre Oppermann 		 * it doesn't consume any memory when idle.
14326741ecf5SAndre Oppermann 		 *
14336741ecf5SAndre Oppermann 		 * TODO: Only step up if the application is actually serving
14346741ecf5SAndre Oppermann 		 * the buffer to better manage the socket buffer resources.
1435df8bae1dSRodney W. Grimes 		 */
1436603724d3SBjoern A. Zeeb 			if (V_tcp_do_autorcvbuf &&
14376741ecf5SAndre Oppermann 			    to.to_tsecr &&
14386741ecf5SAndre Oppermann 			    (so->so_rcv.sb_flags & SB_AUTOSIZE)) {
14396741ecf5SAndre Oppermann 				if (to.to_tsecr > tp->rfbuf_ts &&
14406741ecf5SAndre Oppermann 				    to.to_tsecr - tp->rfbuf_ts < hz) {
14416741ecf5SAndre Oppermann 					if (tp->rfbuf_cnt >
14426741ecf5SAndre Oppermann 					    (so->so_rcv.sb_hiwat / 8 * 7) &&
14436741ecf5SAndre Oppermann 					    so->so_rcv.sb_hiwat <
1444603724d3SBjoern A. Zeeb 					    V_tcp_autorcvbuf_max) {
14456741ecf5SAndre Oppermann 						newsize =
14466741ecf5SAndre Oppermann 						    min(so->so_rcv.sb_hiwat +
1447603724d3SBjoern A. Zeeb 						    V_tcp_autorcvbuf_inc,
1448603724d3SBjoern A. Zeeb 						    V_tcp_autorcvbuf_max);
14496741ecf5SAndre Oppermann 					}
14506741ecf5SAndre Oppermann 					/* Start over with next RTT. */
14516741ecf5SAndre Oppermann 					tp->rfbuf_ts = 0;
14526741ecf5SAndre Oppermann 					tp->rfbuf_cnt = 0;
14536741ecf5SAndre Oppermann 				} else
14546741ecf5SAndre Oppermann 					tp->rfbuf_cnt += tlen;	/* add up */
14556741ecf5SAndre Oppermann 			}
14566741ecf5SAndre Oppermann 
14576741ecf5SAndre Oppermann 			/* Add data to socket buffer. */
14581e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
1459c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1460c1c36a2cSMike Silbersack 				m_freem(m);
1461c1c36a2cSMike Silbersack 			} else {
14626741ecf5SAndre Oppermann 				/*
14636741ecf5SAndre Oppermann 				 * Set new socket buffer size.
14646741ecf5SAndre Oppermann 				 * Give up when limit is reached.
14656741ecf5SAndre Oppermann 				 */
14666741ecf5SAndre Oppermann 				if (newsize)
14676741ecf5SAndre Oppermann 					if (!sbreserve_locked(&so->so_rcv,
14686c8286e4SRobert Watson 					    newsize, so, NULL))
14696741ecf5SAndre Oppermann 						so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1470fb59c426SYoshinobu Inoue 				m_adj(m, drop_hdrlen);	/* delayed header drop */
14711e4d7da7SRobert Watson 				sbappendstream_locked(&so->so_rcv, m);
1472c1c36a2cSMike Silbersack 			}
1473e8949f74SAndre Oppermann 			/* NB: sorwakeup_locked() does an implicit unlock. */
14741e4d7da7SRobert Watson 			sorwakeup_locked(so);
1475d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp)) {
14763bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
1477f498eeeeSDavid Greenman 			} else {
1478e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
1479e612a582SDavid Greenman 				tcp_output(tp);
1480e612a582SDavid Greenman 			}
1481a14c749fSJonathan Lemon 			goto check_delack;
1482df8bae1dSRodney W. Grimes 		}
1483df8bae1dSRodney W. Grimes 	}
1484df8bae1dSRodney W. Grimes 
1485df8bae1dSRodney W. Grimes 	/*
1486df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1487df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1488df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1489df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1490df8bae1dSRodney W. Grimes 	 */
1491df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1492df8bae1dSRodney W. Grimes 	if (win < 0)
1493df8bae1dSRodney W. Grimes 		win = 0;
149466e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1495df8bae1dSRodney W. Grimes 
14966741ecf5SAndre Oppermann 	/* Reset receive buffer auto scaling when not in bulk receive mode. */
14976741ecf5SAndre Oppermann 	tp->rfbuf_ts = 0;
14986741ecf5SAndre Oppermann 	tp->rfbuf_cnt = 0;
14996741ecf5SAndre Oppermann 
1500df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1501df8bae1dSRodney W. Grimes 
1502df8bae1dSRodney W. Grimes 	/*
1503764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1504764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1505764d8cefSBill Fenner 	 */
1506764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1507fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1508fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1509a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1510a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1511a57815efSBosko Milekic 				goto dropwithreset;
1512a57815efSBosko Milekic 		}
1513764d8cefSBill Fenner 		break;
1514764d8cefSBill Fenner 
1515764d8cefSBill Fenner 	/*
1516df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
1517df8bae1dSRodney W. Grimes 	 *	if seg contains an ACK, but not for our SYN, drop the input.
1518df8bae1dSRodney W. Grimes 	 *	if seg contains a RST, then drop the connection.
1519df8bae1dSRodney W. Grimes 	 *	if seg does not contain SYN, then drop it.
1520df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
1521df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
1522df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
1523f2512ba1SRui Paulo 	 *	if seg contains an ECE and ECN support is enabled, the stream
1524f2512ba1SRui Paulo 	 *	    is ECN capable.
1525df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1526df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
1527df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
1528df8bae1dSRodney W. Grimes 	 */
1529df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
1530fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1531fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
1532fb59c426SYoshinobu Inoue 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1533a57815efSBosko Milekic 			rstreason = BANDLIM_UNLIMITED;
1534a0292f23SGarrett Wollman 			goto dropwithreset;
1535a0292f23SGarrett Wollman 		}
15360ca3f933SAndre Oppermann 		if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST))
1537df8bae1dSRodney W. Grimes 			tp = tcp_drop(tp, ECONNREFUSED);
15380ca3f933SAndre Oppermann 		if (thflags & TH_RST)
1539df8bae1dSRodney W. Grimes 			goto drop;
15400ca3f933SAndre Oppermann 		if (!(thflags & TH_SYN))
1541df8bae1dSRodney W. Grimes 			goto drop;
1542464fcfbcSAndre Oppermann 
1543fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
1544df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
1545fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1546603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_connects++;
1547845799c1SAndras Olah 			soisconnected(so);
1548c488362eSRobert Watson #ifdef MAC
1549310e7cebSRobert Watson 			SOCK_LOCK(so);
155030d239bcSRobert Watson 			mac_socketpeer_set_from_mbuf(m, so);
1551310e7cebSRobert Watson 			SOCK_UNLOCK(so);
1552c488362eSRobert Watson #endif
1553845799c1SAndras Olah 			/* Do window scaling on this connection? */
1554845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1555845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1556845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
1557845799c1SAndras Olah 			}
1558a0292f23SGarrett Wollman 			tp->rcv_adv += tp->rcv_wnd;
1559a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
1560a0292f23SGarrett Wollman 			/*
1561a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
1562a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
1563a0292f23SGarrett Wollman 			 */
1564d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp) && tlen != 0)
1565b8152ba7SAndre Oppermann 				tcp_timer_activate(tp, TT_DELACK,
1566b8152ba7SAndre Oppermann 				    tcp_delacktime);
1567a0292f23SGarrett Wollman 			else
1568a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
1569f2512ba1SRui Paulo 
1570603724d3SBjoern A. Zeeb 			if ((thflags & TH_ECE) && V_tcp_do_ecn) {
1571f2512ba1SRui Paulo 				tp->t_flags |= TF_ECN_PERMIT;
1572603724d3SBjoern A. Zeeb 				V_tcpstat.tcps_ecn_shs++;
1573f2512ba1SRui Paulo 			}
1574f2512ba1SRui Paulo 
1575a0292f23SGarrett Wollman 			/*
1576a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
1577a0292f23SGarrett Wollman 			 * Transitions:
1578a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
1579a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
1580a0292f23SGarrett Wollman 			 */
15819b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
1582a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
1583a0292f23SGarrett Wollman 				tp->t_state = TCPS_FIN_WAIT_1;
1584a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
1585fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
15867ff19458SPaul Traina 			} else {
1587a0292f23SGarrett Wollman 				tp->t_state = TCPS_ESTABLISHED;
1588b8152ba7SAndre Oppermann 				tcp_timer_activate(tp, TT_KEEP, tcp_keepidle);
15897ff19458SPaul Traina 			}
1590a0292f23SGarrett Wollman 		} else {
1591a0292f23SGarrett Wollman 			/*
1592c068736aSJeffrey Hsu 			 * Received initial SYN in SYN-SENT[*] state =>
1593c068736aSJeffrey Hsu 			 * simultaneous open.  If segment contains CC option
1594c068736aSJeffrey Hsu 			 * and there is a cached CC, apply TAO test.
1595c068736aSJeffrey Hsu 			 * If it succeeds, connection is * half-synchronized.
1596c068736aSJeffrey Hsu 			 * Otherwise, do 3-way handshake:
1597a0292f23SGarrett Wollman 			 *        SYN-SENT -> SYN-RECEIVED
1598a0292f23SGarrett Wollman 			 *        SYN-SENT* -> SYN-RECEIVED*
1599a0292f23SGarrett Wollman 			 * If there was no CC option, clear cached CC value.
1600a0292f23SGarrett Wollman 			 */
16014b8e98d6SQing Li 			tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
1602b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
1603df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_SYN_RECEIVED;
1604a0292f23SGarrett Wollman 		}
1605df8bae1dSRodney W. Grimes 
1606252ca428SRobert Watson 		KASSERT(ti_locked == TI_WLOCKED, ("%s: trimthenstep6: "
1607252ca428SRobert Watson 		    "ti_locked %d", __func__, ti_locked));
1608252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
16098501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
16107cfc6904SRobert Watson 
1611df8bae1dSRodney W. Grimes 		/*
1612fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
1613df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
1614df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
1615df8bae1dSRodney W. Grimes 		 */
1616fb59c426SYoshinobu Inoue 		th->th_seq++;
1617fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
1618fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
1619df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
1620fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
1621fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
1622603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_rcvpackafterwin++;
1623603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_rcvbyteafterwin += todrop;
1624df8bae1dSRodney W. Grimes 		}
1625fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
1626fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
1627a0292f23SGarrett Wollman 		/*
1628a0292f23SGarrett Wollman 		 * Client side of transaction: already sent SYN and data.
1629a0292f23SGarrett Wollman 		 * If the remote host used T/TCP to validate the SYN,
1630a0292f23SGarrett Wollman 		 * our data will be ACK'd; if so, enter normal data segment
1631a0292f23SGarrett Wollman 		 * processing in the middle of step 5, ack processing.
1632a0292f23SGarrett Wollman 		 * Otherwise, goto step 6.
1633a0292f23SGarrett Wollman 		 */
1634fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK)
1635a0292f23SGarrett Wollman 			goto process_ACK;
1636c068736aSJeffrey Hsu 
1637df8bae1dSRodney W. Grimes 		goto step6;
1638c068736aSJeffrey Hsu 
1639a0292f23SGarrett Wollman 	/*
1640a0292f23SGarrett Wollman 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
1641c94c54e4SAndre Oppermann 	 *      do normal processing.
1642a0292f23SGarrett Wollman 	 *
1643c94c54e4SAndre Oppermann 	 * NB: Leftover from RFC1644 T/TCP.  Cases to be reused later.
1644a0292f23SGarrett Wollman 	 */
1645a0292f23SGarrett Wollman 	case TCPS_LAST_ACK:
1646a0292f23SGarrett Wollman 	case TCPS_CLOSING:
1647a0292f23SGarrett Wollman 		break;  /* continue normal processing */
1648df8bae1dSRodney W. Grimes 	}
1649df8bae1dSRodney W. Grimes 
1650df8bae1dSRodney W. Grimes 	/*
1651df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
165280ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
165380ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
165480ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
165580ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
165680ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
165780ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
165880ab7c0eSGarrett Wollman 	 * killing a connection).
165980ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
1660a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
1661df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
1662df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
1663df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
1664df8bae1dSRodney W. Grimes 	 *
166580ab7c0eSGarrett Wollman 	 *
166680ab7c0eSGarrett Wollman 	 * If the RST bit is set, check the sequence number to see
166780ab7c0eSGarrett Wollman 	 * if this is a valid reset segment.
166880ab7c0eSGarrett Wollman 	 * RFC 793 page 37:
166980ab7c0eSGarrett Wollman 	 *   In all states except SYN-SENT, all reset (RST) segments
167080ab7c0eSGarrett Wollman 	 *   are validated by checking their SEQ-fields.  A reset is
167180ab7c0eSGarrett Wollman 	 *   valid if its sequence number is in the window.
167280ab7c0eSGarrett Wollman 	 * Note: this does not take into account delayed ACKs, so
167380ab7c0eSGarrett Wollman 	 *   we should test against last_ack_sent instead of rcv_nxt.
16741a244a61SJonathan Lemon 	 *   The sequence number in the reset segment is normally an
16751a244a61SJonathan Lemon 	 *   echo of our outgoing acknowlegement numbers, but some hosts
16761a244a61SJonathan Lemon 	 *   send a reset with the sequence number at the rightmost edge
16771a244a61SJonathan Lemon 	 *   of our receive window, and we have to handle this case.
167880dd2a81SMike Silbersack 	 * Note 2: Paul Watson's paper "Slipping in the Window" has shown
167980dd2a81SMike Silbersack 	 *   that brute force RST attacks are possible.  To combat this,
168080dd2a81SMike Silbersack 	 *   we use a much stricter check while in the ESTABLISHED state,
168180dd2a81SMike Silbersack 	 *   only accepting RSTs where the sequence number is equal to
168280dd2a81SMike Silbersack 	 *   last_ack_sent.  In all other states (the states in which a
168380dd2a81SMike Silbersack 	 *   RST is more likely), the more permissive check is used.
16848e5c87f4SBjoern A. Zeeb 	 * If we have multiple segments in flight, the initial reset
168580ab7c0eSGarrett Wollman 	 * segment sequence numbers will be to the left of last_ack_sent,
168680ab7c0eSGarrett Wollman 	 * but they will eventually catch up.
168780ab7c0eSGarrett Wollman 	 * In any case, it never made sense to trim reset segments to
168880ab7c0eSGarrett Wollman 	 * fit the receive window since RFC 1122 says:
168980ab7c0eSGarrett Wollman 	 *   4.2.2.12  RST Segment: RFC-793 Section 3.4
169080ab7c0eSGarrett Wollman 	 *
169180ab7c0eSGarrett Wollman 	 *    A TCP SHOULD allow a received RST segment to include data.
169280ab7c0eSGarrett Wollman 	 *
169380ab7c0eSGarrett Wollman 	 *    DISCUSSION
169480ab7c0eSGarrett Wollman 	 *         It has been suggested that a RST segment could contain
169580ab7c0eSGarrett Wollman 	 *         ASCII text that encoded and explained the cause of the
169680ab7c0eSGarrett Wollman 	 *         RST.  No standard has yet been established for such
169780ab7c0eSGarrett Wollman 	 *         data.
169880ab7c0eSGarrett Wollman 	 *
169980ab7c0eSGarrett Wollman 	 * If the reset segment passes the sequence number test examine
170080ab7c0eSGarrett Wollman 	 * the state:
170180ab7c0eSGarrett Wollman 	 *    SYN_RECEIVED STATE:
170280ab7c0eSGarrett Wollman 	 *	If passive open, return to LISTEN state.
170380ab7c0eSGarrett Wollman 	 *	If active open, inform user that connection was refused.
1704745bab7fSDima Dorfman 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
170580ab7c0eSGarrett Wollman 	 *	Inform user that connection was reset, and close tcb.
1706e9bd3a37SJonathan M. Bresler 	 *    CLOSING, LAST_ACK STATES:
170780ab7c0eSGarrett Wollman 	 *	Close the tcb.
1708e9bd3a37SJonathan M. Bresler 	 *    TIME_WAIT STATE:
170980ab7c0eSGarrett Wollman 	 *	Drop the segment - see Stevens, vol. 2, p. 964 and
171080ab7c0eSGarrett Wollman 	 *      RFC 1337.
171180ab7c0eSGarrett Wollman 	 */
1712fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
171395ad8418SQing Li 		if (SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) &&
171495ad8418SQing Li 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
171580ab7c0eSGarrett Wollman 			switch (tp->t_state) {
171680ab7c0eSGarrett Wollman 
171780ab7c0eSGarrett Wollman 			case TCPS_SYN_RECEIVED:
171880ab7c0eSGarrett Wollman 				so->so_error = ECONNREFUSED;
171980ab7c0eSGarrett Wollman 				goto close;
172080ab7c0eSGarrett Wollman 
172180ab7c0eSGarrett Wollman 			case TCPS_ESTABLISHED:
1722603724d3SBjoern A. Zeeb 				if (V_tcp_insecure_rst == 0 &&
172395ad8418SQing Li 				    !(SEQ_GEQ(th->th_seq, tp->rcv_nxt - 1) &&
172495ad8418SQing Li 				    SEQ_LEQ(th->th_seq, tp->rcv_nxt + 1)) &&
172595ad8418SQing Li 				    !(SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) &&
172695ad8418SQing Li 				    SEQ_LEQ(th->th_seq, tp->last_ack_sent + 1))) {
1727603724d3SBjoern A. Zeeb 					V_tcpstat.tcps_badrst++;
172880dd2a81SMike Silbersack 					goto drop;
172980dd2a81SMike Silbersack 				}
1730564aab1fSAndre Oppermann 				/* FALLTHROUGH */
173180ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_1:
173280ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_2:
173380ab7c0eSGarrett Wollman 			case TCPS_CLOSE_WAIT:
173480ab7c0eSGarrett Wollman 				so->so_error = ECONNRESET;
173580ab7c0eSGarrett Wollman 			close:
1736252ca428SRobert Watson 				KASSERT(ti_locked == TI_WLOCKED,
1737252ca428SRobert Watson 				    ("tcp_do_segment: TH_RST 1 ti_locked %d",
1738252ca428SRobert Watson 				    ti_locked));
1739252ca428SRobert Watson 				INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1740252ca428SRobert Watson 
174180ab7c0eSGarrett Wollman 				tp->t_state = TCPS_CLOSED;
1742603724d3SBjoern A. Zeeb 				V_tcpstat.tcps_drops++;
174380ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
174480ab7c0eSGarrett Wollman 				break;
174580ab7c0eSGarrett Wollman 
174680ab7c0eSGarrett Wollman 			case TCPS_CLOSING:
174780ab7c0eSGarrett Wollman 			case TCPS_LAST_ACK:
1748252ca428SRobert Watson 				KASSERT(ti_locked == TI_WLOCKED,
1749252ca428SRobert Watson 				    ("tcp_do_segment: TH_RST 2 ti_locked %d",
1750252ca428SRobert Watson 				    ti_locked));
1751252ca428SRobert Watson 				INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1752252ca428SRobert Watson 
175380ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
175480ab7c0eSGarrett Wollman 				break;
175580ab7c0eSGarrett Wollman 			}
175680ab7c0eSGarrett Wollman 		}
175780ab7c0eSGarrett Wollman 		goto drop;
175880ab7c0eSGarrett Wollman 	}
175980ab7c0eSGarrett Wollman 
176080ab7c0eSGarrett Wollman 	/*
1761df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
1762df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
1763df8bae1dSRodney W. Grimes 	 */
1764be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
176580ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
1766df8bae1dSRodney W. Grimes 
1767df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
17689b8b58e0SJonathan Lemon 		if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) {
1769df8bae1dSRodney W. Grimes 			/*
1770df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
1771df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
1772df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
1773df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
1774df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
1775df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
1776df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
1777df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
1778df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
1779df8bae1dSRodney W. Grimes 			 */
1780df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
1781df8bae1dSRodney W. Grimes 		} else {
1782603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_rcvduppack++;
1783603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_rcvdupbyte += tlen;
1784603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_pawsdrop++;
178507fd333dSMatthew Dillon 			if (tlen)
1786df8bae1dSRodney W. Grimes 				goto dropafterack;
17871ab4789dSMatthew Dillon 			goto drop;
17881ab4789dSMatthew Dillon 		}
1789df8bae1dSRodney W. Grimes 	}
1790df8bae1dSRodney W. Grimes 
1791a0292f23SGarrett Wollman 	/*
179280ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
179380ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
179480ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
179580ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
179680ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
179780ab7c0eSGarrett Wollman 	 */
1798a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
1799a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
1800a57815efSBosko Milekic 		goto dropwithreset;
1801a57815efSBosko Milekic 	}
180280ab7c0eSGarrett Wollman 
1803fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
1804df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1805fb59c426SYoshinobu Inoue 		if (thflags & TH_SYN) {
1806fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
1807fb59c426SYoshinobu Inoue 			th->th_seq++;
1808fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
1809fb59c426SYoshinobu Inoue 				th->th_urp--;
1810df8bae1dSRodney W. Grimes 			else
1811fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
1812df8bae1dSRodney W. Grimes 			todrop--;
1813df8bae1dSRodney W. Grimes 		}
1814df8bae1dSRodney W. Grimes 		/*
1815dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
1816df8bae1dSRodney W. Grimes 		 */
1817fb59c426SYoshinobu Inoue 		if (todrop > tlen
1818fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
1819dac20301SGarrett Wollman 			/*
1820dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
1821dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
1822dac20301SGarrett Wollman 			 * of sequence; drop it.
1823dac20301SGarrett Wollman 			 */
1824fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
1825dac20301SGarrett Wollman 
1826df8bae1dSRodney W. Grimes 			/*
1827dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
1828dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
1829df8bae1dSRodney W. Grimes 			 */
1830dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
1831fb59c426SYoshinobu Inoue 			todrop = tlen;
1832603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_rcvduppack++;
1833603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_rcvdupbyte += todrop;
1834df8bae1dSRodney W. Grimes 		} else {
1835603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_rcvpartduppack++;
1836603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_rcvpartdupbyte += todrop;
1837df8bae1dSRodney W. Grimes 		}
1838fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
1839fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
1840fb59c426SYoshinobu Inoue 		tlen -= todrop;
1841fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
1842fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
1843df8bae1dSRodney W. Grimes 		else {
1844fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
1845fb59c426SYoshinobu Inoue 			th->th_urp = 0;
1846df8bae1dSRodney W. Grimes 		}
1847df8bae1dSRodney W. Grimes 	}
1848df8bae1dSRodney W. Grimes 
1849df8bae1dSRodney W. Grimes 	/*
1850df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
1851df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
1852df8bae1dSRodney W. Grimes 	 */
1853df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
1854fb59c426SYoshinobu Inoue 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
1855773673c1SAndre Oppermann 		char *s;
1856773673c1SAndre Oppermann 
1857252ca428SRobert Watson 		KASSERT(ti_locked == TI_WLOCKED, ("%s: SS_NOFDEREF && "
1858252ca428SRobert Watson 		    "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked));
1859252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1860252ca428SRobert Watson 
1861773673c1SAndre Oppermann 		if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, NULL))) {
1862e31d8aa3SMike Silbersack 			log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data after socket "
1863773673c1SAndre Oppermann 			    "was closed, sending RST and removing tcpcb\n",
1864e31d8aa3SMike Silbersack 			    s, __func__, tcpstates[tp->t_state], tlen);
1865773673c1SAndre Oppermann 			free(s, M_TCPLOG);
1866773673c1SAndre Oppermann 		}
1867df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1868603724d3SBjoern A. Zeeb 		V_tcpstat.tcps_rcvafterclose++;
1869a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
1870df8bae1dSRodney W. Grimes 		goto dropwithreset;
1871df8bae1dSRodney W. Grimes 	}
1872df8bae1dSRodney W. Grimes 
1873df8bae1dSRodney W. Grimes 	/*
1874df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
1875df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
1876df8bae1dSRodney W. Grimes 	 */
1877fb59c426SYoshinobu Inoue 	todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
1878df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1879603724d3SBjoern A. Zeeb 		V_tcpstat.tcps_rcvpackafterwin++;
1880fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
1881603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_rcvbyteafterwin += tlen;
1882df8bae1dSRodney W. Grimes 			/*
1883df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
1884df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
1885df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
1886df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
1887df8bae1dSRodney W. Grimes 			 * and ack.
1888df8bae1dSRodney W. Grimes 			 */
1889fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
1890df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
1891603724d3SBjoern A. Zeeb 				V_tcpstat.tcps_rcvwinprobe++;
1892df8bae1dSRodney W. Grimes 			} else
1893df8bae1dSRodney W. Grimes 				goto dropafterack;
1894df8bae1dSRodney W. Grimes 		} else
1895603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_rcvbyteafterwin += todrop;
1896df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
1897fb59c426SYoshinobu Inoue 		tlen -= todrop;
1898fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
1899df8bae1dSRodney W. Grimes 	}
1900df8bae1dSRodney W. Grimes 
1901df8bae1dSRodney W. Grimes 	/*
1902df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
1903df8bae1dSRodney W. Grimes 	 * record its timestamp.
1904cf09195bSPaul Saab 	 * NOTE:
1905cf09195bSPaul Saab 	 * 1) That the test incorporates suggestions from the latest
1906a0292f23SGarrett Wollman 	 *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
1907cf09195bSPaul Saab 	 * 2) That updating only on newer timestamps interferes with
1908cf09195bSPaul Saab 	 *    our earlier PAWS tests, so this check should be solely
1909cf09195bSPaul Saab 	 *    predicated on the sequence space of this segment.
1910cf09195bSPaul Saab 	 * 3) That we modify the segment boundary check to be
1911cf09195bSPaul Saab 	 *        Last.ACK.Sent <= SEG.SEQ + SEG.Len
1912cf09195bSPaul Saab 	 *    instead of RFC1323's
1913cf09195bSPaul Saab 	 *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
1914cf09195bSPaul Saab 	 *    This modified check allows us to overcome RFC1323's
1915cf09195bSPaul Saab 	 *    limitations as described in Stevens TCP/IP Illustrated
1916cf09195bSPaul Saab 	 *    Vol. 2 p.869. In such cases, we can still calculate the
1917cf09195bSPaul Saab 	 *    RTT correctly when RCV.NXT == Last.ACK.Sent.
1918df8bae1dSRodney W. Grimes 	 */
1919be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
1920cf09195bSPaul Saab 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
1921cf09195bSPaul Saab 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
1922cf09195bSPaul Saab 		((thflags & (TH_SYN|TH_FIN)) != 0))) {
19239b8b58e0SJonathan Lemon 		tp->ts_recent_age = ticks;
1924a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
1925df8bae1dSRodney W. Grimes 	}
1926df8bae1dSRodney W. Grimes 
1927df8bae1dSRodney W. Grimes 	/*
1928df8bae1dSRodney W. Grimes 	 * If a SYN is in the window, then this is an
1929df8bae1dSRodney W. Grimes 	 * error and we send an RST and drop the connection.
1930df8bae1dSRodney W. Grimes 	 */
1931fb59c426SYoshinobu Inoue 	if (thflags & TH_SYN) {
1932252ca428SRobert Watson 		KASSERT(ti_locked == TI_WLOCKED,
1933252ca428SRobert Watson 		    ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked));
1934252ca428SRobert Watson 		INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1935252ca428SRobert Watson 
1936df8bae1dSRodney W. Grimes 		tp = tcp_drop(tp, ECONNRESET);
1937a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
1938122aad88SAndre Oppermann 		goto drop;
1939df8bae1dSRodney W. Grimes 	}
1940df8bae1dSRodney W. Grimes 
1941a0292f23SGarrett Wollman 	/*
1942a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
1943a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
1944a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
1945a0292f23SGarrett Wollman 	 */
1946fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
1947a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
1948a0292f23SGarrett Wollman 		    (tp->t_flags & TF_NEEDSYN))
1949a0292f23SGarrett Wollman 			goto step6;
19505e1aa279SDavid Malone 		else if (tp->t_flags & TF_ACKNOW)
19515e1aa279SDavid Malone 			goto dropafterack;
1952a0292f23SGarrett Wollman 		else
1953a0292f23SGarrett Wollman 			goto drop;
1954a0292f23SGarrett Wollman 	}
1955df8bae1dSRodney W. Grimes 
1956df8bae1dSRodney W. Grimes 	/*
1957df8bae1dSRodney W. Grimes 	 * Ack processing.
1958df8bae1dSRodney W. Grimes 	 */
1959df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1960df8bae1dSRodney W. Grimes 
1961df8bae1dSRodney W. Grimes 	/*
1962764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
1963764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
1964764d8cefSBill Fenner 	 * The ACK was checked above.
1965df8bae1dSRodney W. Grimes 	 */
1966df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1967a0292f23SGarrett Wollman 
1968603724d3SBjoern A. Zeeb 		V_tcpstat.tcps_connects++;
1969df8bae1dSRodney W. Grimes 		soisconnected(so);
1970df8bae1dSRodney W. Grimes 		/* Do window scaling? */
1971df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1972df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1973df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
1974464fcfbcSAndre Oppermann 			tp->snd_wnd = tiwin;
1975df8bae1dSRodney W. Grimes 		}
1976a0292f23SGarrett Wollman 		/*
1977a0292f23SGarrett Wollman 		 * Make transitions:
1978a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
1979a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
1980a0292f23SGarrett Wollman 		 */
19819b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
1982a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDFIN) {
1983a0292f23SGarrett Wollman 			tp->t_state = TCPS_FIN_WAIT_1;
1984a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDFIN;
19857ff19458SPaul Traina 		} else {
1986a0292f23SGarrett Wollman 			tp->t_state = TCPS_ESTABLISHED;
1987b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_KEEP, tcp_keepidle);
19887ff19458SPaul Traina 		}
1989a0292f23SGarrett Wollman 		/*
1990a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
1991a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
1992a0292f23SGarrett Wollman 		 */
1993fb59c426SYoshinobu Inoue 		if (tlen == 0 && (thflags & TH_FIN) == 0)
1994fb59c426SYoshinobu Inoue 			(void) tcp_reass(tp, (struct tcphdr *)0, 0,
1995a0292f23SGarrett Wollman 			    (struct mbuf *)0);
1996fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
199793b0017fSPhilippe Charnier 		/* FALLTHROUGH */
1998df8bae1dSRodney W. Grimes 
1999df8bae1dSRodney W. Grimes 	/*
2000df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2001df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
2002fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
2003fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
2004df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
2005df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
2006df8bae1dSRodney W. Grimes 	 */
2007df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
2008df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
2009df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
2010df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
2011df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
2012df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
20135a53ca16SPaul Saab 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
2014603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_rcvacktoomuch++;
20155a53ca16SPaul Saab 			goto dropafterack;
20165a53ca16SPaul Saab 		}
20173529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
2018fc30a251SAndre Oppermann 		    ((to.to_flags & TOF_SACK) ||
2019fc30a251SAndre Oppermann 		     !TAILQ_EMPTY(&tp->snd_holes)))
20205a53ca16SPaul Saab 			tcp_sack_doack(tp, &to, th->th_ack);
2021fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
2022fb59c426SYoshinobu Inoue 			if (tlen == 0 && tiwin == tp->snd_wnd) {
2023603724d3SBjoern A. Zeeb 				V_tcpstat.tcps_rcvdupack++;
2024df8bae1dSRodney W. Grimes 				/*
2025df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
2026df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
2027df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
2028df8bae1dSRodney W. Grimes 				 * change), the ack is the biggest we've
2029df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
2030df8bae1dSRodney W. Grimes 				 * threshhold of them, assume a packet
2031df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
2032df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
2033df8bae1dSRodney W. Grimes 				 * window so we send only this one
2034df8bae1dSRodney W. Grimes 				 * packet.
2035df8bae1dSRodney W. Grimes 				 *
2036df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
2037df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
2038df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
2039df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
2040df8bae1dSRodney W. Grimes 				 * the new ssthresh).
2041df8bae1dSRodney W. Grimes 				 *
2042df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
2043df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
2044df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
2045df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
2046df8bae1dSRodney W. Grimes 				 * network.
2047f2512ba1SRui Paulo 				 *
2048f2512ba1SRui Paulo 				 * When using TCP ECN, notify the peer that
2049f2512ba1SRui Paulo 				 * we reduced the cwnd.
2050df8bae1dSRodney W. Grimes 				 */
2051b8152ba7SAndre Oppermann 				if (!tcp_timer_active(tp, TT_REXMT) ||
2052fb59c426SYoshinobu Inoue 				    th->th_ack != tp->snd_una)
2053df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
2054cb942153SJeffrey Hsu 				else if (++tp->t_dupacks > tcprexmtthresh ||
2055603724d3SBjoern A. Zeeb 				    ((V_tcp_do_newreno ||
20563529149eSAndre Oppermann 				      (tp->t_flags & TF_SACK_PERMIT)) &&
20579d11646dSJeffrey Hsu 				     IN_FASTRECOVERY(tp))) {
20583529149eSAndre Oppermann 					if ((tp->t_flags & TF_SACK_PERMIT) &&
20593529149eSAndre Oppermann 					    IN_FASTRECOVERY(tp)) {
2060808f11b7SPaul Saab 						int awnd;
206125e6f9edSPaul Saab 
206225e6f9edSPaul Saab 						/*
206325e6f9edSPaul Saab 						 * Compute the amount of data in flight first.
206425e6f9edSPaul Saab 						 * We can inject new data into the pipe iff
206525e6f9edSPaul Saab 						 * we have less than 1/2 the original window's
206625e6f9edSPaul Saab 						 * worth of data in flight.
206725e6f9edSPaul Saab 						 */
2068808f11b7SPaul Saab 						awnd = (tp->snd_nxt - tp->snd_fack) +
20690077b016SPaul Saab 							tp->sackhint.sack_bytes_rexmit;
2070808f11b7SPaul Saab 						if (awnd < tp->snd_ssthresh) {
207125e6f9edSPaul Saab 							tp->snd_cwnd += tp->t_maxseg;
207225e6f9edSPaul Saab 							if (tp->snd_cwnd > tp->snd_ssthresh)
207325e6f9edSPaul Saab 								tp->snd_cwnd = tp->snd_ssthresh;
207425e6f9edSPaul Saab 						}
207525e6f9edSPaul Saab 					} else
207646f58482SJonathan Lemon 						tp->snd_cwnd += tp->t_maxseg;
207746f58482SJonathan Lemon 					(void) tcp_output(tp);
207846f58482SJonathan Lemon 					goto drop;
2079cb942153SJeffrey Hsu 				} else if (tp->t_dupacks == tcprexmtthresh) {
2080cb942153SJeffrey Hsu 					tcp_seq onxt = tp->snd_nxt;
2081a0445c2eSJayanth Vijayaraghavan 
2082a0445c2eSJayanth Vijayaraghavan 					/*
2083a0445c2eSJayanth Vijayaraghavan 					 * If we're doing sack, check to
2084a0445c2eSJayanth Vijayaraghavan 					 * see if we're already in sack
2085a0445c2eSJayanth Vijayaraghavan 					 * recovery. If we're not doing sack,
2086a0445c2eSJayanth Vijayaraghavan 					 * check to see if we're in newreno
2087a0445c2eSJayanth Vijayaraghavan 					 * recovery.
2088a0445c2eSJayanth Vijayaraghavan 					 */
20893529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
2090a0445c2eSJayanth Vijayaraghavan 						if (IN_FASTRECOVERY(tp)) {
2091a0445c2eSJayanth Vijayaraghavan 							tp->t_dupacks = 0;
2092a0445c2eSJayanth Vijayaraghavan 							break;
2093a0445c2eSJayanth Vijayaraghavan 						}
2094603724d3SBjoern A. Zeeb 					} else if (V_tcp_do_newreno ||
2095603724d3SBjoern A. Zeeb 					    V_tcp_do_ecn) {
2096a0445c2eSJayanth Vijayaraghavan 						if (SEQ_LEQ(th->th_ack,
20979d11646dSJeffrey Hsu 						    tp->snd_recover)) {
2098cb942153SJeffrey Hsu 							tp->t_dupacks = 0;
2099cb942153SJeffrey Hsu 							break;
210046f58482SJonathan Lemon 						}
2101a0445c2eSJayanth Vijayaraghavan 					}
2102f2512ba1SRui Paulo 					tcp_congestion_exp(tp);
2103b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
21049b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
21053529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
2106603724d3SBjoern A. Zeeb 						V_tcpstat.tcps_sack_recovery_episode++;
2107a55db2b6SPaul Saab 						tp->sack_newdata = tp->snd_nxt;
21088db456bfSPaul Saab 						tp->snd_cwnd = tp->t_maxseg;
21096d90faf3SPaul Saab 						(void) tcp_output(tp);
21106d90faf3SPaul Saab 						goto drop;
21116d90faf3SPaul Saab 					}
2112fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
2113df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->t_maxseg;
2114df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
211548d2549cSJeffrey Hsu 					KASSERT(tp->snd_limited <= 2,
2116302ce8d6SAndre Oppermann 					    ("%s: tp->snd_limited too big",
2117302ce8d6SAndre Oppermann 					    __func__));
2118df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
211948d2549cSJeffrey Hsu 					     tp->t_maxseg *
212048d2549cSJeffrey Hsu 					     (tp->t_dupacks - tp->snd_limited);
2121df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
2122df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
2123df8bae1dSRodney W. Grimes 					goto drop;
2124603724d3SBjoern A. Zeeb 				} else if (V_tcp_do_rfc3042) {
2125582a954bSJeffrey Hsu 					u_long oldcwnd = tp->snd_cwnd;
212648d2549cSJeffrey Hsu 					tcp_seq oldsndmax = tp->snd_max;
212748d2549cSJeffrey Hsu 					u_int sent;
212889c02376SJeffrey Hsu 
2129582a954bSJeffrey Hsu 					KASSERT(tp->t_dupacks == 1 ||
2130582a954bSJeffrey Hsu 					    tp->t_dupacks == 2,
2131302ce8d6SAndre Oppermann 					    ("%s: dupacks not 1 or 2",
2132302ce8d6SAndre Oppermann 					    __func__));
213361a36e3dSJeffrey Hsu 					if (tp->t_dupacks == 1)
213448d2549cSJeffrey Hsu 						tp->snd_limited = 0;
213561a36e3dSJeffrey Hsu 					tp->snd_cwnd =
213661a36e3dSJeffrey Hsu 					    (tp->snd_nxt - tp->snd_una) +
213761a36e3dSJeffrey Hsu 					    (tp->t_dupacks - tp->snd_limited) *
213861a36e3dSJeffrey Hsu 					    tp->t_maxseg;
2139582a954bSJeffrey Hsu 					(void) tcp_output(tp);
214048d2549cSJeffrey Hsu 					sent = tp->snd_max - oldsndmax;
214148d2549cSJeffrey Hsu 					if (sent > tp->t_maxseg) {
214289c02376SJeffrey Hsu 						KASSERT((tp->t_dupacks == 2 &&
214389c02376SJeffrey Hsu 						    tp->snd_limited == 0) ||
214489c02376SJeffrey Hsu 						   (sent == tp->t_maxseg + 1 &&
214589c02376SJeffrey Hsu 						    tp->t_flags & TF_SENTFIN),
2146302ce8d6SAndre Oppermann 						    ("%s: sent too much",
2147302ce8d6SAndre Oppermann 						    __func__));
214848d2549cSJeffrey Hsu 						tp->snd_limited = 2;
214948d2549cSJeffrey Hsu 					} else if (sent > 0)
215048d2549cSJeffrey Hsu 						++tp->snd_limited;
2151582a954bSJeffrey Hsu 					tp->snd_cwnd = oldcwnd;
2152582a954bSJeffrey Hsu 					goto drop;
2153df8bae1dSRodney W. Grimes 				}
2154df8bae1dSRodney W. Grimes 			} else
2155df8bae1dSRodney W. Grimes 				tp->t_dupacks = 0;
2156df8bae1dSRodney W. Grimes 			break;
2157df8bae1dSRodney W. Grimes 		}
2158cb942153SJeffrey Hsu 
2159302ce8d6SAndre Oppermann 		KASSERT(SEQ_GT(th->th_ack, tp->snd_una),
2160302ce8d6SAndre Oppermann 		    ("%s: th_ack <= snd_una", __func__));
2161cb942153SJeffrey Hsu 
2162df8bae1dSRodney W. Grimes 		/*
2163df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
2164df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
2165df8bae1dSRodney W. Grimes 		 */
2166603724d3SBjoern A. Zeeb 		if (V_tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) {
21679d11646dSJeffrey Hsu 			if (IN_FASTRECOVERY(tp)) {
2168cb942153SJeffrey Hsu 				if (SEQ_LT(th->th_ack, tp->snd_recover)) {
21693529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT)
21706d90faf3SPaul Saab 						tcp_sack_partialack(tp, th);
21716d90faf3SPaul Saab 					else
2172c068736aSJeffrey Hsu 						tcp_newreno_partial_ack(tp, th);
2173c068736aSJeffrey Hsu 				} else {
2174c068736aSJeffrey Hsu 					/*
21756d90faf3SPaul Saab 					 * Out of fast recovery.
2176c068736aSJeffrey Hsu 					 * Window inflation should have left us
2177c068736aSJeffrey Hsu 					 * with approximately snd_ssthresh
2178c068736aSJeffrey Hsu 					 * outstanding data.
2179c068736aSJeffrey Hsu 					 * But in case we would be inclined to
2180c068736aSJeffrey Hsu 					 * send a burst, better to do it via
2181c068736aSJeffrey Hsu 					 * the slow start mechanism.
2182c068736aSJeffrey Hsu 					 */
2183c068736aSJeffrey Hsu 					if (SEQ_GT(th->th_ack +
2184c068736aSJeffrey Hsu 							tp->snd_ssthresh,
2185c068736aSJeffrey Hsu 						   tp->snd_max))
2186c068736aSJeffrey Hsu 						tp->snd_cwnd = tp->snd_max -
2187c068736aSJeffrey Hsu 								th->th_ack +
2188c068736aSJeffrey Hsu 								tp->t_maxseg;
2189c068736aSJeffrey Hsu 					else
2190c068736aSJeffrey Hsu 						tp->snd_cwnd = tp->snd_ssthresh;
2191c068736aSJeffrey Hsu 				}
2192c068736aSJeffrey Hsu 			}
2193c068736aSJeffrey Hsu 		} else {
2194233e8c18SGarrett Wollman 			if (tp->t_dupacks >= tcprexmtthresh &&
2195df8bae1dSRodney W. Grimes 			    tp->snd_cwnd > tp->snd_ssthresh)
2196df8bae1dSRodney W. Grimes 				tp->snd_cwnd = tp->snd_ssthresh;
219746f58482SJonathan Lemon 		}
2198cb942153SJeffrey Hsu 		tp->t_dupacks = 0;
2199a0292f23SGarrett Wollman 		/*
2200a0292f23SGarrett Wollman 		 * If we reach this point, ACK is not a duplicate,
2201a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
2202a0292f23SGarrett Wollman 		 */
2203a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
2204a0292f23SGarrett Wollman 			/*
2205a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
2206a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
220707e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
220807e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
220907e43e10SAndras Olah 			 * we can do window scaling.
2210a0292f23SGarrett Wollman 			 */
2211a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
2212a0292f23SGarrett Wollman 			tp->snd_una++;
221307e43e10SAndras Olah 			/* Do window scaling? */
221407e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
221507e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
221607e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2217464fcfbcSAndre Oppermann 				/* Send window already scaled. */
221807e43e10SAndras Olah 			}
2219a0292f23SGarrett Wollman 		}
2220a0292f23SGarrett Wollman 
2221a0292f23SGarrett Wollman process_ACK:
2222252ca428SRobert Watson 		INP_INFO_LOCK_ASSERT(&V_tcbinfo);
2223252ca428SRobert Watson 		KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED,
2224252ca428SRobert Watson 		    ("tcp_input: process_ACK ti_locked %d", ti_locked));
22258501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
22267cfc6904SRobert Watson 
2227fb59c426SYoshinobu Inoue 		acked = th->th_ack - tp->snd_una;
2228603724d3SBjoern A. Zeeb 		V_tcpstat.tcps_rcvackpack++;
2229603724d3SBjoern A. Zeeb 		V_tcpstat.tcps_rcvackbyte += acked;
2230df8bae1dSRodney W. Grimes 
2231df8bae1dSRodney W. Grimes 		/*
22329b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
22339b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
22349b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
22359b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
22369b8b58e0SJonathan Lemon 		 * we left off.
22379b8b58e0SJonathan Lemon 		 */
22389b8b58e0SJonathan Lemon 		if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) {
2239603724d3SBjoern A. Zeeb 			++V_tcpstat.tcps_sndrexmitbad;
22409b8b58e0SJonathan Lemon 			tp->snd_cwnd = tp->snd_cwnd_prev;
22419b8b58e0SJonathan Lemon 			tp->snd_ssthresh = tp->snd_ssthresh_prev;
22429d11646dSJeffrey Hsu 			tp->snd_recover = tp->snd_recover_prev;
22439d11646dSJeffrey Hsu 			if (tp->t_flags & TF_WASFRECOVERY)
22449d11646dSJeffrey Hsu 				ENTER_FASTRECOVERY(tp);
22459b8b58e0SJonathan Lemon 			tp->snd_nxt = tp->snd_max;
22469b8b58e0SJonathan Lemon 			tp->t_badrxtwin = 0;	/* XXX probably not required */
22479b8b58e0SJonathan Lemon 		}
22489b8b58e0SJonathan Lemon 
22499b8b58e0SJonathan Lemon 		/*
2250df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
2251df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
2252df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
2253df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
2254df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
2255df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
2256df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
2257fa55172bSMatthew Dillon 		 *
2258fa55172bSMatthew Dillon 		 * Some boxes send broken timestamp replies
2259fa55172bSMatthew Dillon 		 * during the SYN+ACK phase, ignore
2260fa55172bSMatthew Dillon 		 * timestamps of 0 or we could calculate a
2261fa55172bSMatthew Dillon 		 * huge RTT and blow up the retransmit timer.
2262df8bae1dSRodney W. Grimes 		 */
2263fa55172bSMatthew Dillon 		if ((to.to_flags & TOF_TS) != 0 &&
2264fa55172bSMatthew Dillon 		    to.to_tsecr) {
2265eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - to.to_tsecr)
2266eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - to.to_tsecr;
22679b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - to.to_tsecr + 1);
2268fa55172bSMatthew Dillon 		} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2269eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
2270eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - tp->t_rtttime;
22719b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2272fa55172bSMatthew Dillon 		}
22731fcc99b5SMatthew Dillon 		tcp_xmit_bandwidth_limit(tp, th->th_ack);
2274df8bae1dSRodney W. Grimes 
2275df8bae1dSRodney W. Grimes 		/*
2276df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
2277df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
2278df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
2279df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
2280df8bae1dSRodney W. Grimes 		 */
2281fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
2282b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
2283df8bae1dSRodney W. Grimes 			needoutput = 1;
2284b8152ba7SAndre Oppermann 		} else if (!tcp_timer_active(tp, TT_PERSIST))
2285b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
2286a0292f23SGarrett Wollman 
2287a0292f23SGarrett Wollman 		/*
2288a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
2289a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
2290a0292f23SGarrett Wollman 		 */
2291a0292f23SGarrett Wollman 		if (acked == 0)
2292a0292f23SGarrett Wollman 			goto step6;
2293a0292f23SGarrett Wollman 
2294df8bae1dSRodney W. Grimes 		/*
2295df8bae1dSRodney W. Grimes 		 * When new data is acked, open the congestion window.
2296df8bae1dSRodney W. Grimes 		 * If the window gives us less than ssthresh packets
2297df8bae1dSRodney W. Grimes 		 * in flight, open exponentially (maxseg per packet).
2298df8bae1dSRodney W. Grimes 		 * Otherwise open linearly: maxseg per window
229910be5648SGarrett Wollman 		 * (maxseg^2 / cwnd per packet).
2300c10eb6d1SBjoern A. Zeeb 		 * If cwnd > maxseg^2, fix the cwnd increment at 1 byte
2301c10eb6d1SBjoern A. Zeeb 		 * to avoid capping cwnd (as suggested in RFC2581).
2302df8bae1dSRodney W. Grimes 		 */
2303603724d3SBjoern A. Zeeb 		if ((!V_tcp_do_newreno && !(tp->t_flags & TF_SACK_PERMIT)) ||
23046d90faf3SPaul Saab 		    !IN_FASTRECOVERY(tp)) {
2305ad3f9ab3SAndre Oppermann 			u_int cw = tp->snd_cwnd;
2306ad3f9ab3SAndre Oppermann 			u_int incr = tp->t_maxseg;
2307df8bae1dSRodney W. Grimes 			if (cw > tp->snd_ssthresh)
2308c10eb6d1SBjoern A. Zeeb 				incr = max((incr * incr / cw), 1);
2309df8bae1dSRodney W. Grimes 			tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale);
2310df8bae1dSRodney W. Grimes 		}
23115905999bSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
2312df8bae1dSRodney W. Grimes 		if (acked > so->so_snd.sb_cc) {
2313df8bae1dSRodney W. Grimes 			tp->snd_wnd -= so->so_snd.sb_cc;
23145905999bSRobert Watson 			sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc);
2315df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
2316df8bae1dSRodney W. Grimes 		} else {
23175905999bSRobert Watson 			sbdrop_locked(&so->so_snd, acked);
2318df8bae1dSRodney W. Grimes 			tp->snd_wnd -= acked;
2319df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
2320df8bae1dSRodney W. Grimes 		}
2321564aab1fSAndre Oppermann 		/* NB: sowwakeup_locked() does an implicit unlock. */
23221e4d7da7SRobert Watson 		sowwakeup_locked(so);
2323e8949f74SAndre Oppermann 		/* Detect una wraparound. */
2324603724d3SBjoern A. Zeeb 		if ((V_tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) &&
23256d90faf3SPaul Saab 		    !IN_FASTRECOVERY(tp) &&
23269d11646dSJeffrey Hsu 		    SEQ_GT(tp->snd_una, tp->snd_recover) &&
23279d11646dSJeffrey Hsu 		    SEQ_LEQ(th->th_ack, tp->snd_recover))
23289d11646dSJeffrey Hsu 			tp->snd_recover = th->th_ack - 1;
2329603724d3SBjoern A. Zeeb 		if ((V_tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) &&
23306d90faf3SPaul Saab 		    IN_FASTRECOVERY(tp) &&
23319d11646dSJeffrey Hsu 		    SEQ_GEQ(th->th_ack, tp->snd_recover))
23329d11646dSJeffrey Hsu 			EXIT_FASTRECOVERY(tp);
2333fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
23343529149eSAndre Oppermann 		if (tp->t_flags & TF_SACK_PERMIT) {
23356d90faf3SPaul Saab 			if (SEQ_GT(tp->snd_una, tp->snd_recover))
23366d90faf3SPaul Saab 				tp->snd_recover = tp->snd_una;
23376d90faf3SPaul Saab 		}
2338df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2339df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
2340df8bae1dSRodney W. Grimes 
2341df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2342df8bae1dSRodney W. Grimes 
2343df8bae1dSRodney W. Grimes 		/*
2344df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
2345df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
2346df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
2347df8bae1dSRodney W. Grimes 		 */
2348df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2349df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2350df8bae1dSRodney W. Grimes 				/*
2351df8bae1dSRodney W. Grimes 				 * If we can't receive any more
2352df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
2353df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
2354df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
2355df8bae1dSRodney W. Grimes 				 * we'll hang forever.
2356e8949f74SAndre Oppermann 				 *
2357e8949f74SAndre Oppermann 				 * XXXjl:
2358340c35deSJonathan Lemon 				 * we should release the tp also, and use a
2359340c35deSJonathan Lemon 				 * compressed state.
2360340c35deSJonathan Lemon 				 */
2361c0b99ffaSRobert Watson 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
23627c72af87SMohan Srinivasan 					int timeout;
23637c72af87SMohan Srinivasan 
236403e49181SSeigo Tanimura 					soisdisconnected(so);
23657c72af87SMohan Srinivasan 					timeout = (tcp_fast_finwait2_recycle) ?
23667c72af87SMohan Srinivasan 						tcp_finwait2_timeout : tcp_maxidle;
2367b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_2MSL, timeout);
23684cc20ab1SSeigo Tanimura 				}
2369df8bae1dSRodney W. Grimes 				tp->t_state = TCPS_FIN_WAIT_2;
2370df8bae1dSRodney W. Grimes 			}
2371df8bae1dSRodney W. Grimes 			break;
2372df8bae1dSRodney W. Grimes 
2373df8bae1dSRodney W. Grimes 		/*
2374df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
2375df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
2376df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
2377df8bae1dSRodney W. Grimes 		 * the segment.
2378df8bae1dSRodney W. Grimes 		 */
2379df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
2380df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2381252ca428SRobert Watson 				INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
238211a20fb8SJeffrey Hsu 				tcp_twstart(tp);
2383603724d3SBjoern A. Zeeb 				INP_INFO_WUNLOCK(&V_tcbinfo);
2384340c35deSJonathan Lemon 				m_freem(m);
2385679d9708SAndre Oppermann 				return;
2386df8bae1dSRodney W. Grimes 			}
2387df8bae1dSRodney W. Grimes 			break;
2388df8bae1dSRodney W. Grimes 
2389df8bae1dSRodney W. Grimes 		/*
2390df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
2391df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
2392df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
2393df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
2394df8bae1dSRodney W. Grimes 		 */
2395df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
2396df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2397252ca428SRobert Watson 				INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2398df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
2399df8bae1dSRodney W. Grimes 				goto drop;
2400df8bae1dSRodney W. Grimes 			}
2401df8bae1dSRodney W. Grimes 			break;
2402df8bae1dSRodney W. Grimes 		}
2403df8bae1dSRodney W. Grimes 	}
2404df8bae1dSRodney W. Grimes 
2405df8bae1dSRodney W. Grimes step6:
2406252ca428SRobert Watson 	INP_INFO_LOCK_ASSERT(&V_tcbinfo);
2407252ca428SRobert Watson 	KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED,
2408252ca428SRobert Watson 	    ("tcp_do_segment: step6 ti_locked %d", ti_locked));
24098501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
24107cfc6904SRobert Watson 
2411df8bae1dSRodney W. Grimes 	/*
2412df8bae1dSRodney W. Grimes 	 * Update window information.
2413df8bae1dSRodney W. Grimes 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2414df8bae1dSRodney W. Grimes 	 */
2415fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) &&
2416fb59c426SYoshinobu Inoue 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
2417fb59c426SYoshinobu Inoue 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
2418fb59c426SYoshinobu Inoue 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
2419df8bae1dSRodney W. Grimes 		/* keep track of pure window updates */
2420fb59c426SYoshinobu Inoue 		if (tlen == 0 &&
2421fb59c426SYoshinobu Inoue 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
2422603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_rcvwinupd++;
2423df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
2424fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq;
2425fb59c426SYoshinobu Inoue 		tp->snd_wl2 = th->th_ack;
2426df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
2427df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
2428df8bae1dSRodney W. Grimes 		needoutput = 1;
2429df8bae1dSRodney W. Grimes 	}
2430df8bae1dSRodney W. Grimes 
2431df8bae1dSRodney W. Grimes 	/*
2432df8bae1dSRodney W. Grimes 	 * Process segments with URG.
2433df8bae1dSRodney W. Grimes 	 */
2434fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
2435df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2436df8bae1dSRodney W. Grimes 		/*
2437df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
2438df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
2439df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
2440df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
2441df8bae1dSRodney W. Grimes 		 */
244218ad5842SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
2443fb59c426SYoshinobu Inoue 		if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
2444fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
2445fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
244618ad5842SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);	/* XXX */
2447df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
2448df8bae1dSRodney W. Grimes 		}
2449df8bae1dSRodney W. Grimes 		/*
2450df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
2451df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
2452df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2453df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
2454df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
2455df8bae1dSRodney W. Grimes 		 *
2456df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
2457df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
2458df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
2459df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
2460df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
2461df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
2462df8bae1dSRodney W. Grimes 		 */
2463fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2464fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
2465df8bae1dSRodney W. Grimes 			so->so_oobmark = so->so_rcv.sb_cc +
2466df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
2467927c5ceaSRobert Watson 			if (so->so_oobmark == 0)
2468c0b99ffaSRobert Watson 				so->so_rcv.sb_state |= SBS_RCVATMARK;
2469df8bae1dSRodney W. Grimes 			sohasoutofband(so);
2470df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2471df8bae1dSRodney W. Grimes 		}
247218ad5842SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
2473df8bae1dSRodney W. Grimes 		/*
2474df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
2475df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
2476df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
2477df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
2478df8bae1dSRodney W. Grimes 		 */
247930613f56SJeffrey Hsu 		if (th->th_urp <= (u_long)tlen &&
248030613f56SJeffrey Hsu 		    !(so->so_options & SO_OOBINLINE)) {
248130613f56SJeffrey Hsu 			/* hdr drop is delayed */
248230613f56SJeffrey Hsu 			tcp_pulloutofband(so, th, m, drop_hdrlen);
248330613f56SJeffrey Hsu 		}
2484c068736aSJeffrey Hsu 	} else {
2485df8bae1dSRodney W. Grimes 		/*
2486df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
2487df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
2488df8bae1dSRodney W. Grimes 		 * with the receive window.
2489df8bae1dSRodney W. Grimes 		 */
2490df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2491df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
2492c068736aSJeffrey Hsu 	}
2493df8bae1dSRodney W. Grimes dodata:							/* XXX */
2494252ca428SRobert Watson 	INP_INFO_LOCK_ASSERT(&V_tcbinfo);
2495252ca428SRobert Watson 	KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED,
2496252ca428SRobert Watson 	    ("tcp_do_segment: dodata ti_locked %d", ti_locked));
24978501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
24987cfc6904SRobert Watson 
2499df8bae1dSRodney W. Grimes 	/*
2500df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
2501df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
2502df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
2503df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
2504df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
2505df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
2506df8bae1dSRodney W. Grimes 	 */
2507fb59c426SYoshinobu Inoue 	if ((tlen || (thflags & TH_FIN)) &&
2508df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
250969e03620SPaul Saab 		tcp_seq save_start = th->th_seq;
2510fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
2511e4b64281SJesper Skriver 		/*
2512c068736aSJeffrey Hsu 		 * Insert segment which includes th into TCP reassembly queue
2513c068736aSJeffrey Hsu 		 * with control block tp.  Set thflags to whether reassembly now
2514c068736aSJeffrey Hsu 		 * includes a segment with FIN.  This handles the common case
2515c068736aSJeffrey Hsu 		 * inline (segment is the next to be received on an established
2516c068736aSJeffrey Hsu 		 * connection, and the queue is empty), avoiding linkage into
2517c068736aSJeffrey Hsu 		 * and removal from the queue and repetition of various
2518c068736aSJeffrey Hsu 		 * conversions.
2519c068736aSJeffrey Hsu 		 * Set DELACK for segments received in order, but ack
2520c068736aSJeffrey Hsu 		 * immediately when segments are out of order (so
2521c068736aSJeffrey Hsu 		 * fast retransmit can work).
2522e4b64281SJesper Skriver 		 */
2523e4b64281SJesper Skriver 		if (th->th_seq == tp->rcv_nxt &&
2524e4b64281SJesper Skriver 		    LIST_EMPTY(&tp->t_segq) &&
2525e4b64281SJesper Skriver 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
2526e4b64281SJesper Skriver 			if (DELAY_ACK(tp))
25273bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
2528e4b64281SJesper Skriver 			else
2529e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
2530e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
2531e4b64281SJesper Skriver 			thflags = th->th_flags & TH_FIN;
2532603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_rcvpack++;
2533603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_rcvbyte += tlen;
2534e4b64281SJesper Skriver 			ND6_HINT(tp);
25351e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
2536c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
2537c1c36a2cSMike Silbersack 				m_freem(m);
2538c1c36a2cSMike Silbersack 			else
25391e4d7da7SRobert Watson 				sbappendstream_locked(&so->so_rcv, m);
2540e8949f74SAndre Oppermann 			/* NB: sorwakeup_locked() does an implicit unlock. */
25411e4d7da7SRobert Watson 			sorwakeup_locked(so);
2542e4b64281SJesper Skriver 		} else {
2543e8949f74SAndre Oppermann 			/*
2544e8949f74SAndre Oppermann 			 * XXX: Due to the header drop above "th" is
2545e8949f74SAndre Oppermann 			 * theoretically invalid by now.  Fortunately
2546e8949f74SAndre Oppermann 			 * m_adj() doesn't actually frees any mbufs
2547e8949f74SAndre Oppermann 			 * when trimming from the head.
2548e8949f74SAndre Oppermann 			 */
2549e4b64281SJesper Skriver 			thflags = tcp_reass(tp, th, &tlen, m);
2550e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
2551e4b64281SJesper Skriver 		}
25523529149eSAndre Oppermann 		if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT))
2553f194524fSAndre Oppermann 			tcp_update_sack_list(tp, save_start, save_start + tlen);
2554302ce8d6SAndre Oppermann #if 0
2555df8bae1dSRodney W. Grimes 		/*
2556df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
2557df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
2558df8bae1dSRodney W. Grimes 		 * buffer size.
2559302ce8d6SAndre Oppermann 		 * XXX: Unused.
2560df8bae1dSRodney W. Grimes 		 */
2561df8bae1dSRodney W. Grimes 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2562302ce8d6SAndre Oppermann #endif
2563df8bae1dSRodney W. Grimes 	} else {
2564df8bae1dSRodney W. Grimes 		m_freem(m);
2565fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
2566df8bae1dSRodney W. Grimes 	}
2567df8bae1dSRodney W. Grimes 
2568df8bae1dSRodney W. Grimes 	/*
2569df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
2570df8bae1dSRodney W. Grimes 	 * that the connection is closing.
2571df8bae1dSRodney W. Grimes 	 */
2572fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
2573df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2574df8bae1dSRodney W. Grimes 			socantrcvmore(so);
2575a0292f23SGarrett Wollman 			/*
2576a0292f23SGarrett Wollman 			 * If connection is half-synchronized
2577d3eede9dSAndras Olah 			 * (ie NEEDSYN flag on) then delay ACK,
2578a0292f23SGarrett Wollman 			 * so it may be piggybacked when SYN is sent.
2579a0292f23SGarrett Wollman 			 * Otherwise, since we received a FIN then no
2580a0292f23SGarrett Wollman 			 * more input can be expected, send ACK now.
2581a0292f23SGarrett Wollman 			 */
25823bfd6421SJonathan Lemon 			if (tp->t_flags & TF_NEEDSYN)
25833bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
2584a0292f23SGarrett Wollman 			else
2585df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
2586df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
2587df8bae1dSRodney W. Grimes 		}
2588df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2589df8bae1dSRodney W. Grimes 
2590df8bae1dSRodney W. Grimes 		/*
2591df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
2592df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
2593df8bae1dSRodney W. Grimes 		 */
2594df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
25959b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
25969b8b58e0SJonathan Lemon 			/* FALLTHROUGH */
2597df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
2598df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSE_WAIT;
2599df8bae1dSRodney W. Grimes 			break;
2600df8bae1dSRodney W. Grimes 
2601df8bae1dSRodney W. Grimes 		/*
2602df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2603df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
2604df8bae1dSRodney W. Grimes 		 */
2605df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2606df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSING;
2607df8bae1dSRodney W. Grimes 			break;
2608df8bae1dSRodney W. Grimes 
2609df8bae1dSRodney W. Grimes 		/*
2610df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2611df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
2612df8bae1dSRodney W. Grimes 		 * standard timers.
2613df8bae1dSRodney W. Grimes 		 */
2614df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
2615252ca428SRobert Watson 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2616252ca428SRobert Watson 			KASSERT(ti_locked == TI_WLOCKED, ("%s: dodata "
2617252ca428SRobert Watson 			    "TCP_FIN_WAIT_2 ti_locked: %d", __func__,
2618252ca428SRobert Watson 			    ti_locked));
2619252ca428SRobert Watson 
2620340c35deSJonathan Lemon 			tcp_twstart(tp);
2621603724d3SBjoern A. Zeeb 			INP_INFO_WUNLOCK(&V_tcbinfo);
2622679d9708SAndre Oppermann 			return;
2623df8bae1dSRodney W. Grimes 		}
2624df8bae1dSRodney W. Grimes 	}
2625252ca428SRobert Watson 	if (ti_locked == TI_RLOCKED)
2626252ca428SRobert Watson 		INP_INFO_RUNLOCK(&V_tcbinfo);
2627252ca428SRobert Watson 	else if (ti_locked == TI_WLOCKED)
2628603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
2629252ca428SRobert Watson 	else
2630252ca428SRobert Watson 		panic("%s: dodata epilogue ti_locked %d", __func__,
2631252ca428SRobert Watson 		    ti_locked);
2632252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
2633252ca428SRobert Watson 
2634610ee2f9SDavid Greenman #ifdef TCPDEBUG
26354cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
2636fb59c426SYoshinobu Inoue 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
2637fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2638610ee2f9SDavid Greenman #endif
2639df8bae1dSRodney W. Grimes 
2640df8bae1dSRodney W. Grimes 	/*
2641df8bae1dSRodney W. Grimes 	 * Return any desired output.
2642df8bae1dSRodney W. Grimes 	 */
2643df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
2644df8bae1dSRodney W. Grimes 		(void) tcp_output(tp);
26457792ea27SJeffrey Hsu 
2646a14c749fSJonathan Lemon check_delack:
2647252ca428SRobert Watson 	KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d",
2648252ca428SRobert Watson 	    __func__, ti_locked));
2649603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
26508501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
2651252ca428SRobert Watson 
2652a14c749fSJonathan Lemon 	if (tp->t_flags & TF_DELACK) {
26533bfd6421SJonathan Lemon 		tp->t_flags &= ~TF_DELACK;
2654b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
26553bfd6421SJonathan Lemon 	}
26568501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
2657679d9708SAndre Oppermann 	return;
2658df8bae1dSRodney W. Grimes 
2659df8bae1dSRodney W. Grimes dropafterack:
2660252ca428SRobert Watson 	KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED,
2661252ca428SRobert Watson 	    ("tcp_do_segment: dropafterack ti_locked %d", ti_locked));
2662252ca428SRobert Watson 
2663df8bae1dSRodney W. Grimes 	/*
2664df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
2665df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
266680ab7c0eSGarrett Wollman 	 *
266780ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
266880ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
266980ab7c0eSGarrett Wollman 	 * RST have been dropped.
267080ab7c0eSGarrett Wollman 	 *
267180ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
267280ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
267380ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
267480ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
267580ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
267680ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
2677df8bae1dSRodney W. Grimes 	 */
2678fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
2679fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
2680a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
2681a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2682a57815efSBosko Milekic 		goto dropwithreset;
2683a57815efSBosko Milekic 	}
2684a0292f23SGarrett Wollman #ifdef TCPDEBUG
26854cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
2686fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2687fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2688a0292f23SGarrett Wollman #endif
2689252ca428SRobert Watson 	if (ti_locked == TI_RLOCKED)
2690252ca428SRobert Watson 		INP_INFO_RUNLOCK(&V_tcbinfo);
2691252ca428SRobert Watson 	else if (ti_locked == TI_WLOCKED)
2692603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
2693252ca428SRobert Watson 	else
2694252ca428SRobert Watson 		panic("%s: dropafterack epilogue ti_locked %d", __func__,
2695252ca428SRobert Watson 		    ti_locked);
2696252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
2697252ca428SRobert Watson 
2698df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
2699df8bae1dSRodney W. Grimes 	(void) tcp_output(tp);
27008501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
2701d6915262SRobert Watson 	m_freem(m);
2702679d9708SAndre Oppermann 	return;
2703df8bae1dSRodney W. Grimes 
2704df8bae1dSRodney W. Grimes dropwithreset:
2705252ca428SRobert Watson 	if (ti_locked == TI_RLOCKED)
2706252ca428SRobert Watson 		INP_INFO_RUNLOCK(&V_tcbinfo);
2707252ca428SRobert Watson 	else if (ti_locked == TI_WLOCKED)
2708dd8ac7f9SRobert Watson 		INP_INFO_WUNLOCK(&V_tcbinfo);
2709252ca428SRobert Watson 	else
2710252ca428SRobert Watson 		panic("%s: dropwithreset ti_locked %d", __func__, ti_locked);
2711252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
2712a57815efSBosko Milekic 
2713a0ca0871SRobert Watson 	if (tp != NULL) {
2714302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
27158501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
2716dd8ac7f9SRobert Watson 	} else
2717a0ca0871SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
2718679d9708SAndre Oppermann 	return;
2719df8bae1dSRodney W. Grimes 
2720df8bae1dSRodney W. Grimes drop:
2721252ca428SRobert Watson 	if (ti_locked == TI_RLOCKED)
2722252ca428SRobert Watson 		INP_INFO_RUNLOCK(&V_tcbinfo);
2723252ca428SRobert Watson 	else if (ti_locked == TI_WLOCKED)
2724252ca428SRobert Watson 		INP_INFO_WUNLOCK(&V_tcbinfo);
2725252ca428SRobert Watson #ifdef INVARIANTS
2726252ca428SRobert Watson 	else
2727252ca428SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
2728252ca428SRobert Watson #endif
2729252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
2730252ca428SRobert Watson 
2731df8bae1dSRodney W. Grimes 	/*
2732df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
2733df8bae1dSRodney W. Grimes 	 */
2734610ee2f9SDavid Greenman #ifdef TCPDEBUG
27351c53f806SRobert Watson 	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2736fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2737fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2738a0292f23SGarrett Wollman #endif
27391c53f806SRobert Watson 	if (tp != NULL)
27408501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
2741d6915262SRobert Watson 	m_freem(m);
2742302ce8d6SAndre Oppermann }
2743302ce8d6SAndre Oppermann 
2744302ce8d6SAndre Oppermann /*
27450ca3f933SAndre Oppermann  * Issue RST and make ACK acceptable to originator of segment.
27460ca3f933SAndre Oppermann  * The mbuf must still include the original packet header.
27470ca3f933SAndre Oppermann  * tp may be NULL.
2748302ce8d6SAndre Oppermann  */
2749302ce8d6SAndre Oppermann static void
2750302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp,
2751302ce8d6SAndre Oppermann     int tlen, int rstreason)
2752302ce8d6SAndre Oppermann {
2753302ce8d6SAndre Oppermann 	struct ip *ip;
2754302ce8d6SAndre Oppermann #ifdef INET6
2755302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6;
2756302ce8d6SAndre Oppermann #endif
27577a3244ccSRobert Watson 
27587a3244ccSRobert Watson 	if (tp != NULL) {
27598501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
27607a3244ccSRobert Watson 	}
27617a3244ccSRobert Watson 
27620ca3f933SAndre Oppermann 	/* Don't bother if destination was broadcast/multicast. */
2763302ce8d6SAndre Oppermann 	if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
2764302ce8d6SAndre Oppermann 		goto drop;
2765302ce8d6SAndre Oppermann #ifdef INET6
2766302ce8d6SAndre Oppermann 	if (mtod(m, struct ip *)->ip_v == 6) {
2767302ce8d6SAndre Oppermann 		ip6 = mtod(m, struct ip6_hdr *);
2768302ce8d6SAndre Oppermann 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
2769302ce8d6SAndre Oppermann 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
2770302ce8d6SAndre Oppermann 			goto drop;
2771302ce8d6SAndre Oppermann 		/* IPv6 anycast check is done at tcp6_input() */
2772302ce8d6SAndre Oppermann 	} else
2773302ce8d6SAndre Oppermann #endif
2774302ce8d6SAndre Oppermann 	{
2775302ce8d6SAndre Oppermann 		ip = mtod(m, struct ip *);
2776302ce8d6SAndre Oppermann 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
2777302ce8d6SAndre Oppermann 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
2778302ce8d6SAndre Oppermann 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
2779302ce8d6SAndre Oppermann 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
2780302ce8d6SAndre Oppermann 			goto drop;
2781302ce8d6SAndre Oppermann 	}
2782302ce8d6SAndre Oppermann 
2783302ce8d6SAndre Oppermann 	/* Perform bandwidth limiting. */
2784302ce8d6SAndre Oppermann 	if (badport_bandlim(rstreason) < 0)
2785302ce8d6SAndre Oppermann 		goto drop;
2786302ce8d6SAndre Oppermann 
2787302ce8d6SAndre Oppermann 	/* tcp_respond consumes the mbuf chain. */
2788302ce8d6SAndre Oppermann 	if (th->th_flags & TH_ACK) {
2789302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0,
2790302ce8d6SAndre Oppermann 		    th->th_ack, TH_RST);
2791302ce8d6SAndre Oppermann 	} else {
2792302ce8d6SAndre Oppermann 		if (th->th_flags & TH_SYN)
2793302ce8d6SAndre Oppermann 			tlen++;
2794302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
2795302ce8d6SAndre Oppermann 		    (tcp_seq)0, TH_RST|TH_ACK);
2796302ce8d6SAndre Oppermann 	}
2797302ce8d6SAndre Oppermann 	return;
2798302ce8d6SAndre Oppermann drop:
2799302ce8d6SAndre Oppermann 	m_freem(m);
2800df8bae1dSRodney W. Grimes }
2801df8bae1dSRodney W. Grimes 
2802be2ac88cSJonathan Lemon /*
2803be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
2804be2ac88cSJonathan Lemon  */
28050312fbe9SPoul-Henning Kamp static void
2806ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags)
2807df8bae1dSRodney W. Grimes {
28088b615593SMarko Zec 	INIT_VNET_INET(curvnet);
2809df8bae1dSRodney W. Grimes 	int opt, optlen;
2810df8bae1dSRodney W. Grimes 
2811be2ac88cSJonathan Lemon 	to->to_flags = 0;
2812df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
2813df8bae1dSRodney W. Grimes 		opt = cp[0];
2814df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
2815df8bae1dSRodney W. Grimes 			break;
2816df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
2817df8bae1dSRodney W. Grimes 			optlen = 1;
2818df8bae1dSRodney W. Grimes 		else {
2819b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
2820b474779fSJun-ichiro itojun Hagino 				break;
2821df8bae1dSRodney W. Grimes 			optlen = cp[1];
2822b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
2823df8bae1dSRodney W. Grimes 				break;
2824df8bae1dSRodney W. Grimes 		}
2825df8bae1dSRodney W. Grimes 		switch (opt) {
2826df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
2827df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
2828df8bae1dSRodney W. Grimes 				continue;
2829f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
2830df8bae1dSRodney W. Grimes 				continue;
2831be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
2832be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
2833be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
2834fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
2835df8bae1dSRodney W. Grimes 			break;
2836df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
2837df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
2838df8bae1dSRodney W. Grimes 				continue;
2839f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
2840df8bae1dSRodney W. Grimes 				continue;
2841be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
284202a1a643SAndre Oppermann 			to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT);
2843df8bae1dSRodney W. Grimes 			break;
2844df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
2845df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
2846df8bae1dSRodney W. Grimes 				continue;
2847be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
2848a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2849a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
2850fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
2851a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
2852a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
2853fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
2854df8bae1dSRodney W. Grimes 			break;
28551cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
28561cfd4b53SBruce M Simpson 		/*
28571cfd4b53SBruce M Simpson 		 * XXX In order to reply to a host which has set the
28581cfd4b53SBruce M Simpson 		 * TCP_SIGNATURE option in its initial SYN, we have to
28591cfd4b53SBruce M Simpson 		 * record the fact that the option was observed here
28601cfd4b53SBruce M Simpson 		 * for the syncache code to perform the correct response.
28611cfd4b53SBruce M Simpson 		 */
28621cfd4b53SBruce M Simpson 		case TCPOPT_SIGNATURE:
28631cfd4b53SBruce M Simpson 			if (optlen != TCPOLEN_SIGNATURE)
28641cfd4b53SBruce M Simpson 				continue;
2865df47e437SAndre Oppermann 			to->to_flags |= TOF_SIGNATURE;
2866df47e437SAndre Oppermann 			to->to_signature = cp + 2;
28671cfd4b53SBruce M Simpson 			break;
2868265ed012SBruce M Simpson #endif
28696d90faf3SPaul Saab 		case TCPOPT_SACK_PERMITTED:
2870f72167f4SAndre Oppermann 			if (optlen != TCPOLEN_SACK_PERMITTED)
28716d90faf3SPaul Saab 				continue;
2872f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
2873f72167f4SAndre Oppermann 				continue;
2874603724d3SBjoern A. Zeeb 			if (!V_tcp_do_sack)
2875f72167f4SAndre Oppermann 				continue;
2876fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACKPERM;
28776d90faf3SPaul Saab 			break;
28786d90faf3SPaul Saab 		case TCPOPT_SACK:
28795a53ca16SPaul Saab 			if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
28806d90faf3SPaul Saab 				continue;
2881b728e902SAndre Oppermann 			if (flags & TO_SYN)
2882b728e902SAndre Oppermann 				continue;
2883fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACK;
28845a53ca16SPaul Saab 			to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
28855a53ca16SPaul Saab 			to->to_sacks = cp + 2;
2886603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_sack_rcv_blocks++;
28876d90faf3SPaul Saab 			break;
2888be2ac88cSJonathan Lemon 		default:
2889be2ac88cSJonathan Lemon 			continue;
2890df8bae1dSRodney W. Grimes 		}
2891df8bae1dSRodney W. Grimes 	}
2892df8bae1dSRodney W. Grimes }
2893df8bae1dSRodney W. Grimes 
2894df8bae1dSRodney W. Grimes /*
2895df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
2896df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
2897df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
2898df8bae1dSRodney W. Grimes  * sequencing purposes.
2899df8bae1dSRodney W. Grimes  */
29000312fbe9SPoul-Henning Kamp static void
2901ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m,
2902ad3f9ab3SAndre Oppermann     int off)
2903df8bae1dSRodney W. Grimes {
2904fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
2905df8bae1dSRodney W. Grimes 
2906df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
2907df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
2908df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
2909df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
2910df8bae1dSRodney W. Grimes 
29118501a69cSRobert Watson 			INP_WLOCK_ASSERT(tp->t_inpcb);
29127a3244ccSRobert Watson 
2913df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
2914df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
2915df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
2916df8bae1dSRodney W. Grimes 			m->m_len--;
291769a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
291869a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
2919df8bae1dSRodney W. Grimes 			return;
2920df8bae1dSRodney W. Grimes 		}
2921df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
2922df8bae1dSRodney W. Grimes 		m = m->m_next;
29234dfdffe9SAndre Oppermann 		if (m == NULL)
2924df8bae1dSRodney W. Grimes 			break;
2925df8bae1dSRodney W. Grimes 	}
2926df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
2927df8bae1dSRodney W. Grimes }
2928df8bae1dSRodney W. Grimes 
2929df8bae1dSRodney W. Grimes /*
2930df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
2931df8bae1dSRodney W. Grimes  * and update averages and current timeout.
2932df8bae1dSRodney W. Grimes  */
29330312fbe9SPoul-Henning Kamp static void
2934ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt)
2935df8bae1dSRodney W. Grimes {
29368b615593SMarko Zec 	INIT_VNET_INET(tp->t_inpcb->inp_vnet);
2937ad3f9ab3SAndre Oppermann 	int delta;
2938233e8c18SGarrett Wollman 
29398501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
29402be3bf22SRobert Watson 
2941603724d3SBjoern A. Zeeb 	V_tcpstat.tcps_rttupdated++;
2942233e8c18SGarrett Wollman 	tp->t_rttupdated++;
2943233e8c18SGarrett Wollman 	if (tp->t_srtt != 0) {
2944233e8c18SGarrett Wollman 		/*
2945233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
2946233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
2947233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
2948233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
2949233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
2950233e8c18SGarrett Wollman 		 */
2951233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
2952233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
2953233e8c18SGarrett Wollman 
2954233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
2955233e8c18SGarrett Wollman 			tp->t_srtt = 1;
2956233e8c18SGarrett Wollman 
2957233e8c18SGarrett Wollman 		/*
2958233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
2959233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
2960233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
2961233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
2962233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
2963233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
2964233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
2965233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
2966233e8c18SGarrett Wollman 		 */
2967233e8c18SGarrett Wollman 		if (delta < 0)
2968233e8c18SGarrett Wollman 			delta = -delta;
2969233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
2970233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
2971233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
29721fcc99b5SMatthew Dillon 		if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
29731fcc99b5SMatthew Dillon 		    tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
2974233e8c18SGarrett Wollman 	} else {
2975233e8c18SGarrett Wollman 		/*
2976233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
2977233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
2978233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
2979233e8c18SGarrett Wollman 		 */
2980233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
2981233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
29821fcc99b5SMatthew Dillon 		tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
2983233e8c18SGarrett Wollman 	}
29849b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
2985df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
2986df8bae1dSRodney W. Grimes 
2987df8bae1dSRodney W. Grimes 	/*
2988df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
2989df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
2990df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
2991df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
2992df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
2993df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
2994df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
2995df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
2996df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
2997df8bae1dSRodney W. Grimes 	 */
2998233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
29999e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
3000df8bae1dSRodney W. Grimes 
3001df8bae1dSRodney W. Grimes 	/*
3002df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
3003df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
3004df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
3005df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
3006df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
3007df8bae1dSRodney W. Grimes 	 */
3008df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
3009df8bae1dSRodney W. Grimes }
3010df8bae1dSRodney W. Grimes 
3011df8bae1dSRodney W. Grimes /*
3012df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
3013df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
3014df8bae1dSRodney W. Grimes  * If none, use an mss that can be handled on the outgoing
3015df8bae1dSRodney W. Grimes  * interface without forcing IP to fragment; if bigger than
3016df8bae1dSRodney W. Grimes  * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
3017df8bae1dSRodney W. Grimes  * to utilize large mbufs.  If no route is found, route has no mtu,
3018df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
3019df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
3020df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
3021df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
3022df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
3023df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
3024df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
3025a0292f23SGarrett Wollman  *
3026a0292f23SGarrett Wollman  * Also take into account the space needed for options that we
3027a0292f23SGarrett Wollman  * send regularly.  Make maxseg shorter by that amount to assure
3028a0292f23SGarrett Wollman  * that we can send maxseg amount of data even when the options
3029a0292f23SGarrett Wollman  * are present.  Store the upper limit of the length of options plus
3030a0292f23SGarrett Wollman  * data in maxopd.
3031a0292f23SGarrett Wollman  *
3032a0292f23SGarrett Wollman  * In case of T/TCP, we call this routine during implicit connection
3033a0292f23SGarrett Wollman  * setup as well (offer = -1), to initialize maxseg from the cached
3034a0292f23SGarrett Wollman  * MSS of our peer.
303597d8d152SAndre Oppermann  *
303697d8d152SAndre Oppermann  * NOTE that this routine is only called when we process an incoming
303797d8d152SAndre Oppermann  * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt().
3038df8bae1dSRodney W. Grimes  */
3039a0292f23SGarrett Wollman void
304091d6cfa6SBjoern A. Zeeb tcp_mss_update(struct tcpcb *tp, int offer,
304191d6cfa6SBjoern A. Zeeb     struct hc_metrics_lite *metricptr, int *mtuflags)
3042df8bae1dSRodney W. Grimes {
30438b615593SMarko Zec 	INIT_VNET_INET(tp->t_inpcb->inp_vnet);
30443cee92e0SBjoern A. Zeeb 	int mss;
304597d8d152SAndre Oppermann 	u_long maxmtu;
3046c068736aSJeffrey Hsu 	struct inpcb *inp = tp->t_inpcb;
304797d8d152SAndre Oppermann 	struct hc_metrics_lite metrics;
3048a0292f23SGarrett Wollman 	int origoffer = offer;
3049fb59c426SYoshinobu Inoue #ifdef INET6
3050c068736aSJeffrey Hsu 	int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
3051c068736aSJeffrey Hsu 	size_t min_protoh = isipv6 ?
3052c068736aSJeffrey Hsu 			    sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
3053c068736aSJeffrey Hsu 			    sizeof (struct tcpiphdr);
3054c068736aSJeffrey Hsu #else
3055c068736aSJeffrey Hsu 	const size_t min_protoh = sizeof(struct tcpiphdr);
3056fb59c426SYoshinobu Inoue #endif
3057df8bae1dSRodney W. Grimes 
30588501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
30597a3244ccSRobert Watson 
3060e8949f74SAndre Oppermann 	/* Initialize. */
306197d8d152SAndre Oppermann #ifdef INET6
306297d8d152SAndre Oppermann 	if (isipv6) {
306391d6cfa6SBjoern A. Zeeb 		maxmtu = tcp_maxmtu6(&inp->inp_inc, mtuflags);
3064603724d3SBjoern A. Zeeb 		tp->t_maxopd = tp->t_maxseg = V_tcp_v6mssdflt;
306597d8d152SAndre Oppermann 	} else
306697d8d152SAndre Oppermann #endif
306797d8d152SAndre Oppermann 	{
306891d6cfa6SBjoern A. Zeeb 		maxmtu = tcp_maxmtu(&inp->inp_inc, mtuflags);
3069603724d3SBjoern A. Zeeb 		tp->t_maxopd = tp->t_maxseg = V_tcp_mssdflt;
3070df8bae1dSRodney W. Grimes 	}
3071df8bae1dSRodney W. Grimes 
307297d8d152SAndre Oppermann 	/*
3073e8949f74SAndre Oppermann 	 * No route to sender, stay with default mss and return.
307497d8d152SAndre Oppermann 	 */
30756f01cac6SBjoern A. Zeeb 	if (maxmtu == 0) {
30766f01cac6SBjoern A. Zeeb 		/*
30778e5c87f4SBjoern A. Zeeb 		 * In case we return early we need to initialize metrics
30786f01cac6SBjoern A. Zeeb 		 * to a defined state as tcp_hc_get() would do for us
30796f01cac6SBjoern A. Zeeb 		 * if there was no cache hit.
30806f01cac6SBjoern A. Zeeb 		 */
30816f01cac6SBjoern A. Zeeb 		if (metricptr != NULL)
30826f01cac6SBjoern A. Zeeb 			bzero(metricptr, sizeof(struct hc_metrics_lite));
308397d8d152SAndre Oppermann 		return;
30846f01cac6SBjoern A. Zeeb 	}
308597d8d152SAndre Oppermann 
3086e8949f74SAndre Oppermann 	/* What have we got? */
308797d8d152SAndre Oppermann 	switch (offer) {
308897d8d152SAndre Oppermann 		case 0:
308997d8d152SAndre Oppermann 			/*
309097d8d152SAndre Oppermann 			 * Offer == 0 means that there was no MSS on the SYN
3091c3b02504SBjoern A. Zeeb 			 * segment, in this case we use tcp_mssdflt as
3092c3b02504SBjoern A. Zeeb 			 * already assigned to t_maxopd above.
309397d8d152SAndre Oppermann 			 */
3094c3b02504SBjoern A. Zeeb 			offer = tp->t_maxopd;
309597d8d152SAndre Oppermann 			break;
309697d8d152SAndre Oppermann 
309797d8d152SAndre Oppermann 		case -1:
3098a0292f23SGarrett Wollman 			/*
3099c94c54e4SAndre Oppermann 			 * Offer == -1 means that we didn't receive SYN yet.
3100a0292f23SGarrett Wollman 			 */
310197d8d152SAndre Oppermann 			/* FALLTHROUGH */
310297d8d152SAndre Oppermann 
310397d8d152SAndre Oppermann 		default:
3104a0292f23SGarrett Wollman 			/*
310553369ac9SAndre Oppermann 			 * Prevent DoS attack with too small MSS. Round up
310653369ac9SAndre Oppermann 			 * to at least minmss.
310753369ac9SAndre Oppermann 			 */
3108603724d3SBjoern A. Zeeb 			offer = max(offer, V_tcp_minmss);
310997d8d152SAndre Oppermann 	}
3110a0292f23SGarrett Wollman 
3111df8bae1dSRodney W. Grimes 	/*
3112e8949f74SAndre Oppermann 	 * rmx information is now retrieved from tcp_hostcache.
3113df8bae1dSRodney W. Grimes 	 */
311497d8d152SAndre Oppermann 	tcp_hc_get(&inp->inp_inc, &metrics);
31153cee92e0SBjoern A. Zeeb 	if (metricptr != NULL)
31163cee92e0SBjoern A. Zeeb 		bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite));
311797d8d152SAndre Oppermann 
3118df8bae1dSRodney W. Grimes 	/*
3119e8949f74SAndre Oppermann 	 * If there's a discovered mtu int tcp hostcache, use it
3120fb59c426SYoshinobu Inoue 	 * else, use the link mtu.
3121df8bae1dSRodney W. Grimes 	 */
312297d8d152SAndre Oppermann 	if (metrics.rmx_mtu)
31232d166c02SAndre Oppermann 		mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
3124c068736aSJeffrey Hsu 	else {
312531b3783cSHajimu UMEMOTO #ifdef INET6
3126fb59c426SYoshinobu Inoue 		if (isipv6) {
312797d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3128603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
312997d8d152SAndre Oppermann 			    !in6_localaddr(&inp->in6p_faddr))
3130603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_v6mssdflt);
3131b3399803SHajimu UMEMOTO 		} else
3132b3399803SHajimu UMEMOTO #endif
313397d8d152SAndre Oppermann 		{
313497d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3135603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
313697d8d152SAndre Oppermann 			    !in_localaddr(inp->inp_faddr))
3137603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_mssdflt);
3138a0292f23SGarrett Wollman 		}
31393cee92e0SBjoern A. Zeeb 		/*
31403cee92e0SBjoern A. Zeeb 		 * XXX - The above conditional (mss = maxmtu - min_protoh)
31413cee92e0SBjoern A. Zeeb 		 * probably violates the TCP spec.
31423cee92e0SBjoern A. Zeeb 		 * The problem is that, since we don't know the
31433cee92e0SBjoern A. Zeeb 		 * other end's MSS, we are supposed to use a conservative
31443cee92e0SBjoern A. Zeeb 		 * default.  But, if we do that, then MTU discovery will
31453cee92e0SBjoern A. Zeeb 		 * never actually take place, because the conservative
31463cee92e0SBjoern A. Zeeb 		 * default is much less than the MTUs typically seen
31473cee92e0SBjoern A. Zeeb 		 * on the Internet today.  For the moment, we'll sweep
31483cee92e0SBjoern A. Zeeb 		 * this under the carpet.
31493cee92e0SBjoern A. Zeeb 		 *
31503cee92e0SBjoern A. Zeeb 		 * The conservative default might not actually be a problem
31513cee92e0SBjoern A. Zeeb 		 * if the only case this occurs is when sending an initial
31523cee92e0SBjoern A. Zeeb 		 * SYN with options and data to a host we've never talked
31533cee92e0SBjoern A. Zeeb 		 * to before.  Then, they will reply with an MSS value which
31543cee92e0SBjoern A. Zeeb 		 * will get recorded and the new parameters should get
31553cee92e0SBjoern A. Zeeb 		 * recomputed.  For Further Study.
31563cee92e0SBjoern A. Zeeb 		 */
315797d8d152SAndre Oppermann 	}
3158a0292f23SGarrett Wollman 	mss = min(mss, offer);
315997d8d152SAndre Oppermann 
3160a0292f23SGarrett Wollman 	/*
31613cee92e0SBjoern A. Zeeb 	 * Sanity check: make sure that maxopd will be large
31623cee92e0SBjoern A. Zeeb 	 * enough to allow some data on segments even if the
31633cee92e0SBjoern A. Zeeb 	 * all the option space is used (40bytes).  Otherwise
31643cee92e0SBjoern A. Zeeb 	 * funny things may happen in tcp_output.
31653cee92e0SBjoern A. Zeeb 	 */
31663cee92e0SBjoern A. Zeeb 	mss = max(mss, 64);
31673cee92e0SBjoern A. Zeeb 
31683cee92e0SBjoern A. Zeeb 	/*
3169a0292f23SGarrett Wollman 	 * maxopd stores the maximum length of data AND options
3170a0292f23SGarrett Wollman 	 * in a segment; maxseg is the amount of data in a normal
3171a0292f23SGarrett Wollman 	 * segment.  We need to store this value (maxopd) apart
3172a0292f23SGarrett Wollman 	 * from maxseg, because now every segment carries options
3173a0292f23SGarrett Wollman 	 * and thus we normally have somewhat less data in segments.
3174a0292f23SGarrett Wollman 	 */
3175a0292f23SGarrett Wollman 	tp->t_maxopd = mss;
3176a0292f23SGarrett Wollman 
3177a0292f23SGarrett Wollman 	/*
3178e8949f74SAndre Oppermann 	 * origoffer==-1 indicates that no segments were received yet.
3179c94c54e4SAndre Oppermann 	 * In this case we just guess.
3180a0292f23SGarrett Wollman 	 */
3181a0292f23SGarrett Wollman 	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
3182a0292f23SGarrett Wollman 	    (origoffer == -1 ||
3183a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
3184a0292f23SGarrett Wollman 		mss -= TCPOLEN_TSTAMP_APPA;
3185a0292f23SGarrett Wollman 
3186df8bae1dSRodney W. Grimes #if	(MCLBYTES & (MCLBYTES - 1)) == 0
3187df8bae1dSRodney W. Grimes 	if (mss > MCLBYTES)
3188df8bae1dSRodney W. Grimes 		mss &= ~(MCLBYTES-1);
3189df8bae1dSRodney W. Grimes #else
3190df8bae1dSRodney W. Grimes 	if (mss > MCLBYTES)
3191df8bae1dSRodney W. Grimes 		mss = mss / MCLBYTES * MCLBYTES;
3192df8bae1dSRodney W. Grimes #endif
319397d8d152SAndre Oppermann 	tp->t_maxseg = mss;
31943cee92e0SBjoern A. Zeeb }
31953cee92e0SBjoern A. Zeeb 
31963cee92e0SBjoern A. Zeeb void
31973cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer)
31983cee92e0SBjoern A. Zeeb {
31993cee92e0SBjoern A. Zeeb 	int rtt, mss;
32003cee92e0SBjoern A. Zeeb 	u_long bufsize;
32013cee92e0SBjoern A. Zeeb 	struct inpcb *inp;
32023cee92e0SBjoern A. Zeeb 	struct socket *so;
32033cee92e0SBjoern A. Zeeb 	struct hc_metrics_lite metrics;
320491d6cfa6SBjoern A. Zeeb 	int mtuflags = 0;
32053cee92e0SBjoern A. Zeeb #ifdef INET6
32063cee92e0SBjoern A. Zeeb 	int isipv6;
32073cee92e0SBjoern A. Zeeb #endif
32083cee92e0SBjoern A. Zeeb 	KASSERT(tp != NULL, ("%s: tp == NULL", __func__));
320997021c24SMarko Zec 	INIT_VNET_INET(tp->t_vnet);
32103cee92e0SBjoern A. Zeeb 
321191d6cfa6SBjoern A. Zeeb 	tcp_mss_update(tp, offer, &metrics, &mtuflags);
32123cee92e0SBjoern A. Zeeb 
32133cee92e0SBjoern A. Zeeb 	mss = tp->t_maxseg;
32143cee92e0SBjoern A. Zeeb 	inp = tp->t_inpcb;
32153cee92e0SBjoern A. Zeeb #ifdef INET6
32163cee92e0SBjoern A. Zeeb 	isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
32173cee92e0SBjoern A. Zeeb #endif
321897d8d152SAndre Oppermann 
3219df8bae1dSRodney W. Grimes 	/*
322097d8d152SAndre Oppermann 	 * If there's a pipesize, change the socket buffer to that size,
322197d8d152SAndre Oppermann 	 * don't change if sb_hiwat is different than default (then it
322297d8d152SAndre Oppermann 	 * has been changed on purpose with setsockopt).
322397d8d152SAndre Oppermann 	 * Make the socket buffers an integral number of mss units;
322497d8d152SAndre Oppermann 	 * if the mss is larger than the socket buffer, decrease the mss.
3225df8bae1dSRodney W. Grimes 	 */
3226c3b02504SBjoern A. Zeeb 	so = inp->inp_socket;
32273f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
322897d8d152SAndre Oppermann 	if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe)
322997d8d152SAndre Oppermann 		bufsize = metrics.rmx_sendpipe;
323097d8d152SAndre Oppermann 	else
3231df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
3232df8bae1dSRodney W. Grimes 	if (bufsize < mss)
3233df8bae1dSRodney W. Grimes 		mss = bufsize;
3234df8bae1dSRodney W. Grimes 	else {
3235df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3236df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3237df8bae1dSRodney W. Grimes 			bufsize = sb_max;
323888c39af3SRuslan Ermilov 		if (bufsize > so->so_snd.sb_hiwat)
32393f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_snd, bufsize, so, NULL);
3240df8bae1dSRodney W. Grimes 	}
32413f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
3242df8bae1dSRodney W. Grimes 	tp->t_maxseg = mss;
3243df8bae1dSRodney W. Grimes 
32443f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
324597d8d152SAndre Oppermann 	if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe)
324697d8d152SAndre Oppermann 		bufsize = metrics.rmx_recvpipe;
324797d8d152SAndre Oppermann 	else
3248df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
3249df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
3250df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3251df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3252df8bae1dSRodney W. Grimes 			bufsize = sb_max;
325388c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
32543f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL);
3255df8bae1dSRodney W. Grimes 	}
32563f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
3257a0292f23SGarrett Wollman 	/*
3258e8949f74SAndre Oppermann 	 * While we're here, check the others too.
3259a0292f23SGarrett Wollman 	 */
326097d8d152SAndre Oppermann 	if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) {
326197d8d152SAndre Oppermann 		tp->t_srtt = rtt;
326297d8d152SAndre Oppermann 		tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
3263603724d3SBjoern A. Zeeb 		V_tcpstat.tcps_usedrtt++;
326497d8d152SAndre Oppermann 		if (metrics.rmx_rttvar) {
326597d8d152SAndre Oppermann 			tp->t_rttvar = metrics.rmx_rttvar;
3266603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_usedrttvar++;
326797d8d152SAndre Oppermann 		} else {
326897d8d152SAndre Oppermann 			/* default variation is +- 1 rtt */
326997d8d152SAndre Oppermann 			tp->t_rttvar =
327097d8d152SAndre Oppermann 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
327197d8d152SAndre Oppermann 		}
327297d8d152SAndre Oppermann 		TCPT_RANGESET(tp->t_rxtcur,
327397d8d152SAndre Oppermann 			      ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
327497d8d152SAndre Oppermann 			      tp->t_rttmin, TCPTV_REXMTMAX);
327597d8d152SAndre Oppermann 	}
327697d8d152SAndre Oppermann 	if (metrics.rmx_ssthresh) {
3277df8bae1dSRodney W. Grimes 		/*
3278df8bae1dSRodney W. Grimes 		 * There's some sort of gateway or interface
3279df8bae1dSRodney W. Grimes 		 * buffer limit on the path.  Use this to set
3280df8bae1dSRodney W. Grimes 		 * the slow start threshhold, but set the
3281df8bae1dSRodney W. Grimes 		 * threshold to no less than 2*mss.
3282df8bae1dSRodney W. Grimes 		 */
328397d8d152SAndre Oppermann 		tp->snd_ssthresh = max(2 * mss, metrics.rmx_ssthresh);
3284603724d3SBjoern A. Zeeb 		V_tcpstat.tcps_usedssthresh++;
3285df8bae1dSRodney W. Grimes 	}
328697d8d152SAndre Oppermann 	if (metrics.rmx_bandwidth)
328797d8d152SAndre Oppermann 		tp->snd_bandwidth = metrics.rmx_bandwidth;
328897d8d152SAndre Oppermann 
328997d8d152SAndre Oppermann 	/*
329097d8d152SAndre Oppermann 	 * Set the slow-start flight size depending on whether this
329197d8d152SAndre Oppermann 	 * is a local network or not.
329297d8d152SAndre Oppermann 	 *
329397d8d152SAndre Oppermann 	 * Extend this so we cache the cwnd too and retrieve it here.
329497d8d152SAndre Oppermann 	 * Make cwnd even bigger than RFC3390 suggests but only if we
329597d8d152SAndre Oppermann 	 * have previous experience with the remote host. Be careful
329697d8d152SAndre Oppermann 	 * not make cwnd bigger than remote receive window or our own
329797d8d152SAndre Oppermann 	 * send socket buffer. Maybe put some additional upper bound
329897d8d152SAndre Oppermann 	 * on the retrieved cwnd. Should do incremental updates to
329997d8d152SAndre Oppermann 	 * hostcache when cwnd collapses so next connection doesn't
330097d8d152SAndre Oppermann 	 * overloads the path again.
330197d8d152SAndre Oppermann 	 *
330297d8d152SAndre Oppermann 	 * RFC3390 says only do this if SYN or SYN/ACK didn't got lost.
330397d8d152SAndre Oppermann 	 * We currently check only in syncache_socket for that.
330497d8d152SAndre Oppermann 	 */
330597d8d152SAndre Oppermann #define TCP_METRICS_CWND
330697d8d152SAndre Oppermann #ifdef TCP_METRICS_CWND
330797d8d152SAndre Oppermann 	if (metrics.rmx_cwnd)
330897d8d152SAndre Oppermann 		tp->snd_cwnd = max(mss,
330997d8d152SAndre Oppermann 				min(metrics.rmx_cwnd / 2,
331097d8d152SAndre Oppermann 				 min(tp->snd_wnd, so->so_snd.sb_hiwat)));
331197d8d152SAndre Oppermann 	else
331297d8d152SAndre Oppermann #endif
3313603724d3SBjoern A. Zeeb 	if (V_tcp_do_rfc3390)
331497d8d152SAndre Oppermann 		tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380));
331597d8d152SAndre Oppermann #ifdef INET6
331697d8d152SAndre Oppermann 	else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) ||
331797d8d152SAndre Oppermann 		 (!isipv6 && in_localaddr(inp->inp_faddr)))
3318943ae302SAndre Oppermann #else
3319943ae302SAndre Oppermann 	else if (in_localaddr(inp->inp_faddr))
332097d8d152SAndre Oppermann #endif
3321603724d3SBjoern A. Zeeb 		tp->snd_cwnd = mss * V_ss_fltsz_local;
332297d8d152SAndre Oppermann 	else
3323603724d3SBjoern A. Zeeb 		tp->snd_cwnd = mss * V_ss_fltsz;
332491d6cfa6SBjoern A. Zeeb 
332591d6cfa6SBjoern A. Zeeb 	/* Check the interface for TSO capabilities. */
332691d6cfa6SBjoern A. Zeeb 	if (mtuflags & CSUM_TSO)
332791d6cfa6SBjoern A. Zeeb 		tp->t_flags |= TF_TSO;
3328a0292f23SGarrett Wollman }
3329a0292f23SGarrett Wollman 
3330a0292f23SGarrett Wollman /*
3331a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
3332a0292f23SGarrett Wollman  */
3333a0292f23SGarrett Wollman int
3334ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc)
3335a0292f23SGarrett Wollman {
33368b615593SMarko Zec 	INIT_VNET_INET(curvnet);
333797d8d152SAndre Oppermann 	int mss = 0;
333897d8d152SAndre Oppermann 	u_long maxmtu = 0;
333997d8d152SAndre Oppermann 	u_long thcmtu = 0;
334097d8d152SAndre Oppermann 	size_t min_protoh;
3341a0292f23SGarrett Wollman 
334297d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3343a0292f23SGarrett Wollman 
334431b3783cSHajimu UMEMOTO #ifdef INET6
3345dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
3346603724d3SBjoern A. Zeeb 		mss = V_tcp_v6mssdflt;
3347233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(inc, NULL);
334897d8d152SAndre Oppermann 		thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
334997d8d152SAndre Oppermann 		min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
335097d8d152SAndre Oppermann 	} else
335131b3783cSHajimu UMEMOTO #endif
335297d8d152SAndre Oppermann 	{
3353603724d3SBjoern A. Zeeb 		mss = V_tcp_mssdflt;
3354233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(inc, NULL);
335597d8d152SAndre Oppermann 		thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
335697d8d152SAndre Oppermann 		min_protoh = sizeof(struct tcpiphdr);
335797d8d152SAndre Oppermann 	}
335897d8d152SAndre Oppermann 	if (maxmtu && thcmtu)
335997d8d152SAndre Oppermann 		mss = min(maxmtu, thcmtu) - min_protoh;
336097d8d152SAndre Oppermann 	else if (maxmtu || thcmtu)
336197d8d152SAndre Oppermann 		mss = max(maxmtu, thcmtu) - min_protoh;
336297d8d152SAndre Oppermann 
336397d8d152SAndre Oppermann 	return (mss);
3364df8bae1dSRodney W. Grimes }
336546f58482SJonathan Lemon 
336646f58482SJonathan Lemon 
336746f58482SJonathan Lemon /*
3368c068736aSJeffrey Hsu  * On a partial ack arrives, force the retransmission of the
3369c068736aSJeffrey Hsu  * next unacknowledged segment.  Do not clear tp->t_dupacks.
3370c068736aSJeffrey Hsu  * By setting snd_nxt to ti_ack, this forces retransmission timer to
3371c068736aSJeffrey Hsu  * be started again.
337246f58482SJonathan Lemon  */
3373c068736aSJeffrey Hsu static void
3374ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
337546f58482SJonathan Lemon {
337646f58482SJonathan Lemon 	tcp_seq onxt = tp->snd_nxt;
337746f58482SJonathan Lemon 	u_long  ocwnd = tp->snd_cwnd;
337846f58482SJonathan Lemon 
33798501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
33807a3244ccSRobert Watson 
3381b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_REXMT, 0);
338246f58482SJonathan Lemon 	tp->t_rtttime = 0;
338346f58482SJonathan Lemon 	tp->snd_nxt = th->th_ack;
33846b2a5f92SJayanth Vijayaraghavan 	/*
3385c068736aSJeffrey Hsu 	 * Set snd_cwnd to one segment beyond acknowledged offset.
3386c068736aSJeffrey Hsu 	 * (tp->snd_una has not yet been updated when this function is called.)
33876b2a5f92SJayanth Vijayaraghavan 	 */
33886b2a5f92SJayanth Vijayaraghavan 	tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una);
3389a84db8f4SMatthew Dillon 	tp->t_flags |= TF_ACKNOW;
33906b2a5f92SJayanth Vijayaraghavan 	(void) tcp_output(tp);
339146f58482SJonathan Lemon 	tp->snd_cwnd = ocwnd;
339246f58482SJonathan Lemon 	if (SEQ_GT(onxt, tp->snd_nxt))
339346f58482SJonathan Lemon 		tp->snd_nxt = onxt;
339446f58482SJonathan Lemon 	/*
339546f58482SJonathan Lemon 	 * Partial window deflation.  Relies on fact that tp->snd_una
339646f58482SJonathan Lemon 	 * not updated yet.
339746f58482SJonathan Lemon 	 */
3398d7587117SPaul Saab 	if (tp->snd_cwnd > th->th_ack - tp->snd_una)
3399d7587117SPaul Saab 		tp->snd_cwnd -= th->th_ack - tp->snd_una;
3400d7587117SPaul Saab 	else
3401d7587117SPaul Saab 		tp->snd_cwnd = 0;
3402d7587117SPaul Saab 	tp->snd_cwnd += tp->t_maxseg;
340346f58482SJonathan Lemon }
3404