xref: /csrg-svn/sys/netinet/tcp_usrreq.c (revision 5442)
1*5442Swnj /* tcp_usrreq.c 1.48 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;
2045417Swnj /* END UNIMPLEMENTED HOOKS */
2055113Swnj 
2065113Swnj 	case PRU_RCVOOB:
207*5442Swnj #if TCPTRUEOOB
208*5442Swnj 		if (tp->t_flags & TF_DOOOB) {
209*5442Swnj 			if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
210*5442Swnj 				error = EWOULDBLOCK;
211*5442Swnj 				break;
212*5442Swnj 			}
213*5442Swnj 			*mtod(m, caddr_t) = tp->t_iobc;
214*5442Swnj 			tp->t_oobflags &= ~TCPOOB_HAVEDATA;
215*5442Swnj 			break;
216*5442Swnj 		}
217*5442Swnj #endif
218*5442Swnj 		if (so->so_oobmark == 0 &&
219*5442Swnj 		    (so->so_state & SS_RCVATMARK) == 0) {
2205417Swnj 			error = EINVAL;
2215417Swnj 			break;
2225417Swnj 		}
223*5442Swnj 		if (so->so_rcv.sb_cc < so->so_oobmark) {
224*5442Swnj 			error = EWOULDBLOCK;
225*5442Swnj 			return;
226*5442Swnj 		}
227*5442Swnj 		{ struct mbuf *n = so->so_rcv.sb_mb;
228*5442Swnj 		  int cnt = so->so_oobmark;
229*5442Swnj 		  while (cnt > n->m_len) {
230*5442Swnj 			cnt -= n->m_len;
231*5442Swnj 			n = n->m_next;
232*5442Swnj 		  }
233*5442Swnj 		  *mtod(m, caddr_t) = *(mtod(n, caddr_t) + cnt);
234*5442Swnj 		}
235*5442Swnj 		tp->t_oobflags &= ~TCPOOB_HAVEDATA;
2365113Swnj 		break;
2375113Swnj 
2385113Swnj 	case PRU_SENDOOB:
239*5442Swnj 		if (sbspace(&so->so_snd) < -512) {
240*5442Swnj 			error = ENOBUFS;
241*5442Swnj 			break;
242*5442Swnj 		}
2435417Swnj 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc + 1;
2445417Swnj 		sbappend(&so->so_snd, m);
2455417Swnj /*
2465417Swnj 		if (tp->t_flags & TF_PUSH)
2475417Swnj 			tp->snd_end = tp->snd_una + so->so_snd.sb_cc;
2485417Swnj  */
249*5442Swnj #ifdef TCPTRUEOOB
250*5442Swnj 		if (tp->t_flags & TF_DOOOB) {
251*5442Swnj 			tp->t_oobseq++;
252*5442Swnj 			tp->t_oobc = *mtod(m, caddr_t);
253*5442Swnj printf("sendoob seq now %x oobc %x\n", tp->t_oobseq, tp->t_oobc);
254*5442Swnj 			tp->t_oobflags |= TCPOOB_NEEDACK;
255*5442Swnj 		}
256*5442Swnj #endif
257*5442Swnj 		tp->t_force = 1; (void) tcp_output(tp); tp->t_force = 0;
2585113Swnj 		break;
2595113Swnj 
2605280Sroot 	/*
2615280Sroot 	 * TCP slow timer went off; going through this
2625280Sroot 	 * routine for tracing's sake.
2635280Sroot 	 */
2644809Swnj 	case PRU_SLOWTIMO:
2655075Swnj 		tcp_timers(tp, (int)addr);
2665270Sroot 		req |= (int)addr << 8;		/* for debug's sake */
2674809Swnj 		break;
2684809Swnj 
2694731Swnj 	default:
2704731Swnj 		panic("tcp_usrreq");
2714567Swnj 	}
2725270Sroot 	if (tp && (so->so_options & SO_DEBUG))
2735270Sroot 		tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req);
2744567Swnj 	splx(s);
2754886Swnj 	return (error);
2764497Swnj }
2775245Sroot 
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  */
2835280Sroot tcp_attach(so, sa)
2845280Sroot 	struct socket *so;
2855280Sroot 	struct sockaddr *sa;
2865280Sroot {
2875280Sroot 	register struct tcpcb *tp;
2885280Sroot 	struct inpcb *inp;
2895280Sroot 	int error;
2905280Sroot 
2915280Sroot 	error = in_pcbattach(so, &tcb, 2048, 2048, (struct sockaddr_in *)sa);
2925280Sroot 	if (error)
2935280Sroot 		return (error);
2945280Sroot 	inp = (struct inpcb *)so->so_pcb;
2955280Sroot 	tp = tcp_newtcpcb(inp);
2965280Sroot 	if (so->so_options & SO_ACCEPTCONN) {
2975280Sroot 		if (tp == 0) {
2985280Sroot 			in_pcbdetach(inp);
2995280Sroot 			return (ENOBUFS);
3005280Sroot 		}
3015280Sroot 		tp->t_state = TCPS_LISTEN;
3025280Sroot 	} else
3035280Sroot 		tp->t_state = TCPS_CLOSED;
3045280Sroot 	return (0);
3055280Sroot }
3065280Sroot 
3075280Sroot /*
3085280Sroot  * Initiate (or continue) disconnect.
3095280Sroot  * If embryonic state, just send reset (once).
3105280Sroot  * If not in ``let data drain'' option, just drop.
3115280Sroot  * Otherwise (hard), mark socket disconnecting and drop
3125280Sroot  * current input data; switch states based on user close, and
3135280Sroot  * send segment to peer (with FIN).
3145280Sroot  */
3155280Sroot tcp_disconnect(tp)
3165280Sroot 	struct tcpcb *tp;
3175280Sroot {
3185280Sroot 	struct socket *so = tp->t_inpcb->inp_socket;
3195280Sroot 
3205280Sroot 	if (tp->t_state < TCPS_ESTABLISHED)
3215280Sroot 		tcp_close(tp);
3225392Swnj 	else if (so->so_linger == 0)
3235280Sroot 		tcp_drop(tp, 0);
3245280Sroot 	else {
3255280Sroot 		soisdisconnecting(so);
3265280Sroot 		sbflush(&so->so_rcv);
3275280Sroot 		tcp_usrclosed(tp);
3285280Sroot 		(void) tcp_output(tp);
3295280Sroot 	}
3305280Sroot }
3315280Sroot 
3325280Sroot /*
3335280Sroot  * User issued close, and wish to trail through shutdown states:
3345280Sroot  * if never received SYN, just forget it.  If got a SYN from peer,
3355280Sroot  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
3365280Sroot  * If already got a FIN from peer, then almost done; go to LAST_ACK
3375280Sroot  * state.  In all other cases, have already sent FIN to peer (e.g.
3385280Sroot  * after PRU_SHUTDOWN), and just have to play tedious game waiting
3395280Sroot  * for peer to send FIN or not respond to keep-alives, etc.
3405280Sroot  */
3415245Sroot tcp_usrclosed(tp)
3425245Sroot 	struct tcpcb *tp;
3435245Sroot {
3445245Sroot 
3455245Sroot 	switch (tp->t_state) {
3465245Sroot 
3475245Sroot 	case TCPS_LISTEN:
3485245Sroot 	case TCPS_SYN_SENT:
3495245Sroot 		tp->t_state = TCPS_CLOSED;
3505245Sroot 		tcp_close(tp);
3515245Sroot 		break;
3525245Sroot 
3535245Sroot 	case TCPS_SYN_RECEIVED:
3545245Sroot 	case TCPS_ESTABLISHED:
3555245Sroot 		tp->t_state = TCPS_FIN_WAIT_1;
3565245Sroot 		break;
3575245Sroot 
3585245Sroot 	case TCPS_CLOSE_WAIT:
3595245Sroot 		tp->t_state = TCPS_LAST_ACK;
3605245Sroot 		break;
3615245Sroot 	}
3625245Sroot }
363