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