xref: /original-bsd/sys/netinet/tcp_usrreq.c (revision caa496f3)
1 /*
2  * Copyright (c) 1982, 1986, 1988 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.14 (Berkeley) 05/17/90
18  */
19 
20 #include "param.h"
21 #include "systm.h"
22 #include "malloc.h"
23 #include "mbuf.h"
24 #include "socket.h"
25 #include "socketvar.h"
26 #include "protosw.h"
27 #include "errno.h"
28 #include "stat.h"
29 
30 #include "../net/if.h"
31 #include "../net/route.h"
32 
33 #include "in.h"
34 #include "in_systm.h"
35 #include "ip.h"
36 #include "in_pcb.h"
37 #include "ip_var.h"
38 #include "tcp.h"
39 #include "tcp_fsm.h"
40 #include "tcp_seq.h"
41 #include "tcp_timer.h"
42 #include "tcp_var.h"
43 #include "tcpip.h"
44 #include "tcp_debug.h"
45 
46 /*
47  * TCP protocol interface to socket abstraction.
48  */
49 extern	char *tcpstates[];
50 struct	tcpcb *tcp_newtcpcb();
51 
52 /*
53  * Process a TCP user request for TCP tb.  If this is a send request
54  * then m is the mbuf chain of send data.  If this is a timer expiration
55  * (called from the software clock routine), then timertype tells which timer.
56  */
57 /*ARGSUSED*/
58 tcp_usrreq(so, req, m, nam, control)
59 	struct socket *so;
60 	int req;
61 	struct mbuf *m, *nam, *control;
62 {
63 	register struct inpcb *inp;
64 	register struct tcpcb *tp;
65 	int s;
66 	int error = 0;
67 	int ostate;
68 
69 	if (req == PRU_CONTROL)
70 		return (in_control(so, (int)m, (caddr_t)nam,
71 			(struct ifnet *)control));
72 	if (control && control->m_len) {
73 		m_freem(control);
74 		if (m)
75 			m_freem(m);
76 		return (EINVAL);
77 	}
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_len = sizeof(*sin);
215 		sin->sin_port = inp->inp_fport;
216 		sin->sin_addr = inp->inp_faddr;
217 		break;
218 		}
219 
220 	/*
221 	 * Mark the connection as being incapable of further output.
222 	 */
223 	case PRU_SHUTDOWN:
224 		socantsendmore(so);
225 		tp = tcp_usrclosed(tp);
226 		if (tp)
227 			error = tcp_output(tp);
228 		break;
229 
230 	/*
231 	 * After a receive, possibly send window update to peer.
232 	 */
233 	case PRU_RCVD:
234 		(void) tcp_output(tp);
235 		break;
236 
237 	/*
238 	 * Do a send by putting data in output queue and updating urgent
239 	 * marker if URG set.  Possibly send more data.
240 	 */
241 	case PRU_SEND:
242 		sbappend(&so->so_snd, m);
243 		error = tcp_output(tp);
244 		break;
245 
246 	/*
247 	 * Abort the TCP.
248 	 */
249 	case PRU_ABORT:
250 		tp = tcp_drop(tp, ECONNABORTED);
251 		break;
252 
253 	case PRU_SENSE:
254 		((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
255 		(void) splx(s);
256 		return (0);
257 
258 	case PRU_RCVOOB:
259 		if ((so->so_oobmark == 0 &&
260 		    (so->so_state & SS_RCVATMARK) == 0) ||
261 		    so->so_options & SO_OOBINLINE ||
262 		    tp->t_oobflags & TCPOOB_HADDATA) {
263 			error = EINVAL;
264 			break;
265 		}
266 		if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
267 			error = EWOULDBLOCK;
268 			break;
269 		}
270 		m->m_len = 1;
271 		*mtod(m, caddr_t) = tp->t_iobc;
272 		if (((int)nam & MSG_PEEK) == 0)
273 			tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
274 		break;
275 
276 	case PRU_SENDOOB:
277 		if (sbspace(&so->so_snd) < -512) {
278 			m_freem(m);
279 			error = ENOBUFS;
280 			break;
281 		}
282 		/*
283 		 * According to RFC961 (Assigned Protocols),
284 		 * the urgent pointer points to the last octet
285 		 * of urgent data.  We continue, however,
286 		 * to consider it to indicate the first octet
287 		 * of data past the urgent section.
288 		 * Otherwise, snd_up should be one lower.
289 		 */
290 		sbappend(&so->so_snd, m);
291 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
292 		tp->t_force = 1;
293 		error = tcp_output(tp);
294 		tp->t_force = 0;
295 		break;
296 
297 	case PRU_SOCKADDR:
298 		in_setsockaddr(inp, nam);
299 		break;
300 
301 	case PRU_PEERADDR:
302 		in_setpeeraddr(inp, nam);
303 		break;
304 
305 	/*
306 	 * TCP slow timer went off; going through this
307 	 * routine for tracing's sake.
308 	 */
309 	case PRU_SLOWTIMO:
310 		tp = tcp_timers(tp, (int)nam);
311 		req |= (int)nam << 8;		/* for debug's sake */
312 		break;
313 
314 	default:
315 		panic("tcp_usrreq");
316 	}
317 	if (tp && (so->so_options & SO_DEBUG))
318 		tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req);
319 	splx(s);
320 	return (error);
321 }
322 
323 tcp_ctloutput(op, so, level, optname, mp)
324 	int op;
325 	struct socket *so;
326 	int level, optname;
327 	struct mbuf **mp;
328 {
329 	int error = 0;
330 	struct inpcb *inp = sotoinpcb(so);
331 	register struct tcpcb *tp = intotcpcb(inp);
332 	register struct mbuf *m;
333 
334 	if (level != IPPROTO_TCP)
335 		return (ip_ctloutput(op, so, level, optname, mp));
336 
337 	switch (op) {
338 
339 	case PRCO_SETOPT:
340 		m = *mp;
341 		switch (optname) {
342 
343 		case TCP_NODELAY:
344 			if (m == NULL || m->m_len < sizeof (int))
345 				error = EINVAL;
346 			else if (*mtod(m, int *))
347 				tp->t_flags |= TF_NODELAY;
348 			else
349 				tp->t_flags &= ~TF_NODELAY;
350 			break;
351 
352 		case TCP_MAXSEG:	/* not yet */
353 		default:
354 			error = EINVAL;
355 			break;
356 		}
357 		if (m)
358 			(void) m_free(m);
359 		break;
360 
361 	case PRCO_GETOPT:
362 		*mp = m = m_get(M_WAIT, MT_SOOPTS);
363 		m->m_len = sizeof(int);
364 
365 		switch (optname) {
366 		case TCP_NODELAY:
367 			*mtod(m, int *) = tp->t_flags & TF_NODELAY;
368 			break;
369 		case TCP_MAXSEG:
370 			*mtod(m, int *) = tp->t_maxseg;
371 			break;
372 		default:
373 			error = EINVAL;
374 			break;
375 		}
376 		break;
377 	}
378 	return (error);
379 }
380 
381 u_long	tcp_sendspace = 1024*4;
382 u_long	tcp_recvspace = 1024*4;
383 
384 /*
385  * Attach TCP protocol to socket, allocating
386  * internet protocol control block, tcp control block,
387  * bufer space, and entering LISTEN state if to accept connections.
388  */
389 tcp_attach(so)
390 	struct socket *so;
391 {
392 	register struct tcpcb *tp;
393 	struct inpcb *inp;
394 	int error;
395 
396 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
397 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
398 		if (error)
399 			return (error);
400 	}
401 	error = in_pcballoc(so, &tcb);
402 	if (error)
403 		return (error);
404 	inp = sotoinpcb(so);
405 	tp = tcp_newtcpcb(inp);
406 	if (tp == 0) {
407 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
408 
409 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
410 		in_pcbdetach(inp);
411 		so->so_state |= nofd;
412 		return (ENOBUFS);
413 	}
414 	tp->t_state = TCPS_CLOSED;
415 	return (0);
416 }
417 
418 /*
419  * Initiate (or continue) disconnect.
420  * If embryonic state, just send reset (once).
421  * If in ``let data drain'' option and linger null, just drop.
422  * Otherwise (hard), mark socket disconnecting and drop
423  * current input data; switch states based on user close, and
424  * send segment to peer (with FIN).
425  */
426 struct tcpcb *
427 tcp_disconnect(tp)
428 	register struct tcpcb *tp;
429 {
430 	struct socket *so = tp->t_inpcb->inp_socket;
431 
432 	if (tp->t_state < TCPS_ESTABLISHED)
433 		tp = tcp_close(tp);
434 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
435 		tp = tcp_drop(tp, 0);
436 	else {
437 		soisdisconnecting(so);
438 		sbflush(&so->so_rcv);
439 		tp = tcp_usrclosed(tp);
440 		if (tp)
441 			(void) tcp_output(tp);
442 	}
443 	return (tp);
444 }
445 
446 /*
447  * User issued close, and wish to trail through shutdown states:
448  * if never received SYN, just forget it.  If got a SYN from peer,
449  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
450  * If already got a FIN from peer, then almost done; go to LAST_ACK
451  * state.  In all other cases, have already sent FIN to peer (e.g.
452  * after PRU_SHUTDOWN), and just have to play tedious game waiting
453  * for peer to send FIN or not respond to keep-alives, etc.
454  * We can let the user exit from the close as soon as the FIN is acked.
455  */
456 struct tcpcb *
457 tcp_usrclosed(tp)
458 	register struct tcpcb *tp;
459 {
460 
461 	switch (tp->t_state) {
462 
463 	case TCPS_CLOSED:
464 	case TCPS_LISTEN:
465 	case TCPS_SYN_SENT:
466 		tp->t_state = TCPS_CLOSED;
467 		tp = tcp_close(tp);
468 		break;
469 
470 	case TCPS_SYN_RECEIVED:
471 	case TCPS_ESTABLISHED:
472 		tp->t_state = TCPS_FIN_WAIT_1;
473 		break;
474 
475 	case TCPS_CLOSE_WAIT:
476 		tp->t_state = TCPS_LAST_ACK;
477 		break;
478 	}
479 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2)
480 		soisdisconnected(tp->t_inpcb->inp_socket);
481 	return (tp);
482 }
483