xref: /csrg-svn/sys/netinet/tcp_usrreq.c (revision 57433)
123196Smckusick /*
237323Skarels  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
332789Sbostic  * All rights reserved.
423196Smckusick  *
544491Sbostic  * %sccs.include.redist.c%
632789Sbostic  *
7*57433Sandrew  *	@(#)tcp_usrreq.c	7.18 (Berkeley) 01/08/93
823196Smckusick  */
94567Swnj 
1056531Sbostic #include <sys/param.h>
1156531Sbostic #include <sys/systm.h>
1256531Sbostic #include <sys/malloc.h>
1356531Sbostic #include <sys/mbuf.h>
1456531Sbostic #include <sys/socket.h>
1556531Sbostic #include <sys/socketvar.h>
1656531Sbostic #include <sys/protosw.h>
1756531Sbostic #include <sys/errno.h>
1856531Sbostic #include <sys/stat.h>
198697Sroot 
2056531Sbostic #include <net/if.h>
2156531Sbostic #include <net/route.h>
2210896Ssam 
2356531Sbostic #include <netinet/in.h>
2456531Sbostic #include <netinet/in_systm.h>
2556531Sbostic #include <netinet/ip.h>
2656531Sbostic #include <netinet/in_pcb.h>
2756531Sbostic #include <netinet/ip_var.h>
2856531Sbostic #include <netinet/tcp.h>
2956531Sbostic #include <netinet/tcp_fsm.h>
3056531Sbostic #include <netinet/tcp_seq.h>
3156531Sbostic #include <netinet/tcp_timer.h>
3256531Sbostic #include <netinet/tcp_var.h>
3356531Sbostic #include <netinet/tcpip.h>
3456531Sbostic #include <netinet/tcp_debug.h>
354497Swnj 
365280Sroot /*
375280Sroot  * TCP protocol interface to socket abstraction.
385280Sroot  */
395280Sroot extern	char *tcpstates[];
404954Swnj struct	tcpcb *tcp_newtcpcb();
415280Sroot 
424734Swnj /*
435280Sroot  * Process a TCP user request for TCP tb.  If this is a send request
444731Swnj  * then m is the mbuf chain of send data.  If this is a timer expiration
454731Swnj  * (called from the software clock routine), then timertype tells which timer.
464731Swnj  */
478601Sroot /*ARGSUSED*/
4842184Skarels tcp_usrreq(so, req, m, nam, control)
494809Swnj 	struct socket *so;
504809Swnj 	int req;
5142184Skarels 	struct mbuf *m, *nam, *control;
524497Swnj {
5330909Skarels 	register struct inpcb *inp;
544911Swnj 	register struct tcpcb *tp;
5530909Skarels 	int s;
564809Swnj 	int error = 0;
575270Sroot 	int ostate;
584497Swnj 
5930909Skarels 	if (req == PRU_CONTROL)
6030909Skarels 		return (in_control(so, (int)m, (caddr_t)nam,
6142184Skarels 			(struct ifnet *)control));
6242184Skarels 	if (control && control->m_len) {
6342184Skarels 		m_freem(control);
6442184Skarels 		if (m)
6542184Skarels 			m_freem(m);
6612766Ssam 		return (EINVAL);
6742184Skarels 	}
6830909Skarels 
6930909Skarels 	s = splnet();
7030909Skarels 	inp = sotoinpcb(so);
714886Swnj 	/*
725280Sroot 	 * When a TCP is attached to a socket, then there will be
735280Sroot 	 * a (struct inpcb) pointed at by the socket, and this
745280Sroot 	 * structure will point at a subsidary (struct tcpcb).
754886Swnj 	 */
765089Swnj 	if (inp == 0 && req != PRU_ATTACH) {
775075Swnj 		splx(s);
785280Sroot 		return (EINVAL);		/* XXX */
795075Swnj 	}
805075Swnj 	if (inp) {
814911Swnj 		tp = intotcpcb(inp);
828272Sroot 		/* WHAT IF TP IS 0? */
834731Swnj #ifdef KPROF
845075Swnj 		tcp_acounts[tp->t_state][req]++;
854731Swnj #endif
865270Sroot 		ostate = tp->t_state;
877511Sroot 	} else
887511Sroot 		ostate = 0;
894809Swnj 	switch (req) {
904497Swnj 
915280Sroot 	/*
925280Sroot 	 * TCP attaches to socket via PRU_ATTACH, reserving space,
938272Sroot 	 * and an internet control block.
945280Sroot 	 */
954809Swnj 	case PRU_ATTACH:
964954Swnj 		if (inp) {
974809Swnj 			error = EISCONN;
984911Swnj 			break;
994886Swnj 		}
1008640Sroot 		error = tcp_attach(so);
1015075Swnj 		if (error)
1024954Swnj 			break;
10310397Ssam 		if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1045392Swnj 			so->so_linger = TCP_LINGERTIME;
1055280Sroot 		tp = sototcpcb(so);
1064567Swnj 		break;
1074497Swnj 
1085280Sroot 	/*
1095280Sroot 	 * PRU_DETACH detaches the TCP protocol from the socket.
1105280Sroot 	 * If the protocol state is non-embryonic, then can't
1115280Sroot 	 * do this directly: have to initiate a PRU_DISCONNECT,
1125280Sroot 	 * which may finish later; embryonic TCB's can just
1135280Sroot 	 * be discarded here.
1145280Sroot 	 */
1154809Swnj 	case PRU_DETACH:
1165280Sroot 		if (tp->t_state > TCPS_LISTEN)
11710397Ssam 			tp = tcp_disconnect(tp);
11810397Ssam 		else
11910397Ssam 			tp = tcp_close(tp);
1204809Swnj 		break;
1214809Swnj 
1225280Sroot 	/*
1238272Sroot 	 * Give the socket an address.
1248272Sroot 	 */
1258272Sroot 	case PRU_BIND:
1268272Sroot 		error = in_pcbbind(inp, nam);
1278272Sroot 		if (error)
1288272Sroot 			break;
1298272Sroot 		break;
1308272Sroot 
1318272Sroot 	/*
1328272Sroot 	 * Prepare to accept connections.
1338272Sroot 	 */
1348272Sroot 	case PRU_LISTEN:
1358272Sroot 		if (inp->inp_lport == 0)
1368272Sroot 			error = in_pcbbind(inp, (struct mbuf *)0);
1378272Sroot 		if (error == 0)
1388272Sroot 			tp->t_state = TCPS_LISTEN;
1398272Sroot 		break;
1408272Sroot 
1418272Sroot 	/*
1425280Sroot 	 * Initiate connection to peer.
1435280Sroot 	 * Create a template for use in transmissions on this connection.
1445280Sroot 	 * Enter SYN_SENT state, and mark socket as connecting.
1455280Sroot 	 * Start keep-alive timer, and seed output sequence space.
1465280Sroot 	 * Send initial segment on connection.
1475280Sroot 	 */
1484809Swnj 	case PRU_CONNECT:
1498272Sroot 		if (inp->inp_lport == 0) {
1508272Sroot 			error = in_pcbbind(inp, (struct mbuf *)0);
1518272Sroot 			if (error)
1528272Sroot 				break;
1538272Sroot 		}
1548272Sroot 		error = in_pcbconnect(inp, nam);
1554954Swnj 		if (error)
1564886Swnj 			break;
1575174Swnj 		tp->t_template = tcp_template(tp);
1585280Sroot 		if (tp->t_template == 0) {
1595280Sroot 			in_pcbdisconnect(inp);
1605280Sroot 			error = ENOBUFS;
1615280Sroot 			break;
1625280Sroot 		}
163*57433Sandrew 		/* Compute window scaling to request.  */
164*57433Sandrew 		while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
165*57433Sandrew 		    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
166*57433Sandrew 			tp->request_r_scale++;
1674886Swnj 		soisconnecting(so);
16830527Skarels 		tcpstat.tcps_connattempt++;
1695075Swnj 		tp->t_state = TCPS_SYN_SENT;
17033747Skarels 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
1715245Sroot 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
1725245Sroot 		tcp_sendseqinit(tp);
1736506Ssam 		error = tcp_output(tp);
1744567Swnj 		break;
1754497Swnj 
1765280Sroot 	/*
17713117Ssam 	 * Create a TCP connection between two sockets.
17813117Ssam 	 */
17913117Ssam 	case PRU_CONNECT2:
18013117Ssam 		error = EOPNOTSUPP;
18113117Ssam 		break;
18213117Ssam 
18313117Ssam 	/*
1845280Sroot 	 * Initiate disconnect from peer.
1855280Sroot 	 * If connection never passed embryonic stage, just drop;
1865280Sroot 	 * else if don't need to let data drain, then can just drop anyways,
1875280Sroot 	 * else have to begin TCP shutdown process: mark socket disconnecting,
1885280Sroot 	 * drain unread data, state switch to reflect user close, and
1895280Sroot 	 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
1905280Sroot 	 * when peer sends FIN and acks ours.
1915280Sroot 	 *
1925280Sroot 	 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
1935280Sroot 	 */
1945280Sroot 	case PRU_DISCONNECT:
19510397Ssam 		tp = tcp_disconnect(tp);
1965245Sroot 		break;
1975245Sroot 
1985280Sroot 	/*
1995280Sroot 	 * Accept a connection.  Essentially all the work is
2005280Sroot 	 * done at higher levels; just return the address
2015280Sroot 	 * of the peer, storing through addr.
2025280Sroot 	 */
2036117Swnj 	case PRU_ACCEPT: {
2048272Sroot 		struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
2056117Swnj 
2068272Sroot 		nam->m_len = sizeof (struct sockaddr_in);
2078272Sroot 		sin->sin_family = AF_INET;
20837323Skarels 		sin->sin_len = sizeof(*sin);
2098272Sroot 		sin->sin_port = inp->inp_fport;
2108272Sroot 		sin->sin_addr = inp->inp_faddr;
2118272Sroot 		break;
2126117Swnj 		}
2134925Swnj 
2145280Sroot 	/*
2155280Sroot 	 * Mark the connection as being incapable of further output.
2165280Sroot 	 */
2174809Swnj 	case PRU_SHUTDOWN:
2185089Swnj 		socantsendmore(so);
21910397Ssam 		tp = tcp_usrclosed(tp);
22010397Ssam 		if (tp)
22110397Ssam 			error = tcp_output(tp);
2224567Swnj 		break;
2234497Swnj 
2245280Sroot 	/*
2255280Sroot 	 * After a receive, possibly send window update to peer.
2265280Sroot 	 */
2274809Swnj 	case PRU_RCVD:
2285113Swnj 		(void) tcp_output(tp);
2294567Swnj 		break;
2304497Swnj 
2315280Sroot 	/*
2325280Sroot 	 * Do a send by putting data in output queue and updating urgent
2335280Sroot 	 * marker if URG set.  Possibly send more data.
2345280Sroot 	 */
2354809Swnj 	case PRU_SEND:
2365075Swnj 		sbappend(&so->so_snd, m);
2376506Ssam 		error = tcp_output(tp);
2384567Swnj 		break;
2394567Swnj 
2405280Sroot 	/*
2415280Sroot 	 * Abort the TCP.
2425280Sroot 	 */
2434809Swnj 	case PRU_ABORT:
24410397Ssam 		tp = tcp_drop(tp, ECONNABORTED);
2454567Swnj 		break;
2464567Swnj 
2475113Swnj 	case PRU_SENSE:
24816989Skarels 		((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
24930871Smckusick 		(void) splx(s);
25016989Skarels 		return (0);
2515113Swnj 
2525113Swnj 	case PRU_RCVOOB:
25324821Skarels 		if ((so->so_oobmark == 0 &&
25424821Skarels 		    (so->so_state & SS_RCVATMARK) == 0) ||
25527195Skarels 		    so->so_options & SO_OOBINLINE ||
25624821Skarels 		    tp->t_oobflags & TCPOOB_HADDATA) {
2575417Swnj 			error = EINVAL;
2585417Swnj 			break;
2595417Swnj 		}
2605549Swnj 		if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
2615442Swnj 			error = EWOULDBLOCK;
2625549Swnj 			break;
2635442Swnj 		}
2648310Sroot 		m->m_len = 1;
2655549Swnj 		*mtod(m, caddr_t) = tp->t_iobc;
26624821Skarels 		if (((int)nam & MSG_PEEK) == 0)
26724821Skarels 			tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2685113Swnj 		break;
2695113Swnj 
2705113Swnj 	case PRU_SENDOOB:
2715442Swnj 		if (sbspace(&so->so_snd) < -512) {
27211229Ssam 			m_freem(m);
2735442Swnj 			error = ENOBUFS;
2745442Swnj 			break;
2755442Swnj 		}
27627195Skarels 		/*
27727195Skarels 		 * According to RFC961 (Assigned Protocols),
27827195Skarels 		 * the urgent pointer points to the last octet
27927195Skarels 		 * of urgent data.  We continue, however,
28027195Skarels 		 * to consider it to indicate the first octet
28127195Skarels 		 * of data past the urgent section.
28227195Skarels 		 * Otherwise, snd_up should be one lower.
28327195Skarels 		 */
2845417Swnj 		sbappend(&so->so_snd, m);
28527195Skarels 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
2865549Swnj 		tp->t_force = 1;
2876506Ssam 		error = tcp_output(tp);
2885549Swnj 		tp->t_force = 0;
2895113Swnj 		break;
2905113Swnj 
2916510Ssam 	case PRU_SOCKADDR:
2928272Sroot 		in_setsockaddr(inp, nam);
2936510Ssam 		break;
2946510Ssam 
29514123Ssam 	case PRU_PEERADDR:
29614123Ssam 		in_setpeeraddr(inp, nam);
29714123Ssam 		break;
29814123Ssam 
2995280Sroot 	/*
3005280Sroot 	 * TCP slow timer went off; going through this
3015280Sroot 	 * routine for tracing's sake.
3025280Sroot 	 */
3034809Swnj 	case PRU_SLOWTIMO:
30410397Ssam 		tp = tcp_timers(tp, (int)nam);
3058272Sroot 		req |= (int)nam << 8;		/* for debug's sake */
3064809Swnj 		break;
3074809Swnj 
3084731Swnj 	default:
3094731Swnj 		panic("tcp_usrreq");
3104567Swnj 	}
3115270Sroot 	if (tp && (so->so_options & SO_DEBUG))
3125270Sroot 		tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req);
3134567Swnj 	splx(s);
3144886Swnj 	return (error);
3154497Swnj }
3165245Sroot 
31725896Skarels tcp_ctloutput(op, so, level, optname, mp)
31824821Skarels 	int op;
31924821Skarels 	struct socket *so;
32024821Skarels 	int level, optname;
32125896Skarels 	struct mbuf **mp;
32224821Skarels {
323*57433Sandrew 	int error = 0, s;
324*57433Sandrew 	struct inpcb *inp;
325*57433Sandrew 	register struct tcpcb *tp;
32625896Skarels 	register struct mbuf *m;
32755287Smckusick 	register int i;
32825896Skarels 
329*57433Sandrew 	s = splnet();
330*57433Sandrew 	inp = sotoinpcb(so);
331*57433Sandrew 	if (inp == NULL) {
332*57433Sandrew 		splx(s);
333*57433Sandrew 		return (ECONNRESET);
334*57433Sandrew 	}
335*57433Sandrew 	if (level != IPPROTO_TCP) {
336*57433Sandrew 		error = ip_ctloutput(op, so, level, optname, mp);
337*57433Sandrew 		splx(s);
338*57433Sandrew 		return (error);
339*57433Sandrew 	}
340*57433Sandrew 	tp = intotcpcb(inp);
34125896Skarels 
34225896Skarels 	switch (op) {
34325896Skarels 
34425896Skarels 	case PRCO_SETOPT:
34525896Skarels 		m = *mp;
34625896Skarels 		switch (optname) {
34725896Skarels 
34825896Skarels 		case TCP_NODELAY:
34925896Skarels 			if (m == NULL || m->m_len < sizeof (int))
35025896Skarels 				error = EINVAL;
35125896Skarels 			else if (*mtod(m, int *))
35225896Skarels 				tp->t_flags |= TF_NODELAY;
35325896Skarels 			else
35425896Skarels 				tp->t_flags &= ~TF_NODELAY;
35525896Skarels 			break;
35625896Skarels 
35755287Smckusick 		case TCP_MAXSEG:
35855287Smckusick 			if (m && (i = *mtod(m, int *)) > 0 && i <= tp->t_maxseg)
35955287Smckusick 				tp->t_maxseg = i;
36055287Smckusick 			else
36155287Smckusick 				error = EINVAL;
36255287Smckusick 			break;
36355287Smckusick 
36425896Skarels 		default:
36525896Skarels 			error = EINVAL;
36625896Skarels 			break;
36725896Skarels 		}
36831041Ssam 		if (m)
36931041Ssam 			(void) m_free(m);
37025896Skarels 		break;
37125896Skarels 
37225896Skarels 	case PRCO_GETOPT:
37325896Skarels 		*mp = m = m_get(M_WAIT, MT_SOOPTS);
37425896Skarels 		m->m_len = sizeof(int);
37525896Skarels 
37625896Skarels 		switch (optname) {
37725896Skarels 		case TCP_NODELAY:
37825896Skarels 			*mtod(m, int *) = tp->t_flags & TF_NODELAY;
37925896Skarels 			break;
38025896Skarels 		case TCP_MAXSEG:
38125896Skarels 			*mtod(m, int *) = tp->t_maxseg;
38225896Skarels 			break;
38325896Skarels 		default:
38425896Skarels 			error = EINVAL;
38525896Skarels 			break;
38625896Skarels 		}
38725896Skarels 		break;
38825896Skarels 	}
389*57433Sandrew 	splx(s);
39025896Skarels 	return (error);
39124821Skarels }
39224821Skarels 
39355287Smckusick u_long	tcp_sendspace = 1024*8;
39455287Smckusick u_long	tcp_recvspace = 1024*8;
39537323Skarels 
3965280Sroot /*
3975280Sroot  * Attach TCP protocol to socket, allocating
3985280Sroot  * internet protocol control block, tcp control block,
3995280Sroot  * bufer space, and entering LISTEN state if to accept connections.
4005280Sroot  */
4018272Sroot tcp_attach(so)
4025280Sroot 	struct socket *so;
4035280Sroot {
4045280Sroot 	register struct tcpcb *tp;
4055280Sroot 	struct inpcb *inp;
4065280Sroot 	int error;
4075280Sroot 
40834485Skarels 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
40934485Skarels 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
41034485Skarels 		if (error)
41134485Skarels 			return (error);
41234485Skarels 	}
4137511Sroot 	error = in_pcballoc(so, &tcb);
4147511Sroot 	if (error)
41517047Skarels 		return (error);
4168272Sroot 	inp = sotoinpcb(so);
4175280Sroot 	tp = tcp_newtcpcb(inp);
4187511Sroot 	if (tp == 0) {
41917047Skarels 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
42017047Skarels 
42117047Skarels 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
42217047Skarels 		in_pcbdetach(inp);
42317047Skarels 		so->so_state |= nofd;
42417047Skarels 		return (ENOBUFS);
4257511Sroot 	}
4268272Sroot 	tp->t_state = TCPS_CLOSED;
4275280Sroot 	return (0);
4285280Sroot }
4295280Sroot 
4305280Sroot /*
4315280Sroot  * Initiate (or continue) disconnect.
4325280Sroot  * If embryonic state, just send reset (once).
43313221Ssam  * If in ``let data drain'' option and linger null, just drop.
4345280Sroot  * Otherwise (hard), mark socket disconnecting and drop
4355280Sroot  * current input data; switch states based on user close, and
4365280Sroot  * send segment to peer (with FIN).
4375280Sroot  */
43810397Ssam struct tcpcb *
4395280Sroot tcp_disconnect(tp)
44010397Ssam 	register struct tcpcb *tp;
4415280Sroot {
4425280Sroot 	struct socket *so = tp->t_inpcb->inp_socket;
4435280Sroot 
4445280Sroot 	if (tp->t_state < TCPS_ESTABLISHED)
44510397Ssam 		tp = tcp_close(tp);
44613221Ssam 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
44710397Ssam 		tp = tcp_drop(tp, 0);
4485280Sroot 	else {
4495280Sroot 		soisdisconnecting(so);
4505280Sroot 		sbflush(&so->so_rcv);
45110397Ssam 		tp = tcp_usrclosed(tp);
45210397Ssam 		if (tp)
45310397Ssam 			(void) tcp_output(tp);
4545280Sroot 	}
45510397Ssam 	return (tp);
4565280Sroot }
4575280Sroot 
4585280Sroot /*
4595280Sroot  * User issued close, and wish to trail through shutdown states:
4605280Sroot  * if never received SYN, just forget it.  If got a SYN from peer,
4615280Sroot  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
4625280Sroot  * If already got a FIN from peer, then almost done; go to LAST_ACK
4635280Sroot  * state.  In all other cases, have already sent FIN to peer (e.g.
4645280Sroot  * after PRU_SHUTDOWN), and just have to play tedious game waiting
4655280Sroot  * for peer to send FIN or not respond to keep-alives, etc.
4665897Swnj  * We can let the user exit from the close as soon as the FIN is acked.
4675280Sroot  */
46810397Ssam struct tcpcb *
4695245Sroot tcp_usrclosed(tp)
47010397Ssam 	register struct tcpcb *tp;
4715245Sroot {
4725245Sroot 
4735245Sroot 	switch (tp->t_state) {
4745245Sroot 
47512438Ssam 	case TCPS_CLOSED:
4765245Sroot 	case TCPS_LISTEN:
4775245Sroot 	case TCPS_SYN_SENT:
4785245Sroot 		tp->t_state = TCPS_CLOSED;
47910397Ssam 		tp = tcp_close(tp);
4805245Sroot 		break;
4815245Sroot 
4825245Sroot 	case TCPS_SYN_RECEIVED:
4835245Sroot 	case TCPS_ESTABLISHED:
4845245Sroot 		tp->t_state = TCPS_FIN_WAIT_1;
4855245Sroot 		break;
4865245Sroot 
4875245Sroot 	case TCPS_CLOSE_WAIT:
4885245Sroot 		tp->t_state = TCPS_LAST_ACK;
4895245Sroot 		break;
4905245Sroot 	}
49110397Ssam 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2)
4925897Swnj 		soisdisconnected(tp->t_inpcb->inp_socket);
49310397Ssam 	return (tp);
4945245Sroot }
495