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