xref: /csrg-svn/sys/netinet/tcp_input.c (revision 68512)
123190Smckusick /*
266740Sbostic  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994
363218Sbostic  *	The Regents of the University of California.  All rights reserved.
423190Smckusick  *
544485Sbostic  * %sccs.include.redist.c%
632787Sbostic  *
7*68512Sdab  *	@(#)tcp_input.c	8.8 (Berkeley) 03/10/95
823190Smckusick  */
94601Swnj 
1057947Ssklower #ifndef TUBA_INCLUDE
1156531Sbostic #include <sys/param.h>
1256531Sbostic #include <sys/systm.h>
1356531Sbostic #include <sys/malloc.h>
1456531Sbostic #include <sys/mbuf.h>
1556531Sbostic #include <sys/protosw.h>
1656531Sbostic #include <sys/socket.h>
1756531Sbostic #include <sys/socketvar.h>
1856531Sbostic #include <sys/errno.h>
1910894Ssam 
2056531Sbostic #include <net/if.h>
2156531Sbostic #include <net/route.h>
2210894Ssam 
2356531Sbostic #include <netinet/in.h>
2456531Sbostic #include <netinet/in_systm.h>
2556531Sbostic #include <netinet/ip.h>
2656531Sbostic #include <netinet/in_pcb.h>
2756531Sbostic #include <netinet/ip_var.h>
2856531Sbostic #include <netinet/tcp.h>
2956531Sbostic #include <netinet/tcp_fsm.h>
3056531Sbostic #include <netinet/tcp_seq.h>
3156531Sbostic #include <netinet/tcp_timer.h>
3256531Sbostic #include <netinet/tcp_var.h>
3356531Sbostic #include <netinet/tcpip.h>
3456531Sbostic #include <netinet/tcp_debug.h>
354601Swnj 
3632098Skarels int	tcprexmtthresh = 3;
375267Sroot struct	tcpiphdr tcp_saveti;
3844375Skarels struct	inpcb *tcp_last_inpcb = &tcb;
394601Swnj 
4057947Ssklower extern u_long sb_max;
4157947Ssklower 
4257947Ssklower #endif /* TUBA_INCLUDE */
4357433Sandrew #define TCP_PAWS_IDLE	(24 * 24 * 60 * 60 * PR_SLOWHZ)
4457433Sandrew 
4557433Sandrew /* for modulo comparisons of timestamps */
4657433Sandrew #define TSTMP_LT(a,b)	((int)((a)-(b)) < 0)
4757433Sandrew #define TSTMP_GEQ(a,b)	((int)((a)-(b)) >= 0)
4857433Sandrew 
4924816Skarels 
505065Swnj /*
5124816Skarels  * Insert segment ti into reassembly queue of tcp with
5224816Skarels  * control block tp.  Return TH_FIN if reassembly now includes
5324816Skarels  * a segment with FIN.  The macro form does the common case inline
5424816Skarels  * (segment is the next to be received on an established connection,
5524816Skarels  * and the queue is empty), avoiding linkage into and removal
5624816Skarels  * from the queue and repetition of various conversions.
5734278Skarels  * Set DELACK for segments received in order, but ack immediately
5834278Skarels  * when segments are out of order (so fast retransmit can work).
5924816Skarels  */
6024816Skarels #define	TCP_REASS(tp, ti, m, so, flags) { \
6124816Skarels 	if ((ti)->ti_seq == (tp)->rcv_nxt && \
6224816Skarels 	    (tp)->seg_next == (struct tcpiphdr *)(tp) && \
6324816Skarels 	    (tp)->t_state == TCPS_ESTABLISHED) { \
6434278Skarels 		tp->t_flags |= TF_DELACK; \
6524816Skarels 		(tp)->rcv_nxt += (ti)->ti_len; \
6624816Skarels 		flags = (ti)->ti_flags & TH_FIN; \
6730525Skarels 		tcpstat.tcps_rcvpack++;\
6830525Skarels 		tcpstat.tcps_rcvbyte += (ti)->ti_len;\
6924816Skarels 		sbappend(&(so)->so_rcv, (m)); \
7024816Skarels 		sorwakeup(so); \
7134278Skarels 	} else { \
7244375Skarels 		(flags) = tcp_reass((tp), (ti), (m)); \
7334278Skarels 		tp->t_flags |= TF_ACKNOW; \
7434278Skarels 	} \
7524816Skarels }
7657947Ssklower #ifndef TUBA_INCLUDE
7724816Skarels 
7861335Sbostic int
7944375Skarels tcp_reass(tp, ti, m)
8024816Skarels 	register struct tcpcb *tp;
8124816Skarels 	register struct tcpiphdr *ti;
8244375Skarels 	struct mbuf *m;
8324816Skarels {
8424816Skarels 	register struct tcpiphdr *q;
8524816Skarels 	struct socket *so = tp->t_inpcb->inp_socket;
8624816Skarels 	int flags;
8724816Skarels 
8824816Skarels 	/*
8924816Skarels 	 * Call with ti==0 after become established to
9024816Skarels 	 * force pre-ESTABLISHED data up to user socket.
9124816Skarels 	 */
9224816Skarels 	if (ti == 0)
9324816Skarels 		goto present;
9424816Skarels 
9524816Skarels 	/*
9624816Skarels 	 * Find a segment which begins after this one does.
9724816Skarels 	 */
9824816Skarels 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
9924816Skarels 	    q = (struct tcpiphdr *)q->ti_next)
10024816Skarels 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
10124816Skarels 			break;
10224816Skarels 
10324816Skarels 	/*
10424816Skarels 	 * If there is a preceding segment, it may provide some of
10524816Skarels 	 * our data already.  If so, drop the data from the incoming
10624816Skarels 	 * segment.  If it provides all of our data, drop us.
10724816Skarels 	 */
10824816Skarels 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
10924816Skarels 		register int i;
11024816Skarels 		q = (struct tcpiphdr *)q->ti_prev;
11124816Skarels 		/* conversion to int (in i) handles seq wraparound */
11224816Skarels 		i = q->ti_seq + q->ti_len - ti->ti_seq;
11324816Skarels 		if (i > 0) {
11430525Skarels 			if (i >= ti->ti_len) {
11530525Skarels 				tcpstat.tcps_rcvduppack++;
11630525Skarels 				tcpstat.tcps_rcvdupbyte += ti->ti_len;
11744375Skarels 				m_freem(m);
11844375Skarels 				return (0);
11930525Skarels 			}
12044375Skarels 			m_adj(m, i);
12124816Skarels 			ti->ti_len -= i;
12224816Skarels 			ti->ti_seq += i;
12324816Skarels 		}
12424816Skarels 		q = (struct tcpiphdr *)(q->ti_next);
12524816Skarels 	}
12630525Skarels 	tcpstat.tcps_rcvoopack++;
12730525Skarels 	tcpstat.tcps_rcvoobyte += ti->ti_len;
12844375Skarels 	REASS_MBUF(ti) = m;		/* XXX */
12924816Skarels 
13024816Skarels 	/*
13124816Skarels 	 * While we overlap succeeding segments trim them or,
13224816Skarels 	 * if they are completely covered, dequeue them.
13324816Skarels 	 */
13424816Skarels 	while (q != (struct tcpiphdr *)tp) {
13524816Skarels 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
13624816Skarels 		if (i <= 0)
13724816Skarels 			break;
13824816Skarels 		if (i < q->ti_len) {
13924816Skarels 			q->ti_seq += i;
14024816Skarels 			q->ti_len -= i;
14144375Skarels 			m_adj(REASS_MBUF(q), i);
14224816Skarels 			break;
14324816Skarels 		}
14424816Skarels 		q = (struct tcpiphdr *)q->ti_next;
14544375Skarels 		m = REASS_MBUF((struct tcpiphdr *)q->ti_prev);
14624816Skarels 		remque(q->ti_prev);
14724816Skarels 		m_freem(m);
14824816Skarels 	}
14924816Skarels 
15024816Skarels 	/*
15124816Skarels 	 * Stick new segment in its place.
15224816Skarels 	 */
15324816Skarels 	insque(ti, q->ti_prev);
15424816Skarels 
15524816Skarels present:
15624816Skarels 	/*
15724816Skarels 	 * Present data to user, advancing rcv_nxt through
15824816Skarels 	 * completed sequence space.
15924816Skarels 	 */
16024816Skarels 	if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
16124816Skarels 		return (0);
16224816Skarels 	ti = tp->seg_next;
16324816Skarels 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
16424816Skarels 		return (0);
16524816Skarels 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
16624816Skarels 		return (0);
16724816Skarels 	do {
16824816Skarels 		tp->rcv_nxt += ti->ti_len;
16924816Skarels 		flags = ti->ti_flags & TH_FIN;
17024816Skarels 		remque(ti);
17144375Skarels 		m = REASS_MBUF(ti);
17224816Skarels 		ti = (struct tcpiphdr *)ti->ti_next;
17324816Skarels 		if (so->so_state & SS_CANTRCVMORE)
17424816Skarels 			m_freem(m);
17524816Skarels 		else
17624816Skarels 			sbappend(&so->so_rcv, m);
17724816Skarels 	} while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
17824816Skarels 	sorwakeup(so);
17924816Skarels 	return (flags);
18024816Skarels }
18124816Skarels 
18224816Skarels /*
1835065Swnj  * TCP input routine, follows pages 65-76 of the
1845065Swnj  * protocol specification dated September, 1981 very closely.
1855065Swnj  */
18661335Sbostic void
18737320Skarels tcp_input(m, iphlen)
18837320Skarels 	register struct mbuf *m;
18937320Skarels 	int iphlen;
1904601Swnj {
1914924Swnj 	register struct tcpiphdr *ti;
19244375Skarels 	register struct inpcb *inp;
19367588Smckusick 	u_char *optp = NULL;
19457433Sandrew 	int optlen;
1954924Swnj 	int len, tlen, off;
1965391Swnj 	register struct tcpcb *tp = 0;
1974924Swnj 	register int tiflags;
1984803Swnj 	struct socket *so;
19931721Skarels 	int todrop, acked, ourfinisacked, needoutput = 0;
2005267Sroot 	short ostate;
2016028Sroot 	struct in_addr laddr;
20210769Ssam 	int dropsocket = 0;
20330525Skarels 	int iss = 0;
20457433Sandrew 	u_long tiwin, ts_val, ts_ecr;
20557433Sandrew 	int ts_present = 0;
2064924Swnj 
20730525Skarels 	tcpstat.tcps_rcvtotal++;
2084924Swnj 	/*
2095244Sroot 	 * Get IP and TCP header together in first mbuf.
2105244Sroot 	 * Note: IP leaves IP header in first mbuf.
2114924Swnj 	 */
2125020Sroot 	ti = mtod(m, struct tcpiphdr *);
21337320Skarels 	if (iphlen > sizeof (struct ip))
21437320Skarels 		ip_stripoptions(m, (struct mbuf *)0);
21544375Skarels 	if (m->m_len < sizeof (struct tcpiphdr)) {
2165307Sroot 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
21730525Skarels 			tcpstat.tcps_rcvshort++;
2185307Sroot 			return;
2195085Swnj 		}
2205085Swnj 		ti = mtod(m, struct tcpiphdr *);
2215085Swnj 	}
2224601Swnj 
2234601Swnj 	/*
2245244Sroot 	 * Checksum extended TCP header and data.
2254601Swnj 	 */
2264924Swnj 	tlen = ((struct ip *)ti)->ip_len;
2274924Swnj 	len = sizeof (struct ip) + tlen;
22837320Skarels 	ti->ti_next = ti->ti_prev = 0;
22937320Skarels 	ti->ti_x1 = 0;
23037320Skarels 	ti->ti_len = (u_short)tlen;
23144375Skarels 	HTONS(ti->ti_len);
23237320Skarels 	if (ti->ti_sum = in_cksum(m, len)) {
23337320Skarels 		tcpstat.tcps_rcvbadsum++;
23437320Skarels 		goto drop;
2354601Swnj 	}
23657947Ssklower #endif /* TUBA_INCLUDE */
2374601Swnj 
2384601Swnj 	/*
2395244Sroot 	 * Check that TCP offset makes sense,
24044375Skarels 	 * pull out TCP options and adjust length.		XXX
2414601Swnj 	 */
2424924Swnj 	off = ti->ti_off << 2;
2435231Swnj 	if (off < sizeof (struct tcphdr) || off > tlen) {
24430525Skarels 		tcpstat.tcps_rcvbadoff++;
2455085Swnj 		goto drop;
2464924Swnj 	}
2476211Swnj 	tlen -= off;
2486211Swnj 	ti->ti_len = tlen;
2495440Swnj 	if (off > sizeof (struct tcphdr)) {
25024816Skarels 		if (m->m_len < sizeof(struct ip) + off) {
25124816Skarels 			if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
25230525Skarels 				tcpstat.tcps_rcvshort++;
25324816Skarels 				return;
25424816Skarels 			}
25524816Skarels 			ti = mtod(m, struct tcpiphdr *);
2565440Swnj 		}
25757433Sandrew 		optlen = off - sizeof (struct tcphdr);
25867588Smckusick 		optp = mtod(m, u_char *) + sizeof (struct tcpiphdr);
25957433Sandrew 		/*
26057433Sandrew 		 * Do quick retrieval of timestamp options ("options
26157433Sandrew 		 * prediction?").  If timestamp is the only option and it's
26257433Sandrew 		 * formatted as recommended in RFC 1323 appendix A, we
26357433Sandrew 		 * quickly get the values now and not bother calling
26457433Sandrew 		 * tcp_dooptions(), etc.
26557433Sandrew 		 */
26657433Sandrew 		if ((optlen == TCPOLEN_TSTAMP_APPA ||
26757433Sandrew 		     (optlen > TCPOLEN_TSTAMP_APPA &&
26857433Sandrew 			optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
26957433Sandrew 		     *(u_long *)optp == htonl(TCPOPT_TSTAMP_HDR) &&
27057433Sandrew 		     (ti->ti_flags & TH_SYN) == 0) {
27157433Sandrew 			ts_present = 1;
27257433Sandrew 			ts_val = ntohl(*(u_long *)(optp + 4));
27357433Sandrew 			ts_ecr = ntohl(*(u_long *)(optp + 8));
27457433Sandrew 			optp = NULL;	/* we've parsed the options */
2755440Swnj 		}
2765440Swnj 	}
2775065Swnj 	tiflags = ti->ti_flags;
2784924Swnj 
2796093Sroot 	/*
2805244Sroot 	 * Convert TCP protocol specific fields to host format.
2815085Swnj 	 */
28244375Skarels 	NTOHL(ti->ti_seq);
28344375Skarels 	NTOHL(ti->ti_ack);
28444375Skarels 	NTOHS(ti->ti_win);
28544375Skarels 	NTOHS(ti->ti_urp);
2865085Swnj 
2875085Swnj 	/*
2888271Sroot 	 * Locate pcb for segment.
2894924Swnj 	 */
29030525Skarels findpcb:
29144375Skarels 	inp = tcp_last_inpcb;
29244375Skarels 	if (inp->inp_lport != ti->ti_dport ||
29344375Skarels 	    inp->inp_fport != ti->ti_sport ||
29444375Skarels 	    inp->inp_faddr.s_addr != ti->ti_src.s_addr ||
29544375Skarels 	    inp->inp_laddr.s_addr != ti->ti_dst.s_addr) {
29644375Skarels 		inp = in_pcblookup(&tcb, ti->ti_src, ti->ti_sport,
29744375Skarels 		    ti->ti_dst, ti->ti_dport, INPLOOKUP_WILDCARD);
29844375Skarels 		if (inp)
29944375Skarels 			tcp_last_inpcb = inp;
30066740Sbostic 		++tcpstat.tcps_pcbcachemiss;
30144375Skarels 	}
3025065Swnj 
3035065Swnj 	/*
3045065Swnj 	 * If the state is CLOSED (i.e., TCB does not exist) then
3055244Sroot 	 * all data in the incoming segment is discarded.
30632098Skarels 	 * If the TCB exists but is in CLOSED state, it is embryonic,
30732098Skarels 	 * but should either do a listen or a connect soon.
3085065Swnj 	 */
3095300Sroot 	if (inp == 0)
3105085Swnj 		goto dropwithreset;
3115065Swnj 	tp = intotcpcb(inp);
3125300Sroot 	if (tp == 0)
3135085Swnj 		goto dropwithreset;
31432098Skarels 	if (tp->t_state == TCPS_CLOSED)
31532098Skarels 		goto drop;
31657433Sandrew 
31757433Sandrew 	/* Unscale the window into a 32-bit value. */
31857433Sandrew 	if ((tiflags & TH_SYN) == 0)
31957433Sandrew 		tiwin = ti->ti_win << tp->snd_scale;
32057433Sandrew 	else
32157433Sandrew 		tiwin = ti->ti_win;
32257433Sandrew 
3235109Swnj 	so = inp->inp_socket;
32444375Skarels 	if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
32544375Skarels 		if (so->so_options & SO_DEBUG) {
32644375Skarels 			ostate = tp->t_state;
32744375Skarels 			tcp_saveti = *ti;
32844375Skarels 		}
32944375Skarels 		if (so->so_options & SO_ACCEPTCONN) {
330*68512Sdab 			if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
331*68512Sdab 				/*
332*68512Sdab 				 * Note: dropwithreset makes sure we don't
333*68512Sdab 				 * send a reset in response to a RST.
334*68512Sdab 				 */
335*68512Sdab 				if (tiflags & (TH_ACK|TH_RST))
336*68512Sdab 					goto dropwithreset;
337*68512Sdab 				goto drop;
338*68512Sdab 			}
33944375Skarels 			so = sonewconn(so, 0);
34044375Skarels 			if (so == 0)
34144375Skarels 				goto drop;
34244375Skarels 			/*
34344375Skarels 			 * This is ugly, but ....
34444375Skarels 			 *
34544375Skarels 			 * Mark socket as temporary until we're
34644375Skarels 			 * committed to keeping it.  The code at
34744375Skarels 			 * ``drop'' and ``dropwithreset'' check the
34844375Skarels 			 * flag dropsocket to see if the temporary
34944375Skarels 			 * socket created here should be discarded.
35044375Skarels 			 * We mark the socket as discardable until
35144375Skarels 			 * we're committed to it below in TCPS_LISTEN.
35244375Skarels 			 */
35344375Skarels 			dropsocket++;
35444375Skarels 			inp = (struct inpcb *)so->so_pcb;
35544375Skarels 			inp->inp_laddr = ti->ti_dst;
35644375Skarels 			inp->inp_lport = ti->ti_dport;
35744375Skarels #if BSD>=43
35844375Skarels 			inp->inp_options = ip_srcroute();
35944375Skarels #endif
36044375Skarels 			tp = intotcpcb(inp);
36144375Skarels 			tp->t_state = TCPS_LISTEN;
36257433Sandrew 
36357433Sandrew 			/* Compute proper scaling value from buffer space
36457433Sandrew 			 */
36557433Sandrew 			while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
36657433Sandrew 			   TCP_MAXWIN << tp->request_r_scale < so->so_rcv.sb_hiwat)
36757433Sandrew 				tp->request_r_scale++;
36844375Skarels 		}
3695267Sroot 	}
3704601Swnj 
3714601Swnj 	/*
3725162Swnj 	 * Segment received on connection.
3735162Swnj 	 * Reset idle time and keep-alive timer.
3745162Swnj 	 */
3755162Swnj 	tp->t_idle = 0;
37633745Skarels 	tp->t_timer[TCPT_KEEP] = tcp_keepidle;
3775162Swnj 
3785162Swnj 	/*
37917272Skarels 	 * Process options if not in LISTEN state,
38017272Skarels 	 * else do it below (after getting remote address).
3815440Swnj 	 */
38257433Sandrew 	if (optp && tp->t_state != TCPS_LISTEN)
38357433Sandrew 		tcp_dooptions(tp, optp, optlen, ti,
38457433Sandrew 			&ts_present, &ts_val, &ts_ecr);
38557433Sandrew 
38644375Skarels 	/*
38744375Skarels 	 * Header prediction: check for the two common cases
38844375Skarels 	 * of a uni-directional data xfer.  If the packet has
38944375Skarels 	 * no control flags, is in-sequence, the window didn't
39044375Skarels 	 * change and we're not retransmitting, it's a
39144375Skarels 	 * candidate.  If the length is zero and the ack moved
39244375Skarels 	 * forward, we're the sender side of the xfer.  Just
39344375Skarels 	 * free the data acked & wake any higher level process
39444375Skarels 	 * that was blocked waiting for space.  If the length
39544375Skarels 	 * is non-zero and the ack didn't move, we're the
39644375Skarels 	 * receiver side.  If we're getting packets in-order
39744375Skarels 	 * (the reassembly queue is empty), add the data to
39844375Skarels 	 * the socket buffer and note that we need a delayed ack.
39944375Skarels 	 */
40044375Skarels 	if (tp->t_state == TCPS_ESTABLISHED &&
40144375Skarels 	    (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
40257433Sandrew 	    (!ts_present || TSTMP_GEQ(ts_val, tp->ts_recent)) &&
40344375Skarels 	    ti->ti_seq == tp->rcv_nxt &&
40457433Sandrew 	    tiwin && tiwin == tp->snd_wnd &&
40544375Skarels 	    tp->snd_nxt == tp->snd_max) {
40657433Sandrew 
40757433Sandrew 		/*
40857433Sandrew 		 * If last ACK falls within this segment's sequence numbers,
40957433Sandrew 		 *  record the timestamp.
41057433Sandrew 		 */
41157433Sandrew 		if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
41257433Sandrew 		   SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len)) {
41357433Sandrew 			tp->ts_recent_age = tcp_now;
41457433Sandrew 			tp->ts_recent = ts_val;
41557433Sandrew 		}
41657433Sandrew 
41744375Skarels 		if (ti->ti_len == 0) {
41844375Skarels 			if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
41944375Skarels 			    SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
42044375Skarels 			    tp->snd_cwnd >= tp->snd_wnd) {
42144375Skarels 				/*
42244375Skarels 				 * this is a pure ack for outstanding data.
42344375Skarels 				 */
42466740Sbostic 				++tcpstat.tcps_predack;
42557433Sandrew 				if (ts_present)
42657433Sandrew 					tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
42757433Sandrew 				else if (tp->t_rtt &&
42857433Sandrew 					    SEQ_GT(ti->ti_ack, tp->t_rtseq))
42957433Sandrew 					tcp_xmit_timer(tp, tp->t_rtt);
43044375Skarels 				acked = ti->ti_ack - tp->snd_una;
43144375Skarels 				tcpstat.tcps_rcvackpack++;
43244375Skarels 				tcpstat.tcps_rcvackbyte += acked;
43344375Skarels 				sbdrop(&so->so_snd, acked);
43444375Skarels 				tp->snd_una = ti->ti_ack;
43544375Skarels 				m_freem(m);
4365440Swnj 
43744375Skarels 				/*
43844375Skarels 				 * If all outstanding data are acked, stop
43944375Skarels 				 * retransmit timer, otherwise restart timer
44044375Skarels 				 * using current (possibly backed-off) value.
44144375Skarels 				 * If process is waiting for space,
44244375Skarels 				 * wakeup/selwakeup/signal.  If data
44344375Skarels 				 * are ready to send, let tcp_output
44444375Skarels 				 * decide between more output or persist.
44544375Skarels 				 */
44644375Skarels 				if (tp->snd_una == tp->snd_max)
44744375Skarels 					tp->t_timer[TCPT_REXMT] = 0;
44844375Skarels 				else if (tp->t_timer[TCPT_PERSIST] == 0)
44944375Skarels 					tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
45044375Skarels 
45144375Skarels 				if (so->so_snd.sb_flags & SB_NOTIFY)
45244375Skarels 					sowwakeup(so);
45344375Skarels 				if (so->so_snd.sb_cc)
45444375Skarels 					(void) tcp_output(tp);
45544375Skarels 				return;
45644375Skarels 			}
45744375Skarels 		} else if (ti->ti_ack == tp->snd_una &&
45844375Skarels 		    tp->seg_next == (struct tcpiphdr *)tp &&
45944375Skarels 		    ti->ti_len <= sbspace(&so->so_rcv)) {
46044375Skarels 			/*
46144375Skarels 			 * this is a pure, in-sequence data packet
46244375Skarels 			 * with nothing on the reassembly queue and
46344375Skarels 			 * we have enough buffer space to take it.
46444375Skarels 			 */
46566740Sbostic 			++tcpstat.tcps_preddat;
46644375Skarels 			tp->rcv_nxt += ti->ti_len;
46744375Skarels 			tcpstat.tcps_rcvpack++;
46844375Skarels 			tcpstat.tcps_rcvbyte += ti->ti_len;
46944375Skarels 			/*
47057433Sandrew 			 * Drop TCP, IP headers and TCP options then add data
47157433Sandrew 			 * to socket buffer.
47244375Skarels 			 */
47357433Sandrew 			m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
47457433Sandrew 			m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
47544375Skarels 			sbappend(&so->so_rcv, m);
47644375Skarels 			sorwakeup(so);
47744375Skarels 			tp->t_flags |= TF_DELACK;
47844375Skarels 			return;
47944375Skarels 		}
48044375Skarels 	}
48144375Skarels 
4825440Swnj 	/*
48357433Sandrew 	 * Drop TCP, IP headers and TCP options.
48444375Skarels 	 */
48557433Sandrew 	m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
48657433Sandrew 	m->m_len  -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
48744375Skarels 
48844375Skarels 	/*
4895085Swnj 	 * Calculate amount of space in receive window,
4905085Swnj 	 * and then do TCP input processing.
49124816Skarels 	 * Receive window is amount of space in rcv queue,
49224816Skarels 	 * but not less than advertised window.
4934601Swnj 	 */
49425939Skarels 	{ int win;
4954601Swnj 
49625939Skarels 	win = sbspace(&so->so_rcv);
49725939Skarels 	if (win < 0)
49825939Skarels 		win = 0;
49937320Skarels 	tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt));
50025939Skarels 	}
50125939Skarels 
5024601Swnj 	switch (tp->t_state) {
5034601Swnj 
5045065Swnj 	/*
5055065Swnj 	 * If the state is LISTEN then ignore segment if it contains an RST.
5065065Swnj 	 * If the segment contains an ACK then it is bad and send a RST.
5075065Swnj 	 * If it does not contain a SYN then it is not interesting; drop it.
50825197Skarels 	 * Don't bother responding if the destination was a broadcast.
5095085Swnj 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
5105065Swnj 	 * tp->iss, and send a segment:
5115085Swnj 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
5125065Swnj 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
5135065Swnj 	 * Fill in remote peer address fields if not previously specified.
5145065Swnj 	 * Enter SYN_RECEIVED state, and process any other fields of this
5155244Sroot 	 * segment in this state.
5165065Swnj 	 */
5178271Sroot 	case TCPS_LISTEN: {
51810145Ssam 		struct mbuf *am;
5198271Sroot 		register struct sockaddr_in *sin;
5208271Sroot 
5215065Swnj 		if (tiflags & TH_RST)
5225065Swnj 			goto drop;
5235300Sroot 		if (tiflags & TH_ACK)
5245085Swnj 			goto dropwithreset;
5255300Sroot 		if ((tiflags & TH_SYN) == 0)
5265065Swnj 			goto drop;
52758998Ssklower 		/*
52858998Ssklower 		 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
52958998Ssklower 		 * in_broadcast() should never return true on a received
53058998Ssklower 		 * packet with M_BCAST not set.
53158998Ssklower 		 */
53257433Sandrew 		if (m->m_flags & (M_BCAST|M_MCAST) ||
53367588Smckusick 		    IN_MULTICAST(ntohl(ti->ti_dst.s_addr)))
53425197Skarels 			goto drop;
53544375Skarels 		am = m_get(M_DONTWAIT, MT_SONAME);	/* XXX */
53610145Ssam 		if (am == NULL)
53710145Ssam 			goto drop;
53810145Ssam 		am->m_len = sizeof (struct sockaddr_in);
5398599Sroot 		sin = mtod(am, struct sockaddr_in *);
5408271Sroot 		sin->sin_family = AF_INET;
54137320Skarels 		sin->sin_len = sizeof(*sin);
5428271Sroot 		sin->sin_addr = ti->ti_src;
5438271Sroot 		sin->sin_port = ti->ti_sport;
54456399Ssklower 		bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero));
5456028Sroot 		laddr = inp->inp_laddr;
54610145Ssam 		if (inp->inp_laddr.s_addr == INADDR_ANY)
5476028Sroot 			inp->inp_laddr = ti->ti_dst;
5488599Sroot 		if (in_pcbconnect(inp, am)) {
5496028Sroot 			inp->inp_laddr = laddr;
5508716Sroot 			(void) m_free(am);
5515244Sroot 			goto drop;
5526028Sroot 		}
5538716Sroot 		(void) m_free(am);
5545244Sroot 		tp->t_template = tcp_template(tp);
5555244Sroot 		if (tp->t_template == 0) {
55626386Skarels 			tp = tcp_drop(tp, ENOBUFS);
55717264Skarels 			dropsocket = 0;		/* socket is already gone */
5585244Sroot 			goto drop;
5595244Sroot 		}
56057433Sandrew 		if (optp)
56157433Sandrew 			tcp_dooptions(tp, optp, optlen, ti,
56257433Sandrew 				&ts_present, &ts_val, &ts_ecr);
56330525Skarels 		if (iss)
56430525Skarels 			tp->iss = iss;
56530525Skarels 		else
56630525Skarels 			tp->iss = tcp_iss;
56730525Skarels 		tcp_iss += TCP_ISSINCR/2;
5685065Swnj 		tp->irs = ti->ti_seq;
5695085Swnj 		tcp_sendseqinit(tp);
5705085Swnj 		tcp_rcvseqinit(tp);
57125939Skarels 		tp->t_flags |= TF_ACKNOW;
5725065Swnj 		tp->t_state = TCPS_SYN_RECEIVED;
57333745Skarels 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
57410769Ssam 		dropsocket = 0;		/* committed to socket */
57530525Skarels 		tcpstat.tcps_accepts++;
5765085Swnj 		goto trimthenstep6;
5778271Sroot 		}
5784601Swnj 
5795065Swnj 	/*
5805065Swnj 	 * If the state is SYN_SENT:
5815065Swnj 	 *	if seg contains an ACK, but not for our SYN, drop the input.
5825065Swnj 	 *	if seg contains a RST, then drop the connection.
5835065Swnj 	 *	if seg does not contain SYN, then drop it.
5845065Swnj 	 * Otherwise this is an acceptable SYN segment
5855065Swnj 	 *	initialize tp->rcv_nxt and tp->irs
5865065Swnj 	 *	if seg contains ack then advance tp->snd_una
5875065Swnj 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
5885065Swnj 	 *	arrange for segment to be acked (eventually)
5895065Swnj 	 *	continue processing rest of data/controls, beginning with URG
5905065Swnj 	 */
5915065Swnj 	case TCPS_SYN_SENT:
5925065Swnj 		if ((tiflags & TH_ACK) &&
59324816Skarels 		    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
5945231Swnj 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
5955085Swnj 			goto dropwithreset;
5965065Swnj 		if (tiflags & TH_RST) {
59710394Ssam 			if (tiflags & TH_ACK)
59810394Ssam 				tp = tcp_drop(tp, ECONNREFUSED);
5995065Swnj 			goto drop;
6004601Swnj 		}
6015065Swnj 		if ((tiflags & TH_SYN) == 0)
6025065Swnj 			goto drop;
60330974Skarels 		if (tiflags & TH_ACK) {
60430974Skarels 			tp->snd_una = ti->ti_ack;
60530974Skarels 			if (SEQ_LT(tp->snd_nxt, tp->snd_una))
60630974Skarels 				tp->snd_nxt = tp->snd_una;
60730974Skarels 		}
6085244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
6095065Swnj 		tp->irs = ti->ti_seq;
6105085Swnj 		tcp_rcvseqinit(tp);
6115085Swnj 		tp->t_flags |= TF_ACKNOW;
61230974Skarels 		if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
61330525Skarels 			tcpstat.tcps_connects++;
6145244Sroot 			soisconnected(so);
6155065Swnj 			tp->t_state = TCPS_ESTABLISHED;
61657433Sandrew 			/* Do window scaling on this connection? */
61757433Sandrew 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
61857433Sandrew 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
61957433Sandrew 				tp->snd_scale = tp->requested_s_scale;
62057433Sandrew 				tp->rcv_scale = tp->request_r_scale;
62157433Sandrew 			}
62244375Skarels 			(void) tcp_reass(tp, (struct tcpiphdr *)0,
62344375Skarels 				(struct mbuf *)0);
62432098Skarels 			/*
62532098Skarels 			 * if we didn't have to retransmit the SYN,
62632098Skarels 			 * use its rtt as our initial srtt & rtt var.
62732098Skarels 			 */
62844375Skarels 			if (tp->t_rtt)
62957433Sandrew 				tcp_xmit_timer(tp, tp->t_rtt);
6305162Swnj 		} else
6315085Swnj 			tp->t_state = TCPS_SYN_RECEIVED;
6325085Swnj 
6335085Swnj trimthenstep6:
6345085Swnj 		/*
6355231Swnj 		 * Advance ti->ti_seq to correspond to first data byte.
6365085Swnj 		 * If data, trim to stay within window,
6375085Swnj 		 * dropping FIN if necessary.
6385085Swnj 		 */
6395231Swnj 		ti->ti_seq++;
6405085Swnj 		if (ti->ti_len > tp->rcv_wnd) {
6415085Swnj 			todrop = ti->ti_len - tp->rcv_wnd;
6425085Swnj 			m_adj(m, -todrop);
6435085Swnj 			ti->ti_len = tp->rcv_wnd;
64425939Skarels 			tiflags &= ~TH_FIN;
64530525Skarels 			tcpstat.tcps_rcvpackafterwin++;
64630525Skarels 			tcpstat.tcps_rcvbyteafterwin += todrop;
6475065Swnj 		}
6485263Swnj 		tp->snd_wl1 = ti->ti_seq - 1;
64925939Skarels 		tp->rcv_up = ti->ti_seq;
6505085Swnj 		goto step6;
6515065Swnj 	}
6524601Swnj 
6535065Swnj 	/*
65430525Skarels 	 * States other than LISTEN or SYN_SENT.
65557433Sandrew 	 * First check timestamp, if present.
65657433Sandrew 	 * Then check that at least some bytes of segment are within
65730525Skarels 	 * receive window.  If segment begins before rcv_nxt,
65830525Skarels 	 * drop leading data (and SYN); if nothing left, just ack.
65957433Sandrew 	 *
66057433Sandrew 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
66157433Sandrew 	 * and it's less than ts_recent, drop it.
66216222Skarels 	 */
66357433Sandrew 	if (ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent &&
66457433Sandrew 	    TSTMP_LT(ts_val, tp->ts_recent)) {
66557433Sandrew 
66657433Sandrew 		/* Check to see if ts_recent is over 24 days old.  */
66757433Sandrew 		if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
66857433Sandrew 			/*
66957433Sandrew 			 * Invalidate ts_recent.  If this segment updates
67057433Sandrew 			 * ts_recent, the age will be reset later and ts_recent
67157433Sandrew 			 * will get a valid value.  If it does not, setting
67257433Sandrew 			 * ts_recent to zero will at least satisfy the
67357433Sandrew 			 * requirement that zero be placed in the timestamp
67457433Sandrew 			 * echo reply when ts_recent isn't valid.  The
67557433Sandrew 			 * age isn't reset until we get a valid ts_recent
67657433Sandrew 			 * because we don't want out-of-order segments to be
67757433Sandrew 			 * dropped when ts_recent is old.
67857433Sandrew 			 */
67957433Sandrew 			tp->ts_recent = 0;
68057433Sandrew 		} else {
68157433Sandrew 			tcpstat.tcps_rcvduppack++;
68257433Sandrew 			tcpstat.tcps_rcvdupbyte += ti->ti_len;
68357433Sandrew 			tcpstat.tcps_pawsdrop++;
68457433Sandrew 			goto dropafterack;
68557433Sandrew 		}
68657433Sandrew 	}
68757433Sandrew 
68830525Skarels 	todrop = tp->rcv_nxt - ti->ti_seq;
68930525Skarels 	if (todrop > 0) {
69030525Skarels 		if (tiflags & TH_SYN) {
69130525Skarels 			tiflags &= ~TH_SYN;
69230525Skarels 			ti->ti_seq++;
69330525Skarels 			if (ti->ti_urp > 1)
69430525Skarels 				ti->ti_urp--;
69530525Skarels 			else
69630525Skarels 				tiflags &= ~TH_URG;
69730525Skarels 			todrop--;
69830525Skarels 		}
69964176Smckusick 		if (todrop >= ti->ti_len) {
70033745Skarels 			tcpstat.tcps_rcvduppack++;
70133745Skarels 			tcpstat.tcps_rcvdupbyte += ti->ti_len;
70231730Skarels 			/*
70333745Skarels 			 * If segment is just one to the left of the window,
70433745Skarels 			 * check two special cases:
70533745Skarels 			 * 1. Don't toss RST in response to 4.2-style keepalive.
70633745Skarels 			 * 2. If the only thing to drop is a FIN, we can drop
70733745Skarels 			 *    it, but check the ACK or we will get into FIN
70833745Skarels 			 *    wars if our FINs crossed (both CLOSING).
70933745Skarels 			 * In either case, send ACK to resynchronize,
71033745Skarels 			 * but keep on processing for RST or ACK.
71131730Skarels 			 */
71233745Skarels 			if ((tiflags & TH_FIN && todrop == ti->ti_len + 1)
71333745Skarels #ifdef TCP_COMPAT_42
71433745Skarels 			  || (tiflags & TH_RST && ti->ti_seq == tp->rcv_nxt - 1)
71531730Skarels #endif
71633745Skarels 			   ) {
71733745Skarels 				todrop = ti->ti_len;
71833745Skarels 				tiflags &= ~TH_FIN;
71957433Sandrew 			} else {
72057433Sandrew 				/*
72157433Sandrew 				 * Handle the case when a bound socket connects
72257433Sandrew 				 * to itself. Allow packets with a SYN and
72357433Sandrew 				 * an ACK to continue with the processing.
72457433Sandrew 				 */
72557433Sandrew 				if (todrop != 0 || (tiflags & TH_ACK) == 0)
72657433Sandrew 					goto dropafterack;
72757433Sandrew 			}
728*68512Sdab 			tp->t_flags |= TF_ACKNOW;
72932034Skarels 		} else {
73032034Skarels 			tcpstat.tcps_rcvpartduppack++;
73132034Skarels 			tcpstat.tcps_rcvpartdupbyte += todrop;
73230525Skarels 		}
73330525Skarels 		m_adj(m, todrop);
73430525Skarels 		ti->ti_seq += todrop;
73530525Skarels 		ti->ti_len -= todrop;
73630525Skarels 		if (ti->ti_urp > todrop)
73730525Skarels 			ti->ti_urp -= todrop;
73830525Skarels 		else {
73930525Skarels 			tiflags &= ~TH_URG;
74030525Skarels 			ti->ti_urp = 0;
74130525Skarels 		}
74216222Skarels 	}
74316222Skarels 
74432612Skarels 	/*
74533745Skarels 	 * If new data are received on a connection after the
74632612Skarels 	 * user processes are gone, then RST the other end.
74732612Skarels 	 */
74832612Skarels 	if ((so->so_state & SS_NOFDREF) &&
74932612Skarels 	    tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
75032612Skarels 		tp = tcp_close(tp);
75132612Skarels 		tcpstat.tcps_rcvafterclose++;
75232612Skarels 		goto dropwithreset;
75332612Skarels 	}
75432612Skarels 
75533445Skarels 	/*
75633445Skarels 	 * If segment ends after window, drop trailing data
75733445Skarels 	 * (and PUSH and FIN); if nothing left, just ACK.
75833445Skarels 	 */
75933445Skarels 	todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
76033445Skarels 	if (todrop > 0) {
76133445Skarels 		tcpstat.tcps_rcvpackafterwin++;
76233445Skarels 		if (todrop >= ti->ti_len) {
76330525Skarels 			tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
76433445Skarels 			/*
76533445Skarels 			 * If a new connection request is received
76633445Skarels 			 * while in TIME_WAIT, drop the old connection
76733445Skarels 			 * and start over if the sequence numbers
76833445Skarels 			 * are above the previous ones.
76933445Skarels 			 */
77033445Skarels 			if (tiflags & TH_SYN &&
77133445Skarels 			    tp->t_state == TCPS_TIME_WAIT &&
77233445Skarels 			    SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
77333445Skarels 				iss = tp->rcv_nxt + TCP_ISSINCR;
77444375Skarels 				tp = tcp_close(tp);
77533445Skarels 				goto findpcb;
77633445Skarels 			}
77733445Skarels 			/*
77833445Skarels 			 * If window is closed can only take segments at
77933445Skarels 			 * window edge, and have to drop data and PUSH from
78033445Skarels 			 * incoming segments.  Continue processing, but
78133445Skarels 			 * remember to ack.  Otherwise, drop segment
78233445Skarels 			 * and ack.
78333445Skarels 			 */
78433445Skarels 			if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
78533445Skarels 				tp->t_flags |= TF_ACKNOW;
78630525Skarels 				tcpstat.tcps_rcvwinprobe++;
78733445Skarels 			} else
7885065Swnj 				goto dropafterack;
78933445Skarels 		} else
79030525Skarels 			tcpstat.tcps_rcvbyteafterwin += todrop;
79133445Skarels 		m_adj(m, -todrop);
79233445Skarels 		ti->ti_len -= todrop;
79333445Skarels 		tiflags &= ~(TH_PUSH|TH_FIN);
7945065Swnj 	}
7954601Swnj 
7965065Swnj 	/*
79757433Sandrew 	 * If last ACK falls within this segment's sequence numbers,
79857433Sandrew 	 * record its timestamp.
79957433Sandrew 	 */
80057433Sandrew 	if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
80157433Sandrew 	    SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len +
80257433Sandrew 		   ((tiflags & (TH_SYN|TH_FIN)) != 0))) {
80357433Sandrew 		tp->ts_recent_age = tcp_now;
80457433Sandrew 		tp->ts_recent = ts_val;
80557433Sandrew 	}
80657433Sandrew 
80757433Sandrew 	/*
8085065Swnj 	 * If the RST bit is set examine the state:
8095065Swnj 	 *    SYN_RECEIVED STATE:
8105065Swnj 	 *	If passive open, return to LISTEN state.
8115065Swnj 	 *	If active open, inform user that connection was refused.
8125065Swnj 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
8135065Swnj 	 *	Inform user that connection was reset, and close tcb.
8145065Swnj 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
8155065Swnj 	 *	Close the tcb.
8165065Swnj 	 */
8175065Swnj 	if (tiflags&TH_RST) switch (tp->t_state) {
8185267Sroot 
8195065Swnj 	case TCPS_SYN_RECEIVED:
82033745Skarels 		so->so_error = ECONNREFUSED;
82133745Skarels 		goto close;
8224601Swnj 
8235065Swnj 	case TCPS_ESTABLISHED:
8245065Swnj 	case TCPS_FIN_WAIT_1:
8255065Swnj 	case TCPS_FIN_WAIT_2:
8265065Swnj 	case TCPS_CLOSE_WAIT:
82733745Skarels 		so->so_error = ECONNRESET;
82833745Skarels 	close:
82933745Skarels 		tp->t_state = TCPS_CLOSED;
83033745Skarels 		tcpstat.tcps_drops++;
83133745Skarels 		tp = tcp_close(tp);
8325065Swnj 		goto drop;
8335065Swnj 
8345065Swnj 	case TCPS_CLOSING:
8355065Swnj 	case TCPS_LAST_ACK:
8365065Swnj 	case TCPS_TIME_WAIT:
83710394Ssam 		tp = tcp_close(tp);
8385065Swnj 		goto drop;
8394601Swnj 	}
8404601Swnj 
8414601Swnj 	/*
8425065Swnj 	 * If a SYN is in the window, then this is an
8435065Swnj 	 * error and we send an RST and drop the connection.
8444601Swnj 	 */
8455065Swnj 	if (tiflags & TH_SYN) {
84610394Ssam 		tp = tcp_drop(tp, ECONNRESET);
8475085Swnj 		goto dropwithreset;
8484601Swnj 	}
8494601Swnj 
8504601Swnj 	/*
8515065Swnj 	 * If the ACK bit is off we drop the segment and return.
8524601Swnj 	 */
8535085Swnj 	if ((tiflags & TH_ACK) == 0)
8545065Swnj 		goto drop;
8555065Swnj 
8565065Swnj 	/*
8575065Swnj 	 * Ack processing.
8585065Swnj 	 */
8594601Swnj 	switch (tp->t_state) {
8604601Swnj 
8615065Swnj 	/*
8625065Swnj 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
86331721Skarels 	 * ESTABLISHED state and continue processing, otherwise
8645065Swnj 	 * send an RST.
8655065Swnj 	 */
8665065Swnj 	case TCPS_SYN_RECEIVED:
8675085Swnj 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
8685231Swnj 		    SEQ_GT(ti->ti_ack, tp->snd_max))
8695085Swnj 			goto dropwithreset;
87030525Skarels 		tcpstat.tcps_connects++;
8715085Swnj 		soisconnected(so);
8725085Swnj 		tp->t_state = TCPS_ESTABLISHED;
87357433Sandrew 		/* Do window scaling? */
87457433Sandrew 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
87557433Sandrew 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
87657433Sandrew 			tp->snd_scale = tp->requested_s_scale;
87757433Sandrew 			tp->rcv_scale = tp->request_r_scale;
87857433Sandrew 		}
87944375Skarels 		(void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0);
8805244Sroot 		tp->snd_wl1 = ti->ti_seq - 1;
8815085Swnj 		/* fall into ... */
8824601Swnj 
8835065Swnj 	/*
8845065Swnj 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
8855065Swnj 	 * ACKs.  If the ack is in the range
8865231Swnj 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
8875065Swnj 	 * then advance tp->snd_una to ti->ti_ack and drop
8885065Swnj 	 * data from the retransmission queue.  If this ACK reflects
8895065Swnj 	 * more up to date window information we update our window information.
8905065Swnj 	 */
8915065Swnj 	case TCPS_ESTABLISHED:
8925065Swnj 	case TCPS_FIN_WAIT_1:
8935065Swnj 	case TCPS_FIN_WAIT_2:
8945065Swnj 	case TCPS_CLOSE_WAIT:
8955065Swnj 	case TCPS_CLOSING:
8965244Sroot 	case TCPS_LAST_ACK:
8975244Sroot 	case TCPS_TIME_WAIT:
8985085Swnj 
89930525Skarels 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
90057433Sandrew 			if (ti->ti_len == 0 && tiwin == tp->snd_wnd) {
90130525Skarels 				tcpstat.tcps_rcvdupack++;
90232098Skarels 				/*
90344375Skarels 				 * If we have outstanding data (other than
90444375Skarels 				 * a window probe), this is a completely
90532098Skarels 				 * duplicate ack (ie, window info didn't
90632098Skarels 				 * change), the ack is the biggest we've
90732098Skarels 				 * seen and we've seen exactly our rexmt
90832098Skarels 				 * threshhold of them, assume a packet
90932098Skarels 				 * has been dropped and retransmit it.
91032098Skarels 				 * Kludge snd_nxt & the congestion
91132098Skarels 				 * window so we send only this one
91244375Skarels 				 * packet.
91344375Skarels 				 *
91444375Skarels 				 * We know we're losing at the current
91544375Skarels 				 * window size so do congestion avoidance
91644375Skarels 				 * (set ssthresh to half the current window
91744375Skarels 				 * and pull our congestion window back to
91844375Skarels 				 * the new ssthresh).
91944375Skarels 				 *
92044375Skarels 				 * Dup acks mean that packets have left the
92144375Skarels 				 * network (they're now cached at the receiver)
92244375Skarels 				 * so bump cwnd by the amount in the receiver
92344375Skarels 				 * to keep a constant cwnd packets in the
92444375Skarels 				 * network.
92532098Skarels 				 */
92632098Skarels 				if (tp->t_timer[TCPT_REXMT] == 0 ||
92732098Skarels 				    ti->ti_ack != tp->snd_una)
92832098Skarels 					tp->t_dupacks = 0;
92932098Skarels 				else if (++tp->t_dupacks == tcprexmtthresh) {
93032098Skarels 					tcp_seq onxt = tp->snd_nxt;
93132376Skarels 					u_int win =
93237320Skarels 					    min(tp->snd_wnd, tp->snd_cwnd) / 2 /
93332376Skarels 						tp->t_maxseg;
93432098Skarels 
93532376Skarels 					if (win < 2)
93632376Skarels 						win = 2;
93732376Skarels 					tp->snd_ssthresh = win * tp->t_maxseg;
93832098Skarels 					tp->t_timer[TCPT_REXMT] = 0;
93932098Skarels 					tp->t_rtt = 0;
94032098Skarels 					tp->snd_nxt = ti->ti_ack;
94132098Skarels 					tp->snd_cwnd = tp->t_maxseg;
94232098Skarels 					(void) tcp_output(tp);
94344375Skarels 					tp->snd_cwnd = tp->snd_ssthresh +
94444375Skarels 					       tp->t_maxseg * tp->t_dupacks;
94532098Skarels 					if (SEQ_GT(onxt, tp->snd_nxt))
94632098Skarels 						tp->snd_nxt = onxt;
94732098Skarels 					goto drop;
94844375Skarels 				} else if (tp->t_dupacks > tcprexmtthresh) {
94944375Skarels 					tp->snd_cwnd += tp->t_maxseg;
95044375Skarels 					(void) tcp_output(tp);
95144375Skarels 					goto drop;
95232098Skarels 				}
95332098Skarels 			} else
95432098Skarels 				tp->t_dupacks = 0;
9555065Swnj 			break;
95630525Skarels 		}
95744375Skarels 		/*
95844375Skarels 		 * If the congestion window was inflated to account
95944375Skarels 		 * for the other side's cached packets, retract it.
96044375Skarels 		 */
96144375Skarels 		if (tp->t_dupacks > tcprexmtthresh &&
96244375Skarels 		    tp->snd_cwnd > tp->snd_ssthresh)
96344375Skarels 			tp->snd_cwnd = tp->snd_ssthresh;
96432098Skarels 		tp->t_dupacks = 0;
96530525Skarels 		if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
96630525Skarels 			tcpstat.tcps_rcvacktoomuch++;
9675065Swnj 			goto dropafterack;
96830525Skarels 		}
9695085Swnj 		acked = ti->ti_ack - tp->snd_una;
97030525Skarels 		tcpstat.tcps_rcvackpack++;
97130525Skarels 		tcpstat.tcps_rcvackbyte += acked;
9725951Swnj 
9735951Swnj 		/*
97457433Sandrew 		 * If we have a timestamp reply, update smoothed
97557433Sandrew 		 * round trip time.  If no timestamp is present but
97657433Sandrew 		 * transmit timer is running and timed sequence
9775951Swnj 		 * number was acked, update smoothed round trip time.
97832034Skarels 		 * Since we now have an rtt measurement, cancel the
97932034Skarels 		 * timer backoff (cf., Phil Karn's retransmit alg.).
98032034Skarels 		 * Recompute the initial retransmit timer.
9815951Swnj 		 */
98257433Sandrew 		if (ts_present)
98357433Sandrew 			tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
98457433Sandrew 		else if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
98557433Sandrew 			tcp_xmit_timer(tp,tp->t_rtt);
98631726Skarels 
98726824Skarels 		/*
98826824Skarels 		 * If all outstanding data is acked, stop retransmit
98926824Skarels 		 * timer and remember to restart (more output or persist).
99026824Skarels 		 * If there is more data to be acked, restart retransmit
99132034Skarels 		 * timer, using current (possibly backed-off) value.
99226824Skarels 		 */
99326824Skarels 		if (ti->ti_ack == tp->snd_max) {
9945244Sroot 			tp->t_timer[TCPT_REXMT] = 0;
99526824Skarels 			needoutput = 1;
99632034Skarels 		} else if (tp->t_timer[TCPT_PERSIST] == 0)
99732034Skarels 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
99817360Skarels 		/*
99932098Skarels 		 * When new data is acked, open the congestion window.
100032098Skarels 		 * If the window gives us less than ssthresh packets
100132098Skarels 		 * in flight, open exponentially (maxseg per packet).
100244375Skarels 		 * Otherwise open linearly: maxseg per window
100367870Smckusick 		 * (maxseg * (maxseg / cwnd) per packet).
100417360Skarels 		 */
100532098Skarels 		{
100644375Skarels 		register u_int cw = tp->snd_cwnd;
100744375Skarels 		register u_int incr = tp->t_maxseg;
100832098Skarels 
100944375Skarels 		if (cw > tp->snd_ssthresh)
101067870Smckusick 			incr = incr * incr / cw;
101157433Sandrew 		tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale);
101232098Skarels 		}
10135307Sroot 		if (acked > so->so_snd.sb_cc) {
101415386Ssam 			tp->snd_wnd -= so->so_snd.sb_cc;
101526386Skarels 			sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
101631721Skarels 			ourfinisacked = 1;
10175307Sroot 		} else {
10186161Ssam 			sbdrop(&so->so_snd, acked);
10195307Sroot 			tp->snd_wnd -= acked;
102031721Skarels 			ourfinisacked = 0;
10215307Sroot 		}
102244375Skarels 		if (so->so_snd.sb_flags & SB_NOTIFY)
102344375Skarels 			sowwakeup(so);
10245231Swnj 		tp->snd_una = ti->ti_ack;
10255357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
10265357Sroot 			tp->snd_nxt = tp->snd_una;
10275162Swnj 
10284601Swnj 		switch (tp->t_state) {
10294601Swnj 
10305065Swnj 		/*
10315065Swnj 		 * In FIN_WAIT_1 STATE in addition to the processing
10325065Swnj 		 * for the ESTABLISHED state if our FIN is now acknowledged
10335085Swnj 		 * then enter FIN_WAIT_2.
10345065Swnj 		 */
10355065Swnj 		case TCPS_FIN_WAIT_1:
10365896Swnj 			if (ourfinisacked) {
10375896Swnj 				/*
10385896Swnj 				 * If we can't receive any more
10395896Swnj 				 * data, then closing user can proceed.
104024816Skarels 				 * Starting the timer is contrary to the
104124816Skarels 				 * specification, but if we don't get a FIN
104224816Skarels 				 * we'll hang forever.
10435896Swnj 				 */
104424816Skarels 				if (so->so_state & SS_CANTRCVMORE) {
10455896Swnj 					soisdisconnected(so);
104633745Skarels 					tp->t_timer[TCPT_2MSL] = tcp_maxidle;
104724816Skarels 				}
10485085Swnj 				tp->t_state = TCPS_FIN_WAIT_2;
10495896Swnj 			}
10504601Swnj 			break;
10514601Swnj 
10525065Swnj 	 	/*
10535065Swnj 		 * In CLOSING STATE in addition to the processing for
10545065Swnj 		 * the ESTABLISHED state if the ACK acknowledges our FIN
10555065Swnj 		 * then enter the TIME-WAIT state, otherwise ignore
10565065Swnj 		 * the segment.
10575065Swnj 		 */
10585065Swnj 		case TCPS_CLOSING:
10595244Sroot 			if (ourfinisacked) {
10605065Swnj 				tp->t_state = TCPS_TIME_WAIT;
10615244Sroot 				tcp_canceltimers(tp);
10625244Sroot 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
10635244Sroot 				soisdisconnected(so);
10645244Sroot 			}
10655244Sroot 			break;
10664601Swnj 
10675065Swnj 		/*
106831743Skarels 		 * In LAST_ACK, we may still be waiting for data to drain
106931743Skarels 		 * and/or to be acked, as well as for the ack of our FIN.
107031743Skarels 		 * If our FIN is now acknowledged, delete the TCB,
107131743Skarels 		 * enter the closed state and return.
10725065Swnj 		 */
10735065Swnj 		case TCPS_LAST_ACK:
107431743Skarels 			if (ourfinisacked) {
107510394Ssam 				tp = tcp_close(tp);
107631743Skarels 				goto drop;
107731743Skarels 			}
107831743Skarels 			break;
10794601Swnj 
10805065Swnj 		/*
10815065Swnj 		 * In TIME_WAIT state the only thing that should arrive
10825065Swnj 		 * is a retransmission of the remote FIN.  Acknowledge
10835065Swnj 		 * it and restart the finack timer.
10845065Swnj 		 */
10855065Swnj 		case TCPS_TIME_WAIT:
10865162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
10875065Swnj 			goto dropafterack;
10884601Swnj 		}
10895085Swnj 	}
10904601Swnj 
10915065Swnj step6:
10925065Swnj 	/*
10935244Sroot 	 * Update window information.
109425939Skarels 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
10955244Sroot 	 */
109625939Skarels 	if ((tiflags & TH_ACK) &&
109725939Skarels 	    (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
10985391Swnj 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
109957433Sandrew 	     tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))) {
110030525Skarels 		/* keep track of pure window updates */
110130525Skarels 		if (ti->ti_len == 0 &&
110257433Sandrew 		    tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd)
110330525Skarels 			tcpstat.tcps_rcvwinupd++;
110457433Sandrew 		tp->snd_wnd = tiwin;
11055244Sroot 		tp->snd_wl1 = ti->ti_seq;
11065244Sroot 		tp->snd_wl2 = ti->ti_ack;
110725260Skarels 		if (tp->snd_wnd > tp->max_sndwnd)
110825260Skarels 			tp->max_sndwnd = tp->snd_wnd;
110926824Skarels 		needoutput = 1;
111026824Skarels 	}
11115244Sroot 
11125244Sroot 	/*
11135547Swnj 	 * Process segments with URG.
11145065Swnj 	 */
11157267Swnj 	if ((tiflags & TH_URG) && ti->ti_urp &&
11167267Swnj 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
11175547Swnj 		/*
111825939Skarels 		 * This is a kludge, but if we receive and accept
111913121Ssam 		 * random urgent pointers, we'll crash in
112013121Ssam 		 * soreceive.  It's hard to imagine someone
112113121Ssam 		 * actually wanting to send this much urgent data.
112212441Ssam 		 */
112357433Sandrew 		if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) {
112412441Ssam 			ti->ti_urp = 0;			/* XXX */
112512441Ssam 			tiflags &= ~TH_URG;		/* XXX */
112625939Skarels 			goto dodata;			/* XXX */
112712441Ssam 		}
112812441Ssam 		/*
11295547Swnj 		 * If this segment advances the known urgent pointer,
11305547Swnj 		 * then mark the data stream.  This should not happen
11315547Swnj 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
11325547Swnj 		 * a FIN has been received from the remote side.
11335547Swnj 		 * In these states we ignore the URG.
113427190Skarels 		 *
113527190Skarels 		 * According to RFC961 (Assigned Protocols),
113627190Skarels 		 * the urgent pointer points to the last octet
113727190Skarels 		 * of urgent data.  We continue, however,
113827190Skarels 		 * to consider it to indicate the first octet
113944375Skarels 		 * of data past the urgent section as the original
114044375Skarels 		 * spec states (in one of two places).
11415547Swnj 		 */
11425547Swnj 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
11435547Swnj 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
11445547Swnj 			so->so_oobmark = so->so_rcv.sb_cc +
11455547Swnj 			    (tp->rcv_up - tp->rcv_nxt) - 1;
11465547Swnj 			if (so->so_oobmark == 0)
11475547Swnj 				so->so_state |= SS_RCVATMARK;
11488313Sroot 			sohasoutofband(so);
114924816Skarels 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
11505440Swnj 		}
11515547Swnj 		/*
11525547Swnj 		 * Remove out of band data so doesn't get presented to user.
11535547Swnj 		 * This can happen independent of advancing the URG pointer,
11545547Swnj 		 * but if two URG's are pending at once, some out-of-band
11555547Swnj 		 * data may creep in... ick.
11565547Swnj 		 */
115744375Skarels 		if (ti->ti_urp <= ti->ti_len
115844375Skarels #ifdef SO_OOBINLINE
115944375Skarels 		     && (so->so_options & SO_OOBINLINE) == 0
116044375Skarels #endif
116144375Skarels 		     )
116244375Skarels 			tcp_pulloutofband(so, ti, m);
116325939Skarels 	} else
116425939Skarels 		/*
116525939Skarels 		 * If no out of band data is expected,
116625939Skarels 		 * pull receive urgent pointer along
116725939Skarels 		 * with the receive window.
116825939Skarels 		 */
116925939Skarels 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
117025939Skarels 			tp->rcv_up = tp->rcv_nxt;
117125939Skarels dodata:							/* XXX */
11724601Swnj 
11734601Swnj 	/*
11745065Swnj 	 * Process the segment text, merging it into the TCP sequencing queue,
11755065Swnj 	 * and arranging for acknowledgment of receipt if necessary.
11765065Swnj 	 * This process logically involves adjusting tp->rcv_wnd as data
11775065Swnj 	 * is presented to the user (this happens in tcp_usrreq.c,
11785065Swnj 	 * case PRU_RCVD).  If a FIN has already been received on this
11795065Swnj 	 * connection then we just ignore the text.
11804601Swnj 	 */
118117946Skarels 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
118217946Skarels 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
118324816Skarels 		TCP_REASS(tp, ti, m, so, tiflags);
118425260Skarels 		/*
118525260Skarels 		 * Note the amount of data that peer has sent into
118625260Skarels 		 * our window, in order to estimate the sender's
118725260Skarels 		 * buffer size.
118825260Skarels 		 */
118932098Skarels 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
11905244Sroot 	} else {
11914924Swnj 		m_freem(m);
11925263Swnj 		tiflags &= ~TH_FIN;
11935244Sroot 	}
11944601Swnj 
11954601Swnj 	/*
11965263Swnj 	 * If FIN is received ACK the FIN and let the user know
11975263Swnj 	 * that the connection is closing.
11984601Swnj 	 */
11995263Swnj 	if (tiflags & TH_FIN) {
12005244Sroot 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
12015244Sroot 			socantrcvmore(so);
12025244Sroot 			tp->t_flags |= TF_ACKNOW;
12035244Sroot 			tp->rcv_nxt++;
12045244Sroot 		}
12055065Swnj 		switch (tp->t_state) {
12064601Swnj 
12075065Swnj 	 	/*
12085065Swnj 		 * In SYN_RECEIVED and ESTABLISHED STATES
12095065Swnj 		 * enter the CLOSE_WAIT state.
12104884Swnj 		 */
12115065Swnj 		case TCPS_SYN_RECEIVED:
12125065Swnj 		case TCPS_ESTABLISHED:
12135065Swnj 			tp->t_state = TCPS_CLOSE_WAIT;
12145065Swnj 			break;
12154884Swnj 
12165065Swnj 	 	/*
12175085Swnj 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
12185085Swnj 		 * enter the CLOSING state.
12194884Swnj 		 */
12205065Swnj 		case TCPS_FIN_WAIT_1:
12215085Swnj 			tp->t_state = TCPS_CLOSING;
12225065Swnj 			break;
12234601Swnj 
12245065Swnj 	 	/*
12255065Swnj 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
12265065Swnj 		 * starting the time-wait timer, turning off the other
12275065Swnj 		 * standard timers.
12285065Swnj 		 */
12295065Swnj 		case TCPS_FIN_WAIT_2:
12305244Sroot 			tp->t_state = TCPS_TIME_WAIT;
12315074Swnj 			tcp_canceltimers(tp);
12325162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
12335244Sroot 			soisdisconnected(so);
12345065Swnj 			break;
12355065Swnj 
12364884Swnj 		/*
12375065Swnj 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
12384884Swnj 		 */
12395065Swnj 		case TCPS_TIME_WAIT:
12405162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
12415065Swnj 			break;
12425085Swnj 		}
12434601Swnj 	}
12445267Sroot 	if (so->so_options & SO_DEBUG)
12455267Sroot 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
12465085Swnj 
12475085Swnj 	/*
12485085Swnj 	 * Return any desired output.
12495085Swnj 	 */
125026824Skarels 	if (needoutput || (tp->t_flags & TF_ACKNOW))
125125939Skarels 		(void) tcp_output(tp);
12525065Swnj 	return;
12535085Swnj 
12545065Swnj dropafterack:
12555085Swnj 	/*
12566211Swnj 	 * Generate an ACK dropping incoming segment if it occupies
12576211Swnj 	 * sequence space, where the ACK reflects our state.
12585085Swnj 	 */
125926057Skarels 	if (tiflags & TH_RST)
12605085Swnj 		goto drop;
126131749Skarels 	m_freem(m);
126231721Skarels 	tp->t_flags |= TF_ACKNOW;
126331721Skarels 	(void) tcp_output(tp);
12645231Swnj 	return;
12655085Swnj 
12665085Swnj dropwithreset:
12675085Swnj 	/*
12685244Sroot 	 * Generate a RST, dropping incoming segment.
12695085Swnj 	 * Make ACK acceptable to originator of segment.
127057433Sandrew 	 * Don't bother to respond if destination was broadcast/multicast.
12715085Swnj 	 */
127257433Sandrew 	if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST) ||
127367588Smckusick 	    IN_MULTICAST(ntohl(ti->ti_dst.s_addr)))
12745085Swnj 		goto drop;
12755085Swnj 	if (tiflags & TH_ACK)
127637320Skarels 		tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
12775085Swnj 	else {
12785085Swnj 		if (tiflags & TH_SYN)
12795085Swnj 			ti->ti_len++;
128037320Skarels 		tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
12816211Swnj 		    TH_RST|TH_ACK);
12825085Swnj 	}
128310769Ssam 	/* destroy temporarily created socket */
128410769Ssam 	if (dropsocket)
128510769Ssam 		(void) soabort(so);
12865231Swnj 	return;
12875085Swnj 
12885065Swnj drop:
12895085Swnj 	/*
12905085Swnj 	 * Drop space held by incoming segment and return.
12915085Swnj 	 */
12926303Sroot 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
12936303Sroot 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
12945065Swnj 	m_freem(m);
129510769Ssam 	/* destroy temporarily created socket */
129610769Ssam 	if (dropsocket)
129710769Ssam 		(void) soabort(so);
12985267Sroot 	return;
129957947Ssklower #ifndef TUBA_INCLUDE
13005065Swnj }
13015065Swnj 
130261335Sbostic void
130357433Sandrew tcp_dooptions(tp, cp, cnt, ti, ts_present, ts_val, ts_ecr)
13045440Swnj 	struct tcpcb *tp;
130557433Sandrew 	u_char *cp;
130657433Sandrew 	int cnt;
130717272Skarels 	struct tcpiphdr *ti;
130857433Sandrew 	int *ts_present;
130957433Sandrew 	u_long *ts_val, *ts_ecr;
13105419Swnj {
131144375Skarels 	u_short mss;
131257433Sandrew 	int opt, optlen;
13135419Swnj 
13145440Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
13155440Swnj 		opt = cp[0];
13165440Swnj 		if (opt == TCPOPT_EOL)
13175440Swnj 			break;
13185440Swnj 		if (opt == TCPOPT_NOP)
13195440Swnj 			optlen = 1;
132012169Ssam 		else {
13215440Swnj 			optlen = cp[1];
132212169Ssam 			if (optlen <= 0)
132312169Ssam 				break;
132412169Ssam 		}
13255440Swnj 		switch (opt) {
13265440Swnj 
13275440Swnj 		default:
132844375Skarels 			continue;
13295440Swnj 
13305440Swnj 		case TCPOPT_MAXSEG:
133157433Sandrew 			if (optlen != TCPOLEN_MAXSEG)
13325440Swnj 				continue;
133317272Skarels 			if (!(ti->ti_flags & TH_SYN))
133417272Skarels 				continue;
133544375Skarels 			bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
133644375Skarels 			NTOHS(mss);
133744375Skarels 			(void) tcp_mss(tp, mss);	/* sets t_maxseg */
13385440Swnj 			break;
133957433Sandrew 
134057433Sandrew 		case TCPOPT_WINDOW:
134157433Sandrew 			if (optlen != TCPOLEN_WINDOW)
134257433Sandrew 				continue;
134357433Sandrew 			if (!(ti->ti_flags & TH_SYN))
134457433Sandrew 				continue;
134557433Sandrew 			tp->t_flags |= TF_RCVD_SCALE;
134657433Sandrew 			tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
134757433Sandrew 			break;
134857433Sandrew 
134957433Sandrew 		case TCPOPT_TIMESTAMP:
135057433Sandrew 			if (optlen != TCPOLEN_TIMESTAMP)
135157433Sandrew 				continue;
135257433Sandrew 			*ts_present = 1;
135357433Sandrew 			bcopy((char *)cp + 2, (char *) ts_val, sizeof(*ts_val));
135457433Sandrew 			NTOHL(*ts_val);
135557433Sandrew 			bcopy((char *)cp + 6, (char *) ts_ecr, sizeof(*ts_ecr));
135657433Sandrew 			NTOHL(*ts_ecr);
135757433Sandrew 
135857433Sandrew 			/*
135957433Sandrew 			 * A timestamp received in a SYN makes
136057433Sandrew 			 * it ok to send timestamp requests and replies.
136157433Sandrew 			 */
136257433Sandrew 			if (ti->ti_flags & TH_SYN) {
136357433Sandrew 				tp->t_flags |= TF_RCVD_TSTMP;
136457433Sandrew 				tp->ts_recent = *ts_val;
136557433Sandrew 				tp->ts_recent_age = tcp_now;
136657433Sandrew 			}
136757433Sandrew 			break;
13685419Swnj 		}
13695419Swnj 	}
13705419Swnj }
13715419Swnj 
13725419Swnj /*
13735547Swnj  * Pull out of band byte out of a segment so
13745547Swnj  * it doesn't appear in the user's data queue.
13755547Swnj  * It is still reflected in the segment length for
13765547Swnj  * sequencing purposes.
13775547Swnj  */
137861335Sbostic void
137944375Skarels tcp_pulloutofband(so, ti, m)
13805547Swnj 	struct socket *so;
13815547Swnj 	struct tcpiphdr *ti;
138244375Skarels 	register struct mbuf *m;
13835547Swnj {
13846116Swnj 	int cnt = ti->ti_urp - 1;
13855547Swnj 
13865547Swnj 	while (cnt >= 0) {
13875547Swnj 		if (m->m_len > cnt) {
13885547Swnj 			char *cp = mtod(m, caddr_t) + cnt;
13895547Swnj 			struct tcpcb *tp = sototcpcb(so);
13905547Swnj 
13915547Swnj 			tp->t_iobc = *cp;
13925547Swnj 			tp->t_oobflags |= TCPOOB_HAVEDATA;
13936161Ssam 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
13945547Swnj 			m->m_len--;
13955547Swnj 			return;
13965547Swnj 		}
13975547Swnj 		cnt -= m->m_len;
13985547Swnj 		m = m->m_next;
13995547Swnj 		if (m == 0)
14005547Swnj 			break;
14015547Swnj 	}
14025547Swnj 	panic("tcp_pulloutofband");
14035547Swnj }
14045547Swnj 
14055547Swnj /*
140644375Skarels  * Collect new round-trip time estimate
140744375Skarels  * and update averages and current timeout.
140817272Skarels  */
140961335Sbostic void
141057433Sandrew tcp_xmit_timer(tp, rtt)
141123975Skarels 	register struct tcpcb *tp;
141261335Sbostic 	short rtt;
141317272Skarels {
141444375Skarels 	register short delta;
141544375Skarels 
141644375Skarels 	tcpstat.tcps_rttupdated++;
141744375Skarels 	if (tp->t_srtt != 0) {
141844375Skarels 		/*
141944375Skarels 		 * srtt is stored as fixed point with 3 bits after the
142044375Skarels 		 * binary point (i.e., scaled by 8).  The following magic
142144375Skarels 		 * is equivalent to the smoothing algorithm in rfc793 with
142244375Skarels 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
142357433Sandrew 		 * point).  Adjust rtt to origin 0.
142444375Skarels 		 */
142557433Sandrew 		delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT);
142644375Skarels 		if ((tp->t_srtt += delta) <= 0)
142744375Skarels 			tp->t_srtt = 1;
142844375Skarels 		/*
142944375Skarels 		 * We accumulate a smoothed rtt variance (actually, a
143044375Skarels 		 * smoothed mean difference), then set the retransmit
143144375Skarels 		 * timer to smoothed rtt + 4 times the smoothed variance.
143244375Skarels 		 * rttvar is stored as fixed point with 2 bits after the
143344375Skarels 		 * binary point (scaled by 4).  The following is
143444375Skarels 		 * equivalent to rfc793 smoothing with an alpha of .75
143544375Skarels 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
143644375Skarels 		 * rfc793's wired-in beta.
143744375Skarels 		 */
143844375Skarels 		if (delta < 0)
143944375Skarels 			delta = -delta;
144044375Skarels 		delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
144144375Skarels 		if ((tp->t_rttvar += delta) <= 0)
144244375Skarels 			tp->t_rttvar = 1;
144344375Skarels 	} else {
144444375Skarels 		/*
144544375Skarels 		 * No rtt measurement yet - use the unsmoothed rtt.
144644375Skarels 		 * Set the variance to half the rtt (so our first
144752199Skarels 		 * retransmit happens at 3*rtt).
144844375Skarels 		 */
144957433Sandrew 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
145057433Sandrew 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
145144375Skarels 	}
145244375Skarels 	tp->t_rtt = 0;
145344375Skarels 	tp->t_rxtshift = 0;
145444375Skarels 
145544375Skarels 	/*
145644375Skarels 	 * the retransmit should happen at rtt + 4 * rttvar.
145744375Skarels 	 * Because of the way we do the smoothing, srtt and rttvar
145844375Skarels 	 * will each average +1/2 tick of bias.  When we compute
145944375Skarels 	 * the retransmit timer, we want 1/2 tick of rounding and
146044375Skarels 	 * 1 extra tick because of +-1/2 tick uncertainty in the
146144375Skarels 	 * firing of the timer.  The bias will give us exactly the
146244375Skarels 	 * 1.5 tick we need.  But, because the bias is
146344375Skarels 	 * statistical, we have to test that we don't drop below
146444375Skarels 	 * the minimum feasible timer (which is 2 ticks).
146544375Skarels 	 */
146644375Skarels 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
146744375Skarels 	    tp->t_rttmin, TCPTV_REXMTMAX);
146844375Skarels 
146944375Skarels 	/*
147044375Skarels 	 * We received an ack for a packet that wasn't retransmitted;
147144375Skarels 	 * it is probably safe to discard any error indications we've
147244375Skarels 	 * received recently.  This isn't quite right, but close enough
147344375Skarels 	 * for now (a route might have failed after we sent a segment,
147444375Skarels 	 * and the return path might not be symmetrical).
147544375Skarels 	 */
147644375Skarels 	tp->t_softerror = 0;
147744375Skarels }
147844375Skarels 
147944375Skarels /*
148044375Skarels  * Determine a reasonable value for maxseg size.
148144375Skarels  * If the route is known, check route for mtu.
148244375Skarels  * If none, use an mss that can be handled on the outgoing
148344375Skarels  * interface without forcing IP to fragment; if bigger than
148444375Skarels  * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
148544375Skarels  * to utilize large mbufs.  If no route is found, route has no mtu,
148644375Skarels  * or the destination isn't local, use a default, hopefully conservative
148744375Skarels  * size (usually 512 or the default IP max size, but no more than the mtu
148844375Skarels  * of the interface), as we can't discover anything about intervening
148944375Skarels  * gateways or networks.  We also initialize the congestion/slow start
149044375Skarels  * window to be a single segment if the destination isn't local.
149144375Skarels  * While looking at the routing entry, we also initialize other path-dependent
149244375Skarels  * parameters from pre-set or cached values in the routing entry.
149344375Skarels  */
149461335Sbostic int
149544375Skarels tcp_mss(tp, offer)
149644375Skarels 	register struct tcpcb *tp;
149765606Sbostic 	u_int offer;
149844375Skarels {
149917272Skarels 	struct route *ro;
150044375Skarels 	register struct rtentry *rt;
150117272Skarels 	struct ifnet *ifp;
150244375Skarels 	register int rtt, mss;
150344375Skarels 	u_long bufsize;
150417272Skarels 	struct inpcb *inp;
150544375Skarels 	struct socket *so;
150665460Sbostic 	extern int tcp_mssdflt;
150717272Skarels 
150817272Skarels 	inp = tp->t_inpcb;
150917272Skarels 	ro = &inp->inp_route;
151044375Skarels 
151144375Skarels 	if ((rt = ro->ro_rt) == (struct rtentry *)0) {
151217272Skarels 		/* No route yet, so try to acquire one */
151317272Skarels 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
151417272Skarels 			ro->ro_dst.sa_family = AF_INET;
151537320Skarels 			ro->ro_dst.sa_len = sizeof(ro->ro_dst);
151617272Skarels 			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
151717272Skarels 				inp->inp_faddr;
151817272Skarels 			rtalloc(ro);
151917272Skarels 		}
152044375Skarels 		if ((rt = ro->ro_rt) == (struct rtentry *)0)
152157637Sandrew 			return (tcp_mssdflt);
152217272Skarels 	}
152344375Skarels 	ifp = rt->rt_ifp;
152444375Skarels 	so = inp->inp_socket;
152517272Skarels 
152644375Skarels #ifdef RTV_MTU	/* if route characteristics exist ... */
152744375Skarels 	/*
152844375Skarels 	 * While we're here, check if there's an initial rtt
152944375Skarels 	 * or rttvar.  Convert from the route-table units
153044375Skarels 	 * to scaled multiples of the slow timeout timer.
153144375Skarels 	 */
153244375Skarels 	if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
153352199Skarels 		/*
153452199Skarels 		 * XXX the lock bit for MTU indicates that the value
153552199Skarels 		 * is also a minimum value; this is subject to time.
153652199Skarels 		 */
153752199Skarels 		if (rt->rt_rmx.rmx_locks & RTV_RTT)
153844375Skarels 			tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ);
153944375Skarels 		tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
154044375Skarels 		if (rt->rt_rmx.rmx_rttvar)
154144375Skarels 			tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
154244375Skarels 			    (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
154344375Skarels 		else
154444375Skarels 			/* default variation is +- 1 rtt */
154544375Skarels 			tp->t_rttvar =
154644375Skarels 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
154744375Skarels 		TCPT_RANGESET(tp->t_rxtcur,
154844375Skarels 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
154944375Skarels 		    tp->t_rttmin, TCPTV_REXMTMAX);
155044375Skarels 	}
155144375Skarels 	/*
155244375Skarels 	 * if there's an mtu associated with the route, use it
155344375Skarels 	 */
155444375Skarels 	if (rt->rt_rmx.rmx_mtu)
155557637Sandrew 		mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
155644375Skarels 	else
155744375Skarels #endif /* RTV_MTU */
155844375Skarels 	{
155957637Sandrew 		mss = ifp->if_mtu - sizeof(struct tcpiphdr);
156031726Skarels #if	(MCLBYTES & (MCLBYTES - 1)) == 0
156144375Skarels 		if (mss > MCLBYTES)
156244375Skarels 			mss &= ~(MCLBYTES-1);
156317272Skarels #else
156444375Skarels 		if (mss > MCLBYTES)
156544375Skarels 			mss = mss / MCLBYTES * MCLBYTES;
156617272Skarels #endif
156744375Skarels 		if (!in_localaddr(inp->inp_faddr))
156857637Sandrew 			mss = min(mss, tcp_mssdflt);
156944375Skarels 	}
157044375Skarels 	/*
157144375Skarels 	 * The current mss, t_maxseg, is initialized to the default value.
157244375Skarels 	 * If we compute a smaller value, reduce the current mss.
157344375Skarels 	 * If we compute a larger value, return it for use in sending
157444375Skarels 	 * a max seg size option, but don't store it for use
157544375Skarels 	 * unless we received an offer at least that large from peer.
157644375Skarels 	 * However, do not accept offers under 32 bytes.
157744375Skarels 	 */
157844375Skarels 	if (offer)
157944375Skarels 		mss = min(mss, offer);
158044375Skarels 	mss = max(mss, 32);		/* sanity */
158144375Skarels 	if (mss < tp->t_maxseg || offer != 0) {
158244375Skarels 		/*
158344375Skarels 		 * If there's a pipesize, change the socket buffer
158444375Skarels 		 * to that size.  Make the socket buffers an integral
158544375Skarels 		 * number of mss units; if the mss is larger than
158644375Skarels 		 * the socket buffer, decrease the mss.
158744375Skarels 		 */
158844375Skarels #ifdef RTV_SPIPE
158944375Skarels 		if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
159044375Skarels #endif
159144375Skarels 			bufsize = so->so_snd.sb_hiwat;
159244375Skarels 		if (bufsize < mss)
159344375Skarels 			mss = bufsize;
159444375Skarels 		else {
159556908Storek 			bufsize = roundup(bufsize, mss);
159657433Sandrew 			if (bufsize > sb_max)
159757433Sandrew 				bufsize = sb_max;
159856908Storek 			(void)sbreserve(&so->so_snd, bufsize);
159944375Skarels 		}
160044375Skarels 		tp->t_maxseg = mss;
160132098Skarels 
160244375Skarels #ifdef RTV_RPIPE
160344375Skarels 		if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
160444375Skarels #endif
160544375Skarels 			bufsize = so->so_rcv.sb_hiwat;
160644375Skarels 		if (bufsize > mss) {
160756908Storek 			bufsize = roundup(bufsize, mss);
160857433Sandrew 			if (bufsize > sb_max)
160957433Sandrew 				bufsize = sb_max;
161056908Storek 			(void)sbreserve(&so->so_rcv, bufsize);
161144375Skarels 		}
161244375Skarels 	}
161332034Skarels 	tp->snd_cwnd = mss;
161444375Skarels 
161544375Skarels #ifdef RTV_SSTHRESH
161644375Skarels 	if (rt->rt_rmx.rmx_ssthresh) {
161744375Skarels 		/*
161844375Skarels 		 * There's some sort of gateway or interface
161944375Skarels 		 * buffer limit on the path.  Use this to set
162044375Skarels 		 * the slow start threshhold, but set the
162144375Skarels 		 * threshold to no less than 2*mss.
162244375Skarels 		 */
162344375Skarels 		tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
162444375Skarels 	}
162544375Skarels #endif /* RTV_MTU */
162632034Skarels 	return (mss);
162717272Skarels }
162857947Ssklower #endif /* TUBA_INCLUDE */
1629