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