xref: /csrg-svn/sys/netinet/tcp_input.c (revision 8271)
1*8271Sroot /*	tcp_input.c	1.74	82/09/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"
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"
237300Ssam #include <errno.h>
244601Swnj 
255300Sroot int	tcpprintfs = 0;
264679Swnj int	tcpcksum = 1;
27*8271Sroot struct	mbuf tcp_mb;
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 	/*
136*8271Sroot 	 * Locate pcb for segment.
1374924Swnj 	 */
1385065Swnj 	inp = in_pcblookup
1396028Sroot 		(&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport,
1406028Sroot 		INPLOOKUP_WILDCARD);
1415065Swnj 
1425065Swnj 	/*
1435065Swnj 	 * If the state is CLOSED (i.e., TCB does not exist) then
1445244Sroot 	 * all data in the incoming segment is discarded.
1455065Swnj 	 */
1465300Sroot 	if (inp == 0)
1475085Swnj 		goto dropwithreset;
1485065Swnj 	tp = intotcpcb(inp);
1495300Sroot 	if (tp == 0)
1505085Swnj 		goto dropwithreset;
1515109Swnj 	so = inp->inp_socket;
1525267Sroot 	if (so->so_options & SO_DEBUG) {
1535267Sroot 		ostate = tp->t_state;
1545267Sroot 		tcp_saveti = *ti;
1555267Sroot 	}
1567510Sroot 	if (so->so_options & SO_ACCEPTCONN) {
1577510Sroot 		so = sonewconn(so);
1587510Sroot 		if (so == 0)
1597510Sroot 			goto drop;
1607510Sroot 		inp = (struct inpcb *)so->so_pcb;
1617510Sroot 		inp->inp_laddr = ti->ti_dst;
1627510Sroot 		inp->inp_lport = ti->ti_dport;
1637510Sroot 		tp = intotcpcb(inp);
1647510Sroot 		tp->t_state = TCPS_LISTEN;
1657510Sroot 	}
1664601Swnj 
1674601Swnj 	/*
1685162Swnj 	 * Segment received on connection.
1695162Swnj 	 * Reset idle time and keep-alive timer.
1705162Swnj 	 */
1715162Swnj 	tp->t_idle = 0;
1725162Swnj 	tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
1735162Swnj 
1745162Swnj 	/*
1755440Swnj 	 * Process options.
1765440Swnj 	 */
1775440Swnj 	if (om) {
1785440Swnj 		tcp_dooptions(tp, om);
1795440Swnj 		om = 0;
1805440Swnj 	}
1815440Swnj 
1825440Swnj 	/*
1835085Swnj 	 * Calculate amount of space in receive window,
1845085Swnj 	 * and then do TCP input processing.
1854601Swnj 	 */
1865085Swnj 	tp->rcv_wnd = sbspace(&so->so_rcv);
1875231Swnj 	if (tp->rcv_wnd < 0)
1885231Swnj 		tp->rcv_wnd = 0;
1894601Swnj 
1904601Swnj 	switch (tp->t_state) {
1914601Swnj 
1925065Swnj 	/*
1935065Swnj 	 * If the state is LISTEN then ignore segment if it contains an RST.
1945065Swnj 	 * If the segment contains an ACK then it is bad and send a RST.
1955065Swnj 	 * If it does not contain a SYN then it is not interesting; drop it.
1965085Swnj 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
1975065Swnj 	 * tp->iss, and send a segment:
1985085Swnj 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
1995065Swnj 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
2005065Swnj 	 * Fill in remote peer address fields if not previously specified.
2015065Swnj 	 * Enter SYN_RECEIVED state, and process any other fields of this
2025244Sroot 	 * segment in this state.
2035065Swnj 	 */
204*8271Sroot 	case TCPS_LISTEN: {
205*8271Sroot 		struct mbuf *m = m_get(M_DONTWAIT);
206*8271Sroot 		register struct sockaddr_in *sin;
207*8271Sroot 
208*8271Sroot 		if (m == 0)
209*8271Sroot 			goto drop;
210*8271Sroot 		m->m_off = MMINOFF;
211*8271Sroot 		m->m_len = sizeof (struct sockaddr_in);
2125065Swnj 		if (tiflags & TH_RST)
2135065Swnj 			goto drop;
2145300Sroot 		if (tiflags & TH_ACK)
2155085Swnj 			goto dropwithreset;
2165300Sroot 		if ((tiflags & TH_SYN) == 0)
2175065Swnj 			goto drop;
218*8271Sroot 		sin = mtod(m, struct sockaddr_in *);
219*8271Sroot 		sin->sin_family = AF_INET;
220*8271Sroot 		sin->sin_addr = ti->ti_src;
221*8271Sroot 		sin->sin_port = ti->ti_sport;
2226028Sroot 		laddr = inp->inp_laddr;
2236028Sroot 		if (inp->inp_laddr.s_addr == 0)
2246028Sroot 			inp->inp_laddr = ti->ti_dst;
225*8271Sroot 		if (in_pcbconnect(inp, m)) {
2266028Sroot 			inp->inp_laddr = laddr;
227*8271Sroot 			m_free(m);
2285244Sroot 			goto drop;
2296028Sroot 		}
230*8271Sroot 		m_free(m);
2315244Sroot 		tp->t_template = tcp_template(tp);
2325244Sroot 		if (tp->t_template == 0) {
2335244Sroot 			in_pcbdisconnect(inp);
2346028Sroot 			inp->inp_laddr = laddr;
2356320Swnj 			tp = 0;
2365244Sroot 			goto drop;
2375244Sroot 		}
2385085Swnj 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
2395065Swnj 		tp->irs = ti->ti_seq;
2405085Swnj 		tcp_sendseqinit(tp);
2415085Swnj 		tcp_rcvseqinit(tp);
2425065Swnj 		tp->t_state = TCPS_SYN_RECEIVED;
2435244Sroot 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
2445085Swnj 		goto trimthenstep6;
245*8271Sroot 		}
2464601Swnj 
2475065Swnj 	/*
2485065Swnj 	 * If the state is SYN_SENT:
2495065Swnj 	 *	if seg contains an ACK, but not for our SYN, drop the input.
2505065Swnj 	 *	if seg contains a RST, then drop the connection.
2515065Swnj 	 *	if seg does not contain SYN, then drop it.
2525065Swnj 	 * Otherwise this is an acceptable SYN segment
2535065Swnj 	 *	initialize tp->rcv_nxt and tp->irs
2545065Swnj 	 *	if seg contains ack then advance tp->snd_una
2555065Swnj 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
2565065Swnj 	 *	arrange for segment to be acked (eventually)
2575065Swnj 	 *	continue processing rest of data/controls, beginning with URG
2585065Swnj 	 */
2595065Swnj 	case TCPS_SYN_SENT:
2605065Swnj 		if ((tiflags & TH_ACK) &&
2615300Sroot /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */
2625300Sroot 		    (SEQ_LT(ti->ti_ack, tp->iss) ||
2635231Swnj 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
2645085Swnj 			goto dropwithreset;
2655065Swnj 		if (tiflags & TH_RST) {
2666320Swnj 			if (tiflags & TH_ACK) {
2675267Sroot 				tcp_drop(tp, ECONNREFUSED);
2686320Swnj 				tp = 0;
2696320Swnj 			}
2705065Swnj 			goto drop;
2714601Swnj 		}
2725065Swnj 		if ((tiflags & TH_SYN) == 0)
2735065Swnj 			goto drop;
2745231Swnj 		tp->snd_una = ti->ti_ack;
2755357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2765357Sroot 			tp->snd_nxt = tp->snd_una;
2775244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
2785065Swnj 		tp->irs = ti->ti_seq;
2795085Swnj 		tcp_rcvseqinit(tp);
2805085Swnj 		tp->t_flags |= TF_ACKNOW;
2815162Swnj 		if (SEQ_GT(tp->snd_una, tp->iss)) {
2825244Sroot 			soisconnected(so);
2835065Swnj 			tp->t_state = TCPS_ESTABLISHED;
2845162Swnj 			(void) tcp_reass(tp, (struct tcpiphdr *)0);
2855162Swnj 		} else
2865085Swnj 			tp->t_state = TCPS_SYN_RECEIVED;
2875085Swnj 		goto trimthenstep6;
2885085Swnj 
2895085Swnj trimthenstep6:
2905085Swnj 		/*
2915231Swnj 		 * Advance ti->ti_seq to correspond to first data byte.
2925085Swnj 		 * If data, trim to stay within window,
2935085Swnj 		 * dropping FIN if necessary.
2945085Swnj 		 */
2955231Swnj 		ti->ti_seq++;
2965085Swnj 		if (ti->ti_len > tp->rcv_wnd) {
2975085Swnj 			todrop = ti->ti_len - tp->rcv_wnd;
2985085Swnj 			m_adj(m, -todrop);
2995085Swnj 			ti->ti_len = tp->rcv_wnd;
3005085Swnj 			ti->ti_flags &= ~TH_FIN;
3015065Swnj 		}
3025263Swnj 		tp->snd_wl1 = ti->ti_seq - 1;
3035085Swnj 		goto step6;
3045065Swnj 	}
3054601Swnj 
3065065Swnj 	/*
3075065Swnj 	 * States other than LISTEN or SYN_SENT.
3085065Swnj 	 * First check that at least some bytes of segment are within
3095065Swnj 	 * receive window.
3105065Swnj 	 */
3115065Swnj 	if (tp->rcv_wnd == 0) {
3125065Swnj 		/*
3135065Swnj 		 * If window is closed can only take segments at
3145231Swnj 		 * window edge, and have to drop data and PUSH from
3155065Swnj 		 * incoming segments.
3165065Swnj 		 */
3175300Sroot 		if (tp->rcv_nxt != ti->ti_seq)
3185065Swnj 			goto dropafterack;
3195085Swnj 		if (ti->ti_len > 0) {
3205690Swnj 			m_adj(m, ti->ti_len);
3215085Swnj 			ti->ti_len = 0;
3225085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
3235065Swnj 		}
3245065Swnj 	} else {
3255065Swnj 		/*
3265231Swnj 		 * If segment begins before rcv_nxt, drop leading
3275065Swnj 		 * data (and SYN); if nothing left, just ack.
3285065Swnj 		 */
3295690Swnj 		todrop = tp->rcv_nxt - ti->ti_seq;
3305690Swnj 		if (todrop > 0) {
3315085Swnj 			if (tiflags & TH_SYN) {
3325300Sroot 				tiflags &= ~TH_SYN;
3335690Swnj 				ti->ti_flags &= ~TH_SYN;
3345085Swnj 				ti->ti_seq++;
3355085Swnj 				if (ti->ti_urp > 1)
3365085Swnj 					ti->ti_urp--;
3375085Swnj 				else
3385085Swnj 					tiflags &= ~TH_URG;
3395085Swnj 				todrop--;
3405085Swnj 			}
3416211Swnj 			if (todrop > ti->ti_len ||
3426211Swnj 			    todrop == ti->ti_len && (tiflags&TH_FIN) == 0)
3435065Swnj 				goto dropafterack;
3445065Swnj 			m_adj(m, todrop);
3455065Swnj 			ti->ti_seq += todrop;
3465065Swnj 			ti->ti_len -= todrop;
3475085Swnj 			if (ti->ti_urp > todrop)
3485085Swnj 				ti->ti_urp -= todrop;
3495085Swnj 			else {
3505085Swnj 				tiflags &= ~TH_URG;
3515690Swnj 				ti->ti_flags &= ~TH_URG;
3525690Swnj 				ti->ti_urp = 0;
3535085Swnj 			}
3545065Swnj 		}
3555065Swnj 		/*
3565065Swnj 		 * If segment ends after window, drop trailing data
3575085Swnj 		 * (and PUSH and FIN); if nothing left, just ACK.
3585065Swnj 		 */
3595690Swnj 		todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
3605690Swnj 		if (todrop > 0) {
3616211Swnj 			if (todrop >= ti->ti_len)
3625065Swnj 				goto dropafterack;
3635065Swnj 			m_adj(m, -todrop);
3645065Swnj 			ti->ti_len -= todrop;
3655085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
3665065Swnj 		}
3675065Swnj 	}
3684601Swnj 
3695065Swnj 	/*
3705951Swnj 	 * If a segment is received on a connection after the
3715951Swnj 	 * user processes are gone, then RST the other end.
3725951Swnj 	 */
3737510Sroot 	if (so->so_state & SS_NOFDREF) {
3745951Swnj 		tcp_close(tp);
3756266Swnj 		tp = 0;
3765951Swnj 		goto dropwithreset;
3775951Swnj 	}
3785951Swnj 
3795951Swnj 	/*
3805065Swnj 	 * If the RST bit is set examine the state:
3815065Swnj 	 *    SYN_RECEIVED STATE:
3825065Swnj 	 *	If passive open, return to LISTEN state.
3835065Swnj 	 *	If active open, inform user that connection was refused.
3845065Swnj 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
3855065Swnj 	 *	Inform user that connection was reset, and close tcb.
3865065Swnj 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
3875065Swnj 	 *	Close the tcb.
3885065Swnj 	 */
3895065Swnj 	if (tiflags&TH_RST) switch (tp->t_state) {
3905267Sroot 
3915065Swnj 	case TCPS_SYN_RECEIVED:
3925085Swnj 		tcp_drop(tp, ECONNREFUSED);
3936320Swnj 		tp = 0;
3945065Swnj 		goto drop;
3954601Swnj 
3965065Swnj 	case TCPS_ESTABLISHED:
3975065Swnj 	case TCPS_FIN_WAIT_1:
3985065Swnj 	case TCPS_FIN_WAIT_2:
3995065Swnj 	case TCPS_CLOSE_WAIT:
4005065Swnj 		tcp_drop(tp, ECONNRESET);
4016320Swnj 		tp = 0;
4025065Swnj 		goto drop;
4035065Swnj 
4045065Swnj 	case TCPS_CLOSING:
4055065Swnj 	case TCPS_LAST_ACK:
4065065Swnj 	case TCPS_TIME_WAIT:
4075065Swnj 		tcp_close(tp);
4086320Swnj 		tp = 0;
4095065Swnj 		goto drop;
4104601Swnj 	}
4114601Swnj 
4124601Swnj 	/*
4135065Swnj 	 * If a SYN is in the window, then this is an
4145065Swnj 	 * error and we send an RST and drop the connection.
4154601Swnj 	 */
4165065Swnj 	if (tiflags & TH_SYN) {
4175231Swnj 		tcp_drop(tp, ECONNRESET);
4186266Swnj 		tp = 0;
4195085Swnj 		goto dropwithreset;
4204601Swnj 	}
4214601Swnj 
4224601Swnj 	/*
4235065Swnj 	 * If the ACK bit is off we drop the segment and return.
4244601Swnj 	 */
4255085Swnj 	if ((tiflags & TH_ACK) == 0)
4265065Swnj 		goto drop;
4275065Swnj 
4285065Swnj 	/*
4295065Swnj 	 * Ack processing.
4305065Swnj 	 */
4314601Swnj 	switch (tp->t_state) {
4324601Swnj 
4335065Swnj 	/*
4345065Swnj 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
4355065Swnj 	 * ESTABLISHED state and continue processing, othewise
4365065Swnj 	 * send an RST.
4375065Swnj 	 */
4385065Swnj 	case TCPS_SYN_RECEIVED:
4395085Swnj 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
4405231Swnj 		    SEQ_GT(ti->ti_ack, tp->snd_max))
4415085Swnj 			goto dropwithreset;
4425244Sroot 		tp->snd_una++;			/* SYN acked */
4435357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
4445357Sroot 			tp->snd_nxt = tp->snd_una;
4455244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
4465085Swnj 		soisconnected(so);
4475085Swnj 		tp->t_state = TCPS_ESTABLISHED;
4485162Swnj 		(void) tcp_reass(tp, (struct tcpiphdr *)0);
4495244Sroot 		tp->snd_wl1 = ti->ti_seq - 1;
4505085Swnj 		/* fall into ... */
4514601Swnj 
4525065Swnj 	/*
4535065Swnj 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
4545065Swnj 	 * ACKs.  If the ack is in the range
4555231Swnj 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
4565065Swnj 	 * then advance tp->snd_una to ti->ti_ack and drop
4575065Swnj 	 * data from the retransmission queue.  If this ACK reflects
4585065Swnj 	 * more up to date window information we update our window information.
4595065Swnj 	 */
4605065Swnj 	case TCPS_ESTABLISHED:
4615065Swnj 	case TCPS_FIN_WAIT_1:
4625065Swnj 	case TCPS_FIN_WAIT_2:
4635065Swnj 	case TCPS_CLOSE_WAIT:
4645065Swnj 	case TCPS_CLOSING:
4655244Sroot 	case TCPS_LAST_ACK:
4665244Sroot 	case TCPS_TIME_WAIT:
4675085Swnj #define	ourfinisacked	(acked > 0)
4685085Swnj 
4695244Sroot 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una))
4705065Swnj 			break;
4715300Sroot 		if (SEQ_GT(ti->ti_ack, tp->snd_max))
4725065Swnj 			goto dropafterack;
4735085Swnj 		acked = ti->ti_ack - tp->snd_una;
4745951Swnj 
4755951Swnj 		/*
4765951Swnj 		 * If transmit timer is running and timed sequence
4775951Swnj 		 * number was acked, update smoothed round trip time.
4785951Swnj 		 */
4795951Swnj 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
4805951Swnj 			if (tp->t_srtt == 0)
4815951Swnj 				tp->t_srtt = tp->t_rtt;
4825951Swnj 			else
4835951Swnj 				tp->t_srtt =
4845951Swnj 				    tcp_alpha * tp->t_srtt +
4855951Swnj 				    (1 - tcp_alpha) * tp->t_rtt;
4865951Swnj /* printf("rtt %d srtt*100 now %d\n", tp->t_rtt, (int)(tp->t_srtt*100)); */
4875951Swnj 			tp->t_rtt = 0;
4885951Swnj 		}
4895951Swnj 
4905307Sroot 		if (ti->ti_ack == tp->snd_max)
4915244Sroot 			tp->t_timer[TCPT_REXMT] = 0;
4925307Sroot 		else {
4935244Sroot 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
4945244Sroot 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
4955951Swnj 			tp->t_rtt = 1;
4965300Sroot 			tp->t_rxtshift = 0;
4975085Swnj 		}
4985307Sroot 		if (acked > so->so_snd.sb_cc) {
4995307Sroot 			sbdrop(&so->so_snd, so->so_snd.sb_cc);
5005307Sroot 			tp->snd_wnd -= so->so_snd.sb_cc;
5015307Sroot 		} else {
5026161Ssam 			sbdrop(&so->so_snd, acked);
5035307Sroot 			tp->snd_wnd -= acked;
5045307Sroot 			acked = 0;
5055307Sroot 		}
5066434Swnj 		if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel)
5075300Sroot 			sowwakeup(so);
5085231Swnj 		tp->snd_una = ti->ti_ack;
5095357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
5105357Sroot 			tp->snd_nxt = tp->snd_una;
5115162Swnj 
5124601Swnj 		switch (tp->t_state) {
5134601Swnj 
5145065Swnj 		/*
5155065Swnj 		 * In FIN_WAIT_1 STATE in addition to the processing
5165065Swnj 		 * for the ESTABLISHED state if our FIN is now acknowledged
5175085Swnj 		 * then enter FIN_WAIT_2.
5185065Swnj 		 */
5195065Swnj 		case TCPS_FIN_WAIT_1:
5205896Swnj 			if (ourfinisacked) {
5215896Swnj 				/*
5225896Swnj 				 * If we can't receive any more
5235896Swnj 				 * data, then closing user can proceed.
5245896Swnj 				 */
5255896Swnj 				if (so->so_state & SS_CANTRCVMORE)
5265896Swnj 					soisdisconnected(so);
5275085Swnj 				tp->t_state = TCPS_FIN_WAIT_2;
5285896Swnj 			}
5294601Swnj 			break;
5304601Swnj 
5315065Swnj 	 	/*
5325065Swnj 		 * In CLOSING STATE in addition to the processing for
5335065Swnj 		 * the ESTABLISHED state if the ACK acknowledges our FIN
5345065Swnj 		 * then enter the TIME-WAIT state, otherwise ignore
5355065Swnj 		 * the segment.
5365065Swnj 		 */
5375065Swnj 		case TCPS_CLOSING:
5385244Sroot 			if (ourfinisacked) {
5395065Swnj 				tp->t_state = TCPS_TIME_WAIT;
5405244Sroot 				tcp_canceltimers(tp);
5415244Sroot 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
5425244Sroot 				soisdisconnected(so);
5435244Sroot 			}
5445244Sroot 			break;
5454601Swnj 
5465065Swnj 		/*
5475085Swnj 		 * The only thing that can arrive in  LAST_ACK state
5485085Swnj 		 * is an acknowledgment of our FIN.  If our FIN is now
5495085Swnj 		 * acknowledged, delete the TCB, enter the closed state
5505085Swnj 		 * and return.
5515065Swnj 		 */
5525065Swnj 		case TCPS_LAST_ACK:
5536320Swnj 			if (ourfinisacked) {
5545065Swnj 				tcp_close(tp);
5556320Swnj 				tp = 0;
5566320Swnj 			}
5575065Swnj 			goto drop;
5584601Swnj 
5595065Swnj 		/*
5605065Swnj 		 * In TIME_WAIT state the only thing that should arrive
5615065Swnj 		 * is a retransmission of the remote FIN.  Acknowledge
5625065Swnj 		 * it and restart the finack timer.
5635065Swnj 		 */
5645065Swnj 		case TCPS_TIME_WAIT:
5655162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
5665065Swnj 			goto dropafterack;
5674601Swnj 		}
5685085Swnj #undef ourfinisacked
5695085Swnj 	}
5704601Swnj 
5715065Swnj step6:
5725065Swnj 	/*
5735244Sroot 	 * Update window information.
5745244Sroot 	 */
5755300Sroot 	if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
5765391Swnj 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
5775300Sroot 	     tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) {
5785244Sroot 		tp->snd_wnd = ti->ti_win;
5795244Sroot 		tp->snd_wl1 = ti->ti_seq;
5805244Sroot 		tp->snd_wl2 = ti->ti_ack;
5815244Sroot 		if (tp->snd_wnd > 0)
5825244Sroot 			tp->t_timer[TCPT_PERSIST] = 0;
5835244Sroot 	}
5845244Sroot 
5855244Sroot 	/*
5865547Swnj 	 * Process segments with URG.
5875065Swnj 	 */
5887267Swnj 	if ((tiflags & TH_URG) && ti->ti_urp &&
5897267Swnj 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
5905547Swnj 		/*
5915547Swnj 		 * If this segment advances the known urgent pointer,
5925547Swnj 		 * then mark the data stream.  This should not happen
5935547Swnj 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
5945547Swnj 		 * a FIN has been received from the remote side.
5955547Swnj 		 * In these states we ignore the URG.
5965547Swnj 		 */
5975547Swnj 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
5985547Swnj 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
5995547Swnj 			so->so_oobmark = so->so_rcv.sb_cc +
6005547Swnj 			    (tp->rcv_up - tp->rcv_nxt) - 1;
6015547Swnj 			if (so->so_oobmark == 0)
6025547Swnj 				so->so_state |= SS_RCVATMARK;
6035440Swnj #ifdef TCPTRUEOOB
6045547Swnj 			if ((tp->t_flags & TF_DOOOB) == 0)
6055440Swnj #endif
6065547Swnj 				sohasoutofband(so);
6075547Swnj 			tp->t_oobflags &= ~TCPOOB_HAVEDATA;
6085440Swnj 		}
6095547Swnj 		/*
6105547Swnj 		 * Remove out of band data so doesn't get presented to user.
6115547Swnj 		 * This can happen independent of advancing the URG pointer,
6125547Swnj 		 * but if two URG's are pending at once, some out-of-band
6135547Swnj 		 * data may creep in... ick.
6145547Swnj 		 */
6157510Sroot 		if (ti->ti_urp <= ti->ti_len)
6165547Swnj 			tcp_pulloutofband(so, ti);
6175419Swnj 	}
6184601Swnj 
6194601Swnj 	/*
6205065Swnj 	 * Process the segment text, merging it into the TCP sequencing queue,
6215065Swnj 	 * and arranging for acknowledgment of receipt if necessary.
6225065Swnj 	 * This process logically involves adjusting tp->rcv_wnd as data
6235065Swnj 	 * is presented to the user (this happens in tcp_usrreq.c,
6245065Swnj 	 * case PRU_RCVD).  If a FIN has already been received on this
6255065Swnj 	 * connection then we just ignore the text.
6264601Swnj 	 */
6275263Swnj 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
6285263Swnj 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
6295065Swnj 		tiflags = tcp_reass(tp, ti);
6305440Swnj 		if (tcpnodelack == 0)
6315440Swnj 			tp->t_flags |= TF_DELACK;
6325440Swnj 		else
6335440Swnj 			tp->t_flags |= TF_ACKNOW;
6345244Sroot 	} else {
6354924Swnj 		m_freem(m);
6365263Swnj 		tiflags &= ~TH_FIN;
6375244Sroot 	}
6384601Swnj 
6394601Swnj 	/*
6405263Swnj 	 * If FIN is received ACK the FIN and let the user know
6415263Swnj 	 * that the connection is closing.
6424601Swnj 	 */
6435263Swnj 	if (tiflags & TH_FIN) {
6445244Sroot 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
6455244Sroot 			socantrcvmore(so);
6465244Sroot 			tp->t_flags |= TF_ACKNOW;
6475244Sroot 			tp->rcv_nxt++;
6485244Sroot 		}
6495065Swnj 		switch (tp->t_state) {
6504601Swnj 
6515065Swnj 	 	/*
6525065Swnj 		 * In SYN_RECEIVED and ESTABLISHED STATES
6535065Swnj 		 * enter the CLOSE_WAIT state.
6544884Swnj 		 */
6555065Swnj 		case TCPS_SYN_RECEIVED:
6565065Swnj 		case TCPS_ESTABLISHED:
6575065Swnj 			tp->t_state = TCPS_CLOSE_WAIT;
6585065Swnj 			break;
6594884Swnj 
6605065Swnj 	 	/*
6615085Swnj 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
6625085Swnj 		 * enter the CLOSING state.
6634884Swnj 		 */
6645065Swnj 		case TCPS_FIN_WAIT_1:
6655085Swnj 			tp->t_state = TCPS_CLOSING;
6665065Swnj 			break;
6674601Swnj 
6685065Swnj 	 	/*
6695065Swnj 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
6705065Swnj 		 * starting the time-wait timer, turning off the other
6715065Swnj 		 * standard timers.
6725065Swnj 		 */
6735065Swnj 		case TCPS_FIN_WAIT_2:
6745244Sroot 			tp->t_state = TCPS_TIME_WAIT;
6755074Swnj 			tcp_canceltimers(tp);
6765162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
6775244Sroot 			soisdisconnected(so);
6785065Swnj 			break;
6795065Swnj 
6804884Swnj 		/*
6815065Swnj 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
6824884Swnj 		 */
6835065Swnj 		case TCPS_TIME_WAIT:
6845162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
6855065Swnj 			break;
6865085Swnj 		}
6874601Swnj 	}
6885267Sroot 	if (so->so_options & SO_DEBUG)
6895267Sroot 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
6905085Swnj 
6915085Swnj 	/*
6925085Swnj 	 * Return any desired output.
6935085Swnj 	 */
6946161Ssam 	(void) tcp_output(tp);
6955065Swnj 	return;
6965085Swnj 
6975065Swnj dropafterack:
6985085Swnj 	/*
6996211Swnj 	 * Generate an ACK dropping incoming segment if it occupies
7006211Swnj 	 * sequence space, where the ACK reflects our state.
7015085Swnj 	 */
7026211Swnj 	if ((tiflags&TH_RST) ||
7036211Swnj 	    tlen == 0 && (tiflags&(TH_SYN|TH_FIN)) == 0)
7045085Swnj 		goto drop;
7056303Sroot 	if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
7066303Sroot 		tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0);
7075391Swnj 	tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK);
7085231Swnj 	return;
7095085Swnj 
7105085Swnj dropwithreset:
7115440Swnj 	if (om)
7126161Ssam 		(void) m_free(om);
7135085Swnj 	/*
7145244Sroot 	 * Generate a RST, dropping incoming segment.
7155085Swnj 	 * Make ACK acceptable to originator of segment.
7165085Swnj 	 */
7175085Swnj 	if (tiflags & TH_RST)
7185085Swnj 		goto drop;
7195085Swnj 	if (tiflags & TH_ACK)
7205391Swnj 		tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST);
7215085Swnj 	else {
7225085Swnj 		if (tiflags & TH_SYN)
7235085Swnj 			ti->ti_len++;
7246211Swnj 		tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0,
7256211Swnj 		    TH_RST|TH_ACK);
7265085Swnj 	}
7275231Swnj 	return;
7285085Swnj 
7295065Swnj drop:
7305085Swnj 	/*
7315085Swnj 	 * Drop space held by incoming segment and return.
7325085Swnj 	 */
7336303Sroot 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
7346303Sroot 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
7355065Swnj 	m_freem(m);
7365267Sroot 	return;
7375065Swnj }
7385065Swnj 
7395440Swnj tcp_dooptions(tp, om)
7405440Swnj 	struct tcpcb *tp;
7415440Swnj 	struct mbuf *om;
7425419Swnj {
7435440Swnj 	register u_char *cp;
7445440Swnj 	int opt, optlen, cnt;
7455419Swnj 
7465440Swnj 	cp = mtod(om, u_char *);
7475440Swnj 	cnt = om->m_len;
7485440Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
7495440Swnj 		opt = cp[0];
7505440Swnj 		if (opt == TCPOPT_EOL)
7515440Swnj 			break;
7525440Swnj 		if (opt == TCPOPT_NOP)
7535440Swnj 			optlen = 1;
7545440Swnj 		else
7555440Swnj 			optlen = cp[1];
7565440Swnj 		switch (opt) {
7575440Swnj 
7585440Swnj 		default:
7595440Swnj 			break;
7605440Swnj 
7615440Swnj 		case TCPOPT_MAXSEG:
7625440Swnj 			if (optlen != 4)
7635440Swnj 				continue;
7645440Swnj 			tp->t_maxseg = *(u_short *)(cp + 2);
7656320Swnj #if vax || pdp11
7666161Ssam 			tp->t_maxseg = ntohs((u_short)tp->t_maxseg);
7675440Swnj #endif
7685440Swnj 			break;
7695440Swnj 
7705440Swnj #ifdef TCPTRUEOOB
7715440Swnj 		case TCPOPT_WILLOOB:
7725440Swnj 			tp->t_flags |= TF_DOOOB;
7735440Swnj printf("tp %x dooob\n", tp);
7745440Swnj 			break;
7755440Swnj 
7765440Swnj 		case TCPOPT_OOBDATA: {
7775440Swnj 			int seq;
7785547Swnj 			register struct socket *so = tp->t_inpcb->inp_socket;
7795547Swnj 			tcp_seq mark;
7805440Swnj 
7815547Swnj 			if (optlen != 8)
7825440Swnj 				continue;
7835440Swnj 			seq = cp[2];
7845440Swnj 			if (seq < tp->t_iobseq)
7855440Swnj 				seq += 256;
7865440Swnj printf("oobdata cp[2] %d iobseq %d seq %d\n", cp[2], tp->t_iobseq, seq);
7875440Swnj 			if (seq - tp->t_iobseq > 128) {
7885440Swnj printf("bad seq\n");
7895440Swnj 				tp->t_oobflags |= TCPOOB_OWEACK;
7905440Swnj 				break;
7915440Swnj 			}
7925440Swnj 			tp->t_iobseq = cp[2];
7935440Swnj 			tp->t_iobc = cp[3];
7945547Swnj 			mark = *(tcp_seq *)(cp + 4);
7956320Swnj #if vax || pdp11
7965547Swnj 			mark = ntohl(mark);
7975547Swnj #endif
7985547Swnj 			so->so_oobmark = so->so_rcv.sb_cc + (mark-tp->rcv_nxt);
7995547Swnj 			if (so->so_oobmark == 0)
8005547Swnj 				so->so_state |= SS_RCVATMARK;
8015440Swnj printf("take oob data %x input iobseq now %x\n", tp->t_iobc, tp->t_iobseq);
8025547Swnj 			sohasoutofband(so);
8035440Swnj 			break;
8045419Swnj 		}
8055440Swnj 
8065440Swnj 		case TCPOPT_OOBACK: {
8075440Swnj 			int seq;
8085440Swnj 
8095440Swnj 			if (optlen != 4)
8105440Swnj 				continue;
8115440Swnj 			if (tp->t_oobseq != cp[2]) {
8125440Swnj printf("wrong ack\n");
8135440Swnj 				break;
8145440Swnj 			}
8155440Swnj printf("take oob ack %x and cancel rexmt\n", cp[2]);
8165440Swnj 			tp->t_oobflags &= ~TCPOOB_NEEDACK;
8175440Swnj 			tp->t_timer[TCPT_OOBREXMT] = 0;
8185419Swnj 			break;
8195440Swnj 		}
8205440Swnj #endif TCPTRUEOOB
8215440Swnj 		}
8225419Swnj 	}
8236161Ssam 	(void) m_free(om);
8245419Swnj }
8255419Swnj 
8265419Swnj /*
8275547Swnj  * Pull out of band byte out of a segment so
8285547Swnj  * it doesn't appear in the user's data queue.
8295547Swnj  * It is still reflected in the segment length for
8305547Swnj  * sequencing purposes.
8315547Swnj  */
8325547Swnj tcp_pulloutofband(so, ti)
8335547Swnj 	struct socket *so;
8345547Swnj 	struct tcpiphdr *ti;
8355547Swnj {
8365547Swnj 	register struct mbuf *m;
8376116Swnj 	int cnt = ti->ti_urp - 1;
8385547Swnj 
8395547Swnj 	m = dtom(ti);
8405547Swnj 	while (cnt >= 0) {
8415547Swnj 		if (m->m_len > cnt) {
8425547Swnj 			char *cp = mtod(m, caddr_t) + cnt;
8435547Swnj 			struct tcpcb *tp = sototcpcb(so);
8445547Swnj 
8455547Swnj 			tp->t_iobc = *cp;
8465547Swnj 			tp->t_oobflags |= TCPOOB_HAVEDATA;
8476161Ssam 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
8485547Swnj 			m->m_len--;
8495547Swnj 			return;
8505547Swnj 		}
8515547Swnj 		cnt -= m->m_len;
8525547Swnj 		m = m->m_next;
8535547Swnj 		if (m == 0)
8545547Swnj 			break;
8555547Swnj 	}
8565547Swnj 	panic("tcp_pulloutofband");
8575547Swnj }
8585547Swnj 
8595547Swnj /*
8605065Swnj  * Insert segment ti into reassembly queue of tcp with
8615065Swnj  * control block tp.  Return TH_FIN if reassembly now includes
8625065Swnj  * a segment with FIN.
8635065Swnj  */
8645109Swnj tcp_reass(tp, ti)
8655065Swnj 	register struct tcpcb *tp;
8665065Swnj 	register struct tcpiphdr *ti;
8675065Swnj {
8685065Swnj 	register struct tcpiphdr *q;
8695085Swnj 	struct socket *so = tp->t_inpcb->inp_socket;
8705263Swnj 	struct mbuf *m;
8715263Swnj 	int flags;
8725065Swnj 
8735065Swnj 	/*
8745162Swnj 	 * Call with ti==0 after become established to
8755162Swnj 	 * force pre-ESTABLISHED data up to user socket.
8765065Swnj 	 */
8775162Swnj 	if (ti == 0)
8785065Swnj 		goto present;
8794601Swnj 
8805065Swnj 	/*
8815065Swnj 	 * Find a segment which begins after this one does.
8825065Swnj 	 */
8835065Swnj 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
8845065Swnj 	    q = (struct tcpiphdr *)q->ti_next)
8855065Swnj 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
8865065Swnj 			break;
8874601Swnj 
8885065Swnj 	/*
8895065Swnj 	 * If there is a preceding segment, it may provide some of
8905065Swnj 	 * our data already.  If so, drop the data from the incoming
8915065Swnj 	 * segment.  If it provides all of our data, drop us.
8925065Swnj 	 */
8935065Swnj 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
8945065Swnj 		register int i;
8955690Swnj 		q = (struct tcpiphdr *)q->ti_prev;
8965065Swnj 		/* conversion to int (in i) handles seq wraparound */
8975065Swnj 		i = q->ti_seq + q->ti_len - ti->ti_seq;
8985065Swnj 		if (i > 0) {
8994924Swnj 			if (i >= ti->ti_len)
9005065Swnj 				goto drop;
9017338Swnj 			m_adj(dtom(ti), i);
9025065Swnj 			ti->ti_len -= i;
9034924Swnj 			ti->ti_seq += i;
9044601Swnj 		}
9055065Swnj 		q = (struct tcpiphdr *)(q->ti_next);
9065065Swnj 	}
9074601Swnj 
9085065Swnj 	/*
9095065Swnj 	 * While we overlap succeeding segments trim them or,
9105065Swnj 	 * if they are completely covered, dequeue them.
9115065Swnj 	 */
9125690Swnj 	while (q != (struct tcpiphdr *)tp) {
9135065Swnj 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
9145690Swnj 		if (i <= 0)
9155690Swnj 			break;
9165065Swnj 		if (i < q->ti_len) {
9175690Swnj 			q->ti_seq += i;
9185065Swnj 			q->ti_len -= i;
9195065Swnj 			m_adj(dtom(q), i);
9205065Swnj 			break;
9214601Swnj 		}
9225065Swnj 		q = (struct tcpiphdr *)q->ti_next;
9235623Swnj 		m = dtom(q->ti_prev);
9245065Swnj 		remque(q->ti_prev);
9255623Swnj 		m_freem(m);
9265065Swnj 	}
9274601Swnj 
9285065Swnj 	/*
9295065Swnj 	 * Stick new segment in its place.
9305065Swnj 	 */
9315065Swnj 	insque(ti, q->ti_prev);
9324601Swnj 
9335065Swnj present:
9345065Swnj 	/*
9355244Sroot 	 * Present data to user, advancing rcv_nxt through
9365244Sroot 	 * completed sequence space.
9375065Swnj 	 */
9385263Swnj 	if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
9395244Sroot 		return (0);
9404924Swnj 	ti = tp->seg_next;
9415263Swnj 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
9425263Swnj 		return (0);
9435263Swnj 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
9445263Swnj 		return (0);
9455263Swnj 	do {
9465244Sroot 		tp->rcv_nxt += ti->ti_len;
9475244Sroot 		flags = ti->ti_flags & TH_FIN;
9484924Swnj 		remque(ti);
9495263Swnj 		m = dtom(ti);
9504924Swnj 		ti = (struct tcpiphdr *)ti->ti_next;
9515263Swnj 		if (so->so_state & SS_CANTRCVMORE)
9526161Ssam 			m_freem(m);
9535263Swnj 		else
9545263Swnj 			sbappend(&so->so_rcv, m);
9555263Swnj 	} while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
9565263Swnj 	sorwakeup(so);
9575065Swnj 	return (flags);
9585065Swnj drop:
9595065Swnj 	m_freem(dtom(ti));
9605263Swnj 	return (0);
9614601Swnj }
962