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