xref: /csrg-svn/sys/netinet/tcp_input.c (revision 5231)
1*5231Swnj /*	tcp_input.c	1.38	81/12/09	*/
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"
215109Swnj #include "../errno.h"
224601Swnj 
234679Swnj int	tcpcksum = 1;
244601Swnj 
255065Swnj /*
265065Swnj  * TCP input routine, follows pages 65-76 of the
275065Swnj  * protocol specification dated September, 1981 very closely.
285065Swnj  */
294924Swnj tcp_input(m0)
304924Swnj 	struct mbuf *m0;
314601Swnj {
324924Swnj 	register struct tcpiphdr *ti;
334924Swnj 	struct inpcb *inp;
344924Swnj 	register struct mbuf *m;
354924Swnj 	int len, tlen, off;
364924Swnj 	register struct tcpcb *tp;
374924Swnj 	register int tiflags;
384803Swnj 	struct socket *so;
395109Swnj 	int todrop, acked;
404924Swnj 
414601Swnj COUNT(TCP_INPUT);
424924Swnj 	/*
434924Swnj 	 * Get ip and tcp header together in first mbuf.
445223Swnj 	 * Note: ip leaves ip header in first mbuf.
454924Swnj 	 */
464924Swnj 	m = m0;
475020Sroot 	ti = mtod(m, struct tcpiphdr *);
485223Swnj 	if (((struct ip *)ti)->ip_len > sizeof (struct ip))
495208Swnj 		ip_stripoptions((struct ip *)ti, (struct mbuf *)0);
505085Swnj 	if (m->m_len < sizeof (struct tcpiphdr)) {
515085Swnj 		if (m_pullup(m, sizeof (struct tcpiphdr)) == 0) {
525085Swnj 			tcpstat.tcps_hdrops++;
535085Swnj 			goto drop;
545085Swnj 		}
555085Swnj 		ti = mtod(m, struct tcpiphdr *);
565085Swnj 	}
574601Swnj 
584601Swnj 	/*
594924Swnj 	 * Checksum extended tcp header and data.
604601Swnj 	 */
614924Swnj 	tlen = ((struct ip *)ti)->ip_len;
624924Swnj 	len = sizeof (struct ip) + tlen;
634679Swnj 	if (tcpcksum) {
644924Swnj 		ti->ti_next = ti->ti_prev = 0;
654924Swnj 		ti->ti_x1 = 0;
665223Swnj 		ti->ti_len = (u_short)tlen;
675223Swnj #if vax
685223Swnj 		ti->ti_len = htons(ti->ti_len);
695223Swnj #endif
70*5231Swnj 		if (ti->ti_sum = in_cksum(m, len)) {
714924Swnj 			tcpstat.tcps_badsum++;
725065Swnj 			printf("tcp cksum %x\n", ti->ti_sum);
735085Swnj 			goto drop;
744601Swnj 		}
754601Swnj 	}
764601Swnj 
774601Swnj 	/*
784924Swnj 	 * Check that tcp offset makes sense,
794924Swnj 	 * process tcp options and adjust length.
804601Swnj 	 */
814924Swnj 	off = ti->ti_off << 2;
82*5231Swnj 	if (off < sizeof (struct tcphdr) || off > tlen) {
834924Swnj 		tcpstat.tcps_badoff++;
845085Swnj 		goto drop;
854924Swnj 	}
864924Swnj 	ti->ti_len = tlen - off;
875085Swnj #if 0
88*5231Swnj 	if (off > sizeof (struct tcphdr))
895085Swnj 		tcp_options(ti);
905085Swnj #endif
915065Swnj 	tiflags = ti->ti_flags;
924924Swnj 
93*5231Swnj #if vax
944924Swnj 	/*
955085Swnj 	 * Convert tcp protocol specific fields to host format.
965085Swnj 	 */
975085Swnj 	ti->ti_seq = ntohl(ti->ti_seq);
985085Swnj 	ti->ti_ack = ntohl(ti->ti_ack);
995085Swnj 	ti->ti_win = ntohs(ti->ti_win);
1005085Swnj 	ti->ti_urp = ntohs(ti->ti_urp);
101*5231Swnj #endif
1025085Swnj 
1035085Swnj 	/*
1044924Swnj 	 * Locate pcb for segment.
1054924Swnj 	 */
1065065Swnj 	inp = in_pcblookup
1075065Swnj 		(&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport);
1085065Swnj 
1095065Swnj 	/*
1105065Swnj 	 * If the state is CLOSED (i.e., TCB does not exist) then
1115065Swnj 	 * all data in the incoming segment is discarded.  (p. 65).
1125065Swnj 	 */
1134884Swnj 	if (inp == 0)
1145085Swnj 		goto dropwithreset;
1155065Swnj 	tp = intotcpcb(inp);
1165065Swnj 	if (tp == 0)
1175085Swnj 		goto dropwithreset;
1185109Swnj 	so = inp->inp_socket;
1194601Swnj 
1204601Swnj 	/*
1215162Swnj 	 * Segment received on connection.
1225162Swnj 	 * Reset idle time and keep-alive timer.
1235162Swnj 	 */
1245162Swnj 	tp->t_idle = 0;
1255162Swnj 	tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
1265162Swnj 
1275162Swnj 	/*
1285085Swnj 	 * Calculate amount of space in receive window,
1295085Swnj 	 * and then do TCP input processing.
1304601Swnj 	 */
1315085Swnj 	tp->rcv_wnd = sbspace(&so->so_rcv);
132*5231Swnj 	if (tp->rcv_wnd < 0)
133*5231Swnj 		tp->rcv_wnd = 0;
1344601Swnj 
1354601Swnj 	switch (tp->t_state) {
1364601Swnj 
1375065Swnj 	/*
1385065Swnj 	 * If the state is LISTEN then ignore segment if it contains an RST.
1395065Swnj 	 * If the segment contains an ACK then it is bad and send a RST.
1405065Swnj 	 * If it does not contain a SYN then it is not interesting; drop it.
1415085Swnj 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
1425065Swnj 	 * tp->iss, and send a segment:
1435085Swnj 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
1445065Swnj 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
1455065Swnj 	 * Fill in remote peer address fields if not previously specified.
1465065Swnj 	 * Enter SYN_RECEIVED state, and process any other fields of this
1475065Swnj 	 * segment in this state.  (p. 65)
1485065Swnj 	 */
1495065Swnj 	case TCPS_LISTEN:
1505065Swnj 		if (tiflags & TH_RST)
1515065Swnj 			goto drop;
1525065Swnj 		if (tiflags & TH_ACK)
1535085Swnj 			goto dropwithreset;
1545065Swnj 		if ((tiflags & TH_SYN) == 0)
1555065Swnj 			goto drop;
1565085Swnj 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
1575065Swnj 		tp->irs = ti->ti_seq;
1585085Swnj 		tcp_sendseqinit(tp);
1595085Swnj 		tcp_rcvseqinit(tp);
1605065Swnj 		tp->t_state = TCPS_SYN_RECEIVED;
161*5231Swnj 		in_pcbconnect(inp, ti->ti_src, ti->ti_sport);
1625085Swnj 		goto trimthenstep6;
1634601Swnj 
1645065Swnj 	/*
1655065Swnj 	 * If the state is SYN_SENT:
1665065Swnj 	 *	if seg contains an ACK, but not for our SYN, drop the input.
1675065Swnj 	 *	if seg contains a RST, then drop the connection.
1685065Swnj 	 *	if seg does not contain SYN, then drop it.
1695065Swnj 	 * Otherwise this is an acceptable SYN segment
1705065Swnj 	 *	initialize tp->rcv_nxt and tp->irs
1715065Swnj 	 *	if seg contains ack then advance tp->snd_una
1725065Swnj 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1735065Swnj 	 *	arrange for segment to be acked (eventually)
1745065Swnj 	 *	continue processing rest of data/controls, beginning with URG
1755065Swnj 	 */
1765065Swnj 	case TCPS_SYN_SENT:
1775065Swnj 		if ((tiflags & TH_ACK) &&
1785065Swnj 		    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
179*5231Swnj 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
1805085Swnj 			goto dropwithreset;
1815065Swnj 		if (tiflags & TH_RST) {
1825065Swnj 			if (tiflags & TH_ACK)
1835085Swnj 				tcp_drop(tp, ECONNRESET);
1845065Swnj 			goto drop;
1854601Swnj 		}
1865065Swnj 		if ((tiflags & TH_SYN) == 0)
1875065Swnj 			goto drop;
188*5231Swnj 		tp->snd_una = ti->ti_ack;
1895065Swnj 		tp->irs = ti->ti_seq;
1905085Swnj 		tcp_rcvseqinit(tp);
1915085Swnj 		tp->t_flags |= TF_ACKNOW;
1925162Swnj 		if (SEQ_GT(tp->snd_una, tp->iss)) {
1935065Swnj 			tp->t_state = TCPS_ESTABLISHED;
1945162Swnj 			(void) tcp_reass(tp, (struct tcpiphdr *)0);
195*5231Swnj 			tp->snd_wl1 = ti->ti_seq;
1965162Swnj 		} else
1975085Swnj 			tp->t_state = TCPS_SYN_RECEIVED;
1985085Swnj 		goto trimthenstep6;
1995085Swnj 
2005085Swnj trimthenstep6:
2015085Swnj 		/*
202*5231Swnj 		 * Advance ti->ti_seq to correspond to first data byte.
2035085Swnj 		 * If data, trim to stay within window,
2045085Swnj 		 * dropping FIN if necessary.
2055085Swnj 		 */
206*5231Swnj 		ti->ti_seq++;
2075085Swnj 		if (ti->ti_len > tp->rcv_wnd) {
2085085Swnj 			todrop = ti->ti_len - tp->rcv_wnd;
2095085Swnj 			m_adj(m, -todrop);
2105085Swnj 			ti->ti_len = tp->rcv_wnd;
2115085Swnj 			ti->ti_flags &= ~TH_FIN;
2125065Swnj 		}
2135085Swnj 		goto step6;
2145065Swnj 	}
2154601Swnj 
2165065Swnj 	/*
2175065Swnj 	 * States other than LISTEN or SYN_SENT.
2185065Swnj 	 * First check that at least some bytes of segment are within
2195065Swnj 	 * receive window.
2205065Swnj 	 */
2215065Swnj 	if (tp->rcv_wnd == 0) {
2225065Swnj 		/*
2235065Swnj 		 * If window is closed can only take segments at
224*5231Swnj 		 * window edge, and have to drop data and PUSH from
2255065Swnj 		 * incoming segments.
2265065Swnj 		 */
2275065Swnj 		if (tp->rcv_nxt != ti->ti_seq)
2285065Swnj 			goto dropafterack;
2295085Swnj 		if (ti->ti_len > 0) {
2305085Swnj 			ti->ti_len = 0;
2315085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
2325065Swnj 		}
2335065Swnj 	} else {
2345065Swnj 		/*
235*5231Swnj 		 * If segment begins before rcv_nxt, drop leading
2365065Swnj 		 * data (and SYN); if nothing left, just ack.
2375065Swnj 		 */
2385065Swnj 		if (SEQ_GT(tp->rcv_nxt, ti->ti_seq)) {
2395085Swnj 			todrop = tp->rcv_nxt - ti->ti_seq;
2405085Swnj 			if (tiflags & TH_SYN) {
2415085Swnj 				ti->ti_seq++;
2425085Swnj 				if (ti->ti_urp > 1)
2435085Swnj 					ti->ti_urp--;
2445085Swnj 				else
2455085Swnj 					tiflags &= ~TH_URG;
2465085Swnj 				todrop--;
2475085Swnj 			}
2485065Swnj 			if (todrop > ti->ti_len)
2495065Swnj 				goto dropafterack;
2505065Swnj 			m_adj(m, todrop);
2515065Swnj 			ti->ti_seq += todrop;
2525065Swnj 			ti->ti_len -= todrop;
2535085Swnj 			if (ti->ti_urp > todrop)
2545085Swnj 				ti->ti_urp -= todrop;
2555085Swnj 			else {
2565085Swnj 				tiflags &= ~TH_URG;
2575085Swnj 				/* ti->ti_flags &= ~TH_URG; */
2585085Swnj 				/* ti->ti_urp = 0; */
2595085Swnj 			}
2605085Swnj 			/* tiflags &= ~TH_SYN; */
2615085Swnj 			/* ti->ti_flags &= ~TH_SYN; */
2625065Swnj 		}
2635065Swnj 		/*
2645065Swnj 		 * If segment ends after window, drop trailing data
2655085Swnj 		 * (and PUSH and FIN); if nothing left, just ACK.
2665065Swnj 		 */
2675065Swnj 		if (SEQ_GT(ti->ti_seq+ti->ti_len, tp->rcv_nxt+tp->rcv_wnd)) {
2685085Swnj 			todrop =
2695065Swnj 			     ti->ti_seq+ti->ti_len - (tp->rcv_nxt+tp->rcv_wnd);
2705065Swnj 			if (todrop > ti->ti_len)
2715065Swnj 				goto dropafterack;
2725065Swnj 			m_adj(m, -todrop);
2735065Swnj 			ti->ti_len -= todrop;
2745085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
2755065Swnj 		}
2765065Swnj 	}
2774601Swnj 
2785065Swnj 	/*
2795065Swnj 	 * If the RST bit is set examine the state:
2805065Swnj 	 *    SYN_RECEIVED STATE:
2815065Swnj 	 *	If passive open, return to LISTEN state.
2825065Swnj 	 *	If active open, inform user that connection was refused.
2835065Swnj 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
2845065Swnj 	 *	Inform user that connection was reset, and close tcb.
2855065Swnj 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
2865065Swnj 	 *	Close the tcb.
2875065Swnj 	 */
2885065Swnj 	if (tiflags&TH_RST) switch (tp->t_state) {
2895065Swnj 
2905065Swnj 	case TCPS_SYN_RECEIVED:
2915065Swnj 		if (inp->inp_socket->so_options & SO_ACCEPTCONN) {
2925085Swnj 			tp->t_state = TCPS_LISTEN;
293*5231Swnj 			in_pcbdisconnect(inp);
2945065Swnj 			goto drop;
2954601Swnj 		}
2965085Swnj 		tcp_drop(tp, ECONNREFUSED);
2975065Swnj 		goto drop;
2984601Swnj 
2995065Swnj 	case TCPS_ESTABLISHED:
3005065Swnj 	case TCPS_FIN_WAIT_1:
3015065Swnj 	case TCPS_FIN_WAIT_2:
3025065Swnj 	case TCPS_CLOSE_WAIT:
3035065Swnj 		tcp_drop(tp, ECONNRESET);
3045065Swnj 		goto drop;
3055065Swnj 
3065065Swnj 	case TCPS_CLOSING:
3075065Swnj 	case TCPS_LAST_ACK:
3085065Swnj 	case TCPS_TIME_WAIT:
3095065Swnj 		tcp_close(tp);
3105065Swnj 		goto drop;
3114601Swnj 	}
3124601Swnj 
3134601Swnj 	/*
3145065Swnj 	 * If a SYN is in the window, then this is an
3155065Swnj 	 * error and we send an RST and drop the connection.
3164601Swnj 	 */
3175065Swnj 	if (tiflags & TH_SYN) {
318*5231Swnj 		tcp_drop(tp, ECONNRESET);
3195085Swnj 		goto dropwithreset;
3204601Swnj 	}
3214601Swnj 
3224601Swnj 	/*
3235065Swnj 	 * If the ACK bit is off we drop the segment and return.
3244601Swnj 	 */
3255085Swnj 	if ((tiflags & TH_ACK) == 0)
3265065Swnj 		goto drop;
3275065Swnj 
3285065Swnj 	/*
3295065Swnj 	 * Ack processing.
3305065Swnj 	 */
3314601Swnj 	switch (tp->t_state) {
3324601Swnj 
3335065Swnj 	/*
3345065Swnj 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
3355065Swnj 	 * ESTABLISHED state and continue processing, othewise
3365065Swnj 	 * send an RST.
3375065Swnj 	 */
3385065Swnj 	case TCPS_SYN_RECEIVED:
3395085Swnj 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
340*5231Swnj 		    SEQ_GT(ti->ti_ack, tp->snd_max))
3415085Swnj 			goto dropwithreset;
3425085Swnj 		soisconnected(so);
3435085Swnj 		tp->t_state = TCPS_ESTABLISHED;
3445162Swnj 		(void) tcp_reass(tp, (struct tcpiphdr *)0);
345*5231Swnj 		tp->snd_wl1 = tp->ti_seq - 1;
3465085Swnj 		/* fall into ... */
3474601Swnj 
3485065Swnj 	/*
3495065Swnj 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
3505065Swnj 	 * ACKs.  If the ack is in the range
351*5231Swnj 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
3525065Swnj 	 * then advance tp->snd_una to ti->ti_ack and drop
3535065Swnj 	 * data from the retransmission queue.  If this ACK reflects
3545065Swnj 	 * more up to date window information we update our window information.
3555065Swnj 	 */
3565065Swnj 	case TCPS_ESTABLISHED:
3575065Swnj 	case TCPS_FIN_WAIT_1:
3585065Swnj 	case TCPS_FIN_WAIT_2:
3595065Swnj 	case TCPS_CLOSE_WAIT:
3605065Swnj 	case TCPS_CLOSING:
3615085Swnj #define	ourfinisacked	(acked > 0)
3625085Swnj 
3635065Swnj 		if (SEQ_LT(ti->ti_ack, tp->snd_una))
3645065Swnj 			break;
365*5231Swnj 		if (SEQ_GT(ti->ti_ack, tp->snd_max))
3665065Swnj 			goto dropafterack;
3675085Swnj 		acked = ti->ti_ack - tp->snd_una;
3685085Swnj 		if (acked > so->so_snd.sb_cc) {
3695085Swnj 			sbflush(&so->so_snd);
3705085Swnj 			acked -= so->so_snd.sb_cc;
3715162Swnj 			/* if acked our FIN is acked */
3725085Swnj 		} else {
3735085Swnj 			sbdrop(&so->so_snd, acked);
3745085Swnj 			acked = 0;
3755085Swnj 		}
376*5231Swnj 		tp->snd_una = ti->ti_ack;
3775162Swnj 
3785162Swnj 		/*
3795162Swnj 		 * If transmit timer is running and timed sequence
3805162Swnj 		 * number was acked, update smoothed round trip time.
3815162Swnj 		 */
3825162Swnj 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
3835162Swnj 			tp->t_srtt =
3845162Swnj 			    tcp_beta * tp->t_srtt +
3855162Swnj 			    (1 - tcp_beta) * tp->t_rtt;
3865162Swnj 			tp->t_rtt = 0;
3875162Swnj 		}
3885162Swnj 
3895085Swnj 		/*
3905085Swnj 		 * Update window information.
3915085Swnj 		 */
3925065Swnj 		if (SEQ_LT(tp->snd_wl1, ti->ti_seq) ||
3935085Swnj 		    tp->snd_wl1==ti->ti_seq && SEQ_LEQ(tp->snd_wl2,ti->ti_seq)) {
3945065Swnj 			tp->snd_wnd = ti->ti_win;
3955065Swnj 			tp->snd_wl1 = ti->ti_seq;
3965065Swnj 			tp->snd_wl2 = ti->ti_ack;
3974601Swnj 		}
3984601Swnj 
3994601Swnj 		switch (tp->t_state) {
4004601Swnj 
4015065Swnj 		/*
4025065Swnj 		 * In FIN_WAIT_1 STATE in addition to the processing
4035065Swnj 		 * for the ESTABLISHED state if our FIN is now acknowledged
4045085Swnj 		 * then enter FIN_WAIT_2.
4055065Swnj 		 */
4065065Swnj 		case TCPS_FIN_WAIT_1:
4075085Swnj 			if (ourfinisacked)
4085085Swnj 				tp->t_state = TCPS_FIN_WAIT_2;
4094601Swnj 			break;
4104601Swnj 
4115065Swnj 	 	/*
4125065Swnj 		 * In CLOSING STATE in addition to the processing for
4135065Swnj 		 * the ESTABLISHED state if the ACK acknowledges our FIN
4145065Swnj 		 * then enter the TIME-WAIT state, otherwise ignore
4155065Swnj 		 * the segment.
4165065Swnj 		 */
4175065Swnj 		case TCPS_CLOSING:
4185085Swnj 			if (ourfinisacked)
4195065Swnj 				tp->t_state = TCPS_TIME_WAIT;
4205085Swnj 			goto drop;
4214601Swnj 
4225065Swnj 		/*
4235085Swnj 		 * The only thing that can arrive in  LAST_ACK state
4245085Swnj 		 * is an acknowledgment of our FIN.  If our FIN is now
4255085Swnj 		 * acknowledged, delete the TCB, enter the closed state
4265085Swnj 		 * and return.
4275065Swnj 		 */
4285065Swnj 		case TCPS_LAST_ACK:
4295085Swnj 			if (ourfinisacked)
4305065Swnj 				tcp_close(tp);
4315065Swnj 			goto drop;
4324601Swnj 
4335065Swnj 		/*
4345065Swnj 		 * In TIME_WAIT state the only thing that should arrive
4355065Swnj 		 * is a retransmission of the remote FIN.  Acknowledge
4365065Swnj 		 * it and restart the finack timer.
4375065Swnj 		 */
4385065Swnj 		case TCPS_TIME_WAIT:
4395162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
4405065Swnj 			goto dropafterack;
4414601Swnj 		}
4425085Swnj #undef ourfinisacked
4435085Swnj 	}
4444601Swnj 
4455065Swnj step6:
4465065Swnj 	/*
4475065Swnj 	 * If an URG bit is set in the segment and is greater than the
4485065Swnj 	 * current known urgent pointer, then signal the user that the
4495065Swnj 	 * remote side has urgent data.  This should not happen
4505065Swnj 	 * in CLOSE_WAIT, CLOSING, LAST-ACK or TIME_WAIT STATES since
4515065Swnj 	 * a FIN has been received from the remote side.  In these states
4525065Swnj 	 * we ignore the URG.
4535065Swnj 	 */
4545085Swnj 	if ((tiflags & TH_URG) == 0 && TCPS_HAVERCVDFIN(tp->t_state) == 0)
4555085Swnj 		if (SEQ_GT(ti->ti_urp, tp->rcv_up)) {
4565065Swnj 			tp->rcv_up = ti->ti_urp;
4575085Swnj #if 0
4585065Swnj 			soisurgendata(so);		/* XXX */
4595085Swnj #endif
4604601Swnj 		}
4614601Swnj 
4624601Swnj 	/*
4635065Swnj 	 * Process the segment text, merging it into the TCP sequencing queue,
4645065Swnj 	 * and arranging for acknowledgment of receipt if necessary.
4655065Swnj 	 * This process logically involves adjusting tp->rcv_wnd as data
4665065Swnj 	 * is presented to the user (this happens in tcp_usrreq.c,
4675065Swnj 	 * case PRU_RCVD).  If a FIN has already been received on this
4685065Swnj 	 * connection then we just ignore the text.
4694601Swnj 	 */
4705065Swnj 	if (ti->ti_len) {
4715085Swnj 		if (TCPS_HAVERCVDFIN(tp->t_state))
4725065Swnj 			goto drop;
4735085Swnj 		off += sizeof (struct ip);		/* drop IP header */
4745085Swnj 		m->m_off += off;
4755085Swnj 		m->m_len -= off;
4765065Swnj 		tiflags = tcp_reass(tp, ti);
4775085Swnj 		tp->t_flags |= TF_ACKNOW;		/* XXX TF_DELACK */
4785085Swnj 	} else
4794924Swnj 		m_freem(m);
4804601Swnj 
4814601Swnj 	/*
4825065Swnj 	 * If FIN is received then if we haven't received SYN and
4835065Swnj 	 * therefore can't validate drop the segment.  Otherwise ACK
4845065Swnj 	 * the FIN and let the user know that the connection is closing.
4854601Swnj 	 */
4865085Swnj 	if ((tiflags & TH_FIN)) {
4875074Swnj 		if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
4885074Swnj 			goto drop;
4895074Swnj 		socantrcvmore(so);
4905065Swnj 		tp->t_flags |= TF_ACKNOW;
4915065Swnj 		tp->rcv_nxt++;
4925065Swnj 		switch (tp->t_state) {
4934601Swnj 
4945065Swnj 	 	/*
4955065Swnj 		 * In SYN_RECEIVED and ESTABLISHED STATES
4965065Swnj 		 * enter the CLOSE_WAIT state.
4974884Swnj 		 */
4985065Swnj 		case TCPS_SYN_RECEIVED:
4995065Swnj 		case TCPS_ESTABLISHED:
5005065Swnj 			tp->t_state = TCPS_CLOSE_WAIT;
5015065Swnj 			break;
5024884Swnj 
5035065Swnj 	 	/*
5045085Swnj 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
5055085Swnj 		 * enter the CLOSING state.
5064884Swnj 		 */
5075065Swnj 		case TCPS_FIN_WAIT_1:
5085085Swnj 			tp->t_state = TCPS_CLOSING;
5095065Swnj 			break;
5104601Swnj 
5115065Swnj 	 	/*
5125065Swnj 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
5135065Swnj 		 * starting the time-wait timer, turning off the other
5145065Swnj 		 * standard timers.
5155065Swnj 		 */
5165065Swnj 		case TCPS_FIN_WAIT_2:
5175085Swnj 			tp->t_state = TCPS_TIME_WAIT;;
5185074Swnj 			tcp_canceltimers(tp);
5195162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
5205065Swnj 			break;
5215065Swnj 
5224884Swnj 		/*
5235065Swnj 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
5244884Swnj 		 */
5255065Swnj 		case TCPS_TIME_WAIT:
5265162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
5275065Swnj 			break;
5285085Swnj 		}
5294601Swnj 	}
5305085Swnj 
5315085Swnj 	/*
5325085Swnj 	 * Return any desired output.
5335085Swnj 	 */
5345085Swnj 	tcp_output(tp);
5355065Swnj 	return;
5365085Swnj 
5375065Swnj dropafterack:
5385085Swnj 	/*
5395085Swnj 	 * Generate an ACK, then drop incoming segment.
5405085Swnj 	 * Make ACK reflect our state.
5415085Swnj 	 */
5425085Swnj 	if (tiflags & TH_RST)
5435085Swnj 		goto drop;
5445085Swnj 	tcp_respond(ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK);
545*5231Swnj 	return;
5465085Swnj 
5475085Swnj dropwithreset:
5485085Swnj 	/*
5495085Swnj 	 * Generate a RST, then drop incoming segment.
5505085Swnj 	 * Make ACK acceptable to originator of segment.
5515085Swnj 	 */
5525085Swnj 	if (tiflags & TH_RST)
5535085Swnj 		goto drop;
5545085Swnj 	if (tiflags & TH_ACK)
5555109Swnj 		tcp_respond(ti, (tcp_seq)0, ti->ti_ack, TH_RST);
5565085Swnj 	else {
5575085Swnj 		if (tiflags & TH_SYN)
5585085Swnj 			ti->ti_len++;
5595109Swnj 		tcp_respond(ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK);
5605085Swnj 	}
561*5231Swnj 	return;
5625085Swnj 
5635065Swnj drop:
5645085Swnj 	/*
5655085Swnj 	 * Drop space held by incoming segment and return.
5665085Swnj 	 */
5675065Swnj 	m_freem(m);
5685065Swnj }
5695065Swnj 
5705065Swnj /*
5715065Swnj  * Insert segment ti into reassembly queue of tcp with
5725065Swnj  * control block tp.  Return TH_FIN if reassembly now includes
5735065Swnj  * a segment with FIN.
5745065Swnj  */
5755109Swnj tcp_reass(tp, ti)
5765065Swnj 	register struct tcpcb *tp;
5775065Swnj 	register struct tcpiphdr *ti;
5785065Swnj {
5795065Swnj 	register struct tcpiphdr *q;
5805085Swnj 	struct socket *so = tp->t_inpcb->inp_socket;
5815065Swnj 	int flags = 0;		/* no FIN */
5825085Swnj COUNT(TCP_REASS);
5835065Swnj 
5845065Swnj 	/*
5855162Swnj 	 * Call with ti==0 after become established to
5865162Swnj 	 * force pre-ESTABLISHED data up to user socket.
5875065Swnj 	 */
5885162Swnj 	if (ti == 0)
5895065Swnj 		goto present;
5904601Swnj 
5915065Swnj 	/*
5925065Swnj 	 * Find a segment which begins after this one does.
5935065Swnj 	 */
5945065Swnj 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
5955065Swnj 	    q = (struct tcpiphdr *)q->ti_next)
5965065Swnj 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
5975065Swnj 			break;
5984601Swnj 
5995065Swnj 	/*
6005065Swnj 	 * If there is a preceding segment, it may provide some of
6015065Swnj 	 * our data already.  If so, drop the data from the incoming
6025065Swnj 	 * segment.  If it provides all of our data, drop us.
6035065Swnj 	 */
6045065Swnj 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
6055065Swnj 		register int i;
6065065Swnj 		q = (struct tcpiphdr *)(q->ti_prev);
6075065Swnj 		/* conversion to int (in i) handles seq wraparound */
6085065Swnj 		i = q->ti_seq + q->ti_len - ti->ti_seq;
6095065Swnj 		if (i > 0) {
6104924Swnj 			if (i >= ti->ti_len)
6115065Swnj 				goto drop;
6125065Swnj 			m_adj(dtom(tp), i);
6135065Swnj 			ti->ti_len -= i;
6144924Swnj 			ti->ti_seq += i;
6154601Swnj 		}
6165065Swnj 		q = (struct tcpiphdr *)(q->ti_next);
6175065Swnj 	}
6184601Swnj 
6195065Swnj 	/*
6205065Swnj 	 * While we overlap succeeding segments trim them or,
6215065Swnj 	 * if they are completely covered, dequeue them.
6225065Swnj 	 */
6235065Swnj 	while (q != (struct tcpiphdr *)tp &&
6245065Swnj 	    SEQ_GT(ti->ti_seq + ti->ti_len, q->ti_seq)) {
6255065Swnj 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
6265065Swnj 		if (i < q->ti_len) {
6275065Swnj 			q->ti_len -= i;
6285065Swnj 			m_adj(dtom(q), i);
6295065Swnj 			break;
6304601Swnj 		}
6315065Swnj 		q = (struct tcpiphdr *)q->ti_next;
6325065Swnj 		m_freem(dtom(q->ti_prev));
6335065Swnj 		remque(q->ti_prev);
6345065Swnj 	}
6354601Swnj 
6365065Swnj 	/*
6375065Swnj 	 * Stick new segment in its place.
6385065Swnj 	 */
6395065Swnj 	insque(ti, q->ti_prev);
6404601Swnj 
6415065Swnj 	/*
6425065Swnj 	 * Advance rcv_next through newly completed sequence space.
6435065Swnj 	 */
6445065Swnj 	while (ti->ti_seq == tp->rcv_nxt) {
6455065Swnj 		tp->rcv_nxt += ti->ti_len;
6465065Swnj 		flags = ti->ti_flags & TH_FIN;
6475065Swnj 		ti = (struct tcpiphdr *)ti->ti_next;
6485065Swnj 		if (ti == (struct tcpiphdr *)tp)
6495065Swnj 			break;
6504679Swnj 	}
6514679Swnj 
6525065Swnj present:
6535065Swnj 	/*
6545065Swnj 	 * Present data to user.
6555065Swnj 	 */
6565085Swnj 	if (tp->t_state < TCPS_ESTABLISHED)
6575065Swnj 		return (flags);
6584924Swnj 	ti = tp->seg_next;
6594924Swnj 	while (ti != (struct tcpiphdr *)tp && ti->ti_seq < tp->rcv_nxt) {
6604924Swnj 		remque(ti);
6614924Swnj 		sbappend(&so->so_rcv, dtom(ti));
6624924Swnj 		ti = (struct tcpiphdr *)ti->ti_next;
6634601Swnj 	}
6645074Swnj 	if (so->so_state & SS_CANTRCVMORE)
6655074Swnj 		sbflush(&so->so_rcv);
6665074Swnj 	else
6675074Swnj 		sorwakeup(so);
6685065Swnj 	return (flags);
6695065Swnj drop:
6705065Swnj 	m_freem(dtom(ti));
6715065Swnj 	return (flags);
6724601Swnj }
673