xref: /csrg-svn/sys/netinet/tcp_input.c (revision 5263)
1*5263Swnj /*	tcp_input.c	1.41	81/12/19	*/
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"
165244Sroot #define TCPSTATES
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"
225109Swnj #include "../errno.h"
234601Swnj 
244679Swnj int	tcpcksum = 1;
255244Sroot struct	sockaddr_in tcp_in = { AF_INET };
264601Swnj 
275065Swnj /*
285065Swnj  * TCP input routine, follows pages 65-76 of the
295065Swnj  * protocol specification dated September, 1981 very closely.
305065Swnj  */
314924Swnj tcp_input(m0)
324924Swnj 	struct mbuf *m0;
334601Swnj {
344924Swnj 	register struct tcpiphdr *ti;
354924Swnj 	struct inpcb *inp;
364924Swnj 	register struct mbuf *m;
374924Swnj 	int len, tlen, off;
384924Swnj 	register struct tcpcb *tp;
394924Swnj 	register int tiflags;
404803Swnj 	struct socket *so;
415109Swnj 	int todrop, acked;
424924Swnj 
434601Swnj COUNT(TCP_INPUT);
444924Swnj 	/*
455244Sroot 	 * Get IP and TCP header together in first mbuf.
465244Sroot 	 * Note: IP leaves IP header in first mbuf.
474924Swnj 	 */
484924Swnj 	m = m0;
495020Sroot 	ti = mtod(m, struct tcpiphdr *);
505244Sroot 	if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2))
515208Swnj 		ip_stripoptions((struct ip *)ti, (struct mbuf *)0);
525085Swnj 	if (m->m_len < sizeof (struct tcpiphdr)) {
535085Swnj 		if (m_pullup(m, sizeof (struct tcpiphdr)) == 0) {
545085Swnj 			tcpstat.tcps_hdrops++;
555085Swnj 			goto drop;
565085Swnj 		}
575085Swnj 		ti = mtod(m, struct tcpiphdr *);
585085Swnj 	}
594601Swnj 
604601Swnj 	/*
615244Sroot 	 * Checksum extended TCP header and data.
624601Swnj 	 */
634924Swnj 	tlen = ((struct ip *)ti)->ip_len;
644924Swnj 	len = sizeof (struct ip) + tlen;
654679Swnj 	if (tcpcksum) {
664924Swnj 		ti->ti_next = ti->ti_prev = 0;
674924Swnj 		ti->ti_x1 = 0;
685223Swnj 		ti->ti_len = (u_short)tlen;
695223Swnj #if vax
705223Swnj 		ti->ti_len = htons(ti->ti_len);
715223Swnj #endif
725231Swnj 		if (ti->ti_sum = in_cksum(m, len)) {
734924Swnj 			tcpstat.tcps_badsum++;
745065Swnj 			printf("tcp cksum %x\n", ti->ti_sum);
755085Swnj 			goto drop;
764601Swnj 		}
774601Swnj 	}
784601Swnj 
794601Swnj 	/*
805244Sroot 	 * Check that TCP offset makes sense,
815244Sroot 	 * process TCP options and adjust length.
824601Swnj 	 */
834924Swnj 	off = ti->ti_off << 2;
845231Swnj 	if (off < sizeof (struct tcphdr) || off > tlen) {
854924Swnj 		tcpstat.tcps_badoff++;
865085Swnj 		goto drop;
874924Swnj 	}
884924Swnj 	ti->ti_len = tlen - off;
895085Swnj #if 0
905231Swnj 	if (off > sizeof (struct tcphdr))
915085Swnj 		tcp_options(ti);
925085Swnj #endif
935065Swnj 	tiflags = ti->ti_flags;
944924Swnj 
955231Swnj #if vax
964924Swnj 	/*
975244Sroot 	 * Convert TCP protocol specific fields to host format.
985085Swnj 	 */
995085Swnj 	ti->ti_seq = ntohl(ti->ti_seq);
1005085Swnj 	ti->ti_ack = ntohl(ti->ti_ack);
1015085Swnj 	ti->ti_win = ntohs(ti->ti_win);
1025085Swnj 	ti->ti_urp = ntohs(ti->ti_urp);
1035231Swnj #endif
1045085Swnj 
1055085Swnj 	/*
1064924Swnj 	 * Locate pcb for segment.
1074924Swnj 	 */
1085065Swnj 	inp = in_pcblookup
1095065Swnj 		(&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport);
1105065Swnj 
1115065Swnj 	/*
1125065Swnj 	 * If the state is CLOSED (i.e., TCB does not exist) then
1135244Sroot 	 * all data in the incoming segment is discarded.
1145065Swnj 	 */
1154884Swnj 	if (inp == 0)
1165085Swnj 		goto dropwithreset;
1175065Swnj 	tp = intotcpcb(inp);
1185065Swnj 	if (tp == 0)
1195085Swnj 		goto dropwithreset;
1205109Swnj 	so = inp->inp_socket;
1214601Swnj 
1224601Swnj 	/*
1235162Swnj 	 * Segment received on connection.
1245162Swnj 	 * Reset idle time and keep-alive timer.
1255162Swnj 	 */
1265162Swnj 	tp->t_idle = 0;
1275162Swnj 	tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
1285162Swnj 
1295162Swnj 	/*
1305085Swnj 	 * Calculate amount of space in receive window,
1315085Swnj 	 * and then do TCP input processing.
1324601Swnj 	 */
1335085Swnj 	tp->rcv_wnd = sbspace(&so->so_rcv);
1345231Swnj 	if (tp->rcv_wnd < 0)
1355231Swnj 		tp->rcv_wnd = 0;
1364601Swnj 
1374601Swnj 	switch (tp->t_state) {
1384601Swnj 
1395065Swnj 	/*
1405065Swnj 	 * If the state is LISTEN then ignore segment if it contains an RST.
1415065Swnj 	 * If the segment contains an ACK then it is bad and send a RST.
1425065Swnj 	 * If it does not contain a SYN then it is not interesting; drop it.
1435085Swnj 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
1445065Swnj 	 * tp->iss, and send a segment:
1455085Swnj 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
1465065Swnj 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
1475065Swnj 	 * Fill in remote peer address fields if not previously specified.
1485065Swnj 	 * Enter SYN_RECEIVED state, and process any other fields of this
1495244Sroot 	 * segment in this state.
1505065Swnj 	 */
1515065Swnj 	case TCPS_LISTEN:
1525065Swnj 		if (tiflags & TH_RST)
1535065Swnj 			goto drop;
1545065Swnj 		if (tiflags & TH_ACK)
1555085Swnj 			goto dropwithreset;
1565065Swnj 		if ((tiflags & TH_SYN) == 0)
1575065Swnj 			goto drop;
1585244Sroot 		tcp_in.sin_addr = ti->ti_src;
1595244Sroot 		tcp_in.sin_port = ti->ti_sport;
1605244Sroot 		if (in_pcbconnect(inp, (struct sockaddr *)&tcp_in))
1615244Sroot 			goto drop;
1625244Sroot 		tp->t_template = tcp_template(tp);
1635244Sroot 		if (tp->t_template == 0) {
1645244Sroot 			in_pcbdisconnect(inp);
1655244Sroot 			goto drop;
1665244Sroot 		}
1675085Swnj 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
1685065Swnj 		tp->irs = ti->ti_seq;
1695085Swnj 		tcp_sendseqinit(tp);
1705085Swnj 		tcp_rcvseqinit(tp);
1715065Swnj 		tp->t_state = TCPS_SYN_RECEIVED;
1725244Sroot 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
1735085Swnj 		goto trimthenstep6;
1744601Swnj 
1755065Swnj 	/*
1765065Swnj 	 * If the state is SYN_SENT:
1775065Swnj 	 *	if seg contains an ACK, but not for our SYN, drop the input.
1785065Swnj 	 *	if seg contains a RST, then drop the connection.
1795065Swnj 	 *	if seg does not contain SYN, then drop it.
1805065Swnj 	 * Otherwise this is an acceptable SYN segment
1815065Swnj 	 *	initialize tp->rcv_nxt and tp->irs
1825065Swnj 	 *	if seg contains ack then advance tp->snd_una
1835065Swnj 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1845065Swnj 	 *	arrange for segment to be acked (eventually)
1855065Swnj 	 *	continue processing rest of data/controls, beginning with URG
1865065Swnj 	 */
1875065Swnj 	case TCPS_SYN_SENT:
1885065Swnj 		if ((tiflags & TH_ACK) &&
1895065Swnj 		    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
1905231Swnj 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
1915085Swnj 			goto dropwithreset;
1925065Swnj 		if (tiflags & TH_RST) {
1935065Swnj 			if (tiflags & TH_ACK)
1945085Swnj 				tcp_drop(tp, ECONNRESET);
1955065Swnj 			goto drop;
1964601Swnj 		}
1975065Swnj 		if ((tiflags & TH_SYN) == 0)
1985065Swnj 			goto drop;
1995231Swnj 		tp->snd_una = ti->ti_ack;
2005244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
2015065Swnj 		tp->irs = ti->ti_seq;
2025085Swnj 		tcp_rcvseqinit(tp);
2035085Swnj 		tp->t_flags |= TF_ACKNOW;
2045162Swnj 		if (SEQ_GT(tp->snd_una, tp->iss)) {
205*5263Swnj 			so->so_state |= SS_CONNAWAITING;
2065244Sroot 			soisconnected(so);
2075065Swnj 			tp->t_state = TCPS_ESTABLISHED;
2085162Swnj 			(void) tcp_reass(tp, (struct tcpiphdr *)0);
2095162Swnj 		} else
2105085Swnj 			tp->t_state = TCPS_SYN_RECEIVED;
2115085Swnj 		goto trimthenstep6;
2125085Swnj 
2135085Swnj trimthenstep6:
2145085Swnj 		/*
2155231Swnj 		 * Advance ti->ti_seq to correspond to first data byte.
2165085Swnj 		 * If data, trim to stay within window,
2175085Swnj 		 * dropping FIN if necessary.
2185085Swnj 		 */
2195231Swnj 		ti->ti_seq++;
2205085Swnj 		if (ti->ti_len > tp->rcv_wnd) {
2215085Swnj 			todrop = ti->ti_len - tp->rcv_wnd;
2225085Swnj 			m_adj(m, -todrop);
2235085Swnj 			ti->ti_len = tp->rcv_wnd;
2245085Swnj 			ti->ti_flags &= ~TH_FIN;
2255065Swnj 		}
226*5263Swnj 		tp->snd_wl1 = ti->ti_seq - 1;
2275085Swnj 		goto step6;
2285065Swnj 	}
2294601Swnj 
2305065Swnj 	/*
2315065Swnj 	 * States other than LISTEN or SYN_SENT.
2325065Swnj 	 * First check that at least some bytes of segment are within
2335065Swnj 	 * receive window.
2345065Swnj 	 */
2355065Swnj 	if (tp->rcv_wnd == 0) {
2365065Swnj 		/*
2375065Swnj 		 * If window is closed can only take segments at
2385231Swnj 		 * window edge, and have to drop data and PUSH from
2395065Swnj 		 * incoming segments.
2405065Swnj 		 */
2415065Swnj 		if (tp->rcv_nxt != ti->ti_seq)
2425065Swnj 			goto dropafterack;
2435085Swnj 		if (ti->ti_len > 0) {
2445085Swnj 			ti->ti_len = 0;
2455085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
2465065Swnj 		}
2475065Swnj 	} else {
2485065Swnj 		/*
2495231Swnj 		 * If segment begins before rcv_nxt, drop leading
2505065Swnj 		 * data (and SYN); if nothing left, just ack.
2515065Swnj 		 */
2525065Swnj 		if (SEQ_GT(tp->rcv_nxt, ti->ti_seq)) {
2535085Swnj 			todrop = tp->rcv_nxt - ti->ti_seq;
2545085Swnj 			if (tiflags & TH_SYN) {
2555085Swnj 				ti->ti_seq++;
2565085Swnj 				if (ti->ti_urp > 1)
2575085Swnj 					ti->ti_urp--;
2585085Swnj 				else
2595085Swnj 					tiflags &= ~TH_URG;
2605085Swnj 				todrop--;
2615085Swnj 			}
2625065Swnj 			if (todrop > ti->ti_len)
2635065Swnj 				goto dropafterack;
2645065Swnj 			m_adj(m, todrop);
2655065Swnj 			ti->ti_seq += todrop;
2665065Swnj 			ti->ti_len -= todrop;
2675085Swnj 			if (ti->ti_urp > todrop)
2685085Swnj 				ti->ti_urp -= todrop;
2695085Swnj 			else {
2705085Swnj 				tiflags &= ~TH_URG;
2715085Swnj 				/* ti->ti_flags &= ~TH_URG; */
2725085Swnj 				/* ti->ti_urp = 0; */
2735085Swnj 			}
2745085Swnj 			/* tiflags &= ~TH_SYN; */
2755085Swnj 			/* ti->ti_flags &= ~TH_SYN; */
2765065Swnj 		}
2775065Swnj 		/*
2785065Swnj 		 * If segment ends after window, drop trailing data
2795085Swnj 		 * (and PUSH and FIN); if nothing left, just ACK.
2805065Swnj 		 */
2815065Swnj 		if (SEQ_GT(ti->ti_seq+ti->ti_len, tp->rcv_nxt+tp->rcv_wnd)) {
2825085Swnj 			todrop =
2835065Swnj 			     ti->ti_seq+ti->ti_len - (tp->rcv_nxt+tp->rcv_wnd);
2845065Swnj 			if (todrop > ti->ti_len)
2855065Swnj 				goto dropafterack;
2865065Swnj 			m_adj(m, -todrop);
2875065Swnj 			ti->ti_len -= todrop;
2885085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
2895065Swnj 		}
2905065Swnj 	}
2914601Swnj 
2925065Swnj 	/*
2935065Swnj 	 * If the RST bit is set examine the state:
2945065Swnj 	 *    SYN_RECEIVED STATE:
2955065Swnj 	 *	If passive open, return to LISTEN state.
2965065Swnj 	 *	If active open, inform user that connection was refused.
2975065Swnj 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
2985065Swnj 	 *	Inform user that connection was reset, and close tcb.
2995065Swnj 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
3005065Swnj 	 *	Close the tcb.
3015065Swnj 	 */
3025065Swnj 	if (tiflags&TH_RST) switch (tp->t_state) {
3035065Swnj 
3045065Swnj 	case TCPS_SYN_RECEIVED:
3055065Swnj 		if (inp->inp_socket->so_options & SO_ACCEPTCONN) {
3065085Swnj 			tp->t_state = TCPS_LISTEN;
3075244Sroot 			tp->t_timer[TCPT_KEEP] = 0;
3085244Sroot 			(void) m_free(dtom(tp->t_template));
3095244Sroot 			tp->t_template = 0;
3105231Swnj 			in_pcbdisconnect(inp);
3115065Swnj 			goto drop;
3124601Swnj 		}
3135085Swnj 		tcp_drop(tp, ECONNREFUSED);
3145065Swnj 		goto drop;
3154601Swnj 
3165065Swnj 	case TCPS_ESTABLISHED:
3175065Swnj 	case TCPS_FIN_WAIT_1:
3185065Swnj 	case TCPS_FIN_WAIT_2:
3195065Swnj 	case TCPS_CLOSE_WAIT:
3205065Swnj 		tcp_drop(tp, ECONNRESET);
3215065Swnj 		goto drop;
3225065Swnj 
3235065Swnj 	case TCPS_CLOSING:
3245065Swnj 	case TCPS_LAST_ACK:
3255065Swnj 	case TCPS_TIME_WAIT:
3265065Swnj 		tcp_close(tp);
3275065Swnj 		goto drop;
3284601Swnj 	}
3294601Swnj 
3304601Swnj 	/*
3315065Swnj 	 * If a SYN is in the window, then this is an
3325065Swnj 	 * error and we send an RST and drop the connection.
3334601Swnj 	 */
3345065Swnj 	if (tiflags & TH_SYN) {
3355231Swnj 		tcp_drop(tp, ECONNRESET);
3365085Swnj 		goto dropwithreset;
3374601Swnj 	}
3384601Swnj 
3394601Swnj 	/*
3405065Swnj 	 * If the ACK bit is off we drop the segment and return.
3414601Swnj 	 */
3425085Swnj 	if ((tiflags & TH_ACK) == 0)
3435065Swnj 		goto drop;
3445065Swnj 
3455065Swnj 	/*
3465065Swnj 	 * Ack processing.
3475065Swnj 	 */
3484601Swnj 	switch (tp->t_state) {
3494601Swnj 
3505065Swnj 	/*
3515065Swnj 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
3525065Swnj 	 * ESTABLISHED state and continue processing, othewise
3535065Swnj 	 * send an RST.
3545065Swnj 	 */
3555065Swnj 	case TCPS_SYN_RECEIVED:
3565085Swnj 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
3575231Swnj 		    SEQ_GT(ti->ti_ack, tp->snd_max))
3585085Swnj 			goto dropwithreset;
3595244Sroot 		tp->snd_una++;			/* SYN acked */
3605244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
361*5263Swnj 		so->so_state |= SS_CONNAWAITING;
3625085Swnj 		soisconnected(so);
3635085Swnj 		tp->t_state = TCPS_ESTABLISHED;
3645162Swnj 		(void) tcp_reass(tp, (struct tcpiphdr *)0);
3655244Sroot 		tp->snd_wl1 = ti->ti_seq - 1;
3665085Swnj 		/* fall into ... */
3674601Swnj 
3685065Swnj 	/*
3695065Swnj 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
3705065Swnj 	 * ACKs.  If the ack is in the range
3715231Swnj 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
3725065Swnj 	 * then advance tp->snd_una to ti->ti_ack and drop
3735065Swnj 	 * data from the retransmission queue.  If this ACK reflects
3745065Swnj 	 * more up to date window information we update our window information.
3755065Swnj 	 */
3765065Swnj 	case TCPS_ESTABLISHED:
3775065Swnj 	case TCPS_FIN_WAIT_1:
3785065Swnj 	case TCPS_FIN_WAIT_2:
3795065Swnj 	case TCPS_CLOSE_WAIT:
3805065Swnj 	case TCPS_CLOSING:
3815244Sroot 	case TCPS_LAST_ACK:
3825244Sroot 	case TCPS_TIME_WAIT:
3835085Swnj #define	ourfinisacked	(acked > 0)
3845085Swnj 
3855244Sroot 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una))
3865065Swnj 			break;
3875231Swnj 		if (SEQ_GT(ti->ti_ack, tp->snd_max))
3885065Swnj 			goto dropafterack;
3895085Swnj 		acked = ti->ti_ack - tp->snd_una;
3905244Sroot 		if (acked >= so->so_snd.sb_cc) {
3915085Swnj 			acked -= so->so_snd.sb_cc;
3925244Sroot 			/* if acked > 0 our FIN is acked */
393*5263Swnj if (so->so_snd.sb_cc)
3945244Sroot 			sbdrop(&so->so_snd, so->so_snd.sb_cc);
3955244Sroot 			tp->t_timer[TCPT_REXMT] = 0;
3965085Swnj 		} else {
397*5263Swnj if (acked)
3985085Swnj 			sbdrop(&so->so_snd, acked);
3995085Swnj 			acked = 0;
4005244Sroot 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
4015244Sroot 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
4025085Swnj 		}
4035231Swnj 		tp->snd_una = ti->ti_ack;
4045162Swnj 
4055162Swnj 		/*
4065162Swnj 		 * If transmit timer is running and timed sequence
4075162Swnj 		 * number was acked, update smoothed round trip time.
4085162Swnj 		 */
4095162Swnj 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
4105244Sroot 			if (tp->t_srtt == 0)
4115244Sroot 				tp->t_srtt = tp->t_rtt;
4125244Sroot 			else
4135244Sroot 				tp->t_srtt =
4145244Sroot 				    tcp_alpha * tp->t_srtt +
4155244Sroot 				    (1 - tcp_alpha) * tp->t_rtt;
4165162Swnj 			tp->t_rtt = 0;
4175162Swnj 		}
4185162Swnj 
4194601Swnj 		switch (tp->t_state) {
4204601Swnj 
4215065Swnj 		/*
4225065Swnj 		 * In FIN_WAIT_1 STATE in addition to the processing
4235065Swnj 		 * for the ESTABLISHED state if our FIN is now acknowledged
4245085Swnj 		 * then enter FIN_WAIT_2.
4255065Swnj 		 */
4265065Swnj 		case TCPS_FIN_WAIT_1:
4275085Swnj 			if (ourfinisacked)
4285085Swnj 				tp->t_state = TCPS_FIN_WAIT_2;
4294601Swnj 			break;
4304601Swnj 
4315065Swnj 	 	/*
4325065Swnj 		 * In CLOSING STATE in addition to the processing for
4335065Swnj 		 * the ESTABLISHED state if the ACK acknowledges our FIN
4345065Swnj 		 * then enter the TIME-WAIT state, otherwise ignore
4355065Swnj 		 * the segment.
4365065Swnj 		 */
4375065Swnj 		case TCPS_CLOSING:
4385244Sroot 			if (ourfinisacked) {
4395065Swnj 				tp->t_state = TCPS_TIME_WAIT;
4405244Sroot 				tcp_canceltimers(tp);
4415244Sroot 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
4425244Sroot 				soisdisconnected(so);
4435244Sroot 			}
4445244Sroot 			break;
4454601Swnj 
4465065Swnj 		/*
4475085Swnj 		 * The only thing that can arrive in  LAST_ACK state
4485085Swnj 		 * is an acknowledgment of our FIN.  If our FIN is now
4495085Swnj 		 * acknowledged, delete the TCB, enter the closed state
4505085Swnj 		 * and return.
4515065Swnj 		 */
4525065Swnj 		case TCPS_LAST_ACK:
4535251Sroot 			if (ourfinisacked)
4545065Swnj 				tcp_close(tp);
4555065Swnj 			goto drop;
4564601Swnj 
4575065Swnj 		/*
4585065Swnj 		 * In TIME_WAIT state the only thing that should arrive
4595065Swnj 		 * is a retransmission of the remote FIN.  Acknowledge
4605065Swnj 		 * it and restart the finack timer.
4615065Swnj 		 */
4625065Swnj 		case TCPS_TIME_WAIT:
4635162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
4645065Swnj 			goto dropafterack;
4654601Swnj 		}
4665085Swnj #undef ourfinisacked
4675085Swnj 	}
4684601Swnj 
4695065Swnj step6:
4705065Swnj 	/*
4715244Sroot 	 * Update window information.
4725244Sroot 	 */
4735244Sroot 	if (SEQ_LT(tp->snd_wl1, ti->ti_seq) ||
4745244Sroot 	    tp->snd_wl1==ti->ti_seq && SEQ_LEQ(tp->snd_wl2,ti->ti_seq)) {
4755244Sroot 		tp->snd_wnd = ti->ti_win;
4765244Sroot 		tp->snd_wl1 = ti->ti_seq;
4775244Sroot 		tp->snd_wl2 = ti->ti_ack;
4785244Sroot 		if (tp->snd_wnd > 0)
4795244Sroot 			tp->t_timer[TCPT_PERSIST] = 0;
4805244Sroot 	}
4815244Sroot 
4825244Sroot 	/*
4835065Swnj 	 * If an URG bit is set in the segment and is greater than the
4845065Swnj 	 * current known urgent pointer, then signal the user that the
4855244Sroot 	 * remote side has out of band data.  This should not happen
4865065Swnj 	 * in CLOSE_WAIT, CLOSING, LAST-ACK or TIME_WAIT STATES since
4875065Swnj 	 * a FIN has been received from the remote side.  In these states
4885065Swnj 	 * we ignore the URG.
4895065Swnj 	 */
4905085Swnj 	if ((tiflags & TH_URG) == 0 && TCPS_HAVERCVDFIN(tp->t_state) == 0)
4915085Swnj 		if (SEQ_GT(ti->ti_urp, tp->rcv_up)) {
4925065Swnj 			tp->rcv_up = ti->ti_urp;
4935085Swnj #if 0
4945244Sroot 			sohasoutofband(so);		/* XXX */
4955085Swnj #endif
4964601Swnj 		}
4974601Swnj 
4984601Swnj 	/*
4995065Swnj 	 * Process the segment text, merging it into the TCP sequencing queue,
5005065Swnj 	 * and arranging for acknowledgment of receipt if necessary.
5015065Swnj 	 * This process logically involves adjusting tp->rcv_wnd as data
5025065Swnj 	 * is presented to the user (this happens in tcp_usrreq.c,
5035065Swnj 	 * case PRU_RCVD).  If a FIN has already been received on this
5045065Swnj 	 * connection then we just ignore the text.
5054601Swnj 	 */
506*5263Swnj 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
507*5263Swnj 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
5085085Swnj 		off += sizeof (struct ip);		/* drop IP header */
5095085Swnj 		m->m_off += off;
5105085Swnj 		m->m_len -= off;
5115065Swnj 		tiflags = tcp_reass(tp, ti);
5125085Swnj 		tp->t_flags |= TF_ACKNOW;		/* XXX TF_DELACK */
5135244Sroot 	} else {
5144924Swnj 		m_freem(m);
515*5263Swnj 		tiflags &= ~TH_FIN;
5165244Sroot 	}
5174601Swnj 
5184601Swnj 	/*
519*5263Swnj 	 * If FIN is received ACK the FIN and let the user know
520*5263Swnj 	 * that the connection is closing.
5214601Swnj 	 */
522*5263Swnj 	if (tiflags & TH_FIN) {
5235244Sroot 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
5245244Sroot 			socantrcvmore(so);
5255244Sroot 			tp->t_flags |= TF_ACKNOW;
5265244Sroot 			tp->rcv_nxt++;
5275244Sroot 		}
5285065Swnj 		switch (tp->t_state) {
5294601Swnj 
5305065Swnj 	 	/*
5315065Swnj 		 * In SYN_RECEIVED and ESTABLISHED STATES
5325065Swnj 		 * enter the CLOSE_WAIT state.
5334884Swnj 		 */
5345065Swnj 		case TCPS_SYN_RECEIVED:
5355065Swnj 		case TCPS_ESTABLISHED:
5365065Swnj 			tp->t_state = TCPS_CLOSE_WAIT;
5375065Swnj 			break;
5384884Swnj 
5395065Swnj 	 	/*
5405085Swnj 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
5415085Swnj 		 * enter the CLOSING state.
5424884Swnj 		 */
5435065Swnj 		case TCPS_FIN_WAIT_1:
5445085Swnj 			tp->t_state = TCPS_CLOSING;
5455065Swnj 			break;
5464601Swnj 
5475065Swnj 	 	/*
5485065Swnj 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
5495065Swnj 		 * starting the time-wait timer, turning off the other
5505065Swnj 		 * standard timers.
5515065Swnj 		 */
5525065Swnj 		case TCPS_FIN_WAIT_2:
5535244Sroot 			tp->t_state = TCPS_TIME_WAIT;
5545074Swnj 			tcp_canceltimers(tp);
5555162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
5565244Sroot 			soisdisconnected(so);
5575065Swnj 			break;
5585065Swnj 
5594884Swnj 		/*
5605065Swnj 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
5614884Swnj 		 */
5625065Swnj 		case TCPS_TIME_WAIT:
5635162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
5645065Swnj 			break;
5655085Swnj 		}
5664601Swnj 	}
5675085Swnj 
5685085Swnj 	/*
5695085Swnj 	 * Return any desired output.
5705085Swnj 	 */
5715085Swnj 	tcp_output(tp);
5725065Swnj 	return;
5735085Swnj 
5745065Swnj dropafterack:
5755085Swnj 	/*
5765244Sroot 	 * Generate an ACK dropping incoming segment.
5775085Swnj 	 * Make ACK reflect our state.
5785085Swnj 	 */
5795085Swnj 	if (tiflags & TH_RST)
5805085Swnj 		goto drop;
5815085Swnj 	tcp_respond(ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK);
5825231Swnj 	return;
5835085Swnj 
5845085Swnj dropwithreset:
5855085Swnj 	/*
5865244Sroot 	 * Generate a RST, dropping incoming segment.
5875085Swnj 	 * Make ACK acceptable to originator of segment.
5885085Swnj 	 */
5895085Swnj 	if (tiflags & TH_RST)
5905085Swnj 		goto drop;
5915085Swnj 	if (tiflags & TH_ACK)
5925109Swnj 		tcp_respond(ti, (tcp_seq)0, ti->ti_ack, TH_RST);
5935085Swnj 	else {
5945085Swnj 		if (tiflags & TH_SYN)
5955085Swnj 			ti->ti_len++;
5965109Swnj 		tcp_respond(ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK);
5975085Swnj 	}
5985231Swnj 	return;
5995085Swnj 
6005065Swnj drop:
6015085Swnj 	/*
6025085Swnj 	 * Drop space held by incoming segment and return.
6035085Swnj 	 */
6045065Swnj 	m_freem(m);
6055065Swnj }
6065065Swnj 
6075065Swnj /*
6085065Swnj  * Insert segment ti into reassembly queue of tcp with
6095065Swnj  * control block tp.  Return TH_FIN if reassembly now includes
6105065Swnj  * a segment with FIN.
6115065Swnj  */
6125109Swnj tcp_reass(tp, ti)
6135065Swnj 	register struct tcpcb *tp;
6145065Swnj 	register struct tcpiphdr *ti;
6155065Swnj {
6165065Swnj 	register struct tcpiphdr *q;
6175085Swnj 	struct socket *so = tp->t_inpcb->inp_socket;
618*5263Swnj 	struct mbuf *m;
619*5263Swnj 	int flags;
6205085Swnj COUNT(TCP_REASS);
6215065Swnj 
6225065Swnj 	/*
6235162Swnj 	 * Call with ti==0 after become established to
6245162Swnj 	 * force pre-ESTABLISHED data up to user socket.
6255065Swnj 	 */
6265162Swnj 	if (ti == 0)
6275065Swnj 		goto present;
6284601Swnj 
6295065Swnj 	/*
6305065Swnj 	 * Find a segment which begins after this one does.
6315065Swnj 	 */
6325065Swnj 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
6335065Swnj 	    q = (struct tcpiphdr *)q->ti_next)
6345065Swnj 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
6355065Swnj 			break;
6364601Swnj 
6375065Swnj 	/*
6385065Swnj 	 * If there is a preceding segment, it may provide some of
6395065Swnj 	 * our data already.  If so, drop the data from the incoming
6405065Swnj 	 * segment.  If it provides all of our data, drop us.
6415065Swnj 	 */
6425065Swnj 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
6435065Swnj 		register int i;
6445065Swnj 		q = (struct tcpiphdr *)(q->ti_prev);
6455065Swnj 		/* conversion to int (in i) handles seq wraparound */
6465065Swnj 		i = q->ti_seq + q->ti_len - ti->ti_seq;
6475065Swnj 		if (i > 0) {
6484924Swnj 			if (i >= ti->ti_len)
6495065Swnj 				goto drop;
6505065Swnj 			m_adj(dtom(tp), i);
6515065Swnj 			ti->ti_len -= i;
6524924Swnj 			ti->ti_seq += i;
6534601Swnj 		}
6545065Swnj 		q = (struct tcpiphdr *)(q->ti_next);
6555065Swnj 	}
6564601Swnj 
6575065Swnj 	/*
6585065Swnj 	 * While we overlap succeeding segments trim them or,
6595065Swnj 	 * if they are completely covered, dequeue them.
6605065Swnj 	 */
6615065Swnj 	while (q != (struct tcpiphdr *)tp &&
6625065Swnj 	    SEQ_GT(ti->ti_seq + ti->ti_len, q->ti_seq)) {
6635065Swnj 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
6645065Swnj 		if (i < q->ti_len) {
6655065Swnj 			q->ti_len -= i;
6665065Swnj 			m_adj(dtom(q), i);
6675065Swnj 			break;
6684601Swnj 		}
6695065Swnj 		q = (struct tcpiphdr *)q->ti_next;
6705065Swnj 		m_freem(dtom(q->ti_prev));
6715065Swnj 		remque(q->ti_prev);
6725065Swnj 	}
6734601Swnj 
6745065Swnj 	/*
6755065Swnj 	 * Stick new segment in its place.
6765065Swnj 	 */
6775065Swnj 	insque(ti, q->ti_prev);
6784601Swnj 
6795065Swnj present:
6805065Swnj 	/*
6815244Sroot 	 * Present data to user, advancing rcv_nxt through
6825244Sroot 	 * completed sequence space.
6835065Swnj 	 */
684*5263Swnj 	if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
6855244Sroot 		return (0);
6864924Swnj 	ti = tp->seg_next;
687*5263Swnj 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
688*5263Swnj 		return (0);
689*5263Swnj 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
690*5263Swnj 		return (0);
691*5263Swnj 	do {
6925244Sroot 		tp->rcv_nxt += ti->ti_len;
6935244Sroot 		flags = ti->ti_flags & TH_FIN;
6944924Swnj 		remque(ti);
695*5263Swnj 		m = dtom(ti);
6964924Swnj 		ti = (struct tcpiphdr *)ti->ti_next;
697*5263Swnj 		if (so->so_state & SS_CANTRCVMORE)
698*5263Swnj 			(void) m_freem(m);
699*5263Swnj 		else
700*5263Swnj 			sbappend(&so->so_rcv, m);
701*5263Swnj 	} while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
702*5263Swnj 	sorwakeup(so);
7035065Swnj 	return (flags);
7045065Swnj drop:
7055065Swnj 	m_freem(dtom(ti));
706*5263Swnj 	return (0);
7074601Swnj }
708