xref: /csrg-svn/sys/netinet/tcp_input.c (revision 8716)
1*8716Sroot /*	tcp_input.c	1.81	82/10/20	*/
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"
98401Swnj #include "../netinet/in.h"
106351Ssam #include "../net/route.h"
118401Swnj #include "../netinet/in_pcb.h"
128401Swnj #include "../netinet/in_systm.h"
135085Swnj #include "../net/if.h"
148401Swnj #include "../netinet/ip.h"
158401Swnj #include "../netinet/ip_var.h"
168401Swnj #include "../netinet/tcp.h"
178401Swnj #include "../netinet/tcp_fsm.h"
188401Swnj #include "../netinet/tcp_seq.h"
198401Swnj #include "../netinet/tcp_timer.h"
208401Swnj #include "../netinet/tcp_var.h"
218401Swnj #include "../netinet/tcpip.h"
228401Swnj #include "../netinet/tcp_debug.h"
237300Ssam #include <errno.h>
244601Swnj 
255300Sroot int	tcpprintfs = 0;
264679Swnj int	tcpcksum = 1;
275267Sroot struct	tcpiphdr tcp_saveti;
285440Swnj extern	tcpnodelack;
294601Swnj 
305267Sroot struct	tcpcb *tcp_newtcpcb();
315065Swnj /*
325065Swnj  * TCP input routine, follows pages 65-76 of the
335065Swnj  * protocol specification dated September, 1981 very closely.
345065Swnj  */
354924Swnj tcp_input(m0)
364924Swnj 	struct mbuf *m0;
374601Swnj {
384924Swnj 	register struct tcpiphdr *ti;
394924Swnj 	struct inpcb *inp;
404924Swnj 	register struct mbuf *m;
415440Swnj 	struct mbuf *om = 0;
424924Swnj 	int len, tlen, off;
435391Swnj 	register struct tcpcb *tp = 0;
444924Swnj 	register int tiflags;
454803Swnj 	struct socket *so;
465109Swnj 	int todrop, acked;
475267Sroot 	short ostate;
486028Sroot 	struct in_addr laddr;
494924Swnj 
504924Swnj 	/*
515244Sroot 	 * Get IP and TCP header together in first mbuf.
525244Sroot 	 * Note: IP leaves IP header in first mbuf.
534924Swnj 	 */
544924Swnj 	m = m0;
555020Sroot 	ti = mtod(m, struct tcpiphdr *);
565244Sroot 	if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2))
575208Swnj 		ip_stripoptions((struct ip *)ti, (struct mbuf *)0);
585307Sroot 	if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) {
595307Sroot 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
605085Swnj 			tcpstat.tcps_hdrops++;
615307Sroot 			return;
625085Swnj 		}
635085Swnj 		ti = mtod(m, struct tcpiphdr *);
645085Swnj 	}
654601Swnj 
664601Swnj 	/*
675244Sroot 	 * Checksum extended TCP header and data.
684601Swnj 	 */
694924Swnj 	tlen = ((struct ip *)ti)->ip_len;
704924Swnj 	len = sizeof (struct ip) + tlen;
714679Swnj 	if (tcpcksum) {
724924Swnj 		ti->ti_next = ti->ti_prev = 0;
734924Swnj 		ti->ti_x1 = 0;
745223Swnj 		ti->ti_len = (u_short)tlen;
758599Sroot #if vax || pdp11 || ns16032
766161Ssam 		ti->ti_len = htons((u_short)ti->ti_len);
775223Swnj #endif
785231Swnj 		if (ti->ti_sum = in_cksum(m, len)) {
794924Swnj 			tcpstat.tcps_badsum++;
806211Swnj 			if (tcpprintfs)
816211Swnj 				printf("tcp cksum %x\n", ti->ti_sum);
825085Swnj 			goto drop;
834601Swnj 		}
844601Swnj 	}
854601Swnj 
864601Swnj 	/*
875244Sroot 	 * Check that TCP offset makes sense,
885440Swnj 	 * pull out TCP options and adjust length.
894601Swnj 	 */
904924Swnj 	off = ti->ti_off << 2;
915231Swnj 	if (off < sizeof (struct tcphdr) || off > tlen) {
924924Swnj 		tcpstat.tcps_badoff++;
935085Swnj 		goto drop;
944924Swnj 	}
956211Swnj 	tlen -= off;
966211Swnj 	ti->ti_len = tlen;
975440Swnj 	if (off > sizeof (struct tcphdr)) {
985440Swnj 		if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
995440Swnj 			tcpstat.tcps_hdrops++;
1005440Swnj 			goto drop;
1015440Swnj 		}
1025440Swnj 		ti = mtod(m, struct tcpiphdr *);
1035440Swnj 		om = m_get(M_DONTWAIT);
1045440Swnj 		if (om == 0)
1055440Swnj 			goto drop;
1065440Swnj 		om->m_len = off - sizeof (struct tcphdr);
1075440Swnj 		{ caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
1086161Ssam 		  bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len);
1095440Swnj 		  m->m_len -= om->m_len;
1106161Ssam 		  bcopy(op+om->m_len, op,
1116161Ssam 		   (unsigned)(m->m_len-sizeof (struct tcpiphdr)));
1125440Swnj 		}
1135440Swnj 	}
1145065Swnj 	tiflags = ti->ti_flags;
1154924Swnj 
1166093Sroot 	/*
1176211Swnj 	 * Drop TCP and IP headers.
1186093Sroot 	 */
1196093Sroot 	off += sizeof (struct ip);
1206093Sroot 	m->m_off += off;
1216093Sroot 	m->m_len -= off;
1226093Sroot 
1238599Sroot #if vax || pdp11 || ns16032
1244924Swnj 	/*
1255244Sroot 	 * Convert TCP protocol specific fields to host format.
1265085Swnj 	 */
1275085Swnj 	ti->ti_seq = ntohl(ti->ti_seq);
1285085Swnj 	ti->ti_ack = ntohl(ti->ti_ack);
1295085Swnj 	ti->ti_win = ntohs(ti->ti_win);
1305085Swnj 	ti->ti_urp = ntohs(ti->ti_urp);
1315231Swnj #endif
1325085Swnj 
1335085Swnj 	/*
1348271Sroot 	 * Locate pcb for segment.
1354924Swnj 	 */
1365065Swnj 	inp = in_pcblookup
1376028Sroot 		(&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport,
1386028Sroot 		INPLOOKUP_WILDCARD);
1395065Swnj 
1405065Swnj 	/*
1415065Swnj 	 * If the state is CLOSED (i.e., TCB does not exist) then
1425244Sroot 	 * all data in the incoming segment is discarded.
1435065Swnj 	 */
1445300Sroot 	if (inp == 0)
1455085Swnj 		goto dropwithreset;
1465065Swnj 	tp = intotcpcb(inp);
1475300Sroot 	if (tp == 0)
1485085Swnj 		goto dropwithreset;
1495109Swnj 	so = inp->inp_socket;
1505267Sroot 	if (so->so_options & SO_DEBUG) {
1515267Sroot 		ostate = tp->t_state;
1525267Sroot 		tcp_saveti = *ti;
1535267Sroot 	}
1547510Sroot 	if (so->so_options & SO_ACCEPTCONN) {
1557510Sroot 		so = sonewconn(so);
1567510Sroot 		if (so == 0)
1577510Sroot 			goto drop;
1587510Sroot 		inp = (struct inpcb *)so->so_pcb;
1597510Sroot 		inp->inp_laddr = ti->ti_dst;
1607510Sroot 		inp->inp_lport = ti->ti_dport;
1617510Sroot 		tp = intotcpcb(inp);
1627510Sroot 		tp->t_state = TCPS_LISTEN;
1637510Sroot 	}
1644601Swnj 
1654601Swnj 	/*
1665162Swnj 	 * Segment received on connection.
1675162Swnj 	 * Reset idle time and keep-alive timer.
1685162Swnj 	 */
1695162Swnj 	tp->t_idle = 0;
1705162Swnj 	tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
1715162Swnj 
1725162Swnj 	/*
1735440Swnj 	 * Process options.
1745440Swnj 	 */
1755440Swnj 	if (om) {
1765440Swnj 		tcp_dooptions(tp, om);
1775440Swnj 		om = 0;
1785440Swnj 	}
1795440Swnj 
1805440Swnj 	/*
1815085Swnj 	 * Calculate amount of space in receive window,
1825085Swnj 	 * and then do TCP input processing.
1834601Swnj 	 */
1845085Swnj 	tp->rcv_wnd = sbspace(&so->so_rcv);
1855231Swnj 	if (tp->rcv_wnd < 0)
1865231Swnj 		tp->rcv_wnd = 0;
1874601Swnj 
1884601Swnj 	switch (tp->t_state) {
1894601Swnj 
1905065Swnj 	/*
1915065Swnj 	 * If the state is LISTEN then ignore segment if it contains an RST.
1925065Swnj 	 * If the segment contains an ACK then it is bad and send a RST.
1935065Swnj 	 * If it does not contain a SYN then it is not interesting; drop it.
1945085Swnj 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
1955065Swnj 	 * tp->iss, and send a segment:
1965085Swnj 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
1975065Swnj 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
1985065Swnj 	 * Fill in remote peer address fields if not previously specified.
1995065Swnj 	 * Enter SYN_RECEIVED state, and process any other fields of this
2005244Sroot 	 * segment in this state.
2015065Swnj 	 */
2028271Sroot 	case TCPS_LISTEN: {
2038599Sroot 		struct mbuf *am = m_get(M_DONTWAIT);
2048271Sroot 		register struct sockaddr_in *sin;
2058271Sroot 
2068599Sroot 		if (am == 0)
2078271Sroot 			goto drop;
2088599Sroot 		am->m_len = sizeof (struct sockaddr_in);
2095065Swnj 		if (tiflags & TH_RST)
2105065Swnj 			goto drop;
2115300Sroot 		if (tiflags & TH_ACK)
2125085Swnj 			goto dropwithreset;
2135300Sroot 		if ((tiflags & TH_SYN) == 0)
2145065Swnj 			goto drop;
2158599Sroot 		sin = mtod(am, struct sockaddr_in *);
2168271Sroot 		sin->sin_family = AF_INET;
2178271Sroot 		sin->sin_addr = ti->ti_src;
2188271Sroot 		sin->sin_port = ti->ti_sport;
2196028Sroot 		laddr = inp->inp_laddr;
2206028Sroot 		if (inp->inp_laddr.s_addr == 0)
2216028Sroot 			inp->inp_laddr = ti->ti_dst;
2228599Sroot 		if (in_pcbconnect(inp, am)) {
2236028Sroot 			inp->inp_laddr = laddr;
224*8716Sroot 			(void) m_free(am);
2255244Sroot 			goto drop;
2266028Sroot 		}
227*8716Sroot 		(void) m_free(am);
2285244Sroot 		tp->t_template = tcp_template(tp);
2295244Sroot 		if (tp->t_template == 0) {
2305244Sroot 			in_pcbdisconnect(inp);
2316028Sroot 			inp->inp_laddr = laddr;
2326320Swnj 			tp = 0;
2335244Sroot 			goto drop;
2345244Sroot 		}
2355085Swnj 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
2365065Swnj 		tp->irs = ti->ti_seq;
2375085Swnj 		tcp_sendseqinit(tp);
2385085Swnj 		tcp_rcvseqinit(tp);
2395065Swnj 		tp->t_state = TCPS_SYN_RECEIVED;
2405244Sroot 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
2415085Swnj 		goto trimthenstep6;
2428271Sroot 		}
2434601Swnj 
2445065Swnj 	/*
2455065Swnj 	 * If the state is SYN_SENT:
2465065Swnj 	 *	if seg contains an ACK, but not for our SYN, drop the input.
2475065Swnj 	 *	if seg contains a RST, then drop the connection.
2485065Swnj 	 *	if seg does not contain SYN, then drop it.
2495065Swnj 	 * Otherwise this is an acceptable SYN segment
2505065Swnj 	 *	initialize tp->rcv_nxt and tp->irs
2515065Swnj 	 *	if seg contains ack then advance tp->snd_una
2525065Swnj 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
2535065Swnj 	 *	arrange for segment to be acked (eventually)
2545065Swnj 	 *	continue processing rest of data/controls, beginning with URG
2555065Swnj 	 */
2565065Swnj 	case TCPS_SYN_SENT:
2575065Swnj 		if ((tiflags & TH_ACK) &&
2585300Sroot /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */
2595300Sroot 		    (SEQ_LT(ti->ti_ack, tp->iss) ||
2605231Swnj 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
2615085Swnj 			goto dropwithreset;
2625065Swnj 		if (tiflags & TH_RST) {
2636320Swnj 			if (tiflags & TH_ACK) {
2645267Sroot 				tcp_drop(tp, ECONNREFUSED);
2656320Swnj 				tp = 0;
2666320Swnj 			}
2675065Swnj 			goto drop;
2684601Swnj 		}
2695065Swnj 		if ((tiflags & TH_SYN) == 0)
2705065Swnj 			goto drop;
2715231Swnj 		tp->snd_una = ti->ti_ack;
2725357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2735357Sroot 			tp->snd_nxt = tp->snd_una;
2745244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
2755065Swnj 		tp->irs = ti->ti_seq;
2765085Swnj 		tcp_rcvseqinit(tp);
2775085Swnj 		tp->t_flags |= TF_ACKNOW;
2785162Swnj 		if (SEQ_GT(tp->snd_una, tp->iss)) {
2795244Sroot 			soisconnected(so);
2805065Swnj 			tp->t_state = TCPS_ESTABLISHED;
2815162Swnj 			(void) tcp_reass(tp, (struct tcpiphdr *)0);
2825162Swnj 		} else
2835085Swnj 			tp->t_state = TCPS_SYN_RECEIVED;
2845085Swnj 		goto trimthenstep6;
2855085Swnj 
2865085Swnj trimthenstep6:
2875085Swnj 		/*
2885231Swnj 		 * Advance ti->ti_seq to correspond to first data byte.
2895085Swnj 		 * If data, trim to stay within window,
2905085Swnj 		 * dropping FIN if necessary.
2915085Swnj 		 */
2925231Swnj 		ti->ti_seq++;
2935085Swnj 		if (ti->ti_len > tp->rcv_wnd) {
2945085Swnj 			todrop = ti->ti_len - tp->rcv_wnd;
2955085Swnj 			m_adj(m, -todrop);
2965085Swnj 			ti->ti_len = tp->rcv_wnd;
2975085Swnj 			ti->ti_flags &= ~TH_FIN;
2985065Swnj 		}
2995263Swnj 		tp->snd_wl1 = ti->ti_seq - 1;
3005085Swnj 		goto step6;
3015065Swnj 	}
3024601Swnj 
3035065Swnj 	/*
3045065Swnj 	 * States other than LISTEN or SYN_SENT.
3055065Swnj 	 * First check that at least some bytes of segment are within
3065065Swnj 	 * receive window.
3075065Swnj 	 */
3085065Swnj 	if (tp->rcv_wnd == 0) {
3095065Swnj 		/*
3105065Swnj 		 * If window is closed can only take segments at
3115231Swnj 		 * window edge, and have to drop data and PUSH from
3125065Swnj 		 * incoming segments.
3135065Swnj 		 */
3145300Sroot 		if (tp->rcv_nxt != ti->ti_seq)
3155065Swnj 			goto dropafterack;
3165085Swnj 		if (ti->ti_len > 0) {
3175690Swnj 			m_adj(m, ti->ti_len);
3185085Swnj 			ti->ti_len = 0;
3195085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
3205065Swnj 		}
3215065Swnj 	} else {
3225065Swnj 		/*
3235231Swnj 		 * If segment begins before rcv_nxt, drop leading
3245065Swnj 		 * data (and SYN); if nothing left, just ack.
3255065Swnj 		 */
3265690Swnj 		todrop = tp->rcv_nxt - ti->ti_seq;
3275690Swnj 		if (todrop > 0) {
3285085Swnj 			if (tiflags & TH_SYN) {
3295300Sroot 				tiflags &= ~TH_SYN;
3305690Swnj 				ti->ti_flags &= ~TH_SYN;
3315085Swnj 				ti->ti_seq++;
3325085Swnj 				if (ti->ti_urp > 1)
3335085Swnj 					ti->ti_urp--;
3345085Swnj 				else
3355085Swnj 					tiflags &= ~TH_URG;
3365085Swnj 				todrop--;
3375085Swnj 			}
3386211Swnj 			if (todrop > ti->ti_len ||
3396211Swnj 			    todrop == ti->ti_len && (tiflags&TH_FIN) == 0)
3405065Swnj 				goto dropafterack;
3415065Swnj 			m_adj(m, todrop);
3425065Swnj 			ti->ti_seq += todrop;
3435065Swnj 			ti->ti_len -= todrop;
3445085Swnj 			if (ti->ti_urp > todrop)
3455085Swnj 				ti->ti_urp -= todrop;
3465085Swnj 			else {
3475085Swnj 				tiflags &= ~TH_URG;
3485690Swnj 				ti->ti_flags &= ~TH_URG;
3495690Swnj 				ti->ti_urp = 0;
3505085Swnj 			}
3515065Swnj 		}
3525065Swnj 		/*
3535065Swnj 		 * If segment ends after window, drop trailing data
3545085Swnj 		 * (and PUSH and FIN); if nothing left, just ACK.
3555065Swnj 		 */
3565690Swnj 		todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
3575690Swnj 		if (todrop > 0) {
3586211Swnj 			if (todrop >= ti->ti_len)
3595065Swnj 				goto dropafterack;
3605065Swnj 			m_adj(m, -todrop);
3615065Swnj 			ti->ti_len -= todrop;
3625085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
3635065Swnj 		}
3645065Swnj 	}
3654601Swnj 
3665065Swnj 	/*
3675951Swnj 	 * If a segment is received on a connection after the
3685951Swnj 	 * user processes are gone, then RST the other end.
3695951Swnj 	 */
3707510Sroot 	if (so->so_state & SS_NOFDREF) {
3715951Swnj 		tcp_close(tp);
3726266Swnj 		tp = 0;
3735951Swnj 		goto dropwithreset;
3745951Swnj 	}
3755951Swnj 
3765951Swnj 	/*
3775065Swnj 	 * If the RST bit is set examine the state:
3785065Swnj 	 *    SYN_RECEIVED STATE:
3795065Swnj 	 *	If passive open, return to LISTEN state.
3805065Swnj 	 *	If active open, inform user that connection was refused.
3815065Swnj 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
3825065Swnj 	 *	Inform user that connection was reset, and close tcb.
3835065Swnj 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
3845065Swnj 	 *	Close the tcb.
3855065Swnj 	 */
3865065Swnj 	if (tiflags&TH_RST) switch (tp->t_state) {
3875267Sroot 
3885065Swnj 	case TCPS_SYN_RECEIVED:
3895085Swnj 		tcp_drop(tp, ECONNREFUSED);
3906320Swnj 		tp = 0;
3915065Swnj 		goto drop;
3924601Swnj 
3935065Swnj 	case TCPS_ESTABLISHED:
3945065Swnj 	case TCPS_FIN_WAIT_1:
3955065Swnj 	case TCPS_FIN_WAIT_2:
3965065Swnj 	case TCPS_CLOSE_WAIT:
3975065Swnj 		tcp_drop(tp, ECONNRESET);
3986320Swnj 		tp = 0;
3995065Swnj 		goto drop;
4005065Swnj 
4015065Swnj 	case TCPS_CLOSING:
4025065Swnj 	case TCPS_LAST_ACK:
4035065Swnj 	case TCPS_TIME_WAIT:
4045065Swnj 		tcp_close(tp);
4056320Swnj 		tp = 0;
4065065Swnj 		goto drop;
4074601Swnj 	}
4084601Swnj 
4094601Swnj 	/*
4105065Swnj 	 * If a SYN is in the window, then this is an
4115065Swnj 	 * error and we send an RST and drop the connection.
4124601Swnj 	 */
4135065Swnj 	if (tiflags & TH_SYN) {
4145231Swnj 		tcp_drop(tp, ECONNRESET);
4156266Swnj 		tp = 0;
4165085Swnj 		goto dropwithreset;
4174601Swnj 	}
4184601Swnj 
4194601Swnj 	/*
4205065Swnj 	 * If the ACK bit is off we drop the segment and return.
4214601Swnj 	 */
4225085Swnj 	if ((tiflags & TH_ACK) == 0)
4235065Swnj 		goto drop;
4245065Swnj 
4255065Swnj 	/*
4265065Swnj 	 * Ack processing.
4275065Swnj 	 */
4284601Swnj 	switch (tp->t_state) {
4294601Swnj 
4305065Swnj 	/*
4315065Swnj 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
4325065Swnj 	 * ESTABLISHED state and continue processing, othewise
4335065Swnj 	 * send an RST.
4345065Swnj 	 */
4355065Swnj 	case TCPS_SYN_RECEIVED:
4365085Swnj 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
4375231Swnj 		    SEQ_GT(ti->ti_ack, tp->snd_max))
4385085Swnj 			goto dropwithreset;
4395244Sroot 		tp->snd_una++;			/* SYN acked */
4405357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
4415357Sroot 			tp->snd_nxt = tp->snd_una;
4425244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
4435085Swnj 		soisconnected(so);
4445085Swnj 		tp->t_state = TCPS_ESTABLISHED;
4455162Swnj 		(void) tcp_reass(tp, (struct tcpiphdr *)0);
4465244Sroot 		tp->snd_wl1 = ti->ti_seq - 1;
4475085Swnj 		/* fall into ... */
4484601Swnj 
4495065Swnj 	/*
4505065Swnj 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
4515065Swnj 	 * ACKs.  If the ack is in the range
4525231Swnj 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
4535065Swnj 	 * then advance tp->snd_una to ti->ti_ack and drop
4545065Swnj 	 * data from the retransmission queue.  If this ACK reflects
4555065Swnj 	 * more up to date window information we update our window information.
4565065Swnj 	 */
4575065Swnj 	case TCPS_ESTABLISHED:
4585065Swnj 	case TCPS_FIN_WAIT_1:
4595065Swnj 	case TCPS_FIN_WAIT_2:
4605065Swnj 	case TCPS_CLOSE_WAIT:
4615065Swnj 	case TCPS_CLOSING:
4625244Sroot 	case TCPS_LAST_ACK:
4635244Sroot 	case TCPS_TIME_WAIT:
4645085Swnj #define	ourfinisacked	(acked > 0)
4655085Swnj 
4665244Sroot 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una))
4675065Swnj 			break;
4685300Sroot 		if (SEQ_GT(ti->ti_ack, tp->snd_max))
4695065Swnj 			goto dropafterack;
4705085Swnj 		acked = ti->ti_ack - tp->snd_una;
4715951Swnj 
4725951Swnj 		/*
4735951Swnj 		 * If transmit timer is running and timed sequence
4745951Swnj 		 * number was acked, update smoothed round trip time.
4755951Swnj 		 */
4765951Swnj 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
4775951Swnj 			if (tp->t_srtt == 0)
4785951Swnj 				tp->t_srtt = tp->t_rtt;
4795951Swnj 			else
4805951Swnj 				tp->t_srtt =
4815951Swnj 				    tcp_alpha * tp->t_srtt +
4825951Swnj 				    (1 - tcp_alpha) * tp->t_rtt;
4835951Swnj /* printf("rtt %d srtt*100 now %d\n", tp->t_rtt, (int)(tp->t_srtt*100)); */
4845951Swnj 			tp->t_rtt = 0;
4855951Swnj 		}
4865951Swnj 
4875307Sroot 		if (ti->ti_ack == tp->snd_max)
4885244Sroot 			tp->t_timer[TCPT_REXMT] = 0;
4895307Sroot 		else {
4905244Sroot 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
4915244Sroot 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
4925951Swnj 			tp->t_rtt = 1;
4935300Sroot 			tp->t_rxtshift = 0;
4945085Swnj 		}
4955307Sroot 		if (acked > so->so_snd.sb_cc) {
4965307Sroot 			sbdrop(&so->so_snd, so->so_snd.sb_cc);
4975307Sroot 			tp->snd_wnd -= so->so_snd.sb_cc;
4985307Sroot 		} else {
4996161Ssam 			sbdrop(&so->so_snd, acked);
5005307Sroot 			tp->snd_wnd -= acked;
5015307Sroot 			acked = 0;
5025307Sroot 		}
5036434Swnj 		if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel)
5045300Sroot 			sowwakeup(so);
5055231Swnj 		tp->snd_una = ti->ti_ack;
5065357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
5075357Sroot 			tp->snd_nxt = tp->snd_una;
5085162Swnj 
5094601Swnj 		switch (tp->t_state) {
5104601Swnj 
5115065Swnj 		/*
5125065Swnj 		 * In FIN_WAIT_1 STATE in addition to the processing
5135065Swnj 		 * for the ESTABLISHED state if our FIN is now acknowledged
5145085Swnj 		 * then enter FIN_WAIT_2.
5155065Swnj 		 */
5165065Swnj 		case TCPS_FIN_WAIT_1:
5175896Swnj 			if (ourfinisacked) {
5185896Swnj 				/*
5195896Swnj 				 * If we can't receive any more
5205896Swnj 				 * data, then closing user can proceed.
5215896Swnj 				 */
5225896Swnj 				if (so->so_state & SS_CANTRCVMORE)
5235896Swnj 					soisdisconnected(so);
5245085Swnj 				tp->t_state = TCPS_FIN_WAIT_2;
5255896Swnj 			}
5264601Swnj 			break;
5274601Swnj 
5285065Swnj 	 	/*
5295065Swnj 		 * In CLOSING STATE in addition to the processing for
5305065Swnj 		 * the ESTABLISHED state if the ACK acknowledges our FIN
5315065Swnj 		 * then enter the TIME-WAIT state, otherwise ignore
5325065Swnj 		 * the segment.
5335065Swnj 		 */
5345065Swnj 		case TCPS_CLOSING:
5355244Sroot 			if (ourfinisacked) {
5365065Swnj 				tp->t_state = TCPS_TIME_WAIT;
5375244Sroot 				tcp_canceltimers(tp);
5385244Sroot 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
5395244Sroot 				soisdisconnected(so);
5405244Sroot 			}
5415244Sroot 			break;
5424601Swnj 
5435065Swnj 		/*
5445085Swnj 		 * The only thing that can arrive in  LAST_ACK state
5455085Swnj 		 * is an acknowledgment of our FIN.  If our FIN is now
5465085Swnj 		 * acknowledged, delete the TCB, enter the closed state
5475085Swnj 		 * and return.
5485065Swnj 		 */
5495065Swnj 		case TCPS_LAST_ACK:
5506320Swnj 			if (ourfinisacked) {
5515065Swnj 				tcp_close(tp);
5526320Swnj 				tp = 0;
5536320Swnj 			}
5545065Swnj 			goto drop;
5554601Swnj 
5565065Swnj 		/*
5575065Swnj 		 * In TIME_WAIT state the only thing that should arrive
5585065Swnj 		 * is a retransmission of the remote FIN.  Acknowledge
5595065Swnj 		 * it and restart the finack timer.
5605065Swnj 		 */
5615065Swnj 		case TCPS_TIME_WAIT:
5625162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
5635065Swnj 			goto dropafterack;
5644601Swnj 		}
5655085Swnj #undef ourfinisacked
5665085Swnj 	}
5674601Swnj 
5685065Swnj step6:
5695065Swnj 	/*
5705244Sroot 	 * Update window information.
5715244Sroot 	 */
5725300Sroot 	if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
5735391Swnj 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
5745300Sroot 	     tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) {
5755244Sroot 		tp->snd_wnd = ti->ti_win;
5765244Sroot 		tp->snd_wl1 = ti->ti_seq;
5775244Sroot 		tp->snd_wl2 = ti->ti_ack;
5788599Sroot 		if (tp->snd_wnd != 0)
5795244Sroot 			tp->t_timer[TCPT_PERSIST] = 0;
5805244Sroot 	}
5815244Sroot 
5825244Sroot 	/*
5835547Swnj 	 * Process segments with URG.
5845065Swnj 	 */
5857267Swnj 	if ((tiflags & TH_URG) && ti->ti_urp &&
5867267Swnj 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
5875547Swnj 		/*
5885547Swnj 		 * If this segment advances the known urgent pointer,
5895547Swnj 		 * then mark the data stream.  This should not happen
5905547Swnj 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
5915547Swnj 		 * a FIN has been received from the remote side.
5925547Swnj 		 * In these states we ignore the URG.
5935547Swnj 		 */
5945547Swnj 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
5955547Swnj 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
5965547Swnj 			so->so_oobmark = so->so_rcv.sb_cc +
5975547Swnj 			    (tp->rcv_up - tp->rcv_nxt) - 1;
5985547Swnj 			if (so->so_oobmark == 0)
5995547Swnj 				so->so_state |= SS_RCVATMARK;
6008313Sroot 			sohasoutofband(so);
6015547Swnj 			tp->t_oobflags &= ~TCPOOB_HAVEDATA;
6025440Swnj 		}
6035547Swnj 		/*
6045547Swnj 		 * Remove out of band data so doesn't get presented to user.
6055547Swnj 		 * This can happen independent of advancing the URG pointer,
6065547Swnj 		 * but if two URG's are pending at once, some out-of-band
6075547Swnj 		 * data may creep in... ick.
6085547Swnj 		 */
6097510Sroot 		if (ti->ti_urp <= ti->ti_len)
6105547Swnj 			tcp_pulloutofband(so, ti);
6115419Swnj 	}
6124601Swnj 
6134601Swnj 	/*
6145065Swnj 	 * Process the segment text, merging it into the TCP sequencing queue,
6155065Swnj 	 * and arranging for acknowledgment of receipt if necessary.
6165065Swnj 	 * This process logically involves adjusting tp->rcv_wnd as data
6175065Swnj 	 * is presented to the user (this happens in tcp_usrreq.c,
6185065Swnj 	 * case PRU_RCVD).  If a FIN has already been received on this
6195065Swnj 	 * connection then we just ignore the text.
6204601Swnj 	 */
6215263Swnj 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
6225263Swnj 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
6235065Swnj 		tiflags = tcp_reass(tp, ti);
6245440Swnj 		if (tcpnodelack == 0)
6255440Swnj 			tp->t_flags |= TF_DELACK;
6265440Swnj 		else
6275440Swnj 			tp->t_flags |= TF_ACKNOW;
6285244Sroot 	} else {
6294924Swnj 		m_freem(m);
6305263Swnj 		tiflags &= ~TH_FIN;
6315244Sroot 	}
6324601Swnj 
6334601Swnj 	/*
6345263Swnj 	 * If FIN is received ACK the FIN and let the user know
6355263Swnj 	 * that the connection is closing.
6364601Swnj 	 */
6375263Swnj 	if (tiflags & TH_FIN) {
6385244Sroot 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
6395244Sroot 			socantrcvmore(so);
6405244Sroot 			tp->t_flags |= TF_ACKNOW;
6415244Sroot 			tp->rcv_nxt++;
6425244Sroot 		}
6435065Swnj 		switch (tp->t_state) {
6444601Swnj 
6455065Swnj 	 	/*
6465065Swnj 		 * In SYN_RECEIVED and ESTABLISHED STATES
6475065Swnj 		 * enter the CLOSE_WAIT state.
6484884Swnj 		 */
6495065Swnj 		case TCPS_SYN_RECEIVED:
6505065Swnj 		case TCPS_ESTABLISHED:
6515065Swnj 			tp->t_state = TCPS_CLOSE_WAIT;
6525065Swnj 			break;
6534884Swnj 
6545065Swnj 	 	/*
6555085Swnj 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
6565085Swnj 		 * enter the CLOSING state.
6574884Swnj 		 */
6585065Swnj 		case TCPS_FIN_WAIT_1:
6595085Swnj 			tp->t_state = TCPS_CLOSING;
6605065Swnj 			break;
6614601Swnj 
6625065Swnj 	 	/*
6635065Swnj 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
6645065Swnj 		 * starting the time-wait timer, turning off the other
6655065Swnj 		 * standard timers.
6665065Swnj 		 */
6675065Swnj 		case TCPS_FIN_WAIT_2:
6685244Sroot 			tp->t_state = TCPS_TIME_WAIT;
6695074Swnj 			tcp_canceltimers(tp);
6705162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
6715244Sroot 			soisdisconnected(so);
6725065Swnj 			break;
6735065Swnj 
6744884Swnj 		/*
6755065Swnj 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
6764884Swnj 		 */
6775065Swnj 		case TCPS_TIME_WAIT:
6785162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
6795065Swnj 			break;
6805085Swnj 		}
6814601Swnj 	}
6825267Sroot 	if (so->so_options & SO_DEBUG)
6835267Sroot 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
6845085Swnj 
6855085Swnj 	/*
6865085Swnj 	 * Return any desired output.
6875085Swnj 	 */
6886161Ssam 	(void) tcp_output(tp);
6895065Swnj 	return;
6905085Swnj 
6915065Swnj dropafterack:
6925085Swnj 	/*
6936211Swnj 	 * Generate an ACK dropping incoming segment if it occupies
6946211Swnj 	 * sequence space, where the ACK reflects our state.
6955085Swnj 	 */
6966211Swnj 	if ((tiflags&TH_RST) ||
6976211Swnj 	    tlen == 0 && (tiflags&(TH_SYN|TH_FIN)) == 0)
6985085Swnj 		goto drop;
6996303Sroot 	if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
7006303Sroot 		tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0);
7015391Swnj 	tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK);
7025231Swnj 	return;
7035085Swnj 
7045085Swnj dropwithreset:
7055440Swnj 	if (om)
7066161Ssam 		(void) m_free(om);
7075085Swnj 	/*
7085244Sroot 	 * Generate a RST, dropping incoming segment.
7095085Swnj 	 * Make ACK acceptable to originator of segment.
7105085Swnj 	 */
7115085Swnj 	if (tiflags & TH_RST)
7125085Swnj 		goto drop;
7135085Swnj 	if (tiflags & TH_ACK)
7145391Swnj 		tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST);
7155085Swnj 	else {
7165085Swnj 		if (tiflags & TH_SYN)
7175085Swnj 			ti->ti_len++;
7186211Swnj 		tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0,
7196211Swnj 		    TH_RST|TH_ACK);
7205085Swnj 	}
7215231Swnj 	return;
7225085Swnj 
7235065Swnj drop:
7245085Swnj 	/*
7255085Swnj 	 * Drop space held by incoming segment and return.
7265085Swnj 	 */
7276303Sroot 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
7286303Sroot 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
7295065Swnj 	m_freem(m);
7305267Sroot 	return;
7315065Swnj }
7325065Swnj 
7335440Swnj tcp_dooptions(tp, om)
7345440Swnj 	struct tcpcb *tp;
7355440Swnj 	struct mbuf *om;
7365419Swnj {
7375440Swnj 	register u_char *cp;
7385440Swnj 	int opt, optlen, cnt;
7395419Swnj 
7405440Swnj 	cp = mtod(om, u_char *);
7415440Swnj 	cnt = om->m_len;
7425440Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
7435440Swnj 		opt = cp[0];
7445440Swnj 		if (opt == TCPOPT_EOL)
7455440Swnj 			break;
7465440Swnj 		if (opt == TCPOPT_NOP)
7475440Swnj 			optlen = 1;
7485440Swnj 		else
7495440Swnj 			optlen = cp[1];
7505440Swnj 		switch (opt) {
7515440Swnj 
7525440Swnj 		default:
7535440Swnj 			break;
7545440Swnj 
7555440Swnj 		case TCPOPT_MAXSEG:
7565440Swnj 			if (optlen != 4)
7575440Swnj 				continue;
7585440Swnj 			tp->t_maxseg = *(u_short *)(cp + 2);
7598599Sroot #if vax || pdp11 || ns16032
7606161Ssam 			tp->t_maxseg = ntohs((u_short)tp->t_maxseg);
7615440Swnj #endif
7625440Swnj 			break;
7635419Swnj 		}
7645419Swnj 	}
7656161Ssam 	(void) m_free(om);
7665419Swnj }
7675419Swnj 
7685419Swnj /*
7695547Swnj  * Pull out of band byte out of a segment so
7705547Swnj  * it doesn't appear in the user's data queue.
7715547Swnj  * It is still reflected in the segment length for
7725547Swnj  * sequencing purposes.
7735547Swnj  */
7745547Swnj tcp_pulloutofband(so, ti)
7755547Swnj 	struct socket *so;
7765547Swnj 	struct tcpiphdr *ti;
7775547Swnj {
7785547Swnj 	register struct mbuf *m;
7796116Swnj 	int cnt = ti->ti_urp - 1;
7805547Swnj 
7815547Swnj 	m = dtom(ti);
7825547Swnj 	while (cnt >= 0) {
7835547Swnj 		if (m->m_len > cnt) {
7845547Swnj 			char *cp = mtod(m, caddr_t) + cnt;
7855547Swnj 			struct tcpcb *tp = sototcpcb(so);
7865547Swnj 
7875547Swnj 			tp->t_iobc = *cp;
7885547Swnj 			tp->t_oobflags |= TCPOOB_HAVEDATA;
7896161Ssam 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
7905547Swnj 			m->m_len--;
7915547Swnj 			return;
7925547Swnj 		}
7935547Swnj 		cnt -= m->m_len;
7945547Swnj 		m = m->m_next;
7955547Swnj 		if (m == 0)
7965547Swnj 			break;
7975547Swnj 	}
7985547Swnj 	panic("tcp_pulloutofband");
7995547Swnj }
8005547Swnj 
8015547Swnj /*
8025065Swnj  * Insert segment ti into reassembly queue of tcp with
8035065Swnj  * control block tp.  Return TH_FIN if reassembly now includes
8045065Swnj  * a segment with FIN.
8055065Swnj  */
8065109Swnj tcp_reass(tp, ti)
8075065Swnj 	register struct tcpcb *tp;
8085065Swnj 	register struct tcpiphdr *ti;
8095065Swnj {
8105065Swnj 	register struct tcpiphdr *q;
8115085Swnj 	struct socket *so = tp->t_inpcb->inp_socket;
8125263Swnj 	struct mbuf *m;
8135263Swnj 	int flags;
8145065Swnj 
8155065Swnj 	/*
8165162Swnj 	 * Call with ti==0 after become established to
8175162Swnj 	 * force pre-ESTABLISHED data up to user socket.
8185065Swnj 	 */
8195162Swnj 	if (ti == 0)
8205065Swnj 		goto present;
8214601Swnj 
8225065Swnj 	/*
8235065Swnj 	 * Find a segment which begins after this one does.
8245065Swnj 	 */
8255065Swnj 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
8265065Swnj 	    q = (struct tcpiphdr *)q->ti_next)
8275065Swnj 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
8285065Swnj 			break;
8294601Swnj 
8305065Swnj 	/*
8315065Swnj 	 * If there is a preceding segment, it may provide some of
8325065Swnj 	 * our data already.  If so, drop the data from the incoming
8335065Swnj 	 * segment.  If it provides all of our data, drop us.
8345065Swnj 	 */
8355065Swnj 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
8365065Swnj 		register int i;
8375690Swnj 		q = (struct tcpiphdr *)q->ti_prev;
8385065Swnj 		/* conversion to int (in i) handles seq wraparound */
8395065Swnj 		i = q->ti_seq + q->ti_len - ti->ti_seq;
8405065Swnj 		if (i > 0) {
8414924Swnj 			if (i >= ti->ti_len)
8425065Swnj 				goto drop;
8437338Swnj 			m_adj(dtom(ti), i);
8445065Swnj 			ti->ti_len -= i;
8454924Swnj 			ti->ti_seq += i;
8464601Swnj 		}
8475065Swnj 		q = (struct tcpiphdr *)(q->ti_next);
8485065Swnj 	}
8494601Swnj 
8505065Swnj 	/*
8515065Swnj 	 * While we overlap succeeding segments trim them or,
8525065Swnj 	 * if they are completely covered, dequeue them.
8535065Swnj 	 */
8545690Swnj 	while (q != (struct tcpiphdr *)tp) {
8555065Swnj 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
8565690Swnj 		if (i <= 0)
8575690Swnj 			break;
8585065Swnj 		if (i < q->ti_len) {
8595690Swnj 			q->ti_seq += i;
8605065Swnj 			q->ti_len -= i;
8615065Swnj 			m_adj(dtom(q), i);
8625065Swnj 			break;
8634601Swnj 		}
8645065Swnj 		q = (struct tcpiphdr *)q->ti_next;
8655623Swnj 		m = dtom(q->ti_prev);
8665065Swnj 		remque(q->ti_prev);
8675623Swnj 		m_freem(m);
8685065Swnj 	}
8694601Swnj 
8705065Swnj 	/*
8715065Swnj 	 * Stick new segment in its place.
8725065Swnj 	 */
8735065Swnj 	insque(ti, q->ti_prev);
8744601Swnj 
8755065Swnj present:
8765065Swnj 	/*
8775244Sroot 	 * Present data to user, advancing rcv_nxt through
8785244Sroot 	 * completed sequence space.
8795065Swnj 	 */
8805263Swnj 	if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
8815244Sroot 		return (0);
8824924Swnj 	ti = tp->seg_next;
8835263Swnj 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
8845263Swnj 		return (0);
8855263Swnj 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
8865263Swnj 		return (0);
8875263Swnj 	do {
8885244Sroot 		tp->rcv_nxt += ti->ti_len;
8895244Sroot 		flags = ti->ti_flags & TH_FIN;
8904924Swnj 		remque(ti);
8915263Swnj 		m = dtom(ti);
8924924Swnj 		ti = (struct tcpiphdr *)ti->ti_next;
8935263Swnj 		if (so->so_state & SS_CANTRCVMORE)
8946161Ssam 			m_freem(m);
8958550Sroot 		else {
8968550Sroot SBCHECK(&so->so_rcv, "tcp_input before");
8975263Swnj 			sbappend(&so->so_rcv, m);
8988550Sroot SBCHECK(&so->so_rcv, "tcp_input after");
8998550Sroot 		}
9005263Swnj 	} while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
9015263Swnj 	sorwakeup(so);
9025065Swnj 	return (flags);
9035065Swnj drop:
9045065Swnj 	m_freem(dtom(ti));
9055263Swnj 	return (0);
9064601Swnj }
907