xref: /freebsd/sys/kern/uipc_usrreq.c (revision 6f552cb0988a631e79c8f96cfc4fe8f7edc6ac4f)
19454b2d8SWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3e1ac28e2SRobert Watson  *	The Regents of the University of California.
446a1d9bfSRobert Watson  * Copyright (c) 2004-2007 Robert N. M. Watson
5e1ac28e2SRobert Watson  * All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes  * are met:
10df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes  *    without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
19df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  *
31748e0b0aSGarrett Wollman  *	From: @(#)uipc_usrreq.c	8.3 (Berkeley) 1/4/94
32df8bae1dSRodney W. Grimes  */
33df8bae1dSRodney W. Grimes 
34f23929fbSRobert Watson /*
35f23929fbSRobert Watson  * UNIX Domain (Local) Sockets
36f23929fbSRobert Watson  *
37f23929fbSRobert Watson  * This is an implementation of UNIX (local) domain sockets.  Each socket has
38f23929fbSRobert Watson  * an associated struct unpcb (UNIX protocol control block).  Stream sockets
39f23929fbSRobert Watson  * may be connected to 0 or 1 other socket.  Datagram sockets may be
40f23929fbSRobert Watson  * connected to 0, 1, or many other sockets.  Sockets may be created and
41f23929fbSRobert Watson  * connected in pairs (socketpair(2)), or bound/connected to using the file
42f23929fbSRobert Watson  * system name space.  For most purposes, only the receive socket buffer is
43f23929fbSRobert Watson  * used, as sending on one socket delivers directly to the receive socket
445b950deaSRobert Watson  * buffer of a second socket.
455b950deaSRobert Watson  *
465b950deaSRobert Watson  * The implementation is substantially complicated by the fact that
475b950deaSRobert Watson  * "ancillary data", such as file descriptors or credentials, may be passed
485b950deaSRobert Watson  * across UNIX domain sockets.  The potential for passing UNIX domain sockets
495b950deaSRobert Watson  * over other UNIX domain sockets requires the implementation of a simple
505b950deaSRobert Watson  * garbage collector to find and tear down cycles of disconnected sockets.
51aea52f1bSRobert Watson  *
52aea52f1bSRobert Watson  * TODO:
53aea52f1bSRobert Watson  *	SEQPACKET, RDM
54aea52f1bSRobert Watson  *	rethink name space problems
55aea52f1bSRobert Watson  *	need a proper out-of-band
56aea52f1bSRobert Watson  *	lock pushdown
57f23929fbSRobert Watson  */
58f23929fbSRobert Watson 
59677b542eSDavid E. O'Brien #include <sys/cdefs.h>
60677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
61677b542eSDavid E. O'Brien 
6203c96c31SRobert Watson #include "opt_ddb.h"
63335654d7SRobert Watson #include "opt_mac.h"
64335654d7SRobert Watson 
65df8bae1dSRodney W. Grimes #include <sys/param.h>
66fb919e4dSMark Murray #include <sys/domain.h>
67960ed29cSSeigo Tanimura #include <sys/fcntl.h>
68d826c479SBruce Evans #include <sys/malloc.h>		/* XXX must be before <sys/file.h> */
694f590175SPaul Saab #include <sys/eventhandler.h>
70639acc13SGarrett Wollman #include <sys/file.h>
71960ed29cSSeigo Tanimura #include <sys/filedesc.h>
72960ed29cSSeigo Tanimura #include <sys/jail.h>
73960ed29cSSeigo Tanimura #include <sys/kernel.h>
74960ed29cSSeigo Tanimura #include <sys/lock.h>
75639acc13SGarrett Wollman #include <sys/mbuf.h>
76033eb86eSJeff Roberson #include <sys/mount.h>
77960ed29cSSeigo Tanimura #include <sys/mutex.h>
78639acc13SGarrett Wollman #include <sys/namei.h>
79639acc13SGarrett Wollman #include <sys/proc.h>
80df8bae1dSRodney W. Grimes #include <sys/protosw.h>
81960ed29cSSeigo Tanimura #include <sys/resourcevar.h>
82e7c33e29SRobert Watson #include <sys/rwlock.h>
83df8bae1dSRodney W. Grimes #include <sys/socket.h>
84df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
85960ed29cSSeigo Tanimura #include <sys/signalvar.h>
86df8bae1dSRodney W. Grimes #include <sys/stat.h>
87960ed29cSSeigo Tanimura #include <sys/sx.h>
88639acc13SGarrett Wollman #include <sys/sysctl.h>
89960ed29cSSeigo Tanimura #include <sys/systm.h>
90a0ec558aSRobert Watson #include <sys/taskqueue.h>
91639acc13SGarrett Wollman #include <sys/un.h>
9298271db4SGarrett Wollman #include <sys/unpcb.h>
93639acc13SGarrett Wollman #include <sys/vnode.h>
94df8bae1dSRodney W. Grimes 
9503c96c31SRobert Watson #ifdef DDB
9603c96c31SRobert Watson #include <ddb/ddb.h>
9703c96c31SRobert Watson #endif
9803c96c31SRobert Watson 
99aed55708SRobert Watson #include <security/mac/mac_framework.h>
100aed55708SRobert Watson 
1019e9d298aSJeff Roberson #include <vm/uma.h>
10298271db4SGarrett Wollman 
1039e9d298aSJeff Roberson static uma_zone_t	unp_zone;
10498271db4SGarrett Wollman static unp_gen_t	unp_gencnt;
105aea52f1bSRobert Watson static u_int		unp_count;	/* Count of local sockets. */
106aea52f1bSRobert Watson static ino_t		unp_ino;	/* Prototype for fake inode numbers. */
107aea52f1bSRobert Watson static int		unp_rights;	/* File descriptors in flight. */
108aea52f1bSRobert Watson static struct unp_head	unp_shead;	/* List of local stream sockets. */
109aea52f1bSRobert Watson static struct unp_head	unp_dhead;	/* List of local datagram sockets. */
11098271db4SGarrett Wollman 
111aea52f1bSRobert Watson static const struct sockaddr	sun_noname = { sizeof(sun_noname), AF_LOCAL };
11298271db4SGarrett Wollman 
113df8bae1dSRodney W. Grimes /*
114aea52f1bSRobert Watson  * Garbage collection of cyclic file descriptor/socket references occurs
115aea52f1bSRobert Watson  * asynchronously in a taskqueue context in order to avoid recursion and
116aea52f1bSRobert Watson  * reentrance in the UNIX domain socket, file descriptor, and socket layer
117aea52f1bSRobert Watson  * code.  See unp_gc() for a full description.
118df8bae1dSRodney W. Grimes  */
119aea52f1bSRobert Watson static struct task	unp_gc_task;
120f708ef1bSPoul-Henning Kamp 
121ce5f32deSRobert Watson /*
1227e711c3aSRobert Watson  * Both send and receive buffers are allocated PIPSIZ bytes of buffering for
1237e711c3aSRobert Watson  * stream sockets, although the total for sender and receiver is actually
1247e711c3aSRobert Watson  * only PIPSIZ.
1257e711c3aSRobert Watson  *
1267e711c3aSRobert Watson  * Datagram sockets really use the sendspace as the maximum datagram size,
1277e711c3aSRobert Watson  * and don't really want to reserve the sendspace.  Their recvspace should be
1287e711c3aSRobert Watson  * large enough for at least one max-size datagram plus address.
1297e711c3aSRobert Watson  */
1307e711c3aSRobert Watson #ifndef PIPSIZ
1317e711c3aSRobert Watson #define	PIPSIZ	8192
1327e711c3aSRobert Watson #endif
1337e711c3aSRobert Watson static u_long	unpst_sendspace = PIPSIZ;
1347e711c3aSRobert Watson static u_long	unpst_recvspace = PIPSIZ;
1357e711c3aSRobert Watson static u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
1367e711c3aSRobert Watson static u_long	unpdg_recvspace = 4*1024;
1377e711c3aSRobert Watson 
138e4445a03SRobert Watson SYSCTL_NODE(_net, PF_LOCAL, local, CTLFLAG_RW, 0, "Local domain");
139e4445a03SRobert Watson SYSCTL_NODE(_net_local, SOCK_STREAM, stream, CTLFLAG_RW, 0, "SOCK_STREAM");
140e4445a03SRobert Watson SYSCTL_NODE(_net_local, SOCK_DGRAM, dgram, CTLFLAG_RW, 0, "SOCK_DGRAM");
141e4445a03SRobert Watson 
1427e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
1437e711c3aSRobert Watson 	   &unpst_sendspace, 0, "");
1447e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
1457e711c3aSRobert Watson 	   &unpst_recvspace, 0, "");
1467e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
1477e711c3aSRobert Watson 	   &unpdg_sendspace, 0, "");
1487e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
1497e711c3aSRobert Watson 	   &unpdg_recvspace, 0, "");
1507e711c3aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, "");
1517e711c3aSRobert Watson 
152e7c33e29SRobert Watson /*-
153e7c33e29SRobert Watson  * Locking and synchronization:
154ce5f32deSRobert Watson  *
155e7c33e29SRobert Watson  * The global UNIX domain socket rwlock (unp_global_rwlock) protects all
156e7c33e29SRobert Watson  * global variables, including the linked lists tracking the set of allocated
157e7c33e29SRobert Watson  * UNIX domain sockets.  The global rwlock also serves to prevent deadlock
158e7c33e29SRobert Watson  * when more than one PCB lock is acquired at a time (i.e., during
159e7c33e29SRobert Watson  * connect()).  Finally, the global rwlock protects uncounted references from
160e7c33e29SRobert Watson  * vnodes to sockets bound to those vnodes: to safely dereference the
161e7c33e29SRobert Watson  * v_socket pointer, the global rwlock must be held while a full reference is
162e7c33e29SRobert Watson  * acquired.
163ce5f32deSRobert Watson  *
164e7c33e29SRobert Watson  * UNIX domain sockets each have an unpcb hung off of their so_pcb pointer,
165e7c33e29SRobert Watson  * allocated in pru_attach() and freed in pru_detach().  The validity of that
166e7c33e29SRobert Watson  * pointer is an invariant, so no lock is required to dereference the so_pcb
167e7c33e29SRobert Watson  * pointer if a valid socket reference is held by the caller.  In practice,
168e7c33e29SRobert Watson  * this is always true during operations performed on a socket.  Each unpcb
169e7c33e29SRobert Watson  * has a back-pointer to its socket, unp_socket, which will be stable under
170e7c33e29SRobert Watson  * the same circumstances.
171e7c33e29SRobert Watson  *
172e7c33e29SRobert Watson  * This pointer may only be safely dereferenced as long as a valid reference
173e7c33e29SRobert Watson  * to the unpcb is held.  Typically, this reference will be from the socket,
174e7c33e29SRobert Watson  * or from another unpcb when the referring unpcb's lock is held (in order
175e7c33e29SRobert Watson  * that the reference not be invalidated during use).  For example, to follow
176e7c33e29SRobert Watson  * unp->unp_conn->unp_socket, you need unlock the lock on unp, not unp_conn,
177e7c33e29SRobert Watson  * as unp_socket remains valid as long as the reference to unp_conn is valid.
178e7c33e29SRobert Watson  *
179e7c33e29SRobert Watson  * Fields of unpcbss are locked using a per-unpcb lock, unp_mtx.  Individual
180e7c33e29SRobert Watson  * atomic reads without the lock may be performed "lockless", but more
181e7c33e29SRobert Watson  * complex reads and read-modify-writes require the mutex to be held.  No
182e7c33e29SRobert Watson  * lock order is defined between unpcb locks -- multiple unpcb locks may be
183e7c33e29SRobert Watson  * acquired at the same time only when holding the global UNIX domain socket
184e7c33e29SRobert Watson  * rwlock exclusively, which prevents deadlocks.
185e7c33e29SRobert Watson  *
186e7c33e29SRobert Watson  * Blocking with UNIX domain sockets is a tricky issue: unlike most network
187e7c33e29SRobert Watson  * protocols, bind() is a non-atomic operation, and connect() requires
188e7c33e29SRobert Watson  * potential sleeping in the protocol, due to potentially waiting on local or
189e7c33e29SRobert Watson  * distributed file systems.  We try to separate "lookup" operations, which
190e7c33e29SRobert Watson  * may sleep, and the IPC operations themselves, which typically can occur
191e7c33e29SRobert Watson  * with relative atomicity as locks can be held over the entire operation.
192e7c33e29SRobert Watson  *
193e7c33e29SRobert Watson  * Another tricky issue is simultaneous multi-threaded or multi-process
194e7c33e29SRobert Watson  * access to a single UNIX domain socket.  These are handled by the flags
195e7c33e29SRobert Watson  * UNP_CONNECTING and UNP_BINDING, which prevent concurrent connecting or
196e7c33e29SRobert Watson  * binding, both of which involve dropping UNIX domain socket locks in order
197e7c33e29SRobert Watson  * to perform namei() and other file system operations.
198ce5f32deSRobert Watson  */
199e7c33e29SRobert Watson static struct rwlock	unp_global_rwlock;
200e7c33e29SRobert Watson 
201e7c33e29SRobert Watson #define	UNP_GLOBAL_LOCK_INIT()		rw_init(&unp_global_rwlock,	\
202e7c33e29SRobert Watson 					    "unp_global_rwlock")
203e7c33e29SRobert Watson 
204e7c33e29SRobert Watson #define	UNP_GLOBAL_LOCK_ASSERT()	rw_assert(&unp_global_rwlock,	\
205e7c33e29SRobert Watson 					    RA_LOCKED)
206e7c33e29SRobert Watson #define	UNP_GLOBAL_UNLOCK_ASSERT()	rw_assert(&unp_global_rwlock,	\
207e7c33e29SRobert Watson 					    RA_UNLOCKED)
208e7c33e29SRobert Watson 
209e7c33e29SRobert Watson #define	UNP_GLOBAL_WLOCK()		rw_wlock(&unp_global_rwlock)
210e7c33e29SRobert Watson #define	UNP_GLOBAL_WUNLOCK()		rw_wunlock(&unp_global_rwlock)
211e7c33e29SRobert Watson #define	UNP_GLOBAL_WLOCK_ASSERT()	rw_assert(&unp_global_rwlock,	\
212e7c33e29SRobert Watson 					    RA_WLOCKED)
213e7c33e29SRobert Watson #define	UNP_GLOBAL_WOWNED()		rw_wowned(&unp_global_rwlock)
214e7c33e29SRobert Watson 
215e7c33e29SRobert Watson #define	UNP_GLOBAL_RLOCK()		rw_rlock(&unp_global_rwlock)
216e7c33e29SRobert Watson #define	UNP_GLOBAL_RUNLOCK()		rw_runlock(&unp_global_rwlock)
217e7c33e29SRobert Watson #define	UNP_GLOBAL_RLOCK_ASSERT()	rw_assert(&unp_global_rwlock,	\
218e7c33e29SRobert Watson 					    RA_RLOCKED)
219e7c33e29SRobert Watson 
220e7c33e29SRobert Watson #define UNP_PCB_LOCK_INIT(unp)		mtx_init(&(unp)->unp_mtx,	\
221e7c33e29SRobert Watson 					    "unp_mtx", "unp_mtx",	\
222e7c33e29SRobert Watson 					    MTX_DUPOK|MTX_DEF|MTX_RECURSE)
223e7c33e29SRobert Watson #define	UNP_PCB_LOCK_DESTROY(unp)	mtx_destroy(&(unp)->unp_mtx)
224e7c33e29SRobert Watson #define	UNP_PCB_LOCK(unp)		mtx_lock(&(unp)->unp_mtx)
225e7c33e29SRobert Watson #define	UNP_PCB_UNLOCK(unp)		mtx_unlock(&(unp)->unp_mtx)
226e7c33e29SRobert Watson #define	UNP_PCB_LOCK_ASSERT(unp)	mtx_assert(&(unp)->unp_mtx, MA_OWNED)
2270d9ce3a1SRobert Watson 
228aea52f1bSRobert Watson static int	unp_connect(struct socket *, struct sockaddr *,
229aea52f1bSRobert Watson 		    struct thread *);
2306a2989fdSMatthew N. Dodd static int	unp_connect2(struct socket *so, struct socket *so2, int);
231e7c33e29SRobert Watson static void	unp_disconnect(struct unpcb *unp, struct unpcb *unp2);
2324d77a549SAlfred Perlstein static void	unp_shutdown(struct unpcb *);
2334d77a549SAlfred Perlstein static void	unp_drop(struct unpcb *, int);
234a0ec558aSRobert Watson static void	unp_gc(__unused void *, int);
2354d77a549SAlfred Perlstein static void	unp_scan(struct mbuf *, void (*)(struct file *));
2364d77a549SAlfred Perlstein static void	unp_discard(struct file *);
2374d77a549SAlfred Perlstein static void	unp_freerights(struct file **, int);
2384d77a549SAlfred Perlstein static int	unp_internalize(struct mbuf **, struct thread *);
239397c19d1SJeff Roberson static void	unp_internalize_fp(struct file *);
240397c19d1SJeff Roberson static void	unp_externalize_fp(struct file *);
2415b950deaSRobert Watson static struct mbuf	*unp_addsockcred(struct thread *, struct mbuf *);
242f708ef1bSPoul-Henning Kamp 
243e4445a03SRobert Watson /*
244e4445a03SRobert Watson  * Definitions of protocols supported in the LOCAL domain.
245e4445a03SRobert Watson  */
246e4445a03SRobert Watson static struct domain localdomain;
247e4445a03SRobert Watson static struct protosw localsw[] = {
248e4445a03SRobert Watson {
249e4445a03SRobert Watson 	.pr_type =		SOCK_STREAM,
250e4445a03SRobert Watson 	.pr_domain =		&localdomain,
251e4445a03SRobert Watson 	.pr_flags =		PR_CONNREQUIRED|PR_WANTRCVD|PR_RIGHTS,
252e4445a03SRobert Watson 	.pr_ctloutput =		&uipc_ctloutput,
253e4445a03SRobert Watson 	.pr_usrreqs =		&uipc_usrreqs
254e4445a03SRobert Watson },
255e4445a03SRobert Watson {
256e4445a03SRobert Watson 	.pr_type =		SOCK_DGRAM,
257e4445a03SRobert Watson 	.pr_domain =		&localdomain,
258e4445a03SRobert Watson 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_RIGHTS,
259e4445a03SRobert Watson 	.pr_usrreqs =		&uipc_usrreqs
260e4445a03SRobert Watson },
261e4445a03SRobert Watson };
262e4445a03SRobert Watson 
263e4445a03SRobert Watson static struct domain localdomain = {
264e4445a03SRobert Watson 	.dom_family =		AF_LOCAL,
265e4445a03SRobert Watson 	.dom_name =		"local",
266e4445a03SRobert Watson 	.dom_init =		unp_init,
267e4445a03SRobert Watson 	.dom_externalize =	unp_externalize,
268e4445a03SRobert Watson 	.dom_dispose =		unp_dispose,
269e4445a03SRobert Watson 	.dom_protosw =		localsw,
270e4445a03SRobert Watson 	.dom_protoswNPROTOSW =	&localsw[sizeof(localsw)/sizeof(localsw[0])]
271e4445a03SRobert Watson };
272e4445a03SRobert Watson DOMAIN_SET(local);
273e4445a03SRobert Watson 
274ac45e92fSRobert Watson static void
275a29f300eSGarrett Wollman uipc_abort(struct socket *so)
276df8bae1dSRodney W. Grimes {
277e7c33e29SRobert Watson 	struct unpcb *unp, *unp2;
278df8bae1dSRodney W. Grimes 
27940f2ac28SRobert Watson 	unp = sotounpcb(so);
2804d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_abort: unp == NULL"));
281e7c33e29SRobert Watson 
282e7c33e29SRobert Watson 	UNP_GLOBAL_WLOCK();
283e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
284e7c33e29SRobert Watson 	unp2 = unp->unp_conn;
285e7c33e29SRobert Watson 	if (unp2 != NULL) {
286e7c33e29SRobert Watson 		UNP_PCB_LOCK(unp2);
287e7c33e29SRobert Watson 		unp_drop(unp2, ECONNABORTED);
288e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp2);
289e7c33e29SRobert Watson 	}
290e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
291e7c33e29SRobert Watson 	UNP_GLOBAL_WUNLOCK();
292df8bae1dSRodney W. Grimes }
293df8bae1dSRodney W. Grimes 
294a29f300eSGarrett Wollman static int
29557bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam)
296a29f300eSGarrett Wollman {
297e7c33e29SRobert Watson 	struct unpcb *unp, *unp2;
2980d9ce3a1SRobert Watson 	const struct sockaddr *sa;
299df8bae1dSRodney W. Grimes 
300df8bae1dSRodney W. Grimes 	/*
3011c381b19SRobert Watson 	 * Pass back name of connected socket, if it was bound and we are
3021c381b19SRobert Watson 	 * still connected (our peer may have closed already!).
303df8bae1dSRodney W. Grimes 	 */
3044d4b555eSRobert Watson 	unp = sotounpcb(so);
3054d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_accept: unp == NULL"));
306e7c33e29SRobert Watson 
3070d9ce3a1SRobert Watson 	*nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
308e7c33e29SRobert Watson 	UNP_GLOBAL_RLOCK();
309e7c33e29SRobert Watson 	unp2 = unp->unp_conn;
310e7c33e29SRobert Watson 	if (unp2 != NULL && unp2->unp_addr != NULL) {
311e7c33e29SRobert Watson 		UNP_PCB_LOCK(unp2);
312e7c33e29SRobert Watson 		sa = (struct sockaddr *) unp2->unp_addr;
313e7c33e29SRobert Watson 		bcopy(sa, *nam, sa->sa_len);
314e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp2);
315e7c33e29SRobert Watson 	} else {
3160d9ce3a1SRobert Watson 		sa = &sun_noname;
3170d9ce3a1SRobert Watson 		bcopy(sa, *nam, sa->sa_len);
318e7c33e29SRobert Watson 	}
319e7c33e29SRobert Watson 	UNP_GLOBAL_RUNLOCK();
320e5aeaa0cSDag-Erling Smørgrav 	return (0);
321a29f300eSGarrett Wollman }
322df8bae1dSRodney W. Grimes 
323a29f300eSGarrett Wollman static int
324b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td)
325a29f300eSGarrett Wollman {
326e7c33e29SRobert Watson 	u_long sendspace, recvspace;
3276d32873cSRobert Watson 	struct unpcb *unp;
328e7c33e29SRobert Watson 	int error, locked;
329df8bae1dSRodney W. Grimes 
3306d32873cSRobert Watson 	KASSERT(so->so_pcb == NULL, ("uipc_attach: so_pcb != NULL"));
3316d32873cSRobert Watson 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
3326d32873cSRobert Watson 		switch (so->so_type) {
3336d32873cSRobert Watson 		case SOCK_STREAM:
334e7c33e29SRobert Watson 			sendspace = unpst_sendspace;
335e7c33e29SRobert Watson 			recvspace = unpst_recvspace;
3366d32873cSRobert Watson 			break;
3376d32873cSRobert Watson 
3386d32873cSRobert Watson 		case SOCK_DGRAM:
339e7c33e29SRobert Watson 			sendspace = unpdg_sendspace;
340e7c33e29SRobert Watson 			recvspace = unpdg_recvspace;
3416d32873cSRobert Watson 			break;
3426d32873cSRobert Watson 
3436d32873cSRobert Watson 		default:
344e7c33e29SRobert Watson 			panic("uipc_attach");
3456d32873cSRobert Watson 		}
346e7c33e29SRobert Watson 		error = soreserve(so, sendspace, recvspace);
3476d32873cSRobert Watson 		if (error)
3486d32873cSRobert Watson 			return (error);
3496d32873cSRobert Watson 	}
35046a1d9bfSRobert Watson 	unp = uma_zalloc(unp_zone, M_NOWAIT | M_ZERO);
3516d32873cSRobert Watson 	if (unp == NULL)
3526d32873cSRobert Watson 		return (ENOBUFS);
3536d32873cSRobert Watson 	LIST_INIT(&unp->unp_refs);
354e7c33e29SRobert Watson 	UNP_PCB_LOCK_INIT(unp);
3556d32873cSRobert Watson 	unp->unp_socket = so;
3566d32873cSRobert Watson 	so->so_pcb = unp;
3579ae328fcSJohn Baldwin 	unp->unp_refcount = 1;
358e7c33e29SRobert Watson 
359e7c33e29SRobert Watson 	/*
360e7c33e29SRobert Watson 	 * uipc_attach() may be called indirectly from within the UNIX domain
361e7c33e29SRobert Watson 	 * socket code via sonewconn() in unp_connect().  Since rwlocks can
362e7c33e29SRobert Watson 	 * not be recursed, we do the closest thing.
363e7c33e29SRobert Watson 	 */
364d7924b70SRobert Watson 	locked = 0;
365e7c33e29SRobert Watson 	if (!UNP_GLOBAL_WOWNED()) {
366e7c33e29SRobert Watson 		UNP_GLOBAL_WLOCK();
367e7c33e29SRobert Watson 		locked = 1;
368e7c33e29SRobert Watson 	}
3696d32873cSRobert Watson 	unp->unp_gencnt = ++unp_gencnt;
3706d32873cSRobert Watson 	unp_count++;
371b7e2f3ecSRobert Watson 	LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead : &unp_shead,
372b7e2f3ecSRobert Watson 	    unp, unp_link);
373e7c33e29SRobert Watson 	if (locked)
374e7c33e29SRobert Watson 		UNP_GLOBAL_WUNLOCK();
3756d32873cSRobert Watson 
3766d32873cSRobert Watson 	return (0);
377a29f300eSGarrett Wollman }
378a29f300eSGarrett Wollman 
379a29f300eSGarrett Wollman static int
380b40ce416SJulian Elischer uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
381a29f300eSGarrett Wollman {
382dd47f5caSRobert Watson 	struct sockaddr_un *soun = (struct sockaddr_un *)nam;
383dd47f5caSRobert Watson 	struct vattr vattr;
3849e289446SWojciech A. Koszek 	int error, namelen, vfslocked;
385dd47f5caSRobert Watson 	struct nameidata nd;
38640f2ac28SRobert Watson 	struct unpcb *unp;
387dd47f5caSRobert Watson 	struct vnode *vp;
388dd47f5caSRobert Watson 	struct mount *mp;
389dd47f5caSRobert Watson 	char *buf;
390a29f300eSGarrett Wollman 
39140f2ac28SRobert Watson 	unp = sotounpcb(so);
3924d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_bind: unp == NULL"));
3934f1f0ef5SRobert Watson 
3944f1f0ef5SRobert Watson 	namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
3954f1f0ef5SRobert Watson 	if (namelen <= 0)
3964f1f0ef5SRobert Watson 		return (EINVAL);
397dd47f5caSRobert Watson 
398dd47f5caSRobert Watson 	/*
3994f1f0ef5SRobert Watson 	 * We don't allow simultaneous bind() calls on a single UNIX domain
4004f1f0ef5SRobert Watson 	 * socket, so flag in-progress operations, and return an error if an
4014f1f0ef5SRobert Watson 	 * operation is already in progress.
4024f1f0ef5SRobert Watson 	 *
4034f1f0ef5SRobert Watson 	 * Historically, we have not allowed a socket to be rebound, so this
404d7924b70SRobert Watson 	 * also returns an error.  Not allowing re-binding simplifies the
405d7924b70SRobert Watson 	 * implementation and avoids a great many possible failure modes.
406dd47f5caSRobert Watson 	 */
407e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
408dd47f5caSRobert Watson 	if (unp->unp_vnode != NULL) {
409e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
410dd47f5caSRobert Watson 		return (EINVAL);
411dd47f5caSRobert Watson 	}
4124f1f0ef5SRobert Watson 	if (unp->unp_flags & UNP_BINDING) {
413e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
4144f1f0ef5SRobert Watson 		return (EALREADY);
415dd47f5caSRobert Watson 	}
4164f1f0ef5SRobert Watson 	unp->unp_flags |= UNP_BINDING;
417e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
418dd47f5caSRobert Watson 
419dd47f5caSRobert Watson 	buf = malloc(namelen + 1, M_TEMP, M_WAITOK);
420dd47f5caSRobert Watson 	strlcpy(buf, soun->sun_path, namelen + 1);
421dd47f5caSRobert Watson 
422dd47f5caSRobert Watson restart:
4239e289446SWojciech A. Koszek 	vfslocked = 0;
4249e289446SWojciech A. Koszek 	NDINIT(&nd, CREATE, MPSAFE | NOFOLLOW | LOCKPARENT | SAVENAME,
4259e289446SWojciech A. Koszek 	    UIO_SYSSPACE, buf, td);
426dd47f5caSRobert Watson /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
427dd47f5caSRobert Watson 	error = namei(&nd);
428dd47f5caSRobert Watson 	if (error)
4294f1f0ef5SRobert Watson 		goto error;
430dd47f5caSRobert Watson 	vp = nd.ni_vp;
4319e289446SWojciech A. Koszek 	vfslocked = NDHASGIANT(&nd);
432dd47f5caSRobert Watson 	if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
433dd47f5caSRobert Watson 		NDFREE(&nd, NDF_ONLY_PNBUF);
434dd47f5caSRobert Watson 		if (nd.ni_dvp == vp)
435dd47f5caSRobert Watson 			vrele(nd.ni_dvp);
436dd47f5caSRobert Watson 		else
437dd47f5caSRobert Watson 			vput(nd.ni_dvp);
438dd47f5caSRobert Watson 		if (vp != NULL) {
439dd47f5caSRobert Watson 			vrele(vp);
440dd47f5caSRobert Watson 			error = EADDRINUSE;
4414f1f0ef5SRobert Watson 			goto error;
442dd47f5caSRobert Watson 		}
443dd47f5caSRobert Watson 		error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
444dd47f5caSRobert Watson 		if (error)
4454f1f0ef5SRobert Watson 			goto error;
4469e289446SWojciech A. Koszek 		VFS_UNLOCK_GIANT(vfslocked);
447dd47f5caSRobert Watson 		goto restart;
448dd47f5caSRobert Watson 	}
449dd47f5caSRobert Watson 	VATTR_NULL(&vattr);
450dd47f5caSRobert Watson 	vattr.va_type = VSOCK;
451dd47f5caSRobert Watson 	vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask);
452dd47f5caSRobert Watson #ifdef MAC
45330d239bcSRobert Watson 	error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
454dd47f5caSRobert Watson 	    &vattr);
455dd47f5caSRobert Watson #endif
456dd47f5caSRobert Watson 	if (error == 0) {
457dd47f5caSRobert Watson 		VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
458dd47f5caSRobert Watson 		error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
459dd47f5caSRobert Watson 	}
460dd47f5caSRobert Watson 	NDFREE(&nd, NDF_ONLY_PNBUF);
461dd47f5caSRobert Watson 	vput(nd.ni_dvp);
462dd47f5caSRobert Watson 	if (error) {
463dd47f5caSRobert Watson 		vn_finished_write(mp);
4644f1f0ef5SRobert Watson 		goto error;
465dd47f5caSRobert Watson 	}
466dd47f5caSRobert Watson 	vp = nd.ni_vp;
46757fd3d55SPawel Jakub Dawidek 	ASSERT_VOP_ELOCKED(vp, "uipc_bind");
468dd47f5caSRobert Watson 	soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK);
469e7c33e29SRobert Watson 
470e7c33e29SRobert Watson 	UNP_GLOBAL_WLOCK();
471e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
472dd47f5caSRobert Watson 	vp->v_socket = unp->unp_socket;
473dd47f5caSRobert Watson 	unp->unp_vnode = vp;
474dd47f5caSRobert Watson 	unp->unp_addr = soun;
4754f1f0ef5SRobert Watson 	unp->unp_flags &= ~UNP_BINDING;
476e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
477e7c33e29SRobert Watson 	UNP_GLOBAL_WUNLOCK();
478dd47f5caSRobert Watson 	VOP_UNLOCK(vp, 0, td);
479dd47f5caSRobert Watson 	vn_finished_write(mp);
4809e289446SWojciech A. Koszek 	VFS_UNLOCK_GIANT(vfslocked);
4814f1f0ef5SRobert Watson 	free(buf, M_TEMP);
4824f1f0ef5SRobert Watson 	return (0);
483e7c33e29SRobert Watson 
4844f1f0ef5SRobert Watson error:
4859e289446SWojciech A. Koszek 	VFS_UNLOCK_GIANT(vfslocked);
486e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
4874f1f0ef5SRobert Watson 	unp->unp_flags &= ~UNP_BINDING;
488e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
489dd47f5caSRobert Watson 	free(buf, M_TEMP);
49040f2ac28SRobert Watson 	return (error);
491a29f300eSGarrett Wollman }
492a29f300eSGarrett Wollman 
493a29f300eSGarrett Wollman static int
494b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
495a29f300eSGarrett Wollman {
4960d9ce3a1SRobert Watson 	int error;
497a29f300eSGarrett Wollman 
498fd179ee9SRobert Watson 	KASSERT(td == curthread, ("uipc_connect: td != curthread"));
499e7c33e29SRobert Watson 	UNP_GLOBAL_WLOCK();
500fd179ee9SRobert Watson 	error = unp_connect(so, nam, td);
501e7c33e29SRobert Watson 	UNP_GLOBAL_WUNLOCK();
5020d9ce3a1SRobert Watson 	return (error);
503a29f300eSGarrett Wollman }
504a29f300eSGarrett Wollman 
505a152f8a3SRobert Watson static void
506a152f8a3SRobert Watson uipc_close(struct socket *so)
507a152f8a3SRobert Watson {
508e7c33e29SRobert Watson 	struct unpcb *unp, *unp2;
509a152f8a3SRobert Watson 
510a152f8a3SRobert Watson 	unp = sotounpcb(so);
511a152f8a3SRobert Watson 	KASSERT(unp != NULL, ("uipc_close: unp == NULL"));
512e7c33e29SRobert Watson 
513e7c33e29SRobert Watson 	UNP_GLOBAL_WLOCK();
514e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
515e7c33e29SRobert Watson 	unp2 = unp->unp_conn;
516e7c33e29SRobert Watson 	if (unp2 != NULL) {
517e7c33e29SRobert Watson 		UNP_PCB_LOCK(unp2);
518e7c33e29SRobert Watson 		unp_disconnect(unp, unp2);
519e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp2);
520e7c33e29SRobert Watson 	}
521e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
522e7c33e29SRobert Watson 	UNP_GLOBAL_WUNLOCK();
523a152f8a3SRobert Watson }
524a152f8a3SRobert Watson 
525db48c0d2SRobert Watson int
526a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2)
527a29f300eSGarrett Wollman {
528e7c33e29SRobert Watson 	struct unpcb *unp, *unp2;
5290d9ce3a1SRobert Watson 	int error;
530a29f300eSGarrett Wollman 
531e7c33e29SRobert Watson 	UNP_GLOBAL_WLOCK();
532e7c33e29SRobert Watson 	unp = so1->so_pcb;
5334d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_connect2: unp == NULL"));
534e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
535e7c33e29SRobert Watson 	unp2 = so2->so_pcb;
536e7c33e29SRobert Watson 	KASSERT(unp2 != NULL, ("uipc_connect2: unp2 == NULL"));
537e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp2);
5386a2989fdSMatthew N. Dodd 	error = unp_connect2(so1, so2, PRU_CONNECT2);
539e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp2);
540e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
541e7c33e29SRobert Watson 	UNP_GLOBAL_WUNLOCK();
5420d9ce3a1SRobert Watson 	return (error);
543a29f300eSGarrett Wollman }
544a29f300eSGarrett Wollman 
545a29f300eSGarrett Wollman /* control is EOPNOTSUPP */
546a29f300eSGarrett Wollman 
547bc725eafSRobert Watson static void
548a29f300eSGarrett Wollman uipc_detach(struct socket *so)
549a29f300eSGarrett Wollman {
550e7c33e29SRobert Watson 	struct unpcb *unp, *unp2;
5519ae328fcSJohn Baldwin 	struct sockaddr_un *saved_unp_addr;
5526d32873cSRobert Watson 	struct vnode *vp;
5539ae328fcSJohn Baldwin 	int freeunp, local_unp_rights;
554a29f300eSGarrett Wollman 
55540f2ac28SRobert Watson 	unp = sotounpcb(so);
5564d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_detach: unp == NULL"));
557e7c33e29SRobert Watson 
558e7c33e29SRobert Watson 	UNP_GLOBAL_WLOCK();
559e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
560e7c33e29SRobert Watson 
5616d32873cSRobert Watson 	LIST_REMOVE(unp, unp_link);
5626d32873cSRobert Watson 	unp->unp_gencnt = ++unp_gencnt;
5636d32873cSRobert Watson 	--unp_count;
564e7c33e29SRobert Watson 
565e7c33e29SRobert Watson 	/*
566e7c33e29SRobert Watson 	 * XXXRW: Should assert vp->v_socket == so.
567e7c33e29SRobert Watson 	 */
5686d32873cSRobert Watson 	if ((vp = unp->unp_vnode) != NULL) {
5696d32873cSRobert Watson 		unp->unp_vnode->v_socket = NULL;
5706d32873cSRobert Watson 		unp->unp_vnode = NULL;
5716d32873cSRobert Watson 	}
572e7c33e29SRobert Watson 	unp2 = unp->unp_conn;
573e7c33e29SRobert Watson 	if (unp2 != NULL) {
574e7c33e29SRobert Watson 		UNP_PCB_LOCK(unp2);
575e7c33e29SRobert Watson 		unp_disconnect(unp, unp2);
576e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp2);
577e7c33e29SRobert Watson 	}
578e7c33e29SRobert Watson 
579e7c33e29SRobert Watson 	/*
580e7c33e29SRobert Watson 	 * We hold the global lock, so it's OK to acquire multiple pcb locks
581e7c33e29SRobert Watson 	 * at a time.
582e7c33e29SRobert Watson 	 */
5836d32873cSRobert Watson 	while (!LIST_EMPTY(&unp->unp_refs)) {
5846d32873cSRobert Watson 		struct unpcb *ref = LIST_FIRST(&unp->unp_refs);
585e7c33e29SRobert Watson 
586e7c33e29SRobert Watson 		UNP_PCB_LOCK(ref);
5876d32873cSRobert Watson 		unp_drop(ref, ECONNRESET);
588e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(ref);
5896d32873cSRobert Watson 	}
590397c19d1SJeff Roberson 	local_unp_rights = unp_rights;
591e7c33e29SRobert Watson 	UNP_GLOBAL_WUNLOCK();
5926d32873cSRobert Watson 	unp->unp_socket->so_pcb = NULL;
5939ae328fcSJohn Baldwin 	saved_unp_addr = unp->unp_addr;
5949ae328fcSJohn Baldwin 	unp->unp_addr = NULL;
5959ae328fcSJohn Baldwin 	unp->unp_refcount--;
5969ae328fcSJohn Baldwin 	freeunp = (unp->unp_refcount == 0);
5979ae328fcSJohn Baldwin 	if (saved_unp_addr != NULL)
5989ae328fcSJohn Baldwin 		FREE(saved_unp_addr, M_SONAME);
599e7c33e29SRobert Watson 	if (freeunp) {
600e7c33e29SRobert Watson 		UNP_PCB_LOCK_DESTROY(unp);
6016d32873cSRobert Watson 		uma_zfree(unp_zone, unp);
6026e2faa24SRobert Watson 	} else
6036e2faa24SRobert Watson 		UNP_PCB_UNLOCK(unp);
6046d32873cSRobert Watson 	if (vp) {
6056d32873cSRobert Watson 		int vfslocked;
6066d32873cSRobert Watson 
6076d32873cSRobert Watson 		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
6086d32873cSRobert Watson 		vrele(vp);
6096d32873cSRobert Watson 		VFS_UNLOCK_GIANT(vfslocked);
6106d32873cSRobert Watson 	}
6116d32873cSRobert Watson 	if (local_unp_rights)
6126d32873cSRobert Watson 		taskqueue_enqueue(taskqueue_thread, &unp_gc_task);
613a29f300eSGarrett Wollman }
614a29f300eSGarrett Wollman 
615a29f300eSGarrett Wollman static int
616a29f300eSGarrett Wollman uipc_disconnect(struct socket *so)
617a29f300eSGarrett Wollman {
618e7c33e29SRobert Watson 	struct unpcb *unp, *unp2;
619a29f300eSGarrett Wollman 
62040f2ac28SRobert Watson 	unp = sotounpcb(so);
6214d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_disconnect: unp == NULL"));
622e7c33e29SRobert Watson 
623e7c33e29SRobert Watson 	UNP_GLOBAL_WLOCK();
624e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
625e7c33e29SRobert Watson 	unp2 = unp->unp_conn;
626e7c33e29SRobert Watson 	if (unp2 != NULL) {
627e7c33e29SRobert Watson 		UNP_PCB_LOCK(unp2);
628e7c33e29SRobert Watson 		unp_disconnect(unp, unp2);
629e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp2);
630e7c33e29SRobert Watson 	}
631e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
632e7c33e29SRobert Watson 	UNP_GLOBAL_WUNLOCK();
633e5aeaa0cSDag-Erling Smørgrav 	return (0);
634a29f300eSGarrett Wollman }
635a29f300eSGarrett Wollman 
636a29f300eSGarrett Wollman static int
637d374e81eSRobert Watson uipc_listen(struct socket *so, int backlog, struct thread *td)
638a29f300eSGarrett Wollman {
63940f2ac28SRobert Watson 	struct unpcb *unp;
6400d9ce3a1SRobert Watson 	int error;
641a29f300eSGarrett Wollman 
64240f2ac28SRobert Watson 	unp = sotounpcb(so);
6434d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_listen: unp == NULL"));
644e7c33e29SRobert Watson 
645e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
6464d4b555eSRobert Watson 	if (unp->unp_vnode == NULL) {
647e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
64840f2ac28SRobert Watson 		return (EINVAL);
64940f2ac28SRobert Watson 	}
650e7c33e29SRobert Watson 
651e7c33e29SRobert Watson 	SOCK_LOCK(so);
652e7c33e29SRobert Watson 	error = solisten_proto_check(so);
653e7c33e29SRobert Watson 	if (error == 0) {
654e7c33e29SRobert Watson 		cru2x(td->td_ucred, &unp->unp_peercred);
655e7c33e29SRobert Watson 		unp->unp_flags |= UNP_HAVEPCCACHED;
656e7c33e29SRobert Watson 		solisten_proto(so, backlog);
657e7c33e29SRobert Watson 	}
658e7c33e29SRobert Watson 	SOCK_UNLOCK(so);
659e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
6600d9ce3a1SRobert Watson 	return (error);
661a29f300eSGarrett Wollman }
662a29f300eSGarrett Wollman 
663a29f300eSGarrett Wollman static int
66457bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam)
665a29f300eSGarrett Wollman {
666e7c33e29SRobert Watson 	struct unpcb *unp, *unp2;
6670d9ce3a1SRobert Watson 	const struct sockaddr *sa;
668a29f300eSGarrett Wollman 
6694d4b555eSRobert Watson 	unp = sotounpcb(so);
6704d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL"));
671e7c33e29SRobert Watson 
6720d9ce3a1SRobert Watson 	*nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
673e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
674bdc5f6a3SHajimu UMEMOTO 	/*
675e7c33e29SRobert Watson 	 * XXX: It seems that this test always fails even when connection is
676e7c33e29SRobert Watson 	 * established.  So, this else clause is added as workaround to
677e7c33e29SRobert Watson 	 * return PF_LOCAL sockaddr.
678bdc5f6a3SHajimu UMEMOTO 	 */
679e7c33e29SRobert Watson 	unp2 = unp->unp_conn;
680e7c33e29SRobert Watson 	if (unp2 != NULL) {
681e7c33e29SRobert Watson 		UNP_PCB_LOCK(unp2);
682e7c33e29SRobert Watson 		if (unp2->unp_addr != NULL)
683e7c33e29SRobert Watson 			sa = (struct sockaddr *) unp->unp_conn->unp_addr;
684e7c33e29SRobert Watson 		else
6850d9ce3a1SRobert Watson 			sa = &sun_noname;
6860d9ce3a1SRobert Watson 		bcopy(sa, *nam, sa->sa_len);
687e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp2);
688e7c33e29SRobert Watson 	} else {
689e7c33e29SRobert Watson 		sa = &sun_noname;
690e7c33e29SRobert Watson 		bcopy(sa, *nam, sa->sa_len);
691e7c33e29SRobert Watson 	}
692e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
693e5aeaa0cSDag-Erling Smørgrav 	return (0);
694a29f300eSGarrett Wollman }
695a29f300eSGarrett Wollman 
696a29f300eSGarrett Wollman static int
697a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags)
698a29f300eSGarrett Wollman {
699e7c33e29SRobert Watson 	struct unpcb *unp, *unp2;
700a29f300eSGarrett Wollman 	struct socket *so2;
701337cc6b6SRobert Watson 	u_int mbcnt, sbcc;
7026aef685fSBrian Feldman 	u_long newhiwat;
703a29f300eSGarrett Wollman 
70440f2ac28SRobert Watson 	unp = sotounpcb(so);
7054d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_rcvd: unp == NULL"));
706df8bae1dSRodney W. Grimes 
707e7c33e29SRobert Watson 	if (so->so_type == SOCK_DGRAM)
708e7c33e29SRobert Watson 		panic("uipc_rcvd DGRAM?");
709e7c33e29SRobert Watson 
710e7c33e29SRobert Watson 	if (so->so_type != SOCK_STREAM)
711e7c33e29SRobert Watson 		panic("uipc_rcvd unknown socktype");
712e7c33e29SRobert Watson 
713df8bae1dSRodney W. Grimes 	/*
714e7c33e29SRobert Watson 	 * Adjust backpressure on sender and wakeup any waiting to write.
715e7c33e29SRobert Watson 	 *
716d7924b70SRobert Watson 	 * The unp lock is acquired to maintain the validity of the unp_conn
717d7924b70SRobert Watson 	 * pointer; no lock on unp2 is required as unp2->unp_socket will be
718d7924b70SRobert Watson 	 * static as long as we don't permit unp2 to disconnect from unp,
719d7924b70SRobert Watson 	 * which is prevented by the lock on unp.  We cache values from
720d7924b70SRobert Watson 	 * so_rcv to avoid holding the so_rcv lock over the entire
721d7924b70SRobert Watson 	 * transaction on the remote so_snd.
722df8bae1dSRodney W. Grimes 	 */
723337cc6b6SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
724337cc6b6SRobert Watson 	mbcnt = so->so_rcv.sb_mbcnt;
725337cc6b6SRobert Watson 	sbcc = so->so_rcv.sb_cc;
726337cc6b6SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
727e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
728e7c33e29SRobert Watson 	unp2 = unp->unp_conn;
729e7c33e29SRobert Watson 	if (unp2 == NULL) {
730e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
731e7c33e29SRobert Watson 		return (0);
732337cc6b6SRobert Watson 	}
733e7c33e29SRobert Watson 	so2 = unp2->unp_socket;
734337cc6b6SRobert Watson 	SOCKBUF_LOCK(&so2->so_snd);
735337cc6b6SRobert Watson 	so2->so_snd.sb_mbmax += unp->unp_mbcnt - mbcnt;
736337cc6b6SRobert Watson 	newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - sbcc;
737f535380cSDon Lewis 	(void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat,
7386aef685fSBrian Feldman 	    newhiwat, RLIM_INFINITY);
7391e4d7da7SRobert Watson 	sowwakeup_locked(so2);
740337cc6b6SRobert Watson 	unp->unp_mbcnt = mbcnt;
741337cc6b6SRobert Watson 	unp->unp_cc = sbcc;
742e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
743e5aeaa0cSDag-Erling Smørgrav 	return (0);
744a29f300eSGarrett Wollman }
745df8bae1dSRodney W. Grimes 
746a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */
747a29f300eSGarrett Wollman 
748a29f300eSGarrett Wollman static int
74957bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
750b40ce416SJulian Elischer     struct mbuf *control, struct thread *td)
751a29f300eSGarrett Wollman {
752f3f49bbbSRobert Watson 	struct unpcb *unp, *unp2;
753a29f300eSGarrett Wollman 	struct socket *so2;
754337cc6b6SRobert Watson 	u_int mbcnt, sbcc;
7556aef685fSBrian Feldman 	u_long newhiwat;
756f3f49bbbSRobert Watson 	int error = 0;
757a29f300eSGarrett Wollman 
75840f2ac28SRobert Watson 	unp = sotounpcb(so);
7594d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_send: unp == NULL"));
760e7c33e29SRobert Watson 
761a29f300eSGarrett Wollman 	if (flags & PRUS_OOB) {
762a29f300eSGarrett Wollman 		error = EOPNOTSUPP;
763a29f300eSGarrett Wollman 		goto release;
764a29f300eSGarrett Wollman 	}
765a29f300eSGarrett Wollman 
766fc3fcacfSRobert Watson 	if (control != NULL && (error = unp_internalize(&control, td)))
767a29f300eSGarrett Wollman 		goto release;
768df8bae1dSRodney W. Grimes 
769e7c33e29SRobert Watson 	if ((nam != NULL) || (flags & PRUS_EOF))
770e7c33e29SRobert Watson 		UNP_GLOBAL_WLOCK();
771e7c33e29SRobert Watson 	else
772e7c33e29SRobert Watson 		UNP_GLOBAL_RLOCK();
773e7c33e29SRobert Watson 
774a29f300eSGarrett Wollman 	switch (so->so_type) {
775a29f300eSGarrett Wollman 	case SOCK_DGRAM:
776a29f300eSGarrett Wollman 	{
777e7dd9a10SRobert Watson 		const struct sockaddr *from;
778df8bae1dSRodney W. Grimes 
779e7c33e29SRobert Watson 		unp2 = unp->unp_conn;
780fc3fcacfSRobert Watson 		if (nam != NULL) {
781ede6e136SRobert Watson 			UNP_GLOBAL_WLOCK_ASSERT();
782e7c33e29SRobert Watson 			if (unp2 != NULL) {
783df8bae1dSRodney W. Grimes 				error = EISCONN;
784df8bae1dSRodney W. Grimes 				break;
785df8bae1dSRodney W. Grimes 			}
786b40ce416SJulian Elischer 			error = unp_connect(so, nam, td);
787df8bae1dSRodney W. Grimes 			if (error)
788df8bae1dSRodney W. Grimes 				break;
789e7c33e29SRobert Watson 			unp2 = unp->unp_conn;
790df8bae1dSRodney W. Grimes 		}
791b5ff0914SRobert Watson 		/*
792b5ff0914SRobert Watson 		 * Because connect() and send() are non-atomic in a sendto()
793b5ff0914SRobert Watson 		 * with a target address, it's possible that the socket will
794b5ff0914SRobert Watson 		 * have disconnected before the send() can run.  In that case
795b5ff0914SRobert Watson 		 * return the slightly counter-intuitive but otherwise
796b5ff0914SRobert Watson 		 * correct error that the socket is not connected.
797b5ff0914SRobert Watson 		 */
798b5ff0914SRobert Watson 		if (unp2 == NULL) {
799b5ff0914SRobert Watson 			error = ENOTCONN;
800b5ff0914SRobert Watson 			break;
801b5ff0914SRobert Watson 		}
802ede6e136SRobert Watson 		/* Lockless read. */
803ede6e136SRobert Watson 		if (unp2->unp_flags & UNP_WANTCRED)
804ede6e136SRobert Watson 			control = unp_addsockcred(td, control);
805ede6e136SRobert Watson 		UNP_PCB_LOCK(unp);
806fc3fcacfSRobert Watson 		if (unp->unp_addr != NULL)
80757bf258eSGarrett Wollman 			from = (struct sockaddr *)unp->unp_addr;
808df8bae1dSRodney W. Grimes 		else
809df8bae1dSRodney W. Grimes 			from = &sun_noname;
810ede6e136SRobert Watson 		so2 = unp2->unp_socket;
811a34b7046SRobert Watson 		SOCKBUF_LOCK(&so2->so_rcv);
812a34b7046SRobert Watson 		if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) {
8131e4d7da7SRobert Watson 			sorwakeup_locked(so2);
814fc3fcacfSRobert Watson 			m = NULL;
815fc3fcacfSRobert Watson 			control = NULL;
816e5aeaa0cSDag-Erling Smørgrav 		} else {
817a34b7046SRobert Watson 			SOCKBUF_UNLOCK(&so2->so_rcv);
818df8bae1dSRodney W. Grimes 			error = ENOBUFS;
819e5aeaa0cSDag-Erling Smørgrav 		}
820ede6e136SRobert Watson 		if (nam != NULL) {
821ede6e136SRobert Watson 			UNP_GLOBAL_WLOCK_ASSERT();
822ede6e136SRobert Watson 			UNP_PCB_LOCK(unp2);
823e7c33e29SRobert Watson 			unp_disconnect(unp, unp2);
824e7c33e29SRobert Watson 			UNP_PCB_UNLOCK(unp2);
825ede6e136SRobert Watson 		}
826ede6e136SRobert Watson 		UNP_PCB_UNLOCK(unp);
827df8bae1dSRodney W. Grimes 		break;
828df8bae1dSRodney W. Grimes 	}
829df8bae1dSRodney W. Grimes 
830df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
8316b8fda4dSGarrett Wollman 		/*
8321c381b19SRobert Watson 		 * Connect if not connected yet.
8331c381b19SRobert Watson 		 *
8341c381b19SRobert Watson 		 * Note: A better implementation would complain if not equal
8351c381b19SRobert Watson 		 * to the peer's address.
8366b8fda4dSGarrett Wollman 		 */
837402cc72dSDavid Greenman 		if ((so->so_state & SS_ISCONNECTED) == 0) {
838fc3fcacfSRobert Watson 			if (nam != NULL) {
839ede6e136SRobert Watson 				UNP_GLOBAL_WLOCK_ASSERT();
840b40ce416SJulian Elischer 				error = unp_connect(so, nam, td);
841402cc72dSDavid Greenman 				if (error)
8426b8fda4dSGarrett Wollman 					break;	/* XXX */
843402cc72dSDavid Greenman 			} else {
844402cc72dSDavid Greenman 				error = ENOTCONN;
845402cc72dSDavid Greenman 				break;
846402cc72dSDavid Greenman 			}
847ede6e136SRobert Watson 		}
848402cc72dSDavid Greenman 
849337cc6b6SRobert Watson 		/* Lockless read. */
850c0b99ffaSRobert Watson 		if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
851df8bae1dSRodney W. Grimes 			error = EPIPE;
852df8bae1dSRodney W. Grimes 			break;
853df8bae1dSRodney W. Grimes 		}
854b5ff0914SRobert Watson 		/*
855b5ff0914SRobert Watson 		 * Because connect() and send() are non-atomic in a sendto()
856b5ff0914SRobert Watson 		 * with a target address, it's possible that the socket will
857b5ff0914SRobert Watson 		 * have disconnected before the send() can run.  In that case
858b5ff0914SRobert Watson 		 * return the slightly counter-intuitive but otherwise
859b5ff0914SRobert Watson 		 * correct error that the socket is not connected.
860e7c33e29SRobert Watson 		 *
861d7924b70SRobert Watson 		 * Locking here must be done carefully: the global lock
862d7924b70SRobert Watson 		 * prevents interconnections between unpcbs from changing, so
863d7924b70SRobert Watson 		 * we can traverse from unp to unp2 without acquiring unp's
864d7924b70SRobert Watson 		 * lock.  Socket buffer locks follow unpcb locks, so we can
865d7924b70SRobert Watson 		 * acquire both remote and lock socket buffer locks.
866b5ff0914SRobert Watson 		 */
867f3f49bbbSRobert Watson 		unp2 = unp->unp_conn;
868b5ff0914SRobert Watson 		if (unp2 == NULL) {
869b5ff0914SRobert Watson 			error = ENOTCONN;
870b5ff0914SRobert Watson 			break;
871b5ff0914SRobert Watson 		}
872f3f49bbbSRobert Watson 		so2 = unp2->unp_socket;
873ede6e136SRobert Watson 		UNP_PCB_LOCK(unp2);
874a34b7046SRobert Watson 		SOCKBUF_LOCK(&so2->so_rcv);
875f3f49bbbSRobert Watson 		if (unp2->unp_flags & UNP_WANTCRED) {
8766a2989fdSMatthew N. Dodd 			/*
877ede6e136SRobert Watson 			 * Credentials are passed only once on SOCK_STREAM.
8786a2989fdSMatthew N. Dodd 			 */
879f3f49bbbSRobert Watson 			unp2->unp_flags &= ~UNP_WANTCRED;
8806a2989fdSMatthew N. Dodd 			control = unp_addsockcred(td, control);
8816a2989fdSMatthew N. Dodd 		}
882df8bae1dSRodney W. Grimes 		/*
8831c381b19SRobert Watson 		 * Send to paired receive port, and then reduce send buffer
8841c381b19SRobert Watson 		 * hiwater marks to maintain backpressure.  Wake up readers.
885df8bae1dSRodney W. Grimes 		 */
886fc3fcacfSRobert Watson 		if (control != NULL) {
887a34b7046SRobert Watson 			if (sbappendcontrol_locked(&so2->so_rcv, m, control))
888fc3fcacfSRobert Watson 				control = NULL;
889e7c33e29SRobert Watson 		} else
890a34b7046SRobert Watson 			sbappend_locked(&so2->so_rcv, m);
891f3f49bbbSRobert Watson 		mbcnt = so2->so_rcv.sb_mbcnt - unp2->unp_mbcnt;
892f3f49bbbSRobert Watson 		unp2->unp_mbcnt = so2->so_rcv.sb_mbcnt;
893337cc6b6SRobert Watson 		sbcc = so2->so_rcv.sb_cc;
894337cc6b6SRobert Watson 		sorwakeup_locked(so2);
895337cc6b6SRobert Watson 
896337cc6b6SRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
897f3f49bbbSRobert Watson 		newhiwat = so->so_snd.sb_hiwat - (sbcc - unp2->unp_cc);
898f535380cSDon Lewis 		(void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat,
8996aef685fSBrian Feldman 		    newhiwat, RLIM_INFINITY);
900337cc6b6SRobert Watson 		so->so_snd.sb_mbmax -= mbcnt;
9017abe2ac2SAlan Cox 		SOCKBUF_UNLOCK(&so->so_snd);
902f3f49bbbSRobert Watson 		unp2->unp_cc = sbcc;
903e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp2);
904fc3fcacfSRobert Watson 		m = NULL;
905df8bae1dSRodney W. Grimes 		break;
906df8bae1dSRodney W. Grimes 
907df8bae1dSRodney W. Grimes 	default:
908a29f300eSGarrett Wollman 		panic("uipc_send unknown socktype");
909df8bae1dSRodney W. Grimes 	}
910a29f300eSGarrett Wollman 
9116b8fda4dSGarrett Wollman 	/*
912ede6e136SRobert Watson 	 * SEND_EOF is equivalent to a SEND followed by a SHUTDOWN.
9136b8fda4dSGarrett Wollman 	 */
914a29f300eSGarrett Wollman 	if (flags & PRUS_EOF) {
915ede6e136SRobert Watson 		UNP_PCB_LOCK(unp);
9166b8fda4dSGarrett Wollman 		socantsendmore(so);
9176b8fda4dSGarrett Wollman 		unp_shutdown(unp);
918e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
919ede6e136SRobert Watson 	}
920e7c33e29SRobert Watson 
921e7c33e29SRobert Watson 	if ((nam != NULL) || (flags & PRUS_EOF))
922e7c33e29SRobert Watson 		UNP_GLOBAL_WUNLOCK();
923e7c33e29SRobert Watson 	else
924e7c33e29SRobert Watson 		UNP_GLOBAL_RUNLOCK();
925df8bae1dSRodney W. Grimes 
926fc3fcacfSRobert Watson 	if (control != NULL && error != 0)
927bd508d39SDon Lewis 		unp_dispose(control);
928bd508d39SDon Lewis 
929a29f300eSGarrett Wollman release:
930fc3fcacfSRobert Watson 	if (control != NULL)
931a29f300eSGarrett Wollman 		m_freem(control);
932fc3fcacfSRobert Watson 	if (m != NULL)
933a29f300eSGarrett Wollman 		m_freem(m);
934e5aeaa0cSDag-Erling Smørgrav 	return (error);
935a29f300eSGarrett Wollman }
936df8bae1dSRodney W. Grimes 
937a29f300eSGarrett Wollman static int
938a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb)
939a29f300eSGarrett Wollman {
940e7c33e29SRobert Watson 	struct unpcb *unp, *unp2;
941a29f300eSGarrett Wollman 	struct socket *so2;
942a29f300eSGarrett Wollman 
94340f2ac28SRobert Watson 	unp = sotounpcb(so);
9444d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_sense: unp == NULL"));
945e7c33e29SRobert Watson 
946a29f300eSGarrett Wollman 	sb->st_blksize = so->so_snd.sb_hiwat;
947e7c33e29SRobert Watson 	UNP_GLOBAL_RLOCK();
948e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
949e7c33e29SRobert Watson 	unp2 = unp->unp_conn;
950e7c33e29SRobert Watson 	if (so->so_type == SOCK_STREAM && unp2 != NULL) {
951e7c33e29SRobert Watson 		so2 = unp2->unp_socket;
952a29f300eSGarrett Wollman 		sb->st_blksize += so2->so_rcv.sb_cc;
953df8bae1dSRodney W. Grimes 	}
954f3732fd1SPoul-Henning Kamp 	sb->st_dev = NODEV;
955df8bae1dSRodney W. Grimes 	if (unp->unp_ino == 0)
9566f782c46SJeffrey Hsu 		unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino;
957a29f300eSGarrett Wollman 	sb->st_ino = unp->unp_ino;
958e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
959e7c33e29SRobert Watson 	UNP_GLOBAL_RUNLOCK();
960df8bae1dSRodney W. Grimes 	return (0);
961a29f300eSGarrett Wollman }
962df8bae1dSRodney W. Grimes 
963a29f300eSGarrett Wollman static int
964a29f300eSGarrett Wollman uipc_shutdown(struct socket *so)
965a29f300eSGarrett Wollman {
96640f2ac28SRobert Watson 	struct unpcb *unp;
967df8bae1dSRodney W. Grimes 
96840f2ac28SRobert Watson 	unp = sotounpcb(so);
9694d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_shutdown: unp == NULL"));
970e7c33e29SRobert Watson 
971e7c33e29SRobert Watson 	UNP_GLOBAL_WLOCK();
972e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
973a29f300eSGarrett Wollman 	socantsendmore(so);
974a29f300eSGarrett Wollman 	unp_shutdown(unp);
975e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
976e7c33e29SRobert Watson 	UNP_GLOBAL_WUNLOCK();
977e5aeaa0cSDag-Erling Smørgrav 	return (0);
978a29f300eSGarrett Wollman }
979df8bae1dSRodney W. Grimes 
980a29f300eSGarrett Wollman static int
98157bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam)
982a29f300eSGarrett Wollman {
98340f2ac28SRobert Watson 	struct unpcb *unp;
9840d9ce3a1SRobert Watson 	const struct sockaddr *sa;
985a29f300eSGarrett Wollman 
9864d4b555eSRobert Watson 	unp = sotounpcb(so);
9874d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL"));
988e7c33e29SRobert Watson 
9890d9ce3a1SRobert Watson 	*nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
990e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
991fc3fcacfSRobert Watson 	if (unp->unp_addr != NULL)
9920d9ce3a1SRobert Watson 		sa = (struct sockaddr *) unp->unp_addr;
99383f3198bSThomas Moestl 	else
9940d9ce3a1SRobert Watson 		sa = &sun_noname;
9950d9ce3a1SRobert Watson 	bcopy(sa, *nam, sa->sa_len);
996e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
997e5aeaa0cSDag-Erling Smørgrav 	return (0);
998df8bae1dSRodney W. Grimes }
999a29f300eSGarrett Wollman 
1000a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = {
1001756d52a1SPoul-Henning Kamp 	.pru_abort = 		uipc_abort,
1002756d52a1SPoul-Henning Kamp 	.pru_accept =		uipc_accept,
1003756d52a1SPoul-Henning Kamp 	.pru_attach =		uipc_attach,
1004756d52a1SPoul-Henning Kamp 	.pru_bind =		uipc_bind,
1005756d52a1SPoul-Henning Kamp 	.pru_connect =		uipc_connect,
1006756d52a1SPoul-Henning Kamp 	.pru_connect2 =		uipc_connect2,
1007756d52a1SPoul-Henning Kamp 	.pru_detach =		uipc_detach,
1008756d52a1SPoul-Henning Kamp 	.pru_disconnect =	uipc_disconnect,
1009756d52a1SPoul-Henning Kamp 	.pru_listen =		uipc_listen,
1010756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		uipc_peeraddr,
1011756d52a1SPoul-Henning Kamp 	.pru_rcvd =		uipc_rcvd,
1012756d52a1SPoul-Henning Kamp 	.pru_send =		uipc_send,
1013756d52a1SPoul-Henning Kamp 	.pru_sense =		uipc_sense,
1014756d52a1SPoul-Henning Kamp 	.pru_shutdown =		uipc_shutdown,
1015756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		uipc_sockaddr,
1016a152f8a3SRobert Watson 	.pru_close =		uipc_close,
1017a29f300eSGarrett Wollman };
1018df8bae1dSRodney W. Grimes 
10190c1bb4fbSDima Dorfman int
1020892af6b9SRobert Watson uipc_ctloutput(struct socket *so, struct sockopt *sopt)
10210c1bb4fbSDima Dorfman {
102240f2ac28SRobert Watson 	struct unpcb *unp;
10230d9ce3a1SRobert Watson 	struct xucred xu;
10246a2989fdSMatthew N. Dodd 	int error, optval;
10256a2989fdSMatthew N. Dodd 
102696a041b5SMatthew N. Dodd 	if (sopt->sopt_level != 0)
102796a041b5SMatthew N. Dodd 		return (EINVAL);
102896a041b5SMatthew N. Dodd 
10296a2989fdSMatthew N. Dodd 	unp = sotounpcb(so);
10304d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL"));
10316a2989fdSMatthew N. Dodd 	error = 0;
10320c1bb4fbSDima Dorfman 	switch (sopt->sopt_dir) {
10330c1bb4fbSDima Dorfman 	case SOPT_GET:
10340c1bb4fbSDima Dorfman 		switch (sopt->sopt_name) {
10350c1bb4fbSDima Dorfman 		case LOCAL_PEERCRED:
1036e7c33e29SRobert Watson 			UNP_PCB_LOCK(unp);
10370c1bb4fbSDima Dorfman 			if (unp->unp_flags & UNP_HAVEPC)
10380d9ce3a1SRobert Watson 				xu = unp->unp_peercred;
10390c1bb4fbSDima Dorfman 			else {
10400c1bb4fbSDima Dorfman 				if (so->so_type == SOCK_STREAM)
10410c1bb4fbSDima Dorfman 					error = ENOTCONN;
10420c1bb4fbSDima Dorfman 				else
10430c1bb4fbSDima Dorfman 					error = EINVAL;
10440c1bb4fbSDima Dorfman 			}
1045e7c33e29SRobert Watson 			UNP_PCB_UNLOCK(unp);
10460d9ce3a1SRobert Watson 			if (error == 0)
10470d9ce3a1SRobert Watson 				error = sooptcopyout(sopt, &xu, sizeof(xu));
10480c1bb4fbSDima Dorfman 			break;
1049e7c33e29SRobert Watson 
10506a2989fdSMatthew N. Dodd 		case LOCAL_CREDS:
10511f837c47SRobert Watson 			/* Unocked read. */
10526a2989fdSMatthew N. Dodd 			optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0;
10536a2989fdSMatthew N. Dodd 			error = sooptcopyout(sopt, &optval, sizeof(optval));
10546a2989fdSMatthew N. Dodd 			break;
1055e7c33e29SRobert Watson 
10566a2989fdSMatthew N. Dodd 		case LOCAL_CONNWAIT:
10571f837c47SRobert Watson 			/* Unocked read. */
10586a2989fdSMatthew N. Dodd 			optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0;
10596a2989fdSMatthew N. Dodd 			error = sooptcopyout(sopt, &optval, sizeof(optval));
10606a2989fdSMatthew N. Dodd 			break;
1061e7c33e29SRobert Watson 
10620c1bb4fbSDima Dorfman 		default:
10630c1bb4fbSDima Dorfman 			error = EOPNOTSUPP;
10640c1bb4fbSDima Dorfman 			break;
10650c1bb4fbSDima Dorfman 		}
10660c1bb4fbSDima Dorfman 		break;
1067e7c33e29SRobert Watson 
10680c1bb4fbSDima Dorfman 	case SOPT_SET:
10696a2989fdSMatthew N. Dodd 		switch (sopt->sopt_name) {
10706a2989fdSMatthew N. Dodd 		case LOCAL_CREDS:
10716a2989fdSMatthew N. Dodd 		case LOCAL_CONNWAIT:
10726a2989fdSMatthew N. Dodd 			error = sooptcopyin(sopt, &optval, sizeof(optval),
10736a2989fdSMatthew N. Dodd 					    sizeof(optval));
10746a2989fdSMatthew N. Dodd 			if (error)
10756a2989fdSMatthew N. Dodd 				break;
10766a2989fdSMatthew N. Dodd 
1077e7c33e29SRobert Watson #define	OPTSET(bit) do {						\
1078e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);						\
10796a2989fdSMatthew N. Dodd 	if (optval)							\
10806a2989fdSMatthew N. Dodd 		unp->unp_flags |= bit;					\
10816a2989fdSMatthew N. Dodd 	else								\
1082e7c33e29SRobert Watson 		unp->unp_flags &= ~bit;					\
1083e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);						\
1084e7c33e29SRobert Watson } while (0)
10856a2989fdSMatthew N. Dodd 
10866a2989fdSMatthew N. Dodd 			switch (sopt->sopt_name) {
10876a2989fdSMatthew N. Dodd 			case LOCAL_CREDS:
10886a2989fdSMatthew N. Dodd 				OPTSET(UNP_WANTCRED);
10896a2989fdSMatthew N. Dodd 				break;
1090e7c33e29SRobert Watson 
10916a2989fdSMatthew N. Dodd 			case LOCAL_CONNWAIT:
10926a2989fdSMatthew N. Dodd 				OPTSET(UNP_CONNWAIT);
10936a2989fdSMatthew N. Dodd 				break;
1094e7c33e29SRobert Watson 
10956a2989fdSMatthew N. Dodd 			default:
10966a2989fdSMatthew N. Dodd 				break;
10976a2989fdSMatthew N. Dodd 			}
10986a2989fdSMatthew N. Dodd 			break;
10996a2989fdSMatthew N. Dodd #undef	OPTSET
11006a2989fdSMatthew N. Dodd 		default:
11016a2989fdSMatthew N. Dodd 			error = ENOPROTOOPT;
11026a2989fdSMatthew N. Dodd 			break;
11036a2989fdSMatthew N. Dodd 		}
1104abb886faSMatthew N. Dodd 		break;
1105e7c33e29SRobert Watson 
11060c1bb4fbSDima Dorfman 	default:
11070c1bb4fbSDima Dorfman 		error = EOPNOTSUPP;
11080c1bb4fbSDima Dorfman 		break;
11090c1bb4fbSDima Dorfman 	}
11100c1bb4fbSDima Dorfman 	return (error);
11110c1bb4fbSDima Dorfman }
11120c1bb4fbSDima Dorfman 
1113f708ef1bSPoul-Henning Kamp static int
1114892af6b9SRobert Watson unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1115df8bae1dSRodney W. Grimes {
1116892af6b9SRobert Watson 	struct sockaddr_un *soun = (struct sockaddr_un *)nam;
1117892af6b9SRobert Watson 	struct vnode *vp;
1118892af6b9SRobert Watson 	struct socket *so2, *so3;
1119b295bdcdSRobert Watson 	struct unpcb *unp, *unp2, *unp3;
11209e289446SWojciech A. Koszek 	int error, len, vfslocked;
1121df8bae1dSRodney W. Grimes 	struct nameidata nd;
112257bf258eSGarrett Wollman 	char buf[SOCK_MAXADDRLEN];
11230d9ce3a1SRobert Watson 	struct sockaddr *sa;
11240d9ce3a1SRobert Watson 
1125e7c33e29SRobert Watson 	UNP_GLOBAL_WLOCK_ASSERT();
1126e7c33e29SRobert Watson 	UNP_GLOBAL_WUNLOCK();
1127df8bae1dSRodney W. Grimes 
11284d4b555eSRobert Watson 	unp = sotounpcb(so);
11294d4b555eSRobert Watson 	KASSERT(unp != NULL, ("unp_connect: unp == NULL"));
1130e7c33e29SRobert Watson 
113157bf258eSGarrett Wollman 	len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
113257bf258eSGarrett Wollman 	if (len <= 0)
1133e5aeaa0cSDag-Erling Smørgrav 		return (EINVAL);
113455c85568SRobert Drehmel 	strlcpy(buf, soun->sun_path, len + 1);
1135e7c33e29SRobert Watson 
1136e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
11374f1f0ef5SRobert Watson 	if (unp->unp_flags & UNP_CONNECTING) {
1138e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
11394f1f0ef5SRobert Watson 		return (EALREADY);
11404f1f0ef5SRobert Watson 	}
114105102f04SRobert Watson 	unp->unp_flags |= UNP_CONNECTING;
1142e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
1143e7c33e29SRobert Watson 
11440d9ce3a1SRobert Watson 	sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
11459e289446SWojciech A. Koszek 	NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf,
11469e289446SWojciech A. Koszek 	    td);
1147797f2d22SPoul-Henning Kamp 	error = namei(&nd);
1148797f2d22SPoul-Henning Kamp 	if (error)
11490d9ce3a1SRobert Watson 		vp = NULL;
11500d9ce3a1SRobert Watson 	else
1151df8bae1dSRodney W. Grimes 		vp = nd.ni_vp;
11520d9ce3a1SRobert Watson 	ASSERT_VOP_LOCKED(vp, "unp_connect");
11539e289446SWojciech A. Koszek 	vfslocked = NDHASGIANT(&nd);
1154762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
11550d9ce3a1SRobert Watson 	if (error)
11560d9ce3a1SRobert Watson 		goto bad;
11570d9ce3a1SRobert Watson 
1158df8bae1dSRodney W. Grimes 	if (vp->v_type != VSOCK) {
1159df8bae1dSRodney W. Grimes 		error = ENOTSOCK;
1160df8bae1dSRodney W. Grimes 		goto bad;
1161df8bae1dSRodney W. Grimes 	}
11626fac927cSRobert Watson #ifdef MAC
116330d239bcSRobert Watson 	error = mac_vnode_check_open(td->td_ucred, vp, VWRITE | VREAD);
11646fac927cSRobert Watson 	if (error)
11656fac927cSRobert Watson 		goto bad;
11666fac927cSRobert Watson #endif
1167a854ed98SJohn Baldwin 	error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td);
1168797f2d22SPoul-Henning Kamp 	if (error)
1169df8bae1dSRodney W. Grimes 		goto bad;
11709e289446SWojciech A. Koszek 	VFS_UNLOCK_GIANT(vfslocked);
1171e7c33e29SRobert Watson 
1172b295bdcdSRobert Watson 	unp = sotounpcb(so);
11734d4b555eSRobert Watson 	KASSERT(unp != NULL, ("unp_connect: unp == NULL"));
1174e7c33e29SRobert Watson 
1175e7c33e29SRobert Watson 	/*
1176e7c33e29SRobert Watson 	 * Lock global lock for two reasons: make sure v_socket is stable,
1177e7c33e29SRobert Watson 	 * and to protect simultaneous locking of multiple pcbs.
1178e7c33e29SRobert Watson 	 */
1179e7c33e29SRobert Watson 	UNP_GLOBAL_WLOCK();
1180df8bae1dSRodney W. Grimes 	so2 = vp->v_socket;
1181fc3fcacfSRobert Watson 	if (so2 == NULL) {
1182df8bae1dSRodney W. Grimes 		error = ECONNREFUSED;
11832260c03dSRobert Watson 		goto bad2;
1184df8bae1dSRodney W. Grimes 	}
1185df8bae1dSRodney W. Grimes 	if (so->so_type != so2->so_type) {
1186df8bae1dSRodney W. Grimes 		error = EPROTOTYPE;
11872260c03dSRobert Watson 		goto bad2;
1188df8bae1dSRodney W. Grimes 	}
1189df8bae1dSRodney W. Grimes 	if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
1190e7c33e29SRobert Watson 		if (so2->so_options & SO_ACCEPTCONN) {
1191e7c33e29SRobert Watson 			/*
1192e7c33e29SRobert Watson 			 * We can't drop the global lock here or 'so2' may
1193d7924b70SRobert Watson 			 * become invalid.  As a result, we need to handle
1194d7924b70SRobert Watson 			 * possibly lock recursion in uipc_attach.
1195e7c33e29SRobert Watson 			 */
11960d9ce3a1SRobert Watson 			so3 = sonewconn(so2, 0);
1197e7c33e29SRobert Watson 		} else
11980d9ce3a1SRobert Watson 			so3 = NULL;
11990d9ce3a1SRobert Watson 		if (so3 == NULL) {
1200df8bae1dSRodney W. Grimes 			error = ECONNREFUSED;
12010d9ce3a1SRobert Watson 			goto bad2;
1202df8bae1dSRodney W. Grimes 		}
12030c1bb4fbSDima Dorfman 		unp = sotounpcb(so);
1204df8bae1dSRodney W. Grimes 		unp2 = sotounpcb(so2);
1205df8bae1dSRodney W. Grimes 		unp3 = sotounpcb(so3);
1206e7c33e29SRobert Watson 		UNP_PCB_LOCK(unp);
1207e7c33e29SRobert Watson 		UNP_PCB_LOCK(unp2);
1208e7c33e29SRobert Watson 		UNP_PCB_LOCK(unp3);
12090d9ce3a1SRobert Watson 		if (unp2->unp_addr != NULL) {
12100d9ce3a1SRobert Watson 			bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len);
12110d9ce3a1SRobert Watson 			unp3->unp_addr = (struct sockaddr_un *) sa;
12120d9ce3a1SRobert Watson 			sa = NULL;
12130d9ce3a1SRobert Watson 		}
12140c1bb4fbSDima Dorfman 		/*
12150c1bb4fbSDima Dorfman 		 * unp_peercred management:
12160c1bb4fbSDima Dorfman 		 *
12171c381b19SRobert Watson 		 * The connecter's (client's) credentials are copied from its
12181c381b19SRobert Watson 		 * process structure at the time of connect() (which is now).
12190c1bb4fbSDima Dorfman 		 */
1220a854ed98SJohn Baldwin 		cru2x(td->td_ucred, &unp3->unp_peercred);
12210c1bb4fbSDima Dorfman 		unp3->unp_flags |= UNP_HAVEPC;
12220c1bb4fbSDima Dorfman 		/*
12231c381b19SRobert Watson 		 * The receiver's (server's) credentials are copied from the
12241c381b19SRobert Watson 		 * unp_peercred member of socket on which the former called
1225e7c33e29SRobert Watson 		 * listen(); uipc_listen() cached that process's credentials
12261c381b19SRobert Watson 		 * at that time so we can use them now.
12270c1bb4fbSDima Dorfman 		 */
12280c1bb4fbSDima Dorfman 		KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED,
12290c1bb4fbSDima Dorfman 		    ("unp_connect: listener without cached peercred"));
12300c1bb4fbSDima Dorfman 		memcpy(&unp->unp_peercred, &unp2->unp_peercred,
12310c1bb4fbSDima Dorfman 		    sizeof(unp->unp_peercred));
12320c1bb4fbSDima Dorfman 		unp->unp_flags |= UNP_HAVEPC;
1233481f8fe8SMaxim Konovalov 		if (unp2->unp_flags & UNP_WANTCRED)
1234481f8fe8SMaxim Konovalov 			unp3->unp_flags |= UNP_WANTCRED;
1235e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp3);
1236e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp2);
1237e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
1238335654d7SRobert Watson #ifdef MAC
1239310e7cebSRobert Watson 		SOCK_LOCK(so);
124030d239bcSRobert Watson 		mac_socketpeer_set_from_socket(so, so3);
124130d239bcSRobert Watson 		mac_socketpeer_set_from_socket(so3, so);
1242310e7cebSRobert Watson 		SOCK_UNLOCK(so);
1243335654d7SRobert Watson #endif
12440c1bb4fbSDima Dorfman 
1245df8bae1dSRodney W. Grimes 		so2 = so3;
1246df8bae1dSRodney W. Grimes 	}
1247e7c33e29SRobert Watson 	unp = sotounpcb(so);
1248e7c33e29SRobert Watson 	KASSERT(unp != NULL, ("unp_connect: unp == NULL"));
1249e7c33e29SRobert Watson 	unp2 = sotounpcb(so2);
1250e7c33e29SRobert Watson 	KASSERT(unp2 != NULL, ("unp_connect: unp2 == NULL"));
1251e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
1252e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp2);
12536a2989fdSMatthew N. Dodd 	error = unp_connect2(so, so2, PRU_CONNECT);
1254e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp2);
1255e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
12560d9ce3a1SRobert Watson bad2:
1257e7c33e29SRobert Watson 	UNP_GLOBAL_WUNLOCK();
12589e289446SWojciech A. Koszek 	if (vfslocked)
12599e289446SWojciech A. Koszek 		/*
12609e289446SWojciech A. Koszek 		 * Giant has been previously acquired. This means filesystem
12619e289446SWojciech A. Koszek 		 * isn't MPSAFE. Do it once again.
12629e289446SWojciech A. Koszek 		 */
12630d9ce3a1SRobert Watson 		mtx_lock(&Giant);
1264df8bae1dSRodney W. Grimes bad:
12650d9ce3a1SRobert Watson 	if (vp != NULL)
1266df8bae1dSRodney W. Grimes 		vput(vp);
12679e289446SWojciech A. Koszek 	VFS_UNLOCK_GIANT(vfslocked);
12680d9ce3a1SRobert Watson 	free(sa, M_SONAME);
1269e7c33e29SRobert Watson 	UNP_GLOBAL_WLOCK();
1270e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
12714f1f0ef5SRobert Watson 	unp->unp_flags &= ~UNP_CONNECTING;
1272e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
1273df8bae1dSRodney W. Grimes 	return (error);
1274df8bae1dSRodney W. Grimes }
1275df8bae1dSRodney W. Grimes 
1276db48c0d2SRobert Watson static int
12776a2989fdSMatthew N. Dodd unp_connect2(struct socket *so, struct socket *so2, int req)
1278df8bae1dSRodney W. Grimes {
1279e7c33e29SRobert Watson 	struct unpcb *unp;
1280892af6b9SRobert Watson 	struct unpcb *unp2;
1281df8bae1dSRodney W. Grimes 
1282e7c33e29SRobert Watson 	unp = sotounpcb(so);
1283e7c33e29SRobert Watson 	KASSERT(unp != NULL, ("unp_connect2: unp == NULL"));
1284e7c33e29SRobert Watson 	unp2 = sotounpcb(so2);
1285e7c33e29SRobert Watson 	KASSERT(unp2 != NULL, ("unp_connect2: unp2 == NULL"));
1286e7c33e29SRobert Watson 
1287e7c33e29SRobert Watson 	UNP_GLOBAL_WLOCK_ASSERT();
1288e7c33e29SRobert Watson 	UNP_PCB_LOCK_ASSERT(unp);
1289e7c33e29SRobert Watson 	UNP_PCB_LOCK_ASSERT(unp2);
12900d9ce3a1SRobert Watson 
1291df8bae1dSRodney W. Grimes 	if (so2->so_type != so->so_type)
1292df8bae1dSRodney W. Grimes 		return (EPROTOTYPE);
1293df8bae1dSRodney W. Grimes 	unp->unp_conn = unp2;
1294e7c33e29SRobert Watson 
1295df8bae1dSRodney W. Grimes 	switch (so->so_type) {
1296df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
129798271db4SGarrett Wollman 		LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
1298df8bae1dSRodney W. Grimes 		soisconnected(so);
1299df8bae1dSRodney W. Grimes 		break;
1300df8bae1dSRodney W. Grimes 
1301df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
1302df8bae1dSRodney W. Grimes 		unp2->unp_conn = unp;
13036a2989fdSMatthew N. Dodd 		if (req == PRU_CONNECT &&
13046a2989fdSMatthew N. Dodd 		    ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT))
13056a2989fdSMatthew N. Dodd 			soisconnecting(so);
13066a2989fdSMatthew N. Dodd 		else
1307df8bae1dSRodney W. Grimes 			soisconnected(so);
1308df8bae1dSRodney W. Grimes 		soisconnected(so2);
1309df8bae1dSRodney W. Grimes 		break;
1310df8bae1dSRodney W. Grimes 
1311df8bae1dSRodney W. Grimes 	default:
1312df8bae1dSRodney W. Grimes 		panic("unp_connect2");
1313df8bae1dSRodney W. Grimes 	}
1314df8bae1dSRodney W. Grimes 	return (0);
1315df8bae1dSRodney W. Grimes }
1316df8bae1dSRodney W. Grimes 
1317f708ef1bSPoul-Henning Kamp static void
1318e7c33e29SRobert Watson unp_disconnect(struct unpcb *unp, struct unpcb *unp2)
1319df8bae1dSRodney W. Grimes {
13201b2e3b4bSRobert Watson 	struct socket *so;
1321df8bae1dSRodney W. Grimes 
1322e7c33e29SRobert Watson 	KASSERT(unp2 != NULL, ("unp_disconnect: unp2 == NULL"));
13230d9ce3a1SRobert Watson 
1324e7c33e29SRobert Watson 	UNP_GLOBAL_WLOCK_ASSERT();
1325e7c33e29SRobert Watson 	UNP_PCB_LOCK_ASSERT(unp);
1326e7c33e29SRobert Watson 	UNP_PCB_LOCK_ASSERT(unp2);
1327e7c33e29SRobert Watson 
1328fc3fcacfSRobert Watson 	unp->unp_conn = NULL;
1329df8bae1dSRodney W. Grimes 	switch (unp->unp_socket->so_type) {
1330df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
133198271db4SGarrett Wollman 		LIST_REMOVE(unp, unp_reflink);
13321b2e3b4bSRobert Watson 		so = unp->unp_socket;
13331b2e3b4bSRobert Watson 		SOCK_LOCK(so);
13341b2e3b4bSRobert Watson 		so->so_state &= ~SS_ISCONNECTED;
13351b2e3b4bSRobert Watson 		SOCK_UNLOCK(so);
1336df8bae1dSRodney W. Grimes 		break;
1337df8bae1dSRodney W. Grimes 
1338df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
1339df8bae1dSRodney W. Grimes 		soisdisconnected(unp->unp_socket);
1340fc3fcacfSRobert Watson 		unp2->unp_conn = NULL;
1341df8bae1dSRodney W. Grimes 		soisdisconnected(unp2->unp_socket);
1342df8bae1dSRodney W. Grimes 		break;
1343df8bae1dSRodney W. Grimes 	}
1344df8bae1dSRodney W. Grimes }
1345df8bae1dSRodney W. Grimes 
13460d9ce3a1SRobert Watson /*
1347d7924b70SRobert Watson  * unp_pcblist() walks the global list of struct unpcb's to generate a
1348d7924b70SRobert Watson  * pointer list, bumping the refcount on each unpcb.  It then copies them out
1349d7924b70SRobert Watson  * sequentially, validating the generation number on each to see if it has
1350d7924b70SRobert Watson  * been detached.  All of this is necessary because copyout() may sleep on
1351d7924b70SRobert Watson  * disk I/O.
13520d9ce3a1SRobert Watson  */
135398271db4SGarrett Wollman static int
135482d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS)
135598271db4SGarrett Wollman {
1356f5ef029eSPoul-Henning Kamp 	int error, i, n;
13579ae328fcSJohn Baldwin 	int freeunp;
135898271db4SGarrett Wollman 	struct unpcb *unp, **unp_list;
135998271db4SGarrett Wollman 	unp_gen_t gencnt;
13608f364875SJulian Elischer 	struct xunpgen *xug;
136198271db4SGarrett Wollman 	struct unp_head *head;
13628f364875SJulian Elischer 	struct xunpcb *xu;
136398271db4SGarrett Wollman 
1364a23d65bfSBruce Evans 	head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead);
136598271db4SGarrett Wollman 
136698271db4SGarrett Wollman 	/*
136798271db4SGarrett Wollman 	 * The process of preparing the PCB list is too time-consuming and
136898271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
136998271db4SGarrett Wollman 	 */
1370fc3fcacfSRobert Watson 	if (req->oldptr == NULL) {
137198271db4SGarrett Wollman 		n = unp_count;
13728f364875SJulian Elischer 		req->oldidx = 2 * (sizeof *xug)
137398271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xunpcb);
1374e5aeaa0cSDag-Erling Smørgrav 		return (0);
137598271db4SGarrett Wollman 	}
137698271db4SGarrett Wollman 
1377fc3fcacfSRobert Watson 	if (req->newptr != NULL)
1378e5aeaa0cSDag-Erling Smørgrav 		return (EPERM);
137998271db4SGarrett Wollman 
138098271db4SGarrett Wollman 	/*
138198271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
138298271db4SGarrett Wollman 	 */
1383a163d034SWarner Losh 	xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK);
1384e7c33e29SRobert Watson 	UNP_GLOBAL_RLOCK();
138598271db4SGarrett Wollman 	gencnt = unp_gencnt;
138698271db4SGarrett Wollman 	n = unp_count;
1387e7c33e29SRobert Watson 	UNP_GLOBAL_RUNLOCK();
138898271db4SGarrett Wollman 
13898f364875SJulian Elischer 	xug->xug_len = sizeof *xug;
13908f364875SJulian Elischer 	xug->xug_count = n;
13918f364875SJulian Elischer 	xug->xug_gen = gencnt;
13928f364875SJulian Elischer 	xug->xug_sogen = so_gencnt;
13938f364875SJulian Elischer 	error = SYSCTL_OUT(req, xug, sizeof *xug);
13948f364875SJulian Elischer 	if (error) {
13958f364875SJulian Elischer 		free(xug, M_TEMP);
1396e5aeaa0cSDag-Erling Smørgrav 		return (error);
13978f364875SJulian Elischer 	}
139898271db4SGarrett Wollman 
1399a163d034SWarner Losh 	unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
140098271db4SGarrett Wollman 
1401e7c33e29SRobert Watson 	UNP_GLOBAL_RLOCK();
14022e3c8fcbSPoul-Henning Kamp 	for (unp = LIST_FIRST(head), i = 0; unp && i < n;
14032e3c8fcbSPoul-Henning Kamp 	     unp = LIST_NEXT(unp, unp_link)) {
1404e7c33e29SRobert Watson 		UNP_PCB_LOCK(unp);
14058a7d8cc6SRobert Watson 		if (unp->unp_gencnt <= gencnt) {
1406a854ed98SJohn Baldwin 			if (cr_cansee(req->td->td_ucred,
1407e7c33e29SRobert Watson 			    unp->unp_socket->so_cred)) {
1408e7c33e29SRobert Watson 				UNP_PCB_UNLOCK(unp);
14094787fd37SPaul Saab 				continue;
1410e7c33e29SRobert Watson 			}
141198271db4SGarrett Wollman 			unp_list[i++] = unp;
14129ae328fcSJohn Baldwin 			unp->unp_refcount++;
141398271db4SGarrett Wollman 		}
1414e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
14154787fd37SPaul Saab 	}
1416e7c33e29SRobert Watson 	UNP_GLOBAL_RUNLOCK();
14171c381b19SRobert Watson 	n = i;			/* In case we lost some during malloc. */
141898271db4SGarrett Wollman 
141998271db4SGarrett Wollman 	error = 0;
1420fe2eee82SColin Percival 	xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO);
142198271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
142298271db4SGarrett Wollman 		unp = unp_list[i];
1423e7c33e29SRobert Watson 		UNP_PCB_LOCK(unp);
14249ae328fcSJohn Baldwin 		unp->unp_refcount--;
14259ae328fcSJohn Baldwin 	        if (unp->unp_refcount != 0 && unp->unp_gencnt <= gencnt) {
14268f364875SJulian Elischer 			xu->xu_len = sizeof *xu;
14278f364875SJulian Elischer 			xu->xu_unpp = unp;
142898271db4SGarrett Wollman 			/*
142998271db4SGarrett Wollman 			 * XXX - need more locking here to protect against
143098271db4SGarrett Wollman 			 * connect/disconnect races for SMP.
143198271db4SGarrett Wollman 			 */
1432fc3fcacfSRobert Watson 			if (unp->unp_addr != NULL)
14338f364875SJulian Elischer 				bcopy(unp->unp_addr, &xu->xu_addr,
143498271db4SGarrett Wollman 				      unp->unp_addr->sun_len);
1435fc3fcacfSRobert Watson 			if (unp->unp_conn != NULL &&
1436fc3fcacfSRobert Watson 			    unp->unp_conn->unp_addr != NULL)
143798271db4SGarrett Wollman 				bcopy(unp->unp_conn->unp_addr,
14388f364875SJulian Elischer 				      &xu->xu_caddr,
143998271db4SGarrett Wollman 				      unp->unp_conn->unp_addr->sun_len);
14408f364875SJulian Elischer 			bcopy(unp, &xu->xu_unp, sizeof *unp);
14418f364875SJulian Elischer 			sotoxsocket(unp->unp_socket, &xu->xu_socket);
1442e7c33e29SRobert Watson 			UNP_PCB_UNLOCK(unp);
14438f364875SJulian Elischer 			error = SYSCTL_OUT(req, xu, sizeof *xu);
14449ae328fcSJohn Baldwin 		} else {
14459ae328fcSJohn Baldwin 			freeunp = (unp->unp_refcount == 0);
1446e7c33e29SRobert Watson 			UNP_PCB_UNLOCK(unp);
1447e7c33e29SRobert Watson 			if (freeunp) {
1448e7c33e29SRobert Watson 				UNP_PCB_LOCK_DESTROY(unp);
14499ae328fcSJohn Baldwin 				uma_zfree(unp_zone, unp);
145098271db4SGarrett Wollman 			}
145198271db4SGarrett Wollman 		}
1452e7c33e29SRobert Watson 	}
14538f364875SJulian Elischer 	free(xu, M_TEMP);
145498271db4SGarrett Wollman 	if (!error) {
145598271db4SGarrett Wollman 		/*
14561c381b19SRobert Watson 		 * Give the user an updated idea of our state.  If the
14571c381b19SRobert Watson 		 * generation differs from what we told her before, she knows
14581c381b19SRobert Watson 		 * that something happened while we were processing this
14591c381b19SRobert Watson 		 * request, and it might be necessary to retry.
146098271db4SGarrett Wollman 		 */
14618f364875SJulian Elischer 		xug->xug_gen = unp_gencnt;
14628f364875SJulian Elischer 		xug->xug_sogen = so_gencnt;
14638f364875SJulian Elischer 		xug->xug_count = unp_count;
14648f364875SJulian Elischer 		error = SYSCTL_OUT(req, xug, sizeof *xug);
146598271db4SGarrett Wollman 	}
146698271db4SGarrett Wollman 	free(unp_list, M_TEMP);
14678f364875SJulian Elischer 	free(xug, M_TEMP);
1468e5aeaa0cSDag-Erling Smørgrav 	return (error);
146998271db4SGarrett Wollman }
147098271db4SGarrett Wollman 
147198271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD,
147298271db4SGarrett Wollman 	    (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
147398271db4SGarrett Wollman 	    "List of active local datagram sockets");
147498271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD,
147598271db4SGarrett Wollman 	    (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
147698271db4SGarrett Wollman 	    "List of active local stream sockets");
147798271db4SGarrett Wollman 
1478f708ef1bSPoul-Henning Kamp static void
1479892af6b9SRobert Watson unp_shutdown(struct unpcb *unp)
1480df8bae1dSRodney W. Grimes {
1481e7c33e29SRobert Watson 	struct unpcb *unp2;
1482df8bae1dSRodney W. Grimes 	struct socket *so;
1483df8bae1dSRodney W. Grimes 
1484e7c33e29SRobert Watson 	UNP_GLOBAL_WLOCK_ASSERT();
1485e7c33e29SRobert Watson 	UNP_PCB_LOCK_ASSERT(unp);
14860d9ce3a1SRobert Watson 
1487e7c33e29SRobert Watson 	unp2 = unp->unp_conn;
1488e7c33e29SRobert Watson 	if (unp->unp_socket->so_type == SOCK_STREAM && unp2 != NULL) {
1489e7c33e29SRobert Watson 		so = unp2->unp_socket;
1490e7c33e29SRobert Watson 		if (so != NULL)
1491df8bae1dSRodney W. Grimes 			socantrcvmore(so);
1492df8bae1dSRodney W. Grimes 	}
1493e7c33e29SRobert Watson }
1494df8bae1dSRodney W. Grimes 
1495f708ef1bSPoul-Henning Kamp static void
1496892af6b9SRobert Watson unp_drop(struct unpcb *unp, int errno)
1497df8bae1dSRodney W. Grimes {
1498df8bae1dSRodney W. Grimes 	struct socket *so = unp->unp_socket;
1499e7c33e29SRobert Watson 	struct unpcb *unp2;
1500df8bae1dSRodney W. Grimes 
1501e7c33e29SRobert Watson 	UNP_GLOBAL_WLOCK_ASSERT();
1502e7c33e29SRobert Watson 	UNP_PCB_LOCK_ASSERT(unp);
15030d9ce3a1SRobert Watson 
1504df8bae1dSRodney W. Grimes 	so->so_error = errno;
1505e7c33e29SRobert Watson 	unp2 = unp->unp_conn;
1506e7c33e29SRobert Watson 	if (unp2 == NULL)
1507e7c33e29SRobert Watson 		return;
1508e7c33e29SRobert Watson 
1509e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp2);
1510e7c33e29SRobert Watson 	unp_disconnect(unp, unp2);
1511e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp2);
1512df8bae1dSRodney W. Grimes }
1513df8bae1dSRodney W. Grimes 
15142bc21ed9SDavid Malone static void
1515892af6b9SRobert Watson unp_freerights(struct file **rp, int fdcount)
1516df8bae1dSRodney W. Grimes {
15172bc21ed9SDavid Malone 	int i;
15182bc21ed9SDavid Malone 	struct file *fp;
1519df8bae1dSRodney W. Grimes 
15202bc21ed9SDavid Malone 	for (i = 0; i < fdcount; i++) {
15218692c025SYoshinobu Inoue 		/*
15221c381b19SRobert Watson 		 * Zero the pointer before calling unp_discard since it may
15231c381b19SRobert Watson 		 * end up in unp_gc()..
1524d7dca903SRobert Watson 		 *
1525d7dca903SRobert Watson 		 * XXXRW: This is less true than it used to be.
15268692c025SYoshinobu Inoue 		 */
1527e7c33e29SRobert Watson 		fp = *rp;
1528e7c33e29SRobert Watson 		*rp++ = NULL;
15298692c025SYoshinobu Inoue 		unp_discard(fp);
1530df8bae1dSRodney W. Grimes 	}
15312bc21ed9SDavid Malone }
15322bc21ed9SDavid Malone 
15332bc21ed9SDavid Malone int
1534892af6b9SRobert Watson unp_externalize(struct mbuf *control, struct mbuf **controlp)
15352bc21ed9SDavid Malone {
15362bc21ed9SDavid Malone 	struct thread *td = curthread;		/* XXX */
15372bc21ed9SDavid Malone 	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
15382bc21ed9SDavid Malone 	int i;
15392bc21ed9SDavid Malone 	int *fdp;
15402bc21ed9SDavid Malone 	struct file **rp;
15412bc21ed9SDavid Malone 	struct file *fp;
15422bc21ed9SDavid Malone 	void *data;
15432bc21ed9SDavid Malone 	socklen_t clen = control->m_len, datalen;
15442bc21ed9SDavid Malone 	int error, newfds;
15452bc21ed9SDavid Malone 	int f;
15462bc21ed9SDavid Malone 	u_int newlen;
15472bc21ed9SDavid Malone 
1548e7c33e29SRobert Watson 	UNP_GLOBAL_UNLOCK_ASSERT();
15494c5bc1caSRobert Watson 
15502bc21ed9SDavid Malone 	error = 0;
15512bc21ed9SDavid Malone 	if (controlp != NULL) /* controlp == NULL => free control messages */
15522bc21ed9SDavid Malone 		*controlp = NULL;
15532bc21ed9SDavid Malone 
15542bc21ed9SDavid Malone 	while (cm != NULL) {
15552bc21ed9SDavid Malone 		if (sizeof(*cm) > clen || cm->cmsg_len > clen) {
15562bc21ed9SDavid Malone 			error = EINVAL;
15572bc21ed9SDavid Malone 			break;
15582bc21ed9SDavid Malone 		}
15592bc21ed9SDavid Malone 
15602bc21ed9SDavid Malone 		data = CMSG_DATA(cm);
15612bc21ed9SDavid Malone 		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
15622bc21ed9SDavid Malone 
15632bc21ed9SDavid Malone 		if (cm->cmsg_level == SOL_SOCKET
15642bc21ed9SDavid Malone 		    && cm->cmsg_type == SCM_RIGHTS) {
15652bc21ed9SDavid Malone 			newfds = datalen / sizeof(struct file *);
15662bc21ed9SDavid Malone 			rp = data;
15672bc21ed9SDavid Malone 
1568e2f9a08bSOlivier Houchard 			/* If we're not outputting the descriptors free them. */
15692bc21ed9SDavid Malone 			if (error || controlp == NULL) {
15702bc21ed9SDavid Malone 				unp_freerights(rp, newfds);
15712bc21ed9SDavid Malone 				goto next;
15722bc21ed9SDavid Malone 			}
15735e3f7694SRobert Watson 			FILEDESC_XLOCK(td->td_proc->p_fd);
15742bc21ed9SDavid Malone 			/* if the new FD's will not fit free them.  */
15752bc21ed9SDavid Malone 			if (!fdavail(td, newfds)) {
15765e3f7694SRobert Watson 				FILEDESC_XUNLOCK(td->td_proc->p_fd);
15772bc21ed9SDavid Malone 				error = EMSGSIZE;
15782bc21ed9SDavid Malone 				unp_freerights(rp, newfds);
15792bc21ed9SDavid Malone 				goto next;
1580df8bae1dSRodney W. Grimes 			}
1581ed5b7817SJulian Elischer 			/*
15821c381b19SRobert Watson 			 * Now change each pointer to an fd in the global
15831c381b19SRobert Watson 			 * table to an integer that is the index to the local
15841c381b19SRobert Watson 			 * fd table entry that we set up to point to the
15851c381b19SRobert Watson 			 * global one we are transferring.
1586ed5b7817SJulian Elischer 			 */
15872bc21ed9SDavid Malone 			newlen = newfds * sizeof(int);
15882bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, newlen,
15892bc21ed9SDavid Malone 			    SCM_RIGHTS, SOL_SOCKET);
15902bc21ed9SDavid Malone 			if (*controlp == NULL) {
15915e3f7694SRobert Watson 				FILEDESC_XUNLOCK(td->td_proc->p_fd);
15922bc21ed9SDavid Malone 				error = E2BIG;
15932bc21ed9SDavid Malone 				unp_freerights(rp, newfds);
15942bc21ed9SDavid Malone 				goto next;
15952bc21ed9SDavid Malone 			}
15962bc21ed9SDavid Malone 
15972bc21ed9SDavid Malone 			fdp = (int *)
15982bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1599df8bae1dSRodney W. Grimes 			for (i = 0; i < newfds; i++) {
1600a6d4491cSDag-Erling Smørgrav 				if (fdalloc(td, 0, &f))
16012bc21ed9SDavid Malone 					panic("unp_externalize fdalloc failed");
16028692c025SYoshinobu Inoue 				fp = *rp++;
1603b40ce416SJulian Elischer 				td->td_proc->p_fd->fd_ofiles[f] = fp;
1604397c19d1SJeff Roberson 				unp_externalize_fp(fp);
16058692c025SYoshinobu Inoue 				*fdp++ = f;
1606df8bae1dSRodney W. Grimes 			}
16075e3f7694SRobert Watson 			FILEDESC_XUNLOCK(td->td_proc->p_fd);
16081c381b19SRobert Watson 		} else {
16091c381b19SRobert Watson 			/* We can just copy anything else across. */
16102bc21ed9SDavid Malone 			if (error || controlp == NULL)
16112bc21ed9SDavid Malone 				goto next;
16122bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, datalen,
16132bc21ed9SDavid Malone 			    cm->cmsg_type, cm->cmsg_level);
16142bc21ed9SDavid Malone 			if (*controlp == NULL) {
16152bc21ed9SDavid Malone 				error = ENOBUFS;
16162bc21ed9SDavid Malone 				goto next;
16172bc21ed9SDavid Malone 			}
16182bc21ed9SDavid Malone 			bcopy(data,
16192bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *)),
16202bc21ed9SDavid Malone 			    datalen);
16212bc21ed9SDavid Malone 		}
16222bc21ed9SDavid Malone 
16232bc21ed9SDavid Malone 		controlp = &(*controlp)->m_next;
16242bc21ed9SDavid Malone 
16252bc21ed9SDavid Malone next:
16262bc21ed9SDavid Malone 		if (CMSG_SPACE(datalen) < clen) {
16272bc21ed9SDavid Malone 			clen -= CMSG_SPACE(datalen);
16282bc21ed9SDavid Malone 			cm = (struct cmsghdr *)
16292bc21ed9SDavid Malone 			    ((caddr_t)cm + CMSG_SPACE(datalen));
16308692c025SYoshinobu Inoue 		} else {
16312bc21ed9SDavid Malone 			clen = 0;
16322bc21ed9SDavid Malone 			cm = NULL;
16338692c025SYoshinobu Inoue 		}
16348692c025SYoshinobu Inoue 	}
16358692c025SYoshinobu Inoue 
16362bc21ed9SDavid Malone 	m_freem(control);
16372bc21ed9SDavid Malone 
16382bc21ed9SDavid Malone 	return (error);
1639df8bae1dSRodney W. Grimes }
1640df8bae1dSRodney W. Grimes 
16414f590175SPaul Saab static void
16424f590175SPaul Saab unp_zone_change(void *tag)
16434f590175SPaul Saab {
16444f590175SPaul Saab 
16454f590175SPaul Saab 	uma_zone_set_max(unp_zone, maxsockets);
16464f590175SPaul Saab }
16474f590175SPaul Saab 
164898271db4SGarrett Wollman void
164998271db4SGarrett Wollman unp_init(void)
165098271db4SGarrett Wollman {
16511c381b19SRobert Watson 
16529e9d298aSJeff Roberson 	unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL,
16539ae328fcSJohn Baldwin 	    NULL, NULL, UMA_ALIGN_PTR, 0);
1654fc3fcacfSRobert Watson 	if (unp_zone == NULL)
165598271db4SGarrett Wollman 		panic("unp_init");
16564f590175SPaul Saab 	uma_zone_set_max(unp_zone, maxsockets);
16574f590175SPaul Saab 	EVENTHANDLER_REGISTER(maxsockets_change, unp_zone_change,
16584f590175SPaul Saab 	    NULL, EVENTHANDLER_PRI_ANY);
165998271db4SGarrett Wollman 	LIST_INIT(&unp_dhead);
166098271db4SGarrett Wollman 	LIST_INIT(&unp_shead);
1661a0ec558aSRobert Watson 	TASK_INIT(&unp_gc_task, 0, unp_gc, NULL);
1662e7c33e29SRobert Watson 	UNP_GLOBAL_LOCK_INIT();
166398271db4SGarrett Wollman }
166498271db4SGarrett Wollman 
1665f708ef1bSPoul-Henning Kamp static int
1666892af6b9SRobert Watson unp_internalize(struct mbuf **controlp, struct thread *td)
1667df8bae1dSRodney W. Grimes {
16682bc21ed9SDavid Malone 	struct mbuf *control = *controlp;
1669b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
16708692c025SYoshinobu Inoue 	struct filedesc *fdescp = p->p_fd;
16712bc21ed9SDavid Malone 	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
16722bc21ed9SDavid Malone 	struct cmsgcred *cmcred;
16732bc21ed9SDavid Malone 	struct file **rp;
16742bc21ed9SDavid Malone 	struct file *fp;
16752bc21ed9SDavid Malone 	struct timeval *tv;
16762bc21ed9SDavid Malone 	int i, fd, *fdp;
16772bc21ed9SDavid Malone 	void *data;
16782bc21ed9SDavid Malone 	socklen_t clen = control->m_len, datalen;
16792bc21ed9SDavid Malone 	int error, oldfds;
16808692c025SYoshinobu Inoue 	u_int newlen;
1681df8bae1dSRodney W. Grimes 
1682e7c33e29SRobert Watson 	UNP_GLOBAL_UNLOCK_ASSERT();
16834c5bc1caSRobert Watson 
16842bc21ed9SDavid Malone 	error = 0;
16852bc21ed9SDavid Malone 	*controlp = NULL;
16860b788fa1SBill Paul 
16872bc21ed9SDavid Malone 	while (cm != NULL) {
16882bc21ed9SDavid Malone 		if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET
16892bc21ed9SDavid Malone 		    || cm->cmsg_len > clen) {
16902bc21ed9SDavid Malone 			error = EINVAL;
16912bc21ed9SDavid Malone 			goto out;
16922bc21ed9SDavid Malone 		}
16932bc21ed9SDavid Malone 
16942bc21ed9SDavid Malone 		data = CMSG_DATA(cm);
16952bc21ed9SDavid Malone 		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
16962bc21ed9SDavid Malone 
16972bc21ed9SDavid Malone 		switch (cm->cmsg_type) {
16980b788fa1SBill Paul 		/*
16990b788fa1SBill Paul 		 * Fill in credential information.
17000b788fa1SBill Paul 		 */
17012bc21ed9SDavid Malone 		case SCM_CREDS:
17022bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, sizeof(*cmcred),
17032bc21ed9SDavid Malone 			    SCM_CREDS, SOL_SOCKET);
17042bc21ed9SDavid Malone 			if (*controlp == NULL) {
17052bc21ed9SDavid Malone 				error = ENOBUFS;
17062bc21ed9SDavid Malone 				goto out;
17072bc21ed9SDavid Malone 			}
17082bc21ed9SDavid Malone 
17092bc21ed9SDavid Malone 			cmcred = (struct cmsgcred *)
17102bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
17110b788fa1SBill Paul 			cmcred->cmcred_pid = p->p_pid;
1712a854ed98SJohn Baldwin 			cmcred->cmcred_uid = td->td_ucred->cr_ruid;
1713a854ed98SJohn Baldwin 			cmcred->cmcred_gid = td->td_ucred->cr_rgid;
1714a854ed98SJohn Baldwin 			cmcred->cmcred_euid = td->td_ucred->cr_uid;
1715a854ed98SJohn Baldwin 			cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups,
17160b788fa1SBill Paul 							CMGROUP_MAX);
17170b788fa1SBill Paul 			for (i = 0; i < cmcred->cmcred_ngroups; i++)
17182bc21ed9SDavid Malone 				cmcred->cmcred_groups[i] =
1719a854ed98SJohn Baldwin 				    td->td_ucred->cr_groups[i];
17202bc21ed9SDavid Malone 			break;
17210b788fa1SBill Paul 
17222bc21ed9SDavid Malone 		case SCM_RIGHTS:
17232bc21ed9SDavid Malone 			oldfds = datalen / sizeof (int);
1724ed5b7817SJulian Elischer 			/*
17251c381b19SRobert Watson 			 * Check that all the FDs passed in refer to legal
17261c381b19SRobert Watson 			 * files.  If not, reject the entire operation.
1727ed5b7817SJulian Elischer 			 */
17282bc21ed9SDavid Malone 			fdp = data;
17295e3f7694SRobert Watson 			FILEDESC_SLOCK(fdescp);
1730df8bae1dSRodney W. Grimes 			for (i = 0; i < oldfds; i++) {
17318692c025SYoshinobu Inoue 				fd = *fdp++;
17328692c025SYoshinobu Inoue 				if ((unsigned)fd >= fdescp->fd_nfiles ||
17332bc21ed9SDavid Malone 				    fdescp->fd_ofiles[fd] == NULL) {
17345e3f7694SRobert Watson 					FILEDESC_SUNLOCK(fdescp);
17352bc21ed9SDavid Malone 					error = EBADF;
17362bc21ed9SDavid Malone 					goto out;
17372bc21ed9SDavid Malone 				}
1738e7d6662fSAlfred Perlstein 				fp = fdescp->fd_ofiles[fd];
1739e7d6662fSAlfred Perlstein 				if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) {
17405e3f7694SRobert Watson 					FILEDESC_SUNLOCK(fdescp);
1741e7d6662fSAlfred Perlstein 					error = EOPNOTSUPP;
1742e7d6662fSAlfred Perlstein 					goto out;
1743e7d6662fSAlfred Perlstein 				}
1744e7d6662fSAlfred Perlstein 
1745df8bae1dSRodney W. Grimes 			}
17465e3f7694SRobert Watson 
1747ed5b7817SJulian Elischer 			/*
1748e7c33e29SRobert Watson 			 * Now replace the integer FDs with pointers to
1749e7c33e29SRobert Watson 			 * the associated global file table entry..
1750ed5b7817SJulian Elischer 			 */
17512bc21ed9SDavid Malone 			newlen = oldfds * sizeof(struct file *);
17522bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, newlen,
17532bc21ed9SDavid Malone 			    SCM_RIGHTS, SOL_SOCKET);
17542bc21ed9SDavid Malone 			if (*controlp == NULL) {
17555e3f7694SRobert Watson 				FILEDESC_SUNLOCK(fdescp);
17562bc21ed9SDavid Malone 				error = E2BIG;
17572bc21ed9SDavid Malone 				goto out;
17588692c025SYoshinobu Inoue 			}
17598692c025SYoshinobu Inoue 
17602bc21ed9SDavid Malone 			fdp = data;
17612bc21ed9SDavid Malone 			rp = (struct file **)
17622bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
17638692c025SYoshinobu Inoue 			for (i = 0; i < oldfds; i++) {
17648692c025SYoshinobu Inoue 				fp = fdescp->fd_ofiles[*fdp++];
1765df8bae1dSRodney W. Grimes 				*rp++ = fp;
1766397c19d1SJeff Roberson 				fhold(fp);
1767397c19d1SJeff Roberson 				unp_internalize_fp(fp);
1768df8bae1dSRodney W. Grimes 			}
17695e3f7694SRobert Watson 			FILEDESC_SUNLOCK(fdescp);
17702bc21ed9SDavid Malone 			break;
17712bc21ed9SDavid Malone 
17722bc21ed9SDavid Malone 		case SCM_TIMESTAMP:
17732bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, sizeof(*tv),
17742bc21ed9SDavid Malone 			    SCM_TIMESTAMP, SOL_SOCKET);
17752bc21ed9SDavid Malone 			if (*controlp == NULL) {
17762bc21ed9SDavid Malone 				error = ENOBUFS;
17772bc21ed9SDavid Malone 				goto out;
17788692c025SYoshinobu Inoue 			}
17792bc21ed9SDavid Malone 			tv = (struct timeval *)
17802bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
17812bc21ed9SDavid Malone 			microtime(tv);
17822bc21ed9SDavid Malone 			break;
17832bc21ed9SDavid Malone 
17842bc21ed9SDavid Malone 		default:
17852bc21ed9SDavid Malone 			error = EINVAL;
17862bc21ed9SDavid Malone 			goto out;
17872bc21ed9SDavid Malone 		}
17882bc21ed9SDavid Malone 
17892bc21ed9SDavid Malone 		controlp = &(*controlp)->m_next;
17902bc21ed9SDavid Malone 
17912bc21ed9SDavid Malone 		if (CMSG_SPACE(datalen) < clen) {
17922bc21ed9SDavid Malone 			clen -= CMSG_SPACE(datalen);
17932bc21ed9SDavid Malone 			cm = (struct cmsghdr *)
17942bc21ed9SDavid Malone 			    ((caddr_t)cm + CMSG_SPACE(datalen));
17952bc21ed9SDavid Malone 		} else {
17962bc21ed9SDavid Malone 			clen = 0;
17972bc21ed9SDavid Malone 			cm = NULL;
17982bc21ed9SDavid Malone 		}
17992bc21ed9SDavid Malone 	}
18002bc21ed9SDavid Malone 
18012bc21ed9SDavid Malone out:
18022bc21ed9SDavid Malone 	m_freem(control);
18032bc21ed9SDavid Malone 
18042bc21ed9SDavid Malone 	return (error);
1805df8bae1dSRodney W. Grimes }
1806df8bae1dSRodney W. Grimes 
18075b950deaSRobert Watson static struct mbuf *
18086a2989fdSMatthew N. Dodd unp_addsockcred(struct thread *td, struct mbuf *control)
18096a2989fdSMatthew N. Dodd {
181070df31f4SMaxim Konovalov 	struct mbuf *m, *n, *n_prev;
18116a2989fdSMatthew N. Dodd 	struct sockcred *sc;
181270df31f4SMaxim Konovalov 	const struct cmsghdr *cm;
18136a2989fdSMatthew N. Dodd 	int ngroups;
18146a2989fdSMatthew N. Dodd 	int i;
18156a2989fdSMatthew N. Dodd 
18166a2989fdSMatthew N. Dodd 	ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX);
18176a2989fdSMatthew N. Dodd 
18186a2989fdSMatthew N. Dodd 	m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET);
18196a2989fdSMatthew N. Dodd 	if (m == NULL)
18206a2989fdSMatthew N. Dodd 		return (control);
18216a2989fdSMatthew N. Dodd 
18226a2989fdSMatthew N. Dodd 	sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *));
18236a2989fdSMatthew N. Dodd 	sc->sc_uid = td->td_ucred->cr_ruid;
18246a2989fdSMatthew N. Dodd 	sc->sc_euid = td->td_ucred->cr_uid;
18256a2989fdSMatthew N. Dodd 	sc->sc_gid = td->td_ucred->cr_rgid;
18266a2989fdSMatthew N. Dodd 	sc->sc_egid = td->td_ucred->cr_gid;
18276a2989fdSMatthew N. Dodd 	sc->sc_ngroups = ngroups;
18286a2989fdSMatthew N. Dodd 	for (i = 0; i < sc->sc_ngroups; i++)
18296a2989fdSMatthew N. Dodd 		sc->sc_groups[i] = td->td_ucred->cr_groups[i];
18306a2989fdSMatthew N. Dodd 
18316a2989fdSMatthew N. Dodd 	/*
18321c381b19SRobert Watson 	 * Unlink SCM_CREDS control messages (struct cmsgcred), since just
18331c381b19SRobert Watson 	 * created SCM_CREDS control message (struct sockcred) has another
18341c381b19SRobert Watson 	 * format.
18356a2989fdSMatthew N. Dodd 	 */
183670df31f4SMaxim Konovalov 	if (control != NULL)
183770df31f4SMaxim Konovalov 		for (n = control, n_prev = NULL; n != NULL;) {
183870df31f4SMaxim Konovalov 			cm = mtod(n, struct cmsghdr *);
183970df31f4SMaxim Konovalov     			if (cm->cmsg_level == SOL_SOCKET &&
184070df31f4SMaxim Konovalov 			    cm->cmsg_type == SCM_CREDS) {
184170df31f4SMaxim Konovalov     				if (n_prev == NULL)
184270df31f4SMaxim Konovalov 					control = n->m_next;
184370df31f4SMaxim Konovalov 				else
184470df31f4SMaxim Konovalov 					n_prev->m_next = n->m_next;
184570df31f4SMaxim Konovalov 				n = m_free(n);
184670df31f4SMaxim Konovalov 			} else {
184770df31f4SMaxim Konovalov 				n_prev = n;
184870df31f4SMaxim Konovalov 				n = n->m_next;
184970df31f4SMaxim Konovalov 			}
185070df31f4SMaxim Konovalov 		}
18516a2989fdSMatthew N. Dodd 
185270df31f4SMaxim Konovalov 	/* Prepend it to the head. */
185370df31f4SMaxim Konovalov 	m->m_next = control;
185470df31f4SMaxim Konovalov 
185570df31f4SMaxim Konovalov 	return (m);
18566a2989fdSMatthew N. Dodd }
18576a2989fdSMatthew N. Dodd 
1858397c19d1SJeff Roberson static struct unpcb *
1859397c19d1SJeff Roberson fptounp(struct file *fp)
1860397c19d1SJeff Roberson {
1861397c19d1SJeff Roberson 	struct socket *so;
1862397c19d1SJeff Roberson 
1863397c19d1SJeff Roberson 	if (fp->f_type != DTYPE_SOCKET)
1864397c19d1SJeff Roberson 		return (NULL);
1865397c19d1SJeff Roberson 	if ((so = fp->f_data) == NULL)
1866397c19d1SJeff Roberson 		return (NULL);
1867397c19d1SJeff Roberson 	if (so->so_proto->pr_domain != &localdomain)
1868397c19d1SJeff Roberson 		return (NULL);
1869397c19d1SJeff Roberson 	return sotounpcb(so);
1870397c19d1SJeff Roberson }
1871397c19d1SJeff Roberson 
1872397c19d1SJeff Roberson static void
1873397c19d1SJeff Roberson unp_discard(struct file *fp)
1874397c19d1SJeff Roberson {
1875397c19d1SJeff Roberson 
1876397c19d1SJeff Roberson 	unp_externalize_fp(fp);
1877397c19d1SJeff Roberson 	(void) closef(fp, (struct thread *)NULL);
1878397c19d1SJeff Roberson }
1879397c19d1SJeff Roberson 
1880397c19d1SJeff Roberson static void
1881397c19d1SJeff Roberson unp_internalize_fp(struct file *fp)
1882397c19d1SJeff Roberson {
1883397c19d1SJeff Roberson 	struct unpcb *unp;
1884397c19d1SJeff Roberson 
1885397c19d1SJeff Roberson 	UNP_GLOBAL_WLOCK();
1886397c19d1SJeff Roberson 	if ((unp = fptounp(fp)) != NULL) {
1887397c19d1SJeff Roberson 		unp->unp_file = fp;
1888397c19d1SJeff Roberson 		unp->unp_msgcount++;
1889397c19d1SJeff Roberson 	}
1890397c19d1SJeff Roberson 	unp_rights++;
1891397c19d1SJeff Roberson 	UNP_GLOBAL_WUNLOCK();
1892397c19d1SJeff Roberson }
1893397c19d1SJeff Roberson 
1894397c19d1SJeff Roberson static void
1895397c19d1SJeff Roberson unp_externalize_fp(struct file *fp)
1896397c19d1SJeff Roberson {
1897397c19d1SJeff Roberson 	struct unpcb *unp;
1898397c19d1SJeff Roberson 
1899397c19d1SJeff Roberson 	UNP_GLOBAL_WLOCK();
1900397c19d1SJeff Roberson 	if ((unp = fptounp(fp)) != NULL)
1901397c19d1SJeff Roberson 		unp->unp_msgcount--;
1902397c19d1SJeff Roberson 	unp_rights--;
1903397c19d1SJeff Roberson 	UNP_GLOBAL_WUNLOCK();
1904397c19d1SJeff Roberson }
1905397c19d1SJeff Roberson 
1906161a0c7cSRobert Watson /*
1907a0ec558aSRobert Watson  * unp_defer indicates whether additional work has been defered for a future
1908a0ec558aSRobert Watson  * pass through unp_gc().  It is thread local and does not require explicit
1909a0ec558aSRobert Watson  * synchronization.
1910161a0c7cSRobert Watson  */
1911397c19d1SJeff Roberson static int	unp_marked;
1912397c19d1SJeff Roberson static int	unp_unreachable;
1913a0ec558aSRobert Watson 
1914397c19d1SJeff Roberson static void
1915397c19d1SJeff Roberson unp_accessable(struct file *fp)
1916397c19d1SJeff Roberson {
1917397c19d1SJeff Roberson 	struct unpcb *unp;
1918397c19d1SJeff Roberson 
19196f552cb0SJeff Roberson 	if ((unp = fptounp(fp)) == NULL)
1920397c19d1SJeff Roberson 		return;
1921397c19d1SJeff Roberson 	if (unp->unp_gcflag & UNPGC_REF)
1922397c19d1SJeff Roberson 		return;
1923397c19d1SJeff Roberson 	unp->unp_gcflag &= ~UNPGC_DEAD;
1924397c19d1SJeff Roberson 	unp->unp_gcflag |= UNPGC_REF;
1925397c19d1SJeff Roberson 	unp_marked++;
1926397c19d1SJeff Roberson }
1927397c19d1SJeff Roberson 
1928397c19d1SJeff Roberson static void
1929397c19d1SJeff Roberson unp_gc_process(struct unpcb *unp)
1930397c19d1SJeff Roberson {
1931397c19d1SJeff Roberson 	struct socket *soa;
1932397c19d1SJeff Roberson 	struct socket *so;
1933397c19d1SJeff Roberson 	struct file *fp;
1934397c19d1SJeff Roberson 
1935397c19d1SJeff Roberson 	/* Already processed. */
1936397c19d1SJeff Roberson 	if (unp->unp_gcflag & UNPGC_SCANNED)
1937397c19d1SJeff Roberson 		return;
1938397c19d1SJeff Roberson 	fp = unp->unp_file;
1939397c19d1SJeff Roberson 	/*
1940397c19d1SJeff Roberson 	 * Check for a socket potentially in a cycle.  It must be in a
1941397c19d1SJeff Roberson 	 * queue as indicated by msgcount, and this must equal the file
1942397c19d1SJeff Roberson 	 * reference count.  Note that when msgcount is 0 the file is NULL.
1943397c19d1SJeff Roberson 	 */
19446f552cb0SJeff Roberson 	if (fp && fp->f_count != 0 && fp->f_count == unp->unp_msgcount) {
1945397c19d1SJeff Roberson 		unp->unp_gcflag |= UNPGC_DEAD;
1946397c19d1SJeff Roberson 		unp_unreachable++;
1947397c19d1SJeff Roberson 		return;
1948397c19d1SJeff Roberson 	}
1949397c19d1SJeff Roberson 	/*
1950397c19d1SJeff Roberson 	 * Mark all sockets we reference with RIGHTS.
1951397c19d1SJeff Roberson 	 */
1952397c19d1SJeff Roberson 	so = unp->unp_socket;
1953397c19d1SJeff Roberson 	SOCKBUF_LOCK(&so->so_rcv);
1954397c19d1SJeff Roberson 	unp_scan(so->so_rcv.sb_mb, unp_accessable);
1955397c19d1SJeff Roberson 	SOCKBUF_UNLOCK(&so->so_rcv);
1956397c19d1SJeff Roberson 	/*
1957397c19d1SJeff Roberson 	 * Mark all sockets in our accept queue.
1958397c19d1SJeff Roberson 	 */
1959397c19d1SJeff Roberson 	ACCEPT_LOCK();
1960397c19d1SJeff Roberson 	TAILQ_FOREACH(soa, &so->so_comp, so_list) {
1961397c19d1SJeff Roberson 		SOCKBUF_LOCK(&soa->so_rcv);
1962397c19d1SJeff Roberson 		unp_scan(soa->so_rcv.sb_mb, unp_accessable);
1963397c19d1SJeff Roberson 		SOCKBUF_UNLOCK(&soa->so_rcv);
1964397c19d1SJeff Roberson 	}
1965397c19d1SJeff Roberson 	ACCEPT_UNLOCK();
1966397c19d1SJeff Roberson 	unp->unp_gcflag |= UNPGC_SCANNED;
1967397c19d1SJeff Roberson }
1968a0ec558aSRobert Watson 
1969a0ec558aSRobert Watson static int unp_recycled;
1970a0ec558aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0, "");
1971df8bae1dSRodney W. Grimes 
1972397c19d1SJeff Roberson static int unp_taskcount;
1973397c19d1SJeff Roberson SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0, "");
1974397c19d1SJeff Roberson 
1975f708ef1bSPoul-Henning Kamp static void
1976a0ec558aSRobert Watson unp_gc(__unused void *arg, int pending)
1977df8bae1dSRodney W. Grimes {
1978397c19d1SJeff Roberson 	struct unp_head *heads[] = { &unp_dhead, &unp_shead, NULL };
1979397c19d1SJeff Roberson 	struct unp_head **head;
1980397c19d1SJeff Roberson 	struct file **unref;
1981397c19d1SJeff Roberson 	struct unpcb *unp;
1982397c19d1SJeff Roberson 	int i;
1983df8bae1dSRodney W. Grimes 
1984a0ec558aSRobert Watson 	unp_taskcount++;
1985397c19d1SJeff Roberson 	UNP_GLOBAL_RLOCK();
1986ed5b7817SJulian Elischer 	/*
1987397c19d1SJeff Roberson 	 * First clear all gc flags from previous runs.
1988ed5b7817SJulian Elischer 	 */
1989397c19d1SJeff Roberson 	for (head = heads; *head != NULL; head++)
1990397c19d1SJeff Roberson 		LIST_FOREACH(unp, *head, unp_link)
1991397c19d1SJeff Roberson 			unp->unp_gcflag &= ~(UNPGC_REF|UNPGC_DEAD);
1992397c19d1SJeff Roberson 	/*
1993397c19d1SJeff Roberson 	 * Scan marking all reachable sockets with UNPGC_REF.  Once a socket
1994397c19d1SJeff Roberson 	 * is reachable all of the sockets it references are reachable.
1995397c19d1SJeff Roberson 	 * Stop the scan once we do a complete loop without discovering
1996397c19d1SJeff Roberson 	 * a new reachable socket.
1997397c19d1SJeff Roberson 	 */
1998df8bae1dSRodney W. Grimes 	do {
1999397c19d1SJeff Roberson 		unp_unreachable = 0;
2000397c19d1SJeff Roberson 		unp_marked = 0;
2001397c19d1SJeff Roberson 		for (head = heads; *head != NULL; head++)
2002397c19d1SJeff Roberson 			LIST_FOREACH(unp, *head, unp_link)
2003397c19d1SJeff Roberson 				unp_gc_process(unp);
2004397c19d1SJeff Roberson 	} while (unp_marked);
2005397c19d1SJeff Roberson 	UNP_GLOBAL_RUNLOCK();
2006397c19d1SJeff Roberson 	if (unp_unreachable == 0)
2007397c19d1SJeff Roberson 		return;
2008ed5b7817SJulian Elischer 	/*
2009397c19d1SJeff Roberson 	 * Allocate space for a local list of dead unpcbs.
2010ed5b7817SJulian Elischer 	 */
2011397c19d1SJeff Roberson 	unref = malloc(unp_unreachable * sizeof(struct file *),
2012397c19d1SJeff Roberson 	    M_TEMP, M_WAITOK);
2013ed5b7817SJulian Elischer 	/*
2014397c19d1SJeff Roberson 	 * Iterate looking for sockets which have been specifically marked
2015397c19d1SJeff Roberson 	 * as as unreachable and store them locally.
2016ed5b7817SJulian Elischer 	 */
2017397c19d1SJeff Roberson 	UNP_GLOBAL_RLOCK();
2018397c19d1SJeff Roberson 	for (i = 0, head = heads; *head != NULL; head++)
2019397c19d1SJeff Roberson 		LIST_FOREACH(unp, *head, unp_link)
2020397c19d1SJeff Roberson 			if (unp->unp_gcflag & UNPGC_DEAD) {
2021397c19d1SJeff Roberson 				unref[i++] = unp->unp_file;
2022397c19d1SJeff Roberson 				KASSERT(unp->unp_file != NULL,
2023397c19d1SJeff Roberson 				    ("unp_gc: Invalid unpcb."));
2024397c19d1SJeff Roberson 				KASSERT(i <= unp_unreachable,
2025397c19d1SJeff Roberson 				    ("unp_gc: incorrect unreachable count."));
2026397c19d1SJeff Roberson 			}
2027397c19d1SJeff Roberson 	UNP_GLOBAL_RUNLOCK();
2028ed5b7817SJulian Elischer 	/*
2029397c19d1SJeff Roberson 	 * All further operation is now done on a local list.  We first ref
2030397c19d1SJeff Roberson 	 * all sockets to avoid closing them until all are flushed.
2031ed5b7817SJulian Elischer 	 */
2032397c19d1SJeff Roberson 	for (i = 0; i < unp_unreachable; i++)
2033397c19d1SJeff Roberson 		fhold(unref[i]);
2034ed5b7817SJulian Elischer 	/*
2035397c19d1SJeff Roberson 	 * Now flush all sockets, free'ing rights.  This will free the
2036397c19d1SJeff Roberson 	 * struct files associated with these sockets but leave each socket
2037397c19d1SJeff Roberson 	 * with one remaining ref.
2038ed5b7817SJulian Elischer 	 */
2039397c19d1SJeff Roberson 	for (i = 0; i < unp_unreachable; i++)
2040397c19d1SJeff Roberson 		sorflush(unref[i]->f_data);
2041ed5b7817SJulian Elischer 	/*
2042397c19d1SJeff Roberson 	 * And finally release the sockets so they can be reclaimed.
2043ed5b7817SJulian Elischer 	 */
2044397c19d1SJeff Roberson 	for (i = 0; i < unp_unreachable; i++)
2045397c19d1SJeff Roberson 		fdrop(unref[i], NULL);
2046397c19d1SJeff Roberson 	unp_recycled += unp_unreachable;
2047397c19d1SJeff Roberson 	free(unref, M_TEMP);
2048df8bae1dSRodney W. Grimes }
2049df8bae1dSRodney W. Grimes 
205026f9a767SRodney W. Grimes void
2051892af6b9SRobert Watson unp_dispose(struct mbuf *m)
2052df8bae1dSRodney W. Grimes {
2053996c772fSJohn Dyson 
2054df8bae1dSRodney W. Grimes 	if (m)
2055df8bae1dSRodney W. Grimes 		unp_scan(m, unp_discard);
2056df8bae1dSRodney W. Grimes }
2057df8bae1dSRodney W. Grimes 
2058f708ef1bSPoul-Henning Kamp static void
2059892af6b9SRobert Watson unp_scan(struct mbuf *m0, void (*op)(struct file *))
2060df8bae1dSRodney W. Grimes {
20612bc21ed9SDavid Malone 	struct mbuf *m;
20622bc21ed9SDavid Malone 	struct file **rp;
20632bc21ed9SDavid Malone 	struct cmsghdr *cm;
20642bc21ed9SDavid Malone 	void *data;
20652bc21ed9SDavid Malone 	int i;
20662bc21ed9SDavid Malone 	socklen_t clen, datalen;
2067df8bae1dSRodney W. Grimes 	int qfds;
2068df8bae1dSRodney W. Grimes 
2069fc3fcacfSRobert Watson 	while (m0 != NULL) {
20702bc21ed9SDavid Malone 		for (m = m0; m; m = m->m_next) {
207112396bdcSDavid Malone 			if (m->m_type != MT_CONTROL)
2072df8bae1dSRodney W. Grimes 				continue;
20732bc21ed9SDavid Malone 
20742bc21ed9SDavid Malone 			cm = mtod(m, struct cmsghdr *);
20752bc21ed9SDavid Malone 			clen = m->m_len;
20762bc21ed9SDavid Malone 
20772bc21ed9SDavid Malone 			while (cm != NULL) {
20782bc21ed9SDavid Malone 				if (sizeof(*cm) > clen || cm->cmsg_len > clen)
20792bc21ed9SDavid Malone 					break;
20802bc21ed9SDavid Malone 
20812bc21ed9SDavid Malone 				data = CMSG_DATA(cm);
20822bc21ed9SDavid Malone 				datalen = (caddr_t)cm + cm->cmsg_len
20832bc21ed9SDavid Malone 				    - (caddr_t)data;
20842bc21ed9SDavid Malone 
20852bc21ed9SDavid Malone 				if (cm->cmsg_level == SOL_SOCKET &&
20862bc21ed9SDavid Malone 				    cm->cmsg_type == SCM_RIGHTS) {
20872bc21ed9SDavid Malone 					qfds = datalen / sizeof (struct file *);
20882bc21ed9SDavid Malone 					rp = data;
2089df8bae1dSRodney W. Grimes 					for (i = 0; i < qfds; i++)
2090df8bae1dSRodney W. Grimes 						(*op)(*rp++);
20912bc21ed9SDavid Malone 				}
20922bc21ed9SDavid Malone 
20932bc21ed9SDavid Malone 				if (CMSG_SPACE(datalen) < clen) {
20942bc21ed9SDavid Malone 					clen -= CMSG_SPACE(datalen);
20952bc21ed9SDavid Malone 					cm = (struct cmsghdr *)
20962bc21ed9SDavid Malone 					    ((caddr_t)cm + CMSG_SPACE(datalen));
20972bc21ed9SDavid Malone 				} else {
20982bc21ed9SDavid Malone 					clen = 0;
20992bc21ed9SDavid Malone 					cm = NULL;
21002bc21ed9SDavid Malone 				}
21012bc21ed9SDavid Malone 			}
2102df8bae1dSRodney W. Grimes 		}
2103df8bae1dSRodney W. Grimes 		m0 = m0->m_act;
2104df8bae1dSRodney W. Grimes 	}
2105df8bae1dSRodney W. Grimes }
2106df8bae1dSRodney W. Grimes 
210703c96c31SRobert Watson #ifdef DDB
210803c96c31SRobert Watson static void
210903c96c31SRobert Watson db_print_indent(int indent)
211003c96c31SRobert Watson {
211103c96c31SRobert Watson 	int i;
211203c96c31SRobert Watson 
211303c96c31SRobert Watson 	for (i = 0; i < indent; i++)
211403c96c31SRobert Watson 		db_printf(" ");
211503c96c31SRobert Watson }
211603c96c31SRobert Watson 
211703c96c31SRobert Watson static void
211803c96c31SRobert Watson db_print_unpflags(int unp_flags)
211903c96c31SRobert Watson {
212003c96c31SRobert Watson 	int comma;
212103c96c31SRobert Watson 
212203c96c31SRobert Watson 	comma = 0;
212303c96c31SRobert Watson 	if (unp_flags & UNP_HAVEPC) {
212403c96c31SRobert Watson 		db_printf("%sUNP_HAVEPC", comma ? ", " : "");
212503c96c31SRobert Watson 		comma = 1;
212603c96c31SRobert Watson 	}
212703c96c31SRobert Watson 	if (unp_flags & UNP_HAVEPCCACHED) {
212803c96c31SRobert Watson 		db_printf("%sUNP_HAVEPCCACHED", comma ? ", " : "");
212903c96c31SRobert Watson 		comma = 1;
213003c96c31SRobert Watson 	}
213103c96c31SRobert Watson 	if (unp_flags & UNP_WANTCRED) {
213203c96c31SRobert Watson 		db_printf("%sUNP_WANTCRED", comma ? ", " : "");
213303c96c31SRobert Watson 		comma = 1;
213403c96c31SRobert Watson 	}
213503c96c31SRobert Watson 	if (unp_flags & UNP_CONNWAIT) {
213603c96c31SRobert Watson 		db_printf("%sUNP_CONNWAIT", comma ? ", " : "");
213703c96c31SRobert Watson 		comma = 1;
213803c96c31SRobert Watson 	}
213903c96c31SRobert Watson 	if (unp_flags & UNP_CONNECTING) {
214003c96c31SRobert Watson 		db_printf("%sUNP_CONNECTING", comma ? ", " : "");
214103c96c31SRobert Watson 		comma = 1;
214203c96c31SRobert Watson 	}
214303c96c31SRobert Watson 	if (unp_flags & UNP_BINDING) {
214403c96c31SRobert Watson 		db_printf("%sUNP_BINDING", comma ? ", " : "");
214503c96c31SRobert Watson 		comma = 1;
214603c96c31SRobert Watson 	}
214703c96c31SRobert Watson }
214803c96c31SRobert Watson 
214903c96c31SRobert Watson static void
215003c96c31SRobert Watson db_print_xucred(int indent, struct xucred *xu)
215103c96c31SRobert Watson {
215203c96c31SRobert Watson 	int comma, i;
215303c96c31SRobert Watson 
215403c96c31SRobert Watson 	db_print_indent(indent);
215503c96c31SRobert Watson 	db_printf("cr_version: %u   cr_uid: %u   cr_ngroups: %d\n",
215603c96c31SRobert Watson 	    xu->cr_version, xu->cr_uid, xu->cr_ngroups);
215703c96c31SRobert Watson 	db_print_indent(indent);
215803c96c31SRobert Watson 	db_printf("cr_groups: ");
215903c96c31SRobert Watson 	comma = 0;
216003c96c31SRobert Watson 	for (i = 0; i < xu->cr_ngroups; i++) {
216103c96c31SRobert Watson 		db_printf("%s%u", comma ? ", " : "", xu->cr_groups[i]);
216203c96c31SRobert Watson 		comma = 1;
216303c96c31SRobert Watson 	}
216403c96c31SRobert Watson 	db_printf("\n");
216503c96c31SRobert Watson }
216603c96c31SRobert Watson 
216703c96c31SRobert Watson static void
216803c96c31SRobert Watson db_print_unprefs(int indent, struct unp_head *uh)
216903c96c31SRobert Watson {
217003c96c31SRobert Watson 	struct unpcb *unp;
217103c96c31SRobert Watson 	int counter;
217203c96c31SRobert Watson 
217303c96c31SRobert Watson 	counter = 0;
217403c96c31SRobert Watson 	LIST_FOREACH(unp, uh, unp_reflink) {
217503c96c31SRobert Watson 		if (counter % 4 == 0)
217603c96c31SRobert Watson 			db_print_indent(indent);
217703c96c31SRobert Watson 		db_printf("%p  ", unp);
217803c96c31SRobert Watson 		if (counter % 4 == 3)
217903c96c31SRobert Watson 			db_printf("\n");
218003c96c31SRobert Watson 		counter++;
218103c96c31SRobert Watson 	}
218203c96c31SRobert Watson 	if (counter != 0 && counter % 4 != 0)
218303c96c31SRobert Watson 		db_printf("\n");
218403c96c31SRobert Watson }
218503c96c31SRobert Watson 
218603c96c31SRobert Watson DB_SHOW_COMMAND(unpcb, db_show_unpcb)
218703c96c31SRobert Watson {
218803c96c31SRobert Watson 	struct unpcb *unp;
218903c96c31SRobert Watson 
219003c96c31SRobert Watson         if (!have_addr) {
219103c96c31SRobert Watson                 db_printf("usage: show unpcb <addr>\n");
219203c96c31SRobert Watson                 return;
219303c96c31SRobert Watson         }
219403c96c31SRobert Watson         unp = (struct unpcb *)addr;
219503c96c31SRobert Watson 
219603c96c31SRobert Watson 	db_printf("unp_socket: %p   unp_vnode: %p\n", unp->unp_socket,
219703c96c31SRobert Watson 	    unp->unp_vnode);
219803c96c31SRobert Watson 
219903c96c31SRobert Watson 	db_printf("unp_ino: %d   unp_conn: %p\n", unp->unp_ino,
220003c96c31SRobert Watson 	    unp->unp_conn);
220103c96c31SRobert Watson 
220203c96c31SRobert Watson 	db_printf("unp_refs:\n");
220303c96c31SRobert Watson 	db_print_unprefs(2, &unp->unp_refs);
220403c96c31SRobert Watson 
220503c96c31SRobert Watson 	/* XXXRW: Would be nice to print the full address, if any. */
220603c96c31SRobert Watson 	db_printf("unp_addr: %p\n", unp->unp_addr);
220703c96c31SRobert Watson 
220803c96c31SRobert Watson 	db_printf("unp_cc: %d   unp_mbcnt: %d   unp_gencnt: %llu\n",
220903c96c31SRobert Watson 	    unp->unp_cc, unp->unp_mbcnt,
221003c96c31SRobert Watson 	    (unsigned long long)unp->unp_gencnt);
221103c96c31SRobert Watson 
221203c96c31SRobert Watson 	db_printf("unp_flags: %x (", unp->unp_flags);
221303c96c31SRobert Watson 	db_print_unpflags(unp->unp_flags);
221403c96c31SRobert Watson 	db_printf(")\n");
221503c96c31SRobert Watson 
221603c96c31SRobert Watson 	db_printf("unp_peercred:\n");
221703c96c31SRobert Watson 	db_print_xucred(2, &unp->unp_peercred);
221803c96c31SRobert Watson 
221903c96c31SRobert Watson 	db_printf("unp_refcount: %u\n", unp->unp_refcount);
222003c96c31SRobert Watson }
222103c96c31SRobert Watson #endif
2222