xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 11afcc8f9f96d657b8e6f7547c02c1957331fc96)
1 /*
2  * Copyright (c) 1982, 1986, 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
34  *	$Id: tcp_usrreq.c,v 1.36 1997/12/18 09:50:38 davidg Exp $
35  */
36 
37 #include "opt_tcpdebug.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/sysctl.h>
43 #include <sys/mbuf.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/protosw.h>
47 
48 #include <net/if.h>
49 #include <net/route.h>
50 
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/in_pcb.h>
54 #include <netinet/in_var.h>
55 #include <netinet/ip_var.h>
56 #include <netinet/tcp.h>
57 #include <netinet/tcp_fsm.h>
58 #include <netinet/tcp_seq.h>
59 #include <netinet/tcp_timer.h>
60 #include <netinet/tcp_var.h>
61 #include <netinet/tcpip.h>
62 #ifdef TCPDEBUG
63 #include <netinet/tcp_debug.h>
64 #endif
65 
66 /*
67  * TCP protocol interface to socket abstraction.
68  */
69 extern	char *tcpstates[];	/* XXX ??? */
70 
71 static int	tcp_attach __P((struct socket *, struct proc *));
72 static int	tcp_connect __P((struct tcpcb *, struct sockaddr *,
73 				 struct proc *));
74 static struct tcpcb *
75 		tcp_disconnect __P((struct tcpcb *));
76 static struct tcpcb *
77 		tcp_usrclosed __P((struct tcpcb *));
78 
79 #ifdef TCPDEBUG
80 #define	TCPDEBUG0	int ostate
81 #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
82 #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
83 				tcp_trace(TA_USER, ostate, tp, 0, req)
84 #else
85 #define	TCPDEBUG0
86 #define	TCPDEBUG1()
87 #define	TCPDEBUG2(req)
88 #endif
89 
90 /*
91  * TCP attaches to socket via pru_attach(), reserving space,
92  * and an internet control block.
93  */
94 static int
95 tcp_usr_attach(struct socket *so, int proto, struct proc *p)
96 {
97 	int s = splnet();
98 	int error;
99 	struct inpcb *inp = sotoinpcb(so);
100 	struct tcpcb *tp = 0;
101 	TCPDEBUG0;
102 
103 	TCPDEBUG1();
104 	if (inp) {
105 		error = EISCONN;
106 		goto out;
107 	}
108 
109 	error = tcp_attach(so, p);
110 	if (error)
111 		goto out;
112 
113 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
114 		so->so_linger = TCP_LINGERTIME * hz;
115 	tp = sototcpcb(so);
116 out:
117 	TCPDEBUG2(PRU_ATTACH);
118 	splx(s);
119 	return error;
120 }
121 
122 /*
123  * pru_detach() detaches the TCP protocol from the socket.
124  * If the protocol state is non-embryonic, then can't
125  * do this directly: have to initiate a pru_disconnect(),
126  * which may finish later; embryonic TCB's can just
127  * be discarded here.
128  */
129 static int
130 tcp_usr_detach(struct socket *so)
131 {
132 	int s = splnet();
133 	int error = 0;
134 	struct inpcb *inp = sotoinpcb(so);
135 	struct tcpcb *tp;
136 	TCPDEBUG0;
137 
138 	if (inp == 0) {
139 		splx(s);
140 		return EINVAL;	/* XXX */
141 	}
142 	tp = intotcpcb(inp);
143 	TCPDEBUG1();
144 	tp = tcp_disconnect(tp);
145 
146 	TCPDEBUG2(PRU_DETACH);
147 	splx(s);
148 	return error;
149 }
150 
151 #define	COMMON_START()	TCPDEBUG0; \
152 			do { \
153 				     if (inp == 0) { \
154 					     splx(s); \
155 					     return EINVAL; \
156 				     } \
157 				     tp = intotcpcb(inp); \
158 				     TCPDEBUG1(); \
159 		     } while(0)
160 
161 #define COMMON_END(req)	out: TCPDEBUG2(req); splx(s); return error; goto out
162 
163 
164 /*
165  * Give the socket an address.
166  */
167 static int
168 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
169 {
170 	int s = splnet();
171 	int error = 0;
172 	struct inpcb *inp = sotoinpcb(so);
173 	struct tcpcb *tp;
174 	struct sockaddr_in *sinp;
175 
176 	COMMON_START();
177 
178 	/*
179 	 * Must check for multicast addresses and disallow binding
180 	 * to them.
181 	 */
182 	sinp = (struct sockaddr_in *)nam;
183 	if (sinp->sin_family == AF_INET &&
184 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
185 		error = EAFNOSUPPORT;
186 		goto out;
187 	}
188 	error = in_pcbbind(inp, nam, p);
189 	if (error)
190 		goto out;
191 	COMMON_END(PRU_BIND);
192 
193 }
194 
195 /*
196  * Prepare to accept connections.
197  */
198 static int
199 tcp_usr_listen(struct socket *so, struct proc *p)
200 {
201 	int s = splnet();
202 	int error = 0;
203 	struct inpcb *inp = sotoinpcb(so);
204 	struct tcpcb *tp;
205 
206 	COMMON_START();
207 	if (inp->inp_lport == 0)
208 		error = in_pcbbind(inp, (struct sockaddr *)0, p);
209 	if (error == 0)
210 		tp->t_state = TCPS_LISTEN;
211 	COMMON_END(PRU_LISTEN);
212 }
213 
214 /*
215  * Initiate connection to peer.
216  * Create a template for use in transmissions on this connection.
217  * Enter SYN_SENT state, and mark socket as connecting.
218  * Start keep-alive timer, and seed output sequence space.
219  * Send initial segment on connection.
220  */
221 static int
222 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
223 {
224 	int s = splnet();
225 	int error = 0;
226 	struct inpcb *inp = sotoinpcb(so);
227 	struct tcpcb *tp;
228 	struct sockaddr_in *sinp;
229 
230 	COMMON_START();
231 
232 	/*
233 	 * Must disallow TCP ``connections'' to multicast addresses.
234 	 */
235 	sinp = (struct sockaddr_in *)nam;
236 	if (sinp->sin_family == AF_INET
237 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
238 		error = EAFNOSUPPORT;
239 		goto out;
240 	}
241 
242 	if ((error = tcp_connect(tp, nam, p)) != 0)
243 		goto out;
244 	error = tcp_output(tp);
245 	COMMON_END(PRU_CONNECT);
246 }
247 
248 /*
249  * Initiate disconnect from peer.
250  * If connection never passed embryonic stage, just drop;
251  * else if don't need to let data drain, then can just drop anyways,
252  * else have to begin TCP shutdown process: mark socket disconnecting,
253  * drain unread data, state switch to reflect user close, and
254  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
255  * when peer sends FIN and acks ours.
256  *
257  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
258  */
259 static int
260 tcp_usr_disconnect(struct socket *so)
261 {
262 	int s = splnet();
263 	int error = 0;
264 	struct inpcb *inp = sotoinpcb(so);
265 	struct tcpcb *tp;
266 
267 	COMMON_START();
268 	tp = tcp_disconnect(tp);
269 	COMMON_END(PRU_DISCONNECT);
270 }
271 
272 /*
273  * Accept a connection.  Essentially all the work is
274  * done at higher levels; just return the address
275  * of the peer, storing through addr.
276  */
277 static int
278 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
279 {
280 	int s = splnet();
281 	int error = 0;
282 	struct inpcb *inp = sotoinpcb(so);
283 	struct tcpcb *tp;
284 
285 	COMMON_START();
286 	in_setpeeraddr(so, nam);
287 	COMMON_END(PRU_ACCEPT);
288 }
289 
290 /*
291  * Mark the connection as being incapable of further output.
292  */
293 static int
294 tcp_usr_shutdown(struct socket *so)
295 {
296 	int s = splnet();
297 	int error = 0;
298 	struct inpcb *inp = sotoinpcb(so);
299 	struct tcpcb *tp;
300 
301 	COMMON_START();
302 	socantsendmore(so);
303 	tp = tcp_usrclosed(tp);
304 	if (tp)
305 		error = tcp_output(tp);
306 	COMMON_END(PRU_SHUTDOWN);
307 }
308 
309 /*
310  * After a receive, possibly send window update to peer.
311  */
312 static int
313 tcp_usr_rcvd(struct socket *so, int flags)
314 {
315 	int s = splnet();
316 	int error = 0;
317 	struct inpcb *inp = sotoinpcb(so);
318 	struct tcpcb *tp;
319 
320 	COMMON_START();
321 	tcp_output(tp);
322 	COMMON_END(PRU_RCVD);
323 }
324 
325 /*
326  * Do a send by putting data in output queue and updating urgent
327  * marker if URG set.  Possibly send more data.
328  */
329 static int
330 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
331 	     struct sockaddr *nam, struct mbuf *control, struct proc *p)
332 {
333 	int s = splnet();
334 	int error = 0;
335 	struct inpcb *inp = sotoinpcb(so);
336 	struct tcpcb *tp;
337 
338 	COMMON_START();
339 	if (control && control->m_len) {
340 		m_freem(control); /* XXX shouldn't caller do this??? */
341 		if (m)
342 			m_freem(m);
343 		error = EINVAL;
344 		goto out;
345 	}
346 
347 	if(!(flags & PRUS_OOB)) {
348 		sbappend(&so->so_snd, m);
349 		if (nam && tp->t_state < TCPS_SYN_SENT) {
350 			/*
351 			 * Do implied connect if not yet connected,
352 			 * initialize window to default value, and
353 			 * initialize maxseg/maxopd using peer's cached
354 			 * MSS.
355 			 */
356 			error = tcp_connect(tp, nam, p);
357 			if (error)
358 				goto out;
359 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
360 			tcp_mss(tp, -1);
361 		}
362 
363 		if (flags & PRUS_EOF) {
364 			/*
365 			 * Close the send side of the connection after
366 			 * the data is sent.
367 			 */
368 			socantsendmore(so);
369 			tp = tcp_usrclosed(tp);
370 		}
371 		if (tp != NULL)
372 			error = tcp_output(tp);
373 	} else {
374 		if (sbspace(&so->so_snd) < -512) {
375 			m_freem(m);
376 			error = ENOBUFS;
377 			goto out;
378 		}
379 		/*
380 		 * According to RFC961 (Assigned Protocols),
381 		 * the urgent pointer points to the last octet
382 		 * of urgent data.  We continue, however,
383 		 * to consider it to indicate the first octet
384 		 * of data past the urgent section.
385 		 * Otherwise, snd_up should be one lower.
386 		 */
387 		sbappend(&so->so_snd, m);
388 		if (nam && tp->t_state < TCPS_SYN_SENT) {
389 			/*
390 			 * Do implied connect if not yet connected,
391 			 * initialize window to default value, and
392 			 * initialize maxseg/maxopd using peer's cached
393 			 * MSS.
394 			 */
395 			error = tcp_connect(tp, nam, p);
396 			if (error)
397 				goto out;
398 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
399 			tcp_mss(tp, -1);
400 		}
401 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
402 		tp->t_force = 1;
403 		error = tcp_output(tp);
404 		tp->t_force = 0;
405 	}
406 	COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
407 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
408 }
409 
410 /*
411  * Abort the TCP.
412  */
413 static int
414 tcp_usr_abort(struct socket *so)
415 {
416 	int s = splnet();
417 	int error = 0;
418 	struct inpcb *inp = sotoinpcb(so);
419 	struct tcpcb *tp;
420 
421 	COMMON_START();
422 	tp = tcp_drop(tp, ECONNABORTED);
423 	COMMON_END(PRU_ABORT);
424 }
425 
426 /*
427  * Receive out-of-band data.
428  */
429 static int
430 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
431 {
432 	int s = splnet();
433 	int error = 0;
434 	struct inpcb *inp = sotoinpcb(so);
435 	struct tcpcb *tp;
436 
437 	COMMON_START();
438 	if ((so->so_oobmark == 0 &&
439 	     (so->so_state & SS_RCVATMARK) == 0) ||
440 	    so->so_options & SO_OOBINLINE ||
441 	    tp->t_oobflags & TCPOOB_HADDATA) {
442 		error = EINVAL;
443 		goto out;
444 	}
445 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
446 		error = EWOULDBLOCK;
447 		goto out;
448 	}
449 	m->m_len = 1;
450 	*mtod(m, caddr_t) = tp->t_iobc;
451 	if ((flags & MSG_PEEK) == 0)
452 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
453 	COMMON_END(PRU_RCVOOB);
454 }
455 
456 /* xxx - should be const */
457 struct pr_usrreqs tcp_usrreqs = {
458 	tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
459 	tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
460 	tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd,
461 	tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
462 	in_setsockaddr, sosend, soreceive, sopoll
463 };
464 
465 /*
466  * Common subroutine to open a TCP connection to remote host specified
467  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
468  * port number if needed.  Call in_pcbladdr to do the routing and to choose
469  * a local host address (interface).  If there is an existing incarnation
470  * of the same connection in TIME-WAIT state and if the remote host was
471  * sending CC options and if the connection duration was < MSL, then
472  * truncate the previous TIME-WAIT state and proceed.
473  * Initialize connection parameters and enter SYN-SENT state.
474  */
475 static int
476 tcp_connect(tp, nam, p)
477 	register struct tcpcb *tp;
478 	struct sockaddr *nam;
479 	struct proc *p;
480 {
481 	struct inpcb *inp = tp->t_inpcb, *oinp;
482 	struct socket *so = inp->inp_socket;
483 	struct tcpcb *otp;
484 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
485 	struct sockaddr_in *ifaddr;
486 	struct rmxp_tao *taop;
487 	struct rmxp_tao tao_noncached;
488 	int error;
489 
490 	if (inp->inp_lport == 0) {
491 		error = in_pcbbind(inp, (struct sockaddr *)0, p);
492 		if (error)
493 			return error;
494 	}
495 
496 	/*
497 	 * Cannot simply call in_pcbconnect, because there might be an
498 	 * earlier incarnation of this same connection still in
499 	 * TIME_WAIT state, creating an ADDRINUSE error.
500 	 */
501 	error = in_pcbladdr(inp, nam, &ifaddr);
502 	if (error)
503 		return error;
504 	oinp = in_pcblookup_hash(inp->inp_pcbinfo,
505 	    sin->sin_addr, sin->sin_port,
506 	    inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr
507 						: ifaddr->sin_addr,
508 	    inp->inp_lport,  0);
509 	if (oinp) {
510 		if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
511 		otp->t_state == TCPS_TIME_WAIT &&
512 		    otp->t_duration < TCPTV_MSL &&
513 		    (otp->t_flags & TF_RCVD_CC))
514 			otp = tcp_close(otp);
515 		else
516 			return EADDRINUSE;
517 	}
518 	if (inp->inp_laddr.s_addr == INADDR_ANY)
519 		inp->inp_laddr = ifaddr->sin_addr;
520 	inp->inp_faddr = sin->sin_addr;
521 	inp->inp_fport = sin->sin_port;
522 	in_pcbrehash(inp);
523 
524 	tp->t_template = tcp_template(tp);
525 	if (tp->t_template == 0) {
526 		in_pcbdisconnect(inp);
527 		return ENOBUFS;
528 	}
529 
530 	/* Compute window scaling to request.  */
531 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
532 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
533 		tp->request_r_scale++;
534 
535 	soisconnecting(so);
536 	tcpstat.tcps_connattempt++;
537 	tp->t_state = TCPS_SYN_SENT;
538 	tp->t_timer[TCPT_KEEP] = tcp_keepinit;
539 	tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
540 	tcp_sendseqinit(tp);
541 
542 	/*
543 	 * Generate a CC value for this connection and
544 	 * check whether CC or CCnew should be used.
545 	 */
546 	if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
547 		taop = &tao_noncached;
548 		bzero(taop, sizeof(*taop));
549 	}
550 
551 	tp->cc_send = CC_INC(tcp_ccgen);
552 	if (taop->tao_ccsent != 0 &&
553 	    CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
554 		taop->tao_ccsent = tp->cc_send;
555 	} else {
556 		taop->tao_ccsent = 0;
557 		tp->t_flags |= TF_SENDCCNEW;
558 	}
559 
560 	return 0;
561 }
562 
563 int
564 tcp_ctloutput(op, so, level, optname, mp, p)
565 	int op;
566 	struct socket *so;
567 	int level, optname;
568 	struct mbuf **mp;
569 	struct proc *p;
570 {
571 	int error = 0, s;
572 	struct inpcb *inp;
573 	register struct tcpcb *tp;
574 	register struct mbuf *m;
575 	register int i;
576 
577 	s = splnet();
578 	inp = sotoinpcb(so);
579 	if (inp == NULL) {
580 		splx(s);
581 		if (op == PRCO_SETOPT && *mp)
582 			(void) m_free(*mp);
583 		return (ECONNRESET);
584 	}
585 	if (level != IPPROTO_TCP) {
586 		error = ip_ctloutput(op, so, level, optname, mp, p);
587 		splx(s);
588 		return (error);
589 	}
590 	tp = intotcpcb(inp);
591 
592 	switch (op) {
593 
594 	case PRCO_SETOPT:
595 		m = *mp;
596 		switch (optname) {
597 
598 		case TCP_NODELAY:
599 			if (m == NULL || m->m_len < sizeof (int))
600 				error = EINVAL;
601 			else if (*mtod(m, int *))
602 				tp->t_flags |= TF_NODELAY;
603 			else
604 				tp->t_flags &= ~TF_NODELAY;
605 			break;
606 
607 		case TCP_MAXSEG:
608 			if (m && (i = *mtod(m, int *)) > 0 && i <= tp->t_maxseg)
609 				tp->t_maxseg = i;
610 			else
611 				error = EINVAL;
612 			break;
613 
614 		case TCP_NOOPT:
615 			if (m == NULL || m->m_len < sizeof (int))
616 				error = EINVAL;
617 			else if (*mtod(m, int *))
618 				tp->t_flags |= TF_NOOPT;
619 			else
620 				tp->t_flags &= ~TF_NOOPT;
621 			break;
622 
623 		case TCP_NOPUSH:
624 			if (m == NULL || m->m_len < sizeof (int))
625 				error = EINVAL;
626 			else if (*mtod(m, int *))
627 				tp->t_flags |= TF_NOPUSH;
628 			else
629 				tp->t_flags &= ~TF_NOPUSH;
630 			break;
631 
632 		default:
633 			error = ENOPROTOOPT;
634 			break;
635 		}
636 		if (m)
637 			(void) m_free(m);
638 		break;
639 
640 	case PRCO_GETOPT:
641 		*mp = m = m_get(M_WAIT, MT_SOOPTS);
642 		m->m_len = sizeof(int);
643 
644 		switch (optname) {
645 		case TCP_NODELAY:
646 			*mtod(m, int *) = tp->t_flags & TF_NODELAY;
647 			break;
648 		case TCP_MAXSEG:
649 			*mtod(m, int *) = tp->t_maxseg;
650 			break;
651 		case TCP_NOOPT:
652 			*mtod(m, int *) = tp->t_flags & TF_NOOPT;
653 			break;
654 		case TCP_NOPUSH:
655 			*mtod(m, int *) = tp->t_flags & TF_NOPUSH;
656 			break;
657 		default:
658 			error = ENOPROTOOPT;
659 			break;
660 		}
661 		break;
662 	}
663 	splx(s);
664 	return (error);
665 }
666 
667 /*
668  * tcp_sendspace and tcp_recvspace are the default send and receive window
669  * sizes, respectively.  These are obsolescent (this information should
670  * be set by the route).
671  */
672 u_long	tcp_sendspace = 1024*16;
673 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace,
674 	CTLFLAG_RW, &tcp_sendspace , 0, "");
675 u_long	tcp_recvspace = 1024*16;
676 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace,
677 	CTLFLAG_RW, &tcp_recvspace , 0, "");
678 
679 /*
680  * Attach TCP protocol to socket, allocating
681  * internet protocol control block, tcp control block,
682  * bufer space, and entering LISTEN state if to accept connections.
683  */
684 static int
685 tcp_attach(so, p)
686 	struct socket *so;
687 	struct proc *p;
688 {
689 	register struct tcpcb *tp;
690 	struct inpcb *inp;
691 	int error;
692 
693 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
694 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
695 		if (error)
696 			return (error);
697 	}
698 	error = in_pcballoc(so, &tcbinfo, p);
699 	if (error)
700 		return (error);
701 	inp = sotoinpcb(so);
702 	tp = tcp_newtcpcb(inp);
703 	if (tp == 0) {
704 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
705 
706 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
707 		in_pcbdetach(inp);
708 		so->so_state |= nofd;
709 		return (ENOBUFS);
710 	}
711 	tp->t_state = TCPS_CLOSED;
712 	return (0);
713 }
714 
715 /*
716  * Initiate (or continue) disconnect.
717  * If embryonic state, just send reset (once).
718  * If in ``let data drain'' option and linger null, just drop.
719  * Otherwise (hard), mark socket disconnecting and drop
720  * current input data; switch states based on user close, and
721  * send segment to peer (with FIN).
722  */
723 static struct tcpcb *
724 tcp_disconnect(tp)
725 	register struct tcpcb *tp;
726 {
727 	struct socket *so = tp->t_inpcb->inp_socket;
728 
729 	if (tp->t_state < TCPS_ESTABLISHED)
730 		tp = tcp_close(tp);
731 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
732 		tp = tcp_drop(tp, 0);
733 	else {
734 		soisdisconnecting(so);
735 		sbflush(&so->so_rcv);
736 		tp = tcp_usrclosed(tp);
737 		if (tp)
738 			(void) tcp_output(tp);
739 	}
740 	return (tp);
741 }
742 
743 /*
744  * User issued close, and wish to trail through shutdown states:
745  * if never received SYN, just forget it.  If got a SYN from peer,
746  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
747  * If already got a FIN from peer, then almost done; go to LAST_ACK
748  * state.  In all other cases, have already sent FIN to peer (e.g.
749  * after PRU_SHUTDOWN), and just have to play tedious game waiting
750  * for peer to send FIN or not respond to keep-alives, etc.
751  * We can let the user exit from the close as soon as the FIN is acked.
752  */
753 static struct tcpcb *
754 tcp_usrclosed(tp)
755 	register struct tcpcb *tp;
756 {
757 
758 	switch (tp->t_state) {
759 
760 	case TCPS_CLOSED:
761 	case TCPS_LISTEN:
762 		tp->t_state = TCPS_CLOSED;
763 		tp = tcp_close(tp);
764 		break;
765 
766 	case TCPS_SYN_SENT:
767 	case TCPS_SYN_RECEIVED:
768 		tp->t_flags |= TF_NEEDFIN;
769 		break;
770 
771 	case TCPS_ESTABLISHED:
772 		tp->t_state = TCPS_FIN_WAIT_1;
773 		break;
774 
775 	case TCPS_CLOSE_WAIT:
776 		tp->t_state = TCPS_LAST_ACK;
777 		break;
778 	}
779 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
780 		soisdisconnected(tp->t_inpcb->inp_socket);
781 		/* To prevent the connection hanging in FIN_WAIT_2 forever. */
782 		if (tp->t_state == TCPS_FIN_WAIT_2)
783 			tp->t_timer[TCPT_2MSL] = tcp_maxidle;
784 	}
785 	return (tp);
786 }
787 
788