Home | History | Annotate | Line # | Download | only in netinet
tcp_usrreq.c revision 1.182
      1 /*	$NetBSD: tcp_usrreq.c,v 1.182 2014/07/07 07:09:58 rtr 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, 2005, 2006 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  * This code is derived from software contributed to The NetBSD Foundation
     40  * by Charles M. Hannum.
     41  * This code is derived from software contributed to The NetBSD Foundation
     42  * by Rui Paulo.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     54  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     55  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     56  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     57  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     58  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     59  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     60  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     61  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     62  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     63  * POSSIBILITY OF SUCH DAMAGE.
     64  */
     65 
     66 /*
     67  * Copyright (c) 1982, 1986, 1988, 1993, 1995
     68  *	The Regents of the University of California.  All rights reserved.
     69  *
     70  * Redistribution and use in source and binary forms, with or without
     71  * modification, are permitted provided that the following conditions
     72  * are met:
     73  * 1. Redistributions of source code must retain the above copyright
     74  *    notice, this list of conditions and the following disclaimer.
     75  * 2. Redistributions in binary form must reproduce the above copyright
     76  *    notice, this list of conditions and the following disclaimer in the
     77  *    documentation and/or other materials provided with the distribution.
     78  * 3. Neither the name of the University nor the names of its contributors
     79  *    may be used to endorse or promote products derived from this software
     80  *    without specific prior written permission.
     81  *
     82  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     83  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     84  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     85  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     86  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     87  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     88  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     89  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     90  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     91  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     92  * SUCH DAMAGE.
     93  *
     94  *	@(#)tcp_usrreq.c	8.5 (Berkeley) 6/21/95
     95  */
     96 
     97 /*
     98  * TCP protocol interface to socket abstraction.
     99  */
    100 
    101 #include <sys/cdefs.h>
    102 __KERNEL_RCSID(0, "$NetBSD: tcp_usrreq.c,v 1.182 2014/07/07 07:09:58 rtr Exp $");
    103 
    104 #include "opt_inet.h"
    105 #include "opt_ipsec.h"
    106 #include "opt_tcp_debug.h"
    107 #include "opt_mbuftrace.h"
    108 
    109 #include <sys/param.h>
    110 #include <sys/systm.h>
    111 #include <sys/kernel.h>
    112 #include <sys/malloc.h>
    113 #include <sys/mbuf.h>
    114 #include <sys/socket.h>
    115 #include <sys/socketvar.h>
    116 #include <sys/protosw.h>
    117 #include <sys/errno.h>
    118 #include <sys/stat.h>
    119 #include <sys/proc.h>
    120 #include <sys/domain.h>
    121 #include <sys/sysctl.h>
    122 #include <sys/kauth.h>
    123 #include <sys/uidinfo.h>
    124 
    125 #include <net/if.h>
    126 #include <net/route.h>
    127 
    128 #include <netinet/in.h>
    129 #include <netinet/in_systm.h>
    130 #include <netinet/in_var.h>
    131 #include <netinet/ip.h>
    132 #include <netinet/in_pcb.h>
    133 #include <netinet/ip_var.h>
    134 #include <netinet/in_offload.h>
    135 
    136 #ifdef INET6
    137 #ifndef INET
    138 #include <netinet/in.h>
    139 #endif
    140 #include <netinet/ip6.h>
    141 #include <netinet6/in6_pcb.h>
    142 #include <netinet6/ip6_var.h>
    143 #include <netinet6/scope6_var.h>
    144 #endif
    145 
    146 #include <netinet/tcp.h>
    147 #include <netinet/tcp_fsm.h>
    148 #include <netinet/tcp_seq.h>
    149 #include <netinet/tcp_timer.h>
    150 #include <netinet/tcp_var.h>
    151 #include <netinet/tcp_private.h>
    152 #include <netinet/tcp_congctl.h>
    153 #include <netinet/tcpip.h>
    154 #include <netinet/tcp_debug.h>
    155 #include <netinet/tcp_vtw.h>
    156 
    157 #include "opt_tcp_space.h"
    158 
    159 /*
    160  * Process a TCP user request for TCP tb.  If this is a send request
    161  * then m is the mbuf chain of send data.  If this is a timer expiration
    162  * (called from the software clock routine), then timertype tells which timer.
    163  */
    164 static int
    165 tcp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
    166     struct mbuf *control, struct lwp *l)
    167 {
    168 	struct inpcb *inp;
    169 #ifdef INET6
    170 	struct in6pcb *in6p;
    171 #endif
    172 	struct tcpcb *tp = NULL;
    173 	int s;
    174 	int error = 0;
    175 #ifdef TCP_DEBUG
    176 	int ostate = 0;
    177 #endif
    178 	int family;	/* family of the socket */
    179 
    180 	KASSERT(req != PRU_ATTACH);
    181 	KASSERT(req != PRU_DETACH);
    182 	KASSERT(req != PRU_CONTROL);
    183 	KASSERT(req != PRU_SENSE);
    184 
    185 	family = so->so_proto->pr_domain->dom_family;
    186 
    187 	s = splsoftnet();
    188 
    189 	if (req == PRU_PURGEIF) {
    190 		mutex_enter(softnet_lock);
    191 		switch (family) {
    192 #ifdef INET
    193 		case PF_INET:
    194 			in_pcbpurgeif0(&tcbtable, (struct ifnet *)control);
    195 			in_purgeif((struct ifnet *)control);
    196 			in_pcbpurgeif(&tcbtable, (struct ifnet *)control);
    197 			break;
    198 #endif
    199 #ifdef INET6
    200 		case PF_INET6:
    201 			in6_pcbpurgeif0(&tcbtable, (struct ifnet *)control);
    202 			in6_purgeif((struct ifnet *)control);
    203 			in6_pcbpurgeif(&tcbtable, (struct ifnet *)control);
    204 			break;
    205 #endif
    206 		default:
    207 			mutex_exit(softnet_lock);
    208 			splx(s);
    209 			return (EAFNOSUPPORT);
    210 		}
    211 		mutex_exit(softnet_lock);
    212 		splx(s);
    213 		return (0);
    214 	}
    215 
    216 	KASSERT(solocked(so));
    217 
    218 	switch (family) {
    219 #ifdef INET
    220 	case PF_INET:
    221 		inp = sotoinpcb(so);
    222 #ifdef INET6
    223 		in6p = NULL;
    224 #endif
    225 		break;
    226 #endif
    227 #ifdef INET6
    228 	case PF_INET6:
    229 		inp = NULL;
    230 		in6p = sotoin6pcb(so);
    231 		break;
    232 #endif
    233 	default:
    234 		splx(s);
    235 		return EAFNOSUPPORT;
    236 	}
    237 	KASSERT(!control || (req == PRU_SEND || req == PRU_SENDOOB));
    238 #ifdef INET6
    239 	/* XXX: KASSERT((inp != NULL) ^ (in6p != NULL)); */
    240 #endif
    241 	/*
    242 	 * When a TCP is attached to a socket, then there will be
    243 	 * a (struct inpcb) pointed at by the socket, and this
    244 	 * structure will point at a subsidary (struct tcpcb).
    245 	 */
    246 	if (inp == NULL
    247 #ifdef INET6
    248 	    && in6p == NULL
    249 #endif
    250 	    )
    251 	{
    252 		error = EINVAL;
    253 		goto release;
    254 	}
    255 #ifdef INET
    256 	if (inp) {
    257 		tp = intotcpcb(inp);
    258 		/* WHAT IF TP IS 0? */
    259 #ifdef KPROF
    260 		tcp_acounts[tp->t_state][req]++;
    261 #endif
    262 #ifdef TCP_DEBUG
    263 		ostate = tp->t_state;
    264 #endif
    265 	}
    266 #endif
    267 #ifdef INET6
    268 	if (in6p) {
    269 		tp = in6totcpcb(in6p);
    270 		/* WHAT IF TP IS 0? */
    271 #ifdef KPROF
    272 		tcp_acounts[tp->t_state][req]++;
    273 #endif
    274 #ifdef TCP_DEBUG
    275 		ostate = tp->t_state;
    276 #endif
    277 	}
    278 #endif
    279 
    280 	switch (req) {
    281 
    282 	/*
    283 	 * Give the socket an address.
    284 	 */
    285 	case PRU_BIND:
    286 		switch (family) {
    287 #ifdef INET
    288 		case PF_INET:
    289 			error = in_pcbbind(inp, nam, l);
    290 			break;
    291 #endif
    292 #ifdef INET6
    293 		case PF_INET6:
    294 			error = in6_pcbbind(in6p, nam, l);
    295 			if (!error) {
    296 				/* mapped addr case */
    297 				if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
    298 					tp->t_family = AF_INET;
    299 				else
    300 					tp->t_family = AF_INET6;
    301 			}
    302 			break;
    303 #endif
    304 		}
    305 		break;
    306 
    307 	/*
    308 	 * Prepare to accept connections.
    309 	 */
    310 	case PRU_LISTEN:
    311 #ifdef INET
    312 		if (inp && inp->inp_lport == 0) {
    313 			error = in_pcbbind(inp, NULL, l);
    314 			if (error)
    315 				break;
    316 		}
    317 #endif
    318 #ifdef INET6
    319 		if (in6p && in6p->in6p_lport == 0) {
    320 			error = in6_pcbbind(in6p, NULL, l);
    321 			if (error)
    322 				break;
    323 		}
    324 #endif
    325 		tp->t_state = TCPS_LISTEN;
    326 		break;
    327 
    328 	/*
    329 	 * Initiate connection to peer.
    330 	 * Create a template for use in transmissions on this connection.
    331 	 * Enter SYN_SENT state, and mark socket as connecting.
    332 	 * Start keep-alive timer, and seed output sequence space.
    333 	 * Send initial segment on connection.
    334 	 */
    335 	case PRU_CONNECT:
    336 #ifdef INET
    337 		if (inp) {
    338 			if (inp->inp_lport == 0) {
    339 				error = in_pcbbind(inp, NULL, l);
    340 				if (error)
    341 					break;
    342 			}
    343 			error = in_pcbconnect(inp, nam, l);
    344 		}
    345 #endif
    346 #ifdef INET6
    347 		if (in6p) {
    348 			if (in6p->in6p_lport == 0) {
    349 				error = in6_pcbbind(in6p, NULL, l);
    350 				if (error)
    351 					break;
    352 			}
    353 			error = in6_pcbconnect(in6p, nam, l);
    354 			if (!error) {
    355 				/* mapped addr case */
    356 				if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr))
    357 					tp->t_family = AF_INET;
    358 				else
    359 					tp->t_family = AF_INET6;
    360 			}
    361 		}
    362 #endif
    363 		if (error)
    364 			break;
    365 		tp->t_template = tcp_template(tp);
    366 		if (tp->t_template == 0) {
    367 #ifdef INET
    368 			if (inp)
    369 				in_pcbdisconnect(inp);
    370 #endif
    371 #ifdef INET6
    372 			if (in6p)
    373 				in6_pcbdisconnect(in6p);
    374 #endif
    375 			error = ENOBUFS;
    376 			break;
    377 		}
    378 		/*
    379 		 * Compute window scaling to request.
    380 		 * XXX: This should be moved to tcp_output().
    381 		 */
    382 		while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
    383 		    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
    384 			tp->request_r_scale++;
    385 		soisconnecting(so);
    386 		TCP_STATINC(TCP_STAT_CONNATTEMPT);
    387 		tp->t_state = TCPS_SYN_SENT;
    388 		TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepinit);
    389 		tp->iss = tcp_new_iss(tp, 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 #ifdef INET
    423 		if (inp)
    424 			in_setpeeraddr(inp, nam);
    425 #endif
    426 #ifdef INET6
    427 		if (in6p)
    428 			in6_setpeeraddr(in6p, nam);
    429 #endif
    430 		break;
    431 
    432 	/*
    433 	 * Mark the connection as being incapable of further output.
    434 	 */
    435 	case PRU_SHUTDOWN:
    436 		socantsendmore(so);
    437 		tp = tcp_usrclosed(tp);
    438 		if (tp)
    439 			error = tcp_output(tp);
    440 		break;
    441 
    442 	/*
    443 	 * After a receive, possibly send window update to peer.
    444 	 */
    445 	case PRU_RCVD:
    446 		/*
    447 		 * soreceive() calls this function when a user receives
    448 		 * ancillary data on a listening socket. We don't call
    449 		 * tcp_output in such a case, since there is no header
    450 		 * template for a listening socket and hence the kernel
    451 		 * will panic.
    452 		 */
    453 		if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) != 0)
    454 			(void) tcp_output(tp);
    455 		break;
    456 
    457 	/*
    458 	 * Do a send by putting data in output queue and updating urgent
    459 	 * marker if URG set.  Possibly send more data.
    460 	 */
    461 	case PRU_SEND:
    462 		if (control && control->m_len) {
    463 			m_freem(control);
    464 			m_freem(m);
    465 			error = EINVAL;
    466 			break;
    467 		}
    468 		sbappendstream(&so->so_snd, m);
    469 		error = tcp_output(tp);
    470 		break;
    471 
    472 	/*
    473 	 * Abort the TCP.
    474 	 */
    475 	case PRU_ABORT:
    476 		tp = tcp_drop(tp, ECONNABORTED);
    477 		break;
    478 
    479 	case PRU_RCVOOB:
    480 		if (control && control->m_len) {
    481 			m_freem(control);
    482 			m_freem(m);
    483 			error = EINVAL;
    484 			break;
    485 		}
    486 		if ((so->so_oobmark == 0 &&
    487 		    (so->so_state & SS_RCVATMARK) == 0) ||
    488 		    so->so_options & SO_OOBINLINE ||
    489 		    tp->t_oobflags & TCPOOB_HADDATA) {
    490 			error = EINVAL;
    491 			break;
    492 		}
    493 		if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
    494 			error = EWOULDBLOCK;
    495 			break;
    496 		}
    497 		m->m_len = 1;
    498 		*mtod(m, char *) = tp->t_iobc;
    499 		if (((long)nam & MSG_PEEK) == 0)
    500 			tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
    501 		break;
    502 
    503 	case PRU_SENDOOB:
    504 		if (sbspace(&so->so_snd) < -512) {
    505 			m_freem(m);
    506 			error = ENOBUFS;
    507 			break;
    508 		}
    509 		/*
    510 		 * According to RFC961 (Assigned Protocols),
    511 		 * the urgent pointer points to the last octet
    512 		 * of urgent data.  We continue, however,
    513 		 * to consider it to indicate the first octet
    514 		 * of data past the urgent section.
    515 		 * Otherwise, snd_up should be one lower.
    516 		 */
    517 		sbappendstream(&so->so_snd, m);
    518 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
    519 		tp->t_force = 1;
    520 		error = tcp_output(tp);
    521 		tp->t_force = 0;
    522 		break;
    523 
    524 	case PRU_SOCKADDR:
    525 #ifdef INET
    526 		if (inp)
    527 			in_setsockaddr(inp, nam);
    528 #endif
    529 #ifdef INET6
    530 		if (in6p)
    531 			in6_setsockaddr(in6p, nam);
    532 #endif
    533 		break;
    534 
    535 	case PRU_PEERADDR:
    536 #ifdef INET
    537 		if (inp)
    538 			in_setpeeraddr(inp, nam);
    539 #endif
    540 #ifdef INET6
    541 		if (in6p)
    542 			in6_setpeeraddr(in6p, nam);
    543 #endif
    544 		break;
    545 
    546 	default:
    547 		panic("tcp_usrreq");
    548 	}
    549 #ifdef TCP_DEBUG
    550 	if (tp && (so->so_options & SO_DEBUG))
    551 		tcp_trace(TA_USER, ostate, tp, NULL, req);
    552 #endif
    553 
    554 release:
    555 	splx(s);
    556 	return (error);
    557 }
    558 
    559 static void
    560 change_keepalive(struct socket *so, struct tcpcb *tp)
    561 {
    562 	tp->t_maxidle = tp->t_keepcnt * tp->t_keepintvl;
    563 	TCP_TIMER_DISARM(tp, TCPT_KEEP);
    564 	TCP_TIMER_DISARM(tp, TCPT_2MSL);
    565 
    566 	if (tp->t_state == TCPS_SYN_RECEIVED ||
    567 	    tp->t_state == TCPS_SYN_SENT) {
    568 		TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepinit);
    569 	} else if (so->so_options & SO_KEEPALIVE &&
    570 	    tp->t_state <= TCPS_CLOSE_WAIT) {
    571 		TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepintvl);
    572 	} else {
    573 		TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepidle);
    574 	}
    575 
    576 	if ((tp->t_state == TCPS_FIN_WAIT_2) && (tp->t_maxidle > 0))
    577 		TCP_TIMER_ARM(tp, TCPT_2MSL, tp->t_maxidle);
    578 }
    579 
    580 
    581 int
    582 tcp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
    583 {
    584 	int error = 0, s;
    585 	struct inpcb *inp;
    586 #ifdef INET6
    587 	struct in6pcb *in6p;
    588 #endif
    589 	struct tcpcb *tp;
    590 	u_int ui;
    591 	int family;	/* family of the socket */
    592 	int level, optname, optval;
    593 
    594 	level = sopt->sopt_level;
    595 	optname = sopt->sopt_name;
    596 
    597 	family = so->so_proto->pr_domain->dom_family;
    598 
    599 	s = splsoftnet();
    600 	switch (family) {
    601 #ifdef INET
    602 	case PF_INET:
    603 		inp = sotoinpcb(so);
    604 #ifdef INET6
    605 		in6p = NULL;
    606 #endif
    607 		break;
    608 #endif
    609 #ifdef INET6
    610 	case PF_INET6:
    611 		inp = NULL;
    612 		in6p = sotoin6pcb(so);
    613 		break;
    614 #endif
    615 	default:
    616 		splx(s);
    617 		panic("%s: af %d", __func__, family);
    618 	}
    619 #ifndef INET6
    620 	if (inp == NULL)
    621 #else
    622 	if (inp == NULL && in6p == NULL)
    623 #endif
    624 	{
    625 		splx(s);
    626 		return (ECONNRESET);
    627 	}
    628 	if (level != IPPROTO_TCP) {
    629 		switch (family) {
    630 #ifdef INET
    631 		case PF_INET:
    632 			error = ip_ctloutput(op, so, sopt);
    633 			break;
    634 #endif
    635 #ifdef INET6
    636 		case PF_INET6:
    637 			error = ip6_ctloutput(op, so, sopt);
    638 			break;
    639 #endif
    640 		}
    641 		splx(s);
    642 		return (error);
    643 	}
    644 	if (inp)
    645 		tp = intotcpcb(inp);
    646 #ifdef INET6
    647 	else if (in6p)
    648 		tp = in6totcpcb(in6p);
    649 #endif
    650 	else
    651 		tp = NULL;
    652 
    653 	switch (op) {
    654 	case PRCO_SETOPT:
    655 		switch (optname) {
    656 #ifdef TCP_SIGNATURE
    657 		case TCP_MD5SIG:
    658 			error = sockopt_getint(sopt, &optval);
    659 			if (error)
    660 				break;
    661 			if (optval > 0)
    662 				tp->t_flags |= TF_SIGNATURE;
    663 			else
    664 				tp->t_flags &= ~TF_SIGNATURE;
    665 			break;
    666 #endif /* TCP_SIGNATURE */
    667 
    668 		case TCP_NODELAY:
    669 			error = sockopt_getint(sopt, &optval);
    670 			if (error)
    671 				break;
    672 			if (optval)
    673 				tp->t_flags |= TF_NODELAY;
    674 			else
    675 				tp->t_flags &= ~TF_NODELAY;
    676 			break;
    677 
    678 		case TCP_MAXSEG:
    679 			error = sockopt_getint(sopt, &optval);
    680 			if (error)
    681 				break;
    682 			if (optval > 0 && optval <= tp->t_peermss)
    683 				tp->t_peermss = optval; /* limit on send size */
    684 			else
    685 				error = EINVAL;
    686 			break;
    687 #ifdef notyet
    688 		case TCP_CONGCTL:
    689 			/* XXX string overflow XXX */
    690 			error = tcp_congctl_select(tp, sopt->sopt_data);
    691 			break;
    692 #endif
    693 
    694 		case TCP_KEEPIDLE:
    695 			error = sockopt_get(sopt, &ui, sizeof(ui));
    696 			if (error)
    697 				break;
    698 			if (ui > 0) {
    699 				tp->t_keepidle = ui;
    700 				change_keepalive(so, tp);
    701 			} else
    702 				error = EINVAL;
    703 			break;
    704 
    705 		case TCP_KEEPINTVL:
    706 			error = sockopt_get(sopt, &ui, sizeof(ui));
    707 			if (error)
    708 				break;
    709 			if (ui > 0) {
    710 				tp->t_keepintvl = ui;
    711 				change_keepalive(so, tp);
    712 			} else
    713 				error = EINVAL;
    714 			break;
    715 
    716 		case TCP_KEEPCNT:
    717 			error = sockopt_get(sopt, &ui, sizeof(ui));
    718 			if (error)
    719 				break;
    720 			if (ui > 0) {
    721 				tp->t_keepcnt = ui;
    722 				change_keepalive(so, tp);
    723 			} else
    724 				error = EINVAL;
    725 			break;
    726 
    727 		case TCP_KEEPINIT:
    728 			error = sockopt_get(sopt, &ui, sizeof(ui));
    729 			if (error)
    730 				break;
    731 			if (ui > 0) {
    732 				tp->t_keepinit = ui;
    733 				change_keepalive(so, tp);
    734 			} else
    735 				error = EINVAL;
    736 			break;
    737 
    738 		default:
    739 			error = ENOPROTOOPT;
    740 			break;
    741 		}
    742 		break;
    743 
    744 	case PRCO_GETOPT:
    745 		switch (optname) {
    746 #ifdef TCP_SIGNATURE
    747 		case TCP_MD5SIG:
    748 			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
    749 			error = sockopt_set(sopt, &optval, sizeof(optval));
    750 			break;
    751 #endif
    752 		case TCP_NODELAY:
    753 			optval = tp->t_flags & TF_NODELAY;
    754 			error = sockopt_set(sopt, &optval, sizeof(optval));
    755 			break;
    756 		case TCP_MAXSEG:
    757 			optval = tp->t_peermss;
    758 			error = sockopt_set(sopt, &optval, sizeof(optval));
    759 			break;
    760 #ifdef notyet
    761 		case TCP_CONGCTL:
    762 			break;
    763 #endif
    764 		default:
    765 			error = ENOPROTOOPT;
    766 			break;
    767 		}
    768 		break;
    769 	}
    770 	splx(s);
    771 	return (error);
    772 }
    773 
    774 #ifndef TCP_SENDSPACE
    775 #define	TCP_SENDSPACE	1024*32
    776 #endif
    777 int	tcp_sendspace = TCP_SENDSPACE;
    778 #ifndef TCP_RECVSPACE
    779 #define	TCP_RECVSPACE	1024*32
    780 #endif
    781 int	tcp_recvspace = TCP_RECVSPACE;
    782 
    783 /*
    784  * tcp_attach: attach TCP protocol to socket, allocating internet protocol
    785  * control block, TCP control block, buffer space and entering LISTEN state
    786  * if to accept connections.
    787  */
    788 static int
    789 tcp_attach(struct socket *so, int proto)
    790 {
    791 	struct tcpcb *tp;
    792 	struct inpcb *inp;
    793 #ifdef INET6
    794 	struct in6pcb *in6p;
    795 #endif
    796 	int s, error, family;
    797 
    798 	/* Assign the lock (must happen even if we will error out). */
    799 	s = splsoftnet();
    800 	sosetlock(so);
    801 	KASSERT(solocked(so));
    802 
    803 	family = so->so_proto->pr_domain->dom_family;
    804 	switch (family) {
    805 #ifdef INET
    806 	case PF_INET:
    807 		inp = sotoinpcb(so);
    808 #ifdef INET6
    809 		in6p = NULL;
    810 #endif
    811 		break;
    812 #endif
    813 #ifdef INET6
    814 	case PF_INET6:
    815 		inp = NULL;
    816 		in6p = sotoin6pcb(so);
    817 		break;
    818 #endif
    819 	default:
    820 		error = EAFNOSUPPORT;
    821 		goto out;
    822 	}
    823 
    824 	KASSERT(inp == NULL);
    825 #ifdef INET6
    826 	KASSERT(in6p == NULL);
    827 #endif
    828 
    829 #ifdef MBUFTRACE
    830 	so->so_mowner = &tcp_sock_mowner;
    831 	so->so_rcv.sb_mowner = &tcp_sock_rx_mowner;
    832 	so->so_snd.sb_mowner = &tcp_sock_tx_mowner;
    833 #endif
    834 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    835 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
    836 		if (error)
    837 			goto out;
    838 	}
    839 
    840 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
    841 	so->so_snd.sb_flags |= SB_AUTOSIZE;
    842 
    843 	switch (family) {
    844 #ifdef INET
    845 	case PF_INET:
    846 		error = in_pcballoc(so, &tcbtable);
    847 		if (error)
    848 			goto out;
    849 		inp = sotoinpcb(so);
    850 #ifdef INET6
    851 		in6p = NULL;
    852 #endif
    853 		break;
    854 #endif
    855 #ifdef INET6
    856 	case PF_INET6:
    857 		error = in6_pcballoc(so, &tcbtable);
    858 		if (error)
    859 			goto out;
    860 		inp = NULL;
    861 		in6p = sotoin6pcb(so);
    862 		break;
    863 #endif
    864 	default:
    865 		error = EAFNOSUPPORT;
    866 		goto out;
    867 	}
    868 	if (inp)
    869 		tp = tcp_newtcpcb(family, (void *)inp);
    870 #ifdef INET6
    871 	else if (in6p)
    872 		tp = tcp_newtcpcb(family, (void *)in6p);
    873 #endif
    874 	else
    875 		tp = NULL;
    876 
    877 	if (tp == NULL) {
    878 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
    879 
    880 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
    881 #ifdef INET
    882 		if (inp)
    883 			in_pcbdetach(inp);
    884 #endif
    885 #ifdef INET6
    886 		if (in6p)
    887 			in6_pcbdetach(in6p);
    888 #endif
    889 		so->so_state |= nofd;
    890 		error = ENOBUFS;
    891 		goto out;
    892 	}
    893 	tp->t_state = TCPS_CLOSED;
    894 	if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
    895 		so->so_linger = TCP_LINGERTIME;
    896 	}
    897 out:
    898 	KASSERT(solocked(so));
    899 	splx(s);
    900 	return error;
    901 }
    902 
    903 static void
    904 tcp_detach(struct socket *so)
    905 {
    906 	struct inpcb *inp;
    907 #ifdef INET6
    908 	struct in6pcb *in6p;
    909 #endif
    910 	struct tcpcb *tp = NULL;
    911 	int s, family;
    912 
    913 	KASSERT(solocked(so));
    914 
    915 	s = splsoftnet();
    916 	family = so->so_proto->pr_domain->dom_family;
    917 	switch (family) {
    918 #ifdef INET
    919 	case PF_INET:
    920 		inp = sotoinpcb(so);
    921 		tp = intotcpcb(inp);
    922 		break;
    923 #endif
    924 #ifdef INET6
    925 	case PF_INET6:
    926 		in6p = sotoin6pcb(so);
    927 		tp = in6totcpcb(in6p);
    928 		break;
    929 #endif
    930 	default:
    931 		splx(s);
    932 		return;
    933 	}
    934 	KASSERT(tp != NULL);
    935 	(void)tcp_disconnect(tp);
    936 	splx(s);
    937 }
    938 
    939 static int
    940 tcp_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
    941 {
    942 	switch (so->so_proto->pr_domain->dom_family) {
    943 #ifdef INET
    944 	case PF_INET:
    945 		return in_control(so, cmd, nam, ifp);
    946 #endif
    947 #ifdef INET6
    948 	case PF_INET6:
    949 		return in6_control(so, cmd, nam, ifp);
    950 #endif
    951 	default:
    952 		return EAFNOSUPPORT;
    953 	}
    954 }
    955 
    956 static int
    957 tcp_stat(struct socket *so, struct stat *ub)
    958 {
    959 	return EOPNOTSUPP;
    960 }
    961 
    962 /*
    963  * Initiate (or continue) disconnect.
    964  * If embryonic state, just send reset (once).
    965  * If in ``let data drain'' option and linger null, just drop.
    966  * Otherwise (hard), mark socket disconnecting and drop
    967  * current input data; switch states based on user close, and
    968  * send segment to peer (with FIN).
    969  */
    970 struct tcpcb *
    971 tcp_disconnect(struct tcpcb *tp)
    972 {
    973 	struct socket *so;
    974 
    975 	if (tp->t_inpcb)
    976 		so = tp->t_inpcb->inp_socket;
    977 #ifdef INET6
    978 	else if (tp->t_in6pcb)
    979 		so = tp->t_in6pcb->in6p_socket;
    980 #endif
    981 	else
    982 		so = NULL;
    983 
    984 	if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
    985 		tp = tcp_close(tp);
    986 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
    987 		tp = tcp_drop(tp, 0);
    988 	else {
    989 		soisdisconnecting(so);
    990 		sbflush(&so->so_rcv);
    991 		tp = tcp_usrclosed(tp);
    992 		if (tp)
    993 			(void) tcp_output(tp);
    994 	}
    995 	return (tp);
    996 }
    997 
    998 /*
    999  * User issued close, and wish to trail through shutdown states:
   1000  * if never received SYN, just forget it.  If got a SYN from peer,
   1001  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
   1002  * If already got a FIN from peer, then almost done; go to LAST_ACK
   1003  * state.  In all other cases, have already sent FIN to peer (e.g.
   1004  * after PRU_SHUTDOWN), and just have to play tedious game waiting
   1005  * for peer to send FIN or not respond to keep-alives, etc.
   1006  * We can let the user exit from the close as soon as the FIN is acked.
   1007  */
   1008 struct tcpcb *
   1009 tcp_usrclosed(struct tcpcb *tp)
   1010 {
   1011 
   1012 	switch (tp->t_state) {
   1013 
   1014 	case TCPS_CLOSED:
   1015 	case TCPS_LISTEN:
   1016 	case TCPS_SYN_SENT:
   1017 		tp->t_state = TCPS_CLOSED;
   1018 		tp = tcp_close(tp);
   1019 		break;
   1020 
   1021 	case TCPS_SYN_RECEIVED:
   1022 	case TCPS_ESTABLISHED:
   1023 		tp->t_state = TCPS_FIN_WAIT_1;
   1024 		break;
   1025 
   1026 	case TCPS_CLOSE_WAIT:
   1027 		tp->t_state = TCPS_LAST_ACK;
   1028 		break;
   1029 	}
   1030 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
   1031 		struct socket *so;
   1032 		if (tp->t_inpcb)
   1033 			so = tp->t_inpcb->inp_socket;
   1034 #ifdef INET6
   1035 		else if (tp->t_in6pcb)
   1036 			so = tp->t_in6pcb->in6p_socket;
   1037 #endif
   1038 		else
   1039 			so = NULL;
   1040 		if (so)
   1041 			soisdisconnected(so);
   1042 		/*
   1043 		 * If we are in FIN_WAIT_2, we arrived here because the
   1044 		 * application did a shutdown of the send side.  Like the
   1045 		 * case of a transition from FIN_WAIT_1 to FIN_WAIT_2 after
   1046 		 * a full close, we start a timer to make sure sockets are
   1047 		 * not left in FIN_WAIT_2 forever.
   1048 		 */
   1049 		if ((tp->t_state == TCPS_FIN_WAIT_2) && (tp->t_maxidle > 0))
   1050 			TCP_TIMER_ARM(tp, TCPT_2MSL, tp->t_maxidle);
   1051 		else if (tp->t_state == TCPS_TIME_WAIT
   1052 			 && ((tp->t_inpcb
   1053 			      && (tcp4_vtw_enable & 1)
   1054 			      && vtw_add(AF_INET, tp))
   1055 			     ||
   1056 			     (tp->t_in6pcb
   1057 			      && (tcp6_vtw_enable & 1)
   1058 			      && vtw_add(AF_INET6, tp)))) {
   1059 			tp = 0;
   1060 		}
   1061 	}
   1062 	return (tp);
   1063 }
   1064 
   1065 /*
   1066  * sysctl helper routine for net.inet.ip.mssdflt.  it can't be less
   1067  * than 32.
   1068  */
   1069 static int
   1070 sysctl_net_inet_tcp_mssdflt(SYSCTLFN_ARGS)
   1071 {
   1072 	int error, mssdflt;
   1073 	struct sysctlnode node;
   1074 
   1075 	mssdflt = tcp_mssdflt;
   1076 	node = *rnode;
   1077 	node.sysctl_data = &mssdflt;
   1078 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1079 	if (error || newp == NULL)
   1080 		return (error);
   1081 
   1082 	if (mssdflt < 32)
   1083 		return (EINVAL);
   1084 	tcp_mssdflt = mssdflt;
   1085 
   1086 	mutex_enter(softnet_lock);
   1087 	tcp_tcpcb_template();
   1088 	mutex_exit(softnet_lock);
   1089 
   1090 	return (0);
   1091 }
   1092 
   1093 /*
   1094  * sysctl helper for TCP CB template update
   1095  */
   1096 static int
   1097 sysctl_update_tcpcb_template(SYSCTLFN_ARGS)
   1098 {
   1099 	int t, error;
   1100 	struct sysctlnode node;
   1101 
   1102 	/* follow procedures in sysctl(9) manpage */
   1103 	t = *(int *)rnode->sysctl_data;
   1104 	node = *rnode;
   1105 	node.sysctl_data = &t;
   1106 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1107 	if (error || newp == NULL)
   1108 		return error;
   1109 
   1110 	if (t < 0)
   1111 		return EINVAL;
   1112 
   1113 	*(int *)rnode->sysctl_data = t;
   1114 
   1115 	mutex_enter(softnet_lock);
   1116 	tcp_tcpcb_template();
   1117 	mutex_exit(softnet_lock);
   1118 
   1119 	return 0;
   1120 }
   1121 
   1122 /*
   1123  * sysctl helper routine for setting port related values under
   1124  * net.inet.ip and net.inet6.ip6.  does basic range checking and does
   1125  * additional checks for each type.  this code has placed in
   1126  * tcp_input.c since INET and INET6 both use the same tcp code.
   1127  *
   1128  * this helper is not static so that both inet and inet6 can use it.
   1129  */
   1130 int
   1131 sysctl_net_inet_ip_ports(SYSCTLFN_ARGS)
   1132 {
   1133 	int error, tmp;
   1134 	int apmin, apmax;
   1135 #ifndef IPNOPRIVPORTS
   1136 	int lpmin, lpmax;
   1137 #endif /* IPNOPRIVPORTS */
   1138 	struct sysctlnode node;
   1139 
   1140 	if (namelen != 0)
   1141 		return (EINVAL);
   1142 
   1143 	switch (name[-3]) {
   1144 #ifdef INET
   1145 	    case PF_INET:
   1146 		apmin = anonportmin;
   1147 		apmax = anonportmax;
   1148 #ifndef IPNOPRIVPORTS
   1149 		lpmin = lowportmin;
   1150 		lpmax = lowportmax;
   1151 #endif /* IPNOPRIVPORTS */
   1152 		break;
   1153 #endif /* INET */
   1154 #ifdef INET6
   1155 	    case PF_INET6:
   1156 		apmin = ip6_anonportmin;
   1157 		apmax = ip6_anonportmax;
   1158 #ifndef IPNOPRIVPORTS
   1159 		lpmin = ip6_lowportmin;
   1160 		lpmax = ip6_lowportmax;
   1161 #endif /* IPNOPRIVPORTS */
   1162 		break;
   1163 #endif /* INET6 */
   1164 	    default:
   1165 		return (EINVAL);
   1166 	}
   1167 
   1168 	/*
   1169 	 * insert temporary copy into node, perform lookup on
   1170 	 * temporary, then restore pointer
   1171 	 */
   1172 	node = *rnode;
   1173 	tmp = *(int*)rnode->sysctl_data;
   1174 	node.sysctl_data = &tmp;
   1175 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1176 	if (error || newp == NULL)
   1177 		return (error);
   1178 
   1179 	/*
   1180 	 * simple port range check
   1181 	 */
   1182 	if (tmp < 0 || tmp > 65535)
   1183 		return (EINVAL);
   1184 
   1185 	/*
   1186 	 * per-node range checks
   1187 	 */
   1188 	switch (rnode->sysctl_num) {
   1189 	case IPCTL_ANONPORTMIN:
   1190 	case IPV6CTL_ANONPORTMIN:
   1191 		if (tmp >= apmax)
   1192 			return (EINVAL);
   1193 #ifndef IPNOPRIVPORTS
   1194 		if (tmp < IPPORT_RESERVED)
   1195                         return (EINVAL);
   1196 #endif /* IPNOPRIVPORTS */
   1197 		break;
   1198 
   1199 	case IPCTL_ANONPORTMAX:
   1200 	case IPV6CTL_ANONPORTMAX:
   1201                 if (apmin >= tmp)
   1202 			return (EINVAL);
   1203 #ifndef IPNOPRIVPORTS
   1204 		if (tmp < IPPORT_RESERVED)
   1205                         return (EINVAL);
   1206 #endif /* IPNOPRIVPORTS */
   1207 		break;
   1208 
   1209 #ifndef IPNOPRIVPORTS
   1210 	case IPCTL_LOWPORTMIN:
   1211 	case IPV6CTL_LOWPORTMIN:
   1212 		if (tmp >= lpmax ||
   1213 		    tmp > IPPORT_RESERVEDMAX ||
   1214 		    tmp < IPPORT_RESERVEDMIN)
   1215 			return (EINVAL);
   1216 		break;
   1217 
   1218 	case IPCTL_LOWPORTMAX:
   1219 	case IPV6CTL_LOWPORTMAX:
   1220 		if (lpmin >= tmp ||
   1221 		    tmp > IPPORT_RESERVEDMAX ||
   1222 		    tmp < IPPORT_RESERVEDMIN)
   1223 			return (EINVAL);
   1224 		break;
   1225 #endif /* IPNOPRIVPORTS */
   1226 
   1227 	default:
   1228 		return (EINVAL);
   1229 	}
   1230 
   1231 	*(int*)rnode->sysctl_data = tmp;
   1232 
   1233 	return (0);
   1234 }
   1235 
   1236 static inline int
   1237 copyout_uid(struct socket *sockp, void *oldp, size_t *oldlenp)
   1238 {
   1239 	if (oldp) {
   1240 		size_t sz;
   1241 		uid_t uid;
   1242 		int error;
   1243 
   1244 		if (sockp->so_cred == NULL)
   1245 			return EPERM;
   1246 
   1247 		uid = kauth_cred_geteuid(sockp->so_cred);
   1248 		sz = MIN(sizeof(uid), *oldlenp);
   1249 		if ((error = copyout(&uid, oldp, sz)) != 0)
   1250 			return error;
   1251 	}
   1252 	*oldlenp = sizeof(uid_t);
   1253 	return 0;
   1254 }
   1255 
   1256 static inline int
   1257 inet4_ident_core(struct in_addr raddr, u_int rport,
   1258     struct in_addr laddr, u_int lport,
   1259     void *oldp, size_t *oldlenp,
   1260     struct lwp *l, int dodrop)
   1261 {
   1262 	struct inpcb *inp;
   1263 	struct socket *sockp;
   1264 
   1265 	inp = in_pcblookup_connect(&tcbtable, raddr, rport, laddr, lport, 0);
   1266 
   1267 	if (inp == NULL || (sockp = inp->inp_socket) == NULL)
   1268 		return ESRCH;
   1269 
   1270 	if (dodrop) {
   1271 		struct tcpcb *tp;
   1272 		int error;
   1273 
   1274 		if (inp == NULL || (tp = intotcpcb(inp)) == NULL ||
   1275 		    (inp->inp_socket->so_options & SO_ACCEPTCONN) != 0)
   1276 			return ESRCH;
   1277 
   1278 		error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
   1279 		    KAUTH_REQ_NETWORK_SOCKET_DROP, inp->inp_socket, tp, NULL);
   1280 		if (error)
   1281 			return (error);
   1282 
   1283 		(void)tcp_drop(tp, ECONNABORTED);
   1284 		return 0;
   1285 	}
   1286 	else
   1287 		return copyout_uid(sockp, oldp, oldlenp);
   1288 }
   1289 
   1290 #ifdef INET6
   1291 static inline int
   1292 inet6_ident_core(struct in6_addr *raddr, u_int rport,
   1293     struct in6_addr *laddr, u_int lport,
   1294     void *oldp, size_t *oldlenp,
   1295     struct lwp *l, int dodrop)
   1296 {
   1297 	struct in6pcb *in6p;
   1298 	struct socket *sockp;
   1299 
   1300 	in6p = in6_pcblookup_connect(&tcbtable, raddr, rport, laddr, lport, 0, 0);
   1301 
   1302 	if (in6p == NULL || (sockp = in6p->in6p_socket) == NULL)
   1303 		return ESRCH;
   1304 
   1305 	if (dodrop) {
   1306 		struct tcpcb *tp;
   1307 		int error;
   1308 
   1309 		if (in6p == NULL || (tp = in6totcpcb(in6p)) == NULL ||
   1310 		    (in6p->in6p_socket->so_options & SO_ACCEPTCONN) != 0)
   1311 			return ESRCH;
   1312 
   1313 		error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
   1314 		    KAUTH_REQ_NETWORK_SOCKET_DROP, in6p->in6p_socket, tp, NULL);
   1315 		if (error)
   1316 			return (error);
   1317 
   1318 		(void)tcp_drop(tp, ECONNABORTED);
   1319 		return 0;
   1320 	}
   1321 	else
   1322 		return copyout_uid(sockp, oldp, oldlenp);
   1323 }
   1324 #endif
   1325 
   1326 /*
   1327  * sysctl helper routine for the net.inet.tcp.drop and
   1328  * net.inet6.tcp6.drop nodes.
   1329  */
   1330 #define sysctl_net_inet_tcp_drop sysctl_net_inet_tcp_ident
   1331 
   1332 /*
   1333  * sysctl helper routine for the net.inet.tcp.ident and
   1334  * net.inet6.tcp6.ident nodes.  contains backwards compat code for the
   1335  * old way of looking up the ident information for ipv4 which involves
   1336  * stuffing the port/addr pairs into the mib lookup.
   1337  */
   1338 static int
   1339 sysctl_net_inet_tcp_ident(SYSCTLFN_ARGS)
   1340 {
   1341 #ifdef INET
   1342 	struct sockaddr_in *si4[2];
   1343 #endif /* INET */
   1344 #ifdef INET6
   1345 	struct sockaddr_in6 *si6[2];
   1346 #endif /* INET6 */
   1347 	struct sockaddr_storage sa[2];
   1348 	int error, pf, dodrop;
   1349 
   1350 	dodrop = name[-1] == TCPCTL_DROP;
   1351 	if (dodrop) {
   1352 		if (oldp != NULL || *oldlenp != 0)
   1353 			return EINVAL;
   1354 		if (newp == NULL)
   1355 			return EPERM;
   1356 		if (newlen < sizeof(sa))
   1357 			return ENOMEM;
   1358 	}
   1359 	if (namelen != 4 && namelen != 0)
   1360 		return EINVAL;
   1361 	if (name[-2] != IPPROTO_TCP)
   1362 		return EINVAL;
   1363 	pf = name[-3];
   1364 
   1365 	/* old style lookup, ipv4 only */
   1366 	if (namelen == 4) {
   1367 #ifdef INET
   1368 		struct in_addr laddr, raddr;
   1369 		u_int lport, rport;
   1370 
   1371 		if (pf != PF_INET)
   1372 			return EPROTONOSUPPORT;
   1373 		raddr.s_addr = (uint32_t)name[0];
   1374 		rport = (u_int)name[1];
   1375 		laddr.s_addr = (uint32_t)name[2];
   1376 		lport = (u_int)name[3];
   1377 
   1378 		mutex_enter(softnet_lock);
   1379 		error = inet4_ident_core(raddr, rport, laddr, lport,
   1380 		    oldp, oldlenp, l, dodrop);
   1381 		mutex_exit(softnet_lock);
   1382 		return error;
   1383 #else /* INET */
   1384 		return EINVAL;
   1385 #endif /* INET */
   1386 	}
   1387 
   1388 	if (newp == NULL || newlen != sizeof(sa))
   1389 		return EINVAL;
   1390 	error = copyin(newp, &sa, newlen);
   1391 	if (error)
   1392 		return error;
   1393 
   1394 	/*
   1395 	 * requested families must match
   1396 	 */
   1397 	if (pf != sa[0].ss_family || sa[0].ss_family != sa[1].ss_family)
   1398 		return EINVAL;
   1399 
   1400 	switch (pf) {
   1401 #ifdef INET6
   1402 	case PF_INET6:
   1403 		si6[0] = (struct sockaddr_in6*)&sa[0];
   1404 		si6[1] = (struct sockaddr_in6*)&sa[1];
   1405 		if (si6[0]->sin6_len != sizeof(*si6[0]) ||
   1406 		    si6[1]->sin6_len != sizeof(*si6[1]))
   1407 			return EINVAL;
   1408 
   1409 		if (!IN6_IS_ADDR_V4MAPPED(&si6[0]->sin6_addr) &&
   1410 		    !IN6_IS_ADDR_V4MAPPED(&si6[1]->sin6_addr)) {
   1411 			error = sa6_embedscope(si6[0], ip6_use_defzone);
   1412 			if (error)
   1413 				return error;
   1414 			error = sa6_embedscope(si6[1], ip6_use_defzone);
   1415 			if (error)
   1416 				return error;
   1417 
   1418 			mutex_enter(softnet_lock);
   1419 			error = inet6_ident_core(&si6[0]->sin6_addr,
   1420 			    si6[0]->sin6_port, &si6[1]->sin6_addr,
   1421 			    si6[1]->sin6_port, oldp, oldlenp, l, dodrop);
   1422 			mutex_exit(softnet_lock);
   1423 			return error;
   1424 		}
   1425 
   1426 		if (IN6_IS_ADDR_V4MAPPED(&si6[0]->sin6_addr) !=
   1427 		    IN6_IS_ADDR_V4MAPPED(&si6[1]->sin6_addr))
   1428 			return EINVAL;
   1429 
   1430 		in6_sin6_2_sin_in_sock((struct sockaddr *)&sa[0]);
   1431 		in6_sin6_2_sin_in_sock((struct sockaddr *)&sa[1]);
   1432 		/*FALLTHROUGH*/
   1433 #endif /* INET6 */
   1434 #ifdef INET
   1435 	case PF_INET:
   1436 		si4[0] = (struct sockaddr_in*)&sa[0];
   1437 		si4[1] = (struct sockaddr_in*)&sa[1];
   1438 		if (si4[0]->sin_len != sizeof(*si4[0]) ||
   1439 		    si4[0]->sin_len != sizeof(*si4[1]))
   1440 			return EINVAL;
   1441 
   1442 		mutex_enter(softnet_lock);
   1443 		error = inet4_ident_core(si4[0]->sin_addr, si4[0]->sin_port,
   1444 		    si4[1]->sin_addr, si4[1]->sin_port,
   1445 		    oldp, oldlenp, l, dodrop);
   1446 		mutex_exit(softnet_lock);
   1447 		return error;
   1448 #endif /* INET */
   1449 	default:
   1450 		return EPROTONOSUPPORT;
   1451 	}
   1452 }
   1453 
   1454 /*
   1455  * sysctl helper for the inet and inet6 pcblists.  handles tcp/udp and
   1456  * inet/inet6, as well as raw pcbs for each.  specifically not
   1457  * declared static so that raw sockets and udp/udp6 can use it as
   1458  * well.
   1459  */
   1460 int
   1461 sysctl_inpcblist(SYSCTLFN_ARGS)
   1462 {
   1463 #ifdef INET
   1464 	struct sockaddr_in *in;
   1465 	const struct inpcb *inp;
   1466 #endif
   1467 #ifdef INET6
   1468 	struct sockaddr_in6 *in6;
   1469 	const struct in6pcb *in6p;
   1470 #endif
   1471 	struct inpcbtable *pcbtbl = __UNCONST(rnode->sysctl_data);
   1472 	const struct inpcb_hdr *inph;
   1473 	struct tcpcb *tp;
   1474 	struct kinfo_pcb pcb;
   1475 	char *dp;
   1476 	size_t len, needed, elem_size, out_size;
   1477 	int error, elem_count, pf, proto, pf2;
   1478 
   1479 	if (namelen != 4)
   1480 		return (EINVAL);
   1481 
   1482 	if (oldp != NULL) {
   1483 		    len = *oldlenp;
   1484 		    elem_size = name[2];
   1485 		    elem_count = name[3];
   1486 		    if (elem_size != sizeof(pcb))
   1487 			    return EINVAL;
   1488 	} else {
   1489 		    len = 0;
   1490 		    elem_count = INT_MAX;
   1491 		    elem_size = sizeof(pcb);
   1492 	}
   1493 	error = 0;
   1494 	dp = oldp;
   1495 	out_size = elem_size;
   1496 	needed = 0;
   1497 
   1498 	if (namelen == 1 && name[0] == CTL_QUERY)
   1499 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   1500 
   1501 	if (name - oname != 4)
   1502 		return (EINVAL);
   1503 
   1504 	pf = oname[1];
   1505 	proto = oname[2];
   1506 	pf2 = (oldp != NULL) ? pf : 0;
   1507 
   1508 	mutex_enter(softnet_lock);
   1509 
   1510 	TAILQ_FOREACH(inph, &pcbtbl->inpt_queue, inph_queue) {
   1511 #ifdef INET
   1512 		inp = (const struct inpcb *)inph;
   1513 #endif
   1514 #ifdef INET6
   1515 		in6p = (const struct in6pcb *)inph;
   1516 #endif
   1517 
   1518 		if (inph->inph_af != pf)
   1519 			continue;
   1520 
   1521 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
   1522 		    KAUTH_REQ_NETWORK_SOCKET_CANSEE, inph->inph_socket, NULL,
   1523 		    NULL) != 0)
   1524 			continue;
   1525 
   1526 		memset(&pcb, 0, sizeof(pcb));
   1527 
   1528 		pcb.ki_family = pf;
   1529 		pcb.ki_type = proto;
   1530 
   1531 		switch (pf2) {
   1532 		case 0:
   1533 			/* just probing for size */
   1534 			break;
   1535 #ifdef INET
   1536 		case PF_INET:
   1537 			pcb.ki_family = inp->inp_socket->so_proto->
   1538 			    pr_domain->dom_family;
   1539 			pcb.ki_type = inp->inp_socket->so_proto->
   1540 			    pr_type;
   1541 			pcb.ki_protocol = inp->inp_socket->so_proto->
   1542 			    pr_protocol;
   1543 			pcb.ki_pflags = inp->inp_flags;
   1544 
   1545 			pcb.ki_sostate = inp->inp_socket->so_state;
   1546 			pcb.ki_prstate = inp->inp_state;
   1547 			if (proto == IPPROTO_TCP) {
   1548 				tp = intotcpcb(inp);
   1549 				pcb.ki_tstate = tp->t_state;
   1550 				pcb.ki_tflags = tp->t_flags;
   1551 			}
   1552 
   1553 			pcb.ki_pcbaddr = PTRTOUINT64(inp);
   1554 			pcb.ki_ppcbaddr = PTRTOUINT64(inp->inp_ppcb);
   1555 			pcb.ki_sockaddr = PTRTOUINT64(inp->inp_socket);
   1556 
   1557 			pcb.ki_rcvq = inp->inp_socket->so_rcv.sb_cc;
   1558 			pcb.ki_sndq = inp->inp_socket->so_snd.sb_cc;
   1559 
   1560 			in = satosin(&pcb.ki_src);
   1561 			in->sin_len = sizeof(*in);
   1562 			in->sin_family = pf;
   1563 			in->sin_port = inp->inp_lport;
   1564 			in->sin_addr = inp->inp_laddr;
   1565 			if (pcb.ki_prstate >= INP_CONNECTED) {
   1566 				in = satosin(&pcb.ki_dst);
   1567 				in->sin_len = sizeof(*in);
   1568 				in->sin_family = pf;
   1569 				in->sin_port = inp->inp_fport;
   1570 				in->sin_addr = inp->inp_faddr;
   1571 			}
   1572 			break;
   1573 #endif
   1574 #ifdef INET6
   1575 		case PF_INET6:
   1576 			pcb.ki_family = in6p->in6p_socket->so_proto->
   1577 			    pr_domain->dom_family;
   1578 			pcb.ki_type = in6p->in6p_socket->so_proto->pr_type;
   1579 			pcb.ki_protocol = in6p->in6p_socket->so_proto->
   1580 			    pr_protocol;
   1581 			pcb.ki_pflags = in6p->in6p_flags;
   1582 
   1583 			pcb.ki_sostate = in6p->in6p_socket->so_state;
   1584 			pcb.ki_prstate = in6p->in6p_state;
   1585 			if (proto == IPPROTO_TCP) {
   1586 				tp = in6totcpcb(in6p);
   1587 				pcb.ki_tstate = tp->t_state;
   1588 				pcb.ki_tflags = tp->t_flags;
   1589 			}
   1590 
   1591 			pcb.ki_pcbaddr = PTRTOUINT64(in6p);
   1592 			pcb.ki_ppcbaddr = PTRTOUINT64(in6p->in6p_ppcb);
   1593 			pcb.ki_sockaddr = PTRTOUINT64(in6p->in6p_socket);
   1594 
   1595 			pcb.ki_rcvq = in6p->in6p_socket->so_rcv.sb_cc;
   1596 			pcb.ki_sndq = in6p->in6p_socket->so_snd.sb_cc;
   1597 
   1598 			in6 = satosin6(&pcb.ki_src);
   1599 			in6->sin6_len = sizeof(*in6);
   1600 			in6->sin6_family = pf;
   1601 			in6->sin6_port = in6p->in6p_lport;
   1602 			in6->sin6_flowinfo = in6p->in6p_flowinfo;
   1603 			in6->sin6_addr = in6p->in6p_laddr;
   1604 			in6->sin6_scope_id = 0; /* XXX? */
   1605 
   1606 			if (pcb.ki_prstate >= IN6P_CONNECTED) {
   1607 				in6 = satosin6(&pcb.ki_dst);
   1608 				in6->sin6_len = sizeof(*in6);
   1609 				in6->sin6_family = pf;
   1610 				in6->sin6_port = in6p->in6p_fport;
   1611 				in6->sin6_flowinfo = in6p->in6p_flowinfo;
   1612 				in6->sin6_addr = in6p->in6p_faddr;
   1613 				in6->sin6_scope_id = 0; /* XXX? */
   1614 			}
   1615 			break;
   1616 #endif
   1617 		}
   1618 
   1619 		if (len >= elem_size && elem_count > 0) {
   1620 			error = copyout(&pcb, dp, out_size);
   1621 			if (error) {
   1622 				mutex_exit(softnet_lock);
   1623 				return (error);
   1624 			}
   1625 			dp += elem_size;
   1626 			len -= elem_size;
   1627 		}
   1628 		needed += elem_size;
   1629 		if (elem_count > 0 && elem_count != INT_MAX)
   1630 			elem_count--;
   1631 	}
   1632 
   1633 	*oldlenp = needed;
   1634 	if (oldp == NULL)
   1635 		*oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb);
   1636 
   1637 	mutex_exit(softnet_lock);
   1638 
   1639 	return (error);
   1640 }
   1641 
   1642 static int
   1643 sysctl_tcp_congctl(SYSCTLFN_ARGS)
   1644 {
   1645 	struct sysctlnode node;
   1646 	int error;
   1647 	char newname[TCPCC_MAXLEN];
   1648 
   1649 	strlcpy(newname, tcp_congctl_global_name, sizeof(newname) - 1);
   1650 
   1651 	node = *rnode;
   1652 	node.sysctl_data = newname;
   1653 	node.sysctl_size = sizeof(newname);
   1654 
   1655 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1656 
   1657 	if (error ||
   1658 	    newp == NULL ||
   1659 	    strncmp(newname, tcp_congctl_global_name, sizeof(newname)) == 0)
   1660 		return error;
   1661 
   1662 	mutex_enter(softnet_lock);
   1663 	error = tcp_congctl_select(NULL, newname);
   1664 	mutex_exit(softnet_lock);
   1665 
   1666 	return error;
   1667 }
   1668 
   1669 static int
   1670 sysctl_tcp_init_win(SYSCTLFN_ARGS)
   1671 {
   1672 	int error;
   1673 	u_int iw;
   1674 	struct sysctlnode node;
   1675 
   1676 	iw = *(u_int *)rnode->sysctl_data;
   1677 	node = *rnode;
   1678 	node.sysctl_data = &iw;
   1679 	node.sysctl_size = sizeof(iw);
   1680 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1681 	if (error || newp == NULL)
   1682 		return error;
   1683 
   1684 	if (iw >= __arraycount(tcp_init_win_max))
   1685 		return EINVAL;
   1686 	*(u_int *)rnode->sysctl_data = iw;
   1687 	return 0;
   1688 }
   1689 
   1690 static int
   1691 sysctl_tcp_keep(SYSCTLFN_ARGS)
   1692 {
   1693 	int error;
   1694 	u_int tmp;
   1695 	struct sysctlnode node;
   1696 
   1697 	node = *rnode;
   1698 	tmp = *(u_int *)rnode->sysctl_data;
   1699 	node.sysctl_data = &tmp;
   1700 
   1701 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1702 	if (error || newp == NULL)
   1703 		return error;
   1704 
   1705 	mutex_enter(softnet_lock);
   1706 
   1707 	*(u_int *)rnode->sysctl_data = tmp;
   1708 	tcp_tcpcb_template();	/* update the template */
   1709 
   1710 	mutex_exit(softnet_lock);
   1711 	return 0;
   1712 }
   1713 
   1714 static int
   1715 sysctl_net_inet_tcp_stats(SYSCTLFN_ARGS)
   1716 {
   1717 
   1718 	return (NETSTAT_SYSCTL(tcpstat_percpu, TCP_NSTATS));
   1719 }
   1720 
   1721 /*
   1722  * this (second stage) setup routine is a replacement for tcp_sysctl()
   1723  * (which is currently used for ipv4 and ipv6)
   1724  */
   1725 static void
   1726 sysctl_net_inet_tcp_setup2(struct sysctllog **clog, int pf, const char *pfname,
   1727 			   const char *tcpname)
   1728 {
   1729 	const struct sysctlnode *sack_node;
   1730 	const struct sysctlnode *abc_node;
   1731 	const struct sysctlnode *ecn_node;
   1732 	const struct sysctlnode *congctl_node;
   1733 	const struct sysctlnode *mslt_node;
   1734 	const struct sysctlnode *vtw_node;
   1735 #ifdef TCP_DEBUG
   1736 	extern struct tcp_debug tcp_debug[TCP_NDEBUG];
   1737 	extern int tcp_debx;
   1738 #endif
   1739 
   1740 	sysctl_createv(clog, 0, NULL, NULL,
   1741 		       CTLFLAG_PERMANENT,
   1742 		       CTLTYPE_NODE, pfname, NULL,
   1743 		       NULL, 0, NULL, 0,
   1744 		       CTL_NET, pf, CTL_EOL);
   1745 	sysctl_createv(clog, 0, NULL, NULL,
   1746 		       CTLFLAG_PERMANENT,
   1747 		       CTLTYPE_NODE, tcpname,
   1748 		       SYSCTL_DESCR("TCP related settings"),
   1749 		       NULL, 0, NULL, 0,
   1750 		       CTL_NET, pf, IPPROTO_TCP, CTL_EOL);
   1751 
   1752 	sysctl_createv(clog, 0, NULL, NULL,
   1753 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1754 		       CTLTYPE_INT, "rfc1323",
   1755 		       SYSCTL_DESCR("Enable RFC1323 TCP extensions"),
   1756 		       sysctl_update_tcpcb_template, 0, &tcp_do_rfc1323, 0,
   1757 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RFC1323, CTL_EOL);
   1758 	sysctl_createv(clog, 0, NULL, NULL,
   1759 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1760 		       CTLTYPE_INT, "sendspace",
   1761 		       SYSCTL_DESCR("Default TCP send buffer size"),
   1762 		       NULL, 0, &tcp_sendspace, 0,
   1763 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SENDSPACE, CTL_EOL);
   1764 	sysctl_createv(clog, 0, NULL, NULL,
   1765 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1766 		       CTLTYPE_INT, "recvspace",
   1767 		       SYSCTL_DESCR("Default TCP receive buffer size"),
   1768 		       NULL, 0, &tcp_recvspace, 0,
   1769 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RECVSPACE, CTL_EOL);
   1770 	sysctl_createv(clog, 0, NULL, NULL,
   1771 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1772 		       CTLTYPE_INT, "mssdflt",
   1773 		       SYSCTL_DESCR("Default maximum segment size"),
   1774 		       sysctl_net_inet_tcp_mssdflt, 0, &tcp_mssdflt, 0,
   1775 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSSDFLT, CTL_EOL);
   1776 	sysctl_createv(clog, 0, NULL, NULL,
   1777 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1778 		       CTLTYPE_INT, "minmss",
   1779 		       SYSCTL_DESCR("Lower limit for TCP maximum segment size"),
   1780 		       NULL, 0, &tcp_minmss, 0,
   1781 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   1782 	sysctl_createv(clog, 0, NULL, NULL,
   1783 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1784 		       CTLTYPE_INT, "msl",
   1785 		       SYSCTL_DESCR("Maximum Segment Life"),
   1786 		       NULL, 0, &tcp_msl, 0,
   1787 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSL, CTL_EOL);
   1788 	sysctl_createv(clog, 0, NULL, NULL,
   1789 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1790 		       CTLTYPE_INT, "syn_cache_limit",
   1791 		       SYSCTL_DESCR("Maximum number of entries in the TCP "
   1792 				    "compressed state engine"),
   1793 		       NULL, 0, &tcp_syn_cache_limit, 0,
   1794 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_LIMIT,
   1795 		       CTL_EOL);
   1796 	sysctl_createv(clog, 0, NULL, NULL,
   1797 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1798 		       CTLTYPE_INT, "syn_bucket_limit",
   1799 		       SYSCTL_DESCR("Maximum number of entries per hash "
   1800 				    "bucket in the TCP compressed state "
   1801 				    "engine"),
   1802 		       NULL, 0, &tcp_syn_bucket_limit, 0,
   1803 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_BUCKET_LIMIT,
   1804 		       CTL_EOL);
   1805 #if 0 /* obsoleted */
   1806 	sysctl_createv(clog, 0, NULL, NULL,
   1807 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1808 		       CTLTYPE_INT, "syn_cache_interval",
   1809 		       SYSCTL_DESCR("TCP compressed state engine's timer interval"),
   1810 		       NULL, 0, &tcp_syn_cache_interval, 0,
   1811 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_INTER,
   1812 		       CTL_EOL);
   1813 #endif
   1814 	sysctl_createv(clog, 0, NULL, NULL,
   1815 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1816 		       CTLTYPE_INT, "init_win",
   1817 		       SYSCTL_DESCR("Initial TCP congestion window"),
   1818 		       sysctl_tcp_init_win, 0, &tcp_init_win, 0,
   1819 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN, CTL_EOL);
   1820 	sysctl_createv(clog, 0, NULL, NULL,
   1821 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1822 		       CTLTYPE_INT, "mss_ifmtu",
   1823 		       SYSCTL_DESCR("Use interface MTU for calculating MSS"),
   1824 		       NULL, 0, &tcp_mss_ifmtu, 0,
   1825 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSS_IFMTU, CTL_EOL);
   1826 	sysctl_createv(clog, 0, NULL, &sack_node,
   1827 		       CTLFLAG_PERMANENT,
   1828 		       CTLTYPE_NODE, "sack",
   1829 		       SYSCTL_DESCR("RFC2018 Selective ACKnowledgement tunables"),
   1830 		       NULL, 0, NULL, 0,
   1831 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_EOL);
   1832 
   1833 	/* Congctl subtree */
   1834 	sysctl_createv(clog, 0, NULL, &congctl_node,
   1835 		       CTLFLAG_PERMANENT,
   1836 		       CTLTYPE_NODE, "congctl",
   1837 		       SYSCTL_DESCR("TCP Congestion Control"),
   1838 	    	       NULL, 0, NULL, 0,
   1839 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   1840 	sysctl_createv(clog, 0, &congctl_node, NULL,
   1841 		       CTLFLAG_PERMANENT,
   1842 		       CTLTYPE_STRING, "available",
   1843 		       SYSCTL_DESCR("Available Congestion Control Mechanisms"),
   1844 		       NULL, 0, tcp_congctl_avail, 0, CTL_CREATE, CTL_EOL);
   1845 	sysctl_createv(clog, 0, &congctl_node, NULL,
   1846 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1847 		       CTLTYPE_STRING, "selected",
   1848 		       SYSCTL_DESCR("Selected Congestion Control Mechanism"),
   1849 		       sysctl_tcp_congctl, 0, NULL, TCPCC_MAXLEN,
   1850 		       CTL_CREATE, CTL_EOL);
   1851 
   1852 	sysctl_createv(clog, 0, NULL, NULL,
   1853 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1854 		       CTLTYPE_INT, "win_scale",
   1855 		       SYSCTL_DESCR("Use RFC1323 window scale options"),
   1856 		       sysctl_update_tcpcb_template, 0, &tcp_do_win_scale, 0,
   1857 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_WSCALE, CTL_EOL);
   1858 	sysctl_createv(clog, 0, NULL, NULL,
   1859 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1860 		       CTLTYPE_INT, "timestamps",
   1861 		       SYSCTL_DESCR("Use RFC1323 time stamp options"),
   1862 		       sysctl_update_tcpcb_template, 0, &tcp_do_timestamps, 0,
   1863 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_TSTAMP, CTL_EOL);
   1864 	sysctl_createv(clog, 0, NULL, NULL,
   1865 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1866 		       CTLTYPE_INT, "compat_42",
   1867 		       SYSCTL_DESCR("Enable workarounds for 4.2BSD TCP bugs"),
   1868 		       NULL, 0, &tcp_compat_42, 0,
   1869 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_COMPAT_42, CTL_EOL);
   1870 	sysctl_createv(clog, 0, NULL, NULL,
   1871 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1872 		       CTLTYPE_INT, "cwm",
   1873 		       SYSCTL_DESCR("Hughes/Touch/Heidemann Congestion Window "
   1874 				    "Monitoring"),
   1875 		       NULL, 0, &tcp_cwm, 0,
   1876 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM, CTL_EOL);
   1877 	sysctl_createv(clog, 0, NULL, NULL,
   1878 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1879 		       CTLTYPE_INT, "cwm_burstsize",
   1880 		       SYSCTL_DESCR("Congestion Window Monitoring allowed "
   1881 				    "burst count in packets"),
   1882 		       NULL, 0, &tcp_cwm_burstsize, 0,
   1883 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM_BURSTSIZE,
   1884 		       CTL_EOL);
   1885 	sysctl_createv(clog, 0, NULL, NULL,
   1886 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1887 		       CTLTYPE_INT, "ack_on_push",
   1888 		       SYSCTL_DESCR("Immediately return ACK when PSH is "
   1889 				    "received"),
   1890 		       NULL, 0, &tcp_ack_on_push, 0,
   1891 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_ACK_ON_PUSH, CTL_EOL);
   1892 	sysctl_createv(clog, 0, NULL, NULL,
   1893 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1894 		       CTLTYPE_INT, "keepidle",
   1895 		       SYSCTL_DESCR("Allowed connection idle ticks before a "
   1896 				    "keepalive probe is sent"),
   1897 		       sysctl_tcp_keep, 0, &tcp_keepidle, 0,
   1898 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPIDLE, CTL_EOL);
   1899 	sysctl_createv(clog, 0, NULL, NULL,
   1900 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1901 		       CTLTYPE_INT, "keepintvl",
   1902 		       SYSCTL_DESCR("Ticks before next keepalive probe is sent"),
   1903 		       sysctl_tcp_keep, 0, &tcp_keepintvl, 0,
   1904 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPINTVL, CTL_EOL);
   1905 	sysctl_createv(clog, 0, NULL, NULL,
   1906 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1907 		       CTLTYPE_INT, "keepcnt",
   1908 		       SYSCTL_DESCR("Number of keepalive probes to send"),
   1909 		       sysctl_tcp_keep, 0, &tcp_keepcnt, 0,
   1910 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPCNT, CTL_EOL);
   1911 	sysctl_createv(clog, 0, NULL, NULL,
   1912 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
   1913 		       CTLTYPE_INT, "slowhz",
   1914 		       SYSCTL_DESCR("Keepalive ticks per second"),
   1915 		       NULL, PR_SLOWHZ, NULL, 0,
   1916 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SLOWHZ, CTL_EOL);
   1917 	sysctl_createv(clog, 0, NULL, NULL,
   1918 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1919 		       CTLTYPE_INT, "log_refused",
   1920 		       SYSCTL_DESCR("Log refused TCP connections"),
   1921 		       NULL, 0, &tcp_log_refused, 0,
   1922 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOG_REFUSED, CTL_EOL);
   1923 #if 0 /* obsoleted */
   1924 	sysctl_createv(clog, 0, NULL, NULL,
   1925 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1926 		       CTLTYPE_INT, "rstratelimit", NULL,
   1927 		       NULL, 0, &tcp_rst_ratelim, 0,
   1928 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTRATELIMIT, CTL_EOL);
   1929 #endif
   1930 	sysctl_createv(clog, 0, NULL, NULL,
   1931 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1932 		       CTLTYPE_INT, "rstppslimit",
   1933 		       SYSCTL_DESCR("Maximum number of RST packets to send "
   1934 				    "per second"),
   1935 		       NULL, 0, &tcp_rst_ppslim, 0,
   1936 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTPPSLIMIT, CTL_EOL);
   1937 	sysctl_createv(clog, 0, NULL, NULL,
   1938 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1939 		       CTLTYPE_INT, "delack_ticks",
   1940 		       SYSCTL_DESCR("Number of ticks to delay sending an ACK"),
   1941 		       NULL, 0, &tcp_delack_ticks, 0,
   1942 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DELACK_TICKS, CTL_EOL);
   1943 	sysctl_createv(clog, 0, NULL, NULL,
   1944 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1945 		       CTLTYPE_INT, "init_win_local",
   1946 		       SYSCTL_DESCR("Initial TCP window size (in segments)"),
   1947 		       sysctl_tcp_init_win, 0, &tcp_init_win_local, 0,
   1948 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN_LOCAL,
   1949 		       CTL_EOL);
   1950 	sysctl_createv(clog, 0, NULL, NULL,
   1951 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1952 		       CTLTYPE_STRUCT, "ident",
   1953 		       SYSCTL_DESCR("RFC1413 Identification Protocol lookups"),
   1954 		       sysctl_net_inet_tcp_ident, 0, NULL, sizeof(uid_t),
   1955 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_IDENT, CTL_EOL);
   1956 	sysctl_createv(clog, 0, NULL, NULL,
   1957 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1958 		       CTLTYPE_INT, "do_loopback_cksum",
   1959 		       SYSCTL_DESCR("Perform TCP checksum on loopback"),
   1960 		       NULL, 0, &tcp_do_loopback_cksum, 0,
   1961 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOOPBACKCKSUM,
   1962 		       CTL_EOL);
   1963 	sysctl_createv(clog, 0, NULL, NULL,
   1964 		       CTLFLAG_PERMANENT,
   1965 		       CTLTYPE_STRUCT, "pcblist",
   1966 		       SYSCTL_DESCR("TCP protocol control block list"),
   1967 		       sysctl_inpcblist, 0, &tcbtable, 0,
   1968 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE,
   1969 		       CTL_EOL);
   1970 	sysctl_createv(clog, 0, NULL, NULL,
   1971 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1972 		       CTLTYPE_INT, "keepinit",
   1973 		       SYSCTL_DESCR("Ticks before initial tcp connection times out"),
   1974 		       sysctl_tcp_keep, 0, &tcp_keepinit, 0,
   1975 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   1976 
   1977 	/* TCP socket buffers auto-sizing nodes */
   1978 	sysctl_createv(clog, 0, NULL, NULL,
   1979 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1980 		       CTLTYPE_INT, "recvbuf_auto",
   1981 		       SYSCTL_DESCR("Enable automatic receive "
   1982 		           "buffer sizing (experimental)"),
   1983 		       NULL, 0, &tcp_do_autorcvbuf, 0,
   1984 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   1985 	sysctl_createv(clog, 0, NULL, NULL,
   1986 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1987 		       CTLTYPE_INT, "recvbuf_inc",
   1988 		       SYSCTL_DESCR("Incrementor step size of "
   1989 		           "automatic receive buffer"),
   1990 		       NULL, 0, &tcp_autorcvbuf_inc, 0,
   1991 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   1992 	sysctl_createv(clog, 0, NULL, NULL,
   1993 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1994 		       CTLTYPE_INT, "recvbuf_max",
   1995 		       SYSCTL_DESCR("Max size of automatic receive buffer"),
   1996 		       NULL, 0, &tcp_autorcvbuf_max, 0,
   1997 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   1998 
   1999 	sysctl_createv(clog, 0, NULL, NULL,
   2000 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2001 		       CTLTYPE_INT, "sendbuf_auto",
   2002 		       SYSCTL_DESCR("Enable automatic send "
   2003 		           "buffer sizing (experimental)"),
   2004 		       NULL, 0, &tcp_do_autosndbuf, 0,
   2005 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   2006 	sysctl_createv(clog, 0, NULL, NULL,
   2007 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2008 		       CTLTYPE_INT, "sendbuf_inc",
   2009 		       SYSCTL_DESCR("Incrementor step size of "
   2010 		           "automatic send buffer"),
   2011 		       NULL, 0, &tcp_autosndbuf_inc, 0,
   2012 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   2013 	sysctl_createv(clog, 0, NULL, NULL,
   2014 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2015 		       CTLTYPE_INT, "sendbuf_max",
   2016 		       SYSCTL_DESCR("Max size of automatic send buffer"),
   2017 		       NULL, 0, &tcp_autosndbuf_max, 0,
   2018 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   2019 
   2020 	/* ECN subtree */
   2021 	sysctl_createv(clog, 0, NULL, &ecn_node,
   2022 	    	       CTLFLAG_PERMANENT,
   2023 		       CTLTYPE_NODE, "ecn",
   2024 	    	       SYSCTL_DESCR("RFC3168 Explicit Congestion Notification"),
   2025 	    	       NULL, 0, NULL, 0,
   2026 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   2027 	sysctl_createv(clog, 0, &ecn_node, NULL,
   2028 	    	       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2029 		       CTLTYPE_INT, "enable",
   2030 		       SYSCTL_DESCR("Enable TCP Explicit Congestion "
   2031 			   "Notification"),
   2032 	    	       NULL, 0, &tcp_do_ecn, 0, CTL_CREATE, CTL_EOL);
   2033 	sysctl_createv(clog, 0, &ecn_node, NULL,
   2034 	    	       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2035 		       CTLTYPE_INT, "maxretries",
   2036 		       SYSCTL_DESCR("Number of times to retry ECN setup "
   2037 			       "before disabling ECN on the connection"),
   2038 	    	       NULL, 0, &tcp_ecn_maxretries, 0, CTL_CREATE, CTL_EOL);
   2039 
   2040 	/* SACK gets it's own little subtree. */
   2041 	sysctl_createv(clog, 0, NULL, &sack_node,
   2042 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2043 		       CTLTYPE_INT, "enable",
   2044 		       SYSCTL_DESCR("Enable RFC2018 Selective ACKnowledgement"),
   2045 		       NULL, 0, &tcp_do_sack, 0,
   2046 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
   2047 	sysctl_createv(clog, 0, NULL, &sack_node,
   2048 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2049 		       CTLTYPE_INT, "maxholes",
   2050 		       SYSCTL_DESCR("Maximum number of TCP SACK holes allowed per connection"),
   2051 		       NULL, 0, &tcp_sack_tp_maxholes, 0,
   2052 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
   2053 	sysctl_createv(clog, 0, NULL, &sack_node,
   2054 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2055 		       CTLTYPE_INT, "globalmaxholes",
   2056 		       SYSCTL_DESCR("Global maximum number of TCP SACK holes"),
   2057 		       NULL, 0, &tcp_sack_globalmaxholes, 0,
   2058 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
   2059 	sysctl_createv(clog, 0, NULL, &sack_node,
   2060 		       CTLFLAG_PERMANENT,
   2061 		       CTLTYPE_INT, "globalholes",
   2062 		       SYSCTL_DESCR("Global number of TCP SACK holes"),
   2063 		       NULL, 0, &tcp_sack_globalholes, 0,
   2064 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
   2065 
   2066 	sysctl_createv(clog, 0, NULL, NULL,
   2067 		       CTLFLAG_PERMANENT,
   2068 		       CTLTYPE_STRUCT, "stats",
   2069 		       SYSCTL_DESCR("TCP statistics"),
   2070 		       sysctl_net_inet_tcp_stats, 0, NULL, 0,
   2071 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_STATS,
   2072 		       CTL_EOL);
   2073         sysctl_createv(clog, 0, NULL, NULL,
   2074                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2075                        CTLTYPE_INT, "local_by_rtt",
   2076                        SYSCTL_DESCR("Use RTT estimator to decide which hosts "
   2077 				    "are local"),
   2078 		       NULL, 0, &tcp_rttlocal, 0,
   2079 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   2080 #ifdef TCP_DEBUG
   2081 	sysctl_createv(clog, 0, NULL, NULL,
   2082 		       CTLFLAG_PERMANENT,
   2083 		       CTLTYPE_STRUCT, "debug",
   2084 		       SYSCTL_DESCR("TCP sockets debug information"),
   2085 		       NULL, 0, &tcp_debug, sizeof(tcp_debug),
   2086 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBUG,
   2087 		       CTL_EOL);
   2088 	sysctl_createv(clog, 0, NULL, NULL,
   2089 		       CTLFLAG_PERMANENT,
   2090 		       CTLTYPE_INT, "debx",
   2091 		       SYSCTL_DESCR("Number of TCP debug sockets messages"),
   2092 		       NULL, 0, &tcp_debx, sizeof(tcp_debx),
   2093 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBX,
   2094 		       CTL_EOL);
   2095 #endif
   2096 	sysctl_createv(clog, 0, NULL, NULL,
   2097 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2098 		       CTLTYPE_STRUCT, "drop",
   2099 		       SYSCTL_DESCR("TCP drop connection"),
   2100 		       sysctl_net_inet_tcp_drop, 0, NULL, 0,
   2101 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DROP, CTL_EOL);
   2102 	sysctl_createv(clog, 0, NULL, NULL,
   2103 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2104 		       CTLTYPE_INT, "iss_hash",
   2105 		       SYSCTL_DESCR("Enable RFC 1948 ISS by cryptographic "
   2106 				    "hash computation"),
   2107 		       NULL, 0, &tcp_do_rfc1948, sizeof(tcp_do_rfc1948),
   2108 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE,
   2109 		       CTL_EOL);
   2110 
   2111 	/* ABC subtree */
   2112 
   2113 	sysctl_createv(clog, 0, NULL, &abc_node,
   2114 		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "abc",
   2115 		       SYSCTL_DESCR("RFC3465 Appropriate Byte Counting (ABC)"),
   2116 		       NULL, 0, NULL, 0,
   2117 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   2118 	sysctl_createv(clog, 0, &abc_node, NULL,
   2119 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2120 		       CTLTYPE_INT, "enable",
   2121 		       SYSCTL_DESCR("Enable RFC3465 Appropriate Byte Counting"),
   2122 		       NULL, 0, &tcp_do_abc, 0, CTL_CREATE, CTL_EOL);
   2123 	sysctl_createv(clog, 0, &abc_node, NULL,
   2124 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2125 		       CTLTYPE_INT, "aggressive",
   2126 		       SYSCTL_DESCR("1: L=2*SMSS 0: L=1*SMSS"),
   2127 		       NULL, 0, &tcp_abc_aggressive, 0, CTL_CREATE, CTL_EOL);
   2128 
   2129 	/* MSL tuning subtree */
   2130 
   2131 	sysctl_createv(clog, 0, NULL, &mslt_node,
   2132 		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "mslt",
   2133 		       SYSCTL_DESCR("MSL Tuning for TIME_WAIT truncation"),
   2134 		       NULL, 0, NULL, 0,
   2135 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   2136 	sysctl_createv(clog, 0, &mslt_node, NULL,
   2137 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2138 		       CTLTYPE_INT, "enable",
   2139 		       SYSCTL_DESCR("Enable TIME_WAIT truncation"),
   2140 		       NULL, 0, &tcp_msl_enable, 0, CTL_CREATE, CTL_EOL);
   2141 	sysctl_createv(clog, 0, &mslt_node, NULL,
   2142 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2143 		       CTLTYPE_INT, "loopback",
   2144 		       SYSCTL_DESCR("MSL value to use for loopback connections"),
   2145 		       NULL, 0, &tcp_msl_loop, 0, CTL_CREATE, CTL_EOL);
   2146 	sysctl_createv(clog, 0, &mslt_node, NULL,
   2147 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2148 		       CTLTYPE_INT, "local",
   2149 		       SYSCTL_DESCR("MSL value to use for local connections"),
   2150 		       NULL, 0, &tcp_msl_local, 0, CTL_CREATE, CTL_EOL);
   2151 	sysctl_createv(clog, 0, &mslt_node, NULL,
   2152 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2153 		       CTLTYPE_INT, "remote",
   2154 		       SYSCTL_DESCR("MSL value to use for remote connections"),
   2155 		       NULL, 0, &tcp_msl_remote, 0, CTL_CREATE, CTL_EOL);
   2156 	sysctl_createv(clog, 0, &mslt_node, NULL,
   2157 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2158 		       CTLTYPE_INT, "remote_threshold",
   2159 		       SYSCTL_DESCR("RTT estimate value to promote local to remote"),
   2160 		       NULL, 0, &tcp_msl_remote_threshold, 0, CTL_CREATE, CTL_EOL);
   2161 
   2162 	/* vestigial TIME_WAIT tuning subtree */
   2163 
   2164 	sysctl_createv(clog, 0, NULL, &vtw_node,
   2165 		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "vtw",
   2166 		       SYSCTL_DESCR("Tuning for Vestigial TIME_WAIT"),
   2167 		       NULL, 0, NULL, 0,
   2168 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   2169 	sysctl_createv(clog, 0, &vtw_node, NULL,
   2170 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2171 		       CTLTYPE_INT, "enable",
   2172 		       SYSCTL_DESCR("Enable Vestigial TIME_WAIT"),
   2173 		       sysctl_tcp_vtw_enable, 0,
   2174 	               (pf == AF_INET) ? &tcp4_vtw_enable : &tcp6_vtw_enable,
   2175 		       0, CTL_CREATE, CTL_EOL);
   2176 	sysctl_createv(clog, 0, &vtw_node, NULL,
   2177 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
   2178 		       CTLTYPE_INT, "entries",
   2179 		       SYSCTL_DESCR("Maximum number of vestigial TIME_WAIT entries"),
   2180 		       NULL, 0, &tcp_vtw_entries, 0, CTL_CREATE, CTL_EOL);
   2181 }
   2182 
   2183 void
   2184 tcp_usrreq_init(void)
   2185 {
   2186 
   2187 #ifdef INET
   2188 	sysctl_net_inet_tcp_setup2(NULL, PF_INET, "inet", "tcp");
   2189 #endif
   2190 #ifdef INET6
   2191 	sysctl_net_inet_tcp_setup2(NULL, PF_INET6, "inet6", "tcp6");
   2192 #endif
   2193 }
   2194 
   2195 PR_WRAP_USRREQS(tcp)
   2196 #define	tcp_attach	tcp_attach_wrapper
   2197 #define	tcp_detach	tcp_detach_wrapper
   2198 #define	tcp_ioctl	tcp_ioctl_wrapper
   2199 #define	tcp_stat	tcp_stat_wrapper
   2200 #define	tcp_usrreq	tcp_usrreq_wrapper
   2201 
   2202 const struct pr_usrreqs tcp_usrreqs = {
   2203 	.pr_attach	= tcp_attach,
   2204 	.pr_detach	= tcp_detach,
   2205 	.pr_ioctl	= tcp_ioctl,
   2206 	.pr_stat	= tcp_stat,
   2207 	.pr_generic	= tcp_usrreq,
   2208 };
   2209