xref: /csrg-svn/sys/netinet/tcp_usrreq.c (revision 44491)
123196Smckusick /*
237323Skarels  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
332789Sbostic  * All rights reserved.
423196Smckusick  *
5*44491Sbostic  * %sccs.include.redist.c%
632789Sbostic  *
7*44491Sbostic  *	@(#)tcp_usrreq.c	7.15 (Berkeley) 06/28/90
823196Smckusick  */
94567Swnj 
1017064Sbloom #include "param.h"
1117064Sbloom #include "systm.h"
1237323Skarels #include "malloc.h"
1317064Sbloom #include "mbuf.h"
1417064Sbloom #include "socket.h"
1517064Sbloom #include "socketvar.h"
1617064Sbloom #include "protosw.h"
1717064Sbloom #include "errno.h"
1817064Sbloom #include "stat.h"
198697Sroot 
208697Sroot #include "../net/if.h"
218697Sroot #include "../net/route.h"
2210896Ssam 
2317064Sbloom #include "in.h"
2417064Sbloom #include "in_systm.h"
2517064Sbloom #include "ip.h"
2640690Skarels #include "in_pcb.h"
2717064Sbloom #include "ip_var.h"
2817064Sbloom #include "tcp.h"
2917064Sbloom #include "tcp_fsm.h"
3017064Sbloom #include "tcp_seq.h"
3117064Sbloom #include "tcp_timer.h"
3217064Sbloom #include "tcp_var.h"
3317064Sbloom #include "tcpip.h"
3417064Sbloom #include "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 		}
1634886Swnj 		soisconnecting(so);
16430527Skarels 		tcpstat.tcps_connattempt++;
1655075Swnj 		tp->t_state = TCPS_SYN_SENT;
16633747Skarels 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
1675245Sroot 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
1685245Sroot 		tcp_sendseqinit(tp);
1696506Ssam 		error = tcp_output(tp);
1704567Swnj 		break;
1714497Swnj 
1725280Sroot 	/*
17313117Ssam 	 * Create a TCP connection between two sockets.
17413117Ssam 	 */
17513117Ssam 	case PRU_CONNECT2:
17613117Ssam 		error = EOPNOTSUPP;
17713117Ssam 		break;
17813117Ssam 
17913117Ssam 	/*
1805280Sroot 	 * Initiate disconnect from peer.
1815280Sroot 	 * If connection never passed embryonic stage, just drop;
1825280Sroot 	 * else if don't need to let data drain, then can just drop anyways,
1835280Sroot 	 * else have to begin TCP shutdown process: mark socket disconnecting,
1845280Sroot 	 * drain unread data, state switch to reflect user close, and
1855280Sroot 	 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
1865280Sroot 	 * when peer sends FIN and acks ours.
1875280Sroot 	 *
1885280Sroot 	 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
1895280Sroot 	 */
1905280Sroot 	case PRU_DISCONNECT:
19110397Ssam 		tp = tcp_disconnect(tp);
1925245Sroot 		break;
1935245Sroot 
1945280Sroot 	/*
1955280Sroot 	 * Accept a connection.  Essentially all the work is
1965280Sroot 	 * done at higher levels; just return the address
1975280Sroot 	 * of the peer, storing through addr.
1985280Sroot 	 */
1996117Swnj 	case PRU_ACCEPT: {
2008272Sroot 		struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
2016117Swnj 
2028272Sroot 		nam->m_len = sizeof (struct sockaddr_in);
2038272Sroot 		sin->sin_family = AF_INET;
20437323Skarels 		sin->sin_len = sizeof(*sin);
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);
2344567Swnj 		break;
2354567Swnj 
2365280Sroot 	/*
2375280Sroot 	 * Abort the TCP.
2385280Sroot 	 */
2394809Swnj 	case PRU_ABORT:
24010397Ssam 		tp = tcp_drop(tp, ECONNABORTED);
2414567Swnj 		break;
2424567Swnj 
2435113Swnj 	case PRU_SENSE:
24416989Skarels 		((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
24530871Smckusick 		(void) splx(s);
24616989Skarels 		return (0);
2475113Swnj 
2485113Swnj 	case PRU_RCVOOB:
24924821Skarels 		if ((so->so_oobmark == 0 &&
25024821Skarels 		    (so->so_state & SS_RCVATMARK) == 0) ||
25127195Skarels 		    so->so_options & SO_OOBINLINE ||
25224821Skarels 		    tp->t_oobflags & TCPOOB_HADDATA) {
2535417Swnj 			error = EINVAL;
2545417Swnj 			break;
2555417Swnj 		}
2565549Swnj 		if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
2575442Swnj 			error = EWOULDBLOCK;
2585549Swnj 			break;
2595442Swnj 		}
2608310Sroot 		m->m_len = 1;
2615549Swnj 		*mtod(m, caddr_t) = tp->t_iobc;
26224821Skarels 		if (((int)nam & MSG_PEEK) == 0)
26324821Skarels 			tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2645113Swnj 		break;
2655113Swnj 
2665113Swnj 	case PRU_SENDOOB:
2675442Swnj 		if (sbspace(&so->so_snd) < -512) {
26811229Ssam 			m_freem(m);
2695442Swnj 			error = ENOBUFS;
2705442Swnj 			break;
2715442Swnj 		}
27227195Skarels 		/*
27327195Skarels 		 * According to RFC961 (Assigned Protocols),
27427195Skarels 		 * the urgent pointer points to the last octet
27527195Skarels 		 * of urgent data.  We continue, however,
27627195Skarels 		 * to consider it to indicate the first octet
27727195Skarels 		 * of data past the urgent section.
27827195Skarels 		 * Otherwise, snd_up should be one lower.
27927195Skarels 		 */
2805417Swnj 		sbappend(&so->so_snd, m);
28127195Skarels 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
2825549Swnj 		tp->t_force = 1;
2836506Ssam 		error = tcp_output(tp);
2845549Swnj 		tp->t_force = 0;
2855113Swnj 		break;
2865113Swnj 
2876510Ssam 	case PRU_SOCKADDR:
2888272Sroot 		in_setsockaddr(inp, nam);
2896510Ssam 		break;
2906510Ssam 
29114123Ssam 	case PRU_PEERADDR:
29214123Ssam 		in_setpeeraddr(inp, nam);
29314123Ssam 		break;
29414123Ssam 
2955280Sroot 	/*
2965280Sroot 	 * TCP slow timer went off; going through this
2975280Sroot 	 * routine for tracing's sake.
2985280Sroot 	 */
2994809Swnj 	case PRU_SLOWTIMO:
30010397Ssam 		tp = tcp_timers(tp, (int)nam);
3018272Sroot 		req |= (int)nam << 8;		/* for debug's sake */
3024809Swnj 		break;
3034809Swnj 
3044731Swnj 	default:
3054731Swnj 		panic("tcp_usrreq");
3064567Swnj 	}
3075270Sroot 	if (tp && (so->so_options & SO_DEBUG))
3085270Sroot 		tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req);
3094567Swnj 	splx(s);
3104886Swnj 	return (error);
3114497Swnj }
3125245Sroot 
31325896Skarels tcp_ctloutput(op, so, level, optname, mp)
31424821Skarels 	int op;
31524821Skarels 	struct socket *so;
31624821Skarels 	int level, optname;
31725896Skarels 	struct mbuf **mp;
31824821Skarels {
31925896Skarels 	int error = 0;
32025896Skarels 	struct inpcb *inp = sotoinpcb(so);
32125896Skarels 	register struct tcpcb *tp = intotcpcb(inp);
32225896Skarels 	register struct mbuf *m;
32325896Skarels 
32424821Skarels 	if (level != IPPROTO_TCP)
32526248Skarels 		return (ip_ctloutput(op, so, level, optname, mp));
32625896Skarels 
32725896Skarels 	switch (op) {
32825896Skarels 
32925896Skarels 	case PRCO_SETOPT:
33025896Skarels 		m = *mp;
33125896Skarels 		switch (optname) {
33225896Skarels 
33325896Skarels 		case TCP_NODELAY:
33425896Skarels 			if (m == NULL || m->m_len < sizeof (int))
33525896Skarels 				error = EINVAL;
33625896Skarels 			else if (*mtod(m, int *))
33725896Skarels 				tp->t_flags |= TF_NODELAY;
33825896Skarels 			else
33925896Skarels 				tp->t_flags &= ~TF_NODELAY;
34025896Skarels 			break;
34125896Skarels 
34225896Skarels 		case TCP_MAXSEG:	/* not yet */
34325896Skarels 		default:
34425896Skarels 			error = EINVAL;
34525896Skarels 			break;
34625896Skarels 		}
34731041Ssam 		if (m)
34831041Ssam 			(void) m_free(m);
34925896Skarels 		break;
35025896Skarels 
35125896Skarels 	case PRCO_GETOPT:
35225896Skarels 		*mp = m = m_get(M_WAIT, MT_SOOPTS);
35325896Skarels 		m->m_len = sizeof(int);
35425896Skarels 
35525896Skarels 		switch (optname) {
35625896Skarels 		case TCP_NODELAY:
35725896Skarels 			*mtod(m, int *) = tp->t_flags & TF_NODELAY;
35825896Skarels 			break;
35925896Skarels 		case TCP_MAXSEG:
36025896Skarels 			*mtod(m, int *) = tp->t_maxseg;
36125896Skarels 			break;
36225896Skarels 		default:
36325896Skarels 			error = EINVAL;
36425896Skarels 			break;
36525896Skarels 		}
36625896Skarels 		break;
36725896Skarels 	}
36825896Skarels 	return (error);
36924821Skarels }
37024821Skarels 
37134485Skarels u_long	tcp_sendspace = 1024*4;
37234485Skarels u_long	tcp_recvspace = 1024*4;
37337323Skarels 
3745280Sroot /*
3755280Sroot  * Attach TCP protocol to socket, allocating
3765280Sroot  * internet protocol control block, tcp control block,
3775280Sroot  * bufer space, and entering LISTEN state if to accept connections.
3785280Sroot  */
3798272Sroot tcp_attach(so)
3805280Sroot 	struct socket *so;
3815280Sroot {
3825280Sroot 	register struct tcpcb *tp;
3835280Sroot 	struct inpcb *inp;
3845280Sroot 	int error;
3855280Sroot 
38634485Skarels 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
38734485Skarels 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
38834485Skarels 		if (error)
38934485Skarels 			return (error);
39034485Skarels 	}
3917511Sroot 	error = in_pcballoc(so, &tcb);
3927511Sroot 	if (error)
39317047Skarels 		return (error);
3948272Sroot 	inp = sotoinpcb(so);
3955280Sroot 	tp = tcp_newtcpcb(inp);
3967511Sroot 	if (tp == 0) {
39717047Skarels 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
39817047Skarels 
39917047Skarels 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
40017047Skarels 		in_pcbdetach(inp);
40117047Skarels 		so->so_state |= nofd;
40217047Skarels 		return (ENOBUFS);
4037511Sroot 	}
4048272Sroot 	tp->t_state = TCPS_CLOSED;
4055280Sroot 	return (0);
4065280Sroot }
4075280Sroot 
4085280Sroot /*
4095280Sroot  * Initiate (or continue) disconnect.
4105280Sroot  * If embryonic state, just send reset (once).
41113221Ssam  * If in ``let data drain'' option and linger null, just drop.
4125280Sroot  * Otherwise (hard), mark socket disconnecting and drop
4135280Sroot  * current input data; switch states based on user close, and
4145280Sroot  * send segment to peer (with FIN).
4155280Sroot  */
41610397Ssam struct tcpcb *
4175280Sroot tcp_disconnect(tp)
41810397Ssam 	register struct tcpcb *tp;
4195280Sroot {
4205280Sroot 	struct socket *so = tp->t_inpcb->inp_socket;
4215280Sroot 
4225280Sroot 	if (tp->t_state < TCPS_ESTABLISHED)
42310397Ssam 		tp = tcp_close(tp);
42413221Ssam 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
42510397Ssam 		tp = tcp_drop(tp, 0);
4265280Sroot 	else {
4275280Sroot 		soisdisconnecting(so);
4285280Sroot 		sbflush(&so->so_rcv);
42910397Ssam 		tp = tcp_usrclosed(tp);
43010397Ssam 		if (tp)
43110397Ssam 			(void) tcp_output(tp);
4325280Sroot 	}
43310397Ssam 	return (tp);
4345280Sroot }
4355280Sroot 
4365280Sroot /*
4375280Sroot  * User issued close, and wish to trail through shutdown states:
4385280Sroot  * if never received SYN, just forget it.  If got a SYN from peer,
4395280Sroot  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
4405280Sroot  * If already got a FIN from peer, then almost done; go to LAST_ACK
4415280Sroot  * state.  In all other cases, have already sent FIN to peer (e.g.
4425280Sroot  * after PRU_SHUTDOWN), and just have to play tedious game waiting
4435280Sroot  * for peer to send FIN or not respond to keep-alives, etc.
4445897Swnj  * We can let the user exit from the close as soon as the FIN is acked.
4455280Sroot  */
44610397Ssam struct tcpcb *
4475245Sroot tcp_usrclosed(tp)
44810397Ssam 	register struct tcpcb *tp;
4495245Sroot {
4505245Sroot 
4515245Sroot 	switch (tp->t_state) {
4525245Sroot 
45312438Ssam 	case TCPS_CLOSED:
4545245Sroot 	case TCPS_LISTEN:
4555245Sroot 	case TCPS_SYN_SENT:
4565245Sroot 		tp->t_state = TCPS_CLOSED;
45710397Ssam 		tp = tcp_close(tp);
4585245Sroot 		break;
4595245Sroot 
4605245Sroot 	case TCPS_SYN_RECEIVED:
4615245Sroot 	case TCPS_ESTABLISHED:
4625245Sroot 		tp->t_state = TCPS_FIN_WAIT_1;
4635245Sroot 		break;
4645245Sroot 
4655245Sroot 	case TCPS_CLOSE_WAIT:
4665245Sroot 		tp->t_state = TCPS_LAST_ACK;
4675245Sroot 		break;
4685245Sroot 	}
46910397Ssam 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2)
4705897Swnj 		soisdisconnected(tp->t_inpcb->inp_socket);
47110397Ssam 	return (tp);
4725245Sroot }
473