xref: /freebsd/sys/netinet/tcp_input.c (revision 33841545909f4a4ee94aa148b3a9cbcdc1abb02a)
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_seq	tcp_iss;
1052f96f1f4SGarrett Wollman tcp_cc	tcp_ccgen;
1060312fbe9SPoul-Henning Kamp 
1072f96f1f4SGarrett Wollman struct	tcpstat tcpstat;
1083d177f46SBill Fumerola SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RD,
1093d177f46SBill Fumerola     &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
1100312fbe9SPoul-Henning Kamp 
111d78a37adSPaul Traina static int log_in_vain = 0;
112816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
1133d177f46SBill Fumerola     &log_in_vain, 0, "Log all incoming TCP connections");
114816a3d83SPoul-Henning Kamp 
11516f7f31fSGeoff Rehmet static int blackhole = 0;
11616f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW,
11716f7f31fSGeoff Rehmet 	&blackhole, 0, "Do not send RST when dropping refused connections");
11816f7f31fSGeoff Rehmet 
119f498eeeeSDavid Greenman int tcp_delack_enabled = 1;
12084e33c9eSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW,
1213d177f46SBill Fumerola     &tcp_delack_enabled, 0,
1223d177f46SBill Fumerola     "Delay ACK to try and piggyback it onto a data packet");
123f498eeeeSDavid Greenman 
124e7f32693SJayanth Vijayaraghavan int tcp_lq_overflow = 1;
125e7f32693SJayanth Vijayaraghavan SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcp_lq_overflow, CTLFLAG_RW,
126e7f32693SJayanth Vijayaraghavan     &tcp_lq_overflow, 0,
127e7f32693SJayanth Vijayaraghavan     "Listen Queue Overflow");
128e7f32693SJayanth Vijayaraghavan 
129f8613305SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN
130f8613305SDag-Erling Smørgrav static int drop_synfin = 0;
131f8613305SDag-Erling Smørgrav SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW,
132f8613305SDag-Erling Smørgrav     &drop_synfin, 0, "Drop TCP packets with SYN+FIN set");
133f8613305SDag-Erling Smørgrav #endif
134f8613305SDag-Erling Smørgrav 
13515bd2b43SDavid Greenman struct inpcbhead tcb;
136fb59c426SYoshinobu Inoue #define	tcb6	tcb  /* for KAME src sync over BSD*'s */
13715bd2b43SDavid Greenman struct inpcbinfo tcbinfo;
138df8bae1dSRodney W. Grimes 
1390312fbe9SPoul-Henning Kamp static void	 tcp_dooptions __P((struct tcpcb *,
140fb59c426SYoshinobu Inoue 	    u_char *, int, struct tcphdr *, struct tcpopt *));
1410312fbe9SPoul-Henning Kamp static void	 tcp_pulloutofband __P((struct socket *,
142fb59c426SYoshinobu Inoue 	    struct tcphdr *, struct mbuf *, int));
143fb59c426SYoshinobu Inoue static int	 tcp_reass __P((struct tcpcb *, struct tcphdr *, int *,
144fb59c426SYoshinobu Inoue 				struct mbuf *));
1450312fbe9SPoul-Henning Kamp static void	 tcp_xmit_timer __P((struct tcpcb *, int));
14646f58482SJonathan Lemon static int	 tcp_newreno __P((struct tcpcb *, struct tcphdr *));
1470312fbe9SPoul-Henning Kamp 
148fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
149fb59c426SYoshinobu Inoue #ifdef INET6
150fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \
151fb59c426SYoshinobu Inoue do { \
152fb59c426SYoshinobu Inoue 	if ((tp) && (tp)->t_inpcb && \
153fb59c426SYoshinobu Inoue 	    ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0 && \
154fb59c426SYoshinobu Inoue 	    (tp)->t_inpcb->in6p_route.ro_rt) \
155686cdd19SJun-ichiro itojun Hagino 		nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL, 0); \
156fb59c426SYoshinobu Inoue } while (0)
157fb59c426SYoshinobu Inoue #else
158fb59c426SYoshinobu Inoue #define ND6_HINT(tp)
159fb59c426SYoshinobu Inoue #endif
160df8bae1dSRodney W. Grimes 
161df8bae1dSRodney W. Grimes /*
162d8c85a26SJonathan Lemon  * Indicate whether this ack should be delayed.
163d8c85a26SJonathan Lemon  */
164d8c85a26SJonathan Lemon #define DELAY_ACK(tp) \
165d8c85a26SJonathan Lemon 	(tcp_delack_enabled && !callout_pending(tp->tt_delack))
166d8c85a26SJonathan Lemon 
1670312fbe9SPoul-Henning Kamp static int
168fb59c426SYoshinobu Inoue tcp_reass(tp, th, tlenp, m)
169df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
170fb59c426SYoshinobu Inoue 	register struct tcphdr *th;
171fb59c426SYoshinobu Inoue 	int *tlenp;
172df8bae1dSRodney W. Grimes 	struct mbuf *m;
173df8bae1dSRodney W. Grimes {
174fb59c426SYoshinobu Inoue 	struct tseg_qent *q;
175fb59c426SYoshinobu Inoue 	struct tseg_qent *p = NULL;
176fb59c426SYoshinobu Inoue 	struct tseg_qent *nq;
177fb59c426SYoshinobu Inoue 	struct tseg_qent *te;
178df8bae1dSRodney W. Grimes 	struct socket *so = tp->t_inpcb->inp_socket;
179df8bae1dSRodney W. Grimes 	int flags;
180df8bae1dSRodney W. Grimes 
181df8bae1dSRodney W. Grimes 	/*
182fb59c426SYoshinobu Inoue 	 * Call with th==0 after become established to
183df8bae1dSRodney W. Grimes 	 * force pre-ESTABLISHED data up to user socket.
184df8bae1dSRodney W. Grimes 	 */
185fb59c426SYoshinobu Inoue 	if (th == 0)
186df8bae1dSRodney W. Grimes 		goto present;
187df8bae1dSRodney W. Grimes 
188fb59c426SYoshinobu Inoue 	/* Allocate a new queue entry. If we can't, just drop the pkt. XXX */
189fb59c426SYoshinobu Inoue 	MALLOC(te, struct tseg_qent *, sizeof (struct tseg_qent), M_TSEGQ,
190fb59c426SYoshinobu Inoue 	       M_NOWAIT);
191fb59c426SYoshinobu Inoue 	if (te == NULL) {
192fb59c426SYoshinobu Inoue 		tcpstat.tcps_rcvmemdrop++;
193fb59c426SYoshinobu Inoue 		m_freem(m);
194fb59c426SYoshinobu Inoue 		return (0);
195fb59c426SYoshinobu Inoue 	}
1966effc713SDoug Rabson 
197df8bae1dSRodney W. Grimes 	/*
198df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
199df8bae1dSRodney W. Grimes 	 */
200fb59c426SYoshinobu Inoue 	LIST_FOREACH(q, &tp->t_segq, tqe_q) {
201fb59c426SYoshinobu Inoue 		if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
202df8bae1dSRodney W. Grimes 			break;
203fb59c426SYoshinobu Inoue 		p = q;
204fb59c426SYoshinobu Inoue 	}
205df8bae1dSRodney W. Grimes 
206df8bae1dSRodney W. Grimes 	/*
207df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
208df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
209df8bae1dSRodney W. Grimes 	 * segment.  If it provides all of our data, drop us.
210df8bae1dSRodney W. Grimes 	 */
2116effc713SDoug Rabson 	if (p != NULL) {
212df8bae1dSRodney W. Grimes 		register int i;
213df8bae1dSRodney W. Grimes 		/* conversion to int (in i) handles seq wraparound */
214fb59c426SYoshinobu Inoue 		i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
215df8bae1dSRodney W. Grimes 		if (i > 0) {
216fb59c426SYoshinobu Inoue 			if (i >= *tlenp) {
217df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvduppack++;
218fb59c426SYoshinobu Inoue 				tcpstat.tcps_rcvdupbyte += *tlenp;
219df8bae1dSRodney W. Grimes 				m_freem(m);
220fb59c426SYoshinobu Inoue 				FREE(te, M_TSEGQ);
221a0292f23SGarrett Wollman 				/*
222a0292f23SGarrett Wollman 				 * Try to present any queued data
223a0292f23SGarrett Wollman 				 * at the left window edge to the user.
224a0292f23SGarrett Wollman 				 * This is needed after the 3-WHS
225a0292f23SGarrett Wollman 				 * completes.
226a0292f23SGarrett Wollman 				 */
227a0292f23SGarrett Wollman 				goto present;	/* ??? */
228df8bae1dSRodney W. Grimes 			}
229df8bae1dSRodney W. Grimes 			m_adj(m, i);
230fb59c426SYoshinobu Inoue 			*tlenp -= i;
231fb59c426SYoshinobu Inoue 			th->th_seq += i;
232df8bae1dSRodney W. Grimes 		}
233df8bae1dSRodney W. Grimes 	}
234df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvoopack++;
235fb59c426SYoshinobu Inoue 	tcpstat.tcps_rcvoobyte += *tlenp;
236df8bae1dSRodney W. Grimes 
237df8bae1dSRodney W. Grimes 	/*
238df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
239df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
240df8bae1dSRodney W. Grimes 	 */
2416effc713SDoug Rabson 	while (q) {
242fb59c426SYoshinobu Inoue 		register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
243df8bae1dSRodney W. Grimes 		if (i <= 0)
244df8bae1dSRodney W. Grimes 			break;
245fb59c426SYoshinobu Inoue 		if (i < q->tqe_len) {
246fb59c426SYoshinobu Inoue 			q->tqe_th->th_seq += i;
247fb59c426SYoshinobu Inoue 			q->tqe_len -= i;
248fb59c426SYoshinobu Inoue 			m_adj(q->tqe_m, i);
249df8bae1dSRodney W. Grimes 			break;
250df8bae1dSRodney W. Grimes 		}
2516effc713SDoug Rabson 
252fb59c426SYoshinobu Inoue 		nq = LIST_NEXT(q, tqe_q);
253fb59c426SYoshinobu Inoue 		LIST_REMOVE(q, tqe_q);
254fb59c426SYoshinobu Inoue 		m_freem(q->tqe_m);
255fb59c426SYoshinobu Inoue 		FREE(q, M_TSEGQ);
2566effc713SDoug Rabson 		q = nq;
257df8bae1dSRodney W. Grimes 	}
258df8bae1dSRodney W. Grimes 
259fb59c426SYoshinobu Inoue 	/* Insert the new segment queue entry into place. */
260fb59c426SYoshinobu Inoue 	te->tqe_m = m;
261fb59c426SYoshinobu Inoue 	te->tqe_th = th;
262fb59c426SYoshinobu Inoue 	te->tqe_len = *tlenp;
263fb59c426SYoshinobu Inoue 
2646effc713SDoug Rabson 	if (p == NULL) {
265fb59c426SYoshinobu Inoue 		LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
2666effc713SDoug Rabson 	} else {
267fb59c426SYoshinobu Inoue 		LIST_INSERT_AFTER(p, te, tqe_q);
2686effc713SDoug Rabson 	}
269df8bae1dSRodney W. Grimes 
270df8bae1dSRodney W. Grimes present:
271df8bae1dSRodney W. Grimes 	/*
272df8bae1dSRodney W. Grimes 	 * Present data to user, advancing rcv_nxt through
273df8bae1dSRodney W. Grimes 	 * completed sequence space.
274df8bae1dSRodney W. Grimes 	 */
275a0292f23SGarrett Wollman 	if (!TCPS_HAVEESTABLISHED(tp->t_state))
276df8bae1dSRodney W. Grimes 		return (0);
277fb59c426SYoshinobu Inoue 	q = LIST_FIRST(&tp->t_segq);
278fb59c426SYoshinobu Inoue 	if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
279df8bae1dSRodney W. Grimes 		return (0);
280df8bae1dSRodney W. Grimes 	do {
281fb59c426SYoshinobu Inoue 		tp->rcv_nxt += q->tqe_len;
282fb59c426SYoshinobu Inoue 		flags = q->tqe_th->th_flags & TH_FIN;
283fb59c426SYoshinobu Inoue 		nq = LIST_NEXT(q, tqe_q);
284fb59c426SYoshinobu Inoue 		LIST_REMOVE(q, tqe_q);
285df8bae1dSRodney W. Grimes 		if (so->so_state & SS_CANTRCVMORE)
286fb59c426SYoshinobu Inoue 			m_freem(q->tqe_m);
287df8bae1dSRodney W. Grimes 		else
288fb59c426SYoshinobu Inoue 			sbappend(&so->so_rcv, q->tqe_m);
289fb59c426SYoshinobu Inoue 		FREE(q, M_TSEGQ);
2906effc713SDoug Rabson 		q = nq;
291fb59c426SYoshinobu Inoue 	} while (q && q->tqe_th->th_seq == tp->rcv_nxt);
292fb59c426SYoshinobu Inoue 	ND6_HINT(tp);
293df8bae1dSRodney W. Grimes 	sorwakeup(so);
294df8bae1dSRodney W. Grimes 	return (flags);
295df8bae1dSRodney W. Grimes }
296df8bae1dSRodney W. Grimes 
297df8bae1dSRodney W. Grimes /*
298df8bae1dSRodney W. Grimes  * TCP input routine, follows pages 65-76 of the
299df8bae1dSRodney W. Grimes  * protocol specification dated September, 1981 very closely.
300df8bae1dSRodney W. Grimes  */
301fb59c426SYoshinobu Inoue #ifdef INET6
302fb59c426SYoshinobu Inoue int
303fb59c426SYoshinobu Inoue tcp6_input(mp, offp, proto)
304fb59c426SYoshinobu Inoue 	struct mbuf **mp;
305fb59c426SYoshinobu Inoue 	int *offp, proto;
306fb59c426SYoshinobu Inoue {
307fb59c426SYoshinobu Inoue 	register struct mbuf *m = *mp;
30833841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
309fb59c426SYoshinobu Inoue 
310fb59c426SYoshinobu Inoue 	IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
311fb59c426SYoshinobu Inoue 
312fb59c426SYoshinobu Inoue 	/*
313fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
314fb59c426SYoshinobu Inoue 	 * better place to put this in?
315fb59c426SYoshinobu Inoue 	 */
31633841545SHajimu UMEMOTO 	ia6 = ip6_getdstifaddr(m);
31733841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
318fb59c426SYoshinobu Inoue 		struct ip6_hdr *ip6;
319fb59c426SYoshinobu Inoue 
320fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
321fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
322fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
323fb59c426SYoshinobu Inoue 		return IPPROTO_DONE;
324fb59c426SYoshinobu Inoue 	}
325fb59c426SYoshinobu Inoue 
326fb59c426SYoshinobu Inoue 	tcp_input(m, *offp, proto);
327fb59c426SYoshinobu Inoue 	return IPPROTO_DONE;
328fb59c426SYoshinobu Inoue }
329fb59c426SYoshinobu Inoue #endif
330fb59c426SYoshinobu Inoue 
331df8bae1dSRodney W. Grimes void
3326a800098SYoshinobu Inoue tcp_input(m, off0, proto)
333df8bae1dSRodney W. Grimes 	register struct mbuf *m;
3346a800098SYoshinobu Inoue 	int off0, proto;
335df8bae1dSRodney W. Grimes {
336fb59c426SYoshinobu Inoue 	register struct tcphdr *th;
337fb59c426SYoshinobu Inoue 	register struct ip *ip = NULL;
338fb59c426SYoshinobu Inoue 	register struct ipovly *ipov;
339df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
340e79adb8eSGarrett Wollman 	u_char *optp = NULL;
34126f9a767SRodney W. Grimes 	int optlen = 0;
342df8bae1dSRodney W. Grimes 	int len, tlen, off;
343fb59c426SYoshinobu Inoue 	int drop_hdrlen;
344df8bae1dSRodney W. Grimes 	register struct tcpcb *tp = 0;
345fb59c426SYoshinobu Inoue 	register int thflags;
34626f9a767SRodney W. Grimes 	struct socket *so = 0;
347df8bae1dSRodney W. Grimes 	int todrop, acked, ourfinisacked, needoutput = 0;
348df8bae1dSRodney W. Grimes 	struct in_addr laddr;
349fb59c426SYoshinobu Inoue #ifdef INET6
350fb59c426SYoshinobu Inoue 	struct in6_addr laddr6;
351fb59c426SYoshinobu Inoue #endif
352df8bae1dSRodney W. Grimes 	int dropsocket = 0;
353df8bae1dSRodney W. Grimes 	int iss = 0;
354a0292f23SGarrett Wollman 	u_long tiwin;
355a0292f23SGarrett Wollman 	struct tcpopt to;		/* options in this segment */
356a0292f23SGarrett Wollman 	struct rmxp_tao *taop;		/* pointer to our TAO cache entry */
357a0292f23SGarrett Wollman 	struct rmxp_tao	tao_noncached;	/* in case there's no cached entry */
358610ee2f9SDavid Greenman #ifdef TCPDEBUG
359610ee2f9SDavid Greenman 	short ostate = 0;
360610ee2f9SDavid Greenman #endif
361fb59c426SYoshinobu Inoue #ifdef INET6
362fb59c426SYoshinobu Inoue 	struct ip6_hdr *ip6 = NULL;
363fb59c426SYoshinobu Inoue 	int isipv6;
364fb59c426SYoshinobu Inoue #endif /* INET6 */
365a57815efSBosko Milekic 	int rstreason; /* For badport_bandlim accounting purposes */
366df8bae1dSRodney W. Grimes 
367fb59c426SYoshinobu Inoue #ifdef INET6
368fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
369fb59c426SYoshinobu Inoue #endif
370a0292f23SGarrett Wollman 	bzero((char *)&to, sizeof(to));
371a0292f23SGarrett Wollman 
372df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvtotal++;
373fb59c426SYoshinobu Inoue 
374fb59c426SYoshinobu Inoue #ifdef INET6
375fb59c426SYoshinobu Inoue 	if (isipv6) {
376fb59c426SYoshinobu Inoue 		/* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
377fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
378fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
379fb59c426SYoshinobu Inoue 		if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) {
380fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbadsum++;
381fb59c426SYoshinobu Inoue 			goto drop;
382fb59c426SYoshinobu Inoue 		}
383fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
38433841545SHajimu UMEMOTO 
38533841545SHajimu UMEMOTO 		/*
38633841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
38733841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
38833841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
38933841545SHajimu UMEMOTO 		 *
39033841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
39133841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
39233841545SHajimu UMEMOTO 		 */
39333841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
39433841545SHajimu UMEMOTO 			/* XXX stat */
39533841545SHajimu UMEMOTO 			goto drop;
39633841545SHajimu UMEMOTO 		}
397fb59c426SYoshinobu Inoue 	} else
398fb59c426SYoshinobu Inoue #endif /* INET6 */
399fb59c426SYoshinobu Inoue       {
400df8bae1dSRodney W. Grimes 	/*
401df8bae1dSRodney W. Grimes 	 * Get IP and TCP header together in first mbuf.
402df8bae1dSRodney W. Grimes 	 * Note: IP leaves IP header in first mbuf.
403df8bae1dSRodney W. Grimes 	 */
404fb59c426SYoshinobu Inoue 	if (off0 > sizeof (struct ip)) {
405df8bae1dSRodney W. Grimes 		ip_stripoptions(m, (struct mbuf *)0);
406fb59c426SYoshinobu Inoue 		off0 = sizeof(struct ip);
407fb59c426SYoshinobu Inoue 	}
408df8bae1dSRodney W. Grimes 	if (m->m_len < sizeof (struct tcpiphdr)) {
409df8bae1dSRodney W. Grimes 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
410df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvshort++;
411df8bae1dSRodney W. Grimes 			return;
412df8bae1dSRodney W. Grimes 		}
413df8bae1dSRodney W. Grimes 	}
414fb59c426SYoshinobu Inoue 	ip = mtod(m, struct ip *);
415fb59c426SYoshinobu Inoue 	ipov = (struct ipovly *)ip;
416db4f9cc7SJonathan Lemon 	th = (struct tcphdr *)((caddr_t)ip + off0);
417db4f9cc7SJonathan Lemon 	tlen = ip->ip_len;
418df8bae1dSRodney W. Grimes 
419db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
420db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
421db4f9cc7SJonathan Lemon                 	th->th_sum = m->m_pkthdr.csum_data;
422db4f9cc7SJonathan Lemon 		else
423db4f9cc7SJonathan Lemon 	                th->th_sum = in_pseudo(ip->ip_src.s_addr,
424db4f9cc7SJonathan Lemon 			    ip->ip_dst.s_addr, htonl(m->m_pkthdr.csum_data +
425db4f9cc7SJonathan Lemon 			    ip->ip_len + IPPROTO_TCP));
426db4f9cc7SJonathan Lemon 		th->th_sum ^= 0xffff;
427db4f9cc7SJonathan Lemon 	} else {
428df8bae1dSRodney W. Grimes 		/*
429df8bae1dSRodney W. Grimes 		 * Checksum extended TCP header and data.
430df8bae1dSRodney W. Grimes 		 */
431df8bae1dSRodney W. Grimes 		len = sizeof (struct ip) + tlen;
432fb59c426SYoshinobu Inoue 		bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
433fb59c426SYoshinobu Inoue 		ipov->ih_len = (u_short)tlen;
434fb59c426SYoshinobu Inoue 		HTONS(ipov->ih_len);
435fb59c426SYoshinobu Inoue 		th->th_sum = in_cksum(m, len);
436db4f9cc7SJonathan Lemon 	}
437fb59c426SYoshinobu Inoue 	if (th->th_sum) {
438df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvbadsum++;
439df8bae1dSRodney W. Grimes 		goto drop;
440df8bae1dSRodney W. Grimes 	}
441fb59c426SYoshinobu Inoue #ifdef INET6
442fb59c426SYoshinobu Inoue 	/* Re-initialization for later version check */
443fb59c426SYoshinobu Inoue 	ip->ip_v = IPVERSION;
444fb59c426SYoshinobu Inoue #endif
445fb59c426SYoshinobu Inoue       }
446df8bae1dSRodney W. Grimes 
447df8bae1dSRodney W. Grimes 	/*
448df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
449df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
450df8bae1dSRodney W. Grimes 	 */
451fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
452df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
453df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvbadoff++;
454df8bae1dSRodney W. Grimes 		goto drop;
455df8bae1dSRodney W. Grimes 	}
456fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
457df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
458fb59c426SYoshinobu Inoue #ifdef INET6
459fb59c426SYoshinobu Inoue 		if (isipv6) {
460fb59c426SYoshinobu Inoue 			IP6_EXTHDR_CHECK(m, off0, off, );
461fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
462fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
463fb59c426SYoshinobu Inoue 		} else
464fb59c426SYoshinobu Inoue #endif /* INET6 */
465fb59c426SYoshinobu Inoue 	      {
466df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof(struct ip) + off) {
467df8bae1dSRodney W. Grimes 			if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
468df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvshort++;
469df8bae1dSRodney W. Grimes 				return;
470df8bae1dSRodney W. Grimes 			}
471fb59c426SYoshinobu Inoue 			ip = mtod(m, struct ip *);
472fb59c426SYoshinobu Inoue 			ipov = (struct ipovly *)ip;
473fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip + off0);
474fb59c426SYoshinobu Inoue 		}
475df8bae1dSRodney W. Grimes 	      }
476df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
477fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
478df8bae1dSRodney W. Grimes 	}
479fb59c426SYoshinobu Inoue 	thflags = th->th_flags;
480df8bae1dSRodney W. Grimes 
481e46cd3d4SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN
482e46cd3d4SDag-Erling Smørgrav 	/*
483e46cd3d4SDag-Erling Smørgrav 	 * If the drop_synfin option is enabled, drop all packets with
484e46cd3d4SDag-Erling Smørgrav 	 * both the SYN and FIN bits set. This prevents e.g. nmap from
485e46cd3d4SDag-Erling Smørgrav 	 * identifying the TCP/IP stack.
486e46cd3d4SDag-Erling Smørgrav 	 *
487a589a70eSGarrett Wollman 	 * This is a violation of the TCP specification.
488e46cd3d4SDag-Erling Smørgrav 	 */
489fb59c426SYoshinobu Inoue 	if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN))
490e46cd3d4SDag-Erling Smørgrav 		goto drop;
491e46cd3d4SDag-Erling Smørgrav #endif
492e46cd3d4SDag-Erling Smørgrav 
493df8bae1dSRodney W. Grimes 	/*
494df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
495df8bae1dSRodney W. Grimes 	 */
496fb59c426SYoshinobu Inoue 	NTOHL(th->th_seq);
497fb59c426SYoshinobu Inoue 	NTOHL(th->th_ack);
498fb59c426SYoshinobu Inoue 	NTOHS(th->th_win);
499fb59c426SYoshinobu Inoue 	NTOHS(th->th_urp);
500df8bae1dSRodney W. Grimes 
501df8bae1dSRodney W. Grimes 	/*
502fb59c426SYoshinobu Inoue 	 * Delay droping TCP, IP headers, IPv6 ext headers, and TCP options,
503fb59c426SYoshinobu Inoue 	 * until after ip6_savecontrol() is called and before other functions
504fb59c426SYoshinobu Inoue 	 * which don't want those proto headers.
505fb59c426SYoshinobu Inoue 	 * Because ip6_savecontrol() is going to parse the mbuf to
506fb59c426SYoshinobu Inoue 	 * search for data to be passed up to user-land, it wants mbuf
507fb59c426SYoshinobu Inoue 	 * parameters to be unchanged.
508755c1f07SAndras Olah 	 */
509fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
510755c1f07SAndras Olah 
511755c1f07SAndras Olah 	/*
512df8bae1dSRodney W. Grimes 	 * Locate pcb for segment.
513df8bae1dSRodney W. Grimes 	 */
514df8bae1dSRodney W. Grimes findpcb:
515f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD
516fb59c426SYoshinobu Inoue 	if (ip_fw_fwd_addr != NULL
517fb59c426SYoshinobu Inoue #ifdef INET6
518fb59c426SYoshinobu Inoue 	    && isipv6 == NULL /* IPv6 support is not yet */
519fb59c426SYoshinobu Inoue #endif /* INET6 */
520fb59c426SYoshinobu Inoue 	    ) {
521f9e354dfSJulian Elischer 		/*
522f9e354dfSJulian Elischer 		 * Diverted. Pretend to be the destination.
523f9e354dfSJulian Elischer 		 * already got one like this?
524f9e354dfSJulian Elischer 		 */
525fb59c426SYoshinobu Inoue 		inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport,
526fb59c426SYoshinobu Inoue 			ip->ip_dst, th->th_dport, 0, m->m_pkthdr.rcvif);
527f9e354dfSJulian Elischer 		if (!inp) {
528f9e354dfSJulian Elischer 			/*
529f9e354dfSJulian Elischer 			 * No, then it's new. Try find the ambushing socket
530f9e354dfSJulian Elischer 			 */
531f9e354dfSJulian Elischer 			if (!ip_fw_fwd_addr->sin_port) {
532fb59c426SYoshinobu Inoue 				inp = in_pcblookup_hash(&tcbinfo, ip->ip_src,
533fb59c426SYoshinobu Inoue 				    th->th_sport, ip_fw_fwd_addr->sin_addr,
534fb59c426SYoshinobu Inoue 				    th->th_dport, 1, m->m_pkthdr.rcvif);
535f9e354dfSJulian Elischer 			} else {
536f9e354dfSJulian Elischer 				inp = in_pcblookup_hash(&tcbinfo,
537fb59c426SYoshinobu Inoue 				    ip->ip_src, th->th_sport,
538f9e354dfSJulian Elischer 	    			    ip_fw_fwd_addr->sin_addr,
539cfa1ca9dSYoshinobu Inoue 				    ntohs(ip_fw_fwd_addr->sin_port), 1,
540cfa1ca9dSYoshinobu Inoue 				    m->m_pkthdr.rcvif);
541f9e354dfSJulian Elischer 			}
542f9e354dfSJulian Elischer 		}
543f9e354dfSJulian Elischer 		ip_fw_fwd_addr = NULL;
544f9e354dfSJulian Elischer 	} else
545f9e354dfSJulian Elischer #endif	/* IPFIREWALL_FORWARD */
546fb59c426SYoshinobu Inoue       {
547fb59c426SYoshinobu Inoue #ifdef INET6
548fb59c426SYoshinobu Inoue 	if (isipv6)
549fb59c426SYoshinobu Inoue 		inp = in6_pcblookup_hash(&tcbinfo, &ip6->ip6_src, th->th_sport,
550fb59c426SYoshinobu Inoue 					 &ip6->ip6_dst, th->th_dport, 1,
551fb59c426SYoshinobu Inoue 					 m->m_pkthdr.rcvif);
552fb59c426SYoshinobu Inoue 	else
553fb59c426SYoshinobu Inoue #endif /* INET6 */
554fb59c426SYoshinobu Inoue 	inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport,
555fb59c426SYoshinobu Inoue 	    ip->ip_dst, th->th_dport, 1, m->m_pkthdr.rcvif);
556fb59c426SYoshinobu Inoue       }
557f9e354dfSJulian Elischer 
558fb59c426SYoshinobu Inoue #ifdef IPSEC
559fb59c426SYoshinobu Inoue #ifdef INET6
560fb59c426SYoshinobu Inoue 	if (isipv6) {
561fb59c426SYoshinobu Inoue 		if (inp != NULL && ipsec6_in_reject_so(m, inp->inp_socket)) {
562fb59c426SYoshinobu Inoue 			ipsec6stat.in_polvio++;
563fb59c426SYoshinobu Inoue 			goto drop;
564fb59c426SYoshinobu Inoue 		}
565fb59c426SYoshinobu Inoue 	} else
566fb59c426SYoshinobu Inoue #endif /* INET6 */
567fb59c426SYoshinobu Inoue 	if (inp != NULL && ipsec4_in_reject_so(m, inp->inp_socket)) {
568fb59c426SYoshinobu Inoue 		ipsecstat.in_polvio++;
569fb59c426SYoshinobu Inoue 		goto drop;
570fb59c426SYoshinobu Inoue 	}
571fb59c426SYoshinobu Inoue #endif /*IPSEC*/
572df8bae1dSRodney W. Grimes 
573df8bae1dSRodney W. Grimes 	/*
574df8bae1dSRodney W. Grimes 	 * If the state is CLOSED (i.e., TCB does not exist) then
575df8bae1dSRodney W. Grimes 	 * all data in the incoming segment is discarded.
576df8bae1dSRodney W. Grimes 	 * If the TCB exists but is in CLOSED state, it is embryonic,
577df8bae1dSRodney W. Grimes 	 * but should either do a listen or a connect soon.
578df8bae1dSRodney W. Grimes 	 */
579816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
5802e4e1b4cSGeoff Rehmet 		if (log_in_vain) {
581fb59c426SYoshinobu Inoue #ifdef INET6
582fb59c426SYoshinobu Inoue 			char dbuf[INET6_ADDRSTRLEN], sbuf[INET6_ADDRSTRLEN];
583fb59c426SYoshinobu Inoue #else /* INET6 */
584fb59c426SYoshinobu Inoue 			char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"];
585fb59c426SYoshinobu Inoue #endif /* INET6 */
58675cfc95fSAndrey A. Chernov 
587fb59c426SYoshinobu Inoue #ifdef INET6
588fb59c426SYoshinobu Inoue 			if (isipv6) {
589fb59c426SYoshinobu Inoue 				strcpy(dbuf, ip6_sprintf(&ip6->ip6_dst));
590fb59c426SYoshinobu Inoue 				strcpy(sbuf, ip6_sprintf(&ip6->ip6_src));
591fb59c426SYoshinobu Inoue 			} else
592fb59c426SYoshinobu Inoue #endif
593fb59c426SYoshinobu Inoue 		      {
594fb59c426SYoshinobu Inoue 			strcpy(dbuf, inet_ntoa(ip->ip_dst));
595fb59c426SYoshinobu Inoue 			strcpy(sbuf, inet_ntoa(ip->ip_src));
596fb59c426SYoshinobu Inoue 		      }
5972e4e1b4cSGeoff Rehmet 			switch (log_in_vain) {
5982e4e1b4cSGeoff Rehmet 			case 1:
599fb59c426SYoshinobu Inoue 				if(thflags & TH_SYN)
600592071e8SBruce Evans 					log(LOG_INFO,
601592071e8SBruce Evans 			    		"Connection attempt to TCP %s:%d from %s:%d\n",
602fb59c426SYoshinobu Inoue 			    		dbuf, ntohs(th->th_dport),
603fb59c426SYoshinobu Inoue 					sbuf,
604fb59c426SYoshinobu Inoue 					ntohs(th->th_sport));
6052e4e1b4cSGeoff Rehmet 				break;
6062e4e1b4cSGeoff Rehmet 			case 2:
6072e4e1b4cSGeoff Rehmet 				log(LOG_INFO,
6082e4e1b4cSGeoff Rehmet 			    	"Connection attempt to TCP %s:%d from %s:%d flags:0x%x\n",
609fb59c426SYoshinobu Inoue 			    	dbuf, ntohs(th->th_dport), sbuf,
610fb59c426SYoshinobu Inoue 			    	ntohs(th->th_sport), thflags);
6112e4e1b4cSGeoff Rehmet 				break;
6122e4e1b4cSGeoff Rehmet 			default:
6132e4e1b4cSGeoff Rehmet 				break;
6142e4e1b4cSGeoff Rehmet 			}
61575cfc95fSAndrey A. Chernov 		}
6162e4e1b4cSGeoff Rehmet 		if (blackhole) {
6172e4e1b4cSGeoff Rehmet 			switch (blackhole) {
618828b7f40SGeoff Rehmet 			case 1:
619fb59c426SYoshinobu Inoue 				if (thflags & TH_SYN)
620828b7f40SGeoff Rehmet 					goto drop;
621828b7f40SGeoff Rehmet 				break;
622828b7f40SGeoff Rehmet 			case 2:
623828b7f40SGeoff Rehmet 				goto drop;
624828b7f40SGeoff Rehmet 			default:
625828b7f40SGeoff Rehmet 				goto drop;
6262e4e1b4cSGeoff Rehmet 			}
627828b7f40SGeoff Rehmet 		}
628a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
629a57815efSBosko Milekic 		goto dropwithreset;
630816a3d83SPoul-Henning Kamp 	}
631df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
63209f81a46SBosko Milekic 	if (tp == 0) {
633a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
634a57815efSBosko Milekic 		goto dropwithreset;
63509f81a46SBosko Milekic 	}
636df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_CLOSED)
637df8bae1dSRodney W. Grimes 		goto drop;
638df8bae1dSRodney W. Grimes 
639df8bae1dSRodney W. Grimes 	/* Unscale the window into a 32-bit value. */
640fb59c426SYoshinobu Inoue 	if ((thflags & TH_SYN) == 0)
641fb59c426SYoshinobu Inoue 		tiwin = th->th_win << tp->snd_scale;
642df8bae1dSRodney W. Grimes 	else
643fb59c426SYoshinobu Inoue 		tiwin = th->th_win;
644fb59c426SYoshinobu Inoue 
645df8bae1dSRodney W. Grimes 	so = inp->inp_socket;
646df8bae1dSRodney W. Grimes 	if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
647610ee2f9SDavid Greenman #ifdef TCPDEBUG
648df8bae1dSRodney W. Grimes 		if (so->so_options & SO_DEBUG) {
649df8bae1dSRodney W. Grimes 			ostate = tp->t_state;
650fb59c426SYoshinobu Inoue #ifdef INET6
651fb59c426SYoshinobu Inoue 			if (isipv6)
652fb59c426SYoshinobu Inoue 				bcopy((char *)ip6, (char *)tcp_saveipgen,
653fb59c426SYoshinobu Inoue 				      sizeof(*ip6));
654fb59c426SYoshinobu Inoue 			else
655fb59c426SYoshinobu Inoue #endif /* INET6 */
656fb59c426SYoshinobu Inoue 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
657fb59c426SYoshinobu Inoue 			tcp_savetcp = *th;
658df8bae1dSRodney W. Grimes 		}
659610ee2f9SDavid Greenman #endif
660df8bae1dSRodney W. Grimes 		if (so->so_options & SO_ACCEPTCONN) {
661a0292f23SGarrett Wollman 			register struct tcpcb *tp0 = tp;
6624195b4afSPaul Traina 			struct socket *so2;
663fb59c426SYoshinobu Inoue #ifdef IPSEC
664fb59c426SYoshinobu Inoue 			struct socket *oso;
665fb59c426SYoshinobu Inoue #endif
666fb59c426SYoshinobu Inoue #ifdef INET6
667fb59c426SYoshinobu Inoue 			struct inpcb *oinp = sotoinpcb(so);
668fb59c426SYoshinobu Inoue #endif /* INET6 */
669fb59c426SYoshinobu Inoue 
670fb59c426SYoshinobu Inoue #ifndef IPSEC
671fb59c426SYoshinobu Inoue 			/*
672fb59c426SYoshinobu Inoue 			 * Current IPsec implementation makes incorrect IPsec
673fb59c426SYoshinobu Inoue 			 * cache if this check is done here.
674fb59c426SYoshinobu Inoue 			 * So delay this until duplicated socket is created.
675fb59c426SYoshinobu Inoue 			 */
676fb59c426SYoshinobu Inoue 			if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
6774195b4afSPaul Traina 				/*
678ebb0cbeaSPaul Traina 				 * Note: dropwithreset makes sure we don't
679ebb0cbeaSPaul Traina 				 * send a RST in response to a RST.
6804195b4afSPaul Traina 				 */
681fb59c426SYoshinobu Inoue 				if (thflags & TH_ACK) {
682ebb0cbeaSPaul Traina 					tcpstat.tcps_badsyn++;
683a57815efSBosko Milekic 					rstreason = BANDLIM_RST_OPENPORT;
684a57815efSBosko Milekic 					goto dropwithreset;
6854195b4afSPaul Traina 				}
686ebb0cbeaSPaul Traina 				goto drop;
687ebb0cbeaSPaul Traina 			}
688fb59c426SYoshinobu Inoue #endif
68933841545SHajimu UMEMOTO 
69033841545SHajimu UMEMOTO #ifdef INET6
69133841545SHajimu UMEMOTO 			/*
69233841545SHajimu UMEMOTO 			 * If deprecated address is forbidden,
69333841545SHajimu UMEMOTO 			 * we do not accept SYN to deprecated interface
69433841545SHajimu UMEMOTO 			 * address to prevent any new inbound connection from
69533841545SHajimu UMEMOTO 			 * getting established.
69633841545SHajimu UMEMOTO 			 * When we do not accept SYN, we send a TCP RST,
69733841545SHajimu UMEMOTO 			 * with deprecated source address (instead of dropping
69833841545SHajimu UMEMOTO 			 * it).  We compromise it as it is much better for peer
69933841545SHajimu UMEMOTO 			 * to send a RST, and RST will be the final packet
70033841545SHajimu UMEMOTO 			 * for the exchange.
70133841545SHajimu UMEMOTO 			 *
70233841545SHajimu UMEMOTO 			 * If we do not forbid deprecated addresses, we accept
70333841545SHajimu UMEMOTO 			 * the SYN packet.  RFC2462 does not suggest dropping
70433841545SHajimu UMEMOTO 			 * SYN in this case.
70533841545SHajimu UMEMOTO 			 * If we decipher RFC2462 5.5.4, it says like this:
70633841545SHajimu UMEMOTO 			 * 1. use of deprecated addr with existing
70733841545SHajimu UMEMOTO 			 *    communication is okay - "SHOULD continue to be
70833841545SHajimu UMEMOTO 			 *    used"
70933841545SHajimu UMEMOTO 			 * 2. use of it with new communication:
71033841545SHajimu UMEMOTO 			 *   (2a) "SHOULD NOT be used if alternate address
71133841545SHajimu UMEMOTO 			 *        with sufficient scope is available"
71233841545SHajimu UMEMOTO 			 *   (2b) nothing mentioned otherwise.
71333841545SHajimu UMEMOTO 			 * Here we fall into (2b) case as we have no choice in
71433841545SHajimu UMEMOTO 			 * our source address selection - we must obey the peer.
71533841545SHajimu UMEMOTO 			 *
71633841545SHajimu UMEMOTO 			 * The wording in RFC2462 is confusing, and there are
71733841545SHajimu UMEMOTO 			 * multiple description text for deprecated address
71833841545SHajimu UMEMOTO 			 * handling - worse, they are not exactly the same.
71933841545SHajimu UMEMOTO 			 * I believe 5.5.4 is the best one, so we follow 5.5.4.
72033841545SHajimu UMEMOTO 			 */
72133841545SHajimu UMEMOTO 			if (isipv6 && !ip6_use_deprecated) {
72233841545SHajimu UMEMOTO 				struct in6_ifaddr *ia6;
72333841545SHajimu UMEMOTO 
72433841545SHajimu UMEMOTO 				if ((ia6 = ip6_getdstifaddr(m)) &&
72533841545SHajimu UMEMOTO 				    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
72633841545SHajimu UMEMOTO 					tp = NULL;
72733841545SHajimu UMEMOTO 					rstreason = BANDLIM_RST_OPENPORT;
72833841545SHajimu UMEMOTO 					goto dropwithreset;
72933841545SHajimu UMEMOTO 				}
73033841545SHajimu UMEMOTO 			}
73133841545SHajimu UMEMOTO #endif
73233841545SHajimu UMEMOTO 
733ebb0cbeaSPaul Traina 			so2 = sonewconn(so, 0);
734ebb0cbeaSPaul Traina 			if (so2 == 0) {
73565f28919SJesper Skriver 				/*
73665f28919SJesper Skriver 				 * If we were unable to create a new socket
73765f28919SJesper Skriver 				 * for this SYN, we call sodropablereq to
73865f28919SJesper Skriver 				 * see if there are any other sockets we
73965f28919SJesper Skriver 				 * can kick out of the listen queue.  If
74065f28919SJesper Skriver 				 * so, we'll silently drop the socket
74165f28919SJesper Skriver 				 * sodropablereq told us to drop and
74265f28919SJesper Skriver 				 * create a new one.
74365f28919SJesper Skriver 				 *
74465f28919SJesper Skriver 				 * If sodropablereq returns 0, we'll
74565f28919SJesper Skriver 				 * simply drop the incoming SYN, as we
74665f28919SJesper Skriver 				 * can not allocate a socket for it.
74765f28919SJesper Skriver 				 */
748ebb0cbeaSPaul Traina 				tcpstat.tcps_listendrop++;
749ebb0cbeaSPaul Traina 				so2 = sodropablereq(so);
750a51764a8SPaul Traina 				if (so2) {
751e7f32693SJayanth Vijayaraghavan 					if (tcp_lq_overflow)
752e7f32693SJayanth Vijayaraghavan 						sototcpcb(so2)->t_flags |=
753e7f32693SJayanth Vijayaraghavan 						    TF_LQ_OVERFLOW;
75465f28919SJesper Skriver 					tcp_close(sototcpcb(so2));
755a51764a8SPaul Traina 					so2 = sonewconn(so, 0);
756a51764a8SPaul Traina 				}
757a51764a8SPaul Traina 				if (!so2)
758df8bae1dSRodney W. Grimes 					goto drop;
7591347f5b8SGuido van Rooij 			}
760fb59c426SYoshinobu Inoue #ifdef IPSEC
761fb59c426SYoshinobu Inoue 			oso = so;
762fb59c426SYoshinobu Inoue #endif
7634195b4afSPaul Traina 			so = so2;
764df8bae1dSRodney W. Grimes 			/*
765df8bae1dSRodney W. Grimes 			 * This is ugly, but ....
766df8bae1dSRodney W. Grimes 			 *
767df8bae1dSRodney W. Grimes 			 * Mark socket as temporary until we're
768df8bae1dSRodney W. Grimes 			 * committed to keeping it.  The code at
769df8bae1dSRodney W. Grimes 			 * ``drop'' and ``dropwithreset'' check the
770df8bae1dSRodney W. Grimes 			 * flag dropsocket to see if the temporary
771df8bae1dSRodney W. Grimes 			 * socket created here should be discarded.
772df8bae1dSRodney W. Grimes 			 * We mark the socket as discardable until
773df8bae1dSRodney W. Grimes 			 * we're committed to it below in TCPS_LISTEN.
774df8bae1dSRodney W. Grimes 			 */
775df8bae1dSRodney W. Grimes 			dropsocket++;
776df8bae1dSRodney W. Grimes 			inp = (struct inpcb *)so->so_pcb;
777fb59c426SYoshinobu Inoue #ifdef INET6
778fb59c426SYoshinobu Inoue 			if (isipv6)
779fb59c426SYoshinobu Inoue 				inp->in6p_laddr = ip6->ip6_dst;
780fb59c426SYoshinobu Inoue 			else {
781fb59c426SYoshinobu Inoue 				inp->inp_vflag &= ~INP_IPV6;
782fb59c426SYoshinobu Inoue 				inp->inp_vflag |= INP_IPV4;
783fb59c426SYoshinobu Inoue #endif /* INET6 */
784fb59c426SYoshinobu Inoue 			inp->inp_laddr = ip->ip_dst;
785fb59c426SYoshinobu Inoue #ifdef INET6
786fb59c426SYoshinobu Inoue 			}
787fb59c426SYoshinobu Inoue #endif /* INET6 */
788fb59c426SYoshinobu Inoue 			inp->inp_lport = th->th_dport;
789c3229e05SDavid Greenman 			if (in_pcbinshash(inp) != 0) {
790c3229e05SDavid Greenman 				/*
7915a8c77a8SDavid E. O'Brien 				 * Undo the assignments above if we failed to
7925a8c77a8SDavid E. O'Brien 				 * put the PCB on the hash lists.
793c3229e05SDavid Greenman 				 */
794fb59c426SYoshinobu Inoue #ifdef INET6
795fb59c426SYoshinobu Inoue 				if (isipv6)
796fb59c426SYoshinobu Inoue 					inp->in6p_laddr = in6addr_any;
797fb59c426SYoshinobu Inoue 				else
798fb59c426SYoshinobu Inoue #endif /* INET6 */
799c3229e05SDavid Greenman 				inp->inp_laddr.s_addr = INADDR_ANY;
800c3229e05SDavid Greenman 				inp->inp_lport = 0;
801c3229e05SDavid Greenman 				goto drop;
802c3229e05SDavid Greenman 			}
803fb59c426SYoshinobu Inoue #ifdef IPSEC
804fb59c426SYoshinobu Inoue 			/*
805fb59c426SYoshinobu Inoue 			 * To avoid creating incorrectly cached IPsec
806fb59c426SYoshinobu Inoue 			 * association, this is need to be done here.
807fb59c426SYoshinobu Inoue 			 *
808fb59c426SYoshinobu Inoue 			 * Subject: (KAME-snap 748)
809fb59c426SYoshinobu Inoue 			 * From: Wayne Knowles <w.knowles@niwa.cri.nz>
810fb59c426SYoshinobu Inoue 			 * ftp://ftp.kame.net/pub/mail-list/snap-users/748
811fb59c426SYoshinobu Inoue 			 */
812fb59c426SYoshinobu Inoue 			if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
813fb59c426SYoshinobu Inoue 				/*
814fb59c426SYoshinobu Inoue 				 * Note: dropwithreset makes sure we don't
815fb59c426SYoshinobu Inoue 				 * send a RST in response to a RST.
816fb59c426SYoshinobu Inoue 				 */
817fb59c426SYoshinobu Inoue 				if (thflags & TH_ACK) {
818fb59c426SYoshinobu Inoue 					tcpstat.tcps_badsyn++;
819a57815efSBosko Milekic 					rstreason = BANDLIM_RST_OPENPORT;
820a57815efSBosko Milekic 					goto dropwithreset;
821fb59c426SYoshinobu Inoue 				}
822fb59c426SYoshinobu Inoue 				goto drop;
823fb59c426SYoshinobu Inoue 			}
824fb59c426SYoshinobu Inoue #endif
825fb59c426SYoshinobu Inoue #ifdef INET6
826fb59c426SYoshinobu Inoue 			if (isipv6) {
827fb59c426SYoshinobu Inoue   				/*
82833841545SHajimu UMEMOTO  				 * Inherit socket options from the listening
829fb59c426SYoshinobu Inoue   				 * socket.
83033841545SHajimu UMEMOTO  				 * Note that in6p_inputopts are not (even
83133841545SHajimu UMEMOTO  				 * should not be) copied, since it stores
83233841545SHajimu UMEMOTO 				 * previously received options and is used to
83333841545SHajimu UMEMOTO  				 * detect if each new option is different than
83433841545SHajimu UMEMOTO  				 * the previous one and hence should be passed
83533841545SHajimu UMEMOTO  				 * to a user.
83633841545SHajimu UMEMOTO  				 * If we copied in6p_inputopts, a user would
83733841545SHajimu UMEMOTO  				 * not be able to receive options just after
83833841545SHajimu UMEMOTO  				 * calling the accept system call.
839fb59c426SYoshinobu Inoue  				 */
840fb59c426SYoshinobu Inoue 				inp->inp_flags |=
841fb59c426SYoshinobu Inoue 					oinp->inp_flags & INP_CONTROLOPTS;
84233841545SHajimu UMEMOTO  				if (oinp->in6p_outputopts)
84333841545SHajimu UMEMOTO  					inp->in6p_outputopts =
84433841545SHajimu UMEMOTO  						ip6_copypktopts(oinp->in6p_outputopts,
84533841545SHajimu UMEMOTO  								M_NOWAIT);
846fb59c426SYoshinobu Inoue 			} else
847fb59c426SYoshinobu Inoue #endif /* INET6 */
848df8bae1dSRodney W. Grimes 			inp->inp_options = ip_srcroute();
849fb59c426SYoshinobu Inoue #ifdef IPSEC
850fb59c426SYoshinobu Inoue 			/* copy old policy into new socket's */
851fb59c426SYoshinobu Inoue 			if (ipsec_copy_policy(sotoinpcb(oso)->inp_sp,
852fb59c426SYoshinobu Inoue 			                      inp->inp_sp))
853fb59c426SYoshinobu Inoue 				printf("tcp_input: could not copy policy\n");
854fb59c426SYoshinobu Inoue #endif
855df8bae1dSRodney W. Grimes 			tp = intotcpcb(inp);
856df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_LISTEN;
857a0292f23SGarrett Wollman 			tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT);
858df8bae1dSRodney W. Grimes 
859a0292f23SGarrett Wollman 			/* Compute proper scaling value from buffer space */
860df8bae1dSRodney W. Grimes 			while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
8615a8c77a8SDavid E. O'Brien 			   TCP_MAXWIN << tp->request_r_scale <
8625a8c77a8SDavid E. O'Brien 			   so->so_rcv.sb_hiwat)
863df8bae1dSRodney W. Grimes 				tp->request_r_scale++;
864df8bae1dSRodney W. Grimes 		}
865df8bae1dSRodney W. Grimes 	}
866df8bae1dSRodney W. Grimes 
867df8bae1dSRodney W. Grimes 	/*
868df8bae1dSRodney W. Grimes 	 * Segment received on connection.
869df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
870df8bae1dSRodney W. Grimes 	 */
8719b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
8727ff19458SPaul Traina 	if (TCPS_HAVEESTABLISHED(tp->t_state))
8739b8b58e0SJonathan Lemon 		callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
874df8bae1dSRodney W. Grimes 
875df8bae1dSRodney W. Grimes 	/*
876df8bae1dSRodney W. Grimes 	 * Process options if not in LISTEN state,
877df8bae1dSRodney W. Grimes 	 * else do it below (after getting remote address).
878df8bae1dSRodney W. Grimes 	 */
879f9d5a964SDavid Greenman 	if (tp->t_state != TCPS_LISTEN)
880fb59c426SYoshinobu Inoue 		tcp_dooptions(tp, optp, optlen, th, &to);
881df8bae1dSRodney W. Grimes 
882df8bae1dSRodney W. Grimes 	/*
883df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
884df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
885df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
886df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
887df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
888df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
889df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
890df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
891df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
892df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
893df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
894df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
895a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
896a0292f23SGarrett Wollman 	 * Since we check for TCPS_ESTABLISHED above, it can only
897a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
898df8bae1dSRodney W. Grimes 	 */
899df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
900fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
901a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
902a0292f23SGarrett Wollman 	    ((to.to_flag & TOF_TS) == 0 ||
903a0292f23SGarrett Wollman 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
904a0292f23SGarrett Wollman 	    /*
905a0292f23SGarrett Wollman 	     * Using the CC option is compulsory if once started:
906a0292f23SGarrett Wollman 	     *   the segment is OK if no T/TCP was negotiated or
907a0292f23SGarrett Wollman 	     *   if the segment has a CC option equal to CCrecv
908a0292f23SGarrett Wollman 	     */
909a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) ||
910831a80b0SMatthew Dillon 	     ((to.to_flag & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) &&
911fb59c426SYoshinobu Inoue 	    th->th_seq == tp->rcv_nxt &&
912df8bae1dSRodney W. Grimes 	    tiwin && tiwin == tp->snd_wnd &&
913df8bae1dSRodney W. Grimes 	    tp->snd_nxt == tp->snd_max) {
914df8bae1dSRodney W. Grimes 
915df8bae1dSRodney W. Grimes 		/*
916df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
917df8bae1dSRodney W. Grimes 		 * record the timestamp.
918a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
919a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
920df8bae1dSRodney W. Grimes 		 */
921a0292f23SGarrett Wollman 		if ((to.to_flag & TOF_TS) != 0 &&
922fb59c426SYoshinobu Inoue 		   SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
9239b8b58e0SJonathan Lemon 			tp->ts_recent_age = ticks;
924a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
925df8bae1dSRodney W. Grimes 		}
926df8bae1dSRodney W. Grimes 
927fb59c426SYoshinobu Inoue 		if (tlen == 0) {
928fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
929fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
930233e8c18SGarrett Wollman 			    tp->snd_cwnd >= tp->snd_wnd &&
931233e8c18SGarrett Wollman 			    tp->t_dupacks < tcprexmtthresh) {
932df8bae1dSRodney W. Grimes 				/*
933df8bae1dSRodney W. Grimes 				 * this is a pure ack for outstanding data.
934df8bae1dSRodney W. Grimes 				 */
935df8bae1dSRodney W. Grimes 				++tcpstat.tcps_predack;
9369b8b58e0SJonathan Lemon 				/*
9379b8b58e0SJonathan Lemon 				 * "bad retransmit" recovery
9389b8b58e0SJonathan Lemon 				 */
9399b8b58e0SJonathan Lemon 				if (tp->t_rxtshift == 1 &&
9409b8b58e0SJonathan Lemon 				    ticks < tp->t_badrxtwin) {
9419b8b58e0SJonathan Lemon 					tp->snd_cwnd = tp->snd_cwnd_prev;
9429b8b58e0SJonathan Lemon 					tp->snd_ssthresh =
9439b8b58e0SJonathan Lemon 					    tp->snd_ssthresh_prev;
9449b8b58e0SJonathan Lemon 					tp->snd_nxt = tp->snd_max;
9459b8b58e0SJonathan Lemon 					tp->t_badrxtwin = 0;
9469b8b58e0SJonathan Lemon 				}
947a0292f23SGarrett Wollman 				if ((to.to_flag & TOF_TS) != 0)
948a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
9499b8b58e0SJonathan Lemon 					    ticks - to.to_tsecr + 1);
9509b8b58e0SJonathan Lemon 				else if (tp->t_rtttime &&
951fb59c426SYoshinobu Inoue 					    SEQ_GT(th->th_ack, tp->t_rtseq))
9529b8b58e0SJonathan Lemon 					tcp_xmit_timer(tp, ticks - tp->t_rtttime);
953fb59c426SYoshinobu Inoue 				acked = th->th_ack - tp->snd_una;
954df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackpack++;
955df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackbyte += acked;
956df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
957fb59c426SYoshinobu Inoue 				tp->snd_una = th->th_ack;
958df8bae1dSRodney W. Grimes 				m_freem(m);
959fb59c426SYoshinobu Inoue 				ND6_HINT(tp); /* some progress has been done */
960df8bae1dSRodney W. Grimes 
961df8bae1dSRodney W. Grimes 				/*
962df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
963df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
964df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
965df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
966df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
967df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
968df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
969df8bae1dSRodney W. Grimes 				 */
970df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
9719b8b58e0SJonathan Lemon 					callout_stop(tp->tt_rexmt);
9729b8b58e0SJonathan Lemon 				else if (!callout_active(tp->tt_persist))
9739b8b58e0SJonathan Lemon 					callout_reset(tp->tt_rexmt,
9749b8b58e0SJonathan Lemon 						      tp->t_rxtcur,
9759b8b58e0SJonathan Lemon 						      tcp_timer_rexmt, tp);
976df8bae1dSRodney W. Grimes 
977df8bae1dSRodney W. Grimes 				sowwakeup(so);
978df8bae1dSRodney W. Grimes 				if (so->so_snd.sb_cc)
979df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
980df8bae1dSRodney W. Grimes 				return;
981df8bae1dSRodney W. Grimes 			}
982fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
983fb59c426SYoshinobu Inoue 		    LIST_EMPTY(&tp->t_segq) &&
984fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
985df8bae1dSRodney W. Grimes 			/*
986df8bae1dSRodney W. Grimes 			 * this is a pure, in-sequence data packet
987df8bae1dSRodney W. Grimes 			 * with nothing on the reassembly queue and
988df8bae1dSRodney W. Grimes 			 * we have enough buffer space to take it.
989df8bae1dSRodney W. Grimes 			 */
990df8bae1dSRodney W. Grimes 			++tcpstat.tcps_preddat;
991fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
992df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpack++;
993fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbyte += tlen;
994fb59c426SYoshinobu Inoue 			ND6_HINT(tp);	/* some progress has been done */
995df8bae1dSRodney W. Grimes 			/*
996755c1f07SAndras Olah 			 * Add data to socket buffer.
997df8bae1dSRodney W. Grimes 			 */
998fb59c426SYoshinobu Inoue 			m_adj(m, drop_hdrlen);	/* delayed header drop */
999df8bae1dSRodney W. Grimes 			sbappend(&so->so_rcv, m);
1000df8bae1dSRodney W. Grimes 			sorwakeup(so);
1001d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp)) {
10029b8b58e0SJonathan Lemon 	                        callout_reset(tp->tt_delack, tcp_delacktime,
10039b8b58e0SJonathan Lemon 	                            tcp_timer_delack, tp);
1004f498eeeeSDavid Greenman 			} else {
1005e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
1006e612a582SDavid Greenman 				tcp_output(tp);
1007e612a582SDavid Greenman 			}
1008df8bae1dSRodney W. Grimes 			return;
1009df8bae1dSRodney W. Grimes 		}
1010df8bae1dSRodney W. Grimes 	}
1011df8bae1dSRodney W. Grimes 
1012df8bae1dSRodney W. Grimes 	/*
1013df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1014df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1015df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1016df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1017df8bae1dSRodney W. Grimes 	 */
1018df8bae1dSRodney W. Grimes 	{ int win;
1019df8bae1dSRodney W. Grimes 
1020df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1021df8bae1dSRodney W. Grimes 	if (win < 0)
1022df8bae1dSRodney W. Grimes 		win = 0;
102366e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1024df8bae1dSRodney W. Grimes 	}
1025df8bae1dSRodney W. Grimes 
1026df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1027df8bae1dSRodney W. Grimes 
1028df8bae1dSRodney W. Grimes 	/*
1029df8bae1dSRodney W. Grimes 	 * If the state is LISTEN then ignore segment if it contains an RST.
1030df8bae1dSRodney W. Grimes 	 * If the segment contains an ACK then it is bad and send a RST.
1031df8bae1dSRodney W. Grimes 	 * If it does not contain a SYN then it is not interesting; drop it.
1032764d8cefSBill Fenner 	 * If it is from this socket, drop it, it must be forged.
1033df8bae1dSRodney W. Grimes 	 * Don't bother responding if the destination was a broadcast.
1034df8bae1dSRodney W. Grimes 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
1035df8bae1dSRodney W. Grimes 	 * tp->iss, and send a segment:
1036df8bae1dSRodney W. Grimes 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
1037df8bae1dSRodney W. Grimes 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
1038df8bae1dSRodney W. Grimes 	 * Fill in remote peer address fields if not previously specified.
1039df8bae1dSRodney W. Grimes 	 * Enter SYN_RECEIVED state, and process any other fields of this
1040df8bae1dSRodney W. Grimes 	 * segment in this state.
1041df8bae1dSRodney W. Grimes 	 */
1042df8bae1dSRodney W. Grimes 	case TCPS_LISTEN: {
1043df8bae1dSRodney W. Grimes 		register struct sockaddr_in *sin;
1044fb59c426SYoshinobu Inoue #ifdef INET6
1045fb59c426SYoshinobu Inoue 		register struct sockaddr_in6 *sin6;
1046fb59c426SYoshinobu Inoue #endif
1047df8bae1dSRodney W. Grimes 
1048fb59c426SYoshinobu Inoue 		if (thflags & TH_RST)
1049df8bae1dSRodney W. Grimes 			goto drop;
1050a57815efSBosko Milekic 		if (thflags & TH_ACK) {
1051a57815efSBosko Milekic 			rstreason = BANDLIM_RST_OPENPORT;
1052a57815efSBosko Milekic 			goto dropwithreset;
1053a57815efSBosko Milekic 		}
1054fb59c426SYoshinobu Inoue 		if ((thflags & TH_SYN) == 0)
1055df8bae1dSRodney W. Grimes 			goto drop;
1056fb59c426SYoshinobu Inoue 		if (th->th_dport == th->th_sport) {
1057fb59c426SYoshinobu Inoue #ifdef INET6
1058fb59c426SYoshinobu Inoue 			if (isipv6) {
1059fb59c426SYoshinobu Inoue 				if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1060fb59c426SYoshinobu Inoue 						       &ip6->ip6_src))
1061764d8cefSBill Fenner 					goto drop;
1062fb59c426SYoshinobu Inoue 			} else
1063fb59c426SYoshinobu Inoue #endif /* INET6 */
1064fb59c426SYoshinobu Inoue 			if (ip->ip_dst.s_addr == ip->ip_src.s_addr)
1065fb59c426SYoshinobu Inoue 				goto drop;
1066fb59c426SYoshinobu Inoue 		}
1067df8bae1dSRodney W. Grimes 		/*
1068df8bae1dSRodney W. Grimes 		 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
1069df8bae1dSRodney W. Grimes 		 * in_broadcast() should never return true on a received
1070df8bae1dSRodney W. Grimes 		 * packet with M_BCAST not set.
1071173c0f9fSWarner Losh  		 *
1072173c0f9fSWarner Losh  		 * Packets with a multicast source address should also
1073173c0f9fSWarner Losh  		 * be discarded.
1074df8bae1dSRodney W. Grimes 		 */
1075fb59c426SYoshinobu Inoue 		if (m->m_flags & (M_BCAST|M_MCAST))
1076df8bae1dSRodney W. Grimes 			goto drop;
1077fb59c426SYoshinobu Inoue #ifdef INET6
1078fb59c426SYoshinobu Inoue 		if (isipv6) {
1079173c0f9fSWarner Losh 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1080173c0f9fSWarner Losh 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
1081fb59c426SYoshinobu Inoue 				goto drop;
1082fb59c426SYoshinobu Inoue 		} else
1083fb59c426SYoshinobu Inoue #endif
1084173c0f9fSWarner Losh 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
1085173c0f9fSWarner Losh 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
1086173c0f9fSWarner Losh 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST))
1087fb59c426SYoshinobu Inoue 			goto drop;
1088fb59c426SYoshinobu Inoue #ifdef INET6
1089fb59c426SYoshinobu Inoue 		if (isipv6) {
1090fb59c426SYoshinobu Inoue 			MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6,
10917cc0979fSDavid Malone 			       M_SONAME, M_NOWAIT | M_ZERO);
1092fb59c426SYoshinobu Inoue 			if (sin6 == NULL)
1093fb59c426SYoshinobu Inoue 				goto drop;
1094fb59c426SYoshinobu Inoue 			sin6->sin6_family = AF_INET6;
1095fb59c426SYoshinobu Inoue 			sin6->sin6_len = sizeof(*sin6);
1096fb59c426SYoshinobu Inoue 			sin6->sin6_addr = ip6->ip6_src;
1097fb59c426SYoshinobu Inoue 			sin6->sin6_port = th->th_sport;
1098fb59c426SYoshinobu Inoue 			laddr6 = inp->in6p_laddr;
1099fb59c426SYoshinobu Inoue 			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1100fb59c426SYoshinobu Inoue 				inp->in6p_laddr = ip6->ip6_dst;
1101fb59c426SYoshinobu Inoue 			if (in6_pcbconnect(inp, (struct sockaddr *)sin6,
1102fb59c426SYoshinobu Inoue 					   &proc0)) {
1103fb59c426SYoshinobu Inoue 				inp->in6p_laddr = laddr6;
1104fb59c426SYoshinobu Inoue 				FREE(sin6, M_SONAME);
1105fb59c426SYoshinobu Inoue 				goto drop;
1106fb59c426SYoshinobu Inoue 			}
1107fb59c426SYoshinobu Inoue 			FREE(sin6, M_SONAME);
1108fb59c426SYoshinobu Inoue 		} else
1109fb59c426SYoshinobu Inoue #endif
1110fb59c426SYoshinobu Inoue 	      {
111157bf258eSGarrett Wollman 		MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
111257bf258eSGarrett Wollman 		       M_NOWAIT);
111357bf258eSGarrett Wollman 		if (sin == NULL)
1114df8bae1dSRodney W. Grimes 			goto drop;
1115df8bae1dSRodney W. Grimes 		sin->sin_family = AF_INET;
1116df8bae1dSRodney W. Grimes 		sin->sin_len = sizeof(*sin);
1117fb59c426SYoshinobu Inoue 		sin->sin_addr = ip->ip_src;
1118fb59c426SYoshinobu Inoue 		sin->sin_port = th->th_sport;
1119df8bae1dSRodney W. Grimes 		bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero));
1120df8bae1dSRodney W. Grimes 		laddr = inp->inp_laddr;
1121df8bae1dSRodney W. Grimes 		if (inp->inp_laddr.s_addr == INADDR_ANY)
1122fb59c426SYoshinobu Inoue 			inp->inp_laddr = ip->ip_dst;
112357bf258eSGarrett Wollman 		if (in_pcbconnect(inp, (struct sockaddr *)sin, &proc0)) {
1124df8bae1dSRodney W. Grimes 			inp->inp_laddr = laddr;
112557bf258eSGarrett Wollman 			FREE(sin, M_SONAME);
1126df8bae1dSRodney W. Grimes 			goto drop;
1127df8bae1dSRodney W. Grimes 		}
112857bf258eSGarrett Wollman 		FREE(sin, M_SONAME);
1129fb59c426SYoshinobu Inoue 	      }
1130df8bae1dSRodney W. Grimes 		tp->t_template = tcp_template(tp);
1131df8bae1dSRodney W. Grimes 		if (tp->t_template == 0) {
1132df8bae1dSRodney W. Grimes 			tp = tcp_drop(tp, ENOBUFS);
1133df8bae1dSRodney W. Grimes 			dropsocket = 0;		/* socket is already gone */
1134df8bae1dSRodney W. Grimes 			goto drop;
1135df8bae1dSRodney W. Grimes 		}
1136a0292f23SGarrett Wollman 		if ((taop = tcp_gettaocache(inp)) == NULL) {
1137a0292f23SGarrett Wollman 			taop = &tao_noncached;
1138a0292f23SGarrett Wollman 			bzero(taop, sizeof(*taop));
1139a0292f23SGarrett Wollman 		}
1140fb59c426SYoshinobu Inoue 		tcp_dooptions(tp, optp, optlen, th, &to);
1141df8bae1dSRodney W. Grimes 		if (iss)
1142df8bae1dSRodney W. Grimes 			tp->iss = iss;
1143f0a04f3fSKris Kennaway 		else {
1144f0a04f3fSKris Kennaway 			tp->iss = tcp_rndiss_next();
1145f0a04f3fSKris Kennaway  		}
1146fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
1147df8bae1dSRodney W. Grimes 		tcp_sendseqinit(tp);
1148df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
114946f58482SJonathan Lemon 		tp->snd_recover = tp->snd_una;
1150a0292f23SGarrett Wollman 		/*
1151a0292f23SGarrett Wollman 		 * Initialization of the tcpcb for transaction;
1152a0292f23SGarrett Wollman 		 *   set SND.WND = SEG.WND,
1153a0292f23SGarrett Wollman 		 *   initialize CCsend and CCrecv.
1154a0292f23SGarrett Wollman 		 */
1155a0292f23SGarrett Wollman 		tp->snd_wnd = tiwin;	/* initial send-window */
1156a0292f23SGarrett Wollman 		tp->cc_send = CC_INC(tcp_ccgen);
1157a0292f23SGarrett Wollman 		tp->cc_recv = to.to_cc;
1158a0292f23SGarrett Wollman 		/*
1159a0292f23SGarrett Wollman 		 * Perform TAO test on incoming CC (SEG.CC) option, if any.
1160a0292f23SGarrett Wollman 		 * - compare SEG.CC against cached CC from the same host,
1161a0292f23SGarrett Wollman 		 *	if any.
1162a0292f23SGarrett Wollman 		 * - if SEG.CC > chached value, SYN must be new and is accepted
1163a0292f23SGarrett Wollman 		 *	immediately: save new CC in the cache, mark the socket
1164a0292f23SGarrett Wollman 		 *	connected, enter ESTABLISHED state, turn on flag to
1165a0292f23SGarrett Wollman 		 *	send a SYN in the next segment.
1166a0292f23SGarrett Wollman 		 *	A virtual advertised window is set in rcv_adv to
1167a0292f23SGarrett Wollman 		 *	initialize SWS prevention.  Then enter normal segment
1168a0292f23SGarrett Wollman 		 *	processing: drop SYN, process data and FIN.
1169a0292f23SGarrett Wollman 		 * - otherwise do a normal 3-way handshake.
1170a0292f23SGarrett Wollman 		 */
1171a0292f23SGarrett Wollman 		if ((to.to_flag & TOF_CC) != 0) {
1172068373b6SGuido van Rooij 		    if (((tp->t_flags & TF_NOPUSH) != 0) &&
117311ad4550SGuido van Rooij 			taop->tao_cc != 0 && CC_GT(to.to_cc, taop->tao_cc)) {
117411ad4550SGuido van Rooij 
1175a0292f23SGarrett Wollman 			taop->tao_cc = to.to_cc;
11769b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
1177a0292f23SGarrett Wollman 			tp->t_state = TCPS_ESTABLISHED;
1178a0292f23SGarrett Wollman 
1179a0292f23SGarrett Wollman 			/*
1180a0292f23SGarrett Wollman 			 * If there is a FIN, or if there is data and the
1181a0292f23SGarrett Wollman 			 * connection is local, then delay SYN,ACK(SYN) in
1182a0292f23SGarrett Wollman 			 * the hope of piggy-backing it on a response
1183a0292f23SGarrett Wollman 			 * segment.  Otherwise must send ACK now in case
1184a0292f23SGarrett Wollman 			 * the other side is slow starting.
1185a0292f23SGarrett Wollman 			 */
1186d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp) && ((thflags & TH_FIN) ||
1187fb59c426SYoshinobu Inoue 			    (tlen != 0 &&
1188fb59c426SYoshinobu Inoue #ifdef INET6
1189fb59c426SYoshinobu Inoue 			      ((isipv6 && in6_localaddr(&inp->in6p_faddr))
1190fb59c426SYoshinobu Inoue 			      ||
1191fb59c426SYoshinobu Inoue 			      (!isipv6 &&
1192fb59c426SYoshinobu Inoue #endif
1193fb59c426SYoshinobu Inoue 			    in_localaddr(inp->inp_faddr)
1194fb59c426SYoshinobu Inoue #ifdef INET6
1195fb59c426SYoshinobu Inoue 			       ))
1196fb59c426SYoshinobu Inoue #endif
1197fb59c426SYoshinobu Inoue 			     ))) {
11989b8b58e0SJonathan Lemon                                 callout_reset(tp->tt_delack, tcp_delacktime,
11999b8b58e0SJonathan Lemon                                     tcp_timer_delack, tp);
12009b8b58e0SJonathan Lemon 				tp->t_flags |= TF_NEEDSYN;
12019b8b58e0SJonathan Lemon 			} else
1202a0292f23SGarrett Wollman 				tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
120307e43e10SAndras Olah 
120407e43e10SAndras Olah 			/*
120507e43e10SAndras Olah 			 * Limit the `virtual advertised window' to TCP_MAXWIN
120607e43e10SAndras Olah 			 * here.  Even if we requested window scaling, it will
120707e43e10SAndras Olah 			 * become effective only later when our SYN is acked.
120807e43e10SAndras Olah 			 */
120907e43e10SAndras Olah 			tp->rcv_adv += min(tp->rcv_wnd, TCP_MAXWIN);
1210a0292f23SGarrett Wollman 			tcpstat.tcps_connects++;
1211a0292f23SGarrett Wollman 			soisconnected(so);
12129b8b58e0SJonathan Lemon 			callout_reset(tp->tt_keep, tcp_keepinit,
12139b8b58e0SJonathan Lemon 				      tcp_timer_keep, tp);
1214a0292f23SGarrett Wollman 			dropsocket = 0;		/* committed to socket */
1215a0292f23SGarrett Wollman 			tcpstat.tcps_accepts++;
1216a0292f23SGarrett Wollman 			goto trimthenstep6;
1217a0292f23SGarrett Wollman 		    }
1218a0292f23SGarrett Wollman 		/* else do standard 3-way handshake */
1219a0292f23SGarrett Wollman 		} else {
1220a0292f23SGarrett Wollman 		    /*
1221a0292f23SGarrett Wollman 		     * No CC option, but maybe CC.NEW:
1222a0292f23SGarrett Wollman 		     *   invalidate cached value.
1223a0292f23SGarrett Wollman 		     */
1224a0292f23SGarrett Wollman 		     taop->tao_cc = 0;
1225a0292f23SGarrett Wollman 		}
1226a0292f23SGarrett Wollman 		/*
1227a0292f23SGarrett Wollman 		 * TAO test failed or there was no CC option,
1228a0292f23SGarrett Wollman 		 *    do a standard 3-way handshake.
1229a0292f23SGarrett Wollman 		 */
1230df8bae1dSRodney W. Grimes 		tp->t_flags |= TF_ACKNOW;
1231df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_SYN_RECEIVED;
12329b8b58e0SJonathan Lemon 		callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
1233df8bae1dSRodney W. Grimes 		dropsocket = 0;		/* committed to socket */
1234df8bae1dSRodney W. Grimes 		tcpstat.tcps_accepts++;
1235df8bae1dSRodney W. Grimes 		goto trimthenstep6;
1236df8bae1dSRodney W. Grimes 		}
1237df8bae1dSRodney W. Grimes 
1238df8bae1dSRodney W. Grimes 	/*
1239764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1240764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1241764d8cefSBill Fenner 	 */
1242764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1243fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1244fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1245a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1246a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1247a57815efSBosko Milekic 				goto dropwithreset;
1248a57815efSBosko Milekic 		}
1249764d8cefSBill Fenner 		break;
1250764d8cefSBill Fenner 
1251764d8cefSBill Fenner 	/*
1252df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
1253df8bae1dSRodney W. Grimes 	 *	if seg contains an ACK, but not for our SYN, drop the input.
1254df8bae1dSRodney W. Grimes 	 *	if seg contains a RST, then drop the connection.
1255df8bae1dSRodney W. Grimes 	 *	if seg does not contain SYN, then drop it.
1256df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
1257df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
1258df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
1259df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1260df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
1261df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
1262df8bae1dSRodney W. Grimes 	 */
1263df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
1264a0292f23SGarrett Wollman 		if ((taop = tcp_gettaocache(inp)) == NULL) {
1265a0292f23SGarrett Wollman 			taop = &tao_noncached;
1266a0292f23SGarrett Wollman 			bzero(taop, sizeof(*taop));
1267a0292f23SGarrett Wollman 		}
1268a0292f23SGarrett Wollman 
1269fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1270fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
1271fb59c426SYoshinobu Inoue 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1272a0292f23SGarrett Wollman 			/*
1273a0292f23SGarrett Wollman 			 * If we have a cached CCsent for the remote host,
1274a0292f23SGarrett Wollman 			 * hence we haven't just crashed and restarted,
1275a0292f23SGarrett Wollman 			 * do not send a RST.  This may be a retransmission
1276a0292f23SGarrett Wollman 			 * from the other side after our earlier ACK was lost.
1277a0292f23SGarrett Wollman 			 * Our new SYN, when it arrives, will serve as the
1278a0292f23SGarrett Wollman 			 * needed ACK.
1279a0292f23SGarrett Wollman 			 */
1280a0292f23SGarrett Wollman 			if (taop->tao_ccsent != 0)
1281a0292f23SGarrett Wollman 				goto drop;
1282a57815efSBosko Milekic 			else {
1283a57815efSBosko Milekic 				rstreason = BANDLIM_UNLIMITED;
1284a0292f23SGarrett Wollman 				goto dropwithreset;
1285a0292f23SGarrett Wollman 			}
1286a57815efSBosko Milekic 		}
1287fb59c426SYoshinobu Inoue 		if (thflags & TH_RST) {
1288fb59c426SYoshinobu Inoue 			if (thflags & TH_ACK)
1289df8bae1dSRodney W. Grimes 				tp = tcp_drop(tp, ECONNREFUSED);
1290df8bae1dSRodney W. Grimes 			goto drop;
1291df8bae1dSRodney W. Grimes 		}
1292fb59c426SYoshinobu Inoue 		if ((thflags & TH_SYN) == 0)
1293df8bae1dSRodney W. Grimes 			goto drop;
1294fb59c426SYoshinobu Inoue 		tp->snd_wnd = th->th_win;	/* initial send window */
1295a0292f23SGarrett Wollman 		tp->cc_recv = to.to_cc;		/* foreign CC */
1296a0292f23SGarrett Wollman 
1297fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
1298df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
1299fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1300a0292f23SGarrett Wollman 			/*
1301a0292f23SGarrett Wollman 			 * Our SYN was acked.  If segment contains CC.ECHO
1302a0292f23SGarrett Wollman 			 * option, check it to make sure this segment really
1303a0292f23SGarrett Wollman 			 * matches our SYN.  If not, just drop it as old
1304a0292f23SGarrett Wollman 			 * duplicate, but send an RST if we're still playing
1305026650e5SBill Fenner 			 * by the old rules.  If no CC.ECHO option, make sure
1306026650e5SBill Fenner 			 * we don't get fooled into using T/TCP.
1307a0292f23SGarrett Wollman 			 */
1308026650e5SBill Fenner 			if (to.to_flag & TOF_CCECHO) {
1309dfd5dee1SPeter Wemm 				if (tp->cc_send != to.to_ccecho) {
1310a0292f23SGarrett Wollman 					if (taop->tao_ccsent != 0)
1311a0292f23SGarrett Wollman 						goto drop;
1312a57815efSBosko Milekic 					else {
1313a57815efSBosko Milekic 						rstreason = BANDLIM_UNLIMITED;
1314a0292f23SGarrett Wollman 						goto dropwithreset;
1315dfd5dee1SPeter Wemm 					}
1316a57815efSBosko Milekic 				}
1317026650e5SBill Fenner 			} else
1318026650e5SBill Fenner 				tp->t_flags &= ~TF_RCVD_CC;
1319845799c1SAndras Olah 			tcpstat.tcps_connects++;
1320845799c1SAndras Olah 			soisconnected(so);
1321845799c1SAndras Olah 			/* Do window scaling on this connection? */
1322845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1323845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1324845799c1SAndras Olah 				tp->snd_scale = tp->requested_s_scale;
1325845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
1326845799c1SAndras Olah 			}
1327a0292f23SGarrett Wollman 			/* Segment is acceptable, update cache if undefined. */
1328a0292f23SGarrett Wollman 			if (taop->tao_ccsent == 0)
1329a0292f23SGarrett Wollman 				taop->tao_ccsent = to.to_ccecho;
1330a0292f23SGarrett Wollman 
1331a0292f23SGarrett Wollman 			tp->rcv_adv += tp->rcv_wnd;
1332a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
1333a0292f23SGarrett Wollman 			/*
1334a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
1335a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
1336a0292f23SGarrett Wollman 			 */
1337d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp) && tlen != 0)
13389b8b58e0SJonathan Lemon                                 callout_reset(tp->tt_delack, tcp_delacktime,
13399b8b58e0SJonathan Lemon                                     tcp_timer_delack, tp);
1340a0292f23SGarrett Wollman 			else
1341a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
1342a0292f23SGarrett Wollman 			/*
1343a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
1344a0292f23SGarrett Wollman 			 * Transitions:
1345a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
1346a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
1347a0292f23SGarrett Wollman 			 */
13489b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
1349a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
1350a0292f23SGarrett Wollman 				tp->t_state = TCPS_FIN_WAIT_1;
1351a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
1352fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
13537ff19458SPaul Traina 			} else {
1354a0292f23SGarrett Wollman 				tp->t_state = TCPS_ESTABLISHED;
13559b8b58e0SJonathan Lemon 				callout_reset(tp->tt_keep, tcp_keepidle,
13569b8b58e0SJonathan Lemon 					      tcp_timer_keep, tp);
13577ff19458SPaul Traina 			}
1358a0292f23SGarrett Wollman 		} else {
1359a0292f23SGarrett Wollman 		/*
1360a0292f23SGarrett Wollman 		 *  Received initial SYN in SYN-SENT[*] state => simul-
1361a0292f23SGarrett Wollman 		 *  taneous open.  If segment contains CC option and there is
1362a0292f23SGarrett Wollman 		 *  a cached CC, apply TAO test; if it succeeds, connection is
1363a0292f23SGarrett Wollman 		 *  half-synchronized.  Otherwise, do 3-way handshake:
1364a0292f23SGarrett Wollman 		 *        SYN-SENT -> SYN-RECEIVED
1365a0292f23SGarrett Wollman 		 *        SYN-SENT* -> SYN-RECEIVED*
1366a0292f23SGarrett Wollman 		 *  If there was no CC option, clear cached CC value.
1367a0292f23SGarrett Wollman 		 */
1368a0292f23SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
13699b8b58e0SJonathan Lemon 			callout_stop(tp->tt_rexmt);
1370a0292f23SGarrett Wollman 			if (to.to_flag & TOF_CC) {
1371a0292f23SGarrett Wollman 				if (taop->tao_cc != 0 &&
1372a0292f23SGarrett Wollman 				    CC_GT(to.to_cc, taop->tao_cc)) {
1373a0292f23SGarrett Wollman 					/*
1374a0292f23SGarrett Wollman 					 * update cache and make transition:
1375a0292f23SGarrett Wollman 					 *        SYN-SENT -> ESTABLISHED*
1376a0292f23SGarrett Wollman 					 *        SYN-SENT* -> FIN-WAIT-1*
1377a0292f23SGarrett Wollman 					 */
1378a0292f23SGarrett Wollman 					taop->tao_cc = to.to_cc;
13799b8b58e0SJonathan Lemon 					tp->t_starttime = ticks;
1380a0292f23SGarrett Wollman 					if (tp->t_flags & TF_NEEDFIN) {
1381a0292f23SGarrett Wollman 						tp->t_state = TCPS_FIN_WAIT_1;
1382a0292f23SGarrett Wollman 						tp->t_flags &= ~TF_NEEDFIN;
13837ff19458SPaul Traina 					} else {
1384a0292f23SGarrett Wollman 						tp->t_state = TCPS_ESTABLISHED;
13859b8b58e0SJonathan Lemon 						callout_reset(tp->tt_keep,
13869b8b58e0SJonathan Lemon 							      tcp_keepidle,
13879b8b58e0SJonathan Lemon 							      tcp_timer_keep,
13889b8b58e0SJonathan Lemon 							      tp);
13897ff19458SPaul Traina 					}
1390a0292f23SGarrett Wollman 					tp->t_flags |= TF_NEEDSYN;
1391df8bae1dSRodney W. Grimes 				} else
1392df8bae1dSRodney W. Grimes 					tp->t_state = TCPS_SYN_RECEIVED;
1393a0292f23SGarrett Wollman 			} else {
1394a0292f23SGarrett Wollman 				/* CC.NEW or no option => invalidate cache */
1395a0292f23SGarrett Wollman 				taop->tao_cc = 0;
1396a0292f23SGarrett Wollman 				tp->t_state = TCPS_SYN_RECEIVED;
1397a0292f23SGarrett Wollman 			}
1398a0292f23SGarrett Wollman 		}
1399df8bae1dSRodney W. Grimes 
1400df8bae1dSRodney W. Grimes trimthenstep6:
1401df8bae1dSRodney W. Grimes 		/*
1402fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
1403df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
1404df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
1405df8bae1dSRodney W. Grimes 		 */
1406fb59c426SYoshinobu Inoue 		th->th_seq++;
1407fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
1408fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
1409df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
1410fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
1411fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
1412df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpackafterwin++;
1413df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
1414df8bae1dSRodney W. Grimes 		}
1415fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
1416fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
1417a0292f23SGarrett Wollman 		/*
1418a0292f23SGarrett Wollman 		 *  Client side of transaction: already sent SYN and data.
1419a0292f23SGarrett Wollman 		 *  If the remote host used T/TCP to validate the SYN,
1420a0292f23SGarrett Wollman 		 *  our data will be ACK'd; if so, enter normal data segment
1421a0292f23SGarrett Wollman 		 *  processing in the middle of step 5, ack processing.
1422a0292f23SGarrett Wollman 		 *  Otherwise, goto step 6.
1423a0292f23SGarrett Wollman 		 */
1424fb59c426SYoshinobu Inoue  		if (thflags & TH_ACK)
1425a0292f23SGarrett Wollman 			goto process_ACK;
1426df8bae1dSRodney W. Grimes 		goto step6;
1427a0292f23SGarrett Wollman 	/*
1428a0292f23SGarrett Wollman 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
1429a0292f23SGarrett Wollman 	 *	if segment contains a SYN and CC [not CC.NEW] option:
1430a0292f23SGarrett Wollman 	 *              if state == TIME_WAIT and connection duration > MSL,
1431a0292f23SGarrett Wollman 	 *                  drop packet and send RST;
1432a0292f23SGarrett Wollman 	 *
1433a0292f23SGarrett Wollman 	 *		if SEG.CC > CCrecv then is new SYN, and can implicitly
1434a0292f23SGarrett Wollman 	 *		    ack the FIN (and data) in retransmission queue.
1435a0292f23SGarrett Wollman 	 *                  Complete close and delete TCPCB.  Then reprocess
1436a0292f23SGarrett Wollman 	 *                  segment, hoping to find new TCPCB in LISTEN state;
1437a0292f23SGarrett Wollman 	 *
1438a0292f23SGarrett Wollman 	 *		else must be old SYN; drop it.
1439a0292f23SGarrett Wollman 	 *      else do normal processing.
1440a0292f23SGarrett Wollman 	 */
1441a0292f23SGarrett Wollman 	case TCPS_LAST_ACK:
1442a0292f23SGarrett Wollman 	case TCPS_CLOSING:
1443a0292f23SGarrett Wollman 	case TCPS_TIME_WAIT:
1444fb59c426SYoshinobu Inoue 		if ((thflags & TH_SYN) &&
1445a0292f23SGarrett Wollman 		    (to.to_flag & TOF_CC) && tp->cc_recv != 0) {
1446a0292f23SGarrett Wollman 			if (tp->t_state == TCPS_TIME_WAIT &&
1447a57815efSBosko Milekic 					(ticks - tp->t_starttime) > tcp_msl) {
1448a57815efSBosko Milekic 				rstreason = BANDLIM_UNLIMITED;
1449a0292f23SGarrett Wollman 				goto dropwithreset;
1450a57815efSBosko Milekic 			}
1451a0292f23SGarrett Wollman 			if (CC_GT(to.to_cc, tp->cc_recv)) {
1452a0292f23SGarrett Wollman 				tp = tcp_close(tp);
1453a0292f23SGarrett Wollman 				goto findpcb;
1454a0292f23SGarrett Wollman 			}
1455a0292f23SGarrett Wollman 			else
1456a0292f23SGarrett Wollman 				goto drop;
1457a0292f23SGarrett Wollman 		}
1458a0292f23SGarrett Wollman  		break;  /* continue normal processing */
1459df8bae1dSRodney W. Grimes 	}
1460df8bae1dSRodney W. Grimes 
1461df8bae1dSRodney W. Grimes 	/*
1462df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
146380ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
146480ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
146580ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
146680ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
146780ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
146880ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
146980ab7c0eSGarrett Wollman 	 * killing a connection).
147080ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
1471a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
1472df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
1473df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
1474df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
1475df8bae1dSRodney W. Grimes 	 *
147680ab7c0eSGarrett Wollman 	 *
147780ab7c0eSGarrett Wollman 	 * If the RST bit is set, check the sequence number to see
147880ab7c0eSGarrett Wollman 	 * if this is a valid reset segment.
147980ab7c0eSGarrett Wollman 	 * RFC 793 page 37:
148080ab7c0eSGarrett Wollman 	 *   In all states except SYN-SENT, all reset (RST) segments
148180ab7c0eSGarrett Wollman 	 *   are validated by checking their SEQ-fields.  A reset is
148280ab7c0eSGarrett Wollman 	 *   valid if its sequence number is in the window.
148380ab7c0eSGarrett Wollman 	 * Note: this does not take into account delayed ACKs, so
148480ab7c0eSGarrett Wollman 	 *   we should test against last_ack_sent instead of rcv_nxt.
14851a244a61SJonathan Lemon 	 *   The sequence number in the reset segment is normally an
14861a244a61SJonathan Lemon 	 *   echo of our outgoing acknowlegement numbers, but some hosts
14871a244a61SJonathan Lemon 	 *   send a reset with the sequence number at the rightmost edge
14881a244a61SJonathan Lemon 	 *   of our receive window, and we have to handle this case.
148980ab7c0eSGarrett Wollman 	 * If we have multiple segments in flight, the intial reset
149080ab7c0eSGarrett Wollman 	 * segment sequence numbers will be to the left of last_ack_sent,
149180ab7c0eSGarrett Wollman 	 * but they will eventually catch up.
149280ab7c0eSGarrett Wollman 	 * In any case, it never made sense to trim reset segments to
149380ab7c0eSGarrett Wollman 	 * fit the receive window since RFC 1122 says:
149480ab7c0eSGarrett Wollman 	 *   4.2.2.12  RST Segment: RFC-793 Section 3.4
149580ab7c0eSGarrett Wollman 	 *
149680ab7c0eSGarrett Wollman 	 *    A TCP SHOULD allow a received RST segment to include data.
149780ab7c0eSGarrett Wollman 	 *
149880ab7c0eSGarrett Wollman 	 *    DISCUSSION
149980ab7c0eSGarrett Wollman 	 *         It has been suggested that a RST segment could contain
150080ab7c0eSGarrett Wollman 	 *         ASCII text that encoded and explained the cause of the
150180ab7c0eSGarrett Wollman 	 *         RST.  No standard has yet been established for such
150280ab7c0eSGarrett Wollman 	 *         data.
150380ab7c0eSGarrett Wollman 	 *
150480ab7c0eSGarrett Wollman 	 * If the reset segment passes the sequence number test examine
150580ab7c0eSGarrett Wollman 	 * the state:
150680ab7c0eSGarrett Wollman 	 *    SYN_RECEIVED STATE:
150780ab7c0eSGarrett Wollman 	 *	If passive open, return to LISTEN state.
150880ab7c0eSGarrett Wollman 	 *	If active open, inform user that connection was refused.
150980ab7c0eSGarrett Wollman 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
151080ab7c0eSGarrett Wollman 	 *	Inform user that connection was reset, and close tcb.
1511e9bd3a37SJonathan M. Bresler 	 *    CLOSING, LAST_ACK STATES:
151280ab7c0eSGarrett Wollman 	 *	Close the tcb.
1513e9bd3a37SJonathan M. Bresler 	 *    TIME_WAIT STATE:
151480ab7c0eSGarrett Wollman 	 *	Drop the segment - see Stevens, vol. 2, p. 964 and
151580ab7c0eSGarrett Wollman 	 *      RFC 1337.
151680ab7c0eSGarrett Wollman 	 */
1517fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
1518fb59c426SYoshinobu Inoue 		if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
1519fb59c426SYoshinobu Inoue 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
152080ab7c0eSGarrett Wollman 			switch (tp->t_state) {
152180ab7c0eSGarrett Wollman 
152280ab7c0eSGarrett Wollman 			case TCPS_SYN_RECEIVED:
152380ab7c0eSGarrett Wollman 				so->so_error = ECONNREFUSED;
152480ab7c0eSGarrett Wollman 				goto close;
152580ab7c0eSGarrett Wollman 
152680ab7c0eSGarrett Wollman 			case TCPS_ESTABLISHED:
152780ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_1:
152880ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_2:
152980ab7c0eSGarrett Wollman 			case TCPS_CLOSE_WAIT:
153080ab7c0eSGarrett Wollman 				so->so_error = ECONNRESET;
153180ab7c0eSGarrett Wollman 			close:
153280ab7c0eSGarrett Wollman 				tp->t_state = TCPS_CLOSED;
153380ab7c0eSGarrett Wollman 				tcpstat.tcps_drops++;
153480ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
153580ab7c0eSGarrett Wollman 				break;
153680ab7c0eSGarrett Wollman 
153780ab7c0eSGarrett Wollman 			case TCPS_CLOSING:
153880ab7c0eSGarrett Wollman 			case TCPS_LAST_ACK:
153980ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
154080ab7c0eSGarrett Wollman 				break;
154180ab7c0eSGarrett Wollman 
154280ab7c0eSGarrett Wollman 			case TCPS_TIME_WAIT:
154380ab7c0eSGarrett Wollman 				break;
154480ab7c0eSGarrett Wollman 			}
154580ab7c0eSGarrett Wollman 		}
154680ab7c0eSGarrett Wollman 		goto drop;
154780ab7c0eSGarrett Wollman 	}
154880ab7c0eSGarrett Wollman 
154980ab7c0eSGarrett Wollman 	/*
1550df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
1551df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
1552df8bae1dSRodney W. Grimes 	 */
155380ab7c0eSGarrett Wollman 	if ((to.to_flag & TOF_TS) != 0 && tp->ts_recent &&
155480ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
1555df8bae1dSRodney W. Grimes 
1556df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
15579b8b58e0SJonathan Lemon 		if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) {
1558df8bae1dSRodney W. Grimes 			/*
1559df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
1560df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
1561df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
1562df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
1563df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
1564df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
1565df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
1566df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
1567df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
1568df8bae1dSRodney W. Grimes 			 */
1569df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
1570df8bae1dSRodney W. Grimes 		} else {
1571df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvduppack++;
1572fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvdupbyte += tlen;
1573df8bae1dSRodney W. Grimes 			tcpstat.tcps_pawsdrop++;
1574df8bae1dSRodney W. Grimes 			goto dropafterack;
1575df8bae1dSRodney W. Grimes 		}
1576df8bae1dSRodney W. Grimes 	}
1577df8bae1dSRodney W. Grimes 
1578a0292f23SGarrett Wollman 	/*
1579a0292f23SGarrett Wollman 	 * T/TCP mechanism
1580a0292f23SGarrett Wollman 	 *   If T/TCP was negotiated and the segment doesn't have CC,
1581dc733423SDag-Erling Smørgrav 	 *   or if its CC is wrong then drop the segment.
1582a0292f23SGarrett Wollman 	 *   RST segments do not have to comply with this.
1583a0292f23SGarrett Wollman 	 */
1584a0292f23SGarrett Wollman 	if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) &&
158580ab7c0eSGarrett Wollman 	    ((to.to_flag & TOF_CC) == 0 || tp->cc_recv != to.to_cc))
1586a0292f23SGarrett Wollman  		goto dropafterack;
1587a0292f23SGarrett Wollman 
158880ab7c0eSGarrett Wollman 	/*
158980ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
159080ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
159180ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
159280ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
159380ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
159480ab7c0eSGarrett Wollman 	 */
1595a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
1596a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
1597a57815efSBosko Milekic 		goto dropwithreset;
1598a57815efSBosko Milekic 	}
159980ab7c0eSGarrett Wollman 
1600fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
1601df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1602fb59c426SYoshinobu Inoue 		if (thflags & TH_SYN) {
1603fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
1604fb59c426SYoshinobu Inoue 			th->th_seq++;
1605fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
1606fb59c426SYoshinobu Inoue 				th->th_urp--;
1607df8bae1dSRodney W. Grimes 			else
1608fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
1609df8bae1dSRodney W. Grimes 			todrop--;
1610df8bae1dSRodney W. Grimes 		}
1611df8bae1dSRodney W. Grimes 		/*
1612dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
1613df8bae1dSRodney W. Grimes 		 */
1614fb59c426SYoshinobu Inoue 		if (todrop > tlen
1615fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
1616dac20301SGarrett Wollman 			/*
1617dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
1618dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
1619dac20301SGarrett Wollman 			 * of sequence; drop it.
1620dac20301SGarrett Wollman 			 */
1621fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
1622dac20301SGarrett Wollman 
1623df8bae1dSRodney W. Grimes 			/*
1624dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
1625dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
1626df8bae1dSRodney W. Grimes 			 */
1627dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
1628fb59c426SYoshinobu Inoue 			todrop = tlen;
1629dac20301SGarrett Wollman 			tcpstat.tcps_rcvduppack++;
1630dac20301SGarrett Wollman 			tcpstat.tcps_rcvdupbyte += todrop;
1631df8bae1dSRodney W. Grimes 		} else {
1632df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartduppack++;
1633df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartdupbyte += todrop;
1634df8bae1dSRodney W. Grimes 		}
1635fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
1636fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
1637fb59c426SYoshinobu Inoue 		tlen -= todrop;
1638fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
1639fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
1640df8bae1dSRodney W. Grimes 		else {
1641fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
1642fb59c426SYoshinobu Inoue 			th->th_urp = 0;
1643df8bae1dSRodney W. Grimes 		}
1644df8bae1dSRodney W. Grimes 	}
1645df8bae1dSRodney W. Grimes 
1646df8bae1dSRodney W. Grimes 	/*
1647df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
1648df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
1649df8bae1dSRodney W. Grimes 	 */
1650df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
1651fb59c426SYoshinobu Inoue 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
1652df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1653df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvafterclose++;
1654a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
1655df8bae1dSRodney W. Grimes 		goto dropwithreset;
1656df8bae1dSRodney W. Grimes 	}
1657df8bae1dSRodney W. Grimes 
1658df8bae1dSRodney W. Grimes 	/*
1659df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
1660df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
1661df8bae1dSRodney W. Grimes 	 */
1662fb59c426SYoshinobu Inoue 	todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd);
1663df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1664df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvpackafterwin++;
1665fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
1666fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbyteafterwin += tlen;
1667df8bae1dSRodney W. Grimes 			/*
1668df8bae1dSRodney W. Grimes 			 * If a new connection request is received
1669df8bae1dSRodney W. Grimes 			 * while in TIME_WAIT, drop the old connection
1670df8bae1dSRodney W. Grimes 			 * and start over if the sequence numbers
1671df8bae1dSRodney W. Grimes 			 * are above the previous ones.
1672df8bae1dSRodney W. Grimes 			 */
1673fb59c426SYoshinobu Inoue 			if (thflags & TH_SYN &&
1674df8bae1dSRodney W. Grimes 			    tp->t_state == TCPS_TIME_WAIT &&
1675fb59c426SYoshinobu Inoue 			    SEQ_GT(th->th_seq, tp->rcv_nxt)) {
1676f0a04f3fSKris Kennaway 				iss = tcp_rndiss_next();
1677df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
1678df8bae1dSRodney W. Grimes 				goto findpcb;
1679df8bae1dSRodney W. Grimes 			}
1680df8bae1dSRodney W. Grimes 			/*
1681df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
1682df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
1683df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
1684df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
1685df8bae1dSRodney W. Grimes 			 * and ack.
1686df8bae1dSRodney W. Grimes 			 */
1687fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
1688df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
1689df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvwinprobe++;
1690df8bae1dSRodney W. Grimes 			} else
1691df8bae1dSRodney W. Grimes 				goto dropafterack;
1692df8bae1dSRodney W. Grimes 		} else
1693df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
1694df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
1695fb59c426SYoshinobu Inoue 		tlen -= todrop;
1696fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
1697df8bae1dSRodney W. Grimes 	}
1698df8bae1dSRodney W. Grimes 
1699df8bae1dSRodney W. Grimes 	/*
1700df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
1701df8bae1dSRodney W. Grimes 	 * record its timestamp.
1702a0292f23SGarrett Wollman 	 * NOTE that the test is modified according to the latest
1703a0292f23SGarrett Wollman 	 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1704df8bae1dSRodney W. Grimes 	 */
1705a0292f23SGarrett Wollman 	if ((to.to_flag & TOF_TS) != 0 &&
1706fb59c426SYoshinobu Inoue 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
17079b8b58e0SJonathan Lemon 		tp->ts_recent_age = ticks;
1708a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
1709df8bae1dSRodney W. Grimes 	}
1710df8bae1dSRodney W. Grimes 
1711df8bae1dSRodney W. Grimes 	/*
1712df8bae1dSRodney W. Grimes 	 * If a SYN is in the window, then this is an
1713df8bae1dSRodney W. Grimes 	 * error and we send an RST and drop the connection.
1714df8bae1dSRodney W. Grimes 	 */
1715fb59c426SYoshinobu Inoue 	if (thflags & TH_SYN) {
1716df8bae1dSRodney W. Grimes 		tp = tcp_drop(tp, ECONNRESET);
1717a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
1718df8bae1dSRodney W. Grimes 		goto dropwithreset;
1719df8bae1dSRodney W. Grimes 	}
1720df8bae1dSRodney W. Grimes 
1721a0292f23SGarrett Wollman 	/*
1722a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
1723a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
1724a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
1725a0292f23SGarrett Wollman 	 */
1726fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
1727a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
1728a0292f23SGarrett Wollman 		    (tp->t_flags & TF_NEEDSYN))
1729a0292f23SGarrett Wollman 			goto step6;
1730a0292f23SGarrett Wollman 		else
1731a0292f23SGarrett Wollman 			goto drop;
1732a0292f23SGarrett Wollman 	}
1733df8bae1dSRodney W. Grimes 
1734df8bae1dSRodney W. Grimes 	/*
1735df8bae1dSRodney W. Grimes 	 * Ack processing.
1736df8bae1dSRodney W. Grimes 	 */
1737df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1738df8bae1dSRodney W. Grimes 
1739df8bae1dSRodney W. Grimes 	/*
1740764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
1741764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
1742764d8cefSBill Fenner 	 * The ACK was checked above.
1743df8bae1dSRodney W. Grimes 	 */
1744df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1745a0292f23SGarrett Wollman 
1746df8bae1dSRodney W. Grimes 		tcpstat.tcps_connects++;
1747df8bae1dSRodney W. Grimes 		soisconnected(so);
1748df8bae1dSRodney W. Grimes 		/* Do window scaling? */
1749df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1750df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1751df8bae1dSRodney W. Grimes 			tp->snd_scale = tp->requested_s_scale;
1752df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
1753df8bae1dSRodney W. Grimes 		}
1754a0292f23SGarrett Wollman 		/*
1755a0292f23SGarrett Wollman 		 * Upon successful completion of 3-way handshake,
1756a0292f23SGarrett Wollman 		 * update cache.CC if it was undefined, pass any queued
1757a0292f23SGarrett Wollman 		 * data to the user, and advance state appropriately.
1758a0292f23SGarrett Wollman 		 */
1759a0292f23SGarrett Wollman 		if ((taop = tcp_gettaocache(inp)) != NULL &&
1760a0292f23SGarrett Wollman 		    taop->tao_cc == 0)
1761a0292f23SGarrett Wollman 			taop->tao_cc = tp->cc_recv;
1762a0292f23SGarrett Wollman 
1763a0292f23SGarrett Wollman 		/*
1764a0292f23SGarrett Wollman 		 * Make transitions:
1765a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
1766a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
1767a0292f23SGarrett Wollman 		 */
17689b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
1769a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDFIN) {
1770a0292f23SGarrett Wollman 			tp->t_state = TCPS_FIN_WAIT_1;
1771a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDFIN;
17727ff19458SPaul Traina 		} else {
1773a0292f23SGarrett Wollman 			tp->t_state = TCPS_ESTABLISHED;
17749b8b58e0SJonathan Lemon 			callout_reset(tp->tt_keep, tcp_keepidle,
17759b8b58e0SJonathan Lemon 				      tcp_timer_keep, tp);
17767ff19458SPaul Traina 		}
1777a0292f23SGarrett Wollman 		/*
1778a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
1779a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
1780a0292f23SGarrett Wollman 		 */
1781fb59c426SYoshinobu Inoue 		if (tlen == 0 && (thflags & TH_FIN) == 0)
1782fb59c426SYoshinobu Inoue 			(void) tcp_reass(tp, (struct tcphdr *)0, 0,
1783a0292f23SGarrett Wollman 			    (struct mbuf *)0);
1784fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
1785df8bae1dSRodney W. Grimes 		/* fall into ... */
1786df8bae1dSRodney W. Grimes 
1787df8bae1dSRodney W. Grimes 	/*
1788df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1789df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
1790fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
1791fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
1792df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
1793df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
1794df8bae1dSRodney W. Grimes 	 */
1795df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1796df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
1797df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
1798df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1799df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
1800df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
1801df8bae1dSRodney W. Grimes 	case TCPS_TIME_WAIT:
1802df8bae1dSRodney W. Grimes 
1803fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
1804fb59c426SYoshinobu Inoue 			if (tlen == 0 && tiwin == tp->snd_wnd) {
1805df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvdupack++;
1806df8bae1dSRodney W. Grimes 				/*
1807df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
1808df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
1809df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
1810df8bae1dSRodney W. Grimes 				 * change), the ack is the biggest we've
1811df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
1812df8bae1dSRodney W. Grimes 				 * threshhold of them, assume a packet
1813df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
1814df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
1815df8bae1dSRodney W. Grimes 				 * window so we send only this one
1816df8bae1dSRodney W. Grimes 				 * packet.
1817df8bae1dSRodney W. Grimes 				 *
1818df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
1819df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
1820df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
1821df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
1822df8bae1dSRodney W. Grimes 				 * the new ssthresh).
1823df8bae1dSRodney W. Grimes 				 *
1824df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
1825df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
1826df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
1827df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
1828df8bae1dSRodney W. Grimes 				 * network.
1829df8bae1dSRodney W. Grimes 				 */
18309b8b58e0SJonathan Lemon 				if (!callout_active(tp->tt_rexmt) ||
1831fb59c426SYoshinobu Inoue 				    th->th_ack != tp->snd_una)
1832df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
1833df8bae1dSRodney W. Grimes 				else if (++tp->t_dupacks == tcprexmtthresh) {
1834df8bae1dSRodney W. Grimes 					tcp_seq onxt = tp->snd_nxt;
1835df8bae1dSRodney W. Grimes 					u_int win =
1836df8bae1dSRodney W. Grimes 					    min(tp->snd_wnd, tp->snd_cwnd) / 2 /
1837df8bae1dSRodney W. Grimes 						tp->t_maxseg;
183846f58482SJonathan Lemon 					if (tcp_do_newreno && SEQ_LT(th->th_ack,
183946f58482SJonathan Lemon 					    tp->snd_recover)) {
184046f58482SJonathan Lemon 						/* False retransmit, should not
184146f58482SJonathan Lemon 						 * cut window
184246f58482SJonathan Lemon 						 */
184346f58482SJonathan Lemon 						tp->snd_cwnd += tp->t_maxseg;
184446f58482SJonathan Lemon 						tp->t_dupacks = 0;
184546f58482SJonathan Lemon 						(void) tcp_output(tp);
184646f58482SJonathan Lemon 						goto drop;
184746f58482SJonathan Lemon 					}
1848df8bae1dSRodney W. Grimes 					if (win < 2)
1849df8bae1dSRodney W. Grimes 						win = 2;
1850df8bae1dSRodney W. Grimes 					tp->snd_ssthresh = win * tp->t_maxseg;
185146f58482SJonathan Lemon 					tp->snd_recover = tp->snd_max;
18529b8b58e0SJonathan Lemon 					callout_stop(tp->tt_rexmt);
18539b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
1854fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
1855df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->t_maxseg;
1856df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1857df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
1858df8bae1dSRodney W. Grimes 					       tp->t_maxseg * tp->t_dupacks;
1859df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
1860df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
1861df8bae1dSRodney W. Grimes 					goto drop;
1862df8bae1dSRodney W. Grimes 				} else if (tp->t_dupacks > tcprexmtthresh) {
1863df8bae1dSRodney W. Grimes 					tp->snd_cwnd += tp->t_maxseg;
1864df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1865df8bae1dSRodney W. Grimes 					goto drop;
1866df8bae1dSRodney W. Grimes 				}
1867df8bae1dSRodney W. Grimes 			} else
1868df8bae1dSRodney W. Grimes 				tp->t_dupacks = 0;
1869df8bae1dSRodney W. Grimes 			break;
1870df8bae1dSRodney W. Grimes 		}
1871df8bae1dSRodney W. Grimes 		/*
1872df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
1873df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
1874df8bae1dSRodney W. Grimes 		 */
187546f58482SJonathan Lemon 		if (tcp_do_newreno == 0) {
1876233e8c18SGarrett Wollman                         if (tp->t_dupacks >= tcprexmtthresh &&
1877df8bae1dSRodney W. Grimes                                 tp->snd_cwnd > tp->snd_ssthresh)
1878df8bae1dSRodney W. Grimes                                 tp->snd_cwnd = tp->snd_ssthresh;
1879df8bae1dSRodney W. Grimes                         tp->t_dupacks = 0;
188046f58482SJonathan Lemon                 } else if (tp->t_dupacks >= tcprexmtthresh &&
188146f58482SJonathan Lemon 		    !tcp_newreno(tp, th)) {
188246f58482SJonathan Lemon                         /*
188346f58482SJonathan Lemon                          * Window inflation should have left us with approx.
188446f58482SJonathan Lemon                          * snd_ssthresh outstanding data.  But in case we
188546f58482SJonathan Lemon                          * would be inclined to send a burst, better to do
188646f58482SJonathan Lemon                          * it via the slow start mechanism.
188746f58482SJonathan Lemon                          */
188846f58482SJonathan Lemon 			if (SEQ_GT(th->th_ack + tp->snd_ssthresh, tp->snd_max))
188946f58482SJonathan Lemon                                 tp->snd_cwnd =
189046f58482SJonathan Lemon 				    tp->snd_max - th->th_ack + tp->t_maxseg;
189146f58482SJonathan Lemon 			else
189246f58482SJonathan Lemon                         	tp->snd_cwnd = tp->snd_ssthresh;
189346f58482SJonathan Lemon                         tp->t_dupacks = 0;
189446f58482SJonathan Lemon                 }
1895fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
1896df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvacktoomuch++;
1897df8bae1dSRodney W. Grimes 			goto dropafterack;
1898df8bae1dSRodney W. Grimes 		}
1899a0292f23SGarrett Wollman 		/*
1900a0292f23SGarrett Wollman 		 *  If we reach this point, ACK is not a duplicate,
1901a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
1902a0292f23SGarrett Wollman 		 */
1903a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
1904a0292f23SGarrett Wollman 			/*
1905a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
1906a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
190707e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
190807e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
190907e43e10SAndras Olah 			 * we can do window scaling.
1910a0292f23SGarrett Wollman 			 */
1911a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
1912a0292f23SGarrett Wollman 			tp->snd_una++;
191307e43e10SAndras Olah 			/* Do window scaling? */
191407e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
191507e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
191607e43e10SAndras Olah 				tp->snd_scale = tp->requested_s_scale;
191707e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
191807e43e10SAndras Olah 			}
1919a0292f23SGarrett Wollman 		}
1920a0292f23SGarrett Wollman 
1921a0292f23SGarrett Wollman process_ACK:
1922fb59c426SYoshinobu Inoue 		acked = th->th_ack - tp->snd_una;
1923df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackpack++;
1924df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackbyte += acked;
1925df8bae1dSRodney W. Grimes 
1926df8bae1dSRodney W. Grimes 		/*
19279b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
19289b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
19299b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
19309b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
19319b8b58e0SJonathan Lemon 		 * we left off.
19329b8b58e0SJonathan Lemon 		 */
19339b8b58e0SJonathan Lemon 		if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) {
19349b8b58e0SJonathan Lemon 			tp->snd_cwnd = tp->snd_cwnd_prev;
19359b8b58e0SJonathan Lemon 			tp->snd_ssthresh = tp->snd_ssthresh_prev;
19369b8b58e0SJonathan Lemon 			tp->snd_nxt = tp->snd_max;
19379b8b58e0SJonathan Lemon 			tp->t_badrxtwin = 0;	/* XXX probably not required */
19389b8b58e0SJonathan Lemon 		}
19399b8b58e0SJonathan Lemon 
19409b8b58e0SJonathan Lemon 		/*
1941df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
1942df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
1943df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
1944df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
1945df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
1946df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
1947df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
1948df8bae1dSRodney W. Grimes 		 */
1949a0292f23SGarrett Wollman 		if (to.to_flag & TOF_TS)
19509b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - to.to_tsecr + 1);
1951fb59c426SYoshinobu Inoue 		else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq))
19529b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
1953df8bae1dSRodney W. Grimes 
1954df8bae1dSRodney W. Grimes 		/*
1955df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
1956df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
1957df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
1958df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
1959df8bae1dSRodney W. Grimes 		 */
1960fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
19619b8b58e0SJonathan Lemon 			callout_stop(tp->tt_rexmt);
1962df8bae1dSRodney W. Grimes 			needoutput = 1;
19639b8b58e0SJonathan Lemon 		} else if (!callout_active(tp->tt_persist))
19649b8b58e0SJonathan Lemon 			callout_reset(tp->tt_rexmt, tp->t_rxtcur,
19659b8b58e0SJonathan Lemon 				      tcp_timer_rexmt, tp);
1966a0292f23SGarrett Wollman 
1967a0292f23SGarrett Wollman 		/*
1968a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
1969a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
1970a0292f23SGarrett Wollman 		 */
1971a0292f23SGarrett Wollman 		if (acked == 0)
1972a0292f23SGarrett Wollman 			goto step6;
1973a0292f23SGarrett Wollman 
1974df8bae1dSRodney W. Grimes 		/*
1975df8bae1dSRodney W. Grimes 		 * When new data is acked, open the congestion window.
1976df8bae1dSRodney W. Grimes 		 * If the window gives us less than ssthresh packets
1977df8bae1dSRodney W. Grimes 		 * in flight, open exponentially (maxseg per packet).
1978df8bae1dSRodney W. Grimes 		 * Otherwise open linearly: maxseg per window
197910be5648SGarrett Wollman 		 * (maxseg^2 / cwnd per packet).
1980df8bae1dSRodney W. Grimes 		 */
1981df8bae1dSRodney W. Grimes 		{
1982df8bae1dSRodney W. Grimes 		register u_int cw = tp->snd_cwnd;
1983df8bae1dSRodney W. Grimes 		register u_int incr = tp->t_maxseg;
1984df8bae1dSRodney W. Grimes 
1985df8bae1dSRodney W. Grimes 		if (cw > tp->snd_ssthresh)
198610be5648SGarrett Wollman 			incr = incr * incr / cw;
19878735719eSJonathan Lemon 		/*
19888735719eSJonathan Lemon 		 * If t_dupacks != 0 here, it indicates that we are still
19898735719eSJonathan Lemon 		 * in NewReno fast recovery mode, so we leave the congestion
19908735719eSJonathan Lemon 		 * window alone.
19918735719eSJonathan Lemon 		 */
19928735719eSJonathan Lemon 		if (tcp_do_newreno == 0 || tp->t_dupacks == 0)
1993df8bae1dSRodney W. Grimes 			tp->snd_cwnd = min(cw + incr,TCP_MAXWIN<<tp->snd_scale);
1994df8bae1dSRodney W. Grimes 		}
1995df8bae1dSRodney W. Grimes 		if (acked > so->so_snd.sb_cc) {
1996df8bae1dSRodney W. Grimes 			tp->snd_wnd -= so->so_snd.sb_cc;
1997df8bae1dSRodney W. Grimes 			sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
1998df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
1999df8bae1dSRodney W. Grimes 		} else {
2000df8bae1dSRodney W. Grimes 			sbdrop(&so->so_snd, acked);
2001df8bae1dSRodney W. Grimes 			tp->snd_wnd -= acked;
2002df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
2003df8bae1dSRodney W. Grimes 		}
2004df8bae1dSRodney W. Grimes 		sowwakeup(so);
2005fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
2006df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2007df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
2008df8bae1dSRodney W. Grimes 
2009df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2010df8bae1dSRodney W. Grimes 
2011df8bae1dSRodney W. Grimes 		/*
2012df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
2013df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
2014df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
2015df8bae1dSRodney W. Grimes 		 */
2016df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2017df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2018df8bae1dSRodney W. Grimes 				/*
2019df8bae1dSRodney W. Grimes 				 * If we can't receive any more
2020df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
2021df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
2022df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
2023df8bae1dSRodney W. Grimes 				 * we'll hang forever.
2024df8bae1dSRodney W. Grimes 				 */
2025df8bae1dSRodney W. Grimes 				if (so->so_state & SS_CANTRCVMORE) {
2026df8bae1dSRodney W. Grimes 					soisdisconnected(so);
20279b8b58e0SJonathan Lemon 					callout_reset(tp->tt_2msl, tcp_maxidle,
20289b8b58e0SJonathan Lemon 						      tcp_timer_2msl, tp);
2029df8bae1dSRodney W. Grimes 				}
2030df8bae1dSRodney W. Grimes 				tp->t_state = TCPS_FIN_WAIT_2;
2031df8bae1dSRodney W. Grimes 			}
2032df8bae1dSRodney W. Grimes 			break;
2033df8bae1dSRodney W. Grimes 
2034df8bae1dSRodney W. Grimes 	 	/*
2035df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
2036df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
2037df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
2038df8bae1dSRodney W. Grimes 		 * the segment.
2039df8bae1dSRodney W. Grimes 		 */
2040df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
2041df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2042df8bae1dSRodney W. Grimes 				tp->t_state = TCPS_TIME_WAIT;
2043df8bae1dSRodney W. Grimes 				tcp_canceltimers(tp);
2044a0292f23SGarrett Wollman 				/* Shorten TIME_WAIT [RFC-1644, p.28] */
2045a0292f23SGarrett Wollman 				if (tp->cc_recv != 0 &&
20469b8b58e0SJonathan Lemon 				    (ticks - tp->t_starttime) < tcp_msl)
20479b8b58e0SJonathan Lemon 					callout_reset(tp->tt_2msl,
20489b8b58e0SJonathan Lemon 						      tp->t_rxtcur *
20499b8b58e0SJonathan Lemon 						      TCPTV_TWTRUNC,
20509b8b58e0SJonathan Lemon 						      tcp_timer_2msl, tp);
2051a0292f23SGarrett Wollman 				else
20529b8b58e0SJonathan Lemon 					callout_reset(tp->tt_2msl, 2 * tcp_msl,
20539b8b58e0SJonathan Lemon 						      tcp_timer_2msl, tp);
2054df8bae1dSRodney W. Grimes 				soisdisconnected(so);
2055df8bae1dSRodney W. Grimes 			}
2056df8bae1dSRodney W. Grimes 			break;
2057df8bae1dSRodney W. Grimes 
2058df8bae1dSRodney W. Grimes 		/*
2059df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
2060df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
2061df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
2062df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
2063df8bae1dSRodney W. Grimes 		 */
2064df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
2065df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2066df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
2067df8bae1dSRodney W. Grimes 				goto drop;
2068df8bae1dSRodney W. Grimes 			}
2069df8bae1dSRodney W. Grimes 			break;
2070df8bae1dSRodney W. Grimes 
2071df8bae1dSRodney W. Grimes 		/*
2072df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state the only thing that should arrive
2073df8bae1dSRodney W. Grimes 		 * is a retransmission of the remote FIN.  Acknowledge
2074df8bae1dSRodney W. Grimes 		 * it and restart the finack timer.
2075df8bae1dSRodney W. Grimes 		 */
2076df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
20779b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, 2 * tcp_msl,
20789b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
2079df8bae1dSRodney W. Grimes 			goto dropafterack;
2080df8bae1dSRodney W. Grimes 		}
2081df8bae1dSRodney W. Grimes 	}
2082df8bae1dSRodney W. Grimes 
2083df8bae1dSRodney W. Grimes step6:
2084df8bae1dSRodney W. Grimes 	/*
2085df8bae1dSRodney W. Grimes 	 * Update window information.
2086df8bae1dSRodney W. Grimes 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2087df8bae1dSRodney W. Grimes 	 */
2088fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) &&
2089fb59c426SYoshinobu Inoue 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
2090fb59c426SYoshinobu Inoue 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
2091fb59c426SYoshinobu Inoue 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
2092df8bae1dSRodney W. Grimes 		/* keep track of pure window updates */
2093fb59c426SYoshinobu Inoue 		if (tlen == 0 &&
2094fb59c426SYoshinobu Inoue 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
2095df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvwinupd++;
2096df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
2097fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq;
2098fb59c426SYoshinobu Inoue 		tp->snd_wl2 = th->th_ack;
2099df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
2100df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
2101df8bae1dSRodney W. Grimes 		needoutput = 1;
2102df8bae1dSRodney W. Grimes 	}
2103df8bae1dSRodney W. Grimes 
2104df8bae1dSRodney W. Grimes 	/*
2105df8bae1dSRodney W. Grimes 	 * Process segments with URG.
2106df8bae1dSRodney W. Grimes 	 */
2107fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
2108df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2109df8bae1dSRodney W. Grimes 		/*
2110df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
2111df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
2112df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
2113df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
2114df8bae1dSRodney W. Grimes 		 */
2115fb59c426SYoshinobu Inoue 		if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
2116fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
2117fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
2118df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
2119df8bae1dSRodney W. Grimes 		}
2120df8bae1dSRodney W. Grimes 		/*
2121df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
2122df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
2123df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2124df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
2125df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
2126df8bae1dSRodney W. Grimes 		 *
2127df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
2128df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
2129df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
2130df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
2131df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
2132df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
2133df8bae1dSRodney W. Grimes 		 */
2134fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2135fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
2136df8bae1dSRodney W. Grimes 			so->so_oobmark = so->so_rcv.sb_cc +
2137df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
2138df8bae1dSRodney W. Grimes 			if (so->so_oobmark == 0)
2139df8bae1dSRodney W. Grimes 				so->so_state |= SS_RCVATMARK;
2140df8bae1dSRodney W. Grimes 			sohasoutofband(so);
2141df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2142df8bae1dSRodney W. Grimes 		}
2143df8bae1dSRodney W. Grimes 		/*
2144df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
2145df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
2146df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
2147df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
2148df8bae1dSRodney W. Grimes 		 */
2149fb59c426SYoshinobu Inoue 		if (th->th_urp <= (u_long)tlen
2150df8bae1dSRodney W. Grimes #ifdef SO_OOBINLINE
2151df8bae1dSRodney W. Grimes 		     && (so->so_options & SO_OOBINLINE) == 0
2152df8bae1dSRodney W. Grimes #endif
2153df8bae1dSRodney W. Grimes 		     )
2154fb59c426SYoshinobu Inoue 			tcp_pulloutofband(so, th, m,
2155fb59c426SYoshinobu Inoue 				drop_hdrlen);	/* hdr drop is delayed */
2156df8bae1dSRodney W. Grimes 	} else
2157df8bae1dSRodney W. Grimes 		/*
2158df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
2159df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
2160df8bae1dSRodney W. Grimes 		 * with the receive window.
2161df8bae1dSRodney W. Grimes 		 */
2162df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2163df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
2164df8bae1dSRodney W. Grimes dodata:							/* XXX */
2165df8bae1dSRodney W. Grimes 
2166df8bae1dSRodney W. Grimes 	/*
2167df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
2168df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
2169df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
2170df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
2171df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
2172df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
2173df8bae1dSRodney W. Grimes 	 */
2174fb59c426SYoshinobu Inoue 	if ((tlen || (thflags&TH_FIN)) &&
2175df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2176fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
2177e4b64281SJesper Skriver 		/*
2178e4b64281SJesper Skriver 		 * Insert segment which inludes th into reassembly queue of tcp with
2179e4b64281SJesper Skriver 		 * control block tp.  Return TH_FIN if reassembly now includes
2180e4b64281SJesper Skriver 		 * a segment with FIN.  This handle the common case inline (segment
2181e4b64281SJesper Skriver 		 * is the next to be received on an established connection, and the
2182e4b64281SJesper Skriver 		 * queue is empty), avoiding linkage into and removal from the queue
2183e4b64281SJesper Skriver 		 * and repetition of various conversions.
2184e4b64281SJesper Skriver 		 * Set DELACK for segments received in order, but ack immediately
2185e4b64281SJesper Skriver 		 * when segments are out of order (so fast retransmit can work).
2186e4b64281SJesper Skriver 		 */
2187e4b64281SJesper Skriver 		if (th->th_seq == tp->rcv_nxt &&
2188e4b64281SJesper Skriver 		    LIST_EMPTY(&tp->t_segq) &&
2189e4b64281SJesper Skriver 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
2190e4b64281SJesper Skriver 			if (DELAY_ACK(tp))
2191e4b64281SJesper Skriver 				callout_reset(tp->tt_delack, tcp_delacktime,
2192e4b64281SJesper Skriver 				    tcp_timer_delack, tp);
2193e4b64281SJesper Skriver 			else
2194e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
2195e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
2196e4b64281SJesper Skriver 			thflags = th->th_flags & TH_FIN;
2197e4b64281SJesper Skriver 			tcpstat.tcps_rcvpack++;
2198e4b64281SJesper Skriver 			tcpstat.tcps_rcvbyte += tlen;
2199e4b64281SJesper Skriver 			ND6_HINT(tp);
2200e4b64281SJesper Skriver 			sbappend(&so->so_rcv, m);
2201e4b64281SJesper Skriver 			sorwakeup(so);
2202e4b64281SJesper Skriver 		} else {
2203e4b64281SJesper Skriver 			thflags = tcp_reass(tp, th, &tlen, m);
2204e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
2205e4b64281SJesper Skriver 		}
2206e4b64281SJesper Skriver 
2207df8bae1dSRodney W. Grimes 		/*
2208df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
2209df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
2210df8bae1dSRodney W. Grimes 		 * buffer size.
2211df8bae1dSRodney W. Grimes 		 */
2212df8bae1dSRodney W. Grimes 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2213df8bae1dSRodney W. Grimes 	} else {
2214df8bae1dSRodney W. Grimes 		m_freem(m);
2215fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
2216df8bae1dSRodney W. Grimes 	}
2217df8bae1dSRodney W. Grimes 
2218df8bae1dSRodney W. Grimes 	/*
2219df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
2220df8bae1dSRodney W. Grimes 	 * that the connection is closing.
2221df8bae1dSRodney W. Grimes 	 */
2222fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
2223df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2224df8bae1dSRodney W. Grimes 			socantrcvmore(so);
2225a0292f23SGarrett Wollman 			/*
2226a0292f23SGarrett Wollman 			 *  If connection is half-synchronized
2227d3eede9dSAndras Olah 			 *  (ie NEEDSYN flag on) then delay ACK,
2228a0292f23SGarrett Wollman 			 *  so it may be piggybacked when SYN is sent.
2229a0292f23SGarrett Wollman 			 *  Otherwise, since we received a FIN then no
2230a0292f23SGarrett Wollman 			 *  more input can be expected, send ACK now.
2231a0292f23SGarrett Wollman 			 */
2232d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp) && (tp->t_flags & TF_NEEDSYN))
22339b8b58e0SJonathan Lemon                                 callout_reset(tp->tt_delack, tcp_delacktime,
22349b8b58e0SJonathan Lemon                                     tcp_timer_delack, tp);
2235a0292f23SGarrett Wollman 			else
2236df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
2237df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
2238df8bae1dSRodney W. Grimes 		}
2239df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2240df8bae1dSRodney W. Grimes 
2241df8bae1dSRodney W. Grimes 	 	/*
2242df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
2243df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
2244df8bae1dSRodney W. Grimes 		 */
2245df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
22469b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
22479b8b58e0SJonathan Lemon 			/*FALLTHROUGH*/
2248df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
2249df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSE_WAIT;
2250df8bae1dSRodney W. Grimes 			break;
2251df8bae1dSRodney W. Grimes 
2252df8bae1dSRodney W. Grimes 	 	/*
2253df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2254df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
2255df8bae1dSRodney W. Grimes 		 */
2256df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2257df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSING;
2258df8bae1dSRodney W. Grimes 			break;
2259df8bae1dSRodney W. Grimes 
2260df8bae1dSRodney W. Grimes 	 	/*
2261df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2262df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
2263df8bae1dSRodney W. Grimes 		 * standard timers.
2264df8bae1dSRodney W. Grimes 		 */
2265df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
2266df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_TIME_WAIT;
2267df8bae1dSRodney W. Grimes 			tcp_canceltimers(tp);
2268a0292f23SGarrett Wollman 			/* Shorten TIME_WAIT [RFC-1644, p.28] */
2269a0292f23SGarrett Wollman 			if (tp->cc_recv != 0 &&
22709b8b58e0SJonathan Lemon 			    (ticks - tp->t_starttime) < tcp_msl) {
22719b8b58e0SJonathan Lemon 				callout_reset(tp->tt_2msl,
22729b8b58e0SJonathan Lemon 					      tp->t_rxtcur * TCPTV_TWTRUNC,
22739b8b58e0SJonathan Lemon 					      tcp_timer_2msl, tp);
2274a0292f23SGarrett Wollman 				/* For transaction client, force ACK now. */
2275a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
2276a0292f23SGarrett Wollman 			}
2277a0292f23SGarrett Wollman 			else
22789b8b58e0SJonathan Lemon 				callout_reset(tp->tt_2msl, 2 * tcp_msl,
22799b8b58e0SJonathan Lemon 					      tcp_timer_2msl, tp);
2280df8bae1dSRodney W. Grimes 			soisdisconnected(so);
2281df8bae1dSRodney W. Grimes 			break;
2282df8bae1dSRodney W. Grimes 
2283df8bae1dSRodney W. Grimes 		/*
2284df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
2285df8bae1dSRodney W. Grimes 		 */
2286df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
22879b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, 2 * tcp_msl,
22889b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
2289df8bae1dSRodney W. Grimes 			break;
2290df8bae1dSRodney W. Grimes 		}
2291df8bae1dSRodney W. Grimes 	}
2292610ee2f9SDavid Greenman #ifdef TCPDEBUG
2293df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG)
2294fb59c426SYoshinobu Inoue 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
2295fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2296610ee2f9SDavid Greenman #endif
2297df8bae1dSRodney W. Grimes 
2298df8bae1dSRodney W. Grimes 	/*
2299df8bae1dSRodney W. Grimes 	 * Return any desired output.
2300df8bae1dSRodney W. Grimes 	 */
2301df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
2302df8bae1dSRodney W. Grimes 		(void) tcp_output(tp);
2303df8bae1dSRodney W. Grimes 	return;
2304df8bae1dSRodney W. Grimes 
2305df8bae1dSRodney W. Grimes dropafterack:
2306df8bae1dSRodney W. Grimes 	/*
2307df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
2308df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
230980ab7c0eSGarrett Wollman 	 *
231080ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
231180ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
231280ab7c0eSGarrett Wollman 	 * RST have been dropped.
231380ab7c0eSGarrett Wollman 	 *
231480ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
231580ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
231680ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
231780ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
231880ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
231980ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
2320df8bae1dSRodney W. Grimes 	 */
2321fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
2322fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
2323a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
2324a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2325a57815efSBosko Milekic 		goto dropwithreset;
2326a57815efSBosko Milekic 	}
2327a0292f23SGarrett Wollman #ifdef TCPDEBUG
2328a0292f23SGarrett Wollman 	if (so->so_options & SO_DEBUG)
2329fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2330fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2331a0292f23SGarrett Wollman #endif
2332df8bae1dSRodney W. Grimes 	m_freem(m);
2333df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
2334df8bae1dSRodney W. Grimes 	(void) tcp_output(tp);
2335df8bae1dSRodney W. Grimes 	return;
2336df8bae1dSRodney W. Grimes 
2337df8bae1dSRodney W. Grimes dropwithreset:
2338df8bae1dSRodney W. Grimes 	/*
2339df8bae1dSRodney W. Grimes 	 * Generate a RST, dropping incoming segment.
2340df8bae1dSRodney W. Grimes 	 * Make ACK acceptable to originator of segment.
2341df8bae1dSRodney W. Grimes 	 * Don't bother to respond if destination was broadcast/multicast.
2342df8bae1dSRodney W. Grimes 	 */
2343fb59c426SYoshinobu Inoue 	if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
2344df8bae1dSRodney W. Grimes 		goto drop;
2345fb59c426SYoshinobu Inoue #ifdef INET6
2346fb59c426SYoshinobu Inoue 	if (isipv6) {
2347173c0f9fSWarner Losh 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
2348173c0f9fSWarner Losh 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
2349fb59c426SYoshinobu Inoue 			goto drop;
2350fb59c426SYoshinobu Inoue 	} else
2351fb59c426SYoshinobu Inoue #endif /* INET6 */
2352173c0f9fSWarner Losh 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
2353173c0f9fSWarner Losh 	    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
2354173c0f9fSWarner Losh 	    ip->ip_src.s_addr == htonl(INADDR_BROADCAST))
2355fb59c426SYoshinobu Inoue 		goto drop;
2356fb59c426SYoshinobu Inoue 	/* IPv6 anycast check is done at tcp6_input() */
2357a57815efSBosko Milekic 
2358a57815efSBosko Milekic 	/*
2359c59319bfSDag-Erling Smørgrav 	 * Perform bandwidth limiting.
2360a57815efSBosko Milekic 	 */
2361a57815efSBosko Milekic 	if (badport_bandlim(rstreason) < 0)
2362a57815efSBosko Milekic 		goto drop;
2363a57815efSBosko Milekic 
2364a0292f23SGarrett Wollman #ifdef TCPDEBUG
2365a0292f23SGarrett Wollman 	if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2366fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2367fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2368a0292f23SGarrett Wollman #endif
2369fb59c426SYoshinobu Inoue 	if (thflags & TH_ACK)
2370fb59c426SYoshinobu Inoue 		/* mtod() below is safe as long as hdr dropping is delayed */
2371fb59c426SYoshinobu Inoue 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack,
2372fb59c426SYoshinobu Inoue 			    TH_RST);
2373df8bae1dSRodney W. Grimes 	else {
2374fb59c426SYoshinobu Inoue 		if (thflags & TH_SYN)
2375fb59c426SYoshinobu Inoue 			tlen++;
2376fb59c426SYoshinobu Inoue 		/* mtod() below is safe as long as hdr dropping is delayed */
2377fb59c426SYoshinobu Inoue 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
2378fb59c426SYoshinobu Inoue 			    (tcp_seq)0, TH_RST|TH_ACK);
2379df8bae1dSRodney W. Grimes 	}
2380df8bae1dSRodney W. Grimes 	/* destroy temporarily created socket */
2381df8bae1dSRodney W. Grimes 	if (dropsocket)
2382df8bae1dSRodney W. Grimes 		(void) soabort(so);
2383df8bae1dSRodney W. Grimes 	return;
2384df8bae1dSRodney W. Grimes 
2385df8bae1dSRodney W. Grimes drop:
2386df8bae1dSRodney W. Grimes 	/*
2387df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
2388df8bae1dSRodney W. Grimes 	 */
2389610ee2f9SDavid Greenman #ifdef TCPDEBUG
2390a0292f23SGarrett Wollman 	if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2391fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2392fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2393a0292f23SGarrett Wollman #endif
2394df8bae1dSRodney W. Grimes 	m_freem(m);
2395df8bae1dSRodney W. Grimes 	/* destroy temporarily created socket */
2396df8bae1dSRodney W. Grimes 	if (dropsocket)
2397df8bae1dSRodney W. Grimes 		(void) soabort(so);
2398df8bae1dSRodney W. Grimes 	return;
2399df8bae1dSRodney W. Grimes }
2400df8bae1dSRodney W. Grimes 
24010312fbe9SPoul-Henning Kamp static void
2402fb59c426SYoshinobu Inoue tcp_dooptions(tp, cp, cnt, th, to)
2403df8bae1dSRodney W. Grimes 	struct tcpcb *tp;
2404df8bae1dSRodney W. Grimes 	u_char *cp;
2405df8bae1dSRodney W. Grimes 	int cnt;
2406fb59c426SYoshinobu Inoue 	struct tcphdr *th;
2407a0292f23SGarrett Wollman 	struct tcpopt *to;
2408df8bae1dSRodney W. Grimes {
2409a0292f23SGarrett Wollman 	u_short mss = 0;
2410df8bae1dSRodney W. Grimes 	int opt, optlen;
2411df8bae1dSRodney W. Grimes 
2412df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
2413df8bae1dSRodney W. Grimes 		opt = cp[0];
2414df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
2415df8bae1dSRodney W. Grimes 			break;
2416df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
2417df8bae1dSRodney W. Grimes 			optlen = 1;
2418df8bae1dSRodney W. Grimes 		else {
2419b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
2420b474779fSJun-ichiro itojun Hagino 				break;
2421df8bae1dSRodney W. Grimes 			optlen = cp[1];
2422b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
2423df8bae1dSRodney W. Grimes 				break;
2424df8bae1dSRodney W. Grimes 		}
2425df8bae1dSRodney W. Grimes 		switch (opt) {
2426df8bae1dSRodney W. Grimes 
2427df8bae1dSRodney W. Grimes 		default:
2428df8bae1dSRodney W. Grimes 			continue;
2429df8bae1dSRodney W. Grimes 
2430df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
2431df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
2432df8bae1dSRodney W. Grimes 				continue;
2433fb59c426SYoshinobu Inoue 			if (!(th->th_flags & TH_SYN))
2434df8bae1dSRodney W. Grimes 				continue;
2435df8bae1dSRodney W. Grimes 			bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
2436df8bae1dSRodney W. Grimes 			NTOHS(mss);
2437df8bae1dSRodney W. Grimes 			break;
2438df8bae1dSRodney W. Grimes 
2439df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
2440df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
2441df8bae1dSRodney W. Grimes 				continue;
2442fb59c426SYoshinobu Inoue 			if (!(th->th_flags & TH_SYN))
2443df8bae1dSRodney W. Grimes 				continue;
2444df8bae1dSRodney W. Grimes 			tp->t_flags |= TF_RCVD_SCALE;
2445df8bae1dSRodney W. Grimes 			tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
2446df8bae1dSRodney W. Grimes 			break;
2447df8bae1dSRodney W. Grimes 
2448df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
2449df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
2450df8bae1dSRodney W. Grimes 				continue;
2451a0292f23SGarrett Wollman 			to->to_flag |= TOF_TS;
2452a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2453a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
2454a0292f23SGarrett Wollman 			NTOHL(to->to_tsval);
2455a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
2456a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
2457a0292f23SGarrett Wollman 			NTOHL(to->to_tsecr);
2458df8bae1dSRodney W. Grimes 
2459df8bae1dSRodney W. Grimes 			/*
2460df8bae1dSRodney W. Grimes 			 * A timestamp received in a SYN makes
2461df8bae1dSRodney W. Grimes 			 * it ok to send timestamp requests and replies.
2462df8bae1dSRodney W. Grimes 			 */
2463fb59c426SYoshinobu Inoue 			if (th->th_flags & TH_SYN) {
2464df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_RCVD_TSTMP;
2465a0292f23SGarrett Wollman 				tp->ts_recent = to->to_tsval;
24669b8b58e0SJonathan Lemon 				tp->ts_recent_age = ticks;
2467df8bae1dSRodney W. Grimes 			}
2468df8bae1dSRodney W. Grimes 			break;
2469a0292f23SGarrett Wollman 		case TCPOPT_CC:
2470a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
2471a0292f23SGarrett Wollman 				continue;
247240db8ef7SAndras Olah 			to->to_flag |= TOF_CC;
2473a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2474a0292f23SGarrett Wollman 			    (char *)&to->to_cc, sizeof(to->to_cc));
2475a0292f23SGarrett Wollman 			NTOHL(to->to_cc);
2476a0292f23SGarrett Wollman 			/*
2477a0292f23SGarrett Wollman 			 * A CC or CC.new option received in a SYN makes
2478a0292f23SGarrett Wollman 			 * it ok to send CC in subsequent segments.
2479a0292f23SGarrett Wollman 			 */
2480fb59c426SYoshinobu Inoue 			if (th->th_flags & TH_SYN)
2481a0292f23SGarrett Wollman 				tp->t_flags |= TF_RCVD_CC;
2482a0292f23SGarrett Wollman 			break;
2483a0292f23SGarrett Wollman 		case TCPOPT_CCNEW:
2484a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
2485a0292f23SGarrett Wollman 				continue;
2486fb59c426SYoshinobu Inoue 			if (!(th->th_flags & TH_SYN))
2487a0292f23SGarrett Wollman 				continue;
2488a0292f23SGarrett Wollman 			to->to_flag |= TOF_CCNEW;
2489a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2490a0292f23SGarrett Wollman 			    (char *)&to->to_cc, sizeof(to->to_cc));
2491a0292f23SGarrett Wollman 			NTOHL(to->to_cc);
2492a0292f23SGarrett Wollman 			/*
2493a0292f23SGarrett Wollman 			 * A CC or CC.new option received in a SYN makes
2494a0292f23SGarrett Wollman 			 * it ok to send CC in subsequent segments.
2495a0292f23SGarrett Wollman 			 */
2496a0292f23SGarrett Wollman 			tp->t_flags |= TF_RCVD_CC;
2497a0292f23SGarrett Wollman 			break;
2498a0292f23SGarrett Wollman 		case TCPOPT_CCECHO:
2499a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
2500a0292f23SGarrett Wollman 				continue;
2501fb59c426SYoshinobu Inoue 			if (!(th->th_flags & TH_SYN))
2502a0292f23SGarrett Wollman 				continue;
2503a0292f23SGarrett Wollman 			to->to_flag |= TOF_CCECHO;
2504a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2505a0292f23SGarrett Wollman 			    (char *)&to->to_ccecho, sizeof(to->to_ccecho));
2506a0292f23SGarrett Wollman 			NTOHL(to->to_ccecho);
2507a0292f23SGarrett Wollman 			break;
2508df8bae1dSRodney W. Grimes 		}
2509df8bae1dSRodney W. Grimes 	}
2510fb59c426SYoshinobu Inoue 	if (th->th_flags & TH_SYN)
2511a0292f23SGarrett Wollman 		tcp_mss(tp, mss);	/* sets t_maxseg */
2512df8bae1dSRodney W. Grimes }
2513df8bae1dSRodney W. Grimes 
2514df8bae1dSRodney W. Grimes /*
2515df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
2516df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
2517df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
2518df8bae1dSRodney W. Grimes  * sequencing purposes.
2519df8bae1dSRodney W. Grimes  */
25200312fbe9SPoul-Henning Kamp static void
2521fb59c426SYoshinobu Inoue tcp_pulloutofband(so, th, m, off)
2522df8bae1dSRodney W. Grimes 	struct socket *so;
2523fb59c426SYoshinobu Inoue 	struct tcphdr *th;
2524df8bae1dSRodney W. Grimes 	register struct mbuf *m;
2525fb59c426SYoshinobu Inoue 	int off;		/* delayed to be droped hdrlen */
2526df8bae1dSRodney W. Grimes {
2527fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
2528df8bae1dSRodney W. Grimes 
2529df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
2530df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
2531df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
2532df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
2533df8bae1dSRodney W. Grimes 
2534df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
2535df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
2536df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
2537df8bae1dSRodney W. Grimes 			m->m_len--;
253869a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
253969a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
2540df8bae1dSRodney W. Grimes 			return;
2541df8bae1dSRodney W. Grimes 		}
2542df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
2543df8bae1dSRodney W. Grimes 		m = m->m_next;
2544df8bae1dSRodney W. Grimes 		if (m == 0)
2545df8bae1dSRodney W. Grimes 			break;
2546df8bae1dSRodney W. Grimes 	}
2547df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
2548df8bae1dSRodney W. Grimes }
2549df8bae1dSRodney W. Grimes 
2550df8bae1dSRodney W. Grimes /*
2551df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
2552df8bae1dSRodney W. Grimes  * and update averages and current timeout.
2553df8bae1dSRodney W. Grimes  */
25540312fbe9SPoul-Henning Kamp static void
2555df8bae1dSRodney W. Grimes tcp_xmit_timer(tp, rtt)
2556df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
25579b8b58e0SJonathan Lemon 	int rtt;
2558df8bae1dSRodney W. Grimes {
2559233e8c18SGarrett Wollman 	register int delta;
2560233e8c18SGarrett Wollman 
2561233e8c18SGarrett Wollman 	tcpstat.tcps_rttupdated++;
2562233e8c18SGarrett Wollman 	tp->t_rttupdated++;
2563233e8c18SGarrett Wollman 	if (tp->t_srtt != 0) {
2564233e8c18SGarrett Wollman 		/*
2565233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
2566233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
2567233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
2568233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
2569233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
2570233e8c18SGarrett Wollman 		 */
2571233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
2572233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
2573233e8c18SGarrett Wollman 
2574233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
2575233e8c18SGarrett Wollman 			tp->t_srtt = 1;
2576233e8c18SGarrett Wollman 
2577233e8c18SGarrett Wollman 		/*
2578233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
2579233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
2580233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
2581233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
2582233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
2583233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
2584233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
2585233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
2586233e8c18SGarrett Wollman 		 */
2587233e8c18SGarrett Wollman 		if (delta < 0)
2588233e8c18SGarrett Wollman 			delta = -delta;
2589233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
2590233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
2591233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
2592233e8c18SGarrett Wollman 	} else {
2593233e8c18SGarrett Wollman 		/*
2594233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
2595233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
2596233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
2597233e8c18SGarrett Wollman 		 */
2598233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
2599233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
2600233e8c18SGarrett Wollman 	}
26019b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
2602df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
2603df8bae1dSRodney W. Grimes 
2604df8bae1dSRodney W. Grimes 	/*
2605df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
2606df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
2607df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
2608df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
2609df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
2610df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
2611df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
2612df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
2613df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
2614df8bae1dSRodney W. Grimes 	 */
2615233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
26169e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
2617df8bae1dSRodney W. Grimes 
2618df8bae1dSRodney W. Grimes 	/*
2619df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
2620df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
2621df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
2622df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
2623df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
2624df8bae1dSRodney W. Grimes 	 */
2625df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
2626df8bae1dSRodney W. Grimes }
2627df8bae1dSRodney W. Grimes 
2628df8bae1dSRodney W. Grimes /*
2629df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
2630df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
2631df8bae1dSRodney W. Grimes  * If none, use an mss that can be handled on the outgoing
2632df8bae1dSRodney W. Grimes  * interface without forcing IP to fragment; if bigger than
2633df8bae1dSRodney W. Grimes  * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
2634df8bae1dSRodney W. Grimes  * to utilize large mbufs.  If no route is found, route has no mtu,
2635df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
2636df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
2637df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
2638df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
2639df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
2640df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
2641df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
2642a0292f23SGarrett Wollman  *
2643a0292f23SGarrett Wollman  * Also take into account the space needed for options that we
2644a0292f23SGarrett Wollman  * send regularly.  Make maxseg shorter by that amount to assure
2645a0292f23SGarrett Wollman  * that we can send maxseg amount of data even when the options
2646a0292f23SGarrett Wollman  * are present.  Store the upper limit of the length of options plus
2647a0292f23SGarrett Wollman  * data in maxopd.
2648a0292f23SGarrett Wollman  *
2649a0292f23SGarrett Wollman  * NOTE that this routine is only called when we process an incoming
2650a0292f23SGarrett Wollman  * segment, for outgoing segments only tcp_mssopt is called.
2651a0292f23SGarrett Wollman  *
2652a0292f23SGarrett Wollman  * In case of T/TCP, we call this routine during implicit connection
2653a0292f23SGarrett Wollman  * setup as well (offer = -1), to initialize maxseg from the cached
2654a0292f23SGarrett Wollman  * MSS of our peer.
2655df8bae1dSRodney W. Grimes  */
2656a0292f23SGarrett Wollman void
2657df8bae1dSRodney W. Grimes tcp_mss(tp, offer)
2658a0292f23SGarrett Wollman 	struct tcpcb *tp;
2659a0292f23SGarrett Wollman 	int offer;
2660df8bae1dSRodney W. Grimes {
2661df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
2662df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
2663df8bae1dSRodney W. Grimes 	register int rtt, mss;
2664df8bae1dSRodney W. Grimes 	u_long bufsize;
2665df8bae1dSRodney W. Grimes 	struct inpcb *inp;
2666df8bae1dSRodney W. Grimes 	struct socket *so;
2667a0292f23SGarrett Wollman 	struct rmxp_tao *taop;
2668a0292f23SGarrett Wollman 	int origoffer = offer;
2669fb59c426SYoshinobu Inoue #ifdef INET6
2670fb59c426SYoshinobu Inoue 	int isipv6;
2671fb59c426SYoshinobu Inoue 	int min_protoh;
2672fb59c426SYoshinobu Inoue #endif
2673df8bae1dSRodney W. Grimes 
2674df8bae1dSRodney W. Grimes 	inp = tp->t_inpcb;
2675fb59c426SYoshinobu Inoue #ifdef INET6
2676fb59c426SYoshinobu Inoue 	isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
2677fb59c426SYoshinobu Inoue 	min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr)
2678fb59c426SYoshinobu Inoue 			    : sizeof (struct tcpiphdr);
2679fb59c426SYoshinobu Inoue #else
2680fb59c426SYoshinobu Inoue #define min_protoh  (sizeof (struct tcpiphdr))
2681fb59c426SYoshinobu Inoue #endif
2682fb59c426SYoshinobu Inoue #ifdef INET6
2683fb59c426SYoshinobu Inoue 	if (isipv6)
2684fb59c426SYoshinobu Inoue 		rt = tcp_rtlookup6(inp);
2685fb59c426SYoshinobu Inoue 	else
2686fb59c426SYoshinobu Inoue #endif
2687fb59c426SYoshinobu Inoue 	rt = tcp_rtlookup(inp);
2688fb59c426SYoshinobu Inoue 	if (rt == NULL) {
2689fb59c426SYoshinobu Inoue 		tp->t_maxopd = tp->t_maxseg =
2690fb59c426SYoshinobu Inoue #ifdef INET6
2691fb59c426SYoshinobu Inoue 		isipv6 ? tcp_v6mssdflt :
2692fb59c426SYoshinobu Inoue #endif /* INET6 */
2693fb59c426SYoshinobu Inoue 		tcp_mssdflt;
2694a0292f23SGarrett Wollman 		return;
2695df8bae1dSRodney W. Grimes 	}
2696df8bae1dSRodney W. Grimes 	ifp = rt->rt_ifp;
2697df8bae1dSRodney W. Grimes 	so = inp->inp_socket;
2698df8bae1dSRodney W. Grimes 
2699a0292f23SGarrett Wollman 	taop = rmx_taop(rt->rt_rmx);
2700a0292f23SGarrett Wollman 	/*
2701a0292f23SGarrett Wollman 	 * Offer == -1 means that we didn't receive SYN yet,
2702a0292f23SGarrett Wollman 	 * use cached value in that case;
2703a0292f23SGarrett Wollman 	 */
2704a0292f23SGarrett Wollman 	if (offer == -1)
2705a0292f23SGarrett Wollman 		offer = taop->tao_mssopt;
2706a0292f23SGarrett Wollman 	/*
2707a0292f23SGarrett Wollman 	 * Offer == 0 means that there was no MSS on the SYN segment,
2708a0292f23SGarrett Wollman 	 * in this case we use tcp_mssdflt.
2709a0292f23SGarrett Wollman 	 */
2710a0292f23SGarrett Wollman 	if (offer == 0)
2711fb59c426SYoshinobu Inoue 		offer =
2712fb59c426SYoshinobu Inoue #ifdef INET6
2713fb59c426SYoshinobu Inoue 			isipv6 ? tcp_v6mssdflt :
2714fb59c426SYoshinobu Inoue #endif /* INET6 */
2715fb59c426SYoshinobu Inoue 			tcp_mssdflt;
2716a0292f23SGarrett Wollman 	else
2717a0292f23SGarrett Wollman 		/*
2718a0292f23SGarrett Wollman 		 * Sanity check: make sure that maxopd will be large
2719a0292f23SGarrett Wollman 		 * enough to allow some data on segments even is the
2720a0292f23SGarrett Wollman 		 * all the option space is used (40bytes).  Otherwise
2721a0292f23SGarrett Wollman 		 * funny things may happen in tcp_output.
2722a0292f23SGarrett Wollman 		 */
2723a0292f23SGarrett Wollman 		offer = max(offer, 64);
2724a0292f23SGarrett Wollman 	taop->tao_mssopt = offer;
2725a0292f23SGarrett Wollman 
2726df8bae1dSRodney W. Grimes 	/*
2727df8bae1dSRodney W. Grimes 	 * While we're here, check if there's an initial rtt
2728df8bae1dSRodney W. Grimes 	 * or rttvar.  Convert from the route-table units
2729df8bae1dSRodney W. Grimes 	 * to scaled multiples of the slow timeout timer.
2730df8bae1dSRodney W. Grimes 	 */
2731df8bae1dSRodney W. Grimes 	if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
2732df8bae1dSRodney W. Grimes 		/*
2733a0292f23SGarrett Wollman 		 * XXX the lock bit for RTT indicates that the value
2734df8bae1dSRodney W. Grimes 		 * is also a minimum value; this is subject to time.
2735df8bae1dSRodney W. Grimes 		 */
2736df8bae1dSRodney W. Grimes 		if (rt->rt_rmx.rmx_locks & RTV_RTT)
27379b8b58e0SJonathan Lemon 			tp->t_rttmin = rtt / (RTM_RTTUNIT / hz);
27389b8b58e0SJonathan Lemon 		tp->t_srtt = rtt / (RTM_RTTUNIT / (hz * TCP_RTT_SCALE));
2739dd224982SGarrett Wollman 		tcpstat.tcps_usedrtt++;
2740dd224982SGarrett Wollman 		if (rt->rt_rmx.rmx_rttvar) {
2741df8bae1dSRodney W. Grimes 			tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
27429b8b58e0SJonathan Lemon 			    (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE));
2743dd224982SGarrett Wollman 			tcpstat.tcps_usedrttvar++;
2744dd224982SGarrett Wollman 		} else {
2745df8bae1dSRodney W. Grimes 			/* default variation is +- 1 rtt */
2746df8bae1dSRodney W. Grimes 			tp->t_rttvar =
2747df8bae1dSRodney W. Grimes 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
2748dd224982SGarrett Wollman 		}
2749df8bae1dSRodney W. Grimes 		TCPT_RANGESET(tp->t_rxtcur,
2750df8bae1dSRodney W. Grimes 			      ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
2751df8bae1dSRodney W. Grimes 			      tp->t_rttmin, TCPTV_REXMTMAX);
2752df8bae1dSRodney W. Grimes 	}
2753df8bae1dSRodney W. Grimes 	/*
2754df8bae1dSRodney W. Grimes 	 * if there's an mtu associated with the route, use it
2755fb59c426SYoshinobu Inoue 	 * else, use the link mtu.
2756df8bae1dSRodney W. Grimes 	 */
2757df8bae1dSRodney W. Grimes 	if (rt->rt_rmx.rmx_mtu)
2758fb59c426SYoshinobu Inoue 		mss = rt->rt_rmx.rmx_mtu - min_protoh;
2759df8bae1dSRodney W. Grimes 	else
2760df8bae1dSRodney W. Grimes 	{
2761fb59c426SYoshinobu Inoue 		mss =
2762fb59c426SYoshinobu Inoue #ifdef INET6
2763fb59c426SYoshinobu Inoue 			(isipv6 ? nd_ifinfo[rt->rt_ifp->if_index].linkmtu :
2764fb59c426SYoshinobu Inoue #endif
2765fb59c426SYoshinobu Inoue 			 ifp->if_mtu
2766fb59c426SYoshinobu Inoue #ifdef INET6
2767fb59c426SYoshinobu Inoue 			 )
2768fb59c426SYoshinobu Inoue #endif
2769fb59c426SYoshinobu Inoue 			- min_protoh;
2770fb59c426SYoshinobu Inoue #ifdef INET6
2771fb59c426SYoshinobu Inoue 		if (isipv6) {
2772fb59c426SYoshinobu Inoue 			if (!in6_localaddr(&inp->in6p_faddr))
2773fb59c426SYoshinobu Inoue 				mss = min(mss, tcp_v6mssdflt);
2774fb59c426SYoshinobu Inoue 		} else
2775fb59c426SYoshinobu Inoue #endif
2776a0292f23SGarrett Wollman 		if (!in_localaddr(inp->inp_faddr))
2777a0292f23SGarrett Wollman 			mss = min(mss, tcp_mssdflt);
2778a0292f23SGarrett Wollman 	}
2779a0292f23SGarrett Wollman 	mss = min(mss, offer);
2780a0292f23SGarrett Wollman 	/*
2781a0292f23SGarrett Wollman 	 * maxopd stores the maximum length of data AND options
2782a0292f23SGarrett Wollman 	 * in a segment; maxseg is the amount of data in a normal
2783a0292f23SGarrett Wollman 	 * segment.  We need to store this value (maxopd) apart
2784a0292f23SGarrett Wollman 	 * from maxseg, because now every segment carries options
2785a0292f23SGarrett Wollman 	 * and thus we normally have somewhat less data in segments.
2786a0292f23SGarrett Wollman 	 */
2787a0292f23SGarrett Wollman 	tp->t_maxopd = mss;
2788a0292f23SGarrett Wollman 
2789a0292f23SGarrett Wollman 	/*
2790a0292f23SGarrett Wollman 	 * In case of T/TCP, origoffer==-1 indicates, that no segments
2791a0292f23SGarrett Wollman 	 * were received yet.  In this case we just guess, otherwise
2792a0292f23SGarrett Wollman 	 * we do the same as before T/TCP.
2793a0292f23SGarrett Wollman 	 */
2794a0292f23SGarrett Wollman  	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
2795a0292f23SGarrett Wollman 	    (origoffer == -1 ||
2796a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
2797a0292f23SGarrett Wollman 		mss -= TCPOLEN_TSTAMP_APPA;
2798a0292f23SGarrett Wollman  	if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
2799a0292f23SGarrett Wollman 	    (origoffer == -1 ||
2800a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC))
2801a0292f23SGarrett Wollman 		mss -= TCPOLEN_CC_APPA;
2802a0292f23SGarrett Wollman 
2803df8bae1dSRodney W. Grimes #if	(MCLBYTES & (MCLBYTES - 1)) == 0
2804df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
2805df8bae1dSRodney W. Grimes 			mss &= ~(MCLBYTES-1);
2806df8bae1dSRodney W. Grimes #else
2807df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
2808df8bae1dSRodney W. Grimes 			mss = mss / MCLBYTES * MCLBYTES;
2809df8bae1dSRodney W. Grimes #endif
2810df8bae1dSRodney W. Grimes 	/*
2811df8bae1dSRodney W. Grimes 	 * If there's a pipesize, change the socket buffer
2812df8bae1dSRodney W. Grimes 	 * to that size.  Make the socket buffers an integral
2813df8bae1dSRodney W. Grimes 	 * number of mss units; if the mss is larger than
2814df8bae1dSRodney W. Grimes 	 * the socket buffer, decrease the mss.
2815df8bae1dSRodney W. Grimes 	 */
2816df8bae1dSRodney W. Grimes #ifdef RTV_SPIPE
2817df8bae1dSRodney W. Grimes 	if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
2818df8bae1dSRodney W. Grimes #endif
2819df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
2820df8bae1dSRodney W. Grimes 	if (bufsize < mss)
2821df8bae1dSRodney W. Grimes 		mss = bufsize;
2822df8bae1dSRodney W. Grimes 	else {
2823df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
2824df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
2825df8bae1dSRodney W. Grimes 			bufsize = sb_max;
2826ecf72308SBrian Feldman 		(void)sbreserve(&so->so_snd, bufsize, so, NULL);
2827df8bae1dSRodney W. Grimes 	}
2828df8bae1dSRodney W. Grimes 	tp->t_maxseg = mss;
2829df8bae1dSRodney W. Grimes 
2830df8bae1dSRodney W. Grimes #ifdef RTV_RPIPE
2831df8bae1dSRodney W. Grimes 	if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
2832df8bae1dSRodney W. Grimes #endif
2833df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
2834df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
2835df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
2836df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
2837df8bae1dSRodney W. Grimes 			bufsize = sb_max;
2838ecf72308SBrian Feldman 		(void)sbreserve(&so->so_rcv, bufsize, so, NULL);
2839df8bae1dSRodney W. Grimes 	}
28409b8b58e0SJonathan Lemon 
2841a0292f23SGarrett Wollman 	/*
28429b8b58e0SJonathan Lemon 	 * Set the slow-start flight size depending on whether this
28439b8b58e0SJonathan Lemon 	 * is a local network or not.
2844a0292f23SGarrett Wollman 	 */
2845fb59c426SYoshinobu Inoue 	if (
2846fb59c426SYoshinobu Inoue #ifdef INET6
2847fb59c426SYoshinobu Inoue 	    (isipv6 && in6_localaddr(&inp->in6p_faddr)) ||
2848fb59c426SYoshinobu Inoue 	    (!isipv6 &&
2849fb59c426SYoshinobu Inoue #endif
2850fb59c426SYoshinobu Inoue 	     in_localaddr(inp->inp_faddr)
2851fb59c426SYoshinobu Inoue #ifdef INET6
2852fb59c426SYoshinobu Inoue 	     )
2853fb59c426SYoshinobu Inoue #endif
2854fb59c426SYoshinobu Inoue 	    )
28559b8b58e0SJonathan Lemon 		tp->snd_cwnd = mss * ss_fltsz_local;
28569b8b58e0SJonathan Lemon 	else
28579b8b58e0SJonathan Lemon 		tp->snd_cwnd = mss * ss_fltsz;
2858df8bae1dSRodney W. Grimes 
2859df8bae1dSRodney W. Grimes 	if (rt->rt_rmx.rmx_ssthresh) {
2860df8bae1dSRodney W. Grimes 		/*
2861df8bae1dSRodney W. Grimes 		 * There's some sort of gateway or interface
2862df8bae1dSRodney W. Grimes 		 * buffer limit on the path.  Use this to set
2863df8bae1dSRodney W. Grimes 		 * the slow start threshhold, but set the
2864df8bae1dSRodney W. Grimes 		 * threshold to no less than 2*mss.
2865df8bae1dSRodney W. Grimes 		 */
2866df8bae1dSRodney W. Grimes 		tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
2867dd224982SGarrett Wollman 		tcpstat.tcps_usedssthresh++;
2868df8bae1dSRodney W. Grimes 	}
2869a0292f23SGarrett Wollman }
2870a0292f23SGarrett Wollman 
2871a0292f23SGarrett Wollman /*
2872a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
2873a0292f23SGarrett Wollman  */
2874a0292f23SGarrett Wollman int
2875a0292f23SGarrett Wollman tcp_mssopt(tp)
2876a0292f23SGarrett Wollman 	struct tcpcb *tp;
2877a0292f23SGarrett Wollman {
2878a0292f23SGarrett Wollman 	struct rtentry *rt;
2879fb59c426SYoshinobu Inoue #ifdef INET6
2880fb59c426SYoshinobu Inoue 	int isipv6;
2881fb59c426SYoshinobu Inoue 	int min_protoh;
2882fb59c426SYoshinobu Inoue #endif
2883a0292f23SGarrett Wollman 
2884fb59c426SYoshinobu Inoue #ifdef INET6
2885fb59c426SYoshinobu Inoue 	isipv6 = ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
2886fb59c426SYoshinobu Inoue 	min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr)
2887fb59c426SYoshinobu Inoue 			    : sizeof (struct tcpiphdr);
2888fb59c426SYoshinobu Inoue #else
2889fb59c426SYoshinobu Inoue #define min_protoh  (sizeof (struct tcpiphdr))
2890fb59c426SYoshinobu Inoue #endif
2891fb59c426SYoshinobu Inoue #ifdef INET6
2892fb59c426SYoshinobu Inoue 	if (isipv6)
2893fb59c426SYoshinobu Inoue 		rt = tcp_rtlookup6(tp->t_inpcb);
2894fb59c426SYoshinobu Inoue 	else
2895fb59c426SYoshinobu Inoue #endif /* INET6 */
2896a0292f23SGarrett Wollman 	rt = tcp_rtlookup(tp->t_inpcb);
2897a0292f23SGarrett Wollman 	if (rt == NULL)
2898fb59c426SYoshinobu Inoue 		return
2899fb59c426SYoshinobu Inoue #ifdef INET6
2900fb59c426SYoshinobu Inoue 			isipv6 ? tcp_v6mssdflt :
2901fb59c426SYoshinobu Inoue #endif /* INET6 */
2902fb59c426SYoshinobu Inoue 			tcp_mssdflt;
2903a0292f23SGarrett Wollman 
2904fb59c426SYoshinobu Inoue 	return rt->rt_ifp->if_mtu - min_protoh;
2905df8bae1dSRodney W. Grimes }
290646f58482SJonathan Lemon 
290746f58482SJonathan Lemon 
290846f58482SJonathan Lemon /*
290946f58482SJonathan Lemon  * Checks for partial ack.  If partial ack arrives, force the retransmission
291046f58482SJonathan Lemon  * of the next unacknowledged segment, do not clear tp->t_dupacks, and return
291146f58482SJonathan Lemon  * 1.  By setting snd_nxt to ti_ack, this forces retransmission timer to
291246f58482SJonathan Lemon  * be started again.  If the ack advances at least to tp->snd_recover, return 0.
291346f58482SJonathan Lemon  */
291446f58482SJonathan Lemon static int
291546f58482SJonathan Lemon tcp_newreno(tp, th)
291646f58482SJonathan Lemon 	struct tcpcb *tp;
291746f58482SJonathan Lemon 	struct tcphdr *th;
291846f58482SJonathan Lemon {
291946f58482SJonathan Lemon 	if (SEQ_LT(th->th_ack, tp->snd_recover)) {
292046f58482SJonathan Lemon 		tcp_seq onxt = tp->snd_nxt;
292146f58482SJonathan Lemon 		u_long  ocwnd = tp->snd_cwnd;
292246f58482SJonathan Lemon 
292346f58482SJonathan Lemon 		callout_stop(tp->tt_rexmt);
292446f58482SJonathan Lemon 		tp->t_rtttime = 0;
292546f58482SJonathan Lemon 		tp->snd_nxt = th->th_ack;
29266b2a5f92SJayanth Vijayaraghavan 		/*
29276b2a5f92SJayanth Vijayaraghavan 		 * Set snd_cwnd to one segment beyond acknowledged offset
29286b2a5f92SJayanth Vijayaraghavan 		 * (tp->snd_una has not yet been updated when this function
29296b2a5f92SJayanth Vijayaraghavan 		 *  is called)
29306b2a5f92SJayanth Vijayaraghavan 		 */
29316b2a5f92SJayanth Vijayaraghavan 		tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una);
29326b2a5f92SJayanth Vijayaraghavan 		(void) tcp_output(tp);
293346f58482SJonathan Lemon 		tp->snd_cwnd = ocwnd;
293446f58482SJonathan Lemon 		if (SEQ_GT(onxt, tp->snd_nxt))
293546f58482SJonathan Lemon 			tp->snd_nxt = onxt;
293646f58482SJonathan Lemon 		/*
293746f58482SJonathan Lemon 		 * Partial window deflation.  Relies on fact that tp->snd_una
293846f58482SJonathan Lemon 		 * not updated yet.
293946f58482SJonathan Lemon 		 */
294046f58482SJonathan Lemon 		tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg);
294146f58482SJonathan Lemon 		return (1);
294246f58482SJonathan Lemon 	}
294346f58482SJonathan Lemon 	return (0);
294446f58482SJonathan Lemon }
2945