xref: /csrg-svn/sys/netinet/tcp_usrreq.c (revision 5280)
1*5280Sroot /* tcp_usrreq.c 1.45 81/12/21 */
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 
24*5280Sroot /*
25*5280Sroot  * TCP protocol interface to socket abstraction.
26*5280Sroot  */
27*5280Sroot extern	char *tcpstates[];
284954Swnj struct	tcpcb *tcp_newtcpcb();
29*5280Sroot 
304734Swnj /*
31*5280Sroot  * 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 	/*
49*5280Sroot 	 * When a TCP is attached to a socket, then there will be
50*5280Sroot 	 * a (struct inpcb) pointed at by the socket, and this
51*5280Sroot 	 * structure will point at a subsidary (struct tcpcb).
52*5280Sroot 	 * The normal sequence of events is:
53*5280Sroot 	 *	PRU_ATTACH		creating these structures
54*5280Sroot 	 *	PRU_CONNECT		connecting to a remote peer
55*5280Sroot 	 *	(PRU_SEND|PRU_RCVD)*	exchanging data
56*5280Sroot 	 *	PRU_DISCONNECT		disconnecting from remote peer
57*5280Sroot 	 *	PRU_DETACH		deleting the structures
58*5280Sroot 	 * With the operations from PRU_CONNECT through PRU_DISCONNECT
59*5280Sroot 	 * possible repeated several times.
60*5280Sroot 	 *
61*5280Sroot 	 * MULTIPLE CONNECTS ARE NOT YET IMPLEMENTED.
624886Swnj 	 */
635089Swnj 	if (inp == 0 && req != PRU_ATTACH) {
645075Swnj 		splx(s);
65*5280Sroot 		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 
76*5280Sroot 	/*
77*5280Sroot 	 * TCP attaches to socket via PRU_ATTACH, reserving space,
78*5280Sroot 	 * and internet and TCP control blocks.
79*5280Sroot 	 * If the socket is to receive connections,
80*5280Sroot 	 * then the LISTEN state is entered.
81*5280Sroot 	 */
824809Swnj 	case PRU_ATTACH:
834954Swnj 		if (inp) {
844809Swnj 			error = EISCONN;
854911Swnj 			break;
864886Swnj 		}
87*5280Sroot 		error = tcp_attach(so, (struct sockaddr *)addr);
885075Swnj 		if (error)
894954Swnj 			break;
90*5280Sroot 		tp = sototcpcb(so);
914567Swnj 		break;
924497Swnj 
93*5280Sroot 	/*
94*5280Sroot 	 * PRU_DETACH detaches the TCP protocol from the socket.
95*5280Sroot 	 * If the protocol state is non-embryonic, then can't
96*5280Sroot 	 * do this directly: have to initiate a PRU_DISCONNECT,
97*5280Sroot 	 * which may finish later; embryonic TCB's can just
98*5280Sroot 	 * be discarded here.
99*5280Sroot 	 */
1004809Swnj 	case PRU_DETACH:
101*5280Sroot 		if (tp->t_state > TCPS_LISTEN)
102*5280Sroot 			tcp_disconnect(tp);
103*5280Sroot 		else {
104*5280Sroot 			tcp_close(tp);
105*5280Sroot 			tp = 0;
106*5280Sroot 		}
1074809Swnj 		break;
1084809Swnj 
109*5280Sroot 	/*
110*5280Sroot 	 * Initiate connection to peer.
111*5280Sroot 	 * Create a template for use in transmissions on this connection.
112*5280Sroot 	 * Enter SYN_SENT state, and mark socket as connecting.
113*5280Sroot 	 * Start keep-alive timer, and seed output sequence space.
114*5280Sroot 	 * Send initial segment on connection.
115*5280Sroot 	 */
1164809Swnj 	case PRU_CONNECT:
1175165Swnj 		error = in_pcbconnect(inp, (struct sockaddr_in *)addr);
1184954Swnj 		if (error)
1194886Swnj 			break;
1205174Swnj 		tp->t_template = tcp_template(tp);
121*5280Sroot 		if (tp->t_template == 0) {
122*5280Sroot 			in_pcbdisconnect(inp);
123*5280Sroot 			error = ENOBUFS;
124*5280Sroot 			break;
125*5280Sroot 		}
1264886Swnj 		soisconnecting(so);
1275075Swnj 		tp->t_state = TCPS_SYN_SENT;
1285245Sroot 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
1295245Sroot 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
1305245Sroot 		tcp_sendseqinit(tp);
1315113Swnj 		(void) tcp_output(tp);
1324567Swnj 		break;
1334497Swnj 
134*5280Sroot 	/*
135*5280Sroot 	 * Initiate disconnect from peer.
136*5280Sroot 	 * If connection never passed embryonic stage, just drop;
137*5280Sroot 	 * else if don't need to let data drain, then can just drop anyways,
138*5280Sroot 	 * else have to begin TCP shutdown process: mark socket disconnecting,
139*5280Sroot 	 * drain unread data, state switch to reflect user close, and
140*5280Sroot 	 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
141*5280Sroot 	 * when peer sends FIN and acks ours.
142*5280Sroot 	 *
143*5280Sroot 	 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
144*5280Sroot 	 */
145*5280Sroot 	case PRU_DISCONNECT:
146*5280Sroot 		tcp_disconnect(tp);
1475245Sroot 		break;
1485245Sroot 
149*5280Sroot 	/*
150*5280Sroot 	 * Accept a connection.  Essentially all the work is
151*5280Sroot 	 * done at higher levels; just return the address
152*5280Sroot 	 * of the peer, storing through addr.
153*5280Sroot 	 */
1544925Swnj 	case PRU_ACCEPT:
155*5280Sroot 		in_pcbconnaddr(inp, (struct sockaddr *)addr);
1564954Swnj 		break;
1574925Swnj 
158*5280Sroot 	/*
159*5280Sroot 	 * Mark the connection as being incapable of further output.
160*5280Sroot 	 */
1614809Swnj 	case PRU_SHUTDOWN:
1625089Swnj 		socantsendmore(so);
1635245Sroot 		tcp_usrclosed(tp);
1645245Sroot 		(void) tcp_output(tp);
1654567Swnj 		break;
1664497Swnj 
167*5280Sroot 	/*
168*5280Sroot 	 * After a receive, possibly send window update to peer.
169*5280Sroot 	 */
1704809Swnj 	case PRU_RCVD:
1715113Swnj 		(void) tcp_output(tp);
1724567Swnj 		break;
1734497Swnj 
174*5280Sroot 	/*
175*5280Sroot 	 * Do a send by putting data in output queue and updating urgent
176*5280Sroot 	 * marker if URG set.  Possibly send more data.
177*5280Sroot 	 */
1784809Swnj 	case PRU_SEND:
1795075Swnj 		sbappend(&so->so_snd, m);
1805089Swnj /*
1815089Swnj 		if (tp->t_flags & TF_PUSH)
1825075Swnj 			tp->snd_end = tp->snd_una + so->so_snd.sb_cc;
1835089Swnj  */
1845089Swnj 		if (tp->t_flags & TF_URG)
1855089Swnj 			tp->snd_up = tp->snd_una + so->so_snd.sb_cc + 1;
1865113Swnj 		(void) tcp_output(tp);
1874567Swnj 		break;
1884567Swnj 
189*5280Sroot 	/*
190*5280Sroot 	 * Abort the TCP.
191*5280Sroot 	 */
1924809Swnj 	case PRU_ABORT:
1935075Swnj 		tcp_drop(tp, ECONNABORTED);
1944567Swnj 		break;
1954567Swnj 
196*5280Sroot /* SOME AS YET UNIMPLEMENTED HOOKS */
1974809Swnj 	case PRU_CONTROL:
1984886Swnj 		error = EOPNOTSUPP;
1994809Swnj 		break;
2004809Swnj 
2015113Swnj 	case PRU_SENSE:
2025113Swnj 		error = EOPNOTSUPP;
2035113Swnj 		break;
2045113Swnj 
2055113Swnj 	case PRU_RCVOOB:
2065113Swnj 		error = EOPNOTSUPP;
2075113Swnj 		break;
2085113Swnj 
2095113Swnj 	case PRU_SENDOOB:
2105113Swnj 		error = EOPNOTSUPP;
2115113Swnj 		break;
212*5280Sroot /* END UNIMPLEMENTED HOOKS */
2135113Swnj 
214*5280Sroot 	/*
215*5280Sroot 	 * TCP slow timer went off; going through this
216*5280Sroot 	 * routine for tracing's sake.
217*5280Sroot 	 */
2184809Swnj 	case PRU_SLOWTIMO:
2195075Swnj 		tcp_timers(tp, (int)addr);
2205270Sroot 		req |= (int)addr << 8;		/* for debug's sake */
2214809Swnj 		break;
2224809Swnj 
2234731Swnj 	default:
2244731Swnj 		panic("tcp_usrreq");
2254567Swnj 	}
2265270Sroot 	if (tp && (so->so_options & SO_DEBUG))
2275270Sroot 		tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req);
2284567Swnj 	splx(s);
2294886Swnj 	return (error);
2304497Swnj }
2315245Sroot 
232*5280Sroot /*
233*5280Sroot  * Attach TCP protocol to socket, allocating
234*5280Sroot  * internet protocol control block, tcp control block,
235*5280Sroot  * bufer space, and entering LISTEN state if to accept connections.
236*5280Sroot  */
237*5280Sroot tcp_attach(so, sa)
238*5280Sroot 	struct socket *so;
239*5280Sroot 	struct sockaddr *sa;
240*5280Sroot {
241*5280Sroot 	register struct tcpcb *tp;
242*5280Sroot 	struct inpcb *inp;
243*5280Sroot 	int error;
244*5280Sroot 
245*5280Sroot 	error = in_pcbattach(so, &tcb, 2048, 2048, (struct sockaddr_in *)sa);
246*5280Sroot 	if (error)
247*5280Sroot 		return (error);
248*5280Sroot 	inp = (struct inpcb *)so->so_pcb;
249*5280Sroot 	tp = tcp_newtcpcb(inp);
250*5280Sroot 	if (so->so_options & SO_ACCEPTCONN) {
251*5280Sroot 		if (tp == 0) {
252*5280Sroot 			in_pcbdetach(inp);
253*5280Sroot 			return (ENOBUFS);
254*5280Sroot 		}
255*5280Sroot 		tp->t_state = TCPS_LISTEN;
256*5280Sroot 	} else
257*5280Sroot 		tp->t_state = TCPS_CLOSED;
258*5280Sroot 	return (0);
259*5280Sroot }
260*5280Sroot 
261*5280Sroot /*
262*5280Sroot  * Initiate (or continue) disconnect.
263*5280Sroot  * If embryonic state, just send reset (once).
264*5280Sroot  * If not in ``let data drain'' option, just drop.
265*5280Sroot  * Otherwise (hard), mark socket disconnecting and drop
266*5280Sroot  * current input data; switch states based on user close, and
267*5280Sroot  * send segment to peer (with FIN).
268*5280Sroot  */
269*5280Sroot tcp_disconnect(tp)
270*5280Sroot 	struct tcpcb *tp;
271*5280Sroot {
272*5280Sroot 	struct socket *so = tp->t_inpcb->inp_socket;
273*5280Sroot 
274*5280Sroot 	if (tp->t_state < TCPS_ESTABLISHED)
275*5280Sroot 		tcp_close(tp);
276*5280Sroot 	else if ((so->so_options & SO_LETDATADRAIN) == 0)
277*5280Sroot 		tcp_drop(tp, 0);
278*5280Sroot 	else {
279*5280Sroot 		soisdisconnecting(so);
280*5280Sroot 		sbflush(&so->so_rcv);
281*5280Sroot 		tcp_usrclosed(tp);
282*5280Sroot 		(void) tcp_output(tp);
283*5280Sroot 	}
284*5280Sroot }
285*5280Sroot 
286*5280Sroot /*
287*5280Sroot  * User issued close, and wish to trail through shutdown states:
288*5280Sroot  * if never received SYN, just forget it.  If got a SYN from peer,
289*5280Sroot  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
290*5280Sroot  * If already got a FIN from peer, then almost done; go to LAST_ACK
291*5280Sroot  * state.  In all other cases, have already sent FIN to peer (e.g.
292*5280Sroot  * after PRU_SHUTDOWN), and just have to play tedious game waiting
293*5280Sroot  * for peer to send FIN or not respond to keep-alives, etc.
294*5280Sroot  */
2955245Sroot tcp_usrclosed(tp)
2965245Sroot 	struct tcpcb *tp;
2975245Sroot {
2985245Sroot 
2995245Sroot 	switch (tp->t_state) {
3005245Sroot 
3015245Sroot 	case TCPS_LISTEN:
3025245Sroot 	case TCPS_SYN_SENT:
3035245Sroot 		tp->t_state = TCPS_CLOSED;
3045245Sroot 		tcp_close(tp);
3055245Sroot 		break;
3065245Sroot 
3075245Sroot 	case TCPS_SYN_RECEIVED:
3085245Sroot 	case TCPS_ESTABLISHED:
3095245Sroot 		tp->t_state = TCPS_FIN_WAIT_1;
3105245Sroot 		break;
3115245Sroot 
3125245Sroot 	case TCPS_CLOSE_WAIT:
3135245Sroot 		tp->t_state = TCPS_LAST_ACK;
3145245Sroot 		break;
3155245Sroot 	}
3165245Sroot }
317