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