Home | History | Annotate | Line # | Download | only in netinet
tcp_usrreq.c revision 1.81.2.2
      1 /*	$NetBSD: tcp_usrreq.c,v 1.81.2.2 2004/08/03 10:54:46 skrll 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 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  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  * 3. All advertising materials mentioning features or use of this software
     49  *    must display the following acknowledgement:
     50  *	This product includes software developed by the NetBSD
     51  *	Foundation, Inc. and its contributors.
     52  * 4. Neither the name of The NetBSD Foundation nor the names of its
     53  *    contributors may be used to endorse or promote products derived
     54  *    from this software without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     57  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     58  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     59  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     60  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     61  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     62  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     63  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     64  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     66  * POSSIBILITY OF SUCH DAMAGE.
     67  */
     68 
     69 /*
     70  * Copyright (c) 1982, 1986, 1988, 1993, 1995
     71  *	The Regents of the University of California.  All rights reserved.
     72  *
     73  * Redistribution and use in source and binary forms, with or without
     74  * modification, are permitted provided that the following conditions
     75  * are met:
     76  * 1. Redistributions of source code must retain the above copyright
     77  *    notice, this list of conditions and the following disclaimer.
     78  * 2. Redistributions in binary form must reproduce the above copyright
     79  *    notice, this list of conditions and the following disclaimer in the
     80  *    documentation and/or other materials provided with the distribution.
     81  * 3. Neither the name of the University nor the names of its contributors
     82  *    may be used to endorse or promote products derived from this software
     83  *    without specific prior written permission.
     84  *
     85  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     86  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     87  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     88  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     89  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     90  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     91  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     92  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     93  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     94  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     95  * SUCH DAMAGE.
     96  *
     97  *	@(#)tcp_usrreq.c	8.5 (Berkeley) 6/21/95
     98  */
     99 
    100 #include <sys/cdefs.h>
    101 __KERNEL_RCSID(0, "$NetBSD: tcp_usrreq.c,v 1.81.2.2 2004/08/03 10:54:46 skrll Exp $");
    102 
    103 #include "opt_inet.h"
    104 #include "opt_ipsec.h"
    105 #include "opt_tcp_debug.h"
    106 #include "opt_mbuftrace.h"
    107 
    108 #include <sys/param.h>
    109 #include <sys/systm.h>
    110 #include <sys/kernel.h>
    111 #include <sys/malloc.h>
    112 #include <sys/mbuf.h>
    113 #include <sys/socket.h>
    114 #include <sys/socketvar.h>
    115 #include <sys/protosw.h>
    116 #include <sys/errno.h>
    117 #include <sys/stat.h>
    118 #include <sys/proc.h>
    119 #include <sys/domain.h>
    120 #include <sys/sysctl.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 
    132 #ifdef INET6
    133 #ifndef INET
    134 #include <netinet/in.h>
    135 #endif
    136 #include <netinet/ip6.h>
    137 #include <netinet6/in6_pcb.h>
    138 #include <netinet6/ip6_var.h>
    139 #endif
    140 
    141 #include <netinet/tcp.h>
    142 #include <netinet/tcp_fsm.h>
    143 #include <netinet/tcp_seq.h>
    144 #include <netinet/tcp_timer.h>
    145 #include <netinet/tcp_var.h>
    146 #include <netinet/tcpip.h>
    147 #include <netinet/tcp_debug.h>
    148 
    149 #include "opt_tcp_space.h"
    150 
    151 #ifdef IPSEC
    152 #include <netinet6/ipsec.h>
    153 #endif /*IPSEC*/
    154 
    155 /*
    156  * TCP protocol interface to socket abstraction.
    157  */
    158 
    159 /*
    160  * Process a TCP user request for TCP tb.  If this is a send request
    161  * then m is the mbuf chain of send data.  If this is a timer expiration
    162  * (called from the software clock routine), then timertype tells which timer.
    163  */
    164 /*ARGSUSED*/
    165 int
    166 tcp_usrreq(so, req, m, nam, control, l)
    167 	struct socket *so;
    168 	int req;
    169 	struct mbuf *m, *nam, *control;
    170 	struct lwp *l;
    171 {
    172 	struct proc *p;
    173 	struct inpcb *inp;
    174 #ifdef INET6
    175 	struct in6pcb *in6p;
    176 #endif
    177 	struct tcpcb *tp = NULL;
    178 	int s;
    179 	int error = 0;
    180 #ifdef TCP_DEBUG
    181 	int ostate = 0;
    182 #endif
    183 	int family;	/* family of the socket */
    184 
    185 	p = l ? l->l_proc : NULL;
    186 	family = so->so_proto->pr_domain->dom_family;
    187 
    188 	if (req == PRU_CONTROL) {
    189 		switch (family) {
    190 #ifdef INET
    191 		case PF_INET:
    192 			return (in_control(so, (long)m, (caddr_t)nam,
    193 			    (struct ifnet *)control, p));
    194 #endif
    195 #ifdef INET6
    196 		case PF_INET6:
    197 			return (in6_control(so, (long)m, (caddr_t)nam,
    198 			    (struct ifnet *)control, p));
    199 #endif
    200 		default:
    201 			return EAFNOSUPPORT;
    202 		}
    203 	}
    204 
    205 	if (req == PRU_PURGEIF) {
    206 		switch (family) {
    207 #ifdef INET
    208 		case PF_INET:
    209 			in_pcbpurgeif0(&tcbtable, (struct ifnet *)control);
    210 			in_purgeif((struct ifnet *)control);
    211 			in_pcbpurgeif(&tcbtable, (struct ifnet *)control);
    212 			break;
    213 #endif
    214 #ifdef INET6
    215 		case PF_INET6:
    216 			in6_pcbpurgeif0(&tcbtable, (struct ifnet *)control);
    217 			in6_purgeif((struct ifnet *)control);
    218 			in6_pcbpurgeif(&tcbtable, (struct ifnet *)control);
    219 			break;
    220 #endif
    221 		default:
    222 			return (EAFNOSUPPORT);
    223 		}
    224 		return (0);
    225 	}
    226 
    227 	s = splsoftnet();
    228 	switch (family) {
    229 #ifdef INET
    230 	case PF_INET:
    231 		inp = sotoinpcb(so);
    232 #ifdef INET6
    233 		in6p = NULL;
    234 #endif
    235 		break;
    236 #endif
    237 #ifdef INET6
    238 	case PF_INET6:
    239 		inp = NULL;
    240 		in6p = sotoin6pcb(so);
    241 		break;
    242 #endif
    243 	default:
    244 		splx(s);
    245 		return EAFNOSUPPORT;
    246 	}
    247 
    248 #ifdef DIAGNOSTIC
    249 #ifdef INET6
    250 	if (inp && in6p)
    251 		panic("tcp_usrreq: both inp and in6p set to non-NULL");
    252 #endif
    253 	if (req != PRU_SEND && req != PRU_SENDOOB && control)
    254 		panic("tcp_usrreq: unexpected control mbuf");
    255 #endif
    256 	/*
    257 	 * When a TCP is attached to a socket, then there will be
    258 	 * a (struct inpcb) pointed at by the socket, and this
    259 	 * structure will point at a subsidary (struct tcpcb).
    260 	 */
    261 #ifndef INET6
    262 	if (inp == 0 && req != PRU_ATTACH)
    263 #else
    264 	if ((inp == 0 && in6p == 0) && req != PRU_ATTACH)
    265 #endif
    266 	{
    267 		error = EINVAL;
    268 		goto release;
    269 	}
    270 #ifdef INET
    271 	if (inp) {
    272 		tp = intotcpcb(inp);
    273 		/* WHAT IF TP IS 0? */
    274 #ifdef KPROF
    275 		tcp_acounts[tp->t_state][req]++;
    276 #endif
    277 #ifdef TCP_DEBUG
    278 		ostate = tp->t_state;
    279 #endif
    280 	}
    281 #endif
    282 #ifdef INET6
    283 	if (in6p) {
    284 		tp = in6totcpcb(in6p);
    285 		/* WHAT IF TP IS 0? */
    286 #ifdef KPROF
    287 		tcp_acounts[tp->t_state][req]++;
    288 #endif
    289 #ifdef TCP_DEBUG
    290 		ostate = tp->t_state;
    291 #endif
    292 	}
    293 #endif
    294 
    295 	switch (req) {
    296 
    297 	/*
    298 	 * TCP attaches to socket via PRU_ATTACH, reserving space,
    299 	 * and an internet control block.
    300 	 */
    301 	case PRU_ATTACH:
    302 #ifndef INET6
    303 		if (inp != 0)
    304 #else
    305 		if (inp != 0 || in6p != 0)
    306 #endif
    307 		{
    308 			error = EISCONN;
    309 			break;
    310 		}
    311 		error = tcp_attach(so);
    312 		if (error)
    313 			break;
    314 		if ((so->so_options & SO_LINGER) && so->so_linger == 0)
    315 			so->so_linger = TCP_LINGERTIME;
    316 		tp = sototcpcb(so);
    317 		break;
    318 
    319 	/*
    320 	 * PRU_DETACH detaches the TCP protocol from the socket.
    321 	 */
    322 	case PRU_DETACH:
    323 		tp = tcp_disconnect(tp);
    324 		break;
    325 
    326 	/*
    327 	 * Give the socket an address.
    328 	 */
    329 	case PRU_BIND:
    330 		switch (family) {
    331 #ifdef INET
    332 		case PF_INET:
    333 			error = in_pcbbind(inp, nam, p);
    334 			break;
    335 #endif
    336 #ifdef INET6
    337 		case PF_INET6:
    338 			error = in6_pcbbind(in6p, nam, p);
    339 			if (!error) {
    340 				/* mapped addr case */
    341 				if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
    342 					tp->t_family = AF_INET;
    343 				else
    344 					tp->t_family = AF_INET6;
    345 			}
    346 			break;
    347 #endif
    348 		}
    349 		break;
    350 
    351 	/*
    352 	 * Prepare to accept connections.
    353 	 */
    354 	case PRU_LISTEN:
    355 #ifdef INET
    356 		if (inp && inp->inp_lport == 0) {
    357 			error = in_pcbbind(inp, (struct mbuf *)0,
    358 			    (struct proc *)0);
    359 			if (error)
    360 				break;
    361 		}
    362 #endif
    363 #ifdef INET6
    364 		if (in6p && in6p->in6p_lport == 0) {
    365 			error = in6_pcbbind(in6p, (struct mbuf *)0,
    366 			    (struct proc *)0);
    367 			if (error)
    368 				break;
    369 		}
    370 #endif
    371 		tp->t_state = TCPS_LISTEN;
    372 		break;
    373 
    374 	/*
    375 	 * Initiate connection to peer.
    376 	 * Create a template for use in transmissions on this connection.
    377 	 * Enter SYN_SENT state, and mark socket as connecting.
    378 	 * Start keep-alive timer, and seed output sequence space.
    379 	 * Send initial segment on connection.
    380 	 */
    381 	case PRU_CONNECT:
    382 #ifdef INET
    383 		if (inp) {
    384 			if (inp->inp_lport == 0) {
    385 				error = in_pcbbind(inp, (struct mbuf *)0,
    386 				    (struct proc *)0);
    387 				if (error)
    388 					break;
    389 			}
    390 			error = in_pcbconnect(inp, nam);
    391 		}
    392 #endif
    393 #ifdef INET6
    394 		if (in6p) {
    395 			if (in6p->in6p_lport == 0) {
    396 				error = in6_pcbbind(in6p, (struct mbuf *)0,
    397 				    (struct proc *)0);
    398 				if (error)
    399 					break;
    400 			}
    401 			error = in6_pcbconnect(in6p, nam);
    402 			if (!error) {
    403 				/* mapped addr case */
    404 				if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr))
    405 					tp->t_family = AF_INET;
    406 				else
    407 					tp->t_family = AF_INET6;
    408 			}
    409 		}
    410 #endif
    411 		if (error)
    412 			break;
    413 		tp->t_template = tcp_template(tp);
    414 		if (tp->t_template == 0) {
    415 #ifdef INET
    416 			if (inp)
    417 				in_pcbdisconnect(inp);
    418 #endif
    419 #ifdef INET6
    420 			if (in6p)
    421 				in6_pcbdisconnect(in6p);
    422 #endif
    423 			error = ENOBUFS;
    424 			break;
    425 		}
    426 		/* Compute window scaling to request.  */
    427 		while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
    428 		    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
    429 			tp->request_r_scale++;
    430 		soisconnecting(so);
    431 		tcpstat.tcps_connattempt++;
    432 		tp->t_state = TCPS_SYN_SENT;
    433 		TCP_TIMER_ARM(tp, TCPT_KEEP, TCPTV_KEEP_INIT);
    434 		tp->iss = tcp_new_iss(tp, 0);
    435 		tcp_sendseqinit(tp);
    436 		error = tcp_output(tp);
    437 		break;
    438 
    439 	/*
    440 	 * Create a TCP connection between two sockets.
    441 	 */
    442 	case PRU_CONNECT2:
    443 		error = EOPNOTSUPP;
    444 		break;
    445 
    446 	/*
    447 	 * Initiate disconnect from peer.
    448 	 * If connection never passed embryonic stage, just drop;
    449 	 * else if don't need to let data drain, then can just drop anyways,
    450 	 * else have to begin TCP shutdown process: mark socket disconnecting,
    451 	 * drain unread data, state switch to reflect user close, and
    452 	 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
    453 	 * when peer sends FIN and acks ours.
    454 	 *
    455 	 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
    456 	 */
    457 	case PRU_DISCONNECT:
    458 		tp = tcp_disconnect(tp);
    459 		break;
    460 
    461 	/*
    462 	 * Accept a connection.  Essentially all the work is
    463 	 * done at higher levels; just return the address
    464 	 * of the peer, storing through addr.
    465 	 */
    466 	case PRU_ACCEPT:
    467 #ifdef INET
    468 		if (inp)
    469 			in_setpeeraddr(inp, nam);
    470 #endif
    471 #ifdef INET6
    472 		if (in6p)
    473 			in6_setpeeraddr(in6p, nam);
    474 #endif
    475 		break;
    476 
    477 	/*
    478 	 * Mark the connection as being incapable of further output.
    479 	 */
    480 	case PRU_SHUTDOWN:
    481 		socantsendmore(so);
    482 		tp = tcp_usrclosed(tp);
    483 		if (tp)
    484 			error = tcp_output(tp);
    485 		break;
    486 
    487 	/*
    488 	 * After a receive, possibly send window update to peer.
    489 	 */
    490 	case PRU_RCVD:
    491 		/*
    492 		 * soreceive() calls this function when a user receives
    493 		 * ancillary data on a listening socket. We don't call
    494 		 * tcp_output in such a case, since there is no header
    495 		 * template for a listening socket and hence the kernel
    496 		 * will panic.
    497 		 */
    498 		if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) != 0)
    499 			(void) tcp_output(tp);
    500 		break;
    501 
    502 	/*
    503 	 * Do a send by putting data in output queue and updating urgent
    504 	 * marker if URG set.  Possibly send more data.
    505 	 */
    506 	case PRU_SEND:
    507 		if (control && control->m_len) {
    508 			m_freem(control);
    509 			m_freem(m);
    510 			error = EINVAL;
    511 			break;
    512 		}
    513 		sbappendstream(&so->so_snd, m);
    514 		error = tcp_output(tp);
    515 		break;
    516 
    517 	/*
    518 	 * Abort the TCP.
    519 	 */
    520 	case PRU_ABORT:
    521 		tp = tcp_drop(tp, ECONNABORTED);
    522 		break;
    523 
    524 	case PRU_SENSE:
    525 		/*
    526 		 * stat: don't bother with a blocksize.
    527 		 */
    528 		splx(s);
    529 		return (0);
    530 
    531 	case PRU_RCVOOB:
    532 		if (control && control->m_len) {
    533 			m_freem(control);
    534 			m_freem(m);
    535 			error = EINVAL;
    536 			break;
    537 		}
    538 		if ((so->so_oobmark == 0 &&
    539 		    (so->so_state & SS_RCVATMARK) == 0) ||
    540 		    so->so_options & SO_OOBINLINE ||
    541 		    tp->t_oobflags & TCPOOB_HADDATA) {
    542 			error = EINVAL;
    543 			break;
    544 		}
    545 		if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
    546 			error = EWOULDBLOCK;
    547 			break;
    548 		}
    549 		m->m_len = 1;
    550 		*mtod(m, caddr_t) = tp->t_iobc;
    551 		if (((long)nam & MSG_PEEK) == 0)
    552 			tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
    553 		break;
    554 
    555 	case PRU_SENDOOB:
    556 		if (sbspace(&so->so_snd) < -512) {
    557 			m_freem(m);
    558 			error = ENOBUFS;
    559 			break;
    560 		}
    561 		/*
    562 		 * According to RFC961 (Assigned Protocols),
    563 		 * the urgent pointer points to the last octet
    564 		 * of urgent data.  We continue, however,
    565 		 * to consider it to indicate the first octet
    566 		 * of data past the urgent section.
    567 		 * Otherwise, snd_up should be one lower.
    568 		 */
    569 		sbappendstream(&so->so_snd, m);
    570 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
    571 		tp->t_force = 1;
    572 		error = tcp_output(tp);
    573 		tp->t_force = 0;
    574 		break;
    575 
    576 	case PRU_SOCKADDR:
    577 #ifdef INET
    578 		if (inp)
    579 			in_setsockaddr(inp, nam);
    580 #endif
    581 #ifdef INET6
    582 		if (in6p)
    583 			in6_setsockaddr(in6p, nam);
    584 #endif
    585 		break;
    586 
    587 	case PRU_PEERADDR:
    588 #ifdef INET
    589 		if (inp)
    590 			in_setpeeraddr(inp, nam);
    591 #endif
    592 #ifdef INET6
    593 		if (in6p)
    594 			in6_setpeeraddr(in6p, nam);
    595 #endif
    596 		break;
    597 
    598 	default:
    599 		panic("tcp_usrreq");
    600 	}
    601 #ifdef TCP_DEBUG
    602 	if (tp && (so->so_options & SO_DEBUG))
    603 		tcp_trace(TA_USER, ostate, tp, NULL, req);
    604 #endif
    605 
    606 release:
    607 	splx(s);
    608 	return (error);
    609 }
    610 
    611 int
    612 tcp_ctloutput(op, so, level, optname, mp)
    613 	int op;
    614 	struct socket *so;
    615 	int level, optname;
    616 	struct mbuf **mp;
    617 {
    618 	int error = 0, s;
    619 	struct inpcb *inp;
    620 #ifdef INET6
    621 	struct in6pcb *in6p;
    622 #endif
    623 	struct tcpcb *tp;
    624 	struct mbuf *m;
    625 	int i;
    626 	int family;	/* family of the socket */
    627 
    628 	family = so->so_proto->pr_domain->dom_family;
    629 
    630 	s = splsoftnet();
    631 	switch (family) {
    632 #ifdef INET
    633 	case PF_INET:
    634 		inp = sotoinpcb(so);
    635 #ifdef INET6
    636 		in6p = NULL;
    637 #endif
    638 		break;
    639 #endif
    640 #ifdef INET6
    641 	case PF_INET6:
    642 		inp = NULL;
    643 		in6p = sotoin6pcb(so);
    644 		break;
    645 #endif
    646 	default:
    647 		splx(s);
    648 		return EAFNOSUPPORT;
    649 	}
    650 #ifndef INET6
    651 	if (inp == NULL)
    652 #else
    653 	if (inp == NULL && in6p == NULL)
    654 #endif
    655 	{
    656 		splx(s);
    657 		if (op == PRCO_SETOPT && *mp)
    658 			(void) m_free(*mp);
    659 		return (ECONNRESET);
    660 	}
    661 	if (level != IPPROTO_TCP) {
    662 		switch (family) {
    663 #ifdef INET
    664 		case PF_INET:
    665 			error = ip_ctloutput(op, so, level, optname, mp);
    666 			break;
    667 #endif
    668 #ifdef INET6
    669 		case PF_INET6:
    670 			error = ip6_ctloutput(op, so, level, optname, mp);
    671 			break;
    672 #endif
    673 		}
    674 		splx(s);
    675 		return (error);
    676 	}
    677 	if (inp)
    678 		tp = intotcpcb(inp);
    679 #ifdef INET6
    680 	else if (in6p)
    681 		tp = in6totcpcb(in6p);
    682 #endif
    683 	else
    684 		tp = NULL;
    685 
    686 	switch (op) {
    687 
    688 	case PRCO_SETOPT:
    689 		m = *mp;
    690 		switch (optname) {
    691 
    692 #ifdef TCP_SIGNATURE
    693 		case TCP_MD5SIG:
    694 			if (m == NULL || m->m_len < sizeof (int))
    695 				error = EINVAL;
    696 			if (error)
    697 				break;
    698 			if (*mtod(m, int *) > 0)
    699 				tp->t_flags |= TF_SIGNATURE;
    700 			else
    701 				tp->t_flags &= ~TF_SIGNATURE;
    702 			break;
    703 #endif /* TCP_SIGNATURE */
    704 
    705 		case TCP_NODELAY:
    706 			if (m == NULL || m->m_len < sizeof (int))
    707 				error = EINVAL;
    708 			else if (*mtod(m, int *))
    709 				tp->t_flags |= TF_NODELAY;
    710 			else
    711 				tp->t_flags &= ~TF_NODELAY;
    712 			break;
    713 
    714 		case TCP_MAXSEG:
    715 			if (m && (i = *mtod(m, int *)) > 0 &&
    716 			    i <= tp->t_peermss)
    717 				tp->t_peermss = i;  /* limit on send size */
    718 			else
    719 				error = EINVAL;
    720 			break;
    721 
    722 		default:
    723 			error = ENOPROTOOPT;
    724 			break;
    725 		}
    726 		if (m)
    727 			(void) m_free(m);
    728 		break;
    729 
    730 	case PRCO_GETOPT:
    731 		*mp = m = m_get(M_WAIT, MT_SOOPTS);
    732 		m->m_len = sizeof(int);
    733 		MCLAIM(m, so->so_mowner);
    734 
    735 		switch (optname) {
    736 #ifdef TCP_SIGNATURE
    737 		case TCP_MD5SIG:
    738 			*mtod(m, int *) = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
    739 			break;
    740 #endif
    741 		case TCP_NODELAY:
    742 			*mtod(m, int *) = tp->t_flags & TF_NODELAY;
    743 			break;
    744 		case TCP_MAXSEG:
    745 			*mtod(m, int *) = tp->t_peermss;
    746 			break;
    747 		default:
    748 			error = ENOPROTOOPT;
    749 			break;
    750 		}
    751 		break;
    752 	}
    753 	splx(s);
    754 	return (error);
    755 }
    756 
    757 #ifndef TCP_SENDSPACE
    758 #define	TCP_SENDSPACE	1024*32
    759 #endif
    760 int	tcp_sendspace = TCP_SENDSPACE;
    761 #ifndef TCP_RECVSPACE
    762 #define	TCP_RECVSPACE	1024*32
    763 #endif
    764 int	tcp_recvspace = TCP_RECVSPACE;
    765 
    766 /*
    767  * Attach TCP protocol to socket, allocating
    768  * internet protocol control block, tcp control block,
    769  * bufer space, and entering LISTEN state if to accept connections.
    770  */
    771 int
    772 tcp_attach(so)
    773 	struct socket *so;
    774 {
    775 	struct tcpcb *tp;
    776 	struct inpcb *inp;
    777 #ifdef INET6
    778 	struct in6pcb *in6p;
    779 #endif
    780 	int error;
    781 	int family;	/* family of the socket */
    782 
    783 	family = so->so_proto->pr_domain->dom_family;
    784 
    785 #ifdef MBUFTRACE
    786 	so->so_mowner = &tcp_mowner;
    787 	so->so_rcv.sb_mowner = &tcp_rx_mowner;
    788 	so->so_snd.sb_mowner = &tcp_tx_mowner;
    789 #endif
    790 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    791 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
    792 		if (error)
    793 			return (error);
    794 	}
    795 	switch (family) {
    796 #ifdef INET
    797 	case PF_INET:
    798 		error = in_pcballoc(so, &tcbtable);
    799 		if (error)
    800 			return (error);
    801 		inp = sotoinpcb(so);
    802 #ifdef INET6
    803 		in6p = NULL;
    804 #endif
    805 		break;
    806 #endif
    807 #ifdef INET6
    808 	case PF_INET6:
    809 		error = in6_pcballoc(so, &tcbtable);
    810 		if (error)
    811 			return (error);
    812 		inp = NULL;
    813 		in6p = sotoin6pcb(so);
    814 		break;
    815 #endif
    816 	default:
    817 		return EAFNOSUPPORT;
    818 	}
    819 	if (inp)
    820 		tp = tcp_newtcpcb(family, (void *)inp);
    821 #ifdef INET6
    822 	else if (in6p)
    823 		tp = tcp_newtcpcb(family, (void *)in6p);
    824 #endif
    825 	else
    826 		tp = NULL;
    827 
    828 	if (tp == 0) {
    829 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
    830 
    831 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
    832 #ifdef INET
    833 		if (inp)
    834 			in_pcbdetach(inp);
    835 #endif
    836 #ifdef INET6
    837 		if (in6p)
    838 			in6_pcbdetach(in6p);
    839 #endif
    840 		so->so_state |= nofd;
    841 		return (ENOBUFS);
    842 	}
    843 	tp->t_state = TCPS_CLOSED;
    844 	return (0);
    845 }
    846 
    847 /*
    848  * Initiate (or continue) disconnect.
    849  * If embryonic state, just send reset (once).
    850  * If in ``let data drain'' option and linger null, just drop.
    851  * Otherwise (hard), mark socket disconnecting and drop
    852  * current input data; switch states based on user close, and
    853  * send segment to peer (with FIN).
    854  */
    855 struct tcpcb *
    856 tcp_disconnect(tp)
    857 	struct tcpcb *tp;
    858 {
    859 	struct socket *so;
    860 
    861 	if (tp->t_inpcb)
    862 		so = tp->t_inpcb->inp_socket;
    863 #ifdef INET6
    864 	else if (tp->t_in6pcb)
    865 		so = tp->t_in6pcb->in6p_socket;
    866 #endif
    867 	else
    868 		so = NULL;
    869 
    870 	if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
    871 		tp = tcp_close(tp);
    872 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
    873 		tp = tcp_drop(tp, 0);
    874 	else {
    875 		soisdisconnecting(so);
    876 		sbflush(&so->so_rcv);
    877 		tp = tcp_usrclosed(tp);
    878 		if (tp)
    879 			(void) tcp_output(tp);
    880 	}
    881 	return (tp);
    882 }
    883 
    884 /*
    885  * User issued close, and wish to trail through shutdown states:
    886  * if never received SYN, just forget it.  If got a SYN from peer,
    887  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
    888  * If already got a FIN from peer, then almost done; go to LAST_ACK
    889  * state.  In all other cases, have already sent FIN to peer (e.g.
    890  * after PRU_SHUTDOWN), and just have to play tedious game waiting
    891  * for peer to send FIN or not respond to keep-alives, etc.
    892  * We can let the user exit from the close as soon as the FIN is acked.
    893  */
    894 struct tcpcb *
    895 tcp_usrclosed(tp)
    896 	struct tcpcb *tp;
    897 {
    898 
    899 	switch (tp->t_state) {
    900 
    901 	case TCPS_CLOSED:
    902 	case TCPS_LISTEN:
    903 	case TCPS_SYN_SENT:
    904 		tp->t_state = TCPS_CLOSED;
    905 		tp = tcp_close(tp);
    906 		break;
    907 
    908 	case TCPS_SYN_RECEIVED:
    909 	case TCPS_ESTABLISHED:
    910 		tp->t_state = TCPS_FIN_WAIT_1;
    911 		break;
    912 
    913 	case TCPS_CLOSE_WAIT:
    914 		tp->t_state = TCPS_LAST_ACK;
    915 		break;
    916 	}
    917 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
    918 		struct socket *so;
    919 		if (tp->t_inpcb)
    920 			so = tp->t_inpcb->inp_socket;
    921 #ifdef INET6
    922 		else if (tp->t_in6pcb)
    923 			so = tp->t_in6pcb->in6p_socket;
    924 #endif
    925 		else
    926 			so = NULL;
    927 		soisdisconnected(so);
    928 		/*
    929 		 * If we are in FIN_WAIT_2, we arrived here because the
    930 		 * application did a shutdown of the send side.  Like the
    931 		 * case of a transition from FIN_WAIT_1 to FIN_WAIT_2 after
    932 		 * a full close, we start a timer to make sure sockets are
    933 		 * not left in FIN_WAIT_2 forever.
    934 		 */
    935 		if ((tp->t_state == TCPS_FIN_WAIT_2) && (tcp_maxidle > 0))
    936 			TCP_TIMER_ARM(tp, TCPT_2MSL, tcp_maxidle);
    937 	}
    938 	return (tp);
    939 }
    940 
    941 /*
    942  * sysctl helper routine for net.inet.ip.mssdflt.  it can't be less
    943  * than 32.
    944  */
    945 static int
    946 sysctl_net_inet_tcp_mssdflt(SYSCTLFN_ARGS)
    947 {
    948 	int error, mssdflt;
    949 	struct sysctlnode node;
    950 
    951 	mssdflt = tcp_mssdflt;
    952 	node = *rnode;
    953 	node.sysctl_data = &mssdflt;
    954 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    955 	if (error || newp == NULL)
    956 		return (error);
    957 
    958 	if (mssdflt < 32)
    959 		return (EINVAL);
    960 	tcp_mssdflt = mssdflt;
    961 
    962 	return (0);
    963 }
    964 
    965 /*
    966  * sysctl helper routine for setting port related values under
    967  * net.inet.ip and net.inet6.ip6.  does basic range checking and does
    968  * additional checks for each type.  this code has placed in
    969  * tcp_input.c since INET and INET6 both use the same tcp code.
    970  *
    971  * this helper is not static so that both inet and inet6 can use it.
    972  */
    973 int
    974 sysctl_net_inet_ip_ports(SYSCTLFN_ARGS)
    975 {
    976 	int error, tmp;
    977 	int apmin, apmax;
    978 #ifndef IPNOPRIVPORTS
    979 	int lpmin, lpmax;
    980 #endif /* IPNOPRIVPORTS */
    981 	struct sysctlnode node;
    982 
    983 	if (namelen != 0)
    984 		return (EINVAL);
    985 
    986 	switch (name[-3]) {
    987 #ifdef INET
    988 	    case PF_INET:
    989 		apmin = anonportmin;
    990 		apmax = anonportmax;
    991 #ifndef IPNOPRIVPORTS
    992 		lpmin = lowportmin;
    993 		lpmax = lowportmax;
    994 #endif /* IPNOPRIVPORTS */
    995 		break;
    996 #endif /* INET */
    997 #ifdef INET6
    998 	    case PF_INET6:
    999 		apmin = ip6_anonportmin;
   1000 		apmax = ip6_anonportmax;
   1001 #ifndef IPNOPRIVPORTS
   1002 		lpmin = ip6_lowportmin;
   1003 		lpmax = ip6_lowportmax;
   1004 #endif /* IPNOPRIVPORTS */
   1005 		break;
   1006 #endif /* INET6 */
   1007 	    default:
   1008 		return (EINVAL);
   1009 	}
   1010 
   1011 	/*
   1012 	 * insert temporary copy into node, perform lookup on
   1013 	 * temporary, then restore pointer
   1014 	 */
   1015 	node = *rnode;
   1016 	tmp = *(int*)rnode->sysctl_data;
   1017 	node.sysctl_data = &tmp;
   1018 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1019 	if (error || newp == NULL)
   1020 		return (error);
   1021 
   1022 	/*
   1023 	 * simple port range check
   1024 	 */
   1025 	if (tmp < 0 || tmp > 65535)
   1026 		return (EINVAL);
   1027 
   1028 	/*
   1029 	 * per-node range checks
   1030 	 */
   1031 	switch (rnode->sysctl_num) {
   1032 	case IPCTL_ANONPORTMIN:
   1033 		if (tmp >= apmax)
   1034 			return (EINVAL);
   1035 #ifndef IPNOPRIVPORTS
   1036 		if (tmp < IPPORT_RESERVED)
   1037                         return (EINVAL);
   1038 #endif /* IPNOPRIVPORTS */
   1039 		break;
   1040 
   1041 	case IPCTL_ANONPORTMAX:
   1042                 if (apmin >= tmp)
   1043 			return (EINVAL);
   1044 #ifndef IPNOPRIVPORTS
   1045 		if (tmp < IPPORT_RESERVED)
   1046                         return (EINVAL);
   1047 #endif /* IPNOPRIVPORTS */
   1048 		break;
   1049 
   1050 #ifndef IPNOPRIVPORTS
   1051 	case IPCTL_LOWPORTMIN:
   1052 		if (tmp >= lpmax ||
   1053 		    tmp > IPPORT_RESERVEDMAX ||
   1054 		    tmp < IPPORT_RESERVEDMIN)
   1055 			return (EINVAL);
   1056 		break;
   1057 
   1058 	case IPCTL_LOWPORTMAX:
   1059 		if (lpmin >= tmp ||
   1060 		    tmp > IPPORT_RESERVEDMAX ||
   1061 		    tmp < IPPORT_RESERVEDMIN)
   1062 			return (EINVAL);
   1063 		break;
   1064 #endif /* IPNOPRIVPORTS */
   1065 
   1066 	default:
   1067 		return (EINVAL);
   1068 	}
   1069 
   1070 	*(int*)rnode->sysctl_data = tmp;
   1071 
   1072 	return (0);
   1073 }
   1074 
   1075 /*
   1076  * sysctl helper routine for the net.inet.tcp.ident and
   1077  * net.inet6.tcp6.ident nodes.  contains backwards compat code for the
   1078  * old way of looking up the ident information for ipv4 which involves
   1079  * stuffing the port/addr pairs into the mib lookup.
   1080  */
   1081 static int
   1082 sysctl_net_inet_tcp_ident(SYSCTLFN_ARGS)
   1083 {
   1084 #ifdef INET
   1085 	struct inpcb *inb;
   1086 	struct sockaddr_in *si4[2];
   1087 #endif /* INET */
   1088 #ifdef INET6
   1089 	struct in6pcb *in6b;
   1090 	struct sockaddr_in6 *si6[2];
   1091 #endif /* INET6 */
   1092 	struct sockaddr_storage sa[2];
   1093 	struct socket *sockp;
   1094 	size_t sz;
   1095 	uid_t uid;
   1096 	int error, pf;
   1097 
   1098 	if (namelen != 4 && namelen != 0)
   1099 		return (EINVAL);
   1100 	if (name[-2] != IPPROTO_TCP)
   1101 		return (EINVAL);
   1102 	pf = name[-3];
   1103 
   1104 	/* old style lookup, ipv4 only */
   1105 	if (namelen == 4) {
   1106 #ifdef INET
   1107 		struct in_addr laddr, raddr;
   1108 		u_int lport, rport;
   1109 
   1110 		if (pf != PF_INET)
   1111 			return (EPROTONOSUPPORT);
   1112 		raddr.s_addr = (uint32_t)name[0];
   1113 		rport = (u_int)name[1];
   1114 		laddr.s_addr = (uint32_t)name[2];
   1115 		lport = (u_int)name[3];
   1116 		inb = in_pcblookup_connect(&tcbtable, raddr, rport,
   1117 					   laddr, lport);
   1118 		if (inb == NULL || (sockp = inb->inp_socket) == NULL)
   1119 			return (ESRCH);
   1120 		uid = sockp->so_uid;
   1121 		if (oldp) {
   1122 			sz = MIN(sizeof(uid), *oldlenp);
   1123 			error = copyout(&uid, oldp, sz);
   1124 			if (error)
   1125 				return (error);
   1126 		}
   1127 		*oldlenp = sizeof(uid);
   1128 		return (0);
   1129 #else /* INET */
   1130 		return (EINVAL);
   1131 #endif /* INET */
   1132 	}
   1133 
   1134 	if (newp == NULL || newlen != sizeof(sa))
   1135 		return (EINVAL);
   1136 	error = copyin(newp, &sa, newlen);
   1137 	if (error)
   1138 		return (error);
   1139 
   1140 	/*
   1141 	 * requested families must match
   1142 	 */
   1143 	if (pf != sa[0].ss_family || sa[0].ss_family != sa[1].ss_family)
   1144 		return (EINVAL);
   1145 
   1146 	switch (pf) {
   1147 #ifdef INET
   1148 	    case PF_INET:
   1149 		si4[0] = (struct sockaddr_in*)&sa[0];
   1150 		si4[1] = (struct sockaddr_in*)&sa[1];
   1151 		if (si4[0]->sin_len != sizeof(*si4[0]) ||
   1152 		    si4[0]->sin_len != si4[1]->sin_len)
   1153 			return (EINVAL);
   1154 		inb = in_pcblookup_connect(&tcbtable,
   1155 		    si4[0]->sin_addr, si4[0]->sin_port,
   1156 		    si4[1]->sin_addr, si4[1]->sin_port);
   1157 		if (inb == NULL || (sockp = inb->inp_socket) == NULL)
   1158 			return (ESRCH);
   1159 		break;
   1160 #endif /* INET */
   1161 #ifdef INET6
   1162 	    case PF_INET6:
   1163 		si6[0] = (struct sockaddr_in6*)&sa[0];
   1164 		si6[1] = (struct sockaddr_in6*)&sa[1];
   1165 		if (si6[0]->sin6_len != sizeof(*si6[0]) ||
   1166 		    si6[0]->sin6_len != si6[1]->sin6_len)
   1167 			return (EINVAL);
   1168 		in6b = in6_pcblookup_connect(&tcbtable,
   1169 		    &si6[0]->sin6_addr, si6[0]->sin6_port,
   1170 		    &si6[1]->sin6_addr, si6[1]->sin6_port, 0);
   1171 		if (in6b == NULL || (sockp = in6b->in6p_socket) == NULL)
   1172 			return (ESRCH);
   1173 		break;
   1174 #endif /* INET6 */
   1175 	    default:
   1176 		return (EPROTONOSUPPORT);
   1177 	}
   1178 	*oldlenp = sizeof(uid);
   1179 
   1180 	uid = sockp->so_uid;
   1181 	if (oldp) {
   1182 		sz = MIN(sizeof(uid), *oldlenp);
   1183 		error = copyout(&uid, oldp, sz);
   1184 		if (error)
   1185 			return (error);
   1186 	}
   1187 	*oldlenp = sizeof(uid);
   1188 
   1189 	return (0);
   1190 }
   1191 
   1192 /*
   1193  * this (second stage) setup routine is a replacement for tcp_sysctl()
   1194  * (which is currently used for ipv4 and ipv6)
   1195  */
   1196 static void
   1197 sysctl_net_inet_tcp_setup2(struct sysctllog **clog, int pf, const char *pfname,
   1198 			   const char *tcpname)
   1199 {
   1200 
   1201 	sysctl_createv(clog, 0, NULL, NULL,
   1202 		       CTLFLAG_PERMANENT,
   1203 		       CTLTYPE_NODE, "net", NULL,
   1204 		       NULL, 0, NULL, 0,
   1205 		       CTL_NET, CTL_EOL);
   1206 	sysctl_createv(clog, 0, NULL, NULL,
   1207 		       CTLFLAG_PERMANENT,
   1208 		       CTLTYPE_NODE, pfname, NULL,
   1209 		       NULL, 0, NULL, 0,
   1210 		       CTL_NET, pf, CTL_EOL);
   1211 	sysctl_createv(clog, 0, NULL, NULL,
   1212 		       CTLFLAG_PERMANENT,
   1213 		       CTLTYPE_NODE, tcpname,
   1214 		       SYSCTL_DESCR("TCP related settings"),
   1215 		       NULL, 0, NULL, 0,
   1216 		       CTL_NET, pf, IPPROTO_TCP, CTL_EOL);
   1217 
   1218 	sysctl_createv(clog, 0, NULL, NULL,
   1219 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1220 		       CTLTYPE_INT, "rfc1323",
   1221 		       SYSCTL_DESCR("Enable RFC1323 TCP extensions"),
   1222 		       NULL, 0, &tcp_do_rfc1323, 0,
   1223 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RFC1323, CTL_EOL);
   1224 	sysctl_createv(clog, 0, NULL, NULL,
   1225 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1226 		       CTLTYPE_INT, "sendspace",
   1227 		       SYSCTL_DESCR("Default TCP send buffer size"),
   1228 		       NULL, 0, &tcp_sendspace, 0,
   1229 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SENDSPACE, CTL_EOL);
   1230 	sysctl_createv(clog, 0, NULL, NULL,
   1231 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1232 		       CTLTYPE_INT, "recvspace",
   1233 		       SYSCTL_DESCR("Default TCP receive buffer size"),
   1234 		       NULL, 0, &tcp_recvspace, 0,
   1235 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RECVSPACE, CTL_EOL);
   1236 	sysctl_createv(clog, 0, NULL, NULL,
   1237 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1238 		       CTLTYPE_INT, "mssdflt",
   1239 		       SYSCTL_DESCR("Default maximum segment size"),
   1240 		       sysctl_net_inet_tcp_mssdflt, 0, &tcp_mssdflt, 0,
   1241 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSSDFLT, CTL_EOL);
   1242 	sysctl_createv(clog, 0, NULL, NULL,
   1243 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1244 		       CTLTYPE_INT, "syn_cache_limit",
   1245 		       SYSCTL_DESCR("Maximum number of entries in the TCP "
   1246 				    "compressed state engine"),
   1247 		       NULL, 0, &tcp_syn_cache_limit, 0,
   1248 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_LIMIT,
   1249 		       CTL_EOL);
   1250 	sysctl_createv(clog, 0, NULL, NULL,
   1251 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1252 		       CTLTYPE_INT, "syn_bucket_limit",
   1253 		       SYSCTL_DESCR("Maximum number of entries per hash "
   1254 				    "bucket in the TCP compressed state "
   1255 				    "engine"),
   1256 		       NULL, 0, &tcp_syn_bucket_limit, 0,
   1257 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_BUCKET_LIMIT,
   1258 		       CTL_EOL);
   1259 #if 0 /* obsoleted */
   1260 	sysctl_createv(clog, 0, NULL, NULL,
   1261 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1262 		       CTLTYPE_INT, "syn_cache_interval",
   1263 		       SYSCTL_DESCR("TCP compressed state engine's timer interval"),
   1264 		       NULL, 0, &tcp_syn_cache_interval, 0,
   1265 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_INTER,
   1266 		       CTL_EOL);
   1267 #endif
   1268 	sysctl_createv(clog, 0, NULL, NULL,
   1269 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1270 		       CTLTYPE_INT, "init_win",
   1271 		       SYSCTL_DESCR("Initial TCP congestion window"),
   1272 		       NULL, 0, &tcp_init_win, 0,
   1273 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN, CTL_EOL);
   1274 	sysctl_createv(clog, 0, NULL, NULL,
   1275 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1276 		       CTLTYPE_INT, "mss_ifmtu",
   1277 		       SYSCTL_DESCR("Use interface MTU for calculating MSS"),
   1278 		       NULL, 0, &tcp_mss_ifmtu, 0,
   1279 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSS_IFMTU, CTL_EOL);
   1280 	sysctl_createv(clog, 0, NULL, NULL,
   1281 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1282 		       CTLTYPE_INT, "sack",
   1283 		       SYSCTL_DESCR("Enable RFC2018 Selection ACKnowledgement "
   1284 				    "(not implemented)"),
   1285 		       NULL, 0, &tcp_do_sack, 0,
   1286 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_EOL);
   1287 	sysctl_createv(clog, 0, NULL, NULL,
   1288 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1289 		       CTLTYPE_INT, "win_scale",
   1290 		       SYSCTL_DESCR("Use RFC1323 window scale options"),
   1291 		       NULL, 0, &tcp_do_win_scale, 0,
   1292 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_WSCALE, CTL_EOL);
   1293 	sysctl_createv(clog, 0, NULL, NULL,
   1294 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1295 		       CTLTYPE_INT, "timestamps",
   1296 		       SYSCTL_DESCR("Use RFC1323 time stamp options"),
   1297 		       NULL, 0, &tcp_do_timestamps, 0,
   1298 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_TSTAMP, CTL_EOL);
   1299 	sysctl_createv(clog, 0, NULL, NULL,
   1300 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1301 		       CTLTYPE_INT, "compat_42",
   1302 		       SYSCTL_DESCR("Enable workarounds for 4.2BSD TCP bugs"),
   1303 		       NULL, 0, &tcp_compat_42, 0,
   1304 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_COMPAT_42, CTL_EOL);
   1305 	sysctl_createv(clog, 0, NULL, NULL,
   1306 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1307 		       CTLTYPE_INT, "cwm",
   1308 		       SYSCTL_DESCR("Hughes/Touch/Heidemann Congestion Window "
   1309 				    "Monitoring"),
   1310 		       NULL, 0, &tcp_cwm, 0,
   1311 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM, CTL_EOL);
   1312 	sysctl_createv(clog, 0, NULL, NULL,
   1313 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1314 		       CTLTYPE_INT, "cwm_burstsize",
   1315 		       SYSCTL_DESCR("Congestion Window Monitoring allowed "
   1316 				    "burst count in packets"),
   1317 		       NULL, 0, &tcp_cwm_burstsize, 0,
   1318 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM_BURSTSIZE,
   1319 		       CTL_EOL);
   1320 	sysctl_createv(clog, 0, NULL, NULL,
   1321 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1322 		       CTLTYPE_INT, "ack_on_push",
   1323 		       SYSCTL_DESCR("Immediately return ACK when PSH is "
   1324 				    "received"),
   1325 		       NULL, 0, &tcp_ack_on_push, 0,
   1326 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_ACK_ON_PUSH, CTL_EOL);
   1327 	sysctl_createv(clog, 0, NULL, NULL,
   1328 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1329 		       CTLTYPE_INT, "keepidle",
   1330 		       SYSCTL_DESCR("Allowed connection idle ticks before a "
   1331 				    "keepalive probe is sent"),
   1332 		       NULL, 0, &tcp_keepidle, 0,
   1333 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPIDLE, CTL_EOL);
   1334 	sysctl_createv(clog, 0, NULL, NULL,
   1335 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1336 		       CTLTYPE_INT, "keepintvl",
   1337 		       SYSCTL_DESCR("Ticks before next keepalive probe is sent"),
   1338 		       NULL, 0, &tcp_keepintvl, 0,
   1339 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPINTVL, CTL_EOL);
   1340 	sysctl_createv(clog, 0, NULL, NULL,
   1341 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1342 		       CTLTYPE_INT, "keepcnt",
   1343 		       SYSCTL_DESCR("Number of keepalive probes to send"),
   1344 		       NULL, 0, &tcp_keepcnt, 0,
   1345 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPCNT, CTL_EOL);
   1346 	sysctl_createv(clog, 0, NULL, NULL,
   1347 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
   1348 		       CTLTYPE_INT, "slowhz",
   1349 		       SYSCTL_DESCR("Keepalive ticks per second"),
   1350 		       NULL, PR_SLOWHZ, NULL, 0,
   1351 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SLOWHZ, CTL_EOL);
   1352 	sysctl_createv(clog, 0, NULL, NULL,
   1353 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1354 		       CTLTYPE_INT, "newreno",
   1355 		       SYSCTL_DESCR("NewReno congestion control algorithm"),
   1356 		       NULL, 0, &tcp_do_newreno, 0,
   1357 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_NEWRENO, CTL_EOL);
   1358 	sysctl_createv(clog, 0, NULL, NULL,
   1359 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1360 		       CTLTYPE_INT, "log_refused",
   1361 		       SYSCTL_DESCR("Log refused TCP connections"),
   1362 		       NULL, 0, &tcp_log_refused, 0,
   1363 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOG_REFUSED, CTL_EOL);
   1364 #if 0 /* obsoleted */
   1365 	sysctl_createv(clog, 0, NULL, NULL,
   1366 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1367 		       CTLTYPE_INT, "rstratelimit", NULL,
   1368 		       NULL, 0, &tcp_rst_ratelim, 0,
   1369 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTRATELIMIT, CTL_EOL);
   1370 #endif
   1371 	sysctl_createv(clog, 0, NULL, NULL,
   1372 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1373 		       CTLTYPE_INT, "rstppslimit",
   1374 		       SYSCTL_DESCR("Maximum number of RST packets to send "
   1375 				    "per second"),
   1376 		       NULL, 0, &tcp_rst_ppslim, 0,
   1377 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTPPSLIMIT, CTL_EOL);
   1378 	sysctl_createv(clog, 0, NULL, NULL,
   1379 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1380 		       CTLTYPE_INT, "delack_ticks",
   1381 		       SYSCTL_DESCR("Number of ticks to delay sending an ACK"),
   1382 		       NULL, 0, &tcp_delack_ticks, 0,
   1383 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DELACK_TICKS, CTL_EOL);
   1384 	sysctl_createv(clog, 0, NULL, NULL,
   1385 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1386 		       CTLTYPE_INT, "init_win_local",
   1387 		       SYSCTL_DESCR("Initial TCP window size (in segments)"),
   1388 		       NULL, 0, &tcp_init_win_local, 0,
   1389 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN_LOCAL,
   1390 		       CTL_EOL);
   1391 	sysctl_createv(clog, 0, NULL, NULL,
   1392 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1393 		       CTLTYPE_STRUCT, "ident",
   1394 		       SYSCTL_DESCR("RFC1413 Identification Protocol lookups"),
   1395 		       sysctl_net_inet_tcp_ident, 0, NULL, sizeof(uid_t),
   1396 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_IDENT, CTL_EOL);
   1397 }
   1398 
   1399 /*
   1400  * Sysctl for tcp variables.
   1401  */
   1402 #ifdef INET
   1403 SYSCTL_SETUP(sysctl_net_inet_tcp_setup, "sysctl net.inet.tcp subtree setup")
   1404 {
   1405 
   1406 	sysctl_net_inet_tcp_setup2(clog, PF_INET, "inet", "tcp");
   1407 }
   1408 #endif /* INET */
   1409 
   1410 #ifdef INET6
   1411 SYSCTL_SETUP(sysctl_net_inet6_tcp6_setup, "sysctl net.inet6.tcp6 subtree setup")
   1412 {
   1413 
   1414 	sysctl_net_inet_tcp_setup2(clog, PF_INET6, "inet6", "tcp6");
   1415 }
   1416 #endif /* INET6 */
   1417 
   1418