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