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