xref: /freebsd/sys/kern/uipc_usrreq.c (revision fe2eee8231bddaae179df741f7df389a96e7afef)
19454b2d8SWarner Losh /*-
2d664e4faSRobert Watson  * Copyright 2004-2005 Robert N. M. Watson
3df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1993
4df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
5df8bae1dSRodney W. Grimes  *
6df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
7df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
8df8bae1dSRodney W. Grimes  * are met:
9df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
10df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
11df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
13df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
14df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
15df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
16df8bae1dSRodney W. Grimes  *    without specific prior written permission.
17df8bae1dSRodney W. Grimes  *
18df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
29df8bae1dSRodney W. Grimes  *
30748e0b0aSGarrett Wollman  *	From: @(#)uipc_usrreq.c	8.3 (Berkeley) 1/4/94
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
33677b542eSDavid E. O'Brien #include <sys/cdefs.h>
34677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
35677b542eSDavid E. O'Brien 
36335654d7SRobert Watson #include "opt_mac.h"
37335654d7SRobert Watson 
38df8bae1dSRodney W. Grimes #include <sys/param.h>
39fb919e4dSMark Murray #include <sys/domain.h>
40960ed29cSSeigo Tanimura #include <sys/fcntl.h>
41d826c479SBruce Evans #include <sys/malloc.h>		/* XXX must be before <sys/file.h> */
42639acc13SGarrett Wollman #include <sys/file.h>
43960ed29cSSeigo Tanimura #include <sys/filedesc.h>
44960ed29cSSeigo Tanimura #include <sys/jail.h>
45960ed29cSSeigo Tanimura #include <sys/kernel.h>
46960ed29cSSeigo Tanimura #include <sys/lock.h>
476ea48a90SRobert Watson #include <sys/mac.h>
48639acc13SGarrett Wollman #include <sys/mbuf.h>
49960ed29cSSeigo Tanimura #include <sys/mutex.h>
50639acc13SGarrett Wollman #include <sys/namei.h>
51639acc13SGarrett Wollman #include <sys/proc.h>
52df8bae1dSRodney W. Grimes #include <sys/protosw.h>
53960ed29cSSeigo Tanimura #include <sys/resourcevar.h>
54df8bae1dSRodney W. Grimes #include <sys/socket.h>
55df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
56960ed29cSSeigo Tanimura #include <sys/signalvar.h>
57df8bae1dSRodney W. Grimes #include <sys/stat.h>
58960ed29cSSeigo Tanimura #include <sys/sx.h>
59639acc13SGarrett Wollman #include <sys/sysctl.h>
60960ed29cSSeigo Tanimura #include <sys/systm.h>
61639acc13SGarrett Wollman #include <sys/un.h>
6298271db4SGarrett Wollman #include <sys/unpcb.h>
63639acc13SGarrett Wollman #include <sys/vnode.h>
64df8bae1dSRodney W. Grimes 
659e9d298aSJeff Roberson #include <vm/uma.h>
6698271db4SGarrett Wollman 
679e9d298aSJeff Roberson static uma_zone_t unp_zone;
6898271db4SGarrett Wollman static	unp_gen_t unp_gencnt;
6998271db4SGarrett Wollman static	u_int unp_count;
7098271db4SGarrett Wollman 
7198271db4SGarrett Wollman static	struct unp_head unp_shead, unp_dhead;
7298271db4SGarrett Wollman 
73df8bae1dSRodney W. Grimes /*
74df8bae1dSRodney W. Grimes  * Unix communications domain.
75df8bae1dSRodney W. Grimes  *
76df8bae1dSRodney W. Grimes  * TODO:
77df8bae1dSRodney W. Grimes  *	SEQPACKET, RDM
78df8bae1dSRodney W. Grimes  *	rethink name space problems
79df8bae1dSRodney W. Grimes  *	need a proper out-of-band
8098271db4SGarrett Wollman  *	lock pushdown
81df8bae1dSRodney W. Grimes  */
82e7dd9a10SRobert Watson static const struct	sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL };
83f708ef1bSPoul-Henning Kamp static ino_t	unp_ino;		/* prototype for fake inode numbers */
846a2989fdSMatthew N. Dodd struct mbuf *unp_addsockcred(struct thread *, struct mbuf *);
85f708ef1bSPoul-Henning Kamp 
86ce5f32deSRobert Watson /*
87ce5f32deSRobert Watson  * Currently, UNIX domain sockets are protected by a single subsystem lock,
88ce5f32deSRobert Watson  * which covers global data structures and variables, the contents of each
89ce5f32deSRobert Watson  * per-socket unpcb structure, and the so_pcb field in sockets attached to
90ce5f32deSRobert Watson  * the UNIX domain.  This provides for a moderate degree of paralellism, as
91ce5f32deSRobert Watson  * receive operations on UNIX domain sockets do not need to acquire the
92ce5f32deSRobert Watson  * subsystem lock.  Finer grained locking to permit send() without acquiring
93ce5f32deSRobert Watson  * a global lock would be a logical next step.
94ce5f32deSRobert Watson  *
95ce5f32deSRobert Watson  * The UNIX domain socket lock preceds all socket layer locks, including the
96ce5f32deSRobert Watson  * socket lock and socket buffer lock, permitting UNIX domain socket code to
97ce5f32deSRobert Watson  * call into socket support routines without releasing its locks.
98ce5f32deSRobert Watson  *
99ce5f32deSRobert Watson  * Some caution is required in areas where the UNIX domain socket code enters
100ce5f32deSRobert Watson  * VFS in order to create or find rendezvous points.  This results in
101ce5f32deSRobert Watson  * dropping of the UNIX domain socket subsystem lock, acquisition of the
102ce5f32deSRobert Watson  * Giant lock, and potential sleeping.  This increases the chances of races,
103ce5f32deSRobert Watson  * and exposes weaknesses in the socket->protocol API by offering poor
104ce5f32deSRobert Watson  * failure modes.
105ce5f32deSRobert Watson  */
1060d9ce3a1SRobert Watson static struct mtx unp_mtx;
1070d9ce3a1SRobert Watson #define	UNP_LOCK_INIT() \
1080d9ce3a1SRobert Watson 	mtx_init(&unp_mtx, "unp", NULL, MTX_DEF)
1090d9ce3a1SRobert Watson #define	UNP_LOCK()		mtx_lock(&unp_mtx)
1100d9ce3a1SRobert Watson #define	UNP_UNLOCK()		mtx_unlock(&unp_mtx)
1110d9ce3a1SRobert Watson #define	UNP_LOCK_ASSERT()	mtx_assert(&unp_mtx, MA_OWNED)
1124c5bc1caSRobert Watson #define	UNP_UNLOCK_ASSERT()	mtx_assert(&unp_mtx, MA_NOTOWNED)
1130d9ce3a1SRobert Watson 
1144d77a549SAlfred Perlstein static int     unp_attach(struct socket *);
1154d77a549SAlfred Perlstein static void    unp_detach(struct unpcb *);
1164d77a549SAlfred Perlstein static int     unp_bind(struct unpcb *,struct sockaddr *, struct thread *);
11770f52b48SBruce Evans static int     unp_connect(struct socket *,struct sockaddr *, struct thread *);
1186a2989fdSMatthew N. Dodd static int     unp_connect2(struct socket *so, struct socket *so2, int);
1194d77a549SAlfred Perlstein static void    unp_disconnect(struct unpcb *);
1204d77a549SAlfred Perlstein static void    unp_shutdown(struct unpcb *);
1214d77a549SAlfred Perlstein static void    unp_drop(struct unpcb *, int);
1224d77a549SAlfred Perlstein static void    unp_gc(void);
1234d77a549SAlfred Perlstein static void    unp_scan(struct mbuf *, void (*)(struct file *));
1244d77a549SAlfred Perlstein static void    unp_mark(struct file *);
1254d77a549SAlfred Perlstein static void    unp_discard(struct file *);
1264d77a549SAlfred Perlstein static void    unp_freerights(struct file **, int);
1274d77a549SAlfred Perlstein static int     unp_internalize(struct mbuf **, struct thread *);
1280daccb9cSRobert Watson static int     unp_listen(struct socket *, struct unpcb *, struct thread *);
129f708ef1bSPoul-Henning Kamp 
130a29f300eSGarrett Wollman static int
131a29f300eSGarrett Wollman uipc_abort(struct socket *so)
132df8bae1dSRodney W. Grimes {
13340f2ac28SRobert Watson 	struct unpcb *unp;
134df8bae1dSRodney W. Grimes 
1350d9ce3a1SRobert Watson 	UNP_LOCK();
13640f2ac28SRobert Watson 	unp = sotounpcb(so);
13740f2ac28SRobert Watson 	if (unp == NULL) {
13840f2ac28SRobert Watson 		UNP_UNLOCK();
13940f2ac28SRobert Watson 		return (EINVAL);
14040f2ac28SRobert Watson 	}
141a29f300eSGarrett Wollman 	unp_drop(unp, ECONNABORTED);
1424c5bc1caSRobert Watson 	unp_detach(unp);
1434c5bc1caSRobert Watson 	UNP_UNLOCK_ASSERT();
14481158452SRobert Watson 	ACCEPT_LOCK();
145395a08c9SRobert Watson 	SOCK_LOCK(so);
146ddb7d629SIan Dowse 	sotryfree(so);
147e5aeaa0cSDag-Erling Smørgrav 	return (0);
148df8bae1dSRodney W. Grimes }
149df8bae1dSRodney W. Grimes 
150a29f300eSGarrett Wollman static int
15157bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam)
152a29f300eSGarrett Wollman {
15340f2ac28SRobert Watson 	struct unpcb *unp;
1540d9ce3a1SRobert Watson 	const struct sockaddr *sa;
155df8bae1dSRodney W. Grimes 
156df8bae1dSRodney W. Grimes 	/*
157df8bae1dSRodney W. Grimes 	 * Pass back name of connected socket,
158df8bae1dSRodney W. Grimes 	 * if it was bound and we are still connected
159df8bae1dSRodney W. Grimes 	 * (our peer may have closed already!).
160df8bae1dSRodney W. Grimes 	 */
1610d9ce3a1SRobert Watson 	*nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
1620d9ce3a1SRobert Watson 	UNP_LOCK();
16340f2ac28SRobert Watson 	unp = sotounpcb(so);
16440f2ac28SRobert Watson 	if (unp == NULL) {
16540f2ac28SRobert Watson 		UNP_UNLOCK();
16640f2ac28SRobert Watson 		free(*nam, M_SONAME);
16740f2ac28SRobert Watson 		*nam = NULL;
16840f2ac28SRobert Watson 		return (EINVAL);
16940f2ac28SRobert Watson 	}
1700d9ce3a1SRobert Watson 	if (unp->unp_conn != NULL && unp->unp_conn->unp_addr != NULL)
1710d9ce3a1SRobert Watson 		sa = (struct sockaddr *) unp->unp_conn->unp_addr;
1720d9ce3a1SRobert Watson 	else
1730d9ce3a1SRobert Watson 		sa = &sun_noname;
1740d9ce3a1SRobert Watson 	bcopy(sa, *nam, sa->sa_len);
1750d9ce3a1SRobert Watson 	UNP_UNLOCK();
176e5aeaa0cSDag-Erling Smørgrav 	return (0);
177a29f300eSGarrett Wollman }
178df8bae1dSRodney W. Grimes 
179a29f300eSGarrett Wollman static int
180b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td)
181a29f300eSGarrett Wollman {
182a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
183df8bae1dSRodney W. Grimes 
184fc3fcacfSRobert Watson 	if (unp != NULL)
185e5aeaa0cSDag-Erling Smørgrav 		return (EISCONN);
186e5aeaa0cSDag-Erling Smørgrav 	return (unp_attach(so));
187a29f300eSGarrett Wollman }
188a29f300eSGarrett Wollman 
189a29f300eSGarrett Wollman static int
190b40ce416SJulian Elischer uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
191a29f300eSGarrett Wollman {
19240f2ac28SRobert Watson 	struct unpcb *unp;
19340f2ac28SRobert Watson 	int error;
194a29f300eSGarrett Wollman 
19540f2ac28SRobert Watson 	UNP_LOCK();
19640f2ac28SRobert Watson 	unp = sotounpcb(so);
19740f2ac28SRobert Watson 	if (unp == NULL) {
19840f2ac28SRobert Watson 		UNP_UNLOCK();
199e5aeaa0cSDag-Erling Smørgrav 		return (EINVAL);
20040f2ac28SRobert Watson 	}
20140f2ac28SRobert Watson 	error = unp_bind(unp, nam, td);
20240f2ac28SRobert Watson 	UNP_UNLOCK();
20340f2ac28SRobert Watson 	return (error);
204a29f300eSGarrett Wollman }
205a29f300eSGarrett Wollman 
206a29f300eSGarrett Wollman static int
207b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
208a29f300eSGarrett Wollman {
209b295bdcdSRobert Watson 	struct unpcb *unp;
2100d9ce3a1SRobert Watson 	int error;
211a29f300eSGarrett Wollman 
212fd179ee9SRobert Watson 	KASSERT(td == curthread, ("uipc_connect: td != curthread"));
213fd179ee9SRobert Watson 
2140d9ce3a1SRobert Watson 	UNP_LOCK();
215b295bdcdSRobert Watson 	unp = sotounpcb(so);
216b295bdcdSRobert Watson 	if (unp == NULL) {
21740f2ac28SRobert Watson 		UNP_UNLOCK();
21840f2ac28SRobert Watson 		return (EINVAL);
219b295bdcdSRobert Watson 	}
220fd179ee9SRobert Watson 	error = unp_connect(so, nam, td);
2210d9ce3a1SRobert Watson 	UNP_UNLOCK();
2220d9ce3a1SRobert Watson 	return (error);
223a29f300eSGarrett Wollman }
224a29f300eSGarrett Wollman 
225db48c0d2SRobert Watson int
226a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2)
227a29f300eSGarrett Wollman {
22840f2ac28SRobert Watson 	struct unpcb *unp;
2290d9ce3a1SRobert Watson 	int error;
230a29f300eSGarrett Wollman 
2310d9ce3a1SRobert Watson 	UNP_LOCK();
23240f2ac28SRobert Watson 	unp = sotounpcb(so1);
23340f2ac28SRobert Watson 	if (unp == NULL) {
23440f2ac28SRobert Watson 		UNP_UNLOCK();
23540f2ac28SRobert Watson 		return (EINVAL);
23640f2ac28SRobert Watson 	}
2376a2989fdSMatthew N. Dodd 	error = unp_connect2(so1, so2, PRU_CONNECT2);
2380d9ce3a1SRobert Watson 	UNP_UNLOCK();
2390d9ce3a1SRobert Watson 	return (error);
240a29f300eSGarrett Wollman }
241a29f300eSGarrett Wollman 
242a29f300eSGarrett Wollman /* control is EOPNOTSUPP */
243a29f300eSGarrett Wollman 
244a29f300eSGarrett Wollman static int
245a29f300eSGarrett Wollman uipc_detach(struct socket *so)
246a29f300eSGarrett Wollman {
24740f2ac28SRobert Watson 	struct unpcb *unp;
248a29f300eSGarrett Wollman 
2490d9ce3a1SRobert Watson 	UNP_LOCK();
25040f2ac28SRobert Watson 	unp = sotounpcb(so);
25140f2ac28SRobert Watson 	if (unp == NULL) {
25240f2ac28SRobert Watson 		UNP_UNLOCK();
25340f2ac28SRobert Watson 		return (EINVAL);
25440f2ac28SRobert Watson 	}
2554c5bc1caSRobert Watson 	unp_detach(unp);
2564c5bc1caSRobert Watson 	UNP_UNLOCK_ASSERT();
257e5aeaa0cSDag-Erling Smørgrav 	return (0);
258a29f300eSGarrett Wollman }
259a29f300eSGarrett Wollman 
260a29f300eSGarrett Wollman static int
261a29f300eSGarrett Wollman uipc_disconnect(struct socket *so)
262a29f300eSGarrett Wollman {
26340f2ac28SRobert Watson 	struct unpcb *unp;
264a29f300eSGarrett Wollman 
2650d9ce3a1SRobert Watson 	UNP_LOCK();
26640f2ac28SRobert Watson 	unp = sotounpcb(so);
26740f2ac28SRobert Watson 	if (unp == NULL) {
26840f2ac28SRobert Watson 		UNP_UNLOCK();
26940f2ac28SRobert Watson 		return (EINVAL);
27040f2ac28SRobert Watson 	}
271a29f300eSGarrett Wollman 	unp_disconnect(unp);
2720d9ce3a1SRobert Watson 	UNP_UNLOCK();
273e5aeaa0cSDag-Erling Smørgrav 	return (0);
274a29f300eSGarrett Wollman }
275a29f300eSGarrett Wollman 
276a29f300eSGarrett Wollman static int
277b40ce416SJulian Elischer uipc_listen(struct socket *so, struct thread *td)
278a29f300eSGarrett Wollman {
27940f2ac28SRobert Watson 	struct unpcb *unp;
2800d9ce3a1SRobert Watson 	int error;
281a29f300eSGarrett Wollman 
2820d9ce3a1SRobert Watson 	UNP_LOCK();
28340f2ac28SRobert Watson 	unp = sotounpcb(so);
28440f2ac28SRobert Watson 	if (unp == NULL || unp->unp_vnode == NULL) {
28540f2ac28SRobert Watson 		UNP_UNLOCK();
28640f2ac28SRobert Watson 		return (EINVAL);
28740f2ac28SRobert Watson 	}
2880daccb9cSRobert Watson 	error = unp_listen(so, unp, td);
2890d9ce3a1SRobert Watson 	UNP_UNLOCK();
2900d9ce3a1SRobert Watson 	return (error);
291a29f300eSGarrett Wollman }
292a29f300eSGarrett Wollman 
293a29f300eSGarrett Wollman static int
29457bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam)
295a29f300eSGarrett Wollman {
29640f2ac28SRobert Watson 	struct unpcb *unp;
2970d9ce3a1SRobert Watson 	const struct sockaddr *sa;
298a29f300eSGarrett Wollman 
2990d9ce3a1SRobert Watson 	*nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
3000d9ce3a1SRobert Watson 	UNP_LOCK();
30140f2ac28SRobert Watson 	unp = sotounpcb(so);
30240f2ac28SRobert Watson 	if (unp == NULL) {
30340f2ac28SRobert Watson 		UNP_UNLOCK();
30440f2ac28SRobert Watson 		free(*nam, M_SONAME);
30540f2ac28SRobert Watson 		*nam = NULL;
30640f2ac28SRobert Watson 		return (EINVAL);
30740f2ac28SRobert Watson 	}
308fc3fcacfSRobert Watson 	if (unp->unp_conn != NULL && unp->unp_conn->unp_addr!= NULL)
3090d9ce3a1SRobert Watson 		sa = (struct sockaddr *) unp->unp_conn->unp_addr;
310bdc5f6a3SHajimu UMEMOTO 	else {
311bdc5f6a3SHajimu UMEMOTO 		/*
312bdc5f6a3SHajimu UMEMOTO 		 * XXX: It seems that this test always fails even when
313bdc5f6a3SHajimu UMEMOTO 		 * connection is established.  So, this else clause is
314bdc5f6a3SHajimu UMEMOTO 		 * added as workaround to return PF_LOCAL sockaddr.
315bdc5f6a3SHajimu UMEMOTO 		 */
3160d9ce3a1SRobert Watson 		sa = &sun_noname;
317bdc5f6a3SHajimu UMEMOTO 	}
3180d9ce3a1SRobert Watson 	bcopy(sa, *nam, sa->sa_len);
3190d9ce3a1SRobert Watson 	UNP_UNLOCK();
320e5aeaa0cSDag-Erling Smørgrav 	return (0);
321a29f300eSGarrett Wollman }
322a29f300eSGarrett Wollman 
323a29f300eSGarrett Wollman static int
324a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags)
325a29f300eSGarrett Wollman {
32640f2ac28SRobert Watson 	struct unpcb *unp;
327a29f300eSGarrett Wollman 	struct socket *so2;
3286aef685fSBrian Feldman 	u_long newhiwat;
329a29f300eSGarrett Wollman 
3300d9ce3a1SRobert Watson 	UNP_LOCK();
33140f2ac28SRobert Watson 	unp = sotounpcb(so);
33240f2ac28SRobert Watson 	if (unp == NULL) {
33340f2ac28SRobert Watson 		UNP_UNLOCK();
33440f2ac28SRobert Watson 		return (EINVAL);
33540f2ac28SRobert Watson 	}
336df8bae1dSRodney W. Grimes 	switch (so->so_type) {
337df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
338a29f300eSGarrett Wollman 		panic("uipc_rcvd DGRAM?");
339df8bae1dSRodney W. Grimes 		/*NOTREACHED*/
340df8bae1dSRodney W. Grimes 
341df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
342fc3fcacfSRobert Watson 		if (unp->unp_conn == NULL)
343df8bae1dSRodney W. Grimes 			break;
344df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
345c9f69064SRobert Watson 		SOCKBUF_LOCK(&so2->so_snd);
346c9f69064SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
347df8bae1dSRodney W. Grimes 		/*
348df8bae1dSRodney W. Grimes 		 * Adjust backpressure on sender
349df8bae1dSRodney W. Grimes 		 * and wakeup any waiting to write.
350df8bae1dSRodney W. Grimes 		 */
351ff8b0106SBrian Feldman 		so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt;
352ff8b0106SBrian Feldman 		unp->unp_mbcnt = so->so_rcv.sb_mbcnt;
3536aef685fSBrian Feldman 		newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc -
3546aef685fSBrian Feldman 		    so->so_rcv.sb_cc;
355f535380cSDon Lewis 		(void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat,
3566aef685fSBrian Feldman 		    newhiwat, RLIM_INFINITY);
357ff8b0106SBrian Feldman 		unp->unp_cc = so->so_rcv.sb_cc;
358c9f69064SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
3591e4d7da7SRobert Watson 		sowwakeup_locked(so2);
360df8bae1dSRodney W. Grimes 		break;
361df8bae1dSRodney W. Grimes 
362df8bae1dSRodney W. Grimes 	default:
363a29f300eSGarrett Wollman 		panic("uipc_rcvd unknown socktype");
364df8bae1dSRodney W. Grimes 	}
3650d9ce3a1SRobert Watson 	UNP_UNLOCK();
366e5aeaa0cSDag-Erling Smørgrav 	return (0);
367a29f300eSGarrett Wollman }
368df8bae1dSRodney W. Grimes 
369a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */
370a29f300eSGarrett Wollman 
371a29f300eSGarrett Wollman static int
37257bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
373b40ce416SJulian Elischer     struct mbuf *control, struct thread *td)
374a29f300eSGarrett Wollman {
375a29f300eSGarrett Wollman 	int error = 0;
37640f2ac28SRobert Watson 	struct unpcb *unp;
377a29f300eSGarrett Wollman 	struct socket *so2;
3786aef685fSBrian Feldman 	u_long newhiwat;
379a29f300eSGarrett Wollman 
38040f2ac28SRobert Watson 	unp = sotounpcb(so);
381fc3fcacfSRobert Watson 	if (unp == NULL) {
382a29f300eSGarrett Wollman 		error = EINVAL;
383a29f300eSGarrett Wollman 		goto release;
384a29f300eSGarrett Wollman 	}
385a29f300eSGarrett Wollman 	if (flags & PRUS_OOB) {
386a29f300eSGarrett Wollman 		error = EOPNOTSUPP;
387a29f300eSGarrett Wollman 		goto release;
388a29f300eSGarrett Wollman 	}
389a29f300eSGarrett Wollman 
390fc3fcacfSRobert Watson 	if (control != NULL && (error = unp_internalize(&control, td)))
391a29f300eSGarrett Wollman 		goto release;
392df8bae1dSRodney W. Grimes 
3930d9ce3a1SRobert Watson 	UNP_LOCK();
39440f2ac28SRobert Watson 	unp = sotounpcb(so);
39540f2ac28SRobert Watson 	if (unp == NULL) {
39640f2ac28SRobert Watson 		UNP_UNLOCK();
39740f2ac28SRobert Watson 		error = EINVAL;
39840f2ac28SRobert Watson 		goto dispose_release;
39940f2ac28SRobert Watson 	}
40040f2ac28SRobert Watson 
401a29f300eSGarrett Wollman 	switch (so->so_type) {
402a29f300eSGarrett Wollman 	case SOCK_DGRAM:
403a29f300eSGarrett Wollman 	{
404e7dd9a10SRobert Watson 		const struct sockaddr *from;
405df8bae1dSRodney W. Grimes 
406fc3fcacfSRobert Watson 		if (nam != NULL) {
407fc3fcacfSRobert Watson 			if (unp->unp_conn != NULL) {
408df8bae1dSRodney W. Grimes 				error = EISCONN;
409df8bae1dSRodney W. Grimes 				break;
410df8bae1dSRodney W. Grimes 			}
411b40ce416SJulian Elischer 			error = unp_connect(so, nam, td);
412df8bae1dSRodney W. Grimes 			if (error)
413df8bae1dSRodney W. Grimes 				break;
414df8bae1dSRodney W. Grimes 		} else {
415fc3fcacfSRobert Watson 			if (unp->unp_conn == NULL) {
416df8bae1dSRodney W. Grimes 				error = ENOTCONN;
417df8bae1dSRodney W. Grimes 				break;
418df8bae1dSRodney W. Grimes 			}
419df8bae1dSRodney W. Grimes 		}
420df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
421fc3fcacfSRobert Watson 		if (unp->unp_addr != NULL)
42257bf258eSGarrett Wollman 			from = (struct sockaddr *)unp->unp_addr;
423df8bae1dSRodney W. Grimes 		else
424df8bae1dSRodney W. Grimes 			from = &sun_noname;
4256a2989fdSMatthew N. Dodd 		if (unp->unp_conn->unp_flags & UNP_WANTCRED)
4266a2989fdSMatthew N. Dodd 			control = unp_addsockcred(td, control);
427a34b7046SRobert Watson 		SOCKBUF_LOCK(&so2->so_rcv);
428a34b7046SRobert Watson 		if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) {
4291e4d7da7SRobert Watson 			sorwakeup_locked(so2);
430fc3fcacfSRobert Watson 			m = NULL;
431fc3fcacfSRobert Watson 			control = NULL;
432e5aeaa0cSDag-Erling Smørgrav 		} else {
433a34b7046SRobert Watson 			SOCKBUF_UNLOCK(&so2->so_rcv);
434df8bae1dSRodney W. Grimes 			error = ENOBUFS;
435e5aeaa0cSDag-Erling Smørgrav 		}
436fc3fcacfSRobert Watson 		if (nam != NULL)
437df8bae1dSRodney W. Grimes 			unp_disconnect(unp);
438df8bae1dSRodney W. Grimes 		break;
439df8bae1dSRodney W. Grimes 	}
440df8bae1dSRodney W. Grimes 
441df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
4426b8fda4dSGarrett Wollman 		/* Connect if not connected yet. */
4436b8fda4dSGarrett Wollman 		/*
4446b8fda4dSGarrett Wollman 		 * Note: A better implementation would complain
445402cc72dSDavid Greenman 		 * if not equal to the peer's address.
4466b8fda4dSGarrett Wollman 		 */
447402cc72dSDavid Greenman 		if ((so->so_state & SS_ISCONNECTED) == 0) {
448fc3fcacfSRobert Watson 			if (nam != NULL) {
449b40ce416SJulian Elischer 				error = unp_connect(so, nam, td);
450402cc72dSDavid Greenman 				if (error)
4516b8fda4dSGarrett Wollman 					break;	/* XXX */
452402cc72dSDavid Greenman 			} else {
453402cc72dSDavid Greenman 				error = ENOTCONN;
454402cc72dSDavid Greenman 				break;
455402cc72dSDavid Greenman 			}
456402cc72dSDavid Greenman 		}
457402cc72dSDavid Greenman 
4587abe2ac2SAlan Cox 		SOCKBUF_LOCK(&so->so_snd);
459c0b99ffaSRobert Watson 		if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
4607abe2ac2SAlan Cox 			SOCKBUF_UNLOCK(&so->so_snd);
461df8bae1dSRodney W. Grimes 			error = EPIPE;
462df8bae1dSRodney W. Grimes 			break;
463df8bae1dSRodney W. Grimes 		}
464fc3fcacfSRobert Watson 		if (unp->unp_conn == NULL)
465a29f300eSGarrett Wollman 			panic("uipc_send connected but no connection?");
466df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
467a34b7046SRobert Watson 		SOCKBUF_LOCK(&so2->so_rcv);
4686a2989fdSMatthew N. Dodd 		if (unp->unp_conn->unp_flags & UNP_WANTCRED) {
4696a2989fdSMatthew N. Dodd 			/*
4706a2989fdSMatthew N. Dodd 			 * Credentials are passed only once on
4716a2989fdSMatthew N. Dodd 			 * SOCK_STREAM.
4726a2989fdSMatthew N. Dodd 			 */
4736a2989fdSMatthew N. Dodd 			unp->unp_conn->unp_flags &= ~UNP_WANTCRED;
4746a2989fdSMatthew N. Dodd 			control = unp_addsockcred(td, control);
4756a2989fdSMatthew N. Dodd 		}
476df8bae1dSRodney W. Grimes 		/*
477df8bae1dSRodney W. Grimes 		 * Send to paired receive port, and then reduce
478df8bae1dSRodney W. Grimes 		 * send buffer hiwater marks to maintain backpressure.
479df8bae1dSRodney W. Grimes 		 * Wake up readers.
480df8bae1dSRodney W. Grimes 		 */
481fc3fcacfSRobert Watson 		if (control != NULL) {
482a34b7046SRobert Watson 			if (sbappendcontrol_locked(&so2->so_rcv, m, control))
483fc3fcacfSRobert Watson 				control = NULL;
484e5aeaa0cSDag-Erling Smørgrav 		} else {
485a34b7046SRobert Watson 			sbappend_locked(&so2->so_rcv, m);
486e5aeaa0cSDag-Erling Smørgrav 		}
487ff8b0106SBrian Feldman 		so->so_snd.sb_mbmax -=
488ff8b0106SBrian Feldman 			so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt;
489ff8b0106SBrian Feldman 		unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt;
4906aef685fSBrian Feldman 		newhiwat = so->so_snd.sb_hiwat -
4916aef685fSBrian Feldman 		    (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc);
492f535380cSDon Lewis 		(void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat,
4936aef685fSBrian Feldman 		    newhiwat, RLIM_INFINITY);
4947abe2ac2SAlan Cox 		SOCKBUF_UNLOCK(&so->so_snd);
495ff8b0106SBrian Feldman 		unp->unp_conn->unp_cc = so2->so_rcv.sb_cc;
4961e4d7da7SRobert Watson 		sorwakeup_locked(so2);
497fc3fcacfSRobert Watson 		m = NULL;
498df8bae1dSRodney W. Grimes 		break;
499df8bae1dSRodney W. Grimes 
500df8bae1dSRodney W. Grimes 	default:
501a29f300eSGarrett Wollman 		panic("uipc_send unknown socktype");
502df8bae1dSRodney W. Grimes 	}
503a29f300eSGarrett Wollman 
5046b8fda4dSGarrett Wollman 	/*
5056b8fda4dSGarrett Wollman 	 * SEND_EOF is equivalent to a SEND followed by
5066b8fda4dSGarrett Wollman 	 * a SHUTDOWN.
5076b8fda4dSGarrett Wollman 	 */
508a29f300eSGarrett Wollman 	if (flags & PRUS_EOF) {
5096b8fda4dSGarrett Wollman 		socantsendmore(so);
5106b8fda4dSGarrett Wollman 		unp_shutdown(unp);
5116b8fda4dSGarrett Wollman 	}
5120d9ce3a1SRobert Watson 	UNP_UNLOCK();
513df8bae1dSRodney W. Grimes 
51440f2ac28SRobert Watson dispose_release:
515fc3fcacfSRobert Watson 	if (control != NULL && error != 0)
516bd508d39SDon Lewis 		unp_dispose(control);
517bd508d39SDon Lewis 
518a29f300eSGarrett Wollman release:
519fc3fcacfSRobert Watson 	if (control != NULL)
520a29f300eSGarrett Wollman 		m_freem(control);
521fc3fcacfSRobert Watson 	if (m != NULL)
522a29f300eSGarrett Wollman 		m_freem(m);
523e5aeaa0cSDag-Erling Smørgrav 	return (error);
524a29f300eSGarrett Wollman }
525df8bae1dSRodney W. Grimes 
526a29f300eSGarrett Wollman static int
527a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb)
528a29f300eSGarrett Wollman {
52940f2ac28SRobert Watson 	struct unpcb *unp;
530a29f300eSGarrett Wollman 	struct socket *so2;
531a29f300eSGarrett Wollman 
5320d9ce3a1SRobert Watson 	UNP_LOCK();
53340f2ac28SRobert Watson 	unp = sotounpcb(so);
53440f2ac28SRobert Watson 	if (unp == NULL) {
53540f2ac28SRobert Watson 		UNP_UNLOCK();
53640f2ac28SRobert Watson 		return (EINVAL);
53740f2ac28SRobert Watson 	}
538a29f300eSGarrett Wollman 	sb->st_blksize = so->so_snd.sb_hiwat;
539fc3fcacfSRobert Watson 	if (so->so_type == SOCK_STREAM && unp->unp_conn != NULL) {
540df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
541a29f300eSGarrett Wollman 		sb->st_blksize += so2->so_rcv.sb_cc;
542df8bae1dSRodney W. Grimes 	}
543f3732fd1SPoul-Henning Kamp 	sb->st_dev = NODEV;
544df8bae1dSRodney W. Grimes 	if (unp->unp_ino == 0)
5456f782c46SJeffrey Hsu 		unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino;
546a29f300eSGarrett Wollman 	sb->st_ino = unp->unp_ino;
5470d9ce3a1SRobert Watson 	UNP_UNLOCK();
548df8bae1dSRodney W. Grimes 	return (0);
549a29f300eSGarrett Wollman }
550df8bae1dSRodney W. Grimes 
551a29f300eSGarrett Wollman static int
552a29f300eSGarrett Wollman uipc_shutdown(struct socket *so)
553a29f300eSGarrett Wollman {
55440f2ac28SRobert Watson 	struct unpcb *unp;
555df8bae1dSRodney W. Grimes 
5560d9ce3a1SRobert Watson 	UNP_LOCK();
55740f2ac28SRobert Watson 	unp = sotounpcb(so);
55840f2ac28SRobert Watson 	if (unp == NULL) {
55940f2ac28SRobert Watson 		UNP_UNLOCK();
56040f2ac28SRobert Watson 		return (EINVAL);
56140f2ac28SRobert Watson 	}
562a29f300eSGarrett Wollman 	socantsendmore(so);
563a29f300eSGarrett Wollman 	unp_shutdown(unp);
5640d9ce3a1SRobert Watson 	UNP_UNLOCK();
565e5aeaa0cSDag-Erling Smørgrav 	return (0);
566a29f300eSGarrett Wollman }
567df8bae1dSRodney W. Grimes 
568a29f300eSGarrett Wollman static int
56957bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam)
570a29f300eSGarrett Wollman {
57140f2ac28SRobert Watson 	struct unpcb *unp;
5720d9ce3a1SRobert Watson 	const struct sockaddr *sa;
573a29f300eSGarrett Wollman 
5740d9ce3a1SRobert Watson 	*nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
5750d9ce3a1SRobert Watson 	UNP_LOCK();
57640f2ac28SRobert Watson 	unp = sotounpcb(so);
57740f2ac28SRobert Watson 	if (unp == NULL) {
57840f2ac28SRobert Watson 		UNP_UNLOCK();
57940f2ac28SRobert Watson 		free(*nam, M_SONAME);
58040f2ac28SRobert Watson 		*nam = NULL;
58140f2ac28SRobert Watson 		return (EINVAL);
58240f2ac28SRobert Watson 	}
583fc3fcacfSRobert Watson 	if (unp->unp_addr != NULL)
5840d9ce3a1SRobert Watson 		sa = (struct sockaddr *) unp->unp_addr;
58583f3198bSThomas Moestl 	else
5860d9ce3a1SRobert Watson 		sa = &sun_noname;
5870d9ce3a1SRobert Watson 	bcopy(sa, *nam, sa->sa_len);
5880d9ce3a1SRobert Watson 	UNP_UNLOCK();
589e5aeaa0cSDag-Erling Smørgrav 	return (0);
590df8bae1dSRodney W. Grimes }
591a29f300eSGarrett Wollman 
592a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = {
593756d52a1SPoul-Henning Kamp 	.pru_abort = 		uipc_abort,
594756d52a1SPoul-Henning Kamp 	.pru_accept =		uipc_accept,
595756d52a1SPoul-Henning Kamp 	.pru_attach =		uipc_attach,
596756d52a1SPoul-Henning Kamp 	.pru_bind =		uipc_bind,
597756d52a1SPoul-Henning Kamp 	.pru_connect =		uipc_connect,
598756d52a1SPoul-Henning Kamp 	.pru_connect2 =		uipc_connect2,
599756d52a1SPoul-Henning Kamp 	.pru_detach =		uipc_detach,
600756d52a1SPoul-Henning Kamp 	.pru_disconnect =	uipc_disconnect,
601756d52a1SPoul-Henning Kamp 	.pru_listen =		uipc_listen,
602756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		uipc_peeraddr,
603756d52a1SPoul-Henning Kamp 	.pru_rcvd =		uipc_rcvd,
604756d52a1SPoul-Henning Kamp 	.pru_send =		uipc_send,
605756d52a1SPoul-Henning Kamp 	.pru_sense =		uipc_sense,
606756d52a1SPoul-Henning Kamp 	.pru_shutdown =		uipc_shutdown,
607756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		uipc_sockaddr,
608756d52a1SPoul-Henning Kamp 	.pru_sosend =		sosend,
609756d52a1SPoul-Henning Kamp 	.pru_soreceive =	soreceive,
610756d52a1SPoul-Henning Kamp 	.pru_sopoll =		sopoll,
611a29f300eSGarrett Wollman };
612df8bae1dSRodney W. Grimes 
6130c1bb4fbSDima Dorfman int
614892af6b9SRobert Watson uipc_ctloutput(struct socket *so, struct sockopt *sopt)
6150c1bb4fbSDima Dorfman {
61640f2ac28SRobert Watson 	struct unpcb *unp;
6170d9ce3a1SRobert Watson 	struct xucred xu;
6186a2989fdSMatthew N. Dodd 	int error, optval;
6196a2989fdSMatthew N. Dodd 
62096a041b5SMatthew N. Dodd 	if (sopt->sopt_level != 0)
62196a041b5SMatthew N. Dodd 		return (EINVAL);
62296a041b5SMatthew N. Dodd 
6236a2989fdSMatthew N. Dodd 	UNP_LOCK();
6246a2989fdSMatthew N. Dodd 	unp = sotounpcb(so);
6256a2989fdSMatthew N. Dodd 	if (unp == NULL) {
6266a2989fdSMatthew N. Dodd 		UNP_UNLOCK();
6276a2989fdSMatthew N. Dodd 		return (EINVAL);
6286a2989fdSMatthew N. Dodd 	}
6296a2989fdSMatthew N. Dodd 	error = 0;
6300c1bb4fbSDima Dorfman 
6310c1bb4fbSDima Dorfman 	switch (sopt->sopt_dir) {
6320c1bb4fbSDima Dorfman 	case SOPT_GET:
6330c1bb4fbSDima Dorfman 		switch (sopt->sopt_name) {
6340c1bb4fbSDima Dorfman 		case LOCAL_PEERCRED:
6350c1bb4fbSDima Dorfman 			if (unp->unp_flags & UNP_HAVEPC)
6360d9ce3a1SRobert Watson 				xu = unp->unp_peercred;
6370c1bb4fbSDima Dorfman 			else {
6380c1bb4fbSDima Dorfman 				if (so->so_type == SOCK_STREAM)
6390c1bb4fbSDima Dorfman 					error = ENOTCONN;
6400c1bb4fbSDima Dorfman 				else
6410c1bb4fbSDima Dorfman 					error = EINVAL;
6420c1bb4fbSDima Dorfman 			}
6430d9ce3a1SRobert Watson 			if (error == 0)
6440d9ce3a1SRobert Watson 				error = sooptcopyout(sopt, &xu, sizeof(xu));
6450c1bb4fbSDima Dorfman 			break;
6466a2989fdSMatthew N. Dodd 		case LOCAL_CREDS:
6476a2989fdSMatthew N. Dodd 			optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0;
6486a2989fdSMatthew N. Dodd 			error = sooptcopyout(sopt, &optval, sizeof(optval));
6496a2989fdSMatthew N. Dodd 			break;
6506a2989fdSMatthew N. Dodd 		case LOCAL_CONNWAIT:
6516a2989fdSMatthew N. Dodd 			optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0;
6526a2989fdSMatthew N. Dodd 			error = sooptcopyout(sopt, &optval, sizeof(optval));
6536a2989fdSMatthew N. Dodd 			break;
6540c1bb4fbSDima Dorfman 		default:
6550c1bb4fbSDima Dorfman 			error = EOPNOTSUPP;
6560c1bb4fbSDima Dorfman 			break;
6570c1bb4fbSDima Dorfman 		}
6580c1bb4fbSDima Dorfman 		break;
6590c1bb4fbSDima Dorfman 	case SOPT_SET:
6606a2989fdSMatthew N. Dodd 		switch (sopt->sopt_name) {
6616a2989fdSMatthew N. Dodd 		case LOCAL_CREDS:
6626a2989fdSMatthew N. Dodd 		case LOCAL_CONNWAIT:
6636a2989fdSMatthew N. Dodd 			error = sooptcopyin(sopt, &optval, sizeof(optval),
6646a2989fdSMatthew N. Dodd 					    sizeof(optval));
6656a2989fdSMatthew N. Dodd 			if (error)
6666a2989fdSMatthew N. Dodd 				break;
6676a2989fdSMatthew N. Dodd 
6686a2989fdSMatthew N. Dodd #define	OPTSET(bit) \
6696a2989fdSMatthew N. Dodd 	if (optval) \
6706a2989fdSMatthew N. Dodd 		unp->unp_flags |= bit; \
6716a2989fdSMatthew N. Dodd 	else \
6726a2989fdSMatthew N. Dodd 		unp->unp_flags &= ~bit;
6736a2989fdSMatthew N. Dodd 
6746a2989fdSMatthew N. Dodd 			switch (sopt->sopt_name) {
6756a2989fdSMatthew N. Dodd 			case LOCAL_CREDS:
6766a2989fdSMatthew N. Dodd 				OPTSET(UNP_WANTCRED);
6776a2989fdSMatthew N. Dodd 				break;
6786a2989fdSMatthew N. Dodd 			case LOCAL_CONNWAIT:
6796a2989fdSMatthew N. Dodd 				OPTSET(UNP_CONNWAIT);
6806a2989fdSMatthew N. Dodd 				break;
6816a2989fdSMatthew N. Dodd 			default:
6826a2989fdSMatthew N. Dodd 				break;
6836a2989fdSMatthew N. Dodd 			}
6846a2989fdSMatthew N. Dodd 			break;
6856a2989fdSMatthew N. Dodd #undef	OPTSET
6866a2989fdSMatthew N. Dodd 		default:
6876a2989fdSMatthew N. Dodd 			error = ENOPROTOOPT;
6886a2989fdSMatthew N. Dodd 			break;
6896a2989fdSMatthew N. Dodd 		}
690abb886faSMatthew N. Dodd 		break;
6910c1bb4fbSDima Dorfman 	default:
6920c1bb4fbSDima Dorfman 		error = EOPNOTSUPP;
6930c1bb4fbSDima Dorfman 		break;
6940c1bb4fbSDima Dorfman 	}
6956a2989fdSMatthew N. Dodd 	UNP_UNLOCK();
6960c1bb4fbSDima Dorfman 	return (error);
6970c1bb4fbSDima Dorfman }
6980c1bb4fbSDima Dorfman 
699df8bae1dSRodney W. Grimes /*
700df8bae1dSRodney W. Grimes  * Both send and receive buffers are allocated PIPSIZ bytes of buffering
701df8bae1dSRodney W. Grimes  * for stream sockets, although the total for sender and receiver is
702df8bae1dSRodney W. Grimes  * actually only PIPSIZ.
703df8bae1dSRodney W. Grimes  * Datagram sockets really use the sendspace as the maximum datagram size,
704df8bae1dSRodney W. Grimes  * and don't really want to reserve the sendspace.  Their recvspace should
705df8bae1dSRodney W. Grimes  * be large enough for at least one max-size datagram plus address.
706df8bae1dSRodney W. Grimes  */
7075dce41c5SJohn Dyson #ifndef PIPSIZ
7085dce41c5SJohn Dyson #define	PIPSIZ	8192
7095dce41c5SJohn Dyson #endif
710f708ef1bSPoul-Henning Kamp static u_long	unpst_sendspace = PIPSIZ;
711f708ef1bSPoul-Henning Kamp static u_long	unpst_recvspace = PIPSIZ;
712f708ef1bSPoul-Henning Kamp static u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
713f708ef1bSPoul-Henning Kamp static u_long	unpdg_recvspace = 4*1024;
714df8bae1dSRodney W. Grimes 
715f708ef1bSPoul-Henning Kamp static int	unp_rights;			/* file descriptors in flight */
716df8bae1dSRodney W. Grimes 
717ce02431fSDoug Rabson SYSCTL_DECL(_net_local_stream);
718639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
719639acc13SGarrett Wollman 	   &unpst_sendspace, 0, "");
720639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
721639acc13SGarrett Wollman 	   &unpst_recvspace, 0, "");
722ce02431fSDoug Rabson SYSCTL_DECL(_net_local_dgram);
723639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
724639acc13SGarrett Wollman 	   &unpdg_sendspace, 0, "");
725639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
726639acc13SGarrett Wollman 	   &unpdg_recvspace, 0, "");
727ce02431fSDoug Rabson SYSCTL_DECL(_net_local);
728639acc13SGarrett Wollman SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, "");
729639acc13SGarrett Wollman 
730f708ef1bSPoul-Henning Kamp static int
731892af6b9SRobert Watson unp_attach(struct socket *so)
732df8bae1dSRodney W. Grimes {
733892af6b9SRobert Watson 	struct unpcb *unp;
734df8bae1dSRodney W. Grimes 	int error;
735df8bae1dSRodney W. Grimes 
736df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
737df8bae1dSRodney W. Grimes 		switch (so->so_type) {
738df8bae1dSRodney W. Grimes 
739df8bae1dSRodney W. Grimes 		case SOCK_STREAM:
740df8bae1dSRodney W. Grimes 			error = soreserve(so, unpst_sendspace, unpst_recvspace);
741df8bae1dSRodney W. Grimes 			break;
742df8bae1dSRodney W. Grimes 
743df8bae1dSRodney W. Grimes 		case SOCK_DGRAM:
744df8bae1dSRodney W. Grimes 			error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
745df8bae1dSRodney W. Grimes 			break;
746df8bae1dSRodney W. Grimes 
747df8bae1dSRodney W. Grimes 		default:
748df8bae1dSRodney W. Grimes 			panic("unp_attach");
749df8bae1dSRodney W. Grimes 		}
750df8bae1dSRodney W. Grimes 		if (error)
751df8bae1dSRodney W. Grimes 			return (error);
752df8bae1dSRodney W. Grimes 	}
753d664e4faSRobert Watson 	unp = uma_zalloc(unp_zone, M_WAITOK | M_ZERO);
75457bf258eSGarrett Wollman 	if (unp == NULL)
755df8bae1dSRodney W. Grimes 		return (ENOBUFS);
75698271db4SGarrett Wollman 	LIST_INIT(&unp->unp_refs);
757df8bae1dSRodney W. Grimes 	unp->unp_socket = so;
7587301cf23SRobert Watson 	so->so_pcb = unp;
7590d9ce3a1SRobert Watson 
7600d9ce3a1SRobert Watson 	UNP_LOCK();
7610d9ce3a1SRobert Watson 	unp->unp_gencnt = ++unp_gencnt;
7620d9ce3a1SRobert Watson 	unp_count++;
76398271db4SGarrett Wollman 	LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead
76498271db4SGarrett Wollman 			 : &unp_shead, unp, unp_link);
7650d9ce3a1SRobert Watson 	UNP_UNLOCK();
7660d9ce3a1SRobert Watson 
767df8bae1dSRodney W. Grimes 	return (0);
768df8bae1dSRodney W. Grimes }
769df8bae1dSRodney W. Grimes 
770f708ef1bSPoul-Henning Kamp static void
771892af6b9SRobert Watson unp_detach(struct unpcb *unp)
772df8bae1dSRodney W. Grimes {
7730d9ce3a1SRobert Watson 	struct vnode *vp;
7740d9ce3a1SRobert Watson 
7750d9ce3a1SRobert Watson 	UNP_LOCK_ASSERT();
7760d9ce3a1SRobert Watson 
77798271db4SGarrett Wollman 	LIST_REMOVE(unp, unp_link);
77898271db4SGarrett Wollman 	unp->unp_gencnt = ++unp_gencnt;
77998271db4SGarrett Wollman 	--unp_count;
7800d9ce3a1SRobert Watson 	if ((vp = unp->unp_vnode) != NULL) {
7810d9ce3a1SRobert Watson 		/*
7820d9ce3a1SRobert Watson 		 * XXXRW: should v_socket be frobbed only while holding
7830d9ce3a1SRobert Watson 		 * Giant?
7840d9ce3a1SRobert Watson 		 */
785fc3fcacfSRobert Watson 		unp->unp_vnode->v_socket = NULL;
786fc3fcacfSRobert Watson 		unp->unp_vnode = NULL;
787df8bae1dSRodney W. Grimes 	}
788fc3fcacfSRobert Watson 	if (unp->unp_conn != NULL)
789df8bae1dSRodney W. Grimes 		unp_disconnect(unp);
7900d9ce3a1SRobert Watson 	while (!LIST_EMPTY(&unp->unp_refs)) {
7910d9ce3a1SRobert Watson 		struct unpcb *ref = LIST_FIRST(&unp->unp_refs);
7920d9ce3a1SRobert Watson 		unp_drop(ref, ECONNRESET);
7930d9ce3a1SRobert Watson 	}
794df8bae1dSRodney W. Grimes 	soisdisconnected(unp->unp_socket);
795fc3fcacfSRobert Watson 	unp->unp_socket->so_pcb = NULL;
796df8bae1dSRodney W. Grimes 	if (unp_rights) {
797df8bae1dSRodney W. Grimes 		/*
798df8bae1dSRodney W. Grimes 		 * Normally the receive buffer is flushed later,
799df8bae1dSRodney W. Grimes 		 * in sofree, but if our receive buffer holds references
800df8bae1dSRodney W. Grimes 		 * to descriptors that are now garbage, we will dispose
801df8bae1dSRodney W. Grimes 		 * of those descriptor references after the garbage collector
802df8bae1dSRodney W. Grimes 		 * gets them (resulting in a "panic: closef: count < 0").
803df8bae1dSRodney W. Grimes 		 */
804df8bae1dSRodney W. Grimes 		sorflush(unp->unp_socket);
805161a0c7cSRobert Watson 		unp_gc();	/* Will unlock UNP. */
806161a0c7cSRobert Watson 	} else
807a5993a97SRobert Watson 		UNP_UNLOCK();
808161a0c7cSRobert Watson 	UNP_UNLOCK_ASSERT();
809fc3fcacfSRobert Watson 	if (unp->unp_addr != NULL)
81057bf258eSGarrett Wollman 		FREE(unp->unp_addr, M_SONAME);
8119e9d298aSJeff Roberson 	uma_zfree(unp_zone, unp);
8120d9ce3a1SRobert Watson 	if (vp) {
8130d9ce3a1SRobert Watson 		mtx_lock(&Giant);
8140d9ce3a1SRobert Watson 		vrele(vp);
8150d9ce3a1SRobert Watson 		mtx_unlock(&Giant);
8160d9ce3a1SRobert Watson 	}
817df8bae1dSRodney W. Grimes }
818df8bae1dSRodney W. Grimes 
819f708ef1bSPoul-Henning Kamp static int
820892af6b9SRobert Watson unp_bind(struct unpcb *unp, struct sockaddr *nam, struct thread *td)
821df8bae1dSRodney W. Grimes {
82257bf258eSGarrett Wollman 	struct sockaddr_un *soun = (struct sockaddr_un *)nam;
823f2a2857bSKirk McKusick 	struct vnode *vp;
824f2a2857bSKirk McKusick 	struct mount *mp;
825df8bae1dSRodney W. Grimes 	struct vattr vattr;
82657bf258eSGarrett Wollman 	int error, namelen;
827df8bae1dSRodney W. Grimes 	struct nameidata nd;
8288f364875SJulian Elischer 	char *buf;
829df8bae1dSRodney W. Grimes 
83040f2ac28SRobert Watson 	UNP_LOCK_ASSERT();
83140f2ac28SRobert Watson 
8320d9ce3a1SRobert Watson 	/*
8330d9ce3a1SRobert Watson 	 * XXXRW: This test-and-set of unp_vnode is non-atomic; the
8340d9ce3a1SRobert Watson 	 * unlocked read here is fine, but the value of unp_vnode needs
8350d9ce3a1SRobert Watson 	 * to be tested again after we do all the lookups to see if the
8360d9ce3a1SRobert Watson 	 * pcb is still unbound?
8370d9ce3a1SRobert Watson 	 */
838df8bae1dSRodney W. Grimes 	if (unp->unp_vnode != NULL)
839df8bae1dSRodney W. Grimes 		return (EINVAL);
84055c85568SRobert Drehmel 
84157bf258eSGarrett Wollman 	namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
84257bf258eSGarrett Wollman 	if (namelen <= 0)
843e5aeaa0cSDag-Erling Smørgrav 		return (EINVAL);
84455c85568SRobert Drehmel 
84540f2ac28SRobert Watson 	UNP_UNLOCK();
84640f2ac28SRobert Watson 
847a163d034SWarner Losh 	buf = malloc(namelen + 1, M_TEMP, M_WAITOK);
84855c85568SRobert Drehmel 	strlcpy(buf, soun->sun_path, namelen + 1);
84955c85568SRobert Drehmel 
8500d9ce3a1SRobert Watson 	mtx_lock(&Giant);
851f2a2857bSKirk McKusick restart:
8520d9ce3a1SRobert Watson 	mtx_assert(&Giant, MA_OWNED);
853b65f6f6bSRobert Watson 	NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME, UIO_SYSSPACE,
854b40ce416SJulian Elischer 	    buf, td);
855df8bae1dSRodney W. Grimes /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
856797f2d22SPoul-Henning Kamp 	error = namei(&nd);
8570d9ce3a1SRobert Watson 	if (error)
8580d9ce3a1SRobert Watson 		goto done;
859df8bae1dSRodney W. Grimes 	vp = nd.ni_vp;
860f2a2857bSKirk McKusick 	if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
861762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
862df8bae1dSRodney W. Grimes 		if (nd.ni_dvp == vp)
863df8bae1dSRodney W. Grimes 			vrele(nd.ni_dvp);
864df8bae1dSRodney W. Grimes 		else
865df8bae1dSRodney W. Grimes 			vput(nd.ni_dvp);
866f2a2857bSKirk McKusick 		if (vp != NULL) {
867df8bae1dSRodney W. Grimes 			vrele(vp);
8680d9ce3a1SRobert Watson 			error = EADDRINUSE;
8690d9ce3a1SRobert Watson 			goto done;
870df8bae1dSRodney W. Grimes 		}
8718f364875SJulian Elischer 		error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
8720d9ce3a1SRobert Watson 		if (error)
8730d9ce3a1SRobert Watson 			goto done;
874f2a2857bSKirk McKusick 		goto restart;
875f2a2857bSKirk McKusick 	}
876df8bae1dSRodney W. Grimes 	VATTR_NULL(&vattr);
877df8bae1dSRodney W. Grimes 	vattr.va_type = VSOCK;
878b40ce416SJulian Elischer 	vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask);
8796ea48a90SRobert Watson #ifdef MAC
8806ea48a90SRobert Watson 	error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
8816ea48a90SRobert Watson 	    &vattr);
8826151efaaSRobert Watson #endif
8836ea48a90SRobert Watson 	if (error == 0) {
884a854ed98SJohn Baldwin 		VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
8857be2d300SMike Smith 		error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
8866ea48a90SRobert Watson 	}
887762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
8887be2d300SMike Smith 	vput(nd.ni_dvp);
889c364c823SRobert Watson 	if (error) {
890c364c823SRobert Watson 		vn_finished_write(mp);
8910d9ce3a1SRobert Watson 		goto done;
892c364c823SRobert Watson 	}
893df8bae1dSRodney W. Grimes 	vp = nd.ni_vp;
8940d9ce3a1SRobert Watson 	ASSERT_VOP_LOCKED(vp, "unp_bind");
8950d9ce3a1SRobert Watson 	soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK);
8960d9ce3a1SRobert Watson 	UNP_LOCK();
897df8bae1dSRodney W. Grimes 	vp->v_socket = unp->unp_socket;
898df8bae1dSRodney W. Grimes 	unp->unp_vnode = vp;
8990d9ce3a1SRobert Watson 	unp->unp_addr = soun;
9000d9ce3a1SRobert Watson 	UNP_UNLOCK();
901b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
902f2a2857bSKirk McKusick 	vn_finished_write(mp);
9030d9ce3a1SRobert Watson done:
9040d9ce3a1SRobert Watson 	mtx_unlock(&Giant);
9058f364875SJulian Elischer 	free(buf, M_TEMP);
90640f2ac28SRobert Watson 	UNP_LOCK();
9070d9ce3a1SRobert Watson 	return (error);
908df8bae1dSRodney W. Grimes }
909df8bae1dSRodney W. Grimes 
910f708ef1bSPoul-Henning Kamp static int
911892af6b9SRobert Watson unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
912df8bae1dSRodney W. Grimes {
913892af6b9SRobert Watson 	struct sockaddr_un *soun = (struct sockaddr_un *)nam;
914892af6b9SRobert Watson 	struct vnode *vp;
915892af6b9SRobert Watson 	struct socket *so2, *so3;
916b295bdcdSRobert Watson 	struct unpcb *unp, *unp2, *unp3;
91757bf258eSGarrett Wollman 	int error, len;
918df8bae1dSRodney W. Grimes 	struct nameidata nd;
91957bf258eSGarrett Wollman 	char buf[SOCK_MAXADDRLEN];
9200d9ce3a1SRobert Watson 	struct sockaddr *sa;
9210d9ce3a1SRobert Watson 
9220d9ce3a1SRobert Watson 	UNP_LOCK_ASSERT();
923b295bdcdSRobert Watson 	unp = sotounpcb(so);
924df8bae1dSRodney W. Grimes 
92557bf258eSGarrett Wollman 	len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
92657bf258eSGarrett Wollman 	if (len <= 0)
927e5aeaa0cSDag-Erling Smørgrav 		return (EINVAL);
92855c85568SRobert Drehmel 	strlcpy(buf, soun->sun_path, len + 1);
9290d9ce3a1SRobert Watson 	UNP_UNLOCK();
9300d9ce3a1SRobert Watson 	sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
9310d9ce3a1SRobert Watson 	mtx_lock(&Giant);
932b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td);
933797f2d22SPoul-Henning Kamp 	error = namei(&nd);
934797f2d22SPoul-Henning Kamp 	if (error)
9350d9ce3a1SRobert Watson 		vp = NULL;
9360d9ce3a1SRobert Watson 	else
937df8bae1dSRodney W. Grimes 		vp = nd.ni_vp;
9380d9ce3a1SRobert Watson 	ASSERT_VOP_LOCKED(vp, "unp_connect");
939762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
9400d9ce3a1SRobert Watson 	if (error)
9410d9ce3a1SRobert Watson 		goto bad;
9420d9ce3a1SRobert Watson 
943df8bae1dSRodney W. Grimes 	if (vp->v_type != VSOCK) {
944df8bae1dSRodney W. Grimes 		error = ENOTSOCK;
945df8bae1dSRodney W. Grimes 		goto bad;
946df8bae1dSRodney W. Grimes 	}
947a854ed98SJohn Baldwin 	error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td);
948797f2d22SPoul-Henning Kamp 	if (error)
949df8bae1dSRodney W. Grimes 		goto bad;
9502260c03dSRobert Watson 	mtx_unlock(&Giant);
9512260c03dSRobert Watson 	UNP_LOCK();
952b295bdcdSRobert Watson 	unp = sotounpcb(so);
953b295bdcdSRobert Watson 	if (unp == NULL) {
954b295bdcdSRobert Watson 		error = EINVAL;
955b295bdcdSRobert Watson 		goto bad2;
956b295bdcdSRobert Watson 	}
957df8bae1dSRodney W. Grimes 	so2 = vp->v_socket;
958fc3fcacfSRobert Watson 	if (so2 == NULL) {
959df8bae1dSRodney W. Grimes 		error = ECONNREFUSED;
9602260c03dSRobert Watson 		goto bad2;
961df8bae1dSRodney W. Grimes 	}
962df8bae1dSRodney W. Grimes 	if (so->so_type != so2->so_type) {
963df8bae1dSRodney W. Grimes 		error = EPROTOTYPE;
9642260c03dSRobert Watson 		goto bad2;
965df8bae1dSRodney W. Grimes 	}
966df8bae1dSRodney W. Grimes 	if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
9670d9ce3a1SRobert Watson 		if (so2->so_options & SO_ACCEPTCONN) {
9680d9ce3a1SRobert Watson 			/*
9690d9ce3a1SRobert Watson 			 * NB: drop locks here so unp_attach is entered
9700d9ce3a1SRobert Watson 			 *     w/o locks; this avoids a recursive lock
9710d9ce3a1SRobert Watson 			 *     of the head and holding sleep locks across
9720d9ce3a1SRobert Watson 			 *     a (potentially) blocking malloc.
9730d9ce3a1SRobert Watson 			 */
9740d9ce3a1SRobert Watson 			UNP_UNLOCK();
9750d9ce3a1SRobert Watson 			so3 = sonewconn(so2, 0);
9760d9ce3a1SRobert Watson 			UNP_LOCK();
9770d9ce3a1SRobert Watson 		} else
9780d9ce3a1SRobert Watson 			so3 = NULL;
9790d9ce3a1SRobert Watson 		if (so3 == NULL) {
980df8bae1dSRodney W. Grimes 			error = ECONNREFUSED;
9810d9ce3a1SRobert Watson 			goto bad2;
982df8bae1dSRodney W. Grimes 		}
9830c1bb4fbSDima Dorfman 		unp = sotounpcb(so);
984df8bae1dSRodney W. Grimes 		unp2 = sotounpcb(so2);
985df8bae1dSRodney W. Grimes 		unp3 = sotounpcb(so3);
9860d9ce3a1SRobert Watson 		if (unp2->unp_addr != NULL) {
9870d9ce3a1SRobert Watson 			bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len);
9880d9ce3a1SRobert Watson 			unp3->unp_addr = (struct sockaddr_un *) sa;
9890d9ce3a1SRobert Watson 			sa = NULL;
9900d9ce3a1SRobert Watson 		}
9910c1bb4fbSDima Dorfman 		/*
9920c1bb4fbSDima Dorfman 		 * unp_peercred management:
9930c1bb4fbSDima Dorfman 		 *
9940c1bb4fbSDima Dorfman 		 * The connecter's (client's) credentials are copied
9950c1bb4fbSDima Dorfman 		 * from its process structure at the time of connect()
9960c1bb4fbSDima Dorfman 		 * (which is now).
9970c1bb4fbSDima Dorfman 		 */
998a854ed98SJohn Baldwin 		cru2x(td->td_ucred, &unp3->unp_peercred);
9990c1bb4fbSDima Dorfman 		unp3->unp_flags |= UNP_HAVEPC;
10000c1bb4fbSDima Dorfman 		/*
10010c1bb4fbSDima Dorfman 		 * The receiver's (server's) credentials are copied
10020c1bb4fbSDima Dorfman 		 * from the unp_peercred member of socket on which the
10030c1bb4fbSDima Dorfman 		 * former called listen(); unp_listen() cached that
10040c1bb4fbSDima Dorfman 		 * process's credentials at that time so we can use
10050c1bb4fbSDima Dorfman 		 * them now.
10060c1bb4fbSDima Dorfman 		 */
10070c1bb4fbSDima Dorfman 		KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED,
10080c1bb4fbSDima Dorfman 		    ("unp_connect: listener without cached peercred"));
10090c1bb4fbSDima Dorfman 		memcpy(&unp->unp_peercred, &unp2->unp_peercred,
10100c1bb4fbSDima Dorfman 		    sizeof(unp->unp_peercred));
10110c1bb4fbSDima Dorfman 		unp->unp_flags |= UNP_HAVEPC;
1012335654d7SRobert Watson #ifdef MAC
1013310e7cebSRobert Watson 		SOCK_LOCK(so);
1014335654d7SRobert Watson 		mac_set_socket_peer_from_socket(so, so3);
1015335654d7SRobert Watson 		mac_set_socket_peer_from_socket(so3, so);
1016310e7cebSRobert Watson 		SOCK_UNLOCK(so);
1017335654d7SRobert Watson #endif
10180c1bb4fbSDima Dorfman 
1019df8bae1dSRodney W. Grimes 		so2 = so3;
1020df8bae1dSRodney W. Grimes 	}
10216a2989fdSMatthew N. Dodd 	error = unp_connect2(so, so2, PRU_CONNECT);
10220d9ce3a1SRobert Watson bad2:
10230d9ce3a1SRobert Watson 	UNP_UNLOCK();
10240d9ce3a1SRobert Watson 	mtx_lock(&Giant);
1025df8bae1dSRodney W. Grimes bad:
10260d9ce3a1SRobert Watson 	mtx_assert(&Giant, MA_OWNED);
10270d9ce3a1SRobert Watson 	if (vp != NULL)
1028df8bae1dSRodney W. Grimes 		vput(vp);
10290d9ce3a1SRobert Watson 	mtx_unlock(&Giant);
10300d9ce3a1SRobert Watson 	free(sa, M_SONAME);
10310d9ce3a1SRobert Watson 	UNP_LOCK();
1032df8bae1dSRodney W. Grimes 	return (error);
1033df8bae1dSRodney W. Grimes }
1034df8bae1dSRodney W. Grimes 
1035db48c0d2SRobert Watson static int
10366a2989fdSMatthew N. Dodd unp_connect2(struct socket *so, struct socket *so2, int req)
1037df8bae1dSRodney W. Grimes {
1038892af6b9SRobert Watson 	struct unpcb *unp = sotounpcb(so);
1039892af6b9SRobert Watson 	struct unpcb *unp2;
1040df8bae1dSRodney W. Grimes 
10410d9ce3a1SRobert Watson 	UNP_LOCK_ASSERT();
10420d9ce3a1SRobert Watson 
1043df8bae1dSRodney W. Grimes 	if (so2->so_type != so->so_type)
1044df8bae1dSRodney W. Grimes 		return (EPROTOTYPE);
1045df8bae1dSRodney W. Grimes 	unp2 = sotounpcb(so2);
1046df8bae1dSRodney W. Grimes 	unp->unp_conn = unp2;
1047df8bae1dSRodney W. Grimes 	switch (so->so_type) {
1048df8bae1dSRodney W. Grimes 
1049df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
105098271db4SGarrett Wollman 		LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
1051df8bae1dSRodney W. Grimes 		soisconnected(so);
1052df8bae1dSRodney W. Grimes 		break;
1053df8bae1dSRodney W. Grimes 
1054df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
1055df8bae1dSRodney W. Grimes 		unp2->unp_conn = unp;
10566a2989fdSMatthew N. Dodd 		if (req == PRU_CONNECT &&
10576a2989fdSMatthew N. Dodd 		    ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT))
10586a2989fdSMatthew N. Dodd 			soisconnecting(so);
10596a2989fdSMatthew N. Dodd 		else
1060df8bae1dSRodney W. Grimes 			soisconnected(so);
1061df8bae1dSRodney W. Grimes 		soisconnected(so2);
1062df8bae1dSRodney W. Grimes 		break;
1063df8bae1dSRodney W. Grimes 
1064df8bae1dSRodney W. Grimes 	default:
1065df8bae1dSRodney W. Grimes 		panic("unp_connect2");
1066df8bae1dSRodney W. Grimes 	}
1067df8bae1dSRodney W. Grimes 	return (0);
1068df8bae1dSRodney W. Grimes }
1069df8bae1dSRodney W. Grimes 
1070f708ef1bSPoul-Henning Kamp static void
1071892af6b9SRobert Watson unp_disconnect(struct unpcb *unp)
1072df8bae1dSRodney W. Grimes {
1073892af6b9SRobert Watson 	struct unpcb *unp2 = unp->unp_conn;
10741b2e3b4bSRobert Watson 	struct socket *so;
1075df8bae1dSRodney W. Grimes 
10760d9ce3a1SRobert Watson 	UNP_LOCK_ASSERT();
10770d9ce3a1SRobert Watson 
1078fc3fcacfSRobert Watson 	if (unp2 == NULL)
1079df8bae1dSRodney W. Grimes 		return;
1080fc3fcacfSRobert Watson 	unp->unp_conn = NULL;
1081df8bae1dSRodney W. Grimes 	switch (unp->unp_socket->so_type) {
1082df8bae1dSRodney W. Grimes 
1083df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
108498271db4SGarrett Wollman 		LIST_REMOVE(unp, unp_reflink);
10851b2e3b4bSRobert Watson 		so = unp->unp_socket;
10861b2e3b4bSRobert Watson 		SOCK_LOCK(so);
10871b2e3b4bSRobert Watson 		so->so_state &= ~SS_ISCONNECTED;
10881b2e3b4bSRobert Watson 		SOCK_UNLOCK(so);
1089df8bae1dSRodney W. Grimes 		break;
1090df8bae1dSRodney W. Grimes 
1091df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
1092df8bae1dSRodney W. Grimes 		soisdisconnected(unp->unp_socket);
1093fc3fcacfSRobert Watson 		unp2->unp_conn = NULL;
1094df8bae1dSRodney W. Grimes 		soisdisconnected(unp2->unp_socket);
1095df8bae1dSRodney W. Grimes 		break;
1096df8bae1dSRodney W. Grimes 	}
1097df8bae1dSRodney W. Grimes }
1098df8bae1dSRodney W. Grimes 
1099df8bae1dSRodney W. Grimes #ifdef notdef
110026f9a767SRodney W. Grimes void
1101892af6b9SRobert Watson unp_abort(struct unpcb *unp)
1102df8bae1dSRodney W. Grimes {
1103df8bae1dSRodney W. Grimes 
1104df8bae1dSRodney W. Grimes 	unp_detach(unp);
11054c5bc1caSRobert Watson 	UNP_UNLOCK_ASSERT();
1106df8bae1dSRodney W. Grimes }
1107df8bae1dSRodney W. Grimes #endif
1108df8bae1dSRodney W. Grimes 
11090d9ce3a1SRobert Watson /*
11100d9ce3a1SRobert Watson  * unp_pcblist() assumes that UNIX domain socket memory is never reclaimed
11110d9ce3a1SRobert Watson  * by the zone (UMA_ZONE_NOFREE), and as such potentially stale pointers
11120d9ce3a1SRobert Watson  * are safe to reference.  It first scans the list of struct unpcb's to
11130d9ce3a1SRobert Watson  * generate a pointer list, then it rescans its list one entry at a time to
11140d9ce3a1SRobert Watson  * externalize and copyout.  It checks the generation number to see if a
11150d9ce3a1SRobert Watson  * struct unpcb has been reused, and will skip it if so.
11160d9ce3a1SRobert Watson  */
111798271db4SGarrett Wollman static int
111882d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS)
111998271db4SGarrett Wollman {
1120f5ef029eSPoul-Henning Kamp 	int error, i, n;
112198271db4SGarrett Wollman 	struct unpcb *unp, **unp_list;
112298271db4SGarrett Wollman 	unp_gen_t gencnt;
11238f364875SJulian Elischer 	struct xunpgen *xug;
112498271db4SGarrett Wollman 	struct unp_head *head;
11258f364875SJulian Elischer 	struct xunpcb *xu;
112698271db4SGarrett Wollman 
1127a23d65bfSBruce Evans 	head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead);
112898271db4SGarrett Wollman 
112998271db4SGarrett Wollman 	/*
113098271db4SGarrett Wollman 	 * The process of preparing the PCB list is too time-consuming and
113198271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
113298271db4SGarrett Wollman 	 */
1133fc3fcacfSRobert Watson 	if (req->oldptr == NULL) {
113498271db4SGarrett Wollman 		n = unp_count;
11358f364875SJulian Elischer 		req->oldidx = 2 * (sizeof *xug)
113698271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xunpcb);
1137e5aeaa0cSDag-Erling Smørgrav 		return (0);
113898271db4SGarrett Wollman 	}
113998271db4SGarrett Wollman 
1140fc3fcacfSRobert Watson 	if (req->newptr != NULL)
1141e5aeaa0cSDag-Erling Smørgrav 		return (EPERM);
114298271db4SGarrett Wollman 
114398271db4SGarrett Wollman 	/*
114498271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
114598271db4SGarrett Wollman 	 */
1146a163d034SWarner Losh 	xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK);
11470d9ce3a1SRobert Watson 	UNP_LOCK();
114898271db4SGarrett Wollman 	gencnt = unp_gencnt;
114998271db4SGarrett Wollman 	n = unp_count;
11500d9ce3a1SRobert Watson 	UNP_UNLOCK();
115198271db4SGarrett Wollman 
11528f364875SJulian Elischer 	xug->xug_len = sizeof *xug;
11538f364875SJulian Elischer 	xug->xug_count = n;
11548f364875SJulian Elischer 	xug->xug_gen = gencnt;
11558f364875SJulian Elischer 	xug->xug_sogen = so_gencnt;
11568f364875SJulian Elischer 	error = SYSCTL_OUT(req, xug, sizeof *xug);
11578f364875SJulian Elischer 	if (error) {
11588f364875SJulian Elischer 		free(xug, M_TEMP);
1159e5aeaa0cSDag-Erling Smørgrav 		return (error);
11608f364875SJulian Elischer 	}
116198271db4SGarrett Wollman 
1162a163d034SWarner Losh 	unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
116398271db4SGarrett Wollman 
11640d9ce3a1SRobert Watson 	UNP_LOCK();
11652e3c8fcbSPoul-Henning Kamp 	for (unp = LIST_FIRST(head), i = 0; unp && i < n;
11662e3c8fcbSPoul-Henning Kamp 	     unp = LIST_NEXT(unp, unp_link)) {
11678a7d8cc6SRobert Watson 		if (unp->unp_gencnt <= gencnt) {
1168a854ed98SJohn Baldwin 			if (cr_cansee(req->td->td_ucred,
11698a7d8cc6SRobert Watson 			    unp->unp_socket->so_cred))
11704787fd37SPaul Saab 				continue;
117198271db4SGarrett Wollman 			unp_list[i++] = unp;
117298271db4SGarrett Wollman 		}
11734787fd37SPaul Saab 	}
11740d9ce3a1SRobert Watson 	UNP_UNLOCK();
117598271db4SGarrett Wollman 	n = i;			/* in case we lost some during malloc */
117698271db4SGarrett Wollman 
117798271db4SGarrett Wollman 	error = 0;
1178fe2eee82SColin Percival 	xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO);
117998271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
118098271db4SGarrett Wollman 		unp = unp_list[i];
118198271db4SGarrett Wollman 		if (unp->unp_gencnt <= gencnt) {
11828f364875SJulian Elischer 			xu->xu_len = sizeof *xu;
11838f364875SJulian Elischer 			xu->xu_unpp = unp;
118498271db4SGarrett Wollman 			/*
118598271db4SGarrett Wollman 			 * XXX - need more locking here to protect against
118698271db4SGarrett Wollman 			 * connect/disconnect races for SMP.
118798271db4SGarrett Wollman 			 */
1188fc3fcacfSRobert Watson 			if (unp->unp_addr != NULL)
11898f364875SJulian Elischer 				bcopy(unp->unp_addr, &xu->xu_addr,
119098271db4SGarrett Wollman 				      unp->unp_addr->sun_len);
1191fc3fcacfSRobert Watson 			if (unp->unp_conn != NULL &&
1192fc3fcacfSRobert Watson 			    unp->unp_conn->unp_addr != NULL)
119398271db4SGarrett Wollman 				bcopy(unp->unp_conn->unp_addr,
11948f364875SJulian Elischer 				      &xu->xu_caddr,
119598271db4SGarrett Wollman 				      unp->unp_conn->unp_addr->sun_len);
11968f364875SJulian Elischer 			bcopy(unp, &xu->xu_unp, sizeof *unp);
11978f364875SJulian Elischer 			sotoxsocket(unp->unp_socket, &xu->xu_socket);
11988f364875SJulian Elischer 			error = SYSCTL_OUT(req, xu, sizeof *xu);
119998271db4SGarrett Wollman 		}
120098271db4SGarrett Wollman 	}
12018f364875SJulian Elischer 	free(xu, M_TEMP);
120298271db4SGarrett Wollman 	if (!error) {
120398271db4SGarrett Wollman 		/*
120498271db4SGarrett Wollman 		 * Give the user an updated idea of our state.
120598271db4SGarrett Wollman 		 * If the generation differs from what we told
120698271db4SGarrett Wollman 		 * her before, she knows that something happened
120798271db4SGarrett Wollman 		 * while we were processing this request, and it
120898271db4SGarrett Wollman 		 * might be necessary to retry.
120998271db4SGarrett Wollman 		 */
12108f364875SJulian Elischer 		xug->xug_gen = unp_gencnt;
12118f364875SJulian Elischer 		xug->xug_sogen = so_gencnt;
12128f364875SJulian Elischer 		xug->xug_count = unp_count;
12138f364875SJulian Elischer 		error = SYSCTL_OUT(req, xug, sizeof *xug);
121498271db4SGarrett Wollman 	}
121598271db4SGarrett Wollman 	free(unp_list, M_TEMP);
12168f364875SJulian Elischer 	free(xug, M_TEMP);
1217e5aeaa0cSDag-Erling Smørgrav 	return (error);
121898271db4SGarrett Wollman }
121998271db4SGarrett Wollman 
122098271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD,
122198271db4SGarrett Wollman 	    (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
122298271db4SGarrett Wollman 	    "List of active local datagram sockets");
122398271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD,
122498271db4SGarrett Wollman 	    (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
122598271db4SGarrett Wollman 	    "List of active local stream sockets");
122698271db4SGarrett Wollman 
1227f708ef1bSPoul-Henning Kamp static void
1228892af6b9SRobert Watson unp_shutdown(struct unpcb *unp)
1229df8bae1dSRodney W. Grimes {
1230df8bae1dSRodney W. Grimes 	struct socket *so;
1231df8bae1dSRodney W. Grimes 
12320d9ce3a1SRobert Watson 	UNP_LOCK_ASSERT();
12330d9ce3a1SRobert Watson 
1234df8bae1dSRodney W. Grimes 	if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
1235df8bae1dSRodney W. Grimes 	    (so = unp->unp_conn->unp_socket))
1236df8bae1dSRodney W. Grimes 		socantrcvmore(so);
1237df8bae1dSRodney W. Grimes }
1238df8bae1dSRodney W. Grimes 
1239f708ef1bSPoul-Henning Kamp static void
1240892af6b9SRobert Watson unp_drop(struct unpcb *unp, int errno)
1241df8bae1dSRodney W. Grimes {
1242df8bae1dSRodney W. Grimes 	struct socket *so = unp->unp_socket;
1243df8bae1dSRodney W. Grimes 
12440d9ce3a1SRobert Watson 	UNP_LOCK_ASSERT();
12450d9ce3a1SRobert Watson 
1246df8bae1dSRodney W. Grimes 	so->so_error = errno;
1247df8bae1dSRodney W. Grimes 	unp_disconnect(unp);
1248df8bae1dSRodney W. Grimes }
1249df8bae1dSRodney W. Grimes 
1250df8bae1dSRodney W. Grimes #ifdef notdef
125126f9a767SRodney W. Grimes void
1252892af6b9SRobert Watson unp_drain(void)
1253df8bae1dSRodney W. Grimes {
1254df8bae1dSRodney W. Grimes 
1255df8bae1dSRodney W. Grimes }
1256df8bae1dSRodney W. Grimes #endif
1257df8bae1dSRodney W. Grimes 
12582bc21ed9SDavid Malone static void
1259892af6b9SRobert Watson unp_freerights(struct file **rp, int fdcount)
1260df8bae1dSRodney W. Grimes {
12612bc21ed9SDavid Malone 	int i;
12622bc21ed9SDavid Malone 	struct file *fp;
1263df8bae1dSRodney W. Grimes 
12642bc21ed9SDavid Malone 	for (i = 0; i < fdcount; i++) {
1265df8bae1dSRodney W. Grimes 		fp = *rp;
12668692c025SYoshinobu Inoue 		/*
12672bc21ed9SDavid Malone 		 * zero the pointer before calling
12682bc21ed9SDavid Malone 		 * unp_discard since it may end up
12692bc21ed9SDavid Malone 		 * in unp_gc()..
12708692c025SYoshinobu Inoue 		 */
1271df8bae1dSRodney W. Grimes 		*rp++ = 0;
12728692c025SYoshinobu Inoue 		unp_discard(fp);
1273df8bae1dSRodney W. Grimes 	}
12742bc21ed9SDavid Malone }
12752bc21ed9SDavid Malone 
12762bc21ed9SDavid Malone int
1277892af6b9SRobert Watson unp_externalize(struct mbuf *control, struct mbuf **controlp)
12782bc21ed9SDavid Malone {
12792bc21ed9SDavid Malone 	struct thread *td = curthread;		/* XXX */
12802bc21ed9SDavid Malone 	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
12812bc21ed9SDavid Malone 	int i;
12822bc21ed9SDavid Malone 	int *fdp;
12832bc21ed9SDavid Malone 	struct file **rp;
12842bc21ed9SDavid Malone 	struct file *fp;
12852bc21ed9SDavid Malone 	void *data;
12862bc21ed9SDavid Malone 	socklen_t clen = control->m_len, datalen;
12872bc21ed9SDavid Malone 	int error, newfds;
12882bc21ed9SDavid Malone 	int f;
12892bc21ed9SDavid Malone 	u_int newlen;
12902bc21ed9SDavid Malone 
12914c5bc1caSRobert Watson 	UNP_UNLOCK_ASSERT();
12924c5bc1caSRobert Watson 
12932bc21ed9SDavid Malone 	error = 0;
12942bc21ed9SDavid Malone 	if (controlp != NULL) /* controlp == NULL => free control messages */
12952bc21ed9SDavid Malone 		*controlp = NULL;
12962bc21ed9SDavid Malone 
12972bc21ed9SDavid Malone 	while (cm != NULL) {
12982bc21ed9SDavid Malone 		if (sizeof(*cm) > clen || cm->cmsg_len > clen) {
12992bc21ed9SDavid Malone 			error = EINVAL;
13002bc21ed9SDavid Malone 			break;
13012bc21ed9SDavid Malone 		}
13022bc21ed9SDavid Malone 
13032bc21ed9SDavid Malone 		data = CMSG_DATA(cm);
13042bc21ed9SDavid Malone 		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
13052bc21ed9SDavid Malone 
13062bc21ed9SDavid Malone 		if (cm->cmsg_level == SOL_SOCKET
13072bc21ed9SDavid Malone 		    && cm->cmsg_type == SCM_RIGHTS) {
13082bc21ed9SDavid Malone 			newfds = datalen / sizeof(struct file *);
13092bc21ed9SDavid Malone 			rp = data;
13102bc21ed9SDavid Malone 
1311e2f9a08bSOlivier Houchard 			/* If we're not outputting the descriptors free them. */
13122bc21ed9SDavid Malone 			if (error || controlp == NULL) {
13132bc21ed9SDavid Malone 				unp_freerights(rp, newfds);
13142bc21ed9SDavid Malone 				goto next;
13152bc21ed9SDavid Malone 			}
1316426da3bcSAlfred Perlstein 			FILEDESC_LOCK(td->td_proc->p_fd);
13172bc21ed9SDavid Malone 			/* if the new FD's will not fit free them.  */
13182bc21ed9SDavid Malone 			if (!fdavail(td, newfds)) {
1319426da3bcSAlfred Perlstein 				FILEDESC_UNLOCK(td->td_proc->p_fd);
13202bc21ed9SDavid Malone 				error = EMSGSIZE;
13212bc21ed9SDavid Malone 				unp_freerights(rp, newfds);
13222bc21ed9SDavid Malone 				goto next;
1323df8bae1dSRodney W. Grimes 			}
1324ed5b7817SJulian Elischer 			/*
13252bc21ed9SDavid Malone 			 * now change each pointer to an fd in the global
13262bc21ed9SDavid Malone 			 * table to an integer that is the index to the
13272bc21ed9SDavid Malone 			 * local fd table entry that we set up to point
13282bc21ed9SDavid Malone 			 * to the global one we are transferring.
1329ed5b7817SJulian Elischer 			 */
13302bc21ed9SDavid Malone 			newlen = newfds * sizeof(int);
13312bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, newlen,
13322bc21ed9SDavid Malone 			    SCM_RIGHTS, SOL_SOCKET);
13332bc21ed9SDavid Malone 			if (*controlp == NULL) {
1334426da3bcSAlfred Perlstein 				FILEDESC_UNLOCK(td->td_proc->p_fd);
13352bc21ed9SDavid Malone 				error = E2BIG;
13362bc21ed9SDavid Malone 				unp_freerights(rp, newfds);
13372bc21ed9SDavid Malone 				goto next;
13382bc21ed9SDavid Malone 			}
13392bc21ed9SDavid Malone 
13402bc21ed9SDavid Malone 			fdp = (int *)
13412bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1342df8bae1dSRodney W. Grimes 			for (i = 0; i < newfds; i++) {
1343a6d4491cSDag-Erling Smørgrav 				if (fdalloc(td, 0, &f))
13442bc21ed9SDavid Malone 					panic("unp_externalize fdalloc failed");
13458692c025SYoshinobu Inoue 				fp = *rp++;
1346b40ce416SJulian Elischer 				td->td_proc->p_fd->fd_ofiles[f] = fp;
1347426da3bcSAlfred Perlstein 				FILE_LOCK(fp);
1348df8bae1dSRodney W. Grimes 				fp->f_msgcount--;
1349426da3bcSAlfred Perlstein 				FILE_UNLOCK(fp);
1350df8bae1dSRodney W. Grimes 				unp_rights--;
13518692c025SYoshinobu Inoue 				*fdp++ = f;
1352df8bae1dSRodney W. Grimes 			}
1353426da3bcSAlfred Perlstein 			FILEDESC_UNLOCK(td->td_proc->p_fd);
13542bc21ed9SDavid Malone 		} else { /* We can just copy anything else across */
13552bc21ed9SDavid Malone 			if (error || controlp == NULL)
13562bc21ed9SDavid Malone 				goto next;
13572bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, datalen,
13582bc21ed9SDavid Malone 			    cm->cmsg_type, cm->cmsg_level);
13592bc21ed9SDavid Malone 			if (*controlp == NULL) {
13602bc21ed9SDavid Malone 				error = ENOBUFS;
13612bc21ed9SDavid Malone 				goto next;
13622bc21ed9SDavid Malone 			}
13632bc21ed9SDavid Malone 			bcopy(data,
13642bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *)),
13652bc21ed9SDavid Malone 			    datalen);
13662bc21ed9SDavid Malone 		}
13672bc21ed9SDavid Malone 
13682bc21ed9SDavid Malone 		controlp = &(*controlp)->m_next;
13692bc21ed9SDavid Malone 
13702bc21ed9SDavid Malone next:
13712bc21ed9SDavid Malone 		if (CMSG_SPACE(datalen) < clen) {
13722bc21ed9SDavid Malone 			clen -= CMSG_SPACE(datalen);
13732bc21ed9SDavid Malone 			cm = (struct cmsghdr *)
13742bc21ed9SDavid Malone 			    ((caddr_t)cm + CMSG_SPACE(datalen));
13758692c025SYoshinobu Inoue 		} else {
13762bc21ed9SDavid Malone 			clen = 0;
13772bc21ed9SDavid Malone 			cm = NULL;
13788692c025SYoshinobu Inoue 		}
13798692c025SYoshinobu Inoue 	}
13808692c025SYoshinobu Inoue 
13812bc21ed9SDavid Malone 	m_freem(control);
13822bc21ed9SDavid Malone 
13832bc21ed9SDavid Malone 	return (error);
1384df8bae1dSRodney W. Grimes }
1385df8bae1dSRodney W. Grimes 
138698271db4SGarrett Wollman void
138798271db4SGarrett Wollman unp_init(void)
138898271db4SGarrett Wollman {
13899e9d298aSJeff Roberson 	unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL,
13909e9d298aSJeff Roberson 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1391fc3fcacfSRobert Watson 	if (unp_zone == NULL)
139298271db4SGarrett Wollman 		panic("unp_init");
1393b17dd2bcSColin Percival 	uma_zone_set_max(unp_zone, nmbclusters);
139498271db4SGarrett Wollman 	LIST_INIT(&unp_dhead);
139598271db4SGarrett Wollman 	LIST_INIT(&unp_shead);
13960d9ce3a1SRobert Watson 
13970d9ce3a1SRobert Watson 	UNP_LOCK_INIT();
139898271db4SGarrett Wollman }
139998271db4SGarrett Wollman 
1400f708ef1bSPoul-Henning Kamp static int
1401892af6b9SRobert Watson unp_internalize(struct mbuf **controlp, struct thread *td)
1402df8bae1dSRodney W. Grimes {
14032bc21ed9SDavid Malone 	struct mbuf *control = *controlp;
1404b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
14058692c025SYoshinobu Inoue 	struct filedesc *fdescp = p->p_fd;
14062bc21ed9SDavid Malone 	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
14072bc21ed9SDavid Malone 	struct cmsgcred *cmcred;
14082bc21ed9SDavid Malone 	struct file **rp;
14092bc21ed9SDavid Malone 	struct file *fp;
14102bc21ed9SDavid Malone 	struct timeval *tv;
14112bc21ed9SDavid Malone 	int i, fd, *fdp;
14122bc21ed9SDavid Malone 	void *data;
14132bc21ed9SDavid Malone 	socklen_t clen = control->m_len, datalen;
14142bc21ed9SDavid Malone 	int error, oldfds;
14158692c025SYoshinobu Inoue 	u_int newlen;
1416df8bae1dSRodney W. Grimes 
14174c5bc1caSRobert Watson 	UNP_UNLOCK_ASSERT();
14184c5bc1caSRobert Watson 
14192bc21ed9SDavid Malone 	error = 0;
14202bc21ed9SDavid Malone 	*controlp = NULL;
14210b788fa1SBill Paul 
14222bc21ed9SDavid Malone 	while (cm != NULL) {
14232bc21ed9SDavid Malone 		if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET
14242bc21ed9SDavid Malone 		    || cm->cmsg_len > clen) {
14252bc21ed9SDavid Malone 			error = EINVAL;
14262bc21ed9SDavid Malone 			goto out;
14272bc21ed9SDavid Malone 		}
14282bc21ed9SDavid Malone 
14292bc21ed9SDavid Malone 		data = CMSG_DATA(cm);
14302bc21ed9SDavid Malone 		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
14312bc21ed9SDavid Malone 
14322bc21ed9SDavid Malone 		switch (cm->cmsg_type) {
14330b788fa1SBill Paul 		/*
14340b788fa1SBill Paul 		 * Fill in credential information.
14350b788fa1SBill Paul 		 */
14362bc21ed9SDavid Malone 		case SCM_CREDS:
14372bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, sizeof(*cmcred),
14382bc21ed9SDavid Malone 			    SCM_CREDS, SOL_SOCKET);
14392bc21ed9SDavid Malone 			if (*controlp == NULL) {
14402bc21ed9SDavid Malone 				error = ENOBUFS;
14412bc21ed9SDavid Malone 				goto out;
14422bc21ed9SDavid Malone 			}
14432bc21ed9SDavid Malone 
14442bc21ed9SDavid Malone 			cmcred = (struct cmsgcred *)
14452bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
14460b788fa1SBill Paul 			cmcred->cmcred_pid = p->p_pid;
1447a854ed98SJohn Baldwin 			cmcred->cmcred_uid = td->td_ucred->cr_ruid;
1448a854ed98SJohn Baldwin 			cmcred->cmcred_gid = td->td_ucred->cr_rgid;
1449a854ed98SJohn Baldwin 			cmcred->cmcred_euid = td->td_ucred->cr_uid;
1450a854ed98SJohn Baldwin 			cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups,
14510b788fa1SBill Paul 							CMGROUP_MAX);
14520b788fa1SBill Paul 			for (i = 0; i < cmcred->cmcred_ngroups; i++)
14532bc21ed9SDavid Malone 				cmcred->cmcred_groups[i] =
1454a854ed98SJohn Baldwin 				    td->td_ucred->cr_groups[i];
14552bc21ed9SDavid Malone 			break;
14560b788fa1SBill Paul 
14572bc21ed9SDavid Malone 		case SCM_RIGHTS:
14582bc21ed9SDavid Malone 			oldfds = datalen / sizeof (int);
1459ed5b7817SJulian Elischer 			/*
14602bc21ed9SDavid Malone 			 * check that all the FDs passed in refer to legal files
1461ed5b7817SJulian Elischer 			 * If not, reject the entire operation.
1462ed5b7817SJulian Elischer 			 */
14632bc21ed9SDavid Malone 			fdp = data;
1464426da3bcSAlfred Perlstein 			FILEDESC_LOCK(fdescp);
1465df8bae1dSRodney W. Grimes 			for (i = 0; i < oldfds; i++) {
14668692c025SYoshinobu Inoue 				fd = *fdp++;
14678692c025SYoshinobu Inoue 				if ((unsigned)fd >= fdescp->fd_nfiles ||
14682bc21ed9SDavid Malone 				    fdescp->fd_ofiles[fd] == NULL) {
1469426da3bcSAlfred Perlstein 					FILEDESC_UNLOCK(fdescp);
14702bc21ed9SDavid Malone 					error = EBADF;
14712bc21ed9SDavid Malone 					goto out;
14722bc21ed9SDavid Malone 				}
1473e7d6662fSAlfred Perlstein 				fp = fdescp->fd_ofiles[fd];
1474e7d6662fSAlfred Perlstein 				if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) {
1475e7d6662fSAlfred Perlstein 					FILEDESC_UNLOCK(fdescp);
1476e7d6662fSAlfred Perlstein 					error = EOPNOTSUPP;
1477e7d6662fSAlfred Perlstein 					goto out;
1478e7d6662fSAlfred Perlstein 				}
1479e7d6662fSAlfred Perlstein 
1480df8bae1dSRodney W. Grimes 			}
1481ed5b7817SJulian Elischer 			/*
1482ed5b7817SJulian Elischer 			 * Now replace the integer FDs with pointers to
1483ed5b7817SJulian Elischer 			 * the associated global file table entry..
1484ed5b7817SJulian Elischer 			 */
14852bc21ed9SDavid Malone 			newlen = oldfds * sizeof(struct file *);
14862bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, newlen,
14872bc21ed9SDavid Malone 			    SCM_RIGHTS, SOL_SOCKET);
14882bc21ed9SDavid Malone 			if (*controlp == NULL) {
1489426da3bcSAlfred Perlstein 				FILEDESC_UNLOCK(fdescp);
14902bc21ed9SDavid Malone 				error = E2BIG;
14912bc21ed9SDavid Malone 				goto out;
14928692c025SYoshinobu Inoue 			}
14938692c025SYoshinobu Inoue 
14942bc21ed9SDavid Malone 			fdp = data;
14952bc21ed9SDavid Malone 			rp = (struct file **)
14962bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
14978692c025SYoshinobu Inoue 			for (i = 0; i < oldfds; i++) {
14988692c025SYoshinobu Inoue 				fp = fdescp->fd_ofiles[*fdp++];
1499df8bae1dSRodney W. Grimes 				*rp++ = fp;
1500426da3bcSAlfred Perlstein 				FILE_LOCK(fp);
1501df8bae1dSRodney W. Grimes 				fp->f_count++;
1502df8bae1dSRodney W. Grimes 				fp->f_msgcount++;
1503426da3bcSAlfred Perlstein 				FILE_UNLOCK(fp);
1504df8bae1dSRodney W. Grimes 				unp_rights++;
1505df8bae1dSRodney W. Grimes 			}
1506426da3bcSAlfred Perlstein 			FILEDESC_UNLOCK(fdescp);
15072bc21ed9SDavid Malone 			break;
15082bc21ed9SDavid Malone 
15092bc21ed9SDavid Malone 		case SCM_TIMESTAMP:
15102bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, sizeof(*tv),
15112bc21ed9SDavid Malone 			    SCM_TIMESTAMP, SOL_SOCKET);
15122bc21ed9SDavid Malone 			if (*controlp == NULL) {
15132bc21ed9SDavid Malone 				error = ENOBUFS;
15142bc21ed9SDavid Malone 				goto out;
15158692c025SYoshinobu Inoue 			}
15162bc21ed9SDavid Malone 			tv = (struct timeval *)
15172bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
15182bc21ed9SDavid Malone 			microtime(tv);
15192bc21ed9SDavid Malone 			break;
15202bc21ed9SDavid Malone 
15212bc21ed9SDavid Malone 		default:
15222bc21ed9SDavid Malone 			error = EINVAL;
15232bc21ed9SDavid Malone 			goto out;
15242bc21ed9SDavid Malone 		}
15252bc21ed9SDavid Malone 
15262bc21ed9SDavid Malone 		controlp = &(*controlp)->m_next;
15272bc21ed9SDavid Malone 
15282bc21ed9SDavid Malone 		if (CMSG_SPACE(datalen) < clen) {
15292bc21ed9SDavid Malone 			clen -= CMSG_SPACE(datalen);
15302bc21ed9SDavid Malone 			cm = (struct cmsghdr *)
15312bc21ed9SDavid Malone 			    ((caddr_t)cm + CMSG_SPACE(datalen));
15322bc21ed9SDavid Malone 		} else {
15332bc21ed9SDavid Malone 			clen = 0;
15342bc21ed9SDavid Malone 			cm = NULL;
15352bc21ed9SDavid Malone 		}
15362bc21ed9SDavid Malone 	}
15372bc21ed9SDavid Malone 
15382bc21ed9SDavid Malone out:
15392bc21ed9SDavid Malone 	m_freem(control);
15402bc21ed9SDavid Malone 
15412bc21ed9SDavid Malone 	return (error);
1542df8bae1dSRodney W. Grimes }
1543df8bae1dSRodney W. Grimes 
15446a2989fdSMatthew N. Dodd struct mbuf *
15456a2989fdSMatthew N. Dodd unp_addsockcred(struct thread *td, struct mbuf *control)
15466a2989fdSMatthew N. Dodd {
15476a2989fdSMatthew N. Dodd 	struct mbuf *m, *n;
15486a2989fdSMatthew N. Dodd 	struct sockcred *sc;
15496a2989fdSMatthew N. Dodd 	int ngroups;
15506a2989fdSMatthew N. Dodd 	int i;
15516a2989fdSMatthew N. Dodd 
15526a2989fdSMatthew N. Dodd 	ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX);
15536a2989fdSMatthew N. Dodd 
15546a2989fdSMatthew N. Dodd 	m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET);
15556a2989fdSMatthew N. Dodd 	if (m == NULL)
15566a2989fdSMatthew N. Dodd 		return (control);
15576a2989fdSMatthew N. Dodd 	m->m_next = NULL;
15586a2989fdSMatthew N. Dodd 
15596a2989fdSMatthew N. Dodd 	sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *));
15606a2989fdSMatthew N. Dodd 	sc->sc_uid = td->td_ucred->cr_ruid;
15616a2989fdSMatthew N. Dodd 	sc->sc_euid = td->td_ucred->cr_uid;
15626a2989fdSMatthew N. Dodd 	sc->sc_gid = td->td_ucred->cr_rgid;
15636a2989fdSMatthew N. Dodd 	sc->sc_egid = td->td_ucred->cr_gid;
15646a2989fdSMatthew N. Dodd 	sc->sc_ngroups = ngroups;
15656a2989fdSMatthew N. Dodd 	for (i = 0; i < sc->sc_ngroups; i++)
15666a2989fdSMatthew N. Dodd 		sc->sc_groups[i] = td->td_ucred->cr_groups[i];
15676a2989fdSMatthew N. Dodd 
15686a2989fdSMatthew N. Dodd 	/*
15696a2989fdSMatthew N. Dodd 	 * If a control message already exists, append us to the end.
15706a2989fdSMatthew N. Dodd 	 */
15716a2989fdSMatthew N. Dodd 	if (control != NULL) {
15726a2989fdSMatthew N. Dodd 		for (n = control; n->m_next != NULL; n = n->m_next)
15736a2989fdSMatthew N. Dodd 			;
15746a2989fdSMatthew N. Dodd 		n->m_next = m;
15756a2989fdSMatthew N. Dodd 	} else
15766a2989fdSMatthew N. Dodd 		control = m;
15776a2989fdSMatthew N. Dodd 
15786a2989fdSMatthew N. Dodd 	return (control);
15796a2989fdSMatthew N. Dodd }
15806a2989fdSMatthew N. Dodd 
1581161a0c7cSRobert Watson /*
1582161a0c7cSRobert Watson  * unp_defer is thread-local during garbage collection, and does not require
1583161a0c7cSRobert Watson  * explicit synchronization.  unp_gcing prevents other threads from entering
1584161a0c7cSRobert Watson  * garbage collection, and perhaps should be an sx lock instead.
1585161a0c7cSRobert Watson  */
1586f708ef1bSPoul-Henning Kamp static int	unp_defer, unp_gcing;
1587df8bae1dSRodney W. Grimes 
1588f708ef1bSPoul-Henning Kamp static void
1589892af6b9SRobert Watson unp_gc(void)
1590df8bae1dSRodney W. Grimes {
1591892af6b9SRobert Watson 	struct file *fp, *nextfp;
1592892af6b9SRobert Watson 	struct socket *so;
1593df8bae1dSRodney W. Grimes 	struct file **extra_ref, **fpp;
1594df8bae1dSRodney W. Grimes 	int nunref, i;
159595f004dcSAlfred Perlstein 	int nfiles_snap;
159695f004dcSAlfred Perlstein 	int nfiles_slack = 20;
1597df8bae1dSRodney W. Grimes 
15980d9ce3a1SRobert Watson 	UNP_LOCK_ASSERT();
15990d9ce3a1SRobert Watson 
1600161a0c7cSRobert Watson 	if (unp_gcing) {
1601161a0c7cSRobert Watson 		UNP_UNLOCK();
1602df8bae1dSRodney W. Grimes 		return;
1603161a0c7cSRobert Watson 	}
1604df8bae1dSRodney W. Grimes 	unp_gcing = 1;
1605df8bae1dSRodney W. Grimes 	unp_defer = 0;
1606161a0c7cSRobert Watson 	UNP_UNLOCK();
1607ed5b7817SJulian Elischer 	/*
1608ed5b7817SJulian Elischer 	 * before going through all this, set all FDs to
1609ed5b7817SJulian Elischer 	 * be NOT defered and NOT externally accessible
1610ed5b7817SJulian Elischer 	 */
1611426da3bcSAlfred Perlstein 	sx_slock(&filelist_lock);
16122e3c8fcbSPoul-Henning Kamp 	LIST_FOREACH(fp, &filehead, f_list)
1613426da3bcSAlfred Perlstein 		fp->f_gcflag &= ~(FMARK|FDEFER);
1614df8bae1dSRodney W. Grimes 	do {
16152e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(fp, &filehead, f_list) {
1616426da3bcSAlfred Perlstein 			FILE_LOCK(fp);
1617ed5b7817SJulian Elischer 			/*
1618ed5b7817SJulian Elischer 			 * If the file is not open, skip it
1619ed5b7817SJulian Elischer 			 */
1620426da3bcSAlfred Perlstein 			if (fp->f_count == 0) {
1621426da3bcSAlfred Perlstein 				FILE_UNLOCK(fp);
1622df8bae1dSRodney W. Grimes 				continue;
1623426da3bcSAlfred Perlstein 			}
1624ed5b7817SJulian Elischer 			/*
1625ed5b7817SJulian Elischer 			 * If we already marked it as 'defer'  in a
1626ed5b7817SJulian Elischer 			 * previous pass, then try process it this time
1627ed5b7817SJulian Elischer 			 * and un-mark it
1628ed5b7817SJulian Elischer 			 */
1629426da3bcSAlfred Perlstein 			if (fp->f_gcflag & FDEFER) {
1630426da3bcSAlfred Perlstein 				fp->f_gcflag &= ~FDEFER;
1631df8bae1dSRodney W. Grimes 				unp_defer--;
1632df8bae1dSRodney W. Grimes 			} else {
1633ed5b7817SJulian Elischer 				/*
1634ed5b7817SJulian Elischer 				 * if it's not defered, then check if it's
1635ed5b7817SJulian Elischer 				 * already marked.. if so skip it
1636ed5b7817SJulian Elischer 				 */
1637426da3bcSAlfred Perlstein 				if (fp->f_gcflag & FMARK) {
1638426da3bcSAlfred Perlstein 					FILE_UNLOCK(fp);
1639df8bae1dSRodney W. Grimes 					continue;
1640426da3bcSAlfred Perlstein 				}
1641ed5b7817SJulian Elischer 				/*
1642ed5b7817SJulian Elischer 				 * If all references are from messages
1643ed5b7817SJulian Elischer 				 * in transit, then skip it. it's not
1644ed5b7817SJulian Elischer 				 * externally accessible.
1645ed5b7817SJulian Elischer 				 */
1646426da3bcSAlfred Perlstein 				if (fp->f_count == fp->f_msgcount) {
1647426da3bcSAlfred Perlstein 					FILE_UNLOCK(fp);
1648df8bae1dSRodney W. Grimes 					continue;
1649426da3bcSAlfred Perlstein 				}
1650ed5b7817SJulian Elischer 				/*
1651ed5b7817SJulian Elischer 				 * If it got this far then it must be
1652ed5b7817SJulian Elischer 				 * externally accessible.
1653ed5b7817SJulian Elischer 				 */
1654426da3bcSAlfred Perlstein 				fp->f_gcflag |= FMARK;
1655df8bae1dSRodney W. Grimes 			}
1656ed5b7817SJulian Elischer 			/*
1657ed5b7817SJulian Elischer 			 * either it was defered, or it is externally
1658ed5b7817SJulian Elischer 			 * accessible and not already marked so.
1659ed5b7817SJulian Elischer 			 * Now check if it is possibly one of OUR sockets.
1660ed5b7817SJulian Elischer 			 */
1661df8bae1dSRodney W. Grimes 			if (fp->f_type != DTYPE_SOCKET ||
166248e3128bSMatthew Dillon 			    (so = fp->f_data) == NULL) {
1663426da3bcSAlfred Perlstein 				FILE_UNLOCK(fp);
1664df8bae1dSRodney W. Grimes 				continue;
1665426da3bcSAlfred Perlstein 			}
1666426da3bcSAlfred Perlstein 			FILE_UNLOCK(fp);
1667748e0b0aSGarrett Wollman 			if (so->so_proto->pr_domain != &localdomain ||
1668df8bae1dSRodney W. Grimes 			    (so->so_proto->pr_flags&PR_RIGHTS) == 0)
1669df8bae1dSRodney W. Grimes 				continue;
1670df8bae1dSRodney W. Grimes #ifdef notdef
1671df8bae1dSRodney W. Grimes 			if (so->so_rcv.sb_flags & SB_LOCK) {
1672df8bae1dSRodney W. Grimes 				/*
1673df8bae1dSRodney W. Grimes 				 * This is problematical; it's not clear
1674df8bae1dSRodney W. Grimes 				 * we need to wait for the sockbuf to be
1675df8bae1dSRodney W. Grimes 				 * unlocked (on a uniprocessor, at least),
1676df8bae1dSRodney W. Grimes 				 * and it's also not clear what to do
1677df8bae1dSRodney W. Grimes 				 * if sbwait returns an error due to receipt
1678df8bae1dSRodney W. Grimes 				 * of a signal.  If sbwait does return
1679df8bae1dSRodney W. Grimes 				 * an error, we'll go into an infinite
1680df8bae1dSRodney W. Grimes 				 * loop.  Delete all of this for now.
1681df8bae1dSRodney W. Grimes 				 */
1682df8bae1dSRodney W. Grimes 				(void) sbwait(&so->so_rcv);
1683df8bae1dSRodney W. Grimes 				goto restart;
1684df8bae1dSRodney W. Grimes 			}
1685df8bae1dSRodney W. Grimes #endif
1686ed5b7817SJulian Elischer 			/*
1687ed5b7817SJulian Elischer 			 * So, Ok, it's one of our sockets and it IS externally
1688ed5b7817SJulian Elischer 			 * accessible (or was defered). Now we look
1689dc733423SDag-Erling Smørgrav 			 * to see if we hold any file descriptors in its
1690ed5b7817SJulian Elischer 			 * message buffers. Follow those links and mark them
1691ed5b7817SJulian Elischer 			 * as accessible too.
1692ed5b7817SJulian Elischer 			 */
16937717cf07SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
1694df8bae1dSRodney W. Grimes 			unp_scan(so->so_rcv.sb_mb, unp_mark);
16957717cf07SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);
1696df8bae1dSRodney W. Grimes 		}
1697df8bae1dSRodney W. Grimes 	} while (unp_defer);
1698426da3bcSAlfred Perlstein 	sx_sunlock(&filelist_lock);
1699df8bae1dSRodney W. Grimes 	/*
1700df8bae1dSRodney W. Grimes 	 * We grab an extra reference to each of the file table entries
1701df8bae1dSRodney W. Grimes 	 * that are not otherwise accessible and then free the rights
1702df8bae1dSRodney W. Grimes 	 * that are stored in messages on them.
1703df8bae1dSRodney W. Grimes 	 *
1704df8bae1dSRodney W. Grimes 	 * The bug in the orginal code is a little tricky, so I'll describe
1705df8bae1dSRodney W. Grimes 	 * what's wrong with it here.
1706df8bae1dSRodney W. Grimes 	 *
1707df8bae1dSRodney W. Grimes 	 * It is incorrect to simply unp_discard each entry for f_msgcount
1708df8bae1dSRodney W. Grimes 	 * times -- consider the case of sockets A and B that contain
1709df8bae1dSRodney W. Grimes 	 * references to each other.  On a last close of some other socket,
1710df8bae1dSRodney W. Grimes 	 * we trigger a gc since the number of outstanding rights (unp_rights)
1711df8bae1dSRodney W. Grimes 	 * is non-zero.  If during the sweep phase the gc code un_discards,
1712df8bae1dSRodney W. Grimes 	 * we end up doing a (full) closef on the descriptor.  A closef on A
1713df8bae1dSRodney W. Grimes 	 * results in the following chain.  Closef calls soo_close, which
1714df8bae1dSRodney W. Grimes 	 * calls soclose.   Soclose calls first (through the switch
1715df8bae1dSRodney W. Grimes 	 * uipc_usrreq) unp_detach, which re-invokes unp_gc.  Unp_gc simply
1716df8bae1dSRodney W. Grimes 	 * returns because the previous instance had set unp_gcing, and
1717df8bae1dSRodney W. Grimes 	 * we return all the way back to soclose, which marks the socket
1718df8bae1dSRodney W. Grimes 	 * with SS_NOFDREF, and then calls sofree.  Sofree calls sorflush
1719df8bae1dSRodney W. Grimes 	 * to free up the rights that are queued in messages on the socket A,
1720df8bae1dSRodney W. Grimes 	 * i.e., the reference on B.  The sorflush calls via the dom_dispose
1721df8bae1dSRodney W. Grimes 	 * switch unp_dispose, which unp_scans with unp_discard.  This second
1722df8bae1dSRodney W. Grimes 	 * instance of unp_discard just calls closef on B.
1723df8bae1dSRodney W. Grimes 	 *
1724df8bae1dSRodney W. Grimes 	 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1725df8bae1dSRodney W. Grimes 	 * which results in another closef on A.  Unfortunately, A is already
1726df8bae1dSRodney W. Grimes 	 * being closed, and the descriptor has already been marked with
1727df8bae1dSRodney W. Grimes 	 * SS_NOFDREF, and soclose panics at this point.
1728df8bae1dSRodney W. Grimes 	 *
1729df8bae1dSRodney W. Grimes 	 * Here, we first take an extra reference to each inaccessible
1730df8bae1dSRodney W. Grimes 	 * descriptor.  Then, we call sorflush ourself, since we know
1731df8bae1dSRodney W. Grimes 	 * it is a Unix domain socket anyhow.  After we destroy all the
1732df8bae1dSRodney W. Grimes 	 * rights carried in messages, we do a last closef to get rid
1733df8bae1dSRodney W. Grimes 	 * of our extra reference.  This is the last close, and the
1734df8bae1dSRodney W. Grimes 	 * unp_detach etc will shut down the socket.
1735df8bae1dSRodney W. Grimes 	 *
1736df8bae1dSRodney W. Grimes 	 * 91/09/19, bsy@cs.cmu.edu
1737df8bae1dSRodney W. Grimes 	 */
173895f004dcSAlfred Perlstein again:
1739e4643c73SPoul-Henning Kamp 	nfiles_snap = openfiles + nfiles_slack;	/* some slack */
174095f004dcSAlfred Perlstein 	extra_ref = malloc(nfiles_snap * sizeof(struct file *), M_TEMP,
174195f004dcSAlfred Perlstein 	    M_WAITOK);
1742426da3bcSAlfred Perlstein 	sx_slock(&filelist_lock);
1743e4643c73SPoul-Henning Kamp 	if (nfiles_snap < openfiles) {
174495f004dcSAlfred Perlstein 		sx_sunlock(&filelist_lock);
174595f004dcSAlfred Perlstein 		free(extra_ref, M_TEMP);
174695f004dcSAlfred Perlstein 		nfiles_slack += 20;
174795f004dcSAlfred Perlstein 		goto again;
174895f004dcSAlfred Perlstein 	}
1749fc3fcacfSRobert Watson 	for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref;
1750fc3fcacfSRobert Watson 	    fp != NULL; fp = nextfp) {
17512e3c8fcbSPoul-Henning Kamp 		nextfp = LIST_NEXT(fp, f_list);
1752426da3bcSAlfred Perlstein 		FILE_LOCK(fp);
1753ed5b7817SJulian Elischer 		/*
1754ed5b7817SJulian Elischer 		 * If it's not open, skip it
1755ed5b7817SJulian Elischer 		 */
1756426da3bcSAlfred Perlstein 		if (fp->f_count == 0) {
1757426da3bcSAlfred Perlstein 			FILE_UNLOCK(fp);
1758df8bae1dSRodney W. Grimes 			continue;
1759426da3bcSAlfred Perlstein 		}
1760ed5b7817SJulian Elischer 		/*
1761ed5b7817SJulian Elischer 		 * If all refs are from msgs, and it's not marked accessible
1762ed5b7817SJulian Elischer 		 * then it must be referenced from some unreachable cycle
1763ed5b7817SJulian Elischer 		 * of (shut-down) FDs, so include it in our
1764ed5b7817SJulian Elischer 		 * list of FDs to remove
1765ed5b7817SJulian Elischer 		 */
1766426da3bcSAlfred Perlstein 		if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) {
1767df8bae1dSRodney W. Grimes 			*fpp++ = fp;
1768df8bae1dSRodney W. Grimes 			nunref++;
1769df8bae1dSRodney W. Grimes 			fp->f_count++;
1770df8bae1dSRodney W. Grimes 		}
1771426da3bcSAlfred Perlstein 		FILE_UNLOCK(fp);
1772df8bae1dSRodney W. Grimes 	}
1773426da3bcSAlfred Perlstein 	sx_sunlock(&filelist_lock);
1774ed5b7817SJulian Elischer 	/*
1775ed5b7817SJulian Elischer 	 * for each FD on our hit list, do the following two things
1776ed5b7817SJulian Elischer 	 */
17771c7c3c6aSMatthew Dillon 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
17781c7c3c6aSMatthew Dillon 		struct file *tfp = *fpp;
1779426da3bcSAlfred Perlstein 		FILE_LOCK(tfp);
1780cd72f218SMatthew Dillon 		if (tfp->f_type == DTYPE_SOCKET &&
178148e3128bSMatthew Dillon 		    tfp->f_data != NULL) {
1782426da3bcSAlfred Perlstein 			FILE_UNLOCK(tfp);
178348e3128bSMatthew Dillon 			sorflush(tfp->f_data);
1784e5aeaa0cSDag-Erling Smørgrav 		} else {
1785426da3bcSAlfred Perlstein 			FILE_UNLOCK(tfp);
17861c7c3c6aSMatthew Dillon 		}
1787e5aeaa0cSDag-Erling Smørgrav 	}
1788df8bae1dSRodney W. Grimes 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp)
1789b40ce416SJulian Elischer 		closef(*fpp, (struct thread *) NULL);
1790210a5a71SAlfred Perlstein 	free(extra_ref, M_TEMP);
1791df8bae1dSRodney W. Grimes 	unp_gcing = 0;
1792161a0c7cSRobert Watson 
1793161a0c7cSRobert Watson 	UNP_UNLOCK_ASSERT();
1794df8bae1dSRodney W. Grimes }
1795df8bae1dSRodney W. Grimes 
179626f9a767SRodney W. Grimes void
1797892af6b9SRobert Watson unp_dispose(struct mbuf *m)
1798df8bae1dSRodney W. Grimes {
1799996c772fSJohn Dyson 
1800df8bae1dSRodney W. Grimes 	if (m)
1801df8bae1dSRodney W. Grimes 		unp_scan(m, unp_discard);
1802df8bae1dSRodney W. Grimes }
1803df8bae1dSRodney W. Grimes 
18040c1bb4fbSDima Dorfman static int
18050daccb9cSRobert Watson unp_listen(struct socket *so, struct unpcb *unp, struct thread *td)
18060c1bb4fbSDima Dorfman {
18070daccb9cSRobert Watson 	int error;
18080daccb9cSRobert Watson 
18090d9ce3a1SRobert Watson 	UNP_LOCK_ASSERT();
18100c1bb4fbSDima Dorfman 
18110daccb9cSRobert Watson 	SOCK_LOCK(so);
18120daccb9cSRobert Watson 	error = solisten_proto_check(so);
18130daccb9cSRobert Watson 	if (error == 0) {
18146f105b34SJohn Baldwin 		cru2x(td->td_ucred, &unp->unp_peercred);
18150c1bb4fbSDima Dorfman 		unp->unp_flags |= UNP_HAVEPCCACHED;
18160daccb9cSRobert Watson 		solisten_proto(so);
18170daccb9cSRobert Watson 	}
18180daccb9cSRobert Watson 	SOCK_UNLOCK(so);
18190daccb9cSRobert Watson 	return (error);
18200c1bb4fbSDima Dorfman }
18210c1bb4fbSDima Dorfman 
1822f708ef1bSPoul-Henning Kamp static void
1823892af6b9SRobert Watson unp_scan(struct mbuf *m0, void (*op)(struct file *))
1824df8bae1dSRodney W. Grimes {
18252bc21ed9SDavid Malone 	struct mbuf *m;
18262bc21ed9SDavid Malone 	struct file **rp;
18272bc21ed9SDavid Malone 	struct cmsghdr *cm;
18282bc21ed9SDavid Malone 	void *data;
18292bc21ed9SDavid Malone 	int i;
18302bc21ed9SDavid Malone 	socklen_t clen, datalen;
1831df8bae1dSRodney W. Grimes 	int qfds;
1832df8bae1dSRodney W. Grimes 
1833fc3fcacfSRobert Watson 	while (m0 != NULL) {
18342bc21ed9SDavid Malone 		for (m = m0; m; m = m->m_next) {
183512396bdcSDavid Malone 			if (m->m_type != MT_CONTROL)
1836df8bae1dSRodney W. Grimes 				continue;
18372bc21ed9SDavid Malone 
18382bc21ed9SDavid Malone 			cm = mtod(m, struct cmsghdr *);
18392bc21ed9SDavid Malone 			clen = m->m_len;
18402bc21ed9SDavid Malone 
18412bc21ed9SDavid Malone 			while (cm != NULL) {
18422bc21ed9SDavid Malone 				if (sizeof(*cm) > clen || cm->cmsg_len > clen)
18432bc21ed9SDavid Malone 					break;
18442bc21ed9SDavid Malone 
18452bc21ed9SDavid Malone 				data = CMSG_DATA(cm);
18462bc21ed9SDavid Malone 				datalen = (caddr_t)cm + cm->cmsg_len
18472bc21ed9SDavid Malone 				    - (caddr_t)data;
18482bc21ed9SDavid Malone 
18492bc21ed9SDavid Malone 				if (cm->cmsg_level == SOL_SOCKET &&
18502bc21ed9SDavid Malone 				    cm->cmsg_type == SCM_RIGHTS) {
18512bc21ed9SDavid Malone 					qfds = datalen / sizeof (struct file *);
18522bc21ed9SDavid Malone 					rp = data;
1853df8bae1dSRodney W. Grimes 					for (i = 0; i < qfds; i++)
1854df8bae1dSRodney W. Grimes 						(*op)(*rp++);
18552bc21ed9SDavid Malone 				}
18562bc21ed9SDavid Malone 
18572bc21ed9SDavid Malone 				if (CMSG_SPACE(datalen) < clen) {
18582bc21ed9SDavid Malone 					clen -= CMSG_SPACE(datalen);
18592bc21ed9SDavid Malone 					cm = (struct cmsghdr *)
18602bc21ed9SDavid Malone 					    ((caddr_t)cm + CMSG_SPACE(datalen));
18612bc21ed9SDavid Malone 				} else {
18622bc21ed9SDavid Malone 					clen = 0;
18632bc21ed9SDavid Malone 					cm = NULL;
18642bc21ed9SDavid Malone 				}
18652bc21ed9SDavid Malone 			}
1866df8bae1dSRodney W. Grimes 		}
1867df8bae1dSRodney W. Grimes 		m0 = m0->m_act;
1868df8bae1dSRodney W. Grimes 	}
1869df8bae1dSRodney W. Grimes }
1870df8bae1dSRodney W. Grimes 
1871f708ef1bSPoul-Henning Kamp static void
1872892af6b9SRobert Watson unp_mark(struct file *fp)
1873df8bae1dSRodney W. Grimes {
1874426da3bcSAlfred Perlstein 	if (fp->f_gcflag & FMARK)
1875df8bae1dSRodney W. Grimes 		return;
1876df8bae1dSRodney W. Grimes 	unp_defer++;
1877426da3bcSAlfred Perlstein 	fp->f_gcflag |= (FMARK|FDEFER);
1878df8bae1dSRodney W. Grimes }
1879df8bae1dSRodney W. Grimes 
1880f708ef1bSPoul-Henning Kamp static void
1881892af6b9SRobert Watson unp_discard(struct file *fp)
1882df8bae1dSRodney W. Grimes {
1883426da3bcSAlfred Perlstein 	FILE_LOCK(fp);
1884df8bae1dSRodney W. Grimes 	fp->f_msgcount--;
1885df8bae1dSRodney W. Grimes 	unp_rights--;
1886426da3bcSAlfred Perlstein 	FILE_UNLOCK(fp);
1887b40ce416SJulian Elischer 	(void) closef(fp, (struct thread *)NULL);
1888df8bae1dSRodney W. Grimes }
1889