xref: /csrg-svn/sys/netinet/tcp_input.c (revision 32098)
123190Smckusick /*
229149Smckusick  * Copyright (c) 1982, 1986 Regents of the University of California.
323190Smckusick  * All rights reserved.  The Berkeley software License Agreement
423190Smckusick  * specifies the terms and conditions for redistribution.
523190Smckusick  *
6*32098Skarels  *	@(#)tcp_input.c	7.11 (Berkeley) 09/04/87
723190Smckusick  */
84601Swnj 
917062Sbloom #include "param.h"
1017062Sbloom #include "systm.h"
1117062Sbloom #include "mbuf.h"
1217062Sbloom #include "protosw.h"
1317062Sbloom #include "socket.h"
1417062Sbloom #include "socketvar.h"
1517062Sbloom #include "errno.h"
1610894Ssam 
1710894Ssam #include "../net/if.h"
1810894Ssam #include "../net/route.h"
1910894Ssam 
2017062Sbloom #include "in.h"
2117062Sbloom #include "in_pcb.h"
2217062Sbloom #include "in_systm.h"
2317062Sbloom #include "ip.h"
2417062Sbloom #include "ip_var.h"
2517062Sbloom #include "tcp.h"
2617062Sbloom #include "tcp_fsm.h"
2717062Sbloom #include "tcp_seq.h"
2817062Sbloom #include "tcp_timer.h"
2917062Sbloom #include "tcp_var.h"
3017062Sbloom #include "tcpip.h"
3117062Sbloom #include "tcp_debug.h"
324601Swnj 
335300Sroot int	tcpprintfs = 0;
344679Swnj int	tcpcksum = 1;
35*32098Skarels int	tcprexmtthresh = 3;
365267Sroot struct	tcpiphdr tcp_saveti;
375440Swnj extern	tcpnodelack;
384601Swnj 
395267Sroot struct	tcpcb *tcp_newtcpcb();
4024816Skarels 
415065Swnj /*
4224816Skarels  * Insert segment ti into reassembly queue of tcp with
4324816Skarels  * control block tp.  Return TH_FIN if reassembly now includes
4424816Skarels  * a segment with FIN.  The macro form does the common case inline
4524816Skarels  * (segment is the next to be received on an established connection,
4624816Skarels  * and the queue is empty), avoiding linkage into and removal
4724816Skarels  * from the queue and repetition of various conversions.
4824816Skarels  */
4924816Skarels #define	TCP_REASS(tp, ti, m, so, flags) { \
5024816Skarels 	if ((ti)->ti_seq == (tp)->rcv_nxt && \
5124816Skarels 	    (tp)->seg_next == (struct tcpiphdr *)(tp) && \
5224816Skarels 	    (tp)->t_state == TCPS_ESTABLISHED) { \
5324816Skarels 		(tp)->rcv_nxt += (ti)->ti_len; \
5424816Skarels 		flags = (ti)->ti_flags & TH_FIN; \
5530525Skarels 		tcpstat.tcps_rcvpack++;\
5630525Skarels 		tcpstat.tcps_rcvbyte += (ti)->ti_len;\
5724816Skarels 		sbappend(&(so)->so_rcv, (m)); \
5824816Skarels 		sorwakeup(so); \
5924816Skarels 	} else \
6024816Skarels 		(flags) = tcp_reass((tp), (ti)); \
6124816Skarels }
6224816Skarels 
6324816Skarels tcp_reass(tp, ti)
6424816Skarels 	register struct tcpcb *tp;
6524816Skarels 	register struct tcpiphdr *ti;
6624816Skarels {
6724816Skarels 	register struct tcpiphdr *q;
6824816Skarels 	struct socket *so = tp->t_inpcb->inp_socket;
6924816Skarels 	struct mbuf *m;
7024816Skarels 	int flags;
7124816Skarels 
7224816Skarels 	/*
7324816Skarels 	 * Call with ti==0 after become established to
7424816Skarels 	 * force pre-ESTABLISHED data up to user socket.
7524816Skarels 	 */
7624816Skarels 	if (ti == 0)
7724816Skarels 		goto present;
7824816Skarels 
7924816Skarels 	/*
8024816Skarels 	 * Find a segment which begins after this one does.
8124816Skarels 	 */
8224816Skarels 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
8324816Skarels 	    q = (struct tcpiphdr *)q->ti_next)
8424816Skarels 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
8524816Skarels 			break;
8624816Skarels 
8724816Skarels 	/*
8824816Skarels 	 * If there is a preceding segment, it may provide some of
8924816Skarels 	 * our data already.  If so, drop the data from the incoming
9024816Skarels 	 * segment.  If it provides all of our data, drop us.
9124816Skarels 	 */
9224816Skarels 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
9324816Skarels 		register int i;
9424816Skarels 		q = (struct tcpiphdr *)q->ti_prev;
9524816Skarels 		/* conversion to int (in i) handles seq wraparound */
9624816Skarels 		i = q->ti_seq + q->ti_len - ti->ti_seq;
9724816Skarels 		if (i > 0) {
9830525Skarels 			if (i >= ti->ti_len) {
9930525Skarels 				tcpstat.tcps_rcvduppack++;
10030525Skarels 				tcpstat.tcps_rcvdupbyte += ti->ti_len;
10124816Skarels 				goto drop;
10230525Skarels 			}
10324816Skarels 			m_adj(dtom(ti), i);
10424816Skarels 			ti->ti_len -= i;
10524816Skarels 			ti->ti_seq += i;
10624816Skarels 		}
10724816Skarels 		q = (struct tcpiphdr *)(q->ti_next);
10824816Skarels 	}
10930525Skarels 	tcpstat.tcps_rcvoopack++;
11030525Skarels 	tcpstat.tcps_rcvoobyte += ti->ti_len;
11124816Skarels 
11224816Skarels 	/*
11324816Skarels 	 * While we overlap succeeding segments trim them or,
11424816Skarels 	 * if they are completely covered, dequeue them.
11524816Skarels 	 */
11624816Skarels 	while (q != (struct tcpiphdr *)tp) {
11724816Skarels 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
11824816Skarels 		if (i <= 0)
11924816Skarels 			break;
12024816Skarels 		if (i < q->ti_len) {
12124816Skarels 			q->ti_seq += i;
12224816Skarels 			q->ti_len -= i;
12324816Skarels 			m_adj(dtom(q), i);
12424816Skarels 			break;
12524816Skarels 		}
12624816Skarels 		q = (struct tcpiphdr *)q->ti_next;
12724816Skarels 		m = dtom(q->ti_prev);
12824816Skarels 		remque(q->ti_prev);
12924816Skarels 		m_freem(m);
13024816Skarels 	}
13124816Skarels 
13224816Skarels 	/*
13324816Skarels 	 * Stick new segment in its place.
13424816Skarels 	 */
13524816Skarels 	insque(ti, q->ti_prev);
13624816Skarels 
13724816Skarels present:
13824816Skarels 	/*
13924816Skarels 	 * Present data to user, advancing rcv_nxt through
14024816Skarels 	 * completed sequence space.
14124816Skarels 	 */
14224816Skarels 	if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
14324816Skarels 		return (0);
14424816Skarels 	ti = tp->seg_next;
14524816Skarels 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
14624816Skarels 		return (0);
14724816Skarels 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
14824816Skarels 		return (0);
14924816Skarels 	do {
15024816Skarels 		tp->rcv_nxt += ti->ti_len;
15124816Skarels 		flags = ti->ti_flags & TH_FIN;
15224816Skarels 		remque(ti);
15324816Skarels 		m = dtom(ti);
15424816Skarels 		ti = (struct tcpiphdr *)ti->ti_next;
15524816Skarels 		if (so->so_state & SS_CANTRCVMORE)
15624816Skarels 			m_freem(m);
15724816Skarels 		else
15824816Skarels 			sbappend(&so->so_rcv, m);
15924816Skarels 	} while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
16024816Skarels 	sorwakeup(so);
16124816Skarels 	return (flags);
16224816Skarels drop:
16324816Skarels 	m_freem(dtom(ti));
16424816Skarels 	return (0);
16524816Skarels }
16624816Skarels 
16724816Skarels /*
1685065Swnj  * TCP input routine, follows pages 65-76 of the
1695065Swnj  * protocol specification dated September, 1981 very closely.
1705065Swnj  */
1714924Swnj tcp_input(m0)
1724924Swnj 	struct mbuf *m0;
1734601Swnj {
1744924Swnj 	register struct tcpiphdr *ti;
1754924Swnj 	struct inpcb *inp;
1764924Swnj 	register struct mbuf *m;
1775440Swnj 	struct mbuf *om = 0;
1784924Swnj 	int len, tlen, off;
1795391Swnj 	register struct tcpcb *tp = 0;
1804924Swnj 	register int tiflags;
1814803Swnj 	struct socket *so;
18231721Skarels 	int todrop, acked, ourfinisacked, needoutput = 0;
1835267Sroot 	short ostate;
1846028Sroot 	struct in_addr laddr;
18510769Ssam 	int dropsocket = 0;
18630525Skarels 	int iss = 0;
1874924Swnj 
18830525Skarels 	tcpstat.tcps_rcvtotal++;
1894924Swnj 	/*
1905244Sroot 	 * Get IP and TCP header together in first mbuf.
1915244Sroot 	 * Note: IP leaves IP header in first mbuf.
1924924Swnj 	 */
1934924Swnj 	m = m0;
1945020Sroot 	ti = mtod(m, struct tcpiphdr *);
1955244Sroot 	if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2))
1965208Swnj 		ip_stripoptions((struct ip *)ti, (struct mbuf *)0);
1975307Sroot 	if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) {
1985307Sroot 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
19930525Skarels 			tcpstat.tcps_rcvshort++;
2005307Sroot 			return;
2015085Swnj 		}
2025085Swnj 		ti = mtod(m, struct tcpiphdr *);
2035085Swnj 	}
2044601Swnj 
2054601Swnj 	/*
2065244Sroot 	 * Checksum extended TCP header and data.
2074601Swnj 	 */
2084924Swnj 	tlen = ((struct ip *)ti)->ip_len;
2094924Swnj 	len = sizeof (struct ip) + tlen;
2104679Swnj 	if (tcpcksum) {
2114924Swnj 		ti->ti_next = ti->ti_prev = 0;
2124924Swnj 		ti->ti_x1 = 0;
2135223Swnj 		ti->ti_len = (u_short)tlen;
2146161Ssam 		ti->ti_len = htons((u_short)ti->ti_len);
2155231Swnj 		if (ti->ti_sum = in_cksum(m, len)) {
21611830Ssam 			if (tcpprintfs)
21711830Ssam 				printf("tcp sum: src %x\n", ti->ti_src);
21830525Skarels 			tcpstat.tcps_rcvbadsum++;
2195085Swnj 			goto drop;
2204601Swnj 		}
2214601Swnj 	}
2224601Swnj 
2234601Swnj 	/*
2245244Sroot 	 * Check that TCP offset makes sense,
2255440Swnj 	 * pull out TCP options and adjust length.
2264601Swnj 	 */
2274924Swnj 	off = ti->ti_off << 2;
2285231Swnj 	if (off < sizeof (struct tcphdr) || off > tlen) {
22911830Ssam 		if (tcpprintfs)
23011830Ssam 			printf("tcp off: src %x off %d\n", ti->ti_src, off);
23130525Skarels 		tcpstat.tcps_rcvbadoff++;
2325085Swnj 		goto drop;
2334924Swnj 	}
2346211Swnj 	tlen -= off;
2356211Swnj 	ti->ti_len = tlen;
2365440Swnj 	if (off > sizeof (struct tcphdr)) {
23724816Skarels 		if (m->m_len < sizeof(struct ip) + off) {
23824816Skarels 			if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
23930525Skarels 				tcpstat.tcps_rcvshort++;
24024816Skarels 				return;
24124816Skarels 			}
24224816Skarels 			ti = mtod(m, struct tcpiphdr *);
2435440Swnj 		}
2449642Ssam 		om = m_get(M_DONTWAIT, MT_DATA);
2455440Swnj 		if (om == 0)
2465440Swnj 			goto drop;
2475440Swnj 		om->m_len = off - sizeof (struct tcphdr);
2485440Swnj 		{ caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
2496161Ssam 		  bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len);
2505440Swnj 		  m->m_len -= om->m_len;
2516161Ssam 		  bcopy(op+om->m_len, op,
2526161Ssam 		   (unsigned)(m->m_len-sizeof (struct tcpiphdr)));
2535440Swnj 		}
2545440Swnj 	}
2555065Swnj 	tiflags = ti->ti_flags;
2564924Swnj 
2576093Sroot 	/*
25825729Smckusick 	 * Drop TCP and IP headers; TCP options were dropped above.
2596093Sroot 	 */
26025729Smckusick 	m->m_off += sizeof(struct tcpiphdr);
26125729Smckusick 	m->m_len -= sizeof(struct tcpiphdr);
2626093Sroot 
2634924Swnj 	/*
2645244Sroot 	 * Convert TCP protocol specific fields to host format.
2655085Swnj 	 */
2665085Swnj 	ti->ti_seq = ntohl(ti->ti_seq);
2675085Swnj 	ti->ti_ack = ntohl(ti->ti_ack);
2685085Swnj 	ti->ti_win = ntohs(ti->ti_win);
2695085Swnj 	ti->ti_urp = ntohs(ti->ti_urp);
2705085Swnj 
2715085Swnj 	/*
2728271Sroot 	 * Locate pcb for segment.
2734924Swnj 	 */
27430525Skarels findpcb:
2755065Swnj 	inp = in_pcblookup
2766028Sroot 		(&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport,
2776028Sroot 		INPLOOKUP_WILDCARD);
2785065Swnj 
2795065Swnj 	/*
2805065Swnj 	 * If the state is CLOSED (i.e., TCB does not exist) then
2815244Sroot 	 * all data in the incoming segment is discarded.
282*32098Skarels 	 * If the TCB exists but is in CLOSED state, it is embryonic,
283*32098Skarels 	 * but should either do a listen or a connect soon.
2845065Swnj 	 */
2855300Sroot 	if (inp == 0)
2865085Swnj 		goto dropwithreset;
2875065Swnj 	tp = intotcpcb(inp);
2885300Sroot 	if (tp == 0)
2895085Swnj 		goto dropwithreset;
290*32098Skarels 	if (tp->t_state == TCPS_CLOSED)
291*32098Skarels 		goto drop;
2925109Swnj 	so = inp->inp_socket;
2935267Sroot 	if (so->so_options & SO_DEBUG) {
2945267Sroot 		ostate = tp->t_state;
2955267Sroot 		tcp_saveti = *ti;
2965267Sroot 	}
2977510Sroot 	if (so->so_options & SO_ACCEPTCONN) {
2987510Sroot 		so = sonewconn(so);
2997510Sroot 		if (so == 0)
3007510Sroot 			goto drop;
30110769Ssam 		/*
30210769Ssam 		 * This is ugly, but ....
30310769Ssam 		 *
30410769Ssam 		 * Mark socket as temporary until we're
30510769Ssam 		 * committed to keeping it.  The code at
30610769Ssam 		 * ``drop'' and ``dropwithreset'' check the
30710769Ssam 		 * flag dropsocket to see if the temporary
30810769Ssam 		 * socket created here should be discarded.
30910769Ssam 		 * We mark the socket as discardable until
31010769Ssam 		 * we're committed to it below in TCPS_LISTEN.
31110769Ssam 		 */
31210769Ssam 		dropsocket++;
3137510Sroot 		inp = (struct inpcb *)so->so_pcb;
3147510Sroot 		inp->inp_laddr = ti->ti_dst;
3157510Sroot 		inp->inp_lport = ti->ti_dport;
31624816Skarels 		inp->inp_options = ip_srcroute();
3177510Sroot 		tp = intotcpcb(inp);
3187510Sroot 		tp->t_state = TCPS_LISTEN;
3197510Sroot 	}
3204601Swnj 
3214601Swnj 	/*
3225162Swnj 	 * Segment received on connection.
3235162Swnj 	 * Reset idle time and keep-alive timer.
3245162Swnj 	 */
3255162Swnj 	tp->t_idle = 0;
3265162Swnj 	tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
3275162Swnj 
3285162Swnj 	/*
32917272Skarels 	 * Process options if not in LISTEN state,
33017272Skarels 	 * else do it below (after getting remote address).
3315440Swnj 	 */
33217272Skarels 	if (om && tp->t_state != TCPS_LISTEN) {
33317272Skarels 		tcp_dooptions(tp, om, ti);
3345440Swnj 		om = 0;
3355440Swnj 	}
3365440Swnj 
3375440Swnj 	/*
3385085Swnj 	 * Calculate amount of space in receive window,
3395085Swnj 	 * and then do TCP input processing.
34024816Skarels 	 * Receive window is amount of space in rcv queue,
34124816Skarels 	 * but not less than advertised window.
3424601Swnj 	 */
34325939Skarels 	{ int win;
3444601Swnj 
34525939Skarels 	win = sbspace(&so->so_rcv);
34625939Skarels 	if (win < 0)
34725939Skarels 		win = 0;
34825939Skarels 	tp->rcv_wnd = MAX(win, (int)(tp->rcv_adv - tp->rcv_nxt));
34925939Skarels 	}
35025939Skarels 
3514601Swnj 	switch (tp->t_state) {
3524601Swnj 
3535065Swnj 	/*
3545065Swnj 	 * If the state is LISTEN then ignore segment if it contains an RST.
3555065Swnj 	 * If the segment contains an ACK then it is bad and send a RST.
3565065Swnj 	 * If it does not contain a SYN then it is not interesting; drop it.
35725197Skarels 	 * Don't bother responding if the destination was a broadcast.
3585085Swnj 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
3595065Swnj 	 * tp->iss, and send a segment:
3605085Swnj 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
3615065Swnj 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
3625065Swnj 	 * Fill in remote peer address fields if not previously specified.
3635065Swnj 	 * Enter SYN_RECEIVED state, and process any other fields of this
3645244Sroot 	 * segment in this state.
3655065Swnj 	 */
3668271Sroot 	case TCPS_LISTEN: {
36710145Ssam 		struct mbuf *am;
3688271Sroot 		register struct sockaddr_in *sin;
3698271Sroot 
3705065Swnj 		if (tiflags & TH_RST)
3715065Swnj 			goto drop;
3725300Sroot 		if (tiflags & TH_ACK)
3735085Swnj 			goto dropwithreset;
3745300Sroot 		if ((tiflags & TH_SYN) == 0)
3755065Swnj 			goto drop;
37625197Skarels 		if (in_broadcast(ti->ti_dst))
37725197Skarels 			goto drop;
37810145Ssam 		am = m_get(M_DONTWAIT, MT_SONAME);
37910145Ssam 		if (am == NULL)
38010145Ssam 			goto drop;
38110145Ssam 		am->m_len = sizeof (struct sockaddr_in);
3828599Sroot 		sin = mtod(am, struct sockaddr_in *);
3838271Sroot 		sin->sin_family = AF_INET;
3848271Sroot 		sin->sin_addr = ti->ti_src;
3858271Sroot 		sin->sin_port = ti->ti_sport;
3866028Sroot 		laddr = inp->inp_laddr;
38710145Ssam 		if (inp->inp_laddr.s_addr == INADDR_ANY)
3886028Sroot 			inp->inp_laddr = ti->ti_dst;
3898599Sroot 		if (in_pcbconnect(inp, am)) {
3906028Sroot 			inp->inp_laddr = laddr;
3918716Sroot 			(void) m_free(am);
3925244Sroot 			goto drop;
3936028Sroot 		}
3948716Sroot 		(void) m_free(am);
3955244Sroot 		tp->t_template = tcp_template(tp);
3965244Sroot 		if (tp->t_template == 0) {
39726386Skarels 			tp = tcp_drop(tp, ENOBUFS);
39817264Skarels 			dropsocket = 0;		/* socket is already gone */
3995244Sroot 			goto drop;
4005244Sroot 		}
40117272Skarels 		if (om) {
40217272Skarels 			tcp_dooptions(tp, om, ti);
40317272Skarels 			om = 0;
40417272Skarels 		}
40530525Skarels 		if (iss)
40630525Skarels 			tp->iss = iss;
40730525Skarels 		else
40830525Skarels 			tp->iss = tcp_iss;
40930525Skarels 		tcp_iss += TCP_ISSINCR/2;
4105065Swnj 		tp->irs = ti->ti_seq;
4115085Swnj 		tcp_sendseqinit(tp);
4125085Swnj 		tcp_rcvseqinit(tp);
41325939Skarels 		tp->t_flags |= TF_ACKNOW;
4145065Swnj 		tp->t_state = TCPS_SYN_RECEIVED;
4155244Sroot 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
41610769Ssam 		dropsocket = 0;		/* committed to socket */
41730525Skarels 		tcpstat.tcps_accepts++;
4185085Swnj 		goto trimthenstep6;
4198271Sroot 		}
4204601Swnj 
4215065Swnj 	/*
4225065Swnj 	 * If the state is SYN_SENT:
4235065Swnj 	 *	if seg contains an ACK, but not for our SYN, drop the input.
4245065Swnj 	 *	if seg contains a RST, then drop the connection.
4255065Swnj 	 *	if seg does not contain SYN, then drop it.
4265065Swnj 	 * Otherwise this is an acceptable SYN segment
4275065Swnj 	 *	initialize tp->rcv_nxt and tp->irs
4285065Swnj 	 *	if seg contains ack then advance tp->snd_una
4295065Swnj 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
4305065Swnj 	 *	arrange for segment to be acked (eventually)
4315065Swnj 	 *	continue processing rest of data/controls, beginning with URG
4325065Swnj 	 */
4335065Swnj 	case TCPS_SYN_SENT:
4345065Swnj 		if ((tiflags & TH_ACK) &&
43524816Skarels 		    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
4365231Swnj 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
4375085Swnj 			goto dropwithreset;
4385065Swnj 		if (tiflags & TH_RST) {
43910394Ssam 			if (tiflags & TH_ACK)
44010394Ssam 				tp = tcp_drop(tp, ECONNREFUSED);
4415065Swnj 			goto drop;
4424601Swnj 		}
4435065Swnj 		if ((tiflags & TH_SYN) == 0)
4445065Swnj 			goto drop;
44530974Skarels 		if (tiflags & TH_ACK) {
44630974Skarels 			tp->snd_una = ti->ti_ack;
44730974Skarels 			if (SEQ_LT(tp->snd_nxt, tp->snd_una))
44830974Skarels 				tp->snd_nxt = tp->snd_una;
44930974Skarels 		}
4505244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
4515065Swnj 		tp->irs = ti->ti_seq;
4525085Swnj 		tcp_rcvseqinit(tp);
4535085Swnj 		tp->t_flags |= TF_ACKNOW;
45430974Skarels 		if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
45530525Skarels 			tcpstat.tcps_connects++;
4565244Sroot 			soisconnected(so);
4575065Swnj 			tp->t_state = TCPS_ESTABLISHED;
45817272Skarels 			tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
4595162Swnj 			(void) tcp_reass(tp, (struct tcpiphdr *)0);
460*32098Skarels 			/*
461*32098Skarels 			 * if we didn't have to retransmit the SYN,
462*32098Skarels 			 * use its rtt as our initial srtt & rtt var.
463*32098Skarels 			 */
464*32098Skarels 			if (tp->t_rtt) {
465*32098Skarels 				tp->t_srtt = tp->t_rtt << 3;
466*32098Skarels 				tp->t_rttvar = tp->t_rtt << 1;
467*32098Skarels 				TCPT_RANGESET(tp->t_rxtcur,
468*32098Skarels 				    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
469*32098Skarels 				    TCPTV_MIN, TCPTV_REXMTMAX);
470*32098Skarels 				tp->t_rtt = 0;
471*32098Skarels 			}
4725162Swnj 		} else
4735085Swnj 			tp->t_state = TCPS_SYN_RECEIVED;
4745085Swnj 
4755085Swnj trimthenstep6:
4765085Swnj 		/*
4775231Swnj 		 * Advance ti->ti_seq to correspond to first data byte.
4785085Swnj 		 * If data, trim to stay within window,
4795085Swnj 		 * dropping FIN if necessary.
4805085Swnj 		 */
4815231Swnj 		ti->ti_seq++;
4825085Swnj 		if (ti->ti_len > tp->rcv_wnd) {
4835085Swnj 			todrop = ti->ti_len - tp->rcv_wnd;
4845085Swnj 			m_adj(m, -todrop);
4855085Swnj 			ti->ti_len = tp->rcv_wnd;
48625939Skarels 			tiflags &= ~TH_FIN;
48730525Skarels 			tcpstat.tcps_rcvpackafterwin++;
48830525Skarels 			tcpstat.tcps_rcvbyteafterwin += todrop;
4895065Swnj 		}
4905263Swnj 		tp->snd_wl1 = ti->ti_seq - 1;
49125939Skarels 		tp->rcv_up = ti->ti_seq;
4925085Swnj 		goto step6;
4935065Swnj 	}
4944601Swnj 
4955065Swnj 	/*
49630525Skarels 	 * States other than LISTEN or SYN_SENT.
49730525Skarels 	 * First check that at least some bytes of segment are within
49830525Skarels 	 * receive window.  If segment begins before rcv_nxt,
49930525Skarels 	 * drop leading data (and SYN); if nothing left, just ack.
50016222Skarels 	 */
50130525Skarels 	todrop = tp->rcv_nxt - ti->ti_seq;
50230525Skarels 	if (todrop > 0) {
50330525Skarels 		if (tiflags & TH_SYN) {
50430525Skarels 			tiflags &= ~TH_SYN;
50530525Skarels 			ti->ti_seq++;
50630525Skarels 			if (ti->ti_urp > 1)
50730525Skarels 				ti->ti_urp--;
50830525Skarels 			else
50930525Skarels 				tiflags &= ~TH_URG;
51030525Skarels 			todrop--;
51130525Skarels 		}
51230525Skarels 		if (todrop > ti->ti_len ||
51330525Skarels 		    todrop == ti->ti_len && (tiflags&TH_FIN) == 0) {
51431730Skarels #ifdef TCP_COMPAT_42
51531730Skarels 			/*
51631730Skarels 			 * Don't toss RST in response to 4.2-style keepalive.
51731730Skarels 			 */
51831730Skarels 			if (ti->ti_seq == tp->rcv_nxt - 1 && tiflags & TH_RST)
51931730Skarels 				goto do_rst;
52031730Skarels #endif
52130525Skarels 			tcpstat.tcps_rcvduppack++;
52230525Skarels 			tcpstat.tcps_rcvdupbyte += ti->ti_len;
52332034Skarels 			todrop = ti->ti_len;
524*32098Skarels 			tiflags &= ~TH_FIN;
52532034Skarels 			tp->t_flags |= TF_ACKNOW;
52632034Skarels 		} else {
52732034Skarels 			tcpstat.tcps_rcvpartduppack++;
52832034Skarels 			tcpstat.tcps_rcvpartdupbyte += todrop;
52930525Skarels 		}
53030525Skarels 		m_adj(m, todrop);
53130525Skarels 		ti->ti_seq += todrop;
53230525Skarels 		ti->ti_len -= todrop;
53330525Skarels 		if (ti->ti_urp > todrop)
53430525Skarels 			ti->ti_urp -= todrop;
53530525Skarels 		else {
53630525Skarels 			tiflags &= ~TH_URG;
53730525Skarels 			ti->ti_urp = 0;
53830525Skarels 		}
53916222Skarels 	}
54016222Skarels 
5415065Swnj 	if (tp->rcv_wnd == 0) {
5425065Swnj 		/*
5435065Swnj 		 * If window is closed can only take segments at
5445231Swnj 		 * window edge, and have to drop data and PUSH from
5455065Swnj 		 * incoming segments.
54630525Skarels 		 *
54730525Skarels 		 * If new data is received on a connection after the
54830525Skarels 		 * user processes are gone, then RST the other end.
5495065Swnj 		 */
55030525Skarels 		if ((so->so_state & SS_NOFDREF) &&
55130525Skarels 		    tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
55230525Skarels 			tp = tcp_close(tp);
55330525Skarels 			tcpstat.tcps_rcvafterclose++;
55430525Skarels 			goto dropwithreset;
55530525Skarels 		}
55630525Skarels 		if (tp->rcv_nxt != ti->ti_seq) {
55730525Skarels 			tcpstat.tcps_rcvpackafterwin++;
55830525Skarels 			tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
5595065Swnj 			goto dropafterack;
56030525Skarels 		}
5615085Swnj 		if (ti->ti_len > 0) {
56230525Skarels 			if (ti->ti_len == 1)
56330525Skarels 				tcpstat.tcps_rcvwinprobe++;
56430525Skarels 			else {
56530525Skarels 				tcpstat.tcps_rcvpackafterwin++;
56630525Skarels 				tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
56730525Skarels 			}
5685690Swnj 			m_adj(m, ti->ti_len);
5695085Swnj 			ti->ti_len = 0;
57025939Skarels 			tiflags &= ~(TH_PUSH|TH_FIN);
5715065Swnj 		}
5725065Swnj 	} else {
5735065Swnj 		/*
5745065Swnj 		 * If segment ends after window, drop trailing data
5755085Swnj 		 * (and PUSH and FIN); if nothing left, just ACK.
5765065Swnj 		 */
5775690Swnj 		todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
5785690Swnj 		if (todrop > 0) {
57930525Skarels 			if (todrop >= ti->ti_len) {
58030525Skarels 				/*
58130525Skarels 				 * If a new connection request is received
58230525Skarels 				 * while in TIME_WAIT, drop the old connection
58330525Skarels 				 * and start over if the sequence numbers
58430525Skarels 				 * are above the previous ones.
58530525Skarels 				 */
58630525Skarels 				if (tiflags & TH_SYN &&
58730525Skarels 				    tp->t_state == TCPS_TIME_WAIT &&
58830525Skarels 				    SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
58930525Skarels 					iss = tp->rcv_nxt + TCP_ISSINCR;
59030525Skarels 					(void) tcp_close(tp);
59130525Skarels 					goto findpcb;
59230525Skarels 				}
59330525Skarels 				if (todrop == 1)
59430525Skarels 					tcpstat.tcps_rcvwinprobe++;
59530525Skarels 				else {
59630525Skarels 					tcpstat.tcps_rcvpackafterwin++;
59731442Skarels 					tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
59830525Skarels 				}
5995065Swnj 				goto dropafterack;
60030525Skarels 			}
60130525Skarels 			tcpstat.tcps_rcvpackafterwin++;
60230525Skarels 			tcpstat.tcps_rcvbyteafterwin += todrop;
6035065Swnj 			m_adj(m, -todrop);
6045065Swnj 			ti->ti_len -= todrop;
60525939Skarels 			tiflags &= ~(TH_PUSH|TH_FIN);
6065065Swnj 		}
6075065Swnj 	}
6084601Swnj 
60931730Skarels #ifdef TCP_COMPAT_42
61031730Skarels do_rst:
61131730Skarels #endif
6125065Swnj 	/*
6135065Swnj 	 * If the RST bit is set examine the state:
6145065Swnj 	 *    SYN_RECEIVED STATE:
6155065Swnj 	 *	If passive open, return to LISTEN state.
6165065Swnj 	 *	If active open, inform user that connection was refused.
6175065Swnj 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
6185065Swnj 	 *	Inform user that connection was reset, and close tcb.
6195065Swnj 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
6205065Swnj 	 *	Close the tcb.
6215065Swnj 	 */
6225065Swnj 	if (tiflags&TH_RST) switch (tp->t_state) {
6235267Sroot 
6245065Swnj 	case TCPS_SYN_RECEIVED:
62510394Ssam 		tp = tcp_drop(tp, ECONNREFUSED);
6265065Swnj 		goto drop;
6274601Swnj 
6285065Swnj 	case TCPS_ESTABLISHED:
6295065Swnj 	case TCPS_FIN_WAIT_1:
6305065Swnj 	case TCPS_FIN_WAIT_2:
6315065Swnj 	case TCPS_CLOSE_WAIT:
63210394Ssam 		tp = tcp_drop(tp, ECONNRESET);
6335065Swnj 		goto drop;
6345065Swnj 
6355065Swnj 	case TCPS_CLOSING:
6365065Swnj 	case TCPS_LAST_ACK:
6375065Swnj 	case TCPS_TIME_WAIT:
63810394Ssam 		tp = tcp_close(tp);
6395065Swnj 		goto drop;
6404601Swnj 	}
6414601Swnj 
6424601Swnj 	/*
6435065Swnj 	 * If a SYN is in the window, then this is an
6445065Swnj 	 * error and we send an RST and drop the connection.
6454601Swnj 	 */
6465065Swnj 	if (tiflags & TH_SYN) {
64710394Ssam 		tp = tcp_drop(tp, ECONNRESET);
6485085Swnj 		goto dropwithreset;
6494601Swnj 	}
6504601Swnj 
6514601Swnj 	/*
6525065Swnj 	 * If the ACK bit is off we drop the segment and return.
6534601Swnj 	 */
6545085Swnj 	if ((tiflags & TH_ACK) == 0)
6555065Swnj 		goto drop;
6565065Swnj 
6575065Swnj 	/*
6585065Swnj 	 * Ack processing.
6595065Swnj 	 */
6604601Swnj 	switch (tp->t_state) {
6614601Swnj 
6625065Swnj 	/*
6635065Swnj 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
66431721Skarels 	 * ESTABLISHED state and continue processing, otherwise
6655065Swnj 	 * send an RST.
6665065Swnj 	 */
6675065Swnj 	case TCPS_SYN_RECEIVED:
6685085Swnj 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
6695231Swnj 		    SEQ_GT(ti->ti_ack, tp->snd_max))
6705085Swnj 			goto dropwithreset;
67130525Skarels 		tcpstat.tcps_connects++;
6725085Swnj 		soisconnected(so);
6735085Swnj 		tp->t_state = TCPS_ESTABLISHED;
67417272Skarels 		tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
6755162Swnj 		(void) tcp_reass(tp, (struct tcpiphdr *)0);
6765244Sroot 		tp->snd_wl1 = ti->ti_seq - 1;
6775085Swnj 		/* fall into ... */
6784601Swnj 
6795065Swnj 	/*
6805065Swnj 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
6815065Swnj 	 * ACKs.  If the ack is in the range
6825231Swnj 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
6835065Swnj 	 * then advance tp->snd_una to ti->ti_ack and drop
6845065Swnj 	 * data from the retransmission queue.  If this ACK reflects
6855065Swnj 	 * more up to date window information we update our window information.
6865065Swnj 	 */
6875065Swnj 	case TCPS_ESTABLISHED:
6885065Swnj 	case TCPS_FIN_WAIT_1:
6895065Swnj 	case TCPS_FIN_WAIT_2:
6905065Swnj 	case TCPS_CLOSE_WAIT:
6915065Swnj 	case TCPS_CLOSING:
6925244Sroot 	case TCPS_LAST_ACK:
6935244Sroot 	case TCPS_TIME_WAIT:
6945085Swnj 
69530525Skarels 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
696*32098Skarels 			if (ti->ti_len == 0 && ti->ti_win == tp->snd_wnd) {
69730525Skarels 				tcpstat.tcps_rcvdupack++;
698*32098Skarels 				/*
699*32098Skarels 				 * If we have outstanding data (not a
700*32098Skarels 				 * window probe), this is a completely
701*32098Skarels 				 * duplicate ack (ie, window info didn't
702*32098Skarels 				 * change), the ack is the biggest we've
703*32098Skarels 				 * seen and we've seen exactly our rexmt
704*32098Skarels 				 * threshhold of them, assume a packet
705*32098Skarels 				 * has been dropped and retransmit it.
706*32098Skarels 				 * Kludge snd_nxt & the congestion
707*32098Skarels 				 * window so we send only this one
708*32098Skarels 				 * packet.  Close the congestion
709*32098Skarels 				 * window to half its current size
710*32098Skarels 				 * to prevent a restart burst if this
711*32098Skarels 				 * packet fills all the holes in the
712*32098Skarels 				 * receiver's sequence space and she
713*32098Skarels 				 * advertises a fully open window on
714*32098Skarels 				 * the next real ack.
715*32098Skarels 				 */
716*32098Skarels 				if (tp->t_timer[TCPT_REXMT] == 0 ||
717*32098Skarels 				    ti->ti_ack != tp->snd_una)
718*32098Skarels 					tp->t_dupacks = 0;
719*32098Skarels 				else if (++tp->t_dupacks == tcprexmtthresh) {
720*32098Skarels 					tcp_seq onxt = tp->snd_nxt;
721*32098Skarels 					u_short cwnd = tp->snd_cwnd;
722*32098Skarels 
723*32098Skarels 					tp->t_timer[TCPT_REXMT] = 0;
724*32098Skarels 					tp->t_rtt = 0;
725*32098Skarels 					tp->snd_nxt = ti->ti_ack;
726*32098Skarels 					tp->snd_cwnd = tp->t_maxseg;
727*32098Skarels 					(void) tcp_output(tp);
728*32098Skarels 
729*32098Skarels 					if (cwnd >= 4 * tp->t_maxseg)
730*32098Skarels 						tp->snd_cwnd = cwnd / 2;
731*32098Skarels 					if (SEQ_GT(onxt, tp->snd_nxt))
732*32098Skarels 						tp->snd_nxt = onxt;
733*32098Skarels 					goto drop;
734*32098Skarels 				}
735*32098Skarels 			} else
736*32098Skarels 				tp->t_dupacks = 0;
7375065Swnj 			break;
73830525Skarels 		}
739*32098Skarels 		tp->t_dupacks = 0;
74030525Skarels 		if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
74130525Skarels 			tcpstat.tcps_rcvacktoomuch++;
7425065Swnj 			goto dropafterack;
74330525Skarels 		}
7445085Swnj 		acked = ti->ti_ack - tp->snd_una;
74530525Skarels 		tcpstat.tcps_rcvackpack++;
74630525Skarels 		tcpstat.tcps_rcvackbyte += acked;
7475951Swnj 
7485951Swnj 		/*
7495951Swnj 		 * If transmit timer is running and timed sequence
7505951Swnj 		 * number was acked, update smoothed round trip time.
75132034Skarels 		 * Since we now have an rtt measurement, cancel the
75232034Skarels 		 * timer backoff (cf., Phil Karn's retransmit alg.).
75332034Skarels 		 * Recompute the initial retransmit timer.
7545951Swnj 		 */
7555951Swnj 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
75630525Skarels 			tcpstat.tcps_rttupdated++;
75731726Skarels 			if (tp->t_srtt != 0) {
75831726Skarels 				register short delta;
75931726Skarels 
76031726Skarels 				/*
76131726Skarels 				 * srtt is stored as fixed point with 3 bits
76231726Skarels 				 * after the binary point (i.e., scaled by 8).
76331726Skarels 				 * The following magic is equivalent
76431726Skarels 				 * to the smoothing algorithm in rfc793
76531726Skarels 				 * with an alpha of .875
76631726Skarels 				 * (srtt = rtt/8 + srtt*7/8 in fixed point).
767*32098Skarels 				 * Adjust t_rtt to origin 0.
76831726Skarels 				 */
769*32098Skarels 				tp->t_rtt--;
77031726Skarels 				delta = tp->t_rtt - (tp->t_srtt >> 3);
77131726Skarels 				if ((tp->t_srtt += delta) <= 0)
77231726Skarels 					tp->t_srtt = 1;
77331726Skarels 				/*
77432034Skarels 				 * We accumulate a smoothed rtt variance
77532034Skarels 				 * (actually, a smoothed mean difference),
77631726Skarels 				 * then set the retransmit timer to smoothed
77731726Skarels 				 * rtt + 2 times the smoothed variance.
778*32098Skarels 				 * rttvar is stored as fixed point
77931726Skarels 				 * with 2 bits after the binary point
78031726Skarels 				 * (scaled by 4).  The following is equivalent
78131726Skarels 				 * to rfc793 smoothing with an alpha of .75
78231726Skarels 				 * (rttvar = rttvar*3/4 + |delta| / 4).
78331726Skarels 				 * This replaces rfc793's wired-in beta.
78431726Skarels 				 */
78531726Skarels 				if (delta < 0)
78631726Skarels 					delta = -delta;
78731726Skarels 				delta -= (tp->t_rttvar >> 2);
78831726Skarels 				if ((tp->t_rttvar += delta) <= 0)
78931726Skarels 					tp->t_rttvar = 1;
79031726Skarels 			} else {
79131726Skarels 				/*
79231726Skarels 				 * No rtt measurement yet - use the
79331726Skarels 				 * unsmoothed rtt.  Set the variance
79431726Skarels 				 * to half the rtt (so our first
79531726Skarels 				 * retransmit happens at 2*rtt)
79631726Skarels 				 */
79731726Skarels 				tp->t_srtt = tp->t_rtt << 3;
79831726Skarels 				tp->t_rttvar = tp->t_rtt << 1;
79931726Skarels 			}
8005951Swnj 			tp->t_rtt = 0;
80132034Skarels 			tp->t_rxtshift = 0;
80232034Skarels 			TCPT_RANGESET(tp->t_rxtcur,
80332034Skarels 			    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
80432034Skarels 			    TCPTV_MIN, TCPTV_REXMTMAX);
8055951Swnj 		}
8065951Swnj 
80726824Skarels 		/*
80826824Skarels 		 * If all outstanding data is acked, stop retransmit
80926824Skarels 		 * timer and remember to restart (more output or persist).
81026824Skarels 		 * If there is more data to be acked, restart retransmit
81132034Skarels 		 * timer, using current (possibly backed-off) value.
81226824Skarels 		 */
81326824Skarels 		if (ti->ti_ack == tp->snd_max) {
8145244Sroot 			tp->t_timer[TCPT_REXMT] = 0;
81526824Skarels 			needoutput = 1;
81632034Skarels 		} else if (tp->t_timer[TCPT_PERSIST] == 0)
81732034Skarels 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
81817360Skarels 		/*
819*32098Skarels 		 * When new data is acked, open the congestion window.
820*32098Skarels 		 * If the window gives us less than ssthresh packets
821*32098Skarels 		 * in flight, open exponentially (maxseg per packet).
822*32098Skarels 		 * Otherwise open linearly (maxseg per window,
823*32098Skarels 		 * or maxseg^2 / cwnd per packet).
82417360Skarels 		 */
825*32098Skarels 		{
826*32098Skarels 		u_int incr = tp->t_maxseg;
827*32098Skarels 
828*32098Skarels 		if (tp->snd_cwnd > tp->snd_ssthresh)
829*32098Skarels 			incr = MAX(incr * incr / tp->snd_cwnd, 1);
830*32098Skarels 
831*32098Skarels 		tp->snd_cwnd = MIN(tp->snd_cwnd + incr, 65535); /* XXX */
832*32098Skarels 		}
8335307Sroot 		if (acked > so->so_snd.sb_cc) {
83415386Ssam 			tp->snd_wnd -= so->so_snd.sb_cc;
83526386Skarels 			sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
83631721Skarels 			ourfinisacked = 1;
8375307Sroot 		} else {
8386161Ssam 			sbdrop(&so->so_snd, acked);
8395307Sroot 			tp->snd_wnd -= acked;
84031721Skarels 			ourfinisacked = 0;
8415307Sroot 		}
8426434Swnj 		if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel)
8435300Sroot 			sowwakeup(so);
8445231Swnj 		tp->snd_una = ti->ti_ack;
8455357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
8465357Sroot 			tp->snd_nxt = tp->snd_una;
8475162Swnj 
8484601Swnj 		switch (tp->t_state) {
8494601Swnj 
8505065Swnj 		/*
8515065Swnj 		 * In FIN_WAIT_1 STATE in addition to the processing
8525065Swnj 		 * for the ESTABLISHED state if our FIN is now acknowledged
8535085Swnj 		 * then enter FIN_WAIT_2.
8545065Swnj 		 */
8555065Swnj 		case TCPS_FIN_WAIT_1:
8565896Swnj 			if (ourfinisacked) {
8575896Swnj 				/*
8585896Swnj 				 * If we can't receive any more
8595896Swnj 				 * data, then closing user can proceed.
86024816Skarels 				 * Starting the timer is contrary to the
86124816Skarels 				 * specification, but if we don't get a FIN
86224816Skarels 				 * we'll hang forever.
8635896Swnj 				 */
86424816Skarels 				if (so->so_state & SS_CANTRCVMORE) {
8655896Swnj 					soisdisconnected(so);
86624816Skarels 					tp->t_timer[TCPT_2MSL] = TCPTV_MAXIDLE;
86724816Skarels 				}
8685085Swnj 				tp->t_state = TCPS_FIN_WAIT_2;
8695896Swnj 			}
8704601Swnj 			break;
8714601Swnj 
8725065Swnj 	 	/*
8735065Swnj 		 * In CLOSING STATE in addition to the processing for
8745065Swnj 		 * the ESTABLISHED state if the ACK acknowledges our FIN
8755065Swnj 		 * then enter the TIME-WAIT state, otherwise ignore
8765065Swnj 		 * the segment.
8775065Swnj 		 */
8785065Swnj 		case TCPS_CLOSING:
8795244Sroot 			if (ourfinisacked) {
8805065Swnj 				tp->t_state = TCPS_TIME_WAIT;
8815244Sroot 				tcp_canceltimers(tp);
8825244Sroot 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
8835244Sroot 				soisdisconnected(so);
8845244Sroot 			}
8855244Sroot 			break;
8864601Swnj 
8875065Swnj 		/*
88831743Skarels 		 * In LAST_ACK, we may still be waiting for data to drain
88931743Skarels 		 * and/or to be acked, as well as for the ack of our FIN.
89031743Skarels 		 * If our FIN is now acknowledged, delete the TCB,
89131743Skarels 		 * enter the closed state and return.
8925065Swnj 		 */
8935065Swnj 		case TCPS_LAST_ACK:
89431743Skarels 			if (ourfinisacked) {
89510394Ssam 				tp = tcp_close(tp);
89631743Skarels 				goto drop;
89731743Skarels 			}
89831743Skarels 			break;
8994601Swnj 
9005065Swnj 		/*
9015065Swnj 		 * In TIME_WAIT state the only thing that should arrive
9025065Swnj 		 * is a retransmission of the remote FIN.  Acknowledge
9035065Swnj 		 * it and restart the finack timer.
9045065Swnj 		 */
9055065Swnj 		case TCPS_TIME_WAIT:
9065162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
9075065Swnj 			goto dropafterack;
9084601Swnj 		}
9095085Swnj 	}
9104601Swnj 
9115065Swnj step6:
9125065Swnj 	/*
9135244Sroot 	 * Update window information.
91425939Skarels 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
9155244Sroot 	 */
91625939Skarels 	if ((tiflags & TH_ACK) &&
91725939Skarels 	    (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
9185391Swnj 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
91925939Skarels 	     tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) {
92030525Skarels 		/* keep track of pure window updates */
92130525Skarels 		if (ti->ti_len == 0 &&
922*32098Skarels 		    tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)
92330525Skarels 			tcpstat.tcps_rcvwinupd++;
9245244Sroot 		tp->snd_wnd = ti->ti_win;
9255244Sroot 		tp->snd_wl1 = ti->ti_seq;
9265244Sroot 		tp->snd_wl2 = ti->ti_ack;
92725260Skarels 		if (tp->snd_wnd > tp->max_sndwnd)
92825260Skarels 			tp->max_sndwnd = tp->snd_wnd;
92926824Skarels 		needoutput = 1;
93026824Skarels 	}
9315244Sroot 
9325244Sroot 	/*
9335547Swnj 	 * Process segments with URG.
9345065Swnj 	 */
9357267Swnj 	if ((tiflags & TH_URG) && ti->ti_urp &&
9367267Swnj 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
9375547Swnj 		/*
93825939Skarels 		 * This is a kludge, but if we receive and accept
93913121Ssam 		 * random urgent pointers, we'll crash in
94013121Ssam 		 * soreceive.  It's hard to imagine someone
94113121Ssam 		 * actually wanting to send this much urgent data.
94212441Ssam 		 */
94325652Skarels 		if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) {
94412441Ssam 			ti->ti_urp = 0;			/* XXX */
94512441Ssam 			tiflags &= ~TH_URG;		/* XXX */
94625939Skarels 			goto dodata;			/* XXX */
94712441Ssam 		}
94812441Ssam 		/*
9495547Swnj 		 * If this segment advances the known urgent pointer,
9505547Swnj 		 * then mark the data stream.  This should not happen
9515547Swnj 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
9525547Swnj 		 * a FIN has been received from the remote side.
9535547Swnj 		 * In these states we ignore the URG.
95427190Skarels 		 *
95527190Skarels 		 * According to RFC961 (Assigned Protocols),
95627190Skarels 		 * the urgent pointer points to the last octet
95727190Skarels 		 * of urgent data.  We continue, however,
95827190Skarels 		 * to consider it to indicate the first octet
95927190Skarels 		 * of data past the urgent section
96027190Skarels 		 * as the original spec states.
9615547Swnj 		 */
9625547Swnj 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
9635547Swnj 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
9645547Swnj 			so->so_oobmark = so->so_rcv.sb_cc +
9655547Swnj 			    (tp->rcv_up - tp->rcv_nxt) - 1;
9665547Swnj 			if (so->so_oobmark == 0)
9675547Swnj 				so->so_state |= SS_RCVATMARK;
9688313Sroot 			sohasoutofband(so);
96924816Skarels 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
9705440Swnj 		}
9715547Swnj 		/*
9725547Swnj 		 * Remove out of band data so doesn't get presented to user.
9735547Swnj 		 * This can happen independent of advancing the URG pointer,
9745547Swnj 		 * but if two URG's are pending at once, some out-of-band
9755547Swnj 		 * data may creep in... ick.
9765547Swnj 		 */
97727190Skarels 		if (ti->ti_urp <= ti->ti_len &&
97827190Skarels 		    (so->so_options & SO_OOBINLINE) == 0)
9795547Swnj 			tcp_pulloutofband(so, ti);
98025939Skarels 	} else
98125939Skarels 		/*
98225939Skarels 		 * If no out of band data is expected,
98325939Skarels 		 * pull receive urgent pointer along
98425939Skarels 		 * with the receive window.
98525939Skarels 		 */
98625939Skarels 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
98725939Skarels 			tp->rcv_up = tp->rcv_nxt;
98825939Skarels dodata:							/* XXX */
9894601Swnj 
9904601Swnj 	/*
9915065Swnj 	 * Process the segment text, merging it into the TCP sequencing queue,
9925065Swnj 	 * and arranging for acknowledgment of receipt if necessary.
9935065Swnj 	 * This process logically involves adjusting tp->rcv_wnd as data
9945065Swnj 	 * is presented to the user (this happens in tcp_usrreq.c,
9955065Swnj 	 * case PRU_RCVD).  If a FIN has already been received on this
9965065Swnj 	 * connection then we just ignore the text.
9974601Swnj 	 */
99817946Skarels 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
99917946Skarels 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
100024816Skarels 		TCP_REASS(tp, ti, m, so, tiflags);
10015440Swnj 		if (tcpnodelack == 0)
10025440Swnj 			tp->t_flags |= TF_DELACK;
10035440Swnj 		else
10045440Swnj 			tp->t_flags |= TF_ACKNOW;
100525260Skarels 		/*
100625260Skarels 		 * Note the amount of data that peer has sent into
100725260Skarels 		 * our window, in order to estimate the sender's
100825260Skarels 		 * buffer size.
100925260Skarels 		 */
1010*32098Skarels 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
101125260Skarels 		if (len > tp->max_rcvd)
101225260Skarels 			tp->max_rcvd = len;
10135244Sroot 	} else {
10144924Swnj 		m_freem(m);
10155263Swnj 		tiflags &= ~TH_FIN;
10165244Sroot 	}
10174601Swnj 
10184601Swnj 	/*
10195263Swnj 	 * If FIN is received ACK the FIN and let the user know
10205263Swnj 	 * that the connection is closing.
10214601Swnj 	 */
10225263Swnj 	if (tiflags & TH_FIN) {
10235244Sroot 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
10245244Sroot 			socantrcvmore(so);
10255244Sroot 			tp->t_flags |= TF_ACKNOW;
10265244Sroot 			tp->rcv_nxt++;
10275244Sroot 		}
10285065Swnj 		switch (tp->t_state) {
10294601Swnj 
10305065Swnj 	 	/*
10315065Swnj 		 * In SYN_RECEIVED and ESTABLISHED STATES
10325065Swnj 		 * enter the CLOSE_WAIT state.
10334884Swnj 		 */
10345065Swnj 		case TCPS_SYN_RECEIVED:
10355065Swnj 		case TCPS_ESTABLISHED:
10365065Swnj 			tp->t_state = TCPS_CLOSE_WAIT;
10375065Swnj 			break;
10384884Swnj 
10395065Swnj 	 	/*
10405085Swnj 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
10415085Swnj 		 * enter the CLOSING state.
10424884Swnj 		 */
10435065Swnj 		case TCPS_FIN_WAIT_1:
10445085Swnj 			tp->t_state = TCPS_CLOSING;
10455065Swnj 			break;
10464601Swnj 
10475065Swnj 	 	/*
10485065Swnj 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
10495065Swnj 		 * starting the time-wait timer, turning off the other
10505065Swnj 		 * standard timers.
10515065Swnj 		 */
10525065Swnj 		case TCPS_FIN_WAIT_2:
10535244Sroot 			tp->t_state = TCPS_TIME_WAIT;
10545074Swnj 			tcp_canceltimers(tp);
10555162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
10565244Sroot 			soisdisconnected(so);
10575065Swnj 			break;
10585065Swnj 
10594884Swnj 		/*
10605065Swnj 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
10614884Swnj 		 */
10625065Swnj 		case TCPS_TIME_WAIT:
10635162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
10645065Swnj 			break;
10655085Swnj 		}
10664601Swnj 	}
10675267Sroot 	if (so->so_options & SO_DEBUG)
10685267Sroot 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
10695085Swnj 
10705085Swnj 	/*
10715085Swnj 	 * Return any desired output.
10725085Swnj 	 */
107326824Skarels 	if (needoutput || (tp->t_flags & TF_ACKNOW))
107425939Skarels 		(void) tcp_output(tp);
10755065Swnj 	return;
10765085Swnj 
10775065Swnj dropafterack:
10785085Swnj 	/*
10796211Swnj 	 * Generate an ACK dropping incoming segment if it occupies
10806211Swnj 	 * sequence space, where the ACK reflects our state.
10815085Swnj 	 */
108226057Skarels 	if (tiflags & TH_RST)
10835085Swnj 		goto drop;
108431749Skarels 	m_freem(m);
108531721Skarels 	tp->t_flags |= TF_ACKNOW;
108631721Skarels 	(void) tcp_output(tp);
10875231Swnj 	return;
10885085Swnj 
10895085Swnj dropwithreset:
109011731Ssam 	if (om) {
10916161Ssam 		(void) m_free(om);
109211731Ssam 		om = 0;
109311731Ssam 	}
10945085Swnj 	/*
10955244Sroot 	 * Generate a RST, dropping incoming segment.
10965085Swnj 	 * Make ACK acceptable to originator of segment.
109725197Skarels 	 * Don't bother to respond if destination was broadcast.
10985085Swnj 	 */
109925197Skarels 	if ((tiflags & TH_RST) || in_broadcast(ti->ti_dst))
11005085Swnj 		goto drop;
11015085Swnj 	if (tiflags & TH_ACK)
11025391Swnj 		tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST);
11035085Swnj 	else {
11045085Swnj 		if (tiflags & TH_SYN)
11055085Swnj 			ti->ti_len++;
11066211Swnj 		tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0,
11076211Swnj 		    TH_RST|TH_ACK);
11085085Swnj 	}
110910769Ssam 	/* destroy temporarily created socket */
111010769Ssam 	if (dropsocket)
111110769Ssam 		(void) soabort(so);
11125231Swnj 	return;
11135085Swnj 
11145065Swnj drop:
111511730Ssam 	if (om)
111611730Ssam 		(void) m_free(om);
11175085Swnj 	/*
11185085Swnj 	 * Drop space held by incoming segment and return.
11195085Swnj 	 */
11206303Sroot 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
11216303Sroot 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
11225065Swnj 	m_freem(m);
112310769Ssam 	/* destroy temporarily created socket */
112410769Ssam 	if (dropsocket)
112510769Ssam 		(void) soabort(so);
11265267Sroot 	return;
11275065Swnj }
11285065Swnj 
112917272Skarels tcp_dooptions(tp, om, ti)
11305440Swnj 	struct tcpcb *tp;
11315440Swnj 	struct mbuf *om;
113217272Skarels 	struct tcpiphdr *ti;
11335419Swnj {
11345440Swnj 	register u_char *cp;
11355440Swnj 	int opt, optlen, cnt;
11365419Swnj 
11375440Swnj 	cp = mtod(om, u_char *);
11385440Swnj 	cnt = om->m_len;
11395440Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
11405440Swnj 		opt = cp[0];
11415440Swnj 		if (opt == TCPOPT_EOL)
11425440Swnj 			break;
11435440Swnj 		if (opt == TCPOPT_NOP)
11445440Swnj 			optlen = 1;
114512169Ssam 		else {
11465440Swnj 			optlen = cp[1];
114712169Ssam 			if (optlen <= 0)
114812169Ssam 				break;
114912169Ssam 		}
11505440Swnj 		switch (opt) {
11515440Swnj 
11525440Swnj 		default:
11535440Swnj 			break;
11545440Swnj 
11555440Swnj 		case TCPOPT_MAXSEG:
11565440Swnj 			if (optlen != 4)
11575440Swnj 				continue;
115817272Skarels 			if (!(ti->ti_flags & TH_SYN))
115917272Skarels 				continue;
11605440Swnj 			tp->t_maxseg = *(u_short *)(cp + 2);
11616161Ssam 			tp->t_maxseg = ntohs((u_short)tp->t_maxseg);
116217272Skarels 			tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
11635440Swnj 			break;
11645419Swnj 		}
11655419Swnj 	}
11666161Ssam 	(void) m_free(om);
11675419Swnj }
11685419Swnj 
11695419Swnj /*
11705547Swnj  * Pull out of band byte out of a segment so
11715547Swnj  * it doesn't appear in the user's data queue.
11725547Swnj  * It is still reflected in the segment length for
11735547Swnj  * sequencing purposes.
11745547Swnj  */
11755547Swnj tcp_pulloutofband(so, ti)
11765547Swnj 	struct socket *so;
11775547Swnj 	struct tcpiphdr *ti;
11785547Swnj {
11795547Swnj 	register struct mbuf *m;
11806116Swnj 	int cnt = ti->ti_urp - 1;
11815547Swnj 
11825547Swnj 	m = dtom(ti);
11835547Swnj 	while (cnt >= 0) {
11845547Swnj 		if (m->m_len > cnt) {
11855547Swnj 			char *cp = mtod(m, caddr_t) + cnt;
11865547Swnj 			struct tcpcb *tp = sototcpcb(so);
11875547Swnj 
11885547Swnj 			tp->t_iobc = *cp;
11895547Swnj 			tp->t_oobflags |= TCPOOB_HAVEDATA;
11906161Ssam 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
11915547Swnj 			m->m_len--;
11925547Swnj 			return;
11935547Swnj 		}
11945547Swnj 		cnt -= m->m_len;
11955547Swnj 		m = m->m_next;
11965547Swnj 		if (m == 0)
11975547Swnj 			break;
11985547Swnj 	}
11995547Swnj 	panic("tcp_pulloutofband");
12005547Swnj }
12015547Swnj 
12025547Swnj /*
120317272Skarels  *  Determine a reasonable value for maxseg size.
120417272Skarels  *  If the route is known, use one that can be handled
120517272Skarels  *  on the given interface without forcing IP to fragment.
120631726Skarels  *  If bigger than an mbuf cluster (MCLBYTES), round down to nearest size
120731726Skarels  *  to utilize large mbufs.
120817272Skarels  *  If interface pointer is unavailable, or the destination isn't local,
120923975Skarels  *  use a conservative size (512 or the default IP max size, but no more
121023975Skarels  *  than the mtu of the interface through which we route),
121117272Skarels  *  as we can't discover anything about intervening gateways or networks.
121232034Skarels  *  We also initialize the congestion/slow start window to be a single
121332034Skarels  *  segment if the destination isn't local; this information should
121432034Skarels  *  probably all be saved with the routing entry at the transport level.
121517272Skarels  *
121617272Skarels  *  This is ugly, and doesn't belong at this level, but has to happen somehow.
121717272Skarels  */
121817272Skarels tcp_mss(tp)
121923975Skarels 	register struct tcpcb *tp;
122017272Skarels {
122117272Skarels 	struct route *ro;
122217272Skarels 	struct ifnet *ifp;
122317272Skarels 	int mss;
122417272Skarels 	struct inpcb *inp;
122517272Skarels 
122617272Skarels 	inp = tp->t_inpcb;
122717272Skarels 	ro = &inp->inp_route;
122817272Skarels 	if ((ro->ro_rt == (struct rtentry *)0) ||
122917272Skarels 	    (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) {
123017272Skarels 		/* No route yet, so try to acquire one */
123117272Skarels 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
123217272Skarels 			ro->ro_dst.sa_family = AF_INET;
123317272Skarels 			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
123417272Skarels 				inp->inp_faddr;
123517272Skarels 			rtalloc(ro);
123617272Skarels 		}
123717272Skarels 		if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0)
123817316Skarels 			return (TCP_MSS);
123917272Skarels 	}
124017272Skarels 
124117272Skarels 	mss = ifp->if_mtu - sizeof(struct tcpiphdr);
124231726Skarels #if	(MCLBYTES & (MCLBYTES - 1)) == 0
124331726Skarels 	if (mss > MCLBYTES)
124431726Skarels 		mss &= ~(MCLBYTES-1);
124517272Skarels #else
124631726Skarels 	if (mss > MCLBYTES)
124731726Skarels 		mss = mss / MCLBYTES * MCLBYTES;
124817272Skarels #endif
124923975Skarels 	if (in_localaddr(inp->inp_faddr))
125023975Skarels 		return (mss);
1251*32098Skarels 
125232034Skarels 	mss = MIN(mss, TCP_MSS);
125332034Skarels 	tp->snd_cwnd = mss;
125432034Skarels 	return (mss);
125517272Skarels }
1256