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