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