xref: /csrg-svn/sys/netinet/tcp_input.c (revision 7267)
1*7267Swnj /*	tcp_input.c	1.69	82/06/24	*/
24601Swnj 
34601Swnj #include "../h/param.h"
44601Swnj #include "../h/systm.h"
54663Swnj #include "../h/mbuf.h"
65085Swnj #include "../h/protosw.h"
74663Swnj #include "../h/socket.h"
84803Swnj #include "../h/socketvar.h"
95085Swnj #include "../net/in.h"
106351Ssam #include "../net/route.h"
115085Swnj #include "../net/in_pcb.h"
125085Swnj #include "../net/in_systm.h"
135085Swnj #include "../net/if.h"
144803Swnj #include "../net/ip.h"
154899Swnj #include "../net/ip_var.h"
164803Swnj #include "../net/tcp.h"
174803Swnj #include "../net/tcp_fsm.h"
185085Swnj #include "../net/tcp_seq.h"
195085Swnj #include "../net/tcp_timer.h"
204803Swnj #include "../net/tcp_var.h"
215085Swnj #include "../net/tcpip.h"
225267Sroot #include "../net/tcp_debug.h"
235109Swnj #include "../errno.h"
244601Swnj 
255300Sroot int	tcpprintfs = 0;
264679Swnj int	tcpcksum = 1;
275244Sroot struct	sockaddr_in tcp_in = { AF_INET };
285267Sroot struct	tcpiphdr tcp_saveti;
295440Swnj extern	tcpnodelack;
304601Swnj 
315267Sroot struct	tcpcb *tcp_newtcpcb();
325065Swnj /*
335065Swnj  * TCP input routine, follows pages 65-76 of the
345065Swnj  * protocol specification dated September, 1981 very closely.
355065Swnj  */
364924Swnj tcp_input(m0)
374924Swnj 	struct mbuf *m0;
384601Swnj {
394924Swnj 	register struct tcpiphdr *ti;
404924Swnj 	struct inpcb *inp;
414924Swnj 	register struct mbuf *m;
425440Swnj 	struct mbuf *om = 0;
434924Swnj 	int len, tlen, off;
445391Swnj 	register struct tcpcb *tp = 0;
454924Swnj 	register int tiflags;
464803Swnj 	struct socket *so;
475109Swnj 	int todrop, acked;
485267Sroot 	short ostate;
496028Sroot 	struct in_addr laddr;
504924Swnj 
514924Swnj 	/*
525244Sroot 	 * Get IP and TCP header together in first mbuf.
535244Sroot 	 * Note: IP leaves IP header in first mbuf.
544924Swnj 	 */
554924Swnj 	m = m0;
565020Sroot 	ti = mtod(m, struct tcpiphdr *);
575244Sroot 	if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2))
585208Swnj 		ip_stripoptions((struct ip *)ti, (struct mbuf *)0);
595307Sroot 	if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) {
605307Sroot 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
615085Swnj 			tcpstat.tcps_hdrops++;
625307Sroot 			return;
635085Swnj 		}
645085Swnj 		ti = mtod(m, struct tcpiphdr *);
655085Swnj 	}
664601Swnj 
674601Swnj 	/*
685244Sroot 	 * Checksum extended TCP header and data.
694601Swnj 	 */
704924Swnj 	tlen = ((struct ip *)ti)->ip_len;
714924Swnj 	len = sizeof (struct ip) + tlen;
724679Swnj 	if (tcpcksum) {
734924Swnj 		ti->ti_next = ti->ti_prev = 0;
744924Swnj 		ti->ti_x1 = 0;
755223Swnj 		ti->ti_len = (u_short)tlen;
766320Swnj #if vax || pdp11
776161Ssam 		ti->ti_len = htons((u_short)ti->ti_len);
785223Swnj #endif
795231Swnj 		if (ti->ti_sum = in_cksum(m, len)) {
804924Swnj 			tcpstat.tcps_badsum++;
816211Swnj 			if (tcpprintfs)
826211Swnj 				printf("tcp cksum %x\n", ti->ti_sum);
835085Swnj 			goto drop;
844601Swnj 		}
854601Swnj 	}
864601Swnj 
874601Swnj 	/*
885244Sroot 	 * Check that TCP offset makes sense,
895440Swnj 	 * pull out TCP options and adjust length.
904601Swnj 	 */
914924Swnj 	off = ti->ti_off << 2;
925231Swnj 	if (off < sizeof (struct tcphdr) || off > tlen) {
934924Swnj 		tcpstat.tcps_badoff++;
945085Swnj 		goto drop;
954924Swnj 	}
966211Swnj 	tlen -= off;
976211Swnj 	ti->ti_len = tlen;
985440Swnj 	if (off > sizeof (struct tcphdr)) {
995440Swnj 		if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
1005440Swnj 			tcpstat.tcps_hdrops++;
1015440Swnj 			goto drop;
1025440Swnj 		}
1035440Swnj 		ti = mtod(m, struct tcpiphdr *);
1045440Swnj 		om = m_get(M_DONTWAIT);
1055440Swnj 		if (om == 0)
1065440Swnj 			goto drop;
1075440Swnj 		om->m_off = MMINOFF;
1085440Swnj 		om->m_len = off - sizeof (struct tcphdr);
1095440Swnj 		{ caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
1106161Ssam 		  bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len);
1115440Swnj 		  m->m_len -= om->m_len;
1126161Ssam 		  bcopy(op+om->m_len, op,
1136161Ssam 		   (unsigned)(m->m_len-sizeof (struct tcpiphdr)));
1145440Swnj 		}
1155440Swnj 	}
1165065Swnj 	tiflags = ti->ti_flags;
1174924Swnj 
1186093Sroot 	/*
1196211Swnj 	 * Drop TCP and IP headers.
1206093Sroot 	 */
1216093Sroot 	off += sizeof (struct ip);
1226093Sroot 	m->m_off += off;
1236093Sroot 	m->m_len -= off;
1246093Sroot 
1256320Swnj #if vax || pdp11
1264924Swnj 	/*
1275244Sroot 	 * Convert TCP protocol specific fields to host format.
1285085Swnj 	 */
1295085Swnj 	ti->ti_seq = ntohl(ti->ti_seq);
1305085Swnj 	ti->ti_ack = ntohl(ti->ti_ack);
1315085Swnj 	ti->ti_win = ntohs(ti->ti_win);
1325085Swnj 	ti->ti_urp = ntohs(ti->ti_urp);
1335231Swnj #endif
1345085Swnj 
1355085Swnj 	/*
1365994Swnj 	 * Locate pcb for segment.  On match, update the local
1375994Swnj 	 * address stored in the block to reflect anchoring.
1384924Swnj 	 */
1395065Swnj 	inp = in_pcblookup
1406028Sroot 		(&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport,
1416028Sroot 		INPLOOKUP_WILDCARD);
1425065Swnj 
1435065Swnj 	/*
1445065Swnj 	 * If the state is CLOSED (i.e., TCB does not exist) then
1455244Sroot 	 * all data in the incoming segment is discarded.
1465065Swnj 	 */
1475300Sroot 	if (inp == 0)
1485085Swnj 		goto dropwithreset;
1495065Swnj 	tp = intotcpcb(inp);
1505300Sroot 	if (tp == 0)
1515085Swnj 		goto dropwithreset;
1525109Swnj 	so = inp->inp_socket;
1535267Sroot 	if (so->so_options & SO_DEBUG) {
1545267Sroot 		ostate = tp->t_state;
1555267Sroot 		tcp_saveti = *ti;
1565267Sroot 	}
1574601Swnj 
1584601Swnj 	/*
1595162Swnj 	 * Segment received on connection.
1605162Swnj 	 * Reset idle time and keep-alive timer.
1615162Swnj 	 */
1625162Swnj 	tp->t_idle = 0;
1635162Swnj 	tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
1645162Swnj 
1655162Swnj 	/*
1665440Swnj 	 * Process options.
1675440Swnj 	 */
1685440Swnj 	if (om) {
1695440Swnj 		tcp_dooptions(tp, om);
1705440Swnj 		om = 0;
1715440Swnj 	}
1725440Swnj 
1735440Swnj 	/*
1745085Swnj 	 * Calculate amount of space in receive window,
1755085Swnj 	 * and then do TCP input processing.
1764601Swnj 	 */
1775085Swnj 	tp->rcv_wnd = sbspace(&so->so_rcv);
1785231Swnj 	if (tp->rcv_wnd < 0)
1795231Swnj 		tp->rcv_wnd = 0;
1804601Swnj 
1814601Swnj 	switch (tp->t_state) {
1824601Swnj 
1835065Swnj 	/*
1845065Swnj 	 * If the state is LISTEN then ignore segment if it contains an RST.
1855065Swnj 	 * If the segment contains an ACK then it is bad and send a RST.
1865065Swnj 	 * If it does not contain a SYN then it is not interesting; drop it.
1875085Swnj 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
1885065Swnj 	 * tp->iss, and send a segment:
1895085Swnj 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
1905065Swnj 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
1915065Swnj 	 * Fill in remote peer address fields if not previously specified.
1925065Swnj 	 * Enter SYN_RECEIVED state, and process any other fields of this
1935244Sroot 	 * segment in this state.
1945065Swnj 	 */
1955065Swnj 	case TCPS_LISTEN:
1965065Swnj 		if (tiflags & TH_RST)
1975065Swnj 			goto drop;
1985300Sroot 		if (tiflags & TH_ACK)
1995085Swnj 			goto dropwithreset;
2005300Sroot 		if ((tiflags & TH_SYN) == 0)
2015065Swnj 			goto drop;
2025244Sroot 		tcp_in.sin_addr = ti->ti_src;
2035244Sroot 		tcp_in.sin_port = ti->ti_sport;
2046028Sroot 		laddr = inp->inp_laddr;
2056028Sroot 		if (inp->inp_laddr.s_addr == 0)
2066028Sroot 			inp->inp_laddr = ti->ti_dst;
2076161Ssam 		if (in_pcbconnect(inp, (struct sockaddr_in *)&tcp_in)) {
2086028Sroot 			inp->inp_laddr = laddr;
2095244Sroot 			goto drop;
2106028Sroot 		}
2115244Sroot 		tp->t_template = tcp_template(tp);
2125244Sroot 		if (tp->t_template == 0) {
2135244Sroot 			in_pcbdisconnect(inp);
2146028Sroot 			inp->inp_laddr = laddr;
2156320Swnj 			tp = 0;
2165244Sroot 			goto drop;
2175244Sroot 		}
2185085Swnj 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
2195065Swnj 		tp->irs = ti->ti_seq;
2205085Swnj 		tcp_sendseqinit(tp);
2215085Swnj 		tcp_rcvseqinit(tp);
2225065Swnj 		tp->t_state = TCPS_SYN_RECEIVED;
2235244Sroot 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
2245085Swnj 		goto trimthenstep6;
2254601Swnj 
2265065Swnj 	/*
2275065Swnj 	 * If the state is SYN_SENT:
2285065Swnj 	 *	if seg contains an ACK, but not for our SYN, drop the input.
2295065Swnj 	 *	if seg contains a RST, then drop the connection.
2305065Swnj 	 *	if seg does not contain SYN, then drop it.
2315065Swnj 	 * Otherwise this is an acceptable SYN segment
2325065Swnj 	 *	initialize tp->rcv_nxt and tp->irs
2335065Swnj 	 *	if seg contains ack then advance tp->snd_una
2345065Swnj 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
2355065Swnj 	 *	arrange for segment to be acked (eventually)
2365065Swnj 	 *	continue processing rest of data/controls, beginning with URG
2375065Swnj 	 */
2385065Swnj 	case TCPS_SYN_SENT:
2395065Swnj 		if ((tiflags & TH_ACK) &&
2405300Sroot /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */
2415300Sroot 		    (SEQ_LT(ti->ti_ack, tp->iss) ||
2425231Swnj 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
2435085Swnj 			goto dropwithreset;
2445065Swnj 		if (tiflags & TH_RST) {
2456320Swnj 			if (tiflags & TH_ACK) {
2465267Sroot 				tcp_drop(tp, ECONNREFUSED);
2476320Swnj 				tp = 0;
2486320Swnj 			}
2495065Swnj 			goto drop;
2504601Swnj 		}
2515065Swnj 		if ((tiflags & TH_SYN) == 0)
2525065Swnj 			goto drop;
2535231Swnj 		tp->snd_una = ti->ti_ack;
2545357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2555357Sroot 			tp->snd_nxt = tp->snd_una;
2565244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
2575065Swnj 		tp->irs = ti->ti_seq;
2585085Swnj 		tcp_rcvseqinit(tp);
2595085Swnj 		tp->t_flags |= TF_ACKNOW;
2605162Swnj 		if (SEQ_GT(tp->snd_una, tp->iss)) {
2615391Swnj 			if (so->so_options & SO_ACCEPTCONN)
2625391Swnj 				so->so_state |= SS_CONNAWAITING;
2635244Sroot 			soisconnected(so);
2645065Swnj 			tp->t_state = TCPS_ESTABLISHED;
2655162Swnj 			(void) tcp_reass(tp, (struct tcpiphdr *)0);
2665162Swnj 		} else
2675085Swnj 			tp->t_state = TCPS_SYN_RECEIVED;
2685085Swnj 		goto trimthenstep6;
2695085Swnj 
2705085Swnj trimthenstep6:
2715085Swnj 		/*
2725231Swnj 		 * Advance ti->ti_seq to correspond to first data byte.
2735085Swnj 		 * If data, trim to stay within window,
2745085Swnj 		 * dropping FIN if necessary.
2755085Swnj 		 */
2765231Swnj 		ti->ti_seq++;
2775085Swnj 		if (ti->ti_len > tp->rcv_wnd) {
2785085Swnj 			todrop = ti->ti_len - tp->rcv_wnd;
2795085Swnj 			m_adj(m, -todrop);
2805085Swnj 			ti->ti_len = tp->rcv_wnd;
2815085Swnj 			ti->ti_flags &= ~TH_FIN;
2825065Swnj 		}
2835263Swnj 		tp->snd_wl1 = ti->ti_seq - 1;
2845085Swnj 		goto step6;
2855065Swnj 	}
2864601Swnj 
2875065Swnj 	/*
2885065Swnj 	 * States other than LISTEN or SYN_SENT.
2895065Swnj 	 * First check that at least some bytes of segment are within
2905065Swnj 	 * receive window.
2915065Swnj 	 */
2925065Swnj 	if (tp->rcv_wnd == 0) {
2935065Swnj 		/*
2945065Swnj 		 * If window is closed can only take segments at
2955231Swnj 		 * window edge, and have to drop data and PUSH from
2965065Swnj 		 * incoming segments.
2975065Swnj 		 */
2985300Sroot 		if (tp->rcv_nxt != ti->ti_seq)
2995065Swnj 			goto dropafterack;
3005085Swnj 		if (ti->ti_len > 0) {
3015690Swnj 			m_adj(m, ti->ti_len);
3025085Swnj 			ti->ti_len = 0;
3035085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
3045065Swnj 		}
3055065Swnj 	} else {
3065065Swnj 		/*
3075231Swnj 		 * If segment begins before rcv_nxt, drop leading
3085065Swnj 		 * data (and SYN); if nothing left, just ack.
3095065Swnj 		 */
3105690Swnj 		todrop = tp->rcv_nxt - ti->ti_seq;
3115690Swnj 		if (todrop > 0) {
3125085Swnj 			if (tiflags & TH_SYN) {
3135300Sroot 				tiflags &= ~TH_SYN;
3145690Swnj 				ti->ti_flags &= ~TH_SYN;
3155085Swnj 				ti->ti_seq++;
3165085Swnj 				if (ti->ti_urp > 1)
3175085Swnj 					ti->ti_urp--;
3185085Swnj 				else
3195085Swnj 					tiflags &= ~TH_URG;
3205085Swnj 				todrop--;
3215085Swnj 			}
3226211Swnj 			if (todrop > ti->ti_len ||
3236211Swnj 			    todrop == ti->ti_len && (tiflags&TH_FIN) == 0)
3245065Swnj 				goto dropafterack;
3255065Swnj 			m_adj(m, todrop);
3265065Swnj 			ti->ti_seq += todrop;
3275065Swnj 			ti->ti_len -= todrop;
3285085Swnj 			if (ti->ti_urp > todrop)
3295085Swnj 				ti->ti_urp -= todrop;
3305085Swnj 			else {
3315085Swnj 				tiflags &= ~TH_URG;
3325690Swnj 				ti->ti_flags &= ~TH_URG;
3335690Swnj 				ti->ti_urp = 0;
3345085Swnj 			}
3355065Swnj 		}
3365065Swnj 		/*
3375065Swnj 		 * If segment ends after window, drop trailing data
3385085Swnj 		 * (and PUSH and FIN); if nothing left, just ACK.
3395065Swnj 		 */
3405690Swnj 		todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
3415690Swnj 		if (todrop > 0) {
3426211Swnj 			if (todrop >= ti->ti_len)
3435065Swnj 				goto dropafterack;
3445065Swnj 			m_adj(m, -todrop);
3455065Swnj 			ti->ti_len -= todrop;
3465085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
3475065Swnj 		}
3485065Swnj 	}
3494601Swnj 
3505065Swnj 	/*
3515951Swnj 	 * If a segment is received on a connection after the
3525951Swnj 	 * user processes are gone, then RST the other end.
3535951Swnj 	 */
3545951Swnj 	if (so->so_state & SS_USERGONE) {
3555951Swnj 		tcp_close(tp);
3566266Swnj 		tp = 0;
3575951Swnj 		goto dropwithreset;
3585951Swnj 	}
3595951Swnj 
3605951Swnj 	/*
3615065Swnj 	 * If the RST bit is set examine the state:
3625065Swnj 	 *    SYN_RECEIVED STATE:
3635065Swnj 	 *	If passive open, return to LISTEN state.
3645065Swnj 	 *	If active open, inform user that connection was refused.
3655065Swnj 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
3665065Swnj 	 *	Inform user that connection was reset, and close tcb.
3675065Swnj 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
3685065Swnj 	 *	Close the tcb.
3695065Swnj 	 */
3705065Swnj 	if (tiflags&TH_RST) switch (tp->t_state) {
3715267Sroot 
3725065Swnj 	case TCPS_SYN_RECEIVED:
3735065Swnj 		if (inp->inp_socket->so_options & SO_ACCEPTCONN) {
3745267Sroot 			/* a miniature tcp_close, but invisible to user */
3755267Sroot 			(void) m_free(dtom(tp->t_template));
3765267Sroot 			(void) m_free(dtom(tp));
3775267Sroot 			inp->inp_ppcb = 0;
3785267Sroot 			tp = tcp_newtcpcb(inp);
3795085Swnj 			tp->t_state = TCPS_LISTEN;
3806028Sroot 			inp->inp_faddr.s_addr = 0;
3816028Sroot 			inp->inp_fport = 0;
3826028Sroot 			inp->inp_laddr.s_addr = 0;	/* not quite right */
3836320Swnj 			tp = 0;
3845065Swnj 			goto drop;
3854601Swnj 		}
3865085Swnj 		tcp_drop(tp, ECONNREFUSED);
3876320Swnj 		tp = 0;
3885065Swnj 		goto drop;
3894601Swnj 
3905065Swnj 	case TCPS_ESTABLISHED:
3915065Swnj 	case TCPS_FIN_WAIT_1:
3925065Swnj 	case TCPS_FIN_WAIT_2:
3935065Swnj 	case TCPS_CLOSE_WAIT:
3945065Swnj 		tcp_drop(tp, ECONNRESET);
3956320Swnj 		tp = 0;
3965065Swnj 		goto drop;
3975065Swnj 
3985065Swnj 	case TCPS_CLOSING:
3995065Swnj 	case TCPS_LAST_ACK:
4005065Swnj 	case TCPS_TIME_WAIT:
4015065Swnj 		tcp_close(tp);
4026320Swnj 		tp = 0;
4035065Swnj 		goto drop;
4044601Swnj 	}
4054601Swnj 
4064601Swnj 	/*
4075065Swnj 	 * If a SYN is in the window, then this is an
4085065Swnj 	 * error and we send an RST and drop the connection.
4094601Swnj 	 */
4105065Swnj 	if (tiflags & TH_SYN) {
4115231Swnj 		tcp_drop(tp, ECONNRESET);
4126266Swnj 		tp = 0;
4135085Swnj 		goto dropwithreset;
4144601Swnj 	}
4154601Swnj 
4164601Swnj 	/*
4175065Swnj 	 * If the ACK bit is off we drop the segment and return.
4184601Swnj 	 */
4195085Swnj 	if ((tiflags & TH_ACK) == 0)
4205065Swnj 		goto drop;
4215065Swnj 
4225065Swnj 	/*
4235065Swnj 	 * Ack processing.
4245065Swnj 	 */
4254601Swnj 	switch (tp->t_state) {
4264601Swnj 
4275065Swnj 	/*
4285065Swnj 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
4295065Swnj 	 * ESTABLISHED state and continue processing, othewise
4305065Swnj 	 * send an RST.
4315065Swnj 	 */
4325065Swnj 	case TCPS_SYN_RECEIVED:
4335085Swnj 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
4345231Swnj 		    SEQ_GT(ti->ti_ack, tp->snd_max))
4355085Swnj 			goto dropwithreset;
4365244Sroot 		tp->snd_una++;			/* SYN acked */
4375357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
4385357Sroot 			tp->snd_nxt = tp->snd_una;
4395244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
4405391Swnj 		if (so->so_options & SO_ACCEPTCONN)
4415391Swnj 			so->so_state |= SS_CONNAWAITING;
4425085Swnj 		soisconnected(so);
4435085Swnj 		tp->t_state = TCPS_ESTABLISHED;
4445162Swnj 		(void) tcp_reass(tp, (struct tcpiphdr *)0);
4455244Sroot 		tp->snd_wl1 = ti->ti_seq - 1;
4465085Swnj 		/* fall into ... */
4474601Swnj 
4485065Swnj 	/*
4495065Swnj 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
4505065Swnj 	 * ACKs.  If the ack is in the range
4515231Swnj 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
4525065Swnj 	 * then advance tp->snd_una to ti->ti_ack and drop
4535065Swnj 	 * data from the retransmission queue.  If this ACK reflects
4545065Swnj 	 * more up to date window information we update our window information.
4555065Swnj 	 */
4565065Swnj 	case TCPS_ESTABLISHED:
4575065Swnj 	case TCPS_FIN_WAIT_1:
4585065Swnj 	case TCPS_FIN_WAIT_2:
4595065Swnj 	case TCPS_CLOSE_WAIT:
4605065Swnj 	case TCPS_CLOSING:
4615244Sroot 	case TCPS_LAST_ACK:
4625244Sroot 	case TCPS_TIME_WAIT:
4635085Swnj #define	ourfinisacked	(acked > 0)
4645085Swnj 
4655244Sroot 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una))
4665065Swnj 			break;
4675300Sroot 		if (SEQ_GT(ti->ti_ack, tp->snd_max))
4685065Swnj 			goto dropafterack;
4695085Swnj 		acked = ti->ti_ack - tp->snd_una;
4705951Swnj 
4715951Swnj 		/*
4725951Swnj 		 * If transmit timer is running and timed sequence
4735951Swnj 		 * number was acked, update smoothed round trip time.
4745951Swnj 		 */
4755951Swnj 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
4765951Swnj 			if (tp->t_srtt == 0)
4775951Swnj 				tp->t_srtt = tp->t_rtt;
4785951Swnj 			else
4795951Swnj 				tp->t_srtt =
4805951Swnj 				    tcp_alpha * tp->t_srtt +
4815951Swnj 				    (1 - tcp_alpha) * tp->t_rtt;
4825951Swnj /* printf("rtt %d srtt*100 now %d\n", tp->t_rtt, (int)(tp->t_srtt*100)); */
4835951Swnj 			tp->t_rtt = 0;
4845951Swnj 		}
4855951Swnj 
4865307Sroot 		if (ti->ti_ack == tp->snd_max)
4875244Sroot 			tp->t_timer[TCPT_REXMT] = 0;
4885307Sroot 		else {
4895244Sroot 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
4905244Sroot 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
4915951Swnj 			tp->t_rtt = 1;
4925300Sroot 			tp->t_rxtshift = 0;
4935085Swnj 		}
4945307Sroot 		if (acked > so->so_snd.sb_cc) {
4955307Sroot 			sbdrop(&so->so_snd, so->so_snd.sb_cc);
4965307Sroot 			tp->snd_wnd -= so->so_snd.sb_cc;
4975307Sroot 		} else {
4986161Ssam 			sbdrop(&so->so_snd, acked);
4995307Sroot 			tp->snd_wnd -= acked;
5005307Sroot 			acked = 0;
5015307Sroot 		}
5026434Swnj 		if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel)
5035300Sroot 			sowwakeup(so);
5045231Swnj 		tp->snd_una = ti->ti_ack;
5055357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
5065357Sroot 			tp->snd_nxt = tp->snd_una;
5075162Swnj 
5084601Swnj 		switch (tp->t_state) {
5094601Swnj 
5105065Swnj 		/*
5115065Swnj 		 * In FIN_WAIT_1 STATE in addition to the processing
5125065Swnj 		 * for the ESTABLISHED state if our FIN is now acknowledged
5135085Swnj 		 * then enter FIN_WAIT_2.
5145065Swnj 		 */
5155065Swnj 		case TCPS_FIN_WAIT_1:
5165896Swnj 			if (ourfinisacked) {
5175896Swnj 				/*
5185896Swnj 				 * If we can't receive any more
5195896Swnj 				 * data, then closing user can proceed.
5205896Swnj 				 */
5215896Swnj 				if (so->so_state & SS_CANTRCVMORE)
5225896Swnj 					soisdisconnected(so);
5235085Swnj 				tp->t_state = TCPS_FIN_WAIT_2;
5245896Swnj 			}
5254601Swnj 			break;
5264601Swnj 
5275065Swnj 	 	/*
5285065Swnj 		 * In CLOSING STATE in addition to the processing for
5295065Swnj 		 * the ESTABLISHED state if the ACK acknowledges our FIN
5305065Swnj 		 * then enter the TIME-WAIT state, otherwise ignore
5315065Swnj 		 * the segment.
5325065Swnj 		 */
5335065Swnj 		case TCPS_CLOSING:
5345244Sroot 			if (ourfinisacked) {
5355065Swnj 				tp->t_state = TCPS_TIME_WAIT;
5365244Sroot 				tcp_canceltimers(tp);
5375244Sroot 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
5385244Sroot 				soisdisconnected(so);
5395244Sroot 			}
5405244Sroot 			break;
5414601Swnj 
5425065Swnj 		/*
5435085Swnj 		 * The only thing that can arrive in  LAST_ACK state
5445085Swnj 		 * is an acknowledgment of our FIN.  If our FIN is now
5455085Swnj 		 * acknowledged, delete the TCB, enter the closed state
5465085Swnj 		 * and return.
5475065Swnj 		 */
5485065Swnj 		case TCPS_LAST_ACK:
5496320Swnj 			if (ourfinisacked) {
5505065Swnj 				tcp_close(tp);
5516320Swnj 				tp = 0;
5526320Swnj 			}
5535065Swnj 			goto drop;
5544601Swnj 
5555065Swnj 		/*
5565065Swnj 		 * In TIME_WAIT state the only thing that should arrive
5575065Swnj 		 * is a retransmission of the remote FIN.  Acknowledge
5585065Swnj 		 * it and restart the finack timer.
5595065Swnj 		 */
5605065Swnj 		case TCPS_TIME_WAIT:
5615162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
5625065Swnj 			goto dropafterack;
5634601Swnj 		}
5645085Swnj #undef ourfinisacked
5655085Swnj 	}
5664601Swnj 
5675065Swnj step6:
5685065Swnj 	/*
5695244Sroot 	 * Update window information.
5705244Sroot 	 */
5715300Sroot 	if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
5725391Swnj 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
5735300Sroot 	     tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) {
5745244Sroot 		tp->snd_wnd = ti->ti_win;
5755244Sroot 		tp->snd_wl1 = ti->ti_seq;
5765244Sroot 		tp->snd_wl2 = ti->ti_ack;
5775244Sroot 		if (tp->snd_wnd > 0)
5785244Sroot 			tp->t_timer[TCPT_PERSIST] = 0;
5795244Sroot 	}
5805244Sroot 
5815244Sroot 	/*
5825547Swnj 	 * Process segments with URG.
5835065Swnj 	 */
584*7267Swnj 	if ((tiflags & TH_URG) && ti->ti_urp &&
585*7267Swnj 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
5865547Swnj 		/*
5875547Swnj 		 * If this segment advances the known urgent pointer,
5885547Swnj 		 * then mark the data stream.  This should not happen
5895547Swnj 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
5905547Swnj 		 * a FIN has been received from the remote side.
5915547Swnj 		 * In these states we ignore the URG.
5925547Swnj 		 */
5935547Swnj 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
5945547Swnj 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
5955547Swnj 			so->so_oobmark = so->so_rcv.sb_cc +
5965547Swnj 			    (tp->rcv_up - tp->rcv_nxt) - 1;
5975547Swnj 			if (so->so_oobmark == 0)
5985547Swnj 				so->so_state |= SS_RCVATMARK;
5995440Swnj #ifdef TCPTRUEOOB
6005547Swnj 			if ((tp->t_flags & TF_DOOOB) == 0)
6015440Swnj #endif
6025547Swnj 				sohasoutofband(so);
6035547Swnj 			tp->t_oobflags &= ~TCPOOB_HAVEDATA;
6045440Swnj 		}
6055547Swnj 		/*
6065547Swnj 		 * Remove out of band data so doesn't get presented to user.
6075547Swnj 		 * This can happen independent of advancing the URG pointer,
6085547Swnj 		 * but if two URG's are pending at once, some out-of-band
6095547Swnj 		 * data may creep in... ick.
6105547Swnj 		 */
6115547Swnj 		if (ti->ti_urp <= ti->ti_len) {
6125547Swnj 			tcp_pulloutofband(so, ti);
6135547Swnj 		}
6145419Swnj 	}
6154601Swnj 
6164601Swnj 	/*
6175065Swnj 	 * Process the segment text, merging it into the TCP sequencing queue,
6185065Swnj 	 * and arranging for acknowledgment of receipt if necessary.
6195065Swnj 	 * This process logically involves adjusting tp->rcv_wnd as data
6205065Swnj 	 * is presented to the user (this happens in tcp_usrreq.c,
6215065Swnj 	 * case PRU_RCVD).  If a FIN has already been received on this
6225065Swnj 	 * connection then we just ignore the text.
6234601Swnj 	 */
6245263Swnj 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
6255263Swnj 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
6265065Swnj 		tiflags = tcp_reass(tp, ti);
6275440Swnj 		if (tcpnodelack == 0)
6285440Swnj 			tp->t_flags |= TF_DELACK;
6295440Swnj 		else
6305440Swnj 			tp->t_flags |= TF_ACKNOW;
6315244Sroot 	} else {
6324924Swnj 		m_freem(m);
6335263Swnj 		tiflags &= ~TH_FIN;
6345244Sroot 	}
6354601Swnj 
6364601Swnj 	/*
6375263Swnj 	 * If FIN is received ACK the FIN and let the user know
6385263Swnj 	 * that the connection is closing.
6394601Swnj 	 */
6405263Swnj 	if (tiflags & TH_FIN) {
6415244Sroot 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
6425244Sroot 			socantrcvmore(so);
6435244Sroot 			tp->t_flags |= TF_ACKNOW;
6445244Sroot 			tp->rcv_nxt++;
6455244Sroot 		}
6465065Swnj 		switch (tp->t_state) {
6474601Swnj 
6485065Swnj 	 	/*
6495065Swnj 		 * In SYN_RECEIVED and ESTABLISHED STATES
6505065Swnj 		 * enter the CLOSE_WAIT state.
6514884Swnj 		 */
6525065Swnj 		case TCPS_SYN_RECEIVED:
6535065Swnj 		case TCPS_ESTABLISHED:
6545065Swnj 			tp->t_state = TCPS_CLOSE_WAIT;
6555065Swnj 			break;
6564884Swnj 
6575065Swnj 	 	/*
6585085Swnj 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
6595085Swnj 		 * enter the CLOSING state.
6604884Swnj 		 */
6615065Swnj 		case TCPS_FIN_WAIT_1:
6625085Swnj 			tp->t_state = TCPS_CLOSING;
6635065Swnj 			break;
6644601Swnj 
6655065Swnj 	 	/*
6665065Swnj 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
6675065Swnj 		 * starting the time-wait timer, turning off the other
6685065Swnj 		 * standard timers.
6695065Swnj 		 */
6705065Swnj 		case TCPS_FIN_WAIT_2:
6715244Sroot 			tp->t_state = TCPS_TIME_WAIT;
6725074Swnj 			tcp_canceltimers(tp);
6735162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
6745244Sroot 			soisdisconnected(so);
6755065Swnj 			break;
6765065Swnj 
6774884Swnj 		/*
6785065Swnj 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
6794884Swnj 		 */
6805065Swnj 		case TCPS_TIME_WAIT:
6815162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
6825065Swnj 			break;
6835085Swnj 		}
6844601Swnj 	}
6855267Sroot 	if (so->so_options & SO_DEBUG)
6865267Sroot 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
6875085Swnj 
6885085Swnj 	/*
6895085Swnj 	 * Return any desired output.
6905085Swnj 	 */
6916161Ssam 	(void) tcp_output(tp);
6925065Swnj 	return;
6935085Swnj 
6945065Swnj dropafterack:
6955085Swnj 	/*
6966211Swnj 	 * Generate an ACK dropping incoming segment if it occupies
6976211Swnj 	 * sequence space, where the ACK reflects our state.
6985085Swnj 	 */
6996211Swnj 	if ((tiflags&TH_RST) ||
7006211Swnj 	    tlen == 0 && (tiflags&(TH_SYN|TH_FIN)) == 0)
7015085Swnj 		goto drop;
7026303Sroot 	if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
7036303Sroot 		tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0);
7045391Swnj 	tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK);
7055231Swnj 	return;
7065085Swnj 
7075085Swnj dropwithreset:
7085440Swnj 	if (om)
7096161Ssam 		(void) m_free(om);
7105085Swnj 	/*
7115244Sroot 	 * Generate a RST, dropping incoming segment.
7125085Swnj 	 * Make ACK acceptable to originator of segment.
7135085Swnj 	 */
7145085Swnj 	if (tiflags & TH_RST)
7155085Swnj 		goto drop;
7165085Swnj 	if (tiflags & TH_ACK)
7175391Swnj 		tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST);
7185085Swnj 	else {
7195085Swnj 		if (tiflags & TH_SYN)
7205085Swnj 			ti->ti_len++;
7216211Swnj 		tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0,
7226211Swnj 		    TH_RST|TH_ACK);
7235085Swnj 	}
7245231Swnj 	return;
7255085Swnj 
7265065Swnj drop:
7275085Swnj 	/*
7285085Swnj 	 * Drop space held by incoming segment and return.
7295085Swnj 	 */
7306303Sroot 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
7316303Sroot 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
7325065Swnj 	m_freem(m);
7335267Sroot 	return;
7345065Swnj }
7355065Swnj 
7365440Swnj tcp_dooptions(tp, om)
7375440Swnj 	struct tcpcb *tp;
7385440Swnj 	struct mbuf *om;
7395419Swnj {
7405440Swnj 	register u_char *cp;
7415440Swnj 	int opt, optlen, cnt;
7425419Swnj 
7435440Swnj 	cp = mtod(om, u_char *);
7445440Swnj 	cnt = om->m_len;
7455440Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
7465440Swnj 		opt = cp[0];
7475440Swnj 		if (opt == TCPOPT_EOL)
7485440Swnj 			break;
7495440Swnj 		if (opt == TCPOPT_NOP)
7505440Swnj 			optlen = 1;
7515440Swnj 		else
7525440Swnj 			optlen = cp[1];
7535440Swnj 		switch (opt) {
7545440Swnj 
7555440Swnj 		default:
7565440Swnj 			break;
7575440Swnj 
7585440Swnj 		case TCPOPT_MAXSEG:
7595440Swnj 			if (optlen != 4)
7605440Swnj 				continue;
7615440Swnj 			tp->t_maxseg = *(u_short *)(cp + 2);
7626320Swnj #if vax || pdp11
7636161Ssam 			tp->t_maxseg = ntohs((u_short)tp->t_maxseg);
7645440Swnj #endif
7655440Swnj 			break;
7665440Swnj 
7675440Swnj #ifdef TCPTRUEOOB
7685440Swnj 		case TCPOPT_WILLOOB:
7695440Swnj 			tp->t_flags |= TF_DOOOB;
7705440Swnj printf("tp %x dooob\n", tp);
7715440Swnj 			break;
7725440Swnj 
7735440Swnj 		case TCPOPT_OOBDATA: {
7745440Swnj 			int seq;
7755547Swnj 			register struct socket *so = tp->t_inpcb->inp_socket;
7765547Swnj 			tcp_seq mark;
7775440Swnj 
7785547Swnj 			if (optlen != 8)
7795440Swnj 				continue;
7805440Swnj 			seq = cp[2];
7815440Swnj 			if (seq < tp->t_iobseq)
7825440Swnj 				seq += 256;
7835440Swnj printf("oobdata cp[2] %d iobseq %d seq %d\n", cp[2], tp->t_iobseq, seq);
7845440Swnj 			if (seq - tp->t_iobseq > 128) {
7855440Swnj printf("bad seq\n");
7865440Swnj 				tp->t_oobflags |= TCPOOB_OWEACK;
7875440Swnj 				break;
7885440Swnj 			}
7895440Swnj 			tp->t_iobseq = cp[2];
7905440Swnj 			tp->t_iobc = cp[3];
7915547Swnj 			mark = *(tcp_seq *)(cp + 4);
7926320Swnj #if vax || pdp11
7935547Swnj 			mark = ntohl(mark);
7945547Swnj #endif
7955547Swnj 			so->so_oobmark = so->so_rcv.sb_cc + (mark-tp->rcv_nxt);
7965547Swnj 			if (so->so_oobmark == 0)
7975547Swnj 				so->so_state |= SS_RCVATMARK;
7985440Swnj printf("take oob data %x input iobseq now %x\n", tp->t_iobc, tp->t_iobseq);
7995547Swnj 			sohasoutofband(so);
8005440Swnj 			break;
8015419Swnj 		}
8025440Swnj 
8035440Swnj 		case TCPOPT_OOBACK: {
8045440Swnj 			int seq;
8055440Swnj 
8065440Swnj 			if (optlen != 4)
8075440Swnj 				continue;
8085440Swnj 			if (tp->t_oobseq != cp[2]) {
8095440Swnj printf("wrong ack\n");
8105440Swnj 				break;
8115440Swnj 			}
8125440Swnj printf("take oob ack %x and cancel rexmt\n", cp[2]);
8135440Swnj 			tp->t_oobflags &= ~TCPOOB_NEEDACK;
8145440Swnj 			tp->t_timer[TCPT_OOBREXMT] = 0;
8155419Swnj 			break;
8165440Swnj 		}
8175440Swnj #endif TCPTRUEOOB
8185440Swnj 		}
8195419Swnj 	}
8206161Ssam 	(void) m_free(om);
8215419Swnj }
8225419Swnj 
8235419Swnj /*
8245547Swnj  * Pull out of band byte out of a segment so
8255547Swnj  * it doesn't appear in the user's data queue.
8265547Swnj  * It is still reflected in the segment length for
8275547Swnj  * sequencing purposes.
8285547Swnj  */
8295547Swnj tcp_pulloutofband(so, ti)
8305547Swnj 	struct socket *so;
8315547Swnj 	struct tcpiphdr *ti;
8325547Swnj {
8335547Swnj 	register struct mbuf *m;
8346116Swnj 	int cnt = ti->ti_urp - 1;
8355547Swnj 
8365547Swnj 	m = dtom(ti);
8375547Swnj 	while (cnt >= 0) {
8385547Swnj 		if (m->m_len > cnt) {
8395547Swnj 			char *cp = mtod(m, caddr_t) + cnt;
8405547Swnj 			struct tcpcb *tp = sototcpcb(so);
8415547Swnj 
8425547Swnj 			tp->t_iobc = *cp;
8435547Swnj 			tp->t_oobflags |= TCPOOB_HAVEDATA;
8446161Ssam 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
8455547Swnj 			m->m_len--;
8465547Swnj 			return;
8475547Swnj 		}
8485547Swnj 		cnt -= m->m_len;
8495547Swnj 		m = m->m_next;
8505547Swnj 		if (m == 0)
8515547Swnj 			break;
8525547Swnj 	}
8535547Swnj 	panic("tcp_pulloutofband");
8545547Swnj }
8555547Swnj 
8565547Swnj /*
8575065Swnj  * Insert segment ti into reassembly queue of tcp with
8585065Swnj  * control block tp.  Return TH_FIN if reassembly now includes
8595065Swnj  * a segment with FIN.
8605065Swnj  */
8615109Swnj tcp_reass(tp, ti)
8625065Swnj 	register struct tcpcb *tp;
8635065Swnj 	register struct tcpiphdr *ti;
8645065Swnj {
8655065Swnj 	register struct tcpiphdr *q;
8665085Swnj 	struct socket *so = tp->t_inpcb->inp_socket;
8675263Swnj 	struct mbuf *m;
8685263Swnj 	int flags;
8695065Swnj 
8705065Swnj 	/*
8715162Swnj 	 * Call with ti==0 after become established to
8725162Swnj 	 * force pre-ESTABLISHED data up to user socket.
8735065Swnj 	 */
8745162Swnj 	if (ti == 0)
8755065Swnj 		goto present;
8764601Swnj 
8775065Swnj 	/*
8785065Swnj 	 * Find a segment which begins after this one does.
8795065Swnj 	 */
8805065Swnj 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
8815065Swnj 	    q = (struct tcpiphdr *)q->ti_next)
8825065Swnj 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
8835065Swnj 			break;
8844601Swnj 
8855065Swnj 	/*
8865065Swnj 	 * If there is a preceding segment, it may provide some of
8875065Swnj 	 * our data already.  If so, drop the data from the incoming
8885065Swnj 	 * segment.  If it provides all of our data, drop us.
8895065Swnj 	 */
8905065Swnj 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
8915065Swnj 		register int i;
8925690Swnj 		q = (struct tcpiphdr *)q->ti_prev;
8935065Swnj 		/* conversion to int (in i) handles seq wraparound */
8945065Swnj 		i = q->ti_seq + q->ti_len - ti->ti_seq;
8955065Swnj 		if (i > 0) {
8964924Swnj 			if (i >= ti->ti_len)
8975065Swnj 				goto drop;
8985065Swnj 			m_adj(dtom(tp), i);
8995065Swnj 			ti->ti_len -= i;
9004924Swnj 			ti->ti_seq += i;
9014601Swnj 		}
9025065Swnj 		q = (struct tcpiphdr *)(q->ti_next);
9035065Swnj 	}
9044601Swnj 
9055065Swnj 	/*
9065065Swnj 	 * While we overlap succeeding segments trim them or,
9075065Swnj 	 * if they are completely covered, dequeue them.
9085065Swnj 	 */
9095690Swnj 	while (q != (struct tcpiphdr *)tp) {
9105065Swnj 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
9115690Swnj 		if (i <= 0)
9125690Swnj 			break;
9135065Swnj 		if (i < q->ti_len) {
9145690Swnj 			q->ti_seq += i;
9155065Swnj 			q->ti_len -= i;
9165065Swnj 			m_adj(dtom(q), i);
9175065Swnj 			break;
9184601Swnj 		}
9195065Swnj 		q = (struct tcpiphdr *)q->ti_next;
9205623Swnj 		m = dtom(q->ti_prev);
9215065Swnj 		remque(q->ti_prev);
9225623Swnj 		m_freem(m);
9235065Swnj 	}
9244601Swnj 
9255065Swnj 	/*
9265065Swnj 	 * Stick new segment in its place.
9275065Swnj 	 */
9285065Swnj 	insque(ti, q->ti_prev);
9294601Swnj 
9305065Swnj present:
9315065Swnj 	/*
9325244Sroot 	 * Present data to user, advancing rcv_nxt through
9335244Sroot 	 * completed sequence space.
9345065Swnj 	 */
9355263Swnj 	if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
9365244Sroot 		return (0);
9374924Swnj 	ti = tp->seg_next;
9385263Swnj 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
9395263Swnj 		return (0);
9405263Swnj 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
9415263Swnj 		return (0);
9425263Swnj 	do {
9435244Sroot 		tp->rcv_nxt += ti->ti_len;
9445244Sroot 		flags = ti->ti_flags & TH_FIN;
9454924Swnj 		remque(ti);
9465263Swnj 		m = dtom(ti);
9474924Swnj 		ti = (struct tcpiphdr *)ti->ti_next;
9485263Swnj 		if (so->so_state & SS_CANTRCVMORE)
9496161Ssam 			m_freem(m);
9505263Swnj 		else
9515263Swnj 			sbappend(&so->so_rcv, m);
9525263Swnj 	} while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
9535263Swnj 	sorwakeup(so);
9545065Swnj 	return (flags);
9555065Swnj drop:
9565065Swnj 	m_freem(dtom(ti));
9575263Swnj 	return (0);
9584601Swnj }
959