xref: /csrg-svn/sys/netinet/tcp_input.c (revision 4718)
1*4718Swnj /* tcp_input.c 1.17 81/11/01 */
24601Swnj 
34601Swnj #include "../h/param.h"
44601Swnj #include "../h/systm.h"
54663Swnj #include "../h/mbuf.h"
64663Swnj #include "../h/socket.h"
7*4718Swnj #include "../inet/inet_cksum.h"
84663Swnj #include "../inet/inet.h"
94663Swnj #include "../inet/inet_systm.h"
104663Swnj #include "../inet/imp.h"
114663Swnj #include "../inet/inet_host.h"
124663Swnj #include "../inet/ip.h"
134663Swnj #include "../inet/tcp.h"
144663Swnj #include "../inet/tcp_fsm.h"
154601Swnj 
164679Swnj int	tcpcksum = 1;
174601Swnj 
184601Swnj tcp_input(mp)
194601Swnj 	register struct mbuf *mp;
204601Swnj {
214673Swnj 	register struct th *n;		/* known to be r10 */
224697Swnj 	register int j;
234601Swnj 	register struct tcb *tp;
244601Swnj 	int nstate;
254601Swnj 	struct mbuf *m;
264601Swnj 	struct ucb *up;
274673Swnj 	int hlen, tlen;
284601Swnj 	u_short lport, fport;
294601Swnj #ifdef TCPDEBUG
304601Swnj 	struct tcp_debug tdb;
314601Swnj #endif
324601Swnj COUNT(TCP_INPUT);
334601Swnj 
344601Swnj 	/*
354601Swnj 	 * Build extended tcp header
364601Swnj 	 */
374601Swnj 	n = (struct th *)((int)mp + mp->m_off);
384601Swnj 	tlen = ((struct ip *)n)->ip_len;
394601Swnj 	n->t_len = htons(tlen);
404601Swnj 	n->t_next = NULL;
414601Swnj 	n->t_prev = NULL;
424601Swnj 	n->t_x1 = 0;
434601Swnj 	lport = ntohs(n->t_dst);
444601Swnj 	fport = ntohs(n->t_src);
454601Swnj 
464601Swnj 	/* WONT BE POSSIBLE WHEN MBUFS ARE 256 BYTES */
474601Swnj 	if ((hlen = n->t_off << 2) > mp->m_len)
484601Swnj 		{ printf("tcp header overflow\n"); m_freem(mp); return; }
494601Swnj 
504679Swnj 	if (tcpcksum) {
514679Swnj 		/*
524679Swnj 		 * Checksum extended header and data
534679Swnj 		 */
54*4718Swnj 		CKSUM_TCPCHK(mp, n, r10, sizeof (struct ip) + tlen);
55*4718Swnj 		if (n->t_sum != 0) {
564679Swnj 			netstat.t_badsum++;
574601Swnj 			m_freem(mp);
584601Swnj 			return;
594601Swnj 		}
604601Swnj 	}
614601Swnj 
624601Swnj 	/*
634601Swnj 	 * Find tcb for message (SHOULDN'T USE LINEAR SEARCH!)
644601Swnj 	 */
654681Swnj 	for (tp = tcb.tcb_next; tp != (struct tcb *)&tcb; tp = tp->tcb_next)
664601Swnj 		if (tp->t_lport == lport && tp->t_fport == fport &&
674601Swnj 		    tp->t_ucb->uc_host->h_addr.s_addr == n->t_s.s_addr)
684601Swnj 			goto found;
694681Swnj 	for (tp = tcb.tcb_next; tp != (struct tcb *)&tcb; tp = tp->tcb_next)
704601Swnj 		if (tp->t_lport == lport &&
714601Swnj 		    (tp->t_fport==fport || tp->t_fport==0) &&
724601Swnj 		    (tp->t_ucb->uc_host->h_addr.s_addr == n->t_s.s_addr ||
734601Swnj 		     tp->t_ucb->uc_host->h_addr.s_addr == 0))
744601Swnj 			goto found;
754601Swnj 	goto notwanted;
764601Swnj found:
774601Swnj 
784601Swnj 	/*
794601Swnj 	 * Byte swap header
804601Swnj 	 */
814601Swnj 	n->t_len = tlen - hlen;
824601Swnj 	n->t_src = fport;
834601Swnj 	n->t_dst = lport;
844601Swnj 	n->t_seq = ntohl(n->t_seq);
854601Swnj 	n->t_ackno = ntohl(n->t_ackno);
864601Swnj 	n->t_win = ntohs(n->t_win);
874601Swnj 	n->t_urp = ntohs(n->t_urp);
884601Swnj 
894601Swnj 	/*
904601Swnj 	 * Check segment seq # and do rst processing
914601Swnj 	 */
924601Swnj 	switch (tp->t_state) {
934601Swnj 
944601Swnj 	case LISTEN:
954601Swnj 		if ((n->th_flags&TH_ACK) || !syn_ok(tp, n)) {
964675Swnj 			tcp_sndrst(tp, n);
974601Swnj 			goto badseg;
984601Swnj 		}
994601Swnj 		if (n->th_flags&TH_RST)
1004601Swnj 			goto badseg;
1014601Swnj 		goto goodseg;
1024601Swnj 
1034601Swnj 	case SYN_SENT:
1044601Swnj 		if (!ack_ok(tp, n) || !syn_ok(tp, n)) {
1054675Swnj 			tcp_sndrst(tp, n);			/* 71,72,75 */
1064601Swnj 			goto badseg;
1074601Swnj 		}
1084601Swnj 		if (n->th_flags&TH_RST) {
1094675Swnj 			tcp_close(tp, URESET);			/* 70 */
1104601Swnj 			tp->t_state = CLOSED;
1114601Swnj 			goto badseg;
1124601Swnj 		}
1134601Swnj 		goto goodseg;
1144601Swnj 
1154601Swnj 	default:
1164601Swnj         	if ((n->th_flags&TH_RST) == 0)
1174601Swnj 			goto common;
1184601Swnj 		if (n->t_seq < tp->rcv_nxt)		/* bad rst */
1194601Swnj 			goto badseg;				/* 69 */
1204601Swnj 		switch (tp->t_state) {
1214601Swnj 
1224601Swnj 		case L_SYN_RCVD:
1234601Swnj 			if (ack_ok(tp, n) == 0)
1244601Swnj 				goto badseg;			/* 69 */
1254601Swnj 			tp->t_rexmt = 0;
1264601Swnj 			tp->t_rexmttl = 0;
1274601Swnj 			tp->t_persist = 0;
1284601Swnj 			h_free(tp->t_ucb->uc_host);
1294601Swnj 			tp->t_state = LISTEN;
1304601Swnj 			goto badseg;
1314601Swnj 
1324601Swnj 		default:
1334675Swnj 			tcp_close(tp, URESET);			/* 66 */
1344601Swnj 			tp->t_state = CLOSED;
1354601Swnj 			goto badseg;
1364601Swnj 		}
1374601Swnj 		/*NOTREACHED*/
1384601Swnj 
1394601Swnj 	case SYN_RCVD:
1404601Swnj common:
1414601Swnj 		if (ack_ok(tp, n) == 0) {
1424675Swnj 			tcp_sndrst(tp, n);			/* 74 */
1434601Swnj 			goto badseg;
1444601Swnj 		}
1454601Swnj 		if (syn_ok(tp, n) && n->t_seq != tp->irs) {
1464679Swnj 			tcp_sndnull(tp);			/* 74 */
1474601Swnj 			goto badseg;
1484601Swnj 		}
1494601Swnj 		goto goodseg;
1504601Swnj 	}
1514601Swnj badseg:
1524601Swnj 	m_freem(mp);
1534601Swnj 	return;
1544601Swnj 
1554601Swnj goodseg:
1564601Swnj #ifdef notdef
1574679Swnj 	/* DO SOMETHING ABOUT UNACK!!! */
1584601Swnj 	/*
1594601Swnj 	 * Defer processing if no buffer space for this connection.
1604601Swnj 	 */
1614601Swnj 	up = tp->t_ucb;
1624656Swnj 	if (up->uc_rcc > up->uc_rhiwat &&
1634663Swnj 	     && n->t_len != 0 && mbstat.m_bufs < mbstat.m_lowat) {
1644601Swnj 		mp->m_act = (struct mbuf *)0;
1654601Swnj 		if ((m = tp->t_rcv_unack) != NULL) {
1664601Swnj 			while (m->m_act != NULL)
1674601Swnj 				m = m->m_act;
1684601Swnj 			m->m_act = mp;
1694601Swnj 		} else
1704601Swnj 			tp->t_rcv_unack = mp;
1714601Swnj 		return;
1724601Swnj 	}
1734601Swnj #endif
1744601Swnj 
1754601Swnj 	/*
1764601Swnj 	 * Discard ip header, and do tcp input processing.
1774601Swnj 	 */
1784601Swnj 	hlen += sizeof(struct ip);
1794601Swnj 	mp->m_off += hlen;
1804601Swnj 	mp->m_len -= hlen;
1814601Swnj 	nstate = tp->t_state;
1824601Swnj 	tp->tc_flags &= ~TC_NET_KEEP;
1834601Swnj 	acounts[tp->t_state][INRECV]++;
1844601Swnj #ifdef TCPDEBUG
1854601Swnj 	if ((tp->t_ucb->uc_flags & UDEBUG) || tcpconsdebug) {
1864604Swnj 		tdb_setup(tp, n, INRECV, &tdb);
1874601Swnj 	} else
1884601Swnj 		tdb.td_tod = 0;
1894601Swnj #endif
1904601Swnj 	switch (tp->t_state) {
1914601Swnj 
1924601Swnj 	case LISTEN:
1934601Swnj 		if (!syn_ok(tp, n) ||
1944601Swnj 		    ((tp->t_ucb->uc_host = h_make(&n->t_s)) == 0)) {
1954601Swnj 			nstate = EFAILEC;
1964601Swnj 			goto done;
1974601Swnj 		}
1984601Swnj 		tp->t_fport = n->t_src;
1994603Sroot 		tp->t_ucb->uc_template = tcp_template(tp);
2004675Swnj 		tcp_ctldat(tp, n, 1);
2014601Swnj 		if (tp->tc_flags&TC_FIN_RCVD) {
2024601Swnj 			tp->t_finack = T_2ML;			/* 3 */
2034601Swnj 			tp->tc_flags &= ~TC_WAITED_2_ML;
2044601Swnj 			nstate = CLOSE_WAIT;
2054601Swnj 		} else {
2064601Swnj 			tp->t_init = T_INIT / 2;		/* 4 */
2074601Swnj 			nstate = L_SYN_RCVD;
2084601Swnj 		}
2094601Swnj 		goto done;
2104601Swnj 
2114601Swnj 	case SYN_SENT:
2124601Swnj 		if (!syn_ok(tp, n)) {
2134601Swnj 			nstate = EFAILEC;
2144601Swnj 			goto done;
2154601Swnj 		}
2164675Swnj 		tcp_ctldat(tp, n, 1);
2174601Swnj 		if (tp->tc_flags&TC_FIN_RCVD) {
2184690Swnj 			if ((n->th_flags&TH_ACK) == 0) {
2194601Swnj 				tp->t_finack = T_2ML;		/* 9 */
2204601Swnj 				tp->tc_flags &= ~TC_WAITED_2_ML;
2214601Swnj 			}
2224601Swnj 			nstate = CLOSE_WAIT;
2234601Swnj 			goto done;
2244601Swnj 		}
2254690Swnj 		nstate = (n->th_flags&TH_ACK) ? ESTAB : SYN_RCVD; /* 11:8 */
2264601Swnj 		goto done;
2274601Swnj 
2284601Swnj 	case SYN_RCVD:
2294601Swnj 	case L_SYN_RCVD:
2304601Swnj 		if ((n->th_flags&TH_ACK) == 0 ||
2314601Swnj 		    (n->th_flags&TH_ACK) && n->t_ackno <= tp->iss) {
2324601Swnj 			nstate = EFAILEC;
2334601Swnj 			goto done;
2344601Swnj 		}
2354601Swnj 		goto input;
2364601Swnj 
2374601Swnj 	case ESTAB:
2384601Swnj 	case FIN_W1:
2394601Swnj 	case FIN_W2:
2404601Swnj 	case TIME_WAIT:
2414601Swnj input:
2424675Swnj 		tcp_ctldat(tp, n, 1);				/* 39 */
2434601Swnj 		switch (tp->t_state) {
2444601Swnj 
2454601Swnj 		case ESTAB:
2464601Swnj 			if (tp->tc_flags&TC_FIN_RCVD)
2474601Swnj 				nstate = CLOSE_WAIT;
2484601Swnj 			break;
2494601Swnj 
2504601Swnj 		case SYN_RCVD:
2514601Swnj 		case L_SYN_RCVD:
2524601Swnj 			nstate = (tp->tc_flags&TC_FIN_RCVD) ?
2534601Swnj 			    CLOSE_WAIT : ESTAB;			 /* 33:5 */
2544601Swnj 			break;
2554601Swnj 
2564601Swnj 		case FIN_W1:
2574601Swnj 			j = ack_fin(tp, n);
2584601Swnj 			if ((tp->tc_flags & TC_FIN_RCVD) == 0) {
2594601Swnj 				if (j)
2604601Swnj 					nstate = FIN_W2;	/* 27 */
2614601Swnj 				break;
2624601Swnj 			}
2634601Swnj 			tp->t_finack = T_2ML;
2644601Swnj 			tp->tc_flags &= ~TC_WAITED_2_ML;
2654601Swnj 			nstate = j ? TIME_WAIT : CLOSING1;	/* 28:26 */
2664601Swnj 			break;
2674601Swnj 
2684601Swnj 		case FIN_W2:
2694601Swnj 			if (tp->tc_flags&TC_FIN_RCVD) {
2704601Swnj 				tp->t_finack = T_2ML;		/* 29 */
2714601Swnj 				tp->tc_flags &= ~TC_WAITED_2_ML;
2724601Swnj 				nstate = TIME_WAIT;
2734601Swnj 				break;
2744601Swnj 			}
2754601Swnj 			break;
2764601Swnj 		}
2774601Swnj 		goto done;
2784601Swnj 
2794601Swnj 	case CLOSE_WAIT:
2804601Swnj 		if (n->th_flags&TH_FIN) {
2814601Swnj 			if ((n->th_flags&TH_ACK) &&
2824601Swnj 			    n->t_ackno <= tp->seq_fin) {
2834675Swnj 				tcp_ctldat(tp, n, 0);		/* 30 */
2844601Swnj 				tp->t_finack = T_2ML;
2854601Swnj 				tp->tc_flags &= ~TC_WAITED_2_ML;
2864601Swnj 			} else
2874675Swnj 				tcp_sndctl(tp);			/* 31 */
2884601Swnj 			goto done;
2894601Swnj 		}
2904601Swnj 		goto input;
2914601Swnj 
2924601Swnj 	case CLOSING1:
2934601Swnj 		j = ack_fin(tp, n);
2944601Swnj 		if (n->th_flags&TH_FIN) {
2954675Swnj 			tcp_ctldat(tp, n, 0);
2964601Swnj 			tp->t_finack = T_2ML;
2974601Swnj 			tp->tc_flags &= ~TC_WAITED_2_ML;
2984601Swnj 			if (j)
2994601Swnj 				nstate = TIME_WAIT;		/* 23 */
3004601Swnj 			goto done;
3014601Swnj 		}
3024601Swnj 		if (j) {
3034601Swnj 			if (tp->tc_flags&TC_WAITED_2_ML)
3044601Swnj 				if (rcv_empty(tp)) {
3054675Swnj 					tcp_close(tp, UCLOSED);	/* 15 */
3064601Swnj 					nstate = CLOSED;
3074601Swnj 				} else
3084601Swnj 					nstate = RCV_WAIT;	/* 18 */
3094601Swnj 			else
3104601Swnj 				nstate = TIME_WAIT;
3114601Swnj 			goto done;
3124601Swnj 		}
3134601Swnj 		goto input;
3144601Swnj 
3154601Swnj 	case CLOSING2:
3164601Swnj 		if (ack_fin(tp, n)) {
3174601Swnj 			if (rcv_empty(tp)) {			/* 16 */
3184675Swnj 				tcp_close(tp, UCLOSED);
3194601Swnj 				nstate = CLOSED;
3204601Swnj 			} else
3214601Swnj 				nstate = RCV_WAIT;		/* 19 */
3224601Swnj 			goto done;
3234601Swnj 		}
3244601Swnj 		if (n->th_flags&TH_FIN) {
3254675Swnj 			tcp_sndctl(tp);				/* 31 */
3264601Swnj 			goto done;
3274601Swnj 		}
3284601Swnj 		goto input;
3294601Swnj 
3304601Swnj 	case RCV_WAIT:
3314601Swnj 		if ((n->th_flags&TH_FIN) && (n->th_flags&TH_ACK) &&
3324601Swnj 		    n->t_ackno <= tp->seq_fin) {
3334675Swnj 			tcp_ctldat(tp, n, 0);
3344601Swnj 			tp->t_finack = T_2ML;
3354601Swnj 			tp->tc_flags &= ~TC_WAITED_2_ML;	/* 30 */
3364601Swnj 		}
3374601Swnj 		goto done;
3384601Swnj 	}
3394601Swnj 	panic("tcp_input");
3404601Swnj done:
3414601Swnj 
3424601Swnj 	/*
3434601Swnj 	 * Done with state*input specific processing.
3444601Swnj 	 * Form trace records, free input if not needed,
3454601Swnj 	 * and enter new state.
3464601Swnj 	 */
3474601Swnj #ifdef TCPDEBUG
3484604Swnj 	if (tdb.td_tod)
3494604Swnj 		tdb_stuff(&tdb, nstate);
3504601Swnj #endif
3514601Swnj 	switch (nstate) {
3524601Swnj 
3534601Swnj 	case EFAILEC:
3544601Swnj 		m_freem(mp);
3554601Swnj 		return;
3564601Swnj 
3574601Swnj 	default:
3584601Swnj 		tp->t_state = nstate;
3594601Swnj 		/* fall into ... */
3604601Swnj 
3614601Swnj 	case CLOSED:
3624601Swnj 		/* IF CLOSED CANT LOOK AT tc_flags */
3634601Swnj 		if ((tp->tc_flags&TC_NET_KEEP) == 0)
3644690Swnj 			/* inline expansion of m_freem */
3654690Swnj 			while (mp) {
3664690Swnj 				MFREE(mp, m);
3674690Swnj 				mp = m;
3684690Swnj 			}
3694601Swnj 		return;
3704601Swnj 	}
3714601Swnj 	/* NOTREACHED */
3724601Swnj 
3734601Swnj 	/*
3744601Swnj 	 * Unwanted packed; free everything
3754601Swnj 	 * but the header and return an rst.
3764601Swnj 	 */
3774601Swnj notwanted:
3784601Swnj 	m_freem(mp->m_next);
3794601Swnj 	mp->m_next = NULL;
3804601Swnj 	mp->m_len = sizeof(struct th);
3814601Swnj #define xchg(a,b) j=a; a=b; b=j
3824601Swnj 	xchg(n->t_d.s_addr, n->t_s.s_addr); xchg(n->t_dst, n->t_src);
3834601Swnj #undef xchg
3844601Swnj 	if (n->th_flags&TH_ACK)
3854601Swnj 		n->t_seq = n->t_ackno;
3864601Swnj 	else {
3874601Swnj 		n->t_ackno = htonl(ntohl(n->t_seq) + tlen - hlen);
3884601Swnj 		n->t_seq = 0;
3894601Swnj 	}
3904601Swnj 	n->th_flags = TH_RST; /* not TH_FIN, TH_SYN */
3914601Swnj 	n->th_flags ^= TH_ACK;
3924601Swnj 	n->t_len = htons(TCPSIZE);
3934601Swnj 	n->t_off = 5;
394*4718Swnj 	n->t_sum = inet_cksum(mp, sizeof(struct th));
3954601Swnj 	((struct ip *)n)->ip_len = sizeof(struct th);
3964601Swnj 	ip_output(mp);
3974601Swnj 	netstat.t_badsegs++;
3984601Swnj }
3994601Swnj 
4004675Swnj tcp_ctldat(tp, n, dataok)
4014601Swnj 	register struct tcb *tp;
4024601Swnj 	register struct th *n;
4034601Swnj {
4044679Swnj 	register struct mbuf *m;
4054679Swnj 	int sent;
4064679Swnj COUNT(TCP_CTLDAT);
4074601Swnj 
4084601Swnj 	tp->tc_flags &= ~(TC_DROPPED_TXT|TC_ACK_DUE|TC_NEW_WINDOW);
4094601Swnj /* syn */
4104601Swnj 	if ((tp->tc_flags&TC_SYN_RCVD) == 0 && (n->th_flags&TH_SYN)) {
4114601Swnj 		tp->irs = n->t_seq;
4124601Swnj 		tp->rcv_nxt = n->t_seq + 1;
4134601Swnj 		tp->snd_wl = tp->rcv_urp = tp->irs;
4144601Swnj 		tp->tc_flags |= (TC_SYN_RCVD|TC_ACK_DUE);
4154601Swnj 	}
4164601Swnj /* ack */
4174601Swnj 	if ((n->th_flags&TH_ACK) && (tp->tc_flags&TC_SYN_RCVD) &&
4184601Swnj 	    n->t_ackno > tp->snd_una) {
4194679Swnj 		register struct mbuf *mn;
4204679Swnj 		register struct ucb *up;
4214690Swnj 		int len;
4224679Swnj 
4234601Swnj 		up = tp->t_ucb;
4244601Swnj 
4254601Swnj 		/* update snd_una and snd_nxt */
4264601Swnj 		tp->snd_una = n->t_ackno;
4274601Swnj 		if (tp->snd_una > tp->snd_nxt)
4284601Swnj 			tp->snd_nxt = tp->snd_una;
4294601Swnj 		/* if timed msg acked, set retrans time value */
4304601Swnj 		if ((tp->tc_flags&TC_SYN_ACKED) &&
4314601Swnj 		    tp->snd_una > tp->t_xmt_val) {
4324601Swnj 			tp->t_xmtime = (tp->t_xmt != 0 ? tp->t_xmt : T_REXMT);
4334601Swnj 			if (tp->t_xmtime > T_REMAX)
4344601Swnj 				tp->t_xmtime = T_REMAX;
4354601Swnj 		}
4364601Swnj 
4374601Swnj 		/* remove acked data from send buf */
4384601Swnj 		len = tp->snd_una - tp->snd_off;
4394601Swnj 		m = up->uc_sbuf;
4404601Swnj 		while (len > 0 && m != NULL)
4414601Swnj 			if (m->m_len <= len) {
4424601Swnj 				len -= m->m_len;
4434601Swnj 				if (m->m_off > MMAXOFF)
4444601Swnj 					up->uc_ssize -= NMBPG;
4454601Swnj 				MFREE(m, mn);
4464601Swnj 				m = mn;
4474601Swnj 				up->uc_ssize--;
4484601Swnj 			} else {
4494601Swnj 				m->m_len -= len;
4504601Swnj 				m->m_off += len;
4514601Swnj 				break;
4524601Swnj 			}
4534601Swnj 		up->uc_sbuf = m;
4544601Swnj 		tp->snd_off = tp->snd_una;
4554601Swnj 		if ((tp->tc_flags&TC_SYN_ACKED) == 0 &&
4564601Swnj 		    (tp->snd_una > tp->iss)) {
4574601Swnj 			tp->tc_flags |= TC_SYN_ACKED;
4584601Swnj 			tp->t_init = 0;
4594601Swnj 		}
4604601Swnj 		if (tp->seq_fin != tp->iss && tp->snd_una > tp->seq_fin)
4614601Swnj 			tp->tc_flags &= ~TC_SND_FIN;
4624601Swnj 		tp->t_rexmt = 0;
4634601Swnj 		tp->t_rexmttl = 0;
4644601Swnj 		tp->tc_flags |= TC_CANCELLED;
4654601Swnj 		netwakeup(tp->t_ucb);		/* wasteful */
4664601Swnj 	}
4674601Swnj /* win */
4684601Swnj 	if ((tp->tc_flags & TC_SYN_RCVD) && n->t_seq >= tp->snd_wl) {
4694601Swnj 		tp->snd_wl = n->t_seq;
4704601Swnj 		tp->snd_wnd = n->t_win;
4714601Swnj 		tp->tc_flags |= TC_NEW_WINDOW;
4724601Swnj 		tp->t_persist = 0;
4734601Swnj 	}
4744679Swnj 	if (dataok == 0)
4754679Swnj 		goto ctlonly;
4764601Swnj /* text */
4774679Swnj 	if (n->t_len == 0)
4784679Swnj 		goto notext;
4794679Swnj 	{ register int i;
4804679Swnj 	  register struct th *p, *q;
4814679Swnj 	  register struct mbuf *m;
4824679Swnj 	  int overage;
4834601Swnj 
4844645Swnj 	/*
4854645Swnj 	 * Discard duplicate data already passed to user.
4864645Swnj 	 */
4874648Swnj 	if (SEQ_LT(n->t_seq, tp->rcv_nxt)) {
4884645Swnj 		i = tp->rcv_nxt - n->t_seq;
4894645Swnj 		if (i >= n->t_len)
4904679Swnj 			goto notext;
4914645Swnj 		n->t_seq += i;
4924645Swnj 		n->t_len -= i;
4934645Swnj 		m_adj(dtom(n), i);
4944601Swnj 	}
4954601Swnj 
4964645Swnj 	/*
4974645Swnj 	 * Find a segment which begins after this one does.
4984645Swnj 	 */
4994645Swnj 	for (q = tp->t_rcv_next; q != (struct th *)tp; q = q->t_next)
5004648Swnj 		if (SEQ_GT(q->t_seq, n->t_seq))
5014645Swnj 			break;
5024601Swnj 
5034645Swnj 	/*
5044645Swnj 	 * If there is a preceding segment, it may provide some of
5054645Swnj 	 * our data already.  If so, drop the data from the incoming
5064645Swnj 	 * segment.  If it provides all of our data, drop us.
5074645Swnj 	 */
5084645Swnj 	if (q->t_prev != (struct th *)tp) {
5094648Swnj 		/* conversion to int (in i) handles seq wraparound */
5104645Swnj 		i = q->t_prev->t_seq + q->t_prev->t_len - n->t_seq;
5114645Swnj 		if (i > 0) {
5124645Swnj 			if (i >= n->t_len)
5134679Swnj 				goto notext;	/* w/o setting TC_NET_KEEP */
5144645Swnj 			m_adj(dtom(tp), i);
5154645Swnj 			n->t_len -= i;
5164645Swnj 			n->t_seq += i;
5174601Swnj 		}
5184601Swnj 	}
5194601Swnj 
5204645Swnj 	/*
5214645Swnj 	 * While we overlap succeeding segments trim them or,
5224645Swnj 	 * if they are completely covered, dequeue them.
5234645Swnj 	 */
5244648Swnj 	while (q != (struct th *)tp && SEQ_GT(n->t_seq + n->t_len, q->t_seq)) {
5254645Swnj 		i = (n->t_seq + n->t_len) - q->t_seq;
5264645Swnj 		if (i < q->t_len) {
5274645Swnj 			q->t_len -= i;
5284645Swnj 			m_adj(dtom(q), i);
5294645Swnj 			break;
5304601Swnj 		}
5314645Swnj 		q = q->t_next;
5324645Swnj 		m_freem(dtom(q->t_prev));
5334645Swnj 		remque(q->t_prev);
5344645Swnj 	}
5354601Swnj 
5364645Swnj 	/*
5374645Swnj 	 * Stick new segment in its place.
5384645Swnj 	 */
5394645Swnj 	insque(n, q->t_prev);
5404656Swnj 	tp->seqcnt += n->t_len;
5414601Swnj 
5424601Swnj #ifdef notdef
5434645Swnj 	/*
5444645Swnj 	 * Calculate available space and discard segments for
5454645Swnj 	 * which there is too much.
5464645Swnj 	 */
5474645Swnj 	q = tp->t_rcv_prev;
5484648Swnj 	overage =
5494679Swnj 	    (tp->t_ucb->uc_rcc + tp->rcv_seqcnt) - tp->t_ucb->uc_rhiwat;
5504645Swnj 	if (overage > 0)
5514645Swnj 		for (;;) {
5524645Swnj 			i = MIN(q->t_len, overage);
5534645Swnj 			overage -= i;
5544645Swnj 			q->t_len -= i;
5554645Swnj 			m_adj(q, -i);
5564645Swnj 			if (q == n)
5574645Swnj 				tp->tc_flags |= TC_DROPPED_TXT;
5584645Swnj 			if (q->t_len)
5594645Swnj 				break;
5604645Swnj 			if (q == n)
5614648Swnj 				panic("tcp_text dropall");
5624645Swnj 			q = q->t_prev;
5634645Swnj 			remque(q->t_next);
5644645Swnj 		}
5654645Swnj #endif
5664601Swnj 
5674645Swnj 	/*
5684648Swnj 	 * Advance rcv_next through
5694648Swnj 	 * newly completed sequence space
5704648Swnj 	 * and return forcing an ack.
5714645Swnj 	 */
5724645Swnj 	while (n->t_seq == tp->rcv_nxt) {
5734648Swnj 		/* present data belongs here */
5744645Swnj 		tp->rcv_nxt += n->t_len;
5754645Swnj 		n = n->t_next;
5764645Swnj 		if (n == (struct th *)tp)
5774645Swnj 			break;
5784645Swnj 	}
5794645Swnj 	tp->tc_flags |= (TC_ACK_DUE|TC_NET_KEEP);
5804679Swnj 	}
5814679Swnj notext:
5824690Swnj urgeolfin:
5834679Swnj /* urg */
5844679Swnj 	if (n->th_flags&TH_URG) {
5854679Swnj 		unsigned urgent;
5864601Swnj 
5874679Swnj 		urgent = n->t_urp + n->t_seq;
5884679Swnj 		if (tp->rcv_nxt < urgent) {
5894679Swnj 			if (tp->rcv_urp <= tp->rcv_nxt)
5904679Swnj 				to_user(tp->t_ucb, UURGENT);
5914679Swnj 			tp->rcv_urp = urgent;
5924679Swnj 		}
5934679Swnj 	}
5944679Swnj /* eol */
5954679Swnj 	if ((n->th_flags&TH_EOL) &&
5964679Swnj 	    (tp->tc_flags&TC_DROPPED_TXT) == 0 &&
5974679Swnj 	    tp->t_rcv_prev != (struct th *)tp) {
5984679Swnj 		/* mark last mbuf */
5994679Swnj 		m = dtom(tp->t_rcv_prev);
6004679Swnj 		if (m != NULL) {
6014679Swnj 			while (m->m_next != NULL)
6024679Swnj 				m = m->m_next;
6034679Swnj 			m->m_act =
6044679Swnj 			    (struct mbuf *)(m->m_off + m->m_len - 1);
6054679Swnj 		}
6064679Swnj 	}
6074679Swnj ctlonly:
6084679Swnj /* fin */
6094679Swnj 	if ((n->th_flags&TH_FIN) && (tp->tc_flags&TC_DROPPED_TXT) == 0) {
6104679Swnj 		seq_t last;
6114679Swnj 
6124679Swnj 		if ((tp->tc_flags&TC_FIN_RCVD) == 0) {
6134679Swnj 			/* do we really have fin ? */
6144679Swnj 			last = firstempty(tp);
6154679Swnj 			if (tp->t_rcv_prev == (struct th *)tp ||
6164679Swnj 			    last == t_end(tp->t_rcv_prev)) {
6174679Swnj 				tp->tc_flags |= TC_FIN_RCVD;
6184679Swnj 				netwakeup(tp->t_ucb);		/* poke */
6194679Swnj 			}
6204679Swnj 			if ((tp->tc_flags&TC_FIN_RCVD) &&
6214679Swnj 			    tp->rcv_nxt >= last) {
6224679Swnj 				tp->rcv_nxt = last + 1;		/* fin seq */
6234679Swnj 				tp->tc_flags |= TC_ACK_DUE;
6244679Swnj 			}
6254679Swnj 		} else
6264679Swnj 			tp->tc_flags |= TC_ACK_DUE;
6274679Swnj 	}
6284679Swnj 
6294679Swnj /* respond */
6304679Swnj 	sent = 0;
6314679Swnj 	if (tp->tc_flags&TC_ACK_DUE)
6324679Swnj 		sent = tcp_sndctl(tp);
6334679Swnj 	else if (tp->tc_flags&TC_NEW_WINDOW) {
6344679Swnj 		seq_t last = tp->snd_off;
6354679Swnj 		for (m = tp->t_ucb->uc_sbuf; m != NULL; m = m->m_next)	/*###*/
6364679Swnj 			last += m->m_len;				/*###*/
6374679Swnj 		if (tp->snd_nxt <= last || (tp->tc_flags&TC_SND_FIN))
6384679Swnj 			sent = tcp_send(tp);
6394679Swnj 	}
6404679Swnj 
6414679Swnj /* set for retrans */
6424679Swnj 	if (!sent && tp->snd_una < tp->snd_nxt &&
6434679Swnj 	    (tp->tc_flags&TC_CANCELLED)) {
6444679Swnj 		tp->t_rexmt = tp->t_xmtime;
6454679Swnj 		tp->t_rexmttl = T_REXMTTL;
6464679Swnj 		tp->t_rexmt_val = tp->t_rtl_val = tp->snd_lst;
6474679Swnj 		tp->tc_flags &= ~TC_CANCELLED;
6484679Swnj 	}
6494690Swnj /* present data to user */
6504690Swnj 	{ register struct mbuf **mp;
6514690Swnj 	  register struct ucb *up = tp->t_ucb;
6524690Swnj 	  seq_t ready;
6534601Swnj 
6544601Swnj 	/* connection must be synced and data available for user */
6554656Swnj 	if ((tp->tc_flags&TC_SYN_ACKED) == 0)
6564601Swnj 		return;
6574679Swnj 	up = tp->t_ucb;
6584601Swnj 	mp = &up->uc_rbuf;
6594601Swnj 	while (*mp)
6604601Swnj 		mp = &(*mp)->m_next;
6614690Swnj 	n = tp->t_rcv_next;
6624656Swnj 	/* SHOULD PACK DATA IN HERE */
6634690Swnj 	while (n != (struct th *)tp && n->t_seq < tp->rcv_nxt) {
6644690Swnj 		remque(n);
6654690Swnj 		m = dtom(n);
6664690Swnj 		up->uc_rcc += n->t_len;
6674690Swnj 		tp->seqcnt -= n->t_len;
6684656Swnj 		if (tp->seqcnt < 0) panic("present_data");
6694690Swnj 		n = n->t_next;
6704601Swnj 		while (m) {
6714601Swnj 			if (m->m_len == 0) {
6724692Swnj 				MFREE(m, *mp);
6734692Swnj 			} else {
6744692Swnj 				*mp = m;
6754692Swnj 				mp = &m->m_next;
6764601Swnj 			}
6774601Swnj 			m = *mp;
6784601Swnj 		}
6794601Swnj 	}
6804656Swnj 	if (up->uc_rcc != 0)
6814601Swnj 		netwakeup(up);
6824656Swnj 	if ((tp->tc_flags&TC_FIN_RCVD) &&			/* ### */
6834656Swnj 	    (tp->tc_flags&TC_USR_CLOSED) == 0 &&		/* ### */
6844656Swnj 	    rcv_empty(tp))					/* ### */
6854656Swnj 		to_user(up, UCLOSED);				/* ### */
6864690Swnj 	}
6874601Swnj }
688