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