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