Home | History | Annotate | Line # | Download | only in kern
uipc_usrreq.c revision 1.119
      1 /*	$NetBSD: uipc_usrreq.c,v 1.119 2008/10/11 13:40:57 pooka Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 2000, 2004, 2008 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.
     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.119 2008/10/11 13:40:57 pooka Exp $");
    100 
    101 #include <sys/param.h>
    102 #include <sys/systm.h>
    103 #include <sys/proc.h>
    104 #include <sys/filedesc.h>
    105 #include <sys/domain.h>
    106 #include <sys/protosw.h>
    107 #include <sys/socket.h>
    108 #include <sys/socketvar.h>
    109 #include <sys/unpcb.h>
    110 #include <sys/un.h>
    111 #include <sys/namei.h>
    112 #include <sys/vnode.h>
    113 #include <sys/file.h>
    114 #include <sys/stat.h>
    115 #include <sys/mbuf.h>
    116 #include <sys/kauth.h>
    117 #include <sys/kmem.h>
    118 #include <sys/atomic.h>
    119 #include <sys/uidinfo.h>
    120 
    121 /*
    122  * Unix communications domain.
    123  *
    124  * TODO:
    125  *	SEQPACKET, RDM
    126  *	rethink name space problems
    127  *	need a proper out-of-band
    128  *
    129  * Notes on locking:
    130  *
    131  * The generic rules noted in uipc_socket2.c apply.  In addition:
    132  *
    133  * o We have a global lock, uipc_lock.
    134  *
    135  * o All datagram sockets are locked by uipc_lock.
    136  *
    137  * o For stream socketpairs, the two endpoints are created sharing the same
    138  *   independent lock.  Sockets presented to PRU_CONNECT2 must already have
    139  *   matching locks.
    140  *
    141  * o Stream sockets created via socket() start life with their own
    142  *   independent lock.
    143  *
    144  * o Stream connections to a named endpoint are slightly more complicated.
    145  *   Sockets that have called listen() have their lock pointer mutated to
    146  *   the global uipc_lock.  When establishing a connection, the connecting
    147  *   socket also has its lock mutated to uipc_lock, which matches the head
    148  *   (listening socket).  We create a new socket for accept() to return, and
    149  *   that also shares the head's lock.  Until the connection is completely
    150  *   done on both ends, all three sockets are locked by uipc_lock.  Once the
    151  *   connection is complete, the association with the head's lock is broken.
    152  *   The connecting socket and the socket returned from accept() have their
    153  *   lock pointers mutated away from uipc_lock, and back to the connecting
    154  *   socket's original, independent lock.  The head continues to be locked
    155  *   by uipc_lock.
    156  *
    157  * o If uipc_lock is determined to be a significant source of contention,
    158  *   it could easily be hashed out.  It is difficult to simply make it an
    159  *   independent lock because of visibility / garbage collection issues:
    160  *   if a socket has been associated with a lock at any point, that lock
    161  *   must remain valid until the socket is no longer visible in the system.
    162  *   The lock must not be freed or otherwise destroyed until any sockets
    163  *   that had referenced it have also been destroyed.
    164  */
    165 const struct sockaddr_un sun_noname = {
    166 	.sun_len = sizeof(sun_noname),
    167 	.sun_family = AF_LOCAL,
    168 };
    169 ino_t	unp_ino;			/* prototype for fake inode numbers */
    170 
    171 struct mbuf *unp_addsockcred(struct lwp *, struct mbuf *);
    172 static kmutex_t *uipc_lock;
    173 
    174 /*
    175  * Initialize Unix protocols.
    176  */
    177 void
    178 uipc_init(void)
    179 {
    180 
    181 	uipc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    182 }
    183 
    184 /*
    185  * A connection succeeded: disassociate both endpoints from the head's
    186  * lock, and make them share their own lock.  There is a race here: for
    187  * a very brief time one endpoint will be locked by a different lock
    188  * than the other end.  However, since the current thread holds the old
    189  * lock (the listening socket's lock, the head) access can still only be
    190  * made to one side of the connection.
    191  */
    192 static void
    193 unp_setpeerlocks(struct socket *so, struct socket *so2)
    194 {
    195 	struct unpcb *unp;
    196 	kmutex_t *lock;
    197 
    198 	KASSERT(solocked2(so, so2));
    199 
    200 	/*
    201 	 * Bail out if either end of the socket is not yet fully
    202 	 * connected or accepted.  We only break the lock association
    203 	 * with the head when the pair of sockets stand completely
    204 	 * on their own.
    205 	 */
    206 	if (so->so_head != NULL || so2->so_head != NULL)
    207 		return;
    208 
    209 	/*
    210 	 * Drop references to old lock.  A third reference (from the
    211 	 * queue head) must be held as we still hold its lock.  Bonus:
    212 	 * we don't need to worry about garbage collecting the lock.
    213 	 */
    214 	lock = so->so_lock;
    215 	KASSERT(lock == uipc_lock);
    216 	mutex_obj_free(lock);
    217 	mutex_obj_free(lock);
    218 
    219 	/*
    220 	 * Grab stream lock from the initiator and share between the two
    221 	 * endpoints.  Issue memory barrier to ensure all modifications
    222 	 * become globally visible before the lock change.  so2 is
    223 	 * assumed not to have a stream lock, because it was created
    224 	 * purely for the server side to accept this connection and
    225 	 * started out life using the domain-wide lock.
    226 	 */
    227 	unp = sotounpcb(so);
    228 	KASSERT(unp->unp_streamlock != NULL);
    229 	KASSERT(sotounpcb(so2)->unp_streamlock == NULL);
    230 	lock = unp->unp_streamlock;
    231 	unp->unp_streamlock = NULL;
    232 	mutex_obj_hold(lock);
    233 	membar_exit();
    234 	solockreset(so, lock);
    235 	solockreset(so2, lock);
    236 }
    237 
    238 /*
    239  * Reset a socket's lock back to the domain-wide lock.
    240  */
    241 static void
    242 unp_resetlock(struct socket *so)
    243 {
    244 	kmutex_t *olock, *nlock;
    245 	struct unpcb *unp;
    246 
    247 	KASSERT(solocked(so));
    248 
    249 	olock = so->so_lock;
    250 	nlock = uipc_lock;
    251 	if (olock == nlock)
    252 		return;
    253 	unp = sotounpcb(so);
    254 	KASSERT(unp->unp_streamlock == NULL);
    255 	unp->unp_streamlock = olock;
    256 	mutex_obj_hold(nlock);
    257 	mutex_enter(nlock);
    258 	solockreset(so, nlock);
    259 	mutex_exit(olock);
    260 }
    261 
    262 static void
    263 unp_free(struct unpcb *unp)
    264 {
    265 
    266 	if (unp->unp_addr)
    267 		free(unp->unp_addr, M_SONAME);
    268 	if (unp->unp_streamlock != NULL)
    269 		mutex_obj_free(unp->unp_streamlock);
    270 	free(unp, M_PCB);
    271 }
    272 
    273 int
    274 unp_output(struct mbuf *m, struct mbuf *control, struct unpcb *unp,
    275 	struct lwp *l)
    276 {
    277 	struct socket *so2;
    278 	const struct sockaddr_un *sun;
    279 
    280 	so2 = unp->unp_conn->unp_socket;
    281 
    282 	KASSERT(solocked(so2));
    283 
    284 	if (unp->unp_addr)
    285 		sun = unp->unp_addr;
    286 	else
    287 		sun = &sun_noname;
    288 	if (unp->unp_conn->unp_flags & UNP_WANTCRED)
    289 		control = unp_addsockcred(l, control);
    290 	if (sbappendaddr(&so2->so_rcv, (const struct sockaddr *)sun, m,
    291 	    control) == 0) {
    292 		so2->so_rcv.sb_overflowed++;
    293 	    	sounlock(so2);
    294 		unp_dispose(control);
    295 		m_freem(control);
    296 		m_freem(m);
    297 	    	solock(so2);
    298 		return (ENOBUFS);
    299 	} else {
    300 		sorwakeup(so2);
    301 		return (0);
    302 	}
    303 }
    304 
    305 void
    306 unp_setaddr(struct socket *so, struct mbuf *nam, bool peeraddr)
    307 {
    308 	const struct sockaddr_un *sun;
    309 	struct unpcb *unp;
    310 	bool ext;
    311 
    312 	unp = sotounpcb(so);
    313 	ext = false;
    314 
    315 	for (;;) {
    316 		sun = NULL;
    317 		if (peeraddr) {
    318 			if (unp->unp_conn && unp->unp_conn->unp_addr)
    319 				sun = unp->unp_conn->unp_addr;
    320 		} else {
    321 			if (unp->unp_addr)
    322 				sun = unp->unp_addr;
    323 		}
    324 		if (sun == NULL)
    325 			sun = &sun_noname;
    326 		nam->m_len = sun->sun_len;
    327 		if (nam->m_len > MLEN && !ext) {
    328 			sounlock(so);
    329 			MEXTMALLOC(nam, MAXPATHLEN * 2, M_WAITOK);
    330 			solock(so);
    331 			ext = true;
    332 		} else {
    333 			KASSERT(nam->m_len <= MAXPATHLEN * 2);
    334 			memcpy(mtod(nam, void *), sun, (size_t)nam->m_len);
    335 			break;
    336 		}
    337 	}
    338 }
    339 
    340 /*ARGSUSED*/
    341 int
    342 uipc_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
    343 	struct mbuf *control, struct lwp *l)
    344 {
    345 	struct unpcb *unp = sotounpcb(so);
    346 	struct socket *so2;
    347 	struct proc *p;
    348 	u_int newhiwat;
    349 	int error = 0;
    350 
    351 	if (req == PRU_CONTROL)
    352 		return (EOPNOTSUPP);
    353 
    354 #ifdef DIAGNOSTIC
    355 	if (req != PRU_SEND && req != PRU_SENDOOB && control)
    356 		panic("uipc_usrreq: unexpected control mbuf");
    357 #endif
    358 	p = l ? l->l_proc : NULL;
    359 	if (req != PRU_ATTACH) {
    360 		if (unp == 0) {
    361 			error = EINVAL;
    362 			goto release;
    363 		}
    364 		KASSERT(solocked(so));
    365 	}
    366 
    367 	switch (req) {
    368 
    369 	case PRU_ATTACH:
    370 		if (unp != 0) {
    371 			error = EISCONN;
    372 			break;
    373 		}
    374 		error = unp_attach(so);
    375 		break;
    376 
    377 	case PRU_DETACH:
    378 		unp_detach(unp);
    379 		break;
    380 
    381 	case PRU_BIND:
    382 		KASSERT(l != NULL);
    383 		error = unp_bind(so, nam, l);
    384 		break;
    385 
    386 	case PRU_LISTEN:
    387 		/*
    388 		 * If the socket can accept a connection, it must be
    389 		 * locked by uipc_lock.
    390 		 */
    391 		unp_resetlock(so);
    392 		if (unp->unp_vnode == 0)
    393 			error = EINVAL;
    394 		break;
    395 
    396 	case PRU_CONNECT:
    397 		KASSERT(l != NULL);
    398 		error = unp_connect(so, nam, l);
    399 		break;
    400 
    401 	case PRU_CONNECT2:
    402 		error = unp_connect2(so, (struct socket *)nam, PRU_CONNECT2);
    403 		break;
    404 
    405 	case PRU_DISCONNECT:
    406 		unp_disconnect(unp);
    407 		break;
    408 
    409 	case PRU_ACCEPT:
    410 		KASSERT(so->so_lock == uipc_lock);
    411 		/*
    412 		 * Mark the initiating STREAM socket as connected *ONLY*
    413 		 * after it's been accepted.  This prevents a client from
    414 		 * overrunning a server and receiving ECONNREFUSED.
    415 		 */
    416 		if (unp->unp_conn == NULL)
    417 			break;
    418 		so2 = unp->unp_conn->unp_socket;
    419 		if (so2->so_state & SS_ISCONNECTING) {
    420 			KASSERT(solocked2(so, so->so_head));
    421 			KASSERT(solocked2(so2, so->so_head));
    422 			soisconnected(so2);
    423 		}
    424 		/*
    425 		 * If the connection is fully established, break the
    426 		 * association with uipc_lock and give the connected
    427 		 * pair a seperate lock to share.
    428 		 */
    429 		unp_setpeerlocks(so2, so);
    430 		/*
    431 		 * Only now return peer's address, as we may need to
    432 		 * block in order to allocate memory.
    433 		 *
    434 		 * XXX Minor race: connection can be broken while
    435 		 * lock is dropped in unp_setaddr().  We will return
    436 		 * error == 0 and sun_noname as the peer address.
    437 		 */
    438 		unp_setaddr(so, nam, true);
    439 		break;
    440 
    441 	case PRU_SHUTDOWN:
    442 		socantsendmore(so);
    443 		unp_shutdown(unp);
    444 		break;
    445 
    446 	case PRU_RCVD:
    447 		switch (so->so_type) {
    448 
    449 		case SOCK_DGRAM:
    450 			panic("uipc 1");
    451 			/*NOTREACHED*/
    452 
    453 		case SOCK_STREAM:
    454 #define	rcv (&so->so_rcv)
    455 #define snd (&so2->so_snd)
    456 			if (unp->unp_conn == 0)
    457 				break;
    458 			so2 = unp->unp_conn->unp_socket;
    459 			KASSERT(solocked2(so, so2));
    460 			/*
    461 			 * Adjust backpressure on sender
    462 			 * and wakeup any waiting to write.
    463 			 */
    464 			snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt;
    465 			unp->unp_mbcnt = rcv->sb_mbcnt;
    466 			newhiwat = snd->sb_hiwat + unp->unp_cc - rcv->sb_cc;
    467 			(void)chgsbsize(so2->so_uidinfo,
    468 			    &snd->sb_hiwat, newhiwat, RLIM_INFINITY);
    469 			unp->unp_cc = rcv->sb_cc;
    470 			sowwakeup(so2);
    471 #undef snd
    472 #undef rcv
    473 			break;
    474 
    475 		default:
    476 			panic("uipc 2");
    477 		}
    478 		break;
    479 
    480 	case PRU_SEND:
    481 		/*
    482 		 * Note: unp_internalize() rejects any control message
    483 		 * other than SCM_RIGHTS, and only allows one.  This
    484 		 * has the side-effect of preventing a caller from
    485 		 * forging SCM_CREDS.
    486 		 */
    487 		if (control) {
    488 			sounlock(so);
    489 			error = unp_internalize(&control);
    490 			solock(so);
    491 			if (error != 0) {
    492 				m_freem(control);
    493 				m_freem(m);
    494 				break;
    495 			}
    496 		}
    497 		switch (so->so_type) {
    498 
    499 		case SOCK_DGRAM: {
    500 			KASSERT(so->so_lock == uipc_lock);
    501 			if (nam) {
    502 				if ((so->so_state & SS_ISCONNECTED) != 0)
    503 					error = EISCONN;
    504 				else {
    505 					/*
    506 					 * Note: once connected, the
    507 					 * socket's lock must not be
    508 					 * dropped until we have sent
    509 					 * the message and disconnected.
    510 					 * This is necessary to prevent
    511 					 * intervening control ops, like
    512 					 * another connection.
    513 					 */
    514 					error = unp_connect(so, nam, l);
    515 				}
    516 			} else {
    517 				if ((so->so_state & SS_ISCONNECTED) == 0)
    518 					error = ENOTCONN;
    519 			}
    520 			if (error) {
    521 				sounlock(so);
    522 				unp_dispose(control);
    523 				m_freem(control);
    524 				m_freem(m);
    525 				solock(so);
    526 				break;
    527 			}
    528 			KASSERT(p != NULL);
    529 			error = unp_output(m, control, unp, l);
    530 			if (nam)
    531 				unp_disconnect(unp);
    532 			break;
    533 		}
    534 
    535 		case SOCK_STREAM:
    536 #define	rcv (&so2->so_rcv)
    537 #define	snd (&so->so_snd)
    538 			if (unp->unp_conn == NULL) {
    539 				error = ENOTCONN;
    540 				break;
    541 			}
    542 			so2 = unp->unp_conn->unp_socket;
    543 			KASSERT(solocked2(so, so2));
    544 			if (unp->unp_conn->unp_flags & UNP_WANTCRED) {
    545 				/*
    546 				 * Credentials are passed only once on
    547 				 * SOCK_STREAM.
    548 				 */
    549 				unp->unp_conn->unp_flags &= ~UNP_WANTCRED;
    550 				control = unp_addsockcred(l, 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 				sbappend(rcv, m);
    562 			snd->sb_mbmax -=
    563 			    rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt;
    564 			unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt;
    565 			newhiwat = snd->sb_hiwat -
    566 			    (rcv->sb_cc - unp->unp_conn->unp_cc);
    567 			(void)chgsbsize(so->so_uidinfo,
    568 			    &snd->sb_hiwat, newhiwat, RLIM_INFINITY);
    569 			unp->unp_conn->unp_cc = rcv->sb_cc;
    570 			sorwakeup(so2);
    571 #undef snd
    572 #undef rcv
    573 			if (control != NULL) {
    574 				sounlock(so);
    575 				unp_dispose(control);
    576 				m_freem(control);
    577 				solock(so);
    578 			}
    579 			break;
    580 
    581 		default:
    582 			panic("uipc 4");
    583 		}
    584 		break;
    585 
    586 	case PRU_ABORT:
    587 		(void)unp_drop(unp, ECONNABORTED);
    588 
    589 		KASSERT(so->so_head == NULL);
    590 #ifdef DIAGNOSTIC
    591 		if (so->so_pcb == 0)
    592 			panic("uipc 5: drop killed pcb");
    593 #endif
    594 		unp_detach(unp);
    595 		break;
    596 
    597 	case PRU_SENSE:
    598 		((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
    599 		if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
    600 			so2 = unp->unp_conn->unp_socket;
    601 			KASSERT(solocked2(so, so2));
    602 			((struct stat *) m)->st_blksize += so2->so_rcv.sb_cc;
    603 		}
    604 		((struct stat *) m)->st_dev = NODEV;
    605 		if (unp->unp_ino == 0)
    606 			unp->unp_ino = unp_ino++;
    607 		((struct stat *) m)->st_atimespec =
    608 		    ((struct stat *) m)->st_mtimespec =
    609 		    ((struct stat *) m)->st_ctimespec = unp->unp_ctime;
    610 		((struct stat *) m)->st_ino = unp->unp_ino;
    611 		return (0);
    612 
    613 	case PRU_RCVOOB:
    614 		error = EOPNOTSUPP;
    615 		break;
    616 
    617 	case PRU_SENDOOB:
    618 		m_freem(control);
    619 		m_freem(m);
    620 		error = EOPNOTSUPP;
    621 		break;
    622 
    623 	case PRU_SOCKADDR:
    624 		unp_setaddr(so, nam, false);
    625 		break;
    626 
    627 	case PRU_PEERADDR:
    628 		unp_setaddr(so, nam, true);
    629 		break;
    630 
    631 	default:
    632 		panic("piusrreq");
    633 	}
    634 
    635 release:
    636 	return (error);
    637 }
    638 
    639 /*
    640  * Unix domain socket option processing.
    641  */
    642 int
    643 uipc_ctloutput(int op, struct socket *so, struct sockopt *sopt)
    644 {
    645 	struct unpcb *unp = sotounpcb(so);
    646 	int optval = 0, error = 0;
    647 
    648 	KASSERT(solocked(so));
    649 
    650 	if (sopt->sopt_level != 0) {
    651 		error = ENOPROTOOPT;
    652 	} else switch (op) {
    653 
    654 	case PRCO_SETOPT:
    655 		switch (sopt->sopt_name) {
    656 		case LOCAL_CREDS:
    657 		case LOCAL_CONNWAIT:
    658 			error = sockopt_getint(sopt, &optval);
    659 			if (error)
    660 				break;
    661 			switch (sopt->sopt_name) {
    662 #define	OPTSET(bit) \
    663 	if (optval) \
    664 		unp->unp_flags |= (bit); \
    665 	else \
    666 		unp->unp_flags &= ~(bit);
    667 
    668 			case LOCAL_CREDS:
    669 				OPTSET(UNP_WANTCRED);
    670 				break;
    671 			case LOCAL_CONNWAIT:
    672 				OPTSET(UNP_CONNWAIT);
    673 				break;
    674 			}
    675 			break;
    676 #undef OPTSET
    677 
    678 		default:
    679 			error = ENOPROTOOPT;
    680 			break;
    681 		}
    682 		break;
    683 
    684 	case PRCO_GETOPT:
    685 		sounlock(so);
    686 		switch (sopt->sopt_name) {
    687 		case LOCAL_PEEREID:
    688 			if (unp->unp_flags & UNP_EIDSVALID) {
    689 				error = sockopt_set(sopt,
    690 				    &unp->unp_connid, sizeof(unp->unp_connid));
    691 			} else {
    692 				error = EINVAL;
    693 			}
    694 			break;
    695 		case LOCAL_CREDS:
    696 #define	OPTBIT(bit)	(unp->unp_flags & (bit) ? 1 : 0)
    697 
    698 			optval = OPTBIT(UNP_WANTCRED);
    699 			error = sockopt_setint(sopt, optval);
    700 			break;
    701 #undef OPTBIT
    702 
    703 		default:
    704 			error = ENOPROTOOPT;
    705 			break;
    706 		}
    707 		solock(so);
    708 		break;
    709 	}
    710 	return (error);
    711 }
    712 
    713 /*
    714  * Both send and receive buffers are allocated PIPSIZ bytes of buffering
    715  * for stream sockets, although the total for sender and receiver is
    716  * actually only PIPSIZ.
    717  * Datagram sockets really use the sendspace as the maximum datagram size,
    718  * and don't really want to reserve the sendspace.  Their recvspace should
    719  * be large enough for at least one max-size datagram plus address.
    720  */
    721 #define	PIPSIZ	4096
    722 u_long	unpst_sendspace = PIPSIZ;
    723 u_long	unpst_recvspace = PIPSIZ;
    724 u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
    725 u_long	unpdg_recvspace = 4*1024;
    726 
    727 u_int	unp_rights;			/* file descriptors in flight */
    728 
    729 int
    730 unp_attach(struct socket *so)
    731 {
    732 	struct unpcb *unp;
    733 	int error;
    734 
    735 	switch (so->so_type) {
    736 	case SOCK_STREAM:
    737 		if (so->so_lock == NULL) {
    738 			/*
    739 			 * XXX Assuming that no socket locks are held,
    740 			 * as this call may sleep.
    741 			 */
    742 			so->so_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    743 			solock(so);
    744 		}
    745 		if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    746 			error = soreserve(so, unpst_sendspace, unpst_recvspace);
    747 			if (error != 0)
    748 				return (error);
    749 		}
    750 		break;
    751 
    752 	case SOCK_DGRAM:
    753 		if (so->so_lock == NULL) {
    754 			mutex_obj_hold(uipc_lock);
    755 			so->so_lock = uipc_lock;
    756 			solock(so);
    757 		}
    758 		if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    759 			error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
    760 			if (error != 0)
    761 				return (error);
    762 		}
    763 		break;
    764 
    765 	default:
    766 		panic("unp_attach");
    767 	}
    768 	KASSERT(solocked(so));
    769 	unp = malloc(sizeof(*unp), M_PCB, M_NOWAIT);
    770 	if (unp == NULL)
    771 		return (ENOBUFS);
    772 	memset((void *)unp, 0, sizeof(*unp));
    773 	unp->unp_socket = so;
    774 	so->so_pcb = unp;
    775 	nanotime(&unp->unp_ctime);
    776 	return (0);
    777 }
    778 
    779 void
    780 unp_detach(struct unpcb *unp)
    781 {
    782 	struct socket *so;
    783 	vnode_t *vp;
    784 
    785 	so = unp->unp_socket;
    786 
    787  retry:
    788 	if ((vp = unp->unp_vnode) != NULL) {
    789 		sounlock(so);
    790 		/* Acquire v_interlock to protect against unp_connect(). */
    791 		/* XXXAD racy */
    792 		mutex_enter(&vp->v_interlock);
    793 		vp->v_socket = NULL;
    794 		vrelel(vp, 0);
    795 		solock(so);
    796 		unp->unp_vnode = NULL;
    797 	}
    798 	if (unp->unp_conn)
    799 		unp_disconnect(unp);
    800 	while (unp->unp_refs) {
    801 		KASSERT(solocked2(so, unp->unp_refs->unp_socket));
    802 		if (unp_drop(unp->unp_refs, ECONNRESET)) {
    803 			solock(so);
    804 			goto retry;
    805 		}
    806 	}
    807 	soisdisconnected(so);
    808 	so->so_pcb = NULL;
    809 	if (unp_rights) {
    810 		/*
    811 		 * Normally the receive buffer is flushed later,
    812 		 * in sofree, but if our receive buffer holds references
    813 		 * to descriptors that are now garbage, we will dispose
    814 		 * of those descriptor references after the garbage collector
    815 		 * gets them (resulting in a "panic: closef: count < 0").
    816 		 */
    817 		sorflush(so);
    818 		unp_free(unp);
    819 		sounlock(so);
    820 		unp_gc();
    821 		solock(so);
    822 	} else
    823 		unp_free(unp);
    824 }
    825 
    826 int
    827 unp_bind(struct socket *so, struct mbuf *nam, struct lwp *l)
    828 {
    829 	struct sockaddr_un *sun;
    830 	struct unpcb *unp;
    831 	vnode_t *vp;
    832 	struct vattr vattr;
    833 	size_t addrlen;
    834 	int error;
    835 	struct nameidata nd;
    836 	proc_t *p;
    837 
    838 	unp = sotounpcb(so);
    839 	if (unp->unp_vnode != NULL)
    840 		return (EINVAL);
    841 	if ((unp->unp_flags & UNP_BUSY) != 0) {
    842 		/*
    843 		 * EALREADY may not be strictly accurate, but since this
    844 		 * is a major application error it's hardly a big deal.
    845 		 */
    846 		return (EALREADY);
    847 	}
    848 	unp->unp_flags |= UNP_BUSY;
    849 	sounlock(so);
    850 
    851 	/*
    852 	 * Allocate the new sockaddr.  We have to allocate one
    853 	 * extra byte so that we can ensure that the pathname
    854 	 * is nul-terminated.
    855 	 */
    856 	p = l->l_proc;
    857 	addrlen = nam->m_len + 1;
    858 	sun = malloc(addrlen, M_SONAME, M_WAITOK);
    859 	m_copydata(nam, 0, nam->m_len, (void *)sun);
    860 	*(((char *)sun) + nam->m_len) = '\0';
    861 
    862 	NDINIT(&nd, CREATE, FOLLOW | LOCKPARENT | TRYEMULROOT, UIO_SYSSPACE,
    863 	    sun->sun_path);
    864 
    865 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
    866 	if ((error = namei(&nd)) != 0)
    867 		goto bad;
    868 	vp = nd.ni_vp;
    869 	if (vp != NULL) {
    870 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    871 		if (nd.ni_dvp == vp)
    872 			vrele(nd.ni_dvp);
    873 		else
    874 			vput(nd.ni_dvp);
    875 		vrele(vp);
    876 		error = EADDRINUSE;
    877 		goto bad;
    878 	}
    879 	VATTR_NULL(&vattr);
    880 	vattr.va_type = VSOCK;
    881 	vattr.va_mode = ACCESSPERMS & ~(p->p_cwdi->cwdi_cmask);
    882 	error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
    883 	if (error)
    884 		goto bad;
    885 	vp = nd.ni_vp;
    886 	solock(so);
    887 	vp->v_socket = unp->unp_socket;
    888 	unp->unp_vnode = vp;
    889 	unp->unp_addrlen = addrlen;
    890 	unp->unp_addr = sun;
    891 	unp->unp_connid.unp_pid = p->p_pid;
    892 	unp->unp_connid.unp_euid = kauth_cred_geteuid(l->l_cred);
    893 	unp->unp_connid.unp_egid = kauth_cred_getegid(l->l_cred);
    894 	unp->unp_flags |= UNP_EIDSBIND;
    895 	VOP_UNLOCK(vp, 0);
    896 	unp->unp_flags &= ~UNP_BUSY;
    897 	return (0);
    898 
    899  bad:
    900 	free(sun, M_SONAME);
    901 	solock(so);
    902 	unp->unp_flags &= ~UNP_BUSY;
    903 	return (error);
    904 }
    905 
    906 int
    907 unp_connect(struct socket *so, struct mbuf *nam, struct lwp *l)
    908 {
    909 	struct sockaddr_un *sun;
    910 	vnode_t *vp;
    911 	struct socket *so2, *so3;
    912 	struct unpcb *unp, *unp2, *unp3;
    913 	size_t addrlen;
    914 	int error;
    915 	struct nameidata nd;
    916 
    917 	unp = sotounpcb(so);
    918 	if ((unp->unp_flags & UNP_BUSY) != 0) {
    919 		/*
    920 		 * EALREADY may not be strictly accurate, but since this
    921 		 * is a major application error it's hardly a big deal.
    922 		 */
    923 		return (EALREADY);
    924 	}
    925 	unp->unp_flags |= UNP_BUSY;
    926 	sounlock(so);
    927 
    928 	/*
    929 	 * Allocate a temporary sockaddr.  We have to allocate one extra
    930 	 * byte so that we can ensure that the pathname is nul-terminated.
    931 	 * When we establish the connection, we copy the other PCB's
    932 	 * sockaddr to our own.
    933 	 */
    934 	addrlen = nam->m_len + 1;
    935 	sun = malloc(addrlen, M_SONAME, M_WAITOK);
    936 	m_copydata(nam, 0, nam->m_len, (void *)sun);
    937 	*(((char *)sun) + nam->m_len) = '\0';
    938 
    939 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_SYSSPACE,
    940 	    sun->sun_path);
    941 
    942 	if ((error = namei(&nd)) != 0)
    943 		goto bad2;
    944 	vp = nd.ni_vp;
    945 	if (vp->v_type != VSOCK) {
    946 		error = ENOTSOCK;
    947 		goto bad;
    948 	}
    949 	if ((error = VOP_ACCESS(vp, VWRITE, l->l_cred)) != 0)
    950 		goto bad;
    951 	/* Acquire v_interlock to protect against unp_detach(). */
    952 	mutex_enter(&vp->v_interlock);
    953 	so2 = vp->v_socket;
    954 	if (so2 == NULL) {
    955 		mutex_exit(&vp->v_interlock);
    956 		error = ECONNREFUSED;
    957 		goto bad;
    958 	}
    959 	if (so->so_type != so2->so_type) {
    960 		mutex_exit(&vp->v_interlock);
    961 		error = EPROTOTYPE;
    962 		goto bad;
    963 	}
    964 	solock(so);
    965 	unp_resetlock(so);
    966 	mutex_exit(&vp->v_interlock);
    967 	if ((so->so_proto->pr_flags & PR_CONNREQUIRED) != 0) {
    968 		/*
    969 		 * This may seem somewhat fragile but is OK: if we can
    970 		 * see SO_ACCEPTCONN set on the endpoint, then it must
    971 		 * be locked by the domain-wide uipc_lock.
    972 		 */
    973 		KASSERT((so->so_options & SO_ACCEPTCONN) == 0 ||
    974 		    so2->so_lock == uipc_lock);
    975 		if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
    976 		    (so3 = sonewconn(so2, 0)) == 0) {
    977 			error = ECONNREFUSED;
    978 			sounlock(so);
    979 			goto bad;
    980 		}
    981 		unp2 = sotounpcb(so2);
    982 		unp3 = sotounpcb(so3);
    983 		if (unp2->unp_addr) {
    984 			unp3->unp_addr = malloc(unp2->unp_addrlen,
    985 			    M_SONAME, M_WAITOK);
    986 			memcpy(unp3->unp_addr, unp2->unp_addr,
    987 			    unp2->unp_addrlen);
    988 			unp3->unp_addrlen = unp2->unp_addrlen;
    989 		}
    990 		unp3->unp_flags = unp2->unp_flags;
    991 		unp3->unp_connid.unp_pid = l->l_proc->p_pid;
    992 		unp3->unp_connid.unp_euid = kauth_cred_geteuid(l->l_cred);
    993 		unp3->unp_connid.unp_egid = kauth_cred_getegid(l->l_cred);
    994 		unp3->unp_flags |= UNP_EIDSVALID;
    995 		if (unp2->unp_flags & UNP_EIDSBIND) {
    996 			unp->unp_connid = unp2->unp_connid;
    997 			unp->unp_flags |= UNP_EIDSVALID;
    998 		}
    999 		so2 = so3;
   1000 	}
   1001 	error = unp_connect2(so, so2, PRU_CONNECT);
   1002 	sounlock(so);
   1003  bad:
   1004 	vput(vp);
   1005  bad2:
   1006 	free(sun, M_SONAME);
   1007 	solock(so);
   1008 	unp->unp_flags &= ~UNP_BUSY;
   1009 	return (error);
   1010 }
   1011 
   1012 int
   1013 unp_connect2(struct socket *so, struct socket *so2, int req)
   1014 {
   1015 	struct unpcb *unp = sotounpcb(so);
   1016 	struct unpcb *unp2;
   1017 
   1018 	if (so2->so_type != so->so_type)
   1019 		return (EPROTOTYPE);
   1020 
   1021 	/*
   1022 	 * All three sockets involved must be locked by same lock:
   1023 	 *
   1024 	 * local endpoint (so)
   1025 	 * remote endpoint (so2)
   1026 	 * queue head (so->so_head, only if PR_CONNREQUIRED)
   1027 	 */
   1028 	KASSERT(solocked2(so, so2));
   1029 	if (so->so_head != NULL) {
   1030 		KASSERT(so->so_lock == uipc_lock);
   1031 		KASSERT(solocked2(so, so->so_head));
   1032 	}
   1033 
   1034 	unp2 = sotounpcb(so2);
   1035 	unp->unp_conn = unp2;
   1036 	switch (so->so_type) {
   1037 
   1038 	case SOCK_DGRAM:
   1039 		unp->unp_nextref = unp2->unp_refs;
   1040 		unp2->unp_refs = unp;
   1041 		soisconnected(so);
   1042 		break;
   1043 
   1044 	case SOCK_STREAM:
   1045 		unp2->unp_conn = unp;
   1046 		if (req == PRU_CONNECT &&
   1047 		    ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT))
   1048 			soisconnecting(so);
   1049 		else
   1050 			soisconnected(so);
   1051 		soisconnected(so2);
   1052 		/*
   1053 		 * If the connection is fully established, break the
   1054 		 * association with uipc_lock and give the connected
   1055 		 * pair a seperate lock to share.  For CONNECT2, we
   1056 		 * require that the locks already match (the sockets
   1057 		 * are created that way).
   1058 		 */
   1059 		if (req == PRU_CONNECT)
   1060 			unp_setpeerlocks(so, so2);
   1061 		break;
   1062 
   1063 	default:
   1064 		panic("unp_connect2");
   1065 	}
   1066 	return (0);
   1067 }
   1068 
   1069 void
   1070 unp_disconnect(struct unpcb *unp)
   1071 {
   1072 	struct unpcb *unp2 = unp->unp_conn;
   1073 	struct socket *so;
   1074 
   1075 	if (unp2 == 0)
   1076 		return;
   1077 	unp->unp_conn = 0;
   1078 	so = unp->unp_socket;
   1079 	switch (so->so_type) {
   1080 	case SOCK_DGRAM:
   1081 		if (unp2->unp_refs == unp)
   1082 			unp2->unp_refs = unp->unp_nextref;
   1083 		else {
   1084 			unp2 = unp2->unp_refs;
   1085 			for (;;) {
   1086 				KASSERT(solocked2(so, unp2->unp_socket));
   1087 				if (unp2 == 0)
   1088 					panic("unp_disconnect");
   1089 				if (unp2->unp_nextref == unp)
   1090 					break;
   1091 				unp2 = unp2->unp_nextref;
   1092 			}
   1093 			unp2->unp_nextref = unp->unp_nextref;
   1094 		}
   1095 		unp->unp_nextref = 0;
   1096 		so->so_state &= ~SS_ISCONNECTED;
   1097 		break;
   1098 
   1099 	case SOCK_STREAM:
   1100 		KASSERT(solocked2(so, unp2->unp_socket));
   1101 		soisdisconnected(so);
   1102 		unp2->unp_conn = 0;
   1103 		soisdisconnected(unp2->unp_socket);
   1104 		break;
   1105 	}
   1106 }
   1107 
   1108 #ifdef notdef
   1109 unp_abort(struct unpcb *unp)
   1110 {
   1111 	unp_detach(unp);
   1112 }
   1113 #endif
   1114 
   1115 void
   1116 unp_shutdown(struct unpcb *unp)
   1117 {
   1118 	struct socket *so;
   1119 
   1120 	if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
   1121 	    (so = unp->unp_conn->unp_socket))
   1122 		socantrcvmore(so);
   1123 }
   1124 
   1125 bool
   1126 unp_drop(struct unpcb *unp, int errno)
   1127 {
   1128 	struct socket *so = unp->unp_socket;
   1129 
   1130 	KASSERT(solocked(so));
   1131 
   1132 	so->so_error = errno;
   1133 	unp_disconnect(unp);
   1134 	if (so->so_head) {
   1135 		so->so_pcb = NULL;
   1136 		/* sofree() drops the socket lock */
   1137 		sofree(so);
   1138 		unp_free(unp);
   1139 		return true;
   1140 	}
   1141 	return false;
   1142 }
   1143 
   1144 #ifdef notdef
   1145 unp_drain(void)
   1146 {
   1147 
   1148 }
   1149 #endif
   1150 
   1151 int
   1152 unp_externalize(struct mbuf *rights, struct lwp *l)
   1153 {
   1154 	struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
   1155 	struct proc *p = l->l_proc;
   1156 	int i, *fdp;
   1157 	file_t **rp;
   1158 	file_t *fp;
   1159 	int nfds, error = 0;
   1160 
   1161 	nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) /
   1162 	    sizeof(file_t *);
   1163 	rp = (file_t **)CMSG_DATA(cm);
   1164 
   1165 	fdp = malloc(nfds * sizeof(int), M_TEMP, M_WAITOK);
   1166 	rw_enter(&p->p_cwdi->cwdi_lock, RW_READER);
   1167 
   1168 	/* Make sure the recipient should be able to see the descriptors.. */
   1169 	if (p->p_cwdi->cwdi_rdir != NULL) {
   1170 		rp = (file_t **)CMSG_DATA(cm);
   1171 		for (i = 0; i < nfds; i++) {
   1172 			fp = *rp++;
   1173 			/*
   1174 			 * If we are in a chroot'ed directory, and
   1175 			 * someone wants to pass us a directory, make
   1176 			 * sure it's inside the subtree we're allowed
   1177 			 * to access.
   1178 			 */
   1179 			if (fp->f_type == DTYPE_VNODE) {
   1180 				vnode_t *vp = (vnode_t *)fp->f_data;
   1181 				if ((vp->v_type == VDIR) &&
   1182 				    !vn_isunder(vp, p->p_cwdi->cwdi_rdir, l)) {
   1183 					error = EPERM;
   1184 					break;
   1185 				}
   1186 			}
   1187 		}
   1188 	}
   1189 
   1190  restart:
   1191 	rp = (file_t **)CMSG_DATA(cm);
   1192 	if (error != 0) {
   1193 		for (i = 0; i < nfds; i++) {
   1194 			fp = *rp;
   1195 			/*
   1196 			 * zero the pointer before calling unp_discard,
   1197 			 * since it may end up in unp_gc()..
   1198 			 */
   1199 			*rp++ = 0;
   1200 			unp_discard(fp);
   1201 		}
   1202 		goto out;
   1203 	}
   1204 
   1205 	/*
   1206 	 * First loop -- allocate file descriptor table slots for the
   1207 	 * new descriptors.
   1208 	 */
   1209 	for (i = 0; i < nfds; i++) {
   1210 		fp = *rp++;
   1211 		if ((error = fd_alloc(p, 0, &fdp[i])) != 0) {
   1212 			/*
   1213 			 * Back out what we've done so far.
   1214 			 */
   1215 			for (--i; i >= 0; i--) {
   1216 				fd_abort(p, NULL, fdp[i]);
   1217 			}
   1218 			if (error == ENOSPC) {
   1219 				fd_tryexpand(p);
   1220 				error = 0;
   1221 			} else {
   1222 				/*
   1223 				 * This is the error that has historically
   1224 				 * been returned, and some callers may
   1225 				 * expect it.
   1226 				 */
   1227 				error = EMSGSIZE;
   1228 			}
   1229 			goto restart;
   1230 		}
   1231 	}
   1232 
   1233 	/*
   1234 	 * Now that adding them has succeeded, update all of the
   1235 	 * descriptor passing state.
   1236 	 */
   1237 	rp = (file_t **)CMSG_DATA(cm);
   1238 	for (i = 0; i < nfds; i++) {
   1239 		fp = *rp++;
   1240 		atomic_dec_uint(&unp_rights);
   1241 		fd_affix(p, fp, fdp[i]);
   1242 		mutex_enter(&fp->f_lock);
   1243 		fp->f_msgcount--;
   1244 		mutex_exit(&fp->f_lock);
   1245 		/*
   1246 		 * Note that fd_affix() adds a reference to the file.
   1247 		 * The file may already have been closed by another
   1248 		 * LWP in the process, so we must drop the reference
   1249 		 * added by unp_internalize() with closef().
   1250 		 */
   1251 		closef(fp);
   1252 	}
   1253 
   1254 	/*
   1255 	 * Copy temporary array to message and adjust length, in case of
   1256 	 * transition from large file_t pointers to ints.
   1257 	 */
   1258 	memcpy(CMSG_DATA(cm), fdp, nfds * sizeof(int));
   1259 	cm->cmsg_len = CMSG_LEN(nfds * sizeof(int));
   1260 	rights->m_len = CMSG_SPACE(nfds * sizeof(int));
   1261  out:
   1262 	rw_exit(&p->p_cwdi->cwdi_lock);
   1263 	free(fdp, M_TEMP);
   1264 	return (error);
   1265 }
   1266 
   1267 int
   1268 unp_internalize(struct mbuf **controlp)
   1269 {
   1270 	struct filedesc *fdescp = curlwp->l_fd;
   1271 	struct mbuf *control = *controlp;
   1272 	struct cmsghdr *newcm, *cm = mtod(control, struct cmsghdr *);
   1273 	file_t **rp, **files;
   1274 	file_t *fp;
   1275 	int i, fd, *fdp;
   1276 	int nfds, error;
   1277 
   1278 	error = 0;
   1279 	newcm = NULL;
   1280 
   1281 	/* Sanity check the control message header. */
   1282 	if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
   1283 	    cm->cmsg_len > control->m_len ||
   1284 	    cm->cmsg_len < CMSG_ALIGN(sizeof(*cm)))
   1285 		return (EINVAL);
   1286 
   1287 	/*
   1288 	 * Verify that the file descriptors are valid, and acquire
   1289 	 * a reference to each.
   1290 	 */
   1291 	nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) / sizeof(int);
   1292 	fdp = (int *)CMSG_DATA(cm);
   1293 	for (i = 0; i < nfds; i++) {
   1294 		fd = *fdp++;
   1295 		if ((fp = fd_getfile(fd)) == NULL) {
   1296 			nfds = i + 1;
   1297 			error = EBADF;
   1298 			goto out;
   1299 		}
   1300 	}
   1301 
   1302 	/* Allocate new space and copy header into it. */
   1303 	newcm = malloc(CMSG_SPACE(nfds * sizeof(file_t *)), M_MBUF, M_WAITOK);
   1304 	if (newcm == NULL) {
   1305 		error = E2BIG;
   1306 		goto out;
   1307 	}
   1308 	memcpy(newcm, cm, sizeof(struct cmsghdr));
   1309 	files = (file_t **)CMSG_DATA(newcm);
   1310 
   1311 	/*
   1312 	 * Transform the file descriptors into file_t pointers, in
   1313 	 * reverse order so that if pointers are bigger than ints, the
   1314 	 * int won't get until we're done.  No need to lock, as we have
   1315 	 * already validated the descriptors with fd_getfile().
   1316 	 */
   1317 	fdp = (int *)CMSG_DATA(cm) + nfds;
   1318 	rp = files + nfds;
   1319 	for (i = 0; i < nfds; i++) {
   1320 		fp = fdescp->fd_ofiles[*--fdp]->ff_file;
   1321 		KASSERT(fp != NULL);
   1322 		mutex_enter(&fp->f_lock);
   1323 		*--rp = fp;
   1324 		fp->f_count++;
   1325 		fp->f_msgcount++;
   1326 		mutex_exit(&fp->f_lock);
   1327 		atomic_inc_uint(&unp_rights);
   1328 	}
   1329 
   1330  out:
   1331  	/* Release descriptor references. */
   1332 	fdp = (int *)CMSG_DATA(cm);
   1333 	for (i = 0; i < nfds; i++) {
   1334 		fd_putfile(*fdp++);
   1335 	}
   1336 
   1337 	if (error == 0) {
   1338 		if (control->m_flags & M_EXT) {
   1339 			m_freem(control);
   1340 			*controlp = control = m_get(M_WAIT, MT_CONTROL);
   1341 		}
   1342 		MEXTADD(control, newcm, CMSG_SPACE(nfds * sizeof(file_t *)),
   1343 		    M_MBUF, NULL, NULL);
   1344 		cm = newcm;
   1345 		/*
   1346 		 * Adjust message & mbuf to note amount of space
   1347 		 * actually used.
   1348 		 */
   1349 		cm->cmsg_len = CMSG_LEN(nfds * sizeof(file_t *));
   1350 		control->m_len = CMSG_SPACE(nfds * sizeof(file_t *));
   1351 	}
   1352 
   1353 	return error;
   1354 }
   1355 
   1356 struct mbuf *
   1357 unp_addsockcred(struct lwp *l, struct mbuf *control)
   1358 {
   1359 	struct cmsghdr *cmp;
   1360 	struct sockcred *sc;
   1361 	struct mbuf *m, *n;
   1362 	int len, space, i;
   1363 
   1364 	len = CMSG_LEN(SOCKCREDSIZE(kauth_cred_ngroups(l->l_cred)));
   1365 	space = CMSG_SPACE(SOCKCREDSIZE(kauth_cred_ngroups(l->l_cred)));
   1366 
   1367 	m = m_get(M_WAIT, MT_CONTROL);
   1368 	if (space > MLEN) {
   1369 		if (space > MCLBYTES)
   1370 			MEXTMALLOC(m, space, M_WAITOK);
   1371 		else
   1372 			m_clget(m, M_WAIT);
   1373 		if ((m->m_flags & M_EXT) == 0) {
   1374 			m_free(m);
   1375 			return (control);
   1376 		}
   1377 	}
   1378 
   1379 	m->m_len = space;
   1380 	m->m_next = NULL;
   1381 	cmp = mtod(m, struct cmsghdr *);
   1382 	sc = (struct sockcred *)CMSG_DATA(cmp);
   1383 	cmp->cmsg_len = len;
   1384 	cmp->cmsg_level = SOL_SOCKET;
   1385 	cmp->cmsg_type = SCM_CREDS;
   1386 	sc->sc_uid = kauth_cred_getuid(l->l_cred);
   1387 	sc->sc_euid = kauth_cred_geteuid(l->l_cred);
   1388 	sc->sc_gid = kauth_cred_getgid(l->l_cred);
   1389 	sc->sc_egid = kauth_cred_getegid(l->l_cred);
   1390 	sc->sc_ngroups = kauth_cred_ngroups(l->l_cred);
   1391 	for (i = 0; i < sc->sc_ngroups; i++)
   1392 		sc->sc_groups[i] = kauth_cred_group(l->l_cred, i);
   1393 
   1394 	/*
   1395 	 * If a control message already exists, append us to the end.
   1396 	 */
   1397 	if (control != NULL) {
   1398 		for (n = control; n->m_next != NULL; n = n->m_next)
   1399 			;
   1400 		n->m_next = m;
   1401 	} else
   1402 		control = m;
   1403 
   1404 	return (control);
   1405 }
   1406 
   1407 int	unp_defer, unp_gcing;
   1408 extern	struct domain unixdomain;
   1409 
   1410 /*
   1411  * Comment added long after the fact explaining what's going on here.
   1412  * Do a mark-sweep GC of file descriptors on the system, to free up
   1413  * any which are caught in flight to an about-to-be-closed socket.
   1414  *
   1415  * Traditional mark-sweep gc's start at the "root", and mark
   1416  * everything reachable from the root (which, in our case would be the
   1417  * process table).  The mark bits are cleared during the sweep.
   1418  *
   1419  * XXX For some inexplicable reason (perhaps because the file
   1420  * descriptor tables used to live in the u area which could be swapped
   1421  * out and thus hard to reach), we do multiple scans over the set of
   1422  * descriptors, using use *two* mark bits per object (DEFER and MARK).
   1423  * Whenever we find a descriptor which references other descriptors,
   1424  * the ones it references are marked with both bits, and we iterate
   1425  * over the whole file table until there are no more DEFER bits set.
   1426  * We also make an extra pass *before* the GC to clear the mark bits,
   1427  * which could have been cleared at almost no cost during the previous
   1428  * sweep.
   1429  */
   1430 void
   1431 unp_gc(void)
   1432 {
   1433 	file_t *fp, *nextfp;
   1434 	struct socket *so, *so1;
   1435 	file_t **extra_ref, **fpp;
   1436 	int nunref, nslots, i;
   1437 
   1438 	if (atomic_swap_uint(&unp_gcing, 1) == 1)
   1439 		return;
   1440 
   1441  restart:
   1442  	nslots = nfiles * 2;
   1443  	extra_ref = kmem_alloc(nslots * sizeof(file_t *), KM_SLEEP);
   1444 
   1445 	mutex_enter(&filelist_lock);
   1446 	unp_defer = 0;
   1447 
   1448 	/* Clear mark bits */
   1449 	LIST_FOREACH(fp, &filehead, f_list) {
   1450 		atomic_and_uint(&fp->f_flag, ~(FMARK|FDEFER));
   1451 	}
   1452 
   1453 	/*
   1454 	 * Iterate over the set of descriptors, marking ones believed
   1455 	 * (based on refcount) to be referenced from a process, and
   1456 	 * marking for rescan descriptors which are queued on a socket.
   1457 	 */
   1458 	do {
   1459 		LIST_FOREACH(fp, &filehead, f_list) {
   1460 			mutex_enter(&fp->f_lock);
   1461 			if (fp->f_flag & FDEFER) {
   1462 				atomic_and_uint(&fp->f_flag, ~FDEFER);
   1463 				unp_defer--;
   1464 				KASSERT(fp->f_count != 0);
   1465 			} else {
   1466 				if (fp->f_count == 0 ||
   1467 				    (fp->f_flag & FMARK) ||
   1468 				    fp->f_count == fp->f_msgcount) {
   1469 					mutex_exit(&fp->f_lock);
   1470 					continue;
   1471 				}
   1472 			}
   1473 			atomic_or_uint(&fp->f_flag, FMARK);
   1474 
   1475 			if (fp->f_type != DTYPE_SOCKET ||
   1476 			    (so = fp->f_data) == NULL ||
   1477 			    so->so_proto->pr_domain != &unixdomain ||
   1478 			    (so->so_proto->pr_flags&PR_RIGHTS) == 0) {
   1479 				mutex_exit(&fp->f_lock);
   1480 				continue;
   1481 			}
   1482 #ifdef notdef
   1483 			if (so->so_rcv.sb_flags & SB_LOCK) {
   1484 				mutex_exit(&fp->f_lock);
   1485 				mutex_exit(&filelist_lock);
   1486 				kmem_free(extra_ref, nslots * sizeof(file_t *));
   1487 				/*
   1488 				 * This is problematical; it's not clear
   1489 				 * we need to wait for the sockbuf to be
   1490 				 * unlocked (on a uniprocessor, at least),
   1491 				 * and it's also not clear what to do
   1492 				 * if sbwait returns an error due to receipt
   1493 				 * of a signal.  If sbwait does return
   1494 				 * an error, we'll go into an infinite
   1495 				 * loop.  Delete all of this for now.
   1496 				 */
   1497 				(void) sbwait(&so->so_rcv);
   1498 				goto restart;
   1499 			}
   1500 #endif
   1501 			mutex_exit(&fp->f_lock);
   1502 
   1503 			/*
   1504 			 * XXX Locking a socket with filelist_lock held
   1505 			 * is ugly.  filelist_lock can be taken by the
   1506 			 * pagedaemon when reclaiming items from file_cache.
   1507 			 * Socket activity could delay the pagedaemon.
   1508 			 */
   1509 			solock(so);
   1510 			unp_scan(so->so_rcv.sb_mb, unp_mark, 0);
   1511 			/*
   1512 			 * Mark descriptors referenced from sockets queued
   1513 			 * on the accept queue as well.
   1514 			 */
   1515 			if (so->so_options & SO_ACCEPTCONN) {
   1516 				TAILQ_FOREACH(so1, &so->so_q0, so_qe) {
   1517 					unp_scan(so1->so_rcv.sb_mb, unp_mark, 0);
   1518 				}
   1519 				TAILQ_FOREACH(so1, &so->so_q, so_qe) {
   1520 					unp_scan(so1->so_rcv.sb_mb, unp_mark, 0);
   1521 				}
   1522 			}
   1523 			sounlock(so);
   1524 		}
   1525 	} while (unp_defer);
   1526 
   1527 	/*
   1528 	 * Sweep pass.  Find unmarked descriptors, and free them.
   1529 	 *
   1530 	 * We grab an extra reference to each of the file table entries
   1531 	 * that are not otherwise accessible and then free the rights
   1532 	 * that are stored in messages on them.
   1533 	 *
   1534 	 * The bug in the original code is a little tricky, so I'll describe
   1535 	 * what's wrong with it here.
   1536 	 *
   1537 	 * It is incorrect to simply unp_discard each entry for f_msgcount
   1538 	 * times -- consider the case of sockets A and B that contain
   1539 	 * references to each other.  On a last close of some other socket,
   1540 	 * we trigger a gc since the number of outstanding rights (unp_rights)
   1541 	 * is non-zero.  If during the sweep phase the gc code un_discards,
   1542 	 * we end up doing a (full) closef on the descriptor.  A closef on A
   1543 	 * results in the following chain.  Closef calls soo_close, which
   1544 	 * calls soclose.   Soclose calls first (through the switch
   1545 	 * uipc_usrreq) unp_detach, which re-invokes unp_gc.  Unp_gc simply
   1546 	 * returns because the previous instance had set unp_gcing, and
   1547 	 * we return all the way back to soclose, which marks the socket
   1548 	 * with SS_NOFDREF, and then calls sofree.  Sofree calls sorflush
   1549 	 * to free up the rights that are queued in messages on the socket A,
   1550 	 * i.e., the reference on B.  The sorflush calls via the dom_dispose
   1551 	 * switch unp_dispose, which unp_scans with unp_discard.  This second
   1552 	 * instance of unp_discard just calls closef on B.
   1553 	 *
   1554 	 * Well, a similar chain occurs on B, resulting in a sorflush on B,
   1555 	 * which results in another closef on A.  Unfortunately, A is already
   1556 	 * being closed, and the descriptor has already been marked with
   1557 	 * SS_NOFDREF, and soclose panics at this point.
   1558 	 *
   1559 	 * Here, we first take an extra reference to each inaccessible
   1560 	 * descriptor.  Then, if the inaccessible descriptor is a
   1561 	 * socket, we call sorflush in case it is a Unix domain
   1562 	 * socket.  After we destroy all the rights carried in
   1563 	 * messages, we do a last closef to get rid of our extra
   1564 	 * reference.  This is the last close, and the unp_detach etc
   1565 	 * will shut down the socket.
   1566 	 *
   1567 	 * 91/09/19, bsy (at) cs.cmu.edu
   1568 	 */
   1569 	if (nslots < nfiles) {
   1570 		mutex_exit(&filelist_lock);
   1571 		kmem_free(extra_ref, nslots * sizeof(file_t *));
   1572 		goto restart;
   1573 	}
   1574 	for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; fp != 0;
   1575 	    fp = nextfp) {
   1576 		nextfp = LIST_NEXT(fp, f_list);
   1577 		mutex_enter(&fp->f_lock);
   1578 		if (fp->f_count != 0 &&
   1579 		    fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) {
   1580 			*fpp++ = fp;
   1581 			nunref++;
   1582 			fp->f_count++;
   1583 		}
   1584 		mutex_exit(&fp->f_lock);
   1585 	}
   1586 	mutex_exit(&filelist_lock);
   1587 
   1588 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
   1589 		fp = *fpp;
   1590 		if (fp->f_type == DTYPE_SOCKET) {
   1591 			so = fp->f_data;
   1592 			solock(so);
   1593 			sorflush(fp->f_data);
   1594 			sounlock(so);
   1595 		}
   1596 	}
   1597 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
   1598 		closef(*fpp);
   1599 	}
   1600 	kmem_free(extra_ref, nslots * sizeof(file_t *));
   1601 	atomic_swap_uint(&unp_gcing, 0);
   1602 }
   1603 
   1604 void
   1605 unp_dispose(struct mbuf *m)
   1606 {
   1607 
   1608 	if (m)
   1609 		unp_scan(m, unp_discard, 1);
   1610 }
   1611 
   1612 void
   1613 unp_scan(struct mbuf *m0, void (*op)(file_t *), int discard)
   1614 {
   1615 	struct mbuf *m;
   1616 	file_t **rp;
   1617 	struct cmsghdr *cm;
   1618 	int i;
   1619 	int qfds;
   1620 
   1621 	while (m0) {
   1622 		for (m = m0; m; m = m->m_next) {
   1623 			if (m->m_type == MT_CONTROL &&
   1624 			    m->m_len >= sizeof(*cm)) {
   1625 				cm = mtod(m, struct cmsghdr *);
   1626 				if (cm->cmsg_level != SOL_SOCKET ||
   1627 				    cm->cmsg_type != SCM_RIGHTS)
   1628 					continue;
   1629 				qfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm)))
   1630 				    / sizeof(file_t *);
   1631 				rp = (file_t **)CMSG_DATA(cm);
   1632 				for (i = 0; i < qfds; i++) {
   1633 					file_t *fp = *rp;
   1634 					if (discard)
   1635 						*rp = 0;
   1636 					(*op)(fp);
   1637 					rp++;
   1638 				}
   1639 				break;		/* XXX, but saves time */
   1640 			}
   1641 		}
   1642 		m0 = m0->m_nextpkt;
   1643 	}
   1644 }
   1645 
   1646 void
   1647 unp_mark(file_t *fp)
   1648 {
   1649 
   1650 	if (fp == NULL)
   1651 		return;
   1652 
   1653 	/* If we're already deferred, don't screw up the defer count */
   1654 	mutex_enter(&fp->f_lock);
   1655 	if (fp->f_flag & (FMARK | FDEFER)) {
   1656 		mutex_exit(&fp->f_lock);
   1657 		return;
   1658 	}
   1659 
   1660 	/*
   1661 	 * Minimize the number of deferrals...  Sockets are the only
   1662 	 * type of descriptor which can hold references to another
   1663 	 * descriptor, so just mark other descriptors, and defer
   1664 	 * unmarked sockets for the next pass.
   1665 	 */
   1666 	if (fp->f_type == DTYPE_SOCKET) {
   1667 		unp_defer++;
   1668 		KASSERT(fp->f_count != 0);
   1669 		atomic_or_uint(&fp->f_flag, FDEFER);
   1670 	} else {
   1671 		atomic_or_uint(&fp->f_flag, FMARK);
   1672 	}
   1673 	mutex_exit(&fp->f_lock);
   1674 	return;
   1675 }
   1676 
   1677 void
   1678 unp_discard(file_t *fp)
   1679 {
   1680 
   1681 	if (fp == NULL)
   1682 		return;
   1683 
   1684 	mutex_enter(&fp->f_lock);
   1685 	KASSERT(fp->f_count > 0);
   1686 	fp->f_msgcount--;
   1687 	mutex_exit(&fp->f_lock);
   1688 	atomic_dec_uint(&unp_rights);
   1689 	(void)closef(fp);
   1690 }
   1691