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