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