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