xref: /csrg-svn/sys/netinet/tcp_input.c (revision 4656)
1*4656Swnj /* tcp_input.c 1.6 81/10/29 */
24601Swnj 
34601Swnj #include "../h/param.h"
44601Swnj #include "../h/systm.h"
54601Swnj #include "../bbnnet/net.h"
64601Swnj #include "../bbnnet/mbuf.h"
74601Swnj #include "../bbnnet/host.h"
84601Swnj #include "../bbnnet/imp.h"
94601Swnj #include "../bbnnet/ucb.h"
104601Swnj #include "../bbnnet/tcp.h"
114601Swnj #include "../bbnnet/ip.h"
124601Swnj #include "../h/dir.h"
134601Swnj #include "../h/user.h"
144601Swnj #include "../h/inode.h"
154601Swnj #include "../bbnnet/fsm.h"
164601Swnj 
174601Swnj extern int nosum;
184601Swnj 
194601Swnj tcp_input(mp)
204601Swnj 	register struct mbuf *mp;
214601Swnj {
224601Swnj 	register struct tcb *tp;
234601Swnj 	register struct th *n;
244601Swnj 	int nstate;
254601Swnj 	struct mbuf *m;
264601Swnj 	struct ucb *up;
274601Swnj 	int hlen, tlen, j;
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 
504601Swnj 	/*
514601Swnj 	 * Checksum extended header and data
524601Swnj 	 */
534601Swnj 	j = n->t_sum; n->t_sum = 0;
544601Swnj 	if (j != cksum(mp, sizeof (struct ip) + tlen)) {
554601Swnj 		netstat.t_badsum++;
564601Swnj 		if (nosum == 0) {
574601Swnj 			m_freem(mp);
584601Swnj 			return;
594601Swnj 		}
604601Swnj 	}
614601Swnj 
624601Swnj 	/*
634601Swnj 	 * Find tcb for message (SHOULDN'T USE LINEAR SEARCH!)
644601Swnj 	 */
654601Swnj 	for (tp = netcb.n_tcb_head; tp != 0; tp = tp->t_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;
694601Swnj 	for (tp = netcb.n_tcb_head; tp != 0; tp = tp->t_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)) {
964601Swnj 			send_rst(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)) {
1054601Swnj 			send_rst(tp, n);			/* 71,72,75 */
1064601Swnj 			goto badseg;
1074601Swnj 		}
1084601Swnj 		if (n->th_flags&TH_RST) {
1094601Swnj 			t_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:
1334601Swnj 			t_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) {
1424601Swnj 			send_rst(tp, n);			/* 74 */
1434601Swnj 			goto badseg;
1444601Swnj 		}
1454601Swnj 		if (syn_ok(tp, n) && n->t_seq != tp->irs) {
1464601Swnj 			send_null(tp);				/* 74 */
1474601Swnj 			goto badseg;
1484601Swnj 		}
1494601Swnj 		goto goodseg;
1504601Swnj 	}
1514601Swnj badseg:
1524601Swnj 	m_freem(mp);
1534601Swnj 	return;
1544601Swnj 
1554601Swnj goodseg:
1564601Swnj #ifdef notdef
1574601Swnj 	/*
1584601Swnj 	 * Defer processing if no buffer space for this connection.
1594601Swnj 	 */
1604601Swnj 	up = tp->t_ucb;
161*4656Swnj 	if (up->uc_rcc > up->uc_rhiwat &&
1624601Swnj 	     && n->t_len != 0 && netcb.n_bufs < netcb.n_lowat) {
1634601Swnj 		mp->m_act = (struct mbuf *)0;
1644601Swnj 		if ((m = tp->t_rcv_unack) != NULL) {
1654601Swnj 			while (m->m_act != NULL)
1664601Swnj 				m = m->m_act;
1674601Swnj 			m->m_act = mp;
1684601Swnj 		} else
1694601Swnj 			tp->t_rcv_unack = mp;
1704601Swnj 		return;
1714601Swnj 	}
1724601Swnj #endif
1734601Swnj 
1744601Swnj 	/*
1754601Swnj 	 * Discard ip header, and do tcp input processing.
1764601Swnj 	 */
1774601Swnj 	hlen += sizeof(struct ip);
1784601Swnj 	mp->m_off += hlen;
1794601Swnj 	mp->m_len -= hlen;
1804601Swnj 	nstate = tp->t_state;
1814601Swnj 	tp->tc_flags &= ~TC_NET_KEEP;
1824601Swnj 	acounts[tp->t_state][INRECV]++;
1834601Swnj #ifdef TCPDEBUG
1844601Swnj 	if ((tp->t_ucb->uc_flags & UDEBUG) || tcpconsdebug) {
1854604Swnj 		tdb_setup(tp, n, INRECV, &tdb);
1864601Swnj 	} else
1874601Swnj 		tdb.td_tod = 0;
1884601Swnj #endif
1894601Swnj 	switch (tp->t_state) {
1904601Swnj 
1914601Swnj 	case LISTEN:
1924601Swnj 		if (!syn_ok(tp, n) ||
1934601Swnj 		    ((tp->t_ucb->uc_host = h_make(&n->t_s)) == 0)) {
1944601Swnj 			nstate = EFAILEC;
1954601Swnj 			goto done;
1964601Swnj 		}
1974601Swnj 		tp->t_fport = n->t_src;
1984603Sroot 		tp->t_ucb->uc_template = tcp_template(tp);
1994601Swnj 		rcv_ctldat(tp, n, 1);
2004601Swnj 		if (tp->tc_flags&TC_FIN_RCVD) {
2014601Swnj 			tp->t_finack = T_2ML;			/* 3 */
2024601Swnj 			tp->tc_flags &= ~TC_WAITED_2_ML;
2034601Swnj 			nstate = CLOSE_WAIT;
2044601Swnj 		} else {
2054601Swnj 			tp->t_init = T_INIT / 2;		/* 4 */
2064601Swnj 			nstate = L_SYN_RCVD;
2074601Swnj 		}
2084601Swnj 		goto done;
2094601Swnj 
2104601Swnj 	case SYN_SENT:
2114601Swnj 		if (!syn_ok(tp, n)) {
2124601Swnj 			nstate = EFAILEC;
2134601Swnj 			goto done;
2144601Swnj 		}
2154601Swnj 		rcv_ctldat(tp, n, 1);
2164601Swnj 		if (tp->tc_flags&TC_FIN_RCVD) {
2174601Swnj 			if (n->th_flags&TH_ACK) {
2184601Swnj 				if (n->t_ackno > tp->iss)
2194601Swnj 					present_data(tp);	/* 32 */
2204601Swnj 			} else {
2214601Swnj 				tp->t_finack = T_2ML;		/* 9 */
2224601Swnj 				tp->tc_flags &= ~TC_WAITED_2_ML;
2234601Swnj 			}
2244601Swnj 			nstate = CLOSE_WAIT;
2254601Swnj 			goto done;
2264601Swnj 		}
2274601Swnj 		if (n->th_flags&TH_ACK) {
2284601Swnj 			present_data(tp);			/* 11 */
2294601Swnj 			nstate = ESTAB;
2304601Swnj 		} else
2314601Swnj 			nstate = SYN_RCVD;			/* 8 */
2324601Swnj 		goto done;
2334601Swnj 
2344601Swnj 	case SYN_RCVD:
2354601Swnj 	case L_SYN_RCVD:
2364601Swnj 		if ((n->th_flags&TH_ACK) == 0 ||
2374601Swnj 		    (n->th_flags&TH_ACK) && n->t_ackno <= tp->iss) {
2384601Swnj 			nstate = EFAILEC;
2394601Swnj 			goto done;
2404601Swnj 		}
2414601Swnj 		goto input;
2424601Swnj 
2434601Swnj 	case ESTAB:
2444601Swnj 	case FIN_W1:
2454601Swnj 	case FIN_W2:
2464601Swnj 	case TIME_WAIT:
2474601Swnj input:
2484601Swnj 		rcv_ctldat(tp, n, 1);				/* 39 */
2494601Swnj 		present_data(tp);
2504601Swnj 		switch (tp->t_state) {
2514601Swnj 
2524601Swnj 		case ESTAB:
2534601Swnj 			if (tp->tc_flags&TC_FIN_RCVD)
2544601Swnj 				nstate = CLOSE_WAIT;
2554601Swnj 			break;
2564601Swnj 
2574601Swnj 		case SYN_RCVD:
2584601Swnj 		case L_SYN_RCVD:
2594601Swnj 			nstate = (tp->tc_flags&TC_FIN_RCVD) ?
2604601Swnj 			    CLOSE_WAIT : ESTAB;			 /* 33:5 */
2614601Swnj 			break;
2624601Swnj 
2634601Swnj 		case FIN_W1:
2644601Swnj 			j = ack_fin(tp, n);
2654601Swnj 			if ((tp->tc_flags & TC_FIN_RCVD) == 0) {
2664601Swnj 				if (j)
2674601Swnj 					nstate = FIN_W2;	/* 27 */
2684601Swnj 				break;
2694601Swnj 			}
2704601Swnj 			tp->t_finack = T_2ML;
2714601Swnj 			tp->tc_flags &= ~TC_WAITED_2_ML;
2724601Swnj 			nstate = j ? TIME_WAIT : CLOSING1;	/* 28:26 */
2734601Swnj 			break;
2744601Swnj 
2754601Swnj 		case FIN_W2:
2764601Swnj 			if (tp->tc_flags&TC_FIN_RCVD) {
2774601Swnj 				tp->t_finack = T_2ML;		/* 29 */
2784601Swnj 				tp->tc_flags &= ~TC_WAITED_2_ML;
2794601Swnj 				nstate = TIME_WAIT;
2804601Swnj 				break;
2814601Swnj 			}
2824601Swnj 			break;
2834601Swnj 		}
2844601Swnj 		goto done;
2854601Swnj 
2864601Swnj 	case CLOSE_WAIT:
2874601Swnj 		if (n->th_flags&TH_FIN) {
2884601Swnj 			if ((n->th_flags&TH_ACK) &&
2894601Swnj 			    n->t_ackno <= tp->seq_fin) {
2904601Swnj 				rcv_ctldat(tp, n, 0);		/* 30 */
2914601Swnj 				tp->t_finack = T_2ML;
2924601Swnj 				tp->tc_flags &= ~TC_WAITED_2_ML;
2934601Swnj 			} else
2944601Swnj 				send_ctl(tp);			/* 31 */
2954601Swnj 			goto done;
2964601Swnj 		}
2974601Swnj 		goto input;
2984601Swnj 
2994601Swnj 	case CLOSING1:
3004601Swnj 		j = ack_fin(tp, n);
3014601Swnj 		if (n->th_flags&TH_FIN) {
3024601Swnj 			rcv_ctldat(tp, n, 0);
3034601Swnj 			tp->t_finack = T_2ML;
3044601Swnj 			tp->tc_flags &= ~TC_WAITED_2_ML;
3054601Swnj 			if (j)
3064601Swnj 				nstate = TIME_WAIT;		/* 23 */
3074601Swnj 			goto done;
3084601Swnj 		}
3094601Swnj 		if (j) {
3104601Swnj 			if (tp->tc_flags&TC_WAITED_2_ML)
3114601Swnj 				if (rcv_empty(tp)) {
3124601Swnj 					t_close(tp, UCLOSED);	/* 15 */
3134601Swnj 					nstate = CLOSED;
3144601Swnj 				} else
3154601Swnj 					nstate = RCV_WAIT;	/* 18 */
3164601Swnj 			else
3174601Swnj 				nstate = TIME_WAIT;
3184601Swnj 			goto done;
3194601Swnj 		}
3204601Swnj 		goto input;
3214601Swnj 
3224601Swnj 	case CLOSING2:
3234601Swnj 		if (ack_fin(tp, n)) {
3244601Swnj 			if (rcv_empty(tp)) {			/* 16 */
3254601Swnj 				t_close(tp, UCLOSED);
3264601Swnj 				nstate = CLOSED;
3274601Swnj 			} else
3284601Swnj 				nstate = RCV_WAIT;		/* 19 */
3294601Swnj 			goto done;
3304601Swnj 		}
3314601Swnj 		if (n->th_flags&TH_FIN) {
3324601Swnj 			send_ctl(tp);				/* 31 */
3334601Swnj 			goto done;
3344601Swnj 		}
3354601Swnj 		goto input;
3364601Swnj 
3374601Swnj 	case RCV_WAIT:
3384601Swnj 		if ((n->th_flags&TH_FIN) && (n->th_flags&TH_ACK) &&
3394601Swnj 		    n->t_ackno <= tp->seq_fin) {
3404601Swnj 			rcv_ctldat(tp, n, 0);
3414601Swnj 			tp->t_finack = T_2ML;
3424601Swnj 			tp->tc_flags &= ~TC_WAITED_2_ML;	/* 30 */
3434601Swnj 		}
3444601Swnj 		goto done;
3454601Swnj 	}
3464601Swnj 	panic("tcp_input");
3474601Swnj done:
3484601Swnj 
3494601Swnj 	/*
3504601Swnj 	 * Done with state*input specific processing.
3514601Swnj 	 * Form trace records, free input if not needed,
3524601Swnj 	 * and enter new state.
3534601Swnj 	 */
3544601Swnj #ifdef TCPDEBUG
3554604Swnj 	if (tdb.td_tod)
3564604Swnj 		tdb_stuff(&tdb, nstate);
3574601Swnj #endif
3584601Swnj 	switch (nstate) {
3594601Swnj 
3604601Swnj 	case EFAILEC:
3614601Swnj 		m_freem(mp);
3624601Swnj 		return;
3634601Swnj 
3644601Swnj 	default:
3654601Swnj 		tp->t_state = nstate;
3664601Swnj 		/* fall into ... */
3674601Swnj 
3684601Swnj 	case CLOSED:
3694601Swnj 		/* IF CLOSED CANT LOOK AT tc_flags */
3704601Swnj 		if ((tp->tc_flags&TC_NET_KEEP) == 0)
3714601Swnj 			m_freem(mp);
3724601Swnj 		return;
3734601Swnj 	}
3744601Swnj 	/* NOTREACHED */
3754601Swnj 
3764601Swnj 	/*
3774601Swnj 	 * Unwanted packed; free everything
3784601Swnj 	 * but the header and return an rst.
3794601Swnj 	 */
3804601Swnj notwanted:
3814601Swnj 	m_freem(mp->m_next);
3824601Swnj 	mp->m_next = NULL;
3834601Swnj 	mp->m_len = sizeof(struct th);
3844601Swnj #define xchg(a,b) j=a; a=b; b=j
3854601Swnj 	xchg(n->t_d.s_addr, n->t_s.s_addr); xchg(n->t_dst, n->t_src);
3864601Swnj #undef xchg
3874601Swnj 	if (n->th_flags&TH_ACK)
3884601Swnj 		n->t_seq = n->t_ackno;
3894601Swnj 	else {
3904601Swnj 		n->t_ackno = htonl(ntohl(n->t_seq) + tlen - hlen);
3914601Swnj 		n->t_seq = 0;
3924601Swnj 	}
3934601Swnj 	n->th_flags = TH_RST; /* not TH_FIN, TH_SYN */
3944601Swnj 	n->th_flags ^= TH_ACK;
3954601Swnj 	n->t_len = htons(TCPSIZE);
3964601Swnj 	n->t_off = 5;
3974601Swnj 	n->t_sum = cksum(mp, sizeof(struct th));
3984601Swnj 	((struct ip *)n)->ip_len = sizeof(struct th);
3994601Swnj 	ip_output(mp);
4004601Swnj 	netstat.t_badsegs++;
4014601Swnj }
4024601Swnj 
4034601Swnj rcv_ctldat(tp, n, dataok)
4044601Swnj 	register struct tcb *tp;
4054601Swnj 	register struct th *n;
4064601Swnj {
4074601Swnj 	register sent;
4084601Swnj 	register struct ucb *up;
4094601Swnj 	register struct mbuf *m, *mn;
4104601Swnj 	register len;
4114601Swnj COUNT(RCV_CTLDAT);
4124601Swnj 
4134601Swnj 	tp->tc_flags &= ~(TC_DROPPED_TXT|TC_ACK_DUE|TC_NEW_WINDOW);
4144601Swnj /* syn */
4154601Swnj 	if ((tp->tc_flags&TC_SYN_RCVD) == 0 && (n->th_flags&TH_SYN)) {
4164601Swnj 		tp->irs = n->t_seq;
4174601Swnj 		tp->rcv_nxt = n->t_seq + 1;
4184601Swnj 		tp->snd_wl = tp->rcv_urp = tp->irs;
4194601Swnj 		tp->tc_flags |= (TC_SYN_RCVD|TC_ACK_DUE);
4204601Swnj 	}
4214601Swnj /* ack */
4224601Swnj 	if ((n->th_flags&TH_ACK) && (tp->tc_flags&TC_SYN_RCVD) &&
4234601Swnj 	    n->t_ackno > tp->snd_una) {
4244601Swnj 		up = tp->t_ucb;
4254601Swnj 
4264601Swnj 		/* update snd_una and snd_nxt */
4274601Swnj 		tp->snd_una = n->t_ackno;
4284601Swnj 		if (tp->snd_una > tp->snd_nxt)
4294601Swnj 			tp->snd_nxt = tp->snd_una;
4304601Swnj 
4314601Swnj 		/* if timed msg acked, set retrans time value */
4324601Swnj 		if ((tp->tc_flags&TC_SYN_ACKED) &&
4334601Swnj 		    tp->snd_una > tp->t_xmt_val) {
4344601Swnj 			tp->t_xmtime = (tp->t_xmt != 0 ? tp->t_xmt : T_REXMT);
4354601Swnj 			if (tp->t_xmtime > T_REMAX)
4364601Swnj 				tp->t_xmtime = T_REMAX;
4374601Swnj 		}
4384601Swnj 
4394601Swnj 		/* remove acked data from send buf */
4404601Swnj 		len = tp->snd_una - tp->snd_off;
4414601Swnj 		m = up->uc_sbuf;
4424601Swnj 		while (len > 0 && m != NULL)
4434601Swnj 			if (m->m_len <= len) {
4444601Swnj 				len -= m->m_len;
4454601Swnj 				if (m->m_off > MMAXOFF)
4464601Swnj 					up->uc_ssize -= NMBPG;
4474601Swnj 				MFREE(m, mn);
4484601Swnj 				m = mn;
4494601Swnj 				up->uc_ssize--;
4504601Swnj 			} else {
4514601Swnj 				m->m_len -= len;
4524601Swnj 				m->m_off += len;
4534601Swnj 				break;
4544601Swnj 			}
4554601Swnj 		up->uc_sbuf = m;
4564601Swnj 		tp->snd_off = tp->snd_una;
4574601Swnj 		if ((tp->tc_flags&TC_SYN_ACKED) == 0 &&
4584601Swnj 		    (tp->snd_una > tp->iss)) {
4594601Swnj 			tp->tc_flags |= TC_SYN_ACKED;
4604601Swnj 			tp->t_init = 0;
4614601Swnj 		}
4624601Swnj 		if (tp->seq_fin != tp->iss && tp->snd_una > tp->seq_fin)
4634601Swnj 			tp->tc_flags &= ~TC_SND_FIN;
4644601Swnj 		tp->t_rexmt = 0;
4654601Swnj 		tp->t_rexmttl = 0;
4664601Swnj 		tp->tc_flags |= TC_CANCELLED;
4674601Swnj 		netwakeup(tp->t_ucb);		/* wasteful */
4684601Swnj 	}
4694601Swnj /* win */
4704601Swnj 	if ((tp->tc_flags & TC_SYN_RCVD) && n->t_seq >= tp->snd_wl) {
4714601Swnj 		tp->snd_wl = n->t_seq;
4724601Swnj 		tp->snd_wnd = n->t_win;
4734601Swnj 		tp->tc_flags |= TC_NEW_WINDOW;
4744601Swnj 		tp->t_persist = 0;
4754601Swnj 	}
4764601Swnj 	if (dataok) {
4774601Swnj /* text */
4784601Swnj 		if (n->t_len != 0)
4794601Swnj 			rcv_text(tp, n);
4804601Swnj /* urg */
4814601Swnj 		if (n->th_flags&TH_URG) {
4824601Swnj 			unsigned urgent;
4834601Swnj 
4844601Swnj 			urgent = n->t_urp + n->t_seq;
4854601Swnj 			if (tp->rcv_nxt < urgent) {
4864601Swnj 				if (tp->rcv_urp <= tp->rcv_nxt)
4874601Swnj 					to_user(tp->t_ucb, UURGENT);
4884601Swnj 				tp->rcv_urp = urgent;
4894601Swnj 			}
4904601Swnj 		}
4914601Swnj /* eol */
4924601Swnj 		if ((n->th_flags&TH_EOL) &&
4934601Swnj 		    (tp->tc_flags&TC_DROPPED_TXT) == 0 &&
4944601Swnj 		    tp->t_rcv_prev != (struct th *)tp) {
4954601Swnj 			/* mark last mbuf */
4964601Swnj 			m = dtom(tp->t_rcv_prev);
4974601Swnj 			if (m != NULL) {
4984601Swnj 				while (m->m_next != NULL)
4994601Swnj 					m = m->m_next;
5004601Swnj 				m->m_act =
5014601Swnj 				    (struct mbuf *)(m->m_off + m->m_len - 1);
5024601Swnj 			}
5034601Swnj 		}
5044601Swnj 	}
5054601Swnj /* fin */
5064601Swnj 	if ((n->th_flags&TH_FIN) && (tp->tc_flags&TC_DROPPED_TXT) == 0) {
5074601Swnj 		int last;
5084601Swnj 
5094601Swnj 		if ((tp->tc_flags&TC_FIN_RCVD) == 0) {
5104601Swnj 			/* do we really have fin ? */
5114601Swnj 			last = firstempty(tp);
5124601Swnj 			if (tp->t_rcv_prev == (struct th *)tp ||
5134601Swnj 			    last == t_end(tp->t_rcv_prev)) {
5144601Swnj 				tp->tc_flags |= TC_FIN_RCVD;
5154601Swnj 				netwakeup(tp->t_ucb);		/* poke */
5164601Swnj 			}
5174601Swnj 			if ((tp->tc_flags&TC_FIN_RCVD) &&
5184601Swnj 			    tp->rcv_nxt >= last) {
5194601Swnj 				tp->rcv_nxt = last + 1;		/* fin seq */
5204601Swnj 				tp->tc_flags |= TC_ACK_DUE;
5214601Swnj 			}
5224601Swnj 		} else
5234601Swnj 			tp->tc_flags |= TC_ACK_DUE;
5244601Swnj 	}
5254601Swnj 
5264601Swnj /* respond */
5274601Swnj 	if (tp->tc_flags&TC_ACK_DUE)
5284601Swnj 		sent = send_ctl(tp);
5294601Swnj 	else if (tp->tc_flags&TC_NEW_WINDOW)
5304601Swnj 		sent = send(tp);
5314601Swnj 	else
5324601Swnj 		sent = 0;
5334601Swnj 
5344601Swnj /* set for retrans */
5354601Swnj 	if (!sent && tp->snd_una < tp->snd_nxt &&
5364601Swnj 	    (tp->tc_flags&TC_CANCELLED)) {
5374601Swnj 		tp->t_rexmt = tp->t_xmtime;
5384601Swnj 		tp->t_rexmttl = T_REXMTTL;
5394601Swnj 		tp->t_rexmt_val = tp->t_rtl_val = tp->snd_lst;
5404601Swnj 		tp->tc_flags &= ~TC_CANCELLED;
5414601Swnj 	}
5424601Swnj }
5434601Swnj 
5444645Swnj rcv_text(tp, n)
5454601Swnj 	register struct tcb *tp;
5464645Swnj 	register struct th *n;
5474601Swnj {
5484645Swnj 	register int i;
5494601Swnj 	register struct th *p, *q;
5504645Swnj 	register struct mbuf *m;
5514645Swnj 	int overage;
5524601Swnj COUNT(RCV_TEXT);
5534601Swnj 
5544645Swnj 	/*
5554645Swnj 	 * Discard duplicate data already passed to user.
5564645Swnj 	 */
5574648Swnj 	if (SEQ_LT(n->t_seq, tp->rcv_nxt)) {
5584645Swnj 		i = tp->rcv_nxt - n->t_seq;
5594645Swnj 		if (i >= n->t_len)
5604645Swnj 			goto dropseg;
5614645Swnj 		n->t_seq += i;
5624645Swnj 		n->t_len -= i;
5634645Swnj 		m_adj(dtom(n), i);
5644601Swnj 	}
5654601Swnj 
5664645Swnj 	/*
5674645Swnj 	 * Find a segment which begins after this one does.
5684645Swnj 	 */
5694645Swnj 	for (q = tp->t_rcv_next; q != (struct th *)tp; q = q->t_next)
5704648Swnj 		if (SEQ_GT(q->t_seq, n->t_seq))
5714645Swnj 			break;
5724601Swnj 
5734645Swnj 	/*
5744645Swnj 	 * If there is a preceding segment, it may provide some of
5754645Swnj 	 * our data already.  If so, drop the data from the incoming
5764645Swnj 	 * segment.  If it provides all of our data, drop us.
5774645Swnj 	 */
5784645Swnj 	if (q->t_prev != (struct th *)tp) {
5794648Swnj 		/* conversion to int (in i) handles seq wraparound */
5804645Swnj 		i = q->t_prev->t_seq + q->t_prev->t_len - n->t_seq;
5814645Swnj 		if (i > 0) {
5824645Swnj 			if (i >= n->t_len)
5834645Swnj 				goto dropseg;
5844645Swnj 			m_adj(dtom(tp), i);
5854645Swnj 			n->t_len -= i;
5864645Swnj 			n->t_seq += i;
5874601Swnj 		}
5884601Swnj 	}
5894601Swnj 
5904645Swnj 	/*
5914645Swnj 	 * While we overlap succeeding segments trim them or,
5924645Swnj 	 * if they are completely covered, dequeue them.
5934645Swnj 	 */
5944648Swnj 	while (q != (struct th *)tp && SEQ_GT(n->t_seq + n->t_len, q->t_seq)) {
5954645Swnj 		i = (n->t_seq + n->t_len) - q->t_seq;
5964645Swnj 		if (i < q->t_len) {
5974645Swnj 			q->t_len -= i;
5984645Swnj 			m_adj(dtom(q), i);
5994645Swnj 			break;
6004601Swnj 		}
6014645Swnj 		q = q->t_next;
6024645Swnj 		m_freem(dtom(q->t_prev));
6034645Swnj 		remque(q->t_prev);
6044645Swnj 	}
6054601Swnj 
6064645Swnj 	/*
6074645Swnj 	 * Stick new segment in its place.
6084645Swnj 	 */
6094645Swnj 	insque(n, q->t_prev);
610*4656Swnj 	tp->seqcnt += n->t_len;
6114601Swnj 
6124601Swnj #ifdef notdef
6134645Swnj 	/*
6144645Swnj 	 * Calculate available space and discard segments for
6154645Swnj 	 * which there is too much.
6164645Swnj 	 */
6174645Swnj 	q = tp->t_rcv_prev;
6184648Swnj 	overage =
6194648Swnj 	    (tp->t_socket->uc_rcc + tp->rcv_seqcnt) - tp->t_socket->uc_rhiwat;
6204645Swnj 	if (overage > 0)
6214645Swnj 		for (;;) {
6224645Swnj 			i = MIN(q->t_len, overage);
6234645Swnj 			overage -= i;
6244645Swnj 			q->t_len -= i;
6254645Swnj 			m_adj(q, -i);
6264645Swnj 			if (q == n)
6274645Swnj 				tp->tc_flags |= TC_DROPPED_TXT;
6284645Swnj 			if (q->t_len)
6294645Swnj 				break;
6304645Swnj 			if (q == n)
6314648Swnj 				panic("tcp_text dropall");
6324645Swnj 			q = q->t_prev;
6334645Swnj 			remque(q->t_next);
6344645Swnj 		}
6354645Swnj #endif
6364601Swnj 
6374645Swnj 	/*
6384648Swnj 	 * Advance rcv_next through
6394648Swnj 	 * newly completed sequence space
6404648Swnj 	 * and return forcing an ack.
6414645Swnj 	 */
6424645Swnj 	while (n->t_seq == tp->rcv_nxt) {
6434648Swnj 		/* present data belongs here */
6444645Swnj 		tp->rcv_nxt += n->t_len;
6454645Swnj 		n = n->t_next;
6464645Swnj 		if (n == (struct th *)tp)
6474645Swnj 			break;
6484645Swnj 	}
6494645Swnj 	tp->tc_flags |= (TC_ACK_DUE|TC_NET_KEEP);
6504645Swnj 	return;
6514601Swnj 
6524645Swnj dropseg:
6534648Swnj 	/* don't set TC_NET_KEEP, so that mbuf's will get dropped */
6544645Swnj 	return;
6554601Swnj }
6564601Swnj 
657*4656Swnj #define	socket		ucb			/* ### */
658*4656Swnj #define	t_socket	t_ucb			/* ### */
659*4656Swnj 
6604601Swnj present_data(tp)
6614601Swnj 	register struct tcb *tp;
6624601Swnj {
6634601Swnj 	register struct th *t;
664*4656Swnj 	register struct socket *up;
6654601Swnj 	register struct mbuf *m, **mp;
6664601Swnj 	seq_t ready;
6674601Swnj COUNT(PRESENT_DATA);
6684601Swnj 
6694601Swnj 	/* connection must be synced and data available for user */
670*4656Swnj 	if ((tp->tc_flags&TC_SYN_ACKED) == 0)
6714601Swnj 		return;
672*4656Swnj 	up = tp->t_socket;
6734601Swnj 	mp = &up->uc_rbuf;
6744601Swnj 	while (*mp)
6754601Swnj 		mp = &(*mp)->m_next;
676*4656Swnj 	t = tp->t_rcv_next;
677*4656Swnj 	/* SHOULD PACK DATA IN HERE */
678*4656Swnj 	while (t != (struct th *)tp && t->t_seq < tp->rcv_nxt) {
679*4656Swnj 		remque(t);
6804601Swnj 		m = dtom(t);
681*4656Swnj 		up->uc_rcc += t->t_len;
682*4656Swnj 		tp->seqcnt -= t->t_len;
683*4656Swnj 		if (tp->seqcnt < 0) panic("present_data");
6844601Swnj 		t = t->t_next;
6854601Swnj 		while (m) {
6864601Swnj 			if (m->m_len == 0) {
6874601Swnj 				m = m_free(m);
6884601Swnj 				continue;
6894601Swnj 			}
690*4656Swnj 			*mp = m;
6914601Swnj 			mp = &m->m_next;
6924601Swnj 			m = *mp;
6934601Swnj 		}
6944601Swnj 	}
695*4656Swnj 	if (up->uc_rcc != 0)
6964601Swnj 		netwakeup(up);
697*4656Swnj 	if ((tp->tc_flags&TC_FIN_RCVD) &&			/* ### */
698*4656Swnj 	    (tp->tc_flags&TC_USR_CLOSED) == 0 &&		/* ### */
699*4656Swnj 	    rcv_empty(tp))					/* ### */
700*4656Swnj 		to_user(up, UCLOSED);				/* ### */
7014601Swnj }
702