xref: /csrg-svn/sys/netinet/tcp_usrreq.c (revision 32789)
123196Smckusick /*
229155Smckusick  * Copyright (c) 1982, 1986 Regents of the University of California.
3*32789Sbostic  * All rights reserved.
423196Smckusick  *
5*32789Sbostic  * Redistribution and use in source and binary forms are permitted
6*32789Sbostic  * provided that this notice is preserved and that due credit is given
7*32789Sbostic  * to the University of California at Berkeley. The name of the University
8*32789Sbostic  * may not be used to endorse or promote products derived from this
9*32789Sbostic  * software without specific prior written permission. This software
10*32789Sbostic  * is provided ``as is'' without express or implied warranty.
11*32789Sbostic  *
12*32789Sbostic  *	@(#)tcp_usrreq.c	7.6 (Berkeley) 12/07/87
1323196Smckusick  */
144567Swnj 
1517064Sbloom #include "param.h"
1617064Sbloom #include "systm.h"
1717064Sbloom #include "mbuf.h"
1817064Sbloom #include "socket.h"
1917064Sbloom #include "socketvar.h"
2017064Sbloom #include "protosw.h"
2117064Sbloom #include "errno.h"
2217064Sbloom #include "stat.h"
238697Sroot 
248697Sroot #include "../net/if.h"
258697Sroot #include "../net/route.h"
2610896Ssam 
2717064Sbloom #include "in.h"
2817064Sbloom #include "in_pcb.h"
2917064Sbloom #include "in_systm.h"
3017064Sbloom #include "ip.h"
3117064Sbloom #include "ip_var.h"
3217064Sbloom #include "tcp.h"
3317064Sbloom #include "tcp_fsm.h"
3417064Sbloom #include "tcp_seq.h"
3517064Sbloom #include "tcp_timer.h"
3617064Sbloom #include "tcp_var.h"
3717064Sbloom #include "tcpip.h"
3817064Sbloom #include "tcp_debug.h"
394497Swnj 
405280Sroot /*
415280Sroot  * TCP protocol interface to socket abstraction.
425280Sroot  */
435280Sroot extern	char *tcpstates[];
444954Swnj struct	tcpcb *tcp_newtcpcb();
4512766Ssam int	tcpsenderrors;
465280Sroot 
474734Swnj /*
485280Sroot  * Process a TCP user request for TCP tb.  If this is a send request
494731Swnj  * then m is the mbuf chain of send data.  If this is a timer expiration
504731Swnj  * (called from the software clock routine), then timertype tells which timer.
514731Swnj  */
528601Sroot /*ARGSUSED*/
5312766Ssam tcp_usrreq(so, req, m, nam, rights)
544809Swnj 	struct socket *so;
554809Swnj 	int req;
5612766Ssam 	struct mbuf *m, *nam, *rights;
574497Swnj {
5830909Skarels 	register struct inpcb *inp;
594911Swnj 	register struct tcpcb *tp;
6030909Skarels 	int s;
614809Swnj 	int error = 0;
625270Sroot 	int ostate;
634497Swnj 
6430909Skarels 	if (req == PRU_CONTROL)
6530909Skarels 		return (in_control(so, (int)m, (caddr_t)nam,
6630909Skarels 			(struct ifnet *)rights));
6730909Skarels 	if (rights && rights->m_len)
6812766Ssam 		return (EINVAL);
6930909Skarels 
7030909Skarels 	s = splnet();
7130909Skarels 	inp = sotoinpcb(so);
724886Swnj 	/*
735280Sroot 	 * When a TCP is attached to a socket, then there will be
745280Sroot 	 * a (struct inpcb) pointed at by the socket, and this
755280Sroot 	 * structure will point at a subsidary (struct tcpcb).
764886Swnj 	 */
775089Swnj 	if (inp == 0 && req != PRU_ATTACH) {
785075Swnj 		splx(s);
795280Sroot 		return (EINVAL);		/* XXX */
805075Swnj 	}
815075Swnj 	if (inp) {
824911Swnj 		tp = intotcpcb(inp);
838272Sroot 		/* WHAT IF TP IS 0? */
844731Swnj #ifdef KPROF
855075Swnj 		tcp_acounts[tp->t_state][req]++;
864731Swnj #endif
875270Sroot 		ostate = tp->t_state;
887511Sroot 	} else
897511Sroot 		ostate = 0;
904809Swnj 	switch (req) {
914497Swnj 
925280Sroot 	/*
935280Sroot 	 * TCP attaches to socket via PRU_ATTACH, reserving space,
948272Sroot 	 * and an internet control block.
955280Sroot 	 */
964809Swnj 	case PRU_ATTACH:
974954Swnj 		if (inp) {
984809Swnj 			error = EISCONN;
994911Swnj 			break;
1004886Swnj 		}
1018640Sroot 		error = tcp_attach(so);
1025075Swnj 		if (error)
1034954Swnj 			break;
10410397Ssam 		if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1055392Swnj 			so->so_linger = TCP_LINGERTIME;
1065280Sroot 		tp = sototcpcb(so);
1074567Swnj 		break;
1084497Swnj 
1095280Sroot 	/*
1105280Sroot 	 * PRU_DETACH detaches the TCP protocol from the socket.
1115280Sroot 	 * If the protocol state is non-embryonic, then can't
1125280Sroot 	 * do this directly: have to initiate a PRU_DISCONNECT,
1135280Sroot 	 * which may finish later; embryonic TCB's can just
1145280Sroot 	 * be discarded here.
1155280Sroot 	 */
1164809Swnj 	case PRU_DETACH:
1175280Sroot 		if (tp->t_state > TCPS_LISTEN)
11810397Ssam 			tp = tcp_disconnect(tp);
11910397Ssam 		else
12010397Ssam 			tp = tcp_close(tp);
1214809Swnj 		break;
1224809Swnj 
1235280Sroot 	/*
1248272Sroot 	 * Give the socket an address.
1258272Sroot 	 */
1268272Sroot 	case PRU_BIND:
1278272Sroot 		error = in_pcbbind(inp, nam);
1288272Sroot 		if (error)
1298272Sroot 			break;
1308272Sroot 		break;
1318272Sroot 
1328272Sroot 	/*
1338272Sroot 	 * Prepare to accept connections.
1348272Sroot 	 */
1358272Sroot 	case PRU_LISTEN:
1368272Sroot 		if (inp->inp_lport == 0)
1378272Sroot 			error = in_pcbbind(inp, (struct mbuf *)0);
1388272Sroot 		if (error == 0)
1398272Sroot 			tp->t_state = TCPS_LISTEN;
1408272Sroot 		break;
1418272Sroot 
1428272Sroot 	/*
1435280Sroot 	 * Initiate connection to peer.
1445280Sroot 	 * Create a template for use in transmissions on this connection.
1455280Sroot 	 * Enter SYN_SENT state, and mark socket as connecting.
1465280Sroot 	 * Start keep-alive timer, and seed output sequence space.
1475280Sroot 	 * Send initial segment on connection.
1485280Sroot 	 */
1494809Swnj 	case PRU_CONNECT:
1508272Sroot 		if (inp->inp_lport == 0) {
1518272Sroot 			error = in_pcbbind(inp, (struct mbuf *)0);
1528272Sroot 			if (error)
1538272Sroot 				break;
1548272Sroot 		}
1558272Sroot 		error = in_pcbconnect(inp, nam);
1564954Swnj 		if (error)
1574886Swnj 			break;
1585174Swnj 		tp->t_template = tcp_template(tp);
1595280Sroot 		if (tp->t_template == 0) {
1605280Sroot 			in_pcbdisconnect(inp);
1615280Sroot 			error = ENOBUFS;
1625280Sroot 			break;
1635280Sroot 		}
1644886Swnj 		soisconnecting(so);
16530527Skarels 		tcpstat.tcps_connattempt++;
1665075Swnj 		tp->t_state = TCPS_SYN_SENT;
1675245Sroot 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
1685245Sroot 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
1695245Sroot 		tcp_sendseqinit(tp);
1706506Ssam 		error = tcp_output(tp);
1714567Swnj 		break;
1724497Swnj 
1735280Sroot 	/*
17413117Ssam 	 * Create a TCP connection between two sockets.
17513117Ssam 	 */
17613117Ssam 	case PRU_CONNECT2:
17713117Ssam 		error = EOPNOTSUPP;
17813117Ssam 		break;
17913117Ssam 
18013117Ssam 	/*
1815280Sroot 	 * Initiate disconnect from peer.
1825280Sroot 	 * If connection never passed embryonic stage, just drop;
1835280Sroot 	 * else if don't need to let data drain, then can just drop anyways,
1845280Sroot 	 * else have to begin TCP shutdown process: mark socket disconnecting,
1855280Sroot 	 * drain unread data, state switch to reflect user close, and
1865280Sroot 	 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
1875280Sroot 	 * when peer sends FIN and acks ours.
1885280Sroot 	 *
1895280Sroot 	 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
1905280Sroot 	 */
1915280Sroot 	case PRU_DISCONNECT:
19210397Ssam 		tp = tcp_disconnect(tp);
1935245Sroot 		break;
1945245Sroot 
1955280Sroot 	/*
1965280Sroot 	 * Accept a connection.  Essentially all the work is
1975280Sroot 	 * done at higher levels; just return the address
1985280Sroot 	 * of the peer, storing through addr.
1995280Sroot 	 */
2006117Swnj 	case PRU_ACCEPT: {
2018272Sroot 		struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
2026117Swnj 
2038272Sroot 		nam->m_len = sizeof (struct sockaddr_in);
2048272Sroot 		sin->sin_family = AF_INET;
2058272Sroot 		sin->sin_port = inp->inp_fport;
2068272Sroot 		sin->sin_addr = inp->inp_faddr;
2078272Sroot 		break;
2086117Swnj 		}
2094925Swnj 
2105280Sroot 	/*
2115280Sroot 	 * Mark the connection as being incapable of further output.
2125280Sroot 	 */
2134809Swnj 	case PRU_SHUTDOWN:
2145089Swnj 		socantsendmore(so);
21510397Ssam 		tp = tcp_usrclosed(tp);
21610397Ssam 		if (tp)
21710397Ssam 			error = tcp_output(tp);
2184567Swnj 		break;
2194497Swnj 
2205280Sroot 	/*
2215280Sroot 	 * After a receive, possibly send window update to peer.
2225280Sroot 	 */
2234809Swnj 	case PRU_RCVD:
2245113Swnj 		(void) tcp_output(tp);
2254567Swnj 		break;
2264497Swnj 
2275280Sroot 	/*
2285280Sroot 	 * Do a send by putting data in output queue and updating urgent
2295280Sroot 	 * marker if URG set.  Possibly send more data.
2305280Sroot 	 */
2314809Swnj 	case PRU_SEND:
2325075Swnj 		sbappend(&so->so_snd, m);
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;
25030871Smckusick 		(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 		}
35231041Ssam 		if (m)
35331041Ssam 			(void) m_free(m);
35425896Skarels 		break;
35525896Skarels 
35625896Skarels 	case PRCO_GETOPT:
35725896Skarels 		*mp = m = m_get(M_WAIT, MT_SOOPTS);
35825896Skarels 		m->m_len = sizeof(int);
35925896Skarels 
36025896Skarels 		switch (optname) {
36125896Skarels 		case TCP_NODELAY:
36225896Skarels 			*mtod(m, int *) = tp->t_flags & TF_NODELAY;
36325896Skarels 			break;
36425896Skarels 		case TCP_MAXSEG:
36525896Skarels 			*mtod(m, int *) = tp->t_maxseg;
36625896Skarels 			break;
36725896Skarels 		default:
36825896Skarels 			error = EINVAL;
36925896Skarels 			break;
37025896Skarels 		}
37125896Skarels 		break;
37225896Skarels 	}
37325896Skarels 	return (error);
37424821Skarels }
37524821Skarels 
37618367Skarels int	tcp_sendspace = 1024*4;
37718367Skarels int	tcp_recvspace = 1024*4;
3785280Sroot /*
3795280Sroot  * Attach TCP protocol to socket, allocating
3805280Sroot  * internet protocol control block, tcp control block,
3815280Sroot  * bufer space, and entering LISTEN state if to accept connections.
3825280Sroot  */
3838272Sroot tcp_attach(so)
3845280Sroot 	struct socket *so;
3855280Sroot {
3865280Sroot 	register struct tcpcb *tp;
3875280Sroot 	struct inpcb *inp;
3885280Sroot 	int error;
3895280Sroot 
3909031Sroot 	error = soreserve(so, tcp_sendspace, tcp_recvspace);
3915280Sroot 	if (error)
39217047Skarels 		return (error);
3937511Sroot 	error = in_pcballoc(so, &tcb);
3947511Sroot 	if (error)
39517047Skarels 		return (error);
3968272Sroot 	inp = sotoinpcb(so);
3975280Sroot 	tp = tcp_newtcpcb(inp);
3987511Sroot 	if (tp == 0) {
39917047Skarels 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
40017047Skarels 
40117047Skarels 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
40217047Skarels 		in_pcbdetach(inp);
40317047Skarels 		so->so_state |= nofd;
40417047Skarels 		return (ENOBUFS);
4057511Sroot 	}
4068272Sroot 	tp->t_state = TCPS_CLOSED;
4075280Sroot 	return (0);
4085280Sroot }
4095280Sroot 
4105280Sroot /*
4115280Sroot  * Initiate (or continue) disconnect.
4125280Sroot  * If embryonic state, just send reset (once).
41313221Ssam  * If in ``let data drain'' option and linger null, just drop.
4145280Sroot  * Otherwise (hard), mark socket disconnecting and drop
4155280Sroot  * current input data; switch states based on user close, and
4165280Sroot  * send segment to peer (with FIN).
4175280Sroot  */
41810397Ssam struct tcpcb *
4195280Sroot tcp_disconnect(tp)
42010397Ssam 	register struct tcpcb *tp;
4215280Sroot {
4225280Sroot 	struct socket *so = tp->t_inpcb->inp_socket;
4235280Sroot 
4245280Sroot 	if (tp->t_state < TCPS_ESTABLISHED)
42510397Ssam 		tp = tcp_close(tp);
42613221Ssam 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
42710397Ssam 		tp = tcp_drop(tp, 0);
4285280Sroot 	else {
4295280Sroot 		soisdisconnecting(so);
4305280Sroot 		sbflush(&so->so_rcv);
43110397Ssam 		tp = tcp_usrclosed(tp);
43210397Ssam 		if (tp)
43310397Ssam 			(void) tcp_output(tp);
4345280Sroot 	}
43510397Ssam 	return (tp);
4365280Sroot }
4375280Sroot 
4385280Sroot /*
4395280Sroot  * User issued close, and wish to trail through shutdown states:
4405280Sroot  * if never received SYN, just forget it.  If got a SYN from peer,
4415280Sroot  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
4425280Sroot  * If already got a FIN from peer, then almost done; go to LAST_ACK
4435280Sroot  * state.  In all other cases, have already sent FIN to peer (e.g.
4445280Sroot  * after PRU_SHUTDOWN), and just have to play tedious game waiting
4455280Sroot  * for peer to send FIN or not respond to keep-alives, etc.
4465897Swnj  * We can let the user exit from the close as soon as the FIN is acked.
4475280Sroot  */
44810397Ssam struct tcpcb *
4495245Sroot tcp_usrclosed(tp)
45010397Ssam 	register struct tcpcb *tp;
4515245Sroot {
4525245Sroot 
4535245Sroot 	switch (tp->t_state) {
4545245Sroot 
45512438Ssam 	case TCPS_CLOSED:
4565245Sroot 	case TCPS_LISTEN:
4575245Sroot 	case TCPS_SYN_SENT:
4585245Sroot 		tp->t_state = TCPS_CLOSED;
45910397Ssam 		tp = tcp_close(tp);
4605245Sroot 		break;
4615245Sroot 
4625245Sroot 	case TCPS_SYN_RECEIVED:
4635245Sroot 	case TCPS_ESTABLISHED:
4645245Sroot 		tp->t_state = TCPS_FIN_WAIT_1;
4655245Sroot 		break;
4665245Sroot 
4675245Sroot 	case TCPS_CLOSE_WAIT:
4685245Sroot 		tp->t_state = TCPS_LAST_ACK;
4695245Sroot 		break;
4705245Sroot 	}
47110397Ssam 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2)
4725897Swnj 		soisdisconnected(tp->t_inpcb->inp_socket);
47310397Ssam 	return (tp);
4745245Sroot }
475