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