xref: /csrg-svn/sys/netinet/tcp_usrreq.c (revision 30871)
123196Smckusick /*
229155Smckusick  * Copyright (c) 1982, 1986 Regents of the University of California.
323196Smckusick  * All rights reserved.  The Berkeley software License Agreement
423196Smckusick  * specifies the terms and conditions for redistribution.
523196Smckusick  *
6*30871Smckusick  *	@(#)tcp_usrreq.c	7.3 (Berkeley) 04/09/87
723196Smckusick  */
84567Swnj 
917064Sbloom #include "param.h"
1017064Sbloom #include "systm.h"
1117064Sbloom #include "mbuf.h"
1217064Sbloom #include "socket.h"
1317064Sbloom #include "socketvar.h"
1417064Sbloom #include "protosw.h"
1517064Sbloom #include "errno.h"
1617064Sbloom #include "stat.h"
178697Sroot 
188697Sroot #include "../net/if.h"
198697Sroot #include "../net/route.h"
2010896Ssam 
2117064Sbloom #include "in.h"
2217064Sbloom #include "in_pcb.h"
2317064Sbloom #include "in_systm.h"
2417064Sbloom #include "ip.h"
2517064Sbloom #include "ip_var.h"
2617064Sbloom #include "tcp.h"
2717064Sbloom #include "tcp_fsm.h"
2817064Sbloom #include "tcp_seq.h"
2917064Sbloom #include "tcp_timer.h"
3017064Sbloom #include "tcp_var.h"
3117064Sbloom #include "tcpip.h"
3217064Sbloom #include "tcp_debug.h"
334497Swnj 
345280Sroot /*
355280Sroot  * TCP protocol interface to socket abstraction.
365280Sroot  */
375280Sroot extern	char *tcpstates[];
384954Swnj struct	tcpcb *tcp_newtcpcb();
3912766Ssam int	tcpsenderrors;
405280Sroot 
414734Swnj /*
425280Sroot  * Process a TCP user request for TCP tb.  If this is a send request
434731Swnj  * then m is the mbuf chain of send data.  If this is a timer expiration
444731Swnj  * (called from the software clock routine), then timertype tells which timer.
454731Swnj  */
468601Sroot /*ARGSUSED*/
4712766Ssam tcp_usrreq(so, req, m, nam, rights)
484809Swnj 	struct socket *so;
494809Swnj 	int req;
5012766Ssam 	struct mbuf *m, *nam, *rights;
514497Swnj {
524886Swnj 	register struct inpcb *inp = sotoinpcb(so);
534911Swnj 	register struct tcpcb *tp;
544567Swnj 	int s = splnet();
554809Swnj 	int error = 0;
565270Sroot 	int ostate;
574497Swnj 
58*30871Smckusick 	if (req == PRU_CONTROL) {
59*30871Smckusick 		error = in_control(so, (int)m, (caddr_t)nam,
60*30871Smckusick 			(struct ifnet *)rights);
61*30871Smckusick 		(void) splx(s);
62*30871Smckusick 		return(error);
63*30871Smckusick 	}
6412766Ssam 	if (rights && rights->m_len) {
6512766Ssam 		splx(s);
6612766Ssam 		return (EINVAL);
6712766Ssam 	}
684886Swnj 	/*
695280Sroot 	 * When a TCP is attached to a socket, then there will be
705280Sroot 	 * a (struct inpcb) pointed at by the socket, and this
715280Sroot 	 * structure will point at a subsidary (struct tcpcb).
724886Swnj 	 */
735089Swnj 	if (inp == 0 && req != PRU_ATTACH) {
745075Swnj 		splx(s);
755280Sroot 		return (EINVAL);		/* XXX */
765075Swnj 	}
775075Swnj 	if (inp) {
784911Swnj 		tp = intotcpcb(inp);
798272Sroot 		/* WHAT IF TP IS 0? */
804731Swnj #ifdef KPROF
815075Swnj 		tcp_acounts[tp->t_state][req]++;
824731Swnj #endif
835270Sroot 		ostate = tp->t_state;
847511Sroot 	} else
857511Sroot 		ostate = 0;
864809Swnj 	switch (req) {
874497Swnj 
885280Sroot 	/*
895280Sroot 	 * TCP attaches to socket via PRU_ATTACH, reserving space,
908272Sroot 	 * and an internet control block.
915280Sroot 	 */
924809Swnj 	case PRU_ATTACH:
934954Swnj 		if (inp) {
944809Swnj 			error = EISCONN;
954911Swnj 			break;
964886Swnj 		}
978640Sroot 		error = tcp_attach(so);
985075Swnj 		if (error)
994954Swnj 			break;
10010397Ssam 		if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1015392Swnj 			so->so_linger = TCP_LINGERTIME;
1025280Sroot 		tp = sototcpcb(so);
1034567Swnj 		break;
1044497Swnj 
1055280Sroot 	/*
1065280Sroot 	 * PRU_DETACH detaches the TCP protocol from the socket.
1075280Sroot 	 * If the protocol state is non-embryonic, then can't
1085280Sroot 	 * do this directly: have to initiate a PRU_DISCONNECT,
1095280Sroot 	 * which may finish later; embryonic TCB's can just
1105280Sroot 	 * be discarded here.
1115280Sroot 	 */
1124809Swnj 	case PRU_DETACH:
1135280Sroot 		if (tp->t_state > TCPS_LISTEN)
11410397Ssam 			tp = tcp_disconnect(tp);
11510397Ssam 		else
11610397Ssam 			tp = tcp_close(tp);
1174809Swnj 		break;
1184809Swnj 
1195280Sroot 	/*
1208272Sroot 	 * Give the socket an address.
1218272Sroot 	 */
1228272Sroot 	case PRU_BIND:
1238272Sroot 		error = in_pcbbind(inp, nam);
1248272Sroot 		if (error)
1258272Sroot 			break;
1268272Sroot 		break;
1278272Sroot 
1288272Sroot 	/*
1298272Sroot 	 * Prepare to accept connections.
1308272Sroot 	 */
1318272Sroot 	case PRU_LISTEN:
1328272Sroot 		if (inp->inp_lport == 0)
1338272Sroot 			error = in_pcbbind(inp, (struct mbuf *)0);
1348272Sroot 		if (error == 0)
1358272Sroot 			tp->t_state = TCPS_LISTEN;
1368272Sroot 		break;
1378272Sroot 
1388272Sroot 	/*
1395280Sroot 	 * Initiate connection to peer.
1405280Sroot 	 * Create a template for use in transmissions on this connection.
1415280Sroot 	 * Enter SYN_SENT state, and mark socket as connecting.
1425280Sroot 	 * Start keep-alive timer, and seed output sequence space.
1435280Sroot 	 * Send initial segment on connection.
1445280Sroot 	 */
1454809Swnj 	case PRU_CONNECT:
1468272Sroot 		if (inp->inp_lport == 0) {
1478272Sroot 			error = in_pcbbind(inp, (struct mbuf *)0);
1488272Sroot 			if (error)
1498272Sroot 				break;
1508272Sroot 		}
1518272Sroot 		error = in_pcbconnect(inp, nam);
1524954Swnj 		if (error)
1534886Swnj 			break;
1545174Swnj 		tp->t_template = tcp_template(tp);
1555280Sroot 		if (tp->t_template == 0) {
1565280Sroot 			in_pcbdisconnect(inp);
1575280Sroot 			error = ENOBUFS;
1585280Sroot 			break;
1595280Sroot 		}
1604886Swnj 		soisconnecting(so);
16130527Skarels 		tcpstat.tcps_connattempt++;
1625075Swnj 		tp->t_state = TCPS_SYN_SENT;
1635245Sroot 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
1645245Sroot 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
1655245Sroot 		tcp_sendseqinit(tp);
1666506Ssam 		error = tcp_output(tp);
1674567Swnj 		break;
1684497Swnj 
1695280Sroot 	/*
17013117Ssam 	 * Create a TCP connection between two sockets.
17113117Ssam 	 */
17213117Ssam 	case PRU_CONNECT2:
17313117Ssam 		error = EOPNOTSUPP;
17413117Ssam 		break;
17513117Ssam 
17613117Ssam 	/*
1775280Sroot 	 * Initiate disconnect from peer.
1785280Sroot 	 * If connection never passed embryonic stage, just drop;
1795280Sroot 	 * else if don't need to let data drain, then can just drop anyways,
1805280Sroot 	 * else have to begin TCP shutdown process: mark socket disconnecting,
1815280Sroot 	 * drain unread data, state switch to reflect user close, and
1825280Sroot 	 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
1835280Sroot 	 * when peer sends FIN and acks ours.
1845280Sroot 	 *
1855280Sroot 	 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
1865280Sroot 	 */
1875280Sroot 	case PRU_DISCONNECT:
18810397Ssam 		tp = tcp_disconnect(tp);
1895245Sroot 		break;
1905245Sroot 
1915280Sroot 	/*
1925280Sroot 	 * Accept a connection.  Essentially all the work is
1935280Sroot 	 * done at higher levels; just return the address
1945280Sroot 	 * of the peer, storing through addr.
1955280Sroot 	 */
1966117Swnj 	case PRU_ACCEPT: {
1978272Sroot 		struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
1986117Swnj 
1998272Sroot 		nam->m_len = sizeof (struct sockaddr_in);
2008272Sroot 		sin->sin_family = AF_INET;
2018272Sroot 		sin->sin_port = inp->inp_fport;
2028272Sroot 		sin->sin_addr = inp->inp_faddr;
2038272Sroot 		break;
2046117Swnj 		}
2054925Swnj 
2065280Sroot 	/*
2075280Sroot 	 * Mark the connection as being incapable of further output.
2085280Sroot 	 */
2094809Swnj 	case PRU_SHUTDOWN:
2105089Swnj 		socantsendmore(so);
21110397Ssam 		tp = tcp_usrclosed(tp);
21210397Ssam 		if (tp)
21310397Ssam 			error = tcp_output(tp);
2144567Swnj 		break;
2154497Swnj 
2165280Sroot 	/*
2175280Sroot 	 * After a receive, possibly send window update to peer.
2185280Sroot 	 */
2194809Swnj 	case PRU_RCVD:
2205113Swnj 		(void) tcp_output(tp);
2214567Swnj 		break;
2224497Swnj 
2235280Sroot 	/*
2245280Sroot 	 * Do a send by putting data in output queue and updating urgent
2255280Sroot 	 * marker if URG set.  Possibly send more data.
2265280Sroot 	 */
2274809Swnj 	case PRU_SEND:
2285075Swnj 		sbappend(&so->so_snd, m);
2296506Ssam #ifdef notdef
2305089Swnj 		if (tp->t_flags & TF_PUSH)
2315075Swnj 			tp->snd_end = tp->snd_una + so->so_snd.sb_cc;
2326506Ssam #endif
2336506Ssam 		error = tcp_output(tp);
23412766Ssam 		if (error) {		/* XXX fix to use other path */
23512766Ssam 			if (error == ENOBUFS)		/* XXX */
23612766Ssam 				error = 0;		/* XXX */
23712766Ssam 			tcpsenderrors++;
23812766Ssam 		}
2394567Swnj 		break;
2404567Swnj 
2415280Sroot 	/*
2425280Sroot 	 * Abort the TCP.
2435280Sroot 	 */
2444809Swnj 	case PRU_ABORT:
24510397Ssam 		tp = tcp_drop(tp, ECONNABORTED);
2464567Swnj 		break;
2474567Swnj 
2485113Swnj 	case PRU_SENSE:
24916989Skarels 		((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
250*30871Smckusick 		(void) splx(s);
25116989Skarels 		return (0);
2525113Swnj 
2535113Swnj 	case PRU_RCVOOB:
25424821Skarels 		if ((so->so_oobmark == 0 &&
25524821Skarels 		    (so->so_state & SS_RCVATMARK) == 0) ||
25627195Skarels 		    so->so_options & SO_OOBINLINE ||
25724821Skarels 		    tp->t_oobflags & TCPOOB_HADDATA) {
2585417Swnj 			error = EINVAL;
2595417Swnj 			break;
2605417Swnj 		}
2615549Swnj 		if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
2625442Swnj 			error = EWOULDBLOCK;
2635549Swnj 			break;
2645442Swnj 		}
2658310Sroot 		m->m_len = 1;
2665549Swnj 		*mtod(m, caddr_t) = tp->t_iobc;
26724821Skarels 		if (((int)nam & MSG_PEEK) == 0)
26824821Skarels 			tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2695113Swnj 		break;
2705113Swnj 
2715113Swnj 	case PRU_SENDOOB:
2725442Swnj 		if (sbspace(&so->so_snd) < -512) {
27311229Ssam 			m_freem(m);
2745442Swnj 			error = ENOBUFS;
2755442Swnj 			break;
2765442Swnj 		}
27727195Skarels 		/*
27827195Skarels 		 * According to RFC961 (Assigned Protocols),
27927195Skarels 		 * the urgent pointer points to the last octet
28027195Skarels 		 * of urgent data.  We continue, however,
28127195Skarels 		 * to consider it to indicate the first octet
28227195Skarels 		 * of data past the urgent section.
28327195Skarels 		 * Otherwise, snd_up should be one lower.
28427195Skarels 		 */
2855417Swnj 		sbappend(&so->so_snd, m);
28627195Skarels 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
2875549Swnj 		tp->t_force = 1;
2886506Ssam 		error = tcp_output(tp);
2895549Swnj 		tp->t_force = 0;
2905113Swnj 		break;
2915113Swnj 
2926510Ssam 	case PRU_SOCKADDR:
2938272Sroot 		in_setsockaddr(inp, nam);
2946510Ssam 		break;
2956510Ssam 
29614123Ssam 	case PRU_PEERADDR:
29714123Ssam 		in_setpeeraddr(inp, nam);
29814123Ssam 		break;
29914123Ssam 
3005280Sroot 	/*
3015280Sroot 	 * TCP slow timer went off; going through this
3025280Sroot 	 * routine for tracing's sake.
3035280Sroot 	 */
3044809Swnj 	case PRU_SLOWTIMO:
30510397Ssam 		tp = tcp_timers(tp, (int)nam);
3068272Sroot 		req |= (int)nam << 8;		/* for debug's sake */
3074809Swnj 		break;
3084809Swnj 
3094731Swnj 	default:
3104731Swnj 		panic("tcp_usrreq");
3114567Swnj 	}
3125270Sroot 	if (tp && (so->so_options & SO_DEBUG))
3135270Sroot 		tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req);
3144567Swnj 	splx(s);
3154886Swnj 	return (error);
3164497Swnj }
3175245Sroot 
31825896Skarels tcp_ctloutput(op, so, level, optname, mp)
31924821Skarels 	int op;
32024821Skarels 	struct socket *so;
32124821Skarels 	int level, optname;
32225896Skarels 	struct mbuf **mp;
32324821Skarels {
32425896Skarels 	int error = 0;
32525896Skarels 	struct inpcb *inp = sotoinpcb(so);
32625896Skarels 	register struct tcpcb *tp = intotcpcb(inp);
32725896Skarels 	register struct mbuf *m;
32825896Skarels 
32924821Skarels 	if (level != IPPROTO_TCP)
33026248Skarels 		return (ip_ctloutput(op, so, level, optname, mp));
33125896Skarels 
33225896Skarels 	switch (op) {
33325896Skarels 
33425896Skarels 	case PRCO_SETOPT:
33525896Skarels 		m = *mp;
33625896Skarels 		switch (optname) {
33725896Skarels 
33825896Skarels 		case TCP_NODELAY:
33925896Skarels 			if (m == NULL || m->m_len < sizeof (int))
34025896Skarels 				error = EINVAL;
34125896Skarels 			else if (*mtod(m, int *))
34225896Skarels 				tp->t_flags |= TF_NODELAY;
34325896Skarels 			else
34425896Skarels 				tp->t_flags &= ~TF_NODELAY;
34525896Skarels 			break;
34625896Skarels 
34725896Skarels 		case TCP_MAXSEG:	/* not yet */
34825896Skarels 		default:
34925896Skarels 			error = EINVAL;
35025896Skarels 			break;
35125896Skarels 		}
35226388Skarels 		(void)m_free(m);
35325896Skarels 		break;
35425896Skarels 
35525896Skarels 	case PRCO_GETOPT:
35625896Skarels 		*mp = m = m_get(M_WAIT, MT_SOOPTS);
35725896Skarels 		m->m_len = sizeof(int);
35825896Skarels 
35925896Skarels 		switch (optname) {
36025896Skarels 		case TCP_NODELAY:
36125896Skarels 			*mtod(m, int *) = tp->t_flags & TF_NODELAY;
36225896Skarels 			break;
36325896Skarels 		case TCP_MAXSEG:
36425896Skarels 			*mtod(m, int *) = tp->t_maxseg;
36525896Skarels 			break;
36625896Skarels 		default:
36725896Skarels 			error = EINVAL;
36825896Skarels 			break;
36925896Skarels 		}
37025896Skarels 		break;
37125896Skarels 	}
37225896Skarels 	return (error);
37324821Skarels }
37424821Skarels 
37518367Skarels int	tcp_sendspace = 1024*4;
37618367Skarels int	tcp_recvspace = 1024*4;
3775280Sroot /*
3785280Sroot  * Attach TCP protocol to socket, allocating
3795280Sroot  * internet protocol control block, tcp control block,
3805280Sroot  * bufer space, and entering LISTEN state if to accept connections.
3815280Sroot  */
3828272Sroot tcp_attach(so)
3835280Sroot 	struct socket *so;
3845280Sroot {
3855280Sroot 	register struct tcpcb *tp;
3865280Sroot 	struct inpcb *inp;
3875280Sroot 	int error;
3885280Sroot 
3899031Sroot 	error = soreserve(so, tcp_sendspace, tcp_recvspace);
3905280Sroot 	if (error)
39117047Skarels 		return (error);
3927511Sroot 	error = in_pcballoc(so, &tcb);
3937511Sroot 	if (error)
39417047Skarels 		return (error);
3958272Sroot 	inp = sotoinpcb(so);
3965280Sroot 	tp = tcp_newtcpcb(inp);
3977511Sroot 	if (tp == 0) {
39817047Skarels 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
39917047Skarels 
40017047Skarels 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
40117047Skarels 		in_pcbdetach(inp);
40217047Skarels 		so->so_state |= nofd;
40317047Skarels 		return (ENOBUFS);
4047511Sroot 	}
4058272Sroot 	tp->t_state = TCPS_CLOSED;
4065280Sroot 	return (0);
4075280Sroot }
4085280Sroot 
4095280Sroot /*
4105280Sroot  * Initiate (or continue) disconnect.
4115280Sroot  * If embryonic state, just send reset (once).
41213221Ssam  * If in ``let data drain'' option and linger null, just drop.
4135280Sroot  * Otherwise (hard), mark socket disconnecting and drop
4145280Sroot  * current input data; switch states based on user close, and
4155280Sroot  * send segment to peer (with FIN).
4165280Sroot  */
41710397Ssam struct tcpcb *
4185280Sroot tcp_disconnect(tp)
41910397Ssam 	register struct tcpcb *tp;
4205280Sroot {
4215280Sroot 	struct socket *so = tp->t_inpcb->inp_socket;
4225280Sroot 
4235280Sroot 	if (tp->t_state < TCPS_ESTABLISHED)
42410397Ssam 		tp = tcp_close(tp);
42513221Ssam 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
42610397Ssam 		tp = tcp_drop(tp, 0);
4275280Sroot 	else {
4285280Sroot 		soisdisconnecting(so);
4295280Sroot 		sbflush(&so->so_rcv);
43010397Ssam 		tp = tcp_usrclosed(tp);
43110397Ssam 		if (tp)
43210397Ssam 			(void) tcp_output(tp);
4335280Sroot 	}
43410397Ssam 	return (tp);
4355280Sroot }
4365280Sroot 
4375280Sroot /*
4385280Sroot  * User issued close, and wish to trail through shutdown states:
4395280Sroot  * if never received SYN, just forget it.  If got a SYN from peer,
4405280Sroot  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
4415280Sroot  * If already got a FIN from peer, then almost done; go to LAST_ACK
4425280Sroot  * state.  In all other cases, have already sent FIN to peer (e.g.
4435280Sroot  * after PRU_SHUTDOWN), and just have to play tedious game waiting
4445280Sroot  * for peer to send FIN or not respond to keep-alives, etc.
4455897Swnj  * We can let the user exit from the close as soon as the FIN is acked.
4465280Sroot  */
44710397Ssam struct tcpcb *
4485245Sroot tcp_usrclosed(tp)
44910397Ssam 	register struct tcpcb *tp;
4505245Sroot {
4515245Sroot 
4525245Sroot 	switch (tp->t_state) {
4535245Sroot 
45412438Ssam 	case TCPS_CLOSED:
4555245Sroot 	case TCPS_LISTEN:
4565245Sroot 	case TCPS_SYN_SENT:
4575245Sroot 		tp->t_state = TCPS_CLOSED;
45810397Ssam 		tp = tcp_close(tp);
4595245Sroot 		break;
4605245Sroot 
4615245Sroot 	case TCPS_SYN_RECEIVED:
4625245Sroot 	case TCPS_ESTABLISHED:
4635245Sroot 		tp->t_state = TCPS_FIN_WAIT_1;
4645245Sroot 		break;
4655245Sroot 
4665245Sroot 	case TCPS_CLOSE_WAIT:
4675245Sroot 		tp->t_state = TCPS_LAST_ACK;
4685245Sroot 		break;
4695245Sroot 	}
47010397Ssam 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2)
4715897Swnj 		soisdisconnected(tp->t_inpcb->inp_socket);
47210397Ssam 	return (tp);
4735245Sroot }
474