xref: /freebsd/sys/netinet/tcp_input.c (revision df8bae1de4b67ccf57f4afebd4e2bf258c38910d)
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
34df8bae1dSRodney W. Grimes  */
35df8bae1dSRodney W. Grimes 
36df8bae1dSRodney W. Grimes #ifndef TUBA_INCLUDE
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38df8bae1dSRodney W. Grimes #include <sys/systm.h>
39df8bae1dSRodney W. Grimes #include <sys/malloc.h>
40df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
41df8bae1dSRodney W. Grimes #include <sys/protosw.h>
42df8bae1dSRodney W. Grimes #include <sys/socket.h>
43df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
44df8bae1dSRodney W. Grimes #include <sys/errno.h>
45df8bae1dSRodney W. Grimes 
46df8bae1dSRodney W. Grimes #include <net/if.h>
47df8bae1dSRodney W. Grimes #include <net/route.h>
48df8bae1dSRodney W. Grimes 
49df8bae1dSRodney W. Grimes #include <netinet/in.h>
50df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
51df8bae1dSRodney W. Grimes #include <netinet/ip.h>
52df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
53df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
54df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
55df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
56df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
57df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
58df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
59df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
60df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
61df8bae1dSRodney W. Grimes 
62df8bae1dSRodney W. Grimes int	tcprexmtthresh = 3;
63df8bae1dSRodney W. Grimes struct	tcpiphdr tcp_saveti;
64df8bae1dSRodney W. Grimes struct	inpcb *tcp_last_inpcb = &tcb;
65df8bae1dSRodney W. Grimes 
66df8bae1dSRodney W. Grimes extern u_long sb_max;
67df8bae1dSRodney W. Grimes 
68df8bae1dSRodney W. Grimes #endif /* TUBA_INCLUDE */
69df8bae1dSRodney W. Grimes #define TCP_PAWS_IDLE	(24 * 24 * 60 * 60 * PR_SLOWHZ)
70df8bae1dSRodney W. Grimes 
71df8bae1dSRodney W. Grimes /* for modulo comparisons of timestamps */
72df8bae1dSRodney W. Grimes #define TSTMP_LT(a,b)	((int)((a)-(b)) < 0)
73df8bae1dSRodney W. Grimes #define TSTMP_GEQ(a,b)	((int)((a)-(b)) >= 0)
74df8bae1dSRodney W. Grimes 
75df8bae1dSRodney W. Grimes 
76df8bae1dSRodney W. Grimes /*
77df8bae1dSRodney W. Grimes  * Insert segment ti into reassembly queue of tcp with
78df8bae1dSRodney W. Grimes  * control block tp.  Return TH_FIN if reassembly now includes
79df8bae1dSRodney W. Grimes  * a segment with FIN.  The macro form does the common case inline
80df8bae1dSRodney W. Grimes  * (segment is the next to be received on an established connection,
81df8bae1dSRodney W. Grimes  * and the queue is empty), avoiding linkage into and removal
82df8bae1dSRodney W. Grimes  * from the queue and repetition of various conversions.
83df8bae1dSRodney W. Grimes  * Set DELACK for segments received in order, but ack immediately
84df8bae1dSRodney W. Grimes  * when segments are out of order (so fast retransmit can work).
85df8bae1dSRodney W. Grimes  */
86df8bae1dSRodney W. Grimes #define	TCP_REASS(tp, ti, m, so, flags) { \
87df8bae1dSRodney W. Grimes 	if ((ti)->ti_seq == (tp)->rcv_nxt && \
88df8bae1dSRodney W. Grimes 	    (tp)->seg_next == (struct tcpiphdr *)(tp) && \
89df8bae1dSRodney W. Grimes 	    (tp)->t_state == TCPS_ESTABLISHED) { \
90df8bae1dSRodney W. Grimes 		tp->t_flags |= TF_DELACK; \
91df8bae1dSRodney W. Grimes 		(tp)->rcv_nxt += (ti)->ti_len; \
92df8bae1dSRodney W. Grimes 		flags = (ti)->ti_flags & TH_FIN; \
93df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvpack++;\
94df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvbyte += (ti)->ti_len;\
95df8bae1dSRodney W. Grimes 		sbappend(&(so)->so_rcv, (m)); \
96df8bae1dSRodney W. Grimes 		sorwakeup(so); \
97df8bae1dSRodney W. Grimes 	} else { \
98df8bae1dSRodney W. Grimes 		(flags) = tcp_reass((tp), (ti), (m)); \
99df8bae1dSRodney W. Grimes 		tp->t_flags |= TF_ACKNOW; \
100df8bae1dSRodney W. Grimes 	} \
101df8bae1dSRodney W. Grimes }
102df8bae1dSRodney W. Grimes #ifndef TUBA_INCLUDE
103df8bae1dSRodney W. Grimes 
104df8bae1dSRodney W. Grimes int
105df8bae1dSRodney W. Grimes tcp_reass(tp, ti, m)
106df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
107df8bae1dSRodney W. Grimes 	register struct tcpiphdr *ti;
108df8bae1dSRodney W. Grimes 	struct mbuf *m;
109df8bae1dSRodney W. Grimes {
110df8bae1dSRodney W. Grimes 	register struct tcpiphdr *q;
111df8bae1dSRodney W. Grimes 	struct socket *so = tp->t_inpcb->inp_socket;
112df8bae1dSRodney W. Grimes 	int flags;
113df8bae1dSRodney W. Grimes 
114df8bae1dSRodney W. Grimes 	/*
115df8bae1dSRodney W. Grimes 	 * Call with ti==0 after become established to
116df8bae1dSRodney W. Grimes 	 * force pre-ESTABLISHED data up to user socket.
117df8bae1dSRodney W. Grimes 	 */
118df8bae1dSRodney W. Grimes 	if (ti == 0)
119df8bae1dSRodney W. Grimes 		goto present;
120df8bae1dSRodney W. Grimes 
121df8bae1dSRodney W. Grimes 	/*
122df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
123df8bae1dSRodney W. Grimes 	 */
124df8bae1dSRodney W. Grimes 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
125df8bae1dSRodney W. Grimes 	    q = (struct tcpiphdr *)q->ti_next)
126df8bae1dSRodney W. Grimes 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
127df8bae1dSRodney W. Grimes 			break;
128df8bae1dSRodney W. Grimes 
129df8bae1dSRodney W. Grimes 	/*
130df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
131df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
132df8bae1dSRodney W. Grimes 	 * segment.  If it provides all of our data, drop us.
133df8bae1dSRodney W. Grimes 	 */
134df8bae1dSRodney W. Grimes 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
135df8bae1dSRodney W. Grimes 		register int i;
136df8bae1dSRodney W. Grimes 		q = (struct tcpiphdr *)q->ti_prev;
137df8bae1dSRodney W. Grimes 		/* conversion to int (in i) handles seq wraparound */
138df8bae1dSRodney W. Grimes 		i = q->ti_seq + q->ti_len - ti->ti_seq;
139df8bae1dSRodney W. Grimes 		if (i > 0) {
140df8bae1dSRodney W. Grimes 			if (i >= ti->ti_len) {
141df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvduppack++;
142df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvdupbyte += ti->ti_len;
143df8bae1dSRodney W. Grimes 				m_freem(m);
144df8bae1dSRodney W. Grimes 				return (0);
145df8bae1dSRodney W. Grimes 			}
146df8bae1dSRodney W. Grimes 			m_adj(m, i);
147df8bae1dSRodney W. Grimes 			ti->ti_len -= i;
148df8bae1dSRodney W. Grimes 			ti->ti_seq += i;
149df8bae1dSRodney W. Grimes 		}
150df8bae1dSRodney W. Grimes 		q = (struct tcpiphdr *)(q->ti_next);
151df8bae1dSRodney W. Grimes 	}
152df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvoopack++;
153df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvoobyte += ti->ti_len;
154df8bae1dSRodney W. Grimes 	REASS_MBUF(ti) = m;		/* XXX */
155df8bae1dSRodney W. Grimes 
156df8bae1dSRodney W. Grimes 	/*
157df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
158df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
159df8bae1dSRodney W. Grimes 	 */
160df8bae1dSRodney W. Grimes 	while (q != (struct tcpiphdr *)tp) {
161df8bae1dSRodney W. Grimes 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
162df8bae1dSRodney W. Grimes 		if (i <= 0)
163df8bae1dSRodney W. Grimes 			break;
164df8bae1dSRodney W. Grimes 		if (i < q->ti_len) {
165df8bae1dSRodney W. Grimes 			q->ti_seq += i;
166df8bae1dSRodney W. Grimes 			q->ti_len -= i;
167df8bae1dSRodney W. Grimes 			m_adj(REASS_MBUF(q), i);
168df8bae1dSRodney W. Grimes 			break;
169df8bae1dSRodney W. Grimes 		}
170df8bae1dSRodney W. Grimes 		q = (struct tcpiphdr *)q->ti_next;
171df8bae1dSRodney W. Grimes 		m = REASS_MBUF((struct tcpiphdr *)q->ti_prev);
172df8bae1dSRodney W. Grimes 		remque(q->ti_prev);
173df8bae1dSRodney W. Grimes 		m_freem(m);
174df8bae1dSRodney W. Grimes 	}
175df8bae1dSRodney W. Grimes 
176df8bae1dSRodney W. Grimes 	/*
177df8bae1dSRodney W. Grimes 	 * Stick new segment in its place.
178df8bae1dSRodney W. Grimes 	 */
179df8bae1dSRodney W. Grimes 	insque(ti, q->ti_prev);
180df8bae1dSRodney W. Grimes 
181df8bae1dSRodney W. Grimes present:
182df8bae1dSRodney W. Grimes 	/*
183df8bae1dSRodney W. Grimes 	 * Present data to user, advancing rcv_nxt through
184df8bae1dSRodney W. Grimes 	 * completed sequence space.
185df8bae1dSRodney W. Grimes 	 */
186df8bae1dSRodney W. Grimes 	if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
187df8bae1dSRodney W. Grimes 		return (0);
188df8bae1dSRodney W. Grimes 	ti = tp->seg_next;
189df8bae1dSRodney W. Grimes 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
190df8bae1dSRodney W. Grimes 		return (0);
191df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
192df8bae1dSRodney W. Grimes 		return (0);
193df8bae1dSRodney W. Grimes 	do {
194df8bae1dSRodney W. Grimes 		tp->rcv_nxt += ti->ti_len;
195df8bae1dSRodney W. Grimes 		flags = ti->ti_flags & TH_FIN;
196df8bae1dSRodney W. Grimes 		remque(ti);
197df8bae1dSRodney W. Grimes 		m = REASS_MBUF(ti);
198df8bae1dSRodney W. Grimes 		ti = (struct tcpiphdr *)ti->ti_next;
199df8bae1dSRodney W. Grimes 		if (so->so_state & SS_CANTRCVMORE)
200df8bae1dSRodney W. Grimes 			m_freem(m);
201df8bae1dSRodney W. Grimes 		else
202df8bae1dSRodney W. Grimes 			sbappend(&so->so_rcv, m);
203df8bae1dSRodney W. Grimes 	} while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
204df8bae1dSRodney W. Grimes 	sorwakeup(so);
205df8bae1dSRodney W. Grimes 	return (flags);
206df8bae1dSRodney W. Grimes }
207df8bae1dSRodney W. Grimes 
208df8bae1dSRodney W. Grimes /*
209df8bae1dSRodney W. Grimes  * TCP input routine, follows pages 65-76 of the
210df8bae1dSRodney W. Grimes  * protocol specification dated September, 1981 very closely.
211df8bae1dSRodney W. Grimes  */
212df8bae1dSRodney W. Grimes void
213df8bae1dSRodney W. Grimes tcp_input(m, iphlen)
214df8bae1dSRodney W. Grimes 	register struct mbuf *m;
215df8bae1dSRodney W. Grimes 	int iphlen;
216df8bae1dSRodney W. Grimes {
217df8bae1dSRodney W. Grimes 	register struct tcpiphdr *ti;
218df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
219df8bae1dSRodney W. Grimes 	caddr_t optp = NULL;
220df8bae1dSRodney W. Grimes 	int optlen;
221df8bae1dSRodney W. Grimes 	int len, tlen, off;
222df8bae1dSRodney W. Grimes 	register struct tcpcb *tp = 0;
223df8bae1dSRodney W. Grimes 	register int tiflags;
224df8bae1dSRodney W. Grimes 	struct socket *so;
225df8bae1dSRodney W. Grimes 	int todrop, acked, ourfinisacked, needoutput = 0;
226df8bae1dSRodney W. Grimes 	short ostate;
227df8bae1dSRodney W. Grimes 	struct in_addr laddr;
228df8bae1dSRodney W. Grimes 	int dropsocket = 0;
229df8bae1dSRodney W. Grimes 	int iss = 0;
230df8bae1dSRodney W. Grimes 	u_long tiwin, ts_val, ts_ecr;
231df8bae1dSRodney W. Grimes 	int ts_present = 0;
232df8bae1dSRodney W. Grimes 
233df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvtotal++;
234df8bae1dSRodney W. Grimes 	/*
235df8bae1dSRodney W. Grimes 	 * Get IP and TCP header together in first mbuf.
236df8bae1dSRodney W. Grimes 	 * Note: IP leaves IP header in first mbuf.
237df8bae1dSRodney W. Grimes 	 */
238df8bae1dSRodney W. Grimes 	ti = mtod(m, struct tcpiphdr *);
239df8bae1dSRodney W. Grimes 	if (iphlen > sizeof (struct ip))
240df8bae1dSRodney W. Grimes 		ip_stripoptions(m, (struct mbuf *)0);
241df8bae1dSRodney W. Grimes 	if (m->m_len < sizeof (struct tcpiphdr)) {
242df8bae1dSRodney W. Grimes 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
243df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvshort++;
244df8bae1dSRodney W. Grimes 			return;
245df8bae1dSRodney W. Grimes 		}
246df8bae1dSRodney W. Grimes 		ti = mtod(m, struct tcpiphdr *);
247df8bae1dSRodney W. Grimes 	}
248df8bae1dSRodney W. Grimes 
249df8bae1dSRodney W. Grimes 	/*
250df8bae1dSRodney W. Grimes 	 * Checksum extended TCP header and data.
251df8bae1dSRodney W. Grimes 	 */
252df8bae1dSRodney W. Grimes 	tlen = ((struct ip *)ti)->ip_len;
253df8bae1dSRodney W. Grimes 	len = sizeof (struct ip) + tlen;
254df8bae1dSRodney W. Grimes 	ti->ti_next = ti->ti_prev = 0;
255df8bae1dSRodney W. Grimes 	ti->ti_x1 = 0;
256df8bae1dSRodney W. Grimes 	ti->ti_len = (u_short)tlen;
257df8bae1dSRodney W. Grimes 	HTONS(ti->ti_len);
258df8bae1dSRodney W. Grimes 	if (ti->ti_sum = in_cksum(m, len)) {
259df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvbadsum++;
260df8bae1dSRodney W. Grimes 		goto drop;
261df8bae1dSRodney W. Grimes 	}
262df8bae1dSRodney W. Grimes #endif /* TUBA_INCLUDE */
263df8bae1dSRodney W. Grimes 
264df8bae1dSRodney W. Grimes 	/*
265df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
266df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
267df8bae1dSRodney W. Grimes 	 */
268df8bae1dSRodney W. Grimes 	off = ti->ti_off << 2;
269df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
270df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvbadoff++;
271df8bae1dSRodney W. Grimes 		goto drop;
272df8bae1dSRodney W. Grimes 	}
273df8bae1dSRodney W. Grimes 	tlen -= off;
274df8bae1dSRodney W. Grimes 	ti->ti_len = tlen;
275df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
276df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof(struct ip) + off) {
277df8bae1dSRodney W. Grimes 			if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
278df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvshort++;
279df8bae1dSRodney W. Grimes 				return;
280df8bae1dSRodney W. Grimes 			}
281df8bae1dSRodney W. Grimes 			ti = mtod(m, struct tcpiphdr *);
282df8bae1dSRodney W. Grimes 		}
283df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
284df8bae1dSRodney W. Grimes 		optp = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
285df8bae1dSRodney W. Grimes 		/*
286df8bae1dSRodney W. Grimes 		 * Do quick retrieval of timestamp options ("options
287df8bae1dSRodney W. Grimes 		 * prediction?").  If timestamp is the only option and it's
288df8bae1dSRodney W. Grimes 		 * formatted as recommended in RFC 1323 appendix A, we
289df8bae1dSRodney W. Grimes 		 * quickly get the values now and not bother calling
290df8bae1dSRodney W. Grimes 		 * tcp_dooptions(), etc.
291df8bae1dSRodney W. Grimes 		 */
292df8bae1dSRodney W. Grimes 		if ((optlen == TCPOLEN_TSTAMP_APPA ||
293df8bae1dSRodney W. Grimes 		     (optlen > TCPOLEN_TSTAMP_APPA &&
294df8bae1dSRodney W. Grimes 			optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
295df8bae1dSRodney W. Grimes 		     *(u_long *)optp == htonl(TCPOPT_TSTAMP_HDR) &&
296df8bae1dSRodney W. Grimes 		     (ti->ti_flags & TH_SYN) == 0) {
297df8bae1dSRodney W. Grimes 			ts_present = 1;
298df8bae1dSRodney W. Grimes 			ts_val = ntohl(*(u_long *)(optp + 4));
299df8bae1dSRodney W. Grimes 			ts_ecr = ntohl(*(u_long *)(optp + 8));
300df8bae1dSRodney W. Grimes 			optp = NULL;	/* we've parsed the options */
301df8bae1dSRodney W. Grimes 		}
302df8bae1dSRodney W. Grimes 	}
303df8bae1dSRodney W. Grimes 	tiflags = ti->ti_flags;
304df8bae1dSRodney W. Grimes 
305df8bae1dSRodney W. Grimes 	/*
306df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
307df8bae1dSRodney W. Grimes 	 */
308df8bae1dSRodney W. Grimes 	NTOHL(ti->ti_seq);
309df8bae1dSRodney W. Grimes 	NTOHL(ti->ti_ack);
310df8bae1dSRodney W. Grimes 	NTOHS(ti->ti_win);
311df8bae1dSRodney W. Grimes 	NTOHS(ti->ti_urp);
312df8bae1dSRodney W. Grimes 
313df8bae1dSRodney W. Grimes 	/*
314df8bae1dSRodney W. Grimes 	 * Locate pcb for segment.
315df8bae1dSRodney W. Grimes 	 */
316df8bae1dSRodney W. Grimes findpcb:
317df8bae1dSRodney W. Grimes 	inp = tcp_last_inpcb;
318df8bae1dSRodney W. Grimes 	if (inp->inp_lport != ti->ti_dport ||
319df8bae1dSRodney W. Grimes 	    inp->inp_fport != ti->ti_sport ||
320df8bae1dSRodney W. Grimes 	    inp->inp_faddr.s_addr != ti->ti_src.s_addr ||
321df8bae1dSRodney W. Grimes 	    inp->inp_laddr.s_addr != ti->ti_dst.s_addr) {
322df8bae1dSRodney W. Grimes 		inp = in_pcblookup(&tcb, ti->ti_src, ti->ti_sport,
323df8bae1dSRodney W. Grimes 		    ti->ti_dst, ti->ti_dport, INPLOOKUP_WILDCARD);
324df8bae1dSRodney W. Grimes 		if (inp)
325df8bae1dSRodney W. Grimes 			tcp_last_inpcb = inp;
326df8bae1dSRodney W. Grimes 		++tcpstat.tcps_pcbcachemiss;
327df8bae1dSRodney W. Grimes 	}
328df8bae1dSRodney W. Grimes 
329df8bae1dSRodney W. Grimes 	/*
330df8bae1dSRodney W. Grimes 	 * If the state is CLOSED (i.e., TCB does not exist) then
331df8bae1dSRodney W. Grimes 	 * all data in the incoming segment is discarded.
332df8bae1dSRodney W. Grimes 	 * If the TCB exists but is in CLOSED state, it is embryonic,
333df8bae1dSRodney W. Grimes 	 * but should either do a listen or a connect soon.
334df8bae1dSRodney W. Grimes 	 */
335df8bae1dSRodney W. Grimes 	if (inp == 0)
336df8bae1dSRodney W. Grimes 		goto dropwithreset;
337df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
338df8bae1dSRodney W. Grimes 	if (tp == 0)
339df8bae1dSRodney W. Grimes 		goto dropwithreset;
340df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_CLOSED)
341df8bae1dSRodney W. Grimes 		goto drop;
342df8bae1dSRodney W. Grimes 
343df8bae1dSRodney W. Grimes 	/* Unscale the window into a 32-bit value. */
344df8bae1dSRodney W. Grimes 	if ((tiflags & TH_SYN) == 0)
345df8bae1dSRodney W. Grimes 		tiwin = ti->ti_win << tp->snd_scale;
346df8bae1dSRodney W. Grimes 	else
347df8bae1dSRodney W. Grimes 		tiwin = ti->ti_win;
348df8bae1dSRodney W. Grimes 
349df8bae1dSRodney W. Grimes 	so = inp->inp_socket;
350df8bae1dSRodney W. Grimes 	if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
351df8bae1dSRodney W. Grimes 		if (so->so_options & SO_DEBUG) {
352df8bae1dSRodney W. Grimes 			ostate = tp->t_state;
353df8bae1dSRodney W. Grimes 			tcp_saveti = *ti;
354df8bae1dSRodney W. Grimes 		}
355df8bae1dSRodney W. Grimes 		if (so->so_options & SO_ACCEPTCONN) {
356df8bae1dSRodney W. Grimes 			so = sonewconn(so, 0);
357df8bae1dSRodney W. Grimes 			if (so == 0)
358df8bae1dSRodney W. Grimes 				goto drop;
359df8bae1dSRodney W. Grimes 			/*
360df8bae1dSRodney W. Grimes 			 * This is ugly, but ....
361df8bae1dSRodney W. Grimes 			 *
362df8bae1dSRodney W. Grimes 			 * Mark socket as temporary until we're
363df8bae1dSRodney W. Grimes 			 * committed to keeping it.  The code at
364df8bae1dSRodney W. Grimes 			 * ``drop'' and ``dropwithreset'' check the
365df8bae1dSRodney W. Grimes 			 * flag dropsocket to see if the temporary
366df8bae1dSRodney W. Grimes 			 * socket created here should be discarded.
367df8bae1dSRodney W. Grimes 			 * We mark the socket as discardable until
368df8bae1dSRodney W. Grimes 			 * we're committed to it below in TCPS_LISTEN.
369df8bae1dSRodney W. Grimes 			 */
370df8bae1dSRodney W. Grimes 			dropsocket++;
371df8bae1dSRodney W. Grimes 			inp = (struct inpcb *)so->so_pcb;
372df8bae1dSRodney W. Grimes 			inp->inp_laddr = ti->ti_dst;
373df8bae1dSRodney W. Grimes 			inp->inp_lport = ti->ti_dport;
374df8bae1dSRodney W. Grimes #if BSD>=43
375df8bae1dSRodney W. Grimes 			inp->inp_options = ip_srcroute();
376df8bae1dSRodney W. Grimes #endif
377df8bae1dSRodney W. Grimes 			tp = intotcpcb(inp);
378df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_LISTEN;
379df8bae1dSRodney W. Grimes 
380df8bae1dSRodney W. Grimes 			/* Compute proper scaling value from buffer space
381df8bae1dSRodney W. Grimes 			 */
382df8bae1dSRodney W. Grimes 			while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
383df8bae1dSRodney W. Grimes 			   TCP_MAXWIN << tp->request_r_scale < so->so_rcv.sb_hiwat)
384df8bae1dSRodney W. Grimes 				tp->request_r_scale++;
385df8bae1dSRodney W. Grimes 		}
386df8bae1dSRodney W. Grimes 	}
387df8bae1dSRodney W. Grimes 
388df8bae1dSRodney W. Grimes 	/*
389df8bae1dSRodney W. Grimes 	 * Segment received on connection.
390df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
391df8bae1dSRodney W. Grimes 	 */
392df8bae1dSRodney W. Grimes 	tp->t_idle = 0;
393df8bae1dSRodney W. Grimes 	tp->t_timer[TCPT_KEEP] = tcp_keepidle;
394df8bae1dSRodney W. Grimes 
395df8bae1dSRodney W. Grimes 	/*
396df8bae1dSRodney W. Grimes 	 * Process options if not in LISTEN state,
397df8bae1dSRodney W. Grimes 	 * else do it below (after getting remote address).
398df8bae1dSRodney W. Grimes 	 */
399df8bae1dSRodney W. Grimes 	if (optp && tp->t_state != TCPS_LISTEN)
400df8bae1dSRodney W. Grimes 		tcp_dooptions(tp, optp, optlen, ti,
401df8bae1dSRodney W. Grimes 			&ts_present, &ts_val, &ts_ecr);
402df8bae1dSRodney W. Grimes 
403df8bae1dSRodney W. Grimes 	/*
404df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
405df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
406df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
407df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
408df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
409df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
410df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
411df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
412df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
413df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
414df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
415df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
416df8bae1dSRodney W. Grimes 	 */
417df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
418df8bae1dSRodney W. Grimes 	    (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
419df8bae1dSRodney W. Grimes 	    (!ts_present || TSTMP_GEQ(ts_val, tp->ts_recent)) &&
420df8bae1dSRodney W. Grimes 	    ti->ti_seq == tp->rcv_nxt &&
421df8bae1dSRodney W. Grimes 	    tiwin && tiwin == tp->snd_wnd &&
422df8bae1dSRodney W. Grimes 	    tp->snd_nxt == tp->snd_max) {
423df8bae1dSRodney W. Grimes 
424df8bae1dSRodney W. Grimes 		/*
425df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
426df8bae1dSRodney W. Grimes 		 *  record the timestamp.
427df8bae1dSRodney W. Grimes 		 */
428df8bae1dSRodney W. Grimes 		if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
429df8bae1dSRodney W. Grimes 		   SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len)) {
430df8bae1dSRodney W. Grimes 			tp->ts_recent_age = tcp_now;
431df8bae1dSRodney W. Grimes 			tp->ts_recent = ts_val;
432df8bae1dSRodney W. Grimes 		}
433df8bae1dSRodney W. Grimes 
434df8bae1dSRodney W. Grimes 		if (ti->ti_len == 0) {
435df8bae1dSRodney W. Grimes 			if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
436df8bae1dSRodney W. Grimes 			    SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
437df8bae1dSRodney W. Grimes 			    tp->snd_cwnd >= tp->snd_wnd) {
438df8bae1dSRodney W. Grimes 				/*
439df8bae1dSRodney W. Grimes 				 * this is a pure ack for outstanding data.
440df8bae1dSRodney W. Grimes 				 */
441df8bae1dSRodney W. Grimes 				++tcpstat.tcps_predack;
442df8bae1dSRodney W. Grimes 				if (ts_present)
443df8bae1dSRodney W. Grimes 					tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
444df8bae1dSRodney W. Grimes 				else if (tp->t_rtt &&
445df8bae1dSRodney W. Grimes 					    SEQ_GT(ti->ti_ack, tp->t_rtseq))
446df8bae1dSRodney W. Grimes 					tcp_xmit_timer(tp, tp->t_rtt);
447df8bae1dSRodney W. Grimes 				acked = ti->ti_ack - tp->snd_una;
448df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackpack++;
449df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackbyte += acked;
450df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
451df8bae1dSRodney W. Grimes 				tp->snd_una = ti->ti_ack;
452df8bae1dSRodney W. Grimes 				m_freem(m);
453df8bae1dSRodney W. Grimes 
454df8bae1dSRodney W. Grimes 				/*
455df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
456df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
457df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
458df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
459df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
460df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
461df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
462df8bae1dSRodney W. Grimes 				 */
463df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
464df8bae1dSRodney W. Grimes 					tp->t_timer[TCPT_REXMT] = 0;
465df8bae1dSRodney W. Grimes 				else if (tp->t_timer[TCPT_PERSIST] == 0)
466df8bae1dSRodney W. Grimes 					tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
467df8bae1dSRodney W. Grimes 
468df8bae1dSRodney W. Grimes 				if (so->so_snd.sb_flags & SB_NOTIFY)
469df8bae1dSRodney W. Grimes 					sowwakeup(so);
470df8bae1dSRodney W. Grimes 				if (so->so_snd.sb_cc)
471df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
472df8bae1dSRodney W. Grimes 				return;
473df8bae1dSRodney W. Grimes 			}
474df8bae1dSRodney W. Grimes 		} else if (ti->ti_ack == tp->snd_una &&
475df8bae1dSRodney W. Grimes 		    tp->seg_next == (struct tcpiphdr *)tp &&
476df8bae1dSRodney W. Grimes 		    ti->ti_len <= sbspace(&so->so_rcv)) {
477df8bae1dSRodney W. Grimes 			/*
478df8bae1dSRodney W. Grimes 			 * this is a pure, in-sequence data packet
479df8bae1dSRodney W. Grimes 			 * with nothing on the reassembly queue and
480df8bae1dSRodney W. Grimes 			 * we have enough buffer space to take it.
481df8bae1dSRodney W. Grimes 			 */
482df8bae1dSRodney W. Grimes 			++tcpstat.tcps_preddat;
483df8bae1dSRodney W. Grimes 			tp->rcv_nxt += ti->ti_len;
484df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpack++;
485df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyte += ti->ti_len;
486df8bae1dSRodney W. Grimes 			/*
487df8bae1dSRodney W. Grimes 			 * Drop TCP, IP headers and TCP options then add data
488df8bae1dSRodney W. Grimes 			 * to socket buffer.
489df8bae1dSRodney W. Grimes 			 */
490df8bae1dSRodney W. Grimes 			m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
491df8bae1dSRodney W. Grimes 			m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
492df8bae1dSRodney W. Grimes 			sbappend(&so->so_rcv, m);
493df8bae1dSRodney W. Grimes 			sorwakeup(so);
494df8bae1dSRodney W. Grimes 			tp->t_flags |= TF_DELACK;
495df8bae1dSRodney W. Grimes 			return;
496df8bae1dSRodney W. Grimes 		}
497df8bae1dSRodney W. Grimes 	}
498df8bae1dSRodney W. Grimes 
499df8bae1dSRodney W. Grimes 	/*
500df8bae1dSRodney W. Grimes 	 * Drop TCP, IP headers and TCP options.
501df8bae1dSRodney W. Grimes 	 */
502df8bae1dSRodney W. Grimes 	m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
503df8bae1dSRodney W. Grimes 	m->m_len  -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
504df8bae1dSRodney W. Grimes 
505df8bae1dSRodney W. Grimes 	/*
506df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
507df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
508df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
509df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
510df8bae1dSRodney W. Grimes 	 */
511df8bae1dSRodney W. Grimes 	{ int win;
512df8bae1dSRodney W. Grimes 
513df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
514df8bae1dSRodney W. Grimes 	if (win < 0)
515df8bae1dSRodney W. Grimes 		win = 0;
516df8bae1dSRodney W. Grimes 	tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt));
517df8bae1dSRodney W. Grimes 	}
518df8bae1dSRodney W. Grimes 
519df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
520df8bae1dSRodney W. Grimes 
521df8bae1dSRodney W. Grimes 	/*
522df8bae1dSRodney W. Grimes 	 * If the state is LISTEN then ignore segment if it contains an RST.
523df8bae1dSRodney W. Grimes 	 * If the segment contains an ACK then it is bad and send a RST.
524df8bae1dSRodney W. Grimes 	 * If it does not contain a SYN then it is not interesting; drop it.
525df8bae1dSRodney W. Grimes 	 * Don't bother responding if the destination was a broadcast.
526df8bae1dSRodney W. Grimes 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
527df8bae1dSRodney W. Grimes 	 * tp->iss, and send a segment:
528df8bae1dSRodney W. Grimes 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
529df8bae1dSRodney W. Grimes 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
530df8bae1dSRodney W. Grimes 	 * Fill in remote peer address fields if not previously specified.
531df8bae1dSRodney W. Grimes 	 * Enter SYN_RECEIVED state, and process any other fields of this
532df8bae1dSRodney W. Grimes 	 * segment in this state.
533df8bae1dSRodney W. Grimes 	 */
534df8bae1dSRodney W. Grimes 	case TCPS_LISTEN: {
535df8bae1dSRodney W. Grimes 		struct mbuf *am;
536df8bae1dSRodney W. Grimes 		register struct sockaddr_in *sin;
537df8bae1dSRodney W. Grimes 
538df8bae1dSRodney W. Grimes 		if (tiflags & TH_RST)
539df8bae1dSRodney W. Grimes 			goto drop;
540df8bae1dSRodney W. Grimes 		if (tiflags & TH_ACK)
541df8bae1dSRodney W. Grimes 			goto dropwithreset;
542df8bae1dSRodney W. Grimes 		if ((tiflags & TH_SYN) == 0)
543df8bae1dSRodney W. Grimes 			goto drop;
544df8bae1dSRodney W. Grimes 		/*
545df8bae1dSRodney W. Grimes 		 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
546df8bae1dSRodney W. Grimes 		 * in_broadcast() should never return true on a received
547df8bae1dSRodney W. Grimes 		 * packet with M_BCAST not set.
548df8bae1dSRodney W. Grimes 		 */
549df8bae1dSRodney W. Grimes 		if (m->m_flags & (M_BCAST|M_MCAST) ||
550df8bae1dSRodney W. Grimes 		    IN_MULTICAST(ti->ti_dst.s_addr))
551df8bae1dSRodney W. Grimes 			goto drop;
552df8bae1dSRodney W. Grimes 		am = m_get(M_DONTWAIT, MT_SONAME);	/* XXX */
553df8bae1dSRodney W. Grimes 		if (am == NULL)
554df8bae1dSRodney W. Grimes 			goto drop;
555df8bae1dSRodney W. Grimes 		am->m_len = sizeof (struct sockaddr_in);
556df8bae1dSRodney W. Grimes 		sin = mtod(am, struct sockaddr_in *);
557df8bae1dSRodney W. Grimes 		sin->sin_family = AF_INET;
558df8bae1dSRodney W. Grimes 		sin->sin_len = sizeof(*sin);
559df8bae1dSRodney W. Grimes 		sin->sin_addr = ti->ti_src;
560df8bae1dSRodney W. Grimes 		sin->sin_port = ti->ti_sport;
561df8bae1dSRodney W. Grimes 		bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero));
562df8bae1dSRodney W. Grimes 		laddr = inp->inp_laddr;
563df8bae1dSRodney W. Grimes 		if (inp->inp_laddr.s_addr == INADDR_ANY)
564df8bae1dSRodney W. Grimes 			inp->inp_laddr = ti->ti_dst;
565df8bae1dSRodney W. Grimes 		if (in_pcbconnect(inp, am)) {
566df8bae1dSRodney W. Grimes 			inp->inp_laddr = laddr;
567df8bae1dSRodney W. Grimes 			(void) m_free(am);
568df8bae1dSRodney W. Grimes 			goto drop;
569df8bae1dSRodney W. Grimes 		}
570df8bae1dSRodney W. Grimes 		(void) m_free(am);
571df8bae1dSRodney W. Grimes 		tp->t_template = tcp_template(tp);
572df8bae1dSRodney W. Grimes 		if (tp->t_template == 0) {
573df8bae1dSRodney W. Grimes 			tp = tcp_drop(tp, ENOBUFS);
574df8bae1dSRodney W. Grimes 			dropsocket = 0;		/* socket is already gone */
575df8bae1dSRodney W. Grimes 			goto drop;
576df8bae1dSRodney W. Grimes 		}
577df8bae1dSRodney W. Grimes 		if (optp)
578df8bae1dSRodney W. Grimes 			tcp_dooptions(tp, optp, optlen, ti,
579df8bae1dSRodney W. Grimes 				&ts_present, &ts_val, &ts_ecr);
580df8bae1dSRodney W. Grimes 		if (iss)
581df8bae1dSRodney W. Grimes 			tp->iss = iss;
582df8bae1dSRodney W. Grimes 		else
583df8bae1dSRodney W. Grimes 			tp->iss = tcp_iss;
584df8bae1dSRodney W. Grimes 		tcp_iss += TCP_ISSINCR/2;
585df8bae1dSRodney W. Grimes 		tp->irs = ti->ti_seq;
586df8bae1dSRodney W. Grimes 		tcp_sendseqinit(tp);
587df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
588df8bae1dSRodney W. Grimes 		tp->t_flags |= TF_ACKNOW;
589df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_SYN_RECEIVED;
590df8bae1dSRodney W. Grimes 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
591df8bae1dSRodney W. Grimes 		dropsocket = 0;		/* committed to socket */
592df8bae1dSRodney W. Grimes 		tcpstat.tcps_accepts++;
593df8bae1dSRodney W. Grimes 		goto trimthenstep6;
594df8bae1dSRodney W. Grimes 		}
595df8bae1dSRodney W. Grimes 
596df8bae1dSRodney W. Grimes 	/*
597df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
598df8bae1dSRodney W. Grimes 	 *	if seg contains an ACK, but not for our SYN, drop the input.
599df8bae1dSRodney W. Grimes 	 *	if seg contains a RST, then drop the connection.
600df8bae1dSRodney W. Grimes 	 *	if seg does not contain SYN, then drop it.
601df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
602df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
603df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
604df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
605df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
606df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
607df8bae1dSRodney W. Grimes 	 */
608df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
609df8bae1dSRodney W. Grimes 		if ((tiflags & TH_ACK) &&
610df8bae1dSRodney W. Grimes 		    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
611df8bae1dSRodney W. Grimes 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
612df8bae1dSRodney W. Grimes 			goto dropwithreset;
613df8bae1dSRodney W. Grimes 		if (tiflags & TH_RST) {
614df8bae1dSRodney W. Grimes 			if (tiflags & TH_ACK)
615df8bae1dSRodney W. Grimes 				tp = tcp_drop(tp, ECONNREFUSED);
616df8bae1dSRodney W. Grimes 			goto drop;
617df8bae1dSRodney W. Grimes 		}
618df8bae1dSRodney W. Grimes 		if ((tiflags & TH_SYN) == 0)
619df8bae1dSRodney W. Grimes 			goto drop;
620df8bae1dSRodney W. Grimes 		if (tiflags & TH_ACK) {
621df8bae1dSRodney W. Grimes 			tp->snd_una = ti->ti_ack;
622df8bae1dSRodney W. Grimes 			if (SEQ_LT(tp->snd_nxt, tp->snd_una))
623df8bae1dSRodney W. Grimes 				tp->snd_nxt = tp->snd_una;
624df8bae1dSRodney W. Grimes 		}
625df8bae1dSRodney W. Grimes 		tp->t_timer[TCPT_REXMT] = 0;
626df8bae1dSRodney W. Grimes 		tp->irs = ti->ti_seq;
627df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
628df8bae1dSRodney W. Grimes 		tp->t_flags |= TF_ACKNOW;
629df8bae1dSRodney W. Grimes 		if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
630df8bae1dSRodney W. Grimes 			tcpstat.tcps_connects++;
631df8bae1dSRodney W. Grimes 			soisconnected(so);
632df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_ESTABLISHED;
633df8bae1dSRodney W. Grimes 			/* Do window scaling on this connection? */
634df8bae1dSRodney W. Grimes 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
635df8bae1dSRodney W. Grimes 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
636df8bae1dSRodney W. Grimes 				tp->snd_scale = tp->requested_s_scale;
637df8bae1dSRodney W. Grimes 				tp->rcv_scale = tp->request_r_scale;
638df8bae1dSRodney W. Grimes 			}
639df8bae1dSRodney W. Grimes 			(void) tcp_reass(tp, (struct tcpiphdr *)0,
640df8bae1dSRodney W. Grimes 				(struct mbuf *)0);
641df8bae1dSRodney W. Grimes 			/*
642df8bae1dSRodney W. Grimes 			 * if we didn't have to retransmit the SYN,
643df8bae1dSRodney W. Grimes 			 * use its rtt as our initial srtt & rtt var.
644df8bae1dSRodney W. Grimes 			 */
645df8bae1dSRodney W. Grimes 			if (tp->t_rtt)
646df8bae1dSRodney W. Grimes 				tcp_xmit_timer(tp, tp->t_rtt);
647df8bae1dSRodney W. Grimes 		} else
648df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_SYN_RECEIVED;
649df8bae1dSRodney W. Grimes 
650df8bae1dSRodney W. Grimes trimthenstep6:
651df8bae1dSRodney W. Grimes 		/*
652df8bae1dSRodney W. Grimes 		 * Advance ti->ti_seq to correspond to first data byte.
653df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
654df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
655df8bae1dSRodney W. Grimes 		 */
656df8bae1dSRodney W. Grimes 		ti->ti_seq++;
657df8bae1dSRodney W. Grimes 		if (ti->ti_len > tp->rcv_wnd) {
658df8bae1dSRodney W. Grimes 			todrop = ti->ti_len - tp->rcv_wnd;
659df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
660df8bae1dSRodney W. Grimes 			ti->ti_len = tp->rcv_wnd;
661df8bae1dSRodney W. Grimes 			tiflags &= ~TH_FIN;
662df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpackafterwin++;
663df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
664df8bae1dSRodney W. Grimes 		}
665df8bae1dSRodney W. Grimes 		tp->snd_wl1 = ti->ti_seq - 1;
666df8bae1dSRodney W. Grimes 		tp->rcv_up = ti->ti_seq;
667df8bae1dSRodney W. Grimes 		goto step6;
668df8bae1dSRodney W. Grimes 	}
669df8bae1dSRodney W. Grimes 
670df8bae1dSRodney W. Grimes 	/*
671df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
672df8bae1dSRodney W. Grimes 	 * First check timestamp, if present.
673df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
674df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
675df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
676df8bae1dSRodney W. Grimes 	 *
677df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
678df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
679df8bae1dSRodney W. Grimes 	 */
680df8bae1dSRodney W. Grimes 	if (ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent &&
681df8bae1dSRodney W. Grimes 	    TSTMP_LT(ts_val, tp->ts_recent)) {
682df8bae1dSRodney W. Grimes 
683df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
684df8bae1dSRodney W. Grimes 		if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
685df8bae1dSRodney W. Grimes 			/*
686df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
687df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
688df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
689df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
690df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
691df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
692df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
693df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
694df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
695df8bae1dSRodney W. Grimes 			 */
696df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
697df8bae1dSRodney W. Grimes 		} else {
698df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvduppack++;
699df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvdupbyte += ti->ti_len;
700df8bae1dSRodney W. Grimes 			tcpstat.tcps_pawsdrop++;
701df8bae1dSRodney W. Grimes 			goto dropafterack;
702df8bae1dSRodney W. Grimes 		}
703df8bae1dSRodney W. Grimes 	}
704df8bae1dSRodney W. Grimes 
705df8bae1dSRodney W. Grimes 	todrop = tp->rcv_nxt - ti->ti_seq;
706df8bae1dSRodney W. Grimes 	if (todrop > 0) {
707df8bae1dSRodney W. Grimes 		if (tiflags & TH_SYN) {
708df8bae1dSRodney W. Grimes 			tiflags &= ~TH_SYN;
709df8bae1dSRodney W. Grimes 			ti->ti_seq++;
710df8bae1dSRodney W. Grimes 			if (ti->ti_urp > 1)
711df8bae1dSRodney W. Grimes 				ti->ti_urp--;
712df8bae1dSRodney W. Grimes 			else
713df8bae1dSRodney W. Grimes 				tiflags &= ~TH_URG;
714df8bae1dSRodney W. Grimes 			todrop--;
715df8bae1dSRodney W. Grimes 		}
716df8bae1dSRodney W. Grimes 		if (todrop >= ti->ti_len) {
717df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvduppack++;
718df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvdupbyte += ti->ti_len;
719df8bae1dSRodney W. Grimes 			/*
720df8bae1dSRodney W. Grimes 			 * If segment is just one to the left of the window,
721df8bae1dSRodney W. Grimes 			 * check two special cases:
722df8bae1dSRodney W. Grimes 			 * 1. Don't toss RST in response to 4.2-style keepalive.
723df8bae1dSRodney W. Grimes 			 * 2. If the only thing to drop is a FIN, we can drop
724df8bae1dSRodney W. Grimes 			 *    it, but check the ACK or we will get into FIN
725df8bae1dSRodney W. Grimes 			 *    wars if our FINs crossed (both CLOSING).
726df8bae1dSRodney W. Grimes 			 * In either case, send ACK to resynchronize,
727df8bae1dSRodney W. Grimes 			 * but keep on processing for RST or ACK.
728df8bae1dSRodney W. Grimes 			 */
729df8bae1dSRodney W. Grimes 			if ((tiflags & TH_FIN && todrop == ti->ti_len + 1)
730df8bae1dSRodney W. Grimes #ifdef TCP_COMPAT_42
731df8bae1dSRodney W. Grimes 			  || (tiflags & TH_RST && ti->ti_seq == tp->rcv_nxt - 1)
732df8bae1dSRodney W. Grimes #endif
733df8bae1dSRodney W. Grimes 			   ) {
734df8bae1dSRodney W. Grimes 				todrop = ti->ti_len;
735df8bae1dSRodney W. Grimes 				tiflags &= ~TH_FIN;
736df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
737df8bae1dSRodney W. Grimes 			} else {
738df8bae1dSRodney W. Grimes 				/*
739df8bae1dSRodney W. Grimes 				 * Handle the case when a bound socket connects
740df8bae1dSRodney W. Grimes 				 * to itself. Allow packets with a SYN and
741df8bae1dSRodney W. Grimes 				 * an ACK to continue with the processing.
742df8bae1dSRodney W. Grimes 				 */
743df8bae1dSRodney W. Grimes 				if (todrop != 0 || (tiflags & TH_ACK) == 0)
744df8bae1dSRodney W. Grimes 					goto dropafterack;
745df8bae1dSRodney W. Grimes 			}
746df8bae1dSRodney W. Grimes 		} else {
747df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartduppack++;
748df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartdupbyte += todrop;
749df8bae1dSRodney W. Grimes 		}
750df8bae1dSRodney W. Grimes 		m_adj(m, todrop);
751df8bae1dSRodney W. Grimes 		ti->ti_seq += todrop;
752df8bae1dSRodney W. Grimes 		ti->ti_len -= todrop;
753df8bae1dSRodney W. Grimes 		if (ti->ti_urp > todrop)
754df8bae1dSRodney W. Grimes 			ti->ti_urp -= todrop;
755df8bae1dSRodney W. Grimes 		else {
756df8bae1dSRodney W. Grimes 			tiflags &= ~TH_URG;
757df8bae1dSRodney W. Grimes 			ti->ti_urp = 0;
758df8bae1dSRodney W. Grimes 		}
759df8bae1dSRodney W. Grimes 	}
760df8bae1dSRodney W. Grimes 
761df8bae1dSRodney W. Grimes 	/*
762df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
763df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
764df8bae1dSRodney W. Grimes 	 */
765df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
766df8bae1dSRodney W. Grimes 	    tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
767df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
768df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvafterclose++;
769df8bae1dSRodney W. Grimes 		goto dropwithreset;
770df8bae1dSRodney W. Grimes 	}
771df8bae1dSRodney W. Grimes 
772df8bae1dSRodney W. Grimes 	/*
773df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
774df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
775df8bae1dSRodney W. Grimes 	 */
776df8bae1dSRodney W. Grimes 	todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
777df8bae1dSRodney W. Grimes 	if (todrop > 0) {
778df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvpackafterwin++;
779df8bae1dSRodney W. Grimes 		if (todrop >= ti->ti_len) {
780df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
781df8bae1dSRodney W. Grimes 			/*
782df8bae1dSRodney W. Grimes 			 * If a new connection request is received
783df8bae1dSRodney W. Grimes 			 * while in TIME_WAIT, drop the old connection
784df8bae1dSRodney W. Grimes 			 * and start over if the sequence numbers
785df8bae1dSRodney W. Grimes 			 * are above the previous ones.
786df8bae1dSRodney W. Grimes 			 */
787df8bae1dSRodney W. Grimes 			if (tiflags & TH_SYN &&
788df8bae1dSRodney W. Grimes 			    tp->t_state == TCPS_TIME_WAIT &&
789df8bae1dSRodney W. Grimes 			    SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
790df8bae1dSRodney W. Grimes 				iss = tp->rcv_nxt + TCP_ISSINCR;
791df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
792df8bae1dSRodney W. Grimes 				goto findpcb;
793df8bae1dSRodney W. Grimes 			}
794df8bae1dSRodney W. Grimes 			/*
795df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
796df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
797df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
798df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
799df8bae1dSRodney W. Grimes 			 * and ack.
800df8bae1dSRodney W. Grimes 			 */
801df8bae1dSRodney W. Grimes 			if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
802df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
803df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvwinprobe++;
804df8bae1dSRodney W. Grimes 			} else
805df8bae1dSRodney W. Grimes 				goto dropafterack;
806df8bae1dSRodney W. Grimes 		} else
807df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
808df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
809df8bae1dSRodney W. Grimes 		ti->ti_len -= todrop;
810df8bae1dSRodney W. Grimes 		tiflags &= ~(TH_PUSH|TH_FIN);
811df8bae1dSRodney W. Grimes 	}
812df8bae1dSRodney W. Grimes 
813df8bae1dSRodney W. Grimes 	/*
814df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
815df8bae1dSRodney W. Grimes 	 * record its timestamp.
816df8bae1dSRodney W. Grimes 	 */
817df8bae1dSRodney W. Grimes 	if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
818df8bae1dSRodney W. Grimes 	    SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len +
819df8bae1dSRodney W. Grimes 		   ((tiflags & (TH_SYN|TH_FIN)) != 0))) {
820df8bae1dSRodney W. Grimes 		tp->ts_recent_age = tcp_now;
821df8bae1dSRodney W. Grimes 		tp->ts_recent = ts_val;
822df8bae1dSRodney W. Grimes 	}
823df8bae1dSRodney W. Grimes 
824df8bae1dSRodney W. Grimes 	/*
825df8bae1dSRodney W. Grimes 	 * If the RST bit is set examine the state:
826df8bae1dSRodney W. Grimes 	 *    SYN_RECEIVED STATE:
827df8bae1dSRodney W. Grimes 	 *	If passive open, return to LISTEN state.
828df8bae1dSRodney W. Grimes 	 *	If active open, inform user that connection was refused.
829df8bae1dSRodney W. Grimes 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
830df8bae1dSRodney W. Grimes 	 *	Inform user that connection was reset, and close tcb.
831df8bae1dSRodney W. Grimes 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
832df8bae1dSRodney W. Grimes 	 *	Close the tcb.
833df8bae1dSRodney W. Grimes 	 */
834df8bae1dSRodney W. Grimes 	if (tiflags&TH_RST) switch (tp->t_state) {
835df8bae1dSRodney W. Grimes 
836df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
837df8bae1dSRodney W. Grimes 		so->so_error = ECONNREFUSED;
838df8bae1dSRodney W. Grimes 		goto close;
839df8bae1dSRodney W. Grimes 
840df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
841df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
842df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
843df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
844df8bae1dSRodney W. Grimes 		so->so_error = ECONNRESET;
845df8bae1dSRodney W. Grimes 	close:
846df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_CLOSED;
847df8bae1dSRodney W. Grimes 		tcpstat.tcps_drops++;
848df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
849df8bae1dSRodney W. Grimes 		goto drop;
850df8bae1dSRodney W. Grimes 
851df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
852df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
853df8bae1dSRodney W. Grimes 	case TCPS_TIME_WAIT:
854df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
855df8bae1dSRodney W. Grimes 		goto drop;
856df8bae1dSRodney W. Grimes 	}
857df8bae1dSRodney W. Grimes 
858df8bae1dSRodney W. Grimes 	/*
859df8bae1dSRodney W. Grimes 	 * If a SYN is in the window, then this is an
860df8bae1dSRodney W. Grimes 	 * error and we send an RST and drop the connection.
861df8bae1dSRodney W. Grimes 	 */
862df8bae1dSRodney W. Grimes 	if (tiflags & TH_SYN) {
863df8bae1dSRodney W. Grimes 		tp = tcp_drop(tp, ECONNRESET);
864df8bae1dSRodney W. Grimes 		goto dropwithreset;
865df8bae1dSRodney W. Grimes 	}
866df8bae1dSRodney W. Grimes 
867df8bae1dSRodney W. Grimes 	/*
868df8bae1dSRodney W. Grimes 	 * If the ACK bit is off we drop the segment and return.
869df8bae1dSRodney W. Grimes 	 */
870df8bae1dSRodney W. Grimes 	if ((tiflags & TH_ACK) == 0)
871df8bae1dSRodney W. Grimes 		goto drop;
872df8bae1dSRodney W. Grimes 
873df8bae1dSRodney W. Grimes 	/*
874df8bae1dSRodney W. Grimes 	 * Ack processing.
875df8bae1dSRodney W. Grimes 	 */
876df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
877df8bae1dSRodney W. Grimes 
878df8bae1dSRodney W. Grimes 	/*
879df8bae1dSRodney W. Grimes 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
880df8bae1dSRodney W. Grimes 	 * ESTABLISHED state and continue processing, otherwise
881df8bae1dSRodney W. Grimes 	 * send an RST.
882df8bae1dSRodney W. Grimes 	 */
883df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
884df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
885df8bae1dSRodney W. Grimes 		    SEQ_GT(ti->ti_ack, tp->snd_max))
886df8bae1dSRodney W. Grimes 			goto dropwithreset;
887df8bae1dSRodney W. Grimes 		tcpstat.tcps_connects++;
888df8bae1dSRodney W. Grimes 		soisconnected(so);
889df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_ESTABLISHED;
890df8bae1dSRodney W. Grimes 		/* Do window scaling? */
891df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
892df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
893df8bae1dSRodney W. Grimes 			tp->snd_scale = tp->requested_s_scale;
894df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
895df8bae1dSRodney W. Grimes 		}
896df8bae1dSRodney W. Grimes 		(void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0);
897df8bae1dSRodney W. Grimes 		tp->snd_wl1 = ti->ti_seq - 1;
898df8bae1dSRodney W. Grimes 		/* fall into ... */
899df8bae1dSRodney W. Grimes 
900df8bae1dSRodney W. Grimes 	/*
901df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
902df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
903df8bae1dSRodney W. Grimes 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
904df8bae1dSRodney W. Grimes 	 * then advance tp->snd_una to ti->ti_ack and drop
905df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
906df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
907df8bae1dSRodney W. Grimes 	 */
908df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
909df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
910df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
911df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
912df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
913df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
914df8bae1dSRodney W. Grimes 	case TCPS_TIME_WAIT:
915df8bae1dSRodney W. Grimes 
916df8bae1dSRodney W. Grimes 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
917df8bae1dSRodney W. Grimes 			if (ti->ti_len == 0 && tiwin == tp->snd_wnd) {
918df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvdupack++;
919df8bae1dSRodney W. Grimes 				/*
920df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
921df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
922df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
923df8bae1dSRodney W. Grimes 				 * change), the ack is the biggest we've
924df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
925df8bae1dSRodney W. Grimes 				 * threshhold of them, assume a packet
926df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
927df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
928df8bae1dSRodney W. Grimes 				 * window so we send only this one
929df8bae1dSRodney W. Grimes 				 * packet.
930df8bae1dSRodney W. Grimes 				 *
931df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
932df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
933df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
934df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
935df8bae1dSRodney W. Grimes 				 * the new ssthresh).
936df8bae1dSRodney W. Grimes 				 *
937df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
938df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
939df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
940df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
941df8bae1dSRodney W. Grimes 				 * network.
942df8bae1dSRodney W. Grimes 				 */
943df8bae1dSRodney W. Grimes 				if (tp->t_timer[TCPT_REXMT] == 0 ||
944df8bae1dSRodney W. Grimes 				    ti->ti_ack != tp->snd_una)
945df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
946df8bae1dSRodney W. Grimes 				else if (++tp->t_dupacks == tcprexmtthresh) {
947df8bae1dSRodney W. Grimes 					tcp_seq onxt = tp->snd_nxt;
948df8bae1dSRodney W. Grimes 					u_int win =
949df8bae1dSRodney W. Grimes 					    min(tp->snd_wnd, tp->snd_cwnd) / 2 /
950df8bae1dSRodney W. Grimes 						tp->t_maxseg;
951df8bae1dSRodney W. Grimes 
952df8bae1dSRodney W. Grimes 					if (win < 2)
953df8bae1dSRodney W. Grimes 						win = 2;
954df8bae1dSRodney W. Grimes 					tp->snd_ssthresh = win * tp->t_maxseg;
955df8bae1dSRodney W. Grimes 					tp->t_timer[TCPT_REXMT] = 0;
956df8bae1dSRodney W. Grimes 					tp->t_rtt = 0;
957df8bae1dSRodney W. Grimes 					tp->snd_nxt = ti->ti_ack;
958df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->t_maxseg;
959df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
960df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
961df8bae1dSRodney W. Grimes 					       tp->t_maxseg * tp->t_dupacks;
962df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
963df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
964df8bae1dSRodney W. Grimes 					goto drop;
965df8bae1dSRodney W. Grimes 				} else if (tp->t_dupacks > tcprexmtthresh) {
966df8bae1dSRodney W. Grimes 					tp->snd_cwnd += tp->t_maxseg;
967df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
968df8bae1dSRodney W. Grimes 					goto drop;
969df8bae1dSRodney W. Grimes 				}
970df8bae1dSRodney W. Grimes 			} else
971df8bae1dSRodney W. Grimes 				tp->t_dupacks = 0;
972df8bae1dSRodney W. Grimes 			break;
973df8bae1dSRodney W. Grimes 		}
974df8bae1dSRodney W. Grimes 		/*
975df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
976df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
977df8bae1dSRodney W. Grimes 		 */
978df8bae1dSRodney W. Grimes 		if (tp->t_dupacks > tcprexmtthresh &&
979df8bae1dSRodney W. Grimes 		    tp->snd_cwnd > tp->snd_ssthresh)
980df8bae1dSRodney W. Grimes 			tp->snd_cwnd = tp->snd_ssthresh;
981df8bae1dSRodney W. Grimes 		tp->t_dupacks = 0;
982df8bae1dSRodney W. Grimes 		if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
983df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvacktoomuch++;
984df8bae1dSRodney W. Grimes 			goto dropafterack;
985df8bae1dSRodney W. Grimes 		}
986df8bae1dSRodney W. Grimes 		acked = ti->ti_ack - tp->snd_una;
987df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackpack++;
988df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackbyte += acked;
989df8bae1dSRodney W. Grimes 
990df8bae1dSRodney W. Grimes 		/*
991df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
992df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
993df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
994df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
995df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
996df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
997df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
998df8bae1dSRodney W. Grimes 		 */
999df8bae1dSRodney W. Grimes 		if (ts_present)
1000df8bae1dSRodney W. Grimes 			tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
1001df8bae1dSRodney W. Grimes 		else if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
1002df8bae1dSRodney W. Grimes 			tcp_xmit_timer(tp,tp->t_rtt);
1003df8bae1dSRodney W. Grimes 
1004df8bae1dSRodney W. Grimes 		/*
1005df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
1006df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
1007df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
1008df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
1009df8bae1dSRodney W. Grimes 		 */
1010df8bae1dSRodney W. Grimes 		if (ti->ti_ack == tp->snd_max) {
1011df8bae1dSRodney W. Grimes 			tp->t_timer[TCPT_REXMT] = 0;
1012df8bae1dSRodney W. Grimes 			needoutput = 1;
1013df8bae1dSRodney W. Grimes 		} else if (tp->t_timer[TCPT_PERSIST] == 0)
1014df8bae1dSRodney W. Grimes 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
1015df8bae1dSRodney W. Grimes 		/*
1016df8bae1dSRodney W. Grimes 		 * When new data is acked, open the congestion window.
1017df8bae1dSRodney W. Grimes 		 * If the window gives us less than ssthresh packets
1018df8bae1dSRodney W. Grimes 		 * in flight, open exponentially (maxseg per packet).
1019df8bae1dSRodney W. Grimes 		 * Otherwise open linearly: maxseg per window
1020df8bae1dSRodney W. Grimes 		 * (maxseg^2 / cwnd per packet), plus a constant
1021df8bae1dSRodney W. Grimes 		 * fraction of a packet (maxseg/8) to help larger windows
1022df8bae1dSRodney W. Grimes 		 * open quickly enough.
1023df8bae1dSRodney W. Grimes 		 */
1024df8bae1dSRodney W. Grimes 		{
1025df8bae1dSRodney W. Grimes 		register u_int cw = tp->snd_cwnd;
1026df8bae1dSRodney W. Grimes 		register u_int incr = tp->t_maxseg;
1027df8bae1dSRodney W. Grimes 
1028df8bae1dSRodney W. Grimes 		if (cw > tp->snd_ssthresh)
1029df8bae1dSRodney W. Grimes 			incr = incr * incr / cw + incr / 8;
1030df8bae1dSRodney W. Grimes 		tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale);
1031df8bae1dSRodney W. Grimes 		}
1032df8bae1dSRodney W. Grimes 		if (acked > so->so_snd.sb_cc) {
1033df8bae1dSRodney W. Grimes 			tp->snd_wnd -= so->so_snd.sb_cc;
1034df8bae1dSRodney W. Grimes 			sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
1035df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
1036df8bae1dSRodney W. Grimes 		} else {
1037df8bae1dSRodney W. Grimes 			sbdrop(&so->so_snd, acked);
1038df8bae1dSRodney W. Grimes 			tp->snd_wnd -= acked;
1039df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
1040df8bae1dSRodney W. Grimes 		}
1041df8bae1dSRodney W. Grimes 		if (so->so_snd.sb_flags & SB_NOTIFY)
1042df8bae1dSRodney W. Grimes 			sowwakeup(so);
1043df8bae1dSRodney W. Grimes 		tp->snd_una = ti->ti_ack;
1044df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1045df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
1046df8bae1dSRodney W. Grimes 
1047df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
1048df8bae1dSRodney W. Grimes 
1049df8bae1dSRodney W. Grimes 		/*
1050df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
1051df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
1052df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
1053df8bae1dSRodney W. Grimes 		 */
1054df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
1055df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
1056df8bae1dSRodney W. Grimes 				/*
1057df8bae1dSRodney W. Grimes 				 * If we can't receive any more
1058df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
1059df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
1060df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
1061df8bae1dSRodney W. Grimes 				 * we'll hang forever.
1062df8bae1dSRodney W. Grimes 				 */
1063df8bae1dSRodney W. Grimes 				if (so->so_state & SS_CANTRCVMORE) {
1064df8bae1dSRodney W. Grimes 					soisdisconnected(so);
1065df8bae1dSRodney W. Grimes 					tp->t_timer[TCPT_2MSL] = tcp_maxidle;
1066df8bae1dSRodney W. Grimes 				}
1067df8bae1dSRodney W. Grimes 				tp->t_state = TCPS_FIN_WAIT_2;
1068df8bae1dSRodney W. Grimes 			}
1069df8bae1dSRodney W. Grimes 			break;
1070df8bae1dSRodney W. Grimes 
1071df8bae1dSRodney W. Grimes 	 	/*
1072df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
1073df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
1074df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
1075df8bae1dSRodney W. Grimes 		 * the segment.
1076df8bae1dSRodney W. Grimes 		 */
1077df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
1078df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
1079df8bae1dSRodney W. Grimes 				tp->t_state = TCPS_TIME_WAIT;
1080df8bae1dSRodney W. Grimes 				tcp_canceltimers(tp);
1081df8bae1dSRodney W. Grimes 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1082df8bae1dSRodney W. Grimes 				soisdisconnected(so);
1083df8bae1dSRodney W. Grimes 			}
1084df8bae1dSRodney W. Grimes 			break;
1085df8bae1dSRodney W. Grimes 
1086df8bae1dSRodney W. Grimes 		/*
1087df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
1088df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
1089df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
1090df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
1091df8bae1dSRodney W. Grimes 		 */
1092df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
1093df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
1094df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
1095df8bae1dSRodney W. Grimes 				goto drop;
1096df8bae1dSRodney W. Grimes 			}
1097df8bae1dSRodney W. Grimes 			break;
1098df8bae1dSRodney W. Grimes 
1099df8bae1dSRodney W. Grimes 		/*
1100df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state the only thing that should arrive
1101df8bae1dSRodney W. Grimes 		 * is a retransmission of the remote FIN.  Acknowledge
1102df8bae1dSRodney W. Grimes 		 * it and restart the finack timer.
1103df8bae1dSRodney W. Grimes 		 */
1104df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
1105df8bae1dSRodney W. Grimes 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1106df8bae1dSRodney W. Grimes 			goto dropafterack;
1107df8bae1dSRodney W. Grimes 		}
1108df8bae1dSRodney W. Grimes 	}
1109df8bae1dSRodney W. Grimes 
1110df8bae1dSRodney W. Grimes step6:
1111df8bae1dSRodney W. Grimes 	/*
1112df8bae1dSRodney W. Grimes 	 * Update window information.
1113df8bae1dSRodney W. Grimes 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
1114df8bae1dSRodney W. Grimes 	 */
1115df8bae1dSRodney W. Grimes 	if ((tiflags & TH_ACK) &&
1116df8bae1dSRodney W. Grimes 	    (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
1117df8bae1dSRodney W. Grimes 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
1118df8bae1dSRodney W. Grimes 	     tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))) {
1119df8bae1dSRodney W. Grimes 		/* keep track of pure window updates */
1120df8bae1dSRodney W. Grimes 		if (ti->ti_len == 0 &&
1121df8bae1dSRodney W. Grimes 		    tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd)
1122df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvwinupd++;
1123df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
1124df8bae1dSRodney W. Grimes 		tp->snd_wl1 = ti->ti_seq;
1125df8bae1dSRodney W. Grimes 		tp->snd_wl2 = ti->ti_ack;
1126df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
1127df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
1128df8bae1dSRodney W. Grimes 		needoutput = 1;
1129df8bae1dSRodney W. Grimes 	}
1130df8bae1dSRodney W. Grimes 
1131df8bae1dSRodney W. Grimes 	/*
1132df8bae1dSRodney W. Grimes 	 * Process segments with URG.
1133df8bae1dSRodney W. Grimes 	 */
1134df8bae1dSRodney W. Grimes 	if ((tiflags & TH_URG) && ti->ti_urp &&
1135df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1136df8bae1dSRodney W. Grimes 		/*
1137df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
1138df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
1139df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
1140df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
1141df8bae1dSRodney W. Grimes 		 */
1142df8bae1dSRodney W. Grimes 		if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) {
1143df8bae1dSRodney W. Grimes 			ti->ti_urp = 0;			/* XXX */
1144df8bae1dSRodney W. Grimes 			tiflags &= ~TH_URG;		/* XXX */
1145df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
1146df8bae1dSRodney W. Grimes 		}
1147df8bae1dSRodney W. Grimes 		/*
1148df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
1149df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
1150df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1151df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
1152df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
1153df8bae1dSRodney W. Grimes 		 *
1154df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
1155df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
1156df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
1157df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
1158df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
1159df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
1160df8bae1dSRodney W. Grimes 		 */
1161df8bae1dSRodney W. Grimes 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
1162df8bae1dSRodney W. Grimes 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
1163df8bae1dSRodney W. Grimes 			so->so_oobmark = so->so_rcv.sb_cc +
1164df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
1165df8bae1dSRodney W. Grimes 			if (so->so_oobmark == 0)
1166df8bae1dSRodney W. Grimes 				so->so_state |= SS_RCVATMARK;
1167df8bae1dSRodney W. Grimes 			sohasoutofband(so);
1168df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1169df8bae1dSRodney W. Grimes 		}
1170df8bae1dSRodney W. Grimes 		/*
1171df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
1172df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
1173df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
1174df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
1175df8bae1dSRodney W. Grimes 		 */
1176df8bae1dSRodney W. Grimes 		if (ti->ti_urp <= ti->ti_len
1177df8bae1dSRodney W. Grimes #ifdef SO_OOBINLINE
1178df8bae1dSRodney W. Grimes 		     && (so->so_options & SO_OOBINLINE) == 0
1179df8bae1dSRodney W. Grimes #endif
1180df8bae1dSRodney W. Grimes 		     )
1181df8bae1dSRodney W. Grimes 			tcp_pulloutofband(so, ti, m);
1182df8bae1dSRodney W. Grimes 	} else
1183df8bae1dSRodney W. Grimes 		/*
1184df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
1185df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
1186df8bae1dSRodney W. Grimes 		 * with the receive window.
1187df8bae1dSRodney W. Grimes 		 */
1188df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1189df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
1190df8bae1dSRodney W. Grimes dodata:							/* XXX */
1191df8bae1dSRodney W. Grimes 
1192df8bae1dSRodney W. Grimes 	/*
1193df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
1194df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
1195df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
1196df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
1197df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
1198df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
1199df8bae1dSRodney W. Grimes 	 */
1200df8bae1dSRodney W. Grimes 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
1201df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1202df8bae1dSRodney W. Grimes 		TCP_REASS(tp, ti, m, so, tiflags);
1203df8bae1dSRodney W. Grimes 		/*
1204df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
1205df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
1206df8bae1dSRodney W. Grimes 		 * buffer size.
1207df8bae1dSRodney W. Grimes 		 */
1208df8bae1dSRodney W. Grimes 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
1209df8bae1dSRodney W. Grimes 	} else {
1210df8bae1dSRodney W. Grimes 		m_freem(m);
1211df8bae1dSRodney W. Grimes 		tiflags &= ~TH_FIN;
1212df8bae1dSRodney W. Grimes 	}
1213df8bae1dSRodney W. Grimes 
1214df8bae1dSRodney W. Grimes 	/*
1215df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
1216df8bae1dSRodney W. Grimes 	 * that the connection is closing.
1217df8bae1dSRodney W. Grimes 	 */
1218df8bae1dSRodney W. Grimes 	if (tiflags & TH_FIN) {
1219df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1220df8bae1dSRodney W. Grimes 			socantrcvmore(so);
1221df8bae1dSRodney W. Grimes 			tp->t_flags |= TF_ACKNOW;
1222df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
1223df8bae1dSRodney W. Grimes 		}
1224df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
1225df8bae1dSRodney W. Grimes 
1226df8bae1dSRodney W. Grimes 	 	/*
1227df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
1228df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
1229df8bae1dSRodney W. Grimes 		 */
1230df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
1231df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
1232df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSE_WAIT;
1233df8bae1dSRodney W. Grimes 			break;
1234df8bae1dSRodney W. Grimes 
1235df8bae1dSRodney W. Grimes 	 	/*
1236df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1237df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
1238df8bae1dSRodney W. Grimes 		 */
1239df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
1240df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSING;
1241df8bae1dSRodney W. Grimes 			break;
1242df8bae1dSRodney W. Grimes 
1243df8bae1dSRodney W. Grimes 	 	/*
1244df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1245df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
1246df8bae1dSRodney W. Grimes 		 * standard timers.
1247df8bae1dSRodney W. Grimes 		 */
1248df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
1249df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_TIME_WAIT;
1250df8bae1dSRodney W. Grimes 			tcp_canceltimers(tp);
1251df8bae1dSRodney W. Grimes 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1252df8bae1dSRodney W. Grimes 			soisdisconnected(so);
1253df8bae1dSRodney W. Grimes 			break;
1254df8bae1dSRodney W. Grimes 
1255df8bae1dSRodney W. Grimes 		/*
1256df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1257df8bae1dSRodney W. Grimes 		 */
1258df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
1259df8bae1dSRodney W. Grimes 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1260df8bae1dSRodney W. Grimes 			break;
1261df8bae1dSRodney W. Grimes 		}
1262df8bae1dSRodney W. Grimes 	}
1263df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG)
1264df8bae1dSRodney W. Grimes 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
1265df8bae1dSRodney W. Grimes 
1266df8bae1dSRodney W. Grimes 	/*
1267df8bae1dSRodney W. Grimes 	 * Return any desired output.
1268df8bae1dSRodney W. Grimes 	 */
1269df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
1270df8bae1dSRodney W. Grimes 		(void) tcp_output(tp);
1271df8bae1dSRodney W. Grimes 	return;
1272df8bae1dSRodney W. Grimes 
1273df8bae1dSRodney W. Grimes dropafterack:
1274df8bae1dSRodney W. Grimes 	/*
1275df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
1276df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
1277df8bae1dSRodney W. Grimes 	 */
1278df8bae1dSRodney W. Grimes 	if (tiflags & TH_RST)
1279df8bae1dSRodney W. Grimes 		goto drop;
1280df8bae1dSRodney W. Grimes 	m_freem(m);
1281df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
1282df8bae1dSRodney W. Grimes 	(void) tcp_output(tp);
1283df8bae1dSRodney W. Grimes 	return;
1284df8bae1dSRodney W. Grimes 
1285df8bae1dSRodney W. Grimes dropwithreset:
1286df8bae1dSRodney W. Grimes 	/*
1287df8bae1dSRodney W. Grimes 	 * Generate a RST, dropping incoming segment.
1288df8bae1dSRodney W. Grimes 	 * Make ACK acceptable to originator of segment.
1289df8bae1dSRodney W. Grimes 	 * Don't bother to respond if destination was broadcast/multicast.
1290df8bae1dSRodney W. Grimes 	 */
1291df8bae1dSRodney W. Grimes 	if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST) ||
1292df8bae1dSRodney W. Grimes 	    IN_MULTICAST(ti->ti_dst.s_addr))
1293df8bae1dSRodney W. Grimes 		goto drop;
1294df8bae1dSRodney W. Grimes 	if (tiflags & TH_ACK)
1295df8bae1dSRodney W. Grimes 		tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
1296df8bae1dSRodney W. Grimes 	else {
1297df8bae1dSRodney W. Grimes 		if (tiflags & TH_SYN)
1298df8bae1dSRodney W. Grimes 			ti->ti_len++;
1299df8bae1dSRodney W. Grimes 		tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
1300df8bae1dSRodney W. Grimes 		    TH_RST|TH_ACK);
1301df8bae1dSRodney W. Grimes 	}
1302df8bae1dSRodney W. Grimes 	/* destroy temporarily created socket */
1303df8bae1dSRodney W. Grimes 	if (dropsocket)
1304df8bae1dSRodney W. Grimes 		(void) soabort(so);
1305df8bae1dSRodney W. Grimes 	return;
1306df8bae1dSRodney W. Grimes 
1307df8bae1dSRodney W. Grimes drop:
1308df8bae1dSRodney W. Grimes 	/*
1309df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
1310df8bae1dSRodney W. Grimes 	 */
1311df8bae1dSRodney W. Grimes 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
1312df8bae1dSRodney W. Grimes 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1313df8bae1dSRodney W. Grimes 	m_freem(m);
1314df8bae1dSRodney W. Grimes 	/* destroy temporarily created socket */
1315df8bae1dSRodney W. Grimes 	if (dropsocket)
1316df8bae1dSRodney W. Grimes 		(void) soabort(so);
1317df8bae1dSRodney W. Grimes 	return;
1318df8bae1dSRodney W. Grimes #ifndef TUBA_INCLUDE
1319df8bae1dSRodney W. Grimes }
1320df8bae1dSRodney W. Grimes 
1321df8bae1dSRodney W. Grimes void
1322df8bae1dSRodney W. Grimes tcp_dooptions(tp, cp, cnt, ti, ts_present, ts_val, ts_ecr)
1323df8bae1dSRodney W. Grimes 	struct tcpcb *tp;
1324df8bae1dSRodney W. Grimes 	u_char *cp;
1325df8bae1dSRodney W. Grimes 	int cnt;
1326df8bae1dSRodney W. Grimes 	struct tcpiphdr *ti;
1327df8bae1dSRodney W. Grimes 	int *ts_present;
1328df8bae1dSRodney W. Grimes 	u_long *ts_val, *ts_ecr;
1329df8bae1dSRodney W. Grimes {
1330df8bae1dSRodney W. Grimes 	u_short mss;
1331df8bae1dSRodney W. Grimes 	int opt, optlen;
1332df8bae1dSRodney W. Grimes 
1333df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1334df8bae1dSRodney W. Grimes 		opt = cp[0];
1335df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
1336df8bae1dSRodney W. Grimes 			break;
1337df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
1338df8bae1dSRodney W. Grimes 			optlen = 1;
1339df8bae1dSRodney W. Grimes 		else {
1340df8bae1dSRodney W. Grimes 			optlen = cp[1];
1341df8bae1dSRodney W. Grimes 			if (optlen <= 0)
1342df8bae1dSRodney W. Grimes 				break;
1343df8bae1dSRodney W. Grimes 		}
1344df8bae1dSRodney W. Grimes 		switch (opt) {
1345df8bae1dSRodney W. Grimes 
1346df8bae1dSRodney W. Grimes 		default:
1347df8bae1dSRodney W. Grimes 			continue;
1348df8bae1dSRodney W. Grimes 
1349df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
1350df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
1351df8bae1dSRodney W. Grimes 				continue;
1352df8bae1dSRodney W. Grimes 			if (!(ti->ti_flags & TH_SYN))
1353df8bae1dSRodney W. Grimes 				continue;
1354df8bae1dSRodney W. Grimes 			bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
1355df8bae1dSRodney W. Grimes 			NTOHS(mss);
1356df8bae1dSRodney W. Grimes 			(void) tcp_mss(tp, mss);	/* sets t_maxseg */
1357df8bae1dSRodney W. Grimes 			break;
1358df8bae1dSRodney W. Grimes 
1359df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
1360df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
1361df8bae1dSRodney W. Grimes 				continue;
1362df8bae1dSRodney W. Grimes 			if (!(ti->ti_flags & TH_SYN))
1363df8bae1dSRodney W. Grimes 				continue;
1364df8bae1dSRodney W. Grimes 			tp->t_flags |= TF_RCVD_SCALE;
1365df8bae1dSRodney W. Grimes 			tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
1366df8bae1dSRodney W. Grimes 			break;
1367df8bae1dSRodney W. Grimes 
1368df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
1369df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
1370df8bae1dSRodney W. Grimes 				continue;
1371df8bae1dSRodney W. Grimes 			*ts_present = 1;
1372df8bae1dSRodney W. Grimes 			bcopy((char *)cp + 2, (char *) ts_val, sizeof(*ts_val));
1373df8bae1dSRodney W. Grimes 			NTOHL(*ts_val);
1374df8bae1dSRodney W. Grimes 			bcopy((char *)cp + 6, (char *) ts_ecr, sizeof(*ts_ecr));
1375df8bae1dSRodney W. Grimes 			NTOHL(*ts_ecr);
1376df8bae1dSRodney W. Grimes 
1377df8bae1dSRodney W. Grimes 			/*
1378df8bae1dSRodney W. Grimes 			 * A timestamp received in a SYN makes
1379df8bae1dSRodney W. Grimes 			 * it ok to send timestamp requests and replies.
1380df8bae1dSRodney W. Grimes 			 */
1381df8bae1dSRodney W. Grimes 			if (ti->ti_flags & TH_SYN) {
1382df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_RCVD_TSTMP;
1383df8bae1dSRodney W. Grimes 				tp->ts_recent = *ts_val;
1384df8bae1dSRodney W. Grimes 				tp->ts_recent_age = tcp_now;
1385df8bae1dSRodney W. Grimes 			}
1386df8bae1dSRodney W. Grimes 			break;
1387df8bae1dSRodney W. Grimes 		}
1388df8bae1dSRodney W. Grimes 	}
1389df8bae1dSRodney W. Grimes }
1390df8bae1dSRodney W. Grimes 
1391df8bae1dSRodney W. Grimes /*
1392df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
1393df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
1394df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
1395df8bae1dSRodney W. Grimes  * sequencing purposes.
1396df8bae1dSRodney W. Grimes  */
1397df8bae1dSRodney W. Grimes void
1398df8bae1dSRodney W. Grimes tcp_pulloutofband(so, ti, m)
1399df8bae1dSRodney W. Grimes 	struct socket *so;
1400df8bae1dSRodney W. Grimes 	struct tcpiphdr *ti;
1401df8bae1dSRodney W. Grimes 	register struct mbuf *m;
1402df8bae1dSRodney W. Grimes {
1403df8bae1dSRodney W. Grimes 	int cnt = ti->ti_urp - 1;
1404df8bae1dSRodney W. Grimes 
1405df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
1406df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
1407df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
1408df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
1409df8bae1dSRodney W. Grimes 
1410df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
1411df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
1412df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
1413df8bae1dSRodney W. Grimes 			m->m_len--;
1414df8bae1dSRodney W. Grimes 			return;
1415df8bae1dSRodney W. Grimes 		}
1416df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
1417df8bae1dSRodney W. Grimes 		m = m->m_next;
1418df8bae1dSRodney W. Grimes 		if (m == 0)
1419df8bae1dSRodney W. Grimes 			break;
1420df8bae1dSRodney W. Grimes 	}
1421df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
1422df8bae1dSRodney W. Grimes }
1423df8bae1dSRodney W. Grimes 
1424df8bae1dSRodney W. Grimes /*
1425df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
1426df8bae1dSRodney W. Grimes  * and update averages and current timeout.
1427df8bae1dSRodney W. Grimes  */
1428df8bae1dSRodney W. Grimes void
1429df8bae1dSRodney W. Grimes tcp_xmit_timer(tp, rtt)
1430df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1431df8bae1dSRodney W. Grimes 	short rtt;
1432df8bae1dSRodney W. Grimes {
1433df8bae1dSRodney W. Grimes 	register short delta;
1434df8bae1dSRodney W. Grimes 
1435df8bae1dSRodney W. Grimes 	tcpstat.tcps_rttupdated++;
1436df8bae1dSRodney W. Grimes 	if (tp->t_srtt != 0) {
1437df8bae1dSRodney W. Grimes 		/*
1438df8bae1dSRodney W. Grimes 		 * srtt is stored as fixed point with 3 bits after the
1439df8bae1dSRodney W. Grimes 		 * binary point (i.e., scaled by 8).  The following magic
1440df8bae1dSRodney W. Grimes 		 * is equivalent to the smoothing algorithm in rfc793 with
1441df8bae1dSRodney W. Grimes 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
1442df8bae1dSRodney W. Grimes 		 * point).  Adjust rtt to origin 0.
1443df8bae1dSRodney W. Grimes 		 */
1444df8bae1dSRodney W. Grimes 		delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT);
1445df8bae1dSRodney W. Grimes 		if ((tp->t_srtt += delta) <= 0)
1446df8bae1dSRodney W. Grimes 			tp->t_srtt = 1;
1447df8bae1dSRodney W. Grimes 		/*
1448df8bae1dSRodney W. Grimes 		 * We accumulate a smoothed rtt variance (actually, a
1449df8bae1dSRodney W. Grimes 		 * smoothed mean difference), then set the retransmit
1450df8bae1dSRodney W. Grimes 		 * timer to smoothed rtt + 4 times the smoothed variance.
1451df8bae1dSRodney W. Grimes 		 * rttvar is stored as fixed point with 2 bits after the
1452df8bae1dSRodney W. Grimes 		 * binary point (scaled by 4).  The following is
1453df8bae1dSRodney W. Grimes 		 * equivalent to rfc793 smoothing with an alpha of .75
1454df8bae1dSRodney W. Grimes 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
1455df8bae1dSRodney W. Grimes 		 * rfc793's wired-in beta.
1456df8bae1dSRodney W. Grimes 		 */
1457df8bae1dSRodney W. Grimes 		if (delta < 0)
1458df8bae1dSRodney W. Grimes 			delta = -delta;
1459df8bae1dSRodney W. Grimes 		delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
1460df8bae1dSRodney W. Grimes 		if ((tp->t_rttvar += delta) <= 0)
1461df8bae1dSRodney W. Grimes 			tp->t_rttvar = 1;
1462df8bae1dSRodney W. Grimes 	} else {
1463df8bae1dSRodney W. Grimes 		/*
1464df8bae1dSRodney W. Grimes 		 * No rtt measurement yet - use the unsmoothed rtt.
1465df8bae1dSRodney W. Grimes 		 * Set the variance to half the rtt (so our first
1466df8bae1dSRodney W. Grimes 		 * retransmit happens at 3*rtt).
1467df8bae1dSRodney W. Grimes 		 */
1468df8bae1dSRodney W. Grimes 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
1469df8bae1dSRodney W. Grimes 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
1470df8bae1dSRodney W. Grimes 	}
1471df8bae1dSRodney W. Grimes 	tp->t_rtt = 0;
1472df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
1473df8bae1dSRodney W. Grimes 
1474df8bae1dSRodney W. Grimes 	/*
1475df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
1476df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
1477df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
1478df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
1479df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
1480df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
1481df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
1482df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
1483df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
1484df8bae1dSRodney W. Grimes 	 */
1485df8bae1dSRodney W. Grimes 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
1486df8bae1dSRodney W. Grimes 	    tp->t_rttmin, TCPTV_REXMTMAX);
1487df8bae1dSRodney W. Grimes 
1488df8bae1dSRodney W. Grimes 	/*
1489df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
1490df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
1491df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
1492df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
1493df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
1494df8bae1dSRodney W. Grimes 	 */
1495df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
1496df8bae1dSRodney W. Grimes }
1497df8bae1dSRodney W. Grimes 
1498df8bae1dSRodney W. Grimes /*
1499df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
1500df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
1501df8bae1dSRodney W. Grimes  * If none, use an mss that can be handled on the outgoing
1502df8bae1dSRodney W. Grimes  * interface without forcing IP to fragment; if bigger than
1503df8bae1dSRodney W. Grimes  * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
1504df8bae1dSRodney W. Grimes  * to utilize large mbufs.  If no route is found, route has no mtu,
1505df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
1506df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
1507df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
1508df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
1509df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
1510df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
1511df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
1512df8bae1dSRodney W. Grimes  */
1513df8bae1dSRodney W. Grimes int
1514df8bae1dSRodney W. Grimes tcp_mss(tp, offer)
1515df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1516df8bae1dSRodney W. Grimes 	u_int offer;
1517df8bae1dSRodney W. Grimes {
1518df8bae1dSRodney W. Grimes 	struct route *ro;
1519df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
1520df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
1521df8bae1dSRodney W. Grimes 	register int rtt, mss;
1522df8bae1dSRodney W. Grimes 	u_long bufsize;
1523df8bae1dSRodney W. Grimes 	struct inpcb *inp;
1524df8bae1dSRodney W. Grimes 	struct socket *so;
1525df8bae1dSRodney W. Grimes 	extern int tcp_mssdflt;
1526df8bae1dSRodney W. Grimes 
1527df8bae1dSRodney W. Grimes 	inp = tp->t_inpcb;
1528df8bae1dSRodney W. Grimes 	ro = &inp->inp_route;
1529df8bae1dSRodney W. Grimes 
1530df8bae1dSRodney W. Grimes 	if ((rt = ro->ro_rt) == (struct rtentry *)0) {
1531df8bae1dSRodney W. Grimes 		/* No route yet, so try to acquire one */
1532df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
1533df8bae1dSRodney W. Grimes 			ro->ro_dst.sa_family = AF_INET;
1534df8bae1dSRodney W. Grimes 			ro->ro_dst.sa_len = sizeof(ro->ro_dst);
1535df8bae1dSRodney W. Grimes 			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
1536df8bae1dSRodney W. Grimes 				inp->inp_faddr;
1537df8bae1dSRodney W. Grimes 			rtalloc(ro);
1538df8bae1dSRodney W. Grimes 		}
1539df8bae1dSRodney W. Grimes 		if ((rt = ro->ro_rt) == (struct rtentry *)0)
1540df8bae1dSRodney W. Grimes 			return (tcp_mssdflt);
1541df8bae1dSRodney W. Grimes 	}
1542df8bae1dSRodney W. Grimes 	ifp = rt->rt_ifp;
1543df8bae1dSRodney W. Grimes 	so = inp->inp_socket;
1544df8bae1dSRodney W. Grimes 
1545df8bae1dSRodney W. Grimes #ifdef RTV_MTU	/* if route characteristics exist ... */
1546df8bae1dSRodney W. Grimes 	/*
1547df8bae1dSRodney W. Grimes 	 * While we're here, check if there's an initial rtt
1548df8bae1dSRodney W. Grimes 	 * or rttvar.  Convert from the route-table units
1549df8bae1dSRodney W. Grimes 	 * to scaled multiples of the slow timeout timer.
1550df8bae1dSRodney W. Grimes 	 */
1551df8bae1dSRodney W. Grimes 	if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
1552df8bae1dSRodney W. Grimes 		/*
1553df8bae1dSRodney W. Grimes 		 * XXX the lock bit for MTU indicates that the value
1554df8bae1dSRodney W. Grimes 		 * is also a minimum value; this is subject to time.
1555df8bae1dSRodney W. Grimes 		 */
1556df8bae1dSRodney W. Grimes 		if (rt->rt_rmx.rmx_locks & RTV_RTT)
1557df8bae1dSRodney W. Grimes 			tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ);
1558df8bae1dSRodney W. Grimes 		tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
1559df8bae1dSRodney W. Grimes 		if (rt->rt_rmx.rmx_rttvar)
1560df8bae1dSRodney W. Grimes 			tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
1561df8bae1dSRodney W. Grimes 			    (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
1562df8bae1dSRodney W. Grimes 		else
1563df8bae1dSRodney W. Grimes 			/* default variation is +- 1 rtt */
1564df8bae1dSRodney W. Grimes 			tp->t_rttvar =
1565df8bae1dSRodney W. Grimes 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
1566df8bae1dSRodney W. Grimes 		TCPT_RANGESET(tp->t_rxtcur,
1567df8bae1dSRodney W. Grimes 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
1568df8bae1dSRodney W. Grimes 		    tp->t_rttmin, TCPTV_REXMTMAX);
1569df8bae1dSRodney W. Grimes 	}
1570df8bae1dSRodney W. Grimes 	/*
1571df8bae1dSRodney W. Grimes 	 * if there's an mtu associated with the route, use it
1572df8bae1dSRodney W. Grimes 	 */
1573df8bae1dSRodney W. Grimes 	if (rt->rt_rmx.rmx_mtu)
1574df8bae1dSRodney W. Grimes 		mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
1575df8bae1dSRodney W. Grimes 	else
1576df8bae1dSRodney W. Grimes #endif /* RTV_MTU */
1577df8bae1dSRodney W. Grimes 	{
1578df8bae1dSRodney W. Grimes 		mss = ifp->if_mtu - sizeof(struct tcpiphdr);
1579df8bae1dSRodney W. Grimes #if	(MCLBYTES & (MCLBYTES - 1)) == 0
1580df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
1581df8bae1dSRodney W. Grimes 			mss &= ~(MCLBYTES-1);
1582df8bae1dSRodney W. Grimes #else
1583df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
1584df8bae1dSRodney W. Grimes 			mss = mss / MCLBYTES * MCLBYTES;
1585df8bae1dSRodney W. Grimes #endif
1586df8bae1dSRodney W. Grimes 		if (!in_localaddr(inp->inp_faddr))
1587df8bae1dSRodney W. Grimes 			mss = min(mss, tcp_mssdflt);
1588df8bae1dSRodney W. Grimes 	}
1589df8bae1dSRodney W. Grimes 	/*
1590df8bae1dSRodney W. Grimes 	 * The current mss, t_maxseg, is initialized to the default value.
1591df8bae1dSRodney W. Grimes 	 * If we compute a smaller value, reduce the current mss.
1592df8bae1dSRodney W. Grimes 	 * If we compute a larger value, return it for use in sending
1593df8bae1dSRodney W. Grimes 	 * a max seg size option, but don't store it for use
1594df8bae1dSRodney W. Grimes 	 * unless we received an offer at least that large from peer.
1595df8bae1dSRodney W. Grimes 	 * However, do not accept offers under 32 bytes.
1596df8bae1dSRodney W. Grimes 	 */
1597df8bae1dSRodney W. Grimes 	if (offer)
1598df8bae1dSRodney W. Grimes 		mss = min(mss, offer);
1599df8bae1dSRodney W. Grimes 	mss = max(mss, 32);		/* sanity */
1600df8bae1dSRodney W. Grimes 	if (mss < tp->t_maxseg || offer != 0) {
1601df8bae1dSRodney W. Grimes 		/*
1602df8bae1dSRodney W. Grimes 		 * If there's a pipesize, change the socket buffer
1603df8bae1dSRodney W. Grimes 		 * to that size.  Make the socket buffers an integral
1604df8bae1dSRodney W. Grimes 		 * number of mss units; if the mss is larger than
1605df8bae1dSRodney W. Grimes 		 * the socket buffer, decrease the mss.
1606df8bae1dSRodney W. Grimes 		 */
1607df8bae1dSRodney W. Grimes #ifdef RTV_SPIPE
1608df8bae1dSRodney W. Grimes 		if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
1609df8bae1dSRodney W. Grimes #endif
1610df8bae1dSRodney W. Grimes 			bufsize = so->so_snd.sb_hiwat;
1611df8bae1dSRodney W. Grimes 		if (bufsize < mss)
1612df8bae1dSRodney W. Grimes 			mss = bufsize;
1613df8bae1dSRodney W. Grimes 		else {
1614df8bae1dSRodney W. Grimes 			bufsize = roundup(bufsize, mss);
1615df8bae1dSRodney W. Grimes 			if (bufsize > sb_max)
1616df8bae1dSRodney W. Grimes 				bufsize = sb_max;
1617df8bae1dSRodney W. Grimes 			(void)sbreserve(&so->so_snd, bufsize);
1618df8bae1dSRodney W. Grimes 		}
1619df8bae1dSRodney W. Grimes 		tp->t_maxseg = mss;
1620df8bae1dSRodney W. Grimes 
1621df8bae1dSRodney W. Grimes #ifdef RTV_RPIPE
1622df8bae1dSRodney W. Grimes 		if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
1623df8bae1dSRodney W. Grimes #endif
1624df8bae1dSRodney W. Grimes 			bufsize = so->so_rcv.sb_hiwat;
1625df8bae1dSRodney W. Grimes 		if (bufsize > mss) {
1626df8bae1dSRodney W. Grimes 			bufsize = roundup(bufsize, mss);
1627df8bae1dSRodney W. Grimes 			if (bufsize > sb_max)
1628df8bae1dSRodney W. Grimes 				bufsize = sb_max;
1629df8bae1dSRodney W. Grimes 			(void)sbreserve(&so->so_rcv, bufsize);
1630df8bae1dSRodney W. Grimes 		}
1631df8bae1dSRodney W. Grimes 	}
1632df8bae1dSRodney W. Grimes 	tp->snd_cwnd = mss;
1633df8bae1dSRodney W. Grimes 
1634df8bae1dSRodney W. Grimes #ifdef RTV_SSTHRESH
1635df8bae1dSRodney W. Grimes 	if (rt->rt_rmx.rmx_ssthresh) {
1636df8bae1dSRodney W. Grimes 		/*
1637df8bae1dSRodney W. Grimes 		 * There's some sort of gateway or interface
1638df8bae1dSRodney W. Grimes 		 * buffer limit on the path.  Use this to set
1639df8bae1dSRodney W. Grimes 		 * the slow start threshhold, but set the
1640df8bae1dSRodney W. Grimes 		 * threshold to no less than 2*mss.
1641df8bae1dSRodney W. Grimes 		 */
1642df8bae1dSRodney W. Grimes 		tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
1643df8bae1dSRodney W. Grimes 	}
1644df8bae1dSRodney W. Grimes #endif /* RTV_MTU */
1645df8bae1dSRodney W. Grimes 	return (mss);
1646df8bae1dSRodney W. Grimes }
1647df8bae1dSRodney W. Grimes #endif /* TUBA_INCLUDE */
1648