xref: /csrg-svn/sys/netinet/tcp_usrreq.c (revision 8272)
1*8272Sroot /*	tcp_usrreq.c	1.62	82/09/26	*/
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"
236506Ssam #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  */
36*8272Sroot tcp_usrreq(so, req, m, nam, opt)
374809Swnj 	struct socket *so;
384809Swnj 	int req;
39*8272Sroot 	struct mbuf *m, *nam;
40*8272Sroot 	struct socketopt *opt;
414497Swnj {
424886Swnj 	register struct inpcb *inp = sotoinpcb(so);
434911Swnj 	register struct tcpcb *tp;
444567Swnj 	int s = splnet();
454809Swnj 	int error = 0;
465270Sroot 	int ostate;
474497Swnj 
484886Swnj 	/*
495280Sroot 	 * When a TCP is attached to a socket, then there will be
505280Sroot 	 * a (struct inpcb) pointed at by the socket, and this
515280Sroot 	 * structure will point at a subsidary (struct tcpcb).
524886Swnj 	 */
535089Swnj 	if (inp == 0 && req != PRU_ATTACH) {
545075Swnj 		splx(s);
555280Sroot 		return (EINVAL);		/* XXX */
565075Swnj 	}
575075Swnj 	if (inp) {
584911Swnj 		tp = intotcpcb(inp);
59*8272Sroot 		/* WHAT IF TP IS 0? */
604731Swnj #ifdef KPROF
615075Swnj 		tcp_acounts[tp->t_state][req]++;
624731Swnj #endif
635270Sroot 		ostate = tp->t_state;
647511Sroot 	} else
657511Sroot 		ostate = 0;
664809Swnj 	switch (req) {
674497Swnj 
685280Sroot 	/*
695280Sroot 	 * TCP attaches to socket via PRU_ATTACH, reserving space,
70*8272Sroot 	 * and an internet control block.
715280Sroot 	 */
724809Swnj 	case PRU_ATTACH:
734954Swnj 		if (inp) {
744809Swnj 			error = EISCONN;
754911Swnj 			break;
764886Swnj 		}
77*8272Sroot 		error = tcp_attach(so, nam);
785075Swnj 		if (error)
794954Swnj 			break;
805392Swnj 		if ((so->so_options & SO_DONTLINGER) == 0)
815392Swnj 			so->so_linger = TCP_LINGERTIME;
825280Sroot 		tp = sototcpcb(so);
834567Swnj 		break;
844497Swnj 
855280Sroot 	/*
865280Sroot 	 * PRU_DETACH detaches the TCP protocol from the socket.
875280Sroot 	 * If the protocol state is non-embryonic, then can't
885280Sroot 	 * do this directly: have to initiate a PRU_DISCONNECT,
895280Sroot 	 * which may finish later; embryonic TCB's can just
905280Sroot 	 * be discarded here.
915280Sroot 	 */
924809Swnj 	case PRU_DETACH:
935280Sroot 		if (tp->t_state > TCPS_LISTEN)
945280Sroot 			tcp_disconnect(tp);
955280Sroot 		else {
965280Sroot 			tcp_close(tp);
975280Sroot 			tp = 0;
985280Sroot 		}
994809Swnj 		break;
1004809Swnj 
1015280Sroot 	/*
102*8272Sroot 	 * Give the socket an address.
103*8272Sroot 	 */
104*8272Sroot 	case PRU_BIND:
105*8272Sroot 		error = in_pcbbind(inp, nam);
106*8272Sroot 		if (error)
107*8272Sroot 			break;
108*8272Sroot 		break;
109*8272Sroot 
110*8272Sroot 	/*
111*8272Sroot 	 * Prepare to accept connections.
112*8272Sroot 	 */
113*8272Sroot 	case PRU_LISTEN:
114*8272Sroot 		if (inp->inp_lport == 0)
115*8272Sroot 			error = in_pcbbind(inp, (struct mbuf *)0);
116*8272Sroot 		if (error == 0)
117*8272Sroot 			tp->t_state = TCPS_LISTEN;
118*8272Sroot 		break;
119*8272Sroot 
120*8272Sroot 	/*
1215280Sroot 	 * Initiate connection to peer.
1225280Sroot 	 * Create a template for use in transmissions on this connection.
1235280Sroot 	 * Enter SYN_SENT state, and mark socket as connecting.
1245280Sroot 	 * Start keep-alive timer, and seed output sequence space.
1255280Sroot 	 * Send initial segment on connection.
1265280Sroot 	 */
1274809Swnj 	case PRU_CONNECT:
128*8272Sroot 		if (inp->inp_lport == 0) {
129*8272Sroot 			error = in_pcbbind(inp, (struct mbuf *)0);
130*8272Sroot 			if (error)
131*8272Sroot 				break;
132*8272Sroot 		}
133*8272Sroot 		error = in_pcbconnect(inp, nam);
1344954Swnj 		if (error)
1354886Swnj 			break;
1365174Swnj 		tp->t_template = tcp_template(tp);
1375280Sroot 		if (tp->t_template == 0) {
1385280Sroot 			in_pcbdisconnect(inp);
1395280Sroot 			error = ENOBUFS;
1405280Sroot 			break;
1415280Sroot 		}
1424886Swnj 		soisconnecting(so);
1435075Swnj 		tp->t_state = TCPS_SYN_SENT;
1445245Sroot 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
1455245Sroot 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
1465245Sroot 		tcp_sendseqinit(tp);
1476506Ssam 		error = tcp_output(tp);
1484567Swnj 		break;
1494497Swnj 
1505280Sroot 	/*
1515280Sroot 	 * Initiate disconnect from peer.
1525280Sroot 	 * If connection never passed embryonic stage, just drop;
1535280Sroot 	 * else if don't need to let data drain, then can just drop anyways,
1545280Sroot 	 * else have to begin TCP shutdown process: mark socket disconnecting,
1555280Sroot 	 * drain unread data, state switch to reflect user close, and
1565280Sroot 	 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
1575280Sroot 	 * when peer sends FIN and acks ours.
1585280Sroot 	 *
1595280Sroot 	 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
1605280Sroot 	 */
1615280Sroot 	case PRU_DISCONNECT:
1625280Sroot 		tcp_disconnect(tp);
1635245Sroot 		break;
1645245Sroot 
1655280Sroot 	/*
1665280Sroot 	 * Accept a connection.  Essentially all the work is
1675280Sroot 	 * done at higher levels; just return the address
1685280Sroot 	 * of the peer, storing through addr.
1695280Sroot 	 */
1706117Swnj 	case PRU_ACCEPT: {
171*8272Sroot 		struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
1726117Swnj 
173*8272Sroot 		nam->m_len = sizeof (struct sockaddr_in);
174*8272Sroot 		sin->sin_family = AF_INET;
175*8272Sroot 		sin->sin_port = inp->inp_fport;
176*8272Sroot 		sin->sin_addr = inp->inp_faddr;
177*8272Sroot 		break;
1786117Swnj 		}
1794925Swnj 
1805280Sroot 	/*
1815280Sroot 	 * Mark the connection as being incapable of further output.
1825280Sroot 	 */
1834809Swnj 	case PRU_SHUTDOWN:
1845089Swnj 		socantsendmore(so);
1855245Sroot 		tcp_usrclosed(tp);
1866506Ssam 		error = tcp_output(tp);
1874567Swnj 		break;
1884497Swnj 
1895280Sroot 	/*
1905280Sroot 	 * After a receive, possibly send window update to peer.
1915280Sroot 	 */
1924809Swnj 	case PRU_RCVD:
1935113Swnj 		(void) tcp_output(tp);
1944567Swnj 		break;
1954497Swnj 
1965280Sroot 	/*
1975280Sroot 	 * Do a send by putting data in output queue and updating urgent
1985280Sroot 	 * marker if URG set.  Possibly send more data.
1995280Sroot 	 */
2004809Swnj 	case PRU_SEND:
2015075Swnj 		sbappend(&so->so_snd, m);
2026506Ssam #ifdef notdef
2035089Swnj 		if (tp->t_flags & TF_PUSH)
2045075Swnj 			tp->snd_end = tp->snd_una + so->so_snd.sb_cc;
2056506Ssam #endif
2066506Ssam 		error = tcp_output(tp);
2074567Swnj 		break;
2084567Swnj 
2095280Sroot 	/*
2105280Sroot 	 * Abort the TCP.
2115280Sroot 	 */
2124809Swnj 	case PRU_ABORT:
2135075Swnj 		tcp_drop(tp, ECONNABORTED);
2144567Swnj 		break;
2154567Swnj 
2165280Sroot /* SOME AS YET UNIMPLEMENTED HOOKS */
2174809Swnj 	case PRU_CONTROL:
2184886Swnj 		error = EOPNOTSUPP;
2194809Swnj 		break;
2204809Swnj 
2215113Swnj 	case PRU_SENSE:
2225113Swnj 		error = EOPNOTSUPP;
2235113Swnj 		break;
2245417Swnj /* END UNIMPLEMENTED HOOKS */
2255113Swnj 
2265113Swnj 	case PRU_RCVOOB:
2275442Swnj 		if (so->so_oobmark == 0 &&
2285442Swnj 		    (so->so_state & SS_RCVATMARK) == 0) {
2295417Swnj 			error = EINVAL;
2305417Swnj 			break;
2315417Swnj 		}
2325549Swnj 		if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
2335442Swnj 			error = EWOULDBLOCK;
2345549Swnj 			break;
2355442Swnj 		}
2365549Swnj 		*mtod(m, caddr_t) = tp->t_iobc;
2375113Swnj 		break;
2385113Swnj 
2395113Swnj 	case PRU_SENDOOB:
2405549Swnj #ifdef TCPTRUEOOB
2415549Swnj 		if (tp->t_flags & TF_DOOOB) {
2425549Swnj 			tp->t_oobseq++;
2435549Swnj 			tp->t_oobc = *mtod(m, caddr_t);
2445549Swnj 			tp->t_oobmark = tp->snd_una + so->so_snd.sb_cc;
2455549Swnj 			tp->t_oobflags |= TCPOOB_NEEDACK;
2466506Ssam 			/* what to do ...? */
2476506Ssam 			if (error = tcp_output(tp))
2486506Ssam 				break;
2495549Swnj 		}
2505549Swnj #endif
2515442Swnj 		if (sbspace(&so->so_snd) < -512) {
2525442Swnj 			error = ENOBUFS;
2535442Swnj 			break;
2545442Swnj 		}
2555417Swnj 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc + 1;
2565417Swnj 		sbappend(&so->so_snd, m);
2576506Ssam #ifdef notdef
2585417Swnj 		if (tp->t_flags & TF_PUSH)
2595417Swnj 			tp->snd_end = tp->snd_una + so->so_snd.sb_cc;
2606506Ssam #endif
2615549Swnj 		tp->t_force = 1;
2626506Ssam 		error = tcp_output(tp);
2635549Swnj 		tp->t_force = 0;
2645113Swnj 		break;
2655113Swnj 
2666510Ssam 	case PRU_SOCKADDR:
267*8272Sroot 		in_setsockaddr(inp, nam);
2686510Ssam 		break;
2696510Ssam 
2705280Sroot 	/*
2715280Sroot 	 * TCP slow timer went off; going through this
2725280Sroot 	 * routine for tracing's sake.
2735280Sroot 	 */
2744809Swnj 	case PRU_SLOWTIMO:
275*8272Sroot 		tcp_timers(tp, (int)nam);
276*8272Sroot 		req |= (int)nam << 8;		/* for debug's sake */
2774809Swnj 		break;
2784809Swnj 
2794731Swnj 	default:
2804731Swnj 		panic("tcp_usrreq");
2814567Swnj 	}
2825270Sroot 	if (tp && (so->so_options & SO_DEBUG))
2835270Sroot 		tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req);
2844567Swnj 	splx(s);
2854886Swnj 	return (error);
2864497Swnj }
2875245Sroot 
2885953Swnj int	tcp_sendspace = 1024*2;
2896601Ssam int	tcp_recvspace = 1024*2;
2905280Sroot /*
2915280Sroot  * Attach TCP protocol to socket, allocating
2925280Sroot  * internet protocol control block, tcp control block,
2935280Sroot  * bufer space, and entering LISTEN state if to accept connections.
2945280Sroot  */
295*8272Sroot tcp_attach(so)
2965280Sroot 	struct socket *so;
2975280Sroot {
2985280Sroot 	register struct tcpcb *tp;
2995280Sroot 	struct inpcb *inp;
3005280Sroot 	int error;
3015280Sroot 
3027511Sroot 	error = in_pcbreserve(so, tcp_sendspace, tcp_recvspace);
3035280Sroot 	if (error)
3047511Sroot 		goto bad;
3057511Sroot 	error = in_pcballoc(so, &tcb);
3067511Sroot 	if (error)
307*8272Sroot 		goto bad;
308*8272Sroot 	inp = sotoinpcb(so);
3095280Sroot 	tp = tcp_newtcpcb(inp);
3107511Sroot 	if (tp == 0) {
3117511Sroot 		error = ENOBUFS;
3127511Sroot 		goto bad2;
3137511Sroot 	}
314*8272Sroot 	tp->t_state = TCPS_CLOSED;
3155280Sroot 	return (0);
3167511Sroot bad2:
3177511Sroot 	in_pcbdetach(inp);
3187511Sroot bad:
3197511Sroot 	return (error);
3205280Sroot }
3215280Sroot 
3225280Sroot /*
3235280Sroot  * Initiate (or continue) disconnect.
3245280Sroot  * If embryonic state, just send reset (once).
3255280Sroot  * If not in ``let data drain'' option, just drop.
3265280Sroot  * Otherwise (hard), mark socket disconnecting and drop
3275280Sroot  * current input data; switch states based on user close, and
3285280Sroot  * send segment to peer (with FIN).
3295280Sroot  */
3305280Sroot tcp_disconnect(tp)
3315280Sroot 	struct tcpcb *tp;
3325280Sroot {
3335280Sroot 	struct socket *so = tp->t_inpcb->inp_socket;
3345280Sroot 
3355280Sroot 	if (tp->t_state < TCPS_ESTABLISHED)
3365280Sroot 		tcp_close(tp);
3375392Swnj 	else if (so->so_linger == 0)
3385280Sroot 		tcp_drop(tp, 0);
3395280Sroot 	else {
3405280Sroot 		soisdisconnecting(so);
3415280Sroot 		sbflush(&so->so_rcv);
3425280Sroot 		tcp_usrclosed(tp);
3435280Sroot 		(void) tcp_output(tp);
3445280Sroot 	}
3455280Sroot }
3465280Sroot 
3475280Sroot /*
3485280Sroot  * User issued close, and wish to trail through shutdown states:
3495280Sroot  * if never received SYN, just forget it.  If got a SYN from peer,
3505280Sroot  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
3515280Sroot  * If already got a FIN from peer, then almost done; go to LAST_ACK
3525280Sroot  * state.  In all other cases, have already sent FIN to peer (e.g.
3535280Sroot  * after PRU_SHUTDOWN), and just have to play tedious game waiting
3545280Sroot  * for peer to send FIN or not respond to keep-alives, etc.
3555897Swnj  * We can let the user exit from the close as soon as the FIN is acked.
3565280Sroot  */
3575245Sroot tcp_usrclosed(tp)
3585245Sroot 	struct tcpcb *tp;
3595245Sroot {
3605245Sroot 
3615245Sroot 	switch (tp->t_state) {
3625245Sroot 
3635245Sroot 	case TCPS_LISTEN:
3645245Sroot 	case TCPS_SYN_SENT:
3655245Sroot 		tp->t_state = TCPS_CLOSED;
3665245Sroot 		tcp_close(tp);
3675245Sroot 		break;
3685245Sroot 
3695245Sroot 	case TCPS_SYN_RECEIVED:
3705245Sroot 	case TCPS_ESTABLISHED:
3715245Sroot 		tp->t_state = TCPS_FIN_WAIT_1;
3725245Sroot 		break;
3735245Sroot 
3745245Sroot 	case TCPS_CLOSE_WAIT:
3755245Sroot 		tp->t_state = TCPS_LAST_ACK;
3765245Sroot 		break;
3775245Sroot 	}
3785897Swnj 	if (tp->t_state >= TCPS_FIN_WAIT_2)
3795897Swnj 		soisdisconnected(tp->t_inpcb->inp_socket);
3805245Sroot }
381