xref: /csrg-svn/sys/netinet/tcp_input.c (revision 44485)
123190Smckusick /*
244375Skarels  * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California.
332787Sbostic  * All rights reserved.
423190Smckusick  *
5*44485Sbostic  * %sccs.include.redist.c%
632787Sbostic  *
7*44485Sbostic  *	@(#)tcp_input.c	7.24 (Berkeley) 06/28/90
823190Smckusick  */
94601Swnj 
1017062Sbloom #include "param.h"
1117062Sbloom #include "systm.h"
1237320Skarels #include "malloc.h"
1317062Sbloom #include "mbuf.h"
1417062Sbloom #include "protosw.h"
1517062Sbloom #include "socket.h"
1617062Sbloom #include "socketvar.h"
1717062Sbloom #include "errno.h"
1810894Ssam 
1910894Ssam #include "../net/if.h"
2010894Ssam #include "../net/route.h"
2110894Ssam 
2217062Sbloom #include "in.h"
2317062Sbloom #include "in_systm.h"
2417062Sbloom #include "ip.h"
2540687Skarels #include "in_pcb.h"
2617062Sbloom #include "ip_var.h"
2717062Sbloom #include "tcp.h"
2817062Sbloom #include "tcp_fsm.h"
2917062Sbloom #include "tcp_seq.h"
3017062Sbloom #include "tcp_timer.h"
3117062Sbloom #include "tcp_var.h"
3217062Sbloom #include "tcpip.h"
3317062Sbloom #include "tcp_debug.h"
344601Swnj 
3544375Skarels #define VAN
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 
435267Sroot struct	tcpcb *tcp_newtcpcb();
4424816Skarels 
455065Swnj /*
4624816Skarels  * Insert segment ti into reassembly queue of tcp with
4724816Skarels  * control block tp.  Return TH_FIN if reassembly now includes
4824816Skarels  * a segment with FIN.  The macro form does the common case inline
4924816Skarels  * (segment is the next to be received on an established connection,
5024816Skarels  * and the queue is empty), avoiding linkage into and removal
5124816Skarels  * from the queue and repetition of various conversions.
5234278Skarels  * Set DELACK for segments received in order, but ack immediately
5334278Skarels  * when segments are out of order (so fast retransmit can work).
5424816Skarels  */
5524816Skarels #define	TCP_REASS(tp, ti, m, so, flags) { \
5624816Skarels 	if ((ti)->ti_seq == (tp)->rcv_nxt && \
5724816Skarels 	    (tp)->seg_next == (struct tcpiphdr *)(tp) && \
5824816Skarels 	    (tp)->t_state == TCPS_ESTABLISHED) { \
5934278Skarels 		tp->t_flags |= TF_DELACK; \
6024816Skarels 		(tp)->rcv_nxt += (ti)->ti_len; \
6124816Skarels 		flags = (ti)->ti_flags & TH_FIN; \
6230525Skarels 		tcpstat.tcps_rcvpack++;\
6330525Skarels 		tcpstat.tcps_rcvbyte += (ti)->ti_len;\
6424816Skarels 		sbappend(&(so)->so_rcv, (m)); \
6524816Skarels 		sorwakeup(so); \
6634278Skarels 	} else { \
6744375Skarels 		(flags) = tcp_reass((tp), (ti), (m)); \
6834278Skarels 		tp->t_flags |= TF_ACKNOW; \
6934278Skarels 	} \
7024816Skarels }
7124816Skarels 
7244375Skarels tcp_reass(tp, ti, m)
7324816Skarels 	register struct tcpcb *tp;
7424816Skarels 	register struct tcpiphdr *ti;
7544375Skarels 	struct mbuf *m;
7624816Skarels {
7724816Skarels 	register struct tcpiphdr *q;
7824816Skarels 	struct socket *so = tp->t_inpcb->inp_socket;
7924816Skarels 	int flags;
8024816Skarels 
8124816Skarels 	/*
8224816Skarels 	 * Call with ti==0 after become established to
8324816Skarels 	 * force pre-ESTABLISHED data up to user socket.
8424816Skarels 	 */
8524816Skarels 	if (ti == 0)
8624816Skarels 		goto present;
8724816Skarels 
8824816Skarels 	/*
8924816Skarels 	 * Find a segment which begins after this one does.
9024816Skarels 	 */
9124816Skarels 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
9224816Skarels 	    q = (struct tcpiphdr *)q->ti_next)
9324816Skarels 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
9424816Skarels 			break;
9524816Skarels 
9624816Skarels 	/*
9724816Skarels 	 * If there is a preceding segment, it may provide some of
9824816Skarels 	 * our data already.  If so, drop the data from the incoming
9924816Skarels 	 * segment.  If it provides all of our data, drop us.
10024816Skarels 	 */
10124816Skarels 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
10224816Skarels 		register int i;
10324816Skarels 		q = (struct tcpiphdr *)q->ti_prev;
10424816Skarels 		/* conversion to int (in i) handles seq wraparound */
10524816Skarels 		i = q->ti_seq + q->ti_len - ti->ti_seq;
10624816Skarels 		if (i > 0) {
10730525Skarels 			if (i >= ti->ti_len) {
10830525Skarels 				tcpstat.tcps_rcvduppack++;
10930525Skarels 				tcpstat.tcps_rcvdupbyte += ti->ti_len;
11044375Skarels 				m_freem(m);
11144375Skarels 				return (0);
11230525Skarels 			}
11344375Skarels 			m_adj(m, i);
11424816Skarels 			ti->ti_len -= i;
11524816Skarels 			ti->ti_seq += i;
11624816Skarels 		}
11724816Skarels 		q = (struct tcpiphdr *)(q->ti_next);
11824816Skarels 	}
11930525Skarels 	tcpstat.tcps_rcvoopack++;
12030525Skarels 	tcpstat.tcps_rcvoobyte += ti->ti_len;
12144375Skarels 	REASS_MBUF(ti) = m;		/* XXX */
12224816Skarels 
12324816Skarels 	/*
12424816Skarels 	 * While we overlap succeeding segments trim them or,
12524816Skarels 	 * if they are completely covered, dequeue them.
12624816Skarels 	 */
12724816Skarels 	while (q != (struct tcpiphdr *)tp) {
12824816Skarels 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
12924816Skarels 		if (i <= 0)
13024816Skarels 			break;
13124816Skarels 		if (i < q->ti_len) {
13224816Skarels 			q->ti_seq += i;
13324816Skarels 			q->ti_len -= i;
13444375Skarels 			m_adj(REASS_MBUF(q), i);
13524816Skarels 			break;
13624816Skarels 		}
13724816Skarels 		q = (struct tcpiphdr *)q->ti_next;
13844375Skarels 		m = REASS_MBUF((struct tcpiphdr *)q->ti_prev);
13924816Skarels 		remque(q->ti_prev);
14024816Skarels 		m_freem(m);
14124816Skarels 	}
14224816Skarels 
14324816Skarels 	/*
14424816Skarels 	 * Stick new segment in its place.
14524816Skarels 	 */
14624816Skarels 	insque(ti, q->ti_prev);
14724816Skarels 
14824816Skarels present:
14924816Skarels 	/*
15024816Skarels 	 * Present data to user, advancing rcv_nxt through
15124816Skarels 	 * completed sequence space.
15224816Skarels 	 */
15324816Skarels 	if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
15424816Skarels 		return (0);
15524816Skarels 	ti = tp->seg_next;
15624816Skarels 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
15724816Skarels 		return (0);
15824816Skarels 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
15924816Skarels 		return (0);
16024816Skarels 	do {
16124816Skarels 		tp->rcv_nxt += ti->ti_len;
16224816Skarels 		flags = ti->ti_flags & TH_FIN;
16324816Skarels 		remque(ti);
16444375Skarels 		m = REASS_MBUF(ti);
16524816Skarels 		ti = (struct tcpiphdr *)ti->ti_next;
16624816Skarels 		if (so->so_state & SS_CANTRCVMORE)
16724816Skarels 			m_freem(m);
16824816Skarels 		else
16924816Skarels 			sbappend(&so->so_rcv, m);
17024816Skarels 	} while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
17124816Skarels 	sorwakeup(so);
17224816Skarels 	return (flags);
17324816Skarels }
17424816Skarels 
17524816Skarels /*
1765065Swnj  * TCP input routine, follows pages 65-76 of the
1775065Swnj  * protocol specification dated September, 1981 very closely.
1785065Swnj  */
17937320Skarels tcp_input(m, iphlen)
18037320Skarels 	register struct mbuf *m;
18137320Skarels 	int iphlen;
1824601Swnj {
1834924Swnj 	register struct tcpiphdr *ti;
18444375Skarels 	register struct inpcb *inp;
1855440Swnj 	struct mbuf *om = 0;
1864924Swnj 	int len, tlen, off;
1875391Swnj 	register struct tcpcb *tp = 0;
1884924Swnj 	register int tiflags;
1894803Swnj 	struct socket *so;
19031721Skarels 	int todrop, acked, ourfinisacked, needoutput = 0;
1915267Sroot 	short ostate;
1926028Sroot 	struct in_addr laddr;
19310769Ssam 	int dropsocket = 0;
19430525Skarels 	int iss = 0;
1954924Swnj 
19630525Skarels 	tcpstat.tcps_rcvtotal++;
1974924Swnj 	/*
1985244Sroot 	 * Get IP and TCP header together in first mbuf.
1995244Sroot 	 * Note: IP leaves IP header in first mbuf.
2004924Swnj 	 */
2015020Sroot 	ti = mtod(m, struct tcpiphdr *);
20237320Skarels 	if (iphlen > sizeof (struct ip))
20337320Skarels 		ip_stripoptions(m, (struct mbuf *)0);
20444375Skarels 	if (m->m_len < sizeof (struct tcpiphdr)) {
2055307Sroot 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
20630525Skarels 			tcpstat.tcps_rcvshort++;
2075307Sroot 			return;
2085085Swnj 		}
2095085Swnj 		ti = mtod(m, struct tcpiphdr *);
2105085Swnj 	}
2114601Swnj 
2124601Swnj 	/*
2135244Sroot 	 * Checksum extended TCP header and data.
2144601Swnj 	 */
2154924Swnj 	tlen = ((struct ip *)ti)->ip_len;
2164924Swnj 	len = sizeof (struct ip) + tlen;
21737320Skarels 	ti->ti_next = ti->ti_prev = 0;
21837320Skarels 	ti->ti_x1 = 0;
21937320Skarels 	ti->ti_len = (u_short)tlen;
22044375Skarels 	HTONS(ti->ti_len);
22137320Skarels 	if (ti->ti_sum = in_cksum(m, len)) {
22237320Skarels 		tcpstat.tcps_rcvbadsum++;
22337320Skarels 		goto drop;
2244601Swnj 	}
2254601Swnj 
2264601Swnj 	/*
2275244Sroot 	 * Check that TCP offset makes sense,
22844375Skarels 	 * pull out TCP options and adjust length.		XXX
2294601Swnj 	 */
2304924Swnj 	off = ti->ti_off << 2;
2315231Swnj 	if (off < sizeof (struct tcphdr) || off > tlen) {
23230525Skarels 		tcpstat.tcps_rcvbadoff++;
2335085Swnj 		goto drop;
2344924Swnj 	}
2356211Swnj 	tlen -= off;
2366211Swnj 	ti->ti_len = tlen;
2375440Swnj 	if (off > sizeof (struct tcphdr)) {
23824816Skarels 		if (m->m_len < sizeof(struct ip) + off) {
23924816Skarels 			if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
24030525Skarels 				tcpstat.tcps_rcvshort++;
24124816Skarels 				return;
24224816Skarels 			}
24324816Skarels 			ti = mtod(m, struct tcpiphdr *);
2445440Swnj 		}
2459642Ssam 		om = m_get(M_DONTWAIT, MT_DATA);
2465440Swnj 		if (om == 0)
2475440Swnj 			goto drop;
2485440Swnj 		om->m_len = off - sizeof (struct tcphdr);
2495440Swnj 		{ caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
2506161Ssam 		  bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len);
2515440Swnj 		  m->m_len -= om->m_len;
25237320Skarels 		  m->m_pkthdr.len -= om->m_len;
2536161Ssam 		  bcopy(op+om->m_len, op,
2546161Ssam 		   (unsigned)(m->m_len-sizeof (struct tcpiphdr)));
2555440Swnj 		}
2565440Swnj 	}
2575065Swnj 	tiflags = ti->ti_flags;
2584924Swnj 
2596093Sroot 	/*
2605244Sroot 	 * Convert TCP protocol specific fields to host format.
2615085Swnj 	 */
26244375Skarels 	NTOHL(ti->ti_seq);
26344375Skarels 	NTOHL(ti->ti_ack);
26444375Skarels 	NTOHS(ti->ti_win);
26544375Skarels 	NTOHS(ti->ti_urp);
2665085Swnj 
2675085Swnj 	/*
2688271Sroot 	 * Locate pcb for segment.
2694924Swnj 	 */
27030525Skarels findpcb:
27144375Skarels 	inp = tcp_last_inpcb;
27244375Skarels 	if (inp->inp_lport != ti->ti_dport ||
27344375Skarels 	    inp->inp_fport != ti->ti_sport ||
27444375Skarels 	    inp->inp_faddr.s_addr != ti->ti_src.s_addr ||
27544375Skarels 	    inp->inp_laddr.s_addr != ti->ti_dst.s_addr) {
27644375Skarels 		inp = in_pcblookup(&tcb, ti->ti_src, ti->ti_sport,
27744375Skarels 		    ti->ti_dst, ti->ti_dport, INPLOOKUP_WILDCARD);
27844375Skarels 		if (inp)
27944375Skarels 			tcp_last_inpcb = inp;
28044375Skarels 		++tcppcbcachemiss;
28144375Skarels 	}
2825065Swnj 
2835065Swnj 	/*
2845065Swnj 	 * If the state is CLOSED (i.e., TCB does not exist) then
2855244Sroot 	 * all data in the incoming segment is discarded.
28632098Skarels 	 * If the TCB exists but is in CLOSED state, it is embryonic,
28732098Skarels 	 * but should either do a listen or a connect soon.
2885065Swnj 	 */
2895300Sroot 	if (inp == 0)
2905085Swnj 		goto dropwithreset;
2915065Swnj 	tp = intotcpcb(inp);
2925300Sroot 	if (tp == 0)
2935085Swnj 		goto dropwithreset;
29432098Skarels 	if (tp->t_state == TCPS_CLOSED)
29532098Skarels 		goto drop;
2965109Swnj 	so = inp->inp_socket;
29744375Skarels 	if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
29844375Skarels 		if (so->so_options & SO_DEBUG) {
29944375Skarels 			ostate = tp->t_state;
30044375Skarels 			tcp_saveti = *ti;
30144375Skarels 		}
30244375Skarels 		if (so->so_options & SO_ACCEPTCONN) {
30344375Skarels 			so = sonewconn(so, 0);
30444375Skarels 			if (so == 0)
30544375Skarels 				goto drop;
30644375Skarels 			/*
30744375Skarels 			 * This is ugly, but ....
30844375Skarels 			 *
30944375Skarels 			 * Mark socket as temporary until we're
31044375Skarels 			 * committed to keeping it.  The code at
31144375Skarels 			 * ``drop'' and ``dropwithreset'' check the
31244375Skarels 			 * flag dropsocket to see if the temporary
31344375Skarels 			 * socket created here should be discarded.
31444375Skarels 			 * We mark the socket as discardable until
31544375Skarels 			 * we're committed to it below in TCPS_LISTEN.
31644375Skarels 			 */
31744375Skarels 			dropsocket++;
31844375Skarels 			inp = (struct inpcb *)so->so_pcb;
31944375Skarels 			inp->inp_laddr = ti->ti_dst;
32044375Skarels 			inp->inp_lport = ti->ti_dport;
32144375Skarels #if BSD>=43
32244375Skarels 			inp->inp_options = ip_srcroute();
32344375Skarels #endif
32444375Skarels 			tp = intotcpcb(inp);
32544375Skarels 			tp->t_state = TCPS_LISTEN;
32644375Skarels 		}
3275267Sroot 	}
3284601Swnj 
3294601Swnj 	/*
3305162Swnj 	 * Segment received on connection.
3315162Swnj 	 * Reset idle time and keep-alive timer.
3325162Swnj 	 */
3335162Swnj 	tp->t_idle = 0;
33433745Skarels 	tp->t_timer[TCPT_KEEP] = tcp_keepidle;
3355162Swnj 
33644375Skarels #ifndef VAN
3375162Swnj 	/*
33817272Skarels 	 * Process options if not in LISTEN state,
33917272Skarels 	 * else do it below (after getting remote address).
3405440Swnj 	 */
34117272Skarels 	if (om && tp->t_state != TCPS_LISTEN) {
34217272Skarels 		tcp_dooptions(tp, om, ti);
3435440Swnj 		om = 0;
3445440Swnj 	}
34544375Skarels #endif VAN
34644375Skarels 	/*
34744375Skarels 	 * Header prediction: check for the two common cases
34844375Skarels 	 * of a uni-directional data xfer.  If the packet has
34944375Skarels 	 * no control flags, is in-sequence, the window didn't
35044375Skarels 	 * change and we're not retransmitting, it's a
35144375Skarels 	 * candidate.  If the length is zero and the ack moved
35244375Skarels 	 * forward, we're the sender side of the xfer.  Just
35344375Skarels 	 * free the data acked & wake any higher level process
35444375Skarels 	 * that was blocked waiting for space.  If the length
35544375Skarels 	 * is non-zero and the ack didn't move, we're the
35644375Skarels 	 * receiver side.  If we're getting packets in-order
35744375Skarels 	 * (the reassembly queue is empty), add the data to
35844375Skarels 	 * the socket buffer and note that we need a delayed ack.
35944375Skarels 	 */
36044375Skarels 	if (tp->t_state == TCPS_ESTABLISHED &&
36144375Skarels 	    (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
36244375Skarels 	    ti->ti_seq == tp->rcv_nxt &&
36344375Skarels 	    ti->ti_win && ti->ti_win == tp->snd_wnd &&
36444375Skarels 	    tp->snd_nxt == tp->snd_max) {
36544375Skarels 		if (ti->ti_len == 0) {
36644375Skarels 			if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
36744375Skarels 			    SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
36844375Skarels 			    tp->snd_cwnd >= tp->snd_wnd) {
36944375Skarels 				/*
37044375Skarels 				 * this is a pure ack for outstanding data.
37144375Skarels 				 */
37244375Skarels 				++tcppredack;
37344375Skarels 				if (tp->t_rtt && SEQ_GT(ti->ti_ack,tp->t_rtseq))
37444375Skarels 					tcp_xmit_timer(tp);
37544375Skarels 				acked = ti->ti_ack - tp->snd_una;
37644375Skarels 				tcpstat.tcps_rcvackpack++;
37744375Skarels 				tcpstat.tcps_rcvackbyte += acked;
37844375Skarels 				sbdrop(&so->so_snd, acked);
37944375Skarels 				tp->snd_una = ti->ti_ack;
38044375Skarels 				m_freem(m);
3815440Swnj 
38244375Skarels 				/*
38344375Skarels 				 * If all outstanding data are acked, stop
38444375Skarels 				 * retransmit timer, otherwise restart timer
38544375Skarels 				 * using current (possibly backed-off) value.
38644375Skarels 				 * If process is waiting for space,
38744375Skarels 				 * wakeup/selwakeup/signal.  If data
38844375Skarels 				 * are ready to send, let tcp_output
38944375Skarels 				 * decide between more output or persist.
39044375Skarels 				 */
39144375Skarels 				if (tp->snd_una == tp->snd_max)
39244375Skarels 					tp->t_timer[TCPT_REXMT] = 0;
39344375Skarels 				else if (tp->t_timer[TCPT_PERSIST] == 0)
39444375Skarels 					tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
39544375Skarels 
39644375Skarels 				if (so->so_snd.sb_flags & SB_NOTIFY)
39744375Skarels 					sowwakeup(so);
39844375Skarels 				if (so->so_snd.sb_cc)
39944375Skarels 					(void) tcp_output(tp);
40044375Skarels 				return;
40144375Skarels 			}
40244375Skarels 		} else if (ti->ti_ack == tp->snd_una &&
40344375Skarels 		    tp->seg_next == (struct tcpiphdr *)tp &&
40444375Skarels 		    ti->ti_len <= sbspace(&so->so_rcv)) {
40544375Skarels 			/*
40644375Skarels 			 * this is a pure, in-sequence data packet
40744375Skarels 			 * with nothing on the reassembly queue and
40844375Skarels 			 * we have enough buffer space to take it.
40944375Skarels 			 */
41044375Skarels 			++tcppreddat;
41144375Skarels 			tp->rcv_nxt += ti->ti_len;
41244375Skarels 			tcpstat.tcps_rcvpack++;
41344375Skarels 			tcpstat.tcps_rcvbyte += ti->ti_len;
41444375Skarels 			/*
41544375Skarels 			 * Drop TCP and IP headers then add data
41644375Skarels 			 * to socket buffer
41744375Skarels 			 */
41844375Skarels 			m->m_data += sizeof(struct tcpiphdr);
41944375Skarels 			m->m_len -= sizeof(struct tcpiphdr);
42044375Skarels 			sbappend(&so->so_rcv, m);
42144375Skarels 			sorwakeup(so);
42244375Skarels 			tp->t_flags |= TF_DELACK;
42344375Skarels 			return;
42444375Skarels 		}
42544375Skarels 	}
42644375Skarels 
4275440Swnj 	/*
42844375Skarels 	 * Drop TCP and IP headers; TCP options were dropped above.
42944375Skarels 	 */
43044375Skarels 	m->m_data += sizeof(struct tcpiphdr);
43144375Skarels 	m->m_len -= sizeof(struct tcpiphdr);
43244375Skarels 
43344375Skarels 	/*
4345085Swnj 	 * Calculate amount of space in receive window,
4355085Swnj 	 * and then do TCP input processing.
43624816Skarels 	 * Receive window is amount of space in rcv queue,
43724816Skarels 	 * but not less than advertised window.
4384601Swnj 	 */
43925939Skarels 	{ int win;
4404601Swnj 
44125939Skarels 	win = sbspace(&so->so_rcv);
44225939Skarels 	if (win < 0)
44325939Skarels 		win = 0;
44437320Skarels 	tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt));
44525939Skarels 	}
44625939Skarels 
4474601Swnj 	switch (tp->t_state) {
4484601Swnj 
4495065Swnj 	/*
4505065Swnj 	 * If the state is LISTEN then ignore segment if it contains an RST.
4515065Swnj 	 * If the segment contains an ACK then it is bad and send a RST.
4525065Swnj 	 * If it does not contain a SYN then it is not interesting; drop it.
45325197Skarels 	 * Don't bother responding if the destination was a broadcast.
4545085Swnj 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
4555065Swnj 	 * tp->iss, and send a segment:
4565085Swnj 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
4575065Swnj 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
4585065Swnj 	 * Fill in remote peer address fields if not previously specified.
4595065Swnj 	 * Enter SYN_RECEIVED state, and process any other fields of this
4605244Sroot 	 * segment in this state.
4615065Swnj 	 */
4628271Sroot 	case TCPS_LISTEN: {
46310145Ssam 		struct mbuf *am;
4648271Sroot 		register struct sockaddr_in *sin;
4658271Sroot 
4665065Swnj 		if (tiflags & TH_RST)
4675065Swnj 			goto drop;
4685300Sroot 		if (tiflags & TH_ACK)
4695085Swnj 			goto dropwithreset;
4705300Sroot 		if ((tiflags & TH_SYN) == 0)
4715065Swnj 			goto drop;
47237320Skarels 		if (m->m_flags & M_BCAST)
47325197Skarels 			goto drop;
47444375Skarels 		am = m_get(M_DONTWAIT, MT_SONAME);	/* XXX */
47510145Ssam 		if (am == NULL)
47610145Ssam 			goto drop;
47710145Ssam 		am->m_len = sizeof (struct sockaddr_in);
4788599Sroot 		sin = mtod(am, struct sockaddr_in *);
4798271Sroot 		sin->sin_family = AF_INET;
48037320Skarels 		sin->sin_len = sizeof(*sin);
4818271Sroot 		sin->sin_addr = ti->ti_src;
4828271Sroot 		sin->sin_port = ti->ti_sport;
4836028Sroot 		laddr = inp->inp_laddr;
48410145Ssam 		if (inp->inp_laddr.s_addr == INADDR_ANY)
4856028Sroot 			inp->inp_laddr = ti->ti_dst;
4868599Sroot 		if (in_pcbconnect(inp, am)) {
4876028Sroot 			inp->inp_laddr = laddr;
4888716Sroot 			(void) m_free(am);
4895244Sroot 			goto drop;
4906028Sroot 		}
4918716Sroot 		(void) m_free(am);
4925244Sroot 		tp->t_template = tcp_template(tp);
4935244Sroot 		if (tp->t_template == 0) {
49426386Skarels 			tp = tcp_drop(tp, ENOBUFS);
49517264Skarels 			dropsocket = 0;		/* socket is already gone */
4965244Sroot 			goto drop;
4975244Sroot 		}
49817272Skarels 		if (om) {
49917272Skarels 			tcp_dooptions(tp, om, ti);
50017272Skarels 			om = 0;
50117272Skarels 		}
50230525Skarels 		if (iss)
50330525Skarels 			tp->iss = iss;
50430525Skarels 		else
50530525Skarels 			tp->iss = tcp_iss;
50630525Skarels 		tcp_iss += TCP_ISSINCR/2;
5075065Swnj 		tp->irs = ti->ti_seq;
5085085Swnj 		tcp_sendseqinit(tp);
5095085Swnj 		tcp_rcvseqinit(tp);
51025939Skarels 		tp->t_flags |= TF_ACKNOW;
5115065Swnj 		tp->t_state = TCPS_SYN_RECEIVED;
51233745Skarels 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
51310769Ssam 		dropsocket = 0;		/* committed to socket */
51430525Skarels 		tcpstat.tcps_accepts++;
5155085Swnj 		goto trimthenstep6;
5168271Sroot 		}
5174601Swnj 
5185065Swnj 	/*
5195065Swnj 	 * If the state is SYN_SENT:
5205065Swnj 	 *	if seg contains an ACK, but not for our SYN, drop the input.
5215065Swnj 	 *	if seg contains a RST, then drop the connection.
5225065Swnj 	 *	if seg does not contain SYN, then drop it.
5235065Swnj 	 * Otherwise this is an acceptable SYN segment
5245065Swnj 	 *	initialize tp->rcv_nxt and tp->irs
5255065Swnj 	 *	if seg contains ack then advance tp->snd_una
5265065Swnj 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
5275065Swnj 	 *	arrange for segment to be acked (eventually)
5285065Swnj 	 *	continue processing rest of data/controls, beginning with URG
5295065Swnj 	 */
5305065Swnj 	case TCPS_SYN_SENT:
53144375Skarels #ifdef VAN
53244375Skarels 		if (om) {
53344375Skarels 			tcp_dooptions(tp, om, ti);
53444375Skarels 			om = 0;
53544375Skarels 		}
53644375Skarels #endif VAN
5375065Swnj 		if ((tiflags & TH_ACK) &&
53824816Skarels 		    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
5395231Swnj 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
5405085Swnj 			goto dropwithreset;
5415065Swnj 		if (tiflags & TH_RST) {
54210394Ssam 			if (tiflags & TH_ACK)
54310394Ssam 				tp = tcp_drop(tp, ECONNREFUSED);
5445065Swnj 			goto drop;
5454601Swnj 		}
5465065Swnj 		if ((tiflags & TH_SYN) == 0)
5475065Swnj 			goto drop;
54830974Skarels 		if (tiflags & TH_ACK) {
54930974Skarels 			tp->snd_una = ti->ti_ack;
55030974Skarels 			if (SEQ_LT(tp->snd_nxt, tp->snd_una))
55130974Skarels 				tp->snd_nxt = tp->snd_una;
55230974Skarels 		}
5535244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
5545065Swnj 		tp->irs = ti->ti_seq;
5555085Swnj 		tcp_rcvseqinit(tp);
5565085Swnj 		tp->t_flags |= TF_ACKNOW;
55730974Skarels 		if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
55830525Skarels 			tcpstat.tcps_connects++;
5595244Sroot 			soisconnected(so);
5605065Swnj 			tp->t_state = TCPS_ESTABLISHED;
56144375Skarels 			(void) tcp_reass(tp, (struct tcpiphdr *)0,
56244375Skarels 				(struct mbuf *)0);
56332098Skarels 			/*
56432098Skarels 			 * if we didn't have to retransmit the SYN,
56532098Skarels 			 * use its rtt as our initial srtt & rtt var.
56632098Skarels 			 */
56744375Skarels 			if (tp->t_rtt)
56844375Skarels 				tcp_xmit_timer(tp);
5695162Swnj 		} else
5705085Swnj 			tp->t_state = TCPS_SYN_RECEIVED;
5715085Swnj 
5725085Swnj trimthenstep6:
5735085Swnj 		/*
5745231Swnj 		 * Advance ti->ti_seq to correspond to first data byte.
5755085Swnj 		 * If data, trim to stay within window,
5765085Swnj 		 * dropping FIN if necessary.
5775085Swnj 		 */
5785231Swnj 		ti->ti_seq++;
5795085Swnj 		if (ti->ti_len > tp->rcv_wnd) {
5805085Swnj 			todrop = ti->ti_len - tp->rcv_wnd;
5815085Swnj 			m_adj(m, -todrop);
5825085Swnj 			ti->ti_len = tp->rcv_wnd;
58325939Skarels 			tiflags &= ~TH_FIN;
58430525Skarels 			tcpstat.tcps_rcvpackafterwin++;
58530525Skarels 			tcpstat.tcps_rcvbyteafterwin += todrop;
5865065Swnj 		}
5875263Swnj 		tp->snd_wl1 = ti->ti_seq - 1;
58825939Skarels 		tp->rcv_up = ti->ti_seq;
5895085Swnj 		goto step6;
59044375Skarels #ifdef VAN
59144375Skarels 
59244375Skarels 	default:
59344375Skarels 		if (om) {
59444375Skarels 			tcp_dooptions(tp, om, ti);
59544375Skarels 			om = 0;
59644375Skarels 		}
59744375Skarels #endif VAN
5985065Swnj 	}
5994601Swnj 
6005065Swnj 	/*
60130525Skarels 	 * States other than LISTEN or SYN_SENT.
60230525Skarels 	 * First check that at least some bytes of segment are within
60330525Skarels 	 * receive window.  If segment begins before rcv_nxt,
60430525Skarels 	 * drop leading data (and SYN); if nothing left, just ack.
60516222Skarels 	 */
60630525Skarels 	todrop = tp->rcv_nxt - ti->ti_seq;
60730525Skarels 	if (todrop > 0) {
60830525Skarels 		if (tiflags & TH_SYN) {
60930525Skarels 			tiflags &= ~TH_SYN;
61030525Skarels 			ti->ti_seq++;
61130525Skarels 			if (ti->ti_urp > 1)
61230525Skarels 				ti->ti_urp--;
61330525Skarels 			else
61430525Skarels 				tiflags &= ~TH_URG;
61530525Skarels 			todrop--;
61630525Skarels 		}
61730525Skarels 		if (todrop > ti->ti_len ||
61830525Skarels 		    todrop == ti->ti_len && (tiflags&TH_FIN) == 0) {
61933745Skarels 			tcpstat.tcps_rcvduppack++;
62033745Skarels 			tcpstat.tcps_rcvdupbyte += ti->ti_len;
62131730Skarels 			/*
62233745Skarels 			 * If segment is just one to the left of the window,
62333745Skarels 			 * check two special cases:
62433745Skarels 			 * 1. Don't toss RST in response to 4.2-style keepalive.
62533745Skarels 			 * 2. If the only thing to drop is a FIN, we can drop
62633745Skarels 			 *    it, but check the ACK or we will get into FIN
62733745Skarels 			 *    wars if our FINs crossed (both CLOSING).
62833745Skarels 			 * In either case, send ACK to resynchronize,
62933745Skarels 			 * but keep on processing for RST or ACK.
63031730Skarels 			 */
63133745Skarels 			if ((tiflags & TH_FIN && todrop == ti->ti_len + 1)
63233745Skarels #ifdef TCP_COMPAT_42
63333745Skarels 			  || (tiflags & TH_RST && ti->ti_seq == tp->rcv_nxt - 1)
63431730Skarels #endif
63533745Skarels 			   ) {
63633745Skarels 				todrop = ti->ti_len;
63733745Skarels 				tiflags &= ~TH_FIN;
63833745Skarels 				tp->t_flags |= TF_ACKNOW;
63933745Skarels 			} else
64033745Skarels 				goto dropafterack;
64132034Skarels 		} else {
64232034Skarels 			tcpstat.tcps_rcvpartduppack++;
64332034Skarels 			tcpstat.tcps_rcvpartdupbyte += todrop;
64430525Skarels 		}
64530525Skarels 		m_adj(m, todrop);
64630525Skarels 		ti->ti_seq += todrop;
64730525Skarels 		ti->ti_len -= todrop;
64830525Skarels 		if (ti->ti_urp > todrop)
64930525Skarels 			ti->ti_urp -= todrop;
65030525Skarels 		else {
65130525Skarels 			tiflags &= ~TH_URG;
65230525Skarels 			ti->ti_urp = 0;
65330525Skarels 		}
65416222Skarels 	}
65516222Skarels 
65632612Skarels 	/*
65733745Skarels 	 * If new data are received on a connection after the
65832612Skarels 	 * user processes are gone, then RST the other end.
65932612Skarels 	 */
66032612Skarels 	if ((so->so_state & SS_NOFDREF) &&
66132612Skarels 	    tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
66232612Skarels 		tp = tcp_close(tp);
66332612Skarels 		tcpstat.tcps_rcvafterclose++;
66432612Skarels 		goto dropwithreset;
66532612Skarels 	}
66632612Skarels 
66733445Skarels 	/*
66833445Skarels 	 * If segment ends after window, drop trailing data
66933445Skarels 	 * (and PUSH and FIN); if nothing left, just ACK.
67033445Skarels 	 */
67133445Skarels 	todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
67233445Skarels 	if (todrop > 0) {
67333445Skarels 		tcpstat.tcps_rcvpackafterwin++;
67433445Skarels 		if (todrop >= ti->ti_len) {
67530525Skarels 			tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
67633445Skarels 			/*
67733445Skarels 			 * If a new connection request is received
67833445Skarels 			 * while in TIME_WAIT, drop the old connection
67933445Skarels 			 * and start over if the sequence numbers
68033445Skarels 			 * are above the previous ones.
68133445Skarels 			 */
68233445Skarels 			if (tiflags & TH_SYN &&
68333445Skarels 			    tp->t_state == TCPS_TIME_WAIT &&
68433445Skarels 			    SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
68533445Skarels 				iss = tp->rcv_nxt + TCP_ISSINCR;
68644375Skarels 				tp = tcp_close(tp);
68733445Skarels 				goto findpcb;
68833445Skarels 			}
68933445Skarels 			/*
69033445Skarels 			 * If window is closed can only take segments at
69133445Skarels 			 * window edge, and have to drop data and PUSH from
69233445Skarels 			 * incoming segments.  Continue processing, but
69333445Skarels 			 * remember to ack.  Otherwise, drop segment
69433445Skarels 			 * and ack.
69533445Skarels 			 */
69633445Skarels 			if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
69733445Skarels 				tp->t_flags |= TF_ACKNOW;
69830525Skarels 				tcpstat.tcps_rcvwinprobe++;
69933445Skarels 			} else
7005065Swnj 				goto dropafterack;
70133445Skarels 		} else
70230525Skarels 			tcpstat.tcps_rcvbyteafterwin += todrop;
70333445Skarels 		m_adj(m, -todrop);
70433445Skarels 		ti->ti_len -= todrop;
70533445Skarels 		tiflags &= ~(TH_PUSH|TH_FIN);
7065065Swnj 	}
7074601Swnj 
7085065Swnj 	/*
7095065Swnj 	 * If the RST bit is set examine the state:
7105065Swnj 	 *    SYN_RECEIVED STATE:
7115065Swnj 	 *	If passive open, return to LISTEN state.
7125065Swnj 	 *	If active open, inform user that connection was refused.
7135065Swnj 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
7145065Swnj 	 *	Inform user that connection was reset, and close tcb.
7155065Swnj 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
7165065Swnj 	 *	Close the tcb.
7175065Swnj 	 */
7185065Swnj 	if (tiflags&TH_RST) switch (tp->t_state) {
7195267Sroot 
7205065Swnj 	case TCPS_SYN_RECEIVED:
72133745Skarels 		so->so_error = ECONNREFUSED;
72233745Skarels 		goto close;
7234601Swnj 
7245065Swnj 	case TCPS_ESTABLISHED:
7255065Swnj 	case TCPS_FIN_WAIT_1:
7265065Swnj 	case TCPS_FIN_WAIT_2:
7275065Swnj 	case TCPS_CLOSE_WAIT:
72833745Skarels 		so->so_error = ECONNRESET;
72933745Skarels 	close:
73033745Skarels 		tp->t_state = TCPS_CLOSED;
73133745Skarels 		tcpstat.tcps_drops++;
73233745Skarels 		tp = tcp_close(tp);
7335065Swnj 		goto drop;
7345065Swnj 
7355065Swnj 	case TCPS_CLOSING:
7365065Swnj 	case TCPS_LAST_ACK:
7375065Swnj 	case TCPS_TIME_WAIT:
73810394Ssam 		tp = tcp_close(tp);
7395065Swnj 		goto drop;
7404601Swnj 	}
7414601Swnj 
7424601Swnj 	/*
7435065Swnj 	 * If a SYN is in the window, then this is an
7445065Swnj 	 * error and we send an RST and drop the connection.
7454601Swnj 	 */
7465065Swnj 	if (tiflags & TH_SYN) {
74710394Ssam 		tp = tcp_drop(tp, ECONNRESET);
7485085Swnj 		goto dropwithreset;
7494601Swnj 	}
7504601Swnj 
7514601Swnj 	/*
7525065Swnj 	 * If the ACK bit is off we drop the segment and return.
7534601Swnj 	 */
7545085Swnj 	if ((tiflags & TH_ACK) == 0)
7555065Swnj 		goto drop;
7565065Swnj 
7575065Swnj 	/*
7585065Swnj 	 * Ack processing.
7595065Swnj 	 */
7604601Swnj 	switch (tp->t_state) {
7614601Swnj 
7625065Swnj 	/*
7635065Swnj 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
76431721Skarels 	 * ESTABLISHED state and continue processing, otherwise
7655065Swnj 	 * send an RST.
7665065Swnj 	 */
7675065Swnj 	case TCPS_SYN_RECEIVED:
7685085Swnj 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
7695231Swnj 		    SEQ_GT(ti->ti_ack, tp->snd_max))
7705085Swnj 			goto dropwithreset;
77130525Skarels 		tcpstat.tcps_connects++;
7725085Swnj 		soisconnected(so);
7735085Swnj 		tp->t_state = TCPS_ESTABLISHED;
77444375Skarels 		(void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0);
7755244Sroot 		tp->snd_wl1 = ti->ti_seq - 1;
7765085Swnj 		/* fall into ... */
7774601Swnj 
7785065Swnj 	/*
7795065Swnj 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
7805065Swnj 	 * ACKs.  If the ack is in the range
7815231Swnj 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
7825065Swnj 	 * then advance tp->snd_una to ti->ti_ack and drop
7835065Swnj 	 * data from the retransmission queue.  If this ACK reflects
7845065Swnj 	 * more up to date window information we update our window information.
7855065Swnj 	 */
7865065Swnj 	case TCPS_ESTABLISHED:
7875065Swnj 	case TCPS_FIN_WAIT_1:
7885065Swnj 	case TCPS_FIN_WAIT_2:
7895065Swnj 	case TCPS_CLOSE_WAIT:
7905065Swnj 	case TCPS_CLOSING:
7915244Sroot 	case TCPS_LAST_ACK:
7925244Sroot 	case TCPS_TIME_WAIT:
7935085Swnj 
79430525Skarels 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
79532098Skarels 			if (ti->ti_len == 0 && ti->ti_win == tp->snd_wnd) {
79630525Skarels 				tcpstat.tcps_rcvdupack++;
79732098Skarels 				/*
79844375Skarels 				 * If we have outstanding data (other than
79944375Skarels 				 * a window probe), this is a completely
80032098Skarels 				 * duplicate ack (ie, window info didn't
80132098Skarels 				 * change), the ack is the biggest we've
80232098Skarels 				 * seen and we've seen exactly our rexmt
80332098Skarels 				 * threshhold of them, assume a packet
80432098Skarels 				 * has been dropped and retransmit it.
80532098Skarels 				 * Kludge snd_nxt & the congestion
80632098Skarels 				 * window so we send only this one
80744375Skarels 				 * packet.
80844375Skarels 				 *
80944375Skarels 				 * We know we're losing at the current
81044375Skarels 				 * window size so do congestion avoidance
81144375Skarels 				 * (set ssthresh to half the current window
81244375Skarels 				 * and pull our congestion window back to
81344375Skarels 				 * the new ssthresh).
81444375Skarels 				 *
81544375Skarels 				 * Dup acks mean that packets have left the
81644375Skarels 				 * network (they're now cached at the receiver)
81744375Skarels 				 * so bump cwnd by the amount in the receiver
81844375Skarels 				 * to keep a constant cwnd packets in the
81944375Skarels 				 * network.
82032098Skarels 				 */
82132098Skarels 				if (tp->t_timer[TCPT_REXMT] == 0 ||
82232098Skarels 				    ti->ti_ack != tp->snd_una)
82332098Skarels 					tp->t_dupacks = 0;
82432098Skarels 				else if (++tp->t_dupacks == tcprexmtthresh) {
82532098Skarels 					tcp_seq onxt = tp->snd_nxt;
82632376Skarels 					u_int win =
82737320Skarels 					    min(tp->snd_wnd, tp->snd_cwnd) / 2 /
82832376Skarels 						tp->t_maxseg;
82932098Skarels 
83032376Skarels 					if (win < 2)
83132376Skarels 						win = 2;
83232376Skarels 					tp->snd_ssthresh = win * tp->t_maxseg;
83332098Skarels 					tp->t_timer[TCPT_REXMT] = 0;
83432098Skarels 					tp->t_rtt = 0;
83532098Skarels 					tp->snd_nxt = ti->ti_ack;
83632098Skarels 					tp->snd_cwnd = tp->t_maxseg;
83732098Skarels 					(void) tcp_output(tp);
83844375Skarels 					tp->snd_cwnd = tp->snd_ssthresh +
83944375Skarels 					       tp->t_maxseg * tp->t_dupacks;
84032098Skarels 					if (SEQ_GT(onxt, tp->snd_nxt))
84132098Skarels 						tp->snd_nxt = onxt;
84232098Skarels 					goto drop;
84344375Skarels 				} else if (tp->t_dupacks > tcprexmtthresh) {
84444375Skarels 					tp->snd_cwnd += tp->t_maxseg;
84544375Skarels 					(void) tcp_output(tp);
84644375Skarels 					goto drop;
84732098Skarels 				}
84832098Skarels 			} else
84932098Skarels 				tp->t_dupacks = 0;
8505065Swnj 			break;
85130525Skarels 		}
85244375Skarels 		/*
85344375Skarels 		 * If the congestion window was inflated to account
85444375Skarels 		 * for the other side's cached packets, retract it.
85544375Skarels 		 */
85644375Skarels 		if (tp->t_dupacks > tcprexmtthresh &&
85744375Skarels 		    tp->snd_cwnd > tp->snd_ssthresh)
85844375Skarels 			tp->snd_cwnd = tp->snd_ssthresh;
85932098Skarels 		tp->t_dupacks = 0;
86030525Skarels 		if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
86130525Skarels 			tcpstat.tcps_rcvacktoomuch++;
8625065Swnj 			goto dropafterack;
86330525Skarels 		}
8645085Swnj 		acked = ti->ti_ack - tp->snd_una;
86530525Skarels 		tcpstat.tcps_rcvackpack++;
86630525Skarels 		tcpstat.tcps_rcvackbyte += acked;
8675951Swnj 
8685951Swnj 		/*
8695951Swnj 		 * If transmit timer is running and timed sequence
8705951Swnj 		 * number was acked, update smoothed round trip time.
87132034Skarels 		 * Since we now have an rtt measurement, cancel the
87232034Skarels 		 * timer backoff (cf., Phil Karn's retransmit alg.).
87332034Skarels 		 * Recompute the initial retransmit timer.
8745951Swnj 		 */
87544375Skarels 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
87644375Skarels 			tcp_xmit_timer(tp);
87731726Skarels 
87826824Skarels 		/*
87926824Skarels 		 * If all outstanding data is acked, stop retransmit
88026824Skarels 		 * timer and remember to restart (more output or persist).
88126824Skarels 		 * If there is more data to be acked, restart retransmit
88232034Skarels 		 * timer, using current (possibly backed-off) value.
88326824Skarels 		 */
88426824Skarels 		if (ti->ti_ack == tp->snd_max) {
8855244Sroot 			tp->t_timer[TCPT_REXMT] = 0;
88626824Skarels 			needoutput = 1;
88732034Skarels 		} else if (tp->t_timer[TCPT_PERSIST] == 0)
88832034Skarels 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
88917360Skarels 		/*
89032098Skarels 		 * When new data is acked, open the congestion window.
89132098Skarels 		 * If the window gives us less than ssthresh packets
89232098Skarels 		 * in flight, open exponentially (maxseg per packet).
89344375Skarels 		 * Otherwise open linearly: maxseg per window
89444375Skarels 		 * (maxseg^2 / cwnd per packet), plus a constant
89544375Skarels 		 * fraction of a packet (maxseg/8) to help larger windows
89644375Skarels 		 * open quickly enough.
89717360Skarels 		 */
89832098Skarels 		{
89944375Skarels 		register u_int cw = tp->snd_cwnd;
90044375Skarels 		register u_int incr = tp->t_maxseg;
90132098Skarels 
90244375Skarels 		if (cw > tp->snd_ssthresh)
90344375Skarels 			incr = incr * incr / cw + incr / 8;
90444375Skarels 		tp->snd_cwnd = min(cw + incr, TCP_MAXWIN);
90532098Skarels 		}
9065307Sroot 		if (acked > so->so_snd.sb_cc) {
90715386Ssam 			tp->snd_wnd -= so->so_snd.sb_cc;
90826386Skarels 			sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
90931721Skarels 			ourfinisacked = 1;
9105307Sroot 		} else {
9116161Ssam 			sbdrop(&so->so_snd, acked);
9125307Sroot 			tp->snd_wnd -= acked;
91331721Skarels 			ourfinisacked = 0;
9145307Sroot 		}
91544375Skarels 		if (so->so_snd.sb_flags & SB_NOTIFY)
91644375Skarels 			sowwakeup(so);
9175231Swnj 		tp->snd_una = ti->ti_ack;
9185357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
9195357Sroot 			tp->snd_nxt = tp->snd_una;
9205162Swnj 
9214601Swnj 		switch (tp->t_state) {
9224601Swnj 
9235065Swnj 		/*
9245065Swnj 		 * In FIN_WAIT_1 STATE in addition to the processing
9255065Swnj 		 * for the ESTABLISHED state if our FIN is now acknowledged
9265085Swnj 		 * then enter FIN_WAIT_2.
9275065Swnj 		 */
9285065Swnj 		case TCPS_FIN_WAIT_1:
9295896Swnj 			if (ourfinisacked) {
9305896Swnj 				/*
9315896Swnj 				 * If we can't receive any more
9325896Swnj 				 * data, then closing user can proceed.
93324816Skarels 				 * Starting the timer is contrary to the
93424816Skarels 				 * specification, but if we don't get a FIN
93524816Skarels 				 * we'll hang forever.
9365896Swnj 				 */
93724816Skarels 				if (so->so_state & SS_CANTRCVMORE) {
9385896Swnj 					soisdisconnected(so);
93933745Skarels 					tp->t_timer[TCPT_2MSL] = tcp_maxidle;
94024816Skarels 				}
9415085Swnj 				tp->t_state = TCPS_FIN_WAIT_2;
9425896Swnj 			}
9434601Swnj 			break;
9444601Swnj 
9455065Swnj 	 	/*
9465065Swnj 		 * In CLOSING STATE in addition to the processing for
9475065Swnj 		 * the ESTABLISHED state if the ACK acknowledges our FIN
9485065Swnj 		 * then enter the TIME-WAIT state, otherwise ignore
9495065Swnj 		 * the segment.
9505065Swnj 		 */
9515065Swnj 		case TCPS_CLOSING:
9525244Sroot 			if (ourfinisacked) {
9535065Swnj 				tp->t_state = TCPS_TIME_WAIT;
9545244Sroot 				tcp_canceltimers(tp);
9555244Sroot 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
9565244Sroot 				soisdisconnected(so);
9575244Sroot 			}
9585244Sroot 			break;
9594601Swnj 
9605065Swnj 		/*
96131743Skarels 		 * In LAST_ACK, we may still be waiting for data to drain
96231743Skarels 		 * and/or to be acked, as well as for the ack of our FIN.
96331743Skarels 		 * If our FIN is now acknowledged, delete the TCB,
96431743Skarels 		 * enter the closed state and return.
9655065Swnj 		 */
9665065Swnj 		case TCPS_LAST_ACK:
96731743Skarels 			if (ourfinisacked) {
96810394Ssam 				tp = tcp_close(tp);
96931743Skarels 				goto drop;
97031743Skarels 			}
97131743Skarels 			break;
9724601Swnj 
9735065Swnj 		/*
9745065Swnj 		 * In TIME_WAIT state the only thing that should arrive
9755065Swnj 		 * is a retransmission of the remote FIN.  Acknowledge
9765065Swnj 		 * it and restart the finack timer.
9775065Swnj 		 */
9785065Swnj 		case TCPS_TIME_WAIT:
9795162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
9805065Swnj 			goto dropafterack;
9814601Swnj 		}
9825085Swnj 	}
9834601Swnj 
9845065Swnj step6:
9855065Swnj 	/*
9865244Sroot 	 * Update window information.
98725939Skarels 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
9885244Sroot 	 */
98925939Skarels 	if ((tiflags & TH_ACK) &&
99025939Skarels 	    (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
9915391Swnj 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
99225939Skarels 	     tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) {
99330525Skarels 		/* keep track of pure window updates */
99430525Skarels 		if (ti->ti_len == 0 &&
99532098Skarels 		    tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)
99630525Skarels 			tcpstat.tcps_rcvwinupd++;
9975244Sroot 		tp->snd_wnd = ti->ti_win;
9985244Sroot 		tp->snd_wl1 = ti->ti_seq;
9995244Sroot 		tp->snd_wl2 = ti->ti_ack;
100025260Skarels 		if (tp->snd_wnd > tp->max_sndwnd)
100125260Skarels 			tp->max_sndwnd = tp->snd_wnd;
100226824Skarels 		needoutput = 1;
100326824Skarels 	}
10045244Sroot 
10055244Sroot 	/*
10065547Swnj 	 * Process segments with URG.
10075065Swnj 	 */
10087267Swnj 	if ((tiflags & TH_URG) && ti->ti_urp &&
10097267Swnj 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
10105547Swnj 		/*
101125939Skarels 		 * This is a kludge, but if we receive and accept
101213121Ssam 		 * random urgent pointers, we'll crash in
101313121Ssam 		 * soreceive.  It's hard to imagine someone
101413121Ssam 		 * actually wanting to send this much urgent data.
101512441Ssam 		 */
101625652Skarels 		if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) {
101712441Ssam 			ti->ti_urp = 0;			/* XXX */
101812441Ssam 			tiflags &= ~TH_URG;		/* XXX */
101925939Skarels 			goto dodata;			/* XXX */
102012441Ssam 		}
102112441Ssam 		/*
10225547Swnj 		 * If this segment advances the known urgent pointer,
10235547Swnj 		 * then mark the data stream.  This should not happen
10245547Swnj 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
10255547Swnj 		 * a FIN has been received from the remote side.
10265547Swnj 		 * In these states we ignore the URG.
102727190Skarels 		 *
102827190Skarels 		 * According to RFC961 (Assigned Protocols),
102927190Skarels 		 * the urgent pointer points to the last octet
103027190Skarels 		 * of urgent data.  We continue, however,
103127190Skarels 		 * to consider it to indicate the first octet
103244375Skarels 		 * of data past the urgent section as the original
103344375Skarels 		 * spec states (in one of two places).
10345547Swnj 		 */
10355547Swnj 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
10365547Swnj 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
10375547Swnj 			so->so_oobmark = so->so_rcv.sb_cc +
10385547Swnj 			    (tp->rcv_up - tp->rcv_nxt) - 1;
10395547Swnj 			if (so->so_oobmark == 0)
10405547Swnj 				so->so_state |= SS_RCVATMARK;
10418313Sroot 			sohasoutofband(so);
104224816Skarels 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
10435440Swnj 		}
10445547Swnj 		/*
10455547Swnj 		 * Remove out of band data so doesn't get presented to user.
10465547Swnj 		 * This can happen independent of advancing the URG pointer,
10475547Swnj 		 * but if two URG's are pending at once, some out-of-band
10485547Swnj 		 * data may creep in... ick.
10495547Swnj 		 */
105044375Skarels 		if (ti->ti_urp <= ti->ti_len
105144375Skarels #ifdef SO_OOBINLINE
105244375Skarels 		     && (so->so_options & SO_OOBINLINE) == 0
105344375Skarels #endif
105444375Skarels 		     )
105544375Skarels 			tcp_pulloutofband(so, ti, m);
105625939Skarels 	} else
105725939Skarels 		/*
105825939Skarels 		 * If no out of band data is expected,
105925939Skarels 		 * pull receive urgent pointer along
106025939Skarels 		 * with the receive window.
106125939Skarels 		 */
106225939Skarels 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
106325939Skarels 			tp->rcv_up = tp->rcv_nxt;
106425939Skarels dodata:							/* XXX */
10654601Swnj 
10664601Swnj 	/*
10675065Swnj 	 * Process the segment text, merging it into the TCP sequencing queue,
10685065Swnj 	 * and arranging for acknowledgment of receipt if necessary.
10695065Swnj 	 * This process logically involves adjusting tp->rcv_wnd as data
10705065Swnj 	 * is presented to the user (this happens in tcp_usrreq.c,
10715065Swnj 	 * case PRU_RCVD).  If a FIN has already been received on this
10725065Swnj 	 * connection then we just ignore the text.
10734601Swnj 	 */
107417946Skarels 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
107517946Skarels 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
107624816Skarels 		TCP_REASS(tp, ti, m, so, tiflags);
107725260Skarels 		/*
107825260Skarels 		 * Note the amount of data that peer has sent into
107925260Skarels 		 * our window, in order to estimate the sender's
108025260Skarels 		 * buffer size.
108125260Skarels 		 */
108232098Skarels 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
10835244Sroot 	} else {
10844924Swnj 		m_freem(m);
10855263Swnj 		tiflags &= ~TH_FIN;
10865244Sroot 	}
10874601Swnj 
10884601Swnj 	/*
10895263Swnj 	 * If FIN is received ACK the FIN and let the user know
10905263Swnj 	 * that the connection is closing.
10914601Swnj 	 */
10925263Swnj 	if (tiflags & TH_FIN) {
10935244Sroot 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
10945244Sroot 			socantrcvmore(so);
10955244Sroot 			tp->t_flags |= TF_ACKNOW;
10965244Sroot 			tp->rcv_nxt++;
10975244Sroot 		}
10985065Swnj 		switch (tp->t_state) {
10994601Swnj 
11005065Swnj 	 	/*
11015065Swnj 		 * In SYN_RECEIVED and ESTABLISHED STATES
11025065Swnj 		 * enter the CLOSE_WAIT state.
11034884Swnj 		 */
11045065Swnj 		case TCPS_SYN_RECEIVED:
11055065Swnj 		case TCPS_ESTABLISHED:
11065065Swnj 			tp->t_state = TCPS_CLOSE_WAIT;
11075065Swnj 			break;
11084884Swnj 
11095065Swnj 	 	/*
11105085Swnj 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
11115085Swnj 		 * enter the CLOSING state.
11124884Swnj 		 */
11135065Swnj 		case TCPS_FIN_WAIT_1:
11145085Swnj 			tp->t_state = TCPS_CLOSING;
11155065Swnj 			break;
11164601Swnj 
11175065Swnj 	 	/*
11185065Swnj 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
11195065Swnj 		 * starting the time-wait timer, turning off the other
11205065Swnj 		 * standard timers.
11215065Swnj 		 */
11225065Swnj 		case TCPS_FIN_WAIT_2:
11235244Sroot 			tp->t_state = TCPS_TIME_WAIT;
11245074Swnj 			tcp_canceltimers(tp);
11255162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
11265244Sroot 			soisdisconnected(so);
11275065Swnj 			break;
11285065Swnj 
11294884Swnj 		/*
11305065Swnj 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
11314884Swnj 		 */
11325065Swnj 		case TCPS_TIME_WAIT:
11335162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
11345065Swnj 			break;
11355085Swnj 		}
11364601Swnj 	}
11375267Sroot 	if (so->so_options & SO_DEBUG)
11385267Sroot 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
11395085Swnj 
11405085Swnj 	/*
11415085Swnj 	 * Return any desired output.
11425085Swnj 	 */
114326824Skarels 	if (needoutput || (tp->t_flags & TF_ACKNOW))
114425939Skarels 		(void) tcp_output(tp);
11455065Swnj 	return;
11465085Swnj 
11475065Swnj dropafterack:
11485085Swnj 	/*
11496211Swnj 	 * Generate an ACK dropping incoming segment if it occupies
11506211Swnj 	 * sequence space, where the ACK reflects our state.
11515085Swnj 	 */
115226057Skarels 	if (tiflags & TH_RST)
11535085Swnj 		goto drop;
115431749Skarels 	m_freem(m);
115531721Skarels 	tp->t_flags |= TF_ACKNOW;
115631721Skarels 	(void) tcp_output(tp);
11575231Swnj 	return;
11585085Swnj 
11595085Swnj dropwithreset:
116011731Ssam 	if (om) {
11616161Ssam 		(void) m_free(om);
116211731Ssam 		om = 0;
116311731Ssam 	}
11645085Swnj 	/*
11655244Sroot 	 * Generate a RST, dropping incoming segment.
11665085Swnj 	 * Make ACK acceptable to originator of segment.
116725197Skarels 	 * Don't bother to respond if destination was broadcast.
11685085Swnj 	 */
116937320Skarels 	if ((tiflags & TH_RST) || m->m_flags & M_BCAST)
11705085Swnj 		goto drop;
11715085Swnj 	if (tiflags & TH_ACK)
117237320Skarels 		tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
11735085Swnj 	else {
11745085Swnj 		if (tiflags & TH_SYN)
11755085Swnj 			ti->ti_len++;
117637320Skarels 		tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
11776211Swnj 		    TH_RST|TH_ACK);
11785085Swnj 	}
117910769Ssam 	/* destroy temporarily created socket */
118010769Ssam 	if (dropsocket)
118110769Ssam 		(void) soabort(so);
11825231Swnj 	return;
11835085Swnj 
11845065Swnj drop:
118511730Ssam 	if (om)
118611730Ssam 		(void) m_free(om);
11875085Swnj 	/*
11885085Swnj 	 * Drop space held by incoming segment and return.
11895085Swnj 	 */
11906303Sroot 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
11916303Sroot 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
11925065Swnj 	m_freem(m);
119310769Ssam 	/* destroy temporarily created socket */
119410769Ssam 	if (dropsocket)
119510769Ssam 		(void) soabort(so);
11965267Sroot 	return;
11975065Swnj }
11985065Swnj 
119917272Skarels tcp_dooptions(tp, om, ti)
12005440Swnj 	struct tcpcb *tp;
12015440Swnj 	struct mbuf *om;
120217272Skarels 	struct tcpiphdr *ti;
12035419Swnj {
12045440Swnj 	register u_char *cp;
120544375Skarels 	u_short mss;
12065440Swnj 	int opt, optlen, cnt;
12075419Swnj 
12085440Swnj 	cp = mtod(om, u_char *);
12095440Swnj 	cnt = om->m_len;
12105440Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
12115440Swnj 		opt = cp[0];
12125440Swnj 		if (opt == TCPOPT_EOL)
12135440Swnj 			break;
12145440Swnj 		if (opt == TCPOPT_NOP)
12155440Swnj 			optlen = 1;
121612169Ssam 		else {
12175440Swnj 			optlen = cp[1];
121812169Ssam 			if (optlen <= 0)
121912169Ssam 				break;
122012169Ssam 		}
12215440Swnj 		switch (opt) {
12225440Swnj 
12235440Swnj 		default:
122444375Skarels 			continue;
12255440Swnj 
12265440Swnj 		case TCPOPT_MAXSEG:
12275440Swnj 			if (optlen != 4)
12285440Swnj 				continue;
122917272Skarels 			if (!(ti->ti_flags & TH_SYN))
123017272Skarels 				continue;
123144375Skarels 			bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
123244375Skarels 			NTOHS(mss);
123344375Skarels 			(void) tcp_mss(tp, mss);	/* sets t_maxseg */
12345440Swnj 			break;
12355419Swnj 		}
12365419Swnj 	}
12376161Ssam 	(void) m_free(om);
12385419Swnj }
12395419Swnj 
12405419Swnj /*
12415547Swnj  * Pull out of band byte out of a segment so
12425547Swnj  * it doesn't appear in the user's data queue.
12435547Swnj  * It is still reflected in the segment length for
12445547Swnj  * sequencing purposes.
12455547Swnj  */
124644375Skarels tcp_pulloutofband(so, ti, m)
12475547Swnj 	struct socket *so;
12485547Swnj 	struct tcpiphdr *ti;
124944375Skarels 	register struct mbuf *m;
12505547Swnj {
12516116Swnj 	int cnt = ti->ti_urp - 1;
12525547Swnj 
12535547Swnj 	while (cnt >= 0) {
12545547Swnj 		if (m->m_len > cnt) {
12555547Swnj 			char *cp = mtod(m, caddr_t) + cnt;
12565547Swnj 			struct tcpcb *tp = sototcpcb(so);
12575547Swnj 
12585547Swnj 			tp->t_iobc = *cp;
12595547Swnj 			tp->t_oobflags |= TCPOOB_HAVEDATA;
12606161Ssam 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
12615547Swnj 			m->m_len--;
12625547Swnj 			return;
12635547Swnj 		}
12645547Swnj 		cnt -= m->m_len;
12655547Swnj 		m = m->m_next;
12665547Swnj 		if (m == 0)
12675547Swnj 			break;
12685547Swnj 	}
12695547Swnj 	panic("tcp_pulloutofband");
12705547Swnj }
12715547Swnj 
12725547Swnj /*
127344375Skarels  * Collect new round-trip time estimate
127444375Skarels  * and update averages and current timeout.
127517272Skarels  */
127644375Skarels tcp_xmit_timer(tp)
127723975Skarels 	register struct tcpcb *tp;
127817272Skarels {
127944375Skarels 	register short delta;
128044375Skarels 
128144375Skarels 	tcpstat.tcps_rttupdated++;
128244375Skarels 	if (tp->t_srtt != 0) {
128344375Skarels 		/*
128444375Skarels 		 * srtt is stored as fixed point with 3 bits after the
128544375Skarels 		 * binary point (i.e., scaled by 8).  The following magic
128644375Skarels 		 * is equivalent to the smoothing algorithm in rfc793 with
128744375Skarels 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
128844375Skarels 		 * point).  Adjust t_rtt to origin 0.
128944375Skarels 		 */
129044375Skarels 		delta = tp->t_rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT);
129144375Skarels 		if ((tp->t_srtt += delta) <= 0)
129244375Skarels 			tp->t_srtt = 1;
129344375Skarels 		/*
129444375Skarels 		 * We accumulate a smoothed rtt variance (actually, a
129544375Skarels 		 * smoothed mean difference), then set the retransmit
129644375Skarels 		 * timer to smoothed rtt + 4 times the smoothed variance.
129744375Skarels 		 * rttvar is stored as fixed point with 2 bits after the
129844375Skarels 		 * binary point (scaled by 4).  The following is
129944375Skarels 		 * equivalent to rfc793 smoothing with an alpha of .75
130044375Skarels 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
130144375Skarels 		 * rfc793's wired-in beta.
130244375Skarels 		 */
130344375Skarels 		if (delta < 0)
130444375Skarels 			delta = -delta;
130544375Skarels 		delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
130644375Skarels 		if ((tp->t_rttvar += delta) <= 0)
130744375Skarels 			tp->t_rttvar = 1;
130844375Skarels 	} else {
130944375Skarels 		/*
131044375Skarels 		 * No rtt measurement yet - use the unsmoothed rtt.
131144375Skarels 		 * Set the variance to half the rtt (so our first
131244375Skarels 		 * retransmit happens at 2*rtt)
131344375Skarels 		 */
131444375Skarels 		tp->t_srtt = tp->t_rtt << TCP_RTT_SHIFT;
131544375Skarels 		tp->t_rttvar = tp->t_rtt << (TCP_RTTVAR_SHIFT - 1);
131644375Skarels 	}
131744375Skarels 	tp->t_rtt = 0;
131844375Skarels 	tp->t_rxtshift = 0;
131944375Skarels 
132044375Skarels 	/*
132144375Skarels 	 * the retransmit should happen at rtt + 4 * rttvar.
132244375Skarels 	 * Because of the way we do the smoothing, srtt and rttvar
132344375Skarels 	 * will each average +1/2 tick of bias.  When we compute
132444375Skarels 	 * the retransmit timer, we want 1/2 tick of rounding and
132544375Skarels 	 * 1 extra tick because of +-1/2 tick uncertainty in the
132644375Skarels 	 * firing of the timer.  The bias will give us exactly the
132744375Skarels 	 * 1.5 tick we need.  But, because the bias is
132844375Skarels 	 * statistical, we have to test that we don't drop below
132944375Skarels 	 * the minimum feasible timer (which is 2 ticks).
133044375Skarels 	 */
133144375Skarels 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
133244375Skarels 	    tp->t_rttmin, TCPTV_REXMTMAX);
133344375Skarels 
133444375Skarels 	/*
133544375Skarels 	 * We received an ack for a packet that wasn't retransmitted;
133644375Skarels 	 * it is probably safe to discard any error indications we've
133744375Skarels 	 * received recently.  This isn't quite right, but close enough
133844375Skarels 	 * for now (a route might have failed after we sent a segment,
133944375Skarels 	 * and the return path might not be symmetrical).
134044375Skarels 	 */
134144375Skarels 	tp->t_softerror = 0;
134244375Skarels }
134344375Skarels 
134444375Skarels /*
134544375Skarels  * Determine a reasonable value for maxseg size.
134644375Skarels  * If the route is known, check route for mtu.
134744375Skarels  * If none, use an mss that can be handled on the outgoing
134844375Skarels  * interface without forcing IP to fragment; if bigger than
134944375Skarels  * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
135044375Skarels  * to utilize large mbufs.  If no route is found, route has no mtu,
135144375Skarels  * or the destination isn't local, use a default, hopefully conservative
135244375Skarels  * size (usually 512 or the default IP max size, but no more than the mtu
135344375Skarels  * of the interface), as we can't discover anything about intervening
135444375Skarels  * gateways or networks.  We also initialize the congestion/slow start
135544375Skarels  * window to be a single segment if the destination isn't local.
135644375Skarels  * While looking at the routing entry, we also initialize other path-dependent
135744375Skarels  * parameters from pre-set or cached values in the routing entry.
135844375Skarels  */
135944375Skarels 
136044375Skarels tcp_mss(tp, offer)
136144375Skarels 	register struct tcpcb *tp;
136244375Skarels 	u_short offer;
136344375Skarels {
136417272Skarels 	struct route *ro;
136544375Skarels 	register struct rtentry *rt;
136617272Skarels 	struct ifnet *ifp;
136744375Skarels 	register int rtt, mss;
136844375Skarels 	u_long bufsize;
136917272Skarels 	struct inpcb *inp;
137044375Skarels 	struct socket *so;
137144375Skarels 	extern int tcp_mssdflt, tcp_rttdflt;
137217272Skarels 
137317272Skarels 	inp = tp->t_inpcb;
137417272Skarels 	ro = &inp->inp_route;
137544375Skarels 
137644375Skarels 	if ((rt = ro->ro_rt) == (struct rtentry *)0) {
137717272Skarels 		/* No route yet, so try to acquire one */
137817272Skarels 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
137917272Skarels 			ro->ro_dst.sa_family = AF_INET;
138037320Skarels 			ro->ro_dst.sa_len = sizeof(ro->ro_dst);
138117272Skarels 			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
138217272Skarels 				inp->inp_faddr;
138317272Skarels 			rtalloc(ro);
138417272Skarels 		}
138544375Skarels 		if ((rt = ro->ro_rt) == (struct rtentry *)0)
138644375Skarels 			return (tcp_mssdflt);
138717272Skarels 	}
138844375Skarels 	ifp = rt->rt_ifp;
138944375Skarels 	so = inp->inp_socket;
139017272Skarels 
139144375Skarels #ifdef RTV_MTU	/* if route characteristics exist ... */
139244375Skarels 	/*
139344375Skarels 	 * While we're here, check if there's an initial rtt
139444375Skarels 	 * or rttvar.  Convert from the route-table units
139544375Skarels 	 * to scaled multiples of the slow timeout timer.
139644375Skarels 	 */
139744375Skarels 	if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
139844375Skarels 		if (rt->rt_rmx.rmx_locks & RTV_MTU)
139944375Skarels 			tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ);
140044375Skarels 		tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
140144375Skarels 		if (rt->rt_rmx.rmx_rttvar)
140244375Skarels 			tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
140344375Skarels 			    (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
140444375Skarels 		else
140544375Skarels 			/* default variation is +- 1 rtt */
140644375Skarels 			tp->t_rttvar =
140744375Skarels 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
140844375Skarels 		TCPT_RANGESET(tp->t_rxtcur,
140944375Skarels 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
141044375Skarels 		    tp->t_rttmin, TCPTV_REXMTMAX);
141144375Skarels 	}
141244375Skarels 	/*
141344375Skarels 	 * if there's an mtu associated with the route, use it
141444375Skarels 	 */
141544375Skarels 	if (rt->rt_rmx.rmx_mtu)
141644375Skarels 		mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
141744375Skarels 	else
141844375Skarels #endif /* RTV_MTU */
141944375Skarels 	{
142044375Skarels 		mss = ifp->if_mtu - sizeof(struct tcpiphdr);
142131726Skarels #if	(MCLBYTES & (MCLBYTES - 1)) == 0
142244375Skarels 		if (mss > MCLBYTES)
142344375Skarels 			mss &= ~(MCLBYTES-1);
142417272Skarels #else
142544375Skarels 		if (mss > MCLBYTES)
142644375Skarels 			mss = mss / MCLBYTES * MCLBYTES;
142717272Skarels #endif
142844375Skarels 		if (!in_localaddr(inp->inp_faddr))
142944375Skarels 			mss = min(mss, tcp_mssdflt);
143044375Skarels 	}
143144375Skarels 	/*
143244375Skarels 	 * The current mss, t_maxseg, is initialized to the default value.
143344375Skarels 	 * If we compute a smaller value, reduce the current mss.
143444375Skarels 	 * If we compute a larger value, return it for use in sending
143544375Skarels 	 * a max seg size option, but don't store it for use
143644375Skarels 	 * unless we received an offer at least that large from peer.
143744375Skarels 	 * However, do not accept offers under 32 bytes.
143844375Skarels 	 */
143944375Skarels 	if (offer)
144044375Skarels 		mss = min(mss, offer);
144144375Skarels 	mss = max(mss, 32);		/* sanity */
144244375Skarels 	if (mss < tp->t_maxseg || offer != 0) {
144344375Skarels 		/*
144444375Skarels 		 * If there's a pipesize, change the socket buffer
144544375Skarels 		 * to that size.  Make the socket buffers an integral
144644375Skarels 		 * number of mss units; if the mss is larger than
144744375Skarels 		 * the socket buffer, decrease the mss.
144844375Skarels 		 */
144944375Skarels #ifdef RTV_SPIPE
145044375Skarels 		if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
145144375Skarels #endif
145244375Skarels 			bufsize = so->so_snd.sb_hiwat;
145344375Skarels 		if (bufsize < mss)
145444375Skarels 			mss = bufsize;
145544375Skarels 		else {
145644375Skarels 			bufsize = min(bufsize, SB_MAX) / mss * mss;
145744375Skarels 			(void) sbreserve(&so->so_snd, bufsize);
145844375Skarels 		}
145944375Skarels 		tp->t_maxseg = mss;
146032098Skarels 
146144375Skarels #ifdef RTV_RPIPE
146244375Skarels 		if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
146344375Skarels #endif
146444375Skarels 			bufsize = so->so_rcv.sb_hiwat;
146544375Skarels 		if (bufsize > mss) {
146644375Skarels 			bufsize = min(bufsize, SB_MAX) / mss * mss;
146744375Skarels 			(void) sbreserve(&so->so_rcv, bufsize);
146844375Skarels 		}
146944375Skarels 	}
147032034Skarels 	tp->snd_cwnd = mss;
147144375Skarels 
147244375Skarels #ifdef RTV_SSTHRESH
147344375Skarels 	if (rt->rt_rmx.rmx_ssthresh) {
147444375Skarels 		/*
147544375Skarels 		 * There's some sort of gateway or interface
147644375Skarels 		 * buffer limit on the path.  Use this to set
147744375Skarels 		 * the slow start threshhold, but set the
147844375Skarels 		 * threshold to no less than 2*mss.
147944375Skarels 		 */
148044375Skarels 		tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
148144375Skarels 	}
148244375Skarels #endif /* RTV_MTU */
148332034Skarels 	return (mss);
148417272Skarels }
1485