xref: /csrg-svn/sys/netinet/tcp_input.c (revision 58998)
123190Smckusick /*
244375Skarels  * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California.
332787Sbostic  * All rights reserved.
423190Smckusick  *
544485Sbostic  * %sccs.include.redist.c%
632787Sbostic  *
7*58998Ssklower  *	@(#)tcp_input.c	7.35 (Berkeley) 04/07/93
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;
3744375Skarels int	tcppredack;	/* XXX debugging: times hdr predict ok for acks */
3844375Skarels int	tcppreddat;	/* XXX # times header prediction ok for data packets */
3944375Skarels int	tcppcbcachemiss;
405267Sroot struct	tcpiphdr tcp_saveti;
4144375Skarels struct	inpcb *tcp_last_inpcb = &tcb;
424601Swnj 
4357947Ssklower struct	tcpcb *tcp_newtcpcb();
4457947Ssklower 
4557947Ssklower extern u_long sb_max;
4657947Ssklower 
4757947Ssklower #endif /* TUBA_INCLUDE */
4857433Sandrew #define TCP_PAWS_IDLE	(24 * 24 * 60 * 60 * PR_SLOWHZ)
4957433Sandrew 
5057433Sandrew /* for modulo comparisons of timestamps */
5157433Sandrew #define TSTMP_LT(a,b)	((int)((a)-(b)) < 0)
5257433Sandrew #define TSTMP_GEQ(a,b)	((int)((a)-(b)) >= 0)
5357433Sandrew 
5424816Skarels 
555065Swnj /*
5624816Skarels  * Insert segment ti into reassembly queue of tcp with
5724816Skarels  * control block tp.  Return TH_FIN if reassembly now includes
5824816Skarels  * a segment with FIN.  The macro form does the common case inline
5924816Skarels  * (segment is the next to be received on an established connection,
6024816Skarels  * and the queue is empty), avoiding linkage into and removal
6124816Skarels  * from the queue and repetition of various conversions.
6234278Skarels  * Set DELACK for segments received in order, but ack immediately
6334278Skarels  * when segments are out of order (so fast retransmit can work).
6424816Skarels  */
6524816Skarels #define	TCP_REASS(tp, ti, m, so, flags) { \
6624816Skarels 	if ((ti)->ti_seq == (tp)->rcv_nxt && \
6724816Skarels 	    (tp)->seg_next == (struct tcpiphdr *)(tp) && \
6824816Skarels 	    (tp)->t_state == TCPS_ESTABLISHED) { \
6934278Skarels 		tp->t_flags |= TF_DELACK; \
7024816Skarels 		(tp)->rcv_nxt += (ti)->ti_len; \
7124816Skarels 		flags = (ti)->ti_flags & TH_FIN; \
7230525Skarels 		tcpstat.tcps_rcvpack++;\
7330525Skarels 		tcpstat.tcps_rcvbyte += (ti)->ti_len;\
7424816Skarels 		sbappend(&(so)->so_rcv, (m)); \
7524816Skarels 		sorwakeup(so); \
7634278Skarels 	} else { \
7744375Skarels 		(flags) = tcp_reass((tp), (ti), (m)); \
7834278Skarels 		tp->t_flags |= TF_ACKNOW; \
7934278Skarels 	} \
8024816Skarels }
8157947Ssklower #ifndef TUBA_INCLUDE
8224816Skarels 
8344375Skarels tcp_reass(tp, ti, m)
8424816Skarels 	register struct tcpcb *tp;
8524816Skarels 	register struct tcpiphdr *ti;
8644375Skarels 	struct mbuf *m;
8724816Skarels {
8824816Skarels 	register struct tcpiphdr *q;
8924816Skarels 	struct socket *so = tp->t_inpcb->inp_socket;
9024816Skarels 	int flags;
9124816Skarels 
9224816Skarels 	/*
9324816Skarels 	 * Call with ti==0 after become established to
9424816Skarels 	 * force pre-ESTABLISHED data up to user socket.
9524816Skarels 	 */
9624816Skarels 	if (ti == 0)
9724816Skarels 		goto present;
9824816Skarels 
9924816Skarels 	/*
10024816Skarels 	 * Find a segment which begins after this one does.
10124816Skarels 	 */
10224816Skarels 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
10324816Skarels 	    q = (struct tcpiphdr *)q->ti_next)
10424816Skarels 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
10524816Skarels 			break;
10624816Skarels 
10724816Skarels 	/*
10824816Skarels 	 * If there is a preceding segment, it may provide some of
10924816Skarels 	 * our data already.  If so, drop the data from the incoming
11024816Skarels 	 * segment.  If it provides all of our data, drop us.
11124816Skarels 	 */
11224816Skarels 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
11324816Skarels 		register int i;
11424816Skarels 		q = (struct tcpiphdr *)q->ti_prev;
11524816Skarels 		/* conversion to int (in i) handles seq wraparound */
11624816Skarels 		i = q->ti_seq + q->ti_len - ti->ti_seq;
11724816Skarels 		if (i > 0) {
11830525Skarels 			if (i >= ti->ti_len) {
11930525Skarels 				tcpstat.tcps_rcvduppack++;
12030525Skarels 				tcpstat.tcps_rcvdupbyte += ti->ti_len;
12144375Skarels 				m_freem(m);
12244375Skarels 				return (0);
12330525Skarels 			}
12444375Skarels 			m_adj(m, i);
12524816Skarels 			ti->ti_len -= i;
12624816Skarels 			ti->ti_seq += i;
12724816Skarels 		}
12824816Skarels 		q = (struct tcpiphdr *)(q->ti_next);
12924816Skarels 	}
13030525Skarels 	tcpstat.tcps_rcvoopack++;
13130525Skarels 	tcpstat.tcps_rcvoobyte += ti->ti_len;
13244375Skarels 	REASS_MBUF(ti) = m;		/* XXX */
13324816Skarels 
13424816Skarels 	/*
13524816Skarels 	 * While we overlap succeeding segments trim them or,
13624816Skarels 	 * if they are completely covered, dequeue them.
13724816Skarels 	 */
13824816Skarels 	while (q != (struct tcpiphdr *)tp) {
13924816Skarels 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
14024816Skarels 		if (i <= 0)
14124816Skarels 			break;
14224816Skarels 		if (i < q->ti_len) {
14324816Skarels 			q->ti_seq += i;
14424816Skarels 			q->ti_len -= i;
14544375Skarels 			m_adj(REASS_MBUF(q), i);
14624816Skarels 			break;
14724816Skarels 		}
14824816Skarels 		q = (struct tcpiphdr *)q->ti_next;
14944375Skarels 		m = REASS_MBUF((struct tcpiphdr *)q->ti_prev);
15024816Skarels 		remque(q->ti_prev);
15124816Skarels 		m_freem(m);
15224816Skarels 	}
15324816Skarels 
15424816Skarels 	/*
15524816Skarels 	 * Stick new segment in its place.
15624816Skarels 	 */
15724816Skarels 	insque(ti, q->ti_prev);
15824816Skarels 
15924816Skarels present:
16024816Skarels 	/*
16124816Skarels 	 * Present data to user, advancing rcv_nxt through
16224816Skarels 	 * completed sequence space.
16324816Skarels 	 */
16424816Skarels 	if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
16524816Skarels 		return (0);
16624816Skarels 	ti = tp->seg_next;
16724816Skarels 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
16824816Skarels 		return (0);
16924816Skarels 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
17024816Skarels 		return (0);
17124816Skarels 	do {
17224816Skarels 		tp->rcv_nxt += ti->ti_len;
17324816Skarels 		flags = ti->ti_flags & TH_FIN;
17424816Skarels 		remque(ti);
17544375Skarels 		m = REASS_MBUF(ti);
17624816Skarels 		ti = (struct tcpiphdr *)ti->ti_next;
17724816Skarels 		if (so->so_state & SS_CANTRCVMORE)
17824816Skarels 			m_freem(m);
17924816Skarels 		else
18024816Skarels 			sbappend(&so->so_rcv, m);
18124816Skarels 	} while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
18224816Skarels 	sorwakeup(so);
18324816Skarels 	return (flags);
18424816Skarels }
18524816Skarels 
18624816Skarels /*
1875065Swnj  * TCP input routine, follows pages 65-76 of the
1885065Swnj  * protocol specification dated September, 1981 very closely.
1895065Swnj  */
19037320Skarels tcp_input(m, iphlen)
19137320Skarels 	register struct mbuf *m;
19237320Skarels 	int iphlen;
1934601Swnj {
1944924Swnj 	register struct tcpiphdr *ti;
19544375Skarels 	register struct inpcb *inp;
19657433Sandrew 	caddr_t optp = NULL;
19757433Sandrew 	int optlen;
1984924Swnj 	int len, tlen, off;
1995391Swnj 	register struct tcpcb *tp = 0;
2004924Swnj 	register int tiflags;
2014803Swnj 	struct socket *so;
20231721Skarels 	int todrop, acked, ourfinisacked, needoutput = 0;
2035267Sroot 	short ostate;
2046028Sroot 	struct in_addr laddr;
20510769Ssam 	int dropsocket = 0;
20630525Skarels 	int iss = 0;
20757433Sandrew 	u_long tiwin, ts_val, ts_ecr;
20857433Sandrew 	int ts_present = 0;
2094924Swnj 
21030525Skarels 	tcpstat.tcps_rcvtotal++;
2114924Swnj 	/*
2125244Sroot 	 * Get IP and TCP header together in first mbuf.
2135244Sroot 	 * Note: IP leaves IP header in first mbuf.
2144924Swnj 	 */
2155020Sroot 	ti = mtod(m, struct tcpiphdr *);
21637320Skarels 	if (iphlen > sizeof (struct ip))
21737320Skarels 		ip_stripoptions(m, (struct mbuf *)0);
21844375Skarels 	if (m->m_len < sizeof (struct tcpiphdr)) {
2195307Sroot 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
22030525Skarels 			tcpstat.tcps_rcvshort++;
2215307Sroot 			return;
2225085Swnj 		}
2235085Swnj 		ti = mtod(m, struct tcpiphdr *);
2245085Swnj 	}
2254601Swnj 
2264601Swnj 	/*
2275244Sroot 	 * Checksum extended TCP header and data.
2284601Swnj 	 */
2294924Swnj 	tlen = ((struct ip *)ti)->ip_len;
2304924Swnj 	len = sizeof (struct ip) + tlen;
23137320Skarels 	ti->ti_next = ti->ti_prev = 0;
23237320Skarels 	ti->ti_x1 = 0;
23337320Skarels 	ti->ti_len = (u_short)tlen;
23444375Skarels 	HTONS(ti->ti_len);
23537320Skarels 	if (ti->ti_sum = in_cksum(m, len)) {
23637320Skarels 		tcpstat.tcps_rcvbadsum++;
23737320Skarels 		goto drop;
2384601Swnj 	}
23957947Ssklower #endif /* TUBA_INCLUDE */
2404601Swnj 
2414601Swnj 	/*
2425244Sroot 	 * Check that TCP offset makes sense,
24344375Skarels 	 * pull out TCP options and adjust length.		XXX
2444601Swnj 	 */
2454924Swnj 	off = ti->ti_off << 2;
2465231Swnj 	if (off < sizeof (struct tcphdr) || off > tlen) {
24730525Skarels 		tcpstat.tcps_rcvbadoff++;
2485085Swnj 		goto drop;
2494924Swnj 	}
2506211Swnj 	tlen -= off;
2516211Swnj 	ti->ti_len = tlen;
2525440Swnj 	if (off > sizeof (struct tcphdr)) {
25324816Skarels 		if (m->m_len < sizeof(struct ip) + off) {
25424816Skarels 			if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
25530525Skarels 				tcpstat.tcps_rcvshort++;
25624816Skarels 				return;
25724816Skarels 			}
25824816Skarels 			ti = mtod(m, struct tcpiphdr *);
2595440Swnj 		}
26057433Sandrew 		optlen = off - sizeof (struct tcphdr);
26157433Sandrew 		optp = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
26257433Sandrew 		/*
26357433Sandrew 		 * Do quick retrieval of timestamp options ("options
26457433Sandrew 		 * prediction?").  If timestamp is the only option and it's
26557433Sandrew 		 * formatted as recommended in RFC 1323 appendix A, we
26657433Sandrew 		 * quickly get the values now and not bother calling
26757433Sandrew 		 * tcp_dooptions(), etc.
26857433Sandrew 		 */
26957433Sandrew 		if ((optlen == TCPOLEN_TSTAMP_APPA ||
27057433Sandrew 		     (optlen > TCPOLEN_TSTAMP_APPA &&
27157433Sandrew 			optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
27257433Sandrew 		     *(u_long *)optp == htonl(TCPOPT_TSTAMP_HDR) &&
27357433Sandrew 		     (ti->ti_flags & TH_SYN) == 0) {
27457433Sandrew 			ts_present = 1;
27557433Sandrew 			ts_val = ntohl(*(u_long *)(optp + 4));
27657433Sandrew 			ts_ecr = ntohl(*(u_long *)(optp + 8));
27757433Sandrew 			optp = NULL;	/* we've parsed the options */
2785440Swnj 		}
2795440Swnj 	}
2805065Swnj 	tiflags = ti->ti_flags;
2814924Swnj 
2826093Sroot 	/*
2835244Sroot 	 * Convert TCP protocol specific fields to host format.
2845085Swnj 	 */
28544375Skarels 	NTOHL(ti->ti_seq);
28644375Skarels 	NTOHL(ti->ti_ack);
28744375Skarels 	NTOHS(ti->ti_win);
28844375Skarels 	NTOHS(ti->ti_urp);
2895085Swnj 
2905085Swnj 	/*
2918271Sroot 	 * Locate pcb for segment.
2924924Swnj 	 */
29330525Skarels findpcb:
29444375Skarels 	inp = tcp_last_inpcb;
29544375Skarels 	if (inp->inp_lport != ti->ti_dport ||
29644375Skarels 	    inp->inp_fport != ti->ti_sport ||
29744375Skarels 	    inp->inp_faddr.s_addr != ti->ti_src.s_addr ||
29844375Skarels 	    inp->inp_laddr.s_addr != ti->ti_dst.s_addr) {
29944375Skarels 		inp = in_pcblookup(&tcb, ti->ti_src, ti->ti_sport,
30044375Skarels 		    ti->ti_dst, ti->ti_dport, INPLOOKUP_WILDCARD);
30144375Skarels 		if (inp)
30244375Skarels 			tcp_last_inpcb = inp;
30344375Skarels 		++tcppcbcachemiss;
30444375Skarels 	}
3055065Swnj 
3065065Swnj 	/*
3075065Swnj 	 * If the state is CLOSED (i.e., TCB does not exist) then
3085244Sroot 	 * all data in the incoming segment is discarded.
30932098Skarels 	 * If the TCB exists but is in CLOSED state, it is embryonic,
31032098Skarels 	 * but should either do a listen or a connect soon.
3115065Swnj 	 */
3125300Sroot 	if (inp == 0)
3135085Swnj 		goto dropwithreset;
3145065Swnj 	tp = intotcpcb(inp);
3155300Sroot 	if (tp == 0)
3165085Swnj 		goto dropwithreset;
31732098Skarels 	if (tp->t_state == TCPS_CLOSED)
31832098Skarels 		goto drop;
31957433Sandrew 
32057433Sandrew 	/* Unscale the window into a 32-bit value. */
32157433Sandrew 	if ((tiflags & TH_SYN) == 0)
32257433Sandrew 		tiwin = ti->ti_win << tp->snd_scale;
32357433Sandrew 	else
32457433Sandrew 		tiwin = ti->ti_win;
32557433Sandrew 
3265109Swnj 	so = inp->inp_socket;
32744375Skarels 	if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
32844375Skarels 		if (so->so_options & SO_DEBUG) {
32944375Skarels 			ostate = tp->t_state;
33044375Skarels 			tcp_saveti = *ti;
33144375Skarels 		}
33244375Skarels 		if (so->so_options & SO_ACCEPTCONN) {
33344375Skarels 			so = sonewconn(so, 0);
33444375Skarels 			if (so == 0)
33544375Skarels 				goto drop;
33644375Skarels 			/*
33744375Skarels 			 * This is ugly, but ....
33844375Skarels 			 *
33944375Skarels 			 * Mark socket as temporary until we're
34044375Skarels 			 * committed to keeping it.  The code at
34144375Skarels 			 * ``drop'' and ``dropwithreset'' check the
34244375Skarels 			 * flag dropsocket to see if the temporary
34344375Skarels 			 * socket created here should be discarded.
34444375Skarels 			 * We mark the socket as discardable until
34544375Skarels 			 * we're committed to it below in TCPS_LISTEN.
34644375Skarels 			 */
34744375Skarels 			dropsocket++;
34844375Skarels 			inp = (struct inpcb *)so->so_pcb;
34944375Skarels 			inp->inp_laddr = ti->ti_dst;
35044375Skarels 			inp->inp_lport = ti->ti_dport;
35144375Skarels #if BSD>=43
35244375Skarels 			inp->inp_options = ip_srcroute();
35344375Skarels #endif
35444375Skarels 			tp = intotcpcb(inp);
35544375Skarels 			tp->t_state = TCPS_LISTEN;
35657433Sandrew 
35757433Sandrew 			/* Compute proper scaling value from buffer space
35857433Sandrew 			 */
35957433Sandrew 			while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
36057433Sandrew 			   TCP_MAXWIN << tp->request_r_scale < so->so_rcv.sb_hiwat)
36157433Sandrew 				tp->request_r_scale++;
36244375Skarels 		}
3635267Sroot 	}
3644601Swnj 
3654601Swnj 	/*
3665162Swnj 	 * Segment received on connection.
3675162Swnj 	 * Reset idle time and keep-alive timer.
3685162Swnj 	 */
3695162Swnj 	tp->t_idle = 0;
37033745Skarels 	tp->t_timer[TCPT_KEEP] = tcp_keepidle;
3715162Swnj 
3725162Swnj 	/*
37317272Skarels 	 * Process options if not in LISTEN state,
37417272Skarels 	 * else do it below (after getting remote address).
3755440Swnj 	 */
37657433Sandrew 	if (optp && tp->t_state != TCPS_LISTEN)
37757433Sandrew 		tcp_dooptions(tp, optp, optlen, ti,
37857433Sandrew 			&ts_present, &ts_val, &ts_ecr);
37957433Sandrew 
38044375Skarels 	/*
38144375Skarels 	 * Header prediction: check for the two common cases
38244375Skarels 	 * of a uni-directional data xfer.  If the packet has
38344375Skarels 	 * no control flags, is in-sequence, the window didn't
38444375Skarels 	 * change and we're not retransmitting, it's a
38544375Skarels 	 * candidate.  If the length is zero and the ack moved
38644375Skarels 	 * forward, we're the sender side of the xfer.  Just
38744375Skarels 	 * free the data acked & wake any higher level process
38844375Skarels 	 * that was blocked waiting for space.  If the length
38944375Skarels 	 * is non-zero and the ack didn't move, we're the
39044375Skarels 	 * receiver side.  If we're getting packets in-order
39144375Skarels 	 * (the reassembly queue is empty), add the data to
39244375Skarels 	 * the socket buffer and note that we need a delayed ack.
39344375Skarels 	 */
39444375Skarels 	if (tp->t_state == TCPS_ESTABLISHED &&
39544375Skarels 	    (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
39657433Sandrew 	    (!ts_present || TSTMP_GEQ(ts_val, tp->ts_recent)) &&
39744375Skarels 	    ti->ti_seq == tp->rcv_nxt &&
39857433Sandrew 	    tiwin && tiwin == tp->snd_wnd &&
39944375Skarels 	    tp->snd_nxt == tp->snd_max) {
40057433Sandrew 
40157433Sandrew 		/*
40257433Sandrew 		 * If last ACK falls within this segment's sequence numbers,
40357433Sandrew 		 *  record the timestamp.
40457433Sandrew 		 */
40557433Sandrew 		if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
40657433Sandrew 		   SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len)) {
40757433Sandrew 			tp->ts_recent_age = tcp_now;
40857433Sandrew 			tp->ts_recent = ts_val;
40957433Sandrew 		}
41057433Sandrew 
41144375Skarels 		if (ti->ti_len == 0) {
41244375Skarels 			if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
41344375Skarels 			    SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
41444375Skarels 			    tp->snd_cwnd >= tp->snd_wnd) {
41544375Skarels 				/*
41644375Skarels 				 * this is a pure ack for outstanding data.
41744375Skarels 				 */
41844375Skarels 				++tcppredack;
41957433Sandrew 				if (ts_present)
42057433Sandrew 					tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
42157433Sandrew 				else if (tp->t_rtt &&
42257433Sandrew 					    SEQ_GT(ti->ti_ack, tp->t_rtseq))
42357433Sandrew 					tcp_xmit_timer(tp, tp->t_rtt);
42444375Skarels 				acked = ti->ti_ack - tp->snd_una;
42544375Skarels 				tcpstat.tcps_rcvackpack++;
42644375Skarels 				tcpstat.tcps_rcvackbyte += acked;
42744375Skarels 				sbdrop(&so->so_snd, acked);
42844375Skarels 				tp->snd_una = ti->ti_ack;
42944375Skarels 				m_freem(m);
4305440Swnj 
43144375Skarels 				/*
43244375Skarels 				 * If all outstanding data are acked, stop
43344375Skarels 				 * retransmit timer, otherwise restart timer
43444375Skarels 				 * using current (possibly backed-off) value.
43544375Skarels 				 * If process is waiting for space,
43644375Skarels 				 * wakeup/selwakeup/signal.  If data
43744375Skarels 				 * are ready to send, let tcp_output
43844375Skarels 				 * decide between more output or persist.
43944375Skarels 				 */
44044375Skarels 				if (tp->snd_una == tp->snd_max)
44144375Skarels 					tp->t_timer[TCPT_REXMT] = 0;
44244375Skarels 				else if (tp->t_timer[TCPT_PERSIST] == 0)
44344375Skarels 					tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
44444375Skarels 
44544375Skarels 				if (so->so_snd.sb_flags & SB_NOTIFY)
44644375Skarels 					sowwakeup(so);
44744375Skarels 				if (so->so_snd.sb_cc)
44844375Skarels 					(void) tcp_output(tp);
44944375Skarels 				return;
45044375Skarels 			}
45144375Skarels 		} else if (ti->ti_ack == tp->snd_una &&
45244375Skarels 		    tp->seg_next == (struct tcpiphdr *)tp &&
45344375Skarels 		    ti->ti_len <= sbspace(&so->so_rcv)) {
45444375Skarels 			/*
45544375Skarels 			 * this is a pure, in-sequence data packet
45644375Skarels 			 * with nothing on the reassembly queue and
45744375Skarels 			 * we have enough buffer space to take it.
45844375Skarels 			 */
45944375Skarels 			++tcppreddat;
46044375Skarels 			tp->rcv_nxt += ti->ti_len;
46144375Skarels 			tcpstat.tcps_rcvpack++;
46244375Skarels 			tcpstat.tcps_rcvbyte += ti->ti_len;
46344375Skarels 			/*
46457433Sandrew 			 * Drop TCP, IP headers and TCP options then add data
46557433Sandrew 			 * to socket buffer.
46644375Skarels 			 */
46757433Sandrew 			m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
46857433Sandrew 			m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
46944375Skarels 			sbappend(&so->so_rcv, m);
47044375Skarels 			sorwakeup(so);
47144375Skarels 			tp->t_flags |= TF_DELACK;
47244375Skarels 			return;
47344375Skarels 		}
47444375Skarels 	}
47544375Skarels 
4765440Swnj 	/*
47757433Sandrew 	 * Drop TCP, IP headers and TCP options.
47844375Skarels 	 */
47957433Sandrew 	m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
48057433Sandrew 	m->m_len  -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
48144375Skarels 
48244375Skarels 	/*
4835085Swnj 	 * Calculate amount of space in receive window,
4845085Swnj 	 * and then do TCP input processing.
48524816Skarels 	 * Receive window is amount of space in rcv queue,
48624816Skarels 	 * but not less than advertised window.
4874601Swnj 	 */
48825939Skarels 	{ int win;
4894601Swnj 
49025939Skarels 	win = sbspace(&so->so_rcv);
49125939Skarels 	if (win < 0)
49225939Skarels 		win = 0;
49337320Skarels 	tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt));
49425939Skarels 	}
49525939Skarels 
4964601Swnj 	switch (tp->t_state) {
4974601Swnj 
4985065Swnj 	/*
4995065Swnj 	 * If the state is LISTEN then ignore segment if it contains an RST.
5005065Swnj 	 * If the segment contains an ACK then it is bad and send a RST.
5015065Swnj 	 * If it does not contain a SYN then it is not interesting; drop it.
50225197Skarels 	 * Don't bother responding if the destination was a broadcast.
5035085Swnj 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
5045065Swnj 	 * tp->iss, and send a segment:
5055085Swnj 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
5065065Swnj 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
5075065Swnj 	 * Fill in remote peer address fields if not previously specified.
5085065Swnj 	 * Enter SYN_RECEIVED state, and process any other fields of this
5095244Sroot 	 * segment in this state.
5105065Swnj 	 */
5118271Sroot 	case TCPS_LISTEN: {
51210145Ssam 		struct mbuf *am;
5138271Sroot 		register struct sockaddr_in *sin;
5148271Sroot 
5155065Swnj 		if (tiflags & TH_RST)
5165065Swnj 			goto drop;
5175300Sroot 		if (tiflags & TH_ACK)
5185085Swnj 			goto dropwithreset;
5195300Sroot 		if ((tiflags & TH_SYN) == 0)
5205065Swnj 			goto drop;
521*58998Ssklower 		/*
522*58998Ssklower 		 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
523*58998Ssklower 		 * in_broadcast() should never return true on a received
524*58998Ssklower 		 * packet with M_BCAST not set.
525*58998Ssklower 		 */
52657433Sandrew 		if (m->m_flags & (M_BCAST|M_MCAST) ||
527*58998Ssklower 		    IN_MULTICAST(ti->ti_dst.s_addr))
52825197Skarels 			goto drop;
52944375Skarels 		am = m_get(M_DONTWAIT, MT_SONAME);	/* XXX */
53010145Ssam 		if (am == NULL)
53110145Ssam 			goto drop;
53210145Ssam 		am->m_len = sizeof (struct sockaddr_in);
5338599Sroot 		sin = mtod(am, struct sockaddr_in *);
5348271Sroot 		sin->sin_family = AF_INET;
53537320Skarels 		sin->sin_len = sizeof(*sin);
5368271Sroot 		sin->sin_addr = ti->ti_src;
5378271Sroot 		sin->sin_port = ti->ti_sport;
53856399Ssklower 		bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero));
5396028Sroot 		laddr = inp->inp_laddr;
54010145Ssam 		if (inp->inp_laddr.s_addr == INADDR_ANY)
5416028Sroot 			inp->inp_laddr = ti->ti_dst;
5428599Sroot 		if (in_pcbconnect(inp, am)) {
5436028Sroot 			inp->inp_laddr = laddr;
5448716Sroot 			(void) m_free(am);
5455244Sroot 			goto drop;
5466028Sroot 		}
5478716Sroot 		(void) m_free(am);
5485244Sroot 		tp->t_template = tcp_template(tp);
5495244Sroot 		if (tp->t_template == 0) {
55026386Skarels 			tp = tcp_drop(tp, ENOBUFS);
55117264Skarels 			dropsocket = 0;		/* socket is already gone */
5525244Sroot 			goto drop;
5535244Sroot 		}
55457433Sandrew 		if (optp)
55557433Sandrew 			tcp_dooptions(tp, optp, optlen, ti,
55657433Sandrew 				&ts_present, &ts_val, &ts_ecr);
55730525Skarels 		if (iss)
55830525Skarels 			tp->iss = iss;
55930525Skarels 		else
56030525Skarels 			tp->iss = tcp_iss;
56130525Skarels 		tcp_iss += TCP_ISSINCR/2;
5625065Swnj 		tp->irs = ti->ti_seq;
5635085Swnj 		tcp_sendseqinit(tp);
5645085Swnj 		tcp_rcvseqinit(tp);
56525939Skarels 		tp->t_flags |= TF_ACKNOW;
5665065Swnj 		tp->t_state = TCPS_SYN_RECEIVED;
56733745Skarels 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
56810769Ssam 		dropsocket = 0;		/* committed to socket */
56930525Skarels 		tcpstat.tcps_accepts++;
5705085Swnj 		goto trimthenstep6;
5718271Sroot 		}
5724601Swnj 
5735065Swnj 	/*
5745065Swnj 	 * If the state is SYN_SENT:
5755065Swnj 	 *	if seg contains an ACK, but not for our SYN, drop the input.
5765065Swnj 	 *	if seg contains a RST, then drop the connection.
5775065Swnj 	 *	if seg does not contain SYN, then drop it.
5785065Swnj 	 * Otherwise this is an acceptable SYN segment
5795065Swnj 	 *	initialize tp->rcv_nxt and tp->irs
5805065Swnj 	 *	if seg contains ack then advance tp->snd_una
5815065Swnj 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
5825065Swnj 	 *	arrange for segment to be acked (eventually)
5835065Swnj 	 *	continue processing rest of data/controls, beginning with URG
5845065Swnj 	 */
5855065Swnj 	case TCPS_SYN_SENT:
5865065Swnj 		if ((tiflags & TH_ACK) &&
58724816Skarels 		    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
5885231Swnj 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
5895085Swnj 			goto dropwithreset;
5905065Swnj 		if (tiflags & TH_RST) {
59110394Ssam 			if (tiflags & TH_ACK)
59210394Ssam 				tp = tcp_drop(tp, ECONNREFUSED);
5935065Swnj 			goto drop;
5944601Swnj 		}
5955065Swnj 		if ((tiflags & TH_SYN) == 0)
5965065Swnj 			goto drop;
59730974Skarels 		if (tiflags & TH_ACK) {
59830974Skarels 			tp->snd_una = ti->ti_ack;
59930974Skarels 			if (SEQ_LT(tp->snd_nxt, tp->snd_una))
60030974Skarels 				tp->snd_nxt = tp->snd_una;
60130974Skarels 		}
6025244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
6035065Swnj 		tp->irs = ti->ti_seq;
6045085Swnj 		tcp_rcvseqinit(tp);
6055085Swnj 		tp->t_flags |= TF_ACKNOW;
60630974Skarels 		if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
60730525Skarels 			tcpstat.tcps_connects++;
6085244Sroot 			soisconnected(so);
6095065Swnj 			tp->t_state = TCPS_ESTABLISHED;
61057433Sandrew 			/* Do window scaling on this connection? */
61157433Sandrew 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
61257433Sandrew 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
61357433Sandrew 				tp->snd_scale = tp->requested_s_scale;
61457433Sandrew 				tp->rcv_scale = tp->request_r_scale;
61557433Sandrew 			}
61644375Skarels 			(void) tcp_reass(tp, (struct tcpiphdr *)0,
61744375Skarels 				(struct mbuf *)0);
61832098Skarels 			/*
61932098Skarels 			 * if we didn't have to retransmit the SYN,
62032098Skarels 			 * use its rtt as our initial srtt & rtt var.
62132098Skarels 			 */
62244375Skarels 			if (tp->t_rtt)
62357433Sandrew 				tcp_xmit_timer(tp, tp->t_rtt);
6245162Swnj 		} else
6255085Swnj 			tp->t_state = TCPS_SYN_RECEIVED;
6265085Swnj 
6275085Swnj trimthenstep6:
6285085Swnj 		/*
62958774Smckusick 		 * Must not talk to ourselves.
63058774Smckusick 		 */
63158774Smckusick 		if (inp->inp_laddr.s_addr == inp->inp_faddr.s_addr &&
63258774Smckusick 		    inp->inp_lport == inp->inp_fport) {
63358774Smckusick 			dropsocket = 1; /* do an ECONNRESET */
63458774Smckusick 			goto dropwithreset;
63558774Smckusick 		}
63658774Smckusick 		/*
6375231Swnj 		 * Advance ti->ti_seq to correspond to first data byte.
6385085Swnj 		 * If data, trim to stay within window,
6395085Swnj 		 * dropping FIN if necessary.
6405085Swnj 		 */
6415231Swnj 		ti->ti_seq++;
6425085Swnj 		if (ti->ti_len > tp->rcv_wnd) {
6435085Swnj 			todrop = ti->ti_len - tp->rcv_wnd;
6445085Swnj 			m_adj(m, -todrop);
6455085Swnj 			ti->ti_len = tp->rcv_wnd;
64625939Skarels 			tiflags &= ~TH_FIN;
64730525Skarels 			tcpstat.tcps_rcvpackafterwin++;
64830525Skarels 			tcpstat.tcps_rcvbyteafterwin += todrop;
6495065Swnj 		}
6505263Swnj 		tp->snd_wl1 = ti->ti_seq - 1;
65125939Skarels 		tp->rcv_up = ti->ti_seq;
6525085Swnj 		goto step6;
6535065Swnj 	}
6544601Swnj 
6555065Swnj 	/*
65630525Skarels 	 * States other than LISTEN or SYN_SENT.
65757433Sandrew 	 * First check timestamp, if present.
65857433Sandrew 	 * Then check that at least some bytes of segment are within
65930525Skarels 	 * receive window.  If segment begins before rcv_nxt,
66030525Skarels 	 * drop leading data (and SYN); if nothing left, just ack.
66157433Sandrew 	 *
66257433Sandrew 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
66357433Sandrew 	 * and it's less than ts_recent, drop it.
66416222Skarels 	 */
66557433Sandrew 	if (ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent &&
66657433Sandrew 	    TSTMP_LT(ts_val, tp->ts_recent)) {
66757433Sandrew 
66857433Sandrew 		/* Check to see if ts_recent is over 24 days old.  */
66957433Sandrew 		if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
67057433Sandrew 			/*
67157433Sandrew 			 * Invalidate ts_recent.  If this segment updates
67257433Sandrew 			 * ts_recent, the age will be reset later and ts_recent
67357433Sandrew 			 * will get a valid value.  If it does not, setting
67457433Sandrew 			 * ts_recent to zero will at least satisfy the
67557433Sandrew 			 * requirement that zero be placed in the timestamp
67657433Sandrew 			 * echo reply when ts_recent isn't valid.  The
67757433Sandrew 			 * age isn't reset until we get a valid ts_recent
67857433Sandrew 			 * because we don't want out-of-order segments to be
67957433Sandrew 			 * dropped when ts_recent is old.
68057433Sandrew 			 */
68157433Sandrew 			tp->ts_recent = 0;
68257433Sandrew 		} else {
68357433Sandrew 			tcpstat.tcps_rcvduppack++;
68457433Sandrew 			tcpstat.tcps_rcvdupbyte += ti->ti_len;
68557433Sandrew 			tcpstat.tcps_pawsdrop++;
68657433Sandrew 			goto dropafterack;
68757433Sandrew 		}
68857433Sandrew 	}
68957433Sandrew 
69030525Skarels 	todrop = tp->rcv_nxt - ti->ti_seq;
69130525Skarels 	if (todrop > 0) {
69230525Skarels 		if (tiflags & TH_SYN) {
69330525Skarels 			tiflags &= ~TH_SYN;
69430525Skarels 			ti->ti_seq++;
69530525Skarels 			if (ti->ti_urp > 1)
69630525Skarels 				ti->ti_urp--;
69730525Skarels 			else
69830525Skarels 				tiflags &= ~TH_URG;
69930525Skarels 			todrop--;
70030525Skarels 		}
70130525Skarels 		if (todrop > ti->ti_len ||
70230525Skarels 		    todrop == ti->ti_len && (tiflags&TH_FIN) == 0) {
70333745Skarels 			tcpstat.tcps_rcvduppack++;
70433745Skarels 			tcpstat.tcps_rcvdupbyte += ti->ti_len;
70531730Skarels 			/*
70633745Skarels 			 * If segment is just one to the left of the window,
70733745Skarels 			 * check two special cases:
70833745Skarels 			 * 1. Don't toss RST in response to 4.2-style keepalive.
70933745Skarels 			 * 2. If the only thing to drop is a FIN, we can drop
71033745Skarels 			 *    it, but check the ACK or we will get into FIN
71133745Skarels 			 *    wars if our FINs crossed (both CLOSING).
71233745Skarels 			 * In either case, send ACK to resynchronize,
71333745Skarels 			 * but keep on processing for RST or ACK.
71431730Skarels 			 */
71533745Skarels 			if ((tiflags & TH_FIN && todrop == ti->ti_len + 1)
71633745Skarels #ifdef TCP_COMPAT_42
71733745Skarels 			  || (tiflags & TH_RST && ti->ti_seq == tp->rcv_nxt - 1)
71831730Skarels #endif
71933745Skarels 			   ) {
72033745Skarels 				todrop = ti->ti_len;
72133745Skarels 				tiflags &= ~TH_FIN;
72233745Skarels 				tp->t_flags |= TF_ACKNOW;
72357433Sandrew 			} else {
72457433Sandrew 				/*
72557433Sandrew 				 * Handle the case when a bound socket connects
72657433Sandrew 				 * to itself. Allow packets with a SYN and
72757433Sandrew 				 * an ACK to continue with the processing.
72857433Sandrew 				 */
72957433Sandrew 				if (todrop != 0 || (tiflags & TH_ACK) == 0)
73057433Sandrew 					goto dropafterack;
73157433Sandrew 			}
73232034Skarels 		} else {
73332034Skarels 			tcpstat.tcps_rcvpartduppack++;
73432034Skarels 			tcpstat.tcps_rcvpartdupbyte += todrop;
73530525Skarels 		}
73630525Skarels 		m_adj(m, todrop);
73730525Skarels 		ti->ti_seq += todrop;
73830525Skarels 		ti->ti_len -= todrop;
73930525Skarels 		if (ti->ti_urp > todrop)
74030525Skarels 			ti->ti_urp -= todrop;
74130525Skarels 		else {
74230525Skarels 			tiflags &= ~TH_URG;
74330525Skarels 			ti->ti_urp = 0;
74430525Skarels 		}
74516222Skarels 	}
74616222Skarels 
74732612Skarels 	/*
74833745Skarels 	 * If new data are received on a connection after the
74932612Skarels 	 * user processes are gone, then RST the other end.
75032612Skarels 	 */
75132612Skarels 	if ((so->so_state & SS_NOFDREF) &&
75232612Skarels 	    tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
75332612Skarels 		tp = tcp_close(tp);
75432612Skarels 		tcpstat.tcps_rcvafterclose++;
75532612Skarels 		goto dropwithreset;
75632612Skarels 	}
75732612Skarels 
75833445Skarels 	/*
75933445Skarels 	 * If segment ends after window, drop trailing data
76033445Skarels 	 * (and PUSH and FIN); if nothing left, just ACK.
76133445Skarels 	 */
76233445Skarels 	todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
76333445Skarels 	if (todrop > 0) {
76433445Skarels 		tcpstat.tcps_rcvpackafterwin++;
76533445Skarels 		if (todrop >= ti->ti_len) {
76630525Skarels 			tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
76733445Skarels 			/*
76833445Skarels 			 * If a new connection request is received
76933445Skarels 			 * while in TIME_WAIT, drop the old connection
77033445Skarels 			 * and start over if the sequence numbers
77133445Skarels 			 * are above the previous ones.
77233445Skarels 			 */
77333445Skarels 			if (tiflags & TH_SYN &&
77433445Skarels 			    tp->t_state == TCPS_TIME_WAIT &&
77533445Skarels 			    SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
77633445Skarels 				iss = tp->rcv_nxt + TCP_ISSINCR;
77744375Skarels 				tp = tcp_close(tp);
77833445Skarels 				goto findpcb;
77933445Skarels 			}
78033445Skarels 			/*
78133445Skarels 			 * If window is closed can only take segments at
78233445Skarels 			 * window edge, and have to drop data and PUSH from
78333445Skarels 			 * incoming segments.  Continue processing, but
78433445Skarels 			 * remember to ack.  Otherwise, drop segment
78533445Skarels 			 * and ack.
78633445Skarels 			 */
78733445Skarels 			if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
78833445Skarels 				tp->t_flags |= TF_ACKNOW;
78930525Skarels 				tcpstat.tcps_rcvwinprobe++;
79033445Skarels 			} else
7915065Swnj 				goto dropafterack;
79233445Skarels 		} else
79330525Skarels 			tcpstat.tcps_rcvbyteafterwin += todrop;
79433445Skarels 		m_adj(m, -todrop);
79533445Skarels 		ti->ti_len -= todrop;
79633445Skarels 		tiflags &= ~(TH_PUSH|TH_FIN);
7975065Swnj 	}
7984601Swnj 
7995065Swnj 	/*
80057433Sandrew 	 * If last ACK falls within this segment's sequence numbers,
80157433Sandrew 	 * record its timestamp.
80257433Sandrew 	 */
80357433Sandrew 	if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
80457433Sandrew 	    SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len +
80557433Sandrew 		   ((tiflags & (TH_SYN|TH_FIN)) != 0))) {
80657433Sandrew 		tp->ts_recent_age = tcp_now;
80757433Sandrew 		tp->ts_recent = ts_val;
80857433Sandrew 	}
80957433Sandrew 
81057433Sandrew 	/*
8115065Swnj 	 * If the RST bit is set examine the state:
8125065Swnj 	 *    SYN_RECEIVED STATE:
8135065Swnj 	 *	If passive open, return to LISTEN state.
8145065Swnj 	 *	If active open, inform user that connection was refused.
8155065Swnj 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
8165065Swnj 	 *	Inform user that connection was reset, and close tcb.
8175065Swnj 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
8185065Swnj 	 *	Close the tcb.
8195065Swnj 	 */
8205065Swnj 	if (tiflags&TH_RST) switch (tp->t_state) {
8215267Sroot 
8225065Swnj 	case TCPS_SYN_RECEIVED:
82333745Skarels 		so->so_error = ECONNREFUSED;
82433745Skarels 		goto close;
8254601Swnj 
8265065Swnj 	case TCPS_ESTABLISHED:
8275065Swnj 	case TCPS_FIN_WAIT_1:
8285065Swnj 	case TCPS_FIN_WAIT_2:
8295065Swnj 	case TCPS_CLOSE_WAIT:
83033745Skarels 		so->so_error = ECONNRESET;
83133745Skarels 	close:
83233745Skarels 		tp->t_state = TCPS_CLOSED;
83333745Skarels 		tcpstat.tcps_drops++;
83433745Skarels 		tp = tcp_close(tp);
8355065Swnj 		goto drop;
8365065Swnj 
8375065Swnj 	case TCPS_CLOSING:
8385065Swnj 	case TCPS_LAST_ACK:
8395065Swnj 	case TCPS_TIME_WAIT:
84010394Ssam 		tp = tcp_close(tp);
8415065Swnj 		goto drop;
8424601Swnj 	}
8434601Swnj 
8444601Swnj 	/*
8455065Swnj 	 * If a SYN is in the window, then this is an
8465065Swnj 	 * error and we send an RST and drop the connection.
8474601Swnj 	 */
8485065Swnj 	if (tiflags & TH_SYN) {
84910394Ssam 		tp = tcp_drop(tp, ECONNRESET);
8505085Swnj 		goto dropwithreset;
8514601Swnj 	}
8524601Swnj 
8534601Swnj 	/*
8545065Swnj 	 * If the ACK bit is off we drop the segment and return.
8554601Swnj 	 */
8565085Swnj 	if ((tiflags & TH_ACK) == 0)
8575065Swnj 		goto drop;
8585065Swnj 
8595065Swnj 	/*
8605065Swnj 	 * Ack processing.
8615065Swnj 	 */
8624601Swnj 	switch (tp->t_state) {
8634601Swnj 
8645065Swnj 	/*
8655065Swnj 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
86631721Skarels 	 * ESTABLISHED state and continue processing, otherwise
8675065Swnj 	 * send an RST.
8685065Swnj 	 */
8695065Swnj 	case TCPS_SYN_RECEIVED:
8705085Swnj 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
8715231Swnj 		    SEQ_GT(ti->ti_ack, tp->snd_max))
8725085Swnj 			goto dropwithreset;
87330525Skarels 		tcpstat.tcps_connects++;
8745085Swnj 		soisconnected(so);
8755085Swnj 		tp->t_state = TCPS_ESTABLISHED;
87657433Sandrew 		/* Do window scaling? */
87757433Sandrew 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
87857433Sandrew 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
87957433Sandrew 			tp->snd_scale = tp->requested_s_scale;
88057433Sandrew 			tp->rcv_scale = tp->request_r_scale;
88157433Sandrew 		}
88244375Skarels 		(void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0);
8835244Sroot 		tp->snd_wl1 = ti->ti_seq - 1;
8845085Swnj 		/* fall into ... */
8854601Swnj 
8865065Swnj 	/*
8875065Swnj 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
8885065Swnj 	 * ACKs.  If the ack is in the range
8895231Swnj 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
8905065Swnj 	 * then advance tp->snd_una to ti->ti_ack and drop
8915065Swnj 	 * data from the retransmission queue.  If this ACK reflects
8925065Swnj 	 * more up to date window information we update our window information.
8935065Swnj 	 */
8945065Swnj 	case TCPS_ESTABLISHED:
8955065Swnj 	case TCPS_FIN_WAIT_1:
8965065Swnj 	case TCPS_FIN_WAIT_2:
8975065Swnj 	case TCPS_CLOSE_WAIT:
8985065Swnj 	case TCPS_CLOSING:
8995244Sroot 	case TCPS_LAST_ACK:
9005244Sroot 	case TCPS_TIME_WAIT:
9015085Swnj 
90230525Skarels 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
90357433Sandrew 			if (ti->ti_len == 0 && tiwin == tp->snd_wnd) {
90430525Skarels 				tcpstat.tcps_rcvdupack++;
90532098Skarels 				/*
90644375Skarels 				 * If we have outstanding data (other than
90744375Skarels 				 * a window probe), this is a completely
90832098Skarels 				 * duplicate ack (ie, window info didn't
90932098Skarels 				 * change), the ack is the biggest we've
91032098Skarels 				 * seen and we've seen exactly our rexmt
91132098Skarels 				 * threshhold of them, assume a packet
91232098Skarels 				 * has been dropped and retransmit it.
91332098Skarels 				 * Kludge snd_nxt & the congestion
91432098Skarels 				 * window so we send only this one
91544375Skarels 				 * packet.
91644375Skarels 				 *
91744375Skarels 				 * We know we're losing at the current
91844375Skarels 				 * window size so do congestion avoidance
91944375Skarels 				 * (set ssthresh to half the current window
92044375Skarels 				 * and pull our congestion window back to
92144375Skarels 				 * the new ssthresh).
92244375Skarels 				 *
92344375Skarels 				 * Dup acks mean that packets have left the
92444375Skarels 				 * network (they're now cached at the receiver)
92544375Skarels 				 * so bump cwnd by the amount in the receiver
92644375Skarels 				 * to keep a constant cwnd packets in the
92744375Skarels 				 * network.
92832098Skarels 				 */
92932098Skarels 				if (tp->t_timer[TCPT_REXMT] == 0 ||
93032098Skarels 				    ti->ti_ack != tp->snd_una)
93132098Skarels 					tp->t_dupacks = 0;
93232098Skarels 				else if (++tp->t_dupacks == tcprexmtthresh) {
93332098Skarels 					tcp_seq onxt = tp->snd_nxt;
93432376Skarels 					u_int win =
93537320Skarels 					    min(tp->snd_wnd, tp->snd_cwnd) / 2 /
93632376Skarels 						tp->t_maxseg;
93732098Skarels 
93832376Skarels 					if (win < 2)
93932376Skarels 						win = 2;
94032376Skarels 					tp->snd_ssthresh = win * tp->t_maxseg;
94132098Skarels 					tp->t_timer[TCPT_REXMT] = 0;
94232098Skarels 					tp->t_rtt = 0;
94332098Skarels 					tp->snd_nxt = ti->ti_ack;
94432098Skarels 					tp->snd_cwnd = tp->t_maxseg;
94532098Skarels 					(void) tcp_output(tp);
94644375Skarels 					tp->snd_cwnd = tp->snd_ssthresh +
94744375Skarels 					       tp->t_maxseg * tp->t_dupacks;
94832098Skarels 					if (SEQ_GT(onxt, tp->snd_nxt))
94932098Skarels 						tp->snd_nxt = onxt;
95032098Skarels 					goto drop;
95144375Skarels 				} else if (tp->t_dupacks > tcprexmtthresh) {
95244375Skarels 					tp->snd_cwnd += tp->t_maxseg;
95344375Skarels 					(void) tcp_output(tp);
95444375Skarels 					goto drop;
95532098Skarels 				}
95632098Skarels 			} else
95732098Skarels 				tp->t_dupacks = 0;
9585065Swnj 			break;
95930525Skarels 		}
96044375Skarels 		/*
96144375Skarels 		 * If the congestion window was inflated to account
96244375Skarels 		 * for the other side's cached packets, retract it.
96344375Skarels 		 */
96444375Skarels 		if (tp->t_dupacks > tcprexmtthresh &&
96544375Skarels 		    tp->snd_cwnd > tp->snd_ssthresh)
96644375Skarels 			tp->snd_cwnd = tp->snd_ssthresh;
96732098Skarels 		tp->t_dupacks = 0;
96830525Skarels 		if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
96930525Skarels 			tcpstat.tcps_rcvacktoomuch++;
9705065Swnj 			goto dropafterack;
97130525Skarels 		}
9725085Swnj 		acked = ti->ti_ack - tp->snd_una;
97330525Skarels 		tcpstat.tcps_rcvackpack++;
97430525Skarels 		tcpstat.tcps_rcvackbyte += acked;
9755951Swnj 
9765951Swnj 		/*
97757433Sandrew 		 * If we have a timestamp reply, update smoothed
97857433Sandrew 		 * round trip time.  If no timestamp is present but
97957433Sandrew 		 * transmit timer is running and timed sequence
9805951Swnj 		 * number was acked, update smoothed round trip time.
98132034Skarels 		 * Since we now have an rtt measurement, cancel the
98232034Skarels 		 * timer backoff (cf., Phil Karn's retransmit alg.).
98332034Skarels 		 * Recompute the initial retransmit timer.
9845951Swnj 		 */
98557433Sandrew 		if (ts_present)
98657433Sandrew 			tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
98757433Sandrew 		else if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
98857433Sandrew 			tcp_xmit_timer(tp,tp->t_rtt);
98931726Skarels 
99026824Skarels 		/*
99126824Skarels 		 * If all outstanding data is acked, stop retransmit
99226824Skarels 		 * timer and remember to restart (more output or persist).
99326824Skarels 		 * If there is more data to be acked, restart retransmit
99432034Skarels 		 * timer, using current (possibly backed-off) value.
99526824Skarels 		 */
99626824Skarels 		if (ti->ti_ack == tp->snd_max) {
9975244Sroot 			tp->t_timer[TCPT_REXMT] = 0;
99826824Skarels 			needoutput = 1;
99932034Skarels 		} else if (tp->t_timer[TCPT_PERSIST] == 0)
100032034Skarels 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
100117360Skarels 		/*
100232098Skarels 		 * When new data is acked, open the congestion window.
100332098Skarels 		 * If the window gives us less than ssthresh packets
100432098Skarels 		 * in flight, open exponentially (maxseg per packet).
100544375Skarels 		 * Otherwise open linearly: maxseg per window
100644375Skarels 		 * (maxseg^2 / cwnd per packet), plus a constant
100744375Skarels 		 * fraction of a packet (maxseg/8) to help larger windows
100844375Skarels 		 * open quickly enough.
100917360Skarels 		 */
101032098Skarels 		{
101144375Skarels 		register u_int cw = tp->snd_cwnd;
101244375Skarels 		register u_int incr = tp->t_maxseg;
101332098Skarels 
101444375Skarels 		if (cw > tp->snd_ssthresh)
101544375Skarels 			incr = incr * incr / cw + incr / 8;
101657433Sandrew 		tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale);
101732098Skarels 		}
10185307Sroot 		if (acked > so->so_snd.sb_cc) {
101915386Ssam 			tp->snd_wnd -= so->so_snd.sb_cc;
102026386Skarels 			sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
102131721Skarels 			ourfinisacked = 1;
10225307Sroot 		} else {
10236161Ssam 			sbdrop(&so->so_snd, acked);
10245307Sroot 			tp->snd_wnd -= acked;
102531721Skarels 			ourfinisacked = 0;
10265307Sroot 		}
102744375Skarels 		if (so->so_snd.sb_flags & SB_NOTIFY)
102844375Skarels 			sowwakeup(so);
10295231Swnj 		tp->snd_una = ti->ti_ack;
10305357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
10315357Sroot 			tp->snd_nxt = tp->snd_una;
10325162Swnj 
10334601Swnj 		switch (tp->t_state) {
10344601Swnj 
10355065Swnj 		/*
10365065Swnj 		 * In FIN_WAIT_1 STATE in addition to the processing
10375065Swnj 		 * for the ESTABLISHED state if our FIN is now acknowledged
10385085Swnj 		 * then enter FIN_WAIT_2.
10395065Swnj 		 */
10405065Swnj 		case TCPS_FIN_WAIT_1:
10415896Swnj 			if (ourfinisacked) {
10425896Swnj 				/*
10435896Swnj 				 * If we can't receive any more
10445896Swnj 				 * data, then closing user can proceed.
104524816Skarels 				 * Starting the timer is contrary to the
104624816Skarels 				 * specification, but if we don't get a FIN
104724816Skarels 				 * we'll hang forever.
10485896Swnj 				 */
104924816Skarels 				if (so->so_state & SS_CANTRCVMORE) {
10505896Swnj 					soisdisconnected(so);
105133745Skarels 					tp->t_timer[TCPT_2MSL] = tcp_maxidle;
105224816Skarels 				}
10535085Swnj 				tp->t_state = TCPS_FIN_WAIT_2;
10545896Swnj 			}
10554601Swnj 			break;
10564601Swnj 
10575065Swnj 	 	/*
10585065Swnj 		 * In CLOSING STATE in addition to the processing for
10595065Swnj 		 * the ESTABLISHED state if the ACK acknowledges our FIN
10605065Swnj 		 * then enter the TIME-WAIT state, otherwise ignore
10615065Swnj 		 * the segment.
10625065Swnj 		 */
10635065Swnj 		case TCPS_CLOSING:
10645244Sroot 			if (ourfinisacked) {
10655065Swnj 				tp->t_state = TCPS_TIME_WAIT;
10665244Sroot 				tcp_canceltimers(tp);
10675244Sroot 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
10685244Sroot 				soisdisconnected(so);
10695244Sroot 			}
10705244Sroot 			break;
10714601Swnj 
10725065Swnj 		/*
107331743Skarels 		 * In LAST_ACK, we may still be waiting for data to drain
107431743Skarels 		 * and/or to be acked, as well as for the ack of our FIN.
107531743Skarels 		 * If our FIN is now acknowledged, delete the TCB,
107631743Skarels 		 * enter the closed state and return.
10775065Swnj 		 */
10785065Swnj 		case TCPS_LAST_ACK:
107931743Skarels 			if (ourfinisacked) {
108010394Ssam 				tp = tcp_close(tp);
108131743Skarels 				goto drop;
108231743Skarels 			}
108331743Skarels 			break;
10844601Swnj 
10855065Swnj 		/*
10865065Swnj 		 * In TIME_WAIT state the only thing that should arrive
10875065Swnj 		 * is a retransmission of the remote FIN.  Acknowledge
10885065Swnj 		 * it and restart the finack timer.
10895065Swnj 		 */
10905065Swnj 		case TCPS_TIME_WAIT:
10915162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
10925065Swnj 			goto dropafterack;
10934601Swnj 		}
10945085Swnj 	}
10954601Swnj 
10965065Swnj step6:
10975065Swnj 	/*
10985244Sroot 	 * Update window information.
109925939Skarels 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
11005244Sroot 	 */
110125939Skarels 	if ((tiflags & TH_ACK) &&
110225939Skarels 	    (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
11035391Swnj 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
110457433Sandrew 	     tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))) {
110530525Skarels 		/* keep track of pure window updates */
110630525Skarels 		if (ti->ti_len == 0 &&
110757433Sandrew 		    tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd)
110830525Skarels 			tcpstat.tcps_rcvwinupd++;
110957433Sandrew 		tp->snd_wnd = tiwin;
11105244Sroot 		tp->snd_wl1 = ti->ti_seq;
11115244Sroot 		tp->snd_wl2 = ti->ti_ack;
111225260Skarels 		if (tp->snd_wnd > tp->max_sndwnd)
111325260Skarels 			tp->max_sndwnd = tp->snd_wnd;
111426824Skarels 		needoutput = 1;
111526824Skarels 	}
11165244Sroot 
11175244Sroot 	/*
11185547Swnj 	 * Process segments with URG.
11195065Swnj 	 */
11207267Swnj 	if ((tiflags & TH_URG) && ti->ti_urp &&
11217267Swnj 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
11225547Swnj 		/*
112325939Skarels 		 * This is a kludge, but if we receive and accept
112413121Ssam 		 * random urgent pointers, we'll crash in
112513121Ssam 		 * soreceive.  It's hard to imagine someone
112613121Ssam 		 * actually wanting to send this much urgent data.
112712441Ssam 		 */
112857433Sandrew 		if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) {
112912441Ssam 			ti->ti_urp = 0;			/* XXX */
113012441Ssam 			tiflags &= ~TH_URG;		/* XXX */
113125939Skarels 			goto dodata;			/* XXX */
113212441Ssam 		}
113312441Ssam 		/*
11345547Swnj 		 * If this segment advances the known urgent pointer,
11355547Swnj 		 * then mark the data stream.  This should not happen
11365547Swnj 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
11375547Swnj 		 * a FIN has been received from the remote side.
11385547Swnj 		 * In these states we ignore the URG.
113927190Skarels 		 *
114027190Skarels 		 * According to RFC961 (Assigned Protocols),
114127190Skarels 		 * the urgent pointer points to the last octet
114227190Skarels 		 * of urgent data.  We continue, however,
114327190Skarels 		 * to consider it to indicate the first octet
114444375Skarels 		 * of data past the urgent section as the original
114544375Skarels 		 * spec states (in one of two places).
11465547Swnj 		 */
11475547Swnj 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
11485547Swnj 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
11495547Swnj 			so->so_oobmark = so->so_rcv.sb_cc +
11505547Swnj 			    (tp->rcv_up - tp->rcv_nxt) - 1;
11515547Swnj 			if (so->so_oobmark == 0)
11525547Swnj 				so->so_state |= SS_RCVATMARK;
11538313Sroot 			sohasoutofband(so);
115424816Skarels 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
11555440Swnj 		}
11565547Swnj 		/*
11575547Swnj 		 * Remove out of band data so doesn't get presented to user.
11585547Swnj 		 * This can happen independent of advancing the URG pointer,
11595547Swnj 		 * but if two URG's are pending at once, some out-of-band
11605547Swnj 		 * data may creep in... ick.
11615547Swnj 		 */
116244375Skarels 		if (ti->ti_urp <= ti->ti_len
116344375Skarels #ifdef SO_OOBINLINE
116444375Skarels 		     && (so->so_options & SO_OOBINLINE) == 0
116544375Skarels #endif
116644375Skarels 		     )
116744375Skarels 			tcp_pulloutofband(so, ti, m);
116825939Skarels 	} else
116925939Skarels 		/*
117025939Skarels 		 * If no out of band data is expected,
117125939Skarels 		 * pull receive urgent pointer along
117225939Skarels 		 * with the receive window.
117325939Skarels 		 */
117425939Skarels 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
117525939Skarels 			tp->rcv_up = tp->rcv_nxt;
117625939Skarels dodata:							/* XXX */
11774601Swnj 
11784601Swnj 	/*
11795065Swnj 	 * Process the segment text, merging it into the TCP sequencing queue,
11805065Swnj 	 * and arranging for acknowledgment of receipt if necessary.
11815065Swnj 	 * This process logically involves adjusting tp->rcv_wnd as data
11825065Swnj 	 * is presented to the user (this happens in tcp_usrreq.c,
11835065Swnj 	 * case PRU_RCVD).  If a FIN has already been received on this
11845065Swnj 	 * connection then we just ignore the text.
11854601Swnj 	 */
118617946Skarels 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
118717946Skarels 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
118824816Skarels 		TCP_REASS(tp, ti, m, so, tiflags);
118925260Skarels 		/*
119025260Skarels 		 * Note the amount of data that peer has sent into
119125260Skarels 		 * our window, in order to estimate the sender's
119225260Skarels 		 * buffer size.
119325260Skarels 		 */
119432098Skarels 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
11955244Sroot 	} else {
11964924Swnj 		m_freem(m);
11975263Swnj 		tiflags &= ~TH_FIN;
11985244Sroot 	}
11994601Swnj 
12004601Swnj 	/*
12015263Swnj 	 * If FIN is received ACK the FIN and let the user know
12025263Swnj 	 * that the connection is closing.
12034601Swnj 	 */
12045263Swnj 	if (tiflags & TH_FIN) {
12055244Sroot 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
12065244Sroot 			socantrcvmore(so);
12075244Sroot 			tp->t_flags |= TF_ACKNOW;
12085244Sroot 			tp->rcv_nxt++;
12095244Sroot 		}
12105065Swnj 		switch (tp->t_state) {
12114601Swnj 
12125065Swnj 	 	/*
12135065Swnj 		 * In SYN_RECEIVED and ESTABLISHED STATES
12145065Swnj 		 * enter the CLOSE_WAIT state.
12154884Swnj 		 */
12165065Swnj 		case TCPS_SYN_RECEIVED:
12175065Swnj 		case TCPS_ESTABLISHED:
12185065Swnj 			tp->t_state = TCPS_CLOSE_WAIT;
12195065Swnj 			break;
12204884Swnj 
12215065Swnj 	 	/*
12225085Swnj 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
12235085Swnj 		 * enter the CLOSING state.
12244884Swnj 		 */
12255065Swnj 		case TCPS_FIN_WAIT_1:
12265085Swnj 			tp->t_state = TCPS_CLOSING;
12275065Swnj 			break;
12284601Swnj 
12295065Swnj 	 	/*
12305065Swnj 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
12315065Swnj 		 * starting the time-wait timer, turning off the other
12325065Swnj 		 * standard timers.
12335065Swnj 		 */
12345065Swnj 		case TCPS_FIN_WAIT_2:
12355244Sroot 			tp->t_state = TCPS_TIME_WAIT;
12365074Swnj 			tcp_canceltimers(tp);
12375162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
12385244Sroot 			soisdisconnected(so);
12395065Swnj 			break;
12405065Swnj 
12414884Swnj 		/*
12425065Swnj 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
12434884Swnj 		 */
12445065Swnj 		case TCPS_TIME_WAIT:
12455162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
12465065Swnj 			break;
12475085Swnj 		}
12484601Swnj 	}
12495267Sroot 	if (so->so_options & SO_DEBUG)
12505267Sroot 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
12515085Swnj 
12525085Swnj 	/*
12535085Swnj 	 * Return any desired output.
12545085Swnj 	 */
125526824Skarels 	if (needoutput || (tp->t_flags & TF_ACKNOW))
125625939Skarels 		(void) tcp_output(tp);
12575065Swnj 	return;
12585085Swnj 
12595065Swnj dropafterack:
12605085Swnj 	/*
12616211Swnj 	 * Generate an ACK dropping incoming segment if it occupies
12626211Swnj 	 * sequence space, where the ACK reflects our state.
12635085Swnj 	 */
126426057Skarels 	if (tiflags & TH_RST)
12655085Swnj 		goto drop;
126631749Skarels 	m_freem(m);
126731721Skarels 	tp->t_flags |= TF_ACKNOW;
126831721Skarels 	(void) tcp_output(tp);
12695231Swnj 	return;
12705085Swnj 
12715085Swnj dropwithreset:
12725085Swnj 	/*
12735244Sroot 	 * Generate a RST, dropping incoming segment.
12745085Swnj 	 * Make ACK acceptable to originator of segment.
127557433Sandrew 	 * Don't bother to respond if destination was broadcast/multicast.
12765085Swnj 	 */
127757433Sandrew 	if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST) ||
1278*58998Ssklower 	    IN_MULTICAST(ti->ti_dst.s_addr))
12795085Swnj 		goto drop;
12805085Swnj 	if (tiflags & TH_ACK)
128137320Skarels 		tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
12825085Swnj 	else {
12835085Swnj 		if (tiflags & TH_SYN)
12845085Swnj 			ti->ti_len++;
128537320Skarels 		tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
12866211Swnj 		    TH_RST|TH_ACK);
12875085Swnj 	}
128810769Ssam 	/* destroy temporarily created socket */
128910769Ssam 	if (dropsocket)
129010769Ssam 		(void) soabort(so);
12915231Swnj 	return;
12925085Swnj 
12935065Swnj drop:
12945085Swnj 	/*
12955085Swnj 	 * Drop space held by incoming segment and return.
12965085Swnj 	 */
12976303Sroot 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
12986303Sroot 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
12995065Swnj 	m_freem(m);
130010769Ssam 	/* destroy temporarily created socket */
130110769Ssam 	if (dropsocket)
130210769Ssam 		(void) soabort(so);
13035267Sroot 	return;
130457947Ssklower #ifndef TUBA_INCLUDE
13055065Swnj }
13065065Swnj 
130757433Sandrew tcp_dooptions(tp, cp, cnt, ti, ts_present, ts_val, ts_ecr)
13085440Swnj 	struct tcpcb *tp;
130957433Sandrew 	u_char *cp;
131057433Sandrew 	int cnt;
131117272Skarels 	struct tcpiphdr *ti;
131257433Sandrew 	int *ts_present;
131357433Sandrew 	u_long *ts_val, *ts_ecr;
13145419Swnj {
131544375Skarels 	u_short mss;
131657433Sandrew 	int opt, optlen;
13175419Swnj 
13185440Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
13195440Swnj 		opt = cp[0];
13205440Swnj 		if (opt == TCPOPT_EOL)
13215440Swnj 			break;
13225440Swnj 		if (opt == TCPOPT_NOP)
13235440Swnj 			optlen = 1;
132412169Ssam 		else {
13255440Swnj 			optlen = cp[1];
132612169Ssam 			if (optlen <= 0)
132712169Ssam 				break;
132812169Ssam 		}
13295440Swnj 		switch (opt) {
13305440Swnj 
13315440Swnj 		default:
133244375Skarels 			continue;
13335440Swnj 
13345440Swnj 		case TCPOPT_MAXSEG:
133557433Sandrew 			if (optlen != TCPOLEN_MAXSEG)
13365440Swnj 				continue;
133717272Skarels 			if (!(ti->ti_flags & TH_SYN))
133817272Skarels 				continue;
133944375Skarels 			bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
134044375Skarels 			NTOHS(mss);
134144375Skarels 			(void) tcp_mss(tp, mss);	/* sets t_maxseg */
13425440Swnj 			break;
134357433Sandrew 
134457433Sandrew 		case TCPOPT_WINDOW:
134557433Sandrew 			if (optlen != TCPOLEN_WINDOW)
134657433Sandrew 				continue;
134757433Sandrew 			if (!(ti->ti_flags & TH_SYN))
134857433Sandrew 				continue;
134957433Sandrew 			tp->t_flags |= TF_RCVD_SCALE;
135057433Sandrew 			tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
135157433Sandrew 			break;
135257433Sandrew 
135357433Sandrew 		case TCPOPT_TIMESTAMP:
135457433Sandrew 			if (optlen != TCPOLEN_TIMESTAMP)
135557433Sandrew 				continue;
135657433Sandrew 			*ts_present = 1;
135757433Sandrew 			bcopy((char *)cp + 2, (char *) ts_val, sizeof(*ts_val));
135857433Sandrew 			NTOHL(*ts_val);
135957433Sandrew 			bcopy((char *)cp + 6, (char *) ts_ecr, sizeof(*ts_ecr));
136057433Sandrew 			NTOHL(*ts_ecr);
136157433Sandrew 
136257433Sandrew 			/*
136357433Sandrew 			 * A timestamp received in a SYN makes
136457433Sandrew 			 * it ok to send timestamp requests and replies.
136557433Sandrew 			 */
136657433Sandrew 			if (ti->ti_flags & TH_SYN) {
136757433Sandrew 				tp->t_flags |= TF_RCVD_TSTMP;
136857433Sandrew 				tp->ts_recent = *ts_val;
136957433Sandrew 				tp->ts_recent_age = tcp_now;
137057433Sandrew 			}
137157433Sandrew 			break;
13725419Swnj 		}
13735419Swnj 	}
13745419Swnj }
13755419Swnj 
13765419Swnj /*
13775547Swnj  * Pull out of band byte out of a segment so
13785547Swnj  * it doesn't appear in the user's data queue.
13795547Swnj  * It is still reflected in the segment length for
13805547Swnj  * sequencing purposes.
13815547Swnj  */
138244375Skarels tcp_pulloutofband(so, ti, m)
13835547Swnj 	struct socket *so;
13845547Swnj 	struct tcpiphdr *ti;
138544375Skarels 	register struct mbuf *m;
13865547Swnj {
13876116Swnj 	int cnt = ti->ti_urp - 1;
13885547Swnj 
13895547Swnj 	while (cnt >= 0) {
13905547Swnj 		if (m->m_len > cnt) {
13915547Swnj 			char *cp = mtod(m, caddr_t) + cnt;
13925547Swnj 			struct tcpcb *tp = sototcpcb(so);
13935547Swnj 
13945547Swnj 			tp->t_iobc = *cp;
13955547Swnj 			tp->t_oobflags |= TCPOOB_HAVEDATA;
13966161Ssam 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
13975547Swnj 			m->m_len--;
13985547Swnj 			return;
13995547Swnj 		}
14005547Swnj 		cnt -= m->m_len;
14015547Swnj 		m = m->m_next;
14025547Swnj 		if (m == 0)
14035547Swnj 			break;
14045547Swnj 	}
14055547Swnj 	panic("tcp_pulloutofband");
14065547Swnj }
14075547Swnj 
14085547Swnj /*
140944375Skarels  * Collect new round-trip time estimate
141044375Skarels  * and update averages and current timeout.
141117272Skarels  */
141257433Sandrew tcp_xmit_timer(tp, rtt)
141323975Skarels 	register struct tcpcb *tp;
141457433Sandrew 	short	rtt;
141517272Skarels {
141644375Skarels 	register short delta;
141744375Skarels 
141844375Skarels 	tcpstat.tcps_rttupdated++;
141944375Skarels 	if (tp->t_srtt != 0) {
142044375Skarels 		/*
142144375Skarels 		 * srtt is stored as fixed point with 3 bits after the
142244375Skarels 		 * binary point (i.e., scaled by 8).  The following magic
142344375Skarels 		 * is equivalent to the smoothing algorithm in rfc793 with
142444375Skarels 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
142557433Sandrew 		 * point).  Adjust rtt to origin 0.
142644375Skarels 		 */
142757433Sandrew 		delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT);
142844375Skarels 		if ((tp->t_srtt += delta) <= 0)
142944375Skarels 			tp->t_srtt = 1;
143044375Skarels 		/*
143144375Skarels 		 * We accumulate a smoothed rtt variance (actually, a
143244375Skarels 		 * smoothed mean difference), then set the retransmit
143344375Skarels 		 * timer to smoothed rtt + 4 times the smoothed variance.
143444375Skarels 		 * rttvar is stored as fixed point with 2 bits after the
143544375Skarels 		 * binary point (scaled by 4).  The following is
143644375Skarels 		 * equivalent to rfc793 smoothing with an alpha of .75
143744375Skarels 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
143844375Skarels 		 * rfc793's wired-in beta.
143944375Skarels 		 */
144044375Skarels 		if (delta < 0)
144144375Skarels 			delta = -delta;
144244375Skarels 		delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
144344375Skarels 		if ((tp->t_rttvar += delta) <= 0)
144444375Skarels 			tp->t_rttvar = 1;
144544375Skarels 	} else {
144644375Skarels 		/*
144744375Skarels 		 * No rtt measurement yet - use the unsmoothed rtt.
144844375Skarels 		 * Set the variance to half the rtt (so our first
144952199Skarels 		 * retransmit happens at 3*rtt).
145044375Skarels 		 */
145157433Sandrew 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
145257433Sandrew 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
145344375Skarels 	}
145444375Skarels 	tp->t_rtt = 0;
145544375Skarels 	tp->t_rxtshift = 0;
145644375Skarels 
145744375Skarels 	/*
145844375Skarels 	 * the retransmit should happen at rtt + 4 * rttvar.
145944375Skarels 	 * Because of the way we do the smoothing, srtt and rttvar
146044375Skarels 	 * will each average +1/2 tick of bias.  When we compute
146144375Skarels 	 * the retransmit timer, we want 1/2 tick of rounding and
146244375Skarels 	 * 1 extra tick because of +-1/2 tick uncertainty in the
146344375Skarels 	 * firing of the timer.  The bias will give us exactly the
146444375Skarels 	 * 1.5 tick we need.  But, because the bias is
146544375Skarels 	 * statistical, we have to test that we don't drop below
146644375Skarels 	 * the minimum feasible timer (which is 2 ticks).
146744375Skarels 	 */
146844375Skarels 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
146944375Skarels 	    tp->t_rttmin, TCPTV_REXMTMAX);
147044375Skarels 
147144375Skarels 	/*
147244375Skarels 	 * We received an ack for a packet that wasn't retransmitted;
147344375Skarels 	 * it is probably safe to discard any error indications we've
147444375Skarels 	 * received recently.  This isn't quite right, but close enough
147544375Skarels 	 * for now (a route might have failed after we sent a segment,
147644375Skarels 	 * and the return path might not be symmetrical).
147744375Skarels 	 */
147844375Skarels 	tp->t_softerror = 0;
147944375Skarels }
148044375Skarels 
148144375Skarels /*
148244375Skarels  * Determine a reasonable value for maxseg size.
148344375Skarels  * If the route is known, check route for mtu.
148444375Skarels  * If none, use an mss that can be handled on the outgoing
148544375Skarels  * interface without forcing IP to fragment; if bigger than
148644375Skarels  * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
148744375Skarels  * to utilize large mbufs.  If no route is found, route has no mtu,
148844375Skarels  * or the destination isn't local, use a default, hopefully conservative
148944375Skarels  * size (usually 512 or the default IP max size, but no more than the mtu
149044375Skarels  * of the interface), as we can't discover anything about intervening
149144375Skarels  * gateways or networks.  We also initialize the congestion/slow start
149244375Skarels  * window to be a single segment if the destination isn't local.
149344375Skarels  * While looking at the routing entry, we also initialize other path-dependent
149444375Skarels  * parameters from pre-set or cached values in the routing entry.
149544375Skarels  */
149644375Skarels 
149744375Skarels tcp_mss(tp, offer)
149844375Skarels 	register struct tcpcb *tp;
149944375Skarels 	u_short offer;
150044375Skarels {
150117272Skarels 	struct route *ro;
150244375Skarels 	register struct rtentry *rt;
150317272Skarels 	struct ifnet *ifp;
150444375Skarels 	register int rtt, mss;
150544375Skarels 	u_long bufsize;
150617272Skarels 	struct inpcb *inp;
150744375Skarels 	struct socket *so;
150844375Skarels 	extern int tcp_mssdflt, tcp_rttdflt;
150917272Skarels 
151017272Skarels 	inp = tp->t_inpcb;
151117272Skarels 	ro = &inp->inp_route;
151244375Skarels 
151344375Skarels 	if ((rt = ro->ro_rt) == (struct rtentry *)0) {
151417272Skarels 		/* No route yet, so try to acquire one */
151517272Skarels 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
151617272Skarels 			ro->ro_dst.sa_family = AF_INET;
151737320Skarels 			ro->ro_dst.sa_len = sizeof(ro->ro_dst);
151817272Skarels 			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
151917272Skarels 				inp->inp_faddr;
152017272Skarels 			rtalloc(ro);
152117272Skarels 		}
152244375Skarels 		if ((rt = ro->ro_rt) == (struct rtentry *)0)
152357637Sandrew 			return (tcp_mssdflt);
152417272Skarels 	}
152544375Skarels 	ifp = rt->rt_ifp;
152644375Skarels 	so = inp->inp_socket;
152717272Skarels 
152844375Skarels #ifdef RTV_MTU	/* if route characteristics exist ... */
152944375Skarels 	/*
153044375Skarels 	 * While we're here, check if there's an initial rtt
153144375Skarels 	 * or rttvar.  Convert from the route-table units
153244375Skarels 	 * to scaled multiples of the slow timeout timer.
153344375Skarels 	 */
153444375Skarels 	if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
153552199Skarels 		/*
153652199Skarels 		 * XXX the lock bit for MTU indicates that the value
153752199Skarels 		 * is also a minimum value; this is subject to time.
153852199Skarels 		 */
153952199Skarels 		if (rt->rt_rmx.rmx_locks & RTV_RTT)
154044375Skarels 			tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ);
154144375Skarels 		tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
154244375Skarels 		if (rt->rt_rmx.rmx_rttvar)
154344375Skarels 			tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
154444375Skarels 			    (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
154544375Skarels 		else
154644375Skarels 			/* default variation is +- 1 rtt */
154744375Skarels 			tp->t_rttvar =
154844375Skarels 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
154944375Skarels 		TCPT_RANGESET(tp->t_rxtcur,
155044375Skarels 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
155144375Skarels 		    tp->t_rttmin, TCPTV_REXMTMAX);
155244375Skarels 	}
155344375Skarels 	/*
155444375Skarels 	 * if there's an mtu associated with the route, use it
155544375Skarels 	 */
155644375Skarels 	if (rt->rt_rmx.rmx_mtu)
155757637Sandrew 		mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
155844375Skarels 	else
155944375Skarels #endif /* RTV_MTU */
156044375Skarels 	{
156157637Sandrew 		mss = ifp->if_mtu - sizeof(struct tcpiphdr);
156231726Skarels #if	(MCLBYTES & (MCLBYTES - 1)) == 0
156344375Skarels 		if (mss > MCLBYTES)
156444375Skarels 			mss &= ~(MCLBYTES-1);
156517272Skarels #else
156644375Skarels 		if (mss > MCLBYTES)
156744375Skarels 			mss = mss / MCLBYTES * MCLBYTES;
156817272Skarels #endif
156944375Skarels 		if (!in_localaddr(inp->inp_faddr))
157057637Sandrew 			mss = min(mss, tcp_mssdflt);
157144375Skarels 	}
157244375Skarels 	/*
157344375Skarels 	 * The current mss, t_maxseg, is initialized to the default value.
157444375Skarels 	 * If we compute a smaller value, reduce the current mss.
157544375Skarels 	 * If we compute a larger value, return it for use in sending
157644375Skarels 	 * a max seg size option, but don't store it for use
157744375Skarels 	 * unless we received an offer at least that large from peer.
157844375Skarels 	 * However, do not accept offers under 32 bytes.
157944375Skarels 	 */
158044375Skarels 	if (offer)
158144375Skarels 		mss = min(mss, offer);
158244375Skarels 	mss = max(mss, 32);		/* sanity */
158344375Skarels 	if (mss < tp->t_maxseg || offer != 0) {
158444375Skarels 		/*
158544375Skarels 		 * If there's a pipesize, change the socket buffer
158644375Skarels 		 * to that size.  Make the socket buffers an integral
158744375Skarels 		 * number of mss units; if the mss is larger than
158844375Skarels 		 * the socket buffer, decrease the mss.
158944375Skarels 		 */
159044375Skarels #ifdef RTV_SPIPE
159144375Skarels 		if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
159244375Skarels #endif
159344375Skarels 			bufsize = so->so_snd.sb_hiwat;
159444375Skarels 		if (bufsize < mss)
159544375Skarels 			mss = bufsize;
159644375Skarels 		else {
159756908Storek 			bufsize = roundup(bufsize, mss);
159857433Sandrew 			if (bufsize > sb_max)
159957433Sandrew 				bufsize = sb_max;
160056908Storek 			(void)sbreserve(&so->so_snd, bufsize);
160144375Skarels 		}
160244375Skarels 		tp->t_maxseg = mss;
160332098Skarels 
160444375Skarels #ifdef RTV_RPIPE
160544375Skarels 		if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
160644375Skarels #endif
160744375Skarels 			bufsize = so->so_rcv.sb_hiwat;
160844375Skarels 		if (bufsize > mss) {
160956908Storek 			bufsize = roundup(bufsize, mss);
161057433Sandrew 			if (bufsize > sb_max)
161157433Sandrew 				bufsize = sb_max;
161256908Storek 			(void)sbreserve(&so->so_rcv, bufsize);
161344375Skarels 		}
161444375Skarels 	}
161532034Skarels 	tp->snd_cwnd = mss;
161644375Skarels 
161744375Skarels #ifdef RTV_SSTHRESH
161844375Skarels 	if (rt->rt_rmx.rmx_ssthresh) {
161944375Skarels 		/*
162044375Skarels 		 * There's some sort of gateway or interface
162144375Skarels 		 * buffer limit on the path.  Use this to set
162244375Skarels 		 * the slow start threshhold, but set the
162344375Skarels 		 * threshold to no less than 2*mss.
162444375Skarels 		 */
162544375Skarels 		tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
162644375Skarels 	}
162744375Skarels #endif /* RTV_MTU */
162832034Skarels 	return (mss);
162917272Skarels }
163057947Ssklower #endif /* TUBA_INCLUDE */
1631