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