xref: /csrg-svn/sys/netinet/tcp_input.c (revision 4673)
1*4673Swnj /* tcp_input.c 1.9 81/10/30 */
24601Swnj 
34601Swnj #include "../h/param.h"
44601Swnj #include "../h/systm.h"
54663Swnj #include "../h/mbuf.h"
64663Swnj #include "../h/socket.h"
74663Swnj #include "../inet/inet.h"
84663Swnj #include "../inet/inet_systm.h"
94663Swnj #include "../inet/imp.h"
104663Swnj #include "../inet/inet_host.h"
114663Swnj #include "../inet/ip.h"
124663Swnj #include "../inet/tcp.h"
134663Swnj #include "../inet/tcp_fsm.h"
144601Swnj 
154601Swnj extern int nosum;
164601Swnj 
174601Swnj tcp_input(mp)
184601Swnj 	register struct mbuf *mp;
194601Swnj {
20*4673Swnj 	register struct th *n;		/* known to be r10 */
21*4673Swnj 	register int j;			/* known to be r9 */
224601Swnj 	register struct tcb *tp;
234601Swnj 	int nstate;
244601Swnj 	struct mbuf *m;
254601Swnj 	struct ucb *up;
26*4673Swnj 	int hlen, tlen;
274601Swnj 	u_short lport, fport;
284601Swnj #ifdef TCPDEBUG
294601Swnj 	struct tcp_debug tdb;
304601Swnj #endif
314601Swnj COUNT(TCP_INPUT);
324601Swnj 
334601Swnj 	/*
344601Swnj 	 * Build extended tcp header
354601Swnj 	 */
364601Swnj 	n = (struct th *)((int)mp + mp->m_off);
374601Swnj 	tlen = ((struct ip *)n)->ip_len;
384601Swnj 	n->t_len = htons(tlen);
394601Swnj 	n->t_next = NULL;
404601Swnj 	n->t_prev = NULL;
414601Swnj 	n->t_x1 = 0;
424601Swnj 	lport = ntohs(n->t_dst);
434601Swnj 	fport = ntohs(n->t_src);
444601Swnj 
454601Swnj 	/* WONT BE POSSIBLE WHEN MBUFS ARE 256 BYTES */
464601Swnj 	if ((hlen = n->t_off << 2) > mp->m_len)
474601Swnj 		{ printf("tcp header overflow\n"); m_freem(mp); return; }
484601Swnj 
494601Swnj 	/*
504601Swnj 	 * Checksum extended header and data
514601Swnj 	 */
524601Swnj 	j = n->t_sum; n->t_sum = 0;
53*4673Swnj #ifdef vax
54*4673Swnj 	if (tlen == 20) {
55*4673Swnj 		asm("addl3 $8,r10,r0; movl (r0)+,r1; addl2 (r0)+,r1");
56*4673Swnj 		asm("adwc (r0)+,r1; adwc (r0)+,r1; adwc (r0)+,r1");
57*4673Swnj 		asm("adwc (r0)+,r1; adwc (r0)+,r1; adwc (r0)+,r1");
58*4673Swnj 		asm("adwc $0,r1; ashl $-16,r1,r0; addw2 r0,r1");
59*4673Swnj 		asm("adwc $0,r1");		/* ### */
60*4673Swnj 		asm("mcoml r1,r1; movzwl r1,r1; subl2 r1,r9");
61*4673Swnj 	} else
62*4673Swnj #endif
63*4673Swnj 		j -= cksum(mp, sizeof (struct ip) + tlen);
64*4673Swnj 	if (j != 0) {
654601Swnj 		netstat.t_badsum++;
664601Swnj 		if (nosum == 0) {
674601Swnj 			m_freem(mp);
684601Swnj 			return;
694601Swnj 		}
704601Swnj 	}
714601Swnj 
724601Swnj 	/*
734601Swnj 	 * Find tcb for message (SHOULDN'T USE LINEAR SEARCH!)
744601Swnj 	 */
754663Swnj 	for (tp = tcb_head; tp != 0; tp = tp->t_tcb_next)
764601Swnj 		if (tp->t_lport == lport && tp->t_fport == fport &&
774601Swnj 		    tp->t_ucb->uc_host->h_addr.s_addr == n->t_s.s_addr)
784601Swnj 			goto found;
794663Swnj 	for (tp = tcb_head; tp != 0; tp = tp->t_tcb_next)
804601Swnj 		if (tp->t_lport == lport &&
814601Swnj 		    (tp->t_fport==fport || tp->t_fport==0) &&
824601Swnj 		    (tp->t_ucb->uc_host->h_addr.s_addr == n->t_s.s_addr ||
834601Swnj 		     tp->t_ucb->uc_host->h_addr.s_addr == 0))
844601Swnj 			goto found;
854601Swnj 	goto notwanted;
864601Swnj found:
874601Swnj 
884601Swnj 	/*
894601Swnj 	 * Byte swap header
904601Swnj 	 */
914601Swnj 	n->t_len = tlen - hlen;
924601Swnj 	n->t_src = fport;
934601Swnj 	n->t_dst = lport;
944601Swnj 	n->t_seq = ntohl(n->t_seq);
954601Swnj 	n->t_ackno = ntohl(n->t_ackno);
964601Swnj 	n->t_win = ntohs(n->t_win);
974601Swnj 	n->t_urp = ntohs(n->t_urp);
984601Swnj 
994601Swnj 	/*
1004601Swnj 	 * Check segment seq # and do rst processing
1014601Swnj 	 */
1024601Swnj 	switch (tp->t_state) {
1034601Swnj 
1044601Swnj 	case LISTEN:
1054601Swnj 		if ((n->th_flags&TH_ACK) || !syn_ok(tp, n)) {
1064601Swnj 			send_rst(tp, n);
1074601Swnj 			goto badseg;
1084601Swnj 		}
1094601Swnj 		if (n->th_flags&TH_RST)
1104601Swnj 			goto badseg;
1114601Swnj 		goto goodseg;
1124601Swnj 
1134601Swnj 	case SYN_SENT:
1144601Swnj 		if (!ack_ok(tp, n) || !syn_ok(tp, n)) {
1154601Swnj 			send_rst(tp, n);			/* 71,72,75 */
1164601Swnj 			goto badseg;
1174601Swnj 		}
1184601Swnj 		if (n->th_flags&TH_RST) {
1194601Swnj 			t_close(tp, URESET);			/* 70 */
1204601Swnj 			tp->t_state = CLOSED;
1214601Swnj 			goto badseg;
1224601Swnj 		}
1234601Swnj 		goto goodseg;
1244601Swnj 
1254601Swnj 	default:
1264601Swnj         	if ((n->th_flags&TH_RST) == 0)
1274601Swnj 			goto common;
1284601Swnj 		if (n->t_seq < tp->rcv_nxt)		/* bad rst */
1294601Swnj 			goto badseg;				/* 69 */
1304601Swnj 		switch (tp->t_state) {
1314601Swnj 
1324601Swnj 		case L_SYN_RCVD:
1334601Swnj 			if (ack_ok(tp, n) == 0)
1344601Swnj 				goto badseg;			/* 69 */
1354601Swnj 			tp->t_rexmt = 0;
1364601Swnj 			tp->t_rexmttl = 0;
1374601Swnj 			tp->t_persist = 0;
1384601Swnj 			h_free(tp->t_ucb->uc_host);
1394601Swnj 			tp->t_state = LISTEN;
1404601Swnj 			goto badseg;
1414601Swnj 
1424601Swnj 		default:
1434601Swnj 			t_close(tp, URESET);			/* 66 */
1444601Swnj 			tp->t_state = CLOSED;
1454601Swnj 			goto badseg;
1464601Swnj 		}
1474601Swnj 		/*NOTREACHED*/
1484601Swnj 
1494601Swnj 	case SYN_RCVD:
1504601Swnj common:
1514601Swnj 		if (ack_ok(tp, n) == 0) {
1524601Swnj 			send_rst(tp, n);			/* 74 */
1534601Swnj 			goto badseg;
1544601Swnj 		}
1554601Swnj 		if (syn_ok(tp, n) && n->t_seq != tp->irs) {
1564601Swnj 			send_null(tp);				/* 74 */
1574601Swnj 			goto badseg;
1584601Swnj 		}
1594601Swnj 		goto goodseg;
1604601Swnj 	}
1614601Swnj badseg:
1624601Swnj 	m_freem(mp);
1634601Swnj 	return;
1644601Swnj 
1654601Swnj goodseg:
1664601Swnj #ifdef notdef
1674601Swnj 	/*
1684601Swnj 	 * Defer processing if no buffer space for this connection.
1694601Swnj 	 */
1704601Swnj 	up = tp->t_ucb;
1714656Swnj 	if (up->uc_rcc > up->uc_rhiwat &&
1724663Swnj 	     && n->t_len != 0 && mbstat.m_bufs < mbstat.m_lowat) {
1734601Swnj 		mp->m_act = (struct mbuf *)0;
1744601Swnj 		if ((m = tp->t_rcv_unack) != NULL) {
1754601Swnj 			while (m->m_act != NULL)
1764601Swnj 				m = m->m_act;
1774601Swnj 			m->m_act = mp;
1784601Swnj 		} else
1794601Swnj 			tp->t_rcv_unack = mp;
1804601Swnj 		return;
1814601Swnj 	}
1824601Swnj #endif
1834601Swnj 
1844601Swnj 	/*
1854601Swnj 	 * Discard ip header, and do tcp input processing.
1864601Swnj 	 */
1874601Swnj 	hlen += sizeof(struct ip);
1884601Swnj 	mp->m_off += hlen;
1894601Swnj 	mp->m_len -= hlen;
1904601Swnj 	nstate = tp->t_state;
1914601Swnj 	tp->tc_flags &= ~TC_NET_KEEP;
1924601Swnj 	acounts[tp->t_state][INRECV]++;
1934601Swnj #ifdef TCPDEBUG
1944601Swnj 	if ((tp->t_ucb->uc_flags & UDEBUG) || tcpconsdebug) {
1954604Swnj 		tdb_setup(tp, n, INRECV, &tdb);
1964601Swnj 	} else
1974601Swnj 		tdb.td_tod = 0;
1984601Swnj #endif
1994601Swnj 	switch (tp->t_state) {
2004601Swnj 
2014601Swnj 	case LISTEN:
2024601Swnj 		if (!syn_ok(tp, n) ||
2034601Swnj 		    ((tp->t_ucb->uc_host = h_make(&n->t_s)) == 0)) {
2044601Swnj 			nstate = EFAILEC;
2054601Swnj 			goto done;
2064601Swnj 		}
2074601Swnj 		tp->t_fport = n->t_src;
2084603Sroot 		tp->t_ucb->uc_template = tcp_template(tp);
2094601Swnj 		rcv_ctldat(tp, n, 1);
2104601Swnj 		if (tp->tc_flags&TC_FIN_RCVD) {
2114601Swnj 			tp->t_finack = T_2ML;			/* 3 */
2124601Swnj 			tp->tc_flags &= ~TC_WAITED_2_ML;
2134601Swnj 			nstate = CLOSE_WAIT;
2144601Swnj 		} else {
2154601Swnj 			tp->t_init = T_INIT / 2;		/* 4 */
2164601Swnj 			nstate = L_SYN_RCVD;
2174601Swnj 		}
2184601Swnj 		goto done;
2194601Swnj 
2204601Swnj 	case SYN_SENT:
2214601Swnj 		if (!syn_ok(tp, n)) {
2224601Swnj 			nstate = EFAILEC;
2234601Swnj 			goto done;
2244601Swnj 		}
2254601Swnj 		rcv_ctldat(tp, n, 1);
2264601Swnj 		if (tp->tc_flags&TC_FIN_RCVD) {
2274601Swnj 			if (n->th_flags&TH_ACK) {
2284601Swnj 				if (n->t_ackno > tp->iss)
2294601Swnj 					present_data(tp);	/* 32 */
2304601Swnj 			} else {
2314601Swnj 				tp->t_finack = T_2ML;		/* 9 */
2324601Swnj 				tp->tc_flags &= ~TC_WAITED_2_ML;
2334601Swnj 			}
2344601Swnj 			nstate = CLOSE_WAIT;
2354601Swnj 			goto done;
2364601Swnj 		}
2374601Swnj 		if (n->th_flags&TH_ACK) {
2384601Swnj 			present_data(tp);			/* 11 */
2394601Swnj 			nstate = ESTAB;
2404601Swnj 		} else
2414601Swnj 			nstate = SYN_RCVD;			/* 8 */
2424601Swnj 		goto done;
2434601Swnj 
2444601Swnj 	case SYN_RCVD:
2454601Swnj 	case L_SYN_RCVD:
2464601Swnj 		if ((n->th_flags&TH_ACK) == 0 ||
2474601Swnj 		    (n->th_flags&TH_ACK) && n->t_ackno <= tp->iss) {
2484601Swnj 			nstate = EFAILEC;
2494601Swnj 			goto done;
2504601Swnj 		}
2514601Swnj 		goto input;
2524601Swnj 
2534601Swnj 	case ESTAB:
2544601Swnj 	case FIN_W1:
2554601Swnj 	case FIN_W2:
2564601Swnj 	case TIME_WAIT:
2574601Swnj input:
2584601Swnj 		rcv_ctldat(tp, n, 1);				/* 39 */
2594601Swnj 		present_data(tp);
2604601Swnj 		switch (tp->t_state) {
2614601Swnj 
2624601Swnj 		case ESTAB:
2634601Swnj 			if (tp->tc_flags&TC_FIN_RCVD)
2644601Swnj 				nstate = CLOSE_WAIT;
2654601Swnj 			break;
2664601Swnj 
2674601Swnj 		case SYN_RCVD:
2684601Swnj 		case L_SYN_RCVD:
2694601Swnj 			nstate = (tp->tc_flags&TC_FIN_RCVD) ?
2704601Swnj 			    CLOSE_WAIT : ESTAB;			 /* 33:5 */
2714601Swnj 			break;
2724601Swnj 
2734601Swnj 		case FIN_W1:
2744601Swnj 			j = ack_fin(tp, n);
2754601Swnj 			if ((tp->tc_flags & TC_FIN_RCVD) == 0) {
2764601Swnj 				if (j)
2774601Swnj 					nstate = FIN_W2;	/* 27 */
2784601Swnj 				break;
2794601Swnj 			}
2804601Swnj 			tp->t_finack = T_2ML;
2814601Swnj 			tp->tc_flags &= ~TC_WAITED_2_ML;
2824601Swnj 			nstate = j ? TIME_WAIT : CLOSING1;	/* 28:26 */
2834601Swnj 			break;
2844601Swnj 
2854601Swnj 		case FIN_W2:
2864601Swnj 			if (tp->tc_flags&TC_FIN_RCVD) {
2874601Swnj 				tp->t_finack = T_2ML;		/* 29 */
2884601Swnj 				tp->tc_flags &= ~TC_WAITED_2_ML;
2894601Swnj 				nstate = TIME_WAIT;
2904601Swnj 				break;
2914601Swnj 			}
2924601Swnj 			break;
2934601Swnj 		}
2944601Swnj 		goto done;
2954601Swnj 
2964601Swnj 	case CLOSE_WAIT:
2974601Swnj 		if (n->th_flags&TH_FIN) {
2984601Swnj 			if ((n->th_flags&TH_ACK) &&
2994601Swnj 			    n->t_ackno <= tp->seq_fin) {
3004601Swnj 				rcv_ctldat(tp, n, 0);		/* 30 */
3014601Swnj 				tp->t_finack = T_2ML;
3024601Swnj 				tp->tc_flags &= ~TC_WAITED_2_ML;
3034601Swnj 			} else
3044601Swnj 				send_ctl(tp);			/* 31 */
3054601Swnj 			goto done;
3064601Swnj 		}
3074601Swnj 		goto input;
3084601Swnj 
3094601Swnj 	case CLOSING1:
3104601Swnj 		j = ack_fin(tp, n);
3114601Swnj 		if (n->th_flags&TH_FIN) {
3124601Swnj 			rcv_ctldat(tp, n, 0);
3134601Swnj 			tp->t_finack = T_2ML;
3144601Swnj 			tp->tc_flags &= ~TC_WAITED_2_ML;
3154601Swnj 			if (j)
3164601Swnj 				nstate = TIME_WAIT;		/* 23 */
3174601Swnj 			goto done;
3184601Swnj 		}
3194601Swnj 		if (j) {
3204601Swnj 			if (tp->tc_flags&TC_WAITED_2_ML)
3214601Swnj 				if (rcv_empty(tp)) {
3224601Swnj 					t_close(tp, UCLOSED);	/* 15 */
3234601Swnj 					nstate = CLOSED;
3244601Swnj 				} else
3254601Swnj 					nstate = RCV_WAIT;	/* 18 */
3264601Swnj 			else
3274601Swnj 				nstate = TIME_WAIT;
3284601Swnj 			goto done;
3294601Swnj 		}
3304601Swnj 		goto input;
3314601Swnj 
3324601Swnj 	case CLOSING2:
3334601Swnj 		if (ack_fin(tp, n)) {
3344601Swnj 			if (rcv_empty(tp)) {			/* 16 */
3354601Swnj 				t_close(tp, UCLOSED);
3364601Swnj 				nstate = CLOSED;
3374601Swnj 			} else
3384601Swnj 				nstate = RCV_WAIT;		/* 19 */
3394601Swnj 			goto done;
3404601Swnj 		}
3414601Swnj 		if (n->th_flags&TH_FIN) {
3424601Swnj 			send_ctl(tp);				/* 31 */
3434601Swnj 			goto done;
3444601Swnj 		}
3454601Swnj 		goto input;
3464601Swnj 
3474601Swnj 	case RCV_WAIT:
3484601Swnj 		if ((n->th_flags&TH_FIN) && (n->th_flags&TH_ACK) &&
3494601Swnj 		    n->t_ackno <= tp->seq_fin) {
3504601Swnj 			rcv_ctldat(tp, n, 0);
3514601Swnj 			tp->t_finack = T_2ML;
3524601Swnj 			tp->tc_flags &= ~TC_WAITED_2_ML;	/* 30 */
3534601Swnj 		}
3544601Swnj 		goto done;
3554601Swnj 	}
3564601Swnj 	panic("tcp_input");
3574601Swnj done:
3584601Swnj 
3594601Swnj 	/*
3604601Swnj 	 * Done with state*input specific processing.
3614601Swnj 	 * Form trace records, free input if not needed,
3624601Swnj 	 * and enter new state.
3634601Swnj 	 */
3644601Swnj #ifdef TCPDEBUG
3654604Swnj 	if (tdb.td_tod)
3664604Swnj 		tdb_stuff(&tdb, nstate);
3674601Swnj #endif
3684601Swnj 	switch (nstate) {
3694601Swnj 
3704601Swnj 	case EFAILEC:
3714601Swnj 		m_freem(mp);
3724601Swnj 		return;
3734601Swnj 
3744601Swnj 	default:
3754601Swnj 		tp->t_state = nstate;
3764601Swnj 		/* fall into ... */
3774601Swnj 
3784601Swnj 	case CLOSED:
3794601Swnj 		/* IF CLOSED CANT LOOK AT tc_flags */
3804601Swnj 		if ((tp->tc_flags&TC_NET_KEEP) == 0)
3814601Swnj 			m_freem(mp);
3824601Swnj 		return;
3834601Swnj 	}
3844601Swnj 	/* NOTREACHED */
3854601Swnj 
3864601Swnj 	/*
3874601Swnj 	 * Unwanted packed; free everything
3884601Swnj 	 * but the header and return an rst.
3894601Swnj 	 */
3904601Swnj notwanted:
3914601Swnj 	m_freem(mp->m_next);
3924601Swnj 	mp->m_next = NULL;
3934601Swnj 	mp->m_len = sizeof(struct th);
3944601Swnj #define xchg(a,b) j=a; a=b; b=j
3954601Swnj 	xchg(n->t_d.s_addr, n->t_s.s_addr); xchg(n->t_dst, n->t_src);
3964601Swnj #undef xchg
3974601Swnj 	if (n->th_flags&TH_ACK)
3984601Swnj 		n->t_seq = n->t_ackno;
3994601Swnj 	else {
4004601Swnj 		n->t_ackno = htonl(ntohl(n->t_seq) + tlen - hlen);
4014601Swnj 		n->t_seq = 0;
4024601Swnj 	}
4034601Swnj 	n->th_flags = TH_RST; /* not TH_FIN, TH_SYN */
4044601Swnj 	n->th_flags ^= TH_ACK;
4054601Swnj 	n->t_len = htons(TCPSIZE);
4064601Swnj 	n->t_off = 5;
4074601Swnj 	n->t_sum = cksum(mp, sizeof(struct th));
4084601Swnj 	((struct ip *)n)->ip_len = sizeof(struct th);
4094601Swnj 	ip_output(mp);
4104601Swnj 	netstat.t_badsegs++;
4114601Swnj }
4124601Swnj 
4134601Swnj rcv_ctldat(tp, n, dataok)
4144601Swnj 	register struct tcb *tp;
4154601Swnj 	register struct th *n;
4164601Swnj {
4174601Swnj 	register sent;
4184601Swnj 	register struct ucb *up;
4194601Swnj 	register struct mbuf *m, *mn;
4204601Swnj 	register len;
4214601Swnj COUNT(RCV_CTLDAT);
4224601Swnj 
4234601Swnj 	tp->tc_flags &= ~(TC_DROPPED_TXT|TC_ACK_DUE|TC_NEW_WINDOW);
4244601Swnj /* syn */
4254601Swnj 	if ((tp->tc_flags&TC_SYN_RCVD) == 0 && (n->th_flags&TH_SYN)) {
4264601Swnj 		tp->irs = n->t_seq;
4274601Swnj 		tp->rcv_nxt = n->t_seq + 1;
4284601Swnj 		tp->snd_wl = tp->rcv_urp = tp->irs;
4294601Swnj 		tp->tc_flags |= (TC_SYN_RCVD|TC_ACK_DUE);
4304601Swnj 	}
4314601Swnj /* ack */
4324601Swnj 	if ((n->th_flags&TH_ACK) && (tp->tc_flags&TC_SYN_RCVD) &&
4334601Swnj 	    n->t_ackno > tp->snd_una) {
4344601Swnj 		up = tp->t_ucb;
4354601Swnj 
4364601Swnj 		/* update snd_una and snd_nxt */
4374601Swnj 		tp->snd_una = n->t_ackno;
4384601Swnj 		if (tp->snd_una > tp->snd_nxt)
4394601Swnj 			tp->snd_nxt = tp->snd_una;
4404601Swnj 
4414601Swnj 		/* if timed msg acked, set retrans time value */
4424601Swnj 		if ((tp->tc_flags&TC_SYN_ACKED) &&
4434601Swnj 		    tp->snd_una > tp->t_xmt_val) {
4444601Swnj 			tp->t_xmtime = (tp->t_xmt != 0 ? tp->t_xmt : T_REXMT);
4454601Swnj 			if (tp->t_xmtime > T_REMAX)
4464601Swnj 				tp->t_xmtime = T_REMAX;
4474601Swnj 		}
4484601Swnj 
4494601Swnj 		/* remove acked data from send buf */
4504601Swnj 		len = tp->snd_una - tp->snd_off;
4514601Swnj 		m = up->uc_sbuf;
4524601Swnj 		while (len > 0 && m != NULL)
4534601Swnj 			if (m->m_len <= len) {
4544601Swnj 				len -= m->m_len;
4554601Swnj 				if (m->m_off > MMAXOFF)
4564601Swnj 					up->uc_ssize -= NMBPG;
4574601Swnj 				MFREE(m, mn);
4584601Swnj 				m = mn;
4594601Swnj 				up->uc_ssize--;
4604601Swnj 			} else {
4614601Swnj 				m->m_len -= len;
4624601Swnj 				m->m_off += len;
4634601Swnj 				break;
4644601Swnj 			}
4654601Swnj 		up->uc_sbuf = m;
4664601Swnj 		tp->snd_off = tp->snd_una;
4674601Swnj 		if ((tp->tc_flags&TC_SYN_ACKED) == 0 &&
4684601Swnj 		    (tp->snd_una > tp->iss)) {
4694601Swnj 			tp->tc_flags |= TC_SYN_ACKED;
4704601Swnj 			tp->t_init = 0;
4714601Swnj 		}
4724601Swnj 		if (tp->seq_fin != tp->iss && tp->snd_una > tp->seq_fin)
4734601Swnj 			tp->tc_flags &= ~TC_SND_FIN;
4744601Swnj 		tp->t_rexmt = 0;
4754601Swnj 		tp->t_rexmttl = 0;
4764601Swnj 		tp->tc_flags |= TC_CANCELLED;
4774601Swnj 		netwakeup(tp->t_ucb);		/* wasteful */
4784601Swnj 	}
4794601Swnj /* win */
4804601Swnj 	if ((tp->tc_flags & TC_SYN_RCVD) && n->t_seq >= tp->snd_wl) {
4814601Swnj 		tp->snd_wl = n->t_seq;
4824601Swnj 		tp->snd_wnd = n->t_win;
4834601Swnj 		tp->tc_flags |= TC_NEW_WINDOW;
4844601Swnj 		tp->t_persist = 0;
4854601Swnj 	}
4864601Swnj 	if (dataok) {
4874601Swnj /* text */
4884601Swnj 		if (n->t_len != 0)
4894601Swnj 			rcv_text(tp, n);
4904601Swnj /* urg */
4914601Swnj 		if (n->th_flags&TH_URG) {
4924601Swnj 			unsigned urgent;
4934601Swnj 
4944601Swnj 			urgent = n->t_urp + n->t_seq;
4954601Swnj 			if (tp->rcv_nxt < urgent) {
4964601Swnj 				if (tp->rcv_urp <= tp->rcv_nxt)
4974601Swnj 					to_user(tp->t_ucb, UURGENT);
4984601Swnj 				tp->rcv_urp = urgent;
4994601Swnj 			}
5004601Swnj 		}
5014601Swnj /* eol */
5024601Swnj 		if ((n->th_flags&TH_EOL) &&
5034601Swnj 		    (tp->tc_flags&TC_DROPPED_TXT) == 0 &&
5044601Swnj 		    tp->t_rcv_prev != (struct th *)tp) {
5054601Swnj 			/* mark last mbuf */
5064601Swnj 			m = dtom(tp->t_rcv_prev);
5074601Swnj 			if (m != NULL) {
5084601Swnj 				while (m->m_next != NULL)
5094601Swnj 					m = m->m_next;
5104601Swnj 				m->m_act =
5114601Swnj 				    (struct mbuf *)(m->m_off + m->m_len - 1);
5124601Swnj 			}
5134601Swnj 		}
5144601Swnj 	}
5154601Swnj /* fin */
5164601Swnj 	if ((n->th_flags&TH_FIN) && (tp->tc_flags&TC_DROPPED_TXT) == 0) {
5174601Swnj 		int last;
5184601Swnj 
5194601Swnj 		if ((tp->tc_flags&TC_FIN_RCVD) == 0) {
5204601Swnj 			/* do we really have fin ? */
5214601Swnj 			last = firstempty(tp);
5224601Swnj 			if (tp->t_rcv_prev == (struct th *)tp ||
5234601Swnj 			    last == t_end(tp->t_rcv_prev)) {
5244601Swnj 				tp->tc_flags |= TC_FIN_RCVD;
5254601Swnj 				netwakeup(tp->t_ucb);		/* poke */
5264601Swnj 			}
5274601Swnj 			if ((tp->tc_flags&TC_FIN_RCVD) &&
5284601Swnj 			    tp->rcv_nxt >= last) {
5294601Swnj 				tp->rcv_nxt = last + 1;		/* fin seq */
5304601Swnj 				tp->tc_flags |= TC_ACK_DUE;
5314601Swnj 			}
5324601Swnj 		} else
5334601Swnj 			tp->tc_flags |= TC_ACK_DUE;
5344601Swnj 	}
5354601Swnj 
5364601Swnj /* respond */
5374671Swnj 	sent = 0;
5384601Swnj 	if (tp->tc_flags&TC_ACK_DUE)
5394601Swnj 		sent = send_ctl(tp);
5404671Swnj 	else if (tp->tc_flags&TC_NEW_WINDOW) {
5414671Swnj 		seq_t last = tp->snd_off;
5424671Swnj 		up = tp->t_ucb;
5434671Swnj 		for (m = up->uc_sbuf; m != NULL; m = m->m_next)
5444671Swnj 			last += m->m_len;
5454671Swnj 		if (tp->snd_nxt <= last || (tp->tc_flags&TC_SND_FIN))
5464671Swnj 			sent = send(tp);
5474671Swnj 	}
5484601Swnj 
5494601Swnj /* set for retrans */
5504601Swnj 	if (!sent && tp->snd_una < tp->snd_nxt &&
5514601Swnj 	    (tp->tc_flags&TC_CANCELLED)) {
5524601Swnj 		tp->t_rexmt = tp->t_xmtime;
5534601Swnj 		tp->t_rexmttl = T_REXMTTL;
5544601Swnj 		tp->t_rexmt_val = tp->t_rtl_val = tp->snd_lst;
5554601Swnj 		tp->tc_flags &= ~TC_CANCELLED;
5564601Swnj 	}
5574601Swnj }
5584601Swnj 
5594645Swnj rcv_text(tp, n)
5604601Swnj 	register struct tcb *tp;
5614645Swnj 	register struct th *n;
5624601Swnj {
5634645Swnj 	register int i;
5644601Swnj 	register struct th *p, *q;
5654645Swnj 	register struct mbuf *m;
5664645Swnj 	int overage;
5674601Swnj COUNT(RCV_TEXT);
5684601Swnj 
5694645Swnj 	/*
5704645Swnj 	 * Discard duplicate data already passed to user.
5714645Swnj 	 */
5724648Swnj 	if (SEQ_LT(n->t_seq, tp->rcv_nxt)) {
5734645Swnj 		i = tp->rcv_nxt - n->t_seq;
5744645Swnj 		if (i >= n->t_len)
5754645Swnj 			goto dropseg;
5764645Swnj 		n->t_seq += i;
5774645Swnj 		n->t_len -= i;
5784645Swnj 		m_adj(dtom(n), i);
5794601Swnj 	}
5804601Swnj 
5814645Swnj 	/*
5824645Swnj 	 * Find a segment which begins after this one does.
5834645Swnj 	 */
5844645Swnj 	for (q = tp->t_rcv_next; q != (struct th *)tp; q = q->t_next)
5854648Swnj 		if (SEQ_GT(q->t_seq, n->t_seq))
5864645Swnj 			break;
5874601Swnj 
5884645Swnj 	/*
5894645Swnj 	 * If there is a preceding segment, it may provide some of
5904645Swnj 	 * our data already.  If so, drop the data from the incoming
5914645Swnj 	 * segment.  If it provides all of our data, drop us.
5924645Swnj 	 */
5934645Swnj 	if (q->t_prev != (struct th *)tp) {
5944648Swnj 		/* conversion to int (in i) handles seq wraparound */
5954645Swnj 		i = q->t_prev->t_seq + q->t_prev->t_len - n->t_seq;
5964645Swnj 		if (i > 0) {
5974645Swnj 			if (i >= n->t_len)
5984645Swnj 				goto dropseg;
5994645Swnj 			m_adj(dtom(tp), i);
6004645Swnj 			n->t_len -= i;
6014645Swnj 			n->t_seq += i;
6024601Swnj 		}
6034601Swnj 	}
6044601Swnj 
6054645Swnj 	/*
6064645Swnj 	 * While we overlap succeeding segments trim them or,
6074645Swnj 	 * if they are completely covered, dequeue them.
6084645Swnj 	 */
6094648Swnj 	while (q != (struct th *)tp && SEQ_GT(n->t_seq + n->t_len, q->t_seq)) {
6104645Swnj 		i = (n->t_seq + n->t_len) - q->t_seq;
6114645Swnj 		if (i < q->t_len) {
6124645Swnj 			q->t_len -= i;
6134645Swnj 			m_adj(dtom(q), i);
6144645Swnj 			break;
6154601Swnj 		}
6164645Swnj 		q = q->t_next;
6174645Swnj 		m_freem(dtom(q->t_prev));
6184645Swnj 		remque(q->t_prev);
6194645Swnj 	}
6204601Swnj 
6214645Swnj 	/*
6224645Swnj 	 * Stick new segment in its place.
6234645Swnj 	 */
6244645Swnj 	insque(n, q->t_prev);
6254656Swnj 	tp->seqcnt += n->t_len;
6264601Swnj 
6274601Swnj #ifdef notdef
6284645Swnj 	/*
6294645Swnj 	 * Calculate available space and discard segments for
6304645Swnj 	 * which there is too much.
6314645Swnj 	 */
6324645Swnj 	q = tp->t_rcv_prev;
6334648Swnj 	overage =
6344648Swnj 	    (tp->t_socket->uc_rcc + tp->rcv_seqcnt) - tp->t_socket->uc_rhiwat;
6354645Swnj 	if (overage > 0)
6364645Swnj 		for (;;) {
6374645Swnj 			i = MIN(q->t_len, overage);
6384645Swnj 			overage -= i;
6394645Swnj 			q->t_len -= i;
6404645Swnj 			m_adj(q, -i);
6414645Swnj 			if (q == n)
6424645Swnj 				tp->tc_flags |= TC_DROPPED_TXT;
6434645Swnj 			if (q->t_len)
6444645Swnj 				break;
6454645Swnj 			if (q == n)
6464648Swnj 				panic("tcp_text dropall");
6474645Swnj 			q = q->t_prev;
6484645Swnj 			remque(q->t_next);
6494645Swnj 		}
6504645Swnj #endif
6514601Swnj 
6524645Swnj 	/*
6534648Swnj 	 * Advance rcv_next through
6544648Swnj 	 * newly completed sequence space
6554648Swnj 	 * and return forcing an ack.
6564645Swnj 	 */
6574645Swnj 	while (n->t_seq == tp->rcv_nxt) {
6584648Swnj 		/* present data belongs here */
6594645Swnj 		tp->rcv_nxt += n->t_len;
6604645Swnj 		n = n->t_next;
6614645Swnj 		if (n == (struct th *)tp)
6624645Swnj 			break;
6634645Swnj 	}
6644645Swnj 	tp->tc_flags |= (TC_ACK_DUE|TC_NET_KEEP);
6654645Swnj 	return;
6664601Swnj 
6674645Swnj dropseg:
6684648Swnj 	/* don't set TC_NET_KEEP, so that mbuf's will get dropped */
6694645Swnj 	return;
6704601Swnj }
6714601Swnj 
6724656Swnj #define	socket		ucb			/* ### */
6734656Swnj #define	t_socket	t_ucb			/* ### */
6744656Swnj 
6754601Swnj present_data(tp)
6764601Swnj 	register struct tcb *tp;
6774601Swnj {
6784601Swnj 	register struct th *t;
6794656Swnj 	register struct socket *up;
6804601Swnj 	register struct mbuf *m, **mp;
6814601Swnj 	seq_t ready;
6824601Swnj COUNT(PRESENT_DATA);
6834601Swnj 
6844601Swnj 	/* connection must be synced and data available for user */
6854656Swnj 	if ((tp->tc_flags&TC_SYN_ACKED) == 0)
6864601Swnj 		return;
6874656Swnj 	up = tp->t_socket;
6884601Swnj 	mp = &up->uc_rbuf;
6894601Swnj 	while (*mp)
6904601Swnj 		mp = &(*mp)->m_next;
6914656Swnj 	t = tp->t_rcv_next;
6924656Swnj 	/* SHOULD PACK DATA IN HERE */
6934656Swnj 	while (t != (struct th *)tp && t->t_seq < tp->rcv_nxt) {
6944656Swnj 		remque(t);
6954601Swnj 		m = dtom(t);
6964656Swnj 		up->uc_rcc += t->t_len;
6974656Swnj 		tp->seqcnt -= t->t_len;
6984656Swnj 		if (tp->seqcnt < 0) panic("present_data");
6994601Swnj 		t = t->t_next;
7004601Swnj 		while (m) {
7014601Swnj 			if (m->m_len == 0) {
7024601Swnj 				m = m_free(m);
7034601Swnj 				continue;
7044601Swnj 			}
7054656Swnj 			*mp = m;
7064601Swnj 			mp = &m->m_next;
7074601Swnj 			m = *mp;
7084601Swnj 		}
7094601Swnj 	}
7104656Swnj 	if (up->uc_rcc != 0)
7114601Swnj 		netwakeup(up);
7124656Swnj 	if ((tp->tc_flags&TC_FIN_RCVD) &&			/* ### */
7134656Swnj 	    (tp->tc_flags&TC_USR_CLOSED) == 0 &&		/* ### */
7144656Swnj 	    rcv_empty(tp))					/* ### */
7154656Swnj 		to_user(up, UCLOSED);				/* ### */
7164601Swnj }
717