xref: /freebsd/sys/netinet/tcp_input.c (revision e4b642817111829d4adee7a7fdd93af0510e51e6)
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;
308fb59c426SYoshinobu Inoue 
309fb59c426SYoshinobu Inoue 	IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
310fb59c426SYoshinobu Inoue 
311fb59c426SYoshinobu Inoue 	/*
312fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
313fb59c426SYoshinobu Inoue 	 * better place to put this in?
314fb59c426SYoshinobu Inoue 	 */
315fb59c426SYoshinobu Inoue 	if (m->m_flags & M_ANYCAST6) {
316fb59c426SYoshinobu Inoue 		struct ip6_hdr *ip6;
317fb59c426SYoshinobu Inoue 
318fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
319fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
320fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
321fb59c426SYoshinobu Inoue 		return IPPROTO_DONE;
322fb59c426SYoshinobu Inoue 	}
323fb59c426SYoshinobu Inoue 
324fb59c426SYoshinobu Inoue 	tcp_input(m, *offp, proto);
325fb59c426SYoshinobu Inoue 	return IPPROTO_DONE;
326fb59c426SYoshinobu Inoue }
327fb59c426SYoshinobu Inoue #endif
328fb59c426SYoshinobu Inoue 
329df8bae1dSRodney W. Grimes void
3306a800098SYoshinobu Inoue tcp_input(m, off0, proto)
331df8bae1dSRodney W. Grimes 	register struct mbuf *m;
3326a800098SYoshinobu Inoue 	int off0, proto;
333df8bae1dSRodney W. Grimes {
334fb59c426SYoshinobu Inoue 	register struct tcphdr *th;
335fb59c426SYoshinobu Inoue 	register struct ip *ip = NULL;
336fb59c426SYoshinobu Inoue 	register struct ipovly *ipov;
337df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
338e79adb8eSGarrett Wollman 	u_char *optp = NULL;
33926f9a767SRodney W. Grimes 	int optlen = 0;
340df8bae1dSRodney W. Grimes 	int len, tlen, off;
341fb59c426SYoshinobu Inoue 	int drop_hdrlen;
342df8bae1dSRodney W. Grimes 	register struct tcpcb *tp = 0;
343fb59c426SYoshinobu Inoue 	register int thflags;
34426f9a767SRodney W. Grimes 	struct socket *so = 0;
345df8bae1dSRodney W. Grimes 	int todrop, acked, ourfinisacked, needoutput = 0;
346df8bae1dSRodney W. Grimes 	struct in_addr laddr;
347fb59c426SYoshinobu Inoue #ifdef INET6
348fb59c426SYoshinobu Inoue 	struct in6_addr laddr6;
349fb59c426SYoshinobu Inoue #endif
350df8bae1dSRodney W. Grimes 	int dropsocket = 0;
351df8bae1dSRodney W. Grimes 	int iss = 0;
352a0292f23SGarrett Wollman 	u_long tiwin;
353a0292f23SGarrett Wollman 	struct tcpopt to;		/* options in this segment */
354a0292f23SGarrett Wollman 	struct rmxp_tao *taop;		/* pointer to our TAO cache entry */
355a0292f23SGarrett Wollman 	struct rmxp_tao	tao_noncached;	/* in case there's no cached entry */
356610ee2f9SDavid Greenman #ifdef TCPDEBUG
357610ee2f9SDavid Greenman 	short ostate = 0;
358610ee2f9SDavid Greenman #endif
359fb59c426SYoshinobu Inoue #ifdef INET6
360fb59c426SYoshinobu Inoue 	struct ip6_hdr *ip6 = NULL;
361fb59c426SYoshinobu Inoue 	int isipv6;
362fb59c426SYoshinobu Inoue #endif /* INET6 */
363a57815efSBosko Milekic 	int rstreason; /* For badport_bandlim accounting purposes */
364df8bae1dSRodney W. Grimes 
365fb59c426SYoshinobu Inoue #ifdef INET6
366fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
367fb59c426SYoshinobu Inoue #endif
368a0292f23SGarrett Wollman 	bzero((char *)&to, sizeof(to));
369a0292f23SGarrett Wollman 
370df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvtotal++;
371fb59c426SYoshinobu Inoue 
372fb59c426SYoshinobu Inoue #ifdef INET6
373fb59c426SYoshinobu Inoue 	if (isipv6) {
374fb59c426SYoshinobu Inoue 		/* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
375fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
376fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
377fb59c426SYoshinobu Inoue 		if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) {
378fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbadsum++;
379fb59c426SYoshinobu Inoue 			goto drop;
380fb59c426SYoshinobu Inoue 		}
381fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
382fb59c426SYoshinobu Inoue 	} else
383fb59c426SYoshinobu Inoue #endif /* INET6 */
384fb59c426SYoshinobu Inoue       {
385df8bae1dSRodney W. Grimes 	/*
386df8bae1dSRodney W. Grimes 	 * Get IP and TCP header together in first mbuf.
387df8bae1dSRodney W. Grimes 	 * Note: IP leaves IP header in first mbuf.
388df8bae1dSRodney W. Grimes 	 */
389fb59c426SYoshinobu Inoue 	if (off0 > sizeof (struct ip)) {
390df8bae1dSRodney W. Grimes 		ip_stripoptions(m, (struct mbuf *)0);
391fb59c426SYoshinobu Inoue 		off0 = sizeof(struct ip);
392fb59c426SYoshinobu Inoue 	}
393df8bae1dSRodney W. Grimes 	if (m->m_len < sizeof (struct tcpiphdr)) {
394df8bae1dSRodney W. Grimes 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
395df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvshort++;
396df8bae1dSRodney W. Grimes 			return;
397df8bae1dSRodney W. Grimes 		}
398df8bae1dSRodney W. Grimes 	}
399fb59c426SYoshinobu Inoue 	ip = mtod(m, struct ip *);
400fb59c426SYoshinobu Inoue 	ipov = (struct ipovly *)ip;
401db4f9cc7SJonathan Lemon 	th = (struct tcphdr *)((caddr_t)ip + off0);
402db4f9cc7SJonathan Lemon 	tlen = ip->ip_len;
403df8bae1dSRodney W. Grimes 
404db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
405db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
406db4f9cc7SJonathan Lemon                 	th->th_sum = m->m_pkthdr.csum_data;
407db4f9cc7SJonathan Lemon 		else
408db4f9cc7SJonathan Lemon 	                th->th_sum = in_pseudo(ip->ip_src.s_addr,
409db4f9cc7SJonathan Lemon 			    ip->ip_dst.s_addr, htonl(m->m_pkthdr.csum_data +
410db4f9cc7SJonathan Lemon 			    ip->ip_len + IPPROTO_TCP));
411db4f9cc7SJonathan Lemon 		th->th_sum ^= 0xffff;
412db4f9cc7SJonathan Lemon 	} else {
413df8bae1dSRodney W. Grimes 		/*
414df8bae1dSRodney W. Grimes 		 * Checksum extended TCP header and data.
415df8bae1dSRodney W. Grimes 		 */
416df8bae1dSRodney W. Grimes 		len = sizeof (struct ip) + tlen;
417fb59c426SYoshinobu Inoue 		bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
418fb59c426SYoshinobu Inoue 		ipov->ih_len = (u_short)tlen;
419fb59c426SYoshinobu Inoue 		HTONS(ipov->ih_len);
420fb59c426SYoshinobu Inoue 		th->th_sum = in_cksum(m, len);
421db4f9cc7SJonathan Lemon 	}
422fb59c426SYoshinobu Inoue 	if (th->th_sum) {
423df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvbadsum++;
424df8bae1dSRodney W. Grimes 		goto drop;
425df8bae1dSRodney W. Grimes 	}
426fb59c426SYoshinobu Inoue #ifdef INET6
427fb59c426SYoshinobu Inoue 	/* Re-initialization for later version check */
428fb59c426SYoshinobu Inoue 	ip->ip_v = IPVERSION;
429fb59c426SYoshinobu Inoue #endif
430fb59c426SYoshinobu Inoue       }
431df8bae1dSRodney W. Grimes 
432df8bae1dSRodney W. Grimes 	/*
433df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
434df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
435df8bae1dSRodney W. Grimes 	 */
436fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
437df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
438df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvbadoff++;
439df8bae1dSRodney W. Grimes 		goto drop;
440df8bae1dSRodney W. Grimes 	}
441fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
442df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
443fb59c426SYoshinobu Inoue #ifdef INET6
444fb59c426SYoshinobu Inoue 		if (isipv6) {
445fb59c426SYoshinobu Inoue 			IP6_EXTHDR_CHECK(m, off0, off, );
446fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
447fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
448fb59c426SYoshinobu Inoue 		} else
449fb59c426SYoshinobu Inoue #endif /* INET6 */
450fb59c426SYoshinobu Inoue 	      {
451df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof(struct ip) + off) {
452df8bae1dSRodney W. Grimes 			if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
453df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvshort++;
454df8bae1dSRodney W. Grimes 				return;
455df8bae1dSRodney W. Grimes 			}
456fb59c426SYoshinobu Inoue 			ip = mtod(m, struct ip *);
457fb59c426SYoshinobu Inoue 			ipov = (struct ipovly *)ip;
458fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip + off0);
459fb59c426SYoshinobu Inoue 		}
460df8bae1dSRodney W. Grimes 	      }
461df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
462fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
463df8bae1dSRodney W. Grimes 	}
464fb59c426SYoshinobu Inoue 	thflags = th->th_flags;
465df8bae1dSRodney W. Grimes 
466e46cd3d4SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN
467e46cd3d4SDag-Erling Smørgrav 	/*
468e46cd3d4SDag-Erling Smørgrav 	 * If the drop_synfin option is enabled, drop all packets with
469e46cd3d4SDag-Erling Smørgrav 	 * both the SYN and FIN bits set. This prevents e.g. nmap from
470e46cd3d4SDag-Erling Smørgrav 	 * identifying the TCP/IP stack.
471e46cd3d4SDag-Erling Smørgrav 	 *
472a589a70eSGarrett Wollman 	 * This is a violation of the TCP specification.
473e46cd3d4SDag-Erling Smørgrav 	 */
474fb59c426SYoshinobu Inoue 	if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN))
475e46cd3d4SDag-Erling Smørgrav 		goto drop;
476e46cd3d4SDag-Erling Smørgrav #endif
477e46cd3d4SDag-Erling Smørgrav 
478df8bae1dSRodney W. Grimes 	/*
479df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
480df8bae1dSRodney W. Grimes 	 */
481fb59c426SYoshinobu Inoue 	NTOHL(th->th_seq);
482fb59c426SYoshinobu Inoue 	NTOHL(th->th_ack);
483fb59c426SYoshinobu Inoue 	NTOHS(th->th_win);
484fb59c426SYoshinobu Inoue 	NTOHS(th->th_urp);
485df8bae1dSRodney W. Grimes 
486df8bae1dSRodney W. Grimes 	/*
487fb59c426SYoshinobu Inoue 	 * Delay droping TCP, IP headers, IPv6 ext headers, and TCP options,
488fb59c426SYoshinobu Inoue 	 * until after ip6_savecontrol() is called and before other functions
489fb59c426SYoshinobu Inoue 	 * which don't want those proto headers.
490fb59c426SYoshinobu Inoue 	 * Because ip6_savecontrol() is going to parse the mbuf to
491fb59c426SYoshinobu Inoue 	 * search for data to be passed up to user-land, it wants mbuf
492fb59c426SYoshinobu Inoue 	 * parameters to be unchanged.
493755c1f07SAndras Olah 	 */
494fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
495755c1f07SAndras Olah 
496755c1f07SAndras Olah 	/*
497df8bae1dSRodney W. Grimes 	 * Locate pcb for segment.
498df8bae1dSRodney W. Grimes 	 */
499df8bae1dSRodney W. Grimes findpcb:
500f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD
501fb59c426SYoshinobu Inoue 	if (ip_fw_fwd_addr != NULL
502fb59c426SYoshinobu Inoue #ifdef INET6
503fb59c426SYoshinobu Inoue 	    && isipv6 == NULL /* IPv6 support is not yet */
504fb59c426SYoshinobu Inoue #endif /* INET6 */
505fb59c426SYoshinobu Inoue 	    ) {
506f9e354dfSJulian Elischer 		/*
507f9e354dfSJulian Elischer 		 * Diverted. Pretend to be the destination.
508f9e354dfSJulian Elischer 		 * already got one like this?
509f9e354dfSJulian Elischer 		 */
510fb59c426SYoshinobu Inoue 		inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport,
511fb59c426SYoshinobu Inoue 			ip->ip_dst, th->th_dport, 0, m->m_pkthdr.rcvif);
512f9e354dfSJulian Elischer 		if (!inp) {
513f9e354dfSJulian Elischer 			/*
514f9e354dfSJulian Elischer 			 * No, then it's new. Try find the ambushing socket
515f9e354dfSJulian Elischer 			 */
516f9e354dfSJulian Elischer 			if (!ip_fw_fwd_addr->sin_port) {
517fb59c426SYoshinobu Inoue 				inp = in_pcblookup_hash(&tcbinfo, ip->ip_src,
518fb59c426SYoshinobu Inoue 				    th->th_sport, ip_fw_fwd_addr->sin_addr,
519fb59c426SYoshinobu Inoue 				    th->th_dport, 1, m->m_pkthdr.rcvif);
520f9e354dfSJulian Elischer 			} else {
521f9e354dfSJulian Elischer 				inp = in_pcblookup_hash(&tcbinfo,
522fb59c426SYoshinobu Inoue 				    ip->ip_src, th->th_sport,
523f9e354dfSJulian Elischer 	    			    ip_fw_fwd_addr->sin_addr,
524cfa1ca9dSYoshinobu Inoue 				    ntohs(ip_fw_fwd_addr->sin_port), 1,
525cfa1ca9dSYoshinobu Inoue 				    m->m_pkthdr.rcvif);
526f9e354dfSJulian Elischer 			}
527f9e354dfSJulian Elischer 		}
528f9e354dfSJulian Elischer 		ip_fw_fwd_addr = NULL;
529f9e354dfSJulian Elischer 	} else
530f9e354dfSJulian Elischer #endif	/* IPFIREWALL_FORWARD */
531fb59c426SYoshinobu Inoue       {
532fb59c426SYoshinobu Inoue #ifdef INET6
533fb59c426SYoshinobu Inoue 	if (isipv6)
534fb59c426SYoshinobu Inoue 		inp = in6_pcblookup_hash(&tcbinfo, &ip6->ip6_src, th->th_sport,
535fb59c426SYoshinobu Inoue 					 &ip6->ip6_dst, th->th_dport, 1,
536fb59c426SYoshinobu Inoue 					 m->m_pkthdr.rcvif);
537fb59c426SYoshinobu Inoue 	else
538fb59c426SYoshinobu Inoue #endif /* INET6 */
539fb59c426SYoshinobu Inoue 	inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport,
540fb59c426SYoshinobu Inoue 	    ip->ip_dst, th->th_dport, 1, m->m_pkthdr.rcvif);
541fb59c426SYoshinobu Inoue       }
542f9e354dfSJulian Elischer 
543fb59c426SYoshinobu Inoue #ifdef IPSEC
544fb59c426SYoshinobu Inoue #ifdef INET6
545fb59c426SYoshinobu Inoue 	if (isipv6) {
546fb59c426SYoshinobu Inoue 		if (inp != NULL && ipsec6_in_reject_so(m, inp->inp_socket)) {
547fb59c426SYoshinobu Inoue 			ipsec6stat.in_polvio++;
548fb59c426SYoshinobu Inoue 			goto drop;
549fb59c426SYoshinobu Inoue 		}
550fb59c426SYoshinobu Inoue 	} else
551fb59c426SYoshinobu Inoue #endif /* INET6 */
552fb59c426SYoshinobu Inoue 	if (inp != NULL && ipsec4_in_reject_so(m, inp->inp_socket)) {
553fb59c426SYoshinobu Inoue 		ipsecstat.in_polvio++;
554fb59c426SYoshinobu Inoue 		goto drop;
555fb59c426SYoshinobu Inoue 	}
556fb59c426SYoshinobu Inoue #endif /*IPSEC*/
557df8bae1dSRodney W. Grimes 
558df8bae1dSRodney W. Grimes 	/*
559df8bae1dSRodney W. Grimes 	 * If the state is CLOSED (i.e., TCB does not exist) then
560df8bae1dSRodney W. Grimes 	 * all data in the incoming segment is discarded.
561df8bae1dSRodney W. Grimes 	 * If the TCB exists but is in CLOSED state, it is embryonic,
562df8bae1dSRodney W. Grimes 	 * but should either do a listen or a connect soon.
563df8bae1dSRodney W. Grimes 	 */
564816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
5652e4e1b4cSGeoff Rehmet 		if (log_in_vain) {
566fb59c426SYoshinobu Inoue #ifdef INET6
567fb59c426SYoshinobu Inoue 			char dbuf[INET6_ADDRSTRLEN], sbuf[INET6_ADDRSTRLEN];
568fb59c426SYoshinobu Inoue #else /* INET6 */
569fb59c426SYoshinobu Inoue 			char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"];
570fb59c426SYoshinobu Inoue #endif /* INET6 */
57175cfc95fSAndrey A. Chernov 
572fb59c426SYoshinobu Inoue #ifdef INET6
573fb59c426SYoshinobu Inoue 			if (isipv6) {
574fb59c426SYoshinobu Inoue 				strcpy(dbuf, ip6_sprintf(&ip6->ip6_dst));
575fb59c426SYoshinobu Inoue 				strcpy(sbuf, ip6_sprintf(&ip6->ip6_src));
576fb59c426SYoshinobu Inoue 			} else
577fb59c426SYoshinobu Inoue #endif
578fb59c426SYoshinobu Inoue 		      {
579fb59c426SYoshinobu Inoue 			strcpy(dbuf, inet_ntoa(ip->ip_dst));
580fb59c426SYoshinobu Inoue 			strcpy(sbuf, inet_ntoa(ip->ip_src));
581fb59c426SYoshinobu Inoue 		      }
5822e4e1b4cSGeoff Rehmet 			switch (log_in_vain) {
5832e4e1b4cSGeoff Rehmet 			case 1:
584fb59c426SYoshinobu Inoue 				if(thflags & TH_SYN)
585592071e8SBruce Evans 					log(LOG_INFO,
586592071e8SBruce Evans 			    		"Connection attempt to TCP %s:%d from %s:%d\n",
587fb59c426SYoshinobu Inoue 			    		dbuf, ntohs(th->th_dport),
588fb59c426SYoshinobu Inoue 					sbuf,
589fb59c426SYoshinobu Inoue 					ntohs(th->th_sport));
5902e4e1b4cSGeoff Rehmet 				break;
5912e4e1b4cSGeoff Rehmet 			case 2:
5922e4e1b4cSGeoff Rehmet 				log(LOG_INFO,
5932e4e1b4cSGeoff Rehmet 			    	"Connection attempt to TCP %s:%d from %s:%d flags:0x%x\n",
594fb59c426SYoshinobu Inoue 			    	dbuf, ntohs(th->th_dport), sbuf,
595fb59c426SYoshinobu Inoue 			    	ntohs(th->th_sport), thflags);
5962e4e1b4cSGeoff Rehmet 				break;
5972e4e1b4cSGeoff Rehmet 			default:
5982e4e1b4cSGeoff Rehmet 				break;
5992e4e1b4cSGeoff Rehmet 			}
60075cfc95fSAndrey A. Chernov 		}
6012e4e1b4cSGeoff Rehmet 		if (blackhole) {
6022e4e1b4cSGeoff Rehmet 			switch (blackhole) {
603828b7f40SGeoff Rehmet 			case 1:
604fb59c426SYoshinobu Inoue 				if (thflags & TH_SYN)
605828b7f40SGeoff Rehmet 					goto drop;
606828b7f40SGeoff Rehmet 				break;
607828b7f40SGeoff Rehmet 			case 2:
608828b7f40SGeoff Rehmet 				goto drop;
609828b7f40SGeoff Rehmet 			default:
610828b7f40SGeoff Rehmet 				goto drop;
6112e4e1b4cSGeoff Rehmet 			}
612828b7f40SGeoff Rehmet 		}
613a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
614a57815efSBosko Milekic 		goto dropwithreset;
615816a3d83SPoul-Henning Kamp 	}
616df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
61709f81a46SBosko Milekic 	if (tp == 0) {
618a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
619a57815efSBosko Milekic 		goto dropwithreset;
62009f81a46SBosko Milekic 	}
621df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_CLOSED)
622df8bae1dSRodney W. Grimes 		goto drop;
623df8bae1dSRodney W. Grimes 
624df8bae1dSRodney W. Grimes 	/* Unscale the window into a 32-bit value. */
625fb59c426SYoshinobu Inoue 	if ((thflags & TH_SYN) == 0)
626fb59c426SYoshinobu Inoue 		tiwin = th->th_win << tp->snd_scale;
627df8bae1dSRodney W. Grimes 	else
628fb59c426SYoshinobu Inoue 		tiwin = th->th_win;
629fb59c426SYoshinobu Inoue 
630fb59c426SYoshinobu Inoue #ifdef INET6
631fb59c426SYoshinobu Inoue 	/* save packet options if user wanted */
6324739b807SYoshinobu Inoue 	if (isipv6 && inp->in6p_flags & INP_CONTROLOPTS) {
633fb59c426SYoshinobu Inoue 		if (inp->in6p_options) {
634fb59c426SYoshinobu Inoue 			m_freem(inp->in6p_options);
635fb59c426SYoshinobu Inoue 			inp->in6p_options = 0;
636fb59c426SYoshinobu Inoue 		}
637fb59c426SYoshinobu Inoue 		ip6_savecontrol(inp, &inp->in6p_options, ip6, m);
638fb59c426SYoshinobu Inoue 	}
6398972cdb1SYoshinobu Inoue         /* else, should also do ip_srcroute() here? */
640fb59c426SYoshinobu Inoue #endif /* INET6 */
641df8bae1dSRodney W. Grimes 
642df8bae1dSRodney W. Grimes 	so = inp->inp_socket;
643df8bae1dSRodney W. Grimes 	if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
644610ee2f9SDavid Greenman #ifdef TCPDEBUG
645df8bae1dSRodney W. Grimes 		if (so->so_options & SO_DEBUG) {
646df8bae1dSRodney W. Grimes 			ostate = tp->t_state;
647fb59c426SYoshinobu Inoue #ifdef INET6
648fb59c426SYoshinobu Inoue 			if (isipv6)
649fb59c426SYoshinobu Inoue 				bcopy((char *)ip6, (char *)tcp_saveipgen,
650fb59c426SYoshinobu Inoue 				      sizeof(*ip6));
651fb59c426SYoshinobu Inoue 			else
652fb59c426SYoshinobu Inoue #endif /* INET6 */
653fb59c426SYoshinobu Inoue 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
654fb59c426SYoshinobu Inoue 			tcp_savetcp = *th;
655df8bae1dSRodney W. Grimes 		}
656610ee2f9SDavid Greenman #endif
657df8bae1dSRodney W. Grimes 		if (so->so_options & SO_ACCEPTCONN) {
658a0292f23SGarrett Wollman 			register struct tcpcb *tp0 = tp;
6594195b4afSPaul Traina 			struct socket *so2;
660fb59c426SYoshinobu Inoue #ifdef IPSEC
661fb59c426SYoshinobu Inoue 			struct socket *oso;
662fb59c426SYoshinobu Inoue #endif
663fb59c426SYoshinobu Inoue #ifdef INET6
664fb59c426SYoshinobu Inoue 			struct inpcb *oinp = sotoinpcb(so);
665fb59c426SYoshinobu Inoue #endif /* INET6 */
666fb59c426SYoshinobu Inoue 
667fb59c426SYoshinobu Inoue #ifndef IPSEC
668fb59c426SYoshinobu Inoue 			/*
669fb59c426SYoshinobu Inoue 			 * Current IPsec implementation makes incorrect IPsec
670fb59c426SYoshinobu Inoue 			 * cache if this check is done here.
671fb59c426SYoshinobu Inoue 			 * So delay this until duplicated socket is created.
672fb59c426SYoshinobu Inoue 			 */
673fb59c426SYoshinobu Inoue 			if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
6744195b4afSPaul Traina 				/*
675ebb0cbeaSPaul Traina 				 * Note: dropwithreset makes sure we don't
676ebb0cbeaSPaul Traina 				 * send a RST in response to a RST.
6774195b4afSPaul Traina 				 */
678fb59c426SYoshinobu Inoue 				if (thflags & TH_ACK) {
679ebb0cbeaSPaul Traina 					tcpstat.tcps_badsyn++;
680a57815efSBosko Milekic 					rstreason = BANDLIM_RST_OPENPORT;
681a57815efSBosko Milekic 					goto dropwithreset;
6824195b4afSPaul Traina 				}
683ebb0cbeaSPaul Traina 				goto drop;
684ebb0cbeaSPaul Traina 			}
685fb59c426SYoshinobu Inoue #endif
686ebb0cbeaSPaul Traina 			so2 = sonewconn(so, 0);
687ebb0cbeaSPaul Traina 			if (so2 == 0) {
688ebb0cbeaSPaul Traina 				tcpstat.tcps_listendrop++;
689ebb0cbeaSPaul Traina 				so2 = sodropablereq(so);
690a51764a8SPaul Traina 				if (so2) {
691e7f32693SJayanth Vijayaraghavan 					if (tcp_lq_overflow)
692e7f32693SJayanth Vijayaraghavan 						sototcpcb(so2)->t_flags |=
693e7f32693SJayanth Vijayaraghavan 						    TF_LQ_OVERFLOW;
694ebb0cbeaSPaul Traina 					tcp_drop(sototcpcb(so2), ETIMEDOUT);
695a51764a8SPaul Traina 					so2 = sonewconn(so, 0);
696a51764a8SPaul Traina 				}
697a51764a8SPaul Traina 				if (!so2)
698df8bae1dSRodney W. Grimes 					goto drop;
6991347f5b8SGuido van Rooij 			}
700fb59c426SYoshinobu Inoue #ifdef IPSEC
701fb59c426SYoshinobu Inoue 			oso = so;
702fb59c426SYoshinobu Inoue #endif
7034195b4afSPaul Traina 			so = so2;
704df8bae1dSRodney W. Grimes 			/*
705df8bae1dSRodney W. Grimes 			 * This is ugly, but ....
706df8bae1dSRodney W. Grimes 			 *
707df8bae1dSRodney W. Grimes 			 * Mark socket as temporary until we're
708df8bae1dSRodney W. Grimes 			 * committed to keeping it.  The code at
709df8bae1dSRodney W. Grimes 			 * ``drop'' and ``dropwithreset'' check the
710df8bae1dSRodney W. Grimes 			 * flag dropsocket to see if the temporary
711df8bae1dSRodney W. Grimes 			 * socket created here should be discarded.
712df8bae1dSRodney W. Grimes 			 * We mark the socket as discardable until
713df8bae1dSRodney W. Grimes 			 * we're committed to it below in TCPS_LISTEN.
714df8bae1dSRodney W. Grimes 			 */
715df8bae1dSRodney W. Grimes 			dropsocket++;
716df8bae1dSRodney W. Grimes 			inp = (struct inpcb *)so->so_pcb;
717fb59c426SYoshinobu Inoue #ifdef INET6
718fb59c426SYoshinobu Inoue 			if (isipv6)
719fb59c426SYoshinobu Inoue 				inp->in6p_laddr = ip6->ip6_dst;
720fb59c426SYoshinobu Inoue 			else {
721fdaf052eSYoshinobu Inoue 				if ((inp->inp_flags & IN6P_BINDV6ONLY) == 0) {
722fb59c426SYoshinobu Inoue 					inp->inp_vflag &= ~INP_IPV6;
723fb59c426SYoshinobu Inoue 					inp->inp_vflag |= INP_IPV4;
724fb59c426SYoshinobu Inoue 				}
725fb59c426SYoshinobu Inoue #endif /* INET6 */
726fb59c426SYoshinobu Inoue 			inp->inp_laddr = ip->ip_dst;
727fb59c426SYoshinobu Inoue #ifdef INET6
728fb59c426SYoshinobu Inoue 			}
729fb59c426SYoshinobu Inoue #endif /* INET6 */
730fb59c426SYoshinobu Inoue 			inp->inp_lport = th->th_dport;
731c3229e05SDavid Greenman 			if (in_pcbinshash(inp) != 0) {
732c3229e05SDavid Greenman 				/*
7335a8c77a8SDavid E. O'Brien 				 * Undo the assignments above if we failed to
7345a8c77a8SDavid E. O'Brien 				 * put the PCB on the hash lists.
735c3229e05SDavid Greenman 				 */
736fb59c426SYoshinobu Inoue #ifdef INET6
737fb59c426SYoshinobu Inoue 				if (isipv6)
738fb59c426SYoshinobu Inoue 					inp->in6p_laddr = in6addr_any;
739fb59c426SYoshinobu Inoue 				else
740fb59c426SYoshinobu Inoue #endif /* INET6 */
741c3229e05SDavid Greenman 				inp->inp_laddr.s_addr = INADDR_ANY;
742c3229e05SDavid Greenman 				inp->inp_lport = 0;
743c3229e05SDavid Greenman 				goto drop;
744c3229e05SDavid Greenman 			}
745fb59c426SYoshinobu Inoue #ifdef IPSEC
746fb59c426SYoshinobu Inoue 			/*
747fb59c426SYoshinobu Inoue 			 * To avoid creating incorrectly cached IPsec
748fb59c426SYoshinobu Inoue 			 * association, this is need to be done here.
749fb59c426SYoshinobu Inoue 			 *
750fb59c426SYoshinobu Inoue 			 * Subject: (KAME-snap 748)
751fb59c426SYoshinobu Inoue 			 * From: Wayne Knowles <w.knowles@niwa.cri.nz>
752fb59c426SYoshinobu Inoue 			 * ftp://ftp.kame.net/pub/mail-list/snap-users/748
753fb59c426SYoshinobu Inoue 			 */
754fb59c426SYoshinobu Inoue 			if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
755fb59c426SYoshinobu Inoue 				/*
756fb59c426SYoshinobu Inoue 				 * Note: dropwithreset makes sure we don't
757fb59c426SYoshinobu Inoue 				 * send a RST in response to a RST.
758fb59c426SYoshinobu Inoue 				 */
759fb59c426SYoshinobu Inoue 				if (thflags & TH_ACK) {
760fb59c426SYoshinobu Inoue 					tcpstat.tcps_badsyn++;
761a57815efSBosko Milekic 					rstreason = BANDLIM_RST_OPENPORT;
762a57815efSBosko Milekic 					goto dropwithreset;
763fb59c426SYoshinobu Inoue 				}
764fb59c426SYoshinobu Inoue 				goto drop;
765fb59c426SYoshinobu Inoue 			}
766fb59c426SYoshinobu Inoue #endif
767fb59c426SYoshinobu Inoue #ifdef INET6
768fb59c426SYoshinobu Inoue 			if (isipv6) {
769fb59c426SYoshinobu Inoue 				/*
770fb59c426SYoshinobu Inoue 				 * inherit socket options from the listening
771fb59c426SYoshinobu Inoue 				 * socket.
772fb59c426SYoshinobu Inoue 				 */
773fb59c426SYoshinobu Inoue 				inp->inp_flags |=
774fb59c426SYoshinobu Inoue 					oinp->inp_flags & INP_CONTROLOPTS;
775fb59c426SYoshinobu Inoue 				if (inp->inp_flags & INP_CONTROLOPTS) {
776fb59c426SYoshinobu Inoue 					if (inp->in6p_options) {
777fb59c426SYoshinobu Inoue 						m_freem(inp->in6p_options);
778fb59c426SYoshinobu Inoue 						inp->in6p_options = 0;
779fb59c426SYoshinobu Inoue 					}
780fb59c426SYoshinobu Inoue 					ip6_savecontrol(inp,
781fb59c426SYoshinobu Inoue 							&inp->in6p_options,
782fb59c426SYoshinobu Inoue 							ip6, m);
783fb59c426SYoshinobu Inoue 				}
784fb59c426SYoshinobu Inoue 			} else
785fb59c426SYoshinobu Inoue #endif /* INET6 */
786df8bae1dSRodney W. Grimes 			inp->inp_options = ip_srcroute();
787fb59c426SYoshinobu Inoue #ifdef IPSEC
788fb59c426SYoshinobu Inoue 			/* copy old policy into new socket's */
789fb59c426SYoshinobu Inoue 			if (ipsec_copy_policy(sotoinpcb(oso)->inp_sp,
790fb59c426SYoshinobu Inoue 			                      inp->inp_sp))
791fb59c426SYoshinobu Inoue 				printf("tcp_input: could not copy policy\n");
792fb59c426SYoshinobu Inoue #endif
793df8bae1dSRodney W. Grimes 			tp = intotcpcb(inp);
794df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_LISTEN;
795a0292f23SGarrett Wollman 			tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT);
796df8bae1dSRodney W. Grimes 
797a0292f23SGarrett Wollman 			/* Compute proper scaling value from buffer space */
798df8bae1dSRodney W. Grimes 			while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
7995a8c77a8SDavid E. O'Brien 			   TCP_MAXWIN << tp->request_r_scale <
8005a8c77a8SDavid E. O'Brien 			   so->so_rcv.sb_hiwat)
801df8bae1dSRodney W. Grimes 				tp->request_r_scale++;
802df8bae1dSRodney W. Grimes 		}
803df8bae1dSRodney W. Grimes 	}
804df8bae1dSRodney W. Grimes 
805df8bae1dSRodney W. Grimes 	/*
806df8bae1dSRodney W. Grimes 	 * Segment received on connection.
807df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
808df8bae1dSRodney W. Grimes 	 */
8099b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
8107ff19458SPaul Traina 	if (TCPS_HAVEESTABLISHED(tp->t_state))
8119b8b58e0SJonathan Lemon 		callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
812df8bae1dSRodney W. Grimes 
813df8bae1dSRodney W. Grimes 	/*
814df8bae1dSRodney W. Grimes 	 * Process options if not in LISTEN state,
815df8bae1dSRodney W. Grimes 	 * else do it below (after getting remote address).
816df8bae1dSRodney W. Grimes 	 */
817f9d5a964SDavid Greenman 	if (tp->t_state != TCPS_LISTEN)
818fb59c426SYoshinobu Inoue 		tcp_dooptions(tp, optp, optlen, th, &to);
819df8bae1dSRodney W. Grimes 
820df8bae1dSRodney W. Grimes 	/*
821df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
822df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
823df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
824df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
825df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
826df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
827df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
828df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
829df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
830df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
831df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
832df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
833a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
834a0292f23SGarrett Wollman 	 * Since we check for TCPS_ESTABLISHED above, it can only
835a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
836df8bae1dSRodney W. Grimes 	 */
837df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
838fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
839a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
840a0292f23SGarrett Wollman 	    ((to.to_flag & TOF_TS) == 0 ||
841a0292f23SGarrett Wollman 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
842a0292f23SGarrett Wollman 	    /*
843a0292f23SGarrett Wollman 	     * Using the CC option is compulsory if once started:
844a0292f23SGarrett Wollman 	     *   the segment is OK if no T/TCP was negotiated or
845a0292f23SGarrett Wollman 	     *   if the segment has a CC option equal to CCrecv
846a0292f23SGarrett Wollman 	     */
847a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) ||
848831a80b0SMatthew Dillon 	     ((to.to_flag & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) &&
849fb59c426SYoshinobu Inoue 	    th->th_seq == tp->rcv_nxt &&
850df8bae1dSRodney W. Grimes 	    tiwin && tiwin == tp->snd_wnd &&
851df8bae1dSRodney W. Grimes 	    tp->snd_nxt == tp->snd_max) {
852df8bae1dSRodney W. Grimes 
853df8bae1dSRodney W. Grimes 		/*
854df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
855df8bae1dSRodney W. Grimes 		 * record the timestamp.
856a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
857a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
858df8bae1dSRodney W. Grimes 		 */
859a0292f23SGarrett Wollman 		if ((to.to_flag & TOF_TS) != 0 &&
860fb59c426SYoshinobu Inoue 		   SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
8619b8b58e0SJonathan Lemon 			tp->ts_recent_age = ticks;
862a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
863df8bae1dSRodney W. Grimes 		}
864df8bae1dSRodney W. Grimes 
865fb59c426SYoshinobu Inoue 		if (tlen == 0) {
866fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
867fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
868233e8c18SGarrett Wollman 			    tp->snd_cwnd >= tp->snd_wnd &&
869233e8c18SGarrett Wollman 			    tp->t_dupacks < tcprexmtthresh) {
870df8bae1dSRodney W. Grimes 				/*
871df8bae1dSRodney W. Grimes 				 * this is a pure ack for outstanding data.
872df8bae1dSRodney W. Grimes 				 */
873df8bae1dSRodney W. Grimes 				++tcpstat.tcps_predack;
8749b8b58e0SJonathan Lemon 				/*
8759b8b58e0SJonathan Lemon 				 * "bad retransmit" recovery
8769b8b58e0SJonathan Lemon 				 */
8779b8b58e0SJonathan Lemon 				if (tp->t_rxtshift == 1 &&
8789b8b58e0SJonathan Lemon 				    ticks < tp->t_badrxtwin) {
8799b8b58e0SJonathan Lemon 					tp->snd_cwnd = tp->snd_cwnd_prev;
8809b8b58e0SJonathan Lemon 					tp->snd_ssthresh =
8819b8b58e0SJonathan Lemon 					    tp->snd_ssthresh_prev;
8829b8b58e0SJonathan Lemon 					tp->snd_nxt = tp->snd_max;
8839b8b58e0SJonathan Lemon 					tp->t_badrxtwin = 0;
8849b8b58e0SJonathan Lemon 				}
885a0292f23SGarrett Wollman 				if ((to.to_flag & TOF_TS) != 0)
886a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
8879b8b58e0SJonathan Lemon 					    ticks - to.to_tsecr + 1);
8889b8b58e0SJonathan Lemon 				else if (tp->t_rtttime &&
889fb59c426SYoshinobu Inoue 					    SEQ_GT(th->th_ack, tp->t_rtseq))
8909b8b58e0SJonathan Lemon 					tcp_xmit_timer(tp, ticks - tp->t_rtttime);
891fb59c426SYoshinobu Inoue 				acked = th->th_ack - tp->snd_una;
892df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackpack++;
893df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackbyte += acked;
894df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
895fb59c426SYoshinobu Inoue 				tp->snd_una = th->th_ack;
896df8bae1dSRodney W. Grimes 				m_freem(m);
897fb59c426SYoshinobu Inoue 				ND6_HINT(tp); /* some progress has been done */
898df8bae1dSRodney W. Grimes 
899df8bae1dSRodney W. Grimes 				/*
900df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
901df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
902df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
903df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
904df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
905df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
906df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
907df8bae1dSRodney W. Grimes 				 */
908df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
9099b8b58e0SJonathan Lemon 					callout_stop(tp->tt_rexmt);
9109b8b58e0SJonathan Lemon 				else if (!callout_active(tp->tt_persist))
9119b8b58e0SJonathan Lemon 					callout_reset(tp->tt_rexmt,
9129b8b58e0SJonathan Lemon 						      tp->t_rxtcur,
9139b8b58e0SJonathan Lemon 						      tcp_timer_rexmt, tp);
914df8bae1dSRodney W. Grimes 
915df8bae1dSRodney W. Grimes 				sowwakeup(so);
916df8bae1dSRodney W. Grimes 				if (so->so_snd.sb_cc)
917df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
918df8bae1dSRodney W. Grimes 				return;
919df8bae1dSRodney W. Grimes 			}
920fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
921fb59c426SYoshinobu Inoue 		    LIST_EMPTY(&tp->t_segq) &&
922fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
923df8bae1dSRodney W. Grimes 			/*
924df8bae1dSRodney W. Grimes 			 * this is a pure, in-sequence data packet
925df8bae1dSRodney W. Grimes 			 * with nothing on the reassembly queue and
926df8bae1dSRodney W. Grimes 			 * we have enough buffer space to take it.
927df8bae1dSRodney W. Grimes 			 */
928df8bae1dSRodney W. Grimes 			++tcpstat.tcps_preddat;
929fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
930df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpack++;
931fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbyte += tlen;
932fb59c426SYoshinobu Inoue 			ND6_HINT(tp);	/* some progress has been done */
933df8bae1dSRodney W. Grimes 			/*
934755c1f07SAndras Olah 			 * Add data to socket buffer.
935df8bae1dSRodney W. Grimes 			 */
936fb59c426SYoshinobu Inoue 			m_adj(m, drop_hdrlen);	/* delayed header drop */
937df8bae1dSRodney W. Grimes 			sbappend(&so->so_rcv, m);
938df8bae1dSRodney W. Grimes 			sorwakeup(so);
939d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp)) {
9409b8b58e0SJonathan Lemon 	                        callout_reset(tp->tt_delack, tcp_delacktime,
9419b8b58e0SJonathan Lemon 	                            tcp_timer_delack, tp);
942f498eeeeSDavid Greenman 			} else {
943e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
944e612a582SDavid Greenman 				tcp_output(tp);
945e612a582SDavid Greenman 			}
946df8bae1dSRodney W. Grimes 			return;
947df8bae1dSRodney W. Grimes 		}
948df8bae1dSRodney W. Grimes 	}
949df8bae1dSRodney W. Grimes 
950df8bae1dSRodney W. Grimes 	/*
951df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
952df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
953df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
954df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
955df8bae1dSRodney W. Grimes 	 */
956df8bae1dSRodney W. Grimes 	{ int win;
957df8bae1dSRodney W. Grimes 
958df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
959df8bae1dSRodney W. Grimes 	if (win < 0)
960df8bae1dSRodney W. Grimes 		win = 0;
96166e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
962df8bae1dSRodney W. Grimes 	}
963df8bae1dSRodney W. Grimes 
964df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
965df8bae1dSRodney W. Grimes 
966df8bae1dSRodney W. Grimes 	/*
967df8bae1dSRodney W. Grimes 	 * If the state is LISTEN then ignore segment if it contains an RST.
968df8bae1dSRodney W. Grimes 	 * If the segment contains an ACK then it is bad and send a RST.
969df8bae1dSRodney W. Grimes 	 * If it does not contain a SYN then it is not interesting; drop it.
970764d8cefSBill Fenner 	 * If it is from this socket, drop it, it must be forged.
971df8bae1dSRodney W. Grimes 	 * Don't bother responding if the destination was a broadcast.
972df8bae1dSRodney W. Grimes 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
973df8bae1dSRodney W. Grimes 	 * tp->iss, and send a segment:
974df8bae1dSRodney W. Grimes 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
975df8bae1dSRodney W. Grimes 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
976df8bae1dSRodney W. Grimes 	 * Fill in remote peer address fields if not previously specified.
977df8bae1dSRodney W. Grimes 	 * Enter SYN_RECEIVED state, and process any other fields of this
978df8bae1dSRodney W. Grimes 	 * segment in this state.
979df8bae1dSRodney W. Grimes 	 */
980df8bae1dSRodney W. Grimes 	case TCPS_LISTEN: {
981df8bae1dSRodney W. Grimes 		register struct sockaddr_in *sin;
982fb59c426SYoshinobu Inoue #ifdef INET6
983fb59c426SYoshinobu Inoue 		register struct sockaddr_in6 *sin6;
984fb59c426SYoshinobu Inoue #endif
985df8bae1dSRodney W. Grimes 
986fb59c426SYoshinobu Inoue 		if (thflags & TH_RST)
987df8bae1dSRodney W. Grimes 			goto drop;
988a57815efSBosko Milekic 		if (thflags & TH_ACK) {
989a57815efSBosko Milekic 			rstreason = BANDLIM_RST_OPENPORT;
990a57815efSBosko Milekic 			goto dropwithreset;
991a57815efSBosko Milekic 		}
992fb59c426SYoshinobu Inoue 		if ((thflags & TH_SYN) == 0)
993df8bae1dSRodney W. Grimes 			goto drop;
994fb59c426SYoshinobu Inoue 		if (th->th_dport == th->th_sport) {
995fb59c426SYoshinobu Inoue #ifdef INET6
996fb59c426SYoshinobu Inoue 			if (isipv6) {
997fb59c426SYoshinobu Inoue 				if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
998fb59c426SYoshinobu Inoue 						       &ip6->ip6_src))
999764d8cefSBill Fenner 					goto drop;
1000fb59c426SYoshinobu Inoue 			} else
1001fb59c426SYoshinobu Inoue #endif /* INET6 */
1002fb59c426SYoshinobu Inoue 			if (ip->ip_dst.s_addr == ip->ip_src.s_addr)
1003fb59c426SYoshinobu Inoue 				goto drop;
1004fb59c426SYoshinobu Inoue 		}
1005df8bae1dSRodney W. Grimes 		/*
1006df8bae1dSRodney W. Grimes 		 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
1007df8bae1dSRodney W. Grimes 		 * in_broadcast() should never return true on a received
1008df8bae1dSRodney W. Grimes 		 * packet with M_BCAST not set.
1009173c0f9fSWarner Losh  		 *
1010173c0f9fSWarner Losh  		 * Packets with a multicast source address should also
1011173c0f9fSWarner Losh  		 * be discarded.
1012df8bae1dSRodney W. Grimes 		 */
1013fb59c426SYoshinobu Inoue 		if (m->m_flags & (M_BCAST|M_MCAST))
1014df8bae1dSRodney W. Grimes 			goto drop;
1015fb59c426SYoshinobu Inoue #ifdef INET6
1016fb59c426SYoshinobu Inoue 		if (isipv6) {
1017173c0f9fSWarner Losh 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1018173c0f9fSWarner Losh 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
1019fb59c426SYoshinobu Inoue 				goto drop;
1020fb59c426SYoshinobu Inoue 		} else
1021fb59c426SYoshinobu Inoue #endif
1022173c0f9fSWarner Losh 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
1023173c0f9fSWarner Losh 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
1024173c0f9fSWarner Losh 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST))
1025fb59c426SYoshinobu Inoue 			goto drop;
1026fb59c426SYoshinobu Inoue #ifdef INET6
1027fb59c426SYoshinobu Inoue 		if (isipv6) {
1028fb59c426SYoshinobu Inoue 			MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6,
10297cc0979fSDavid Malone 			       M_SONAME, M_NOWAIT | M_ZERO);
1030fb59c426SYoshinobu Inoue 			if (sin6 == NULL)
1031fb59c426SYoshinobu Inoue 				goto drop;
1032fb59c426SYoshinobu Inoue 			sin6->sin6_family = AF_INET6;
1033fb59c426SYoshinobu Inoue 			sin6->sin6_len = sizeof(*sin6);
1034fb59c426SYoshinobu Inoue 			sin6->sin6_addr = ip6->ip6_src;
1035fb59c426SYoshinobu Inoue 			sin6->sin6_port = th->th_sport;
1036fb59c426SYoshinobu Inoue 			laddr6 = inp->in6p_laddr;
1037fb59c426SYoshinobu Inoue 			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1038fb59c426SYoshinobu Inoue 				inp->in6p_laddr = ip6->ip6_dst;
1039fb59c426SYoshinobu Inoue 			if (in6_pcbconnect(inp, (struct sockaddr *)sin6,
1040fb59c426SYoshinobu Inoue 					   &proc0)) {
1041fb59c426SYoshinobu Inoue 				inp->in6p_laddr = laddr6;
1042fb59c426SYoshinobu Inoue 				FREE(sin6, M_SONAME);
1043fb59c426SYoshinobu Inoue 				goto drop;
1044fb59c426SYoshinobu Inoue 			}
1045fb59c426SYoshinobu Inoue 			FREE(sin6, M_SONAME);
1046fb59c426SYoshinobu Inoue 		} else
1047fb59c426SYoshinobu Inoue #endif
1048fb59c426SYoshinobu Inoue 	      {
104957bf258eSGarrett Wollman 		MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
105057bf258eSGarrett Wollman 		       M_NOWAIT);
105157bf258eSGarrett Wollman 		if (sin == NULL)
1052df8bae1dSRodney W. Grimes 			goto drop;
1053df8bae1dSRodney W. Grimes 		sin->sin_family = AF_INET;
1054df8bae1dSRodney W. Grimes 		sin->sin_len = sizeof(*sin);
1055fb59c426SYoshinobu Inoue 		sin->sin_addr = ip->ip_src;
1056fb59c426SYoshinobu Inoue 		sin->sin_port = th->th_sport;
1057df8bae1dSRodney W. Grimes 		bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero));
1058df8bae1dSRodney W. Grimes 		laddr = inp->inp_laddr;
1059df8bae1dSRodney W. Grimes 		if (inp->inp_laddr.s_addr == INADDR_ANY)
1060fb59c426SYoshinobu Inoue 			inp->inp_laddr = ip->ip_dst;
106157bf258eSGarrett Wollman 		if (in_pcbconnect(inp, (struct sockaddr *)sin, &proc0)) {
1062df8bae1dSRodney W. Grimes 			inp->inp_laddr = laddr;
106357bf258eSGarrett Wollman 			FREE(sin, M_SONAME);
1064df8bae1dSRodney W. Grimes 			goto drop;
1065df8bae1dSRodney W. Grimes 		}
106657bf258eSGarrett Wollman 		FREE(sin, M_SONAME);
1067fb59c426SYoshinobu Inoue 	      }
1068df8bae1dSRodney W. Grimes 		tp->t_template = tcp_template(tp);
1069df8bae1dSRodney W. Grimes 		if (tp->t_template == 0) {
1070df8bae1dSRodney W. Grimes 			tp = tcp_drop(tp, ENOBUFS);
1071df8bae1dSRodney W. Grimes 			dropsocket = 0;		/* socket is already gone */
1072df8bae1dSRodney W. Grimes 			goto drop;
1073df8bae1dSRodney W. Grimes 		}
1074a0292f23SGarrett Wollman 		if ((taop = tcp_gettaocache(inp)) == NULL) {
1075a0292f23SGarrett Wollman 			taop = &tao_noncached;
1076a0292f23SGarrett Wollman 			bzero(taop, sizeof(*taop));
1077a0292f23SGarrett Wollman 		}
1078fb59c426SYoshinobu Inoue 		tcp_dooptions(tp, optp, optlen, th, &to);
1079df8bae1dSRodney W. Grimes 		if (iss)
1080df8bae1dSRodney W. Grimes 			tp->iss = iss;
1081f0a04f3fSKris Kennaway 		else {
1082f0a04f3fSKris Kennaway 			tp->iss = tcp_rndiss_next();
1083f0a04f3fSKris Kennaway  		}
1084fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
1085df8bae1dSRodney W. Grimes 		tcp_sendseqinit(tp);
1086df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
108746f58482SJonathan Lemon 		tp->snd_recover = tp->snd_una;
1088a0292f23SGarrett Wollman 		/*
1089a0292f23SGarrett Wollman 		 * Initialization of the tcpcb for transaction;
1090a0292f23SGarrett Wollman 		 *   set SND.WND = SEG.WND,
1091a0292f23SGarrett Wollman 		 *   initialize CCsend and CCrecv.
1092a0292f23SGarrett Wollman 		 */
1093a0292f23SGarrett Wollman 		tp->snd_wnd = tiwin;	/* initial send-window */
1094a0292f23SGarrett Wollman 		tp->cc_send = CC_INC(tcp_ccgen);
1095a0292f23SGarrett Wollman 		tp->cc_recv = to.to_cc;
1096a0292f23SGarrett Wollman 		/*
1097a0292f23SGarrett Wollman 		 * Perform TAO test on incoming CC (SEG.CC) option, if any.
1098a0292f23SGarrett Wollman 		 * - compare SEG.CC against cached CC from the same host,
1099a0292f23SGarrett Wollman 		 *	if any.
1100a0292f23SGarrett Wollman 		 * - if SEG.CC > chached value, SYN must be new and is accepted
1101a0292f23SGarrett Wollman 		 *	immediately: save new CC in the cache, mark the socket
1102a0292f23SGarrett Wollman 		 *	connected, enter ESTABLISHED state, turn on flag to
1103a0292f23SGarrett Wollman 		 *	send a SYN in the next segment.
1104a0292f23SGarrett Wollman 		 *	A virtual advertised window is set in rcv_adv to
1105a0292f23SGarrett Wollman 		 *	initialize SWS prevention.  Then enter normal segment
1106a0292f23SGarrett Wollman 		 *	processing: drop SYN, process data and FIN.
1107a0292f23SGarrett Wollman 		 * - otherwise do a normal 3-way handshake.
1108a0292f23SGarrett Wollman 		 */
1109a0292f23SGarrett Wollman 		if ((to.to_flag & TOF_CC) != 0) {
1110068373b6SGuido van Rooij 		    if (((tp->t_flags & TF_NOPUSH) != 0) &&
111111ad4550SGuido van Rooij 			taop->tao_cc != 0 && CC_GT(to.to_cc, taop->tao_cc)) {
111211ad4550SGuido van Rooij 
1113a0292f23SGarrett Wollman 			taop->tao_cc = to.to_cc;
11149b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
1115a0292f23SGarrett Wollman 			tp->t_state = TCPS_ESTABLISHED;
1116a0292f23SGarrett Wollman 
1117a0292f23SGarrett Wollman 			/*
1118a0292f23SGarrett Wollman 			 * If there is a FIN, or if there is data and the
1119a0292f23SGarrett Wollman 			 * connection is local, then delay SYN,ACK(SYN) in
1120a0292f23SGarrett Wollman 			 * the hope of piggy-backing it on a response
1121a0292f23SGarrett Wollman 			 * segment.  Otherwise must send ACK now in case
1122a0292f23SGarrett Wollman 			 * the other side is slow starting.
1123a0292f23SGarrett Wollman 			 */
1124d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp) && ((thflags & TH_FIN) ||
1125fb59c426SYoshinobu Inoue 			    (tlen != 0 &&
1126fb59c426SYoshinobu Inoue #ifdef INET6
1127fb59c426SYoshinobu Inoue 			      ((isipv6 && in6_localaddr(&inp->in6p_faddr))
1128fb59c426SYoshinobu Inoue 			      ||
1129fb59c426SYoshinobu Inoue 			      (!isipv6 &&
1130fb59c426SYoshinobu Inoue #endif
1131fb59c426SYoshinobu Inoue 			    in_localaddr(inp->inp_faddr)
1132fb59c426SYoshinobu Inoue #ifdef INET6
1133fb59c426SYoshinobu Inoue 			       ))
1134fb59c426SYoshinobu Inoue #endif
1135fb59c426SYoshinobu Inoue 			     ))) {
11369b8b58e0SJonathan Lemon                                 callout_reset(tp->tt_delack, tcp_delacktime,
11379b8b58e0SJonathan Lemon                                     tcp_timer_delack, tp);
11389b8b58e0SJonathan Lemon 				tp->t_flags |= TF_NEEDSYN;
11399b8b58e0SJonathan Lemon 			} else
1140a0292f23SGarrett Wollman 				tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
114107e43e10SAndras Olah 
114207e43e10SAndras Olah 			/*
114307e43e10SAndras Olah 			 * Limit the `virtual advertised window' to TCP_MAXWIN
114407e43e10SAndras Olah 			 * here.  Even if we requested window scaling, it will
114507e43e10SAndras Olah 			 * become effective only later when our SYN is acked.
114607e43e10SAndras Olah 			 */
114707e43e10SAndras Olah 			tp->rcv_adv += min(tp->rcv_wnd, TCP_MAXWIN);
1148a0292f23SGarrett Wollman 			tcpstat.tcps_connects++;
1149a0292f23SGarrett Wollman 			soisconnected(so);
11509b8b58e0SJonathan Lemon 			callout_reset(tp->tt_keep, tcp_keepinit,
11519b8b58e0SJonathan Lemon 				      tcp_timer_keep, tp);
1152a0292f23SGarrett Wollman 			dropsocket = 0;		/* committed to socket */
1153a0292f23SGarrett Wollman 			tcpstat.tcps_accepts++;
1154a0292f23SGarrett Wollman 			goto trimthenstep6;
1155a0292f23SGarrett Wollman 		    }
1156a0292f23SGarrett Wollman 		/* else do standard 3-way handshake */
1157a0292f23SGarrett Wollman 		} else {
1158a0292f23SGarrett Wollman 		    /*
1159a0292f23SGarrett Wollman 		     * No CC option, but maybe CC.NEW:
1160a0292f23SGarrett Wollman 		     *   invalidate cached value.
1161a0292f23SGarrett Wollman 		     */
1162a0292f23SGarrett Wollman 		     taop->tao_cc = 0;
1163a0292f23SGarrett Wollman 		}
1164a0292f23SGarrett Wollman 		/*
1165a0292f23SGarrett Wollman 		 * TAO test failed or there was no CC option,
1166a0292f23SGarrett Wollman 		 *    do a standard 3-way handshake.
1167a0292f23SGarrett Wollman 		 */
1168df8bae1dSRodney W. Grimes 		tp->t_flags |= TF_ACKNOW;
1169df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_SYN_RECEIVED;
11709b8b58e0SJonathan Lemon 		callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
1171df8bae1dSRodney W. Grimes 		dropsocket = 0;		/* committed to socket */
1172df8bae1dSRodney W. Grimes 		tcpstat.tcps_accepts++;
1173df8bae1dSRodney W. Grimes 		goto trimthenstep6;
1174df8bae1dSRodney W. Grimes 		}
1175df8bae1dSRodney W. Grimes 
1176df8bae1dSRodney W. Grimes 	/*
1177764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1178764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1179764d8cefSBill Fenner 	 */
1180764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1181fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1182fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1183a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1184a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1185a57815efSBosko Milekic 				goto dropwithreset;
1186a57815efSBosko Milekic 		}
1187764d8cefSBill Fenner 		break;
1188764d8cefSBill Fenner 
1189764d8cefSBill Fenner 	/*
1190df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
1191df8bae1dSRodney W. Grimes 	 *	if seg contains an ACK, but not for our SYN, drop the input.
1192df8bae1dSRodney W. Grimes 	 *	if seg contains a RST, then drop the connection.
1193df8bae1dSRodney W. Grimes 	 *	if seg does not contain SYN, then drop it.
1194df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
1195df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
1196df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
1197df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1198df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
1199df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
1200df8bae1dSRodney W. Grimes 	 */
1201df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
1202a0292f23SGarrett Wollman 		if ((taop = tcp_gettaocache(inp)) == NULL) {
1203a0292f23SGarrett Wollman 			taop = &tao_noncached;
1204a0292f23SGarrett Wollman 			bzero(taop, sizeof(*taop));
1205a0292f23SGarrett Wollman 		}
1206a0292f23SGarrett Wollman 
1207fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1208fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
1209fb59c426SYoshinobu Inoue 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1210a0292f23SGarrett Wollman 			/*
1211a0292f23SGarrett Wollman 			 * If we have a cached CCsent for the remote host,
1212a0292f23SGarrett Wollman 			 * hence we haven't just crashed and restarted,
1213a0292f23SGarrett Wollman 			 * do not send a RST.  This may be a retransmission
1214a0292f23SGarrett Wollman 			 * from the other side after our earlier ACK was lost.
1215a0292f23SGarrett Wollman 			 * Our new SYN, when it arrives, will serve as the
1216a0292f23SGarrett Wollman 			 * needed ACK.
1217a0292f23SGarrett Wollman 			 */
1218a0292f23SGarrett Wollman 			if (taop->tao_ccsent != 0)
1219a0292f23SGarrett Wollman 				goto drop;
1220a57815efSBosko Milekic 			else {
1221a57815efSBosko Milekic 				rstreason = BANDLIM_UNLIMITED;
1222a0292f23SGarrett Wollman 				goto dropwithreset;
1223a0292f23SGarrett Wollman 			}
1224a57815efSBosko Milekic 		}
1225fb59c426SYoshinobu Inoue 		if (thflags & TH_RST) {
1226fb59c426SYoshinobu Inoue 			if (thflags & TH_ACK)
1227df8bae1dSRodney W. Grimes 				tp = tcp_drop(tp, ECONNREFUSED);
1228df8bae1dSRodney W. Grimes 			goto drop;
1229df8bae1dSRodney W. Grimes 		}
1230fb59c426SYoshinobu Inoue 		if ((thflags & TH_SYN) == 0)
1231df8bae1dSRodney W. Grimes 			goto drop;
1232fb59c426SYoshinobu Inoue 		tp->snd_wnd = th->th_win;	/* initial send window */
1233a0292f23SGarrett Wollman 		tp->cc_recv = to.to_cc;		/* foreign CC */
1234a0292f23SGarrett Wollman 
1235fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
1236df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
1237fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1238a0292f23SGarrett Wollman 			/*
1239a0292f23SGarrett Wollman 			 * Our SYN was acked.  If segment contains CC.ECHO
1240a0292f23SGarrett Wollman 			 * option, check it to make sure this segment really
1241a0292f23SGarrett Wollman 			 * matches our SYN.  If not, just drop it as old
1242a0292f23SGarrett Wollman 			 * duplicate, but send an RST if we're still playing
1243026650e5SBill Fenner 			 * by the old rules.  If no CC.ECHO option, make sure
1244026650e5SBill Fenner 			 * we don't get fooled into using T/TCP.
1245a0292f23SGarrett Wollman 			 */
1246026650e5SBill Fenner 			if (to.to_flag & TOF_CCECHO) {
1247dfd5dee1SPeter Wemm 				if (tp->cc_send != to.to_ccecho) {
1248a0292f23SGarrett Wollman 					if (taop->tao_ccsent != 0)
1249a0292f23SGarrett Wollman 						goto drop;
1250a57815efSBosko Milekic 					else {
1251a57815efSBosko Milekic 						rstreason = BANDLIM_UNLIMITED;
1252a0292f23SGarrett Wollman 						goto dropwithreset;
1253dfd5dee1SPeter Wemm 					}
1254a57815efSBosko Milekic 				}
1255026650e5SBill Fenner 			} else
1256026650e5SBill Fenner 				tp->t_flags &= ~TF_RCVD_CC;
1257845799c1SAndras Olah 			tcpstat.tcps_connects++;
1258845799c1SAndras Olah 			soisconnected(so);
1259845799c1SAndras Olah 			/* Do window scaling on this connection? */
1260845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1261845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1262845799c1SAndras Olah 				tp->snd_scale = tp->requested_s_scale;
1263845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
1264845799c1SAndras Olah 			}
1265a0292f23SGarrett Wollman 			/* Segment is acceptable, update cache if undefined. */
1266a0292f23SGarrett Wollman 			if (taop->tao_ccsent == 0)
1267a0292f23SGarrett Wollman 				taop->tao_ccsent = to.to_ccecho;
1268a0292f23SGarrett Wollman 
1269a0292f23SGarrett Wollman 			tp->rcv_adv += tp->rcv_wnd;
1270a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
1271a0292f23SGarrett Wollman 			/*
1272a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
1273a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
1274a0292f23SGarrett Wollman 			 */
1275d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp) && tlen != 0)
12769b8b58e0SJonathan Lemon                                 callout_reset(tp->tt_delack, tcp_delacktime,
12779b8b58e0SJonathan Lemon                                     tcp_timer_delack, tp);
1278a0292f23SGarrett Wollman 			else
1279a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
1280a0292f23SGarrett Wollman 			/*
1281a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
1282a0292f23SGarrett Wollman 			 * Transitions:
1283a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
1284a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
1285a0292f23SGarrett Wollman 			 */
12869b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
1287a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
1288a0292f23SGarrett Wollman 				tp->t_state = TCPS_FIN_WAIT_1;
1289a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
1290fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
12917ff19458SPaul Traina 			} else {
1292a0292f23SGarrett Wollman 				tp->t_state = TCPS_ESTABLISHED;
12939b8b58e0SJonathan Lemon 				callout_reset(tp->tt_keep, tcp_keepidle,
12949b8b58e0SJonathan Lemon 					      tcp_timer_keep, tp);
12957ff19458SPaul Traina 			}
1296a0292f23SGarrett Wollman 		} else {
1297a0292f23SGarrett Wollman 		/*
1298a0292f23SGarrett Wollman 		 *  Received initial SYN in SYN-SENT[*] state => simul-
1299a0292f23SGarrett Wollman 		 *  taneous open.  If segment contains CC option and there is
1300a0292f23SGarrett Wollman 		 *  a cached CC, apply TAO test; if it succeeds, connection is
1301a0292f23SGarrett Wollman 		 *  half-synchronized.  Otherwise, do 3-way handshake:
1302a0292f23SGarrett Wollman 		 *        SYN-SENT -> SYN-RECEIVED
1303a0292f23SGarrett Wollman 		 *        SYN-SENT* -> SYN-RECEIVED*
1304a0292f23SGarrett Wollman 		 *  If there was no CC option, clear cached CC value.
1305a0292f23SGarrett Wollman 		 */
1306a0292f23SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
13079b8b58e0SJonathan Lemon 			callout_stop(tp->tt_rexmt);
1308a0292f23SGarrett Wollman 			if (to.to_flag & TOF_CC) {
1309a0292f23SGarrett Wollman 				if (taop->tao_cc != 0 &&
1310a0292f23SGarrett Wollman 				    CC_GT(to.to_cc, taop->tao_cc)) {
1311a0292f23SGarrett Wollman 					/*
1312a0292f23SGarrett Wollman 					 * update cache and make transition:
1313a0292f23SGarrett Wollman 					 *        SYN-SENT -> ESTABLISHED*
1314a0292f23SGarrett Wollman 					 *        SYN-SENT* -> FIN-WAIT-1*
1315a0292f23SGarrett Wollman 					 */
1316a0292f23SGarrett Wollman 					taop->tao_cc = to.to_cc;
13179b8b58e0SJonathan Lemon 					tp->t_starttime = ticks;
1318a0292f23SGarrett Wollman 					if (tp->t_flags & TF_NEEDFIN) {
1319a0292f23SGarrett Wollman 						tp->t_state = TCPS_FIN_WAIT_1;
1320a0292f23SGarrett Wollman 						tp->t_flags &= ~TF_NEEDFIN;
13217ff19458SPaul Traina 					} else {
1322a0292f23SGarrett Wollman 						tp->t_state = TCPS_ESTABLISHED;
13239b8b58e0SJonathan Lemon 						callout_reset(tp->tt_keep,
13249b8b58e0SJonathan Lemon 							      tcp_keepidle,
13259b8b58e0SJonathan Lemon 							      tcp_timer_keep,
13269b8b58e0SJonathan Lemon 							      tp);
13277ff19458SPaul Traina 					}
1328a0292f23SGarrett Wollman 					tp->t_flags |= TF_NEEDSYN;
1329df8bae1dSRodney W. Grimes 				} else
1330df8bae1dSRodney W. Grimes 					tp->t_state = TCPS_SYN_RECEIVED;
1331a0292f23SGarrett Wollman 			} else {
1332a0292f23SGarrett Wollman 				/* CC.NEW or no option => invalidate cache */
1333a0292f23SGarrett Wollman 				taop->tao_cc = 0;
1334a0292f23SGarrett Wollman 				tp->t_state = TCPS_SYN_RECEIVED;
1335a0292f23SGarrett Wollman 			}
1336a0292f23SGarrett Wollman 		}
1337df8bae1dSRodney W. Grimes 
1338df8bae1dSRodney W. Grimes trimthenstep6:
1339df8bae1dSRodney W. Grimes 		/*
1340fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
1341df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
1342df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
1343df8bae1dSRodney W. Grimes 		 */
1344fb59c426SYoshinobu Inoue 		th->th_seq++;
1345fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
1346fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
1347df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
1348fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
1349fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
1350df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpackafterwin++;
1351df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
1352df8bae1dSRodney W. Grimes 		}
1353fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
1354fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
1355a0292f23SGarrett Wollman 		/*
1356a0292f23SGarrett Wollman 		 *  Client side of transaction: already sent SYN and data.
1357a0292f23SGarrett Wollman 		 *  If the remote host used T/TCP to validate the SYN,
1358a0292f23SGarrett Wollman 		 *  our data will be ACK'd; if so, enter normal data segment
1359a0292f23SGarrett Wollman 		 *  processing in the middle of step 5, ack processing.
1360a0292f23SGarrett Wollman 		 *  Otherwise, goto step 6.
1361a0292f23SGarrett Wollman 		 */
1362fb59c426SYoshinobu Inoue  		if (thflags & TH_ACK)
1363a0292f23SGarrett Wollman 			goto process_ACK;
1364df8bae1dSRodney W. Grimes 		goto step6;
1365a0292f23SGarrett Wollman 	/*
1366a0292f23SGarrett Wollman 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
1367a0292f23SGarrett Wollman 	 *	if segment contains a SYN and CC [not CC.NEW] option:
1368a0292f23SGarrett Wollman 	 *              if state == TIME_WAIT and connection duration > MSL,
1369a0292f23SGarrett Wollman 	 *                  drop packet and send RST;
1370a0292f23SGarrett Wollman 	 *
1371a0292f23SGarrett Wollman 	 *		if SEG.CC > CCrecv then is new SYN, and can implicitly
1372a0292f23SGarrett Wollman 	 *		    ack the FIN (and data) in retransmission queue.
1373a0292f23SGarrett Wollman 	 *                  Complete close and delete TCPCB.  Then reprocess
1374a0292f23SGarrett Wollman 	 *                  segment, hoping to find new TCPCB in LISTEN state;
1375a0292f23SGarrett Wollman 	 *
1376a0292f23SGarrett Wollman 	 *		else must be old SYN; drop it.
1377a0292f23SGarrett Wollman 	 *      else do normal processing.
1378a0292f23SGarrett Wollman 	 */
1379a0292f23SGarrett Wollman 	case TCPS_LAST_ACK:
1380a0292f23SGarrett Wollman 	case TCPS_CLOSING:
1381a0292f23SGarrett Wollman 	case TCPS_TIME_WAIT:
1382fb59c426SYoshinobu Inoue 		if ((thflags & TH_SYN) &&
1383a0292f23SGarrett Wollman 		    (to.to_flag & TOF_CC) && tp->cc_recv != 0) {
1384a0292f23SGarrett Wollman 			if (tp->t_state == TCPS_TIME_WAIT &&
1385a57815efSBosko Milekic 					(ticks - tp->t_starttime) > tcp_msl) {
1386a57815efSBosko Milekic 				rstreason = BANDLIM_UNLIMITED;
1387a0292f23SGarrett Wollman 				goto dropwithreset;
1388a57815efSBosko Milekic 			}
1389a0292f23SGarrett Wollman 			if (CC_GT(to.to_cc, tp->cc_recv)) {
1390a0292f23SGarrett Wollman 				tp = tcp_close(tp);
1391a0292f23SGarrett Wollman 				goto findpcb;
1392a0292f23SGarrett Wollman 			}
1393a0292f23SGarrett Wollman 			else
1394a0292f23SGarrett Wollman 				goto drop;
1395a0292f23SGarrett Wollman 		}
1396a0292f23SGarrett Wollman  		break;  /* continue normal processing */
1397df8bae1dSRodney W. Grimes 	}
1398df8bae1dSRodney W. Grimes 
1399df8bae1dSRodney W. Grimes 	/*
1400df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
140180ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
140280ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
140380ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
140480ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
140580ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
140680ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
140780ab7c0eSGarrett Wollman 	 * killing a connection).
140880ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
1409a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
1410df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
1411df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
1412df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
1413df8bae1dSRodney W. Grimes 	 *
141480ab7c0eSGarrett Wollman 	 *
141580ab7c0eSGarrett Wollman 	 * If the RST bit is set, check the sequence number to see
141680ab7c0eSGarrett Wollman 	 * if this is a valid reset segment.
141780ab7c0eSGarrett Wollman 	 * RFC 793 page 37:
141880ab7c0eSGarrett Wollman 	 *   In all states except SYN-SENT, all reset (RST) segments
141980ab7c0eSGarrett Wollman 	 *   are validated by checking their SEQ-fields.  A reset is
142080ab7c0eSGarrett Wollman 	 *   valid if its sequence number is in the window.
142180ab7c0eSGarrett Wollman 	 * Note: this does not take into account delayed ACKs, so
142280ab7c0eSGarrett Wollman 	 *   we should test against last_ack_sent instead of rcv_nxt.
14231a244a61SJonathan Lemon 	 *   The sequence number in the reset segment is normally an
14241a244a61SJonathan Lemon 	 *   echo of our outgoing acknowlegement numbers, but some hosts
14251a244a61SJonathan Lemon 	 *   send a reset with the sequence number at the rightmost edge
14261a244a61SJonathan Lemon 	 *   of our receive window, and we have to handle this case.
142780ab7c0eSGarrett Wollman 	 * If we have multiple segments in flight, the intial reset
142880ab7c0eSGarrett Wollman 	 * segment sequence numbers will be to the left of last_ack_sent,
142980ab7c0eSGarrett Wollman 	 * but they will eventually catch up.
143080ab7c0eSGarrett Wollman 	 * In any case, it never made sense to trim reset segments to
143180ab7c0eSGarrett Wollman 	 * fit the receive window since RFC 1122 says:
143280ab7c0eSGarrett Wollman 	 *   4.2.2.12  RST Segment: RFC-793 Section 3.4
143380ab7c0eSGarrett Wollman 	 *
143480ab7c0eSGarrett Wollman 	 *    A TCP SHOULD allow a received RST segment to include data.
143580ab7c0eSGarrett Wollman 	 *
143680ab7c0eSGarrett Wollman 	 *    DISCUSSION
143780ab7c0eSGarrett Wollman 	 *         It has been suggested that a RST segment could contain
143880ab7c0eSGarrett Wollman 	 *         ASCII text that encoded and explained the cause of the
143980ab7c0eSGarrett Wollman 	 *         RST.  No standard has yet been established for such
144080ab7c0eSGarrett Wollman 	 *         data.
144180ab7c0eSGarrett Wollman 	 *
144280ab7c0eSGarrett Wollman 	 * If the reset segment passes the sequence number test examine
144380ab7c0eSGarrett Wollman 	 * the state:
144480ab7c0eSGarrett Wollman 	 *    SYN_RECEIVED STATE:
144580ab7c0eSGarrett Wollman 	 *	If passive open, return to LISTEN state.
144680ab7c0eSGarrett Wollman 	 *	If active open, inform user that connection was refused.
144780ab7c0eSGarrett Wollman 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
144880ab7c0eSGarrett Wollman 	 *	Inform user that connection was reset, and close tcb.
1449e9bd3a37SJonathan M. Bresler 	 *    CLOSING, LAST_ACK STATES:
145080ab7c0eSGarrett Wollman 	 *	Close the tcb.
1451e9bd3a37SJonathan M. Bresler 	 *    TIME_WAIT STATE:
145280ab7c0eSGarrett Wollman 	 *	Drop the segment - see Stevens, vol. 2, p. 964 and
145380ab7c0eSGarrett Wollman 	 *      RFC 1337.
145480ab7c0eSGarrett Wollman 	 */
1455fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
1456fb59c426SYoshinobu Inoue 		if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
1457fb59c426SYoshinobu Inoue 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
145880ab7c0eSGarrett Wollman 			switch (tp->t_state) {
145980ab7c0eSGarrett Wollman 
146080ab7c0eSGarrett Wollman 			case TCPS_SYN_RECEIVED:
146180ab7c0eSGarrett Wollman 				so->so_error = ECONNREFUSED;
146280ab7c0eSGarrett Wollman 				goto close;
146380ab7c0eSGarrett Wollman 
146480ab7c0eSGarrett Wollman 			case TCPS_ESTABLISHED:
146580ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_1:
146680ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_2:
146780ab7c0eSGarrett Wollman 			case TCPS_CLOSE_WAIT:
146880ab7c0eSGarrett Wollman 				so->so_error = ECONNRESET;
146980ab7c0eSGarrett Wollman 			close:
147080ab7c0eSGarrett Wollman 				tp->t_state = TCPS_CLOSED;
147180ab7c0eSGarrett Wollman 				tcpstat.tcps_drops++;
147280ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
147380ab7c0eSGarrett Wollman 				break;
147480ab7c0eSGarrett Wollman 
147580ab7c0eSGarrett Wollman 			case TCPS_CLOSING:
147680ab7c0eSGarrett Wollman 			case TCPS_LAST_ACK:
147780ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
147880ab7c0eSGarrett Wollman 				break;
147980ab7c0eSGarrett Wollman 
148080ab7c0eSGarrett Wollman 			case TCPS_TIME_WAIT:
148180ab7c0eSGarrett Wollman 				break;
148280ab7c0eSGarrett Wollman 			}
148380ab7c0eSGarrett Wollman 		}
148480ab7c0eSGarrett Wollman 		goto drop;
148580ab7c0eSGarrett Wollman 	}
148680ab7c0eSGarrett Wollman 
148780ab7c0eSGarrett Wollman 	/*
1488df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
1489df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
1490df8bae1dSRodney W. Grimes 	 */
149180ab7c0eSGarrett Wollman 	if ((to.to_flag & TOF_TS) != 0 && tp->ts_recent &&
149280ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
1493df8bae1dSRodney W. Grimes 
1494df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
14959b8b58e0SJonathan Lemon 		if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) {
1496df8bae1dSRodney W. Grimes 			/*
1497df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
1498df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
1499df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
1500df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
1501df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
1502df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
1503df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
1504df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
1505df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
1506df8bae1dSRodney W. Grimes 			 */
1507df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
1508df8bae1dSRodney W. Grimes 		} else {
1509df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvduppack++;
1510fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvdupbyte += tlen;
1511df8bae1dSRodney W. Grimes 			tcpstat.tcps_pawsdrop++;
1512df8bae1dSRodney W. Grimes 			goto dropafterack;
1513df8bae1dSRodney W. Grimes 		}
1514df8bae1dSRodney W. Grimes 	}
1515df8bae1dSRodney W. Grimes 
1516a0292f23SGarrett Wollman 	/*
1517a0292f23SGarrett Wollman 	 * T/TCP mechanism
1518a0292f23SGarrett Wollman 	 *   If T/TCP was negotiated and the segment doesn't have CC,
1519dc733423SDag-Erling Smørgrav 	 *   or if its CC is wrong then drop the segment.
1520a0292f23SGarrett Wollman 	 *   RST segments do not have to comply with this.
1521a0292f23SGarrett Wollman 	 */
1522a0292f23SGarrett Wollman 	if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) &&
152380ab7c0eSGarrett Wollman 	    ((to.to_flag & TOF_CC) == 0 || tp->cc_recv != to.to_cc))
1524a0292f23SGarrett Wollman  		goto dropafterack;
1525a0292f23SGarrett Wollman 
152680ab7c0eSGarrett Wollman 	/*
152780ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
152880ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
152980ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
153080ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
153180ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
153280ab7c0eSGarrett Wollman 	 */
1533a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
1534a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
1535a57815efSBosko Milekic 		goto dropwithreset;
1536a57815efSBosko Milekic 	}
153780ab7c0eSGarrett Wollman 
1538fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
1539df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1540fb59c426SYoshinobu Inoue 		if (thflags & TH_SYN) {
1541fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
1542fb59c426SYoshinobu Inoue 			th->th_seq++;
1543fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
1544fb59c426SYoshinobu Inoue 				th->th_urp--;
1545df8bae1dSRodney W. Grimes 			else
1546fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
1547df8bae1dSRodney W. Grimes 			todrop--;
1548df8bae1dSRodney W. Grimes 		}
1549df8bae1dSRodney W. Grimes 		/*
1550dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
1551df8bae1dSRodney W. Grimes 		 */
1552fb59c426SYoshinobu Inoue 		if (todrop > tlen
1553fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
1554dac20301SGarrett Wollman 			/*
1555dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
1556dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
1557dac20301SGarrett Wollman 			 * of sequence; drop it.
1558dac20301SGarrett Wollman 			 */
1559fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
1560dac20301SGarrett Wollman 
1561df8bae1dSRodney W. Grimes 			/*
1562dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
1563dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
1564df8bae1dSRodney W. Grimes 			 */
1565dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
1566fb59c426SYoshinobu Inoue 			todrop = tlen;
1567dac20301SGarrett Wollman 			tcpstat.tcps_rcvduppack++;
1568dac20301SGarrett Wollman 			tcpstat.tcps_rcvdupbyte += todrop;
1569df8bae1dSRodney W. Grimes 		} else {
1570df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartduppack++;
1571df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartdupbyte += todrop;
1572df8bae1dSRodney W. Grimes 		}
1573fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
1574fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
1575fb59c426SYoshinobu Inoue 		tlen -= todrop;
1576fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
1577fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
1578df8bae1dSRodney W. Grimes 		else {
1579fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
1580fb59c426SYoshinobu Inoue 			th->th_urp = 0;
1581df8bae1dSRodney W. Grimes 		}
1582df8bae1dSRodney W. Grimes 	}
1583df8bae1dSRodney W. Grimes 
1584df8bae1dSRodney W. Grimes 	/*
1585df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
1586df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
1587df8bae1dSRodney W. Grimes 	 */
1588df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
1589fb59c426SYoshinobu Inoue 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
1590df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1591df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvafterclose++;
1592a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
1593df8bae1dSRodney W. Grimes 		goto dropwithreset;
1594df8bae1dSRodney W. Grimes 	}
1595df8bae1dSRodney W. Grimes 
1596df8bae1dSRodney W. Grimes 	/*
1597df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
1598df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
1599df8bae1dSRodney W. Grimes 	 */
1600fb59c426SYoshinobu Inoue 	todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd);
1601df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1602df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvpackafterwin++;
1603fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
1604fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbyteafterwin += tlen;
1605df8bae1dSRodney W. Grimes 			/*
1606df8bae1dSRodney W. Grimes 			 * If a new connection request is received
1607df8bae1dSRodney W. Grimes 			 * while in TIME_WAIT, drop the old connection
1608df8bae1dSRodney W. Grimes 			 * and start over if the sequence numbers
1609df8bae1dSRodney W. Grimes 			 * are above the previous ones.
1610df8bae1dSRodney W. Grimes 			 */
1611fb59c426SYoshinobu Inoue 			if (thflags & TH_SYN &&
1612df8bae1dSRodney W. Grimes 			    tp->t_state == TCPS_TIME_WAIT &&
1613fb59c426SYoshinobu Inoue 			    SEQ_GT(th->th_seq, tp->rcv_nxt)) {
1614f0a04f3fSKris Kennaway 				iss = tcp_rndiss_next();
1615df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
1616df8bae1dSRodney W. Grimes 				goto findpcb;
1617df8bae1dSRodney W. Grimes 			}
1618df8bae1dSRodney W. Grimes 			/*
1619df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
1620df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
1621df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
1622df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
1623df8bae1dSRodney W. Grimes 			 * and ack.
1624df8bae1dSRodney W. Grimes 			 */
1625fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
1626df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
1627df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvwinprobe++;
1628df8bae1dSRodney W. Grimes 			} else
1629df8bae1dSRodney W. Grimes 				goto dropafterack;
1630df8bae1dSRodney W. Grimes 		} else
1631df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
1632df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
1633fb59c426SYoshinobu Inoue 		tlen -= todrop;
1634fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
1635df8bae1dSRodney W. Grimes 	}
1636df8bae1dSRodney W. Grimes 
1637df8bae1dSRodney W. Grimes 	/*
1638df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
1639df8bae1dSRodney W. Grimes 	 * record its timestamp.
1640a0292f23SGarrett Wollman 	 * NOTE that the test is modified according to the latest
1641a0292f23SGarrett Wollman 	 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1642df8bae1dSRodney W. Grimes 	 */
1643a0292f23SGarrett Wollman 	if ((to.to_flag & TOF_TS) != 0 &&
1644fb59c426SYoshinobu Inoue 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
16459b8b58e0SJonathan Lemon 		tp->ts_recent_age = ticks;
1646a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
1647df8bae1dSRodney W. Grimes 	}
1648df8bae1dSRodney W. Grimes 
1649df8bae1dSRodney W. Grimes 	/*
1650df8bae1dSRodney W. Grimes 	 * If a SYN is in the window, then this is an
1651df8bae1dSRodney W. Grimes 	 * error and we send an RST and drop the connection.
1652df8bae1dSRodney W. Grimes 	 */
1653fb59c426SYoshinobu Inoue 	if (thflags & TH_SYN) {
1654df8bae1dSRodney W. Grimes 		tp = tcp_drop(tp, ECONNRESET);
1655a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
1656df8bae1dSRodney W. Grimes 		goto dropwithreset;
1657df8bae1dSRodney W. Grimes 	}
1658df8bae1dSRodney W. Grimes 
1659a0292f23SGarrett Wollman 	/*
1660a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
1661a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
1662a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
1663a0292f23SGarrett Wollman 	 */
1664fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
1665a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
1666a0292f23SGarrett Wollman 		    (tp->t_flags & TF_NEEDSYN))
1667a0292f23SGarrett Wollman 			goto step6;
1668a0292f23SGarrett Wollman 		else
1669a0292f23SGarrett Wollman 			goto drop;
1670a0292f23SGarrett Wollman 	}
1671df8bae1dSRodney W. Grimes 
1672df8bae1dSRodney W. Grimes 	/*
1673df8bae1dSRodney W. Grimes 	 * Ack processing.
1674df8bae1dSRodney W. Grimes 	 */
1675df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1676df8bae1dSRodney W. Grimes 
1677df8bae1dSRodney W. Grimes 	/*
1678764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
1679764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
1680764d8cefSBill Fenner 	 * The ACK was checked above.
1681df8bae1dSRodney W. Grimes 	 */
1682df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1683a0292f23SGarrett Wollman 
1684df8bae1dSRodney W. Grimes 		tcpstat.tcps_connects++;
1685df8bae1dSRodney W. Grimes 		soisconnected(so);
1686df8bae1dSRodney W. Grimes 		/* Do window scaling? */
1687df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1688df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1689df8bae1dSRodney W. Grimes 			tp->snd_scale = tp->requested_s_scale;
1690df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
1691df8bae1dSRodney W. Grimes 		}
1692a0292f23SGarrett Wollman 		/*
1693a0292f23SGarrett Wollman 		 * Upon successful completion of 3-way handshake,
1694a0292f23SGarrett Wollman 		 * update cache.CC if it was undefined, pass any queued
1695a0292f23SGarrett Wollman 		 * data to the user, and advance state appropriately.
1696a0292f23SGarrett Wollman 		 */
1697a0292f23SGarrett Wollman 		if ((taop = tcp_gettaocache(inp)) != NULL &&
1698a0292f23SGarrett Wollman 		    taop->tao_cc == 0)
1699a0292f23SGarrett Wollman 			taop->tao_cc = tp->cc_recv;
1700a0292f23SGarrett Wollman 
1701a0292f23SGarrett Wollman 		/*
1702a0292f23SGarrett Wollman 		 * Make transitions:
1703a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
1704a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
1705a0292f23SGarrett Wollman 		 */
17069b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
1707a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDFIN) {
1708a0292f23SGarrett Wollman 			tp->t_state = TCPS_FIN_WAIT_1;
1709a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDFIN;
17107ff19458SPaul Traina 		} else {
1711a0292f23SGarrett Wollman 			tp->t_state = TCPS_ESTABLISHED;
17129b8b58e0SJonathan Lemon 			callout_reset(tp->tt_keep, tcp_keepidle,
17139b8b58e0SJonathan Lemon 				      tcp_timer_keep, tp);
17147ff19458SPaul Traina 		}
1715a0292f23SGarrett Wollman 		/*
1716a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
1717a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
1718a0292f23SGarrett Wollman 		 */
1719fb59c426SYoshinobu Inoue 		if (tlen == 0 && (thflags & TH_FIN) == 0)
1720fb59c426SYoshinobu Inoue 			(void) tcp_reass(tp, (struct tcphdr *)0, 0,
1721a0292f23SGarrett Wollman 			    (struct mbuf *)0);
1722fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
1723df8bae1dSRodney W. Grimes 		/* fall into ... */
1724df8bae1dSRodney W. Grimes 
1725df8bae1dSRodney W. Grimes 	/*
1726df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1727df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
1728fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
1729fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
1730df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
1731df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
1732df8bae1dSRodney W. Grimes 	 */
1733df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1734df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
1735df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
1736df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1737df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
1738df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
1739df8bae1dSRodney W. Grimes 	case TCPS_TIME_WAIT:
1740df8bae1dSRodney W. Grimes 
1741fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
1742fb59c426SYoshinobu Inoue 			if (tlen == 0 && tiwin == tp->snd_wnd) {
1743df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvdupack++;
1744df8bae1dSRodney W. Grimes 				/*
1745df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
1746df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
1747df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
1748df8bae1dSRodney W. Grimes 				 * change), the ack is the biggest we've
1749df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
1750df8bae1dSRodney W. Grimes 				 * threshhold of them, assume a packet
1751df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
1752df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
1753df8bae1dSRodney W. Grimes 				 * window so we send only this one
1754df8bae1dSRodney W. Grimes 				 * packet.
1755df8bae1dSRodney W. Grimes 				 *
1756df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
1757df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
1758df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
1759df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
1760df8bae1dSRodney W. Grimes 				 * the new ssthresh).
1761df8bae1dSRodney W. Grimes 				 *
1762df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
1763df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
1764df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
1765df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
1766df8bae1dSRodney W. Grimes 				 * network.
1767df8bae1dSRodney W. Grimes 				 */
17689b8b58e0SJonathan Lemon 				if (!callout_active(tp->tt_rexmt) ||
1769fb59c426SYoshinobu Inoue 				    th->th_ack != tp->snd_una)
1770df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
1771df8bae1dSRodney W. Grimes 				else if (++tp->t_dupacks == tcprexmtthresh) {
1772df8bae1dSRodney W. Grimes 					tcp_seq onxt = tp->snd_nxt;
1773df8bae1dSRodney W. Grimes 					u_int win =
1774df8bae1dSRodney W. Grimes 					    min(tp->snd_wnd, tp->snd_cwnd) / 2 /
1775df8bae1dSRodney W. Grimes 						tp->t_maxseg;
177646f58482SJonathan Lemon 					if (tcp_do_newreno && SEQ_LT(th->th_ack,
177746f58482SJonathan Lemon 					    tp->snd_recover)) {
177846f58482SJonathan Lemon 						/* False retransmit, should not
177946f58482SJonathan Lemon 						 * cut window
178046f58482SJonathan Lemon 						 */
178146f58482SJonathan Lemon 						tp->snd_cwnd += tp->t_maxseg;
178246f58482SJonathan Lemon 						tp->t_dupacks = 0;
178346f58482SJonathan Lemon 						(void) tcp_output(tp);
178446f58482SJonathan Lemon 						goto drop;
178546f58482SJonathan Lemon 					}
1786df8bae1dSRodney W. Grimes 					if (win < 2)
1787df8bae1dSRodney W. Grimes 						win = 2;
1788df8bae1dSRodney W. Grimes 					tp->snd_ssthresh = win * tp->t_maxseg;
178946f58482SJonathan Lemon 					tp->snd_recover = tp->snd_max;
17909b8b58e0SJonathan Lemon 					callout_stop(tp->tt_rexmt);
17919b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
1792fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
1793df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->t_maxseg;
1794df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1795df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
1796df8bae1dSRodney W. Grimes 					       tp->t_maxseg * tp->t_dupacks;
1797df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
1798df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
1799df8bae1dSRodney W. Grimes 					goto drop;
1800df8bae1dSRodney W. Grimes 				} else if (tp->t_dupacks > tcprexmtthresh) {
1801df8bae1dSRodney W. Grimes 					tp->snd_cwnd += tp->t_maxseg;
1802df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1803df8bae1dSRodney W. Grimes 					goto drop;
1804df8bae1dSRodney W. Grimes 				}
1805df8bae1dSRodney W. Grimes 			} else
1806df8bae1dSRodney W. Grimes 				tp->t_dupacks = 0;
1807df8bae1dSRodney W. Grimes 			break;
1808df8bae1dSRodney W. Grimes 		}
1809df8bae1dSRodney W. Grimes 		/*
1810df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
1811df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
1812df8bae1dSRodney W. Grimes 		 */
181346f58482SJonathan Lemon 		if (tcp_do_newreno == 0) {
1814233e8c18SGarrett Wollman                         if (tp->t_dupacks >= tcprexmtthresh &&
1815df8bae1dSRodney W. Grimes                                 tp->snd_cwnd > tp->snd_ssthresh)
1816df8bae1dSRodney W. Grimes                                 tp->snd_cwnd = tp->snd_ssthresh;
1817df8bae1dSRodney W. Grimes                         tp->t_dupacks = 0;
181846f58482SJonathan Lemon                 } else if (tp->t_dupacks >= tcprexmtthresh &&
181946f58482SJonathan Lemon 		    !tcp_newreno(tp, th)) {
182046f58482SJonathan Lemon                         /*
182146f58482SJonathan Lemon                          * Window inflation should have left us with approx.
182246f58482SJonathan Lemon                          * snd_ssthresh outstanding data.  But in case we
182346f58482SJonathan Lemon                          * would be inclined to send a burst, better to do
182446f58482SJonathan Lemon                          * it via the slow start mechanism.
182546f58482SJonathan Lemon                          */
182646f58482SJonathan Lemon 			if (SEQ_GT(th->th_ack + tp->snd_ssthresh, tp->snd_max))
182746f58482SJonathan Lemon                                 tp->snd_cwnd =
182846f58482SJonathan Lemon 				    tp->snd_max - th->th_ack + tp->t_maxseg;
182946f58482SJonathan Lemon 			else
183046f58482SJonathan Lemon                         	tp->snd_cwnd = tp->snd_ssthresh;
183146f58482SJonathan Lemon                         tp->t_dupacks = 0;
183246f58482SJonathan Lemon                 }
1833fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
1834df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvacktoomuch++;
1835df8bae1dSRodney W. Grimes 			goto dropafterack;
1836df8bae1dSRodney W. Grimes 		}
1837a0292f23SGarrett Wollman 		/*
1838a0292f23SGarrett Wollman 		 *  If we reach this point, ACK is not a duplicate,
1839a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
1840a0292f23SGarrett Wollman 		 */
1841a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
1842a0292f23SGarrett Wollman 			/*
1843a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
1844a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
184507e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
184607e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
184707e43e10SAndras Olah 			 * we can do window scaling.
1848a0292f23SGarrett Wollman 			 */
1849a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
1850a0292f23SGarrett Wollman 			tp->snd_una++;
185107e43e10SAndras Olah 			/* Do window scaling? */
185207e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
185307e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
185407e43e10SAndras Olah 				tp->snd_scale = tp->requested_s_scale;
185507e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
185607e43e10SAndras Olah 			}
1857a0292f23SGarrett Wollman 		}
1858a0292f23SGarrett Wollman 
1859a0292f23SGarrett Wollman process_ACK:
1860fb59c426SYoshinobu Inoue 		acked = th->th_ack - tp->snd_una;
1861df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackpack++;
1862df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackbyte += acked;
1863df8bae1dSRodney W. Grimes 
1864df8bae1dSRodney W. Grimes 		/*
18659b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
18669b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
18679b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
18689b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
18699b8b58e0SJonathan Lemon 		 * we left off.
18709b8b58e0SJonathan Lemon 		 */
18719b8b58e0SJonathan Lemon 		if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) {
18729b8b58e0SJonathan Lemon 			tp->snd_cwnd = tp->snd_cwnd_prev;
18739b8b58e0SJonathan Lemon 			tp->snd_ssthresh = tp->snd_ssthresh_prev;
18749b8b58e0SJonathan Lemon 			tp->snd_nxt = tp->snd_max;
18759b8b58e0SJonathan Lemon 			tp->t_badrxtwin = 0;	/* XXX probably not required */
18769b8b58e0SJonathan Lemon 		}
18779b8b58e0SJonathan Lemon 
18789b8b58e0SJonathan Lemon 		/*
1879df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
1880df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
1881df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
1882df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
1883df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
1884df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
1885df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
1886df8bae1dSRodney W. Grimes 		 */
1887a0292f23SGarrett Wollman 		if (to.to_flag & TOF_TS)
18889b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - to.to_tsecr + 1);
1889fb59c426SYoshinobu Inoue 		else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq))
18909b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
1891df8bae1dSRodney W. Grimes 
1892df8bae1dSRodney W. Grimes 		/*
1893df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
1894df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
1895df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
1896df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
1897df8bae1dSRodney W. Grimes 		 */
1898fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
18999b8b58e0SJonathan Lemon 			callout_stop(tp->tt_rexmt);
1900df8bae1dSRodney W. Grimes 			needoutput = 1;
19019b8b58e0SJonathan Lemon 		} else if (!callout_active(tp->tt_persist))
19029b8b58e0SJonathan Lemon 			callout_reset(tp->tt_rexmt, tp->t_rxtcur,
19039b8b58e0SJonathan Lemon 				      tcp_timer_rexmt, tp);
1904a0292f23SGarrett Wollman 
1905a0292f23SGarrett Wollman 		/*
1906a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
1907a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
1908a0292f23SGarrett Wollman 		 */
1909a0292f23SGarrett Wollman 		if (acked == 0)
1910a0292f23SGarrett Wollman 			goto step6;
1911a0292f23SGarrett Wollman 
1912df8bae1dSRodney W. Grimes 		/*
1913df8bae1dSRodney W. Grimes 		 * When new data is acked, open the congestion window.
1914df8bae1dSRodney W. Grimes 		 * If the window gives us less than ssthresh packets
1915df8bae1dSRodney W. Grimes 		 * in flight, open exponentially (maxseg per packet).
1916df8bae1dSRodney W. Grimes 		 * Otherwise open linearly: maxseg per window
191710be5648SGarrett Wollman 		 * (maxseg^2 / cwnd per packet).
1918df8bae1dSRodney W. Grimes 		 */
1919df8bae1dSRodney W. Grimes 		{
1920df8bae1dSRodney W. Grimes 		register u_int cw = tp->snd_cwnd;
1921df8bae1dSRodney W. Grimes 		register u_int incr = tp->t_maxseg;
1922df8bae1dSRodney W. Grimes 
1923df8bae1dSRodney W. Grimes 		if (cw > tp->snd_ssthresh)
192410be5648SGarrett Wollman 			incr = incr * incr / cw;
19258735719eSJonathan Lemon 		/*
19268735719eSJonathan Lemon 		 * If t_dupacks != 0 here, it indicates that we are still
19278735719eSJonathan Lemon 		 * in NewReno fast recovery mode, so we leave the congestion
19288735719eSJonathan Lemon 		 * window alone.
19298735719eSJonathan Lemon 		 */
19308735719eSJonathan Lemon 		if (tcp_do_newreno == 0 || tp->t_dupacks == 0)
1931df8bae1dSRodney W. Grimes 			tp->snd_cwnd = min(cw + incr,TCP_MAXWIN<<tp->snd_scale);
1932df8bae1dSRodney W. Grimes 		}
1933df8bae1dSRodney W. Grimes 		if (acked > so->so_snd.sb_cc) {
1934df8bae1dSRodney W. Grimes 			tp->snd_wnd -= so->so_snd.sb_cc;
1935df8bae1dSRodney W. Grimes 			sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
1936df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
1937df8bae1dSRodney W. Grimes 		} else {
1938df8bae1dSRodney W. Grimes 			sbdrop(&so->so_snd, acked);
1939df8bae1dSRodney W. Grimes 			tp->snd_wnd -= acked;
1940df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
1941df8bae1dSRodney W. Grimes 		}
1942df8bae1dSRodney W. Grimes 		sowwakeup(so);
1943fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
1944df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1945df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
1946df8bae1dSRodney W. Grimes 
1947df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
1948df8bae1dSRodney W. Grimes 
1949df8bae1dSRodney W. Grimes 		/*
1950df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
1951df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
1952df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
1953df8bae1dSRodney W. Grimes 		 */
1954df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
1955df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
1956df8bae1dSRodney W. Grimes 				/*
1957df8bae1dSRodney W. Grimes 				 * If we can't receive any more
1958df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
1959df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
1960df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
1961df8bae1dSRodney W. Grimes 				 * we'll hang forever.
1962df8bae1dSRodney W. Grimes 				 */
1963df8bae1dSRodney W. Grimes 				if (so->so_state & SS_CANTRCVMORE) {
1964df8bae1dSRodney W. Grimes 					soisdisconnected(so);
19659b8b58e0SJonathan Lemon 					callout_reset(tp->tt_2msl, tcp_maxidle,
19669b8b58e0SJonathan Lemon 						      tcp_timer_2msl, tp);
1967df8bae1dSRodney W. Grimes 				}
1968df8bae1dSRodney W. Grimes 				tp->t_state = TCPS_FIN_WAIT_2;
1969df8bae1dSRodney W. Grimes 			}
1970df8bae1dSRodney W. Grimes 			break;
1971df8bae1dSRodney W. Grimes 
1972df8bae1dSRodney W. Grimes 	 	/*
1973df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
1974df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
1975df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
1976df8bae1dSRodney W. Grimes 		 * the segment.
1977df8bae1dSRodney W. Grimes 		 */
1978df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
1979df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
1980df8bae1dSRodney W. Grimes 				tp->t_state = TCPS_TIME_WAIT;
1981df8bae1dSRodney W. Grimes 				tcp_canceltimers(tp);
1982a0292f23SGarrett Wollman 				/* Shorten TIME_WAIT [RFC-1644, p.28] */
1983a0292f23SGarrett Wollman 				if (tp->cc_recv != 0 &&
19849b8b58e0SJonathan Lemon 				    (ticks - tp->t_starttime) < tcp_msl)
19859b8b58e0SJonathan Lemon 					callout_reset(tp->tt_2msl,
19869b8b58e0SJonathan Lemon 						      tp->t_rxtcur *
19879b8b58e0SJonathan Lemon 						      TCPTV_TWTRUNC,
19889b8b58e0SJonathan Lemon 						      tcp_timer_2msl, tp);
1989a0292f23SGarrett Wollman 				else
19909b8b58e0SJonathan Lemon 					callout_reset(tp->tt_2msl, 2 * tcp_msl,
19919b8b58e0SJonathan Lemon 						      tcp_timer_2msl, tp);
1992df8bae1dSRodney W. Grimes 				soisdisconnected(so);
1993df8bae1dSRodney W. Grimes 			}
1994df8bae1dSRodney W. Grimes 			break;
1995df8bae1dSRodney W. Grimes 
1996df8bae1dSRodney W. Grimes 		/*
1997df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
1998df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
1999df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
2000df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
2001df8bae1dSRodney W. Grimes 		 */
2002df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
2003df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2004df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
2005df8bae1dSRodney W. Grimes 				goto drop;
2006df8bae1dSRodney W. Grimes 			}
2007df8bae1dSRodney W. Grimes 			break;
2008df8bae1dSRodney W. Grimes 
2009df8bae1dSRodney W. Grimes 		/*
2010df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state the only thing that should arrive
2011df8bae1dSRodney W. Grimes 		 * is a retransmission of the remote FIN.  Acknowledge
2012df8bae1dSRodney W. Grimes 		 * it and restart the finack timer.
2013df8bae1dSRodney W. Grimes 		 */
2014df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
20159b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, 2 * tcp_msl,
20169b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
2017df8bae1dSRodney W. Grimes 			goto dropafterack;
2018df8bae1dSRodney W. Grimes 		}
2019df8bae1dSRodney W. Grimes 	}
2020df8bae1dSRodney W. Grimes 
2021df8bae1dSRodney W. Grimes step6:
2022df8bae1dSRodney W. Grimes 	/*
2023df8bae1dSRodney W. Grimes 	 * Update window information.
2024df8bae1dSRodney W. Grimes 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2025df8bae1dSRodney W. Grimes 	 */
2026fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) &&
2027fb59c426SYoshinobu Inoue 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
2028fb59c426SYoshinobu Inoue 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
2029fb59c426SYoshinobu Inoue 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
2030df8bae1dSRodney W. Grimes 		/* keep track of pure window updates */
2031fb59c426SYoshinobu Inoue 		if (tlen == 0 &&
2032fb59c426SYoshinobu Inoue 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
2033df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvwinupd++;
2034df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
2035fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq;
2036fb59c426SYoshinobu Inoue 		tp->snd_wl2 = th->th_ack;
2037df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
2038df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
2039df8bae1dSRodney W. Grimes 		needoutput = 1;
2040df8bae1dSRodney W. Grimes 	}
2041df8bae1dSRodney W. Grimes 
2042df8bae1dSRodney W. Grimes 	/*
2043df8bae1dSRodney W. Grimes 	 * Process segments with URG.
2044df8bae1dSRodney W. Grimes 	 */
2045fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
2046df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2047df8bae1dSRodney W. Grimes 		/*
2048df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
2049df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
2050df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
2051df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
2052df8bae1dSRodney W. Grimes 		 */
2053fb59c426SYoshinobu Inoue 		if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
2054fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
2055fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
2056df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
2057df8bae1dSRodney W. Grimes 		}
2058df8bae1dSRodney W. Grimes 		/*
2059df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
2060df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
2061df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2062df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
2063df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
2064df8bae1dSRodney W. Grimes 		 *
2065df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
2066df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
2067df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
2068df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
2069df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
2070df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
2071df8bae1dSRodney W. Grimes 		 */
2072fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2073fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
2074df8bae1dSRodney W. Grimes 			so->so_oobmark = so->so_rcv.sb_cc +
2075df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
2076df8bae1dSRodney W. Grimes 			if (so->so_oobmark == 0)
2077df8bae1dSRodney W. Grimes 				so->so_state |= SS_RCVATMARK;
2078df8bae1dSRodney W. Grimes 			sohasoutofband(so);
2079df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2080df8bae1dSRodney W. Grimes 		}
2081df8bae1dSRodney W. Grimes 		/*
2082df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
2083df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
2084df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
2085df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
2086df8bae1dSRodney W. Grimes 		 */
2087fb59c426SYoshinobu Inoue 		if (th->th_urp <= (u_long)tlen
2088df8bae1dSRodney W. Grimes #ifdef SO_OOBINLINE
2089df8bae1dSRodney W. Grimes 		     && (so->so_options & SO_OOBINLINE) == 0
2090df8bae1dSRodney W. Grimes #endif
2091df8bae1dSRodney W. Grimes 		     )
2092fb59c426SYoshinobu Inoue 			tcp_pulloutofband(so, th, m,
2093fb59c426SYoshinobu Inoue 				drop_hdrlen);	/* hdr drop is delayed */
2094df8bae1dSRodney W. Grimes 	} else
2095df8bae1dSRodney W. Grimes 		/*
2096df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
2097df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
2098df8bae1dSRodney W. Grimes 		 * with the receive window.
2099df8bae1dSRodney W. Grimes 		 */
2100df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2101df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
2102df8bae1dSRodney W. Grimes dodata:							/* XXX */
2103df8bae1dSRodney W. Grimes 
2104df8bae1dSRodney W. Grimes 	/*
2105df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
2106df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
2107df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
2108df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
2109df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
2110df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
2111df8bae1dSRodney W. Grimes 	 */
2112fb59c426SYoshinobu Inoue 	if ((tlen || (thflags&TH_FIN)) &&
2113df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2114fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
2115e4b64281SJesper Skriver 		/*
2116e4b64281SJesper Skriver 		 * Insert segment which inludes th into reassembly queue of tcp with
2117e4b64281SJesper Skriver 		 * control block tp.  Return TH_FIN if reassembly now includes
2118e4b64281SJesper Skriver 		 * a segment with FIN.  This handle the common case inline (segment
2119e4b64281SJesper Skriver 		 * is the next to be received on an established connection, and the
2120e4b64281SJesper Skriver 		 * queue is empty), avoiding linkage into and removal from the queue
2121e4b64281SJesper Skriver 		 * and repetition of various conversions.
2122e4b64281SJesper Skriver 		 * Set DELACK for segments received in order, but ack immediately
2123e4b64281SJesper Skriver 		 * when segments are out of order (so fast retransmit can work).
2124e4b64281SJesper Skriver 		 */
2125e4b64281SJesper Skriver 		if (th->th_seq == tp->rcv_nxt &&
2126e4b64281SJesper Skriver 		    LIST_EMPTY(&tp->t_segq) &&
2127e4b64281SJesper Skriver 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
2128e4b64281SJesper Skriver 			if (DELAY_ACK(tp))
2129e4b64281SJesper Skriver 				callout_reset(tp->tt_delack, tcp_delacktime,
2130e4b64281SJesper Skriver 				    tcp_timer_delack, tp);
2131e4b64281SJesper Skriver 			else
2132e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
2133e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
2134e4b64281SJesper Skriver 			thflags = th->th_flags & TH_FIN;
2135e4b64281SJesper Skriver 			tcpstat.tcps_rcvpack++;
2136e4b64281SJesper Skriver 			tcpstat.tcps_rcvbyte += tlen;
2137e4b64281SJesper Skriver 			ND6_HINT(tp);
2138e4b64281SJesper Skriver 			sbappend(&so->so_rcv, m);
2139e4b64281SJesper Skriver 			sorwakeup(so);
2140e4b64281SJesper Skriver 		} else {
2141e4b64281SJesper Skriver 			thflags = tcp_reass(tp, th, &tlen, m);
2142e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
2143e4b64281SJesper Skriver 		}
2144e4b64281SJesper Skriver 
2145df8bae1dSRodney W. Grimes 		/*
2146df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
2147df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
2148df8bae1dSRodney W. Grimes 		 * buffer size.
2149df8bae1dSRodney W. Grimes 		 */
2150df8bae1dSRodney W. Grimes 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2151df8bae1dSRodney W. Grimes 	} else {
2152df8bae1dSRodney W. Grimes 		m_freem(m);
2153fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
2154df8bae1dSRodney W. Grimes 	}
2155df8bae1dSRodney W. Grimes 
2156df8bae1dSRodney W. Grimes 	/*
2157df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
2158df8bae1dSRodney W. Grimes 	 * that the connection is closing.
2159df8bae1dSRodney W. Grimes 	 */
2160fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
2161df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2162df8bae1dSRodney W. Grimes 			socantrcvmore(so);
2163a0292f23SGarrett Wollman 			/*
2164a0292f23SGarrett Wollman 			 *  If connection is half-synchronized
2165d3eede9dSAndras Olah 			 *  (ie NEEDSYN flag on) then delay ACK,
2166a0292f23SGarrett Wollman 			 *  so it may be piggybacked when SYN is sent.
2167a0292f23SGarrett Wollman 			 *  Otherwise, since we received a FIN then no
2168a0292f23SGarrett Wollman 			 *  more input can be expected, send ACK now.
2169a0292f23SGarrett Wollman 			 */
2170d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp) && (tp->t_flags & TF_NEEDSYN))
21719b8b58e0SJonathan Lemon                                 callout_reset(tp->tt_delack, tcp_delacktime,
21729b8b58e0SJonathan Lemon                                     tcp_timer_delack, tp);
2173a0292f23SGarrett Wollman 			else
2174df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
2175df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
2176df8bae1dSRodney W. Grimes 		}
2177df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2178df8bae1dSRodney W. Grimes 
2179df8bae1dSRodney W. Grimes 	 	/*
2180df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
2181df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
2182df8bae1dSRodney W. Grimes 		 */
2183df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
21849b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
21859b8b58e0SJonathan Lemon 			/*FALLTHROUGH*/
2186df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
2187df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSE_WAIT;
2188df8bae1dSRodney W. Grimes 			break;
2189df8bae1dSRodney W. Grimes 
2190df8bae1dSRodney W. Grimes 	 	/*
2191df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2192df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
2193df8bae1dSRodney W. Grimes 		 */
2194df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2195df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSING;
2196df8bae1dSRodney W. Grimes 			break;
2197df8bae1dSRodney W. Grimes 
2198df8bae1dSRodney W. Grimes 	 	/*
2199df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2200df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
2201df8bae1dSRodney W. Grimes 		 * standard timers.
2202df8bae1dSRodney W. Grimes 		 */
2203df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
2204df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_TIME_WAIT;
2205df8bae1dSRodney W. Grimes 			tcp_canceltimers(tp);
2206a0292f23SGarrett Wollman 			/* Shorten TIME_WAIT [RFC-1644, p.28] */
2207a0292f23SGarrett Wollman 			if (tp->cc_recv != 0 &&
22089b8b58e0SJonathan Lemon 			    (ticks - tp->t_starttime) < tcp_msl) {
22099b8b58e0SJonathan Lemon 				callout_reset(tp->tt_2msl,
22109b8b58e0SJonathan Lemon 					      tp->t_rxtcur * TCPTV_TWTRUNC,
22119b8b58e0SJonathan Lemon 					      tcp_timer_2msl, tp);
2212a0292f23SGarrett Wollman 				/* For transaction client, force ACK now. */
2213a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
2214a0292f23SGarrett Wollman 			}
2215a0292f23SGarrett Wollman 			else
22169b8b58e0SJonathan Lemon 				callout_reset(tp->tt_2msl, 2 * tcp_msl,
22179b8b58e0SJonathan Lemon 					      tcp_timer_2msl, tp);
2218df8bae1dSRodney W. Grimes 			soisdisconnected(so);
2219df8bae1dSRodney W. Grimes 			break;
2220df8bae1dSRodney W. Grimes 
2221df8bae1dSRodney W. Grimes 		/*
2222df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
2223df8bae1dSRodney W. Grimes 		 */
2224df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
22259b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, 2 * tcp_msl,
22269b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
2227df8bae1dSRodney W. Grimes 			break;
2228df8bae1dSRodney W. Grimes 		}
2229df8bae1dSRodney W. Grimes 	}
2230610ee2f9SDavid Greenman #ifdef TCPDEBUG
2231df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG)
2232fb59c426SYoshinobu Inoue 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
2233fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2234610ee2f9SDavid Greenman #endif
2235df8bae1dSRodney W. Grimes 
2236df8bae1dSRodney W. Grimes 	/*
2237df8bae1dSRodney W. Grimes 	 * Return any desired output.
2238df8bae1dSRodney W. Grimes 	 */
2239df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
2240df8bae1dSRodney W. Grimes 		(void) tcp_output(tp);
2241df8bae1dSRodney W. Grimes 	return;
2242df8bae1dSRodney W. Grimes 
2243df8bae1dSRodney W. Grimes dropafterack:
2244df8bae1dSRodney W. Grimes 	/*
2245df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
2246df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
224780ab7c0eSGarrett Wollman 	 *
224880ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
224980ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
225080ab7c0eSGarrett Wollman 	 * RST have been dropped.
225180ab7c0eSGarrett Wollman 	 *
225280ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
225380ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
225480ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
225580ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
225680ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
225780ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
2258df8bae1dSRodney W. Grimes 	 */
2259fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
2260fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
2261a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
2262a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2263a57815efSBosko Milekic 		goto dropwithreset;
2264a57815efSBosko Milekic 	}
2265a0292f23SGarrett Wollman #ifdef TCPDEBUG
2266a0292f23SGarrett Wollman 	if (so->so_options & SO_DEBUG)
2267fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2268fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2269a0292f23SGarrett Wollman #endif
2270df8bae1dSRodney W. Grimes 	m_freem(m);
2271df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
2272df8bae1dSRodney W. Grimes 	(void) tcp_output(tp);
2273df8bae1dSRodney W. Grimes 	return;
2274df8bae1dSRodney W. Grimes 
2275df8bae1dSRodney W. Grimes dropwithreset:
2276df8bae1dSRodney W. Grimes 	/*
2277df8bae1dSRodney W. Grimes 	 * Generate a RST, dropping incoming segment.
2278df8bae1dSRodney W. Grimes 	 * Make ACK acceptable to originator of segment.
2279df8bae1dSRodney W. Grimes 	 * Don't bother to respond if destination was broadcast/multicast.
2280df8bae1dSRodney W. Grimes 	 */
2281fb59c426SYoshinobu Inoue 	if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
2282df8bae1dSRodney W. Grimes 		goto drop;
2283fb59c426SYoshinobu Inoue #ifdef INET6
2284fb59c426SYoshinobu Inoue 	if (isipv6) {
2285173c0f9fSWarner Losh 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
2286173c0f9fSWarner Losh 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
2287fb59c426SYoshinobu Inoue 			goto drop;
2288fb59c426SYoshinobu Inoue 	} else
2289fb59c426SYoshinobu Inoue #endif /* INET6 */
2290173c0f9fSWarner Losh 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
2291173c0f9fSWarner Losh 	    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
2292173c0f9fSWarner Losh 	    ip->ip_src.s_addr == htonl(INADDR_BROADCAST))
2293fb59c426SYoshinobu Inoue 		goto drop;
2294fb59c426SYoshinobu Inoue 	/* IPv6 anycast check is done at tcp6_input() */
2295a57815efSBosko Milekic 
2296a57815efSBosko Milekic 	/*
2297c59319bfSDag-Erling Smørgrav 	 * Perform bandwidth limiting.
2298a57815efSBosko Milekic 	 */
2299a57815efSBosko Milekic 	if (badport_bandlim(rstreason) < 0)
2300a57815efSBosko Milekic 		goto drop;
2301a57815efSBosko Milekic 
2302a0292f23SGarrett Wollman #ifdef TCPDEBUG
2303a0292f23SGarrett Wollman 	if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2304fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2305fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2306a0292f23SGarrett Wollman #endif
2307fb59c426SYoshinobu Inoue 	if (thflags & TH_ACK)
2308fb59c426SYoshinobu Inoue 		/* mtod() below is safe as long as hdr dropping is delayed */
2309fb59c426SYoshinobu Inoue 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack,
2310fb59c426SYoshinobu Inoue 			    TH_RST);
2311df8bae1dSRodney W. Grimes 	else {
2312fb59c426SYoshinobu Inoue 		if (thflags & TH_SYN)
2313fb59c426SYoshinobu Inoue 			tlen++;
2314fb59c426SYoshinobu Inoue 		/* mtod() below is safe as long as hdr dropping is delayed */
2315fb59c426SYoshinobu Inoue 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
2316fb59c426SYoshinobu Inoue 			    (tcp_seq)0, TH_RST|TH_ACK);
2317df8bae1dSRodney W. Grimes 	}
2318df8bae1dSRodney W. Grimes 	/* destroy temporarily created socket */
2319df8bae1dSRodney W. Grimes 	if (dropsocket)
2320df8bae1dSRodney W. Grimes 		(void) soabort(so);
2321df8bae1dSRodney W. Grimes 	return;
2322df8bae1dSRodney W. Grimes 
2323df8bae1dSRodney W. Grimes drop:
2324df8bae1dSRodney W. Grimes 	/*
2325df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
2326df8bae1dSRodney W. Grimes 	 */
2327610ee2f9SDavid Greenman #ifdef TCPDEBUG
2328a0292f23SGarrett Wollman 	if (tp == 0 || (tp->t_inpcb->inp_socket->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 	/* destroy temporarily created socket */
2334df8bae1dSRodney W. Grimes 	if (dropsocket)
2335df8bae1dSRodney W. Grimes 		(void) soabort(so);
2336df8bae1dSRodney W. Grimes 	return;
2337df8bae1dSRodney W. Grimes }
2338df8bae1dSRodney W. Grimes 
23390312fbe9SPoul-Henning Kamp static void
2340fb59c426SYoshinobu Inoue tcp_dooptions(tp, cp, cnt, th, to)
2341df8bae1dSRodney W. Grimes 	struct tcpcb *tp;
2342df8bae1dSRodney W. Grimes 	u_char *cp;
2343df8bae1dSRodney W. Grimes 	int cnt;
2344fb59c426SYoshinobu Inoue 	struct tcphdr *th;
2345a0292f23SGarrett Wollman 	struct tcpopt *to;
2346df8bae1dSRodney W. Grimes {
2347a0292f23SGarrett Wollman 	u_short mss = 0;
2348df8bae1dSRodney W. Grimes 	int opt, optlen;
2349df8bae1dSRodney W. Grimes 
2350df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
2351df8bae1dSRodney W. Grimes 		opt = cp[0];
2352df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
2353df8bae1dSRodney W. Grimes 			break;
2354df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
2355df8bae1dSRodney W. Grimes 			optlen = 1;
2356df8bae1dSRodney W. Grimes 		else {
2357b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
2358b474779fSJun-ichiro itojun Hagino 				break;
2359df8bae1dSRodney W. Grimes 			optlen = cp[1];
2360b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
2361df8bae1dSRodney W. Grimes 				break;
2362df8bae1dSRodney W. Grimes 		}
2363df8bae1dSRodney W. Grimes 		switch (opt) {
2364df8bae1dSRodney W. Grimes 
2365df8bae1dSRodney W. Grimes 		default:
2366df8bae1dSRodney W. Grimes 			continue;
2367df8bae1dSRodney W. Grimes 
2368df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
2369df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
2370df8bae1dSRodney W. Grimes 				continue;
2371fb59c426SYoshinobu Inoue 			if (!(th->th_flags & TH_SYN))
2372df8bae1dSRodney W. Grimes 				continue;
2373df8bae1dSRodney W. Grimes 			bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
2374df8bae1dSRodney W. Grimes 			NTOHS(mss);
2375df8bae1dSRodney W. Grimes 			break;
2376df8bae1dSRodney W. Grimes 
2377df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
2378df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
2379df8bae1dSRodney W. Grimes 				continue;
2380fb59c426SYoshinobu Inoue 			if (!(th->th_flags & TH_SYN))
2381df8bae1dSRodney W. Grimes 				continue;
2382df8bae1dSRodney W. Grimes 			tp->t_flags |= TF_RCVD_SCALE;
2383df8bae1dSRodney W. Grimes 			tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
2384df8bae1dSRodney W. Grimes 			break;
2385df8bae1dSRodney W. Grimes 
2386df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
2387df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
2388df8bae1dSRodney W. Grimes 				continue;
2389a0292f23SGarrett Wollman 			to->to_flag |= TOF_TS;
2390a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2391a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
2392a0292f23SGarrett Wollman 			NTOHL(to->to_tsval);
2393a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
2394a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
2395a0292f23SGarrett Wollman 			NTOHL(to->to_tsecr);
2396df8bae1dSRodney W. Grimes 
2397df8bae1dSRodney W. Grimes 			/*
2398df8bae1dSRodney W. Grimes 			 * A timestamp received in a SYN makes
2399df8bae1dSRodney W. Grimes 			 * it ok to send timestamp requests and replies.
2400df8bae1dSRodney W. Grimes 			 */
2401fb59c426SYoshinobu Inoue 			if (th->th_flags & TH_SYN) {
2402df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_RCVD_TSTMP;
2403a0292f23SGarrett Wollman 				tp->ts_recent = to->to_tsval;
24049b8b58e0SJonathan Lemon 				tp->ts_recent_age = ticks;
2405df8bae1dSRodney W. Grimes 			}
2406df8bae1dSRodney W. Grimes 			break;
2407a0292f23SGarrett Wollman 		case TCPOPT_CC:
2408a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
2409a0292f23SGarrett Wollman 				continue;
241040db8ef7SAndras Olah 			to->to_flag |= TOF_CC;
2411a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2412a0292f23SGarrett Wollman 			    (char *)&to->to_cc, sizeof(to->to_cc));
2413a0292f23SGarrett Wollman 			NTOHL(to->to_cc);
2414a0292f23SGarrett Wollman 			/*
2415a0292f23SGarrett Wollman 			 * A CC or CC.new option received in a SYN makes
2416a0292f23SGarrett Wollman 			 * it ok to send CC in subsequent segments.
2417a0292f23SGarrett Wollman 			 */
2418fb59c426SYoshinobu Inoue 			if (th->th_flags & TH_SYN)
2419a0292f23SGarrett Wollman 				tp->t_flags |= TF_RCVD_CC;
2420a0292f23SGarrett Wollman 			break;
2421a0292f23SGarrett Wollman 		case TCPOPT_CCNEW:
2422a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
2423a0292f23SGarrett Wollman 				continue;
2424fb59c426SYoshinobu Inoue 			if (!(th->th_flags & TH_SYN))
2425a0292f23SGarrett Wollman 				continue;
2426a0292f23SGarrett Wollman 			to->to_flag |= TOF_CCNEW;
2427a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2428a0292f23SGarrett Wollman 			    (char *)&to->to_cc, sizeof(to->to_cc));
2429a0292f23SGarrett Wollman 			NTOHL(to->to_cc);
2430a0292f23SGarrett Wollman 			/*
2431a0292f23SGarrett Wollman 			 * A CC or CC.new option received in a SYN makes
2432a0292f23SGarrett Wollman 			 * it ok to send CC in subsequent segments.
2433a0292f23SGarrett Wollman 			 */
2434a0292f23SGarrett Wollman 			tp->t_flags |= TF_RCVD_CC;
2435a0292f23SGarrett Wollman 			break;
2436a0292f23SGarrett Wollman 		case TCPOPT_CCECHO:
2437a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
2438a0292f23SGarrett Wollman 				continue;
2439fb59c426SYoshinobu Inoue 			if (!(th->th_flags & TH_SYN))
2440a0292f23SGarrett Wollman 				continue;
2441a0292f23SGarrett Wollman 			to->to_flag |= TOF_CCECHO;
2442a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2443a0292f23SGarrett Wollman 			    (char *)&to->to_ccecho, sizeof(to->to_ccecho));
2444a0292f23SGarrett Wollman 			NTOHL(to->to_ccecho);
2445a0292f23SGarrett Wollman 			break;
2446df8bae1dSRodney W. Grimes 		}
2447df8bae1dSRodney W. Grimes 	}
2448fb59c426SYoshinobu Inoue 	if (th->th_flags & TH_SYN)
2449a0292f23SGarrett Wollman 		tcp_mss(tp, mss);	/* sets t_maxseg */
2450df8bae1dSRodney W. Grimes }
2451df8bae1dSRodney W. Grimes 
2452df8bae1dSRodney W. Grimes /*
2453df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
2454df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
2455df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
2456df8bae1dSRodney W. Grimes  * sequencing purposes.
2457df8bae1dSRodney W. Grimes  */
24580312fbe9SPoul-Henning Kamp static void
2459fb59c426SYoshinobu Inoue tcp_pulloutofband(so, th, m, off)
2460df8bae1dSRodney W. Grimes 	struct socket *so;
2461fb59c426SYoshinobu Inoue 	struct tcphdr *th;
2462df8bae1dSRodney W. Grimes 	register struct mbuf *m;
2463fb59c426SYoshinobu Inoue 	int off;		/* delayed to be droped hdrlen */
2464df8bae1dSRodney W. Grimes {
2465fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
2466df8bae1dSRodney W. Grimes 
2467df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
2468df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
2469df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
2470df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
2471df8bae1dSRodney W. Grimes 
2472df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
2473df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
2474df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
2475df8bae1dSRodney W. Grimes 			m->m_len--;
247669a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
247769a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
2478df8bae1dSRodney W. Grimes 			return;
2479df8bae1dSRodney W. Grimes 		}
2480df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
2481df8bae1dSRodney W. Grimes 		m = m->m_next;
2482df8bae1dSRodney W. Grimes 		if (m == 0)
2483df8bae1dSRodney W. Grimes 			break;
2484df8bae1dSRodney W. Grimes 	}
2485df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
2486df8bae1dSRodney W. Grimes }
2487df8bae1dSRodney W. Grimes 
2488df8bae1dSRodney W. Grimes /*
2489df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
2490df8bae1dSRodney W. Grimes  * and update averages and current timeout.
2491df8bae1dSRodney W. Grimes  */
24920312fbe9SPoul-Henning Kamp static void
2493df8bae1dSRodney W. Grimes tcp_xmit_timer(tp, rtt)
2494df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
24959b8b58e0SJonathan Lemon 	int rtt;
2496df8bae1dSRodney W. Grimes {
2497233e8c18SGarrett Wollman 	register int delta;
2498233e8c18SGarrett Wollman 
2499233e8c18SGarrett Wollman 	tcpstat.tcps_rttupdated++;
2500233e8c18SGarrett Wollman 	tp->t_rttupdated++;
2501233e8c18SGarrett Wollman 	if (tp->t_srtt != 0) {
2502233e8c18SGarrett Wollman 		/*
2503233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
2504233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
2505233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
2506233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
2507233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
2508233e8c18SGarrett Wollman 		 */
2509233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
2510233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
2511233e8c18SGarrett Wollman 
2512233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
2513233e8c18SGarrett Wollman 			tp->t_srtt = 1;
2514233e8c18SGarrett Wollman 
2515233e8c18SGarrett Wollman 		/*
2516233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
2517233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
2518233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
2519233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
2520233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
2521233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
2522233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
2523233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
2524233e8c18SGarrett Wollman 		 */
2525233e8c18SGarrett Wollman 		if (delta < 0)
2526233e8c18SGarrett Wollman 			delta = -delta;
2527233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
2528233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
2529233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
2530233e8c18SGarrett Wollman 	} else {
2531233e8c18SGarrett Wollman 		/*
2532233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
2533233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
2534233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
2535233e8c18SGarrett Wollman 		 */
2536233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
2537233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
2538233e8c18SGarrett Wollman 	}
25399b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
2540df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
2541df8bae1dSRodney W. Grimes 
2542df8bae1dSRodney W. Grimes 	/*
2543df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
2544df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
2545df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
2546df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
2547df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
2548df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
2549df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
2550df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
2551df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
2552df8bae1dSRodney W. Grimes 	 */
2553233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
25549e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
2555df8bae1dSRodney W. Grimes 
2556df8bae1dSRodney W. Grimes 	/*
2557df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
2558df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
2559df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
2560df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
2561df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
2562df8bae1dSRodney W. Grimes 	 */
2563df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
2564df8bae1dSRodney W. Grimes }
2565df8bae1dSRodney W. Grimes 
2566df8bae1dSRodney W. Grimes /*
2567df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
2568df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
2569df8bae1dSRodney W. Grimes  * If none, use an mss that can be handled on the outgoing
2570df8bae1dSRodney W. Grimes  * interface without forcing IP to fragment; if bigger than
2571df8bae1dSRodney W. Grimes  * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
2572df8bae1dSRodney W. Grimes  * to utilize large mbufs.  If no route is found, route has no mtu,
2573df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
2574df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
2575df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
2576df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
2577df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
2578df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
2579df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
2580a0292f23SGarrett Wollman  *
2581a0292f23SGarrett Wollman  * Also take into account the space needed for options that we
2582a0292f23SGarrett Wollman  * send regularly.  Make maxseg shorter by that amount to assure
2583a0292f23SGarrett Wollman  * that we can send maxseg amount of data even when the options
2584a0292f23SGarrett Wollman  * are present.  Store the upper limit of the length of options plus
2585a0292f23SGarrett Wollman  * data in maxopd.
2586a0292f23SGarrett Wollman  *
2587a0292f23SGarrett Wollman  * NOTE that this routine is only called when we process an incoming
2588a0292f23SGarrett Wollman  * segment, for outgoing segments only tcp_mssopt is called.
2589a0292f23SGarrett Wollman  *
2590a0292f23SGarrett Wollman  * In case of T/TCP, we call this routine during implicit connection
2591a0292f23SGarrett Wollman  * setup as well (offer = -1), to initialize maxseg from the cached
2592a0292f23SGarrett Wollman  * MSS of our peer.
2593df8bae1dSRodney W. Grimes  */
2594a0292f23SGarrett Wollman void
2595df8bae1dSRodney W. Grimes tcp_mss(tp, offer)
2596a0292f23SGarrett Wollman 	struct tcpcb *tp;
2597a0292f23SGarrett Wollman 	int offer;
2598df8bae1dSRodney W. Grimes {
2599df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
2600df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
2601df8bae1dSRodney W. Grimes 	register int rtt, mss;
2602df8bae1dSRodney W. Grimes 	u_long bufsize;
2603df8bae1dSRodney W. Grimes 	struct inpcb *inp;
2604df8bae1dSRodney W. Grimes 	struct socket *so;
2605a0292f23SGarrett Wollman 	struct rmxp_tao *taop;
2606a0292f23SGarrett Wollman 	int origoffer = offer;
2607fb59c426SYoshinobu Inoue #ifdef INET6
2608fb59c426SYoshinobu Inoue 	int isipv6;
2609fb59c426SYoshinobu Inoue 	int min_protoh;
2610fb59c426SYoshinobu Inoue #endif
2611df8bae1dSRodney W. Grimes 
2612df8bae1dSRodney W. Grimes 	inp = tp->t_inpcb;
2613fb59c426SYoshinobu Inoue #ifdef INET6
2614fb59c426SYoshinobu Inoue 	isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
2615fb59c426SYoshinobu Inoue 	min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr)
2616fb59c426SYoshinobu Inoue 			    : sizeof (struct tcpiphdr);
2617fb59c426SYoshinobu Inoue #else
2618fb59c426SYoshinobu Inoue #define min_protoh  (sizeof (struct tcpiphdr))
2619fb59c426SYoshinobu Inoue #endif
2620fb59c426SYoshinobu Inoue #ifdef INET6
2621fb59c426SYoshinobu Inoue 	if (isipv6)
2622fb59c426SYoshinobu Inoue 		rt = tcp_rtlookup6(inp);
2623fb59c426SYoshinobu Inoue 	else
2624fb59c426SYoshinobu Inoue #endif
2625fb59c426SYoshinobu Inoue 	rt = tcp_rtlookup(inp);
2626fb59c426SYoshinobu Inoue 	if (rt == NULL) {
2627fb59c426SYoshinobu Inoue 		tp->t_maxopd = tp->t_maxseg =
2628fb59c426SYoshinobu Inoue #ifdef INET6
2629fb59c426SYoshinobu Inoue 		isipv6 ? tcp_v6mssdflt :
2630fb59c426SYoshinobu Inoue #endif /* INET6 */
2631fb59c426SYoshinobu Inoue 		tcp_mssdflt;
2632a0292f23SGarrett Wollman 		return;
2633df8bae1dSRodney W. Grimes 	}
2634df8bae1dSRodney W. Grimes 	ifp = rt->rt_ifp;
2635df8bae1dSRodney W. Grimes 	so = inp->inp_socket;
2636df8bae1dSRodney W. Grimes 
2637a0292f23SGarrett Wollman 	taop = rmx_taop(rt->rt_rmx);
2638a0292f23SGarrett Wollman 	/*
2639a0292f23SGarrett Wollman 	 * Offer == -1 means that we didn't receive SYN yet,
2640a0292f23SGarrett Wollman 	 * use cached value in that case;
2641a0292f23SGarrett Wollman 	 */
2642a0292f23SGarrett Wollman 	if (offer == -1)
2643a0292f23SGarrett Wollman 		offer = taop->tao_mssopt;
2644a0292f23SGarrett Wollman 	/*
2645a0292f23SGarrett Wollman 	 * Offer == 0 means that there was no MSS on the SYN segment,
2646a0292f23SGarrett Wollman 	 * in this case we use tcp_mssdflt.
2647a0292f23SGarrett Wollman 	 */
2648a0292f23SGarrett Wollman 	if (offer == 0)
2649fb59c426SYoshinobu Inoue 		offer =
2650fb59c426SYoshinobu Inoue #ifdef INET6
2651fb59c426SYoshinobu Inoue 			isipv6 ? tcp_v6mssdflt :
2652fb59c426SYoshinobu Inoue #endif /* INET6 */
2653fb59c426SYoshinobu Inoue 			tcp_mssdflt;
2654a0292f23SGarrett Wollman 	else
2655a0292f23SGarrett Wollman 		/*
2656a0292f23SGarrett Wollman 		 * Sanity check: make sure that maxopd will be large
2657a0292f23SGarrett Wollman 		 * enough to allow some data on segments even is the
2658a0292f23SGarrett Wollman 		 * all the option space is used (40bytes).  Otherwise
2659a0292f23SGarrett Wollman 		 * funny things may happen in tcp_output.
2660a0292f23SGarrett Wollman 		 */
2661a0292f23SGarrett Wollman 		offer = max(offer, 64);
2662a0292f23SGarrett Wollman 	taop->tao_mssopt = offer;
2663a0292f23SGarrett Wollman 
2664df8bae1dSRodney W. Grimes 	/*
2665df8bae1dSRodney W. Grimes 	 * While we're here, check if there's an initial rtt
2666df8bae1dSRodney W. Grimes 	 * or rttvar.  Convert from the route-table units
2667df8bae1dSRodney W. Grimes 	 * to scaled multiples of the slow timeout timer.
2668df8bae1dSRodney W. Grimes 	 */
2669df8bae1dSRodney W. Grimes 	if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
2670df8bae1dSRodney W. Grimes 		/*
2671a0292f23SGarrett Wollman 		 * XXX the lock bit for RTT indicates that the value
2672df8bae1dSRodney W. Grimes 		 * is also a minimum value; this is subject to time.
2673df8bae1dSRodney W. Grimes 		 */
2674df8bae1dSRodney W. Grimes 		if (rt->rt_rmx.rmx_locks & RTV_RTT)
26759b8b58e0SJonathan Lemon 			tp->t_rttmin = rtt / (RTM_RTTUNIT / hz);
26769b8b58e0SJonathan Lemon 		tp->t_srtt = rtt / (RTM_RTTUNIT / (hz * TCP_RTT_SCALE));
2677dd224982SGarrett Wollman 		tcpstat.tcps_usedrtt++;
2678dd224982SGarrett Wollman 		if (rt->rt_rmx.rmx_rttvar) {
2679df8bae1dSRodney W. Grimes 			tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
26809b8b58e0SJonathan Lemon 			    (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE));
2681dd224982SGarrett Wollman 			tcpstat.tcps_usedrttvar++;
2682dd224982SGarrett Wollman 		} else {
2683df8bae1dSRodney W. Grimes 			/* default variation is +- 1 rtt */
2684df8bae1dSRodney W. Grimes 			tp->t_rttvar =
2685df8bae1dSRodney W. Grimes 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
2686dd224982SGarrett Wollman 		}
2687df8bae1dSRodney W. Grimes 		TCPT_RANGESET(tp->t_rxtcur,
2688df8bae1dSRodney W. Grimes 			      ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
2689df8bae1dSRodney W. Grimes 			      tp->t_rttmin, TCPTV_REXMTMAX);
2690df8bae1dSRodney W. Grimes 	}
2691df8bae1dSRodney W. Grimes 	/*
2692df8bae1dSRodney W. Grimes 	 * if there's an mtu associated with the route, use it
2693fb59c426SYoshinobu Inoue 	 * else, use the link mtu.
2694df8bae1dSRodney W. Grimes 	 */
2695df8bae1dSRodney W. Grimes 	if (rt->rt_rmx.rmx_mtu)
2696fb59c426SYoshinobu Inoue 		mss = rt->rt_rmx.rmx_mtu - min_protoh;
2697df8bae1dSRodney W. Grimes 	else
2698df8bae1dSRodney W. Grimes 	{
2699fb59c426SYoshinobu Inoue 		mss =
2700fb59c426SYoshinobu Inoue #ifdef INET6
2701fb59c426SYoshinobu Inoue 			(isipv6 ? nd_ifinfo[rt->rt_ifp->if_index].linkmtu :
2702fb59c426SYoshinobu Inoue #endif
2703fb59c426SYoshinobu Inoue 			 ifp->if_mtu
2704fb59c426SYoshinobu Inoue #ifdef INET6
2705fb59c426SYoshinobu Inoue 			 )
2706fb59c426SYoshinobu Inoue #endif
2707fb59c426SYoshinobu Inoue 			- min_protoh;
2708fb59c426SYoshinobu Inoue #ifdef INET6
2709fb59c426SYoshinobu Inoue 		if (isipv6) {
2710fb59c426SYoshinobu Inoue 			if (!in6_localaddr(&inp->in6p_faddr))
2711fb59c426SYoshinobu Inoue 				mss = min(mss, tcp_v6mssdflt);
2712fb59c426SYoshinobu Inoue 		} else
2713fb59c426SYoshinobu Inoue #endif
2714a0292f23SGarrett Wollman 		if (!in_localaddr(inp->inp_faddr))
2715a0292f23SGarrett Wollman 			mss = min(mss, tcp_mssdflt);
2716a0292f23SGarrett Wollman 	}
2717a0292f23SGarrett Wollman 	mss = min(mss, offer);
2718a0292f23SGarrett Wollman 	/*
2719a0292f23SGarrett Wollman 	 * maxopd stores the maximum length of data AND options
2720a0292f23SGarrett Wollman 	 * in a segment; maxseg is the amount of data in a normal
2721a0292f23SGarrett Wollman 	 * segment.  We need to store this value (maxopd) apart
2722a0292f23SGarrett Wollman 	 * from maxseg, because now every segment carries options
2723a0292f23SGarrett Wollman 	 * and thus we normally have somewhat less data in segments.
2724a0292f23SGarrett Wollman 	 */
2725a0292f23SGarrett Wollman 	tp->t_maxopd = mss;
2726a0292f23SGarrett Wollman 
2727a0292f23SGarrett Wollman 	/*
2728a0292f23SGarrett Wollman 	 * In case of T/TCP, origoffer==-1 indicates, that no segments
2729a0292f23SGarrett Wollman 	 * were received yet.  In this case we just guess, otherwise
2730a0292f23SGarrett Wollman 	 * we do the same as before T/TCP.
2731a0292f23SGarrett Wollman 	 */
2732a0292f23SGarrett Wollman  	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
2733a0292f23SGarrett Wollman 	    (origoffer == -1 ||
2734a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
2735a0292f23SGarrett Wollman 		mss -= TCPOLEN_TSTAMP_APPA;
2736a0292f23SGarrett Wollman  	if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
2737a0292f23SGarrett Wollman 	    (origoffer == -1 ||
2738a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC))
2739a0292f23SGarrett Wollman 		mss -= TCPOLEN_CC_APPA;
2740a0292f23SGarrett Wollman 
2741df8bae1dSRodney W. Grimes #if	(MCLBYTES & (MCLBYTES - 1)) == 0
2742df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
2743df8bae1dSRodney W. Grimes 			mss &= ~(MCLBYTES-1);
2744df8bae1dSRodney W. Grimes #else
2745df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
2746df8bae1dSRodney W. Grimes 			mss = mss / MCLBYTES * MCLBYTES;
2747df8bae1dSRodney W. Grimes #endif
2748df8bae1dSRodney W. Grimes 	/*
2749df8bae1dSRodney W. Grimes 	 * If there's a pipesize, change the socket buffer
2750df8bae1dSRodney W. Grimes 	 * to that size.  Make the socket buffers an integral
2751df8bae1dSRodney W. Grimes 	 * number of mss units; if the mss is larger than
2752df8bae1dSRodney W. Grimes 	 * the socket buffer, decrease the mss.
2753df8bae1dSRodney W. Grimes 	 */
2754df8bae1dSRodney W. Grimes #ifdef RTV_SPIPE
2755df8bae1dSRodney W. Grimes 	if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
2756df8bae1dSRodney W. Grimes #endif
2757df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
2758df8bae1dSRodney W. Grimes 	if (bufsize < mss)
2759df8bae1dSRodney W. Grimes 		mss = bufsize;
2760df8bae1dSRodney W. Grimes 	else {
2761df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
2762df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
2763df8bae1dSRodney W. Grimes 			bufsize = sb_max;
2764ecf72308SBrian Feldman 		(void)sbreserve(&so->so_snd, bufsize, so, NULL);
2765df8bae1dSRodney W. Grimes 	}
2766df8bae1dSRodney W. Grimes 	tp->t_maxseg = mss;
2767df8bae1dSRodney W. Grimes 
2768df8bae1dSRodney W. Grimes #ifdef RTV_RPIPE
2769df8bae1dSRodney W. Grimes 	if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
2770df8bae1dSRodney W. Grimes #endif
2771df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
2772df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
2773df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
2774df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
2775df8bae1dSRodney W. Grimes 			bufsize = sb_max;
2776ecf72308SBrian Feldman 		(void)sbreserve(&so->so_rcv, bufsize, so, NULL);
2777df8bae1dSRodney W. Grimes 	}
27789b8b58e0SJonathan Lemon 
2779a0292f23SGarrett Wollman 	/*
27809b8b58e0SJonathan Lemon 	 * Set the slow-start flight size depending on whether this
27819b8b58e0SJonathan Lemon 	 * is a local network or not.
2782a0292f23SGarrett Wollman 	 */
2783fb59c426SYoshinobu Inoue 	if (
2784fb59c426SYoshinobu Inoue #ifdef INET6
2785fb59c426SYoshinobu Inoue 	    (isipv6 && in6_localaddr(&inp->in6p_faddr)) ||
2786fb59c426SYoshinobu Inoue 	    (!isipv6 &&
2787fb59c426SYoshinobu Inoue #endif
2788fb59c426SYoshinobu Inoue 	     in_localaddr(inp->inp_faddr)
2789fb59c426SYoshinobu Inoue #ifdef INET6
2790fb59c426SYoshinobu Inoue 	     )
2791fb59c426SYoshinobu Inoue #endif
2792fb59c426SYoshinobu Inoue 	    )
27939b8b58e0SJonathan Lemon 		tp->snd_cwnd = mss * ss_fltsz_local;
27949b8b58e0SJonathan Lemon 	else
27959b8b58e0SJonathan Lemon 		tp->snd_cwnd = mss * ss_fltsz;
2796df8bae1dSRodney W. Grimes 
2797df8bae1dSRodney W. Grimes 	if (rt->rt_rmx.rmx_ssthresh) {
2798df8bae1dSRodney W. Grimes 		/*
2799df8bae1dSRodney W. Grimes 		 * There's some sort of gateway or interface
2800df8bae1dSRodney W. Grimes 		 * buffer limit on the path.  Use this to set
2801df8bae1dSRodney W. Grimes 		 * the slow start threshhold, but set the
2802df8bae1dSRodney W. Grimes 		 * threshold to no less than 2*mss.
2803df8bae1dSRodney W. Grimes 		 */
2804df8bae1dSRodney W. Grimes 		tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
2805dd224982SGarrett Wollman 		tcpstat.tcps_usedssthresh++;
2806df8bae1dSRodney W. Grimes 	}
2807a0292f23SGarrett Wollman }
2808a0292f23SGarrett Wollman 
2809a0292f23SGarrett Wollman /*
2810a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
2811a0292f23SGarrett Wollman  */
2812a0292f23SGarrett Wollman int
2813a0292f23SGarrett Wollman tcp_mssopt(tp)
2814a0292f23SGarrett Wollman 	struct tcpcb *tp;
2815a0292f23SGarrett Wollman {
2816a0292f23SGarrett Wollman 	struct rtentry *rt;
2817fb59c426SYoshinobu Inoue #ifdef INET6
2818fb59c426SYoshinobu Inoue 	int isipv6;
2819fb59c426SYoshinobu Inoue 	int min_protoh;
2820fb59c426SYoshinobu Inoue #endif
2821a0292f23SGarrett Wollman 
2822fb59c426SYoshinobu Inoue #ifdef INET6
2823fb59c426SYoshinobu Inoue 	isipv6 = ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
2824fb59c426SYoshinobu Inoue 	min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr)
2825fb59c426SYoshinobu Inoue 			    : sizeof (struct tcpiphdr);
2826fb59c426SYoshinobu Inoue #else
2827fb59c426SYoshinobu Inoue #define min_protoh  (sizeof (struct tcpiphdr))
2828fb59c426SYoshinobu Inoue #endif
2829fb59c426SYoshinobu Inoue #ifdef INET6
2830fb59c426SYoshinobu Inoue 	if (isipv6)
2831fb59c426SYoshinobu Inoue 		rt = tcp_rtlookup6(tp->t_inpcb);
2832fb59c426SYoshinobu Inoue 	else
2833fb59c426SYoshinobu Inoue #endif /* INET6 */
2834a0292f23SGarrett Wollman 	rt = tcp_rtlookup(tp->t_inpcb);
2835a0292f23SGarrett Wollman 	if (rt == NULL)
2836fb59c426SYoshinobu Inoue 		return
2837fb59c426SYoshinobu Inoue #ifdef INET6
2838fb59c426SYoshinobu Inoue 			isipv6 ? tcp_v6mssdflt :
2839fb59c426SYoshinobu Inoue #endif /* INET6 */
2840fb59c426SYoshinobu Inoue 			tcp_mssdflt;
2841a0292f23SGarrett Wollman 
2842fb59c426SYoshinobu Inoue 	return rt->rt_ifp->if_mtu - min_protoh;
2843df8bae1dSRodney W. Grimes }
284446f58482SJonathan Lemon 
284546f58482SJonathan Lemon 
284646f58482SJonathan Lemon /*
284746f58482SJonathan Lemon  * Checks for partial ack.  If partial ack arrives, force the retransmission
284846f58482SJonathan Lemon  * of the next unacknowledged segment, do not clear tp->t_dupacks, and return
284946f58482SJonathan Lemon  * 1.  By setting snd_nxt to ti_ack, this forces retransmission timer to
285046f58482SJonathan Lemon  * be started again.  If the ack advances at least to tp->snd_recover, return 0.
285146f58482SJonathan Lemon  */
285246f58482SJonathan Lemon static int
285346f58482SJonathan Lemon tcp_newreno(tp, th)
285446f58482SJonathan Lemon 	struct tcpcb *tp;
285546f58482SJonathan Lemon 	struct tcphdr *th;
285646f58482SJonathan Lemon {
285746f58482SJonathan Lemon 	if (SEQ_LT(th->th_ack, tp->snd_recover)) {
285846f58482SJonathan Lemon 		tcp_seq onxt = tp->snd_nxt;
285946f58482SJonathan Lemon 		u_long  ocwnd = tp->snd_cwnd;
286046f58482SJonathan Lemon 
286146f58482SJonathan Lemon 		callout_stop(tp->tt_rexmt);
286246f58482SJonathan Lemon 		tp->t_rtttime = 0;
286346f58482SJonathan Lemon 		tp->snd_nxt = th->th_ack;
28646b2a5f92SJayanth Vijayaraghavan 		/*
28656b2a5f92SJayanth Vijayaraghavan 		 * Set snd_cwnd to one segment beyond acknowledged offset
28666b2a5f92SJayanth Vijayaraghavan 		 * (tp->snd_una has not yet been updated when this function
28676b2a5f92SJayanth Vijayaraghavan 		 *  is called)
28686b2a5f92SJayanth Vijayaraghavan 		 */
28696b2a5f92SJayanth Vijayaraghavan 		tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una);
28706b2a5f92SJayanth Vijayaraghavan 		(void) tcp_output(tp);
287146f58482SJonathan Lemon 		tp->snd_cwnd = ocwnd;
287246f58482SJonathan Lemon 		if (SEQ_GT(onxt, tp->snd_nxt))
287346f58482SJonathan Lemon 			tp->snd_nxt = onxt;
287446f58482SJonathan Lemon 		/*
287546f58482SJonathan Lemon 		 * Partial window deflation.  Relies on fact that tp->snd_una
287646f58482SJonathan Lemon 		 * not updated yet.
287746f58482SJonathan Lemon 		 */
287846f58482SJonathan Lemon 		tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg);
287946f58482SJonathan Lemon 		return (1);
288046f58482SJonathan Lemon 	}
288146f58482SJonathan Lemon 	return (0);
288246f58482SJonathan Lemon }
2883