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