Home | History | Annotate | Line # | Download | only in netinet
tcp_usrreq.c revision 1.166.4.1
      1 /*	$NetBSD: tcp_usrreq.c,v 1.166.4.1 2013/07/17 03:16:31 rmind 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 #include <sys/cdefs.h>
     98 __KERNEL_RCSID(0, "$NetBSD: tcp_usrreq.c,v 1.166.4.1 2013/07/17 03:16:31 rmind Exp $");
     99 
    100 #include "opt_inet.h"
    101 #include "opt_ipsec.h"
    102 #include "opt_tcp_debug.h"
    103 #include "opt_mbuftrace.h"
    104 
    105 
    106 #include <sys/param.h>
    107 #include <sys/systm.h>
    108 #include <sys/kernel.h>
    109 #include <sys/malloc.h>
    110 #include <sys/mbuf.h>
    111 #include <sys/socket.h>
    112 #include <sys/socketvar.h>
    113 #include <sys/protosw.h>
    114 #include <sys/errno.h>
    115 #include <sys/stat.h>
    116 #include <sys/proc.h>
    117 #include <sys/domain.h>
    118 #include <sys/sysctl.h>
    119 #include <sys/kauth.h>
    120 #include <sys/uidinfo.h>
    121 
    122 #include <net/if.h>
    123 #include <net/route.h>
    124 
    125 #include <netinet/in.h>
    126 #include <netinet/in_systm.h>
    127 #include <netinet/in_var.h>
    128 #include <netinet/ip.h>
    129 #include <netinet/in_pcb.h>
    130 #include <netinet/ip_var.h>
    131 #include <netinet/in_offload.h>
    132 
    133 #ifdef INET6
    134 #ifndef INET
    135 #include <netinet/in.h>
    136 #endif
    137 #include <netinet/ip6.h>
    138 #include <netinet6/in6_pcb.h>
    139 #include <netinet6/ip6_var.h>
    140 #include <netinet6/scope6_var.h>
    141 #endif
    142 
    143 #include <netinet/tcp.h>
    144 #include <netinet/tcp_fsm.h>
    145 #include <netinet/tcp_seq.h>
    146 #include <netinet/tcp_timer.h>
    147 #include <netinet/tcp_var.h>
    148 #include <netinet/tcp_private.h>
    149 #include <netinet/tcp_congctl.h>
    150 #include <netinet/tcpip.h>
    151 #include <netinet/tcp_debug.h>
    152 #include <netinet/tcp_vtw.h>
    153 
    154 #include "opt_tcp_space.h"
    155 
    156 /*
    157  * TCP protocol interface to socket abstraction.
    158  */
    159 
    160 /*
    161  * Process a TCP user request for TCP tb.  If this is a send request
    162  * then m is the mbuf chain of send data.  If this is a timer expiration
    163  * (called from the software clock routine), then timertype tells which timer.
    164  */
    165 int
    166 tcp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
    167     struct mbuf *control, struct lwp *l)
    168 {
    169 	inpcb_t *inp;
    170 #ifdef INET6
    171 	struct in6pcb *in6p;
    172 #endif
    173 	struct tcpcb *tp = NULL;
    174 	int s;
    175 	int error = 0;
    176 #ifdef TCP_DEBUG
    177 	int ostate = 0;
    178 #endif
    179 	int family;	/* family of the socket */
    180 
    181 	family = so->so_proto->pr_domain->dom_family;
    182 
    183 	if (req == PRU_CONTROL) {
    184 		switch (family) {
    185 #ifdef INET
    186 		case PF_INET:
    187 			return (in_control(so, (long)m, (void *)nam,
    188 			    (struct ifnet *)control, l));
    189 #endif
    190 #ifdef INET6
    191 		case PF_INET6:
    192 			return (in6_control(so, (long)m, (void *)nam,
    193 			    (struct ifnet *)control, l));
    194 #endif
    195 		default:
    196 			return EAFNOSUPPORT;
    197 		}
    198 	}
    199 
    200 	s = splsoftnet();
    201 
    202 	if (req == PRU_PURGEIF) {
    203 		mutex_enter(softnet_lock);
    204 		switch (family) {
    205 #ifdef INET
    206 		case PF_INET:
    207 			inpcb_purgeif0(tcbtable, (struct ifnet *)control);
    208 			in_purgeif((struct ifnet *)control);
    209 			inpcb_purgeif(tcbtable, (struct ifnet *)control);
    210 			break;
    211 #endif
    212 #ifdef INET6
    213 		case PF_INET6:
    214 			in6_pcbpurgeif0(tcbtable, (struct ifnet *)control);
    215 			in6_purgeif((struct ifnet *)control);
    216 			in6_pcbpurgeif(tcbtable, (struct ifnet *)control);
    217 			break;
    218 #endif
    219 		default:
    220 			mutex_exit(softnet_lock);
    221 			splx(s);
    222 			return (EAFNOSUPPORT);
    223 		}
    224 		mutex_exit(softnet_lock);
    225 		splx(s);
    226 		return (0);
    227 	}
    228 
    229 	KASSERT(req == PRU_ATTACH || solocked(so));
    230 
    231 	if (req == PRU_ATTACH)
    232 		sosetlock(so);
    233 
    234 	switch (family) {
    235 #ifdef INET
    236 	case PF_INET:
    237 		inp = sotoinpcb(so);
    238 #ifdef INET6
    239 		in6p = NULL;
    240 #endif
    241 		break;
    242 #endif
    243 #ifdef INET6
    244 	case PF_INET6:
    245 		inp = NULL;
    246 		in6p = sotoin6pcb(so);
    247 		break;
    248 #endif
    249 	default:
    250 		splx(s);
    251 		return EAFNOSUPPORT;
    252 	}
    253 
    254 #ifdef DIAGNOSTIC
    255 #ifdef INET6
    256 	if (inp && in6p)
    257 		panic("tcp_usrreq: both inp and in6p set to non-NULL");
    258 #endif
    259 	if (req != PRU_SEND && req != PRU_SENDOOB && control)
    260 		panic("tcp_usrreq: unexpected control mbuf");
    261 #endif
    262 	/*
    263 	 * When a TCP is attached to a socket, then there will be
    264 	 * a (struct inpcb) pointed at by the socket, and this
    265 	 * structure will point at a subsidary (struct tcpcb).
    266 	 */
    267 	if ((inp == 0
    268 #ifdef INET6
    269 	    && in6p == 0
    270 #endif
    271 	    ) && (req != PRU_ATTACH && req != PRU_SENSE))
    272 	{
    273 		error = EINVAL;
    274 		goto release;
    275 	}
    276 #ifdef INET
    277 	if (inp) {
    278 		tp = intotcpcb(inp);
    279 		/* WHAT IF TP IS 0? */
    280 #ifdef KPROF
    281 		tcp_acounts[tp->t_state][req]++;
    282 #endif
    283 #ifdef TCP_DEBUG
    284 		ostate = tp->t_state;
    285 #endif
    286 	}
    287 #endif
    288 #ifdef INET6
    289 	if (in6p) {
    290 		tp = in6totcpcb(in6p);
    291 		/* WHAT IF TP IS 0? */
    292 #ifdef KPROF
    293 		tcp_acounts[tp->t_state][req]++;
    294 #endif
    295 #ifdef TCP_DEBUG
    296 		ostate = tp->t_state;
    297 #endif
    298 	}
    299 #endif
    300 
    301 	switch (req) {
    302 
    303 	/*
    304 	 * TCP attaches to socket via PRU_ATTACH, reserving space,
    305 	 * and an internet control block.
    306 	 */
    307 	case PRU_ATTACH:
    308 #ifndef INET6
    309 		if (inp != 0)
    310 #else
    311 		if (inp != 0 || in6p != 0)
    312 #endif
    313 		{
    314 			error = EISCONN;
    315 			break;
    316 		}
    317 		error = tcp_attach(so);
    318 		if (error)
    319 			break;
    320 		if ((so->so_options & SO_LINGER) && so->so_linger == 0)
    321 			so->so_linger = TCP_LINGERTIME;
    322 		tp = sototcpcb(so);
    323 		break;
    324 
    325 	/*
    326 	 * PRU_DETACH detaches the TCP protocol from the socket.
    327 	 */
    328 	case PRU_DETACH:
    329 		tp = tcp_disconnect(tp);
    330 		break;
    331 
    332 	/*
    333 	 * Give the socket an address.
    334 	 */
    335 	case PRU_BIND:
    336 		switch (family) {
    337 #ifdef INET
    338 		case PF_INET:
    339 			error = inpcb_bind(inp, nam, l);
    340 			break;
    341 #endif
    342 #ifdef INET6
    343 		case PF_INET6:
    344 			error = in6_pcbbind(in6p, nam, l);
    345 			if (!error) {
    346 				/* mapped addr case */
    347 				if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
    348 					tp->t_family = AF_INET;
    349 				else
    350 					tp->t_family = AF_INET6;
    351 			}
    352 			break;
    353 #endif
    354 		}
    355 		break;
    356 
    357 	/*
    358 	 * Prepare to accept connections.
    359 	 */
    360 	case PRU_LISTEN:
    361 #ifdef INET
    362 		if (inp) {
    363 			in_port_t lport;
    364 
    365 			inpcb_get_ports(inp, &lport, NULL);
    366 			if (lport == 0) {
    367 				error = inpcb_bind(inp, NULL, l);
    368 				if (error)
    369 					break;
    370 			}
    371 		}
    372 #endif
    373 #ifdef INET6
    374 		if (in6p && in6p->in6p_lport == 0) {
    375 			error = in6_pcbbind(in6p, NULL, l);
    376 			if (error)
    377 				break;
    378 		}
    379 #endif
    380 		tp->t_state = TCPS_LISTEN;
    381 		break;
    382 
    383 	/*
    384 	 * Initiate connection to peer.
    385 	 * Create a template for use in transmissions on this connection.
    386 	 * Enter SYN_SENT state, and mark socket as connecting.
    387 	 * Start keep-alive timer, and seed output sequence space.
    388 	 * Send initial segment on connection.
    389 	 */
    390 	case PRU_CONNECT:
    391 #ifdef INET
    392 		if (inp) {
    393 			in_port_t lport;
    394 
    395 			inpcb_get_ports(inp, &lport, NULL);
    396 			if (lport == 0) {
    397 				error = inpcb_bind(inp, NULL, l);
    398 				if (error)
    399 					break;
    400 			}
    401 			error = inpcb_connect(inp, nam, l);
    402 		}
    403 #endif
    404 #ifdef INET6
    405 		if (in6p) {
    406 			if (in6p->in6p_lport == 0) {
    407 				error = in6_pcbbind(in6p, NULL, l);
    408 				if (error)
    409 					break;
    410 			}
    411 			error = in6_pcbconnect(in6p, nam, l);
    412 			if (!error) {
    413 				/* mapped addr case */
    414 				if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr))
    415 					tp->t_family = AF_INET;
    416 				else
    417 					tp->t_family = AF_INET6;
    418 			}
    419 		}
    420 #endif
    421 		if (error)
    422 			break;
    423 		tp->t_template = tcp_template(tp);
    424 		if (tp->t_template == 0) {
    425 #ifdef INET
    426 			if (inp)
    427 				inpcb_disconnect(inp);
    428 #endif
    429 #ifdef INET6
    430 			if (in6p)
    431 				in6_pcbdisconnect(in6p);
    432 #endif
    433 			error = ENOBUFS;
    434 			break;
    435 		}
    436 		/*
    437 		 * Compute window scaling to request.
    438 		 * XXX: This should be moved to tcp_output().
    439 		 */
    440 		while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
    441 		    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
    442 			tp->request_r_scale++;
    443 		soisconnecting(so);
    444 		TCP_STATINC(TCP_STAT_CONNATTEMPT);
    445 		tp->t_state = TCPS_SYN_SENT;
    446 		TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepinit);
    447 		tp->iss = tcp_new_iss(tp, 0);
    448 		tcp_sendseqinit(tp);
    449 		error = tcp_output(tp);
    450 		break;
    451 
    452 	/*
    453 	 * Create a TCP connection between two sockets.
    454 	 */
    455 	case PRU_CONNECT2:
    456 		error = EOPNOTSUPP;
    457 		break;
    458 
    459 	/*
    460 	 * Initiate disconnect from peer.
    461 	 * If connection never passed embryonic stage, just drop;
    462 	 * else if don't need to let data drain, then can just drop anyways,
    463 	 * else have to begin TCP shutdown process: mark socket disconnecting,
    464 	 * drain unread data, state switch to reflect user close, and
    465 	 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
    466 	 * when peer sends FIN and acks ours.
    467 	 *
    468 	 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
    469 	 */
    470 	case PRU_DISCONNECT:
    471 		tp = tcp_disconnect(tp);
    472 		break;
    473 
    474 	/*
    475 	 * Accept a connection.  Essentially all the work is
    476 	 * done at higher levels; just return the address
    477 	 * of the peer, storing through addr.
    478 	 */
    479 	case PRU_ACCEPT:
    480 #ifdef INET
    481 		if (inp)
    482 			inpcb_fetch_peeraddr(inp, nam);
    483 #endif
    484 #ifdef INET6
    485 		if (in6p)
    486 			in6_setpeeraddr(in6p, nam);
    487 #endif
    488 		break;
    489 
    490 	/*
    491 	 * Mark the connection as being incapable of further output.
    492 	 */
    493 	case PRU_SHUTDOWN:
    494 		socantsendmore(so);
    495 		tp = tcp_usrclosed(tp);
    496 		if (tp)
    497 			error = tcp_output(tp);
    498 		break;
    499 
    500 	/*
    501 	 * After a receive, possibly send window update to peer.
    502 	 */
    503 	case PRU_RCVD:
    504 		/*
    505 		 * soreceive() calls this function when a user receives
    506 		 * ancillary data on a listening socket. We don't call
    507 		 * tcp_output in such a case, since there is no header
    508 		 * template for a listening socket and hence the kernel
    509 		 * will panic.
    510 		 */
    511 		if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) != 0)
    512 			(void) tcp_output(tp);
    513 		break;
    514 
    515 	/*
    516 	 * Do a send by putting data in output queue and updating urgent
    517 	 * marker if URG set.  Possibly send more data.
    518 	 */
    519 	case PRU_SEND:
    520 		if (control && control->m_len) {
    521 			m_freem(control);
    522 			m_freem(m);
    523 			error = EINVAL;
    524 			break;
    525 		}
    526 		sbappendstream(&so->so_snd, m);
    527 		error = tcp_output(tp);
    528 		break;
    529 
    530 	/*
    531 	 * Abort the TCP.
    532 	 */
    533 	case PRU_ABORT:
    534 		tp = tcp_drop(tp, ECONNABORTED);
    535 		break;
    536 
    537 	case PRU_SENSE:
    538 		/*
    539 		 * stat: don't bother with a blocksize.
    540 		 */
    541 		splx(s);
    542 		return (0);
    543 
    544 	case PRU_RCVOOB:
    545 		if (control && control->m_len) {
    546 			m_freem(control);
    547 			m_freem(m);
    548 			error = EINVAL;
    549 			break;
    550 		}
    551 		if ((so->so_oobmark == 0 &&
    552 		    (so->so_state & SS_RCVATMARK) == 0) ||
    553 		    so->so_options & SO_OOBINLINE ||
    554 		    tp->t_oobflags & TCPOOB_HADDATA) {
    555 			error = EINVAL;
    556 			break;
    557 		}
    558 		if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
    559 			error = EWOULDBLOCK;
    560 			break;
    561 		}
    562 		m->m_len = 1;
    563 		*mtod(m, char *) = tp->t_iobc;
    564 		if (((long)nam & MSG_PEEK) == 0)
    565 			tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
    566 		break;
    567 
    568 	case PRU_SENDOOB:
    569 		if (sbspace(&so->so_snd) < -512) {
    570 			m_freem(m);
    571 			error = ENOBUFS;
    572 			break;
    573 		}
    574 		/*
    575 		 * According to RFC961 (Assigned Protocols),
    576 		 * the urgent pointer points to the last octet
    577 		 * of urgent data.  We continue, however,
    578 		 * to consider it to indicate the first octet
    579 		 * of data past the urgent section.
    580 		 * Otherwise, snd_up should be one lower.
    581 		 */
    582 		sbappendstream(&so->so_snd, m);
    583 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
    584 		tp->t_force = 1;
    585 		error = tcp_output(tp);
    586 		tp->t_force = 0;
    587 		break;
    588 
    589 	case PRU_SOCKADDR:
    590 #ifdef INET
    591 		if (inp)
    592 			inpcb_fetch_sockaddr(inp, nam);
    593 #endif
    594 #ifdef INET6
    595 		if (in6p)
    596 			in6_setsockaddr(in6p, nam);
    597 #endif
    598 		break;
    599 
    600 	case PRU_PEERADDR:
    601 #ifdef INET
    602 		if (inp)
    603 			inpcb_fetch_peeraddr(inp, nam);
    604 #endif
    605 #ifdef INET6
    606 		if (in6p)
    607 			in6_setpeeraddr(in6p, nam);
    608 #endif
    609 		break;
    610 
    611 	default:
    612 		panic("tcp_usrreq");
    613 	}
    614 #ifdef TCP_DEBUG
    615 	if (tp && (so->so_options & SO_DEBUG))
    616 		tcp_trace(TA_USER, ostate, tp, NULL, req);
    617 #endif
    618 
    619 release:
    620 	splx(s);
    621 	return (error);
    622 }
    623 
    624 static void
    625 change_keepalive(struct socket *so, struct tcpcb *tp)
    626 {
    627 	tp->t_maxidle = tp->t_keepcnt * tp->t_keepintvl;
    628 	TCP_TIMER_DISARM(tp, TCPT_KEEP);
    629 	TCP_TIMER_DISARM(tp, TCPT_2MSL);
    630 
    631 	if (tp->t_state == TCPS_SYN_RECEIVED ||
    632 	    tp->t_state == TCPS_SYN_SENT) {
    633 		TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepinit);
    634 	} else if (so->so_options & SO_KEEPALIVE &&
    635 	    tp->t_state <= TCPS_CLOSE_WAIT) {
    636 		TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepintvl);
    637 	} else {
    638 		TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepidle);
    639 	}
    640 
    641 	if ((tp->t_state == TCPS_FIN_WAIT_2) && (tp->t_maxidle > 0))
    642 		TCP_TIMER_ARM(tp, TCPT_2MSL, tp->t_maxidle);
    643 }
    644 
    645 
    646 int
    647 tcp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
    648 {
    649 	int error = 0, s;
    650 	inpcb_t *inp;
    651 #ifdef INET6
    652 	struct in6pcb *in6p;
    653 #endif
    654 	struct tcpcb *tp;
    655 	u_int ui;
    656 	int family;	/* family of the socket */
    657 	int level, optname, optval;
    658 
    659 	level = sopt->sopt_level;
    660 	optname = sopt->sopt_name;
    661 
    662 	family = so->so_proto->pr_domain->dom_family;
    663 
    664 	s = splsoftnet();
    665 	switch (family) {
    666 #ifdef INET
    667 	case PF_INET:
    668 		inp = sotoinpcb(so);
    669 #ifdef INET6
    670 		in6p = NULL;
    671 #endif
    672 		break;
    673 #endif
    674 #ifdef INET6
    675 	case PF_INET6:
    676 		inp = NULL;
    677 		in6p = sotoin6pcb(so);
    678 		break;
    679 #endif
    680 	default:
    681 		splx(s);
    682 		panic("%s: af %d", __func__, family);
    683 	}
    684 #ifndef INET6
    685 	if (inp == NULL)
    686 #else
    687 	if (inp == NULL && in6p == NULL)
    688 #endif
    689 	{
    690 		splx(s);
    691 		return (ECONNRESET);
    692 	}
    693 	if (level != IPPROTO_TCP) {
    694 		switch (family) {
    695 #ifdef INET
    696 		case PF_INET:
    697 			error = ip_ctloutput(op, so, sopt);
    698 			break;
    699 #endif
    700 #ifdef INET6
    701 		case PF_INET6:
    702 			error = ip6_ctloutput(op, so, sopt);
    703 			break;
    704 #endif
    705 		}
    706 		splx(s);
    707 		return (error);
    708 	}
    709 	if (inp)
    710 		tp = intotcpcb(inp);
    711 #ifdef INET6
    712 	else if (in6p)
    713 		tp = in6totcpcb(in6p);
    714 #endif
    715 	else
    716 		tp = NULL;
    717 
    718 	switch (op) {
    719 	case PRCO_SETOPT:
    720 		switch (optname) {
    721 #ifdef TCP_SIGNATURE
    722 		case TCP_MD5SIG:
    723 			error = sockopt_getint(sopt, &optval);
    724 			if (error)
    725 				break;
    726 			if (optval > 0)
    727 				tp->t_flags |= TF_SIGNATURE;
    728 			else
    729 				tp->t_flags &= ~TF_SIGNATURE;
    730 			break;
    731 #endif /* TCP_SIGNATURE */
    732 
    733 		case TCP_NODELAY:
    734 			error = sockopt_getint(sopt, &optval);
    735 			if (error)
    736 				break;
    737 			if (optval)
    738 				tp->t_flags |= TF_NODELAY;
    739 			else
    740 				tp->t_flags &= ~TF_NODELAY;
    741 			break;
    742 
    743 		case TCP_MAXSEG:
    744 			error = sockopt_getint(sopt, &optval);
    745 			if (error)
    746 				break;
    747 			if (optval > 0 && optval <= tp->t_peermss)
    748 				tp->t_peermss = optval; /* limit on send size */
    749 			else
    750 				error = EINVAL;
    751 			break;
    752 #ifdef notyet
    753 		case TCP_CONGCTL:
    754 			/* XXX string overflow XXX */
    755 			error = tcp_congctl_select(tp, sopt->sopt_data);
    756 			break;
    757 #endif
    758 
    759 		case TCP_KEEPIDLE:
    760 			error = sockopt_get(sopt, &ui, sizeof(ui));
    761 			if (error)
    762 				break;
    763 			if (ui > 0) {
    764 				tp->t_keepidle = ui;
    765 				change_keepalive(so, tp);
    766 			} else
    767 				error = EINVAL;
    768 			break;
    769 
    770 		case TCP_KEEPINTVL:
    771 			error = sockopt_get(sopt, &ui, sizeof(ui));
    772 			if (error)
    773 				break;
    774 			if (ui > 0) {
    775 				tp->t_keepintvl = ui;
    776 				change_keepalive(so, tp);
    777 			} else
    778 				error = EINVAL;
    779 			break;
    780 
    781 		case TCP_KEEPCNT:
    782 			error = sockopt_get(sopt, &ui, sizeof(ui));
    783 			if (error)
    784 				break;
    785 			if (ui > 0) {
    786 				tp->t_keepcnt = ui;
    787 				change_keepalive(so, tp);
    788 			} else
    789 				error = EINVAL;
    790 			break;
    791 
    792 		case TCP_KEEPINIT:
    793 			error = sockopt_get(sopt, &ui, sizeof(ui));
    794 			if (error)
    795 				break;
    796 			if (ui > 0) {
    797 				tp->t_keepinit = ui;
    798 				change_keepalive(so, tp);
    799 			} else
    800 				error = EINVAL;
    801 			break;
    802 
    803 		default:
    804 			error = ENOPROTOOPT;
    805 			break;
    806 		}
    807 		break;
    808 
    809 	case PRCO_GETOPT:
    810 		switch (optname) {
    811 #ifdef TCP_SIGNATURE
    812 		case TCP_MD5SIG:
    813 			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
    814 			error = sockopt_set(sopt, &optval, sizeof(optval));
    815 			break;
    816 #endif
    817 		case TCP_NODELAY:
    818 			optval = tp->t_flags & TF_NODELAY;
    819 			error = sockopt_set(sopt, &optval, sizeof(optval));
    820 			break;
    821 		case TCP_MAXSEG:
    822 			optval = tp->t_peermss;
    823 			error = sockopt_set(sopt, &optval, sizeof(optval));
    824 			break;
    825 #ifdef notyet
    826 		case TCP_CONGCTL:
    827 			break;
    828 #endif
    829 		default:
    830 			error = ENOPROTOOPT;
    831 			break;
    832 		}
    833 		break;
    834 	}
    835 	splx(s);
    836 	return (error);
    837 }
    838 
    839 #ifndef TCP_SENDSPACE
    840 #define	TCP_SENDSPACE	1024*32
    841 #endif
    842 int	tcp_sendspace = TCP_SENDSPACE;
    843 #ifndef TCP_RECVSPACE
    844 #define	TCP_RECVSPACE	1024*32
    845 #endif
    846 int	tcp_recvspace = TCP_RECVSPACE;
    847 
    848 /*
    849  * Attach TCP protocol to socket, allocating
    850  * internet protocol control block, tcp control block,
    851  * bufer space, and entering LISTEN state if to accept connections.
    852  */
    853 int
    854 tcp_attach(struct socket *so)
    855 {
    856 	struct tcpcb *tp;
    857 	inpcb_t *inp;
    858 #ifdef INET6
    859 	struct in6pcb *in6p;
    860 #endif
    861 	int error;
    862 	int family;	/* family of the socket */
    863 
    864 	family = so->so_proto->pr_domain->dom_family;
    865 
    866 #ifdef MBUFTRACE
    867 	so->so_mowner = &tcp_sock_mowner;
    868 	so->so_rcv.sb_mowner = &tcp_sock_rx_mowner;
    869 	so->so_snd.sb_mowner = &tcp_sock_tx_mowner;
    870 #endif
    871 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    872 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
    873 		if (error)
    874 			return (error);
    875 	}
    876 
    877 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
    878 	so->so_snd.sb_flags |= SB_AUTOSIZE;
    879 
    880 	switch (family) {
    881 #ifdef INET
    882 	case PF_INET:
    883 		error = inpcb_create(so, tcbtable);
    884 		if (error)
    885 			return (error);
    886 		inp = sotoinpcb(so);
    887 #ifdef INET6
    888 		in6p = NULL;
    889 #endif
    890 		break;
    891 #endif
    892 #ifdef INET6
    893 	case PF_INET6:
    894 		error = in6_pcballoc(so, tcbtable);
    895 		if (error)
    896 			return (error);
    897 		inp = NULL;
    898 		in6p = sotoin6pcb(so);
    899 		break;
    900 #endif
    901 	default:
    902 		return EAFNOSUPPORT;
    903 	}
    904 	if (inp)
    905 		tp = tcp_newtcpcb(family, (void *)inp);
    906 #ifdef INET6
    907 	else if (in6p)
    908 		tp = tcp_newtcpcb(family, (void *)in6p);
    909 #endif
    910 	else
    911 		tp = NULL;
    912 
    913 	if (tp == NULL) {
    914 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
    915 
    916 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
    917 #ifdef INET
    918 		if (inp)
    919 			inpcb_destroy(inp);
    920 #endif
    921 #ifdef INET6
    922 		if (in6p)
    923 			in6_pcbdetach(in6p);
    924 #endif
    925 		so->so_state |= nofd;
    926 		return (ENOBUFS);
    927 	}
    928 	tp->t_state = TCPS_CLOSED;
    929 	return (0);
    930 }
    931 
    932 /*
    933  * Initiate (or continue) disconnect.
    934  * If embryonic state, just send reset (once).
    935  * If in ``let data drain'' option and linger null, just drop.
    936  * Otherwise (hard), mark socket disconnecting and drop
    937  * current input data; switch states based on user close, and
    938  * send segment to peer (with FIN).
    939  */
    940 struct tcpcb *
    941 tcp_disconnect(struct tcpcb *tp)
    942 {
    943 	struct socket *so;
    944 
    945 	if (tp->t_inpcb)
    946 		so = inpcb_get_socket(tp->t_inpcb);
    947 #ifdef INET6
    948 	else if (tp->t_in6pcb)
    949 		so = tp->t_in6pcb->in6p_socket;
    950 #endif
    951 	else
    952 		so = NULL;
    953 
    954 	KASSERT(so != NULL);
    955 
    956 	if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
    957 		tp = tcp_close(tp);
    958 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
    959 		tp = tcp_drop(tp, 0);
    960 	else {
    961 		soisdisconnecting(so);
    962 		sbflush(&so->so_rcv);
    963 		tp = tcp_usrclosed(tp);
    964 		if (tp)
    965 			(void) tcp_output(tp);
    966 	}
    967 	return (tp);
    968 }
    969 
    970 /*
    971  * User issued close, and wish to trail through shutdown states:
    972  * if never received SYN, just forget it.  If got a SYN from peer,
    973  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
    974  * If already got a FIN from peer, then almost done; go to LAST_ACK
    975  * state.  In all other cases, have already sent FIN to peer (e.g.
    976  * after PRU_SHUTDOWN), and just have to play tedious game waiting
    977  * for peer to send FIN or not respond to keep-alives, etc.
    978  * We can let the user exit from the close as soon as the FIN is acked.
    979  */
    980 struct tcpcb *
    981 tcp_usrclosed(struct tcpcb *tp)
    982 {
    983 
    984 	switch (tp->t_state) {
    985 
    986 	case TCPS_CLOSED:
    987 	case TCPS_LISTEN:
    988 	case TCPS_SYN_SENT:
    989 		tp->t_state = TCPS_CLOSED;
    990 		tp = tcp_close(tp);
    991 		break;
    992 
    993 	case TCPS_SYN_RECEIVED:
    994 	case TCPS_ESTABLISHED:
    995 		tp->t_state = TCPS_FIN_WAIT_1;
    996 		break;
    997 
    998 	case TCPS_CLOSE_WAIT:
    999 		tp->t_state = TCPS_LAST_ACK;
   1000 		break;
   1001 	}
   1002 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
   1003 		struct socket *so;
   1004 		if (tp->t_inpcb)
   1005 			so = inpcb_get_socket(tp->t_inpcb);
   1006 #ifdef INET6
   1007 		else if (tp->t_in6pcb)
   1008 			so = tp->t_in6pcb->in6p_socket;
   1009 #endif
   1010 		else
   1011 			so = NULL;
   1012 		if (so)
   1013 			soisdisconnected(so);
   1014 		/*
   1015 		 * If we are in FIN_WAIT_2, we arrived here because the
   1016 		 * application did a shutdown of the send side.  Like the
   1017 		 * case of a transition from FIN_WAIT_1 to FIN_WAIT_2 after
   1018 		 * a full close, we start a timer to make sure sockets are
   1019 		 * not left in FIN_WAIT_2 forever.
   1020 		 */
   1021 		if ((tp->t_state == TCPS_FIN_WAIT_2) && (tp->t_maxidle > 0))
   1022 			TCP_TIMER_ARM(tp, TCPT_2MSL, tp->t_maxidle);
   1023 		else if (tp->t_state == TCPS_TIME_WAIT
   1024 			 && ((tp->t_inpcb
   1025 			      && (tcp4_vtw_enable & 1)
   1026 			      && vtw_add(AF_INET, tp))
   1027 			     ||
   1028 			     (tp->t_in6pcb
   1029 			      && (tcp6_vtw_enable & 1)
   1030 			      && vtw_add(AF_INET6, tp)))) {
   1031 			tp = 0;
   1032 		}
   1033 	}
   1034 	return (tp);
   1035 }
   1036 
   1037 /*
   1038  * sysctl helper routine for net.inet.ip.mssdflt.  it can't be less
   1039  * than 32.
   1040  */
   1041 static int
   1042 sysctl_net_inet_tcp_mssdflt(SYSCTLFN_ARGS)
   1043 {
   1044 	int error, mssdflt;
   1045 	struct sysctlnode node;
   1046 
   1047 	mssdflt = tcp_mssdflt;
   1048 	node = *rnode;
   1049 	node.sysctl_data = &mssdflt;
   1050 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1051 	if (error || newp == NULL)
   1052 		return (error);
   1053 
   1054 	if (mssdflt < 32)
   1055 		return (EINVAL);
   1056 	tcp_mssdflt = mssdflt;
   1057 
   1058 	return (0);
   1059 }
   1060 
   1061 /*
   1062  * sysctl helper routine for setting port related values under
   1063  * net.inet.ip and net.inet6.ip6.  does basic range checking and does
   1064  * additional checks for each type.  this code has placed in
   1065  * tcp_input.c since INET and INET6 both use the same tcp code.
   1066  *
   1067  * this helper is not static so that both inet and inet6 can use it.
   1068  */
   1069 int
   1070 sysctl_net_inet_ip_ports(SYSCTLFN_ARGS)
   1071 {
   1072 	int error, tmp;
   1073 	int apmin, apmax;
   1074 #ifndef IPNOPRIVPORTS
   1075 	int lpmin, lpmax;
   1076 #endif /* IPNOPRIVPORTS */
   1077 	struct sysctlnode node;
   1078 
   1079 	if (namelen != 0)
   1080 		return (EINVAL);
   1081 
   1082 	switch (name[-3]) {
   1083 #ifdef INET
   1084 	    case PF_INET:
   1085 		apmin = anonportmin;
   1086 		apmax = anonportmax;
   1087 #ifndef IPNOPRIVPORTS
   1088 		lpmin = lowportmin;
   1089 		lpmax = lowportmax;
   1090 #endif /* IPNOPRIVPORTS */
   1091 		break;
   1092 #endif /* INET */
   1093 #ifdef INET6
   1094 	    case PF_INET6:
   1095 		apmin = ip6_anonportmin;
   1096 		apmax = ip6_anonportmax;
   1097 #ifndef IPNOPRIVPORTS
   1098 		lpmin = ip6_lowportmin;
   1099 		lpmax = ip6_lowportmax;
   1100 #endif /* IPNOPRIVPORTS */
   1101 		break;
   1102 #endif /* INET6 */
   1103 	    default:
   1104 		return (EINVAL);
   1105 	}
   1106 
   1107 	/*
   1108 	 * insert temporary copy into node, perform lookup on
   1109 	 * temporary, then restore pointer
   1110 	 */
   1111 	node = *rnode;
   1112 	tmp = *(int*)rnode->sysctl_data;
   1113 	node.sysctl_data = &tmp;
   1114 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1115 	if (error || newp == NULL)
   1116 		return (error);
   1117 
   1118 	/*
   1119 	 * simple port range check
   1120 	 */
   1121 	if (tmp < 0 || tmp > 65535)
   1122 		return (EINVAL);
   1123 
   1124 	/*
   1125 	 * per-node range checks
   1126 	 */
   1127 	switch (rnode->sysctl_num) {
   1128 	case IPCTL_ANONPORTMIN:
   1129 	case IPV6CTL_ANONPORTMIN:
   1130 		if (tmp >= apmax)
   1131 			return (EINVAL);
   1132 #ifndef IPNOPRIVPORTS
   1133 		if (tmp < IPPORT_RESERVED)
   1134                         return (EINVAL);
   1135 #endif /* IPNOPRIVPORTS */
   1136 		break;
   1137 
   1138 	case IPCTL_ANONPORTMAX:
   1139 	case IPV6CTL_ANONPORTMAX:
   1140                 if (apmin >= tmp)
   1141 			return (EINVAL);
   1142 #ifndef IPNOPRIVPORTS
   1143 		if (tmp < IPPORT_RESERVED)
   1144                         return (EINVAL);
   1145 #endif /* IPNOPRIVPORTS */
   1146 		break;
   1147 
   1148 #ifndef IPNOPRIVPORTS
   1149 	case IPCTL_LOWPORTMIN:
   1150 	case IPV6CTL_LOWPORTMIN:
   1151 		if (tmp >= lpmax ||
   1152 		    tmp > IPPORT_RESERVEDMAX ||
   1153 		    tmp < IPPORT_RESERVEDMIN)
   1154 			return (EINVAL);
   1155 		break;
   1156 
   1157 	case IPCTL_LOWPORTMAX:
   1158 	case IPV6CTL_LOWPORTMAX:
   1159 		if (lpmin >= tmp ||
   1160 		    tmp > IPPORT_RESERVEDMAX ||
   1161 		    tmp < IPPORT_RESERVEDMIN)
   1162 			return (EINVAL);
   1163 		break;
   1164 #endif /* IPNOPRIVPORTS */
   1165 
   1166 	default:
   1167 		return (EINVAL);
   1168 	}
   1169 
   1170 	*(int*)rnode->sysctl_data = tmp;
   1171 
   1172 	return (0);
   1173 }
   1174 
   1175 static inline int
   1176 copyout_uid(struct socket *sockp, void *oldp, size_t *oldlenp)
   1177 {
   1178 	size_t sz;
   1179 	int error;
   1180 	uid_t uid;
   1181 
   1182 	uid = kauth_cred_geteuid(sockp->so_cred);
   1183 	if (oldp) {
   1184 		sz = MIN(sizeof(uid), *oldlenp);
   1185 		error = copyout(&uid, oldp, sz);
   1186 		if (error)
   1187 			return error;
   1188 	}
   1189 	*oldlenp = sizeof(uid);
   1190 	return 0;
   1191 }
   1192 
   1193 static inline int
   1194 inet4_ident_core(struct in_addr raddr, u_int rport,
   1195     struct in_addr laddr, u_int lport,
   1196     void *oldp, size_t *oldlenp,
   1197     struct lwp *l, int dodrop)
   1198 {
   1199 	inpcb_t *inp;
   1200 	struct socket *so;
   1201 
   1202 	inp = inpcb_lookup_connect(tcbtable, raddr, rport, laddr, lport, 0);
   1203 	if (inp == NULL || (so = inpcb_get_socket(inp)) == NULL)
   1204 		return ESRCH;
   1205 
   1206 	if (dodrop) {
   1207 		struct tcpcb *tp;
   1208 		int error;
   1209 
   1210 		if ((tp = intotcpcb(inp)) == NULL ||
   1211 		    (so->so_options & SO_ACCEPTCONN) != 0)
   1212 			return ESRCH;
   1213 
   1214 		error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
   1215 		    KAUTH_REQ_NETWORK_SOCKET_DROP, so, tp, NULL);
   1216 		if (error) {
   1217 			return error;
   1218 		}
   1219 
   1220 		(void)tcp_drop(tp, ECONNABORTED);
   1221 		return 0;
   1222 	}
   1223 
   1224 	return copyout_uid(so, oldp, oldlenp);
   1225 }
   1226 
   1227 #ifdef INET6
   1228 static inline int
   1229 inet6_ident_core(struct in6_addr *raddr, u_int rport,
   1230     struct in6_addr *laddr, u_int lport,
   1231     void *oldp, size_t *oldlenp,
   1232     struct lwp *l, int dodrop)
   1233 {
   1234 	struct in6pcb *in6p;
   1235 	struct socket *sockp;
   1236 
   1237 	in6p = in6_pcblookup_connect(tcbtable, raddr, rport, laddr, lport, 0, 0);
   1238 
   1239 	if (in6p == NULL || (sockp = in6p->in6p_socket) == NULL)
   1240 		return ESRCH;
   1241 
   1242 	if (dodrop) {
   1243 		struct tcpcb *tp;
   1244 		int error;
   1245 
   1246 		if (in6p == NULL || (tp = in6totcpcb(in6p)) == NULL ||
   1247 		    (in6p->in6p_socket->so_options & SO_ACCEPTCONN) != 0)
   1248 			return ESRCH;
   1249 
   1250 		error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
   1251 		    KAUTH_REQ_NETWORK_SOCKET_DROP, in6p->in6p_socket, tp, NULL);
   1252 		if (error)
   1253 			return (error);
   1254 
   1255 		(void)tcp_drop(tp, ECONNABORTED);
   1256 		return 0;
   1257 	}
   1258 	else
   1259 		return copyout_uid(sockp, oldp, oldlenp);
   1260 }
   1261 #endif
   1262 
   1263 /*
   1264  * sysctl helper routine for the net.inet.tcp.drop and
   1265  * net.inet6.tcp6.drop nodes.
   1266  */
   1267 #define sysctl_net_inet_tcp_drop sysctl_net_inet_tcp_ident
   1268 
   1269 /*
   1270  * sysctl helper routine for the net.inet.tcp.ident and
   1271  * net.inet6.tcp6.ident nodes.  contains backwards compat code for the
   1272  * old way of looking up the ident information for ipv4 which involves
   1273  * stuffing the port/addr pairs into the mib lookup.
   1274  */
   1275 static int
   1276 sysctl_net_inet_tcp_ident(SYSCTLFN_ARGS)
   1277 {
   1278 #ifdef INET
   1279 	struct sockaddr_in *si4[2];
   1280 #endif /* INET */
   1281 #ifdef INET6
   1282 	struct sockaddr_in6 *si6[2];
   1283 #endif /* INET6 */
   1284 	struct sockaddr_storage sa[2];
   1285 	int error, pf, dodrop;
   1286 
   1287 	dodrop = name[-1] == TCPCTL_DROP;
   1288 	if (dodrop) {
   1289 		if (oldp != NULL || *oldlenp != 0)
   1290 			return EINVAL;
   1291 		if (newp == NULL)
   1292 			return EPERM;
   1293 		if (newlen < sizeof(sa))
   1294 			return ENOMEM;
   1295 	}
   1296 	if (namelen != 4 && namelen != 0)
   1297 		return EINVAL;
   1298 	if (name[-2] != IPPROTO_TCP)
   1299 		return EINVAL;
   1300 	pf = name[-3];
   1301 
   1302 	/* old style lookup, ipv4 only */
   1303 	if (namelen == 4) {
   1304 #ifdef INET
   1305 		struct in_addr laddr, raddr;
   1306 		u_int lport, rport;
   1307 
   1308 		if (pf != PF_INET)
   1309 			return EPROTONOSUPPORT;
   1310 		raddr.s_addr = (uint32_t)name[0];
   1311 		rport = (u_int)name[1];
   1312 		laddr.s_addr = (uint32_t)name[2];
   1313 		lport = (u_int)name[3];
   1314 
   1315 		mutex_enter(softnet_lock);
   1316 		error = inet4_ident_core(raddr, rport, laddr, lport,
   1317 		    oldp, oldlenp, l, dodrop);
   1318 		mutex_exit(softnet_lock);
   1319 		return error;
   1320 #else /* INET */
   1321 		return EINVAL;
   1322 #endif /* INET */
   1323 	}
   1324 
   1325 	if (newp == NULL || newlen != sizeof(sa))
   1326 		return EINVAL;
   1327 	error = copyin(newp, &sa, newlen);
   1328 	if (error)
   1329 		return error;
   1330 
   1331 	/*
   1332 	 * requested families must match
   1333 	 */
   1334 	if (pf != sa[0].ss_family || sa[0].ss_family != sa[1].ss_family)
   1335 		return EINVAL;
   1336 
   1337 	switch (pf) {
   1338 #ifdef INET6
   1339 	case PF_INET6:
   1340 		si6[0] = (struct sockaddr_in6*)&sa[0];
   1341 		si6[1] = (struct sockaddr_in6*)&sa[1];
   1342 		if (si6[0]->sin6_len != sizeof(*si6[0]) ||
   1343 		    si6[1]->sin6_len != sizeof(*si6[1]))
   1344 			return EINVAL;
   1345 
   1346 		if (!IN6_IS_ADDR_V4MAPPED(&si6[0]->sin6_addr) &&
   1347 		    !IN6_IS_ADDR_V4MAPPED(&si6[1]->sin6_addr)) {
   1348 			error = sa6_embedscope(si6[0], ip6_use_defzone);
   1349 			if (error)
   1350 				return error;
   1351 			error = sa6_embedscope(si6[1], ip6_use_defzone);
   1352 			if (error)
   1353 				return error;
   1354 
   1355 			mutex_enter(softnet_lock);
   1356 			error = inet6_ident_core(&si6[0]->sin6_addr,
   1357 			    si6[0]->sin6_port, &si6[1]->sin6_addr,
   1358 			    si6[1]->sin6_port, oldp, oldlenp, l, dodrop);
   1359 			mutex_exit(softnet_lock);
   1360 			return error;
   1361 		}
   1362 
   1363 		if (IN6_IS_ADDR_V4MAPPED(&si6[0]->sin6_addr) !=
   1364 		    IN6_IS_ADDR_V4MAPPED(&si6[1]->sin6_addr))
   1365 			return EINVAL;
   1366 
   1367 		in6_sin6_2_sin_in_sock((struct sockaddr *)&sa[0]);
   1368 		in6_sin6_2_sin_in_sock((struct sockaddr *)&sa[1]);
   1369 		/*FALLTHROUGH*/
   1370 #endif /* INET6 */
   1371 #ifdef INET
   1372 	case PF_INET:
   1373 		si4[0] = (struct sockaddr_in*)&sa[0];
   1374 		si4[1] = (struct sockaddr_in*)&sa[1];
   1375 		if (si4[0]->sin_len != sizeof(*si4[0]) ||
   1376 		    si4[0]->sin_len != sizeof(*si4[1]))
   1377 			return EINVAL;
   1378 
   1379 		mutex_enter(softnet_lock);
   1380 		error = inet4_ident_core(si4[0]->sin_addr, si4[0]->sin_port,
   1381 		    si4[1]->sin_addr, si4[1]->sin_port,
   1382 		    oldp, oldlenp, l, dodrop);
   1383 		mutex_exit(softnet_lock);
   1384 		return error;
   1385 #endif /* INET */
   1386 	default:
   1387 		return EPROTONOSUPPORT;
   1388 	}
   1389 }
   1390 
   1391 static int
   1392 sysctl_tcp_congctl(SYSCTLFN_ARGS)
   1393 {
   1394 	struct sysctlnode node;
   1395 	int error;
   1396 	char newname[TCPCC_MAXLEN];
   1397 
   1398 	strlcpy(newname, tcp_congctl_global_name, sizeof(newname) - 1);
   1399 
   1400 	node = *rnode;
   1401 	node.sysctl_data = newname;
   1402 	node.sysctl_size = sizeof(newname);
   1403 
   1404 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1405 
   1406 	if (error ||
   1407 	    newp == NULL ||
   1408 	    strncmp(newname, tcp_congctl_global_name, sizeof(newname)) == 0)
   1409 		return error;
   1410 
   1411 	mutex_enter(softnet_lock);
   1412 	error = tcp_congctl_select(NULL, newname);
   1413 	mutex_exit(softnet_lock);
   1414 
   1415 	return error;
   1416 }
   1417 
   1418 static int
   1419 sysctl_tcp_init_win(SYSCTLFN_ARGS)
   1420 {
   1421 	int error;
   1422 	u_int iw;
   1423 	struct sysctlnode node;
   1424 
   1425 	iw = *(u_int *)rnode->sysctl_data;
   1426 	node = *rnode;
   1427 	node.sysctl_data = &iw;
   1428 	node.sysctl_size = sizeof(iw);
   1429 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1430 	if (error || newp == NULL)
   1431 		return error;
   1432 
   1433 	if (iw >= __arraycount(tcp_init_win_max))
   1434 		return EINVAL;
   1435 	*(u_int *)rnode->sysctl_data = iw;
   1436 	return 0;
   1437 }
   1438 
   1439 static int
   1440 sysctl_tcp_keep(SYSCTLFN_ARGS)
   1441 {
   1442 	int error;
   1443 	u_int tmp;
   1444 	struct sysctlnode node;
   1445 
   1446 	node = *rnode;
   1447 	tmp = *(u_int *)rnode->sysctl_data;
   1448 	node.sysctl_data = &tmp;
   1449 
   1450 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1451 	if (error || newp == NULL)
   1452 		return error;
   1453 
   1454 	mutex_enter(softnet_lock);
   1455 
   1456 	*(u_int *)rnode->sysctl_data = tmp;
   1457 	tcp_tcpcb_template();	/* update the template */
   1458 
   1459 	mutex_exit(softnet_lock);
   1460 	return 0;
   1461 }
   1462 
   1463 static int
   1464 sysctl_net_inet_tcp_stats(SYSCTLFN_ARGS)
   1465 {
   1466 
   1467 	return (NETSTAT_SYSCTL(tcpstat_percpu, TCP_NSTATS));
   1468 }
   1469 
   1470 /*
   1471  * this (second stage) setup routine is a replacement for tcp_sysctl()
   1472  * (which is currently used for ipv4 and ipv6)
   1473  */
   1474 static void
   1475 sysctl_net_inet_tcp_setup2(struct sysctllog **clog, int pf, const char *pfname,
   1476 			   const char *tcpname)
   1477 {
   1478 	const struct sysctlnode *sack_node;
   1479 	const struct sysctlnode *abc_node;
   1480 	const struct sysctlnode *ecn_node;
   1481 	const struct sysctlnode *congctl_node;
   1482 	const struct sysctlnode *mslt_node;
   1483 	const struct sysctlnode *vtw_node;
   1484 #ifdef TCP_DEBUG
   1485 	extern struct tcp_debug tcp_debug[TCP_NDEBUG];
   1486 	extern int tcp_debx;
   1487 #endif
   1488 
   1489 	sysctl_createv(clog, 0, NULL, NULL,
   1490 		       CTLFLAG_PERMANENT,
   1491 		       CTLTYPE_NODE, "net", NULL,
   1492 		       NULL, 0, NULL, 0,
   1493 		       CTL_NET, CTL_EOL);
   1494 	sysctl_createv(clog, 0, NULL, NULL,
   1495 		       CTLFLAG_PERMANENT,
   1496 		       CTLTYPE_NODE, pfname, NULL,
   1497 		       NULL, 0, NULL, 0,
   1498 		       CTL_NET, pf, CTL_EOL);
   1499 	sysctl_createv(clog, 0, NULL, NULL,
   1500 		       CTLFLAG_PERMANENT,
   1501 		       CTLTYPE_NODE, tcpname,
   1502 		       SYSCTL_DESCR("TCP related settings"),
   1503 		       NULL, 0, NULL, 0,
   1504 		       CTL_NET, pf, IPPROTO_TCP, CTL_EOL);
   1505 
   1506 	sysctl_createv(clog, 0, NULL, NULL,
   1507 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1508 		       CTLTYPE_INT, "rfc1323",
   1509 		       SYSCTL_DESCR("Enable RFC1323 TCP extensions"),
   1510 		       NULL, 0, &tcp_do_rfc1323, 0,
   1511 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RFC1323, CTL_EOL);
   1512 	sysctl_createv(clog, 0, NULL, NULL,
   1513 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1514 		       CTLTYPE_INT, "sendspace",
   1515 		       SYSCTL_DESCR("Default TCP send buffer size"),
   1516 		       NULL, 0, &tcp_sendspace, 0,
   1517 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SENDSPACE, CTL_EOL);
   1518 	sysctl_createv(clog, 0, NULL, NULL,
   1519 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1520 		       CTLTYPE_INT, "recvspace",
   1521 		       SYSCTL_DESCR("Default TCP receive buffer size"),
   1522 		       NULL, 0, &tcp_recvspace, 0,
   1523 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RECVSPACE, CTL_EOL);
   1524 	sysctl_createv(clog, 0, NULL, NULL,
   1525 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1526 		       CTLTYPE_INT, "mssdflt",
   1527 		       SYSCTL_DESCR("Default maximum segment size"),
   1528 		       sysctl_net_inet_tcp_mssdflt, 0, &tcp_mssdflt, 0,
   1529 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSSDFLT, CTL_EOL);
   1530 	sysctl_createv(clog, 0, NULL, NULL,
   1531 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1532 		       CTLTYPE_INT, "minmss",
   1533 		       SYSCTL_DESCR("Lower limit for TCP maximum segment size"),
   1534 		       NULL, 0, &tcp_minmss, 0,
   1535 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   1536 	sysctl_createv(clog, 0, NULL, NULL,
   1537 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1538 		       CTLTYPE_INT, "msl",
   1539 		       SYSCTL_DESCR("Maximum Segment Life"),
   1540 		       NULL, 0, &tcp_msl, 0,
   1541 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSL, CTL_EOL);
   1542 	sysctl_createv(clog, 0, NULL, NULL,
   1543 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1544 		       CTLTYPE_INT, "syn_cache_limit",
   1545 		       SYSCTL_DESCR("Maximum number of entries in the TCP "
   1546 				    "compressed state engine"),
   1547 		       NULL, 0, &tcp_syn_cache_limit, 0,
   1548 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_LIMIT,
   1549 		       CTL_EOL);
   1550 	sysctl_createv(clog, 0, NULL, NULL,
   1551 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1552 		       CTLTYPE_INT, "syn_bucket_limit",
   1553 		       SYSCTL_DESCR("Maximum number of entries per hash "
   1554 				    "bucket in the TCP compressed state "
   1555 				    "engine"),
   1556 		       NULL, 0, &tcp_syn_bucket_limit, 0,
   1557 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_BUCKET_LIMIT,
   1558 		       CTL_EOL);
   1559 #if 0 /* obsoleted */
   1560 	sysctl_createv(clog, 0, NULL, NULL,
   1561 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1562 		       CTLTYPE_INT, "syn_cache_interval",
   1563 		       SYSCTL_DESCR("TCP compressed state engine's timer interval"),
   1564 		       NULL, 0, &tcp_syn_cache_interval, 0,
   1565 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_INTER,
   1566 		       CTL_EOL);
   1567 #endif
   1568 	sysctl_createv(clog, 0, NULL, NULL,
   1569 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1570 		       CTLTYPE_INT, "init_win",
   1571 		       SYSCTL_DESCR("Initial TCP congestion window"),
   1572 		       sysctl_tcp_init_win, 0, &tcp_init_win, 0,
   1573 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN, CTL_EOL);
   1574 	sysctl_createv(clog, 0, NULL, NULL,
   1575 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1576 		       CTLTYPE_INT, "mss_ifmtu",
   1577 		       SYSCTL_DESCR("Use interface MTU for calculating MSS"),
   1578 		       NULL, 0, &tcp_mss_ifmtu, 0,
   1579 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSS_IFMTU, CTL_EOL);
   1580 	sysctl_createv(clog, 0, NULL, &sack_node,
   1581 		       CTLFLAG_PERMANENT,
   1582 		       CTLTYPE_NODE, "sack",
   1583 		       SYSCTL_DESCR("RFC2018 Selective ACKnowledgement tunables"),
   1584 		       NULL, 0, NULL, 0,
   1585 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_EOL);
   1586 
   1587 	/* Congctl subtree */
   1588 	sysctl_createv(clog, 0, NULL, &congctl_node,
   1589 		       CTLFLAG_PERMANENT,
   1590 		       CTLTYPE_NODE, "congctl",
   1591 		       SYSCTL_DESCR("TCP Congestion Control"),
   1592 	    	       NULL, 0, NULL, 0,
   1593 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   1594 	sysctl_createv(clog, 0, &congctl_node, NULL,
   1595 		       CTLFLAG_PERMANENT,
   1596 		       CTLTYPE_STRING, "available",
   1597 		       SYSCTL_DESCR("Available Congestion Control Mechanisms"),
   1598 		       NULL, 0, tcp_congctl_avail, 0, CTL_CREATE, CTL_EOL);
   1599 	sysctl_createv(clog, 0, &congctl_node, NULL,
   1600 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1601 		       CTLTYPE_STRING, "selected",
   1602 		       SYSCTL_DESCR("Selected Congestion Control Mechanism"),
   1603 		       sysctl_tcp_congctl, 0, NULL, TCPCC_MAXLEN,
   1604 		       CTL_CREATE, CTL_EOL);
   1605 
   1606 	sysctl_createv(clog, 0, NULL, NULL,
   1607 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1608 		       CTLTYPE_INT, "win_scale",
   1609 		       SYSCTL_DESCR("Use RFC1323 window scale options"),
   1610 		       NULL, 0, &tcp_do_win_scale, 0,
   1611 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_WSCALE, CTL_EOL);
   1612 	sysctl_createv(clog, 0, NULL, NULL,
   1613 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1614 		       CTLTYPE_INT, "timestamps",
   1615 		       SYSCTL_DESCR("Use RFC1323 time stamp options"),
   1616 		       NULL, 0, &tcp_do_timestamps, 0,
   1617 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_TSTAMP, CTL_EOL);
   1618 	sysctl_createv(clog, 0, NULL, NULL,
   1619 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1620 		       CTLTYPE_INT, "compat_42",
   1621 		       SYSCTL_DESCR("Enable workarounds for 4.2BSD TCP bugs"),
   1622 		       NULL, 0, &tcp_compat_42, 0,
   1623 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_COMPAT_42, CTL_EOL);
   1624 	sysctl_createv(clog, 0, NULL, NULL,
   1625 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1626 		       CTLTYPE_INT, "cwm",
   1627 		       SYSCTL_DESCR("Hughes/Touch/Heidemann Congestion Window "
   1628 				    "Monitoring"),
   1629 		       NULL, 0, &tcp_cwm, 0,
   1630 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM, CTL_EOL);
   1631 	sysctl_createv(clog, 0, NULL, NULL,
   1632 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1633 		       CTLTYPE_INT, "cwm_burstsize",
   1634 		       SYSCTL_DESCR("Congestion Window Monitoring allowed "
   1635 				    "burst count in packets"),
   1636 		       NULL, 0, &tcp_cwm_burstsize, 0,
   1637 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM_BURSTSIZE,
   1638 		       CTL_EOL);
   1639 	sysctl_createv(clog, 0, NULL, NULL,
   1640 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1641 		       CTLTYPE_INT, "ack_on_push",
   1642 		       SYSCTL_DESCR("Immediately return ACK when PSH is "
   1643 				    "received"),
   1644 		       NULL, 0, &tcp_ack_on_push, 0,
   1645 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_ACK_ON_PUSH, CTL_EOL);
   1646 	sysctl_createv(clog, 0, NULL, NULL,
   1647 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1648 		       CTLTYPE_INT, "keepidle",
   1649 		       SYSCTL_DESCR("Allowed connection idle ticks before a "
   1650 				    "keepalive probe is sent"),
   1651 		       sysctl_tcp_keep, 0, &tcp_keepidle, 0,
   1652 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPIDLE, CTL_EOL);
   1653 	sysctl_createv(clog, 0, NULL, NULL,
   1654 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1655 		       CTLTYPE_INT, "keepintvl",
   1656 		       SYSCTL_DESCR("Ticks before next keepalive probe is sent"),
   1657 		       sysctl_tcp_keep, 0, &tcp_keepintvl, 0,
   1658 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPINTVL, CTL_EOL);
   1659 	sysctl_createv(clog, 0, NULL, NULL,
   1660 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1661 		       CTLTYPE_INT, "keepcnt",
   1662 		       SYSCTL_DESCR("Number of keepalive probes to send"),
   1663 		       sysctl_tcp_keep, 0, &tcp_keepcnt, 0,
   1664 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPCNT, CTL_EOL);
   1665 	sysctl_createv(clog, 0, NULL, NULL,
   1666 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
   1667 		       CTLTYPE_INT, "slowhz",
   1668 		       SYSCTL_DESCR("Keepalive ticks per second"),
   1669 		       NULL, PR_SLOWHZ, NULL, 0,
   1670 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SLOWHZ, CTL_EOL);
   1671 	sysctl_createv(clog, 0, NULL, NULL,
   1672 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1673 		       CTLTYPE_INT, "log_refused",
   1674 		       SYSCTL_DESCR("Log refused TCP connections"),
   1675 		       NULL, 0, &tcp_log_refused, 0,
   1676 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOG_REFUSED, CTL_EOL);
   1677 #if 0 /* obsoleted */
   1678 	sysctl_createv(clog, 0, NULL, NULL,
   1679 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1680 		       CTLTYPE_INT, "rstratelimit", NULL,
   1681 		       NULL, 0, &tcp_rst_ratelim, 0,
   1682 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTRATELIMIT, CTL_EOL);
   1683 #endif
   1684 	sysctl_createv(clog, 0, NULL, NULL,
   1685 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1686 		       CTLTYPE_INT, "rstppslimit",
   1687 		       SYSCTL_DESCR("Maximum number of RST packets to send "
   1688 				    "per second"),
   1689 		       NULL, 0, &tcp_rst_ppslim, 0,
   1690 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTPPSLIMIT, CTL_EOL);
   1691 	sysctl_createv(clog, 0, NULL, NULL,
   1692 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1693 		       CTLTYPE_INT, "delack_ticks",
   1694 		       SYSCTL_DESCR("Number of ticks to delay sending an ACK"),
   1695 		       NULL, 0, &tcp_delack_ticks, 0,
   1696 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DELACK_TICKS, CTL_EOL);
   1697 	sysctl_createv(clog, 0, NULL, NULL,
   1698 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1699 		       CTLTYPE_INT, "init_win_local",
   1700 		       SYSCTL_DESCR("Initial TCP window size (in segments)"),
   1701 		       sysctl_tcp_init_win, 0, &tcp_init_win_local, 0,
   1702 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN_LOCAL,
   1703 		       CTL_EOL);
   1704 	sysctl_createv(clog, 0, NULL, NULL,
   1705 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1706 		       CTLTYPE_STRUCT, "ident",
   1707 		       SYSCTL_DESCR("RFC1413 Identification Protocol lookups"),
   1708 		       sysctl_net_inet_tcp_ident, 0, NULL, sizeof(uid_t),
   1709 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_IDENT, CTL_EOL);
   1710 	sysctl_createv(clog, 0, NULL, NULL,
   1711 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1712 		       CTLTYPE_INT, "do_loopback_cksum",
   1713 		       SYSCTL_DESCR("Perform TCP checksum on loopback"),
   1714 		       NULL, 0, &tcp_do_loopback_cksum, 0,
   1715 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOOPBACKCKSUM,
   1716 		       CTL_EOL);
   1717 	sysctl_createv(clog, 0, NULL, NULL,
   1718 		       CTLFLAG_PERMANENT,
   1719 		       CTLTYPE_STRUCT, "pcblist",
   1720 		       SYSCTL_DESCR("TCP protocol control block list"),
   1721 		       sysctl_inpcblist, 0, tcbtable, 0,
   1722 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE,
   1723 		       CTL_EOL);
   1724 	sysctl_createv(clog, 0, NULL, NULL,
   1725 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1726 		       CTLTYPE_INT, "keepinit",
   1727 		       SYSCTL_DESCR("Ticks before initial tcp connection times out"),
   1728 		       sysctl_tcp_keep, 0, &tcp_keepinit, 0,
   1729 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   1730 
   1731 	/* TCP socket buffers auto-sizing nodes */
   1732 	sysctl_createv(clog, 0, NULL, NULL,
   1733 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1734 		       CTLTYPE_INT, "recvbuf_auto",
   1735 		       SYSCTL_DESCR("Enable automatic receive "
   1736 		           "buffer sizing (experimental)"),
   1737 		       NULL, 0, &tcp_do_autorcvbuf, 0,
   1738 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   1739 	sysctl_createv(clog, 0, NULL, NULL,
   1740 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1741 		       CTLTYPE_INT, "recvbuf_inc",
   1742 		       SYSCTL_DESCR("Incrementor step size of "
   1743 		           "automatic receive buffer"),
   1744 		       NULL, 0, &tcp_autorcvbuf_inc, 0,
   1745 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   1746 	sysctl_createv(clog, 0, NULL, NULL,
   1747 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1748 		       CTLTYPE_INT, "recvbuf_max",
   1749 		       SYSCTL_DESCR("Max size of automatic receive buffer"),
   1750 		       NULL, 0, &tcp_autorcvbuf_max, 0,
   1751 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   1752 
   1753 	sysctl_createv(clog, 0, NULL, NULL,
   1754 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1755 		       CTLTYPE_INT, "sendbuf_auto",
   1756 		       SYSCTL_DESCR("Enable automatic send "
   1757 		           "buffer sizing (experimental)"),
   1758 		       NULL, 0, &tcp_do_autosndbuf, 0,
   1759 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   1760 	sysctl_createv(clog, 0, NULL, NULL,
   1761 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1762 		       CTLTYPE_INT, "sendbuf_inc",
   1763 		       SYSCTL_DESCR("Incrementor step size of "
   1764 		           "automatic send buffer"),
   1765 		       NULL, 0, &tcp_autosndbuf_inc, 0,
   1766 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   1767 	sysctl_createv(clog, 0, NULL, NULL,
   1768 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1769 		       CTLTYPE_INT, "sendbuf_max",
   1770 		       SYSCTL_DESCR("Max size of automatic send buffer"),
   1771 		       NULL, 0, &tcp_autosndbuf_max, 0,
   1772 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   1773 
   1774 	/* ECN subtree */
   1775 	sysctl_createv(clog, 0, NULL, &ecn_node,
   1776 	    	       CTLFLAG_PERMANENT,
   1777 		       CTLTYPE_NODE, "ecn",
   1778 	    	       SYSCTL_DESCR("RFC3168 Explicit Congestion Notification"),
   1779 	    	       NULL, 0, NULL, 0,
   1780 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   1781 	sysctl_createv(clog, 0, &ecn_node, NULL,
   1782 	    	       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1783 		       CTLTYPE_INT, "enable",
   1784 		       SYSCTL_DESCR("Enable TCP Explicit Congestion "
   1785 			   "Notification"),
   1786 	    	       NULL, 0, &tcp_do_ecn, 0, CTL_CREATE, CTL_EOL);
   1787 	sysctl_createv(clog, 0, &ecn_node, NULL,
   1788 	    	       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1789 		       CTLTYPE_INT, "maxretries",
   1790 		       SYSCTL_DESCR("Number of times to retry ECN setup "
   1791 			       "before disabling ECN on the connection"),
   1792 	    	       NULL, 0, &tcp_ecn_maxretries, 0, CTL_CREATE, CTL_EOL);
   1793 
   1794 	/* SACK gets it's own little subtree. */
   1795 	sysctl_createv(clog, 0, NULL, &sack_node,
   1796 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1797 		       CTLTYPE_INT, "enable",
   1798 		       SYSCTL_DESCR("Enable RFC2018 Selective ACKnowledgement"),
   1799 		       NULL, 0, &tcp_do_sack, 0,
   1800 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
   1801 	sysctl_createv(clog, 0, NULL, &sack_node,
   1802 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1803 		       CTLTYPE_INT, "maxholes",
   1804 		       SYSCTL_DESCR("Maximum number of TCP SACK holes allowed per connection"),
   1805 		       NULL, 0, &tcp_sack_tp_maxholes, 0,
   1806 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
   1807 	sysctl_createv(clog, 0, NULL, &sack_node,
   1808 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1809 		       CTLTYPE_INT, "globalmaxholes",
   1810 		       SYSCTL_DESCR("Global maximum number of TCP SACK holes"),
   1811 		       NULL, 0, &tcp_sack_globalmaxholes, 0,
   1812 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
   1813 	sysctl_createv(clog, 0, NULL, &sack_node,
   1814 		       CTLFLAG_PERMANENT,
   1815 		       CTLTYPE_INT, "globalholes",
   1816 		       SYSCTL_DESCR("Global number of TCP SACK holes"),
   1817 		       NULL, 0, &tcp_sack_globalholes, 0,
   1818 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
   1819 
   1820 	sysctl_createv(clog, 0, NULL, NULL,
   1821 		       CTLFLAG_PERMANENT,
   1822 		       CTLTYPE_STRUCT, "stats",
   1823 		       SYSCTL_DESCR("TCP statistics"),
   1824 		       sysctl_net_inet_tcp_stats, 0, NULL, 0,
   1825 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_STATS,
   1826 		       CTL_EOL);
   1827         sysctl_createv(clog, 0, NULL, NULL,
   1828                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1829                        CTLTYPE_INT, "local_by_rtt",
   1830                        SYSCTL_DESCR("Use RTT estimator to decide which hosts "
   1831 				    "are local"),
   1832 		       NULL, 0, &tcp_rttlocal, 0,
   1833 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   1834 #ifdef TCP_DEBUG
   1835 	sysctl_createv(clog, 0, NULL, NULL,
   1836 		       CTLFLAG_PERMANENT,
   1837 		       CTLTYPE_STRUCT, "debug",
   1838 		       SYSCTL_DESCR("TCP sockets debug information"),
   1839 		       NULL, 0, &tcp_debug, sizeof(tcp_debug),
   1840 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBUG,
   1841 		       CTL_EOL);
   1842 	sysctl_createv(clog, 0, NULL, NULL,
   1843 		       CTLFLAG_PERMANENT,
   1844 		       CTLTYPE_INT, "debx",
   1845 		       SYSCTL_DESCR("Number of TCP debug sockets messages"),
   1846 		       NULL, 0, &tcp_debx, sizeof(tcp_debx),
   1847 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBX,
   1848 		       CTL_EOL);
   1849 #endif
   1850 	sysctl_createv(clog, 0, NULL, NULL,
   1851 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1852 		       CTLTYPE_STRUCT, "drop",
   1853 		       SYSCTL_DESCR("TCP drop connection"),
   1854 		       sysctl_net_inet_tcp_drop, 0, NULL, 0,
   1855 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DROP, CTL_EOL);
   1856 	sysctl_createv(clog, 0, NULL, NULL,
   1857 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1858 		       CTLTYPE_INT, "iss_hash",
   1859 		       SYSCTL_DESCR("Enable RFC 1948 ISS by cryptographic "
   1860 				    "hash computation"),
   1861 		       NULL, 0, &tcp_do_rfc1948, sizeof(tcp_do_rfc1948),
   1862 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE,
   1863 		       CTL_EOL);
   1864 
   1865 	/* ABC subtree */
   1866 
   1867 	sysctl_createv(clog, 0, NULL, &abc_node,
   1868 		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "abc",
   1869 		       SYSCTL_DESCR("RFC3465 Appropriate Byte Counting (ABC)"),
   1870 		       NULL, 0, NULL, 0,
   1871 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   1872 	sysctl_createv(clog, 0, &abc_node, NULL,
   1873 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1874 		       CTLTYPE_INT, "enable",
   1875 		       SYSCTL_DESCR("Enable RFC3465 Appropriate Byte Counting"),
   1876 		       NULL, 0, &tcp_do_abc, 0, CTL_CREATE, CTL_EOL);
   1877 	sysctl_createv(clog, 0, &abc_node, NULL,
   1878 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1879 		       CTLTYPE_INT, "aggressive",
   1880 		       SYSCTL_DESCR("1: L=2*SMSS 0: L=1*SMSS"),
   1881 		       NULL, 0, &tcp_abc_aggressive, 0, CTL_CREATE, CTL_EOL);
   1882 
   1883 	/* MSL tuning subtree */
   1884 
   1885 	sysctl_createv(clog, 0, NULL, &mslt_node,
   1886 		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "mslt",
   1887 		       SYSCTL_DESCR("MSL Tuning for TIME_WAIT truncation"),
   1888 		       NULL, 0, NULL, 0,
   1889 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   1890 	sysctl_createv(clog, 0, &mslt_node, NULL,
   1891 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1892 		       CTLTYPE_INT, "enable",
   1893 		       SYSCTL_DESCR("Enable TIME_WAIT truncation"),
   1894 		       NULL, 0, &tcp_msl_enable, 0, CTL_CREATE, CTL_EOL);
   1895 	sysctl_createv(clog, 0, &mslt_node, NULL,
   1896 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1897 		       CTLTYPE_INT, "loopback",
   1898 		       SYSCTL_DESCR("MSL value to use for loopback connections"),
   1899 		       NULL, 0, &tcp_msl_loop, 0, CTL_CREATE, CTL_EOL);
   1900 	sysctl_createv(clog, 0, &mslt_node, NULL,
   1901 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1902 		       CTLTYPE_INT, "local",
   1903 		       SYSCTL_DESCR("MSL value to use for local connections"),
   1904 		       NULL, 0, &tcp_msl_local, 0, CTL_CREATE, CTL_EOL);
   1905 	sysctl_createv(clog, 0, &mslt_node, NULL,
   1906 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1907 		       CTLTYPE_INT, "remote",
   1908 		       SYSCTL_DESCR("MSL value to use for remote connections"),
   1909 		       NULL, 0, &tcp_msl_remote, 0, CTL_CREATE, CTL_EOL);
   1910 	sysctl_createv(clog, 0, &mslt_node, NULL,
   1911 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1912 		       CTLTYPE_INT, "remote_threshold",
   1913 		       SYSCTL_DESCR("RTT estimate value to promote local to remote"),
   1914 		       NULL, 0, &tcp_msl_remote_threshold, 0, CTL_CREATE, CTL_EOL);
   1915 
   1916 	/* vestigial TIME_WAIT tuning subtree */
   1917 
   1918 	sysctl_createv(clog, 0, NULL, &vtw_node,
   1919 		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "vtw",
   1920 		       SYSCTL_DESCR("Tuning for Vestigial TIME_WAIT"),
   1921 		       NULL, 0, NULL, 0,
   1922 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
   1923 	sysctl_createv(clog, 0, &vtw_node, NULL,
   1924 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1925 		       CTLTYPE_INT, "enable",
   1926 		       SYSCTL_DESCR("Enable Vestigial TIME_WAIT"),
   1927 		       sysctl_tcp_vtw_enable, 0,
   1928 	               (pf == AF_INET) ? &tcp4_vtw_enable : &tcp6_vtw_enable,
   1929 		       0, CTL_CREATE, CTL_EOL);
   1930 	sysctl_createv(clog, 0, &vtw_node, NULL,
   1931 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
   1932 		       CTLTYPE_INT, "entries",
   1933 		       SYSCTL_DESCR("Maximum number of vestigial TIME_WAIT entries"),
   1934 		       NULL, 0, &tcp_vtw_entries, 0, CTL_CREATE, CTL_EOL);
   1935 }
   1936 
   1937 void
   1938 tcp_usrreq_init(void)
   1939 {
   1940 
   1941 #ifdef INET
   1942 	sysctl_net_inet_tcp_setup2(NULL, PF_INET, "inet", "tcp");
   1943 #endif
   1944 #ifdef INET6
   1945 	sysctl_net_inet_tcp_setup2(NULL, PF_INET6, "inet6", "tcp6");
   1946 #endif
   1947 }
   1948