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