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