Home | History | Annotate | Line # | Download | only in kern
uipc_usrreq.c revision 1.106
      1 /*	$NetBSD: uipc_usrreq.c,v 1.106 2008/03/21 21:55:00 ad 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  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1982, 1986, 1989, 1991, 1993
     42  *	The Regents of the University of California.  All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. Neither the name of the University nor the names of its contributors
     53  *    may be used to endorse or promote products derived from this software
     54  *    without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     66  * SUCH DAMAGE.
     67  *
     68  *	@(#)uipc_usrreq.c	8.9 (Berkeley) 5/14/95
     69  */
     70 
     71 /*
     72  * Copyright (c) 1997 Christopher G. Demetriou.  All rights reserved.
     73  *
     74  * Redistribution and use in source and binary forms, with or without
     75  * modification, are permitted provided that the following conditions
     76  * are met:
     77  * 1. Redistributions of source code must retain the above copyright
     78  *    notice, this list of conditions and the following disclaimer.
     79  * 2. Redistributions in binary form must reproduce the above copyright
     80  *    notice, this list of conditions and the following disclaimer in the
     81  *    documentation and/or other materials provided with the distribution.
     82  * 3. All advertising materials mentioning features or use of this software
     83  *    must display the following acknowledgement:
     84  *	This product includes software developed by the University of
     85  *	California, Berkeley and its contributors.
     86  * 4. Neither the name of the University nor the names of its contributors
     87  *    may be used to endorse or promote products derived from this software
     88  *    without specific prior written permission.
     89  *
     90  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     91  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     92  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     93  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     94  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     95  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     96  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     97  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     98  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     99  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    100  * SUCH DAMAGE.
    101  *
    102  *	@(#)uipc_usrreq.c	8.9 (Berkeley) 5/14/95
    103  */
    104 
    105 #include <sys/cdefs.h>
    106 __KERNEL_RCSID(0, "$NetBSD: uipc_usrreq.c,v 1.106 2008/03/21 21:55:00 ad Exp $");
    107 
    108 #include <sys/param.h>
    109 #include <sys/systm.h>
    110 #include <sys/proc.h>
    111 #include <sys/filedesc.h>
    112 #include <sys/domain.h>
    113 #include <sys/protosw.h>
    114 #include <sys/socket.h>
    115 #include <sys/socketvar.h>
    116 #include <sys/unpcb.h>
    117 #include <sys/un.h>
    118 #include <sys/namei.h>
    119 #include <sys/vnode.h>
    120 #include <sys/file.h>
    121 #include <sys/stat.h>
    122 #include <sys/mbuf.h>
    123 #include <sys/kauth.h>
    124 #include <sys/kmem.h>
    125 #include <sys/atomic.h>
    126 
    127 /*
    128  * Unix communications domain.
    129  *
    130  * TODO:
    131  *	SEQPACKET, RDM
    132  *	rethink name space problems
    133  *	need a proper out-of-band
    134  */
    135 const struct sockaddr_un sun_noname = {
    136 	.sun_len = sizeof(sun_noname),
    137 	.sun_family = AF_LOCAL,
    138 };
    139 ino_t	unp_ino;			/* prototype for fake inode numbers */
    140 
    141 struct mbuf *unp_addsockcred(struct lwp *, struct mbuf *);
    142 
    143 int
    144 unp_output(struct mbuf *m, struct mbuf *control, struct unpcb *unp,
    145 	struct lwp *l)
    146 {
    147 	struct socket *so2;
    148 	const struct sockaddr_un *sun;
    149 
    150 	so2 = unp->unp_conn->unp_socket;
    151 	if (unp->unp_addr)
    152 		sun = unp->unp_addr;
    153 	else
    154 		sun = &sun_noname;
    155 	if (unp->unp_conn->unp_flags & UNP_WANTCRED)
    156 		control = unp_addsockcred(l, control);
    157 	if (sbappendaddr(&so2->so_rcv, (const struct sockaddr *)sun, m,
    158 	    control) == 0) {
    159 		unp_dispose(control);
    160 		m_freem(control);
    161 		m_freem(m);
    162 		so2->so_rcv.sb_overflowed++;
    163 		return (ENOBUFS);
    164 	} else {
    165 		sorwakeup(so2);
    166 		return (0);
    167 	}
    168 }
    169 
    170 void
    171 unp_setsockaddr(struct unpcb *unp, struct mbuf *nam)
    172 {
    173 	const struct sockaddr_un *sun;
    174 
    175 	if (unp->unp_addr)
    176 		sun = unp->unp_addr;
    177 	else
    178 		sun = &sun_noname;
    179 	nam->m_len = sun->sun_len;
    180 	if (nam->m_len > MLEN)
    181 		MEXTMALLOC(nam, nam->m_len, M_WAITOK);
    182 	memcpy(mtod(nam, void *), sun, (size_t)nam->m_len);
    183 }
    184 
    185 void
    186 unp_setpeeraddr(struct unpcb *unp, struct mbuf *nam)
    187 {
    188 	const struct sockaddr_un *sun;
    189 
    190 	if (unp->unp_conn && unp->unp_conn->unp_addr)
    191 		sun = unp->unp_conn->unp_addr;
    192 	else
    193 		sun = &sun_noname;
    194 	nam->m_len = sun->sun_len;
    195 	if (nam->m_len > MLEN)
    196 		MEXTMALLOC(nam, nam->m_len, M_WAITOK);
    197 	memcpy(mtod(nam, void *), sun, (size_t)nam->m_len);
    198 }
    199 
    200 /*ARGSUSED*/
    201 int
    202 uipc_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
    203 	struct mbuf *control, struct lwp *l)
    204 {
    205 	struct unpcb *unp = sotounpcb(so);
    206 	struct socket *so2;
    207 	struct proc *p;
    208 	u_int newhiwat;
    209 	int error = 0;
    210 
    211 	if (req == PRU_CONTROL)
    212 		return (EOPNOTSUPP);
    213 
    214 #ifdef DIAGNOSTIC
    215 	if (req != PRU_SEND && req != PRU_SENDOOB && control)
    216 		panic("uipc_usrreq: unexpected control mbuf");
    217 #endif
    218 	p = l ? l->l_proc : NULL;
    219 	if (unp == 0 && req != PRU_ATTACH) {
    220 		error = EINVAL;
    221 		goto release;
    222 	}
    223 
    224 	switch (req) {
    225 
    226 	case PRU_ATTACH:
    227 		if (unp != 0) {
    228 			error = EISCONN;
    229 			break;
    230 		}
    231 		error = unp_attach(so);
    232 		break;
    233 
    234 	case PRU_DETACH:
    235 		unp_detach(unp);
    236 		break;
    237 
    238 	case PRU_BIND:
    239 		KASSERT(l != NULL);
    240 		error = unp_bind(unp, nam, l);
    241 		break;
    242 
    243 	case PRU_LISTEN:
    244 		if (unp->unp_vnode == 0)
    245 			error = EINVAL;
    246 		break;
    247 
    248 	case PRU_CONNECT:
    249 		KASSERT(l != NULL);
    250 		error = unp_connect(so, nam, l);
    251 		break;
    252 
    253 	case PRU_CONNECT2:
    254 		error = unp_connect2(so, (struct socket *)nam, PRU_CONNECT2);
    255 		break;
    256 
    257 	case PRU_DISCONNECT:
    258 		unp_disconnect(unp);
    259 		break;
    260 
    261 	case PRU_ACCEPT:
    262 		unp_setpeeraddr(unp, nam);
    263 		/*
    264 		 * Mark the initiating STREAM socket as connected *ONLY*
    265 		 * after it's been accepted.  This prevents a client from
    266 		 * overrunning a server and receiving ECONNREFUSED.
    267 		 */
    268 		if (unp->unp_conn != NULL &&
    269 		    (unp->unp_conn->unp_socket->so_state & SS_ISCONNECTING))
    270 			soisconnected(unp->unp_conn->unp_socket);
    271 		break;
    272 
    273 	case PRU_SHUTDOWN:
    274 		socantsendmore(so);
    275 		unp_shutdown(unp);
    276 		break;
    277 
    278 	case PRU_RCVD:
    279 		switch (so->so_type) {
    280 
    281 		case SOCK_DGRAM:
    282 			panic("uipc 1");
    283 			/*NOTREACHED*/
    284 
    285 		case SOCK_STREAM:
    286 #define	rcv (&so->so_rcv)
    287 #define snd (&so2->so_snd)
    288 			if (unp->unp_conn == 0)
    289 				break;
    290 			so2 = unp->unp_conn->unp_socket;
    291 			/*
    292 			 * Adjust backpressure on sender
    293 			 * and wakeup any waiting to write.
    294 			 */
    295 			snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt;
    296 			unp->unp_mbcnt = rcv->sb_mbcnt;
    297 			newhiwat = snd->sb_hiwat + unp->unp_cc - rcv->sb_cc;
    298 			(void)chgsbsize(so2->so_uidinfo,
    299 			    &snd->sb_hiwat, newhiwat, RLIM_INFINITY);
    300 			unp->unp_cc = rcv->sb_cc;
    301 			sowwakeup(so2);
    302 #undef snd
    303 #undef rcv
    304 			break;
    305 
    306 		default:
    307 			panic("uipc 2");
    308 		}
    309 		break;
    310 
    311 	case PRU_SEND:
    312 		/*
    313 		 * Note: unp_internalize() rejects any control message
    314 		 * other than SCM_RIGHTS, and only allows one.  This
    315 		 * has the side-effect of preventing a caller from
    316 		 * forging SCM_CREDS.
    317 		 */
    318 		if (control) {
    319 			KASSERT(l != NULL);
    320 			if ((error = unp_internalize(control, l)) != 0)
    321 				goto die;
    322 		}
    323 		switch (so->so_type) {
    324 
    325 		case SOCK_DGRAM: {
    326 			if (nam) {
    327 				if ((so->so_state & SS_ISCONNECTED) != 0) {
    328 					error = EISCONN;
    329 					goto die;
    330 				}
    331 				KASSERT(l != NULL);
    332 				error = unp_connect(so, nam, l);
    333 				if (error) {
    334 				die:
    335 					unp_dispose(control);
    336 					m_freem(control);
    337 					m_freem(m);
    338 					break;
    339 				}
    340 			} else {
    341 				if ((so->so_state & SS_ISCONNECTED) == 0) {
    342 					error = ENOTCONN;
    343 					goto die;
    344 				}
    345 			}
    346 			KASSERT(p != NULL);
    347 			error = unp_output(m, control, unp, l);
    348 			if (nam)
    349 				unp_disconnect(unp);
    350 			break;
    351 		}
    352 
    353 		case SOCK_STREAM:
    354 #define	rcv (&so2->so_rcv)
    355 #define	snd (&so->so_snd)
    356 			if (unp->unp_conn == NULL) {
    357 				error = ENOTCONN;
    358 				break;
    359 			}
    360 			so2 = unp->unp_conn->unp_socket;
    361 			if (unp->unp_conn->unp_flags & UNP_WANTCRED) {
    362 				/*
    363 				 * Credentials are passed only once on
    364 				 * SOCK_STREAM.
    365 				 */
    366 				unp->unp_conn->unp_flags &= ~UNP_WANTCRED;
    367 				control = unp_addsockcred(l, control);
    368 			}
    369 			/*
    370 			 * Send to paired receive port, and then reduce
    371 			 * send buffer hiwater marks to maintain backpressure.
    372 			 * Wake up readers.
    373 			 */
    374 			if (control) {
    375 				if (sbappendcontrol(rcv, m, control) == 0) {
    376 					unp_dispose(control);
    377 					m_freem(control);
    378 				}
    379 			} else
    380 				sbappend(rcv, m);
    381 			snd->sb_mbmax -=
    382 			    rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt;
    383 			unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt;
    384 			newhiwat = snd->sb_hiwat -
    385 			    (rcv->sb_cc - unp->unp_conn->unp_cc);
    386 			(void)chgsbsize(so->so_uidinfo,
    387 			    &snd->sb_hiwat, newhiwat, RLIM_INFINITY);
    388 			unp->unp_conn->unp_cc = rcv->sb_cc;
    389 			sorwakeup(so2);
    390 #undef snd
    391 #undef rcv
    392 			break;
    393 
    394 		default:
    395 			panic("uipc 4");
    396 		}
    397 		break;
    398 
    399 	case PRU_ABORT:
    400 		unp_drop(unp, ECONNABORTED);
    401 
    402 		KASSERT(so->so_head == NULL);
    403 #ifdef DIAGNOSTIC
    404 		if (so->so_pcb == 0)
    405 			panic("uipc 5: drop killed pcb");
    406 #endif
    407 		unp_detach(unp);
    408 		break;
    409 
    410 	case PRU_SENSE:
    411 		((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
    412 		if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
    413 			so2 = unp->unp_conn->unp_socket;
    414 			((struct stat *) m)->st_blksize += so2->so_rcv.sb_cc;
    415 		}
    416 		((struct stat *) m)->st_dev = NODEV;
    417 		if (unp->unp_ino == 0)
    418 			unp->unp_ino = unp_ino++;
    419 		((struct stat *) m)->st_atimespec =
    420 		    ((struct stat *) m)->st_mtimespec =
    421 		    ((struct stat *) m)->st_ctimespec = unp->unp_ctime;
    422 		((struct stat *) m)->st_ino = unp->unp_ino;
    423 		return (0);
    424 
    425 	case PRU_RCVOOB:
    426 		error = EOPNOTSUPP;
    427 		break;
    428 
    429 	case PRU_SENDOOB:
    430 		m_freem(control);
    431 		m_freem(m);
    432 		error = EOPNOTSUPP;
    433 		break;
    434 
    435 	case PRU_SOCKADDR:
    436 		unp_setsockaddr(unp, nam);
    437 		break;
    438 
    439 	case PRU_PEERADDR:
    440 		unp_setpeeraddr(unp, nam);
    441 		break;
    442 
    443 	default:
    444 		panic("piusrreq");
    445 	}
    446 
    447 release:
    448 	return (error);
    449 }
    450 
    451 /*
    452  * Unix domain socket option processing.
    453  */
    454 int
    455 uipc_ctloutput(int op, struct socket *so, int level, int optname,
    456 	struct mbuf **mp)
    457 {
    458 	struct unpcb *unp = sotounpcb(so);
    459 	struct mbuf *m = *mp;
    460 	int optval = 0, error = 0;
    461 
    462 	if (level != 0) {
    463 		error = ENOPROTOOPT;
    464 		if (op == PRCO_SETOPT && m)
    465 			(void) m_free(m);
    466 	} else switch (op) {
    467 
    468 	case PRCO_SETOPT:
    469 		switch (optname) {
    470 		case LOCAL_CREDS:
    471 		case LOCAL_CONNWAIT:
    472 			if (m == NULL || m->m_len != sizeof(int))
    473 				error = EINVAL;
    474 			else {
    475 				optval = *mtod(m, int *);
    476 				switch (optname) {
    477 #define	OPTSET(bit) \
    478 	if (optval) \
    479 		unp->unp_flags |= (bit); \
    480 	else \
    481 		unp->unp_flags &= ~(bit);
    482 
    483 				case LOCAL_CREDS:
    484 					OPTSET(UNP_WANTCRED);
    485 					break;
    486 				case LOCAL_CONNWAIT:
    487 					OPTSET(UNP_CONNWAIT);
    488 					break;
    489 				}
    490 			}
    491 			break;
    492 #undef OPTSET
    493 
    494 		default:
    495 			error = ENOPROTOOPT;
    496 			break;
    497 		}
    498 		if (m)
    499 			(void) m_free(m);
    500 		break;
    501 
    502 	case PRCO_GETOPT:
    503 		switch (optname) {
    504 		case LOCAL_PEEREID:
    505 			if (unp->unp_flags & UNP_EIDSVALID) {
    506 				*mp = m = m_get(M_WAIT, MT_SOOPTS);
    507 				m->m_len = sizeof(struct unpcbid);
    508 				*mtod(m, struct unpcbid *) = unp->unp_connid;
    509 			} else {
    510 				error = EINVAL;
    511 			}
    512 			break;
    513 		case LOCAL_CREDS:
    514 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
    515 			m->m_len = sizeof(int);
    516 
    517 #define	OPTBIT(bit)	(unp->unp_flags & (bit) ? 1 : 0)
    518 
    519 			optval = OPTBIT(UNP_WANTCRED);
    520 			*mtod(m, int *) = optval;
    521 			break;
    522 #undef OPTBIT
    523 
    524 		default:
    525 			error = ENOPROTOOPT;
    526 			break;
    527 		}
    528 		break;
    529 	}
    530 	return (error);
    531 }
    532 
    533 /*
    534  * Both send and receive buffers are allocated PIPSIZ bytes of buffering
    535  * for stream sockets, although the total for sender and receiver is
    536  * actually only PIPSIZ.
    537  * Datagram sockets really use the sendspace as the maximum datagram size,
    538  * and don't really want to reserve the sendspace.  Their recvspace should
    539  * be large enough for at least one max-size datagram plus address.
    540  */
    541 #define	PIPSIZ	4096
    542 u_long	unpst_sendspace = PIPSIZ;
    543 u_long	unpst_recvspace = PIPSIZ;
    544 u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
    545 u_long	unpdg_recvspace = 4*1024;
    546 
    547 u_int	unp_rights;			/* file descriptors in flight */
    548 
    549 int
    550 unp_attach(struct socket *so)
    551 {
    552 	struct unpcb *unp;
    553 	int error;
    554 
    555 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    556 		switch (so->so_type) {
    557 
    558 		case SOCK_STREAM:
    559 			error = soreserve(so, unpst_sendspace, unpst_recvspace);
    560 			break;
    561 
    562 		case SOCK_DGRAM:
    563 			error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
    564 			break;
    565 
    566 		default:
    567 			panic("unp_attach");
    568 		}
    569 		if (error)
    570 			return (error);
    571 	}
    572 	unp = malloc(sizeof(*unp), M_PCB, M_NOWAIT);
    573 	if (unp == NULL)
    574 		return (ENOBUFS);
    575 	memset((void *)unp, 0, sizeof(*unp));
    576 	unp->unp_socket = so;
    577 	so->so_pcb = unp;
    578 	nanotime(&unp->unp_ctime);
    579 	return (0);
    580 }
    581 
    582 void
    583 unp_detach(struct unpcb *unp)
    584 {
    585 
    586 	if (unp->unp_vnode) {
    587 		unp->unp_vnode->v_socket = 0;
    588 		vrele(unp->unp_vnode);
    589 		unp->unp_vnode = 0;
    590 	}
    591 	if (unp->unp_conn)
    592 		unp_disconnect(unp);
    593 	while (unp->unp_refs)
    594 		unp_drop(unp->unp_refs, ECONNRESET);
    595 	soisdisconnected(unp->unp_socket);
    596 	unp->unp_socket->so_pcb = 0;
    597 	if (unp->unp_addr)
    598 		free(unp->unp_addr, M_SONAME);
    599 	if (unp_rights) {
    600 		/*
    601 		 * Normally the receive buffer is flushed later,
    602 		 * in sofree, but if our receive buffer holds references
    603 		 * to descriptors that are now garbage, we will dispose
    604 		 * of those descriptor references after the garbage collector
    605 		 * gets them (resulting in a "panic: closef: count < 0").
    606 		 */
    607 		sorflush(unp->unp_socket);
    608 		free(unp, M_PCB);
    609 		unp_gc();
    610 	} else
    611 		free(unp, M_PCB);
    612 }
    613 
    614 int
    615 unp_bind(struct unpcb *unp, struct mbuf *nam, struct lwp *l)
    616 {
    617 	struct sockaddr_un *sun;
    618 	vnode_t *vp;
    619 	struct vattr vattr;
    620 	size_t addrlen;
    621 	struct proc *p;
    622 	int error;
    623 	struct nameidata nd;
    624 
    625 	if (unp->unp_vnode != 0)
    626 		return (EINVAL);
    627 
    628 	p = l->l_proc;
    629 	/*
    630 	 * Allocate the new sockaddr.  We have to allocate one
    631 	 * extra byte so that we can ensure that the pathname
    632 	 * is nul-terminated.
    633 	 */
    634 	addrlen = nam->m_len + 1;
    635 	sun = malloc(addrlen, M_SONAME, M_WAITOK);
    636 	m_copydata(nam, 0, nam->m_len, (void *)sun);
    637 	*(((char *)sun) + nam->m_len) = '\0';
    638 
    639 	NDINIT(&nd, CREATE, FOLLOW | LOCKPARENT | TRYEMULROOT, UIO_SYSSPACE,
    640 	    sun->sun_path);
    641 
    642 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
    643 	if ((error = namei(&nd)) != 0)
    644 		goto bad;
    645 	vp = nd.ni_vp;
    646 	if (vp != NULL) {
    647 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    648 		if (nd.ni_dvp == vp)
    649 			vrele(nd.ni_dvp);
    650 		else
    651 			vput(nd.ni_dvp);
    652 		vrele(vp);
    653 		error = EADDRINUSE;
    654 		goto bad;
    655 	}
    656 	VATTR_NULL(&vattr);
    657 	vattr.va_type = VSOCK;
    658 	vattr.va_mode = ACCESSPERMS & ~(p->p_cwdi->cwdi_cmask);
    659 	error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
    660 	if (error)
    661 		goto bad;
    662 	vp = nd.ni_vp;
    663 	vp->v_socket = unp->unp_socket;
    664 	unp->unp_vnode = vp;
    665 	unp->unp_addrlen = addrlen;
    666 	unp->unp_addr = sun;
    667 	unp->unp_connid.unp_pid = p->p_pid;
    668 	unp->unp_connid.unp_euid = kauth_cred_geteuid(p->p_cred);
    669 	unp->unp_connid.unp_egid = kauth_cred_getegid(p->p_cred);
    670 	unp->unp_flags |= UNP_EIDSBIND;
    671 	VOP_UNLOCK(vp, 0);
    672 	return (0);
    673 
    674  bad:
    675 	free(sun, M_SONAME);
    676 	return (error);
    677 }
    678 
    679 int
    680 unp_connect(struct socket *so, struct mbuf *nam, struct lwp *l)
    681 {
    682 	struct sockaddr_un *sun;
    683 	vnode_t *vp;
    684 	struct socket *so2, *so3;
    685 	struct unpcb *unp, *unp2, *unp3;
    686 	size_t addrlen;
    687 	struct proc *p;
    688 	int error;
    689 	struct nameidata nd;
    690 
    691 	p = l->l_proc;
    692 	/*
    693 	 * Allocate a temporary sockaddr.  We have to allocate one extra
    694 	 * byte so that we can ensure that the pathname is nul-terminated.
    695 	 * When we establish the connection, we copy the other PCB's
    696 	 * sockaddr to our own.
    697 	 */
    698 	addrlen = nam->m_len + 1;
    699 	sun = malloc(addrlen, M_SONAME, M_WAITOK);
    700 	m_copydata(nam, 0, nam->m_len, (void *)sun);
    701 	*(((char *)sun) + nam->m_len) = '\0';
    702 
    703 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_SYSSPACE,
    704 	    sun->sun_path);
    705 
    706 	if ((error = namei(&nd)) != 0)
    707 		goto bad2;
    708 	vp = nd.ni_vp;
    709 	if (vp->v_type != VSOCK) {
    710 		error = ENOTSOCK;
    711 		goto bad;
    712 	}
    713 	if ((error = VOP_ACCESS(vp, VWRITE, l->l_cred)) != 0)
    714 		goto bad;
    715 	so2 = vp->v_socket;
    716 	if (so2 == 0) {
    717 		error = ECONNREFUSED;
    718 		goto bad;
    719 	}
    720 	if (so->so_type != so2->so_type) {
    721 		error = EPROTOTYPE;
    722 		goto bad;
    723 	}
    724 	if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
    725 		if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
    726 		    (so3 = sonewconn(so2, 0)) == 0) {
    727 			error = ECONNREFUSED;
    728 			goto bad;
    729 		}
    730 		unp = sotounpcb(so);
    731 		unp2 = sotounpcb(so2);
    732 		unp3 = sotounpcb(so3);
    733 		if (unp2->unp_addr) {
    734 			unp3->unp_addr = malloc(unp2->unp_addrlen,
    735 			    M_SONAME, M_WAITOK);
    736 			memcpy(unp3->unp_addr, unp2->unp_addr,
    737 			    unp2->unp_addrlen);
    738 			unp3->unp_addrlen = unp2->unp_addrlen;
    739 		}
    740 		unp3->unp_flags = unp2->unp_flags;
    741 		unp3->unp_connid.unp_pid = p->p_pid;
    742 		unp3->unp_connid.unp_euid = kauth_cred_geteuid(p->p_cred);
    743 		unp3->unp_connid.unp_egid = kauth_cred_getegid(p->p_cred);
    744 		unp3->unp_flags |= UNP_EIDSVALID;
    745 		so2 = so3;
    746 		if (unp2->unp_flags & UNP_EIDSBIND) {
    747 			unp->unp_connid = unp2->unp_connid;
    748 			unp->unp_flags |= UNP_EIDSVALID;
    749 		}
    750 	}
    751 	error = unp_connect2(so, so2, PRU_CONNECT);
    752  bad:
    753 	vput(vp);
    754  bad2:
    755 	free(sun, M_SONAME);
    756 	return (error);
    757 }
    758 
    759 int
    760 unp_connect2(struct socket *so, struct socket *so2, int req)
    761 {
    762 	struct unpcb *unp = sotounpcb(so);
    763 	struct unpcb *unp2;
    764 
    765 	if (so2->so_type != so->so_type)
    766 		return (EPROTOTYPE);
    767 	unp2 = sotounpcb(so2);
    768 	unp->unp_conn = unp2;
    769 	switch (so->so_type) {
    770 
    771 	case SOCK_DGRAM:
    772 		unp->unp_nextref = unp2->unp_refs;
    773 		unp2->unp_refs = unp;
    774 		soisconnected(so);
    775 		break;
    776 
    777 	case SOCK_STREAM:
    778 		unp2->unp_conn = unp;
    779 		if (req == PRU_CONNECT &&
    780 		    ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT))
    781 			soisconnecting(so);
    782 		else
    783 			soisconnected(so);
    784 		soisconnected(so2);
    785 		break;
    786 
    787 	default:
    788 		panic("unp_connect2");
    789 	}
    790 	return (0);
    791 }
    792 
    793 void
    794 unp_disconnect(struct unpcb *unp)
    795 {
    796 	struct unpcb *unp2 = unp->unp_conn;
    797 
    798 	if (unp2 == 0)
    799 		return;
    800 	unp->unp_conn = 0;
    801 	switch (unp->unp_socket->so_type) {
    802 
    803 	case SOCK_DGRAM:
    804 		if (unp2->unp_refs == unp)
    805 			unp2->unp_refs = unp->unp_nextref;
    806 		else {
    807 			unp2 = unp2->unp_refs;
    808 			for (;;) {
    809 				if (unp2 == 0)
    810 					panic("unp_disconnect");
    811 				if (unp2->unp_nextref == unp)
    812 					break;
    813 				unp2 = unp2->unp_nextref;
    814 			}
    815 			unp2->unp_nextref = unp->unp_nextref;
    816 		}
    817 		unp->unp_nextref = 0;
    818 		unp->unp_socket->so_state &= ~SS_ISCONNECTED;
    819 		break;
    820 
    821 	case SOCK_STREAM:
    822 		soisdisconnected(unp->unp_socket);
    823 		unp2->unp_conn = 0;
    824 		soisdisconnected(unp2->unp_socket);
    825 		break;
    826 	}
    827 }
    828 
    829 #ifdef notdef
    830 unp_abort(struct unpcb *unp)
    831 {
    832 	unp_detach(unp);
    833 }
    834 #endif
    835 
    836 void
    837 unp_shutdown(struct unpcb *unp)
    838 {
    839 	struct socket *so;
    840 
    841 	if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
    842 	    (so = unp->unp_conn->unp_socket))
    843 		socantrcvmore(so);
    844 }
    845 
    846 void
    847 unp_drop(struct unpcb *unp, int errno)
    848 {
    849 	struct socket *so = unp->unp_socket;
    850 
    851 	so->so_error = errno;
    852 	unp_disconnect(unp);
    853 	if (so->so_head) {
    854 		so->so_pcb = 0;
    855 		sofree(so);
    856 		if (unp->unp_addr)
    857 			free(unp->unp_addr, M_SONAME);
    858 		free(unp, M_PCB);
    859 	}
    860 }
    861 
    862 #ifdef notdef
    863 unp_drain(void)
    864 {
    865 
    866 }
    867 #endif
    868 
    869 int
    870 unp_externalize(struct mbuf *rights, struct lwp *l)
    871 {
    872 	struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
    873 	struct proc *p = l->l_proc;
    874 	int i, *fdp;
    875 	file_t **rp;
    876 	file_t *fp;
    877 	int nfds, error = 0;
    878 
    879 	nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) /
    880 	    sizeof(file_t *);
    881 	rp = (file_t **)CMSG_DATA(cm);
    882 
    883 	fdp = malloc(nfds * sizeof(int), M_TEMP, M_WAITOK);
    884 	rw_enter(&p->p_cwdi->cwdi_lock, RW_READER);
    885 
    886 	/* Make sure the recipient should be able to see the descriptors.. */
    887 	if (p->p_cwdi->cwdi_rdir != NULL) {
    888 		rp = (file_t **)CMSG_DATA(cm);
    889 		for (i = 0; i < nfds; i++) {
    890 			fp = *rp++;
    891 			/*
    892 			 * If we are in a chroot'ed directory, and
    893 			 * someone wants to pass us a directory, make
    894 			 * sure it's inside the subtree we're allowed
    895 			 * to access.
    896 			 */
    897 			if (fp->f_type == DTYPE_VNODE) {
    898 				vnode_t *vp = (vnode_t *)fp->f_data;
    899 				if ((vp->v_type == VDIR) &&
    900 				    !vn_isunder(vp, p->p_cwdi->cwdi_rdir, l)) {
    901 					error = EPERM;
    902 					break;
    903 				}
    904 			}
    905 		}
    906 	}
    907 
    908  restart:
    909 	rp = (file_t **)CMSG_DATA(cm);
    910 	if (error != 0) {
    911 		for (i = 0; i < nfds; i++) {
    912 			fp = *rp;
    913 			/*
    914 			 * zero the pointer before calling unp_discard,
    915 			 * since it may end up in unp_gc()..
    916 			 */
    917 			*rp++ = 0;
    918 			unp_discard(fp);
    919 		}
    920 		goto out;
    921 	}
    922 
    923 	/*
    924 	 * First loop -- allocate file descriptor table slots for the
    925 	 * new descriptors.
    926 	 */
    927 	for (i = 0; i < nfds; i++) {
    928 		fp = *rp++;
    929 		if ((error = fd_alloc(p, 0, &fdp[i])) != 0) {
    930 			/*
    931 			 * Back out what we've done so far.
    932 			 */
    933 			for (--i; i >= 0; i--) {
    934 				fd_abort(p, NULL, fdp[i]);
    935 			}
    936 			if (error == ENOSPC) {
    937 				fd_tryexpand(p);
    938 				error = 0;
    939 			} else {
    940 				/*
    941 				 * This is the error that has historically
    942 				 * been returned, and some callers may
    943 				 * expect it.
    944 				 */
    945 				error = EMSGSIZE;
    946 			}
    947 			goto restart;
    948 		}
    949 	}
    950 
    951 	/*
    952 	 * Now that adding them has succeeded, update all of the
    953 	 * descriptor passing state.	 */
    954 	rp = (file_t **)CMSG_DATA(cm);
    955 	for (i = 0; i < nfds; i++) {
    956 		fp = *rp++;
    957 		atomic_dec_uint(&unp_rights);
    958 		fd_affix(p, fp, fdp[i]);
    959 		mutex_enter(&fp->f_lock);
    960 		fp->f_msgcount--;
    961 		mutex_exit(&fp->f_lock);
    962 		/*
    963 		 * Note that fd_affix() adds a reference to the file.
    964 		 * The file may already have been closed by another
    965 		 * LWP in the process, so we must drop the reference
    966 		 * added by unp_internalize() with closef().
    967 		 */
    968 		closef(fp);
    969 	}
    970 
    971 	/*
    972 	 * Copy temporary array to message and adjust length, in case of
    973 	 * transition from large file_t pointers to ints.
    974 	 */
    975 	memcpy(CMSG_DATA(cm), fdp, nfds * sizeof(int));
    976 	cm->cmsg_len = CMSG_LEN(nfds * sizeof(int));
    977 	rights->m_len = CMSG_SPACE(nfds * sizeof(int));
    978  out:
    979 	rw_exit(&p->p_cwdi->cwdi_lock);
    980 	free(fdp, M_TEMP);
    981 	return (error);
    982 }
    983 
    984 int
    985 unp_internalize(struct mbuf *control, struct lwp *l)
    986 {
    987 	struct filedesc *fdescp = curlwp->l_fd;
    988 	struct cmsghdr *newcm, *cm = mtod(control, struct cmsghdr *);
    989 	file_t **rp, **files;
    990 	file_t *fp;
    991 	int i, fd, *fdp;
    992 	int nfds, error;
    993 
    994 	KASSERT(l == curlwp);
    995 
    996 	error = 0;
    997 	newcm = NULL;
    998 
    999 	/* Sanity check the control message header. */
   1000 	if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
   1001 	    cm->cmsg_len != control->m_len)
   1002 		return (EINVAL);
   1003 
   1004 	/*
   1005 	 * Verify that the file descriptors are valid, and acquire
   1006 	 * a reference to each.
   1007 	 */
   1008 	nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) / sizeof(int);
   1009 	fdp = (int *)CMSG_DATA(cm);
   1010 	for (i = 0; i < nfds; i++) {
   1011 		fd = *fdp++;
   1012 		if ((fp = fd_getfile(fd)) == NULL) {
   1013 			nfds = i + 1;
   1014 			error = EBADF;
   1015 			goto out;
   1016 		}
   1017 	}
   1018 
   1019 	/* Allocate new space and copy header into it. */
   1020 	newcm = malloc(CMSG_SPACE(nfds * sizeof(file_t *)), M_MBUF, M_WAITOK);
   1021 	if (newcm == NULL) {
   1022 		error = E2BIG;
   1023 		goto out;
   1024 	}
   1025 	memcpy(newcm, cm, sizeof(struct cmsghdr));
   1026 	files = (file_t **)CMSG_DATA(newcm);
   1027 
   1028 	/*
   1029 	 * Transform the file descriptors into file_t pointers, in
   1030 	 * reverse order so that if pointers are bigger than ints, the
   1031 	 * int won't get until we're done.  No need to lock, as we have
   1032 	 * already validated the descriptors with fd_getfile().
   1033 	 */
   1034 	fdp = (int *)CMSG_DATA(cm) + nfds;
   1035 	rp = files + nfds;
   1036 	for (i = 0; i < nfds; i++) {
   1037 		fp = fdescp->fd_ofiles[*--fdp]->ff_file;
   1038 		KASSERT(fp != NULL);
   1039 		mutex_enter(&fp->f_lock);
   1040 		*--rp = fp;
   1041 		fp->f_count++;
   1042 		fp->f_msgcount++;
   1043 		mutex_exit(&fp->f_lock);
   1044 		atomic_inc_uint(&unp_rights);
   1045 	}
   1046 
   1047  out:
   1048  	/* Release descriptor references. */
   1049 	fdp = (int *)CMSG_DATA(cm);
   1050 	for (i = 0; i < nfds; i++) {
   1051 		fd_putfile(*fdp++);
   1052 	}
   1053 
   1054 	if (error == 0) {
   1055 		if (control->m_flags & M_EXT)
   1056 			MEXTREMOVE(control);
   1057 		MEXTADD(control, newcm, CMSG_SPACE(nfds * sizeof(file_t *)),
   1058 		    M_MBUF, NULL, NULL);
   1059 		cm = newcm;
   1060 		/*
   1061 		 * Adjust message & mbuf to note amount of space
   1062 		 * actually used.
   1063 		 */
   1064 		cm->cmsg_len = CMSG_LEN(nfds * sizeof(file_t *));
   1065 		control->m_len = CMSG_SPACE(nfds * sizeof(file_t *));
   1066 	}
   1067 
   1068 	return error;
   1069 }
   1070 
   1071 struct mbuf *
   1072 unp_addsockcred(struct lwp *l, struct mbuf *control)
   1073 {
   1074 	struct cmsghdr *cmp;
   1075 	struct sockcred *sc;
   1076 	struct mbuf *m, *n;
   1077 	int len, space, i;
   1078 
   1079 	len = CMSG_LEN(SOCKCREDSIZE(kauth_cred_ngroups(l->l_cred)));
   1080 	space = CMSG_SPACE(SOCKCREDSIZE(kauth_cred_ngroups(l->l_cred)));
   1081 
   1082 	m = m_get(M_WAIT, MT_CONTROL);
   1083 	if (space > MLEN) {
   1084 		if (space > MCLBYTES)
   1085 			MEXTMALLOC(m, space, M_WAITOK);
   1086 		else
   1087 			m_clget(m, M_WAIT);
   1088 		if ((m->m_flags & M_EXT) == 0) {
   1089 			m_free(m);
   1090 			return (control);
   1091 		}
   1092 	}
   1093 
   1094 	m->m_len = space;
   1095 	m->m_next = NULL;
   1096 	cmp = mtod(m, struct cmsghdr *);
   1097 	sc = (struct sockcred *)CMSG_DATA(cmp);
   1098 	cmp->cmsg_len = len;
   1099 	cmp->cmsg_level = SOL_SOCKET;
   1100 	cmp->cmsg_type = SCM_CREDS;
   1101 	sc->sc_uid = kauth_cred_getuid(l->l_cred);
   1102 	sc->sc_euid = kauth_cred_geteuid(l->l_cred);
   1103 	sc->sc_gid = kauth_cred_getgid(l->l_cred);
   1104 	sc->sc_egid = kauth_cred_getegid(l->l_cred);
   1105 	sc->sc_ngroups = kauth_cred_ngroups(l->l_cred);
   1106 	for (i = 0; i < sc->sc_ngroups; i++)
   1107 		sc->sc_groups[i] = kauth_cred_group(l->l_cred, i);
   1108 
   1109 	/*
   1110 	 * If a control message already exists, append us to the end.
   1111 	 */
   1112 	if (control != NULL) {
   1113 		for (n = control; n->m_next != NULL; n = n->m_next)
   1114 			;
   1115 		n->m_next = m;
   1116 	} else
   1117 		control = m;
   1118 
   1119 	return (control);
   1120 }
   1121 
   1122 int	unp_defer, unp_gcing;
   1123 extern	struct domain unixdomain;
   1124 
   1125 /*
   1126  * Comment added long after the fact explaining what's going on here.
   1127  * Do a mark-sweep GC of file descriptors on the system, to free up
   1128  * any which are caught in flight to an about-to-be-closed socket.
   1129  *
   1130  * Traditional mark-sweep gc's start at the "root", and mark
   1131  * everything reachable from the root (which, in our case would be the
   1132  * process table).  The mark bits are cleared during the sweep.
   1133  *
   1134  * XXX For some inexplicable reason (perhaps because the file
   1135  * descriptor tables used to live in the u area which could be swapped
   1136  * out and thus hard to reach), we do multiple scans over the set of
   1137  * descriptors, using use *two* mark bits per object (DEFER and MARK).
   1138  * Whenever we find a descriptor which references other descriptors,
   1139  * the ones it references are marked with both bits, and we iterate
   1140  * over the whole file table until there are no more DEFER bits set.
   1141  * We also make an extra pass *before* the GC to clear the mark bits,
   1142  * which could have been cleared at almost no cost during the previous
   1143  * sweep.
   1144  */
   1145 void
   1146 unp_gc(void)
   1147 {
   1148 	file_t *fp, *nextfp;
   1149 	struct socket *so, *so1;
   1150 	file_t **extra_ref, **fpp;
   1151 	int nunref, nslots, i;
   1152 
   1153 	if (atomic_swap_uint(&unp_gcing, 1) == 1)
   1154 		return;
   1155 
   1156  restart:
   1157  	nslots = nfiles * 2;
   1158  	extra_ref = kmem_alloc(nslots * sizeof(file_t *), KM_SLEEP);
   1159 
   1160 	mutex_enter(&filelist_lock);
   1161 	unp_defer = 0;
   1162 
   1163 	/* Clear mark bits */
   1164 	LIST_FOREACH(fp, &filehead, f_list) {
   1165 		atomic_and_uint(&fp->f_flag, ~(FMARK|FDEFER));
   1166 	}
   1167 
   1168 	/*
   1169 	 * Iterate over the set of descriptors, marking ones believed
   1170 	 * (based on refcount) to be referenced from a process, and
   1171 	 * marking for rescan descriptors which are queued on a socket.
   1172 	 */
   1173 	do {
   1174 		LIST_FOREACH(fp, &filehead, f_list) {
   1175 			mutex_enter(&fp->f_lock);
   1176 			if (fp->f_flag & FDEFER) {
   1177 				atomic_and_uint(&fp->f_flag, ~FDEFER);
   1178 				unp_defer--;
   1179 				KASSERT(fp->f_count != 0);
   1180 			} else {
   1181 				if (fp->f_count == 0 ||
   1182 				    (fp->f_flag & FMARK) ||
   1183 				    fp->f_count == fp->f_msgcount) {
   1184 					mutex_exit(&fp->f_lock);
   1185 					continue;
   1186 				}
   1187 			}
   1188 			atomic_or_uint(&fp->f_flag, FMARK);
   1189 
   1190 			if (fp->f_type != DTYPE_SOCKET ||
   1191 			    (so = (struct socket *)fp->f_data) == NULL ||
   1192 			    so->so_proto->pr_domain != &unixdomain ||
   1193 			    (so->so_proto->pr_flags&PR_RIGHTS) == 0) {
   1194 				mutex_exit(&fp->f_lock);
   1195 				continue;
   1196 			}
   1197 #ifdef notdef
   1198 			if (so->so_rcv.sb_flags & SB_LOCK) {
   1199 				/*
   1200 				 * This is problematical; it's not clear
   1201 				 * we need to wait for the sockbuf to be
   1202 				 * unlocked (on a uniprocessor, at least),
   1203 				 * and it's also not clear what to do
   1204 				 * if sbwait returns an error due to receipt
   1205 				 * of a signal.  If sbwait does return
   1206 				 * an error, we'll go into an infinite
   1207 				 * loop.  Delete all of this for now.
   1208 				 */
   1209 				(void) sbwait(&so->so_rcv);
   1210 				goto restart;
   1211 			}
   1212 #endif
   1213 			mutex_exit(&fp->f_lock);
   1214 
   1215 			unp_scan(so->so_rcv.sb_mb, unp_mark, 0);
   1216 			/*
   1217 			 * Mark descriptors referenced from sockets queued
   1218 			 * on the accept queue as well.
   1219 			 */
   1220 			if (so->so_options & SO_ACCEPTCONN) {
   1221 				TAILQ_FOREACH(so1, &so->so_q0, so_qe) {
   1222 					unp_scan(so1->so_rcv.sb_mb, unp_mark, 0);
   1223 				}
   1224 				TAILQ_FOREACH(so1, &so->so_q, so_qe) {
   1225 					unp_scan(so1->so_rcv.sb_mb, unp_mark, 0);
   1226 				}
   1227 			}
   1228 		}
   1229 	} while (unp_defer);
   1230 
   1231 	/*
   1232 	 * Sweep pass.  Find unmarked descriptors, and free them.
   1233 	 *
   1234 	 * We grab an extra reference to each of the file table entries
   1235 	 * that are not otherwise accessible and then free the rights
   1236 	 * that are stored in messages on them.
   1237 	 *
   1238 	 * The bug in the original code is a little tricky, so I'll describe
   1239 	 * what's wrong with it here.
   1240 	 *
   1241 	 * It is incorrect to simply unp_discard each entry for f_msgcount
   1242 	 * times -- consider the case of sockets A and B that contain
   1243 	 * references to each other.  On a last close of some other socket,
   1244 	 * we trigger a gc since the number of outstanding rights (unp_rights)
   1245 	 * is non-zero.  If during the sweep phase the gc code un_discards,
   1246 	 * we end up doing a (full) closef on the descriptor.  A closef on A
   1247 	 * results in the following chain.  Closef calls soo_close, which
   1248 	 * calls soclose.   Soclose calls first (through the switch
   1249 	 * uipc_usrreq) unp_detach, which re-invokes unp_gc.  Unp_gc simply
   1250 	 * returns because the previous instance had set unp_gcing, and
   1251 	 * we return all the way back to soclose, which marks the socket
   1252 	 * with SS_NOFDREF, and then calls sofree.  Sofree calls sorflush
   1253 	 * to free up the rights that are queued in messages on the socket A,
   1254 	 * i.e., the reference on B.  The sorflush calls via the dom_dispose
   1255 	 * switch unp_dispose, which unp_scans with unp_discard.  This second
   1256 	 * instance of unp_discard just calls closef on B.
   1257 	 *
   1258 	 * Well, a similar chain occurs on B, resulting in a sorflush on B,
   1259 	 * which results in another closef on A.  Unfortunately, A is already
   1260 	 * being closed, and the descriptor has already been marked with
   1261 	 * SS_NOFDREF, and soclose panics at this point.
   1262 	 *
   1263 	 * Here, we first take an extra reference to each inaccessible
   1264 	 * descriptor.  Then, if the inaccessible descriptor is a
   1265 	 * socket, we call sorflush in case it is a Unix domain
   1266 	 * socket.  After we destroy all the rights carried in
   1267 	 * messages, we do a last closef to get rid of our extra
   1268 	 * reference.  This is the last close, and the unp_detach etc
   1269 	 * will shut down the socket.
   1270 	 *
   1271 	 * 91/09/19, bsy (at) cs.cmu.edu
   1272 	 */
   1273 	if (nslots < nfiles) {
   1274 		 kmem_free(extra_ref, nslots * sizeof(file_t *));
   1275 		 goto restart;
   1276 	}
   1277 	for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; fp != 0;
   1278 	    fp = nextfp) {
   1279 		nextfp = LIST_NEXT(fp, f_list);
   1280 		mutex_enter(&fp->f_lock);
   1281 		if (fp->f_count != 0 &&
   1282 		    fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) {
   1283 			*fpp++ = fp;
   1284 			nunref++;
   1285 			fp->f_count++;
   1286 		}
   1287 		mutex_exit(&fp->f_lock);
   1288 	}
   1289 	mutex_exit(&filelist_lock);
   1290 
   1291 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
   1292 		fp = *fpp;
   1293 		if (fp->f_type == DTYPE_SOCKET)
   1294 			sorflush(fp->f_data);
   1295 	}
   1296 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
   1297 		closef(*fpp);
   1298 	}
   1299 	kmem_free(extra_ref, nslots * sizeof(file_t *));
   1300 	atomic_swap_uint(&unp_gcing, 0);
   1301 }
   1302 
   1303 void
   1304 unp_dispose(struct mbuf *m)
   1305 {
   1306 
   1307 	if (m)
   1308 		unp_scan(m, unp_discard, 1);
   1309 }
   1310 
   1311 void
   1312 unp_scan(struct mbuf *m0, void (*op)(file_t *), int discard)
   1313 {
   1314 	struct mbuf *m;
   1315 	file_t **rp;
   1316 	struct cmsghdr *cm;
   1317 	int i;
   1318 	int qfds;
   1319 
   1320 	while (m0) {
   1321 		for (m = m0; m; m = m->m_next) {
   1322 			if (m->m_type == MT_CONTROL &&
   1323 			    m->m_len >= sizeof(*cm)) {
   1324 				cm = mtod(m, struct cmsghdr *);
   1325 				if (cm->cmsg_level != SOL_SOCKET ||
   1326 				    cm->cmsg_type != SCM_RIGHTS)
   1327 					continue;
   1328 				qfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm)))
   1329 				    / sizeof(file_t *);
   1330 				rp = (file_t **)CMSG_DATA(cm);
   1331 				for (i = 0; i < qfds; i++) {
   1332 					file_t *fp = *rp;
   1333 					if (discard)
   1334 						*rp = 0;
   1335 					(*op)(fp);
   1336 					rp++;
   1337 				}
   1338 				break;		/* XXX, but saves time */
   1339 			}
   1340 		}
   1341 		m0 = m0->m_nextpkt;
   1342 	}
   1343 }
   1344 
   1345 void
   1346 unp_mark(file_t *fp)
   1347 {
   1348 
   1349 	if (fp == NULL)
   1350 		return;
   1351 
   1352 	/* If we're already deferred, don't screw up the defer count */
   1353 	mutex_enter(&fp->f_lock);
   1354 	if (fp->f_flag & (FMARK | FDEFER)) {
   1355 		mutex_exit(&fp->f_lock);
   1356 		return;
   1357 	}
   1358 
   1359 	/*
   1360 	 * Minimize the number of deferrals...  Sockets are the only
   1361 	 * type of descriptor which can hold references to another
   1362 	 * descriptor, so just mark other descriptors, and defer
   1363 	 * unmarked sockets for the next pass.
   1364 	 */
   1365 	if (fp->f_type == DTYPE_SOCKET) {
   1366 		unp_defer++;
   1367 		KASSERT(fp->f_count != 0);
   1368 		atomic_or_uint(&fp->f_flag, FDEFER);
   1369 	} else {
   1370 		atomic_or_uint(&fp->f_flag, FMARK);
   1371 	}
   1372 	mutex_exit(&fp->f_lock);
   1373 	return;
   1374 }
   1375 
   1376 void
   1377 unp_discard(file_t *fp)
   1378 {
   1379 
   1380 	if (fp == NULL)
   1381 		return;
   1382 
   1383 	mutex_enter(&fp->f_lock);
   1384 	KASSERT(fp->f_count > 0);
   1385 	fp->f_msgcount--;
   1386 	mutex_exit(&fp->f_lock);
   1387 	atomic_dec_uint(&unp_rights);
   1388 	(void)closef(fp);
   1389 }
   1390