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