xref: /csrg-svn/sys/netinet/tcp_usrreq.c (revision 42184)
123196Smckusick /*
237323Skarels  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
332789Sbostic  * All rights reserved.
423196Smckusick  *
532789Sbostic  * Redistribution and use in source and binary forms are permitted
634855Sbostic  * provided that the above copyright notice and this paragraph are
734855Sbostic  * duplicated in all such forms and that any documentation,
834855Sbostic  * advertising materials, and other materials related to such
934855Sbostic  * distribution and use acknowledge that the software was developed
1034855Sbostic  * by the University of California, Berkeley.  The name of the
1134855Sbostic  * University may not be used to endorse or promote products derived
1234855Sbostic  * from this software without specific prior written permission.
1334855Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434855Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534855Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1632789Sbostic  *
17*42184Skarels  *	@(#)tcp_usrreq.c	7.14 (Berkeley) 05/17/90
1823196Smckusick  */
194567Swnj 
2017064Sbloom #include "param.h"
2117064Sbloom #include "systm.h"
2237323Skarels #include "malloc.h"
2317064Sbloom #include "mbuf.h"
2417064Sbloom #include "socket.h"
2517064Sbloom #include "socketvar.h"
2617064Sbloom #include "protosw.h"
2717064Sbloom #include "errno.h"
2817064Sbloom #include "stat.h"
298697Sroot 
308697Sroot #include "../net/if.h"
318697Sroot #include "../net/route.h"
3210896Ssam 
3317064Sbloom #include "in.h"
3417064Sbloom #include "in_systm.h"
3517064Sbloom #include "ip.h"
3640690Skarels #include "in_pcb.h"
3717064Sbloom #include "ip_var.h"
3817064Sbloom #include "tcp.h"
3917064Sbloom #include "tcp_fsm.h"
4017064Sbloom #include "tcp_seq.h"
4117064Sbloom #include "tcp_timer.h"
4217064Sbloom #include "tcp_var.h"
4317064Sbloom #include "tcpip.h"
4417064Sbloom #include "tcp_debug.h"
454497Swnj 
465280Sroot /*
475280Sroot  * TCP protocol interface to socket abstraction.
485280Sroot  */
495280Sroot extern	char *tcpstates[];
504954Swnj struct	tcpcb *tcp_newtcpcb();
515280Sroot 
524734Swnj /*
535280Sroot  * Process a TCP user request for TCP tb.  If this is a send request
544731Swnj  * then m is the mbuf chain of send data.  If this is a timer expiration
554731Swnj  * (called from the software clock routine), then timertype tells which timer.
564731Swnj  */
578601Sroot /*ARGSUSED*/
58*42184Skarels tcp_usrreq(so, req, m, nam, control)
594809Swnj 	struct socket *so;
604809Swnj 	int req;
61*42184Skarels 	struct mbuf *m, *nam, *control;
624497Swnj {
6330909Skarels 	register struct inpcb *inp;
644911Swnj 	register struct tcpcb *tp;
6530909Skarels 	int s;
664809Swnj 	int error = 0;
675270Sroot 	int ostate;
684497Swnj 
6930909Skarels 	if (req == PRU_CONTROL)
7030909Skarels 		return (in_control(so, (int)m, (caddr_t)nam,
71*42184Skarels 			(struct ifnet *)control));
72*42184Skarels 	if (control && control->m_len) {
73*42184Skarels 		m_freem(control);
74*42184Skarels 		if (m)
75*42184Skarels 			m_freem(m);
7612766Ssam 		return (EINVAL);
77*42184Skarels 	}
7830909Skarels 
7930909Skarels 	s = splnet();
8030909Skarels 	inp = sotoinpcb(so);
814886Swnj 	/*
825280Sroot 	 * When a TCP is attached to a socket, then there will be
835280Sroot 	 * a (struct inpcb) pointed at by the socket, and this
845280Sroot 	 * structure will point at a subsidary (struct tcpcb).
854886Swnj 	 */
865089Swnj 	if (inp == 0 && req != PRU_ATTACH) {
875075Swnj 		splx(s);
885280Sroot 		return (EINVAL);		/* XXX */
895075Swnj 	}
905075Swnj 	if (inp) {
914911Swnj 		tp = intotcpcb(inp);
928272Sroot 		/* WHAT IF TP IS 0? */
934731Swnj #ifdef KPROF
945075Swnj 		tcp_acounts[tp->t_state][req]++;
954731Swnj #endif
965270Sroot 		ostate = tp->t_state;
977511Sroot 	} else
987511Sroot 		ostate = 0;
994809Swnj 	switch (req) {
1004497Swnj 
1015280Sroot 	/*
1025280Sroot 	 * TCP attaches to socket via PRU_ATTACH, reserving space,
1038272Sroot 	 * and an internet control block.
1045280Sroot 	 */
1054809Swnj 	case PRU_ATTACH:
1064954Swnj 		if (inp) {
1074809Swnj 			error = EISCONN;
1084911Swnj 			break;
1094886Swnj 		}
1108640Sroot 		error = tcp_attach(so);
1115075Swnj 		if (error)
1124954Swnj 			break;
11310397Ssam 		if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1145392Swnj 			so->so_linger = TCP_LINGERTIME;
1155280Sroot 		tp = sototcpcb(so);
1164567Swnj 		break;
1174497Swnj 
1185280Sroot 	/*
1195280Sroot 	 * PRU_DETACH detaches the TCP protocol from the socket.
1205280Sroot 	 * If the protocol state is non-embryonic, then can't
1215280Sroot 	 * do this directly: have to initiate a PRU_DISCONNECT,
1225280Sroot 	 * which may finish later; embryonic TCB's can just
1235280Sroot 	 * be discarded here.
1245280Sroot 	 */
1254809Swnj 	case PRU_DETACH:
1265280Sroot 		if (tp->t_state > TCPS_LISTEN)
12710397Ssam 			tp = tcp_disconnect(tp);
12810397Ssam 		else
12910397Ssam 			tp = tcp_close(tp);
1304809Swnj 		break;
1314809Swnj 
1325280Sroot 	/*
1338272Sroot 	 * Give the socket an address.
1348272Sroot 	 */
1358272Sroot 	case PRU_BIND:
1368272Sroot 		error = in_pcbbind(inp, nam);
1378272Sroot 		if (error)
1388272Sroot 			break;
1398272Sroot 		break;
1408272Sroot 
1418272Sroot 	/*
1428272Sroot 	 * Prepare to accept connections.
1438272Sroot 	 */
1448272Sroot 	case PRU_LISTEN:
1458272Sroot 		if (inp->inp_lport == 0)
1468272Sroot 			error = in_pcbbind(inp, (struct mbuf *)0);
1478272Sroot 		if (error == 0)
1488272Sroot 			tp->t_state = TCPS_LISTEN;
1498272Sroot 		break;
1508272Sroot 
1518272Sroot 	/*
1525280Sroot 	 * Initiate connection to peer.
1535280Sroot 	 * Create a template for use in transmissions on this connection.
1545280Sroot 	 * Enter SYN_SENT state, and mark socket as connecting.
1555280Sroot 	 * Start keep-alive timer, and seed output sequence space.
1565280Sroot 	 * Send initial segment on connection.
1575280Sroot 	 */
1584809Swnj 	case PRU_CONNECT:
1598272Sroot 		if (inp->inp_lport == 0) {
1608272Sroot 			error = in_pcbbind(inp, (struct mbuf *)0);
1618272Sroot 			if (error)
1628272Sroot 				break;
1638272Sroot 		}
1648272Sroot 		error = in_pcbconnect(inp, nam);
1654954Swnj 		if (error)
1664886Swnj 			break;
1675174Swnj 		tp->t_template = tcp_template(tp);
1685280Sroot 		if (tp->t_template == 0) {
1695280Sroot 			in_pcbdisconnect(inp);
1705280Sroot 			error = ENOBUFS;
1715280Sroot 			break;
1725280Sroot 		}
1734886Swnj 		soisconnecting(so);
17430527Skarels 		tcpstat.tcps_connattempt++;
1755075Swnj 		tp->t_state = TCPS_SYN_SENT;
17633747Skarels 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
1775245Sroot 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
1785245Sroot 		tcp_sendseqinit(tp);
1796506Ssam 		error = tcp_output(tp);
1804567Swnj 		break;
1814497Swnj 
1825280Sroot 	/*
18313117Ssam 	 * Create a TCP connection between two sockets.
18413117Ssam 	 */
18513117Ssam 	case PRU_CONNECT2:
18613117Ssam 		error = EOPNOTSUPP;
18713117Ssam 		break;
18813117Ssam 
18913117Ssam 	/*
1905280Sroot 	 * Initiate disconnect from peer.
1915280Sroot 	 * If connection never passed embryonic stage, just drop;
1925280Sroot 	 * else if don't need to let data drain, then can just drop anyways,
1935280Sroot 	 * else have to begin TCP shutdown process: mark socket disconnecting,
1945280Sroot 	 * drain unread data, state switch to reflect user close, and
1955280Sroot 	 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
1965280Sroot 	 * when peer sends FIN and acks ours.
1975280Sroot 	 *
1985280Sroot 	 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
1995280Sroot 	 */
2005280Sroot 	case PRU_DISCONNECT:
20110397Ssam 		tp = tcp_disconnect(tp);
2025245Sroot 		break;
2035245Sroot 
2045280Sroot 	/*
2055280Sroot 	 * Accept a connection.  Essentially all the work is
2065280Sroot 	 * done at higher levels; just return the address
2075280Sroot 	 * of the peer, storing through addr.
2085280Sroot 	 */
2096117Swnj 	case PRU_ACCEPT: {
2108272Sroot 		struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
2116117Swnj 
2128272Sroot 		nam->m_len = sizeof (struct sockaddr_in);
2138272Sroot 		sin->sin_family = AF_INET;
21437323Skarels 		sin->sin_len = sizeof(*sin);
2158272Sroot 		sin->sin_port = inp->inp_fport;
2168272Sroot 		sin->sin_addr = inp->inp_faddr;
2178272Sroot 		break;
2186117Swnj 		}
2194925Swnj 
2205280Sroot 	/*
2215280Sroot 	 * Mark the connection as being incapable of further output.
2225280Sroot 	 */
2234809Swnj 	case PRU_SHUTDOWN:
2245089Swnj 		socantsendmore(so);
22510397Ssam 		tp = tcp_usrclosed(tp);
22610397Ssam 		if (tp)
22710397Ssam 			error = tcp_output(tp);
2284567Swnj 		break;
2294497Swnj 
2305280Sroot 	/*
2315280Sroot 	 * After a receive, possibly send window update to peer.
2325280Sroot 	 */
2334809Swnj 	case PRU_RCVD:
2345113Swnj 		(void) tcp_output(tp);
2354567Swnj 		break;
2364497Swnj 
2375280Sroot 	/*
2385280Sroot 	 * Do a send by putting data in output queue and updating urgent
2395280Sroot 	 * marker if URG set.  Possibly send more data.
2405280Sroot 	 */
2414809Swnj 	case PRU_SEND:
2425075Swnj 		sbappend(&so->so_snd, m);
2436506Ssam 		error = tcp_output(tp);
2444567Swnj 		break;
2454567Swnj 
2465280Sroot 	/*
2475280Sroot 	 * Abort the TCP.
2485280Sroot 	 */
2494809Swnj 	case PRU_ABORT:
25010397Ssam 		tp = tcp_drop(tp, ECONNABORTED);
2514567Swnj 		break;
2524567Swnj 
2535113Swnj 	case PRU_SENSE:
25416989Skarels 		((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
25530871Smckusick 		(void) splx(s);
25616989Skarels 		return (0);
2575113Swnj 
2585113Swnj 	case PRU_RCVOOB:
25924821Skarels 		if ((so->so_oobmark == 0 &&
26024821Skarels 		    (so->so_state & SS_RCVATMARK) == 0) ||
26127195Skarels 		    so->so_options & SO_OOBINLINE ||
26224821Skarels 		    tp->t_oobflags & TCPOOB_HADDATA) {
2635417Swnj 			error = EINVAL;
2645417Swnj 			break;
2655417Swnj 		}
2665549Swnj 		if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
2675442Swnj 			error = EWOULDBLOCK;
2685549Swnj 			break;
2695442Swnj 		}
2708310Sroot 		m->m_len = 1;
2715549Swnj 		*mtod(m, caddr_t) = tp->t_iobc;
27224821Skarels 		if (((int)nam & MSG_PEEK) == 0)
27324821Skarels 			tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2745113Swnj 		break;
2755113Swnj 
2765113Swnj 	case PRU_SENDOOB:
2775442Swnj 		if (sbspace(&so->so_snd) < -512) {
27811229Ssam 			m_freem(m);
2795442Swnj 			error = ENOBUFS;
2805442Swnj 			break;
2815442Swnj 		}
28227195Skarels 		/*
28327195Skarels 		 * According to RFC961 (Assigned Protocols),
28427195Skarels 		 * the urgent pointer points to the last octet
28527195Skarels 		 * of urgent data.  We continue, however,
28627195Skarels 		 * to consider it to indicate the first octet
28727195Skarels 		 * of data past the urgent section.
28827195Skarels 		 * Otherwise, snd_up should be one lower.
28927195Skarels 		 */
2905417Swnj 		sbappend(&so->so_snd, m);
29127195Skarels 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
2925549Swnj 		tp->t_force = 1;
2936506Ssam 		error = tcp_output(tp);
2945549Swnj 		tp->t_force = 0;
2955113Swnj 		break;
2965113Swnj 
2976510Ssam 	case PRU_SOCKADDR:
2988272Sroot 		in_setsockaddr(inp, nam);
2996510Ssam 		break;
3006510Ssam 
30114123Ssam 	case PRU_PEERADDR:
30214123Ssam 		in_setpeeraddr(inp, nam);
30314123Ssam 		break;
30414123Ssam 
3055280Sroot 	/*
3065280Sroot 	 * TCP slow timer went off; going through this
3075280Sroot 	 * routine for tracing's sake.
3085280Sroot 	 */
3094809Swnj 	case PRU_SLOWTIMO:
31010397Ssam 		tp = tcp_timers(tp, (int)nam);
3118272Sroot 		req |= (int)nam << 8;		/* for debug's sake */
3124809Swnj 		break;
3134809Swnj 
3144731Swnj 	default:
3154731Swnj 		panic("tcp_usrreq");
3164567Swnj 	}
3175270Sroot 	if (tp && (so->so_options & SO_DEBUG))
3185270Sroot 		tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req);
3194567Swnj 	splx(s);
3204886Swnj 	return (error);
3214497Swnj }
3225245Sroot 
32325896Skarels tcp_ctloutput(op, so, level, optname, mp)
32424821Skarels 	int op;
32524821Skarels 	struct socket *so;
32624821Skarels 	int level, optname;
32725896Skarels 	struct mbuf **mp;
32824821Skarels {
32925896Skarels 	int error = 0;
33025896Skarels 	struct inpcb *inp = sotoinpcb(so);
33125896Skarels 	register struct tcpcb *tp = intotcpcb(inp);
33225896Skarels 	register struct mbuf *m;
33325896Skarels 
33424821Skarels 	if (level != IPPROTO_TCP)
33526248Skarels 		return (ip_ctloutput(op, so, level, optname, mp));
33625896Skarels 
33725896Skarels 	switch (op) {
33825896Skarels 
33925896Skarels 	case PRCO_SETOPT:
34025896Skarels 		m = *mp;
34125896Skarels 		switch (optname) {
34225896Skarels 
34325896Skarels 		case TCP_NODELAY:
34425896Skarels 			if (m == NULL || m->m_len < sizeof (int))
34525896Skarels 				error = EINVAL;
34625896Skarels 			else if (*mtod(m, int *))
34725896Skarels 				tp->t_flags |= TF_NODELAY;
34825896Skarels 			else
34925896Skarels 				tp->t_flags &= ~TF_NODELAY;
35025896Skarels 			break;
35125896Skarels 
35225896Skarels 		case TCP_MAXSEG:	/* not yet */
35325896Skarels 		default:
35425896Skarels 			error = EINVAL;
35525896Skarels 			break;
35625896Skarels 		}
35731041Ssam 		if (m)
35831041Ssam 			(void) m_free(m);
35925896Skarels 		break;
36025896Skarels 
36125896Skarels 	case PRCO_GETOPT:
36225896Skarels 		*mp = m = m_get(M_WAIT, MT_SOOPTS);
36325896Skarels 		m->m_len = sizeof(int);
36425896Skarels 
36525896Skarels 		switch (optname) {
36625896Skarels 		case TCP_NODELAY:
36725896Skarels 			*mtod(m, int *) = tp->t_flags & TF_NODELAY;
36825896Skarels 			break;
36925896Skarels 		case TCP_MAXSEG:
37025896Skarels 			*mtod(m, int *) = tp->t_maxseg;
37125896Skarels 			break;
37225896Skarels 		default:
37325896Skarels 			error = EINVAL;
37425896Skarels 			break;
37525896Skarels 		}
37625896Skarels 		break;
37725896Skarels 	}
37825896Skarels 	return (error);
37924821Skarels }
38024821Skarels 
38134485Skarels u_long	tcp_sendspace = 1024*4;
38234485Skarels u_long	tcp_recvspace = 1024*4;
38337323Skarels 
3845280Sroot /*
3855280Sroot  * Attach TCP protocol to socket, allocating
3865280Sroot  * internet protocol control block, tcp control block,
3875280Sroot  * bufer space, and entering LISTEN state if to accept connections.
3885280Sroot  */
3898272Sroot tcp_attach(so)
3905280Sroot 	struct socket *so;
3915280Sroot {
3925280Sroot 	register struct tcpcb *tp;
3935280Sroot 	struct inpcb *inp;
3945280Sroot 	int error;
3955280Sroot 
39634485Skarels 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
39734485Skarels 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
39834485Skarels 		if (error)
39934485Skarels 			return (error);
40034485Skarels 	}
4017511Sroot 	error = in_pcballoc(so, &tcb);
4027511Sroot 	if (error)
40317047Skarels 		return (error);
4048272Sroot 	inp = sotoinpcb(so);
4055280Sroot 	tp = tcp_newtcpcb(inp);
4067511Sroot 	if (tp == 0) {
40717047Skarels 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
40817047Skarels 
40917047Skarels 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
41017047Skarels 		in_pcbdetach(inp);
41117047Skarels 		so->so_state |= nofd;
41217047Skarels 		return (ENOBUFS);
4137511Sroot 	}
4148272Sroot 	tp->t_state = TCPS_CLOSED;
4155280Sroot 	return (0);
4165280Sroot }
4175280Sroot 
4185280Sroot /*
4195280Sroot  * Initiate (or continue) disconnect.
4205280Sroot  * If embryonic state, just send reset (once).
42113221Ssam  * If in ``let data drain'' option and linger null, just drop.
4225280Sroot  * Otherwise (hard), mark socket disconnecting and drop
4235280Sroot  * current input data; switch states based on user close, and
4245280Sroot  * send segment to peer (with FIN).
4255280Sroot  */
42610397Ssam struct tcpcb *
4275280Sroot tcp_disconnect(tp)
42810397Ssam 	register struct tcpcb *tp;
4295280Sroot {
4305280Sroot 	struct socket *so = tp->t_inpcb->inp_socket;
4315280Sroot 
4325280Sroot 	if (tp->t_state < TCPS_ESTABLISHED)
43310397Ssam 		tp = tcp_close(tp);
43413221Ssam 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
43510397Ssam 		tp = tcp_drop(tp, 0);
4365280Sroot 	else {
4375280Sroot 		soisdisconnecting(so);
4385280Sroot 		sbflush(&so->so_rcv);
43910397Ssam 		tp = tcp_usrclosed(tp);
44010397Ssam 		if (tp)
44110397Ssam 			(void) tcp_output(tp);
4425280Sroot 	}
44310397Ssam 	return (tp);
4445280Sroot }
4455280Sroot 
4465280Sroot /*
4475280Sroot  * User issued close, and wish to trail through shutdown states:
4485280Sroot  * if never received SYN, just forget it.  If got a SYN from peer,
4495280Sroot  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
4505280Sroot  * If already got a FIN from peer, then almost done; go to LAST_ACK
4515280Sroot  * state.  In all other cases, have already sent FIN to peer (e.g.
4525280Sroot  * after PRU_SHUTDOWN), and just have to play tedious game waiting
4535280Sroot  * for peer to send FIN or not respond to keep-alives, etc.
4545897Swnj  * We can let the user exit from the close as soon as the FIN is acked.
4555280Sroot  */
45610397Ssam struct tcpcb *
4575245Sroot tcp_usrclosed(tp)
45810397Ssam 	register struct tcpcb *tp;
4595245Sroot {
4605245Sroot 
4615245Sroot 	switch (tp->t_state) {
4625245Sroot 
46312438Ssam 	case TCPS_CLOSED:
4645245Sroot 	case TCPS_LISTEN:
4655245Sroot 	case TCPS_SYN_SENT:
4665245Sroot 		tp->t_state = TCPS_CLOSED;
46710397Ssam 		tp = tcp_close(tp);
4685245Sroot 		break;
4695245Sroot 
4705245Sroot 	case TCPS_SYN_RECEIVED:
4715245Sroot 	case TCPS_ESTABLISHED:
4725245Sroot 		tp->t_state = TCPS_FIN_WAIT_1;
4735245Sroot 		break;
4745245Sroot 
4755245Sroot 	case TCPS_CLOSE_WAIT:
4765245Sroot 		tp->t_state = TCPS_LAST_ACK;
4775245Sroot 		break;
4785245Sroot 	}
47910397Ssam 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2)
4805897Swnj 		soisdisconnected(tp->t_inpcb->inp_socket);
48110397Ssam 	return (tp);
4825245Sroot }
483