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