xref: /csrg-svn/sys/netinet/tcp_usrreq.c (revision 8406)
1*8406Swnj /*	tcp_usrreq.c	1.65	82/10/09	*/
24567Swnj 
34497Swnj #include "../h/param.h"
44567Swnj #include "../h/systm.h"
54664Swnj #include "../h/mbuf.h"
64664Swnj #include "../h/socket.h"
74809Swnj #include "../h/socketvar.h"
84809Swnj #include "../h/protosw.h"
9*8406Swnj #include "../netinet/in.h"
106353Ssam #include "../net/route.h"
11*8406Swnj #include "../netinet/in_pcb.h"
12*8406Swnj #include "../netinet/in_systm.h"
134954Swnj #include "../net/if.h"
14*8406Swnj #include "../netinet/ip.h"
15*8406Swnj #include "../netinet/ip_var.h"
16*8406Swnj #include "../netinet/tcp.h"
17*8406Swnj #include "../netinet/tcp_fsm.h"
18*8406Swnj #include "../netinet/tcp_seq.h"
19*8406Swnj #include "../netinet/tcp_timer.h"
20*8406Swnj #include "../netinet/tcp_var.h"
21*8406Swnj #include "../netinet/tcpip.h"
22*8406Swnj #include "../netinet/tcp_debug.h"
236506Ssam #include <errno.h>
244497Swnj 
255280Sroot /*
265280Sroot  * TCP protocol interface to socket abstraction.
275280Sroot  */
285280Sroot extern	char *tcpstates[];
294954Swnj struct	tcpcb *tcp_newtcpcb();
305280Sroot 
314734Swnj /*
325280Sroot  * Process a TCP user request for TCP tb.  If this is a send request
334731Swnj  * then m is the mbuf chain of send data.  If this is a timer expiration
344731Swnj  * (called from the software clock routine), then timertype tells which timer.
354731Swnj  */
368272Sroot tcp_usrreq(so, req, m, nam, opt)
374809Swnj 	struct socket *so;
384809Swnj 	int req;
398272Sroot 	struct mbuf *m, *nam;
408272Sroot 	struct socketopt *opt;
414497Swnj {
424886Swnj 	register struct inpcb *inp = sotoinpcb(so);
434911Swnj 	register struct tcpcb *tp;
444567Swnj 	int s = splnet();
454809Swnj 	int error = 0;
465270Sroot 	int ostate;
474497Swnj 
484886Swnj 	/*
495280Sroot 	 * When a TCP is attached to a socket, then there will be
505280Sroot 	 * a (struct inpcb) pointed at by the socket, and this
515280Sroot 	 * structure will point at a subsidary (struct tcpcb).
524886Swnj 	 */
535089Swnj 	if (inp == 0 && req != PRU_ATTACH) {
545075Swnj 		splx(s);
555280Sroot 		return (EINVAL);		/* XXX */
565075Swnj 	}
575075Swnj 	if (inp) {
584911Swnj 		tp = intotcpcb(inp);
598272Sroot 		/* WHAT IF TP IS 0? */
604731Swnj #ifdef KPROF
615075Swnj 		tcp_acounts[tp->t_state][req]++;
624731Swnj #endif
635270Sroot 		ostate = tp->t_state;
647511Sroot 	} else
657511Sroot 		ostate = 0;
664809Swnj 	switch (req) {
674497Swnj 
685280Sroot 	/*
695280Sroot 	 * TCP attaches to socket via PRU_ATTACH, reserving space,
708272Sroot 	 * and an internet control block.
715280Sroot 	 */
724809Swnj 	case PRU_ATTACH:
734954Swnj 		if (inp) {
744809Swnj 			error = EISCONN;
754911Swnj 			break;
764886Swnj 		}
778272Sroot 		error = tcp_attach(so, nam);
785075Swnj 		if (error)
794954Swnj 			break;
805392Swnj 		if ((so->so_options & SO_DONTLINGER) == 0)
815392Swnj 			so->so_linger = TCP_LINGERTIME;
825280Sroot 		tp = sototcpcb(so);
834567Swnj 		break;
844497Swnj 
855280Sroot 	/*
865280Sroot 	 * PRU_DETACH detaches the TCP protocol from the socket.
875280Sroot 	 * If the protocol state is non-embryonic, then can't
885280Sroot 	 * do this directly: have to initiate a PRU_DISCONNECT,
895280Sroot 	 * which may finish later; embryonic TCB's can just
905280Sroot 	 * be discarded here.
915280Sroot 	 */
924809Swnj 	case PRU_DETACH:
935280Sroot 		if (tp->t_state > TCPS_LISTEN)
945280Sroot 			tcp_disconnect(tp);
955280Sroot 		else {
965280Sroot 			tcp_close(tp);
975280Sroot 			tp = 0;
985280Sroot 		}
994809Swnj 		break;
1004809Swnj 
1015280Sroot 	/*
1028272Sroot 	 * Give the socket an address.
1038272Sroot 	 */
1048272Sroot 	case PRU_BIND:
1058272Sroot 		error = in_pcbbind(inp, nam);
1068272Sroot 		if (error)
1078272Sroot 			break;
1088272Sroot 		break;
1098272Sroot 
1108272Sroot 	/*
1118272Sroot 	 * Prepare to accept connections.
1128272Sroot 	 */
1138272Sroot 	case PRU_LISTEN:
1148272Sroot 		if (inp->inp_lport == 0)
1158272Sroot 			error = in_pcbbind(inp, (struct mbuf *)0);
1168272Sroot 		if (error == 0)
1178272Sroot 			tp->t_state = TCPS_LISTEN;
1188272Sroot 		break;
1198272Sroot 
1208272Sroot 	/*
1215280Sroot 	 * Initiate connection to peer.
1225280Sroot 	 * Create a template for use in transmissions on this connection.
1235280Sroot 	 * Enter SYN_SENT state, and mark socket as connecting.
1245280Sroot 	 * Start keep-alive timer, and seed output sequence space.
1255280Sroot 	 * Send initial segment on connection.
1265280Sroot 	 */
1274809Swnj 	case PRU_CONNECT:
1288272Sroot 		if (inp->inp_lport == 0) {
1298272Sroot 			error = in_pcbbind(inp, (struct mbuf *)0);
1308272Sroot 			if (error)
1318272Sroot 				break;
1328272Sroot 		}
1338272Sroot 		error = in_pcbconnect(inp, nam);
1344954Swnj 		if (error)
1354886Swnj 			break;
1365174Swnj 		tp->t_template = tcp_template(tp);
1375280Sroot 		if (tp->t_template == 0) {
1385280Sroot 			in_pcbdisconnect(inp);
1395280Sroot 			error = ENOBUFS;
1405280Sroot 			break;
1415280Sroot 		}
1424886Swnj 		soisconnecting(so);
1435075Swnj 		tp->t_state = TCPS_SYN_SENT;
1445245Sroot 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
1455245Sroot 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
1465245Sroot 		tcp_sendseqinit(tp);
1476506Ssam 		error = tcp_output(tp);
1484567Swnj 		break;
1494497Swnj 
1505280Sroot 	/*
1515280Sroot 	 * Initiate disconnect from peer.
1525280Sroot 	 * If connection never passed embryonic stage, just drop;
1535280Sroot 	 * else if don't need to let data drain, then can just drop anyways,
1545280Sroot 	 * else have to begin TCP shutdown process: mark socket disconnecting,
1555280Sroot 	 * drain unread data, state switch to reflect user close, and
1565280Sroot 	 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
1575280Sroot 	 * when peer sends FIN and acks ours.
1585280Sroot 	 *
1595280Sroot 	 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
1605280Sroot 	 */
1615280Sroot 	case PRU_DISCONNECT:
1625280Sroot 		tcp_disconnect(tp);
1635245Sroot 		break;
1645245Sroot 
1655280Sroot 	/*
1665280Sroot 	 * Accept a connection.  Essentially all the work is
1675280Sroot 	 * done at higher levels; just return the address
1685280Sroot 	 * of the peer, storing through addr.
1695280Sroot 	 */
1706117Swnj 	case PRU_ACCEPT: {
1718272Sroot 		struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
1726117Swnj 
1738272Sroot 		nam->m_len = sizeof (struct sockaddr_in);
1748272Sroot 		sin->sin_family = AF_INET;
1758272Sroot 		sin->sin_port = inp->inp_fport;
1768272Sroot 		sin->sin_addr = inp->inp_faddr;
1778272Sroot 		break;
1786117Swnj 		}
1794925Swnj 
1805280Sroot 	/*
1815280Sroot 	 * Mark the connection as being incapable of further output.
1825280Sroot 	 */
1834809Swnj 	case PRU_SHUTDOWN:
1845089Swnj 		socantsendmore(so);
1855245Sroot 		tcp_usrclosed(tp);
1866506Ssam 		error = tcp_output(tp);
1874567Swnj 		break;
1884497Swnj 
1895280Sroot 	/*
1905280Sroot 	 * After a receive, possibly send window update to peer.
1915280Sroot 	 */
1924809Swnj 	case PRU_RCVD:
1935113Swnj 		(void) tcp_output(tp);
1944567Swnj 		break;
1954497Swnj 
1965280Sroot 	/*
1975280Sroot 	 * Do a send by putting data in output queue and updating urgent
1985280Sroot 	 * marker if URG set.  Possibly send more data.
1995280Sroot 	 */
2004809Swnj 	case PRU_SEND:
2015075Swnj 		sbappend(&so->so_snd, m);
2026506Ssam #ifdef notdef
2035089Swnj 		if (tp->t_flags & TF_PUSH)
2045075Swnj 			tp->snd_end = tp->snd_una + so->so_snd.sb_cc;
2056506Ssam #endif
2066506Ssam 		error = tcp_output(tp);
2074567Swnj 		break;
2084567Swnj 
2095280Sroot 	/*
2105280Sroot 	 * Abort the TCP.
2115280Sroot 	 */
2124809Swnj 	case PRU_ABORT:
2135075Swnj 		tcp_drop(tp, ECONNABORTED);
2144567Swnj 		break;
2154567Swnj 
2165280Sroot /* SOME AS YET UNIMPLEMENTED HOOKS */
2174809Swnj 	case PRU_CONTROL:
2184886Swnj 		error = EOPNOTSUPP;
2194809Swnj 		break;
2204809Swnj 
2215113Swnj 	case PRU_SENSE:
2225113Swnj 		error = EOPNOTSUPP;
2235113Swnj 		break;
2245417Swnj /* END UNIMPLEMENTED HOOKS */
2255113Swnj 
2265113Swnj 	case PRU_RCVOOB:
2275442Swnj 		if (so->so_oobmark == 0 &&
2285442Swnj 		    (so->so_state & SS_RCVATMARK) == 0) {
2295417Swnj 			error = EINVAL;
2305417Swnj 			break;
2315417Swnj 		}
2325549Swnj 		if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
2335442Swnj 			error = EWOULDBLOCK;
2345549Swnj 			break;
2355442Swnj 		}
2368310Sroot 		m->m_len = 1;
2375549Swnj 		*mtod(m, caddr_t) = tp->t_iobc;
2385113Swnj 		break;
2395113Swnj 
2405113Swnj 	case PRU_SENDOOB:
2415442Swnj 		if (sbspace(&so->so_snd) < -512) {
2425442Swnj 			error = ENOBUFS;
2435442Swnj 			break;
2445442Swnj 		}
2455417Swnj 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc + 1;
2465417Swnj 		sbappend(&so->so_snd, m);
2475549Swnj 		tp->t_force = 1;
2486506Ssam 		error = tcp_output(tp);
2495549Swnj 		tp->t_force = 0;
2505113Swnj 		break;
2515113Swnj 
2526510Ssam 	case PRU_SOCKADDR:
2538272Sroot 		in_setsockaddr(inp, nam);
2546510Ssam 		break;
2556510Ssam 
2565280Sroot 	/*
2575280Sroot 	 * TCP slow timer went off; going through this
2585280Sroot 	 * routine for tracing's sake.
2595280Sroot 	 */
2604809Swnj 	case PRU_SLOWTIMO:
2618272Sroot 		tcp_timers(tp, (int)nam);
2628272Sroot 		req |= (int)nam << 8;		/* for debug's sake */
2634809Swnj 		break;
2644809Swnj 
2654731Swnj 	default:
2664731Swnj 		panic("tcp_usrreq");
2674567Swnj 	}
2685270Sroot 	if (tp && (so->so_options & SO_DEBUG))
2695270Sroot 		tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req);
2704567Swnj 	splx(s);
2714886Swnj 	return (error);
2724497Swnj }
2735245Sroot 
2745953Swnj int	tcp_sendspace = 1024*2;
2756601Ssam int	tcp_recvspace = 1024*2;
2765280Sroot /*
2775280Sroot  * Attach TCP protocol to socket, allocating
2785280Sroot  * internet protocol control block, tcp control block,
2795280Sroot  * bufer space, and entering LISTEN state if to accept connections.
2805280Sroot  */
2818272Sroot tcp_attach(so)
2825280Sroot 	struct socket *so;
2835280Sroot {
2845280Sroot 	register struct tcpcb *tp;
2855280Sroot 	struct inpcb *inp;
2865280Sroot 	int error;
2875280Sroot 
2887511Sroot 	error = in_pcbreserve(so, tcp_sendspace, tcp_recvspace);
2895280Sroot 	if (error)
2907511Sroot 		goto bad;
2917511Sroot 	error = in_pcballoc(so, &tcb);
2927511Sroot 	if (error)
2938272Sroot 		goto bad;
2948272Sroot 	inp = sotoinpcb(so);
2955280Sroot 	tp = tcp_newtcpcb(inp);
2967511Sroot 	if (tp == 0) {
2977511Sroot 		error = ENOBUFS;
2987511Sroot 		goto bad2;
2997511Sroot 	}
3008272Sroot 	tp->t_state = TCPS_CLOSED;
3015280Sroot 	return (0);
3027511Sroot bad2:
3037511Sroot 	in_pcbdetach(inp);
3047511Sroot bad:
3057511Sroot 	return (error);
3065280Sroot }
3075280Sroot 
3085280Sroot /*
3095280Sroot  * Initiate (or continue) disconnect.
3105280Sroot  * If embryonic state, just send reset (once).
3115280Sroot  * If not in ``let data drain'' option, just drop.
3125280Sroot  * Otherwise (hard), mark socket disconnecting and drop
3135280Sroot  * current input data; switch states based on user close, and
3145280Sroot  * send segment to peer (with FIN).
3155280Sroot  */
3165280Sroot tcp_disconnect(tp)
3175280Sroot 	struct tcpcb *tp;
3185280Sroot {
3195280Sroot 	struct socket *so = tp->t_inpcb->inp_socket;
3205280Sroot 
3215280Sroot 	if (tp->t_state < TCPS_ESTABLISHED)
3225280Sroot 		tcp_close(tp);
3235392Swnj 	else if (so->so_linger == 0)
3245280Sroot 		tcp_drop(tp, 0);
3255280Sroot 	else {
3265280Sroot 		soisdisconnecting(so);
3275280Sroot 		sbflush(&so->so_rcv);
3285280Sroot 		tcp_usrclosed(tp);
3295280Sroot 		(void) tcp_output(tp);
3305280Sroot 	}
3315280Sroot }
3325280Sroot 
3335280Sroot /*
3345280Sroot  * User issued close, and wish to trail through shutdown states:
3355280Sroot  * if never received SYN, just forget it.  If got a SYN from peer,
3365280Sroot  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
3375280Sroot  * If already got a FIN from peer, then almost done; go to LAST_ACK
3385280Sroot  * state.  In all other cases, have already sent FIN to peer (e.g.
3395280Sroot  * after PRU_SHUTDOWN), and just have to play tedious game waiting
3405280Sroot  * for peer to send FIN or not respond to keep-alives, etc.
3415897Swnj  * We can let the user exit from the close as soon as the FIN is acked.
3425280Sroot  */
3435245Sroot tcp_usrclosed(tp)
3445245Sroot 	struct tcpcb *tp;
3455245Sroot {
3465245Sroot 
3475245Sroot 	switch (tp->t_state) {
3485245Sroot 
3495245Sroot 	case TCPS_LISTEN:
3505245Sroot 	case TCPS_SYN_SENT:
3515245Sroot 		tp->t_state = TCPS_CLOSED;
3525245Sroot 		tcp_close(tp);
3535245Sroot 		break;
3545245Sroot 
3555245Sroot 	case TCPS_SYN_RECEIVED:
3565245Sroot 	case TCPS_ESTABLISHED:
3575245Sroot 		tp->t_state = TCPS_FIN_WAIT_1;
3585245Sroot 		break;
3595245Sroot 
3605245Sroot 	case TCPS_CLOSE_WAIT:
3615245Sroot 		tp->t_state = TCPS_LAST_ACK;
3625245Sroot 		break;
3635245Sroot 	}
3645897Swnj 	if (tp->t_state >= TCPS_FIN_WAIT_2)
3655897Swnj 		soisdisconnected(tp->t_inpcb->inp_socket);
3665245Sroot }
367