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