xref: /freebsd/sys/netinet/tcp_input.c (revision 3a2a9f797620b306b1d93461c5e568497d0a9e0f)
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>
6351508de1SMatthew Dillon #include <netinet/ip_icmp.h>	/* for ICMP_BANDLIM		*/
64fb59c426SYoshinobu Inoue #ifdef INET6
65fb59c426SYoshinobu Inoue #include <netinet/ip6.h>
66fb59c426SYoshinobu Inoue #include <netinet/in_var.h>
67fb59c426SYoshinobu Inoue #include <netinet6/nd6.h>
68fb59c426SYoshinobu Inoue #include <netinet/icmp6.h>
69fb59c426SYoshinobu Inoue #endif
70df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
71fb59c426SYoshinobu Inoue #ifdef INET6
72fb59c426SYoshinobu Inoue #include <netinet6/in6_pcb.h>
73fb59c426SYoshinobu Inoue #endif
74df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
75fb59c426SYoshinobu Inoue #ifdef INET6
76fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
77fb59c426SYoshinobu Inoue #endif
7851508de1SMatthew Dillon #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM		*/
79df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
80df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
81df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
82df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
83df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
84fb59c426SYoshinobu Inoue #ifdef INET6
85fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h>
86fb59c426SYoshinobu Inoue #endif
87df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
88610ee2f9SDavid Greenman #ifdef TCPDEBUG
89df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
90fb59c426SYoshinobu Inoue 
91fb59c426SYoshinobu Inoue u_char tcp_saveipgen[40]; /* the size must be of max ip header, now IPv6 */
92fb59c426SYoshinobu Inoue struct tcphdr tcp_savetcp;
93fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */
94fb59c426SYoshinobu Inoue 
95fb59c426SYoshinobu Inoue #ifdef IPSEC
96fb59c426SYoshinobu Inoue #include <netinet6/ipsec.h>
973a2a9f79SYoshinobu Inoue #ifdef INET6
983a2a9f79SYoshinobu Inoue #include <netinet6/ipsec6.h>
993a2a9f79SYoshinobu Inoue #endif
100fb59c426SYoshinobu Inoue #include <netkey/key.h>
101fb59c426SYoshinobu Inoue #endif /*IPSEC*/
102fb59c426SYoshinobu Inoue 
103fb59c426SYoshinobu Inoue MALLOC_DEFINE(M_TSEGQ, "tseg_qent", "TCP segment queue entry");
104df8bae1dSRodney W. Grimes 
1050312fbe9SPoul-Henning Kamp static int	tcprexmtthresh = 3;
1062f96f1f4SGarrett Wollman tcp_seq	tcp_iss;
1072f96f1f4SGarrett Wollman tcp_cc	tcp_ccgen;
1080312fbe9SPoul-Henning Kamp 
1092f96f1f4SGarrett Wollman struct	tcpstat tcpstat;
1103d177f46SBill Fumerola SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RD,
1113d177f46SBill Fumerola     &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
1120312fbe9SPoul-Henning Kamp 
113d78a37adSPaul Traina static int log_in_vain = 0;
114816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
1153d177f46SBill Fumerola     &log_in_vain, 0, "Log all incoming TCP connections");
116816a3d83SPoul-Henning Kamp 
11716f7f31fSGeoff Rehmet static int blackhole = 0;
11816f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW,
11916f7f31fSGeoff Rehmet 	&blackhole, 0, "Do not send RST when dropping refused connections");
12016f7f31fSGeoff Rehmet 
121f498eeeeSDavid Greenman int tcp_delack_enabled = 1;
12284e33c9eSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW,
1233d177f46SBill Fumerola     &tcp_delack_enabled, 0,
1243d177f46SBill Fumerola     "Delay ACK to try and piggyback it onto a data packet");
125f498eeeeSDavid Greenman 
126f8613305SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN
127f8613305SDag-Erling Smørgrav static int drop_synfin = 0;
128f8613305SDag-Erling Smørgrav SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW,
129f8613305SDag-Erling Smørgrav     &drop_synfin, 0, "Drop TCP packets with SYN+FIN set");
130f8613305SDag-Erling Smørgrav #endif
131f8613305SDag-Erling Smørgrav 
132e46cd3d4SDag-Erling Smørgrav #ifdef TCP_RESTRICT_RST
133e46cd3d4SDag-Erling Smørgrav static int restrict_rst = 0;
134e46cd3d4SDag-Erling Smørgrav SYSCTL_INT(_net_inet_tcp, OID_AUTO, restrict_rst, CTLFLAG_RW,
135e46cd3d4SDag-Erling Smørgrav     &restrict_rst, 0, "Restrict RST emission");
136e46cd3d4SDag-Erling Smørgrav #endif
137e46cd3d4SDag-Erling Smørgrav 
13815bd2b43SDavid Greenman struct inpcbhead tcb;
139fb59c426SYoshinobu Inoue #define	tcb6	tcb  /* for KAME src sync over BSD*'s */
14015bd2b43SDavid Greenman struct inpcbinfo tcbinfo;
141df8bae1dSRodney W. Grimes 
1420312fbe9SPoul-Henning Kamp static void	 tcp_dooptions __P((struct tcpcb *,
143fb59c426SYoshinobu Inoue 	    u_char *, int, struct tcphdr *, struct tcpopt *));
1440312fbe9SPoul-Henning Kamp static void	 tcp_pulloutofband __P((struct socket *,
145fb59c426SYoshinobu Inoue 	    struct tcphdr *, struct mbuf *, int));
146fb59c426SYoshinobu Inoue static int	 tcp_reass __P((struct tcpcb *, struct tcphdr *, int *,
147fb59c426SYoshinobu Inoue 				struct mbuf *));
1480312fbe9SPoul-Henning Kamp static void	 tcp_xmit_timer __P((struct tcpcb *, int));
1490312fbe9SPoul-Henning Kamp 
150fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
151fb59c426SYoshinobu Inoue #ifdef INET6
152fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \
153fb59c426SYoshinobu Inoue do { \
154fb59c426SYoshinobu Inoue 	if ((tp) && (tp)->t_inpcb && \
155fb59c426SYoshinobu Inoue 	    ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0 && \
156fb59c426SYoshinobu Inoue 	    (tp)->t_inpcb->in6p_route.ro_rt) \
157fb59c426SYoshinobu Inoue 		nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL); \
158fb59c426SYoshinobu Inoue } while (0)
159fb59c426SYoshinobu Inoue #else
160fb59c426SYoshinobu Inoue #define ND6_HINT(tp)
161fb59c426SYoshinobu Inoue #endif
162df8bae1dSRodney W. Grimes 
163df8bae1dSRodney W. Grimes /*
164fb59c426SYoshinobu Inoue  * Insert segment which inludes th into reassembly queue of tcp with
165df8bae1dSRodney W. Grimes  * control block tp.  Return TH_FIN if reassembly now includes
166df8bae1dSRodney W. Grimes  * a segment with FIN.  The macro form does the common case inline
167df8bae1dSRodney W. Grimes  * (segment is the next to be received on an established connection,
168df8bae1dSRodney W. Grimes  * and the queue is empty), avoiding linkage into and removal
169df8bae1dSRodney W. Grimes  * from the queue and repetition of various conversions.
170df8bae1dSRodney W. Grimes  * Set DELACK for segments received in order, but ack immediately
171df8bae1dSRodney W. Grimes  * when segments are out of order (so fast retransmit can work).
172df8bae1dSRodney W. Grimes  */
173fb59c426SYoshinobu Inoue #define	TCP_REASS(tp, th, tlenp, m, so, flags) { \
174fb59c426SYoshinobu Inoue 	if ((th)->th_seq == (tp)->rcv_nxt && \
175fb59c426SYoshinobu Inoue 	    LIST_EMPTY(&(tp)->t_segq) && \
1766b067b07SDavid Greenman 	    (tp)->t_state == TCPS_ESTABLISHED) { \
177f498eeeeSDavid Greenman 		if (tcp_delack_enabled) \
1789b8b58e0SJonathan Lemon 			callout_reset(tp->tt_delack, tcp_delacktime, \
1799b8b58e0SJonathan Lemon 			    tcp_timer_delack, tp); \
180f498eeeeSDavid Greenman 		else \
181f498eeeeSDavid Greenman 			tp->t_flags |= TF_ACKNOW; \
182fb59c426SYoshinobu Inoue 		(tp)->rcv_nxt += *(tlenp); \
183fb59c426SYoshinobu Inoue 		flags = (th)->th_flags & TH_FIN; \
1846b067b07SDavid Greenman 		tcpstat.tcps_rcvpack++;\
185fb59c426SYoshinobu Inoue 		tcpstat.tcps_rcvbyte += *(tlenp);\
186fb59c426SYoshinobu Inoue 		ND6_HINT(tp); \
1876b067b07SDavid Greenman 		sbappend(&(so)->so_rcv, (m)); \
1886b067b07SDavid Greenman 		sorwakeup(so); \
1896b067b07SDavid Greenman 	} else { \
190fb59c426SYoshinobu Inoue 		(flags) = tcp_reass((tp), (th), (tlenp), (m)); \
1916b067b07SDavid Greenman 		tp->t_flags |= TF_ACKNOW; \
1926b067b07SDavid Greenman 	} \
1936b067b07SDavid Greenman }
194df8bae1dSRodney W. Grimes 
1950312fbe9SPoul-Henning Kamp static int
196fb59c426SYoshinobu Inoue tcp_reass(tp, th, tlenp, m)
197df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
198fb59c426SYoshinobu Inoue 	register struct tcphdr *th;
199fb59c426SYoshinobu Inoue 	int *tlenp;
200df8bae1dSRodney W. Grimes 	struct mbuf *m;
201df8bae1dSRodney W. Grimes {
202fb59c426SYoshinobu Inoue 	struct tseg_qent *q;
203fb59c426SYoshinobu Inoue 	struct tseg_qent *p = NULL;
204fb59c426SYoshinobu Inoue 	struct tseg_qent *nq;
205fb59c426SYoshinobu Inoue 	struct tseg_qent *te;
206df8bae1dSRodney W. Grimes 	struct socket *so = tp->t_inpcb->inp_socket;
207df8bae1dSRodney W. Grimes 	int flags;
208df8bae1dSRodney W. Grimes 
209df8bae1dSRodney W. Grimes 	/*
210fb59c426SYoshinobu Inoue 	 * Call with th==0 after become established to
211df8bae1dSRodney W. Grimes 	 * force pre-ESTABLISHED data up to user socket.
212df8bae1dSRodney W. Grimes 	 */
213fb59c426SYoshinobu Inoue 	if (th == 0)
214df8bae1dSRodney W. Grimes 		goto present;
215df8bae1dSRodney W. Grimes 
216fb59c426SYoshinobu Inoue 	/* Allocate a new queue entry. If we can't, just drop the pkt. XXX */
217fb59c426SYoshinobu Inoue 	MALLOC(te, struct tseg_qent *, sizeof (struct tseg_qent), M_TSEGQ,
218fb59c426SYoshinobu Inoue 	       M_NOWAIT);
219fb59c426SYoshinobu Inoue 	if (te == NULL) {
220fb59c426SYoshinobu Inoue 		tcpstat.tcps_rcvmemdrop++;
221fb59c426SYoshinobu Inoue 		m_freem(m);
222fb59c426SYoshinobu Inoue 		return (0);
223fb59c426SYoshinobu Inoue 	}
2246effc713SDoug Rabson 
225df8bae1dSRodney W. Grimes 	/*
226df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
227df8bae1dSRodney W. Grimes 	 */
228fb59c426SYoshinobu Inoue 	LIST_FOREACH(q, &tp->t_segq, tqe_q) {
229fb59c426SYoshinobu Inoue 		if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
230df8bae1dSRodney W. Grimes 			break;
231fb59c426SYoshinobu Inoue 		p = q;
232fb59c426SYoshinobu Inoue 	}
233df8bae1dSRodney W. Grimes 
234df8bae1dSRodney W. Grimes 	/*
235df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
236df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
237df8bae1dSRodney W. Grimes 	 * segment.  If it provides all of our data, drop us.
238df8bae1dSRodney W. Grimes 	 */
2396effc713SDoug Rabson 	if (p != NULL) {
240df8bae1dSRodney W. Grimes 		register int i;
241df8bae1dSRodney W. Grimes 		/* conversion to int (in i) handles seq wraparound */
242fb59c426SYoshinobu Inoue 		i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
243df8bae1dSRodney W. Grimes 		if (i > 0) {
244fb59c426SYoshinobu Inoue 			if (i >= *tlenp) {
245df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvduppack++;
246fb59c426SYoshinobu Inoue 				tcpstat.tcps_rcvdupbyte += *tlenp;
247df8bae1dSRodney W. Grimes 				m_freem(m);
248fb59c426SYoshinobu Inoue 				FREE(te, M_TSEGQ);
249a0292f23SGarrett Wollman 				/*
250a0292f23SGarrett Wollman 				 * Try to present any queued data
251a0292f23SGarrett Wollman 				 * at the left window edge to the user.
252a0292f23SGarrett Wollman 				 * This is needed after the 3-WHS
253a0292f23SGarrett Wollman 				 * completes.
254a0292f23SGarrett Wollman 				 */
255a0292f23SGarrett Wollman 				goto present;	/* ??? */
256df8bae1dSRodney W. Grimes 			}
257df8bae1dSRodney W. Grimes 			m_adj(m, i);
258fb59c426SYoshinobu Inoue 			*tlenp -= i;
259fb59c426SYoshinobu Inoue 			th->th_seq += i;
260df8bae1dSRodney W. Grimes 		}
261df8bae1dSRodney W. Grimes 	}
262df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvoopack++;
263fb59c426SYoshinobu Inoue 	tcpstat.tcps_rcvoobyte += *tlenp;
264df8bae1dSRodney W. Grimes 
265df8bae1dSRodney W. Grimes 	/*
266df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
267df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
268df8bae1dSRodney W. Grimes 	 */
2696effc713SDoug Rabson 	while (q) {
270fb59c426SYoshinobu Inoue 		register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
271df8bae1dSRodney W. Grimes 		if (i <= 0)
272df8bae1dSRodney W. Grimes 			break;
273fb59c426SYoshinobu Inoue 		if (i < q->tqe_len) {
274fb59c426SYoshinobu Inoue 			q->tqe_th->th_seq += i;
275fb59c426SYoshinobu Inoue 			q->tqe_len -= i;
276fb59c426SYoshinobu Inoue 			m_adj(q->tqe_m, i);
277df8bae1dSRodney W. Grimes 			break;
278df8bae1dSRodney W. Grimes 		}
2796effc713SDoug Rabson 
280fb59c426SYoshinobu Inoue 		nq = LIST_NEXT(q, tqe_q);
281fb59c426SYoshinobu Inoue 		LIST_REMOVE(q, tqe_q);
282fb59c426SYoshinobu Inoue 		m_freem(q->tqe_m);
283fb59c426SYoshinobu Inoue 		FREE(q, M_TSEGQ);
2846effc713SDoug Rabson 		q = nq;
285df8bae1dSRodney W. Grimes 	}
286df8bae1dSRodney W. Grimes 
287fb59c426SYoshinobu Inoue 	/* Insert the new segment queue entry into place. */
288fb59c426SYoshinobu Inoue 	te->tqe_m = m;
289fb59c426SYoshinobu Inoue 	te->tqe_th = th;
290fb59c426SYoshinobu Inoue 	te->tqe_len = *tlenp;
291fb59c426SYoshinobu Inoue 
2926effc713SDoug Rabson 	if (p == NULL) {
293fb59c426SYoshinobu Inoue 		LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
2946effc713SDoug Rabson 	} else {
295fb59c426SYoshinobu Inoue 		LIST_INSERT_AFTER(p, te, tqe_q);
2966effc713SDoug Rabson 	}
297df8bae1dSRodney W. Grimes 
298df8bae1dSRodney W. Grimes present:
299df8bae1dSRodney W. Grimes 	/*
300df8bae1dSRodney W. Grimes 	 * Present data to user, advancing rcv_nxt through
301df8bae1dSRodney W. Grimes 	 * completed sequence space.
302df8bae1dSRodney W. Grimes 	 */
303a0292f23SGarrett Wollman 	if (!TCPS_HAVEESTABLISHED(tp->t_state))
304df8bae1dSRodney W. Grimes 		return (0);
305fb59c426SYoshinobu Inoue 	q = LIST_FIRST(&tp->t_segq);
306fb59c426SYoshinobu Inoue 	if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
307df8bae1dSRodney W. Grimes 		return (0);
308df8bae1dSRodney W. Grimes 	do {
309fb59c426SYoshinobu Inoue 		tp->rcv_nxt += q->tqe_len;
310fb59c426SYoshinobu Inoue 		flags = q->tqe_th->th_flags & TH_FIN;
311fb59c426SYoshinobu Inoue 		nq = LIST_NEXT(q, tqe_q);
312fb59c426SYoshinobu Inoue 		LIST_REMOVE(q, tqe_q);
313df8bae1dSRodney W. Grimes 		if (so->so_state & SS_CANTRCVMORE)
314fb59c426SYoshinobu Inoue 			m_freem(q->tqe_m);
315df8bae1dSRodney W. Grimes 		else
316fb59c426SYoshinobu Inoue 			sbappend(&so->so_rcv, q->tqe_m);
317fb59c426SYoshinobu Inoue 		FREE(q, M_TSEGQ);
3186effc713SDoug Rabson 		q = nq;
319fb59c426SYoshinobu Inoue 	} while (q && q->tqe_th->th_seq == tp->rcv_nxt);
320fb59c426SYoshinobu Inoue 	ND6_HINT(tp);
321df8bae1dSRodney W. Grimes 	sorwakeup(so);
322df8bae1dSRodney W. Grimes 	return (flags);
323df8bae1dSRodney W. Grimes }
324df8bae1dSRodney W. Grimes 
325df8bae1dSRodney W. Grimes /*
326df8bae1dSRodney W. Grimes  * TCP input routine, follows pages 65-76 of the
327df8bae1dSRodney W. Grimes  * protocol specification dated September, 1981 very closely.
328df8bae1dSRodney W. Grimes  */
329fb59c426SYoshinobu Inoue #ifdef INET6
330fb59c426SYoshinobu Inoue int
331fb59c426SYoshinobu Inoue tcp6_input(mp, offp, proto)
332fb59c426SYoshinobu Inoue 	struct mbuf **mp;
333fb59c426SYoshinobu Inoue 	int *offp, proto;
334fb59c426SYoshinobu Inoue {
335fb59c426SYoshinobu Inoue 	register struct mbuf *m = *mp;
336fb59c426SYoshinobu Inoue 
337fb59c426SYoshinobu Inoue 	IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
338fb59c426SYoshinobu Inoue 
339fb59c426SYoshinobu Inoue 	/*
340fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
341fb59c426SYoshinobu Inoue 	 * better place to put this in?
342fb59c426SYoshinobu Inoue 	 */
343fb59c426SYoshinobu Inoue 	if (m->m_flags & M_ANYCAST6) {
344fb59c426SYoshinobu Inoue 		struct ip6_hdr *ip6;
345fb59c426SYoshinobu Inoue 
346fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
347fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
348fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
349fb59c426SYoshinobu Inoue 		return IPPROTO_DONE;
350fb59c426SYoshinobu Inoue 	}
351fb59c426SYoshinobu Inoue 
352fb59c426SYoshinobu Inoue 	tcp_input(m, *offp, proto);
353fb59c426SYoshinobu Inoue 	return IPPROTO_DONE;
354fb59c426SYoshinobu Inoue }
355fb59c426SYoshinobu Inoue #endif
356fb59c426SYoshinobu Inoue 
357df8bae1dSRodney W. Grimes void
3586a800098SYoshinobu Inoue tcp_input(m, off0, proto)
359df8bae1dSRodney W. Grimes 	register struct mbuf *m;
3606a800098SYoshinobu Inoue 	int off0, proto;
361df8bae1dSRodney W. Grimes {
362fb59c426SYoshinobu Inoue 	register struct tcphdr *th;
363fb59c426SYoshinobu Inoue 	register struct ip *ip = NULL;
364fb59c426SYoshinobu Inoue 	register struct ipovly *ipov;
365df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
366e79adb8eSGarrett Wollman 	u_char *optp = NULL;
36726f9a767SRodney W. Grimes 	int optlen = 0;
368df8bae1dSRodney W. Grimes 	int len, tlen, off;
369fb59c426SYoshinobu Inoue 	int drop_hdrlen;
370df8bae1dSRodney W. Grimes 	register struct tcpcb *tp = 0;
371fb59c426SYoshinobu Inoue 	register int thflags;
37226f9a767SRodney W. Grimes 	struct socket *so = 0;
373df8bae1dSRodney W. Grimes 	int todrop, acked, ourfinisacked, needoutput = 0;
374df8bae1dSRodney W. Grimes 	struct in_addr laddr;
375fb59c426SYoshinobu Inoue #ifdef INET6
376fb59c426SYoshinobu Inoue 	struct in6_addr laddr6;
377fb59c426SYoshinobu Inoue #endif
378df8bae1dSRodney W. Grimes 	int dropsocket = 0;
379df8bae1dSRodney W. Grimes 	int iss = 0;
380a0292f23SGarrett Wollman 	u_long tiwin;
381a0292f23SGarrett Wollman 	struct tcpopt to;		/* options in this segment */
382a0292f23SGarrett Wollman 	struct rmxp_tao *taop;		/* pointer to our TAO cache entry */
383a0292f23SGarrett Wollman 	struct rmxp_tao	tao_noncached;	/* in case there's no cached entry */
384610ee2f9SDavid Greenman #ifdef TCPDEBUG
385610ee2f9SDavid Greenman 	short ostate = 0;
386610ee2f9SDavid Greenman #endif
387fb59c426SYoshinobu Inoue #ifdef INET6
388fb59c426SYoshinobu Inoue 	struct ip6_hdr *ip6 = NULL;
389fb59c426SYoshinobu Inoue 	int isipv6;
390fb59c426SYoshinobu Inoue #endif /* INET6 */
391df8bae1dSRodney W. Grimes 
392fb59c426SYoshinobu Inoue #ifdef INET6
393fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
394fb59c426SYoshinobu Inoue #endif
395a0292f23SGarrett Wollman 	bzero((char *)&to, sizeof(to));
396a0292f23SGarrett Wollman 
397df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvtotal++;
398fb59c426SYoshinobu Inoue 
399fb59c426SYoshinobu Inoue #ifdef INET6
400fb59c426SYoshinobu Inoue 	if (isipv6) {
401fb59c426SYoshinobu Inoue 		/* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
402fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
403fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
404fb59c426SYoshinobu Inoue 		if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) {
405fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbadsum++;
406fb59c426SYoshinobu Inoue 			goto drop;
407fb59c426SYoshinobu Inoue 		}
408fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
409fb59c426SYoshinobu Inoue 	} else
410fb59c426SYoshinobu Inoue #endif /* INET6 */
411fb59c426SYoshinobu Inoue       {
412df8bae1dSRodney W. Grimes 	/*
413df8bae1dSRodney W. Grimes 	 * Get IP and TCP header together in first mbuf.
414df8bae1dSRodney W. Grimes 	 * Note: IP leaves IP header in first mbuf.
415df8bae1dSRodney W. Grimes 	 */
416fb59c426SYoshinobu Inoue 	if (off0 > sizeof (struct ip)) {
417df8bae1dSRodney W. Grimes 		ip_stripoptions(m, (struct mbuf *)0);
418fb59c426SYoshinobu Inoue 		off0 = sizeof(struct ip);
419fb59c426SYoshinobu Inoue 	}
420df8bae1dSRodney W. Grimes 	if (m->m_len < sizeof (struct tcpiphdr)) {
421df8bae1dSRodney W. Grimes 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
422df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvshort++;
423df8bae1dSRodney W. Grimes 			return;
424df8bae1dSRodney W. Grimes 		}
425df8bae1dSRodney W. Grimes 	}
426fb59c426SYoshinobu Inoue 	ip = mtod(m, struct ip *);
427fb59c426SYoshinobu Inoue 	ipov = (struct ipovly *)ip;
428df8bae1dSRodney W. Grimes 
429df8bae1dSRodney W. Grimes 	/*
430df8bae1dSRodney W. Grimes 	 * Checksum extended TCP header and data.
431df8bae1dSRodney W. Grimes 	 */
432fb59c426SYoshinobu Inoue 	tlen = ip->ip_len;
433df8bae1dSRodney W. Grimes 	len = sizeof (struct ip) + tlen;
434fb59c426SYoshinobu Inoue 	bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
435fb59c426SYoshinobu Inoue 	ipov->ih_len = (u_short)tlen;
436fb59c426SYoshinobu Inoue 	HTONS(ipov->ih_len);
437fb59c426SYoshinobu Inoue 	th = (struct tcphdr *)((caddr_t)ip + off0);
438fb59c426SYoshinobu Inoue 	th->th_sum = in_cksum(m, len);
439fb59c426SYoshinobu Inoue 	if (th->th_sum) {
440df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvbadsum++;
441df8bae1dSRodney W. Grimes 		goto drop;
442df8bae1dSRodney W. Grimes 	}
443fb59c426SYoshinobu Inoue #ifdef INET6
444fb59c426SYoshinobu Inoue 	/* Re-initialization for later version check */
445fb59c426SYoshinobu Inoue 	ip->ip_v = IPVERSION;
446fb59c426SYoshinobu Inoue #endif
447fb59c426SYoshinobu Inoue       }
448df8bae1dSRodney W. Grimes 
449df8bae1dSRodney W. Grimes 	/*
450df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
451df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
452df8bae1dSRodney W. Grimes 	 */
453fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
454df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
455df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvbadoff++;
456df8bae1dSRodney W. Grimes 		goto drop;
457df8bae1dSRodney W. Grimes 	}
458fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
459df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
460fb59c426SYoshinobu Inoue #ifdef INET6
461fb59c426SYoshinobu Inoue 		if (isipv6) {
462fb59c426SYoshinobu Inoue 			IP6_EXTHDR_CHECK(m, off0, off, );
463fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
464fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
465fb59c426SYoshinobu Inoue 		} else
466fb59c426SYoshinobu Inoue #endif /* INET6 */
467fb59c426SYoshinobu Inoue 	      {
468df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof(struct ip) + off) {
469df8bae1dSRodney W. Grimes 			if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
470df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvshort++;
471df8bae1dSRodney W. Grimes 				return;
472df8bae1dSRodney W. Grimes 			}
473fb59c426SYoshinobu Inoue 			ip = mtod(m, struct ip *);
474fb59c426SYoshinobu Inoue 			ipov = (struct ipovly *)ip;
475fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip + off0);
476fb59c426SYoshinobu Inoue 		}
477df8bae1dSRodney W. Grimes 	      }
478df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
479fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
480df8bae1dSRodney W. Grimes 	}
481fb59c426SYoshinobu Inoue 	thflags = th->th_flags;
482df8bae1dSRodney W. Grimes 
483e46cd3d4SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN
484e46cd3d4SDag-Erling Smørgrav 	/*
485e46cd3d4SDag-Erling Smørgrav 	 * If the drop_synfin option is enabled, drop all packets with
486e46cd3d4SDag-Erling Smørgrav 	 * both the SYN and FIN bits set. This prevents e.g. nmap from
487e46cd3d4SDag-Erling Smørgrav 	 * identifying the TCP/IP stack.
488e46cd3d4SDag-Erling Smørgrav 	 *
489e46cd3d4SDag-Erling Smørgrav 	 * This is incompatible with RFC1644 extensions (T/TCP).
490e46cd3d4SDag-Erling Smørgrav 	 */
491fb59c426SYoshinobu Inoue 	if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN))
492e46cd3d4SDag-Erling Smørgrav 		goto drop;
493e46cd3d4SDag-Erling Smørgrav #endif
494e46cd3d4SDag-Erling Smørgrav 
495df8bae1dSRodney W. Grimes 	/*
496df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
497df8bae1dSRodney W. Grimes 	 */
498fb59c426SYoshinobu Inoue 	NTOHL(th->th_seq);
499fb59c426SYoshinobu Inoue 	NTOHL(th->th_ack);
500fb59c426SYoshinobu Inoue 	NTOHS(th->th_win);
501fb59c426SYoshinobu Inoue 	NTOHS(th->th_urp);
502df8bae1dSRodney W. Grimes 
503df8bae1dSRodney W. Grimes 	/*
504fb59c426SYoshinobu Inoue 	 * Delay droping TCP, IP headers, IPv6 ext headers, and TCP options,
505fb59c426SYoshinobu Inoue 	 * until after ip6_savecontrol() is called and before other functions
506fb59c426SYoshinobu Inoue 	 * which don't want those proto headers.
507fb59c426SYoshinobu Inoue 	 * Because ip6_savecontrol() is going to parse the mbuf to
508fb59c426SYoshinobu Inoue 	 * search for data to be passed up to user-land, it wants mbuf
509fb59c426SYoshinobu Inoue 	 * parameters to be unchanged.
510755c1f07SAndras Olah 	 */
511fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
512755c1f07SAndras Olah 
513755c1f07SAndras Olah 	/*
514df8bae1dSRodney W. Grimes 	 * Locate pcb for segment.
515df8bae1dSRodney W. Grimes 	 */
516df8bae1dSRodney W. Grimes findpcb:
517f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD
518fb59c426SYoshinobu Inoue 	if (ip_fw_fwd_addr != NULL
519fb59c426SYoshinobu Inoue #ifdef INET6
520fb59c426SYoshinobu Inoue 	    && isipv6 == NULL /* IPv6 support is not yet */
521fb59c426SYoshinobu Inoue #endif /* INET6 */
522fb59c426SYoshinobu Inoue 	    ) {
523f9e354dfSJulian Elischer 		/*
524f9e354dfSJulian Elischer 		 * Diverted. Pretend to be the destination.
525f9e354dfSJulian Elischer 		 * already got one like this?
526f9e354dfSJulian Elischer 		 */
527fb59c426SYoshinobu Inoue 		inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport,
528fb59c426SYoshinobu Inoue 			ip->ip_dst, th->th_dport, 0, m->m_pkthdr.rcvif);
529f9e354dfSJulian Elischer 		if (!inp) {
530f9e354dfSJulian Elischer 			/*
531f9e354dfSJulian Elischer 			 * No, then it's new. Try find the ambushing socket
532f9e354dfSJulian Elischer 			 */
533f9e354dfSJulian Elischer 			if (!ip_fw_fwd_addr->sin_port) {
534fb59c426SYoshinobu Inoue 				inp = in_pcblookup_hash(&tcbinfo, ip->ip_src,
535fb59c426SYoshinobu Inoue 				    th->th_sport, ip_fw_fwd_addr->sin_addr,
536fb59c426SYoshinobu Inoue 				    th->th_dport, 1, m->m_pkthdr.rcvif);
537f9e354dfSJulian Elischer 			} else {
538f9e354dfSJulian Elischer 				inp = in_pcblookup_hash(&tcbinfo,
539fb59c426SYoshinobu Inoue 				    ip->ip_src, th->th_sport,
540f9e354dfSJulian Elischer 	    			    ip_fw_fwd_addr->sin_addr,
541cfa1ca9dSYoshinobu Inoue 				    ntohs(ip_fw_fwd_addr->sin_port), 1,
542cfa1ca9dSYoshinobu Inoue 				    m->m_pkthdr.rcvif);
543f9e354dfSJulian Elischer 			}
544f9e354dfSJulian Elischer 		}
545f9e354dfSJulian Elischer 		ip_fw_fwd_addr = NULL;
546f9e354dfSJulian Elischer 	} else
547f9e354dfSJulian Elischer #endif	/* IPFIREWALL_FORWARD */
548fb59c426SYoshinobu Inoue       {
549fb59c426SYoshinobu Inoue #ifdef INET6
550fb59c426SYoshinobu Inoue 	if (isipv6)
551fb59c426SYoshinobu Inoue 		inp = in6_pcblookup_hash(&tcbinfo, &ip6->ip6_src, th->th_sport,
552fb59c426SYoshinobu Inoue 					 &ip6->ip6_dst, th->th_dport, 1,
553fb59c426SYoshinobu Inoue 					 m->m_pkthdr.rcvif);
554fb59c426SYoshinobu Inoue 	else
555fb59c426SYoshinobu Inoue #endif /* INET6 */
556fb59c426SYoshinobu Inoue 	inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport,
557fb59c426SYoshinobu Inoue 	    ip->ip_dst, th->th_dport, 1, m->m_pkthdr.rcvif);
558fb59c426SYoshinobu Inoue       }
559f9e354dfSJulian Elischer 
560fb59c426SYoshinobu Inoue #ifdef IPSEC
561fb59c426SYoshinobu Inoue #ifdef INET6
562fb59c426SYoshinobu Inoue 	if (isipv6) {
563fb59c426SYoshinobu Inoue 		if (inp != NULL && ipsec6_in_reject_so(m, inp->inp_socket)) {
564fb59c426SYoshinobu Inoue 			ipsec6stat.in_polvio++;
565fb59c426SYoshinobu Inoue 			goto drop;
566fb59c426SYoshinobu Inoue 		}
567fb59c426SYoshinobu Inoue 	} else
568fb59c426SYoshinobu Inoue #endif /* INET6 */
569fb59c426SYoshinobu Inoue 	if (inp != NULL && ipsec4_in_reject_so(m, inp->inp_socket)) {
570fb59c426SYoshinobu Inoue 		ipsecstat.in_polvio++;
571fb59c426SYoshinobu Inoue 		goto drop;
572fb59c426SYoshinobu Inoue 	}
573fb59c426SYoshinobu Inoue #endif /*IPSEC*/
574df8bae1dSRodney W. Grimes 
575df8bae1dSRodney W. Grimes 	/*
576df8bae1dSRodney W. Grimes 	 * If the state is CLOSED (i.e., TCB does not exist) then
577df8bae1dSRodney W. Grimes 	 * all data in the incoming segment is discarded.
578df8bae1dSRodney W. Grimes 	 * If the TCB exists but is in CLOSED state, it is embryonic,
579df8bae1dSRodney W. Grimes 	 * but should either do a listen or a connect soon.
580df8bae1dSRodney W. Grimes 	 */
581816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
5822e4e1b4cSGeoff Rehmet 		if (log_in_vain) {
583fb59c426SYoshinobu Inoue #ifdef INET6
584fb59c426SYoshinobu Inoue 			char dbuf[INET6_ADDRSTRLEN], sbuf[INET6_ADDRSTRLEN];
585fb59c426SYoshinobu Inoue #else /* INET6 */
586fb59c426SYoshinobu Inoue 			char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"];
587fb59c426SYoshinobu Inoue #endif /* INET6 */
58875cfc95fSAndrey A. Chernov 
589fb59c426SYoshinobu Inoue #ifdef INET6
590fb59c426SYoshinobu Inoue 			if (isipv6) {
591fb59c426SYoshinobu Inoue 				strcpy(dbuf, ip6_sprintf(&ip6->ip6_dst));
592fb59c426SYoshinobu Inoue 				strcpy(sbuf, ip6_sprintf(&ip6->ip6_src));
593fb59c426SYoshinobu Inoue 			} else
594fb59c426SYoshinobu Inoue #endif
595fb59c426SYoshinobu Inoue 		      {
596fb59c426SYoshinobu Inoue 			strcpy(dbuf, inet_ntoa(ip->ip_dst));
597fb59c426SYoshinobu Inoue 			strcpy(sbuf, inet_ntoa(ip->ip_src));
598fb59c426SYoshinobu Inoue 		      }
5992e4e1b4cSGeoff Rehmet 			switch (log_in_vain) {
6002e4e1b4cSGeoff Rehmet 			case 1:
601fb59c426SYoshinobu Inoue 				if(thflags & TH_SYN)
602592071e8SBruce Evans 					log(LOG_INFO,
603592071e8SBruce Evans 			    		"Connection attempt to TCP %s:%d from %s:%d\n",
604fb59c426SYoshinobu Inoue 			    		dbuf, ntohs(th->th_dport),
605fb59c426SYoshinobu Inoue 					sbuf,
606fb59c426SYoshinobu Inoue 					ntohs(th->th_sport));
6072e4e1b4cSGeoff Rehmet 				break;
6082e4e1b4cSGeoff Rehmet 			case 2:
6092e4e1b4cSGeoff Rehmet 				log(LOG_INFO,
6102e4e1b4cSGeoff Rehmet 			    	"Connection attempt to TCP %s:%d from %s:%d flags:0x%x\n",
611fb59c426SYoshinobu Inoue 			    	dbuf, ntohs(th->th_dport), sbuf,
612fb59c426SYoshinobu Inoue 			    	ntohs(th->th_sport), thflags);
6132e4e1b4cSGeoff Rehmet 				break;
6142e4e1b4cSGeoff Rehmet 			default:
6152e4e1b4cSGeoff Rehmet 				break;
6162e4e1b4cSGeoff Rehmet 			}
61775cfc95fSAndrey A. Chernov 		}
61851508de1SMatthew Dillon #ifdef ICMP_BANDLIM
61951508de1SMatthew Dillon 		if (badport_bandlim(1) < 0)
62051508de1SMatthew Dillon 			goto drop;
62151508de1SMatthew Dillon #endif
6222e4e1b4cSGeoff Rehmet 		if (blackhole) {
6232e4e1b4cSGeoff Rehmet 			switch (blackhole) {
624828b7f40SGeoff Rehmet 			case 1:
625fb59c426SYoshinobu Inoue 				if (thflags & TH_SYN)
626828b7f40SGeoff Rehmet 					goto drop;
627828b7f40SGeoff Rehmet 				break;
628828b7f40SGeoff Rehmet 			case 2:
629828b7f40SGeoff Rehmet 				goto drop;
630828b7f40SGeoff Rehmet 			default:
631828b7f40SGeoff Rehmet 				goto drop;
6322e4e1b4cSGeoff Rehmet 			}
633828b7f40SGeoff Rehmet 		}
634df8bae1dSRodney W. Grimes 		goto dropwithreset;
635816a3d83SPoul-Henning Kamp 	}
636df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
637df8bae1dSRodney W. Grimes 	if (tp == 0)
638df8bae1dSRodney W. Grimes 		goto dropwithreset;
639df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_CLOSED)
640df8bae1dSRodney W. Grimes 		goto drop;
641df8bae1dSRodney W. Grimes 
642df8bae1dSRodney W. Grimes 	/* Unscale the window into a 32-bit value. */
643fb59c426SYoshinobu Inoue 	if ((thflags & TH_SYN) == 0)
644fb59c426SYoshinobu Inoue 		tiwin = th->th_win << tp->snd_scale;
645df8bae1dSRodney W. Grimes 	else
646fb59c426SYoshinobu Inoue 		tiwin = th->th_win;
647fb59c426SYoshinobu Inoue 
648fb59c426SYoshinobu Inoue #ifdef INET6
649fb59c426SYoshinobu Inoue 	/* save packet options if user wanted */
650fb59c426SYoshinobu Inoue 	if (inp->in6p_flags & INP_CONTROLOPTS) {
651fb59c426SYoshinobu Inoue 		if (inp->in6p_options) {
652fb59c426SYoshinobu Inoue 			m_freem(inp->in6p_options);
653fb59c426SYoshinobu Inoue 			inp->in6p_options = 0;
654fb59c426SYoshinobu Inoue 		}
655fb59c426SYoshinobu Inoue 		ip6_savecontrol(inp, &inp->in6p_options, ip6, m);
656fb59c426SYoshinobu Inoue 	}
6578972cdb1SYoshinobu Inoue         /* else, should also do ip_srcroute() here? */
658fb59c426SYoshinobu Inoue #endif /* INET6 */
659df8bae1dSRodney W. Grimes 
660df8bae1dSRodney W. Grimes 	so = inp->inp_socket;
661df8bae1dSRodney W. Grimes 	if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
662610ee2f9SDavid Greenman #ifdef TCPDEBUG
663df8bae1dSRodney W. Grimes 		if (so->so_options & SO_DEBUG) {
664df8bae1dSRodney W. Grimes 			ostate = tp->t_state;
665fb59c426SYoshinobu Inoue #ifdef INET6
666fb59c426SYoshinobu Inoue 			if (isipv6)
667fb59c426SYoshinobu Inoue 				bcopy((char *)ip6, (char *)tcp_saveipgen,
668fb59c426SYoshinobu Inoue 				      sizeof(*ip6));
669fb59c426SYoshinobu Inoue 			else
670fb59c426SYoshinobu Inoue #endif /* INET6 */
671fb59c426SYoshinobu Inoue 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
672fb59c426SYoshinobu Inoue 			tcp_savetcp = *th;
673df8bae1dSRodney W. Grimes 		}
674610ee2f9SDavid Greenman #endif
675df8bae1dSRodney W. Grimes 		if (so->so_options & SO_ACCEPTCONN) {
676a0292f23SGarrett Wollman 			register struct tcpcb *tp0 = tp;
6774195b4afSPaul Traina 			struct socket *so2;
678fb59c426SYoshinobu Inoue #ifdef IPSEC
679fb59c426SYoshinobu Inoue 			struct socket *oso;
680fb59c426SYoshinobu Inoue #endif
681fb59c426SYoshinobu Inoue #ifdef INET6
682fb59c426SYoshinobu Inoue 			struct inpcb *oinp = sotoinpcb(so);
683fb59c426SYoshinobu Inoue #endif /* INET6 */
684fb59c426SYoshinobu Inoue 
685fb59c426SYoshinobu Inoue #ifndef IPSEC
686fb59c426SYoshinobu Inoue 			/*
687fb59c426SYoshinobu Inoue 			 * Current IPsec implementation makes incorrect IPsec
688fb59c426SYoshinobu Inoue 			 * cache if this check is done here.
689fb59c426SYoshinobu Inoue 			 * So delay this until duplicated socket is created.
690fb59c426SYoshinobu Inoue 			 */
691fb59c426SYoshinobu Inoue 			if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
6924195b4afSPaul Traina 				/*
693ebb0cbeaSPaul Traina 				 * Note: dropwithreset makes sure we don't
694ebb0cbeaSPaul Traina 				 * send a RST in response to a RST.
6954195b4afSPaul Traina 				 */
696fb59c426SYoshinobu Inoue 				if (thflags & TH_ACK) {
697ebb0cbeaSPaul Traina 					tcpstat.tcps_badsyn++;
698ebb0cbeaSPaul Traina 					goto dropwithreset;
6994195b4afSPaul Traina 				}
700ebb0cbeaSPaul Traina 				goto drop;
701ebb0cbeaSPaul Traina 			}
702fb59c426SYoshinobu Inoue #endif
703ebb0cbeaSPaul Traina 			so2 = sonewconn(so, 0);
704ebb0cbeaSPaul Traina 			if (so2 == 0) {
705ebb0cbeaSPaul Traina 				tcpstat.tcps_listendrop++;
706ebb0cbeaSPaul Traina 				so2 = sodropablereq(so);
707a51764a8SPaul Traina 				if (so2) {
708ebb0cbeaSPaul Traina 					tcp_drop(sototcpcb(so2), ETIMEDOUT);
709a51764a8SPaul Traina 					so2 = sonewconn(so, 0);
710a51764a8SPaul Traina 				}
711a51764a8SPaul Traina 				if (!so2)
712df8bae1dSRodney W. Grimes 					goto drop;
7131347f5b8SGuido van Rooij 			}
714fb59c426SYoshinobu Inoue #ifdef IPSEC
715fb59c426SYoshinobu Inoue 			oso = so;
716fb59c426SYoshinobu Inoue #endif
7174195b4afSPaul Traina 			so = so2;
718df8bae1dSRodney W. Grimes 			/*
719df8bae1dSRodney W. Grimes 			 * This is ugly, but ....
720df8bae1dSRodney W. Grimes 			 *
721df8bae1dSRodney W. Grimes 			 * Mark socket as temporary until we're
722df8bae1dSRodney W. Grimes 			 * committed to keeping it.  The code at
723df8bae1dSRodney W. Grimes 			 * ``drop'' and ``dropwithreset'' check the
724df8bae1dSRodney W. Grimes 			 * flag dropsocket to see if the temporary
725df8bae1dSRodney W. Grimes 			 * socket created here should be discarded.
726df8bae1dSRodney W. Grimes 			 * We mark the socket as discardable until
727df8bae1dSRodney W. Grimes 			 * we're committed to it below in TCPS_LISTEN.
728df8bae1dSRodney W. Grimes 			 */
729df8bae1dSRodney W. Grimes 			dropsocket++;
730df8bae1dSRodney W. Grimes 			inp = (struct inpcb *)so->so_pcb;
731fb59c426SYoshinobu Inoue #ifdef INET6
732fb59c426SYoshinobu Inoue 			if (isipv6)
733fb59c426SYoshinobu Inoue 				inp->in6p_laddr = ip6->ip6_dst;
734fb59c426SYoshinobu Inoue 			else {
735fb59c426SYoshinobu Inoue 				if (ip6_mapped_addr_on) {
736fb59c426SYoshinobu Inoue 					inp->inp_vflag &= ~INP_IPV6;
737fb59c426SYoshinobu Inoue 					inp->inp_vflag |= INP_IPV4;
738fb59c426SYoshinobu Inoue 				}
739fb59c426SYoshinobu Inoue #endif /* INET6 */
740fb59c426SYoshinobu Inoue 			inp->inp_laddr = ip->ip_dst;
741fb59c426SYoshinobu Inoue #ifdef INET6
742fb59c426SYoshinobu Inoue 			}
743fb59c426SYoshinobu Inoue #endif /* INET6 */
744fb59c426SYoshinobu Inoue 			inp->inp_lport = th->th_dport;
745c3229e05SDavid Greenman 			if (in_pcbinshash(inp) != 0) {
746c3229e05SDavid Greenman 				/*
7475a8c77a8SDavid E. O'Brien 				 * Undo the assignments above if we failed to
7485a8c77a8SDavid E. O'Brien 				 * put the PCB on the hash lists.
749c3229e05SDavid Greenman 				 */
750fb59c426SYoshinobu Inoue #ifdef INET6
751fb59c426SYoshinobu Inoue 				if (isipv6)
752fb59c426SYoshinobu Inoue 					inp->in6p_laddr = in6addr_any;
753fb59c426SYoshinobu Inoue 				else
754fb59c426SYoshinobu Inoue #endif /* INET6 */
755c3229e05SDavid Greenman 				inp->inp_laddr.s_addr = INADDR_ANY;
756c3229e05SDavid Greenman 				inp->inp_lport = 0;
757c3229e05SDavid Greenman 				goto drop;
758c3229e05SDavid Greenman 			}
759fb59c426SYoshinobu Inoue #ifdef IPSEC
760fb59c426SYoshinobu Inoue 			/*
761fb59c426SYoshinobu Inoue 			 * To avoid creating incorrectly cached IPsec
762fb59c426SYoshinobu Inoue 			 * association, this is need to be done here.
763fb59c426SYoshinobu Inoue 			 *
764fb59c426SYoshinobu Inoue 			 * Subject: (KAME-snap 748)
765fb59c426SYoshinobu Inoue 			 * From: Wayne Knowles <w.knowles@niwa.cri.nz>
766fb59c426SYoshinobu Inoue 			 * ftp://ftp.kame.net/pub/mail-list/snap-users/748
767fb59c426SYoshinobu Inoue 			 */
768fb59c426SYoshinobu Inoue 			if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
769fb59c426SYoshinobu Inoue 				/*
770fb59c426SYoshinobu Inoue 				 * Note: dropwithreset makes sure we don't
771fb59c426SYoshinobu Inoue 				 * send a RST in response to a RST.
772fb59c426SYoshinobu Inoue 				 */
773fb59c426SYoshinobu Inoue 				if (thflags & TH_ACK) {
774fb59c426SYoshinobu Inoue 					tcpstat.tcps_badsyn++;
775fb59c426SYoshinobu Inoue 					goto dropwithreset;
776fb59c426SYoshinobu Inoue 				}
777fb59c426SYoshinobu Inoue 				goto drop;
778fb59c426SYoshinobu Inoue 			}
779fb59c426SYoshinobu Inoue #endif
780fb59c426SYoshinobu Inoue #ifdef INET6
781fb59c426SYoshinobu Inoue 			if (isipv6) {
782fb59c426SYoshinobu Inoue 				/*
783fb59c426SYoshinobu Inoue 				 * inherit socket options from the listening
784fb59c426SYoshinobu Inoue 				 * socket.
785fb59c426SYoshinobu Inoue 				 */
786fb59c426SYoshinobu Inoue 				inp->inp_flags |=
787fb59c426SYoshinobu Inoue 					oinp->inp_flags & INP_CONTROLOPTS;
788fb59c426SYoshinobu Inoue 				if (inp->inp_flags & INP_CONTROLOPTS) {
789fb59c426SYoshinobu Inoue 					if (inp->in6p_options) {
790fb59c426SYoshinobu Inoue 						m_freem(inp->in6p_options);
791fb59c426SYoshinobu Inoue 						inp->in6p_options = 0;
792fb59c426SYoshinobu Inoue 					}
793fb59c426SYoshinobu Inoue 					ip6_savecontrol(inp,
794fb59c426SYoshinobu Inoue 							&inp->in6p_options,
795fb59c426SYoshinobu Inoue 							ip6, m);
796fb59c426SYoshinobu Inoue 				}
797fb59c426SYoshinobu Inoue 			} else
798fb59c426SYoshinobu Inoue #endif /* INET6 */
799df8bae1dSRodney W. Grimes 			inp->inp_options = ip_srcroute();
800fb59c426SYoshinobu Inoue #ifdef IPSEC
801fb59c426SYoshinobu Inoue 			/* copy old policy into new socket's */
802fb59c426SYoshinobu Inoue 			if (ipsec_copy_policy(sotoinpcb(oso)->inp_sp,
803fb59c426SYoshinobu Inoue 			                      inp->inp_sp))
804fb59c426SYoshinobu Inoue 				printf("tcp_input: could not copy policy\n");
805fb59c426SYoshinobu Inoue #endif
806df8bae1dSRodney W. Grimes 			tp = intotcpcb(inp);
807df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_LISTEN;
808a0292f23SGarrett Wollman 			tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT);
809df8bae1dSRodney W. Grimes 
810a0292f23SGarrett Wollman 			/* Compute proper scaling value from buffer space */
811df8bae1dSRodney W. Grimes 			while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
8125a8c77a8SDavid E. O'Brien 			   TCP_MAXWIN << tp->request_r_scale <
8135a8c77a8SDavid E. O'Brien 			   so->so_rcv.sb_hiwat)
814df8bae1dSRodney W. Grimes 				tp->request_r_scale++;
815df8bae1dSRodney W. Grimes 		}
816df8bae1dSRodney W. Grimes 	}
817df8bae1dSRodney W. Grimes 
818df8bae1dSRodney W. Grimes 	/*
819df8bae1dSRodney W. Grimes 	 * Segment received on connection.
820df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
821df8bae1dSRodney W. Grimes 	 */
8229b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
8237ff19458SPaul Traina 	if (TCPS_HAVEESTABLISHED(tp->t_state))
8249b8b58e0SJonathan Lemon 		callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
825df8bae1dSRodney W. Grimes 
826df8bae1dSRodney W. Grimes 	/*
827df8bae1dSRodney W. Grimes 	 * Process options if not in LISTEN state,
828df8bae1dSRodney W. Grimes 	 * else do it below (after getting remote address).
829df8bae1dSRodney W. Grimes 	 */
830f9d5a964SDavid Greenman 	if (tp->t_state != TCPS_LISTEN)
831fb59c426SYoshinobu Inoue 		tcp_dooptions(tp, optp, optlen, th, &to);
832df8bae1dSRodney W. Grimes 
833df8bae1dSRodney W. Grimes 	/*
834df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
835df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
836df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
837df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
838df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
839df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
840df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
841df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
842df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
843df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
844df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
845df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
846a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
847a0292f23SGarrett Wollman 	 * Since we check for TCPS_ESTABLISHED above, it can only
848a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
849df8bae1dSRodney W. Grimes 	 */
850df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
851fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
852a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
853a0292f23SGarrett Wollman 	    ((to.to_flag & TOF_TS) == 0 ||
854a0292f23SGarrett Wollman 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
855a0292f23SGarrett Wollman 	    /*
856a0292f23SGarrett Wollman 	     * Using the CC option is compulsory if once started:
857a0292f23SGarrett Wollman 	     *   the segment is OK if no T/TCP was negotiated or
858a0292f23SGarrett Wollman 	     *   if the segment has a CC option equal to CCrecv
859a0292f23SGarrett Wollman 	     */
860a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) ||
861831a80b0SMatthew Dillon 	     ((to.to_flag & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) &&
862fb59c426SYoshinobu Inoue 	    th->th_seq == tp->rcv_nxt &&
863df8bae1dSRodney W. Grimes 	    tiwin && tiwin == tp->snd_wnd &&
864df8bae1dSRodney W. Grimes 	    tp->snd_nxt == tp->snd_max) {
865df8bae1dSRodney W. Grimes 
866df8bae1dSRodney W. Grimes 		/*
867df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
868df8bae1dSRodney W. Grimes 		 * record the timestamp.
869a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
870a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
871df8bae1dSRodney W. Grimes 		 */
872a0292f23SGarrett Wollman 		if ((to.to_flag & TOF_TS) != 0 &&
873fb59c426SYoshinobu Inoue 		   SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
8749b8b58e0SJonathan Lemon 			tp->ts_recent_age = ticks;
875a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
876df8bae1dSRodney W. Grimes 		}
877df8bae1dSRodney W. Grimes 
878fb59c426SYoshinobu Inoue 		if (tlen == 0) {
879fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
880fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
881233e8c18SGarrett Wollman 			    tp->snd_cwnd >= tp->snd_wnd &&
882233e8c18SGarrett Wollman 			    tp->t_dupacks < tcprexmtthresh) {
883df8bae1dSRodney W. Grimes 				/*
884df8bae1dSRodney W. Grimes 				 * this is a pure ack for outstanding data.
885df8bae1dSRodney W. Grimes 				 */
886df8bae1dSRodney W. Grimes 				++tcpstat.tcps_predack;
8879b8b58e0SJonathan Lemon 				/*
8889b8b58e0SJonathan Lemon 				 * "bad retransmit" recovery
8899b8b58e0SJonathan Lemon 				 */
8909b8b58e0SJonathan Lemon 				if (tp->t_rxtshift == 1 &&
8919b8b58e0SJonathan Lemon 				    ticks < tp->t_badrxtwin) {
8929b8b58e0SJonathan Lemon 					tp->snd_cwnd = tp->snd_cwnd_prev;
8939b8b58e0SJonathan Lemon 					tp->snd_ssthresh =
8949b8b58e0SJonathan Lemon 					    tp->snd_ssthresh_prev;
8959b8b58e0SJonathan Lemon 					tp->snd_nxt = tp->snd_max;
8969b8b58e0SJonathan Lemon 					tp->t_badrxtwin = 0;
8979b8b58e0SJonathan Lemon 				}
898a0292f23SGarrett Wollman 				if ((to.to_flag & TOF_TS) != 0)
899a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
9009b8b58e0SJonathan Lemon 					    ticks - to.to_tsecr + 1);
9019b8b58e0SJonathan Lemon 				else if (tp->t_rtttime &&
902fb59c426SYoshinobu Inoue 					    SEQ_GT(th->th_ack, tp->t_rtseq))
9039b8b58e0SJonathan Lemon 					tcp_xmit_timer(tp, ticks - tp->t_rtttime);
904fb59c426SYoshinobu Inoue 				acked = th->th_ack - tp->snd_una;
905df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackpack++;
906df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackbyte += acked;
907df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
908fb59c426SYoshinobu Inoue 				tp->snd_una = th->th_ack;
909df8bae1dSRodney W. Grimes 				m_freem(m);
910fb59c426SYoshinobu Inoue 				ND6_HINT(tp); /* some progress has been done */
911df8bae1dSRodney W. Grimes 
912df8bae1dSRodney W. Grimes 				/*
913df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
914df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
915df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
916df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
917df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
918df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
919df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
920df8bae1dSRodney W. Grimes 				 */
921df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
9229b8b58e0SJonathan Lemon 					callout_stop(tp->tt_rexmt);
9239b8b58e0SJonathan Lemon 				else if (!callout_active(tp->tt_persist))
9249b8b58e0SJonathan Lemon 					callout_reset(tp->tt_rexmt,
9259b8b58e0SJonathan Lemon 						      tp->t_rxtcur,
9269b8b58e0SJonathan Lemon 						      tcp_timer_rexmt, tp);
927df8bae1dSRodney W. Grimes 
928df8bae1dSRodney W. Grimes 				sowwakeup(so);
929df8bae1dSRodney W. Grimes 				if (so->so_snd.sb_cc)
930df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
931df8bae1dSRodney W. Grimes 				return;
932df8bae1dSRodney W. Grimes 			}
933fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
934fb59c426SYoshinobu Inoue 		    LIST_EMPTY(&tp->t_segq) &&
935fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
936df8bae1dSRodney W. Grimes 			/*
937df8bae1dSRodney W. Grimes 			 * this is a pure, in-sequence data packet
938df8bae1dSRodney W. Grimes 			 * with nothing on the reassembly queue and
939df8bae1dSRodney W. Grimes 			 * we have enough buffer space to take it.
940df8bae1dSRodney W. Grimes 			 */
941df8bae1dSRodney W. Grimes 			++tcpstat.tcps_preddat;
942fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
943df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpack++;
944fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbyte += tlen;
945fb59c426SYoshinobu Inoue 			ND6_HINT(tp);	/* some progress has been done */
946df8bae1dSRodney W. Grimes 			/*
947755c1f07SAndras Olah 			 * Add data to socket buffer.
948df8bae1dSRodney W. Grimes 			 */
949fb59c426SYoshinobu Inoue 			m_adj(m, drop_hdrlen);	/* delayed header drop */
950df8bae1dSRodney W. Grimes 			sbappend(&so->so_rcv, m);
951df8bae1dSRodney W. Grimes 			sorwakeup(so);
952f498eeeeSDavid Greenman 			if (tcp_delack_enabled) {
9539b8b58e0SJonathan Lemon 	                        callout_reset(tp->tt_delack, tcp_delacktime,
9549b8b58e0SJonathan Lemon 	                            tcp_timer_delack, tp);
955f498eeeeSDavid Greenman 			} else {
956e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
957e612a582SDavid Greenman 				tcp_output(tp);
958e612a582SDavid Greenman 			}
959df8bae1dSRodney W. Grimes 			return;
960df8bae1dSRodney W. Grimes 		}
961df8bae1dSRodney W. Grimes 	}
962df8bae1dSRodney W. Grimes 
963df8bae1dSRodney W. Grimes 	/*
964df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
965df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
966df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
967df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
968df8bae1dSRodney W. Grimes 	 */
969df8bae1dSRodney W. Grimes 	{ int win;
970df8bae1dSRodney W. Grimes 
971df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
972df8bae1dSRodney W. Grimes 	if (win < 0)
973df8bae1dSRodney W. Grimes 		win = 0;
97466e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
975df8bae1dSRodney W. Grimes 	}
976df8bae1dSRodney W. Grimes 
977df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
978df8bae1dSRodney W. Grimes 
979df8bae1dSRodney W. Grimes 	/*
980df8bae1dSRodney W. Grimes 	 * If the state is LISTEN then ignore segment if it contains an RST.
981df8bae1dSRodney W. Grimes 	 * If the segment contains an ACK then it is bad and send a RST.
982df8bae1dSRodney W. Grimes 	 * If it does not contain a SYN then it is not interesting; drop it.
983764d8cefSBill Fenner 	 * If it is from this socket, drop it, it must be forged.
984df8bae1dSRodney W. Grimes 	 * Don't bother responding if the destination was a broadcast.
985df8bae1dSRodney W. Grimes 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
986df8bae1dSRodney W. Grimes 	 * tp->iss, and send a segment:
987df8bae1dSRodney W. Grimes 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
988df8bae1dSRodney W. Grimes 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
989df8bae1dSRodney W. Grimes 	 * Fill in remote peer address fields if not previously specified.
990df8bae1dSRodney W. Grimes 	 * Enter SYN_RECEIVED state, and process any other fields of this
991df8bae1dSRodney W. Grimes 	 * segment in this state.
992df8bae1dSRodney W. Grimes 	 */
993df8bae1dSRodney W. Grimes 	case TCPS_LISTEN: {
994df8bae1dSRodney W. Grimes 		register struct sockaddr_in *sin;
995fb59c426SYoshinobu Inoue #ifdef INET6
996fb59c426SYoshinobu Inoue 		register struct sockaddr_in6 *sin6;
997fb59c426SYoshinobu Inoue #endif
998df8bae1dSRodney W. Grimes 
999fb59c426SYoshinobu Inoue 		if (thflags & TH_RST)
1000df8bae1dSRodney W. Grimes 			goto drop;
1001fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK)
1002df8bae1dSRodney W. Grimes 			goto dropwithreset;
1003fb59c426SYoshinobu Inoue 		if ((thflags & TH_SYN) == 0)
1004df8bae1dSRodney W. Grimes 			goto drop;
1005fb59c426SYoshinobu Inoue 		if (th->th_dport == th->th_sport) {
1006fb59c426SYoshinobu Inoue #ifdef INET6
1007fb59c426SYoshinobu Inoue 			if (isipv6) {
1008fb59c426SYoshinobu Inoue 				if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1009fb59c426SYoshinobu Inoue 						       &ip6->ip6_src))
1010764d8cefSBill Fenner 					goto drop;
1011fb59c426SYoshinobu Inoue 			} else
1012fb59c426SYoshinobu Inoue #endif /* INET6 */
1013fb59c426SYoshinobu Inoue 			if (ip->ip_dst.s_addr == ip->ip_src.s_addr)
1014fb59c426SYoshinobu Inoue 				goto drop;
1015fb59c426SYoshinobu Inoue 		}
1016df8bae1dSRodney W. Grimes 		/*
1017df8bae1dSRodney W. Grimes 		 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
1018df8bae1dSRodney W. Grimes 		 * in_broadcast() should never return true on a received
1019df8bae1dSRodney W. Grimes 		 * packet with M_BCAST not set.
1020df8bae1dSRodney W. Grimes 		 */
1021fb59c426SYoshinobu Inoue 		if (m->m_flags & (M_BCAST|M_MCAST))
1022df8bae1dSRodney W. Grimes 			goto drop;
1023fb59c426SYoshinobu Inoue #ifdef INET6
1024fb59c426SYoshinobu Inoue 		if (isipv6) {
1025fb59c426SYoshinobu Inoue 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
1026fb59c426SYoshinobu Inoue 				goto drop;
1027fb59c426SYoshinobu Inoue 		} else
1028fb59c426SYoshinobu Inoue #endif
1029fb59c426SYoshinobu Inoue 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)))
1030fb59c426SYoshinobu Inoue 			goto drop;
1031fb59c426SYoshinobu Inoue #ifdef INET6
1032fb59c426SYoshinobu Inoue 		if (isipv6) {
1033fb59c426SYoshinobu Inoue 			MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6,
1034fb59c426SYoshinobu Inoue 			       M_SONAME, M_NOWAIT);
1035fb59c426SYoshinobu Inoue 			if (sin6 == NULL)
1036fb59c426SYoshinobu Inoue 				goto drop;
1037fb59c426SYoshinobu Inoue 			bzero(sin6, sizeof(*sin6));
1038fb59c426SYoshinobu Inoue 			sin6->sin6_family = AF_INET6;
1039fb59c426SYoshinobu Inoue 			sin6->sin6_len = sizeof(*sin6);
1040fb59c426SYoshinobu Inoue 			sin6->sin6_addr = ip6->ip6_src;
1041fb59c426SYoshinobu Inoue 			sin6->sin6_port = th->th_sport;
1042fb59c426SYoshinobu Inoue 			laddr6 = inp->in6p_laddr;
1043fb59c426SYoshinobu Inoue 			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1044fb59c426SYoshinobu Inoue 				inp->in6p_laddr = ip6->ip6_dst;
1045fb59c426SYoshinobu Inoue 			if (in6_pcbconnect(inp, (struct sockaddr *)sin6,
1046fb59c426SYoshinobu Inoue 					   &proc0)) {
1047fb59c426SYoshinobu Inoue 				inp->in6p_laddr = laddr6;
1048fb59c426SYoshinobu Inoue 				FREE(sin6, M_SONAME);
1049fb59c426SYoshinobu Inoue 				goto drop;
1050fb59c426SYoshinobu Inoue 			}
1051fb59c426SYoshinobu Inoue 			FREE(sin6, M_SONAME);
1052fb59c426SYoshinobu Inoue 		} else
1053fb59c426SYoshinobu Inoue #endif
1054fb59c426SYoshinobu Inoue 	      {
105557bf258eSGarrett Wollman 		MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
105657bf258eSGarrett Wollman 		       M_NOWAIT);
105757bf258eSGarrett Wollman 		if (sin == NULL)
1058df8bae1dSRodney W. Grimes 			goto drop;
1059df8bae1dSRodney W. Grimes 		sin->sin_family = AF_INET;
1060df8bae1dSRodney W. Grimes 		sin->sin_len = sizeof(*sin);
1061fb59c426SYoshinobu Inoue 		sin->sin_addr = ip->ip_src;
1062fb59c426SYoshinobu Inoue 		sin->sin_port = th->th_sport;
1063df8bae1dSRodney W. Grimes 		bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero));
1064df8bae1dSRodney W. Grimes 		laddr = inp->inp_laddr;
1065df8bae1dSRodney W. Grimes 		if (inp->inp_laddr.s_addr == INADDR_ANY)
1066fb59c426SYoshinobu Inoue 			inp->inp_laddr = ip->ip_dst;
106757bf258eSGarrett Wollman 		if (in_pcbconnect(inp, (struct sockaddr *)sin, &proc0)) {
1068df8bae1dSRodney W. Grimes 			inp->inp_laddr = laddr;
106957bf258eSGarrett Wollman 			FREE(sin, M_SONAME);
1070df8bae1dSRodney W. Grimes 			goto drop;
1071df8bae1dSRodney W. Grimes 		}
107257bf258eSGarrett Wollman 		FREE(sin, M_SONAME);
1073fb59c426SYoshinobu Inoue 	      }
1074df8bae1dSRodney W. Grimes 		tp->t_template = tcp_template(tp);
1075df8bae1dSRodney W. Grimes 		if (tp->t_template == 0) {
1076df8bae1dSRodney W. Grimes 			tp = tcp_drop(tp, ENOBUFS);
1077df8bae1dSRodney W. Grimes 			dropsocket = 0;		/* socket is already gone */
1078df8bae1dSRodney W. Grimes 			goto drop;
1079df8bae1dSRodney W. Grimes 		}
1080a0292f23SGarrett Wollman 		if ((taop = tcp_gettaocache(inp)) == NULL) {
1081a0292f23SGarrett Wollman 			taop = &tao_noncached;
1082a0292f23SGarrett Wollman 			bzero(taop, sizeof(*taop));
1083a0292f23SGarrett Wollman 		}
1084fb59c426SYoshinobu Inoue 		tcp_dooptions(tp, optp, optlen, th, &to);
1085df8bae1dSRodney W. Grimes 		if (iss)
1086df8bae1dSRodney W. Grimes 			tp->iss = iss;
1087df8bae1dSRodney W. Grimes 		else
1088df8bae1dSRodney W. Grimes 			tp->iss = tcp_iss;
1089e79adb8eSGarrett Wollman 		tcp_iss += TCP_ISSINCR/4;
1090fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
1091df8bae1dSRodney W. Grimes 		tcp_sendseqinit(tp);
1092df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
1093a0292f23SGarrett Wollman 		/*
1094a0292f23SGarrett Wollman 		 * Initialization of the tcpcb for transaction;
1095a0292f23SGarrett Wollman 		 *   set SND.WND = SEG.WND,
1096a0292f23SGarrett Wollman 		 *   initialize CCsend and CCrecv.
1097a0292f23SGarrett Wollman 		 */
1098a0292f23SGarrett Wollman 		tp->snd_wnd = tiwin;	/* initial send-window */
1099a0292f23SGarrett Wollman 		tp->cc_send = CC_INC(tcp_ccgen);
1100a0292f23SGarrett Wollman 		tp->cc_recv = to.to_cc;
1101a0292f23SGarrett Wollman 		/*
1102a0292f23SGarrett Wollman 		 * Perform TAO test on incoming CC (SEG.CC) option, if any.
1103a0292f23SGarrett Wollman 		 * - compare SEG.CC against cached CC from the same host,
1104a0292f23SGarrett Wollman 		 *	if any.
1105a0292f23SGarrett Wollman 		 * - if SEG.CC > chached value, SYN must be new and is accepted
1106a0292f23SGarrett Wollman 		 *	immediately: save new CC in the cache, mark the socket
1107a0292f23SGarrett Wollman 		 *	connected, enter ESTABLISHED state, turn on flag to
1108a0292f23SGarrett Wollman 		 *	send a SYN in the next segment.
1109a0292f23SGarrett Wollman 		 *	A virtual advertised window is set in rcv_adv to
1110a0292f23SGarrett Wollman 		 *	initialize SWS prevention.  Then enter normal segment
1111a0292f23SGarrett Wollman 		 *	processing: drop SYN, process data and FIN.
1112a0292f23SGarrett Wollman 		 * - otherwise do a normal 3-way handshake.
1113a0292f23SGarrett Wollman 		 */
1114a0292f23SGarrett Wollman 		if ((to.to_flag & TOF_CC) != 0) {
1115068373b6SGuido van Rooij 		    if (((tp->t_flags & TF_NOPUSH) != 0) &&
111611ad4550SGuido van Rooij 			taop->tao_cc != 0 && CC_GT(to.to_cc, taop->tao_cc)) {
111711ad4550SGuido van Rooij 
1118a0292f23SGarrett Wollman 			taop->tao_cc = to.to_cc;
11199b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
1120a0292f23SGarrett Wollman 			tp->t_state = TCPS_ESTABLISHED;
1121a0292f23SGarrett Wollman 
1122a0292f23SGarrett Wollman 			/*
1123a0292f23SGarrett Wollman 			 * If there is a FIN, or if there is data and the
1124a0292f23SGarrett Wollman 			 * connection is local, then delay SYN,ACK(SYN) in
1125a0292f23SGarrett Wollman 			 * the hope of piggy-backing it on a response
1126a0292f23SGarrett Wollman 			 * segment.  Otherwise must send ACK now in case
1127a0292f23SGarrett Wollman 			 * the other side is slow starting.
1128a0292f23SGarrett Wollman 			 */
1129fb59c426SYoshinobu Inoue 			if (tcp_delack_enabled && ((thflags & TH_FIN) ||
1130fb59c426SYoshinobu Inoue 			    (tlen != 0 &&
1131fb59c426SYoshinobu Inoue #ifdef INET6
1132fb59c426SYoshinobu Inoue 			      ((isipv6 && in6_localaddr(&inp->in6p_faddr))
1133fb59c426SYoshinobu Inoue 			      ||
1134fb59c426SYoshinobu Inoue 			      (!isipv6 &&
1135fb59c426SYoshinobu Inoue #endif
1136fb59c426SYoshinobu Inoue 			    in_localaddr(inp->inp_faddr)
1137fb59c426SYoshinobu Inoue #ifdef INET6
1138fb59c426SYoshinobu Inoue 			       ))
1139fb59c426SYoshinobu Inoue #endif
1140fb59c426SYoshinobu Inoue 			     ))) {
11419b8b58e0SJonathan Lemon                                 callout_reset(tp->tt_delack, tcp_delacktime,
11429b8b58e0SJonathan Lemon                                     tcp_timer_delack, tp);
11439b8b58e0SJonathan Lemon 				tp->t_flags |= TF_NEEDSYN;
11449b8b58e0SJonathan Lemon 			} else
1145a0292f23SGarrett Wollman 				tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
114607e43e10SAndras Olah 
114707e43e10SAndras Olah 			/*
114807e43e10SAndras Olah 			 * Limit the `virtual advertised window' to TCP_MAXWIN
114907e43e10SAndras Olah 			 * here.  Even if we requested window scaling, it will
115007e43e10SAndras Olah 			 * become effective only later when our SYN is acked.
115107e43e10SAndras Olah 			 */
115207e43e10SAndras Olah 			tp->rcv_adv += min(tp->rcv_wnd, TCP_MAXWIN);
1153a0292f23SGarrett Wollman 			tcpstat.tcps_connects++;
1154a0292f23SGarrett Wollman 			soisconnected(so);
11559b8b58e0SJonathan Lemon 			callout_reset(tp->tt_keep, tcp_keepinit,
11569b8b58e0SJonathan Lemon 				      tcp_timer_keep, tp);
1157a0292f23SGarrett Wollman 			dropsocket = 0;		/* committed to socket */
1158a0292f23SGarrett Wollman 			tcpstat.tcps_accepts++;
1159a0292f23SGarrett Wollman 			goto trimthenstep6;
1160a0292f23SGarrett Wollman 		    }
1161a0292f23SGarrett Wollman 		/* else do standard 3-way handshake */
1162a0292f23SGarrett Wollman 		} else {
1163a0292f23SGarrett Wollman 		    /*
1164a0292f23SGarrett Wollman 		     * No CC option, but maybe CC.NEW:
1165a0292f23SGarrett Wollman 		     *   invalidate cached value.
1166a0292f23SGarrett Wollman 		     */
1167a0292f23SGarrett Wollman 		     taop->tao_cc = 0;
1168a0292f23SGarrett Wollman 		}
1169a0292f23SGarrett Wollman 		/*
1170a0292f23SGarrett Wollman 		 * TAO test failed or there was no CC option,
1171a0292f23SGarrett Wollman 		 *    do a standard 3-way handshake.
1172a0292f23SGarrett Wollman 		 */
1173df8bae1dSRodney W. Grimes 		tp->t_flags |= TF_ACKNOW;
1174df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_SYN_RECEIVED;
11759b8b58e0SJonathan Lemon 		callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
1176df8bae1dSRodney W. Grimes 		dropsocket = 0;		/* committed to socket */
1177df8bae1dSRodney W. Grimes 		tcpstat.tcps_accepts++;
1178fb59c426SYoshinobu Inoue 		ND6_HINT((struct tcpcb *)inp->inp_ppcb);
1179df8bae1dSRodney W. Grimes 		goto trimthenstep6;
1180df8bae1dSRodney W. Grimes 		}
1181df8bae1dSRodney W. Grimes 
1182df8bae1dSRodney W. Grimes 	/*
1183764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1184764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1185764d8cefSBill Fenner 	 */
1186764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1187fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1188fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1189fb59c426SYoshinobu Inoue 		     SEQ_GT(th->th_ack, tp->snd_max)))
1190764d8cefSBill Fenner 				goto dropwithreset;
1191764d8cefSBill Fenner 		break;
1192764d8cefSBill Fenner 
1193764d8cefSBill Fenner 	/*
1194df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
1195df8bae1dSRodney W. Grimes 	 *	if seg contains an ACK, but not for our SYN, drop the input.
1196df8bae1dSRodney W. Grimes 	 *	if seg contains a RST, then drop the connection.
1197df8bae1dSRodney W. Grimes 	 *	if seg does not contain SYN, then drop it.
1198df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
1199df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
1200df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
1201df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1202df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
1203df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
1204df8bae1dSRodney W. Grimes 	 */
1205df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
1206a0292f23SGarrett Wollman 		if ((taop = tcp_gettaocache(inp)) == NULL) {
1207a0292f23SGarrett Wollman 			taop = &tao_noncached;
1208a0292f23SGarrett Wollman 			bzero(taop, sizeof(*taop));
1209a0292f23SGarrett Wollman 		}
1210a0292f23SGarrett Wollman 
1211fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1212fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
1213fb59c426SYoshinobu Inoue 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1214a0292f23SGarrett Wollman 			/*
1215a0292f23SGarrett Wollman 			 * If we have a cached CCsent for the remote host,
1216a0292f23SGarrett Wollman 			 * hence we haven't just crashed and restarted,
1217a0292f23SGarrett Wollman 			 * do not send a RST.  This may be a retransmission
1218a0292f23SGarrett Wollman 			 * from the other side after our earlier ACK was lost.
1219a0292f23SGarrett Wollman 			 * Our new SYN, when it arrives, will serve as the
1220a0292f23SGarrett Wollman 			 * needed ACK.
1221a0292f23SGarrett Wollman 			 */
1222a0292f23SGarrett Wollman 			if (taop->tao_ccsent != 0)
1223a0292f23SGarrett Wollman 				goto drop;
1224a0292f23SGarrett Wollman 			else
1225a0292f23SGarrett Wollman 				goto dropwithreset;
1226a0292f23SGarrett Wollman 		}
1227fb59c426SYoshinobu Inoue 		if (thflags & TH_RST) {
1228fb59c426SYoshinobu Inoue 			if (thflags & TH_ACK)
1229df8bae1dSRodney W. Grimes 				tp = tcp_drop(tp, ECONNREFUSED);
1230df8bae1dSRodney W. Grimes 			goto drop;
1231df8bae1dSRodney W. Grimes 		}
1232fb59c426SYoshinobu Inoue 		if ((thflags & TH_SYN) == 0)
1233df8bae1dSRodney W. Grimes 			goto drop;
1234fb59c426SYoshinobu Inoue 		tp->snd_wnd = th->th_win;	/* initial send window */
1235a0292f23SGarrett Wollman 		tp->cc_recv = to.to_cc;		/* foreign CC */
1236a0292f23SGarrett Wollman 
1237fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
1238df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
1239fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1240a0292f23SGarrett Wollman 			/*
1241a0292f23SGarrett Wollman 			 * Our SYN was acked.  If segment contains CC.ECHO
1242a0292f23SGarrett Wollman 			 * option, check it to make sure this segment really
1243a0292f23SGarrett Wollman 			 * matches our SYN.  If not, just drop it as old
1244a0292f23SGarrett Wollman 			 * duplicate, but send an RST if we're still playing
1245026650e5SBill Fenner 			 * by the old rules.  If no CC.ECHO option, make sure
1246026650e5SBill Fenner 			 * we don't get fooled into using T/TCP.
1247a0292f23SGarrett Wollman 			 */
1248026650e5SBill Fenner 			if (to.to_flag & TOF_CCECHO) {
1249dfd5dee1SPeter Wemm 				if (tp->cc_send != to.to_ccecho) {
1250a0292f23SGarrett Wollman 					if (taop->tao_ccsent != 0)
1251a0292f23SGarrett Wollman 						goto drop;
1252a0292f23SGarrett Wollman 					else
1253a0292f23SGarrett Wollman 						goto dropwithreset;
1254dfd5dee1SPeter Wemm 				}
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 			 */
1275fb59c426SYoshinobu Inoue 			if (tcp_delack_enabled && 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 &&
13859b8b58e0SJonathan Lemon 					(ticks - tp->t_starttime) > tcp_msl)
1386a0292f23SGarrett Wollman 				goto dropwithreset;
1387a0292f23SGarrett Wollman 			if (CC_GT(to.to_cc, tp->cc_recv)) {
1388a0292f23SGarrett Wollman 				tp = tcp_close(tp);
1389a0292f23SGarrett Wollman 				goto findpcb;
1390a0292f23SGarrett Wollman 			}
1391a0292f23SGarrett Wollman 			else
1392a0292f23SGarrett Wollman 				goto drop;
1393a0292f23SGarrett Wollman 		}
1394a0292f23SGarrett Wollman  		break;  /* continue normal processing */
1395df8bae1dSRodney W. Grimes 	}
1396df8bae1dSRodney W. Grimes 
1397df8bae1dSRodney W. Grimes 	/*
1398df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
139980ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
140080ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
140180ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
140280ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
140380ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
140480ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
140580ab7c0eSGarrett Wollman 	 * killing a connection).
140680ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
1407a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
1408df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
1409df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
1410df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
1411df8bae1dSRodney W. Grimes 	 *
141280ab7c0eSGarrett Wollman 	 *
141380ab7c0eSGarrett Wollman 	 * If the RST bit is set, check the sequence number to see
141480ab7c0eSGarrett Wollman 	 * if this is a valid reset segment.
141580ab7c0eSGarrett Wollman 	 * RFC 793 page 37:
141680ab7c0eSGarrett Wollman 	 *   In all states except SYN-SENT, all reset (RST) segments
141780ab7c0eSGarrett Wollman 	 *   are validated by checking their SEQ-fields.  A reset is
141880ab7c0eSGarrett Wollman 	 *   valid if its sequence number is in the window.
141980ab7c0eSGarrett Wollman 	 * Note: this does not take into account delayed ACKs, so
142080ab7c0eSGarrett Wollman 	 *   we should test against last_ack_sent instead of rcv_nxt.
14211a244a61SJonathan Lemon 	 *   The sequence number in the reset segment is normally an
14221a244a61SJonathan Lemon 	 *   echo of our outgoing acknowlegement numbers, but some hosts
14231a244a61SJonathan Lemon 	 *   send a reset with the sequence number at the rightmost edge
14241a244a61SJonathan Lemon 	 *   of our receive window, and we have to handle this case.
142580ab7c0eSGarrett Wollman 	 * If we have multiple segments in flight, the intial reset
142680ab7c0eSGarrett Wollman 	 * segment sequence numbers will be to the left of last_ack_sent,
142780ab7c0eSGarrett Wollman 	 * but they will eventually catch up.
142880ab7c0eSGarrett Wollman 	 * In any case, it never made sense to trim reset segments to
142980ab7c0eSGarrett Wollman 	 * fit the receive window since RFC 1122 says:
143080ab7c0eSGarrett Wollman 	 *   4.2.2.12  RST Segment: RFC-793 Section 3.4
143180ab7c0eSGarrett Wollman 	 *
143280ab7c0eSGarrett Wollman 	 *    A TCP SHOULD allow a received RST segment to include data.
143380ab7c0eSGarrett Wollman 	 *
143480ab7c0eSGarrett Wollman 	 *    DISCUSSION
143580ab7c0eSGarrett Wollman 	 *         It has been suggested that a RST segment could contain
143680ab7c0eSGarrett Wollman 	 *         ASCII text that encoded and explained the cause of the
143780ab7c0eSGarrett Wollman 	 *         RST.  No standard has yet been established for such
143880ab7c0eSGarrett Wollman 	 *         data.
143980ab7c0eSGarrett Wollman 	 *
144080ab7c0eSGarrett Wollman 	 * If the reset segment passes the sequence number test examine
144180ab7c0eSGarrett Wollman 	 * the state:
144280ab7c0eSGarrett Wollman 	 *    SYN_RECEIVED STATE:
144380ab7c0eSGarrett Wollman 	 *	If passive open, return to LISTEN state.
144480ab7c0eSGarrett Wollman 	 *	If active open, inform user that connection was refused.
144580ab7c0eSGarrett Wollman 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
144680ab7c0eSGarrett Wollman 	 *	Inform user that connection was reset, and close tcb.
1447e9bd3a37SJonathan M. Bresler 	 *    CLOSING, LAST_ACK STATES:
144880ab7c0eSGarrett Wollman 	 *	Close the tcb.
1449e9bd3a37SJonathan M. Bresler 	 *    TIME_WAIT STATE:
145080ab7c0eSGarrett Wollman 	 *	Drop the segment - see Stevens, vol. 2, p. 964 and
145180ab7c0eSGarrett Wollman 	 *      RFC 1337.
145280ab7c0eSGarrett Wollman 	 */
1453fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
1454fb59c426SYoshinobu Inoue 		if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
1455fb59c426SYoshinobu Inoue 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
145680ab7c0eSGarrett Wollman 			switch (tp->t_state) {
145780ab7c0eSGarrett Wollman 
145880ab7c0eSGarrett Wollman 			case TCPS_SYN_RECEIVED:
145980ab7c0eSGarrett Wollman 				so->so_error = ECONNREFUSED;
146080ab7c0eSGarrett Wollman 				goto close;
146180ab7c0eSGarrett Wollman 
146280ab7c0eSGarrett Wollman 			case TCPS_ESTABLISHED:
146380ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_1:
146480ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_2:
146580ab7c0eSGarrett Wollman 			case TCPS_CLOSE_WAIT:
146680ab7c0eSGarrett Wollman 				so->so_error = ECONNRESET;
146780ab7c0eSGarrett Wollman 			close:
146880ab7c0eSGarrett Wollman 				tp->t_state = TCPS_CLOSED;
146980ab7c0eSGarrett Wollman 				tcpstat.tcps_drops++;
147080ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
147180ab7c0eSGarrett Wollman 				break;
147280ab7c0eSGarrett Wollman 
147380ab7c0eSGarrett Wollman 			case TCPS_CLOSING:
147480ab7c0eSGarrett Wollman 			case TCPS_LAST_ACK:
147580ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
147680ab7c0eSGarrett Wollman 				break;
147780ab7c0eSGarrett Wollman 
147880ab7c0eSGarrett Wollman 			case TCPS_TIME_WAIT:
147980ab7c0eSGarrett Wollman 				break;
148080ab7c0eSGarrett Wollman 			}
148180ab7c0eSGarrett Wollman 		}
148280ab7c0eSGarrett Wollman 		goto drop;
148380ab7c0eSGarrett Wollman 	}
148480ab7c0eSGarrett Wollman 
148580ab7c0eSGarrett Wollman 	/*
1486df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
1487df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
1488df8bae1dSRodney W. Grimes 	 */
148980ab7c0eSGarrett Wollman 	if ((to.to_flag & TOF_TS) != 0 && tp->ts_recent &&
149080ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
1491df8bae1dSRodney W. Grimes 
1492df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
14939b8b58e0SJonathan Lemon 		if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) {
1494df8bae1dSRodney W. Grimes 			/*
1495df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
1496df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
1497df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
1498df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
1499df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
1500df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
1501df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
1502df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
1503df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
1504df8bae1dSRodney W. Grimes 			 */
1505df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
1506df8bae1dSRodney W. Grimes 		} else {
1507df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvduppack++;
1508fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvdupbyte += tlen;
1509df8bae1dSRodney W. Grimes 			tcpstat.tcps_pawsdrop++;
1510df8bae1dSRodney W. Grimes 			goto dropafterack;
1511df8bae1dSRodney W. Grimes 		}
1512df8bae1dSRodney W. Grimes 	}
1513df8bae1dSRodney W. Grimes 
1514a0292f23SGarrett Wollman 	/*
1515a0292f23SGarrett Wollman 	 * T/TCP mechanism
1516a0292f23SGarrett Wollman 	 *   If T/TCP was negotiated and the segment doesn't have CC,
1517dc733423SDag-Erling Smørgrav 	 *   or if its CC is wrong then drop the segment.
1518a0292f23SGarrett Wollman 	 *   RST segments do not have to comply with this.
1519a0292f23SGarrett Wollman 	 */
1520a0292f23SGarrett Wollman 	if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) &&
152180ab7c0eSGarrett Wollman 	    ((to.to_flag & TOF_CC) == 0 || tp->cc_recv != to.to_cc))
1522a0292f23SGarrett Wollman  		goto dropafterack;
1523a0292f23SGarrett Wollman 
152480ab7c0eSGarrett Wollman 	/*
152580ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
152680ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
152780ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
152880ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
152980ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
153080ab7c0eSGarrett Wollman 	 */
1531fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs))
153280ab7c0eSGarrett Wollman 		goto dropwithreset;
153380ab7c0eSGarrett Wollman 
1534fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
1535df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1536fb59c426SYoshinobu Inoue 		if (thflags & TH_SYN) {
1537fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
1538fb59c426SYoshinobu Inoue 			th->th_seq++;
1539fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
1540fb59c426SYoshinobu Inoue 				th->th_urp--;
1541df8bae1dSRodney W. Grimes 			else
1542fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
1543df8bae1dSRodney W. Grimes 			todrop--;
1544df8bae1dSRodney W. Grimes 		}
1545df8bae1dSRodney W. Grimes 		/*
1546dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
1547df8bae1dSRodney W. Grimes 		 */
1548fb59c426SYoshinobu Inoue 		if (todrop > tlen
1549fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
1550dac20301SGarrett Wollman 			/*
1551dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
1552dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
1553dac20301SGarrett Wollman 			 * of sequence; drop it.
1554dac20301SGarrett Wollman 			 */
1555fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
1556dac20301SGarrett Wollman 
1557df8bae1dSRodney W. Grimes 			/*
1558dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
1559dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
1560df8bae1dSRodney W. Grimes 			 */
1561dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
1562fb59c426SYoshinobu Inoue 			todrop = tlen;
1563dac20301SGarrett Wollman 			tcpstat.tcps_rcvduppack++;
1564dac20301SGarrett Wollman 			tcpstat.tcps_rcvdupbyte += todrop;
1565df8bae1dSRodney W. Grimes 		} else {
1566df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartduppack++;
1567df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartdupbyte += todrop;
1568df8bae1dSRodney W. Grimes 		}
1569fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
1570fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
1571fb59c426SYoshinobu Inoue 		tlen -= todrop;
1572fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
1573fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
1574df8bae1dSRodney W. Grimes 		else {
1575fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
1576fb59c426SYoshinobu Inoue 			th->th_urp = 0;
1577df8bae1dSRodney W. Grimes 		}
1578df8bae1dSRodney W. Grimes 	}
1579df8bae1dSRodney W. Grimes 
1580df8bae1dSRodney W. Grimes 	/*
1581df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
1582df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
1583df8bae1dSRodney W. Grimes 	 */
1584df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
1585fb59c426SYoshinobu Inoue 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
1586df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1587df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvafterclose++;
1588df8bae1dSRodney W. Grimes 		goto dropwithreset;
1589df8bae1dSRodney W. Grimes 	}
1590df8bae1dSRodney W. Grimes 
1591df8bae1dSRodney W. Grimes 	/*
1592df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
1593df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
1594df8bae1dSRodney W. Grimes 	 */
1595fb59c426SYoshinobu Inoue 	todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd);
1596df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1597df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvpackafterwin++;
1598fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
1599fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbyteafterwin += tlen;
1600df8bae1dSRodney W. Grimes 			/*
1601df8bae1dSRodney W. Grimes 			 * If a new connection request is received
1602df8bae1dSRodney W. Grimes 			 * while in TIME_WAIT, drop the old connection
1603df8bae1dSRodney W. Grimes 			 * and start over if the sequence numbers
1604df8bae1dSRodney W. Grimes 			 * are above the previous ones.
1605df8bae1dSRodney W. Grimes 			 */
1606fb59c426SYoshinobu Inoue 			if (thflags & TH_SYN &&
1607df8bae1dSRodney W. Grimes 			    tp->t_state == TCPS_TIME_WAIT &&
1608fb59c426SYoshinobu Inoue 			    SEQ_GT(th->th_seq, tp->rcv_nxt)) {
160951b7b337SBill Fenner 				iss = tp->snd_nxt + TCP_ISSINCR;
1610df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
1611df8bae1dSRodney W. Grimes 				goto findpcb;
1612df8bae1dSRodney W. Grimes 			}
1613df8bae1dSRodney W. Grimes 			/*
1614df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
1615df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
1616df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
1617df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
1618df8bae1dSRodney W. Grimes 			 * and ack.
1619df8bae1dSRodney W. Grimes 			 */
1620fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
1621df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
1622df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvwinprobe++;
1623df8bae1dSRodney W. Grimes 			} else
1624df8bae1dSRodney W. Grimes 				goto dropafterack;
1625df8bae1dSRodney W. Grimes 		} else
1626df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
1627df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
1628fb59c426SYoshinobu Inoue 		tlen -= todrop;
1629fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
1630df8bae1dSRodney W. Grimes 	}
1631df8bae1dSRodney W. Grimes 
1632df8bae1dSRodney W. Grimes 	/*
1633df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
1634df8bae1dSRodney W. Grimes 	 * record its timestamp.
1635a0292f23SGarrett Wollman 	 * NOTE that the test is modified according to the latest
1636a0292f23SGarrett Wollman 	 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1637df8bae1dSRodney W. Grimes 	 */
1638a0292f23SGarrett Wollman 	if ((to.to_flag & TOF_TS) != 0 &&
1639fb59c426SYoshinobu Inoue 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
16409b8b58e0SJonathan Lemon 		tp->ts_recent_age = ticks;
1641a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
1642df8bae1dSRodney W. Grimes 	}
1643df8bae1dSRodney W. Grimes 
1644df8bae1dSRodney W. Grimes 	/*
1645df8bae1dSRodney W. Grimes 	 * If a SYN is in the window, then this is an
1646df8bae1dSRodney W. Grimes 	 * error and we send an RST and drop the connection.
1647df8bae1dSRodney W. Grimes 	 */
1648fb59c426SYoshinobu Inoue 	if (thflags & TH_SYN) {
1649df8bae1dSRodney W. Grimes 		tp = tcp_drop(tp, ECONNRESET);
1650df8bae1dSRodney W. Grimes 		goto dropwithreset;
1651df8bae1dSRodney W. Grimes 	}
1652df8bae1dSRodney W. Grimes 
1653a0292f23SGarrett Wollman 	/*
1654a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
1655a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
1656a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
1657a0292f23SGarrett Wollman 	 */
1658fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
1659a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
1660a0292f23SGarrett Wollman 		    (tp->t_flags & TF_NEEDSYN))
1661a0292f23SGarrett Wollman 			goto step6;
1662a0292f23SGarrett Wollman 		else
1663a0292f23SGarrett Wollman 			goto drop;
1664a0292f23SGarrett Wollman 	}
1665df8bae1dSRodney W. Grimes 
1666df8bae1dSRodney W. Grimes 	/*
1667df8bae1dSRodney W. Grimes 	 * Ack processing.
1668df8bae1dSRodney W. Grimes 	 */
1669df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1670df8bae1dSRodney W. Grimes 
1671df8bae1dSRodney W. Grimes 	/*
1672764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
1673764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
1674764d8cefSBill Fenner 	 * The ACK was checked above.
1675df8bae1dSRodney W. Grimes 	 */
1676df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1677a0292f23SGarrett Wollman 
1678df8bae1dSRodney W. Grimes 		tcpstat.tcps_connects++;
1679df8bae1dSRodney W. Grimes 		soisconnected(so);
1680df8bae1dSRodney W. Grimes 		/* Do window scaling? */
1681df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1682df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1683df8bae1dSRodney W. Grimes 			tp->snd_scale = tp->requested_s_scale;
1684df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
1685df8bae1dSRodney W. Grimes 		}
1686a0292f23SGarrett Wollman 		/*
1687a0292f23SGarrett Wollman 		 * Upon successful completion of 3-way handshake,
1688a0292f23SGarrett Wollman 		 * update cache.CC if it was undefined, pass any queued
1689a0292f23SGarrett Wollman 		 * data to the user, and advance state appropriately.
1690a0292f23SGarrett Wollman 		 */
1691a0292f23SGarrett Wollman 		if ((taop = tcp_gettaocache(inp)) != NULL &&
1692a0292f23SGarrett Wollman 		    taop->tao_cc == 0)
1693a0292f23SGarrett Wollman 			taop->tao_cc = tp->cc_recv;
1694a0292f23SGarrett Wollman 
1695a0292f23SGarrett Wollman 		/*
1696a0292f23SGarrett Wollman 		 * Make transitions:
1697a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
1698a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
1699a0292f23SGarrett Wollman 		 */
17009b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
1701a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDFIN) {
1702a0292f23SGarrett Wollman 			tp->t_state = TCPS_FIN_WAIT_1;
1703a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDFIN;
17047ff19458SPaul Traina 		} else {
1705a0292f23SGarrett Wollman 			tp->t_state = TCPS_ESTABLISHED;
17069b8b58e0SJonathan Lemon 			callout_reset(tp->tt_keep, tcp_keepidle,
17079b8b58e0SJonathan Lemon 				      tcp_timer_keep, tp);
17087ff19458SPaul Traina 		}
1709a0292f23SGarrett Wollman 		/*
1710a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
1711a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
1712a0292f23SGarrett Wollman 		 */
1713fb59c426SYoshinobu Inoue 		if (tlen == 0 && (thflags & TH_FIN) == 0)
1714fb59c426SYoshinobu Inoue 			(void) tcp_reass(tp, (struct tcphdr *)0, 0,
1715a0292f23SGarrett Wollman 			    (struct mbuf *)0);
1716fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
1717df8bae1dSRodney W. Grimes 		/* fall into ... */
1718df8bae1dSRodney W. Grimes 
1719df8bae1dSRodney W. Grimes 	/*
1720df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1721df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
1722fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
1723fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
1724df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
1725df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
1726df8bae1dSRodney W. Grimes 	 */
1727df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1728df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
1729df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
1730df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1731df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
1732df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
1733df8bae1dSRodney W. Grimes 	case TCPS_TIME_WAIT:
1734df8bae1dSRodney W. Grimes 
1735fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
1736fb59c426SYoshinobu Inoue 			if (tlen == 0 && tiwin == tp->snd_wnd) {
1737df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvdupack++;
1738df8bae1dSRodney W. Grimes 				/*
1739df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
1740df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
1741df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
1742df8bae1dSRodney W. Grimes 				 * change), the ack is the biggest we've
1743df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
1744df8bae1dSRodney W. Grimes 				 * threshhold of them, assume a packet
1745df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
1746df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
1747df8bae1dSRodney W. Grimes 				 * window so we send only this one
1748df8bae1dSRodney W. Grimes 				 * packet.
1749df8bae1dSRodney W. Grimes 				 *
1750df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
1751df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
1752df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
1753df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
1754df8bae1dSRodney W. Grimes 				 * the new ssthresh).
1755df8bae1dSRodney W. Grimes 				 *
1756df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
1757df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
1758df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
1759df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
1760df8bae1dSRodney W. Grimes 				 * network.
1761df8bae1dSRodney W. Grimes 				 */
17629b8b58e0SJonathan Lemon 				if (!callout_active(tp->tt_rexmt) ||
1763fb59c426SYoshinobu Inoue 				    th->th_ack != tp->snd_una)
1764df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
1765df8bae1dSRodney W. Grimes 				else if (++tp->t_dupacks == tcprexmtthresh) {
1766df8bae1dSRodney W. Grimes 					tcp_seq onxt = tp->snd_nxt;
1767df8bae1dSRodney W. Grimes 					u_int win =
1768df8bae1dSRodney W. Grimes 					    min(tp->snd_wnd, tp->snd_cwnd) / 2 /
1769df8bae1dSRodney W. Grimes 						tp->t_maxseg;
1770df8bae1dSRodney W. Grimes 
1771df8bae1dSRodney W. Grimes 					if (win < 2)
1772df8bae1dSRodney W. Grimes 						win = 2;
1773df8bae1dSRodney W. Grimes 					tp->snd_ssthresh = win * tp->t_maxseg;
17749b8b58e0SJonathan Lemon 					callout_stop(tp->tt_rexmt);
17759b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
1776fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
1777df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->t_maxseg;
1778df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1779df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
1780df8bae1dSRodney W. Grimes 					       tp->t_maxseg * tp->t_dupacks;
1781df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
1782df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
1783df8bae1dSRodney W. Grimes 					goto drop;
1784df8bae1dSRodney W. Grimes 				} else if (tp->t_dupacks > tcprexmtthresh) {
1785df8bae1dSRodney W. Grimes 					tp->snd_cwnd += tp->t_maxseg;
1786df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1787df8bae1dSRodney W. Grimes 					goto drop;
1788df8bae1dSRodney W. Grimes 				}
1789df8bae1dSRodney W. Grimes 			} else
1790df8bae1dSRodney W. Grimes 				tp->t_dupacks = 0;
1791df8bae1dSRodney W. Grimes 			break;
1792df8bae1dSRodney W. Grimes 		}
1793df8bae1dSRodney W. Grimes 		/*
1794df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
1795df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
1796df8bae1dSRodney W. Grimes 		 */
1797233e8c18SGarrett Wollman 		if (tp->t_dupacks >= tcprexmtthresh &&
1798df8bae1dSRodney W. Grimes 		    tp->snd_cwnd > tp->snd_ssthresh)
1799df8bae1dSRodney W. Grimes 			tp->snd_cwnd = tp->snd_ssthresh;
1800df8bae1dSRodney W. Grimes 		tp->t_dupacks = 0;
1801fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
1802df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvacktoomuch++;
1803df8bae1dSRodney W. Grimes 			goto dropafterack;
1804df8bae1dSRodney W. Grimes 		}
1805a0292f23SGarrett Wollman 		/*
1806a0292f23SGarrett Wollman 		 *  If we reach this point, ACK is not a duplicate,
1807a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
1808a0292f23SGarrett Wollman 		 */
1809a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
1810a0292f23SGarrett Wollman 			/*
1811a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
1812a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
181307e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
181407e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
181507e43e10SAndras Olah 			 * we can do window scaling.
1816a0292f23SGarrett Wollman 			 */
1817a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
1818a0292f23SGarrett Wollman 			tp->snd_una++;
181907e43e10SAndras Olah 			/* Do window scaling? */
182007e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
182107e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
182207e43e10SAndras Olah 				tp->snd_scale = tp->requested_s_scale;
182307e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
182407e43e10SAndras Olah 			}
1825a0292f23SGarrett Wollman 		}
1826a0292f23SGarrett Wollman 
1827a0292f23SGarrett Wollman process_ACK:
1828fb59c426SYoshinobu Inoue 		acked = th->th_ack - tp->snd_una;
1829df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackpack++;
1830df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackbyte += acked;
1831df8bae1dSRodney W. Grimes 
1832df8bae1dSRodney W. Grimes 		/*
18339b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
18349b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
18359b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
18369b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
18379b8b58e0SJonathan Lemon 		 * we left off.
18389b8b58e0SJonathan Lemon 		 */
18399b8b58e0SJonathan Lemon 		if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) {
18409b8b58e0SJonathan Lemon 			tp->snd_cwnd = tp->snd_cwnd_prev;
18419b8b58e0SJonathan Lemon 			tp->snd_ssthresh = tp->snd_ssthresh_prev;
18429b8b58e0SJonathan Lemon 			tp->snd_nxt = tp->snd_max;
18439b8b58e0SJonathan Lemon 			tp->t_badrxtwin = 0;	/* XXX probably not required */
18449b8b58e0SJonathan Lemon 		}
18459b8b58e0SJonathan Lemon 
18469b8b58e0SJonathan Lemon 		/*
1847df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
1848df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
1849df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
1850df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
1851df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
1852df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
1853df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
1854df8bae1dSRodney W. Grimes 		 */
1855a0292f23SGarrett Wollman 		if (to.to_flag & TOF_TS)
18569b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - to.to_tsecr + 1);
1857fb59c426SYoshinobu Inoue 		else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq))
18589b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
1859df8bae1dSRodney W. Grimes 
1860df8bae1dSRodney W. Grimes 		/*
1861df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
1862df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
1863df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
1864df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
1865df8bae1dSRodney W. Grimes 		 */
1866fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
18679b8b58e0SJonathan Lemon 			callout_stop(tp->tt_rexmt);
1868df8bae1dSRodney W. Grimes 			needoutput = 1;
18699b8b58e0SJonathan Lemon 		} else if (!callout_active(tp->tt_persist))
18709b8b58e0SJonathan Lemon 			callout_reset(tp->tt_rexmt, tp->t_rxtcur,
18719b8b58e0SJonathan Lemon 				      tcp_timer_rexmt, tp);
1872a0292f23SGarrett Wollman 
1873a0292f23SGarrett Wollman 		/*
1874a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
1875a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
1876a0292f23SGarrett Wollman 		 */
1877a0292f23SGarrett Wollman 		if (acked == 0)
1878a0292f23SGarrett Wollman 			goto step6;
1879a0292f23SGarrett Wollman 
1880df8bae1dSRodney W. Grimes 		/*
1881df8bae1dSRodney W. Grimes 		 * When new data is acked, open the congestion window.
1882df8bae1dSRodney W. Grimes 		 * If the window gives us less than ssthresh packets
1883df8bae1dSRodney W. Grimes 		 * in flight, open exponentially (maxseg per packet).
1884df8bae1dSRodney W. Grimes 		 * Otherwise open linearly: maxseg per window
188510be5648SGarrett Wollman 		 * (maxseg^2 / cwnd per packet).
1886df8bae1dSRodney W. Grimes 		 */
1887df8bae1dSRodney W. Grimes 		{
1888df8bae1dSRodney W. Grimes 		register u_int cw = tp->snd_cwnd;
1889df8bae1dSRodney W. Grimes 		register u_int incr = tp->t_maxseg;
1890df8bae1dSRodney W. Grimes 
1891df8bae1dSRodney W. Grimes 		if (cw > tp->snd_ssthresh)
189210be5648SGarrett Wollman 			incr = incr * incr / cw;
1893df8bae1dSRodney W. Grimes 		tp->snd_cwnd = min(cw + incr, TCP_MAXWIN << tp->snd_scale);
1894df8bae1dSRodney W. Grimes 		}
1895df8bae1dSRodney W. Grimes 		if (acked > so->so_snd.sb_cc) {
1896df8bae1dSRodney W. Grimes 			tp->snd_wnd -= so->so_snd.sb_cc;
1897df8bae1dSRodney W. Grimes 			sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
1898df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
1899df8bae1dSRodney W. Grimes 		} else {
1900df8bae1dSRodney W. Grimes 			sbdrop(&so->so_snd, acked);
1901df8bae1dSRodney W. Grimes 			tp->snd_wnd -= acked;
1902df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
1903df8bae1dSRodney W. Grimes 		}
1904df8bae1dSRodney W. Grimes 		sowwakeup(so);
1905fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
1906df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1907df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
1908df8bae1dSRodney W. Grimes 
1909df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
1910df8bae1dSRodney W. Grimes 
1911df8bae1dSRodney W. Grimes 		/*
1912df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
1913df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
1914df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
1915df8bae1dSRodney W. Grimes 		 */
1916df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
1917df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
1918df8bae1dSRodney W. Grimes 				/*
1919df8bae1dSRodney W. Grimes 				 * If we can't receive any more
1920df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
1921df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
1922df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
1923df8bae1dSRodney W. Grimes 				 * we'll hang forever.
1924df8bae1dSRodney W. Grimes 				 */
1925df8bae1dSRodney W. Grimes 				if (so->so_state & SS_CANTRCVMORE) {
1926df8bae1dSRodney W. Grimes 					soisdisconnected(so);
19279b8b58e0SJonathan Lemon 					callout_reset(tp->tt_2msl, tcp_maxidle,
19289b8b58e0SJonathan Lemon 						      tcp_timer_2msl, tp);
1929df8bae1dSRodney W. Grimes 				}
1930df8bae1dSRodney W. Grimes 				tp->t_state = TCPS_FIN_WAIT_2;
1931df8bae1dSRodney W. Grimes 			}
1932df8bae1dSRodney W. Grimes 			break;
1933df8bae1dSRodney W. Grimes 
1934df8bae1dSRodney W. Grimes 	 	/*
1935df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
1936df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
1937df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
1938df8bae1dSRodney W. Grimes 		 * the segment.
1939df8bae1dSRodney W. Grimes 		 */
1940df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
1941df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
1942df8bae1dSRodney W. Grimes 				tp->t_state = TCPS_TIME_WAIT;
1943df8bae1dSRodney W. Grimes 				tcp_canceltimers(tp);
1944a0292f23SGarrett Wollman 				/* Shorten TIME_WAIT [RFC-1644, p.28] */
1945a0292f23SGarrett Wollman 				if (tp->cc_recv != 0 &&
19469b8b58e0SJonathan Lemon 				    (ticks - tp->t_starttime) < tcp_msl)
19479b8b58e0SJonathan Lemon 					callout_reset(tp->tt_2msl,
19489b8b58e0SJonathan Lemon 						      tp->t_rxtcur *
19499b8b58e0SJonathan Lemon 						      TCPTV_TWTRUNC,
19509b8b58e0SJonathan Lemon 						      tcp_timer_2msl, tp);
1951a0292f23SGarrett Wollman 				else
19529b8b58e0SJonathan Lemon 					callout_reset(tp->tt_2msl, 2 * tcp_msl,
19539b8b58e0SJonathan Lemon 						      tcp_timer_2msl, tp);
1954df8bae1dSRodney W. Grimes 				soisdisconnected(so);
1955df8bae1dSRodney W. Grimes 			}
1956df8bae1dSRodney W. Grimes 			break;
1957df8bae1dSRodney W. Grimes 
1958df8bae1dSRodney W. Grimes 		/*
1959df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
1960df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
1961df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
1962df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
1963df8bae1dSRodney W. Grimes 		 */
1964df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
1965df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
1966df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
1967df8bae1dSRodney W. Grimes 				goto drop;
1968df8bae1dSRodney W. Grimes 			}
1969df8bae1dSRodney W. Grimes 			break;
1970df8bae1dSRodney W. Grimes 
1971df8bae1dSRodney W. Grimes 		/*
1972df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state the only thing that should arrive
1973df8bae1dSRodney W. Grimes 		 * is a retransmission of the remote FIN.  Acknowledge
1974df8bae1dSRodney W. Grimes 		 * it and restart the finack timer.
1975df8bae1dSRodney W. Grimes 		 */
1976df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
19779b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, 2 * tcp_msl,
19789b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
1979df8bae1dSRodney W. Grimes 			goto dropafterack;
1980df8bae1dSRodney W. Grimes 		}
1981df8bae1dSRodney W. Grimes 	}
1982df8bae1dSRodney W. Grimes 
1983df8bae1dSRodney W. Grimes step6:
1984df8bae1dSRodney W. Grimes 	/*
1985df8bae1dSRodney W. Grimes 	 * Update window information.
1986df8bae1dSRodney W. Grimes 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
1987df8bae1dSRodney W. Grimes 	 */
1988fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) &&
1989fb59c426SYoshinobu Inoue 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
1990fb59c426SYoshinobu Inoue 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
1991fb59c426SYoshinobu Inoue 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
1992df8bae1dSRodney W. Grimes 		/* keep track of pure window updates */
1993fb59c426SYoshinobu Inoue 		if (tlen == 0 &&
1994fb59c426SYoshinobu Inoue 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
1995df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvwinupd++;
1996df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
1997fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq;
1998fb59c426SYoshinobu Inoue 		tp->snd_wl2 = th->th_ack;
1999df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
2000df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
2001df8bae1dSRodney W. Grimes 		needoutput = 1;
2002df8bae1dSRodney W. Grimes 	}
2003df8bae1dSRodney W. Grimes 
2004df8bae1dSRodney W. Grimes 	/*
2005df8bae1dSRodney W. Grimes 	 * Process segments with URG.
2006df8bae1dSRodney W. Grimes 	 */
2007fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
2008df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2009df8bae1dSRodney W. Grimes 		/*
2010df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
2011df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
2012df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
2013df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
2014df8bae1dSRodney W. Grimes 		 */
2015fb59c426SYoshinobu Inoue 		if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
2016fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
2017fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
2018df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
2019df8bae1dSRodney W. Grimes 		}
2020df8bae1dSRodney W. Grimes 		/*
2021df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
2022df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
2023df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2024df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
2025df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
2026df8bae1dSRodney W. Grimes 		 *
2027df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
2028df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
2029df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
2030df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
2031df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
2032df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
2033df8bae1dSRodney W. Grimes 		 */
2034fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2035fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
2036df8bae1dSRodney W. Grimes 			so->so_oobmark = so->so_rcv.sb_cc +
2037df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
2038df8bae1dSRodney W. Grimes 			if (so->so_oobmark == 0)
2039df8bae1dSRodney W. Grimes 				so->so_state |= SS_RCVATMARK;
2040df8bae1dSRodney W. Grimes 			sohasoutofband(so);
2041df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2042df8bae1dSRodney W. Grimes 		}
2043df8bae1dSRodney W. Grimes 		/*
2044df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
2045df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
2046df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
2047df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
2048df8bae1dSRodney W. Grimes 		 */
2049fb59c426SYoshinobu Inoue 		if (th->th_urp <= (u_long)tlen
2050df8bae1dSRodney W. Grimes #ifdef SO_OOBINLINE
2051df8bae1dSRodney W. Grimes 		     && (so->so_options & SO_OOBINLINE) == 0
2052df8bae1dSRodney W. Grimes #endif
2053df8bae1dSRodney W. Grimes 		     )
2054fb59c426SYoshinobu Inoue 			tcp_pulloutofband(so, th, m,
2055fb59c426SYoshinobu Inoue 				drop_hdrlen);	/* hdr drop is delayed */
2056df8bae1dSRodney W. Grimes 	} else
2057df8bae1dSRodney W. Grimes 		/*
2058df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
2059df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
2060df8bae1dSRodney W. Grimes 		 * with the receive window.
2061df8bae1dSRodney W. Grimes 		 */
2062df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2063df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
2064df8bae1dSRodney W. Grimes dodata:							/* XXX */
2065df8bae1dSRodney W. Grimes 
2066df8bae1dSRodney W. Grimes 	/*
2067df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
2068df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
2069df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
2070df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
2071df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
2072df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
2073df8bae1dSRodney W. Grimes 	 */
2074fb59c426SYoshinobu Inoue 	if ((tlen || (thflags&TH_FIN)) &&
2075df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2076fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
2077fb59c426SYoshinobu Inoue 		TCP_REASS(tp, th, &tlen, m, so, thflags);
2078df8bae1dSRodney W. Grimes 		/*
2079df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
2080df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
2081df8bae1dSRodney W. Grimes 		 * buffer size.
2082df8bae1dSRodney W. Grimes 		 */
2083df8bae1dSRodney W. Grimes 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2084df8bae1dSRodney W. Grimes 	} else {
2085df8bae1dSRodney W. Grimes 		m_freem(m);
2086fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
2087df8bae1dSRodney W. Grimes 	}
2088df8bae1dSRodney W. Grimes 
2089df8bae1dSRodney W. Grimes 	/*
2090df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
2091df8bae1dSRodney W. Grimes 	 * that the connection is closing.
2092df8bae1dSRodney W. Grimes 	 */
2093fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
2094df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2095df8bae1dSRodney W. Grimes 			socantrcvmore(so);
2096a0292f23SGarrett Wollman 			/*
2097a0292f23SGarrett Wollman 			 *  If connection is half-synchronized
2098d3eede9dSAndras Olah 			 *  (ie NEEDSYN flag on) then delay ACK,
2099a0292f23SGarrett Wollman 			 *  so it may be piggybacked when SYN is sent.
2100a0292f23SGarrett Wollman 			 *  Otherwise, since we received a FIN then no
2101a0292f23SGarrett Wollman 			 *  more input can be expected, send ACK now.
2102a0292f23SGarrett Wollman 			 */
2103f498eeeeSDavid Greenman 			if (tcp_delack_enabled && (tp->t_flags & TF_NEEDSYN))
21049b8b58e0SJonathan Lemon                                 callout_reset(tp->tt_delack, tcp_delacktime,
21059b8b58e0SJonathan Lemon                                     tcp_timer_delack, tp);
2106a0292f23SGarrett Wollman 			else
2107df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
2108df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
2109df8bae1dSRodney W. Grimes 		}
2110df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2111df8bae1dSRodney W. Grimes 
2112df8bae1dSRodney W. Grimes 	 	/*
2113df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
2114df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
2115df8bae1dSRodney W. Grimes 		 */
2116df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
21179b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
21189b8b58e0SJonathan Lemon 			/*FALLTHROUGH*/
2119df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
2120df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSE_WAIT;
2121df8bae1dSRodney W. Grimes 			break;
2122df8bae1dSRodney W. Grimes 
2123df8bae1dSRodney W. Grimes 	 	/*
2124df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2125df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
2126df8bae1dSRodney W. Grimes 		 */
2127df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2128df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSING;
2129df8bae1dSRodney W. Grimes 			break;
2130df8bae1dSRodney W. Grimes 
2131df8bae1dSRodney W. Grimes 	 	/*
2132df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2133df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
2134df8bae1dSRodney W. Grimes 		 * standard timers.
2135df8bae1dSRodney W. Grimes 		 */
2136df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
2137df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_TIME_WAIT;
2138df8bae1dSRodney W. Grimes 			tcp_canceltimers(tp);
2139a0292f23SGarrett Wollman 			/* Shorten TIME_WAIT [RFC-1644, p.28] */
2140a0292f23SGarrett Wollman 			if (tp->cc_recv != 0 &&
21419b8b58e0SJonathan Lemon 			    (ticks - tp->t_starttime) < tcp_msl) {
21429b8b58e0SJonathan Lemon 				callout_reset(tp->tt_2msl,
21439b8b58e0SJonathan Lemon 					      tp->t_rxtcur * TCPTV_TWTRUNC,
21449b8b58e0SJonathan Lemon 					      tcp_timer_2msl, tp);
2145a0292f23SGarrett Wollman 				/* For transaction client, force ACK now. */
2146a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
2147a0292f23SGarrett Wollman 			}
2148a0292f23SGarrett Wollman 			else
21499b8b58e0SJonathan Lemon 				callout_reset(tp->tt_2msl, 2 * tcp_msl,
21509b8b58e0SJonathan Lemon 					      tcp_timer_2msl, tp);
2151df8bae1dSRodney W. Grimes 			soisdisconnected(so);
2152df8bae1dSRodney W. Grimes 			break;
2153df8bae1dSRodney W. Grimes 
2154df8bae1dSRodney W. Grimes 		/*
2155df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
2156df8bae1dSRodney W. Grimes 		 */
2157df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
21589b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, 2 * tcp_msl,
21599b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
2160df8bae1dSRodney W. Grimes 			break;
2161df8bae1dSRodney W. Grimes 		}
2162df8bae1dSRodney W. Grimes 	}
2163610ee2f9SDavid Greenman #ifdef TCPDEBUG
2164df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG)
2165fb59c426SYoshinobu Inoue 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
2166fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2167610ee2f9SDavid Greenman #endif
2168df8bae1dSRodney W. Grimes 
2169df8bae1dSRodney W. Grimes 	/*
2170df8bae1dSRodney W. Grimes 	 * Return any desired output.
2171df8bae1dSRodney W. Grimes 	 */
2172df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
2173df8bae1dSRodney W. Grimes 		(void) tcp_output(tp);
2174df8bae1dSRodney W. Grimes 	return;
2175df8bae1dSRodney W. Grimes 
2176df8bae1dSRodney W. Grimes dropafterack:
2177df8bae1dSRodney W. Grimes 	/*
2178df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
2179df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
218080ab7c0eSGarrett Wollman 	 *
218180ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
218280ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
218380ab7c0eSGarrett Wollman 	 * RST have been dropped.
218480ab7c0eSGarrett Wollman 	 *
218580ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
218680ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
218780ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
218880ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
218980ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
219080ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
2191df8bae1dSRodney W. Grimes 	 */
2192fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
2193fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
2194fb59c426SYoshinobu Inoue 	     SEQ_GT(th->th_ack, tp->snd_max)) )
219580ab7c0eSGarrett Wollman 		goto dropwithreset;
2196a0292f23SGarrett Wollman #ifdef TCPDEBUG
2197a0292f23SGarrett Wollman 	if (so->so_options & SO_DEBUG)
2198fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2199fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2200a0292f23SGarrett Wollman #endif
2201df8bae1dSRodney W. Grimes 	m_freem(m);
2202df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
2203df8bae1dSRodney W. Grimes 	(void) tcp_output(tp);
2204df8bae1dSRodney W. Grimes 	return;
2205df8bae1dSRodney W. Grimes 
2206df8bae1dSRodney W. Grimes dropwithreset:
2207e46cd3d4SDag-Erling Smørgrav #ifdef TCP_RESTRICT_RST
2208e46cd3d4SDag-Erling Smørgrav 	if (restrict_rst)
2209e46cd3d4SDag-Erling Smørgrav 		goto drop;
2210e46cd3d4SDag-Erling Smørgrav #endif
2211df8bae1dSRodney W. Grimes 	/*
2212df8bae1dSRodney W. Grimes 	 * Generate a RST, dropping incoming segment.
2213df8bae1dSRodney W. Grimes 	 * Make ACK acceptable to originator of segment.
2214df8bae1dSRodney W. Grimes 	 * Don't bother to respond if destination was broadcast/multicast.
2215df8bae1dSRodney W. Grimes 	 */
2216fb59c426SYoshinobu Inoue 	if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
2217df8bae1dSRodney W. Grimes 		goto drop;
2218fb59c426SYoshinobu Inoue #ifdef INET6
2219fb59c426SYoshinobu Inoue 	if (isipv6) {
2220fb59c426SYoshinobu Inoue 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
2221fb59c426SYoshinobu Inoue 			goto drop;
2222fb59c426SYoshinobu Inoue 	} else
2223fb59c426SYoshinobu Inoue #endif /* INET6 */
2224fb59c426SYoshinobu Inoue 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)))
2225fb59c426SYoshinobu Inoue 		goto drop;
2226fb59c426SYoshinobu Inoue 	/* IPv6 anycast check is done at tcp6_input() */
2227a0292f23SGarrett Wollman #ifdef TCPDEBUG
2228a0292f23SGarrett Wollman 	if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2229fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2230fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2231a0292f23SGarrett Wollman #endif
2232fb59c426SYoshinobu Inoue 	if (thflags & TH_ACK)
2233fb59c426SYoshinobu Inoue 		/* mtod() below is safe as long as hdr dropping is delayed */
2234fb59c426SYoshinobu Inoue 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack,
2235fb59c426SYoshinobu Inoue 			    TH_RST);
2236df8bae1dSRodney W. Grimes 	else {
2237fb59c426SYoshinobu Inoue 		if (thflags & TH_SYN)
2238fb59c426SYoshinobu Inoue 			tlen++;
2239fb59c426SYoshinobu Inoue 		/* mtod() below is safe as long as hdr dropping is delayed */
2240fb59c426SYoshinobu Inoue 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
2241fb59c426SYoshinobu Inoue 			    (tcp_seq)0, TH_RST|TH_ACK);
2242df8bae1dSRodney W. Grimes 	}
2243df8bae1dSRodney W. Grimes 	/* destroy temporarily created socket */
2244df8bae1dSRodney W. Grimes 	if (dropsocket)
2245df8bae1dSRodney W. Grimes 		(void) soabort(so);
2246df8bae1dSRodney W. Grimes 	return;
2247df8bae1dSRodney W. Grimes 
2248df8bae1dSRodney W. Grimes drop:
2249df8bae1dSRodney W. Grimes 	/*
2250df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
2251df8bae1dSRodney W. Grimes 	 */
2252610ee2f9SDavid Greenman #ifdef TCPDEBUG
2253a0292f23SGarrett Wollman 	if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2254fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2255fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2256a0292f23SGarrett Wollman #endif
2257df8bae1dSRodney W. Grimes 	m_freem(m);
2258df8bae1dSRodney W. Grimes 	/* destroy temporarily created socket */
2259df8bae1dSRodney W. Grimes 	if (dropsocket)
2260df8bae1dSRodney W. Grimes 		(void) soabort(so);
2261df8bae1dSRodney W. Grimes 	return;
2262df8bae1dSRodney W. Grimes }
2263df8bae1dSRodney W. Grimes 
22640312fbe9SPoul-Henning Kamp static void
2265fb59c426SYoshinobu Inoue tcp_dooptions(tp, cp, cnt, th, to)
2266df8bae1dSRodney W. Grimes 	struct tcpcb *tp;
2267df8bae1dSRodney W. Grimes 	u_char *cp;
2268df8bae1dSRodney W. Grimes 	int cnt;
2269fb59c426SYoshinobu Inoue 	struct tcphdr *th;
2270a0292f23SGarrett Wollman 	struct tcpopt *to;
2271df8bae1dSRodney W. Grimes {
2272a0292f23SGarrett Wollman 	u_short mss = 0;
2273df8bae1dSRodney W. Grimes 	int opt, optlen;
2274df8bae1dSRodney W. Grimes 
2275df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
2276df8bae1dSRodney W. Grimes 		opt = cp[0];
2277df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
2278df8bae1dSRodney W. Grimes 			break;
2279df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
2280df8bae1dSRodney W. Grimes 			optlen = 1;
2281df8bae1dSRodney W. Grimes 		else {
2282df8bae1dSRodney W. Grimes 			optlen = cp[1];
2283df8bae1dSRodney W. Grimes 			if (optlen <= 0)
2284df8bae1dSRodney W. Grimes 				break;
2285df8bae1dSRodney W. Grimes 		}
2286df8bae1dSRodney W. Grimes 		switch (opt) {
2287df8bae1dSRodney W. Grimes 
2288df8bae1dSRodney W. Grimes 		default:
2289df8bae1dSRodney W. Grimes 			continue;
2290df8bae1dSRodney W. Grimes 
2291df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
2292df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
2293df8bae1dSRodney W. Grimes 				continue;
2294fb59c426SYoshinobu Inoue 			if (!(th->th_flags & TH_SYN))
2295df8bae1dSRodney W. Grimes 				continue;
2296df8bae1dSRodney W. Grimes 			bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
2297df8bae1dSRodney W. Grimes 			NTOHS(mss);
2298df8bae1dSRodney W. Grimes 			break;
2299df8bae1dSRodney W. Grimes 
2300df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
2301df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
2302df8bae1dSRodney W. Grimes 				continue;
2303fb59c426SYoshinobu Inoue 			if (!(th->th_flags & TH_SYN))
2304df8bae1dSRodney W. Grimes 				continue;
2305df8bae1dSRodney W. Grimes 			tp->t_flags |= TF_RCVD_SCALE;
2306df8bae1dSRodney W. Grimes 			tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
2307df8bae1dSRodney W. Grimes 			break;
2308df8bae1dSRodney W. Grimes 
2309df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
2310df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
2311df8bae1dSRodney W. Grimes 				continue;
2312a0292f23SGarrett Wollman 			to->to_flag |= TOF_TS;
2313a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2314a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
2315a0292f23SGarrett Wollman 			NTOHL(to->to_tsval);
2316a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
2317a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
2318a0292f23SGarrett Wollman 			NTOHL(to->to_tsecr);
2319df8bae1dSRodney W. Grimes 
2320df8bae1dSRodney W. Grimes 			/*
2321df8bae1dSRodney W. Grimes 			 * A timestamp received in a SYN makes
2322df8bae1dSRodney W. Grimes 			 * it ok to send timestamp requests and replies.
2323df8bae1dSRodney W. Grimes 			 */
2324fb59c426SYoshinobu Inoue 			if (th->th_flags & TH_SYN) {
2325df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_RCVD_TSTMP;
2326a0292f23SGarrett Wollman 				tp->ts_recent = to->to_tsval;
23279b8b58e0SJonathan Lemon 				tp->ts_recent_age = ticks;
2328df8bae1dSRodney W. Grimes 			}
2329df8bae1dSRodney W. Grimes 			break;
2330a0292f23SGarrett Wollman 		case TCPOPT_CC:
2331a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
2332a0292f23SGarrett Wollman 				continue;
233340db8ef7SAndras Olah 			to->to_flag |= TOF_CC;
2334a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2335a0292f23SGarrett Wollman 			    (char *)&to->to_cc, sizeof(to->to_cc));
2336a0292f23SGarrett Wollman 			NTOHL(to->to_cc);
2337a0292f23SGarrett Wollman 			/*
2338a0292f23SGarrett Wollman 			 * A CC or CC.new option received in a SYN makes
2339a0292f23SGarrett Wollman 			 * it ok to send CC in subsequent segments.
2340a0292f23SGarrett Wollman 			 */
2341fb59c426SYoshinobu Inoue 			if (th->th_flags & TH_SYN)
2342a0292f23SGarrett Wollman 				tp->t_flags |= TF_RCVD_CC;
2343a0292f23SGarrett Wollman 			break;
2344a0292f23SGarrett Wollman 		case TCPOPT_CCNEW:
2345a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
2346a0292f23SGarrett Wollman 				continue;
2347fb59c426SYoshinobu Inoue 			if (!(th->th_flags & TH_SYN))
2348a0292f23SGarrett Wollman 				continue;
2349a0292f23SGarrett Wollman 			to->to_flag |= TOF_CCNEW;
2350a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2351a0292f23SGarrett Wollman 			    (char *)&to->to_cc, sizeof(to->to_cc));
2352a0292f23SGarrett Wollman 			NTOHL(to->to_cc);
2353a0292f23SGarrett Wollman 			/*
2354a0292f23SGarrett Wollman 			 * A CC or CC.new option received in a SYN makes
2355a0292f23SGarrett Wollman 			 * it ok to send CC in subsequent segments.
2356a0292f23SGarrett Wollman 			 */
2357a0292f23SGarrett Wollman 			tp->t_flags |= TF_RCVD_CC;
2358a0292f23SGarrett Wollman 			break;
2359a0292f23SGarrett Wollman 		case TCPOPT_CCECHO:
2360a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
2361a0292f23SGarrett Wollman 				continue;
2362fb59c426SYoshinobu Inoue 			if (!(th->th_flags & TH_SYN))
2363a0292f23SGarrett Wollman 				continue;
2364a0292f23SGarrett Wollman 			to->to_flag |= TOF_CCECHO;
2365a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2366a0292f23SGarrett Wollman 			    (char *)&to->to_ccecho, sizeof(to->to_ccecho));
2367a0292f23SGarrett Wollman 			NTOHL(to->to_ccecho);
2368a0292f23SGarrett Wollman 			break;
2369df8bae1dSRodney W. Grimes 		}
2370df8bae1dSRodney W. Grimes 	}
2371fb59c426SYoshinobu Inoue 	if (th->th_flags & TH_SYN)
2372a0292f23SGarrett Wollman 		tcp_mss(tp, mss);	/* sets t_maxseg */
2373df8bae1dSRodney W. Grimes }
2374df8bae1dSRodney W. Grimes 
2375df8bae1dSRodney W. Grimes /*
2376df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
2377df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
2378df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
2379df8bae1dSRodney W. Grimes  * sequencing purposes.
2380df8bae1dSRodney W. Grimes  */
23810312fbe9SPoul-Henning Kamp static void
2382fb59c426SYoshinobu Inoue tcp_pulloutofband(so, th, m, off)
2383df8bae1dSRodney W. Grimes 	struct socket *so;
2384fb59c426SYoshinobu Inoue 	struct tcphdr *th;
2385df8bae1dSRodney W. Grimes 	register struct mbuf *m;
2386fb59c426SYoshinobu Inoue 	int off;		/* delayed to be droped hdrlen */
2387df8bae1dSRodney W. Grimes {
2388fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
2389df8bae1dSRodney W. Grimes 
2390df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
2391df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
2392df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
2393df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
2394df8bae1dSRodney W. Grimes 
2395df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
2396df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
2397df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
2398df8bae1dSRodney W. Grimes 			m->m_len--;
2399df8bae1dSRodney W. Grimes 			return;
2400df8bae1dSRodney W. Grimes 		}
2401df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
2402df8bae1dSRodney W. Grimes 		m = m->m_next;
2403df8bae1dSRodney W. Grimes 		if (m == 0)
2404df8bae1dSRodney W. Grimes 			break;
2405df8bae1dSRodney W. Grimes 	}
2406df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
2407df8bae1dSRodney W. Grimes }
2408df8bae1dSRodney W. Grimes 
2409df8bae1dSRodney W. Grimes /*
2410df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
2411df8bae1dSRodney W. Grimes  * and update averages and current timeout.
2412df8bae1dSRodney W. Grimes  */
24130312fbe9SPoul-Henning Kamp static void
2414df8bae1dSRodney W. Grimes tcp_xmit_timer(tp, rtt)
2415df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
24169b8b58e0SJonathan Lemon 	int rtt;
2417df8bae1dSRodney W. Grimes {
2418233e8c18SGarrett Wollman 	register int delta;
2419233e8c18SGarrett Wollman 
2420233e8c18SGarrett Wollman 	tcpstat.tcps_rttupdated++;
2421233e8c18SGarrett Wollman 	tp->t_rttupdated++;
2422233e8c18SGarrett Wollman 	if (tp->t_srtt != 0) {
2423233e8c18SGarrett Wollman 		/*
2424233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
2425233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
2426233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
2427233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
2428233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
2429233e8c18SGarrett Wollman 		 */
2430233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
2431233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
2432233e8c18SGarrett Wollman 
2433233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
2434233e8c18SGarrett Wollman 			tp->t_srtt = 1;
2435233e8c18SGarrett Wollman 
2436233e8c18SGarrett Wollman 		/*
2437233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
2438233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
2439233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
2440233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
2441233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
2442233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
2443233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
2444233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
2445233e8c18SGarrett Wollman 		 */
2446233e8c18SGarrett Wollman 		if (delta < 0)
2447233e8c18SGarrett Wollman 			delta = -delta;
2448233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
2449233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
2450233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
2451233e8c18SGarrett Wollman 	} else {
2452233e8c18SGarrett Wollman 		/*
2453233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
2454233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
2455233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
2456233e8c18SGarrett Wollman 		 */
2457233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
2458233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
2459233e8c18SGarrett Wollman 	}
24609b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
2461df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
2462df8bae1dSRodney W. Grimes 
2463df8bae1dSRodney W. Grimes 	/*
2464df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
2465df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
2466df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
2467df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
2468df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
2469df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
2470df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
2471df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
2472df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
2473df8bae1dSRodney W. Grimes 	 */
2474233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
24759e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
2476df8bae1dSRodney W. Grimes 
2477df8bae1dSRodney W. Grimes 	/*
2478df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
2479df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
2480df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
2481df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
2482df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
2483df8bae1dSRodney W. Grimes 	 */
2484df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
2485df8bae1dSRodney W. Grimes }
2486df8bae1dSRodney W. Grimes 
2487df8bae1dSRodney W. Grimes /*
2488df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
2489df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
2490df8bae1dSRodney W. Grimes  * If none, use an mss that can be handled on the outgoing
2491df8bae1dSRodney W. Grimes  * interface without forcing IP to fragment; if bigger than
2492df8bae1dSRodney W. Grimes  * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
2493df8bae1dSRodney W. Grimes  * to utilize large mbufs.  If no route is found, route has no mtu,
2494df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
2495df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
2496df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
2497df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
2498df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
2499df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
2500df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
2501a0292f23SGarrett Wollman  *
2502a0292f23SGarrett Wollman  * Also take into account the space needed for options that we
2503a0292f23SGarrett Wollman  * send regularly.  Make maxseg shorter by that amount to assure
2504a0292f23SGarrett Wollman  * that we can send maxseg amount of data even when the options
2505a0292f23SGarrett Wollman  * are present.  Store the upper limit of the length of options plus
2506a0292f23SGarrett Wollman  * data in maxopd.
2507a0292f23SGarrett Wollman  *
2508a0292f23SGarrett Wollman  * NOTE that this routine is only called when we process an incoming
2509a0292f23SGarrett Wollman  * segment, for outgoing segments only tcp_mssopt is called.
2510a0292f23SGarrett Wollman  *
2511a0292f23SGarrett Wollman  * In case of T/TCP, we call this routine during implicit connection
2512a0292f23SGarrett Wollman  * setup as well (offer = -1), to initialize maxseg from the cached
2513a0292f23SGarrett Wollman  * MSS of our peer.
2514df8bae1dSRodney W. Grimes  */
2515a0292f23SGarrett Wollman void
2516df8bae1dSRodney W. Grimes tcp_mss(tp, offer)
2517a0292f23SGarrett Wollman 	struct tcpcb *tp;
2518a0292f23SGarrett Wollman 	int offer;
2519df8bae1dSRodney W. Grimes {
2520df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
2521df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
2522df8bae1dSRodney W. Grimes 	register int rtt, mss;
2523df8bae1dSRodney W. Grimes 	u_long bufsize;
2524df8bae1dSRodney W. Grimes 	struct inpcb *inp;
2525df8bae1dSRodney W. Grimes 	struct socket *so;
2526a0292f23SGarrett Wollman 	struct rmxp_tao *taop;
2527a0292f23SGarrett Wollman 	int origoffer = offer;
2528fb59c426SYoshinobu Inoue #ifdef INET6
2529fb59c426SYoshinobu Inoue 	int isipv6;
2530fb59c426SYoshinobu Inoue 	int min_protoh;
2531fb59c426SYoshinobu Inoue #endif
2532df8bae1dSRodney W. Grimes 
2533df8bae1dSRodney W. Grimes 	inp = tp->t_inpcb;
2534fb59c426SYoshinobu Inoue #ifdef INET6
2535fb59c426SYoshinobu Inoue 	isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
2536fb59c426SYoshinobu Inoue 	min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr)
2537fb59c426SYoshinobu Inoue 			    : sizeof (struct tcpiphdr);
2538fb59c426SYoshinobu Inoue #else
2539fb59c426SYoshinobu Inoue #define min_protoh  (sizeof (struct tcpiphdr))
2540fb59c426SYoshinobu Inoue #endif
2541fb59c426SYoshinobu Inoue #ifdef INET6
2542fb59c426SYoshinobu Inoue 	if (isipv6)
2543fb59c426SYoshinobu Inoue 		rt = tcp_rtlookup6(inp);
2544fb59c426SYoshinobu Inoue 	else
2545fb59c426SYoshinobu Inoue #endif
2546fb59c426SYoshinobu Inoue 	rt = tcp_rtlookup(inp);
2547fb59c426SYoshinobu Inoue 	if (rt == NULL) {
2548fb59c426SYoshinobu Inoue 		tp->t_maxopd = tp->t_maxseg =
2549fb59c426SYoshinobu Inoue #ifdef INET6
2550fb59c426SYoshinobu Inoue 		isipv6 ? tcp_v6mssdflt :
2551fb59c426SYoshinobu Inoue #endif /* INET6 */
2552fb59c426SYoshinobu Inoue 		tcp_mssdflt;
2553a0292f23SGarrett Wollman 		return;
2554df8bae1dSRodney W. Grimes 	}
2555df8bae1dSRodney W. Grimes 	ifp = rt->rt_ifp;
2556df8bae1dSRodney W. Grimes 	so = inp->inp_socket;
2557df8bae1dSRodney W. Grimes 
2558a0292f23SGarrett Wollman 	taop = rmx_taop(rt->rt_rmx);
2559a0292f23SGarrett Wollman 	/*
2560a0292f23SGarrett Wollman 	 * Offer == -1 means that we didn't receive SYN yet,
2561a0292f23SGarrett Wollman 	 * use cached value in that case;
2562a0292f23SGarrett Wollman 	 */
2563a0292f23SGarrett Wollman 	if (offer == -1)
2564a0292f23SGarrett Wollman 		offer = taop->tao_mssopt;
2565a0292f23SGarrett Wollman 	/*
2566a0292f23SGarrett Wollman 	 * Offer == 0 means that there was no MSS on the SYN segment,
2567a0292f23SGarrett Wollman 	 * in this case we use tcp_mssdflt.
2568a0292f23SGarrett Wollman 	 */
2569a0292f23SGarrett Wollman 	if (offer == 0)
2570fb59c426SYoshinobu Inoue 		offer =
2571fb59c426SYoshinobu Inoue #ifdef INET6
2572fb59c426SYoshinobu Inoue 			isipv6 ? tcp_v6mssdflt :
2573fb59c426SYoshinobu Inoue #endif /* INET6 */
2574fb59c426SYoshinobu Inoue 			tcp_mssdflt;
2575a0292f23SGarrett Wollman 	else
2576a0292f23SGarrett Wollman 		/*
2577a0292f23SGarrett Wollman 		 * Sanity check: make sure that maxopd will be large
2578a0292f23SGarrett Wollman 		 * enough to allow some data on segments even is the
2579a0292f23SGarrett Wollman 		 * all the option space is used (40bytes).  Otherwise
2580a0292f23SGarrett Wollman 		 * funny things may happen in tcp_output.
2581a0292f23SGarrett Wollman 		 */
2582a0292f23SGarrett Wollman 		offer = max(offer, 64);
2583a0292f23SGarrett Wollman 	taop->tao_mssopt = offer;
2584a0292f23SGarrett Wollman 
2585df8bae1dSRodney W. Grimes 	/*
2586df8bae1dSRodney W. Grimes 	 * While we're here, check if there's an initial rtt
2587df8bae1dSRodney W. Grimes 	 * or rttvar.  Convert from the route-table units
2588df8bae1dSRodney W. Grimes 	 * to scaled multiples of the slow timeout timer.
2589df8bae1dSRodney W. Grimes 	 */
2590df8bae1dSRodney W. Grimes 	if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
2591df8bae1dSRodney W. Grimes 		/*
2592a0292f23SGarrett Wollman 		 * XXX the lock bit for RTT indicates that the value
2593df8bae1dSRodney W. Grimes 		 * is also a minimum value; this is subject to time.
2594df8bae1dSRodney W. Grimes 		 */
2595df8bae1dSRodney W. Grimes 		if (rt->rt_rmx.rmx_locks & RTV_RTT)
25969b8b58e0SJonathan Lemon 			tp->t_rttmin = rtt / (RTM_RTTUNIT / hz);
25979b8b58e0SJonathan Lemon 		tp->t_srtt = rtt / (RTM_RTTUNIT / (hz * TCP_RTT_SCALE));
2598dd224982SGarrett Wollman 		tcpstat.tcps_usedrtt++;
2599dd224982SGarrett Wollman 		if (rt->rt_rmx.rmx_rttvar) {
2600df8bae1dSRodney W. Grimes 			tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
26019b8b58e0SJonathan Lemon 			    (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE));
2602dd224982SGarrett Wollman 			tcpstat.tcps_usedrttvar++;
2603dd224982SGarrett Wollman 		} else {
2604df8bae1dSRodney W. Grimes 			/* default variation is +- 1 rtt */
2605df8bae1dSRodney W. Grimes 			tp->t_rttvar =
2606df8bae1dSRodney W. Grimes 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
2607dd224982SGarrett Wollman 		}
2608df8bae1dSRodney W. Grimes 		TCPT_RANGESET(tp->t_rxtcur,
2609df8bae1dSRodney W. Grimes 			      ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
2610df8bae1dSRodney W. Grimes 			      tp->t_rttmin, TCPTV_REXMTMAX);
2611df8bae1dSRodney W. Grimes 	}
2612df8bae1dSRodney W. Grimes 	/*
2613df8bae1dSRodney W. Grimes 	 * if there's an mtu associated with the route, use it
2614fb59c426SYoshinobu Inoue 	 * else, use the link mtu.
2615df8bae1dSRodney W. Grimes 	 */
2616df8bae1dSRodney W. Grimes 	if (rt->rt_rmx.rmx_mtu)
2617fb59c426SYoshinobu Inoue 		mss = rt->rt_rmx.rmx_mtu - min_protoh;
2618df8bae1dSRodney W. Grimes 	else
2619df8bae1dSRodney W. Grimes 	{
2620fb59c426SYoshinobu Inoue 		mss =
2621fb59c426SYoshinobu Inoue #ifdef INET6
2622fb59c426SYoshinobu Inoue 			(isipv6 ? nd_ifinfo[rt->rt_ifp->if_index].linkmtu :
2623fb59c426SYoshinobu Inoue #endif
2624fb59c426SYoshinobu Inoue 			 ifp->if_mtu
2625fb59c426SYoshinobu Inoue #ifdef INET6
2626fb59c426SYoshinobu Inoue 			 )
2627fb59c426SYoshinobu Inoue #endif
2628fb59c426SYoshinobu Inoue 			- min_protoh;
2629fb59c426SYoshinobu Inoue #ifdef INET6
2630fb59c426SYoshinobu Inoue 		if (isipv6) {
2631fb59c426SYoshinobu Inoue 			if (!in6_localaddr(&inp->in6p_faddr))
2632fb59c426SYoshinobu Inoue 				mss = min(mss, tcp_v6mssdflt);
2633fb59c426SYoshinobu Inoue 		} else
2634fb59c426SYoshinobu Inoue #endif
2635a0292f23SGarrett Wollman 		if (!in_localaddr(inp->inp_faddr))
2636a0292f23SGarrett Wollman 			mss = min(mss, tcp_mssdflt);
2637a0292f23SGarrett Wollman 	}
2638a0292f23SGarrett Wollman 	mss = min(mss, offer);
2639a0292f23SGarrett Wollman 	/*
2640a0292f23SGarrett Wollman 	 * maxopd stores the maximum length of data AND options
2641a0292f23SGarrett Wollman 	 * in a segment; maxseg is the amount of data in a normal
2642a0292f23SGarrett Wollman 	 * segment.  We need to store this value (maxopd) apart
2643a0292f23SGarrett Wollman 	 * from maxseg, because now every segment carries options
2644a0292f23SGarrett Wollman 	 * and thus we normally have somewhat less data in segments.
2645a0292f23SGarrett Wollman 	 */
2646a0292f23SGarrett Wollman 	tp->t_maxopd = mss;
2647a0292f23SGarrett Wollman 
2648a0292f23SGarrett Wollman 	/*
2649a0292f23SGarrett Wollman 	 * In case of T/TCP, origoffer==-1 indicates, that no segments
2650a0292f23SGarrett Wollman 	 * were received yet.  In this case we just guess, otherwise
2651a0292f23SGarrett Wollman 	 * we do the same as before T/TCP.
2652a0292f23SGarrett Wollman 	 */
2653a0292f23SGarrett Wollman  	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
2654a0292f23SGarrett Wollman 	    (origoffer == -1 ||
2655a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
2656a0292f23SGarrett Wollman 		mss -= TCPOLEN_TSTAMP_APPA;
2657a0292f23SGarrett Wollman  	if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
2658a0292f23SGarrett Wollman 	    (origoffer == -1 ||
2659a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC))
2660a0292f23SGarrett Wollman 		mss -= TCPOLEN_CC_APPA;
2661a0292f23SGarrett Wollman 
2662df8bae1dSRodney W. Grimes #if	(MCLBYTES & (MCLBYTES - 1)) == 0
2663df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
2664df8bae1dSRodney W. Grimes 			mss &= ~(MCLBYTES-1);
2665df8bae1dSRodney W. Grimes #else
2666df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
2667df8bae1dSRodney W. Grimes 			mss = mss / MCLBYTES * MCLBYTES;
2668df8bae1dSRodney W. Grimes #endif
2669df8bae1dSRodney W. Grimes 	/*
2670df8bae1dSRodney W. Grimes 	 * If there's a pipesize, change the socket buffer
2671df8bae1dSRodney W. Grimes 	 * to that size.  Make the socket buffers an integral
2672df8bae1dSRodney W. Grimes 	 * number of mss units; if the mss is larger than
2673df8bae1dSRodney W. Grimes 	 * the socket buffer, decrease the mss.
2674df8bae1dSRodney W. Grimes 	 */
2675df8bae1dSRodney W. Grimes #ifdef RTV_SPIPE
2676df8bae1dSRodney W. Grimes 	if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
2677df8bae1dSRodney W. Grimes #endif
2678df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
2679df8bae1dSRodney W. Grimes 	if (bufsize < mss)
2680df8bae1dSRodney W. Grimes 		mss = bufsize;
2681df8bae1dSRodney W. Grimes 	else {
2682df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
2683df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
2684df8bae1dSRodney W. Grimes 			bufsize = sb_max;
2685ecf72308SBrian Feldman 		(void)sbreserve(&so->so_snd, bufsize, so, NULL);
2686df8bae1dSRodney W. Grimes 	}
2687df8bae1dSRodney W. Grimes 	tp->t_maxseg = mss;
2688df8bae1dSRodney W. Grimes 
2689df8bae1dSRodney W. Grimes #ifdef RTV_RPIPE
2690df8bae1dSRodney W. Grimes 	if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
2691df8bae1dSRodney W. Grimes #endif
2692df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
2693df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
2694df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
2695df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
2696df8bae1dSRodney W. Grimes 			bufsize = sb_max;
2697ecf72308SBrian Feldman 		(void)sbreserve(&so->so_rcv, bufsize, so, NULL);
2698df8bae1dSRodney W. Grimes 	}
26999b8b58e0SJonathan Lemon 
2700a0292f23SGarrett Wollman 	/*
27019b8b58e0SJonathan Lemon 	 * Set the slow-start flight size depending on whether this
27029b8b58e0SJonathan Lemon 	 * is a local network or not.
2703a0292f23SGarrett Wollman 	 */
2704fb59c426SYoshinobu Inoue 	if (
2705fb59c426SYoshinobu Inoue #ifdef INET6
2706fb59c426SYoshinobu Inoue 	    (isipv6 && in6_localaddr(&inp->in6p_faddr)) ||
2707fb59c426SYoshinobu Inoue 	    (!isipv6 &&
2708fb59c426SYoshinobu Inoue #endif
2709fb59c426SYoshinobu Inoue 	     in_localaddr(inp->inp_faddr)
2710fb59c426SYoshinobu Inoue #ifdef INET6
2711fb59c426SYoshinobu Inoue 	     )
2712fb59c426SYoshinobu Inoue #endif
2713fb59c426SYoshinobu Inoue 	    )
27149b8b58e0SJonathan Lemon 		tp->snd_cwnd = mss * ss_fltsz_local;
27159b8b58e0SJonathan Lemon 	else
27169b8b58e0SJonathan Lemon 		tp->snd_cwnd = mss * ss_fltsz;
2717df8bae1dSRodney W. Grimes 
2718df8bae1dSRodney W. Grimes 	if (rt->rt_rmx.rmx_ssthresh) {
2719df8bae1dSRodney W. Grimes 		/*
2720df8bae1dSRodney W. Grimes 		 * There's some sort of gateway or interface
2721df8bae1dSRodney W. Grimes 		 * buffer limit on the path.  Use this to set
2722df8bae1dSRodney W. Grimes 		 * the slow start threshhold, but set the
2723df8bae1dSRodney W. Grimes 		 * threshold to no less than 2*mss.
2724df8bae1dSRodney W. Grimes 		 */
2725df8bae1dSRodney W. Grimes 		tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
2726dd224982SGarrett Wollman 		tcpstat.tcps_usedssthresh++;
2727df8bae1dSRodney W. Grimes 	}
2728a0292f23SGarrett Wollman }
2729a0292f23SGarrett Wollman 
2730a0292f23SGarrett Wollman /*
2731a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
2732a0292f23SGarrett Wollman  */
2733a0292f23SGarrett Wollman int
2734a0292f23SGarrett Wollman tcp_mssopt(tp)
2735a0292f23SGarrett Wollman 	struct tcpcb *tp;
2736a0292f23SGarrett Wollman {
2737a0292f23SGarrett Wollman 	struct rtentry *rt;
2738fb59c426SYoshinobu Inoue #ifdef INET6
2739fb59c426SYoshinobu Inoue 	int isipv6;
2740fb59c426SYoshinobu Inoue 	int min_protoh;
2741fb59c426SYoshinobu Inoue #endif
2742a0292f23SGarrett Wollman 
2743fb59c426SYoshinobu Inoue #ifdef INET6
2744fb59c426SYoshinobu Inoue 	isipv6 = ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
2745fb59c426SYoshinobu Inoue 	min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr)
2746fb59c426SYoshinobu Inoue 			    : sizeof (struct tcpiphdr);
2747fb59c426SYoshinobu Inoue #else
2748fb59c426SYoshinobu Inoue #define min_protoh  (sizeof (struct tcpiphdr))
2749fb59c426SYoshinobu Inoue #endif
2750fb59c426SYoshinobu Inoue #ifdef INET6
2751fb59c426SYoshinobu Inoue 	if (isipv6)
2752fb59c426SYoshinobu Inoue 		rt = tcp_rtlookup6(tp->t_inpcb);
2753fb59c426SYoshinobu Inoue 	else
2754fb59c426SYoshinobu Inoue #endif /* INET6 */
2755a0292f23SGarrett Wollman 	rt = tcp_rtlookup(tp->t_inpcb);
2756a0292f23SGarrett Wollman 	if (rt == NULL)
2757fb59c426SYoshinobu Inoue 		return
2758fb59c426SYoshinobu Inoue #ifdef INET6
2759fb59c426SYoshinobu Inoue 			isipv6 ? tcp_v6mssdflt :
2760fb59c426SYoshinobu Inoue #endif /* INET6 */
2761fb59c426SYoshinobu Inoue 			tcp_mssdflt;
2762a0292f23SGarrett Wollman 
2763fb59c426SYoshinobu Inoue 	return rt->rt_ifp->if_mtu - min_protoh;
2764df8bae1dSRodney W. Grimes }
2765