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