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