Home | History | Annotate | Line # | Download | only in netinet
tcp_usrreq.c revision 1.42.2.1
      1 /*	$NetBSD: tcp_usrreq.c,v 1.42.2.1 2000/11/20 18:10:37 bouyer 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 <uvm/uvm_extern.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 	struct inpcb *inp;
    177 #ifdef INET6
    178 	struct in6pcb *in6p;
    179 #endif
    180 	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 #ifdef INET
    191 		case PF_INET:
    192 			return (in_control(so, (long)m, (caddr_t)nam,
    193 			    (struct ifnet *)control, p));
    194 #endif
    195 #ifdef INET6
    196 		case PF_INET6:
    197 			return (in6_control(so, (long)m, (caddr_t)nam,
    198 			    (struct ifnet *)control, p));
    199 #endif
    200 		default:
    201 			return EAFNOSUPPORT;
    202 		}
    203 	}
    204 
    205 	if (req == PRU_PURGEIF) {
    206 		switch (family) {
    207 #ifdef INET
    208 		case PF_INET:
    209 			in_purgeif((struct ifnet *)control);
    210 			in_pcbpurgeif(&tcbtable, (struct ifnet *)control);
    211 			break;
    212 #endif
    213 #ifdef INET6
    214 		case PF_INET6:
    215 			in6_purgeif((struct ifnet *)control);
    216 			in6_pcbpurgeif(&tcb6, (struct ifnet *)control);
    217 			break;
    218 #endif
    219 		default:
    220 			return (EAFNOSUPPORT);
    221 		}
    222 		return (0);
    223 	}
    224 
    225 	s = splsoftnet();
    226 	switch (family) {
    227 #ifdef INET
    228 	case PF_INET:
    229 		inp = sotoinpcb(so);
    230 #ifdef INET6
    231 		in6p = NULL;
    232 #endif
    233 		break;
    234 #endif
    235 #ifdef INET6
    236 	case PF_INET6:
    237 		inp = NULL;
    238 		in6p = sotoin6pcb(so);
    239 		break;
    240 #endif
    241 	default:
    242 		splx(s);
    243 		return EAFNOSUPPORT;
    244 	}
    245 
    246 #ifdef DIAGNOSTIC
    247 #ifdef INET6
    248 	if (inp && in6p)
    249 		panic("tcp_usrreq: both inp and in6p set to non-NULL");
    250 #endif
    251 	if (req != PRU_SEND && req != PRU_SENDOOB && control)
    252 		panic("tcp_usrreq: unexpected control mbuf");
    253 #endif
    254 	/*
    255 	 * When a TCP is attached to a socket, then there will be
    256 	 * a (struct inpcb) pointed at by the socket, and this
    257 	 * structure will point at a subsidary (struct tcpcb).
    258 	 */
    259 #ifndef INET6
    260 	if (inp == 0 && req != PRU_ATTACH)
    261 #else
    262 	if ((inp == 0 && in6p == 0) && req != PRU_ATTACH)
    263 #endif
    264 	{
    265 		error = EINVAL;
    266 		goto release;
    267 	}
    268 #ifdef INET
    269 	if (inp) {
    270 		tp = intotcpcb(inp);
    271 		/* WHAT IF TP IS 0? */
    272 #ifdef KPROF
    273 		tcp_acounts[tp->t_state][req]++;
    274 #endif
    275 		ostate = tp->t_state;
    276 	}
    277 #endif
    278 #ifdef INET6
    279 	if (in6p) {
    280 		tp = in6totcpcb(in6p);
    281 		/* WHAT IF TP IS 0? */
    282 #ifdef KPROF
    283 		tcp_acounts[tp->t_state][req]++;
    284 #endif
    285 		ostate = tp->t_state;
    286 	}
    287 #endif
    288 	else
    289 		ostate = 0;
    290 
    291 	switch (req) {
    292 
    293 	/*
    294 	 * TCP attaches to socket via PRU_ATTACH, reserving space,
    295 	 * and an internet control block.
    296 	 */
    297 	case PRU_ATTACH:
    298 #ifndef INET6
    299 		if (inp != 0)
    300 #else
    301 		if (inp != 0 || in6p != 0)
    302 #endif
    303 		{
    304 			error = EISCONN;
    305 			break;
    306 		}
    307 		error = tcp_attach(so);
    308 		if (error)
    309 			break;
    310 		if ((so->so_options & SO_LINGER) && so->so_linger == 0)
    311 			so->so_linger = TCP_LINGERTIME;
    312 		tp = sototcpcb(so);
    313 		break;
    314 
    315 	/*
    316 	 * PRU_DETACH detaches the TCP protocol from the socket.
    317 	 */
    318 	case PRU_DETACH:
    319 		tp = tcp_disconnect(tp);
    320 		break;
    321 
    322 	/*
    323 	 * Give the socket an address.
    324 	 */
    325 	case PRU_BIND:
    326 		switch (family) {
    327 #ifdef INET
    328 		case PF_INET:
    329 			error = in_pcbbind(inp, nam, p);
    330 			break;
    331 #endif
    332 #ifdef INET6
    333 		case PF_INET6:
    334 			error = in6_pcbbind(in6p, nam, p);
    335 			/* mapped addr case */
    336 			if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
    337 				tp->t_family = AF_INET;
    338 			break;
    339 #endif
    340 		}
    341 		break;
    342 
    343 	/*
    344 	 * Prepare to accept connections.
    345 	 */
    346 	case PRU_LISTEN:
    347 #ifdef INET
    348 		if (inp && inp->inp_lport == 0) {
    349 			error = in_pcbbind(inp, (struct mbuf *)0,
    350 			    (struct proc *)0);
    351 			if (error)
    352 				break;
    353 		}
    354 #endif
    355 #ifdef INET6
    356 		if (in6p && in6p->in6p_lport == 0) {
    357 			error = in6_pcbbind(in6p, (struct mbuf *)0,
    358 			    (struct proc *)0);
    359 			if (error)
    360 				break;
    361 		}
    362 #endif
    363 		tp->t_state = TCPS_LISTEN;
    364 		break;
    365 
    366 	/*
    367 	 * Initiate connection to peer.
    368 	 * Create a template for use in transmissions on this connection.
    369 	 * Enter SYN_SENT state, and mark socket as connecting.
    370 	 * Start keep-alive timer, and seed output sequence space.
    371 	 * Send initial segment on connection.
    372 	 */
    373 	case PRU_CONNECT:
    374 #ifdef INET
    375 		if (inp) {
    376 			if (inp->inp_lport == 0) {
    377 				error = in_pcbbind(inp, (struct mbuf *)0,
    378 				    (struct proc *)0);
    379 				if (error)
    380 					break;
    381 			}
    382 			error = in_pcbconnect(inp, nam);
    383 		}
    384 #endif
    385 #ifdef INET6
    386 		if (in6p) {
    387 			if (in6p->in6p_lport == 0) {
    388 				error = in6_pcbbind(in6p, (struct mbuf *)0,
    389 				    (struct proc *)0);
    390 				if (error)
    391 					break;
    392 			}
    393 			error = in6_pcbconnect(in6p, nam);
    394 			/* mapped addr case */
    395 			if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr))
    396 				tp->t_family = AF_INET;
    397 		}
    398 #endif
    399 		if (error)
    400 			break;
    401 		tp->t_template = tcp_template(tp);
    402 		if (tp->t_template == 0) {
    403 #ifdef INET
    404 			if (inp)
    405 				in_pcbdisconnect(inp);
    406 #endif
    407 #ifdef INET6
    408 			if (in6p)
    409 				in6_pcbdisconnect(in6p);
    410 #endif
    411 			error = ENOBUFS;
    412 			break;
    413 		}
    414 		/* Compute window scaling to request.  */
    415 		while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
    416 		    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
    417 			tp->request_r_scale++;
    418 		soisconnecting(so);
    419 		tcpstat.tcps_connattempt++;
    420 		tp->t_state = TCPS_SYN_SENT;
    421 		TCP_TIMER_ARM(tp, TCPT_KEEP, TCPTV_KEEP_INIT);
    422 		tp->iss = tcp_new_iss(tp, sizeof(struct tcpcb), 0);
    423 		tcp_sendseqinit(tp);
    424 		error = tcp_output(tp);
    425 		break;
    426 
    427 	/*
    428 	 * Create a TCP connection between two sockets.
    429 	 */
    430 	case PRU_CONNECT2:
    431 		error = EOPNOTSUPP;
    432 		break;
    433 
    434 	/*
    435 	 * Initiate disconnect from peer.
    436 	 * If connection never passed embryonic stage, just drop;
    437 	 * else if don't need to let data drain, then can just drop anyways,
    438 	 * else have to begin TCP shutdown process: mark socket disconnecting,
    439 	 * drain unread data, state switch to reflect user close, and
    440 	 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
    441 	 * when peer sends FIN and acks ours.
    442 	 *
    443 	 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
    444 	 */
    445 	case PRU_DISCONNECT:
    446 		tp = tcp_disconnect(tp);
    447 		break;
    448 
    449 	/*
    450 	 * Accept a connection.  Essentially all the work is
    451 	 * done at higher levels; just return the address
    452 	 * of the peer, storing through addr.
    453 	 */
    454 	case PRU_ACCEPT:
    455 #ifdef INET
    456 		if (inp)
    457 			in_setpeeraddr(inp, nam);
    458 #endif
    459 #ifdef INET6
    460 		if (in6p)
    461 			in6_setpeeraddr(in6p, nam);
    462 #endif
    463 		break;
    464 
    465 	/*
    466 	 * Mark the connection as being incapable of further output.
    467 	 */
    468 	case PRU_SHUTDOWN:
    469 		socantsendmore(so);
    470 		tp = tcp_usrclosed(tp);
    471 		if (tp)
    472 			error = tcp_output(tp);
    473 		break;
    474 
    475 	/*
    476 	 * After a receive, possibly send window update to peer.
    477 	 */
    478 	case PRU_RCVD:
    479 		(void) tcp_output(tp);
    480 		break;
    481 
    482 	/*
    483 	 * Do a send by putting data in output queue and updating urgent
    484 	 * marker if URG set.  Possibly send more data.
    485 	 */
    486 	case PRU_SEND:
    487 		if (control && control->m_len) {
    488 			m_freem(control);
    489 			m_freem(m);
    490 			error = EINVAL;
    491 			break;
    492 		}
    493 		sbappend(&so->so_snd, m);
    494 		error = tcp_output(tp);
    495 		break;
    496 
    497 	/*
    498 	 * Abort the TCP.
    499 	 */
    500 	case PRU_ABORT:
    501 		tp = tcp_drop(tp, ECONNABORTED);
    502 		break;
    503 
    504 	case PRU_SENSE:
    505 		/*
    506 		 * stat: don't bother with a blocksize.
    507 		 */
    508 		splx(s);
    509 		return (0);
    510 
    511 	case PRU_RCVOOB:
    512 		if (control && control->m_len) {
    513 			m_freem(control);
    514 			m_freem(m);
    515 			error = EINVAL;
    516 			break;
    517 		}
    518 		if ((so->so_oobmark == 0 &&
    519 		    (so->so_state & SS_RCVATMARK) == 0) ||
    520 		    so->so_options & SO_OOBINLINE ||
    521 		    tp->t_oobflags & TCPOOB_HADDATA) {
    522 			error = EINVAL;
    523 			break;
    524 		}
    525 		if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
    526 			error = EWOULDBLOCK;
    527 			break;
    528 		}
    529 		m->m_len = 1;
    530 		*mtod(m, caddr_t) = tp->t_iobc;
    531 		if (((long)nam & MSG_PEEK) == 0)
    532 			tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
    533 		break;
    534 
    535 	case PRU_SENDOOB:
    536 		if (sbspace(&so->so_snd) < -512) {
    537 			m_freem(m);
    538 			error = ENOBUFS;
    539 			break;
    540 		}
    541 		/*
    542 		 * According to RFC961 (Assigned Protocols),
    543 		 * the urgent pointer points to the last octet
    544 		 * of urgent data.  We continue, however,
    545 		 * to consider it to indicate the first octet
    546 		 * of data past the urgent section.
    547 		 * Otherwise, snd_up should be one lower.
    548 		 */
    549 		sbappend(&so->so_snd, m);
    550 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
    551 		tp->t_force = 1;
    552 		error = tcp_output(tp);
    553 		tp->t_force = 0;
    554 		break;
    555 
    556 	case PRU_SOCKADDR:
    557 #ifdef INET
    558 		if (inp)
    559 			in_setsockaddr(inp, nam);
    560 #endif
    561 #ifdef INET6
    562 		if (in6p)
    563 			in6_setsockaddr(in6p, nam);
    564 #endif
    565 		break;
    566 
    567 	case PRU_PEERADDR:
    568 #ifdef INET
    569 		if (inp)
    570 			in_setpeeraddr(inp, nam);
    571 #endif
    572 #ifdef INET6
    573 		if (in6p)
    574 			in6_setpeeraddr(in6p, nam);
    575 #endif
    576 		break;
    577 
    578 	/*
    579 	 * TCP slow timer went off; going through this
    580 	 * routine for tracing's sake.
    581 	 */
    582 	case PRU_SLOWTIMO:
    583 		tp = tcp_timers(tp, (long)nam);
    584 		req |= (long)nam << 8;		/* for debug's sake */
    585 		break;
    586 
    587 	default:
    588 		panic("tcp_usrreq");
    589 	}
    590 	if (tp && (so->so_options & SO_DEBUG))
    591 		tcp_trace(TA_USER, ostate, tp, NULL, req);
    592 
    593 release:
    594 	splx(s);
    595 	return (error);
    596 }
    597 
    598 int
    599 tcp_ctloutput(op, so, level, optname, mp)
    600 	int op;
    601 	struct socket *so;
    602 	int level, optname;
    603 	struct mbuf **mp;
    604 {
    605 	int error = 0, s;
    606 	struct inpcb *inp;
    607 #ifdef INET6
    608 	struct in6pcb *in6p;
    609 #endif
    610 	struct tcpcb *tp;
    611 	struct mbuf *m;
    612 	int i;
    613 	int family;	/* family of the socket */
    614 
    615 	family = so->so_proto->pr_domain->dom_family;
    616 
    617 	s = splsoftnet();
    618 	switch (family) {
    619 #ifdef INET
    620 	case PF_INET:
    621 		inp = sotoinpcb(so);
    622 #ifdef INET6
    623 		in6p = NULL;
    624 #endif
    625 		break;
    626 #endif
    627 #ifdef INET6
    628 	case PF_INET6:
    629 		inp = NULL;
    630 		in6p = sotoin6pcb(so);
    631 		break;
    632 #endif
    633 	default:
    634 		splx(s);
    635 		return EAFNOSUPPORT;
    636 	}
    637 #ifndef INET6
    638 	if (inp == NULL)
    639 #else
    640 	if (inp == NULL && in6p == NULL)
    641 #endif
    642 	{
    643 		splx(s);
    644 		if (op == PRCO_SETOPT && *mp)
    645 			(void) m_free(*mp);
    646 		return (ECONNRESET);
    647 	}
    648 	if (level != IPPROTO_TCP) {
    649 		switch (family) {
    650 #ifdef INET
    651 		case PF_INET:
    652 			error = ip_ctloutput(op, so, level, optname, mp);
    653 			break;
    654 #endif
    655 #ifdef INET6
    656 		case PF_INET6:
    657 			error = ip6_ctloutput(op, so, level, optname, mp);
    658 			break;
    659 #endif
    660 		}
    661 		splx(s);
    662 		return (error);
    663 	}
    664 	if (inp)
    665 		tp = intotcpcb(inp);
    666 #ifdef INET6
    667 	else if (in6p)
    668 		tp = in6totcpcb(in6p);
    669 #endif
    670 	else
    671 		tp = NULL;
    672 
    673 	switch (op) {
    674 
    675 	case PRCO_SETOPT:
    676 		m = *mp;
    677 		switch (optname) {
    678 
    679 		case TCP_NODELAY:
    680 			if (m == NULL || m->m_len < sizeof (int))
    681 				error = EINVAL;
    682 			else if (*mtod(m, int *))
    683 				tp->t_flags |= TF_NODELAY;
    684 			else
    685 				tp->t_flags &= ~TF_NODELAY;
    686 			break;
    687 
    688 		case TCP_MAXSEG:
    689 			if (m && (i = *mtod(m, int *)) > 0 &&
    690 			    i <= tp->t_peermss)
    691 				tp->t_peermss = i;  /* limit on send size */
    692 			else
    693 				error = EINVAL;
    694 			break;
    695 
    696 		default:
    697 			error = ENOPROTOOPT;
    698 			break;
    699 		}
    700 		if (m)
    701 			(void) m_free(m);
    702 		break;
    703 
    704 	case PRCO_GETOPT:
    705 		*mp = m = m_get(M_WAIT, MT_SOOPTS);
    706 		m->m_len = sizeof(int);
    707 
    708 		switch (optname) {
    709 		case TCP_NODELAY:
    710 			*mtod(m, int *) = tp->t_flags & TF_NODELAY;
    711 			break;
    712 		case TCP_MAXSEG:
    713 			*mtod(m, int *) = tp->t_peermss;
    714 			break;
    715 		default:
    716 			error = ENOPROTOOPT;
    717 			break;
    718 		}
    719 		break;
    720 	}
    721 	splx(s);
    722 	return (error);
    723 }
    724 
    725 #ifndef TCP_SENDSPACE
    726 #define	TCP_SENDSPACE	1024*16;
    727 #endif
    728 int	tcp_sendspace = TCP_SENDSPACE;
    729 #ifndef TCP_RECVSPACE
    730 #define	TCP_RECVSPACE	1024*16;
    731 #endif
    732 int	tcp_recvspace = TCP_RECVSPACE;
    733 
    734 /*
    735  * Attach TCP protocol to socket, allocating
    736  * internet protocol control block, tcp control block,
    737  * bufer space, and entering LISTEN state if to accept connections.
    738  */
    739 int
    740 tcp_attach(so)
    741 	struct socket *so;
    742 {
    743 	struct tcpcb *tp;
    744 	struct inpcb *inp;
    745 #ifdef INET6
    746 	struct in6pcb *in6p;
    747 #endif
    748 	int error;
    749 	int family;	/* family of the socket */
    750 
    751 	family = so->so_proto->pr_domain->dom_family;
    752 
    753 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    754 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
    755 		if (error)
    756 			return (error);
    757 	}
    758 	switch (family) {
    759 #ifdef INET
    760 	case PF_INET:
    761 		error = in_pcballoc(so, &tcbtable);
    762 		if (error)
    763 			return (error);
    764 		inp = sotoinpcb(so);
    765 #ifdef INET6
    766 		in6p = NULL;
    767 #endif
    768 		break;
    769 #endif
    770 #ifdef INET6
    771 	case PF_INET6:
    772 		error = in6_pcballoc(so, &tcb6);
    773 		if (error)
    774 			return (error);
    775 		inp = NULL;
    776 		in6p = sotoin6pcb(so);
    777 		break;
    778 #endif
    779 	default:
    780 		return EAFNOSUPPORT;
    781 	}
    782 #ifdef IPSEC
    783 #ifdef INET
    784 	if (inp) {
    785 		error = ipsec_init_policy(so, &inp->inp_sp);
    786 		if (error != 0) {
    787 			in_pcbdetach(inp);
    788 			return (error);
    789 		}
    790 	}
    791 #endif
    792 #ifdef INET6
    793 	if (in6p) {
    794 		error = ipsec_init_policy(so, &in6p->in6p_sp);
    795 		if (error != 0) {
    796 			in6_pcbdetach(in6p);
    797 			return (error);
    798 		}
    799 	}
    800 #endif
    801 #endif /*IPSEC*/
    802 	if (inp)
    803 		tp = tcp_newtcpcb(family, (void *)inp);
    804 #ifdef INET6
    805 	else if (in6p)
    806 		tp = tcp_newtcpcb(family, (void *)in6p);
    807 #endif
    808 	else
    809 		tp = NULL;
    810 
    811 	if (tp == 0) {
    812 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
    813 
    814 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
    815 #ifdef INET
    816 		if (inp)
    817 			in_pcbdetach(inp);
    818 #endif
    819 #ifdef INET6
    820 		if (in6p)
    821 			in6_pcbdetach(in6p);
    822 #endif
    823 		so->so_state |= nofd;
    824 		return (ENOBUFS);
    825 	}
    826 	tp->t_state = TCPS_CLOSED;
    827 	return (0);
    828 }
    829 
    830 /*
    831  * Initiate (or continue) disconnect.
    832  * If embryonic state, just send reset (once).
    833  * If in ``let data drain'' option and linger null, just drop.
    834  * Otherwise (hard), mark socket disconnecting and drop
    835  * current input data; switch states based on user close, and
    836  * send segment to peer (with FIN).
    837  */
    838 struct tcpcb *
    839 tcp_disconnect(tp)
    840 	struct tcpcb *tp;
    841 {
    842 	struct socket *so;
    843 
    844 	if (tp->t_inpcb)
    845 		so = tp->t_inpcb->inp_socket;
    846 #ifdef INET6
    847 	else if (tp->t_in6pcb)
    848 		so = tp->t_in6pcb->in6p_socket;
    849 #endif
    850 	else
    851 		so = NULL;
    852 
    853 	if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
    854 		tp = tcp_close(tp);
    855 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
    856 		tp = tcp_drop(tp, 0);
    857 	else {
    858 		soisdisconnecting(so);
    859 		sbflush(&so->so_rcv);
    860 		tp = tcp_usrclosed(tp);
    861 		if (tp)
    862 			(void) tcp_output(tp);
    863 	}
    864 	return (tp);
    865 }
    866 
    867 /*
    868  * User issued close, and wish to trail through shutdown states:
    869  * if never received SYN, just forget it.  If got a SYN from peer,
    870  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
    871  * If already got a FIN from peer, then almost done; go to LAST_ACK
    872  * state.  In all other cases, have already sent FIN to peer (e.g.
    873  * after PRU_SHUTDOWN), and just have to play tedious game waiting
    874  * for peer to send FIN or not respond to keep-alives, etc.
    875  * We can let the user exit from the close as soon as the FIN is acked.
    876  */
    877 struct tcpcb *
    878 tcp_usrclosed(tp)
    879 	struct tcpcb *tp;
    880 {
    881 
    882 	switch (tp->t_state) {
    883 
    884 	case TCPS_CLOSED:
    885 	case TCPS_LISTEN:
    886 	case TCPS_SYN_SENT:
    887 		tp->t_state = TCPS_CLOSED;
    888 		tp = tcp_close(tp);
    889 		break;
    890 
    891 	case TCPS_SYN_RECEIVED:
    892 	case TCPS_ESTABLISHED:
    893 		tp->t_state = TCPS_FIN_WAIT_1;
    894 		break;
    895 
    896 	case TCPS_CLOSE_WAIT:
    897 		tp->t_state = TCPS_LAST_ACK;
    898 		break;
    899 	}
    900 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
    901 		struct socket *so;
    902 		if (tp->t_inpcb)
    903 			so = tp->t_inpcb->inp_socket;
    904 #ifdef INET6
    905 		else if (tp->t_in6pcb)
    906 			so = tp->t_in6pcb->in6p_socket;
    907 #endif
    908 		else
    909 			so = NULL;
    910 		soisdisconnected(so);
    911 		/*
    912 		 * If we are in FIN_WAIT_2, we arrived here because the
    913 		 * application did a shutdown of the send side.  Like the
    914 		 * case of a transition from FIN_WAIT_1 to FIN_WAIT_2 after
    915 		 * a full close, we start a timer to make sure sockets are
    916 		 * not left in FIN_WAIT_2 forever.
    917 		 */
    918 		if ((tp->t_state == TCPS_FIN_WAIT_2) && (tcp_maxidle > 0))
    919 			TCP_TIMER_ARM(tp, TCPT_2MSL, tcp_maxidle);
    920 	}
    921 	return (tp);
    922 }
    923 
    924 static struct {
    925 	 unsigned int valid : 1;
    926 	 unsigned int rdonly : 1;
    927 	 int *var;
    928 	 int val;
    929 	 } tcp_ctlvars[] = TCPCTL_VARIABLES;
    930 
    931 /*
    932  * Sysctl for tcp variables.
    933  */
    934 int
    935 tcp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
    936 	int *name;
    937 	u_int namelen;
    938 	void *oldp;
    939 	size_t *oldlenp;
    940 	void *newp;
    941 	size_t newlen;
    942 {
    943 
    944 	/* All sysctl names at this level are terminal. */
    945 	if (namelen != 1)
    946 		return (ENOTDIR);
    947 
    948 	if (name[0] < sizeof(tcp_ctlvars)/sizeof(tcp_ctlvars[0])
    949 	    && tcp_ctlvars[name[0]].valid) {
    950 		if (tcp_ctlvars[name[0]].rdonly)
    951 			return (sysctl_rdint(oldp, oldlenp, newp,
    952 			    tcp_ctlvars[name[0]].val));
    953 		else
    954 			return (sysctl_int(oldp, oldlenp, newp, newlen,
    955 			    tcp_ctlvars[name[0]].var));
    956 	}
    957 
    958 	return (ENOPROTOOPT);
    959 }
    960