xref: /csrg-svn/sys/netinet/tcp_input.c (revision 6320)
1*6320Swnj /*	tcp_input.c	1.64	82/03/26	*/
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"
105085Swnj #include "../net/in_pcb.h"
115085Swnj #include "../net/in_systm.h"
125085Swnj #include "../net/if.h"
134803Swnj #include "../net/ip.h"
144899Swnj #include "../net/ip_var.h"
154803Swnj #include "../net/tcp.h"
164803Swnj #include "../net/tcp_fsm.h"
175085Swnj #include "../net/tcp_seq.h"
185085Swnj #include "../net/tcp_timer.h"
194803Swnj #include "../net/tcp_var.h"
205085Swnj #include "../net/tcpip.h"
215267Sroot #include "../net/tcp_debug.h"
225109Swnj #include "../errno.h"
234601Swnj 
245300Sroot int	tcpprintfs = 0;
254679Swnj int	tcpcksum = 1;
265244Sroot struct	sockaddr_in tcp_in = { AF_INET };
275267Sroot struct	tcpiphdr tcp_saveti;
285440Swnj extern	tcpnodelack;
294601Swnj 
305267Sroot struct	tcpcb *tcp_newtcpcb();
315065Swnj /*
325065Swnj  * TCP input routine, follows pages 65-76 of the
335065Swnj  * protocol specification dated September, 1981 very closely.
345065Swnj  */
354924Swnj tcp_input(m0)
364924Swnj 	struct mbuf *m0;
374601Swnj {
384924Swnj 	register struct tcpiphdr *ti;
394924Swnj 	struct inpcb *inp;
404924Swnj 	register struct mbuf *m;
415440Swnj 	struct mbuf *om = 0;
424924Swnj 	int len, tlen, off;
435391Swnj 	register struct tcpcb *tp = 0;
444924Swnj 	register int tiflags;
454803Swnj 	struct socket *so;
465109Swnj 	int todrop, acked;
475267Sroot 	short ostate;
486028Sroot 	struct in_addr laddr;
494924Swnj 
504601Swnj COUNT(TCP_INPUT);
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;
76*6320Swnj #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 
125*6320Swnj #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;
215*6320Swnj 			tp = 0;
2165244Sroot 			goto drop;
2175244Sroot 		}
2186028Sroot 		in_setsockaddr(inp);
2195085Swnj 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
2205065Swnj 		tp->irs = ti->ti_seq;
2215085Swnj 		tcp_sendseqinit(tp);
2225085Swnj 		tcp_rcvseqinit(tp);
2235065Swnj 		tp->t_state = TCPS_SYN_RECEIVED;
2245244Sroot 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
2255085Swnj 		goto trimthenstep6;
2264601Swnj 
2275065Swnj 	/*
2285065Swnj 	 * If the state is SYN_SENT:
2295065Swnj 	 *	if seg contains an ACK, but not for our SYN, drop the input.
2305065Swnj 	 *	if seg contains a RST, then drop the connection.
2315065Swnj 	 *	if seg does not contain SYN, then drop it.
2325065Swnj 	 * Otherwise this is an acceptable SYN segment
2335065Swnj 	 *	initialize tp->rcv_nxt and tp->irs
2345065Swnj 	 *	if seg contains ack then advance tp->snd_una
2355065Swnj 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
2365065Swnj 	 *	arrange for segment to be acked (eventually)
2375065Swnj 	 *	continue processing rest of data/controls, beginning with URG
2385065Swnj 	 */
2395065Swnj 	case TCPS_SYN_SENT:
2405065Swnj 		if ((tiflags & TH_ACK) &&
2415300Sroot /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */
2425300Sroot 		    (SEQ_LT(ti->ti_ack, tp->iss) ||
2435231Swnj 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
2445085Swnj 			goto dropwithreset;
2455065Swnj 		if (tiflags & TH_RST) {
246*6320Swnj 			if (tiflags & TH_ACK) {
2475267Sroot 				tcp_drop(tp, ECONNREFUSED);
248*6320Swnj 				tp = 0;
249*6320Swnj 			}
2505065Swnj 			goto drop;
2514601Swnj 		}
2525065Swnj 		if ((tiflags & TH_SYN) == 0)
2535065Swnj 			goto drop;
2545231Swnj 		tp->snd_una = ti->ti_ack;
2555357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2565357Sroot 			tp->snd_nxt = tp->snd_una;
2575244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
2585065Swnj 		tp->irs = ti->ti_seq;
2595085Swnj 		tcp_rcvseqinit(tp);
2605085Swnj 		tp->t_flags |= TF_ACKNOW;
2615162Swnj 		if (SEQ_GT(tp->snd_una, tp->iss)) {
2625391Swnj 			if (so->so_options & SO_ACCEPTCONN)
2635391Swnj 				so->so_state |= SS_CONNAWAITING;
2645244Sroot 			soisconnected(so);
2655065Swnj 			tp->t_state = TCPS_ESTABLISHED;
2665162Swnj 			(void) tcp_reass(tp, (struct tcpiphdr *)0);
2675162Swnj 		} else
2685085Swnj 			tp->t_state = TCPS_SYN_RECEIVED;
2695085Swnj 		goto trimthenstep6;
2705085Swnj 
2715085Swnj trimthenstep6:
2725085Swnj 		/*
2735231Swnj 		 * Advance ti->ti_seq to correspond to first data byte.
2745085Swnj 		 * If data, trim to stay within window,
2755085Swnj 		 * dropping FIN if necessary.
2765085Swnj 		 */
2775231Swnj 		ti->ti_seq++;
2785085Swnj 		if (ti->ti_len > tp->rcv_wnd) {
2795085Swnj 			todrop = ti->ti_len - tp->rcv_wnd;
2805085Swnj 			m_adj(m, -todrop);
2815085Swnj 			ti->ti_len = tp->rcv_wnd;
2825085Swnj 			ti->ti_flags &= ~TH_FIN;
2835065Swnj 		}
2845263Swnj 		tp->snd_wl1 = ti->ti_seq - 1;
2855085Swnj 		goto step6;
2865065Swnj 	}
2874601Swnj 
2885065Swnj 	/*
2895065Swnj 	 * States other than LISTEN or SYN_SENT.
2905065Swnj 	 * First check that at least some bytes of segment are within
2915065Swnj 	 * receive window.
2925065Swnj 	 */
2935065Swnj 	if (tp->rcv_wnd == 0) {
2945065Swnj 		/*
2955065Swnj 		 * If window is closed can only take segments at
2965231Swnj 		 * window edge, and have to drop data and PUSH from
2975065Swnj 		 * incoming segments.
2985065Swnj 		 */
2995300Sroot 		if (tp->rcv_nxt != ti->ti_seq)
3005065Swnj 			goto dropafterack;
3015085Swnj 		if (ti->ti_len > 0) {
3025690Swnj 			m_adj(m, ti->ti_len);
3035085Swnj 			ti->ti_len = 0;
3045085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
3055065Swnj 		}
3065065Swnj 	} else {
3075065Swnj 		/*
3085231Swnj 		 * If segment begins before rcv_nxt, drop leading
3095065Swnj 		 * data (and SYN); if nothing left, just ack.
3105065Swnj 		 */
3115690Swnj 		todrop = tp->rcv_nxt - ti->ti_seq;
3125690Swnj 		if (todrop > 0) {
3135085Swnj 			if (tiflags & TH_SYN) {
3145300Sroot 				tiflags &= ~TH_SYN;
3155690Swnj 				ti->ti_flags &= ~TH_SYN;
3165085Swnj 				ti->ti_seq++;
3175085Swnj 				if (ti->ti_urp > 1)
3185085Swnj 					ti->ti_urp--;
3195085Swnj 				else
3205085Swnj 					tiflags &= ~TH_URG;
3215085Swnj 				todrop--;
3225085Swnj 			}
3236211Swnj 			if (todrop > ti->ti_len ||
3246211Swnj 			    todrop == ti->ti_len && (tiflags&TH_FIN) == 0)
3255065Swnj 				goto dropafterack;
3265065Swnj 			m_adj(m, todrop);
3275065Swnj 			ti->ti_seq += todrop;
3285065Swnj 			ti->ti_len -= todrop;
3295085Swnj 			if (ti->ti_urp > todrop)
3305085Swnj 				ti->ti_urp -= todrop;
3315085Swnj 			else {
3325085Swnj 				tiflags &= ~TH_URG;
3335690Swnj 				ti->ti_flags &= ~TH_URG;
3345690Swnj 				ti->ti_urp = 0;
3355085Swnj 			}
3365065Swnj 		}
3375065Swnj 		/*
3385065Swnj 		 * If segment ends after window, drop trailing data
3395085Swnj 		 * (and PUSH and FIN); if nothing left, just ACK.
3405065Swnj 		 */
3415690Swnj 		todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
3425690Swnj 		if (todrop > 0) {
3436211Swnj 			if (todrop >= ti->ti_len)
3445065Swnj 				goto dropafterack;
3455065Swnj 			m_adj(m, -todrop);
3465065Swnj 			ti->ti_len -= todrop;
3475085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
3485065Swnj 		}
3495065Swnj 	}
3504601Swnj 
3515065Swnj 	/*
3525951Swnj 	 * If a segment is received on a connection after the
3535951Swnj 	 * user processes are gone, then RST the other end.
3545951Swnj 	 */
3555951Swnj 	if (so->so_state & SS_USERGONE) {
3565951Swnj 		tcp_close(tp);
3576266Swnj 		tp = 0;
3585951Swnj 		goto dropwithreset;
3595951Swnj 	}
3605951Swnj 
3615951Swnj 	/*
3625065Swnj 	 * If the RST bit is set examine the state:
3635065Swnj 	 *    SYN_RECEIVED STATE:
3645065Swnj 	 *	If passive open, return to LISTEN state.
3655065Swnj 	 *	If active open, inform user that connection was refused.
3665065Swnj 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
3675065Swnj 	 *	Inform user that connection was reset, and close tcb.
3685065Swnj 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
3695065Swnj 	 *	Close the tcb.
3705065Swnj 	 */
3715065Swnj 	if (tiflags&TH_RST) switch (tp->t_state) {
3725267Sroot 
3735065Swnj 	case TCPS_SYN_RECEIVED:
3745065Swnj 		if (inp->inp_socket->so_options & SO_ACCEPTCONN) {
3755267Sroot 			/* a miniature tcp_close, but invisible to user */
3765267Sroot 			(void) m_free(dtom(tp->t_template));
3775267Sroot 			(void) m_free(dtom(tp));
3785267Sroot 			inp->inp_ppcb = 0;
3795267Sroot 			tp = tcp_newtcpcb(inp);
3805085Swnj 			tp->t_state = TCPS_LISTEN;
3816028Sroot 			inp->inp_faddr.s_addr = 0;
3826028Sroot 			inp->inp_fport = 0;
3836028Sroot 			inp->inp_laddr.s_addr = 0;	/* not quite right */
384*6320Swnj 			tp = 0;
3855065Swnj 			goto drop;
3864601Swnj 		}
3875085Swnj 		tcp_drop(tp, ECONNREFUSED);
388*6320Swnj 		tp = 0;
3895065Swnj 		goto drop;
3904601Swnj 
3915065Swnj 	case TCPS_ESTABLISHED:
3925065Swnj 	case TCPS_FIN_WAIT_1:
3935065Swnj 	case TCPS_FIN_WAIT_2:
3945065Swnj 	case TCPS_CLOSE_WAIT:
3955065Swnj 		tcp_drop(tp, ECONNRESET);
396*6320Swnj 		tp = 0;
3975065Swnj 		goto drop;
3985065Swnj 
3995065Swnj 	case TCPS_CLOSING:
4005065Swnj 	case TCPS_LAST_ACK:
4015065Swnj 	case TCPS_TIME_WAIT:
4025065Swnj 		tcp_close(tp);
403*6320Swnj 		tp = 0;
4045065Swnj 		goto drop;
4054601Swnj 	}
4064601Swnj 
4074601Swnj 	/*
4085065Swnj 	 * If a SYN is in the window, then this is an
4095065Swnj 	 * error and we send an RST and drop the connection.
4104601Swnj 	 */
4115065Swnj 	if (tiflags & TH_SYN) {
4125231Swnj 		tcp_drop(tp, ECONNRESET);
4136266Swnj 		tp = 0;
4145085Swnj 		goto dropwithreset;
4154601Swnj 	}
4164601Swnj 
4174601Swnj 	/*
4185065Swnj 	 * If the ACK bit is off we drop the segment and return.
4194601Swnj 	 */
4205085Swnj 	if ((tiflags & TH_ACK) == 0)
4215065Swnj 		goto drop;
4225065Swnj 
4235065Swnj 	/*
4245065Swnj 	 * Ack processing.
4255065Swnj 	 */
4264601Swnj 	switch (tp->t_state) {
4274601Swnj 
4285065Swnj 	/*
4295065Swnj 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
4305065Swnj 	 * ESTABLISHED state and continue processing, othewise
4315065Swnj 	 * send an RST.
4325065Swnj 	 */
4335065Swnj 	case TCPS_SYN_RECEIVED:
4345085Swnj 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
4355231Swnj 		    SEQ_GT(ti->ti_ack, tp->snd_max))
4365085Swnj 			goto dropwithreset;
4375244Sroot 		tp->snd_una++;			/* SYN acked */
4385357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
4395357Sroot 			tp->snd_nxt = tp->snd_una;
4405244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
4415391Swnj 		if (so->so_options & SO_ACCEPTCONN)
4425391Swnj 			so->so_state |= SS_CONNAWAITING;
4435085Swnj 		soisconnected(so);
4445085Swnj 		tp->t_state = TCPS_ESTABLISHED;
4455162Swnj 		(void) tcp_reass(tp, (struct tcpiphdr *)0);
4465244Sroot 		tp->snd_wl1 = ti->ti_seq - 1;
4475085Swnj 		/* fall into ... */
4484601Swnj 
4495065Swnj 	/*
4505065Swnj 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
4515065Swnj 	 * ACKs.  If the ack is in the range
4525231Swnj 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
4535065Swnj 	 * then advance tp->snd_una to ti->ti_ack and drop
4545065Swnj 	 * data from the retransmission queue.  If this ACK reflects
4555065Swnj 	 * more up to date window information we update our window information.
4565065Swnj 	 */
4575065Swnj 	case TCPS_ESTABLISHED:
4585065Swnj 	case TCPS_FIN_WAIT_1:
4595065Swnj 	case TCPS_FIN_WAIT_2:
4605065Swnj 	case TCPS_CLOSE_WAIT:
4615065Swnj 	case TCPS_CLOSING:
4625244Sroot 	case TCPS_LAST_ACK:
4635244Sroot 	case TCPS_TIME_WAIT:
4645085Swnj #define	ourfinisacked	(acked > 0)
4655085Swnj 
4665244Sroot 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una))
4675065Swnj 			break;
4685300Sroot 		if (SEQ_GT(ti->ti_ack, tp->snd_max))
4695065Swnj 			goto dropafterack;
4705085Swnj 		acked = ti->ti_ack - tp->snd_una;
4715951Swnj 
4725951Swnj 		/*
4735951Swnj 		 * If transmit timer is running and timed sequence
4745951Swnj 		 * number was acked, update smoothed round trip time.
4755951Swnj 		 */
4765951Swnj 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
4775951Swnj 			if (tp->t_srtt == 0)
4785951Swnj 				tp->t_srtt = tp->t_rtt;
4795951Swnj 			else
4805951Swnj 				tp->t_srtt =
4815951Swnj 				    tcp_alpha * tp->t_srtt +
4825951Swnj 				    (1 - tcp_alpha) * tp->t_rtt;
4835951Swnj /* printf("rtt %d srtt*100 now %d\n", tp->t_rtt, (int)(tp->t_srtt*100)); */
4845951Swnj 			tp->t_rtt = 0;
4855951Swnj 		}
4865951Swnj 
4875307Sroot 		if (ti->ti_ack == tp->snd_max)
4885244Sroot 			tp->t_timer[TCPT_REXMT] = 0;
4895307Sroot 		else {
4905244Sroot 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
4915244Sroot 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
4925951Swnj 			tp->t_rtt = 1;
4935300Sroot 			tp->t_rxtshift = 0;
4945085Swnj 		}
4955307Sroot 		if (acked > so->so_snd.sb_cc) {
4965307Sroot 			sbdrop(&so->so_snd, so->so_snd.sb_cc);
4975307Sroot 			tp->snd_wnd -= so->so_snd.sb_cc;
4985307Sroot 		} else {
4996161Ssam 			sbdrop(&so->so_snd, acked);
5005307Sroot 			tp->snd_wnd -= acked;
5015307Sroot 			acked = 0;
5025307Sroot 		}
5035300Sroot 		if (so->so_snd.sb_flags & SB_WAIT)
5045300Sroot 			sowwakeup(so);
5055231Swnj 		tp->snd_una = ti->ti_ack;
5065357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
5075357Sroot 			tp->snd_nxt = tp->snd_una;
5085162Swnj 
5094601Swnj 		switch (tp->t_state) {
5104601Swnj 
5115065Swnj 		/*
5125065Swnj 		 * In FIN_WAIT_1 STATE in addition to the processing
5135065Swnj 		 * for the ESTABLISHED state if our FIN is now acknowledged
5145085Swnj 		 * then enter FIN_WAIT_2.
5155065Swnj 		 */
5165065Swnj 		case TCPS_FIN_WAIT_1:
5175896Swnj 			if (ourfinisacked) {
5185896Swnj 				/*
5195896Swnj 				 * If we can't receive any more
5205896Swnj 				 * data, then closing user can proceed.
5215896Swnj 				 */
5225896Swnj 				if (so->so_state & SS_CANTRCVMORE)
5235896Swnj 					soisdisconnected(so);
5245085Swnj 				tp->t_state = TCPS_FIN_WAIT_2;
5255896Swnj 			}
5264601Swnj 			break;
5274601Swnj 
5285065Swnj 	 	/*
5295065Swnj 		 * In CLOSING STATE in addition to the processing for
5305065Swnj 		 * the ESTABLISHED state if the ACK acknowledges our FIN
5315065Swnj 		 * then enter the TIME-WAIT state, otherwise ignore
5325065Swnj 		 * the segment.
5335065Swnj 		 */
5345065Swnj 		case TCPS_CLOSING:
5355244Sroot 			if (ourfinisacked) {
5365065Swnj 				tp->t_state = TCPS_TIME_WAIT;
5375244Sroot 				tcp_canceltimers(tp);
5385244Sroot 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
5395244Sroot 				soisdisconnected(so);
5405244Sroot 			}
5415244Sroot 			break;
5424601Swnj 
5435065Swnj 		/*
5445085Swnj 		 * The only thing that can arrive in  LAST_ACK state
5455085Swnj 		 * is an acknowledgment of our FIN.  If our FIN is now
5465085Swnj 		 * acknowledged, delete the TCB, enter the closed state
5475085Swnj 		 * and return.
5485065Swnj 		 */
5495065Swnj 		case TCPS_LAST_ACK:
550*6320Swnj 			if (ourfinisacked) {
5515065Swnj 				tcp_close(tp);
552*6320Swnj 				tp = 0;
553*6320Swnj 			}
5545065Swnj 			goto drop;
5554601Swnj 
5565065Swnj 		/*
5575065Swnj 		 * In TIME_WAIT state the only thing that should arrive
5585065Swnj 		 * is a retransmission of the remote FIN.  Acknowledge
5595065Swnj 		 * it and restart the finack timer.
5605065Swnj 		 */
5615065Swnj 		case TCPS_TIME_WAIT:
5625162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
5635065Swnj 			goto dropafterack;
5644601Swnj 		}
5655085Swnj #undef ourfinisacked
5665085Swnj 	}
5674601Swnj 
5685065Swnj step6:
5695065Swnj 	/*
5705244Sroot 	 * Update window information.
5715244Sroot 	 */
5725300Sroot 	if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
5735391Swnj 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
5745300Sroot 	     tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) {
5755244Sroot 		tp->snd_wnd = ti->ti_win;
5765244Sroot 		tp->snd_wl1 = ti->ti_seq;
5775244Sroot 		tp->snd_wl2 = ti->ti_ack;
5785244Sroot 		if (tp->snd_wnd > 0)
5795244Sroot 			tp->t_timer[TCPT_PERSIST] = 0;
5805244Sroot 	}
5815244Sroot 
5825244Sroot 	/*
5835547Swnj 	 * Process segments with URG.
5845065Swnj 	 */
5855547Swnj 	if ((tiflags & TH_URG) && 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);
762*6320Swnj #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);
792*6320Swnj #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;
8695085Swnj COUNT(TCP_REASS);
8705065Swnj 
8715065Swnj 	/*
8725162Swnj 	 * Call with ti==0 after become established to
8735162Swnj 	 * force pre-ESTABLISHED data up to user socket.
8745065Swnj 	 */
8755162Swnj 	if (ti == 0)
8765065Swnj 		goto present;
8774601Swnj 
8785065Swnj 	/*
8795065Swnj 	 * Find a segment which begins after this one does.
8805065Swnj 	 */
8815065Swnj 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
8825065Swnj 	    q = (struct tcpiphdr *)q->ti_next)
8835065Swnj 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
8845065Swnj 			break;
8854601Swnj 
8865065Swnj 	/*
8875065Swnj 	 * If there is a preceding segment, it may provide some of
8885065Swnj 	 * our data already.  If so, drop the data from the incoming
8895065Swnj 	 * segment.  If it provides all of our data, drop us.
8905065Swnj 	 */
8915065Swnj 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
8925065Swnj 		register int i;
8935690Swnj 		q = (struct tcpiphdr *)q->ti_prev;
8945065Swnj 		/* conversion to int (in i) handles seq wraparound */
8955065Swnj 		i = q->ti_seq + q->ti_len - ti->ti_seq;
8965065Swnj 		if (i > 0) {
8974924Swnj 			if (i >= ti->ti_len)
8985065Swnj 				goto drop;
8995065Swnj 			m_adj(dtom(tp), i);
9005065Swnj 			ti->ti_len -= i;
9014924Swnj 			ti->ti_seq += i;
9024601Swnj 		}
9035065Swnj 		q = (struct tcpiphdr *)(q->ti_next);
9045065Swnj 	}
9054601Swnj 
9065065Swnj 	/*
9075065Swnj 	 * While we overlap succeeding segments trim them or,
9085065Swnj 	 * if they are completely covered, dequeue them.
9095065Swnj 	 */
9105690Swnj 	while (q != (struct tcpiphdr *)tp) {
9115065Swnj 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
9125690Swnj 		if (i <= 0)
9135690Swnj 			break;
9145065Swnj 		if (i < q->ti_len) {
9155690Swnj 			q->ti_seq += i;
9165065Swnj 			q->ti_len -= i;
9175065Swnj 			m_adj(dtom(q), i);
9185065Swnj 			break;
9194601Swnj 		}
9205065Swnj 		q = (struct tcpiphdr *)q->ti_next;
9215623Swnj 		m = dtom(q->ti_prev);
9225065Swnj 		remque(q->ti_prev);
9235623Swnj 		m_freem(m);
9245065Swnj 	}
9254601Swnj 
9265065Swnj 	/*
9275065Swnj 	 * Stick new segment in its place.
9285065Swnj 	 */
9295065Swnj 	insque(ti, q->ti_prev);
9304601Swnj 
9315065Swnj present:
9325065Swnj 	/*
9335244Sroot 	 * Present data to user, advancing rcv_nxt through
9345244Sroot 	 * completed sequence space.
9355065Swnj 	 */
9365263Swnj 	if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
9375244Sroot 		return (0);
9384924Swnj 	ti = tp->seg_next;
9395263Swnj 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
9405263Swnj 		return (0);
9415263Swnj 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
9425263Swnj 		return (0);
9435263Swnj 	do {
9445244Sroot 		tp->rcv_nxt += ti->ti_len;
9455244Sroot 		flags = ti->ti_flags & TH_FIN;
9464924Swnj 		remque(ti);
9475263Swnj 		m = dtom(ti);
9484924Swnj 		ti = (struct tcpiphdr *)ti->ti_next;
9495263Swnj 		if (so->so_state & SS_CANTRCVMORE)
9506161Ssam 			m_freem(m);
9515263Swnj 		else
9525263Swnj 			sbappend(&so->so_rcv, m);
9535263Swnj 	} while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
9545263Swnj 	sorwakeup(so);
9555065Swnj 	return (flags);
9565065Swnj drop:
9575065Swnj 	m_freem(dtom(ti));
9585263Swnj 	return (0);
9594601Swnj }
960