xref: /freebsd/sys/kern/uipc_usrreq.c (revision 1b2e3b4b465d68c3f6aa7e8a369d0d432dae10aa)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29748e0b0aSGarrett Wollman  *	From: @(#)uipc_usrreq.c	8.3 (Berkeley) 1/4/94
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
32677b542eSDavid E. O'Brien #include <sys/cdefs.h>
33677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
34677b542eSDavid E. O'Brien 
35335654d7SRobert Watson #include "opt_mac.h"
36335654d7SRobert Watson 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38fb919e4dSMark Murray #include <sys/domain.h>
39960ed29cSSeigo Tanimura #include <sys/fcntl.h>
40d826c479SBruce Evans #include <sys/malloc.h>		/* XXX must be before <sys/file.h> */
41639acc13SGarrett Wollman #include <sys/file.h>
42960ed29cSSeigo Tanimura #include <sys/filedesc.h>
43960ed29cSSeigo Tanimura #include <sys/jail.h>
44960ed29cSSeigo Tanimura #include <sys/kernel.h>
45960ed29cSSeigo Tanimura #include <sys/lock.h>
466ea48a90SRobert Watson #include <sys/mac.h>
47639acc13SGarrett Wollman #include <sys/mbuf.h>
48960ed29cSSeigo Tanimura #include <sys/mutex.h>
49639acc13SGarrett Wollman #include <sys/namei.h>
50639acc13SGarrett Wollman #include <sys/proc.h>
51df8bae1dSRodney W. Grimes #include <sys/protosw.h>
52960ed29cSSeigo Tanimura #include <sys/resourcevar.h>
53df8bae1dSRodney W. Grimes #include <sys/socket.h>
54df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
55960ed29cSSeigo Tanimura #include <sys/signalvar.h>
56df8bae1dSRodney W. Grimes #include <sys/stat.h>
57960ed29cSSeigo Tanimura #include <sys/sx.h>
58639acc13SGarrett Wollman #include <sys/sysctl.h>
59960ed29cSSeigo Tanimura #include <sys/systm.h>
60639acc13SGarrett Wollman #include <sys/un.h>
6198271db4SGarrett Wollman #include <sys/unpcb.h>
62639acc13SGarrett Wollman #include <sys/vnode.h>
63df8bae1dSRodney W. Grimes 
649e9d298aSJeff Roberson #include <vm/uma.h>
6598271db4SGarrett Wollman 
669e9d298aSJeff Roberson static uma_zone_t unp_zone;
6798271db4SGarrett Wollman static	unp_gen_t unp_gencnt;
6898271db4SGarrett Wollman static	u_int unp_count;
6998271db4SGarrett Wollman 
7098271db4SGarrett Wollman static	struct unp_head unp_shead, unp_dhead;
7198271db4SGarrett Wollman 
72df8bae1dSRodney W. Grimes /*
73df8bae1dSRodney W. Grimes  * Unix communications domain.
74df8bae1dSRodney W. Grimes  *
75df8bae1dSRodney W. Grimes  * TODO:
76df8bae1dSRodney W. Grimes  *	SEQPACKET, RDM
77df8bae1dSRodney W. Grimes  *	rethink name space problems
78df8bae1dSRodney W. Grimes  *	need a proper out-of-band
7998271db4SGarrett Wollman  *	lock pushdown
80df8bae1dSRodney W. Grimes  */
81e7dd9a10SRobert Watson static const struct	sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL };
82f708ef1bSPoul-Henning Kamp static ino_t	unp_ino;		/* prototype for fake inode numbers */
83f708ef1bSPoul-Henning Kamp 
840d9ce3a1SRobert Watson static struct mtx unp_mtx;
850d9ce3a1SRobert Watson #define	UNP_LOCK_INIT() \
860d9ce3a1SRobert Watson 	mtx_init(&unp_mtx, "unp", NULL, MTX_DEF)
870d9ce3a1SRobert Watson #define	UNP_LOCK()		mtx_lock(&unp_mtx)
880d9ce3a1SRobert Watson #define	UNP_UNLOCK()		mtx_unlock(&unp_mtx)
890d9ce3a1SRobert Watson #define	UNP_LOCK_ASSERT()	mtx_assert(&unp_mtx, MA_OWNED)
900d9ce3a1SRobert Watson 
914d77a549SAlfred Perlstein static int     unp_attach(struct socket *);
924d77a549SAlfred Perlstein static void    unp_detach(struct unpcb *);
934d77a549SAlfred Perlstein static int     unp_bind(struct unpcb *,struct sockaddr *, struct thread *);
9470f52b48SBruce Evans static int     unp_connect(struct socket *,struct sockaddr *, struct thread *);
95db48c0d2SRobert Watson static int     unp_connect2(struct socket *so, struct socket *so2);
964d77a549SAlfred Perlstein static void    unp_disconnect(struct unpcb *);
974d77a549SAlfred Perlstein static void    unp_shutdown(struct unpcb *);
984d77a549SAlfred Perlstein static void    unp_drop(struct unpcb *, int);
994d77a549SAlfred Perlstein static void    unp_gc(void);
1004d77a549SAlfred Perlstein static void    unp_scan(struct mbuf *, void (*)(struct file *));
1014d77a549SAlfred Perlstein static void    unp_mark(struct file *);
1024d77a549SAlfred Perlstein static void    unp_discard(struct file *);
1034d77a549SAlfred Perlstein static void    unp_freerights(struct file **, int);
1044d77a549SAlfred Perlstein static int     unp_internalize(struct mbuf **, struct thread *);
1054d77a549SAlfred Perlstein static int     unp_listen(struct unpcb *, struct thread *);
106f708ef1bSPoul-Henning Kamp 
107a29f300eSGarrett Wollman static int
108a29f300eSGarrett Wollman uipc_abort(struct socket *so)
109df8bae1dSRodney W. Grimes {
110df8bae1dSRodney W. Grimes 	struct unpcb *unp = sotounpcb(so);
111df8bae1dSRodney W. Grimes 
112fc3fcacfSRobert Watson 	if (unp == NULL)
113e5aeaa0cSDag-Erling Smørgrav 		return (EINVAL);
1140d9ce3a1SRobert Watson 	UNP_LOCK();
115a29f300eSGarrett Wollman 	unp_drop(unp, ECONNABORTED);
1160d9ce3a1SRobert Watson 	unp_detach(unp);	/* NB: unlocks */
117395a08c9SRobert Watson 	SOCK_LOCK(so);
118ddb7d629SIan Dowse 	sotryfree(so);
119e5aeaa0cSDag-Erling Smørgrav 	return (0);
120df8bae1dSRodney W. Grimes }
121df8bae1dSRodney W. Grimes 
122a29f300eSGarrett Wollman static int
12357bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam)
124a29f300eSGarrett Wollman {
125a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
1260d9ce3a1SRobert Watson 	const struct sockaddr *sa;
127df8bae1dSRodney W. Grimes 
128fc3fcacfSRobert Watson 	if (unp == NULL)
129e5aeaa0cSDag-Erling Smørgrav 		return (EINVAL);
130df8bae1dSRodney W. Grimes 
131df8bae1dSRodney W. Grimes 	/*
132df8bae1dSRodney W. Grimes 	 * Pass back name of connected socket,
133df8bae1dSRodney W. Grimes 	 * if it was bound and we are still connected
134df8bae1dSRodney W. Grimes 	 * (our peer may have closed already!).
135df8bae1dSRodney W. Grimes 	 */
1360d9ce3a1SRobert Watson 	*nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
1370d9ce3a1SRobert Watson 	UNP_LOCK();
1380d9ce3a1SRobert Watson 	if (unp->unp_conn != NULL && unp->unp_conn->unp_addr != NULL)
1390d9ce3a1SRobert Watson 		sa = (struct sockaddr *) unp->unp_conn->unp_addr;
1400d9ce3a1SRobert Watson 	else
1410d9ce3a1SRobert Watson 		sa = &sun_noname;
1420d9ce3a1SRobert Watson 	bcopy(sa, *nam, sa->sa_len);
1430d9ce3a1SRobert Watson 	UNP_UNLOCK();
144e5aeaa0cSDag-Erling Smørgrav 	return (0);
145a29f300eSGarrett Wollman }
146df8bae1dSRodney W. Grimes 
147a29f300eSGarrett Wollman static int
148b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td)
149a29f300eSGarrett Wollman {
150a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
151df8bae1dSRodney W. Grimes 
152fc3fcacfSRobert Watson 	if (unp != NULL)
153e5aeaa0cSDag-Erling Smørgrav 		return (EISCONN);
154e5aeaa0cSDag-Erling Smørgrav 	return (unp_attach(so));
155a29f300eSGarrett Wollman }
156a29f300eSGarrett Wollman 
157a29f300eSGarrett Wollman static int
158b40ce416SJulian Elischer uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
159a29f300eSGarrett Wollman {
160a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
161a29f300eSGarrett Wollman 
162fc3fcacfSRobert Watson 	if (unp == NULL)
163e5aeaa0cSDag-Erling Smørgrav 		return (EINVAL);
164a29f300eSGarrett Wollman 
165e5aeaa0cSDag-Erling Smørgrav 	return (unp_bind(unp, nam, td));
166a29f300eSGarrett Wollman }
167a29f300eSGarrett Wollman 
168a29f300eSGarrett Wollman static int
169b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
170a29f300eSGarrett Wollman {
171a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
1720d9ce3a1SRobert Watson 	int error;
173a29f300eSGarrett Wollman 
174fc3fcacfSRobert Watson 	if (unp == NULL)
175e5aeaa0cSDag-Erling Smørgrav 		return (EINVAL);
1760d9ce3a1SRobert Watson 	UNP_LOCK();
1770d9ce3a1SRobert Watson 	error = unp_connect(so, nam, curthread);
1780d9ce3a1SRobert Watson 	UNP_UNLOCK();
1790d9ce3a1SRobert Watson 	return (error);
180a29f300eSGarrett Wollman }
181a29f300eSGarrett Wollman 
182db48c0d2SRobert Watson int
183a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2)
184a29f300eSGarrett Wollman {
185a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so1);
1860d9ce3a1SRobert Watson 	int error;
187a29f300eSGarrett Wollman 
188fc3fcacfSRobert Watson 	if (unp == NULL)
189e5aeaa0cSDag-Erling Smørgrav 		return (EINVAL);
190a29f300eSGarrett Wollman 
1910d9ce3a1SRobert Watson 	UNP_LOCK();
1920d9ce3a1SRobert Watson 	error = unp_connect2(so1, so2);
1930d9ce3a1SRobert Watson 	UNP_UNLOCK();
1940d9ce3a1SRobert Watson 	return (error);
195a29f300eSGarrett Wollman }
196a29f300eSGarrett Wollman 
197a29f300eSGarrett Wollman /* control is EOPNOTSUPP */
198a29f300eSGarrett Wollman 
199a29f300eSGarrett Wollman static int
200a29f300eSGarrett Wollman uipc_detach(struct socket *so)
201a29f300eSGarrett Wollman {
202a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
203a29f300eSGarrett Wollman 
204fc3fcacfSRobert Watson 	if (unp == NULL)
205e5aeaa0cSDag-Erling Smørgrav 		return (EINVAL);
206a29f300eSGarrett Wollman 
2070d9ce3a1SRobert Watson 	UNP_LOCK();
2080d9ce3a1SRobert Watson 	unp_detach(unp);	/* NB: unlocks unp */
209e5aeaa0cSDag-Erling Smørgrav 	return (0);
210a29f300eSGarrett Wollman }
211a29f300eSGarrett Wollman 
212a29f300eSGarrett Wollman static int
213a29f300eSGarrett Wollman uipc_disconnect(struct socket *so)
214a29f300eSGarrett Wollman {
215a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
216a29f300eSGarrett Wollman 
217fc3fcacfSRobert Watson 	if (unp == NULL)
218e5aeaa0cSDag-Erling Smørgrav 		return (EINVAL);
2190d9ce3a1SRobert Watson 	UNP_LOCK();
220a29f300eSGarrett Wollman 	unp_disconnect(unp);
2210d9ce3a1SRobert Watson 	UNP_UNLOCK();
222e5aeaa0cSDag-Erling Smørgrav 	return (0);
223a29f300eSGarrett Wollman }
224a29f300eSGarrett Wollman 
225a29f300eSGarrett Wollman static int
226b40ce416SJulian Elischer uipc_listen(struct socket *so, struct thread *td)
227a29f300eSGarrett Wollman {
228a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
2290d9ce3a1SRobert Watson 	int error;
230a29f300eSGarrett Wollman 
231fc3fcacfSRobert Watson 	if (unp == NULL || unp->unp_vnode == NULL)
232e5aeaa0cSDag-Erling Smørgrav 		return (EINVAL);
2330d9ce3a1SRobert Watson 	UNP_LOCK();
2340d9ce3a1SRobert Watson 	error = unp_listen(unp, td);
2350d9ce3a1SRobert Watson 	UNP_UNLOCK();
2360d9ce3a1SRobert Watson 	return (error);
237a29f300eSGarrett Wollman }
238a29f300eSGarrett Wollman 
239a29f300eSGarrett Wollman static int
24057bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam)
241a29f300eSGarrett Wollman {
242a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
2430d9ce3a1SRobert Watson 	const struct sockaddr *sa;
244a29f300eSGarrett Wollman 
245fc3fcacfSRobert Watson 	if (unp == NULL)
246e5aeaa0cSDag-Erling Smørgrav 		return (EINVAL);
2470d9ce3a1SRobert Watson 	*nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
2480d9ce3a1SRobert Watson 	UNP_LOCK();
249fc3fcacfSRobert Watson 	if (unp->unp_conn != NULL && unp->unp_conn->unp_addr!= NULL)
2500d9ce3a1SRobert Watson 		sa = (struct sockaddr *) unp->unp_conn->unp_addr;
251bdc5f6a3SHajimu UMEMOTO 	else {
252bdc5f6a3SHajimu UMEMOTO 		/*
253bdc5f6a3SHajimu UMEMOTO 		 * XXX: It seems that this test always fails even when
254bdc5f6a3SHajimu UMEMOTO 		 * connection is established.  So, this else clause is
255bdc5f6a3SHajimu UMEMOTO 		 * added as workaround to return PF_LOCAL sockaddr.
256bdc5f6a3SHajimu UMEMOTO 		 */
2570d9ce3a1SRobert Watson 		sa = &sun_noname;
258bdc5f6a3SHajimu UMEMOTO 	}
2590d9ce3a1SRobert Watson 	bcopy(sa, *nam, sa->sa_len);
2600d9ce3a1SRobert Watson 	UNP_UNLOCK();
261e5aeaa0cSDag-Erling Smørgrav 	return (0);
262a29f300eSGarrett Wollman }
263a29f300eSGarrett Wollman 
264a29f300eSGarrett Wollman static int
265a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags)
266a29f300eSGarrett Wollman {
267a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
268a29f300eSGarrett Wollman 	struct socket *so2;
2696aef685fSBrian Feldman 	u_long newhiwat;
270a29f300eSGarrett Wollman 
271fc3fcacfSRobert Watson 	if (unp == NULL)
272e5aeaa0cSDag-Erling Smørgrav 		return (EINVAL);
2730d9ce3a1SRobert Watson 	UNP_LOCK();
274df8bae1dSRodney W. Grimes 	switch (so->so_type) {
275df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
276a29f300eSGarrett Wollman 		panic("uipc_rcvd DGRAM?");
277df8bae1dSRodney W. Grimes 		/*NOTREACHED*/
278df8bae1dSRodney W. Grimes 
279df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
280fc3fcacfSRobert Watson 		if (unp->unp_conn == NULL)
281df8bae1dSRodney W. Grimes 			break;
282df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
283df8bae1dSRodney W. Grimes 		/*
284df8bae1dSRodney W. Grimes 		 * Adjust backpressure on sender
285df8bae1dSRodney W. Grimes 		 * and wakeup any waiting to write.
286df8bae1dSRodney W. Grimes 		 */
287ff8b0106SBrian Feldman 		so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt;
288ff8b0106SBrian Feldman 		unp->unp_mbcnt = so->so_rcv.sb_mbcnt;
2896aef685fSBrian Feldman 		newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc -
2906aef685fSBrian Feldman 		    so->so_rcv.sb_cc;
291f535380cSDon Lewis 		(void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat,
2926aef685fSBrian Feldman 		    newhiwat, RLIM_INFINITY);
293ff8b0106SBrian Feldman 		unp->unp_cc = so->so_rcv.sb_cc;
294df8bae1dSRodney W. Grimes 		sowwakeup(so2);
295df8bae1dSRodney W. Grimes 		break;
296df8bae1dSRodney W. Grimes 
297df8bae1dSRodney W. Grimes 	default:
298a29f300eSGarrett Wollman 		panic("uipc_rcvd unknown socktype");
299df8bae1dSRodney W. Grimes 	}
3000d9ce3a1SRobert Watson 	UNP_UNLOCK();
301e5aeaa0cSDag-Erling Smørgrav 	return (0);
302a29f300eSGarrett Wollman }
303df8bae1dSRodney W. Grimes 
304a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */
305a29f300eSGarrett Wollman 
306a29f300eSGarrett Wollman static int
30757bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
308b40ce416SJulian Elischer 	  struct mbuf *control, struct thread *td)
309a29f300eSGarrett Wollman {
310a29f300eSGarrett Wollman 	int error = 0;
311a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
312a29f300eSGarrett Wollman 	struct socket *so2;
3136aef685fSBrian Feldman 	u_long newhiwat;
314a29f300eSGarrett Wollman 
315fc3fcacfSRobert Watson 	if (unp == NULL) {
316a29f300eSGarrett Wollman 		error = EINVAL;
317a29f300eSGarrett Wollman 		goto release;
318a29f300eSGarrett Wollman 	}
319a29f300eSGarrett Wollman 	if (flags & PRUS_OOB) {
320a29f300eSGarrett Wollman 		error = EOPNOTSUPP;
321a29f300eSGarrett Wollman 		goto release;
322a29f300eSGarrett Wollman 	}
323a29f300eSGarrett Wollman 
324fc3fcacfSRobert Watson 	if (control != NULL && (error = unp_internalize(&control, td)))
325a29f300eSGarrett Wollman 		goto release;
326df8bae1dSRodney W. Grimes 
3270d9ce3a1SRobert Watson 	UNP_LOCK();
328a29f300eSGarrett Wollman 	switch (so->so_type) {
329a29f300eSGarrett Wollman 	case SOCK_DGRAM:
330a29f300eSGarrett Wollman 	{
331e7dd9a10SRobert Watson 		const struct sockaddr *from;
332df8bae1dSRodney W. Grimes 
333fc3fcacfSRobert Watson 		if (nam != NULL) {
334fc3fcacfSRobert Watson 			if (unp->unp_conn != NULL) {
335df8bae1dSRodney W. Grimes 				error = EISCONN;
336df8bae1dSRodney W. Grimes 				break;
337df8bae1dSRodney W. Grimes 			}
338b40ce416SJulian Elischer 			error = unp_connect(so, nam, td);
339df8bae1dSRodney W. Grimes 			if (error)
340df8bae1dSRodney W. Grimes 				break;
341df8bae1dSRodney W. Grimes 		} else {
342fc3fcacfSRobert Watson 			if (unp->unp_conn == NULL) {
343df8bae1dSRodney W. Grimes 				error = ENOTCONN;
344df8bae1dSRodney W. Grimes 				break;
345df8bae1dSRodney W. Grimes 			}
346df8bae1dSRodney W. Grimes 		}
347df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
348fc3fcacfSRobert Watson 		if (unp->unp_addr != NULL)
34957bf258eSGarrett Wollman 			from = (struct sockaddr *)unp->unp_addr;
350df8bae1dSRodney W. Grimes 		else
351df8bae1dSRodney W. Grimes 			from = &sun_noname;
352df8bae1dSRodney W. Grimes 		if (sbappendaddr(&so2->so_rcv, from, m, control)) {
353df8bae1dSRodney W. Grimes 			sorwakeup(so2);
354fc3fcacfSRobert Watson 			m = NULL;
355fc3fcacfSRobert Watson 			control = NULL;
356e5aeaa0cSDag-Erling Smørgrav 		} else {
357df8bae1dSRodney W. Grimes 			error = ENOBUFS;
358e5aeaa0cSDag-Erling Smørgrav 		}
359fc3fcacfSRobert Watson 		if (nam != NULL)
360df8bae1dSRodney W. Grimes 			unp_disconnect(unp);
361df8bae1dSRodney W. Grimes 		break;
362df8bae1dSRodney W. Grimes 	}
363df8bae1dSRodney W. Grimes 
364df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
3656b8fda4dSGarrett Wollman 		/* Connect if not connected yet. */
3666b8fda4dSGarrett Wollman 		/*
3676b8fda4dSGarrett Wollman 		 * Note: A better implementation would complain
368402cc72dSDavid Greenman 		 * if not equal to the peer's address.
3696b8fda4dSGarrett Wollman 		 */
370402cc72dSDavid Greenman 		if ((so->so_state & SS_ISCONNECTED) == 0) {
371fc3fcacfSRobert Watson 			if (nam != NULL) {
372b40ce416SJulian Elischer 				error = unp_connect(so, nam, td);
373402cc72dSDavid Greenman 				if (error)
3746b8fda4dSGarrett Wollman 					break;	/* XXX */
375402cc72dSDavid Greenman 			} else {
376402cc72dSDavid Greenman 				error = ENOTCONN;
377402cc72dSDavid Greenman 				break;
378402cc72dSDavid Greenman 			}
379402cc72dSDavid Greenman 		}
380402cc72dSDavid Greenman 
381c0b99ffaSRobert Watson 		if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
382df8bae1dSRodney W. Grimes 			error = EPIPE;
383df8bae1dSRodney W. Grimes 			break;
384df8bae1dSRodney W. Grimes 		}
385fc3fcacfSRobert Watson 		if (unp->unp_conn == NULL)
386a29f300eSGarrett Wollman 			panic("uipc_send connected but no connection?");
387df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
388df8bae1dSRodney W. Grimes 		/*
389df8bae1dSRodney W. Grimes 		 * Send to paired receive port, and then reduce
390df8bae1dSRodney W. Grimes 		 * send buffer hiwater marks to maintain backpressure.
391df8bae1dSRodney W. Grimes 		 * Wake up readers.
392df8bae1dSRodney W. Grimes 		 */
393fc3fcacfSRobert Watson 		if (control != NULL) {
394ff8b0106SBrian Feldman 			if (sbappendcontrol(&so2->so_rcv, m, control))
395fc3fcacfSRobert Watson 				control = NULL;
396e5aeaa0cSDag-Erling Smørgrav 		} else {
397ff8b0106SBrian Feldman 			sbappend(&so2->so_rcv, m);
398e5aeaa0cSDag-Erling Smørgrav 		}
399ff8b0106SBrian Feldman 		so->so_snd.sb_mbmax -=
400ff8b0106SBrian Feldman 			so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt;
401ff8b0106SBrian Feldman 		unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt;
4026aef685fSBrian Feldman 		newhiwat = so->so_snd.sb_hiwat -
4036aef685fSBrian Feldman 		    (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc);
404f535380cSDon Lewis 		(void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat,
4056aef685fSBrian Feldman 		    newhiwat, RLIM_INFINITY);
406ff8b0106SBrian Feldman 		unp->unp_conn->unp_cc = so2->so_rcv.sb_cc;
407df8bae1dSRodney W. Grimes 		sorwakeup(so2);
408fc3fcacfSRobert Watson 		m = NULL;
409df8bae1dSRodney W. Grimes 		break;
410df8bae1dSRodney W. Grimes 
411df8bae1dSRodney W. Grimes 	default:
412a29f300eSGarrett Wollman 		panic("uipc_send unknown socktype");
413df8bae1dSRodney W. Grimes 	}
414a29f300eSGarrett Wollman 
4156b8fda4dSGarrett Wollman 	/*
4166b8fda4dSGarrett Wollman 	 * SEND_EOF is equivalent to a SEND followed by
4176b8fda4dSGarrett Wollman 	 * a SHUTDOWN.
4186b8fda4dSGarrett Wollman 	 */
419a29f300eSGarrett Wollman 	if (flags & PRUS_EOF) {
4206b8fda4dSGarrett Wollman 		socantsendmore(so);
4216b8fda4dSGarrett Wollman 		unp_shutdown(unp);
4226b8fda4dSGarrett Wollman 	}
4230d9ce3a1SRobert Watson 	UNP_UNLOCK();
424df8bae1dSRodney W. Grimes 
425fc3fcacfSRobert Watson 	if (control != NULL && error != 0)
426bd508d39SDon Lewis 		unp_dispose(control);
427bd508d39SDon Lewis 
428a29f300eSGarrett Wollman release:
429fc3fcacfSRobert Watson 	if (control != NULL)
430a29f300eSGarrett Wollman 		m_freem(control);
431fc3fcacfSRobert Watson 	if (m != NULL)
432a29f300eSGarrett Wollman 		m_freem(m);
433e5aeaa0cSDag-Erling Smørgrav 	return (error);
434a29f300eSGarrett Wollman }
435df8bae1dSRodney W. Grimes 
436a29f300eSGarrett Wollman static int
437a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb)
438a29f300eSGarrett Wollman {
439a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
440a29f300eSGarrett Wollman 	struct socket *so2;
441a29f300eSGarrett Wollman 
442fc3fcacfSRobert Watson 	if (unp == NULL)
443e5aeaa0cSDag-Erling Smørgrav 		return (EINVAL);
4440d9ce3a1SRobert Watson 	UNP_LOCK();
445a29f300eSGarrett Wollman 	sb->st_blksize = so->so_snd.sb_hiwat;
446fc3fcacfSRobert Watson 	if (so->so_type == SOCK_STREAM && unp->unp_conn != NULL) {
447df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
448a29f300eSGarrett Wollman 		sb->st_blksize += so2->so_rcv.sb_cc;
449df8bae1dSRodney W. Grimes 	}
450f3732fd1SPoul-Henning Kamp 	sb->st_dev = NODEV;
451df8bae1dSRodney W. Grimes 	if (unp->unp_ino == 0)
4526f782c46SJeffrey Hsu 		unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino;
453a29f300eSGarrett Wollman 	sb->st_ino = unp->unp_ino;
4540d9ce3a1SRobert Watson 	UNP_UNLOCK();
455df8bae1dSRodney W. Grimes 	return (0);
456a29f300eSGarrett Wollman }
457df8bae1dSRodney W. Grimes 
458a29f300eSGarrett Wollman static int
459a29f300eSGarrett Wollman uipc_shutdown(struct socket *so)
460a29f300eSGarrett Wollman {
461a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
462df8bae1dSRodney W. Grimes 
463fc3fcacfSRobert Watson 	if (unp == NULL)
464e5aeaa0cSDag-Erling Smørgrav 		return (EINVAL);
4650d9ce3a1SRobert Watson 	UNP_LOCK();
466a29f300eSGarrett Wollman 	socantsendmore(so);
467a29f300eSGarrett Wollman 	unp_shutdown(unp);
4680d9ce3a1SRobert Watson 	UNP_UNLOCK();
469e5aeaa0cSDag-Erling Smørgrav 	return (0);
470a29f300eSGarrett Wollman }
471df8bae1dSRodney W. Grimes 
472a29f300eSGarrett Wollman static int
47357bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam)
474a29f300eSGarrett Wollman {
475a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
4760d9ce3a1SRobert Watson 	const struct sockaddr *sa;
477a29f300eSGarrett Wollman 
478fc3fcacfSRobert Watson 	if (unp == NULL)
479e5aeaa0cSDag-Erling Smørgrav 		return (EINVAL);
4800d9ce3a1SRobert Watson 	*nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
4810d9ce3a1SRobert Watson 	UNP_LOCK();
482fc3fcacfSRobert Watson 	if (unp->unp_addr != NULL)
4830d9ce3a1SRobert Watson 		sa = (struct sockaddr *) unp->unp_addr;
48483f3198bSThomas Moestl 	else
4850d9ce3a1SRobert Watson 		sa = &sun_noname;
4860d9ce3a1SRobert Watson 	bcopy(sa, *nam, sa->sa_len);
4870d9ce3a1SRobert Watson 	UNP_UNLOCK();
488e5aeaa0cSDag-Erling Smørgrav 	return (0);
489df8bae1dSRodney W. Grimes }
490a29f300eSGarrett Wollman 
491a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = {
492a29f300eSGarrett Wollman 	uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect,
493a29f300eSGarrett Wollman 	uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect,
494a29f300eSGarrett Wollman 	uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp,
495a29f300eSGarrett Wollman 	uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr,
496a557af22SRobert Watson 	sosend, soreceive, sopoll, pru_sosetlabel_null
497a29f300eSGarrett Wollman };
498df8bae1dSRodney W. Grimes 
4990c1bb4fbSDima Dorfman int
5000c1bb4fbSDima Dorfman uipc_ctloutput(so, sopt)
5010c1bb4fbSDima Dorfman 	struct socket *so;
5020c1bb4fbSDima Dorfman 	struct sockopt *sopt;
5030c1bb4fbSDima Dorfman {
5040c1bb4fbSDima Dorfman 	struct unpcb *unp = sotounpcb(so);
5050d9ce3a1SRobert Watson 	struct xucred xu;
5060c1bb4fbSDima Dorfman 	int error;
5070c1bb4fbSDima Dorfman 
5080c1bb4fbSDima Dorfman 	switch (sopt->sopt_dir) {
5090c1bb4fbSDima Dorfman 	case SOPT_GET:
5100c1bb4fbSDima Dorfman 		switch (sopt->sopt_name) {
5110c1bb4fbSDima Dorfman 		case LOCAL_PEERCRED:
5120d9ce3a1SRobert Watson 			error = 0;
5130d9ce3a1SRobert Watson 			UNP_LOCK();
5140c1bb4fbSDima Dorfman 			if (unp->unp_flags & UNP_HAVEPC)
5150d9ce3a1SRobert Watson 				xu = unp->unp_peercred;
5160c1bb4fbSDima Dorfman 			else {
5170c1bb4fbSDima Dorfman 				if (so->so_type == SOCK_STREAM)
5180c1bb4fbSDima Dorfman 					error = ENOTCONN;
5190c1bb4fbSDima Dorfman 				else
5200c1bb4fbSDima Dorfman 					error = EINVAL;
5210c1bb4fbSDima Dorfman 			}
5220d9ce3a1SRobert Watson 			UNP_UNLOCK();
5230d9ce3a1SRobert Watson 			if (error == 0)
5240d9ce3a1SRobert Watson 				error = sooptcopyout(sopt, &xu, sizeof(xu));
5250c1bb4fbSDima Dorfman 			break;
5260c1bb4fbSDima Dorfman 		default:
5270c1bb4fbSDima Dorfman 			error = EOPNOTSUPP;
5280c1bb4fbSDima Dorfman 			break;
5290c1bb4fbSDima Dorfman 		}
5300c1bb4fbSDima Dorfman 		break;
5310c1bb4fbSDima Dorfman 	case SOPT_SET:
5320c1bb4fbSDima Dorfman 	default:
5330c1bb4fbSDima Dorfman 		error = EOPNOTSUPP;
5340c1bb4fbSDima Dorfman 		break;
5350c1bb4fbSDima Dorfman 	}
5360c1bb4fbSDima Dorfman 	return (error);
5370c1bb4fbSDima Dorfman }
5380c1bb4fbSDima Dorfman 
539df8bae1dSRodney W. Grimes /*
540df8bae1dSRodney W. Grimes  * Both send and receive buffers are allocated PIPSIZ bytes of buffering
541df8bae1dSRodney W. Grimes  * for stream sockets, although the total for sender and receiver is
542df8bae1dSRodney W. Grimes  * actually only PIPSIZ.
543df8bae1dSRodney W. Grimes  * Datagram sockets really use the sendspace as the maximum datagram size,
544df8bae1dSRodney W. Grimes  * and don't really want to reserve the sendspace.  Their recvspace should
545df8bae1dSRodney W. Grimes  * be large enough for at least one max-size datagram plus address.
546df8bae1dSRodney W. Grimes  */
5475dce41c5SJohn Dyson #ifndef PIPSIZ
5485dce41c5SJohn Dyson #define	PIPSIZ	8192
5495dce41c5SJohn Dyson #endif
550f708ef1bSPoul-Henning Kamp static u_long	unpst_sendspace = PIPSIZ;
551f708ef1bSPoul-Henning Kamp static u_long	unpst_recvspace = PIPSIZ;
552f708ef1bSPoul-Henning Kamp static u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
553f708ef1bSPoul-Henning Kamp static u_long	unpdg_recvspace = 4*1024;
554df8bae1dSRodney W. Grimes 
555f708ef1bSPoul-Henning Kamp static int	unp_rights;			/* file descriptors in flight */
556df8bae1dSRodney W. Grimes 
557ce02431fSDoug Rabson SYSCTL_DECL(_net_local_stream);
558639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
559639acc13SGarrett Wollman 	   &unpst_sendspace, 0, "");
560639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
561639acc13SGarrett Wollman 	   &unpst_recvspace, 0, "");
562ce02431fSDoug Rabson SYSCTL_DECL(_net_local_dgram);
563639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
564639acc13SGarrett Wollman 	   &unpdg_sendspace, 0, "");
565639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
566639acc13SGarrett Wollman 	   &unpdg_recvspace, 0, "");
567ce02431fSDoug Rabson SYSCTL_DECL(_net_local);
568639acc13SGarrett Wollman SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, "");
569639acc13SGarrett Wollman 
570f708ef1bSPoul-Henning Kamp static int
571df8bae1dSRodney W. Grimes unp_attach(so)
572df8bae1dSRodney W. Grimes 	struct socket *so;
573df8bae1dSRodney W. Grimes {
574df8bae1dSRodney W. Grimes 	register struct unpcb *unp;
575df8bae1dSRodney W. Grimes 	int error;
576df8bae1dSRodney W. Grimes 
577df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
578df8bae1dSRodney W. Grimes 		switch (so->so_type) {
579df8bae1dSRodney W. Grimes 
580df8bae1dSRodney W. Grimes 		case SOCK_STREAM:
581df8bae1dSRodney W. Grimes 			error = soreserve(so, unpst_sendspace, unpst_recvspace);
582df8bae1dSRodney W. Grimes 			break;
583df8bae1dSRodney W. Grimes 
584df8bae1dSRodney W. Grimes 		case SOCK_DGRAM:
585df8bae1dSRodney W. Grimes 			error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
586df8bae1dSRodney W. Grimes 			break;
587df8bae1dSRodney W. Grimes 
588df8bae1dSRodney W. Grimes 		default:
589df8bae1dSRodney W. Grimes 			panic("unp_attach");
590df8bae1dSRodney W. Grimes 		}
591df8bae1dSRodney W. Grimes 		if (error)
592df8bae1dSRodney W. Grimes 			return (error);
593df8bae1dSRodney W. Grimes 	}
594a163d034SWarner Losh 	unp = uma_zalloc(unp_zone, M_WAITOK);
59557bf258eSGarrett Wollman 	if (unp == NULL)
596df8bae1dSRodney W. Grimes 		return (ENOBUFS);
59757bf258eSGarrett Wollman 	bzero(unp, sizeof *unp);
59898271db4SGarrett Wollman 	LIST_INIT(&unp->unp_refs);
599df8bae1dSRodney W. Grimes 	unp->unp_socket = so;
6000d9ce3a1SRobert Watson 
6010d9ce3a1SRobert Watson 	UNP_LOCK();
6020d9ce3a1SRobert Watson 	unp->unp_gencnt = ++unp_gencnt;
6030d9ce3a1SRobert Watson 	unp_count++;
60498271db4SGarrett Wollman 	LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead
60598271db4SGarrett Wollman 			 : &unp_shead, unp, unp_link);
6060d9ce3a1SRobert Watson 	UNP_UNLOCK();
6070d9ce3a1SRobert Watson 
608210a5a71SAlfred Perlstein 	so->so_pcb = unp;
609df8bae1dSRodney W. Grimes 	return (0);
610df8bae1dSRodney W. Grimes }
611df8bae1dSRodney W. Grimes 
612f708ef1bSPoul-Henning Kamp static void
613df8bae1dSRodney W. Grimes unp_detach(unp)
614df8bae1dSRodney W. Grimes 	register struct unpcb *unp;
615df8bae1dSRodney W. Grimes {
6160d9ce3a1SRobert Watson 	struct vnode *vp;
6170d9ce3a1SRobert Watson 
6180d9ce3a1SRobert Watson 	UNP_LOCK_ASSERT();
6190d9ce3a1SRobert Watson 
62098271db4SGarrett Wollman 	LIST_REMOVE(unp, unp_link);
62198271db4SGarrett Wollman 	unp->unp_gencnt = ++unp_gencnt;
62298271db4SGarrett Wollman 	--unp_count;
6230d9ce3a1SRobert Watson 	if ((vp = unp->unp_vnode) != NULL) {
6240d9ce3a1SRobert Watson 		/*
6250d9ce3a1SRobert Watson 		 * XXXRW: should v_socket be frobbed only while holding
6260d9ce3a1SRobert Watson 		 * Giant?
6270d9ce3a1SRobert Watson 		 */
628fc3fcacfSRobert Watson 		unp->unp_vnode->v_socket = NULL;
629fc3fcacfSRobert Watson 		unp->unp_vnode = NULL;
630df8bae1dSRodney W. Grimes 	}
631fc3fcacfSRobert Watson 	if (unp->unp_conn != NULL)
632df8bae1dSRodney W. Grimes 		unp_disconnect(unp);
6330d9ce3a1SRobert Watson 	while (!LIST_EMPTY(&unp->unp_refs)) {
6340d9ce3a1SRobert Watson 		struct unpcb *ref = LIST_FIRST(&unp->unp_refs);
6350d9ce3a1SRobert Watson 		unp_drop(ref, ECONNRESET);
6360d9ce3a1SRobert Watson 	}
637df8bae1dSRodney W. Grimes 	soisdisconnected(unp->unp_socket);
638fc3fcacfSRobert Watson 	unp->unp_socket->so_pcb = NULL;
639df8bae1dSRodney W. Grimes 	if (unp_rights) {
640df8bae1dSRodney W. Grimes 		/*
641df8bae1dSRodney W. Grimes 		 * Normally the receive buffer is flushed later,
642df8bae1dSRodney W. Grimes 		 * in sofree, but if our receive buffer holds references
643df8bae1dSRodney W. Grimes 		 * to descriptors that are now garbage, we will dispose
644df8bae1dSRodney W. Grimes 		 * of those descriptor references after the garbage collector
645df8bae1dSRodney W. Grimes 		 * gets them (resulting in a "panic: closef: count < 0").
646df8bae1dSRodney W. Grimes 		 */
647df8bae1dSRodney W. Grimes 		sorflush(unp->unp_socket);
648df8bae1dSRodney W. Grimes 		unp_gc();
649df8bae1dSRodney W. Grimes 	}
650fc3fcacfSRobert Watson 	if (unp->unp_addr != NULL)
65157bf258eSGarrett Wollman 		FREE(unp->unp_addr, M_SONAME);
6520d9ce3a1SRobert Watson 	UNP_UNLOCK();
6539e9d298aSJeff Roberson 	uma_zfree(unp_zone, unp);
6540d9ce3a1SRobert Watson 	if (vp) {
6550d9ce3a1SRobert Watson 		mtx_lock(&Giant);
6560d9ce3a1SRobert Watson 		vrele(vp);
6570d9ce3a1SRobert Watson 		mtx_unlock(&Giant);
6580d9ce3a1SRobert Watson 	}
659df8bae1dSRodney W. Grimes }
660df8bae1dSRodney W. Grimes 
661f708ef1bSPoul-Henning Kamp static int
662b40ce416SJulian Elischer unp_bind(unp, nam, td)
663df8bae1dSRodney W. Grimes 	struct unpcb *unp;
66457bf258eSGarrett Wollman 	struct sockaddr *nam;
665b40ce416SJulian Elischer 	struct thread *td;
666df8bae1dSRodney W. Grimes {
66757bf258eSGarrett Wollman 	struct sockaddr_un *soun = (struct sockaddr_un *)nam;
668f2a2857bSKirk McKusick 	struct vnode *vp;
669f2a2857bSKirk McKusick 	struct mount *mp;
670df8bae1dSRodney W. Grimes 	struct vattr vattr;
67157bf258eSGarrett Wollman 	int error, namelen;
672df8bae1dSRodney W. Grimes 	struct nameidata nd;
6738f364875SJulian Elischer 	char *buf;
674df8bae1dSRodney W. Grimes 
6750d9ce3a1SRobert Watson 	/*
6760d9ce3a1SRobert Watson 	 * XXXRW: This test-and-set of unp_vnode is non-atomic; the
6770d9ce3a1SRobert Watson 	 * unlocked read here is fine, but the value of unp_vnode needs
6780d9ce3a1SRobert Watson 	 * to be tested again after we do all the lookups to see if the
6790d9ce3a1SRobert Watson 	 * pcb is still unbound?
6800d9ce3a1SRobert Watson 	 */
681df8bae1dSRodney W. Grimes 	if (unp->unp_vnode != NULL)
682df8bae1dSRodney W. Grimes 		return (EINVAL);
68355c85568SRobert Drehmel 
68457bf258eSGarrett Wollman 	namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
68557bf258eSGarrett Wollman 	if (namelen <= 0)
686e5aeaa0cSDag-Erling Smørgrav 		return (EINVAL);
68755c85568SRobert Drehmel 
688a163d034SWarner Losh 	buf = malloc(namelen + 1, M_TEMP, M_WAITOK);
68955c85568SRobert Drehmel 	strlcpy(buf, soun->sun_path, namelen + 1);
69055c85568SRobert Drehmel 
6910d9ce3a1SRobert Watson 	mtx_lock(&Giant);
692f2a2857bSKirk McKusick restart:
6930d9ce3a1SRobert Watson 	mtx_assert(&Giant, MA_OWNED);
694b65f6f6bSRobert Watson 	NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME, UIO_SYSSPACE,
695b40ce416SJulian Elischer 	    buf, td);
696df8bae1dSRodney W. Grimes /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
697797f2d22SPoul-Henning Kamp 	error = namei(&nd);
6980d9ce3a1SRobert Watson 	if (error)
6990d9ce3a1SRobert Watson 		goto done;
700df8bae1dSRodney W. Grimes 	vp = nd.ni_vp;
701f2a2857bSKirk McKusick 	if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
702762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
703df8bae1dSRodney W. Grimes 		if (nd.ni_dvp == vp)
704df8bae1dSRodney W. Grimes 			vrele(nd.ni_dvp);
705df8bae1dSRodney W. Grimes 		else
706df8bae1dSRodney W. Grimes 			vput(nd.ni_dvp);
707f2a2857bSKirk McKusick 		if (vp != NULL) {
708df8bae1dSRodney W. Grimes 			vrele(vp);
7090d9ce3a1SRobert Watson 			error = EADDRINUSE;
7100d9ce3a1SRobert Watson 			goto done;
711df8bae1dSRodney W. Grimes 		}
7128f364875SJulian Elischer 		error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
7130d9ce3a1SRobert Watson 		if (error)
7140d9ce3a1SRobert Watson 			goto done;
715f2a2857bSKirk McKusick 		goto restart;
716f2a2857bSKirk McKusick 	}
717df8bae1dSRodney W. Grimes 	VATTR_NULL(&vattr);
718df8bae1dSRodney W. Grimes 	vattr.va_type = VSOCK;
719b40ce416SJulian Elischer 	vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask);
7206ea48a90SRobert Watson #ifdef MAC
7216ea48a90SRobert Watson 	error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
7226ea48a90SRobert Watson 	    &vattr);
7236151efaaSRobert Watson #endif
7246ea48a90SRobert Watson 	if (error == 0) {
725a854ed98SJohn Baldwin 		VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
7267be2d300SMike Smith 		error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
7276ea48a90SRobert Watson 	}
728762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
7297be2d300SMike Smith 	vput(nd.ni_dvp);
7300d9ce3a1SRobert Watson 	if (error)
7310d9ce3a1SRobert Watson 		goto done;
732df8bae1dSRodney W. Grimes 	vp = nd.ni_vp;
7330d9ce3a1SRobert Watson 	ASSERT_VOP_LOCKED(vp, "unp_bind");
7340d9ce3a1SRobert Watson 	soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK);
7350d9ce3a1SRobert Watson 	UNP_LOCK();
736df8bae1dSRodney W. Grimes 	vp->v_socket = unp->unp_socket;
737df8bae1dSRodney W. Grimes 	unp->unp_vnode = vp;
7380d9ce3a1SRobert Watson 	unp->unp_addr = soun;
7390d9ce3a1SRobert Watson 	UNP_UNLOCK();
740b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
741f2a2857bSKirk McKusick 	vn_finished_write(mp);
7420d9ce3a1SRobert Watson done:
7430d9ce3a1SRobert Watson 	mtx_unlock(&Giant);
7448f364875SJulian Elischer 	free(buf, M_TEMP);
7450d9ce3a1SRobert Watson 	return (error);
746df8bae1dSRodney W. Grimes }
747df8bae1dSRodney W. Grimes 
748f708ef1bSPoul-Henning Kamp static int
749b40ce416SJulian Elischer unp_connect(so, nam, td)
750df8bae1dSRodney W. Grimes 	struct socket *so;
75157bf258eSGarrett Wollman 	struct sockaddr *nam;
752b40ce416SJulian Elischer 	struct thread *td;
753df8bae1dSRodney W. Grimes {
75457bf258eSGarrett Wollman 	register struct sockaddr_un *soun = (struct sockaddr_un *)nam;
755df8bae1dSRodney W. Grimes 	register struct vnode *vp;
756df8bae1dSRodney W. Grimes 	register struct socket *so2, *so3;
7570d9ce3a1SRobert Watson 	struct unpcb *unp = sotounpcb(so);
7580d9ce3a1SRobert Watson 	struct unpcb *unp2, *unp3;
75957bf258eSGarrett Wollman 	int error, len;
760df8bae1dSRodney W. Grimes 	struct nameidata nd;
76157bf258eSGarrett Wollman 	char buf[SOCK_MAXADDRLEN];
7620d9ce3a1SRobert Watson 	struct sockaddr *sa;
7630d9ce3a1SRobert Watson 
7640d9ce3a1SRobert Watson 	UNP_LOCK_ASSERT();
765df8bae1dSRodney W. Grimes 
76657bf258eSGarrett Wollman 	len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
76757bf258eSGarrett Wollman 	if (len <= 0)
768e5aeaa0cSDag-Erling Smørgrav 		return (EINVAL);
76955c85568SRobert Drehmel 	strlcpy(buf, soun->sun_path, len + 1);
7700d9ce3a1SRobert Watson 	UNP_UNLOCK();
7710d9ce3a1SRobert Watson 	sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
7720d9ce3a1SRobert Watson 	mtx_lock(&Giant);
773b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td);
774797f2d22SPoul-Henning Kamp 	error = namei(&nd);
775797f2d22SPoul-Henning Kamp 	if (error)
7760d9ce3a1SRobert Watson 		vp = NULL;
7770d9ce3a1SRobert Watson 	else
778df8bae1dSRodney W. Grimes 		vp = nd.ni_vp;
7790d9ce3a1SRobert Watson 	ASSERT_VOP_LOCKED(vp, "unp_connect");
780762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
7810d9ce3a1SRobert Watson 	if (error)
7820d9ce3a1SRobert Watson 		goto bad;
7830d9ce3a1SRobert Watson 
784df8bae1dSRodney W. Grimes 	if (vp->v_type != VSOCK) {
785df8bae1dSRodney W. Grimes 		error = ENOTSOCK;
786df8bae1dSRodney W. Grimes 		goto bad;
787df8bae1dSRodney W. Grimes 	}
788a854ed98SJohn Baldwin 	error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td);
789797f2d22SPoul-Henning Kamp 	if (error)
790df8bae1dSRodney W. Grimes 		goto bad;
791df8bae1dSRodney W. Grimes 	so2 = vp->v_socket;
792fc3fcacfSRobert Watson 	if (so2 == NULL) {
793df8bae1dSRodney W. Grimes 		error = ECONNREFUSED;
794df8bae1dSRodney W. Grimes 		goto bad;
795df8bae1dSRodney W. Grimes 	}
796df8bae1dSRodney W. Grimes 	if (so->so_type != so2->so_type) {
797df8bae1dSRodney W. Grimes 		error = EPROTOTYPE;
798df8bae1dSRodney W. Grimes 		goto bad;
799df8bae1dSRodney W. Grimes 	}
8000d9ce3a1SRobert Watson 	mtx_unlock(&Giant);
8010d9ce3a1SRobert Watson 	UNP_LOCK();
802df8bae1dSRodney W. Grimes 	if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
8030d9ce3a1SRobert Watson 		if (so2->so_options & SO_ACCEPTCONN) {
8040d9ce3a1SRobert Watson 			/*
8050d9ce3a1SRobert Watson 			 * NB: drop locks here so unp_attach is entered
8060d9ce3a1SRobert Watson 			 *     w/o locks; this avoids a recursive lock
8070d9ce3a1SRobert Watson 			 *     of the head and holding sleep locks across
8080d9ce3a1SRobert Watson 			 *     a (potentially) blocking malloc.
8090d9ce3a1SRobert Watson 			 */
8100d9ce3a1SRobert Watson 			UNP_UNLOCK();
8110d9ce3a1SRobert Watson 			so3 = sonewconn(so2, 0);
8120d9ce3a1SRobert Watson 			UNP_LOCK();
8130d9ce3a1SRobert Watson 		} else
8140d9ce3a1SRobert Watson 			so3 = NULL;
8150d9ce3a1SRobert Watson 		if (so3 == NULL) {
816df8bae1dSRodney W. Grimes 			error = ECONNREFUSED;
8170d9ce3a1SRobert Watson 			goto bad2;
818df8bae1dSRodney W. Grimes 		}
8190c1bb4fbSDima Dorfman 		unp = sotounpcb(so);
820df8bae1dSRodney W. Grimes 		unp2 = sotounpcb(so2);
821df8bae1dSRodney W. Grimes 		unp3 = sotounpcb(so3);
8220d9ce3a1SRobert Watson 		if (unp2->unp_addr != NULL) {
8230d9ce3a1SRobert Watson 			bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len);
8240d9ce3a1SRobert Watson 			unp3->unp_addr = (struct sockaddr_un *) sa;
8250d9ce3a1SRobert Watson 			sa = NULL;
8260d9ce3a1SRobert Watson 		}
8270c1bb4fbSDima Dorfman 		/*
8280c1bb4fbSDima Dorfman 		 * unp_peercred management:
8290c1bb4fbSDima Dorfman 		 *
8300c1bb4fbSDima Dorfman 		 * The connecter's (client's) credentials are copied
8310c1bb4fbSDima Dorfman 		 * from its process structure at the time of connect()
8320c1bb4fbSDima Dorfman 		 * (which is now).
8330c1bb4fbSDima Dorfman 		 */
834a854ed98SJohn Baldwin 		cru2x(td->td_ucred, &unp3->unp_peercred);
8350c1bb4fbSDima Dorfman 		unp3->unp_flags |= UNP_HAVEPC;
8360c1bb4fbSDima Dorfman 		/*
8370c1bb4fbSDima Dorfman 		 * The receiver's (server's) credentials are copied
8380c1bb4fbSDima Dorfman 		 * from the unp_peercred member of socket on which the
8390c1bb4fbSDima Dorfman 		 * former called listen(); unp_listen() cached that
8400c1bb4fbSDima Dorfman 		 * process's credentials at that time so we can use
8410c1bb4fbSDima Dorfman 		 * them now.
8420c1bb4fbSDima Dorfman 		 */
8430c1bb4fbSDima Dorfman 		KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED,
8440c1bb4fbSDima Dorfman 		    ("unp_connect: listener without cached peercred"));
8450c1bb4fbSDima Dorfman 		memcpy(&unp->unp_peercred, &unp2->unp_peercred,
8460c1bb4fbSDima Dorfman 		    sizeof(unp->unp_peercred));
8470c1bb4fbSDima Dorfman 		unp->unp_flags |= UNP_HAVEPC;
848335654d7SRobert Watson #ifdef MAC
849310e7cebSRobert Watson 		SOCK_LOCK(so);
850335654d7SRobert Watson 		mac_set_socket_peer_from_socket(so, so3);
851335654d7SRobert Watson 		mac_set_socket_peer_from_socket(so3, so);
852310e7cebSRobert Watson 		SOCK_UNLOCK(so);
853335654d7SRobert Watson #endif
8540c1bb4fbSDima Dorfman 
855df8bae1dSRodney W. Grimes 		so2 = so3;
856df8bae1dSRodney W. Grimes 	}
857df8bae1dSRodney W. Grimes 	error = unp_connect2(so, so2);
8580d9ce3a1SRobert Watson bad2:
8590d9ce3a1SRobert Watson 	UNP_UNLOCK();
8600d9ce3a1SRobert Watson 	mtx_lock(&Giant);
861df8bae1dSRodney W. Grimes bad:
8620d9ce3a1SRobert Watson 	mtx_assert(&Giant, MA_OWNED);
8630d9ce3a1SRobert Watson 	if (vp != NULL)
864df8bae1dSRodney W. Grimes 		vput(vp);
8650d9ce3a1SRobert Watson 	mtx_unlock(&Giant);
8660d9ce3a1SRobert Watson 	free(sa, M_SONAME);
8670d9ce3a1SRobert Watson 	UNP_LOCK();
868df8bae1dSRodney W. Grimes 	return (error);
869df8bae1dSRodney W. Grimes }
870df8bae1dSRodney W. Grimes 
871db48c0d2SRobert Watson static int
872df8bae1dSRodney W. Grimes unp_connect2(so, so2)
873df8bae1dSRodney W. Grimes 	register struct socket *so;
874df8bae1dSRodney W. Grimes 	register struct socket *so2;
875df8bae1dSRodney W. Grimes {
876df8bae1dSRodney W. Grimes 	register struct unpcb *unp = sotounpcb(so);
877df8bae1dSRodney W. Grimes 	register struct unpcb *unp2;
878df8bae1dSRodney W. Grimes 
8790d9ce3a1SRobert Watson 	UNP_LOCK_ASSERT();
8800d9ce3a1SRobert Watson 
881df8bae1dSRodney W. Grimes 	if (so2->so_type != so->so_type)
882df8bae1dSRodney W. Grimes 		return (EPROTOTYPE);
883df8bae1dSRodney W. Grimes 	unp2 = sotounpcb(so2);
884df8bae1dSRodney W. Grimes 	unp->unp_conn = unp2;
885df8bae1dSRodney W. Grimes 	switch (so->so_type) {
886df8bae1dSRodney W. Grimes 
887df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
88898271db4SGarrett Wollman 		LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
889df8bae1dSRodney W. Grimes 		soisconnected(so);
890df8bae1dSRodney W. Grimes 		break;
891df8bae1dSRodney W. Grimes 
892df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
893df8bae1dSRodney W. Grimes 		unp2->unp_conn = unp;
894df8bae1dSRodney W. Grimes 		soisconnected(so);
895df8bae1dSRodney W. Grimes 		soisconnected(so2);
896df8bae1dSRodney W. Grimes 		break;
897df8bae1dSRodney W. Grimes 
898df8bae1dSRodney W. Grimes 	default:
899df8bae1dSRodney W. Grimes 		panic("unp_connect2");
900df8bae1dSRodney W. Grimes 	}
901df8bae1dSRodney W. Grimes 	return (0);
902df8bae1dSRodney W. Grimes }
903df8bae1dSRodney W. Grimes 
904f708ef1bSPoul-Henning Kamp static void
905df8bae1dSRodney W. Grimes unp_disconnect(unp)
906df8bae1dSRodney W. Grimes 	struct unpcb *unp;
907df8bae1dSRodney W. Grimes {
908df8bae1dSRodney W. Grimes 	register struct unpcb *unp2 = unp->unp_conn;
9091b2e3b4bSRobert Watson 	struct socket *so;
910df8bae1dSRodney W. Grimes 
9110d9ce3a1SRobert Watson 	UNP_LOCK_ASSERT();
9120d9ce3a1SRobert Watson 
913fc3fcacfSRobert Watson 	if (unp2 == NULL)
914df8bae1dSRodney W. Grimes 		return;
915fc3fcacfSRobert Watson 	unp->unp_conn = NULL;
916df8bae1dSRodney W. Grimes 	switch (unp->unp_socket->so_type) {
917df8bae1dSRodney W. Grimes 
918df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
91998271db4SGarrett Wollman 		LIST_REMOVE(unp, unp_reflink);
9201b2e3b4bSRobert Watson 		so = unp->unp_socket;
9211b2e3b4bSRobert Watson 		SOCK_LOCK(so);
9221b2e3b4bSRobert Watson 		so->so_state &= ~SS_ISCONNECTED;
9231b2e3b4bSRobert Watson 		SOCK_UNLOCK(so);
924df8bae1dSRodney W. Grimes 		break;
925df8bae1dSRodney W. Grimes 
926df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
927df8bae1dSRodney W. Grimes 		soisdisconnected(unp->unp_socket);
928fc3fcacfSRobert Watson 		unp2->unp_conn = NULL;
929df8bae1dSRodney W. Grimes 		soisdisconnected(unp2->unp_socket);
930df8bae1dSRodney W. Grimes 		break;
931df8bae1dSRodney W. Grimes 	}
932df8bae1dSRodney W. Grimes }
933df8bae1dSRodney W. Grimes 
934df8bae1dSRodney W. Grimes #ifdef notdef
93526f9a767SRodney W. Grimes void
936df8bae1dSRodney W. Grimes unp_abort(unp)
937df8bae1dSRodney W. Grimes 	struct unpcb *unp;
938df8bae1dSRodney W. Grimes {
939df8bae1dSRodney W. Grimes 
940df8bae1dSRodney W. Grimes 	unp_detach(unp);
941df8bae1dSRodney W. Grimes }
942df8bae1dSRodney W. Grimes #endif
943df8bae1dSRodney W. Grimes 
9440d9ce3a1SRobert Watson /*
9450d9ce3a1SRobert Watson  * unp_pcblist() assumes that UNIX domain socket memory is never reclaimed
9460d9ce3a1SRobert Watson  * by the zone (UMA_ZONE_NOFREE), and as such potentially stale pointers
9470d9ce3a1SRobert Watson  * are safe to reference.  It first scans the list of struct unpcb's to
9480d9ce3a1SRobert Watson  * generate a pointer list, then it rescans its list one entry at a time to
9490d9ce3a1SRobert Watson  * externalize and copyout.  It checks the generation number to see if a
9500d9ce3a1SRobert Watson  * struct unpcb has been reused, and will skip it if so.
9510d9ce3a1SRobert Watson  */
95298271db4SGarrett Wollman static int
95382d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS)
95498271db4SGarrett Wollman {
955f5ef029eSPoul-Henning Kamp 	int error, i, n;
95698271db4SGarrett Wollman 	struct unpcb *unp, **unp_list;
95798271db4SGarrett Wollman 	unp_gen_t gencnt;
9588f364875SJulian Elischer 	struct xunpgen *xug;
95998271db4SGarrett Wollman 	struct unp_head *head;
9608f364875SJulian Elischer 	struct xunpcb *xu;
96198271db4SGarrett Wollman 
962a23d65bfSBruce Evans 	head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead);
96398271db4SGarrett Wollman 
96498271db4SGarrett Wollman 	/*
96598271db4SGarrett Wollman 	 * The process of preparing the PCB list is too time-consuming and
96698271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
96798271db4SGarrett Wollman 	 */
968fc3fcacfSRobert Watson 	if (req->oldptr == NULL) {
96998271db4SGarrett Wollman 		n = unp_count;
9708f364875SJulian Elischer 		req->oldidx = 2 * (sizeof *xug)
97198271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xunpcb);
972e5aeaa0cSDag-Erling Smørgrav 		return (0);
97398271db4SGarrett Wollman 	}
97498271db4SGarrett Wollman 
975fc3fcacfSRobert Watson 	if (req->newptr != NULL)
976e5aeaa0cSDag-Erling Smørgrav 		return (EPERM);
97798271db4SGarrett Wollman 
97898271db4SGarrett Wollman 	/*
97998271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
98098271db4SGarrett Wollman 	 */
981a163d034SWarner Losh 	xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK);
9820d9ce3a1SRobert Watson 	UNP_LOCK();
98398271db4SGarrett Wollman 	gencnt = unp_gencnt;
98498271db4SGarrett Wollman 	n = unp_count;
9850d9ce3a1SRobert Watson 	UNP_UNLOCK();
98698271db4SGarrett Wollman 
9878f364875SJulian Elischer 	xug->xug_len = sizeof *xug;
9888f364875SJulian Elischer 	xug->xug_count = n;
9898f364875SJulian Elischer 	xug->xug_gen = gencnt;
9908f364875SJulian Elischer 	xug->xug_sogen = so_gencnt;
9918f364875SJulian Elischer 	error = SYSCTL_OUT(req, xug, sizeof *xug);
9928f364875SJulian Elischer 	if (error) {
9938f364875SJulian Elischer 		free(xug, M_TEMP);
994e5aeaa0cSDag-Erling Smørgrav 		return (error);
9958f364875SJulian Elischer 	}
99698271db4SGarrett Wollman 
997a163d034SWarner Losh 	unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
99898271db4SGarrett Wollman 
9990d9ce3a1SRobert Watson 	UNP_LOCK();
10002e3c8fcbSPoul-Henning Kamp 	for (unp = LIST_FIRST(head), i = 0; unp && i < n;
10012e3c8fcbSPoul-Henning Kamp 	     unp = LIST_NEXT(unp, unp_link)) {
10028a7d8cc6SRobert Watson 		if (unp->unp_gencnt <= gencnt) {
1003a854ed98SJohn Baldwin 			if (cr_cansee(req->td->td_ucred,
10048a7d8cc6SRobert Watson 			    unp->unp_socket->so_cred))
10054787fd37SPaul Saab 				continue;
100698271db4SGarrett Wollman 			unp_list[i++] = unp;
100798271db4SGarrett Wollman 		}
10084787fd37SPaul Saab 	}
10090d9ce3a1SRobert Watson 	UNP_UNLOCK();
101098271db4SGarrett Wollman 	n = i;			/* in case we lost some during malloc */
101198271db4SGarrett Wollman 
101298271db4SGarrett Wollman 	error = 0;
1013a163d034SWarner Losh 	xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK);
101498271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
101598271db4SGarrett Wollman 		unp = unp_list[i];
101698271db4SGarrett Wollman 		if (unp->unp_gencnt <= gencnt) {
10178f364875SJulian Elischer 			xu->xu_len = sizeof *xu;
10188f364875SJulian Elischer 			xu->xu_unpp = unp;
101998271db4SGarrett Wollman 			/*
102098271db4SGarrett Wollman 			 * XXX - need more locking here to protect against
102198271db4SGarrett Wollman 			 * connect/disconnect races for SMP.
102298271db4SGarrett Wollman 			 */
1023fc3fcacfSRobert Watson 			if (unp->unp_addr != NULL)
10248f364875SJulian Elischer 				bcopy(unp->unp_addr, &xu->xu_addr,
102598271db4SGarrett Wollman 				      unp->unp_addr->sun_len);
1026fc3fcacfSRobert Watson 			if (unp->unp_conn != NULL &&
1027fc3fcacfSRobert Watson 			    unp->unp_conn->unp_addr != NULL)
102898271db4SGarrett Wollman 				bcopy(unp->unp_conn->unp_addr,
10298f364875SJulian Elischer 				      &xu->xu_caddr,
103098271db4SGarrett Wollman 				      unp->unp_conn->unp_addr->sun_len);
10318f364875SJulian Elischer 			bcopy(unp, &xu->xu_unp, sizeof *unp);
10328f364875SJulian Elischer 			sotoxsocket(unp->unp_socket, &xu->xu_socket);
10338f364875SJulian Elischer 			error = SYSCTL_OUT(req, xu, sizeof *xu);
103498271db4SGarrett Wollman 		}
103598271db4SGarrett Wollman 	}
10368f364875SJulian Elischer 	free(xu, M_TEMP);
103798271db4SGarrett Wollman 	if (!error) {
103898271db4SGarrett Wollman 		/*
103998271db4SGarrett Wollman 		 * Give the user an updated idea of our state.
104098271db4SGarrett Wollman 		 * If the generation differs from what we told
104198271db4SGarrett Wollman 		 * her before, she knows that something happened
104298271db4SGarrett Wollman 		 * while we were processing this request, and it
104398271db4SGarrett Wollman 		 * might be necessary to retry.
104498271db4SGarrett Wollman 		 */
10458f364875SJulian Elischer 		xug->xug_gen = unp_gencnt;
10468f364875SJulian Elischer 		xug->xug_sogen = so_gencnt;
10478f364875SJulian Elischer 		xug->xug_count = unp_count;
10488f364875SJulian Elischer 		error = SYSCTL_OUT(req, xug, sizeof *xug);
104998271db4SGarrett Wollman 	}
105098271db4SGarrett Wollman 	free(unp_list, M_TEMP);
10518f364875SJulian Elischer 	free(xug, M_TEMP);
1052e5aeaa0cSDag-Erling Smørgrav 	return (error);
105398271db4SGarrett Wollman }
105498271db4SGarrett Wollman 
105598271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD,
105698271db4SGarrett Wollman 	    (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
105798271db4SGarrett Wollman 	    "List of active local datagram sockets");
105898271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD,
105998271db4SGarrett Wollman 	    (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
106098271db4SGarrett Wollman 	    "List of active local stream sockets");
106198271db4SGarrett Wollman 
1062f708ef1bSPoul-Henning Kamp static void
1063df8bae1dSRodney W. Grimes unp_shutdown(unp)
1064df8bae1dSRodney W. Grimes 	struct unpcb *unp;
1065df8bae1dSRodney W. Grimes {
1066df8bae1dSRodney W. Grimes 	struct socket *so;
1067df8bae1dSRodney W. Grimes 
10680d9ce3a1SRobert Watson 	UNP_LOCK_ASSERT();
10690d9ce3a1SRobert Watson 
1070df8bae1dSRodney W. Grimes 	if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
1071df8bae1dSRodney W. Grimes 	    (so = unp->unp_conn->unp_socket))
1072df8bae1dSRodney W. Grimes 		socantrcvmore(so);
1073df8bae1dSRodney W. Grimes }
1074df8bae1dSRodney W. Grimes 
1075f708ef1bSPoul-Henning Kamp static void
1076df8bae1dSRodney W. Grimes unp_drop(unp, errno)
1077df8bae1dSRodney W. Grimes 	struct unpcb *unp;
1078df8bae1dSRodney W. Grimes 	int errno;
1079df8bae1dSRodney W. Grimes {
1080df8bae1dSRodney W. Grimes 	struct socket *so = unp->unp_socket;
1081df8bae1dSRodney W. Grimes 
10820d9ce3a1SRobert Watson 	UNP_LOCK_ASSERT();
10830d9ce3a1SRobert Watson 
1084df8bae1dSRodney W. Grimes 	so->so_error = errno;
1085df8bae1dSRodney W. Grimes 	unp_disconnect(unp);
1086df8bae1dSRodney W. Grimes }
1087df8bae1dSRodney W. Grimes 
1088df8bae1dSRodney W. Grimes #ifdef notdef
108926f9a767SRodney W. Grimes void
1090df8bae1dSRodney W. Grimes unp_drain()
1091df8bae1dSRodney W. Grimes {
1092df8bae1dSRodney W. Grimes 
1093df8bae1dSRodney W. Grimes }
1094df8bae1dSRodney W. Grimes #endif
1095df8bae1dSRodney W. Grimes 
10962bc21ed9SDavid Malone static void
10972bc21ed9SDavid Malone unp_freerights(rp, fdcount)
10982bc21ed9SDavid Malone 	struct file **rp;
10992bc21ed9SDavid Malone 	int fdcount;
1100df8bae1dSRodney W. Grimes {
11012bc21ed9SDavid Malone 	int i;
11022bc21ed9SDavid Malone 	struct file *fp;
1103df8bae1dSRodney W. Grimes 
11042bc21ed9SDavid Malone 	for (i = 0; i < fdcount; i++) {
1105df8bae1dSRodney W. Grimes 		fp = *rp;
11068692c025SYoshinobu Inoue 		/*
11072bc21ed9SDavid Malone 		 * zero the pointer before calling
11082bc21ed9SDavid Malone 		 * unp_discard since it may end up
11092bc21ed9SDavid Malone 		 * in unp_gc()..
11108692c025SYoshinobu Inoue 		 */
1111df8bae1dSRodney W. Grimes 		*rp++ = 0;
11128692c025SYoshinobu Inoue 		unp_discard(fp);
1113df8bae1dSRodney W. Grimes 	}
11142bc21ed9SDavid Malone }
11152bc21ed9SDavid Malone 
11162bc21ed9SDavid Malone int
11172bc21ed9SDavid Malone unp_externalize(control, controlp)
11182bc21ed9SDavid Malone 	struct mbuf *control, **controlp;
11192bc21ed9SDavid Malone {
11202bc21ed9SDavid Malone 	struct thread *td = curthread;		/* XXX */
11212bc21ed9SDavid Malone 	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
11222bc21ed9SDavid Malone 	int i;
11232bc21ed9SDavid Malone 	int *fdp;
11242bc21ed9SDavid Malone 	struct file **rp;
11252bc21ed9SDavid Malone 	struct file *fp;
11262bc21ed9SDavid Malone 	void *data;
11272bc21ed9SDavid Malone 	socklen_t clen = control->m_len, datalen;
11282bc21ed9SDavid Malone 	int error, newfds;
11292bc21ed9SDavid Malone 	int f;
11302bc21ed9SDavid Malone 	u_int newlen;
11312bc21ed9SDavid Malone 
11322bc21ed9SDavid Malone 	error = 0;
11332bc21ed9SDavid Malone 	if (controlp != NULL) /* controlp == NULL => free control messages */
11342bc21ed9SDavid Malone 		*controlp = NULL;
11352bc21ed9SDavid Malone 
11362bc21ed9SDavid Malone 	while (cm != NULL) {
11372bc21ed9SDavid Malone 		if (sizeof(*cm) > clen || cm->cmsg_len > clen) {
11382bc21ed9SDavid Malone 			error = EINVAL;
11392bc21ed9SDavid Malone 			break;
11402bc21ed9SDavid Malone 		}
11412bc21ed9SDavid Malone 
11422bc21ed9SDavid Malone 		data = CMSG_DATA(cm);
11432bc21ed9SDavid Malone 		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
11442bc21ed9SDavid Malone 
11452bc21ed9SDavid Malone 		if (cm->cmsg_level == SOL_SOCKET
11462bc21ed9SDavid Malone 		    && cm->cmsg_type == SCM_RIGHTS) {
11472bc21ed9SDavid Malone 			newfds = datalen / sizeof(struct file *);
11482bc21ed9SDavid Malone 			rp = data;
11492bc21ed9SDavid Malone 
1150e2f9a08bSOlivier Houchard 			/* If we're not outputting the descriptors free them. */
11512bc21ed9SDavid Malone 			if (error || controlp == NULL) {
11522bc21ed9SDavid Malone 				unp_freerights(rp, newfds);
11532bc21ed9SDavid Malone 				goto next;
11542bc21ed9SDavid Malone 			}
1155426da3bcSAlfred Perlstein 			FILEDESC_LOCK(td->td_proc->p_fd);
11562bc21ed9SDavid Malone 			/* if the new FD's will not fit free them.  */
11572bc21ed9SDavid Malone 			if (!fdavail(td, newfds)) {
1158426da3bcSAlfred Perlstein 				FILEDESC_UNLOCK(td->td_proc->p_fd);
11592bc21ed9SDavid Malone 				error = EMSGSIZE;
11602bc21ed9SDavid Malone 				unp_freerights(rp, newfds);
11612bc21ed9SDavid Malone 				goto next;
1162df8bae1dSRodney W. Grimes 			}
1163ed5b7817SJulian Elischer 			/*
11642bc21ed9SDavid Malone 			 * now change each pointer to an fd in the global
11652bc21ed9SDavid Malone 			 * table to an integer that is the index to the
11662bc21ed9SDavid Malone 			 * local fd table entry that we set up to point
11672bc21ed9SDavid Malone 			 * to the global one we are transferring.
1168ed5b7817SJulian Elischer 			 */
11692bc21ed9SDavid Malone 			newlen = newfds * sizeof(int);
11702bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, newlen,
11712bc21ed9SDavid Malone 			    SCM_RIGHTS, SOL_SOCKET);
11722bc21ed9SDavid Malone 			if (*controlp == NULL) {
1173426da3bcSAlfred Perlstein 				FILEDESC_UNLOCK(td->td_proc->p_fd);
11742bc21ed9SDavid Malone 				error = E2BIG;
11752bc21ed9SDavid Malone 				unp_freerights(rp, newfds);
11762bc21ed9SDavid Malone 				goto next;
11772bc21ed9SDavid Malone 			}
11782bc21ed9SDavid Malone 
11792bc21ed9SDavid Malone 			fdp = (int *)
11802bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1181df8bae1dSRodney W. Grimes 			for (i = 0; i < newfds; i++) {
1182a6d4491cSDag-Erling Smørgrav 				if (fdalloc(td, 0, &f))
11832bc21ed9SDavid Malone 					panic("unp_externalize fdalloc failed");
11848692c025SYoshinobu Inoue 				fp = *rp++;
1185b40ce416SJulian Elischer 				td->td_proc->p_fd->fd_ofiles[f] = fp;
1186426da3bcSAlfred Perlstein 				FILE_LOCK(fp);
1187df8bae1dSRodney W. Grimes 				fp->f_msgcount--;
1188426da3bcSAlfred Perlstein 				FILE_UNLOCK(fp);
1189df8bae1dSRodney W. Grimes 				unp_rights--;
11908692c025SYoshinobu Inoue 				*fdp++ = f;
1191df8bae1dSRodney W. Grimes 			}
1192426da3bcSAlfred Perlstein 			FILEDESC_UNLOCK(td->td_proc->p_fd);
11932bc21ed9SDavid Malone 		} else { /* We can just copy anything else across */
11942bc21ed9SDavid Malone 			if (error || controlp == NULL)
11952bc21ed9SDavid Malone 				goto next;
11962bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, datalen,
11972bc21ed9SDavid Malone 			    cm->cmsg_type, cm->cmsg_level);
11982bc21ed9SDavid Malone 			if (*controlp == NULL) {
11992bc21ed9SDavid Malone 				error = ENOBUFS;
12002bc21ed9SDavid Malone 				goto next;
12012bc21ed9SDavid Malone 			}
12022bc21ed9SDavid Malone 			bcopy(data,
12032bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *)),
12042bc21ed9SDavid Malone 			    datalen);
12052bc21ed9SDavid Malone 		}
12062bc21ed9SDavid Malone 
12072bc21ed9SDavid Malone 		controlp = &(*controlp)->m_next;
12082bc21ed9SDavid Malone 
12092bc21ed9SDavid Malone next:
12102bc21ed9SDavid Malone 		if (CMSG_SPACE(datalen) < clen) {
12112bc21ed9SDavid Malone 			clen -= CMSG_SPACE(datalen);
12122bc21ed9SDavid Malone 			cm = (struct cmsghdr *)
12132bc21ed9SDavid Malone 			    ((caddr_t)cm + CMSG_SPACE(datalen));
12148692c025SYoshinobu Inoue 		} else {
12152bc21ed9SDavid Malone 			clen = 0;
12162bc21ed9SDavid Malone 			cm = NULL;
12178692c025SYoshinobu Inoue 		}
12188692c025SYoshinobu Inoue 	}
12198692c025SYoshinobu Inoue 
12202bc21ed9SDavid Malone 	m_freem(control);
12212bc21ed9SDavid Malone 
12222bc21ed9SDavid Malone 	return (error);
1223df8bae1dSRodney W. Grimes }
1224df8bae1dSRodney W. Grimes 
122598271db4SGarrett Wollman void
122698271db4SGarrett Wollman unp_init(void)
122798271db4SGarrett Wollman {
12289e9d298aSJeff Roberson 	unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL,
12299e9d298aSJeff Roberson 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1230fc3fcacfSRobert Watson 	if (unp_zone == NULL)
123198271db4SGarrett Wollman 		panic("unp_init");
1232b17dd2bcSColin Percival 	uma_zone_set_max(unp_zone, nmbclusters);
123398271db4SGarrett Wollman 	LIST_INIT(&unp_dhead);
123498271db4SGarrett Wollman 	LIST_INIT(&unp_shead);
12350d9ce3a1SRobert Watson 
12360d9ce3a1SRobert Watson 	UNP_LOCK_INIT();
123798271db4SGarrett Wollman }
123898271db4SGarrett Wollman 
1239f708ef1bSPoul-Henning Kamp static int
12402bc21ed9SDavid Malone unp_internalize(controlp, td)
12412bc21ed9SDavid Malone 	struct mbuf **controlp;
1242b40ce416SJulian Elischer 	struct thread *td;
1243df8bae1dSRodney W. Grimes {
12442bc21ed9SDavid Malone 	struct mbuf *control = *controlp;
1245b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
12468692c025SYoshinobu Inoue 	struct filedesc *fdescp = p->p_fd;
12472bc21ed9SDavid Malone 	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
12482bc21ed9SDavid Malone 	struct cmsgcred *cmcred;
12492bc21ed9SDavid Malone 	struct file **rp;
12502bc21ed9SDavid Malone 	struct file *fp;
12512bc21ed9SDavid Malone 	struct timeval *tv;
12522bc21ed9SDavid Malone 	int i, fd, *fdp;
12532bc21ed9SDavid Malone 	void *data;
12542bc21ed9SDavid Malone 	socklen_t clen = control->m_len, datalen;
12552bc21ed9SDavid Malone 	int error, oldfds;
12568692c025SYoshinobu Inoue 	u_int newlen;
1257df8bae1dSRodney W. Grimes 
12582bc21ed9SDavid Malone 	error = 0;
12592bc21ed9SDavid Malone 	*controlp = NULL;
12600b788fa1SBill Paul 
12612bc21ed9SDavid Malone 	while (cm != NULL) {
12622bc21ed9SDavid Malone 		if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET
12632bc21ed9SDavid Malone 		    || cm->cmsg_len > clen) {
12642bc21ed9SDavid Malone 			error = EINVAL;
12652bc21ed9SDavid Malone 			goto out;
12662bc21ed9SDavid Malone 		}
12672bc21ed9SDavid Malone 
12682bc21ed9SDavid Malone 		data = CMSG_DATA(cm);
12692bc21ed9SDavid Malone 		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
12702bc21ed9SDavid Malone 
12712bc21ed9SDavid Malone 		switch (cm->cmsg_type) {
12720b788fa1SBill Paul 		/*
12730b788fa1SBill Paul 		 * Fill in credential information.
12740b788fa1SBill Paul 		 */
12752bc21ed9SDavid Malone 		case SCM_CREDS:
12762bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, sizeof(*cmcred),
12772bc21ed9SDavid Malone 			    SCM_CREDS, SOL_SOCKET);
12782bc21ed9SDavid Malone 			if (*controlp == NULL) {
12792bc21ed9SDavid Malone 				error = ENOBUFS;
12802bc21ed9SDavid Malone 				goto out;
12812bc21ed9SDavid Malone 			}
12822bc21ed9SDavid Malone 
12832bc21ed9SDavid Malone 			cmcred = (struct cmsgcred *)
12842bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
12850b788fa1SBill Paul 			cmcred->cmcred_pid = p->p_pid;
1286a854ed98SJohn Baldwin 			cmcred->cmcred_uid = td->td_ucred->cr_ruid;
1287a854ed98SJohn Baldwin 			cmcred->cmcred_gid = td->td_ucred->cr_rgid;
1288a854ed98SJohn Baldwin 			cmcred->cmcred_euid = td->td_ucred->cr_uid;
1289a854ed98SJohn Baldwin 			cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups,
12900b788fa1SBill Paul 							CMGROUP_MAX);
12910b788fa1SBill Paul 			for (i = 0; i < cmcred->cmcred_ngroups; i++)
12922bc21ed9SDavid Malone 				cmcred->cmcred_groups[i] =
1293a854ed98SJohn Baldwin 				    td->td_ucred->cr_groups[i];
12942bc21ed9SDavid Malone 			break;
12950b788fa1SBill Paul 
12962bc21ed9SDavid Malone 		case SCM_RIGHTS:
12972bc21ed9SDavid Malone 			oldfds = datalen / sizeof (int);
1298ed5b7817SJulian Elischer 			/*
12992bc21ed9SDavid Malone 			 * check that all the FDs passed in refer to legal files
1300ed5b7817SJulian Elischer 			 * If not, reject the entire operation.
1301ed5b7817SJulian Elischer 			 */
13022bc21ed9SDavid Malone 			fdp = data;
1303426da3bcSAlfred Perlstein 			FILEDESC_LOCK(fdescp);
1304df8bae1dSRodney W. Grimes 			for (i = 0; i < oldfds; i++) {
13058692c025SYoshinobu Inoue 				fd = *fdp++;
13068692c025SYoshinobu Inoue 				if ((unsigned)fd >= fdescp->fd_nfiles ||
13072bc21ed9SDavid Malone 				    fdescp->fd_ofiles[fd] == NULL) {
1308426da3bcSAlfred Perlstein 					FILEDESC_UNLOCK(fdescp);
13092bc21ed9SDavid Malone 					error = EBADF;
13102bc21ed9SDavid Malone 					goto out;
13112bc21ed9SDavid Malone 				}
1312e7d6662fSAlfred Perlstein 				fp = fdescp->fd_ofiles[fd];
1313e7d6662fSAlfred Perlstein 				if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) {
1314e7d6662fSAlfred Perlstein 					FILEDESC_UNLOCK(fdescp);
1315e7d6662fSAlfred Perlstein 					error = EOPNOTSUPP;
1316e7d6662fSAlfred Perlstein 					goto out;
1317e7d6662fSAlfred Perlstein 				}
1318e7d6662fSAlfred Perlstein 
1319df8bae1dSRodney W. Grimes 			}
1320ed5b7817SJulian Elischer 			/*
1321ed5b7817SJulian Elischer 			 * Now replace the integer FDs with pointers to
1322ed5b7817SJulian Elischer 			 * the associated global file table entry..
1323ed5b7817SJulian Elischer 			 */
13242bc21ed9SDavid Malone 			newlen = oldfds * sizeof(struct file *);
13252bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, newlen,
13262bc21ed9SDavid Malone 			    SCM_RIGHTS, SOL_SOCKET);
13272bc21ed9SDavid Malone 			if (*controlp == NULL) {
1328426da3bcSAlfred Perlstein 				FILEDESC_UNLOCK(fdescp);
13292bc21ed9SDavid Malone 				error = E2BIG;
13302bc21ed9SDavid Malone 				goto out;
13318692c025SYoshinobu Inoue 			}
13328692c025SYoshinobu Inoue 
13332bc21ed9SDavid Malone 			fdp = data;
13342bc21ed9SDavid Malone 			rp = (struct file **)
13352bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
13368692c025SYoshinobu Inoue 			for (i = 0; i < oldfds; i++) {
13378692c025SYoshinobu Inoue 				fp = fdescp->fd_ofiles[*fdp++];
1338df8bae1dSRodney W. Grimes 				*rp++ = fp;
1339426da3bcSAlfred Perlstein 				FILE_LOCK(fp);
1340df8bae1dSRodney W. Grimes 				fp->f_count++;
1341df8bae1dSRodney W. Grimes 				fp->f_msgcount++;
1342426da3bcSAlfred Perlstein 				FILE_UNLOCK(fp);
1343df8bae1dSRodney W. Grimes 				unp_rights++;
1344df8bae1dSRodney W. Grimes 			}
1345426da3bcSAlfred Perlstein 			FILEDESC_UNLOCK(fdescp);
13462bc21ed9SDavid Malone 			break;
13472bc21ed9SDavid Malone 
13482bc21ed9SDavid Malone 		case SCM_TIMESTAMP:
13492bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, sizeof(*tv),
13502bc21ed9SDavid Malone 			    SCM_TIMESTAMP, SOL_SOCKET);
13512bc21ed9SDavid Malone 			if (*controlp == NULL) {
13522bc21ed9SDavid Malone 				error = ENOBUFS;
13532bc21ed9SDavid Malone 				goto out;
13548692c025SYoshinobu Inoue 			}
13552bc21ed9SDavid Malone 			tv = (struct timeval *)
13562bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
13572bc21ed9SDavid Malone 			microtime(tv);
13582bc21ed9SDavid Malone 			break;
13592bc21ed9SDavid Malone 
13602bc21ed9SDavid Malone 		default:
13612bc21ed9SDavid Malone 			error = EINVAL;
13622bc21ed9SDavid Malone 			goto out;
13632bc21ed9SDavid Malone 		}
13642bc21ed9SDavid Malone 
13652bc21ed9SDavid Malone 		controlp = &(*controlp)->m_next;
13662bc21ed9SDavid Malone 
13672bc21ed9SDavid Malone 		if (CMSG_SPACE(datalen) < clen) {
13682bc21ed9SDavid Malone 			clen -= CMSG_SPACE(datalen);
13692bc21ed9SDavid Malone 			cm = (struct cmsghdr *)
13702bc21ed9SDavid Malone 			    ((caddr_t)cm + CMSG_SPACE(datalen));
13712bc21ed9SDavid Malone 		} else {
13722bc21ed9SDavid Malone 			clen = 0;
13732bc21ed9SDavid Malone 			cm = NULL;
13742bc21ed9SDavid Malone 		}
13752bc21ed9SDavid Malone 	}
13762bc21ed9SDavid Malone 
13772bc21ed9SDavid Malone out:
13782bc21ed9SDavid Malone 	m_freem(control);
13792bc21ed9SDavid Malone 
13802bc21ed9SDavid Malone 	return (error);
1381df8bae1dSRodney W. Grimes }
1382df8bae1dSRodney W. Grimes 
1383f708ef1bSPoul-Henning Kamp static int	unp_defer, unp_gcing;
1384df8bae1dSRodney W. Grimes 
1385f708ef1bSPoul-Henning Kamp static void
1386df8bae1dSRodney W. Grimes unp_gc()
1387df8bae1dSRodney W. Grimes {
1388df8bae1dSRodney W. Grimes 	register struct file *fp, *nextfp;
1389df8bae1dSRodney W. Grimes 	register struct socket *so;
1390df8bae1dSRodney W. Grimes 	struct file **extra_ref, **fpp;
1391df8bae1dSRodney W. Grimes 	int nunref, i;
1392df8bae1dSRodney W. Grimes 
13930d9ce3a1SRobert Watson 	UNP_LOCK_ASSERT();
13940d9ce3a1SRobert Watson 
1395df8bae1dSRodney W. Grimes 	if (unp_gcing)
1396df8bae1dSRodney W. Grimes 		return;
1397df8bae1dSRodney W. Grimes 	unp_gcing = 1;
1398df8bae1dSRodney W. Grimes 	unp_defer = 0;
1399ed5b7817SJulian Elischer 	/*
1400ed5b7817SJulian Elischer 	 * before going through all this, set all FDs to
1401ed5b7817SJulian Elischer 	 * be NOT defered and NOT externally accessible
1402ed5b7817SJulian Elischer 	 */
14030d9ce3a1SRobert Watson 	/*
14040d9ce3a1SRobert Watson 	 * XXXRW: Acquiring a sleep lock while holding UNP
14050d9ce3a1SRobert Watson 	 * mutex cannot be a good thing.
14060d9ce3a1SRobert Watson 	 */
1407426da3bcSAlfred Perlstein 	sx_slock(&filelist_lock);
14082e3c8fcbSPoul-Henning Kamp 	LIST_FOREACH(fp, &filehead, f_list)
1409426da3bcSAlfred Perlstein 		fp->f_gcflag &= ~(FMARK|FDEFER);
1410df8bae1dSRodney W. Grimes 	do {
14112e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(fp, &filehead, f_list) {
1412426da3bcSAlfred Perlstein 			FILE_LOCK(fp);
1413ed5b7817SJulian Elischer 			/*
1414ed5b7817SJulian Elischer 			 * If the file is not open, skip it
1415ed5b7817SJulian Elischer 			 */
1416426da3bcSAlfred Perlstein 			if (fp->f_count == 0) {
1417426da3bcSAlfred Perlstein 				FILE_UNLOCK(fp);
1418df8bae1dSRodney W. Grimes 				continue;
1419426da3bcSAlfred Perlstein 			}
1420ed5b7817SJulian Elischer 			/*
1421ed5b7817SJulian Elischer 			 * If we already marked it as 'defer'  in a
1422ed5b7817SJulian Elischer 			 * previous pass, then try process it this time
1423ed5b7817SJulian Elischer 			 * and un-mark it
1424ed5b7817SJulian Elischer 			 */
1425426da3bcSAlfred Perlstein 			if (fp->f_gcflag & FDEFER) {
1426426da3bcSAlfred Perlstein 				fp->f_gcflag &= ~FDEFER;
1427df8bae1dSRodney W. Grimes 				unp_defer--;
1428df8bae1dSRodney W. Grimes 			} else {
1429ed5b7817SJulian Elischer 				/*
1430ed5b7817SJulian Elischer 				 * if it's not defered, then check if it's
1431ed5b7817SJulian Elischer 				 * already marked.. if so skip it
1432ed5b7817SJulian Elischer 				 */
1433426da3bcSAlfred Perlstein 				if (fp->f_gcflag & FMARK) {
1434426da3bcSAlfred Perlstein 					FILE_UNLOCK(fp);
1435df8bae1dSRodney W. Grimes 					continue;
1436426da3bcSAlfred Perlstein 				}
1437ed5b7817SJulian Elischer 				/*
1438ed5b7817SJulian Elischer 				 * If all references are from messages
1439ed5b7817SJulian Elischer 				 * in transit, then skip it. it's not
1440ed5b7817SJulian Elischer 				 * externally accessible.
1441ed5b7817SJulian Elischer 				 */
1442426da3bcSAlfred Perlstein 				if (fp->f_count == fp->f_msgcount) {
1443426da3bcSAlfred Perlstein 					FILE_UNLOCK(fp);
1444df8bae1dSRodney W. Grimes 					continue;
1445426da3bcSAlfred Perlstein 				}
1446ed5b7817SJulian Elischer 				/*
1447ed5b7817SJulian Elischer 				 * If it got this far then it must be
1448ed5b7817SJulian Elischer 				 * externally accessible.
1449ed5b7817SJulian Elischer 				 */
1450426da3bcSAlfred Perlstein 				fp->f_gcflag |= FMARK;
1451df8bae1dSRodney W. Grimes 			}
1452ed5b7817SJulian Elischer 			/*
1453ed5b7817SJulian Elischer 			 * either it was defered, or it is externally
1454ed5b7817SJulian Elischer 			 * accessible and not already marked so.
1455ed5b7817SJulian Elischer 			 * Now check if it is possibly one of OUR sockets.
1456ed5b7817SJulian Elischer 			 */
1457df8bae1dSRodney W. Grimes 			if (fp->f_type != DTYPE_SOCKET ||
145848e3128bSMatthew Dillon 			    (so = fp->f_data) == NULL) {
1459426da3bcSAlfred Perlstein 				FILE_UNLOCK(fp);
1460df8bae1dSRodney W. Grimes 				continue;
1461426da3bcSAlfred Perlstein 			}
1462426da3bcSAlfred Perlstein 			FILE_UNLOCK(fp);
1463748e0b0aSGarrett Wollman 			if (so->so_proto->pr_domain != &localdomain ||
1464df8bae1dSRodney W. Grimes 			    (so->so_proto->pr_flags&PR_RIGHTS) == 0)
1465df8bae1dSRodney W. Grimes 				continue;
1466df8bae1dSRodney W. Grimes #ifdef notdef
1467df8bae1dSRodney W. Grimes 			if (so->so_rcv.sb_flags & SB_LOCK) {
1468df8bae1dSRodney W. Grimes 				/*
1469df8bae1dSRodney W. Grimes 				 * This is problematical; it's not clear
1470df8bae1dSRodney W. Grimes 				 * we need to wait for the sockbuf to be
1471df8bae1dSRodney W. Grimes 				 * unlocked (on a uniprocessor, at least),
1472df8bae1dSRodney W. Grimes 				 * and it's also not clear what to do
1473df8bae1dSRodney W. Grimes 				 * if sbwait returns an error due to receipt
1474df8bae1dSRodney W. Grimes 				 * of a signal.  If sbwait does return
1475df8bae1dSRodney W. Grimes 				 * an error, we'll go into an infinite
1476df8bae1dSRodney W. Grimes 				 * loop.  Delete all of this for now.
1477df8bae1dSRodney W. Grimes 				 */
1478df8bae1dSRodney W. Grimes 				(void) sbwait(&so->so_rcv);
1479df8bae1dSRodney W. Grimes 				goto restart;
1480df8bae1dSRodney W. Grimes 			}
1481df8bae1dSRodney W. Grimes #endif
1482ed5b7817SJulian Elischer 			/*
1483ed5b7817SJulian Elischer 			 * So, Ok, it's one of our sockets and it IS externally
1484ed5b7817SJulian Elischer 			 * accessible (or was defered). Now we look
1485dc733423SDag-Erling Smørgrav 			 * to see if we hold any file descriptors in its
1486ed5b7817SJulian Elischer 			 * message buffers. Follow those links and mark them
1487ed5b7817SJulian Elischer 			 * as accessible too.
1488ed5b7817SJulian Elischer 			 */
1489df8bae1dSRodney W. Grimes 			unp_scan(so->so_rcv.sb_mb, unp_mark);
1490df8bae1dSRodney W. Grimes 		}
1491df8bae1dSRodney W. Grimes 	} while (unp_defer);
1492426da3bcSAlfred Perlstein 	sx_sunlock(&filelist_lock);
1493df8bae1dSRodney W. Grimes 	/*
1494df8bae1dSRodney W. Grimes 	 * We grab an extra reference to each of the file table entries
1495df8bae1dSRodney W. Grimes 	 * that are not otherwise accessible and then free the rights
1496df8bae1dSRodney W. Grimes 	 * that are stored in messages on them.
1497df8bae1dSRodney W. Grimes 	 *
1498df8bae1dSRodney W. Grimes 	 * The bug in the orginal code is a little tricky, so I'll describe
1499df8bae1dSRodney W. Grimes 	 * what's wrong with it here.
1500df8bae1dSRodney W. Grimes 	 *
1501df8bae1dSRodney W. Grimes 	 * It is incorrect to simply unp_discard each entry for f_msgcount
1502df8bae1dSRodney W. Grimes 	 * times -- consider the case of sockets A and B that contain
1503df8bae1dSRodney W. Grimes 	 * references to each other.  On a last close of some other socket,
1504df8bae1dSRodney W. Grimes 	 * we trigger a gc since the number of outstanding rights (unp_rights)
1505df8bae1dSRodney W. Grimes 	 * is non-zero.  If during the sweep phase the gc code un_discards,
1506df8bae1dSRodney W. Grimes 	 * we end up doing a (full) closef on the descriptor.  A closef on A
1507df8bae1dSRodney W. Grimes 	 * results in the following chain.  Closef calls soo_close, which
1508df8bae1dSRodney W. Grimes 	 * calls soclose.   Soclose calls first (through the switch
1509df8bae1dSRodney W. Grimes 	 * uipc_usrreq) unp_detach, which re-invokes unp_gc.  Unp_gc simply
1510df8bae1dSRodney W. Grimes 	 * returns because the previous instance had set unp_gcing, and
1511df8bae1dSRodney W. Grimes 	 * we return all the way back to soclose, which marks the socket
1512df8bae1dSRodney W. Grimes 	 * with SS_NOFDREF, and then calls sofree.  Sofree calls sorflush
1513df8bae1dSRodney W. Grimes 	 * to free up the rights that are queued in messages on the socket A,
1514df8bae1dSRodney W. Grimes 	 * i.e., the reference on B.  The sorflush calls via the dom_dispose
1515df8bae1dSRodney W. Grimes 	 * switch unp_dispose, which unp_scans with unp_discard.  This second
1516df8bae1dSRodney W. Grimes 	 * instance of unp_discard just calls closef on B.
1517df8bae1dSRodney W. Grimes 	 *
1518df8bae1dSRodney W. Grimes 	 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1519df8bae1dSRodney W. Grimes 	 * which results in another closef on A.  Unfortunately, A is already
1520df8bae1dSRodney W. Grimes 	 * being closed, and the descriptor has already been marked with
1521df8bae1dSRodney W. Grimes 	 * SS_NOFDREF, and soclose panics at this point.
1522df8bae1dSRodney W. Grimes 	 *
1523df8bae1dSRodney W. Grimes 	 * Here, we first take an extra reference to each inaccessible
1524df8bae1dSRodney W. Grimes 	 * descriptor.  Then, we call sorflush ourself, since we know
1525df8bae1dSRodney W. Grimes 	 * it is a Unix domain socket anyhow.  After we destroy all the
1526df8bae1dSRodney W. Grimes 	 * rights carried in messages, we do a last closef to get rid
1527df8bae1dSRodney W. Grimes 	 * of our extra reference.  This is the last close, and the
1528df8bae1dSRodney W. Grimes 	 * unp_detach etc will shut down the socket.
1529df8bae1dSRodney W. Grimes 	 *
1530df8bae1dSRodney W. Grimes 	 * 91/09/19, bsy@cs.cmu.edu
1531df8bae1dSRodney W. Grimes 	 */
1532a163d034SWarner Losh 	extra_ref = malloc(nfiles * sizeof(struct file *), M_TEMP, M_WAITOK);
1533426da3bcSAlfred Perlstein 	sx_slock(&filelist_lock);
1534fc3fcacfSRobert Watson 	for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref;
1535fc3fcacfSRobert Watson 	    fp != NULL; fp = nextfp) {
15362e3c8fcbSPoul-Henning Kamp 		nextfp = LIST_NEXT(fp, f_list);
1537426da3bcSAlfred Perlstein 		FILE_LOCK(fp);
1538ed5b7817SJulian Elischer 		/*
1539ed5b7817SJulian Elischer 		 * If it's not open, skip it
1540ed5b7817SJulian Elischer 		 */
1541426da3bcSAlfred Perlstein 		if (fp->f_count == 0) {
1542426da3bcSAlfred Perlstein 			FILE_UNLOCK(fp);
1543df8bae1dSRodney W. Grimes 			continue;
1544426da3bcSAlfred Perlstein 		}
1545ed5b7817SJulian Elischer 		/*
1546ed5b7817SJulian Elischer 		 * If all refs are from msgs, and it's not marked accessible
1547ed5b7817SJulian Elischer 		 * then it must be referenced from some unreachable cycle
1548ed5b7817SJulian Elischer 		 * of (shut-down) FDs, so include it in our
1549ed5b7817SJulian Elischer 		 * list of FDs to remove
1550ed5b7817SJulian Elischer 		 */
1551426da3bcSAlfred Perlstein 		if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) {
1552df8bae1dSRodney W. Grimes 			*fpp++ = fp;
1553df8bae1dSRodney W. Grimes 			nunref++;
1554df8bae1dSRodney W. Grimes 			fp->f_count++;
1555df8bae1dSRodney W. Grimes 		}
1556426da3bcSAlfred Perlstein 		FILE_UNLOCK(fp);
1557df8bae1dSRodney W. Grimes 	}
1558426da3bcSAlfred Perlstein 	sx_sunlock(&filelist_lock);
1559ed5b7817SJulian Elischer 	/*
1560ed5b7817SJulian Elischer 	 * for each FD on our hit list, do the following two things
1561ed5b7817SJulian Elischer 	 */
15621c7c3c6aSMatthew Dillon 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
15631c7c3c6aSMatthew Dillon 		struct file *tfp = *fpp;
1564426da3bcSAlfred Perlstein 		FILE_LOCK(tfp);
1565cd72f218SMatthew Dillon 		if (tfp->f_type == DTYPE_SOCKET &&
156648e3128bSMatthew Dillon 		    tfp->f_data != NULL) {
1567426da3bcSAlfred Perlstein 			FILE_UNLOCK(tfp);
156848e3128bSMatthew Dillon 			sorflush(tfp->f_data);
1569e5aeaa0cSDag-Erling Smørgrav 		} else {
1570426da3bcSAlfred Perlstein 			FILE_UNLOCK(tfp);
15711c7c3c6aSMatthew Dillon 		}
1572e5aeaa0cSDag-Erling Smørgrav 	}
1573df8bae1dSRodney W. Grimes 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp)
1574b40ce416SJulian Elischer 		closef(*fpp, (struct thread *) NULL);
1575210a5a71SAlfred Perlstein 	free(extra_ref, M_TEMP);
1576df8bae1dSRodney W. Grimes 	unp_gcing = 0;
1577df8bae1dSRodney W. Grimes }
1578df8bae1dSRodney W. Grimes 
157926f9a767SRodney W. Grimes void
1580df8bae1dSRodney W. Grimes unp_dispose(m)
1581df8bae1dSRodney W. Grimes 	struct mbuf *m;
1582df8bae1dSRodney W. Grimes {
1583996c772fSJohn Dyson 
1584df8bae1dSRodney W. Grimes 	if (m)
1585df8bae1dSRodney W. Grimes 		unp_scan(m, unp_discard);
1586df8bae1dSRodney W. Grimes }
1587df8bae1dSRodney W. Grimes 
15880c1bb4fbSDima Dorfman static int
15896f105b34SJohn Baldwin unp_listen(unp, td)
15900c1bb4fbSDima Dorfman 	struct unpcb *unp;
15916f105b34SJohn Baldwin 	struct thread *td;
15920c1bb4fbSDima Dorfman {
15930d9ce3a1SRobert Watson 	UNP_LOCK_ASSERT();
15940c1bb4fbSDima Dorfman 
15950d9ce3a1SRobert Watson 	/*
15960d9ce3a1SRobert Watson 	 * XXXRW: Why populate the local peer cred with our own credential?
15970d9ce3a1SRobert Watson 	 */
15986f105b34SJohn Baldwin 	cru2x(td->td_ucred, &unp->unp_peercred);
15990c1bb4fbSDima Dorfman 	unp->unp_flags |= UNP_HAVEPCCACHED;
16000c1bb4fbSDima Dorfman 	return (0);
16010c1bb4fbSDima Dorfman }
16020c1bb4fbSDima Dorfman 
1603f708ef1bSPoul-Henning Kamp static void
1604df8bae1dSRodney W. Grimes unp_scan(m0, op)
1605df8bae1dSRodney W. Grimes 	register struct mbuf *m0;
16064d77a549SAlfred Perlstein 	void (*op)(struct file *);
1607df8bae1dSRodney W. Grimes {
16082bc21ed9SDavid Malone 	struct mbuf *m;
16092bc21ed9SDavid Malone 	struct file **rp;
16102bc21ed9SDavid Malone 	struct cmsghdr *cm;
16112bc21ed9SDavid Malone 	void *data;
16122bc21ed9SDavid Malone 	int i;
16132bc21ed9SDavid Malone 	socklen_t clen, datalen;
1614df8bae1dSRodney W. Grimes 	int qfds;
1615df8bae1dSRodney W. Grimes 
1616fc3fcacfSRobert Watson 	while (m0 != NULL) {
16172bc21ed9SDavid Malone 		for (m = m0; m; m = m->m_next) {
161812396bdcSDavid Malone 			if (m->m_type != MT_CONTROL)
1619df8bae1dSRodney W. Grimes 				continue;
16202bc21ed9SDavid Malone 
16212bc21ed9SDavid Malone 			cm = mtod(m, struct cmsghdr *);
16222bc21ed9SDavid Malone 			clen = m->m_len;
16232bc21ed9SDavid Malone 
16242bc21ed9SDavid Malone 			while (cm != NULL) {
16252bc21ed9SDavid Malone 				if (sizeof(*cm) > clen || cm->cmsg_len > clen)
16262bc21ed9SDavid Malone 					break;
16272bc21ed9SDavid Malone 
16282bc21ed9SDavid Malone 				data = CMSG_DATA(cm);
16292bc21ed9SDavid Malone 				datalen = (caddr_t)cm + cm->cmsg_len
16302bc21ed9SDavid Malone 				    - (caddr_t)data;
16312bc21ed9SDavid Malone 
16322bc21ed9SDavid Malone 				if (cm->cmsg_level == SOL_SOCKET &&
16332bc21ed9SDavid Malone 				    cm->cmsg_type == SCM_RIGHTS) {
16342bc21ed9SDavid Malone 					qfds = datalen / sizeof (struct file *);
16352bc21ed9SDavid Malone 					rp = data;
1636df8bae1dSRodney W. Grimes 					for (i = 0; i < qfds; i++)
1637df8bae1dSRodney W. Grimes 						(*op)(*rp++);
16382bc21ed9SDavid Malone 				}
16392bc21ed9SDavid Malone 
16402bc21ed9SDavid Malone 				if (CMSG_SPACE(datalen) < clen) {
16412bc21ed9SDavid Malone 					clen -= CMSG_SPACE(datalen);
16422bc21ed9SDavid Malone 					cm = (struct cmsghdr *)
16432bc21ed9SDavid Malone 					    ((caddr_t)cm + CMSG_SPACE(datalen));
16442bc21ed9SDavid Malone 				} else {
16452bc21ed9SDavid Malone 					clen = 0;
16462bc21ed9SDavid Malone 					cm = NULL;
16472bc21ed9SDavid Malone 				}
16482bc21ed9SDavid Malone 			}
1649df8bae1dSRodney W. Grimes 		}
1650df8bae1dSRodney W. Grimes 		m0 = m0->m_act;
1651df8bae1dSRodney W. Grimes 	}
1652df8bae1dSRodney W. Grimes }
1653df8bae1dSRodney W. Grimes 
1654f708ef1bSPoul-Henning Kamp static void
1655df8bae1dSRodney W. Grimes unp_mark(fp)
1656df8bae1dSRodney W. Grimes 	struct file *fp;
1657df8bae1dSRodney W. Grimes {
1658426da3bcSAlfred Perlstein 	if (fp->f_gcflag & FMARK)
1659df8bae1dSRodney W. Grimes 		return;
1660df8bae1dSRodney W. Grimes 	unp_defer++;
1661426da3bcSAlfred Perlstein 	fp->f_gcflag |= (FMARK|FDEFER);
1662df8bae1dSRodney W. Grimes }
1663df8bae1dSRodney W. Grimes 
1664f708ef1bSPoul-Henning Kamp static void
1665df8bae1dSRodney W. Grimes unp_discard(fp)
1666df8bae1dSRodney W. Grimes 	struct file *fp;
1667df8bae1dSRodney W. Grimes {
1668426da3bcSAlfred Perlstein 	FILE_LOCK(fp);
1669df8bae1dSRodney W. Grimes 	fp->f_msgcount--;
1670df8bae1dSRodney W. Grimes 	unp_rights--;
1671426da3bcSAlfred Perlstein 	FILE_UNLOCK(fp);
1672b40ce416SJulian Elischer 	(void) closef(fp, (struct thread *)NULL);
1673df8bae1dSRodney W. Grimes }
1674