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