xref: /csrg-svn/sys/netinet/tcp_input.c (revision 10394)
1*10394Ssam /*	tcp_input.c	1.87	83/01/17	*/
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;
756161Ssam 		ti->ti_len = htons((u_short)ti->ti_len);
765231Swnj 		if (ti->ti_sum = in_cksum(m, len)) {
774924Swnj 			tcpstat.tcps_badsum++;
786211Swnj 			if (tcpprintfs)
796211Swnj 				printf("tcp cksum %x\n", ti->ti_sum);
805085Swnj 			goto drop;
814601Swnj 		}
824601Swnj 	}
834601Swnj 
844601Swnj 	/*
855244Sroot 	 * Check that TCP offset makes sense,
865440Swnj 	 * pull out TCP options and adjust length.
874601Swnj 	 */
884924Swnj 	off = ti->ti_off << 2;
895231Swnj 	if (off < sizeof (struct tcphdr) || off > tlen) {
904924Swnj 		tcpstat.tcps_badoff++;
915085Swnj 		goto drop;
924924Swnj 	}
936211Swnj 	tlen -= off;
946211Swnj 	ti->ti_len = tlen;
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 *);
1019642Ssam 		om = m_get(M_DONTWAIT, MT_DATA);
1025440Swnj 		if (om == 0)
1035440Swnj 			goto drop;
1045440Swnj 		om->m_len = off - sizeof (struct tcphdr);
1055440Swnj 		{ caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
1066161Ssam 		  bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len);
1075440Swnj 		  m->m_len -= om->m_len;
1086161Ssam 		  bcopy(op+om->m_len, op,
1096161Ssam 		   (unsigned)(m->m_len-sizeof (struct tcpiphdr)));
1105440Swnj 		}
1115440Swnj 	}
1125065Swnj 	tiflags = ti->ti_flags;
1134924Swnj 
1146093Sroot 	/*
1156211Swnj 	 * Drop TCP and IP headers.
1166093Sroot 	 */
1176093Sroot 	off += sizeof (struct ip);
1186093Sroot 	m->m_off += off;
1196093Sroot 	m->m_len -= off;
1206093Sroot 
1214924Swnj 	/*
1225244Sroot 	 * Convert TCP protocol specific fields to host format.
1235085Swnj 	 */
1245085Swnj 	ti->ti_seq = ntohl(ti->ti_seq);
1255085Swnj 	ti->ti_ack = ntohl(ti->ti_ack);
1265085Swnj 	ti->ti_win = ntohs(ti->ti_win);
1275085Swnj 	ti->ti_urp = ntohs(ti->ti_urp);
1285085Swnj 
1295085Swnj 	/*
1308271Sroot 	 * Locate pcb for segment.
1314924Swnj 	 */
1325065Swnj 	inp = in_pcblookup
1336028Sroot 		(&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport,
1346028Sroot 		INPLOOKUP_WILDCARD);
1355065Swnj 
1365065Swnj 	/*
1375065Swnj 	 * If the state is CLOSED (i.e., TCB does not exist) then
1385244Sroot 	 * all data in the incoming segment is discarded.
1395065Swnj 	 */
1405300Sroot 	if (inp == 0)
1415085Swnj 		goto dropwithreset;
1425065Swnj 	tp = intotcpcb(inp);
1435300Sroot 	if (tp == 0)
1445085Swnj 		goto dropwithreset;
1455109Swnj 	so = inp->inp_socket;
1465267Sroot 	if (so->so_options & SO_DEBUG) {
1475267Sroot 		ostate = tp->t_state;
1485267Sroot 		tcp_saveti = *ti;
1495267Sroot 	}
1507510Sroot 	if (so->so_options & SO_ACCEPTCONN) {
1517510Sroot 		so = sonewconn(so);
1527510Sroot 		if (so == 0)
1537510Sroot 			goto drop;
1547510Sroot 		inp = (struct inpcb *)so->so_pcb;
1557510Sroot 		inp->inp_laddr = ti->ti_dst;
1567510Sroot 		inp->inp_lport = ti->ti_dport;
1577510Sroot 		tp = intotcpcb(inp);
1587510Sroot 		tp->t_state = TCPS_LISTEN;
1597510Sroot 	}
1604601Swnj 
1614601Swnj 	/*
1625162Swnj 	 * Segment received on connection.
1635162Swnj 	 * Reset idle time and keep-alive timer.
1645162Swnj 	 */
1655162Swnj 	tp->t_idle = 0;
1665162Swnj 	tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
1675162Swnj 
1685162Swnj 	/*
1695440Swnj 	 * Process options.
1705440Swnj 	 */
1715440Swnj 	if (om) {
1725440Swnj 		tcp_dooptions(tp, om);
1735440Swnj 		om = 0;
1745440Swnj 	}
1755440Swnj 
1765440Swnj 	/*
1775085Swnj 	 * Calculate amount of space in receive window,
1785085Swnj 	 * and then do TCP input processing.
1794601Swnj 	 */
1805085Swnj 	tp->rcv_wnd = sbspace(&so->so_rcv);
1815231Swnj 	if (tp->rcv_wnd < 0)
1825231Swnj 		tp->rcv_wnd = 0;
1834601Swnj 
1844601Swnj 	switch (tp->t_state) {
1854601Swnj 
1865065Swnj 	/*
1875065Swnj 	 * If the state is LISTEN then ignore segment if it contains an RST.
1885065Swnj 	 * If the segment contains an ACK then it is bad and send a RST.
1895065Swnj 	 * If it does not contain a SYN then it is not interesting; drop it.
1905085Swnj 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
1915065Swnj 	 * tp->iss, and send a segment:
1925085Swnj 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
1935065Swnj 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
1945065Swnj 	 * Fill in remote peer address fields if not previously specified.
1955065Swnj 	 * Enter SYN_RECEIVED state, and process any other fields of this
1965244Sroot 	 * segment in this state.
1975065Swnj 	 */
1988271Sroot 	case TCPS_LISTEN: {
19910145Ssam 		struct mbuf *am;
2008271Sroot 		register struct sockaddr_in *sin;
2018271Sroot 
2025065Swnj 		if (tiflags & TH_RST)
2035065Swnj 			goto drop;
2045300Sroot 		if (tiflags & TH_ACK)
2055085Swnj 			goto dropwithreset;
2065300Sroot 		if ((tiflags & TH_SYN) == 0)
2075065Swnj 			goto drop;
20810145Ssam 		am = m_get(M_DONTWAIT, MT_SONAME);
20910145Ssam 		if (am == NULL)
21010145Ssam 			goto drop;
21110145Ssam 		am->m_len = sizeof (struct sockaddr_in);
2128599Sroot 		sin = mtod(am, struct sockaddr_in *);
2138271Sroot 		sin->sin_family = AF_INET;
2148271Sroot 		sin->sin_addr = ti->ti_src;
2158271Sroot 		sin->sin_port = ti->ti_sport;
2166028Sroot 		laddr = inp->inp_laddr;
21710145Ssam 		if (inp->inp_laddr.s_addr == INADDR_ANY)
2186028Sroot 			inp->inp_laddr = ti->ti_dst;
2198599Sroot 		if (in_pcbconnect(inp, am)) {
2206028Sroot 			inp->inp_laddr = laddr;
2218716Sroot 			(void) m_free(am);
2225244Sroot 			goto drop;
2236028Sroot 		}
2248716Sroot 		(void) m_free(am);
2255244Sroot 		tp->t_template = tcp_template(tp);
2265244Sroot 		if (tp->t_template == 0) {
2275244Sroot 			in_pcbdisconnect(inp);
2286028Sroot 			inp->inp_laddr = laddr;
2296320Swnj 			tp = 0;
2305244Sroot 			goto drop;
2315244Sroot 		}
2325085Swnj 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
2335065Swnj 		tp->irs = ti->ti_seq;
2345085Swnj 		tcp_sendseqinit(tp);
2355085Swnj 		tcp_rcvseqinit(tp);
2365065Swnj 		tp->t_state = TCPS_SYN_RECEIVED;
2375244Sroot 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
2385085Swnj 		goto trimthenstep6;
2398271Sroot 		}
2404601Swnj 
2415065Swnj 	/*
2425065Swnj 	 * If the state is SYN_SENT:
2435065Swnj 	 *	if seg contains an ACK, but not for our SYN, drop the input.
2445065Swnj 	 *	if seg contains a RST, then drop the connection.
2455065Swnj 	 *	if seg does not contain SYN, then drop it.
2465065Swnj 	 * Otherwise this is an acceptable SYN segment
2475065Swnj 	 *	initialize tp->rcv_nxt and tp->irs
2485065Swnj 	 *	if seg contains ack then advance tp->snd_una
2495065Swnj 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
2505065Swnj 	 *	arrange for segment to be acked (eventually)
2515065Swnj 	 *	continue processing rest of data/controls, beginning with URG
2525065Swnj 	 */
2535065Swnj 	case TCPS_SYN_SENT:
2545065Swnj 		if ((tiflags & TH_ACK) &&
2555300Sroot /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */
2565300Sroot 		    (SEQ_LT(ti->ti_ack, tp->iss) ||
2575231Swnj 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
2585085Swnj 			goto dropwithreset;
2595065Swnj 		if (tiflags & TH_RST) {
260*10394Ssam 			if (tiflags & TH_ACK)
261*10394Ssam 				tp = tcp_drop(tp, ECONNREFUSED);
2625065Swnj 			goto drop;
2634601Swnj 		}
2645065Swnj 		if ((tiflags & TH_SYN) == 0)
2655065Swnj 			goto drop;
2665231Swnj 		tp->snd_una = ti->ti_ack;
2675357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2685357Sroot 			tp->snd_nxt = tp->snd_una;
2695244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
2705065Swnj 		tp->irs = ti->ti_seq;
2715085Swnj 		tcp_rcvseqinit(tp);
2725085Swnj 		tp->t_flags |= TF_ACKNOW;
2735162Swnj 		if (SEQ_GT(tp->snd_una, tp->iss)) {
2745244Sroot 			soisconnected(so);
2755065Swnj 			tp->t_state = TCPS_ESTABLISHED;
2765162Swnj 			(void) tcp_reass(tp, (struct tcpiphdr *)0);
2775162Swnj 		} else
2785085Swnj 			tp->t_state = TCPS_SYN_RECEIVED;
2795085Swnj 		goto trimthenstep6;
2805085Swnj 
2815085Swnj trimthenstep6:
2825085Swnj 		/*
2835231Swnj 		 * Advance ti->ti_seq to correspond to first data byte.
2845085Swnj 		 * If data, trim to stay within window,
2855085Swnj 		 * dropping FIN if necessary.
2865085Swnj 		 */
2875231Swnj 		ti->ti_seq++;
2885085Swnj 		if (ti->ti_len > tp->rcv_wnd) {
2895085Swnj 			todrop = ti->ti_len - tp->rcv_wnd;
2905085Swnj 			m_adj(m, -todrop);
2915085Swnj 			ti->ti_len = tp->rcv_wnd;
2925085Swnj 			ti->ti_flags &= ~TH_FIN;
2935065Swnj 		}
2945263Swnj 		tp->snd_wl1 = ti->ti_seq - 1;
2955085Swnj 		goto step6;
2965065Swnj 	}
2974601Swnj 
2985065Swnj 	/*
2995065Swnj 	 * States other than LISTEN or SYN_SENT.
3005065Swnj 	 * First check that at least some bytes of segment are within
3015065Swnj 	 * receive window.
3025065Swnj 	 */
3035065Swnj 	if (tp->rcv_wnd == 0) {
3045065Swnj 		/*
3055065Swnj 		 * If window is closed can only take segments at
3065231Swnj 		 * window edge, and have to drop data and PUSH from
3075065Swnj 		 * incoming segments.
3085065Swnj 		 */
3095300Sroot 		if (tp->rcv_nxt != ti->ti_seq)
3105065Swnj 			goto dropafterack;
3115085Swnj 		if (ti->ti_len > 0) {
3125690Swnj 			m_adj(m, ti->ti_len);
3135085Swnj 			ti->ti_len = 0;
3145085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
3155065Swnj 		}
3165065Swnj 	} else {
3175065Swnj 		/*
3185231Swnj 		 * If segment begins before rcv_nxt, drop leading
3195065Swnj 		 * data (and SYN); if nothing left, just ack.
3205065Swnj 		 */
3215690Swnj 		todrop = tp->rcv_nxt - ti->ti_seq;
3225690Swnj 		if (todrop > 0) {
3235085Swnj 			if (tiflags & TH_SYN) {
3245300Sroot 				tiflags &= ~TH_SYN;
3255690Swnj 				ti->ti_flags &= ~TH_SYN;
3265085Swnj 				ti->ti_seq++;
3275085Swnj 				if (ti->ti_urp > 1)
3285085Swnj 					ti->ti_urp--;
3295085Swnj 				else
3305085Swnj 					tiflags &= ~TH_URG;
3315085Swnj 				todrop--;
3325085Swnj 			}
3336211Swnj 			if (todrop > ti->ti_len ||
3346211Swnj 			    todrop == ti->ti_len && (tiflags&TH_FIN) == 0)
3355065Swnj 				goto dropafterack;
3365065Swnj 			m_adj(m, todrop);
3375065Swnj 			ti->ti_seq += todrop;
3385065Swnj 			ti->ti_len -= todrop;
3395085Swnj 			if (ti->ti_urp > todrop)
3405085Swnj 				ti->ti_urp -= todrop;
3415085Swnj 			else {
3425085Swnj 				tiflags &= ~TH_URG;
3435690Swnj 				ti->ti_flags &= ~TH_URG;
3445690Swnj 				ti->ti_urp = 0;
3455085Swnj 			}
3465065Swnj 		}
3475065Swnj 		/*
3485065Swnj 		 * If segment ends after window, drop trailing data
3495085Swnj 		 * (and PUSH and FIN); if nothing left, just ACK.
3505065Swnj 		 */
3515690Swnj 		todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
3525690Swnj 		if (todrop > 0) {
3536211Swnj 			if (todrop >= ti->ti_len)
3545065Swnj 				goto dropafterack;
3555065Swnj 			m_adj(m, -todrop);
3565065Swnj 			ti->ti_len -= todrop;
3575085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
3585065Swnj 		}
3595065Swnj 	}
3604601Swnj 
3615065Swnj 	/*
36210013Ssam 	 * If data is received on a connection after the
3635951Swnj 	 * user processes are gone, then RST the other end.
3645951Swnj 	 */
365*10394Ssam 	if ((so->so_state & SS_NOFDREF) && tp->t_state > TCPS_CLOSE_WAIT &&
36610203Ssam 	    ti->ti_len) {
367*10394Ssam 		tp = tcp_close(tp);
3685951Swnj 		goto dropwithreset;
3695951Swnj 	}
3705951Swnj 
3715951Swnj 	/*
3725065Swnj 	 * If the RST bit is set examine the state:
3735065Swnj 	 *    SYN_RECEIVED STATE:
3745065Swnj 	 *	If passive open, return to LISTEN state.
3755065Swnj 	 *	If active open, inform user that connection was refused.
3765065Swnj 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
3775065Swnj 	 *	Inform user that connection was reset, and close tcb.
3785065Swnj 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
3795065Swnj 	 *	Close the tcb.
3805065Swnj 	 */
3815065Swnj 	if (tiflags&TH_RST) switch (tp->t_state) {
3825267Sroot 
3835065Swnj 	case TCPS_SYN_RECEIVED:
384*10394Ssam 		tp = tcp_drop(tp, ECONNREFUSED);
3855065Swnj 		goto drop;
3864601Swnj 
3875065Swnj 	case TCPS_ESTABLISHED:
3885065Swnj 	case TCPS_FIN_WAIT_1:
3895065Swnj 	case TCPS_FIN_WAIT_2:
3905065Swnj 	case TCPS_CLOSE_WAIT:
391*10394Ssam 		tp = tcp_drop(tp, ECONNRESET);
3925065Swnj 		goto drop;
3935065Swnj 
3945065Swnj 	case TCPS_CLOSING:
3955065Swnj 	case TCPS_LAST_ACK:
3965065Swnj 	case TCPS_TIME_WAIT:
397*10394Ssam 		tp = tcp_close(tp);
3985065Swnj 		goto drop;
3994601Swnj 	}
4004601Swnj 
4014601Swnj 	/*
4025065Swnj 	 * If a SYN is in the window, then this is an
4035065Swnj 	 * error and we send an RST and drop the connection.
4044601Swnj 	 */
4055065Swnj 	if (tiflags & TH_SYN) {
406*10394Ssam 		tp = tcp_drop(tp, ECONNRESET);
4075085Swnj 		goto dropwithreset;
4084601Swnj 	}
4094601Swnj 
4104601Swnj 	/*
4115065Swnj 	 * If the ACK bit is off we drop the segment and return.
4124601Swnj 	 */
4135085Swnj 	if ((tiflags & TH_ACK) == 0)
4145065Swnj 		goto drop;
4155065Swnj 
4165065Swnj 	/*
4175065Swnj 	 * Ack processing.
4185065Swnj 	 */
4194601Swnj 	switch (tp->t_state) {
4204601Swnj 
4215065Swnj 	/*
4225065Swnj 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
4235065Swnj 	 * ESTABLISHED state and continue processing, othewise
4245065Swnj 	 * send an RST.
4255065Swnj 	 */
4265065Swnj 	case TCPS_SYN_RECEIVED:
4275085Swnj 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
4285231Swnj 		    SEQ_GT(ti->ti_ack, tp->snd_max))
4295085Swnj 			goto dropwithreset;
4305244Sroot 		tp->snd_una++;			/* SYN acked */
4315357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
4325357Sroot 			tp->snd_nxt = tp->snd_una;
4335244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
4345085Swnj 		soisconnected(so);
4355085Swnj 		tp->t_state = TCPS_ESTABLISHED;
4365162Swnj 		(void) tcp_reass(tp, (struct tcpiphdr *)0);
4375244Sroot 		tp->snd_wl1 = ti->ti_seq - 1;
4385085Swnj 		/* fall into ... */
4394601Swnj 
4405065Swnj 	/*
4415065Swnj 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
4425065Swnj 	 * ACKs.  If the ack is in the range
4435231Swnj 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
4445065Swnj 	 * then advance tp->snd_una to ti->ti_ack and drop
4455065Swnj 	 * data from the retransmission queue.  If this ACK reflects
4465065Swnj 	 * more up to date window information we update our window information.
4475065Swnj 	 */
4485065Swnj 	case TCPS_ESTABLISHED:
4495065Swnj 	case TCPS_FIN_WAIT_1:
4505065Swnj 	case TCPS_FIN_WAIT_2:
4515065Swnj 	case TCPS_CLOSE_WAIT:
4525065Swnj 	case TCPS_CLOSING:
4535244Sroot 	case TCPS_LAST_ACK:
4545244Sroot 	case TCPS_TIME_WAIT:
4555085Swnj #define	ourfinisacked	(acked > 0)
4565085Swnj 
4575244Sroot 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una))
4585065Swnj 			break;
4595300Sroot 		if (SEQ_GT(ti->ti_ack, tp->snd_max))
4605065Swnj 			goto dropafterack;
4615085Swnj 		acked = ti->ti_ack - tp->snd_una;
4625951Swnj 
4635951Swnj 		/*
4645951Swnj 		 * If transmit timer is running and timed sequence
4655951Swnj 		 * number was acked, update smoothed round trip time.
4665951Swnj 		 */
4675951Swnj 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
4685951Swnj 			if (tp->t_srtt == 0)
4695951Swnj 				tp->t_srtt = tp->t_rtt;
4705951Swnj 			else
4715951Swnj 				tp->t_srtt =
4725951Swnj 				    tcp_alpha * tp->t_srtt +
4735951Swnj 				    (1 - tcp_alpha) * tp->t_rtt;
4745951Swnj 			tp->t_rtt = 0;
4755951Swnj 		}
4765951Swnj 
4775307Sroot 		if (ti->ti_ack == tp->snd_max)
4785244Sroot 			tp->t_timer[TCPT_REXMT] = 0;
4795307Sroot 		else {
4805244Sroot 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
4815244Sroot 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
4825951Swnj 			tp->t_rtt = 1;
4835300Sroot 			tp->t_rxtshift = 0;
4845085Swnj 		}
4855307Sroot 		if (acked > so->so_snd.sb_cc) {
4865307Sroot 			sbdrop(&so->so_snd, so->so_snd.sb_cc);
4875307Sroot 			tp->snd_wnd -= so->so_snd.sb_cc;
4885307Sroot 		} else {
4896161Ssam 			sbdrop(&so->so_snd, acked);
4905307Sroot 			tp->snd_wnd -= acked;
4915307Sroot 			acked = 0;
4925307Sroot 		}
4936434Swnj 		if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel)
4945300Sroot 			sowwakeup(so);
4955231Swnj 		tp->snd_una = ti->ti_ack;
4965357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
4975357Sroot 			tp->snd_nxt = tp->snd_una;
4985162Swnj 
4994601Swnj 		switch (tp->t_state) {
5004601Swnj 
5015065Swnj 		/*
5025065Swnj 		 * In FIN_WAIT_1 STATE in addition to the processing
5035065Swnj 		 * for the ESTABLISHED state if our FIN is now acknowledged
5045085Swnj 		 * then enter FIN_WAIT_2.
5055065Swnj 		 */
5065065Swnj 		case TCPS_FIN_WAIT_1:
5075896Swnj 			if (ourfinisacked) {
5085896Swnj 				/*
5095896Swnj 				 * If we can't receive any more
5105896Swnj 				 * data, then closing user can proceed.
5115896Swnj 				 */
5125896Swnj 				if (so->so_state & SS_CANTRCVMORE)
5135896Swnj 					soisdisconnected(so);
5145085Swnj 				tp->t_state = TCPS_FIN_WAIT_2;
5155896Swnj 			}
5164601Swnj 			break;
5174601Swnj 
5185065Swnj 	 	/*
5195065Swnj 		 * In CLOSING STATE in addition to the processing for
5205065Swnj 		 * the ESTABLISHED state if the ACK acknowledges our FIN
5215065Swnj 		 * then enter the TIME-WAIT state, otherwise ignore
5225065Swnj 		 * the segment.
5235065Swnj 		 */
5245065Swnj 		case TCPS_CLOSING:
5255244Sroot 			if (ourfinisacked) {
5265065Swnj 				tp->t_state = TCPS_TIME_WAIT;
5275244Sroot 				tcp_canceltimers(tp);
5285244Sroot 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
5295244Sroot 				soisdisconnected(so);
5305244Sroot 			}
5315244Sroot 			break;
5324601Swnj 
5335065Swnj 		/*
5345085Swnj 		 * The only thing that can arrive in  LAST_ACK state
5355085Swnj 		 * is an acknowledgment of our FIN.  If our FIN is now
5365085Swnj 		 * acknowledged, delete the TCB, enter the closed state
5375085Swnj 		 * and return.
5385065Swnj 		 */
5395065Swnj 		case TCPS_LAST_ACK:
540*10394Ssam 			if (ourfinisacked)
541*10394Ssam 				tp = tcp_close(tp);
5425065Swnj 			goto drop;
5434601Swnj 
5445065Swnj 		/*
5455065Swnj 		 * In TIME_WAIT state the only thing that should arrive
5465065Swnj 		 * is a retransmission of the remote FIN.  Acknowledge
5475065Swnj 		 * it and restart the finack timer.
5485065Swnj 		 */
5495065Swnj 		case TCPS_TIME_WAIT:
5505162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
5515065Swnj 			goto dropafterack;
5524601Swnj 		}
5535085Swnj #undef ourfinisacked
5545085Swnj 	}
5554601Swnj 
5565065Swnj step6:
5575065Swnj 	/*
5585244Sroot 	 * Update window information.
5595244Sroot 	 */
5605300Sroot 	if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
5615391Swnj 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
5625300Sroot 	     tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) {
5635244Sroot 		tp->snd_wnd = ti->ti_win;
5645244Sroot 		tp->snd_wl1 = ti->ti_seq;
5655244Sroot 		tp->snd_wl2 = ti->ti_ack;
5668599Sroot 		if (tp->snd_wnd != 0)
5675244Sroot 			tp->t_timer[TCPT_PERSIST] = 0;
5685244Sroot 	}
5695244Sroot 
5705244Sroot 	/*
5715547Swnj 	 * Process segments with URG.
5725065Swnj 	 */
5737267Swnj 	if ((tiflags & TH_URG) && ti->ti_urp &&
5747267Swnj 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
5755547Swnj 		/*
5765547Swnj 		 * If this segment advances the known urgent pointer,
5775547Swnj 		 * then mark the data stream.  This should not happen
5785547Swnj 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
5795547Swnj 		 * a FIN has been received from the remote side.
5805547Swnj 		 * In these states we ignore the URG.
5815547Swnj 		 */
5825547Swnj 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
5835547Swnj 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
5845547Swnj 			so->so_oobmark = so->so_rcv.sb_cc +
5855547Swnj 			    (tp->rcv_up - tp->rcv_nxt) - 1;
5865547Swnj 			if (so->so_oobmark == 0)
5875547Swnj 				so->so_state |= SS_RCVATMARK;
5888313Sroot 			sohasoutofband(so);
5895547Swnj 			tp->t_oobflags &= ~TCPOOB_HAVEDATA;
5905440Swnj 		}
5915547Swnj 		/*
5925547Swnj 		 * Remove out of band data so doesn't get presented to user.
5935547Swnj 		 * This can happen independent of advancing the URG pointer,
5945547Swnj 		 * but if two URG's are pending at once, some out-of-band
5955547Swnj 		 * data may creep in... ick.
5965547Swnj 		 */
5977510Sroot 		if (ti->ti_urp <= ti->ti_len)
5985547Swnj 			tcp_pulloutofband(so, ti);
5995419Swnj 	}
6004601Swnj 
6014601Swnj 	/*
6025065Swnj 	 * Process the segment text, merging it into the TCP sequencing queue,
6035065Swnj 	 * and arranging for acknowledgment of receipt if necessary.
6045065Swnj 	 * This process logically involves adjusting tp->rcv_wnd as data
6055065Swnj 	 * is presented to the user (this happens in tcp_usrreq.c,
6065065Swnj 	 * case PRU_RCVD).  If a FIN has already been received on this
6075065Swnj 	 * connection then we just ignore the text.
6084601Swnj 	 */
6095263Swnj 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
6105263Swnj 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
6115065Swnj 		tiflags = tcp_reass(tp, ti);
6125440Swnj 		if (tcpnodelack == 0)
6135440Swnj 			tp->t_flags |= TF_DELACK;
6145440Swnj 		else
6155440Swnj 			tp->t_flags |= TF_ACKNOW;
6165244Sroot 	} else {
6174924Swnj 		m_freem(m);
6185263Swnj 		tiflags &= ~TH_FIN;
6195244Sroot 	}
6204601Swnj 
6214601Swnj 	/*
6225263Swnj 	 * If FIN is received ACK the FIN and let the user know
6235263Swnj 	 * that the connection is closing.
6244601Swnj 	 */
6255263Swnj 	if (tiflags & TH_FIN) {
6265244Sroot 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
6275244Sroot 			socantrcvmore(so);
6285244Sroot 			tp->t_flags |= TF_ACKNOW;
6295244Sroot 			tp->rcv_nxt++;
6305244Sroot 		}
6315065Swnj 		switch (tp->t_state) {
6324601Swnj 
6335065Swnj 	 	/*
6345065Swnj 		 * In SYN_RECEIVED and ESTABLISHED STATES
6355065Swnj 		 * enter the CLOSE_WAIT state.
6364884Swnj 		 */
6375065Swnj 		case TCPS_SYN_RECEIVED:
6385065Swnj 		case TCPS_ESTABLISHED:
6395065Swnj 			tp->t_state = TCPS_CLOSE_WAIT;
6405065Swnj 			break;
6414884Swnj 
6425065Swnj 	 	/*
6435085Swnj 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
6445085Swnj 		 * enter the CLOSING state.
6454884Swnj 		 */
6465065Swnj 		case TCPS_FIN_WAIT_1:
6475085Swnj 			tp->t_state = TCPS_CLOSING;
6485065Swnj 			break;
6494601Swnj 
6505065Swnj 	 	/*
6515065Swnj 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
6525065Swnj 		 * starting the time-wait timer, turning off the other
6535065Swnj 		 * standard timers.
6545065Swnj 		 */
6555065Swnj 		case TCPS_FIN_WAIT_2:
6565244Sroot 			tp->t_state = TCPS_TIME_WAIT;
6575074Swnj 			tcp_canceltimers(tp);
6585162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
6595244Sroot 			soisdisconnected(so);
6605065Swnj 			break;
6615065Swnj 
6624884Swnj 		/*
6635065Swnj 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
6644884Swnj 		 */
6655065Swnj 		case TCPS_TIME_WAIT:
6665162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
6675065Swnj 			break;
6685085Swnj 		}
6694601Swnj 	}
6705267Sroot 	if (so->so_options & SO_DEBUG)
6715267Sroot 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
6725085Swnj 
6735085Swnj 	/*
6745085Swnj 	 * Return any desired output.
6755085Swnj 	 */
6766161Ssam 	(void) tcp_output(tp);
6775065Swnj 	return;
6785085Swnj 
6795065Swnj dropafterack:
6805085Swnj 	/*
6816211Swnj 	 * Generate an ACK dropping incoming segment if it occupies
6826211Swnj 	 * sequence space, where the ACK reflects our state.
6835085Swnj 	 */
6846211Swnj 	if ((tiflags&TH_RST) ||
6856211Swnj 	    tlen == 0 && (tiflags&(TH_SYN|TH_FIN)) == 0)
6865085Swnj 		goto drop;
6876303Sroot 	if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
6886303Sroot 		tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0);
6895391Swnj 	tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK);
6905231Swnj 	return;
6915085Swnj 
6925085Swnj dropwithreset:
6935440Swnj 	if (om)
6946161Ssam 		(void) m_free(om);
6955085Swnj 	/*
6965244Sroot 	 * Generate a RST, dropping incoming segment.
6975085Swnj 	 * Make ACK acceptable to originator of segment.
6985085Swnj 	 */
6995085Swnj 	if (tiflags & TH_RST)
7005085Swnj 		goto drop;
7015085Swnj 	if (tiflags & TH_ACK)
7025391Swnj 		tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST);
7035085Swnj 	else {
7045085Swnj 		if (tiflags & TH_SYN)
7055085Swnj 			ti->ti_len++;
7066211Swnj 		tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0,
7076211Swnj 		    TH_RST|TH_ACK);
7085085Swnj 	}
7095231Swnj 	return;
7105085Swnj 
7115065Swnj drop:
7125085Swnj 	/*
7135085Swnj 	 * Drop space held by incoming segment and return.
7145085Swnj 	 */
7156303Sroot 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
7166303Sroot 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
7175065Swnj 	m_freem(m);
7185267Sroot 	return;
7195065Swnj }
7205065Swnj 
7215440Swnj tcp_dooptions(tp, om)
7225440Swnj 	struct tcpcb *tp;
7235440Swnj 	struct mbuf *om;
7245419Swnj {
7255440Swnj 	register u_char *cp;
7265440Swnj 	int opt, optlen, cnt;
7275419Swnj 
7285440Swnj 	cp = mtod(om, u_char *);
7295440Swnj 	cnt = om->m_len;
7305440Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
7315440Swnj 		opt = cp[0];
7325440Swnj 		if (opt == TCPOPT_EOL)
7335440Swnj 			break;
7345440Swnj 		if (opt == TCPOPT_NOP)
7355440Swnj 			optlen = 1;
7365440Swnj 		else
7375440Swnj 			optlen = cp[1];
7385440Swnj 		switch (opt) {
7395440Swnj 
7405440Swnj 		default:
7415440Swnj 			break;
7425440Swnj 
7435440Swnj 		case TCPOPT_MAXSEG:
7445440Swnj 			if (optlen != 4)
7455440Swnj 				continue;
7465440Swnj 			tp->t_maxseg = *(u_short *)(cp + 2);
7476161Ssam 			tp->t_maxseg = ntohs((u_short)tp->t_maxseg);
7485440Swnj 			break;
7495419Swnj 		}
7505419Swnj 	}
7516161Ssam 	(void) m_free(om);
7525419Swnj }
7535419Swnj 
7545419Swnj /*
7555547Swnj  * Pull out of band byte out of a segment so
7565547Swnj  * it doesn't appear in the user's data queue.
7575547Swnj  * It is still reflected in the segment length for
7585547Swnj  * sequencing purposes.
7595547Swnj  */
7605547Swnj tcp_pulloutofband(so, ti)
7615547Swnj 	struct socket *so;
7625547Swnj 	struct tcpiphdr *ti;
7635547Swnj {
7645547Swnj 	register struct mbuf *m;
7656116Swnj 	int cnt = ti->ti_urp - 1;
7665547Swnj 
7675547Swnj 	m = dtom(ti);
7685547Swnj 	while (cnt >= 0) {
7695547Swnj 		if (m->m_len > cnt) {
7705547Swnj 			char *cp = mtod(m, caddr_t) + cnt;
7715547Swnj 			struct tcpcb *tp = sototcpcb(so);
7725547Swnj 
7735547Swnj 			tp->t_iobc = *cp;
7745547Swnj 			tp->t_oobflags |= TCPOOB_HAVEDATA;
7756161Ssam 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
7765547Swnj 			m->m_len--;
7775547Swnj 			return;
7785547Swnj 		}
7795547Swnj 		cnt -= m->m_len;
7805547Swnj 		m = m->m_next;
7815547Swnj 		if (m == 0)
7825547Swnj 			break;
7835547Swnj 	}
7845547Swnj 	panic("tcp_pulloutofband");
7855547Swnj }
7865547Swnj 
7875547Swnj /*
7885065Swnj  * Insert segment ti into reassembly queue of tcp with
7895065Swnj  * control block tp.  Return TH_FIN if reassembly now includes
7905065Swnj  * a segment with FIN.
7915065Swnj  */
7925109Swnj tcp_reass(tp, ti)
7935065Swnj 	register struct tcpcb *tp;
7945065Swnj 	register struct tcpiphdr *ti;
7955065Swnj {
7965065Swnj 	register struct tcpiphdr *q;
7975085Swnj 	struct socket *so = tp->t_inpcb->inp_socket;
7985263Swnj 	struct mbuf *m;
7995263Swnj 	int flags;
8005065Swnj 
8015065Swnj 	/*
8025162Swnj 	 * Call with ti==0 after become established to
8035162Swnj 	 * force pre-ESTABLISHED data up to user socket.
8045065Swnj 	 */
8055162Swnj 	if (ti == 0)
8065065Swnj 		goto present;
8074601Swnj 
8085065Swnj 	/*
8095065Swnj 	 * Find a segment which begins after this one does.
8105065Swnj 	 */
8115065Swnj 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
8125065Swnj 	    q = (struct tcpiphdr *)q->ti_next)
8135065Swnj 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
8145065Swnj 			break;
8154601Swnj 
8165065Swnj 	/*
8175065Swnj 	 * If there is a preceding segment, it may provide some of
8185065Swnj 	 * our data already.  If so, drop the data from the incoming
8195065Swnj 	 * segment.  If it provides all of our data, drop us.
8205065Swnj 	 */
8215065Swnj 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
8225065Swnj 		register int i;
8235690Swnj 		q = (struct tcpiphdr *)q->ti_prev;
8245065Swnj 		/* conversion to int (in i) handles seq wraparound */
8255065Swnj 		i = q->ti_seq + q->ti_len - ti->ti_seq;
8265065Swnj 		if (i > 0) {
8274924Swnj 			if (i >= ti->ti_len)
8285065Swnj 				goto drop;
8297338Swnj 			m_adj(dtom(ti), i);
8305065Swnj 			ti->ti_len -= i;
8314924Swnj 			ti->ti_seq += i;
8324601Swnj 		}
8335065Swnj 		q = (struct tcpiphdr *)(q->ti_next);
8345065Swnj 	}
8354601Swnj 
8365065Swnj 	/*
8375065Swnj 	 * While we overlap succeeding segments trim them or,
8385065Swnj 	 * if they are completely covered, dequeue them.
8395065Swnj 	 */
8405690Swnj 	while (q != (struct tcpiphdr *)tp) {
8415065Swnj 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
8425690Swnj 		if (i <= 0)
8435690Swnj 			break;
8445065Swnj 		if (i < q->ti_len) {
8455690Swnj 			q->ti_seq += i;
8465065Swnj 			q->ti_len -= i;
8475065Swnj 			m_adj(dtom(q), i);
8485065Swnj 			break;
8494601Swnj 		}
8505065Swnj 		q = (struct tcpiphdr *)q->ti_next;
8515623Swnj 		m = dtom(q->ti_prev);
8525065Swnj 		remque(q->ti_prev);
8535623Swnj 		m_freem(m);
8545065Swnj 	}
8554601Swnj 
8565065Swnj 	/*
8575065Swnj 	 * Stick new segment in its place.
8585065Swnj 	 */
8595065Swnj 	insque(ti, q->ti_prev);
8604601Swnj 
8615065Swnj present:
8625065Swnj 	/*
8635244Sroot 	 * Present data to user, advancing rcv_nxt through
8645244Sroot 	 * completed sequence space.
8655065Swnj 	 */
8665263Swnj 	if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
8675244Sroot 		return (0);
8684924Swnj 	ti = tp->seg_next;
8695263Swnj 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
8705263Swnj 		return (0);
8715263Swnj 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
8725263Swnj 		return (0);
8735263Swnj 	do {
8745244Sroot 		tp->rcv_nxt += ti->ti_len;
8755244Sroot 		flags = ti->ti_flags & TH_FIN;
8764924Swnj 		remque(ti);
8775263Swnj 		m = dtom(ti);
8784924Swnj 		ti = (struct tcpiphdr *)ti->ti_next;
8795263Swnj 		if (so->so_state & SS_CANTRCVMORE)
8806161Ssam 			m_freem(m);
88110145Ssam 		else
8825263Swnj 			sbappend(&so->so_rcv, m);
8835263Swnj 	} while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
8845263Swnj 	sorwakeup(so);
8855065Swnj 	return (flags);
8865065Swnj drop:
8875065Swnj 	m_freem(dtom(ti));
8885263Swnj 	return (0);
8894601Swnj }
890