xref: /freebsd/sys/netinet/tcp_input.c (revision a0292f237572bd29b1bd4b376a2198c6f404e349)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994
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  *
33df8bae1dSRodney W. Grimes  *	@(#)tcp_input.c	8.5 (Berkeley) 4/10/94
34a0292f23SGarrett Wollman  * $Id: tcp_input.c,v 1.10 1994/10/13 18:36:32 wollman Exp $
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes #ifndef TUBA_INCLUDE
38df8bae1dSRodney W. Grimes #include <sys/param.h>
39df8bae1dSRodney W. Grimes #include <sys/systm.h>
40df8bae1dSRodney W. Grimes #include <sys/malloc.h>
41df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
42df8bae1dSRodney W. Grimes #include <sys/protosw.h>
43df8bae1dSRodney W. Grimes #include <sys/socket.h>
44df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
45df8bae1dSRodney W. Grimes #include <sys/errno.h>
46df8bae1dSRodney W. Grimes 
47df8bae1dSRodney W. Grimes #include <net/if.h>
48df8bae1dSRodney W. Grimes #include <net/route.h>
49df8bae1dSRodney W. Grimes 
50df8bae1dSRodney W. Grimes #include <netinet/in.h>
51df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
52df8bae1dSRodney W. Grimes #include <netinet/ip.h>
53df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
54df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
55df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
56df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
57df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
58df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
59df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
60df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
61610ee2f9SDavid Greenman #ifdef TCPDEBUG
62df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
63610ee2f9SDavid Greenman struct	tcpiphdr tcp_saveti;
64610ee2f9SDavid Greenman #endif
65df8bae1dSRodney W. Grimes 
66df8bae1dSRodney W. Grimes int	tcprexmtthresh = 3;
67df8bae1dSRodney W. Grimes struct	inpcb *tcp_last_inpcb = &tcb;
68df8bae1dSRodney W. Grimes 
69df8bae1dSRodney W. Grimes #endif /* TUBA_INCLUDE */
70df8bae1dSRodney W. Grimes 
71df8bae1dSRodney W. Grimes /*
72df8bae1dSRodney W. Grimes  * Insert segment ti into reassembly queue of tcp with
73df8bae1dSRodney W. Grimes  * control block tp.  Return TH_FIN if reassembly now includes
74df8bae1dSRodney W. Grimes  * a segment with FIN.  The macro form does the common case inline
75df8bae1dSRodney W. Grimes  * (segment is the next to be received on an established connection,
76df8bae1dSRodney W. Grimes  * and the queue is empty), avoiding linkage into and removal
77df8bae1dSRodney W. Grimes  * from the queue and repetition of various conversions.
78df8bae1dSRodney W. Grimes  * Set DELACK for segments received in order, but ack immediately
79df8bae1dSRodney W. Grimes  * when segments are out of order (so fast retransmit can work).
80df8bae1dSRodney W. Grimes  */
81df8bae1dSRodney W. Grimes #define	TCP_REASS(tp, ti, m, so, flags) { \
82df8bae1dSRodney W. Grimes 	if ((ti)->ti_seq == (tp)->rcv_nxt && \
83df8bae1dSRodney W. Grimes 	    (tp)->seg_next == (struct tcpiphdr *)(tp) && \
84df8bae1dSRodney W. Grimes 	    (tp)->t_state == TCPS_ESTABLISHED) { \
85df8bae1dSRodney W. Grimes 		tp->t_flags |= TF_DELACK; \
86df8bae1dSRodney W. Grimes 		(tp)->rcv_nxt += (ti)->ti_len; \
87df8bae1dSRodney W. Grimes 		flags = (ti)->ti_flags & TH_FIN; \
88df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvpack++;\
89df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvbyte += (ti)->ti_len;\
90df8bae1dSRodney W. Grimes 		sbappend(&(so)->so_rcv, (m)); \
91df8bae1dSRodney W. Grimes 		sorwakeup(so); \
92df8bae1dSRodney W. Grimes 	} else { \
93df8bae1dSRodney W. Grimes 		(flags) = tcp_reass((tp), (ti), (m)); \
94df8bae1dSRodney W. Grimes 		tp->t_flags |= TF_ACKNOW; \
95df8bae1dSRodney W. Grimes 	} \
96df8bae1dSRodney W. Grimes }
97df8bae1dSRodney W. Grimes #ifndef TUBA_INCLUDE
98df8bae1dSRodney W. Grimes 
99df8bae1dSRodney W. Grimes int
100df8bae1dSRodney W. Grimes tcp_reass(tp, ti, m)
101df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
102df8bae1dSRodney W. Grimes 	register struct tcpiphdr *ti;
103df8bae1dSRodney W. Grimes 	struct mbuf *m;
104df8bae1dSRodney W. Grimes {
105df8bae1dSRodney W. Grimes 	register struct tcpiphdr *q;
106df8bae1dSRodney W. Grimes 	struct socket *so = tp->t_inpcb->inp_socket;
107df8bae1dSRodney W. Grimes 	int flags;
108df8bae1dSRodney W. Grimes 
109df8bae1dSRodney W. Grimes 	/*
110df8bae1dSRodney W. Grimes 	 * Call with ti==0 after become established to
111df8bae1dSRodney W. Grimes 	 * force pre-ESTABLISHED data up to user socket.
112df8bae1dSRodney W. Grimes 	 */
113df8bae1dSRodney W. Grimes 	if (ti == 0)
114df8bae1dSRodney W. Grimes 		goto present;
115df8bae1dSRodney W. Grimes 
116df8bae1dSRodney W. Grimes 	/*
117df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
118df8bae1dSRodney W. Grimes 	 */
119df8bae1dSRodney W. Grimes 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
120df8bae1dSRodney W. Grimes 	    q = (struct tcpiphdr *)q->ti_next)
121df8bae1dSRodney W. Grimes 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
122df8bae1dSRodney W. Grimes 			break;
123df8bae1dSRodney W. Grimes 
124df8bae1dSRodney W. Grimes 	/*
125df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
126df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
127df8bae1dSRodney W. Grimes 	 * segment.  If it provides all of our data, drop us.
128df8bae1dSRodney W. Grimes 	 */
129df8bae1dSRodney W. Grimes 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
130df8bae1dSRodney W. Grimes 		register int i;
131df8bae1dSRodney W. Grimes 		q = (struct tcpiphdr *)q->ti_prev;
132df8bae1dSRodney W. Grimes 		/* conversion to int (in i) handles seq wraparound */
133df8bae1dSRodney W. Grimes 		i = q->ti_seq + q->ti_len - ti->ti_seq;
134df8bae1dSRodney W. Grimes 		if (i > 0) {
135df8bae1dSRodney W. Grimes 			if (i >= ti->ti_len) {
136df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvduppack++;
137df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvdupbyte += ti->ti_len;
138df8bae1dSRodney W. Grimes 				m_freem(m);
139a0292f23SGarrett Wollman #ifdef TTCP
140a0292f23SGarrett Wollman 				/*
141a0292f23SGarrett Wollman 				 * Try to present any queued data
142a0292f23SGarrett Wollman 				 * at the left window edge to the user.
143a0292f23SGarrett Wollman 				 * This is needed after the 3-WHS
144a0292f23SGarrett Wollman 				 * completes.
145a0292f23SGarrett Wollman 				 */
146a0292f23SGarrett Wollman 				goto present;	/* ??? */
147a0292f23SGarrett Wollman #else
148df8bae1dSRodney W. Grimes 				return (0);
149a0292f23SGarrett Wollman #endif
150df8bae1dSRodney W. Grimes 			}
151df8bae1dSRodney W. Grimes 			m_adj(m, i);
152df8bae1dSRodney W. Grimes 			ti->ti_len -= i;
153df8bae1dSRodney W. Grimes 			ti->ti_seq += i;
154df8bae1dSRodney W. Grimes 		}
155df8bae1dSRodney W. Grimes 		q = (struct tcpiphdr *)(q->ti_next);
156df8bae1dSRodney W. Grimes 	}
157df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvoopack++;
158df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvoobyte += ti->ti_len;
159df8bae1dSRodney W. Grimes 	REASS_MBUF(ti) = m;		/* XXX */
160df8bae1dSRodney W. Grimes 
161df8bae1dSRodney W. Grimes 	/*
162df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
163df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
164df8bae1dSRodney W. Grimes 	 */
165df8bae1dSRodney W. Grimes 	while (q != (struct tcpiphdr *)tp) {
166df8bae1dSRodney W. Grimes 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
167df8bae1dSRodney W. Grimes 		if (i <= 0)
168df8bae1dSRodney W. Grimes 			break;
169df8bae1dSRodney W. Grimes 		if (i < q->ti_len) {
170df8bae1dSRodney W. Grimes 			q->ti_seq += i;
171df8bae1dSRodney W. Grimes 			q->ti_len -= i;
172df8bae1dSRodney W. Grimes 			m_adj(REASS_MBUF(q), i);
173df8bae1dSRodney W. Grimes 			break;
174df8bae1dSRodney W. Grimes 		}
175df8bae1dSRodney W. Grimes 		q = (struct tcpiphdr *)q->ti_next;
176df8bae1dSRodney W. Grimes 		m = REASS_MBUF((struct tcpiphdr *)q->ti_prev);
177df8bae1dSRodney W. Grimes 		remque(q->ti_prev);
178df8bae1dSRodney W. Grimes 		m_freem(m);
179df8bae1dSRodney W. Grimes 	}
180df8bae1dSRodney W. Grimes 
181df8bae1dSRodney W. Grimes 	/*
182df8bae1dSRodney W. Grimes 	 * Stick new segment in its place.
183df8bae1dSRodney W. Grimes 	 */
184df8bae1dSRodney W. Grimes 	insque(ti, q->ti_prev);
185df8bae1dSRodney W. Grimes 
186df8bae1dSRodney W. Grimes present:
187df8bae1dSRodney W. Grimes 	/*
188df8bae1dSRodney W. Grimes 	 * Present data to user, advancing rcv_nxt through
189df8bae1dSRodney W. Grimes 	 * completed sequence space.
190df8bae1dSRodney W. Grimes 	 */
191a0292f23SGarrett Wollman 	if (!TCPS_HAVEESTABLISHED(tp->t_state))
192df8bae1dSRodney W. Grimes 		return (0);
193df8bae1dSRodney W. Grimes 	ti = tp->seg_next;
194df8bae1dSRodney W. Grimes 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
195df8bae1dSRodney W. Grimes 		return (0);
196df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
197df8bae1dSRodney W. Grimes 		return (0);
198df8bae1dSRodney W. Grimes 	do {
199df8bae1dSRodney W. Grimes 		tp->rcv_nxt += ti->ti_len;
200df8bae1dSRodney W. Grimes 		flags = ti->ti_flags & TH_FIN;
201df8bae1dSRodney W. Grimes 		remque(ti);
202df8bae1dSRodney W. Grimes 		m = REASS_MBUF(ti);
203df8bae1dSRodney W. Grimes 		ti = (struct tcpiphdr *)ti->ti_next;
204df8bae1dSRodney W. Grimes 		if (so->so_state & SS_CANTRCVMORE)
205df8bae1dSRodney W. Grimes 			m_freem(m);
206df8bae1dSRodney W. Grimes 		else
207df8bae1dSRodney W. Grimes 			sbappend(&so->so_rcv, m);
208df8bae1dSRodney W. Grimes 	} while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
209df8bae1dSRodney W. Grimes 	sorwakeup(so);
210df8bae1dSRodney W. Grimes 	return (flags);
211df8bae1dSRodney W. Grimes }
212df8bae1dSRodney W. Grimes 
213df8bae1dSRodney W. Grimes /*
214df8bae1dSRodney W. Grimes  * TCP input routine, follows pages 65-76 of the
215df8bae1dSRodney W. Grimes  * protocol specification dated September, 1981 very closely.
216df8bae1dSRodney W. Grimes  */
217df8bae1dSRodney W. Grimes void
218df8bae1dSRodney W. Grimes tcp_input(m, iphlen)
219df8bae1dSRodney W. Grimes 	register struct mbuf *m;
220df8bae1dSRodney W. Grimes 	int iphlen;
221df8bae1dSRodney W. Grimes {
222df8bae1dSRodney W. Grimes 	register struct tcpiphdr *ti;
223df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
224df8bae1dSRodney W. Grimes 	caddr_t optp = NULL;
22526f9a767SRodney W. Grimes 	int optlen = 0;
226df8bae1dSRodney W. Grimes 	int len, tlen, off;
227df8bae1dSRodney W. Grimes 	register struct tcpcb *tp = 0;
228df8bae1dSRodney W. Grimes 	register int tiflags;
22926f9a767SRodney W. Grimes 	struct socket *so = 0;
230df8bae1dSRodney W. Grimes 	int todrop, acked, ourfinisacked, needoutput = 0;
231df8bae1dSRodney W. Grimes 	struct in_addr laddr;
232df8bae1dSRodney W. Grimes 	int dropsocket = 0;
233df8bae1dSRodney W. Grimes 	int iss = 0;
234a0292f23SGarrett Wollman #ifdef TTCP
235a0292f23SGarrett Wollman 	u_long tiwin;
236a0292f23SGarrett Wollman 	struct tcpopt to;		/* options in this segment */
237a0292f23SGarrett Wollman 	struct rmxp_tao *taop;		/* pointer to our TAO cache entry */
238a0292f23SGarrett Wollman 	struct rmxp_tao	tao_noncached;	/* in case there's no cached entry */
239a0292f23SGarrett Wollman #else
240df8bae1dSRodney W. Grimes 	u_long tiwin, ts_val, ts_ecr;
241df8bae1dSRodney W. Grimes 	int ts_present = 0;
242a0292f23SGarrett Wollman #endif
243610ee2f9SDavid Greenman #ifdef TCPDEBUG
244610ee2f9SDavid Greenman 	short ostate = 0;
245610ee2f9SDavid Greenman #endif
246df8bae1dSRodney W. Grimes 
247a0292f23SGarrett Wollman #ifdef TTCP
248a0292f23SGarrett Wollman 	bzero((char *)&to, sizeof(to));
249a0292f23SGarrett Wollman #endif
250a0292f23SGarrett Wollman 
251df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvtotal++;
252df8bae1dSRodney W. Grimes 	/*
253df8bae1dSRodney W. Grimes 	 * Get IP and TCP header together in first mbuf.
254df8bae1dSRodney W. Grimes 	 * Note: IP leaves IP header in first mbuf.
255df8bae1dSRodney W. Grimes 	 */
256df8bae1dSRodney W. Grimes 	ti = mtod(m, struct tcpiphdr *);
257df8bae1dSRodney W. Grimes 	if (iphlen > sizeof (struct ip))
258df8bae1dSRodney W. Grimes 		ip_stripoptions(m, (struct mbuf *)0);
259df8bae1dSRodney W. Grimes 	if (m->m_len < sizeof (struct tcpiphdr)) {
260df8bae1dSRodney W. Grimes 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
261df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvshort++;
262df8bae1dSRodney W. Grimes 			return;
263df8bae1dSRodney W. Grimes 		}
264df8bae1dSRodney W. Grimes 		ti = mtod(m, struct tcpiphdr *);
265df8bae1dSRodney W. Grimes 	}
266df8bae1dSRodney W. Grimes 
267df8bae1dSRodney W. Grimes 	/*
268df8bae1dSRodney W. Grimes 	 * Checksum extended TCP header and data.
269df8bae1dSRodney W. Grimes 	 */
270df8bae1dSRodney W. Grimes 	tlen = ((struct ip *)ti)->ip_len;
271df8bae1dSRodney W. Grimes 	len = sizeof (struct ip) + tlen;
272df8bae1dSRodney W. Grimes 	ti->ti_next = ti->ti_prev = 0;
273df8bae1dSRodney W. Grimes 	ti->ti_x1 = 0;
274df8bae1dSRodney W. Grimes 	ti->ti_len = (u_short)tlen;
275df8bae1dSRodney W. Grimes 	HTONS(ti->ti_len);
276623ae52eSPoul-Henning Kamp 	ti->ti_sum = in_cksum(m, len);
277623ae52eSPoul-Henning Kamp 	if (ti->ti_sum) {
278df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvbadsum++;
279df8bae1dSRodney W. Grimes 		goto drop;
280df8bae1dSRodney W. Grimes 	}
281df8bae1dSRodney W. Grimes #endif /* TUBA_INCLUDE */
282df8bae1dSRodney W. Grimes 
283df8bae1dSRodney W. Grimes 	/*
284df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
285df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
286df8bae1dSRodney W. Grimes 	 */
287df8bae1dSRodney W. Grimes 	off = ti->ti_off << 2;
288df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
289df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvbadoff++;
290df8bae1dSRodney W. Grimes 		goto drop;
291df8bae1dSRodney W. Grimes 	}
292df8bae1dSRodney W. Grimes 	tlen -= off;
293df8bae1dSRodney W. Grimes 	ti->ti_len = tlen;
294df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
295df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof(struct ip) + off) {
296df8bae1dSRodney W. Grimes 			if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
297df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvshort++;
298df8bae1dSRodney W. Grimes 				return;
299df8bae1dSRodney W. Grimes 			}
300df8bae1dSRodney W. Grimes 			ti = mtod(m, struct tcpiphdr *);
301df8bae1dSRodney W. Grimes 		}
302df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
303df8bae1dSRodney W. Grimes 		optp = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
304df8bae1dSRodney W. Grimes 		/*
305df8bae1dSRodney W. Grimes 		 * Do quick retrieval of timestamp options ("options
306df8bae1dSRodney W. Grimes 		 * prediction?").  If timestamp is the only option and it's
307df8bae1dSRodney W. Grimes 		 * formatted as recommended in RFC 1323 appendix A, we
308df8bae1dSRodney W. Grimes 		 * quickly get the values now and not bother calling
309df8bae1dSRodney W. Grimes 		 * tcp_dooptions(), etc.
310df8bae1dSRodney W. Grimes 		 */
311df8bae1dSRodney W. Grimes 		if ((optlen == TCPOLEN_TSTAMP_APPA ||
312df8bae1dSRodney W. Grimes 		     (optlen > TCPOLEN_TSTAMP_APPA &&
313df8bae1dSRodney W. Grimes 			optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
314df8bae1dSRodney W. Grimes 		     *(u_long *)optp == htonl(TCPOPT_TSTAMP_HDR) &&
315df8bae1dSRodney W. Grimes 		     (ti->ti_flags & TH_SYN) == 0) {
316a0292f23SGarrett Wollman #ifdef TTCP
317a0292f23SGarrett Wollman 			to.to_flag |= TOF_TS;
318a0292f23SGarrett Wollman 			to.to_tsval = ntohl(*(u_long *)(optp + 4));
319a0292f23SGarrett Wollman 			to.to_tsecr = ntohl(*(u_long *)(optp + 8));
320a0292f23SGarrett Wollman #else
321df8bae1dSRodney W. Grimes 			ts_present = 1;
322df8bae1dSRodney W. Grimes 			ts_val = ntohl(*(u_long *)(optp + 4));
323df8bae1dSRodney W. Grimes 			ts_ecr = ntohl(*(u_long *)(optp + 8));
324a0292f23SGarrett Wollman #endif
325df8bae1dSRodney W. Grimes 			optp = NULL;	/* we've parsed the options */
326df8bae1dSRodney W. Grimes 		}
327df8bae1dSRodney W. Grimes 	}
328df8bae1dSRodney W. Grimes 	tiflags = ti->ti_flags;
329df8bae1dSRodney W. Grimes 
330df8bae1dSRodney W. Grimes 	/*
331df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
332df8bae1dSRodney W. Grimes 	 */
333df8bae1dSRodney W. Grimes 	NTOHL(ti->ti_seq);
334df8bae1dSRodney W. Grimes 	NTOHL(ti->ti_ack);
335df8bae1dSRodney W. Grimes 	NTOHS(ti->ti_win);
336df8bae1dSRodney W. Grimes 	NTOHS(ti->ti_urp);
337df8bae1dSRodney W. Grimes 
338df8bae1dSRodney W. Grimes 	/*
339df8bae1dSRodney W. Grimes 	 * Locate pcb for segment.
340df8bae1dSRodney W. Grimes 	 */
341df8bae1dSRodney W. Grimes findpcb:
342df8bae1dSRodney W. Grimes 	inp = tcp_last_inpcb;
343df8bae1dSRodney W. Grimes 	if (inp->inp_lport != ti->ti_dport ||
344df8bae1dSRodney W. Grimes 	    inp->inp_fport != ti->ti_sport ||
345df8bae1dSRodney W. Grimes 	    inp->inp_faddr.s_addr != ti->ti_src.s_addr ||
346df8bae1dSRodney W. Grimes 	    inp->inp_laddr.s_addr != ti->ti_dst.s_addr) {
347df8bae1dSRodney W. Grimes 		inp = in_pcblookup(&tcb, ti->ti_src, ti->ti_sport,
348df8bae1dSRodney W. Grimes 		    ti->ti_dst, ti->ti_dport, INPLOOKUP_WILDCARD);
349df8bae1dSRodney W. Grimes 		if (inp)
350df8bae1dSRodney W. Grimes 			tcp_last_inpcb = inp;
351df8bae1dSRodney W. Grimes 		++tcpstat.tcps_pcbcachemiss;
352df8bae1dSRodney W. Grimes 	}
353df8bae1dSRodney W. Grimes 
354df8bae1dSRodney W. Grimes 	/*
355df8bae1dSRodney W. Grimes 	 * If the state is CLOSED (i.e., TCB does not exist) then
356df8bae1dSRodney W. Grimes 	 * all data in the incoming segment is discarded.
357df8bae1dSRodney W. Grimes 	 * If the TCB exists but is in CLOSED state, it is embryonic,
358df8bae1dSRodney W. Grimes 	 * but should either do a listen or a connect soon.
359df8bae1dSRodney W. Grimes 	 */
360df8bae1dSRodney W. Grimes 	if (inp == 0)
361df8bae1dSRodney W. Grimes 		goto dropwithreset;
362df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
363df8bae1dSRodney W. Grimes 	if (tp == 0)
364df8bae1dSRodney W. Grimes 		goto dropwithreset;
365df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_CLOSED)
366df8bae1dSRodney W. Grimes 		goto drop;
367df8bae1dSRodney W. Grimes 
368df8bae1dSRodney W. Grimes 	/* Unscale the window into a 32-bit value. */
369df8bae1dSRodney W. Grimes 	if ((tiflags & TH_SYN) == 0)
370df8bae1dSRodney W. Grimes 		tiwin = ti->ti_win << tp->snd_scale;
371df8bae1dSRodney W. Grimes 	else
372df8bae1dSRodney W. Grimes 		tiwin = ti->ti_win;
373df8bae1dSRodney W. Grimes 
374df8bae1dSRodney W. Grimes 	so = inp->inp_socket;
375df8bae1dSRodney W. Grimes 	if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
376610ee2f9SDavid Greenman #ifdef TCPDEBUG
377df8bae1dSRodney W. Grimes 		if (so->so_options & SO_DEBUG) {
378df8bae1dSRodney W. Grimes 			ostate = tp->t_state;
379df8bae1dSRodney W. Grimes 			tcp_saveti = *ti;
380df8bae1dSRodney W. Grimes 		}
381610ee2f9SDavid Greenman #endif
382df8bae1dSRodney W. Grimes 		if (so->so_options & SO_ACCEPTCONN) {
383a0292f23SGarrett Wollman #ifdef TTCP
384a0292f23SGarrett Wollman 			register struct tcpcb *tp0 = tp;
385a0292f23SGarrett Wollman #endif
386df8bae1dSRodney W. Grimes 			so = sonewconn(so, 0);
387df8bae1dSRodney W. Grimes 			if (so == 0)
388df8bae1dSRodney W. Grimes 				goto drop;
389df8bae1dSRodney W. Grimes 			/*
390df8bae1dSRodney W. Grimes 			 * This is ugly, but ....
391df8bae1dSRodney W. Grimes 			 *
392df8bae1dSRodney W. Grimes 			 * Mark socket as temporary until we're
393df8bae1dSRodney W. Grimes 			 * committed to keeping it.  The code at
394df8bae1dSRodney W. Grimes 			 * ``drop'' and ``dropwithreset'' check the
395df8bae1dSRodney W. Grimes 			 * flag dropsocket to see if the temporary
396df8bae1dSRodney W. Grimes 			 * socket created here should be discarded.
397df8bae1dSRodney W. Grimes 			 * We mark the socket as discardable until
398df8bae1dSRodney W. Grimes 			 * we're committed to it below in TCPS_LISTEN.
399df8bae1dSRodney W. Grimes 			 */
400df8bae1dSRodney W. Grimes 			dropsocket++;
401df8bae1dSRodney W. Grimes 			inp = (struct inpcb *)so->so_pcb;
402df8bae1dSRodney W. Grimes 			inp->inp_laddr = ti->ti_dst;
403df8bae1dSRodney W. Grimes 			inp->inp_lport = ti->ti_dport;
404df8bae1dSRodney W. Grimes #if BSD>=43
405df8bae1dSRodney W. Grimes 			inp->inp_options = ip_srcroute();
406df8bae1dSRodney W. Grimes #endif
407df8bae1dSRodney W. Grimes 			tp = intotcpcb(inp);
408df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_LISTEN;
409a0292f23SGarrett Wollman #ifdef TTCP
410a0292f23SGarrett Wollman 			tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT);
411a0292f23SGarrett Wollman #endif
412df8bae1dSRodney W. Grimes 
413a0292f23SGarrett Wollman 			/* Compute proper scaling value from buffer space */
414df8bae1dSRodney W. Grimes 			while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
415df8bae1dSRodney W. Grimes 			   TCP_MAXWIN << tp->request_r_scale < so->so_rcv.sb_hiwat)
416df8bae1dSRodney W. Grimes 				tp->request_r_scale++;
417df8bae1dSRodney W. Grimes 		}
418df8bae1dSRodney W. Grimes 	}
419df8bae1dSRodney W. Grimes 
420df8bae1dSRodney W. Grimes 	/*
421df8bae1dSRodney W. Grimes 	 * Segment received on connection.
422df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
423df8bae1dSRodney W. Grimes 	 */
424df8bae1dSRodney W. Grimes 	tp->t_idle = 0;
425df8bae1dSRodney W. Grimes 	tp->t_timer[TCPT_KEEP] = tcp_keepidle;
426df8bae1dSRodney W. Grimes 
427df8bae1dSRodney W. Grimes 	/*
428df8bae1dSRodney W. Grimes 	 * Process options if not in LISTEN state,
429df8bae1dSRodney W. Grimes 	 * else do it below (after getting remote address).
430df8bae1dSRodney W. Grimes 	 */
431df8bae1dSRodney W. Grimes 	if (optp && tp->t_state != TCPS_LISTEN)
432df8bae1dSRodney W. Grimes 		tcp_dooptions(tp, optp, optlen, ti,
433a0292f23SGarrett Wollman #ifdef TTCP
434a0292f23SGarrett Wollman 			&to);
435a0292f23SGarrett Wollman #else
436df8bae1dSRodney W. Grimes 			&ts_present, &ts_val, &ts_ecr);
437a0292f23SGarrett Wollman #endif
438df8bae1dSRodney W. Grimes 
439df8bae1dSRodney W. Grimes 	/*
440df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
441df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
442df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
443df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
444df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
445df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
446df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
447df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
448df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
449df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
450df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
451df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
452a0292f23SGarrett Wollman #ifdef TTCP
453a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
454a0292f23SGarrett Wollman 	 * Since we check for TCPS_ESTABLISHED above, it can only
455a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
456a0292f23SGarrett Wollman #endif
457df8bae1dSRodney W. Grimes 	 */
458df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
459df8bae1dSRodney W. Grimes 	    (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
460a0292f23SGarrett Wollman #ifdef TTCP
461a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
462a0292f23SGarrett Wollman 	    ((to.to_flag & TOF_TS) == 0 ||
463a0292f23SGarrett Wollman 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
464a0292f23SGarrett Wollman 	    /*
465a0292f23SGarrett Wollman 	     * Using the CC option is compulsory if once started:
466a0292f23SGarrett Wollman 	     *   the segment is OK if no T/TCP was negotiated or
467a0292f23SGarrett Wollman 	     *   if the segment has a CC option equal to CCrecv
468a0292f23SGarrett Wollman 	     */
469a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) ||
470a0292f23SGarrett Wollman 	     (to.to_flag & TOF_CC) != 0 && to.to_cc == tp->cc_recv) &&
471a0292f23SGarrett Wollman #else
472df8bae1dSRodney W. Grimes 	    (!ts_present || TSTMP_GEQ(ts_val, tp->ts_recent)) &&
473a0292f23SGarrett Wollman #endif
474df8bae1dSRodney W. Grimes 	    ti->ti_seq == tp->rcv_nxt &&
475df8bae1dSRodney W. Grimes 	    tiwin && tiwin == tp->snd_wnd &&
476df8bae1dSRodney W. Grimes 	    tp->snd_nxt == tp->snd_max) {
477df8bae1dSRodney W. Grimes 
478df8bae1dSRodney W. Grimes 		/*
479df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
480df8bae1dSRodney W. Grimes 		 * record the timestamp.
481a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
482a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
483df8bae1dSRodney W. Grimes 		 */
484a0292f23SGarrett Wollman #ifdef TTCP
485a0292f23SGarrett Wollman 		if ((to.to_flag & TOF_TS) != 0 &&
486a0292f23SGarrett Wollman 		   SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) {
487a0292f23SGarrett Wollman 			tp->ts_recent_age = tcp_now;
488a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
489a0292f23SGarrett Wollman #else
490a0292f23SGarrett Wollman 		if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) {
491df8bae1dSRodney W. Grimes 			tp->ts_recent_age = tcp_now;
492df8bae1dSRodney W. Grimes 			tp->ts_recent = ts_val;
493a0292f23SGarrett Wollman #endif
494df8bae1dSRodney W. Grimes 		}
495df8bae1dSRodney W. Grimes 
496df8bae1dSRodney W. Grimes 		if (ti->ti_len == 0) {
497df8bae1dSRodney W. Grimes 			if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
498df8bae1dSRodney W. Grimes 			    SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
499df8bae1dSRodney W. Grimes 			    tp->snd_cwnd >= tp->snd_wnd) {
500df8bae1dSRodney W. Grimes 				/*
501df8bae1dSRodney W. Grimes 				 * this is a pure ack for outstanding data.
502df8bae1dSRodney W. Grimes 				 */
503df8bae1dSRodney W. Grimes 				++tcpstat.tcps_predack;
504a0292f23SGarrett Wollman #ifdef TTCP
505a0292f23SGarrett Wollman 				if ((to.to_flag & TOF_TS) != 0)
506a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
507a0292f23SGarrett Wollman 					    tcp_now - to.to_tsecr + 1);
508a0292f23SGarrett Wollman #else
509df8bae1dSRodney W. Grimes 				if (ts_present)
510df8bae1dSRodney W. Grimes 					tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
511a0292f23SGarrett Wollman #endif
512df8bae1dSRodney W. Grimes 				else if (tp->t_rtt &&
513df8bae1dSRodney W. Grimes 					    SEQ_GT(ti->ti_ack, tp->t_rtseq))
514df8bae1dSRodney W. Grimes 					tcp_xmit_timer(tp, tp->t_rtt);
515df8bae1dSRodney W. Grimes 				acked = ti->ti_ack - tp->snd_una;
516df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackpack++;
517df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackbyte += acked;
518df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
519df8bae1dSRodney W. Grimes 				tp->snd_una = ti->ti_ack;
520df8bae1dSRodney W. Grimes 				m_freem(m);
521df8bae1dSRodney W. Grimes 
522df8bae1dSRodney W. Grimes 				/*
523df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
524df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
525df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
526df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
527df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
528df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
529df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
530df8bae1dSRodney W. Grimes 				 */
531df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
532df8bae1dSRodney W. Grimes 					tp->t_timer[TCPT_REXMT] = 0;
533df8bae1dSRodney W. Grimes 				else if (tp->t_timer[TCPT_PERSIST] == 0)
534df8bae1dSRodney W. Grimes 					tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
535df8bae1dSRodney W. Grimes 
536df8bae1dSRodney W. Grimes 				if (so->so_snd.sb_flags & SB_NOTIFY)
537df8bae1dSRodney W. Grimes 					sowwakeup(so);
538df8bae1dSRodney W. Grimes 				if (so->so_snd.sb_cc)
539df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
540df8bae1dSRodney W. Grimes 				return;
541df8bae1dSRodney W. Grimes 			}
542df8bae1dSRodney W. Grimes 		} else if (ti->ti_ack == tp->snd_una &&
543df8bae1dSRodney W. Grimes 		    tp->seg_next == (struct tcpiphdr *)tp &&
544df8bae1dSRodney W. Grimes 		    ti->ti_len <= sbspace(&so->so_rcv)) {
545df8bae1dSRodney W. Grimes 			/*
546df8bae1dSRodney W. Grimes 			 * this is a pure, in-sequence data packet
547df8bae1dSRodney W. Grimes 			 * with nothing on the reassembly queue and
548df8bae1dSRodney W. Grimes 			 * we have enough buffer space to take it.
549df8bae1dSRodney W. Grimes 			 */
550df8bae1dSRodney W. Grimes 			++tcpstat.tcps_preddat;
551df8bae1dSRodney W. Grimes 			tp->rcv_nxt += ti->ti_len;
552df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpack++;
553df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyte += ti->ti_len;
554df8bae1dSRodney W. Grimes 			/*
555df8bae1dSRodney W. Grimes 			 * Drop TCP, IP headers and TCP options then add data
556df8bae1dSRodney W. Grimes 			 * to socket buffer.
557df8bae1dSRodney W. Grimes 			 */
558df8bae1dSRodney W. Grimes 			m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
559df8bae1dSRodney W. Grimes 			m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
560df8bae1dSRodney W. Grimes 			sbappend(&so->so_rcv, m);
561df8bae1dSRodney W. Grimes 			sorwakeup(so);
562df8bae1dSRodney W. Grimes 			tp->t_flags |= TF_DELACK;
563df8bae1dSRodney W. Grimes 			return;
564df8bae1dSRodney W. Grimes 		}
565df8bae1dSRodney W. Grimes 	}
566df8bae1dSRodney W. Grimes 
567df8bae1dSRodney W. Grimes 	/*
568df8bae1dSRodney W. Grimes 	 * Drop TCP, IP headers and TCP options.
569df8bae1dSRodney W. Grimes 	 */
570df8bae1dSRodney W. Grimes 	m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
571df8bae1dSRodney W. Grimes 	m->m_len  -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
572df8bae1dSRodney W. Grimes 
573df8bae1dSRodney W. Grimes 	/*
574df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
575df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
576df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
577df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
578df8bae1dSRodney W. Grimes 	 */
579df8bae1dSRodney W. Grimes 	{ int win;
580df8bae1dSRodney W. Grimes 
581df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
582df8bae1dSRodney W. Grimes 	if (win < 0)
583df8bae1dSRodney W. Grimes 		win = 0;
584df8bae1dSRodney W. Grimes 	tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt));
585df8bae1dSRodney W. Grimes 	}
586df8bae1dSRodney W. Grimes 
587df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
588df8bae1dSRodney W. Grimes 
589df8bae1dSRodney W. Grimes 	/*
590df8bae1dSRodney W. Grimes 	 * If the state is LISTEN then ignore segment if it contains an RST.
591df8bae1dSRodney W. Grimes 	 * If the segment contains an ACK then it is bad and send a RST.
592df8bae1dSRodney W. Grimes 	 * If it does not contain a SYN then it is not interesting; drop it.
593df8bae1dSRodney W. Grimes 	 * Don't bother responding if the destination was a broadcast.
594df8bae1dSRodney W. Grimes 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
595df8bae1dSRodney W. Grimes 	 * tp->iss, and send a segment:
596df8bae1dSRodney W. Grimes 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
597df8bae1dSRodney W. Grimes 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
598df8bae1dSRodney W. Grimes 	 * Fill in remote peer address fields if not previously specified.
599df8bae1dSRodney W. Grimes 	 * Enter SYN_RECEIVED state, and process any other fields of this
600df8bae1dSRodney W. Grimes 	 * segment in this state.
601df8bae1dSRodney W. Grimes 	 */
602df8bae1dSRodney W. Grimes 	case TCPS_LISTEN: {
603df8bae1dSRodney W. Grimes 		struct mbuf *am;
604df8bae1dSRodney W. Grimes 		register struct sockaddr_in *sin;
605df8bae1dSRodney W. Grimes 
606df8bae1dSRodney W. Grimes 		if (tiflags & TH_RST)
607df8bae1dSRodney W. Grimes 			goto drop;
608df8bae1dSRodney W. Grimes 		if (tiflags & TH_ACK)
609df8bae1dSRodney W. Grimes 			goto dropwithreset;
610df8bae1dSRodney W. Grimes 		if ((tiflags & TH_SYN) == 0)
611df8bae1dSRodney W. Grimes 			goto drop;
612df8bae1dSRodney W. Grimes 		/*
613df8bae1dSRodney W. Grimes 		 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
614df8bae1dSRodney W. Grimes 		 * in_broadcast() should never return true on a received
615df8bae1dSRodney W. Grimes 		 * packet with M_BCAST not set.
616df8bae1dSRodney W. Grimes 		 */
617df8bae1dSRodney W. Grimes 		if (m->m_flags & (M_BCAST|M_MCAST) ||
618d4d0967eSDavid Greenman 		    IN_MULTICAST(ntohl(ti->ti_dst.s_addr)))
619df8bae1dSRodney W. Grimes 			goto drop;
620df8bae1dSRodney W. Grimes 		am = m_get(M_DONTWAIT, MT_SONAME);	/* XXX */
621df8bae1dSRodney W. Grimes 		if (am == NULL)
622df8bae1dSRodney W. Grimes 			goto drop;
623df8bae1dSRodney W. Grimes 		am->m_len = sizeof (struct sockaddr_in);
624df8bae1dSRodney W. Grimes 		sin = mtod(am, struct sockaddr_in *);
625df8bae1dSRodney W. Grimes 		sin->sin_family = AF_INET;
626df8bae1dSRodney W. Grimes 		sin->sin_len = sizeof(*sin);
627df8bae1dSRodney W. Grimes 		sin->sin_addr = ti->ti_src;
628df8bae1dSRodney W. Grimes 		sin->sin_port = ti->ti_sport;
629df8bae1dSRodney W. Grimes 		bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero));
630df8bae1dSRodney W. Grimes 		laddr = inp->inp_laddr;
631df8bae1dSRodney W. Grimes 		if (inp->inp_laddr.s_addr == INADDR_ANY)
632df8bae1dSRodney W. Grimes 			inp->inp_laddr = ti->ti_dst;
633df8bae1dSRodney W. Grimes 		if (in_pcbconnect(inp, am)) {
634df8bae1dSRodney W. Grimes 			inp->inp_laddr = laddr;
635df8bae1dSRodney W. Grimes 			(void) m_free(am);
636df8bae1dSRodney W. Grimes 			goto drop;
637df8bae1dSRodney W. Grimes 		}
638df8bae1dSRodney W. Grimes 		(void) m_free(am);
639df8bae1dSRodney W. Grimes 		tp->t_template = tcp_template(tp);
640df8bae1dSRodney W. Grimes 		if (tp->t_template == 0) {
641df8bae1dSRodney W. Grimes 			tp = tcp_drop(tp, ENOBUFS);
642df8bae1dSRodney W. Grimes 			dropsocket = 0;		/* socket is already gone */
643df8bae1dSRodney W. Grimes 			goto drop;
644df8bae1dSRodney W. Grimes 		}
645a0292f23SGarrett Wollman #ifdef TTCP
646a0292f23SGarrett Wollman 		if ((taop = tcp_gettaocache(inp)) == NULL) {
647a0292f23SGarrett Wollman 			taop = &tao_noncached;
648a0292f23SGarrett Wollman 			bzero(taop, sizeof(*taop));
649a0292f23SGarrett Wollman 		}
650a0292f23SGarrett Wollman #endif /* TTCP */
651df8bae1dSRodney W. Grimes 		if (optp)
652df8bae1dSRodney W. Grimes 			tcp_dooptions(tp, optp, optlen, ti,
653a0292f23SGarrett Wollman #ifdef TTCP
654a0292f23SGarrett Wollman 				&to);
655a0292f23SGarrett Wollman #else
656df8bae1dSRodney W. Grimes 				&ts_present, &ts_val, &ts_ecr);
657a0292f23SGarrett Wollman #endif
658df8bae1dSRodney W. Grimes 		if (iss)
659df8bae1dSRodney W. Grimes 			tp->iss = iss;
660df8bae1dSRodney W. Grimes 		else
661df8bae1dSRodney W. Grimes 			tp->iss = tcp_iss;
662df8bae1dSRodney W. Grimes 		tcp_iss += TCP_ISSINCR/2;
663df8bae1dSRodney W. Grimes 		tp->irs = ti->ti_seq;
664df8bae1dSRodney W. Grimes 		tcp_sendseqinit(tp);
665df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
666a0292f23SGarrett Wollman #ifdef TTCP
667a0292f23SGarrett Wollman 		/*
668a0292f23SGarrett Wollman 		 * Initialization of the tcpcb for transaction;
669a0292f23SGarrett Wollman 		 *   set SND.WND = SEG.WND,
670a0292f23SGarrett Wollman 		 *   initialize CCsend and CCrecv.
671a0292f23SGarrett Wollman 		 */
672a0292f23SGarrett Wollman 		tp->snd_wnd = tiwin;	/* initial send-window */
673a0292f23SGarrett Wollman 		tp->cc_send = CC_INC(tcp_ccgen);
674a0292f23SGarrett Wollman 		tp->cc_recv = to.to_cc;
675a0292f23SGarrett Wollman 		/*
676a0292f23SGarrett Wollman 		 * Perform TAO test on incoming CC (SEG.CC) option, if any.
677a0292f23SGarrett Wollman 		 * - compare SEG.CC against cached CC from the same host,
678a0292f23SGarrett Wollman 		 *	if any.
679a0292f23SGarrett Wollman 		 * - if SEG.CC > chached value, SYN must be new and is accepted
680a0292f23SGarrett Wollman 		 *	immediately: save new CC in the cache, mark the socket
681a0292f23SGarrett Wollman 		 *	connected, enter ESTABLISHED state, turn on flag to
682a0292f23SGarrett Wollman 		 *	send a SYN in the next segment.
683a0292f23SGarrett Wollman 		 *	A virtual advertised window is set in rcv_adv to
684a0292f23SGarrett Wollman 		 *	initialize SWS prevention.  Then enter normal segment
685a0292f23SGarrett Wollman 		 *	processing: drop SYN, process data and FIN.
686a0292f23SGarrett Wollman 		 * - otherwise do a normal 3-way handshake.
687a0292f23SGarrett Wollman 		 */
688a0292f23SGarrett Wollman 		if ((to.to_flag & TOF_CC) != 0) {
689a0292f23SGarrett Wollman 		    if (taop->tao_cc != 0 && CC_GT(to.to_cc, taop->tao_cc)) {
690a0292f23SGarrett Wollman 			taop->tao_cc = to.to_cc;
691a0292f23SGarrett Wollman 			tp->t_state = TCPS_ESTABLISHED;
692a0292f23SGarrett Wollman 
693a0292f23SGarrett Wollman 			/*
694a0292f23SGarrett Wollman 			 * If there is a FIN, or if there is data and the
695a0292f23SGarrett Wollman 			 * connection is local, then delay SYN,ACK(SYN) in
696a0292f23SGarrett Wollman 			 * the hope of piggy-backing it on a response
697a0292f23SGarrett Wollman 			 * segment.  Otherwise must send ACK now in case
698a0292f23SGarrett Wollman 			 * the other side is slow starting.
699a0292f23SGarrett Wollman 			 */
700a0292f23SGarrett Wollman 			if ((tiflags & TH_FIN) || (ti->ti_len != 0 &&
701a0292f23SGarrett Wollman 			    in_localaddr(inp->inp_faddr)))
702a0292f23SGarrett Wollman 				tp->t_flags |= (TF_DELACK | TF_NEEDSYN);
703a0292f23SGarrett Wollman 			else
704a0292f23SGarrett Wollman 				tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
705a0292f23SGarrett Wollman 			tp->rcv_adv += tp->rcv_wnd;
706a0292f23SGarrett Wollman 			tcpstat.tcps_connects++;
707a0292f23SGarrett Wollman 			soisconnected(so);
708a0292f23SGarrett Wollman 			tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
709a0292f23SGarrett Wollman 			dropsocket = 0;		/* committed to socket */
710a0292f23SGarrett Wollman 			tcpstat.tcps_accepts++;
711a0292f23SGarrett Wollman 			goto trimthenstep6;
712a0292f23SGarrett Wollman 		    }
713a0292f23SGarrett Wollman 		/* else do standard 3-way handshake */
714a0292f23SGarrett Wollman 		} else {
715a0292f23SGarrett Wollman 		    /*
716a0292f23SGarrett Wollman 		     * No CC option, but maybe CC.NEW:
717a0292f23SGarrett Wollman 		     *   invalidate cached value.
718a0292f23SGarrett Wollman 		     */
719a0292f23SGarrett Wollman 		     taop->tao_cc = 0;
720a0292f23SGarrett Wollman 		}
721a0292f23SGarrett Wollman 		/*
722a0292f23SGarrett Wollman 		 * TAO test failed or there was no CC option,
723a0292f23SGarrett Wollman 		 *    do a standard 3-way handshake.
724a0292f23SGarrett Wollman 		 */
725a0292f23SGarrett Wollman #endif /* TTCP */
726df8bae1dSRodney W. Grimes 		tp->t_flags |= TF_ACKNOW;
727df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_SYN_RECEIVED;
728df8bae1dSRodney W. Grimes 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
729df8bae1dSRodney W. Grimes 		dropsocket = 0;		/* committed to socket */
730df8bae1dSRodney W. Grimes 		tcpstat.tcps_accepts++;
731df8bae1dSRodney W. Grimes 		goto trimthenstep6;
732df8bae1dSRodney W. Grimes 		}
733df8bae1dSRodney W. Grimes 
734df8bae1dSRodney W. Grimes 	/*
735df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
736df8bae1dSRodney W. Grimes 	 *	if seg contains an ACK, but not for our SYN, drop the input.
737df8bae1dSRodney W. Grimes 	 *	if seg contains a RST, then drop the connection.
738df8bae1dSRodney W. Grimes 	 *	if seg does not contain SYN, then drop it.
739df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
740df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
741df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
742df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
743df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
744df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
745df8bae1dSRodney W. Grimes 	 */
746df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
747a0292f23SGarrett Wollman #ifdef TTCP
748a0292f23SGarrett Wollman 		if ((taop = tcp_gettaocache(inp)) == NULL) {
749a0292f23SGarrett Wollman 			taop = &tao_noncached;
750a0292f23SGarrett Wollman 			bzero(taop, sizeof(*taop));
751a0292f23SGarrett Wollman 		}
752a0292f23SGarrett Wollman 
753a0292f23SGarrett Wollman 		if ((tiflags & TH_ACK) &&
754a0292f23SGarrett Wollman 		    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
755a0292f23SGarrett Wollman 		     SEQ_GT(ti->ti_ack, tp->snd_max))) {
756a0292f23SGarrett Wollman 			/*
757a0292f23SGarrett Wollman 			 * If we have a cached CCsent for the remote host,
758a0292f23SGarrett Wollman 			 * hence we haven't just crashed and restarted,
759a0292f23SGarrett Wollman 			 * do not send a RST.  This may be a retransmission
760a0292f23SGarrett Wollman 			 * from the other side after our earlier ACK was lost.
761a0292f23SGarrett Wollman 			 * Our new SYN, when it arrives, will serve as the
762a0292f23SGarrett Wollman 			 * needed ACK.
763a0292f23SGarrett Wollman 			 */
764a0292f23SGarrett Wollman 			if (taop->tao_ccsent != 0)
765a0292f23SGarrett Wollman 				goto drop;
766a0292f23SGarrett Wollman 			else
767a0292f23SGarrett Wollman 				goto dropwithreset;
768a0292f23SGarrett Wollman 		}
769a0292f23SGarrett Wollman #else
770df8bae1dSRodney W. Grimes 		if ((tiflags & TH_ACK) &&
771df8bae1dSRodney W. Grimes 		    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
772df8bae1dSRodney W. Grimes 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
773df8bae1dSRodney W. Grimes 			goto dropwithreset;
774a0292f23SGarrett Wollman #endif
775df8bae1dSRodney W. Grimes 		if (tiflags & TH_RST) {
776df8bae1dSRodney W. Grimes 			if (tiflags & TH_ACK)
777df8bae1dSRodney W. Grimes 				tp = tcp_drop(tp, ECONNREFUSED);
778df8bae1dSRodney W. Grimes 			goto drop;
779df8bae1dSRodney W. Grimes 		}
780df8bae1dSRodney W. Grimes 		if ((tiflags & TH_SYN) == 0)
781df8bae1dSRodney W. Grimes 			goto drop;
782a0292f23SGarrett Wollman #ifdef TTCP
783a0292f23SGarrett Wollman 		tp->snd_wnd = ti->ti_win;	/* initial send window */
784a0292f23SGarrett Wollman 		tp->cc_recv = to.to_cc;		/* foreign CC */
785a0292f23SGarrett Wollman #else
786df8bae1dSRodney W. Grimes 		if (tiflags & TH_ACK) {
787df8bae1dSRodney W. Grimes 			tp->snd_una = ti->ti_ack;
788df8bae1dSRodney W. Grimes 			if (SEQ_LT(tp->snd_nxt, tp->snd_una))
789df8bae1dSRodney W. Grimes 				tp->snd_nxt = tp->snd_una;
790df8bae1dSRodney W. Grimes 		}
791df8bae1dSRodney W. Grimes 		tp->t_timer[TCPT_REXMT] = 0;
792a0292f23SGarrett Wollman #endif
793a0292f23SGarrett Wollman 
794df8bae1dSRodney W. Grimes 		tp->irs = ti->ti_seq;
795df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
796a0292f23SGarrett Wollman #ifndef TTCP
797df8bae1dSRodney W. Grimes 		tp->t_flags |= TF_ACKNOW;
798df8bae1dSRodney W. Grimes 		if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
799a0292f23SGarrett Wollman #else
800a0292f23SGarrett Wollman 		if (tiflags & TH_ACK && SEQ_GT(ti->ti_ack, tp->iss)) {
801a0292f23SGarrett Wollman #endif
802df8bae1dSRodney W. Grimes 			tcpstat.tcps_connects++;
803df8bae1dSRodney W. Grimes 			soisconnected(so);
804df8bae1dSRodney W. Grimes 			/* Do window scaling on this connection? */
805df8bae1dSRodney W. Grimes 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
806df8bae1dSRodney W. Grimes 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
807df8bae1dSRodney W. Grimes 				tp->snd_scale = tp->requested_s_scale;
808df8bae1dSRodney W. Grimes 				tp->rcv_scale = tp->request_r_scale;
809df8bae1dSRodney W. Grimes 			}
810a0292f23SGarrett Wollman #ifdef TTCP
811a0292f23SGarrett Wollman 			/*
812a0292f23SGarrett Wollman 			 * Our SYN was acked.  If segment contains CC.ECHO
813a0292f23SGarrett Wollman 			 * option, check it to make sure this segment really
814a0292f23SGarrett Wollman 			 * matches our SYN.  If not, just drop it as old
815a0292f23SGarrett Wollman 			 * duplicate, but send an RST if we're still playing
816a0292f23SGarrett Wollman 			 * by the old rules.
817a0292f23SGarrett Wollman 			 */
818a0292f23SGarrett Wollman 			if ((to.to_flag & TOF_CCECHO) &&
819a0292f23SGarrett Wollman 			    tp->cc_send != to.to_ccecho) {
820a0292f23SGarrett Wollman 				if (taop->tao_ccsent != 0)
821a0292f23SGarrett Wollman 					goto drop;
822a0292f23SGarrett Wollman 				else
823a0292f23SGarrett Wollman 					goto dropwithreset;
824a0292f23SGarrett Wollman 			}
825a0292f23SGarrett Wollman 			/* Segment is acceptable, update cache if undefined. */
826a0292f23SGarrett Wollman 			if (taop->tao_ccsent == 0)
827a0292f23SGarrett Wollman 				taop->tao_ccsent = to.to_ccecho;
828a0292f23SGarrett Wollman 
829a0292f23SGarrett Wollman 			tp->rcv_adv += tp->rcv_wnd;
830a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
831a0292f23SGarrett Wollman 			/*
832a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
833a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
834a0292f23SGarrett Wollman 			 */
835a0292f23SGarrett Wollman 			if (ti->ti_len != 0)
836a0292f23SGarrett Wollman 				tp->t_flags |= TF_DELACK;
837a0292f23SGarrett Wollman 			else
838a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
839a0292f23SGarrett Wollman 			/*
840a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
841a0292f23SGarrett Wollman 			 * Transitions:
842a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
843a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
844a0292f23SGarrett Wollman 			 */
845a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
846a0292f23SGarrett Wollman 				tp->t_state = TCPS_FIN_WAIT_1;
847a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
848a0292f23SGarrett Wollman 				tiflags &= ~TH_SYN;
849a0292f23SGarrett Wollman 			} else
850a0292f23SGarrett Wollman 				tp->t_state = TCPS_ESTABLISHED;
851a0292f23SGarrett Wollman #else
852a0292f23SGarrett Wollman 			tp->t_state = TCPS_ESTABLISHED;
853df8bae1dSRodney W. Grimes 			(void) tcp_reass(tp, (struct tcpiphdr *)0,
854df8bae1dSRodney W. Grimes 				(struct mbuf *)0);
855df8bae1dSRodney W. Grimes 			/*
856df8bae1dSRodney W. Grimes 			 * if we didn't have to retransmit the SYN,
857df8bae1dSRodney W. Grimes 			 * use its rtt as our initial srtt & rtt var.
858df8bae1dSRodney W. Grimes 			 */
859df8bae1dSRodney W. Grimes 			if (tp->t_rtt)
860df8bae1dSRodney W. Grimes 				tcp_xmit_timer(tp, tp->t_rtt);
861a0292f23SGarrett Wollman #endif
862a0292f23SGarrett Wollman 
863a0292f23SGarrett Wollman #ifdef TTCP
864a0292f23SGarrett Wollman 		} else {
865a0292f23SGarrett Wollman 		/*
866a0292f23SGarrett Wollman 		 *  Received initial SYN in SYN-SENT[*] state => simul-
867a0292f23SGarrett Wollman 		 *  taneous open.  If segment contains CC option and there is
868a0292f23SGarrett Wollman 		 *  a cached CC, apply TAO test; if it succeeds, connection is
869a0292f23SGarrett Wollman 		 *  half-synchronized.  Otherwise, do 3-way handshake:
870a0292f23SGarrett Wollman 		 *        SYN-SENT -> SYN-RECEIVED
871a0292f23SGarrett Wollman 		 *        SYN-SENT* -> SYN-RECEIVED*
872a0292f23SGarrett Wollman 		 *  If there was no CC option, clear cached CC value.
873a0292f23SGarrett Wollman 		 */
874a0292f23SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
875a0292f23SGarrett Wollman 			tp->t_timer[TCPT_REXMT] = 0;
876a0292f23SGarrett Wollman 			if (to.to_flag & TOF_CC) {
877a0292f23SGarrett Wollman 				if (taop->tao_cc != 0 &&
878a0292f23SGarrett Wollman 				    CC_GT(to.to_cc, taop->tao_cc)) {
879a0292f23SGarrett Wollman 					/*
880a0292f23SGarrett Wollman 					 * update cache and make transition:
881a0292f23SGarrett Wollman 					 *        SYN-SENT -> ESTABLISHED*
882a0292f23SGarrett Wollman 					 *        SYN-SENT* -> FIN-WAIT-1*
883a0292f23SGarrett Wollman 					 */
884a0292f23SGarrett Wollman 					taop->tao_cc = to.to_cc;
885a0292f23SGarrett Wollman 					if (tp->t_flags & TF_NEEDFIN) {
886a0292f23SGarrett Wollman 						tp->t_state = TCPS_FIN_WAIT_1;
887a0292f23SGarrett Wollman 						tp->t_flags &= ~TF_NEEDFIN;
888a0292f23SGarrett Wollman 					} else
889a0292f23SGarrett Wollman 						tp->t_state = TCPS_ESTABLISHED;
890a0292f23SGarrett Wollman 					tp->t_flags |= TF_NEEDSYN;
891df8bae1dSRodney W. Grimes 				} else
892df8bae1dSRodney W. Grimes 					tp->t_state = TCPS_SYN_RECEIVED;
893a0292f23SGarrett Wollman 			} else {
894a0292f23SGarrett Wollman 				/* CC.NEW or no option => invalidate cache */
895a0292f23SGarrett Wollman 				taop->tao_cc = 0;
896a0292f23SGarrett Wollman 				tp->t_state = TCPS_SYN_RECEIVED;
897a0292f23SGarrett Wollman 			}
898a0292f23SGarrett Wollman 		}
899a0292f23SGarrett Wollman #else
900a0292f23SGarrett Wollman 		} else
901a0292f23SGarrett Wollman 			tp->t_state = TCPS_SYN_RECEIVED;
902a0292f23SGarrett Wollman #endif
903df8bae1dSRodney W. Grimes 
904df8bae1dSRodney W. Grimes trimthenstep6:
905df8bae1dSRodney W. Grimes 		/*
906df8bae1dSRodney W. Grimes 		 * Advance ti->ti_seq to correspond to first data byte.
907df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
908df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
909df8bae1dSRodney W. Grimes 		 */
910df8bae1dSRodney W. Grimes 		ti->ti_seq++;
911df8bae1dSRodney W. Grimes 		if (ti->ti_len > tp->rcv_wnd) {
912df8bae1dSRodney W. Grimes 			todrop = ti->ti_len - tp->rcv_wnd;
913df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
914df8bae1dSRodney W. Grimes 			ti->ti_len = tp->rcv_wnd;
915df8bae1dSRodney W. Grimes 			tiflags &= ~TH_FIN;
916df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpackafterwin++;
917df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
918df8bae1dSRodney W. Grimes 		}
919df8bae1dSRodney W. Grimes 		tp->snd_wl1 = ti->ti_seq - 1;
920df8bae1dSRodney W. Grimes 		tp->rcv_up = ti->ti_seq;
921a0292f23SGarrett Wollman #ifdef TTCP
922a0292f23SGarrett Wollman 		/*
923a0292f23SGarrett Wollman 		 *  Client side of transaction: already sent SYN and data.
924a0292f23SGarrett Wollman 		 *  If the remote host used T/TCP to validate the SYN,
925a0292f23SGarrett Wollman 		 *  our data will be ACK'd; if so, enter normal data segment
926a0292f23SGarrett Wollman 		 *  processing in the middle of step 5, ack processing.
927a0292f23SGarrett Wollman 		 *  Otherwise, goto step 6.
928a0292f23SGarrett Wollman 		 */
929a0292f23SGarrett Wollman  		if (tiflags & TH_ACK)
930a0292f23SGarrett Wollman 			goto process_ACK;
931a0292f23SGarrett Wollman #endif
932df8bae1dSRodney W. Grimes 		goto step6;
933a0292f23SGarrett Wollman #ifdef TTCP
934a0292f23SGarrett Wollman 	/*
935a0292f23SGarrett Wollman 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
936a0292f23SGarrett Wollman 	 *	if segment contains a SYN and CC [not CC.NEW] option:
937a0292f23SGarrett Wollman 	 *              if state == TIME_WAIT and connection duration > MSL,
938a0292f23SGarrett Wollman 	 *                  drop packet and send RST;
939a0292f23SGarrett Wollman 	 *
940a0292f23SGarrett Wollman 	 *		if SEG.CC > CCrecv then is new SYN, and can implicitly
941a0292f23SGarrett Wollman 	 *		    ack the FIN (and data) in retransmission queue.
942a0292f23SGarrett Wollman 	 *                  Complete close and delete TCPCB.  Then reprocess
943a0292f23SGarrett Wollman 	 *                  segment, hoping to find new TCPCB in LISTEN state;
944a0292f23SGarrett Wollman 	 *
945a0292f23SGarrett Wollman 	 *		else must be old SYN; drop it.
946a0292f23SGarrett Wollman 	 *      else do normal processing.
947a0292f23SGarrett Wollman 	 */
948a0292f23SGarrett Wollman 	case TCPS_LAST_ACK:
949a0292f23SGarrett Wollman 	case TCPS_CLOSING:
950a0292f23SGarrett Wollman 	case TCPS_TIME_WAIT:
951a0292f23SGarrett Wollman 		if ((tiflags & TH_SYN) &&
952a0292f23SGarrett Wollman 		    (to.to_flag & TOF_CC) && tp->cc_recv != 0) {
953a0292f23SGarrett Wollman 			if (tp->t_state == TCPS_TIME_WAIT &&
954a0292f23SGarrett Wollman 					tp->t_duration > TCPTV_MSL)
955a0292f23SGarrett Wollman 				goto dropwithreset;
956a0292f23SGarrett Wollman 			if (CC_GT(to.to_cc, tp->cc_recv)) {
957a0292f23SGarrett Wollman 				tp = tcp_close(tp);
958a0292f23SGarrett Wollman 				goto findpcb;
959a0292f23SGarrett Wollman 			}
960a0292f23SGarrett Wollman 			else
961a0292f23SGarrett Wollman 				goto drop;
962a0292f23SGarrett Wollman 		}
963a0292f23SGarrett Wollman  		break;  /* continue normal processing */
964a0292f23SGarrett Wollman #endif
965df8bae1dSRodney W. Grimes 	}
966df8bae1dSRodney W. Grimes 
967df8bae1dSRodney W. Grimes 	/*
968df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
969df8bae1dSRodney W. Grimes 	 * First check timestamp, if present.
970a0292f23SGarrett Wollman #ifdef TTCP
971a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
972a0292f23SGarrett Wollman #endif
973df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
974df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
975df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
976df8bae1dSRodney W. Grimes 	 *
977df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
978df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
979df8bae1dSRodney W. Grimes 	 */
980a0292f23SGarrett Wollman #ifdef TTCP
981a0292f23SGarrett Wollman 	if ((to.to_flag & TOF_TS) != 0 && (tiflags & TH_RST) == 0 &&
982a0292f23SGarrett Wollman 	    tp->ts_recent && TSTMP_LT(to.to_tsval, tp->ts_recent)) {
983a0292f23SGarrett Wollman #else
984df8bae1dSRodney W. Grimes 	if (ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent &&
985df8bae1dSRodney W. Grimes 	    TSTMP_LT(ts_val, tp->ts_recent)) {
986a0292f23SGarrett Wollman #endif
987df8bae1dSRodney W. Grimes 
988df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
989df8bae1dSRodney W. Grimes 		if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
990df8bae1dSRodney W. Grimes 			/*
991df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
992df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
993df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
994df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
995df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
996df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
997df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
998df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
999df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
1000df8bae1dSRodney W. Grimes 			 */
1001df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
1002df8bae1dSRodney W. Grimes 		} else {
1003df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvduppack++;
1004df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvdupbyte += ti->ti_len;
1005df8bae1dSRodney W. Grimes 			tcpstat.tcps_pawsdrop++;
1006df8bae1dSRodney W. Grimes 			goto dropafterack;
1007df8bae1dSRodney W. Grimes 		}
1008df8bae1dSRodney W. Grimes 	}
1009df8bae1dSRodney W. Grimes 
1010a0292f23SGarrett Wollman #ifdef TTCP
1011a0292f23SGarrett Wollman 	/*
1012a0292f23SGarrett Wollman 	 * T/TCP mechanism
1013a0292f23SGarrett Wollman 	 *   If T/TCP was negotiated and the segment doesn't have CC,
1014a0292f23SGarrett Wollman 	 *   or if it's CC is wrong then drop the segment.
1015a0292f23SGarrett Wollman 	 *   RST segments do not have to comply with this.
1016a0292f23SGarrett Wollman 	 */
1017a0292f23SGarrett Wollman 	if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) &&
1018a0292f23SGarrett Wollman 	    ((to.to_flag & TOF_CC) == 0 || tp->cc_recv != to.to_cc) &&
1019a0292f23SGarrett Wollman 	    (tiflags & TH_RST) == 0)
1020a0292f23SGarrett Wollman  		goto dropafterack;
1021a0292f23SGarrett Wollman #endif
1022a0292f23SGarrett Wollman 
1023df8bae1dSRodney W. Grimes 	todrop = tp->rcv_nxt - ti->ti_seq;
1024df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1025df8bae1dSRodney W. Grimes 		if (tiflags & TH_SYN) {
1026df8bae1dSRodney W. Grimes 			tiflags &= ~TH_SYN;
1027df8bae1dSRodney W. Grimes 			ti->ti_seq++;
1028df8bae1dSRodney W. Grimes 			if (ti->ti_urp > 1)
1029df8bae1dSRodney W. Grimes 				ti->ti_urp--;
1030df8bae1dSRodney W. Grimes 			else
1031df8bae1dSRodney W. Grimes 				tiflags &= ~TH_URG;
1032df8bae1dSRodney W. Grimes 			todrop--;
1033df8bae1dSRodney W. Grimes 		}
1034df8bae1dSRodney W. Grimes 		if (todrop >= ti->ti_len) {
1035df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvduppack++;
1036df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvdupbyte += ti->ti_len;
1037df8bae1dSRodney W. Grimes 			/*
1038df8bae1dSRodney W. Grimes 			 * If segment is just one to the left of the window,
1039df8bae1dSRodney W. Grimes 			 * check two special cases:
1040df8bae1dSRodney W. Grimes 			 * 1. Don't toss RST in response to 4.2-style keepalive.
1041df8bae1dSRodney W. Grimes 			 * 2. If the only thing to drop is a FIN, we can drop
1042df8bae1dSRodney W. Grimes 			 *    it, but check the ACK or we will get into FIN
1043df8bae1dSRodney W. Grimes 			 *    wars if our FINs crossed (both CLOSING).
1044df8bae1dSRodney W. Grimes 			 * In either case, send ACK to resynchronize,
1045df8bae1dSRodney W. Grimes 			 * but keep on processing for RST or ACK.
1046df8bae1dSRodney W. Grimes 			 */
1047df8bae1dSRodney W. Grimes 			if ((tiflags & TH_FIN && todrop == ti->ti_len + 1)
1048df8bae1dSRodney W. Grimes #ifdef TCP_COMPAT_42
1049df8bae1dSRodney W. Grimes 			  || (tiflags & TH_RST && ti->ti_seq == tp->rcv_nxt - 1)
1050df8bae1dSRodney W. Grimes #endif
1051df8bae1dSRodney W. Grimes 			   ) {
1052df8bae1dSRodney W. Grimes 				todrop = ti->ti_len;
1053df8bae1dSRodney W. Grimes 				tiflags &= ~TH_FIN;
1054df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
1055df8bae1dSRodney W. Grimes 			} else {
1056df8bae1dSRodney W. Grimes 				/*
1057df8bae1dSRodney W. Grimes 				 * Handle the case when a bound socket connects
1058df8bae1dSRodney W. Grimes 				 * to itself. Allow packets with a SYN and
1059df8bae1dSRodney W. Grimes 				 * an ACK to continue with the processing.
1060df8bae1dSRodney W. Grimes 				 */
1061df8bae1dSRodney W. Grimes 				if (todrop != 0 || (tiflags & TH_ACK) == 0)
1062df8bae1dSRodney W. Grimes 					goto dropafterack;
1063df8bae1dSRodney W. Grimes 			}
1064df8bae1dSRodney W. Grimes 		} else {
1065df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartduppack++;
1066df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartdupbyte += todrop;
1067df8bae1dSRodney W. Grimes 		}
1068df8bae1dSRodney W. Grimes 		m_adj(m, todrop);
1069df8bae1dSRodney W. Grimes 		ti->ti_seq += todrop;
1070df8bae1dSRodney W. Grimes 		ti->ti_len -= todrop;
1071df8bae1dSRodney W. Grimes 		if (ti->ti_urp > todrop)
1072df8bae1dSRodney W. Grimes 			ti->ti_urp -= todrop;
1073df8bae1dSRodney W. Grimes 		else {
1074df8bae1dSRodney W. Grimes 			tiflags &= ~TH_URG;
1075df8bae1dSRodney W. Grimes 			ti->ti_urp = 0;
1076df8bae1dSRodney W. Grimes 		}
1077df8bae1dSRodney W. Grimes 	}
1078df8bae1dSRodney W. Grimes 
1079df8bae1dSRodney W. Grimes 	/*
1080df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
1081df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
1082df8bae1dSRodney W. Grimes 	 */
1083df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
1084df8bae1dSRodney W. Grimes 	    tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
1085df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1086df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvafterclose++;
1087df8bae1dSRodney W. Grimes 		goto dropwithreset;
1088df8bae1dSRodney W. Grimes 	}
1089df8bae1dSRodney W. Grimes 
1090df8bae1dSRodney W. Grimes 	/*
1091df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
1092df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
1093df8bae1dSRodney W. Grimes 	 */
1094df8bae1dSRodney W. Grimes 	todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
1095df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1096df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvpackafterwin++;
1097df8bae1dSRodney W. Grimes 		if (todrop >= ti->ti_len) {
1098df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
1099df8bae1dSRodney W. Grimes 			/*
1100df8bae1dSRodney W. Grimes 			 * If a new connection request is received
1101df8bae1dSRodney W. Grimes 			 * while in TIME_WAIT, drop the old connection
1102df8bae1dSRodney W. Grimes 			 * and start over if the sequence numbers
1103df8bae1dSRodney W. Grimes 			 * are above the previous ones.
1104df8bae1dSRodney W. Grimes 			 */
1105df8bae1dSRodney W. Grimes 			if (tiflags & TH_SYN &&
1106df8bae1dSRodney W. Grimes 			    tp->t_state == TCPS_TIME_WAIT &&
1107df8bae1dSRodney W. Grimes 			    SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
1108df8bae1dSRodney W. Grimes 				iss = tp->rcv_nxt + TCP_ISSINCR;
1109df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
1110df8bae1dSRodney W. Grimes 				goto findpcb;
1111df8bae1dSRodney W. Grimes 			}
1112df8bae1dSRodney W. Grimes 			/*
1113df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
1114df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
1115df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
1116df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
1117df8bae1dSRodney W. Grimes 			 * and ack.
1118df8bae1dSRodney W. Grimes 			 */
1119df8bae1dSRodney W. Grimes 			if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
1120df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
1121df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvwinprobe++;
1122df8bae1dSRodney W. Grimes 			} else
1123df8bae1dSRodney W. Grimes 				goto dropafterack;
1124df8bae1dSRodney W. Grimes 		} else
1125df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
1126df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
1127df8bae1dSRodney W. Grimes 		ti->ti_len -= todrop;
1128df8bae1dSRodney W. Grimes 		tiflags &= ~(TH_PUSH|TH_FIN);
1129df8bae1dSRodney W. Grimes 	}
1130df8bae1dSRodney W. Grimes 
1131df8bae1dSRodney W. Grimes 	/*
1132df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
1133df8bae1dSRodney W. Grimes 	 * record its timestamp.
1134a0292f23SGarrett Wollman 	 * NOTE that the test is modified according to the latest
1135a0292f23SGarrett Wollman 	 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1136df8bae1dSRodney W. Grimes 	 */
1137a0292f23SGarrett Wollman #ifdef TTCP
1138a0292f23SGarrett Wollman 	if ((to.to_flag & TOF_TS) != 0 &&
1139a0292f23SGarrett Wollman 	    SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) {
1140a0292f23SGarrett Wollman 		tp->ts_recent_age = tcp_now;
1141a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
1142a0292f23SGarrett Wollman #else
1143a0292f23SGarrett Wollman 	if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) {
1144df8bae1dSRodney W. Grimes 		tp->ts_recent_age = tcp_now;
1145df8bae1dSRodney W. Grimes 		tp->ts_recent = ts_val;
1146a0292f23SGarrett Wollman #endif
1147df8bae1dSRodney W. Grimes 	}
1148df8bae1dSRodney W. Grimes 
1149df8bae1dSRodney W. Grimes 	/*
1150df8bae1dSRodney W. Grimes 	 * If the RST bit is set examine the state:
1151df8bae1dSRodney W. Grimes 	 *    SYN_RECEIVED STATE:
1152df8bae1dSRodney W. Grimes 	 *	If passive open, return to LISTEN state.
1153df8bae1dSRodney W. Grimes 	 *	If active open, inform user that connection was refused.
1154df8bae1dSRodney W. Grimes 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
1155df8bae1dSRodney W. Grimes 	 *	Inform user that connection was reset, and close tcb.
1156df8bae1dSRodney W. Grimes 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
1157df8bae1dSRodney W. Grimes 	 *	Close the tcb.
1158df8bae1dSRodney W. Grimes 	 */
1159df8bae1dSRodney W. Grimes 	if (tiflags&TH_RST) switch (tp->t_state) {
1160df8bae1dSRodney W. Grimes 
1161df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1162df8bae1dSRodney W. Grimes 		so->so_error = ECONNREFUSED;
1163df8bae1dSRodney W. Grimes 		goto close;
1164df8bae1dSRodney W. Grimes 
1165df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1166df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
1167df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
1168df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1169df8bae1dSRodney W. Grimes 		so->so_error = ECONNRESET;
1170df8bae1dSRodney W. Grimes 	close:
1171df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_CLOSED;
1172df8bae1dSRodney W. Grimes 		tcpstat.tcps_drops++;
1173df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1174df8bae1dSRodney W. Grimes 		goto drop;
1175df8bae1dSRodney W. Grimes 
1176df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
1177df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
1178df8bae1dSRodney W. Grimes 	case TCPS_TIME_WAIT:
1179df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1180df8bae1dSRodney W. Grimes 		goto drop;
1181df8bae1dSRodney W. Grimes 	}
1182df8bae1dSRodney W. Grimes 
1183df8bae1dSRodney W. Grimes 	/*
1184df8bae1dSRodney W. Grimes 	 * If a SYN is in the window, then this is an
1185df8bae1dSRodney W. Grimes 	 * error and we send an RST and drop the connection.
1186df8bae1dSRodney W. Grimes 	 */
1187df8bae1dSRodney W. Grimes 	if (tiflags & TH_SYN) {
1188df8bae1dSRodney W. Grimes 		tp = tcp_drop(tp, ECONNRESET);
1189df8bae1dSRodney W. Grimes 		goto dropwithreset;
1190df8bae1dSRodney W. Grimes 	}
1191df8bae1dSRodney W. Grimes 
1192a0292f23SGarrett Wollman #ifdef TTCP
1193a0292f23SGarrett Wollman 	/*
1194a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
1195a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
1196a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
1197a0292f23SGarrett Wollman 	 */
1198a0292f23SGarrett Wollman 	if ((tiflags & TH_ACK) == 0) {
1199a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
1200a0292f23SGarrett Wollman 		    (tp->t_flags & TF_NEEDSYN))
1201a0292f23SGarrett Wollman 			goto step6;
1202a0292f23SGarrett Wollman 		else
1203a0292f23SGarrett Wollman 			goto drop;
1204a0292f23SGarrett Wollman 	}
1205a0292f23SGarrett Wollman #else
1206df8bae1dSRodney W. Grimes 	/*
1207df8bae1dSRodney W. Grimes 	 * If the ACK bit is off we drop the segment and return.
1208df8bae1dSRodney W. Grimes 	 */
1209df8bae1dSRodney W. Grimes 	if ((tiflags & TH_ACK) == 0)
1210df8bae1dSRodney W. Grimes 		goto drop;
1211a0292f23SGarrett Wollman #endif
1212df8bae1dSRodney W. Grimes 
1213df8bae1dSRodney W. Grimes 	/*
1214df8bae1dSRodney W. Grimes 	 * Ack processing.
1215df8bae1dSRodney W. Grimes 	 */
1216df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1217df8bae1dSRodney W. Grimes 
1218df8bae1dSRodney W. Grimes 	/*
1219df8bae1dSRodney W. Grimes 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
1220df8bae1dSRodney W. Grimes 	 * ESTABLISHED state and continue processing, otherwise
1221df8bae1dSRodney W. Grimes 	 * send an RST.
1222df8bae1dSRodney W. Grimes 	 */
1223df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1224df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
1225df8bae1dSRodney W. Grimes 		    SEQ_GT(ti->ti_ack, tp->snd_max))
1226df8bae1dSRodney W. Grimes 			goto dropwithreset;
1227a0292f23SGarrett Wollman 
1228df8bae1dSRodney W. Grimes 		tcpstat.tcps_connects++;
1229df8bae1dSRodney W. Grimes 		soisconnected(so);
1230df8bae1dSRodney W. Grimes 		/* Do window scaling? */
1231df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1232df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1233df8bae1dSRodney W. Grimes 			tp->snd_scale = tp->requested_s_scale;
1234df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
1235df8bae1dSRodney W. Grimes 		}
1236a0292f23SGarrett Wollman #ifdef TTCP
1237a0292f23SGarrett Wollman 		/*
1238a0292f23SGarrett Wollman 		 * Upon successful completion of 3-way handshake,
1239a0292f23SGarrett Wollman 		 * update cache.CC if it was undefined, pass any queued
1240a0292f23SGarrett Wollman 		 * data to the user, and advance state appropriately.
1241a0292f23SGarrett Wollman 		 */
1242a0292f23SGarrett Wollman 		if ((taop = tcp_gettaocache(inp)) != NULL &&
1243a0292f23SGarrett Wollman 		    taop->tao_cc == 0)
1244a0292f23SGarrett Wollman 			taop->tao_cc = tp->cc_recv;
1245a0292f23SGarrett Wollman 
1246a0292f23SGarrett Wollman 		/*
1247a0292f23SGarrett Wollman 		 * Make transitions:
1248a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
1249a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
1250a0292f23SGarrett Wollman 		 */
1251a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDFIN) {
1252a0292f23SGarrett Wollman 			tp->t_state = TCPS_FIN_WAIT_1;
1253a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDFIN;
1254a0292f23SGarrett Wollman 		} else
1255a0292f23SGarrett Wollman 			tp->t_state = TCPS_ESTABLISHED;
1256a0292f23SGarrett Wollman 		/*
1257a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
1258a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
1259a0292f23SGarrett Wollman 		 */
1260a0292f23SGarrett Wollman 		if (ti->ti_len == 0 && (tiflags & TH_FIN) == 0)
1261a0292f23SGarrett Wollman 			(void) tcp_reass(tp, (struct tcpiphdr *)0,
1262a0292f23SGarrett Wollman 			    (struct mbuf *)0);
1263a0292f23SGarrett Wollman #else /* TTCP */
1264a0292f23SGarrett Wollman 		tp->t_state = TCPS_ESTABLISHED;
1265df8bae1dSRodney W. Grimes 		(void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0);
1266a0292f23SGarrett Wollman #endif /* TTCP */
1267df8bae1dSRodney W. Grimes 		tp->snd_wl1 = ti->ti_seq - 1;
1268df8bae1dSRodney W. Grimes 		/* fall into ... */
1269df8bae1dSRodney W. Grimes 
1270df8bae1dSRodney W. Grimes 	/*
1271df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1272df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
1273df8bae1dSRodney W. Grimes 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
1274df8bae1dSRodney W. Grimes 	 * then advance tp->snd_una to ti->ti_ack and drop
1275df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
1276df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
1277df8bae1dSRodney W. Grimes 	 */
1278df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1279df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
1280df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
1281df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1282df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
1283df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
1284df8bae1dSRodney W. Grimes 	case TCPS_TIME_WAIT:
1285df8bae1dSRodney W. Grimes 
1286df8bae1dSRodney W. Grimes 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
1287df8bae1dSRodney W. Grimes 			if (ti->ti_len == 0 && tiwin == tp->snd_wnd) {
1288df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvdupack++;
1289df8bae1dSRodney W. Grimes 				/*
1290df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
1291df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
1292df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
1293df8bae1dSRodney W. Grimes 				 * change), the ack is the biggest we've
1294df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
1295df8bae1dSRodney W. Grimes 				 * threshhold of them, assume a packet
1296df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
1297df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
1298df8bae1dSRodney W. Grimes 				 * window so we send only this one
1299df8bae1dSRodney W. Grimes 				 * packet.
1300df8bae1dSRodney W. Grimes 				 *
1301df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
1302df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
1303df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
1304df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
1305df8bae1dSRodney W. Grimes 				 * the new ssthresh).
1306df8bae1dSRodney W. Grimes 				 *
1307df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
1308df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
1309df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
1310df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
1311df8bae1dSRodney W. Grimes 				 * network.
1312df8bae1dSRodney W. Grimes 				 */
1313df8bae1dSRodney W. Grimes 				if (tp->t_timer[TCPT_REXMT] == 0 ||
1314df8bae1dSRodney W. Grimes 				    ti->ti_ack != tp->snd_una)
1315df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
1316df8bae1dSRodney W. Grimes 				else if (++tp->t_dupacks == tcprexmtthresh) {
1317df8bae1dSRodney W. Grimes 					tcp_seq onxt = tp->snd_nxt;
1318df8bae1dSRodney W. Grimes 					u_int win =
1319df8bae1dSRodney W. Grimes 					    min(tp->snd_wnd, tp->snd_cwnd) / 2 /
1320df8bae1dSRodney W. Grimes 						tp->t_maxseg;
1321df8bae1dSRodney W. Grimes 
1322df8bae1dSRodney W. Grimes 					if (win < 2)
1323df8bae1dSRodney W. Grimes 						win = 2;
1324df8bae1dSRodney W. Grimes 					tp->snd_ssthresh = win * tp->t_maxseg;
1325df8bae1dSRodney W. Grimes 					tp->t_timer[TCPT_REXMT] = 0;
1326df8bae1dSRodney W. Grimes 					tp->t_rtt = 0;
1327df8bae1dSRodney W. Grimes 					tp->snd_nxt = ti->ti_ack;
1328df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->t_maxseg;
1329df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1330df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
1331df8bae1dSRodney W. Grimes 					       tp->t_maxseg * tp->t_dupacks;
1332df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
1333df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
1334df8bae1dSRodney W. Grimes 					goto drop;
1335df8bae1dSRodney W. Grimes 				} else if (tp->t_dupacks > tcprexmtthresh) {
1336df8bae1dSRodney W. Grimes 					tp->snd_cwnd += tp->t_maxseg;
1337df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1338df8bae1dSRodney W. Grimes 					goto drop;
1339df8bae1dSRodney W. Grimes 				}
1340df8bae1dSRodney W. Grimes 			} else
1341df8bae1dSRodney W. Grimes 				tp->t_dupacks = 0;
1342df8bae1dSRodney W. Grimes 			break;
1343df8bae1dSRodney W. Grimes 		}
1344df8bae1dSRodney W. Grimes 		/*
1345df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
1346df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
1347df8bae1dSRodney W. Grimes 		 */
1348df8bae1dSRodney W. Grimes 		if (tp->t_dupacks > tcprexmtthresh &&
1349df8bae1dSRodney W. Grimes 		    tp->snd_cwnd > tp->snd_ssthresh)
1350df8bae1dSRodney W. Grimes 			tp->snd_cwnd = tp->snd_ssthresh;
1351df8bae1dSRodney W. Grimes 		tp->t_dupacks = 0;
1352df8bae1dSRodney W. Grimes 		if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
1353df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvacktoomuch++;
1354df8bae1dSRodney W. Grimes 			goto dropafterack;
1355df8bae1dSRodney W. Grimes 		}
1356a0292f23SGarrett Wollman #ifdef TTCP
1357a0292f23SGarrett Wollman 		/*
1358a0292f23SGarrett Wollman 		 *  If we reach this point, ACK is not a duplicate,
1359a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
1360a0292f23SGarrett Wollman 		 */
1361a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
1362a0292f23SGarrett Wollman 			/*
1363a0292f23SGarrett Wollman 			 *   T/TCP: Connection was half-synchronized, and our
1364a0292f23SGarrett Wollman 			 *   SYN has been ACK'd (so connection is now fully
1365a0292f23SGarrett Wollman 			 *   synchronized).  Go to non-starred state and
1366a0292f23SGarrett Wollman 			 *   increment snd_una for ACK of SYN.
1367a0292f23SGarrett Wollman 			 */
1368a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
1369a0292f23SGarrett Wollman 			tp->snd_una++;
1370a0292f23SGarrett Wollman 		}
1371a0292f23SGarrett Wollman 
1372a0292f23SGarrett Wollman process_ACK:
1373a0292f23SGarrett Wollman #endif
1374df8bae1dSRodney W. Grimes 		acked = ti->ti_ack - tp->snd_una;
1375df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackpack++;
1376df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackbyte += acked;
1377df8bae1dSRodney W. Grimes 
1378df8bae1dSRodney W. Grimes 		/*
1379df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
1380df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
1381df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
1382df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
1383df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
1384df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
1385df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
1386df8bae1dSRodney W. Grimes 		 */
1387a0292f23SGarrett Wollman #ifdef TTCP
1388a0292f23SGarrett Wollman 		if (to.to_flag & TOF_TS)
1389a0292f23SGarrett Wollman 			tcp_xmit_timer(tp, tcp_now - to.to_tsecr + 1);
1390a0292f23SGarrett Wollman #else
1391df8bae1dSRodney W. Grimes 		if (ts_present)
1392df8bae1dSRodney W. Grimes 			tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
1393a0292f23SGarrett Wollman #endif
1394df8bae1dSRodney W. Grimes 		else if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
1395df8bae1dSRodney W. Grimes 			tcp_xmit_timer(tp,tp->t_rtt);
1396df8bae1dSRodney W. Grimes 
1397df8bae1dSRodney W. Grimes 		/*
1398df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
1399df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
1400df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
1401df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
1402df8bae1dSRodney W. Grimes 		 */
1403df8bae1dSRodney W. Grimes 		if (ti->ti_ack == tp->snd_max) {
1404df8bae1dSRodney W. Grimes 			tp->t_timer[TCPT_REXMT] = 0;
1405df8bae1dSRodney W. Grimes 			needoutput = 1;
1406df8bae1dSRodney W. Grimes 		} else if (tp->t_timer[TCPT_PERSIST] == 0)
1407df8bae1dSRodney W. Grimes 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
1408a0292f23SGarrett Wollman 
1409a0292f23SGarrett Wollman #ifdef TTCP
1410a0292f23SGarrett Wollman 		/*
1411a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
1412a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
1413a0292f23SGarrett Wollman 		 */
1414a0292f23SGarrett Wollman 		if (acked == 0)
1415a0292f23SGarrett Wollman 			goto step6;
1416a0292f23SGarrett Wollman #endif
1417a0292f23SGarrett Wollman 
1418df8bae1dSRodney W. Grimes 		/*
1419df8bae1dSRodney W. Grimes 		 * When new data is acked, open the congestion window.
1420df8bae1dSRodney W. Grimes 		 * If the window gives us less than ssthresh packets
1421df8bae1dSRodney W. Grimes 		 * in flight, open exponentially (maxseg per packet).
1422df8bae1dSRodney W. Grimes 		 * Otherwise open linearly: maxseg per window
142310be5648SGarrett Wollman 		 * (maxseg^2 / cwnd per packet).
1424df8bae1dSRodney W. Grimes 		 */
1425df8bae1dSRodney W. Grimes 		{
1426df8bae1dSRodney W. Grimes 		register u_int cw = tp->snd_cwnd;
1427df8bae1dSRodney W. Grimes 		register u_int incr = tp->t_maxseg;
1428df8bae1dSRodney W. Grimes 
1429df8bae1dSRodney W. Grimes 		if (cw > tp->snd_ssthresh)
143010be5648SGarrett Wollman 			incr = incr * incr / cw;
1431df8bae1dSRodney W. Grimes 		tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale);
1432df8bae1dSRodney W. Grimes 		}
1433df8bae1dSRodney W. Grimes 		if (acked > so->so_snd.sb_cc) {
1434df8bae1dSRodney W. Grimes 			tp->snd_wnd -= so->so_snd.sb_cc;
1435df8bae1dSRodney W. Grimes 			sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
1436df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
1437df8bae1dSRodney W. Grimes 		} else {
1438df8bae1dSRodney W. Grimes 			sbdrop(&so->so_snd, acked);
1439df8bae1dSRodney W. Grimes 			tp->snd_wnd -= acked;
1440df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
1441df8bae1dSRodney W. Grimes 		}
1442df8bae1dSRodney W. Grimes 		if (so->so_snd.sb_flags & SB_NOTIFY)
1443df8bae1dSRodney W. Grimes 			sowwakeup(so);
1444df8bae1dSRodney W. Grimes 		tp->snd_una = ti->ti_ack;
1445df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1446df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
1447df8bae1dSRodney W. Grimes 
1448df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
1449df8bae1dSRodney W. Grimes 
1450df8bae1dSRodney W. Grimes 		/*
1451df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
1452df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
1453df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
1454df8bae1dSRodney W. Grimes 		 */
1455df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
1456df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
1457df8bae1dSRodney W. Grimes 				/*
1458df8bae1dSRodney W. Grimes 				 * If we can't receive any more
1459df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
1460df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
1461df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
1462df8bae1dSRodney W. Grimes 				 * we'll hang forever.
1463df8bae1dSRodney W. Grimes 				 */
1464df8bae1dSRodney W. Grimes 				if (so->so_state & SS_CANTRCVMORE) {
1465df8bae1dSRodney W. Grimes 					soisdisconnected(so);
1466df8bae1dSRodney W. Grimes 					tp->t_timer[TCPT_2MSL] = tcp_maxidle;
1467df8bae1dSRodney W. Grimes 				}
1468df8bae1dSRodney W. Grimes 				tp->t_state = TCPS_FIN_WAIT_2;
1469df8bae1dSRodney W. Grimes 			}
1470df8bae1dSRodney W. Grimes 			break;
1471df8bae1dSRodney W. Grimes 
1472df8bae1dSRodney W. Grimes 	 	/*
1473df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
1474df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
1475df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
1476df8bae1dSRodney W. Grimes 		 * the segment.
1477df8bae1dSRodney W. Grimes 		 */
1478df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
1479df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
1480df8bae1dSRodney W. Grimes 				tp->t_state = TCPS_TIME_WAIT;
1481df8bae1dSRodney W. Grimes 				tcp_canceltimers(tp);
1482a0292f23SGarrett Wollman #ifdef TTCP
1483a0292f23SGarrett Wollman 				/* Shorten TIME_WAIT [RFC-1644, p.28] */
1484a0292f23SGarrett Wollman 				if (tp->cc_recv != 0 &&
1485a0292f23SGarrett Wollman 				    tp->t_duration < TCPTV_MSL)
1486a0292f23SGarrett Wollman 					tp->t_timer[TCPT_2MSL] =
1487a0292f23SGarrett Wollman 					    tp->t_rxtcur * TCPTV_TWTRUNC;
1488a0292f23SGarrett Wollman 				else
1489a0292f23SGarrett Wollman #endif
1490df8bae1dSRodney W. Grimes 					tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1491df8bae1dSRodney W. Grimes 				soisdisconnected(so);
1492df8bae1dSRodney W. Grimes 			}
1493df8bae1dSRodney W. Grimes 			break;
1494df8bae1dSRodney W. Grimes 
1495df8bae1dSRodney W. Grimes 		/*
1496df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
1497df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
1498df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
1499df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
1500df8bae1dSRodney W. Grimes 		 */
1501df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
1502df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
1503df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
1504df8bae1dSRodney W. Grimes 				goto drop;
1505df8bae1dSRodney W. Grimes 			}
1506df8bae1dSRodney W. Grimes 			break;
1507df8bae1dSRodney W. Grimes 
1508df8bae1dSRodney W. Grimes 		/*
1509df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state the only thing that should arrive
1510df8bae1dSRodney W. Grimes 		 * is a retransmission of the remote FIN.  Acknowledge
1511df8bae1dSRodney W. Grimes 		 * it and restart the finack timer.
1512df8bae1dSRodney W. Grimes 		 */
1513df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
1514df8bae1dSRodney W. Grimes 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1515df8bae1dSRodney W. Grimes 			goto dropafterack;
1516df8bae1dSRodney W. Grimes 		}
1517df8bae1dSRodney W. Grimes 	}
1518df8bae1dSRodney W. Grimes 
1519df8bae1dSRodney W. Grimes step6:
1520df8bae1dSRodney W. Grimes 	/*
1521df8bae1dSRodney W. Grimes 	 * Update window information.
1522df8bae1dSRodney W. Grimes 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
1523df8bae1dSRodney W. Grimes 	 */
1524df8bae1dSRodney W. Grimes 	if ((tiflags & TH_ACK) &&
1525623ae52eSPoul-Henning Kamp 	    (SEQ_LT(tp->snd_wl1, ti->ti_seq) ||
1526623ae52eSPoul-Henning Kamp 	    (tp->snd_wl1 == ti->ti_seq && (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
1527623ae52eSPoul-Henning Kamp 	     (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))))) {
1528df8bae1dSRodney W. Grimes 		/* keep track of pure window updates */
1529df8bae1dSRodney W. Grimes 		if (ti->ti_len == 0 &&
1530df8bae1dSRodney W. Grimes 		    tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd)
1531df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvwinupd++;
1532df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
1533df8bae1dSRodney W. Grimes 		tp->snd_wl1 = ti->ti_seq;
1534df8bae1dSRodney W. Grimes 		tp->snd_wl2 = ti->ti_ack;
1535df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
1536df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
1537df8bae1dSRodney W. Grimes 		needoutput = 1;
1538df8bae1dSRodney W. Grimes 	}
1539df8bae1dSRodney W. Grimes 
1540df8bae1dSRodney W. Grimes 	/*
1541df8bae1dSRodney W. Grimes 	 * Process segments with URG.
1542df8bae1dSRodney W. Grimes 	 */
1543df8bae1dSRodney W. Grimes 	if ((tiflags & TH_URG) && ti->ti_urp &&
1544df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1545df8bae1dSRodney W. Grimes 		/*
1546df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
1547df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
1548df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
1549df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
1550df8bae1dSRodney W. Grimes 		 */
1551df8bae1dSRodney W. Grimes 		if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) {
1552df8bae1dSRodney W. Grimes 			ti->ti_urp = 0;			/* XXX */
1553df8bae1dSRodney W. Grimes 			tiflags &= ~TH_URG;		/* XXX */
1554df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
1555df8bae1dSRodney W. Grimes 		}
1556df8bae1dSRodney W. Grimes 		/*
1557df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
1558df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
1559df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1560df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
1561df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
1562df8bae1dSRodney W. Grimes 		 *
1563df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
1564df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
1565df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
1566df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
1567df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
1568df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
1569df8bae1dSRodney W. Grimes 		 */
1570df8bae1dSRodney W. Grimes 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
1571df8bae1dSRodney W. Grimes 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
1572df8bae1dSRodney W. Grimes 			so->so_oobmark = so->so_rcv.sb_cc +
1573df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
1574df8bae1dSRodney W. Grimes 			if (so->so_oobmark == 0)
1575df8bae1dSRodney W. Grimes 				so->so_state |= SS_RCVATMARK;
1576df8bae1dSRodney W. Grimes 			sohasoutofband(so);
1577df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1578df8bae1dSRodney W. Grimes 		}
1579df8bae1dSRodney W. Grimes 		/*
1580df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
1581df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
1582df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
1583df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
1584df8bae1dSRodney W. Grimes 		 */
158526f9a767SRodney W. Grimes 		if (ti->ti_urp <= (u_long)ti->ti_len
1586df8bae1dSRodney W. Grimes #ifdef SO_OOBINLINE
1587df8bae1dSRodney W. Grimes 		     && (so->so_options & SO_OOBINLINE) == 0
1588df8bae1dSRodney W. Grimes #endif
1589df8bae1dSRodney W. Grimes 		     )
1590df8bae1dSRodney W. Grimes 			tcp_pulloutofband(so, ti, m);
1591df8bae1dSRodney W. Grimes 	} else
1592df8bae1dSRodney W. Grimes 		/*
1593df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
1594df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
1595df8bae1dSRodney W. Grimes 		 * with the receive window.
1596df8bae1dSRodney W. Grimes 		 */
1597df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1598df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
1599df8bae1dSRodney W. Grimes dodata:							/* XXX */
1600df8bae1dSRodney W. Grimes 
1601df8bae1dSRodney W. Grimes 	/*
1602df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
1603df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
1604df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
1605df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
1606df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
1607df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
1608df8bae1dSRodney W. Grimes 	 */
1609df8bae1dSRodney W. Grimes 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
1610df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1611df8bae1dSRodney W. Grimes 		TCP_REASS(tp, ti, m, so, tiflags);
1612df8bae1dSRodney W. Grimes 		/*
1613df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
1614df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
1615df8bae1dSRodney W. Grimes 		 * buffer size.
1616df8bae1dSRodney W. Grimes 		 */
1617df8bae1dSRodney W. Grimes 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
1618df8bae1dSRodney W. Grimes 	} else {
1619df8bae1dSRodney W. Grimes 		m_freem(m);
1620df8bae1dSRodney W. Grimes 		tiflags &= ~TH_FIN;
1621df8bae1dSRodney W. Grimes 	}
1622df8bae1dSRodney W. Grimes 
1623df8bae1dSRodney W. Grimes 	/*
1624df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
1625df8bae1dSRodney W. Grimes 	 * that the connection is closing.
1626df8bae1dSRodney W. Grimes 	 */
1627df8bae1dSRodney W. Grimes 	if (tiflags & TH_FIN) {
1628df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1629df8bae1dSRodney W. Grimes 			socantrcvmore(so);
1630a0292f23SGarrett Wollman #ifdef TTCP
1631a0292f23SGarrett Wollman 			/*
1632a0292f23SGarrett Wollman 			 *  If connection is half-synchronized
1633a0292f23SGarrett Wollman 			 *  (ie SEND_SYN flag on) then delay ACK,
1634a0292f23SGarrett Wollman 			 *  so it may be piggybacked when SYN is sent.
1635a0292f23SGarrett Wollman 			 *  Otherwise, since we received a FIN then no
1636a0292f23SGarrett Wollman 			 *  more input can be expected, send ACK now.
1637a0292f23SGarrett Wollman 			 */
1638a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDSYN)
1639a0292f23SGarrett Wollman 				tp->t_flags |= TF_DELACK;
1640a0292f23SGarrett Wollman 			else
1641a0292f23SGarrett Wollman #endif /* TTCP */
1642df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
1643df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
1644df8bae1dSRodney W. Grimes 		}
1645df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
1646df8bae1dSRodney W. Grimes 
1647df8bae1dSRodney W. Grimes 	 	/*
1648df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
1649df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
1650df8bae1dSRodney W. Grimes 		 */
1651df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
1652df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
1653df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSE_WAIT;
1654df8bae1dSRodney W. Grimes 			break;
1655df8bae1dSRodney W. Grimes 
1656df8bae1dSRodney W. Grimes 	 	/*
1657df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1658df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
1659df8bae1dSRodney W. Grimes 		 */
1660df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
1661df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSING;
1662df8bae1dSRodney W. Grimes 			break;
1663df8bae1dSRodney W. Grimes 
1664df8bae1dSRodney W. Grimes 	 	/*
1665df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1666df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
1667df8bae1dSRodney W. Grimes 		 * standard timers.
1668df8bae1dSRodney W. Grimes 		 */
1669df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
1670df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_TIME_WAIT;
1671df8bae1dSRodney W. Grimes 			tcp_canceltimers(tp);
1672a0292f23SGarrett Wollman #ifdef TTCP
1673a0292f23SGarrett Wollman 			/* Shorten TIME_WAIT [RFC-1644, p.28] */
1674a0292f23SGarrett Wollman 			if (tp->cc_recv != 0 &&
1675a0292f23SGarrett Wollman 			    tp->t_duration < TCPTV_MSL) {
1676a0292f23SGarrett Wollman 				tp->t_timer[TCPT_2MSL] =
1677a0292f23SGarrett Wollman 				    tp->t_rxtcur * TCPTV_TWTRUNC;
1678a0292f23SGarrett Wollman 				/* For transaction client, force ACK now. */
1679a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
1680a0292f23SGarrett Wollman 			}
1681a0292f23SGarrett Wollman 			else
1682a0292f23SGarrett Wollman #endif
1683df8bae1dSRodney W. Grimes 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1684df8bae1dSRodney W. Grimes 			soisdisconnected(so);
1685df8bae1dSRodney W. Grimes 			break;
1686df8bae1dSRodney W. Grimes 
1687df8bae1dSRodney W. Grimes 		/*
1688df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1689df8bae1dSRodney W. Grimes 		 */
1690df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
1691df8bae1dSRodney W. Grimes 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1692df8bae1dSRodney W. Grimes 			break;
1693df8bae1dSRodney W. Grimes 		}
1694df8bae1dSRodney W. Grimes 	}
1695610ee2f9SDavid Greenman #ifdef TCPDEBUG
1696df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG)
1697df8bae1dSRodney W. Grimes 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
1698610ee2f9SDavid Greenman #endif
1699df8bae1dSRodney W. Grimes 
1700df8bae1dSRodney W. Grimes 	/*
1701df8bae1dSRodney W. Grimes 	 * Return any desired output.
1702df8bae1dSRodney W. Grimes 	 */
1703df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
1704df8bae1dSRodney W. Grimes 		(void) tcp_output(tp);
1705df8bae1dSRodney W. Grimes 	return;
1706df8bae1dSRodney W. Grimes 
1707df8bae1dSRodney W. Grimes dropafterack:
1708df8bae1dSRodney W. Grimes 	/*
1709df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
1710df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
1711df8bae1dSRodney W. Grimes 	 */
1712df8bae1dSRodney W. Grimes 	if (tiflags & TH_RST)
1713df8bae1dSRodney W. Grimes 		goto drop;
1714a0292f23SGarrett Wollman #ifdef TTCP
1715a0292f23SGarrett Wollman #ifdef TCPDEBUG
1716a0292f23SGarrett Wollman 	if (so->so_options & SO_DEBUG)
1717a0292f23SGarrett Wollman 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1718a0292f23SGarrett Wollman #endif
1719a0292f23SGarrett Wollman #endif
1720df8bae1dSRodney W. Grimes 	m_freem(m);
1721df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
1722df8bae1dSRodney W. Grimes 	(void) tcp_output(tp);
1723df8bae1dSRodney W. Grimes 	return;
1724df8bae1dSRodney W. Grimes 
1725df8bae1dSRodney W. Grimes dropwithreset:
1726df8bae1dSRodney W. Grimes 	/*
1727df8bae1dSRodney W. Grimes 	 * Generate a RST, dropping incoming segment.
1728df8bae1dSRodney W. Grimes 	 * Make ACK acceptable to originator of segment.
1729df8bae1dSRodney W. Grimes 	 * Don't bother to respond if destination was broadcast/multicast.
1730df8bae1dSRodney W. Grimes 	 */
1731df8bae1dSRodney W. Grimes 	if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST) ||
1732d4d0967eSDavid Greenman 	    IN_MULTICAST(ntohl(ti->ti_dst.s_addr)))
1733df8bae1dSRodney W. Grimes 		goto drop;
1734a0292f23SGarrett Wollman #ifdef TTCP
1735a0292f23SGarrett Wollman #ifdef TCPDEBUG
1736a0292f23SGarrett Wollman 	if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
1737a0292f23SGarrett Wollman 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1738a0292f23SGarrett Wollman #endif
1739a0292f23SGarrett Wollman #endif
1740df8bae1dSRodney W. Grimes 	if (tiflags & TH_ACK)
1741df8bae1dSRodney W. Grimes 		tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
1742df8bae1dSRodney W. Grimes 	else {
1743df8bae1dSRodney W. Grimes 		if (tiflags & TH_SYN)
1744df8bae1dSRodney W. Grimes 			ti->ti_len++;
1745df8bae1dSRodney W. Grimes 		tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
1746df8bae1dSRodney W. Grimes 		    TH_RST|TH_ACK);
1747df8bae1dSRodney W. Grimes 	}
1748df8bae1dSRodney W. Grimes 	/* destroy temporarily created socket */
1749df8bae1dSRodney W. Grimes 	if (dropsocket)
1750df8bae1dSRodney W. Grimes 		(void) soabort(so);
1751df8bae1dSRodney W. Grimes 	return;
1752df8bae1dSRodney W. Grimes 
1753df8bae1dSRodney W. Grimes drop:
1754df8bae1dSRodney W. Grimes 	/*
1755df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
1756df8bae1dSRodney W. Grimes 	 */
1757610ee2f9SDavid Greenman #ifdef TCPDEBUG
1758a0292f23SGarrett Wollman #ifdef TTCP
1759a0292f23SGarrett Wollman 	if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
1760a0292f23SGarrett Wollman 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1761a0292f23SGarrett Wollman #else
1762df8bae1dSRodney W. Grimes 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
1763df8bae1dSRodney W. Grimes 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1764610ee2f9SDavid Greenman #endif
1765a0292f23SGarrett Wollman #endif
1766df8bae1dSRodney W. Grimes 	m_freem(m);
1767df8bae1dSRodney W. Grimes 	/* destroy temporarily created socket */
1768df8bae1dSRodney W. Grimes 	if (dropsocket)
1769df8bae1dSRodney W. Grimes 		(void) soabort(so);
1770df8bae1dSRodney W. Grimes 	return;
1771df8bae1dSRodney W. Grimes #ifndef TUBA_INCLUDE
1772df8bae1dSRodney W. Grimes }
1773df8bae1dSRodney W. Grimes 
1774df8bae1dSRodney W. Grimes void
1775a0292f23SGarrett Wollman #ifdef TTCP
1776a0292f23SGarrett Wollman tcp_dooptions(tp, cp, cnt, ti, to)
1777a0292f23SGarrett Wollman #else
1778df8bae1dSRodney W. Grimes tcp_dooptions(tp, cp, cnt, ti, ts_present, ts_val, ts_ecr)
1779a0292f23SGarrett Wollman #endif
1780df8bae1dSRodney W. Grimes 	struct tcpcb *tp;
1781df8bae1dSRodney W. Grimes 	u_char *cp;
1782df8bae1dSRodney W. Grimes 	int cnt;
1783df8bae1dSRodney W. Grimes 	struct tcpiphdr *ti;
1784a0292f23SGarrett Wollman #ifdef TTCP
1785a0292f23SGarrett Wollman 	struct tcpopt *to;
1786a0292f23SGarrett Wollman #else
1787df8bae1dSRodney W. Grimes 	int *ts_present;
1788df8bae1dSRodney W. Grimes 	u_long *ts_val, *ts_ecr;
1789a0292f23SGarrett Wollman #endif
1790df8bae1dSRodney W. Grimes {
1791a0292f23SGarrett Wollman 	u_short mss = 0;
1792df8bae1dSRodney W. Grimes 	int opt, optlen;
1793df8bae1dSRodney W. Grimes 
1794df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1795df8bae1dSRodney W. Grimes 		opt = cp[0];
1796df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
1797df8bae1dSRodney W. Grimes 			break;
1798df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
1799df8bae1dSRodney W. Grimes 			optlen = 1;
1800df8bae1dSRodney W. Grimes 		else {
1801df8bae1dSRodney W. Grimes 			optlen = cp[1];
1802df8bae1dSRodney W. Grimes 			if (optlen <= 0)
1803df8bae1dSRodney W. Grimes 				break;
1804df8bae1dSRodney W. Grimes 		}
1805df8bae1dSRodney W. Grimes 		switch (opt) {
1806df8bae1dSRodney W. Grimes 
1807df8bae1dSRodney W. Grimes 		default:
1808df8bae1dSRodney W. Grimes 			continue;
1809df8bae1dSRodney W. Grimes 
1810df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
1811df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
1812df8bae1dSRodney W. Grimes 				continue;
1813df8bae1dSRodney W. Grimes 			if (!(ti->ti_flags & TH_SYN))
1814df8bae1dSRodney W. Grimes 				continue;
1815df8bae1dSRodney W. Grimes 			bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
1816df8bae1dSRodney W. Grimes 			NTOHS(mss);
1817df8bae1dSRodney W. Grimes 			break;
1818df8bae1dSRodney W. Grimes 
1819df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
1820df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
1821df8bae1dSRodney W. Grimes 				continue;
1822df8bae1dSRodney W. Grimes 			if (!(ti->ti_flags & TH_SYN))
1823df8bae1dSRodney W. Grimes 				continue;
1824df8bae1dSRodney W. Grimes 			tp->t_flags |= TF_RCVD_SCALE;
1825df8bae1dSRodney W. Grimes 			tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
1826df8bae1dSRodney W. Grimes 			break;
1827df8bae1dSRodney W. Grimes 
1828df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
1829df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
1830df8bae1dSRodney W. Grimes 				continue;
1831a0292f23SGarrett Wollman #ifdef TTCP
1832a0292f23SGarrett Wollman 			to->to_flag |= TOF_TS;
1833a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
1834a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
1835a0292f23SGarrett Wollman 			NTOHL(to->to_tsval);
1836a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
1837a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
1838a0292f23SGarrett Wollman 			NTOHL(to->to_tsecr);
1839a0292f23SGarrett Wollman #else
1840df8bae1dSRodney W. Grimes 			*ts_present = 1;
1841df8bae1dSRodney W. Grimes 			bcopy((char *)cp + 2, (char *) ts_val, sizeof(*ts_val));
1842df8bae1dSRodney W. Grimes 			NTOHL(*ts_val);
1843df8bae1dSRodney W. Grimes 			bcopy((char *)cp + 6, (char *) ts_ecr, sizeof(*ts_ecr));
1844df8bae1dSRodney W. Grimes 			NTOHL(*ts_ecr);
1845a0292f23SGarrett Wollman #endif
1846df8bae1dSRodney W. Grimes 
1847df8bae1dSRodney W. Grimes 			/*
1848df8bae1dSRodney W. Grimes 			 * A timestamp received in a SYN makes
1849df8bae1dSRodney W. Grimes 			 * it ok to send timestamp requests and replies.
1850df8bae1dSRodney W. Grimes 			 */
1851df8bae1dSRodney W. Grimes 			if (ti->ti_flags & TH_SYN) {
1852df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_RCVD_TSTMP;
1853a0292f23SGarrett Wollman #ifdef TTCP
1854a0292f23SGarrett Wollman 				tp->ts_recent = to->to_tsval;
1855a0292f23SGarrett Wollman #else
1856df8bae1dSRodney W. Grimes 				tp->ts_recent = *ts_val;
1857a0292f23SGarrett Wollman #endif
1858df8bae1dSRodney W. Grimes 				tp->ts_recent_age = tcp_now;
1859df8bae1dSRodney W. Grimes 			}
1860df8bae1dSRodney W. Grimes 			break;
1861a0292f23SGarrett Wollman #ifdef TTCP
1862a0292f23SGarrett Wollman 		case TCPOPT_CC:
1863a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
1864a0292f23SGarrett Wollman 				continue;
1865a0292f23SGarrett Wollman 			to->to_flag |= TCPOPT_CC;
1866a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
1867a0292f23SGarrett Wollman 			    (char *)&to->to_cc, sizeof(to->to_cc));
1868a0292f23SGarrett Wollman 			NTOHL(to->to_cc);
1869a0292f23SGarrett Wollman 			/*
1870a0292f23SGarrett Wollman 			 * A CC or CC.new option received in a SYN makes
1871a0292f23SGarrett Wollman 			 * it ok to send CC in subsequent segments.
1872a0292f23SGarrett Wollman 			 */
1873a0292f23SGarrett Wollman 			if (ti->ti_flags & TH_SYN)
1874a0292f23SGarrett Wollman 				tp->t_flags |= TF_RCVD_CC;
1875a0292f23SGarrett Wollman 			break;
1876a0292f23SGarrett Wollman 		case TCPOPT_CCNEW:
1877a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
1878a0292f23SGarrett Wollman 				continue;
1879a0292f23SGarrett Wollman 			if (!(ti->ti_flags & TH_SYN))
1880a0292f23SGarrett Wollman 				continue;
1881a0292f23SGarrett Wollman 			to->to_flag |= TOF_CCNEW;
1882a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
1883a0292f23SGarrett Wollman 			    (char *)&to->to_cc, sizeof(to->to_cc));
1884a0292f23SGarrett Wollman 			NTOHL(to->to_cc);
1885a0292f23SGarrett Wollman 			/*
1886a0292f23SGarrett Wollman 			 * A CC or CC.new option received in a SYN makes
1887a0292f23SGarrett Wollman 			 * it ok to send CC in subsequent segments.
1888a0292f23SGarrett Wollman 			 */
1889a0292f23SGarrett Wollman 			tp->t_flags |= TF_RCVD_CC;
1890a0292f23SGarrett Wollman 			break;
1891a0292f23SGarrett Wollman 		case TCPOPT_CCECHO:
1892a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
1893a0292f23SGarrett Wollman 				continue;
1894a0292f23SGarrett Wollman 			if (!(ti->ti_flags & TH_SYN))
1895a0292f23SGarrett Wollman 				continue;
1896a0292f23SGarrett Wollman 			to->to_flag |= TOF_CCECHO;
1897a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
1898a0292f23SGarrett Wollman 			    (char *)&to->to_ccecho, sizeof(to->to_ccecho));
1899a0292f23SGarrett Wollman 			NTOHL(to->to_ccecho);
1900a0292f23SGarrett Wollman 			break;
1901a0292f23SGarrett Wollman #endif /* TTCP*/
1902df8bae1dSRodney W. Grimes 		}
1903df8bae1dSRodney W. Grimes 	}
1904a0292f23SGarrett Wollman 	if (ti->ti_flags & TH_SYN)
1905a0292f23SGarrett Wollman 		tcp_mss(tp, mss);	/* sets t_maxseg */
1906df8bae1dSRodney W. Grimes }
1907df8bae1dSRodney W. Grimes 
1908df8bae1dSRodney W. Grimes /*
1909df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
1910df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
1911df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
1912df8bae1dSRodney W. Grimes  * sequencing purposes.
1913df8bae1dSRodney W. Grimes  */
1914df8bae1dSRodney W. Grimes void
1915df8bae1dSRodney W. Grimes tcp_pulloutofband(so, ti, m)
1916df8bae1dSRodney W. Grimes 	struct socket *so;
1917df8bae1dSRodney W. Grimes 	struct tcpiphdr *ti;
1918df8bae1dSRodney W. Grimes 	register struct mbuf *m;
1919df8bae1dSRodney W. Grimes {
1920df8bae1dSRodney W. Grimes 	int cnt = ti->ti_urp - 1;
1921df8bae1dSRodney W. Grimes 
1922df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
1923df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
1924df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
1925df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
1926df8bae1dSRodney W. Grimes 
1927df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
1928df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
1929df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
1930df8bae1dSRodney W. Grimes 			m->m_len--;
1931df8bae1dSRodney W. Grimes 			return;
1932df8bae1dSRodney W. Grimes 		}
1933df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
1934df8bae1dSRodney W. Grimes 		m = m->m_next;
1935df8bae1dSRodney W. Grimes 		if (m == 0)
1936df8bae1dSRodney W. Grimes 			break;
1937df8bae1dSRodney W. Grimes 	}
1938df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
1939df8bae1dSRodney W. Grimes }
1940df8bae1dSRodney W. Grimes 
1941df8bae1dSRodney W. Grimes /*
1942df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
1943df8bae1dSRodney W. Grimes  * and update averages and current timeout.
1944df8bae1dSRodney W. Grimes  */
1945df8bae1dSRodney W. Grimes void
1946df8bae1dSRodney W. Grimes tcp_xmit_timer(tp, rtt)
1947df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1948df8bae1dSRodney W. Grimes 	short rtt;
1949df8bae1dSRodney W. Grimes {
1950df8bae1dSRodney W. Grimes 	register short delta;
1951df8bae1dSRodney W. Grimes 
1952df8bae1dSRodney W. Grimes 	tcpstat.tcps_rttupdated++;
1953df8bae1dSRodney W. Grimes 	if (tp->t_srtt != 0) {
1954df8bae1dSRodney W. Grimes 		/*
1955df8bae1dSRodney W. Grimes 		 * srtt is stored as fixed point with 3 bits after the
1956df8bae1dSRodney W. Grimes 		 * binary point (i.e., scaled by 8).  The following magic
1957df8bae1dSRodney W. Grimes 		 * is equivalent to the smoothing algorithm in rfc793 with
1958df8bae1dSRodney W. Grimes 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
1959df8bae1dSRodney W. Grimes 		 * point).  Adjust rtt to origin 0.
1960df8bae1dSRodney W. Grimes 		 */
1961df8bae1dSRodney W. Grimes 		delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT);
1962df8bae1dSRodney W. Grimes 		if ((tp->t_srtt += delta) <= 0)
1963df8bae1dSRodney W. Grimes 			tp->t_srtt = 1;
1964df8bae1dSRodney W. Grimes 		/*
1965df8bae1dSRodney W. Grimes 		 * We accumulate a smoothed rtt variance (actually, a
1966df8bae1dSRodney W. Grimes 		 * smoothed mean difference), then set the retransmit
1967df8bae1dSRodney W. Grimes 		 * timer to smoothed rtt + 4 times the smoothed variance.
1968df8bae1dSRodney W. Grimes 		 * rttvar is stored as fixed point with 2 bits after the
1969df8bae1dSRodney W. Grimes 		 * binary point (scaled by 4).  The following is
1970df8bae1dSRodney W. Grimes 		 * equivalent to rfc793 smoothing with an alpha of .75
1971df8bae1dSRodney W. Grimes 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
1972df8bae1dSRodney W. Grimes 		 * rfc793's wired-in beta.
1973df8bae1dSRodney W. Grimes 		 */
1974df8bae1dSRodney W. Grimes 		if (delta < 0)
1975df8bae1dSRodney W. Grimes 			delta = -delta;
1976df8bae1dSRodney W. Grimes 		delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
1977df8bae1dSRodney W. Grimes 		if ((tp->t_rttvar += delta) <= 0)
1978df8bae1dSRodney W. Grimes 			tp->t_rttvar = 1;
1979df8bae1dSRodney W. Grimes 	} else {
1980df8bae1dSRodney W. Grimes 		/*
1981df8bae1dSRodney W. Grimes 		 * No rtt measurement yet - use the unsmoothed rtt.
1982df8bae1dSRodney W. Grimes 		 * Set the variance to half the rtt (so our first
1983df8bae1dSRodney W. Grimes 		 * retransmit happens at 3*rtt).
1984df8bae1dSRodney W. Grimes 		 */
1985df8bae1dSRodney W. Grimes 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
1986df8bae1dSRodney W. Grimes 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
1987df8bae1dSRodney W. Grimes 	}
1988df8bae1dSRodney W. Grimes 	tp->t_rtt = 0;
1989df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
1990df8bae1dSRodney W. Grimes 
1991df8bae1dSRodney W. Grimes 	/*
1992df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
1993df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
1994df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
1995df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
1996df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
1997df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
1998df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
1999df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
2000df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
2001df8bae1dSRodney W. Grimes 	 */
2002df8bae1dSRodney W. Grimes 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
2003df8bae1dSRodney W. Grimes 	    tp->t_rttmin, TCPTV_REXMTMAX);
2004df8bae1dSRodney W. Grimes 
2005df8bae1dSRodney W. Grimes 	/*
2006df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
2007df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
2008df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
2009df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
2010df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
2011df8bae1dSRodney W. Grimes 	 */
2012df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
2013df8bae1dSRodney W. Grimes }
2014df8bae1dSRodney W. Grimes 
2015df8bae1dSRodney W. Grimes /*
2016df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
2017df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
2018df8bae1dSRodney W. Grimes  * If none, use an mss that can be handled on the outgoing
2019df8bae1dSRodney W. Grimes  * interface without forcing IP to fragment; if bigger than
2020df8bae1dSRodney W. Grimes  * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
2021df8bae1dSRodney W. Grimes  * to utilize large mbufs.  If no route is found, route has no mtu,
2022df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
2023df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
2024df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
2025df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
2026df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
2027df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
2028df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
2029a0292f23SGarrett Wollman  *
2030a0292f23SGarrett Wollman  * Also take into account the space needed for options that we
2031a0292f23SGarrett Wollman  * send regularly.  Make maxseg shorter by that amount to assure
2032a0292f23SGarrett Wollman  * that we can send maxseg amount of data even when the options
2033a0292f23SGarrett Wollman  * are present.  Store the upper limit of the length of options plus
2034a0292f23SGarrett Wollman  * data in maxopd.
2035a0292f23SGarrett Wollman  *
2036a0292f23SGarrett Wollman  * NOTE that this routine is only called when we process an incoming
2037a0292f23SGarrett Wollman  * segment, for outgoing segments only tcp_mssopt is called.
2038a0292f23SGarrett Wollman  *
2039a0292f23SGarrett Wollman #ifdef TTCP
2040a0292f23SGarrett Wollman  * In case of T/TCP, we call this routine during implicit connection
2041a0292f23SGarrett Wollman  * setup as well (offer = -1), to initialize maxseg from the cached
2042a0292f23SGarrett Wollman  * MSS of our peer.
2043a0292f23SGarrett Wollman #endif
2044df8bae1dSRodney W. Grimes  */
2045a0292f23SGarrett Wollman void
2046df8bae1dSRodney W. Grimes tcp_mss(tp, offer)
2047a0292f23SGarrett Wollman 	struct tcpcb *tp;
2048a0292f23SGarrett Wollman 	int offer;
2049df8bae1dSRodney W. Grimes {
2050df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
2051df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
2052df8bae1dSRodney W. Grimes 	register int rtt, mss;
2053df8bae1dSRodney W. Grimes 	u_long bufsize;
2054df8bae1dSRodney W. Grimes 	struct inpcb *inp;
2055df8bae1dSRodney W. Grimes 	struct socket *so;
2056a0292f23SGarrett Wollman #ifdef TTCP
2057a0292f23SGarrett Wollman 	struct rmxp_tao *taop;
2058a0292f23SGarrett Wollman 	int origoffer = offer;
2059a0292f23SGarrett Wollman 	extern int tcp_do_rfc1644;
2060a0292f23SGarrett Wollman #endif
2061df8bae1dSRodney W. Grimes 	extern int tcp_mssdflt;
2062a0292f23SGarrett Wollman 	extern int tcp_do_rfc1323;
2063df8bae1dSRodney W. Grimes 
2064df8bae1dSRodney W. Grimes 	inp = tp->t_inpcb;
2065a0292f23SGarrett Wollman 	if ((rt = tcp_rtlookup(inp)) == NULL) {
2066a0292f23SGarrett Wollman 		tp->t_maxopd = tp->t_maxseg = tcp_mssdflt;
2067a0292f23SGarrett Wollman 		return;
2068df8bae1dSRodney W. Grimes 	}
2069df8bae1dSRodney W. Grimes 	ifp = rt->rt_ifp;
2070df8bae1dSRodney W. Grimes 	so = inp->inp_socket;
2071df8bae1dSRodney W. Grimes 
2072a0292f23SGarrett Wollman #ifdef TTCP
2073a0292f23SGarrett Wollman 	taop = rmx_taop(rt->rt_rmx);
2074a0292f23SGarrett Wollman 	/*
2075a0292f23SGarrett Wollman 	 * Offer == -1 means that we didn't receive SYN yet,
2076a0292f23SGarrett Wollman 	 * use cached value in that case;
2077a0292f23SGarrett Wollman 	 */
2078a0292f23SGarrett Wollman 	if (offer == -1)
2079a0292f23SGarrett Wollman 		offer = taop->tao_mssopt;
2080a0292f23SGarrett Wollman #endif /* TTCP */
2081a0292f23SGarrett Wollman 	/*
2082a0292f23SGarrett Wollman 	 * Offer == 0 means that there was no MSS on the SYN segment,
2083a0292f23SGarrett Wollman 	 * in this case we use tcp_mssdflt.
2084a0292f23SGarrett Wollman 	 */
2085a0292f23SGarrett Wollman 	if (offer == 0)
2086a0292f23SGarrett Wollman 		offer = tcp_mssdflt;
2087a0292f23SGarrett Wollman 	else
2088a0292f23SGarrett Wollman 		/*
2089a0292f23SGarrett Wollman 		 * Sanity check: make sure that maxopd will be large
2090a0292f23SGarrett Wollman 		 * enough to allow some data on segments even is the
2091a0292f23SGarrett Wollman 		 * all the option space is used (40bytes).  Otherwise
2092a0292f23SGarrett Wollman 		 * funny things may happen in tcp_output.
2093a0292f23SGarrett Wollman 		 */
2094a0292f23SGarrett Wollman 		offer = max(offer, 64);
2095a0292f23SGarrett Wollman #ifdef TTCP
2096a0292f23SGarrett Wollman 	taop->tao_mssopt = offer;
2097a0292f23SGarrett Wollman #endif /* TTCP */
2098a0292f23SGarrett Wollman 
2099df8bae1dSRodney W. Grimes #ifdef RTV_MTU	/* if route characteristics exist ... */
2100df8bae1dSRodney W. Grimes 	/*
2101df8bae1dSRodney W. Grimes 	 * While we're here, check if there's an initial rtt
2102df8bae1dSRodney W. Grimes 	 * or rttvar.  Convert from the route-table units
2103df8bae1dSRodney W. Grimes 	 * to scaled multiples of the slow timeout timer.
2104df8bae1dSRodney W. Grimes 	 */
2105df8bae1dSRodney W. Grimes 	if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
2106df8bae1dSRodney W. Grimes 		/*
2107a0292f23SGarrett Wollman 		 * XXX the lock bit for RTT indicates that the value
2108df8bae1dSRodney W. Grimes 		 * is also a minimum value; this is subject to time.
2109df8bae1dSRodney W. Grimes 		 */
2110df8bae1dSRodney W. Grimes 		if (rt->rt_rmx.rmx_locks & RTV_RTT)
2111df8bae1dSRodney W. Grimes 			tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ);
2112df8bae1dSRodney W. Grimes 		tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
2113df8bae1dSRodney W. Grimes 		if (rt->rt_rmx.rmx_rttvar)
2114df8bae1dSRodney W. Grimes 			tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
2115df8bae1dSRodney W. Grimes 			    (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
2116df8bae1dSRodney W. Grimes 		else
2117df8bae1dSRodney W. Grimes 			/* default variation is +- 1 rtt */
2118df8bae1dSRodney W. Grimes 			tp->t_rttvar =
2119df8bae1dSRodney W. Grimes 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
2120df8bae1dSRodney W. Grimes 		TCPT_RANGESET(tp->t_rxtcur,
2121df8bae1dSRodney W. Grimes 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
2122df8bae1dSRodney W. Grimes 		    tp->t_rttmin, TCPTV_REXMTMAX);
2123df8bae1dSRodney W. Grimes 	}
2124df8bae1dSRodney W. Grimes 	/*
2125df8bae1dSRodney W. Grimes 	 * if there's an mtu associated with the route, use it
2126df8bae1dSRodney W. Grimes 	 */
2127df8bae1dSRodney W. Grimes 	if (rt->rt_rmx.rmx_mtu)
2128df8bae1dSRodney W. Grimes 		mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
2129df8bae1dSRodney W. Grimes 	else
2130df8bae1dSRodney W. Grimes #endif /* RTV_MTU */
2131df8bae1dSRodney W. Grimes 	{
2132df8bae1dSRodney W. Grimes 		mss = ifp->if_mtu - sizeof(struct tcpiphdr);
2133a0292f23SGarrett Wollman 		if (!in_localaddr(inp->inp_faddr))
2134a0292f23SGarrett Wollman 			mss = min(mss, tcp_mssdflt);
2135a0292f23SGarrett Wollman 	}
2136a0292f23SGarrett Wollman 	mss = min(mss, offer);
2137a0292f23SGarrett Wollman 	/*
2138a0292f23SGarrett Wollman 	 * maxopd stores the maximum length of data AND options
2139a0292f23SGarrett Wollman 	 * in a segment; maxseg is the amount of data in a normal
2140a0292f23SGarrett Wollman 	 * segment.  We need to store this value (maxopd) apart
2141a0292f23SGarrett Wollman 	 * from maxseg, because now every segment carries options
2142a0292f23SGarrett Wollman 	 * and thus we normally have somewhat less data in segments.
2143a0292f23SGarrett Wollman 	 */
2144a0292f23SGarrett Wollman 	tp->t_maxopd = mss;
2145a0292f23SGarrett Wollman 
2146a0292f23SGarrett Wollman #ifdef TTCP
2147a0292f23SGarrett Wollman 	/*
2148a0292f23SGarrett Wollman 	 * In case of T/TCP, origoffer==-1 indicates, that no segments
2149a0292f23SGarrett Wollman 	 * were received yet.  In this case we just guess, otherwise
2150a0292f23SGarrett Wollman 	 * we do the same as before T/TCP.
2151a0292f23SGarrett Wollman 	 */
2152a0292f23SGarrett Wollman  	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
2153a0292f23SGarrett Wollman 	    (origoffer == -1 ||
2154a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
2155a0292f23SGarrett Wollman 		mss -= TCPOLEN_TSTAMP_APPA;
2156a0292f23SGarrett Wollman  	if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
2157a0292f23SGarrett Wollman 	    (origoffer == -1 ||
2158a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC))
2159a0292f23SGarrett Wollman 		mss -= TCPOLEN_CC_APPA;
2160a0292f23SGarrett Wollman #else /* TTCP */
2161a0292f23SGarrett Wollman 	/*
2162a0292f23SGarrett Wollman 	 * Adjust mss to leave space for the usual options.  We're
2163a0292f23SGarrett Wollman 	 * called from the end of tcp_dooptions so we can use the
2164a0292f23SGarrett Wollman 	 * REQ/RCVD flags to see if options will be used.
2165a0292f23SGarrett Wollman 	 */
2166a0292f23SGarrett Wollman  	if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) ==
2167a0292f23SGarrett Wollman 	    (TF_REQ_TSTMP|TF_RCVD_TSTMP))
2168a0292f23SGarrett Wollman 		mss -= TCPOLEN_TSTAMP_APPA;
2169a0292f23SGarrett Wollman #endif /* TTCP */
2170a0292f23SGarrett Wollman 
2171df8bae1dSRodney W. Grimes #if	(MCLBYTES & (MCLBYTES - 1)) == 0
2172df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
2173df8bae1dSRodney W. Grimes 			mss &= ~(MCLBYTES-1);
2174df8bae1dSRodney W. Grimes #else
2175df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
2176df8bae1dSRodney W. Grimes 			mss = mss / MCLBYTES * MCLBYTES;
2177df8bae1dSRodney W. Grimes #endif
2178df8bae1dSRodney W. Grimes 	/*
2179df8bae1dSRodney W. Grimes 	 * If there's a pipesize, change the socket buffer
2180df8bae1dSRodney W. Grimes 	 * to that size.  Make the socket buffers an integral
2181df8bae1dSRodney W. Grimes 	 * number of mss units; if the mss is larger than
2182df8bae1dSRodney W. Grimes 	 * the socket buffer, decrease the mss.
2183df8bae1dSRodney W. Grimes 	 */
2184df8bae1dSRodney W. Grimes #ifdef RTV_SPIPE
2185df8bae1dSRodney W. Grimes 	if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
2186df8bae1dSRodney W. Grimes #endif
2187df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
2188df8bae1dSRodney W. Grimes 	if (bufsize < mss)
2189df8bae1dSRodney W. Grimes 		mss = bufsize;
2190df8bae1dSRodney W. Grimes 	else {
2191df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
2192df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
2193df8bae1dSRodney W. Grimes 			bufsize = sb_max;
2194df8bae1dSRodney W. Grimes 		(void)sbreserve(&so->so_snd, bufsize);
2195df8bae1dSRodney W. Grimes 	}
2196df8bae1dSRodney W. Grimes 	tp->t_maxseg = mss;
2197df8bae1dSRodney W. Grimes 
2198df8bae1dSRodney W. Grimes #ifdef RTV_RPIPE
2199df8bae1dSRodney W. Grimes 	if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
2200df8bae1dSRodney W. Grimes #endif
2201df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
2202df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
2203df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
2204df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
2205df8bae1dSRodney W. Grimes 			bufsize = sb_max;
2206df8bae1dSRodney W. Grimes 		(void)sbreserve(&so->so_rcv, bufsize);
2207df8bae1dSRodney W. Grimes 	}
2208a0292f23SGarrett Wollman #ifdef TTCP
2209a0292f23SGarrett Wollman 	/*
2210a0292f23SGarrett Wollman 	 * Don't force slow-start on local network.
2211a0292f23SGarrett Wollman 	 */
2212a0292f23SGarrett Wollman 	if (!in_localaddr(inp->inp_faddr))
2213a0292f23SGarrett Wollman #endif /* TTCP */
2214df8bae1dSRodney W. Grimes 		tp->snd_cwnd = mss;
2215df8bae1dSRodney W. Grimes 
2216df8bae1dSRodney W. Grimes #ifdef RTV_SSTHRESH
2217df8bae1dSRodney W. Grimes 	if (rt->rt_rmx.rmx_ssthresh) {
2218df8bae1dSRodney W. Grimes 		/*
2219df8bae1dSRodney W. Grimes 		 * There's some sort of gateway or interface
2220df8bae1dSRodney W. Grimes 		 * buffer limit on the path.  Use this to set
2221df8bae1dSRodney W. Grimes 		 * the slow start threshhold, but set the
2222df8bae1dSRodney W. Grimes 		 * threshold to no less than 2*mss.
2223df8bae1dSRodney W. Grimes 		 */
2224df8bae1dSRodney W. Grimes 		tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
2225df8bae1dSRodney W. Grimes 	}
2226a0292f23SGarrett Wollman #endif
2227a0292f23SGarrett Wollman }
2228a0292f23SGarrett Wollman 
2229a0292f23SGarrett Wollman /*
2230a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
2231a0292f23SGarrett Wollman  */
2232a0292f23SGarrett Wollman int
2233a0292f23SGarrett Wollman tcp_mssopt(tp)
2234a0292f23SGarrett Wollman 	struct tcpcb *tp;
2235a0292f23SGarrett Wollman {
2236a0292f23SGarrett Wollman 	struct rtentry *rt;
2237a0292f23SGarrett Wollman 	extern int tcp_mssdflt;
2238a0292f23SGarrett Wollman 
2239a0292f23SGarrett Wollman 	rt = tcp_rtlookup(tp->t_inpcb);
2240a0292f23SGarrett Wollman 	if (rt == NULL)
2241a0292f23SGarrett Wollman 		return tcp_mssdflt;
2242a0292f23SGarrett Wollman 
2243a0292f23SGarrett Wollman 	/*
2244a0292f23SGarrett Wollman 	 * if there's an mtu associated with the route, use it
2245a0292f23SGarrett Wollman 	 */
2246a0292f23SGarrett Wollman 	if (rt->rt_rmx.rmx_mtu)
2247a0292f23SGarrett Wollman 		return rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
2248a0292f23SGarrett Wollman 
2249a0292f23SGarrett Wollman 	return rt->rt_ifp->if_mtu - sizeof(struct tcpiphdr);
2250df8bae1dSRodney W. Grimes }
2251df8bae1dSRodney W. Grimes #endif /* TUBA_INCLUDE */
2252