xref: /freebsd/sys/netinet/tcp_input.c (revision aed557087269cd052aa76cc15af4a1fd70cbbf24)
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
30c3aac50fSPeter Wemm  * $FreeBSD$
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
33f9e354dfSJulian Elischer #include "opt_ipfw.h"		/* for ipfw_fwd		*/
341cfd4b53SBruce M Simpson #include "opt_inet.h"
35fb59c426SYoshinobu Inoue #include "opt_inet6.h"
363a2a9f79SYoshinobu Inoue #include "opt_ipsec.h"
37c488362eSRobert Watson #include "opt_mac.h"
380cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
39e46cd3d4SDag-Erling Smørgrav #include "opt_tcp_input.h"
406d90faf3SPaul Saab #include "opt_tcp_sack.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>
54df8bae1dSRodney W. Grimes 
55e79adb8eSGarrett Wollman #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
56e79adb8eSGarrett Wollman 
5712e2e970SAndre Oppermann #include <vm/uma.h>
5812e2e970SAndre Oppermann 
59df8bae1dSRodney W. Grimes #include <net/if.h>
60df8bae1dSRodney W. Grimes #include <net/route.h>
61df8bae1dSRodney W. Grimes 
62df8bae1dSRodney W. Grimes #include <netinet/in.h>
63960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
64df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
65960ed29cSSeigo Tanimura #include <netinet/in_var.h>
66df8bae1dSRodney W. Grimes #include <netinet/ip.h>
678e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h>	/* required for icmp_var.h */
68686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM */
69df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
70ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
71686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
72686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
73686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
74960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h>
75960ed29cSSeigo Tanimura #include <netinet6/nd6.h>
76df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
77df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
78df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
79df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
80df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
81fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h>
82df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
83610ee2f9SDavid Greenman #ifdef TCPDEBUG
84df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
85fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */
86fb59c426SYoshinobu Inoue 
87b9234fafSSam Leffler #ifdef FAST_IPSEC
88b9234fafSSam Leffler #include <netipsec/ipsec.h>
89b9234fafSSam Leffler #include <netipsec/ipsec6.h>
90b9234fafSSam Leffler #endif /*FAST_IPSEC*/
91b9234fafSSam Leffler 
92fb59c426SYoshinobu Inoue #ifdef IPSEC
93fb59c426SYoshinobu Inoue #include <netinet6/ipsec.h>
943a2a9f79SYoshinobu Inoue #include <netinet6/ipsec6.h>
95fb59c426SYoshinobu Inoue #include <netkey/key.h>
96fb59c426SYoshinobu Inoue #endif /*IPSEC*/
97fb59c426SYoshinobu Inoue 
98db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
99db4f9cc7SJonathan Lemon 
100aed55708SRobert Watson #include <security/mac/mac_framework.h>
101aed55708SRobert Watson 
102c068736aSJeffrey Hsu static const int tcprexmtthresh = 3;
1030312fbe9SPoul-Henning Kamp 
1042f96f1f4SGarrett Wollman struct	tcpstat tcpstat;
105c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW,
1063d177f46SBill Fumerola     &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
1070312fbe9SPoul-Henning Kamp 
108d78a37adSPaul Traina static int log_in_vain = 0;
109816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
1103d177f46SBill Fumerola     &log_in_vain, 0, "Log all incoming TCP connections");
111816a3d83SPoul-Henning Kamp 
11216f7f31fSGeoff Rehmet static int blackhole = 0;
11316f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW,
11416f7f31fSGeoff Rehmet 	&blackhole, 0, "Do not send RST when dropping refused connections");
11516f7f31fSGeoff Rehmet 
116f498eeeeSDavid Greenman int tcp_delack_enabled = 1;
11784e33c9eSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW,
1183d177f46SBill Fumerola     &tcp_delack_enabled, 0,
1193d177f46SBill Fumerola     "Delay ACK to try and piggyback it onto a data packet");
120f498eeeeSDavid Greenman 
121f8613305SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN
122f8613305SDag-Erling Smørgrav static int drop_synfin = 0;
123f8613305SDag-Erling Smørgrav SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW,
124f8613305SDag-Erling Smørgrav     &drop_synfin, 0, "Drop TCP packets with SYN+FIN set");
125f8613305SDag-Erling Smørgrav #endif
126f8613305SDag-Erling Smørgrav 
127dba7bc6aSAndre Oppermann static int tcp_do_rfc3042 = 1;
128582a954bSJeffrey Hsu SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW,
129582a954bSJeffrey Hsu     &tcp_do_rfc3042, 0, "Enable RFC 3042 (Limited Transmit)");
130582a954bSJeffrey Hsu 
131dba7bc6aSAndre Oppermann static int tcp_do_rfc3390 = 1;
132da3a8a1aSJeffrey Hsu SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW,
133da3a8a1aSJeffrey Hsu     &tcp_do_rfc3390, 0,
134da3a8a1aSJeffrey Hsu     "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
135da3a8a1aSJeffrey Hsu 
136a69968eeSMike Silbersack static int tcp_insecure_rst = 0;
137a69968eeSMike Silbersack SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_RW,
138a69968eeSMike Silbersack     &tcp_insecure_rst, 0,
139a69968eeSMike Silbersack     "Follow the old (insecure) criteria for accepting RST packets.");
140a69968eeSMike Silbersack 
14112e2e970SAndre Oppermann SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW, 0,
14212e2e970SAndre Oppermann 	    "TCP Segment Reassembly Queue");
14312e2e970SAndre Oppermann 
14412e2e970SAndre Oppermann static int tcp_reass_maxseg = 0;
14512e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxsegments, CTLFLAG_RDTUN,
14612e2e970SAndre Oppermann 	   &tcp_reass_maxseg, 0,
14712e2e970SAndre Oppermann 	   "Global maximum number of TCP Segments in Reassembly Queue");
14812e2e970SAndre Oppermann 
14912e2e970SAndre Oppermann int tcp_reass_qsize = 0;
15012e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, cursegments, CTLFLAG_RD,
15112e2e970SAndre Oppermann 	   &tcp_reass_qsize, 0,
15212e2e970SAndre Oppermann 	   "Global number of TCP Segments currently in Reassembly Queue");
15312e2e970SAndre Oppermann 
15412e2e970SAndre Oppermann static int tcp_reass_maxqlen = 48;
15512e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxqlen, CTLFLAG_RW,
15612e2e970SAndre Oppermann 	   &tcp_reass_maxqlen, 0,
15712e2e970SAndre Oppermann 	   "Maximum number of TCP Segments per individual Reassembly Queue");
15812e2e970SAndre Oppermann 
15912e2e970SAndre Oppermann static int tcp_reass_overflows = 0;
16012e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, overflows, CTLFLAG_RD,
16112e2e970SAndre Oppermann 	   &tcp_reass_overflows, 0,
16212e2e970SAndre Oppermann 	   "Global number of TCP Segment Reassembly Queue Overflows");
16312e2e970SAndre Oppermann 
16415bd2b43SDavid Greenman struct inpcbhead tcb;
165fb59c426SYoshinobu Inoue #define	tcb6	tcb  /* for KAME src sync over BSD*'s */
16615bd2b43SDavid Greenman struct inpcbinfo tcbinfo;
167f76fcf6dSJeffrey Hsu struct mtx	*tcbinfo_mtx;
168df8bae1dSRodney W. Grimes 
1695a53ca16SPaul Saab static void	 tcp_dooptions(struct tcpopt *, u_char *, int, int);
1706d90faf3SPaul Saab 
1714d77a549SAlfred Perlstein static void	 tcp_pulloutofband(struct socket *,
1724d77a549SAlfred Perlstein 		     struct tcphdr *, struct mbuf *, int);
1734d77a549SAlfred Perlstein static int	 tcp_reass(struct tcpcb *, struct tcphdr *, int *,
1744d77a549SAlfred Perlstein 		     struct mbuf *);
1754d77a549SAlfred Perlstein static void	 tcp_xmit_timer(struct tcpcb *, int);
176c068736aSJeffrey Hsu static void	 tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *);
1773cbe7fafSRobert Watson static int	 tcp_timewait(struct inpcb *, struct tcpopt *,
178340c35deSJonathan Lemon 		     struct tcphdr *, struct mbuf *, int);
1790312fbe9SPoul-Henning Kamp 
180fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
181fb59c426SYoshinobu Inoue #ifdef INET6
182fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \
183fb59c426SYoshinobu Inoue do { \
184fb59c426SYoshinobu Inoue 	if ((tp) && (tp)->t_inpcb && \
18597d8d152SAndre Oppermann 	    ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \
18697d8d152SAndre Oppermann 		nd6_nud_hint(NULL, NULL, 0); \
187fb59c426SYoshinobu Inoue } while (0)
188fb59c426SYoshinobu Inoue #else
189fb59c426SYoshinobu Inoue #define ND6_HINT(tp)
190fb59c426SYoshinobu Inoue #endif
191df8bae1dSRodney W. Grimes 
192df8bae1dSRodney W. Grimes /*
193262c1c1aSMatthew Dillon  * Indicate whether this ack should be delayed.  We can delay the ack if
194262c1c1aSMatthew Dillon  *	- there is no delayed ack timer in progress and
195262c1c1aSMatthew Dillon  *	- our last ack wasn't a 0-sized window.  We never want to delay
1963bfd6421SJonathan Lemon  *	  the ack that opens up a 0-sized window and
1973bfd6421SJonathan Lemon  *		- delayed acks are enabled or
1983bfd6421SJonathan Lemon  *		- this is a half-synchronized T/TCP connection.
199d8c85a26SJonathan Lemon  */
200d8c85a26SJonathan Lemon #define DELAY_ACK(tp)							\
201a14c749fSJonathan Lemon 	((!callout_active(tp->tt_delack) &&				\
202a14c749fSJonathan Lemon 	    (tp->t_flags & TF_RXWIN0SENT) == 0) &&			\
2033bfd6421SJonathan Lemon 	    (tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
204d8c85a26SJonathan Lemon 
20512e2e970SAndre Oppermann /* Initialize TCP reassembly queue */
2064f590175SPaul Saab static void
2074f590175SPaul Saab tcp_reass_zone_change(void *tag)
2084f590175SPaul Saab {
2094f590175SPaul Saab 
2104f590175SPaul Saab 	tcp_reass_maxseg = nmbclusters / 16;
2114f590175SPaul Saab 	uma_zone_set_max(tcp_reass_zone, tcp_reass_maxseg);
2124f590175SPaul Saab }
2134f590175SPaul Saab 
21412e2e970SAndre Oppermann uma_zone_t	tcp_reass_zone;
21512e2e970SAndre Oppermann void
21612e2e970SAndre Oppermann tcp_reass_init()
21712e2e970SAndre Oppermann {
21812e2e970SAndre Oppermann 	tcp_reass_maxseg = nmbclusters / 16;
21912e2e970SAndre Oppermann 	TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments",
22012e2e970SAndre Oppermann 	    &tcp_reass_maxseg);
22112e2e970SAndre Oppermann 	tcp_reass_zone = uma_zcreate("tcpreass", sizeof (struct tseg_qent),
22212e2e970SAndre Oppermann 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
22312e2e970SAndre Oppermann 	uma_zone_set_max(tcp_reass_zone, tcp_reass_maxseg);
2244f590175SPaul Saab 	EVENTHANDLER_REGISTER(nmbclusters_change,
2254f590175SPaul Saab 	    tcp_reass_zone_change, NULL, EVENTHANDLER_PRI_ANY);
22612e2e970SAndre Oppermann }
22712e2e970SAndre Oppermann 
2280312fbe9SPoul-Henning Kamp static int
229fb59c426SYoshinobu Inoue tcp_reass(tp, th, tlenp, m)
230df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
231fb59c426SYoshinobu Inoue 	register struct tcphdr *th;
232fb59c426SYoshinobu Inoue 	int *tlenp;
233df8bae1dSRodney W. Grimes 	struct mbuf *m;
234df8bae1dSRodney W. Grimes {
235fb59c426SYoshinobu Inoue 	struct tseg_qent *q;
236fb59c426SYoshinobu Inoue 	struct tseg_qent *p = NULL;
237fb59c426SYoshinobu Inoue 	struct tseg_qent *nq;
23812e2e970SAndre Oppermann 	struct tseg_qent *te = NULL;
239df8bae1dSRodney W. Grimes 	struct socket *so = tp->t_inpcb->inp_socket;
240df8bae1dSRodney W. Grimes 	int flags;
241df8bae1dSRodney W. Grimes 
242de30ea13SRobert Watson 	INP_LOCK_ASSERT(tp->t_inpcb);
243de30ea13SRobert Watson 
244df8bae1dSRodney W. Grimes 	/*
24512e2e970SAndre Oppermann 	 * XXX: tcp_reass() is rather inefficient with its data structures
24612e2e970SAndre Oppermann 	 * and should be rewritten (see NetBSD for optimizations).  While
24712e2e970SAndre Oppermann 	 * doing that it should move to its own file tcp_reass.c.
24812e2e970SAndre Oppermann 	 */
24912e2e970SAndre Oppermann 
25012e2e970SAndre Oppermann 	/*
251de30ea13SRobert Watson 	 * Call with th==NULL after become established to
252df8bae1dSRodney W. Grimes 	 * force pre-ESTABLISHED data up to user socket.
253df8bae1dSRodney W. Grimes 	 */
254de30ea13SRobert Watson 	if (th == NULL)
255df8bae1dSRodney W. Grimes 		goto present;
256df8bae1dSRodney W. Grimes 
25712e2e970SAndre Oppermann 	/*
25812e2e970SAndre Oppermann 	 * Limit the number of segments in the reassembly queue to prevent
25912e2e970SAndre Oppermann 	 * holding on to too many segments (and thus running out of mbufs).
26012e2e970SAndre Oppermann 	 * Make sure to let the missing segment through which caused this
26112e2e970SAndre Oppermann 	 * queue.  Always keep one global queue entry spare to be able to
26212e2e970SAndre Oppermann 	 * process the missing segment.
26312e2e970SAndre Oppermann 	 */
26412e2e970SAndre Oppermann 	if (th->th_seq != tp->rcv_nxt &&
26512e2e970SAndre Oppermann 	    (tcp_reass_qsize + 1 >= tcp_reass_maxseg ||
26612e2e970SAndre Oppermann 	     tp->t_segqlen >= tcp_reass_maxqlen)) {
26712e2e970SAndre Oppermann 		tcp_reass_overflows++;
26812e2e970SAndre Oppermann 		tcpstat.tcps_rcvmemdrop++;
26912e2e970SAndre Oppermann 		m_freem(m);
270e346eeffSPaul Saab 		*tlenp = 0;
27112e2e970SAndre Oppermann 		return (0);
27212e2e970SAndre Oppermann 	}
27312e2e970SAndre Oppermann 
27412e2e970SAndre Oppermann 	/*
27512e2e970SAndre Oppermann 	 * Allocate a new queue entry. If we can't, or hit the zone limit
27612e2e970SAndre Oppermann 	 * just drop the pkt.
27712e2e970SAndre Oppermann 	 */
27812e2e970SAndre Oppermann 	te = uma_zalloc(tcp_reass_zone, M_NOWAIT);
279fb59c426SYoshinobu Inoue 	if (te == NULL) {
280fb59c426SYoshinobu Inoue 		tcpstat.tcps_rcvmemdrop++;
281fb59c426SYoshinobu Inoue 		m_freem(m);
282e346eeffSPaul Saab 		*tlenp = 0;
283fb59c426SYoshinobu Inoue 		return (0);
284fb59c426SYoshinobu Inoue 	}
28512e2e970SAndre Oppermann 	tp->t_segqlen++;
28612e2e970SAndre Oppermann 	tcp_reass_qsize++;
2876effc713SDoug Rabson 
288df8bae1dSRodney W. Grimes 	/*
289df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
290df8bae1dSRodney W. Grimes 	 */
291fb59c426SYoshinobu Inoue 	LIST_FOREACH(q, &tp->t_segq, tqe_q) {
292fb59c426SYoshinobu Inoue 		if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
293df8bae1dSRodney W. Grimes 			break;
294fb59c426SYoshinobu Inoue 		p = q;
295fb59c426SYoshinobu Inoue 	}
296df8bae1dSRodney W. Grimes 
297df8bae1dSRodney W. Grimes 	/*
298df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
299df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
300df8bae1dSRodney W. Grimes 	 * segment.  If it provides all of our data, drop us.
301df8bae1dSRodney W. Grimes 	 */
3026effc713SDoug Rabson 	if (p != NULL) {
303df8bae1dSRodney W. Grimes 		register int i;
304df8bae1dSRodney W. Grimes 		/* conversion to int (in i) handles seq wraparound */
305fb59c426SYoshinobu Inoue 		i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
306df8bae1dSRodney W. Grimes 		if (i > 0) {
307fb59c426SYoshinobu Inoue 			if (i >= *tlenp) {
308df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvduppack++;
309fb59c426SYoshinobu Inoue 				tcpstat.tcps_rcvdupbyte += *tlenp;
310df8bae1dSRodney W. Grimes 				m_freem(m);
31112e2e970SAndre Oppermann 				uma_zfree(tcp_reass_zone, te);
31212e2e970SAndre Oppermann 				tp->t_segqlen--;
31312e2e970SAndre Oppermann 				tcp_reass_qsize--;
314a0292f23SGarrett Wollman 				/*
315a0292f23SGarrett Wollman 				 * Try to present any queued data
316a0292f23SGarrett Wollman 				 * at the left window edge to the user.
317a0292f23SGarrett Wollman 				 * This is needed after the 3-WHS
318a0292f23SGarrett Wollman 				 * completes.
319a0292f23SGarrett Wollman 				 */
320a0292f23SGarrett Wollman 				goto present;	/* ??? */
321df8bae1dSRodney W. Grimes 			}
322df8bae1dSRodney W. Grimes 			m_adj(m, i);
323fb59c426SYoshinobu Inoue 			*tlenp -= i;
324fb59c426SYoshinobu Inoue 			th->th_seq += i;
325df8bae1dSRodney W. Grimes 		}
326df8bae1dSRodney W. Grimes 	}
327df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvoopack++;
328fb59c426SYoshinobu Inoue 	tcpstat.tcps_rcvoobyte += *tlenp;
329df8bae1dSRodney W. Grimes 
330df8bae1dSRodney W. Grimes 	/*
331df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
332df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
333df8bae1dSRodney W. Grimes 	 */
3346effc713SDoug Rabson 	while (q) {
335fb59c426SYoshinobu Inoue 		register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
336df8bae1dSRodney W. Grimes 		if (i <= 0)
337df8bae1dSRodney W. Grimes 			break;
338fb59c426SYoshinobu Inoue 		if (i < q->tqe_len) {
339fb59c426SYoshinobu Inoue 			q->tqe_th->th_seq += i;
340fb59c426SYoshinobu Inoue 			q->tqe_len -= i;
341fb59c426SYoshinobu Inoue 			m_adj(q->tqe_m, i);
342df8bae1dSRodney W. Grimes 			break;
343df8bae1dSRodney W. Grimes 		}
3446effc713SDoug Rabson 
345fb59c426SYoshinobu Inoue 		nq = LIST_NEXT(q, tqe_q);
346fb59c426SYoshinobu Inoue 		LIST_REMOVE(q, tqe_q);
347fb59c426SYoshinobu Inoue 		m_freem(q->tqe_m);
34812e2e970SAndre Oppermann 		uma_zfree(tcp_reass_zone, q);
34912e2e970SAndre Oppermann 		tp->t_segqlen--;
35012e2e970SAndre Oppermann 		tcp_reass_qsize--;
3516effc713SDoug Rabson 		q = nq;
352df8bae1dSRodney W. Grimes 	}
353df8bae1dSRodney W. Grimes 
354fb59c426SYoshinobu Inoue 	/* Insert the new segment queue entry into place. */
355fb59c426SYoshinobu Inoue 	te->tqe_m = m;
356fb59c426SYoshinobu Inoue 	te->tqe_th = th;
357fb59c426SYoshinobu Inoue 	te->tqe_len = *tlenp;
358fb59c426SYoshinobu Inoue 
3596effc713SDoug Rabson 	if (p == NULL) {
360fb59c426SYoshinobu Inoue 		LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
3616effc713SDoug Rabson 	} else {
362fb59c426SYoshinobu Inoue 		LIST_INSERT_AFTER(p, te, tqe_q);
3636effc713SDoug Rabson 	}
364df8bae1dSRodney W. Grimes 
365df8bae1dSRodney W. Grimes present:
366df8bae1dSRodney W. Grimes 	/*
367df8bae1dSRodney W. Grimes 	 * Present data to user, advancing rcv_nxt through
368df8bae1dSRodney W. Grimes 	 * completed sequence space.
369df8bae1dSRodney W. Grimes 	 */
370a0292f23SGarrett Wollman 	if (!TCPS_HAVEESTABLISHED(tp->t_state))
371df8bae1dSRodney W. Grimes 		return (0);
372fb59c426SYoshinobu Inoue 	q = LIST_FIRST(&tp->t_segq);
373fb59c426SYoshinobu Inoue 	if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
374df8bae1dSRodney W. Grimes 		return (0);
3751e4d7da7SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
376df8bae1dSRodney W. Grimes 	do {
377fb59c426SYoshinobu Inoue 		tp->rcv_nxt += q->tqe_len;
378fb59c426SYoshinobu Inoue 		flags = q->tqe_th->th_flags & TH_FIN;
379fb59c426SYoshinobu Inoue 		nq = LIST_NEXT(q, tqe_q);
380fb59c426SYoshinobu Inoue 		LIST_REMOVE(q, tqe_q);
381c0b99ffaSRobert Watson 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
382fb59c426SYoshinobu Inoue 			m_freem(q->tqe_m);
3834cc20ab1SSeigo Tanimura 		else
3841e4d7da7SRobert Watson 			sbappendstream_locked(&so->so_rcv, q->tqe_m);
38512e2e970SAndre Oppermann 		uma_zfree(tcp_reass_zone, q);
38612e2e970SAndre Oppermann 		tp->t_segqlen--;
38712e2e970SAndre Oppermann 		tcp_reass_qsize--;
3886effc713SDoug Rabson 		q = nq;
389fb59c426SYoshinobu Inoue 	} while (q && q->tqe_th->th_seq == tp->rcv_nxt);
390fb59c426SYoshinobu Inoue 	ND6_HINT(tp);
3911e4d7da7SRobert Watson 	sorwakeup_locked(so);
392df8bae1dSRodney W. Grimes 	return (flags);
393df8bae1dSRodney W. Grimes }
394df8bae1dSRodney W. Grimes 
395df8bae1dSRodney W. Grimes /*
396df8bae1dSRodney W. Grimes  * TCP input routine, follows pages 65-76 of the
397df8bae1dSRodney W. Grimes  * protocol specification dated September, 1981 very closely.
398df8bae1dSRodney W. Grimes  */
399fb59c426SYoshinobu Inoue #ifdef INET6
400fb59c426SYoshinobu Inoue int
401fb59c426SYoshinobu Inoue tcp6_input(mp, offp, proto)
402fb59c426SYoshinobu Inoue 	struct mbuf **mp;
403fb59c426SYoshinobu Inoue 	int *offp, proto;
404fb59c426SYoshinobu Inoue {
405fb59c426SYoshinobu Inoue 	register struct mbuf *m = *mp;
40633841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
407fb59c426SYoshinobu Inoue 
408fb59c426SYoshinobu Inoue 	IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
409fb59c426SYoshinobu Inoue 
410fb59c426SYoshinobu Inoue 	/*
411fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
412fb59c426SYoshinobu Inoue 	 * better place to put this in?
413fb59c426SYoshinobu Inoue 	 */
41433841545SHajimu UMEMOTO 	ia6 = ip6_getdstifaddr(m);
41533841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
416fb59c426SYoshinobu Inoue 		struct ip6_hdr *ip6;
417fb59c426SYoshinobu Inoue 
418fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
419fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
420fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
421fb59c426SYoshinobu Inoue 		return IPPROTO_DONE;
422fb59c426SYoshinobu Inoue 	}
423fb59c426SYoshinobu Inoue 
424f0ffb944SJulian Elischer 	tcp_input(m, *offp);
425fb59c426SYoshinobu Inoue 	return IPPROTO_DONE;
426fb59c426SYoshinobu Inoue }
427fb59c426SYoshinobu Inoue #endif
428fb59c426SYoshinobu Inoue 
429df8bae1dSRodney W. Grimes void
430f0ffb944SJulian Elischer tcp_input(m, off0)
431df8bae1dSRodney W. Grimes 	register struct mbuf *m;
432f0ffb944SJulian Elischer 	int off0;
433df8bae1dSRodney W. Grimes {
434fb59c426SYoshinobu Inoue 	register struct tcphdr *th;
435fb59c426SYoshinobu Inoue 	register struct ip *ip = NULL;
436fb59c426SYoshinobu Inoue 	register struct ipovly *ipov;
437f76fcf6dSJeffrey Hsu 	register struct inpcb *inp = NULL;
438e79adb8eSGarrett Wollman 	u_char *optp = NULL;
43926f9a767SRodney W. Grimes 	int optlen = 0;
440a0194ef1SBruce M Simpson 	int len, tlen, off;
441fb59c426SYoshinobu Inoue 	int drop_hdrlen;
442df8bae1dSRodney W. Grimes 	register struct tcpcb *tp = 0;
443fb59c426SYoshinobu Inoue 	register int thflags;
44426f9a767SRodney W. Grimes 	struct socket *so = 0;
445df8bae1dSRodney W. Grimes 	int todrop, acked, ourfinisacked, needoutput = 0;
446a0292f23SGarrett Wollman 	u_long tiwin;
447a0292f23SGarrett Wollman 	struct tcpopt to;		/* options in this segment */
448f76fcf6dSJeffrey Hsu 	int headlocked = 0;
4499b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
4509b932e9eSAndre Oppermann 	struct m_tag *fwd_tag;
4519b932e9eSAndre Oppermann #endif
452c068736aSJeffrey Hsu 	int rstreason; /* For badport_bandlim accounting purposes */
453c068736aSJeffrey Hsu 
454c068736aSJeffrey Hsu 	struct ip6_hdr *ip6 = NULL;
455c068736aSJeffrey Hsu #ifdef INET6
456c068736aSJeffrey Hsu 	int isipv6;
457c068736aSJeffrey Hsu #else
458c068736aSJeffrey Hsu 	const int isipv6 = 0;
459c068736aSJeffrey Hsu #endif
460f76fcf6dSJeffrey Hsu 
461610ee2f9SDavid Greenman #ifdef TCPDEBUG
462c068736aSJeffrey Hsu 	/*
463c068736aSJeffrey Hsu 	 * The size of tcp_saveipgen must be the size of the max ip header,
464c068736aSJeffrey Hsu 	 * now IPv6.
465c068736aSJeffrey Hsu 	 */
466410bb1bfSLuigi Rizzo 	u_char tcp_saveipgen[40];
467410bb1bfSLuigi Rizzo 	struct tcphdr tcp_savetcp;
468610ee2f9SDavid Greenman 	short ostate = 0;
469610ee2f9SDavid Greenman #endif
470c068736aSJeffrey Hsu 
471fb59c426SYoshinobu Inoue #ifdef INET6
472fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
473fb59c426SYoshinobu Inoue #endif
474a0292f23SGarrett Wollman 	bzero((char *)&to, sizeof(to));
475a0292f23SGarrett Wollman 
476df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvtotal++;
477fb59c426SYoshinobu Inoue 
478fb59c426SYoshinobu Inoue 	if (isipv6) {
47904d3a452SHajimu UMEMOTO #ifdef INET6
480fb59c426SYoshinobu Inoue 		/* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
481fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
482fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
483fb59c426SYoshinobu Inoue 		if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) {
484fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbadsum++;
485fb59c426SYoshinobu Inoue 			goto drop;
486fb59c426SYoshinobu Inoue 		}
487fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
48833841545SHajimu UMEMOTO 
48933841545SHajimu UMEMOTO 		/*
49033841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
49133841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
49233841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
49333841545SHajimu UMEMOTO 		 *
49433841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
49533841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
49633841545SHajimu UMEMOTO 		 */
49733841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
49833841545SHajimu UMEMOTO 			/* XXX stat */
49933841545SHajimu UMEMOTO 			goto drop;
50033841545SHajimu UMEMOTO 		}
50104d3a452SHajimu UMEMOTO #else
50204d3a452SHajimu UMEMOTO 		th = NULL;		/* XXX: avoid compiler warning */
50304d3a452SHajimu UMEMOTO #endif
504c068736aSJeffrey Hsu 	} else {
505df8bae1dSRodney W. Grimes 		/*
506df8bae1dSRodney W. Grimes 		 * Get IP and TCP header together in first mbuf.
507df8bae1dSRodney W. Grimes 		 * Note: IP leaves IP header in first mbuf.
508df8bae1dSRodney W. Grimes 		 */
509fb59c426SYoshinobu Inoue 		if (off0 > sizeof (struct ip)) {
510df8bae1dSRodney W. Grimes 			ip_stripoptions(m, (struct mbuf *)0);
511fb59c426SYoshinobu Inoue 			off0 = sizeof(struct ip);
512fb59c426SYoshinobu Inoue 		}
513df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof (struct tcpiphdr)) {
514df8bae1dSRodney W. Grimes 			if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
515df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvshort++;
516df8bae1dSRodney W. Grimes 				return;
517df8bae1dSRodney W. Grimes 			}
518df8bae1dSRodney W. Grimes 		}
519fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
520fb59c426SYoshinobu Inoue 		ipov = (struct ipovly *)ip;
521db4f9cc7SJonathan Lemon 		th = (struct tcphdr *)((caddr_t)ip + off0);
522db4f9cc7SJonathan Lemon 		tlen = ip->ip_len;
523df8bae1dSRodney W. Grimes 
524db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
525db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
526db4f9cc7SJonathan Lemon 				th->th_sum = m->m_pkthdr.csum_data;
527db4f9cc7SJonathan Lemon 			else
528db4f9cc7SJonathan Lemon 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
529c068736aSJeffrey Hsu 						ip->ip_dst.s_addr,
530c068736aSJeffrey Hsu 						htonl(m->m_pkthdr.csum_data +
531c068736aSJeffrey Hsu 							ip->ip_len +
532c068736aSJeffrey Hsu 							IPPROTO_TCP));
533db4f9cc7SJonathan Lemon 			th->th_sum ^= 0xffff;
5343c653157SHartmut Brandt #ifdef TCPDEBUG
5353c653157SHartmut Brandt 			ipov->ih_len = (u_short)tlen;
5363c653157SHartmut Brandt 			ipov->ih_len = htons(ipov->ih_len);
5373c653157SHartmut Brandt #endif
538db4f9cc7SJonathan Lemon 		} else {
539df8bae1dSRodney W. Grimes 			/*
540df8bae1dSRodney W. Grimes 			 * Checksum extended TCP header and data.
541df8bae1dSRodney W. Grimes 			 */
542df8bae1dSRodney W. Grimes 			len = sizeof (struct ip) + tlen;
543fb59c426SYoshinobu Inoue 			bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
544fb59c426SYoshinobu Inoue 			ipov->ih_len = (u_short)tlen;
545fd8e4ebcSMike Barcroft 			ipov->ih_len = htons(ipov->ih_len);
546fb59c426SYoshinobu Inoue 			th->th_sum = in_cksum(m, len);
547db4f9cc7SJonathan Lemon 		}
548fb59c426SYoshinobu Inoue 		if (th->th_sum) {
549df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbadsum++;
550df8bae1dSRodney W. Grimes 			goto drop;
551df8bae1dSRodney W. Grimes 		}
552fb59c426SYoshinobu Inoue 		/* Re-initialization for later version check */
553fb59c426SYoshinobu Inoue 		ip->ip_v = IPVERSION;
554fb59c426SYoshinobu Inoue 	}
555df8bae1dSRodney W. Grimes 
556df8bae1dSRodney W. Grimes 	/*
557df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
558df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
559df8bae1dSRodney W. Grimes 	 */
560fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
561df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
562df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvbadoff++;
563df8bae1dSRodney W. Grimes 		goto drop;
564df8bae1dSRodney W. Grimes 	}
565fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
566df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
567fb59c426SYoshinobu Inoue 		if (isipv6) {
56804d3a452SHajimu UMEMOTO #ifdef INET6
569fb59c426SYoshinobu Inoue 			IP6_EXTHDR_CHECK(m, off0, off, );
570fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
571fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
57204d3a452SHajimu UMEMOTO #endif
573c068736aSJeffrey Hsu 		} else {
574df8bae1dSRodney W. Grimes 			if (m->m_len < sizeof(struct ip) + off) {
575c068736aSJeffrey Hsu 				if ((m = m_pullup(m, sizeof (struct ip) + off))
576c068736aSJeffrey Hsu 						== 0) {
577df8bae1dSRodney W. Grimes 					tcpstat.tcps_rcvshort++;
578df8bae1dSRodney W. Grimes 					return;
579df8bae1dSRodney W. Grimes 				}
580fb59c426SYoshinobu Inoue 				ip = mtod(m, struct ip *);
581fb59c426SYoshinobu Inoue 				ipov = (struct ipovly *)ip;
582fb59c426SYoshinobu Inoue 				th = (struct tcphdr *)((caddr_t)ip + off0);
583fb59c426SYoshinobu Inoue 			}
584df8bae1dSRodney W. Grimes 		}
585df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
586fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
587df8bae1dSRodney W. Grimes 	}
588fb59c426SYoshinobu Inoue 	thflags = th->th_flags;
589df8bae1dSRodney W. Grimes 
590e46cd3d4SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN
591e46cd3d4SDag-Erling Smørgrav 	/*
592e46cd3d4SDag-Erling Smørgrav 	 * If the drop_synfin option is enabled, drop all packets with
593e46cd3d4SDag-Erling Smørgrav 	 * both the SYN and FIN bits set. This prevents e.g. nmap from
594e46cd3d4SDag-Erling Smørgrav 	 * identifying the TCP/IP stack.
595e46cd3d4SDag-Erling Smørgrav 	 *
596a589a70eSGarrett Wollman 	 * This is a violation of the TCP specification.
597e46cd3d4SDag-Erling Smørgrav 	 */
598fb59c426SYoshinobu Inoue 	if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN))
599e46cd3d4SDag-Erling Smørgrav 		goto drop;
600e46cd3d4SDag-Erling Smørgrav #endif
601e46cd3d4SDag-Erling Smørgrav 
602df8bae1dSRodney W. Grimes 	/*
603df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
604df8bae1dSRodney W. Grimes 	 */
605fd8e4ebcSMike Barcroft 	th->th_seq = ntohl(th->th_seq);
606fd8e4ebcSMike Barcroft 	th->th_ack = ntohl(th->th_ack);
607fd8e4ebcSMike Barcroft 	th->th_win = ntohs(th->th_win);
608fd8e4ebcSMike Barcroft 	th->th_urp = ntohs(th->th_urp);
609df8bae1dSRodney W. Grimes 
610df8bae1dSRodney W. Grimes 	/*
6113bfd6421SJonathan Lemon 	 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options,
612fb59c426SYoshinobu Inoue 	 * until after ip6_savecontrol() is called and before other functions
613fb59c426SYoshinobu Inoue 	 * which don't want those proto headers.
614fb59c426SYoshinobu Inoue 	 * Because ip6_savecontrol() is going to parse the mbuf to
615fb59c426SYoshinobu Inoue 	 * search for data to be passed up to user-land, it wants mbuf
616fb59c426SYoshinobu Inoue 	 * parameters to be unchanged.
61788ff5695SSUZUKI Shinsuke 	 * XXX: the call of ip6_savecontrol() has been obsoleted based on
61888ff5695SSUZUKI Shinsuke 	 * latest version of the advanced API (20020110).
619755c1f07SAndras Olah 	 */
620fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
621755c1f07SAndras Olah 
622755c1f07SAndras Olah 	/*
623df8bae1dSRodney W. Grimes 	 * Locate pcb for segment.
624df8bae1dSRodney W. Grimes 	 */
625f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&tcbinfo);
626f76fcf6dSJeffrey Hsu 	headlocked = 1;
627df8bae1dSRodney W. Grimes findpcb:
628de30ea13SRobert Watson 	KASSERT(headlocked, ("tcp_input: findpcb: head not locked"));
6299b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
6309b932e9eSAndre Oppermann 	/* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */
6319b932e9eSAndre Oppermann 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
6329b932e9eSAndre Oppermann 
6339b932e9eSAndre Oppermann 	if (fwd_tag != NULL && isipv6 == 0) {	/* IPv6 support is not yet */
6349b932e9eSAndre Oppermann 		struct sockaddr_in *next_hop;
6359b932e9eSAndre Oppermann 
6369b932e9eSAndre Oppermann 		next_hop = (struct sockaddr_in *)(fwd_tag+1);
637f9e354dfSJulian Elischer 		/*
6382b25acc1SLuigi Rizzo 		 * Transparently forwarded. Pretend to be the destination.
639f9e354dfSJulian Elischer 		 * already got one like this?
640f9e354dfSJulian Elischer 		 */
6419b932e9eSAndre Oppermann 		inp = in_pcblookup_hash(&tcbinfo,
6429b932e9eSAndre Oppermann 					ip->ip_src, th->th_sport,
643c068736aSJeffrey Hsu 					ip->ip_dst, th->th_dport,
644c068736aSJeffrey Hsu 					0, m->m_pkthdr.rcvif);
645f9e354dfSJulian Elischer 		if (!inp) {
6469b932e9eSAndre Oppermann 			/* It's new.  Try to find the ambushing socket. */
647f9e354dfSJulian Elischer 			inp = in_pcblookup_hash(&tcbinfo,
648fb59c426SYoshinobu Inoue 						ip->ip_src, th->th_sport,
6492b25acc1SLuigi Rizzo 						next_hop->sin_addr,
650c068736aSJeffrey Hsu 						next_hop->sin_port ?
651c068736aSJeffrey Hsu 						    ntohs(next_hop->sin_port) :
652c068736aSJeffrey Hsu 						    th->th_dport,
653421d8aa6SBjoern A. Zeeb 						INPLOOKUP_WILDCARD,
654421d8aa6SBjoern A. Zeeb 						m->m_pkthdr.rcvif);
655f9e354dfSJulian Elischer 		}
6569b932e9eSAndre Oppermann 		/* Remove the tag from the packet.  We don't need it anymore. */
6579b932e9eSAndre Oppermann 		m_tag_delete(m, fwd_tag);
658c068736aSJeffrey Hsu 	} else {
6599b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */
66004d3a452SHajimu UMEMOTO 		if (isipv6) {
66104d3a452SHajimu UMEMOTO #ifdef INET6
662c068736aSJeffrey Hsu 			inp = in6_pcblookup_hash(&tcbinfo,
663c068736aSJeffrey Hsu 						 &ip6->ip6_src, th->th_sport,
664c068736aSJeffrey Hsu 						 &ip6->ip6_dst, th->th_dport,
665421d8aa6SBjoern A. Zeeb 						 INPLOOKUP_WILDCARD,
666421d8aa6SBjoern A. Zeeb 						 m->m_pkthdr.rcvif);
66704d3a452SHajimu UMEMOTO #endif
66804d3a452SHajimu UMEMOTO 		} else
669c068736aSJeffrey Hsu 			inp = in_pcblookup_hash(&tcbinfo,
670c068736aSJeffrey Hsu 						ip->ip_src, th->th_sport,
671c068736aSJeffrey Hsu 						ip->ip_dst, th->th_dport,
672421d8aa6SBjoern A. Zeeb 						INPLOOKUP_WILDCARD,
673421d8aa6SBjoern A. Zeeb 						m->m_pkthdr.rcvif);
6749b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
675fb59c426SYoshinobu Inoue 	}
6769b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */
677f9e354dfSJulian Elischer 
678da0f4099SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC)
679d420fcdaSBruce M Simpson #ifdef INET6
680da0f4099SHajimu UMEMOTO 	if (isipv6) {
681da0f4099SHajimu UMEMOTO 		if (inp != NULL && ipsec6_in_reject(m, inp)) {
682fb59c426SYoshinobu Inoue #ifdef IPSEC
683fb59c426SYoshinobu Inoue 			ipsec6stat.in_polvio++;
684d420fcdaSBruce M Simpson #endif
685fb59c426SYoshinobu Inoue 			goto drop;
686fb59c426SYoshinobu Inoue 		}
687d420fcdaSBruce M Simpson 	} else
688d420fcdaSBruce M Simpson #endif /* INET6 */
689d420fcdaSBruce M Simpson 	if (inp != NULL && ipsec4_in_reject(m, inp)) {
690da0f4099SHajimu UMEMOTO #ifdef IPSEC
691fb59c426SYoshinobu Inoue 		ipsecstat.in_polvio++;
692d420fcdaSBruce M Simpson #endif
693fb59c426SYoshinobu Inoue 		goto drop;
694fb59c426SYoshinobu Inoue 	}
695da0f4099SHajimu UMEMOTO #endif /*IPSEC || FAST_IPSEC*/
696df8bae1dSRodney W. Grimes 
697df8bae1dSRodney W. Grimes 	/*
698df8bae1dSRodney W. Grimes 	 * If the state is CLOSED (i.e., TCB does not exist) then
699df8bae1dSRodney W. Grimes 	 * all data in the incoming segment is discarded.
700df8bae1dSRodney W. Grimes 	 * If the TCB exists but is in CLOSED state, it is embryonic,
701df8bae1dSRodney W. Grimes 	 * but should either do a listen or a connect soon.
702df8bae1dSRodney W. Grimes 	 */
703816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
7042e4e1b4cSGeoff Rehmet 		if (log_in_vain) {
705fb59c426SYoshinobu Inoue #ifdef INET6
706ded7008aSJuli Mallett 			char dbuf[INET6_ADDRSTRLEN+2], sbuf[INET6_ADDRSTRLEN+2];
707c068736aSJeffrey Hsu #else
708fb59c426SYoshinobu Inoue 			char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"];
709c068736aSJeffrey Hsu #endif
71075cfc95fSAndrey A. Chernov 
711fb59c426SYoshinobu Inoue 			if (isipv6) {
71204d3a452SHajimu UMEMOTO #ifdef INET6
713ded7008aSJuli Mallett 				strcpy(dbuf, "[");
714ded7008aSJuli Mallett 				strcpy(sbuf, "[");
715ded7008aSJuli Mallett 				strcat(dbuf, ip6_sprintf(&ip6->ip6_dst));
716ded7008aSJuli Mallett 				strcat(sbuf, ip6_sprintf(&ip6->ip6_src));
717ded7008aSJuli Mallett 				strcat(dbuf, "]");
718ded7008aSJuli Mallett 				strcat(sbuf, "]");
71904d3a452SHajimu UMEMOTO #endif
720c068736aSJeffrey Hsu 			} else {
721fb59c426SYoshinobu Inoue 				strcpy(dbuf, inet_ntoa(ip->ip_dst));
722fb59c426SYoshinobu Inoue 				strcpy(sbuf, inet_ntoa(ip->ip_src));
723fb59c426SYoshinobu Inoue 			}
7242e4e1b4cSGeoff Rehmet 			switch (log_in_vain) {
7252e4e1b4cSGeoff Rehmet 			case 1:
72639eb27a4SCrist J. Clark 				if ((thflags & TH_SYN) == 0)
7272e4e1b4cSGeoff Rehmet 					break;
728e4d2978dSPoul-Henning Kamp 				/* FALLTHROUGH */
7292e4e1b4cSGeoff Rehmet 			case 2:
7302e4e1b4cSGeoff Rehmet 				log(LOG_INFO,
731c068736aSJeffrey Hsu 				    "Connection attempt to TCP %s:%d "
73239eb27a4SCrist J. Clark 				    "from %s:%d flags:0x%02x\n",
733fb59c426SYoshinobu Inoue 				    dbuf, ntohs(th->th_dport), sbuf,
734fb59c426SYoshinobu Inoue 				    ntohs(th->th_sport), thflags);
7352e4e1b4cSGeoff Rehmet 				break;
7362e4e1b4cSGeoff Rehmet 			default:
7372e4e1b4cSGeoff Rehmet 				break;
7382e4e1b4cSGeoff Rehmet 			}
73975cfc95fSAndrey A. Chernov 		}
7402e4e1b4cSGeoff Rehmet 		if (blackhole) {
7412e4e1b4cSGeoff Rehmet 			switch (blackhole) {
742828b7f40SGeoff Rehmet 			case 1:
743fb59c426SYoshinobu Inoue 				if (thflags & TH_SYN)
744828b7f40SGeoff Rehmet 					goto drop;
745828b7f40SGeoff Rehmet 				break;
746828b7f40SGeoff Rehmet 			case 2:
747828b7f40SGeoff Rehmet 				goto drop;
748828b7f40SGeoff Rehmet 			default:
749828b7f40SGeoff Rehmet 				goto drop;
7502e4e1b4cSGeoff Rehmet 			}
751828b7f40SGeoff Rehmet 		}
752a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
753a57815efSBosko Milekic 		goto dropwithreset;
754816a3d83SPoul-Henning Kamp 	}
755f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
756936cd18dSAndre Oppermann 
757936cd18dSAndre Oppermann 	/* Check the minimum TTL for socket. */
75834f83c52SGeorge V. Neville-Neil 	if (inp->inp_ip_minttl != 0) {
75934f83c52SGeorge V. Neville-Neil #ifdef INET6
76034f83c52SGeorge V. Neville-Neil 		if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim)
761936cd18dSAndre Oppermann 			goto drop;
76202707462SAndre Oppermann 		else
76334f83c52SGeorge V. Neville-Neil #endif
76434f83c52SGeorge V. Neville-Neil 		if (inp->inp_ip_minttl > ip->ip_ttl)
76534f83c52SGeorge V. Neville-Neil 			goto drop;
76634f83c52SGeorge V. Neville-Neil 	}
767936cd18dSAndre Oppermann 
768340c35deSJonathan Lemon 	if (inp->inp_vflag & INP_TIMEWAIT) {
769340c35deSJonathan Lemon 		/*
770340c35deSJonathan Lemon 		 * The only option of relevance is TOF_CC, and only if
771340c35deSJonathan Lemon 		 * present in a SYN segment.  See tcp_timewait().
772340c35deSJonathan Lemon 		 */
773340c35deSJonathan Lemon 		if (thflags & TH_SYN)
774f72167f4SAndre Oppermann 			tcp_dooptions(&to, optp, optlen, TO_SYN);
7753cbe7fafSRobert Watson 		if (tcp_timewait(inp, &to, th, m, tlen))
776340c35deSJonathan Lemon 			goto findpcb;
777340c35deSJonathan Lemon 		/*
778340c35deSJonathan Lemon 		 * tcp_timewait unlocks inp.
779340c35deSJonathan Lemon 		 */
780340c35deSJonathan Lemon 		INP_INFO_WUNLOCK(&tcbinfo);
781340c35deSJonathan Lemon 		return;
782340c35deSJonathan Lemon 	}
783df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
78409f81a46SBosko Milekic 	if (tp == 0) {
785f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
786a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
787a57815efSBosko Milekic 		goto dropwithreset;
78809f81a46SBosko Milekic 	}
789df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_CLOSED)
790df8bae1dSRodney W. Grimes 		goto drop;
791df8bae1dSRodney W. Grimes 
792c488362eSRobert Watson #ifdef MAC
7931f82efb3SRobert Watson 	INP_LOCK_ASSERT(inp);
794a557af22SRobert Watson 	if (mac_check_inpcb_deliver(inp, m))
795c488362eSRobert Watson 		goto drop;
796c488362eSRobert Watson #endif
797a557af22SRobert Watson 	so = inp->inp_socket;
7981c53f806SRobert Watson 	KASSERT(so != NULL, ("tcp_input: so == NULL"));
799610ee2f9SDavid Greenman #ifdef TCPDEBUG
800df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG) {
801df8bae1dSRodney W. Grimes 		ostate = tp->t_state;
802fb59c426SYoshinobu Inoue 		if (isipv6)
803540e8b7eSJeffrey Hsu 			bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6));
804fb59c426SYoshinobu Inoue 		else
805540e8b7eSJeffrey Hsu 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
806fb59c426SYoshinobu Inoue 		tcp_savetcp = *th;
807df8bae1dSRodney W. Grimes 	}
808610ee2f9SDavid Greenman #endif
809540e8b7eSJeffrey Hsu 	if (so->so_options & SO_ACCEPTCONN) {
810540e8b7eSJeffrey Hsu 		struct in_conninfo inc;
811540e8b7eSJeffrey Hsu 
8128bfb1918SAndre Oppermann 		bzero(&inc, sizeof(inc));
813fb59c426SYoshinobu Inoue #ifdef INET6
814be2ac88cSJonathan Lemon 		inc.inc_isipv6 = isipv6;
815c068736aSJeffrey Hsu #endif
816be2ac88cSJonathan Lemon 		if (isipv6) {
817be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
818be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
819c068736aSJeffrey Hsu 		} else {
820be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
821be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
822be2ac88cSJonathan Lemon 		}
823be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
824be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
825be2ac88cSJonathan Lemon 
826fb59c426SYoshinobu Inoue 	        /*
827be2ac88cSJonathan Lemon 	         * If the state is LISTEN then ignore segment if it contains
828be2ac88cSJonathan Lemon 		 * a RST.  If the segment contains an ACK then it is bad and
829be2ac88cSJonathan Lemon 		 * send a RST.  If it does not contain a SYN then it is not
830be2ac88cSJonathan Lemon 		 * interesting; drop it.
831be2ac88cSJonathan Lemon 		 *
832be2ac88cSJonathan Lemon 		 * If the state is SYN_RECEIVED (syncache) and seg contains
833be2ac88cSJonathan Lemon 		 * an ACK, but not for our SYN/ACK, send a RST.  If the seg
834be2ac88cSJonathan Lemon 		 * contains a RST, check the sequence number to see if it
835be2ac88cSJonathan Lemon 		 * is a valid reset segment.
836fb59c426SYoshinobu Inoue 		 */
837fb59c426SYoshinobu Inoue 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
838be2ac88cSJonathan Lemon 			if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
839bf6d304aSAndre Oppermann 				/*
840bf6d304aSAndre Oppermann 				 * Parse the TCP options here because
841bf6d304aSAndre Oppermann 				 * syncookies need access to the reflected
842bf6d304aSAndre Oppermann 				 * timestamp.
843bf6d304aSAndre Oppermann 				 */
844bf6d304aSAndre Oppermann 				tcp_dooptions(&to, optp, optlen, 0);
845bf6d304aSAndre Oppermann 				if (!syncache_expand(&inc, &to, th, &so, m)) {
8464195b4afSPaul Traina 					/*
847be2ac88cSJonathan Lemon 					 * No syncache entry, or ACK was not
848be2ac88cSJonathan Lemon 					 * for our SYN/ACK.  Send a RST.
8494195b4afSPaul Traina 					 */
850be2ac88cSJonathan Lemon 					tcpstat.tcps_badsyn++;
851be2ac88cSJonathan Lemon 					rstreason = BANDLIM_RST_OPENPORT;
852be2ac88cSJonathan Lemon 					goto dropwithreset;
853be2ac88cSJonathan Lemon 				}
854f76fcf6dSJeffrey Hsu 				if (so == NULL) {
855be2ac88cSJonathan Lemon 					/*
856be2ac88cSJonathan Lemon 					 * Could not complete 3-way handshake,
857be2ac88cSJonathan Lemon 					 * connection is being closed down, and
858464fcfbcSAndre Oppermann 					 * syncache has free'd mbuf.
859be2ac88cSJonathan Lemon 					 */
860f76fcf6dSJeffrey Hsu 					INP_UNLOCK(inp);
861f76fcf6dSJeffrey Hsu 					INP_INFO_WUNLOCK(&tcbinfo);
862be2ac88cSJonathan Lemon 					return;
863f76fcf6dSJeffrey Hsu 				}
864be2ac88cSJonathan Lemon 				/*
865be2ac88cSJonathan Lemon 				 * Socket is created in state SYN_RECEIVED.
866be2ac88cSJonathan Lemon 				 * Continue processing segment.
867be2ac88cSJonathan Lemon 				 */
868f76fcf6dSJeffrey Hsu 				INP_UNLOCK(inp);
869be2ac88cSJonathan Lemon 				inp = sotoinpcb(so);
870f76fcf6dSJeffrey Hsu 				INP_LOCK(inp);
871be2ac88cSJonathan Lemon 				tp = intotcpcb(inp);
872be2ac88cSJonathan Lemon 				/*
873be2ac88cSJonathan Lemon 				 * This is what would have happened in
874e6658b12SRobert Watson 				 * tcp_output() when the SYN,ACK was sent.
875be2ac88cSJonathan Lemon 				 */
876be2ac88cSJonathan Lemon 				tp->snd_up = tp->snd_una;
877be2ac88cSJonathan Lemon 				tp->snd_max = tp->snd_nxt = tp->iss + 1;
878be2ac88cSJonathan Lemon 				tp->last_ack_sent = tp->rcv_nxt;
879be2ac88cSJonathan Lemon 				goto after_listen;
880be2ac88cSJonathan Lemon 			}
881be2ac88cSJonathan Lemon 			if (thflags & TH_RST) {
882be2ac88cSJonathan Lemon 				syncache_chkrst(&inc, th);
883be2ac88cSJonathan Lemon 				goto drop;
884be2ac88cSJonathan Lemon 			}
885fb59c426SYoshinobu Inoue 			if (thflags & TH_ACK) {
886be2ac88cSJonathan Lemon 				syncache_badack(&inc);
887ebb0cbeaSPaul Traina 				tcpstat.tcps_badsyn++;
888a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
889a57815efSBosko Milekic 				goto dropwithreset;
8904195b4afSPaul Traina 			}
891ebb0cbeaSPaul Traina 			goto drop;
892ebb0cbeaSPaul Traina 		}
89333841545SHajimu UMEMOTO 
894be2ac88cSJonathan Lemon 		/*
895be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
896be2ac88cSJonathan Lemon 		 */
89733841545SHajimu UMEMOTO #ifdef INET6
89833841545SHajimu UMEMOTO 		/*
89933841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
90033841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
90133841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
90233841545SHajimu UMEMOTO 		 * getting established.
90333841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
90433841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
90533841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
90633841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
90733841545SHajimu UMEMOTO 		 * for the exchange.
90833841545SHajimu UMEMOTO 		 *
90933841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
91033841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
91133841545SHajimu UMEMOTO 		 * SYN in this case.
91233841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
91333841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
91433841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
91533841545SHajimu UMEMOTO 		 *    used"
91633841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
91733841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
91833841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
91933841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
92033841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
92133841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
92233841545SHajimu UMEMOTO 		 *
92333841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
92433841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
92533841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
92633841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
92733841545SHajimu UMEMOTO 		 */
92833841545SHajimu UMEMOTO 		if (isipv6 && !ip6_use_deprecated) {
92933841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
93033841545SHajimu UMEMOTO 
93133841545SHajimu UMEMOTO 			if ((ia6 = ip6_getdstifaddr(m)) &&
93233841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
933f76fcf6dSJeffrey Hsu 				INP_UNLOCK(inp);
93433841545SHajimu UMEMOTO 				tp = NULL;
93533841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
93633841545SHajimu UMEMOTO 				goto dropwithreset;
93733841545SHajimu UMEMOTO 			}
93833841545SHajimu UMEMOTO 		}
93933841545SHajimu UMEMOTO #endif
94065f28919SJesper Skriver 		/*
941be2ac88cSJonathan Lemon 		 * If it is from this socket, drop it, it must be forged.
942be2ac88cSJonathan Lemon 		 * Don't bother responding if the destination was a broadcast.
94365f28919SJesper Skriver 		 */
944be2ac88cSJonathan Lemon 		if (th->th_dport == th->th_sport) {
945fb59c426SYoshinobu Inoue 			if (isipv6) {
946be2ac88cSJonathan Lemon 				if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
947be2ac88cSJonathan Lemon 						       &ip6->ip6_src))
948be2ac88cSJonathan Lemon 					goto drop;
949c068736aSJeffrey Hsu 			} else {
950be2ac88cSJonathan Lemon 				if (ip->ip_dst.s_addr == ip->ip_src.s_addr)
951be2ac88cSJonathan Lemon 					goto drop;
952be2ac88cSJonathan Lemon 			}
953c068736aSJeffrey Hsu 		}
954be2ac88cSJonathan Lemon 		/*
955be2ac88cSJonathan Lemon 		 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
9562ca2159fSCrist J. Clark 		 *
95793ec91baSCrist J. Clark 		 * Note that it is quite possible to receive unicast
95893ec91baSCrist J. Clark 		 * link-layer packets with a broadcast IP address. Use
95993ec91baSCrist J. Clark 		 * in_broadcast() to find them.
960be2ac88cSJonathan Lemon 		 */
961be2ac88cSJonathan Lemon 		if (m->m_flags & (M_BCAST|M_MCAST))
962be2ac88cSJonathan Lemon 			goto drop;
963be2ac88cSJonathan Lemon 		if (isipv6) {
964be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
965be2ac88cSJonathan Lemon 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
966be2ac88cSJonathan Lemon 				goto drop;
967c068736aSJeffrey Hsu 		} else {
968be2ac88cSJonathan Lemon 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
969be2ac88cSJonathan Lemon 			    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
9702ca2159fSCrist J. Clark 			    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
9712ca2159fSCrist J. Clark 			    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
972be2ac88cSJonathan Lemon 				goto drop;
973c068736aSJeffrey Hsu 		}
974be2ac88cSJonathan Lemon 		/*
975be2ac88cSJonathan Lemon 		 * SYN appears to be valid; create compressed TCP state
976be2ac88cSJonathan Lemon 		 * for syncache, or perform t/tcp connection.
977be2ac88cSJonathan Lemon 		 */
978be2ac88cSJonathan Lemon 		if (so->so_qlen <= so->so_qlimit) {
9793c653157SHartmut Brandt #ifdef TCPDEBUG
9803c653157SHartmut Brandt 			if (so->so_options & SO_DEBUG)
9813c653157SHartmut Brandt 				tcp_trace(TA_INPUT, ostate, tp,
9823c653157SHartmut Brandt 				    (void *)tcp_saveipgen, &tcp_savetcp, 0);
9833c653157SHartmut Brandt #endif
984f72167f4SAndre Oppermann 			tcp_dooptions(&to, optp, optlen, TO_SYN);
985351630c4SAndre Oppermann 			if (!syncache_add(&inc, &to, th, inp, &so, m))
986351630c4SAndre Oppermann 				goto drop;	/* XXX: does not happen */
987f76fcf6dSJeffrey Hsu 			if (so == NULL) {
988be2ac88cSJonathan Lemon 				/*
989be2ac88cSJonathan Lemon 				 * Entry added to syncache, mbuf used to
990351630c4SAndre Oppermann 				 * send SYN,ACK packet.  Everything unlocked
991351630c4SAndre Oppermann 				 * already.
992be2ac88cSJonathan Lemon 				 */
993be2ac88cSJonathan Lemon 				return;
994f76fcf6dSJeffrey Hsu 			}
995351630c4SAndre Oppermann 			panic("T/TCP not supported at the moment");
996351630c4SAndre Oppermann #if 0 /* T/TCP */
997be2ac88cSJonathan Lemon 			/*
998be2ac88cSJonathan Lemon 			 * Segment passed TAO tests.
999464fcfbcSAndre Oppermann 			 * XXX: Can't happen at the moment.
1000be2ac88cSJonathan Lemon 			 */
1001f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
1002be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
1003f76fcf6dSJeffrey Hsu 			INP_LOCK(inp);
1004df8bae1dSRodney W. Grimes 			tp = intotcpcb(inp);
1005be2ac88cSJonathan Lemon 			tp->t_starttime = ticks;
1006be2ac88cSJonathan Lemon 			tp->t_state = TCPS_ESTABLISHED;
1007df8bae1dSRodney W. Grimes 
1008be2ac88cSJonathan Lemon 			/*
10093bfd6421SJonathan Lemon 			 * T/TCP logic:
10103bfd6421SJonathan Lemon 			 * If there is a FIN or if there is data, then
10113bfd6421SJonathan Lemon 			 * delay SYN,ACK(SYN) in the hope of piggy-backing
10123bfd6421SJonathan Lemon 			 * it on a response segment.  Otherwise must send
10133bfd6421SJonathan Lemon 			 * ACK now in case the other side is slow starting.
1014be2ac88cSJonathan Lemon 			 */
10153bfd6421SJonathan Lemon 			if (thflags & TH_FIN || tlen != 0)
10163bfd6421SJonathan Lemon 				tp->t_flags |= (TF_DELACK | TF_NEEDSYN);
10173bfd6421SJonathan Lemon 			else
1018f243998bSJonathan Lemon 				tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
1019464fcfbcSAndre Oppermann 			tiwin = th->th_win << tp->snd_scale;
1020be2ac88cSJonathan Lemon 			tcpstat.tcps_connects++;
1021be2ac88cSJonathan Lemon 			soisconnected(so);
1022be2ac88cSJonathan Lemon 			goto trimthenstep6;
1023351630c4SAndre Oppermann #endif	/* T/TCP */
1024df8bae1dSRodney W. Grimes 		}
1025be2ac88cSJonathan Lemon 		goto drop;
10264cc20ab1SSeigo Tanimura 	}
1027be2ac88cSJonathan Lemon after_listen:
1028de30ea13SRobert Watson 	KASSERT(headlocked, ("tcp_input: after_listen: head not locked"));
10297cfc6904SRobert Watson 	INP_LOCK_ASSERT(inp);
1030be2ac88cSJonathan Lemon 
1031a65e12b0SRobert Watson 	/* Syncache takes care of sockets in the listen state. */
1032a65e12b0SRobert Watson 	KASSERT(tp->t_state != TCPS_LISTEN, ("tcp_input: TCPS_LISTEN"));
1033df8bae1dSRodney W. Grimes 
1034df8bae1dSRodney W. Grimes 	/*
103553369ac9SAndre Oppermann 	 * This is the second part of the MSS DoS prevention code (after
103653369ac9SAndre Oppermann 	 * minmss on the sending side) and it deals with too many too small
103753369ac9SAndre Oppermann 	 * tcp packets in a too short timeframe (1 second).
103853369ac9SAndre Oppermann 	 *
103953369ac9SAndre Oppermann 	 * For every full second we count the number of received packets
104053369ac9SAndre Oppermann 	 * and bytes. If we get a lot of packets per second for this connection
104153369ac9SAndre Oppermann 	 * (tcp_minmssoverload) we take a closer look at it and compute the
104253369ac9SAndre Oppermann 	 * average packet size for the past second. If that is less than
104353369ac9SAndre Oppermann 	 * tcp_minmss we get too many packets with very small payload which
104453369ac9SAndre Oppermann 	 * is not good and burdens our system (and every packet generates
104553369ac9SAndre Oppermann 	 * a wakeup to the process connected to our socket). We can reasonable
104653369ac9SAndre Oppermann 	 * expect this to be small packet DoS attack to exhaust our CPU
104753369ac9SAndre Oppermann 	 * cycles.
104853369ac9SAndre Oppermann 	 *
104953369ac9SAndre Oppermann 	 * Care has to be taken for the minimum packet overload value. This
105053369ac9SAndre Oppermann 	 * value defines the minimum number of packets per second before we
105153369ac9SAndre Oppermann 	 * start to worry. This must not be too low to avoid killing for
105253369ac9SAndre Oppermann 	 * example interactive connections with many small packets like
105353369ac9SAndre Oppermann 	 * telnet or SSH.
105453369ac9SAndre Oppermann 	 *
105553369ac9SAndre Oppermann 	 * Setting either tcp_minmssoverload or tcp_minmss to "0" disables
105653369ac9SAndre Oppermann 	 * this check.
105753369ac9SAndre Oppermann 	 *
105853369ac9SAndre Oppermann 	 * Account for packet if payload packet, skip over ACK, etc.
105953369ac9SAndre Oppermann 	 */
106053369ac9SAndre Oppermann 	if (tcp_minmss && tcp_minmssoverload &&
106153369ac9SAndre Oppermann 	    tp->t_state == TCPS_ESTABLISHED && tlen > 0) {
106268d37625SStephan Uphoff 		if ((unsigned int)(tp->rcv_second - ticks) < hz) {
106353369ac9SAndre Oppermann 			tp->rcv_pps++;
106453369ac9SAndre Oppermann 			tp->rcv_byps += tlen + off;
106553369ac9SAndre Oppermann 			if (tp->rcv_pps > tcp_minmssoverload) {
106653369ac9SAndre Oppermann 				if ((tp->rcv_byps / tp->rcv_pps) < tcp_minmss) {
106753369ac9SAndre Oppermann 					printf("too many small tcp packets from "
106853369ac9SAndre Oppermann 					       "%s:%u, av. %lubyte/packet, "
106953369ac9SAndre Oppermann 					       "dropping connection\n",
107053369ac9SAndre Oppermann #ifdef INET6
107153369ac9SAndre Oppermann 						isipv6 ?
107253369ac9SAndre Oppermann 						ip6_sprintf(&inp->inp_inc.inc6_faddr) :
107353369ac9SAndre Oppermann #endif
107453369ac9SAndre Oppermann 						inet_ntoa(inp->inp_inc.inc_faddr),
107553369ac9SAndre Oppermann 						inp->inp_inc.inc_fport,
107653369ac9SAndre Oppermann 						tp->rcv_byps / tp->rcv_pps);
10771e2d989dSRobert Watson 					KASSERT(headlocked, ("tcp_input: "
10781e2d989dSRobert Watson 					    "after_listen: tcp_drop: head "
10791e2d989dSRobert Watson 					    "not locked"));
108053369ac9SAndre Oppermann 					tp = tcp_drop(tp, ECONNRESET);
108153369ac9SAndre Oppermann 					tcpstat.tcps_minmssdrops++;
108253369ac9SAndre Oppermann 					goto drop;
108353369ac9SAndre Oppermann 				}
108453369ac9SAndre Oppermann 			}
108553369ac9SAndre Oppermann 		} else {
108653369ac9SAndre Oppermann 			tp->rcv_second = ticks + hz;
108753369ac9SAndre Oppermann 			tp->rcv_pps = 1;
108853369ac9SAndre Oppermann 			tp->rcv_byps = tlen + off;
108953369ac9SAndre Oppermann 		}
109053369ac9SAndre Oppermann 	}
109153369ac9SAndre Oppermann 
109253369ac9SAndre Oppermann 	/*
1093df8bae1dSRodney W. Grimes 	 * Segment received on connection.
1094df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
1095df8bae1dSRodney W. Grimes 	 */
10969b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
10977ff19458SPaul Traina 	if (TCPS_HAVEESTABLISHED(tp->t_state))
10989b8b58e0SJonathan Lemon 		callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
1099df8bae1dSRodney W. Grimes 
1100df8bae1dSRodney W. Grimes 	/*
1101464fcfbcSAndre Oppermann 	 * Unscale the window into a 32-bit value.
1102464fcfbcSAndre Oppermann 	 * This value is bogus for the TCPS_SYN_SENT state
1103464fcfbcSAndre Oppermann 	 * and is overwritten later.
1104464fcfbcSAndre Oppermann 	 */
1105464fcfbcSAndre Oppermann 	tiwin = th->th_win << tp->snd_scale;
1106464fcfbcSAndre Oppermann 
1107464fcfbcSAndre Oppermann 	/*
1108f72167f4SAndre Oppermann 	 * Parse options on any incoming segment.
1109f72167f4SAndre Oppermann 	 */
1110f72167f4SAndre Oppermann 	tcp_dooptions(&to, optp, optlen, (thflags & TH_SYN) ? TO_SYN : 0);
1111f72167f4SAndre Oppermann 
1112f72167f4SAndre Oppermann 	/*
1113f72167f4SAndre Oppermann 	 * If echoed timestamp is later than the current time,
1114bf6d304aSAndre Oppermann 	 * fall back to non RFC1323 RTT calculation.  Normalize
1115bf6d304aSAndre Oppermann 	 * timestamp if syncookies were used when this connection
1116bf6d304aSAndre Oppermann 	 * was established.
1117f72167f4SAndre Oppermann 	 */
1118bf6d304aSAndre Oppermann 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
1119e16fa5caSJohn-Mark Gurney 		to.to_tsecr -= tp->ts_offset;
1120bf6d304aSAndre Oppermann 		if (TSTMP_GT(to.to_tsecr, ticks))
1121f72167f4SAndre Oppermann 			to.to_tsecr = 0;
1122bf6d304aSAndre Oppermann 	}
1123f72167f4SAndre Oppermann 
1124f72167f4SAndre Oppermann 	/*
112597d8d152SAndre Oppermann 	 * Process options only when we get SYN/ACK back. The SYN case
112697d8d152SAndre Oppermann 	 * for incoming connections is handled in tcp_syncache.
112797d8d152SAndre Oppermann 	 * XXX this is traditional behavior, may need to be cleaned up.
1128df8bae1dSRodney W. Grimes 	 */
11290a389eabSSimon L. B. Nielsen 	if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
1130464fcfbcSAndre Oppermann 		if ((to.to_flags & TOF_SCALE) &&
1131464fcfbcSAndre Oppermann 		    (tp->t_flags & TF_REQ_SCALE)) {
1132be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
1133464fcfbcSAndre Oppermann 			tp->snd_scale = to.to_requested_s_scale;
1134464fcfbcSAndre Oppermann 			tp->snd_wnd = th->th_win << tp->snd_scale;
1135464fcfbcSAndre Oppermann 			tiwin = tp->snd_wnd;
1136be2ac88cSJonathan Lemon 		}
1137be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_TS) {
1138be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
1139be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
1140be2ac88cSJonathan Lemon 			tp->ts_recent_age = ticks;
1141be2ac88cSJonathan Lemon 		}
1142be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_MSS)
1143be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
11446d90faf3SPaul Saab 		if (tp->sack_enable) {
11456d90faf3SPaul Saab 			if (!(to.to_flags & TOF_SACK))
11466d90faf3SPaul Saab 				tp->sack_enable = 0;
11476d90faf3SPaul Saab 			else
11486d90faf3SPaul Saab 				tp->t_flags |= TF_SACK_PERMIT;
11496d90faf3SPaul Saab 		}
11506d90faf3SPaul Saab 
11516d90faf3SPaul Saab 	}
11526d90faf3SPaul Saab 
1153df8bae1dSRodney W. Grimes 	/*
1154df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
1155df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
1156df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
1157df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
1158df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
1159df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
1160df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
1161df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
1162df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
1163df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
1164df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
1165df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
1166a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
1167a0292f23SGarrett Wollman 	 * Since we check for TCPS_ESTABLISHED above, it can only
1168a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
1169df8bae1dSRodney W. Grimes 	 */
1170df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
1171fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1172a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
1173be2ac88cSJonathan Lemon 	    ((to.to_flags & TOF_TS) == 0 ||
1174a0292f23SGarrett Wollman 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
1175c94c54e4SAndre Oppermann 	     th->th_seq == tp->rcv_nxt && tiwin && tiwin == tp->snd_wnd &&
1176df8bae1dSRodney W. Grimes 	     tp->snd_nxt == tp->snd_max) {
1177df8bae1dSRodney W. Grimes 
1178df8bae1dSRodney W. Grimes 		/*
1179df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
1180df8bae1dSRodney W. Grimes 		 * record the timestamp.
1181a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
1182a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1183df8bae1dSRodney W. Grimes 		 */
1184be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
1185fb59c426SYoshinobu Inoue 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
11869b8b58e0SJonathan Lemon 			tp->ts_recent_age = ticks;
1187a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
1188df8bae1dSRodney W. Grimes 		}
1189df8bae1dSRodney W. Grimes 
1190fb59c426SYoshinobu Inoue 		if (tlen == 0) {
1191fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
1192fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
1193233e8c18SGarrett Wollman 			    tp->snd_cwnd >= tp->snd_wnd &&
11946d90faf3SPaul Saab 			    ((!tcp_do_newreno && !tp->sack_enable &&
1195cb942153SJeffrey Hsu 			      tp->t_dupacks < tcprexmtthresh) ||
11966d90faf3SPaul Saab 			     ((tcp_do_newreno || tp->sack_enable) &&
1197482ac968SPaul Saab 			      !IN_FASTRECOVERY(tp) && to.to_nsacks == 0 &&
1198482ac968SPaul Saab 			      TAILQ_EMPTY(&tp->snd_holes)))) {
1199f76fcf6dSJeffrey Hsu 				KASSERT(headlocked, ("headlocked"));
1200e0bef1cbSRobert Watson 				INP_INFO_WUNLOCK(&tcbinfo);
1201e0bef1cbSRobert Watson 				headlocked = 0;
1202df8bae1dSRodney W. Grimes 				/*
1203df8bae1dSRodney W. Grimes 				 * this is a pure ack for outstanding data.
1204df8bae1dSRodney W. Grimes 				 */
1205df8bae1dSRodney W. Grimes 				++tcpstat.tcps_predack;
12069b8b58e0SJonathan Lemon 				/*
12079b8b58e0SJonathan Lemon 				 * "bad retransmit" recovery
12089b8b58e0SJonathan Lemon 				 */
12099b8b58e0SJonathan Lemon 				if (tp->t_rxtshift == 1 &&
12109b8b58e0SJonathan Lemon 				    ticks < tp->t_badrxtwin) {
1211cb942153SJeffrey Hsu 					++tcpstat.tcps_sndrexmitbad;
12129b8b58e0SJonathan Lemon 					tp->snd_cwnd = tp->snd_cwnd_prev;
12139b8b58e0SJonathan Lemon 					tp->snd_ssthresh =
12149b8b58e0SJonathan Lemon 					    tp->snd_ssthresh_prev;
12159d11646dSJeffrey Hsu 					tp->snd_recover = tp->snd_recover_prev;
12169d11646dSJeffrey Hsu 					if (tp->t_flags & TF_WASFRECOVERY)
12179d11646dSJeffrey Hsu 					    ENTER_FASTRECOVERY(tp);
12189b8b58e0SJonathan Lemon 					tp->snd_nxt = tp->snd_max;
12199b8b58e0SJonathan Lemon 					tp->t_badrxtwin = 0;
12209b8b58e0SJonathan Lemon 				}
1221fa55172bSMatthew Dillon 
1222fa55172bSMatthew Dillon 				/*
1223fa55172bSMatthew Dillon 				 * Recalculate the transmit timer / rtt.
1224fa55172bSMatthew Dillon 				 *
1225fa55172bSMatthew Dillon 				 * Some boxes send broken timestamp replies
1226fa55172bSMatthew Dillon 				 * during the SYN+ACK phase, ignore
1227fa55172bSMatthew Dillon 				 * timestamps of 0 or we could calculate a
1228fa55172bSMatthew Dillon 				 * huge RTT and blow up the retransmit timer.
1229fa55172bSMatthew Dillon 				 */
1230fa55172bSMatthew Dillon 				if ((to.to_flags & TOF_TS) != 0 &&
1231fa55172bSMatthew Dillon 				    to.to_tsecr) {
1232eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1233eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - to.to_tsecr)
1234eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - to.to_tsecr;
1235a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
12369b8b58e0SJonathan Lemon 					    ticks - to.to_tsecr + 1);
1237fa55172bSMatthew Dillon 				} else if (tp->t_rtttime &&
1238fa55172bSMatthew Dillon 					    SEQ_GT(th->th_ack, tp->t_rtseq)) {
1239eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1240eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - tp->t_rtttime)
1241eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - tp->t_rtttime;
1242c068736aSJeffrey Hsu 					tcp_xmit_timer(tp,
1243c068736aSJeffrey Hsu 							ticks - tp->t_rtttime);
1244fa55172bSMatthew Dillon 				}
12451fcc99b5SMatthew Dillon 				tcp_xmit_bandwidth_limit(tp, th->th_ack);
1246fb59c426SYoshinobu Inoue 				acked = th->th_ack - tp->snd_una;
1247df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackpack++;
1248df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackbyte += acked;
1249df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
12509d11646dSJeffrey Hsu 				if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
12519d11646dSJeffrey Hsu 				    SEQ_LEQ(th->th_ack, tp->snd_recover))
12529d11646dSJeffrey Hsu 					tp->snd_recover = th->th_ack - 1;
12539d11646dSJeffrey Hsu 				tp->snd_una = th->th_ack;
12541645d090SJeff Roberson 				/*
12551645d090SJeff Roberson 				 * pull snd_wl2 up to prevent seq wrap relative
12561645d090SJeff Roberson 				 * to th_ack.
12571645d090SJeff Roberson 				 */
1258cb942153SJeffrey Hsu 				tp->snd_wl2 = th->th_ack;
1259b5addd85SJeffrey Hsu 				tp->t_dupacks = 0;
1260df8bae1dSRodney W. Grimes 				m_freem(m);
1261fb59c426SYoshinobu Inoue 				ND6_HINT(tp); /* some progress has been done */
1262df8bae1dSRodney W. Grimes 
1263df8bae1dSRodney W. Grimes 				/*
1264df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
1265df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
1266df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
1267df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
1268df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
1269df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
1270df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
12713c653157SHartmut Brandt 
12723c653157SHartmut Brandt #ifdef TCPDEBUG
12733c653157SHartmut Brandt 				if (so->so_options & SO_DEBUG)
12743c653157SHartmut Brandt 					tcp_trace(TA_INPUT, ostate, tp,
12753c653157SHartmut Brandt 					    (void *)tcp_saveipgen,
12763c653157SHartmut Brandt 					    &tcp_savetcp, 0);
12773c653157SHartmut Brandt #endif
1278df8bae1dSRodney W. Grimes 				 */
1279df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
12809b8b58e0SJonathan Lemon 					callout_stop(tp->tt_rexmt);
12819b8b58e0SJonathan Lemon 				else if (!callout_active(tp->tt_persist))
12829b8b58e0SJonathan Lemon 					callout_reset(tp->tt_rexmt,
12839b8b58e0SJonathan Lemon 						      tp->t_rxtcur,
12849b8b58e0SJonathan Lemon 						      tcp_timer_rexmt, tp);
1285df8bae1dSRodney W. Grimes 
1286df8bae1dSRodney W. Grimes 				sowwakeup(so);
1287df8bae1dSRodney W. Grimes 				if (so->so_snd.sb_cc)
1288df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1289a14c749fSJonathan Lemon 				goto check_delack;
1290df8bae1dSRodney W. Grimes 			}
1291fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1292fb59c426SYoshinobu Inoue 		    LIST_EMPTY(&tp->t_segq) &&
1293fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
1294f76fcf6dSJeffrey Hsu 			KASSERT(headlocked, ("headlocked"));
1295e0bef1cbSRobert Watson 			INP_INFO_WUNLOCK(&tcbinfo);
1296e0bef1cbSRobert Watson 			headlocked = 0;
1297df8bae1dSRodney W. Grimes 			/*
1298df8bae1dSRodney W. Grimes 			 * this is a pure, in-sequence data packet
1299df8bae1dSRodney W. Grimes 			 * with nothing on the reassembly queue and
1300df8bae1dSRodney W. Grimes 			 * we have enough buffer space to take it.
1301df8bae1dSRodney W. Grimes 			 */
13026d90faf3SPaul Saab 			/* Clean receiver SACK report if present */
13036d90faf3SPaul Saab 			if (tp->sack_enable && tp->rcv_numsacks)
13046d90faf3SPaul Saab 				tcp_clean_sackreport(tp);
1305df8bae1dSRodney W. Grimes 			++tcpstat.tcps_preddat;
1306fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
13071645d090SJeff Roberson 			/*
13081645d090SJeff Roberson 			 * Pull snd_wl1 up to prevent seq wrap relative to
13091645d090SJeff Roberson 			 * th_seq.
13101645d090SJeff Roberson 			 */
13111645d090SJeff Roberson 			tp->snd_wl1 = th->th_seq;
13121645d090SJeff Roberson 			/*
13131645d090SJeff Roberson 			 * Pull rcv_up up to prevent seq wrap relative to
13141645d090SJeff Roberson 			 * rcv_nxt.
13151645d090SJeff Roberson 			 */
13161645d090SJeff Roberson 			tp->rcv_up = tp->rcv_nxt;
1317df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpack++;
1318fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbyte += tlen;
1319fb59c426SYoshinobu Inoue 			ND6_HINT(tp);	/* some progress has been done */
1320df8bae1dSRodney W. Grimes 			/*
13213c653157SHartmut Brandt #ifdef TCPDEBUG
13223c653157SHartmut Brandt 			if (so->so_options & SO_DEBUG)
13233c653157SHartmut Brandt 				tcp_trace(TA_INPUT, ostate, tp,
13243c653157SHartmut Brandt 				    (void *)tcp_saveipgen, &tcp_savetcp, 0);
13253c653157SHartmut Brandt #endif
1326755c1f07SAndras Olah 			 * Add data to socket buffer.
1327df8bae1dSRodney W. Grimes 			 */
13281e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
1329c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1330c1c36a2cSMike Silbersack 				m_freem(m);
1331c1c36a2cSMike Silbersack 			} else {
1332fb59c426SYoshinobu Inoue 				m_adj(m, drop_hdrlen);	/* delayed header drop */
13331e4d7da7SRobert Watson 				sbappendstream_locked(&so->so_rcv, m);
1334c1c36a2cSMike Silbersack 			}
13351e4d7da7SRobert Watson 			sorwakeup_locked(so);
1336d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp)) {
13373bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
1338f498eeeeSDavid Greenman 			} else {
1339e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
1340e612a582SDavid Greenman 				tcp_output(tp);
1341e612a582SDavid Greenman 			}
1342a14c749fSJonathan Lemon 			goto check_delack;
1343df8bae1dSRodney W. Grimes 		}
1344df8bae1dSRodney W. Grimes 	}
1345df8bae1dSRodney W. Grimes 
1346df8bae1dSRodney W. Grimes 	/*
1347df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1348df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1349df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1350df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1351df8bae1dSRodney W. Grimes 	 */
1352df8bae1dSRodney W. Grimes 	{ int win;
1353df8bae1dSRodney W. Grimes 
1354df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1355df8bae1dSRodney W. Grimes 	if (win < 0)
1356df8bae1dSRodney W. Grimes 		win = 0;
135766e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1358df8bae1dSRodney W. Grimes 	}
1359df8bae1dSRodney W. Grimes 
1360df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1361df8bae1dSRodney W. Grimes 
1362df8bae1dSRodney W. Grimes 	/*
1363764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1364764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1365764d8cefSBill Fenner 	 */
1366764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1367fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1368fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1369a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1370a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1371a57815efSBosko Milekic 				goto dropwithreset;
1372a57815efSBosko Milekic 		}
1373764d8cefSBill Fenner 		break;
1374764d8cefSBill Fenner 
1375764d8cefSBill Fenner 	/*
1376df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
1377df8bae1dSRodney W. Grimes 	 *	if seg contains an ACK, but not for our SYN, drop the input.
1378df8bae1dSRodney W. Grimes 	 *	if seg contains a RST, then drop the connection.
1379df8bae1dSRodney W. Grimes 	 *	if seg does not contain SYN, then drop it.
1380df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
1381df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
1382df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
1383df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1384df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
1385df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
1386df8bae1dSRodney W. Grimes 	 */
1387df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
1388fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1389fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
1390fb59c426SYoshinobu Inoue 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1391a57815efSBosko Milekic 			rstreason = BANDLIM_UNLIMITED;
1392a0292f23SGarrett Wollman 			goto dropwithreset;
1393a0292f23SGarrett Wollman 		}
1394fb59c426SYoshinobu Inoue 		if (thflags & TH_RST) {
13951e2d989dSRobert Watson 			if (thflags & TH_ACK) {
13961e2d989dSRobert Watson 				KASSERT(headlocked, ("tcp_input: after_listen"
13971e2d989dSRobert Watson 				    ": tcp_drop.2: head not locked"));
1398df8bae1dSRodney W. Grimes 				tp = tcp_drop(tp, ECONNREFUSED);
13991e2d989dSRobert Watson 			}
1400df8bae1dSRodney W. Grimes 			goto drop;
1401df8bae1dSRodney W. Grimes 		}
1402fb59c426SYoshinobu Inoue 		if ((thflags & TH_SYN) == 0)
1403df8bae1dSRodney W. Grimes 			goto drop;
1404464fcfbcSAndre Oppermann 
1405464fcfbcSAndre Oppermann 		/* Initial send window, already scaled. */
1406464fcfbcSAndre Oppermann 		tp->snd_wnd = th->th_win;
1407a0292f23SGarrett Wollman 
1408fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
1409df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
1410fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1411845799c1SAndras Olah 			tcpstat.tcps_connects++;
1412845799c1SAndras Olah 			soisconnected(so);
1413c488362eSRobert Watson #ifdef MAC
1414310e7cebSRobert Watson 			SOCK_LOCK(so);
1415c488362eSRobert Watson 			mac_set_socket_peer_from_mbuf(m, so);
1416310e7cebSRobert Watson 			SOCK_UNLOCK(so);
1417c488362eSRobert Watson #endif
1418845799c1SAndras Olah 			/* Do window scaling on this connection? */
1419845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1420845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1421845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
1422845799c1SAndras Olah 			}
1423a0292f23SGarrett Wollman 			tp->rcv_adv += tp->rcv_wnd;
1424a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
1425a0292f23SGarrett Wollman 			/*
1426a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
1427a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
1428a0292f23SGarrett Wollman 			 */
1429d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp) && tlen != 0)
14309b8b58e0SJonathan Lemon 				callout_reset(tp->tt_delack, tcp_delacktime,
14319b8b58e0SJonathan Lemon 				    tcp_timer_delack, tp);
1432a0292f23SGarrett Wollman 			else
1433a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
1434a0292f23SGarrett Wollman 			/*
1435a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
1436a0292f23SGarrett Wollman 			 * Transitions:
1437a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
1438a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
1439a0292f23SGarrett Wollman 			 */
14409b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
1441a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
1442a0292f23SGarrett Wollman 				tp->t_state = TCPS_FIN_WAIT_1;
1443a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
1444fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
14457ff19458SPaul Traina 			} else {
1446a0292f23SGarrett Wollman 				tp->t_state = TCPS_ESTABLISHED;
14479b8b58e0SJonathan Lemon 				callout_reset(tp->tt_keep, tcp_keepidle,
14489b8b58e0SJonathan Lemon 					      tcp_timer_keep, tp);
14497ff19458SPaul Traina 			}
1450a0292f23SGarrett Wollman 		} else {
1451a0292f23SGarrett Wollman 			/*
1452c068736aSJeffrey Hsu 			 * Received initial SYN in SYN-SENT[*] state =>
1453c068736aSJeffrey Hsu 			 * simultaneous open.  If segment contains CC option
1454c068736aSJeffrey Hsu 			 * and there is a cached CC, apply TAO test.
1455c068736aSJeffrey Hsu 			 * If it succeeds, connection is * half-synchronized.
1456c068736aSJeffrey Hsu 			 * Otherwise, do 3-way handshake:
1457a0292f23SGarrett Wollman 			 *        SYN-SENT -> SYN-RECEIVED
1458a0292f23SGarrett Wollman 			 *        SYN-SENT* -> SYN-RECEIVED*
1459a0292f23SGarrett Wollman 			 * If there was no CC option, clear cached CC value.
1460a0292f23SGarrett Wollman 			 */
14614b8e98d6SQing Li 			tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
14629b8b58e0SJonathan Lemon 			callout_stop(tp->tt_rexmt);
1463df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_SYN_RECEIVED;
1464a0292f23SGarrett Wollman 		}
1465df8bae1dSRodney W. Grimes 
1466351630c4SAndre Oppermann #if 0 /* T/TCP */
1467df8bae1dSRodney W. Grimes trimthenstep6:
1468351630c4SAndre Oppermann #endif
1469de30ea13SRobert Watson 		KASSERT(headlocked, ("tcp_input: trimthenstep6: head not "
1470de30ea13SRobert Watson 		    "locked"));
14717cfc6904SRobert Watson 		INP_LOCK_ASSERT(inp);
14727cfc6904SRobert Watson 
1473df8bae1dSRodney W. Grimes 		/*
1474fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
1475df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
1476df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
1477df8bae1dSRodney W. Grimes 		 */
1478fb59c426SYoshinobu Inoue 		th->th_seq++;
1479fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
1480fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
1481df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
1482fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
1483fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
1484df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpackafterwin++;
1485df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
1486df8bae1dSRodney W. Grimes 		}
1487fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
1488fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
1489a0292f23SGarrett Wollman 		/*
1490a0292f23SGarrett Wollman 		 * Client side of transaction: already sent SYN and data.
1491a0292f23SGarrett Wollman 		 * If the remote host used T/TCP to validate the SYN,
1492a0292f23SGarrett Wollman 		 * our data will be ACK'd; if so, enter normal data segment
1493a0292f23SGarrett Wollman 		 * processing in the middle of step 5, ack processing.
1494a0292f23SGarrett Wollman 		 * Otherwise, goto step 6.
1495a0292f23SGarrett Wollman 		 */
1496fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK)
1497a0292f23SGarrett Wollman 			goto process_ACK;
1498c068736aSJeffrey Hsu 
1499df8bae1dSRodney W. Grimes 		goto step6;
1500c068736aSJeffrey Hsu 
1501a0292f23SGarrett Wollman 	/*
1502a0292f23SGarrett Wollman 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
1503c94c54e4SAndre Oppermann 	 *      do normal processing.
1504a0292f23SGarrett Wollman 	 *
1505c94c54e4SAndre Oppermann 	 * NB: Leftover from RFC1644 T/TCP.  Cases to be reused later.
1506a0292f23SGarrett Wollman 	 */
1507a0292f23SGarrett Wollman 	case TCPS_LAST_ACK:
1508a0292f23SGarrett Wollman 	case TCPS_CLOSING:
1509a0292f23SGarrett Wollman 	case TCPS_TIME_WAIT:
1510340c35deSJonathan Lemon 		KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
1511a0292f23SGarrett Wollman 		break;  /* continue normal processing */
1512df8bae1dSRodney W. Grimes 	}
1513df8bae1dSRodney W. Grimes 
1514df8bae1dSRodney W. Grimes 	/*
1515df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
151680ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
151780ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
151880ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
151980ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
152080ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
152180ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
152280ab7c0eSGarrett Wollman 	 * killing a connection).
152380ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
1524a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
1525df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
1526df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
1527df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
1528df8bae1dSRodney W. Grimes 	 *
152980ab7c0eSGarrett Wollman 	 *
153080ab7c0eSGarrett Wollman 	 * If the RST bit is set, check the sequence number to see
153180ab7c0eSGarrett Wollman 	 * if this is a valid reset segment.
153280ab7c0eSGarrett Wollman 	 * RFC 793 page 37:
153380ab7c0eSGarrett Wollman 	 *   In all states except SYN-SENT, all reset (RST) segments
153480ab7c0eSGarrett Wollman 	 *   are validated by checking their SEQ-fields.  A reset is
153580ab7c0eSGarrett Wollman 	 *   valid if its sequence number is in the window.
153680ab7c0eSGarrett Wollman 	 * Note: this does not take into account delayed ACKs, so
153780ab7c0eSGarrett Wollman 	 *   we should test against last_ack_sent instead of rcv_nxt.
15381a244a61SJonathan Lemon 	 *   The sequence number in the reset segment is normally an
15391a244a61SJonathan Lemon 	 *   echo of our outgoing acknowlegement numbers, but some hosts
15401a244a61SJonathan Lemon 	 *   send a reset with the sequence number at the rightmost edge
15411a244a61SJonathan Lemon 	 *   of our receive window, and we have to handle this case.
154280dd2a81SMike Silbersack 	 * Note 2: Paul Watson's paper "Slipping in the Window" has shown
154380dd2a81SMike Silbersack 	 *   that brute force RST attacks are possible.  To combat this,
154480dd2a81SMike Silbersack 	 *   we use a much stricter check while in the ESTABLISHED state,
154580dd2a81SMike Silbersack 	 *   only accepting RSTs where the sequence number is equal to
154680dd2a81SMike Silbersack 	 *   last_ack_sent.  In all other states (the states in which a
154780dd2a81SMike Silbersack 	 *   RST is more likely), the more permissive check is used.
154880ab7c0eSGarrett Wollman 	 * If we have multiple segments in flight, the intial reset
154980ab7c0eSGarrett Wollman 	 * segment sequence numbers will be to the left of last_ack_sent,
155080ab7c0eSGarrett Wollman 	 * but they will eventually catch up.
155180ab7c0eSGarrett Wollman 	 * In any case, it never made sense to trim reset segments to
155280ab7c0eSGarrett Wollman 	 * fit the receive window since RFC 1122 says:
155380ab7c0eSGarrett Wollman 	 *   4.2.2.12  RST Segment: RFC-793 Section 3.4
155480ab7c0eSGarrett Wollman 	 *
155580ab7c0eSGarrett Wollman 	 *    A TCP SHOULD allow a received RST segment to include data.
155680ab7c0eSGarrett Wollman 	 *
155780ab7c0eSGarrett Wollman 	 *    DISCUSSION
155880ab7c0eSGarrett Wollman 	 *         It has been suggested that a RST segment could contain
155980ab7c0eSGarrett Wollman 	 *         ASCII text that encoded and explained the cause of the
156080ab7c0eSGarrett Wollman 	 *         RST.  No standard has yet been established for such
156180ab7c0eSGarrett Wollman 	 *         data.
156280ab7c0eSGarrett Wollman 	 *
156380ab7c0eSGarrett Wollman 	 * If the reset segment passes the sequence number test examine
156480ab7c0eSGarrett Wollman 	 * the state:
156580ab7c0eSGarrett Wollman 	 *    SYN_RECEIVED STATE:
156680ab7c0eSGarrett Wollman 	 *	If passive open, return to LISTEN state.
156780ab7c0eSGarrett Wollman 	 *	If active open, inform user that connection was refused.
1568745bab7fSDima Dorfman 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
156980ab7c0eSGarrett Wollman 	 *	Inform user that connection was reset, and close tcb.
1570e9bd3a37SJonathan M. Bresler 	 *    CLOSING, LAST_ACK STATES:
157180ab7c0eSGarrett Wollman 	 *	Close the tcb.
1572e9bd3a37SJonathan M. Bresler 	 *    TIME_WAIT STATE:
157380ab7c0eSGarrett Wollman 	 *	Drop the segment - see Stevens, vol. 2, p. 964 and
157480ab7c0eSGarrett Wollman 	 *      RFC 1337.
157580ab7c0eSGarrett Wollman 	 */
1576fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
15776a220ed8SMike Silbersack 		if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
15786a220ed8SMike Silbersack 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
15796a220ed8SMike Silbersack 		    (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) {
158080ab7c0eSGarrett Wollman 			switch (tp->t_state) {
158180ab7c0eSGarrett Wollman 
158280ab7c0eSGarrett Wollman 			case TCPS_SYN_RECEIVED:
158380ab7c0eSGarrett Wollman 				so->so_error = ECONNREFUSED;
158480ab7c0eSGarrett Wollman 				goto close;
158580ab7c0eSGarrett Wollman 
158680ab7c0eSGarrett Wollman 			case TCPS_ESTABLISHED:
1587a69968eeSMike Silbersack 				if (tp->last_ack_sent != th->th_seq &&
1588a69968eeSMike Silbersack 			 	    tcp_insecure_rst == 0) {
158980dd2a81SMike Silbersack 					tcpstat.tcps_badrst++;
159080dd2a81SMike Silbersack 					goto drop;
159180dd2a81SMike Silbersack 				}
159280ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_1:
159380ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_2:
159480ab7c0eSGarrett Wollman 			case TCPS_CLOSE_WAIT:
159580ab7c0eSGarrett Wollman 				so->so_error = ECONNRESET;
159680ab7c0eSGarrett Wollman 			close:
159780ab7c0eSGarrett Wollman 				tp->t_state = TCPS_CLOSED;
159880ab7c0eSGarrett Wollman 				tcpstat.tcps_drops++;
1599416738a7SRobert Watson 				KASSERT(headlocked, ("tcp_input: "
1600416738a7SRobert Watson 				    "trimthenstep6: tcp_close: head not "
1601416738a7SRobert Watson 				    "locked"));
160280ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
160380ab7c0eSGarrett Wollman 				break;
160480ab7c0eSGarrett Wollman 
160580ab7c0eSGarrett Wollman 			case TCPS_CLOSING:
160680ab7c0eSGarrett Wollman 			case TCPS_LAST_ACK:
1607416738a7SRobert Watson 				KASSERT(headlocked, ("trimthenstep6: "
1608416738a7SRobert Watson 				    "tcp_close.2: head not locked"));
160980ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
161080ab7c0eSGarrett Wollman 				break;
161180ab7c0eSGarrett Wollman 
161280ab7c0eSGarrett Wollman 			case TCPS_TIME_WAIT:
1613340c35deSJonathan Lemon 				KASSERT(tp->t_state != TCPS_TIME_WAIT,
1614340c35deSJonathan Lemon 				    ("timewait"));
161580ab7c0eSGarrett Wollman 				break;
161680ab7c0eSGarrett Wollman 			}
161780ab7c0eSGarrett Wollman 		}
161880ab7c0eSGarrett Wollman 		goto drop;
161980ab7c0eSGarrett Wollman 	}
162080ab7c0eSGarrett Wollman 
162180ab7c0eSGarrett Wollman 	/*
1622df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
1623df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
1624df8bae1dSRodney W. Grimes 	 */
1625be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
162680ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
1627df8bae1dSRodney W. Grimes 
1628df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
16299b8b58e0SJonathan Lemon 		if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) {
1630df8bae1dSRodney W. Grimes 			/*
1631df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
1632df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
1633df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
1634df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
1635df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
1636df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
1637df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
1638df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
1639df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
1640df8bae1dSRodney W. Grimes 			 */
1641df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
1642df8bae1dSRodney W. Grimes 		} else {
1643df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvduppack++;
1644fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvdupbyte += tlen;
1645df8bae1dSRodney W. Grimes 			tcpstat.tcps_pawsdrop++;
164607fd333dSMatthew Dillon 			if (tlen)
1647df8bae1dSRodney W. Grimes 				goto dropafterack;
16481ab4789dSMatthew Dillon 			goto drop;
16491ab4789dSMatthew Dillon 		}
1650df8bae1dSRodney W. Grimes 	}
1651df8bae1dSRodney W. Grimes 
1652a0292f23SGarrett Wollman 	/*
165380ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
165480ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
165580ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
165680ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
165780ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
165880ab7c0eSGarrett Wollman 	 */
1659a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
1660a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
1661a57815efSBosko Milekic 		goto dropwithreset;
1662a57815efSBosko Milekic 	}
166380ab7c0eSGarrett Wollman 
1664fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
1665df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1666fb59c426SYoshinobu Inoue 		if (thflags & TH_SYN) {
1667fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
1668fb59c426SYoshinobu Inoue 			th->th_seq++;
1669fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
1670fb59c426SYoshinobu Inoue 				th->th_urp--;
1671df8bae1dSRodney W. Grimes 			else
1672fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
1673df8bae1dSRodney W. Grimes 			todrop--;
1674df8bae1dSRodney W. Grimes 		}
1675df8bae1dSRodney W. Grimes 		/*
1676dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
1677df8bae1dSRodney W. Grimes 		 */
1678fb59c426SYoshinobu Inoue 		if (todrop > tlen
1679fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
1680dac20301SGarrett Wollman 			/*
1681dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
1682dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
1683dac20301SGarrett Wollman 			 * of sequence; drop it.
1684dac20301SGarrett Wollman 			 */
1685fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
1686dac20301SGarrett Wollman 
1687df8bae1dSRodney W. Grimes 			/*
1688dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
1689dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
1690df8bae1dSRodney W. Grimes 			 */
1691dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
1692fb59c426SYoshinobu Inoue 			todrop = tlen;
1693dac20301SGarrett Wollman 			tcpstat.tcps_rcvduppack++;
1694dac20301SGarrett Wollman 			tcpstat.tcps_rcvdupbyte += todrop;
1695df8bae1dSRodney W. Grimes 		} else {
1696df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartduppack++;
1697df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartdupbyte += todrop;
1698df8bae1dSRodney W. Grimes 		}
1699fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
1700fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
1701fb59c426SYoshinobu Inoue 		tlen -= todrop;
1702fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
1703fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
1704df8bae1dSRodney W. Grimes 		else {
1705fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
1706fb59c426SYoshinobu Inoue 			th->th_urp = 0;
1707df8bae1dSRodney W. Grimes 		}
1708df8bae1dSRodney W. Grimes 	}
1709df8bae1dSRodney W. Grimes 
1710df8bae1dSRodney W. Grimes 	/*
1711df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
1712df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
1713df8bae1dSRodney W. Grimes 	 */
1714df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
1715fb59c426SYoshinobu Inoue 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
1716416738a7SRobert Watson 		KASSERT(headlocked, ("trimthenstep6: tcp_close.3: head not "
1717416738a7SRobert Watson 		    "locked"));
1718df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1719df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvafterclose++;
1720a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
1721df8bae1dSRodney W. Grimes 		goto dropwithreset;
1722df8bae1dSRodney W. Grimes 	}
1723df8bae1dSRodney W. Grimes 
1724df8bae1dSRodney W. Grimes 	/*
1725df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
1726df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
1727df8bae1dSRodney W. Grimes 	 */
1728fb59c426SYoshinobu Inoue 	todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd);
1729df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1730df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvpackafterwin++;
1731fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
1732fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbyteafterwin += tlen;
1733df8bae1dSRodney W. Grimes 			/*
1734df8bae1dSRodney W. Grimes 			 * If a new connection request is received
1735df8bae1dSRodney W. Grimes 			 * while in TIME_WAIT, drop the old connection
1736df8bae1dSRodney W. Grimes 			 * and start over if the sequence numbers
1737df8bae1dSRodney W. Grimes 			 * are above the previous ones.
1738df8bae1dSRodney W. Grimes 			 */
1739340c35deSJonathan Lemon 			KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
1740fb59c426SYoshinobu Inoue 			if (thflags & TH_SYN &&
1741df8bae1dSRodney W. Grimes 			    tp->t_state == TCPS_TIME_WAIT &&
1742fb59c426SYoshinobu Inoue 			    SEQ_GT(th->th_seq, tp->rcv_nxt)) {
1743416738a7SRobert Watson 				KASSERT(headlocked, ("trimthenstep6: "
1744416738a7SRobert Watson 				    "tcp_close.4: head not locked"));
1745df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
1746df8bae1dSRodney W. Grimes 				goto findpcb;
1747df8bae1dSRodney W. Grimes 			}
1748df8bae1dSRodney W. Grimes 			/*
1749df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
1750df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
1751df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
1752df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
1753df8bae1dSRodney W. Grimes 			 * and ack.
1754df8bae1dSRodney W. Grimes 			 */
1755fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
1756df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
1757df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvwinprobe++;
1758df8bae1dSRodney W. Grimes 			} else
1759df8bae1dSRodney W. Grimes 				goto dropafterack;
1760df8bae1dSRodney W. Grimes 		} else
1761df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
1762df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
1763fb59c426SYoshinobu Inoue 		tlen -= todrop;
1764fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
1765df8bae1dSRodney W. Grimes 	}
1766df8bae1dSRodney W. Grimes 
1767df8bae1dSRodney W. Grimes 	/*
1768df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
1769df8bae1dSRodney W. Grimes 	 * record its timestamp.
1770cf09195bSPaul Saab 	 * NOTE:
1771cf09195bSPaul Saab 	 * 1) That the test incorporates suggestions from the latest
1772a0292f23SGarrett Wollman 	 *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
1773cf09195bSPaul Saab 	 * 2) That updating only on newer timestamps interferes with
1774cf09195bSPaul Saab 	 *    our earlier PAWS tests, so this check should be solely
1775cf09195bSPaul Saab 	 *    predicated on the sequence space of this segment.
1776cf09195bSPaul Saab 	 * 3) That we modify the segment boundary check to be
1777cf09195bSPaul Saab 	 *        Last.ACK.Sent <= SEG.SEQ + SEG.Len
1778cf09195bSPaul Saab 	 *    instead of RFC1323's
1779cf09195bSPaul Saab 	 *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
1780cf09195bSPaul Saab 	 *    This modified check allows us to overcome RFC1323's
1781cf09195bSPaul Saab 	 *    limitations as described in Stevens TCP/IP Illustrated
1782cf09195bSPaul Saab 	 *    Vol. 2 p.869. In such cases, we can still calculate the
1783cf09195bSPaul Saab 	 *    RTT correctly when RCV.NXT == Last.ACK.Sent.
1784df8bae1dSRodney W. Grimes 	 */
1785be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
1786cf09195bSPaul Saab 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
1787cf09195bSPaul Saab 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
1788cf09195bSPaul Saab 		((thflags & (TH_SYN|TH_FIN)) != 0))) {
17899b8b58e0SJonathan Lemon 		tp->ts_recent_age = ticks;
1790a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
1791df8bae1dSRodney W. Grimes 	}
1792df8bae1dSRodney W. Grimes 
1793df8bae1dSRodney W. Grimes 	/*
1794df8bae1dSRodney W. Grimes 	 * If a SYN is in the window, then this is an
1795df8bae1dSRodney W. Grimes 	 * error and we send an RST and drop the connection.
1796df8bae1dSRodney W. Grimes 	 */
1797fb59c426SYoshinobu Inoue 	if (thflags & TH_SYN) {
17981e2d989dSRobert Watson 		KASSERT(headlocked, ("tcp_input: tcp_drop: trimthenstep6: "
17991e2d989dSRobert Watson 		    "head not locked"));
1800df8bae1dSRodney W. Grimes 		tp = tcp_drop(tp, ECONNRESET);
1801a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
1802122aad88SAndre Oppermann 		goto drop;
1803df8bae1dSRodney W. Grimes 	}
1804df8bae1dSRodney W. Grimes 
1805a0292f23SGarrett Wollman 	/*
1806a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
1807a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
1808a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
1809a0292f23SGarrett Wollman 	 */
1810fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
1811a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
1812a0292f23SGarrett Wollman 		    (tp->t_flags & TF_NEEDSYN))
1813a0292f23SGarrett Wollman 			goto step6;
18145e1aa279SDavid Malone 		else if (tp->t_flags & TF_ACKNOW)
18155e1aa279SDavid Malone 			goto dropafterack;
1816a0292f23SGarrett Wollman 		else
1817a0292f23SGarrett Wollman 			goto drop;
1818a0292f23SGarrett Wollman 	}
1819df8bae1dSRodney W. Grimes 
1820df8bae1dSRodney W. Grimes 	/*
1821df8bae1dSRodney W. Grimes 	 * Ack processing.
1822df8bae1dSRodney W. Grimes 	 */
1823df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1824df8bae1dSRodney W. Grimes 
1825df8bae1dSRodney W. Grimes 	/*
1826764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
1827764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
1828764d8cefSBill Fenner 	 * The ACK was checked above.
1829df8bae1dSRodney W. Grimes 	 */
1830df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1831a0292f23SGarrett Wollman 
1832df8bae1dSRodney W. Grimes 		tcpstat.tcps_connects++;
1833df8bae1dSRodney W. Grimes 		soisconnected(so);
1834df8bae1dSRodney W. Grimes 		/* Do window scaling? */
1835df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1836df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1837df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
1838464fcfbcSAndre Oppermann 			tp->snd_wnd = tiwin;
1839df8bae1dSRodney W. Grimes 		}
1840a0292f23SGarrett Wollman 		/*
1841a0292f23SGarrett Wollman 		 * Make transitions:
1842a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
1843a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
1844a0292f23SGarrett Wollman 		 */
18459b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
1846a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDFIN) {
1847a0292f23SGarrett Wollman 			tp->t_state = TCPS_FIN_WAIT_1;
1848a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDFIN;
18497ff19458SPaul Traina 		} else {
1850a0292f23SGarrett Wollman 			tp->t_state = TCPS_ESTABLISHED;
18519b8b58e0SJonathan Lemon 			callout_reset(tp->tt_keep, tcp_keepidle,
18529b8b58e0SJonathan Lemon 				      tcp_timer_keep, tp);
18537ff19458SPaul Traina 		}
1854a0292f23SGarrett Wollman 		/*
1855a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
1856a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
1857a0292f23SGarrett Wollman 		 */
1858fb59c426SYoshinobu Inoue 		if (tlen == 0 && (thflags & TH_FIN) == 0)
1859fb59c426SYoshinobu Inoue 			(void) tcp_reass(tp, (struct tcphdr *)0, 0,
1860a0292f23SGarrett Wollman 			    (struct mbuf *)0);
1861fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
186293b0017fSPhilippe Charnier 		/* FALLTHROUGH */
1863df8bae1dSRodney W. Grimes 
1864df8bae1dSRodney W. Grimes 	/*
1865df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1866df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
1867fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
1868fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
1869df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
1870df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
1871df8bae1dSRodney W. Grimes 	 */
1872df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1873df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
1874df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
1875df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1876df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
1877df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
1878df8bae1dSRodney W. Grimes 	case TCPS_TIME_WAIT:
1879340c35deSJonathan Lemon 		KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
18805a53ca16SPaul Saab 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
18815a53ca16SPaul Saab 			tcpstat.tcps_rcvacktoomuch++;
18825a53ca16SPaul Saab 			goto dropafterack;
18835a53ca16SPaul Saab 		}
1884482ac968SPaul Saab 		if (tp->sack_enable &&
1885482ac968SPaul Saab 		    (to.to_nsacks > 0 || !TAILQ_EMPTY(&tp->snd_holes)))
18865a53ca16SPaul Saab 			tcp_sack_doack(tp, &to, th->th_ack);
1887fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
1888fb59c426SYoshinobu Inoue 			if (tlen == 0 && tiwin == tp->snd_wnd) {
1889df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvdupack++;
1890df8bae1dSRodney W. Grimes 				/*
1891df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
1892df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
1893df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
1894df8bae1dSRodney W. Grimes 				 * change), the ack is the biggest we've
1895df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
1896df8bae1dSRodney W. Grimes 				 * threshhold of them, assume a packet
1897df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
1898df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
1899df8bae1dSRodney W. Grimes 				 * window so we send only this one
1900df8bae1dSRodney W. Grimes 				 * packet.
1901df8bae1dSRodney W. Grimes 				 *
1902df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
1903df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
1904df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
1905df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
1906df8bae1dSRodney W. Grimes 				 * the new ssthresh).
1907df8bae1dSRodney W. Grimes 				 *
1908df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
1909df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
1910df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
1911df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
1912df8bae1dSRodney W. Grimes 				 * network.
1913df8bae1dSRodney W. Grimes 				 */
19149b8b58e0SJonathan Lemon 				if (!callout_active(tp->tt_rexmt) ||
1915fb59c426SYoshinobu Inoue 				    th->th_ack != tp->snd_una)
1916df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
1917cb942153SJeffrey Hsu 				else if (++tp->t_dupacks > tcprexmtthresh ||
19186d90faf3SPaul Saab 					 ((tcp_do_newreno || tp->sack_enable) &&
19199d11646dSJeffrey Hsu 					  IN_FASTRECOVERY(tp))) {
192025e6f9edSPaul Saab                                         if (tp->sack_enable && IN_FASTRECOVERY(tp)) {
1921808f11b7SPaul Saab 						int awnd;
192225e6f9edSPaul Saab 
192325e6f9edSPaul Saab 						/*
192425e6f9edSPaul Saab 						 * Compute the amount of data in flight first.
192525e6f9edSPaul Saab 						 * We can inject new data into the pipe iff
192625e6f9edSPaul Saab 						 * we have less than 1/2 the original window's
192725e6f9edSPaul Saab 						 * worth of data in flight.
192825e6f9edSPaul Saab 						 */
1929808f11b7SPaul Saab 						awnd = (tp->snd_nxt - tp->snd_fack) +
19300077b016SPaul Saab 							tp->sackhint.sack_bytes_rexmit;
1931808f11b7SPaul Saab 						if (awnd < tp->snd_ssthresh) {
193225e6f9edSPaul Saab 							tp->snd_cwnd += tp->t_maxseg;
193325e6f9edSPaul Saab 							if (tp->snd_cwnd > tp->snd_ssthresh)
193425e6f9edSPaul Saab 								tp->snd_cwnd = tp->snd_ssthresh;
193525e6f9edSPaul Saab 						}
193625e6f9edSPaul Saab 					} else
193746f58482SJonathan Lemon 						tp->snd_cwnd += tp->t_maxseg;
193846f58482SJonathan Lemon 					(void) tcp_output(tp);
193946f58482SJonathan Lemon 					goto drop;
1940cb942153SJeffrey Hsu 				} else if (tp->t_dupacks == tcprexmtthresh) {
1941cb942153SJeffrey Hsu 					tcp_seq onxt = tp->snd_nxt;
1942cb942153SJeffrey Hsu 					u_int win;
1943a0445c2eSJayanth Vijayaraghavan 
1944a0445c2eSJayanth Vijayaraghavan 					/*
1945a0445c2eSJayanth Vijayaraghavan 					 * If we're doing sack, check to
1946a0445c2eSJayanth Vijayaraghavan 					 * see if we're already in sack
1947a0445c2eSJayanth Vijayaraghavan 					 * recovery. If we're not doing sack,
1948a0445c2eSJayanth Vijayaraghavan 					 * check to see if we're in newreno
1949a0445c2eSJayanth Vijayaraghavan 					 * recovery.
1950a0445c2eSJayanth Vijayaraghavan 					 */
1951a0445c2eSJayanth Vijayaraghavan 					if (tp->sack_enable) {
1952a0445c2eSJayanth Vijayaraghavan 						if (IN_FASTRECOVERY(tp)) {
1953a0445c2eSJayanth Vijayaraghavan 							tp->t_dupacks = 0;
1954a0445c2eSJayanth Vijayaraghavan 							break;
1955a0445c2eSJayanth Vijayaraghavan 						}
1956a0445c2eSJayanth Vijayaraghavan 					} else if (tcp_do_newreno) {
1957a0445c2eSJayanth Vijayaraghavan 						if (SEQ_LEQ(th->th_ack,
19589d11646dSJeffrey Hsu 						    tp->snd_recover)) {
1959cb942153SJeffrey Hsu 							tp->t_dupacks = 0;
1960cb942153SJeffrey Hsu 							break;
196146f58482SJonathan Lemon 						}
1962a0445c2eSJayanth Vijayaraghavan 					}
1963cb942153SJeffrey Hsu 					win = min(tp->snd_wnd, tp->snd_cwnd) /
1964cb942153SJeffrey Hsu 					    2 / tp->t_maxseg;
1965df8bae1dSRodney W. Grimes 					if (win < 2)
1966df8bae1dSRodney W. Grimes 						win = 2;
1967df8bae1dSRodney W. Grimes 					tp->snd_ssthresh = win * tp->t_maxseg;
19689d11646dSJeffrey Hsu 					ENTER_FASTRECOVERY(tp);
196946f58482SJonathan Lemon 					tp->snd_recover = tp->snd_max;
19709b8b58e0SJonathan Lemon 					callout_stop(tp->tt_rexmt);
19719b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
19726d90faf3SPaul Saab 					if (tp->sack_enable) {
19736d90faf3SPaul Saab 						tcpstat.tcps_sack_recovery_episode++;
1974a55db2b6SPaul Saab 						tp->sack_newdata = tp->snd_nxt;
19758db456bfSPaul Saab 						tp->snd_cwnd = tp->t_maxseg;
19766d90faf3SPaul Saab 						(void) tcp_output(tp);
19776d90faf3SPaul Saab 						goto drop;
19786d90faf3SPaul Saab 					}
1979fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
1980df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->t_maxseg;
1981df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
198248d2549cSJeffrey Hsu 					KASSERT(tp->snd_limited <= 2,
198348d2549cSJeffrey Hsu 					    ("tp->snd_limited too big"));
1984df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
198548d2549cSJeffrey Hsu 					     tp->t_maxseg *
198648d2549cSJeffrey Hsu 					     (tp->t_dupacks - tp->snd_limited);
1987df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
1988df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
1989df8bae1dSRodney W. Grimes 					goto drop;
1990582a954bSJeffrey Hsu 				} else if (tcp_do_rfc3042) {
1991582a954bSJeffrey Hsu 					u_long oldcwnd = tp->snd_cwnd;
199248d2549cSJeffrey Hsu 					tcp_seq oldsndmax = tp->snd_max;
199348d2549cSJeffrey Hsu 					u_int sent;
199489c02376SJeffrey Hsu 
1995582a954bSJeffrey Hsu 					KASSERT(tp->t_dupacks == 1 ||
1996582a954bSJeffrey Hsu 					    tp->t_dupacks == 2,
1997582a954bSJeffrey Hsu 					    ("dupacks not 1 or 2"));
199861a36e3dSJeffrey Hsu 					if (tp->t_dupacks == 1)
199948d2549cSJeffrey Hsu 						tp->snd_limited = 0;
200061a36e3dSJeffrey Hsu 					tp->snd_cwnd =
200161a36e3dSJeffrey Hsu 					    (tp->snd_nxt - tp->snd_una) +
200261a36e3dSJeffrey Hsu 					    (tp->t_dupacks - tp->snd_limited) *
200361a36e3dSJeffrey Hsu 					    tp->t_maxseg;
2004582a954bSJeffrey Hsu 					(void) tcp_output(tp);
200548d2549cSJeffrey Hsu 					sent = tp->snd_max - oldsndmax;
200648d2549cSJeffrey Hsu 					if (sent > tp->t_maxseg) {
200789c02376SJeffrey Hsu 						KASSERT((tp->t_dupacks == 2 &&
200889c02376SJeffrey Hsu 						    tp->snd_limited == 0) ||
200989c02376SJeffrey Hsu 						   (sent == tp->t_maxseg + 1 &&
201089c02376SJeffrey Hsu 						    tp->t_flags & TF_SENTFIN),
201148d2549cSJeffrey Hsu 						    ("sent too much"));
201248d2549cSJeffrey Hsu 						tp->snd_limited = 2;
201348d2549cSJeffrey Hsu 					} else if (sent > 0)
201448d2549cSJeffrey Hsu 						++tp->snd_limited;
2015582a954bSJeffrey Hsu 					tp->snd_cwnd = oldcwnd;
2016582a954bSJeffrey Hsu 					goto drop;
2017df8bae1dSRodney W. Grimes 				}
2018df8bae1dSRodney W. Grimes 			} else
2019df8bae1dSRodney W. Grimes 				tp->t_dupacks = 0;
2020df8bae1dSRodney W. Grimes 			break;
2021df8bae1dSRodney W. Grimes 		}
2022cb942153SJeffrey Hsu 
2023cb942153SJeffrey Hsu 		KASSERT(SEQ_GT(th->th_ack, tp->snd_una), ("th_ack <= snd_una"));
2024cb942153SJeffrey Hsu 
2025df8bae1dSRodney W. Grimes 		/*
2026df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
2027df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
2028df8bae1dSRodney W. Grimes 		 */
20296d90faf3SPaul Saab 		if (tcp_do_newreno || tp->sack_enable) {
20309d11646dSJeffrey Hsu 			if (IN_FASTRECOVERY(tp)) {
2031cb942153SJeffrey Hsu 				if (SEQ_LT(th->th_ack, tp->snd_recover)) {
20326d90faf3SPaul Saab 					if (tp->sack_enable)
20336d90faf3SPaul Saab 						tcp_sack_partialack(tp, th);
20346d90faf3SPaul Saab 					else
2035c068736aSJeffrey Hsu 						tcp_newreno_partial_ack(tp, th);
2036c068736aSJeffrey Hsu 				} else {
2037c068736aSJeffrey Hsu 					/*
20386d90faf3SPaul Saab 					 * Out of fast recovery.
2039c068736aSJeffrey Hsu 					 * Window inflation should have left us
2040c068736aSJeffrey Hsu 					 * with approximately snd_ssthresh
2041c068736aSJeffrey Hsu 					 * outstanding data.
2042c068736aSJeffrey Hsu 					 * But in case we would be inclined to
2043c068736aSJeffrey Hsu 					 * send a burst, better to do it via
2044c068736aSJeffrey Hsu 					 * the slow start mechanism.
2045c068736aSJeffrey Hsu 					 */
2046c068736aSJeffrey Hsu 					if (SEQ_GT(th->th_ack +
2047c068736aSJeffrey Hsu 							tp->snd_ssthresh,
2048c068736aSJeffrey Hsu 						   tp->snd_max))
2049c068736aSJeffrey Hsu 						tp->snd_cwnd = tp->snd_max -
2050c068736aSJeffrey Hsu 								th->th_ack +
2051c068736aSJeffrey Hsu 								tp->t_maxseg;
2052c068736aSJeffrey Hsu 					else
2053c068736aSJeffrey Hsu 						tp->snd_cwnd = tp->snd_ssthresh;
2054c068736aSJeffrey Hsu 				}
2055c068736aSJeffrey Hsu 			}
2056c068736aSJeffrey Hsu 		} else {
2057233e8c18SGarrett Wollman 			if (tp->t_dupacks >= tcprexmtthresh &&
2058df8bae1dSRodney W. Grimes 			    tp->snd_cwnd > tp->snd_ssthresh)
2059df8bae1dSRodney W. Grimes 				tp->snd_cwnd = tp->snd_ssthresh;
206046f58482SJonathan Lemon 		}
2061cb942153SJeffrey Hsu 		tp->t_dupacks = 0;
2062a0292f23SGarrett Wollman 		/*
2063a0292f23SGarrett Wollman 		 * If we reach this point, ACK is not a duplicate,
2064a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
2065a0292f23SGarrett Wollman 		 */
2066a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
2067a0292f23SGarrett Wollman 			/*
2068a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
2069a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
207007e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
207107e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
207207e43e10SAndras Olah 			 * we can do window scaling.
2073a0292f23SGarrett Wollman 			 */
2074a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
2075a0292f23SGarrett Wollman 			tp->snd_una++;
207607e43e10SAndras Olah 			/* Do window scaling? */
207707e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
207807e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
207907e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2080464fcfbcSAndre Oppermann 				/* Send window already scaled. */
208107e43e10SAndras Olah 			}
2082a0292f23SGarrett Wollman 		}
2083a0292f23SGarrett Wollman 
2084a0292f23SGarrett Wollman process_ACK:
2085de30ea13SRobert Watson 		KASSERT(headlocked, ("tcp_input: process_ACK: head not "
2086de30ea13SRobert Watson 		    "locked"));
20877cfc6904SRobert Watson 		INP_LOCK_ASSERT(inp);
20887cfc6904SRobert Watson 
2089fb59c426SYoshinobu Inoue 		acked = th->th_ack - tp->snd_una;
2090df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackpack++;
2091df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackbyte += acked;
2092df8bae1dSRodney W. Grimes 
2093df8bae1dSRodney W. Grimes 		/*
20949b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
20959b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
20969b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
20979b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
20989b8b58e0SJonathan Lemon 		 * we left off.
20999b8b58e0SJonathan Lemon 		 */
21009b8b58e0SJonathan Lemon 		if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) {
2101d65bf08aSMatthew Dillon 			++tcpstat.tcps_sndrexmitbad;
21029b8b58e0SJonathan Lemon 			tp->snd_cwnd = tp->snd_cwnd_prev;
21039b8b58e0SJonathan Lemon 			tp->snd_ssthresh = tp->snd_ssthresh_prev;
21049d11646dSJeffrey Hsu 			tp->snd_recover = tp->snd_recover_prev;
21059d11646dSJeffrey Hsu 			if (tp->t_flags & TF_WASFRECOVERY)
21069d11646dSJeffrey Hsu 				ENTER_FASTRECOVERY(tp);
21079b8b58e0SJonathan Lemon 			tp->snd_nxt = tp->snd_max;
21089b8b58e0SJonathan Lemon 			tp->t_badrxtwin = 0;	/* XXX probably not required */
21099b8b58e0SJonathan Lemon 		}
21109b8b58e0SJonathan Lemon 
21119b8b58e0SJonathan Lemon 		/*
2112df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
2113df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
2114df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
2115df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
2116df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
2117df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
2118df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
2119fa55172bSMatthew Dillon 		 *
2120fa55172bSMatthew Dillon 		 * Some boxes send broken timestamp replies
2121fa55172bSMatthew Dillon 		 * during the SYN+ACK phase, ignore
2122fa55172bSMatthew Dillon 		 * timestamps of 0 or we could calculate a
2123fa55172bSMatthew Dillon 		 * huge RTT and blow up the retransmit timer.
2124df8bae1dSRodney W. Grimes 		 */
2125fa55172bSMatthew Dillon 		if ((to.to_flags & TOF_TS) != 0 &&
2126fa55172bSMatthew Dillon 		    to.to_tsecr) {
2127eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - to.to_tsecr)
2128eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - to.to_tsecr;
21299b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - to.to_tsecr + 1);
2130fa55172bSMatthew Dillon 		} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2131eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
2132eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - tp->t_rtttime;
21339b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2134fa55172bSMatthew Dillon 		}
21351fcc99b5SMatthew Dillon 		tcp_xmit_bandwidth_limit(tp, th->th_ack);
2136df8bae1dSRodney W. Grimes 
2137df8bae1dSRodney W. Grimes 		/*
2138df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
2139df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
2140df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
2141df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
2142df8bae1dSRodney W. Grimes 		 */
2143fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
21449b8b58e0SJonathan Lemon 			callout_stop(tp->tt_rexmt);
2145df8bae1dSRodney W. Grimes 			needoutput = 1;
21469b8b58e0SJonathan Lemon 		} else if (!callout_active(tp->tt_persist))
21479b8b58e0SJonathan Lemon 			callout_reset(tp->tt_rexmt, tp->t_rxtcur,
21489b8b58e0SJonathan Lemon 				      tcp_timer_rexmt, tp);
2149a0292f23SGarrett Wollman 
2150a0292f23SGarrett Wollman 		/*
2151a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
2152a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
2153a0292f23SGarrett Wollman 		 */
2154a0292f23SGarrett Wollman 		if (acked == 0)
2155a0292f23SGarrett Wollman 			goto step6;
2156a0292f23SGarrett Wollman 
2157df8bae1dSRodney W. Grimes 		/*
2158df8bae1dSRodney W. Grimes 		 * When new data is acked, open the congestion window.
2159df8bae1dSRodney W. Grimes 		 * If the window gives us less than ssthresh packets
2160df8bae1dSRodney W. Grimes 		 * in flight, open exponentially (maxseg per packet).
2161df8bae1dSRodney W. Grimes 		 * Otherwise open linearly: maxseg per window
216210be5648SGarrett Wollman 		 * (maxseg^2 / cwnd per packet).
2163df8bae1dSRodney W. Grimes 		 */
21646d90faf3SPaul Saab 		if ((!tcp_do_newreno && !tp->sack_enable) ||
21656d90faf3SPaul Saab 		    !IN_FASTRECOVERY(tp)) {
2166df8bae1dSRodney W. Grimes 			register u_int cw = tp->snd_cwnd;
2167df8bae1dSRodney W. Grimes 			register u_int incr = tp->t_maxseg;
2168df8bae1dSRodney W. Grimes 			if (cw > tp->snd_ssthresh)
216910be5648SGarrett Wollman 				incr = incr * incr / cw;
2170df8bae1dSRodney W. Grimes 			tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale);
2171df8bae1dSRodney W. Grimes 		}
21725905999bSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
2173df8bae1dSRodney W. Grimes 		if (acked > so->so_snd.sb_cc) {
2174df8bae1dSRodney W. Grimes 			tp->snd_wnd -= so->so_snd.sb_cc;
21755905999bSRobert Watson 			sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc);
2176df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
2177df8bae1dSRodney W. Grimes 		} else {
21785905999bSRobert Watson 			sbdrop_locked(&so->so_snd, acked);
2179df8bae1dSRodney W. Grimes 			tp->snd_wnd -= acked;
2180df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
2181df8bae1dSRodney W. Grimes 		}
21821e4d7da7SRobert Watson 		sowwakeup_locked(so);
2183cb942153SJeffrey Hsu 		/* detect una wraparound */
21846d90faf3SPaul Saab 		if ((tcp_do_newreno || tp->sack_enable) &&
21856d90faf3SPaul Saab 		    !IN_FASTRECOVERY(tp) &&
21869d11646dSJeffrey Hsu 		    SEQ_GT(tp->snd_una, tp->snd_recover) &&
21879d11646dSJeffrey Hsu 		    SEQ_LEQ(th->th_ack, tp->snd_recover))
21889d11646dSJeffrey Hsu 			tp->snd_recover = th->th_ack - 1;
21896d90faf3SPaul Saab 		if ((tcp_do_newreno || tp->sack_enable) &&
21906d90faf3SPaul Saab 		    IN_FASTRECOVERY(tp) &&
21919d11646dSJeffrey Hsu 		    SEQ_GEQ(th->th_ack, tp->snd_recover))
21929d11646dSJeffrey Hsu 			EXIT_FASTRECOVERY(tp);
2193fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
21946d90faf3SPaul Saab 		if (tp->sack_enable) {
21956d90faf3SPaul Saab 			if (SEQ_GT(tp->snd_una, tp->snd_recover))
21966d90faf3SPaul Saab 				tp->snd_recover = tp->snd_una;
21976d90faf3SPaul Saab 		}
2198df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2199df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
2200df8bae1dSRodney W. Grimes 
2201df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2202df8bae1dSRodney W. Grimes 
2203df8bae1dSRodney W. Grimes 		/*
2204df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
2205df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
2206df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
2207df8bae1dSRodney W. Grimes 		 */
2208df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2209df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2210df8bae1dSRodney W. Grimes 				/*
2211df8bae1dSRodney W. Grimes 				 * If we can't receive any more
2212df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
2213df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
2214df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
2215df8bae1dSRodney W. Grimes 				 * we'll hang forever.
2216df8bae1dSRodney W. Grimes 				 */
2217340c35deSJonathan Lemon 		/* XXXjl
2218340c35deSJonathan Lemon 		 * we should release the tp also, and use a
2219340c35deSJonathan Lemon 		 * compressed state.
2220340c35deSJonathan Lemon 		 */
2221c0b99ffaSRobert Watson 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
222203e49181SSeigo Tanimura 					soisdisconnected(so);
22239b8b58e0SJonathan Lemon 					callout_reset(tp->tt_2msl, tcp_maxidle,
22249b8b58e0SJonathan Lemon 						      tcp_timer_2msl, tp);
22254cc20ab1SSeigo Tanimura 				}
2226df8bae1dSRodney W. Grimes 				tp->t_state = TCPS_FIN_WAIT_2;
2227df8bae1dSRodney W. Grimes 			}
2228df8bae1dSRodney W. Grimes 			break;
2229df8bae1dSRodney W. Grimes 
2230df8bae1dSRodney W. Grimes 		/*
2231df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
2232df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
2233df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
2234df8bae1dSRodney W. Grimes 		 * the segment.
2235df8bae1dSRodney W. Grimes 		 */
2236df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
2237df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2238de30ea13SRobert Watson 				KASSERT(headlocked, ("tcp_input: process_ACK: "
2239de30ea13SRobert Watson 				    "head not locked"));
224011a20fb8SJeffrey Hsu 				tcp_twstart(tp);
2241340c35deSJonathan Lemon 				INP_INFO_WUNLOCK(&tcbinfo);
2242340c35deSJonathan Lemon 				m_freem(m);
2243340c35deSJonathan Lemon 				return;
2244df8bae1dSRodney W. Grimes 			}
2245df8bae1dSRodney W. Grimes 			break;
2246df8bae1dSRodney W. Grimes 
2247df8bae1dSRodney W. Grimes 		/*
2248df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
2249df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
2250df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
2251df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
2252df8bae1dSRodney W. Grimes 		 */
2253df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
2254df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2255416738a7SRobert Watson 				KASSERT(headlocked, ("tcp_input: process_ACK:"
2256416738a7SRobert Watson 				    " tcp_close: head not locked"));
2257df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
2258df8bae1dSRodney W. Grimes 				goto drop;
2259df8bae1dSRodney W. Grimes 			}
2260df8bae1dSRodney W. Grimes 			break;
2261df8bae1dSRodney W. Grimes 
2262df8bae1dSRodney W. Grimes 		/*
2263df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state the only thing that should arrive
2264df8bae1dSRodney W. Grimes 		 * is a retransmission of the remote FIN.  Acknowledge
2265df8bae1dSRodney W. Grimes 		 * it and restart the finack timer.
2266df8bae1dSRodney W. Grimes 		 */
2267df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
2268340c35deSJonathan Lemon 			KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
22699b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, 2 * tcp_msl,
22709b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
2271df8bae1dSRodney W. Grimes 			goto dropafterack;
2272df8bae1dSRodney W. Grimes 		}
2273df8bae1dSRodney W. Grimes 	}
2274df8bae1dSRodney W. Grimes 
2275df8bae1dSRodney W. Grimes step6:
2276de30ea13SRobert Watson 	KASSERT(headlocked, ("tcp_input: step6: head not locked"));
22777cfc6904SRobert Watson 	INP_LOCK_ASSERT(inp);
22787cfc6904SRobert Watson 
2279df8bae1dSRodney W. Grimes 	/*
2280df8bae1dSRodney W. Grimes 	 * Update window information.
2281df8bae1dSRodney W. Grimes 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2282df8bae1dSRodney W. Grimes 	 */
2283fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) &&
2284fb59c426SYoshinobu Inoue 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
2285fb59c426SYoshinobu Inoue 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
2286fb59c426SYoshinobu Inoue 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
2287df8bae1dSRodney W. Grimes 		/* keep track of pure window updates */
2288fb59c426SYoshinobu Inoue 		if (tlen == 0 &&
2289fb59c426SYoshinobu Inoue 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
2290df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvwinupd++;
2291df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
2292fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq;
2293fb59c426SYoshinobu Inoue 		tp->snd_wl2 = th->th_ack;
2294df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
2295df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
2296df8bae1dSRodney W. Grimes 		needoutput = 1;
2297df8bae1dSRodney W. Grimes 	}
2298df8bae1dSRodney W. Grimes 
2299df8bae1dSRodney W. Grimes 	/*
2300df8bae1dSRodney W. Grimes 	 * Process segments with URG.
2301df8bae1dSRodney W. Grimes 	 */
2302fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
2303df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2304df8bae1dSRodney W. Grimes 		/*
2305df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
2306df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
2307df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
2308df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
2309df8bae1dSRodney W. Grimes 		 */
231018ad5842SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
2311fb59c426SYoshinobu Inoue 		if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
2312fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
2313fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
231418ad5842SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);	/* XXX */
2315df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
2316df8bae1dSRodney W. Grimes 		}
2317df8bae1dSRodney W. Grimes 		/*
2318df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
2319df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
2320df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2321df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
2322df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
2323df8bae1dSRodney W. Grimes 		 *
2324df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
2325df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
2326df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
2327df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
2328df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
2329df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
2330df8bae1dSRodney W. Grimes 		 */
2331fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2332fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
2333df8bae1dSRodney W. Grimes 			so->so_oobmark = so->so_rcv.sb_cc +
2334df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
2335927c5ceaSRobert Watson 			if (so->so_oobmark == 0)
2336c0b99ffaSRobert Watson 				so->so_rcv.sb_state |= SBS_RCVATMARK;
2337df8bae1dSRodney W. Grimes 			sohasoutofband(so);
2338df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2339df8bae1dSRodney W. Grimes 		}
234018ad5842SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
2341df8bae1dSRodney W. Grimes 		/*
2342df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
2343df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
2344df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
2345df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
2346df8bae1dSRodney W. Grimes 		 */
234730613f56SJeffrey Hsu 		if (th->th_urp <= (u_long)tlen &&
234830613f56SJeffrey Hsu 		    !(so->so_options & SO_OOBINLINE)) {
234930613f56SJeffrey Hsu 			/* hdr drop is delayed */
235030613f56SJeffrey Hsu 			tcp_pulloutofband(so, th, m, drop_hdrlen);
235130613f56SJeffrey Hsu 		}
2352c068736aSJeffrey Hsu 	} else {
2353df8bae1dSRodney W. Grimes 		/*
2354df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
2355df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
2356df8bae1dSRodney W. Grimes 		 * with the receive window.
2357df8bae1dSRodney W. Grimes 		 */
2358df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2359df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
2360c068736aSJeffrey Hsu 	}
2361df8bae1dSRodney W. Grimes dodata:							/* XXX */
2362de30ea13SRobert Watson 	KASSERT(headlocked, ("tcp_input: dodata: head not locked"));
23637cfc6904SRobert Watson 	INP_LOCK_ASSERT(inp);
23647cfc6904SRobert Watson 
2365df8bae1dSRodney W. Grimes 	/*
2366df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
2367df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
2368df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
2369df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
2370df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
2371df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
2372df8bae1dSRodney W. Grimes 	 */
2373fb59c426SYoshinobu Inoue 	if ((tlen || (thflags & TH_FIN)) &&
2374df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
237569e03620SPaul Saab 		tcp_seq save_start = th->th_seq;
237669e03620SPaul Saab 		tcp_seq save_end = th->th_seq + tlen;
2377fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
2378e4b64281SJesper Skriver 		/*
2379c068736aSJeffrey Hsu 		 * Insert segment which includes th into TCP reassembly queue
2380c068736aSJeffrey Hsu 		 * with control block tp.  Set thflags to whether reassembly now
2381c068736aSJeffrey Hsu 		 * includes a segment with FIN.  This handles the common case
2382c068736aSJeffrey Hsu 		 * inline (segment is the next to be received on an established
2383c068736aSJeffrey Hsu 		 * connection, and the queue is empty), avoiding linkage into
2384c068736aSJeffrey Hsu 		 * and removal from the queue and repetition of various
2385c068736aSJeffrey Hsu 		 * conversions.
2386c068736aSJeffrey Hsu 		 * Set DELACK for segments received in order, but ack
2387c068736aSJeffrey Hsu 		 * immediately when segments are out of order (so
2388c068736aSJeffrey Hsu 		 * fast retransmit can work).
2389e4b64281SJesper Skriver 		 */
2390e4b64281SJesper Skriver 		if (th->th_seq == tp->rcv_nxt &&
2391e4b64281SJesper Skriver 		    LIST_EMPTY(&tp->t_segq) &&
2392e4b64281SJesper Skriver 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
2393e4b64281SJesper Skriver 			if (DELAY_ACK(tp))
23943bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
2395e4b64281SJesper Skriver 			else
2396e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
2397e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
2398e4b64281SJesper Skriver 			thflags = th->th_flags & TH_FIN;
2399e4b64281SJesper Skriver 			tcpstat.tcps_rcvpack++;
2400e4b64281SJesper Skriver 			tcpstat.tcps_rcvbyte += tlen;
2401e4b64281SJesper Skriver 			ND6_HINT(tp);
24021e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
2403c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
2404c1c36a2cSMike Silbersack 				m_freem(m);
2405c1c36a2cSMike Silbersack 			else
24061e4d7da7SRobert Watson 				sbappendstream_locked(&so->so_rcv, m);
24071e4d7da7SRobert Watson 			sorwakeup_locked(so);
2408e4b64281SJesper Skriver 		} else {
2409e4b64281SJesper Skriver 			thflags = tcp_reass(tp, th, &tlen, m);
2410e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
2411e4b64281SJesper Skriver 		}
2412e346eeffSPaul Saab 		if (tlen > 0 && tp->sack_enable)
241369e03620SPaul Saab 			tcp_update_sack_list(tp, save_start, save_end);
2414df8bae1dSRodney W. Grimes 		/*
2415df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
2416df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
2417df8bae1dSRodney W. Grimes 		 * buffer size.
2418df8bae1dSRodney W. Grimes 		 */
2419df8bae1dSRodney W. Grimes 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2420df8bae1dSRodney W. Grimes 	} else {
2421df8bae1dSRodney W. Grimes 		m_freem(m);
2422fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
2423df8bae1dSRodney W. Grimes 	}
2424df8bae1dSRodney W. Grimes 
2425df8bae1dSRodney W. Grimes 	/*
2426df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
2427df8bae1dSRodney W. Grimes 	 * that the connection is closing.
2428df8bae1dSRodney W. Grimes 	 */
2429fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
2430df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2431df8bae1dSRodney W. Grimes 			socantrcvmore(so);
2432a0292f23SGarrett Wollman 			/*
2433a0292f23SGarrett Wollman 			 * If connection is half-synchronized
2434d3eede9dSAndras Olah 			 * (ie NEEDSYN flag on) then delay ACK,
2435a0292f23SGarrett Wollman 			 * so it may be piggybacked when SYN is sent.
2436a0292f23SGarrett Wollman 			 * Otherwise, since we received a FIN then no
2437a0292f23SGarrett Wollman 			 * more input can be expected, send ACK now.
2438a0292f23SGarrett Wollman 			 */
24393bfd6421SJonathan Lemon 			if (tp->t_flags & TF_NEEDSYN)
24403bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
2441a0292f23SGarrett Wollman 			else
2442df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
2443df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
2444df8bae1dSRodney W. Grimes 		}
2445df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2446df8bae1dSRodney W. Grimes 
2447df8bae1dSRodney W. Grimes 		/*
2448df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
2449df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
2450df8bae1dSRodney W. Grimes 		 */
2451df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
24529b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
24539b8b58e0SJonathan Lemon 			/*FALLTHROUGH*/
2454df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
2455df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSE_WAIT;
2456df8bae1dSRodney W. Grimes 			break;
2457df8bae1dSRodney W. Grimes 
2458df8bae1dSRodney W. Grimes 		/*
2459df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2460df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
2461df8bae1dSRodney W. Grimes 		 */
2462df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2463df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSING;
2464df8bae1dSRodney W. Grimes 			break;
2465df8bae1dSRodney W. Grimes 
2466df8bae1dSRodney W. Grimes 		/*
2467df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2468df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
2469df8bae1dSRodney W. Grimes 		 * standard timers.
2470df8bae1dSRodney W. Grimes 		 */
2471df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
2472de30ea13SRobert Watson 			KASSERT(headlocked == 1, ("tcp_input: dodata: "
2473de30ea13SRobert Watson 			    "TCP_FIN_WAIT_2: head not locked"));
2474340c35deSJonathan Lemon 			tcp_twstart(tp);
247511a20fb8SJeffrey Hsu 			INP_INFO_WUNLOCK(&tcbinfo);
2476340c35deSJonathan Lemon 			return;
2477df8bae1dSRodney W. Grimes 
2478df8bae1dSRodney W. Grimes 		/*
2479df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
2480df8bae1dSRodney W. Grimes 		 */
2481df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
2482340c35deSJonathan Lemon 			KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
24839b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, 2 * tcp_msl,
24849b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
2485df8bae1dSRodney W. Grimes 			break;
2486df8bae1dSRodney W. Grimes 		}
2487df8bae1dSRodney W. Grimes 	}
2488e0bef1cbSRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
2489e0bef1cbSRobert Watson 	headlocked = 0;
2490610ee2f9SDavid Greenman #ifdef TCPDEBUG
24914cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
2492fb59c426SYoshinobu Inoue 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
2493fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2494610ee2f9SDavid Greenman #endif
2495df8bae1dSRodney W. Grimes 
2496df8bae1dSRodney W. Grimes 	/*
2497df8bae1dSRodney W. Grimes 	 * Return any desired output.
2498df8bae1dSRodney W. Grimes 	 */
2499df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
2500df8bae1dSRodney W. Grimes 		(void) tcp_output(tp);
25017792ea27SJeffrey Hsu 
2502a14c749fSJonathan Lemon check_delack:
2503e0bef1cbSRobert Watson 	KASSERT(headlocked == 0, ("tcp_input: check_delack: head locked"));
25047cfc6904SRobert Watson 	INP_LOCK_ASSERT(inp);
2505a14c749fSJonathan Lemon 	if (tp->t_flags & TF_DELACK) {
25063bfd6421SJonathan Lemon 		tp->t_flags &= ~TF_DELACK;
25073bfd6421SJonathan Lemon 		callout_reset(tp->tt_delack, tcp_delacktime,
25083bfd6421SJonathan Lemon 		    tcp_timer_delack, tp);
25093bfd6421SJonathan Lemon 	}
2510f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
2511df8bae1dSRodney W. Grimes 	return;
2512df8bae1dSRodney W. Grimes 
2513df8bae1dSRodney W. Grimes dropafterack:
2514de30ea13SRobert Watson 	KASSERT(headlocked, ("tcp_input: dropafterack: head not locked"));
2515df8bae1dSRodney W. Grimes 	/*
2516df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
2517df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
251880ab7c0eSGarrett Wollman 	 *
251980ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
252080ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
252180ab7c0eSGarrett Wollman 	 * RST have been dropped.
252280ab7c0eSGarrett Wollman 	 *
252380ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
252480ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
252580ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
252680ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
252780ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
252880ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
2529df8bae1dSRodney W. Grimes 	 */
2530fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
2531fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
2532a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
2533a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2534a57815efSBosko Milekic 		goto dropwithreset;
2535a57815efSBosko Milekic 	}
2536a0292f23SGarrett Wollman #ifdef TCPDEBUG
25374cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
2538fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2539fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2540a0292f23SGarrett Wollman #endif
254185e8b243SJeffrey Hsu 	KASSERT(headlocked, ("headlocked should be 1"));
254242cf3289SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
2543df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
2544df8bae1dSRodney W. Grimes 	(void) tcp_output(tp);
2545f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
2546d6915262SRobert Watson 	m_freem(m);
2547df8bae1dSRodney W. Grimes 	return;
2548df8bae1dSRodney W. Grimes 
2549df8bae1dSRodney W. Grimes dropwithreset:
2550de30ea13SRobert Watson 	KASSERT(headlocked, ("tcp_input: dropwithreset: head not locked"));
2551df8bae1dSRodney W. Grimes 	/*
2552df8bae1dSRodney W. Grimes 	 * Generate a RST, dropping incoming segment.
2553df8bae1dSRodney W. Grimes 	 * Make ACK acceptable to originator of segment.
2554df8bae1dSRodney W. Grimes 	 * Don't bother to respond if destination was broadcast/multicast.
2555df8bae1dSRodney W. Grimes 	 */
2556fb59c426SYoshinobu Inoue 	if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
2557df8bae1dSRodney W. Grimes 		goto drop;
2558fb59c426SYoshinobu Inoue 	if (isipv6) {
2559173c0f9fSWarner Losh 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
2560173c0f9fSWarner Losh 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
2561fb59c426SYoshinobu Inoue 			goto drop;
2562c068736aSJeffrey Hsu 	} else {
2563173c0f9fSWarner Losh 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
2564173c0f9fSWarner Losh 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
25652ca2159fSCrist J. Clark 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
25662ca2159fSCrist J. Clark 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
2567fb59c426SYoshinobu Inoue 			goto drop;
2568c068736aSJeffrey Hsu 	}
2569fb59c426SYoshinobu Inoue 	/* IPv6 anycast check is done at tcp6_input() */
2570a57815efSBosko Milekic 
2571a57815efSBosko Milekic 	/*
2572c59319bfSDag-Erling Smørgrav 	 * Perform bandwidth limiting.
2573a57815efSBosko Milekic 	 */
2574a57815efSBosko Milekic 	if (badport_bandlim(rstreason) < 0)
2575a57815efSBosko Milekic 		goto drop;
2576a57815efSBosko Milekic 
2577a0292f23SGarrett Wollman #ifdef TCPDEBUG
25784cc20ab1SSeigo Tanimura 	if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2579fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2580fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2581a0292f23SGarrett Wollman #endif
25826fd22cafSJeffrey Hsu 
2583fb59c426SYoshinobu Inoue 	if (thflags & TH_ACK)
2584fb59c426SYoshinobu Inoue 		/* mtod() below is safe as long as hdr dropping is delayed */
2585fb59c426SYoshinobu Inoue 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack,
2586fb59c426SYoshinobu Inoue 			    TH_RST);
2587df8bae1dSRodney W. Grimes 	else {
2588fb59c426SYoshinobu Inoue 		if (thflags & TH_SYN)
2589fb59c426SYoshinobu Inoue 			tlen++;
2590fb59c426SYoshinobu Inoue 		/* mtod() below is safe as long as hdr dropping is delayed */
2591fb59c426SYoshinobu Inoue 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
2592fb59c426SYoshinobu Inoue 			    (tcp_seq)0, TH_RST|TH_ACK);
2593df8bae1dSRodney W. Grimes 	}
2594c29afad6SSam Leffler 
25951c53f806SRobert Watson 	if (tp != NULL)
2596c29afad6SSam Leffler 		INP_UNLOCK(inp);
2597f76fcf6dSJeffrey Hsu 	if (headlocked)
2598f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&tcbinfo);
2599df8bae1dSRodney W. Grimes 	return;
2600df8bae1dSRodney W. Grimes 
2601df8bae1dSRodney W. Grimes drop:
2602df8bae1dSRodney W. Grimes 	/*
2603df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
2604df8bae1dSRodney W. Grimes 	 */
2605610ee2f9SDavid Greenman #ifdef TCPDEBUG
26061c53f806SRobert Watson 	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2607fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2608fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2609a0292f23SGarrett Wollman #endif
26101c53f806SRobert Watson 	if (tp != NULL)
2611f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
2612f76fcf6dSJeffrey Hsu 	if (headlocked)
2613f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&tcbinfo);
2614d6915262SRobert Watson 	m_freem(m);
2615df8bae1dSRodney W. Grimes 	return;
2616df8bae1dSRodney W. Grimes }
2617df8bae1dSRodney W. Grimes 
2618be2ac88cSJonathan Lemon /*
2619be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
2620be2ac88cSJonathan Lemon  */
26210312fbe9SPoul-Henning Kamp static void
2622f72167f4SAndre Oppermann tcp_dooptions(to, cp, cnt, flags)
2623be2ac88cSJonathan Lemon 	struct tcpopt *to;
2624df8bae1dSRodney W. Grimes 	u_char *cp;
2625df8bae1dSRodney W. Grimes 	int cnt;
2626f72167f4SAndre Oppermann 	int flags;
2627df8bae1dSRodney W. Grimes {
2628df8bae1dSRodney W. Grimes 	int opt, optlen;
2629df8bae1dSRodney W. Grimes 
2630be2ac88cSJonathan Lemon 	to->to_flags = 0;
2631df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
2632df8bae1dSRodney W. Grimes 		opt = cp[0];
2633df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
2634df8bae1dSRodney W. Grimes 			break;
2635df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
2636df8bae1dSRodney W. Grimes 			optlen = 1;
2637df8bae1dSRodney W. Grimes 		else {
2638b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
2639b474779fSJun-ichiro itojun Hagino 				break;
2640df8bae1dSRodney W. Grimes 			optlen = cp[1];
2641b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
2642df8bae1dSRodney W. Grimes 				break;
2643df8bae1dSRodney W. Grimes 		}
2644df8bae1dSRodney W. Grimes 		switch (opt) {
2645df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
2646df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
2647df8bae1dSRodney W. Grimes 				continue;
2648f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
2649df8bae1dSRodney W. Grimes 				continue;
2650be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
2651be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
2652be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
2653fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
2654df8bae1dSRodney W. Grimes 			break;
2655df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
2656df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
2657df8bae1dSRodney W. Grimes 				continue;
2658f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
2659df8bae1dSRodney W. Grimes 				continue;
2660be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
2661be2ac88cSJonathan Lemon 			to->to_requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
2662df8bae1dSRodney W. Grimes 			break;
2663df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
2664df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
2665df8bae1dSRodney W. Grimes 				continue;
2666be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
2667a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2668a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
2669fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
2670a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
2671a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
2672fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
2673df8bae1dSRodney W. Grimes 			break;
26741cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
26751cfd4b53SBruce M Simpson 		/*
26761cfd4b53SBruce M Simpson 		 * XXX In order to reply to a host which has set the
26771cfd4b53SBruce M Simpson 		 * TCP_SIGNATURE option in its initial SYN, we have to
26781cfd4b53SBruce M Simpson 		 * record the fact that the option was observed here
26791cfd4b53SBruce M Simpson 		 * for the syncache code to perform the correct response.
26801cfd4b53SBruce M Simpson 		 */
26811cfd4b53SBruce M Simpson 		case TCPOPT_SIGNATURE:
26821cfd4b53SBruce M Simpson 			if (optlen != TCPOLEN_SIGNATURE)
26831cfd4b53SBruce M Simpson 				continue;
26841cfd4b53SBruce M Simpson 			to->to_flags |= (TOF_SIGNATURE | TOF_SIGLEN);
26851cfd4b53SBruce M Simpson 			break;
2686265ed012SBruce M Simpson #endif
26876d90faf3SPaul Saab 		case TCPOPT_SACK_PERMITTED:
2688f72167f4SAndre Oppermann 			if (optlen != TCPOLEN_SACK_PERMITTED)
26896d90faf3SPaul Saab 				continue;
2690f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
2691f72167f4SAndre Oppermann 				continue;
2692f72167f4SAndre Oppermann 			if (!tcp_do_sack)
2693f72167f4SAndre Oppermann 				continue;
26946d90faf3SPaul Saab 			to->to_flags |= TOF_SACK;
26956d90faf3SPaul Saab 			break;
26966d90faf3SPaul Saab 		case TCPOPT_SACK:
26975a53ca16SPaul Saab 			if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
26986d90faf3SPaul Saab 				continue;
26995a53ca16SPaul Saab 			to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
27005a53ca16SPaul Saab 			to->to_sacks = cp + 2;
27015a53ca16SPaul Saab 			tcpstat.tcps_sack_rcv_blocks++;
27026d90faf3SPaul Saab 			break;
2703be2ac88cSJonathan Lemon 		default:
2704be2ac88cSJonathan Lemon 			continue;
2705df8bae1dSRodney W. Grimes 		}
2706df8bae1dSRodney W. Grimes 	}
2707df8bae1dSRodney W. Grimes }
2708df8bae1dSRodney W. Grimes 
2709df8bae1dSRodney W. Grimes /*
2710df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
2711df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
2712df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
2713df8bae1dSRodney W. Grimes  * sequencing purposes.
2714df8bae1dSRodney W. Grimes  */
27150312fbe9SPoul-Henning Kamp static void
2716fb59c426SYoshinobu Inoue tcp_pulloutofband(so, th, m, off)
2717df8bae1dSRodney W. Grimes 	struct socket *so;
2718fb59c426SYoshinobu Inoue 	struct tcphdr *th;
2719df8bae1dSRodney W. Grimes 	register struct mbuf *m;
2720fb59c426SYoshinobu Inoue 	int off;		/* delayed to be droped hdrlen */
2721df8bae1dSRodney W. Grimes {
2722fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
2723df8bae1dSRodney W. Grimes 
2724df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
2725df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
2726df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
2727df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
2728df8bae1dSRodney W. Grimes 
2729df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
2730df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
2731df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
2732df8bae1dSRodney W. Grimes 			m->m_len--;
273369a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
273469a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
2735df8bae1dSRodney W. Grimes 			return;
2736df8bae1dSRodney W. Grimes 		}
2737df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
2738df8bae1dSRodney W. Grimes 		m = m->m_next;
2739df8bae1dSRodney W. Grimes 		if (m == 0)
2740df8bae1dSRodney W. Grimes 			break;
2741df8bae1dSRodney W. Grimes 	}
2742df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
2743df8bae1dSRodney W. Grimes }
2744df8bae1dSRodney W. Grimes 
2745df8bae1dSRodney W. Grimes /*
2746df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
2747df8bae1dSRodney W. Grimes  * and update averages and current timeout.
2748df8bae1dSRodney W. Grimes  */
27490312fbe9SPoul-Henning Kamp static void
2750df8bae1dSRodney W. Grimes tcp_xmit_timer(tp, rtt)
2751df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
27529b8b58e0SJonathan Lemon 	int rtt;
2753df8bae1dSRodney W. Grimes {
2754233e8c18SGarrett Wollman 	register int delta;
2755233e8c18SGarrett Wollman 
27562be3bf22SRobert Watson 	INP_LOCK_ASSERT(tp->t_inpcb);
27572be3bf22SRobert Watson 
2758233e8c18SGarrett Wollman 	tcpstat.tcps_rttupdated++;
2759233e8c18SGarrett Wollman 	tp->t_rttupdated++;
2760233e8c18SGarrett Wollman 	if (tp->t_srtt != 0) {
2761233e8c18SGarrett Wollman 		/*
2762233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
2763233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
2764233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
2765233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
2766233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
2767233e8c18SGarrett Wollman 		 */
2768233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
2769233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
2770233e8c18SGarrett Wollman 
2771233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
2772233e8c18SGarrett Wollman 			tp->t_srtt = 1;
2773233e8c18SGarrett Wollman 
2774233e8c18SGarrett Wollman 		/*
2775233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
2776233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
2777233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
2778233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
2779233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
2780233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
2781233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
2782233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
2783233e8c18SGarrett Wollman 		 */
2784233e8c18SGarrett Wollman 		if (delta < 0)
2785233e8c18SGarrett Wollman 			delta = -delta;
2786233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
2787233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
2788233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
27891fcc99b5SMatthew Dillon 		if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
27901fcc99b5SMatthew Dillon 		    tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
2791233e8c18SGarrett Wollman 	} else {
2792233e8c18SGarrett Wollman 		/*
2793233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
2794233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
2795233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
2796233e8c18SGarrett Wollman 		 */
2797233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
2798233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
27991fcc99b5SMatthew Dillon 		tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
2800233e8c18SGarrett Wollman 	}
28019b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
2802df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
2803df8bae1dSRodney W. Grimes 
2804df8bae1dSRodney W. Grimes 	/*
2805df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
2806df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
2807df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
2808df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
2809df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
2810df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
2811df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
2812df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
2813df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
2814df8bae1dSRodney W. Grimes 	 */
2815233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
28169e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
2817df8bae1dSRodney W. Grimes 
2818df8bae1dSRodney W. Grimes 	/*
2819df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
2820df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
2821df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
2822df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
2823df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
2824df8bae1dSRodney W. Grimes 	 */
2825df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
2826df8bae1dSRodney W. Grimes }
2827df8bae1dSRodney W. Grimes 
2828df8bae1dSRodney W. Grimes /*
2829df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
2830df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
2831df8bae1dSRodney W. Grimes  * If none, use an mss that can be handled on the outgoing
2832df8bae1dSRodney W. Grimes  * interface without forcing IP to fragment; if bigger than
2833df8bae1dSRodney W. Grimes  * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
2834df8bae1dSRodney W. Grimes  * to utilize large mbufs.  If no route is found, route has no mtu,
2835df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
2836df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
2837df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
2838df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
2839df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
2840df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
2841df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
2842a0292f23SGarrett Wollman  *
2843a0292f23SGarrett Wollman  * Also take into account the space needed for options that we
2844a0292f23SGarrett Wollman  * send regularly.  Make maxseg shorter by that amount to assure
2845a0292f23SGarrett Wollman  * that we can send maxseg amount of data even when the options
2846a0292f23SGarrett Wollman  * are present.  Store the upper limit of the length of options plus
2847a0292f23SGarrett Wollman  * data in maxopd.
2848a0292f23SGarrett Wollman  *
2849a0292f23SGarrett Wollman  *
2850a0292f23SGarrett Wollman  * In case of T/TCP, we call this routine during implicit connection
2851a0292f23SGarrett Wollman  * setup as well (offer = -1), to initialize maxseg from the cached
2852a0292f23SGarrett Wollman  * MSS of our peer.
285397d8d152SAndre Oppermann  *
285497d8d152SAndre Oppermann  * NOTE that this routine is only called when we process an incoming
285597d8d152SAndre Oppermann  * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt().
2856df8bae1dSRodney W. Grimes  */
2857a0292f23SGarrett Wollman void
2858df8bae1dSRodney W. Grimes tcp_mss(tp, offer)
2859a0292f23SGarrett Wollman 	struct tcpcb *tp;
2860a0292f23SGarrett Wollman 	int offer;
2861df8bae1dSRodney W. Grimes {
286297d8d152SAndre Oppermann 	int rtt, mss;
2863df8bae1dSRodney W. Grimes 	u_long bufsize;
286497d8d152SAndre Oppermann 	u_long maxmtu;
2865c068736aSJeffrey Hsu 	struct inpcb *inp = tp->t_inpcb;
2866df8bae1dSRodney W. Grimes 	struct socket *so;
286797d8d152SAndre Oppermann 	struct hc_metrics_lite metrics;
2868a0292f23SGarrett Wollman 	int origoffer = offer;
2869233dcce1SAndre Oppermann 	int mtuflags = 0;
2870fb59c426SYoshinobu Inoue #ifdef INET6
2871c068736aSJeffrey Hsu 	int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
2872c068736aSJeffrey Hsu 	size_t min_protoh = isipv6 ?
2873c068736aSJeffrey Hsu 			    sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
2874c068736aSJeffrey Hsu 			    sizeof (struct tcpiphdr);
2875c068736aSJeffrey Hsu #else
2876c068736aSJeffrey Hsu 	const size_t min_protoh = sizeof(struct tcpiphdr);
2877fb59c426SYoshinobu Inoue #endif
2878df8bae1dSRodney W. Grimes 
287997d8d152SAndre Oppermann 	/* initialize */
288097d8d152SAndre Oppermann #ifdef INET6
288197d8d152SAndre Oppermann 	if (isipv6) {
2882233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(&inp->inp_inc, &mtuflags);
288397d8d152SAndre Oppermann 		tp->t_maxopd = tp->t_maxseg = tcp_v6mssdflt;
288497d8d152SAndre Oppermann 	} else
288597d8d152SAndre Oppermann #endif
288697d8d152SAndre Oppermann 	{
2887233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(&inp->inp_inc, &mtuflags);
288897d8d152SAndre Oppermann 		tp->t_maxopd = tp->t_maxseg = tcp_mssdflt;
2889df8bae1dSRodney W. Grimes 	}
2890df8bae1dSRodney W. Grimes 	so = inp->inp_socket;
2891df8bae1dSRodney W. Grimes 
289297d8d152SAndre Oppermann 	/*
28932d166c02SAndre Oppermann 	 * no route to sender, stay with default mss and return
289497d8d152SAndre Oppermann 	 */
289597d8d152SAndre Oppermann 	if (maxmtu == 0)
289697d8d152SAndre Oppermann 		return;
289797d8d152SAndre Oppermann 
289897d8d152SAndre Oppermann 	/* what have we got? */
289997d8d152SAndre Oppermann 	switch (offer) {
290097d8d152SAndre Oppermann 		case 0:
290197d8d152SAndre Oppermann 			/*
290297d8d152SAndre Oppermann 			 * Offer == 0 means that there was no MSS on the SYN
290397d8d152SAndre Oppermann 			 * segment, in this case we use tcp_mssdflt.
290497d8d152SAndre Oppermann 			 */
290597d8d152SAndre Oppermann 			offer =
290697d8d152SAndre Oppermann #ifdef INET6
290797d8d152SAndre Oppermann 				isipv6 ? tcp_v6mssdflt :
290897d8d152SAndre Oppermann #endif
290997d8d152SAndre Oppermann 				tcp_mssdflt;
291097d8d152SAndre Oppermann 			break;
291197d8d152SAndre Oppermann 
291297d8d152SAndre Oppermann 		case -1:
2913a0292f23SGarrett Wollman 			/*
2914c94c54e4SAndre Oppermann 			 * Offer == -1 means that we didn't receive SYN yet.
2915a0292f23SGarrett Wollman 			 */
291697d8d152SAndre Oppermann 			/* FALLTHROUGH */
291797d8d152SAndre Oppermann 
291897d8d152SAndre Oppermann 		default:
2919a0292f23SGarrett Wollman 			/*
292053369ac9SAndre Oppermann 			 * Prevent DoS attack with too small MSS. Round up
292153369ac9SAndre Oppermann 			 * to at least minmss.
292253369ac9SAndre Oppermann 			 */
292353369ac9SAndre Oppermann 			offer = max(offer, tcp_minmss);
292453369ac9SAndre Oppermann 			/*
2925a0292f23SGarrett Wollman 			 * Sanity check: make sure that maxopd will be large
292697d8d152SAndre Oppermann 			 * enough to allow some data on segments even if the
2927a0292f23SGarrett Wollman 			 * all the option space is used (40bytes).  Otherwise
2928a0292f23SGarrett Wollman 			 * funny things may happen in tcp_output.
2929a0292f23SGarrett Wollman 			 */
2930a0292f23SGarrett Wollman 			offer = max(offer, 64);
293197d8d152SAndre Oppermann 	}
2932a0292f23SGarrett Wollman 
2933df8bae1dSRodney W. Grimes 	/*
293497d8d152SAndre Oppermann 	 * rmx information is now retrieved from tcp_hostcache
2935df8bae1dSRodney W. Grimes 	 */
293697d8d152SAndre Oppermann 	tcp_hc_get(&inp->inp_inc, &metrics);
293797d8d152SAndre Oppermann 
2938df8bae1dSRodney W. Grimes 	/*
293997d8d152SAndre Oppermann 	 * if there's a discovered mtu int tcp hostcache, use it
2940fb59c426SYoshinobu Inoue 	 * else, use the link mtu.
2941df8bae1dSRodney W. Grimes 	 */
294297d8d152SAndre Oppermann 	if (metrics.rmx_mtu)
29432d166c02SAndre Oppermann 		mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
2944c068736aSJeffrey Hsu 	else {
294531b3783cSHajimu UMEMOTO #ifdef INET6
2946fb59c426SYoshinobu Inoue 		if (isipv6) {
294797d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
294897d8d152SAndre Oppermann 			if (!path_mtu_discovery &&
294997d8d152SAndre Oppermann 			    !in6_localaddr(&inp->in6p_faddr))
2950fb59c426SYoshinobu Inoue 				mss = min(mss, tcp_v6mssdflt);
2951b3399803SHajimu UMEMOTO 		} else
2952b3399803SHajimu UMEMOTO #endif
295397d8d152SAndre Oppermann 		{
295497d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
295597d8d152SAndre Oppermann 			if (!path_mtu_discovery &&
295697d8d152SAndre Oppermann 			    !in_localaddr(inp->inp_faddr))
2957a0292f23SGarrett Wollman 				mss = min(mss, tcp_mssdflt);
2958a0292f23SGarrett Wollman 		}
295997d8d152SAndre Oppermann 	}
2960a0292f23SGarrett Wollman 	mss = min(mss, offer);
296197d8d152SAndre Oppermann 
2962a0292f23SGarrett Wollman 	/*
2963a0292f23SGarrett Wollman 	 * maxopd stores the maximum length of data AND options
2964a0292f23SGarrett Wollman 	 * in a segment; maxseg is the amount of data in a normal
2965a0292f23SGarrett Wollman 	 * segment.  We need to store this value (maxopd) apart
2966a0292f23SGarrett Wollman 	 * from maxseg, because now every segment carries options
2967a0292f23SGarrett Wollman 	 * and thus we normally have somewhat less data in segments.
2968a0292f23SGarrett Wollman 	 */
2969a0292f23SGarrett Wollman 	tp->t_maxopd = mss;
2970a0292f23SGarrett Wollman 
2971a0292f23SGarrett Wollman 	/*
2972c94c54e4SAndre Oppermann 	 * origoffer==-1 indicates, that no segments were received yet.
2973c94c54e4SAndre Oppermann 	 * In this case we just guess.
2974a0292f23SGarrett Wollman 	 */
2975a0292f23SGarrett Wollman 	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
2976a0292f23SGarrett Wollman 	    (origoffer == -1 ||
2977a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
2978a0292f23SGarrett Wollman 		mss -= TCPOLEN_TSTAMP_APPA;
297997d8d152SAndre Oppermann 	tp->t_maxseg = mss;
2980a0292f23SGarrett Wollman 
2981df8bae1dSRodney W. Grimes #if	(MCLBYTES & (MCLBYTES - 1)) == 0
2982df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
2983df8bae1dSRodney W. Grimes 			mss &= ~(MCLBYTES-1);
2984df8bae1dSRodney W. Grimes #else
2985df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
2986df8bae1dSRodney W. Grimes 			mss = mss / MCLBYTES * MCLBYTES;
2987df8bae1dSRodney W. Grimes #endif
298897d8d152SAndre Oppermann 	tp->t_maxseg = mss;
298997d8d152SAndre Oppermann 
2990df8bae1dSRodney W. Grimes 	/*
299197d8d152SAndre Oppermann 	 * If there's a pipesize, change the socket buffer to that size,
299297d8d152SAndre Oppermann 	 * don't change if sb_hiwat is different than default (then it
299397d8d152SAndre Oppermann 	 * has been changed on purpose with setsockopt).
299497d8d152SAndre Oppermann 	 * Make the socket buffers an integral number of mss units;
299597d8d152SAndre Oppermann 	 * if the mss is larger than the socket buffer, decrease the mss.
2996df8bae1dSRodney W. Grimes 	 */
29973f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
299897d8d152SAndre Oppermann 	if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe)
299997d8d152SAndre Oppermann 		bufsize = metrics.rmx_sendpipe;
300097d8d152SAndre Oppermann 	else
3001df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
3002df8bae1dSRodney W. Grimes 	if (bufsize < mss)
3003df8bae1dSRodney W. Grimes 		mss = bufsize;
3004df8bae1dSRodney W. Grimes 	else {
3005df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3006df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3007df8bae1dSRodney W. Grimes 			bufsize = sb_max;
300888c39af3SRuslan Ermilov 		if (bufsize > so->so_snd.sb_hiwat)
30093f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_snd, bufsize, so, NULL);
3010df8bae1dSRodney W. Grimes 	}
30113f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
3012df8bae1dSRodney W. Grimes 	tp->t_maxseg = mss;
3013df8bae1dSRodney W. Grimes 
30143f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
301597d8d152SAndre Oppermann 	if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe)
301697d8d152SAndre Oppermann 		bufsize = metrics.rmx_recvpipe;
301797d8d152SAndre Oppermann 	else
3018df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
3019df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
3020df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3021df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3022df8bae1dSRodney W. Grimes 			bufsize = sb_max;
302388c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
30243f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL);
3025df8bae1dSRodney W. Grimes 	}
30263f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
3027a0292f23SGarrett Wollman 	/*
302897d8d152SAndre Oppermann 	 * While we're here, check the others too
3029a0292f23SGarrett Wollman 	 */
303097d8d152SAndre Oppermann 	if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) {
303197d8d152SAndre Oppermann 		tp->t_srtt = rtt;
303297d8d152SAndre Oppermann 		tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
303397d8d152SAndre Oppermann 		tcpstat.tcps_usedrtt++;
303497d8d152SAndre Oppermann 		if (metrics.rmx_rttvar) {
303597d8d152SAndre Oppermann 			tp->t_rttvar = metrics.rmx_rttvar;
303697d8d152SAndre Oppermann 			tcpstat.tcps_usedrttvar++;
303797d8d152SAndre Oppermann 		} else {
303897d8d152SAndre Oppermann 			/* default variation is +- 1 rtt */
303997d8d152SAndre Oppermann 			tp->t_rttvar =
304097d8d152SAndre Oppermann 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
304197d8d152SAndre Oppermann 		}
304297d8d152SAndre Oppermann 		TCPT_RANGESET(tp->t_rxtcur,
304397d8d152SAndre Oppermann 			      ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
304497d8d152SAndre Oppermann 			      tp->t_rttmin, TCPTV_REXMTMAX);
304597d8d152SAndre Oppermann 	}
304697d8d152SAndre Oppermann 	if (metrics.rmx_ssthresh) {
3047df8bae1dSRodney W. Grimes 		/*
3048df8bae1dSRodney W. Grimes 		 * There's some sort of gateway or interface
3049df8bae1dSRodney W. Grimes 		 * buffer limit on the path.  Use this to set
3050df8bae1dSRodney W. Grimes 		 * the slow start threshhold, but set the
3051df8bae1dSRodney W. Grimes 		 * threshold to no less than 2*mss.
3052df8bae1dSRodney W. Grimes 		 */
305397d8d152SAndre Oppermann 		tp->snd_ssthresh = max(2 * mss, metrics.rmx_ssthresh);
3054dd224982SGarrett Wollman 		tcpstat.tcps_usedssthresh++;
3055df8bae1dSRodney W. Grimes 	}
305697d8d152SAndre Oppermann 	if (metrics.rmx_bandwidth)
305797d8d152SAndre Oppermann 		tp->snd_bandwidth = metrics.rmx_bandwidth;
305897d8d152SAndre Oppermann 
305997d8d152SAndre Oppermann 	/*
306097d8d152SAndre Oppermann 	 * Set the slow-start flight size depending on whether this
306197d8d152SAndre Oppermann 	 * is a local network or not.
306297d8d152SAndre Oppermann 	 *
306397d8d152SAndre Oppermann 	 * Extend this so we cache the cwnd too and retrieve it here.
306497d8d152SAndre Oppermann 	 * Make cwnd even bigger than RFC3390 suggests but only if we
306597d8d152SAndre Oppermann 	 * have previous experience with the remote host. Be careful
306697d8d152SAndre Oppermann 	 * not make cwnd bigger than remote receive window or our own
306797d8d152SAndre Oppermann 	 * send socket buffer. Maybe put some additional upper bound
306897d8d152SAndre Oppermann 	 * on the retrieved cwnd. Should do incremental updates to
306997d8d152SAndre Oppermann 	 * hostcache when cwnd collapses so next connection doesn't
307097d8d152SAndre Oppermann 	 * overloads the path again.
307197d8d152SAndre Oppermann 	 *
307297d8d152SAndre Oppermann 	 * RFC3390 says only do this if SYN or SYN/ACK didn't got lost.
307397d8d152SAndre Oppermann 	 * We currently check only in syncache_socket for that.
307497d8d152SAndre Oppermann 	 */
307597d8d152SAndre Oppermann #define TCP_METRICS_CWND
307697d8d152SAndre Oppermann #ifdef TCP_METRICS_CWND
307797d8d152SAndre Oppermann 	if (metrics.rmx_cwnd)
307897d8d152SAndre Oppermann 		tp->snd_cwnd = max(mss,
307997d8d152SAndre Oppermann 				min(metrics.rmx_cwnd / 2,
308097d8d152SAndre Oppermann 				 min(tp->snd_wnd, so->so_snd.sb_hiwat)));
308197d8d152SAndre Oppermann 	else
308297d8d152SAndre Oppermann #endif
308397d8d152SAndre Oppermann 	if (tcp_do_rfc3390)
308497d8d152SAndre Oppermann 		tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380));
308597d8d152SAndre Oppermann #ifdef INET6
308697d8d152SAndre Oppermann 	else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) ||
308797d8d152SAndre Oppermann 		 (!isipv6 && in_localaddr(inp->inp_faddr)))
3088943ae302SAndre Oppermann #else
3089943ae302SAndre Oppermann 	else if (in_localaddr(inp->inp_faddr))
309097d8d152SAndre Oppermann #endif
3091943ae302SAndre Oppermann 		tp->snd_cwnd = mss * ss_fltsz_local;
309297d8d152SAndre Oppermann 	else
309397d8d152SAndre Oppermann 		tp->snd_cwnd = mss * ss_fltsz;
3094233dcce1SAndre Oppermann 
3095233dcce1SAndre Oppermann 	/* Check the interface for TSO capabilities. */
3096233dcce1SAndre Oppermann 	if (mtuflags & CSUM_TSO)
3097233dcce1SAndre Oppermann 		tp->t_flags |= TF_TSO;
3098a0292f23SGarrett Wollman }
3099a0292f23SGarrett Wollman 
3100a0292f23SGarrett Wollman /*
3101a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
3102a0292f23SGarrett Wollman  */
3103a0292f23SGarrett Wollman int
310497d8d152SAndre Oppermann tcp_mssopt(inc)
310597d8d152SAndre Oppermann 	struct in_conninfo *inc;
3106a0292f23SGarrett Wollman {
310797d8d152SAndre Oppermann 	int mss = 0;
310897d8d152SAndre Oppermann 	u_long maxmtu = 0;
310997d8d152SAndre Oppermann 	u_long thcmtu = 0;
311097d8d152SAndre Oppermann 	size_t min_protoh;
3111fb59c426SYoshinobu Inoue #ifdef INET6
311297d8d152SAndre Oppermann 	int isipv6 = inc->inc_isipv6 ? 1 : 0;
3113fb59c426SYoshinobu Inoue #endif
3114a0292f23SGarrett Wollman 
311597d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3116a0292f23SGarrett Wollman 
311731b3783cSHajimu UMEMOTO #ifdef INET6
311897d8d152SAndre Oppermann 	if (isipv6) {
311997d8d152SAndre Oppermann 		mss = tcp_v6mssdflt;
3120233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(inc, NULL);
312197d8d152SAndre Oppermann 		thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
312297d8d152SAndre Oppermann 		min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
312397d8d152SAndre Oppermann 	} else
312431b3783cSHajimu UMEMOTO #endif
312597d8d152SAndre Oppermann 	{
312697d8d152SAndre Oppermann 		mss = tcp_mssdflt;
3127233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(inc, NULL);
312897d8d152SAndre Oppermann 		thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
312997d8d152SAndre Oppermann 		min_protoh = sizeof(struct tcpiphdr);
313097d8d152SAndre Oppermann 	}
313197d8d152SAndre Oppermann 	if (maxmtu && thcmtu)
313297d8d152SAndre Oppermann 		mss = min(maxmtu, thcmtu) - min_protoh;
313397d8d152SAndre Oppermann 	else if (maxmtu || thcmtu)
313497d8d152SAndre Oppermann 		mss = max(maxmtu, thcmtu) - min_protoh;
313597d8d152SAndre Oppermann 
313697d8d152SAndre Oppermann 	return (mss);
3137df8bae1dSRodney W. Grimes }
313846f58482SJonathan Lemon 
313946f58482SJonathan Lemon 
314046f58482SJonathan Lemon /*
3141c068736aSJeffrey Hsu  * On a partial ack arrives, force the retransmission of the
3142c068736aSJeffrey Hsu  * next unacknowledged segment.  Do not clear tp->t_dupacks.
3143c068736aSJeffrey Hsu  * By setting snd_nxt to ti_ack, this forces retransmission timer to
3144c068736aSJeffrey Hsu  * be started again.
314546f58482SJonathan Lemon  */
3146c068736aSJeffrey Hsu static void
3147c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th)
314846f58482SJonathan Lemon 	struct tcpcb *tp;
314946f58482SJonathan Lemon 	struct tcphdr *th;
315046f58482SJonathan Lemon {
315146f58482SJonathan Lemon 	tcp_seq onxt = tp->snd_nxt;
315246f58482SJonathan Lemon 	u_long  ocwnd = tp->snd_cwnd;
315346f58482SJonathan Lemon 
315446f58482SJonathan Lemon 	callout_stop(tp->tt_rexmt);
315546f58482SJonathan Lemon 	tp->t_rtttime = 0;
315646f58482SJonathan Lemon 	tp->snd_nxt = th->th_ack;
31576b2a5f92SJayanth Vijayaraghavan 	/*
3158c068736aSJeffrey Hsu 	 * Set snd_cwnd to one segment beyond acknowledged offset.
3159c068736aSJeffrey Hsu 	 * (tp->snd_una has not yet been updated when this function is called.)
31606b2a5f92SJayanth Vijayaraghavan 	 */
31616b2a5f92SJayanth Vijayaraghavan 	tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una);
3162a84db8f4SMatthew Dillon 	tp->t_flags |= TF_ACKNOW;
31636b2a5f92SJayanth Vijayaraghavan 	(void) tcp_output(tp);
316446f58482SJonathan Lemon 	tp->snd_cwnd = ocwnd;
316546f58482SJonathan Lemon 	if (SEQ_GT(onxt, tp->snd_nxt))
316646f58482SJonathan Lemon 		tp->snd_nxt = onxt;
316746f58482SJonathan Lemon 	/*
316846f58482SJonathan Lemon 	 * Partial window deflation.  Relies on fact that tp->snd_una
316946f58482SJonathan Lemon 	 * not updated yet.
317046f58482SJonathan Lemon 	 */
3171d7587117SPaul Saab 	if (tp->snd_cwnd > th->th_ack - tp->snd_una)
3172d7587117SPaul Saab 		tp->snd_cwnd -= th->th_ack - tp->snd_una;
3173d7587117SPaul Saab 	else
3174d7587117SPaul Saab 		tp->snd_cwnd = 0;
3175d7587117SPaul Saab 	tp->snd_cwnd += tp->t_maxseg;
317646f58482SJonathan Lemon }
3177340c35deSJonathan Lemon 
3178340c35deSJonathan Lemon /*
3179340c35deSJonathan Lemon  * Returns 1 if the TIME_WAIT state was killed and we should start over,
3180340c35deSJonathan Lemon  * looking for a pcb in the listen state.  Returns 0 otherwise.
3181340c35deSJonathan Lemon  */
3182340c35deSJonathan Lemon static int
31833cbe7fafSRobert Watson tcp_timewait(inp, to, th, m, tlen)
31843cbe7fafSRobert Watson 	struct inpcb *inp;
3185340c35deSJonathan Lemon 	struct tcpopt *to;
3186340c35deSJonathan Lemon 	struct tcphdr *th;
3187340c35deSJonathan Lemon 	struct mbuf *m;
3188340c35deSJonathan Lemon 	int tlen;
3189340c35deSJonathan Lemon {
31903cbe7fafSRobert Watson 	struct tcptw *tw;
3191340c35deSJonathan Lemon 	int thflags;
3192340c35deSJonathan Lemon 	tcp_seq seq;
3193340c35deSJonathan Lemon #ifdef INET6
3194340c35deSJonathan Lemon 	int isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
3195340c35deSJonathan Lemon #else
3196340c35deSJonathan Lemon 	const int isipv6 = 0;
3197340c35deSJonathan Lemon #endif
3198340c35deSJonathan Lemon 
3199751dea29SRuslan Ermilov 	/* tcbinfo lock required for tcp_twclose(), tcp_timer_2msl_reset(). */
32003cbe7fafSRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
32013cbe7fafSRobert Watson 	INP_LOCK_ASSERT(inp);
32023cbe7fafSRobert Watson 
3203ae0e7143SRobert Watson 	/*
3204ae0e7143SRobert Watson 	 * XXXRW: Time wait state for inpcb has been recycled, but inpcb is
3205ae0e7143SRobert Watson 	 * still present.  This is undesirable, but temporarily necessary
3206ae0e7143SRobert Watson 	 * until we work out how to handle inpcb's who's timewait state has
3207ae0e7143SRobert Watson 	 * been removed.
3208ae0e7143SRobert Watson 	 */
32093cbe7fafSRobert Watson 	tw = intotw(inp);
3210ae0e7143SRobert Watson 	if (tw == NULL)
3211ae0e7143SRobert Watson 		goto drop;
3212ae0e7143SRobert Watson 
3213340c35deSJonathan Lemon 	thflags = th->th_flags;
3214340c35deSJonathan Lemon 
3215340c35deSJonathan Lemon 	/*
3216340c35deSJonathan Lemon 	 * NOTE: for FIN_WAIT_2 (to be added later),
3217340c35deSJonathan Lemon 	 * must validate sequence number before accepting RST
3218340c35deSJonathan Lemon 	 */
3219340c35deSJonathan Lemon 
3220340c35deSJonathan Lemon 	/*
3221340c35deSJonathan Lemon 	 * If the segment contains RST:
3222340c35deSJonathan Lemon 	 *	Drop the segment - see Stevens, vol. 2, p. 964 and
3223340c35deSJonathan Lemon 	 *      RFC 1337.
3224340c35deSJonathan Lemon 	 */
3225340c35deSJonathan Lemon 	if (thflags & TH_RST)
3226340c35deSJonathan Lemon 		goto drop;
3227340c35deSJonathan Lemon 
3228340c35deSJonathan Lemon #if 0
3229340c35deSJonathan Lemon /* PAWS not needed at the moment */
3230340c35deSJonathan Lemon 	/*
3231340c35deSJonathan Lemon 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
3232340c35deSJonathan Lemon 	 * and it's less than ts_recent, drop it.
3233340c35deSJonathan Lemon 	 */
3234340c35deSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
3235340c35deSJonathan Lemon 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
3236340c35deSJonathan Lemon 		if ((thflags & TH_ACK) == 0)
3237340c35deSJonathan Lemon 			goto drop;
3238340c35deSJonathan Lemon 		goto ack;
3239340c35deSJonathan Lemon 	}
3240340c35deSJonathan Lemon 	/*
3241340c35deSJonathan Lemon 	 * ts_recent is never updated because we never accept new segments.
3242340c35deSJonathan Lemon 	 */
3243340c35deSJonathan Lemon #endif
3244340c35deSJonathan Lemon 
3245340c35deSJonathan Lemon 	/*
3246340c35deSJonathan Lemon 	 * If a new connection request is received
3247340c35deSJonathan Lemon 	 * while in TIME_WAIT, drop the old connection
3248340c35deSJonathan Lemon 	 * and start over if the sequence numbers
3249340c35deSJonathan Lemon 	 * are above the previous ones.
3250340c35deSJonathan Lemon 	 */
3251340c35deSJonathan Lemon 	if ((thflags & TH_SYN) && SEQ_GT(th->th_seq, tw->rcv_nxt)) {
3252623dce13SRobert Watson 		tcp_twclose(tw, 0);
3253340c35deSJonathan Lemon 		return (1);
3254340c35deSJonathan Lemon 	}
3255340c35deSJonathan Lemon 
3256340c35deSJonathan Lemon 	/*
3257340c35deSJonathan Lemon 	 * Drop the the segment if it does not contain an ACK.
3258340c35deSJonathan Lemon 	 */
3259340c35deSJonathan Lemon 	if ((thflags & TH_ACK) == 0)
3260340c35deSJonathan Lemon 		goto drop;
3261340c35deSJonathan Lemon 
3262340c35deSJonathan Lemon 	/*
3263340c35deSJonathan Lemon 	 * Reset the 2MSL timer if this is a duplicate FIN.
3264340c35deSJonathan Lemon 	 */
3265340c35deSJonathan Lemon 	if (thflags & TH_FIN) {
3266340c35deSJonathan Lemon 		seq = th->th_seq + tlen + (thflags & TH_SYN ? 1 : 0);
3267340c35deSJonathan Lemon 		if (seq + 1 == tw->rcv_nxt)
3268751dea29SRuslan Ermilov 			tcp_timer_2msl_reset(tw, 1);
3269340c35deSJonathan Lemon 	}
3270340c35deSJonathan Lemon 
3271340c35deSJonathan Lemon 	/*
3272272c5dfeSJonathan Lemon 	 * Acknowledge the segment if it has data or is not a duplicate ACK.
3273340c35deSJonathan Lemon 	 */
3274272c5dfeSJonathan Lemon 	if (thflags != TH_ACK || tlen != 0 ||
3275272c5dfeSJonathan Lemon 	    th->th_seq != tw->rcv_nxt || th->th_ack != tw->snd_nxt)
3276a7b6a14aSRobert Watson 		tcp_twrespond(tw, TH_ACK);
3277340c35deSJonathan Lemon 	goto drop;
3278340c35deSJonathan Lemon 
3279340c35deSJonathan Lemon 	/*
3280340c35deSJonathan Lemon 	 * Generate a RST, dropping incoming segment.
3281340c35deSJonathan Lemon 	 * Make ACK acceptable to originator of segment.
3282340c35deSJonathan Lemon 	 * Don't bother to respond if destination was broadcast/multicast.
3283340c35deSJonathan Lemon 	 */
3284340c35deSJonathan Lemon 	if (m->m_flags & (M_BCAST|M_MCAST))
3285340c35deSJonathan Lemon 		goto drop;
3286340c35deSJonathan Lemon 	if (isipv6) {
3287340c35deSJonathan Lemon 		struct ip6_hdr *ip6;
3288340c35deSJonathan Lemon 
3289340c35deSJonathan Lemon 		/* IPv6 anycast check is done at tcp6_input() */
3290340c35deSJonathan Lemon 		ip6 = mtod(m, struct ip6_hdr *);
3291340c35deSJonathan Lemon 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3292340c35deSJonathan Lemon 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3293340c35deSJonathan Lemon 			goto drop;
3294340c35deSJonathan Lemon 	} else {
3295340c35deSJonathan Lemon 		struct ip *ip;
3296340c35deSJonathan Lemon 
3297340c35deSJonathan Lemon 		ip = mtod(m, struct ip *);
3298340c35deSJonathan Lemon 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3299340c35deSJonathan Lemon 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3300340c35deSJonathan Lemon 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3301340c35deSJonathan Lemon 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
3302340c35deSJonathan Lemon 			goto drop;
3303340c35deSJonathan Lemon 	}
3304340c35deSJonathan Lemon 	if (thflags & TH_ACK) {
3305340c35deSJonathan Lemon 		tcp_respond(NULL,
3306340c35deSJonathan Lemon 		    mtod(m, void *), th, m, 0, th->th_ack, TH_RST);
3307340c35deSJonathan Lemon 	} else {
3308340c35deSJonathan Lemon 		seq = th->th_seq + (thflags & TH_SYN ? 1 : 0);
3309340c35deSJonathan Lemon 		tcp_respond(NULL,
3310340c35deSJonathan Lemon 		    mtod(m, void *), th, m, seq, 0, TH_RST|TH_ACK);
3311340c35deSJonathan Lemon 	}
33123cbe7fafSRobert Watson 	INP_UNLOCK(inp);
3313340c35deSJonathan Lemon 	return (0);
3314340c35deSJonathan Lemon 
3315340c35deSJonathan Lemon drop:
33163cbe7fafSRobert Watson 	INP_UNLOCK(inp);
3317340c35deSJonathan Lemon 	m_freem(m);
3318340c35deSJonathan Lemon 	return (0);
3319340c35deSJonathan Lemon }
3320