xref: /csrg-svn/sys/netinet/tcp_usrreq.c (revision 5417)
1*5417Swnj /* tcp_usrreq.c 1.47 82/01/17 */
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"
95089Swnj #include "../net/in.h"
105089Swnj #include "../net/in_pcb.h"
115089Swnj #include "../net/in_systm.h"
124954Swnj #include "../net/if.h"
134809Swnj #include "../net/ip.h"
144900Swnj #include "../net/ip_var.h"
154809Swnj #include "../net/tcp.h"
164809Swnj #include "../net/tcp_fsm.h"
175089Swnj #include "../net/tcp_seq.h"
185089Swnj #include "../net/tcp_timer.h"
194809Swnj #include "../net/tcp_var.h"
205089Swnj #include "../net/tcpip.h"
215270Sroot #include "../net/tcp_debug.h"
225113Swnj #include "../errno.h"
234497Swnj 
245280Sroot /*
255280Sroot  * TCP protocol interface to socket abstraction.
265280Sroot  */
275280Sroot extern	char *tcpstates[];
284954Swnj struct	tcpcb *tcp_newtcpcb();
295280Sroot 
304734Swnj /*
315280Sroot  * Process a TCP user request for TCP tb.  If this is a send request
324731Swnj  * then m is the mbuf chain of send data.  If this is a timer expiration
334731Swnj  * (called from the software clock routine), then timertype tells which timer.
344731Swnj  */
354809Swnj tcp_usrreq(so, req, m, addr)
364809Swnj 	struct socket *so;
374809Swnj 	int req;
384731Swnj 	struct mbuf *m;
394809Swnj 	caddr_t addr;
404497Swnj {
414886Swnj 	register struct inpcb *inp = sotoinpcb(so);
424911Swnj 	register struct tcpcb *tp;
434567Swnj 	int s = splnet();
444809Swnj 	int error = 0;
455270Sroot 	int ostate;
464567Swnj COUNT(TCP_USRREQ);
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).
525280Sroot 	 * The normal sequence of events is:
535280Sroot 	 *	PRU_ATTACH		creating these structures
545280Sroot 	 *	PRU_CONNECT		connecting to a remote peer
555280Sroot 	 *	(PRU_SEND|PRU_RCVD)*	exchanging data
565280Sroot 	 *	PRU_DISCONNECT		disconnecting from remote peer
575280Sroot 	 *	PRU_DETACH		deleting the structures
585280Sroot 	 * With the operations from PRU_CONNECT through PRU_DISCONNECT
595280Sroot 	 * possible repeated several times.
605280Sroot 	 *
615280Sroot 	 * MULTIPLE CONNECTS ARE NOT YET IMPLEMENTED.
624886Swnj 	 */
635089Swnj 	if (inp == 0 && req != PRU_ATTACH) {
645075Swnj 		splx(s);
655280Sroot 		return (EINVAL);		/* XXX */
665075Swnj 	}
675075Swnj 	if (inp) {
684911Swnj 		tp = intotcpcb(inp);
694731Swnj #ifdef KPROF
705075Swnj 		tcp_acounts[tp->t_state][req]++;
714731Swnj #endif
725270Sroot 		ostate = tp->t_state;
734911Swnj 	}
744809Swnj 	switch (req) {
754497Swnj 
765280Sroot 	/*
775280Sroot 	 * TCP attaches to socket via PRU_ATTACH, reserving space,
785280Sroot 	 * and internet and TCP control blocks.
795280Sroot 	 * If the socket is to receive connections,
805280Sroot 	 * then the LISTEN state is entered.
815280Sroot 	 */
824809Swnj 	case PRU_ATTACH:
834954Swnj 		if (inp) {
844809Swnj 			error = EISCONN;
854911Swnj 			break;
864886Swnj 		}
875280Sroot 		error = tcp_attach(so, (struct sockaddr *)addr);
885075Swnj 		if (error)
894954Swnj 			break;
905392Swnj 		if ((so->so_options & SO_DONTLINGER) == 0)
915392Swnj 			so->so_linger = TCP_LINGERTIME;
925280Sroot 		tp = sototcpcb(so);
934567Swnj 		break;
944497Swnj 
955280Sroot 	/*
965280Sroot 	 * PRU_DETACH detaches the TCP protocol from the socket.
975280Sroot 	 * If the protocol state is non-embryonic, then can't
985280Sroot 	 * do this directly: have to initiate a PRU_DISCONNECT,
995280Sroot 	 * which may finish later; embryonic TCB's can just
1005280Sroot 	 * be discarded here.
1015280Sroot 	 */
1024809Swnj 	case PRU_DETACH:
1035280Sroot 		if (tp->t_state > TCPS_LISTEN)
1045280Sroot 			tcp_disconnect(tp);
1055280Sroot 		else {
1065280Sroot 			tcp_close(tp);
1075280Sroot 			tp = 0;
1085280Sroot 		}
1094809Swnj 		break;
1104809Swnj 
1115280Sroot 	/*
1125280Sroot 	 * Initiate connection to peer.
1135280Sroot 	 * Create a template for use in transmissions on this connection.
1145280Sroot 	 * Enter SYN_SENT state, and mark socket as connecting.
1155280Sroot 	 * Start keep-alive timer, and seed output sequence space.
1165280Sroot 	 * Send initial segment on connection.
1175280Sroot 	 */
1184809Swnj 	case PRU_CONNECT:
1195165Swnj 		error = in_pcbconnect(inp, (struct sockaddr_in *)addr);
1204954Swnj 		if (error)
1214886Swnj 			break;
1225174Swnj 		tp->t_template = tcp_template(tp);
1235280Sroot 		if (tp->t_template == 0) {
1245280Sroot 			in_pcbdisconnect(inp);
1255280Sroot 			error = ENOBUFS;
1265280Sroot 			break;
1275280Sroot 		}
1284886Swnj 		soisconnecting(so);
1295075Swnj 		tp->t_state = TCPS_SYN_SENT;
1305245Sroot 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
1315245Sroot 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
1325245Sroot 		tcp_sendseqinit(tp);
1335113Swnj 		(void) tcp_output(tp);
1344567Swnj 		break;
1354497Swnj 
1365280Sroot 	/*
1375280Sroot 	 * Initiate disconnect from peer.
1385280Sroot 	 * If connection never passed embryonic stage, just drop;
1395280Sroot 	 * else if don't need to let data drain, then can just drop anyways,
1405280Sroot 	 * else have to begin TCP shutdown process: mark socket disconnecting,
1415280Sroot 	 * drain unread data, state switch to reflect user close, and
1425280Sroot 	 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
1435280Sroot 	 * when peer sends FIN and acks ours.
1445280Sroot 	 *
1455280Sroot 	 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
1465280Sroot 	 */
1475280Sroot 	case PRU_DISCONNECT:
1485280Sroot 		tcp_disconnect(tp);
1495245Sroot 		break;
1505245Sroot 
1515280Sroot 	/*
1525280Sroot 	 * Accept a connection.  Essentially all the work is
1535280Sroot 	 * done at higher levels; just return the address
1545280Sroot 	 * of the peer, storing through addr.
1555280Sroot 	 */
1564925Swnj 	case PRU_ACCEPT:
1575280Sroot 		in_pcbconnaddr(inp, (struct sockaddr *)addr);
1584954Swnj 		break;
1594925Swnj 
1605280Sroot 	/*
1615280Sroot 	 * Mark the connection as being incapable of further output.
1625280Sroot 	 */
1634809Swnj 	case PRU_SHUTDOWN:
1645089Swnj 		socantsendmore(so);
1655245Sroot 		tcp_usrclosed(tp);
1665245Sroot 		(void) tcp_output(tp);
1674567Swnj 		break;
1684497Swnj 
1695280Sroot 	/*
1705280Sroot 	 * After a receive, possibly send window update to peer.
1715280Sroot 	 */
1724809Swnj 	case PRU_RCVD:
1735113Swnj 		(void) tcp_output(tp);
1744567Swnj 		break;
1754497Swnj 
1765280Sroot 	/*
1775280Sroot 	 * Do a send by putting data in output queue and updating urgent
1785280Sroot 	 * marker if URG set.  Possibly send more data.
1795280Sroot 	 */
1804809Swnj 	case PRU_SEND:
1815075Swnj 		sbappend(&so->so_snd, m);
1825089Swnj /*
1835089Swnj 		if (tp->t_flags & TF_PUSH)
1845075Swnj 			tp->snd_end = tp->snd_una + so->so_snd.sb_cc;
1855089Swnj  */
1865113Swnj 		(void) tcp_output(tp);
1874567Swnj 		break;
1884567Swnj 
1895280Sroot 	/*
1905280Sroot 	 * Abort the TCP.
1915280Sroot 	 */
1924809Swnj 	case PRU_ABORT:
1935075Swnj 		tcp_drop(tp, ECONNABORTED);
1944567Swnj 		break;
1954567Swnj 
1965280Sroot /* SOME AS YET UNIMPLEMENTED HOOKS */
1974809Swnj 	case PRU_CONTROL:
1984886Swnj 		error = EOPNOTSUPP;
1994809Swnj 		break;
2004809Swnj 
2015113Swnj 	case PRU_SENSE:
2025113Swnj 		error = EOPNOTSUPP;
2035113Swnj 		break;
204*5417Swnj /* END UNIMPLEMENTED HOOKS */
2055113Swnj 
2065113Swnj 	case PRU_RCVOOB:
207*5417Swnj 		if (tp->t_haveoob == 0) {
208*5417Swnj 			error = EINVAL;
209*5417Swnj 			break;
210*5417Swnj 		}
211*5417Swnj 		*mtod(m, caddr_t) = tp->t_oobc;
212*5417Swnj 		tp->t_haveoob = 0;
2135113Swnj 		break;
2145113Swnj 
2155113Swnj 	case PRU_SENDOOB:
216*5417Swnj 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc + 1;
217*5417Swnj 		sbappend(&so->so_snd, m);
218*5417Swnj /*
219*5417Swnj 		if (tp->t_flags & TF_PUSH)
220*5417Swnj 			tp->snd_end = tp->snd_una + so->so_snd.sb_cc;
221*5417Swnj  */
222*5417Swnj 		(void) tcp_output(tp);
2235113Swnj 		break;
2245113Swnj 
2255280Sroot 	/*
2265280Sroot 	 * TCP slow timer went off; going through this
2275280Sroot 	 * routine for tracing's sake.
2285280Sroot 	 */
2294809Swnj 	case PRU_SLOWTIMO:
2305075Swnj 		tcp_timers(tp, (int)addr);
2315270Sroot 		req |= (int)addr << 8;		/* for debug's sake */
2324809Swnj 		break;
2334809Swnj 
2344731Swnj 	default:
2354731Swnj 		panic("tcp_usrreq");
2364567Swnj 	}
2375270Sroot 	if (tp && (so->so_options & SO_DEBUG))
2385270Sroot 		tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req);
2394567Swnj 	splx(s);
2404886Swnj 	return (error);
2414497Swnj }
2425245Sroot 
2435280Sroot /*
2445280Sroot  * Attach TCP protocol to socket, allocating
2455280Sroot  * internet protocol control block, tcp control block,
2465280Sroot  * bufer space, and entering LISTEN state if to accept connections.
2475280Sroot  */
2485280Sroot tcp_attach(so, sa)
2495280Sroot 	struct socket *so;
2505280Sroot 	struct sockaddr *sa;
2515280Sroot {
2525280Sroot 	register struct tcpcb *tp;
2535280Sroot 	struct inpcb *inp;
2545280Sroot 	int error;
2555280Sroot 
2565280Sroot 	error = in_pcbattach(so, &tcb, 2048, 2048, (struct sockaddr_in *)sa);
2575280Sroot 	if (error)
2585280Sroot 		return (error);
2595280Sroot 	inp = (struct inpcb *)so->so_pcb;
2605280Sroot 	tp = tcp_newtcpcb(inp);
2615280Sroot 	if (so->so_options & SO_ACCEPTCONN) {
2625280Sroot 		if (tp == 0) {
2635280Sroot 			in_pcbdetach(inp);
2645280Sroot 			return (ENOBUFS);
2655280Sroot 		}
2665280Sroot 		tp->t_state = TCPS_LISTEN;
2675280Sroot 	} else
2685280Sroot 		tp->t_state = TCPS_CLOSED;
2695280Sroot 	return (0);
2705280Sroot }
2715280Sroot 
2725280Sroot /*
2735280Sroot  * Initiate (or continue) disconnect.
2745280Sroot  * If embryonic state, just send reset (once).
2755280Sroot  * If not in ``let data drain'' option, just drop.
2765280Sroot  * Otherwise (hard), mark socket disconnecting and drop
2775280Sroot  * current input data; switch states based on user close, and
2785280Sroot  * send segment to peer (with FIN).
2795280Sroot  */
2805280Sroot tcp_disconnect(tp)
2815280Sroot 	struct tcpcb *tp;
2825280Sroot {
2835280Sroot 	struct socket *so = tp->t_inpcb->inp_socket;
2845280Sroot 
2855280Sroot 	if (tp->t_state < TCPS_ESTABLISHED)
2865280Sroot 		tcp_close(tp);
2875392Swnj 	else if (so->so_linger == 0)
2885280Sroot 		tcp_drop(tp, 0);
2895280Sroot 	else {
2905280Sroot 		soisdisconnecting(so);
2915280Sroot 		sbflush(&so->so_rcv);
2925280Sroot 		tcp_usrclosed(tp);
2935280Sroot 		(void) tcp_output(tp);
2945280Sroot 	}
2955280Sroot }
2965280Sroot 
2975280Sroot /*
2985280Sroot  * User issued close, and wish to trail through shutdown states:
2995280Sroot  * if never received SYN, just forget it.  If got a SYN from peer,
3005280Sroot  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
3015280Sroot  * If already got a FIN from peer, then almost done; go to LAST_ACK
3025280Sroot  * state.  In all other cases, have already sent FIN to peer (e.g.
3035280Sroot  * after PRU_SHUTDOWN), and just have to play tedious game waiting
3045280Sroot  * for peer to send FIN or not respond to keep-alives, etc.
3055280Sroot  */
3065245Sroot tcp_usrclosed(tp)
3075245Sroot 	struct tcpcb *tp;
3085245Sroot {
3095245Sroot 
3105245Sroot 	switch (tp->t_state) {
3115245Sroot 
3125245Sroot 	case TCPS_LISTEN:
3135245Sroot 	case TCPS_SYN_SENT:
3145245Sroot 		tp->t_state = TCPS_CLOSED;
3155245Sroot 		tcp_close(tp);
3165245Sroot 		break;
3175245Sroot 
3185245Sroot 	case TCPS_SYN_RECEIVED:
3195245Sroot 	case TCPS_ESTABLISHED:
3205245Sroot 		tp->t_state = TCPS_FIN_WAIT_1;
3215245Sroot 		break;
3225245Sroot 
3235245Sroot 	case TCPS_CLOSE_WAIT:
3245245Sroot 		tp->t_state = TCPS_LAST_ACK;
3255245Sroot 		break;
3265245Sroot 	}
3275245Sroot }
328