xref: /csrg-svn/sys/netinet/tcp_usrreq.c (revision 4670)
1*4670Swnj /* tcp_usrreq.c 1.15 81/10/30 */
24567Swnj 
34497Swnj #include "../h/param.h"
44567Swnj #include "../h/systm.h"
54664Swnj #include "../h/mbuf.h"
64664Swnj #include "../h/socket.h"
74664Swnj #include "../inet/inet.h"
84664Swnj #include "../inet/inet_systm.h"
94664Swnj #include "../inet/imp.h"
104664Swnj #include "../inet/ip.h"
114664Swnj #include "../inet/tcp.h"
124567Swnj #define TCPFSTAB
134584Swnj #ifdef TCPDEBUG
144584Swnj #define TCPSTATES
154584Swnj #endif
164664Swnj #include "../inet/tcp_fsm.h"
174497Swnj 
184567Swnj tcp_timeo()
194497Swnj {
204567Swnj 	register struct tcb *tp;
214567Swnj 	int s = splnet();
224567Swnj COUNT(TCP_TIMEO);
234497Swnj 
244567Swnj 	/*
254567Swnj 	 * Search through tcb's and update active timers.
264567Swnj 	 */
274664Swnj 	for (tp = tcb_head; tp != NULL; tp = tp->t_tcb_next) {
284567Swnj 		if (tp->t_init != 0 && --tp->t_init == 0)
294567Swnj 			tcp_usrreq(ISTIMER, TINIT, tp, 0);
304567Swnj 		if (tp->t_rexmt != 0 && --tp->t_rexmt == 0)
314567Swnj 			tcp_usrreq(ISTIMER, TREXMT, tp, 0);
324567Swnj 		if (tp->t_rexmttl != 0 && --tp->t_rexmttl == 0)
334567Swnj 			tcp_usrreq(ISTIMER, TREXMTTL, tp, 0);
344567Swnj 		if (tp->t_persist != 0 && --tp->t_persist == 0)
354567Swnj 			tcp_usrreq(ISTIMER, TPERSIST, tp, 0);
364567Swnj 		if (tp->t_finack != 0 && --tp->t_finack == 0)
374567Swnj 			tcp_usrreq(ISTIMER, TFINACK, tp, 0);
384567Swnj 		tp->t_xmt++;
394567Swnj 	}
404664Swnj 	tcp_iss += ISSINCR;		/* increment iss */
414567Swnj 	timeout(tcp_timeo, 0, hz);      /* reschedule every second */
424567Swnj 	splx(s);
434497Swnj }
444497Swnj 
454584Swnj tcp_usrreq(input, timertype, tp, mp)
464584Swnj 	int input, timertype;
474567Swnj 	register struct tcb *tp;
484584Swnj 	struct mbuf *mp;
494497Swnj {
504567Swnj 	int s = splnet();
514567Swnj 	register int nstate;
524584Swnj #ifdef TCPDEBUG
534584Swnj 	struct tcp_debug tdb;
544584Swnj #endif
554567Swnj COUNT(TCP_USRREQ);
564497Swnj 
574567Swnj 	nstate = tp->t_state;
584576Swnj 	tp->tc_flags &= ~TC_NET_KEEP;
594567Swnj 	acounts[nstate][input]++;
604584Swnj #ifdef TCPDEBUG
614584Swnj 	if ((tp->t_ucb->uc_flags & UDEBUG) || tcpconsdebug) {
624605Swnj 		tdb_setup(tp, (struct th *)0, input, &tdb);
634584Swnj 		tdb.td_tim = timertype;
644584Swnj 	} else
654584Swnj 		tdb.td_tod = 0;
664584Swnj #endif
674567Swnj 	switch (tcp_fstab[nstate][input]) {
684497Swnj 
694567Swnj 	default:
704567Swnj 		printf("tcp: bad state: tcb=%x state=%d input=%d\n",
714567Swnj 		    tp, tp->t_state, input);
724567Swnj 		nstate = EFAILEC;
734567Swnj 		break;
744497Swnj 
754567Swnj 	case LIS_CLS:				/* 1 */
764567Swnj 		t_open(tp, PASSIVE);
774567Swnj 		nstate = LISTEN;
784567Swnj 		break;
794497Swnj 
804567Swnj 	case SYS_CLS:				/* 2 */
814567Swnj 		t_open(tp, ACTIVE);
824567Swnj 		send_ctl(tp);
834567Swnj 		nstate = SYN_SENT;
844567Swnj 		break;
854497Swnj 
864567Swnj 	case CLS_OPN:				/* 10 */
874567Swnj 		t_close(tp, UCLOSED);
884567Swnj 		nstate = CLOSED;
894567Swnj 		break;
904497Swnj 
914567Swnj 	case CL2_CLW:				/* 10 */
924576Swnj 		tp->tc_flags |= TC_SND_FIN;
934567Swnj 		send_ctl(tp);
944576Swnj 		tp->tc_flags |= TC_USR_CLOSED;
954567Swnj 		nstate = CLOSING2;
964567Swnj 		break;
974497Swnj 
984567Swnj 	case TIMERS:				/* 14,17,34,35,36,37,38 */
994584Swnj 		nstate = tcp_timers(tp, timertype);
1004567Swnj 		break;
1014497Swnj 
1024567Swnj 	case CLS_RWT:				/* 20 */
1034567Swnj 		present_data(tp);
1044567Swnj 		if (rcv_empty(tp)) {
1054567Swnj 			t_close(tp, UCLOSED);
1064567Swnj 			nstate = CLOSED;
1074567Swnj 		} else
1084567Swnj 			nstate = RCV_WAIT;
1094567Swnj 		break;
1104497Swnj 
1114567Swnj 	case FW1_SYR:				/* 24,25 */
1124576Swnj 		tp->tc_flags |= TC_SND_FIN;
1134567Swnj 		send_ctl(tp);
1144576Swnj 		tp->tc_flags |= TC_USR_CLOSED;
1154567Swnj 		nstate = FIN_W1;
1164567Swnj 		break;
1174567Swnj 
1184567Swnj 	case SSS_SND:				/* 40,41 */
1194602Swnj 		nstate = sss_send(tp, mp);
1204567Swnj 		break;
1214567Swnj 
1224567Swnj 	case SSS_RCV:				/* 42 */
1234567Swnj 		send_ctl(tp);		/* send new window */
1244567Swnj 		present_data(tp);
1254567Swnj 		break;
1264567Swnj 
1274567Swnj 	case CLS_NSY:				/* 44 */
1284567Swnj 		t_close(tp, UABORT);
1294567Swnj 		nstate = CLOSED;
1304567Swnj 		break;
1314567Swnj 
1324567Swnj 	case CLS_SYN:				/* 45 */
1334576Swnj 		tp->tc_flags |= TC_SND_RST;
1344567Swnj 		send_null(tp);
1354567Swnj 		t_close(tp, UABORT);
1364567Swnj 		nstate = CLOSED;
1374567Swnj 		break;
1384567Swnj 
1394567Swnj 	case CLS_ACT:				/* 47 */
1404567Swnj 		t_close(tp, UNETDWN);
1414567Swnj 		nstate = CLOSED;
1424567Swnj 		break;
1434567Swnj 
1444567Swnj 	case NOP:
1454567Swnj 		break;
1464567Swnj 
1474567Swnj 	case CLS_ERR:
1484567Swnj 		to_user(tp->t_ucb, UCLSERR);
1494567Swnj 		break;
1504567Swnj 	}
1514567Swnj #ifdef TCPDEBUG
1524605Swnj 	if (tdb.td_tod)
1534605Swnj 		tdb_stuff(&tdb, nstate);
1544567Swnj #endif
1554567Swnj 	/* YECH */
1564567Swnj 	switch (nstate) {
1574567Swnj 
1584584Swnj 	case CLOSED:
1594567Swnj 	case SAME:
1604567Swnj 		break;
1614567Swnj 
1624567Swnj 	case EFAILEC:
1634584Swnj 		if (mp)
1644584Swnj 			m_freem(dtom(mp));
1654567Swnj 		break;
1664567Swnj 
1674567Swnj 	default:
1684567Swnj 		tp->t_state = nstate;
1694567Swnj 		break;
1704567Swnj 	}
1714567Swnj 	splx(s);
1724497Swnj }
1734497Swnj 
1744567Swnj t_open(tp, mode)                /* set up a tcb for a connection */
1754567Swnj 	register struct tcb *tp;
1764567Swnj 	int mode;
1774497Swnj {
1784567Swnj 	register struct ucb *up;
1794567Swnj COUNT(T_OPEN);
1804497Swnj 
1814567Swnj 	/* enqueue the tcb */
1824497Swnj 
1834664Swnj 	if (tcb_head == NULL) {
1844664Swnj 		tcb_head = tp;
1854664Swnj 		tcb_tail = tp;
1864567Swnj 	} else {
1874664Swnj 		tp->t_tcb_next = tcb_head;
1884664Swnj 		tcb_head->t_tcb_prev = tp;
1894664Swnj 		tcb_head = tp;
1904567Swnj 	}
1914497Swnj 
1924567Swnj 	/* initialize non-zero tcb fields */
1934497Swnj 
1944567Swnj 	tp->t_rcv_next = (struct th *)tp;
1954567Swnj 	tp->t_rcv_prev = (struct th *)tp;
1964567Swnj 	tp->t_xmtime = T_REXMT;
1974567Swnj 	tp->snd_end = tp->seq_fin = tp->snd_nxt = tp->snd_hi =
1984664Swnj 	              tp->snd_una = tp->iss = tcp_iss;
1994567Swnj 	tp->snd_off = tp->iss + 1;
2004664Swnj 	tcp_iss += (ISSINCR >> 1) + 1;
2014567Swnj 
2024567Swnj 	/* set timeout for open */
2034567Swnj 
2044567Swnj 	up = tp->t_ucb;
2054567Swnj 	tp->t_init = (up->uc_timeo != 0 ? up->uc_timeo :
2064567Swnj 					(mode == ACTIVE ? T_INIT : 0));
2074567Swnj 	up->uc_timeo = 0;       /* overlays uc_ssize */
2084497Swnj }
2094497Swnj 
2104567Swnj t_close(tp, state)
2114567Swnj 	register struct tcb *tp;
2124567Swnj 	short state;
2134497Swnj {
2144567Swnj 	register struct ucb *up;
2154567Swnj 	register struct th *t;
2164567Swnj 	register struct mbuf *m;
2174567Swnj COUNT(T_CLOSE);
2184497Swnj 
2194567Swnj 	up = tp->t_ucb;
2204497Swnj 
2214567Swnj 	tp->t_init = tp->t_rexmt = tp->t_rexmttl = tp->t_persist =
2224567Swnj 	    tp->t_finack = 0;
2234497Swnj 
2244567Swnj 	/* delete tcb */
2254567Swnj 
2264567Swnj 	if (tp->t_tcb_prev == NULL)
2274664Swnj 		tcb_head = tp->t_tcb_next;
2284567Swnj 	else
2294567Swnj 		tp->t_tcb_prev->t_tcb_next = tp->t_tcb_next;
2304567Swnj 	if (tp->t_tcb_next == NULL)
2314664Swnj 		tcb_tail = tp->t_tcb_prev;
2324567Swnj 	else
2334567Swnj 		tp->t_tcb_next->t_tcb_prev = tp->t_tcb_prev;
2344567Swnj 
2354567Swnj 	/* free all data on receive and send buffers */
2364567Swnj 
2374567Swnj 	for (t = tp->t_rcv_next; t != (struct th *)tp; t = t->t_next)
2384567Swnj 		m_freem(dtom(t));
2394567Swnj 
2404567Swnj 	if (up->uc_rbuf != NULL) {
2414567Swnj 		m_freem(up->uc_rbuf);
2424567Swnj 		up->uc_rbuf = NULL;
2434567Swnj 	}
2444657Swnj 	up->uc_rcc = 0;
2454567Swnj 	if (up->uc_sbuf != NULL) {
2464567Swnj 		m_freem(up->uc_sbuf);
2474567Swnj 		up->uc_sbuf = NULL;
2484567Swnj 	}
2494592Swnj 	up->uc_ssize = 0;
2504567Swnj 	for (m = tp->t_rcv_unack; m != NULL; m = m->m_act) {
2514567Swnj 		m_freem(m);
2524567Swnj 		tp->t_rcv_unack = NULL;
2534567Swnj 	}
2544664Swnj 	if (up->uc_template) {
2554664Swnj 		m_free(dtom(up->uc_template));
2564664Swnj 		up->uc_template = 0;
2574664Swnj 	}
258*4670Swnj 	wmemfree((caddr_t)tp, 1024);
2594567Swnj 	up->uc_tcb = NULL;
2604567Swnj 
2614567Swnj 	/* lower buffer allocation and decrement host entry */
2624567Swnj 
2634664Swnj 	mbstat.m_lowat -= up->uc_snd + (up->uc_rhiwat/MSIZE) + 2;
2644664Swnj 	mbstat.m_hiwat = 2 * mbstat.m_lowat;
2654567Swnj 	if (up->uc_host != NULL) {
2664567Swnj 		h_free(up->uc_host);
2674567Swnj 		up->uc_host = NULL;
2684567Swnj 	}
2694567Swnj 
2704567Swnj 	/* if user has initiated close (via close call), delete ucb
2714567Swnj 	   entry, otherwise just wakeup so user can issue close call */
2724567Swnj 
2734576Swnj 	if (tp->tc_flags&TC_USR_ABORT)
2744567Swnj         	up->uc_proc = NULL;
2754567Swnj 	else
2764567Swnj         	to_user(up, state);
2774497Swnj }
2784497Swnj 
2794602Swnj sss_send(tp, m0)
2804584Swnj 	register struct tcb *tp;
2814584Swnj 	struct mbuf *m0;
2824497Swnj {
2834497Swnj 	register struct mbuf *m, *n;
2844584Swnj 	register struct ucb *up = tp->t_ucb;
2854497Swnj 	register off;
2864574Swnj 	seq_t last;
2874602Swnj COUNT(SSS_SEND);
2884497Swnj 
2894497Swnj 	last = tp->snd_off;
2904584Swnj 	for (m = n = m0; m != NULL; m = m->m_next) {
2914497Swnj 		up->uc_ssize++;
2924591Swnj 		if (m->m_off > MMAXOFF)
2934588Swnj 			up->uc_ssize += NMBPG;
2944497Swnj 		last += m->m_len;
2954497Swnj 	}
2964588Swnj 	if ((m = up->uc_sbuf) == NULL)
2974588Swnj 		up->uc_sbuf = n;
2984588Swnj 	else {
2994588Swnj 		while (m->m_next != NULL) {
3004497Swnj 			m = m->m_next;
3014497Swnj 			last += m->m_len;
3024497Swnj 		}
3034591Swnj 		if (m->m_off <= MMAXOFF) {
3044588Swnj 			last += m->m_len;
3054588Swnj 			off = m->m_off + m->m_len;
3064591Swnj 			while (n && n->m_off <= MMAXOFF &&
3074591Swnj 			    (MMAXOFF - off) >= n->m_len) {
3084588Swnj 				bcopy((caddr_t)((int)n + n->m_off),
3094588Swnj 				      (caddr_t)((int)m + off), n->m_len);
3104588Swnj 				m->m_len += n->m_len;
3114588Swnj 				off += n->m_len;
3124588Swnj 				up->uc_ssize--;
3134588Swnj 				n = m_free(n);
3144588Swnj 			}
3154497Swnj 		}
3164497Swnj 		m->m_next = n;
3174588Swnj 	}
3184588Swnj 	if (up->uc_flags & UEOL)
3194497Swnj 		tp->snd_end = last;
3204588Swnj 	if (up->uc_flags & UURG) {
3214497Swnj 		tp->snd_urp = last+1;
3224576Swnj 		tp->tc_flags |= TC_SND_URG;
3234567Swnj 	}
3244497Swnj 	send(tp);
3254567Swnj 	return (SAME);
3264497Swnj }
3274497Swnj 
3284584Swnj tcp_timers(tp, timertype)
3294584Swnj 	register struct tcb *tp;
3304584Swnj 	int timertype;
3314497Swnj {
3324497Swnj 
3334567Swnj COUNT(TCP_TIMERS);
3344584Swnj 	switch (timertype) {
3354497Swnj 
3364567Swnj 	case TINIT:		/* initialization timer */
3374576Swnj 		if ((tp->tc_flags&TC_SYN_ACKED) == 0) {		/* 35 */
3384567Swnj 			t_close(tp, UINTIMO);
3394567Swnj 			return (CLOSED);
3404567Swnj 		}
3414567Swnj 		return (SAME);
3424497Swnj 
3434567Swnj 	case TFINACK:		/* fin-ack timer */
3444567Swnj 		switch (tp->t_state) {
3454497Swnj 
3464567Swnj 		case TIME_WAIT:
3474567Swnj 			/*
3484567Swnj 			 * We can be sure our ACK of foreign FIN was rcvd,
3494567Swnj 			 * and can close if no data left for user.
3504567Swnj 			 */
3514567Swnj 			if (rcv_empty(tp)) {
3524567Swnj 				t_close(tp, UCLOSED);		/* 14 */
3534567Swnj 				return (CLOSED);
3544567Swnj 			}
3554567Swnj 			return (RCV_WAIT);			/* 17 */
3564497Swnj 
3574567Swnj 		case CLOSING1:
3584576Swnj 			tp->tc_flags |= TC_WAITED_2_ML;
3594567Swnj 			return (SAME);
3604497Swnj 
3614567Swnj 		default:
3624567Swnj 			return (SAME);
3634567Swnj 		}
3644497Swnj 
3654567Swnj 	case TREXMT:		/* retransmission timer */
3664567Swnj 		if (tp->t_rexmt_val > tp->snd_una) {	 	/* 34 */
3674567Swnj 			/*
3684567Swnj 			 * Set up for a retransmission, increase rexmt time
3694567Swnj 			 * in case of multiple retransmissions.
3704567Swnj 			 */
3714567Swnj 			tp->snd_nxt = tp->snd_una;
3724576Swnj 			tp->tc_flags |= TC_REXMT;
3734567Swnj 			tp->t_xmtime = tp->t_xmtime << 1;
3744567Swnj 			if (tp->t_xmtime > T_REMAX)
3754567Swnj 				tp->t_xmtime = T_REMAX;
3764567Swnj 			send(tp);
3774567Swnj 		}
3784567Swnj 		return (SAME);
3794497Swnj 
3804567Swnj 	case TREXMTTL:		/* retransmit too long */
3814567Swnj 		if (tp->t_rtl_val > tp->snd_una)		/* 36 */
3824567Swnj 			to_user(tp->t_ucb, URXTIMO);
3834567Swnj 		/*
3844567Swnj 		 * If user has already closed, abort the connection.
3854567Swnj 		 */
3864576Swnj 		if (tp->tc_flags & TC_USR_CLOSED) {
3874567Swnj 			t_close(tp, URXTIMO);
3884567Swnj 			return (CLOSED);
3894567Swnj 		}
3904567Swnj 		return (SAME);
3914497Swnj 
3924567Swnj 	case TPERSIST:		/* persist timer */
3934567Swnj 		/*
3944567Swnj 		 * Force a byte send through closed window.
3954567Swnj 		 */
3964576Swnj 		tp->tc_flags |= TC_FORCE_ONE;
3974567Swnj 		send(tp);
3984567Swnj 		return (SAME);
3994567Swnj 	}
4004567Swnj 	panic("tcp_timers");
4014497Swnj }
4024497Swnj 
4034567Swnj /* THIS ROUTINE IS A CROCK */
4044567Swnj to_user(up, state)
4054567Swnj 	register struct ucb *up;
4064567Swnj 	register short state;
4074497Swnj {
4084567Swnj COUNT(TO_USER);
4094497Swnj 
4104567Swnj 	up->uc_state |= state;
4114567Swnj 	netwakeup(up);
4124567Swnj   	if (state == UURGENT)
4134567Swnj 		psignal(up->uc_proc, SIGURG);
4144497Swnj }
4154584Swnj 
4164584Swnj #ifdef TCPDEBUG
4174584Swnj tcp_prt(tdp)
4184584Swnj 	register struct tcp_debug *tdp;
4194584Swnj {
4204584Swnj COUNT(TCP_PRT);
4214584Swnj 
4224605Swnj 	printf("TCP(%x) %s x %s",
4234584Swnj 	    tdp->td_tcb, tcpstates[tdp->td_old], tcpinputs[tdp->td_inp]);
4244584Swnj 	if (tdp->td_inp == ISTIMER)
4254584Swnj 		printf("(%s)", tcptimers[tdp->td_tim]);
4264584Swnj 	printf(" --> %s",
4274584Swnj 	    tcpstates[(tdp->td_new > 0) ? tdp->td_new : tdp->td_old]);
4284584Swnj 	/* GROSS... DEPENDS ON SIGN EXTENSION OF CHARACTERS */
4294584Swnj 	if (tdp->td_new < 0)
4304584Swnj 		printf(" (FAILED)");
4314605Swnj 	if (tdp->td_sno) {
4324605Swnj 		printf(" sno %x ano %x win %d len %d flags %x",
4334605Swnj 		    tdp->td_sno, tdp->td_ano, tdp->td_wno, tdp->td_lno, tdp->td_flg);
4344605Swnj 	}
4354584Swnj 	printf("\n");
4364584Swnj }
4374584Swnj #endif
438*4670Swnj #ifdef TCPDEBUG
439*4670Swnj tdb_setup(tp, n, input, tdp)
440*4670Swnj 	struct tcb *tp;
441*4670Swnj 	register struct th *n;
442*4670Swnj 	int input;
443*4670Swnj 	register struct tcp_debug *tdp;
444*4670Swnj {
445*4670Swnj 
446*4670Swnj 	tdp->td_tod = time;
447*4670Swnj 	tdp->td_tcb = tp;
448*4670Swnj 	tdp->td_old = tp->t_state;
449*4670Swnj 	tdp->td_inp = input;
450*4670Swnj 	tdp->td_tim = 0;
451*4670Swnj 	tdp->td_new = -1;
452*4670Swnj 	if (n) {
453*4670Swnj 		tdp->td_sno = n->t_seq;
454*4670Swnj 		tdp->td_ano = n->t_ackno;
455*4670Swnj 		tdp->td_wno = n->t_win;
456*4670Swnj 		tdp->td_lno = n->t_len;
457*4670Swnj 		tdp->td_flg = n->th_flags;
458*4670Swnj 	} else
459*4670Swnj 		tdp->td_sno = tdp->td_ano = tdp->td_wno = tdp->td_lno =
460*4670Swnj 		    tdp->td_flg = 0;
461*4670Swnj }
462*4670Swnj 
463*4670Swnj tdb_stuff(tdp, nstate)
464*4670Swnj 	struct tcp_debug *tdp;
465*4670Swnj 	int nstate;
466*4670Swnj {
467*4670Swnj 
468*4670Swnj 	tdp->td_new = nstate;
469*4670Swnj 	tcp_debug[tdbx++ % TDBSIZE] = *tdp;
470*4670Swnj 	if (tcpconsdebug & 2)
471*4670Swnj 		tcp_prt(tdp);
472*4670Swnj }
473*4670Swnj #endif
474