xref: /csrg-svn/sys/netinet/tcp_input.c (revision 5287)
1*5287Sroot /*	tcp_input.c	1.43	81/12/21	*/
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 
244679Swnj int	tcpcksum = 1;
255244Sroot struct	sockaddr_in tcp_in = { AF_INET };
265267Sroot struct	tcpiphdr tcp_saveti;
274601Swnj 
285267Sroot struct	tcpcb *tcp_newtcpcb();
295065Swnj /*
305065Swnj  * TCP input routine, follows pages 65-76 of the
315065Swnj  * protocol specification dated September, 1981 very closely.
325065Swnj  */
334924Swnj tcp_input(m0)
344924Swnj 	struct mbuf *m0;
354601Swnj {
364924Swnj 	register struct tcpiphdr *ti;
374924Swnj 	struct inpcb *inp;
384924Swnj 	register struct mbuf *m;
394924Swnj 	int len, tlen, off;
404924Swnj 	register struct tcpcb *tp;
414924Swnj 	register int tiflags;
424803Swnj 	struct socket *so;
435109Swnj 	int todrop, acked;
445267Sroot 	short ostate;
454924Swnj 
464601Swnj COUNT(TCP_INPUT);
474924Swnj 	/*
485244Sroot 	 * Get IP and TCP header together in first mbuf.
495244Sroot 	 * Note: IP leaves IP header in first mbuf.
504924Swnj 	 */
514924Swnj 	m = m0;
525020Sroot 	ti = mtod(m, struct tcpiphdr *);
535244Sroot 	if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2))
545208Swnj 		ip_stripoptions((struct ip *)ti, (struct mbuf *)0);
555085Swnj 	if (m->m_len < sizeof (struct tcpiphdr)) {
565085Swnj 		if (m_pullup(m, sizeof (struct tcpiphdr)) == 0) {
575085Swnj 			tcpstat.tcps_hdrops++;
585085Swnj 			goto drop;
595085Swnj 		}
605085Swnj 		ti = mtod(m, struct tcpiphdr *);
615085Swnj 	}
624601Swnj 
634601Swnj 	/*
645244Sroot 	 * Checksum extended TCP header and data.
654601Swnj 	 */
664924Swnj 	tlen = ((struct ip *)ti)->ip_len;
674924Swnj 	len = sizeof (struct ip) + tlen;
684679Swnj 	if (tcpcksum) {
694924Swnj 		ti->ti_next = ti->ti_prev = 0;
704924Swnj 		ti->ti_x1 = 0;
715223Swnj 		ti->ti_len = (u_short)tlen;
725223Swnj #if vax
735223Swnj 		ti->ti_len = htons(ti->ti_len);
745223Swnj #endif
755231Swnj 		if (ti->ti_sum = in_cksum(m, len)) {
764924Swnj 			tcpstat.tcps_badsum++;
775065Swnj 			printf("tcp cksum %x\n", ti->ti_sum);
785085Swnj 			goto drop;
794601Swnj 		}
804601Swnj 	}
814601Swnj 
824601Swnj 	/*
835244Sroot 	 * Check that TCP offset makes sense,
845244Sroot 	 * process TCP options and adjust length.
854601Swnj 	 */
864924Swnj 	off = ti->ti_off << 2;
875231Swnj 	if (off < sizeof (struct tcphdr) || off > tlen) {
884924Swnj 		tcpstat.tcps_badoff++;
895085Swnj 		goto drop;
904924Swnj 	}
914924Swnj 	ti->ti_len = tlen - off;
925085Swnj #if 0
935231Swnj 	if (off > sizeof (struct tcphdr))
945085Swnj 		tcp_options(ti);
955085Swnj #endif
965065Swnj 	tiflags = ti->ti_flags;
974924Swnj 
985231Swnj #if vax
994924Swnj 	/*
1005244Sroot 	 * Convert TCP protocol specific fields to host format.
1015085Swnj 	 */
1025085Swnj 	ti->ti_seq = ntohl(ti->ti_seq);
1035085Swnj 	ti->ti_ack = ntohl(ti->ti_ack);
1045085Swnj 	ti->ti_win = ntohs(ti->ti_win);
1055085Swnj 	ti->ti_urp = ntohs(ti->ti_urp);
1065231Swnj #endif
1075085Swnj 
1085085Swnj 	/*
1094924Swnj 	 * Locate pcb for segment.
1104924Swnj 	 */
1115065Swnj 	inp = in_pcblookup
1125065Swnj 		(&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport);
1135065Swnj 
1145065Swnj 	/*
1155065Swnj 	 * If the state is CLOSED (i.e., TCB does not exist) then
1165244Sroot 	 * all data in the incoming segment is discarded.
1175065Swnj 	 */
118*5287Sroot 	if (inp == 0) {
119*5287Sroot printf("cant find inp\n");
1205085Swnj 		goto dropwithreset;
121*5287Sroot 	}
1225065Swnj 	tp = intotcpcb(inp);
123*5287Sroot 	if (tp == 0) {
124*5287Sroot printf("tp is 0\n");
1255085Swnj 		goto dropwithreset;
126*5287Sroot 	}
1275109Swnj 	so = inp->inp_socket;
1285267Sroot 	if (so->so_options & SO_DEBUG) {
1295267Sroot 		ostate = tp->t_state;
1305267Sroot 		tcp_saveti = *ti;
1315267Sroot 	}
1324601Swnj 
1334601Swnj 	/*
1345162Swnj 	 * Segment received on connection.
1355162Swnj 	 * Reset idle time and keep-alive timer.
1365162Swnj 	 */
1375162Swnj 	tp->t_idle = 0;
1385162Swnj 	tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
1395162Swnj 
1405162Swnj 	/*
1415085Swnj 	 * Calculate amount of space in receive window,
1425085Swnj 	 * and then do TCP input processing.
1434601Swnj 	 */
1445085Swnj 	tp->rcv_wnd = sbspace(&so->so_rcv);
1455231Swnj 	if (tp->rcv_wnd < 0)
1465231Swnj 		tp->rcv_wnd = 0;
1474601Swnj 
1484601Swnj 	switch (tp->t_state) {
1494601Swnj 
1505065Swnj 	/*
1515065Swnj 	 * If the state is LISTEN then ignore segment if it contains an RST.
1525065Swnj 	 * If the segment contains an ACK then it is bad and send a RST.
1535065Swnj 	 * If it does not contain a SYN then it is not interesting; drop it.
1545085Swnj 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
1555065Swnj 	 * tp->iss, and send a segment:
1565085Swnj 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
1575065Swnj 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
1585065Swnj 	 * Fill in remote peer address fields if not previously specified.
1595065Swnj 	 * Enter SYN_RECEIVED state, and process any other fields of this
1605244Sroot 	 * segment in this state.
1615065Swnj 	 */
1625065Swnj 	case TCPS_LISTEN:
1635065Swnj 		if (tiflags & TH_RST)
1645065Swnj 			goto drop;
165*5287Sroot 		if (tiflags & TH_ACK) {
166*5287Sroot printf("contains ACK\n");
1675085Swnj 			goto dropwithreset;
168*5287Sroot }
169*5287Sroot 		if ((tiflags & TH_SYN) == 0) {
170*5287Sroot printf("no syn\n");
1715065Swnj 			goto drop;
172*5287Sroot }
1735244Sroot 		tcp_in.sin_addr = ti->ti_src;
1745244Sroot 		tcp_in.sin_port = ti->ti_sport;
175*5287Sroot 		if (in_pcbconnect(inp, (struct sockaddr *)&tcp_in)) {
176*5287Sroot printf("pcb cant connect\n");
1775244Sroot 			goto drop;
178*5287Sroot }
1795244Sroot 		tp->t_template = tcp_template(tp);
1805244Sroot 		if (tp->t_template == 0) {
181*5287Sroot printf("can't get template\n");
1825244Sroot 			in_pcbdisconnect(inp);
1835244Sroot 			goto drop;
1845244Sroot 		}
1855085Swnj 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
1865065Swnj 		tp->irs = ti->ti_seq;
1875085Swnj 		tcp_sendseqinit(tp);
1885085Swnj 		tcp_rcvseqinit(tp);
1895065Swnj 		tp->t_state = TCPS_SYN_RECEIVED;
1905244Sroot 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
1915085Swnj 		goto trimthenstep6;
1924601Swnj 
1935065Swnj 	/*
1945065Swnj 	 * If the state is SYN_SENT:
1955065Swnj 	 *	if seg contains an ACK, but not for our SYN, drop the input.
1965065Swnj 	 *	if seg contains a RST, then drop the connection.
1975065Swnj 	 *	if seg does not contain SYN, then drop it.
1985065Swnj 	 * Otherwise this is an acceptable SYN segment
1995065Swnj 	 *	initialize tp->rcv_nxt and tp->irs
2005065Swnj 	 *	if seg contains ack then advance tp->snd_una
2015065Swnj 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
2025065Swnj 	 *	arrange for segment to be acked (eventually)
2035065Swnj 	 *	continue processing rest of data/controls, beginning with URG
2045065Swnj 	 */
2055065Swnj 	case TCPS_SYN_SENT:
2065065Swnj 		if ((tiflags & TH_ACK) &&
2075065Swnj 		    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
2085231Swnj 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
2095085Swnj 			goto dropwithreset;
2105065Swnj 		if (tiflags & TH_RST) {
2115065Swnj 			if (tiflags & TH_ACK)
2125267Sroot 				tcp_drop(tp, ECONNREFUSED);
2135065Swnj 			goto drop;
2144601Swnj 		}
2155065Swnj 		if ((tiflags & TH_SYN) == 0)
2165065Swnj 			goto drop;
2175231Swnj 		tp->snd_una = ti->ti_ack;
2185244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
2195065Swnj 		tp->irs = ti->ti_seq;
2205085Swnj 		tcp_rcvseqinit(tp);
2215085Swnj 		tp->t_flags |= TF_ACKNOW;
2225162Swnj 		if (SEQ_GT(tp->snd_una, tp->iss)) {
2235263Swnj 			so->so_state |= SS_CONNAWAITING;
2245244Sroot 			soisconnected(so);
2255065Swnj 			tp->t_state = TCPS_ESTABLISHED;
2265162Swnj 			(void) tcp_reass(tp, (struct tcpiphdr *)0);
2275162Swnj 		} else
2285085Swnj 			tp->t_state = TCPS_SYN_RECEIVED;
2295085Swnj 		goto trimthenstep6;
2305085Swnj 
2315085Swnj trimthenstep6:
2325085Swnj 		/*
2335231Swnj 		 * Advance ti->ti_seq to correspond to first data byte.
2345085Swnj 		 * If data, trim to stay within window,
2355085Swnj 		 * dropping FIN if necessary.
2365085Swnj 		 */
2375231Swnj 		ti->ti_seq++;
2385085Swnj 		if (ti->ti_len > tp->rcv_wnd) {
2395085Swnj 			todrop = ti->ti_len - tp->rcv_wnd;
2405085Swnj 			m_adj(m, -todrop);
2415085Swnj 			ti->ti_len = tp->rcv_wnd;
2425085Swnj 			ti->ti_flags &= ~TH_FIN;
2435065Swnj 		}
2445263Swnj 		tp->snd_wl1 = ti->ti_seq - 1;
2455085Swnj 		goto step6;
2465065Swnj 	}
2474601Swnj 
2485065Swnj 	/*
2495065Swnj 	 * States other than LISTEN or SYN_SENT.
2505065Swnj 	 * First check that at least some bytes of segment are within
2515065Swnj 	 * receive window.
2525065Swnj 	 */
2535065Swnj 	if (tp->rcv_wnd == 0) {
2545065Swnj 		/*
2555065Swnj 		 * If window is closed can only take segments at
2565231Swnj 		 * window edge, and have to drop data and PUSH from
2575065Swnj 		 * incoming segments.
2585065Swnj 		 */
259*5287Sroot 		if (tp->rcv_nxt != ti->ti_seq) {
260*5287Sroot printf("wnd closed, not at edge\n");
2615065Swnj 			goto dropafterack;
262*5287Sroot 		}
2635085Swnj 		if (ti->ti_len > 0) {
2645085Swnj 			ti->ti_len = 0;
2655085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
2665065Swnj 		}
2675065Swnj 	} else {
2685065Swnj 		/*
2695231Swnj 		 * If segment begins before rcv_nxt, drop leading
2705065Swnj 		 * data (and SYN); if nothing left, just ack.
2715065Swnj 		 */
2725065Swnj 		if (SEQ_GT(tp->rcv_nxt, ti->ti_seq)) {
2735085Swnj 			todrop = tp->rcv_nxt - ti->ti_seq;
2745085Swnj 			if (tiflags & TH_SYN) {
2755085Swnj 				ti->ti_seq++;
2765085Swnj 				if (ti->ti_urp > 1)
2775085Swnj 					ti->ti_urp--;
2785085Swnj 				else
2795085Swnj 					tiflags &= ~TH_URG;
2805085Swnj 				todrop--;
2815085Swnj 			}
282*5287Sroot 			if (todrop > ti->ti_len) {
283*5287Sroot printf("window open but outside\n");
2845065Swnj 				goto dropafterack;
285*5287Sroot }
2865065Swnj 			m_adj(m, todrop);
2875065Swnj 			ti->ti_seq += todrop;
2885065Swnj 			ti->ti_len -= todrop;
2895085Swnj 			if (ti->ti_urp > todrop)
2905085Swnj 				ti->ti_urp -= todrop;
2915085Swnj 			else {
2925085Swnj 				tiflags &= ~TH_URG;
2935085Swnj 				/* ti->ti_flags &= ~TH_URG; */
2945085Swnj 				/* ti->ti_urp = 0; */
2955085Swnj 			}
2965085Swnj 			/* tiflags &= ~TH_SYN; */
2975085Swnj 			/* ti->ti_flags &= ~TH_SYN; */
2985065Swnj 		}
2995065Swnj 		/*
3005065Swnj 		 * If segment ends after window, drop trailing data
3015085Swnj 		 * (and PUSH and FIN); if nothing left, just ACK.
3025065Swnj 		 */
3035065Swnj 		if (SEQ_GT(ti->ti_seq+ti->ti_len, tp->rcv_nxt+tp->rcv_wnd)) {
3045085Swnj 			todrop =
3055065Swnj 			     ti->ti_seq+ti->ti_len - (tp->rcv_nxt+tp->rcv_wnd);
306*5287Sroot 			if (todrop > ti->ti_len) {
307*5287Sroot printf("segment outside window\n");
3085065Swnj 				goto dropafterack;
309*5287Sroot }
3105065Swnj 			m_adj(m, -todrop);
3115065Swnj 			ti->ti_len -= todrop;
3125085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
3135065Swnj 		}
3145065Swnj 	}
3154601Swnj 
3165065Swnj 	/*
3175065Swnj 	 * If the RST bit is set examine the state:
3185065Swnj 	 *    SYN_RECEIVED STATE:
3195065Swnj 	 *	If passive open, return to LISTEN state.
3205065Swnj 	 *	If active open, inform user that connection was refused.
3215065Swnj 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
3225065Swnj 	 *	Inform user that connection was reset, and close tcb.
3235065Swnj 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
3245065Swnj 	 *	Close the tcb.
3255065Swnj 	 */
3265065Swnj 	if (tiflags&TH_RST) switch (tp->t_state) {
3275267Sroot 
3285065Swnj 	case TCPS_SYN_RECEIVED:
3295065Swnj 		if (inp->inp_socket->so_options & SO_ACCEPTCONN) {
3305267Sroot 			/* a miniature tcp_close, but invisible to user */
3315267Sroot 			(void) m_free(dtom(tp->t_template));
3325267Sroot 			(void) m_free(dtom(tp));
3335267Sroot 			inp->inp_ppcb = 0;
3345267Sroot 			tp = tcp_newtcpcb(inp);
3355085Swnj 			tp->t_state = TCPS_LISTEN;
3365065Swnj 			goto drop;
3374601Swnj 		}
3385085Swnj 		tcp_drop(tp, ECONNREFUSED);
3395065Swnj 		goto drop;
3404601Swnj 
3415065Swnj 	case TCPS_ESTABLISHED:
3425065Swnj 	case TCPS_FIN_WAIT_1:
3435065Swnj 	case TCPS_FIN_WAIT_2:
3445065Swnj 	case TCPS_CLOSE_WAIT:
3455065Swnj 		tcp_drop(tp, ECONNRESET);
3465065Swnj 		goto drop;
3475065Swnj 
3485065Swnj 	case TCPS_CLOSING:
3495065Swnj 	case TCPS_LAST_ACK:
3505065Swnj 	case TCPS_TIME_WAIT:
3515065Swnj 		tcp_close(tp);
3525065Swnj 		goto drop;
3534601Swnj 	}
3544601Swnj 
3554601Swnj 	/*
3565065Swnj 	 * If a SYN is in the window, then this is an
3575065Swnj 	 * error and we send an RST and drop the connection.
3584601Swnj 	 */
3595065Swnj 	if (tiflags & TH_SYN) {
3605231Swnj 		tcp_drop(tp, ECONNRESET);
3615085Swnj 		goto dropwithreset;
3624601Swnj 	}
3634601Swnj 
3644601Swnj 	/*
3655065Swnj 	 * If the ACK bit is off we drop the segment and return.
3664601Swnj 	 */
3675085Swnj 	if ((tiflags & TH_ACK) == 0)
3685065Swnj 		goto drop;
3695065Swnj 
3705065Swnj 	/*
3715065Swnj 	 * Ack processing.
3725065Swnj 	 */
3734601Swnj 	switch (tp->t_state) {
3744601Swnj 
3755065Swnj 	/*
3765065Swnj 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
3775065Swnj 	 * ESTABLISHED state and continue processing, othewise
3785065Swnj 	 * send an RST.
3795065Swnj 	 */
3805065Swnj 	case TCPS_SYN_RECEIVED:
3815085Swnj 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
3825231Swnj 		    SEQ_GT(ti->ti_ack, tp->snd_max))
3835085Swnj 			goto dropwithreset;
3845244Sroot 		tp->snd_una++;			/* SYN acked */
3855244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
3865263Swnj 		so->so_state |= SS_CONNAWAITING;
3875085Swnj 		soisconnected(so);
3885085Swnj 		tp->t_state = TCPS_ESTABLISHED;
3895162Swnj 		(void) tcp_reass(tp, (struct tcpiphdr *)0);
3905244Sroot 		tp->snd_wl1 = ti->ti_seq - 1;
3915085Swnj 		/* fall into ... */
3924601Swnj 
3935065Swnj 	/*
3945065Swnj 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
3955065Swnj 	 * ACKs.  If the ack is in the range
3965231Swnj 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
3975065Swnj 	 * then advance tp->snd_una to ti->ti_ack and drop
3985065Swnj 	 * data from the retransmission queue.  If this ACK reflects
3995065Swnj 	 * more up to date window information we update our window information.
4005065Swnj 	 */
4015065Swnj 	case TCPS_ESTABLISHED:
4025065Swnj 	case TCPS_FIN_WAIT_1:
4035065Swnj 	case TCPS_FIN_WAIT_2:
4045065Swnj 	case TCPS_CLOSE_WAIT:
4055065Swnj 	case TCPS_CLOSING:
4065244Sroot 	case TCPS_LAST_ACK:
4075244Sroot 	case TCPS_TIME_WAIT:
4085085Swnj #define	ourfinisacked	(acked > 0)
4095085Swnj 
4105244Sroot 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una))
4115065Swnj 			break;
412*5287Sroot 		if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
413*5287Sroot printf("ack > snd_max\n");
4145065Swnj 			goto dropafterack;
415*5287Sroot }
4165085Swnj 		acked = ti->ti_ack - tp->snd_una;
4175244Sroot 		if (acked >= so->so_snd.sb_cc) {
4185085Swnj 			acked -= so->so_snd.sb_cc;
419*5287Sroot 			tp->snd_wnd -= so->so_snd.sb_cc;
4205244Sroot 			/* if acked > 0 our FIN is acked */
4215244Sroot 			sbdrop(&so->so_snd, so->so_snd.sb_cc);
4225244Sroot 			tp->t_timer[TCPT_REXMT] = 0;
4235085Swnj 		} else {
424*5287Sroot 			if (acked) {
425*5287Sroot 				sbdrop(&so->so_snd, acked);
426*5287Sroot 				tp->snd_wnd -= acked;
427*5287Sroot 				acked = 0;
428*5287Sroot 			}
4295244Sroot 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
4305244Sroot 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
4315085Swnj 		}
4325231Swnj 		tp->snd_una = ti->ti_ack;
4335162Swnj 
4345162Swnj 		/*
4355162Swnj 		 * If transmit timer is running and timed sequence
4365162Swnj 		 * number was acked, update smoothed round trip time.
4375162Swnj 		 */
4385162Swnj 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
4395244Sroot 			if (tp->t_srtt == 0)
4405244Sroot 				tp->t_srtt = tp->t_rtt;
4415244Sroot 			else
4425244Sroot 				tp->t_srtt =
4435244Sroot 				    tcp_alpha * tp->t_srtt +
4445244Sroot 				    (1 - tcp_alpha) * tp->t_rtt;
4455162Swnj 			tp->t_rtt = 0;
4465162Swnj 		}
4475162Swnj 
4484601Swnj 		switch (tp->t_state) {
4494601Swnj 
4505065Swnj 		/*
4515065Swnj 		 * In FIN_WAIT_1 STATE in addition to the processing
4525065Swnj 		 * for the ESTABLISHED state if our FIN is now acknowledged
4535085Swnj 		 * then enter FIN_WAIT_2.
4545065Swnj 		 */
4555065Swnj 		case TCPS_FIN_WAIT_1:
4565085Swnj 			if (ourfinisacked)
4575085Swnj 				tp->t_state = TCPS_FIN_WAIT_2;
4584601Swnj 			break;
4594601Swnj 
4605065Swnj 	 	/*
4615065Swnj 		 * In CLOSING STATE in addition to the processing for
4625065Swnj 		 * the ESTABLISHED state if the ACK acknowledges our FIN
4635065Swnj 		 * then enter the TIME-WAIT state, otherwise ignore
4645065Swnj 		 * the segment.
4655065Swnj 		 */
4665065Swnj 		case TCPS_CLOSING:
4675244Sroot 			if (ourfinisacked) {
4685065Swnj 				tp->t_state = TCPS_TIME_WAIT;
4695244Sroot 				tcp_canceltimers(tp);
4705244Sroot 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
4715244Sroot 				soisdisconnected(so);
4725244Sroot 			}
4735244Sroot 			break;
4744601Swnj 
4755065Swnj 		/*
4765085Swnj 		 * The only thing that can arrive in  LAST_ACK state
4775085Swnj 		 * is an acknowledgment of our FIN.  If our FIN is now
4785085Swnj 		 * acknowledged, delete the TCB, enter the closed state
4795085Swnj 		 * and return.
4805065Swnj 		 */
4815065Swnj 		case TCPS_LAST_ACK:
4825251Sroot 			if (ourfinisacked)
4835065Swnj 				tcp_close(tp);
4845065Swnj 			goto drop;
4854601Swnj 
4865065Swnj 		/*
4875065Swnj 		 * In TIME_WAIT state the only thing that should arrive
4885065Swnj 		 * is a retransmission of the remote FIN.  Acknowledge
4895065Swnj 		 * it and restart the finack timer.
4905065Swnj 		 */
4915065Swnj 		case TCPS_TIME_WAIT:
4925162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
4935065Swnj 			goto dropafterack;
4944601Swnj 		}
4955085Swnj #undef ourfinisacked
4965085Swnj 	}
4974601Swnj 
4985065Swnj step6:
4995065Swnj 	/*
5005244Sroot 	 * Update window information.
5015244Sroot 	 */
5025244Sroot 	if (SEQ_LT(tp->snd_wl1, ti->ti_seq) ||
503*5287Sroot 	    tp->snd_wl1==ti->ti_seq && SEQ_LEQ(tp->snd_wl2,ti->ti_ack)) {
5045244Sroot 		tp->snd_wnd = ti->ti_win;
5055244Sroot 		tp->snd_wl1 = ti->ti_seq;
5065244Sroot 		tp->snd_wl2 = ti->ti_ack;
5075244Sroot 		if (tp->snd_wnd > 0)
5085244Sroot 			tp->t_timer[TCPT_PERSIST] = 0;
5095244Sroot 	}
5105244Sroot 
5115244Sroot 	/*
5125065Swnj 	 * If an URG bit is set in the segment and is greater than the
5135065Swnj 	 * current known urgent pointer, then signal the user that the
5145244Sroot 	 * remote side has out of band data.  This should not happen
5155065Swnj 	 * in CLOSE_WAIT, CLOSING, LAST-ACK or TIME_WAIT STATES since
5165065Swnj 	 * a FIN has been received from the remote side.  In these states
5175065Swnj 	 * we ignore the URG.
5185065Swnj 	 */
5195085Swnj 	if ((tiflags & TH_URG) == 0 && TCPS_HAVERCVDFIN(tp->t_state) == 0)
5205085Swnj 		if (SEQ_GT(ti->ti_urp, tp->rcv_up)) {
5215065Swnj 			tp->rcv_up = ti->ti_urp;
5225085Swnj #if 0
5235244Sroot 			sohasoutofband(so);		/* XXX */
5245085Swnj #endif
5254601Swnj 		}
5264601Swnj 
5274601Swnj 	/*
5285065Swnj 	 * Process the segment text, merging it into the TCP sequencing queue,
5295065Swnj 	 * and arranging for acknowledgment of receipt if necessary.
5305065Swnj 	 * This process logically involves adjusting tp->rcv_wnd as data
5315065Swnj 	 * is presented to the user (this happens in tcp_usrreq.c,
5325065Swnj 	 * case PRU_RCVD).  If a FIN has already been received on this
5335065Swnj 	 * connection then we just ignore the text.
5344601Swnj 	 */
5355263Swnj 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
5365263Swnj 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
5375085Swnj 		off += sizeof (struct ip);		/* drop IP header */
5385085Swnj 		m->m_off += off;
5395085Swnj 		m->m_len -= off;
5405065Swnj 		tiflags = tcp_reass(tp, ti);
541*5287Sroot { extern tcpdelack;
542*5287Sroot if (tcpdelack) tp->t_flags |= TF_DELACK; else
5435085Swnj 		tp->t_flags |= TF_ACKNOW;		/* XXX TF_DELACK */
544*5287Sroot }
5455244Sroot 	} else {
5464924Swnj 		m_freem(m);
5475263Swnj 		tiflags &= ~TH_FIN;
5485244Sroot 	}
5494601Swnj 
5504601Swnj 	/*
5515263Swnj 	 * If FIN is received ACK the FIN and let the user know
5525263Swnj 	 * that the connection is closing.
5534601Swnj 	 */
5545263Swnj 	if (tiflags & TH_FIN) {
5555244Sroot 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
5565244Sroot 			socantrcvmore(so);
5575244Sroot 			tp->t_flags |= TF_ACKNOW;
5585244Sroot 			tp->rcv_nxt++;
5595244Sroot 		}
5605065Swnj 		switch (tp->t_state) {
5614601Swnj 
5625065Swnj 	 	/*
5635065Swnj 		 * In SYN_RECEIVED and ESTABLISHED STATES
5645065Swnj 		 * enter the CLOSE_WAIT state.
5654884Swnj 		 */
5665065Swnj 		case TCPS_SYN_RECEIVED:
5675065Swnj 		case TCPS_ESTABLISHED:
5685065Swnj 			tp->t_state = TCPS_CLOSE_WAIT;
5695065Swnj 			break;
5704884Swnj 
5715065Swnj 	 	/*
5725085Swnj 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
5735085Swnj 		 * enter the CLOSING state.
5744884Swnj 		 */
5755065Swnj 		case TCPS_FIN_WAIT_1:
5765085Swnj 			tp->t_state = TCPS_CLOSING;
5775065Swnj 			break;
5784601Swnj 
5795065Swnj 	 	/*
5805065Swnj 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
5815065Swnj 		 * starting the time-wait timer, turning off the other
5825065Swnj 		 * standard timers.
5835065Swnj 		 */
5845065Swnj 		case TCPS_FIN_WAIT_2:
5855244Sroot 			tp->t_state = TCPS_TIME_WAIT;
5865074Swnj 			tcp_canceltimers(tp);
5875162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
5885244Sroot 			soisdisconnected(so);
5895065Swnj 			break;
5905065Swnj 
5914884Swnj 		/*
5925065Swnj 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
5934884Swnj 		 */
5945065Swnj 		case TCPS_TIME_WAIT:
5955162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
5965065Swnj 			break;
5975085Swnj 		}
5984601Swnj 	}
5995267Sroot 	if (so->so_options & SO_DEBUG)
6005267Sroot 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
6015085Swnj 
6025085Swnj 	/*
6035085Swnj 	 * Return any desired output.
6045085Swnj 	 */
6055085Swnj 	tcp_output(tp);
6065065Swnj 	return;
6075085Swnj 
6085065Swnj dropafterack:
6095085Swnj 	/*
6105244Sroot 	 * Generate an ACK dropping incoming segment.
6115085Swnj 	 * Make ACK reflect our state.
6125085Swnj 	 */
6135085Swnj 	if (tiflags & TH_RST)
6145085Swnj 		goto drop;
6155085Swnj 	tcp_respond(ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK);
6165231Swnj 	return;
6175085Swnj 
6185085Swnj dropwithreset:
6195085Swnj 	/*
6205244Sroot 	 * Generate a RST, dropping incoming segment.
6215085Swnj 	 * Make ACK acceptable to originator of segment.
6225085Swnj 	 */
6235085Swnj 	if (tiflags & TH_RST)
6245085Swnj 		goto drop;
6255085Swnj 	if (tiflags & TH_ACK)
6265109Swnj 		tcp_respond(ti, (tcp_seq)0, ti->ti_ack, TH_RST);
6275085Swnj 	else {
6285085Swnj 		if (tiflags & TH_SYN)
6295085Swnj 			ti->ti_len++;
6305109Swnj 		tcp_respond(ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK);
6315085Swnj 	}
6325231Swnj 	return;
6335085Swnj 
6345065Swnj drop:
635*5287Sroot printf("drop\n");
6365085Swnj 	/*
6375085Swnj 	 * Drop space held by incoming segment and return.
6385085Swnj 	 */
6395065Swnj 	m_freem(m);
6405267Sroot 	return;
6415065Swnj }
6425065Swnj 
6435065Swnj /*
6445065Swnj  * Insert segment ti into reassembly queue of tcp with
6455065Swnj  * control block tp.  Return TH_FIN if reassembly now includes
6465065Swnj  * a segment with FIN.
6475065Swnj  */
6485109Swnj tcp_reass(tp, ti)
6495065Swnj 	register struct tcpcb *tp;
6505065Swnj 	register struct tcpiphdr *ti;
6515065Swnj {
6525065Swnj 	register struct tcpiphdr *q;
6535085Swnj 	struct socket *so = tp->t_inpcb->inp_socket;
6545263Swnj 	struct mbuf *m;
6555263Swnj 	int flags;
6565085Swnj COUNT(TCP_REASS);
6575065Swnj 
6585065Swnj 	/*
6595162Swnj 	 * Call with ti==0 after become established to
6605162Swnj 	 * force pre-ESTABLISHED data up to user socket.
6615065Swnj 	 */
6625162Swnj 	if (ti == 0)
6635065Swnj 		goto present;
6644601Swnj 
6655065Swnj 	/*
6665065Swnj 	 * Find a segment which begins after this one does.
6675065Swnj 	 */
6685065Swnj 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
6695065Swnj 	    q = (struct tcpiphdr *)q->ti_next)
6705065Swnj 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
6715065Swnj 			break;
6724601Swnj 
6735065Swnj 	/*
6745065Swnj 	 * If there is a preceding segment, it may provide some of
6755065Swnj 	 * our data already.  If so, drop the data from the incoming
6765065Swnj 	 * segment.  If it provides all of our data, drop us.
6775065Swnj 	 */
6785065Swnj 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
6795065Swnj 		register int i;
6805065Swnj 		q = (struct tcpiphdr *)(q->ti_prev);
6815065Swnj 		/* conversion to int (in i) handles seq wraparound */
6825065Swnj 		i = q->ti_seq + q->ti_len - ti->ti_seq;
6835065Swnj 		if (i > 0) {
6844924Swnj 			if (i >= ti->ti_len)
6855065Swnj 				goto drop;
6865065Swnj 			m_adj(dtom(tp), i);
6875065Swnj 			ti->ti_len -= i;
6884924Swnj 			ti->ti_seq += i;
6894601Swnj 		}
6905065Swnj 		q = (struct tcpiphdr *)(q->ti_next);
6915065Swnj 	}
6924601Swnj 
6935065Swnj 	/*
6945065Swnj 	 * While we overlap succeeding segments trim them or,
6955065Swnj 	 * if they are completely covered, dequeue them.
6965065Swnj 	 */
6975065Swnj 	while (q != (struct tcpiphdr *)tp &&
6985065Swnj 	    SEQ_GT(ti->ti_seq + ti->ti_len, q->ti_seq)) {
6995065Swnj 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
7005065Swnj 		if (i < q->ti_len) {
7015065Swnj 			q->ti_len -= i;
7025065Swnj 			m_adj(dtom(q), i);
7035065Swnj 			break;
7044601Swnj 		}
7055065Swnj 		q = (struct tcpiphdr *)q->ti_next;
7065065Swnj 		m_freem(dtom(q->ti_prev));
7075065Swnj 		remque(q->ti_prev);
7085065Swnj 	}
7094601Swnj 
7105065Swnj 	/*
7115065Swnj 	 * Stick new segment in its place.
7125065Swnj 	 */
7135065Swnj 	insque(ti, q->ti_prev);
7144601Swnj 
7155065Swnj present:
7165065Swnj 	/*
7175244Sroot 	 * Present data to user, advancing rcv_nxt through
7185244Sroot 	 * completed sequence space.
7195065Swnj 	 */
7205263Swnj 	if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
7215244Sroot 		return (0);
7224924Swnj 	ti = tp->seg_next;
7235263Swnj 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
7245263Swnj 		return (0);
7255263Swnj 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
7265263Swnj 		return (0);
7275263Swnj 	do {
7285244Sroot 		tp->rcv_nxt += ti->ti_len;
7295244Sroot 		flags = ti->ti_flags & TH_FIN;
7304924Swnj 		remque(ti);
7315263Swnj 		m = dtom(ti);
7324924Swnj 		ti = (struct tcpiphdr *)ti->ti_next;
7335263Swnj 		if (so->so_state & SS_CANTRCVMORE)
7345263Swnj 			(void) m_freem(m);
7355263Swnj 		else
7365263Swnj 			sbappend(&so->so_rcv, m);
7375263Swnj 	} while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
7385263Swnj 	sorwakeup(so);
7395065Swnj 	return (flags);
7405065Swnj drop:
7415065Swnj 	m_freem(dtom(ti));
7425263Swnj 	return (0);
7434601Swnj }
744