Home | History | Annotate | Line # | Download | only in netinet
tcp_usrreq.c revision 1.204
      1 /*	$NetBSD: tcp_usrreq.c,v 1.204 2015/03/31 08:47:01 ozaki-r Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 1997, 1998, 2005, 2006 The NetBSD Foundation, Inc.
     34  * All rights reserved.
     35  *
     36  * This code is derived from software contributed to The NetBSD Foundation
     37  * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
     38  * Facility, NASA Ames Research Center.
     39  * This code is derived from software contributed to The NetBSD Foundation
     40  * by Charles M. Hannum.
     41  * This code is derived from software contributed to The NetBSD Foundation
     42  * by Rui Paulo.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     54  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     55  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     56  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     57  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     58  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     59  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     60  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     61  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     62  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     63  * POSSIBILITY OF SUCH DAMAGE.
     64  */
     65 
     66 /*
     67  * Copyright (c) 1982, 1986, 1988, 1993, 1995
     68  *	The Regents of the University of California.  All rights reserved.
     69  *
     70  * Redistribution and use in source and binary forms, with or without
     71  * modification, are permitted provided that the following conditions
     72  * are met:
     73  * 1. Redistributions of source code must retain the above copyright
     74  *    notice, this list of conditions and the following disclaimer.
     75  * 2. Redistributions in binary form must reproduce the above copyright
     76  *    notice, this list of conditions and the following disclaimer in the
     77  *    documentation and/or other materials provided with the distribution.
     78  * 3. Neither the name of the University nor the names of its contributors
     79  *    may be used to endorse or promote products derived from this software
     80  *    without specific prior written permission.
     81  *
     82  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     83  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     84  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     85  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     86  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     87  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     88  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     89  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     90  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     91  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     92  * SUCH DAMAGE.
     93  *
     94  *	@(#)tcp_usrreq.c	8.5 (Berkeley) 6/21/95
     95  */
     96 
     97 /*
     98  * TCP protocol interface to socket abstraction.
     99  */
    100 
    101 #include <sys/cdefs.h>
    102 __KERNEL_RCSID(0, "$NetBSD: tcp_usrreq.c,v 1.204 2015/03/31 08:47:01 ozaki-r 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 mbuf *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, nam);
    700 	}
    701 #endif
    702 #ifdef INET6
    703 	if (in6p) {
    704 		in6_setpeeraddr(in6p, 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 mbuf *nam, struct lwp *l)
    715 {
    716 	struct inpcb *inp = NULL;
    717 	struct in6pcb *in6p = NULL;
    718 	struct tcpcb *tp = NULL;
    719 	int s;
    720 	int error = 0;
    721 	int ostate = 0;
    722 
    723 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
    724 		return error;
    725 
    726 	ostate = tcp_debug_capture(tp, PRU_BIND);
    727 
    728 	/*
    729 	 * Give the socket an address.
    730 	 */
    731 	s = splsoftnet();
    732 	switch (so->so_proto->pr_domain->dom_family) {
    733 #ifdef INET
    734 	case PF_INET:
    735 		error = in_pcbbind(inp, nam, l);
    736 		break;
    737 #endif
    738 #ifdef INET6
    739 	case PF_INET6:
    740 		error = in6_pcbbind(in6p, nam, l);
    741 		if (!error) {
    742 			/* mapped addr case */
    743 			if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
    744 				tp->t_family = AF_INET;
    745 			else
    746 				tp->t_family = AF_INET6;
    747 		}
    748 		break;
    749 #endif
    750 	}
    751 	tcp_debug_trace(so, tp, ostate, PRU_BIND);
    752 	splx(s);
    753 
    754 	return error;
    755 }
    756 
    757 static int
    758 tcp_listen(struct socket *so, struct lwp *l)
    759 {
    760 	struct inpcb *inp = NULL;
    761 	struct in6pcb *in6p = NULL;
    762 	struct tcpcb *tp = NULL;
    763 	int error = 0;
    764 	int ostate = 0;
    765 	int s;
    766 
    767 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
    768 		return error;
    769 
    770 	ostate = tcp_debug_capture(tp, PRU_LISTEN);
    771 
    772 	/*
    773 	 * Prepare to accept connections.
    774 	 */
    775 	s = splsoftnet();
    776 #ifdef INET
    777 	if (inp && inp->inp_lport == 0) {
    778 		error = in_pcbbind(inp, NULL, l);
    779 		if (error)
    780 			goto release;
    781 	}
    782 #endif
    783 #ifdef INET6
    784 	if (in6p && in6p->in6p_lport == 0) {
    785 		error = in6_pcbbind(in6p, NULL, l);
    786 		if (error)
    787 			goto release;
    788 	}
    789 #endif
    790 	tp->t_state = TCPS_LISTEN;
    791 
    792 release:
    793 	tcp_debug_trace(so, tp, ostate, PRU_LISTEN);
    794 	splx(s);
    795 
    796 	return error;
    797 }
    798 
    799 static int
    800 tcp_connect(struct socket *so, struct mbuf *nam, struct lwp *l)
    801 {
    802 	struct inpcb *inp = NULL;
    803 	struct in6pcb *in6p = NULL;
    804 	struct tcpcb *tp = NULL;
    805 	int s;
    806 	int error = 0;
    807 	int ostate = 0;
    808 
    809 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
    810 		return error;
    811 
    812 	ostate = tcp_debug_capture(tp, PRU_CONNECT);
    813 
    814 	/*
    815 	 * Initiate connection to peer.
    816 	 * Create a template for use in transmissions on this connection.
    817 	 * Enter SYN_SENT state, and mark socket as connecting.
    818 	 * Start keep-alive timer, and seed output sequence space.
    819 	 * Send initial segment on connection.
    820 	 */
    821 	s = splsoftnet();
    822 #ifdef INET
    823 	if (inp) {
    824 		if (inp->inp_lport == 0) {
    825 			error = in_pcbbind(inp, NULL, l);
    826 			if (error)
    827 				goto release;
    828 		}
    829 		error = in_pcbconnect(inp, nam, l);
    830 	}
    831 #endif
    832 #ifdef INET6
    833 	if (in6p) {
    834 		if (in6p->in6p_lport == 0) {
    835 			error = in6_pcbbind(in6p, NULL, l);
    836 			if (error)
    837 				goto release;
    838 		}
    839 		error = in6_pcbconnect(in6p, nam, l);
    840 		if (!error) {
    841 			/* mapped addr case */
    842 			if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr))
    843 				tp->t_family = AF_INET;
    844 			else
    845 				tp->t_family = AF_INET6;
    846 		}
    847 	}
    848 #endif
    849 	if (error)
    850 		goto release;
    851 	tp->t_template = tcp_template(tp);
    852 	if (tp->t_template == 0) {
    853 #ifdef INET
    854 		if (inp)
    855 			in_pcbdisconnect(inp);
    856 #endif
    857 #ifdef INET6
    858 		if (in6p)
    859 			in6_pcbdisconnect(in6p);
    860 #endif
    861 		error = ENOBUFS;
    862 		goto release;
    863 	}
    864 	/*
    865 	 * Compute window scaling to request.
    866 	 * XXX: This should be moved to tcp_output().
    867 	 */
    868 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
    869 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
    870 		tp->request_r_scale++;
    871 	soisconnecting(so);
    872 	TCP_STATINC(TCP_STAT_CONNATTEMPT);
    873 	tp->t_state = TCPS_SYN_SENT;
    874 	TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepinit);
    875 	tp->iss = tcp_new_iss(tp, 0);
    876 	tcp_sendseqinit(tp);
    877 	error = tcp_output(tp);
    878 
    879 release:
    880 	tcp_debug_trace(so, tp, ostate, PRU_CONNECT);
    881 	splx(s);
    882 
    883 	return error;
    884 }
    885 
    886 static int
    887 tcp_connect2(struct socket *so, struct socket *so2)
    888 {
    889 	struct inpcb *inp = NULL;
    890 	struct in6pcb *in6p = NULL;
    891 	struct tcpcb *tp = NULL;
    892 	int error = 0;
    893 	int ostate = 0;
    894 
    895 	KASSERT(solocked(so));
    896 
    897 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
    898 		return error;
    899 
    900 	ostate = tcp_debug_capture(tp, PRU_CONNECT2);
    901 
    902 	tcp_debug_trace(so, tp, ostate, PRU_CONNECT2);
    903 
    904 	return EOPNOTSUPP;
    905 }
    906 
    907 static int
    908 tcp_disconnect(struct socket *so)
    909 {
    910 	struct inpcb *inp = NULL;
    911 	struct in6pcb *in6p = NULL;
    912 	struct tcpcb *tp = NULL;
    913 	int error = 0;
    914 	int ostate = 0;
    915 	int s;
    916 
    917 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
    918 		return error;
    919 
    920 	ostate = tcp_debug_capture(tp, PRU_DISCONNECT);
    921 
    922 	/*
    923 	 * Initiate disconnect from peer.
    924 	 * If connection never passed embryonic stage, just drop;
    925 	 * else if don't need to let data drain, then can just drop anyways,
    926 	 * else have to begin TCP shutdown process: mark socket disconnecting,
    927 	 * drain unread data, state switch to reflect user close, and
    928 	 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
    929 	 * when peer sends FIN and acks ours.
    930 	 *
    931 	 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
    932 	 */
    933 	s = splsoftnet();
    934 	tp = tcp_disconnect1(tp);
    935 	tcp_debug_trace(so, tp, ostate, PRU_DISCONNECT);
    936 	splx(s);
    937 
    938 	return error;
    939 }
    940 
    941 static int
    942 tcp_shutdown(struct socket *so)
    943 {
    944 	struct inpcb *inp = NULL;
    945 	struct in6pcb *in6p = NULL;
    946 	struct tcpcb *tp = NULL;
    947 	int error = 0;
    948 	int ostate = 0;
    949 	int s;
    950 
    951 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
    952 		return error;
    953 
    954 	ostate = tcp_debug_capture(tp, PRU_SHUTDOWN);
    955 	/*
    956 	 * Mark the connection as being incapable of further output.
    957 	 */
    958 	s = splsoftnet();
    959 	socantsendmore(so);
    960 	tp = tcp_usrclosed(tp);
    961 	if (tp)
    962 		error = tcp_output(tp);
    963 	tcp_debug_trace(so, tp, ostate, PRU_SHUTDOWN);
    964 	splx(s);
    965 
    966 	return error;
    967 }
    968 
    969 static int
    970 tcp_abort(struct socket *so)
    971 {
    972 	struct inpcb *inp = NULL;
    973 	struct in6pcb *in6p = NULL;
    974 	struct tcpcb *tp = NULL;
    975 	int error = 0;
    976 	int ostate = 0;
    977 	int s;
    978 
    979 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
    980 		return error;
    981 
    982 	ostate = tcp_debug_capture(tp, PRU_ABORT);
    983 
    984 	/*
    985 	 * Abort the TCP.
    986 	 */
    987 	s = splsoftnet();
    988 	tp = tcp_drop(tp, ECONNABORTED);
    989 	tcp_debug_trace(so, tp, ostate, PRU_ABORT);
    990 	splx(s);
    991 
    992 	return error;
    993 }
    994 
    995 static int
    996 tcp_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
    997 {
    998 	switch (so->so_proto->pr_domain->dom_family) {
    999 #ifdef INET
   1000 	case PF_INET:
   1001 		return in_control(so, cmd, nam, ifp);
   1002 #endif
   1003 #ifdef INET6
   1004 	case PF_INET6:
   1005 		return in6_control(so, cmd, nam, ifp);
   1006 #endif
   1007 	default:
   1008 		return EAFNOSUPPORT;
   1009 	}
   1010 }
   1011 
   1012 static int
   1013 tcp_stat(struct socket *so, struct stat *ub)
   1014 {
   1015 	KASSERT(solocked(so));
   1016 
   1017 	/* stat: don't bother with a blocksize.  */
   1018 	return 0;
   1019 }
   1020 
   1021 static int
   1022 tcp_peeraddr(struct socket *so, struct mbuf *nam)
   1023 {
   1024 	struct inpcb *inp = NULL;
   1025 	struct in6pcb *in6p = NULL;
   1026 	struct tcpcb *tp = NULL;
   1027 	int ostate = 0;
   1028 	int error = 0;
   1029 	int s;
   1030 
   1031 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
   1032 		return error;
   1033 
   1034 	ostate = tcp_debug_capture(tp, PRU_PEERADDR);
   1035 
   1036 	s = splsoftnet();
   1037 #ifdef INET
   1038 	if (inp)
   1039 		in_setpeeraddr(inp, nam);
   1040 #endif
   1041 #ifdef INET6
   1042 	if (in6p)
   1043 		in6_setpeeraddr(in6p, nam);
   1044 #endif
   1045 	tcp_debug_trace(so, tp, ostate, PRU_PEERADDR);
   1046 	splx(s);
   1047 
   1048 	return 0;
   1049 }
   1050 
   1051 static int
   1052 tcp_sockaddr(struct socket *so, struct mbuf *nam)
   1053 {
   1054 	struct inpcb *inp = NULL;
   1055 	struct in6pcb *in6p = NULL;
   1056 	struct tcpcb *tp = NULL;
   1057 	int ostate = 0;
   1058 	int error = 0;
   1059 	int s;
   1060 
   1061 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
   1062 		return error;
   1063 
   1064 	ostate = tcp_debug_capture(tp, PRU_SOCKADDR);
   1065 
   1066 	s = splsoftnet();
   1067 #ifdef INET
   1068 	if (inp)
   1069 		in_setsockaddr(inp, nam);
   1070 #endif
   1071 #ifdef INET6
   1072 	if (in6p)
   1073 		in6_setsockaddr(in6p, nam);
   1074 #endif
   1075 	tcp_debug_trace(so, tp, ostate, PRU_SOCKADDR);
   1076 	splx(s);
   1077 
   1078 	return 0;
   1079 }
   1080 
   1081 static int
   1082 tcp_rcvd(struct socket *so, int flags, struct lwp *l)
   1083 {
   1084 	struct inpcb *inp = NULL;
   1085 	struct in6pcb *in6p = NULL;
   1086 	struct tcpcb *tp = NULL;
   1087 	int ostate = 0;
   1088 	int error = 0;
   1089 	int s;
   1090 
   1091 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
   1092 		return error;
   1093 
   1094 	ostate = tcp_debug_capture(tp, PRU_RCVD);
   1095 
   1096 	/*
   1097 	 * After a receive, possibly send window update to peer.
   1098 	 *
   1099 	 * soreceive() calls this function when a user receives
   1100 	 * ancillary data on a listening socket. We don't call
   1101 	 * tcp_output in such a case, since there is no header
   1102 	 * template for a listening socket and hence the kernel
   1103 	 * will panic.
   1104 	 */
   1105 	s = splsoftnet();
   1106 	if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) != 0)
   1107 		(void) tcp_output(tp);
   1108 	splx(s);
   1109 
   1110 	tcp_debug_trace(so, tp, ostate, PRU_RCVD);
   1111 
   1112 	return 0;
   1113 }
   1114 
   1115 static int
   1116 tcp_recvoob(struct socket *so, struct mbuf *m, int flags)
   1117 {
   1118 	struct inpcb *inp = NULL;
   1119 	struct in6pcb *in6p = NULL;
   1120 	struct tcpcb *tp = NULL;
   1121 	int ostate = 0;
   1122 	int error = 0;
   1123 	int s;
   1124 
   1125 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
   1126 		return error;
   1127 
   1128 	ostate = tcp_debug_capture(tp, PRU_RCVOOB);
   1129 
   1130 	s = splsoftnet();
   1131 	if ((so->so_oobmark == 0 &&
   1132 	    (so->so_state & SS_RCVATMARK) == 0) ||
   1133 	    so->so_options & SO_OOBINLINE ||
   1134 	    tp->t_oobflags & TCPOOB_HADDATA) {
   1135 		splx(s);
   1136 		return EINVAL;
   1137 	}
   1138 
   1139 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
   1140 		splx(s);
   1141 		return EWOULDBLOCK;
   1142 	}
   1143 
   1144 	m->m_len = 1;
   1145 	*mtod(m, char *) = tp->t_iobc;
   1146 	if ((flags & MSG_PEEK) == 0)
   1147 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
   1148 
   1149 	tcp_debug_trace(so, tp, ostate, PRU_RCVOOB);
   1150 	splx(s);
   1151 
   1152 	return 0;
   1153 }
   1154 
   1155 static int
   1156 tcp_send(struct socket *so, struct mbuf *m, struct mbuf *nam,
   1157     struct mbuf *control, struct lwp *l)
   1158 {
   1159 	struct inpcb *inp = NULL;
   1160 	struct in6pcb *in6p = NULL;
   1161 	struct tcpcb *tp = NULL;
   1162 	int ostate = 0;
   1163 	int error = 0;
   1164 	int s;
   1165 
   1166 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
   1167 		return error;
   1168 
   1169 	ostate = tcp_debug_capture(tp, PRU_SEND);
   1170 
   1171 	/*
   1172 	 * Do a send by putting data in output queue and updating urgent
   1173 	 * marker if URG set.  Possibly send more data.
   1174 	 */
   1175 	s = splsoftnet();
   1176 	if (control && control->m_len) {
   1177 		m_freem(control);
   1178 		m_freem(m);
   1179 		tcp_debug_trace(so, tp, ostate, PRU_SEND);
   1180 		splx(s);
   1181 		return EINVAL;
   1182 	}
   1183 
   1184 	sbappendstream(&so->so_snd, m);
   1185 	error = tcp_output(tp);
   1186 	tcp_debug_trace(so, tp, ostate, PRU_SEND);
   1187 	splx(s);
   1188 
   1189 	return error;
   1190 }
   1191 
   1192 static int
   1193 tcp_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
   1194 {
   1195 	struct inpcb *inp = NULL;
   1196 	struct in6pcb *in6p = NULL;
   1197 	struct tcpcb *tp = NULL;
   1198 	int ostate = 0;
   1199 	int error = 0;
   1200 	int s;
   1201 
   1202 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
   1203 		return error;
   1204 
   1205 	ostate = tcp_debug_capture(tp, PRU_SENDOOB);
   1206 
   1207 	s = splsoftnet();
   1208 	if (sbspace(&so->so_snd) < -512) {
   1209 		m_freem(m);
   1210 		splx(s);
   1211 		return ENOBUFS;
   1212 	}
   1213 	/*
   1214 	 * According to RFC961 (Assigned Protocols),
   1215 	 * the urgent pointer points to the last octet
   1216 	 * of urgent data.  We continue, however,
   1217 	 * to consider it to indicate the first octet
   1218 	 * of data past the urgent section.
   1219 	 * Otherwise, snd_up should be one lower.
   1220 	 */
   1221 	sbappendstream(&so->so_snd, m);
   1222 	tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
   1223 	tp->t_force = 1;
   1224 	error = tcp_output(tp);
   1225 	tp->t_force = 0;
   1226 	tcp_debug_trace(so, tp, ostate, PRU_SENDOOB);
   1227 	splx(s);
   1228 
   1229 	return error;
   1230 }
   1231 
   1232 static int
   1233 tcp_purgeif(struct socket *so, struct ifnet *ifp)
   1234 {
   1235 	int s;
   1236 
   1237 	s = splsoftnet();
   1238 	mutex_enter(softnet_lock);
   1239 	switch (so->so_proto->pr_domain->dom_family) {
   1240 #ifdef INET
   1241 	case PF_INET:
   1242 		in_pcbpurgeif0(&tcbtable, ifp);
   1243 		in_purgeif(ifp);
   1244 		in_pcbpurgeif(&tcbtable, ifp);
   1245 		break;
   1246 #endif
   1247 #ifdef INET6
   1248 	case PF_INET6:
   1249 		in6_pcbpurgeif0(&tcbtable, ifp);
   1250 		in6_purgeif(ifp);
   1251 		in6_pcbpurgeif(&tcbtable, ifp);
   1252 		break;
   1253 #endif
   1254 	default:
   1255 		mutex_exit(softnet_lock);
   1256 		splx(s);
   1257 		return EAFNOSUPPORT;
   1258 	}
   1259 	mutex_exit(softnet_lock);
   1260 	splx(s);
   1261 
   1262 	return 0;
   1263 }
   1264 
   1265 /*
   1266  * Initiate (or continue) disconnect.
   1267  * If embryonic state, just send reset (once).
   1268  * If in ``let data drain'' option and linger null, just drop.
   1269  * Otherwise (hard), mark socket disconnecting and drop
   1270  * current input data; switch states based on user close, and
   1271  * send segment to peer (with FIN).
   1272  */
   1273 struct tcpcb *
   1274 tcp_disconnect1(struct tcpcb *tp)
   1275 {
   1276 	struct socket *so;
   1277 
   1278 	if (tp->t_inpcb)
   1279 		so = tp->t_inpcb->inp_socket;
   1280 #ifdef INET6
   1281 	else if (tp->t_in6pcb)
   1282 		so = tp->t_in6pcb->in6p_socket;
   1283 #endif
   1284 	else
   1285 		so = NULL;
   1286 
   1287 	if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
   1288 		tp = tcp_close(tp);
   1289 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
   1290 		tp = tcp_drop(tp, 0);
   1291 	else {
   1292 		soisdisconnecting(so);
   1293 		sbflush(&so->so_rcv);
   1294 		tp = tcp_usrclosed(tp);
   1295 		if (tp)
   1296 			(void) tcp_output(tp);
   1297 	}
   1298 	return (tp);
   1299 }
   1300 
   1301 /*
   1302  * User issued close, and wish to trail through shutdown states:
   1303  * if never received SYN, just forget it.  If got a SYN from peer,
   1304  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
   1305  * If already got a FIN from peer, then almost done; go to LAST_ACK
   1306  * state.  In all other cases, have already sent FIN to peer (e.g.
   1307  * after PRU_SHUTDOWN), and just have to play tedious game waiting
   1308  * for peer to send FIN or not respond to keep-alives, etc.
   1309  * We can let the user exit from the close as soon as the FIN is acked.
   1310  */
   1311 struct tcpcb *
   1312 tcp_usrclosed(struct tcpcb *tp)
   1313 {
   1314 
   1315 	switch (tp->t_state) {
   1316 
   1317 	case TCPS_CLOSED:
   1318 	case TCPS_LISTEN:
   1319 	case TCPS_SYN_SENT:
   1320 		tp->t_state = TCPS_CLOSED;
   1321 		tp = tcp_close(tp);
   1322 		break;
   1323 
   1324 	case TCPS_SYN_RECEIVED:
   1325 	case TCPS_ESTABLISHED:
   1326 		tp->t_state = TCPS_FIN_WAIT_1;
   1327 		break;
   1328 
   1329 	case TCPS_CLOSE_WAIT:
   1330 		tp->t_state = TCPS_LAST_ACK;
   1331 		break;
   1332 	}
   1333 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
   1334 		struct socket *so;
   1335 		if (tp->t_inpcb)
   1336 			so = tp->t_inpcb->inp_socket;
   1337 #ifdef INET6
   1338 		else if (tp->t_in6pcb)
   1339 			so = tp->t_in6pcb->in6p_socket;
   1340 #endif
   1341 		else
   1342 			so = NULL;
   1343 		if (so)
   1344 			soisdisconnected(so);
   1345 		/*
   1346 		 * If we are in FIN_WAIT_2, we arrived here because the
   1347 		 * application did a shutdown of the send side.  Like the
   1348 		 * case of a transition from FIN_WAIT_1 to FIN_WAIT_2 after
   1349 		 * a full close, we start a timer to make sure sockets are
   1350 		 * not left in FIN_WAIT_2 forever.
   1351 		 */
   1352 		if ((tp->t_state == TCPS_FIN_WAIT_2) && (tp->t_maxidle > 0))
   1353 			TCP_TIMER_ARM(tp, TCPT_2MSL, tp->t_maxidle);
   1354 		else if (tp->t_state == TCPS_TIME_WAIT
   1355 			 && ((tp->t_inpcb
   1356 			      && (tcp4_vtw_enable & 1)
   1357 			      && vtw_add(AF_INET, tp))
   1358 			     ||
   1359 			     (tp->t_in6pcb
   1360 			      && (tcp6_vtw_enable & 1)
   1361 			      && vtw_add(AF_INET6, tp)))) {
   1362 			tp = 0;
   1363 		}
   1364 	}
   1365 	return (tp);
   1366 }
   1367 
   1368 /*
   1369  * sysctl helper routine for net.inet.ip.mssdflt.  it can't be less
   1370  * than 32.
   1371  */
   1372 static int
   1373 sysctl_net_inet_tcp_mssdflt(SYSCTLFN_ARGS)
   1374 {
   1375 	int error, mssdflt;
   1376 	struct sysctlnode node;
   1377 
   1378 	mssdflt = tcp_mssdflt;
   1379 	node = *rnode;
   1380 	node.sysctl_data = &mssdflt;
   1381 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1382 	if (error || newp == NULL)
   1383 		return (error);
   1384 
   1385 	if (mssdflt < 32)
   1386 		return (EINVAL);
   1387 	tcp_mssdflt = mssdflt;
   1388 
   1389 	mutex_enter(softnet_lock);
   1390 	tcp_tcpcb_template();
   1391 	mutex_exit(softnet_lock);
   1392 
   1393 	return (0);
   1394 }
   1395 
   1396 /*
   1397  * sysctl helper for TCP CB template update
   1398  */
   1399 static int
   1400 sysctl_update_tcpcb_template(SYSCTLFN_ARGS)
   1401 {
   1402 	int t, error;
   1403 	struct sysctlnode node;
   1404 
   1405 	/* follow procedures in sysctl(9) manpage */
   1406 	t = *(int *)rnode->sysctl_data;
   1407 	node = *rnode;
   1408 	node.sysctl_data = &t;
   1409 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1410 	if (error || newp == NULL)
   1411 		return error;
   1412 
   1413 	if (t < 0)
   1414 		return EINVAL;
   1415 
   1416 	*(int *)rnode->sysctl_data = t;
   1417 
   1418 	mutex_enter(softnet_lock);
   1419 	tcp_tcpcb_template();
   1420 	mutex_exit(softnet_lock);
   1421 
   1422 	return 0;
   1423 }
   1424 
   1425 /*
   1426  * sysctl helper routine for setting port related values under
   1427  * net.inet.ip and net.inet6.ip6.  does basic range checking and does
   1428  * additional checks for each type.  this code has placed in
   1429  * tcp_input.c since INET and INET6 both use the same tcp code.
   1430  *
   1431  * this helper is not static so that both inet and inet6 can use it.
   1432  */
   1433 int
   1434 sysctl_net_inet_ip_ports(SYSCTLFN_ARGS)
   1435 {
   1436 	int error, tmp;
   1437 	int apmin, apmax;
   1438 #ifndef IPNOPRIVPORTS
   1439 	int lpmin, lpmax;
   1440 #endif /* IPNOPRIVPORTS */
   1441 	struct sysctlnode node;
   1442 
   1443 	if (namelen != 0)
   1444 		return (EINVAL);
   1445 
   1446 	switch (name[-3]) {
   1447 #ifdef INET
   1448 	    case PF_INET:
   1449 		apmin = anonportmin;
   1450 		apmax = anonportmax;
   1451 #ifndef IPNOPRIVPORTS
   1452 		lpmin = lowportmin;
   1453 		lpmax = lowportmax;
   1454 #endif /* IPNOPRIVPORTS */
   1455 		break;
   1456 #endif /* INET */
   1457 #ifdef INET6
   1458 	    case PF_INET6:
   1459 		apmin = ip6_anonportmin;
   1460 		apmax = ip6_anonportmax;
   1461 #ifndef IPNOPRIVPORTS
   1462 		lpmin = ip6_lowportmin;
   1463 		lpmax = ip6_lowportmax;
   1464 #endif /* IPNOPRIVPORTS */
   1465 		break;
   1466 #endif /* INET6 */
   1467 	    default:
   1468 		return (EINVAL);
   1469 	}
   1470 
   1471 	/*
   1472 	 * insert temporary copy into node, perform lookup on
   1473 	 * temporary, then restore pointer
   1474 	 */
   1475 	node = *rnode;
   1476 	tmp = *(int*)rnode->sysctl_data;
   1477 	node.sysctl_data = &tmp;
   1478 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1479 	if (error || newp == NULL)
   1480 		return (error);
   1481 
   1482 	/*
   1483 	 * simple port range check
   1484 	 */
   1485 	if (tmp < 0 || tmp > 65535)
   1486 		return (EINVAL);
   1487 
   1488 	/*
   1489 	 * per-node range checks
   1490 	 */
   1491 	switch (rnode->sysctl_num) {
   1492 	case IPCTL_ANONPORTMIN:
   1493 	case IPV6CTL_ANONPORTMIN:
   1494 		if (tmp >= apmax)
   1495 			return (EINVAL);
   1496 #ifndef IPNOPRIVPORTS
   1497 		if (tmp < IPPORT_RESERVED)
   1498                         return (EINVAL);
   1499 #endif /* IPNOPRIVPORTS */
   1500 		break;
   1501 
   1502 	case IPCTL_ANONPORTMAX:
   1503 	case IPV6CTL_ANONPORTMAX:
   1504                 if (apmin >= tmp)
   1505 			return (EINVAL);
   1506 #ifndef IPNOPRIVPORTS
   1507 		if (tmp < IPPORT_RESERVED)
   1508                         return (EINVAL);
   1509 #endif /* IPNOPRIVPORTS */
   1510 		break;
   1511 
   1512 #ifndef IPNOPRIVPORTS
   1513 	case IPCTL_LOWPORTMIN:
   1514 	case IPV6CTL_LOWPORTMIN:
   1515 		if (tmp >= lpmax ||
   1516 		    tmp > IPPORT_RESERVEDMAX ||
   1517 		    tmp < IPPORT_RESERVEDMIN)
   1518 			return (EINVAL);
   1519 		break;
   1520 
   1521 	case IPCTL_LOWPORTMAX:
   1522 	case IPV6CTL_LOWPORTMAX:
   1523 		if (lpmin >= tmp ||
   1524 		    tmp > IPPORT_RESERVEDMAX ||
   1525 		    tmp < IPPORT_RESERVEDMIN)
   1526 			return (EINVAL);
   1527 		break;
   1528 #endif /* IPNOPRIVPORTS */
   1529 
   1530 	default:
   1531 		return (EINVAL);
   1532 	}
   1533 
   1534 	*(int*)rnode->sysctl_data = tmp;
   1535 
   1536 	return (0);
   1537 }
   1538 
   1539 static inline int
   1540 copyout_uid(struct socket *sockp, void *oldp, size_t *oldlenp)
   1541 {
   1542 	if (oldp) {
   1543 		size_t sz;
   1544 		uid_t uid;
   1545 		int error;
   1546 
   1547 		if (sockp->so_cred == NULL)
   1548 			return EPERM;
   1549 
   1550 		uid = kauth_cred_geteuid(sockp->so_cred);
   1551 		sz = MIN(sizeof(uid), *oldlenp);
   1552 		if ((error = copyout(&uid, oldp, sz)) != 0)
   1553 			return error;
   1554 	}
   1555 	*oldlenp = sizeof(uid_t);
   1556 	return 0;
   1557 }
   1558 
   1559 static inline int
   1560 inet4_ident_core(struct in_addr raddr, u_int rport,
   1561     struct in_addr laddr, u_int lport,
   1562     void *oldp, size_t *oldlenp,
   1563     struct lwp *l, int dodrop)
   1564 {
   1565 	struct inpcb *inp;
   1566 	struct socket *sockp;
   1567 
   1568 	inp = in_pcblookup_connect(&tcbtable, raddr, rport, laddr, lport, 0);
   1569 
   1570 	if (inp == NULL || (sockp = inp->inp_socket) == NULL)
   1571 		return ESRCH;
   1572 
   1573 	if (dodrop) {
   1574 		struct tcpcb *tp;
   1575 		int error;
   1576 
   1577 		if (inp == NULL || (tp = intotcpcb(inp)) == NULL ||
   1578 		    (inp->inp_socket->so_options & SO_ACCEPTCONN) != 0)
   1579 			return ESRCH;
   1580 
   1581 		error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
   1582 		    KAUTH_REQ_NETWORK_SOCKET_DROP, inp->inp_socket, tp, NULL);
   1583 		if (error)
   1584 			return (error);
   1585 
   1586 		(void)tcp_drop(tp, ECONNABORTED);
   1587 		return 0;
   1588 	}
   1589 	else
   1590 		return copyout_uid(sockp, oldp, oldlenp);
   1591 }
   1592 
   1593 #ifdef INET6
   1594 static inline int
   1595 inet6_ident_core(struct in6_addr *raddr, u_int rport,
   1596     struct in6_addr *laddr, u_int lport,
   1597     void *oldp, size_t *oldlenp,
   1598     struct lwp *l, int dodrop)
   1599 {
   1600 	struct in6pcb *in6p;
   1601 	struct socket *sockp;
   1602 
   1603 	in6p = in6_pcblookup_connect(&tcbtable, raddr, rport, laddr, lport, 0, 0);
   1604 
   1605 	if (in6p == NULL || (sockp = in6p->in6p_socket) == NULL)
   1606 		return ESRCH;
   1607 
   1608 	if (dodrop) {
   1609 		struct tcpcb *tp;
   1610 		int error;
   1611 
   1612 		if (in6p == NULL || (tp = in6totcpcb(in6p)) == NULL ||
   1613 		    (in6p->in6p_socket->so_options & SO_ACCEPTCONN) != 0)
   1614 			return ESRCH;
   1615 
   1616 		error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
   1617 		    KAUTH_REQ_NETWORK_SOCKET_DROP, in6p->in6p_socket, tp, NULL);
   1618 		if (error)
   1619 			return (error);
   1620 
   1621 		(void)tcp_drop(tp, ECONNABORTED);
   1622 		return 0;
   1623 	}
   1624 	else
   1625 		return copyout_uid(sockp, oldp, oldlenp);
   1626 }
   1627 #endif
   1628 
   1629 /*
   1630  * sysctl helper routine for the net.inet.tcp.drop and
   1631  * net.inet6.tcp6.drop nodes.
   1632  */
   1633 #define sysctl_net_inet_tcp_drop sysctl_net_inet_tcp_ident
   1634 
   1635 /*
   1636  * sysctl helper routine for the net.inet.tcp.ident and
   1637  * net.inet6.tcp6.ident nodes.  contains backwards compat code for the
   1638  * old way of looking up the ident information for ipv4 which involves
   1639  * stuffing the port/addr pairs into the mib lookup.
   1640  */
   1641 static int
   1642 sysctl_net_inet_tcp_ident(SYSCTLFN_ARGS)
   1643 {
   1644 #ifdef INET
   1645 	struct sockaddr_in *si4[2];
   1646 #endif /* INET */
   1647 #ifdef INET6
   1648 	struct sockaddr_in6 *si6[2];
   1649 #endif /* INET6 */
   1650 	struct sockaddr_storage sa[2];
   1651 	int error, pf, dodrop;
   1652 
   1653 	dodrop = name[-1] == TCPCTL_DROP;
   1654 	if (dodrop) {
   1655 		if (oldp != NULL || *oldlenp != 0)
   1656 			return EINVAL;
   1657 		if (newp == NULL)
   1658 			return EPERM;
   1659 		if (newlen < sizeof(sa))
   1660 			return ENOMEM;
   1661 	}
   1662 	if (namelen != 4 && namelen != 0)
   1663 		return EINVAL;
   1664 	if (name[-2] != IPPROTO_TCP)
   1665 		return EINVAL;
   1666 	pf = name[-3];
   1667 
   1668 	/* old style lookup, ipv4 only */
   1669 	if (namelen == 4) {
   1670 #ifdef INET
   1671 		struct in_addr laddr, raddr;
   1672 		u_int lport, rport;
   1673 
   1674 		if (pf != PF_INET)
   1675 			return EPROTONOSUPPORT;
   1676 		raddr.s_addr = (uint32_t)name[0];
   1677 		rport = (u_int)name[1];
   1678 		laddr.s_addr = (uint32_t)name[2];
   1679 		lport = (u_int)name[3];
   1680 
   1681 		mutex_enter(softnet_lock);
   1682 		error = inet4_ident_core(raddr, rport, laddr, lport,
   1683 		    oldp, oldlenp, l, dodrop);
   1684 		mutex_exit(softnet_lock);
   1685 		return error;
   1686 #else /* INET */
   1687 		return EINVAL;
   1688 #endif /* INET */
   1689 	}
   1690 
   1691 	if (newp == NULL || newlen != sizeof(sa))
   1692 		return EINVAL;
   1693 	error = copyin(newp, &sa, newlen);
   1694 	if (error)
   1695 		return error;
   1696 
   1697 	/*
   1698 	 * requested families must match
   1699 	 */
   1700 	if (pf != sa[0].ss_family || sa[0].ss_family != sa[1].ss_family)
   1701 		return EINVAL;
   1702 
   1703 	switch (pf) {
   1704 #ifdef INET6
   1705 	case PF_INET6:
   1706 		si6[0] = (struct sockaddr_in6*)&sa[0];
   1707 		si6[1] = (struct sockaddr_in6*)&sa[1];
   1708 		if (si6[0]->sin6_len != sizeof(*si6[0]) ||
   1709 		    si6[1]->sin6_len != sizeof(*si6[1]))
   1710 			return EINVAL;
   1711 
   1712 		if (!IN6_IS_ADDR_V4MAPPED(&si6[0]->sin6_addr) &&
   1713 		    !IN6_IS_ADDR_V4MAPPED(&si6[1]->sin6_addr)) {
   1714 			error = sa6_embedscope(si6[0], ip6_use_defzone);
   1715 			if (error)
   1716 				return error;
   1717 			error = sa6_embedscope(si6[1], ip6_use_defzone);
   1718 			if (error)
   1719 				return error;
   1720 
   1721 			mutex_enter(softnet_lock);
   1722 			error = inet6_ident_core(&si6[0]->sin6_addr,
   1723 			    si6[0]->sin6_port, &si6[1]->sin6_addr,
   1724 			    si6[1]->sin6_port, oldp, oldlenp, l, dodrop);
   1725 			mutex_exit(softnet_lock);
   1726 			return error;
   1727 		}
   1728 
   1729 		if (IN6_IS_ADDR_V4MAPPED(&si6[0]->sin6_addr) !=
   1730 		    IN6_IS_ADDR_V4MAPPED(&si6[1]->sin6_addr))
   1731 			return EINVAL;
   1732 
   1733 		in6_sin6_2_sin_in_sock((struct sockaddr *)&sa[0]);
   1734 		in6_sin6_2_sin_in_sock((struct sockaddr *)&sa[1]);
   1735 		/*FALLTHROUGH*/
   1736 #endif /* INET6 */
   1737 #ifdef INET
   1738 	case PF_INET:
   1739 		si4[0] = (struct sockaddr_in*)&sa[0];
   1740 		si4[1] = (struct sockaddr_in*)&sa[1];
   1741 		if (si4[0]->sin_len != sizeof(*si4[0]) ||
   1742 		    si4[0]->sin_len != sizeof(*si4[1]))
   1743 			return EINVAL;
   1744 
   1745 		mutex_enter(softnet_lock);
   1746 		error = inet4_ident_core(si4[0]->sin_addr, si4[0]->sin_port,
   1747 		    si4[1]->sin_addr, si4[1]->sin_port,
   1748 		    oldp, oldlenp, l, dodrop);
   1749 		mutex_exit(softnet_lock);
   1750 		return error;
   1751 #endif /* INET */
   1752 	default:
   1753 		return EPROTONOSUPPORT;
   1754 	}
   1755 }
   1756 
   1757 /*
   1758  * sysctl helper for the inet and inet6 pcblists.  handles tcp/udp and
   1759  * inet/inet6, as well as raw pcbs for each.  specifically not
   1760  * declared static so that raw sockets and udp/udp6 can use it as
   1761  * well.
   1762  */
   1763 int
   1764 sysctl_inpcblist(SYSCTLFN_ARGS)
   1765 {
   1766 #ifdef INET
   1767 	struct sockaddr_in *in;
   1768 	const struct inpcb *inp;
   1769 #endif
   1770 #ifdef INET6
   1771 	struct sockaddr_in6 *in6;
   1772 	const struct in6pcb *in6p;
   1773 #endif
   1774 	struct inpcbtable *pcbtbl = __UNCONST(rnode->sysctl_data);
   1775 	const struct inpcb_hdr *inph;
   1776 	struct tcpcb *tp;
   1777 	struct kinfo_pcb pcb;
   1778 	char *dp;
   1779 	size_t len, needed, elem_size, out_size;
   1780 	int error, elem_count, pf, proto, pf2;
   1781 
   1782 	if (namelen != 4)
   1783 		return (EINVAL);
   1784 
   1785 	if (oldp != NULL) {
   1786 		    len = *oldlenp;
   1787 		    elem_size = name[2];
   1788 		    elem_count = name[3];
   1789 		    if (elem_size != sizeof(pcb))
   1790 			    return EINVAL;
   1791 	} else {
   1792 		    len = 0;
   1793 		    elem_count = INT_MAX;
   1794 		    elem_size = sizeof(pcb);
   1795 	}
   1796 	error = 0;
   1797 	dp = oldp;
   1798 	out_size = elem_size;
   1799 	needed = 0;
   1800 
   1801 	if (namelen == 1 && name[0] == CTL_QUERY)
   1802 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   1803 
   1804 	if (name - oname != 4)
   1805 		return (EINVAL);
   1806 
   1807 	pf = oname[1];
   1808 	proto = oname[2];
   1809 	pf2 = (oldp != NULL) ? pf : 0;
   1810 
   1811 	mutex_enter(softnet_lock);
   1812 
   1813 	TAILQ_FOREACH(inph, &pcbtbl->inpt_queue, inph_queue) {
   1814 #ifdef INET
   1815 		inp = (const struct inpcb *)inph;
   1816 #endif
   1817 #ifdef INET6
   1818 		in6p = (const struct in6pcb *)inph;
   1819 #endif
   1820 
   1821 		if (inph->inph_af != pf)
   1822 			continue;
   1823 
   1824 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
   1825 		    KAUTH_REQ_NETWORK_SOCKET_CANSEE, inph->inph_socket, NULL,
   1826 		    NULL) != 0)
   1827 			continue;
   1828 
   1829 		memset(&pcb, 0, sizeof(pcb));
   1830 
   1831 		pcb.ki_family = pf;
   1832 		pcb.ki_type = proto;
   1833 
   1834 		switch (pf2) {
   1835 		case 0:
   1836 			/* just probing for size */
   1837 			break;
   1838 #ifdef INET
   1839 		case PF_INET:
   1840 			pcb.ki_family = inp->inp_socket->so_proto->
   1841 			    pr_domain->dom_family;
   1842 			pcb.ki_type = inp->inp_socket->so_proto->
   1843 			    pr_type;
   1844 			pcb.ki_protocol = inp->inp_socket->so_proto->
   1845 			    pr_protocol;
   1846 			pcb.ki_pflags = inp->inp_flags;
   1847 
   1848 			pcb.ki_sostate = inp->inp_socket->so_state;
   1849 			pcb.ki_prstate = inp->inp_state;
   1850 			if (proto == IPPROTO_TCP) {
   1851 				tp = intotcpcb(inp);
   1852 				pcb.ki_tstate = tp->t_state;
   1853 				pcb.ki_tflags = tp->t_flags;
   1854 			}
   1855 
   1856 			pcb.ki_pcbaddr = PTRTOUINT64(inp);
   1857 			pcb.ki_ppcbaddr = PTRTOUINT64(inp->inp_ppcb);
   1858 			pcb.ki_sockaddr = PTRTOUINT64(inp->inp_socket);
   1859 
   1860 			pcb.ki_rcvq = inp->inp_socket->so_rcv.sb_cc;
   1861 			pcb.ki_sndq = inp->inp_socket->so_snd.sb_cc;
   1862 
   1863 			in = satosin(&pcb.ki_src);
   1864 			in->sin_len = sizeof(*in);
   1865 			in->sin_family = pf;
   1866 			in->sin_port = inp->inp_lport;
   1867 			in->sin_addr = inp->inp_laddr;
   1868 			if (pcb.ki_prstate >= INP_CONNECTED) {
   1869 				in = satosin(&pcb.ki_dst);
   1870 				in->sin_len = sizeof(*in);
   1871 				in->sin_family = pf;
   1872 				in->sin_port = inp->inp_fport;
   1873 				in->sin_addr = inp->inp_faddr;
   1874 			}
   1875 			break;
   1876 #endif
   1877 #ifdef INET6
   1878 		case PF_INET6:
   1879 			pcb.ki_family = in6p->in6p_socket->so_proto->
   1880 			    pr_domain->dom_family;
   1881 			pcb.ki_type = in6p->in6p_socket->so_proto->pr_type;
   1882 			pcb.ki_protocol = in6p->in6p_socket->so_proto->
   1883 			    pr_protocol;
   1884 			pcb.ki_pflags = in6p->in6p_flags;
   1885 
   1886 			pcb.ki_sostate = in6p->in6p_socket->so_state;
   1887 			pcb.ki_prstate = in6p->in6p_state;
   1888 			if (proto == IPPROTO_TCP) {
   1889 				tp = in6totcpcb(in6p);
   1890 				pcb.ki_tstate = tp->t_state;
   1891 				pcb.ki_tflags = tp->t_flags;
   1892 			}
   1893 
   1894 			pcb.ki_pcbaddr = PTRTOUINT64(in6p);
   1895 			pcb.ki_ppcbaddr = PTRTOUINT64(in6p->in6p_ppcb);
   1896 			pcb.ki_sockaddr = PTRTOUINT64(in6p->in6p_socket);
   1897 
   1898 			pcb.ki_rcvq = in6p->in6p_socket->so_rcv.sb_cc;
   1899 			pcb.ki_sndq = in6p->in6p_socket->so_snd.sb_cc;
   1900 
   1901 			in6 = satosin6(&pcb.ki_src);
   1902 			in6->sin6_len = sizeof(*in6);
   1903 			in6->sin6_family = pf;
   1904 			in6->sin6_port = in6p->in6p_lport;
   1905 			in6->sin6_flowinfo = in6p->in6p_flowinfo;
   1906 			in6->sin6_addr = in6p->in6p_laddr;
   1907 			in6->sin6_scope_id = 0; /* XXX? */
   1908 
   1909 			if (pcb.ki_prstate >= IN6P_CONNECTED) {
   1910 				in6 = satosin6(&pcb.ki_dst);
   1911 				in6->sin6_len = sizeof(*in6);
   1912 				in6->sin6_family = pf;
   1913 				in6->sin6_port = in6p->in6p_fport;
   1914 				in6->sin6_flowinfo = in6p->in6p_flowinfo;
   1915 				in6->sin6_addr = in6p->in6p_faddr;
   1916 				in6->sin6_scope_id = 0; /* XXX? */
   1917 			}
   1918 			break;
   1919 #endif
   1920 		}
   1921 
   1922 		if (len >= elem_size && elem_count > 0) {
   1923 			error = copyout(&pcb, dp, out_size);
   1924 			if (error) {
   1925 				mutex_exit(softnet_lock);
   1926 				return (error);
   1927 			}
   1928 			dp += elem_size;
   1929 			len -= elem_size;
   1930 		}
   1931 		needed += elem_size;
   1932 		if (elem_count > 0 && elem_count != INT_MAX)
   1933 			elem_count--;
   1934 	}
   1935 
   1936 	*oldlenp = needed;
   1937 	if (oldp == NULL)
   1938 		*oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb);
   1939 
   1940 	mutex_exit(softnet_lock);
   1941 
   1942 	return (error);
   1943 }
   1944 
   1945 static int
   1946 sysctl_tcp_congctl(SYSCTLFN_ARGS)
   1947 {
   1948 	struct sysctlnode node;
   1949 	int error;
   1950 	char newname[TCPCC_MAXLEN];
   1951 
   1952 	strlcpy(newname, tcp_congctl_global_name, sizeof(newname) - 1);
   1953 
   1954 	node = *rnode;
   1955 	node.sysctl_data = newname;
   1956 	node.sysctl_size = sizeof(newname);
   1957 
   1958 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1959 
   1960 	if (error ||
   1961 	    newp == NULL ||
   1962 	    strncmp(newname, tcp_congctl_global_name, sizeof(newname)) == 0)
   1963 		return error;
   1964 
   1965 	mutex_enter(softnet_lock);
   1966 	error = tcp_congctl_select(NULL, newname);
   1967 	mutex_exit(softnet_lock);
   1968 
   1969 	return error;
   1970 }
   1971 
   1972 static int
   1973 sysctl_tcp_init_win(SYSCTLFN_ARGS)
   1974 {
   1975 	int error;
   1976 	u_int iw;
   1977 	struct sysctlnode node;
   1978 
   1979 	iw = *(u_int *)rnode->sysctl_data;
   1980 	node = *rnode;
   1981 	node.sysctl_data = &iw;
   1982 	node.sysctl_size = sizeof(iw);
   1983 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1984 	if (error || newp == NULL)
   1985 		return error;
   1986 
   1987 	if (iw >= __arraycount(tcp_init_win_max))
   1988 		return EINVAL;
   1989 	*(u_int *)rnode->sysctl_data = iw;
   1990 	return 0;
   1991 }
   1992 
   1993 static int
   1994 sysctl_tcp_keep(SYSCTLFN_ARGS)
   1995 {
   1996 	int error;
   1997 	u_int tmp;
   1998 	struct sysctlnode node;
   1999 
   2000 	node = *rnode;
   2001 	tmp = *(u_int *)rnode->sysctl_data;
   2002 	node.sysctl_data = &tmp;
   2003 
   2004 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2005 	if (error || newp == NULL)
   2006 		return error;
   2007 
   2008 	mutex_enter(softnet_lock);
   2009 
   2010 	*(u_int *)rnode->sysctl_data = tmp;
   2011 	tcp_tcpcb_template();	/* update the template */
   2012 
   2013 	mutex_exit(softnet_lock);
   2014 	return 0;
   2015 }
   2016 
   2017 static int
   2018 sysctl_net_inet_tcp_stats(SYSCTLFN_ARGS)
   2019 {
   2020 
   2021 	return (NETSTAT_SYSCTL(tcpstat_percpu, TCP_NSTATS));
   2022 }
   2023 
   2024 /*
   2025  * this (second stage) setup routine is a replacement for tcp_sysctl()
   2026  * (which is currently used for ipv4 and ipv6)
   2027  */
   2028 static void
   2029 sysctl_net_inet_tcp_setup2(struct sysctllog **clog, int pf, const char *pfname,
   2030 			   const char *tcpname)
   2031 {
   2032 	const struct sysctlnode *sack_node;
   2033 	const struct sysctlnode *abc_node;
   2034 	const struct sysctlnode *ecn_node;
   2035 	const struct sysctlnode *congctl_node;
   2036 	const struct sysctlnode *mslt_node;
   2037 	const struct sysctlnode *vtw_node;
   2038 #ifdef TCP_DEBUG
   2039 	extern struct tcp_debug tcp_debug[TCP_NDEBUG];
   2040 	extern int tcp_debx;
   2041 #endif
   2042 
   2043 	sysctl_createv(clog, 0, NULL, NULL,
   2044 		       CTLFLAG_PERMANENT,
   2045 		       CTLTYPE_NODE, pfname, NULL,
   2046 		       NULL, 0, NULL, 0,
   2047 		       CTL_NET, pf, CTL_EOL);
   2048 	sysctl_createv(clog, 0, NULL, NULL,
   2049 		       CTLFLAG_PERMANENT,
   2050 		       CTLTYPE_NODE, tcpname,
   2051 		       SYSCTL_DESCR("TCP related settings"),
   2052 		       NULL, 0, NULL, 0,
   2053 		       CTL_NET, pf, IPPROTO_TCP, CTL_EOL);
   2054 
   2055 	sysctl_createv(clog, 0, NULL, NULL,
   2056 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2057 		       CTLTYPE_INT, "rfc1323",
   2058 		       SYSCTL_DESCR("Enable RFC1323 TCP extensions"),
   2059 		       sysctl_update_tcpcb_template, 0, &tcp_do_rfc1323, 0,
   2060 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RFC1323, CTL_EOL);
   2061 	sysctl_createv(clog, 0, NULL, NULL,
   2062 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2063 		       CTLTYPE_INT, "sendspace",
   2064 		       SYSCTL_DESCR("Default TCP send buffer size"),
   2065 		       NULL, 0, &tcp_sendspace, 0,
   2066 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SENDSPACE, CTL_EOL);
   2067 	sysctl_createv(clog, 0, NULL, NULL,
   2068 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2069 		       CTLTYPE_INT, "recvspace",
   2070 		       SYSCTL_DESCR("Default TCP receive buffer size"),
   2071 		       NULL, 0, &tcp_recvspace, 0,
   2072 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RECVSPACE, CTL_EOL);
   2073 	sysctl_createv(clog, 0, NULL, NULL,
   2074 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2075 		       CTLTYPE_INT, "mssdflt",
   2076 		       SYSCTL_DESCR("Default maximum segment size"),
   2077 		       sysctl_net_inet_tcp_mssdflt, 0, &tcp_mssdflt, 0,
   2078 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSSDFLT, CTL_EOL);
   2079 	sysctl_createv(clog, 0, NULL, NULL,
   2080 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2081 		       CTLTYPE_INT, "minmss",
   2082 		       SYSCTL_DESCR("Lower limit for TCP maximum segment size"),
   2083 		       NULL, 0, &tcp_minmss, 0,
   2084 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   2085 	sysctl_createv(clog, 0, NULL, NULL,
   2086 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2087 		       CTLTYPE_INT, "msl",
   2088 		       SYSCTL_DESCR("Maximum Segment Life"),
   2089 		       NULL, 0, &tcp_msl, 0,
   2090 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSL, CTL_EOL);
   2091 	sysctl_createv(clog, 0, NULL, NULL,
   2092 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2093 		       CTLTYPE_INT, "syn_cache_limit",
   2094 		       SYSCTL_DESCR("Maximum number of entries in the TCP "
   2095 				    "compressed state engine"),
   2096 		       NULL, 0, &tcp_syn_cache_limit, 0,
   2097 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_LIMIT,
   2098 		       CTL_EOL);
   2099 	sysctl_createv(clog, 0, NULL, NULL,
   2100 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2101 		       CTLTYPE_INT, "syn_bucket_limit",
   2102 		       SYSCTL_DESCR("Maximum number of entries per hash "
   2103 				    "bucket in the TCP compressed state "
   2104 				    "engine"),
   2105 		       NULL, 0, &tcp_syn_bucket_limit, 0,
   2106 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_BUCKET_LIMIT,
   2107 		       CTL_EOL);
   2108 #if 0 /* obsoleted */
   2109 	sysctl_createv(clog, 0, NULL, NULL,
   2110 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2111 		       CTLTYPE_INT, "syn_cache_interval",
   2112 		       SYSCTL_DESCR("TCP compressed state engine's timer interval"),
   2113 		       NULL, 0, &tcp_syn_cache_interval, 0,
   2114 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_INTER,
   2115 		       CTL_EOL);
   2116 #endif
   2117 	sysctl_createv(clog, 0, NULL, NULL,
   2118 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2119 		       CTLTYPE_INT, "init_win",
   2120 		       SYSCTL_DESCR("Initial TCP congestion window"),
   2121 		       sysctl_tcp_init_win, 0, &tcp_init_win, 0,
   2122 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN, CTL_EOL);
   2123 	sysctl_createv(clog, 0, NULL, NULL,
   2124 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2125 		       CTLTYPE_INT, "mss_ifmtu",
   2126 		       SYSCTL_DESCR("Use interface MTU for calculating MSS"),
   2127 		       NULL, 0, &tcp_mss_ifmtu, 0,
   2128 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSS_IFMTU, CTL_EOL);
   2129 	sysctl_createv(clog, 0, NULL, &sack_node,
   2130 		       CTLFLAG_PERMANENT,
   2131 		       CTLTYPE_NODE, "sack",
   2132 		       SYSCTL_DESCR("RFC2018 Selective ACKnowledgement tunables"),
   2133 		       NULL, 0, NULL, 0,
   2134 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_EOL);
   2135 
   2136 	/* Congctl subtree */
   2137 	sysctl_createv(clog, 0, NULL, &congctl_node,
   2138 		       CTLFLAG_PERMANENT,
   2139 		       CTLTYPE_NODE, "congctl",
   2140 		       SYSCTL_DESCR("TCP Congestion Control"),
   2141 	    	       NULL, 0, NULL, 0,
   2142 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   2143 	sysctl_createv(clog, 0, &congctl_node, NULL,
   2144 		       CTLFLAG_PERMANENT,
   2145 		       CTLTYPE_STRING, "available",
   2146 		       SYSCTL_DESCR("Available Congestion Control Mechanisms"),
   2147 		       NULL, 0, tcp_congctl_avail, 0, CTL_CREATE, CTL_EOL);
   2148 	sysctl_createv(clog, 0, &congctl_node, NULL,
   2149 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2150 		       CTLTYPE_STRING, "selected",
   2151 		       SYSCTL_DESCR("Selected Congestion Control Mechanism"),
   2152 		       sysctl_tcp_congctl, 0, NULL, TCPCC_MAXLEN,
   2153 		       CTL_CREATE, CTL_EOL);
   2154 
   2155 	sysctl_createv(clog, 0, NULL, NULL,
   2156 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2157 		       CTLTYPE_INT, "win_scale",
   2158 		       SYSCTL_DESCR("Use RFC1323 window scale options"),
   2159 		       sysctl_update_tcpcb_template, 0, &tcp_do_win_scale, 0,
   2160 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_WSCALE, CTL_EOL);
   2161 	sysctl_createv(clog, 0, NULL, NULL,
   2162 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2163 		       CTLTYPE_INT, "timestamps",
   2164 		       SYSCTL_DESCR("Use RFC1323 time stamp options"),
   2165 		       sysctl_update_tcpcb_template, 0, &tcp_do_timestamps, 0,
   2166 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_TSTAMP, CTL_EOL);
   2167 	sysctl_createv(clog, 0, NULL, NULL,
   2168 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2169 		       CTLTYPE_INT, "compat_42",
   2170 		       SYSCTL_DESCR("Enable workarounds for 4.2BSD TCP bugs"),
   2171 		       NULL, 0, &tcp_compat_42, 0,
   2172 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_COMPAT_42, CTL_EOL);
   2173 	sysctl_createv(clog, 0, NULL, NULL,
   2174 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2175 		       CTLTYPE_INT, "cwm",
   2176 		       SYSCTL_DESCR("Hughes/Touch/Heidemann Congestion Window "
   2177 				    "Monitoring"),
   2178 		       NULL, 0, &tcp_cwm, 0,
   2179 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM, CTL_EOL);
   2180 	sysctl_createv(clog, 0, NULL, NULL,
   2181 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2182 		       CTLTYPE_INT, "cwm_burstsize",
   2183 		       SYSCTL_DESCR("Congestion Window Monitoring allowed "
   2184 				    "burst count in packets"),
   2185 		       NULL, 0, &tcp_cwm_burstsize, 0,
   2186 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM_BURSTSIZE,
   2187 		       CTL_EOL);
   2188 	sysctl_createv(clog, 0, NULL, NULL,
   2189 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2190 		       CTLTYPE_INT, "ack_on_push",
   2191 		       SYSCTL_DESCR("Immediately return ACK when PSH is "
   2192 				    "received"),
   2193 		       NULL, 0, &tcp_ack_on_push, 0,
   2194 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_ACK_ON_PUSH, CTL_EOL);
   2195 	sysctl_createv(clog, 0, NULL, NULL,
   2196 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2197 		       CTLTYPE_INT, "keepidle",
   2198 		       SYSCTL_DESCR("Allowed connection idle ticks before a "
   2199 				    "keepalive probe is sent"),
   2200 		       sysctl_tcp_keep, 0, &tcp_keepidle, 0,
   2201 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPIDLE, CTL_EOL);
   2202 	sysctl_createv(clog, 0, NULL, NULL,
   2203 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2204 		       CTLTYPE_INT, "keepintvl",
   2205 		       SYSCTL_DESCR("Ticks before next keepalive probe is sent"),
   2206 		       sysctl_tcp_keep, 0, &tcp_keepintvl, 0,
   2207 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPINTVL, CTL_EOL);
   2208 	sysctl_createv(clog, 0, NULL, NULL,
   2209 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2210 		       CTLTYPE_INT, "keepcnt",
   2211 		       SYSCTL_DESCR("Number of keepalive probes to send"),
   2212 		       sysctl_tcp_keep, 0, &tcp_keepcnt, 0,
   2213 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPCNT, CTL_EOL);
   2214 	sysctl_createv(clog, 0, NULL, NULL,
   2215 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
   2216 		       CTLTYPE_INT, "slowhz",
   2217 		       SYSCTL_DESCR("Keepalive ticks per second"),
   2218 		       NULL, PR_SLOWHZ, NULL, 0,
   2219 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SLOWHZ, CTL_EOL);
   2220 	sysctl_createv(clog, 0, NULL, NULL,
   2221 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2222 		       CTLTYPE_INT, "log_refused",
   2223 		       SYSCTL_DESCR("Log refused TCP connections"),
   2224 		       NULL, 0, &tcp_log_refused, 0,
   2225 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOG_REFUSED, CTL_EOL);
   2226 #if 0 /* obsoleted */
   2227 	sysctl_createv(clog, 0, NULL, NULL,
   2228 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2229 		       CTLTYPE_INT, "rstratelimit", NULL,
   2230 		       NULL, 0, &tcp_rst_ratelim, 0,
   2231 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTRATELIMIT, CTL_EOL);
   2232 #endif
   2233 	sysctl_createv(clog, 0, NULL, NULL,
   2234 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2235 		       CTLTYPE_INT, "rstppslimit",
   2236 		       SYSCTL_DESCR("Maximum number of RST packets to send "
   2237 				    "per second"),
   2238 		       NULL, 0, &tcp_rst_ppslim, 0,
   2239 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTPPSLIMIT, CTL_EOL);
   2240 	sysctl_createv(clog, 0, NULL, NULL,
   2241 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2242 		       CTLTYPE_INT, "delack_ticks",
   2243 		       SYSCTL_DESCR("Number of ticks to delay sending an ACK"),
   2244 		       NULL, 0, &tcp_delack_ticks, 0,
   2245 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DELACK_TICKS, CTL_EOL);
   2246 	sysctl_createv(clog, 0, NULL, NULL,
   2247 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2248 		       CTLTYPE_INT, "init_win_local",
   2249 		       SYSCTL_DESCR("Initial TCP window size (in segments)"),
   2250 		       sysctl_tcp_init_win, 0, &tcp_init_win_local, 0,
   2251 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN_LOCAL,
   2252 		       CTL_EOL);
   2253 	sysctl_createv(clog, 0, NULL, NULL,
   2254 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2255 		       CTLTYPE_STRUCT, "ident",
   2256 		       SYSCTL_DESCR("RFC1413 Identification Protocol lookups"),
   2257 		       sysctl_net_inet_tcp_ident, 0, NULL, sizeof(uid_t),
   2258 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_IDENT, CTL_EOL);
   2259 	sysctl_createv(clog, 0, NULL, NULL,
   2260 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2261 		       CTLTYPE_INT, "do_loopback_cksum",
   2262 		       SYSCTL_DESCR("Perform TCP checksum on loopback"),
   2263 		       NULL, 0, &tcp_do_loopback_cksum, 0,
   2264 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOOPBACKCKSUM,
   2265 		       CTL_EOL);
   2266 	sysctl_createv(clog, 0, NULL, NULL,
   2267 		       CTLFLAG_PERMANENT,
   2268 		       CTLTYPE_STRUCT, "pcblist",
   2269 		       SYSCTL_DESCR("TCP protocol control block list"),
   2270 		       sysctl_inpcblist, 0, &tcbtable, 0,
   2271 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE,
   2272 		       CTL_EOL);
   2273 	sysctl_createv(clog, 0, NULL, NULL,
   2274 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2275 		       CTLTYPE_INT, "keepinit",
   2276 		       SYSCTL_DESCR("Ticks before initial tcp connection times out"),
   2277 		       sysctl_tcp_keep, 0, &tcp_keepinit, 0,
   2278 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   2279 
   2280 	/* TCP socket buffers auto-sizing nodes */
   2281 	sysctl_createv(clog, 0, NULL, NULL,
   2282 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2283 		       CTLTYPE_INT, "recvbuf_auto",
   2284 		       SYSCTL_DESCR("Enable automatic receive "
   2285 		           "buffer sizing (experimental)"),
   2286 		       NULL, 0, &tcp_do_autorcvbuf, 0,
   2287 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   2288 	sysctl_createv(clog, 0, NULL, NULL,
   2289 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2290 		       CTLTYPE_INT, "recvbuf_inc",
   2291 		       SYSCTL_DESCR("Incrementor step size of "
   2292 		           "automatic receive buffer"),
   2293 		       NULL, 0, &tcp_autorcvbuf_inc, 0,
   2294 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   2295 	sysctl_createv(clog, 0, NULL, NULL,
   2296 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2297 		       CTLTYPE_INT, "recvbuf_max",
   2298 		       SYSCTL_DESCR("Max size of automatic receive buffer"),
   2299 		       NULL, 0, &tcp_autorcvbuf_max, 0,
   2300 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   2301 
   2302 	sysctl_createv(clog, 0, NULL, NULL,
   2303 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2304 		       CTLTYPE_INT, "sendbuf_auto",
   2305 		       SYSCTL_DESCR("Enable automatic send "
   2306 		           "buffer sizing (experimental)"),
   2307 		       NULL, 0, &tcp_do_autosndbuf, 0,
   2308 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   2309 	sysctl_createv(clog, 0, NULL, NULL,
   2310 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2311 		       CTLTYPE_INT, "sendbuf_inc",
   2312 		       SYSCTL_DESCR("Incrementor step size of "
   2313 		           "automatic send buffer"),
   2314 		       NULL, 0, &tcp_autosndbuf_inc, 0,
   2315 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   2316 	sysctl_createv(clog, 0, NULL, NULL,
   2317 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2318 		       CTLTYPE_INT, "sendbuf_max",
   2319 		       SYSCTL_DESCR("Max size of automatic send buffer"),
   2320 		       NULL, 0, &tcp_autosndbuf_max, 0,
   2321 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   2322 
   2323 	/* ECN subtree */
   2324 	sysctl_createv(clog, 0, NULL, &ecn_node,
   2325 	    	       CTLFLAG_PERMANENT,
   2326 		       CTLTYPE_NODE, "ecn",
   2327 	    	       SYSCTL_DESCR("RFC3168 Explicit Congestion Notification"),
   2328 	    	       NULL, 0, NULL, 0,
   2329 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   2330 	sysctl_createv(clog, 0, &ecn_node, NULL,
   2331 	    	       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2332 		       CTLTYPE_INT, "enable",
   2333 		       SYSCTL_DESCR("Enable TCP Explicit Congestion "
   2334 			   "Notification"),
   2335 	    	       NULL, 0, &tcp_do_ecn, 0, CTL_CREATE, CTL_EOL);
   2336 	sysctl_createv(clog, 0, &ecn_node, NULL,
   2337 	    	       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2338 		       CTLTYPE_INT, "maxretries",
   2339 		       SYSCTL_DESCR("Number of times to retry ECN setup "
   2340 			       "before disabling ECN on the connection"),
   2341 	    	       NULL, 0, &tcp_ecn_maxretries, 0, CTL_CREATE, CTL_EOL);
   2342 
   2343 	/* SACK gets its own little subtree. */
   2344 	sysctl_createv(clog, 0, NULL, &sack_node,
   2345 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2346 		       CTLTYPE_INT, "enable",
   2347 		       SYSCTL_DESCR("Enable RFC2018 Selective ACKnowledgement"),
   2348 		       NULL, 0, &tcp_do_sack, 0,
   2349 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
   2350 	sysctl_createv(clog, 0, NULL, &sack_node,
   2351 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2352 		       CTLTYPE_INT, "maxholes",
   2353 		       SYSCTL_DESCR("Maximum number of TCP SACK holes allowed per connection"),
   2354 		       NULL, 0, &tcp_sack_tp_maxholes, 0,
   2355 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
   2356 	sysctl_createv(clog, 0, NULL, &sack_node,
   2357 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2358 		       CTLTYPE_INT, "globalmaxholes",
   2359 		       SYSCTL_DESCR("Global maximum number of TCP SACK holes"),
   2360 		       NULL, 0, &tcp_sack_globalmaxholes, 0,
   2361 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
   2362 	sysctl_createv(clog, 0, NULL, &sack_node,
   2363 		       CTLFLAG_PERMANENT,
   2364 		       CTLTYPE_INT, "globalholes",
   2365 		       SYSCTL_DESCR("Global number of TCP SACK holes"),
   2366 		       NULL, 0, &tcp_sack_globalholes, 0,
   2367 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
   2368 
   2369 	sysctl_createv(clog, 0, NULL, NULL,
   2370 		       CTLFLAG_PERMANENT,
   2371 		       CTLTYPE_STRUCT, "stats",
   2372 		       SYSCTL_DESCR("TCP statistics"),
   2373 		       sysctl_net_inet_tcp_stats, 0, NULL, 0,
   2374 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_STATS,
   2375 		       CTL_EOL);
   2376         sysctl_createv(clog, 0, NULL, NULL,
   2377                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2378                        CTLTYPE_INT, "local_by_rtt",
   2379                        SYSCTL_DESCR("Use RTT estimator to decide which hosts "
   2380 				    "are local"),
   2381 		       NULL, 0, &tcp_rttlocal, 0,
   2382 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   2383 #ifdef TCP_DEBUG
   2384 	sysctl_createv(clog, 0, NULL, NULL,
   2385 		       CTLFLAG_PERMANENT,
   2386 		       CTLTYPE_STRUCT, "debug",
   2387 		       SYSCTL_DESCR("TCP sockets debug information"),
   2388 		       NULL, 0, &tcp_debug, sizeof(tcp_debug),
   2389 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBUG,
   2390 		       CTL_EOL);
   2391 	sysctl_createv(clog, 0, NULL, NULL,
   2392 		       CTLFLAG_PERMANENT,
   2393 		       CTLTYPE_INT, "debx",
   2394 		       SYSCTL_DESCR("Number of TCP debug sockets messages"),
   2395 		       NULL, 0, &tcp_debx, sizeof(tcp_debx),
   2396 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBX,
   2397 		       CTL_EOL);
   2398 #endif
   2399 	sysctl_createv(clog, 0, NULL, NULL,
   2400 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2401 		       CTLTYPE_STRUCT, "drop",
   2402 		       SYSCTL_DESCR("TCP drop connection"),
   2403 		       sysctl_net_inet_tcp_drop, 0, NULL, 0,
   2404 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DROP, CTL_EOL);
   2405 	sysctl_createv(clog, 0, NULL, NULL,
   2406 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2407 		       CTLTYPE_INT, "iss_hash",
   2408 		       SYSCTL_DESCR("Enable RFC 1948 ISS by cryptographic "
   2409 				    "hash computation"),
   2410 		       NULL, 0, &tcp_do_rfc1948, sizeof(tcp_do_rfc1948),
   2411 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE,
   2412 		       CTL_EOL);
   2413 
   2414 	/* ABC subtree */
   2415 
   2416 	sysctl_createv(clog, 0, NULL, &abc_node,
   2417 		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "abc",
   2418 		       SYSCTL_DESCR("RFC3465 Appropriate Byte Counting (ABC)"),
   2419 		       NULL, 0, NULL, 0,
   2420 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   2421 	sysctl_createv(clog, 0, &abc_node, NULL,
   2422 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2423 		       CTLTYPE_INT, "enable",
   2424 		       SYSCTL_DESCR("Enable RFC3465 Appropriate Byte Counting"),
   2425 		       NULL, 0, &tcp_do_abc, 0, CTL_CREATE, CTL_EOL);
   2426 	sysctl_createv(clog, 0, &abc_node, NULL,
   2427 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2428 		       CTLTYPE_INT, "aggressive",
   2429 		       SYSCTL_DESCR("1: L=2*SMSS 0: L=1*SMSS"),
   2430 		       NULL, 0, &tcp_abc_aggressive, 0, CTL_CREATE, CTL_EOL);
   2431 
   2432 	/* MSL tuning subtree */
   2433 
   2434 	sysctl_createv(clog, 0, NULL, &mslt_node,
   2435 		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "mslt",
   2436 		       SYSCTL_DESCR("MSL Tuning for TIME_WAIT truncation"),
   2437 		       NULL, 0, NULL, 0,
   2438 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   2439 	sysctl_createv(clog, 0, &mslt_node, NULL,
   2440 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2441 		       CTLTYPE_INT, "enable",
   2442 		       SYSCTL_DESCR("Enable TIME_WAIT truncation"),
   2443 		       NULL, 0, &tcp_msl_enable, 0, CTL_CREATE, CTL_EOL);
   2444 	sysctl_createv(clog, 0, &mslt_node, NULL,
   2445 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2446 		       CTLTYPE_INT, "loopback",
   2447 		       SYSCTL_DESCR("MSL value to use for loopback connections"),
   2448 		       NULL, 0, &tcp_msl_loop, 0, CTL_CREATE, CTL_EOL);
   2449 	sysctl_createv(clog, 0, &mslt_node, NULL,
   2450 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2451 		       CTLTYPE_INT, "local",
   2452 		       SYSCTL_DESCR("MSL value to use for local connections"),
   2453 		       NULL, 0, &tcp_msl_local, 0, CTL_CREATE, CTL_EOL);
   2454 	sysctl_createv(clog, 0, &mslt_node, NULL,
   2455 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2456 		       CTLTYPE_INT, "remote",
   2457 		       SYSCTL_DESCR("MSL value to use for remote connections"),
   2458 		       NULL, 0, &tcp_msl_remote, 0, CTL_CREATE, CTL_EOL);
   2459 	sysctl_createv(clog, 0, &mslt_node, NULL,
   2460 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2461 		       CTLTYPE_INT, "remote_threshold",
   2462 		       SYSCTL_DESCR("RTT estimate value to promote local to remote"),
   2463 		       NULL, 0, &tcp_msl_remote_threshold, 0, CTL_CREATE, CTL_EOL);
   2464 
   2465 	/* vestigial TIME_WAIT tuning subtree */
   2466 
   2467 	sysctl_createv(clog, 0, NULL, &vtw_node,
   2468 		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "vtw",
   2469 		       SYSCTL_DESCR("Tuning for Vestigial TIME_WAIT"),
   2470 		       NULL, 0, NULL, 0,
   2471 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   2472 	sysctl_createv(clog, 0, &vtw_node, NULL,
   2473 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2474 		       CTLTYPE_INT, "enable",
   2475 		       SYSCTL_DESCR("Enable Vestigial TIME_WAIT"),
   2476 		       sysctl_tcp_vtw_enable, 0,
   2477 	               (pf == AF_INET) ? &tcp4_vtw_enable : &tcp6_vtw_enable,
   2478 		       0, CTL_CREATE, CTL_EOL);
   2479 	sysctl_createv(clog, 0, &vtw_node, NULL,
   2480 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
   2481 		       CTLTYPE_INT, "entries",
   2482 		       SYSCTL_DESCR("Maximum number of vestigial TIME_WAIT entries"),
   2483 		       NULL, 0, &tcp_vtw_entries, 0, CTL_CREATE, CTL_EOL);
   2484 }
   2485 
   2486 void
   2487 tcp_usrreq_init(void)
   2488 {
   2489 
   2490 #ifdef INET
   2491 	sysctl_net_inet_tcp_setup2(NULL, PF_INET, "inet", "tcp");
   2492 #endif
   2493 #ifdef INET6
   2494 	sysctl_net_inet_tcp_setup2(NULL, PF_INET6, "inet6", "tcp6");
   2495 #endif
   2496 }
   2497 
   2498 PR_WRAP_USRREQS(tcp)
   2499 #define	tcp_attach	tcp_attach_wrapper
   2500 #define	tcp_detach	tcp_detach_wrapper
   2501 #define	tcp_accept	tcp_accept_wrapper
   2502 #define	tcp_bind	tcp_bind_wrapper
   2503 #define	tcp_listen	tcp_listen_wrapper
   2504 #define	tcp_connect	tcp_connect_wrapper
   2505 #define	tcp_connect2	tcp_connect2_wrapper
   2506 #define	tcp_disconnect	tcp_disconnect_wrapper
   2507 #define	tcp_shutdown	tcp_shutdown_wrapper
   2508 #define	tcp_abort	tcp_abort_wrapper
   2509 #define	tcp_ioctl	tcp_ioctl_wrapper
   2510 #define	tcp_stat	tcp_stat_wrapper
   2511 #define	tcp_peeraddr	tcp_peeraddr_wrapper
   2512 #define	tcp_sockaddr	tcp_sockaddr_wrapper
   2513 #define	tcp_rcvd	tcp_rcvd_wrapper
   2514 #define	tcp_recvoob	tcp_recvoob_wrapper
   2515 #define	tcp_send	tcp_send_wrapper
   2516 #define	tcp_sendoob	tcp_sendoob_wrapper
   2517 #define	tcp_purgeif	tcp_purgeif_wrapper
   2518 #define	tcp_usrreq	tcp_usrreq_wrapper
   2519 
   2520 const struct pr_usrreqs tcp_usrreqs = {
   2521 	.pr_attach	= tcp_attach,
   2522 	.pr_detach	= tcp_detach,
   2523 	.pr_accept	= tcp_accept,
   2524 	.pr_bind	= tcp_bind,
   2525 	.pr_listen	= tcp_listen,
   2526 	.pr_connect	= tcp_connect,
   2527 	.pr_connect2	= tcp_connect2,
   2528 	.pr_disconnect	= tcp_disconnect,
   2529 	.pr_shutdown	= tcp_shutdown,
   2530 	.pr_abort	= tcp_abort,
   2531 	.pr_ioctl	= tcp_ioctl,
   2532 	.pr_stat	= tcp_stat,
   2533 	.pr_peeraddr	= tcp_peeraddr,
   2534 	.pr_sockaddr	= tcp_sockaddr,
   2535 	.pr_rcvd	= tcp_rcvd,
   2536 	.pr_recvoob	= tcp_recvoob,
   2537 	.pr_send	= tcp_send,
   2538 	.pr_sendoob	= tcp_sendoob,
   2539 	.pr_purgeif	= tcp_purgeif,
   2540 	.pr_generic	= tcp_usrreq,
   2541 };
   2542