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