xref: /csrg-svn/sys/netinet/tcp_input.c (revision 5690)
1*5690Swnj /*	tcp_input.c	1.52	82/02/03	*/
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 
245300Sroot int	tcpprintfs = 0;
254679Swnj int	tcpcksum = 1;
265244Sroot struct	sockaddr_in tcp_in = { AF_INET };
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;
484924Swnj 
494601Swnj COUNT(TCP_INPUT);
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;
755223Swnj #if vax
765223Swnj 		ti->ti_len = htons(ti->ti_len);
775223Swnj #endif
785231Swnj 		if (ti->ti_sum = in_cksum(m, len)) {
794924Swnj 			tcpstat.tcps_badsum++;
805065Swnj 			printf("tcp cksum %x\n", ti->ti_sum);
815085Swnj 			goto drop;
824601Swnj 		}
834601Swnj 	}
844601Swnj 
854601Swnj 	/*
865244Sroot 	 * Check that TCP offset makes sense,
875440Swnj 	 * pull out TCP options and adjust length.
884601Swnj 	 */
894924Swnj 	off = ti->ti_off << 2;
905231Swnj 	if (off < sizeof (struct tcphdr) || off > tlen) {
914924Swnj 		tcpstat.tcps_badoff++;
925085Swnj 		goto drop;
934924Swnj 	}
944924Swnj 	ti->ti_len = tlen - off;
955440Swnj 	if (off > sizeof (struct tcphdr)) {
965440Swnj 		if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
975440Swnj 			tcpstat.tcps_hdrops++;
985440Swnj 			goto drop;
995440Swnj 		}
1005440Swnj 		ti = mtod(m, struct tcpiphdr *);
1015440Swnj 		om = m_get(M_DONTWAIT);
1025440Swnj 		if (om == 0)
1035440Swnj 			goto drop;
1045440Swnj 		om->m_off = MMINOFF;
1055440Swnj 		om->m_len = off - sizeof (struct tcphdr);
1065440Swnj 		{ caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
1075440Swnj 		  bcopy(op, mtod(om, caddr_t), om->m_len);
1085440Swnj 		  m->m_len -= om->m_len;
1095440Swnj 		  bcopy(op+om->m_len, op, m->m_len-sizeof (struct tcpiphdr));
1105440Swnj 		}
1115440Swnj 	}
1125065Swnj 	tiflags = ti->ti_flags;
1134924Swnj 
1145231Swnj #if vax
1154924Swnj 	/*
1165244Sroot 	 * Convert TCP protocol specific fields to host format.
1175085Swnj 	 */
1185085Swnj 	ti->ti_seq = ntohl(ti->ti_seq);
1195085Swnj 	ti->ti_ack = ntohl(ti->ti_ack);
1205085Swnj 	ti->ti_win = ntohs(ti->ti_win);
1215085Swnj 	ti->ti_urp = ntohs(ti->ti_urp);
1225231Swnj #endif
1235085Swnj 
1245085Swnj 	/*
1254924Swnj 	 * Locate pcb for segment.
1264924Swnj 	 */
1275065Swnj 	inp = in_pcblookup
1285065Swnj 		(&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport);
1295065Swnj 
1305065Swnj 	/*
1315065Swnj 	 * If the state is CLOSED (i.e., TCB does not exist) then
1325244Sroot 	 * all data in the incoming segment is discarded.
1335065Swnj 	 */
1345300Sroot 	if (inp == 0)
1355085Swnj 		goto dropwithreset;
1365065Swnj 	tp = intotcpcb(inp);
1375300Sroot 	if (tp == 0)
1385085Swnj 		goto dropwithreset;
1395109Swnj 	so = inp->inp_socket;
1405267Sroot 	if (so->so_options & SO_DEBUG) {
1415267Sroot 		ostate = tp->t_state;
1425267Sroot 		tcp_saveti = *ti;
1435267Sroot 	}
1444601Swnj 
1454601Swnj 	/*
1465162Swnj 	 * Segment received on connection.
1475162Swnj 	 * Reset idle time and keep-alive timer.
1485162Swnj 	 */
1495162Swnj 	tp->t_idle = 0;
1505162Swnj 	tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
1515162Swnj 
1525162Swnj 	/*
1535440Swnj 	 * Process options.
1545440Swnj 	 */
1555440Swnj 	if (om) {
1565440Swnj 		tcp_dooptions(tp, om);
1575440Swnj 		om = 0;
1585440Swnj 	}
1595440Swnj 
1605440Swnj 	/*
1615085Swnj 	 * Calculate amount of space in receive window,
1625085Swnj 	 * and then do TCP input processing.
1634601Swnj 	 */
1645085Swnj 	tp->rcv_wnd = sbspace(&so->so_rcv);
1655231Swnj 	if (tp->rcv_wnd < 0)
1665231Swnj 		tp->rcv_wnd = 0;
1674601Swnj 
1684601Swnj 	switch (tp->t_state) {
1694601Swnj 
1705065Swnj 	/*
1715065Swnj 	 * If the state is LISTEN then ignore segment if it contains an RST.
1725065Swnj 	 * If the segment contains an ACK then it is bad and send a RST.
1735065Swnj 	 * If it does not contain a SYN then it is not interesting; drop it.
1745085Swnj 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
1755065Swnj 	 * tp->iss, and send a segment:
1765085Swnj 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
1775065Swnj 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
1785065Swnj 	 * Fill in remote peer address fields if not previously specified.
1795065Swnj 	 * Enter SYN_RECEIVED state, and process any other fields of this
1805244Sroot 	 * segment in this state.
1815065Swnj 	 */
1825065Swnj 	case TCPS_LISTEN:
1835065Swnj 		if (tiflags & TH_RST)
1845065Swnj 			goto drop;
1855300Sroot 		if (tiflags & TH_ACK)
1865085Swnj 			goto dropwithreset;
1875300Sroot 		if ((tiflags & TH_SYN) == 0)
1885065Swnj 			goto drop;
1895244Sroot 		tcp_in.sin_addr = ti->ti_src;
1905244Sroot 		tcp_in.sin_port = ti->ti_sport;
1915300Sroot 		if (in_pcbconnect(inp, (struct sockaddr *)&tcp_in))
1925244Sroot 			goto drop;
1935244Sroot 		tp->t_template = tcp_template(tp);
1945244Sroot 		if (tp->t_template == 0) {
1955244Sroot 			in_pcbdisconnect(inp);
1965244Sroot 			goto drop;
1975244Sroot 		}
1985085Swnj 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
1995065Swnj 		tp->irs = ti->ti_seq;
2005085Swnj 		tcp_sendseqinit(tp);
2015085Swnj 		tcp_rcvseqinit(tp);
2025065Swnj 		tp->t_state = TCPS_SYN_RECEIVED;
2035244Sroot 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
2045085Swnj 		goto trimthenstep6;
2054601Swnj 
2065065Swnj 	/*
2075065Swnj 	 * If the state is SYN_SENT:
2085065Swnj 	 *	if seg contains an ACK, but not for our SYN, drop the input.
2095065Swnj 	 *	if seg contains a RST, then drop the connection.
2105065Swnj 	 *	if seg does not contain SYN, then drop it.
2115065Swnj 	 * Otherwise this is an acceptable SYN segment
2125065Swnj 	 *	initialize tp->rcv_nxt and tp->irs
2135065Swnj 	 *	if seg contains ack then advance tp->snd_una
2145065Swnj 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
2155065Swnj 	 *	arrange for segment to be acked (eventually)
2165065Swnj 	 *	continue processing rest of data/controls, beginning with URG
2175065Swnj 	 */
2185065Swnj 	case TCPS_SYN_SENT:
2195065Swnj 		if ((tiflags & TH_ACK) &&
2205300Sroot /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */
2215300Sroot 		    (SEQ_LT(ti->ti_ack, tp->iss) ||
2225231Swnj 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
2235085Swnj 			goto dropwithreset;
2245065Swnj 		if (tiflags & TH_RST) {
2255065Swnj 			if (tiflags & TH_ACK)
2265267Sroot 				tcp_drop(tp, ECONNREFUSED);
2275065Swnj 			goto drop;
2284601Swnj 		}
2295065Swnj 		if ((tiflags & TH_SYN) == 0)
2305065Swnj 			goto drop;
2315231Swnj 		tp->snd_una = ti->ti_ack;
2325357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2335357Sroot 			tp->snd_nxt = tp->snd_una;
2345244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
2355065Swnj 		tp->irs = ti->ti_seq;
2365085Swnj 		tcp_rcvseqinit(tp);
2375085Swnj 		tp->t_flags |= TF_ACKNOW;
2385162Swnj 		if (SEQ_GT(tp->snd_una, tp->iss)) {
2395391Swnj 			if (so->so_options & SO_ACCEPTCONN)
2405391Swnj 				so->so_state |= SS_CONNAWAITING;
2415244Sroot 			soisconnected(so);
2425065Swnj 			tp->t_state = TCPS_ESTABLISHED;
2435162Swnj 			(void) tcp_reass(tp, (struct tcpiphdr *)0);
2445162Swnj 		} else
2455085Swnj 			tp->t_state = TCPS_SYN_RECEIVED;
2465085Swnj 		goto trimthenstep6;
2475085Swnj 
2485085Swnj trimthenstep6:
2495085Swnj 		/*
2505231Swnj 		 * Advance ti->ti_seq to correspond to first data byte.
2515085Swnj 		 * If data, trim to stay within window,
2525085Swnj 		 * dropping FIN if necessary.
2535085Swnj 		 */
2545231Swnj 		ti->ti_seq++;
2555085Swnj 		if (ti->ti_len > tp->rcv_wnd) {
2565085Swnj 			todrop = ti->ti_len - tp->rcv_wnd;
2575085Swnj 			m_adj(m, -todrop);
2585085Swnj 			ti->ti_len = tp->rcv_wnd;
2595085Swnj 			ti->ti_flags &= ~TH_FIN;
2605065Swnj 		}
2615263Swnj 		tp->snd_wl1 = ti->ti_seq - 1;
2625085Swnj 		goto step6;
2635065Swnj 	}
2644601Swnj 
2655065Swnj 	/*
2665065Swnj 	 * States other than LISTEN or SYN_SENT.
2675065Swnj 	 * First check that at least some bytes of segment are within
2685065Swnj 	 * receive window.
2695065Swnj 	 */
2705065Swnj 	if (tp->rcv_wnd == 0) {
2715065Swnj 		/*
2725065Swnj 		 * If window is closed can only take segments at
2735231Swnj 		 * window edge, and have to drop data and PUSH from
2745065Swnj 		 * incoming segments.
2755065Swnj 		 */
2765300Sroot 		if (tp->rcv_nxt != ti->ti_seq)
2775065Swnj 			goto dropafterack;
2785085Swnj 		if (ti->ti_len > 0) {
279*5690Swnj 			m_adj(m, ti->ti_len);
2805085Swnj 			ti->ti_len = 0;
2815085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
2825065Swnj 		}
2835065Swnj 	} else {
2845065Swnj 		/*
2855231Swnj 		 * If segment begins before rcv_nxt, drop leading
2865065Swnj 		 * data (and SYN); if nothing left, just ack.
2875065Swnj 		 */
288*5690Swnj 		todrop = tp->rcv_nxt - ti->ti_seq;
289*5690Swnj 		if (todrop > 0) {
2905085Swnj 			if (tiflags & TH_SYN) {
2915300Sroot 				tiflags &= ~TH_SYN;
292*5690Swnj 				ti->ti_flags &= ~TH_SYN;
2935085Swnj 				ti->ti_seq++;
2945085Swnj 				if (ti->ti_urp > 1)
2955085Swnj 					ti->ti_urp--;
2965085Swnj 				else
2975085Swnj 					tiflags &= ~TH_URG;
2985085Swnj 				todrop--;
2995085Swnj 			}
3005300Sroot 			if (todrop > ti->ti_len)
3015065Swnj 				goto dropafterack;
3025065Swnj 			m_adj(m, todrop);
3035065Swnj 			ti->ti_seq += todrop;
3045065Swnj 			ti->ti_len -= todrop;
3055085Swnj 			if (ti->ti_urp > todrop)
3065085Swnj 				ti->ti_urp -= todrop;
3075085Swnj 			else {
3085085Swnj 				tiflags &= ~TH_URG;
309*5690Swnj 				ti->ti_flags &= ~TH_URG;
310*5690Swnj 				ti->ti_urp = 0;
3115085Swnj 			}
3125065Swnj 		}
3135065Swnj 		/*
3145065Swnj 		 * If segment ends after window, drop trailing data
3155085Swnj 		 * (and PUSH and FIN); if nothing left, just ACK.
3165065Swnj 		 */
317*5690Swnj 		todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
318*5690Swnj 		if (todrop > 0) {
3195300Sroot 			if (todrop > ti->ti_len)
3205065Swnj 				goto dropafterack;
3215065Swnj 			m_adj(m, -todrop);
3225065Swnj 			ti->ti_len -= todrop;
3235085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
3245065Swnj 		}
3255065Swnj 	}
3264601Swnj 
3275065Swnj 	/*
3285065Swnj 	 * If the RST bit is set examine the state:
3295065Swnj 	 *    SYN_RECEIVED STATE:
3305065Swnj 	 *	If passive open, return to LISTEN state.
3315065Swnj 	 *	If active open, inform user that connection was refused.
3325065Swnj 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
3335065Swnj 	 *	Inform user that connection was reset, and close tcb.
3345065Swnj 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
3355065Swnj 	 *	Close the tcb.
3365065Swnj 	 */
3375065Swnj 	if (tiflags&TH_RST) switch (tp->t_state) {
3385267Sroot 
3395065Swnj 	case TCPS_SYN_RECEIVED:
3405065Swnj 		if (inp->inp_socket->so_options & SO_ACCEPTCONN) {
3415267Sroot 			/* a miniature tcp_close, but invisible to user */
3425267Sroot 			(void) m_free(dtom(tp->t_template));
3435267Sroot 			(void) m_free(dtom(tp));
3445267Sroot 			inp->inp_ppcb = 0;
3455267Sroot 			tp = tcp_newtcpcb(inp);
3465085Swnj 			tp->t_state = TCPS_LISTEN;
3475065Swnj 			goto drop;
3484601Swnj 		}
3495085Swnj 		tcp_drop(tp, ECONNREFUSED);
3505065Swnj 		goto drop;
3514601Swnj 
3525065Swnj 	case TCPS_ESTABLISHED:
3535065Swnj 	case TCPS_FIN_WAIT_1:
3545065Swnj 	case TCPS_FIN_WAIT_2:
3555065Swnj 	case TCPS_CLOSE_WAIT:
3565065Swnj 		tcp_drop(tp, ECONNRESET);
3575065Swnj 		goto drop;
3585065Swnj 
3595065Swnj 	case TCPS_CLOSING:
3605065Swnj 	case TCPS_LAST_ACK:
3615065Swnj 	case TCPS_TIME_WAIT:
3625065Swnj 		tcp_close(tp);
3635065Swnj 		goto drop;
3644601Swnj 	}
3654601Swnj 
3664601Swnj 	/*
3675065Swnj 	 * If a SYN is in the window, then this is an
3685065Swnj 	 * error and we send an RST and drop the connection.
3694601Swnj 	 */
3705065Swnj 	if (tiflags & TH_SYN) {
3715231Swnj 		tcp_drop(tp, ECONNRESET);
3725085Swnj 		goto dropwithreset;
3734601Swnj 	}
3744601Swnj 
3754601Swnj 	/*
3765065Swnj 	 * If the ACK bit is off we drop the segment and return.
3774601Swnj 	 */
3785085Swnj 	if ((tiflags & TH_ACK) == 0)
3795065Swnj 		goto drop;
3805065Swnj 
3815065Swnj 	/*
3825065Swnj 	 * Ack processing.
3835065Swnj 	 */
3844601Swnj 	switch (tp->t_state) {
3854601Swnj 
3865065Swnj 	/*
3875065Swnj 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
3885065Swnj 	 * ESTABLISHED state and continue processing, othewise
3895065Swnj 	 * send an RST.
3905065Swnj 	 */
3915065Swnj 	case TCPS_SYN_RECEIVED:
3925085Swnj 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
3935231Swnj 		    SEQ_GT(ti->ti_ack, tp->snd_max))
3945085Swnj 			goto dropwithreset;
3955244Sroot 		tp->snd_una++;			/* SYN acked */
3965357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
3975357Sroot 			tp->snd_nxt = tp->snd_una;
3985244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
3995391Swnj 		if (so->so_options & SO_ACCEPTCONN)
4005391Swnj 			so->so_state |= SS_CONNAWAITING;
4015085Swnj 		soisconnected(so);
4025085Swnj 		tp->t_state = TCPS_ESTABLISHED;
4035162Swnj 		(void) tcp_reass(tp, (struct tcpiphdr *)0);
4045244Sroot 		tp->snd_wl1 = ti->ti_seq - 1;
4055085Swnj 		/* fall into ... */
4064601Swnj 
4075065Swnj 	/*
4085065Swnj 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
4095065Swnj 	 * ACKs.  If the ack is in the range
4105231Swnj 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
4115065Swnj 	 * then advance tp->snd_una to ti->ti_ack and drop
4125065Swnj 	 * data from the retransmission queue.  If this ACK reflects
4135065Swnj 	 * more up to date window information we update our window information.
4145065Swnj 	 */
4155065Swnj 	case TCPS_ESTABLISHED:
4165065Swnj 	case TCPS_FIN_WAIT_1:
4175065Swnj 	case TCPS_FIN_WAIT_2:
4185065Swnj 	case TCPS_CLOSE_WAIT:
4195065Swnj 	case TCPS_CLOSING:
4205244Sroot 	case TCPS_LAST_ACK:
4215244Sroot 	case TCPS_TIME_WAIT:
4225085Swnj #define	ourfinisacked	(acked > 0)
4235085Swnj 
4245244Sroot 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una))
4255065Swnj 			break;
4265300Sroot 		if (SEQ_GT(ti->ti_ack, tp->snd_max))
4275065Swnj 			goto dropafterack;
4285085Swnj 		acked = ti->ti_ack - tp->snd_una;
4295307Sroot 		if (ti->ti_ack == tp->snd_max)
4305244Sroot 			tp->t_timer[TCPT_REXMT] = 0;
4315307Sroot 		else {
4325244Sroot 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
4335244Sroot 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
4345300Sroot 			tp->t_rtt = 0;
4355300Sroot 			tp->t_rxtshift = 0;
4365085Swnj 		}
4375307Sroot 		if (acked > so->so_snd.sb_cc) {
4385307Sroot 			sbdrop(&so->so_snd, so->so_snd.sb_cc);
4395307Sroot 			tp->snd_wnd -= so->so_snd.sb_cc;
4405307Sroot 		} else {
4415307Sroot 			sbdrop(&so->so_snd.sb_cc, acked);
4425307Sroot 			tp->snd_wnd -= acked;
4435307Sroot 			acked = 0;
4445307Sroot 		}
4455300Sroot 		if (so->so_snd.sb_flags & SB_WAIT)
4465300Sroot 			sowwakeup(so);
4475231Swnj 		tp->snd_una = ti->ti_ack;
4485357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
4495357Sroot 			tp->snd_nxt = tp->snd_una;
4505162Swnj 
4515162Swnj 		/*
4525162Swnj 		 * If transmit timer is running and timed sequence
4535162Swnj 		 * number was acked, update smoothed round trip time.
4545162Swnj 		 */
4555162Swnj 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
4565244Sroot 			if (tp->t_srtt == 0)
4575244Sroot 				tp->t_srtt = tp->t_rtt;
4585244Sroot 			else
4595244Sroot 				tp->t_srtt =
4605244Sroot 				    tcp_alpha * tp->t_srtt +
4615244Sroot 				    (1 - tcp_alpha) * tp->t_rtt;
4625162Swnj 			tp->t_rtt = 0;
4635162Swnj 		}
4645162Swnj 
4654601Swnj 		switch (tp->t_state) {
4664601Swnj 
4675065Swnj 		/*
4685065Swnj 		 * In FIN_WAIT_1 STATE in addition to the processing
4695065Swnj 		 * for the ESTABLISHED state if our FIN is now acknowledged
4705085Swnj 		 * then enter FIN_WAIT_2.
4715065Swnj 		 */
4725065Swnj 		case TCPS_FIN_WAIT_1:
4735085Swnj 			if (ourfinisacked)
4745085Swnj 				tp->t_state = TCPS_FIN_WAIT_2;
4754601Swnj 			break;
4764601Swnj 
4775065Swnj 	 	/*
4785065Swnj 		 * In CLOSING STATE in addition to the processing for
4795065Swnj 		 * the ESTABLISHED state if the ACK acknowledges our FIN
4805065Swnj 		 * then enter the TIME-WAIT state, otherwise ignore
4815065Swnj 		 * the segment.
4825065Swnj 		 */
4835065Swnj 		case TCPS_CLOSING:
4845244Sroot 			if (ourfinisacked) {
4855065Swnj 				tp->t_state = TCPS_TIME_WAIT;
4865244Sroot 				tcp_canceltimers(tp);
4875244Sroot 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
4885244Sroot 				soisdisconnected(so);
4895244Sroot 			}
4905244Sroot 			break;
4914601Swnj 
4925065Swnj 		/*
4935085Swnj 		 * The only thing that can arrive in  LAST_ACK state
4945085Swnj 		 * is an acknowledgment of our FIN.  If our FIN is now
4955085Swnj 		 * acknowledged, delete the TCB, enter the closed state
4965085Swnj 		 * and return.
4975065Swnj 		 */
4985065Swnj 		case TCPS_LAST_ACK:
4995251Sroot 			if (ourfinisacked)
5005065Swnj 				tcp_close(tp);
5015065Swnj 			goto drop;
5024601Swnj 
5035065Swnj 		/*
5045065Swnj 		 * In TIME_WAIT state the only thing that should arrive
5055065Swnj 		 * is a retransmission of the remote FIN.  Acknowledge
5065065Swnj 		 * it and restart the finack timer.
5075065Swnj 		 */
5085065Swnj 		case TCPS_TIME_WAIT:
5095162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
5105065Swnj 			goto dropafterack;
5114601Swnj 		}
5125085Swnj #undef ourfinisacked
5135085Swnj 	}
5144601Swnj 
5155065Swnj step6:
5165065Swnj 	/*
5175244Sroot 	 * Update window information.
5185244Sroot 	 */
5195300Sroot 	if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
5205391Swnj 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
5215300Sroot 	     tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) {
5225391Swnj /*
5235391Swnj printf("wl1 %x seq %x wl2 %x ack %x win %x wnd %x\n", tp->snd_wl1, ti->ti_seq, tp->snd_wl2, ti->ti_ack, ti->ti_win, tp->snd_wnd);
5245391Swnj */
5255244Sroot 		tp->snd_wnd = ti->ti_win;
5265244Sroot 		tp->snd_wl1 = ti->ti_seq;
5275244Sroot 		tp->snd_wl2 = ti->ti_ack;
5285244Sroot 		if (tp->snd_wnd > 0)
5295244Sroot 			tp->t_timer[TCPT_PERSIST] = 0;
5305244Sroot 	}
5315244Sroot 
5325244Sroot 	/*
5335547Swnj 	 * Process segments with URG.
5345065Swnj 	 */
5355547Swnj 	if ((tiflags & TH_URG) && TCPS_HAVERCVDFIN(tp->t_state) == 0) {
5365547Swnj 		/*
5375547Swnj 		 * If this segment advances the known urgent pointer,
5385547Swnj 		 * then mark the data stream.  This should not happen
5395547Swnj 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
5405547Swnj 		 * a FIN has been received from the remote side.
5415547Swnj 		 * In these states we ignore the URG.
5425547Swnj 		 */
5435547Swnj 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
5445547Swnj 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
5455547Swnj 			so->so_oobmark = so->so_rcv.sb_cc +
5465547Swnj 			    (tp->rcv_up - tp->rcv_nxt) - 1;
5475547Swnj 			if (so->so_oobmark == 0)
5485547Swnj 				so->so_state |= SS_RCVATMARK;
5495440Swnj #ifdef TCPTRUEOOB
5505547Swnj 			if ((tp->t_flags & TF_DOOOB) == 0)
5515440Swnj #endif
5525547Swnj 				sohasoutofband(so);
5535547Swnj 			tp->t_oobflags &= ~TCPOOB_HAVEDATA;
5545440Swnj 		}
5555547Swnj 		/*
5565547Swnj 		 * Remove out of band data so doesn't get presented to user.
5575547Swnj 		 * This can happen independent of advancing the URG pointer,
5585547Swnj 		 * but if two URG's are pending at once, some out-of-band
5595547Swnj 		 * data may creep in... ick.
5605547Swnj 		 */
5615547Swnj 		if (ti->ti_urp <= ti->ti_len) {
5625547Swnj 			tcp_pulloutofband(so, ti);
5635547Swnj 		}
5645419Swnj 	}
5654601Swnj 
5664601Swnj 	/*
5675065Swnj 	 * Process the segment text, merging it into the TCP sequencing queue,
5685065Swnj 	 * and arranging for acknowledgment of receipt if necessary.
5695065Swnj 	 * This process logically involves adjusting tp->rcv_wnd as data
5705065Swnj 	 * is presented to the user (this happens in tcp_usrreq.c,
5715065Swnj 	 * case PRU_RCVD).  If a FIN has already been received on this
5725065Swnj 	 * connection then we just ignore the text.
5734601Swnj 	 */
5745263Swnj 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
5755263Swnj 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
5765085Swnj 		off += sizeof (struct ip);		/* drop IP header */
5775085Swnj 		m->m_off += off;
5785085Swnj 		m->m_len -= off;
5795065Swnj 		tiflags = tcp_reass(tp, ti);
5805440Swnj 		if (tcpnodelack == 0)
5815440Swnj 			tp->t_flags |= TF_DELACK;
5825440Swnj 		else
5835440Swnj 			tp->t_flags |= TF_ACKNOW;
5845244Sroot 	} else {
5854924Swnj 		m_freem(m);
5865263Swnj 		tiflags &= ~TH_FIN;
5875244Sroot 	}
5884601Swnj 
5894601Swnj 	/*
5905263Swnj 	 * If FIN is received ACK the FIN and let the user know
5915263Swnj 	 * that the connection is closing.
5924601Swnj 	 */
5935263Swnj 	if (tiflags & TH_FIN) {
5945244Sroot 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
5955244Sroot 			socantrcvmore(so);
5965244Sroot 			tp->t_flags |= TF_ACKNOW;
5975244Sroot 			tp->rcv_nxt++;
5985244Sroot 		}
5995065Swnj 		switch (tp->t_state) {
6004601Swnj 
6015065Swnj 	 	/*
6025065Swnj 		 * In SYN_RECEIVED and ESTABLISHED STATES
6035065Swnj 		 * enter the CLOSE_WAIT state.
6044884Swnj 		 */
6055065Swnj 		case TCPS_SYN_RECEIVED:
6065065Swnj 		case TCPS_ESTABLISHED:
6075065Swnj 			tp->t_state = TCPS_CLOSE_WAIT;
6085065Swnj 			break;
6094884Swnj 
6105065Swnj 	 	/*
6115085Swnj 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
6125085Swnj 		 * enter the CLOSING state.
6134884Swnj 		 */
6145065Swnj 		case TCPS_FIN_WAIT_1:
6155085Swnj 			tp->t_state = TCPS_CLOSING;
6165065Swnj 			break;
6174601Swnj 
6185065Swnj 	 	/*
6195065Swnj 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
6205065Swnj 		 * starting the time-wait timer, turning off the other
6215065Swnj 		 * standard timers.
6225065Swnj 		 */
6235065Swnj 		case TCPS_FIN_WAIT_2:
6245244Sroot 			tp->t_state = TCPS_TIME_WAIT;
6255074Swnj 			tcp_canceltimers(tp);
6265162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
6275244Sroot 			soisdisconnected(so);
6285065Swnj 			break;
6295065Swnj 
6304884Swnj 		/*
6315065Swnj 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
6324884Swnj 		 */
6335065Swnj 		case TCPS_TIME_WAIT:
6345162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
6355065Swnj 			break;
6365085Swnj 		}
6374601Swnj 	}
6385267Sroot 	if (so->so_options & SO_DEBUG)
6395267Sroot 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
6405085Swnj 
6415085Swnj 	/*
6425085Swnj 	 * Return any desired output.
6435085Swnj 	 */
6445085Swnj 	tcp_output(tp);
6455065Swnj 	return;
6465085Swnj 
6475065Swnj dropafterack:
6485085Swnj 	/*
6495244Sroot 	 * Generate an ACK dropping incoming segment.
6505085Swnj 	 * Make ACK reflect our state.
6515085Swnj 	 */
6525085Swnj 	if (tiflags & TH_RST)
6535085Swnj 		goto drop;
6545391Swnj 	tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK);
6555231Swnj 	return;
6565085Swnj 
6575085Swnj dropwithreset:
6585440Swnj 	if (om)
6595440Swnj 		m_free(om);
6605085Swnj 	/*
6615244Sroot 	 * Generate a RST, dropping incoming segment.
6625085Swnj 	 * Make ACK acceptable to originator of segment.
6635085Swnj 	 */
6645085Swnj 	if (tiflags & TH_RST)
6655085Swnj 		goto drop;
6665085Swnj 	if (tiflags & TH_ACK)
6675391Swnj 		tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST);
6685085Swnj 	else {
6695085Swnj 		if (tiflags & TH_SYN)
6705085Swnj 			ti->ti_len++;
6715391Swnj 		tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK);
6725085Swnj 	}
6735231Swnj 	return;
6745085Swnj 
6755065Swnj drop:
6765085Swnj 	/*
6775085Swnj 	 * Drop space held by incoming segment and return.
6785085Swnj 	 */
6795065Swnj 	m_freem(m);
6805267Sroot 	return;
6815065Swnj }
6825065Swnj 
6835440Swnj tcp_dooptions(tp, om)
6845440Swnj 	struct tcpcb *tp;
6855440Swnj 	struct mbuf *om;
6865419Swnj {
6875440Swnj 	register u_char *cp;
6885440Swnj 	int opt, optlen, cnt;
6895419Swnj 
6905440Swnj 	cp = mtod(om, u_char *);
6915440Swnj 	cnt = om->m_len;
6925440Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
6935440Swnj 		opt = cp[0];
6945440Swnj 		if (opt == TCPOPT_EOL)
6955440Swnj 			break;
6965440Swnj 		if (opt == TCPOPT_NOP)
6975440Swnj 			optlen = 1;
6985440Swnj 		else
6995440Swnj 			optlen = cp[1];
7005440Swnj 		switch (opt) {
7015440Swnj 
7025440Swnj 		default:
7035440Swnj 			break;
7045440Swnj 
7055440Swnj 		case TCPOPT_MAXSEG:
7065440Swnj 			if (optlen != 4)
7075440Swnj 				continue;
7085440Swnj 			tp->t_maxseg = *(u_short *)(cp + 2);
7095440Swnj #if vax
7105440Swnj 			tp->t_maxseg = ntohs(tp->t_maxseg);
7115440Swnj #endif
7125440Swnj 			break;
7135440Swnj 
7145440Swnj #ifdef TCPTRUEOOB
7155440Swnj 		case TCPOPT_WILLOOB:
7165440Swnj 			tp->t_flags |= TF_DOOOB;
7175440Swnj printf("tp %x dooob\n", tp);
7185440Swnj 			break;
7195440Swnj 
7205440Swnj 		case TCPOPT_OOBDATA: {
7215440Swnj 			int seq;
7225547Swnj 			register struct socket *so = tp->t_inpcb->inp_socket;
7235547Swnj 			tcp_seq mark;
7245440Swnj 
7255547Swnj 			if (optlen != 8)
7265440Swnj 				continue;
7275440Swnj 			seq = cp[2];
7285440Swnj 			if (seq < tp->t_iobseq)
7295440Swnj 				seq += 256;
7305440Swnj printf("oobdata cp[2] %d iobseq %d seq %d\n", cp[2], tp->t_iobseq, seq);
7315440Swnj 			if (seq - tp->t_iobseq > 128) {
7325440Swnj printf("bad seq\n");
7335440Swnj 				tp->t_oobflags |= TCPOOB_OWEACK;
7345440Swnj 				break;
7355440Swnj 			}
7365440Swnj 			tp->t_iobseq = cp[2];
7375440Swnj 			tp->t_iobc = cp[3];
7385547Swnj 			mark = *(tcp_seq *)(cp + 4);
7395547Swnj #if vax
7405547Swnj 			mark = ntohl(mark);
7415547Swnj #endif
7425547Swnj 			so->so_oobmark = so->so_rcv.sb_cc + (mark-tp->rcv_nxt);
7435547Swnj 			if (so->so_oobmark == 0)
7445547Swnj 				so->so_state |= SS_RCVATMARK;
7455440Swnj printf("take oob data %x input iobseq now %x\n", tp->t_iobc, tp->t_iobseq);
7465547Swnj 			sohasoutofband(so);
7475440Swnj 			break;
7485419Swnj 		}
7495440Swnj 
7505440Swnj 		case TCPOPT_OOBACK: {
7515440Swnj 			int seq;
7525440Swnj 
7535440Swnj 			if (optlen != 4)
7545440Swnj 				continue;
7555440Swnj 			if (tp->t_oobseq != cp[2]) {
7565440Swnj printf("wrong ack\n");
7575440Swnj 				break;
7585440Swnj 			}
7595440Swnj printf("take oob ack %x and cancel rexmt\n", cp[2]);
7605440Swnj 			tp->t_oobflags &= ~TCPOOB_NEEDACK;
7615440Swnj 			tp->t_timer[TCPT_OOBREXMT] = 0;
7625419Swnj 			break;
7635440Swnj 		}
7645440Swnj #endif TCPTRUEOOB
7655440Swnj 		}
7665419Swnj 	}
7675440Swnj 	m_free(om);
7685419Swnj }
7695419Swnj 
7705419Swnj /*
7715547Swnj  * Pull out of band byte out of a segment so
7725547Swnj  * it doesn't appear in the user's data queue.
7735547Swnj  * It is still reflected in the segment length for
7745547Swnj  * sequencing purposes.
7755547Swnj  */
7765547Swnj tcp_pulloutofband(so, ti)
7775547Swnj 	struct socket *so;
7785547Swnj 	struct tcpiphdr *ti;
7795547Swnj {
7805547Swnj 	register struct mbuf *m;
7815547Swnj 	int cnt = sizeof (struct tcpiphdr) + ti->ti_urp - 1;
7825547Swnj 
7835547Swnj 	m = dtom(ti);
7845547Swnj 	while (cnt >= 0) {
7855547Swnj 		if (m->m_len > cnt) {
7865547Swnj 			char *cp = mtod(m, caddr_t) + cnt;
7875547Swnj 			struct tcpcb *tp = sototcpcb(so);
7885547Swnj 
7895547Swnj 			tp->t_iobc = *cp;
7905547Swnj 			tp->t_oobflags |= TCPOOB_HAVEDATA;
7915547Swnj 			bcopy(cp+1, cp, m->m_len - cnt - 1);
7925547Swnj 			m->m_len--;
7935547Swnj 			return;
7945547Swnj 		}
7955547Swnj 		cnt -= m->m_len;
7965547Swnj 		m = m->m_next;
7975547Swnj 		if (m == 0)
7985547Swnj 			break;
7995547Swnj 	}
8005547Swnj 	panic("tcp_pulloutofband");
8015547Swnj }
8025547Swnj 
8035547Swnj /*
8045065Swnj  * Insert segment ti into reassembly queue of tcp with
8055065Swnj  * control block tp.  Return TH_FIN if reassembly now includes
8065065Swnj  * a segment with FIN.
8075065Swnj  */
8085109Swnj tcp_reass(tp, ti)
8095065Swnj 	register struct tcpcb *tp;
8105065Swnj 	register struct tcpiphdr *ti;
8115065Swnj {
8125065Swnj 	register struct tcpiphdr *q;
8135085Swnj 	struct socket *so = tp->t_inpcb->inp_socket;
8145263Swnj 	struct mbuf *m;
8155263Swnj 	int flags;
8165085Swnj COUNT(TCP_REASS);
8175065Swnj 
8185065Swnj 	/*
8195162Swnj 	 * Call with ti==0 after become established to
8205162Swnj 	 * force pre-ESTABLISHED data up to user socket.
8215065Swnj 	 */
8225162Swnj 	if (ti == 0)
8235065Swnj 		goto present;
8244601Swnj 
8255065Swnj 	/*
8265065Swnj 	 * Find a segment which begins after this one does.
8275065Swnj 	 */
8285065Swnj 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
8295065Swnj 	    q = (struct tcpiphdr *)q->ti_next)
8305065Swnj 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
8315065Swnj 			break;
8324601Swnj 
8335065Swnj 	/*
8345065Swnj 	 * If there is a preceding segment, it may provide some of
8355065Swnj 	 * our data already.  If so, drop the data from the incoming
8365065Swnj 	 * segment.  If it provides all of our data, drop us.
8375065Swnj 	 */
8385065Swnj 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
8395065Swnj 		register int i;
840*5690Swnj 		q = (struct tcpiphdr *)q->ti_prev;
8415065Swnj 		/* conversion to int (in i) handles seq wraparound */
8425065Swnj 		i = q->ti_seq + q->ti_len - ti->ti_seq;
8435065Swnj 		if (i > 0) {
8444924Swnj 			if (i >= ti->ti_len)
8455065Swnj 				goto drop;
8465065Swnj 			m_adj(dtom(tp), i);
8475065Swnj 			ti->ti_len -= i;
8484924Swnj 			ti->ti_seq += i;
8494601Swnj 		}
8505065Swnj 		q = (struct tcpiphdr *)(q->ti_next);
8515065Swnj 	}
8524601Swnj 
8535065Swnj 	/*
8545065Swnj 	 * While we overlap succeeding segments trim them or,
8555065Swnj 	 * if they are completely covered, dequeue them.
8565065Swnj 	 */
857*5690Swnj 	while (q != (struct tcpiphdr *)tp) {
8585065Swnj 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
859*5690Swnj 		if (i <= 0)
860*5690Swnj 			break;
8615065Swnj 		if (i < q->ti_len) {
862*5690Swnj 			q->ti_seq += i;
8635065Swnj 			q->ti_len -= i;
8645065Swnj 			m_adj(dtom(q), i);
8655065Swnj 			break;
8664601Swnj 		}
8675065Swnj 		q = (struct tcpiphdr *)q->ti_next;
8685623Swnj 		m = dtom(q->ti_prev);
8695065Swnj 		remque(q->ti_prev);
8705623Swnj 		m_freem(m);
8715065Swnj 	}
8724601Swnj 
8735065Swnj 	/*
8745065Swnj 	 * Stick new segment in its place.
8755065Swnj 	 */
8765065Swnj 	insque(ti, q->ti_prev);
8774601Swnj 
8785065Swnj present:
8795065Swnj 	/*
8805244Sroot 	 * Present data to user, advancing rcv_nxt through
8815244Sroot 	 * completed sequence space.
8825065Swnj 	 */
8835263Swnj 	if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
8845244Sroot 		return (0);
8854924Swnj 	ti = tp->seg_next;
8865263Swnj 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
8875263Swnj 		return (0);
8885263Swnj 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
8895263Swnj 		return (0);
8905263Swnj 	do {
8915244Sroot 		tp->rcv_nxt += ti->ti_len;
8925244Sroot 		flags = ti->ti_flags & TH_FIN;
8934924Swnj 		remque(ti);
8945263Swnj 		m = dtom(ti);
8954924Swnj 		ti = (struct tcpiphdr *)ti->ti_next;
8965263Swnj 		if (so->so_state & SS_CANTRCVMORE)
8975263Swnj 			(void) m_freem(m);
8985263Swnj 		else
8995263Swnj 			sbappend(&so->so_rcv, m);
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