xref: /csrg-svn/sys/netinet/tcp_usrreq.c (revision 30871)
1 /*
2  * Copyright (c) 1982, 1986 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)tcp_usrreq.c	7.3 (Berkeley) 04/09/87
7  */
8 
9 #include "param.h"
10 #include "systm.h"
11 #include "mbuf.h"
12 #include "socket.h"
13 #include "socketvar.h"
14 #include "protosw.h"
15 #include "errno.h"
16 #include "stat.h"
17 
18 #include "../net/if.h"
19 #include "../net/route.h"
20 
21 #include "in.h"
22 #include "in_pcb.h"
23 #include "in_systm.h"
24 #include "ip.h"
25 #include "ip_var.h"
26 #include "tcp.h"
27 #include "tcp_fsm.h"
28 #include "tcp_seq.h"
29 #include "tcp_timer.h"
30 #include "tcp_var.h"
31 #include "tcpip.h"
32 #include "tcp_debug.h"
33 
34 /*
35  * TCP protocol interface to socket abstraction.
36  */
37 extern	char *tcpstates[];
38 struct	tcpcb *tcp_newtcpcb();
39 int	tcpsenderrors;
40 
41 /*
42  * Process a TCP user request for TCP tb.  If this is a send request
43  * then m is the mbuf chain of send data.  If this is a timer expiration
44  * (called from the software clock routine), then timertype tells which timer.
45  */
46 /*ARGSUSED*/
47 tcp_usrreq(so, req, m, nam, rights)
48 	struct socket *so;
49 	int req;
50 	struct mbuf *m, *nam, *rights;
51 {
52 	register struct inpcb *inp = sotoinpcb(so);
53 	register struct tcpcb *tp;
54 	int s = splnet();
55 	int error = 0;
56 	int ostate;
57 
58 	if (req == PRU_CONTROL) {
59 		error = in_control(so, (int)m, (caddr_t)nam,
60 			(struct ifnet *)rights);
61 		(void) splx(s);
62 		return(error);
63 	}
64 	if (rights && rights->m_len) {
65 		splx(s);
66 		return (EINVAL);
67 	}
68 	/*
69 	 * When a TCP is attached to a socket, then there will be
70 	 * a (struct inpcb) pointed at by the socket, and this
71 	 * structure will point at a subsidary (struct tcpcb).
72 	 */
73 	if (inp == 0 && req != PRU_ATTACH) {
74 		splx(s);
75 		return (EINVAL);		/* XXX */
76 	}
77 	if (inp) {
78 		tp = intotcpcb(inp);
79 		/* WHAT IF TP IS 0? */
80 #ifdef KPROF
81 		tcp_acounts[tp->t_state][req]++;
82 #endif
83 		ostate = tp->t_state;
84 	} else
85 		ostate = 0;
86 	switch (req) {
87 
88 	/*
89 	 * TCP attaches to socket via PRU_ATTACH, reserving space,
90 	 * and an internet control block.
91 	 */
92 	case PRU_ATTACH:
93 		if (inp) {
94 			error = EISCONN;
95 			break;
96 		}
97 		error = tcp_attach(so);
98 		if (error)
99 			break;
100 		if ((so->so_options & SO_LINGER) && so->so_linger == 0)
101 			so->so_linger = TCP_LINGERTIME;
102 		tp = sototcpcb(so);
103 		break;
104 
105 	/*
106 	 * PRU_DETACH detaches the TCP protocol from the socket.
107 	 * If the protocol state is non-embryonic, then can't
108 	 * do this directly: have to initiate a PRU_DISCONNECT,
109 	 * which may finish later; embryonic TCB's can just
110 	 * be discarded here.
111 	 */
112 	case PRU_DETACH:
113 		if (tp->t_state > TCPS_LISTEN)
114 			tp = tcp_disconnect(tp);
115 		else
116 			tp = tcp_close(tp);
117 		break;
118 
119 	/*
120 	 * Give the socket an address.
121 	 */
122 	case PRU_BIND:
123 		error = in_pcbbind(inp, nam);
124 		if (error)
125 			break;
126 		break;
127 
128 	/*
129 	 * Prepare to accept connections.
130 	 */
131 	case PRU_LISTEN:
132 		if (inp->inp_lport == 0)
133 			error = in_pcbbind(inp, (struct mbuf *)0);
134 		if (error == 0)
135 			tp->t_state = TCPS_LISTEN;
136 		break;
137 
138 	/*
139 	 * Initiate connection to peer.
140 	 * Create a template for use in transmissions on this connection.
141 	 * Enter SYN_SENT state, and mark socket as connecting.
142 	 * Start keep-alive timer, and seed output sequence space.
143 	 * Send initial segment on connection.
144 	 */
145 	case PRU_CONNECT:
146 		if (inp->inp_lport == 0) {
147 			error = in_pcbbind(inp, (struct mbuf *)0);
148 			if (error)
149 				break;
150 		}
151 		error = in_pcbconnect(inp, nam);
152 		if (error)
153 			break;
154 		tp->t_template = tcp_template(tp);
155 		if (tp->t_template == 0) {
156 			in_pcbdisconnect(inp);
157 			error = ENOBUFS;
158 			break;
159 		}
160 		soisconnecting(so);
161 		tcpstat.tcps_connattempt++;
162 		tp->t_state = TCPS_SYN_SENT;
163 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
164 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
165 		tcp_sendseqinit(tp);
166 		error = tcp_output(tp);
167 		break;
168 
169 	/*
170 	 * Create a TCP connection between two sockets.
171 	 */
172 	case PRU_CONNECT2:
173 		error = EOPNOTSUPP;
174 		break;
175 
176 	/*
177 	 * Initiate disconnect from peer.
178 	 * If connection never passed embryonic stage, just drop;
179 	 * else if don't need to let data drain, then can just drop anyways,
180 	 * else have to begin TCP shutdown process: mark socket disconnecting,
181 	 * drain unread data, state switch to reflect user close, and
182 	 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
183 	 * when peer sends FIN and acks ours.
184 	 *
185 	 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
186 	 */
187 	case PRU_DISCONNECT:
188 		tp = tcp_disconnect(tp);
189 		break;
190 
191 	/*
192 	 * Accept a connection.  Essentially all the work is
193 	 * done at higher levels; just return the address
194 	 * of the peer, storing through addr.
195 	 */
196 	case PRU_ACCEPT: {
197 		struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
198 
199 		nam->m_len = sizeof (struct sockaddr_in);
200 		sin->sin_family = AF_INET;
201 		sin->sin_port = inp->inp_fport;
202 		sin->sin_addr = inp->inp_faddr;
203 		break;
204 		}
205 
206 	/*
207 	 * Mark the connection as being incapable of further output.
208 	 */
209 	case PRU_SHUTDOWN:
210 		socantsendmore(so);
211 		tp = tcp_usrclosed(tp);
212 		if (tp)
213 			error = tcp_output(tp);
214 		break;
215 
216 	/*
217 	 * After a receive, possibly send window update to peer.
218 	 */
219 	case PRU_RCVD:
220 		(void) tcp_output(tp);
221 		break;
222 
223 	/*
224 	 * Do a send by putting data in output queue and updating urgent
225 	 * marker if URG set.  Possibly send more data.
226 	 */
227 	case PRU_SEND:
228 		sbappend(&so->so_snd, m);
229 #ifdef notdef
230 		if (tp->t_flags & TF_PUSH)
231 			tp->snd_end = tp->snd_una + so->so_snd.sb_cc;
232 #endif
233 		error = tcp_output(tp);
234 		if (error) {		/* XXX fix to use other path */
235 			if (error == ENOBUFS)		/* XXX */
236 				error = 0;		/* XXX */
237 			tcpsenderrors++;
238 		}
239 		break;
240 
241 	/*
242 	 * Abort the TCP.
243 	 */
244 	case PRU_ABORT:
245 		tp = tcp_drop(tp, ECONNABORTED);
246 		break;
247 
248 	case PRU_SENSE:
249 		((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
250 		(void) splx(s);
251 		return (0);
252 
253 	case PRU_RCVOOB:
254 		if ((so->so_oobmark == 0 &&
255 		    (so->so_state & SS_RCVATMARK) == 0) ||
256 		    so->so_options & SO_OOBINLINE ||
257 		    tp->t_oobflags & TCPOOB_HADDATA) {
258 			error = EINVAL;
259 			break;
260 		}
261 		if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
262 			error = EWOULDBLOCK;
263 			break;
264 		}
265 		m->m_len = 1;
266 		*mtod(m, caddr_t) = tp->t_iobc;
267 		if (((int)nam & MSG_PEEK) == 0)
268 			tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
269 		break;
270 
271 	case PRU_SENDOOB:
272 		if (sbspace(&so->so_snd) < -512) {
273 			m_freem(m);
274 			error = ENOBUFS;
275 			break;
276 		}
277 		/*
278 		 * According to RFC961 (Assigned Protocols),
279 		 * the urgent pointer points to the last octet
280 		 * of urgent data.  We continue, however,
281 		 * to consider it to indicate the first octet
282 		 * of data past the urgent section.
283 		 * Otherwise, snd_up should be one lower.
284 		 */
285 		sbappend(&so->so_snd, m);
286 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
287 		tp->t_force = 1;
288 		error = tcp_output(tp);
289 		tp->t_force = 0;
290 		break;
291 
292 	case PRU_SOCKADDR:
293 		in_setsockaddr(inp, nam);
294 		break;
295 
296 	case PRU_PEERADDR:
297 		in_setpeeraddr(inp, nam);
298 		break;
299 
300 	/*
301 	 * TCP slow timer went off; going through this
302 	 * routine for tracing's sake.
303 	 */
304 	case PRU_SLOWTIMO:
305 		tp = tcp_timers(tp, (int)nam);
306 		req |= (int)nam << 8;		/* for debug's sake */
307 		break;
308 
309 	default:
310 		panic("tcp_usrreq");
311 	}
312 	if (tp && (so->so_options & SO_DEBUG))
313 		tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req);
314 	splx(s);
315 	return (error);
316 }
317 
318 tcp_ctloutput(op, so, level, optname, mp)
319 	int op;
320 	struct socket *so;
321 	int level, optname;
322 	struct mbuf **mp;
323 {
324 	int error = 0;
325 	struct inpcb *inp = sotoinpcb(so);
326 	register struct tcpcb *tp = intotcpcb(inp);
327 	register struct mbuf *m;
328 
329 	if (level != IPPROTO_TCP)
330 		return (ip_ctloutput(op, so, level, optname, mp));
331 
332 	switch (op) {
333 
334 	case PRCO_SETOPT:
335 		m = *mp;
336 		switch (optname) {
337 
338 		case TCP_NODELAY:
339 			if (m == NULL || m->m_len < sizeof (int))
340 				error = EINVAL;
341 			else if (*mtod(m, int *))
342 				tp->t_flags |= TF_NODELAY;
343 			else
344 				tp->t_flags &= ~TF_NODELAY;
345 			break;
346 
347 		case TCP_MAXSEG:	/* not yet */
348 		default:
349 			error = EINVAL;
350 			break;
351 		}
352 		(void)m_free(m);
353 		break;
354 
355 	case PRCO_GETOPT:
356 		*mp = m = m_get(M_WAIT, MT_SOOPTS);
357 		m->m_len = sizeof(int);
358 
359 		switch (optname) {
360 		case TCP_NODELAY:
361 			*mtod(m, int *) = tp->t_flags & TF_NODELAY;
362 			break;
363 		case TCP_MAXSEG:
364 			*mtod(m, int *) = tp->t_maxseg;
365 			break;
366 		default:
367 			error = EINVAL;
368 			break;
369 		}
370 		break;
371 	}
372 	return (error);
373 }
374 
375 int	tcp_sendspace = 1024*4;
376 int	tcp_recvspace = 1024*4;
377 /*
378  * Attach TCP protocol to socket, allocating
379  * internet protocol control block, tcp control block,
380  * bufer space, and entering LISTEN state if to accept connections.
381  */
382 tcp_attach(so)
383 	struct socket *so;
384 {
385 	register struct tcpcb *tp;
386 	struct inpcb *inp;
387 	int error;
388 
389 	error = soreserve(so, tcp_sendspace, tcp_recvspace);
390 	if (error)
391 		return (error);
392 	error = in_pcballoc(so, &tcb);
393 	if (error)
394 		return (error);
395 	inp = sotoinpcb(so);
396 	tp = tcp_newtcpcb(inp);
397 	if (tp == 0) {
398 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
399 
400 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
401 		in_pcbdetach(inp);
402 		so->so_state |= nofd;
403 		return (ENOBUFS);
404 	}
405 	tp->t_state = TCPS_CLOSED;
406 	return (0);
407 }
408 
409 /*
410  * Initiate (or continue) disconnect.
411  * If embryonic state, just send reset (once).
412  * If in ``let data drain'' option and linger null, just drop.
413  * Otherwise (hard), mark socket disconnecting and drop
414  * current input data; switch states based on user close, and
415  * send segment to peer (with FIN).
416  */
417 struct tcpcb *
418 tcp_disconnect(tp)
419 	register struct tcpcb *tp;
420 {
421 	struct socket *so = tp->t_inpcb->inp_socket;
422 
423 	if (tp->t_state < TCPS_ESTABLISHED)
424 		tp = tcp_close(tp);
425 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
426 		tp = tcp_drop(tp, 0);
427 	else {
428 		soisdisconnecting(so);
429 		sbflush(&so->so_rcv);
430 		tp = tcp_usrclosed(tp);
431 		if (tp)
432 			(void) tcp_output(tp);
433 	}
434 	return (tp);
435 }
436 
437 /*
438  * User issued close, and wish to trail through shutdown states:
439  * if never received SYN, just forget it.  If got a SYN from peer,
440  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
441  * If already got a FIN from peer, then almost done; go to LAST_ACK
442  * state.  In all other cases, have already sent FIN to peer (e.g.
443  * after PRU_SHUTDOWN), and just have to play tedious game waiting
444  * for peer to send FIN or not respond to keep-alives, etc.
445  * We can let the user exit from the close as soon as the FIN is acked.
446  */
447 struct tcpcb *
448 tcp_usrclosed(tp)
449 	register struct tcpcb *tp;
450 {
451 
452 	switch (tp->t_state) {
453 
454 	case TCPS_CLOSED:
455 	case TCPS_LISTEN:
456 	case TCPS_SYN_SENT:
457 		tp->t_state = TCPS_CLOSED;
458 		tp = tcp_close(tp);
459 		break;
460 
461 	case TCPS_SYN_RECEIVED:
462 	case TCPS_ESTABLISHED:
463 		tp->t_state = TCPS_FIN_WAIT_1;
464 		break;
465 
466 	case TCPS_CLOSE_WAIT:
467 		tp->t_state = TCPS_LAST_ACK;
468 		break;
469 	}
470 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2)
471 		soisdisconnected(tp->t_inpcb->inp_socket);
472 	return (tp);
473 }
474