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