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