Home | History | Annotate | Line # | Download | only in netinet
tcp_usrreq.c revision 1.45
      1 /*	$NetBSD: tcp_usrreq.c,v 1.45 2000/02/01 22:52:10 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
     34  * All rights reserved.
     35  *
     36  * This code is derived from software contributed to The NetBSD Foundation
     37  * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
     38  * Facility, NASA Ames Research Center.
     39  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  * 3. All advertising materials mentioning features or use of this software
     49  *    must display the following acknowledgement:
     50  *	This product includes software developed by the NetBSD
     51  *	Foundation, Inc. and its contributors.
     52  * 4. Neither the name of The NetBSD Foundation nor the names of its
     53  *    contributors may be used to endorse or promote products derived
     54  *    from this software without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     57  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     58  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     59  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     60  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     61  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     62  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     63  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     64  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     66  * POSSIBILITY OF SUCH DAMAGE.
     67  */
     68 
     69 /*
     70  * Copyright (c) 1982, 1986, 1988, 1993, 1995
     71  *	The Regents of the University of California.  All rights reserved.
     72  *
     73  * Redistribution and use in source and binary forms, with or without
     74  * modification, are permitted provided that the following conditions
     75  * are met:
     76  * 1. Redistributions of source code must retain the above copyright
     77  *    notice, this list of conditions and the following disclaimer.
     78  * 2. Redistributions in binary form must reproduce the above copyright
     79  *    notice, this list of conditions and the following disclaimer in the
     80  *    documentation and/or other materials provided with the distribution.
     81  * 3. All advertising materials mentioning features or use of this software
     82  *    must display the following acknowledgement:
     83  *	This product includes software developed by the University of
     84  *	California, Berkeley and its contributors.
     85  * 4. Neither the name of the University nor the names of its contributors
     86  *    may be used to endorse or promote products derived from this software
     87  *    without specific prior written permission.
     88  *
     89  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     90  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     91  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     92  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     93  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     94  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     95  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     96  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     97  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     98  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     99  * SUCH DAMAGE.
    100  *
    101  *	@(#)tcp_usrreq.c	8.5 (Berkeley) 6/21/95
    102  */
    103 
    104 #include "opt_inet.h"
    105 #include "opt_ipsec.h"
    106 
    107 #include <sys/param.h>
    108 #include <sys/systm.h>
    109 #include <sys/kernel.h>
    110 #include <sys/malloc.h>
    111 #include <sys/mbuf.h>
    112 #include <sys/socket.h>
    113 #include <sys/socketvar.h>
    114 #include <sys/protosw.h>
    115 #include <sys/errno.h>
    116 #include <sys/stat.h>
    117 #include <sys/proc.h>
    118 #include <sys/ucred.h>
    119 #include <sys/domain.h>
    120 
    121 #include <vm/vm.h>
    122 #include <sys/sysctl.h>
    123 
    124 #include <net/if.h>
    125 #include <net/route.h>
    126 
    127 #include <netinet/in.h>
    128 #include <netinet/in_systm.h>
    129 #include <netinet/in_var.h>
    130 #include <netinet/ip.h>
    131 #include <netinet/in_pcb.h>
    132 #include <netinet/ip_var.h>
    133 
    134 #ifdef INET6
    135 #ifndef INET
    136 #include <netinet/in.h>
    137 #endif
    138 #include <netinet/ip6.h>
    139 #include <netinet6/in6_pcb.h>
    140 #include <netinet6/ip6_var.h>
    141 #endif
    142 
    143 #include <netinet/tcp.h>
    144 #include <netinet/tcp_fsm.h>
    145 #include <netinet/tcp_seq.h>
    146 #include <netinet/tcp_timer.h>
    147 #include <netinet/tcp_var.h>
    148 #include <netinet/tcpip.h>
    149 #include <netinet/tcp_debug.h>
    150 
    151 #include "opt_tcp_recvspace.h"
    152 #include "opt_tcp_sendspace.h"
    153 
    154 #ifdef IPSEC
    155 #include <netinet6/ipsec.h>
    156 #endif /*IPSEC*/
    157 
    158 /*
    159  * TCP protocol interface to socket abstraction.
    160  */
    161 extern	char *tcpstates[];
    162 
    163 /*
    164  * Process a TCP user request for TCP tb.  If this is a send request
    165  * then m is the mbuf chain of send data.  If this is a timer expiration
    166  * (called from the software clock routine), then timertype tells which timer.
    167  */
    168 /*ARGSUSED*/
    169 int
    170 tcp_usrreq(so, req, m, nam, control, p)
    171 	struct socket *so;
    172 	int req;
    173 	struct mbuf *m, *nam, *control;
    174 	struct proc *p;
    175 {
    176 	register struct inpcb *inp;
    177 #ifdef INET6
    178 	register struct in6pcb *in6p;
    179 #endif
    180 	register struct tcpcb *tp = NULL;
    181 	int s;
    182 	int error = 0;
    183 	int ostate;
    184 	int family;	/* family of the socket */
    185 
    186 	family = so->so_proto->pr_domain->dom_family;
    187 
    188 	if (req == PRU_CONTROL) {
    189 		switch (family) {
    190 		case PF_INET:
    191 			return (in_control(so, (long)m, (caddr_t)nam,
    192 			    (struct ifnet *)control, p));
    193 #ifdef INET6
    194 		case PF_INET6:
    195 			return (in6_control(so, (long)m, (caddr_t)nam,
    196 			    (struct ifnet *)control, p));
    197 #endif
    198 		default:
    199 			return EAFNOSUPPORT;
    200 		}
    201 	}
    202 
    203 	if (req == PRU_PURGEADDR) {
    204 		in_purgeaddr((struct ifaddr *)nam, (struct ifnet *)control);
    205 		return (0);
    206 	}
    207 
    208 	s = splsoftnet();
    209 	switch (family) {
    210 	case PF_INET:
    211 		inp = sotoinpcb(so);
    212 #ifdef INET6
    213 		in6p = NULL;
    214 #endif
    215 		break;
    216 #ifdef INET6
    217 	case PF_INET6:
    218 		inp = NULL;
    219 		in6p = sotoin6pcb(so);
    220 		break;
    221 #endif
    222 	default:
    223 		splx(s);
    224 		return EAFNOSUPPORT;
    225 	}
    226 
    227 #ifdef DIAGNOSTIC
    228 	if (req != PRU_SEND && req != PRU_SENDOOB && control)
    229 		panic("tcp_usrreq: unexpected control mbuf");
    230 #endif
    231 	/*
    232 	 * When a TCP is attached to a socket, then there will be
    233 	 * a (struct inpcb) pointed at by the socket, and this
    234 	 * structure will point at a subsidary (struct tcpcb).
    235 	 */
    236 #ifndef INET6
    237 	if (inp == 0 && req != PRU_ATTACH)
    238 #else
    239 	if ((inp == 0 && in6p == 0) && req != PRU_ATTACH)
    240 #endif
    241 	{
    242 		error = EINVAL;
    243 		goto release;
    244 	}
    245 	if (inp) {
    246 		tp = intotcpcb(inp);
    247 		/* WHAT IF TP IS 0? */
    248 #ifdef KPROF
    249 		tcp_acounts[tp->t_state][req]++;
    250 #endif
    251 		ostate = tp->t_state;
    252 	}
    253 #ifdef INET6
    254 	else if (in6p) {
    255 		tp = in6totcpcb(in6p);
    256 		/* WHAT IF TP IS 0? */
    257 #ifdef KPROF
    258 		tcp_acounts[tp->t_state][req]++;
    259 #endif
    260 		ostate = tp->t_state;
    261 	}
    262 #endif
    263 	else
    264 		ostate = 0;
    265 
    266 	switch (req) {
    267 
    268 	/*
    269 	 * TCP attaches to socket via PRU_ATTACH, reserving space,
    270 	 * and an internet control block.
    271 	 */
    272 	case PRU_ATTACH:
    273 #ifndef INET6
    274 		if (inp != 0)
    275 #else
    276 		if (inp != 0 || in6p != 0)
    277 #endif
    278 		{
    279 			error = EISCONN;
    280 			break;
    281 		}
    282 		error = tcp_attach(so);
    283 		if (error)
    284 			break;
    285 		if ((so->so_options & SO_LINGER) && so->so_linger == 0)
    286 			so->so_linger = TCP_LINGERTIME;
    287 		tp = sototcpcb(so);
    288 		break;
    289 
    290 	/*
    291 	 * PRU_DETACH detaches the TCP protocol from the socket.
    292 	 */
    293 	case PRU_DETACH:
    294 		tp = tcp_disconnect(tp);
    295 		break;
    296 
    297 	/*
    298 	 * Give the socket an address.
    299 	 */
    300 	case PRU_BIND:
    301 		switch (family) {
    302 		case PF_INET:
    303 			error = in_pcbbind(inp, nam, p);
    304 			break;
    305 #ifdef INET6
    306 		case PF_INET6:
    307 			error = in6_pcbbind(in6p, nam /*, p*/ );
    308 			/* mapped addr case */
    309 			if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
    310 				tp->t_family = AF_INET;
    311 			break;
    312 #endif
    313 		}
    314 		break;
    315 
    316 	/*
    317 	 * Prepare to accept connections.
    318 	 */
    319 	case PRU_LISTEN:
    320 		if (inp && inp->inp_lport == 0) {
    321 			error = in_pcbbind(inp, (struct mbuf *)0,
    322 			    (struct proc *)0);
    323 			if (error)
    324 				break;
    325 		}
    326 #ifdef INET6
    327 		else if (in6p && in6p->in6p_lport == 0) {
    328 			error = in6_pcbbind(in6p, (struct mbuf *)0 /*,
    329 			    (struct proc *)0 */ );
    330 			if (error)
    331 				break;
    332 		}
    333 #endif
    334 		tp->t_state = TCPS_LISTEN;
    335 		break;
    336 
    337 	/*
    338 	 * Initiate connection to peer.
    339 	 * Create a template for use in transmissions on this connection.
    340 	 * Enter SYN_SENT state, and mark socket as connecting.
    341 	 * Start keep-alive timer, and seed output sequence space.
    342 	 * Send initial segment on connection.
    343 	 */
    344 	case PRU_CONNECT:
    345 		if (inp) {
    346 			if (inp->inp_lport == 0) {
    347 				error = in_pcbbind(inp, (struct mbuf *)0,
    348 				    (struct proc *)0);
    349 				if (error)
    350 					break;
    351 			}
    352 			error = in_pcbconnect(inp, nam);
    353 		}
    354 #ifdef INET6
    355 		else if (in6p) {
    356 			if (in6p->in6p_lport == 0) {
    357 				error = in6_pcbbind(in6p, (struct mbuf *)0 /*,
    358 				    (struct proc *)0 */ );
    359 				if (error)
    360 					break;
    361 			}
    362 			error = in6_pcbconnect(in6p, nam);
    363 			/* mapped addr case */
    364 			if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr))
    365 				tp->t_family = AF_INET;
    366 		}
    367 #endif
    368 		if (error)
    369 			break;
    370 		tp->t_template = tcp_template(tp);
    371 		if (tp->t_template == 0) {
    372 			if (inp)
    373 				in_pcbdisconnect(inp);
    374 #ifdef INET6
    375 			else if (in6p)
    376 				in6_pcbdisconnect(in6p);
    377 #endif
    378 			error = ENOBUFS;
    379 			break;
    380 		}
    381 		/* Compute window scaling to request.  */
    382 		while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
    383 		    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
    384 			tp->request_r_scale++;
    385 		soisconnecting(so);
    386 		tcpstat.tcps_connattempt++;
    387 		tp->t_state = TCPS_SYN_SENT;
    388 		TCP_TIMER_ARM(tp, TCPT_KEEP, TCPTV_KEEP_INIT);
    389 		tp->iss = tcp_new_iss(tp, sizeof(struct tcpcb), 0);
    390 		tcp_sendseqinit(tp);
    391 		error = tcp_output(tp);
    392 		break;
    393 
    394 	/*
    395 	 * Create a TCP connection between two sockets.
    396 	 */
    397 	case PRU_CONNECT2:
    398 		error = EOPNOTSUPP;
    399 		break;
    400 
    401 	/*
    402 	 * Initiate disconnect from peer.
    403 	 * If connection never passed embryonic stage, just drop;
    404 	 * else if don't need to let data drain, then can just drop anyways,
    405 	 * else have to begin TCP shutdown process: mark socket disconnecting,
    406 	 * drain unread data, state switch to reflect user close, and
    407 	 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
    408 	 * when peer sends FIN and acks ours.
    409 	 *
    410 	 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
    411 	 */
    412 	case PRU_DISCONNECT:
    413 		tp = tcp_disconnect(tp);
    414 		break;
    415 
    416 	/*
    417 	 * Accept a connection.  Essentially all the work is
    418 	 * done at higher levels; just return the address
    419 	 * of the peer, storing through addr.
    420 	 */
    421 	case PRU_ACCEPT:
    422 		if (inp)
    423 			in_setpeeraddr(inp, nam);
    424 #ifdef INET6
    425 		else if (in6p)
    426 			in6_setpeeraddr(in6p, nam);
    427 #endif
    428 		break;
    429 
    430 	/*
    431 	 * Mark the connection as being incapable of further output.
    432 	 */
    433 	case PRU_SHUTDOWN:
    434 		socantsendmore(so);
    435 		tp = tcp_usrclosed(tp);
    436 		if (tp)
    437 			error = tcp_output(tp);
    438 		break;
    439 
    440 	/*
    441 	 * After a receive, possibly send window update to peer.
    442 	 */
    443 	case PRU_RCVD:
    444 		(void) tcp_output(tp);
    445 		break;
    446 
    447 	/*
    448 	 * Do a send by putting data in output queue and updating urgent
    449 	 * marker if URG set.  Possibly send more data.
    450 	 */
    451 	case PRU_SEND:
    452 		if (control && control->m_len) {
    453 			m_freem(control);
    454 			m_freem(m);
    455 			error = EINVAL;
    456 			break;
    457 		}
    458 		sbappend(&so->so_snd, m);
    459 		error = tcp_output(tp);
    460 		break;
    461 
    462 	/*
    463 	 * Abort the TCP.
    464 	 */
    465 	case PRU_ABORT:
    466 		tp = tcp_drop(tp, ECONNABORTED);
    467 		break;
    468 
    469 	case PRU_SENSE:
    470 		/*
    471 		 * stat: don't bother with a blocksize.
    472 		 */
    473 		splx(s);
    474 		return (0);
    475 
    476 	case PRU_RCVOOB:
    477 		if (control && control->m_len) {
    478 			m_freem(control);
    479 			m_freem(m);
    480 			error = EINVAL;
    481 			break;
    482 		}
    483 		if ((so->so_oobmark == 0 &&
    484 		    (so->so_state & SS_RCVATMARK) == 0) ||
    485 		    so->so_options & SO_OOBINLINE ||
    486 		    tp->t_oobflags & TCPOOB_HADDATA) {
    487 			error = EINVAL;
    488 			break;
    489 		}
    490 		if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
    491 			error = EWOULDBLOCK;
    492 			break;
    493 		}
    494 		m->m_len = 1;
    495 		*mtod(m, caddr_t) = tp->t_iobc;
    496 		if (((long)nam & MSG_PEEK) == 0)
    497 			tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
    498 		break;
    499 
    500 	case PRU_SENDOOB:
    501 		if (sbspace(&so->so_snd) < -512) {
    502 			m_freem(m);
    503 			error = ENOBUFS;
    504 			break;
    505 		}
    506 		/*
    507 		 * According to RFC961 (Assigned Protocols),
    508 		 * the urgent pointer points to the last octet
    509 		 * of urgent data.  We continue, however,
    510 		 * to consider it to indicate the first octet
    511 		 * of data past the urgent section.
    512 		 * Otherwise, snd_up should be one lower.
    513 		 */
    514 		sbappend(&so->so_snd, m);
    515 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
    516 		tp->t_force = 1;
    517 		error = tcp_output(tp);
    518 		tp->t_force = 0;
    519 		break;
    520 
    521 	case PRU_SOCKADDR:
    522 		if (inp)
    523 			in_setsockaddr(inp, nam);
    524 #ifdef INET6
    525 		else if (in6p)
    526 			in6_setsockaddr(in6p, nam);
    527 #endif
    528 		break;
    529 
    530 	case PRU_PEERADDR:
    531 		if (inp)
    532 			in_setpeeraddr(inp, nam);
    533 #ifdef INET6
    534 		else if (in6p)
    535 			in6_setpeeraddr(in6p, nam);
    536 #endif
    537 		break;
    538 
    539 	/*
    540 	 * TCP slow timer went off; going through this
    541 	 * routine for tracing's sake.
    542 	 */
    543 	case PRU_SLOWTIMO:
    544 		tp = tcp_timers(tp, (long)nam);
    545 		req |= (long)nam << 8;		/* for debug's sake */
    546 		break;
    547 
    548 	default:
    549 		panic("tcp_usrreq");
    550 	}
    551 	if (tp && (so->so_options & SO_DEBUG))
    552 		tcp_trace(TA_USER, ostate, tp, NULL, req);
    553 
    554 release:
    555 	splx(s);
    556 	return (error);
    557 }
    558 
    559 int
    560 tcp_ctloutput(op, so, level, optname, mp)
    561 	int op;
    562 	struct socket *so;
    563 	int level, optname;
    564 	struct mbuf **mp;
    565 {
    566 	int error = 0, s;
    567 	struct inpcb *inp;
    568 #ifdef INET6
    569 	register struct in6pcb *in6p;
    570 #endif
    571 	register struct tcpcb *tp;
    572 	register struct mbuf *m;
    573 	register int i;
    574 	int family;	/* family of the socket */
    575 
    576 	family = so->so_proto->pr_domain->dom_family;
    577 
    578 	s = splsoftnet();
    579 	switch (family) {
    580 	case PF_INET:
    581 		inp = sotoinpcb(so);
    582 #ifdef INET6
    583 		in6p = NULL;
    584 #endif
    585 		break;
    586 #ifdef INET6
    587 	case PF_INET6:
    588 		inp = NULL;
    589 		in6p = sotoin6pcb(so);
    590 		break;
    591 #endif
    592 	default:
    593 		splx(s);
    594 		return EAFNOSUPPORT;
    595 	}
    596 #ifndef INET6
    597 	if (inp == NULL)
    598 #else
    599 	if (inp == NULL && in6p == NULL)
    600 #endif
    601 	{
    602 		splx(s);
    603 		if (op == PRCO_SETOPT && *mp)
    604 			(void) m_free(*mp);
    605 		return (ECONNRESET);
    606 	}
    607 	if (level != IPPROTO_TCP) {
    608 		switch (family) {
    609 		case PF_INET:
    610 			error = ip_ctloutput(op, so, level, optname, mp);
    611 			break;
    612 #ifdef INET6
    613 		case PF_INET6:
    614 			error = ip6_ctloutput(op, so, level, optname, mp);
    615 			break;
    616 #endif
    617 		}
    618 		splx(s);
    619 		return (error);
    620 	}
    621 	if (inp)
    622 		tp = intotcpcb(inp);
    623 #ifdef INET6
    624 	else if (in6p)
    625 		tp = in6totcpcb(in6p);
    626 #endif
    627 	else
    628 		tp = NULL;
    629 
    630 	switch (op) {
    631 
    632 	case PRCO_SETOPT:
    633 		m = *mp;
    634 		switch (optname) {
    635 
    636 		case TCP_NODELAY:
    637 			if (m == NULL || m->m_len < sizeof (int))
    638 				error = EINVAL;
    639 			else if (*mtod(m, int *))
    640 				tp->t_flags |= TF_NODELAY;
    641 			else
    642 				tp->t_flags &= ~TF_NODELAY;
    643 			break;
    644 
    645 		case TCP_MAXSEG:
    646 			if (m && (i = *mtod(m, int *)) > 0 &&
    647 			    i <= tp->t_peermss)
    648 				tp->t_peermss = i;  /* limit on send size */
    649 			else
    650 				error = EINVAL;
    651 			break;
    652 
    653 		default:
    654 			error = ENOPROTOOPT;
    655 			break;
    656 		}
    657 		if (m)
    658 			(void) m_free(m);
    659 		break;
    660 
    661 	case PRCO_GETOPT:
    662 		*mp = m = m_get(M_WAIT, MT_SOOPTS);
    663 		m->m_len = sizeof(int);
    664 
    665 		switch (optname) {
    666 		case TCP_NODELAY:
    667 			*mtod(m, int *) = tp->t_flags & TF_NODELAY;
    668 			break;
    669 		case TCP_MAXSEG:
    670 			*mtod(m, int *) = tp->t_peermss;
    671 			break;
    672 		default:
    673 			error = ENOPROTOOPT;
    674 			break;
    675 		}
    676 		break;
    677 	}
    678 	splx(s);
    679 	return (error);
    680 }
    681 
    682 #ifndef TCP_SENDSPACE
    683 #define	TCP_SENDSPACE	1024*16;
    684 #endif
    685 int	tcp_sendspace = TCP_SENDSPACE;
    686 #ifndef TCP_RECVSPACE
    687 #define	TCP_RECVSPACE	1024*16;
    688 #endif
    689 int	tcp_recvspace = TCP_RECVSPACE;
    690 
    691 /*
    692  * Attach TCP protocol to socket, allocating
    693  * internet protocol control block, tcp control block,
    694  * bufer space, and entering LISTEN state if to accept connections.
    695  */
    696 int
    697 tcp_attach(so)
    698 	struct socket *so;
    699 {
    700 	register struct tcpcb *tp;
    701 	struct inpcb *inp;
    702 #ifdef INET6
    703 	struct in6pcb *in6p;
    704 #endif
    705 	int error;
    706 	int family;	/* family of the socket */
    707 
    708 	family = so->so_proto->pr_domain->dom_family;
    709 
    710 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    711 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
    712 		if (error)
    713 			return (error);
    714 	}
    715 	switch (family) {
    716 	case PF_INET:
    717 		error = in_pcballoc(so, &tcbtable);
    718 		if (error)
    719 			return (error);
    720 		inp = sotoinpcb(so);
    721 #ifdef INET6
    722 		in6p = NULL;
    723 #endif
    724 		break;
    725 #ifdef INET6
    726 	case PF_INET6:
    727 		error = in6_pcballoc(so, &tcb6);
    728 		if (error)
    729 			return (error);
    730 		inp = NULL;
    731 		in6p = sotoin6pcb(so);
    732 		break;
    733 #endif
    734 	default:
    735 		return EAFNOSUPPORT;
    736 	}
    737 #ifdef IPSEC
    738 	if (inp) {
    739 		error = ipsec_init_policy(so, &inp->inp_sp);
    740 		if (error != 0) {
    741 			in_pcbdetach(inp);
    742 			return (error);
    743 		}
    744 	}
    745 #ifdef INET6
    746 	else if (in6p) {
    747 		error = ipsec_init_policy(so, &in6p->in6p_sp);
    748 		if (error != 0) {
    749 			in6_pcbdetach(in6p);
    750 			return (error);
    751 		}
    752 	}
    753 #endif
    754 #endif /*IPSEC*/
    755 	if (inp)
    756 		tp = tcp_newtcpcb(family, (void *)inp);
    757 #ifdef INET6
    758 	else if (in6p)
    759 		tp = tcp_newtcpcb(family, (void *)in6p);
    760 #endif
    761 	else
    762 		tp = NULL;
    763 
    764 	if (tp == 0) {
    765 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
    766 
    767 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
    768 		if (inp)
    769 			in_pcbdetach(inp);
    770 #ifdef INET6
    771 		else if (in6p)
    772 			in6_pcbdetach(in6p);
    773 #endif
    774 		so->so_state |= nofd;
    775 		return (ENOBUFS);
    776 	}
    777 	tp->t_state = TCPS_CLOSED;
    778 	return (0);
    779 }
    780 
    781 /*
    782  * Initiate (or continue) disconnect.
    783  * If embryonic state, just send reset (once).
    784  * If in ``let data drain'' option and linger null, just drop.
    785  * Otherwise (hard), mark socket disconnecting and drop
    786  * current input data; switch states based on user close, and
    787  * send segment to peer (with FIN).
    788  */
    789 struct tcpcb *
    790 tcp_disconnect(tp)
    791 	register struct tcpcb *tp;
    792 {
    793 	struct socket *so;
    794 
    795 	if (tp->t_inpcb)
    796 		so = tp->t_inpcb->inp_socket;
    797 #ifdef INET6
    798 	else if (tp->t_in6pcb)
    799 		so = tp->t_in6pcb->in6p_socket;
    800 #endif
    801 	else
    802 		so = NULL;
    803 
    804 	if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
    805 		tp = tcp_close(tp);
    806 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
    807 		tp = tcp_drop(tp, 0);
    808 	else {
    809 		soisdisconnecting(so);
    810 		sbflush(&so->so_rcv);
    811 		tp = tcp_usrclosed(tp);
    812 		if (tp)
    813 			(void) tcp_output(tp);
    814 	}
    815 	return (tp);
    816 }
    817 
    818 /*
    819  * User issued close, and wish to trail through shutdown states:
    820  * if never received SYN, just forget it.  If got a SYN from peer,
    821  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
    822  * If already got a FIN from peer, then almost done; go to LAST_ACK
    823  * state.  In all other cases, have already sent FIN to peer (e.g.
    824  * after PRU_SHUTDOWN), and just have to play tedious game waiting
    825  * for peer to send FIN or not respond to keep-alives, etc.
    826  * We can let the user exit from the close as soon as the FIN is acked.
    827  */
    828 struct tcpcb *
    829 tcp_usrclosed(tp)
    830 	register struct tcpcb *tp;
    831 {
    832 
    833 	switch (tp->t_state) {
    834 
    835 	case TCPS_CLOSED:
    836 	case TCPS_LISTEN:
    837 	case TCPS_SYN_SENT:
    838 		tp->t_state = TCPS_CLOSED;
    839 		tp = tcp_close(tp);
    840 		break;
    841 
    842 	case TCPS_SYN_RECEIVED:
    843 	case TCPS_ESTABLISHED:
    844 		tp->t_state = TCPS_FIN_WAIT_1;
    845 		break;
    846 
    847 	case TCPS_CLOSE_WAIT:
    848 		tp->t_state = TCPS_LAST_ACK;
    849 		break;
    850 	}
    851 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
    852 		struct socket *so;
    853 		if (tp->t_inpcb)
    854 			so = tp->t_inpcb->inp_socket;
    855 #ifdef INET6
    856 		else if (tp->t_in6pcb)
    857 			so = tp->t_in6pcb->in6p_socket;
    858 #endif
    859 		else
    860 			so = NULL;
    861 		soisdisconnected(so);
    862 		/*
    863 		 * If we are in FIN_WAIT_2, we arrived here because the
    864 		 * application did a shutdown of the send side.  Like the
    865 		 * case of a transition from FIN_WAIT_1 to FIN_WAIT_2 after
    866 		 * a full close, we start a timer to make sure sockets are
    867 		 * not left in FIN_WAIT_2 forever.
    868 		 */
    869 		if ((tp->t_state == TCPS_FIN_WAIT_2) && (tcp_maxidle > 0))
    870 			TCP_TIMER_ARM(tp, TCPT_2MSL, tcp_maxidle);
    871 	}
    872 	return (tp);
    873 }
    874 
    875 static struct {
    876 	 unsigned int valid : 1;
    877 	 unsigned int rdonly : 1;
    878 	 int *var;
    879 	 int val;
    880 	 } tcp_ctlvars[] = TCPCTL_VARIABLES;
    881 
    882 /*
    883  * Sysctl for tcp variables.
    884  */
    885 int
    886 tcp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
    887 	int *name;
    888 	u_int namelen;
    889 	void *oldp;
    890 	size_t *oldlenp;
    891 	void *newp;
    892 	size_t newlen;
    893 {
    894 	/* All sysctl names at this level are terminal. */
    895 	if (namelen != 1)
    896 		return (ENOTDIR);
    897 
    898 	if (name[0] < sizeof(tcp_ctlvars)/sizeof(tcp_ctlvars[0])
    899 	    && tcp_ctlvars[name[0]].valid) {
    900 		if (tcp_ctlvars[name[0]].rdonly)
    901 			return (sysctl_rdint(oldp, oldlenp, newp,
    902 			    tcp_ctlvars[name[0]].val));
    903 		else
    904 			return (sysctl_int(oldp, oldlenp, newp, newlen,
    905 			    tcp_ctlvars[name[0]].var));
    906 	}
    907 
    908 	return (ENOPROTOOPT);
    909 }
    910