xref: /freebsd/sys/netinet/tcp_input.c (revision ad3f9ab320ed8ae3b99e300f7daa1469cb66139d)
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"
396d90faf3SPaul Saab #include "opt_tcp_sack.h"
400cc12cc5SJoerg Wunsch 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
4298163b98SPoul-Henning Kamp #include <sys/kernel.h>
43df8bae1dSRodney W. Grimes #include <sys/malloc.h>
44df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
45a29f300eSGarrett Wollman #include <sys/proc.h>		/* for proc0 declaration */
46df8bae1dSRodney W. Grimes #include <sys/protosw.h>
47960ed29cSSeigo Tanimura #include <sys/signalvar.h>
48df8bae1dSRodney W. Grimes #include <sys/socket.h>
49df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
50960ed29cSSeigo Tanimura #include <sys/sysctl.h>
51816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
52960ed29cSSeigo Tanimura #include <sys/systm.h>
53df8bae1dSRodney W. Grimes 
54e79adb8eSGarrett Wollman #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
55e79adb8eSGarrett Wollman 
5612e2e970SAndre Oppermann #include <vm/uma.h>
5712e2e970SAndre Oppermann 
58df8bae1dSRodney W. Grimes #include <net/if.h>
59df8bae1dSRodney W. Grimes #include <net/route.h>
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes #include <netinet/in.h>
62960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
63df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
64960ed29cSSeigo Tanimura #include <netinet/in_var.h>
65df8bae1dSRodney W. Grimes #include <netinet/ip.h>
668e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h>	/* required for icmp_var.h */
67686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM */
68df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
69ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
70686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
71686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
72686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
73960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h>
74960ed29cSSeigo Tanimura #include <netinet6/nd6.h>
75df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
76df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
77df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
78df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
79df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
80fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h>
81df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
82610ee2f9SDavid Greenman #ifdef TCPDEBUG
83df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
84fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */
85fb59c426SYoshinobu Inoue 
86b9234fafSSam Leffler #ifdef FAST_IPSEC
87b9234fafSSam Leffler #include <netipsec/ipsec.h>
88b9234fafSSam Leffler #include <netipsec/ipsec6.h>
89b9234fafSSam Leffler #endif /*FAST_IPSEC*/
90b9234fafSSam Leffler 
91fb59c426SYoshinobu Inoue #ifdef IPSEC
92fb59c426SYoshinobu Inoue #include <netinet6/ipsec.h>
933a2a9f79SYoshinobu Inoue #include <netinet6/ipsec6.h>
94fb59c426SYoshinobu Inoue #include <netkey/key.h>
95fb59c426SYoshinobu Inoue #endif /*IPSEC*/
96fb59c426SYoshinobu Inoue 
97db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
98db4f9cc7SJonathan Lemon 
99aed55708SRobert Watson #include <security/mac/mac_framework.h>
100aed55708SRobert Watson 
101c068736aSJeffrey Hsu static const int tcprexmtthresh = 3;
1020312fbe9SPoul-Henning Kamp 
1032f96f1f4SGarrett Wollman struct	tcpstat tcpstat;
104c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW,
1053d177f46SBill Fumerola     &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
1060312fbe9SPoul-Henning Kamp 
107afdb4274SRobert Watson static int tcp_log_in_vain = 0;
108816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
109574b6964SAndre Oppermann     &tcp_log_in_vain, 0, "Log all incoming TCP segments to closed ports");
110816a3d83SPoul-Henning Kamp 
11116f7f31fSGeoff Rehmet static int blackhole = 0;
11216f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW,
113574b6964SAndre Oppermann     &blackhole, 0, "Do not send RST on segments to closed ports");
11416f7f31fSGeoff Rehmet 
115f498eeeeSDavid Greenman int tcp_delack_enabled = 1;
11684e33c9eSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW,
1173d177f46SBill Fumerola     &tcp_delack_enabled, 0,
1183d177f46SBill Fumerola     "Delay ACK to try and piggyback it onto a data packet");
119f498eeeeSDavid Greenman 
120f8613305SDag-Erling Smørgrav static int drop_synfin = 0;
121f8613305SDag-Erling Smørgrav SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW,
122f8613305SDag-Erling Smørgrav     &drop_synfin, 0, "Drop TCP packets with SYN+FIN set");
123f8613305SDag-Erling Smørgrav 
124dba7bc6aSAndre Oppermann static int tcp_do_rfc3042 = 1;
125582a954bSJeffrey Hsu SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW,
126582a954bSJeffrey Hsu     &tcp_do_rfc3042, 0, "Enable RFC 3042 (Limited Transmit)");
127582a954bSJeffrey Hsu 
128dba7bc6aSAndre Oppermann static int tcp_do_rfc3390 = 1;
129da3a8a1aSJeffrey Hsu SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW,
130da3a8a1aSJeffrey Hsu     &tcp_do_rfc3390, 0,
131da3a8a1aSJeffrey Hsu     "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
132da3a8a1aSJeffrey Hsu 
133a69968eeSMike Silbersack static int tcp_insecure_rst = 0;
134a69968eeSMike Silbersack SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_RW,
135a69968eeSMike Silbersack     &tcp_insecure_rst, 0,
1366489fe65SAndre Oppermann     "Follow the old (insecure) criteria for accepting RST packets");
137a69968eeSMike Silbersack 
13812e2e970SAndre Oppermann SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW, 0,
13912e2e970SAndre Oppermann     "TCP Segment Reassembly Queue");
14012e2e970SAndre Oppermann 
14112e2e970SAndre Oppermann static int tcp_reass_maxseg = 0;
14212e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxsegments, CTLFLAG_RDTUN,
14312e2e970SAndre Oppermann     &tcp_reass_maxseg, 0,
14412e2e970SAndre Oppermann     "Global maximum number of TCP Segments in Reassembly Queue");
14512e2e970SAndre Oppermann 
14612e2e970SAndre Oppermann int tcp_reass_qsize = 0;
14712e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, cursegments, CTLFLAG_RD,
14812e2e970SAndre Oppermann     &tcp_reass_qsize, 0,
14912e2e970SAndre Oppermann     "Global number of TCP Segments currently in Reassembly Queue");
15012e2e970SAndre Oppermann 
15112e2e970SAndre Oppermann static int tcp_reass_maxqlen = 48;
15212e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxqlen, CTLFLAG_RW,
15312e2e970SAndre Oppermann     &tcp_reass_maxqlen, 0,
15412e2e970SAndre Oppermann     "Maximum number of TCP Segments per individual Reassembly Queue");
15512e2e970SAndre Oppermann 
15612e2e970SAndre Oppermann static int tcp_reass_overflows = 0;
15712e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, overflows, CTLFLAG_RD,
15812e2e970SAndre Oppermann     &tcp_reass_overflows, 0,
15912e2e970SAndre Oppermann     "Global number of TCP Segment Reassembly Queue Overflows");
16012e2e970SAndre Oppermann 
1616741ecf5SAndre Oppermann int	tcp_do_autorcvbuf = 1;
1626741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_RW,
1636741ecf5SAndre Oppermann     &tcp_do_autorcvbuf, 0, "Enable automatic receive buffer sizing");
1646741ecf5SAndre Oppermann 
1656741ecf5SAndre Oppermann int	tcp_autorcvbuf_inc = 16*1024;
1666741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_RW,
1676489fe65SAndre Oppermann     &tcp_autorcvbuf_inc, 0,
1686489fe65SAndre Oppermann     "Incrementor step size of automatic receive buffer");
1696741ecf5SAndre Oppermann 
1706741ecf5SAndre Oppermann int	tcp_autorcvbuf_max = 256*1024;
1716741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_RW,
1726741ecf5SAndre Oppermann     &tcp_autorcvbuf_max, 0, "Max size of automatic receive buffer");
1736741ecf5SAndre Oppermann 
17415bd2b43SDavid Greenman struct inpcbhead tcb;
175fb59c426SYoshinobu Inoue #define	tcb6	tcb  /* for KAME src sync over BSD*'s */
17615bd2b43SDavid Greenman struct inpcbinfo tcbinfo;
177f76fcf6dSJeffrey Hsu struct mtx	*tcbinfo_mtx;
178df8bae1dSRodney W. Grimes 
1795a53ca16SPaul Saab static void	 tcp_dooptions(struct tcpopt *, u_char *, int, int);
1806d90faf3SPaul Saab 
1814d77a549SAlfred Perlstein static void	 tcp_pulloutofband(struct socket *,
1824d77a549SAlfred Perlstein 		     struct tcphdr *, struct mbuf *, int);
1834d77a549SAlfred Perlstein static int	 tcp_reass(struct tcpcb *, struct tcphdr *, int *,
1844d77a549SAlfred Perlstein 		     struct mbuf *);
1854d77a549SAlfred Perlstein static void	 tcp_xmit_timer(struct tcpcb *, int);
186c068736aSJeffrey Hsu static void	 tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *);
1873cbe7fafSRobert Watson static int	 tcp_timewait(struct inpcb *, struct tcpopt *,
188340c35deSJonathan Lemon 		     struct tcphdr *, struct mbuf *, int);
1890312fbe9SPoul-Henning Kamp 
190fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
191fb59c426SYoshinobu Inoue #ifdef INET6
192fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \
193fb59c426SYoshinobu Inoue do { \
194fb59c426SYoshinobu Inoue 	if ((tp) && (tp)->t_inpcb && \
19597d8d152SAndre Oppermann 	    ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \
19697d8d152SAndre Oppermann 		nd6_nud_hint(NULL, NULL, 0); \
197fb59c426SYoshinobu Inoue } while (0)
198fb59c426SYoshinobu Inoue #else
199fb59c426SYoshinobu Inoue #define ND6_HINT(tp)
200fb59c426SYoshinobu Inoue #endif
201df8bae1dSRodney W. Grimes 
202df8bae1dSRodney W. Grimes /*
203262c1c1aSMatthew Dillon  * Indicate whether this ack should be delayed.  We can delay the ack if
204262c1c1aSMatthew Dillon  *	- there is no delayed ack timer in progress and
205262c1c1aSMatthew Dillon  *	- our last ack wasn't a 0-sized window.  We never want to delay
2063bfd6421SJonathan Lemon  *	  the ack that opens up a 0-sized window and
2073bfd6421SJonathan Lemon  *		- delayed acks are enabled or
2083bfd6421SJonathan Lemon  *		- this is a half-synchronized T/TCP connection.
209d8c85a26SJonathan Lemon  */
210d8c85a26SJonathan Lemon #define DELAY_ACK(tp)							\
211a14c749fSJonathan Lemon 	((!callout_active(tp->tt_delack) &&				\
212a14c749fSJonathan Lemon 	    (tp->t_flags & TF_RXWIN0SENT) == 0) &&			\
2133bfd6421SJonathan Lemon 	    (tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
214d8c85a26SJonathan Lemon 
21512e2e970SAndre Oppermann /* Initialize TCP reassembly queue */
2164f590175SPaul Saab static void
2174f590175SPaul Saab tcp_reass_zone_change(void *tag)
2184f590175SPaul Saab {
2194f590175SPaul Saab 
2204f590175SPaul Saab 	tcp_reass_maxseg = nmbclusters / 16;
2214f590175SPaul Saab 	uma_zone_set_max(tcp_reass_zone, tcp_reass_maxseg);
2224f590175SPaul Saab }
2234f590175SPaul Saab 
22412e2e970SAndre Oppermann uma_zone_t	tcp_reass_zone;
22512e2e970SAndre Oppermann void
22612e2e970SAndre Oppermann tcp_reass_init()
22712e2e970SAndre Oppermann {
22812e2e970SAndre Oppermann 	tcp_reass_maxseg = nmbclusters / 16;
22912e2e970SAndre Oppermann 	TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments",
23012e2e970SAndre Oppermann 	    &tcp_reass_maxseg);
23112e2e970SAndre Oppermann 	tcp_reass_zone = uma_zcreate("tcpreass", sizeof (struct tseg_qent),
23212e2e970SAndre Oppermann 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
23312e2e970SAndre Oppermann 	uma_zone_set_max(tcp_reass_zone, tcp_reass_maxseg);
2344f590175SPaul Saab 	EVENTHANDLER_REGISTER(nmbclusters_change,
2354f590175SPaul Saab 	    tcp_reass_zone_change, NULL, EVENTHANDLER_PRI_ANY);
23612e2e970SAndre Oppermann }
23712e2e970SAndre Oppermann 
2380312fbe9SPoul-Henning Kamp static int
239ad3f9ab3SAndre Oppermann tcp_reass(struct tcpcb *tp, struct tcphdr *th, int *tlenp, struct mbuf *m)
240df8bae1dSRodney W. Grimes {
241fb59c426SYoshinobu Inoue 	struct tseg_qent *q;
242fb59c426SYoshinobu Inoue 	struct tseg_qent *p = NULL;
243fb59c426SYoshinobu Inoue 	struct tseg_qent *nq;
24412e2e970SAndre Oppermann 	struct tseg_qent *te = NULL;
245df8bae1dSRodney W. Grimes 	struct socket *so = tp->t_inpcb->inp_socket;
246df8bae1dSRodney W. Grimes 	int flags;
247df8bae1dSRodney W. Grimes 
248de30ea13SRobert Watson 	INP_LOCK_ASSERT(tp->t_inpcb);
249de30ea13SRobert Watson 
250df8bae1dSRodney W. Grimes 	/*
25112e2e970SAndre Oppermann 	 * XXX: tcp_reass() is rather inefficient with its data structures
25212e2e970SAndre Oppermann 	 * and should be rewritten (see NetBSD for optimizations).  While
25312e2e970SAndre Oppermann 	 * doing that it should move to its own file tcp_reass.c.
25412e2e970SAndre Oppermann 	 */
25512e2e970SAndre Oppermann 
25612e2e970SAndre Oppermann 	/*
257de30ea13SRobert Watson 	 * Call with th==NULL after become established to
258df8bae1dSRodney W. Grimes 	 * force pre-ESTABLISHED data up to user socket.
259df8bae1dSRodney W. Grimes 	 */
260de30ea13SRobert Watson 	if (th == NULL)
261df8bae1dSRodney W. Grimes 		goto present;
262df8bae1dSRodney W. Grimes 
26312e2e970SAndre Oppermann 	/*
26412e2e970SAndre Oppermann 	 * Limit the number of segments in the reassembly queue to prevent
26512e2e970SAndre Oppermann 	 * holding on to too many segments (and thus running out of mbufs).
26612e2e970SAndre Oppermann 	 * Make sure to let the missing segment through which caused this
26712e2e970SAndre Oppermann 	 * queue.  Always keep one global queue entry spare to be able to
26812e2e970SAndre Oppermann 	 * process the missing segment.
26912e2e970SAndre Oppermann 	 */
27012e2e970SAndre Oppermann 	if (th->th_seq != tp->rcv_nxt &&
27112e2e970SAndre Oppermann 	    (tcp_reass_qsize + 1 >= tcp_reass_maxseg ||
27212e2e970SAndre Oppermann 	     tp->t_segqlen >= tcp_reass_maxqlen)) {
27312e2e970SAndre Oppermann 		tcp_reass_overflows++;
27412e2e970SAndre Oppermann 		tcpstat.tcps_rcvmemdrop++;
27512e2e970SAndre Oppermann 		m_freem(m);
276e346eeffSPaul Saab 		*tlenp = 0;
27712e2e970SAndre Oppermann 		return (0);
27812e2e970SAndre Oppermann 	}
27912e2e970SAndre Oppermann 
28012e2e970SAndre Oppermann 	/*
28112e2e970SAndre Oppermann 	 * Allocate a new queue entry. If we can't, or hit the zone limit
28212e2e970SAndre Oppermann 	 * just drop the pkt.
28312e2e970SAndre Oppermann 	 */
28412e2e970SAndre Oppermann 	te = uma_zalloc(tcp_reass_zone, M_NOWAIT);
285fb59c426SYoshinobu Inoue 	if (te == NULL) {
286fb59c426SYoshinobu Inoue 		tcpstat.tcps_rcvmemdrop++;
287fb59c426SYoshinobu Inoue 		m_freem(m);
288e346eeffSPaul Saab 		*tlenp = 0;
289fb59c426SYoshinobu Inoue 		return (0);
290fb59c426SYoshinobu Inoue 	}
29112e2e970SAndre Oppermann 	tp->t_segqlen++;
29212e2e970SAndre Oppermann 	tcp_reass_qsize++;
2936effc713SDoug Rabson 
294df8bae1dSRodney W. Grimes 	/*
295df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
296df8bae1dSRodney W. Grimes 	 */
297fb59c426SYoshinobu Inoue 	LIST_FOREACH(q, &tp->t_segq, tqe_q) {
298fb59c426SYoshinobu Inoue 		if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
299df8bae1dSRodney W. Grimes 			break;
300fb59c426SYoshinobu Inoue 		p = q;
301fb59c426SYoshinobu Inoue 	}
302df8bae1dSRodney W. Grimes 
303df8bae1dSRodney W. Grimes 	/*
304df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
305df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
306df8bae1dSRodney W. Grimes 	 * segment.  If it provides all of our data, drop us.
307df8bae1dSRodney W. Grimes 	 */
3086effc713SDoug Rabson 	if (p != NULL) {
309ad3f9ab3SAndre Oppermann 		int i;
310df8bae1dSRodney W. Grimes 		/* conversion to int (in i) handles seq wraparound */
311fb59c426SYoshinobu Inoue 		i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
312df8bae1dSRodney W. Grimes 		if (i > 0) {
313fb59c426SYoshinobu Inoue 			if (i >= *tlenp) {
314df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvduppack++;
315fb59c426SYoshinobu Inoue 				tcpstat.tcps_rcvdupbyte += *tlenp;
316df8bae1dSRodney W. Grimes 				m_freem(m);
31712e2e970SAndre Oppermann 				uma_zfree(tcp_reass_zone, te);
31812e2e970SAndre Oppermann 				tp->t_segqlen--;
31912e2e970SAndre Oppermann 				tcp_reass_qsize--;
320a0292f23SGarrett Wollman 				/*
321a0292f23SGarrett Wollman 				 * Try to present any queued data
322a0292f23SGarrett Wollman 				 * at the left window edge to the user.
323a0292f23SGarrett Wollman 				 * This is needed after the 3-WHS
324a0292f23SGarrett Wollman 				 * completes.
325a0292f23SGarrett Wollman 				 */
326a0292f23SGarrett Wollman 				goto present;	/* ??? */
327df8bae1dSRodney W. Grimes 			}
328df8bae1dSRodney W. Grimes 			m_adj(m, i);
329fb59c426SYoshinobu Inoue 			*tlenp -= i;
330fb59c426SYoshinobu Inoue 			th->th_seq += i;
331df8bae1dSRodney W. Grimes 		}
332df8bae1dSRodney W. Grimes 	}
333df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvoopack++;
334fb59c426SYoshinobu Inoue 	tcpstat.tcps_rcvoobyte += *tlenp;
335df8bae1dSRodney W. Grimes 
336df8bae1dSRodney W. Grimes 	/*
337df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
338df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
339df8bae1dSRodney W. Grimes 	 */
3406effc713SDoug Rabson 	while (q) {
341ad3f9ab3SAndre Oppermann 		int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
342df8bae1dSRodney W. Grimes 		if (i <= 0)
343df8bae1dSRodney W. Grimes 			break;
344fb59c426SYoshinobu Inoue 		if (i < q->tqe_len) {
345fb59c426SYoshinobu Inoue 			q->tqe_th->th_seq += i;
346fb59c426SYoshinobu Inoue 			q->tqe_len -= i;
347fb59c426SYoshinobu Inoue 			m_adj(q->tqe_m, i);
348df8bae1dSRodney W. Grimes 			break;
349df8bae1dSRodney W. Grimes 		}
3506effc713SDoug Rabson 
351fb59c426SYoshinobu Inoue 		nq = LIST_NEXT(q, tqe_q);
352fb59c426SYoshinobu Inoue 		LIST_REMOVE(q, tqe_q);
353fb59c426SYoshinobu Inoue 		m_freem(q->tqe_m);
35412e2e970SAndre Oppermann 		uma_zfree(tcp_reass_zone, q);
35512e2e970SAndre Oppermann 		tp->t_segqlen--;
35612e2e970SAndre Oppermann 		tcp_reass_qsize--;
3576effc713SDoug Rabson 		q = nq;
358df8bae1dSRodney W. Grimes 	}
359df8bae1dSRodney W. Grimes 
360fb59c426SYoshinobu Inoue 	/* Insert the new segment queue entry into place. */
361fb59c426SYoshinobu Inoue 	te->tqe_m = m;
362fb59c426SYoshinobu Inoue 	te->tqe_th = th;
363fb59c426SYoshinobu Inoue 	te->tqe_len = *tlenp;
364fb59c426SYoshinobu Inoue 
3656effc713SDoug Rabson 	if (p == NULL) {
366fb59c426SYoshinobu Inoue 		LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
3676effc713SDoug Rabson 	} else {
368fb59c426SYoshinobu Inoue 		LIST_INSERT_AFTER(p, te, tqe_q);
3696effc713SDoug Rabson 	}
370df8bae1dSRodney W. Grimes 
371df8bae1dSRodney W. Grimes present:
372df8bae1dSRodney W. Grimes 	/*
373df8bae1dSRodney W. Grimes 	 * Present data to user, advancing rcv_nxt through
374df8bae1dSRodney W. Grimes 	 * completed sequence space.
375df8bae1dSRodney W. Grimes 	 */
376a0292f23SGarrett Wollman 	if (!TCPS_HAVEESTABLISHED(tp->t_state))
377df8bae1dSRodney W. Grimes 		return (0);
378fb59c426SYoshinobu Inoue 	q = LIST_FIRST(&tp->t_segq);
379fb59c426SYoshinobu Inoue 	if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
380df8bae1dSRodney W. Grimes 		return (0);
3811e4d7da7SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
382df8bae1dSRodney W. Grimes 	do {
383fb59c426SYoshinobu Inoue 		tp->rcv_nxt += q->tqe_len;
384fb59c426SYoshinobu Inoue 		flags = q->tqe_th->th_flags & TH_FIN;
385fb59c426SYoshinobu Inoue 		nq = LIST_NEXT(q, tqe_q);
386fb59c426SYoshinobu Inoue 		LIST_REMOVE(q, tqe_q);
387c0b99ffaSRobert Watson 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
388fb59c426SYoshinobu Inoue 			m_freem(q->tqe_m);
3894cc20ab1SSeigo Tanimura 		else
3901e4d7da7SRobert Watson 			sbappendstream_locked(&so->so_rcv, q->tqe_m);
39112e2e970SAndre Oppermann 		uma_zfree(tcp_reass_zone, q);
39212e2e970SAndre Oppermann 		tp->t_segqlen--;
39312e2e970SAndre Oppermann 		tcp_reass_qsize--;
3946effc713SDoug Rabson 		q = nq;
395fb59c426SYoshinobu Inoue 	} while (q && q->tqe_th->th_seq == tp->rcv_nxt);
396fb59c426SYoshinobu Inoue 	ND6_HINT(tp);
3971e4d7da7SRobert Watson 	sorwakeup_locked(so);
398df8bae1dSRodney W. Grimes 	return (flags);
399df8bae1dSRodney W. Grimes }
400df8bae1dSRodney W. Grimes 
401df8bae1dSRodney W. Grimes /*
402df8bae1dSRodney W. Grimes  * TCP input routine, follows pages 65-76 of the
403df8bae1dSRodney W. Grimes  * protocol specification dated September, 1981 very closely.
404df8bae1dSRodney W. Grimes  */
405fb59c426SYoshinobu Inoue #ifdef INET6
406fb59c426SYoshinobu Inoue int
407ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto)
408fb59c426SYoshinobu Inoue {
409ad3f9ab3SAndre Oppermann 	struct mbuf *m = *mp;
41033841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
411fb59c426SYoshinobu Inoue 
412fb59c426SYoshinobu Inoue 	IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
413fb59c426SYoshinobu Inoue 
414fb59c426SYoshinobu Inoue 	/*
415fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
416fb59c426SYoshinobu Inoue 	 * better place to put this in?
417fb59c426SYoshinobu Inoue 	 */
41833841545SHajimu UMEMOTO 	ia6 = ip6_getdstifaddr(m);
41933841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
420fb59c426SYoshinobu Inoue 		struct ip6_hdr *ip6;
421fb59c426SYoshinobu Inoue 
422fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
423fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
424fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
425fb59c426SYoshinobu Inoue 		return IPPROTO_DONE;
426fb59c426SYoshinobu Inoue 	}
427fb59c426SYoshinobu Inoue 
428f0ffb944SJulian Elischer 	tcp_input(m, *offp);
429fb59c426SYoshinobu Inoue 	return IPPROTO_DONE;
430fb59c426SYoshinobu Inoue }
431fb59c426SYoshinobu Inoue #endif
432fb59c426SYoshinobu Inoue 
433df8bae1dSRodney W. Grimes void
434ad3f9ab3SAndre Oppermann tcp_input(struct mbuf *m, int off0)
435df8bae1dSRodney W. Grimes {
436ad3f9ab3SAndre Oppermann 	struct tcphdr *th;
437ad3f9ab3SAndre Oppermann 	struct ip *ip = NULL;
438ad3f9ab3SAndre Oppermann 	struct ipovly *ipov;
439ad3f9ab3SAndre Oppermann 	struct inpcb *inp = NULL;
440e79adb8eSGarrett Wollman 	u_char *optp = NULL;
44126f9a767SRodney W. Grimes 	int optlen = 0;
442a0194ef1SBruce M Simpson 	int len, tlen, off;
443fb59c426SYoshinobu Inoue 	int drop_hdrlen;
444ad3f9ab3SAndre Oppermann 	struct tcpcb *tp = 0;
445ad3f9ab3SAndre Oppermann 	int thflags;
44626f9a767SRodney W. Grimes 	struct socket *so = 0;
447df8bae1dSRodney W. Grimes 	int todrop, acked, ourfinisacked, needoutput = 0;
448a0292f23SGarrett Wollman 	u_long tiwin;
449a0292f23SGarrett Wollman 	struct tcpopt to;		/* options in this segment */
450f76fcf6dSJeffrey Hsu 	int headlocked = 0;
4519b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
4529b932e9eSAndre Oppermann 	struct m_tag *fwd_tag;
4539b932e9eSAndre Oppermann #endif
454c068736aSJeffrey Hsu 	int rstreason; /* For badport_bandlim accounting purposes */
455c068736aSJeffrey Hsu 
456c068736aSJeffrey Hsu 	struct ip6_hdr *ip6 = NULL;
457c068736aSJeffrey Hsu #ifdef INET6
458c068736aSJeffrey Hsu 	int isipv6;
4591d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
460c068736aSJeffrey Hsu #else
461c068736aSJeffrey Hsu 	const int isipv6 = 0;
462c068736aSJeffrey Hsu #endif
463f76fcf6dSJeffrey Hsu 
464610ee2f9SDavid Greenman #ifdef TCPDEBUG
465c068736aSJeffrey Hsu 	/*
466c068736aSJeffrey Hsu 	 * The size of tcp_saveipgen must be the size of the max ip header,
467c068736aSJeffrey Hsu 	 * now IPv6.
468c068736aSJeffrey Hsu 	 */
469410bb1bfSLuigi Rizzo 	u_char tcp_saveipgen[40];
470410bb1bfSLuigi Rizzo 	struct tcphdr tcp_savetcp;
471610ee2f9SDavid Greenman 	short ostate = 0;
472610ee2f9SDavid Greenman #endif
473c068736aSJeffrey Hsu 
474fb59c426SYoshinobu Inoue #ifdef INET6
475fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
476fb59c426SYoshinobu Inoue #endif
477a0292f23SGarrett Wollman 	bzero((char *)&to, sizeof(to));
478a0292f23SGarrett Wollman 
479df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvtotal++;
480fb59c426SYoshinobu Inoue 
481fb59c426SYoshinobu Inoue 	if (isipv6) {
48204d3a452SHajimu UMEMOTO #ifdef INET6
483fb59c426SYoshinobu Inoue 		/* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
484fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
485fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
486fb59c426SYoshinobu Inoue 		if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) {
487fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbadsum++;
488fb59c426SYoshinobu Inoue 			goto drop;
489fb59c426SYoshinobu Inoue 		}
490fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
49133841545SHajimu UMEMOTO 
49233841545SHajimu UMEMOTO 		/*
49333841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
49433841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
49533841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
49633841545SHajimu UMEMOTO 		 *
49733841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
49833841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
49933841545SHajimu UMEMOTO 		 */
50033841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
50133841545SHajimu UMEMOTO 			/* XXX stat */
50233841545SHajimu UMEMOTO 			goto drop;
50333841545SHajimu UMEMOTO 		}
50404d3a452SHajimu UMEMOTO #else
50504d3a452SHajimu UMEMOTO 		th = NULL;		/* XXX: avoid compiler warning */
50604d3a452SHajimu UMEMOTO #endif
507c068736aSJeffrey Hsu 	} else {
508df8bae1dSRodney W. Grimes 		/*
509df8bae1dSRodney W. Grimes 		 * Get IP and TCP header together in first mbuf.
510df8bae1dSRodney W. Grimes 		 * Note: IP leaves IP header in first mbuf.
511df8bae1dSRodney W. Grimes 		 */
512fb59c426SYoshinobu Inoue 		if (off0 > sizeof (struct ip)) {
513df8bae1dSRodney W. Grimes 			ip_stripoptions(m, (struct mbuf *)0);
514fb59c426SYoshinobu Inoue 			off0 = sizeof(struct ip);
515fb59c426SYoshinobu Inoue 		}
516df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof (struct tcpiphdr)) {
517df8bae1dSRodney W. Grimes 			if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
518df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvshort++;
519df8bae1dSRodney W. Grimes 				return;
520df8bae1dSRodney W. Grimes 			}
521df8bae1dSRodney W. Grimes 		}
522fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
523fb59c426SYoshinobu Inoue 		ipov = (struct ipovly *)ip;
524db4f9cc7SJonathan Lemon 		th = (struct tcphdr *)((caddr_t)ip + off0);
525db4f9cc7SJonathan Lemon 		tlen = ip->ip_len;
526df8bae1dSRodney W. Grimes 
527db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
528db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
529db4f9cc7SJonathan Lemon 				th->th_sum = m->m_pkthdr.csum_data;
530db4f9cc7SJonathan Lemon 			else
531db4f9cc7SJonathan Lemon 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
532c068736aSJeffrey Hsu 						ip->ip_dst.s_addr,
533c068736aSJeffrey Hsu 						htonl(m->m_pkthdr.csum_data +
534c068736aSJeffrey Hsu 							ip->ip_len +
535c068736aSJeffrey Hsu 							IPPROTO_TCP));
536db4f9cc7SJonathan Lemon 			th->th_sum ^= 0xffff;
5373c653157SHartmut Brandt #ifdef TCPDEBUG
5383c653157SHartmut Brandt 			ipov->ih_len = (u_short)tlen;
5393c653157SHartmut Brandt 			ipov->ih_len = htons(ipov->ih_len);
5403c653157SHartmut Brandt #endif
541db4f9cc7SJonathan Lemon 		} else {
542df8bae1dSRodney W. Grimes 			/*
543df8bae1dSRodney W. Grimes 			 * Checksum extended TCP header and data.
544df8bae1dSRodney W. Grimes 			 */
545df8bae1dSRodney W. Grimes 			len = sizeof (struct ip) + tlen;
546fb59c426SYoshinobu Inoue 			bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
547fb59c426SYoshinobu Inoue 			ipov->ih_len = (u_short)tlen;
548fd8e4ebcSMike Barcroft 			ipov->ih_len = htons(ipov->ih_len);
549fb59c426SYoshinobu Inoue 			th->th_sum = in_cksum(m, len);
550db4f9cc7SJonathan Lemon 		}
551fb59c426SYoshinobu Inoue 		if (th->th_sum) {
552df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbadsum++;
553df8bae1dSRodney W. Grimes 			goto drop;
554df8bae1dSRodney W. Grimes 		}
555fb59c426SYoshinobu Inoue 		/* Re-initialization for later version check */
556fb59c426SYoshinobu Inoue 		ip->ip_v = IPVERSION;
557fb59c426SYoshinobu Inoue 	}
558df8bae1dSRodney W. Grimes 
559df8bae1dSRodney W. Grimes 	/*
560df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
561df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
562df8bae1dSRodney W. Grimes 	 */
563fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
564df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
565df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvbadoff++;
566df8bae1dSRodney W. Grimes 		goto drop;
567df8bae1dSRodney W. Grimes 	}
568fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
569df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
570fb59c426SYoshinobu Inoue 		if (isipv6) {
57104d3a452SHajimu UMEMOTO #ifdef INET6
572fb59c426SYoshinobu Inoue 			IP6_EXTHDR_CHECK(m, off0, off, );
573fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
574fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
57504d3a452SHajimu UMEMOTO #endif
576c068736aSJeffrey Hsu 		} else {
577df8bae1dSRodney W. Grimes 			if (m->m_len < sizeof(struct ip) + off) {
578c068736aSJeffrey Hsu 				if ((m = m_pullup(m, sizeof (struct ip) + off))
579c068736aSJeffrey Hsu 						== 0) {
580df8bae1dSRodney W. Grimes 					tcpstat.tcps_rcvshort++;
581df8bae1dSRodney W. Grimes 					return;
582df8bae1dSRodney W. Grimes 				}
583fb59c426SYoshinobu Inoue 				ip = mtod(m, struct ip *);
584fb59c426SYoshinobu Inoue 				ipov = (struct ipovly *)ip;
585fb59c426SYoshinobu Inoue 				th = (struct tcphdr *)((caddr_t)ip + off0);
586fb59c426SYoshinobu Inoue 			}
587df8bae1dSRodney W. Grimes 		}
588df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
589fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
590df8bae1dSRodney W. Grimes 	}
591fb59c426SYoshinobu Inoue 	thflags = th->th_flags;
592df8bae1dSRodney W. Grimes 
593e46cd3d4SDag-Erling Smørgrav 	/*
594e46cd3d4SDag-Erling Smørgrav 	 * If the drop_synfin option is enabled, drop all packets with
595e46cd3d4SDag-Erling Smørgrav 	 * both the SYN and FIN bits set. This prevents e.g. nmap from
596e46cd3d4SDag-Erling Smørgrav 	 * identifying the TCP/IP stack.
597e46cd3d4SDag-Erling Smørgrav 	 *
598a589a70eSGarrett Wollman 	 * This is a violation of the TCP specification.
599e46cd3d4SDag-Erling Smørgrav 	 */
600fb59c426SYoshinobu Inoue 	if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN))
601e46cd3d4SDag-Erling Smørgrav 		goto drop;
602e46cd3d4SDag-Erling Smørgrav 
603df8bae1dSRodney W. Grimes 	/*
604df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
605df8bae1dSRodney W. Grimes 	 */
606fd8e4ebcSMike Barcroft 	th->th_seq = ntohl(th->th_seq);
607fd8e4ebcSMike Barcroft 	th->th_ack = ntohl(th->th_ack);
608fd8e4ebcSMike Barcroft 	th->th_win = ntohs(th->th_win);
609fd8e4ebcSMike Barcroft 	th->th_urp = ntohs(th->th_urp);
610df8bae1dSRodney W. Grimes 
611df8bae1dSRodney W. Grimes 	/*
612794235b7SAndre Oppermann 	 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options.
613755c1f07SAndras Olah 	 */
614fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
615755c1f07SAndras Olah 
616755c1f07SAndras Olah 	/*
617df8bae1dSRodney W. Grimes 	 * Locate pcb for segment.
618df8bae1dSRodney W. Grimes 	 */
619f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&tcbinfo);
620f76fcf6dSJeffrey Hsu 	headlocked = 1;
621df8bae1dSRodney W. Grimes findpcb:
622de30ea13SRobert Watson 	KASSERT(headlocked, ("tcp_input: findpcb: head not locked"));
6239b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
6249b932e9eSAndre Oppermann 	/* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */
6259b932e9eSAndre Oppermann 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
6269b932e9eSAndre Oppermann 
6279b932e9eSAndre Oppermann 	if (fwd_tag != NULL && isipv6 == 0) {	/* IPv6 support is not yet */
6289b932e9eSAndre Oppermann 		struct sockaddr_in *next_hop;
6299b932e9eSAndre Oppermann 
6309b932e9eSAndre Oppermann 		next_hop = (struct sockaddr_in *)(fwd_tag+1);
631f9e354dfSJulian Elischer 		/*
6322b25acc1SLuigi Rizzo 		 * Transparently forwarded. Pretend to be the destination.
633f9e354dfSJulian Elischer 		 * already got one like this?
634f9e354dfSJulian Elischer 		 */
6359b932e9eSAndre Oppermann 		inp = in_pcblookup_hash(&tcbinfo,
6369b932e9eSAndre Oppermann 					ip->ip_src, th->th_sport,
637c068736aSJeffrey Hsu 					ip->ip_dst, th->th_dport,
638c068736aSJeffrey Hsu 					0, m->m_pkthdr.rcvif);
639f9e354dfSJulian Elischer 		if (!inp) {
6409b932e9eSAndre Oppermann 			/* It's new.  Try to find the ambushing socket. */
641f9e354dfSJulian Elischer 			inp = in_pcblookup_hash(&tcbinfo,
642fb59c426SYoshinobu Inoue 						ip->ip_src, th->th_sport,
6432b25acc1SLuigi Rizzo 						next_hop->sin_addr,
644c068736aSJeffrey Hsu 						next_hop->sin_port ?
645c068736aSJeffrey Hsu 						    ntohs(next_hop->sin_port) :
646c068736aSJeffrey Hsu 						    th->th_dport,
647421d8aa6SBjoern A. Zeeb 						INPLOOKUP_WILDCARD,
648421d8aa6SBjoern A. Zeeb 						m->m_pkthdr.rcvif);
649f9e354dfSJulian Elischer 		}
6509b932e9eSAndre Oppermann 		/* Remove the tag from the packet.  We don't need it anymore. */
6519b932e9eSAndre Oppermann 		m_tag_delete(m, fwd_tag);
652b10fbdeaSAndre Oppermann 	} else
6539b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */
654b10fbdeaSAndre Oppermann 	{
65504d3a452SHajimu UMEMOTO 		if (isipv6) {
65604d3a452SHajimu UMEMOTO #ifdef INET6
657c068736aSJeffrey Hsu 			inp = in6_pcblookup_hash(&tcbinfo,
658c068736aSJeffrey Hsu 						 &ip6->ip6_src, th->th_sport,
659c068736aSJeffrey Hsu 						 &ip6->ip6_dst, th->th_dport,
660421d8aa6SBjoern A. Zeeb 						 INPLOOKUP_WILDCARD,
661421d8aa6SBjoern A. Zeeb 						 m->m_pkthdr.rcvif);
66204d3a452SHajimu UMEMOTO #endif
66304d3a452SHajimu UMEMOTO 		} else
664c068736aSJeffrey Hsu 			inp = in_pcblookup_hash(&tcbinfo,
665c068736aSJeffrey Hsu 						ip->ip_src, th->th_sport,
666c068736aSJeffrey Hsu 						ip->ip_dst, th->th_dport,
667421d8aa6SBjoern A. Zeeb 						INPLOOKUP_WILDCARD,
668421d8aa6SBjoern A. Zeeb 						m->m_pkthdr.rcvif);
669fb59c426SYoshinobu Inoue 	}
670f9e354dfSJulian Elischer 
671da0f4099SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC)
672d420fcdaSBruce M Simpson #ifdef INET6
673da0f4099SHajimu UMEMOTO 	if (isipv6) {
674da0f4099SHajimu UMEMOTO 		if (inp != NULL && ipsec6_in_reject(m, inp)) {
675fb59c426SYoshinobu Inoue #ifdef IPSEC
676fb59c426SYoshinobu Inoue 			ipsec6stat.in_polvio++;
677d420fcdaSBruce M Simpson #endif
678fb59c426SYoshinobu Inoue 			goto drop;
679fb59c426SYoshinobu Inoue 		}
680d420fcdaSBruce M Simpson 	} else
681d420fcdaSBruce M Simpson #endif /* INET6 */
682d420fcdaSBruce M Simpson 	if (inp != NULL && ipsec4_in_reject(m, inp)) {
683da0f4099SHajimu UMEMOTO #ifdef IPSEC
684fb59c426SYoshinobu Inoue 		ipsecstat.in_polvio++;
685d420fcdaSBruce M Simpson #endif
686fb59c426SYoshinobu Inoue 		goto drop;
687fb59c426SYoshinobu Inoue 	}
688da0f4099SHajimu UMEMOTO #endif /*IPSEC || FAST_IPSEC*/
689df8bae1dSRodney W. Grimes 
690df8bae1dSRodney W. Grimes 	/*
691574b6964SAndre Oppermann 	 * If the INPCB does not exist then all data in the incoming
692574b6964SAndre Oppermann 	 * segment is discarded and an appropriate RST is sent back.
693df8bae1dSRodney W. Grimes 	 */
694816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
695574b6964SAndre Oppermann 		/*
696574b6964SAndre Oppermann 		 * Log communication attempts to ports that are not
697574b6964SAndre Oppermann 		 * in use.
698574b6964SAndre Oppermann 		 */
699574b6964SAndre Oppermann 		if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) ||
700574b6964SAndre Oppermann 		    tcp_log_in_vain == 2) {
701574b6964SAndre Oppermann #ifndef INET6
702fb59c426SYoshinobu Inoue 			char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"];
703574b6964SAndre Oppermann #else
704574b6964SAndre Oppermann 			char dbuf[INET6_ADDRSTRLEN+2], sbuf[INET6_ADDRSTRLEN+2];
705fb59c426SYoshinobu Inoue 			if (isipv6) {
706ded7008aSJuli Mallett 				strcpy(dbuf, "[");
7071d54aa3bSBjoern A. Zeeb 				strcat(dbuf,
7081d54aa3bSBjoern A. Zeeb 				    ip6_sprintf(ip6buf, &ip6->ip6_dst));
709574b6964SAndre Oppermann 				strcat(dbuf, "]");
710574b6964SAndre Oppermann 				strcpy(sbuf, "[");
7111d54aa3bSBjoern A. Zeeb 				strcat(sbuf,
7121d54aa3bSBjoern A. Zeeb 				    ip6_sprintf(ip6buf, &ip6->ip6_src));
713ded7008aSJuli Mallett 				strcat(sbuf, "]");
714574b6964SAndre Oppermann 			} else
715574b6964SAndre Oppermann #endif /* INET6 */
716574b6964SAndre Oppermann 			{
717fb59c426SYoshinobu Inoue 				strcpy(dbuf, inet_ntoa(ip->ip_dst));
718fb59c426SYoshinobu Inoue 				strcpy(sbuf, inet_ntoa(ip->ip_src));
719fb59c426SYoshinobu Inoue 			}
7202e4e1b4cSGeoff Rehmet 			log(LOG_INFO,
721c068736aSJeffrey Hsu 			    "Connection attempt to TCP %s:%d "
72239eb27a4SCrist J. Clark 			    "from %s:%d flags:0x%02x\n",
723fb59c426SYoshinobu Inoue 			    dbuf, ntohs(th->th_dport), sbuf,
724fb59c426SYoshinobu Inoue 			    ntohs(th->th_sport), thflags);
7252e4e1b4cSGeoff Rehmet 		}
726574b6964SAndre Oppermann 		/*
727574b6964SAndre Oppermann 		 * When blackholing do not respond with a RST but
728574b6964SAndre Oppermann 		 * completely ignore the segment and drop it.
729574b6964SAndre Oppermann 		 */
730574b6964SAndre Oppermann 		if ((blackhole == 1 && (thflags & TH_SYN)) ||
731574b6964SAndre Oppermann 		    blackhole == 2)
732828b7f40SGeoff Rehmet 			goto drop;
733574b6964SAndre Oppermann 
734a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
735a57815efSBosko Milekic 		goto dropwithreset;
736816a3d83SPoul-Henning Kamp 	}
737f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
738936cd18dSAndre Oppermann 
739936cd18dSAndre Oppermann 	/* Check the minimum TTL for socket. */
74034f83c52SGeorge V. Neville-Neil 	if (inp->inp_ip_minttl != 0) {
74134f83c52SGeorge V. Neville-Neil #ifdef INET6
74234f83c52SGeorge V. Neville-Neil 		if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim)
743936cd18dSAndre Oppermann 			goto drop;
74402707462SAndre Oppermann 		else
74534f83c52SGeorge V. Neville-Neil #endif
74634f83c52SGeorge V. Neville-Neil 		if (inp->inp_ip_minttl > ip->ip_ttl)
74734f83c52SGeorge V. Neville-Neil 			goto drop;
74834f83c52SGeorge V. Neville-Neil 	}
749936cd18dSAndre Oppermann 
750340c35deSJonathan Lemon 	/*
751794235b7SAndre Oppermann 	 * A previous connection in TIMEWAIT state is supposed to catch
752794235b7SAndre Oppermann 	 * stray or duplicate segments arriving late.  If this segment
753794235b7SAndre Oppermann 	 * was a legitimate new connection attempt the old INPCB gets
754794235b7SAndre Oppermann 	 * removed and we can try again to find a listening socket.
755340c35deSJonathan Lemon 	 */
756794235b7SAndre Oppermann 	if (inp->inp_vflag & INP_TIMEWAIT) {
757340c35deSJonathan Lemon 		if (thflags & TH_SYN)
758f72167f4SAndre Oppermann 			tcp_dooptions(&to, optp, optlen, TO_SYN);
7593cbe7fafSRobert Watson 		if (tcp_timewait(inp, &to, th, m, tlen))
760340c35deSJonathan Lemon 			goto findpcb;
761794235b7SAndre Oppermann 		/* tcp_timewait unlocks inp. */
762340c35deSJonathan Lemon 		INP_INFO_WUNLOCK(&tcbinfo);
763340c35deSJonathan Lemon 		return;
764340c35deSJonathan Lemon 	}
765794235b7SAndre Oppermann 	/*
766794235b7SAndre Oppermann 	 * The TCPCB may no longer exist if the connection is winding
767794235b7SAndre Oppermann 	 * down or it is in the CLOSED state.  Either way we drop the
768794235b7SAndre Oppermann 	 * segment and send an appropriate response.
769794235b7SAndre Oppermann 	 */
770df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
77109f81a46SBosko Milekic 	if (tp == 0) {
772f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
773a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
774a57815efSBosko Milekic 		goto dropwithreset;
77509f81a46SBosko Milekic 	}
776df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_CLOSED)
777df8bae1dSRodney W. Grimes 		goto drop;
778df8bae1dSRodney W. Grimes 
779c488362eSRobert Watson #ifdef MAC
7801f82efb3SRobert Watson 	INP_LOCK_ASSERT(inp);
781a557af22SRobert Watson 	if (mac_check_inpcb_deliver(inp, m))
782c488362eSRobert Watson 		goto drop;
783c488362eSRobert Watson #endif
784a557af22SRobert Watson 	so = inp->inp_socket;
7851c53f806SRobert Watson 	KASSERT(so != NULL, ("tcp_input: so == NULL"));
786610ee2f9SDavid Greenman #ifdef TCPDEBUG
787df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG) {
788df8bae1dSRodney W. Grimes 		ostate = tp->t_state;
789fb59c426SYoshinobu Inoue 		if (isipv6)
790540e8b7eSJeffrey Hsu 			bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6));
791fb59c426SYoshinobu Inoue 		else
792540e8b7eSJeffrey Hsu 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
793fb59c426SYoshinobu Inoue 		tcp_savetcp = *th;
794df8bae1dSRodney W. Grimes 	}
795610ee2f9SDavid Greenman #endif
796db33b3e6SAndre Oppermann 	/*
797db33b3e6SAndre Oppermann 	 * When the socket is accepting connections (the INPCB is in LISTEN
798db33b3e6SAndre Oppermann 	 * state) we look into the SYN cache if this is a new connection
799db33b3e6SAndre Oppermann 	 * attempt or the completion of a previous one.
800db33b3e6SAndre Oppermann 	 */
801540e8b7eSJeffrey Hsu 	if (so->so_options & SO_ACCEPTCONN) {
802540e8b7eSJeffrey Hsu 		struct in_conninfo inc;
803540e8b7eSJeffrey Hsu 
8048bfb1918SAndre Oppermann 		bzero(&inc, sizeof(inc));
805be2ac88cSJonathan Lemon 		inc.inc_isipv6 = isipv6;
806be2ac88cSJonathan Lemon 		if (isipv6) {
807be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
808be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
809c068736aSJeffrey Hsu 		} else {
810be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
811be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
812be2ac88cSJonathan Lemon 		}
813be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
814be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
815be2ac88cSJonathan Lemon 
816fb59c426SYoshinobu Inoue 	        /*
817be2ac88cSJonathan Lemon 	         * If the state is LISTEN then ignore segment if it contains
818be2ac88cSJonathan Lemon 		 * a RST.  If the segment contains an ACK then it is bad and
819be2ac88cSJonathan Lemon 		 * send a RST.  If it does not contain a SYN then it is not
820be2ac88cSJonathan Lemon 		 * interesting; drop it.
821be2ac88cSJonathan Lemon 		 *
822be2ac88cSJonathan Lemon 		 * If the state is SYN_RECEIVED (syncache) and seg contains
823be2ac88cSJonathan Lemon 		 * an ACK, but not for our SYN/ACK, send a RST.  If the seg
824be2ac88cSJonathan Lemon 		 * contains a RST, check the sequence number to see if it
825be2ac88cSJonathan Lemon 		 * is a valid reset segment.
826fb59c426SYoshinobu Inoue 		 */
827fb59c426SYoshinobu Inoue 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
828be2ac88cSJonathan Lemon 			if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
829bf6d304aSAndre Oppermann 				/*
830bf6d304aSAndre Oppermann 				 * Parse the TCP options here because
831bf6d304aSAndre Oppermann 				 * syncookies need access to the reflected
832bf6d304aSAndre Oppermann 				 * timestamp.
833bf6d304aSAndre Oppermann 				 */
834bf6d304aSAndre Oppermann 				tcp_dooptions(&to, optp, optlen, 0);
835bf6d304aSAndre Oppermann 				if (!syncache_expand(&inc, &to, th, &so, m)) {
8364195b4afSPaul Traina 					/*
837be2ac88cSJonathan Lemon 					 * No syncache entry, or ACK was not
838be2ac88cSJonathan Lemon 					 * for our SYN/ACK.  Send a RST.
8394195b4afSPaul Traina 					 */
840be2ac88cSJonathan Lemon 					tcpstat.tcps_badsyn++;
841be2ac88cSJonathan Lemon 					rstreason = BANDLIM_RST_OPENPORT;
842be2ac88cSJonathan Lemon 					goto dropwithreset;
843be2ac88cSJonathan Lemon 				}
844f76fcf6dSJeffrey Hsu 				if (so == NULL) {
845be2ac88cSJonathan Lemon 					/*
846be2ac88cSJonathan Lemon 					 * Could not complete 3-way handshake,
847be2ac88cSJonathan Lemon 					 * connection is being closed down, and
848464fcfbcSAndre Oppermann 					 * syncache has free'd mbuf.
849be2ac88cSJonathan Lemon 					 */
850f76fcf6dSJeffrey Hsu 					INP_UNLOCK(inp);
851f76fcf6dSJeffrey Hsu 					INP_INFO_WUNLOCK(&tcbinfo);
852be2ac88cSJonathan Lemon 					return;
853f76fcf6dSJeffrey Hsu 				}
854be2ac88cSJonathan Lemon 				/*
855be2ac88cSJonathan Lemon 				 * Socket is created in state SYN_RECEIVED.
856be2ac88cSJonathan Lemon 				 * Continue processing segment.
857be2ac88cSJonathan Lemon 				 */
858f76fcf6dSJeffrey Hsu 				INP_UNLOCK(inp);
859be2ac88cSJonathan Lemon 				inp = sotoinpcb(so);
860f76fcf6dSJeffrey Hsu 				INP_LOCK(inp);
861be2ac88cSJonathan Lemon 				tp = intotcpcb(inp);
862be2ac88cSJonathan Lemon 				/*
863be2ac88cSJonathan Lemon 				 * This is what would have happened in
864e6658b12SRobert Watson 				 * tcp_output() when the SYN,ACK was sent.
865be2ac88cSJonathan Lemon 				 */
866be2ac88cSJonathan Lemon 				tp->snd_up = tp->snd_una;
867be2ac88cSJonathan Lemon 				tp->snd_max = tp->snd_nxt = tp->iss + 1;
868be2ac88cSJonathan Lemon 				tp->last_ack_sent = tp->rcv_nxt;
869be2ac88cSJonathan Lemon 				goto after_listen;
870be2ac88cSJonathan Lemon 			}
871be2ac88cSJonathan Lemon 			if (thflags & TH_RST) {
872be2ac88cSJonathan Lemon 				syncache_chkrst(&inc, th);
873be2ac88cSJonathan Lemon 				goto drop;
874be2ac88cSJonathan Lemon 			}
875fb59c426SYoshinobu Inoue 			if (thflags & TH_ACK) {
876be2ac88cSJonathan Lemon 				syncache_badack(&inc);
877ebb0cbeaSPaul Traina 				tcpstat.tcps_badsyn++;
878a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
879a57815efSBosko Milekic 				goto dropwithreset;
8804195b4afSPaul Traina 			}
881ebb0cbeaSPaul Traina 			goto drop;
882ebb0cbeaSPaul Traina 		}
88333841545SHajimu UMEMOTO 
884be2ac88cSJonathan Lemon 		/*
885be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
886be2ac88cSJonathan Lemon 		 */
88733841545SHajimu UMEMOTO #ifdef INET6
88833841545SHajimu UMEMOTO 		/*
88933841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
89033841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
89133841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
89233841545SHajimu UMEMOTO 		 * getting established.
89333841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
89433841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
89533841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
89633841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
89733841545SHajimu UMEMOTO 		 * for the exchange.
89833841545SHajimu UMEMOTO 		 *
89933841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
90033841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
90133841545SHajimu UMEMOTO 		 * SYN in this case.
90233841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
90333841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
90433841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
90533841545SHajimu UMEMOTO 		 *    used"
90633841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
90733841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
90833841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
90933841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
91033841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
91133841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
91233841545SHajimu UMEMOTO 		 *
91333841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
91433841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
91533841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
91633841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
91733841545SHajimu UMEMOTO 		 */
91833841545SHajimu UMEMOTO 		if (isipv6 && !ip6_use_deprecated) {
91933841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
92033841545SHajimu UMEMOTO 
92133841545SHajimu UMEMOTO 			if ((ia6 = ip6_getdstifaddr(m)) &&
92233841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
923f76fcf6dSJeffrey Hsu 				INP_UNLOCK(inp);
92433841545SHajimu UMEMOTO 				tp = NULL;
92533841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
92633841545SHajimu UMEMOTO 				goto dropwithreset;
92733841545SHajimu UMEMOTO 			}
92833841545SHajimu UMEMOTO 		}
92933841545SHajimu UMEMOTO #endif
93065f28919SJesper Skriver 		/*
931db33b3e6SAndre Oppermann 		 * Basic sanity checks on incoming SYN requests:
932db33b3e6SAndre Oppermann 		 *
933db33b3e6SAndre Oppermann 		 * Don't bother responding if the destination was a
934db33b3e6SAndre Oppermann 		 * broadcast according to RFC1122 4.2.3.10, p. 104.
935db33b3e6SAndre Oppermann 		 *
936be2ac88cSJonathan Lemon 		 * If it is from this socket, drop it, it must be forged.
9372ca2159fSCrist J. Clark 		 *
93893ec91baSCrist J. Clark 		 * Note that it is quite possible to receive unicast
93993ec91baSCrist J. Clark 		 * link-layer packets with a broadcast IP address. Use
94093ec91baSCrist J. Clark 		 * in_broadcast() to find them.
941be2ac88cSJonathan Lemon 		 */
942be2ac88cSJonathan Lemon 		if (m->m_flags & (M_BCAST|M_MCAST))
943be2ac88cSJonathan Lemon 			goto drop;
944be2ac88cSJonathan Lemon 		if (isipv6) {
945db33b3e6SAndre Oppermann #ifdef INET6
946db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
947db33b3e6SAndre Oppermann 			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src))
948db33b3e6SAndre Oppermann 				goto drop;
949be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
950be2ac88cSJonathan Lemon 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
951be2ac88cSJonathan Lemon 				goto drop;
952db33b3e6SAndre Oppermann #endif
953c068736aSJeffrey Hsu 		} else {
954db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
955db33b3e6SAndre Oppermann 			    ip->ip_dst.s_addr == ip->ip_src.s_addr)
956db33b3e6SAndre Oppermann 				goto drop;
957be2ac88cSJonathan Lemon 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
958be2ac88cSJonathan Lemon 			    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
9592ca2159fSCrist J. Clark 			    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
9602ca2159fSCrist J. Clark 			    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
961be2ac88cSJonathan Lemon 				goto drop;
962c068736aSJeffrey Hsu 		}
963be2ac88cSJonathan Lemon 		/*
964db33b3e6SAndre Oppermann 		 * SYN appears to be valid.  Create compressed TCP state
965db33b3e6SAndre Oppermann 		 * for syncache.
966be2ac88cSJonathan Lemon 		 */
967be2ac88cSJonathan Lemon 		if (so->so_qlen <= so->so_qlimit) {
9683c653157SHartmut Brandt #ifdef TCPDEBUG
9693c653157SHartmut Brandt 			if (so->so_options & SO_DEBUG)
9703c653157SHartmut Brandt 				tcp_trace(TA_INPUT, ostate, tp,
9713c653157SHartmut Brandt 				    (void *)tcp_saveipgen, &tcp_savetcp, 0);
9723c653157SHartmut Brandt #endif
973f72167f4SAndre Oppermann 			tcp_dooptions(&to, optp, optlen, TO_SYN);
974351630c4SAndre Oppermann 			if (!syncache_add(&inc, &to, th, inp, &so, m))
975db33b3e6SAndre Oppermann 				goto drop;
976be2ac88cSJonathan Lemon 			/*
977be2ac88cSJonathan Lemon 			 * Entry added to syncache, mbuf used to
978db33b3e6SAndre Oppermann 			 * send SYN-ACK packet.  Everything unlocked
979351630c4SAndre Oppermann 			 * already.
980be2ac88cSJonathan Lemon 			 */
981be2ac88cSJonathan Lemon 			return;
982f76fcf6dSJeffrey Hsu 		}
983db33b3e6SAndre Oppermann 		/* Catch all.  Everthing that makes it down here is junk. */
984be2ac88cSJonathan Lemon 		goto drop;
9854cc20ab1SSeigo Tanimura 	}
986db33b3e6SAndre Oppermann 
987be2ac88cSJonathan Lemon after_listen:
988de30ea13SRobert Watson 	KASSERT(headlocked, ("tcp_input: after_listen: head not locked"));
9897cfc6904SRobert Watson 	INP_LOCK_ASSERT(inp);
990be2ac88cSJonathan Lemon 
991a65e12b0SRobert Watson 	/* Syncache takes care of sockets in the listen state. */
992a65e12b0SRobert Watson 	KASSERT(tp->t_state != TCPS_LISTEN, ("tcp_input: TCPS_LISTEN"));
993df8bae1dSRodney W. Grimes 
994df8bae1dSRodney W. Grimes 	/*
995df8bae1dSRodney W. Grimes 	 * Segment received on connection.
996df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
997df8bae1dSRodney W. Grimes 	 */
9989b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
9997ff19458SPaul Traina 	if (TCPS_HAVEESTABLISHED(tp->t_state))
10009b8b58e0SJonathan Lemon 		callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
1001df8bae1dSRodney W. Grimes 
1002df8bae1dSRodney W. Grimes 	/*
1003464fcfbcSAndre Oppermann 	 * Unscale the window into a 32-bit value.
1004464fcfbcSAndre Oppermann 	 * This value is bogus for the TCPS_SYN_SENT state
1005464fcfbcSAndre Oppermann 	 * and is overwritten later.
1006464fcfbcSAndre Oppermann 	 */
1007464fcfbcSAndre Oppermann 	tiwin = th->th_win << tp->snd_scale;
1008464fcfbcSAndre Oppermann 
1009464fcfbcSAndre Oppermann 	/*
1010f72167f4SAndre Oppermann 	 * Parse options on any incoming segment.
1011f72167f4SAndre Oppermann 	 */
1012f72167f4SAndre Oppermann 	tcp_dooptions(&to, optp, optlen, (thflags & TH_SYN) ? TO_SYN : 0);
1013f72167f4SAndre Oppermann 
1014f72167f4SAndre Oppermann 	/*
1015f72167f4SAndre Oppermann 	 * If echoed timestamp is later than the current time,
1016bf6d304aSAndre Oppermann 	 * fall back to non RFC1323 RTT calculation.  Normalize
1017bf6d304aSAndre Oppermann 	 * timestamp if syncookies were used when this connection
1018bf6d304aSAndre Oppermann 	 * was established.
1019f72167f4SAndre Oppermann 	 */
1020bf6d304aSAndre Oppermann 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
1021e16fa5caSJohn-Mark Gurney 		to.to_tsecr -= tp->ts_offset;
1022bf6d304aSAndre Oppermann 		if (TSTMP_GT(to.to_tsecr, ticks))
1023f72167f4SAndre Oppermann 			to.to_tsecr = 0;
1024bf6d304aSAndre Oppermann 	}
1025f72167f4SAndre Oppermann 
1026f72167f4SAndre Oppermann 	/*
102797d8d152SAndre Oppermann 	 * Process options only when we get SYN/ACK back. The SYN case
102897d8d152SAndre Oppermann 	 * for incoming connections is handled in tcp_syncache.
102997d8d152SAndre Oppermann 	 * XXX this is traditional behavior, may need to be cleaned up.
1030df8bae1dSRodney W. Grimes 	 */
10310a389eabSSimon L. B. Nielsen 	if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
1032464fcfbcSAndre Oppermann 		if ((to.to_flags & TOF_SCALE) &&
1033464fcfbcSAndre Oppermann 		    (tp->t_flags & TF_REQ_SCALE)) {
1034be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
103502a1a643SAndre Oppermann 			tp->snd_scale = to.to_wscale;
1036464fcfbcSAndre Oppermann 			tp->snd_wnd = th->th_win << tp->snd_scale;
1037464fcfbcSAndre Oppermann 			tiwin = tp->snd_wnd;
1038be2ac88cSJonathan Lemon 		}
1039be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_TS) {
1040be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
1041be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
1042be2ac88cSJonathan Lemon 			tp->ts_recent_age = ticks;
1043be2ac88cSJonathan Lemon 		}
10444a32dc29SMohan Srinivasan 		/* Initial send window, already scaled. */
10454a32dc29SMohan Srinivasan 		tp->snd_wnd = th->th_win;
1046be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_MSS)
1047be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
10486d90faf3SPaul Saab 		if (tp->sack_enable) {
10496d90faf3SPaul Saab 			if (!(to.to_flags & TOF_SACK))
10506d90faf3SPaul Saab 				tp->sack_enable = 0;
10516d90faf3SPaul Saab 			else
10526d90faf3SPaul Saab 				tp->t_flags |= TF_SACK_PERMIT;
10536d90faf3SPaul Saab 		}
10546d90faf3SPaul Saab 
10556d90faf3SPaul Saab 	}
10566d90faf3SPaul Saab 
1057df8bae1dSRodney W. Grimes 	/*
1058df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
1059df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
1060df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
1061df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
1062df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
1063df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
1064df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
1065df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
1066df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
1067df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
1068df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
1069df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
1070a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
1071a0292f23SGarrett Wollman 	 * Since we check for TCPS_ESTABLISHED above, it can only
1072a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
1073df8bae1dSRodney W. Grimes 	 */
1074df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
1075fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1076a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
1077be2ac88cSJonathan Lemon 	    ((to.to_flags & TOF_TS) == 0 ||
1078a0292f23SGarrett Wollman 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
1079c94c54e4SAndre Oppermann 	     th->th_seq == tp->rcv_nxt && tiwin && tiwin == tp->snd_wnd &&
1080df8bae1dSRodney W. Grimes 	     tp->snd_nxt == tp->snd_max) {
1081df8bae1dSRodney W. Grimes 
1082df8bae1dSRodney W. Grimes 		/*
1083df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
1084df8bae1dSRodney W. Grimes 		 * record the timestamp.
1085a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
1086a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1087df8bae1dSRodney W. Grimes 		 */
1088be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
1089fb59c426SYoshinobu Inoue 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
10909b8b58e0SJonathan Lemon 			tp->ts_recent_age = ticks;
1091a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
1092df8bae1dSRodney W. Grimes 		}
1093df8bae1dSRodney W. Grimes 
1094fb59c426SYoshinobu Inoue 		if (tlen == 0) {
1095fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
1096fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
1097233e8c18SGarrett Wollman 			    tp->snd_cwnd >= tp->snd_wnd &&
10986d90faf3SPaul Saab 			    ((!tcp_do_newreno && !tp->sack_enable &&
1099cb942153SJeffrey Hsu 			      tp->t_dupacks < tcprexmtthresh) ||
11006d90faf3SPaul Saab 			     ((tcp_do_newreno || tp->sack_enable) &&
1101482ac968SPaul Saab 			      !IN_FASTRECOVERY(tp) && to.to_nsacks == 0 &&
1102482ac968SPaul Saab 			      TAILQ_EMPTY(&tp->snd_holes)))) {
1103f76fcf6dSJeffrey Hsu 				KASSERT(headlocked, ("headlocked"));
1104e0bef1cbSRobert Watson 				INP_INFO_WUNLOCK(&tcbinfo);
1105e0bef1cbSRobert Watson 				headlocked = 0;
1106df8bae1dSRodney W. Grimes 				/*
1107df8bae1dSRodney W. Grimes 				 * this is a pure ack for outstanding data.
1108df8bae1dSRodney W. Grimes 				 */
1109df8bae1dSRodney W. Grimes 				++tcpstat.tcps_predack;
11109b8b58e0SJonathan Lemon 				/*
11119b8b58e0SJonathan Lemon 				 * "bad retransmit" recovery
11129b8b58e0SJonathan Lemon 				 */
11139b8b58e0SJonathan Lemon 				if (tp->t_rxtshift == 1 &&
11149b8b58e0SJonathan Lemon 				    ticks < tp->t_badrxtwin) {
1115cb942153SJeffrey Hsu 					++tcpstat.tcps_sndrexmitbad;
11169b8b58e0SJonathan Lemon 					tp->snd_cwnd = tp->snd_cwnd_prev;
11179b8b58e0SJonathan Lemon 					tp->snd_ssthresh =
11189b8b58e0SJonathan Lemon 					    tp->snd_ssthresh_prev;
11199d11646dSJeffrey Hsu 					tp->snd_recover = tp->snd_recover_prev;
11209d11646dSJeffrey Hsu 					if (tp->t_flags & TF_WASFRECOVERY)
11219d11646dSJeffrey Hsu 					    ENTER_FASTRECOVERY(tp);
11229b8b58e0SJonathan Lemon 					tp->snd_nxt = tp->snd_max;
11239b8b58e0SJonathan Lemon 					tp->t_badrxtwin = 0;
11249b8b58e0SJonathan Lemon 				}
1125fa55172bSMatthew Dillon 
1126fa55172bSMatthew Dillon 				/*
1127fa55172bSMatthew Dillon 				 * Recalculate the transmit timer / rtt.
1128fa55172bSMatthew Dillon 				 *
1129fa55172bSMatthew Dillon 				 * Some boxes send broken timestamp replies
1130fa55172bSMatthew Dillon 				 * during the SYN+ACK phase, ignore
1131fa55172bSMatthew Dillon 				 * timestamps of 0 or we could calculate a
1132fa55172bSMatthew Dillon 				 * huge RTT and blow up the retransmit timer.
1133fa55172bSMatthew Dillon 				 */
1134fa55172bSMatthew Dillon 				if ((to.to_flags & TOF_TS) != 0 &&
1135fa55172bSMatthew Dillon 				    to.to_tsecr) {
1136eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1137eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - to.to_tsecr)
1138eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - to.to_tsecr;
1139a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
11409b8b58e0SJonathan Lemon 					    ticks - to.to_tsecr + 1);
1141fa55172bSMatthew Dillon 				} else if (tp->t_rtttime &&
1142fa55172bSMatthew Dillon 					    SEQ_GT(th->th_ack, tp->t_rtseq)) {
1143eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1144eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - tp->t_rtttime)
1145eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - tp->t_rtttime;
1146c068736aSJeffrey Hsu 					tcp_xmit_timer(tp,
1147c068736aSJeffrey Hsu 							ticks - tp->t_rtttime);
1148fa55172bSMatthew Dillon 				}
11491fcc99b5SMatthew Dillon 				tcp_xmit_bandwidth_limit(tp, th->th_ack);
1150fb59c426SYoshinobu Inoue 				acked = th->th_ack - tp->snd_una;
1151df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackpack++;
1152df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackbyte += acked;
1153df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
11549d11646dSJeffrey Hsu 				if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
11559d11646dSJeffrey Hsu 				    SEQ_LEQ(th->th_ack, tp->snd_recover))
11569d11646dSJeffrey Hsu 					tp->snd_recover = th->th_ack - 1;
11579d11646dSJeffrey Hsu 				tp->snd_una = th->th_ack;
11581645d090SJeff Roberson 				/*
11591645d090SJeff Roberson 				 * pull snd_wl2 up to prevent seq wrap relative
11601645d090SJeff Roberson 				 * to th_ack.
11611645d090SJeff Roberson 				 */
1162cb942153SJeffrey Hsu 				tp->snd_wl2 = th->th_ack;
1163b5addd85SJeffrey Hsu 				tp->t_dupacks = 0;
1164df8bae1dSRodney W. Grimes 				m_freem(m);
1165fb59c426SYoshinobu Inoue 				ND6_HINT(tp); /* some progress has been done */
1166df8bae1dSRodney W. Grimes 
1167df8bae1dSRodney W. Grimes 				/*
1168df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
1169df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
1170df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
1171df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
1172df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
1173df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
1174df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
11753c653157SHartmut Brandt 
11763c653157SHartmut Brandt #ifdef TCPDEBUG
11773c653157SHartmut Brandt 				if (so->so_options & SO_DEBUG)
11783c653157SHartmut Brandt 					tcp_trace(TA_INPUT, ostate, tp,
11793c653157SHartmut Brandt 					    (void *)tcp_saveipgen,
11803c653157SHartmut Brandt 					    &tcp_savetcp, 0);
11813c653157SHartmut Brandt #endif
1182df8bae1dSRodney W. Grimes 				 */
1183df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
11849b8b58e0SJonathan Lemon 					callout_stop(tp->tt_rexmt);
11859b8b58e0SJonathan Lemon 				else if (!callout_active(tp->tt_persist))
11869b8b58e0SJonathan Lemon 					callout_reset(tp->tt_rexmt,
11879b8b58e0SJonathan Lemon 						      tp->t_rxtcur,
11889b8b58e0SJonathan Lemon 						      tcp_timer_rexmt, tp);
1189df8bae1dSRodney W. Grimes 
1190df8bae1dSRodney W. Grimes 				sowwakeup(so);
1191df8bae1dSRodney W. Grimes 				if (so->so_snd.sb_cc)
1192df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1193a14c749fSJonathan Lemon 				goto check_delack;
1194df8bae1dSRodney W. Grimes 			}
1195fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1196fb59c426SYoshinobu Inoue 		    LIST_EMPTY(&tp->t_segq) &&
1197fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
11986741ecf5SAndre Oppermann 			int newsize = 0;	/* automatic sockbuf scaling */
11996741ecf5SAndre Oppermann 
1200f76fcf6dSJeffrey Hsu 			KASSERT(headlocked, ("headlocked"));
1201e0bef1cbSRobert Watson 			INP_INFO_WUNLOCK(&tcbinfo);
1202e0bef1cbSRobert Watson 			headlocked = 0;
1203df8bae1dSRodney W. Grimes 			/*
1204df8bae1dSRodney W. Grimes 			 * this is a pure, in-sequence data packet
1205df8bae1dSRodney W. Grimes 			 * with nothing on the reassembly queue and
1206df8bae1dSRodney W. Grimes 			 * we have enough buffer space to take it.
1207df8bae1dSRodney W. Grimes 			 */
12086d90faf3SPaul Saab 			/* Clean receiver SACK report if present */
12096d90faf3SPaul Saab 			if (tp->sack_enable && tp->rcv_numsacks)
12106d90faf3SPaul Saab 				tcp_clean_sackreport(tp);
1211df8bae1dSRodney W. Grimes 			++tcpstat.tcps_preddat;
1212fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
12131645d090SJeff Roberson 			/*
12141645d090SJeff Roberson 			 * Pull snd_wl1 up to prevent seq wrap relative to
12151645d090SJeff Roberson 			 * th_seq.
12161645d090SJeff Roberson 			 */
12171645d090SJeff Roberson 			tp->snd_wl1 = th->th_seq;
12181645d090SJeff Roberson 			/*
12191645d090SJeff Roberson 			 * Pull rcv_up up to prevent seq wrap relative to
12201645d090SJeff Roberson 			 * rcv_nxt.
12211645d090SJeff Roberson 			 */
12221645d090SJeff Roberson 			tp->rcv_up = tp->rcv_nxt;
1223df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpack++;
1224fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbyte += tlen;
1225fb59c426SYoshinobu Inoue 			ND6_HINT(tp);	/* some progress has been done */
12263c653157SHartmut Brandt #ifdef TCPDEBUG
12273c653157SHartmut Brandt 			if (so->so_options & SO_DEBUG)
12283c653157SHartmut Brandt 				tcp_trace(TA_INPUT, ostate, tp,
12293c653157SHartmut Brandt 				    (void *)tcp_saveipgen, &tcp_savetcp, 0);
12303c653157SHartmut Brandt #endif
12316741ecf5SAndre Oppermann 		/*
12326741ecf5SAndre Oppermann 		 * Automatic sizing of receive socket buffer.  Often the send
12336741ecf5SAndre Oppermann 		 * buffer size is not optimally adjusted to the actual network
12346741ecf5SAndre Oppermann 		 * conditions at hand (delay bandwidth product).  Setting the
12356741ecf5SAndre Oppermann 		 * buffer size too small limits throughput on links with high
12366741ecf5SAndre Oppermann 		 * bandwidth and high delay (eg. trans-continental/oceanic links).
12376741ecf5SAndre Oppermann 		 *
12386741ecf5SAndre Oppermann 		 * On the receive side the socket buffer memory is only rarely
12396741ecf5SAndre Oppermann 		 * used to any significant extent.  This allows us to be much
12406741ecf5SAndre Oppermann 		 * more aggressive in scaling the receive socket buffer.  For
12416741ecf5SAndre Oppermann 		 * the case that the buffer space is actually used to a large
12426741ecf5SAndre Oppermann 		 * extent and we run out of kernel memory we can simply drop
12436741ecf5SAndre Oppermann 		 * the new segments; TCP on the sender will just retransmit it
12446741ecf5SAndre Oppermann 		 * later.  Setting the buffer size too big may only consume too
12456741ecf5SAndre Oppermann 		 * much kernel memory if the application doesn't read() from
12466741ecf5SAndre Oppermann 		 * the socket or packet loss or reordering makes use of the
12476741ecf5SAndre Oppermann 		 * reassembly queue.
12486741ecf5SAndre Oppermann 		 *
12496741ecf5SAndre Oppermann 		 * The criteria to step up the receive buffer one notch are:
12506741ecf5SAndre Oppermann 		 *  1. the number of bytes received during the time it takes
12516741ecf5SAndre Oppermann 		 *     one timestamp to be reflected back to us (the RTT);
12526741ecf5SAndre Oppermann 		 *  2. received bytes per RTT is within seven eighth of the
12536741ecf5SAndre Oppermann 		 *     current socket buffer size;
12546741ecf5SAndre Oppermann 		 *  3. receive buffer size has not hit maximal automatic size;
12556741ecf5SAndre Oppermann 		 *
12566741ecf5SAndre Oppermann 		 * This algorithm does one step per RTT at most and only if
12576741ecf5SAndre Oppermann 		 * we receive a bulk stream w/o packet losses or reorderings.
12586741ecf5SAndre Oppermann 		 * Shrinking the buffer during idle times is not necessary as
12596741ecf5SAndre Oppermann 		 * it doesn't consume any memory when idle.
12606741ecf5SAndre Oppermann 		 *
12616741ecf5SAndre Oppermann 		 * TODO: Only step up if the application is actually serving
12626741ecf5SAndre Oppermann 		 * the buffer to better manage the socket buffer resources.
1263df8bae1dSRodney W. Grimes 		 */
12646741ecf5SAndre Oppermann 			if (tcp_do_autorcvbuf &&
12656741ecf5SAndre Oppermann 			    to.to_tsecr &&
12666741ecf5SAndre Oppermann 			    (so->so_rcv.sb_flags & SB_AUTOSIZE)) {
12676741ecf5SAndre Oppermann 				if (to.to_tsecr > tp->rfbuf_ts &&
12686741ecf5SAndre Oppermann 				    to.to_tsecr - tp->rfbuf_ts < hz) {
12696741ecf5SAndre Oppermann 					if (tp->rfbuf_cnt >
12706741ecf5SAndre Oppermann 					    (so->so_rcv.sb_hiwat / 8 * 7) &&
12716741ecf5SAndre Oppermann 					    so->so_rcv.sb_hiwat <
12726741ecf5SAndre Oppermann 					    tcp_autorcvbuf_max) {
12736741ecf5SAndre Oppermann 						newsize =
12746741ecf5SAndre Oppermann 						    min(so->so_rcv.sb_hiwat +
12756741ecf5SAndre Oppermann 						    tcp_autorcvbuf_inc,
12766741ecf5SAndre Oppermann 						    tcp_autorcvbuf_max);
12776741ecf5SAndre Oppermann 					}
12786741ecf5SAndre Oppermann 					/* Start over with next RTT. */
12796741ecf5SAndre Oppermann 					tp->rfbuf_ts = 0;
12806741ecf5SAndre Oppermann 					tp->rfbuf_cnt = 0;
12816741ecf5SAndre Oppermann 				} else
12826741ecf5SAndre Oppermann 					tp->rfbuf_cnt += tlen;	/* add up */
12836741ecf5SAndre Oppermann 			}
12846741ecf5SAndre Oppermann 
12856741ecf5SAndre Oppermann 			/* Add data to socket buffer. */
12861e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
1287c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1288c1c36a2cSMike Silbersack 				m_freem(m);
1289c1c36a2cSMike Silbersack 			} else {
12906741ecf5SAndre Oppermann 				/*
12916741ecf5SAndre Oppermann 				 * Set new socket buffer size.
12926741ecf5SAndre Oppermann 				 * Give up when limit is reached.
12936741ecf5SAndre Oppermann 				 */
12946741ecf5SAndre Oppermann 				if (newsize)
12956741ecf5SAndre Oppermann 					if (!sbreserve_locked(&so->so_rcv,
12966741ecf5SAndre Oppermann 					    newsize, so, curthread))
12976741ecf5SAndre Oppermann 						so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1298fb59c426SYoshinobu Inoue 				m_adj(m, drop_hdrlen);	/* delayed header drop */
12991e4d7da7SRobert Watson 				sbappendstream_locked(&so->so_rcv, m);
1300c1c36a2cSMike Silbersack 			}
13011e4d7da7SRobert Watson 			sorwakeup_locked(so);
1302d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp)) {
13033bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
1304f498eeeeSDavid Greenman 			} else {
1305e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
1306e612a582SDavid Greenman 				tcp_output(tp);
1307e612a582SDavid Greenman 			}
1308a14c749fSJonathan Lemon 			goto check_delack;
1309df8bae1dSRodney W. Grimes 		}
1310df8bae1dSRodney W. Grimes 	}
1311df8bae1dSRodney W. Grimes 
1312df8bae1dSRodney W. Grimes 	/*
1313df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1314df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1315df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1316df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1317df8bae1dSRodney W. Grimes 	 */
1318df8bae1dSRodney W. Grimes 	{ int win;
1319df8bae1dSRodney W. Grimes 
1320df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1321df8bae1dSRodney W. Grimes 	if (win < 0)
1322df8bae1dSRodney W. Grimes 		win = 0;
132366e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1324df8bae1dSRodney W. Grimes 	}
1325df8bae1dSRodney W. Grimes 
13266741ecf5SAndre Oppermann 	/* Reset receive buffer auto scaling when not in bulk receive mode. */
13276741ecf5SAndre Oppermann 	tp->rfbuf_ts = 0;
13286741ecf5SAndre Oppermann 	tp->rfbuf_cnt = 0;
13296741ecf5SAndre Oppermann 
1330df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1331df8bae1dSRodney W. Grimes 
1332df8bae1dSRodney W. Grimes 	/*
1333764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1334764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1335764d8cefSBill Fenner 	 */
1336764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1337fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1338fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1339a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1340a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1341a57815efSBosko Milekic 				goto dropwithreset;
1342a57815efSBosko Milekic 		}
1343764d8cefSBill Fenner 		break;
1344764d8cefSBill Fenner 
1345764d8cefSBill Fenner 	/*
1346df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
1347df8bae1dSRodney W. Grimes 	 *	if seg contains an ACK, but not for our SYN, drop the input.
1348df8bae1dSRodney W. Grimes 	 *	if seg contains a RST, then drop the connection.
1349df8bae1dSRodney W. Grimes 	 *	if seg does not contain SYN, then drop it.
1350df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
1351df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
1352df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
1353df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1354df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
1355df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
1356df8bae1dSRodney W. Grimes 	 */
1357df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
1358fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1359fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
1360fb59c426SYoshinobu Inoue 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1361a57815efSBosko Milekic 			rstreason = BANDLIM_UNLIMITED;
1362a0292f23SGarrett Wollman 			goto dropwithreset;
1363a0292f23SGarrett Wollman 		}
1364fb59c426SYoshinobu Inoue 		if (thflags & TH_RST) {
13651e2d989dSRobert Watson 			if (thflags & TH_ACK) {
13661e2d989dSRobert Watson 				KASSERT(headlocked, ("tcp_input: after_listen"
13671e2d989dSRobert Watson 				    ": tcp_drop.2: head not locked"));
1368df8bae1dSRodney W. Grimes 				tp = tcp_drop(tp, ECONNREFUSED);
13691e2d989dSRobert Watson 			}
1370df8bae1dSRodney W. Grimes 			goto drop;
1371df8bae1dSRodney W. Grimes 		}
1372fb59c426SYoshinobu Inoue 		if ((thflags & TH_SYN) == 0)
1373df8bae1dSRodney W. Grimes 			goto drop;
1374464fcfbcSAndre Oppermann 
1375fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
1376df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
1377fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1378845799c1SAndras Olah 			tcpstat.tcps_connects++;
1379845799c1SAndras Olah 			soisconnected(so);
1380c488362eSRobert Watson #ifdef MAC
1381310e7cebSRobert Watson 			SOCK_LOCK(so);
1382c488362eSRobert Watson 			mac_set_socket_peer_from_mbuf(m, so);
1383310e7cebSRobert Watson 			SOCK_UNLOCK(so);
1384c488362eSRobert Watson #endif
1385845799c1SAndras Olah 			/* Do window scaling on this connection? */
1386845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1387845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1388845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
1389845799c1SAndras Olah 			}
1390a0292f23SGarrett Wollman 			tp->rcv_adv += tp->rcv_wnd;
1391a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
1392a0292f23SGarrett Wollman 			/*
1393a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
1394a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
1395a0292f23SGarrett Wollman 			 */
1396d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp) && tlen != 0)
13979b8b58e0SJonathan Lemon 				callout_reset(tp->tt_delack, tcp_delacktime,
13989b8b58e0SJonathan Lemon 				    tcp_timer_delack, tp);
1399a0292f23SGarrett Wollman 			else
1400a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
1401a0292f23SGarrett Wollman 			/*
1402a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
1403a0292f23SGarrett Wollman 			 * Transitions:
1404a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
1405a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
1406a0292f23SGarrett Wollman 			 */
14079b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
1408a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
1409a0292f23SGarrett Wollman 				tp->t_state = TCPS_FIN_WAIT_1;
1410a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
1411fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
14127ff19458SPaul Traina 			} else {
1413a0292f23SGarrett Wollman 				tp->t_state = TCPS_ESTABLISHED;
14149b8b58e0SJonathan Lemon 				callout_reset(tp->tt_keep, tcp_keepidle,
14159b8b58e0SJonathan Lemon 					      tcp_timer_keep, tp);
14167ff19458SPaul Traina 			}
1417a0292f23SGarrett Wollman 		} else {
1418a0292f23SGarrett Wollman 			/*
1419c068736aSJeffrey Hsu 			 * Received initial SYN in SYN-SENT[*] state =>
1420c068736aSJeffrey Hsu 			 * simultaneous open.  If segment contains CC option
1421c068736aSJeffrey Hsu 			 * and there is a cached CC, apply TAO test.
1422c068736aSJeffrey Hsu 			 * If it succeeds, connection is * half-synchronized.
1423c068736aSJeffrey Hsu 			 * Otherwise, do 3-way handshake:
1424a0292f23SGarrett Wollman 			 *        SYN-SENT -> SYN-RECEIVED
1425a0292f23SGarrett Wollman 			 *        SYN-SENT* -> SYN-RECEIVED*
1426a0292f23SGarrett Wollman 			 * If there was no CC option, clear cached CC value.
1427a0292f23SGarrett Wollman 			 */
14284b8e98d6SQing Li 			tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
14299b8b58e0SJonathan Lemon 			callout_stop(tp->tt_rexmt);
1430df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_SYN_RECEIVED;
1431a0292f23SGarrett Wollman 		}
1432df8bae1dSRodney W. Grimes 
1433de30ea13SRobert Watson 		KASSERT(headlocked, ("tcp_input: trimthenstep6: head not "
1434de30ea13SRobert Watson 		    "locked"));
14357cfc6904SRobert Watson 		INP_LOCK_ASSERT(inp);
14367cfc6904SRobert Watson 
1437df8bae1dSRodney W. Grimes 		/*
1438fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
1439df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
1440df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
1441df8bae1dSRodney W. Grimes 		 */
1442fb59c426SYoshinobu Inoue 		th->th_seq++;
1443fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
1444fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
1445df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
1446fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
1447fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
1448df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpackafterwin++;
1449df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
1450df8bae1dSRodney W. Grimes 		}
1451fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
1452fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
1453a0292f23SGarrett Wollman 		/*
1454a0292f23SGarrett Wollman 		 * Client side of transaction: already sent SYN and data.
1455a0292f23SGarrett Wollman 		 * If the remote host used T/TCP to validate the SYN,
1456a0292f23SGarrett Wollman 		 * our data will be ACK'd; if so, enter normal data segment
1457a0292f23SGarrett Wollman 		 * processing in the middle of step 5, ack processing.
1458a0292f23SGarrett Wollman 		 * Otherwise, goto step 6.
1459a0292f23SGarrett Wollman 		 */
1460fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK)
1461a0292f23SGarrett Wollman 			goto process_ACK;
1462c068736aSJeffrey Hsu 
1463df8bae1dSRodney W. Grimes 		goto step6;
1464c068736aSJeffrey Hsu 
1465a0292f23SGarrett Wollman 	/*
1466a0292f23SGarrett Wollman 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
1467c94c54e4SAndre Oppermann 	 *      do normal processing.
1468a0292f23SGarrett Wollman 	 *
1469c94c54e4SAndre Oppermann 	 * NB: Leftover from RFC1644 T/TCP.  Cases to be reused later.
1470a0292f23SGarrett Wollman 	 */
1471a0292f23SGarrett Wollman 	case TCPS_LAST_ACK:
1472a0292f23SGarrett Wollman 	case TCPS_CLOSING:
1473a0292f23SGarrett Wollman 	case TCPS_TIME_WAIT:
1474340c35deSJonathan Lemon 		KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
1475a0292f23SGarrett Wollman 		break;  /* continue normal processing */
1476df8bae1dSRodney W. Grimes 	}
1477df8bae1dSRodney W. Grimes 
1478df8bae1dSRodney W. Grimes 	/*
1479df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
148080ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
148180ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
148280ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
148380ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
148480ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
148580ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
148680ab7c0eSGarrett Wollman 	 * killing a connection).
148780ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
1488a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
1489df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
1490df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
1491df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
1492df8bae1dSRodney W. Grimes 	 *
149380ab7c0eSGarrett Wollman 	 *
149480ab7c0eSGarrett Wollman 	 * If the RST bit is set, check the sequence number to see
149580ab7c0eSGarrett Wollman 	 * if this is a valid reset segment.
149680ab7c0eSGarrett Wollman 	 * RFC 793 page 37:
149780ab7c0eSGarrett Wollman 	 *   In all states except SYN-SENT, all reset (RST) segments
149880ab7c0eSGarrett Wollman 	 *   are validated by checking their SEQ-fields.  A reset is
149980ab7c0eSGarrett Wollman 	 *   valid if its sequence number is in the window.
150080ab7c0eSGarrett Wollman 	 * Note: this does not take into account delayed ACKs, so
150180ab7c0eSGarrett Wollman 	 *   we should test against last_ack_sent instead of rcv_nxt.
15021a244a61SJonathan Lemon 	 *   The sequence number in the reset segment is normally an
15031a244a61SJonathan Lemon 	 *   echo of our outgoing acknowlegement numbers, but some hosts
15041a244a61SJonathan Lemon 	 *   send a reset with the sequence number at the rightmost edge
15051a244a61SJonathan Lemon 	 *   of our receive window, and we have to handle this case.
150680dd2a81SMike Silbersack 	 * Note 2: Paul Watson's paper "Slipping in the Window" has shown
150780dd2a81SMike Silbersack 	 *   that brute force RST attacks are possible.  To combat this,
150880dd2a81SMike Silbersack 	 *   we use a much stricter check while in the ESTABLISHED state,
150980dd2a81SMike Silbersack 	 *   only accepting RSTs where the sequence number is equal to
151080dd2a81SMike Silbersack 	 *   last_ack_sent.  In all other states (the states in which a
151180dd2a81SMike Silbersack 	 *   RST is more likely), the more permissive check is used.
151280ab7c0eSGarrett Wollman 	 * If we have multiple segments in flight, the intial reset
151380ab7c0eSGarrett Wollman 	 * segment sequence numbers will be to the left of last_ack_sent,
151480ab7c0eSGarrett Wollman 	 * but they will eventually catch up.
151580ab7c0eSGarrett Wollman 	 * In any case, it never made sense to trim reset segments to
151680ab7c0eSGarrett Wollman 	 * fit the receive window since RFC 1122 says:
151780ab7c0eSGarrett Wollman 	 *   4.2.2.12  RST Segment: RFC-793 Section 3.4
151880ab7c0eSGarrett Wollman 	 *
151980ab7c0eSGarrett Wollman 	 *    A TCP SHOULD allow a received RST segment to include data.
152080ab7c0eSGarrett Wollman 	 *
152180ab7c0eSGarrett Wollman 	 *    DISCUSSION
152280ab7c0eSGarrett Wollman 	 *         It has been suggested that a RST segment could contain
152380ab7c0eSGarrett Wollman 	 *         ASCII text that encoded and explained the cause of the
152480ab7c0eSGarrett Wollman 	 *         RST.  No standard has yet been established for such
152580ab7c0eSGarrett Wollman 	 *         data.
152680ab7c0eSGarrett Wollman 	 *
152780ab7c0eSGarrett Wollman 	 * If the reset segment passes the sequence number test examine
152880ab7c0eSGarrett Wollman 	 * the state:
152980ab7c0eSGarrett Wollman 	 *    SYN_RECEIVED STATE:
153080ab7c0eSGarrett Wollman 	 *	If passive open, return to LISTEN state.
153180ab7c0eSGarrett Wollman 	 *	If active open, inform user that connection was refused.
1532745bab7fSDima Dorfman 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
153380ab7c0eSGarrett Wollman 	 *	Inform user that connection was reset, and close tcb.
1534e9bd3a37SJonathan M. Bresler 	 *    CLOSING, LAST_ACK STATES:
153580ab7c0eSGarrett Wollman 	 *	Close the tcb.
1536e9bd3a37SJonathan M. Bresler 	 *    TIME_WAIT STATE:
153780ab7c0eSGarrett Wollman 	 *	Drop the segment - see Stevens, vol. 2, p. 964 and
153880ab7c0eSGarrett Wollman 	 *      RFC 1337.
153980ab7c0eSGarrett Wollman 	 */
1540fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
154195ad8418SQing Li 		if (SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) &&
154295ad8418SQing Li 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
154380ab7c0eSGarrett Wollman 			switch (tp->t_state) {
154480ab7c0eSGarrett Wollman 
154580ab7c0eSGarrett Wollman 			case TCPS_SYN_RECEIVED:
154680ab7c0eSGarrett Wollman 				so->so_error = ECONNREFUSED;
154780ab7c0eSGarrett Wollman 				goto close;
154880ab7c0eSGarrett Wollman 
154980ab7c0eSGarrett Wollman 			case TCPS_ESTABLISHED:
155095ad8418SQing Li 				if (tcp_insecure_rst == 0 &&
155195ad8418SQing Li 				    !(SEQ_GEQ(th->th_seq, tp->rcv_nxt - 1) &&
155295ad8418SQing Li 				    SEQ_LEQ(th->th_seq, tp->rcv_nxt + 1)) &&
155395ad8418SQing Li 				    !(SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) &&
155495ad8418SQing Li 				    SEQ_LEQ(th->th_seq, tp->last_ack_sent + 1))) {
155580dd2a81SMike Silbersack 					tcpstat.tcps_badrst++;
155680dd2a81SMike Silbersack 					goto drop;
155780dd2a81SMike Silbersack 				}
155880ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_1:
155980ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_2:
156080ab7c0eSGarrett Wollman 			case TCPS_CLOSE_WAIT:
156180ab7c0eSGarrett Wollman 				so->so_error = ECONNRESET;
156280ab7c0eSGarrett Wollman 			close:
156380ab7c0eSGarrett Wollman 				tp->t_state = TCPS_CLOSED;
156480ab7c0eSGarrett Wollman 				tcpstat.tcps_drops++;
1565416738a7SRobert Watson 				KASSERT(headlocked, ("tcp_input: "
1566416738a7SRobert Watson 				    "trimthenstep6: tcp_close: head not "
1567416738a7SRobert Watson 				    "locked"));
156880ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
156980ab7c0eSGarrett Wollman 				break;
157080ab7c0eSGarrett Wollman 
157180ab7c0eSGarrett Wollman 			case TCPS_CLOSING:
157280ab7c0eSGarrett Wollman 			case TCPS_LAST_ACK:
1573416738a7SRobert Watson 				KASSERT(headlocked, ("trimthenstep6: "
1574416738a7SRobert Watson 				    "tcp_close.2: head not locked"));
157580ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
157680ab7c0eSGarrett Wollman 				break;
157780ab7c0eSGarrett Wollman 
157880ab7c0eSGarrett Wollman 			case TCPS_TIME_WAIT:
1579340c35deSJonathan Lemon 				KASSERT(tp->t_state != TCPS_TIME_WAIT,
1580340c35deSJonathan Lemon 				    ("timewait"));
158180ab7c0eSGarrett Wollman 				break;
158280ab7c0eSGarrett Wollman 			}
158380ab7c0eSGarrett Wollman 		}
158480ab7c0eSGarrett Wollman 		goto drop;
158580ab7c0eSGarrett Wollman 	}
158680ab7c0eSGarrett Wollman 
158780ab7c0eSGarrett Wollman 	/*
1588df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
1589df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
1590df8bae1dSRodney W. Grimes 	 */
1591be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
159280ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
1593df8bae1dSRodney W. Grimes 
1594df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
15959b8b58e0SJonathan Lemon 		if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) {
1596df8bae1dSRodney W. Grimes 			/*
1597df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
1598df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
1599df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
1600df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
1601df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
1602df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
1603df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
1604df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
1605df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
1606df8bae1dSRodney W. Grimes 			 */
1607df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
1608df8bae1dSRodney W. Grimes 		} else {
1609df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvduppack++;
1610fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvdupbyte += tlen;
1611df8bae1dSRodney W. Grimes 			tcpstat.tcps_pawsdrop++;
161207fd333dSMatthew Dillon 			if (tlen)
1613df8bae1dSRodney W. Grimes 				goto dropafterack;
16141ab4789dSMatthew Dillon 			goto drop;
16151ab4789dSMatthew Dillon 		}
1616df8bae1dSRodney W. Grimes 	}
1617df8bae1dSRodney W. Grimes 
1618a0292f23SGarrett Wollman 	/*
161980ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
162080ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
162180ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
162280ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
162380ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
162480ab7c0eSGarrett Wollman 	 */
1625a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
1626a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
1627a57815efSBosko Milekic 		goto dropwithreset;
1628a57815efSBosko Milekic 	}
162980ab7c0eSGarrett Wollman 
1630fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
1631df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1632fb59c426SYoshinobu Inoue 		if (thflags & TH_SYN) {
1633fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
1634fb59c426SYoshinobu Inoue 			th->th_seq++;
1635fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
1636fb59c426SYoshinobu Inoue 				th->th_urp--;
1637df8bae1dSRodney W. Grimes 			else
1638fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
1639df8bae1dSRodney W. Grimes 			todrop--;
1640df8bae1dSRodney W. Grimes 		}
1641df8bae1dSRodney W. Grimes 		/*
1642dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
1643df8bae1dSRodney W. Grimes 		 */
1644fb59c426SYoshinobu Inoue 		if (todrop > tlen
1645fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
1646dac20301SGarrett Wollman 			/*
1647dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
1648dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
1649dac20301SGarrett Wollman 			 * of sequence; drop it.
1650dac20301SGarrett Wollman 			 */
1651fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
1652dac20301SGarrett Wollman 
1653df8bae1dSRodney W. Grimes 			/*
1654dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
1655dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
1656df8bae1dSRodney W. Grimes 			 */
1657dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
1658fb59c426SYoshinobu Inoue 			todrop = tlen;
1659dac20301SGarrett Wollman 			tcpstat.tcps_rcvduppack++;
1660dac20301SGarrett Wollman 			tcpstat.tcps_rcvdupbyte += todrop;
1661df8bae1dSRodney W. Grimes 		} else {
1662df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartduppack++;
1663df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartdupbyte += todrop;
1664df8bae1dSRodney W. Grimes 		}
1665fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
1666fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
1667fb59c426SYoshinobu Inoue 		tlen -= todrop;
1668fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
1669fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
1670df8bae1dSRodney W. Grimes 		else {
1671fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
1672fb59c426SYoshinobu Inoue 			th->th_urp = 0;
1673df8bae1dSRodney W. Grimes 		}
1674df8bae1dSRodney W. Grimes 	}
1675df8bae1dSRodney W. Grimes 
1676df8bae1dSRodney W. Grimes 	/*
1677df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
1678df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
1679df8bae1dSRodney W. Grimes 	 */
1680df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
1681fb59c426SYoshinobu Inoue 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
1682416738a7SRobert Watson 		KASSERT(headlocked, ("trimthenstep6: tcp_close.3: head not "
1683416738a7SRobert Watson 		    "locked"));
1684df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1685df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvafterclose++;
1686a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
1687df8bae1dSRodney W. Grimes 		goto dropwithreset;
1688df8bae1dSRodney W. Grimes 	}
1689df8bae1dSRodney W. Grimes 
1690df8bae1dSRodney W. Grimes 	/*
1691df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
1692df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
1693df8bae1dSRodney W. Grimes 	 */
1694fb59c426SYoshinobu Inoue 	todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd);
1695df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1696df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvpackafterwin++;
1697fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
1698fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbyteafterwin += tlen;
1699df8bae1dSRodney W. Grimes 			/*
1700df8bae1dSRodney W. Grimes 			 * If a new connection request is received
1701df8bae1dSRodney W. Grimes 			 * while in TIME_WAIT, drop the old connection
1702df8bae1dSRodney W. Grimes 			 * and start over if the sequence numbers
1703df8bae1dSRodney W. Grimes 			 * are above the previous ones.
1704df8bae1dSRodney W. Grimes 			 */
1705340c35deSJonathan Lemon 			KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
1706fb59c426SYoshinobu Inoue 			if (thflags & TH_SYN &&
1707df8bae1dSRodney W. Grimes 			    tp->t_state == TCPS_TIME_WAIT &&
1708fb59c426SYoshinobu Inoue 			    SEQ_GT(th->th_seq, tp->rcv_nxt)) {
1709416738a7SRobert Watson 				KASSERT(headlocked, ("trimthenstep6: "
1710416738a7SRobert Watson 				    "tcp_close.4: head not locked"));
1711df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
1712df8bae1dSRodney W. Grimes 				goto findpcb;
1713df8bae1dSRodney W. Grimes 			}
1714df8bae1dSRodney W. Grimes 			/*
1715df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
1716df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
1717df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
1718df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
1719df8bae1dSRodney W. Grimes 			 * and ack.
1720df8bae1dSRodney W. Grimes 			 */
1721fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
1722df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
1723df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvwinprobe++;
1724df8bae1dSRodney W. Grimes 			} else
1725df8bae1dSRodney W. Grimes 				goto dropafterack;
1726df8bae1dSRodney W. Grimes 		} else
1727df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
1728df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
1729fb59c426SYoshinobu Inoue 		tlen -= todrop;
1730fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
1731df8bae1dSRodney W. Grimes 	}
1732df8bae1dSRodney W. Grimes 
1733df8bae1dSRodney W. Grimes 	/*
1734df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
1735df8bae1dSRodney W. Grimes 	 * record its timestamp.
1736cf09195bSPaul Saab 	 * NOTE:
1737cf09195bSPaul Saab 	 * 1) That the test incorporates suggestions from the latest
1738a0292f23SGarrett Wollman 	 *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
1739cf09195bSPaul Saab 	 * 2) That updating only on newer timestamps interferes with
1740cf09195bSPaul Saab 	 *    our earlier PAWS tests, so this check should be solely
1741cf09195bSPaul Saab 	 *    predicated on the sequence space of this segment.
1742cf09195bSPaul Saab 	 * 3) That we modify the segment boundary check to be
1743cf09195bSPaul Saab 	 *        Last.ACK.Sent <= SEG.SEQ + SEG.Len
1744cf09195bSPaul Saab 	 *    instead of RFC1323's
1745cf09195bSPaul Saab 	 *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
1746cf09195bSPaul Saab 	 *    This modified check allows us to overcome RFC1323's
1747cf09195bSPaul Saab 	 *    limitations as described in Stevens TCP/IP Illustrated
1748cf09195bSPaul Saab 	 *    Vol. 2 p.869. In such cases, we can still calculate the
1749cf09195bSPaul Saab 	 *    RTT correctly when RCV.NXT == Last.ACK.Sent.
1750df8bae1dSRodney W. Grimes 	 */
1751be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
1752cf09195bSPaul Saab 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
1753cf09195bSPaul Saab 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
1754cf09195bSPaul Saab 		((thflags & (TH_SYN|TH_FIN)) != 0))) {
17559b8b58e0SJonathan Lemon 		tp->ts_recent_age = ticks;
1756a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
1757df8bae1dSRodney W. Grimes 	}
1758df8bae1dSRodney W. Grimes 
1759df8bae1dSRodney W. Grimes 	/*
1760df8bae1dSRodney W. Grimes 	 * If a SYN is in the window, then this is an
1761df8bae1dSRodney W. Grimes 	 * error and we send an RST and drop the connection.
1762df8bae1dSRodney W. Grimes 	 */
1763fb59c426SYoshinobu Inoue 	if (thflags & TH_SYN) {
17641e2d989dSRobert Watson 		KASSERT(headlocked, ("tcp_input: tcp_drop: trimthenstep6: "
17651e2d989dSRobert Watson 		    "head not locked"));
1766df8bae1dSRodney W. Grimes 		tp = tcp_drop(tp, ECONNRESET);
1767a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
1768122aad88SAndre Oppermann 		goto drop;
1769df8bae1dSRodney W. Grimes 	}
1770df8bae1dSRodney W. Grimes 
1771a0292f23SGarrett Wollman 	/*
1772a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
1773a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
1774a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
1775a0292f23SGarrett Wollman 	 */
1776fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
1777a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
1778a0292f23SGarrett Wollman 		    (tp->t_flags & TF_NEEDSYN))
1779a0292f23SGarrett Wollman 			goto step6;
17805e1aa279SDavid Malone 		else if (tp->t_flags & TF_ACKNOW)
17815e1aa279SDavid Malone 			goto dropafterack;
1782a0292f23SGarrett Wollman 		else
1783a0292f23SGarrett Wollman 			goto drop;
1784a0292f23SGarrett Wollman 	}
1785df8bae1dSRodney W. Grimes 
1786df8bae1dSRodney W. Grimes 	/*
1787df8bae1dSRodney W. Grimes 	 * Ack processing.
1788df8bae1dSRodney W. Grimes 	 */
1789df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1790df8bae1dSRodney W. Grimes 
1791df8bae1dSRodney W. Grimes 	/*
1792764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
1793764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
1794764d8cefSBill Fenner 	 * The ACK was checked above.
1795df8bae1dSRodney W. Grimes 	 */
1796df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1797a0292f23SGarrett Wollman 
1798df8bae1dSRodney W. Grimes 		tcpstat.tcps_connects++;
1799df8bae1dSRodney W. Grimes 		soisconnected(so);
1800df8bae1dSRodney W. Grimes 		/* Do window scaling? */
1801df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1802df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1803df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
1804464fcfbcSAndre Oppermann 			tp->snd_wnd = tiwin;
1805df8bae1dSRodney W. Grimes 		}
1806a0292f23SGarrett Wollman 		/*
1807a0292f23SGarrett Wollman 		 * Make transitions:
1808a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
1809a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
1810a0292f23SGarrett Wollman 		 */
18119b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
1812a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDFIN) {
1813a0292f23SGarrett Wollman 			tp->t_state = TCPS_FIN_WAIT_1;
1814a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDFIN;
18157ff19458SPaul Traina 		} else {
1816a0292f23SGarrett Wollman 			tp->t_state = TCPS_ESTABLISHED;
18179b8b58e0SJonathan Lemon 			callout_reset(tp->tt_keep, tcp_keepidle,
18189b8b58e0SJonathan Lemon 				      tcp_timer_keep, tp);
18197ff19458SPaul Traina 		}
1820a0292f23SGarrett Wollman 		/*
1821a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
1822a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
1823a0292f23SGarrett Wollman 		 */
1824fb59c426SYoshinobu Inoue 		if (tlen == 0 && (thflags & TH_FIN) == 0)
1825fb59c426SYoshinobu Inoue 			(void) tcp_reass(tp, (struct tcphdr *)0, 0,
1826a0292f23SGarrett Wollman 			    (struct mbuf *)0);
1827fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
182893b0017fSPhilippe Charnier 		/* FALLTHROUGH */
1829df8bae1dSRodney W. Grimes 
1830df8bae1dSRodney W. Grimes 	/*
1831df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1832df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
1833fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
1834fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
1835df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
1836df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
1837df8bae1dSRodney W. Grimes 	 */
1838df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1839df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
1840df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
1841df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1842df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
1843df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
1844df8bae1dSRodney W. Grimes 	case TCPS_TIME_WAIT:
1845340c35deSJonathan Lemon 		KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
18465a53ca16SPaul Saab 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
18475a53ca16SPaul Saab 			tcpstat.tcps_rcvacktoomuch++;
18485a53ca16SPaul Saab 			goto dropafterack;
18495a53ca16SPaul Saab 		}
1850482ac968SPaul Saab 		if (tp->sack_enable &&
1851482ac968SPaul Saab 		    (to.to_nsacks > 0 || !TAILQ_EMPTY(&tp->snd_holes)))
18525a53ca16SPaul Saab 			tcp_sack_doack(tp, &to, th->th_ack);
1853fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
1854fb59c426SYoshinobu Inoue 			if (tlen == 0 && tiwin == tp->snd_wnd) {
1855df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvdupack++;
1856df8bae1dSRodney W. Grimes 				/*
1857df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
1858df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
1859df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
1860df8bae1dSRodney W. Grimes 				 * change), the ack is the biggest we've
1861df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
1862df8bae1dSRodney W. Grimes 				 * threshhold of them, assume a packet
1863df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
1864df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
1865df8bae1dSRodney W. Grimes 				 * window so we send only this one
1866df8bae1dSRodney W. Grimes 				 * packet.
1867df8bae1dSRodney W. Grimes 				 *
1868df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
1869df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
1870df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
1871df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
1872df8bae1dSRodney W. Grimes 				 * the new ssthresh).
1873df8bae1dSRodney W. Grimes 				 *
1874df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
1875df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
1876df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
1877df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
1878df8bae1dSRodney W. Grimes 				 * network.
1879df8bae1dSRodney W. Grimes 				 */
18809b8b58e0SJonathan Lemon 				if (!callout_active(tp->tt_rexmt) ||
1881fb59c426SYoshinobu Inoue 				    th->th_ack != tp->snd_una)
1882df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
1883cb942153SJeffrey Hsu 				else if (++tp->t_dupacks > tcprexmtthresh ||
18846d90faf3SPaul Saab 					 ((tcp_do_newreno || tp->sack_enable) &&
18859d11646dSJeffrey Hsu 					  IN_FASTRECOVERY(tp))) {
188625e6f9edSPaul Saab                                         if (tp->sack_enable && IN_FASTRECOVERY(tp)) {
1887808f11b7SPaul Saab 						int awnd;
188825e6f9edSPaul Saab 
188925e6f9edSPaul Saab 						/*
189025e6f9edSPaul Saab 						 * Compute the amount of data in flight first.
189125e6f9edSPaul Saab 						 * We can inject new data into the pipe iff
189225e6f9edSPaul Saab 						 * we have less than 1/2 the original window's
189325e6f9edSPaul Saab 						 * worth of data in flight.
189425e6f9edSPaul Saab 						 */
1895808f11b7SPaul Saab 						awnd = (tp->snd_nxt - tp->snd_fack) +
18960077b016SPaul Saab 							tp->sackhint.sack_bytes_rexmit;
1897808f11b7SPaul Saab 						if (awnd < tp->snd_ssthresh) {
189825e6f9edSPaul Saab 							tp->snd_cwnd += tp->t_maxseg;
189925e6f9edSPaul Saab 							if (tp->snd_cwnd > tp->snd_ssthresh)
190025e6f9edSPaul Saab 								tp->snd_cwnd = tp->snd_ssthresh;
190125e6f9edSPaul Saab 						}
190225e6f9edSPaul Saab 					} else
190346f58482SJonathan Lemon 						tp->snd_cwnd += tp->t_maxseg;
190446f58482SJonathan Lemon 					(void) tcp_output(tp);
190546f58482SJonathan Lemon 					goto drop;
1906cb942153SJeffrey Hsu 				} else if (tp->t_dupacks == tcprexmtthresh) {
1907cb942153SJeffrey Hsu 					tcp_seq onxt = tp->snd_nxt;
1908cb942153SJeffrey Hsu 					u_int win;
1909a0445c2eSJayanth Vijayaraghavan 
1910a0445c2eSJayanth Vijayaraghavan 					/*
1911a0445c2eSJayanth Vijayaraghavan 					 * If we're doing sack, check to
1912a0445c2eSJayanth Vijayaraghavan 					 * see if we're already in sack
1913a0445c2eSJayanth Vijayaraghavan 					 * recovery. If we're not doing sack,
1914a0445c2eSJayanth Vijayaraghavan 					 * check to see if we're in newreno
1915a0445c2eSJayanth Vijayaraghavan 					 * recovery.
1916a0445c2eSJayanth Vijayaraghavan 					 */
1917a0445c2eSJayanth Vijayaraghavan 					if (tp->sack_enable) {
1918a0445c2eSJayanth Vijayaraghavan 						if (IN_FASTRECOVERY(tp)) {
1919a0445c2eSJayanth Vijayaraghavan 							tp->t_dupacks = 0;
1920a0445c2eSJayanth Vijayaraghavan 							break;
1921a0445c2eSJayanth Vijayaraghavan 						}
1922a0445c2eSJayanth Vijayaraghavan 					} else if (tcp_do_newreno) {
1923a0445c2eSJayanth Vijayaraghavan 						if (SEQ_LEQ(th->th_ack,
19249d11646dSJeffrey Hsu 						    tp->snd_recover)) {
1925cb942153SJeffrey Hsu 							tp->t_dupacks = 0;
1926cb942153SJeffrey Hsu 							break;
192746f58482SJonathan Lemon 						}
1928a0445c2eSJayanth Vijayaraghavan 					}
1929cb942153SJeffrey Hsu 					win = min(tp->snd_wnd, tp->snd_cwnd) /
1930cb942153SJeffrey Hsu 					    2 / tp->t_maxseg;
1931df8bae1dSRodney W. Grimes 					if (win < 2)
1932df8bae1dSRodney W. Grimes 						win = 2;
1933df8bae1dSRodney W. Grimes 					tp->snd_ssthresh = win * tp->t_maxseg;
19349d11646dSJeffrey Hsu 					ENTER_FASTRECOVERY(tp);
193546f58482SJonathan Lemon 					tp->snd_recover = tp->snd_max;
19369b8b58e0SJonathan Lemon 					callout_stop(tp->tt_rexmt);
19379b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
19386d90faf3SPaul Saab 					if (tp->sack_enable) {
19396d90faf3SPaul Saab 						tcpstat.tcps_sack_recovery_episode++;
1940a55db2b6SPaul Saab 						tp->sack_newdata = tp->snd_nxt;
19418db456bfSPaul Saab 						tp->snd_cwnd = tp->t_maxseg;
19426d90faf3SPaul Saab 						(void) tcp_output(tp);
19436d90faf3SPaul Saab 						goto drop;
19446d90faf3SPaul Saab 					}
1945fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
1946df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->t_maxseg;
1947df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
194848d2549cSJeffrey Hsu 					KASSERT(tp->snd_limited <= 2,
194948d2549cSJeffrey Hsu 					    ("tp->snd_limited too big"));
1950df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
195148d2549cSJeffrey Hsu 					     tp->t_maxseg *
195248d2549cSJeffrey Hsu 					     (tp->t_dupacks - tp->snd_limited);
1953df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
1954df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
1955df8bae1dSRodney W. Grimes 					goto drop;
1956582a954bSJeffrey Hsu 				} else if (tcp_do_rfc3042) {
1957582a954bSJeffrey Hsu 					u_long oldcwnd = tp->snd_cwnd;
195848d2549cSJeffrey Hsu 					tcp_seq oldsndmax = tp->snd_max;
195948d2549cSJeffrey Hsu 					u_int sent;
196089c02376SJeffrey Hsu 
1961582a954bSJeffrey Hsu 					KASSERT(tp->t_dupacks == 1 ||
1962582a954bSJeffrey Hsu 					    tp->t_dupacks == 2,
1963582a954bSJeffrey Hsu 					    ("dupacks not 1 or 2"));
196461a36e3dSJeffrey Hsu 					if (tp->t_dupacks == 1)
196548d2549cSJeffrey Hsu 						tp->snd_limited = 0;
196661a36e3dSJeffrey Hsu 					tp->snd_cwnd =
196761a36e3dSJeffrey Hsu 					    (tp->snd_nxt - tp->snd_una) +
196861a36e3dSJeffrey Hsu 					    (tp->t_dupacks - tp->snd_limited) *
196961a36e3dSJeffrey Hsu 					    tp->t_maxseg;
1970582a954bSJeffrey Hsu 					(void) tcp_output(tp);
197148d2549cSJeffrey Hsu 					sent = tp->snd_max - oldsndmax;
197248d2549cSJeffrey Hsu 					if (sent > tp->t_maxseg) {
197389c02376SJeffrey Hsu 						KASSERT((tp->t_dupacks == 2 &&
197489c02376SJeffrey Hsu 						    tp->snd_limited == 0) ||
197589c02376SJeffrey Hsu 						   (sent == tp->t_maxseg + 1 &&
197689c02376SJeffrey Hsu 						    tp->t_flags & TF_SENTFIN),
197748d2549cSJeffrey Hsu 						    ("sent too much"));
197848d2549cSJeffrey Hsu 						tp->snd_limited = 2;
197948d2549cSJeffrey Hsu 					} else if (sent > 0)
198048d2549cSJeffrey Hsu 						++tp->snd_limited;
1981582a954bSJeffrey Hsu 					tp->snd_cwnd = oldcwnd;
1982582a954bSJeffrey Hsu 					goto drop;
1983df8bae1dSRodney W. Grimes 				}
1984df8bae1dSRodney W. Grimes 			} else
1985df8bae1dSRodney W. Grimes 				tp->t_dupacks = 0;
1986df8bae1dSRodney W. Grimes 			break;
1987df8bae1dSRodney W. Grimes 		}
1988cb942153SJeffrey Hsu 
1989cb942153SJeffrey Hsu 		KASSERT(SEQ_GT(th->th_ack, tp->snd_una), ("th_ack <= snd_una"));
1990cb942153SJeffrey Hsu 
1991df8bae1dSRodney W. Grimes 		/*
1992df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
1993df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
1994df8bae1dSRodney W. Grimes 		 */
19956d90faf3SPaul Saab 		if (tcp_do_newreno || tp->sack_enable) {
19969d11646dSJeffrey Hsu 			if (IN_FASTRECOVERY(tp)) {
1997cb942153SJeffrey Hsu 				if (SEQ_LT(th->th_ack, tp->snd_recover)) {
19986d90faf3SPaul Saab 					if (tp->sack_enable)
19996d90faf3SPaul Saab 						tcp_sack_partialack(tp, th);
20006d90faf3SPaul Saab 					else
2001c068736aSJeffrey Hsu 						tcp_newreno_partial_ack(tp, th);
2002c068736aSJeffrey Hsu 				} else {
2003c068736aSJeffrey Hsu 					/*
20046d90faf3SPaul Saab 					 * Out of fast recovery.
2005c068736aSJeffrey Hsu 					 * Window inflation should have left us
2006c068736aSJeffrey Hsu 					 * with approximately snd_ssthresh
2007c068736aSJeffrey Hsu 					 * outstanding data.
2008c068736aSJeffrey Hsu 					 * But in case we would be inclined to
2009c068736aSJeffrey Hsu 					 * send a burst, better to do it via
2010c068736aSJeffrey Hsu 					 * the slow start mechanism.
2011c068736aSJeffrey Hsu 					 */
2012c068736aSJeffrey Hsu 					if (SEQ_GT(th->th_ack +
2013c068736aSJeffrey Hsu 							tp->snd_ssthresh,
2014c068736aSJeffrey Hsu 						   tp->snd_max))
2015c068736aSJeffrey Hsu 						tp->snd_cwnd = tp->snd_max -
2016c068736aSJeffrey Hsu 								th->th_ack +
2017c068736aSJeffrey Hsu 								tp->t_maxseg;
2018c068736aSJeffrey Hsu 					else
2019c068736aSJeffrey Hsu 						tp->snd_cwnd = tp->snd_ssthresh;
2020c068736aSJeffrey Hsu 				}
2021c068736aSJeffrey Hsu 			}
2022c068736aSJeffrey Hsu 		} else {
2023233e8c18SGarrett Wollman 			if (tp->t_dupacks >= tcprexmtthresh &&
2024df8bae1dSRodney W. Grimes 			    tp->snd_cwnd > tp->snd_ssthresh)
2025df8bae1dSRodney W. Grimes 				tp->snd_cwnd = tp->snd_ssthresh;
202646f58482SJonathan Lemon 		}
2027cb942153SJeffrey Hsu 		tp->t_dupacks = 0;
2028a0292f23SGarrett Wollman 		/*
2029a0292f23SGarrett Wollman 		 * If we reach this point, ACK is not a duplicate,
2030a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
2031a0292f23SGarrett Wollman 		 */
2032a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
2033a0292f23SGarrett Wollman 			/*
2034a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
2035a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
203607e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
203707e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
203807e43e10SAndras Olah 			 * we can do window scaling.
2039a0292f23SGarrett Wollman 			 */
2040a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
2041a0292f23SGarrett Wollman 			tp->snd_una++;
204207e43e10SAndras Olah 			/* Do window scaling? */
204307e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
204407e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
204507e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2046464fcfbcSAndre Oppermann 				/* Send window already scaled. */
204707e43e10SAndras Olah 			}
2048a0292f23SGarrett Wollman 		}
2049a0292f23SGarrett Wollman 
2050a0292f23SGarrett Wollman process_ACK:
2051de30ea13SRobert Watson 		KASSERT(headlocked, ("tcp_input: process_ACK: head not "
2052de30ea13SRobert Watson 		    "locked"));
20537cfc6904SRobert Watson 		INP_LOCK_ASSERT(inp);
20547cfc6904SRobert Watson 
2055fb59c426SYoshinobu Inoue 		acked = th->th_ack - tp->snd_una;
2056df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackpack++;
2057df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackbyte += acked;
2058df8bae1dSRodney W. Grimes 
2059df8bae1dSRodney W. Grimes 		/*
20609b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
20619b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
20629b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
20639b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
20649b8b58e0SJonathan Lemon 		 * we left off.
20659b8b58e0SJonathan Lemon 		 */
20669b8b58e0SJonathan Lemon 		if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) {
2067d65bf08aSMatthew Dillon 			++tcpstat.tcps_sndrexmitbad;
20689b8b58e0SJonathan Lemon 			tp->snd_cwnd = tp->snd_cwnd_prev;
20699b8b58e0SJonathan Lemon 			tp->snd_ssthresh = tp->snd_ssthresh_prev;
20709d11646dSJeffrey Hsu 			tp->snd_recover = tp->snd_recover_prev;
20719d11646dSJeffrey Hsu 			if (tp->t_flags & TF_WASFRECOVERY)
20729d11646dSJeffrey Hsu 				ENTER_FASTRECOVERY(tp);
20739b8b58e0SJonathan Lemon 			tp->snd_nxt = tp->snd_max;
20749b8b58e0SJonathan Lemon 			tp->t_badrxtwin = 0;	/* XXX probably not required */
20759b8b58e0SJonathan Lemon 		}
20769b8b58e0SJonathan Lemon 
20779b8b58e0SJonathan Lemon 		/*
2078df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
2079df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
2080df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
2081df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
2082df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
2083df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
2084df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
2085fa55172bSMatthew Dillon 		 *
2086fa55172bSMatthew Dillon 		 * Some boxes send broken timestamp replies
2087fa55172bSMatthew Dillon 		 * during the SYN+ACK phase, ignore
2088fa55172bSMatthew Dillon 		 * timestamps of 0 or we could calculate a
2089fa55172bSMatthew Dillon 		 * huge RTT and blow up the retransmit timer.
2090df8bae1dSRodney W. Grimes 		 */
2091fa55172bSMatthew Dillon 		if ((to.to_flags & TOF_TS) != 0 &&
2092fa55172bSMatthew Dillon 		    to.to_tsecr) {
2093eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - to.to_tsecr)
2094eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - to.to_tsecr;
20959b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - to.to_tsecr + 1);
2096fa55172bSMatthew Dillon 		} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2097eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
2098eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - tp->t_rtttime;
20999b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2100fa55172bSMatthew Dillon 		}
21011fcc99b5SMatthew Dillon 		tcp_xmit_bandwidth_limit(tp, th->th_ack);
2102df8bae1dSRodney W. Grimes 
2103df8bae1dSRodney W. Grimes 		/*
2104df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
2105df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
2106df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
2107df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
2108df8bae1dSRodney W. Grimes 		 */
2109fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
21109b8b58e0SJonathan Lemon 			callout_stop(tp->tt_rexmt);
2111df8bae1dSRodney W. Grimes 			needoutput = 1;
21129b8b58e0SJonathan Lemon 		} else if (!callout_active(tp->tt_persist))
21139b8b58e0SJonathan Lemon 			callout_reset(tp->tt_rexmt, tp->t_rxtcur,
21149b8b58e0SJonathan Lemon 				      tcp_timer_rexmt, tp);
2115a0292f23SGarrett Wollman 
2116a0292f23SGarrett Wollman 		/*
2117a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
2118a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
2119a0292f23SGarrett Wollman 		 */
2120a0292f23SGarrett Wollman 		if (acked == 0)
2121a0292f23SGarrett Wollman 			goto step6;
2122a0292f23SGarrett Wollman 
2123df8bae1dSRodney W. Grimes 		/*
2124df8bae1dSRodney W. Grimes 		 * When new data is acked, open the congestion window.
2125df8bae1dSRodney W. Grimes 		 * If the window gives us less than ssthresh packets
2126df8bae1dSRodney W. Grimes 		 * in flight, open exponentially (maxseg per packet).
2127df8bae1dSRodney W. Grimes 		 * Otherwise open linearly: maxseg per window
212810be5648SGarrett Wollman 		 * (maxseg^2 / cwnd per packet).
2129df8bae1dSRodney W. Grimes 		 */
21306d90faf3SPaul Saab 		if ((!tcp_do_newreno && !tp->sack_enable) ||
21316d90faf3SPaul Saab 		    !IN_FASTRECOVERY(tp)) {
2132ad3f9ab3SAndre Oppermann 			u_int cw = tp->snd_cwnd;
2133ad3f9ab3SAndre Oppermann 			u_int incr = tp->t_maxseg;
2134df8bae1dSRodney W. Grimes 			if (cw > tp->snd_ssthresh)
213510be5648SGarrett Wollman 				incr = incr * incr / cw;
2136df8bae1dSRodney W. Grimes 			tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale);
2137df8bae1dSRodney W. Grimes 		}
21385905999bSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
2139df8bae1dSRodney W. Grimes 		if (acked > so->so_snd.sb_cc) {
2140df8bae1dSRodney W. Grimes 			tp->snd_wnd -= so->so_snd.sb_cc;
21415905999bSRobert Watson 			sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc);
2142df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
2143df8bae1dSRodney W. Grimes 		} else {
21445905999bSRobert Watson 			sbdrop_locked(&so->so_snd, acked);
2145df8bae1dSRodney W. Grimes 			tp->snd_wnd -= acked;
2146df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
2147df8bae1dSRodney W. Grimes 		}
21481e4d7da7SRobert Watson 		sowwakeup_locked(so);
2149cb942153SJeffrey Hsu 		/* detect una wraparound */
21506d90faf3SPaul Saab 		if ((tcp_do_newreno || tp->sack_enable) &&
21516d90faf3SPaul Saab 		    !IN_FASTRECOVERY(tp) &&
21529d11646dSJeffrey Hsu 		    SEQ_GT(tp->snd_una, tp->snd_recover) &&
21539d11646dSJeffrey Hsu 		    SEQ_LEQ(th->th_ack, tp->snd_recover))
21549d11646dSJeffrey Hsu 			tp->snd_recover = th->th_ack - 1;
21556d90faf3SPaul Saab 		if ((tcp_do_newreno || tp->sack_enable) &&
21566d90faf3SPaul Saab 		    IN_FASTRECOVERY(tp) &&
21579d11646dSJeffrey Hsu 		    SEQ_GEQ(th->th_ack, tp->snd_recover))
21589d11646dSJeffrey Hsu 			EXIT_FASTRECOVERY(tp);
2159fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
21606d90faf3SPaul Saab 		if (tp->sack_enable) {
21616d90faf3SPaul Saab 			if (SEQ_GT(tp->snd_una, tp->snd_recover))
21626d90faf3SPaul Saab 				tp->snd_recover = tp->snd_una;
21636d90faf3SPaul Saab 		}
2164df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2165df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
2166df8bae1dSRodney W. Grimes 
2167df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2168df8bae1dSRodney W. Grimes 
2169df8bae1dSRodney W. Grimes 		/*
2170df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
2171df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
2172df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
2173df8bae1dSRodney W. Grimes 		 */
2174df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2175df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2176df8bae1dSRodney W. Grimes 				/*
2177df8bae1dSRodney W. Grimes 				 * If we can't receive any more
2178df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
2179df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
2180df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
2181df8bae1dSRodney W. Grimes 				 * we'll hang forever.
2182df8bae1dSRodney W. Grimes 				 */
2183340c35deSJonathan Lemon 		/* XXXjl
2184340c35deSJonathan Lemon 		 * we should release the tp also, and use a
2185340c35deSJonathan Lemon 		 * compressed state.
2186340c35deSJonathan Lemon 		 */
2187c0b99ffaSRobert Watson 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
21887c72af87SMohan Srinivasan 					int timeout;
21897c72af87SMohan Srinivasan 
219003e49181SSeigo Tanimura 					soisdisconnected(so);
21917c72af87SMohan Srinivasan 					timeout = (tcp_fast_finwait2_recycle) ?
21927c72af87SMohan Srinivasan 						tcp_finwait2_timeout : tcp_maxidle;
21937c72af87SMohan Srinivasan 					callout_reset(tp->tt_2msl, timeout,
21949b8b58e0SJonathan Lemon 						      tcp_timer_2msl, tp);
21954cc20ab1SSeigo Tanimura 				}
2196df8bae1dSRodney W. Grimes 				tp->t_state = TCPS_FIN_WAIT_2;
2197df8bae1dSRodney W. Grimes 			}
2198df8bae1dSRodney W. Grimes 			break;
2199df8bae1dSRodney W. Grimes 
2200df8bae1dSRodney W. Grimes 		/*
2201df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
2202df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
2203df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
2204df8bae1dSRodney W. Grimes 		 * the segment.
2205df8bae1dSRodney W. Grimes 		 */
2206df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
2207df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2208de30ea13SRobert Watson 				KASSERT(headlocked, ("tcp_input: process_ACK: "
2209de30ea13SRobert Watson 				    "head not locked"));
221011a20fb8SJeffrey Hsu 				tcp_twstart(tp);
2211340c35deSJonathan Lemon 				INP_INFO_WUNLOCK(&tcbinfo);
2212340c35deSJonathan Lemon 				m_freem(m);
2213340c35deSJonathan Lemon 				return;
2214df8bae1dSRodney W. Grimes 			}
2215df8bae1dSRodney W. Grimes 			break;
2216df8bae1dSRodney W. Grimes 
2217df8bae1dSRodney W. Grimes 		/*
2218df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
2219df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
2220df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
2221df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
2222df8bae1dSRodney W. Grimes 		 */
2223df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
2224df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2225416738a7SRobert Watson 				KASSERT(headlocked, ("tcp_input: process_ACK:"
2226416738a7SRobert Watson 				    " tcp_close: head not locked"));
2227df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
2228df8bae1dSRodney W. Grimes 				goto drop;
2229df8bae1dSRodney W. Grimes 			}
2230df8bae1dSRodney W. Grimes 			break;
2231df8bae1dSRodney W. Grimes 
2232df8bae1dSRodney W. Grimes 		/*
2233df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state the only thing that should arrive
2234df8bae1dSRodney W. Grimes 		 * is a retransmission of the remote FIN.  Acknowledge
2235df8bae1dSRodney W. Grimes 		 * it and restart the finack timer.
2236df8bae1dSRodney W. Grimes 		 */
2237df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
2238340c35deSJonathan Lemon 			KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
22399b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, 2 * tcp_msl,
22409b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
2241df8bae1dSRodney W. Grimes 			goto dropafterack;
2242df8bae1dSRodney W. Grimes 		}
2243df8bae1dSRodney W. Grimes 	}
2244df8bae1dSRodney W. Grimes 
2245df8bae1dSRodney W. Grimes step6:
2246de30ea13SRobert Watson 	KASSERT(headlocked, ("tcp_input: step6: head not locked"));
22477cfc6904SRobert Watson 	INP_LOCK_ASSERT(inp);
22487cfc6904SRobert Watson 
2249df8bae1dSRodney W. Grimes 	/*
2250df8bae1dSRodney W. Grimes 	 * Update window information.
2251df8bae1dSRodney W. Grimes 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2252df8bae1dSRodney W. Grimes 	 */
2253fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) &&
2254fb59c426SYoshinobu Inoue 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
2255fb59c426SYoshinobu Inoue 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
2256fb59c426SYoshinobu Inoue 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
2257df8bae1dSRodney W. Grimes 		/* keep track of pure window updates */
2258fb59c426SYoshinobu Inoue 		if (tlen == 0 &&
2259fb59c426SYoshinobu Inoue 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
2260df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvwinupd++;
2261df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
2262fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq;
2263fb59c426SYoshinobu Inoue 		tp->snd_wl2 = th->th_ack;
2264df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
2265df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
2266df8bae1dSRodney W. Grimes 		needoutput = 1;
2267df8bae1dSRodney W. Grimes 	}
2268df8bae1dSRodney W. Grimes 
2269df8bae1dSRodney W. Grimes 	/*
2270df8bae1dSRodney W. Grimes 	 * Process segments with URG.
2271df8bae1dSRodney W. Grimes 	 */
2272fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
2273df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2274df8bae1dSRodney W. Grimes 		/*
2275df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
2276df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
2277df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
2278df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
2279df8bae1dSRodney W. Grimes 		 */
228018ad5842SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
2281fb59c426SYoshinobu Inoue 		if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
2282fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
2283fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
228418ad5842SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);	/* XXX */
2285df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
2286df8bae1dSRodney W. Grimes 		}
2287df8bae1dSRodney W. Grimes 		/*
2288df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
2289df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
2290df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2291df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
2292df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
2293df8bae1dSRodney W. Grimes 		 *
2294df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
2295df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
2296df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
2297df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
2298df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
2299df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
2300df8bae1dSRodney W. Grimes 		 */
2301fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2302fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
2303df8bae1dSRodney W. Grimes 			so->so_oobmark = so->so_rcv.sb_cc +
2304df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
2305927c5ceaSRobert Watson 			if (so->so_oobmark == 0)
2306c0b99ffaSRobert Watson 				so->so_rcv.sb_state |= SBS_RCVATMARK;
2307df8bae1dSRodney W. Grimes 			sohasoutofband(so);
2308df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2309df8bae1dSRodney W. Grimes 		}
231018ad5842SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
2311df8bae1dSRodney W. Grimes 		/*
2312df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
2313df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
2314df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
2315df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
2316df8bae1dSRodney W. Grimes 		 */
231730613f56SJeffrey Hsu 		if (th->th_urp <= (u_long)tlen &&
231830613f56SJeffrey Hsu 		    !(so->so_options & SO_OOBINLINE)) {
231930613f56SJeffrey Hsu 			/* hdr drop is delayed */
232030613f56SJeffrey Hsu 			tcp_pulloutofband(so, th, m, drop_hdrlen);
232130613f56SJeffrey Hsu 		}
2322c068736aSJeffrey Hsu 	} else {
2323df8bae1dSRodney W. Grimes 		/*
2324df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
2325df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
2326df8bae1dSRodney W. Grimes 		 * with the receive window.
2327df8bae1dSRodney W. Grimes 		 */
2328df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2329df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
2330c068736aSJeffrey Hsu 	}
2331df8bae1dSRodney W. Grimes dodata:							/* XXX */
2332de30ea13SRobert Watson 	KASSERT(headlocked, ("tcp_input: dodata: head not locked"));
23337cfc6904SRobert Watson 	INP_LOCK_ASSERT(inp);
23347cfc6904SRobert Watson 
2335df8bae1dSRodney W. Grimes 	/*
2336df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
2337df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
2338df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
2339df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
2340df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
2341df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
2342df8bae1dSRodney W. Grimes 	 */
2343fb59c426SYoshinobu Inoue 	if ((tlen || (thflags & TH_FIN)) &&
2344df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
234569e03620SPaul Saab 		tcp_seq save_start = th->th_seq;
234669e03620SPaul Saab 		tcp_seq save_end = th->th_seq + tlen;
2347fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
2348e4b64281SJesper Skriver 		/*
2349c068736aSJeffrey Hsu 		 * Insert segment which includes th into TCP reassembly queue
2350c068736aSJeffrey Hsu 		 * with control block tp.  Set thflags to whether reassembly now
2351c068736aSJeffrey Hsu 		 * includes a segment with FIN.  This handles the common case
2352c068736aSJeffrey Hsu 		 * inline (segment is the next to be received on an established
2353c068736aSJeffrey Hsu 		 * connection, and the queue is empty), avoiding linkage into
2354c068736aSJeffrey Hsu 		 * and removal from the queue and repetition of various
2355c068736aSJeffrey Hsu 		 * conversions.
2356c068736aSJeffrey Hsu 		 * Set DELACK for segments received in order, but ack
2357c068736aSJeffrey Hsu 		 * immediately when segments are out of order (so
2358c068736aSJeffrey Hsu 		 * fast retransmit can work).
2359e4b64281SJesper Skriver 		 */
2360e4b64281SJesper Skriver 		if (th->th_seq == tp->rcv_nxt &&
2361e4b64281SJesper Skriver 		    LIST_EMPTY(&tp->t_segq) &&
2362e4b64281SJesper Skriver 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
2363e4b64281SJesper Skriver 			if (DELAY_ACK(tp))
23643bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
2365e4b64281SJesper Skriver 			else
2366e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
2367e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
2368e4b64281SJesper Skriver 			thflags = th->th_flags & TH_FIN;
2369e4b64281SJesper Skriver 			tcpstat.tcps_rcvpack++;
2370e4b64281SJesper Skriver 			tcpstat.tcps_rcvbyte += tlen;
2371e4b64281SJesper Skriver 			ND6_HINT(tp);
23721e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
2373c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
2374c1c36a2cSMike Silbersack 				m_freem(m);
2375c1c36a2cSMike Silbersack 			else
23761e4d7da7SRobert Watson 				sbappendstream_locked(&so->so_rcv, m);
23771e4d7da7SRobert Watson 			sorwakeup_locked(so);
2378e4b64281SJesper Skriver 		} else {
2379e4b64281SJesper Skriver 			thflags = tcp_reass(tp, th, &tlen, m);
2380e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
2381e4b64281SJesper Skriver 		}
2382e346eeffSPaul Saab 		if (tlen > 0 && tp->sack_enable)
238369e03620SPaul Saab 			tcp_update_sack_list(tp, save_start, save_end);
2384df8bae1dSRodney W. Grimes 		/*
2385df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
2386df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
2387df8bae1dSRodney W. Grimes 		 * buffer size.
2388df8bae1dSRodney W. Grimes 		 */
2389df8bae1dSRodney W. Grimes 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2390df8bae1dSRodney W. Grimes 	} else {
2391df8bae1dSRodney W. Grimes 		m_freem(m);
2392fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
2393df8bae1dSRodney W. Grimes 	}
2394df8bae1dSRodney W. Grimes 
2395df8bae1dSRodney W. Grimes 	/*
2396df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
2397df8bae1dSRodney W. Grimes 	 * that the connection is closing.
2398df8bae1dSRodney W. Grimes 	 */
2399fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
2400df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2401df8bae1dSRodney W. Grimes 			socantrcvmore(so);
2402a0292f23SGarrett Wollman 			/*
2403a0292f23SGarrett Wollman 			 * If connection is half-synchronized
2404d3eede9dSAndras Olah 			 * (ie NEEDSYN flag on) then delay ACK,
2405a0292f23SGarrett Wollman 			 * so it may be piggybacked when SYN is sent.
2406a0292f23SGarrett Wollman 			 * Otherwise, since we received a FIN then no
2407a0292f23SGarrett Wollman 			 * more input can be expected, send ACK now.
2408a0292f23SGarrett Wollman 			 */
24093bfd6421SJonathan Lemon 			if (tp->t_flags & TF_NEEDSYN)
24103bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
2411a0292f23SGarrett Wollman 			else
2412df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
2413df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
2414df8bae1dSRodney W. Grimes 		}
2415df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2416df8bae1dSRodney W. Grimes 
2417df8bae1dSRodney W. Grimes 		/*
2418df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
2419df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
2420df8bae1dSRodney W. Grimes 		 */
2421df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
24229b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
24239b8b58e0SJonathan Lemon 			/*FALLTHROUGH*/
2424df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
2425df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSE_WAIT;
2426df8bae1dSRodney W. Grimes 			break;
2427df8bae1dSRodney W. Grimes 
2428df8bae1dSRodney W. Grimes 		/*
2429df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2430df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
2431df8bae1dSRodney W. Grimes 		 */
2432df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2433df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSING;
2434df8bae1dSRodney W. Grimes 			break;
2435df8bae1dSRodney W. Grimes 
2436df8bae1dSRodney W. Grimes 		/*
2437df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2438df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
2439df8bae1dSRodney W. Grimes 		 * standard timers.
2440df8bae1dSRodney W. Grimes 		 */
2441df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
2442de30ea13SRobert Watson 			KASSERT(headlocked == 1, ("tcp_input: dodata: "
2443de30ea13SRobert Watson 			    "TCP_FIN_WAIT_2: head not locked"));
2444340c35deSJonathan Lemon 			tcp_twstart(tp);
244511a20fb8SJeffrey Hsu 			INP_INFO_WUNLOCK(&tcbinfo);
2446340c35deSJonathan Lemon 			return;
2447df8bae1dSRodney W. Grimes 
2448df8bae1dSRodney W. Grimes 		/*
2449df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
2450df8bae1dSRodney W. Grimes 		 */
2451df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
2452340c35deSJonathan Lemon 			KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
24539b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, 2 * tcp_msl,
24549b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
2455df8bae1dSRodney W. Grimes 			break;
2456df8bae1dSRodney W. Grimes 		}
2457df8bae1dSRodney W. Grimes 	}
2458e0bef1cbSRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
2459e0bef1cbSRobert Watson 	headlocked = 0;
2460610ee2f9SDavid Greenman #ifdef TCPDEBUG
24614cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
2462fb59c426SYoshinobu Inoue 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
2463fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2464610ee2f9SDavid Greenman #endif
2465df8bae1dSRodney W. Grimes 
2466df8bae1dSRodney W. Grimes 	/*
2467df8bae1dSRodney W. Grimes 	 * Return any desired output.
2468df8bae1dSRodney W. Grimes 	 */
2469df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
2470df8bae1dSRodney W. Grimes 		(void) tcp_output(tp);
24717792ea27SJeffrey Hsu 
2472a14c749fSJonathan Lemon check_delack:
2473e0bef1cbSRobert Watson 	KASSERT(headlocked == 0, ("tcp_input: check_delack: head locked"));
24747cfc6904SRobert Watson 	INP_LOCK_ASSERT(inp);
2475a14c749fSJonathan Lemon 	if (tp->t_flags & TF_DELACK) {
24763bfd6421SJonathan Lemon 		tp->t_flags &= ~TF_DELACK;
24773bfd6421SJonathan Lemon 		callout_reset(tp->tt_delack, tcp_delacktime,
24783bfd6421SJonathan Lemon 		    tcp_timer_delack, tp);
24793bfd6421SJonathan Lemon 	}
2480f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
2481df8bae1dSRodney W. Grimes 	return;
2482df8bae1dSRodney W. Grimes 
2483df8bae1dSRodney W. Grimes dropafterack:
2484de30ea13SRobert Watson 	KASSERT(headlocked, ("tcp_input: dropafterack: head not locked"));
2485df8bae1dSRodney W. Grimes 	/*
2486df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
2487df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
248880ab7c0eSGarrett Wollman 	 *
248980ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
249080ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
249180ab7c0eSGarrett Wollman 	 * RST have been dropped.
249280ab7c0eSGarrett Wollman 	 *
249380ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
249480ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
249580ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
249680ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
249780ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
249880ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
2499df8bae1dSRodney W. Grimes 	 */
2500fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
2501fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
2502a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
2503a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2504a57815efSBosko Milekic 		goto dropwithreset;
2505a57815efSBosko Milekic 	}
2506a0292f23SGarrett Wollman #ifdef TCPDEBUG
25074cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
2508fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2509fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2510a0292f23SGarrett Wollman #endif
251185e8b243SJeffrey Hsu 	KASSERT(headlocked, ("headlocked should be 1"));
251242cf3289SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
2513df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
2514df8bae1dSRodney W. Grimes 	(void) tcp_output(tp);
2515f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
2516d6915262SRobert Watson 	m_freem(m);
2517df8bae1dSRodney W. Grimes 	return;
2518df8bae1dSRodney W. Grimes 
2519df8bae1dSRodney W. Grimes dropwithreset:
2520de30ea13SRobert Watson 	KASSERT(headlocked, ("tcp_input: dropwithreset: head not locked"));
2521df8bae1dSRodney W. Grimes 	/*
2522df8bae1dSRodney W. Grimes 	 * Generate a RST, dropping incoming segment.
2523df8bae1dSRodney W. Grimes 	 * Make ACK acceptable to originator of segment.
2524df8bae1dSRodney W. Grimes 	 * Don't bother to respond if destination was broadcast/multicast.
2525df8bae1dSRodney W. Grimes 	 */
2526fb59c426SYoshinobu Inoue 	if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
2527df8bae1dSRodney W. Grimes 		goto drop;
2528fb59c426SYoshinobu Inoue 	if (isipv6) {
2529173c0f9fSWarner Losh 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
2530173c0f9fSWarner Losh 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
2531fb59c426SYoshinobu Inoue 			goto drop;
2532c068736aSJeffrey Hsu 	} else {
2533173c0f9fSWarner Losh 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
2534173c0f9fSWarner Losh 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
25352ca2159fSCrist J. Clark 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
25362ca2159fSCrist J. Clark 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
2537fb59c426SYoshinobu Inoue 			goto drop;
2538c068736aSJeffrey Hsu 	}
2539fb59c426SYoshinobu Inoue 	/* IPv6 anycast check is done at tcp6_input() */
2540a57815efSBosko Milekic 
2541a57815efSBosko Milekic 	/*
2542c59319bfSDag-Erling Smørgrav 	 * Perform bandwidth limiting.
2543a57815efSBosko Milekic 	 */
2544a57815efSBosko Milekic 	if (badport_bandlim(rstreason) < 0)
2545a57815efSBosko Milekic 		goto drop;
2546a57815efSBosko Milekic 
2547a0292f23SGarrett Wollman #ifdef TCPDEBUG
25484cc20ab1SSeigo Tanimura 	if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2549fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2550fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2551a0292f23SGarrett Wollman #endif
25526fd22cafSJeffrey Hsu 
2553fb59c426SYoshinobu Inoue 	if (thflags & TH_ACK)
2554fb59c426SYoshinobu Inoue 		/* mtod() below is safe as long as hdr dropping is delayed */
2555fb59c426SYoshinobu Inoue 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack,
2556fb59c426SYoshinobu Inoue 			    TH_RST);
2557df8bae1dSRodney W. Grimes 	else {
2558fb59c426SYoshinobu Inoue 		if (thflags & TH_SYN)
2559fb59c426SYoshinobu Inoue 			tlen++;
2560fb59c426SYoshinobu Inoue 		/* mtod() below is safe as long as hdr dropping is delayed */
2561fb59c426SYoshinobu Inoue 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
2562fb59c426SYoshinobu Inoue 			    (tcp_seq)0, TH_RST|TH_ACK);
2563df8bae1dSRodney W. Grimes 	}
2564c29afad6SSam Leffler 
25651c53f806SRobert Watson 	if (tp != NULL)
2566c29afad6SSam Leffler 		INP_UNLOCK(inp);
2567f76fcf6dSJeffrey Hsu 	if (headlocked)
2568f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&tcbinfo);
2569df8bae1dSRodney W. Grimes 	return;
2570df8bae1dSRodney W. Grimes 
2571df8bae1dSRodney W. Grimes drop:
2572df8bae1dSRodney W. Grimes 	/*
2573df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
2574df8bae1dSRodney W. Grimes 	 */
2575610ee2f9SDavid Greenman #ifdef TCPDEBUG
25761c53f806SRobert Watson 	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2577fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2578fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2579a0292f23SGarrett Wollman #endif
25801c53f806SRobert Watson 	if (tp != NULL)
2581f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
2582f76fcf6dSJeffrey Hsu 	if (headlocked)
2583f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&tcbinfo);
2584d6915262SRobert Watson 	m_freem(m);
2585df8bae1dSRodney W. Grimes 	return;
2586df8bae1dSRodney W. Grimes }
2587df8bae1dSRodney W. Grimes 
2588be2ac88cSJonathan Lemon /*
2589be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
2590be2ac88cSJonathan Lemon  */
25910312fbe9SPoul-Henning Kamp static void
2592ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags)
2593df8bae1dSRodney W. Grimes {
2594df8bae1dSRodney W. Grimes 	int opt, optlen;
2595df8bae1dSRodney W. Grimes 
2596be2ac88cSJonathan Lemon 	to->to_flags = 0;
2597df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
2598df8bae1dSRodney W. Grimes 		opt = cp[0];
2599df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
2600df8bae1dSRodney W. Grimes 			break;
2601df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
2602df8bae1dSRodney W. Grimes 			optlen = 1;
2603df8bae1dSRodney W. Grimes 		else {
2604b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
2605b474779fSJun-ichiro itojun Hagino 				break;
2606df8bae1dSRodney W. Grimes 			optlen = cp[1];
2607b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
2608df8bae1dSRodney W. Grimes 				break;
2609df8bae1dSRodney W. Grimes 		}
2610df8bae1dSRodney W. Grimes 		switch (opt) {
2611df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
2612df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
2613df8bae1dSRodney W. Grimes 				continue;
2614f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
2615df8bae1dSRodney W. Grimes 				continue;
2616be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
2617be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
2618be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
2619fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
2620df8bae1dSRodney W. Grimes 			break;
2621df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
2622df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
2623df8bae1dSRodney W. Grimes 				continue;
2624f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
2625df8bae1dSRodney W. Grimes 				continue;
2626be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
262702a1a643SAndre Oppermann 			to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT);
2628df8bae1dSRodney W. Grimes 			break;
2629df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
2630df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
2631df8bae1dSRodney W. Grimes 				continue;
2632be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
2633a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2634a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
2635fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
2636a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
2637a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
2638fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
2639df8bae1dSRodney W. Grimes 			break;
26401cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
26411cfd4b53SBruce M Simpson 		/*
26421cfd4b53SBruce M Simpson 		 * XXX In order to reply to a host which has set the
26431cfd4b53SBruce M Simpson 		 * TCP_SIGNATURE option in its initial SYN, we have to
26441cfd4b53SBruce M Simpson 		 * record the fact that the option was observed here
26451cfd4b53SBruce M Simpson 		 * for the syncache code to perform the correct response.
26461cfd4b53SBruce M Simpson 		 */
26471cfd4b53SBruce M Simpson 		case TCPOPT_SIGNATURE:
26481cfd4b53SBruce M Simpson 			if (optlen != TCPOLEN_SIGNATURE)
26491cfd4b53SBruce M Simpson 				continue;
26501cfd4b53SBruce M Simpson 			to->to_flags |= (TOF_SIGNATURE | TOF_SIGLEN);
26511cfd4b53SBruce M Simpson 			break;
2652265ed012SBruce M Simpson #endif
26536d90faf3SPaul Saab 		case TCPOPT_SACK_PERMITTED:
2654f72167f4SAndre Oppermann 			if (optlen != TCPOLEN_SACK_PERMITTED)
26556d90faf3SPaul Saab 				continue;
2656f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
2657f72167f4SAndre Oppermann 				continue;
2658f72167f4SAndre Oppermann 			if (!tcp_do_sack)
2659f72167f4SAndre Oppermann 				continue;
26606d90faf3SPaul Saab 			to->to_flags |= TOF_SACK;
26616d90faf3SPaul Saab 			break;
26626d90faf3SPaul Saab 		case TCPOPT_SACK:
26635a53ca16SPaul Saab 			if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
26646d90faf3SPaul Saab 				continue;
26655a53ca16SPaul Saab 			to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
26665a53ca16SPaul Saab 			to->to_sacks = cp + 2;
26675a53ca16SPaul Saab 			tcpstat.tcps_sack_rcv_blocks++;
26686d90faf3SPaul Saab 			break;
2669be2ac88cSJonathan Lemon 		default:
2670be2ac88cSJonathan Lemon 			continue;
2671df8bae1dSRodney W. Grimes 		}
2672df8bae1dSRodney W. Grimes 	}
2673df8bae1dSRodney W. Grimes }
2674df8bae1dSRodney W. Grimes 
2675df8bae1dSRodney W. Grimes /*
2676df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
2677df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
2678df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
2679df8bae1dSRodney W. Grimes  * sequencing purposes.
2680df8bae1dSRodney W. Grimes  */
26810312fbe9SPoul-Henning Kamp static void
2682ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m,
2683ad3f9ab3SAndre Oppermann     int off)
2684df8bae1dSRodney W. Grimes {
2685fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
2686df8bae1dSRodney W. Grimes 
2687df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
2688df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
2689df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
2690df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
2691df8bae1dSRodney W. Grimes 
2692df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
2693df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
2694df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
2695df8bae1dSRodney W. Grimes 			m->m_len--;
269669a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
269769a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
2698df8bae1dSRodney W. Grimes 			return;
2699df8bae1dSRodney W. Grimes 		}
2700df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
2701df8bae1dSRodney W. Grimes 		m = m->m_next;
2702df8bae1dSRodney W. Grimes 		if (m == 0)
2703df8bae1dSRodney W. Grimes 			break;
2704df8bae1dSRodney W. Grimes 	}
2705df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
2706df8bae1dSRodney W. Grimes }
2707df8bae1dSRodney W. Grimes 
2708df8bae1dSRodney W. Grimes /*
2709df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
2710df8bae1dSRodney W. Grimes  * and update averages and current timeout.
2711df8bae1dSRodney W. Grimes  */
27120312fbe9SPoul-Henning Kamp static void
2713ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt)
2714df8bae1dSRodney W. Grimes {
2715ad3f9ab3SAndre Oppermann 	int delta;
2716233e8c18SGarrett Wollman 
27172be3bf22SRobert Watson 	INP_LOCK_ASSERT(tp->t_inpcb);
27182be3bf22SRobert Watson 
2719233e8c18SGarrett Wollman 	tcpstat.tcps_rttupdated++;
2720233e8c18SGarrett Wollman 	tp->t_rttupdated++;
2721233e8c18SGarrett Wollman 	if (tp->t_srtt != 0) {
2722233e8c18SGarrett Wollman 		/*
2723233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
2724233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
2725233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
2726233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
2727233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
2728233e8c18SGarrett Wollman 		 */
2729233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
2730233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
2731233e8c18SGarrett Wollman 
2732233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
2733233e8c18SGarrett Wollman 			tp->t_srtt = 1;
2734233e8c18SGarrett Wollman 
2735233e8c18SGarrett Wollman 		/*
2736233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
2737233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
2738233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
2739233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
2740233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
2741233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
2742233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
2743233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
2744233e8c18SGarrett Wollman 		 */
2745233e8c18SGarrett Wollman 		if (delta < 0)
2746233e8c18SGarrett Wollman 			delta = -delta;
2747233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
2748233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
2749233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
27501fcc99b5SMatthew Dillon 		if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
27511fcc99b5SMatthew Dillon 		    tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
2752233e8c18SGarrett Wollman 	} else {
2753233e8c18SGarrett Wollman 		/*
2754233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
2755233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
2756233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
2757233e8c18SGarrett Wollman 		 */
2758233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
2759233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
27601fcc99b5SMatthew Dillon 		tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
2761233e8c18SGarrett Wollman 	}
27629b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
2763df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
2764df8bae1dSRodney W. Grimes 
2765df8bae1dSRodney W. Grimes 	/*
2766df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
2767df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
2768df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
2769df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
2770df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
2771df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
2772df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
2773df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
2774df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
2775df8bae1dSRodney W. Grimes 	 */
2776233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
27779e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
2778df8bae1dSRodney W. Grimes 
2779df8bae1dSRodney W. Grimes 	/*
2780df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
2781df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
2782df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
2783df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
2784df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
2785df8bae1dSRodney W. Grimes 	 */
2786df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
2787df8bae1dSRodney W. Grimes }
2788df8bae1dSRodney W. Grimes 
2789df8bae1dSRodney W. Grimes /*
2790df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
2791df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
2792df8bae1dSRodney W. Grimes  * If none, use an mss that can be handled on the outgoing
2793df8bae1dSRodney W. Grimes  * interface without forcing IP to fragment; if bigger than
2794df8bae1dSRodney W. Grimes  * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
2795df8bae1dSRodney W. Grimes  * to utilize large mbufs.  If no route is found, route has no mtu,
2796df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
2797df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
2798df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
2799df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
2800df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
2801df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
2802df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
2803a0292f23SGarrett Wollman  *
2804a0292f23SGarrett Wollman  * Also take into account the space needed for options that we
2805a0292f23SGarrett Wollman  * send regularly.  Make maxseg shorter by that amount to assure
2806a0292f23SGarrett Wollman  * that we can send maxseg amount of data even when the options
2807a0292f23SGarrett Wollman  * are present.  Store the upper limit of the length of options plus
2808a0292f23SGarrett Wollman  * data in maxopd.
2809a0292f23SGarrett Wollman  *
2810a0292f23SGarrett Wollman  *
2811a0292f23SGarrett Wollman  * In case of T/TCP, we call this routine during implicit connection
2812a0292f23SGarrett Wollman  * setup as well (offer = -1), to initialize maxseg from the cached
2813a0292f23SGarrett Wollman  * MSS of our peer.
281497d8d152SAndre Oppermann  *
281597d8d152SAndre Oppermann  * NOTE that this routine is only called when we process an incoming
281697d8d152SAndre Oppermann  * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt().
2817df8bae1dSRodney W. Grimes  */
2818a0292f23SGarrett Wollman void
2819ad3f9ab3SAndre Oppermann tcp_mss(struct tcpcb *tp, int offer)
2820df8bae1dSRodney W. Grimes {
282197d8d152SAndre Oppermann 	int rtt, mss;
2822df8bae1dSRodney W. Grimes 	u_long bufsize;
282397d8d152SAndre Oppermann 	u_long maxmtu;
2824c068736aSJeffrey Hsu 	struct inpcb *inp = tp->t_inpcb;
2825df8bae1dSRodney W. Grimes 	struct socket *so;
282697d8d152SAndre Oppermann 	struct hc_metrics_lite metrics;
2827a0292f23SGarrett Wollman 	int origoffer = offer;
2828233dcce1SAndre Oppermann 	int mtuflags = 0;
2829fb59c426SYoshinobu Inoue #ifdef INET6
2830c068736aSJeffrey Hsu 	int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
2831c068736aSJeffrey Hsu 	size_t min_protoh = isipv6 ?
2832c068736aSJeffrey Hsu 			    sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
2833c068736aSJeffrey Hsu 			    sizeof (struct tcpiphdr);
2834c068736aSJeffrey Hsu #else
2835c068736aSJeffrey Hsu 	const size_t min_protoh = sizeof(struct tcpiphdr);
2836fb59c426SYoshinobu Inoue #endif
2837df8bae1dSRodney W. Grimes 
283897d8d152SAndre Oppermann 	/* initialize */
283997d8d152SAndre Oppermann #ifdef INET6
284097d8d152SAndre Oppermann 	if (isipv6) {
2841233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(&inp->inp_inc, &mtuflags);
284297d8d152SAndre Oppermann 		tp->t_maxopd = tp->t_maxseg = tcp_v6mssdflt;
284397d8d152SAndre Oppermann 	} else
284497d8d152SAndre Oppermann #endif
284597d8d152SAndre Oppermann 	{
2846233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(&inp->inp_inc, &mtuflags);
284797d8d152SAndre Oppermann 		tp->t_maxopd = tp->t_maxseg = tcp_mssdflt;
2848df8bae1dSRodney W. Grimes 	}
2849df8bae1dSRodney W. Grimes 	so = inp->inp_socket;
2850df8bae1dSRodney W. Grimes 
285197d8d152SAndre Oppermann 	/*
28522d166c02SAndre Oppermann 	 * no route to sender, stay with default mss and return
285397d8d152SAndre Oppermann 	 */
285497d8d152SAndre Oppermann 	if (maxmtu == 0)
285597d8d152SAndre Oppermann 		return;
285697d8d152SAndre Oppermann 
285797d8d152SAndre Oppermann 	/* what have we got? */
285897d8d152SAndre Oppermann 	switch (offer) {
285997d8d152SAndre Oppermann 		case 0:
286097d8d152SAndre Oppermann 			/*
286197d8d152SAndre Oppermann 			 * Offer == 0 means that there was no MSS on the SYN
286297d8d152SAndre Oppermann 			 * segment, in this case we use tcp_mssdflt.
286397d8d152SAndre Oppermann 			 */
286497d8d152SAndre Oppermann 			offer =
286597d8d152SAndre Oppermann #ifdef INET6
286697d8d152SAndre Oppermann 				isipv6 ? tcp_v6mssdflt :
286797d8d152SAndre Oppermann #endif
286897d8d152SAndre Oppermann 				tcp_mssdflt;
286997d8d152SAndre Oppermann 			break;
287097d8d152SAndre Oppermann 
287197d8d152SAndre Oppermann 		case -1:
2872a0292f23SGarrett Wollman 			/*
2873c94c54e4SAndre Oppermann 			 * Offer == -1 means that we didn't receive SYN yet.
2874a0292f23SGarrett Wollman 			 */
287597d8d152SAndre Oppermann 			/* FALLTHROUGH */
287697d8d152SAndre Oppermann 
287797d8d152SAndre Oppermann 		default:
2878a0292f23SGarrett Wollman 			/*
287953369ac9SAndre Oppermann 			 * Prevent DoS attack with too small MSS. Round up
288053369ac9SAndre Oppermann 			 * to at least minmss.
288153369ac9SAndre Oppermann 			 */
288253369ac9SAndre Oppermann 			offer = max(offer, tcp_minmss);
288353369ac9SAndre Oppermann 			/*
2884a0292f23SGarrett Wollman 			 * Sanity check: make sure that maxopd will be large
288597d8d152SAndre Oppermann 			 * enough to allow some data on segments even if the
2886a0292f23SGarrett Wollman 			 * all the option space is used (40bytes).  Otherwise
2887a0292f23SGarrett Wollman 			 * funny things may happen in tcp_output.
2888a0292f23SGarrett Wollman 			 */
2889a0292f23SGarrett Wollman 			offer = max(offer, 64);
289097d8d152SAndre Oppermann 	}
2891a0292f23SGarrett Wollman 
2892df8bae1dSRodney W. Grimes 	/*
289397d8d152SAndre Oppermann 	 * rmx information is now retrieved from tcp_hostcache
2894df8bae1dSRodney W. Grimes 	 */
289597d8d152SAndre Oppermann 	tcp_hc_get(&inp->inp_inc, &metrics);
289697d8d152SAndre Oppermann 
2897df8bae1dSRodney W. Grimes 	/*
289897d8d152SAndre Oppermann 	 * if there's a discovered mtu int tcp hostcache, use it
2899fb59c426SYoshinobu Inoue 	 * else, use the link mtu.
2900df8bae1dSRodney W. Grimes 	 */
290197d8d152SAndre Oppermann 	if (metrics.rmx_mtu)
29022d166c02SAndre Oppermann 		mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
2903c068736aSJeffrey Hsu 	else {
290431b3783cSHajimu UMEMOTO #ifdef INET6
2905fb59c426SYoshinobu Inoue 		if (isipv6) {
290697d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
290797d8d152SAndre Oppermann 			if (!path_mtu_discovery &&
290897d8d152SAndre Oppermann 			    !in6_localaddr(&inp->in6p_faddr))
2909fb59c426SYoshinobu Inoue 				mss = min(mss, tcp_v6mssdflt);
2910b3399803SHajimu UMEMOTO 		} else
2911b3399803SHajimu UMEMOTO #endif
291297d8d152SAndre Oppermann 		{
291397d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
291497d8d152SAndre Oppermann 			if (!path_mtu_discovery &&
291597d8d152SAndre Oppermann 			    !in_localaddr(inp->inp_faddr))
2916a0292f23SGarrett Wollman 				mss = min(mss, tcp_mssdflt);
2917a0292f23SGarrett Wollman 		}
291897d8d152SAndre Oppermann 	}
2919a0292f23SGarrett Wollman 	mss = min(mss, offer);
292097d8d152SAndre Oppermann 
2921a0292f23SGarrett Wollman 	/*
2922a0292f23SGarrett Wollman 	 * maxopd stores the maximum length of data AND options
2923a0292f23SGarrett Wollman 	 * in a segment; maxseg is the amount of data in a normal
2924a0292f23SGarrett Wollman 	 * segment.  We need to store this value (maxopd) apart
2925a0292f23SGarrett Wollman 	 * from maxseg, because now every segment carries options
2926a0292f23SGarrett Wollman 	 * and thus we normally have somewhat less data in segments.
2927a0292f23SGarrett Wollman 	 */
2928a0292f23SGarrett Wollman 	tp->t_maxopd = mss;
2929a0292f23SGarrett Wollman 
2930a0292f23SGarrett Wollman 	/*
2931c94c54e4SAndre Oppermann 	 * origoffer==-1 indicates, that no segments were received yet.
2932c94c54e4SAndre Oppermann 	 * In this case we just guess.
2933a0292f23SGarrett Wollman 	 */
2934a0292f23SGarrett Wollman 	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
2935a0292f23SGarrett Wollman 	    (origoffer == -1 ||
2936a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
2937a0292f23SGarrett Wollman 		mss -= TCPOLEN_TSTAMP_APPA;
293897d8d152SAndre Oppermann 	tp->t_maxseg = mss;
2939a0292f23SGarrett Wollman 
2940df8bae1dSRodney W. Grimes #if	(MCLBYTES & (MCLBYTES - 1)) == 0
2941df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
2942df8bae1dSRodney W. Grimes 			mss &= ~(MCLBYTES-1);
2943df8bae1dSRodney W. Grimes #else
2944df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
2945df8bae1dSRodney W. Grimes 			mss = mss / MCLBYTES * MCLBYTES;
2946df8bae1dSRodney W. Grimes #endif
294797d8d152SAndre Oppermann 	tp->t_maxseg = mss;
294897d8d152SAndre Oppermann 
2949df8bae1dSRodney W. Grimes 	/*
295097d8d152SAndre Oppermann 	 * If there's a pipesize, change the socket buffer to that size,
295197d8d152SAndre Oppermann 	 * don't change if sb_hiwat is different than default (then it
295297d8d152SAndre Oppermann 	 * has been changed on purpose with setsockopt).
295397d8d152SAndre Oppermann 	 * Make the socket buffers an integral number of mss units;
295497d8d152SAndre Oppermann 	 * if the mss is larger than the socket buffer, decrease the mss.
2955df8bae1dSRodney W. Grimes 	 */
29563f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
295797d8d152SAndre Oppermann 	if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe)
295897d8d152SAndre Oppermann 		bufsize = metrics.rmx_sendpipe;
295997d8d152SAndre Oppermann 	else
2960df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
2961df8bae1dSRodney W. Grimes 	if (bufsize < mss)
2962df8bae1dSRodney W. Grimes 		mss = bufsize;
2963df8bae1dSRodney W. Grimes 	else {
2964df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
2965df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
2966df8bae1dSRodney W. Grimes 			bufsize = sb_max;
296788c39af3SRuslan Ermilov 		if (bufsize > so->so_snd.sb_hiwat)
29683f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_snd, bufsize, so, NULL);
2969df8bae1dSRodney W. Grimes 	}
29703f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
2971df8bae1dSRodney W. Grimes 	tp->t_maxseg = mss;
2972df8bae1dSRodney W. Grimes 
29733f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
297497d8d152SAndre Oppermann 	if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe)
297597d8d152SAndre Oppermann 		bufsize = metrics.rmx_recvpipe;
297697d8d152SAndre Oppermann 	else
2977df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
2978df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
2979df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
2980df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
2981df8bae1dSRodney W. Grimes 			bufsize = sb_max;
298288c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
29833f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL);
2984df8bae1dSRodney W. Grimes 	}
29853f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
2986a0292f23SGarrett Wollman 	/*
298797d8d152SAndre Oppermann 	 * While we're here, check the others too
2988a0292f23SGarrett Wollman 	 */
298997d8d152SAndre Oppermann 	if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) {
299097d8d152SAndre Oppermann 		tp->t_srtt = rtt;
299197d8d152SAndre Oppermann 		tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
299297d8d152SAndre Oppermann 		tcpstat.tcps_usedrtt++;
299397d8d152SAndre Oppermann 		if (metrics.rmx_rttvar) {
299497d8d152SAndre Oppermann 			tp->t_rttvar = metrics.rmx_rttvar;
299597d8d152SAndre Oppermann 			tcpstat.tcps_usedrttvar++;
299697d8d152SAndre Oppermann 		} else {
299797d8d152SAndre Oppermann 			/* default variation is +- 1 rtt */
299897d8d152SAndre Oppermann 			tp->t_rttvar =
299997d8d152SAndre Oppermann 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
300097d8d152SAndre Oppermann 		}
300197d8d152SAndre Oppermann 		TCPT_RANGESET(tp->t_rxtcur,
300297d8d152SAndre Oppermann 			      ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
300397d8d152SAndre Oppermann 			      tp->t_rttmin, TCPTV_REXMTMAX);
300497d8d152SAndre Oppermann 	}
300597d8d152SAndre Oppermann 	if (metrics.rmx_ssthresh) {
3006df8bae1dSRodney W. Grimes 		/*
3007df8bae1dSRodney W. Grimes 		 * There's some sort of gateway or interface
3008df8bae1dSRodney W. Grimes 		 * buffer limit on the path.  Use this to set
3009df8bae1dSRodney W. Grimes 		 * the slow start threshhold, but set the
3010df8bae1dSRodney W. Grimes 		 * threshold to no less than 2*mss.
3011df8bae1dSRodney W. Grimes 		 */
301297d8d152SAndre Oppermann 		tp->snd_ssthresh = max(2 * mss, metrics.rmx_ssthresh);
3013dd224982SGarrett Wollman 		tcpstat.tcps_usedssthresh++;
3014df8bae1dSRodney W. Grimes 	}
301597d8d152SAndre Oppermann 	if (metrics.rmx_bandwidth)
301697d8d152SAndre Oppermann 		tp->snd_bandwidth = metrics.rmx_bandwidth;
301797d8d152SAndre Oppermann 
301897d8d152SAndre Oppermann 	/*
301997d8d152SAndre Oppermann 	 * Set the slow-start flight size depending on whether this
302097d8d152SAndre Oppermann 	 * is a local network or not.
302197d8d152SAndre Oppermann 	 *
302297d8d152SAndre Oppermann 	 * Extend this so we cache the cwnd too and retrieve it here.
302397d8d152SAndre Oppermann 	 * Make cwnd even bigger than RFC3390 suggests but only if we
302497d8d152SAndre Oppermann 	 * have previous experience with the remote host. Be careful
302597d8d152SAndre Oppermann 	 * not make cwnd bigger than remote receive window or our own
302697d8d152SAndre Oppermann 	 * send socket buffer. Maybe put some additional upper bound
302797d8d152SAndre Oppermann 	 * on the retrieved cwnd. Should do incremental updates to
302897d8d152SAndre Oppermann 	 * hostcache when cwnd collapses so next connection doesn't
302997d8d152SAndre Oppermann 	 * overloads the path again.
303097d8d152SAndre Oppermann 	 *
303197d8d152SAndre Oppermann 	 * RFC3390 says only do this if SYN or SYN/ACK didn't got lost.
303297d8d152SAndre Oppermann 	 * We currently check only in syncache_socket for that.
303397d8d152SAndre Oppermann 	 */
303497d8d152SAndre Oppermann #define TCP_METRICS_CWND
303597d8d152SAndre Oppermann #ifdef TCP_METRICS_CWND
303697d8d152SAndre Oppermann 	if (metrics.rmx_cwnd)
303797d8d152SAndre Oppermann 		tp->snd_cwnd = max(mss,
303897d8d152SAndre Oppermann 				min(metrics.rmx_cwnd / 2,
303997d8d152SAndre Oppermann 				 min(tp->snd_wnd, so->so_snd.sb_hiwat)));
304097d8d152SAndre Oppermann 	else
304197d8d152SAndre Oppermann #endif
304297d8d152SAndre Oppermann 	if (tcp_do_rfc3390)
304397d8d152SAndre Oppermann 		tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380));
304497d8d152SAndre Oppermann #ifdef INET6
304597d8d152SAndre Oppermann 	else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) ||
304697d8d152SAndre Oppermann 		 (!isipv6 && in_localaddr(inp->inp_faddr)))
3047943ae302SAndre Oppermann #else
3048943ae302SAndre Oppermann 	else if (in_localaddr(inp->inp_faddr))
304997d8d152SAndre Oppermann #endif
3050943ae302SAndre Oppermann 		tp->snd_cwnd = mss * ss_fltsz_local;
305197d8d152SAndre Oppermann 	else
305297d8d152SAndre Oppermann 		tp->snd_cwnd = mss * ss_fltsz;
3053233dcce1SAndre Oppermann 
3054233dcce1SAndre Oppermann 	/* Check the interface for TSO capabilities. */
3055233dcce1SAndre Oppermann 	if (mtuflags & CSUM_TSO)
3056233dcce1SAndre Oppermann 		tp->t_flags |= TF_TSO;
3057a0292f23SGarrett Wollman }
3058a0292f23SGarrett Wollman 
3059a0292f23SGarrett Wollman /*
3060a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
3061a0292f23SGarrett Wollman  */
3062a0292f23SGarrett Wollman int
3063ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc)
3064a0292f23SGarrett Wollman {
306597d8d152SAndre Oppermann 	int mss = 0;
306697d8d152SAndre Oppermann 	u_long maxmtu = 0;
306797d8d152SAndre Oppermann 	u_long thcmtu = 0;
306897d8d152SAndre Oppermann 	size_t min_protoh;
3069fb59c426SYoshinobu Inoue #ifdef INET6
307097d8d152SAndre Oppermann 	int isipv6 = inc->inc_isipv6 ? 1 : 0;
3071fb59c426SYoshinobu Inoue #endif
3072a0292f23SGarrett Wollman 
307397d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3074a0292f23SGarrett Wollman 
307531b3783cSHajimu UMEMOTO #ifdef INET6
307697d8d152SAndre Oppermann 	if (isipv6) {
307797d8d152SAndre Oppermann 		mss = tcp_v6mssdflt;
3078233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(inc, NULL);
307997d8d152SAndre Oppermann 		thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
308097d8d152SAndre Oppermann 		min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
308197d8d152SAndre Oppermann 	} else
308231b3783cSHajimu UMEMOTO #endif
308397d8d152SAndre Oppermann 	{
308497d8d152SAndre Oppermann 		mss = tcp_mssdflt;
3085233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(inc, NULL);
308697d8d152SAndre Oppermann 		thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
308797d8d152SAndre Oppermann 		min_protoh = sizeof(struct tcpiphdr);
308897d8d152SAndre Oppermann 	}
308997d8d152SAndre Oppermann 	if (maxmtu && thcmtu)
309097d8d152SAndre Oppermann 		mss = min(maxmtu, thcmtu) - min_protoh;
309197d8d152SAndre Oppermann 	else if (maxmtu || thcmtu)
309297d8d152SAndre Oppermann 		mss = max(maxmtu, thcmtu) - min_protoh;
309397d8d152SAndre Oppermann 
309497d8d152SAndre Oppermann 	return (mss);
3095df8bae1dSRodney W. Grimes }
309646f58482SJonathan Lemon 
309746f58482SJonathan Lemon 
309846f58482SJonathan Lemon /*
3099c068736aSJeffrey Hsu  * On a partial ack arrives, force the retransmission of the
3100c068736aSJeffrey Hsu  * next unacknowledged segment.  Do not clear tp->t_dupacks.
3101c068736aSJeffrey Hsu  * By setting snd_nxt to ti_ack, this forces retransmission timer to
3102c068736aSJeffrey Hsu  * be started again.
310346f58482SJonathan Lemon  */
3104c068736aSJeffrey Hsu static void
3105ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
310646f58482SJonathan Lemon {
310746f58482SJonathan Lemon 	tcp_seq onxt = tp->snd_nxt;
310846f58482SJonathan Lemon 	u_long  ocwnd = tp->snd_cwnd;
310946f58482SJonathan Lemon 
311046f58482SJonathan Lemon 	callout_stop(tp->tt_rexmt);
311146f58482SJonathan Lemon 	tp->t_rtttime = 0;
311246f58482SJonathan Lemon 	tp->snd_nxt = th->th_ack;
31136b2a5f92SJayanth Vijayaraghavan 	/*
3114c068736aSJeffrey Hsu 	 * Set snd_cwnd to one segment beyond acknowledged offset.
3115c068736aSJeffrey Hsu 	 * (tp->snd_una has not yet been updated when this function is called.)
31166b2a5f92SJayanth Vijayaraghavan 	 */
31176b2a5f92SJayanth Vijayaraghavan 	tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una);
3118a84db8f4SMatthew Dillon 	tp->t_flags |= TF_ACKNOW;
31196b2a5f92SJayanth Vijayaraghavan 	(void) tcp_output(tp);
312046f58482SJonathan Lemon 	tp->snd_cwnd = ocwnd;
312146f58482SJonathan Lemon 	if (SEQ_GT(onxt, tp->snd_nxt))
312246f58482SJonathan Lemon 		tp->snd_nxt = onxt;
312346f58482SJonathan Lemon 	/*
312446f58482SJonathan Lemon 	 * Partial window deflation.  Relies on fact that tp->snd_una
312546f58482SJonathan Lemon 	 * not updated yet.
312646f58482SJonathan Lemon 	 */
3127d7587117SPaul Saab 	if (tp->snd_cwnd > th->th_ack - tp->snd_una)
3128d7587117SPaul Saab 		tp->snd_cwnd -= th->th_ack - tp->snd_una;
3129d7587117SPaul Saab 	else
3130d7587117SPaul Saab 		tp->snd_cwnd = 0;
3131d7587117SPaul Saab 	tp->snd_cwnd += tp->t_maxseg;
313246f58482SJonathan Lemon }
3133340c35deSJonathan Lemon 
3134340c35deSJonathan Lemon /*
3135340c35deSJonathan Lemon  * Returns 1 if the TIME_WAIT state was killed and we should start over,
3136340c35deSJonathan Lemon  * looking for a pcb in the listen state.  Returns 0 otherwise.
3137340c35deSJonathan Lemon  */
3138340c35deSJonathan Lemon static int
3139ad3f9ab3SAndre Oppermann tcp_timewait(struct inpcb *inp, struct tcpopt *to, struct tcphdr *th,
3140ad3f9ab3SAndre Oppermann     struct mbuf *m, int tlen)
3141340c35deSJonathan Lemon {
31423cbe7fafSRobert Watson 	struct tcptw *tw;
3143340c35deSJonathan Lemon 	int thflags;
3144340c35deSJonathan Lemon 	tcp_seq seq;
3145340c35deSJonathan Lemon #ifdef INET6
3146340c35deSJonathan Lemon 	int isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
3147340c35deSJonathan Lemon #else
3148340c35deSJonathan Lemon 	const int isipv6 = 0;
3149340c35deSJonathan Lemon #endif
3150340c35deSJonathan Lemon 
3151751dea29SRuslan Ermilov 	/* tcbinfo lock required for tcp_twclose(), tcp_timer_2msl_reset(). */
31523cbe7fafSRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
31533cbe7fafSRobert Watson 	INP_LOCK_ASSERT(inp);
31543cbe7fafSRobert Watson 
3155ae0e7143SRobert Watson 	/*
3156ae0e7143SRobert Watson 	 * XXXRW: Time wait state for inpcb has been recycled, but inpcb is
3157ae0e7143SRobert Watson 	 * still present.  This is undesirable, but temporarily necessary
3158ae0e7143SRobert Watson 	 * until we work out how to handle inpcb's who's timewait state has
3159ae0e7143SRobert Watson 	 * been removed.
3160ae0e7143SRobert Watson 	 */
31613cbe7fafSRobert Watson 	tw = intotw(inp);
3162ae0e7143SRobert Watson 	if (tw == NULL)
3163ae0e7143SRobert Watson 		goto drop;
3164ae0e7143SRobert Watson 
3165340c35deSJonathan Lemon 	thflags = th->th_flags;
3166340c35deSJonathan Lemon 
3167340c35deSJonathan Lemon 	/*
3168340c35deSJonathan Lemon 	 * NOTE: for FIN_WAIT_2 (to be added later),
3169340c35deSJonathan Lemon 	 * must validate sequence number before accepting RST
3170340c35deSJonathan Lemon 	 */
3171340c35deSJonathan Lemon 
3172340c35deSJonathan Lemon 	/*
3173340c35deSJonathan Lemon 	 * If the segment contains RST:
3174340c35deSJonathan Lemon 	 *	Drop the segment - see Stevens, vol. 2, p. 964 and
3175340c35deSJonathan Lemon 	 *      RFC 1337.
3176340c35deSJonathan Lemon 	 */
3177340c35deSJonathan Lemon 	if (thflags & TH_RST)
3178340c35deSJonathan Lemon 		goto drop;
3179340c35deSJonathan Lemon 
3180340c35deSJonathan Lemon #if 0
3181340c35deSJonathan Lemon /* PAWS not needed at the moment */
3182340c35deSJonathan Lemon 	/*
3183340c35deSJonathan Lemon 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
3184340c35deSJonathan Lemon 	 * and it's less than ts_recent, drop it.
3185340c35deSJonathan Lemon 	 */
3186340c35deSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
3187340c35deSJonathan Lemon 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
3188340c35deSJonathan Lemon 		if ((thflags & TH_ACK) == 0)
3189340c35deSJonathan Lemon 			goto drop;
3190340c35deSJonathan Lemon 		goto ack;
3191340c35deSJonathan Lemon 	}
3192340c35deSJonathan Lemon 	/*
3193340c35deSJonathan Lemon 	 * ts_recent is never updated because we never accept new segments.
3194340c35deSJonathan Lemon 	 */
3195340c35deSJonathan Lemon #endif
3196340c35deSJonathan Lemon 
3197340c35deSJonathan Lemon 	/*
3198340c35deSJonathan Lemon 	 * If a new connection request is received
3199340c35deSJonathan Lemon 	 * while in TIME_WAIT, drop the old connection
3200340c35deSJonathan Lemon 	 * and start over if the sequence numbers
3201340c35deSJonathan Lemon 	 * are above the previous ones.
3202340c35deSJonathan Lemon 	 */
3203340c35deSJonathan Lemon 	if ((thflags & TH_SYN) && SEQ_GT(th->th_seq, tw->rcv_nxt)) {
3204623dce13SRobert Watson 		tcp_twclose(tw, 0);
3205340c35deSJonathan Lemon 		return (1);
3206340c35deSJonathan Lemon 	}
3207340c35deSJonathan Lemon 
3208340c35deSJonathan Lemon 	/*
3209340c35deSJonathan Lemon 	 * Drop the the segment if it does not contain an ACK.
3210340c35deSJonathan Lemon 	 */
3211340c35deSJonathan Lemon 	if ((thflags & TH_ACK) == 0)
3212340c35deSJonathan Lemon 		goto drop;
3213340c35deSJonathan Lemon 
3214340c35deSJonathan Lemon 	/*
3215340c35deSJonathan Lemon 	 * Reset the 2MSL timer if this is a duplicate FIN.
3216340c35deSJonathan Lemon 	 */
3217340c35deSJonathan Lemon 	if (thflags & TH_FIN) {
3218340c35deSJonathan Lemon 		seq = th->th_seq + tlen + (thflags & TH_SYN ? 1 : 0);
3219340c35deSJonathan Lemon 		if (seq + 1 == tw->rcv_nxt)
3220751dea29SRuslan Ermilov 			tcp_timer_2msl_reset(tw, 1);
3221340c35deSJonathan Lemon 	}
3222340c35deSJonathan Lemon 
3223340c35deSJonathan Lemon 	/*
3224272c5dfeSJonathan Lemon 	 * Acknowledge the segment if it has data or is not a duplicate ACK.
3225340c35deSJonathan Lemon 	 */
3226272c5dfeSJonathan Lemon 	if (thflags != TH_ACK || tlen != 0 ||
3227272c5dfeSJonathan Lemon 	    th->th_seq != tw->rcv_nxt || th->th_ack != tw->snd_nxt)
3228a7b6a14aSRobert Watson 		tcp_twrespond(tw, TH_ACK);
3229340c35deSJonathan Lemon 	goto drop;
3230340c35deSJonathan Lemon 
3231340c35deSJonathan Lemon 	/*
3232340c35deSJonathan Lemon 	 * Generate a RST, dropping incoming segment.
3233340c35deSJonathan Lemon 	 * Make ACK acceptable to originator of segment.
3234340c35deSJonathan Lemon 	 * Don't bother to respond if destination was broadcast/multicast.
3235340c35deSJonathan Lemon 	 */
3236340c35deSJonathan Lemon 	if (m->m_flags & (M_BCAST|M_MCAST))
3237340c35deSJonathan Lemon 		goto drop;
3238340c35deSJonathan Lemon 	if (isipv6) {
3239340c35deSJonathan Lemon 		struct ip6_hdr *ip6;
3240340c35deSJonathan Lemon 
3241340c35deSJonathan Lemon 		/* IPv6 anycast check is done at tcp6_input() */
3242340c35deSJonathan Lemon 		ip6 = mtod(m, struct ip6_hdr *);
3243340c35deSJonathan Lemon 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3244340c35deSJonathan Lemon 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3245340c35deSJonathan Lemon 			goto drop;
3246340c35deSJonathan Lemon 	} else {
3247340c35deSJonathan Lemon 		struct ip *ip;
3248340c35deSJonathan Lemon 
3249340c35deSJonathan Lemon 		ip = mtod(m, struct ip *);
3250340c35deSJonathan Lemon 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3251340c35deSJonathan Lemon 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3252340c35deSJonathan Lemon 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3253340c35deSJonathan Lemon 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
3254340c35deSJonathan Lemon 			goto drop;
3255340c35deSJonathan Lemon 	}
3256340c35deSJonathan Lemon 	if (thflags & TH_ACK) {
3257340c35deSJonathan Lemon 		tcp_respond(NULL,
3258340c35deSJonathan Lemon 		    mtod(m, void *), th, m, 0, th->th_ack, TH_RST);
3259340c35deSJonathan Lemon 	} else {
3260340c35deSJonathan Lemon 		seq = th->th_seq + (thflags & TH_SYN ? 1 : 0);
3261340c35deSJonathan Lemon 		tcp_respond(NULL,
3262340c35deSJonathan Lemon 		    mtod(m, void *), th, m, seq, 0, TH_RST|TH_ACK);
3263340c35deSJonathan Lemon 	}
32643cbe7fafSRobert Watson 	INP_UNLOCK(inp);
3265340c35deSJonathan Lemon 	return (0);
3266340c35deSJonathan Lemon 
3267340c35deSJonathan Lemon drop:
32683cbe7fafSRobert Watson 	INP_UNLOCK(inp);
3269340c35deSJonathan Lemon 	m_freem(m);
3270340c35deSJonathan Lemon 	return (0);
3271340c35deSJonathan Lemon }
3272