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