xref: /freebsd/sys/netinet/tcp_input.c (revision 80ab7c0ed8a852ad9f36cd4fee124a31bc2543d2)
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
3480ab7c0eSGarrett Wollman  *	$Id: tcp_input.c,v 1.80 1998/08/24 07:47:39 dfr Exp $
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37f9e354dfSJulian Elischer #include "opt_ipfw.h"	/* for ipfw_fwd */
380cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
390cc12cc5SJoerg Wunsch 
40df8bae1dSRodney W. Grimes #include <sys/param.h>
41df8bae1dSRodney W. Grimes #include <sys/systm.h>
4298163b98SPoul-Henning Kamp #include <sys/kernel.h>
4398163b98SPoul-Henning Kamp #include <sys/sysctl.h>
44df8bae1dSRodney W. Grimes #include <sys/malloc.h>
45df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
46a29f300eSGarrett Wollman #include <sys/proc.h>		/* for proc0 declaration */
47df8bae1dSRodney W. Grimes #include <sys/protosw.h>
48df8bae1dSRodney W. Grimes #include <sys/socket.h>
49df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
50816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
51df8bae1dSRodney W. Grimes 
52e79adb8eSGarrett Wollman #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
53e79adb8eSGarrett Wollman 
54df8bae1dSRodney W. Grimes #include <net/if.h>
55df8bae1dSRodney W. Grimes #include <net/route.h>
56df8bae1dSRodney W. Grimes 
57df8bae1dSRodney W. Grimes #include <netinet/in.h>
58df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
59df8bae1dSRodney W. Grimes #include <netinet/ip.h>
60df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
61df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
62df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
63df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
64df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
65df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
66df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
67df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
68610ee2f9SDavid Greenman #ifdef TCPDEBUG
69df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
70f708ef1bSPoul-Henning Kamp static struct	tcpiphdr tcp_saveti;
71610ee2f9SDavid Greenman #endif
72df8bae1dSRodney W. Grimes 
730312fbe9SPoul-Henning Kamp static int	tcprexmtthresh = 3;
742f96f1f4SGarrett Wollman tcp_seq	tcp_iss;
752f96f1f4SGarrett Wollman tcp_cc	tcp_ccgen;
760312fbe9SPoul-Henning Kamp 
772f96f1f4SGarrett Wollman struct	tcpstat tcpstat;
7898163b98SPoul-Henning Kamp SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats,
7998163b98SPoul-Henning Kamp 	CTLFLAG_RD, &tcpstat , tcpstat, "");
800312fbe9SPoul-Henning Kamp 
81d78a37adSPaul Traina static int log_in_vain = 0;
82816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
83816a3d83SPoul-Henning Kamp 	&log_in_vain, 0, "");
84816a3d83SPoul-Henning Kamp 
85f498eeeeSDavid Greenman int tcp_delack_enabled = 1;
8684e33c9eSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW,
87f498eeeeSDavid Greenman 	&tcp_delack_enabled, 0, "");
88f498eeeeSDavid Greenman 
892f96f1f4SGarrett Wollman u_long	tcp_now;
9015bd2b43SDavid Greenman struct inpcbhead tcb;
9115bd2b43SDavid Greenman struct inpcbinfo tcbinfo;
92df8bae1dSRodney W. Grimes 
930312fbe9SPoul-Henning Kamp static void	 tcp_dooptions __P((struct tcpcb *,
940312fbe9SPoul-Henning Kamp 	    u_char *, int, struct tcpiphdr *, struct tcpopt *));
950312fbe9SPoul-Henning Kamp static void	 tcp_pulloutofband __P((struct socket *,
960312fbe9SPoul-Henning Kamp 	    struct tcpiphdr *, struct mbuf *));
970312fbe9SPoul-Henning Kamp static int	 tcp_reass __P((struct tcpcb *, struct tcpiphdr *, struct mbuf *));
980312fbe9SPoul-Henning Kamp static void	 tcp_xmit_timer __P((struct tcpcb *, int));
990312fbe9SPoul-Henning Kamp 
100df8bae1dSRodney W. Grimes 
101df8bae1dSRodney W. Grimes /*
102df8bae1dSRodney W. Grimes  * Insert segment ti into reassembly queue of tcp with
103df8bae1dSRodney W. Grimes  * control block tp.  Return TH_FIN if reassembly now includes
104df8bae1dSRodney W. Grimes  * a segment with FIN.  The macro form does the common case inline
105df8bae1dSRodney W. Grimes  * (segment is the next to be received on an established connection,
106df8bae1dSRodney W. Grimes  * and the queue is empty), avoiding linkage into and removal
107df8bae1dSRodney W. Grimes  * from the queue and repetition of various conversions.
108df8bae1dSRodney W. Grimes  * Set DELACK for segments received in order, but ack immediately
109df8bae1dSRodney W. Grimes  * when segments are out of order (so fast retransmit can work).
110df8bae1dSRodney W. Grimes  */
1116b067b07SDavid Greenman #define	TCP_REASS(tp, ti, m, so, flags) { \
1126b067b07SDavid Greenman 	if ((ti)->ti_seq == (tp)->rcv_nxt && \
1136effc713SDoug Rabson 	    (tp)->t_segq == NULL && \
1146b067b07SDavid Greenman 	    (tp)->t_state == TCPS_ESTABLISHED) { \
115f498eeeeSDavid Greenman 		if (tcp_delack_enabled) \
1166b067b07SDavid Greenman 			tp->t_flags |= TF_DELACK; \
117f498eeeeSDavid Greenman 		else \
118f498eeeeSDavid Greenman 			tp->t_flags |= TF_ACKNOW; \
1196b067b07SDavid Greenman 		(tp)->rcv_nxt += (ti)->ti_len; \
1206b067b07SDavid Greenman 		flags = (ti)->ti_flags & TH_FIN; \
1216b067b07SDavid Greenman 		tcpstat.tcps_rcvpack++;\
1226b067b07SDavid Greenman 		tcpstat.tcps_rcvbyte += (ti)->ti_len;\
1236b067b07SDavid Greenman 		sbappend(&(so)->so_rcv, (m)); \
1246b067b07SDavid Greenman 		sorwakeup(so); \
1256b067b07SDavid Greenman 	} else { \
1266b067b07SDavid Greenman 		(flags) = tcp_reass((tp), (ti), (m)); \
1276b067b07SDavid Greenman 		tp->t_flags |= TF_ACKNOW; \
1286b067b07SDavid Greenman 	} \
1296b067b07SDavid Greenman }
130df8bae1dSRodney W. Grimes 
1310312fbe9SPoul-Henning Kamp static int
132df8bae1dSRodney W. Grimes tcp_reass(tp, ti, m)
133df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
134df8bae1dSRodney W. Grimes 	register struct tcpiphdr *ti;
135df8bae1dSRodney W. Grimes 	struct mbuf *m;
136df8bae1dSRodney W. Grimes {
1376effc713SDoug Rabson 	struct mbuf *q;
1386effc713SDoug Rabson 	struct mbuf *p;
1396effc713SDoug Rabson 	struct mbuf *nq;
140df8bae1dSRodney W. Grimes 	struct socket *so = tp->t_inpcb->inp_socket;
141df8bae1dSRodney W. Grimes 	int flags;
142df8bae1dSRodney W. Grimes 
1436effc713SDoug Rabson #define GETTCP(m)	((struct tcpiphdr *)m->m_pkthdr.header)
1446effc713SDoug Rabson 
145df8bae1dSRodney W. Grimes 	/*
146df8bae1dSRodney W. Grimes 	 * Call with ti==0 after become established to
147df8bae1dSRodney W. Grimes 	 * force pre-ESTABLISHED data up to user socket.
148df8bae1dSRodney W. Grimes 	 */
149df8bae1dSRodney W. Grimes 	if (ti == 0)
150df8bae1dSRodney W. Grimes 		goto present;
151df8bae1dSRodney W. Grimes 
1526effc713SDoug Rabson 	m->m_pkthdr.header = ti;
1536effc713SDoug Rabson 
154df8bae1dSRodney W. Grimes 	/*
155df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
156df8bae1dSRodney W. Grimes 	 */
1576effc713SDoug Rabson 	for (q = tp->t_segq, p = NULL; q; p = q, q = q->m_nextpkt)
1586effc713SDoug Rabson 		if (SEQ_GT(GETTCP(q)->ti_seq, ti->ti_seq))
159df8bae1dSRodney W. Grimes 			break;
160df8bae1dSRodney W. Grimes 
161df8bae1dSRodney W. Grimes 	/*
162df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
163df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
164df8bae1dSRodney W. Grimes 	 * segment.  If it provides all of our data, drop us.
165df8bae1dSRodney W. Grimes 	 */
1666effc713SDoug Rabson 	if (p != NULL) {
167df8bae1dSRodney W. Grimes 		register int i;
168df8bae1dSRodney W. Grimes 		/* conversion to int (in i) handles seq wraparound */
1696effc713SDoug Rabson 		i = GETTCP(p)->ti_seq + GETTCP(p)->ti_len - ti->ti_seq;
170df8bae1dSRodney W. Grimes 		if (i > 0) {
171df8bae1dSRodney W. Grimes 			if (i >= ti->ti_len) {
172df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvduppack++;
173df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvdupbyte += ti->ti_len;
174df8bae1dSRodney W. Grimes 				m_freem(m);
175a0292f23SGarrett Wollman 				/*
176a0292f23SGarrett Wollman 				 * Try to present any queued data
177a0292f23SGarrett Wollman 				 * at the left window edge to the user.
178a0292f23SGarrett Wollman 				 * This is needed after the 3-WHS
179a0292f23SGarrett Wollman 				 * completes.
180a0292f23SGarrett Wollman 				 */
181a0292f23SGarrett Wollman 				goto present;	/* ??? */
182df8bae1dSRodney W. Grimes 			}
183df8bae1dSRodney W. Grimes 			m_adj(m, i);
184df8bae1dSRodney W. Grimes 			ti->ti_len -= i;
185df8bae1dSRodney W. Grimes 			ti->ti_seq += i;
186df8bae1dSRodney W. Grimes 		}
187df8bae1dSRodney W. Grimes 	}
188df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvoopack++;
189df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvoobyte += ti->ti_len;
190df8bae1dSRodney W. Grimes 
191df8bae1dSRodney W. Grimes 	/*
192df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
193df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
194df8bae1dSRodney W. Grimes 	 */
1956effc713SDoug Rabson 	while (q) {
1966effc713SDoug Rabson 		register int i = (ti->ti_seq + ti->ti_len) - GETTCP(q)->ti_seq;
197df8bae1dSRodney W. Grimes 		if (i <= 0)
198df8bae1dSRodney W. Grimes 			break;
1996effc713SDoug Rabson 		if (i < GETTCP(q)->ti_len) {
2006effc713SDoug Rabson 			GETTCP(q)->ti_seq += i;
2016effc713SDoug Rabson 			GETTCP(q)->ti_len -= i;
2026effc713SDoug Rabson 			m_adj(q, i);
203df8bae1dSRodney W. Grimes 			break;
204df8bae1dSRodney W. Grimes 		}
2056effc713SDoug Rabson 
2066effc713SDoug Rabson 		nq = q->m_nextpkt;
2076effc713SDoug Rabson 		if (p)
2086effc713SDoug Rabson 			p->m_nextpkt = nq;
2096effc713SDoug Rabson 		else
2106effc713SDoug Rabson 			tp->t_segq = nq;
2116effc713SDoug Rabson 		m_freem(q);
2126effc713SDoug Rabson 		q = nq;
213df8bae1dSRodney W. Grimes 	}
214df8bae1dSRodney W. Grimes 
2156effc713SDoug Rabson 	if (p == NULL) {
2166effc713SDoug Rabson 		m->m_nextpkt = tp->t_segq;
2176effc713SDoug Rabson 		tp->t_segq = m;
2186effc713SDoug Rabson 	} else {
2196effc713SDoug Rabson 		m->m_nextpkt = p->m_nextpkt;
2206effc713SDoug Rabson 		p->m_nextpkt = m;
2216effc713SDoug Rabson 	}
222df8bae1dSRodney W. Grimes 
223df8bae1dSRodney W. Grimes present:
224df8bae1dSRodney W. Grimes 	/*
225df8bae1dSRodney W. Grimes 	 * Present data to user, advancing rcv_nxt through
226df8bae1dSRodney W. Grimes 	 * completed sequence space.
227df8bae1dSRodney W. Grimes 	 */
228a0292f23SGarrett Wollman 	if (!TCPS_HAVEESTABLISHED(tp->t_state))
229df8bae1dSRodney W. Grimes 		return (0);
2306effc713SDoug Rabson 	q = tp->t_segq;
2316effc713SDoug Rabson 	if (!q || GETTCP(q)->ti_seq != tp->rcv_nxt)
232df8bae1dSRodney W. Grimes 		return (0);
233df8bae1dSRodney W. Grimes 	do {
2346effc713SDoug Rabson 		tp->rcv_nxt += GETTCP(q)->ti_len;
2356effc713SDoug Rabson 		flags = GETTCP(q)->ti_flags & TH_FIN;
2366effc713SDoug Rabson 		nq = q->m_nextpkt;
2376effc713SDoug Rabson 		tp->t_segq = nq;
2386effc713SDoug Rabson 		q->m_nextpkt = NULL;
239df8bae1dSRodney W. Grimes 		if (so->so_state & SS_CANTRCVMORE)
2406effc713SDoug Rabson 			m_freem(q);
241df8bae1dSRodney W. Grimes 		else
2426effc713SDoug Rabson 			sbappend(&so->so_rcv, q);
2436effc713SDoug Rabson 		q = nq;
2446effc713SDoug Rabson 	} while (q && GETTCP(q)->ti_seq == tp->rcv_nxt);
245df8bae1dSRodney W. Grimes 	sorwakeup(so);
246df8bae1dSRodney W. Grimes 	return (flags);
2476effc713SDoug Rabson 
2486effc713SDoug Rabson #undef GETTCP
249df8bae1dSRodney W. Grimes }
250df8bae1dSRodney W. Grimes 
251df8bae1dSRodney W. Grimes /*
252df8bae1dSRodney W. Grimes  * TCP input routine, follows pages 65-76 of the
253df8bae1dSRodney W. Grimes  * protocol specification dated September, 1981 very closely.
254df8bae1dSRodney W. Grimes  */
255df8bae1dSRodney W. Grimes void
256df8bae1dSRodney W. Grimes tcp_input(m, iphlen)
257df8bae1dSRodney W. Grimes 	register struct mbuf *m;
258df8bae1dSRodney W. Grimes 	int iphlen;
259df8bae1dSRodney W. Grimes {
260df8bae1dSRodney W. Grimes 	register struct tcpiphdr *ti;
261df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
262e79adb8eSGarrett Wollman 	u_char *optp = NULL;
26326f9a767SRodney W. Grimes 	int optlen = 0;
264df8bae1dSRodney W. Grimes 	int len, tlen, off;
265df8bae1dSRodney W. Grimes 	register struct tcpcb *tp = 0;
266df8bae1dSRodney W. Grimes 	register int tiflags;
26726f9a767SRodney W. Grimes 	struct socket *so = 0;
268df8bae1dSRodney W. Grimes 	int todrop, acked, ourfinisacked, needoutput = 0;
269df8bae1dSRodney W. Grimes 	struct in_addr laddr;
270df8bae1dSRodney W. Grimes 	int dropsocket = 0;
271df8bae1dSRodney W. Grimes 	int iss = 0;
272a0292f23SGarrett Wollman 	u_long tiwin;
273a0292f23SGarrett Wollman 	struct tcpopt to;		/* options in this segment */
274a0292f23SGarrett Wollman 	struct rmxp_tao *taop;		/* pointer to our TAO cache entry */
275a0292f23SGarrett Wollman 	struct rmxp_tao	tao_noncached;	/* in case there's no cached entry */
276610ee2f9SDavid Greenman #ifdef TCPDEBUG
277610ee2f9SDavid Greenman 	short ostate = 0;
278610ee2f9SDavid Greenman #endif
279df8bae1dSRodney W. Grimes 
280a0292f23SGarrett Wollman 	bzero((char *)&to, sizeof(to));
281a0292f23SGarrett Wollman 
282df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvtotal++;
283df8bae1dSRodney W. Grimes 	/*
284df8bae1dSRodney W. Grimes 	 * Get IP and TCP header together in first mbuf.
285df8bae1dSRodney W. Grimes 	 * Note: IP leaves IP header in first mbuf.
286df8bae1dSRodney W. Grimes 	 */
287df8bae1dSRodney W. Grimes 	ti = mtod(m, struct tcpiphdr *);
288df8bae1dSRodney W. Grimes 	if (iphlen > sizeof (struct ip))
289df8bae1dSRodney W. Grimes 		ip_stripoptions(m, (struct mbuf *)0);
290df8bae1dSRodney W. Grimes 	if (m->m_len < sizeof (struct tcpiphdr)) {
291df8bae1dSRodney W. Grimes 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
292df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvshort++;
293df8bae1dSRodney W. Grimes 			return;
294df8bae1dSRodney W. Grimes 		}
295df8bae1dSRodney W. Grimes 		ti = mtod(m, struct tcpiphdr *);
296df8bae1dSRodney W. Grimes 	}
297df8bae1dSRodney W. Grimes 
298df8bae1dSRodney W. Grimes 	/*
299df8bae1dSRodney W. Grimes 	 * Checksum extended TCP header and data.
300df8bae1dSRodney W. Grimes 	 */
301df8bae1dSRodney W. Grimes 	tlen = ((struct ip *)ti)->ip_len;
302df8bae1dSRodney W. Grimes 	len = sizeof (struct ip) + tlen;
3036effc713SDoug Rabson 	bzero(ti->ti_x1, sizeof(ti->ti_x1));
304df8bae1dSRodney W. Grimes 	ti->ti_len = (u_short)tlen;
305df8bae1dSRodney W. Grimes 	HTONS(ti->ti_len);
306623ae52eSPoul-Henning Kamp 	ti->ti_sum = in_cksum(m, len);
307623ae52eSPoul-Henning Kamp 	if (ti->ti_sum) {
308df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvbadsum++;
309df8bae1dSRodney W. Grimes 		goto drop;
310df8bae1dSRodney W. Grimes 	}
311df8bae1dSRodney W. Grimes 
312df8bae1dSRodney W. Grimes 	/*
313df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
314df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
315df8bae1dSRodney W. Grimes 	 */
316df8bae1dSRodney W. Grimes 	off = ti->ti_off << 2;
317df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
318df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvbadoff++;
319df8bae1dSRodney W. Grimes 		goto drop;
320df8bae1dSRodney W. Grimes 	}
321df8bae1dSRodney W. Grimes 	tlen -= off;
322df8bae1dSRodney W. Grimes 	ti->ti_len = tlen;
323df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
324df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof(struct ip) + off) {
325df8bae1dSRodney W. Grimes 			if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
326df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvshort++;
327df8bae1dSRodney W. Grimes 				return;
328df8bae1dSRodney W. Grimes 			}
329df8bae1dSRodney W. Grimes 			ti = mtod(m, struct tcpiphdr *);
330df8bae1dSRodney W. Grimes 		}
331df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
332e79adb8eSGarrett Wollman 		optp = mtod(m, u_char *) + sizeof (struct tcpiphdr);
333df8bae1dSRodney W. Grimes 	}
334df8bae1dSRodney W. Grimes 	tiflags = ti->ti_flags;
335df8bae1dSRodney W. Grimes 
336df8bae1dSRodney W. Grimes 	/*
337df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
338df8bae1dSRodney W. Grimes 	 */
339df8bae1dSRodney W. Grimes 	NTOHL(ti->ti_seq);
340df8bae1dSRodney W. Grimes 	NTOHL(ti->ti_ack);
341df8bae1dSRodney W. Grimes 	NTOHS(ti->ti_win);
342df8bae1dSRodney W. Grimes 	NTOHS(ti->ti_urp);
343df8bae1dSRodney W. Grimes 
344df8bae1dSRodney W. Grimes 	/*
345755c1f07SAndras Olah 	 * Drop TCP, IP headers and TCP options.
346755c1f07SAndras Olah 	 */
347755c1f07SAndras Olah 	m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
348755c1f07SAndras Olah 	m->m_len  -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
349755c1f07SAndras Olah 
350755c1f07SAndras Olah 	/*
351df8bae1dSRodney W. Grimes 	 * Locate pcb for segment.
352df8bae1dSRodney W. Grimes 	 */
353df8bae1dSRodney W. Grimes findpcb:
354f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD
355f9e354dfSJulian Elischer 	if (ip_fw_fwd_addr != NULL) {
356f9e354dfSJulian Elischer 		/*
357f9e354dfSJulian Elischer 		 * Diverted. Pretend to be the destination.
358f9e354dfSJulian Elischer 		 * already got one like this?
359f9e354dfSJulian Elischer 		 */
360f9e354dfSJulian Elischer 		inp = in_pcblookup_hash(&tcbinfo, ti->ti_src, ti->ti_sport,
361f9e354dfSJulian Elischer 			ti->ti_dst, ti->ti_dport, 0);
362f9e354dfSJulian Elischer 		if (!inp) {
363f9e354dfSJulian Elischer 			/*
364f9e354dfSJulian Elischer 			 * No, then it's new. Try find the ambushing socket
365f9e354dfSJulian Elischer 			 */
366f9e354dfSJulian Elischer 			if (!ip_fw_fwd_addr->sin_port) {
367f9e354dfSJulian Elischer 				inp = in_pcblookup_hash(&tcbinfo, ti->ti_src,
368f9e354dfSJulian Elischer 				    ti->ti_sport, ip_fw_fwd_addr->sin_addr,
369f9e354dfSJulian Elischer 				    ti->ti_dport, 1);
370f9e354dfSJulian Elischer 			} else {
371f9e354dfSJulian Elischer 				inp = in_pcblookup_hash(&tcbinfo,
372f9e354dfSJulian Elischer 				    ti->ti_src, ti->ti_sport,
373f9e354dfSJulian Elischer 	    			    ip_fw_fwd_addr->sin_addr,
374f9e354dfSJulian Elischer 				    ntohs(ip_fw_fwd_addr->sin_port), 1);
375f9e354dfSJulian Elischer 			}
376f9e354dfSJulian Elischer 		}
377f9e354dfSJulian Elischer 		ip_fw_fwd_addr = NULL;
378f9e354dfSJulian Elischer 	} else
379f9e354dfSJulian Elischer #endif	/* IPFIREWALL_FORWARD */
380f9e354dfSJulian Elischer 
381c3229e05SDavid Greenman 	inp = in_pcblookup_hash(&tcbinfo, ti->ti_src, ti->ti_sport,
3826d6a026bSDavid Greenman 	    ti->ti_dst, ti->ti_dport, 1);
383df8bae1dSRodney W. Grimes 
384df8bae1dSRodney W. Grimes 	/*
385df8bae1dSRodney W. Grimes 	 * If the state is CLOSED (i.e., TCB does not exist) then
386df8bae1dSRodney W. Grimes 	 * all data in the incoming segment is discarded.
387df8bae1dSRodney W. Grimes 	 * If the TCB exists but is in CLOSED state, it is embryonic,
388df8bae1dSRodney W. Grimes 	 * but should either do a listen or a connect soon.
389df8bae1dSRodney W. Grimes 	 */
390816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
39175cfc95fSAndrey A. Chernov 		if (log_in_vain && tiflags & TH_SYN) {
392df5c0b8aSBill Fenner 			char buf[4*sizeof "123"];
39375cfc95fSAndrey A. Chernov 
39475cfc95fSAndrey A. Chernov 			strcpy(buf, inet_ntoa(ti->ti_dst));
395592071e8SBruce Evans 			log(LOG_INFO,
396592071e8SBruce Evans 			    "Connection attempt to TCP %s:%d from %s:%d\n",
397592071e8SBruce Evans 			    buf, ntohs(ti->ti_dport), inet_ntoa(ti->ti_src),
398592071e8SBruce Evans 			    ntohs(ti->ti_sport));
39975cfc95fSAndrey A. Chernov 		}
400df8bae1dSRodney W. Grimes 		goto dropwithreset;
401816a3d83SPoul-Henning Kamp 	}
402df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
403df8bae1dSRodney W. Grimes 	if (tp == 0)
404df8bae1dSRodney W. Grimes 		goto dropwithreset;
405df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_CLOSED)
406df8bae1dSRodney W. Grimes 		goto drop;
407df8bae1dSRodney W. Grimes 
408df8bae1dSRodney W. Grimes 	/* Unscale the window into a 32-bit value. */
409df8bae1dSRodney W. Grimes 	if ((tiflags & TH_SYN) == 0)
410df8bae1dSRodney W. Grimes 		tiwin = ti->ti_win << tp->snd_scale;
411df8bae1dSRodney W. Grimes 	else
412df8bae1dSRodney W. Grimes 		tiwin = ti->ti_win;
413df8bae1dSRodney W. Grimes 
414df8bae1dSRodney W. Grimes 	so = inp->inp_socket;
415df8bae1dSRodney W. Grimes 	if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
416610ee2f9SDavid Greenman #ifdef TCPDEBUG
417df8bae1dSRodney W. Grimes 		if (so->so_options & SO_DEBUG) {
418df8bae1dSRodney W. Grimes 			ostate = tp->t_state;
419df8bae1dSRodney W. Grimes 			tcp_saveti = *ti;
420df8bae1dSRodney W. Grimes 		}
421610ee2f9SDavid Greenman #endif
422df8bae1dSRodney W. Grimes 		if (so->so_options & SO_ACCEPTCONN) {
423a0292f23SGarrett Wollman 			register struct tcpcb *tp0 = tp;
4244195b4afSPaul Traina 			struct socket *so2;
425ebb0cbeaSPaul Traina 			if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
4264195b4afSPaul Traina 				/*
427ebb0cbeaSPaul Traina 				 * Note: dropwithreset makes sure we don't
428ebb0cbeaSPaul Traina 				 * send a RST in response to a RST.
4294195b4afSPaul Traina 				 */
430ebb0cbeaSPaul Traina 				if (tiflags & TH_ACK) {
431ebb0cbeaSPaul Traina 					tcpstat.tcps_badsyn++;
432ebb0cbeaSPaul Traina 					goto dropwithreset;
4334195b4afSPaul Traina 				}
434ebb0cbeaSPaul Traina 				goto drop;
435ebb0cbeaSPaul Traina 			}
436ebb0cbeaSPaul Traina 			so2 = sonewconn(so, 0);
437ebb0cbeaSPaul Traina 			if (so2 == 0) {
438ebb0cbeaSPaul Traina 				tcpstat.tcps_listendrop++;
439ebb0cbeaSPaul Traina 				so2 = sodropablereq(so);
440a51764a8SPaul Traina 				if (so2) {
441ebb0cbeaSPaul Traina 					tcp_drop(sototcpcb(so2), ETIMEDOUT);
442a51764a8SPaul Traina 					so2 = sonewconn(so, 0);
443a51764a8SPaul Traina 				}
444a51764a8SPaul Traina 				if (!so2)
445df8bae1dSRodney W. Grimes 					goto drop;
4461347f5b8SGuido van Rooij 			}
4474195b4afSPaul Traina 			so = so2;
448df8bae1dSRodney W. Grimes 			/*
449df8bae1dSRodney W. Grimes 			 * This is ugly, but ....
450df8bae1dSRodney W. Grimes 			 *
451df8bae1dSRodney W. Grimes 			 * Mark socket as temporary until we're
452df8bae1dSRodney W. Grimes 			 * committed to keeping it.  The code at
453df8bae1dSRodney W. Grimes 			 * ``drop'' and ``dropwithreset'' check the
454df8bae1dSRodney W. Grimes 			 * flag dropsocket to see if the temporary
455df8bae1dSRodney W. Grimes 			 * socket created here should be discarded.
456df8bae1dSRodney W. Grimes 			 * We mark the socket as discardable until
457df8bae1dSRodney W. Grimes 			 * we're committed to it below in TCPS_LISTEN.
458df8bae1dSRodney W. Grimes 			 */
459df8bae1dSRodney W. Grimes 			dropsocket++;
460df8bae1dSRodney W. Grimes 			inp = (struct inpcb *)so->so_pcb;
461df8bae1dSRodney W. Grimes 			inp->inp_laddr = ti->ti_dst;
462df8bae1dSRodney W. Grimes 			inp->inp_lport = ti->ti_dport;
463c3229e05SDavid Greenman 			if (in_pcbinshash(inp) != 0) {
464c3229e05SDavid Greenman 				/*
465c3229e05SDavid Greenman 				 * Undo the assignments above if we failed to put
466c3229e05SDavid Greenman 				 * the PCB on the hash lists.
467c3229e05SDavid Greenman 				 */
468c3229e05SDavid Greenman 				inp->inp_laddr.s_addr = INADDR_ANY;
469c3229e05SDavid Greenman 				inp->inp_lport = 0;
470c3229e05SDavid Greenman 				goto drop;
471c3229e05SDavid Greenman 			}
472df8bae1dSRodney W. Grimes 			inp->inp_options = ip_srcroute();
473df8bae1dSRodney W. Grimes 			tp = intotcpcb(inp);
474df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_LISTEN;
475a0292f23SGarrett Wollman 			tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT);
476df8bae1dSRodney W. Grimes 
477a0292f23SGarrett Wollman 			/* Compute proper scaling value from buffer space */
478df8bae1dSRodney W. Grimes 			while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
479df8bae1dSRodney W. Grimes 			   TCP_MAXWIN << tp->request_r_scale < so->so_rcv.sb_hiwat)
480df8bae1dSRodney W. Grimes 				tp->request_r_scale++;
481df8bae1dSRodney W. Grimes 		}
482df8bae1dSRodney W. Grimes 	}
483df8bae1dSRodney W. Grimes 
484df8bae1dSRodney W. Grimes 	/*
485df8bae1dSRodney W. Grimes 	 * Segment received on connection.
486df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
487df8bae1dSRodney W. Grimes 	 */
488df8bae1dSRodney W. Grimes 	tp->t_idle = 0;
4897ff19458SPaul Traina 	if (TCPS_HAVEESTABLISHED(tp->t_state))
490df8bae1dSRodney W. Grimes 		tp->t_timer[TCPT_KEEP] = tcp_keepidle;
491df8bae1dSRodney W. Grimes 
492df8bae1dSRodney W. Grimes 	/*
493df8bae1dSRodney W. Grimes 	 * Process options if not in LISTEN state,
494df8bae1dSRodney W. Grimes 	 * else do it below (after getting remote address).
495df8bae1dSRodney W. Grimes 	 */
496f9d5a964SDavid Greenman 	if (tp->t_state != TCPS_LISTEN)
497f9d5a964SDavid Greenman 		tcp_dooptions(tp, optp, optlen, ti, &to);
498df8bae1dSRodney W. Grimes 
499df8bae1dSRodney W. Grimes 	/*
500df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
501df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
502df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
503df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
504df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
505df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
506df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
507df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
508df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
509df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
510df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
511df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
512a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
513a0292f23SGarrett Wollman 	 * Since we check for TCPS_ESTABLISHED above, it can only
514a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
515df8bae1dSRodney W. Grimes 	 */
516df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
517df8bae1dSRodney W. Grimes 	    (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
518a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
519a0292f23SGarrett Wollman 	    ((to.to_flag & TOF_TS) == 0 ||
520a0292f23SGarrett Wollman 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
521a0292f23SGarrett Wollman 	    /*
522a0292f23SGarrett Wollman 	     * Using the CC option is compulsory if once started:
523a0292f23SGarrett Wollman 	     *   the segment is OK if no T/TCP was negotiated or
524a0292f23SGarrett Wollman 	     *   if the segment has a CC option equal to CCrecv
525a0292f23SGarrett Wollman 	     */
526a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) ||
527a0292f23SGarrett Wollman 	     (to.to_flag & TOF_CC) != 0 && to.to_cc == tp->cc_recv) &&
528df8bae1dSRodney W. Grimes 	    ti->ti_seq == tp->rcv_nxt &&
529df8bae1dSRodney W. Grimes 	    tiwin && tiwin == tp->snd_wnd &&
530df8bae1dSRodney W. Grimes 	    tp->snd_nxt == tp->snd_max) {
531df8bae1dSRodney W. Grimes 
532df8bae1dSRodney W. Grimes 		/*
533df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
534df8bae1dSRodney W. Grimes 		 * record the timestamp.
535a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
536a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
537df8bae1dSRodney W. Grimes 		 */
538a0292f23SGarrett Wollman 		if ((to.to_flag & TOF_TS) != 0 &&
539a0292f23SGarrett Wollman 		   SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) {
540a0292f23SGarrett Wollman 			tp->ts_recent_age = tcp_now;
541a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
542df8bae1dSRodney W. Grimes 		}
543df8bae1dSRodney W. Grimes 
544df8bae1dSRodney W. Grimes 		if (ti->ti_len == 0) {
545df8bae1dSRodney W. Grimes 			if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
546df8bae1dSRodney W. Grimes 			    SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
547233e8c18SGarrett Wollman 			    tp->snd_cwnd >= tp->snd_wnd &&
548233e8c18SGarrett Wollman 			    tp->t_dupacks < tcprexmtthresh) {
549df8bae1dSRodney W. Grimes 				/*
550df8bae1dSRodney W. Grimes 				 * this is a pure ack for outstanding data.
551df8bae1dSRodney W. Grimes 				 */
552df8bae1dSRodney W. Grimes 				++tcpstat.tcps_predack;
553a0292f23SGarrett Wollman 				if ((to.to_flag & TOF_TS) != 0)
554a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
555a0292f23SGarrett Wollman 					    tcp_now - to.to_tsecr + 1);
556df8bae1dSRodney W. Grimes 				else if (tp->t_rtt &&
557df8bae1dSRodney W. Grimes 					    SEQ_GT(ti->ti_ack, tp->t_rtseq))
558df8bae1dSRodney W. Grimes 					tcp_xmit_timer(tp, tp->t_rtt);
559df8bae1dSRodney W. Grimes 				acked = ti->ti_ack - tp->snd_una;
560df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackpack++;
561df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackbyte += acked;
562df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
563df8bae1dSRodney W. Grimes 				tp->snd_una = ti->ti_ack;
564df8bae1dSRodney W. Grimes 				m_freem(m);
565df8bae1dSRodney W. Grimes 
566df8bae1dSRodney W. Grimes 				/*
567df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
568df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
569df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
570df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
571df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
572df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
573df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
574df8bae1dSRodney W. Grimes 				 */
575df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
576df8bae1dSRodney W. Grimes 					tp->t_timer[TCPT_REXMT] = 0;
577df8bae1dSRodney W. Grimes 				else if (tp->t_timer[TCPT_PERSIST] == 0)
578df8bae1dSRodney W. Grimes 					tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
579df8bae1dSRodney W. Grimes 
580df8bae1dSRodney W. Grimes 				sowwakeup(so);
581df8bae1dSRodney W. Grimes 				if (so->so_snd.sb_cc)
582df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
583df8bae1dSRodney W. Grimes 				return;
584df8bae1dSRodney W. Grimes 			}
585df8bae1dSRodney W. Grimes 		} else if (ti->ti_ack == tp->snd_una &&
5866effc713SDoug Rabson 		    tp->t_segq == NULL &&
587df8bae1dSRodney W. Grimes 		    ti->ti_len <= sbspace(&so->so_rcv)) {
588df8bae1dSRodney W. Grimes 			/*
589df8bae1dSRodney W. Grimes 			 * this is a pure, in-sequence data packet
590df8bae1dSRodney W. Grimes 			 * with nothing on the reassembly queue and
591df8bae1dSRodney W. Grimes 			 * we have enough buffer space to take it.
592df8bae1dSRodney W. Grimes 			 */
593df8bae1dSRodney W. Grimes 			++tcpstat.tcps_preddat;
594df8bae1dSRodney W. Grimes 			tp->rcv_nxt += ti->ti_len;
595df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpack++;
596df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyte += ti->ti_len;
597df8bae1dSRodney W. Grimes 			/*
598755c1f07SAndras Olah 			 * Add data to socket buffer.
599df8bae1dSRodney W. Grimes 			 */
600df8bae1dSRodney W. Grimes 			sbappend(&so->so_rcv, m);
601df8bae1dSRodney W. Grimes 			sorwakeup(so);
602f498eeeeSDavid Greenman 			if (tcp_delack_enabled) {
603f498eeeeSDavid Greenman 				tp->t_flags |= TF_DELACK;
604f498eeeeSDavid Greenman 			} else {
605e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
606e612a582SDavid Greenman 				tcp_output(tp);
607e612a582SDavid Greenman 			}
608df8bae1dSRodney W. Grimes 			return;
609df8bae1dSRodney W. Grimes 		}
610df8bae1dSRodney W. Grimes 	}
611df8bae1dSRodney W. Grimes 
612df8bae1dSRodney W. Grimes 	/*
613df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
614df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
615df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
616df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
617df8bae1dSRodney W. Grimes 	 */
618df8bae1dSRodney W. Grimes 	{ int win;
619df8bae1dSRodney W. Grimes 
620df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
621df8bae1dSRodney W. Grimes 	if (win < 0)
622df8bae1dSRodney W. Grimes 		win = 0;
62366e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
624df8bae1dSRodney W. Grimes 	}
625df8bae1dSRodney W. Grimes 
626df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
627df8bae1dSRodney W. Grimes 
628df8bae1dSRodney W. Grimes 	/*
629df8bae1dSRodney W. Grimes 	 * If the state is LISTEN then ignore segment if it contains an RST.
630df8bae1dSRodney W. Grimes 	 * If the segment contains an ACK then it is bad and send a RST.
631df8bae1dSRodney W. Grimes 	 * If it does not contain a SYN then it is not interesting; drop it.
632764d8cefSBill Fenner 	 * If it is from this socket, drop it, it must be forged.
633df8bae1dSRodney W. Grimes 	 * Don't bother responding if the destination was a broadcast.
634df8bae1dSRodney W. Grimes 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
635df8bae1dSRodney W. Grimes 	 * tp->iss, and send a segment:
636df8bae1dSRodney W. Grimes 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
637df8bae1dSRodney W. Grimes 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
638df8bae1dSRodney W. Grimes 	 * Fill in remote peer address fields if not previously specified.
639df8bae1dSRodney W. Grimes 	 * Enter SYN_RECEIVED state, and process any other fields of this
640df8bae1dSRodney W. Grimes 	 * segment in this state.
641df8bae1dSRodney W. Grimes 	 */
642df8bae1dSRodney W. Grimes 	case TCPS_LISTEN: {
643df8bae1dSRodney W. Grimes 		register struct sockaddr_in *sin;
644df8bae1dSRodney W. Grimes 
645df8bae1dSRodney W. Grimes 		if (tiflags & TH_RST)
646df8bae1dSRodney W. Grimes 			goto drop;
647df8bae1dSRodney W. Grimes 		if (tiflags & TH_ACK)
648df8bae1dSRodney W. Grimes 			goto dropwithreset;
649df8bae1dSRodney W. Grimes 		if ((tiflags & TH_SYN) == 0)
650df8bae1dSRodney W. Grimes 			goto drop;
651764d8cefSBill Fenner 		if ((ti->ti_dport == ti->ti_sport) &&
652764d8cefSBill Fenner 		    (ti->ti_dst.s_addr == ti->ti_src.s_addr))
653764d8cefSBill Fenner 			goto drop;
654df8bae1dSRodney W. Grimes 		/*
655df8bae1dSRodney W. Grimes 		 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
656df8bae1dSRodney W. Grimes 		 * in_broadcast() should never return true on a received
657df8bae1dSRodney W. Grimes 		 * packet with M_BCAST not set.
658df8bae1dSRodney W. Grimes 		 */
659df8bae1dSRodney W. Grimes 		if (m->m_flags & (M_BCAST|M_MCAST) ||
660d4d0967eSDavid Greenman 		    IN_MULTICAST(ntohl(ti->ti_dst.s_addr)))
661df8bae1dSRodney W. Grimes 			goto drop;
66257bf258eSGarrett Wollman 		MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
66357bf258eSGarrett Wollman 		       M_NOWAIT);
66457bf258eSGarrett Wollman 		if (sin == NULL)
665df8bae1dSRodney W. Grimes 			goto drop;
666df8bae1dSRodney W. Grimes 		sin->sin_family = AF_INET;
667df8bae1dSRodney W. Grimes 		sin->sin_len = sizeof(*sin);
668df8bae1dSRodney W. Grimes 		sin->sin_addr = ti->ti_src;
669df8bae1dSRodney W. Grimes 		sin->sin_port = ti->ti_sport;
670df8bae1dSRodney W. Grimes 		bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero));
671df8bae1dSRodney W. Grimes 		laddr = inp->inp_laddr;
672df8bae1dSRodney W. Grimes 		if (inp->inp_laddr.s_addr == INADDR_ANY)
673df8bae1dSRodney W. Grimes 			inp->inp_laddr = ti->ti_dst;
67457bf258eSGarrett Wollman 		if (in_pcbconnect(inp, (struct sockaddr *)sin, &proc0)) {
675df8bae1dSRodney W. Grimes 			inp->inp_laddr = laddr;
67657bf258eSGarrett Wollman 			FREE(sin, M_SONAME);
677df8bae1dSRodney W. Grimes 			goto drop;
678df8bae1dSRodney W. Grimes 		}
67957bf258eSGarrett Wollman 		FREE(sin, M_SONAME);
680df8bae1dSRodney W. Grimes 		tp->t_template = tcp_template(tp);
681df8bae1dSRodney W. Grimes 		if (tp->t_template == 0) {
682df8bae1dSRodney W. Grimes 			tp = tcp_drop(tp, ENOBUFS);
683df8bae1dSRodney W. Grimes 			dropsocket = 0;		/* socket is already gone */
684df8bae1dSRodney W. Grimes 			goto drop;
685df8bae1dSRodney W. Grimes 		}
686a0292f23SGarrett Wollman 		if ((taop = tcp_gettaocache(inp)) == NULL) {
687a0292f23SGarrett Wollman 			taop = &tao_noncached;
688a0292f23SGarrett Wollman 			bzero(taop, sizeof(*taop));
689a0292f23SGarrett Wollman 		}
690f9d5a964SDavid Greenman 		tcp_dooptions(tp, optp, optlen, ti, &to);
691df8bae1dSRodney W. Grimes 		if (iss)
692df8bae1dSRodney W. Grimes 			tp->iss = iss;
693df8bae1dSRodney W. Grimes 		else
694df8bae1dSRodney W. Grimes 			tp->iss = tcp_iss;
695e79adb8eSGarrett Wollman 		tcp_iss += TCP_ISSINCR/4;
696df8bae1dSRodney W. Grimes 		tp->irs = ti->ti_seq;
697df8bae1dSRodney W. Grimes 		tcp_sendseqinit(tp);
698df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
699a0292f23SGarrett Wollman 		/*
700a0292f23SGarrett Wollman 		 * Initialization of the tcpcb for transaction;
701a0292f23SGarrett Wollman 		 *   set SND.WND = SEG.WND,
702a0292f23SGarrett Wollman 		 *   initialize CCsend and CCrecv.
703a0292f23SGarrett Wollman 		 */
704a0292f23SGarrett Wollman 		tp->snd_wnd = tiwin;	/* initial send-window */
705a0292f23SGarrett Wollman 		tp->cc_send = CC_INC(tcp_ccgen);
706a0292f23SGarrett Wollman 		tp->cc_recv = to.to_cc;
707a0292f23SGarrett Wollman 		/*
708a0292f23SGarrett Wollman 		 * Perform TAO test on incoming CC (SEG.CC) option, if any.
709a0292f23SGarrett Wollman 		 * - compare SEG.CC against cached CC from the same host,
710a0292f23SGarrett Wollman 		 *	if any.
711a0292f23SGarrett Wollman 		 * - if SEG.CC > chached value, SYN must be new and is accepted
712a0292f23SGarrett Wollman 		 *	immediately: save new CC in the cache, mark the socket
713a0292f23SGarrett Wollman 		 *	connected, enter ESTABLISHED state, turn on flag to
714a0292f23SGarrett Wollman 		 *	send a SYN in the next segment.
715a0292f23SGarrett Wollman 		 *	A virtual advertised window is set in rcv_adv to
716a0292f23SGarrett Wollman 		 *	initialize SWS prevention.  Then enter normal segment
717a0292f23SGarrett Wollman 		 *	processing: drop SYN, process data and FIN.
718a0292f23SGarrett Wollman 		 * - otherwise do a normal 3-way handshake.
719a0292f23SGarrett Wollman 		 */
720a0292f23SGarrett Wollman 		if ((to.to_flag & TOF_CC) != 0) {
721068373b6SGuido van Rooij 		    if (((tp->t_flags & TF_NOPUSH) != 0) &&
72211ad4550SGuido van Rooij 			taop->tao_cc != 0 && CC_GT(to.to_cc, taop->tao_cc)) {
72311ad4550SGuido van Rooij 
724a0292f23SGarrett Wollman 			taop->tao_cc = to.to_cc;
725a0292f23SGarrett Wollman 			tp->t_state = TCPS_ESTABLISHED;
726a0292f23SGarrett Wollman 
727a0292f23SGarrett Wollman 			/*
728a0292f23SGarrett Wollman 			 * If there is a FIN, or if there is data and the
729a0292f23SGarrett Wollman 			 * connection is local, then delay SYN,ACK(SYN) in
730a0292f23SGarrett Wollman 			 * the hope of piggy-backing it on a response
731a0292f23SGarrett Wollman 			 * segment.  Otherwise must send ACK now in case
732a0292f23SGarrett Wollman 			 * the other side is slow starting.
733a0292f23SGarrett Wollman 			 */
734f498eeeeSDavid Greenman 			if (tcp_delack_enabled && ((tiflags & TH_FIN) || (ti->ti_len != 0 &&
735f498eeeeSDavid Greenman 			    in_localaddr(inp->inp_faddr))))
736a0292f23SGarrett Wollman 				tp->t_flags |= (TF_DELACK | TF_NEEDSYN);
737a0292f23SGarrett Wollman 			else
738a0292f23SGarrett Wollman 				tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
73907e43e10SAndras Olah 
74007e43e10SAndras Olah 			/*
74107e43e10SAndras Olah 			 * Limit the `virtual advertised window' to TCP_MAXWIN
74207e43e10SAndras Olah 			 * here.  Even if we requested window scaling, it will
74307e43e10SAndras Olah 			 * become effective only later when our SYN is acked.
74407e43e10SAndras Olah 			 */
74507e43e10SAndras Olah 			tp->rcv_adv += min(tp->rcv_wnd, TCP_MAXWIN);
746a0292f23SGarrett Wollman 			tcpstat.tcps_connects++;
747a0292f23SGarrett Wollman 			soisconnected(so);
7487b40aa32SPaul Traina 			tp->t_timer[TCPT_KEEP] = tcp_keepinit;
749a0292f23SGarrett Wollman 			dropsocket = 0;		/* committed to socket */
750a0292f23SGarrett Wollman 			tcpstat.tcps_accepts++;
751a0292f23SGarrett Wollman 			goto trimthenstep6;
752a0292f23SGarrett Wollman 		    }
753a0292f23SGarrett Wollman 		/* else do standard 3-way handshake */
754a0292f23SGarrett Wollman 		} else {
755a0292f23SGarrett Wollman 		    /*
756a0292f23SGarrett Wollman 		     * No CC option, but maybe CC.NEW:
757a0292f23SGarrett Wollman 		     *   invalidate cached value.
758a0292f23SGarrett Wollman 		     */
759a0292f23SGarrett Wollman 		     taop->tao_cc = 0;
760a0292f23SGarrett Wollman 		}
761a0292f23SGarrett Wollman 		/*
762a0292f23SGarrett Wollman 		 * TAO test failed or there was no CC option,
763a0292f23SGarrett Wollman 		 *    do a standard 3-way handshake.
764a0292f23SGarrett Wollman 		 */
765df8bae1dSRodney W. Grimes 		tp->t_flags |= TF_ACKNOW;
766df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_SYN_RECEIVED;
7677b40aa32SPaul Traina 		tp->t_timer[TCPT_KEEP] = tcp_keepinit;
768df8bae1dSRodney W. Grimes 		dropsocket = 0;		/* committed to socket */
769df8bae1dSRodney W. Grimes 		tcpstat.tcps_accepts++;
770df8bae1dSRodney W. Grimes 		goto trimthenstep6;
771df8bae1dSRodney W. Grimes 		}
772df8bae1dSRodney W. Grimes 
773df8bae1dSRodney W. Grimes 	/*
774764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
775764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
776764d8cefSBill Fenner 	 */
777764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
77875daa6a5SBill Fenner 		if ((tiflags & TH_ACK) &&
77975daa6a5SBill Fenner 		    (SEQ_LEQ(ti->ti_ack, tp->snd_una) ||
78075daa6a5SBill Fenner 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
781764d8cefSBill Fenner 				goto dropwithreset;
782764d8cefSBill Fenner 		break;
783764d8cefSBill Fenner 
784764d8cefSBill Fenner 	/*
785df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
786df8bae1dSRodney W. Grimes 	 *	if seg contains an ACK, but not for our SYN, drop the input.
787df8bae1dSRodney W. Grimes 	 *	if seg contains a RST, then drop the connection.
788df8bae1dSRodney W. Grimes 	 *	if seg does not contain SYN, then drop it.
789df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
790df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
791df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
792df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
793df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
794df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
795df8bae1dSRodney W. Grimes 	 */
796df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
797a0292f23SGarrett Wollman 		if ((taop = tcp_gettaocache(inp)) == NULL) {
798a0292f23SGarrett Wollman 			taop = &tao_noncached;
799a0292f23SGarrett Wollman 			bzero(taop, sizeof(*taop));
800a0292f23SGarrett Wollman 		}
801a0292f23SGarrett Wollman 
802a0292f23SGarrett Wollman 		if ((tiflags & TH_ACK) &&
803a0292f23SGarrett Wollman 		    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
804a0292f23SGarrett Wollman 		     SEQ_GT(ti->ti_ack, tp->snd_max))) {
805a0292f23SGarrett Wollman 			/*
806a0292f23SGarrett Wollman 			 * If we have a cached CCsent for the remote host,
807a0292f23SGarrett Wollman 			 * hence we haven't just crashed and restarted,
808a0292f23SGarrett Wollman 			 * do not send a RST.  This may be a retransmission
809a0292f23SGarrett Wollman 			 * from the other side after our earlier ACK was lost.
810a0292f23SGarrett Wollman 			 * Our new SYN, when it arrives, will serve as the
811a0292f23SGarrett Wollman 			 * needed ACK.
812a0292f23SGarrett Wollman 			 */
813a0292f23SGarrett Wollman 			if (taop->tao_ccsent != 0)
814a0292f23SGarrett Wollman 				goto drop;
815a0292f23SGarrett Wollman 			else
816a0292f23SGarrett Wollman 				goto dropwithreset;
817a0292f23SGarrett Wollman 		}
818df8bae1dSRodney W. Grimes 		if (tiflags & TH_RST) {
819df8bae1dSRodney W. Grimes 			if (tiflags & TH_ACK)
820df8bae1dSRodney W. Grimes 				tp = tcp_drop(tp, ECONNREFUSED);
821df8bae1dSRodney W. Grimes 			goto drop;
822df8bae1dSRodney W. Grimes 		}
823df8bae1dSRodney W. Grimes 		if ((tiflags & TH_SYN) == 0)
824df8bae1dSRodney W. Grimes 			goto drop;
825a0292f23SGarrett Wollman 		tp->snd_wnd = ti->ti_win;	/* initial send window */
826a0292f23SGarrett Wollman 		tp->cc_recv = to.to_cc;		/* foreign CC */
827a0292f23SGarrett Wollman 
828df8bae1dSRodney W. Grimes 		tp->irs = ti->ti_seq;
829df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
830845799c1SAndras Olah 		if (tiflags & TH_ACK) {
831a0292f23SGarrett Wollman 			/*
832a0292f23SGarrett Wollman 			 * Our SYN was acked.  If segment contains CC.ECHO
833a0292f23SGarrett Wollman 			 * option, check it to make sure this segment really
834a0292f23SGarrett Wollman 			 * matches our SYN.  If not, just drop it as old
835a0292f23SGarrett Wollman 			 * duplicate, but send an RST if we're still playing
836026650e5SBill Fenner 			 * by the old rules.  If no CC.ECHO option, make sure
837026650e5SBill Fenner 			 * we don't get fooled into using T/TCP.
838a0292f23SGarrett Wollman 			 */
839026650e5SBill Fenner 			if (to.to_flag & TOF_CCECHO) {
840026650e5SBill Fenner 				if (tp->cc_send != to.to_ccecho)
841a0292f23SGarrett Wollman 					if (taop->tao_ccsent != 0)
842a0292f23SGarrett Wollman 						goto drop;
843a0292f23SGarrett Wollman 					else
844a0292f23SGarrett Wollman 						goto dropwithreset;
845026650e5SBill Fenner 			} else
846026650e5SBill Fenner 				tp->t_flags &= ~TF_RCVD_CC;
847845799c1SAndras Olah 			tcpstat.tcps_connects++;
848845799c1SAndras Olah 			soisconnected(so);
849845799c1SAndras Olah 			/* Do window scaling on this connection? */
850845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
851845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
852845799c1SAndras Olah 				tp->snd_scale = tp->requested_s_scale;
853845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
854845799c1SAndras Olah 			}
855a0292f23SGarrett Wollman 			/* Segment is acceptable, update cache if undefined. */
856a0292f23SGarrett Wollman 			if (taop->tao_ccsent == 0)
857a0292f23SGarrett Wollman 				taop->tao_ccsent = to.to_ccecho;
858a0292f23SGarrett Wollman 
859a0292f23SGarrett Wollman 			tp->rcv_adv += tp->rcv_wnd;
860a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
861a0292f23SGarrett Wollman 			/*
862a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
863a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
864a0292f23SGarrett Wollman 			 */
865f498eeeeSDavid Greenman 			if (tcp_delack_enabled && ti->ti_len != 0)
866a0292f23SGarrett Wollman 				tp->t_flags |= TF_DELACK;
867a0292f23SGarrett Wollman 			else
868a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
869a0292f23SGarrett Wollman 			/*
870a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
871a0292f23SGarrett Wollman 			 * Transitions:
872a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
873a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
874a0292f23SGarrett Wollman 			 */
875a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
876a0292f23SGarrett Wollman 				tp->t_state = TCPS_FIN_WAIT_1;
877a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
878a0292f23SGarrett Wollman 				tiflags &= ~TH_SYN;
8797ff19458SPaul Traina 			} else {
880a0292f23SGarrett Wollman 				tp->t_state = TCPS_ESTABLISHED;
8817ff19458SPaul Traina 				tp->t_timer[TCPT_KEEP] = tcp_keepidle;
8827ff19458SPaul Traina 			}
883a0292f23SGarrett Wollman 		} else {
884a0292f23SGarrett Wollman 		/*
885a0292f23SGarrett Wollman 		 *  Received initial SYN in SYN-SENT[*] state => simul-
886a0292f23SGarrett Wollman 		 *  taneous open.  If segment contains CC option and there is
887a0292f23SGarrett Wollman 		 *  a cached CC, apply TAO test; if it succeeds, connection is
888a0292f23SGarrett Wollman 		 *  half-synchronized.  Otherwise, do 3-way handshake:
889a0292f23SGarrett Wollman 		 *        SYN-SENT -> SYN-RECEIVED
890a0292f23SGarrett Wollman 		 *        SYN-SENT* -> SYN-RECEIVED*
891a0292f23SGarrett Wollman 		 *  If there was no CC option, clear cached CC value.
892a0292f23SGarrett Wollman 		 */
893a0292f23SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
894a0292f23SGarrett Wollman 			tp->t_timer[TCPT_REXMT] = 0;
895a0292f23SGarrett Wollman 			if (to.to_flag & TOF_CC) {
896a0292f23SGarrett Wollman 				if (taop->tao_cc != 0 &&
897a0292f23SGarrett Wollman 				    CC_GT(to.to_cc, taop->tao_cc)) {
898a0292f23SGarrett Wollman 					/*
899a0292f23SGarrett Wollman 					 * update cache and make transition:
900a0292f23SGarrett Wollman 					 *        SYN-SENT -> ESTABLISHED*
901a0292f23SGarrett Wollman 					 *        SYN-SENT* -> FIN-WAIT-1*
902a0292f23SGarrett Wollman 					 */
903a0292f23SGarrett Wollman 					taop->tao_cc = to.to_cc;
904a0292f23SGarrett Wollman 					if (tp->t_flags & TF_NEEDFIN) {
905a0292f23SGarrett Wollman 						tp->t_state = TCPS_FIN_WAIT_1;
906a0292f23SGarrett Wollman 						tp->t_flags &= ~TF_NEEDFIN;
9077ff19458SPaul Traina 					} else {
908a0292f23SGarrett Wollman 						tp->t_state = TCPS_ESTABLISHED;
9097ff19458SPaul Traina 						tp->t_timer[TCPT_KEEP] = tcp_keepidle;
9107ff19458SPaul Traina 					}
911a0292f23SGarrett Wollman 					tp->t_flags |= TF_NEEDSYN;
912df8bae1dSRodney W. Grimes 				} else
913df8bae1dSRodney W. Grimes 					tp->t_state = TCPS_SYN_RECEIVED;
914a0292f23SGarrett Wollman 			} else {
915a0292f23SGarrett Wollman 				/* CC.NEW or no option => invalidate cache */
916a0292f23SGarrett Wollman 				taop->tao_cc = 0;
917a0292f23SGarrett Wollman 				tp->t_state = TCPS_SYN_RECEIVED;
918a0292f23SGarrett Wollman 			}
919a0292f23SGarrett Wollman 		}
920df8bae1dSRodney W. Grimes 
921df8bae1dSRodney W. Grimes trimthenstep6:
922df8bae1dSRodney W. Grimes 		/*
923df8bae1dSRodney W. Grimes 		 * Advance ti->ti_seq to correspond to first data byte.
924df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
925df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
926df8bae1dSRodney W. Grimes 		 */
927df8bae1dSRodney W. Grimes 		ti->ti_seq++;
928df8bae1dSRodney W. Grimes 		if (ti->ti_len > tp->rcv_wnd) {
929df8bae1dSRodney W. Grimes 			todrop = ti->ti_len - tp->rcv_wnd;
930df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
931df8bae1dSRodney W. Grimes 			ti->ti_len = tp->rcv_wnd;
932df8bae1dSRodney W. Grimes 			tiflags &= ~TH_FIN;
933df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpackafterwin++;
934df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
935df8bae1dSRodney W. Grimes 		}
936df8bae1dSRodney W. Grimes 		tp->snd_wl1 = ti->ti_seq - 1;
937df8bae1dSRodney W. Grimes 		tp->rcv_up = ti->ti_seq;
938a0292f23SGarrett Wollman 		/*
939a0292f23SGarrett Wollman 		 *  Client side of transaction: already sent SYN and data.
940a0292f23SGarrett Wollman 		 *  If the remote host used T/TCP to validate the SYN,
941a0292f23SGarrett Wollman 		 *  our data will be ACK'd; if so, enter normal data segment
942a0292f23SGarrett Wollman 		 *  processing in the middle of step 5, ack processing.
943a0292f23SGarrett Wollman 		 *  Otherwise, goto step 6.
944a0292f23SGarrett Wollman 		 */
945a0292f23SGarrett Wollman  		if (tiflags & TH_ACK)
946a0292f23SGarrett Wollman 			goto process_ACK;
947df8bae1dSRodney W. Grimes 		goto step6;
948a0292f23SGarrett Wollman 	/*
949a0292f23SGarrett Wollman 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
950a0292f23SGarrett Wollman 	 *	if segment contains a SYN and CC [not CC.NEW] option:
951a0292f23SGarrett Wollman 	 *              if state == TIME_WAIT and connection duration > MSL,
952a0292f23SGarrett Wollman 	 *                  drop packet and send RST;
953a0292f23SGarrett Wollman 	 *
954a0292f23SGarrett Wollman 	 *		if SEG.CC > CCrecv then is new SYN, and can implicitly
955a0292f23SGarrett Wollman 	 *		    ack the FIN (and data) in retransmission queue.
956a0292f23SGarrett Wollman 	 *                  Complete close and delete TCPCB.  Then reprocess
957a0292f23SGarrett Wollman 	 *                  segment, hoping to find new TCPCB in LISTEN state;
958a0292f23SGarrett Wollman 	 *
959a0292f23SGarrett Wollman 	 *		else must be old SYN; drop it.
960a0292f23SGarrett Wollman 	 *      else do normal processing.
961a0292f23SGarrett Wollman 	 */
962a0292f23SGarrett Wollman 	case TCPS_LAST_ACK:
963a0292f23SGarrett Wollman 	case TCPS_CLOSING:
964a0292f23SGarrett Wollman 	case TCPS_TIME_WAIT:
965a0292f23SGarrett Wollman 		if ((tiflags & TH_SYN) &&
966a0292f23SGarrett Wollman 		    (to.to_flag & TOF_CC) && tp->cc_recv != 0) {
967a0292f23SGarrett Wollman 			if (tp->t_state == TCPS_TIME_WAIT &&
968a0292f23SGarrett Wollman 					tp->t_duration > TCPTV_MSL)
969a0292f23SGarrett Wollman 				goto dropwithreset;
970a0292f23SGarrett Wollman 			if (CC_GT(to.to_cc, tp->cc_recv)) {
971a0292f23SGarrett Wollman 				tp = tcp_close(tp);
972a0292f23SGarrett Wollman 				goto findpcb;
973a0292f23SGarrett Wollman 			}
974a0292f23SGarrett Wollman 			else
975a0292f23SGarrett Wollman 				goto drop;
976a0292f23SGarrett Wollman 		}
977a0292f23SGarrett Wollman  		break;  /* continue normal processing */
978df8bae1dSRodney W. Grimes 	}
979df8bae1dSRodney W. Grimes 
980df8bae1dSRodney W. Grimes 	/*
981df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
98280ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
98380ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
98480ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
98580ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
98680ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
98780ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
98880ab7c0eSGarrett Wollman 	 * killing a connection).
98980ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
990a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
991df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
992df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
993df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
994df8bae1dSRodney W. Grimes 	 *
99580ab7c0eSGarrett Wollman 	 *
99680ab7c0eSGarrett Wollman 	 * If the RST bit is set, check the sequence number to see
99780ab7c0eSGarrett Wollman 	 * if this is a valid reset segment.
99880ab7c0eSGarrett Wollman 	 * RFC 793 page 37:
99980ab7c0eSGarrett Wollman 	 *   In all states except SYN-SENT, all reset (RST) segments
100080ab7c0eSGarrett Wollman 	 *   are validated by checking their SEQ-fields.  A reset is
100180ab7c0eSGarrett Wollman 	 *   valid if its sequence number is in the window.
100280ab7c0eSGarrett Wollman 	 * Note: this does not take into account delayed ACKs, so
100380ab7c0eSGarrett Wollman 	 *   we should test against last_ack_sent instead of rcv_nxt.
100480ab7c0eSGarrett Wollman 	 *   Also, it does not make sense to allow reset segments with
100580ab7c0eSGarrett Wollman 	 *   sequence numbers greater than last_ack_sent to be processed
100680ab7c0eSGarrett Wollman 	 *   since these sequence numbers are just the acknowledgement
100780ab7c0eSGarrett Wollman 	 *   numbers in our outgoing packets being echoed back at us,
100880ab7c0eSGarrett Wollman 	 *   and these acknowledgement numbers are monotonically
100980ab7c0eSGarrett Wollman 	 *   increasing.
101080ab7c0eSGarrett Wollman 	 * If we have multiple segments in flight, the intial reset
101180ab7c0eSGarrett Wollman 	 * segment sequence numbers will be to the left of last_ack_sent,
101280ab7c0eSGarrett Wollman 	 * but they will eventually catch up.
101380ab7c0eSGarrett Wollman 	 * In any case, it never made sense to trim reset segments to
101480ab7c0eSGarrett Wollman 	 * fit the receive window since RFC 1122 says:
101580ab7c0eSGarrett Wollman 	 *   4.2.2.12  RST Segment: RFC-793 Section 3.4
101680ab7c0eSGarrett Wollman 	 *
101780ab7c0eSGarrett Wollman 	 *    A TCP SHOULD allow a received RST segment to include data.
101880ab7c0eSGarrett Wollman 	 *
101980ab7c0eSGarrett Wollman 	 *    DISCUSSION
102080ab7c0eSGarrett Wollman 	 *         It has been suggested that a RST segment could contain
102180ab7c0eSGarrett Wollman 	 *         ASCII text that encoded and explained the cause of the
102280ab7c0eSGarrett Wollman 	 *         RST.  No standard has yet been established for such
102380ab7c0eSGarrett Wollman 	 *         data.
102480ab7c0eSGarrett Wollman 	 *
102580ab7c0eSGarrett Wollman 	 * If the reset segment passes the sequence number test examine
102680ab7c0eSGarrett Wollman 	 * the state:
102780ab7c0eSGarrett Wollman 	 *    SYN_RECEIVED STATE:
102880ab7c0eSGarrett Wollman 	 *	If passive open, return to LISTEN state.
102980ab7c0eSGarrett Wollman 	 *	If active open, inform user that connection was refused.
103080ab7c0eSGarrett Wollman 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
103180ab7c0eSGarrett Wollman 	 *	Inform user that connection was reset, and close tcb.
103280ab7c0eSGarrett Wollman 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
103380ab7c0eSGarrett Wollman 	 *	Close the tcb.
103480ab7c0eSGarrett Wollman 	 *    TIME_WAIT state:
103580ab7c0eSGarrett Wollman 	 *	Drop the segment - see Stevens, vol. 2, p. 964 and
103680ab7c0eSGarrett Wollman 	 *      RFC 1337.
103780ab7c0eSGarrett Wollman 	 */
103880ab7c0eSGarrett Wollman 	if (tiflags & TH_RST) {
103980ab7c0eSGarrett Wollman 		if (tp->last_ack_sent == ti->ti_seq) {
104080ab7c0eSGarrett Wollman 			switch (tp->t_state) {
104180ab7c0eSGarrett Wollman 
104280ab7c0eSGarrett Wollman 			case TCPS_SYN_RECEIVED:
104380ab7c0eSGarrett Wollman 				so->so_error = ECONNREFUSED;
104480ab7c0eSGarrett Wollman 				goto close;
104580ab7c0eSGarrett Wollman 
104680ab7c0eSGarrett Wollman 			case TCPS_ESTABLISHED:
104780ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_1:
104880ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_2:
104980ab7c0eSGarrett Wollman 			case TCPS_CLOSE_WAIT:
105080ab7c0eSGarrett Wollman 				so->so_error = ECONNRESET;
105180ab7c0eSGarrett Wollman 			close:
105280ab7c0eSGarrett Wollman 				tp->t_state = TCPS_CLOSED;
105380ab7c0eSGarrett Wollman 				tcpstat.tcps_drops++;
105480ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
105580ab7c0eSGarrett Wollman 				break;
105680ab7c0eSGarrett Wollman 
105780ab7c0eSGarrett Wollman 			case TCPS_CLOSING:
105880ab7c0eSGarrett Wollman 			case TCPS_LAST_ACK:
105980ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
106080ab7c0eSGarrett Wollman 				break;
106180ab7c0eSGarrett Wollman 
106280ab7c0eSGarrett Wollman 			case TCPS_TIME_WAIT:
106380ab7c0eSGarrett Wollman 				break;
106480ab7c0eSGarrett Wollman 			}
106580ab7c0eSGarrett Wollman 		}
106680ab7c0eSGarrett Wollman 		goto drop;
106780ab7c0eSGarrett Wollman 	}
106880ab7c0eSGarrett Wollman 
106980ab7c0eSGarrett Wollman 	/*
1070df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
1071df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
1072df8bae1dSRodney W. Grimes 	 */
107380ab7c0eSGarrett Wollman 	if ((to.to_flag & TOF_TS) != 0 && tp->ts_recent &&
107480ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
1075df8bae1dSRodney W. Grimes 
1076df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
1077df8bae1dSRodney W. Grimes 		if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
1078df8bae1dSRodney W. Grimes 			/*
1079df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
1080df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
1081df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
1082df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
1083df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
1084df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
1085df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
1086df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
1087df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
1088df8bae1dSRodney W. Grimes 			 */
1089df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
1090df8bae1dSRodney W. Grimes 		} else {
1091df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvduppack++;
1092df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvdupbyte += ti->ti_len;
1093df8bae1dSRodney W. Grimes 			tcpstat.tcps_pawsdrop++;
1094df8bae1dSRodney W. Grimes 			goto dropafterack;
1095df8bae1dSRodney W. Grimes 		}
1096df8bae1dSRodney W. Grimes 	}
1097df8bae1dSRodney W. Grimes 
1098a0292f23SGarrett Wollman 	/*
1099a0292f23SGarrett Wollman 	 * T/TCP mechanism
1100a0292f23SGarrett Wollman 	 *   If T/TCP was negotiated and the segment doesn't have CC,
1101dc733423SDag-Erling Smørgrav 	 *   or if its CC is wrong then drop the segment.
1102a0292f23SGarrett Wollman 	 *   RST segments do not have to comply with this.
1103a0292f23SGarrett Wollman 	 */
1104a0292f23SGarrett Wollman 	if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) &&
110580ab7c0eSGarrett Wollman 	    ((to.to_flag & TOF_CC) == 0 || tp->cc_recv != to.to_cc))
1106a0292f23SGarrett Wollman  		goto dropafterack;
1107a0292f23SGarrett Wollman 
110880ab7c0eSGarrett Wollman 	/*
110980ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
111080ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
111180ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
111280ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
111380ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
111480ab7c0eSGarrett Wollman 	 */
111580ab7c0eSGarrett Wollman 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(ti->ti_seq, tp->irs))
111680ab7c0eSGarrett Wollman 		goto dropwithreset;
111780ab7c0eSGarrett Wollman 
1118df8bae1dSRodney W. Grimes 	todrop = tp->rcv_nxt - ti->ti_seq;
1119df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1120df8bae1dSRodney W. Grimes 		if (tiflags & TH_SYN) {
1121df8bae1dSRodney W. Grimes 			tiflags &= ~TH_SYN;
1122df8bae1dSRodney W. Grimes 			ti->ti_seq++;
1123df8bae1dSRodney W. Grimes 			if (ti->ti_urp > 1)
1124df8bae1dSRodney W. Grimes 				ti->ti_urp--;
1125df8bae1dSRodney W. Grimes 			else
1126df8bae1dSRodney W. Grimes 				tiflags &= ~TH_URG;
1127df8bae1dSRodney W. Grimes 			todrop--;
1128df8bae1dSRodney W. Grimes 		}
1129df8bae1dSRodney W. Grimes 		/*
1130dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
1131df8bae1dSRodney W. Grimes 		 */
1132dac20301SGarrett Wollman 		if (todrop > ti->ti_len
1133dac20301SGarrett Wollman 		    || (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) {
1134dac20301SGarrett Wollman 			/*
1135dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
1136dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
1137dac20301SGarrett Wollman 			 * of sequence; drop it.
1138dac20301SGarrett Wollman 			 */
1139df8bae1dSRodney W. Grimes 			tiflags &= ~TH_FIN;
1140dac20301SGarrett Wollman 
1141df8bae1dSRodney W. Grimes 			/*
1142dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
1143dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
1144df8bae1dSRodney W. Grimes 			 */
1145dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
1146dac20301SGarrett Wollman 			todrop = ti->ti_len;
1147dac20301SGarrett Wollman 			tcpstat.tcps_rcvduppack++;
1148dac20301SGarrett Wollman 			tcpstat.tcps_rcvdupbyte += todrop;
1149df8bae1dSRodney W. Grimes 		} else {
1150df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartduppack++;
1151df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartdupbyte += todrop;
1152df8bae1dSRodney W. Grimes 		}
1153df8bae1dSRodney W. Grimes 		m_adj(m, todrop);
1154df8bae1dSRodney W. Grimes 		ti->ti_seq += todrop;
1155df8bae1dSRodney W. Grimes 		ti->ti_len -= todrop;
1156df8bae1dSRodney W. Grimes 		if (ti->ti_urp > todrop)
1157df8bae1dSRodney W. Grimes 			ti->ti_urp -= todrop;
1158df8bae1dSRodney W. Grimes 		else {
1159df8bae1dSRodney W. Grimes 			tiflags &= ~TH_URG;
1160df8bae1dSRodney W. Grimes 			ti->ti_urp = 0;
1161df8bae1dSRodney W. Grimes 		}
1162df8bae1dSRodney W. Grimes 	}
1163df8bae1dSRodney W. Grimes 
1164df8bae1dSRodney W. Grimes 	/*
1165df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
1166df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
1167df8bae1dSRodney W. Grimes 	 */
1168df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
1169df8bae1dSRodney W. Grimes 	    tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
1170df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1171df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvafterclose++;
1172df8bae1dSRodney W. Grimes 		goto dropwithreset;
1173df8bae1dSRodney W. Grimes 	}
1174df8bae1dSRodney W. Grimes 
1175df8bae1dSRodney W. Grimes 	/*
1176df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
1177df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
1178df8bae1dSRodney W. Grimes 	 */
1179df8bae1dSRodney W. Grimes 	todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
1180df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1181df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvpackafterwin++;
1182df8bae1dSRodney W. Grimes 		if (todrop >= ti->ti_len) {
1183df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
1184df8bae1dSRodney W. Grimes 			/*
1185df8bae1dSRodney W. Grimes 			 * If a new connection request is received
1186df8bae1dSRodney W. Grimes 			 * while in TIME_WAIT, drop the old connection
1187df8bae1dSRodney W. Grimes 			 * and start over if the sequence numbers
1188df8bae1dSRodney W. Grimes 			 * are above the previous ones.
1189df8bae1dSRodney W. Grimes 			 */
1190df8bae1dSRodney W. Grimes 			if (tiflags & TH_SYN &&
1191df8bae1dSRodney W. Grimes 			    tp->t_state == TCPS_TIME_WAIT &&
1192df8bae1dSRodney W. Grimes 			    SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
1193df8bae1dSRodney W. Grimes 				iss = tp->rcv_nxt + TCP_ISSINCR;
1194df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
1195df8bae1dSRodney W. Grimes 				goto findpcb;
1196df8bae1dSRodney W. Grimes 			}
1197df8bae1dSRodney W. Grimes 			/*
1198df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
1199df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
1200df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
1201df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
1202df8bae1dSRodney W. Grimes 			 * and ack.
1203df8bae1dSRodney W. Grimes 			 */
1204df8bae1dSRodney W. Grimes 			if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
1205df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
1206df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvwinprobe++;
1207df8bae1dSRodney W. Grimes 			} else
1208df8bae1dSRodney W. Grimes 				goto dropafterack;
1209df8bae1dSRodney W. Grimes 		} else
1210df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
1211df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
1212df8bae1dSRodney W. Grimes 		ti->ti_len -= todrop;
1213df8bae1dSRodney W. Grimes 		tiflags &= ~(TH_PUSH|TH_FIN);
1214df8bae1dSRodney W. Grimes 	}
1215df8bae1dSRodney W. Grimes 
1216df8bae1dSRodney W. Grimes 	/*
1217df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
1218df8bae1dSRodney W. Grimes 	 * record its timestamp.
1219a0292f23SGarrett Wollman 	 * NOTE that the test is modified according to the latest
1220a0292f23SGarrett Wollman 	 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1221df8bae1dSRodney W. Grimes 	 */
1222a0292f23SGarrett Wollman 	if ((to.to_flag & TOF_TS) != 0 &&
1223a0292f23SGarrett Wollman 	    SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) {
1224a0292f23SGarrett Wollman 		tp->ts_recent_age = tcp_now;
1225a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
1226df8bae1dSRodney W. Grimes 	}
1227df8bae1dSRodney W. Grimes 
1228df8bae1dSRodney W. Grimes 	/*
1229df8bae1dSRodney W. Grimes 	 * If a SYN is in the window, then this is an
1230df8bae1dSRodney W. Grimes 	 * error and we send an RST and drop the connection.
1231df8bae1dSRodney W. Grimes 	 */
1232df8bae1dSRodney W. Grimes 	if (tiflags & TH_SYN) {
1233df8bae1dSRodney W. Grimes 		tp = tcp_drop(tp, ECONNRESET);
1234df8bae1dSRodney W. Grimes 		goto dropwithreset;
1235df8bae1dSRodney W. Grimes 	}
1236df8bae1dSRodney W. Grimes 
1237a0292f23SGarrett Wollman 	/*
1238a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
1239a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
1240a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
1241a0292f23SGarrett Wollman 	 */
1242a0292f23SGarrett Wollman 	if ((tiflags & TH_ACK) == 0) {
1243a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
1244a0292f23SGarrett Wollman 		    (tp->t_flags & TF_NEEDSYN))
1245a0292f23SGarrett Wollman 			goto step6;
1246a0292f23SGarrett Wollman 		else
1247a0292f23SGarrett Wollman 			goto drop;
1248a0292f23SGarrett Wollman 	}
1249df8bae1dSRodney W. Grimes 
1250df8bae1dSRodney W. Grimes 	/*
1251df8bae1dSRodney W. Grimes 	 * Ack processing.
1252df8bae1dSRodney W. Grimes 	 */
1253df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1254df8bae1dSRodney W. Grimes 
1255df8bae1dSRodney W. Grimes 	/*
1256764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
1257764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
1258764d8cefSBill Fenner 	 * The ACK was checked above.
1259df8bae1dSRodney W. Grimes 	 */
1260df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1261a0292f23SGarrett Wollman 
1262df8bae1dSRodney W. Grimes 		tcpstat.tcps_connects++;
1263df8bae1dSRodney W. Grimes 		soisconnected(so);
1264df8bae1dSRodney W. Grimes 		/* Do window scaling? */
1265df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1266df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1267df8bae1dSRodney W. Grimes 			tp->snd_scale = tp->requested_s_scale;
1268df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
1269df8bae1dSRodney W. Grimes 		}
1270a0292f23SGarrett Wollman 		/*
1271a0292f23SGarrett Wollman 		 * Upon successful completion of 3-way handshake,
1272a0292f23SGarrett Wollman 		 * update cache.CC if it was undefined, pass any queued
1273a0292f23SGarrett Wollman 		 * data to the user, and advance state appropriately.
1274a0292f23SGarrett Wollman 		 */
1275a0292f23SGarrett Wollman 		if ((taop = tcp_gettaocache(inp)) != NULL &&
1276a0292f23SGarrett Wollman 		    taop->tao_cc == 0)
1277a0292f23SGarrett Wollman 			taop->tao_cc = tp->cc_recv;
1278a0292f23SGarrett Wollman 
1279a0292f23SGarrett Wollman 		/*
1280a0292f23SGarrett Wollman 		 * Make transitions:
1281a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
1282a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
1283a0292f23SGarrett Wollman 		 */
1284a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDFIN) {
1285a0292f23SGarrett Wollman 			tp->t_state = TCPS_FIN_WAIT_1;
1286a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDFIN;
12877ff19458SPaul Traina 		} else {
1288a0292f23SGarrett Wollman 			tp->t_state = TCPS_ESTABLISHED;
12897ff19458SPaul Traina 			tp->t_timer[TCPT_KEEP] = tcp_keepidle;
12907ff19458SPaul Traina 		}
1291a0292f23SGarrett Wollman 		/*
1292a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
1293a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
1294a0292f23SGarrett Wollman 		 */
1295a0292f23SGarrett Wollman 		if (ti->ti_len == 0 && (tiflags & TH_FIN) == 0)
1296a0292f23SGarrett Wollman 			(void) tcp_reass(tp, (struct tcpiphdr *)0,
1297a0292f23SGarrett Wollman 			    (struct mbuf *)0);
1298df8bae1dSRodney W. Grimes 		tp->snd_wl1 = ti->ti_seq - 1;
1299df8bae1dSRodney W. Grimes 		/* fall into ... */
1300df8bae1dSRodney W. Grimes 
1301df8bae1dSRodney W. Grimes 	/*
1302df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1303df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
1304df8bae1dSRodney W. Grimes 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
1305df8bae1dSRodney W. Grimes 	 * then advance tp->snd_una to ti->ti_ack and drop
1306df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
1307df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
1308df8bae1dSRodney W. Grimes 	 */
1309df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1310df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
1311df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
1312df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1313df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
1314df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
1315df8bae1dSRodney W. Grimes 	case TCPS_TIME_WAIT:
1316df8bae1dSRodney W. Grimes 
1317df8bae1dSRodney W. Grimes 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
1318df8bae1dSRodney W. Grimes 			if (ti->ti_len == 0 && tiwin == tp->snd_wnd) {
1319df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvdupack++;
1320df8bae1dSRodney W. Grimes 				/*
1321df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
1322df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
1323df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
1324df8bae1dSRodney W. Grimes 				 * change), the ack is the biggest we've
1325df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
1326df8bae1dSRodney W. Grimes 				 * threshhold of them, assume a packet
1327df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
1328df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
1329df8bae1dSRodney W. Grimes 				 * window so we send only this one
1330df8bae1dSRodney W. Grimes 				 * packet.
1331df8bae1dSRodney W. Grimes 				 *
1332df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
1333df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
1334df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
1335df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
1336df8bae1dSRodney W. Grimes 				 * the new ssthresh).
1337df8bae1dSRodney W. Grimes 				 *
1338df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
1339df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
1340df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
1341df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
1342df8bae1dSRodney W. Grimes 				 * network.
1343df8bae1dSRodney W. Grimes 				 */
1344df8bae1dSRodney W. Grimes 				if (tp->t_timer[TCPT_REXMT] == 0 ||
1345df8bae1dSRodney W. Grimes 				    ti->ti_ack != tp->snd_una)
1346df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
1347df8bae1dSRodney W. Grimes 				else if (++tp->t_dupacks == tcprexmtthresh) {
1348df8bae1dSRodney W. Grimes 					tcp_seq onxt = tp->snd_nxt;
1349df8bae1dSRodney W. Grimes 					u_int win =
1350df8bae1dSRodney W. Grimes 					    min(tp->snd_wnd, tp->snd_cwnd) / 2 /
1351df8bae1dSRodney W. Grimes 						tp->t_maxseg;
1352df8bae1dSRodney W. Grimes 
1353df8bae1dSRodney W. Grimes 					if (win < 2)
1354df8bae1dSRodney W. Grimes 						win = 2;
1355df8bae1dSRodney W. Grimes 					tp->snd_ssthresh = win * tp->t_maxseg;
1356df8bae1dSRodney W. Grimes 					tp->t_timer[TCPT_REXMT] = 0;
1357df8bae1dSRodney W. Grimes 					tp->t_rtt = 0;
1358df8bae1dSRodney W. Grimes 					tp->snd_nxt = ti->ti_ack;
1359df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->t_maxseg;
1360df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1361df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
1362df8bae1dSRodney W. Grimes 					       tp->t_maxseg * tp->t_dupacks;
1363df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
1364df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
1365df8bae1dSRodney W. Grimes 					goto drop;
1366df8bae1dSRodney W. Grimes 				} else if (tp->t_dupacks > tcprexmtthresh) {
1367df8bae1dSRodney W. Grimes 					tp->snd_cwnd += tp->t_maxseg;
1368df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1369df8bae1dSRodney W. Grimes 					goto drop;
1370df8bae1dSRodney W. Grimes 				}
1371df8bae1dSRodney W. Grimes 			} else
1372df8bae1dSRodney W. Grimes 				tp->t_dupacks = 0;
1373df8bae1dSRodney W. Grimes 			break;
1374df8bae1dSRodney W. Grimes 		}
1375df8bae1dSRodney W. Grimes 		/*
1376df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
1377df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
1378df8bae1dSRodney W. Grimes 		 */
1379233e8c18SGarrett Wollman 		if (tp->t_dupacks >= tcprexmtthresh &&
1380df8bae1dSRodney W. Grimes 		    tp->snd_cwnd > tp->snd_ssthresh)
1381df8bae1dSRodney W. Grimes 			tp->snd_cwnd = tp->snd_ssthresh;
1382df8bae1dSRodney W. Grimes 		tp->t_dupacks = 0;
1383df8bae1dSRodney W. Grimes 		if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
1384df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvacktoomuch++;
1385df8bae1dSRodney W. Grimes 			goto dropafterack;
1386df8bae1dSRodney W. Grimes 		}
1387a0292f23SGarrett Wollman 		/*
1388a0292f23SGarrett Wollman 		 *  If we reach this point, ACK is not a duplicate,
1389a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
1390a0292f23SGarrett Wollman 		 */
1391a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
1392a0292f23SGarrett Wollman 			/*
1393a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
1394a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
139507e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
139607e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
139707e43e10SAndras Olah 			 * we can do window scaling.
1398a0292f23SGarrett Wollman 			 */
1399a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
1400a0292f23SGarrett Wollman 			tp->snd_una++;
140107e43e10SAndras Olah 			/* Do window scaling? */
140207e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
140307e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
140407e43e10SAndras Olah 				tp->snd_scale = tp->requested_s_scale;
140507e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
140607e43e10SAndras Olah 			}
1407a0292f23SGarrett Wollman 		}
1408a0292f23SGarrett Wollman 
1409a0292f23SGarrett Wollman process_ACK:
1410df8bae1dSRodney W. Grimes 		acked = ti->ti_ack - tp->snd_una;
1411df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackpack++;
1412df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackbyte += acked;
1413df8bae1dSRodney W. Grimes 
1414df8bae1dSRodney W. Grimes 		/*
1415df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
1416df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
1417df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
1418df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
1419df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
1420df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
1421df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
1422df8bae1dSRodney W. Grimes 		 */
1423a0292f23SGarrett Wollman 		if (to.to_flag & TOF_TS)
1424a0292f23SGarrett Wollman 			tcp_xmit_timer(tp, tcp_now - to.to_tsecr + 1);
1425df8bae1dSRodney W. Grimes 		else if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
1426df8bae1dSRodney W. Grimes 			tcp_xmit_timer(tp,tp->t_rtt);
1427df8bae1dSRodney W. Grimes 
1428df8bae1dSRodney W. Grimes 		/*
1429df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
1430df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
1431df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
1432df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
1433df8bae1dSRodney W. Grimes 		 */
1434df8bae1dSRodney W. Grimes 		if (ti->ti_ack == tp->snd_max) {
1435df8bae1dSRodney W. Grimes 			tp->t_timer[TCPT_REXMT] = 0;
1436df8bae1dSRodney W. Grimes 			needoutput = 1;
1437df8bae1dSRodney W. Grimes 		} else if (tp->t_timer[TCPT_PERSIST] == 0)
1438df8bae1dSRodney W. Grimes 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
1439a0292f23SGarrett Wollman 
1440a0292f23SGarrett Wollman 		/*
1441a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
1442a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
1443a0292f23SGarrett Wollman 		 */
1444a0292f23SGarrett Wollman 		if (acked == 0)
1445a0292f23SGarrett Wollman 			goto step6;
1446a0292f23SGarrett Wollman 
1447df8bae1dSRodney W. Grimes 		/*
1448df8bae1dSRodney W. Grimes 		 * When new data is acked, open the congestion window.
1449df8bae1dSRodney W. Grimes 		 * If the window gives us less than ssthresh packets
1450df8bae1dSRodney W. Grimes 		 * in flight, open exponentially (maxseg per packet).
1451df8bae1dSRodney W. Grimes 		 * Otherwise open linearly: maxseg per window
145210be5648SGarrett Wollman 		 * (maxseg^2 / cwnd per packet).
1453df8bae1dSRodney W. Grimes 		 */
1454df8bae1dSRodney W. Grimes 		{
1455df8bae1dSRodney W. Grimes 		register u_int cw = tp->snd_cwnd;
1456df8bae1dSRodney W. Grimes 		register u_int incr = tp->t_maxseg;
1457df8bae1dSRodney W. Grimes 
1458df8bae1dSRodney W. Grimes 		if (cw > tp->snd_ssthresh)
145910be5648SGarrett Wollman 			incr = incr * incr / cw;
1460df8bae1dSRodney W. Grimes 		tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale);
1461df8bae1dSRodney W. Grimes 		}
1462df8bae1dSRodney W. Grimes 		if (acked > so->so_snd.sb_cc) {
1463df8bae1dSRodney W. Grimes 			tp->snd_wnd -= so->so_snd.sb_cc;
1464df8bae1dSRodney W. Grimes 			sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
1465df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
1466df8bae1dSRodney W. Grimes 		} else {
1467df8bae1dSRodney W. Grimes 			sbdrop(&so->so_snd, acked);
1468df8bae1dSRodney W. Grimes 			tp->snd_wnd -= acked;
1469df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
1470df8bae1dSRodney W. Grimes 		}
1471df8bae1dSRodney W. Grimes 		sowwakeup(so);
1472df8bae1dSRodney W. Grimes 		tp->snd_una = ti->ti_ack;
1473df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1474df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
1475df8bae1dSRodney W. Grimes 
1476df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
1477df8bae1dSRodney W. Grimes 
1478df8bae1dSRodney W. Grimes 		/*
1479df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
1480df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
1481df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
1482df8bae1dSRodney W. Grimes 		 */
1483df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
1484df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
1485df8bae1dSRodney W. Grimes 				/*
1486df8bae1dSRodney W. Grimes 				 * If we can't receive any more
1487df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
1488df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
1489df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
1490df8bae1dSRodney W. Grimes 				 * we'll hang forever.
1491df8bae1dSRodney W. Grimes 				 */
1492df8bae1dSRodney W. Grimes 				if (so->so_state & SS_CANTRCVMORE) {
1493df8bae1dSRodney W. Grimes 					soisdisconnected(so);
1494df8bae1dSRodney W. Grimes 					tp->t_timer[TCPT_2MSL] = tcp_maxidle;
1495df8bae1dSRodney W. Grimes 				}
1496df8bae1dSRodney W. Grimes 				tp->t_state = TCPS_FIN_WAIT_2;
1497df8bae1dSRodney W. Grimes 			}
1498df8bae1dSRodney W. Grimes 			break;
1499df8bae1dSRodney W. Grimes 
1500df8bae1dSRodney W. Grimes 	 	/*
1501df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
1502df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
1503df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
1504df8bae1dSRodney W. Grimes 		 * the segment.
1505df8bae1dSRodney W. Grimes 		 */
1506df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
1507df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
1508df8bae1dSRodney W. Grimes 				tp->t_state = TCPS_TIME_WAIT;
1509df8bae1dSRodney W. Grimes 				tcp_canceltimers(tp);
1510a0292f23SGarrett Wollman 				/* Shorten TIME_WAIT [RFC-1644, p.28] */
1511a0292f23SGarrett Wollman 				if (tp->cc_recv != 0 &&
1512a0292f23SGarrett Wollman 				    tp->t_duration < TCPTV_MSL)
1513a0292f23SGarrett Wollman 					tp->t_timer[TCPT_2MSL] =
1514a0292f23SGarrett Wollman 					    tp->t_rxtcur * TCPTV_TWTRUNC;
1515a0292f23SGarrett Wollman 				else
1516df8bae1dSRodney W. Grimes 					tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1517df8bae1dSRodney W. Grimes 				soisdisconnected(so);
1518df8bae1dSRodney W. Grimes 			}
1519df8bae1dSRodney W. Grimes 			break;
1520df8bae1dSRodney W. Grimes 
1521df8bae1dSRodney W. Grimes 		/*
1522df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
1523df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
1524df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
1525df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
1526df8bae1dSRodney W. Grimes 		 */
1527df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
1528df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
1529df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
1530df8bae1dSRodney W. Grimes 				goto drop;
1531df8bae1dSRodney W. Grimes 			}
1532df8bae1dSRodney W. Grimes 			break;
1533df8bae1dSRodney W. Grimes 
1534df8bae1dSRodney W. Grimes 		/*
1535df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state the only thing that should arrive
1536df8bae1dSRodney W. Grimes 		 * is a retransmission of the remote FIN.  Acknowledge
1537df8bae1dSRodney W. Grimes 		 * it and restart the finack timer.
1538df8bae1dSRodney W. Grimes 		 */
1539df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
1540df8bae1dSRodney W. Grimes 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1541df8bae1dSRodney W. Grimes 			goto dropafterack;
1542df8bae1dSRodney W. Grimes 		}
1543df8bae1dSRodney W. Grimes 	}
1544df8bae1dSRodney W. Grimes 
1545df8bae1dSRodney W. Grimes step6:
1546df8bae1dSRodney W. Grimes 	/*
1547df8bae1dSRodney W. Grimes 	 * Update window information.
1548df8bae1dSRodney W. Grimes 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
1549df8bae1dSRodney W. Grimes 	 */
1550df8bae1dSRodney W. Grimes 	if ((tiflags & TH_ACK) &&
1551623ae52eSPoul-Henning Kamp 	    (SEQ_LT(tp->snd_wl1, ti->ti_seq) ||
1552623ae52eSPoul-Henning Kamp 	    (tp->snd_wl1 == ti->ti_seq && (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
1553623ae52eSPoul-Henning Kamp 	     (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))))) {
1554df8bae1dSRodney W. Grimes 		/* keep track of pure window updates */
1555df8bae1dSRodney W. Grimes 		if (ti->ti_len == 0 &&
1556df8bae1dSRodney W. Grimes 		    tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd)
1557df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvwinupd++;
1558df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
1559df8bae1dSRodney W. Grimes 		tp->snd_wl1 = ti->ti_seq;
1560df8bae1dSRodney W. Grimes 		tp->snd_wl2 = ti->ti_ack;
1561df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
1562df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
1563df8bae1dSRodney W. Grimes 		needoutput = 1;
1564df8bae1dSRodney W. Grimes 	}
1565df8bae1dSRodney W. Grimes 
1566df8bae1dSRodney W. Grimes 	/*
1567df8bae1dSRodney W. Grimes 	 * Process segments with URG.
1568df8bae1dSRodney W. Grimes 	 */
1569df8bae1dSRodney W. Grimes 	if ((tiflags & TH_URG) && ti->ti_urp &&
1570df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1571df8bae1dSRodney W. Grimes 		/*
1572df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
1573df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
1574df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
1575df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
1576df8bae1dSRodney W. Grimes 		 */
1577df8bae1dSRodney W. Grimes 		if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) {
1578df8bae1dSRodney W. Grimes 			ti->ti_urp = 0;			/* XXX */
1579df8bae1dSRodney W. Grimes 			tiflags &= ~TH_URG;		/* XXX */
1580df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
1581df8bae1dSRodney W. Grimes 		}
1582df8bae1dSRodney W. Grimes 		/*
1583df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
1584df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
1585df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1586df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
1587df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
1588df8bae1dSRodney W. Grimes 		 *
1589df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
1590df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
1591df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
1592df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
1593df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
1594df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
1595df8bae1dSRodney W. Grimes 		 */
1596df8bae1dSRodney W. Grimes 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
1597df8bae1dSRodney W. Grimes 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
1598df8bae1dSRodney W. Grimes 			so->so_oobmark = so->so_rcv.sb_cc +
1599df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
1600df8bae1dSRodney W. Grimes 			if (so->so_oobmark == 0)
1601df8bae1dSRodney W. Grimes 				so->so_state |= SS_RCVATMARK;
1602df8bae1dSRodney W. Grimes 			sohasoutofband(so);
1603df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1604df8bae1dSRodney W. Grimes 		}
1605df8bae1dSRodney W. Grimes 		/*
1606df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
1607df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
1608df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
1609df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
1610df8bae1dSRodney W. Grimes 		 */
161126f9a767SRodney W. Grimes 		if (ti->ti_urp <= (u_long)ti->ti_len
1612df8bae1dSRodney W. Grimes #ifdef SO_OOBINLINE
1613df8bae1dSRodney W. Grimes 		     && (so->so_options & SO_OOBINLINE) == 0
1614df8bae1dSRodney W. Grimes #endif
1615df8bae1dSRodney W. Grimes 		     )
1616df8bae1dSRodney W. Grimes 			tcp_pulloutofband(so, ti, m);
1617df8bae1dSRodney W. Grimes 	} else
1618df8bae1dSRodney W. Grimes 		/*
1619df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
1620df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
1621df8bae1dSRodney W. Grimes 		 * with the receive window.
1622df8bae1dSRodney W. Grimes 		 */
1623df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1624df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
1625df8bae1dSRodney W. Grimes dodata:							/* XXX */
1626df8bae1dSRodney W. Grimes 
1627df8bae1dSRodney W. Grimes 	/*
1628df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
1629df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
1630df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
1631df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
1632df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
1633df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
1634df8bae1dSRodney W. Grimes 	 */
1635df8bae1dSRodney W. Grimes 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
1636df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1637df8bae1dSRodney W. Grimes 		TCP_REASS(tp, ti, m, so, tiflags);
1638df8bae1dSRodney W. Grimes 		/*
1639df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
1640df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
1641df8bae1dSRodney W. Grimes 		 * buffer size.
1642df8bae1dSRodney W. Grimes 		 */
1643df8bae1dSRodney W. Grimes 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
1644df8bae1dSRodney W. Grimes 	} else {
1645df8bae1dSRodney W. Grimes 		m_freem(m);
1646df8bae1dSRodney W. Grimes 		tiflags &= ~TH_FIN;
1647df8bae1dSRodney W. Grimes 	}
1648df8bae1dSRodney W. Grimes 
1649df8bae1dSRodney W. Grimes 	/*
1650df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
1651df8bae1dSRodney W. Grimes 	 * that the connection is closing.
1652df8bae1dSRodney W. Grimes 	 */
1653df8bae1dSRodney W. Grimes 	if (tiflags & TH_FIN) {
1654df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1655df8bae1dSRodney W. Grimes 			socantrcvmore(so);
1656a0292f23SGarrett Wollman 			/*
1657a0292f23SGarrett Wollman 			 *  If connection is half-synchronized
1658d3eede9dSAndras Olah 			 *  (ie NEEDSYN flag on) then delay ACK,
1659a0292f23SGarrett Wollman 			 *  so it may be piggybacked when SYN is sent.
1660a0292f23SGarrett Wollman 			 *  Otherwise, since we received a FIN then no
1661a0292f23SGarrett Wollman 			 *  more input can be expected, send ACK now.
1662a0292f23SGarrett Wollman 			 */
1663f498eeeeSDavid Greenman 			if (tcp_delack_enabled && (tp->t_flags & TF_NEEDSYN))
1664a0292f23SGarrett Wollman 				tp->t_flags |= TF_DELACK;
1665a0292f23SGarrett Wollman 			else
1666df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
1667df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
1668df8bae1dSRodney W. Grimes 		}
1669df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
1670df8bae1dSRodney W. Grimes 
1671df8bae1dSRodney W. Grimes 	 	/*
1672df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
1673df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
1674df8bae1dSRodney W. Grimes 		 */
1675df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
1676df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
1677df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSE_WAIT;
1678df8bae1dSRodney W. Grimes 			break;
1679df8bae1dSRodney W. Grimes 
1680df8bae1dSRodney W. Grimes 	 	/*
1681df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1682df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
1683df8bae1dSRodney W. Grimes 		 */
1684df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
1685df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSING;
1686df8bae1dSRodney W. Grimes 			break;
1687df8bae1dSRodney W. Grimes 
1688df8bae1dSRodney W. Grimes 	 	/*
1689df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1690df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
1691df8bae1dSRodney W. Grimes 		 * standard timers.
1692df8bae1dSRodney W. Grimes 		 */
1693df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
1694df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_TIME_WAIT;
1695df8bae1dSRodney W. Grimes 			tcp_canceltimers(tp);
1696a0292f23SGarrett Wollman 			/* Shorten TIME_WAIT [RFC-1644, p.28] */
1697a0292f23SGarrett Wollman 			if (tp->cc_recv != 0 &&
1698a0292f23SGarrett Wollman 			    tp->t_duration < TCPTV_MSL) {
1699a0292f23SGarrett Wollman 				tp->t_timer[TCPT_2MSL] =
1700a0292f23SGarrett Wollman 				    tp->t_rxtcur * TCPTV_TWTRUNC;
1701a0292f23SGarrett Wollman 				/* For transaction client, force ACK now. */
1702a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
1703a0292f23SGarrett Wollman 			}
1704a0292f23SGarrett Wollman 			else
1705df8bae1dSRodney W. Grimes 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1706df8bae1dSRodney W. Grimes 			soisdisconnected(so);
1707df8bae1dSRodney W. Grimes 			break;
1708df8bae1dSRodney W. Grimes 
1709df8bae1dSRodney W. Grimes 		/*
1710df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1711df8bae1dSRodney W. Grimes 		 */
1712df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
1713df8bae1dSRodney W. Grimes 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1714df8bae1dSRodney W. Grimes 			break;
1715df8bae1dSRodney W. Grimes 		}
1716df8bae1dSRodney W. Grimes 	}
1717610ee2f9SDavid Greenman #ifdef TCPDEBUG
1718df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG)
1719df8bae1dSRodney W. Grimes 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
1720610ee2f9SDavid Greenman #endif
1721df8bae1dSRodney W. Grimes 
1722df8bae1dSRodney W. Grimes 	/*
1723df8bae1dSRodney W. Grimes 	 * Return any desired output.
1724df8bae1dSRodney W. Grimes 	 */
1725df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
1726df8bae1dSRodney W. Grimes 		(void) tcp_output(tp);
1727df8bae1dSRodney W. Grimes 	return;
1728df8bae1dSRodney W. Grimes 
1729df8bae1dSRodney W. Grimes dropafterack:
1730df8bae1dSRodney W. Grimes 	/*
1731df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
1732df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
173380ab7c0eSGarrett Wollman 	 *
173480ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
173580ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
173680ab7c0eSGarrett Wollman 	 * RST have been dropped.
173780ab7c0eSGarrett Wollman 	 *
173880ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
173980ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
174080ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
174180ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
174280ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
174380ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
1744df8bae1dSRodney W. Grimes 	 */
174580ab7c0eSGarrett Wollman 	if (tp->t_state == TCPS_SYN_RECEIVED && (tiflags & TH_ACK) &&
174680ab7c0eSGarrett Wollman 	    (SEQ_GT(tp->snd_una, ti->ti_ack) ||
174780ab7c0eSGarrett Wollman 	     SEQ_GT(ti->ti_ack, tp->snd_max)) )
174880ab7c0eSGarrett Wollman 		goto dropwithreset;
1749a0292f23SGarrett Wollman #ifdef TCPDEBUG
1750a0292f23SGarrett Wollman 	if (so->so_options & SO_DEBUG)
1751a0292f23SGarrett Wollman 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1752a0292f23SGarrett Wollman #endif
1753df8bae1dSRodney W. Grimes 	m_freem(m);
1754df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
1755df8bae1dSRodney W. Grimes 	(void) tcp_output(tp);
1756df8bae1dSRodney W. Grimes 	return;
1757df8bae1dSRodney W. Grimes 
1758df8bae1dSRodney W. Grimes dropwithreset:
1759df8bae1dSRodney W. Grimes 	/*
1760df8bae1dSRodney W. Grimes 	 * Generate a RST, dropping incoming segment.
1761df8bae1dSRodney W. Grimes 	 * Make ACK acceptable to originator of segment.
1762df8bae1dSRodney W. Grimes 	 * Don't bother to respond if destination was broadcast/multicast.
1763df8bae1dSRodney W. Grimes 	 */
1764df8bae1dSRodney W. Grimes 	if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST) ||
1765d4d0967eSDavid Greenman 	    IN_MULTICAST(ntohl(ti->ti_dst.s_addr)))
1766df8bae1dSRodney W. Grimes 		goto drop;
1767a0292f23SGarrett Wollman #ifdef TCPDEBUG
1768a0292f23SGarrett Wollman 	if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
1769a0292f23SGarrett Wollman 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1770a0292f23SGarrett Wollman #endif
1771df8bae1dSRodney W. Grimes 	if (tiflags & TH_ACK)
1772df8bae1dSRodney W. Grimes 		tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
1773df8bae1dSRodney W. Grimes 	else {
1774df8bae1dSRodney W. Grimes 		if (tiflags & TH_SYN)
1775df8bae1dSRodney W. Grimes 			ti->ti_len++;
1776df8bae1dSRodney W. Grimes 		tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
1777df8bae1dSRodney W. Grimes 		    TH_RST|TH_ACK);
1778df8bae1dSRodney W. Grimes 	}
1779df8bae1dSRodney W. Grimes 	/* destroy temporarily created socket */
1780df8bae1dSRodney W. Grimes 	if (dropsocket)
1781df8bae1dSRodney W. Grimes 		(void) soabort(so);
1782df8bae1dSRodney W. Grimes 	return;
1783df8bae1dSRodney W. Grimes 
1784df8bae1dSRodney W. Grimes drop:
1785df8bae1dSRodney W. Grimes 	/*
1786df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
1787df8bae1dSRodney W. Grimes 	 */
1788610ee2f9SDavid Greenman #ifdef TCPDEBUG
1789a0292f23SGarrett Wollman 	if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
1790a0292f23SGarrett Wollman 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1791a0292f23SGarrett Wollman #endif
1792df8bae1dSRodney W. Grimes 	m_freem(m);
1793df8bae1dSRodney W. Grimes 	/* destroy temporarily created socket */
1794df8bae1dSRodney W. Grimes 	if (dropsocket)
1795df8bae1dSRodney W. Grimes 		(void) soabort(so);
1796df8bae1dSRodney W. Grimes 	return;
1797df8bae1dSRodney W. Grimes }
1798df8bae1dSRodney W. Grimes 
17990312fbe9SPoul-Henning Kamp static void
1800a0292f23SGarrett Wollman tcp_dooptions(tp, cp, cnt, ti, to)
1801df8bae1dSRodney W. Grimes 	struct tcpcb *tp;
1802df8bae1dSRodney W. Grimes 	u_char *cp;
1803df8bae1dSRodney W. Grimes 	int cnt;
1804df8bae1dSRodney W. Grimes 	struct tcpiphdr *ti;
1805a0292f23SGarrett Wollman 	struct tcpopt *to;
1806df8bae1dSRodney W. Grimes {
1807a0292f23SGarrett Wollman 	u_short mss = 0;
1808df8bae1dSRodney W. Grimes 	int opt, optlen;
1809df8bae1dSRodney W. Grimes 
1810df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1811df8bae1dSRodney W. Grimes 		opt = cp[0];
1812df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
1813df8bae1dSRodney W. Grimes 			break;
1814df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
1815df8bae1dSRodney W. Grimes 			optlen = 1;
1816df8bae1dSRodney W. Grimes 		else {
1817df8bae1dSRodney W. Grimes 			optlen = cp[1];
1818df8bae1dSRodney W. Grimes 			if (optlen <= 0)
1819df8bae1dSRodney W. Grimes 				break;
1820df8bae1dSRodney W. Grimes 		}
1821df8bae1dSRodney W. Grimes 		switch (opt) {
1822df8bae1dSRodney W. Grimes 
1823df8bae1dSRodney W. Grimes 		default:
1824df8bae1dSRodney W. Grimes 			continue;
1825df8bae1dSRodney W. Grimes 
1826df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
1827df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
1828df8bae1dSRodney W. Grimes 				continue;
1829df8bae1dSRodney W. Grimes 			if (!(ti->ti_flags & TH_SYN))
1830df8bae1dSRodney W. Grimes 				continue;
1831df8bae1dSRodney W. Grimes 			bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
1832df8bae1dSRodney W. Grimes 			NTOHS(mss);
1833df8bae1dSRodney W. Grimes 			break;
1834df8bae1dSRodney W. Grimes 
1835df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
1836df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
1837df8bae1dSRodney W. Grimes 				continue;
1838df8bae1dSRodney W. Grimes 			if (!(ti->ti_flags & TH_SYN))
1839df8bae1dSRodney W. Grimes 				continue;
1840df8bae1dSRodney W. Grimes 			tp->t_flags |= TF_RCVD_SCALE;
1841df8bae1dSRodney W. Grimes 			tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
1842df8bae1dSRodney W. Grimes 			break;
1843df8bae1dSRodney W. Grimes 
1844df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
1845df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
1846df8bae1dSRodney W. Grimes 				continue;
1847a0292f23SGarrett Wollman 			to->to_flag |= TOF_TS;
1848a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
1849a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
1850a0292f23SGarrett Wollman 			NTOHL(to->to_tsval);
1851a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
1852a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
1853a0292f23SGarrett Wollman 			NTOHL(to->to_tsecr);
1854df8bae1dSRodney W. Grimes 
1855df8bae1dSRodney W. Grimes 			/*
1856df8bae1dSRodney W. Grimes 			 * A timestamp received in a SYN makes
1857df8bae1dSRodney W. Grimes 			 * it ok to send timestamp requests and replies.
1858df8bae1dSRodney W. Grimes 			 */
1859df8bae1dSRodney W. Grimes 			if (ti->ti_flags & TH_SYN) {
1860df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_RCVD_TSTMP;
1861a0292f23SGarrett Wollman 				tp->ts_recent = to->to_tsval;
1862df8bae1dSRodney W. Grimes 				tp->ts_recent_age = tcp_now;
1863df8bae1dSRodney W. Grimes 			}
1864df8bae1dSRodney W. Grimes 			break;
1865a0292f23SGarrett Wollman 		case TCPOPT_CC:
1866a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
1867a0292f23SGarrett Wollman 				continue;
186840db8ef7SAndras Olah 			to->to_flag |= TOF_CC;
1869a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
1870a0292f23SGarrett Wollman 			    (char *)&to->to_cc, sizeof(to->to_cc));
1871a0292f23SGarrett Wollman 			NTOHL(to->to_cc);
1872a0292f23SGarrett Wollman 			/*
1873a0292f23SGarrett Wollman 			 * A CC or CC.new option received in a SYN makes
1874a0292f23SGarrett Wollman 			 * it ok to send CC in subsequent segments.
1875a0292f23SGarrett Wollman 			 */
1876a0292f23SGarrett Wollman 			if (ti->ti_flags & TH_SYN)
1877a0292f23SGarrett Wollman 				tp->t_flags |= TF_RCVD_CC;
1878a0292f23SGarrett Wollman 			break;
1879a0292f23SGarrett Wollman 		case TCPOPT_CCNEW:
1880a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
1881a0292f23SGarrett Wollman 				continue;
1882a0292f23SGarrett Wollman 			if (!(ti->ti_flags & TH_SYN))
1883a0292f23SGarrett Wollman 				continue;
1884a0292f23SGarrett Wollman 			to->to_flag |= TOF_CCNEW;
1885a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
1886a0292f23SGarrett Wollman 			    (char *)&to->to_cc, sizeof(to->to_cc));
1887a0292f23SGarrett Wollman 			NTOHL(to->to_cc);
1888a0292f23SGarrett Wollman 			/*
1889a0292f23SGarrett Wollman 			 * A CC or CC.new option received in a SYN makes
1890a0292f23SGarrett Wollman 			 * it ok to send CC in subsequent segments.
1891a0292f23SGarrett Wollman 			 */
1892a0292f23SGarrett Wollman 			tp->t_flags |= TF_RCVD_CC;
1893a0292f23SGarrett Wollman 			break;
1894a0292f23SGarrett Wollman 		case TCPOPT_CCECHO:
1895a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
1896a0292f23SGarrett Wollman 				continue;
1897a0292f23SGarrett Wollman 			if (!(ti->ti_flags & TH_SYN))
1898a0292f23SGarrett Wollman 				continue;
1899a0292f23SGarrett Wollman 			to->to_flag |= TOF_CCECHO;
1900a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
1901a0292f23SGarrett Wollman 			    (char *)&to->to_ccecho, sizeof(to->to_ccecho));
1902a0292f23SGarrett Wollman 			NTOHL(to->to_ccecho);
1903a0292f23SGarrett Wollman 			break;
1904df8bae1dSRodney W. Grimes 		}
1905df8bae1dSRodney W. Grimes 	}
1906a0292f23SGarrett Wollman 	if (ti->ti_flags & TH_SYN)
1907a0292f23SGarrett Wollman 		tcp_mss(tp, mss);	/* sets t_maxseg */
1908df8bae1dSRodney W. Grimes }
1909df8bae1dSRodney W. Grimes 
1910df8bae1dSRodney W. Grimes /*
1911df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
1912df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
1913df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
1914df8bae1dSRodney W. Grimes  * sequencing purposes.
1915df8bae1dSRodney W. Grimes  */
19160312fbe9SPoul-Henning Kamp static void
1917df8bae1dSRodney W. Grimes tcp_pulloutofband(so, ti, m)
1918df8bae1dSRodney W. Grimes 	struct socket *so;
1919df8bae1dSRodney W. Grimes 	struct tcpiphdr *ti;
1920df8bae1dSRodney W. Grimes 	register struct mbuf *m;
1921df8bae1dSRodney W. Grimes {
1922df8bae1dSRodney W. Grimes 	int cnt = ti->ti_urp - 1;
1923df8bae1dSRodney W. Grimes 
1924df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
1925df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
1926df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
1927df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
1928df8bae1dSRodney W. Grimes 
1929df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
1930df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
1931df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
1932df8bae1dSRodney W. Grimes 			m->m_len--;
1933df8bae1dSRodney W. Grimes 			return;
1934df8bae1dSRodney W. Grimes 		}
1935df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
1936df8bae1dSRodney W. Grimes 		m = m->m_next;
1937df8bae1dSRodney W. Grimes 		if (m == 0)
1938df8bae1dSRodney W. Grimes 			break;
1939df8bae1dSRodney W. Grimes 	}
1940df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
1941df8bae1dSRodney W. Grimes }
1942df8bae1dSRodney W. Grimes 
1943df8bae1dSRodney W. Grimes /*
1944df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
1945df8bae1dSRodney W. Grimes  * and update averages and current timeout.
1946df8bae1dSRodney W. Grimes  */
19470312fbe9SPoul-Henning Kamp static void
1948df8bae1dSRodney W. Grimes tcp_xmit_timer(tp, rtt)
1949df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1950df8bae1dSRodney W. Grimes 	short rtt;
1951df8bae1dSRodney W. Grimes {
1952233e8c18SGarrett Wollman 	register int delta;
1953233e8c18SGarrett Wollman 
1954233e8c18SGarrett Wollman 	tcpstat.tcps_rttupdated++;
1955233e8c18SGarrett Wollman 	tp->t_rttupdated++;
1956233e8c18SGarrett Wollman 	if (tp->t_srtt != 0) {
1957233e8c18SGarrett Wollman 		/*
1958233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
1959233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
1960233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
1961233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
1962233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
1963233e8c18SGarrett Wollman 		 */
1964233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
1965233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
1966233e8c18SGarrett Wollman 
1967233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
1968233e8c18SGarrett Wollman 			tp->t_srtt = 1;
1969233e8c18SGarrett Wollman 
1970233e8c18SGarrett Wollman 		/*
1971233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
1972233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
1973233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
1974233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
1975233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
1976233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
1977233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
1978233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
1979233e8c18SGarrett Wollman 		 */
1980233e8c18SGarrett Wollman 		if (delta < 0)
1981233e8c18SGarrett Wollman 			delta = -delta;
1982233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
1983233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
1984233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
1985233e8c18SGarrett Wollman 	} else {
1986233e8c18SGarrett Wollman 		/*
1987233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
1988233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
1989233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
1990233e8c18SGarrett Wollman 		 */
1991233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
1992233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
1993233e8c18SGarrett Wollman 	}
1994df8bae1dSRodney W. Grimes 	tp->t_rtt = 0;
1995df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
1996df8bae1dSRodney W. Grimes 
1997df8bae1dSRodney W. Grimes 	/*
1998df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
1999df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
2000df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
2001df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
2002df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
2003df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
2004df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
2005df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
2006df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
2007df8bae1dSRodney W. Grimes 	 */
2008233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
20099e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
2010df8bae1dSRodney W. Grimes 
2011df8bae1dSRodney W. Grimes 	/*
2012df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
2013df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
2014df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
2015df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
2016df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
2017df8bae1dSRodney W. Grimes 	 */
2018df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
2019df8bae1dSRodney W. Grimes }
2020df8bae1dSRodney W. Grimes 
2021df8bae1dSRodney W. Grimes /*
2022df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
2023df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
2024df8bae1dSRodney W. Grimes  * If none, use an mss that can be handled on the outgoing
2025df8bae1dSRodney W. Grimes  * interface without forcing IP to fragment; if bigger than
2026df8bae1dSRodney W. Grimes  * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
2027df8bae1dSRodney W. Grimes  * to utilize large mbufs.  If no route is found, route has no mtu,
2028df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
2029df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
2030df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
2031df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
2032df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
2033df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
2034df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
2035a0292f23SGarrett Wollman  *
2036a0292f23SGarrett Wollman  * Also take into account the space needed for options that we
2037a0292f23SGarrett Wollman  * send regularly.  Make maxseg shorter by that amount to assure
2038a0292f23SGarrett Wollman  * that we can send maxseg amount of data even when the options
2039a0292f23SGarrett Wollman  * are present.  Store the upper limit of the length of options plus
2040a0292f23SGarrett Wollman  * data in maxopd.
2041a0292f23SGarrett Wollman  *
2042a0292f23SGarrett Wollman  * NOTE that this routine is only called when we process an incoming
2043a0292f23SGarrett Wollman  * segment, for outgoing segments only tcp_mssopt is called.
2044a0292f23SGarrett Wollman  *
2045a0292f23SGarrett Wollman  * In case of T/TCP, we call this routine during implicit connection
2046a0292f23SGarrett Wollman  * setup as well (offer = -1), to initialize maxseg from the cached
2047a0292f23SGarrett Wollman  * MSS of our peer.
2048df8bae1dSRodney W. Grimes  */
2049a0292f23SGarrett Wollman void
2050df8bae1dSRodney W. Grimes tcp_mss(tp, offer)
2051a0292f23SGarrett Wollman 	struct tcpcb *tp;
2052a0292f23SGarrett Wollman 	int offer;
2053df8bae1dSRodney W. Grimes {
2054df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
2055df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
2056df8bae1dSRodney W. Grimes 	register int rtt, mss;
2057df8bae1dSRodney W. Grimes 	u_long bufsize;
2058df8bae1dSRodney W. Grimes 	struct inpcb *inp;
2059df8bae1dSRodney W. Grimes 	struct socket *so;
2060a0292f23SGarrett Wollman 	struct rmxp_tao *taop;
2061a0292f23SGarrett Wollman 	int origoffer = offer;
2062df8bae1dSRodney W. Grimes 
2063df8bae1dSRodney W. Grimes 	inp = tp->t_inpcb;
2064a0292f23SGarrett Wollman 	if ((rt = tcp_rtlookup(inp)) == NULL) {
2065a0292f23SGarrett Wollman 		tp->t_maxopd = tp->t_maxseg = tcp_mssdflt;
2066a0292f23SGarrett Wollman 		return;
2067df8bae1dSRodney W. Grimes 	}
2068df8bae1dSRodney W. Grimes 	ifp = rt->rt_ifp;
2069df8bae1dSRodney W. Grimes 	so = inp->inp_socket;
2070df8bae1dSRodney W. Grimes 
2071a0292f23SGarrett Wollman 	taop = rmx_taop(rt->rt_rmx);
2072a0292f23SGarrett Wollman 	/*
2073a0292f23SGarrett Wollman 	 * Offer == -1 means that we didn't receive SYN yet,
2074a0292f23SGarrett Wollman 	 * use cached value in that case;
2075a0292f23SGarrett Wollman 	 */
2076a0292f23SGarrett Wollman 	if (offer == -1)
2077a0292f23SGarrett Wollman 		offer = taop->tao_mssopt;
2078a0292f23SGarrett Wollman 	/*
2079a0292f23SGarrett Wollman 	 * Offer == 0 means that there was no MSS on the SYN segment,
2080a0292f23SGarrett Wollman 	 * in this case we use tcp_mssdflt.
2081a0292f23SGarrett Wollman 	 */
2082a0292f23SGarrett Wollman 	if (offer == 0)
2083a0292f23SGarrett Wollman 		offer = tcp_mssdflt;
2084a0292f23SGarrett Wollman 	else
2085a0292f23SGarrett Wollman 		/*
2086a0292f23SGarrett Wollman 		 * Sanity check: make sure that maxopd will be large
2087a0292f23SGarrett Wollman 		 * enough to allow some data on segments even is the
2088a0292f23SGarrett Wollman 		 * all the option space is used (40bytes).  Otherwise
2089a0292f23SGarrett Wollman 		 * funny things may happen in tcp_output.
2090a0292f23SGarrett Wollman 		 */
2091a0292f23SGarrett Wollman 		offer = max(offer, 64);
2092a0292f23SGarrett Wollman 	taop->tao_mssopt = offer;
2093a0292f23SGarrett Wollman 
2094df8bae1dSRodney W. Grimes 	/*
2095df8bae1dSRodney W. Grimes 	 * While we're here, check if there's an initial rtt
2096df8bae1dSRodney W. Grimes 	 * or rttvar.  Convert from the route-table units
2097df8bae1dSRodney W. Grimes 	 * to scaled multiples of the slow timeout timer.
2098df8bae1dSRodney W. Grimes 	 */
2099df8bae1dSRodney W. Grimes 	if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
2100df8bae1dSRodney W. Grimes 		/*
2101a0292f23SGarrett Wollman 		 * XXX the lock bit for RTT indicates that the value
2102df8bae1dSRodney W. Grimes 		 * is also a minimum value; this is subject to time.
2103df8bae1dSRodney W. Grimes 		 */
2104df8bae1dSRodney W. Grimes 		if (rt->rt_rmx.rmx_locks & RTV_RTT)
2105df8bae1dSRodney W. Grimes 			tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ);
2106df8bae1dSRodney W. Grimes 		tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
2107dd224982SGarrett Wollman 		tcpstat.tcps_usedrtt++;
2108dd224982SGarrett Wollman 		if (rt->rt_rmx.rmx_rttvar) {
2109df8bae1dSRodney W. Grimes 			tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
2110df8bae1dSRodney W. Grimes 			    (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
2111dd224982SGarrett Wollman 			tcpstat.tcps_usedrttvar++;
2112dd224982SGarrett Wollman 		} else {
2113df8bae1dSRodney W. Grimes 			/* default variation is +- 1 rtt */
2114df8bae1dSRodney W. Grimes 			tp->t_rttvar =
2115df8bae1dSRodney W. Grimes 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
2116dd224982SGarrett Wollman 		}
2117df8bae1dSRodney W. Grimes 		TCPT_RANGESET(tp->t_rxtcur,
2118df8bae1dSRodney W. Grimes 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
2119df8bae1dSRodney W. Grimes 		    tp->t_rttmin, TCPTV_REXMTMAX);
2120df8bae1dSRodney W. Grimes 	}
2121df8bae1dSRodney W. Grimes 	/*
2122df8bae1dSRodney W. Grimes 	 * if there's an mtu associated with the route, use it
2123df8bae1dSRodney W. Grimes 	 */
2124df8bae1dSRodney W. Grimes 	if (rt->rt_rmx.rmx_mtu)
2125df8bae1dSRodney W. Grimes 		mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
2126df8bae1dSRodney W. Grimes 	else
2127df8bae1dSRodney W. Grimes 	{
2128df8bae1dSRodney W. Grimes 		mss = ifp->if_mtu - sizeof(struct tcpiphdr);
2129a0292f23SGarrett Wollman 		if (!in_localaddr(inp->inp_faddr))
2130a0292f23SGarrett Wollman 			mss = min(mss, tcp_mssdflt);
2131a0292f23SGarrett Wollman 	}
2132a0292f23SGarrett Wollman 	mss = min(mss, offer);
2133a0292f23SGarrett Wollman 	/*
2134a0292f23SGarrett Wollman 	 * maxopd stores the maximum length of data AND options
2135a0292f23SGarrett Wollman 	 * in a segment; maxseg is the amount of data in a normal
2136a0292f23SGarrett Wollman 	 * segment.  We need to store this value (maxopd) apart
2137a0292f23SGarrett Wollman 	 * from maxseg, because now every segment carries options
2138a0292f23SGarrett Wollman 	 * and thus we normally have somewhat less data in segments.
2139a0292f23SGarrett Wollman 	 */
2140a0292f23SGarrett Wollman 	tp->t_maxopd = mss;
2141a0292f23SGarrett Wollman 
2142a0292f23SGarrett Wollman 	/*
2143a0292f23SGarrett Wollman 	 * In case of T/TCP, origoffer==-1 indicates, that no segments
2144a0292f23SGarrett Wollman 	 * were received yet.  In this case we just guess, otherwise
2145a0292f23SGarrett Wollman 	 * we do the same as before T/TCP.
2146a0292f23SGarrett Wollman 	 */
2147a0292f23SGarrett Wollman  	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
2148a0292f23SGarrett Wollman 	    (origoffer == -1 ||
2149a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
2150a0292f23SGarrett Wollman 		mss -= TCPOLEN_TSTAMP_APPA;
2151a0292f23SGarrett Wollman  	if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
2152a0292f23SGarrett Wollman 	    (origoffer == -1 ||
2153a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC))
2154a0292f23SGarrett Wollman 		mss -= TCPOLEN_CC_APPA;
2155a0292f23SGarrett Wollman 
2156df8bae1dSRodney W. Grimes #if	(MCLBYTES & (MCLBYTES - 1)) == 0
2157df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
2158df8bae1dSRodney W. Grimes 			mss &= ~(MCLBYTES-1);
2159df8bae1dSRodney W. Grimes #else
2160df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
2161df8bae1dSRodney W. Grimes 			mss = mss / MCLBYTES * MCLBYTES;
2162df8bae1dSRodney W. Grimes #endif
2163df8bae1dSRodney W. Grimes 	/*
2164df8bae1dSRodney W. Grimes 	 * If there's a pipesize, change the socket buffer
2165df8bae1dSRodney W. Grimes 	 * to that size.  Make the socket buffers an integral
2166df8bae1dSRodney W. Grimes 	 * number of mss units; if the mss is larger than
2167df8bae1dSRodney W. Grimes 	 * the socket buffer, decrease the mss.
2168df8bae1dSRodney W. Grimes 	 */
2169df8bae1dSRodney W. Grimes #ifdef RTV_SPIPE
2170df8bae1dSRodney W. Grimes 	if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
2171df8bae1dSRodney W. Grimes #endif
2172df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
2173df8bae1dSRodney W. Grimes 	if (bufsize < mss)
2174df8bae1dSRodney W. Grimes 		mss = bufsize;
2175df8bae1dSRodney W. Grimes 	else {
2176df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
2177df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
2178df8bae1dSRodney W. Grimes 			bufsize = sb_max;
2179df8bae1dSRodney W. Grimes 		(void)sbreserve(&so->so_snd, bufsize);
2180df8bae1dSRodney W. Grimes 	}
2181df8bae1dSRodney W. Grimes 	tp->t_maxseg = mss;
2182df8bae1dSRodney W. Grimes 
2183df8bae1dSRodney W. Grimes #ifdef RTV_RPIPE
2184df8bae1dSRodney W. Grimes 	if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
2185df8bae1dSRodney W. Grimes #endif
2186df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
2187df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
2188df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
2189df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
2190df8bae1dSRodney W. Grimes 			bufsize = sb_max;
2191df8bae1dSRodney W. Grimes 		(void)sbreserve(&so->so_rcv, bufsize);
2192df8bae1dSRodney W. Grimes 	}
2193a0292f23SGarrett Wollman 	/*
2194a0292f23SGarrett Wollman 	 * Don't force slow-start on local network.
2195a0292f23SGarrett Wollman 	 */
2196a0292f23SGarrett Wollman 	if (!in_localaddr(inp->inp_faddr))
2197df8bae1dSRodney W. Grimes 		tp->snd_cwnd = mss;
2198df8bae1dSRodney W. Grimes 
2199df8bae1dSRodney W. Grimes 	if (rt->rt_rmx.rmx_ssthresh) {
2200df8bae1dSRodney W. Grimes 		/*
2201df8bae1dSRodney W. Grimes 		 * There's some sort of gateway or interface
2202df8bae1dSRodney W. Grimes 		 * buffer limit on the path.  Use this to set
2203df8bae1dSRodney W. Grimes 		 * the slow start threshhold, but set the
2204df8bae1dSRodney W. Grimes 		 * threshold to no less than 2*mss.
2205df8bae1dSRodney W. Grimes 		 */
2206df8bae1dSRodney W. Grimes 		tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
2207dd224982SGarrett Wollman 		tcpstat.tcps_usedssthresh++;
2208df8bae1dSRodney W. Grimes 	}
2209a0292f23SGarrett Wollman }
2210a0292f23SGarrett Wollman 
2211a0292f23SGarrett Wollman /*
2212a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
2213a0292f23SGarrett Wollman  */
2214a0292f23SGarrett Wollman int
2215a0292f23SGarrett Wollman tcp_mssopt(tp)
2216a0292f23SGarrett Wollman 	struct tcpcb *tp;
2217a0292f23SGarrett Wollman {
2218a0292f23SGarrett Wollman 	struct rtentry *rt;
2219a0292f23SGarrett Wollman 
2220a0292f23SGarrett Wollman 	rt = tcp_rtlookup(tp->t_inpcb);
2221a0292f23SGarrett Wollman 	if (rt == NULL)
2222a0292f23SGarrett Wollman 		return tcp_mssdflt;
2223a0292f23SGarrett Wollman 
2224a0292f23SGarrett Wollman 	return rt->rt_ifp->if_mtu - sizeof(struct tcpiphdr);
2225df8bae1dSRodney W. Grimes }
2226