xref: /csrg-svn/sys/netinet/tcp_usrreq.c (revision 6506)
1*6506Ssam /*	tcp_usrreq.c	1.55	82/04/10	*/
24567Swnj 
34497Swnj #include "../h/param.h"
44567Swnj #include "../h/systm.h"
54664Swnj #include "../h/mbuf.h"
64664Swnj #include "../h/socket.h"
74809Swnj #include "../h/socketvar.h"
84809Swnj #include "../h/protosw.h"
95089Swnj #include "../net/in.h"
106353Ssam #include "../net/route.h"
115089Swnj #include "../net/in_pcb.h"
125089Swnj #include "../net/in_systm.h"
134954Swnj #include "../net/if.h"
144809Swnj #include "../net/ip.h"
154900Swnj #include "../net/ip_var.h"
164809Swnj #include "../net/tcp.h"
174809Swnj #include "../net/tcp_fsm.h"
185089Swnj #include "../net/tcp_seq.h"
195089Swnj #include "../net/tcp_timer.h"
204809Swnj #include "../net/tcp_var.h"
215089Swnj #include "../net/tcpip.h"
225270Sroot #include "../net/tcp_debug.h"
23*6506Ssam #include <errno.h>
244497Swnj 
255280Sroot /*
265280Sroot  * TCP protocol interface to socket abstraction.
275280Sroot  */
285280Sroot extern	char *tcpstates[];
294954Swnj struct	tcpcb *tcp_newtcpcb();
305280Sroot 
314734Swnj /*
325280Sroot  * Process a TCP user request for TCP tb.  If this is a send request
334731Swnj  * then m is the mbuf chain of send data.  If this is a timer expiration
344731Swnj  * (called from the software clock routine), then timertype tells which timer.
354731Swnj  */
364809Swnj tcp_usrreq(so, req, m, addr)
374809Swnj 	struct socket *so;
384809Swnj 	int req;
394731Swnj 	struct mbuf *m;
404809Swnj 	caddr_t addr;
414497Swnj {
424886Swnj 	register struct inpcb *inp = sotoinpcb(so);
434911Swnj 	register struct tcpcb *tp;
444567Swnj 	int s = splnet();
454809Swnj 	int error = 0;
465270Sroot 	int ostate;
474567Swnj COUNT(TCP_USRREQ);
484497Swnj 
494886Swnj 	/*
505280Sroot 	 * When a TCP is attached to a socket, then there will be
515280Sroot 	 * a (struct inpcb) pointed at by the socket, and this
525280Sroot 	 * structure will point at a subsidary (struct tcpcb).
535280Sroot 	 * The normal sequence of events is:
545280Sroot 	 *	PRU_ATTACH		creating these structures
555280Sroot 	 *	PRU_CONNECT		connecting to a remote peer
565280Sroot 	 *	(PRU_SEND|PRU_RCVD)*	exchanging data
575280Sroot 	 *	PRU_DISCONNECT		disconnecting from remote peer
585280Sroot 	 *	PRU_DETACH		deleting the structures
595280Sroot 	 * With the operations from PRU_CONNECT through PRU_DISCONNECT
605280Sroot 	 * possible repeated several times.
615280Sroot 	 *
625280Sroot 	 * MULTIPLE CONNECTS ARE NOT YET IMPLEMENTED.
634886Swnj 	 */
645089Swnj 	if (inp == 0 && req != PRU_ATTACH) {
655075Swnj 		splx(s);
665280Sroot 		return (EINVAL);		/* XXX */
675075Swnj 	}
685075Swnj 	if (inp) {
694911Swnj 		tp = intotcpcb(inp);
704731Swnj #ifdef KPROF
715075Swnj 		tcp_acounts[tp->t_state][req]++;
724731Swnj #endif
735270Sroot 		ostate = tp->t_state;
744911Swnj 	}
754809Swnj 	switch (req) {
764497Swnj 
775280Sroot 	/*
785280Sroot 	 * TCP attaches to socket via PRU_ATTACH, reserving space,
795280Sroot 	 * and internet and TCP control blocks.
805280Sroot 	 * If the socket is to receive connections,
815280Sroot 	 * then the LISTEN state is entered.
825280Sroot 	 */
834809Swnj 	case PRU_ATTACH:
844954Swnj 		if (inp) {
854809Swnj 			error = EISCONN;
864911Swnj 			break;
874886Swnj 		}
885280Sroot 		error = tcp_attach(so, (struct sockaddr *)addr);
895075Swnj 		if (error)
904954Swnj 			break;
915392Swnj 		if ((so->so_options & SO_DONTLINGER) == 0)
925392Swnj 			so->so_linger = TCP_LINGERTIME;
935280Sroot 		tp = sototcpcb(so);
944567Swnj 		break;
954497Swnj 
965280Sroot 	/*
975280Sroot 	 * PRU_DETACH detaches the TCP protocol from the socket.
985280Sroot 	 * If the protocol state is non-embryonic, then can't
995280Sroot 	 * do this directly: have to initiate a PRU_DISCONNECT,
1005280Sroot 	 * which may finish later; embryonic TCB's can just
1015280Sroot 	 * be discarded here.
1025280Sroot 	 */
1034809Swnj 	case PRU_DETACH:
1045280Sroot 		if (tp->t_state > TCPS_LISTEN)
1055280Sroot 			tcp_disconnect(tp);
1065280Sroot 		else {
1075280Sroot 			tcp_close(tp);
1085280Sroot 			tp = 0;
1095280Sroot 		}
1104809Swnj 		break;
1114809Swnj 
1125280Sroot 	/*
1135280Sroot 	 * Initiate connection to peer.
1145280Sroot 	 * Create a template for use in transmissions on this connection.
1155280Sroot 	 * Enter SYN_SENT state, and mark socket as connecting.
1165280Sroot 	 * Start keep-alive timer, and seed output sequence space.
1175280Sroot 	 * Send initial segment on connection.
1185280Sroot 	 */
1194809Swnj 	case PRU_CONNECT:
1205165Swnj 		error = in_pcbconnect(inp, (struct sockaddr_in *)addr);
1214954Swnj 		if (error)
1224886Swnj 			break;
1235174Swnj 		tp->t_template = tcp_template(tp);
1245280Sroot 		if (tp->t_template == 0) {
1255280Sroot 			in_pcbdisconnect(inp);
1265280Sroot 			error = ENOBUFS;
1275280Sroot 			break;
1285280Sroot 		}
1294886Swnj 		soisconnecting(so);
1305075Swnj 		tp->t_state = TCPS_SYN_SENT;
1315245Sroot 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
1325245Sroot 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
1335245Sroot 		tcp_sendseqinit(tp);
134*6506Ssam 		error = tcp_output(tp);
1354567Swnj 		break;
1364497Swnj 
1375280Sroot 	/*
1385280Sroot 	 * Initiate disconnect from peer.
1395280Sroot 	 * If connection never passed embryonic stage, just drop;
1405280Sroot 	 * else if don't need to let data drain, then can just drop anyways,
1415280Sroot 	 * else have to begin TCP shutdown process: mark socket disconnecting,
1425280Sroot 	 * drain unread data, state switch to reflect user close, and
1435280Sroot 	 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
1445280Sroot 	 * when peer sends FIN and acks ours.
1455280Sroot 	 *
1465280Sroot 	 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
1475280Sroot 	 */
1485280Sroot 	case PRU_DISCONNECT:
1495280Sroot 		tcp_disconnect(tp);
1505245Sroot 		break;
1515245Sroot 
1525280Sroot 	/*
1535280Sroot 	 * Accept a connection.  Essentially all the work is
1545280Sroot 	 * done at higher levels; just return the address
1555280Sroot 	 * of the peer, storing through addr.
1565280Sroot 	 */
1576117Swnj 	case PRU_ACCEPT: {
1586117Swnj 		struct sockaddr_in *sin = (struct sockaddr_in *)addr;
1596117Swnj 
1606117Swnj 		if (sin) {
1616117Swnj 			bzero((caddr_t)sin, sizeof (*sin));
1626117Swnj 			sin->sin_family = AF_INET;
1636117Swnj 			sin->sin_port = inp->inp_fport;
1646117Swnj 			sin->sin_addr = inp->inp_faddr;
1656117Swnj 		}
1666117Swnj 		}
1674954Swnj 		break;
1684925Swnj 
1695280Sroot 	/*
1705280Sroot 	 * Mark the connection as being incapable of further output.
1715280Sroot 	 */
1724809Swnj 	case PRU_SHUTDOWN:
1735089Swnj 		socantsendmore(so);
1745245Sroot 		tcp_usrclosed(tp);
175*6506Ssam 		error = tcp_output(tp);
1764567Swnj 		break;
1774497Swnj 
1785280Sroot 	/*
1795280Sroot 	 * After a receive, possibly send window update to peer.
1805280Sroot 	 */
1814809Swnj 	case PRU_RCVD:
1825113Swnj 		(void) tcp_output(tp);
1834567Swnj 		break;
1844497Swnj 
1855280Sroot 	/*
1865280Sroot 	 * Do a send by putting data in output queue and updating urgent
1875280Sroot 	 * marker if URG set.  Possibly send more data.
1885280Sroot 	 */
1894809Swnj 	case PRU_SEND:
1905075Swnj 		sbappend(&so->so_snd, m);
191*6506Ssam #ifdef notdef
1925089Swnj 		if (tp->t_flags & TF_PUSH)
1935075Swnj 			tp->snd_end = tp->snd_una + so->so_snd.sb_cc;
194*6506Ssam #endif
195*6506Ssam 		error = tcp_output(tp);
1964567Swnj 		break;
1974567Swnj 
1985280Sroot 	/*
1995280Sroot 	 * Abort the TCP.
2005280Sroot 	 */
2014809Swnj 	case PRU_ABORT:
2025075Swnj 		tcp_drop(tp, ECONNABORTED);
2034567Swnj 		break;
2044567Swnj 
2055280Sroot /* SOME AS YET UNIMPLEMENTED HOOKS */
2064809Swnj 	case PRU_CONTROL:
2074886Swnj 		error = EOPNOTSUPP;
2084809Swnj 		break;
2094809Swnj 
2105113Swnj 	case PRU_SENSE:
2115113Swnj 		error = EOPNOTSUPP;
2125113Swnj 		break;
2135417Swnj /* END UNIMPLEMENTED HOOKS */
2145113Swnj 
2155113Swnj 	case PRU_RCVOOB:
2165442Swnj 		if (so->so_oobmark == 0 &&
2175442Swnj 		    (so->so_state & SS_RCVATMARK) == 0) {
2185417Swnj 			error = EINVAL;
2195417Swnj 			break;
2205417Swnj 		}
2215549Swnj 		if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
2225442Swnj 			error = EWOULDBLOCK;
2235549Swnj 			break;
2245442Swnj 		}
2255549Swnj 		*mtod(m, caddr_t) = tp->t_iobc;
2265113Swnj 		break;
2275113Swnj 
2285113Swnj 	case PRU_SENDOOB:
2295549Swnj #ifdef TCPTRUEOOB
2305549Swnj 		if (tp->t_flags & TF_DOOOB) {
2315549Swnj 			tp->t_oobseq++;
2325549Swnj 			tp->t_oobc = *mtod(m, caddr_t);
2335549Swnj 			tp->t_oobmark = tp->snd_una + so->so_snd.sb_cc;
2345549Swnj printf("sendoob seq now %x oobc %x\n", tp->t_oobseq, tp->t_oobc);
2355549Swnj 			tp->t_oobflags |= TCPOOB_NEEDACK;
236*6506Ssam 			/* what to do ...? */
237*6506Ssam 			if (error = tcp_output(tp))
238*6506Ssam 				break;
2395549Swnj 		}
2405549Swnj #endif
2415442Swnj 		if (sbspace(&so->so_snd) < -512) {
2425442Swnj 			error = ENOBUFS;
2435442Swnj 			break;
2445442Swnj 		}
2455417Swnj 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc + 1;
2465417Swnj 		sbappend(&so->so_snd, m);
247*6506Ssam #ifdef notdef
2485417Swnj 		if (tp->t_flags & TF_PUSH)
2495417Swnj 			tp->snd_end = tp->snd_una + so->so_snd.sb_cc;
250*6506Ssam #endif
2515549Swnj 		tp->t_force = 1;
252*6506Ssam 		error = tcp_output(tp);
2535549Swnj 		tp->t_force = 0;
2545113Swnj 		break;
2555113Swnj 
2565280Sroot 	/*
2575280Sroot 	 * TCP slow timer went off; going through this
2585280Sroot 	 * routine for tracing's sake.
2595280Sroot 	 */
2604809Swnj 	case PRU_SLOWTIMO:
2615075Swnj 		tcp_timers(tp, (int)addr);
2625270Sroot 		req |= (int)addr << 8;		/* for debug's sake */
2634809Swnj 		break;
2644809Swnj 
2654731Swnj 	default:
2664731Swnj 		panic("tcp_usrreq");
2674567Swnj 	}
2685270Sroot 	if (tp && (so->so_options & SO_DEBUG))
2695270Sroot 		tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req);
2704567Swnj 	splx(s);
2714886Swnj 	return (error);
2724497Swnj }
2735245Sroot 
2745953Swnj int	tcp_sendspace = 1024*2;
2755953Swnj int	tcp_recvspace = 1024*3;
2765280Sroot /*
2775280Sroot  * Attach TCP protocol to socket, allocating
2785280Sroot  * internet protocol control block, tcp control block,
2795280Sroot  * bufer space, and entering LISTEN state if to accept connections.
2805280Sroot  */
2815280Sroot tcp_attach(so, sa)
2825280Sroot 	struct socket *so;
2835280Sroot 	struct sockaddr *sa;
2845280Sroot {
2855280Sroot 	register struct tcpcb *tp;
2865280Sroot 	struct inpcb *inp;
2875280Sroot 	int error;
2885280Sroot 
2895953Swnj 	error = in_pcbattach(so, &tcb,
2905953Swnj 	    tcp_sendspace, tcp_recvspace, (struct sockaddr_in *)sa);
2915280Sroot 	if (error)
2925280Sroot 		return (error);
2935280Sroot 	inp = (struct inpcb *)so->so_pcb;
2945280Sroot 	tp = tcp_newtcpcb(inp);
2955280Sroot 	if (so->so_options & SO_ACCEPTCONN) {
2965280Sroot 		if (tp == 0) {
2975280Sroot 			in_pcbdetach(inp);
2985280Sroot 			return (ENOBUFS);
2995280Sroot 		}
3005280Sroot 		tp->t_state = TCPS_LISTEN;
3015280Sroot 	} else
3025280Sroot 		tp->t_state = TCPS_CLOSED;
3035280Sroot 	return (0);
3045280Sroot }
3055280Sroot 
3065280Sroot /*
3075280Sroot  * Initiate (or continue) disconnect.
3085280Sroot  * If embryonic state, just send reset (once).
3095280Sroot  * If not in ``let data drain'' option, just drop.
3105280Sroot  * Otherwise (hard), mark socket disconnecting and drop
3115280Sroot  * current input data; switch states based on user close, and
3125280Sroot  * send segment to peer (with FIN).
3135280Sroot  */
3145280Sroot tcp_disconnect(tp)
3155280Sroot 	struct tcpcb *tp;
3165280Sroot {
3175280Sroot 	struct socket *so = tp->t_inpcb->inp_socket;
3185280Sroot 
3195280Sroot 	if (tp->t_state < TCPS_ESTABLISHED)
3205280Sroot 		tcp_close(tp);
3215392Swnj 	else if (so->so_linger == 0)
3225280Sroot 		tcp_drop(tp, 0);
3235280Sroot 	else {
3245280Sroot 		soisdisconnecting(so);
3255280Sroot 		sbflush(&so->so_rcv);
3265280Sroot 		tcp_usrclosed(tp);
3275280Sroot 		(void) tcp_output(tp);
3285280Sroot 	}
3295280Sroot }
3305280Sroot 
3315280Sroot /*
3325280Sroot  * User issued close, and wish to trail through shutdown states:
3335280Sroot  * if never received SYN, just forget it.  If got a SYN from peer,
3345280Sroot  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
3355280Sroot  * If already got a FIN from peer, then almost done; go to LAST_ACK
3365280Sroot  * state.  In all other cases, have already sent FIN to peer (e.g.
3375280Sroot  * after PRU_SHUTDOWN), and just have to play tedious game waiting
3385280Sroot  * for peer to send FIN or not respond to keep-alives, etc.
3395897Swnj  * We can let the user exit from the close as soon as the FIN is acked.
3405280Sroot  */
3415245Sroot tcp_usrclosed(tp)
3425245Sroot 	struct tcpcb *tp;
3435245Sroot {
3445245Sroot 
3455245Sroot 	switch (tp->t_state) {
3465245Sroot 
3475245Sroot 	case TCPS_LISTEN:
3485245Sroot 	case TCPS_SYN_SENT:
3495245Sroot 		tp->t_state = TCPS_CLOSED;
3505245Sroot 		tcp_close(tp);
3515245Sroot 		break;
3525245Sroot 
3535245Sroot 	case TCPS_SYN_RECEIVED:
3545245Sroot 	case TCPS_ESTABLISHED:
3555245Sroot 		tp->t_state = TCPS_FIN_WAIT_1;
3565245Sroot 		break;
3575245Sroot 
3585245Sroot 	case TCPS_CLOSE_WAIT:
3595245Sroot 		tp->t_state = TCPS_LAST_ACK;
3605245Sroot 		break;
3615245Sroot 	}
3625897Swnj 	if (tp->t_state >= TCPS_FIN_WAIT_2)
3635897Swnj 		soisdisconnected(tp->t_inpcb->inp_socket);
3645245Sroot }
365