Home | History | Annotate | Line # | Download | only in kern
uipc_usrreq.c revision 1.205
      1  1.205  riastrad /*	$NetBSD: uipc_usrreq.c,v 1.205 2024/12/06 18:36:47 riastradh Exp $	*/
      2   1.30   thorpej 
      3   1.30   thorpej /*-
      4  1.197        ad  * Copyright (c) 1998, 2000, 2004, 2008, 2009, 2020 The NetBSD Foundation, Inc.
      5   1.30   thorpej  * All rights reserved.
      6   1.30   thorpej  *
      7   1.30   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8   1.30   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.121       mrg  * NASA Ames Research Center, and by Andrew Doran.
     10   1.30   thorpej  *
     11   1.30   thorpej  * Redistribution and use in source and binary forms, with or without
     12   1.30   thorpej  * modification, are permitted provided that the following conditions
     13   1.30   thorpej  * are met:
     14   1.30   thorpej  * 1. Redistributions of source code must retain the above copyright
     15   1.30   thorpej  *    notice, this list of conditions and the following disclaimer.
     16   1.30   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.30   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18   1.30   thorpej  *    documentation and/or other materials provided with the distribution.
     19   1.30   thorpej  *
     20   1.30   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.30   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.30   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.30   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.30   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.30   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.30   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.30   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.30   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.30   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.30   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31   1.30   thorpej  */
     32   1.10       cgd 
     33    1.1       cgd /*
     34    1.8   mycroft  * Copyright (c) 1982, 1986, 1989, 1991, 1993
     35    1.8   mycroft  *	The Regents of the University of California.  All rights reserved.
     36    1.1       cgd  *
     37    1.1       cgd  * Redistribution and use in source and binary forms, with or without
     38    1.1       cgd  * modification, are permitted provided that the following conditions
     39    1.1       cgd  * are met:
     40    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     41    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     42    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     43    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     44    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     45   1.67       agc  * 3. Neither the name of the University nor the names of its contributors
     46   1.67       agc  *    may be used to endorse or promote products derived from this software
     47   1.67       agc  *    without specific prior written permission.
     48   1.67       agc  *
     49   1.67       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     50   1.67       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51   1.67       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52   1.67       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     53   1.67       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54   1.67       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55   1.67       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56   1.67       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57   1.67       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58   1.67       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59   1.67       agc  * SUCH DAMAGE.
     60   1.67       agc  *
     61   1.67       agc  *	@(#)uipc_usrreq.c	8.9 (Berkeley) 5/14/95
     62   1.67       agc  */
     63   1.67       agc 
     64   1.67       agc /*
     65   1.67       agc  * Copyright (c) 1997 Christopher G. Demetriou.  All rights reserved.
     66   1.67       agc  *
     67   1.67       agc  * Redistribution and use in source and binary forms, with or without
     68   1.67       agc  * modification, are permitted provided that the following conditions
     69   1.67       agc  * are met:
     70   1.67       agc  * 1. Redistributions of source code must retain the above copyright
     71   1.67       agc  *    notice, this list of conditions and the following disclaimer.
     72   1.67       agc  * 2. Redistributions in binary form must reproduce the above copyright
     73   1.67       agc  *    notice, this list of conditions and the following disclaimer in the
     74   1.67       agc  *    documentation and/or other materials provided with the distribution.
     75    1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     76    1.1       cgd  *    must display the following acknowledgement:
     77    1.1       cgd  *	This product includes software developed by the University of
     78    1.1       cgd  *	California, Berkeley and its contributors.
     79    1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     80    1.1       cgd  *    may be used to endorse or promote products derived from this software
     81    1.1       cgd  *    without specific prior written permission.
     82    1.1       cgd  *
     83    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     84    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     85    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     86    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     87    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     88    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     89    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     90    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     91    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     92    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     93    1.1       cgd  * SUCH DAMAGE.
     94    1.1       cgd  *
     95   1.31      fvdl  *	@(#)uipc_usrreq.c	8.9 (Berkeley) 5/14/95
     96    1.1       cgd  */
     97   1.53     lukem 
     98   1.53     lukem #include <sys/cdefs.h>
     99  1.205  riastrad __KERNEL_RCSID(0, "$NetBSD: uipc_usrreq.c,v 1.205 2024/12/06 18:36:47 riastradh Exp $");
    100  1.182       mrg 
    101  1.182       mrg #ifdef _KERNEL_OPT
    102  1.182       mrg #include "opt_compat_netbsd.h"
    103  1.182       mrg #endif
    104    1.1       cgd 
    105    1.7   mycroft #include <sys/param.h>
    106  1.204  riastrad #include <sys/types.h>
    107  1.204  riastrad 
    108  1.204  riastrad #include <sys/atomic.h>
    109  1.204  riastrad #include <sys/compat_stub.h>
    110  1.204  riastrad #include <sys/domain.h>
    111  1.204  riastrad #include <sys/file.h>
    112  1.204  riastrad #include <sys/filedesc.h>
    113  1.204  riastrad #include <sys/kauth.h>
    114  1.204  riastrad #include <sys/kernel.h>
    115  1.204  riastrad #include <sys/kmem.h>
    116  1.204  riastrad #include <sys/kthread.h>
    117  1.204  riastrad #include <sys/mbuf.h>
    118  1.204  riastrad #include <sys/namei.h>
    119    1.7   mycroft #include <sys/proc.h>
    120    1.7   mycroft #include <sys/protosw.h>
    121    1.7   mycroft #include <sys/socket.h>
    122    1.7   mycroft #include <sys/socketvar.h>
    123  1.204  riastrad #include <sys/stat.h>
    124  1.204  riastrad #include <sys/systm.h>
    125  1.204  riastrad #include <sys/uidinfo.h>
    126  1.204  riastrad #include <sys/un.h>
    127    1.7   mycroft #include <sys/unpcb.h>
    128    1.7   mycroft #include <sys/vnode.h>
    129    1.1       cgd 
    130  1.204  riastrad #include <compat/net/route_70.h>
    131  1.180       roy #include <compat/sys/socket.h>
    132  1.180       roy 
    133    1.1       cgd /*
    134    1.1       cgd  * Unix communications domain.
    135    1.1       cgd  *
    136    1.1       cgd  * TODO:
    137  1.134      manu  *	RDM
    138    1.1       cgd  *	rethink name space problems
    139    1.1       cgd  *	need a proper out-of-band
    140  1.112        ad  *
    141  1.112        ad  * Notes on locking:
    142  1.112        ad  *
    143  1.112        ad  * The generic rules noted in uipc_socket2.c apply.  In addition:
    144  1.112        ad  *
    145  1.112        ad  * o We have a global lock, uipc_lock.
    146  1.112        ad  *
    147  1.112        ad  * o All datagram sockets are locked by uipc_lock.
    148  1.112        ad  *
    149  1.112        ad  * o For stream socketpairs, the two endpoints are created sharing the same
    150  1.112        ad  *   independent lock.  Sockets presented to PRU_CONNECT2 must already have
    151  1.112        ad  *   matching locks.
    152  1.112        ad  *
    153  1.112        ad  * o Stream sockets created via socket() start life with their own
    154  1.112        ad  *   independent lock.
    155  1.205  riastrad  *
    156  1.112        ad  * o Stream connections to a named endpoint are slightly more complicated.
    157  1.112        ad  *   Sockets that have called listen() have their lock pointer mutated to
    158  1.112        ad  *   the global uipc_lock.  When establishing a connection, the connecting
    159  1.112        ad  *   socket also has its lock mutated to uipc_lock, which matches the head
    160  1.112        ad  *   (listening socket).  We create a new socket for accept() to return, and
    161  1.112        ad  *   that also shares the head's lock.  Until the connection is completely
    162  1.112        ad  *   done on both ends, all three sockets are locked by uipc_lock.  Once the
    163  1.112        ad  *   connection is complete, the association with the head's lock is broken.
    164  1.112        ad  *   The connecting socket and the socket returned from accept() have their
    165  1.112        ad  *   lock pointers mutated away from uipc_lock, and back to the connecting
    166  1.112        ad  *   socket's original, independent lock.  The head continues to be locked
    167  1.112        ad  *   by uipc_lock.
    168  1.112        ad  *
    169  1.112        ad  * o If uipc_lock is determined to be a significant source of contention,
    170  1.112        ad  *   it could easily be hashed out.  It is difficult to simply make it an
    171  1.112        ad  *   independent lock because of visibility / garbage collection issues:
    172  1.112        ad  *   if a socket has been associated with a lock at any point, that lock
    173  1.112        ad  *   must remain valid until the socket is no longer visible in the system.
    174  1.112        ad  *   The lock must not be freed or otherwise destroyed until any sockets
    175  1.112        ad  *   that had referenced it have also been destroyed.
    176    1.1       cgd  */
    177   1.93  christos const struct sockaddr_un sun_noname = {
    178  1.145  christos 	.sun_len = offsetof(struct sockaddr_un, sun_path),
    179   1.93  christos 	.sun_family = AF_LOCAL,
    180   1.93  christos };
    181    1.1       cgd ino_t	unp_ino;			/* prototype for fake inode numbers */
    182    1.1       cgd 
    183  1.164       rtr static struct mbuf * unp_addsockcred(struct lwp *, struct mbuf *);
    184  1.164       rtr static void   unp_discard_later(file_t *);
    185  1.164       rtr static void   unp_discard_now(file_t *);
    186  1.164       rtr static void   unp_disconnect1(struct unpcb *);
    187  1.164       rtr static bool   unp_drop(struct unpcb *, int);
    188  1.164       rtr static int    unp_internalize(struct mbuf **);
    189  1.164       rtr static void   unp_mark(file_t *);
    190  1.164       rtr static void   unp_scan(struct mbuf *, void (*)(file_t *), int);
    191  1.164       rtr static void   unp_shutdown1(struct unpcb *);
    192  1.164       rtr static void   unp_thread(void *);
    193  1.164       rtr static void   unp_thread_kick(void);
    194  1.164       rtr 
    195  1.112        ad static kmutex_t *uipc_lock;
    196  1.112        ad 
    197  1.121       mrg static kcondvar_t unp_thread_cv;
    198  1.121       mrg static lwp_t *unp_thread_lwp;
    199  1.121       mrg static SLIST_HEAD(,file) unp_thread_discard;
    200  1.121       mrg static int unp_defer;
    201  1.200  christos static struct sysctllog *usrreq_sysctllog;
    202  1.200  christos static void unp_sysctl_create(void);
    203  1.121       mrg 
    204  1.188  pgoyette /* Compat interface */
    205  1.188  pgoyette 
    206  1.188  pgoyette struct mbuf * stub_compat_70_unp_addsockcred(lwp_t *, struct mbuf *);
    207  1.188  pgoyette 
    208  1.188  pgoyette struct mbuf * stub_compat_70_unp_addsockcred(struct lwp *lwp,
    209  1.188  pgoyette     struct mbuf *control)
    210  1.188  pgoyette {
    211  1.188  pgoyette 
    212  1.188  pgoyette /* just copy our initial argument */
    213  1.188  pgoyette 	return control;
    214  1.188  pgoyette }
    215  1.188  pgoyette 
    216  1.191  pgoyette bool compat70_ocreds_valid = false;
    217  1.188  pgoyette 
    218  1.112        ad /*
    219  1.112        ad  * Initialize Unix protocols.
    220  1.112        ad  */
    221  1.112        ad void
    222  1.112        ad uipc_init(void)
    223  1.112        ad {
    224  1.121       mrg 	int error;
    225  1.112        ad 
    226  1.200  christos 	unp_sysctl_create();
    227  1.200  christos 
    228  1.112        ad 	uipc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    229  1.121       mrg 	cv_init(&unp_thread_cv, "unpgc");
    230  1.121       mrg 
    231  1.121       mrg 	error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, unp_thread,
    232  1.121       mrg 	    NULL, &unp_thread_lwp, "unpgc");
    233  1.121       mrg 	if (error != 0)
    234  1.121       mrg 		panic("uipc_init %d", error);
    235  1.112        ad }
    236  1.112        ad 
    237  1.183  christos static void
    238  1.183  christos unp_connid(struct lwp *l, struct unpcb *unp, int flags)
    239  1.183  christos {
    240  1.183  christos 	unp->unp_connid.unp_pid = l->l_proc->p_pid;
    241  1.183  christos 	unp->unp_connid.unp_euid = kauth_cred_geteuid(l->l_cred);
    242  1.183  christos 	unp->unp_connid.unp_egid = kauth_cred_getegid(l->l_cred);
    243  1.183  christos 	unp->unp_flags |= flags;
    244  1.183  christos }
    245  1.183  christos 
    246  1.112        ad /*
    247  1.112        ad  * A connection succeeded: disassociate both endpoints from the head's
    248  1.112        ad  * lock, and make them share their own lock.  There is a race here: for
    249  1.112        ad  * a very brief time one endpoint will be locked by a different lock
    250  1.112        ad  * than the other end.  However, since the current thread holds the old
    251  1.112        ad  * lock (the listening socket's lock, the head) access can still only be
    252  1.112        ad  * made to one side of the connection.
    253  1.112        ad  */
    254  1.112        ad static void
    255  1.112        ad unp_setpeerlocks(struct socket *so, struct socket *so2)
    256  1.112        ad {
    257  1.112        ad 	struct unpcb *unp;
    258  1.112        ad 	kmutex_t *lock;
    259  1.112        ad 
    260  1.112        ad 	KASSERT(solocked2(so, so2));
    261  1.112        ad 
    262  1.112        ad 	/*
    263  1.112        ad 	 * Bail out if either end of the socket is not yet fully
    264  1.112        ad 	 * connected or accepted.  We only break the lock association
    265  1.112        ad 	 * with the head when the pair of sockets stand completely
    266  1.112        ad 	 * on their own.
    267  1.112        ad 	 */
    268  1.125      yamt 	KASSERT(so->so_head == NULL);
    269  1.125      yamt 	if (so2->so_head != NULL)
    270  1.112        ad 		return;
    271  1.112        ad 
    272  1.112        ad 	/*
    273  1.112        ad 	 * Drop references to old lock.  A third reference (from the
    274  1.112        ad 	 * queue head) must be held as we still hold its lock.  Bonus:
    275  1.112        ad 	 * we don't need to worry about garbage collecting the lock.
    276  1.112        ad 	 */
    277  1.112        ad 	lock = so->so_lock;
    278  1.112        ad 	KASSERT(lock == uipc_lock);
    279  1.112        ad 	mutex_obj_free(lock);
    280  1.112        ad 	mutex_obj_free(lock);
    281  1.112        ad 
    282  1.112        ad 	/*
    283  1.112        ad 	 * Grab stream lock from the initiator and share between the two
    284  1.112        ad 	 * endpoints.  Issue memory barrier to ensure all modifications
    285  1.112        ad 	 * become globally visible before the lock change.  so2 is
    286  1.112        ad 	 * assumed not to have a stream lock, because it was created
    287  1.112        ad 	 * purely for the server side to accept this connection and
    288  1.112        ad 	 * started out life using the domain-wide lock.
    289  1.112        ad 	 */
    290  1.112        ad 	unp = sotounpcb(so);
    291  1.112        ad 	KASSERT(unp->unp_streamlock != NULL);
    292  1.112        ad 	KASSERT(sotounpcb(so2)->unp_streamlock == NULL);
    293  1.112        ad 	lock = unp->unp_streamlock;
    294  1.112        ad 	unp->unp_streamlock = NULL;
    295  1.112        ad 	mutex_obj_hold(lock);
    296  1.202  riastrad 	/*
    297  1.202  riastrad 	 * Ensure lock is initialized before publishing it with
    298  1.202  riastrad 	 * solockreset.  Pairs with atomic_load_consume in solock and
    299  1.202  riastrad 	 * various loops to reacquire lock after wakeup.
    300  1.202  riastrad 	 */
    301  1.202  riastrad 	membar_release();
    302  1.127    bouyer 	/*
    303  1.127    bouyer 	 * possible race if lock is not held - see comment in
    304  1.127    bouyer 	 * uipc_usrreq(PRU_ACCEPT).
    305  1.127    bouyer 	 */
    306  1.127    bouyer 	KASSERT(mutex_owned(lock));
    307  1.115        ad 	solockreset(so, lock);
    308  1.115        ad 	solockreset(so2, lock);
    309  1.112        ad }
    310  1.112        ad 
    311  1.112        ad /*
    312  1.112        ad  * Reset a socket's lock back to the domain-wide lock.
    313  1.112        ad  */
    314  1.112        ad static void
    315  1.112        ad unp_resetlock(struct socket *so)
    316  1.112        ad {
    317  1.112        ad 	kmutex_t *olock, *nlock;
    318  1.112        ad 	struct unpcb *unp;
    319  1.112        ad 
    320  1.112        ad 	KASSERT(solocked(so));
    321  1.112        ad 
    322  1.112        ad 	olock = so->so_lock;
    323  1.112        ad 	nlock = uipc_lock;
    324  1.112        ad 	if (olock == nlock)
    325  1.112        ad 		return;
    326  1.112        ad 	unp = sotounpcb(so);
    327  1.112        ad 	KASSERT(unp->unp_streamlock == NULL);
    328  1.112        ad 	unp->unp_streamlock = olock;
    329  1.112        ad 	mutex_obj_hold(nlock);
    330  1.112        ad 	mutex_enter(nlock);
    331  1.115        ad 	solockreset(so, nlock);
    332  1.112        ad 	mutex_exit(olock);
    333  1.112        ad }
    334  1.112        ad 
    335  1.112        ad static void
    336  1.112        ad unp_free(struct unpcb *unp)
    337  1.112        ad {
    338  1.112        ad 	if (unp->unp_addr)
    339  1.112        ad 		free(unp->unp_addr, M_SONAME);
    340  1.112        ad 	if (unp->unp_streamlock != NULL)
    341  1.112        ad 		mutex_obj_free(unp->unp_streamlock);
    342  1.152     rmind 	kmem_free(unp, sizeof(*unp));
    343  1.112        ad }
    344   1.30   thorpej 
    345  1.164       rtr static int
    346  1.164       rtr unp_output(struct mbuf *m, struct mbuf *control, struct unpcb *unp)
    347   1.20   mycroft {
    348   1.20   mycroft 	struct socket *so2;
    349   1.77      matt 	const struct sockaddr_un *sun;
    350   1.20   mycroft 
    351  1.153  christos 	/* XXX: server side closed the socket */
    352  1.153  christos 	if (unp->unp_conn == NULL)
    353  1.153  christos 		return ECONNREFUSED;
    354   1.20   mycroft 	so2 = unp->unp_conn->unp_socket;
    355  1.112        ad 
    356  1.112        ad 	KASSERT(solocked(so2));
    357  1.112        ad 
    358   1.20   mycroft 	if (unp->unp_addr)
    359   1.20   mycroft 		sun = unp->unp_addr;
    360   1.20   mycroft 	else
    361   1.20   mycroft 		sun = &sun_noname;
    362   1.30   thorpej 	if (unp->unp_conn->unp_flags & UNP_WANTCRED)
    363  1.164       rtr 		control = unp_addsockcred(curlwp, control);
    364  1.180       roy 	if (unp->unp_conn->unp_flags & UNP_OWANTCRED)
    365  1.192  pgoyette 		MODULE_HOOK_CALL(uipc_unp_70_hook, (curlwp, control),
    366  1.188  pgoyette 		    stub_compat_70_unp_addsockcred(curlwp, control), control);
    367   1.82  christos 	if (sbappendaddr(&so2->so_rcv, (const struct sockaddr *)sun, m,
    368   1.20   mycroft 	    control) == 0) {
    369   1.98    martin 		unp_dispose(control);
    370   1.20   mycroft 		m_freem(control);
    371   1.20   mycroft 		m_freem(m);
    372  1.187       roy 		/* Don't call soroverflow because we're returning this
    373  1.187       roy 		 * error directly to the sender. */
    374  1.187       roy 		so2->so_rcv.sb_overflowed++;
    375  1.187       roy 		return ENOBUFS;
    376   1.20   mycroft 	} else {
    377   1.20   mycroft 		sorwakeup(so2);
    378  1.187       roy 		return 0;
    379   1.20   mycroft 	}
    380   1.20   mycroft }
    381   1.20   mycroft 
    382  1.164       rtr static void
    383  1.177       rtr unp_setaddr(struct socket *so, struct sockaddr *nam, bool peeraddr)
    384   1.20   mycroft {
    385  1.177       rtr 	const struct sockaddr_un *sun = NULL;
    386  1.112        ad 	struct unpcb *unp;
    387   1.20   mycroft 
    388  1.127    bouyer 	KASSERT(solocked(so));
    389  1.112        ad 	unp = sotounpcb(so);
    390   1.20   mycroft 
    391  1.177       rtr 	if (peeraddr) {
    392  1.177       rtr 		if (unp->unp_conn && unp->unp_conn->unp_addr)
    393  1.177       rtr 			sun = unp->unp_conn->unp_addr;
    394  1.177       rtr 	} else {
    395  1.177       rtr 		if (unp->unp_addr)
    396  1.177       rtr 			sun = unp->unp_addr;
    397  1.112        ad 	}
    398  1.177       rtr 	if (sun == NULL)
    399  1.177       rtr 		sun = &sun_noname;
    400  1.177       rtr 
    401  1.177       rtr 	memcpy(nam, sun, sun->sun_len);
    402   1.20   mycroft }
    403   1.20   mycroft 
    404  1.151     rmind static int
    405  1.168       rtr unp_rcvd(struct socket *so, int flags, struct lwp *l)
    406  1.168       rtr {
    407  1.168       rtr 	struct unpcb *unp = sotounpcb(so);
    408  1.168       rtr 	struct socket *so2;
    409  1.168       rtr 	u_int newhiwat;
    410  1.168       rtr 
    411  1.168       rtr 	KASSERT(solocked(so));
    412  1.168       rtr 	KASSERT(unp != NULL);
    413  1.168       rtr 
    414  1.168       rtr 	switch (so->so_type) {
    415  1.168       rtr 
    416  1.168       rtr 	case SOCK_DGRAM:
    417  1.168       rtr 		panic("uipc 1");
    418  1.168       rtr 		/*NOTREACHED*/
    419  1.168       rtr 
    420  1.168       rtr 	case SOCK_SEQPACKET: /* FALLTHROUGH */
    421  1.168       rtr 	case SOCK_STREAM:
    422  1.168       rtr #define	rcv (&so->so_rcv)
    423  1.168       rtr #define snd (&so2->so_snd)
    424  1.168       rtr 		if (unp->unp_conn == 0)
    425  1.168       rtr 			break;
    426  1.168       rtr 		so2 = unp->unp_conn->unp_socket;
    427  1.168       rtr 		KASSERT(solocked2(so, so2));
    428  1.168       rtr 		/*
    429  1.168       rtr 		 * Adjust backpressure on sender
    430  1.168       rtr 		 * and wakeup any waiting to write.
    431  1.168       rtr 		 */
    432  1.168       rtr 		snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt;
    433  1.168       rtr 		unp->unp_mbcnt = rcv->sb_mbcnt;
    434  1.168       rtr 		newhiwat = snd->sb_hiwat + unp->unp_cc - rcv->sb_cc;
    435  1.168       rtr 		(void)chgsbsize(so2->so_uidinfo,
    436  1.168       rtr 		    &snd->sb_hiwat, newhiwat, RLIM_INFINITY);
    437  1.168       rtr 		unp->unp_cc = rcv->sb_cc;
    438  1.168       rtr 		sowwakeup(so2);
    439  1.168       rtr #undef snd
    440  1.168       rtr #undef rcv
    441  1.168       rtr 		break;
    442  1.168       rtr 
    443  1.168       rtr 	default:
    444  1.168       rtr 		panic("uipc 2");
    445  1.168       rtr 	}
    446  1.168       rtr 
    447  1.168       rtr 	return 0;
    448  1.168       rtr }
    449  1.168       rtr 
    450  1.168       rtr static int
    451  1.160       rtr unp_recvoob(struct socket *so, struct mbuf *m, int flags)
    452  1.160       rtr {
    453  1.160       rtr 	KASSERT(solocked(so));
    454  1.160       rtr 
    455  1.160       rtr 	return EOPNOTSUPP;
    456  1.160       rtr }
    457  1.160       rtr 
    458  1.160       rtr static int
    459  1.179       rtr unp_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
    460  1.166       rtr     struct mbuf *control, struct lwp *l)
    461  1.166       rtr {
    462  1.166       rtr 	struct unpcb *unp = sotounpcb(so);
    463  1.166       rtr 	int error = 0;
    464  1.166       rtr 	u_int newhiwat;
    465  1.166       rtr 	struct socket *so2;
    466  1.166       rtr 
    467  1.166       rtr 	KASSERT(solocked(so));
    468  1.166       rtr 	KASSERT(unp != NULL);
    469  1.166       rtr 	KASSERT(m != NULL);
    470  1.166       rtr 
    471  1.166       rtr 	/*
    472  1.166       rtr 	 * Note: unp_internalize() rejects any control message
    473  1.166       rtr 	 * other than SCM_RIGHTS, and only allows one.  This
    474  1.166       rtr 	 * has the side-effect of preventing a caller from
    475  1.166       rtr 	 * forging SCM_CREDS.
    476  1.166       rtr 	 */
    477  1.166       rtr 	if (control) {
    478  1.166       rtr 		sounlock(so);
    479  1.166       rtr 		error = unp_internalize(&control);
    480  1.166       rtr 		solock(so);
    481  1.166       rtr 		if (error != 0) {
    482  1.166       rtr 			m_freem(control);
    483  1.166       rtr 			m_freem(m);
    484  1.166       rtr 			return error;
    485  1.166       rtr 		}
    486  1.166       rtr 	}
    487  1.166       rtr 
    488  1.166       rtr 	switch (so->so_type) {
    489  1.166       rtr 
    490  1.166       rtr 	case SOCK_DGRAM: {
    491  1.166       rtr 		KASSERT(so->so_lock == uipc_lock);
    492  1.166       rtr 		if (nam) {
    493  1.166       rtr 			if ((so->so_state & SS_ISCONNECTED) != 0)
    494  1.166       rtr 				error = EISCONN;
    495  1.166       rtr 			else {
    496  1.166       rtr 				/*
    497  1.166       rtr 				 * Note: once connected, the
    498  1.166       rtr 				 * socket's lock must not be
    499  1.166       rtr 				 * dropped until we have sent
    500  1.166       rtr 				 * the message and disconnected.
    501  1.166       rtr 				 * This is necessary to prevent
    502  1.166       rtr 				 * intervening control ops, like
    503  1.166       rtr 				 * another connection.
    504  1.166       rtr 				 */
    505  1.166       rtr 				error = unp_connect(so, nam, l);
    506  1.166       rtr 			}
    507  1.166       rtr 		} else {
    508  1.166       rtr 			if ((so->so_state & SS_ISCONNECTED) == 0)
    509  1.166       rtr 				error = ENOTCONN;
    510  1.166       rtr 		}
    511  1.166       rtr 		if (error) {
    512  1.166       rtr 			unp_dispose(control);
    513  1.166       rtr 			m_freem(control);
    514  1.166       rtr 			m_freem(m);
    515  1.166       rtr 			return error;
    516  1.166       rtr 		}
    517  1.166       rtr 		error = unp_output(m, control, unp);
    518  1.166       rtr 		if (nam)
    519  1.166       rtr 			unp_disconnect1(unp);
    520  1.166       rtr 		break;
    521  1.166       rtr 	}
    522  1.166       rtr 
    523  1.166       rtr 	case SOCK_SEQPACKET: /* FALLTHROUGH */
    524  1.166       rtr 	case SOCK_STREAM:
    525  1.166       rtr #define	rcv (&so2->so_rcv)
    526  1.166       rtr #define	snd (&so->so_snd)
    527  1.166       rtr 		if (unp->unp_conn == NULL) {
    528  1.166       rtr 			error = ENOTCONN;
    529  1.166       rtr 			break;
    530  1.166       rtr 		}
    531  1.166       rtr 		so2 = unp->unp_conn->unp_socket;
    532  1.166       rtr 		KASSERT(solocked2(so, so2));
    533  1.166       rtr 		if (unp->unp_conn->unp_flags & UNP_WANTCRED) {
    534  1.166       rtr 			/*
    535  1.166       rtr 			 * Credentials are passed only once on
    536  1.166       rtr 			 * SOCK_STREAM and SOCK_SEQPACKET.
    537  1.166       rtr 			 */
    538  1.166       rtr 			unp->unp_conn->unp_flags &= ~UNP_WANTCRED;
    539  1.166       rtr 			control = unp_addsockcred(l, control);
    540  1.166       rtr 		}
    541  1.180       roy 		if (unp->unp_conn->unp_flags & UNP_OWANTCRED) {
    542  1.180       roy 			/*
    543  1.180       roy 			 * Credentials are passed only once on
    544  1.180       roy 			 * SOCK_STREAM and SOCK_SEQPACKET.
    545  1.180       roy 			 */
    546  1.180       roy 			unp->unp_conn->unp_flags &= ~UNP_OWANTCRED;
    547  1.192  pgoyette 			MODULE_HOOK_CALL(uipc_unp_70_hook, (curlwp, control),
    548  1.188  pgoyette 			    stub_compat_70_unp_addsockcred(curlwp, control),
    549  1.188  pgoyette 			    control);
    550  1.180       roy 		}
    551  1.166       rtr 		/*
    552  1.166       rtr 		 * Send to paired receive port, and then reduce
    553  1.166       rtr 		 * send buffer hiwater marks to maintain backpressure.
    554  1.166       rtr 		 * Wake up readers.
    555  1.166       rtr 		 */
    556  1.166       rtr 		if (control) {
    557  1.166       rtr 			if (sbappendcontrol(rcv, m, control) != 0)
    558  1.166       rtr 				control = NULL;
    559  1.166       rtr 		} else {
    560  1.166       rtr 			switch(so->so_type) {
    561  1.166       rtr 			case SOCK_SEQPACKET:
    562  1.166       rtr 				sbappendrecord(rcv, m);
    563  1.166       rtr 				break;
    564  1.166       rtr 			case SOCK_STREAM:
    565  1.166       rtr 				sbappend(rcv, m);
    566  1.166       rtr 				break;
    567  1.166       rtr 			default:
    568  1.166       rtr 				panic("uipc_usrreq");
    569  1.166       rtr 				break;
    570  1.166       rtr 			}
    571  1.166       rtr 		}
    572  1.166       rtr 		snd->sb_mbmax -=
    573  1.166       rtr 		    rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt;
    574  1.166       rtr 		unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt;
    575  1.166       rtr 		newhiwat = snd->sb_hiwat -
    576  1.166       rtr 		    (rcv->sb_cc - unp->unp_conn->unp_cc);
    577  1.166       rtr 		(void)chgsbsize(so->so_uidinfo,
    578  1.166       rtr 		    &snd->sb_hiwat, newhiwat, RLIM_INFINITY);
    579  1.166       rtr 		unp->unp_conn->unp_cc = rcv->sb_cc;
    580  1.166       rtr 		sorwakeup(so2);
    581  1.166       rtr #undef snd
    582  1.166       rtr #undef rcv
    583  1.166       rtr 		if (control != NULL) {
    584  1.166       rtr 			unp_dispose(control);
    585  1.166       rtr 			m_freem(control);
    586  1.166       rtr 		}
    587  1.166       rtr 		break;
    588  1.166       rtr 
    589  1.166       rtr 	default:
    590  1.166       rtr 		panic("uipc 4");
    591  1.166       rtr 	}
    592  1.166       rtr 
    593  1.166       rtr 	return error;
    594  1.166       rtr }
    595  1.166       rtr 
    596  1.166       rtr static int
    597  1.160       rtr unp_sendoob(struct socket *so, struct mbuf *m, struct mbuf * control)
    598  1.160       rtr {
    599  1.160       rtr 	KASSERT(solocked(so));
    600  1.160       rtr 
    601  1.160       rtr 	m_freem(m);
    602  1.160       rtr 	m_freem(control);
    603  1.160       rtr 
    604  1.160       rtr 	return EOPNOTSUPP;
    605  1.160       rtr }
    606  1.160       rtr 
    607    1.1       cgd /*
    608   1.30   thorpej  * Unix domain socket option processing.
    609   1.30   thorpej  */
    610   1.30   thorpej int
    611  1.118    plunky uipc_ctloutput(int op, struct socket *so, struct sockopt *sopt)
    612   1.30   thorpej {
    613   1.30   thorpej 	struct unpcb *unp = sotounpcb(so);
    614   1.30   thorpej 	int optval = 0, error = 0;
    615   1.30   thorpej 
    616  1.112        ad 	KASSERT(solocked(so));
    617  1.112        ad 
    618  1.201       nia 	if (sopt->sopt_level != SOL_LOCAL) {
    619  1.100    dyoung 		error = ENOPROTOOPT;
    620   1.30   thorpej 	} else switch (op) {
    621   1.30   thorpej 
    622   1.30   thorpej 	case PRCO_SETOPT:
    623  1.118    plunky 		switch (sopt->sopt_name) {
    624  1.188  pgoyette 		case LOCAL_OCREDS:
    625  1.191  pgoyette 			if (!compat70_ocreds_valid)  {
    626  1.188  pgoyette 				error = ENOPROTOOPT;
    627  1.188  pgoyette 				break;
    628  1.188  pgoyette 			}
    629  1.188  pgoyette 			/* FALLTHROUGH */
    630   1.30   thorpej 		case LOCAL_CREDS:
    631   1.72      matt 		case LOCAL_CONNWAIT:
    632  1.118    plunky 			error = sockopt_getint(sopt, &optval);
    633  1.118    plunky 			if (error)
    634  1.118    plunky 				break;
    635  1.118    plunky 			switch (sopt->sopt_name) {
    636   1.30   thorpej #define	OPTSET(bit) \
    637   1.30   thorpej 	if (optval) \
    638   1.30   thorpej 		unp->unp_flags |= (bit); \
    639   1.30   thorpej 	else \
    640   1.30   thorpej 		unp->unp_flags &= ~(bit);
    641   1.30   thorpej 
    642  1.118    plunky 			case LOCAL_CREDS:
    643  1.118    plunky 				OPTSET(UNP_WANTCRED);
    644  1.118    plunky 				break;
    645  1.118    plunky 			case LOCAL_CONNWAIT:
    646  1.118    plunky 				OPTSET(UNP_CONNWAIT);
    647  1.118    plunky 				break;
    648  1.180       roy 			case LOCAL_OCREDS:
    649  1.180       roy 				OPTSET(UNP_OWANTCRED);
    650  1.180       roy 				break;
    651   1.30   thorpej 			}
    652   1.30   thorpej 			break;
    653   1.30   thorpej #undef OPTSET
    654   1.30   thorpej 
    655   1.30   thorpej 		default:
    656   1.30   thorpej 			error = ENOPROTOOPT;
    657   1.30   thorpej 			break;
    658   1.30   thorpej 		}
    659   1.30   thorpej 		break;
    660   1.30   thorpej 
    661   1.30   thorpej 	case PRCO_GETOPT:
    662  1.112        ad 		sounlock(so);
    663  1.118    plunky 		switch (sopt->sopt_name) {
    664   1.99        he 		case LOCAL_PEEREID:
    665   1.99        he 			if (unp->unp_flags & UNP_EIDSVALID) {
    666  1.183  christos 				error = sockopt_set(sopt, &unp->unp_connid,
    667  1.183  christos 				    sizeof(unp->unp_connid));
    668   1.99        he 			} else {
    669   1.99        he 				error = EINVAL;
    670   1.99        he 			}
    671   1.99        he 			break;
    672   1.30   thorpej 		case LOCAL_CREDS:
    673   1.30   thorpej #define	OPTBIT(bit)	(unp->unp_flags & (bit) ? 1 : 0)
    674   1.30   thorpej 
    675   1.99        he 			optval = OPTBIT(UNP_WANTCRED);
    676  1.118    plunky 			error = sockopt_setint(sopt, optval);
    677   1.30   thorpej 			break;
    678  1.180       roy 		case LOCAL_OCREDS:
    679  1.191  pgoyette 			if (compat70_ocreds_valid) {
    680  1.188  pgoyette 				optval = OPTBIT(UNP_OWANTCRED);
    681  1.188  pgoyette 				error = sockopt_setint(sopt, optval);
    682  1.188  pgoyette 				break;
    683  1.188  pgoyette 			}
    684  1.190       mrg #undef OPTBIT
    685  1.188  pgoyette 			/* FALLTHROUGH */
    686   1.30   thorpej 		default:
    687   1.30   thorpej 			error = ENOPROTOOPT;
    688   1.30   thorpej 			break;
    689   1.30   thorpej 		}
    690  1.112        ad 		solock(so);
    691   1.30   thorpej 		break;
    692   1.30   thorpej 	}
    693   1.30   thorpej 	return (error);
    694   1.30   thorpej }
    695   1.30   thorpej 
    696   1.30   thorpej /*
    697    1.1       cgd  * Both send and receive buffers are allocated PIPSIZ bytes of buffering
    698    1.1       cgd  * for stream sockets, although the total for sender and receiver is
    699    1.1       cgd  * actually only PIPSIZ.
    700    1.1       cgd  * Datagram sockets really use the sendspace as the maximum datagram size,
    701    1.1       cgd  * and don't really want to reserve the sendspace.  Their recvspace should
    702    1.1       cgd  * be large enough for at least one max-size datagram plus address.
    703    1.1       cgd  */
    704  1.185  christos #ifndef PIPSIZ
    705  1.185  christos #define	PIPSIZ	8192
    706  1.185  christos #endif
    707    1.1       cgd u_long	unpst_sendspace = PIPSIZ;
    708    1.1       cgd u_long	unpst_recvspace = PIPSIZ;
    709    1.1       cgd u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
    710  1.186       roy u_long	unpdg_recvspace = 16*1024;
    711    1.1       cgd 
    712  1.121       mrg u_int	unp_rights;			/* files in flight */
    713  1.121       mrg u_int	unp_rights_ratio = 2;		/* limit, fraction of maxfiles */
    714    1.1       cgd 
    715  1.152     rmind static int
    716  1.152     rmind unp_attach(struct socket *so, int proto)
    717    1.1       cgd {
    718  1.152     rmind 	struct unpcb *unp = sotounpcb(so);
    719  1.152     rmind 	u_long sndspc, rcvspc;
    720    1.1       cgd 	int error;
    721   1.80     perry 
    722  1.152     rmind 	KASSERT(unp == NULL);
    723  1.152     rmind 
    724  1.112        ad 	switch (so->so_type) {
    725  1.152     rmind 	case SOCK_SEQPACKET:
    726  1.152     rmind 		/* FALLTHROUGH */
    727  1.112        ad 	case SOCK_STREAM:
    728  1.112        ad 		if (so->so_lock == NULL) {
    729  1.112        ad 			so->so_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    730  1.112        ad 			solock(so);
    731  1.112        ad 		}
    732  1.152     rmind 		sndspc = unpst_sendspace;
    733  1.152     rmind 		rcvspc = unpst_recvspace;
    734  1.112        ad 		break;
    735    1.1       cgd 
    736  1.112        ad 	case SOCK_DGRAM:
    737  1.112        ad 		if (so->so_lock == NULL) {
    738  1.112        ad 			mutex_obj_hold(uipc_lock);
    739  1.112        ad 			so->so_lock = uipc_lock;
    740  1.112        ad 			solock(so);
    741  1.112        ad 		}
    742  1.152     rmind 		sndspc = unpdg_sendspace;
    743  1.152     rmind 		rcvspc = unpdg_recvspace;
    744  1.112        ad 		break;
    745    1.8   mycroft 
    746  1.112        ad 	default:
    747  1.112        ad 		panic("unp_attach");
    748    1.1       cgd 	}
    749  1.152     rmind 
    750  1.152     rmind 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    751  1.152     rmind 		error = soreserve(so, sndspc, rcvspc);
    752  1.152     rmind 		if (error) {
    753  1.152     rmind 			return error;
    754  1.152     rmind 		}
    755  1.152     rmind 	}
    756  1.152     rmind 
    757  1.152     rmind 	unp = kmem_zalloc(sizeof(*unp), KM_SLEEP);
    758  1.152     rmind 	nanotime(&unp->unp_ctime);
    759   1.14   mycroft 	unp->unp_socket = so;
    760   1.15   mycroft 	so->so_pcb = unp;
    761  1.152     rmind 
    762  1.152     rmind 	KASSERT(solocked(so));
    763  1.152     rmind 	return 0;
    764    1.1       cgd }
    765    1.1       cgd 
    766  1.152     rmind static void
    767  1.152     rmind unp_detach(struct socket *so)
    768    1.1       cgd {
    769  1.152     rmind 	struct unpcb *unp;
    770  1.112        ad 	vnode_t *vp;
    771  1.112        ad 
    772  1.152     rmind 	unp = sotounpcb(so);
    773  1.152     rmind 	KASSERT(unp != NULL);
    774  1.152     rmind 	KASSERT(solocked(so));
    775  1.112        ad  retry:
    776  1.112        ad 	if ((vp = unp->unp_vnode) != NULL) {
    777  1.112        ad 		sounlock(so);
    778  1.112        ad 		/* Acquire v_interlock to protect against unp_connect(). */
    779  1.113        ad 		/* XXXAD racy */
    780  1.135     rmind 		mutex_enter(vp->v_interlock);
    781  1.112        ad 		vp->v_socket = NULL;
    782  1.148   hannken 		mutex_exit(vp->v_interlock);
    783  1.148   hannken 		vrele(vp);
    784  1.112        ad 		solock(so);
    785  1.112        ad 		unp->unp_vnode = NULL;
    786    1.1       cgd 	}
    787    1.1       cgd 	if (unp->unp_conn)
    788  1.163       rtr 		unp_disconnect1(unp);
    789  1.112        ad 	while (unp->unp_refs) {
    790  1.112        ad 		KASSERT(solocked2(so, unp->unp_refs->unp_socket));
    791  1.112        ad 		if (unp_drop(unp->unp_refs, ECONNRESET)) {
    792  1.112        ad 			solock(so);
    793  1.112        ad 			goto retry;
    794  1.112        ad 		}
    795  1.112        ad 	}
    796  1.112        ad 	soisdisconnected(so);
    797  1.112        ad 	so->so_pcb = NULL;
    798    1.8   mycroft 	if (unp_rights) {
    799    1.8   mycroft 		/*
    800  1.121       mrg 		 * Normally the receive buffer is flushed later, in sofree,
    801  1.121       mrg 		 * but if our receive buffer holds references to files that
    802  1.121       mrg 		 * are now garbage, we will enqueue those file references to
    803  1.121       mrg 		 * the garbage collector and kick it into action.
    804    1.8   mycroft 		 */
    805  1.112        ad 		sorflush(so);
    806  1.112        ad 		unp_free(unp);
    807  1.121       mrg 		unp_thread_kick();
    808   1.14   mycroft 	} else
    809  1.112        ad 		unp_free(unp);
    810    1.1       cgd }
    811    1.1       cgd 
    812  1.154       rtr static int
    813  1.177       rtr unp_accept(struct socket *so, struct sockaddr *nam)
    814  1.159       rtr {
    815  1.159       rtr 	struct unpcb *unp = sotounpcb(so);
    816  1.159       rtr 	struct socket *so2;
    817  1.159       rtr 
    818  1.159       rtr 	KASSERT(solocked(so));
    819  1.159       rtr 	KASSERT(nam != NULL);
    820  1.159       rtr 
    821  1.159       rtr 	/* XXX code review required to determine if unp can ever be NULL */
    822  1.159       rtr 	if (unp == NULL)
    823  1.159       rtr 		return EINVAL;
    824  1.159       rtr 
    825  1.159       rtr 	KASSERT(so->so_lock == uipc_lock);
    826  1.159       rtr 	/*
    827  1.159       rtr 	 * Mark the initiating STREAM socket as connected *ONLY*
    828  1.159       rtr 	 * after it's been accepted.  This prevents a client from
    829  1.159       rtr 	 * overrunning a server and receiving ECONNREFUSED.
    830  1.159       rtr 	 */
    831  1.159       rtr 	if (unp->unp_conn == NULL) {
    832  1.159       rtr 		/*
    833  1.159       rtr 		 * This will use the empty socket and will not
    834  1.159       rtr 		 * allocate.
    835  1.159       rtr 		 */
    836  1.159       rtr 		unp_setaddr(so, nam, true);
    837  1.159       rtr 		return 0;
    838  1.159       rtr 	}
    839  1.159       rtr 	so2 = unp->unp_conn->unp_socket;
    840  1.159       rtr 	if (so2->so_state & SS_ISCONNECTING) {
    841  1.159       rtr 		KASSERT(solocked2(so, so->so_head));
    842  1.159       rtr 		KASSERT(solocked2(so2, so->so_head));
    843  1.159       rtr 		soisconnected(so2);
    844  1.159       rtr 	}
    845  1.159       rtr 	/*
    846  1.159       rtr 	 * If the connection is fully established, break the
    847  1.159       rtr 	 * association with uipc_lock and give the connected
    848  1.159       rtr 	 * pair a separate lock to share.
    849  1.159       rtr 	 * There is a race here: sotounpcb(so2)->unp_streamlock
    850  1.159       rtr 	 * is not locked, so when changing so2->so_lock
    851  1.159       rtr 	 * another thread can grab it while so->so_lock is still
    852  1.159       rtr 	 * pointing to the (locked) uipc_lock.
    853  1.159       rtr 	 * this should be harmless, except that this makes
    854  1.159       rtr 	 * solocked2() and solocked() unreliable.
    855  1.159       rtr 	 * Another problem is that unp_setaddr() expects the
    856  1.203    andvar 	 * the socket locked. Grabbing sotounpcb(so2)->unp_streamlock
    857  1.159       rtr 	 * fixes both issues.
    858  1.159       rtr 	 */
    859  1.159       rtr 	mutex_enter(sotounpcb(so2)->unp_streamlock);
    860  1.159       rtr 	unp_setpeerlocks(so2, so);
    861  1.159       rtr 	/*
    862  1.159       rtr 	 * Only now return peer's address, as we may need to
    863  1.159       rtr 	 * block in order to allocate memory.
    864  1.159       rtr 	 *
    865  1.159       rtr 	 * XXX Minor race: connection can be broken while
    866  1.159       rtr 	 * lock is dropped in unp_setaddr().  We will return
    867  1.159       rtr 	 * error == 0 and sun_noname as the peer address.
    868  1.159       rtr 	 */
    869  1.159       rtr 	unp_setaddr(so, nam, true);
    870  1.159       rtr 	/* so_lock now points to unp_streamlock */
    871  1.159       rtr 	mutex_exit(so2->so_lock);
    872  1.159       rtr 	return 0;
    873  1.159       rtr }
    874  1.159       rtr 
    875  1.159       rtr static int
    876  1.155       rtr unp_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
    877  1.154       rtr {
    878  1.154       rtr 	return EOPNOTSUPP;
    879  1.154       rtr }
    880  1.154       rtr 
    881  1.156       rtr static int
    882  1.156       rtr unp_stat(struct socket *so, struct stat *ub)
    883  1.156       rtr {
    884  1.156       rtr 	struct unpcb *unp;
    885  1.156       rtr 	struct socket *so2;
    886  1.156       rtr 
    887  1.157       rtr 	KASSERT(solocked(so));
    888  1.157       rtr 
    889  1.156       rtr 	unp = sotounpcb(so);
    890  1.156       rtr 	if (unp == NULL)
    891  1.156       rtr 		return EINVAL;
    892  1.156       rtr 
    893  1.156       rtr 	ub->st_blksize = so->so_snd.sb_hiwat;
    894  1.156       rtr 	switch (so->so_type) {
    895  1.156       rtr 	case SOCK_SEQPACKET: /* FALLTHROUGH */
    896  1.156       rtr 	case SOCK_STREAM:
    897  1.205  riastrad 		if (unp->unp_conn == 0)
    898  1.156       rtr 			break;
    899  1.156       rtr 
    900  1.156       rtr 		so2 = unp->unp_conn->unp_socket;
    901  1.156       rtr 		KASSERT(solocked2(so, so2));
    902  1.156       rtr 		ub->st_blksize += so2->so_rcv.sb_cc;
    903  1.156       rtr 		break;
    904  1.156       rtr 	default:
    905  1.156       rtr 		break;
    906  1.156       rtr 	}
    907  1.156       rtr 	ub->st_dev = NODEV;
    908  1.156       rtr 	if (unp->unp_ino == 0)
    909  1.156       rtr 		unp->unp_ino = unp_ino++;
    910  1.156       rtr 	ub->st_atimespec = ub->st_mtimespec = ub->st_ctimespec = unp->unp_ctime;
    911  1.156       rtr 	ub->st_ino = unp->unp_ino;
    912  1.199  christos 	ub->st_uid = so->so_uidinfo->ui_uid;
    913  1.199  christos 	ub->st_gid = so->so_egid;
    914  1.156       rtr 	return (0);
    915  1.156       rtr }
    916  1.156       rtr 
    917  1.158       rtr static int
    918  1.177       rtr unp_peeraddr(struct socket *so, struct sockaddr *nam)
    919  1.158       rtr {
    920  1.158       rtr 	KASSERT(solocked(so));
    921  1.158       rtr 	KASSERT(sotounpcb(so) != NULL);
    922  1.158       rtr 	KASSERT(nam != NULL);
    923  1.158       rtr 
    924  1.158       rtr 	unp_setaddr(so, nam, true);
    925  1.158       rtr 	return 0;
    926  1.158       rtr }
    927  1.158       rtr 
    928  1.158       rtr static int
    929  1.177       rtr unp_sockaddr(struct socket *so, struct sockaddr *nam)
    930  1.158       rtr {
    931  1.158       rtr 	KASSERT(solocked(so));
    932  1.158       rtr 	KASSERT(sotounpcb(so) != NULL);
    933  1.158       rtr 	KASSERT(nam != NULL);
    934  1.158       rtr 
    935  1.158       rtr 	unp_setaddr(so, nam, false);
    936  1.158       rtr 	return 0;
    937  1.158       rtr }
    938  1.158       rtr 
    939  1.146  christos /*
    940  1.176       rtr  * we only need to perform this allocation until syscalls other than
    941  1.176       rtr  * bind are adjusted to use sockaddr_big.
    942  1.176       rtr  */
    943  1.176       rtr static struct sockaddr_un *
    944  1.176       rtr makeun_sb(struct sockaddr *nam, size_t *addrlen)
    945  1.176       rtr {
    946  1.176       rtr 	struct sockaddr_un *sun;
    947  1.176       rtr 
    948  1.176       rtr 	*addrlen = nam->sa_len + 1;
    949  1.176       rtr 	sun = malloc(*addrlen, M_SONAME, M_WAITOK);
    950  1.176       rtr 	memcpy(sun, nam, nam->sa_len);
    951  1.176       rtr 	*(((char *)sun) + nam->sa_len) = '\0';
    952  1.176       rtr 	return sun;
    953  1.176       rtr }
    954  1.176       rtr 
    955  1.164       rtr static int
    956  1.176       rtr unp_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
    957    1.1       cgd {
    958   1.27   thorpej 	struct sockaddr_un *sun;
    959  1.112        ad 	struct unpcb *unp;
    960  1.106        ad 	vnode_t *vp;
    961    1.1       cgd 	struct vattr vattr;
    962   1.27   thorpej 	size_t addrlen;
    963    1.1       cgd 	int error;
    964  1.133  dholland 	struct pathbuf *pb;
    965    1.1       cgd 	struct nameidata nd;
    966  1.112        ad 	proc_t *p;
    967    1.1       cgd 
    968  1.112        ad 	unp = sotounpcb(so);
    969  1.161       rtr 
    970  1.161       rtr 	KASSERT(solocked(so));
    971  1.161       rtr 	KASSERT(unp != NULL);
    972  1.161       rtr 	KASSERT(nam != NULL);
    973  1.161       rtr 
    974  1.112        ad 	if (unp->unp_vnode != NULL)
    975   1.20   mycroft 		return (EINVAL);
    976  1.109        ad 	if ((unp->unp_flags & UNP_BUSY) != 0) {
    977  1.109        ad 		/*
    978  1.109        ad 		 * EALREADY may not be strictly accurate, but since this
    979  1.109        ad 		 * is a major application error it's hardly a big deal.
    980  1.109        ad 		 */
    981  1.109        ad 		return (EALREADY);
    982  1.109        ad 	}
    983  1.109        ad 	unp->unp_flags |= UNP_BUSY;
    984  1.112        ad 	sounlock(so);
    985  1.109        ad 
    986  1.165       rtr 	p = l->l_proc;
    987  1.176       rtr 	sun = makeun_sb(nam, &addrlen);
    988   1.27   thorpej 
    989  1.133  dholland 	pb = pathbuf_create(sun->sun_path);
    990  1.133  dholland 	if (pb == NULL) {
    991  1.133  dholland 		error = ENOMEM;
    992  1.133  dholland 		goto bad;
    993  1.133  dholland 	}
    994  1.133  dholland 	NDINIT(&nd, CREATE, FOLLOW | LOCKPARENT | TRYEMULROOT, pb);
    995   1.27   thorpej 
    996    1.1       cgd /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
    997  1.133  dholland 	if ((error = namei(&nd)) != 0) {
    998  1.133  dholland 		pathbuf_destroy(pb);
    999   1.27   thorpej 		goto bad;
   1000  1.133  dholland 	}
   1001    1.9   mycroft 	vp = nd.ni_vp;
   1002   1.96   hannken 	if (vp != NULL) {
   1003    1.9   mycroft 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1004    1.9   mycroft 		if (nd.ni_dvp == vp)
   1005    1.9   mycroft 			vrele(nd.ni_dvp);
   1006    1.1       cgd 		else
   1007    1.9   mycroft 			vput(nd.ni_dvp);
   1008    1.1       cgd 		vrele(vp);
   1009  1.133  dholland 		pathbuf_destroy(pb);
   1010   1.96   hannken 		error = EADDRINUSE;
   1011   1.96   hannken 		goto bad;
   1012    1.1       cgd 	}
   1013  1.128     pooka 	vattr_null(&vattr);
   1014    1.1       cgd 	vattr.va_type = VSOCK;
   1015   1.84      jmmv 	vattr.va_mode = ACCESSPERMS & ~(p->p_cwdi->cwdi_cmask);
   1016   1.16  christos 	error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
   1017  1.133  dholland 	if (error) {
   1018  1.149   hannken 		vput(nd.ni_dvp);
   1019  1.133  dholland 		pathbuf_destroy(pb);
   1020   1.27   thorpej 		goto bad;
   1021  1.133  dholland 	}
   1022    1.9   mycroft 	vp = nd.ni_vp;
   1023  1.150   hannken 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1024  1.112        ad 	solock(so);
   1025    1.1       cgd 	vp->v_socket = unp->unp_socket;
   1026    1.1       cgd 	unp->unp_vnode = vp;
   1027   1.27   thorpej 	unp->unp_addrlen = addrlen;
   1028   1.27   thorpej 	unp->unp_addr = sun;
   1029  1.130   hannken 	VOP_UNLOCK(vp);
   1030  1.149   hannken 	vput(nd.ni_dvp);
   1031  1.109        ad 	unp->unp_flags &= ~UNP_BUSY;
   1032  1.133  dholland 	pathbuf_destroy(pb);
   1033    1.1       cgd 	return (0);
   1034   1.27   thorpej 
   1035   1.27   thorpej  bad:
   1036   1.27   thorpej 	free(sun, M_SONAME);
   1037  1.112        ad 	solock(so);
   1038  1.109        ad 	unp->unp_flags &= ~UNP_BUSY;
   1039   1.27   thorpej 	return (error);
   1040    1.1       cgd }
   1041    1.1       cgd 
   1042  1.161       rtr static int
   1043  1.165       rtr unp_listen(struct socket *so, struct lwp *l)
   1044  1.161       rtr {
   1045  1.161       rtr 	struct unpcb *unp = sotounpcb(so);
   1046  1.161       rtr 
   1047  1.161       rtr 	KASSERT(solocked(so));
   1048  1.161       rtr 	KASSERT(unp != NULL);
   1049  1.161       rtr 
   1050  1.161       rtr 	/*
   1051  1.161       rtr 	 * If the socket can accept a connection, it must be
   1052  1.161       rtr 	 * locked by uipc_lock.
   1053  1.161       rtr 	 */
   1054  1.161       rtr 	unp_resetlock(so);
   1055  1.161       rtr 	if (unp->unp_vnode == NULL)
   1056  1.161       rtr 		return EINVAL;
   1057  1.161       rtr 
   1058  1.183  christos 	unp_connid(l, unp, UNP_EIDSBIND);
   1059  1.161       rtr 	return 0;
   1060  1.161       rtr }
   1061  1.161       rtr 
   1062  1.163       rtr static int
   1063  1.163       rtr unp_disconnect(struct socket *so)
   1064  1.163       rtr {
   1065  1.163       rtr 	KASSERT(solocked(so));
   1066  1.163       rtr 	KASSERT(sotounpcb(so) != NULL);
   1067  1.163       rtr 
   1068  1.163       rtr 	unp_disconnect1(sotounpcb(so));
   1069  1.163       rtr 	return 0;
   1070  1.163       rtr }
   1071  1.163       rtr 
   1072  1.163       rtr static int
   1073  1.163       rtr unp_shutdown(struct socket *so)
   1074  1.163       rtr {
   1075  1.163       rtr 	KASSERT(solocked(so));
   1076  1.163       rtr 	KASSERT(sotounpcb(so) != NULL);
   1077  1.163       rtr 
   1078  1.163       rtr 	socantsendmore(so);
   1079  1.163       rtr 	unp_shutdown1(sotounpcb(so));
   1080  1.163       rtr 	return 0;
   1081  1.163       rtr }
   1082  1.163       rtr 
   1083  1.163       rtr static int
   1084  1.163       rtr unp_abort(struct socket *so)
   1085  1.163       rtr {
   1086  1.163       rtr 	KASSERT(solocked(so));
   1087  1.163       rtr 	KASSERT(sotounpcb(so) != NULL);
   1088  1.163       rtr 
   1089  1.163       rtr 	(void)unp_drop(sotounpcb(so), ECONNABORTED);
   1090  1.163       rtr 	KASSERT(so->so_head == NULL);
   1091  1.163       rtr 	KASSERT(so->so_pcb != NULL);
   1092  1.163       rtr 	unp_detach(so);
   1093  1.163       rtr 	return 0;
   1094  1.163       rtr }
   1095  1.163       rtr 
   1096  1.169       rtr static int
   1097  1.173  christos unp_connect1(struct socket *so, struct socket *so2, struct lwp *l)
   1098  1.169       rtr {
   1099  1.169       rtr 	struct unpcb *unp = sotounpcb(so);
   1100  1.169       rtr 	struct unpcb *unp2;
   1101  1.169       rtr 
   1102  1.169       rtr 	if (so2->so_type != so->so_type)
   1103  1.169       rtr 		return EPROTOTYPE;
   1104  1.169       rtr 
   1105  1.169       rtr 	/*
   1106  1.169       rtr 	 * All three sockets involved must be locked by same lock:
   1107  1.169       rtr 	 *
   1108  1.169       rtr 	 * local endpoint (so)
   1109  1.169       rtr 	 * remote endpoint (so2)
   1110  1.169       rtr 	 * queue head (so2->so_head, only if PR_CONNREQUIRED)
   1111  1.169       rtr 	 */
   1112  1.169       rtr 	KASSERT(solocked2(so, so2));
   1113  1.169       rtr 	KASSERT(so->so_head == NULL);
   1114  1.169       rtr 	if (so2->so_head != NULL) {
   1115  1.169       rtr 		KASSERT(so2->so_lock == uipc_lock);
   1116  1.169       rtr 		KASSERT(solocked2(so2, so2->so_head));
   1117  1.169       rtr 	}
   1118  1.169       rtr 
   1119  1.169       rtr 	unp2 = sotounpcb(so2);
   1120  1.169       rtr 	unp->unp_conn = unp2;
   1121  1.173  christos 
   1122  1.169       rtr 	switch (so->so_type) {
   1123  1.169       rtr 
   1124  1.169       rtr 	case SOCK_DGRAM:
   1125  1.169       rtr 		unp->unp_nextref = unp2->unp_refs;
   1126  1.169       rtr 		unp2->unp_refs = unp;
   1127  1.169       rtr 		soisconnected(so);
   1128  1.169       rtr 		break;
   1129  1.169       rtr 
   1130  1.169       rtr 	case SOCK_SEQPACKET: /* FALLTHROUGH */
   1131  1.169       rtr 	case SOCK_STREAM:
   1132  1.169       rtr 
   1133  1.169       rtr 		/*
   1134  1.169       rtr 		 * SOCK_SEQPACKET and SOCK_STREAM cases are handled by callers
   1135  1.169       rtr 		 * which are unp_connect() or unp_connect2().
   1136  1.169       rtr 		 */
   1137  1.169       rtr 
   1138  1.169       rtr 		break;
   1139  1.169       rtr 
   1140  1.169       rtr 	default:
   1141  1.169       rtr 		panic("unp_connect1");
   1142  1.169       rtr 	}
   1143  1.169       rtr 
   1144  1.169       rtr 	return 0;
   1145  1.169       rtr }
   1146  1.169       rtr 
   1147    1.5    andrew int
   1148  1.179       rtr unp_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
   1149    1.1       cgd {
   1150   1.46  augustss 	struct sockaddr_un *sun;
   1151  1.106        ad 	vnode_t *vp;
   1152   1.46  augustss 	struct socket *so2, *so3;
   1153   1.99        he 	struct unpcb *unp, *unp2, *unp3;
   1154   1.27   thorpej 	size_t addrlen;
   1155    1.1       cgd 	int error;
   1156  1.133  dholland 	struct pathbuf *pb;
   1157    1.1       cgd 	struct nameidata nd;
   1158    1.1       cgd 
   1159  1.109        ad 	unp = sotounpcb(so);
   1160  1.109        ad 	if ((unp->unp_flags & UNP_BUSY) != 0) {
   1161  1.109        ad 		/*
   1162  1.109        ad 		 * EALREADY may not be strictly accurate, but since this
   1163  1.109        ad 		 * is a major application error it's hardly a big deal.
   1164  1.109        ad 		 */
   1165  1.109        ad 		return (EALREADY);
   1166  1.109        ad 	}
   1167  1.109        ad 	unp->unp_flags |= UNP_BUSY;
   1168  1.112        ad 	sounlock(so);
   1169  1.109        ad 
   1170  1.179       rtr 	sun = makeun_sb(nam, &addrlen);
   1171  1.133  dholland 	pb = pathbuf_create(sun->sun_path);
   1172  1.133  dholland 	if (pb == NULL) {
   1173  1.133  dholland 		error = ENOMEM;
   1174  1.133  dholland 		goto bad2;
   1175  1.133  dholland 	}
   1176   1.27   thorpej 
   1177  1.133  dholland 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
   1178  1.133  dholland 
   1179  1.133  dholland 	if ((error = namei(&nd)) != 0) {
   1180  1.133  dholland 		pathbuf_destroy(pb);
   1181   1.27   thorpej 		goto bad2;
   1182  1.133  dholland 	}
   1183    1.9   mycroft 	vp = nd.ni_vp;
   1184  1.181      maxv 	pathbuf_destroy(pb);
   1185    1.1       cgd 	if (vp->v_type != VSOCK) {
   1186    1.1       cgd 		error = ENOTSOCK;
   1187    1.1       cgd 		goto bad;
   1188    1.1       cgd 	}
   1189  1.167       rtr 	if ((error = VOP_ACCESS(vp, VWRITE, l->l_cred)) != 0)
   1190    1.1       cgd 		goto bad;
   1191  1.112        ad 	/* Acquire v_interlock to protect against unp_detach(). */
   1192  1.135     rmind 	mutex_enter(vp->v_interlock);
   1193    1.1       cgd 	so2 = vp->v_socket;
   1194  1.112        ad 	if (so2 == NULL) {
   1195  1.135     rmind 		mutex_exit(vp->v_interlock);
   1196    1.1       cgd 		error = ECONNREFUSED;
   1197    1.1       cgd 		goto bad;
   1198    1.1       cgd 	}
   1199    1.1       cgd 	if (so->so_type != so2->so_type) {
   1200  1.135     rmind 		mutex_exit(vp->v_interlock);
   1201    1.1       cgd 		error = EPROTOTYPE;
   1202    1.1       cgd 		goto bad;
   1203    1.1       cgd 	}
   1204  1.112        ad 	solock(so);
   1205  1.112        ad 	unp_resetlock(so);
   1206  1.135     rmind 	mutex_exit(vp->v_interlock);
   1207  1.112        ad 	if ((so->so_proto->pr_flags & PR_CONNREQUIRED) != 0) {
   1208  1.112        ad 		/*
   1209  1.112        ad 		 * This may seem somewhat fragile but is OK: if we can
   1210  1.112        ad 		 * see SO_ACCEPTCONN set on the endpoint, then it must
   1211  1.112        ad 		 * be locked by the domain-wide uipc_lock.
   1212  1.112        ad 		 */
   1213  1.132      yamt 		KASSERT((so2->so_options & SO_ACCEPTCONN) == 0 ||
   1214  1.112        ad 		    so2->so_lock == uipc_lock);
   1215    1.1       cgd 		if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
   1216  1.144     rmind 		    (so3 = sonewconn(so2, false)) == NULL) {
   1217    1.1       cgd 			error = ECONNREFUSED;
   1218  1.112        ad 			sounlock(so);
   1219    1.1       cgd 			goto bad;
   1220    1.1       cgd 		}
   1221    1.1       cgd 		unp2 = sotounpcb(so2);
   1222    1.1       cgd 		unp3 = sotounpcb(so3);
   1223   1.26   thorpej 		if (unp2->unp_addr) {
   1224   1.26   thorpej 			unp3->unp_addr = malloc(unp2->unp_addrlen,
   1225   1.26   thorpej 			    M_SONAME, M_WAITOK);
   1226   1.36     perry 			memcpy(unp3->unp_addr, unp2->unp_addr,
   1227   1.26   thorpej 			    unp2->unp_addrlen);
   1228   1.26   thorpej 			unp3->unp_addrlen = unp2->unp_addrlen;
   1229   1.26   thorpej 		}
   1230   1.30   thorpej 		unp3->unp_flags = unp2->unp_flags;
   1231  1.112        ad 		so2 = so3;
   1232  1.183  christos 		/*
   1233  1.183  christos 		 * The connector's (client's) credentials are copied from its
   1234  1.183  christos 		 * process structure at the time of connect() (which is now).
   1235  1.183  christos 		 */
   1236  1.183  christos 		unp_connid(l, unp3, UNP_EIDSVALID);
   1237  1.183  christos 		 /*
   1238  1.183  christos 		  * The receiver's (server's) credentials are copied from the
   1239  1.183  christos 		  * unp_peercred member of socket on which the former called
   1240  1.183  christos 		  * listen(); unp_listen() cached that process's credentials
   1241  1.183  christos 		  * at that time so we can use them now.
   1242  1.183  christos 		  */
   1243  1.183  christos 		if (unp2->unp_flags & UNP_EIDSBIND) {
   1244  1.183  christos 			memcpy(&unp->unp_connid, &unp2->unp_connid,
   1245  1.183  christos 			    sizeof(unp->unp_connid));
   1246  1.183  christos 			unp->unp_flags |= UNP_EIDSVALID;
   1247  1.183  christos 		}
   1248   1.33   thorpej 	}
   1249  1.173  christos 	error = unp_connect1(so, so2, l);
   1250  1.169       rtr 	if (error) {
   1251  1.169       rtr 		sounlock(so);
   1252  1.169       rtr 		goto bad;
   1253  1.169       rtr 	}
   1254  1.169       rtr 	unp2 = sotounpcb(so2);
   1255  1.169       rtr 	switch (so->so_type) {
   1256  1.169       rtr 
   1257  1.169       rtr 	/*
   1258  1.169       rtr 	 * SOCK_DGRAM and default cases are handled in prior call to
   1259  1.169       rtr 	 * unp_connect1(), do not add a default case without fixing
   1260  1.169       rtr 	 * unp_connect1().
   1261  1.169       rtr 	 */
   1262  1.169       rtr 
   1263  1.169       rtr 	case SOCK_SEQPACKET: /* FALLTHROUGH */
   1264  1.169       rtr 	case SOCK_STREAM:
   1265  1.169       rtr 		unp2->unp_conn = unp;
   1266  1.169       rtr 		if ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT)
   1267  1.169       rtr 			soisconnecting(so);
   1268  1.169       rtr 		else
   1269  1.169       rtr 			soisconnected(so);
   1270  1.169       rtr 		soisconnected(so2);
   1271  1.169       rtr 		/*
   1272  1.169       rtr 		 * If the connection is fully established, break the
   1273  1.169       rtr 		 * association with uipc_lock and give the connected
   1274  1.193   msaitoh 		 * pair a separate lock to share.
   1275  1.169       rtr 		 */
   1276  1.169       rtr 		KASSERT(so2->so_head != NULL);
   1277  1.169       rtr 		unp_setpeerlocks(so, so2);
   1278  1.169       rtr 		break;
   1279  1.169       rtr 
   1280  1.169       rtr 	}
   1281  1.112        ad 	sounlock(so);
   1282   1.27   thorpej  bad:
   1283    1.1       cgd 	vput(vp);
   1284   1.27   thorpej  bad2:
   1285   1.27   thorpej 	free(sun, M_SONAME);
   1286  1.112        ad 	solock(so);
   1287  1.109        ad 	unp->unp_flags &= ~UNP_BUSY;
   1288    1.1       cgd 	return (error);
   1289    1.1       cgd }
   1290    1.1       cgd 
   1291    1.5    andrew int
   1292  1.169       rtr unp_connect2(struct socket *so, struct socket *so2)
   1293    1.1       cgd {
   1294   1.46  augustss 	struct unpcb *unp = sotounpcb(so);
   1295   1.46  augustss 	struct unpcb *unp2;
   1296  1.169       rtr 	int error = 0;
   1297    1.1       cgd 
   1298  1.169       rtr 	KASSERT(solocked2(so, so2));
   1299  1.112        ad 
   1300  1.173  christos 	error = unp_connect1(so, so2, curlwp);
   1301  1.169       rtr 	if (error)
   1302  1.169       rtr 		return error;
   1303  1.112        ad 
   1304    1.1       cgd 	unp2 = sotounpcb(so2);
   1305    1.1       cgd 	switch (so->so_type) {
   1306    1.1       cgd 
   1307  1.169       rtr 	/*
   1308  1.169       rtr 	 * SOCK_DGRAM and default cases are handled in prior call to
   1309  1.169       rtr 	 * unp_connect1(), do not add a default case without fixing
   1310  1.169       rtr 	 * unp_connect1().
   1311  1.169       rtr 	 */
   1312    1.1       cgd 
   1313  1.134      manu 	case SOCK_SEQPACKET: /* FALLTHROUGH */
   1314    1.1       cgd 	case SOCK_STREAM:
   1315    1.1       cgd 		unp2->unp_conn = unp;
   1316  1.169       rtr 		soisconnected(so);
   1317    1.1       cgd 		soisconnected(so2);
   1318    1.1       cgd 		break;
   1319    1.1       cgd 
   1320    1.1       cgd 	}
   1321  1.169       rtr 	return error;
   1322    1.1       cgd }
   1323    1.1       cgd 
   1324  1.164       rtr static void
   1325  1.163       rtr unp_disconnect1(struct unpcb *unp)
   1326    1.1       cgd {
   1327   1.46  augustss 	struct unpcb *unp2 = unp->unp_conn;
   1328  1.112        ad 	struct socket *so;
   1329    1.1       cgd 
   1330    1.1       cgd 	if (unp2 == 0)
   1331    1.1       cgd 		return;
   1332    1.1       cgd 	unp->unp_conn = 0;
   1333  1.112        ad 	so = unp->unp_socket;
   1334  1.112        ad 	switch (so->so_type) {
   1335    1.1       cgd 	case SOCK_DGRAM:
   1336    1.1       cgd 		if (unp2->unp_refs == unp)
   1337    1.1       cgd 			unp2->unp_refs = unp->unp_nextref;
   1338    1.1       cgd 		else {
   1339    1.1       cgd 			unp2 = unp2->unp_refs;
   1340    1.1       cgd 			for (;;) {
   1341  1.112        ad 				KASSERT(solocked2(so, unp2->unp_socket));
   1342    1.1       cgd 				if (unp2 == 0)
   1343  1.163       rtr 					panic("unp_disconnect1");
   1344    1.1       cgd 				if (unp2->unp_nextref == unp)
   1345    1.1       cgd 					break;
   1346    1.1       cgd 				unp2 = unp2->unp_nextref;
   1347    1.1       cgd 			}
   1348    1.1       cgd 			unp2->unp_nextref = unp->unp_nextref;
   1349    1.1       cgd 		}
   1350    1.1       cgd 		unp->unp_nextref = 0;
   1351  1.112        ad 		so->so_state &= ~SS_ISCONNECTED;
   1352    1.1       cgd 		break;
   1353    1.1       cgd 
   1354  1.134      manu 	case SOCK_SEQPACKET: /* FALLTHROUGH */
   1355    1.1       cgd 	case SOCK_STREAM:
   1356  1.112        ad 		KASSERT(solocked2(so, unp2->unp_socket));
   1357  1.112        ad 		soisdisconnected(so);
   1358    1.1       cgd 		unp2->unp_conn = 0;
   1359    1.1       cgd 		soisdisconnected(unp2->unp_socket);
   1360    1.1       cgd 		break;
   1361    1.1       cgd 	}
   1362    1.1       cgd }
   1363    1.1       cgd 
   1364  1.164       rtr static void
   1365  1.163       rtr unp_shutdown1(struct unpcb *unp)
   1366    1.1       cgd {
   1367    1.1       cgd 	struct socket *so;
   1368    1.1       cgd 
   1369  1.134      manu 	switch(unp->unp_socket->so_type) {
   1370  1.134      manu 	case SOCK_SEQPACKET: /* FALLTHROUGH */
   1371  1.134      manu 	case SOCK_STREAM:
   1372  1.134      manu 		if (unp->unp_conn && (so = unp->unp_conn->unp_socket))
   1373  1.134      manu 			socantrcvmore(so);
   1374  1.134      manu 		break;
   1375  1.134      manu 	default:
   1376  1.134      manu 		break;
   1377  1.134      manu 	}
   1378    1.1       cgd }
   1379    1.1       cgd 
   1380  1.164       rtr static bool
   1381   1.76      matt unp_drop(struct unpcb *unp, int errno)
   1382    1.1       cgd {
   1383    1.1       cgd 	struct socket *so = unp->unp_socket;
   1384    1.1       cgd 
   1385  1.112        ad 	KASSERT(solocked(so));
   1386  1.112        ad 
   1387    1.1       cgd 	so->so_error = errno;
   1388  1.163       rtr 	unp_disconnect1(unp);
   1389    1.1       cgd 	if (so->so_head) {
   1390  1.112        ad 		so->so_pcb = NULL;
   1391  1.112        ad 		/* sofree() drops the socket lock */
   1392   1.14   mycroft 		sofree(so);
   1393  1.112        ad 		unp_free(unp);
   1394  1.112        ad 		return true;
   1395    1.1       cgd 	}
   1396  1.112        ad 	return false;
   1397    1.1       cgd }
   1398    1.1       cgd 
   1399    1.1       cgd #ifdef notdef
   1400   1.76      matt unp_drain(void)
   1401    1.1       cgd {
   1402    1.1       cgd 
   1403    1.1       cgd }
   1404    1.1       cgd #endif
   1405    1.1       cgd 
   1406    1.5    andrew int
   1407  1.136  christos unp_externalize(struct mbuf *rights, struct lwp *l, int flags)
   1408    1.1       cgd {
   1409  1.138  christos 	struct cmsghdr * const cm = mtod(rights, struct cmsghdr *);
   1410  1.138  christos 	struct proc * const p = l->l_proc;
   1411  1.106        ad 	file_t **rp;
   1412  1.138  christos 	int error = 0;
   1413   1.47   thorpej 
   1414  1.138  christos 	const size_t nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) /
   1415  1.106        ad 	    sizeof(file_t *);
   1416  1.143  drochner 	if (nfds == 0)
   1417  1.143  drochner 		goto noop;
   1418    1.1       cgd 
   1419  1.138  christos 	int * const fdp = kmem_alloc(nfds * sizeof(int), KM_SLEEP);
   1420  1.198        ad 	rw_enter(&p->p_cwdi->cwdi_lock, RW_READER);
   1421   1.50   thorpej 
   1422  1.121       mrg 	/* Make sure the recipient should be able to see the files.. */
   1423  1.140  christos 	rp = (file_t **)CMSG_DATA(cm);
   1424  1.140  christos 	for (size_t i = 0; i < nfds; i++) {
   1425  1.140  christos 		file_t * const fp = *rp++;
   1426  1.140  christos 		if (fp == NULL) {
   1427  1.140  christos 			error = EINVAL;
   1428  1.140  christos 			goto out;
   1429  1.140  christos 		}
   1430  1.140  christos 		/*
   1431  1.140  christos 		 * If we are in a chroot'ed directory, and
   1432  1.140  christos 		 * someone wants to pass us a directory, make
   1433  1.140  christos 		 * sure it's inside the subtree we're allowed
   1434  1.140  christos 		 * to access.
   1435  1.140  christos 		 */
   1436  1.198        ad 		if (p->p_cwdi->cwdi_rdir != NULL && fp->f_type == DTYPE_VNODE) {
   1437  1.171      matt 			vnode_t *vp = fp->f_vnode;
   1438  1.198        ad 			if ((vp->v_type == VDIR) &&
   1439  1.198        ad 			    !vn_isunder(vp, p->p_cwdi->cwdi_rdir, l)) {
   1440  1.140  christos 				error = EPERM;
   1441  1.140  christos 				goto out;
   1442   1.39  sommerfe 			}
   1443   1.39  sommerfe 		}
   1444   1.39  sommerfe 	}
   1445  1.198        ad 
   1446   1.50   thorpej  restart:
   1447   1.24       cgd 	/*
   1448   1.50   thorpej 	 * First loop -- allocate file descriptor table slots for the
   1449  1.121       mrg 	 * new files.
   1450   1.24       cgd 	 */
   1451  1.138  christos 	for (size_t i = 0; i < nfds; i++) {
   1452  1.106        ad 		if ((error = fd_alloc(p, 0, &fdp[i])) != 0) {
   1453   1.49   thorpej 			/*
   1454   1.50   thorpej 			 * Back out what we've done so far.
   1455   1.49   thorpej 			 */
   1456  1.138  christos 			while (i-- > 0) {
   1457  1.106        ad 				fd_abort(p, NULL, fdp[i]);
   1458  1.106        ad 			}
   1459   1.50   thorpej 			if (error == ENOSPC) {
   1460  1.106        ad 				fd_tryexpand(p);
   1461   1.50   thorpej 				error = 0;
   1462  1.138  christos 				goto restart;
   1463   1.50   thorpej 			}
   1464  1.138  christos 			/*
   1465  1.138  christos 			 * This is the error that has historically
   1466  1.138  christos 			 * been returned, and some callers may
   1467  1.138  christos 			 * expect it.
   1468  1.138  christos 			 */
   1469  1.138  christos 			error = EMSGSIZE;
   1470  1.138  christos 			goto out;
   1471   1.49   thorpej 		}
   1472    1.1       cgd 	}
   1473   1.24       cgd 
   1474   1.24       cgd 	/*
   1475   1.50   thorpej 	 * Now that adding them has succeeded, update all of the
   1476  1.121       mrg 	 * file passing state and affix the descriptors.
   1477  1.112        ad 	 */
   1478  1.106        ad 	rp = (file_t **)CMSG_DATA(cm);
   1479  1.138  christos 	int *ofdp = (int *)CMSG_DATA(cm);
   1480  1.138  christos 	for (size_t i = 0; i < nfds; i++) {
   1481  1.138  christos 		file_t * const fp = *rp++;
   1482  1.138  christos 		const int fd = fdp[i];
   1483  1.106        ad 		atomic_dec_uint(&unp_rights);
   1484  1.136  christos 		fd_set_exclose(l, fd, (flags & O_CLOEXEC) != 0);
   1485  1.136  christos 		fd_affix(p, fp, fd);
   1486  1.138  christos 		/*
   1487  1.138  christos 		 * Done with this file pointer, replace it with a fd;
   1488  1.138  christos 		 */
   1489  1.138  christos 		*ofdp++ = fd;
   1490  1.106        ad 		mutex_enter(&fp->f_lock);
   1491   1.50   thorpej 		fp->f_msgcount--;
   1492  1.106        ad 		mutex_exit(&fp->f_lock);
   1493  1.106        ad 		/*
   1494  1.106        ad 		 * Note that fd_affix() adds a reference to the file.
   1495  1.106        ad 		 * The file may already have been closed by another
   1496  1.106        ad 		 * LWP in the process, so we must drop the reference
   1497  1.106        ad 		 * added by unp_internalize() with closef().
   1498  1.106        ad 		 */
   1499  1.106        ad 		closef(fp);
   1500   1.50   thorpej 	}
   1501   1.50   thorpej 
   1502   1.50   thorpej 	/*
   1503  1.138  christos 	 * Adjust length, in case of transition from large file_t
   1504  1.138  christos 	 * pointers to ints.
   1505   1.50   thorpej 	 */
   1506  1.138  christos 	if (sizeof(file_t *) != sizeof(int)) {
   1507  1.138  christos 		cm->cmsg_len = CMSG_LEN(nfds * sizeof(int));
   1508  1.138  christos 		rights->m_len = CMSG_SPACE(nfds * sizeof(int));
   1509  1.138  christos 	}
   1510   1.50   thorpej  out:
   1511  1.138  christos 	if (__predict_false(error != 0)) {
   1512  1.141  riastrad 		file_t **const fpp = (file_t **)CMSG_DATA(cm);
   1513  1.141  riastrad 		for (size_t i = 0; i < nfds; i++)
   1514  1.141  riastrad 			unp_discard_now(fpp[i]);
   1515  1.141  riastrad 		/*
   1516  1.141  riastrad 		 * Truncate the array so that nobody will try to interpret
   1517  1.141  riastrad 		 * what is now garbage in it.
   1518  1.141  riastrad 		 */
   1519  1.141  riastrad 		cm->cmsg_len = CMSG_LEN(0);
   1520  1.141  riastrad 		rights->m_len = CMSG_SPACE(0);
   1521  1.138  christos 	}
   1522  1.198        ad 	rw_exit(&p->p_cwdi->cwdi_lock);
   1523  1.143  drochner 	kmem_free(fdp, nfds * sizeof(int));
   1524  1.138  christos 
   1525  1.143  drochner  noop:
   1526  1.141  riastrad 	/*
   1527  1.141  riastrad 	 * Don't disclose kernel memory in the alignment space.
   1528  1.141  riastrad 	 */
   1529  1.141  riastrad 	KASSERT(cm->cmsg_len <= rights->m_len);
   1530  1.141  riastrad 	memset(&mtod(rights, char *)[cm->cmsg_len], 0, rights->m_len -
   1531  1.141  riastrad 	    cm->cmsg_len);
   1532  1.139  christos 	return error;
   1533    1.1       cgd }
   1534    1.1       cgd 
   1535  1.164       rtr static int
   1536  1.112        ad unp_internalize(struct mbuf **controlp)
   1537    1.1       cgd {
   1538  1.121       mrg 	filedesc_t *fdescp = curlwp->l_fd;
   1539  1.195  riastrad 	fdtab_t *dt;
   1540  1.108      yamt 	struct mbuf *control = *controlp;
   1541   1.73    martin 	struct cmsghdr *newcm, *cm = mtod(control, struct cmsghdr *);
   1542  1.106        ad 	file_t **rp, **files;
   1543  1.106        ad 	file_t *fp;
   1544   1.46  augustss 	int i, fd, *fdp;
   1545  1.106        ad 	int nfds, error;
   1546  1.121       mrg 	u_int maxmsg;
   1547  1.106        ad 
   1548  1.106        ad 	error = 0;
   1549  1.106        ad 	newcm = NULL;
   1550   1.38   thorpej 
   1551  1.106        ad 	/* Sanity check the control message header. */
   1552   1.66  jdolecek 	if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
   1553  1.117  christos 	    cm->cmsg_len > control->m_len ||
   1554  1.117  christos 	    cm->cmsg_len < CMSG_ALIGN(sizeof(*cm)))
   1555    1.1       cgd 		return (EINVAL);
   1556   1.24       cgd 
   1557  1.106        ad 	/*
   1558  1.106        ad 	 * Verify that the file descriptors are valid, and acquire
   1559  1.106        ad 	 * a reference to each.
   1560  1.106        ad 	 */
   1561   1.47   thorpej 	nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) / sizeof(int);
   1562   1.47   thorpej 	fdp = (int *)CMSG_DATA(cm);
   1563  1.121       mrg 	maxmsg = maxfiles / unp_rights_ratio;
   1564   1.24       cgd 	for (i = 0; i < nfds; i++) {
   1565   1.24       cgd 		fd = *fdp++;
   1566  1.121       mrg 		if (atomic_inc_uint_nv(&unp_rights) > maxmsg) {
   1567  1.121       mrg 			atomic_dec_uint(&unp_rights);
   1568  1.121       mrg 			nfds = i;
   1569  1.121       mrg 			error = EAGAIN;
   1570  1.121       mrg 			goto out;
   1571  1.121       mrg 		}
   1572  1.137    martin 		if ((fp = fd_getfile(fd)) == NULL
   1573  1.137    martin 		    || fp->f_type == DTYPE_KQUEUE) {
   1574  1.137    martin 		    	if (fp)
   1575  1.137    martin 		    		fd_putfile(fd);
   1576  1.121       mrg 			atomic_dec_uint(&unp_rights);
   1577  1.120     pooka 			nfds = i;
   1578  1.106        ad 			error = EBADF;
   1579  1.106        ad 			goto out;
   1580  1.101        ad 		}
   1581   1.24       cgd 	}
   1582   1.24       cgd 
   1583  1.106        ad 	/* Allocate new space and copy header into it. */
   1584  1.106        ad 	newcm = malloc(CMSG_SPACE(nfds * sizeof(file_t *)), M_MBUF, M_WAITOK);
   1585  1.106        ad 	if (newcm == NULL) {
   1586  1.106        ad 		error = E2BIG;
   1587  1.106        ad 		goto out;
   1588  1.106        ad 	}
   1589  1.106        ad 	memcpy(newcm, cm, sizeof(struct cmsghdr));
   1590  1.194      maxv 	memset(newcm + 1, 0, CMSG_LEN(0) - sizeof(struct cmsghdr));
   1591  1.106        ad 	files = (file_t **)CMSG_DATA(newcm);
   1592  1.106        ad 
   1593   1.24       cgd 	/*
   1594  1.106        ad 	 * Transform the file descriptors into file_t pointers, in
   1595   1.24       cgd 	 * reverse order so that if pointers are bigger than ints, the
   1596  1.106        ad 	 * int won't get until we're done.  No need to lock, as we have
   1597  1.106        ad 	 * already validated the descriptors with fd_getfile().
   1598   1.24       cgd 	 */
   1599   1.94    cbiere 	fdp = (int *)CMSG_DATA(cm) + nfds;
   1600   1.94    cbiere 	rp = files + nfds;
   1601   1.24       cgd 	for (i = 0; i < nfds; i++) {
   1602  1.195  riastrad 		dt = atomic_load_consume(&fdescp->fd_dt);
   1603  1.196  riastrad 		fp = atomic_load_consume(&dt->dt_ff[*--fdp]->ff_file);
   1604  1.106        ad 		KASSERT(fp != NULL);
   1605  1.106        ad 		mutex_enter(&fp->f_lock);
   1606   1.94    cbiere 		*--rp = fp;
   1607    1.1       cgd 		fp->f_count++;
   1608    1.1       cgd 		fp->f_msgcount++;
   1609  1.106        ad 		mutex_exit(&fp->f_lock);
   1610  1.106        ad 	}
   1611  1.106        ad 
   1612  1.106        ad  out:
   1613  1.106        ad  	/* Release descriptor references. */
   1614  1.106        ad 	fdp = (int *)CMSG_DATA(cm);
   1615  1.106        ad 	for (i = 0; i < nfds; i++) {
   1616  1.106        ad 		fd_putfile(*fdp++);
   1617  1.121       mrg 		if (error != 0) {
   1618  1.121       mrg 			atomic_dec_uint(&unp_rights);
   1619  1.121       mrg 		}
   1620    1.1       cgd 	}
   1621   1.73    martin 
   1622  1.106        ad 	if (error == 0) {
   1623  1.108      yamt 		if (control->m_flags & M_EXT) {
   1624  1.108      yamt 			m_freem(control);
   1625  1.108      yamt 			*controlp = control = m_get(M_WAIT, MT_CONTROL);
   1626  1.108      yamt 		}
   1627  1.106        ad 		MEXTADD(control, newcm, CMSG_SPACE(nfds * sizeof(file_t *)),
   1628   1.73    martin 		    M_MBUF, NULL, NULL);
   1629   1.73    martin 		cm = newcm;
   1630  1.106        ad 		/*
   1631  1.106        ad 		 * Adjust message & mbuf to note amount of space
   1632  1.106        ad 		 * actually used.
   1633  1.106        ad 		 */
   1634  1.106        ad 		cm->cmsg_len = CMSG_LEN(nfds * sizeof(file_t *));
   1635  1.106        ad 		control->m_len = CMSG_SPACE(nfds * sizeof(file_t *));
   1636   1.73    martin 	}
   1637   1.73    martin 
   1638  1.106        ad 	return error;
   1639   1.30   thorpej }
   1640   1.30   thorpej 
   1641   1.30   thorpej struct mbuf *
   1642   1.92        ad unp_addsockcred(struct lwp *l, struct mbuf *control)
   1643   1.30   thorpej {
   1644   1.30   thorpej 	struct sockcred *sc;
   1645  1.142  christos 	struct mbuf *m;
   1646  1.142  christos 	void *p;
   1647   1.30   thorpej 
   1648  1.142  christos 	m = sbcreatecontrol1(&p, SOCKCREDSIZE(kauth_cred_ngroups(l->l_cred)),
   1649  1.142  christos 		SCM_CREDS, SOL_SOCKET, M_WAITOK);
   1650  1.142  christos 	if (m == NULL)
   1651  1.142  christos 		return control;
   1652  1.180       roy 
   1653  1.142  christos 	sc = p;
   1654  1.180       roy 	sc->sc_pid = l->l_proc->p_pid;
   1655   1.92        ad 	sc->sc_uid = kauth_cred_getuid(l->l_cred);
   1656   1.92        ad 	sc->sc_euid = kauth_cred_geteuid(l->l_cred);
   1657   1.92        ad 	sc->sc_gid = kauth_cred_getgid(l->l_cred);
   1658   1.92        ad 	sc->sc_egid = kauth_cred_getegid(l->l_cred);
   1659   1.92        ad 	sc->sc_ngroups = kauth_cred_ngroups(l->l_cred);
   1660  1.142  christos 
   1661  1.142  christos 	for (int i = 0; i < sc->sc_ngroups; i++)
   1662   1.92        ad 		sc->sc_groups[i] = kauth_cred_group(l->l_cred, i);
   1663   1.30   thorpej 
   1664  1.142  christos 	return m_add(control, m);
   1665    1.1       cgd }
   1666    1.1       cgd 
   1667   1.39  sommerfe /*
   1668  1.121       mrg  * Do a mark-sweep GC of files in the system, to free up any which are
   1669  1.121       mrg  * caught in flight to an about-to-be-closed socket.  Additionally,
   1670  1.121       mrg  * process deferred file closures.
   1671   1.39  sommerfe  */
   1672  1.121       mrg static void
   1673  1.121       mrg unp_gc(file_t *dp)
   1674    1.1       cgd {
   1675  1.121       mrg 	extern	struct domain unixdomain;
   1676  1.121       mrg 	file_t *fp, *np;
   1677   1.46  augustss 	struct socket *so, *so1;
   1678  1.170      matt 	u_int i, oflags, rflags;
   1679  1.121       mrg 	bool didwork;
   1680    1.1       cgd 
   1681  1.121       mrg 	KASSERT(curlwp == unp_thread_lwp);
   1682  1.121       mrg 	KASSERT(mutex_owned(&filelist_lock));
   1683  1.106        ad 
   1684  1.121       mrg 	/*
   1685  1.121       mrg 	 * First, process deferred file closures.
   1686  1.121       mrg 	 */
   1687  1.121       mrg 	while (!SLIST_EMPTY(&unp_thread_discard)) {
   1688  1.121       mrg 		fp = SLIST_FIRST(&unp_thread_discard);
   1689  1.121       mrg 		KASSERT(fp->f_unpcount > 0);
   1690  1.121       mrg 		KASSERT(fp->f_count > 0);
   1691  1.121       mrg 		KASSERT(fp->f_msgcount > 0);
   1692  1.121       mrg 		KASSERT(fp->f_count >= fp->f_unpcount);
   1693  1.121       mrg 		KASSERT(fp->f_count >= fp->f_msgcount);
   1694  1.121       mrg 		KASSERT(fp->f_msgcount >= fp->f_unpcount);
   1695  1.121       mrg 		SLIST_REMOVE_HEAD(&unp_thread_discard, f_unplist);
   1696  1.121       mrg 		i = fp->f_unpcount;
   1697  1.121       mrg 		fp->f_unpcount = 0;
   1698  1.121       mrg 		mutex_exit(&filelist_lock);
   1699  1.121       mrg 		for (; i != 0; i--) {
   1700  1.121       mrg 			unp_discard_now(fp);
   1701  1.121       mrg 		}
   1702  1.121       mrg 		mutex_enter(&filelist_lock);
   1703  1.121       mrg 	}
   1704   1.39  sommerfe 
   1705  1.121       mrg 	/*
   1706  1.121       mrg 	 * Clear mark bits.  Ensure that we don't consider new files
   1707  1.121       mrg 	 * entering the file table during this loop (they will not have
   1708  1.121       mrg 	 * FSCAN set).
   1709  1.121       mrg 	 */
   1710  1.106        ad 	unp_defer = 0;
   1711  1.106        ad 	LIST_FOREACH(fp, &filehead, f_list) {
   1712  1.170      matt 		for (oflags = fp->f_flag;; oflags = rflags) {
   1713  1.170      matt 			rflags = atomic_cas_uint(&fp->f_flag, oflags,
   1714  1.170      matt 			    (oflags | FSCAN) & ~(FMARK|FDEFER));
   1715  1.170      matt 			if (__predict_true(oflags == rflags)) {
   1716  1.121       mrg 				break;
   1717  1.121       mrg 			}
   1718  1.121       mrg 		}
   1719  1.106        ad 	}
   1720   1.39  sommerfe 
   1721   1.39  sommerfe 	/*
   1722  1.121       mrg 	 * Iterate over the set of sockets, marking ones believed (based on
   1723  1.121       mrg 	 * refcount) to be referenced from a process, and marking for rescan
   1724  1.121       mrg 	 * sockets which are queued on a socket.  Recan continues descending
   1725  1.121       mrg 	 * and searching for sockets referenced by sockets (FDEFER), until
   1726  1.121       mrg 	 * there are no more socket->socket references to be discovered.
   1727   1.39  sommerfe 	 */
   1728    1.1       cgd 	do {
   1729  1.121       mrg 		didwork = false;
   1730  1.121       mrg 		for (fp = LIST_FIRST(&filehead); fp != NULL; fp = np) {
   1731  1.121       mrg 			KASSERT(mutex_owned(&filelist_lock));
   1732  1.121       mrg 			np = LIST_NEXT(fp, f_list);
   1733  1.106        ad 			mutex_enter(&fp->f_lock);
   1734  1.121       mrg 			if ((fp->f_flag & FDEFER) != 0) {
   1735  1.106        ad 				atomic_and_uint(&fp->f_flag, ~FDEFER);
   1736    1.1       cgd 				unp_defer--;
   1737  1.175  christos 				if (fp->f_count == 0) {
   1738  1.175  christos 					/*
   1739  1.175  christos 					 * XXX: closef() doesn't pay attention
   1740  1.175  christos 					 * to FDEFER
   1741  1.175  christos 					 */
   1742  1.175  christos 					mutex_exit(&fp->f_lock);
   1743  1.175  christos 					continue;
   1744  1.175  christos 				}
   1745    1.1       cgd 			} else {
   1746  1.101        ad 				if (fp->f_count == 0 ||
   1747  1.121       mrg 				    (fp->f_flag & FMARK) != 0 ||
   1748  1.121       mrg 				    fp->f_count == fp->f_msgcount ||
   1749  1.121       mrg 				    fp->f_unpcount != 0) {
   1750  1.106        ad 					mutex_exit(&fp->f_lock);
   1751    1.1       cgd 					continue;
   1752  1.101        ad 				}
   1753    1.1       cgd 			}
   1754  1.106        ad 			atomic_or_uint(&fp->f_flag, FMARK);
   1755   1.39  sommerfe 
   1756    1.1       cgd 			if (fp->f_type != DTYPE_SOCKET ||
   1757  1.171      matt 			    (so = fp->f_socket) == NULL ||
   1758  1.101        ad 			    so->so_proto->pr_domain != &unixdomain ||
   1759  1.121       mrg 			    (so->so_proto->pr_flags & PR_RIGHTS) == 0) {
   1760  1.106        ad 				mutex_exit(&fp->f_lock);
   1761    1.1       cgd 				continue;
   1762  1.101        ad 			}
   1763  1.121       mrg 
   1764  1.121       mrg 			/* Gain file ref, mark our position, and unlock. */
   1765  1.121       mrg 			didwork = true;
   1766  1.121       mrg 			LIST_INSERT_AFTER(fp, dp, f_list);
   1767  1.121       mrg 			fp->f_count++;
   1768  1.106        ad 			mutex_exit(&fp->f_lock);
   1769  1.121       mrg 			mutex_exit(&filelist_lock);
   1770  1.101        ad 
   1771  1.112        ad 			/*
   1772  1.121       mrg 			 * Mark files referenced from sockets queued on the
   1773  1.121       mrg 			 * accept queue as well.
   1774  1.112        ad 			 */
   1775  1.112        ad 			solock(so);
   1776   1.39  sommerfe 			unp_scan(so->so_rcv.sb_mb, unp_mark, 0);
   1777  1.121       mrg 			if ((so->so_options & SO_ACCEPTCONN) != 0) {
   1778   1.54      matt 				TAILQ_FOREACH(so1, &so->so_q0, so_qe) {
   1779   1.39  sommerfe 					unp_scan(so1->so_rcv.sb_mb, unp_mark, 0);
   1780   1.39  sommerfe 				}
   1781   1.54      matt 				TAILQ_FOREACH(so1, &so->so_q, so_qe) {
   1782   1.39  sommerfe 					unp_scan(so1->so_rcv.sb_mb, unp_mark, 0);
   1783   1.39  sommerfe 				}
   1784   1.39  sommerfe 			}
   1785  1.112        ad 			sounlock(so);
   1786  1.121       mrg 
   1787  1.121       mrg 			/* Re-lock and restart from where we left off. */
   1788  1.121       mrg 			closef(fp);
   1789  1.121       mrg 			mutex_enter(&filelist_lock);
   1790  1.121       mrg 			np = LIST_NEXT(dp, f_list);
   1791  1.121       mrg 			LIST_REMOVE(dp, f_list);
   1792    1.1       cgd 		}
   1793  1.121       mrg 		/*
   1794  1.121       mrg 		 * Bail early if we did nothing in the loop above.  Could
   1795  1.121       mrg 		 * happen because of concurrent activity causing unp_defer
   1796  1.121       mrg 		 * to get out of sync.
   1797  1.121       mrg 		 */
   1798  1.121       mrg 	} while (unp_defer != 0 && didwork);
   1799  1.101        ad 
   1800    1.8   mycroft 	/*
   1801  1.121       mrg 	 * Sweep pass.
   1802    1.8   mycroft 	 *
   1803  1.121       mrg 	 * We grab an extra reference to each of the files that are
   1804  1.121       mrg 	 * not otherwise accessible and then free the rights that are
   1805  1.121       mrg 	 * stored in messages on them.
   1806    1.8   mycroft 	 */
   1807  1.121       mrg 	for (fp = LIST_FIRST(&filehead); fp != NULL; fp = np) {
   1808  1.121       mrg 		KASSERT(mutex_owned(&filelist_lock));
   1809  1.121       mrg 		np = LIST_NEXT(fp, f_list);
   1810  1.106        ad 		mutex_enter(&fp->f_lock);
   1811  1.121       mrg 
   1812  1.121       mrg 		/*
   1813  1.121       mrg 		 * Ignore non-sockets.
   1814  1.121       mrg 		 * Ignore dead sockets, or sockets with pending close.
   1815  1.205  riastrad 		 * Ignore sockets obviously referenced elsewhere.
   1816  1.121       mrg 		 * Ignore sockets marked as referenced by our scan.
   1817  1.121       mrg 		 * Ignore new sockets that did not exist during the scan.
   1818  1.121       mrg 		 */
   1819  1.121       mrg 		if (fp->f_type != DTYPE_SOCKET ||
   1820  1.121       mrg 		    fp->f_count == 0 || fp->f_unpcount != 0 ||
   1821  1.121       mrg 		    fp->f_count != fp->f_msgcount ||
   1822  1.121       mrg 		    (fp->f_flag & (FMARK | FSCAN)) != FSCAN) {
   1823  1.121       mrg 			mutex_exit(&fp->f_lock);
   1824  1.121       mrg 			continue;
   1825    1.8   mycroft 		}
   1826  1.121       mrg 
   1827  1.121       mrg 		/* Gain file ref, mark our position, and unlock. */
   1828  1.121       mrg 		LIST_INSERT_AFTER(fp, dp, f_list);
   1829  1.121       mrg 		fp->f_count++;
   1830  1.106        ad 		mutex_exit(&fp->f_lock);
   1831  1.121       mrg 		mutex_exit(&filelist_lock);
   1832  1.121       mrg 
   1833  1.121       mrg 		/*
   1834  1.121       mrg 		 * Flush all data from the socket's receive buffer.
   1835  1.121       mrg 		 * This will cause files referenced only by the
   1836  1.121       mrg 		 * socket to be queued for close.
   1837  1.121       mrg 		 */
   1838  1.171      matt 		so = fp->f_socket;
   1839  1.121       mrg 		solock(so);
   1840  1.121       mrg 		sorflush(so);
   1841  1.121       mrg 		sounlock(so);
   1842  1.121       mrg 
   1843  1.121       mrg 		/* Re-lock and restart from where we left off. */
   1844  1.121       mrg 		closef(fp);
   1845  1.121       mrg 		mutex_enter(&filelist_lock);
   1846  1.121       mrg 		np = LIST_NEXT(dp, f_list);
   1847  1.121       mrg 		LIST_REMOVE(dp, f_list);
   1848  1.121       mrg 	}
   1849  1.121       mrg }
   1850  1.121       mrg 
   1851  1.121       mrg /*
   1852  1.121       mrg  * Garbage collector thread.  While SCM_RIGHTS messages are in transit,
   1853  1.121       mrg  * wake once per second to garbage collect.  Run continually while we
   1854  1.121       mrg  * have deferred closes to process.
   1855  1.121       mrg  */
   1856  1.121       mrg static void
   1857  1.121       mrg unp_thread(void *cookie)
   1858  1.121       mrg {
   1859  1.121       mrg 	file_t *dp;
   1860  1.121       mrg 
   1861  1.121       mrg 	/* Allocate a dummy file for our scans. */
   1862  1.121       mrg 	if ((dp = fgetdummy()) == NULL) {
   1863  1.121       mrg 		panic("unp_thread");
   1864    1.1       cgd 	}
   1865  1.101        ad 
   1866  1.121       mrg 	mutex_enter(&filelist_lock);
   1867  1.121       mrg 	for (;;) {
   1868  1.121       mrg 		KASSERT(mutex_owned(&filelist_lock));
   1869  1.121       mrg 		if (SLIST_EMPTY(&unp_thread_discard)) {
   1870  1.121       mrg 			if (unp_rights != 0) {
   1871  1.121       mrg 				(void)cv_timedwait(&unp_thread_cv,
   1872  1.121       mrg 				    &filelist_lock, hz);
   1873  1.121       mrg 			} else {
   1874  1.121       mrg 				cv_wait(&unp_thread_cv, &filelist_lock);
   1875  1.121       mrg 			}
   1876  1.112        ad 		}
   1877  1.121       mrg 		unp_gc(dp);
   1878   1.39  sommerfe 	}
   1879  1.121       mrg 	/* NOTREACHED */
   1880  1.121       mrg }
   1881  1.121       mrg 
   1882  1.121       mrg /*
   1883  1.121       mrg  * Kick the garbage collector into action if there is something for
   1884  1.121       mrg  * it to process.
   1885  1.121       mrg  */
   1886  1.121       mrg static void
   1887  1.121       mrg unp_thread_kick(void)
   1888  1.121       mrg {
   1889  1.121       mrg 
   1890  1.121       mrg 	if (!SLIST_EMPTY(&unp_thread_discard) || unp_rights != 0) {
   1891  1.121       mrg 		mutex_enter(&filelist_lock);
   1892  1.121       mrg 		cv_signal(&unp_thread_cv);
   1893  1.121       mrg 		mutex_exit(&filelist_lock);
   1894   1.44   thorpej 	}
   1895    1.1       cgd }
   1896    1.1       cgd 
   1897    1.5    andrew void
   1898   1.76      matt unp_dispose(struct mbuf *m)
   1899    1.1       cgd {
   1900    1.8   mycroft 
   1901    1.1       cgd 	if (m)
   1902  1.121       mrg 		unp_scan(m, unp_discard_later, 1);
   1903    1.1       cgd }
   1904    1.1       cgd 
   1905    1.5    andrew void
   1906  1.106        ad unp_scan(struct mbuf *m0, void (*op)(file_t *), int discard)
   1907    1.1       cgd {
   1908   1.46  augustss 	struct mbuf *m;
   1909  1.121       mrg 	file_t **rp, *fp;
   1910   1.46  augustss 	struct cmsghdr *cm;
   1911  1.121       mrg 	int i, qfds;
   1912    1.1       cgd 
   1913    1.1       cgd 	while (m0) {
   1914   1.48   thorpej 		for (m = m0; m; m = m->m_next) {
   1915  1.121       mrg 			if (m->m_type != MT_CONTROL ||
   1916  1.121       mrg 			    m->m_len < sizeof(*cm)) {
   1917  1.121       mrg 			    	continue;
   1918  1.121       mrg 			}
   1919  1.121       mrg 			cm = mtod(m, struct cmsghdr *);
   1920  1.121       mrg 			if (cm->cmsg_level != SOL_SOCKET ||
   1921  1.121       mrg 			    cm->cmsg_type != SCM_RIGHTS)
   1922  1.121       mrg 				continue;
   1923  1.121       mrg 			qfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm)))
   1924  1.121       mrg 			    / sizeof(file_t *);
   1925  1.121       mrg 			rp = (file_t **)CMSG_DATA(cm);
   1926  1.121       mrg 			for (i = 0; i < qfds; i++) {
   1927  1.121       mrg 				fp = *rp;
   1928  1.121       mrg 				if (discard) {
   1929  1.121       mrg 					*rp = 0;
   1930   1.39  sommerfe 				}
   1931  1.121       mrg 				(*op)(fp);
   1932  1.121       mrg 				rp++;
   1933    1.1       cgd 			}
   1934   1.48   thorpej 		}
   1935   1.52   thorpej 		m0 = m0->m_nextpkt;
   1936    1.1       cgd 	}
   1937    1.1       cgd }
   1938    1.1       cgd 
   1939    1.5    andrew void
   1940  1.106        ad unp_mark(file_t *fp)
   1941    1.1       cgd {
   1942  1.101        ad 
   1943   1.39  sommerfe 	if (fp == NULL)
   1944   1.39  sommerfe 		return;
   1945   1.80     perry 
   1946   1.39  sommerfe 	/* If we're already deferred, don't screw up the defer count */
   1947  1.106        ad 	mutex_enter(&fp->f_lock);
   1948  1.101        ad 	if (fp->f_flag & (FMARK | FDEFER)) {
   1949  1.106        ad 		mutex_exit(&fp->f_lock);
   1950    1.1       cgd 		return;
   1951  1.101        ad 	}
   1952   1.39  sommerfe 
   1953   1.39  sommerfe 	/*
   1954  1.121       mrg 	 * Minimize the number of deferrals...  Sockets are the only type of
   1955  1.121       mrg 	 * file which can hold references to another file, so just mark
   1956  1.121       mrg 	 * other files, and defer unmarked sockets for the next pass.
   1957   1.39  sommerfe 	 */
   1958   1.39  sommerfe 	if (fp->f_type == DTYPE_SOCKET) {
   1959   1.39  sommerfe 		unp_defer++;
   1960  1.106        ad 		KASSERT(fp->f_count != 0);
   1961  1.106        ad 		atomic_or_uint(&fp->f_flag, FDEFER);
   1962   1.39  sommerfe 	} else {
   1963  1.106        ad 		atomic_or_uint(&fp->f_flag, FMARK);
   1964   1.39  sommerfe 	}
   1965  1.106        ad 	mutex_exit(&fp->f_lock);
   1966    1.1       cgd }
   1967    1.1       cgd 
   1968  1.121       mrg static void
   1969  1.121       mrg unp_discard_now(file_t *fp)
   1970    1.1       cgd {
   1971  1.106        ad 
   1972   1.39  sommerfe 	if (fp == NULL)
   1973   1.39  sommerfe 		return;
   1974  1.106        ad 
   1975  1.121       mrg 	KASSERT(fp->f_count > 0);
   1976  1.121       mrg 	KASSERT(fp->f_msgcount > 0);
   1977  1.121       mrg 
   1978  1.106        ad 	mutex_enter(&fp->f_lock);
   1979    1.1       cgd 	fp->f_msgcount--;
   1980  1.106        ad 	mutex_exit(&fp->f_lock);
   1981  1.106        ad 	atomic_dec_uint(&unp_rights);
   1982  1.106        ad 	(void)closef(fp);
   1983    1.1       cgd }
   1984  1.121       mrg 
   1985  1.121       mrg static void
   1986  1.121       mrg unp_discard_later(file_t *fp)
   1987  1.121       mrg {
   1988  1.121       mrg 
   1989  1.121       mrg 	if (fp == NULL)
   1990  1.121       mrg 		return;
   1991  1.121       mrg 
   1992  1.121       mrg 	KASSERT(fp->f_count > 0);
   1993  1.121       mrg 	KASSERT(fp->f_msgcount > 0);
   1994  1.121       mrg 
   1995  1.121       mrg 	mutex_enter(&filelist_lock);
   1996  1.121       mrg 	if (fp->f_unpcount++ == 0) {
   1997  1.121       mrg 		SLIST_INSERT_HEAD(&unp_thread_discard, fp, f_unplist);
   1998  1.121       mrg 	}
   1999  1.121       mrg 	mutex_exit(&filelist_lock);
   2000  1.121       mrg }
   2001  1.151     rmind 
   2002  1.200  christos static void
   2003  1.200  christos unp_sysctl_create(void)
   2004  1.185  christos {
   2005  1.200  christos 
   2006  1.200  christos 	KASSERT(usrreq_sysctllog == NULL);
   2007  1.200  christos 	sysctl_createv(&usrreq_sysctllog, 0, NULL, NULL,
   2008  1.185  christos 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2009  1.185  christos 		       CTLTYPE_LONG, "sendspace",
   2010  1.185  christos 		       SYSCTL_DESCR("Default stream send space"),
   2011  1.185  christos 		       NULL, 0, &unpst_sendspace, 0,
   2012  1.185  christos 		       CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_CREATE, CTL_EOL);
   2013  1.200  christos 	sysctl_createv(&usrreq_sysctllog, 0, NULL, NULL,
   2014  1.185  christos 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2015  1.185  christos 		       CTLTYPE_LONG, "recvspace",
   2016  1.185  christos 		       SYSCTL_DESCR("Default stream recv space"),
   2017  1.185  christos 		       NULL, 0, &unpst_recvspace, 0,
   2018  1.185  christos 		       CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_CREATE, CTL_EOL);
   2019  1.200  christos 	sysctl_createv(&usrreq_sysctllog, 0, NULL, NULL,
   2020  1.185  christos 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2021  1.185  christos 		       CTLTYPE_LONG, "sendspace",
   2022  1.185  christos 		       SYSCTL_DESCR("Default datagram send space"),
   2023  1.185  christos 		       NULL, 0, &unpdg_sendspace, 0,
   2024  1.185  christos 		       CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_CREATE, CTL_EOL);
   2025  1.200  christos 	sysctl_createv(&usrreq_sysctllog, 0, NULL, NULL,
   2026  1.185  christos 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2027  1.185  christos 		       CTLTYPE_LONG, "recvspace",
   2028  1.185  christos 		       SYSCTL_DESCR("Default datagram recv space"),
   2029  1.185  christos 		       NULL, 0, &unpdg_recvspace, 0,
   2030  1.185  christos 		       CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_CREATE, CTL_EOL);
   2031  1.200  christos 	sysctl_createv(&usrreq_sysctllog, 0, NULL, NULL,
   2032  1.185  christos 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
   2033  1.185  christos 		       CTLTYPE_INT, "inflight",
   2034  1.185  christos 		       SYSCTL_DESCR("File descriptors in flight"),
   2035  1.185  christos 		       NULL, 0, &unp_rights, 0,
   2036  1.185  christos 		       CTL_NET, PF_LOCAL, CTL_CREATE, CTL_EOL);
   2037  1.200  christos 	sysctl_createv(&usrreq_sysctllog, 0, NULL, NULL,
   2038  1.185  christos 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
   2039  1.185  christos 		       CTLTYPE_INT, "deferred",
   2040  1.185  christos 		       SYSCTL_DESCR("File descriptors deferred for close"),
   2041  1.185  christos 		       NULL, 0, &unp_defer, 0,
   2042  1.185  christos 		       CTL_NET, PF_LOCAL, CTL_CREATE, CTL_EOL);
   2043  1.185  christos }
   2044  1.185  christos 
   2045  1.151     rmind const struct pr_usrreqs unp_usrreqs = {
   2046  1.152     rmind 	.pr_attach	= unp_attach,
   2047  1.152     rmind 	.pr_detach	= unp_detach,
   2048  1.159       rtr 	.pr_accept	= unp_accept,
   2049  1.161       rtr 	.pr_bind	= unp_bind,
   2050  1.161       rtr 	.pr_listen	= unp_listen,
   2051  1.162       rtr 	.pr_connect	= unp_connect,
   2052  1.169       rtr 	.pr_connect2	= unp_connect2,
   2053  1.163       rtr 	.pr_disconnect	= unp_disconnect,
   2054  1.163       rtr 	.pr_shutdown	= unp_shutdown,
   2055  1.163       rtr 	.pr_abort	= unp_abort,
   2056  1.154       rtr 	.pr_ioctl	= unp_ioctl,
   2057  1.156       rtr 	.pr_stat	= unp_stat,
   2058  1.158       rtr 	.pr_peeraddr	= unp_peeraddr,
   2059  1.158       rtr 	.pr_sockaddr	= unp_sockaddr,
   2060  1.168       rtr 	.pr_rcvd	= unp_rcvd,
   2061  1.160       rtr 	.pr_recvoob	= unp_recvoob,
   2062  1.166       rtr 	.pr_send	= unp_send,
   2063  1.160       rtr 	.pr_sendoob	= unp_sendoob,
   2064  1.151     rmind };
   2065