Home | History | Annotate | Line # | Download | only in netinet
tcp_usrreq.c revision 1.84
      1  1.84       tls /*	$NetBSD: tcp_usrreq.c,v 1.84 2003/09/29 21:39:35 tls Exp $	*/
      2  1.40    itojun 
      3  1.40    itojun /*
      4  1.40    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  1.40    itojun  * All rights reserved.
      6  1.71    itojun  *
      7  1.40    itojun  * Redistribution and use in source and binary forms, with or without
      8  1.40    itojun  * modification, are permitted provided that the following conditions
      9  1.40    itojun  * are met:
     10  1.40    itojun  * 1. Redistributions of source code must retain the above copyright
     11  1.40    itojun  *    notice, this list of conditions and the following disclaimer.
     12  1.40    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.40    itojun  *    notice, this list of conditions and the following disclaimer in the
     14  1.40    itojun  *    documentation and/or other materials provided with the distribution.
     15  1.40    itojun  * 3. Neither the name of the project nor the names of its contributors
     16  1.40    itojun  *    may be used to endorse or promote products derived from this software
     17  1.40    itojun  *    without specific prior written permission.
     18  1.71    itojun  *
     19  1.40    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  1.40    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.40    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.40    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  1.40    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.40    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.40    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.40    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.40    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.40    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.40    itojun  * SUCH DAMAGE.
     30  1.40    itojun  */
     31  1.34   thorpej 
     32  1.34   thorpej /*-
     33  1.34   thorpej  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
     34  1.34   thorpej  * All rights reserved.
     35  1.34   thorpej  *
     36  1.34   thorpej  * This code is derived from software contributed to The NetBSD Foundation
     37  1.34   thorpej  * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
     38  1.34   thorpej  * Facility, NASA Ames Research Center.
     39  1.34   thorpej  *
     40  1.34   thorpej  * Redistribution and use in source and binary forms, with or without
     41  1.34   thorpej  * modification, are permitted provided that the following conditions
     42  1.34   thorpej  * are met:
     43  1.34   thorpej  * 1. Redistributions of source code must retain the above copyright
     44  1.34   thorpej  *    notice, this list of conditions and the following disclaimer.
     45  1.34   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     46  1.34   thorpej  *    notice, this list of conditions and the following disclaimer in the
     47  1.34   thorpej  *    documentation and/or other materials provided with the distribution.
     48  1.34   thorpej  * 3. All advertising materials mentioning features or use of this software
     49  1.34   thorpej  *    must display the following acknowledgement:
     50  1.34   thorpej  *	This product includes software developed by the NetBSD
     51  1.34   thorpej  *	Foundation, Inc. and its contributors.
     52  1.34   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     53  1.34   thorpej  *    contributors may be used to endorse or promote products derived
     54  1.34   thorpej  *    from this software without specific prior written permission.
     55  1.34   thorpej  *
     56  1.34   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     57  1.34   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     58  1.34   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     59  1.34   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     60  1.34   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     61  1.34   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     62  1.34   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     63  1.34   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     64  1.34   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     65  1.34   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     66  1.34   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     67  1.34   thorpej  */
     68  1.10       cgd 
     69   1.1       cgd /*
     70  1.33   thorpej  * Copyright (c) 1982, 1986, 1988, 1993, 1995
     71   1.9   mycroft  *	The Regents of the University of California.  All rights reserved.
     72   1.1       cgd  *
     73   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     74   1.1       cgd  * modification, are permitted provided that the following conditions
     75   1.1       cgd  * are met:
     76   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     77   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     78   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     79   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     80   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     81  1.82       agc  * 3. Neither the name of the University nor the names of its contributors
     82   1.1       cgd  *    may be used to endorse or promote products derived from this software
     83   1.1       cgd  *    without specific prior written permission.
     84   1.1       cgd  *
     85   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     86   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     87   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     88   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     89   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     90   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     91   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     92   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     93   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     94   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     95   1.1       cgd  * SUCH DAMAGE.
     96   1.1       cgd  *
     97  1.33   thorpej  *	@(#)tcp_usrreq.c	8.5 (Berkeley) 6/21/95
     98   1.1       cgd  */
     99  1.67     lukem 
    100  1.67     lukem #include <sys/cdefs.h>
    101  1.84       tls __KERNEL_RCSID(0, "$NetBSD: tcp_usrreq.c,v 1.84 2003/09/29 21:39:35 tls Exp $");
    102   1.1       cgd 
    103  1.40    itojun #include "opt_inet.h"
    104  1.42   thorpej #include "opt_ipsec.h"
    105  1.63       abs #include "opt_tcp_debug.h"
    106  1.77    martin #include "opt_mbuftrace.h"
    107  1.40    itojun 
    108   1.5   mycroft #include <sys/param.h>
    109   1.5   mycroft #include <sys/systm.h>
    110  1.13     glass #include <sys/kernel.h>
    111   1.5   mycroft #include <sys/malloc.h>
    112   1.5   mycroft #include <sys/mbuf.h>
    113   1.5   mycroft #include <sys/socket.h>
    114   1.5   mycroft #include <sys/socketvar.h>
    115   1.5   mycroft #include <sys/protosw.h>
    116   1.5   mycroft #include <sys/errno.h>
    117   1.5   mycroft #include <sys/stat.h>
    118  1.20  christos #include <sys/proc.h>
    119  1.40    itojun #include <sys/domain.h>
    120  1.20  christos #include <sys/sysctl.h>
    121   1.1       cgd 
    122   1.5   mycroft #include <net/if.h>
    123   1.5   mycroft #include <net/route.h>
    124   1.1       cgd 
    125   1.5   mycroft #include <netinet/in.h>
    126   1.5   mycroft #include <netinet/in_systm.h>
    127  1.14       cgd #include <netinet/in_var.h>
    128   1.5   mycroft #include <netinet/ip.h>
    129   1.5   mycroft #include <netinet/in_pcb.h>
    130   1.5   mycroft #include <netinet/ip_var.h>
    131  1.40    itojun 
    132  1.40    itojun #ifdef INET6
    133  1.40    itojun #ifndef INET
    134  1.40    itojun #include <netinet/in.h>
    135  1.40    itojun #endif
    136  1.40    itojun #include <netinet/ip6.h>
    137  1.40    itojun #include <netinet6/in6_pcb.h>
    138  1.40    itojun #include <netinet6/ip6_var.h>
    139  1.40    itojun #endif
    140  1.40    itojun 
    141   1.5   mycroft #include <netinet/tcp.h>
    142   1.5   mycroft #include <netinet/tcp_fsm.h>
    143   1.5   mycroft #include <netinet/tcp_seq.h>
    144   1.5   mycroft #include <netinet/tcp_timer.h>
    145   1.5   mycroft #include <netinet/tcp_var.h>
    146   1.5   mycroft #include <netinet/tcpip.h>
    147   1.5   mycroft #include <netinet/tcp_debug.h>
    148  1.26   thorpej 
    149  1.68     lukem #include "opt_tcp_space.h"
    150   1.1       cgd 
    151  1.40    itojun #ifdef IPSEC
    152  1.40    itojun #include <netinet6/ipsec.h>
    153  1.40    itojun #endif /*IPSEC*/
    154  1.40    itojun 
    155   1.1       cgd /*
    156   1.1       cgd  * TCP protocol interface to socket abstraction.
    157   1.1       cgd  */
    158   1.1       cgd extern	char *tcpstates[];
    159   1.1       cgd 
    160  1.78  christos static int tcp_sysctl_ident(int *, u_int, void *, size_t *, void *, size_t);
    161  1.76  christos 
    162   1.1       cgd /*
    163   1.1       cgd  * Process a TCP user request for TCP tb.  If this is a send request
    164   1.1       cgd  * then m is the mbuf chain of send data.  If this is a timer expiration
    165   1.1       cgd  * (called from the software clock routine), then timertype tells which timer.
    166   1.1       cgd  */
    167   1.1       cgd /*ARGSUSED*/
    168   1.6   mycroft int
    169  1.81      fvdl tcp_usrreq(so, req, m, nam, control, p)
    170   1.1       cgd 	struct socket *so;
    171   1.1       cgd 	int req;
    172   1.1       cgd 	struct mbuf *m, *nam, *control;
    173  1.81      fvdl 	struct proc *p;
    174  1.79   darrenr {
    175  1.49  augustss 	struct inpcb *inp;
    176  1.40    itojun #ifdef INET6
    177  1.49  augustss 	struct in6pcb *in6p;
    178  1.40    itojun #endif
    179  1.49  augustss 	struct tcpcb *tp = NULL;
    180   1.1       cgd 	int s;
    181   1.1       cgd 	int error = 0;
    182  1.74    simonb #ifdef TCP_DEBUG
    183  1.74    simonb 	int ostate = 0;
    184  1.74    simonb #endif
    185  1.40    itojun 	int family;	/* family of the socket */
    186  1.40    itojun 
    187  1.40    itojun 	family = so->so_proto->pr_domain->dom_family;
    188   1.1       cgd 
    189  1.40    itojun 	if (req == PRU_CONTROL) {
    190  1.40    itojun 		switch (family) {
    191  1.56    itojun #ifdef INET
    192  1.40    itojun 		case PF_INET:
    193  1.40    itojun 			return (in_control(so, (long)m, (caddr_t)nam,
    194  1.40    itojun 			    (struct ifnet *)control, p));
    195  1.56    itojun #endif
    196  1.40    itojun #ifdef INET6
    197  1.40    itojun 		case PF_INET6:
    198  1.40    itojun 			return (in6_control(so, (long)m, (caddr_t)nam,
    199  1.40    itojun 			    (struct ifnet *)control, p));
    200  1.40    itojun #endif
    201  1.40    itojun 		default:
    202  1.40    itojun 			return EAFNOSUPPORT;
    203  1.40    itojun 		}
    204  1.45   thorpej 	}
    205  1.45   thorpej 
    206  1.46   thorpej 	if (req == PRU_PURGEIF) {
    207  1.54     enami 		switch (family) {
    208  1.56    itojun #ifdef INET
    209  1.54     enami 		case PF_INET:
    210  1.62    itojun 			in_pcbpurgeif0(&tcbtable, (struct ifnet *)control);
    211  1.54     enami 			in_purgeif((struct ifnet *)control);
    212  1.54     enami 			in_pcbpurgeif(&tcbtable, (struct ifnet *)control);
    213  1.55     enami 			break;
    214  1.56    itojun #endif
    215  1.46   thorpej #ifdef INET6
    216  1.54     enami 		case PF_INET6:
    217  1.83    itojun 			in6_pcbpurgeif0(&tcbtable, (struct ifnet *)control);
    218  1.54     enami 			in6_purgeif((struct ifnet *)control);
    219  1.83    itojun 			in6_pcbpurgeif(&tcbtable, (struct ifnet *)control);
    220  1.55     enami 			break;
    221  1.46   thorpej #endif
    222  1.54     enami 		default:
    223  1.54     enami 			return (EAFNOSUPPORT);
    224  1.54     enami 		}
    225  1.45   thorpej 		return (0);
    226  1.40    itojun 	}
    227  1.22   mycroft 
    228  1.22   mycroft 	s = splsoftnet();
    229  1.40    itojun 	switch (family) {
    230  1.56    itojun #ifdef INET
    231  1.40    itojun 	case PF_INET:
    232  1.40    itojun 		inp = sotoinpcb(so);
    233  1.40    itojun #ifdef INET6
    234  1.40    itojun 		in6p = NULL;
    235  1.40    itojun #endif
    236  1.40    itojun 		break;
    237  1.56    itojun #endif
    238  1.40    itojun #ifdef INET6
    239  1.40    itojun 	case PF_INET6:
    240  1.40    itojun 		inp = NULL;
    241  1.40    itojun 		in6p = sotoin6pcb(so);
    242  1.40    itojun 		break;
    243  1.40    itojun #endif
    244  1.40    itojun 	default:
    245  1.40    itojun 		splx(s);
    246  1.40    itojun 		return EAFNOSUPPORT;
    247  1.40    itojun 	}
    248  1.40    itojun 
    249  1.23   mycroft #ifdef DIAGNOSTIC
    250  1.57    itojun #ifdef INET6
    251  1.56    itojun 	if (inp && in6p)
    252  1.56    itojun 		panic("tcp_usrreq: both inp and in6p set to non-NULL");
    253  1.57    itojun #endif
    254  1.23   mycroft 	if (req != PRU_SEND && req != PRU_SENDOOB && control)
    255  1.23   mycroft 		panic("tcp_usrreq: unexpected control mbuf");
    256  1.23   mycroft #endif
    257   1.1       cgd 	/*
    258   1.1       cgd 	 * When a TCP is attached to a socket, then there will be
    259   1.1       cgd 	 * a (struct inpcb) pointed at by the socket, and this
    260   1.1       cgd 	 * structure will point at a subsidary (struct tcpcb).
    261   1.1       cgd 	 */
    262  1.40    itojun #ifndef INET6
    263  1.40    itojun 	if (inp == 0 && req != PRU_ATTACH)
    264  1.40    itojun #else
    265  1.40    itojun 	if ((inp == 0 && in6p == 0) && req != PRU_ATTACH)
    266  1.40    itojun #endif
    267  1.40    itojun 	{
    268  1.22   mycroft 		error = EINVAL;
    269  1.22   mycroft 		goto release;
    270   1.1       cgd 	}
    271  1.56    itojun #ifdef INET
    272   1.1       cgd 	if (inp) {
    273   1.1       cgd 		tp = intotcpcb(inp);
    274   1.1       cgd 		/* WHAT IF TP IS 0? */
    275   1.1       cgd #ifdef KPROF
    276   1.1       cgd 		tcp_acounts[tp->t_state][req]++;
    277   1.1       cgd #endif
    278  1.74    simonb #ifdef TCP_DEBUG
    279   1.1       cgd 		ostate = tp->t_state;
    280  1.74    simonb #endif
    281  1.40    itojun 	}
    282  1.56    itojun #endif
    283  1.40    itojun #ifdef INET6
    284  1.56    itojun 	if (in6p) {
    285  1.40    itojun 		tp = in6totcpcb(in6p);
    286  1.40    itojun 		/* WHAT IF TP IS 0? */
    287  1.40    itojun #ifdef KPROF
    288  1.40    itojun 		tcp_acounts[tp->t_state][req]++;
    289  1.40    itojun #endif
    290  1.74    simonb #ifdef TCP_DEBUG
    291  1.40    itojun 		ostate = tp->t_state;
    292  1.74    simonb #endif
    293  1.40    itojun 	}
    294  1.40    itojun #endif
    295  1.22   mycroft 
    296   1.1       cgd 	switch (req) {
    297   1.1       cgd 
    298   1.1       cgd 	/*
    299   1.1       cgd 	 * TCP attaches to socket via PRU_ATTACH, reserving space,
    300   1.1       cgd 	 * and an internet control block.
    301   1.1       cgd 	 */
    302   1.1       cgd 	case PRU_ATTACH:
    303  1.40    itojun #ifndef INET6
    304  1.40    itojun 		if (inp != 0)
    305  1.40    itojun #else
    306  1.40    itojun 		if (inp != 0 || in6p != 0)
    307  1.40    itojun #endif
    308  1.40    itojun 		{
    309   1.1       cgd 			error = EISCONN;
    310   1.1       cgd 			break;
    311   1.1       cgd 		}
    312   1.1       cgd 		error = tcp_attach(so);
    313   1.1       cgd 		if (error)
    314   1.1       cgd 			break;
    315   1.1       cgd 		if ((so->so_options & SO_LINGER) && so->so_linger == 0)
    316  1.32   thorpej 			so->so_linger = TCP_LINGERTIME;
    317   1.1       cgd 		tp = sototcpcb(so);
    318   1.1       cgd 		break;
    319   1.1       cgd 
    320   1.1       cgd 	/*
    321   1.1       cgd 	 * PRU_DETACH detaches the TCP protocol from the socket.
    322   1.1       cgd 	 */
    323   1.1       cgd 	case PRU_DETACH:
    324  1.24    kleink 		tp = tcp_disconnect(tp);
    325   1.1       cgd 		break;
    326   1.1       cgd 
    327   1.1       cgd 	/*
    328   1.1       cgd 	 * Give the socket an address.
    329   1.1       cgd 	 */
    330   1.1       cgd 	case PRU_BIND:
    331  1.40    itojun 		switch (family) {
    332  1.56    itojun #ifdef INET
    333  1.40    itojun 		case PF_INET:
    334  1.40    itojun 			error = in_pcbbind(inp, nam, p);
    335  1.40    itojun 			break;
    336  1.56    itojun #endif
    337  1.40    itojun #ifdef INET6
    338  1.40    itojun 		case PF_INET6:
    339  1.51    itojun 			error = in6_pcbbind(in6p, nam, p);
    340  1.58    itojun 			if (!error) {
    341  1.58    itojun 				/* mapped addr case */
    342  1.58    itojun 				if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
    343  1.58    itojun 					tp->t_family = AF_INET;
    344  1.58    itojun 				else
    345  1.58    itojun 					tp->t_family = AF_INET6;
    346  1.58    itojun 			}
    347  1.40    itojun 			break;
    348  1.40    itojun #endif
    349  1.40    itojun 		}
    350   1.1       cgd 		break;
    351   1.1       cgd 
    352   1.1       cgd 	/*
    353   1.1       cgd 	 * Prepare to accept connections.
    354   1.1       cgd 	 */
    355   1.1       cgd 	case PRU_LISTEN:
    356  1.56    itojun #ifdef INET
    357  1.40    itojun 		if (inp && inp->inp_lport == 0) {
    358  1.21   mycroft 			error = in_pcbbind(inp, (struct mbuf *)0,
    359  1.21   mycroft 			    (struct proc *)0);
    360  1.22   mycroft 			if (error)
    361  1.22   mycroft 				break;
    362  1.22   mycroft 		}
    363  1.56    itojun #endif
    364  1.40    itojun #ifdef INET6
    365  1.56    itojun 		if (in6p && in6p->in6p_lport == 0) {
    366  1.51    itojun 			error = in6_pcbbind(in6p, (struct mbuf *)0,
    367  1.51    itojun 			    (struct proc *)0);
    368  1.40    itojun 			if (error)
    369  1.40    itojun 				break;
    370  1.40    itojun 		}
    371  1.40    itojun #endif
    372  1.22   mycroft 		tp->t_state = TCPS_LISTEN;
    373   1.1       cgd 		break;
    374   1.1       cgd 
    375   1.1       cgd 	/*
    376   1.1       cgd 	 * Initiate connection to peer.
    377   1.1       cgd 	 * Create a template for use in transmissions on this connection.
    378   1.1       cgd 	 * Enter SYN_SENT state, and mark socket as connecting.
    379   1.1       cgd 	 * Start keep-alive timer, and seed output sequence space.
    380   1.1       cgd 	 * Send initial segment on connection.
    381   1.1       cgd 	 */
    382   1.1       cgd 	case PRU_CONNECT:
    383  1.56    itojun #ifdef INET
    384  1.40    itojun 		if (inp) {
    385  1.40    itojun 			if (inp->inp_lport == 0) {
    386  1.40    itojun 				error = in_pcbbind(inp, (struct mbuf *)0,
    387  1.40    itojun 				    (struct proc *)0);
    388  1.40    itojun 				if (error)
    389  1.40    itojun 					break;
    390  1.40    itojun 			}
    391  1.40    itojun 			error = in_pcbconnect(inp, nam);
    392  1.40    itojun 		}
    393  1.56    itojun #endif
    394  1.40    itojun #ifdef INET6
    395  1.56    itojun 		if (in6p) {
    396  1.40    itojun 			if (in6p->in6p_lport == 0) {
    397  1.51    itojun 				error = in6_pcbbind(in6p, (struct mbuf *)0,
    398  1.51    itojun 				    (struct proc *)0);
    399  1.40    itojun 				if (error)
    400  1.40    itojun 					break;
    401  1.40    itojun 			}
    402  1.40    itojun 			error = in6_pcbconnect(in6p, nam);
    403  1.58    itojun 			if (!error) {
    404  1.58    itojun 				/* mapped addr case */
    405  1.58    itojun 				if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr))
    406  1.58    itojun 					tp->t_family = AF_INET;
    407  1.58    itojun 				else
    408  1.58    itojun 					tp->t_family = AF_INET6;
    409  1.58    itojun 			}
    410   1.1       cgd 		}
    411  1.40    itojun #endif
    412   1.1       cgd 		if (error)
    413   1.1       cgd 			break;
    414   1.1       cgd 		tp->t_template = tcp_template(tp);
    415   1.1       cgd 		if (tp->t_template == 0) {
    416  1.56    itojun #ifdef INET
    417  1.40    itojun 			if (inp)
    418  1.40    itojun 				in_pcbdisconnect(inp);
    419  1.56    itojun #endif
    420  1.40    itojun #ifdef INET6
    421  1.56    itojun 			if (in6p)
    422  1.40    itojun 				in6_pcbdisconnect(in6p);
    423  1.40    itojun #endif
    424   1.1       cgd 			error = ENOBUFS;
    425   1.1       cgd 			break;
    426   1.1       cgd 		}
    427   1.9   mycroft 		/* Compute window scaling to request.  */
    428   1.9   mycroft 		while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
    429   1.9   mycroft 		    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
    430   1.9   mycroft 			tp->request_r_scale++;
    431   1.1       cgd 		soisconnecting(so);
    432   1.1       cgd 		tcpstat.tcps_connattempt++;
    433   1.1       cgd 		tp->t_state = TCPS_SYN_SENT;
    434  1.37   thorpej 		TCP_TIMER_ARM(tp, TCPT_KEEP, TCPTV_KEEP_INIT);
    435  1.61   thorpej 		tp->iss = tcp_new_iss(tp, 0);
    436   1.1       cgd 		tcp_sendseqinit(tp);
    437   1.1       cgd 		error = tcp_output(tp);
    438   1.1       cgd 		break;
    439   1.1       cgd 
    440   1.1       cgd 	/*
    441   1.1       cgd 	 * Create a TCP connection between two sockets.
    442   1.1       cgd 	 */
    443   1.1       cgd 	case PRU_CONNECT2:
    444   1.1       cgd 		error = EOPNOTSUPP;
    445   1.1       cgd 		break;
    446   1.1       cgd 
    447   1.1       cgd 	/*
    448   1.1       cgd 	 * Initiate disconnect from peer.
    449   1.1       cgd 	 * If connection never passed embryonic stage, just drop;
    450   1.1       cgd 	 * else if don't need to let data drain, then can just drop anyways,
    451   1.1       cgd 	 * else have to begin TCP shutdown process: mark socket disconnecting,
    452   1.1       cgd 	 * drain unread data, state switch to reflect user close, and
    453   1.1       cgd 	 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
    454   1.1       cgd 	 * when peer sends FIN and acks ours.
    455   1.1       cgd 	 *
    456   1.1       cgd 	 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
    457   1.1       cgd 	 */
    458   1.1       cgd 	case PRU_DISCONNECT:
    459   1.1       cgd 		tp = tcp_disconnect(tp);
    460   1.1       cgd 		break;
    461   1.1       cgd 
    462   1.1       cgd 	/*
    463   1.1       cgd 	 * Accept a connection.  Essentially all the work is
    464   1.1       cgd 	 * done at higher levels; just return the address
    465   1.1       cgd 	 * of the peer, storing through addr.
    466   1.1       cgd 	 */
    467   1.8   mycroft 	case PRU_ACCEPT:
    468  1.56    itojun #ifdef INET
    469  1.40    itojun 		if (inp)
    470  1.40    itojun 			in_setpeeraddr(inp, nam);
    471  1.56    itojun #endif
    472  1.40    itojun #ifdef INET6
    473  1.56    itojun 		if (in6p)
    474  1.40    itojun 			in6_setpeeraddr(in6p, nam);
    475  1.40    itojun #endif
    476   1.1       cgd 		break;
    477   1.1       cgd 
    478   1.1       cgd 	/*
    479   1.1       cgd 	 * Mark the connection as being incapable of further output.
    480   1.1       cgd 	 */
    481   1.1       cgd 	case PRU_SHUTDOWN:
    482   1.1       cgd 		socantsendmore(so);
    483   1.1       cgd 		tp = tcp_usrclosed(tp);
    484   1.1       cgd 		if (tp)
    485   1.1       cgd 			error = tcp_output(tp);
    486   1.1       cgd 		break;
    487   1.1       cgd 
    488   1.1       cgd 	/*
    489   1.1       cgd 	 * After a receive, possibly send window update to peer.
    490   1.1       cgd 	 */
    491   1.1       cgd 	case PRU_RCVD:
    492  1.60    itojun 		/*
    493  1.60    itojun 		 * soreceive() calls this function when a user receives
    494  1.60    itojun 		 * ancillary data on a listening socket. We don't call
    495  1.60    itojun 		 * tcp_output in such a case, since there is no header
    496  1.60    itojun 		 * template for a listening socket and hence the kernel
    497  1.60    itojun 		 * will panic.
    498  1.60    itojun 		 */
    499  1.60    itojun 		if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) != 0)
    500  1.60    itojun 			(void) tcp_output(tp);
    501   1.1       cgd 		break;
    502   1.1       cgd 
    503   1.1       cgd 	/*
    504   1.1       cgd 	 * Do a send by putting data in output queue and updating urgent
    505   1.1       cgd 	 * marker if URG set.  Possibly send more data.
    506   1.1       cgd 	 */
    507   1.1       cgd 	case PRU_SEND:
    508  1.23   mycroft 		if (control && control->m_len) {
    509  1.23   mycroft 			m_freem(control);
    510  1.23   mycroft 			m_freem(m);
    511  1.23   mycroft 			error = EINVAL;
    512  1.23   mycroft 			break;
    513  1.23   mycroft 		}
    514  1.73   thorpej 		sbappendstream(&so->so_snd, m);
    515   1.1       cgd 		error = tcp_output(tp);
    516   1.1       cgd 		break;
    517   1.1       cgd 
    518   1.1       cgd 	/*
    519   1.1       cgd 	 * Abort the TCP.
    520   1.1       cgd 	 */
    521   1.1       cgd 	case PRU_ABORT:
    522   1.1       cgd 		tp = tcp_drop(tp, ECONNABORTED);
    523   1.1       cgd 		break;
    524   1.1       cgd 
    525   1.1       cgd 	case PRU_SENSE:
    526  1.22   mycroft 		/*
    527  1.22   mycroft 		 * stat: don't bother with a blocksize.
    528  1.22   mycroft 		 */
    529  1.22   mycroft 		splx(s);
    530   1.1       cgd 		return (0);
    531   1.1       cgd 
    532   1.1       cgd 	case PRU_RCVOOB:
    533  1.23   mycroft 		if (control && control->m_len) {
    534  1.23   mycroft 			m_freem(control);
    535  1.23   mycroft 			m_freem(m);
    536  1.23   mycroft 			error = EINVAL;
    537  1.23   mycroft 			break;
    538  1.23   mycroft 		}
    539   1.1       cgd 		if ((so->so_oobmark == 0 &&
    540   1.1       cgd 		    (so->so_state & SS_RCVATMARK) == 0) ||
    541   1.1       cgd 		    so->so_options & SO_OOBINLINE ||
    542   1.1       cgd 		    tp->t_oobflags & TCPOOB_HADDATA) {
    543   1.1       cgd 			error = EINVAL;
    544   1.1       cgd 			break;
    545   1.1       cgd 		}
    546   1.1       cgd 		if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
    547   1.1       cgd 			error = EWOULDBLOCK;
    548   1.1       cgd 			break;
    549   1.1       cgd 		}
    550   1.1       cgd 		m->m_len = 1;
    551   1.1       cgd 		*mtod(m, caddr_t) = tp->t_iobc;
    552  1.14       cgd 		if (((long)nam & MSG_PEEK) == 0)
    553   1.1       cgd 			tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
    554   1.1       cgd 		break;
    555   1.1       cgd 
    556   1.1       cgd 	case PRU_SENDOOB:
    557   1.1       cgd 		if (sbspace(&so->so_snd) < -512) {
    558   1.1       cgd 			m_freem(m);
    559   1.1       cgd 			error = ENOBUFS;
    560   1.1       cgd 			break;
    561   1.1       cgd 		}
    562   1.1       cgd 		/*
    563   1.1       cgd 		 * According to RFC961 (Assigned Protocols),
    564   1.1       cgd 		 * the urgent pointer points to the last octet
    565   1.1       cgd 		 * of urgent data.  We continue, however,
    566   1.1       cgd 		 * to consider it to indicate the first octet
    567   1.1       cgd 		 * of data past the urgent section.
    568   1.1       cgd 		 * Otherwise, snd_up should be one lower.
    569   1.1       cgd 		 */
    570  1.73   thorpej 		sbappendstream(&so->so_snd, m);
    571   1.1       cgd 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
    572   1.1       cgd 		tp->t_force = 1;
    573   1.1       cgd 		error = tcp_output(tp);
    574   1.1       cgd 		tp->t_force = 0;
    575   1.1       cgd 		break;
    576   1.1       cgd 
    577   1.1       cgd 	case PRU_SOCKADDR:
    578  1.56    itojun #ifdef INET
    579  1.40    itojun 		if (inp)
    580  1.40    itojun 			in_setsockaddr(inp, nam);
    581  1.56    itojun #endif
    582  1.40    itojun #ifdef INET6
    583  1.56    itojun 		if (in6p)
    584  1.40    itojun 			in6_setsockaddr(in6p, nam);
    585  1.40    itojun #endif
    586   1.1       cgd 		break;
    587   1.1       cgd 
    588   1.1       cgd 	case PRU_PEERADDR:
    589  1.56    itojun #ifdef INET
    590  1.40    itojun 		if (inp)
    591  1.40    itojun 			in_setpeeraddr(inp, nam);
    592  1.56    itojun #endif
    593  1.40    itojun #ifdef INET6
    594  1.56    itojun 		if (in6p)
    595  1.40    itojun 			in6_setpeeraddr(in6p, nam);
    596  1.40    itojun #endif
    597   1.1       cgd 		break;
    598   1.1       cgd 
    599   1.1       cgd 	default:
    600   1.1       cgd 		panic("tcp_usrreq");
    601   1.1       cgd 	}
    602  1.63       abs #ifdef TCP_DEBUG
    603   1.1       cgd 	if (tp && (so->so_options & SO_DEBUG))
    604  1.40    itojun 		tcp_trace(TA_USER, ostate, tp, NULL, req);
    605  1.63       abs #endif
    606  1.22   mycroft 
    607  1.22   mycroft release:
    608   1.1       cgd 	splx(s);
    609   1.1       cgd 	return (error);
    610   1.1       cgd }
    611   1.1       cgd 
    612   1.6   mycroft int
    613   1.1       cgd tcp_ctloutput(op, so, level, optname, mp)
    614   1.1       cgd 	int op;
    615   1.1       cgd 	struct socket *so;
    616   1.1       cgd 	int level, optname;
    617   1.1       cgd 	struct mbuf **mp;
    618   1.1       cgd {
    619   1.9   mycroft 	int error = 0, s;
    620   1.9   mycroft 	struct inpcb *inp;
    621  1.40    itojun #ifdef INET6
    622  1.49  augustss 	struct in6pcb *in6p;
    623  1.40    itojun #endif
    624  1.49  augustss 	struct tcpcb *tp;
    625  1.49  augustss 	struct mbuf *m;
    626  1.49  augustss 	int i;
    627  1.40    itojun 	int family;	/* family of the socket */
    628  1.40    itojun 
    629  1.40    itojun 	family = so->so_proto->pr_domain->dom_family;
    630   1.1       cgd 
    631  1.16   mycroft 	s = splsoftnet();
    632  1.40    itojun 	switch (family) {
    633  1.56    itojun #ifdef INET
    634  1.40    itojun 	case PF_INET:
    635  1.40    itojun 		inp = sotoinpcb(so);
    636  1.40    itojun #ifdef INET6
    637  1.40    itojun 		in6p = NULL;
    638  1.40    itojun #endif
    639  1.40    itojun 		break;
    640  1.56    itojun #endif
    641  1.40    itojun #ifdef INET6
    642  1.40    itojun 	case PF_INET6:
    643  1.40    itojun 		inp = NULL;
    644  1.40    itojun 		in6p = sotoin6pcb(so);
    645  1.40    itojun 		break;
    646  1.40    itojun #endif
    647  1.40    itojun 	default:
    648  1.40    itojun 		splx(s);
    649  1.40    itojun 		return EAFNOSUPPORT;
    650  1.40    itojun 	}
    651  1.40    itojun #ifndef INET6
    652  1.40    itojun 	if (inp == NULL)
    653  1.40    itojun #else
    654  1.40    itojun 	if (inp == NULL && in6p == NULL)
    655  1.40    itojun #endif
    656  1.40    itojun 	{
    657   1.9   mycroft 		splx(s);
    658   1.9   mycroft 		if (op == PRCO_SETOPT && *mp)
    659   1.9   mycroft 			(void) m_free(*mp);
    660   1.9   mycroft 		return (ECONNRESET);
    661   1.9   mycroft 	}
    662   1.9   mycroft 	if (level != IPPROTO_TCP) {
    663  1.40    itojun 		switch (family) {
    664  1.56    itojun #ifdef INET
    665  1.40    itojun 		case PF_INET:
    666  1.40    itojun 			error = ip_ctloutput(op, so, level, optname, mp);
    667  1.40    itojun 			break;
    668  1.56    itojun #endif
    669  1.40    itojun #ifdef INET6
    670  1.40    itojun 		case PF_INET6:
    671  1.40    itojun 			error = ip6_ctloutput(op, so, level, optname, mp);
    672  1.40    itojun 			break;
    673  1.40    itojun #endif
    674  1.40    itojun 		}
    675   1.9   mycroft 		splx(s);
    676   1.9   mycroft 		return (error);
    677   1.9   mycroft 	}
    678  1.40    itojun 	if (inp)
    679  1.40    itojun 		tp = intotcpcb(inp);
    680  1.40    itojun #ifdef INET6
    681  1.40    itojun 	else if (in6p)
    682  1.40    itojun 		tp = in6totcpcb(in6p);
    683  1.40    itojun #endif
    684  1.40    itojun 	else
    685  1.40    itojun 		tp = NULL;
    686   1.1       cgd 
    687   1.1       cgd 	switch (op) {
    688   1.1       cgd 
    689   1.1       cgd 	case PRCO_SETOPT:
    690   1.1       cgd 		m = *mp;
    691   1.1       cgd 		switch (optname) {
    692   1.1       cgd 
    693   1.1       cgd 		case TCP_NODELAY:
    694   1.1       cgd 			if (m == NULL || m->m_len < sizeof (int))
    695   1.1       cgd 				error = EINVAL;
    696   1.1       cgd 			else if (*mtod(m, int *))
    697   1.1       cgd 				tp->t_flags |= TF_NODELAY;
    698   1.1       cgd 			else
    699   1.1       cgd 				tp->t_flags &= ~TF_NODELAY;
    700   1.1       cgd 			break;
    701   1.1       cgd 
    702   1.9   mycroft 		case TCP_MAXSEG:
    703  1.71    itojun 			if (m && (i = *mtod(m, int *)) > 0 &&
    704  1.28       kml 			    i <= tp->t_peermss)
    705  1.28       kml 				tp->t_peermss = i;  /* limit on send size */
    706   1.9   mycroft 			else
    707   1.9   mycroft 				error = EINVAL;
    708   1.9   mycroft 			break;
    709   1.9   mycroft 
    710   1.1       cgd 		default:
    711   1.9   mycroft 			error = ENOPROTOOPT;
    712   1.1       cgd 			break;
    713   1.1       cgd 		}
    714   1.1       cgd 		if (m)
    715   1.1       cgd 			(void) m_free(m);
    716   1.1       cgd 		break;
    717   1.1       cgd 
    718   1.1       cgd 	case PRCO_GETOPT:
    719   1.1       cgd 		*mp = m = m_get(M_WAIT, MT_SOOPTS);
    720   1.1       cgd 		m->m_len = sizeof(int);
    721  1.75      matt 		MCLAIM(m, so->so_mowner);
    722   1.1       cgd 
    723   1.1       cgd 		switch (optname) {
    724   1.1       cgd 		case TCP_NODELAY:
    725   1.1       cgd 			*mtod(m, int *) = tp->t_flags & TF_NODELAY;
    726   1.1       cgd 			break;
    727   1.1       cgd 		case TCP_MAXSEG:
    728  1.28       kml 			*mtod(m, int *) = tp->t_peermss;
    729   1.1       cgd 			break;
    730   1.1       cgd 		default:
    731   1.9   mycroft 			error = ENOPROTOOPT;
    732   1.1       cgd 			break;
    733   1.1       cgd 		}
    734   1.1       cgd 		break;
    735   1.1       cgd 	}
    736   1.9   mycroft 	splx(s);
    737   1.1       cgd 	return (error);
    738   1.1       cgd }
    739   1.1       cgd 
    740  1.11   mycroft #ifndef TCP_SENDSPACE
    741  1.84       tls #define	TCP_SENDSPACE	1024*32
    742  1.11   mycroft #endif
    743  1.25   thorpej int	tcp_sendspace = TCP_SENDSPACE;
    744  1.11   mycroft #ifndef TCP_RECVSPACE
    745  1.84       tls #define	TCP_RECVSPACE	1024*32
    746  1.11   mycroft #endif
    747  1.25   thorpej int	tcp_recvspace = TCP_RECVSPACE;
    748   1.1       cgd 
    749   1.1       cgd /*
    750   1.1       cgd  * Attach TCP protocol to socket, allocating
    751   1.1       cgd  * internet protocol control block, tcp control block,
    752   1.1       cgd  * bufer space, and entering LISTEN state if to accept connections.
    753   1.1       cgd  */
    754   1.6   mycroft int
    755   1.1       cgd tcp_attach(so)
    756   1.1       cgd 	struct socket *so;
    757   1.1       cgd {
    758  1.49  augustss 	struct tcpcb *tp;
    759   1.1       cgd 	struct inpcb *inp;
    760  1.40    itojun #ifdef INET6
    761  1.40    itojun 	struct in6pcb *in6p;
    762  1.40    itojun #endif
    763   1.1       cgd 	int error;
    764  1.40    itojun 	int family;	/* family of the socket */
    765  1.40    itojun 
    766  1.40    itojun 	family = so->so_proto->pr_domain->dom_family;
    767   1.1       cgd 
    768  1.75      matt #ifdef MBUFTRACE
    769  1.75      matt 	so->so_mowner = &tcp_mowner;
    770  1.75      matt 	so->so_rcv.sb_mowner = &tcp_rx_mowner;
    771  1.75      matt 	so->so_snd.sb_mowner = &tcp_tx_mowner;
    772  1.75      matt #endif
    773   1.1       cgd 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    774   1.1       cgd 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
    775   1.1       cgd 		if (error)
    776   1.1       cgd 			return (error);
    777   1.1       cgd 	}
    778  1.40    itojun 	switch (family) {
    779  1.56    itojun #ifdef INET
    780  1.40    itojun 	case PF_INET:
    781  1.40    itojun 		error = in_pcballoc(so, &tcbtable);
    782  1.40    itojun 		if (error)
    783  1.40    itojun 			return (error);
    784  1.40    itojun 		inp = sotoinpcb(so);
    785  1.40    itojun #ifdef INET6
    786  1.40    itojun 		in6p = NULL;
    787  1.40    itojun #endif
    788  1.40    itojun 		break;
    789  1.56    itojun #endif
    790  1.40    itojun #ifdef INET6
    791  1.40    itojun 	case PF_INET6:
    792  1.83    itojun 		error = in6_pcballoc(so, &tcbtable);
    793  1.40    itojun 		if (error)
    794  1.40    itojun 			return (error);
    795  1.40    itojun 		inp = NULL;
    796  1.40    itojun 		in6p = sotoin6pcb(so);
    797  1.40    itojun 		break;
    798  1.40    itojun #endif
    799  1.40    itojun 	default:
    800  1.40    itojun 		return EAFNOSUPPORT;
    801  1.40    itojun 	}
    802  1.40    itojun 	if (inp)
    803  1.40    itojun 		tp = tcp_newtcpcb(family, (void *)inp);
    804  1.40    itojun #ifdef INET6
    805  1.40    itojun 	else if (in6p)
    806  1.40    itojun 		tp = tcp_newtcpcb(family, (void *)in6p);
    807  1.40    itojun #endif
    808  1.41    itojun 	else
    809  1.41    itojun 		tp = NULL;
    810  1.40    itojun 
    811   1.1       cgd 	if (tp == 0) {
    812   1.1       cgd 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
    813   1.1       cgd 
    814   1.1       cgd 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
    815  1.56    itojun #ifdef INET
    816  1.40    itojun 		if (inp)
    817  1.40    itojun 			in_pcbdetach(inp);
    818  1.56    itojun #endif
    819  1.40    itojun #ifdef INET6
    820  1.56    itojun 		if (in6p)
    821  1.40    itojun 			in6_pcbdetach(in6p);
    822  1.40    itojun #endif
    823   1.1       cgd 		so->so_state |= nofd;
    824   1.1       cgd 		return (ENOBUFS);
    825   1.1       cgd 	}
    826   1.1       cgd 	tp->t_state = TCPS_CLOSED;
    827   1.1       cgd 	return (0);
    828   1.1       cgd }
    829   1.1       cgd 
    830   1.1       cgd /*
    831   1.1       cgd  * Initiate (or continue) disconnect.
    832   1.1       cgd  * If embryonic state, just send reset (once).
    833   1.1       cgd  * If in ``let data drain'' option and linger null, just drop.
    834   1.1       cgd  * Otherwise (hard), mark socket disconnecting and drop
    835   1.1       cgd  * current input data; switch states based on user close, and
    836   1.1       cgd  * send segment to peer (with FIN).
    837   1.1       cgd  */
    838   1.1       cgd struct tcpcb *
    839   1.1       cgd tcp_disconnect(tp)
    840  1.49  augustss 	struct tcpcb *tp;
    841   1.1       cgd {
    842  1.40    itojun 	struct socket *so;
    843  1.40    itojun 
    844  1.40    itojun 	if (tp->t_inpcb)
    845  1.40    itojun 		so = tp->t_inpcb->inp_socket;
    846  1.40    itojun #ifdef INET6
    847  1.40    itojun 	else if (tp->t_in6pcb)
    848  1.40    itojun 		so = tp->t_in6pcb->in6p_socket;
    849  1.40    itojun #endif
    850  1.40    itojun 	else
    851  1.40    itojun 		so = NULL;
    852   1.1       cgd 
    853  1.12   mycroft 	if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
    854   1.1       cgd 		tp = tcp_close(tp);
    855   1.1       cgd 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
    856   1.1       cgd 		tp = tcp_drop(tp, 0);
    857   1.1       cgd 	else {
    858   1.1       cgd 		soisdisconnecting(so);
    859   1.1       cgd 		sbflush(&so->so_rcv);
    860   1.1       cgd 		tp = tcp_usrclosed(tp);
    861   1.1       cgd 		if (tp)
    862   1.1       cgd 			(void) tcp_output(tp);
    863   1.1       cgd 	}
    864   1.1       cgd 	return (tp);
    865   1.1       cgd }
    866   1.1       cgd 
    867   1.1       cgd /*
    868   1.1       cgd  * User issued close, and wish to trail through shutdown states:
    869   1.1       cgd  * if never received SYN, just forget it.  If got a SYN from peer,
    870   1.1       cgd  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
    871   1.1       cgd  * If already got a FIN from peer, then almost done; go to LAST_ACK
    872   1.1       cgd  * state.  In all other cases, have already sent FIN to peer (e.g.
    873   1.1       cgd  * after PRU_SHUTDOWN), and just have to play tedious game waiting
    874   1.1       cgd  * for peer to send FIN or not respond to keep-alives, etc.
    875   1.1       cgd  * We can let the user exit from the close as soon as the FIN is acked.
    876   1.1       cgd  */
    877   1.1       cgd struct tcpcb *
    878   1.1       cgd tcp_usrclosed(tp)
    879  1.49  augustss 	struct tcpcb *tp;
    880   1.1       cgd {
    881   1.1       cgd 
    882   1.1       cgd 	switch (tp->t_state) {
    883   1.1       cgd 
    884   1.1       cgd 	case TCPS_CLOSED:
    885   1.1       cgd 	case TCPS_LISTEN:
    886   1.1       cgd 	case TCPS_SYN_SENT:
    887   1.1       cgd 		tp->t_state = TCPS_CLOSED;
    888   1.1       cgd 		tp = tcp_close(tp);
    889   1.1       cgd 		break;
    890   1.1       cgd 
    891   1.1       cgd 	case TCPS_SYN_RECEIVED:
    892   1.1       cgd 	case TCPS_ESTABLISHED:
    893   1.1       cgd 		tp->t_state = TCPS_FIN_WAIT_1;
    894   1.1       cgd 		break;
    895   1.1       cgd 
    896   1.1       cgd 	case TCPS_CLOSE_WAIT:
    897   1.1       cgd 		tp->t_state = TCPS_LAST_ACK;
    898   1.1       cgd 		break;
    899   1.1       cgd 	}
    900  1.18   mycroft 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
    901  1.40    itojun 		struct socket *so;
    902  1.40    itojun 		if (tp->t_inpcb)
    903  1.40    itojun 			so = tp->t_inpcb->inp_socket;
    904  1.40    itojun #ifdef INET6
    905  1.40    itojun 		else if (tp->t_in6pcb)
    906  1.40    itojun 			so = tp->t_in6pcb->in6p_socket;
    907  1.40    itojun #endif
    908  1.40    itojun 		else
    909  1.40    itojun 			so = NULL;
    910  1.40    itojun 		soisdisconnected(so);
    911  1.19   mycroft 		/*
    912  1.19   mycroft 		 * If we are in FIN_WAIT_2, we arrived here because the
    913  1.19   mycroft 		 * application did a shutdown of the send side.  Like the
    914  1.19   mycroft 		 * case of a transition from FIN_WAIT_1 to FIN_WAIT_2 after
    915  1.19   mycroft 		 * a full close, we start a timer to make sure sockets are
    916  1.19   mycroft 		 * not left in FIN_WAIT_2 forever.
    917  1.19   mycroft 		 */
    918  1.38     mouse 		if ((tp->t_state == TCPS_FIN_WAIT_2) && (tcp_maxidle > 0))
    919  1.37   thorpej 			TCP_TIMER_ARM(tp, TCPT_2MSL, tcp_maxidle);
    920  1.18   mycroft 	}
    921   1.1       cgd 	return (tp);
    922  1.17   thorpej }
    923  1.17   thorpej 
    924  1.59  jdolecek static const struct {
    925  1.38     mouse 	 unsigned int valid : 1;
    926  1.38     mouse 	 unsigned int rdonly : 1;
    927  1.38     mouse 	 int *var;
    928  1.38     mouse 	 int val;
    929  1.38     mouse 	 } tcp_ctlvars[] = TCPCTL_VARIABLES;
    930  1.36      matt 
    931  1.17   thorpej /*
    932  1.17   thorpej  * Sysctl for tcp variables.
    933  1.17   thorpej  */
    934  1.17   thorpej int
    935  1.17   thorpej tcp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
    936  1.17   thorpej 	int *name;
    937  1.17   thorpej 	u_int namelen;
    938  1.17   thorpej 	void *oldp;
    939  1.17   thorpej 	size_t *oldlenp;
    940  1.17   thorpej 	void *newp;
    941  1.17   thorpej 	size_t newlen;
    942  1.17   thorpej {
    943  1.69    martin 	int error, saved_value = 0;
    944  1.48   thorpej 
    945  1.78  christos 	if (name[0] == TCPCTL_IDENT)
    946  1.78  christos 		return tcp_sysctl_ident(&name[1], namelen - 1, oldp, oldlenp,
    947  1.78  christos 		    newp, newlen);
    948  1.78  christos 
    949  1.80    simonb 	/* All remaining sysctl names at this level are terminal. */
    950  1.17   thorpej 	if (namelen != 1)
    951  1.17   thorpej 		return (ENOTDIR);
    952  1.17   thorpej 
    953  1.36      matt 	if (name[0] < sizeof(tcp_ctlvars)/sizeof(tcp_ctlvars[0])
    954  1.39        tv 	    && tcp_ctlvars[name[0]].valid) {
    955  1.69    martin 		if (tcp_ctlvars[name[0]].rdonly) {
    956  1.71    itojun 			return (sysctl_rdint(oldp, oldlenp, newp,
    957  1.38     mouse 			    tcp_ctlvars[name[0]].val));
    958  1.69    martin 		} else {
    959  1.69    martin 			switch (name[0]) {
    960  1.69    martin 			case TCPCTL_MSSDFLT:
    961  1.70    martin 				saved_value = tcp_mssdflt;
    962  1.70    martin 				break;
    963  1.69    martin 			}
    964  1.70    martin 			error = sysctl_int(oldp, oldlenp, newp, newlen,
    965  1.70    martin 			    tcp_ctlvars[name[0]].var);
    966  1.69    martin 			if (error)
    967  1.70    martin 				return (error);
    968  1.69    martin 			switch (name[0]) {
    969  1.69    martin 			case TCPCTL_MSSDFLT:
    970  1.70    martin 				if (tcp_mssdflt < 32) {
    971  1.70    martin 					tcp_mssdflt = saved_value;
    972  1.70    martin 					return (EINVAL);
    973  1.70    martin 				}
    974  1.70    martin 				break;
    975  1.69    martin 			}
    976  1.70    martin 			return (0);
    977  1.69    martin 		}
    978  1.39        tv 	}
    979  1.38     mouse 
    980  1.36      matt 	return (ENOPROTOOPT);
    981  1.76  christos }
    982  1.76  christos 
    983  1.76  christos 
    984  1.76  christos static int
    985  1.78  christos tcp_sysctl_ident(int *name, u_int namelen, void *oldp, size_t *oldlenp,
    986  1.78  christos     void *newp, size_t newlen)
    987  1.76  christos {
    988  1.76  christos 	struct inpcb *inb;
    989  1.78  christos 	struct in_addr laddr, raddr;
    990  1.78  christos 	u_int lport, rport;
    991  1.76  christos 	uid_t uid;
    992  1.76  christos 	int error;
    993  1.76  christos 
    994  1.76  christos 	if (*oldlenp != sizeof(uid_t))
    995  1.76  christos 		return ENOMEM;
    996  1.76  christos 	if (!oldp || *oldlenp != sizeof(uid_t))
    997  1.76  christos 		return ENOMEM;
    998  1.78  christos 	if (namelen != 4)
    999  1.78  christos 		return EINVAL;
   1000  1.78  christos 
   1001  1.78  christos 	raddr.s_addr = (uint32_t)name[0];
   1002  1.78  christos 	rport = (u_int)name[1];
   1003  1.78  christos 	laddr.s_addr = (uint32_t)name[2];
   1004  1.78  christos 	lport = (u_int)name[3];
   1005  1.78  christos 
   1006  1.78  christos 	inb = in_pcblookup_connect(&tcbtable, raddr, rport, laddr, lport);
   1007  1.76  christos 	if (inb) {
   1008  1.78  christos 		struct socket *sockp = inb->inp_socket;
   1009  1.76  christos 		if (sockp)
   1010  1.76  christos 			uid = sockp->so_uid;
   1011  1.76  christos 		else
   1012  1.76  christos 			return ESRCH;
   1013  1.76  christos 	} else
   1014  1.76  christos 		return ESRCH;
   1015  1.76  christos 
   1016  1.76  christos 	if ((error = copyout(&uid, oldp, sizeof(uid))) != 0)
   1017  1.76  christos 		return error;
   1018  1.76  christos 
   1019  1.76  christos 	return 0;
   1020   1.1       cgd }
   1021