xref: /freebsd/sys/netinet/tcp_input.c (revision be2ac88c59ecd9c64240df8d72fcef339fe9aed4)
1df8bae1dSRodney W. Grimes /*
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  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33e79adb8eSGarrett Wollman  *	@(#)tcp_input.c	8.12 (Berkeley) 5/24/95
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37f9e354dfSJulian Elischer #include "opt_ipfw.h"		/* for ipfw_fwd		*/
38fb59c426SYoshinobu Inoue #include "opt_inet6.h"
393a2a9f79SYoshinobu Inoue #include "opt_ipsec.h"
400cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
41e46cd3d4SDag-Erling Smørgrav #include "opt_tcp_input.h"
420cc12cc5SJoerg Wunsch 
43df8bae1dSRodney W. Grimes #include <sys/param.h>
44df8bae1dSRodney W. Grimes #include <sys/systm.h>
4598163b98SPoul-Henning Kamp #include <sys/kernel.h>
4698163b98SPoul-Henning Kamp #include <sys/sysctl.h>
47df8bae1dSRodney W. Grimes #include <sys/malloc.h>
48df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
49a29f300eSGarrett Wollman #include <sys/proc.h>		/* for proc0 declaration */
50df8bae1dSRodney W. Grimes #include <sys/protosw.h>
51df8bae1dSRodney W. Grimes #include <sys/socket.h>
52df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
53816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
54df8bae1dSRodney W. Grimes 
55e79adb8eSGarrett Wollman #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
56e79adb8eSGarrett Wollman 
57df8bae1dSRodney W. Grimes #include <net/if.h>
58df8bae1dSRodney W. Grimes #include <net/route.h>
59df8bae1dSRodney W. Grimes 
60df8bae1dSRodney W. Grimes #include <netinet/in.h>
61df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
62df8bae1dSRodney W. Grimes #include <netinet/ip.h>
63686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_icmp.h>	/* for ICMP_BANDLIM		*/
64fb59c426SYoshinobu Inoue #include <netinet/in_var.h>
65686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM		*/
66df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
67df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
68fb59c426SYoshinobu Inoue #ifdef INET6
69686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
70686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
71686cdd19SJun-ichiro itojun Hagino #include <netinet6/nd6.h>
72fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
73686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
74fb59c426SYoshinobu Inoue #endif
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 #ifdef INET6
81fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h>
82fb59c426SYoshinobu Inoue #endif
83df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
84610ee2f9SDavid Greenman #ifdef TCPDEBUG
85df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
86fb59c426SYoshinobu Inoue 
87fb59c426SYoshinobu Inoue u_char tcp_saveipgen[40]; /* the size must be of max ip header, now IPv6 */
88fb59c426SYoshinobu Inoue struct tcphdr tcp_savetcp;
89fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */
90fb59c426SYoshinobu Inoue 
91fb59c426SYoshinobu Inoue #ifdef IPSEC
92fb59c426SYoshinobu Inoue #include <netinet6/ipsec.h>
933a2a9f79SYoshinobu Inoue #ifdef INET6
943a2a9f79SYoshinobu Inoue #include <netinet6/ipsec6.h>
953a2a9f79SYoshinobu Inoue #endif
96fb59c426SYoshinobu Inoue #include <netkey/key.h>
97fb59c426SYoshinobu Inoue #endif /*IPSEC*/
98fb59c426SYoshinobu Inoue 
99db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
100db4f9cc7SJonathan Lemon 
101fb59c426SYoshinobu Inoue MALLOC_DEFINE(M_TSEGQ, "tseg_qent", "TCP segment queue entry");
102df8bae1dSRodney W. Grimes 
1030312fbe9SPoul-Henning Kamp static int	tcprexmtthresh = 3;
1042f96f1f4SGarrett Wollman tcp_cc	tcp_ccgen;
1050312fbe9SPoul-Henning Kamp 
1062f96f1f4SGarrett Wollman struct	tcpstat tcpstat;
107c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW,
1083d177f46SBill Fumerola     &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
1090312fbe9SPoul-Henning Kamp 
110d78a37adSPaul Traina static int log_in_vain = 0;
111816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
1123d177f46SBill Fumerola     &log_in_vain, 0, "Log all incoming TCP connections");
113816a3d83SPoul-Henning Kamp 
11416f7f31fSGeoff Rehmet static int blackhole = 0;
11516f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW,
11616f7f31fSGeoff Rehmet 	&blackhole, 0, "Do not send RST when dropping refused connections");
11716f7f31fSGeoff Rehmet 
118f498eeeeSDavid Greenman int tcp_delack_enabled = 1;
11984e33c9eSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW,
1203d177f46SBill Fumerola     &tcp_delack_enabled, 0,
1213d177f46SBill Fumerola     "Delay ACK to try and piggyback it onto a data packet");
122f498eeeeSDavid Greenman 
123f8613305SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN
124f8613305SDag-Erling Smørgrav static int drop_synfin = 0;
125f8613305SDag-Erling Smørgrav SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW,
126f8613305SDag-Erling Smørgrav     &drop_synfin, 0, "Drop TCP packets with SYN+FIN set");
127f8613305SDag-Erling Smørgrav #endif
128f8613305SDag-Erling Smørgrav 
12915bd2b43SDavid Greenman struct inpcbhead tcb;
130fb59c426SYoshinobu Inoue #define	tcb6	tcb  /* for KAME src sync over BSD*'s */
13115bd2b43SDavid Greenman struct inpcbinfo tcbinfo;
132df8bae1dSRodney W. Grimes 
133be2ac88cSJonathan Lemon static void	 tcp_dooptions __P((struct tcpopt *, u_char *, int, int));
1340312fbe9SPoul-Henning Kamp static void	 tcp_pulloutofband __P((struct socket *,
135fb59c426SYoshinobu Inoue 		     struct tcphdr *, struct mbuf *, int));
136fb59c426SYoshinobu Inoue static int	 tcp_reass __P((struct tcpcb *, struct tcphdr *, int *,
137fb59c426SYoshinobu Inoue 				struct mbuf *));
1380312fbe9SPoul-Henning Kamp static void	 tcp_xmit_timer __P((struct tcpcb *, int));
13946f58482SJonathan Lemon static int	 tcp_newreno __P((struct tcpcb *, struct tcphdr *));
1400312fbe9SPoul-Henning Kamp 
141fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
142fb59c426SYoshinobu Inoue #ifdef INET6
143fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \
144fb59c426SYoshinobu Inoue do { \
145fb59c426SYoshinobu Inoue 	if ((tp) && (tp)->t_inpcb && \
146fb59c426SYoshinobu Inoue 	    ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0 && \
147fb59c426SYoshinobu Inoue 	    (tp)->t_inpcb->in6p_route.ro_rt) \
148686cdd19SJun-ichiro itojun Hagino 		nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL, 0); \
149fb59c426SYoshinobu Inoue } while (0)
150fb59c426SYoshinobu Inoue #else
151fb59c426SYoshinobu Inoue #define ND6_HINT(tp)
152fb59c426SYoshinobu Inoue #endif
153df8bae1dSRodney W. Grimes 
154df8bae1dSRodney W. Grimes /*
155d8c85a26SJonathan Lemon  * Indicate whether this ack should be delayed.
156d8c85a26SJonathan Lemon  */
157d8c85a26SJonathan Lemon #define DELAY_ACK(tp) \
158d8c85a26SJonathan Lemon 	(tcp_delack_enabled && !callout_pending(tp->tt_delack))
159d8c85a26SJonathan Lemon 
1600312fbe9SPoul-Henning Kamp static int
161fb59c426SYoshinobu Inoue tcp_reass(tp, th, tlenp, m)
162df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
163fb59c426SYoshinobu Inoue 	register struct tcphdr *th;
164fb59c426SYoshinobu Inoue 	int *tlenp;
165df8bae1dSRodney W. Grimes 	struct mbuf *m;
166df8bae1dSRodney W. Grimes {
167fb59c426SYoshinobu Inoue 	struct tseg_qent *q;
168fb59c426SYoshinobu Inoue 	struct tseg_qent *p = NULL;
169fb59c426SYoshinobu Inoue 	struct tseg_qent *nq;
170fb59c426SYoshinobu Inoue 	struct tseg_qent *te;
171df8bae1dSRodney W. Grimes 	struct socket *so = tp->t_inpcb->inp_socket;
172df8bae1dSRodney W. Grimes 	int flags;
173df8bae1dSRodney W. Grimes 
174df8bae1dSRodney W. Grimes 	/*
175fb59c426SYoshinobu Inoue 	 * Call with th==0 after become established to
176df8bae1dSRodney W. Grimes 	 * force pre-ESTABLISHED data up to user socket.
177df8bae1dSRodney W. Grimes 	 */
178fb59c426SYoshinobu Inoue 	if (th == 0)
179df8bae1dSRodney W. Grimes 		goto present;
180df8bae1dSRodney W. Grimes 
181fb59c426SYoshinobu Inoue 	/* Allocate a new queue entry. If we can't, just drop the pkt. XXX */
182fb59c426SYoshinobu Inoue 	MALLOC(te, struct tseg_qent *, sizeof (struct tseg_qent), M_TSEGQ,
183fb59c426SYoshinobu Inoue 	       M_NOWAIT);
184fb59c426SYoshinobu Inoue 	if (te == NULL) {
185fb59c426SYoshinobu Inoue 		tcpstat.tcps_rcvmemdrop++;
186fb59c426SYoshinobu Inoue 		m_freem(m);
187fb59c426SYoshinobu Inoue 		return (0);
188fb59c426SYoshinobu Inoue 	}
1896effc713SDoug Rabson 
190df8bae1dSRodney W. Grimes 	/*
191df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
192df8bae1dSRodney W. Grimes 	 */
193fb59c426SYoshinobu Inoue 	LIST_FOREACH(q, &tp->t_segq, tqe_q) {
194fb59c426SYoshinobu Inoue 		if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
195df8bae1dSRodney W. Grimes 			break;
196fb59c426SYoshinobu Inoue 		p = q;
197fb59c426SYoshinobu Inoue 	}
198df8bae1dSRodney W. Grimes 
199df8bae1dSRodney W. Grimes 	/*
200df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
201df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
202df8bae1dSRodney W. Grimes 	 * segment.  If it provides all of our data, drop us.
203df8bae1dSRodney W. Grimes 	 */
2046effc713SDoug Rabson 	if (p != NULL) {
205df8bae1dSRodney W. Grimes 		register int i;
206df8bae1dSRodney W. Grimes 		/* conversion to int (in i) handles seq wraparound */
207fb59c426SYoshinobu Inoue 		i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
208df8bae1dSRodney W. Grimes 		if (i > 0) {
209fb59c426SYoshinobu Inoue 			if (i >= *tlenp) {
210df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvduppack++;
211fb59c426SYoshinobu Inoue 				tcpstat.tcps_rcvdupbyte += *tlenp;
212df8bae1dSRodney W. Grimes 				m_freem(m);
213fb59c426SYoshinobu Inoue 				FREE(te, M_TSEGQ);
214a0292f23SGarrett Wollman 				/*
215a0292f23SGarrett Wollman 				 * Try to present any queued data
216a0292f23SGarrett Wollman 				 * at the left window edge to the user.
217a0292f23SGarrett Wollman 				 * This is needed after the 3-WHS
218a0292f23SGarrett Wollman 				 * completes.
219a0292f23SGarrett Wollman 				 */
220a0292f23SGarrett Wollman 				goto present;	/* ??? */
221df8bae1dSRodney W. Grimes 			}
222df8bae1dSRodney W. Grimes 			m_adj(m, i);
223fb59c426SYoshinobu Inoue 			*tlenp -= i;
224fb59c426SYoshinobu Inoue 			th->th_seq += i;
225df8bae1dSRodney W. Grimes 		}
226df8bae1dSRodney W. Grimes 	}
227df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvoopack++;
228fb59c426SYoshinobu Inoue 	tcpstat.tcps_rcvoobyte += *tlenp;
229df8bae1dSRodney W. Grimes 
230df8bae1dSRodney W. Grimes 	/*
231df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
232df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
233df8bae1dSRodney W. Grimes 	 */
2346effc713SDoug Rabson 	while (q) {
235fb59c426SYoshinobu Inoue 		register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
236df8bae1dSRodney W. Grimes 		if (i <= 0)
237df8bae1dSRodney W. Grimes 			break;
238fb59c426SYoshinobu Inoue 		if (i < q->tqe_len) {
239fb59c426SYoshinobu Inoue 			q->tqe_th->th_seq += i;
240fb59c426SYoshinobu Inoue 			q->tqe_len -= i;
241fb59c426SYoshinobu Inoue 			m_adj(q->tqe_m, i);
242df8bae1dSRodney W. Grimes 			break;
243df8bae1dSRodney W. Grimes 		}
2446effc713SDoug Rabson 
245fb59c426SYoshinobu Inoue 		nq = LIST_NEXT(q, tqe_q);
246fb59c426SYoshinobu Inoue 		LIST_REMOVE(q, tqe_q);
247fb59c426SYoshinobu Inoue 		m_freem(q->tqe_m);
248fb59c426SYoshinobu Inoue 		FREE(q, M_TSEGQ);
2496effc713SDoug Rabson 		q = nq;
250df8bae1dSRodney W. Grimes 	}
251df8bae1dSRodney W. Grimes 
252fb59c426SYoshinobu Inoue 	/* Insert the new segment queue entry into place. */
253fb59c426SYoshinobu Inoue 	te->tqe_m = m;
254fb59c426SYoshinobu Inoue 	te->tqe_th = th;
255fb59c426SYoshinobu Inoue 	te->tqe_len = *tlenp;
256fb59c426SYoshinobu Inoue 
2576effc713SDoug Rabson 	if (p == NULL) {
258fb59c426SYoshinobu Inoue 		LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
2596effc713SDoug Rabson 	} else {
260fb59c426SYoshinobu Inoue 		LIST_INSERT_AFTER(p, te, tqe_q);
2616effc713SDoug Rabson 	}
262df8bae1dSRodney W. Grimes 
263df8bae1dSRodney W. Grimes present:
264df8bae1dSRodney W. Grimes 	/*
265df8bae1dSRodney W. Grimes 	 * Present data to user, advancing rcv_nxt through
266df8bae1dSRodney W. Grimes 	 * completed sequence space.
267df8bae1dSRodney W. Grimes 	 */
268a0292f23SGarrett Wollman 	if (!TCPS_HAVEESTABLISHED(tp->t_state))
269df8bae1dSRodney W. Grimes 		return (0);
270fb59c426SYoshinobu Inoue 	q = LIST_FIRST(&tp->t_segq);
271fb59c426SYoshinobu Inoue 	if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
272df8bae1dSRodney W. Grimes 		return (0);
273df8bae1dSRodney W. Grimes 	do {
274fb59c426SYoshinobu Inoue 		tp->rcv_nxt += q->tqe_len;
275fb59c426SYoshinobu Inoue 		flags = q->tqe_th->th_flags & TH_FIN;
276fb59c426SYoshinobu Inoue 		nq = LIST_NEXT(q, tqe_q);
277fb59c426SYoshinobu Inoue 		LIST_REMOVE(q, tqe_q);
278df8bae1dSRodney W. Grimes 		if (so->so_state & SS_CANTRCVMORE)
279fb59c426SYoshinobu Inoue 			m_freem(q->tqe_m);
280df8bae1dSRodney W. Grimes 		else
281fb59c426SYoshinobu Inoue 			sbappend(&so->so_rcv, q->tqe_m);
282fb59c426SYoshinobu Inoue 		FREE(q, M_TSEGQ);
2836effc713SDoug Rabson 		q = nq;
284fb59c426SYoshinobu Inoue 	} while (q && q->tqe_th->th_seq == tp->rcv_nxt);
285fb59c426SYoshinobu Inoue 	ND6_HINT(tp);
286df8bae1dSRodney W. Grimes 	sorwakeup(so);
287df8bae1dSRodney W. Grimes 	return (flags);
288df8bae1dSRodney W. Grimes }
289df8bae1dSRodney W. Grimes 
290df8bae1dSRodney W. Grimes /*
291df8bae1dSRodney W. Grimes  * TCP input routine, follows pages 65-76 of the
292df8bae1dSRodney W. Grimes  * protocol specification dated September, 1981 very closely.
293df8bae1dSRodney W. Grimes  */
294fb59c426SYoshinobu Inoue #ifdef INET6
295fb59c426SYoshinobu Inoue int
296fb59c426SYoshinobu Inoue tcp6_input(mp, offp, proto)
297fb59c426SYoshinobu Inoue 	struct mbuf **mp;
298fb59c426SYoshinobu Inoue 	int *offp, proto;
299fb59c426SYoshinobu Inoue {
300fb59c426SYoshinobu Inoue 	register struct mbuf *m = *mp;
30133841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
302fb59c426SYoshinobu Inoue 
303fb59c426SYoshinobu Inoue 	IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
304fb59c426SYoshinobu Inoue 
305fb59c426SYoshinobu Inoue 	/*
306fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
307fb59c426SYoshinobu Inoue 	 * better place to put this in?
308fb59c426SYoshinobu Inoue 	 */
30933841545SHajimu UMEMOTO 	ia6 = ip6_getdstifaddr(m);
31033841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
311fb59c426SYoshinobu Inoue 		struct ip6_hdr *ip6;
312fb59c426SYoshinobu Inoue 
313fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
314fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
315fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
316fb59c426SYoshinobu Inoue 		return IPPROTO_DONE;
317fb59c426SYoshinobu Inoue 	}
318fb59c426SYoshinobu Inoue 
319f0ffb944SJulian Elischer 	tcp_input(m, *offp);
320fb59c426SYoshinobu Inoue 	return IPPROTO_DONE;
321fb59c426SYoshinobu Inoue }
322fb59c426SYoshinobu Inoue #endif
323fb59c426SYoshinobu Inoue 
324df8bae1dSRodney W. Grimes void
325f0ffb944SJulian Elischer tcp_input(m, off0)
326df8bae1dSRodney W. Grimes 	register struct mbuf *m;
327f0ffb944SJulian Elischer 	int off0;
328df8bae1dSRodney W. Grimes {
329fb59c426SYoshinobu Inoue 	register struct tcphdr *th;
330fb59c426SYoshinobu Inoue 	register struct ip *ip = NULL;
331fb59c426SYoshinobu Inoue 	register struct ipovly *ipov;
332df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
333e79adb8eSGarrett Wollman 	u_char *optp = NULL;
33426f9a767SRodney W. Grimes 	int optlen = 0;
335df8bae1dSRodney W. Grimes 	int len, tlen, off;
336fb59c426SYoshinobu Inoue 	int drop_hdrlen;
337df8bae1dSRodney W. Grimes 	register struct tcpcb *tp = 0;
338fb59c426SYoshinobu Inoue 	register int thflags;
33926f9a767SRodney W. Grimes 	struct socket *so = 0;
340df8bae1dSRodney W. Grimes 	int todrop, acked, ourfinisacked, needoutput = 0;
341df8bae1dSRodney W. Grimes 	int iss = 0;
342a0292f23SGarrett Wollman 	u_long tiwin;
343a0292f23SGarrett Wollman 	struct tcpopt to;		/* options in this segment */
344a0292f23SGarrett Wollman 	struct rmxp_tao *taop;		/* pointer to our TAO cache entry */
345a0292f23SGarrett Wollman 	struct rmxp_tao	tao_noncached;	/* in case there's no cached entry */
346610ee2f9SDavid Greenman #ifdef TCPDEBUG
347610ee2f9SDavid Greenman 	short ostate = 0;
348610ee2f9SDavid Greenman #endif
349fb59c426SYoshinobu Inoue #ifdef INET6
350fb59c426SYoshinobu Inoue 	struct ip6_hdr *ip6 = NULL;
351fb59c426SYoshinobu Inoue 	int isipv6;
352fb59c426SYoshinobu Inoue #endif /* INET6 */
353a57815efSBosko Milekic 	int rstreason; /* For badport_bandlim accounting purposes */
354df8bae1dSRodney W. Grimes 
355fb59c426SYoshinobu Inoue #ifdef INET6
356fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
357fb59c426SYoshinobu Inoue #endif
358a0292f23SGarrett Wollman 	bzero((char *)&to, sizeof(to));
359a0292f23SGarrett Wollman 
360df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvtotal++;
361fb59c426SYoshinobu Inoue 
362fb59c426SYoshinobu Inoue #ifdef INET6
363fb59c426SYoshinobu Inoue 	if (isipv6) {
364fb59c426SYoshinobu Inoue 		/* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
365fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
366fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
367fb59c426SYoshinobu Inoue 		if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) {
368fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbadsum++;
369fb59c426SYoshinobu Inoue 			goto drop;
370fb59c426SYoshinobu Inoue 		}
371fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
37233841545SHajimu UMEMOTO 
37333841545SHajimu UMEMOTO 		/*
37433841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
37533841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
37633841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
37733841545SHajimu UMEMOTO 		 *
37833841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
37933841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
38033841545SHajimu UMEMOTO 		 */
38133841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
38233841545SHajimu UMEMOTO 			/* XXX stat */
38333841545SHajimu UMEMOTO 			goto drop;
38433841545SHajimu UMEMOTO 		}
385fb59c426SYoshinobu Inoue 	} else
386fb59c426SYoshinobu Inoue #endif /* INET6 */
387fb59c426SYoshinobu Inoue       {
388df8bae1dSRodney W. Grimes 	/*
389df8bae1dSRodney W. Grimes 	 * Get IP and TCP header together in first mbuf.
390df8bae1dSRodney W. Grimes 	 * Note: IP leaves IP header in first mbuf.
391df8bae1dSRodney W. Grimes 	 */
392fb59c426SYoshinobu Inoue 	if (off0 > sizeof (struct ip)) {
393df8bae1dSRodney W. Grimes 		ip_stripoptions(m, (struct mbuf *)0);
394fb59c426SYoshinobu Inoue 		off0 = sizeof(struct ip);
395fb59c426SYoshinobu Inoue 	}
396df8bae1dSRodney W. Grimes 	if (m->m_len < sizeof (struct tcpiphdr)) {
397df8bae1dSRodney W. Grimes 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
398df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvshort++;
399df8bae1dSRodney W. Grimes 			return;
400df8bae1dSRodney W. Grimes 		}
401df8bae1dSRodney W. Grimes 	}
402fb59c426SYoshinobu Inoue 	ip = mtod(m, struct ip *);
403fb59c426SYoshinobu Inoue 	ipov = (struct ipovly *)ip;
404db4f9cc7SJonathan Lemon 	th = (struct tcphdr *)((caddr_t)ip + off0);
405db4f9cc7SJonathan Lemon 	tlen = ip->ip_len;
406df8bae1dSRodney W. Grimes 
407db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
408db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
409db4f9cc7SJonathan Lemon                 	th->th_sum = m->m_pkthdr.csum_data;
410db4f9cc7SJonathan Lemon 		else
411db4f9cc7SJonathan Lemon 	                th->th_sum = in_pseudo(ip->ip_src.s_addr,
412db4f9cc7SJonathan Lemon 			    ip->ip_dst.s_addr, htonl(m->m_pkthdr.csum_data +
413db4f9cc7SJonathan Lemon 			    ip->ip_len + IPPROTO_TCP));
414db4f9cc7SJonathan Lemon 		th->th_sum ^= 0xffff;
415db4f9cc7SJonathan Lemon 	} else {
416df8bae1dSRodney W. Grimes 		/*
417df8bae1dSRodney W. Grimes 		 * Checksum extended TCP header and data.
418df8bae1dSRodney W. Grimes 		 */
419df8bae1dSRodney W. Grimes 		len = sizeof (struct ip) + tlen;
420fb59c426SYoshinobu Inoue 		bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
421fb59c426SYoshinobu Inoue 		ipov->ih_len = (u_short)tlen;
422fb59c426SYoshinobu Inoue 		HTONS(ipov->ih_len);
423fb59c426SYoshinobu Inoue 		th->th_sum = in_cksum(m, len);
424db4f9cc7SJonathan Lemon 	}
425fb59c426SYoshinobu Inoue 	if (th->th_sum) {
426df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvbadsum++;
427df8bae1dSRodney W. Grimes 		goto drop;
428df8bae1dSRodney W. Grimes 	}
429fb59c426SYoshinobu Inoue #ifdef INET6
430fb59c426SYoshinobu Inoue 	/* Re-initialization for later version check */
431fb59c426SYoshinobu Inoue 	ip->ip_v = IPVERSION;
432fb59c426SYoshinobu Inoue #endif
433fb59c426SYoshinobu Inoue       }
434df8bae1dSRodney W. Grimes 
435df8bae1dSRodney W. Grimes 	/*
436df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
437df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
438df8bae1dSRodney W. Grimes 	 */
439fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
440df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
441df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvbadoff++;
442df8bae1dSRodney W. Grimes 		goto drop;
443df8bae1dSRodney W. Grimes 	}
444fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
445df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
446fb59c426SYoshinobu Inoue #ifdef INET6
447fb59c426SYoshinobu Inoue 		if (isipv6) {
448fb59c426SYoshinobu Inoue 			IP6_EXTHDR_CHECK(m, off0, off, );
449fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
450fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
451fb59c426SYoshinobu Inoue 		} else
452fb59c426SYoshinobu Inoue #endif /* INET6 */
453fb59c426SYoshinobu Inoue 	      {
454df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof(struct ip) + off) {
455df8bae1dSRodney W. Grimes 			if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
456df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvshort++;
457df8bae1dSRodney W. Grimes 				return;
458df8bae1dSRodney W. Grimes 			}
459fb59c426SYoshinobu Inoue 			ip = mtod(m, struct ip *);
460fb59c426SYoshinobu Inoue 			ipov = (struct ipovly *)ip;
461fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip + off0);
462fb59c426SYoshinobu Inoue 		}
463df8bae1dSRodney W. Grimes 	      }
464df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
465fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
466df8bae1dSRodney W. Grimes 	}
467fb59c426SYoshinobu Inoue 	thflags = th->th_flags;
468df8bae1dSRodney W. Grimes 
469e46cd3d4SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN
470e46cd3d4SDag-Erling Smørgrav 	/*
471e46cd3d4SDag-Erling Smørgrav 	 * If the drop_synfin option is enabled, drop all packets with
472e46cd3d4SDag-Erling Smørgrav 	 * both the SYN and FIN bits set. This prevents e.g. nmap from
473e46cd3d4SDag-Erling Smørgrav 	 * identifying the TCP/IP stack.
474e46cd3d4SDag-Erling Smørgrav 	 *
475a589a70eSGarrett Wollman 	 * This is a violation of the TCP specification.
476e46cd3d4SDag-Erling Smørgrav 	 */
477fb59c426SYoshinobu Inoue 	if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN))
478e46cd3d4SDag-Erling Smørgrav 		goto drop;
479e46cd3d4SDag-Erling Smørgrav #endif
480e46cd3d4SDag-Erling Smørgrav 
481df8bae1dSRodney W. Grimes 	/*
482df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
483df8bae1dSRodney W. Grimes 	 */
484fb59c426SYoshinobu Inoue 	NTOHL(th->th_seq);
485fb59c426SYoshinobu Inoue 	NTOHL(th->th_ack);
486fb59c426SYoshinobu Inoue 	NTOHS(th->th_win);
487fb59c426SYoshinobu Inoue 	NTOHS(th->th_urp);
488df8bae1dSRodney W. Grimes 
489df8bae1dSRodney W. Grimes 	/*
490fb59c426SYoshinobu Inoue 	 * Delay droping TCP, IP headers, IPv6 ext headers, and TCP options,
491fb59c426SYoshinobu Inoue 	 * until after ip6_savecontrol() is called and before other functions
492fb59c426SYoshinobu Inoue 	 * which don't want those proto headers.
493fb59c426SYoshinobu Inoue 	 * Because ip6_savecontrol() is going to parse the mbuf to
494fb59c426SYoshinobu Inoue 	 * search for data to be passed up to user-land, it wants mbuf
495fb59c426SYoshinobu Inoue 	 * parameters to be unchanged.
496755c1f07SAndras Olah 	 */
497fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
498755c1f07SAndras Olah 
499755c1f07SAndras Olah 	/*
500df8bae1dSRodney W. Grimes 	 * Locate pcb for segment.
501df8bae1dSRodney W. Grimes 	 */
502df8bae1dSRodney W. Grimes findpcb:
503f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD
504fb59c426SYoshinobu Inoue 	if (ip_fw_fwd_addr != NULL
505fb59c426SYoshinobu Inoue #ifdef INET6
506fb59c426SYoshinobu Inoue 	    && isipv6 == NULL /* IPv6 support is not yet */
507fb59c426SYoshinobu Inoue #endif /* INET6 */
508fb59c426SYoshinobu Inoue 	    ) {
509f9e354dfSJulian Elischer 		/*
510f9e354dfSJulian Elischer 		 * Diverted. Pretend to be the destination.
511f9e354dfSJulian Elischer 		 * already got one like this?
512f9e354dfSJulian Elischer 		 */
513fb59c426SYoshinobu Inoue 		inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport,
514fb59c426SYoshinobu Inoue 			ip->ip_dst, th->th_dport, 0, m->m_pkthdr.rcvif);
515f9e354dfSJulian Elischer 		if (!inp) {
516f9e354dfSJulian Elischer 			/*
517f9e354dfSJulian Elischer 			 * No, then it's new. Try find the ambushing socket
518f9e354dfSJulian Elischer 			 */
519f9e354dfSJulian Elischer 			if (!ip_fw_fwd_addr->sin_port) {
520fb59c426SYoshinobu Inoue 				inp = in_pcblookup_hash(&tcbinfo, ip->ip_src,
521fb59c426SYoshinobu Inoue 				    th->th_sport, ip_fw_fwd_addr->sin_addr,
522fb59c426SYoshinobu Inoue 				    th->th_dport, 1, m->m_pkthdr.rcvif);
523f9e354dfSJulian Elischer 			} else {
524f9e354dfSJulian Elischer 				inp = in_pcblookup_hash(&tcbinfo,
525fb59c426SYoshinobu Inoue 				    ip->ip_src, th->th_sport,
526f9e354dfSJulian Elischer 	    			    ip_fw_fwd_addr->sin_addr,
527cfa1ca9dSYoshinobu Inoue 				    ntohs(ip_fw_fwd_addr->sin_port), 1,
528cfa1ca9dSYoshinobu Inoue 				    m->m_pkthdr.rcvif);
529f9e354dfSJulian Elischer 			}
530f9e354dfSJulian Elischer 		}
531f9e354dfSJulian Elischer 		ip_fw_fwd_addr = NULL;
532f9e354dfSJulian Elischer 	} else
533f9e354dfSJulian Elischer #endif	/* IPFIREWALL_FORWARD */
534fb59c426SYoshinobu Inoue       {
535fb59c426SYoshinobu Inoue #ifdef INET6
536fb59c426SYoshinobu Inoue 	if (isipv6)
537fb59c426SYoshinobu Inoue 		inp = in6_pcblookup_hash(&tcbinfo, &ip6->ip6_src, th->th_sport,
538fb59c426SYoshinobu Inoue 					 &ip6->ip6_dst, th->th_dport, 1,
539fb59c426SYoshinobu Inoue 					 m->m_pkthdr.rcvif);
540fb59c426SYoshinobu Inoue 	else
541fb59c426SYoshinobu Inoue #endif /* INET6 */
542fb59c426SYoshinobu Inoue 	inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport,
543fb59c426SYoshinobu Inoue 	    ip->ip_dst, th->th_dport, 1, m->m_pkthdr.rcvif);
544fb59c426SYoshinobu Inoue       }
545f9e354dfSJulian Elischer 
546fb59c426SYoshinobu Inoue #ifdef IPSEC
547fb59c426SYoshinobu Inoue #ifdef INET6
548fb59c426SYoshinobu Inoue 	if (isipv6) {
549fb59c426SYoshinobu Inoue 		if (inp != NULL && ipsec6_in_reject_so(m, inp->inp_socket)) {
550fb59c426SYoshinobu Inoue 			ipsec6stat.in_polvio++;
551fb59c426SYoshinobu Inoue 			goto drop;
552fb59c426SYoshinobu Inoue 		}
553fb59c426SYoshinobu Inoue 	} else
554fb59c426SYoshinobu Inoue #endif /* INET6 */
555fb59c426SYoshinobu Inoue 	if (inp != NULL && ipsec4_in_reject_so(m, inp->inp_socket)) {
556fb59c426SYoshinobu Inoue 		ipsecstat.in_polvio++;
557fb59c426SYoshinobu Inoue 		goto drop;
558fb59c426SYoshinobu Inoue 	}
559fb59c426SYoshinobu Inoue #endif /*IPSEC*/
560df8bae1dSRodney W. Grimes 
561df8bae1dSRodney W. Grimes 	/*
562df8bae1dSRodney W. Grimes 	 * If the state is CLOSED (i.e., TCB does not exist) then
563df8bae1dSRodney W. Grimes 	 * all data in the incoming segment is discarded.
564df8bae1dSRodney W. Grimes 	 * If the TCB exists but is in CLOSED state, it is embryonic,
565df8bae1dSRodney W. Grimes 	 * but should either do a listen or a connect soon.
566df8bae1dSRodney W. Grimes 	 */
567816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
5682e4e1b4cSGeoff Rehmet 		if (log_in_vain) {
569fb59c426SYoshinobu Inoue #ifdef INET6
570fb59c426SYoshinobu Inoue 			char dbuf[INET6_ADDRSTRLEN], sbuf[INET6_ADDRSTRLEN];
571fb59c426SYoshinobu Inoue #else /* INET6 */
572fb59c426SYoshinobu Inoue 			char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"];
573fb59c426SYoshinobu Inoue #endif /* INET6 */
57475cfc95fSAndrey A. Chernov 
575fb59c426SYoshinobu Inoue #ifdef INET6
576fb59c426SYoshinobu Inoue 			if (isipv6) {
577fb59c426SYoshinobu Inoue 				strcpy(dbuf, ip6_sprintf(&ip6->ip6_dst));
578fb59c426SYoshinobu Inoue 				strcpy(sbuf, ip6_sprintf(&ip6->ip6_src));
579fb59c426SYoshinobu Inoue 			} else
580fb59c426SYoshinobu Inoue #endif
581fb59c426SYoshinobu Inoue 		      {
582fb59c426SYoshinobu Inoue 			strcpy(dbuf, inet_ntoa(ip->ip_dst));
583fb59c426SYoshinobu Inoue 			strcpy(sbuf, inet_ntoa(ip->ip_src));
584fb59c426SYoshinobu Inoue 		      }
5852e4e1b4cSGeoff Rehmet 			switch (log_in_vain) {
5862e4e1b4cSGeoff Rehmet 			case 1:
587fb59c426SYoshinobu Inoue 				if(thflags & TH_SYN)
588592071e8SBruce Evans 					log(LOG_INFO,
589592071e8SBruce Evans 			    		"Connection attempt to TCP %s:%d from %s:%d\n",
590fb59c426SYoshinobu Inoue 			    		dbuf, ntohs(th->th_dport),
591fb59c426SYoshinobu Inoue 					sbuf,
592fb59c426SYoshinobu Inoue 					ntohs(th->th_sport));
5932e4e1b4cSGeoff Rehmet 				break;
5942e4e1b4cSGeoff Rehmet 			case 2:
5952e4e1b4cSGeoff Rehmet 				log(LOG_INFO,
5962e4e1b4cSGeoff Rehmet 			    	"Connection attempt to TCP %s:%d from %s:%d flags:0x%x\n",
597fb59c426SYoshinobu Inoue 			    	dbuf, ntohs(th->th_dport), sbuf,
598fb59c426SYoshinobu Inoue 			    	ntohs(th->th_sport), thflags);
5992e4e1b4cSGeoff Rehmet 				break;
6002e4e1b4cSGeoff Rehmet 			default:
6012e4e1b4cSGeoff Rehmet 				break;
6022e4e1b4cSGeoff Rehmet 			}
60375cfc95fSAndrey A. Chernov 		}
6042e4e1b4cSGeoff Rehmet 		if (blackhole) {
6052e4e1b4cSGeoff Rehmet 			switch (blackhole) {
606828b7f40SGeoff Rehmet 			case 1:
607fb59c426SYoshinobu Inoue 				if (thflags & TH_SYN)
608828b7f40SGeoff Rehmet 					goto drop;
609828b7f40SGeoff Rehmet 				break;
610828b7f40SGeoff Rehmet 			case 2:
611828b7f40SGeoff Rehmet 				goto drop;
612828b7f40SGeoff Rehmet 			default:
613828b7f40SGeoff Rehmet 				goto drop;
6142e4e1b4cSGeoff Rehmet 			}
615828b7f40SGeoff Rehmet 		}
616a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
617a57815efSBosko Milekic 		goto dropwithreset;
618816a3d83SPoul-Henning Kamp 	}
619df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
62009f81a46SBosko Milekic 	if (tp == 0) {
621a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
622a57815efSBosko Milekic 		goto dropwithreset;
62309f81a46SBosko Milekic 	}
624df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_CLOSED)
625df8bae1dSRodney W. Grimes 		goto drop;
626df8bae1dSRodney W. Grimes 
627df8bae1dSRodney W. Grimes 	/* Unscale the window into a 32-bit value. */
628fb59c426SYoshinobu Inoue 	if ((thflags & TH_SYN) == 0)
629fb59c426SYoshinobu Inoue 		tiwin = th->th_win << tp->snd_scale;
630df8bae1dSRodney W. Grimes 	else
631fb59c426SYoshinobu Inoue 		tiwin = th->th_win;
632fb59c426SYoshinobu Inoue 
633df8bae1dSRodney W. Grimes 	so = inp->inp_socket;
634df8bae1dSRodney W. Grimes 	if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
635be2ac88cSJonathan Lemon 		struct in_conninfo inc;
636610ee2f9SDavid Greenman #ifdef TCPDEBUG
637df8bae1dSRodney W. Grimes 		if (so->so_options & SO_DEBUG) {
638df8bae1dSRodney W. Grimes 			ostate = tp->t_state;
639fb59c426SYoshinobu Inoue #ifdef INET6
640fb59c426SYoshinobu Inoue 			if (isipv6)
641fb59c426SYoshinobu Inoue 				bcopy((char *)ip6, (char *)tcp_saveipgen,
642fb59c426SYoshinobu Inoue 				      sizeof(*ip6));
643fb59c426SYoshinobu Inoue 			else
644fb59c426SYoshinobu Inoue #endif /* INET6 */
645fb59c426SYoshinobu Inoue 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
646fb59c426SYoshinobu Inoue 			tcp_savetcp = *th;
647df8bae1dSRodney W. Grimes 		}
648610ee2f9SDavid Greenman #endif
649be2ac88cSJonathan Lemon 		/* skip if this isn't a listen socket */
650be2ac88cSJonathan Lemon 		if ((so->so_options & SO_ACCEPTCONN) == 0)
651be2ac88cSJonathan Lemon 			goto after_listen;
652fb59c426SYoshinobu Inoue #ifdef INET6
653be2ac88cSJonathan Lemon 		inc.inc_isipv6 = isipv6;
654be2ac88cSJonathan Lemon 		if (isipv6) {
655be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
656be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
657be2ac88cSJonathan Lemon 			inc.inc6_route.ro_rt = NULL;		/* XXX */
658fb59c426SYoshinobu Inoue 
659be2ac88cSJonathan Lemon 		} else
660be2ac88cSJonathan Lemon #endif /* INET6 */
661be2ac88cSJonathan Lemon 		{
662be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
663be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
664be2ac88cSJonathan Lemon 			inc.inc_route.ro_rt = NULL;		/* XXX */
665be2ac88cSJonathan Lemon 		}
666be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
667be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
668be2ac88cSJonathan Lemon 
669fb59c426SYoshinobu Inoue 	        /*
670be2ac88cSJonathan Lemon 	         * If the state is LISTEN then ignore segment if it contains
671be2ac88cSJonathan Lemon 		 * a RST.  If the segment contains an ACK then it is bad and
672be2ac88cSJonathan Lemon 		 * send a RST.  If it does not contain a SYN then it is not
673be2ac88cSJonathan Lemon 		 * interesting; drop it.
674be2ac88cSJonathan Lemon 		 *
675be2ac88cSJonathan Lemon 		 * If the state is SYN_RECEIVED (syncache) and seg contains
676be2ac88cSJonathan Lemon 		 * an ACK, but not for our SYN/ACK, send a RST.  If the seg
677be2ac88cSJonathan Lemon 		 * contains a RST, check the sequence number to see if it
678be2ac88cSJonathan Lemon 		 * is a valid reset segment.
679fb59c426SYoshinobu Inoue 		 */
680fb59c426SYoshinobu Inoue 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
681be2ac88cSJonathan Lemon 			if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
682be2ac88cSJonathan Lemon 				if (!syncache_expand(&inc, th, &so, m)) {
6834195b4afSPaul Traina 					/*
684be2ac88cSJonathan Lemon 					 * No syncache entry, or ACK was not
685be2ac88cSJonathan Lemon 					 * for our SYN/ACK.  Send a RST.
6864195b4afSPaul Traina 					 */
687be2ac88cSJonathan Lemon 					tcpstat.tcps_badsyn++;
688be2ac88cSJonathan Lemon 					rstreason = BANDLIM_RST_OPENPORT;
689be2ac88cSJonathan Lemon 					goto dropwithreset;
690be2ac88cSJonathan Lemon 				}
691be2ac88cSJonathan Lemon 				if (so == NULL)
692be2ac88cSJonathan Lemon 					/*
693be2ac88cSJonathan Lemon 					 * Could not complete 3-way handshake,
694be2ac88cSJonathan Lemon 					 * connection is being closed down, and
695be2ac88cSJonathan Lemon 					 * syncache will free mbuf.
696be2ac88cSJonathan Lemon 					 */
697be2ac88cSJonathan Lemon 					return;
698be2ac88cSJonathan Lemon 				/*
699be2ac88cSJonathan Lemon 				 * Socket is created in state SYN_RECEIVED.
700be2ac88cSJonathan Lemon 				 * Continue processing segment.
701be2ac88cSJonathan Lemon 				 */
702be2ac88cSJonathan Lemon 				inp = sotoinpcb(so);
703be2ac88cSJonathan Lemon 				tp = intotcpcb(inp);
704be2ac88cSJonathan Lemon 				/*
705be2ac88cSJonathan Lemon 				 * This is what would have happened in
706be2ac88cSJonathan Lemon 				 * tcp_ouput() when the SYN,ACK was sent.
707be2ac88cSJonathan Lemon 				 */
708be2ac88cSJonathan Lemon 				tp->snd_up = tp->snd_una;
709be2ac88cSJonathan Lemon 				tp->snd_max = tp->snd_nxt = tp->iss + 1;
710be2ac88cSJonathan Lemon 				tp->last_ack_sent = tp->rcv_nxt;
711be2ac88cSJonathan Lemon /*
712be2ac88cSJonathan Lemon  * XXX possible bug - it doesn't appear that tp->snd_wnd is unscaled
713be2ac88cSJonathan Lemon  * until the _second_ ACK is received:
714be2ac88cSJonathan Lemon  *    rcv SYN (set wscale opts)	 --> send SYN/ACK, set snd_wnd = window.
715be2ac88cSJonathan Lemon  *    rcv ACK, calculate tiwin --> process SYN_RECEIVED, determine wscale,
716be2ac88cSJonathan Lemon  *        move to ESTAB, set snd_wnd to tiwin.
717be2ac88cSJonathan Lemon  */
718be2ac88cSJonathan Lemon 				tp->snd_wnd = tiwin;	/* unscaled */
719be2ac88cSJonathan Lemon 				goto after_listen;
720be2ac88cSJonathan Lemon 			}
721be2ac88cSJonathan Lemon 			if (thflags & TH_RST) {
722be2ac88cSJonathan Lemon 				syncache_chkrst(&inc, th);
723be2ac88cSJonathan Lemon 				goto drop;
724be2ac88cSJonathan Lemon 			}
725fb59c426SYoshinobu Inoue 			if (thflags & TH_ACK) {
726be2ac88cSJonathan Lemon 				syncache_badack(&inc);
727ebb0cbeaSPaul Traina 				tcpstat.tcps_badsyn++;
728a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
729a57815efSBosko Milekic 				goto dropwithreset;
7304195b4afSPaul Traina 			}
731ebb0cbeaSPaul Traina 			goto drop;
732ebb0cbeaSPaul Traina 		}
73333841545SHajimu UMEMOTO 
734be2ac88cSJonathan Lemon 		/*
735be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
736be2ac88cSJonathan Lemon 		 */
73733841545SHajimu UMEMOTO #ifdef INET6
73833841545SHajimu UMEMOTO 		/*
73933841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
74033841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
74133841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
74233841545SHajimu UMEMOTO 		 * getting established.
74333841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
74433841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
74533841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
74633841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
74733841545SHajimu UMEMOTO 		 * for the exchange.
74833841545SHajimu UMEMOTO 		 *
74933841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
75033841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
75133841545SHajimu UMEMOTO 		 * SYN in this case.
75233841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
75333841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
75433841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
75533841545SHajimu UMEMOTO 		 *    used"
75633841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
75733841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
75833841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
75933841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
76033841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
76133841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
76233841545SHajimu UMEMOTO 		 *
76333841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
76433841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
76533841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
76633841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
76733841545SHajimu UMEMOTO 		 */
76833841545SHajimu UMEMOTO 		if (isipv6 && !ip6_use_deprecated) {
76933841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
77033841545SHajimu UMEMOTO 
77133841545SHajimu UMEMOTO 			if ((ia6 = ip6_getdstifaddr(m)) &&
77233841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
77333841545SHajimu UMEMOTO 				tp = NULL;
77433841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
77533841545SHajimu UMEMOTO 				goto dropwithreset;
77633841545SHajimu UMEMOTO 			}
77733841545SHajimu UMEMOTO 		}
77833841545SHajimu UMEMOTO #endif
77965f28919SJesper Skriver 		/*
780be2ac88cSJonathan Lemon 		 * If it is from this socket, drop it, it must be forged.
781be2ac88cSJonathan Lemon 		 * Don't bother responding if the destination was a broadcast.
78265f28919SJesper Skriver 		 */
783be2ac88cSJonathan Lemon 		if (th->th_dport == th->th_sport) {
784fb59c426SYoshinobu Inoue #ifdef INET6
785fb59c426SYoshinobu Inoue 			if (isipv6) {
786be2ac88cSJonathan Lemon 				if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
787be2ac88cSJonathan Lemon 						       &ip6->ip6_src))
788be2ac88cSJonathan Lemon 					goto drop;
789fb59c426SYoshinobu Inoue 			} else
790fb59c426SYoshinobu Inoue #endif /* INET6 */
791be2ac88cSJonathan Lemon 			if (ip->ip_dst.s_addr == ip->ip_src.s_addr)
792be2ac88cSJonathan Lemon 				goto drop;
793be2ac88cSJonathan Lemon 		}
794be2ac88cSJonathan Lemon 		/*
795be2ac88cSJonathan Lemon 		 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
796be2ac88cSJonathan Lemon 		 * in_broadcast() should never return true on a received
797be2ac88cSJonathan Lemon 		 * packet with M_BCAST not set.
798be2ac88cSJonathan Lemon  		 *
799be2ac88cSJonathan Lemon  		 * Packets with a multicast source address should also
800be2ac88cSJonathan Lemon  		 * be discarded.
801be2ac88cSJonathan Lemon 		 */
802be2ac88cSJonathan Lemon 		if (m->m_flags & (M_BCAST|M_MCAST))
803be2ac88cSJonathan Lemon 			goto drop;
804be2ac88cSJonathan Lemon #ifdef INET6
805be2ac88cSJonathan Lemon 		if (isipv6) {
806be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
807be2ac88cSJonathan Lemon 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
808be2ac88cSJonathan Lemon 				goto drop;
809be2ac88cSJonathan Lemon 		} else
810fb59c426SYoshinobu Inoue #endif
811be2ac88cSJonathan Lemon 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
812be2ac88cSJonathan Lemon 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
813be2ac88cSJonathan Lemon 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST))
814be2ac88cSJonathan Lemon 			goto drop;
815be2ac88cSJonathan Lemon 		/*
816be2ac88cSJonathan Lemon 		 * SYN appears to be valid; create compressed TCP state
817be2ac88cSJonathan Lemon 		 * for syncache, or perform t/tcp connection.
818be2ac88cSJonathan Lemon 		 */
819be2ac88cSJonathan Lemon 		if (so->so_qlen <= so->so_qlimit) {
820be2ac88cSJonathan Lemon 			tcp_dooptions(&to, optp, optlen, 1);
821be2ac88cSJonathan Lemon 			if (!syncache_add(&inc, &to, th, &so, m))
822be2ac88cSJonathan Lemon 				goto drop;
823be2ac88cSJonathan Lemon 			if (so == NULL)
824be2ac88cSJonathan Lemon 				/*
825be2ac88cSJonathan Lemon 				 * Entry added to syncache, mbuf used to
826be2ac88cSJonathan Lemon 				 * send SYN,ACK packet.
827be2ac88cSJonathan Lemon 				 */
828be2ac88cSJonathan Lemon 				return;
829be2ac88cSJonathan Lemon 			/*
830be2ac88cSJonathan Lemon 			 * Segment passed TAO tests.
831be2ac88cSJonathan Lemon 			 */
832be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
833df8bae1dSRodney W. Grimes 			tp = intotcpcb(inp);
834be2ac88cSJonathan Lemon 			tp->snd_wnd = tiwin;
835be2ac88cSJonathan Lemon 			tp->t_starttime = ticks;
836be2ac88cSJonathan Lemon 			tp->t_state = TCPS_ESTABLISHED;
837df8bae1dSRodney W. Grimes 
838be2ac88cSJonathan Lemon 			/*
839be2ac88cSJonathan Lemon 			 * If there is a FIN, or if there is data and the
840be2ac88cSJonathan Lemon 			 * connection is local, then delay SYN,ACK(SYN) in
841be2ac88cSJonathan Lemon 			 * the hope of piggy-backing it on a response
842be2ac88cSJonathan Lemon 			 * segment.  Otherwise must send ACK now in case
843be2ac88cSJonathan Lemon 			 * the other side is slow starting.
844be2ac88cSJonathan Lemon 			 */
845be2ac88cSJonathan Lemon 			if (DELAY_ACK(tp) && ((thflags & TH_FIN) ||
846be2ac88cSJonathan Lemon 			    (tlen != 0 &&
847be2ac88cSJonathan Lemon #ifdef INET6
848be2ac88cSJonathan Lemon 			      ((isipv6 && in6_localaddr(&inp->in6p_faddr))
849be2ac88cSJonathan Lemon 			      ||
850be2ac88cSJonathan Lemon 			      (!isipv6 &&
851be2ac88cSJonathan Lemon #endif
852be2ac88cSJonathan Lemon 			    in_localaddr(inp->inp_faddr)
853be2ac88cSJonathan Lemon #ifdef INET6
854be2ac88cSJonathan Lemon 			       ))
855be2ac88cSJonathan Lemon #endif
856be2ac88cSJonathan Lemon 			     ))) {
857be2ac88cSJonathan Lemon                                 callout_reset(tp->tt_delack, tcp_delacktime,
858be2ac88cSJonathan Lemon                                     tcp_timer_delack, tp);
859be2ac88cSJonathan Lemon 				tp->t_flags |= TF_NEEDSYN;
860be2ac88cSJonathan Lemon 			} else
861be2ac88cSJonathan Lemon 				tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
862be2ac88cSJonathan Lemon 
863be2ac88cSJonathan Lemon 			tcpstat.tcps_connects++;
864be2ac88cSJonathan Lemon 			soisconnected(so);
865be2ac88cSJonathan Lemon 			goto trimthenstep6;
866df8bae1dSRodney W. Grimes 		}
867be2ac88cSJonathan Lemon 		goto drop;
868df8bae1dSRodney W. Grimes 	}
869be2ac88cSJonathan Lemon after_listen:
870be2ac88cSJonathan Lemon 
871be2ac88cSJonathan Lemon /* XXX temp debugging */
872be2ac88cSJonathan Lemon 	/* should not happen - syncache should pick up these connections */
873be2ac88cSJonathan Lemon 	if (tp->t_state == TCPS_LISTEN)
874be2ac88cSJonathan Lemon 		panic("tcp_input: TCPS_LISTEN");
875df8bae1dSRodney W. Grimes 
876df8bae1dSRodney W. Grimes 	/*
877df8bae1dSRodney W. Grimes 	 * Segment received on connection.
878df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
879df8bae1dSRodney W. Grimes 	 */
8809b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
8817ff19458SPaul Traina 	if (TCPS_HAVEESTABLISHED(tp->t_state))
8829b8b58e0SJonathan Lemon 		callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
883df8bae1dSRodney W. Grimes 
884df8bae1dSRodney W. Grimes 	/*
885be2ac88cSJonathan Lemon 	 * Process options.
886be2ac88cSJonathan Lemon 	 * XXX this is tradtitional behavior, may need to be cleaned up.
887df8bae1dSRodney W. Grimes 	 */
888be2ac88cSJonathan Lemon 	tcp_dooptions(&to, optp, optlen, thflags & TH_SYN);
889be2ac88cSJonathan Lemon 	if (thflags & TH_SYN) {
890be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_SCALE) {
891be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
892be2ac88cSJonathan Lemon 			tp->requested_s_scale = to.to_requested_s_scale;
893be2ac88cSJonathan Lemon 		}
894be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_TS) {
895be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
896be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
897be2ac88cSJonathan Lemon 			tp->ts_recent_age = ticks;
898be2ac88cSJonathan Lemon 		}
899be2ac88cSJonathan Lemon 		if (to.to_flags & (TOF_CC|TOF_CCNEW))
900be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_CC;
901be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_MSS)
902be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
903be2ac88cSJonathan Lemon 	}
904df8bae1dSRodney W. Grimes 
905df8bae1dSRodney W. Grimes 	/*
906df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
907df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
908df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
909df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
910df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
911df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
912df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
913df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
914df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
915df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
916df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
917df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
918a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
919a0292f23SGarrett Wollman 	 * Since we check for TCPS_ESTABLISHED above, it can only
920a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
921df8bae1dSRodney W. Grimes 	 */
922df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
923fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
924a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
925be2ac88cSJonathan Lemon 	    ((to.to_flags & TOF_TS) == 0 ||
926a0292f23SGarrett Wollman 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
927a0292f23SGarrett Wollman 	    /*
928a0292f23SGarrett Wollman 	     * Using the CC option is compulsory if once started:
929a0292f23SGarrett Wollman 	     *   the segment is OK if no T/TCP was negotiated or
930a0292f23SGarrett Wollman 	     *   if the segment has a CC option equal to CCrecv
931a0292f23SGarrett Wollman 	     */
932a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) ||
933be2ac88cSJonathan Lemon 	     ((to.to_flags & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) &&
934fb59c426SYoshinobu Inoue 	    th->th_seq == tp->rcv_nxt &&
935df8bae1dSRodney W. Grimes 	    tiwin && tiwin == tp->snd_wnd &&
936df8bae1dSRodney W. Grimes 	    tp->snd_nxt == tp->snd_max) {
937df8bae1dSRodney W. Grimes 
938df8bae1dSRodney W. Grimes 		/*
939df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
940df8bae1dSRodney W. Grimes 		 * record the timestamp.
941a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
942a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
943df8bae1dSRodney W. Grimes 		 */
944be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
945fb59c426SYoshinobu Inoue 		   SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
9469b8b58e0SJonathan Lemon 			tp->ts_recent_age = ticks;
947a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
948df8bae1dSRodney W. Grimes 		}
949df8bae1dSRodney W. Grimes 
950fb59c426SYoshinobu Inoue 		if (tlen == 0) {
951fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
952fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
953233e8c18SGarrett Wollman 			    tp->snd_cwnd >= tp->snd_wnd &&
954233e8c18SGarrett Wollman 			    tp->t_dupacks < tcprexmtthresh) {
955df8bae1dSRodney W. Grimes 				/*
956df8bae1dSRodney W. Grimes 				 * this is a pure ack for outstanding data.
957df8bae1dSRodney W. Grimes 				 */
958df8bae1dSRodney W. Grimes 				++tcpstat.tcps_predack;
9599b8b58e0SJonathan Lemon 				/*
9609b8b58e0SJonathan Lemon 				 * "bad retransmit" recovery
9619b8b58e0SJonathan Lemon 				 */
9629b8b58e0SJonathan Lemon 				if (tp->t_rxtshift == 1 &&
9639b8b58e0SJonathan Lemon 				    ticks < tp->t_badrxtwin) {
9649b8b58e0SJonathan Lemon 					tp->snd_cwnd = tp->snd_cwnd_prev;
9659b8b58e0SJonathan Lemon 					tp->snd_ssthresh =
9669b8b58e0SJonathan Lemon 					    tp->snd_ssthresh_prev;
9679b8b58e0SJonathan Lemon 					tp->snd_nxt = tp->snd_max;
9689b8b58e0SJonathan Lemon 					tp->t_badrxtwin = 0;
9699b8b58e0SJonathan Lemon 				}
970be2ac88cSJonathan Lemon 				if ((to.to_flags & TOF_TS) != 0)
971a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
9729b8b58e0SJonathan Lemon 					    ticks - to.to_tsecr + 1);
9739b8b58e0SJonathan Lemon 				else if (tp->t_rtttime &&
974fb59c426SYoshinobu Inoue 					    SEQ_GT(th->th_ack, tp->t_rtseq))
9759b8b58e0SJonathan Lemon 					tcp_xmit_timer(tp, ticks - tp->t_rtttime);
976fb59c426SYoshinobu Inoue 				acked = th->th_ack - tp->snd_una;
977df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackpack++;
978df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackbyte += acked;
979df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
980fb59c426SYoshinobu Inoue 				tp->snd_una = th->th_ack;
981df8bae1dSRodney W. Grimes 				m_freem(m);
982fb59c426SYoshinobu Inoue 				ND6_HINT(tp); /* some progress has been done */
983df8bae1dSRodney W. Grimes 
984df8bae1dSRodney W. Grimes 				/*
985df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
986df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
987df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
988df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
989df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
990df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
991df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
992df8bae1dSRodney W. Grimes 				 */
993df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
9949b8b58e0SJonathan Lemon 					callout_stop(tp->tt_rexmt);
9959b8b58e0SJonathan Lemon 				else if (!callout_active(tp->tt_persist))
9969b8b58e0SJonathan Lemon 					callout_reset(tp->tt_rexmt,
9979b8b58e0SJonathan Lemon 						      tp->t_rxtcur,
9989b8b58e0SJonathan Lemon 						      tcp_timer_rexmt, tp);
999df8bae1dSRodney W. Grimes 
1000df8bae1dSRodney W. Grimes 				sowwakeup(so);
1001df8bae1dSRodney W. Grimes 				if (so->so_snd.sb_cc)
1002df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1003df8bae1dSRodney W. Grimes 				return;
1004df8bae1dSRodney W. Grimes 			}
1005fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1006fb59c426SYoshinobu Inoue 		    LIST_EMPTY(&tp->t_segq) &&
1007fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
1008df8bae1dSRodney W. Grimes 			/*
1009df8bae1dSRodney W. Grimes 			 * this is a pure, in-sequence data packet
1010df8bae1dSRodney W. Grimes 			 * with nothing on the reassembly queue and
1011df8bae1dSRodney W. Grimes 			 * we have enough buffer space to take it.
1012df8bae1dSRodney W. Grimes 			 */
1013df8bae1dSRodney W. Grimes 			++tcpstat.tcps_preddat;
1014fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
1015df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpack++;
1016fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbyte += tlen;
1017fb59c426SYoshinobu Inoue 			ND6_HINT(tp);	/* some progress has been done */
1018df8bae1dSRodney W. Grimes 			/*
1019755c1f07SAndras Olah 			 * Add data to socket buffer.
1020df8bae1dSRodney W. Grimes 			 */
1021fb59c426SYoshinobu Inoue 			m_adj(m, drop_hdrlen);	/* delayed header drop */
1022df8bae1dSRodney W. Grimes 			sbappend(&so->so_rcv, m);
1023df8bae1dSRodney W. Grimes 			sorwakeup(so);
1024d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp)) {
10259b8b58e0SJonathan Lemon 	                        callout_reset(tp->tt_delack, tcp_delacktime,
10269b8b58e0SJonathan Lemon 	                            tcp_timer_delack, tp);
1027f498eeeeSDavid Greenman 			} else {
1028e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
1029e612a582SDavid Greenman 				tcp_output(tp);
1030e612a582SDavid Greenman 			}
1031df8bae1dSRodney W. Grimes 			return;
1032df8bae1dSRodney W. Grimes 		}
1033df8bae1dSRodney W. Grimes 	}
1034df8bae1dSRodney W. Grimes 
1035df8bae1dSRodney W. Grimes 	/*
1036df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1037df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1038df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1039df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1040df8bae1dSRodney W. Grimes 	 */
1041df8bae1dSRodney W. Grimes 	{ int win;
1042df8bae1dSRodney W. Grimes 
1043df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1044df8bae1dSRodney W. Grimes 	if (win < 0)
1045df8bae1dSRodney W. Grimes 		win = 0;
104666e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1047df8bae1dSRodney W. Grimes 	}
1048df8bae1dSRodney W. Grimes 
1049df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1050df8bae1dSRodney W. Grimes 
1051df8bae1dSRodney W. Grimes 	/*
1052764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1053764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1054764d8cefSBill Fenner 	 */
1055764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1056fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1057fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1058a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1059a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1060a57815efSBosko Milekic 				goto dropwithreset;
1061a57815efSBosko Milekic 		}
1062764d8cefSBill Fenner 		break;
1063764d8cefSBill Fenner 
1064764d8cefSBill Fenner 	/*
1065df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
1066df8bae1dSRodney W. Grimes 	 *	if seg contains an ACK, but not for our SYN, drop the input.
1067df8bae1dSRodney W. Grimes 	 *	if seg contains a RST, then drop the connection.
1068df8bae1dSRodney W. Grimes 	 *	if seg does not contain SYN, then drop it.
1069df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
1070df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
1071df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
1072df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1073df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
1074df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
1075df8bae1dSRodney W. Grimes 	 */
1076df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
1077be2ac88cSJonathan Lemon 		if ((taop = tcp_gettaocache(&inp->inp_inc)) == NULL) {
1078a0292f23SGarrett Wollman 			taop = &tao_noncached;
1079a0292f23SGarrett Wollman 			bzero(taop, sizeof(*taop));
1080a0292f23SGarrett Wollman 		}
1081a0292f23SGarrett Wollman 
1082fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1083fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
1084fb59c426SYoshinobu Inoue 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1085a0292f23SGarrett Wollman 			/*
1086a0292f23SGarrett Wollman 			 * If we have a cached CCsent for the remote host,
1087a0292f23SGarrett Wollman 			 * hence we haven't just crashed and restarted,
1088a0292f23SGarrett Wollman 			 * do not send a RST.  This may be a retransmission
1089a0292f23SGarrett Wollman 			 * from the other side after our earlier ACK was lost.
1090a0292f23SGarrett Wollman 			 * Our new SYN, when it arrives, will serve as the
1091a0292f23SGarrett Wollman 			 * needed ACK.
1092a0292f23SGarrett Wollman 			 */
1093a0292f23SGarrett Wollman 			if (taop->tao_ccsent != 0)
1094a0292f23SGarrett Wollman 				goto drop;
1095a57815efSBosko Milekic 			else {
1096a57815efSBosko Milekic 				rstreason = BANDLIM_UNLIMITED;
1097a0292f23SGarrett Wollman 				goto dropwithreset;
1098a0292f23SGarrett Wollman 			}
1099a57815efSBosko Milekic 		}
1100fb59c426SYoshinobu Inoue 		if (thflags & TH_RST) {
1101fb59c426SYoshinobu Inoue 			if (thflags & TH_ACK)
1102df8bae1dSRodney W. Grimes 				tp = tcp_drop(tp, ECONNREFUSED);
1103df8bae1dSRodney W. Grimes 			goto drop;
1104df8bae1dSRodney W. Grimes 		}
1105fb59c426SYoshinobu Inoue 		if ((thflags & TH_SYN) == 0)
1106df8bae1dSRodney W. Grimes 			goto drop;
1107fb59c426SYoshinobu Inoue 		tp->snd_wnd = th->th_win;	/* initial send window */
1108a0292f23SGarrett Wollman 		tp->cc_recv = to.to_cc;		/* foreign CC */
1109a0292f23SGarrett Wollman 
1110fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
1111df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
1112fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1113a0292f23SGarrett Wollman 			/*
1114a0292f23SGarrett Wollman 			 * Our SYN was acked.  If segment contains CC.ECHO
1115a0292f23SGarrett Wollman 			 * option, check it to make sure this segment really
1116a0292f23SGarrett Wollman 			 * matches our SYN.  If not, just drop it as old
1117a0292f23SGarrett Wollman 			 * duplicate, but send an RST if we're still playing
1118026650e5SBill Fenner 			 * by the old rules.  If no CC.ECHO option, make sure
1119026650e5SBill Fenner 			 * we don't get fooled into using T/TCP.
1120a0292f23SGarrett Wollman 			 */
1121be2ac88cSJonathan Lemon 			if (to.to_flags & TOF_CCECHO) {
1122dfd5dee1SPeter Wemm 				if (tp->cc_send != to.to_ccecho) {
1123a0292f23SGarrett Wollman 					if (taop->tao_ccsent != 0)
1124a0292f23SGarrett Wollman 						goto drop;
1125a57815efSBosko Milekic 					else {
1126a57815efSBosko Milekic 						rstreason = BANDLIM_UNLIMITED;
1127a0292f23SGarrett Wollman 						goto dropwithreset;
1128dfd5dee1SPeter Wemm 					}
1129a57815efSBosko Milekic 				}
1130026650e5SBill Fenner 			} else
1131026650e5SBill Fenner 				tp->t_flags &= ~TF_RCVD_CC;
1132845799c1SAndras Olah 			tcpstat.tcps_connects++;
1133845799c1SAndras Olah 			soisconnected(so);
1134845799c1SAndras Olah 			/* Do window scaling on this connection? */
1135845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1136845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1137845799c1SAndras Olah 				tp->snd_scale = tp->requested_s_scale;
1138845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
1139845799c1SAndras Olah 			}
1140a0292f23SGarrett Wollman 			/* Segment is acceptable, update cache if undefined. */
1141a0292f23SGarrett Wollman 			if (taop->tao_ccsent == 0)
1142a0292f23SGarrett Wollman 				taop->tao_ccsent = to.to_ccecho;
1143a0292f23SGarrett Wollman 
1144a0292f23SGarrett Wollman 			tp->rcv_adv += tp->rcv_wnd;
1145a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
1146a0292f23SGarrett Wollman 			/*
1147a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
1148a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
1149a0292f23SGarrett Wollman 			 */
1150d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp) && tlen != 0)
11519b8b58e0SJonathan Lemon                                 callout_reset(tp->tt_delack, tcp_delacktime,
11529b8b58e0SJonathan Lemon                                     tcp_timer_delack, tp);
1153a0292f23SGarrett Wollman 			else
1154a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
1155a0292f23SGarrett Wollman 			/*
1156a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
1157a0292f23SGarrett Wollman 			 * Transitions:
1158a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
1159a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
1160a0292f23SGarrett Wollman 			 */
11619b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
1162a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
1163a0292f23SGarrett Wollman 				tp->t_state = TCPS_FIN_WAIT_1;
1164a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
1165fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
11667ff19458SPaul Traina 			} else {
1167a0292f23SGarrett Wollman 				tp->t_state = TCPS_ESTABLISHED;
11689b8b58e0SJonathan Lemon 				callout_reset(tp->tt_keep, tcp_keepidle,
11699b8b58e0SJonathan Lemon 					      tcp_timer_keep, tp);
11707ff19458SPaul Traina 			}
1171a0292f23SGarrett Wollman 		} else {
1172a0292f23SGarrett Wollman 		/*
1173a0292f23SGarrett Wollman 		 *  Received initial SYN in SYN-SENT[*] state => simul-
1174a0292f23SGarrett Wollman 		 *  taneous open.  If segment contains CC option and there is
1175a0292f23SGarrett Wollman 		 *  a cached CC, apply TAO test; if it succeeds, connection is
1176a0292f23SGarrett Wollman 		 *  half-synchronized.  Otherwise, do 3-way handshake:
1177a0292f23SGarrett Wollman 		 *        SYN-SENT -> SYN-RECEIVED
1178a0292f23SGarrett Wollman 		 *        SYN-SENT* -> SYN-RECEIVED*
1179a0292f23SGarrett Wollman 		 *  If there was no CC option, clear cached CC value.
1180a0292f23SGarrett Wollman 		 */
1181a0292f23SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
11829b8b58e0SJonathan Lemon 			callout_stop(tp->tt_rexmt);
1183be2ac88cSJonathan Lemon 			if (to.to_flags & TOF_CC) {
1184a0292f23SGarrett Wollman 				if (taop->tao_cc != 0 &&
1185a0292f23SGarrett Wollman 				    CC_GT(to.to_cc, taop->tao_cc)) {
1186a0292f23SGarrett Wollman 					/*
1187a0292f23SGarrett Wollman 					 * update cache and make transition:
1188a0292f23SGarrett Wollman 					 *        SYN-SENT -> ESTABLISHED*
1189a0292f23SGarrett Wollman 					 *        SYN-SENT* -> FIN-WAIT-1*
1190a0292f23SGarrett Wollman 					 */
1191a0292f23SGarrett Wollman 					taop->tao_cc = to.to_cc;
11929b8b58e0SJonathan Lemon 					tp->t_starttime = ticks;
1193a0292f23SGarrett Wollman 					if (tp->t_flags & TF_NEEDFIN) {
1194a0292f23SGarrett Wollman 						tp->t_state = TCPS_FIN_WAIT_1;
1195a0292f23SGarrett Wollman 						tp->t_flags &= ~TF_NEEDFIN;
11967ff19458SPaul Traina 					} else {
1197a0292f23SGarrett Wollman 						tp->t_state = TCPS_ESTABLISHED;
11989b8b58e0SJonathan Lemon 						callout_reset(tp->tt_keep,
11999b8b58e0SJonathan Lemon 							      tcp_keepidle,
12009b8b58e0SJonathan Lemon 							      tcp_timer_keep,
12019b8b58e0SJonathan Lemon 							      tp);
12027ff19458SPaul Traina 					}
1203a0292f23SGarrett Wollman 					tp->t_flags |= TF_NEEDSYN;
1204df8bae1dSRodney W. Grimes 				} else
1205df8bae1dSRodney W. Grimes 					tp->t_state = TCPS_SYN_RECEIVED;
1206a0292f23SGarrett Wollman 			} else {
1207a0292f23SGarrett Wollman 				/* CC.NEW or no option => invalidate cache */
1208a0292f23SGarrett Wollman 				taop->tao_cc = 0;
1209a0292f23SGarrett Wollman 				tp->t_state = TCPS_SYN_RECEIVED;
1210a0292f23SGarrett Wollman 			}
1211a0292f23SGarrett Wollman 		}
1212df8bae1dSRodney W. Grimes 
1213df8bae1dSRodney W. Grimes trimthenstep6:
1214df8bae1dSRodney W. Grimes 		/*
1215fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
1216df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
1217df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
1218df8bae1dSRodney W. Grimes 		 */
1219fb59c426SYoshinobu Inoue 		th->th_seq++;
1220fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
1221fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
1222df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
1223fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
1224fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
1225df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpackafterwin++;
1226df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
1227df8bae1dSRodney W. Grimes 		}
1228fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
1229fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
1230a0292f23SGarrett Wollman 		/*
1231a0292f23SGarrett Wollman 		 *  Client side of transaction: already sent SYN and data.
1232a0292f23SGarrett Wollman 		 *  If the remote host used T/TCP to validate the SYN,
1233a0292f23SGarrett Wollman 		 *  our data will be ACK'd; if so, enter normal data segment
1234a0292f23SGarrett Wollman 		 *  processing in the middle of step 5, ack processing.
1235a0292f23SGarrett Wollman 		 *  Otherwise, goto step 6.
1236a0292f23SGarrett Wollman 		 */
1237fb59c426SYoshinobu Inoue  		if (thflags & TH_ACK)
1238a0292f23SGarrett Wollman 			goto process_ACK;
1239df8bae1dSRodney W. Grimes 		goto step6;
1240a0292f23SGarrett Wollman 	/*
1241a0292f23SGarrett Wollman 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
1242a0292f23SGarrett Wollman 	 *	if segment contains a SYN and CC [not CC.NEW] option:
1243a0292f23SGarrett Wollman 	 *              if state == TIME_WAIT and connection duration > MSL,
1244a0292f23SGarrett Wollman 	 *                  drop packet and send RST;
1245a0292f23SGarrett Wollman 	 *
1246a0292f23SGarrett Wollman 	 *		if SEG.CC > CCrecv then is new SYN, and can implicitly
1247a0292f23SGarrett Wollman 	 *		    ack the FIN (and data) in retransmission queue.
1248a0292f23SGarrett Wollman 	 *                  Complete close and delete TCPCB.  Then reprocess
1249a0292f23SGarrett Wollman 	 *                  segment, hoping to find new TCPCB in LISTEN state;
1250a0292f23SGarrett Wollman 	 *
1251a0292f23SGarrett Wollman 	 *		else must be old SYN; drop it.
1252a0292f23SGarrett Wollman 	 *      else do normal processing.
1253a0292f23SGarrett Wollman 	 */
1254a0292f23SGarrett Wollman 	case TCPS_LAST_ACK:
1255a0292f23SGarrett Wollman 	case TCPS_CLOSING:
1256a0292f23SGarrett Wollman 	case TCPS_TIME_WAIT:
1257fb59c426SYoshinobu Inoue 		if ((thflags & TH_SYN) &&
1258be2ac88cSJonathan Lemon 		    (to.to_flags & TOF_CC) && tp->cc_recv != 0) {
1259a0292f23SGarrett Wollman 			if (tp->t_state == TCPS_TIME_WAIT &&
1260a57815efSBosko Milekic 					(ticks - tp->t_starttime) > tcp_msl) {
1261a57815efSBosko Milekic 				rstreason = BANDLIM_UNLIMITED;
1262a0292f23SGarrett Wollman 				goto dropwithreset;
1263a57815efSBosko Milekic 			}
1264a0292f23SGarrett Wollman 			if (CC_GT(to.to_cc, tp->cc_recv)) {
1265a0292f23SGarrett Wollman 				tp = tcp_close(tp);
1266a0292f23SGarrett Wollman 				goto findpcb;
1267a0292f23SGarrett Wollman 			}
1268a0292f23SGarrett Wollman 			else
1269a0292f23SGarrett Wollman 				goto drop;
1270a0292f23SGarrett Wollman 		}
1271a0292f23SGarrett Wollman  		break;  /* continue normal processing */
1272df8bae1dSRodney W. Grimes 	}
1273df8bae1dSRodney W. Grimes 
1274df8bae1dSRodney W. Grimes 	/*
1275df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
127680ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
127780ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
127880ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
127980ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
128080ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
128180ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
128280ab7c0eSGarrett Wollman 	 * killing a connection).
128380ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
1284a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
1285df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
1286df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
1287df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
1288df8bae1dSRodney W. Grimes 	 *
128980ab7c0eSGarrett Wollman 	 *
129080ab7c0eSGarrett Wollman 	 * If the RST bit is set, check the sequence number to see
129180ab7c0eSGarrett Wollman 	 * if this is a valid reset segment.
129280ab7c0eSGarrett Wollman 	 * RFC 793 page 37:
129380ab7c0eSGarrett Wollman 	 *   In all states except SYN-SENT, all reset (RST) segments
129480ab7c0eSGarrett Wollman 	 *   are validated by checking their SEQ-fields.  A reset is
129580ab7c0eSGarrett Wollman 	 *   valid if its sequence number is in the window.
129680ab7c0eSGarrett Wollman 	 * Note: this does not take into account delayed ACKs, so
129780ab7c0eSGarrett Wollman 	 *   we should test against last_ack_sent instead of rcv_nxt.
12981a244a61SJonathan Lemon 	 *   The sequence number in the reset segment is normally an
12991a244a61SJonathan Lemon 	 *   echo of our outgoing acknowlegement numbers, but some hosts
13001a244a61SJonathan Lemon 	 *   send a reset with the sequence number at the rightmost edge
13011a244a61SJonathan Lemon 	 *   of our receive window, and we have to handle this case.
130280ab7c0eSGarrett Wollman 	 * If we have multiple segments in flight, the intial reset
130380ab7c0eSGarrett Wollman 	 * segment sequence numbers will be to the left of last_ack_sent,
130480ab7c0eSGarrett Wollman 	 * but they will eventually catch up.
130580ab7c0eSGarrett Wollman 	 * In any case, it never made sense to trim reset segments to
130680ab7c0eSGarrett Wollman 	 * fit the receive window since RFC 1122 says:
130780ab7c0eSGarrett Wollman 	 *   4.2.2.12  RST Segment: RFC-793 Section 3.4
130880ab7c0eSGarrett Wollman 	 *
130980ab7c0eSGarrett Wollman 	 *    A TCP SHOULD allow a received RST segment to include data.
131080ab7c0eSGarrett Wollman 	 *
131180ab7c0eSGarrett Wollman 	 *    DISCUSSION
131280ab7c0eSGarrett Wollman 	 *         It has been suggested that a RST segment could contain
131380ab7c0eSGarrett Wollman 	 *         ASCII text that encoded and explained the cause of the
131480ab7c0eSGarrett Wollman 	 *         RST.  No standard has yet been established for such
131580ab7c0eSGarrett Wollman 	 *         data.
131680ab7c0eSGarrett Wollman 	 *
131780ab7c0eSGarrett Wollman 	 * If the reset segment passes the sequence number test examine
131880ab7c0eSGarrett Wollman 	 * the state:
131980ab7c0eSGarrett Wollman 	 *    SYN_RECEIVED STATE:
132080ab7c0eSGarrett Wollman 	 *	If passive open, return to LISTEN state.
132180ab7c0eSGarrett Wollman 	 *	If active open, inform user that connection was refused.
1322745bab7fSDima Dorfman 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
132380ab7c0eSGarrett Wollman 	 *	Inform user that connection was reset, and close tcb.
1324e9bd3a37SJonathan M. Bresler 	 *    CLOSING, LAST_ACK STATES:
132580ab7c0eSGarrett Wollman 	 *	Close the tcb.
1326e9bd3a37SJonathan M. Bresler 	 *    TIME_WAIT STATE:
132780ab7c0eSGarrett Wollman 	 *	Drop the segment - see Stevens, vol. 2, p. 964 and
132880ab7c0eSGarrett Wollman 	 *      RFC 1337.
132980ab7c0eSGarrett Wollman 	 */
1330fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
1331fb59c426SYoshinobu Inoue 		if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
1332fb59c426SYoshinobu Inoue 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
133380ab7c0eSGarrett Wollman 			switch (tp->t_state) {
133480ab7c0eSGarrett Wollman 
133580ab7c0eSGarrett Wollman 			case TCPS_SYN_RECEIVED:
133680ab7c0eSGarrett Wollman 				so->so_error = ECONNREFUSED;
133780ab7c0eSGarrett Wollman 				goto close;
133880ab7c0eSGarrett Wollman 
133980ab7c0eSGarrett Wollman 			case TCPS_ESTABLISHED:
134080ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_1:
134180ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_2:
134280ab7c0eSGarrett Wollman 			case TCPS_CLOSE_WAIT:
134380ab7c0eSGarrett Wollman 				so->so_error = ECONNRESET;
134480ab7c0eSGarrett Wollman 			close:
134580ab7c0eSGarrett Wollman 				tp->t_state = TCPS_CLOSED;
134680ab7c0eSGarrett Wollman 				tcpstat.tcps_drops++;
134780ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
134880ab7c0eSGarrett Wollman 				break;
134980ab7c0eSGarrett Wollman 
135080ab7c0eSGarrett Wollman 			case TCPS_CLOSING:
135180ab7c0eSGarrett Wollman 			case TCPS_LAST_ACK:
135280ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
135380ab7c0eSGarrett Wollman 				break;
135480ab7c0eSGarrett Wollman 
135580ab7c0eSGarrett Wollman 			case TCPS_TIME_WAIT:
135680ab7c0eSGarrett Wollman 				break;
135780ab7c0eSGarrett Wollman 			}
135880ab7c0eSGarrett Wollman 		}
135980ab7c0eSGarrett Wollman 		goto drop;
136080ab7c0eSGarrett Wollman 	}
136180ab7c0eSGarrett Wollman 
136280ab7c0eSGarrett Wollman 	/*
1363df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
1364df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
1365df8bae1dSRodney W. Grimes 	 */
1366be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
136780ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
1368df8bae1dSRodney W. Grimes 
1369df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
13709b8b58e0SJonathan Lemon 		if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) {
1371df8bae1dSRodney W. Grimes 			/*
1372df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
1373df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
1374df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
1375df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
1376df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
1377df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
1378df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
1379df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
1380df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
1381df8bae1dSRodney W. Grimes 			 */
1382df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
1383df8bae1dSRodney W. Grimes 		} else {
1384df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvduppack++;
1385fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvdupbyte += tlen;
1386df8bae1dSRodney W. Grimes 			tcpstat.tcps_pawsdrop++;
1387df8bae1dSRodney W. Grimes 			goto dropafterack;
1388df8bae1dSRodney W. Grimes 		}
1389df8bae1dSRodney W. Grimes 	}
1390df8bae1dSRodney W. Grimes 
1391a0292f23SGarrett Wollman 	/*
1392a0292f23SGarrett Wollman 	 * T/TCP mechanism
1393a0292f23SGarrett Wollman 	 *   If T/TCP was negotiated and the segment doesn't have CC,
1394dc733423SDag-Erling Smørgrav 	 *   or if its CC is wrong then drop the segment.
1395a0292f23SGarrett Wollman 	 *   RST segments do not have to comply with this.
1396a0292f23SGarrett Wollman 	 */
1397a0292f23SGarrett Wollman 	if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) &&
1398be2ac88cSJonathan Lemon 	    ((to.to_flags & TOF_CC) == 0 || tp->cc_recv != to.to_cc))
1399a0292f23SGarrett Wollman  		goto dropafterack;
1400a0292f23SGarrett Wollman 
140180ab7c0eSGarrett Wollman 	/*
140280ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
140380ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
140480ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
140580ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
140680ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
140780ab7c0eSGarrett Wollman 	 */
1408a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
1409a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
1410a57815efSBosko Milekic 		goto dropwithreset;
1411a57815efSBosko Milekic 	}
141280ab7c0eSGarrett Wollman 
1413fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
1414df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1415fb59c426SYoshinobu Inoue 		if (thflags & TH_SYN) {
1416fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
1417fb59c426SYoshinobu Inoue 			th->th_seq++;
1418fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
1419fb59c426SYoshinobu Inoue 				th->th_urp--;
1420df8bae1dSRodney W. Grimes 			else
1421fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
1422df8bae1dSRodney W. Grimes 			todrop--;
1423df8bae1dSRodney W. Grimes 		}
1424df8bae1dSRodney W. Grimes 		/*
1425dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
1426df8bae1dSRodney W. Grimes 		 */
1427fb59c426SYoshinobu Inoue 		if (todrop > tlen
1428fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
1429dac20301SGarrett Wollman 			/*
1430dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
1431dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
1432dac20301SGarrett Wollman 			 * of sequence; drop it.
1433dac20301SGarrett Wollman 			 */
1434fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
1435dac20301SGarrett Wollman 
1436df8bae1dSRodney W. Grimes 			/*
1437dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
1438dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
1439df8bae1dSRodney W. Grimes 			 */
1440dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
1441fb59c426SYoshinobu Inoue 			todrop = tlen;
1442dac20301SGarrett Wollman 			tcpstat.tcps_rcvduppack++;
1443dac20301SGarrett Wollman 			tcpstat.tcps_rcvdupbyte += todrop;
1444df8bae1dSRodney W. Grimes 		} else {
1445df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartduppack++;
1446df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartdupbyte += todrop;
1447df8bae1dSRodney W. Grimes 		}
1448fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
1449fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
1450fb59c426SYoshinobu Inoue 		tlen -= todrop;
1451fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
1452fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
1453df8bae1dSRodney W. Grimes 		else {
1454fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
1455fb59c426SYoshinobu Inoue 			th->th_urp = 0;
1456df8bae1dSRodney W. Grimes 		}
1457df8bae1dSRodney W. Grimes 	}
1458df8bae1dSRodney W. Grimes 
1459df8bae1dSRodney W. Grimes 	/*
1460df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
1461df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
1462df8bae1dSRodney W. Grimes 	 */
1463df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
1464fb59c426SYoshinobu Inoue 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
1465df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1466df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvafterclose++;
1467a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
1468df8bae1dSRodney W. Grimes 		goto dropwithreset;
1469df8bae1dSRodney W. Grimes 	}
1470df8bae1dSRodney W. Grimes 
1471df8bae1dSRodney W. Grimes 	/*
1472df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
1473df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
1474df8bae1dSRodney W. Grimes 	 */
1475fb59c426SYoshinobu Inoue 	todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd);
1476df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1477df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvpackafterwin++;
1478fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
1479fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbyteafterwin += tlen;
1480df8bae1dSRodney W. Grimes 			/*
1481df8bae1dSRodney W. Grimes 			 * If a new connection request is received
1482df8bae1dSRodney W. Grimes 			 * while in TIME_WAIT, drop the old connection
1483df8bae1dSRodney W. Grimes 			 * and start over if the sequence numbers
1484df8bae1dSRodney W. Grimes 			 * are above the previous ones.
1485df8bae1dSRodney W. Grimes 			 */
1486fb59c426SYoshinobu Inoue 			if (thflags & TH_SYN &&
1487df8bae1dSRodney W. Grimes 			    tp->t_state == TCPS_TIME_WAIT &&
1488fb59c426SYoshinobu Inoue 			    SEQ_GT(th->th_seq, tp->rcv_nxt)) {
1489b0e3ad75SMike Silbersack 				iss = tcp_new_isn(tp);
1490df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
1491df8bae1dSRodney W. Grimes 				goto findpcb;
1492df8bae1dSRodney W. Grimes 			}
1493df8bae1dSRodney W. Grimes 			/*
1494df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
1495df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
1496df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
1497df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
1498df8bae1dSRodney W. Grimes 			 * and ack.
1499df8bae1dSRodney W. Grimes 			 */
1500fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
1501df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
1502df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvwinprobe++;
1503df8bae1dSRodney W. Grimes 			} else
1504df8bae1dSRodney W. Grimes 				goto dropafterack;
1505df8bae1dSRodney W. Grimes 		} else
1506df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
1507df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
1508fb59c426SYoshinobu Inoue 		tlen -= todrop;
1509fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
1510df8bae1dSRodney W. Grimes 	}
1511df8bae1dSRodney W. Grimes 
1512df8bae1dSRodney W. Grimes 	/*
1513df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
1514df8bae1dSRodney W. Grimes 	 * record its timestamp.
1515a0292f23SGarrett Wollman 	 * NOTE that the test is modified according to the latest
1516a0292f23SGarrett Wollman 	 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1517df8bae1dSRodney W. Grimes 	 */
1518be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
1519fb59c426SYoshinobu Inoue 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
15209b8b58e0SJonathan Lemon 		tp->ts_recent_age = ticks;
1521a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
1522df8bae1dSRodney W. Grimes 	}
1523df8bae1dSRodney W. Grimes 
1524df8bae1dSRodney W. Grimes 	/*
1525df8bae1dSRodney W. Grimes 	 * If a SYN is in the window, then this is an
1526df8bae1dSRodney W. Grimes 	 * error and we send an RST and drop the connection.
1527df8bae1dSRodney W. Grimes 	 */
1528fb59c426SYoshinobu Inoue 	if (thflags & TH_SYN) {
1529df8bae1dSRodney W. Grimes 		tp = tcp_drop(tp, ECONNRESET);
1530a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
1531df8bae1dSRodney W. Grimes 		goto dropwithreset;
1532df8bae1dSRodney W. Grimes 	}
1533df8bae1dSRodney W. Grimes 
1534a0292f23SGarrett Wollman 	/*
1535a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
1536a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
1537a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
1538a0292f23SGarrett Wollman 	 */
1539fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
1540a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
1541a0292f23SGarrett Wollman 		    (tp->t_flags & TF_NEEDSYN))
1542a0292f23SGarrett Wollman 			goto step6;
1543a0292f23SGarrett Wollman 		else
1544a0292f23SGarrett Wollman 			goto drop;
1545a0292f23SGarrett Wollman 	}
1546df8bae1dSRodney W. Grimes 
1547df8bae1dSRodney W. Grimes 	/*
1548df8bae1dSRodney W. Grimes 	 * Ack processing.
1549df8bae1dSRodney W. Grimes 	 */
1550df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1551df8bae1dSRodney W. Grimes 
1552df8bae1dSRodney W. Grimes 	/*
1553764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
1554764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
1555764d8cefSBill Fenner 	 * The ACK was checked above.
1556df8bae1dSRodney W. Grimes 	 */
1557df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1558a0292f23SGarrett Wollman 
1559df8bae1dSRodney W. Grimes 		tcpstat.tcps_connects++;
1560df8bae1dSRodney W. Grimes 		soisconnected(so);
1561df8bae1dSRodney W. Grimes 		/* Do window scaling? */
1562df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1563df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1564df8bae1dSRodney W. Grimes 			tp->snd_scale = tp->requested_s_scale;
1565df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
1566df8bae1dSRodney W. Grimes 		}
1567a0292f23SGarrett Wollman 		/*
1568a0292f23SGarrett Wollman 		 * Upon successful completion of 3-way handshake,
1569a0292f23SGarrett Wollman 		 * update cache.CC if it was undefined, pass any queued
1570a0292f23SGarrett Wollman 		 * data to the user, and advance state appropriately.
1571a0292f23SGarrett Wollman 		 */
1572be2ac88cSJonathan Lemon 		if ((taop = tcp_gettaocache(&inp->inp_inc)) != NULL &&
1573a0292f23SGarrett Wollman 		    taop->tao_cc == 0)
1574a0292f23SGarrett Wollman 			taop->tao_cc = tp->cc_recv;
1575a0292f23SGarrett Wollman 
1576a0292f23SGarrett Wollman 		/*
1577a0292f23SGarrett Wollman 		 * Make transitions:
1578a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
1579a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
1580a0292f23SGarrett Wollman 		 */
15819b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
1582a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDFIN) {
1583a0292f23SGarrett Wollman 			tp->t_state = TCPS_FIN_WAIT_1;
1584a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDFIN;
15857ff19458SPaul Traina 		} else {
1586a0292f23SGarrett Wollman 			tp->t_state = TCPS_ESTABLISHED;
15879b8b58e0SJonathan Lemon 			callout_reset(tp->tt_keep, tcp_keepidle,
15889b8b58e0SJonathan Lemon 				      tcp_timer_keep, tp);
15897ff19458SPaul Traina 		}
1590a0292f23SGarrett Wollman 		/*
1591a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
1592a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
1593a0292f23SGarrett Wollman 		 */
1594fb59c426SYoshinobu Inoue 		if (tlen == 0 && (thflags & TH_FIN) == 0)
1595fb59c426SYoshinobu Inoue 			(void) tcp_reass(tp, (struct tcphdr *)0, 0,
1596a0292f23SGarrett Wollman 			    (struct mbuf *)0);
1597fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
1598df8bae1dSRodney W. Grimes 		/* fall into ... */
1599df8bae1dSRodney W. Grimes 
1600df8bae1dSRodney W. Grimes 	/*
1601df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1602df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
1603fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
1604fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
1605df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
1606df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
1607df8bae1dSRodney W. Grimes 	 */
1608df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1609df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
1610df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
1611df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1612df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
1613df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
1614df8bae1dSRodney W. Grimes 	case TCPS_TIME_WAIT:
1615df8bae1dSRodney W. Grimes 
1616fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
1617fb59c426SYoshinobu Inoue 			if (tlen == 0 && tiwin == tp->snd_wnd) {
1618df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvdupack++;
1619df8bae1dSRodney W. Grimes 				/*
1620df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
1621df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
1622df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
1623df8bae1dSRodney W. Grimes 				 * change), the ack is the biggest we've
1624df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
1625df8bae1dSRodney W. Grimes 				 * threshhold of them, assume a packet
1626df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
1627df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
1628df8bae1dSRodney W. Grimes 				 * window so we send only this one
1629df8bae1dSRodney W. Grimes 				 * packet.
1630df8bae1dSRodney W. Grimes 				 *
1631df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
1632df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
1633df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
1634df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
1635df8bae1dSRodney W. Grimes 				 * the new ssthresh).
1636df8bae1dSRodney W. Grimes 				 *
1637df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
1638df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
1639df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
1640df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
1641df8bae1dSRodney W. Grimes 				 * network.
1642df8bae1dSRodney W. Grimes 				 */
16439b8b58e0SJonathan Lemon 				if (!callout_active(tp->tt_rexmt) ||
1644fb59c426SYoshinobu Inoue 				    th->th_ack != tp->snd_una)
1645df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
1646df8bae1dSRodney W. Grimes 				else if (++tp->t_dupacks == tcprexmtthresh) {
1647df8bae1dSRodney W. Grimes 					tcp_seq onxt = tp->snd_nxt;
1648df8bae1dSRodney W. Grimes 					u_int win =
1649df8bae1dSRodney W. Grimes 					    min(tp->snd_wnd, tp->snd_cwnd) / 2 /
1650df8bae1dSRodney W. Grimes 						tp->t_maxseg;
165146f58482SJonathan Lemon 					if (tcp_do_newreno && SEQ_LT(th->th_ack,
165246f58482SJonathan Lemon 					    tp->snd_recover)) {
165346f58482SJonathan Lemon 						/* False retransmit, should not
165446f58482SJonathan Lemon 						 * cut window
165546f58482SJonathan Lemon 						 */
165646f58482SJonathan Lemon 						tp->snd_cwnd += tp->t_maxseg;
165746f58482SJonathan Lemon 						tp->t_dupacks = 0;
165846f58482SJonathan Lemon 						(void) tcp_output(tp);
165946f58482SJonathan Lemon 						goto drop;
166046f58482SJonathan Lemon 					}
1661df8bae1dSRodney W. Grimes 					if (win < 2)
1662df8bae1dSRodney W. Grimes 						win = 2;
1663df8bae1dSRodney W. Grimes 					tp->snd_ssthresh = win * tp->t_maxseg;
166446f58482SJonathan Lemon 					tp->snd_recover = tp->snd_max;
16659b8b58e0SJonathan Lemon 					callout_stop(tp->tt_rexmt);
16669b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
1667fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
1668df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->t_maxseg;
1669df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1670df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
1671df8bae1dSRodney W. Grimes 					       tp->t_maxseg * tp->t_dupacks;
1672df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
1673df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
1674df8bae1dSRodney W. Grimes 					goto drop;
1675df8bae1dSRodney W. Grimes 				} else if (tp->t_dupacks > tcprexmtthresh) {
1676df8bae1dSRodney W. Grimes 					tp->snd_cwnd += tp->t_maxseg;
1677df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1678df8bae1dSRodney W. Grimes 					goto drop;
1679df8bae1dSRodney W. Grimes 				}
1680df8bae1dSRodney W. Grimes 			} else
1681df8bae1dSRodney W. Grimes 				tp->t_dupacks = 0;
1682df8bae1dSRodney W. Grimes 			break;
1683df8bae1dSRodney W. Grimes 		}
1684df8bae1dSRodney W. Grimes 		/*
1685df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
1686df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
1687df8bae1dSRodney W. Grimes 		 */
168846f58482SJonathan Lemon 		if (tcp_do_newreno == 0) {
1689233e8c18SGarrett Wollman                         if (tp->t_dupacks >= tcprexmtthresh &&
1690df8bae1dSRodney W. Grimes                                 tp->snd_cwnd > tp->snd_ssthresh)
1691df8bae1dSRodney W. Grimes                                 tp->snd_cwnd = tp->snd_ssthresh;
1692df8bae1dSRodney W. Grimes                         tp->t_dupacks = 0;
169346f58482SJonathan Lemon                 } else if (tp->t_dupacks >= tcprexmtthresh &&
169446f58482SJonathan Lemon 		    !tcp_newreno(tp, th)) {
169546f58482SJonathan Lemon                         /*
169646f58482SJonathan Lemon                          * Window inflation should have left us with approx.
169746f58482SJonathan Lemon                          * snd_ssthresh outstanding data.  But in case we
169846f58482SJonathan Lemon                          * would be inclined to send a burst, better to do
169946f58482SJonathan Lemon                          * it via the slow start mechanism.
170046f58482SJonathan Lemon                          */
170146f58482SJonathan Lemon 			if (SEQ_GT(th->th_ack + tp->snd_ssthresh, tp->snd_max))
170246f58482SJonathan Lemon                                 tp->snd_cwnd =
170346f58482SJonathan Lemon 				    tp->snd_max - th->th_ack + tp->t_maxseg;
170446f58482SJonathan Lemon 			else
170546f58482SJonathan Lemon                         	tp->snd_cwnd = tp->snd_ssthresh;
170646f58482SJonathan Lemon                         tp->t_dupacks = 0;
170746f58482SJonathan Lemon                 }
1708e7e2b801SJayanth Vijayaraghavan 		if (tp->t_dupacks < tcprexmtthresh)
1709e7e2b801SJayanth Vijayaraghavan 			tp->t_dupacks = 0;
1710fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
1711df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvacktoomuch++;
1712df8bae1dSRodney W. Grimes 			goto dropafterack;
1713df8bae1dSRodney W. Grimes 		}
1714a0292f23SGarrett Wollman 		/*
1715a0292f23SGarrett Wollman 		 *  If we reach this point, ACK is not a duplicate,
1716a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
1717a0292f23SGarrett Wollman 		 */
1718a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
1719a0292f23SGarrett Wollman 			/*
1720a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
1721a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
172207e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
172307e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
172407e43e10SAndras Olah 			 * we can do window scaling.
1725a0292f23SGarrett Wollman 			 */
1726a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
1727a0292f23SGarrett Wollman 			tp->snd_una++;
172807e43e10SAndras Olah 			/* Do window scaling? */
172907e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
173007e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
173107e43e10SAndras Olah 				tp->snd_scale = tp->requested_s_scale;
173207e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
173307e43e10SAndras Olah 			}
1734a0292f23SGarrett Wollman 		}
1735a0292f23SGarrett Wollman 
1736a0292f23SGarrett Wollman process_ACK:
1737fb59c426SYoshinobu Inoue 		acked = th->th_ack - tp->snd_una;
1738df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackpack++;
1739df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackbyte += acked;
1740df8bae1dSRodney W. Grimes 
1741df8bae1dSRodney W. Grimes 		/*
17429b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
17439b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
17449b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
17459b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
17469b8b58e0SJonathan Lemon 		 * we left off.
17479b8b58e0SJonathan Lemon 		 */
17489b8b58e0SJonathan Lemon 		if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) {
17499b8b58e0SJonathan Lemon 			tp->snd_cwnd = tp->snd_cwnd_prev;
17509b8b58e0SJonathan Lemon 			tp->snd_ssthresh = tp->snd_ssthresh_prev;
17519b8b58e0SJonathan Lemon 			tp->snd_nxt = tp->snd_max;
17529b8b58e0SJonathan Lemon 			tp->t_badrxtwin = 0;	/* XXX probably not required */
17539b8b58e0SJonathan Lemon 		}
17549b8b58e0SJonathan Lemon 
17559b8b58e0SJonathan Lemon 		/*
1756df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
1757df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
1758df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
1759df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
1760df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
1761df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
1762df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
1763df8bae1dSRodney W. Grimes 		 */
1764be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_TS)
17659b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - to.to_tsecr + 1);
1766fb59c426SYoshinobu Inoue 		else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq))
17679b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
1768df8bae1dSRodney W. Grimes 
1769df8bae1dSRodney W. Grimes 		/*
1770df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
1771df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
1772df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
1773df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
1774df8bae1dSRodney W. Grimes 		 */
1775fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
17769b8b58e0SJonathan Lemon 			callout_stop(tp->tt_rexmt);
1777df8bae1dSRodney W. Grimes 			needoutput = 1;
17789b8b58e0SJonathan Lemon 		} else if (!callout_active(tp->tt_persist))
17799b8b58e0SJonathan Lemon 			callout_reset(tp->tt_rexmt, tp->t_rxtcur,
17809b8b58e0SJonathan Lemon 				      tcp_timer_rexmt, tp);
1781a0292f23SGarrett Wollman 
1782a0292f23SGarrett Wollman 		/*
1783a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
1784a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
1785a0292f23SGarrett Wollman 		 */
1786a0292f23SGarrett Wollman 		if (acked == 0)
1787a0292f23SGarrett Wollman 			goto step6;
1788a0292f23SGarrett Wollman 
1789df8bae1dSRodney W. Grimes 		/*
1790df8bae1dSRodney W. Grimes 		 * When new data is acked, open the congestion window.
1791df8bae1dSRodney W. Grimes 		 * If the window gives us less than ssthresh packets
1792df8bae1dSRodney W. Grimes 		 * in flight, open exponentially (maxseg per packet).
1793df8bae1dSRodney W. Grimes 		 * Otherwise open linearly: maxseg per window
179410be5648SGarrett Wollman 		 * (maxseg^2 / cwnd per packet).
1795df8bae1dSRodney W. Grimes 		 */
1796df8bae1dSRodney W. Grimes 		{
1797df8bae1dSRodney W. Grimes 		register u_int cw = tp->snd_cwnd;
1798df8bae1dSRodney W. Grimes 		register u_int incr = tp->t_maxseg;
1799df8bae1dSRodney W. Grimes 
1800df8bae1dSRodney W. Grimes 		if (cw > tp->snd_ssthresh)
180110be5648SGarrett Wollman 			incr = incr * incr / cw;
18028735719eSJonathan Lemon 		/*
18038735719eSJonathan Lemon 		 * If t_dupacks != 0 here, it indicates that we are still
18048735719eSJonathan Lemon 		 * in NewReno fast recovery mode, so we leave the congestion
18058735719eSJonathan Lemon 		 * window alone.
18068735719eSJonathan Lemon 		 */
18078735719eSJonathan Lemon 		if (tcp_do_newreno == 0 || tp->t_dupacks == 0)
1808df8bae1dSRodney W. Grimes 			tp->snd_cwnd = min(cw + incr,TCP_MAXWIN<<tp->snd_scale);
1809df8bae1dSRodney W. Grimes 		}
1810df8bae1dSRodney W. Grimes 		if (acked > so->so_snd.sb_cc) {
1811df8bae1dSRodney W. Grimes 			tp->snd_wnd -= so->so_snd.sb_cc;
1812df8bae1dSRodney W. Grimes 			sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
1813df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
1814df8bae1dSRodney W. Grimes 		} else {
1815df8bae1dSRodney W. Grimes 			sbdrop(&so->so_snd, acked);
1816df8bae1dSRodney W. Grimes 			tp->snd_wnd -= acked;
1817df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
1818df8bae1dSRodney W. Grimes 		}
1819df8bae1dSRodney W. Grimes 		sowwakeup(so);
1820fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
1821df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1822df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
1823df8bae1dSRodney W. Grimes 
1824df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
1825df8bae1dSRodney W. Grimes 
1826df8bae1dSRodney W. Grimes 		/*
1827df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
1828df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
1829df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
1830df8bae1dSRodney W. Grimes 		 */
1831df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
1832df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
1833df8bae1dSRodney W. Grimes 				/*
1834df8bae1dSRodney W. Grimes 				 * If we can't receive any more
1835df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
1836df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
1837df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
1838df8bae1dSRodney W. Grimes 				 * we'll hang forever.
1839df8bae1dSRodney W. Grimes 				 */
1840df8bae1dSRodney W. Grimes 				if (so->so_state & SS_CANTRCVMORE) {
1841df8bae1dSRodney W. Grimes 					soisdisconnected(so);
18429b8b58e0SJonathan Lemon 					callout_reset(tp->tt_2msl, tcp_maxidle,
18439b8b58e0SJonathan Lemon 						      tcp_timer_2msl, tp);
1844df8bae1dSRodney W. Grimes 				}
1845df8bae1dSRodney W. Grimes 				tp->t_state = TCPS_FIN_WAIT_2;
1846df8bae1dSRodney W. Grimes 			}
1847df8bae1dSRodney W. Grimes 			break;
1848df8bae1dSRodney W. Grimes 
1849df8bae1dSRodney W. Grimes 	 	/*
1850df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
1851df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
1852df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
1853df8bae1dSRodney W. Grimes 		 * the segment.
1854df8bae1dSRodney W. Grimes 		 */
1855df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
1856df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
1857df8bae1dSRodney W. Grimes 				tp->t_state = TCPS_TIME_WAIT;
1858df8bae1dSRodney W. Grimes 				tcp_canceltimers(tp);
1859a0292f23SGarrett Wollman 				/* Shorten TIME_WAIT [RFC-1644, p.28] */
1860a0292f23SGarrett Wollman 				if (tp->cc_recv != 0 &&
18619b8b58e0SJonathan Lemon 				    (ticks - tp->t_starttime) < tcp_msl)
18629b8b58e0SJonathan Lemon 					callout_reset(tp->tt_2msl,
18639b8b58e0SJonathan Lemon 						      tp->t_rxtcur *
18649b8b58e0SJonathan Lemon 						      TCPTV_TWTRUNC,
18659b8b58e0SJonathan Lemon 						      tcp_timer_2msl, tp);
1866a0292f23SGarrett Wollman 				else
18679b8b58e0SJonathan Lemon 					callout_reset(tp->tt_2msl, 2 * tcp_msl,
18689b8b58e0SJonathan Lemon 						      tcp_timer_2msl, tp);
1869df8bae1dSRodney W. Grimes 				soisdisconnected(so);
1870df8bae1dSRodney W. Grimes 			}
1871df8bae1dSRodney W. Grimes 			break;
1872df8bae1dSRodney W. Grimes 
1873df8bae1dSRodney W. Grimes 		/*
1874df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
1875df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
1876df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
1877df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
1878df8bae1dSRodney W. Grimes 		 */
1879df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
1880df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
1881df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
1882df8bae1dSRodney W. Grimes 				goto drop;
1883df8bae1dSRodney W. Grimes 			}
1884df8bae1dSRodney W. Grimes 			break;
1885df8bae1dSRodney W. Grimes 
1886df8bae1dSRodney W. Grimes 		/*
1887df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state the only thing that should arrive
1888df8bae1dSRodney W. Grimes 		 * is a retransmission of the remote FIN.  Acknowledge
1889df8bae1dSRodney W. Grimes 		 * it and restart the finack timer.
1890df8bae1dSRodney W. Grimes 		 */
1891df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
18929b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, 2 * tcp_msl,
18939b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
1894df8bae1dSRodney W. Grimes 			goto dropafterack;
1895df8bae1dSRodney W. Grimes 		}
1896df8bae1dSRodney W. Grimes 	}
1897df8bae1dSRodney W. Grimes 
1898df8bae1dSRodney W. Grimes step6:
1899df8bae1dSRodney W. Grimes 	/*
1900df8bae1dSRodney W. Grimes 	 * Update window information.
1901df8bae1dSRodney W. Grimes 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
1902df8bae1dSRodney W. Grimes 	 */
1903fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) &&
1904fb59c426SYoshinobu Inoue 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
1905fb59c426SYoshinobu Inoue 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
1906fb59c426SYoshinobu Inoue 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
1907df8bae1dSRodney W. Grimes 		/* keep track of pure window updates */
1908fb59c426SYoshinobu Inoue 		if (tlen == 0 &&
1909fb59c426SYoshinobu Inoue 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
1910df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvwinupd++;
1911df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
1912fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq;
1913fb59c426SYoshinobu Inoue 		tp->snd_wl2 = th->th_ack;
1914df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
1915df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
1916df8bae1dSRodney W. Grimes 		needoutput = 1;
1917df8bae1dSRodney W. Grimes 	}
1918df8bae1dSRodney W. Grimes 
1919df8bae1dSRodney W. Grimes 	/*
1920df8bae1dSRodney W. Grimes 	 * Process segments with URG.
1921df8bae1dSRodney W. Grimes 	 */
1922fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
1923df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1924df8bae1dSRodney W. Grimes 		/*
1925df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
1926df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
1927df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
1928df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
1929df8bae1dSRodney W. Grimes 		 */
1930fb59c426SYoshinobu Inoue 		if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
1931fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
1932fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
1933df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
1934df8bae1dSRodney W. Grimes 		}
1935df8bae1dSRodney W. Grimes 		/*
1936df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
1937df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
1938df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1939df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
1940df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
1941df8bae1dSRodney W. Grimes 		 *
1942df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
1943df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
1944df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
1945df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
1946df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
1947df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
1948df8bae1dSRodney W. Grimes 		 */
1949fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
1950fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
1951df8bae1dSRodney W. Grimes 			so->so_oobmark = so->so_rcv.sb_cc +
1952df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
1953df8bae1dSRodney W. Grimes 			if (so->so_oobmark == 0)
1954df8bae1dSRodney W. Grimes 				so->so_state |= SS_RCVATMARK;
1955df8bae1dSRodney W. Grimes 			sohasoutofband(so);
1956df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1957df8bae1dSRodney W. Grimes 		}
1958df8bae1dSRodney W. Grimes 		/*
1959df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
1960df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
1961df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
1962df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
1963df8bae1dSRodney W. Grimes 		 */
1964fb59c426SYoshinobu Inoue 		if (th->th_urp <= (u_long)tlen
1965df8bae1dSRodney W. Grimes #ifdef SO_OOBINLINE
1966df8bae1dSRodney W. Grimes 		     && (so->so_options & SO_OOBINLINE) == 0
1967df8bae1dSRodney W. Grimes #endif
1968df8bae1dSRodney W. Grimes 		     )
1969fb59c426SYoshinobu Inoue 			tcp_pulloutofband(so, th, m,
1970fb59c426SYoshinobu Inoue 				drop_hdrlen);	/* hdr drop is delayed */
1971df8bae1dSRodney W. Grimes 	} else
1972df8bae1dSRodney W. Grimes 		/*
1973df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
1974df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
1975df8bae1dSRodney W. Grimes 		 * with the receive window.
1976df8bae1dSRodney W. Grimes 		 */
1977df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1978df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
1979df8bae1dSRodney W. Grimes dodata:							/* XXX */
1980df8bae1dSRodney W. Grimes 
1981df8bae1dSRodney W. Grimes 	/*
1982df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
1983df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
1984df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
1985df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
1986df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
1987df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
1988df8bae1dSRodney W. Grimes 	 */
1989fb59c426SYoshinobu Inoue 	if ((tlen || (thflags&TH_FIN)) &&
1990df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1991fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
1992e4b64281SJesper Skriver 		/*
1993e4b64281SJesper Skriver 		 * Insert segment which inludes th into reassembly queue of tcp with
1994e4b64281SJesper Skriver 		 * control block tp.  Return TH_FIN if reassembly now includes
1995e4b64281SJesper Skriver 		 * a segment with FIN.  This handle the common case inline (segment
1996e4b64281SJesper Skriver 		 * is the next to be received on an established connection, and the
1997e4b64281SJesper Skriver 		 * queue is empty), avoiding linkage into and removal from the queue
1998e4b64281SJesper Skriver 		 * and repetition of various conversions.
1999e4b64281SJesper Skriver 		 * Set DELACK for segments received in order, but ack immediately
2000e4b64281SJesper Skriver 		 * when segments are out of order (so fast retransmit can work).
2001e4b64281SJesper Skriver 		 */
2002e4b64281SJesper Skriver 		if (th->th_seq == tp->rcv_nxt &&
2003e4b64281SJesper Skriver 		    LIST_EMPTY(&tp->t_segq) &&
2004e4b64281SJesper Skriver 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
2005e4b64281SJesper Skriver 			if (DELAY_ACK(tp))
2006e4b64281SJesper Skriver 				callout_reset(tp->tt_delack, tcp_delacktime,
2007e4b64281SJesper Skriver 				    tcp_timer_delack, tp);
2008e4b64281SJesper Skriver 			else
2009e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
2010e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
2011e4b64281SJesper Skriver 			thflags = th->th_flags & TH_FIN;
2012e4b64281SJesper Skriver 			tcpstat.tcps_rcvpack++;
2013e4b64281SJesper Skriver 			tcpstat.tcps_rcvbyte += tlen;
2014e4b64281SJesper Skriver 			ND6_HINT(tp);
2015e4b64281SJesper Skriver 			sbappend(&so->so_rcv, m);
2016e4b64281SJesper Skriver 			sorwakeup(so);
2017e4b64281SJesper Skriver 		} else {
2018e4b64281SJesper Skriver 			thflags = tcp_reass(tp, th, &tlen, m);
2019e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
2020e4b64281SJesper Skriver 		}
2021e4b64281SJesper Skriver 
2022df8bae1dSRodney W. Grimes 		/*
2023df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
2024df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
2025df8bae1dSRodney W. Grimes 		 * buffer size.
2026df8bae1dSRodney W. Grimes 		 */
2027df8bae1dSRodney W. Grimes 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2028df8bae1dSRodney W. Grimes 	} else {
2029df8bae1dSRodney W. Grimes 		m_freem(m);
2030fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
2031df8bae1dSRodney W. Grimes 	}
2032df8bae1dSRodney W. Grimes 
2033df8bae1dSRodney W. Grimes 	/*
2034df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
2035df8bae1dSRodney W. Grimes 	 * that the connection is closing.
2036df8bae1dSRodney W. Grimes 	 */
2037fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
2038df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2039df8bae1dSRodney W. Grimes 			socantrcvmore(so);
2040a0292f23SGarrett Wollman 			/*
2041a0292f23SGarrett Wollman 			 *  If connection is half-synchronized
2042d3eede9dSAndras Olah 			 *  (ie NEEDSYN flag on) then delay ACK,
2043a0292f23SGarrett Wollman 			 *  so it may be piggybacked when SYN is sent.
2044a0292f23SGarrett Wollman 			 *  Otherwise, since we received a FIN then no
2045a0292f23SGarrett Wollman 			 *  more input can be expected, send ACK now.
2046a0292f23SGarrett Wollman 			 */
2047d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp) && (tp->t_flags & TF_NEEDSYN))
20489b8b58e0SJonathan Lemon                                 callout_reset(tp->tt_delack, tcp_delacktime,
20499b8b58e0SJonathan Lemon                                     tcp_timer_delack, tp);
2050a0292f23SGarrett Wollman 			else
2051df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
2052df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
2053df8bae1dSRodney W. Grimes 		}
2054df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2055df8bae1dSRodney W. Grimes 
2056df8bae1dSRodney W. Grimes 	 	/*
2057df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
2058df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
2059df8bae1dSRodney W. Grimes 		 */
2060df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
20619b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
20629b8b58e0SJonathan Lemon 			/*FALLTHROUGH*/
2063df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
2064df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSE_WAIT;
2065df8bae1dSRodney W. Grimes 			break;
2066df8bae1dSRodney W. Grimes 
2067df8bae1dSRodney W. Grimes 	 	/*
2068df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2069df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
2070df8bae1dSRodney W. Grimes 		 */
2071df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2072df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSING;
2073df8bae1dSRodney W. Grimes 			break;
2074df8bae1dSRodney W. Grimes 
2075df8bae1dSRodney W. Grimes 	 	/*
2076df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2077df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
2078df8bae1dSRodney W. Grimes 		 * standard timers.
2079df8bae1dSRodney W. Grimes 		 */
2080df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
2081df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_TIME_WAIT;
2082df8bae1dSRodney W. Grimes 			tcp_canceltimers(tp);
2083a0292f23SGarrett Wollman 			/* Shorten TIME_WAIT [RFC-1644, p.28] */
2084a0292f23SGarrett Wollman 			if (tp->cc_recv != 0 &&
20859b8b58e0SJonathan Lemon 			    (ticks - tp->t_starttime) < tcp_msl) {
20869b8b58e0SJonathan Lemon 				callout_reset(tp->tt_2msl,
20879b8b58e0SJonathan Lemon 					      tp->t_rxtcur * TCPTV_TWTRUNC,
20889b8b58e0SJonathan Lemon 					      tcp_timer_2msl, tp);
2089a0292f23SGarrett Wollman 				/* For transaction client, force ACK now. */
2090a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
2091a0292f23SGarrett Wollman 			}
2092a0292f23SGarrett Wollman 			else
20939b8b58e0SJonathan Lemon 				callout_reset(tp->tt_2msl, 2 * tcp_msl,
20949b8b58e0SJonathan Lemon 					      tcp_timer_2msl, tp);
2095df8bae1dSRodney W. Grimes 			soisdisconnected(so);
2096df8bae1dSRodney W. Grimes 			break;
2097df8bae1dSRodney W. Grimes 
2098df8bae1dSRodney W. Grimes 		/*
2099df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
2100df8bae1dSRodney W. Grimes 		 */
2101df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
21029b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, 2 * tcp_msl,
21039b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
2104df8bae1dSRodney W. Grimes 			break;
2105df8bae1dSRodney W. Grimes 		}
2106df8bae1dSRodney W. Grimes 	}
2107610ee2f9SDavid Greenman #ifdef TCPDEBUG
2108df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG)
2109fb59c426SYoshinobu Inoue 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
2110fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2111610ee2f9SDavid Greenman #endif
2112df8bae1dSRodney W. Grimes 
2113df8bae1dSRodney W. Grimes 	/*
2114df8bae1dSRodney W. Grimes 	 * Return any desired output.
2115df8bae1dSRodney W. Grimes 	 */
2116df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
2117df8bae1dSRodney W. Grimes 		(void) tcp_output(tp);
2118df8bae1dSRodney W. Grimes 	return;
2119df8bae1dSRodney W. Grimes 
2120df8bae1dSRodney W. Grimes dropafterack:
2121df8bae1dSRodney W. Grimes 	/*
2122df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
2123df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
212480ab7c0eSGarrett Wollman 	 *
212580ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
212680ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
212780ab7c0eSGarrett Wollman 	 * RST have been dropped.
212880ab7c0eSGarrett Wollman 	 *
212980ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
213080ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
213180ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
213280ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
213380ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
213480ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
2135df8bae1dSRodney W. Grimes 	 */
2136fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
2137fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
2138a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
2139a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2140a57815efSBosko Milekic 		goto dropwithreset;
2141a57815efSBosko Milekic 	}
2142a0292f23SGarrett Wollman #ifdef TCPDEBUG
2143a0292f23SGarrett Wollman 	if (so->so_options & SO_DEBUG)
2144fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2145fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2146a0292f23SGarrett Wollman #endif
2147df8bae1dSRodney W. Grimes 	m_freem(m);
2148df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
2149df8bae1dSRodney W. Grimes 	(void) tcp_output(tp);
2150df8bae1dSRodney W. Grimes 	return;
2151df8bae1dSRodney W. Grimes 
2152df8bae1dSRodney W. Grimes dropwithreset:
2153df8bae1dSRodney W. Grimes 	/*
2154df8bae1dSRodney W. Grimes 	 * Generate a RST, dropping incoming segment.
2155df8bae1dSRodney W. Grimes 	 * Make ACK acceptable to originator of segment.
2156df8bae1dSRodney W. Grimes 	 * Don't bother to respond if destination was broadcast/multicast.
2157df8bae1dSRodney W. Grimes 	 */
2158fb59c426SYoshinobu Inoue 	if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
2159df8bae1dSRodney W. Grimes 		goto drop;
2160fb59c426SYoshinobu Inoue #ifdef INET6
2161fb59c426SYoshinobu Inoue 	if (isipv6) {
2162173c0f9fSWarner Losh 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
2163173c0f9fSWarner Losh 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
2164fb59c426SYoshinobu Inoue 			goto drop;
2165fb59c426SYoshinobu Inoue 	} else
2166fb59c426SYoshinobu Inoue #endif /* INET6 */
2167173c0f9fSWarner Losh 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
2168173c0f9fSWarner Losh 	    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
2169173c0f9fSWarner Losh 	    ip->ip_src.s_addr == htonl(INADDR_BROADCAST))
2170fb59c426SYoshinobu Inoue 		goto drop;
2171fb59c426SYoshinobu Inoue 	/* IPv6 anycast check is done at tcp6_input() */
2172a57815efSBosko Milekic 
2173a57815efSBosko Milekic 	/*
2174c59319bfSDag-Erling Smørgrav 	 * Perform bandwidth limiting.
2175a57815efSBosko Milekic 	 */
2176a57815efSBosko Milekic 	if (badport_bandlim(rstreason) < 0)
2177a57815efSBosko Milekic 		goto drop;
2178a57815efSBosko Milekic 
2179a0292f23SGarrett Wollman #ifdef TCPDEBUG
2180a0292f23SGarrett Wollman 	if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2181fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2182fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2183a0292f23SGarrett Wollman #endif
2184fb59c426SYoshinobu Inoue 	if (thflags & TH_ACK)
2185fb59c426SYoshinobu Inoue 		/* mtod() below is safe as long as hdr dropping is delayed */
2186fb59c426SYoshinobu Inoue 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack,
2187fb59c426SYoshinobu Inoue 			    TH_RST);
2188df8bae1dSRodney W. Grimes 	else {
2189fb59c426SYoshinobu Inoue 		if (thflags & TH_SYN)
2190fb59c426SYoshinobu Inoue 			tlen++;
2191fb59c426SYoshinobu Inoue 		/* mtod() below is safe as long as hdr dropping is delayed */
2192fb59c426SYoshinobu Inoue 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
2193fb59c426SYoshinobu Inoue 			    (tcp_seq)0, TH_RST|TH_ACK);
2194df8bae1dSRodney W. Grimes 	}
2195df8bae1dSRodney W. Grimes 	return;
2196df8bae1dSRodney W. Grimes 
2197df8bae1dSRodney W. Grimes drop:
2198df8bae1dSRodney W. Grimes 	/*
2199df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
2200df8bae1dSRodney W. Grimes 	 */
2201610ee2f9SDavid Greenman #ifdef TCPDEBUG
2202a0292f23SGarrett Wollman 	if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2203fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2204fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2205a0292f23SGarrett Wollman #endif
2206df8bae1dSRodney W. Grimes 	m_freem(m);
2207df8bae1dSRodney W. Grimes 	return;
2208df8bae1dSRodney W. Grimes }
2209df8bae1dSRodney W. Grimes 
2210be2ac88cSJonathan Lemon /*
2211be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
2212be2ac88cSJonathan Lemon  */
22130312fbe9SPoul-Henning Kamp static void
2214be2ac88cSJonathan Lemon tcp_dooptions(to, cp, cnt, is_syn)
2215be2ac88cSJonathan Lemon 	struct tcpopt *to;
2216df8bae1dSRodney W. Grimes 	u_char *cp;
2217df8bae1dSRodney W. Grimes 	int cnt;
2218df8bae1dSRodney W. Grimes {
2219df8bae1dSRodney W. Grimes 	int opt, optlen;
2220df8bae1dSRodney W. Grimes 
2221be2ac88cSJonathan Lemon 	to->to_flags = 0;
2222df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
2223df8bae1dSRodney W. Grimes 		opt = cp[0];
2224df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
2225df8bae1dSRodney W. Grimes 			break;
2226df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
2227df8bae1dSRodney W. Grimes 			optlen = 1;
2228df8bae1dSRodney W. Grimes 		else {
2229b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
2230b474779fSJun-ichiro itojun Hagino 				break;
2231df8bae1dSRodney W. Grimes 			optlen = cp[1];
2232b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
2233df8bae1dSRodney W. Grimes 				break;
2234df8bae1dSRodney W. Grimes 		}
2235df8bae1dSRodney W. Grimes 		switch (opt) {
2236df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
2237df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
2238df8bae1dSRodney W. Grimes 				continue;
2239be2ac88cSJonathan Lemon 			if (!is_syn)
2240df8bae1dSRodney W. Grimes 				continue;
2241be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
2242be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
2243be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
2244be2ac88cSJonathan Lemon 			NTOHS(to->to_mss);
2245df8bae1dSRodney W. Grimes 			break;
2246df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
2247df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
2248df8bae1dSRodney W. Grimes 				continue;
2249be2ac88cSJonathan Lemon 			if (! is_syn)
2250df8bae1dSRodney W. Grimes 				continue;
2251be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
2252be2ac88cSJonathan Lemon 			to->to_requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
2253df8bae1dSRodney W. Grimes 			break;
2254df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
2255df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
2256df8bae1dSRodney W. Grimes 				continue;
2257be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
2258a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2259a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
2260a0292f23SGarrett Wollman 			NTOHL(to->to_tsval);
2261a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
2262a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
2263a0292f23SGarrett Wollman 			NTOHL(to->to_tsecr);
2264df8bae1dSRodney W. Grimes 			break;
2265a0292f23SGarrett Wollman 		case TCPOPT_CC:
2266a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
2267a0292f23SGarrett Wollman 				continue;
2268be2ac88cSJonathan Lemon 			to->to_flags |= TOF_CC;
2269a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2270a0292f23SGarrett Wollman 			    (char *)&to->to_cc, sizeof(to->to_cc));
2271a0292f23SGarrett Wollman 			NTOHL(to->to_cc);
2272a0292f23SGarrett Wollman 			break;
2273a0292f23SGarrett Wollman 		case TCPOPT_CCNEW:
2274a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
2275a0292f23SGarrett Wollman 				continue;
2276be2ac88cSJonathan Lemon 			if (!is_syn)
2277a0292f23SGarrett Wollman 				continue;
2278be2ac88cSJonathan Lemon 			to->to_flags |= TOF_CCNEW;
2279a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2280a0292f23SGarrett Wollman 			    (char *)&to->to_cc, sizeof(to->to_cc));
2281a0292f23SGarrett Wollman 			NTOHL(to->to_cc);
2282a0292f23SGarrett Wollman 			break;
2283a0292f23SGarrett Wollman 		case TCPOPT_CCECHO:
2284a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
2285a0292f23SGarrett Wollman 				continue;
2286be2ac88cSJonathan Lemon 			if (!is_syn)
2287a0292f23SGarrett Wollman 				continue;
2288be2ac88cSJonathan Lemon 			to->to_flags |= TOF_CCECHO;
2289a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2290a0292f23SGarrett Wollman 			    (char *)&to->to_ccecho, sizeof(to->to_ccecho));
2291a0292f23SGarrett Wollman 			NTOHL(to->to_ccecho);
2292a0292f23SGarrett Wollman 			break;
2293be2ac88cSJonathan Lemon 		default:
2294be2ac88cSJonathan Lemon 			continue;
2295df8bae1dSRodney W. Grimes 		}
2296df8bae1dSRodney W. Grimes 	}
2297df8bae1dSRodney W. Grimes }
2298df8bae1dSRodney W. Grimes 
2299df8bae1dSRodney W. Grimes /*
2300df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
2301df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
2302df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
2303df8bae1dSRodney W. Grimes  * sequencing purposes.
2304df8bae1dSRodney W. Grimes  */
23050312fbe9SPoul-Henning Kamp static void
2306fb59c426SYoshinobu Inoue tcp_pulloutofband(so, th, m, off)
2307df8bae1dSRodney W. Grimes 	struct socket *so;
2308fb59c426SYoshinobu Inoue 	struct tcphdr *th;
2309df8bae1dSRodney W. Grimes 	register struct mbuf *m;
2310fb59c426SYoshinobu Inoue 	int off;		/* delayed to be droped hdrlen */
2311df8bae1dSRodney W. Grimes {
2312fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
2313df8bae1dSRodney W. Grimes 
2314df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
2315df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
2316df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
2317df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
2318df8bae1dSRodney W. Grimes 
2319df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
2320df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
2321df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
2322df8bae1dSRodney W. Grimes 			m->m_len--;
232369a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
232469a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
2325df8bae1dSRodney W. Grimes 			return;
2326df8bae1dSRodney W. Grimes 		}
2327df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
2328df8bae1dSRodney W. Grimes 		m = m->m_next;
2329df8bae1dSRodney W. Grimes 		if (m == 0)
2330df8bae1dSRodney W. Grimes 			break;
2331df8bae1dSRodney W. Grimes 	}
2332df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
2333df8bae1dSRodney W. Grimes }
2334df8bae1dSRodney W. Grimes 
2335df8bae1dSRodney W. Grimes /*
2336df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
2337df8bae1dSRodney W. Grimes  * and update averages and current timeout.
2338df8bae1dSRodney W. Grimes  */
23390312fbe9SPoul-Henning Kamp static void
2340df8bae1dSRodney W. Grimes tcp_xmit_timer(tp, rtt)
2341df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
23429b8b58e0SJonathan Lemon 	int rtt;
2343df8bae1dSRodney W. Grimes {
2344233e8c18SGarrett Wollman 	register int delta;
2345233e8c18SGarrett Wollman 
2346233e8c18SGarrett Wollman 	tcpstat.tcps_rttupdated++;
2347233e8c18SGarrett Wollman 	tp->t_rttupdated++;
2348233e8c18SGarrett Wollman 	if (tp->t_srtt != 0) {
2349233e8c18SGarrett Wollman 		/*
2350233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
2351233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
2352233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
2353233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
2354233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
2355233e8c18SGarrett Wollman 		 */
2356233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
2357233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
2358233e8c18SGarrett Wollman 
2359233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
2360233e8c18SGarrett Wollman 			tp->t_srtt = 1;
2361233e8c18SGarrett Wollman 
2362233e8c18SGarrett Wollman 		/*
2363233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
2364233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
2365233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
2366233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
2367233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
2368233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
2369233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
2370233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
2371233e8c18SGarrett Wollman 		 */
2372233e8c18SGarrett Wollman 		if (delta < 0)
2373233e8c18SGarrett Wollman 			delta = -delta;
2374233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
2375233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
2376233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
2377233e8c18SGarrett Wollman 	} else {
2378233e8c18SGarrett Wollman 		/*
2379233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
2380233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
2381233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
2382233e8c18SGarrett Wollman 		 */
2383233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
2384233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
2385233e8c18SGarrett Wollman 	}
23869b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
2387df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
2388df8bae1dSRodney W. Grimes 
2389df8bae1dSRodney W. Grimes 	/*
2390df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
2391df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
2392df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
2393df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
2394df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
2395df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
2396df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
2397df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
2398df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
2399df8bae1dSRodney W. Grimes 	 */
2400233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
24019e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
2402df8bae1dSRodney W. Grimes 
2403df8bae1dSRodney W. Grimes 	/*
2404df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
2405df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
2406df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
2407df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
2408df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
2409df8bae1dSRodney W. Grimes 	 */
2410df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
2411df8bae1dSRodney W. Grimes }
2412df8bae1dSRodney W. Grimes 
2413df8bae1dSRodney W. Grimes /*
2414df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
2415df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
2416df8bae1dSRodney W. Grimes  * If none, use an mss that can be handled on the outgoing
2417df8bae1dSRodney W. Grimes  * interface without forcing IP to fragment; if bigger than
2418df8bae1dSRodney W. Grimes  * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
2419df8bae1dSRodney W. Grimes  * to utilize large mbufs.  If no route is found, route has no mtu,
2420df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
2421df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
2422df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
2423df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
2424df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
2425df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
2426df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
2427a0292f23SGarrett Wollman  *
2428a0292f23SGarrett Wollman  * Also take into account the space needed for options that we
2429a0292f23SGarrett Wollman  * send regularly.  Make maxseg shorter by that amount to assure
2430a0292f23SGarrett Wollman  * that we can send maxseg amount of data even when the options
2431a0292f23SGarrett Wollman  * are present.  Store the upper limit of the length of options plus
2432a0292f23SGarrett Wollman  * data in maxopd.
2433a0292f23SGarrett Wollman  *
2434a0292f23SGarrett Wollman  * NOTE that this routine is only called when we process an incoming
2435a0292f23SGarrett Wollman  * segment, for outgoing segments only tcp_mssopt is called.
2436a0292f23SGarrett Wollman  *
2437a0292f23SGarrett Wollman  * In case of T/TCP, we call this routine during implicit connection
2438a0292f23SGarrett Wollman  * setup as well (offer = -1), to initialize maxseg from the cached
2439a0292f23SGarrett Wollman  * MSS of our peer.
2440df8bae1dSRodney W. Grimes  */
2441a0292f23SGarrett Wollman void
2442df8bae1dSRodney W. Grimes tcp_mss(tp, offer)
2443a0292f23SGarrett Wollman 	struct tcpcb *tp;
2444a0292f23SGarrett Wollman 	int offer;
2445df8bae1dSRodney W. Grimes {
2446df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
2447df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
2448df8bae1dSRodney W. Grimes 	register int rtt, mss;
2449df8bae1dSRodney W. Grimes 	u_long bufsize;
2450df8bae1dSRodney W. Grimes 	struct inpcb *inp;
2451df8bae1dSRodney W. Grimes 	struct socket *so;
2452a0292f23SGarrett Wollman 	struct rmxp_tao *taop;
2453a0292f23SGarrett Wollman 	int origoffer = offer;
2454fb59c426SYoshinobu Inoue #ifdef INET6
2455fb59c426SYoshinobu Inoue 	int isipv6;
2456fb59c426SYoshinobu Inoue 	int min_protoh;
2457fb59c426SYoshinobu Inoue #endif
2458df8bae1dSRodney W. Grimes 
2459df8bae1dSRodney W. Grimes 	inp = tp->t_inpcb;
2460fb59c426SYoshinobu Inoue #ifdef INET6
2461fb59c426SYoshinobu Inoue 	isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
2462fb59c426SYoshinobu Inoue 	min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr)
2463fb59c426SYoshinobu Inoue 			    : sizeof (struct tcpiphdr);
2464fb59c426SYoshinobu Inoue #else
2465fb59c426SYoshinobu Inoue #define min_protoh  (sizeof (struct tcpiphdr))
2466fb59c426SYoshinobu Inoue #endif
2467fb59c426SYoshinobu Inoue #ifdef INET6
2468fb59c426SYoshinobu Inoue 	if (isipv6)
2469be2ac88cSJonathan Lemon 		rt = tcp_rtlookup6(&inp->inp_inc);
2470fb59c426SYoshinobu Inoue 	else
2471fb59c426SYoshinobu Inoue #endif
2472be2ac88cSJonathan Lemon 	rt = tcp_rtlookup(&inp->inp_inc);
2473fb59c426SYoshinobu Inoue 	if (rt == NULL) {
2474fb59c426SYoshinobu Inoue 		tp->t_maxopd = tp->t_maxseg =
2475fb59c426SYoshinobu Inoue #ifdef INET6
2476fb59c426SYoshinobu Inoue 		isipv6 ? tcp_v6mssdflt :
2477fb59c426SYoshinobu Inoue #endif /* INET6 */
2478fb59c426SYoshinobu Inoue 		tcp_mssdflt;
2479a0292f23SGarrett Wollman 		return;
2480df8bae1dSRodney W. Grimes 	}
2481df8bae1dSRodney W. Grimes 	ifp = rt->rt_ifp;
2482df8bae1dSRodney W. Grimes 	so = inp->inp_socket;
2483df8bae1dSRodney W. Grimes 
2484a0292f23SGarrett Wollman 	taop = rmx_taop(rt->rt_rmx);
2485a0292f23SGarrett Wollman 	/*
2486a0292f23SGarrett Wollman 	 * Offer == -1 means that we didn't receive SYN yet,
2487a0292f23SGarrett Wollman 	 * use cached value in that case;
2488a0292f23SGarrett Wollman 	 */
2489a0292f23SGarrett Wollman 	if (offer == -1)
2490a0292f23SGarrett Wollman 		offer = taop->tao_mssopt;
2491a0292f23SGarrett Wollman 	/*
2492a0292f23SGarrett Wollman 	 * Offer == 0 means that there was no MSS on the SYN segment,
2493a0292f23SGarrett Wollman 	 * in this case we use tcp_mssdflt.
2494a0292f23SGarrett Wollman 	 */
2495a0292f23SGarrett Wollman 	if (offer == 0)
2496fb59c426SYoshinobu Inoue 		offer =
2497fb59c426SYoshinobu Inoue #ifdef INET6
2498fb59c426SYoshinobu Inoue 			isipv6 ? tcp_v6mssdflt :
2499fb59c426SYoshinobu Inoue #endif /* INET6 */
2500fb59c426SYoshinobu Inoue 			tcp_mssdflt;
2501a0292f23SGarrett Wollman 	else
2502a0292f23SGarrett Wollman 		/*
2503a0292f23SGarrett Wollman 		 * Sanity check: make sure that maxopd will be large
2504a0292f23SGarrett Wollman 		 * enough to allow some data on segments even is the
2505a0292f23SGarrett Wollman 		 * all the option space is used (40bytes).  Otherwise
2506a0292f23SGarrett Wollman 		 * funny things may happen in tcp_output.
2507a0292f23SGarrett Wollman 		 */
2508a0292f23SGarrett Wollman 		offer = max(offer, 64);
2509a0292f23SGarrett Wollman 	taop->tao_mssopt = offer;
2510a0292f23SGarrett Wollman 
2511df8bae1dSRodney W. Grimes 	/*
2512df8bae1dSRodney W. Grimes 	 * While we're here, check if there's an initial rtt
2513df8bae1dSRodney W. Grimes 	 * or rttvar.  Convert from the route-table units
2514df8bae1dSRodney W. Grimes 	 * to scaled multiples of the slow timeout timer.
2515df8bae1dSRodney W. Grimes 	 */
2516df8bae1dSRodney W. Grimes 	if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
2517df8bae1dSRodney W. Grimes 		/*
2518a0292f23SGarrett Wollman 		 * XXX the lock bit for RTT indicates that the value
2519df8bae1dSRodney W. Grimes 		 * is also a minimum value; this is subject to time.
2520df8bae1dSRodney W. Grimes 		 */
2521df8bae1dSRodney W. Grimes 		if (rt->rt_rmx.rmx_locks & RTV_RTT)
25229b8b58e0SJonathan Lemon 			tp->t_rttmin = rtt / (RTM_RTTUNIT / hz);
25239b8b58e0SJonathan Lemon 		tp->t_srtt = rtt / (RTM_RTTUNIT / (hz * TCP_RTT_SCALE));
2524dd224982SGarrett Wollman 		tcpstat.tcps_usedrtt++;
2525dd224982SGarrett Wollman 		if (rt->rt_rmx.rmx_rttvar) {
2526df8bae1dSRodney W. Grimes 			tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
25279b8b58e0SJonathan Lemon 			    (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE));
2528dd224982SGarrett Wollman 			tcpstat.tcps_usedrttvar++;
2529dd224982SGarrett Wollman 		} else {
2530df8bae1dSRodney W. Grimes 			/* default variation is +- 1 rtt */
2531df8bae1dSRodney W. Grimes 			tp->t_rttvar =
2532df8bae1dSRodney W. Grimes 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
2533dd224982SGarrett Wollman 		}
2534df8bae1dSRodney W. Grimes 		TCPT_RANGESET(tp->t_rxtcur,
2535df8bae1dSRodney W. Grimes 			      ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
2536df8bae1dSRodney W. Grimes 			      tp->t_rttmin, TCPTV_REXMTMAX);
2537df8bae1dSRodney W. Grimes 	}
2538df8bae1dSRodney W. Grimes 	/*
2539df8bae1dSRodney W. Grimes 	 * if there's an mtu associated with the route, use it
2540fb59c426SYoshinobu Inoue 	 * else, use the link mtu.
2541df8bae1dSRodney W. Grimes 	 */
2542df8bae1dSRodney W. Grimes 	if (rt->rt_rmx.rmx_mtu)
2543fb59c426SYoshinobu Inoue 		mss = rt->rt_rmx.rmx_mtu - min_protoh;
2544df8bae1dSRodney W. Grimes 	else
2545df8bae1dSRodney W. Grimes 	{
2546fb59c426SYoshinobu Inoue 		mss =
2547fb59c426SYoshinobu Inoue #ifdef INET6
2548fb59c426SYoshinobu Inoue 			(isipv6 ? nd_ifinfo[rt->rt_ifp->if_index].linkmtu :
2549fb59c426SYoshinobu Inoue #endif
2550fb59c426SYoshinobu Inoue 			 ifp->if_mtu
2551fb59c426SYoshinobu Inoue #ifdef INET6
2552fb59c426SYoshinobu Inoue 			 )
2553fb59c426SYoshinobu Inoue #endif
2554fb59c426SYoshinobu Inoue 			- min_protoh;
2555fb59c426SYoshinobu Inoue #ifdef INET6
2556fb59c426SYoshinobu Inoue 		if (isipv6) {
2557fb59c426SYoshinobu Inoue 			if (!in6_localaddr(&inp->in6p_faddr))
2558fb59c426SYoshinobu Inoue 				mss = min(mss, tcp_v6mssdflt);
2559fb59c426SYoshinobu Inoue 		} else
2560fb59c426SYoshinobu Inoue #endif
2561a0292f23SGarrett Wollman 		if (!in_localaddr(inp->inp_faddr))
2562a0292f23SGarrett Wollman 			mss = min(mss, tcp_mssdflt);
2563a0292f23SGarrett Wollman 	}
2564a0292f23SGarrett Wollman 	mss = min(mss, offer);
2565a0292f23SGarrett Wollman 	/*
2566a0292f23SGarrett Wollman 	 * maxopd stores the maximum length of data AND options
2567a0292f23SGarrett Wollman 	 * in a segment; maxseg is the amount of data in a normal
2568a0292f23SGarrett Wollman 	 * segment.  We need to store this value (maxopd) apart
2569a0292f23SGarrett Wollman 	 * from maxseg, because now every segment carries options
2570a0292f23SGarrett Wollman 	 * and thus we normally have somewhat less data in segments.
2571a0292f23SGarrett Wollman 	 */
2572a0292f23SGarrett Wollman 	tp->t_maxopd = mss;
2573a0292f23SGarrett Wollman 
2574a0292f23SGarrett Wollman 	/*
2575a0292f23SGarrett Wollman 	 * In case of T/TCP, origoffer==-1 indicates, that no segments
2576a0292f23SGarrett Wollman 	 * were received yet.  In this case we just guess, otherwise
2577a0292f23SGarrett Wollman 	 * we do the same as before T/TCP.
2578a0292f23SGarrett Wollman 	 */
2579a0292f23SGarrett Wollman  	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
2580a0292f23SGarrett Wollman 	    (origoffer == -1 ||
2581a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
2582a0292f23SGarrett Wollman 		mss -= TCPOLEN_TSTAMP_APPA;
2583a0292f23SGarrett Wollman  	if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
2584a0292f23SGarrett Wollman 	    (origoffer == -1 ||
2585a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC))
2586a0292f23SGarrett Wollman 		mss -= TCPOLEN_CC_APPA;
2587a0292f23SGarrett Wollman 
2588df8bae1dSRodney W. Grimes #if	(MCLBYTES & (MCLBYTES - 1)) == 0
2589df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
2590df8bae1dSRodney W. Grimes 			mss &= ~(MCLBYTES-1);
2591df8bae1dSRodney W. Grimes #else
2592df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
2593df8bae1dSRodney W. Grimes 			mss = mss / MCLBYTES * MCLBYTES;
2594df8bae1dSRodney W. Grimes #endif
2595df8bae1dSRodney W. Grimes 	/*
2596df8bae1dSRodney W. Grimes 	 * If there's a pipesize, change the socket buffer
2597df8bae1dSRodney W. Grimes 	 * to that size.  Make the socket buffers an integral
2598df8bae1dSRodney W. Grimes 	 * number of mss units; if the mss is larger than
2599df8bae1dSRodney W. Grimes 	 * the socket buffer, decrease the mss.
2600df8bae1dSRodney W. Grimes 	 */
2601df8bae1dSRodney W. Grimes #ifdef RTV_SPIPE
2602df8bae1dSRodney W. Grimes 	if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
2603df8bae1dSRodney W. Grimes #endif
2604df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
2605df8bae1dSRodney W. Grimes 	if (bufsize < mss)
2606df8bae1dSRodney W. Grimes 		mss = bufsize;
2607df8bae1dSRodney W. Grimes 	else {
2608df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
2609df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
2610df8bae1dSRodney W. Grimes 			bufsize = sb_max;
2611ecf72308SBrian Feldman 		(void)sbreserve(&so->so_snd, bufsize, so, NULL);
2612df8bae1dSRodney W. Grimes 	}
2613df8bae1dSRodney W. Grimes 	tp->t_maxseg = mss;
2614df8bae1dSRodney W. Grimes 
2615df8bae1dSRodney W. Grimes #ifdef RTV_RPIPE
2616df8bae1dSRodney W. Grimes 	if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
2617df8bae1dSRodney W. Grimes #endif
2618df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
2619df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
2620df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
2621df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
2622df8bae1dSRodney W. Grimes 			bufsize = sb_max;
2623ecf72308SBrian Feldman 		(void)sbreserve(&so->so_rcv, bufsize, so, NULL);
2624df8bae1dSRodney W. Grimes 	}
26259b8b58e0SJonathan Lemon 
2626a0292f23SGarrett Wollman 	/*
26279b8b58e0SJonathan Lemon 	 * Set the slow-start flight size depending on whether this
26289b8b58e0SJonathan Lemon 	 * is a local network or not.
2629a0292f23SGarrett Wollman 	 */
2630fb59c426SYoshinobu Inoue 	if (
2631fb59c426SYoshinobu Inoue #ifdef INET6
2632fb59c426SYoshinobu Inoue 	    (isipv6 && in6_localaddr(&inp->in6p_faddr)) ||
2633fb59c426SYoshinobu Inoue 	    (!isipv6 &&
2634fb59c426SYoshinobu Inoue #endif
2635fb59c426SYoshinobu Inoue 	     in_localaddr(inp->inp_faddr)
2636fb59c426SYoshinobu Inoue #ifdef INET6
2637fb59c426SYoshinobu Inoue 	     )
2638fb59c426SYoshinobu Inoue #endif
2639fb59c426SYoshinobu Inoue 	    )
26409b8b58e0SJonathan Lemon 		tp->snd_cwnd = mss * ss_fltsz_local;
26419b8b58e0SJonathan Lemon 	else
26429b8b58e0SJonathan Lemon 		tp->snd_cwnd = mss * ss_fltsz;
2643df8bae1dSRodney W. Grimes 
2644df8bae1dSRodney W. Grimes 	if (rt->rt_rmx.rmx_ssthresh) {
2645df8bae1dSRodney W. Grimes 		/*
2646df8bae1dSRodney W. Grimes 		 * There's some sort of gateway or interface
2647df8bae1dSRodney W. Grimes 		 * buffer limit on the path.  Use this to set
2648df8bae1dSRodney W. Grimes 		 * the slow start threshhold, but set the
2649df8bae1dSRodney W. Grimes 		 * threshold to no less than 2*mss.
2650df8bae1dSRodney W. Grimes 		 */
2651df8bae1dSRodney W. Grimes 		tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
2652dd224982SGarrett Wollman 		tcpstat.tcps_usedssthresh++;
2653df8bae1dSRodney W. Grimes 	}
2654a0292f23SGarrett Wollman }
2655a0292f23SGarrett Wollman 
2656a0292f23SGarrett Wollman /*
2657a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
2658a0292f23SGarrett Wollman  */
2659a0292f23SGarrett Wollman int
2660a0292f23SGarrett Wollman tcp_mssopt(tp)
2661a0292f23SGarrett Wollman 	struct tcpcb *tp;
2662a0292f23SGarrett Wollman {
2663a0292f23SGarrett Wollman 	struct rtentry *rt;
2664fb59c426SYoshinobu Inoue #ifdef INET6
2665fb59c426SYoshinobu Inoue 	int isipv6;
2666fb59c426SYoshinobu Inoue 	int min_protoh;
2667fb59c426SYoshinobu Inoue #endif
2668a0292f23SGarrett Wollman 
2669fb59c426SYoshinobu Inoue #ifdef INET6
2670fb59c426SYoshinobu Inoue 	isipv6 = ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
2671fb59c426SYoshinobu Inoue 	min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr)
2672fb59c426SYoshinobu Inoue 			    : sizeof (struct tcpiphdr);
2673fb59c426SYoshinobu Inoue #else
2674fb59c426SYoshinobu Inoue #define min_protoh  (sizeof (struct tcpiphdr))
2675fb59c426SYoshinobu Inoue #endif
2676fb59c426SYoshinobu Inoue #ifdef INET6
2677fb59c426SYoshinobu Inoue 	if (isipv6)
2678be2ac88cSJonathan Lemon 		rt = tcp_rtlookup6(&tp->t_inpcb->inp_inc);
2679fb59c426SYoshinobu Inoue 	else
2680fb59c426SYoshinobu Inoue #endif /* INET6 */
2681be2ac88cSJonathan Lemon 	rt = tcp_rtlookup(&tp->t_inpcb->inp_inc);
2682a0292f23SGarrett Wollman 	if (rt == NULL)
2683fb59c426SYoshinobu Inoue 		return
2684fb59c426SYoshinobu Inoue #ifdef INET6
2685fb59c426SYoshinobu Inoue 			isipv6 ? tcp_v6mssdflt :
2686fb59c426SYoshinobu Inoue #endif /* INET6 */
2687fb59c426SYoshinobu Inoue 			tcp_mssdflt;
2688a0292f23SGarrett Wollman 
2689fb59c426SYoshinobu Inoue 	return rt->rt_ifp->if_mtu - min_protoh;
2690df8bae1dSRodney W. Grimes }
269146f58482SJonathan Lemon 
269246f58482SJonathan Lemon 
269346f58482SJonathan Lemon /*
269446f58482SJonathan Lemon  * Checks for partial ack.  If partial ack arrives, force the retransmission
269546f58482SJonathan Lemon  * of the next unacknowledged segment, do not clear tp->t_dupacks, and return
269646f58482SJonathan Lemon  * 1.  By setting snd_nxt to ti_ack, this forces retransmission timer to
269746f58482SJonathan Lemon  * be started again.  If the ack advances at least to tp->snd_recover, return 0.
269846f58482SJonathan Lemon  */
269946f58482SJonathan Lemon static int
270046f58482SJonathan Lemon tcp_newreno(tp, th)
270146f58482SJonathan Lemon 	struct tcpcb *tp;
270246f58482SJonathan Lemon 	struct tcphdr *th;
270346f58482SJonathan Lemon {
270446f58482SJonathan Lemon 	if (SEQ_LT(th->th_ack, tp->snd_recover)) {
270546f58482SJonathan Lemon 		tcp_seq onxt = tp->snd_nxt;
270646f58482SJonathan Lemon 		u_long  ocwnd = tp->snd_cwnd;
270746f58482SJonathan Lemon 
270846f58482SJonathan Lemon 		callout_stop(tp->tt_rexmt);
270946f58482SJonathan Lemon 		tp->t_rtttime = 0;
271046f58482SJonathan Lemon 		tp->snd_nxt = th->th_ack;
27116b2a5f92SJayanth Vijayaraghavan 		/*
27126b2a5f92SJayanth Vijayaraghavan 		 * Set snd_cwnd to one segment beyond acknowledged offset
27136b2a5f92SJayanth Vijayaraghavan 		 * (tp->snd_una has not yet been updated when this function
27146b2a5f92SJayanth Vijayaraghavan 		 *  is called)
27156b2a5f92SJayanth Vijayaraghavan 		 */
27166b2a5f92SJayanth Vijayaraghavan 		tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una);
27176b2a5f92SJayanth Vijayaraghavan 		(void) tcp_output(tp);
271846f58482SJonathan Lemon 		tp->snd_cwnd = ocwnd;
271946f58482SJonathan Lemon 		if (SEQ_GT(onxt, tp->snd_nxt))
272046f58482SJonathan Lemon 			tp->snd_nxt = onxt;
272146f58482SJonathan Lemon 		/*
272246f58482SJonathan Lemon 		 * Partial window deflation.  Relies on fact that tp->snd_una
272346f58482SJonathan Lemon 		 * not updated yet.
272446f58482SJonathan Lemon 		 */
272546f58482SJonathan Lemon 		tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg);
272646f58482SJonathan Lemon 		return (1);
272746f58482SJonathan Lemon 	}
272846f58482SJonathan Lemon 	return (0);
272946f58482SJonathan Lemon }
2730