xref: /csrg-svn/sys/netinet/tcp_input.c (revision 5994)
1*5994Swnj /*	tcp_input.c	1.55	82/02/27	*/
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 	/*
125*5994Swnj 	 * Locate pcb for segment.  On match, update the local
126*5994Swnj 	 * address stored in the block to reflect anchoring.
1274924Swnj 	 */
1285065Swnj 	inp = in_pcblookup
129*5994Swnj 		(&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport, 1);
1305065Swnj 
1315065Swnj 	/*
1325065Swnj 	 * If the state is CLOSED (i.e., TCB does not exist) then
1335244Sroot 	 * all data in the incoming segment is discarded.
1345065Swnj 	 */
1355300Sroot 	if (inp == 0)
1365085Swnj 		goto dropwithreset;
1375065Swnj 	tp = intotcpcb(inp);
1385300Sroot 	if (tp == 0)
1395085Swnj 		goto dropwithreset;
1405109Swnj 	so = inp->inp_socket;
1415267Sroot 	if (so->so_options & SO_DEBUG) {
1425267Sroot 		ostate = tp->t_state;
1435267Sroot 		tcp_saveti = *ti;
1445267Sroot 	}
1454601Swnj 
1464601Swnj 	/*
1475162Swnj 	 * Segment received on connection.
1485162Swnj 	 * Reset idle time and keep-alive timer.
1495162Swnj 	 */
1505162Swnj 	tp->t_idle = 0;
1515162Swnj 	tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
1525162Swnj 
1535162Swnj 	/*
1545440Swnj 	 * Process options.
1555440Swnj 	 */
1565440Swnj 	if (om) {
1575440Swnj 		tcp_dooptions(tp, om);
1585440Swnj 		om = 0;
1595440Swnj 	}
1605440Swnj 
1615440Swnj 	/*
1625085Swnj 	 * Calculate amount of space in receive window,
1635085Swnj 	 * and then do TCP input processing.
1644601Swnj 	 */
1655085Swnj 	tp->rcv_wnd = sbspace(&so->so_rcv);
1665231Swnj 	if (tp->rcv_wnd < 0)
1675231Swnj 		tp->rcv_wnd = 0;
1684601Swnj 
1694601Swnj 	switch (tp->t_state) {
1704601Swnj 
1715065Swnj 	/*
1725065Swnj 	 * If the state is LISTEN then ignore segment if it contains an RST.
1735065Swnj 	 * If the segment contains an ACK then it is bad and send a RST.
1745065Swnj 	 * If it does not contain a SYN then it is not interesting; drop it.
1755085Swnj 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
1765065Swnj 	 * tp->iss, and send a segment:
1775085Swnj 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
1785065Swnj 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
1795065Swnj 	 * Fill in remote peer address fields if not previously specified.
1805065Swnj 	 * Enter SYN_RECEIVED state, and process any other fields of this
1815244Sroot 	 * segment in this state.
1825065Swnj 	 */
1835065Swnj 	case TCPS_LISTEN:
1845065Swnj 		if (tiflags & TH_RST)
1855065Swnj 			goto drop;
1865300Sroot 		if (tiflags & TH_ACK)
1875085Swnj 			goto dropwithreset;
1885300Sroot 		if ((tiflags & TH_SYN) == 0)
1895065Swnj 			goto drop;
1905244Sroot 		tcp_in.sin_addr = ti->ti_src;
1915244Sroot 		tcp_in.sin_port = ti->ti_sport;
1925300Sroot 		if (in_pcbconnect(inp, (struct sockaddr *)&tcp_in))
1935244Sroot 			goto drop;
1945244Sroot 		tp->t_template = tcp_template(tp);
1955244Sroot 		if (tp->t_template == 0) {
1965244Sroot 			in_pcbdisconnect(inp);
1975244Sroot 			goto drop;
1985244Sroot 		}
1995085Swnj 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
2005065Swnj 		tp->irs = ti->ti_seq;
2015085Swnj 		tcp_sendseqinit(tp);
2025085Swnj 		tcp_rcvseqinit(tp);
2035065Swnj 		tp->t_state = TCPS_SYN_RECEIVED;
2045244Sroot 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
2055085Swnj 		goto trimthenstep6;
2064601Swnj 
2075065Swnj 	/*
2085065Swnj 	 * If the state is SYN_SENT:
2095065Swnj 	 *	if seg contains an ACK, but not for our SYN, drop the input.
2105065Swnj 	 *	if seg contains a RST, then drop the connection.
2115065Swnj 	 *	if seg does not contain SYN, then drop it.
2125065Swnj 	 * Otherwise this is an acceptable SYN segment
2135065Swnj 	 *	initialize tp->rcv_nxt and tp->irs
2145065Swnj 	 *	if seg contains ack then advance tp->snd_una
2155065Swnj 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
2165065Swnj 	 *	arrange for segment to be acked (eventually)
2175065Swnj 	 *	continue processing rest of data/controls, beginning with URG
2185065Swnj 	 */
2195065Swnj 	case TCPS_SYN_SENT:
2205065Swnj 		if ((tiflags & TH_ACK) &&
2215300Sroot /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */
2225300Sroot 		    (SEQ_LT(ti->ti_ack, tp->iss) ||
2235231Swnj 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
2245085Swnj 			goto dropwithreset;
2255065Swnj 		if (tiflags & TH_RST) {
2265065Swnj 			if (tiflags & TH_ACK)
2275267Sroot 				tcp_drop(tp, ECONNREFUSED);
2285065Swnj 			goto drop;
2294601Swnj 		}
2305065Swnj 		if ((tiflags & TH_SYN) == 0)
2315065Swnj 			goto drop;
2325231Swnj 		tp->snd_una = ti->ti_ack;
2335357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2345357Sroot 			tp->snd_nxt = tp->snd_una;
2355244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
2365065Swnj 		tp->irs = ti->ti_seq;
2375085Swnj 		tcp_rcvseqinit(tp);
2385085Swnj 		tp->t_flags |= TF_ACKNOW;
2395162Swnj 		if (SEQ_GT(tp->snd_una, tp->iss)) {
2405391Swnj 			if (so->so_options & SO_ACCEPTCONN)
2415391Swnj 				so->so_state |= SS_CONNAWAITING;
2425244Sroot 			soisconnected(so);
2435065Swnj 			tp->t_state = TCPS_ESTABLISHED;
2445162Swnj 			(void) tcp_reass(tp, (struct tcpiphdr *)0);
2455162Swnj 		} else
2465085Swnj 			tp->t_state = TCPS_SYN_RECEIVED;
2475085Swnj 		goto trimthenstep6;
2485085Swnj 
2495085Swnj trimthenstep6:
2505085Swnj 		/*
2515231Swnj 		 * Advance ti->ti_seq to correspond to first data byte.
2525085Swnj 		 * If data, trim to stay within window,
2535085Swnj 		 * dropping FIN if necessary.
2545085Swnj 		 */
2555231Swnj 		ti->ti_seq++;
2565085Swnj 		if (ti->ti_len > tp->rcv_wnd) {
2575085Swnj 			todrop = ti->ti_len - tp->rcv_wnd;
2585085Swnj 			m_adj(m, -todrop);
2595085Swnj 			ti->ti_len = tp->rcv_wnd;
2605085Swnj 			ti->ti_flags &= ~TH_FIN;
2615065Swnj 		}
2625263Swnj 		tp->snd_wl1 = ti->ti_seq - 1;
2635085Swnj 		goto step6;
2645065Swnj 	}
2654601Swnj 
2665065Swnj 	/*
2675065Swnj 	 * States other than LISTEN or SYN_SENT.
2685065Swnj 	 * First check that at least some bytes of segment are within
2695065Swnj 	 * receive window.
2705065Swnj 	 */
2715065Swnj 	if (tp->rcv_wnd == 0) {
2725065Swnj 		/*
2735065Swnj 		 * If window is closed can only take segments at
2745231Swnj 		 * window edge, and have to drop data and PUSH from
2755065Swnj 		 * incoming segments.
2765065Swnj 		 */
2775300Sroot 		if (tp->rcv_nxt != ti->ti_seq)
2785065Swnj 			goto dropafterack;
2795085Swnj 		if (ti->ti_len > 0) {
2805690Swnj 			m_adj(m, ti->ti_len);
2815085Swnj 			ti->ti_len = 0;
2825085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
2835065Swnj 		}
2845065Swnj 	} else {
2855065Swnj 		/*
2865231Swnj 		 * If segment begins before rcv_nxt, drop leading
2875065Swnj 		 * data (and SYN); if nothing left, just ack.
2885065Swnj 		 */
2895690Swnj 		todrop = tp->rcv_nxt - ti->ti_seq;
2905690Swnj 		if (todrop > 0) {
2915085Swnj 			if (tiflags & TH_SYN) {
2925300Sroot 				tiflags &= ~TH_SYN;
2935690Swnj 				ti->ti_flags &= ~TH_SYN;
2945085Swnj 				ti->ti_seq++;
2955085Swnj 				if (ti->ti_urp > 1)
2965085Swnj 					ti->ti_urp--;
2975085Swnj 				else
2985085Swnj 					tiflags &= ~TH_URG;
2995085Swnj 				todrop--;
3005085Swnj 			}
3015300Sroot 			if (todrop > ti->ti_len)
3025065Swnj 				goto dropafterack;
3035065Swnj 			m_adj(m, todrop);
3045065Swnj 			ti->ti_seq += todrop;
3055065Swnj 			ti->ti_len -= todrop;
3065085Swnj 			if (ti->ti_urp > todrop)
3075085Swnj 				ti->ti_urp -= todrop;
3085085Swnj 			else {
3095085Swnj 				tiflags &= ~TH_URG;
3105690Swnj 				ti->ti_flags &= ~TH_URG;
3115690Swnj 				ti->ti_urp = 0;
3125085Swnj 			}
3135065Swnj 		}
3145065Swnj 		/*
3155065Swnj 		 * If segment ends after window, drop trailing data
3165085Swnj 		 * (and PUSH and FIN); if nothing left, just ACK.
3175065Swnj 		 */
3185690Swnj 		todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
3195690Swnj 		if (todrop > 0) {
3205300Sroot 			if (todrop > ti->ti_len)
3215065Swnj 				goto dropafterack;
3225065Swnj 			m_adj(m, -todrop);
3235065Swnj 			ti->ti_len -= todrop;
3245085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
3255065Swnj 		}
3265065Swnj 	}
3274601Swnj 
3285065Swnj 	/*
3295951Swnj 	 * If a segment is received on a connection after the
3305951Swnj 	 * user processes are gone, then RST the other end.
3315951Swnj 	 */
3325951Swnj 	if (so->so_state & SS_USERGONE) {
3335951Swnj 		tcp_close(tp);
3345951Swnj 		goto dropwithreset;
3355951Swnj 	}
3365951Swnj 
3375951Swnj 	/*
3385065Swnj 	 * If the RST bit is set examine the state:
3395065Swnj 	 *    SYN_RECEIVED STATE:
3405065Swnj 	 *	If passive open, return to LISTEN state.
3415065Swnj 	 *	If active open, inform user that connection was refused.
3425065Swnj 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
3435065Swnj 	 *	Inform user that connection was reset, and close tcb.
3445065Swnj 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
3455065Swnj 	 *	Close the tcb.
3465065Swnj 	 */
3475065Swnj 	if (tiflags&TH_RST) switch (tp->t_state) {
3485267Sroot 
3495065Swnj 	case TCPS_SYN_RECEIVED:
3505065Swnj 		if (inp->inp_socket->so_options & SO_ACCEPTCONN) {
3515267Sroot 			/* a miniature tcp_close, but invisible to user */
3525267Sroot 			(void) m_free(dtom(tp->t_template));
3535267Sroot 			(void) m_free(dtom(tp));
3545267Sroot 			inp->inp_ppcb = 0;
3555267Sroot 			tp = tcp_newtcpcb(inp);
3565085Swnj 			tp->t_state = TCPS_LISTEN;
3575065Swnj 			goto drop;
3584601Swnj 		}
3595085Swnj 		tcp_drop(tp, ECONNREFUSED);
3605065Swnj 		goto drop;
3614601Swnj 
3625065Swnj 	case TCPS_ESTABLISHED:
3635065Swnj 	case TCPS_FIN_WAIT_1:
3645065Swnj 	case TCPS_FIN_WAIT_2:
3655065Swnj 	case TCPS_CLOSE_WAIT:
3665065Swnj 		tcp_drop(tp, ECONNRESET);
3675065Swnj 		goto drop;
3685065Swnj 
3695065Swnj 	case TCPS_CLOSING:
3705065Swnj 	case TCPS_LAST_ACK:
3715065Swnj 	case TCPS_TIME_WAIT:
3725065Swnj 		tcp_close(tp);
3735065Swnj 		goto drop;
3744601Swnj 	}
3754601Swnj 
3764601Swnj 	/*
3775065Swnj 	 * If a SYN is in the window, then this is an
3785065Swnj 	 * error and we send an RST and drop the connection.
3794601Swnj 	 */
3805065Swnj 	if (tiflags & TH_SYN) {
3815231Swnj 		tcp_drop(tp, ECONNRESET);
3825085Swnj 		goto dropwithreset;
3834601Swnj 	}
3844601Swnj 
3854601Swnj 	/*
3865065Swnj 	 * If the ACK bit is off we drop the segment and return.
3874601Swnj 	 */
3885085Swnj 	if ((tiflags & TH_ACK) == 0)
3895065Swnj 		goto drop;
3905065Swnj 
3915065Swnj 	/*
3925065Swnj 	 * Ack processing.
3935065Swnj 	 */
3944601Swnj 	switch (tp->t_state) {
3954601Swnj 
3965065Swnj 	/*
3975065Swnj 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
3985065Swnj 	 * ESTABLISHED state and continue processing, othewise
3995065Swnj 	 * send an RST.
4005065Swnj 	 */
4015065Swnj 	case TCPS_SYN_RECEIVED:
4025085Swnj 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
4035231Swnj 		    SEQ_GT(ti->ti_ack, tp->snd_max))
4045085Swnj 			goto dropwithreset;
4055244Sroot 		tp->snd_una++;			/* SYN acked */
4065357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
4075357Sroot 			tp->snd_nxt = tp->snd_una;
4085244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
4095391Swnj 		if (so->so_options & SO_ACCEPTCONN)
4105391Swnj 			so->so_state |= SS_CONNAWAITING;
4115085Swnj 		soisconnected(so);
4125085Swnj 		tp->t_state = TCPS_ESTABLISHED;
4135162Swnj 		(void) tcp_reass(tp, (struct tcpiphdr *)0);
4145244Sroot 		tp->snd_wl1 = ti->ti_seq - 1;
4155085Swnj 		/* fall into ... */
4164601Swnj 
4175065Swnj 	/*
4185065Swnj 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
4195065Swnj 	 * ACKs.  If the ack is in the range
4205231Swnj 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
4215065Swnj 	 * then advance tp->snd_una to ti->ti_ack and drop
4225065Swnj 	 * data from the retransmission queue.  If this ACK reflects
4235065Swnj 	 * more up to date window information we update our window information.
4245065Swnj 	 */
4255065Swnj 	case TCPS_ESTABLISHED:
4265065Swnj 	case TCPS_FIN_WAIT_1:
4275065Swnj 	case TCPS_FIN_WAIT_2:
4285065Swnj 	case TCPS_CLOSE_WAIT:
4295065Swnj 	case TCPS_CLOSING:
4305244Sroot 	case TCPS_LAST_ACK:
4315244Sroot 	case TCPS_TIME_WAIT:
4325085Swnj #define	ourfinisacked	(acked > 0)
4335085Swnj 
4345244Sroot 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una))
4355065Swnj 			break;
4365300Sroot 		if (SEQ_GT(ti->ti_ack, tp->snd_max))
4375065Swnj 			goto dropafterack;
4385085Swnj 		acked = ti->ti_ack - tp->snd_una;
4395951Swnj 
4405951Swnj 		/*
4415951Swnj 		 * If transmit timer is running and timed sequence
4425951Swnj 		 * number was acked, update smoothed round trip time.
4435951Swnj 		 */
4445951Swnj 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
4455951Swnj 			if (tp->t_srtt == 0)
4465951Swnj 				tp->t_srtt = tp->t_rtt;
4475951Swnj 			else
4485951Swnj 				tp->t_srtt =
4495951Swnj 				    tcp_alpha * tp->t_srtt +
4505951Swnj 				    (1 - tcp_alpha) * tp->t_rtt;
4515951Swnj /* printf("rtt %d srtt*100 now %d\n", tp->t_rtt, (int)(tp->t_srtt*100)); */
4525951Swnj 			tp->t_rtt = 0;
4535951Swnj 		}
4545951Swnj 
4555307Sroot 		if (ti->ti_ack == tp->snd_max)
4565244Sroot 			tp->t_timer[TCPT_REXMT] = 0;
4575307Sroot 		else {
4585244Sroot 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
4595244Sroot 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
4605951Swnj 			tp->t_rtt = 1;
4615300Sroot 			tp->t_rxtshift = 0;
4625085Swnj 		}
4635307Sroot 		if (acked > so->so_snd.sb_cc) {
4645307Sroot 			sbdrop(&so->so_snd, so->so_snd.sb_cc);
4655307Sroot 			tp->snd_wnd -= so->so_snd.sb_cc;
4665307Sroot 		} else {
4675307Sroot 			sbdrop(&so->so_snd.sb_cc, acked);
4685307Sroot 			tp->snd_wnd -= acked;
4695307Sroot 			acked = 0;
4705307Sroot 		}
4715300Sroot 		if (so->so_snd.sb_flags & SB_WAIT)
4725300Sroot 			sowwakeup(so);
4735231Swnj 		tp->snd_una = ti->ti_ack;
4745357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
4755357Sroot 			tp->snd_nxt = tp->snd_una;
4765162Swnj 
4774601Swnj 		switch (tp->t_state) {
4784601Swnj 
4795065Swnj 		/*
4805065Swnj 		 * In FIN_WAIT_1 STATE in addition to the processing
4815065Swnj 		 * for the ESTABLISHED state if our FIN is now acknowledged
4825085Swnj 		 * then enter FIN_WAIT_2.
4835065Swnj 		 */
4845065Swnj 		case TCPS_FIN_WAIT_1:
4855896Swnj 			if (ourfinisacked) {
4865896Swnj 				/*
4875896Swnj 				 * If we can't receive any more
4885896Swnj 				 * data, then closing user can proceed.
4895896Swnj 				 */
4905896Swnj 				if (so->so_state & SS_CANTRCVMORE)
4915896Swnj 					soisdisconnected(so);
4925085Swnj 				tp->t_state = TCPS_FIN_WAIT_2;
4935896Swnj 			}
4944601Swnj 			break;
4954601Swnj 
4965065Swnj 	 	/*
4975065Swnj 		 * In CLOSING STATE in addition to the processing for
4985065Swnj 		 * the ESTABLISHED state if the ACK acknowledges our FIN
4995065Swnj 		 * then enter the TIME-WAIT state, otherwise ignore
5005065Swnj 		 * the segment.
5015065Swnj 		 */
5025065Swnj 		case TCPS_CLOSING:
5035244Sroot 			if (ourfinisacked) {
5045065Swnj 				tp->t_state = TCPS_TIME_WAIT;
5055244Sroot 				tcp_canceltimers(tp);
5065244Sroot 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
5075244Sroot 				soisdisconnected(so);
5085244Sroot 			}
5095244Sroot 			break;
5104601Swnj 
5115065Swnj 		/*
5125085Swnj 		 * The only thing that can arrive in  LAST_ACK state
5135085Swnj 		 * is an acknowledgment of our FIN.  If our FIN is now
5145085Swnj 		 * acknowledged, delete the TCB, enter the closed state
5155085Swnj 		 * and return.
5165065Swnj 		 */
5175065Swnj 		case TCPS_LAST_ACK:
5185251Sroot 			if (ourfinisacked)
5195065Swnj 				tcp_close(tp);
5205065Swnj 			goto drop;
5214601Swnj 
5225065Swnj 		/*
5235065Swnj 		 * In TIME_WAIT state the only thing that should arrive
5245065Swnj 		 * is a retransmission of the remote FIN.  Acknowledge
5255065Swnj 		 * it and restart the finack timer.
5265065Swnj 		 */
5275065Swnj 		case TCPS_TIME_WAIT:
5285162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
5295065Swnj 			goto dropafterack;
5304601Swnj 		}
5315085Swnj #undef ourfinisacked
5325085Swnj 	}
5334601Swnj 
5345065Swnj step6:
5355065Swnj 	/*
5365244Sroot 	 * Update window information.
5375244Sroot 	 */
5385300Sroot 	if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
5395391Swnj 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
5405300Sroot 	     tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) {
5415244Sroot 		tp->snd_wnd = ti->ti_win;
5425244Sroot 		tp->snd_wl1 = ti->ti_seq;
5435244Sroot 		tp->snd_wl2 = ti->ti_ack;
5445244Sroot 		if (tp->snd_wnd > 0)
5455244Sroot 			tp->t_timer[TCPT_PERSIST] = 0;
5465244Sroot 	}
5475244Sroot 
5485244Sroot 	/*
5495547Swnj 	 * Process segments with URG.
5505065Swnj 	 */
5515547Swnj 	if ((tiflags & TH_URG) && TCPS_HAVERCVDFIN(tp->t_state) == 0) {
5525547Swnj 		/*
5535547Swnj 		 * If this segment advances the known urgent pointer,
5545547Swnj 		 * then mark the data stream.  This should not happen
5555547Swnj 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
5565547Swnj 		 * a FIN has been received from the remote side.
5575547Swnj 		 * In these states we ignore the URG.
5585547Swnj 		 */
5595547Swnj 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
5605547Swnj 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
5615547Swnj 			so->so_oobmark = so->so_rcv.sb_cc +
5625547Swnj 			    (tp->rcv_up - tp->rcv_nxt) - 1;
5635547Swnj 			if (so->so_oobmark == 0)
5645547Swnj 				so->so_state |= SS_RCVATMARK;
5655440Swnj #ifdef TCPTRUEOOB
5665547Swnj 			if ((tp->t_flags & TF_DOOOB) == 0)
5675440Swnj #endif
5685547Swnj 				sohasoutofband(so);
5695547Swnj 			tp->t_oobflags &= ~TCPOOB_HAVEDATA;
5705440Swnj 		}
5715547Swnj 		/*
5725547Swnj 		 * Remove out of band data so doesn't get presented to user.
5735547Swnj 		 * This can happen independent of advancing the URG pointer,
5745547Swnj 		 * but if two URG's are pending at once, some out-of-band
5755547Swnj 		 * data may creep in... ick.
5765547Swnj 		 */
5775547Swnj 		if (ti->ti_urp <= ti->ti_len) {
5785547Swnj 			tcp_pulloutofband(so, ti);
5795547Swnj 		}
5805419Swnj 	}
5814601Swnj 
5824601Swnj 	/*
5835065Swnj 	 * Process the segment text, merging it into the TCP sequencing queue,
5845065Swnj 	 * and arranging for acknowledgment of receipt if necessary.
5855065Swnj 	 * This process logically involves adjusting tp->rcv_wnd as data
5865065Swnj 	 * is presented to the user (this happens in tcp_usrreq.c,
5875065Swnj 	 * case PRU_RCVD).  If a FIN has already been received on this
5885065Swnj 	 * connection then we just ignore the text.
5894601Swnj 	 */
5905263Swnj 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
5915263Swnj 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
5925085Swnj 		off += sizeof (struct ip);		/* drop IP header */
5935085Swnj 		m->m_off += off;
5945085Swnj 		m->m_len -= off;
5955065Swnj 		tiflags = tcp_reass(tp, ti);
5965440Swnj 		if (tcpnodelack == 0)
5975440Swnj 			tp->t_flags |= TF_DELACK;
5985440Swnj 		else
5995440Swnj 			tp->t_flags |= TF_ACKNOW;
6005244Sroot 	} else {
6014924Swnj 		m_freem(m);
6025263Swnj 		tiflags &= ~TH_FIN;
6035244Sroot 	}
6044601Swnj 
6054601Swnj 	/*
6065263Swnj 	 * If FIN is received ACK the FIN and let the user know
6075263Swnj 	 * that the connection is closing.
6084601Swnj 	 */
6095263Swnj 	if (tiflags & TH_FIN) {
6105244Sroot 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
6115244Sroot 			socantrcvmore(so);
6125244Sroot 			tp->t_flags |= TF_ACKNOW;
6135244Sroot 			tp->rcv_nxt++;
6145244Sroot 		}
6155065Swnj 		switch (tp->t_state) {
6164601Swnj 
6175065Swnj 	 	/*
6185065Swnj 		 * In SYN_RECEIVED and ESTABLISHED STATES
6195065Swnj 		 * enter the CLOSE_WAIT state.
6204884Swnj 		 */
6215065Swnj 		case TCPS_SYN_RECEIVED:
6225065Swnj 		case TCPS_ESTABLISHED:
6235065Swnj 			tp->t_state = TCPS_CLOSE_WAIT;
6245065Swnj 			break;
6254884Swnj 
6265065Swnj 	 	/*
6275085Swnj 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
6285085Swnj 		 * enter the CLOSING state.
6294884Swnj 		 */
6305065Swnj 		case TCPS_FIN_WAIT_1:
6315085Swnj 			tp->t_state = TCPS_CLOSING;
6325065Swnj 			break;
6334601Swnj 
6345065Swnj 	 	/*
6355065Swnj 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
6365065Swnj 		 * starting the time-wait timer, turning off the other
6375065Swnj 		 * standard timers.
6385065Swnj 		 */
6395065Swnj 		case TCPS_FIN_WAIT_2:
6405244Sroot 			tp->t_state = TCPS_TIME_WAIT;
6415074Swnj 			tcp_canceltimers(tp);
6425162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
6435244Sroot 			soisdisconnected(so);
6445065Swnj 			break;
6455065Swnj 
6464884Swnj 		/*
6475065Swnj 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
6484884Swnj 		 */
6495065Swnj 		case TCPS_TIME_WAIT:
6505162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
6515065Swnj 			break;
6525085Swnj 		}
6534601Swnj 	}
6545267Sroot 	if (so->so_options & SO_DEBUG)
6555267Sroot 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
6565085Swnj 
6575085Swnj 	/*
6585085Swnj 	 * Return any desired output.
6595085Swnj 	 */
6605085Swnj 	tcp_output(tp);
6615065Swnj 	return;
6625085Swnj 
6635065Swnj dropafterack:
6645085Swnj 	/*
6655244Sroot 	 * Generate an ACK dropping incoming segment.
6665085Swnj 	 * Make ACK reflect our state.
6675085Swnj 	 */
6685085Swnj 	if (tiflags & TH_RST)
6695085Swnj 		goto drop;
6705391Swnj 	tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK);
6715231Swnj 	return;
6725085Swnj 
6735085Swnj dropwithreset:
6745440Swnj 	if (om)
6755440Swnj 		m_free(om);
6765085Swnj 	/*
6775244Sroot 	 * Generate a RST, dropping incoming segment.
6785085Swnj 	 * Make ACK acceptable to originator of segment.
6795085Swnj 	 */
6805085Swnj 	if (tiflags & TH_RST)
6815085Swnj 		goto drop;
6825085Swnj 	if (tiflags & TH_ACK)
6835391Swnj 		tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST);
6845085Swnj 	else {
6855085Swnj 		if (tiflags & TH_SYN)
6865085Swnj 			ti->ti_len++;
6875391Swnj 		tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK);
6885085Swnj 	}
6895231Swnj 	return;
6905085Swnj 
6915065Swnj drop:
6925085Swnj 	/*
6935085Swnj 	 * Drop space held by incoming segment and return.
6945085Swnj 	 */
6955065Swnj 	m_freem(m);
6965267Sroot 	return;
6975065Swnj }
6985065Swnj 
6995440Swnj tcp_dooptions(tp, om)
7005440Swnj 	struct tcpcb *tp;
7015440Swnj 	struct mbuf *om;
7025419Swnj {
7035440Swnj 	register u_char *cp;
7045440Swnj 	int opt, optlen, cnt;
7055419Swnj 
7065440Swnj 	cp = mtod(om, u_char *);
7075440Swnj 	cnt = om->m_len;
7085440Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
7095440Swnj 		opt = cp[0];
7105440Swnj 		if (opt == TCPOPT_EOL)
7115440Swnj 			break;
7125440Swnj 		if (opt == TCPOPT_NOP)
7135440Swnj 			optlen = 1;
7145440Swnj 		else
7155440Swnj 			optlen = cp[1];
7165440Swnj 		switch (opt) {
7175440Swnj 
7185440Swnj 		default:
7195440Swnj 			break;
7205440Swnj 
7215440Swnj 		case TCPOPT_MAXSEG:
7225440Swnj 			if (optlen != 4)
7235440Swnj 				continue;
7245440Swnj 			tp->t_maxseg = *(u_short *)(cp + 2);
7255440Swnj #if vax
7265440Swnj 			tp->t_maxseg = ntohs(tp->t_maxseg);
7275440Swnj #endif
7285440Swnj 			break;
7295440Swnj 
7305440Swnj #ifdef TCPTRUEOOB
7315440Swnj 		case TCPOPT_WILLOOB:
7325440Swnj 			tp->t_flags |= TF_DOOOB;
7335440Swnj printf("tp %x dooob\n", tp);
7345440Swnj 			break;
7355440Swnj 
7365440Swnj 		case TCPOPT_OOBDATA: {
7375440Swnj 			int seq;
7385547Swnj 			register struct socket *so = tp->t_inpcb->inp_socket;
7395547Swnj 			tcp_seq mark;
7405440Swnj 
7415547Swnj 			if (optlen != 8)
7425440Swnj 				continue;
7435440Swnj 			seq = cp[2];
7445440Swnj 			if (seq < tp->t_iobseq)
7455440Swnj 				seq += 256;
7465440Swnj printf("oobdata cp[2] %d iobseq %d seq %d\n", cp[2], tp->t_iobseq, seq);
7475440Swnj 			if (seq - tp->t_iobseq > 128) {
7485440Swnj printf("bad seq\n");
7495440Swnj 				tp->t_oobflags |= TCPOOB_OWEACK;
7505440Swnj 				break;
7515440Swnj 			}
7525440Swnj 			tp->t_iobseq = cp[2];
7535440Swnj 			tp->t_iobc = cp[3];
7545547Swnj 			mark = *(tcp_seq *)(cp + 4);
7555547Swnj #if vax
7565547Swnj 			mark = ntohl(mark);
7575547Swnj #endif
7585547Swnj 			so->so_oobmark = so->so_rcv.sb_cc + (mark-tp->rcv_nxt);
7595547Swnj 			if (so->so_oobmark == 0)
7605547Swnj 				so->so_state |= SS_RCVATMARK;
7615440Swnj printf("take oob data %x input iobseq now %x\n", tp->t_iobc, tp->t_iobseq);
7625547Swnj 			sohasoutofband(so);
7635440Swnj 			break;
7645419Swnj 		}
7655440Swnj 
7665440Swnj 		case TCPOPT_OOBACK: {
7675440Swnj 			int seq;
7685440Swnj 
7695440Swnj 			if (optlen != 4)
7705440Swnj 				continue;
7715440Swnj 			if (tp->t_oobseq != cp[2]) {
7725440Swnj printf("wrong ack\n");
7735440Swnj 				break;
7745440Swnj 			}
7755440Swnj printf("take oob ack %x and cancel rexmt\n", cp[2]);
7765440Swnj 			tp->t_oobflags &= ~TCPOOB_NEEDACK;
7775440Swnj 			tp->t_timer[TCPT_OOBREXMT] = 0;
7785419Swnj 			break;
7795440Swnj 		}
7805440Swnj #endif TCPTRUEOOB
7815440Swnj 		}
7825419Swnj 	}
7835440Swnj 	m_free(om);
7845419Swnj }
7855419Swnj 
7865419Swnj /*
7875547Swnj  * Pull out of band byte out of a segment so
7885547Swnj  * it doesn't appear in the user's data queue.
7895547Swnj  * It is still reflected in the segment length for
7905547Swnj  * sequencing purposes.
7915547Swnj  */
7925547Swnj tcp_pulloutofband(so, ti)
7935547Swnj 	struct socket *so;
7945547Swnj 	struct tcpiphdr *ti;
7955547Swnj {
7965547Swnj 	register struct mbuf *m;
7975547Swnj 	int cnt = sizeof (struct tcpiphdr) + ti->ti_urp - 1;
7985547Swnj 
7995547Swnj 	m = dtom(ti);
8005547Swnj 	while (cnt >= 0) {
8015547Swnj 		if (m->m_len > cnt) {
8025547Swnj 			char *cp = mtod(m, caddr_t) + cnt;
8035547Swnj 			struct tcpcb *tp = sototcpcb(so);
8045547Swnj 
8055547Swnj 			tp->t_iobc = *cp;
8065547Swnj 			tp->t_oobflags |= TCPOOB_HAVEDATA;
8075547Swnj 			bcopy(cp+1, cp, m->m_len - cnt - 1);
8085547Swnj 			m->m_len--;
8095547Swnj 			return;
8105547Swnj 		}
8115547Swnj 		cnt -= m->m_len;
8125547Swnj 		m = m->m_next;
8135547Swnj 		if (m == 0)
8145547Swnj 			break;
8155547Swnj 	}
8165547Swnj 	panic("tcp_pulloutofband");
8175547Swnj }
8185547Swnj 
8195547Swnj /*
8205065Swnj  * Insert segment ti into reassembly queue of tcp with
8215065Swnj  * control block tp.  Return TH_FIN if reassembly now includes
8225065Swnj  * a segment with FIN.
8235065Swnj  */
8245109Swnj tcp_reass(tp, ti)
8255065Swnj 	register struct tcpcb *tp;
8265065Swnj 	register struct tcpiphdr *ti;
8275065Swnj {
8285065Swnj 	register struct tcpiphdr *q;
8295085Swnj 	struct socket *so = tp->t_inpcb->inp_socket;
8305263Swnj 	struct mbuf *m;
8315263Swnj 	int flags;
8325085Swnj COUNT(TCP_REASS);
8335065Swnj 
8345065Swnj 	/*
8355162Swnj 	 * Call with ti==0 after become established to
8365162Swnj 	 * force pre-ESTABLISHED data up to user socket.
8375065Swnj 	 */
8385162Swnj 	if (ti == 0)
8395065Swnj 		goto present;
8404601Swnj 
8415065Swnj 	/*
8425065Swnj 	 * Find a segment which begins after this one does.
8435065Swnj 	 */
8445065Swnj 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
8455065Swnj 	    q = (struct tcpiphdr *)q->ti_next)
8465065Swnj 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
8475065Swnj 			break;
8484601Swnj 
8495065Swnj 	/*
8505065Swnj 	 * If there is a preceding segment, it may provide some of
8515065Swnj 	 * our data already.  If so, drop the data from the incoming
8525065Swnj 	 * segment.  If it provides all of our data, drop us.
8535065Swnj 	 */
8545065Swnj 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
8555065Swnj 		register int i;
8565690Swnj 		q = (struct tcpiphdr *)q->ti_prev;
8575065Swnj 		/* conversion to int (in i) handles seq wraparound */
8585065Swnj 		i = q->ti_seq + q->ti_len - ti->ti_seq;
8595065Swnj 		if (i > 0) {
8604924Swnj 			if (i >= ti->ti_len)
8615065Swnj 				goto drop;
8625065Swnj 			m_adj(dtom(tp), i);
8635065Swnj 			ti->ti_len -= i;
8644924Swnj 			ti->ti_seq += i;
8654601Swnj 		}
8665065Swnj 		q = (struct tcpiphdr *)(q->ti_next);
8675065Swnj 	}
8684601Swnj 
8695065Swnj 	/*
8705065Swnj 	 * While we overlap succeeding segments trim them or,
8715065Swnj 	 * if they are completely covered, dequeue them.
8725065Swnj 	 */
8735690Swnj 	while (q != (struct tcpiphdr *)tp) {
8745065Swnj 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
8755690Swnj 		if (i <= 0)
8765690Swnj 			break;
8775065Swnj 		if (i < q->ti_len) {
8785690Swnj 			q->ti_seq += i;
8795065Swnj 			q->ti_len -= i;
8805065Swnj 			m_adj(dtom(q), i);
8815065Swnj 			break;
8824601Swnj 		}
8835065Swnj 		q = (struct tcpiphdr *)q->ti_next;
8845623Swnj 		m = dtom(q->ti_prev);
8855065Swnj 		remque(q->ti_prev);
8865623Swnj 		m_freem(m);
8875065Swnj 	}
8884601Swnj 
8895065Swnj 	/*
8905065Swnj 	 * Stick new segment in its place.
8915065Swnj 	 */
8925065Swnj 	insque(ti, q->ti_prev);
8934601Swnj 
8945065Swnj present:
8955065Swnj 	/*
8965244Sroot 	 * Present data to user, advancing rcv_nxt through
8975244Sroot 	 * completed sequence space.
8985065Swnj 	 */
8995263Swnj 	if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
9005244Sroot 		return (0);
9014924Swnj 	ti = tp->seg_next;
9025263Swnj 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
9035263Swnj 		return (0);
9045263Swnj 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
9055263Swnj 		return (0);
9065263Swnj 	do {
9075244Sroot 		tp->rcv_nxt += ti->ti_len;
9085244Sroot 		flags = ti->ti_flags & TH_FIN;
9094924Swnj 		remque(ti);
9105263Swnj 		m = dtom(ti);
9114924Swnj 		ti = (struct tcpiphdr *)ti->ti_next;
9125263Swnj 		if (so->so_state & SS_CANTRCVMORE)
9135263Swnj 			(void) m_freem(m);
9145263Swnj 		else
9155263Swnj 			sbappend(&so->so_rcv, m);
9165263Swnj 	} while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
9175263Swnj 	sorwakeup(so);
9185065Swnj 	return (flags);
9195065Swnj drop:
9205065Swnj 	m_freem(dtom(ti));
9215263Swnj 	return (0);
9224601Swnj }
923