xref: /csrg-svn/sys/netinet/tcp_usrreq.c (revision 8697)
1*8697Sroot /*	tcp_usrreq.c	1.68	82/10/20	*/
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*8697Sroot 
10*8697Sroot #include "../net/if.h"
11*8697Sroot #include "../net/route.h"
128406Swnj #include "../netinet/in.h"
138406Swnj #include "../netinet/in_pcb.h"
148406Swnj #include "../netinet/in_systm.h"
158406Swnj #include "../netinet/ip.h"
168406Swnj #include "../netinet/ip_var.h"
178406Swnj #include "../netinet/tcp.h"
188406Swnj #include "../netinet/tcp_fsm.h"
198406Swnj #include "../netinet/tcp_seq.h"
208406Swnj #include "../netinet/tcp_timer.h"
218406Swnj #include "../netinet/tcp_var.h"
228406Swnj #include "../netinet/tcpip.h"
238406Swnj #include "../netinet/tcp_debug.h"
246506Ssam #include <errno.h>
254497Swnj 
265280Sroot /*
275280Sroot  * TCP protocol interface to socket abstraction.
285280Sroot  */
295280Sroot extern	char *tcpstates[];
304954Swnj struct	tcpcb *tcp_newtcpcb();
315280Sroot 
324734Swnj /*
335280Sroot  * Process a TCP user request for TCP tb.  If this is a send request
344731Swnj  * then m is the mbuf chain of send data.  If this is a timer expiration
354731Swnj  * (called from the software clock routine), then timertype tells which timer.
364731Swnj  */
378601Sroot /*ARGSUSED*/
388272Sroot tcp_usrreq(so, req, m, nam, opt)
394809Swnj 	struct socket *so;
404809Swnj 	int req;
418272Sroot 	struct mbuf *m, *nam;
428272Sroot 	struct socketopt *opt;
434497Swnj {
444886Swnj 	register struct inpcb *inp = sotoinpcb(so);
454911Swnj 	register struct tcpcb *tp;
464567Swnj 	int s = splnet();
474809Swnj 	int error = 0;
485270Sroot 	int ostate;
494497Swnj 
504886Swnj 	/*
515280Sroot 	 * When a TCP is attached to a socket, then there will be
525280Sroot 	 * a (struct inpcb) pointed at by the socket, and this
535280Sroot 	 * structure will point at a subsidary (struct tcpcb).
544886Swnj 	 */
555089Swnj 	if (inp == 0 && req != PRU_ATTACH) {
565075Swnj 		splx(s);
575280Sroot 		return (EINVAL);		/* XXX */
585075Swnj 	}
595075Swnj 	if (inp) {
604911Swnj 		tp = intotcpcb(inp);
618272Sroot 		/* WHAT IF TP IS 0? */
624731Swnj #ifdef KPROF
635075Swnj 		tcp_acounts[tp->t_state][req]++;
644731Swnj #endif
655270Sroot 		ostate = tp->t_state;
667511Sroot 	} else
677511Sroot 		ostate = 0;
684809Swnj 	switch (req) {
694497Swnj 
705280Sroot 	/*
715280Sroot 	 * TCP attaches to socket via PRU_ATTACH, reserving space,
728272Sroot 	 * and an internet control block.
735280Sroot 	 */
744809Swnj 	case PRU_ATTACH:
754954Swnj 		if (inp) {
764809Swnj 			error = EISCONN;
774911Swnj 			break;
784886Swnj 		}
798640Sroot 		error = tcp_attach(so);
805075Swnj 		if (error)
814954Swnj 			break;
825392Swnj 		if ((so->so_options & SO_DONTLINGER) == 0)
835392Swnj 			so->so_linger = TCP_LINGERTIME;
845280Sroot 		tp = sototcpcb(so);
854567Swnj 		break;
864497Swnj 
875280Sroot 	/*
885280Sroot 	 * PRU_DETACH detaches the TCP protocol from the socket.
895280Sroot 	 * If the protocol state is non-embryonic, then can't
905280Sroot 	 * do this directly: have to initiate a PRU_DISCONNECT,
915280Sroot 	 * which may finish later; embryonic TCB's can just
925280Sroot 	 * be discarded here.
935280Sroot 	 */
944809Swnj 	case PRU_DETACH:
955280Sroot 		if (tp->t_state > TCPS_LISTEN)
965280Sroot 			tcp_disconnect(tp);
975280Sroot 		else {
985280Sroot 			tcp_close(tp);
995280Sroot 			tp = 0;
1005280Sroot 		}
1014809Swnj 		break;
1024809Swnj 
1035280Sroot 	/*
1048272Sroot 	 * Give the socket an address.
1058272Sroot 	 */
1068272Sroot 	case PRU_BIND:
1078272Sroot 		error = in_pcbbind(inp, nam);
1088272Sroot 		if (error)
1098272Sroot 			break;
1108272Sroot 		break;
1118272Sroot 
1128272Sroot 	/*
1138272Sroot 	 * Prepare to accept connections.
1148272Sroot 	 */
1158272Sroot 	case PRU_LISTEN:
1168272Sroot 		if (inp->inp_lport == 0)
1178272Sroot 			error = in_pcbbind(inp, (struct mbuf *)0);
1188272Sroot 		if (error == 0)
1198272Sroot 			tp->t_state = TCPS_LISTEN;
1208272Sroot 		break;
1218272Sroot 
1228272Sroot 	/*
1235280Sroot 	 * Initiate connection to peer.
1245280Sroot 	 * Create a template for use in transmissions on this connection.
1255280Sroot 	 * Enter SYN_SENT state, and mark socket as connecting.
1265280Sroot 	 * Start keep-alive timer, and seed output sequence space.
1275280Sroot 	 * Send initial segment on connection.
1285280Sroot 	 */
1294809Swnj 	case PRU_CONNECT:
1308272Sroot 		if (inp->inp_lport == 0) {
1318272Sroot 			error = in_pcbbind(inp, (struct mbuf *)0);
1328272Sroot 			if (error)
1338272Sroot 				break;
1348272Sroot 		}
1358272Sroot 		error = in_pcbconnect(inp, nam);
1364954Swnj 		if (error)
1374886Swnj 			break;
1385174Swnj 		tp->t_template = tcp_template(tp);
1395280Sroot 		if (tp->t_template == 0) {
1405280Sroot 			in_pcbdisconnect(inp);
1415280Sroot 			error = ENOBUFS;
1425280Sroot 			break;
1435280Sroot 		}
1444886Swnj 		soisconnecting(so);
1455075Swnj 		tp->t_state = TCPS_SYN_SENT;
1465245Sroot 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
1475245Sroot 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
1485245Sroot 		tcp_sendseqinit(tp);
1496506Ssam 		error = tcp_output(tp);
1504567Swnj 		break;
1514497Swnj 
1525280Sroot 	/*
1535280Sroot 	 * Initiate disconnect from peer.
1545280Sroot 	 * If connection never passed embryonic stage, just drop;
1555280Sroot 	 * else if don't need to let data drain, then can just drop anyways,
1565280Sroot 	 * else have to begin TCP shutdown process: mark socket disconnecting,
1575280Sroot 	 * drain unread data, state switch to reflect user close, and
1585280Sroot 	 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
1595280Sroot 	 * when peer sends FIN and acks ours.
1605280Sroot 	 *
1615280Sroot 	 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
1625280Sroot 	 */
1635280Sroot 	case PRU_DISCONNECT:
1645280Sroot 		tcp_disconnect(tp);
1655245Sroot 		break;
1665245Sroot 
1675280Sroot 	/*
1685280Sroot 	 * Accept a connection.  Essentially all the work is
1695280Sroot 	 * done at higher levels; just return the address
1705280Sroot 	 * of the peer, storing through addr.
1715280Sroot 	 */
1726117Swnj 	case PRU_ACCEPT: {
1738272Sroot 		struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
1746117Swnj 
1758272Sroot 		nam->m_len = sizeof (struct sockaddr_in);
1768272Sroot 		sin->sin_family = AF_INET;
1778272Sroot 		sin->sin_port = inp->inp_fport;
1788272Sroot 		sin->sin_addr = inp->inp_faddr;
1798272Sroot 		break;
1806117Swnj 		}
1814925Swnj 
1825280Sroot 	/*
1835280Sroot 	 * Mark the connection as being incapable of further output.
1845280Sroot 	 */
1854809Swnj 	case PRU_SHUTDOWN:
1865089Swnj 		socantsendmore(so);
1875245Sroot 		tcp_usrclosed(tp);
1886506Ssam 		error = tcp_output(tp);
1894567Swnj 		break;
1904497Swnj 
1915280Sroot 	/*
1925280Sroot 	 * After a receive, possibly send window update to peer.
1935280Sroot 	 */
1944809Swnj 	case PRU_RCVD:
1955113Swnj 		(void) tcp_output(tp);
1964567Swnj 		break;
1974497Swnj 
1985280Sroot 	/*
1995280Sroot 	 * Do a send by putting data in output queue and updating urgent
2005280Sroot 	 * marker if URG set.  Possibly send more data.
2015280Sroot 	 */
2024809Swnj 	case PRU_SEND:
2035075Swnj 		sbappend(&so->so_snd, m);
2046506Ssam #ifdef notdef
2055089Swnj 		if (tp->t_flags & TF_PUSH)
2065075Swnj 			tp->snd_end = tp->snd_una + so->so_snd.sb_cc;
2076506Ssam #endif
2086506Ssam 		error = tcp_output(tp);
2094567Swnj 		break;
2104567Swnj 
2115280Sroot 	/*
2125280Sroot 	 * Abort the TCP.
2135280Sroot 	 */
2144809Swnj 	case PRU_ABORT:
2155075Swnj 		tcp_drop(tp, ECONNABORTED);
2164567Swnj 		break;
2174567Swnj 
2185280Sroot /* SOME AS YET UNIMPLEMENTED HOOKS */
2194809Swnj 	case PRU_CONTROL:
2204886Swnj 		error = EOPNOTSUPP;
2214809Swnj 		break;
2224809Swnj 
2235113Swnj 	case PRU_SENSE:
2245113Swnj 		error = EOPNOTSUPP;
2255113Swnj 		break;
2265417Swnj /* END UNIMPLEMENTED HOOKS */
2275113Swnj 
2285113Swnj 	case PRU_RCVOOB:
2295442Swnj 		if (so->so_oobmark == 0 &&
2305442Swnj 		    (so->so_state & SS_RCVATMARK) == 0) {
2315417Swnj 			error = EINVAL;
2325417Swnj 			break;
2335417Swnj 		}
2345549Swnj 		if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
2355442Swnj 			error = EWOULDBLOCK;
2365549Swnj 			break;
2375442Swnj 		}
2388310Sroot 		m->m_len = 1;
2395549Swnj 		*mtod(m, caddr_t) = tp->t_iobc;
2405113Swnj 		break;
2415113Swnj 
2425113Swnj 	case PRU_SENDOOB:
2435442Swnj 		if (sbspace(&so->so_snd) < -512) {
2445442Swnj 			error = ENOBUFS;
2455442Swnj 			break;
2465442Swnj 		}
2475417Swnj 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc + 1;
2485417Swnj 		sbappend(&so->so_snd, m);
2495549Swnj 		tp->t_force = 1;
2506506Ssam 		error = tcp_output(tp);
2515549Swnj 		tp->t_force = 0;
2525113Swnj 		break;
2535113Swnj 
2546510Ssam 	case PRU_SOCKADDR:
2558272Sroot 		in_setsockaddr(inp, nam);
2566510Ssam 		break;
2576510Ssam 
2585280Sroot 	/*
2595280Sroot 	 * TCP slow timer went off; going through this
2605280Sroot 	 * routine for tracing's sake.
2615280Sroot 	 */
2624809Swnj 	case PRU_SLOWTIMO:
2638272Sroot 		tcp_timers(tp, (int)nam);
2648272Sroot 		req |= (int)nam << 8;		/* for debug's sake */
2654809Swnj 		break;
2664809Swnj 
2674731Swnj 	default:
2684731Swnj 		panic("tcp_usrreq");
2694567Swnj 	}
2705270Sroot 	if (tp && (so->so_options & SO_DEBUG))
2715270Sroot 		tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req);
2724567Swnj 	splx(s);
2734886Swnj 	return (error);
2744497Swnj }
2755245Sroot 
2765953Swnj int	tcp_sendspace = 1024*2;
2776601Ssam int	tcp_recvspace = 1024*2;
2785280Sroot /*
2795280Sroot  * Attach TCP protocol to socket, allocating
2805280Sroot  * internet protocol control block, tcp control block,
2815280Sroot  * bufer space, and entering LISTEN state if to accept connections.
2825280Sroot  */
2838272Sroot tcp_attach(so)
2845280Sroot 	struct socket *so;
2855280Sroot {
2865280Sroot 	register struct tcpcb *tp;
2875280Sroot 	struct inpcb *inp;
2885280Sroot 	int error;
2895280Sroot 
2907511Sroot 	error = in_pcbreserve(so, tcp_sendspace, tcp_recvspace);
2915280Sroot 	if (error)
2927511Sroot 		goto bad;
2937511Sroot 	error = in_pcballoc(so, &tcb);
2947511Sroot 	if (error)
2958272Sroot 		goto bad;
2968272Sroot 	inp = sotoinpcb(so);
2975280Sroot 	tp = tcp_newtcpcb(inp);
2987511Sroot 	if (tp == 0) {
2997511Sroot 		error = ENOBUFS;
3007511Sroot 		goto bad2;
3017511Sroot 	}
3028272Sroot 	tp->t_state = TCPS_CLOSED;
3035280Sroot 	return (0);
3047511Sroot bad2:
3057511Sroot 	in_pcbdetach(inp);
3067511Sroot bad:
3077511Sroot 	return (error);
3085280Sroot }
3095280Sroot 
3105280Sroot /*
3115280Sroot  * Initiate (or continue) disconnect.
3125280Sroot  * If embryonic state, just send reset (once).
3135280Sroot  * If not in ``let data drain'' option, just drop.
3145280Sroot  * Otherwise (hard), mark socket disconnecting and drop
3155280Sroot  * current input data; switch states based on user close, and
3165280Sroot  * send segment to peer (with FIN).
3175280Sroot  */
3185280Sroot tcp_disconnect(tp)
3195280Sroot 	struct tcpcb *tp;
3205280Sroot {
3215280Sroot 	struct socket *so = tp->t_inpcb->inp_socket;
3225280Sroot 
3235280Sroot 	if (tp->t_state < TCPS_ESTABLISHED)
3245280Sroot 		tcp_close(tp);
3255392Swnj 	else if (so->so_linger == 0)
3265280Sroot 		tcp_drop(tp, 0);
3275280Sroot 	else {
3285280Sroot 		soisdisconnecting(so);
3295280Sroot 		sbflush(&so->so_rcv);
3305280Sroot 		tcp_usrclosed(tp);
3315280Sroot 		(void) tcp_output(tp);
3325280Sroot 	}
3335280Sroot }
3345280Sroot 
3355280Sroot /*
3365280Sroot  * User issued close, and wish to trail through shutdown states:
3375280Sroot  * if never received SYN, just forget it.  If got a SYN from peer,
3385280Sroot  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
3395280Sroot  * If already got a FIN from peer, then almost done; go to LAST_ACK
3405280Sroot  * state.  In all other cases, have already sent FIN to peer (e.g.
3415280Sroot  * after PRU_SHUTDOWN), and just have to play tedious game waiting
3425280Sroot  * for peer to send FIN or not respond to keep-alives, etc.
3435897Swnj  * We can let the user exit from the close as soon as the FIN is acked.
3445280Sroot  */
3455245Sroot tcp_usrclosed(tp)
3465245Sroot 	struct tcpcb *tp;
3475245Sroot {
3485245Sroot 
3495245Sroot 	switch (tp->t_state) {
3505245Sroot 
3515245Sroot 	case TCPS_LISTEN:
3525245Sroot 	case TCPS_SYN_SENT:
3535245Sroot 		tp->t_state = TCPS_CLOSED;
3545245Sroot 		tcp_close(tp);
3555245Sroot 		break;
3565245Sroot 
3575245Sroot 	case TCPS_SYN_RECEIVED:
3585245Sroot 	case TCPS_ESTABLISHED:
3595245Sroot 		tp->t_state = TCPS_FIN_WAIT_1;
3605245Sroot 		break;
3615245Sroot 
3625245Sroot 	case TCPS_CLOSE_WAIT:
3635245Sroot 		tp->t_state = TCPS_LAST_ACK;
3645245Sroot 		break;
3655245Sroot 	}
3665897Swnj 	if (tp->t_state >= TCPS_FIN_WAIT_2)
3675897Swnj 		soisdisconnected(tp->t_inpcb->inp_socket);
3685245Sroot }
369