xref: /freebsd/sys/kern/uipc_usrreq.c (revision 3a20f06a1c390f8599f9fb7ad31fd9f145f9af6d)
19454b2d8SWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1993
5f344fb0bSWarner Losh  *	The Regents of the University of California. All Rights Reserved.
6f344fb0bSWarner Losh  * Copyright (c) 2004-2009 Robert N. M. Watson All Rights Reserved.
7c0874c34SMatt Macy  * Copyright (c) 2018 Matthew Macy
8df8bae1dSRodney W. Grimes  *
9df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
10df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
11df8bae1dSRodney W. Grimes  * are met:
12df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
14df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
15df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
16df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1769a28758SEd Maste  * 3. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33748e0b0aSGarrett Wollman  *	From: @(#)uipc_usrreq.c	8.3 (Berkeley) 1/4/94
34df8bae1dSRodney W. Grimes  */
35df8bae1dSRodney W. Grimes 
36f23929fbSRobert Watson /*
37f23929fbSRobert Watson  * UNIX Domain (Local) Sockets
38f23929fbSRobert Watson  *
39f23929fbSRobert Watson  * This is an implementation of UNIX (local) domain sockets.  Each socket has
40f23929fbSRobert Watson  * an associated struct unpcb (UNIX protocol control block).  Stream sockets
41f23929fbSRobert Watson  * may be connected to 0 or 1 other socket.  Datagram sockets may be
42f23929fbSRobert Watson  * connected to 0, 1, or many other sockets.  Sockets may be created and
43f23929fbSRobert Watson  * connected in pairs (socketpair(2)), or bound/connected to using the file
44f23929fbSRobert Watson  * system name space.  For most purposes, only the receive socket buffer is
45f23929fbSRobert Watson  * used, as sending on one socket delivers directly to the receive socket
465b950deaSRobert Watson  * buffer of a second socket.
475b950deaSRobert Watson  *
485b950deaSRobert Watson  * The implementation is substantially complicated by the fact that
495b950deaSRobert Watson  * "ancillary data", such as file descriptors or credentials, may be passed
505b950deaSRobert Watson  * across UNIX domain sockets.  The potential for passing UNIX domain sockets
515b950deaSRobert Watson  * over other UNIX domain sockets requires the implementation of a simple
525b950deaSRobert Watson  * garbage collector to find and tear down cycles of disconnected sockets.
53aea52f1bSRobert Watson  *
54aea52f1bSRobert Watson  * TODO:
5584d61770SRobert Watson  *	RDM
56aea52f1bSRobert Watson  *	rethink name space problems
57aea52f1bSRobert Watson  *	need a proper out-of-band
58f23929fbSRobert Watson  */
59f23929fbSRobert Watson 
60677b542eSDavid E. O'Brien #include <sys/cdefs.h>
61677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
62677b542eSDavid E. O'Brien 
6303c96c31SRobert Watson #include "opt_ddb.h"
64335654d7SRobert Watson 
65df8bae1dSRodney W. Grimes #include <sys/param.h>
664a144410SRobert Watson #include <sys/capsicum.h>
67fb919e4dSMark Murray #include <sys/domain.h>
68960ed29cSSeigo Tanimura #include <sys/fcntl.h>
69d826c479SBruce Evans #include <sys/malloc.h>		/* XXX must be before <sys/file.h> */
704f590175SPaul Saab #include <sys/eventhandler.h>
71639acc13SGarrett Wollman #include <sys/file.h>
72960ed29cSSeigo Tanimura #include <sys/filedesc.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>
810cb64678SKonstantin Belousov #include <sys/queue.h>
82960ed29cSSeigo Tanimura #include <sys/resourcevar.h>
83e7c33e29SRobert Watson #include <sys/rwlock.h>
84df8bae1dSRodney W. Grimes #include <sys/socket.h>
85df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
86960ed29cSSeigo Tanimura #include <sys/signalvar.h>
87df8bae1dSRodney W. Grimes #include <sys/stat.h>
88960ed29cSSeigo Tanimura #include <sys/sx.h>
89639acc13SGarrett Wollman #include <sys/sysctl.h>
90960ed29cSSeigo Tanimura #include <sys/systm.h>
91a0ec558aSRobert Watson #include <sys/taskqueue.h>
92639acc13SGarrett Wollman #include <sys/un.h>
9398271db4SGarrett Wollman #include <sys/unpcb.h>
94639acc13SGarrett Wollman #include <sys/vnode.h>
95530c0060SRobert Watson 
96530c0060SRobert Watson #include <net/vnet.h>
97df8bae1dSRodney W. Grimes 
9803c96c31SRobert Watson #ifdef DDB
9903c96c31SRobert Watson #include <ddb/ddb.h>
10003c96c31SRobert Watson #endif
10103c96c31SRobert Watson 
102aed55708SRobert Watson #include <security/mac/mac_framework.h>
103aed55708SRobert Watson 
1049e9d298aSJeff Roberson #include <vm/uma.h>
10598271db4SGarrett Wollman 
1068cb539f1SPawel Jakub Dawidek MALLOC_DECLARE(M_FILECAPS);
1078cb539f1SPawel Jakub Dawidek 
1083dab55bcSRobert Watson /*
1093dab55bcSRobert Watson  * Locking key:
1103dab55bcSRobert Watson  * (l)	Locked using list lock
1113dab55bcSRobert Watson  * (g)	Locked using linkage lock
1123dab55bcSRobert Watson  */
1133dab55bcSRobert Watson 
1149e9d298aSJeff Roberson static uma_zone_t	unp_zone;
1153dab55bcSRobert Watson static unp_gen_t	unp_gencnt;	/* (l) */
1163dab55bcSRobert Watson static u_int		unp_count;	/* (l) Count of local sockets. */
117aea52f1bSRobert Watson static ino_t		unp_ino;	/* Prototype for fake inode numbers. */
1183dab55bcSRobert Watson static int		unp_rights;	/* (g) File descriptors in flight. */
1193dab55bcSRobert Watson static struct unp_head	unp_shead;	/* (l) List of stream sockets. */
1203dab55bcSRobert Watson static struct unp_head	unp_dhead;	/* (l) List of datagram sockets. */
12184d61770SRobert Watson static struct unp_head	unp_sphead;	/* (l) List of seqpacket sockets. */
12298271db4SGarrett Wollman 
1230cb64678SKonstantin Belousov struct unp_defer {
1240cb64678SKonstantin Belousov 	SLIST_ENTRY(unp_defer) ud_link;
1250cb64678SKonstantin Belousov 	struct file *ud_fp;
1260cb64678SKonstantin Belousov };
1270cb64678SKonstantin Belousov static SLIST_HEAD(, unp_defer) unp_defers;
1280cb64678SKonstantin Belousov static int unp_defers_count;
1290cb64678SKonstantin Belousov 
130aea52f1bSRobert Watson static const struct sockaddr	sun_noname = { sizeof(sun_noname), AF_LOCAL };
13198271db4SGarrett Wollman 
132df8bae1dSRodney W. Grimes /*
133aea52f1bSRobert Watson  * Garbage collection of cyclic file descriptor/socket references occurs
134aea52f1bSRobert Watson  * asynchronously in a taskqueue context in order to avoid recursion and
135aea52f1bSRobert Watson  * reentrance in the UNIX domain socket, file descriptor, and socket layer
136aea52f1bSRobert Watson  * code.  See unp_gc() for a full description.
137df8bae1dSRodney W. Grimes  */
138daee0f0bSKonstantin Belousov static struct timeout_task unp_gc_task;
139f708ef1bSPoul-Henning Kamp 
140ce5f32deSRobert Watson /*
1410cb64678SKonstantin Belousov  * The close of unix domain sockets attached as SCM_RIGHTS is
1420cb64678SKonstantin Belousov  * postponed to the taskqueue, to avoid arbitrary recursion depth.
1430cb64678SKonstantin Belousov  * The attached sockets might have another sockets attached.
1440cb64678SKonstantin Belousov  */
1450cb64678SKonstantin Belousov static struct task	unp_defer_task;
1460cb64678SKonstantin Belousov 
1470cb64678SKonstantin Belousov /*
1487e711c3aSRobert Watson  * Both send and receive buffers are allocated PIPSIZ bytes of buffering for
1497e711c3aSRobert Watson  * stream sockets, although the total for sender and receiver is actually
1507e711c3aSRobert Watson  * only PIPSIZ.
1517e711c3aSRobert Watson  *
1527e711c3aSRobert Watson  * Datagram sockets really use the sendspace as the maximum datagram size,
1537e711c3aSRobert Watson  * and don't really want to reserve the sendspace.  Their recvspace should be
1547e711c3aSRobert Watson  * large enough for at least one max-size datagram plus address.
1557e711c3aSRobert Watson  */
1567e711c3aSRobert Watson #ifndef PIPSIZ
1577e711c3aSRobert Watson #define	PIPSIZ	8192
1587e711c3aSRobert Watson #endif
1597e711c3aSRobert Watson static u_long	unpst_sendspace = PIPSIZ;
1607e711c3aSRobert Watson static u_long	unpst_recvspace = PIPSIZ;
1617e711c3aSRobert Watson static u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
1627e711c3aSRobert Watson static u_long	unpdg_recvspace = 4*1024;
16384d61770SRobert Watson static u_long	unpsp_sendspace = PIPSIZ;	/* really max datagram size */
16484d61770SRobert Watson static u_long	unpsp_recvspace = PIPSIZ;
1657e711c3aSRobert Watson 
1666472ac3dSEd Schouten static SYSCTL_NODE(_net, PF_LOCAL, local, CTLFLAG_RW, 0, "Local domain");
1676472ac3dSEd Schouten static SYSCTL_NODE(_net_local, SOCK_STREAM, stream, CTLFLAG_RW, 0,
1686472ac3dSEd Schouten     "SOCK_STREAM");
1696472ac3dSEd Schouten static SYSCTL_NODE(_net_local, SOCK_DGRAM, dgram, CTLFLAG_RW, 0, "SOCK_DGRAM");
1706472ac3dSEd Schouten static SYSCTL_NODE(_net_local, SOCK_SEQPACKET, seqpacket, CTLFLAG_RW, 0,
17184d61770SRobert Watson     "SOCK_SEQPACKET");
172e4445a03SRobert Watson 
1737e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
174be6b1304STom Rhodes 	   &unpst_sendspace, 0, "Default stream send space.");
1757e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
176be6b1304STom Rhodes 	   &unpst_recvspace, 0, "Default stream receive space.");
1777e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
178be6b1304STom Rhodes 	   &unpdg_sendspace, 0, "Default datagram send space.");
1797e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
180be6b1304STom Rhodes 	   &unpdg_recvspace, 0, "Default datagram receive space.");
18184d61770SRobert Watson SYSCTL_ULONG(_net_local_seqpacket, OID_AUTO, maxseqpacket, CTLFLAG_RW,
18284d61770SRobert Watson 	   &unpsp_sendspace, 0, "Default seqpacket send space.");
18384d61770SRobert Watson SYSCTL_ULONG(_net_local_seqpacket, OID_AUTO, recvspace, CTLFLAG_RW,
18484d61770SRobert Watson 	   &unpsp_recvspace, 0, "Default seqpacket receive space.");
185be6b1304STom Rhodes SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0,
186be6b1304STom Rhodes     "File descriptors in flight.");
1870cb64678SKonstantin Belousov SYSCTL_INT(_net_local, OID_AUTO, deferred, CTLFLAG_RD,
1880cb64678SKonstantin Belousov     &unp_defers_count, 0,
1890cb64678SKonstantin Belousov     "File descriptors deferred to taskqueue for close.");
1907e711c3aSRobert Watson 
191175389cfSEdward Tomasz Napierala /*
192e7c33e29SRobert Watson  * Locking and synchronization:
193ce5f32deSRobert Watson  *
19475a67bf3SMatt Macy  * Three types of locks exist in the local domain socket implementation: a
19575a67bf3SMatt Macy  * a global linkage rwlock, the mtxpool lock, and per-unpcb mutexes.
19675a67bf3SMatt Macy  * The linkage lock protects the socket count, global generation number,
19775a67bf3SMatt Macy  * and stream/datagram global lists.
19875a67bf3SMatt Macy  *
19975a67bf3SMatt Macy  * The mtxpool lock protects the vnode from being modified while referenced.
20075a67bf3SMatt Macy  * Lock ordering requires that it be acquired before any unpcb locks.
20175a67bf3SMatt Macy  *
20275a67bf3SMatt Macy  * The unpcb lock (unp_mtx) protects all fields in the unpcb. Of particular
20375a67bf3SMatt Macy  * note is that this includes the unp_conn field. So long as the unpcb lock
20475a67bf3SMatt Macy  * is held the reference to the unpcb pointed to by unp_conn is valid. If we
20575a67bf3SMatt Macy  * require that the unpcb pointed to by unp_conn remain live in cases where
20675a67bf3SMatt Macy  * we need to drop the unp_mtx as when we need to acquire the lock for a
20775a67bf3SMatt Macy  * second unpcb the caller must first acquire an additional reference on the
20875a67bf3SMatt Macy  * second unpcb and then revalidate any state (typically check that unp_conn
20975a67bf3SMatt Macy  * is non-NULL) upon requiring the initial unpcb lock. The lock ordering
21075a67bf3SMatt Macy  * between unpcbs is the conventional ascending address order. Two helper
21175a67bf3SMatt Macy  * routines exist for this:
21275a67bf3SMatt Macy  *
21375a67bf3SMatt Macy  *   - unp_pcb_lock2(unp, unp2) - which just acquires the two locks in the
21475a67bf3SMatt Macy  *     safe ordering.
21575a67bf3SMatt Macy  *
21675a67bf3SMatt Macy  *   - unp_pcb_owned_lock2(unp, unp2, freed) - the lock for unp is held
21775a67bf3SMatt Macy  *     when called. If unp is unlocked and unp2 is subsequently freed
21875a67bf3SMatt Macy  *     freed will be set to 1.
21975a67bf3SMatt Macy  *
22075a67bf3SMatt Macy  * The helper routines for references are:
22175a67bf3SMatt Macy  *
22275a67bf3SMatt Macy  *   - unp_pcb_hold(unp): Can be called any time we currently hold a valid
22375a67bf3SMatt Macy  *     reference to unp.
22475a67bf3SMatt Macy  *
22575a67bf3SMatt Macy  *    - unp_pcb_rele(unp): The caller must hold the unp lock. If we are
22675a67bf3SMatt Macy  *      releasing the last reference, detach must have been called thus
22775a67bf3SMatt Macy  *      unp->unp_socket be NULL.
228ce5f32deSRobert Watson  *
229e7c33e29SRobert Watson  * UNIX domain sockets each have an unpcb hung off of their so_pcb pointer,
230e7c33e29SRobert Watson  * allocated in pru_attach() and freed in pru_detach().  The validity of that
231e7c33e29SRobert Watson  * pointer is an invariant, so no lock is required to dereference the so_pcb
232e7c33e29SRobert Watson  * pointer if a valid socket reference is held by the caller.  In practice,
233e7c33e29SRobert Watson  * this is always true during operations performed on a socket.  Each unpcb
234e7c33e29SRobert Watson  * has a back-pointer to its socket, unp_socket, which will be stable under
235e7c33e29SRobert Watson  * the same circumstances.
236e7c33e29SRobert Watson  *
237e7c33e29SRobert Watson  * This pointer may only be safely dereferenced as long as a valid reference
238e7c33e29SRobert Watson  * to the unpcb is held.  Typically, this reference will be from the socket,
239e7c33e29SRobert Watson  * or from another unpcb when the referring unpcb's lock is held (in order
240e7c33e29SRobert Watson  * that the reference not be invalidated during use).  For example, to follow
24175a67bf3SMatt Macy  * unp->unp_conn->unp_socket, you need to hold a lock on unp_conn to guarantee
24275a67bf3SMatt Macy  * that detach is not run clearing unp_socket.
243e7c33e29SRobert Watson  *
244e7c33e29SRobert Watson  * Blocking with UNIX domain sockets is a tricky issue: unlike most network
245e7c33e29SRobert Watson  * protocols, bind() is a non-atomic operation, and connect() requires
246e7c33e29SRobert Watson  * potential sleeping in the protocol, due to potentially waiting on local or
247e7c33e29SRobert Watson  * distributed file systems.  We try to separate "lookup" operations, which
248e7c33e29SRobert Watson  * may sleep, and the IPC operations themselves, which typically can occur
249e7c33e29SRobert Watson  * with relative atomicity as locks can be held over the entire operation.
250e7c33e29SRobert Watson  *
251e7c33e29SRobert Watson  * Another tricky issue is simultaneous multi-threaded or multi-process
252e7c33e29SRobert Watson  * access to a single UNIX domain socket.  These are handled by the flags
253e7c33e29SRobert Watson  * UNP_CONNECTING and UNP_BINDING, which prevent concurrent connecting or
254e7c33e29SRobert Watson  * binding, both of which involve dropping UNIX domain socket locks in order
255e7c33e29SRobert Watson  * to perform namei() and other file system operations.
256ce5f32deSRobert Watson  */
2573dab55bcSRobert Watson static struct rwlock	unp_link_rwlock;
2580cb64678SKonstantin Belousov static struct mtx	unp_defers_lock;
259e7c33e29SRobert Watson 
2603dab55bcSRobert Watson #define	UNP_LINK_LOCK_INIT()		rw_init(&unp_link_rwlock,	\
2613dab55bcSRobert Watson 					    "unp_link_rwlock")
262e7c33e29SRobert Watson 
2633dab55bcSRobert Watson #define	UNP_LINK_LOCK_ASSERT()	rw_assert(&unp_link_rwlock,	\
264e7c33e29SRobert Watson 					    RA_LOCKED)
2653dab55bcSRobert Watson #define	UNP_LINK_UNLOCK_ASSERT()	rw_assert(&unp_link_rwlock,	\
266e7c33e29SRobert Watson 					    RA_UNLOCKED)
267e7c33e29SRobert Watson 
2683dab55bcSRobert Watson #define	UNP_LINK_RLOCK()		rw_rlock(&unp_link_rwlock)
2693dab55bcSRobert Watson #define	UNP_LINK_RUNLOCK()		rw_runlock(&unp_link_rwlock)
2703dab55bcSRobert Watson #define	UNP_LINK_WLOCK()		rw_wlock(&unp_link_rwlock)
2713dab55bcSRobert Watson #define	UNP_LINK_WUNLOCK()		rw_wunlock(&unp_link_rwlock)
2723dab55bcSRobert Watson #define	UNP_LINK_WLOCK_ASSERT()		rw_assert(&unp_link_rwlock,	\
273e7c33e29SRobert Watson 					    RA_WLOCKED)
274779f106aSGleb Smirnoff #define	UNP_LINK_WOWNED()		rw_wowned(&unp_link_rwlock)
275e7c33e29SRobert Watson 
2760cb64678SKonstantin Belousov #define	UNP_DEFERRED_LOCK_INIT()	mtx_init(&unp_defers_lock, \
2770cb64678SKonstantin Belousov 					    "unp_defer", NULL, MTX_DEF)
2780cb64678SKonstantin Belousov #define	UNP_DEFERRED_LOCK()		mtx_lock(&unp_defers_lock)
2790cb64678SKonstantin Belousov #define	UNP_DEFERRED_UNLOCK()		mtx_unlock(&unp_defers_lock)
2800cb64678SKonstantin Belousov 
28175a67bf3SMatt Macy #define UNP_REF_LIST_LOCK()		UNP_DEFERRED_LOCK();
28275a67bf3SMatt Macy #define UNP_REF_LIST_UNLOCK()		UNP_DEFERRED_UNLOCK();
28375a67bf3SMatt Macy 
284e7c33e29SRobert Watson #define UNP_PCB_LOCK_INIT(unp)		mtx_init(&(unp)->unp_mtx,	\
285d9525340SMatt Macy 					    "unp", "unp",	\
28675a67bf3SMatt Macy 					    MTX_DUPOK|MTX_DEF)
287e7c33e29SRobert Watson #define	UNP_PCB_LOCK_DESTROY(unp)	mtx_destroy(&(unp)->unp_mtx)
288e7c33e29SRobert Watson #define	UNP_PCB_LOCK(unp)		mtx_lock(&(unp)->unp_mtx)
28975a67bf3SMatt Macy #define	UNP_PCB_TRYLOCK(unp)		mtx_trylock(&(unp)->unp_mtx)
290e7c33e29SRobert Watson #define	UNP_PCB_UNLOCK(unp)		mtx_unlock(&(unp)->unp_mtx)
29175a67bf3SMatt Macy #define	UNP_PCB_OWNED(unp)		mtx_owned(&(unp)->unp_mtx)
292e7c33e29SRobert Watson #define	UNP_PCB_LOCK_ASSERT(unp)	mtx_assert(&(unp)->unp_mtx, MA_OWNED)
29375a67bf3SMatt Macy #define	UNP_PCB_UNLOCK_ASSERT(unp)	mtx_assert(&(unp)->unp_mtx, MA_NOTOWNED)
2940d9ce3a1SRobert Watson 
2952c899584SRobert Watson static int	uipc_connect2(struct socket *, struct socket *);
2960b36cd25SRobert Watson static int	uipc_ctloutput(struct socket *, struct sockopt *);
297aea52f1bSRobert Watson static int	unp_connect(struct socket *, struct sockaddr *,
298aea52f1bSRobert Watson 		    struct thread *);
2997493f24eSPawel Jakub Dawidek static int	unp_connectat(int, struct socket *, struct sockaddr *,
3007493f24eSPawel Jakub Dawidek 		    struct thread *);
3016a2989fdSMatthew N. Dodd static int	unp_connect2(struct socket *so, struct socket *so2, int);
302e7c33e29SRobert Watson static void	unp_disconnect(struct unpcb *unp, struct unpcb *unp2);
30399ab95dbSMark Johnston static void	unp_dispose(struct socket *so);
30499ab95dbSMark Johnston static void	unp_dispose_mbuf(struct mbuf *);
3054d77a549SAlfred Perlstein static void	unp_shutdown(struct unpcb *);
306afc055d9SEd Schouten static void	unp_drop(struct unpcb *);
307a0ec558aSRobert Watson static void	unp_gc(__unused void *, int);
308be26ba7cSPawel Jakub Dawidek static void	unp_scan(struct mbuf *, void (*)(struct filedescent **, int));
3094d77a549SAlfred Perlstein static void	unp_discard(struct file *);
3108cb539f1SPawel Jakub Dawidek static void	unp_freerights(struct filedescent **, int);
3110b36cd25SRobert Watson static void	unp_init(void);
3124d77a549SAlfred Perlstein static int	unp_internalize(struct mbuf **, struct thread *);
313397c19d1SJeff Roberson static void	unp_internalize_fp(struct file *);
314c2e3c52eSJilles Tjoelker static int	unp_externalize(struct mbuf *, struct mbuf **, int);
3150cb64678SKonstantin Belousov static int	unp_externalize_fp(struct file *);
3165b950deaSRobert Watson static struct mbuf	*unp_addsockcred(struct thread *, struct mbuf *);
3170cb64678SKonstantin Belousov static void	unp_process_defers(void * __unused, int);
318f708ef1bSPoul-Henning Kamp 
31975a67bf3SMatt Macy 
32075a67bf3SMatt Macy static void
32175a67bf3SMatt Macy unp_pcb_hold(struct unpcb *unp)
32275a67bf3SMatt Macy {
32375a67bf3SMatt Macy 	MPASS(unp->unp_refcount);
32475a67bf3SMatt Macy 	refcount_acquire(&unp->unp_refcount);
32575a67bf3SMatt Macy }
32675a67bf3SMatt Macy 
32775a67bf3SMatt Macy static int
32875a67bf3SMatt Macy unp_pcb_rele(struct unpcb *unp)
32975a67bf3SMatt Macy {
33075a67bf3SMatt Macy 	int freed;
33175a67bf3SMatt Macy 
33275a67bf3SMatt Macy 	UNP_PCB_LOCK_ASSERT(unp);
33375a67bf3SMatt Macy 	MPASS(unp->unp_refcount);
33475a67bf3SMatt Macy 	if ((freed = refcount_release(&unp->unp_refcount))) {
33575a67bf3SMatt Macy 		/* we got here with having detached? */
33675a67bf3SMatt Macy 		MPASS(unp->unp_socket == NULL);
33775a67bf3SMatt Macy 		UNP_PCB_UNLOCK(unp);
33875a67bf3SMatt Macy 		UNP_PCB_LOCK_DESTROY(unp);
33975a67bf3SMatt Macy 		uma_zfree(unp_zone, unp);
34075a67bf3SMatt Macy 	}
34175a67bf3SMatt Macy 	return (freed);
34275a67bf3SMatt Macy }
34375a67bf3SMatt Macy 
34475a67bf3SMatt Macy static void
34575a67bf3SMatt Macy unp_pcb_lock2(struct unpcb *unp, struct unpcb *unp2)
34675a67bf3SMatt Macy {
34716529dacSMatt Macy 	MPASS(unp != unp2);
34875a67bf3SMatt Macy 	UNP_PCB_UNLOCK_ASSERT(unp);
34975a67bf3SMatt Macy 	UNP_PCB_UNLOCK_ASSERT(unp2);
35075a67bf3SMatt Macy 	if ((uintptr_t)unp2 > (uintptr_t)unp) {
35175a67bf3SMatt Macy 		UNP_PCB_LOCK(unp);
35275a67bf3SMatt Macy 		UNP_PCB_LOCK(unp2);
35375a67bf3SMatt Macy 	} else {
35475a67bf3SMatt Macy 		UNP_PCB_LOCK(unp2);
35575a67bf3SMatt Macy 		UNP_PCB_LOCK(unp);
35675a67bf3SMatt Macy 	}
35775a67bf3SMatt Macy }
35875a67bf3SMatt Macy 
35975a67bf3SMatt Macy static __noinline void
36075a67bf3SMatt Macy unp_pcb_owned_lock2_slowpath(struct unpcb *unp, struct unpcb **unp2p, int *freed)
36175a67bf3SMatt Macy 
36275a67bf3SMatt Macy {
36375a67bf3SMatt Macy 	struct unpcb *unp2;
36475a67bf3SMatt Macy 
36575a67bf3SMatt Macy 	unp2 = *unp2p;
36675a67bf3SMatt Macy 	unp_pcb_hold((unp2));
36775a67bf3SMatt Macy 	UNP_PCB_UNLOCK((unp));
36875a67bf3SMatt Macy 	UNP_PCB_LOCK((unp2));
36975a67bf3SMatt Macy 	UNP_PCB_LOCK((unp));
37075a67bf3SMatt Macy 	*freed = unp_pcb_rele((unp2));
37175a67bf3SMatt Macy 	if (*freed)
37275a67bf3SMatt Macy 		*unp2p = NULL;
37375a67bf3SMatt Macy }
37475a67bf3SMatt Macy 
37575a67bf3SMatt Macy #define unp_pcb_owned_lock2(unp, unp2, freed) do {					\
37675a67bf3SMatt Macy 		freed = 0;													\
37775a67bf3SMatt Macy 		UNP_PCB_LOCK_ASSERT((unp));									\
37875a67bf3SMatt Macy 		UNP_PCB_UNLOCK_ASSERT((unp2));								\
37916529dacSMatt Macy 		MPASS(unp != unp2);											\
38075a67bf3SMatt Macy 		if (__predict_true(UNP_PCB_TRYLOCK((unp2))))				\
38175a67bf3SMatt Macy 			break;													\
38275a67bf3SMatt Macy 		else if ((uintptr_t)(unp2) > (uintptr_t)(unp))				\
38375a67bf3SMatt Macy 			UNP_PCB_LOCK((unp2));									\
38475a67bf3SMatt Macy 		else {														\
38575a67bf3SMatt Macy 			unp_pcb_owned_lock2_slowpath((unp), &(unp2), &freed);	\
38675a67bf3SMatt Macy 		}															\
38775a67bf3SMatt Macy } while (0)
38875a67bf3SMatt Macy 
38975a67bf3SMatt Macy 
390e4445a03SRobert Watson /*
391e4445a03SRobert Watson  * Definitions of protocols supported in the LOCAL domain.
392e4445a03SRobert Watson  */
393e4445a03SRobert Watson static struct domain localdomain;
394fa9402f2SRobert Watson static struct pr_usrreqs uipc_usrreqs_dgram, uipc_usrreqs_stream;
39584d61770SRobert Watson static struct pr_usrreqs uipc_usrreqs_seqpacket;
396e4445a03SRobert Watson static struct protosw localsw[] = {
397e4445a03SRobert Watson {
398e4445a03SRobert Watson 	.pr_type =		SOCK_STREAM,
399e4445a03SRobert Watson 	.pr_domain =		&localdomain,
400e4445a03SRobert Watson 	.pr_flags =		PR_CONNREQUIRED|PR_WANTRCVD|PR_RIGHTS,
401e4445a03SRobert Watson 	.pr_ctloutput =		&uipc_ctloutput,
402fa9402f2SRobert Watson 	.pr_usrreqs =		&uipc_usrreqs_stream
403e4445a03SRobert Watson },
404e4445a03SRobert Watson {
405e4445a03SRobert Watson 	.pr_type =		SOCK_DGRAM,
406e4445a03SRobert Watson 	.pr_domain =		&localdomain,
407e4445a03SRobert Watson 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_RIGHTS,
408aaf63435SGleb Smirnoff 	.pr_ctloutput =		&uipc_ctloutput,
409fa9402f2SRobert Watson 	.pr_usrreqs =		&uipc_usrreqs_dgram
410e4445a03SRobert Watson },
41184d61770SRobert Watson {
41284d61770SRobert Watson 	.pr_type =		SOCK_SEQPACKET,
41384d61770SRobert Watson 	.pr_domain =		&localdomain,
41484d61770SRobert Watson 
41584d61770SRobert Watson 	/*
41684d61770SRobert Watson 	 * XXXRW: For now, PR_ADDR because soreceive will bump into them
41784d61770SRobert Watson 	 * due to our use of sbappendaddr.  A new sbappend variants is needed
41884d61770SRobert Watson 	 * that supports both atomic record writes and control data.
41984d61770SRobert Watson 	 */
42084d61770SRobert Watson 	.pr_flags =		PR_ADDR|PR_ATOMIC|PR_CONNREQUIRED|PR_WANTRCVD|
42184d61770SRobert Watson 				    PR_RIGHTS,
422e0643280SGleb Smirnoff 	.pr_ctloutput =		&uipc_ctloutput,
42384d61770SRobert Watson 	.pr_usrreqs =		&uipc_usrreqs_seqpacket,
42484d61770SRobert Watson },
425e4445a03SRobert Watson };
426e4445a03SRobert Watson 
427e4445a03SRobert Watson static struct domain localdomain = {
428e4445a03SRobert Watson 	.dom_family =		AF_LOCAL,
429e4445a03SRobert Watson 	.dom_name =		"local",
430e4445a03SRobert Watson 	.dom_init =		unp_init,
431e4445a03SRobert Watson 	.dom_externalize =	unp_externalize,
43299ab95dbSMark Johnston 	.dom_dispose =		unp_dispose,
433e4445a03SRobert Watson 	.dom_protosw =		localsw,
43402abd400SPedro F. Giffuni 	.dom_protoswNPROTOSW =	&localsw[nitems(localsw)]
435e4445a03SRobert Watson };
436e4445a03SRobert Watson DOMAIN_SET(local);
437e4445a03SRobert Watson 
438ac45e92fSRobert Watson static void
439a29f300eSGarrett Wollman uipc_abort(struct socket *so)
440df8bae1dSRodney W. Grimes {
441e7c33e29SRobert Watson 	struct unpcb *unp, *unp2;
442df8bae1dSRodney W. Grimes 
44340f2ac28SRobert Watson 	unp = sotounpcb(so);
4444d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_abort: unp == NULL"));
44575a67bf3SMatt Macy 	UNP_PCB_UNLOCK_ASSERT(unp);
446e7c33e29SRobert Watson 
447e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
448e7c33e29SRobert Watson 	unp2 = unp->unp_conn;
449e7c33e29SRobert Watson 	if (unp2 != NULL) {
45075a67bf3SMatt Macy 		unp_pcb_hold(unp2);
451e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
45275a67bf3SMatt Macy 		unp_drop(unp2);
45375a67bf3SMatt Macy 	} else
45475a67bf3SMatt Macy 		UNP_PCB_UNLOCK(unp);
455df8bae1dSRodney W. Grimes }
456df8bae1dSRodney W. Grimes 
457a29f300eSGarrett Wollman static int
45857bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam)
459a29f300eSGarrett Wollman {
460e7c33e29SRobert Watson 	struct unpcb *unp, *unp2;
4610d9ce3a1SRobert Watson 	const struct sockaddr *sa;
462df8bae1dSRodney W. Grimes 
463df8bae1dSRodney W. Grimes 	/*
4641c381b19SRobert Watson 	 * Pass back name of connected socket, if it was bound and we are
4651c381b19SRobert Watson 	 * still connected (our peer may have closed already!).
466df8bae1dSRodney W. Grimes 	 */
4674d4b555eSRobert Watson 	unp = sotounpcb(so);
4684d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_accept: unp == NULL"));
469e7c33e29SRobert Watson 
4700d9ce3a1SRobert Watson 	*nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
4713dab55bcSRobert Watson 	UNP_LINK_RLOCK();
472e7c33e29SRobert Watson 	unp2 = unp->unp_conn;
473e7c33e29SRobert Watson 	if (unp2 != NULL && unp2->unp_addr != NULL) {
474e7c33e29SRobert Watson 		UNP_PCB_LOCK(unp2);
475e7c33e29SRobert Watson 		sa = (struct sockaddr *) unp2->unp_addr;
476e7c33e29SRobert Watson 		bcopy(sa, *nam, sa->sa_len);
477e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp2);
478e7c33e29SRobert Watson 	} else {
4790d9ce3a1SRobert Watson 		sa = &sun_noname;
4800d9ce3a1SRobert Watson 		bcopy(sa, *nam, sa->sa_len);
481e7c33e29SRobert Watson 	}
4823dab55bcSRobert Watson 	UNP_LINK_RUNLOCK();
483e5aeaa0cSDag-Erling Smørgrav 	return (0);
484a29f300eSGarrett Wollman }
485df8bae1dSRodney W. Grimes 
486a29f300eSGarrett Wollman static int
487b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td)
488a29f300eSGarrett Wollman {
489e7c33e29SRobert Watson 	u_long sendspace, recvspace;
4906d32873cSRobert Watson 	struct unpcb *unp;
4913dab55bcSRobert Watson 	int error;
492779f106aSGleb Smirnoff 	bool locked;
493df8bae1dSRodney W. Grimes 
4946d32873cSRobert Watson 	KASSERT(so->so_pcb == NULL, ("uipc_attach: so_pcb != NULL"));
4956d32873cSRobert Watson 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
4966d32873cSRobert Watson 		switch (so->so_type) {
4976d32873cSRobert Watson 		case SOCK_STREAM:
498e7c33e29SRobert Watson 			sendspace = unpst_sendspace;
499e7c33e29SRobert Watson 			recvspace = unpst_recvspace;
5006d32873cSRobert Watson 			break;
5016d32873cSRobert Watson 
5026d32873cSRobert Watson 		case SOCK_DGRAM:
503e7c33e29SRobert Watson 			sendspace = unpdg_sendspace;
504e7c33e29SRobert Watson 			recvspace = unpdg_recvspace;
5056d32873cSRobert Watson 			break;
5066d32873cSRobert Watson 
50784d61770SRobert Watson 		case SOCK_SEQPACKET:
50884d61770SRobert Watson 			sendspace = unpsp_sendspace;
50984d61770SRobert Watson 			recvspace = unpsp_recvspace;
51084d61770SRobert Watson 			break;
51184d61770SRobert Watson 
5126d32873cSRobert Watson 		default:
513e7c33e29SRobert Watson 			panic("uipc_attach");
5146d32873cSRobert Watson 		}
515e7c33e29SRobert Watson 		error = soreserve(so, sendspace, recvspace);
5166d32873cSRobert Watson 		if (error)
5176d32873cSRobert Watson 			return (error);
5186d32873cSRobert Watson 	}
51946a1d9bfSRobert Watson 	unp = uma_zalloc(unp_zone, M_NOWAIT | M_ZERO);
5206d32873cSRobert Watson 	if (unp == NULL)
5216d32873cSRobert Watson 		return (ENOBUFS);
5226d32873cSRobert Watson 	LIST_INIT(&unp->unp_refs);
523e7c33e29SRobert Watson 	UNP_PCB_LOCK_INIT(unp);
5246d32873cSRobert Watson 	unp->unp_socket = so;
5256d32873cSRobert Watson 	so->so_pcb = unp;
5269ae328fcSJohn Baldwin 	unp->unp_refcount = 1;
527779f106aSGleb Smirnoff 	if (so->so_listen != NULL)
528434ac8b6SMark Johnston 		unp->unp_flags |= UNP_NASCENT;
529e7c33e29SRobert Watson 
530779f106aSGleb Smirnoff 	if ((locked = UNP_LINK_WOWNED()) == false)
531779f106aSGleb Smirnoff 		UNP_LINK_WLOCK();
532779f106aSGleb Smirnoff 
5336d32873cSRobert Watson 	unp->unp_gencnt = ++unp_gencnt;
5346d32873cSRobert Watson 	unp_count++;
53584d61770SRobert Watson 	switch (so->so_type) {
53684d61770SRobert Watson 	case SOCK_STREAM:
53784d61770SRobert Watson 		LIST_INSERT_HEAD(&unp_shead, unp, unp_link);
53884d61770SRobert Watson 		break;
53984d61770SRobert Watson 
54084d61770SRobert Watson 	case SOCK_DGRAM:
54184d61770SRobert Watson 		LIST_INSERT_HEAD(&unp_dhead, unp, unp_link);
54284d61770SRobert Watson 		break;
54384d61770SRobert Watson 
54484d61770SRobert Watson 	case SOCK_SEQPACKET:
54584d61770SRobert Watson 		LIST_INSERT_HEAD(&unp_sphead, unp, unp_link);
54684d61770SRobert Watson 		break;
54784d61770SRobert Watson 
54884d61770SRobert Watson 	default:
54984d61770SRobert Watson 		panic("uipc_attach");
55084d61770SRobert Watson 	}
551779f106aSGleb Smirnoff 
552779f106aSGleb Smirnoff 	if (locked == false)
553779f106aSGleb Smirnoff 		UNP_LINK_WUNLOCK();
5546d32873cSRobert Watson 
5556d32873cSRobert Watson 	return (0);
556a29f300eSGarrett Wollman }
557a29f300eSGarrett Wollman 
558a29f300eSGarrett Wollman static int
5597493f24eSPawel Jakub Dawidek uipc_bindat(int fd, struct socket *so, struct sockaddr *nam, struct thread *td)
560a29f300eSGarrett Wollman {
561dd47f5caSRobert Watson 	struct sockaddr_un *soun = (struct sockaddr_un *)nam;
562dd47f5caSRobert Watson 	struct vattr vattr;
5635050aa86SKonstantin Belousov 	int error, namelen;
564dd47f5caSRobert Watson 	struct nameidata nd;
56540f2ac28SRobert Watson 	struct unpcb *unp;
566dd47f5caSRobert Watson 	struct vnode *vp;
567dd47f5caSRobert Watson 	struct mount *mp;
5687008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
569dd47f5caSRobert Watson 	char *buf;
570a29f300eSGarrett Wollman 
571cb7df69bSKevin Lo 	if (nam->sa_family != AF_UNIX)
572cb7df69bSKevin Lo 		return (EAFNOSUPPORT);
573cb7df69bSKevin Lo 
57440f2ac28SRobert Watson 	unp = sotounpcb(so);
5754d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_bind: unp == NULL"));
5764f1f0ef5SRobert Watson 
577a06534c3SBjoern A. Zeeb 	if (soun->sun_len > sizeof(struct sockaddr_un))
578a06534c3SBjoern A. Zeeb 		return (EINVAL);
5794f1f0ef5SRobert Watson 	namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
5804f1f0ef5SRobert Watson 	if (namelen <= 0)
5814f1f0ef5SRobert Watson 		return (EINVAL);
582dd47f5caSRobert Watson 
583dd47f5caSRobert Watson 	/*
5844f1f0ef5SRobert Watson 	 * We don't allow simultaneous bind() calls on a single UNIX domain
5854f1f0ef5SRobert Watson 	 * socket, so flag in-progress operations, and return an error if an
5864f1f0ef5SRobert Watson 	 * operation is already in progress.
5874f1f0ef5SRobert Watson 	 *
5884f1f0ef5SRobert Watson 	 * Historically, we have not allowed a socket to be rebound, so this
589d7924b70SRobert Watson 	 * also returns an error.  Not allowing re-binding simplifies the
590d7924b70SRobert Watson 	 * implementation and avoids a great many possible failure modes.
591dd47f5caSRobert Watson 	 */
592e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
593dd47f5caSRobert Watson 	if (unp->unp_vnode != NULL) {
594e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
595dd47f5caSRobert Watson 		return (EINVAL);
596dd47f5caSRobert Watson 	}
5974f1f0ef5SRobert Watson 	if (unp->unp_flags & UNP_BINDING) {
598e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
5994f1f0ef5SRobert Watson 		return (EALREADY);
600dd47f5caSRobert Watson 	}
6014f1f0ef5SRobert Watson 	unp->unp_flags |= UNP_BINDING;
602e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
603dd47f5caSRobert Watson 
604dd47f5caSRobert Watson 	buf = malloc(namelen + 1, M_TEMP, M_WAITOK);
6057928893dSEd Maste 	bcopy(soun->sun_path, buf, namelen);
6067928893dSEd Maste 	buf[namelen] = 0;
607dd47f5caSRobert Watson 
608dd47f5caSRobert Watson restart:
6096c21f6edSKonstantin Belousov 	NDINIT_ATRIGHTS(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME | NOCACHE,
6107008be5bSPawel Jakub Dawidek 	    UIO_SYSSPACE, buf, fd, cap_rights_init(&rights, CAP_BINDAT), td);
611dd47f5caSRobert Watson /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
612dd47f5caSRobert Watson 	error = namei(&nd);
613dd47f5caSRobert Watson 	if (error)
6144f1f0ef5SRobert Watson 		goto error;
615dd47f5caSRobert Watson 	vp = nd.ni_vp;
616dd47f5caSRobert Watson 	if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
617dd47f5caSRobert Watson 		NDFREE(&nd, NDF_ONLY_PNBUF);
618dd47f5caSRobert Watson 		if (nd.ni_dvp == vp)
619dd47f5caSRobert Watson 			vrele(nd.ni_dvp);
620dd47f5caSRobert Watson 		else
621dd47f5caSRobert Watson 			vput(nd.ni_dvp);
622dd47f5caSRobert Watson 		if (vp != NULL) {
623dd47f5caSRobert Watson 			vrele(vp);
624dd47f5caSRobert Watson 			error = EADDRINUSE;
6254f1f0ef5SRobert Watson 			goto error;
626dd47f5caSRobert Watson 		}
627dd47f5caSRobert Watson 		error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
628dd47f5caSRobert Watson 		if (error)
6294f1f0ef5SRobert Watson 			goto error;
630dd47f5caSRobert Watson 		goto restart;
631dd47f5caSRobert Watson 	}
632dd47f5caSRobert Watson 	VATTR_NULL(&vattr);
633dd47f5caSRobert Watson 	vattr.va_type = VSOCK;
634dd47f5caSRobert Watson 	vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask);
635dd47f5caSRobert Watson #ifdef MAC
63630d239bcSRobert Watson 	error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
637dd47f5caSRobert Watson 	    &vattr);
638dd47f5caSRobert Watson #endif
639885868cdSRobert Watson 	if (error == 0)
640dd47f5caSRobert Watson 		error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
641dd47f5caSRobert Watson 	NDFREE(&nd, NDF_ONLY_PNBUF);
642dd47f5caSRobert Watson 	vput(nd.ni_dvp);
643dd47f5caSRobert Watson 	if (error) {
644dd47f5caSRobert Watson 		vn_finished_write(mp);
6454f1f0ef5SRobert Watson 		goto error;
646dd47f5caSRobert Watson 	}
647dd47f5caSRobert Watson 	vp = nd.ni_vp;
64857fd3d55SPawel Jakub Dawidek 	ASSERT_VOP_ELOCKED(vp, "uipc_bind");
649dd47f5caSRobert Watson 	soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK);
650e7c33e29SRobert Watson 
651e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
6520c3c207fSGleb Smirnoff 	VOP_UNP_BIND(vp, unp);
653dd47f5caSRobert Watson 	unp->unp_vnode = vp;
654dd47f5caSRobert Watson 	unp->unp_addr = soun;
6554f1f0ef5SRobert Watson 	unp->unp_flags &= ~UNP_BINDING;
656e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
65722db15c0SAttilio Rao 	VOP_UNLOCK(vp, 0);
658dd47f5caSRobert Watson 	vn_finished_write(mp);
6594f1f0ef5SRobert Watson 	free(buf, M_TEMP);
6604f1f0ef5SRobert Watson 	return (0);
661e7c33e29SRobert Watson 
6624f1f0ef5SRobert Watson error:
663e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
6644f1f0ef5SRobert Watson 	unp->unp_flags &= ~UNP_BINDING;
665e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
666dd47f5caSRobert Watson 	free(buf, M_TEMP);
66740f2ac28SRobert Watson 	return (error);
668a29f300eSGarrett Wollman }
669a29f300eSGarrett Wollman 
670a29f300eSGarrett Wollman static int
6717493f24eSPawel Jakub Dawidek uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
6727493f24eSPawel Jakub Dawidek {
6737493f24eSPawel Jakub Dawidek 
6747493f24eSPawel Jakub Dawidek 	return (uipc_bindat(AT_FDCWD, so, nam, td));
6757493f24eSPawel Jakub Dawidek }
6767493f24eSPawel Jakub Dawidek 
6777493f24eSPawel Jakub Dawidek static int
678b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
679a29f300eSGarrett Wollman {
6800d9ce3a1SRobert Watson 	int error;
681a29f300eSGarrett Wollman 
682fd179ee9SRobert Watson 	KASSERT(td == curthread, ("uipc_connect: td != curthread"));
683fd179ee9SRobert Watson 	error = unp_connect(so, nam, td);
6840d9ce3a1SRobert Watson 	return (error);
685a29f300eSGarrett Wollman }
686a29f300eSGarrett Wollman 
6877493f24eSPawel Jakub Dawidek static int
6887493f24eSPawel Jakub Dawidek uipc_connectat(int fd, struct socket *so, struct sockaddr *nam,
6897493f24eSPawel Jakub Dawidek     struct thread *td)
6907493f24eSPawel Jakub Dawidek {
6917493f24eSPawel Jakub Dawidek 	int error;
6927493f24eSPawel Jakub Dawidek 
6937493f24eSPawel Jakub Dawidek 	KASSERT(td == curthread, ("uipc_connectat: td != curthread"));
6947493f24eSPawel Jakub Dawidek 	error = unp_connectat(fd, so, nam, td);
6957493f24eSPawel Jakub Dawidek 	return (error);
6967493f24eSPawel Jakub Dawidek }
6977493f24eSPawel Jakub Dawidek 
698a152f8a3SRobert Watson static void
699a152f8a3SRobert Watson uipc_close(struct socket *so)
700a152f8a3SRobert Watson {
701e7c33e29SRobert Watson 	struct unpcb *unp, *unp2;
702779f106aSGleb Smirnoff 	struct vnode *vp = NULL;
70375a67bf3SMatt Macy 	struct mtx *vplock;
70475a67bf3SMatt Macy 	int freed;
705a152f8a3SRobert Watson 	unp = sotounpcb(so);
706a152f8a3SRobert Watson 	KASSERT(unp != NULL, ("uipc_close: unp == NULL"));
707e7c33e29SRobert Watson 
70875a67bf3SMatt Macy 
70975a67bf3SMatt Macy 	vplock = NULL;
71075a67bf3SMatt Macy 	if ((vp = unp->unp_vnode) != NULL) {
71175a67bf3SMatt Macy 		vplock = mtx_pool_find(mtxpool_sleep, vp);
71275a67bf3SMatt Macy 		mtx_lock(vplock);
713e7c33e29SRobert Watson 	}
71475a67bf3SMatt Macy 	UNP_PCB_LOCK(unp);
71575a67bf3SMatt Macy 	if (vp && unp->unp_vnode == NULL) {
71675a67bf3SMatt Macy 		mtx_unlock(vplock);
71775a67bf3SMatt Macy 		vp = NULL;
71875a67bf3SMatt Macy 	}
71975a67bf3SMatt Macy 	if (vp != NULL) {
720779f106aSGleb Smirnoff 		VOP_UNP_DETACH(vp);
721779f106aSGleb Smirnoff 		unp->unp_vnode = NULL;
722779f106aSGleb Smirnoff 	}
72375a67bf3SMatt Macy 	unp2 = unp->unp_conn;
72475a67bf3SMatt Macy 	unp_pcb_hold(unp);
725acf9fd05SMatt Macy 	if (__predict_false(unp == unp2)) {
726acf9fd05SMatt Macy 		unp_disconnect(unp, unp2);
727acf9fd05SMatt Macy 	} else if (unp2 != NULL) {
72875a67bf3SMatt Macy 		unp_pcb_hold(unp2);
72975a67bf3SMatt Macy 		unp_pcb_owned_lock2(unp, unp2, freed);
73075a67bf3SMatt Macy 		unp_disconnect(unp, unp2);
73175a67bf3SMatt Macy 		if (unp_pcb_rele(unp2) == 0)
73275a67bf3SMatt Macy 			UNP_PCB_UNLOCK(unp2);
73375a67bf3SMatt Macy 	}
73475a67bf3SMatt Macy 	if (unp_pcb_rele(unp) == 0)
735e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
73675a67bf3SMatt Macy 	if (vp) {
73775a67bf3SMatt Macy 		mtx_unlock(vplock);
738779f106aSGleb Smirnoff 		vrele(vp);
739a152f8a3SRobert Watson 	}
74075a67bf3SMatt Macy }
741a152f8a3SRobert Watson 
7422c899584SRobert Watson static int
743a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2)
744a29f300eSGarrett Wollman {
745e7c33e29SRobert Watson 	struct unpcb *unp, *unp2;
7460d9ce3a1SRobert Watson 	int error;
747a29f300eSGarrett Wollman 
748e7c33e29SRobert Watson 	unp = so1->so_pcb;
7494d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_connect2: unp == NULL"));
750e7c33e29SRobert Watson 	unp2 = so2->so_pcb;
751e7c33e29SRobert Watson 	KASSERT(unp2 != NULL, ("uipc_connect2: unp2 == NULL"));
752acf9fd05SMatt Macy 	if (unp != unp2)
75375a67bf3SMatt Macy 		unp_pcb_lock2(unp, unp2);
754acf9fd05SMatt Macy 	else
755acf9fd05SMatt Macy 		UNP_PCB_LOCK(unp);
7566a2989fdSMatthew N. Dodd 	error = unp_connect2(so1, so2, PRU_CONNECT2);
757acf9fd05SMatt Macy 	if (unp != unp2)
758e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp2);
759e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
7600d9ce3a1SRobert Watson 	return (error);
761a29f300eSGarrett Wollman }
762a29f300eSGarrett Wollman 
763bc725eafSRobert Watson static void
764a29f300eSGarrett Wollman uipc_detach(struct socket *so)
765a29f300eSGarrett Wollman {
766e7c33e29SRobert Watson 	struct unpcb *unp, *unp2;
76775a67bf3SMatt Macy 	struct mtx *vplock;
7689ae328fcSJohn Baldwin 	struct sockaddr_un *saved_unp_addr;
7696d32873cSRobert Watson 	struct vnode *vp;
7709ae328fcSJohn Baldwin 	int freeunp, local_unp_rights;
771a29f300eSGarrett Wollman 
77240f2ac28SRobert Watson 	unp = sotounpcb(so);
7734d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_detach: unp == NULL"));
774e7c33e29SRobert Watson 
775434ac8b6SMark Johnston 	vp = NULL;
776c0874c34SMatt Macy 	vplock = NULL;
777434ac8b6SMark Johnston 	local_unp_rights = 0;
778434ac8b6SMark Johnston 
779779f106aSGleb Smirnoff 	UNP_LINK_WLOCK();
7806d32873cSRobert Watson 	LIST_REMOVE(unp, unp_link);
7816d32873cSRobert Watson 	unp->unp_gencnt = ++unp_gencnt;
7826d32873cSRobert Watson 	--unp_count;
78375a67bf3SMatt Macy 	UNP_LINK_WUNLOCK();
784434ac8b6SMark Johnston 
78575a67bf3SMatt Macy 	UNP_PCB_UNLOCK_ASSERT(unp);
78675a67bf3SMatt Macy  restart:
78775a67bf3SMatt Macy 	if ((vp = unp->unp_vnode) != NULL) {
78875a67bf3SMatt Macy 		vplock = mtx_pool_find(mtxpool_sleep, vp);
78975a67bf3SMatt Macy 		mtx_lock(vplock);
79075a67bf3SMatt Macy 	}
79175a67bf3SMatt Macy 	UNP_PCB_LOCK(unp);
79275a67bf3SMatt Macy 	if (unp->unp_vnode != vp &&
79375a67bf3SMatt Macy 		unp->unp_vnode != NULL) {
794c0874c34SMatt Macy 		if (vplock)
79575a67bf3SMatt Macy 			mtx_unlock(vplock);
79675a67bf3SMatt Macy 		UNP_PCB_UNLOCK(unp);
79775a67bf3SMatt Macy 		goto restart;
79875a67bf3SMatt Macy 	}
79975a67bf3SMatt Macy 	if ((unp->unp_flags & UNP_NASCENT) != 0) {
80075a67bf3SMatt Macy 		goto teardown;
80175a67bf3SMatt Macy 	}
8026d32873cSRobert Watson 	if ((vp = unp->unp_vnode) != NULL) {
803c7e41c8bSMikolaj Golub 		VOP_UNP_DETACH(vp);
8046d32873cSRobert Watson 		unp->unp_vnode = NULL;
8056d32873cSRobert Watson 	}
806acf9fd05SMatt Macy 	if (__predict_false(unp == unp->unp_conn)) {
807acf9fd05SMatt Macy 		unp_disconnect(unp, unp);
808acf9fd05SMatt Macy 		unp2 = NULL;
809acf9fd05SMatt Macy 		goto connect_self;
810acf9fd05SMatt Macy 	}
811acf9fd05SMatt Macy 	if ((unp2 = unp->unp_conn) != NULL) {
812acf9fd05SMatt Macy 		unp_pcb_owned_lock2(unp, unp2, freeunp);
813acf9fd05SMatt Macy 		if (freeunp)
814acf9fd05SMatt Macy 			unp2 = NULL;
815acf9fd05SMatt Macy 	}
81675a67bf3SMatt Macy 	unp_pcb_hold(unp);
817e7c33e29SRobert Watson 	if (unp2 != NULL) {
81875a67bf3SMatt Macy 		unp_pcb_hold(unp2);
819e7c33e29SRobert Watson 		unp_disconnect(unp, unp2);
82075a67bf3SMatt Macy 		if (unp_pcb_rele(unp2) == 0)
821e7c33e29SRobert Watson 			UNP_PCB_UNLOCK(unp2);
822e7c33e29SRobert Watson 	}
823acf9fd05SMatt Macy  connect_self:
82475a67bf3SMatt Macy 	UNP_PCB_UNLOCK(unp);
82575a67bf3SMatt Macy 	UNP_REF_LIST_LOCK();
8266d32873cSRobert Watson 	while (!LIST_EMPTY(&unp->unp_refs)) {
8276d32873cSRobert Watson 		struct unpcb *ref = LIST_FIRST(&unp->unp_refs);
828e7c33e29SRobert Watson 
82975a67bf3SMatt Macy 		unp_pcb_hold(ref);
83075a67bf3SMatt Macy 		UNP_REF_LIST_UNLOCK();
83175a67bf3SMatt Macy 
83275a67bf3SMatt Macy 		MPASS(ref != unp);
83375a67bf3SMatt Macy 		UNP_PCB_UNLOCK_ASSERT(ref);
834afc055d9SEd Schouten 		unp_drop(ref);
83575a67bf3SMatt Macy 		UNP_REF_LIST_LOCK();
8366d32873cSRobert Watson 	}
83775a67bf3SMatt Macy 
83875a67bf3SMatt Macy 	UNP_REF_LIST_UNLOCK();
83975a67bf3SMatt Macy 	UNP_PCB_LOCK(unp);
84075a67bf3SMatt Macy 	freeunp = unp_pcb_rele(unp);
84175a67bf3SMatt Macy 	MPASS(freeunp == 0);
842397c19d1SJeff Roberson 	local_unp_rights = unp_rights;
843434ac8b6SMark Johnston teardown:
8446d32873cSRobert Watson 	unp->unp_socket->so_pcb = NULL;
8459ae328fcSJohn Baldwin 	saved_unp_addr = unp->unp_addr;
8469ae328fcSJohn Baldwin 	unp->unp_addr = NULL;
84775a67bf3SMatt Macy 	unp->unp_socket = NULL;
84875a67bf3SMatt Macy 	freeunp = unp_pcb_rele(unp);
8499ae328fcSJohn Baldwin 	if (saved_unp_addr != NULL)
8501ede983cSDag-Erling Smørgrav 		free(saved_unp_addr, M_SONAME);
85175a67bf3SMatt Macy 	if (!freeunp)
8526e2faa24SRobert Watson 		UNP_PCB_UNLOCK(unp);
85375a67bf3SMatt Macy 	if (vp) {
85475a67bf3SMatt Macy 		mtx_unlock(vplock);
8556d32873cSRobert Watson 		vrele(vp);
85675a67bf3SMatt Macy 	}
8576d32873cSRobert Watson 	if (local_unp_rights)
858daee0f0bSKonstantin Belousov 		taskqueue_enqueue_timeout(taskqueue_thread, &unp_gc_task, -1);
859a29f300eSGarrett Wollman }
860a29f300eSGarrett Wollman 
861a29f300eSGarrett Wollman static int
862a29f300eSGarrett Wollman uipc_disconnect(struct socket *so)
863a29f300eSGarrett Wollman {
864e7c33e29SRobert Watson 	struct unpcb *unp, *unp2;
86575a67bf3SMatt Macy 	int freed;
866a29f300eSGarrett Wollman 
86740f2ac28SRobert Watson 	unp = sotounpcb(so);
8684d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_disconnect: unp == NULL"));
869e7c33e29SRobert Watson 
870e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
87175a67bf3SMatt Macy 	if ((unp2 = unp->unp_conn) == NULL) {
872e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
87375a67bf3SMatt Macy 		return (0);
87475a67bf3SMatt Macy 	}
875fcabd541SMatt Macy 	if (__predict_true(unp != unp2)) {
87675a67bf3SMatt Macy 		unp_pcb_owned_lock2(unp, unp2, freed);
87775a67bf3SMatt Macy 		if (__predict_false(freed)) {
87875a67bf3SMatt Macy 			UNP_PCB_UNLOCK(unp);
87975a67bf3SMatt Macy 			return (0);
88075a67bf3SMatt Macy 		}
88175a67bf3SMatt Macy 		unp_pcb_hold(unp2);
882fcabd541SMatt Macy 	}
88375a67bf3SMatt Macy 	unp_pcb_hold(unp);
88475a67bf3SMatt Macy 	unp_disconnect(unp, unp2);
88575a67bf3SMatt Macy 	if (unp_pcb_rele(unp) == 0)
88675a67bf3SMatt Macy 		UNP_PCB_UNLOCK(unp);
887fcabd541SMatt Macy 	if ((unp != unp2) && unp_pcb_rele(unp2) == 0)
88875a67bf3SMatt Macy 		UNP_PCB_UNLOCK(unp2);
889e5aeaa0cSDag-Erling Smørgrav 	return (0);
890a29f300eSGarrett Wollman }
891a29f300eSGarrett Wollman 
892a29f300eSGarrett Wollman static int
893d374e81eSRobert Watson uipc_listen(struct socket *so, int backlog, struct thread *td)
894a29f300eSGarrett Wollman {
89540f2ac28SRobert Watson 	struct unpcb *unp;
8960d9ce3a1SRobert Watson 	int error;
897a29f300eSGarrett Wollman 
898beb4b312SGleb Smirnoff 	if (so->so_type != SOCK_STREAM && so->so_type != SOCK_SEQPACKET)
899beb4b312SGleb Smirnoff 		return (EOPNOTSUPP);
900beb4b312SGleb Smirnoff 
90140f2ac28SRobert Watson 	unp = sotounpcb(so);
9024d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_listen: unp == NULL"));
903e7c33e29SRobert Watson 
904e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
9054d4b555eSRobert Watson 	if (unp->unp_vnode == NULL) {
90647a84387SEd Schouten 		/* Already connected or not bound to an address. */
90747a84387SEd Schouten 		error = unp->unp_conn != NULL ? EINVAL : EDESTADDRREQ;
908e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
90947a84387SEd Schouten 		return (error);
91040f2ac28SRobert Watson 	}
911e7c33e29SRobert Watson 
912e7c33e29SRobert Watson 	SOCK_LOCK(so);
913e7c33e29SRobert Watson 	error = solisten_proto_check(so);
914e7c33e29SRobert Watson 	if (error == 0) {
915e7c33e29SRobert Watson 		cru2x(td->td_ucred, &unp->unp_peercred);
916e7c33e29SRobert Watson 		solisten_proto(so, backlog);
917e7c33e29SRobert Watson 	}
918e7c33e29SRobert Watson 	SOCK_UNLOCK(so);
919e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
9200d9ce3a1SRobert Watson 	return (error);
921a29f300eSGarrett Wollman }
922a29f300eSGarrett Wollman 
923a29f300eSGarrett Wollman static int
92457bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam)
925a29f300eSGarrett Wollman {
926e7c33e29SRobert Watson 	struct unpcb *unp, *unp2;
9270d9ce3a1SRobert Watson 	const struct sockaddr *sa;
928a29f300eSGarrett Wollman 
9294d4b555eSRobert Watson 	unp = sotounpcb(so);
9304d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL"));
931e7c33e29SRobert Watson 
9320d9ce3a1SRobert Watson 	*nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
933afd9f91cSJohn Baldwin 	UNP_LINK_RLOCK();
934bdc5f6a3SHajimu UMEMOTO 	/*
935e7c33e29SRobert Watson 	 * XXX: It seems that this test always fails even when connection is
936e7c33e29SRobert Watson 	 * established.  So, this else clause is added as workaround to
937e7c33e29SRobert Watson 	 * return PF_LOCAL sockaddr.
938bdc5f6a3SHajimu UMEMOTO 	 */
939e7c33e29SRobert Watson 	unp2 = unp->unp_conn;
940e7c33e29SRobert Watson 	if (unp2 != NULL) {
941e7c33e29SRobert Watson 		UNP_PCB_LOCK(unp2);
942e7c33e29SRobert Watson 		if (unp2->unp_addr != NULL)
943afd9f91cSJohn Baldwin 			sa = (struct sockaddr *) unp2->unp_addr;
944e7c33e29SRobert Watson 		else
9450d9ce3a1SRobert Watson 			sa = &sun_noname;
9460d9ce3a1SRobert Watson 		bcopy(sa, *nam, sa->sa_len);
947e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp2);
948e7c33e29SRobert Watson 	} else {
949e7c33e29SRobert Watson 		sa = &sun_noname;
950e7c33e29SRobert Watson 		bcopy(sa, *nam, sa->sa_len);
951e7c33e29SRobert Watson 	}
952afd9f91cSJohn Baldwin 	UNP_LINK_RUNLOCK();
953e5aeaa0cSDag-Erling Smørgrav 	return (0);
954a29f300eSGarrett Wollman }
955a29f300eSGarrett Wollman 
956a29f300eSGarrett Wollman static int
957a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags)
958a29f300eSGarrett Wollman {
959e7c33e29SRobert Watson 	struct unpcb *unp, *unp2;
960a29f300eSGarrett Wollman 	struct socket *so2;
961337cc6b6SRobert Watson 	u_int mbcnt, sbcc;
962a29f300eSGarrett Wollman 
96340f2ac28SRobert Watson 	unp = sotounpcb(so);
9642b21d0e8SGleb Smirnoff 	KASSERT(unp != NULL, ("%s: unp == NULL", __func__));
9652b21d0e8SGleb Smirnoff 	KASSERT(so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET,
9662b21d0e8SGleb Smirnoff 	    ("%s: socktype %d", __func__, so->so_type));
967e7c33e29SRobert Watson 
968df8bae1dSRodney W. Grimes 	/*
969e7c33e29SRobert Watson 	 * Adjust backpressure on sender and wakeup any waiting to write.
970e7c33e29SRobert Watson 	 *
971d7924b70SRobert Watson 	 * The unp lock is acquired to maintain the validity of the unp_conn
972d7924b70SRobert Watson 	 * pointer; no lock on unp2 is required as unp2->unp_socket will be
973d7924b70SRobert Watson 	 * static as long as we don't permit unp2 to disconnect from unp,
974d7924b70SRobert Watson 	 * which is prevented by the lock on unp.  We cache values from
975d7924b70SRobert Watson 	 * so_rcv to avoid holding the so_rcv lock over the entire
976d7924b70SRobert Watson 	 * transaction on the remote so_snd.
977df8bae1dSRodney W. Grimes 	 */
978337cc6b6SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
979337cc6b6SRobert Watson 	mbcnt = so->so_rcv.sb_mbcnt;
9802b21d0e8SGleb Smirnoff 	sbcc = sbavail(&so->so_rcv);
981337cc6b6SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
982c2090e73SAlan Somers 	/*
983c2090e73SAlan Somers 	 * There is a benign race condition at this point.  If we're planning to
984c2090e73SAlan Somers 	 * clear SB_STOP, but uipc_send is called on the connected socket at
985c2090e73SAlan Somers 	 * this instant, it might add data to the sockbuf and set SB_STOP.  Then
986c2090e73SAlan Somers 	 * we would erroneously clear SB_STOP below, even though the sockbuf is
987c2090e73SAlan Somers 	 * full.  The race is benign because the only ill effect is to allow the
988c2090e73SAlan Somers 	 * sockbuf to exceed its size limit, and the size limits are not
989c2090e73SAlan Somers 	 * strictly guaranteed anyway.
990c2090e73SAlan Somers 	 */
991e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
992e7c33e29SRobert Watson 	unp2 = unp->unp_conn;
993e7c33e29SRobert Watson 	if (unp2 == NULL) {
994e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
995e7c33e29SRobert Watson 		return (0);
996337cc6b6SRobert Watson 	}
997e7c33e29SRobert Watson 	so2 = unp2->unp_socket;
998337cc6b6SRobert Watson 	SOCKBUF_LOCK(&so2->so_snd);
999c2090e73SAlan Somers 	if (sbcc < so2->so_snd.sb_hiwat && mbcnt < so2->so_snd.sb_mbmax)
1000c2090e73SAlan Somers 		so2->so_snd.sb_flags &= ~SB_STOP;
10011e4d7da7SRobert Watson 	sowwakeup_locked(so2);
1002e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
1003e5aeaa0cSDag-Erling Smørgrav 	return (0);
1004a29f300eSGarrett Wollman }
1005df8bae1dSRodney W. Grimes 
1006a29f300eSGarrett Wollman static int
100775a67bf3SMatt Macy connect_internal(struct socket *so, struct sockaddr *nam, struct thread *td)
100875a67bf3SMatt Macy {
100975a67bf3SMatt Macy 	int error;
101075a67bf3SMatt Macy 	struct unpcb *unp;
101175a67bf3SMatt Macy 
101275a67bf3SMatt Macy 	unp = so->so_pcb;
101375a67bf3SMatt Macy 	if (unp->unp_conn != NULL)
101475a67bf3SMatt Macy 		return (EISCONN);
101575a67bf3SMatt Macy 	error = unp_connect(so, nam, td);
101675a67bf3SMatt Macy 	if (error)
101775a67bf3SMatt Macy 		return (error);
101875a67bf3SMatt Macy 	UNP_PCB_LOCK(unp);
101975a67bf3SMatt Macy 	if (unp->unp_conn == NULL) {
102075a67bf3SMatt Macy 		UNP_PCB_UNLOCK(unp);
102175a67bf3SMatt Macy 		if (error == 0)
102275a67bf3SMatt Macy 			error = ENOTCONN;
102375a67bf3SMatt Macy 	}
102475a67bf3SMatt Macy 	return (error);
102575a67bf3SMatt Macy }
102675a67bf3SMatt Macy 
102775a67bf3SMatt Macy 
102875a67bf3SMatt Macy static int
102957bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
1030b40ce416SJulian Elischer     struct mbuf *control, struct thread *td)
1031a29f300eSGarrett Wollman {
1032f3f49bbbSRobert Watson 	struct unpcb *unp, *unp2;
1033a29f300eSGarrett Wollman 	struct socket *so2;
1034c2090e73SAlan Somers 	u_int mbcnt, sbcc;
103575a67bf3SMatt Macy 	int freed, error;
1036a29f300eSGarrett Wollman 
103740f2ac28SRobert Watson 	unp = sotounpcb(so);
10382b21d0e8SGleb Smirnoff 	KASSERT(unp != NULL, ("%s: unp == NULL", __func__));
10392b21d0e8SGleb Smirnoff 	KASSERT(so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM ||
10402b21d0e8SGleb Smirnoff 	    so->so_type == SOCK_SEQPACKET,
10412b21d0e8SGleb Smirnoff 	    ("%s: socktype %d", __func__, so->so_type));
1042e7c33e29SRobert Watson 
104375a67bf3SMatt Macy 	freed = error = 0;
1044a29f300eSGarrett Wollman 	if (flags & PRUS_OOB) {
1045a29f300eSGarrett Wollman 		error = EOPNOTSUPP;
1046a29f300eSGarrett Wollman 		goto release;
1047a29f300eSGarrett Wollman 	}
1048fc3fcacfSRobert Watson 	if (control != NULL && (error = unp_internalize(&control, td)))
1049a29f300eSGarrett Wollman 		goto release;
105075a67bf3SMatt Macy 
105175a67bf3SMatt Macy 	unp2 = NULL;
1052a29f300eSGarrett Wollman 	switch (so->so_type) {
1053a29f300eSGarrett Wollman 	case SOCK_DGRAM:
1054a29f300eSGarrett Wollman 	{
1055e7dd9a10SRobert Watson 		const struct sockaddr *from;
1056df8bae1dSRodney W. Grimes 
1057fc3fcacfSRobert Watson 		if (nam != NULL) {
105875a67bf3SMatt Macy 			/*
105975a67bf3SMatt Macy 			 * We return with UNP_PCB_LOCK_HELD so we know that
106075a67bf3SMatt Macy 			 * the reference is live if the pointer is valid.
106175a67bf3SMatt Macy 			 */
106275a67bf3SMatt Macy 			if ((error = connect_internal(so, nam, td)))
1063df8bae1dSRodney W. Grimes 				break;
106475a67bf3SMatt Macy 			MPASS(unp->unp_conn != NULL);
1065e7c33e29SRobert Watson 			unp2 = unp->unp_conn;
106675a67bf3SMatt Macy 		} else  {
106775a67bf3SMatt Macy 			UNP_PCB_LOCK(unp);
106860a5ef26SRobert Watson 
1069b5ff0914SRobert Watson 			/*
1070b5ff0914SRobert Watson 			 * Because connect() and send() are non-atomic in a sendto()
1071b5ff0914SRobert Watson 			 * with a target address, it's possible that the socket will
1072b5ff0914SRobert Watson 			 * have disconnected before the send() can run.  In that case
1073b5ff0914SRobert Watson 			 * return the slightly counter-intuitive but otherwise
1074b5ff0914SRobert Watson 			 * correct error that the socket is not connected.
1075b5ff0914SRobert Watson 			 */
107675a67bf3SMatt Macy 			if ((unp2 = unp->unp_conn)  == NULL) {
107775a67bf3SMatt Macy 				UNP_PCB_UNLOCK(unp);
1078b5ff0914SRobert Watson 				error = ENOTCONN;
1079b5ff0914SRobert Watson 				break;
1080b5ff0914SRobert Watson 			}
108175a67bf3SMatt Macy 		}
1082c684c14cSMatt Macy 		if (__predict_false(unp == unp2)) {
1083c684c14cSMatt Macy 			if (unp->unp_socket == NULL) {
1084c684c14cSMatt Macy 				error = ENOTCONN;
1085c684c14cSMatt Macy 				break;
1086c684c14cSMatt Macy 			}
1087c684c14cSMatt Macy 			goto connect_self;
1088c684c14cSMatt Macy 		}
108975a67bf3SMatt Macy 		unp_pcb_owned_lock2(unp, unp2, freed);
109075a67bf3SMatt Macy 		if (__predict_false(freed)) {
109175a67bf3SMatt Macy 			UNP_PCB_UNLOCK(unp);
109275a67bf3SMatt Macy 			error = ENOTCONN;
109375a67bf3SMatt Macy 			break;
109475a67bf3SMatt Macy 		}
109575a67bf3SMatt Macy 		/*
109675a67bf3SMatt Macy 		 * The socket referencing unp2 may have been closed
109775a67bf3SMatt Macy 		 * or unp may have been disconnected if the unp lock
109875a67bf3SMatt Macy 		 * was dropped to acquire unp2.
109975a67bf3SMatt Macy 		 */
110075a67bf3SMatt Macy 		if (__predict_false(unp->unp_conn == NULL) ||
110175a67bf3SMatt Macy 			unp2->unp_socket == NULL) {
110275a67bf3SMatt Macy 			UNP_PCB_UNLOCK(unp);
110375a67bf3SMatt Macy 			if (unp_pcb_rele(unp2) == 0)
110475a67bf3SMatt Macy 				UNP_PCB_UNLOCK(unp2);
110575a67bf3SMatt Macy 			error = ENOTCONN;
110675a67bf3SMatt Macy 			break;
110775a67bf3SMatt Macy 		}
1108c684c14cSMatt Macy 	connect_self:
1109ede6e136SRobert Watson 		if (unp2->unp_flags & UNP_WANTCRED)
1110ede6e136SRobert Watson 			control = unp_addsockcred(td, control);
1111fc3fcacfSRobert Watson 		if (unp->unp_addr != NULL)
111257bf258eSGarrett Wollman 			from = (struct sockaddr *)unp->unp_addr;
1113df8bae1dSRodney W. Grimes 		else
1114df8bae1dSRodney W. Grimes 			from = &sun_noname;
1115ede6e136SRobert Watson 		so2 = unp2->unp_socket;
1116a34b7046SRobert Watson 		SOCKBUF_LOCK(&so2->so_rcv);
11176dde7ecbSPeter Wemm 		if (sbappendaddr_locked(&so2->so_rcv, from, m,
11188de34a88SAlan Somers 		    control)) {
11191e4d7da7SRobert Watson 			sorwakeup_locked(so2);
1120fc3fcacfSRobert Watson 			m = NULL;
1121fc3fcacfSRobert Watson 			control = NULL;
1122e5aeaa0cSDag-Erling Smørgrav 		} else {
1123a34b7046SRobert Watson 			SOCKBUF_UNLOCK(&so2->so_rcv);
1124df8bae1dSRodney W. Grimes 			error = ENOBUFS;
1125e5aeaa0cSDag-Erling Smørgrav 		}
112675a67bf3SMatt Macy 		if (nam != NULL)
1127e7c33e29SRobert Watson 			unp_disconnect(unp, unp2);
1128c684c14cSMatt Macy 		if (__predict_true(unp != unp2))
1129e7c33e29SRobert Watson 			UNP_PCB_UNLOCK(unp2);
1130ede6e136SRobert Watson 		UNP_PCB_UNLOCK(unp);
1131df8bae1dSRodney W. Grimes 		break;
1132df8bae1dSRodney W. Grimes 	}
1133df8bae1dSRodney W. Grimes 
113484d61770SRobert Watson 	case SOCK_SEQPACKET:
1135df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
1136402cc72dSDavid Greenman 		if ((so->so_state & SS_ISCONNECTED) == 0) {
1137fc3fcacfSRobert Watson 			if (nam != NULL) {
113875a67bf3SMatt Macy 				if ((error = connect_internal(so, nam, td)))
113975a67bf3SMatt Macy 					break;
1140402cc72dSDavid Greenman 			} else  {
1141402cc72dSDavid Greenman 				error = ENOTCONN;
1142402cc72dSDavid Greenman 				break;
1143402cc72dSDavid Greenman 			}
114475a67bf3SMatt Macy 		} else if ((unp2 = unp->unp_conn) == NULL) {
114575a67bf3SMatt Macy 			error = ENOTCONN;
114675a67bf3SMatt Macy 			break;
114775a67bf3SMatt Macy 		} else if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1148df8bae1dSRodney W. Grimes 			error = EPIPE;
1149df8bae1dSRodney W. Grimes 			break;
115075a67bf3SMatt Macy 		} else {
115175a67bf3SMatt Macy 			UNP_PCB_LOCK(unp);
115275a67bf3SMatt Macy 			if ((unp2 = unp->unp_conn) == NULL) {
115375a67bf3SMatt Macy 				UNP_PCB_UNLOCK(unp);
1154b5ff0914SRobert Watson 				error = ENOTCONN;
1155b5ff0914SRobert Watson 				break;
1156b5ff0914SRobert Watson 			}
115775a67bf3SMatt Macy 		}
115875a67bf3SMatt Macy 		unp_pcb_owned_lock2(unp, unp2, freed);
115975a67bf3SMatt Macy 		UNP_PCB_UNLOCK(unp);
116075a67bf3SMatt Macy 		if (__predict_false(freed)) {
116175a67bf3SMatt Macy 			error = ENOTCONN;
116275a67bf3SMatt Macy 			break;
116375a67bf3SMatt Macy 		}
116475a67bf3SMatt Macy 		if ((so2 = unp2->unp_socket) == NULL) {
116575a67bf3SMatt Macy 			UNP_PCB_UNLOCK(unp2);
116675a67bf3SMatt Macy 			error = ENOTCONN;
116775a67bf3SMatt Macy 			break;
116875a67bf3SMatt Macy 		}
1169a34b7046SRobert Watson 		SOCKBUF_LOCK(&so2->so_rcv);
1170f3f49bbbSRobert Watson 		if (unp2->unp_flags & UNP_WANTCRED) {
11716a2989fdSMatthew N. Dodd 			/*
1172716963cbSGleb Smirnoff 			 * Credentials are passed only once on SOCK_STREAM
1173716963cbSGleb Smirnoff 			 * and SOCK_SEQPACKET.
11746a2989fdSMatthew N. Dodd 			 */
1175f3f49bbbSRobert Watson 			unp2->unp_flags &= ~UNP_WANTCRED;
11766a2989fdSMatthew N. Dodd 			control = unp_addsockcred(td, control);
11776a2989fdSMatthew N. Dodd 		}
1178df8bae1dSRodney W. Grimes 		/*
11791c381b19SRobert Watson 		 * Send to paired receive port, and then reduce send buffer
11801c381b19SRobert Watson 		 * hiwater marks to maintain backpressure.  Wake up readers.
1181df8bae1dSRodney W. Grimes 		 */
118284d61770SRobert Watson 		switch (so->so_type) {
118384d61770SRobert Watson 		case SOCK_STREAM:
1184fc3fcacfSRobert Watson 			if (control != NULL) {
118584d61770SRobert Watson 				if (sbappendcontrol_locked(&so2->so_rcv, m,
118684d61770SRobert Watson 				    control))
1187fc3fcacfSRobert Watson 					control = NULL;
1188e7c33e29SRobert Watson 			} else
1189829fae90SGleb Smirnoff 				sbappend_locked(&so2->so_rcv, m, flags);
119084d61770SRobert Watson 			break;
119184d61770SRobert Watson 
119284d61770SRobert Watson 		case SOCK_SEQPACKET: {
119384d61770SRobert Watson 			const struct sockaddr *from;
119484d61770SRobert Watson 
119584d61770SRobert Watson 			from = &sun_noname;
11968de34a88SAlan Somers 			/*
11978de34a88SAlan Somers 			 * Don't check for space available in so2->so_rcv.
11988de34a88SAlan Somers 			 * Unix domain sockets only check for space in the
11998de34a88SAlan Somers 			 * sending sockbuf, and that check is performed one
12008de34a88SAlan Somers 			 * level up the stack.
12018de34a88SAlan Somers 			 */
12028de34a88SAlan Somers 			if (sbappendaddr_nospacecheck_locked(&so2->so_rcv,
12038de34a88SAlan Somers 				from, m, control))
120484d61770SRobert Watson 				control = NULL;
120584d61770SRobert Watson 			break;
120684d61770SRobert Watson 			}
120784d61770SRobert Watson 		}
120884d61770SRobert Watson 
1209c2090e73SAlan Somers 		mbcnt = so2->so_rcv.sb_mbcnt;
12102b21d0e8SGleb Smirnoff 		sbcc = sbavail(&so2->so_rcv);
12112b21d0e8SGleb Smirnoff 		if (sbcc)
1212337cc6b6SRobert Watson 			sorwakeup_locked(so2);
12132b21d0e8SGleb Smirnoff 		else
12142b21d0e8SGleb Smirnoff 			SOCKBUF_UNLOCK(&so2->so_rcv);
1215337cc6b6SRobert Watson 
1216c2090e73SAlan Somers 		/*
1217c2090e73SAlan Somers 		 * The PCB lock on unp2 protects the SB_STOP flag.  Without it,
1218c2090e73SAlan Somers 		 * it would be possible for uipc_rcvd to be called at this
1219c2090e73SAlan Somers 		 * point, drain the receiving sockbuf, clear SB_STOP, and then
1220c2090e73SAlan Somers 		 * we would set SB_STOP below.  That could lead to an empty
1221c2090e73SAlan Somers 		 * sockbuf having SB_STOP set
1222c2090e73SAlan Somers 		 */
1223337cc6b6SRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
1224c2090e73SAlan Somers 		if (sbcc >= so->so_snd.sb_hiwat || mbcnt >= so->so_snd.sb_mbmax)
1225c2090e73SAlan Somers 			so->so_snd.sb_flags |= SB_STOP;
12267abe2ac2SAlan Cox 		SOCKBUF_UNLOCK(&so->so_snd);
1227e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp2);
1228fc3fcacfSRobert Watson 		m = NULL;
1229df8bae1dSRodney W. Grimes 		break;
1230df8bae1dSRodney W. Grimes 	}
1231a29f300eSGarrett Wollman 
12326b8fda4dSGarrett Wollman 	/*
123360a5ef26SRobert Watson 	 * PRUS_EOF is equivalent to pru_send followed by pru_shutdown.
12346b8fda4dSGarrett Wollman 	 */
1235a29f300eSGarrett Wollman 	if (flags & PRUS_EOF) {
1236ede6e136SRobert Watson 		UNP_PCB_LOCK(unp);
12376b8fda4dSGarrett Wollman 		socantsendmore(so);
12386b8fda4dSGarrett Wollman 		unp_shutdown(unp);
1239e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
1240ede6e136SRobert Watson 	}
1241fc3fcacfSRobert Watson 	if (control != NULL && error != 0)
124299ab95dbSMark Johnston 		unp_dispose_mbuf(control);
1243bd508d39SDon Lewis 
1244a29f300eSGarrett Wollman release:
1245fc3fcacfSRobert Watson 	if (control != NULL)
1246a29f300eSGarrett Wollman 		m_freem(control);
1247100db364SGleb Smirnoff 	/*
1248100db364SGleb Smirnoff 	 * In case of PRUS_NOTREADY, uipc_ready() is responsible
1249100db364SGleb Smirnoff 	 * for freeing memory.
1250100db364SGleb Smirnoff 	 */
1251100db364SGleb Smirnoff 	if (m != NULL && (flags & PRUS_NOTREADY) == 0)
1252a29f300eSGarrett Wollman 		m_freem(m);
1253e5aeaa0cSDag-Erling Smørgrav 	return (error);
1254a29f300eSGarrett Wollman }
1255df8bae1dSRodney W. Grimes 
1256a29f300eSGarrett Wollman static int
1257c80ea19bSGleb Smirnoff uipc_ready(struct socket *so, struct mbuf *m, int count)
1258c80ea19bSGleb Smirnoff {
1259c80ea19bSGleb Smirnoff 	struct unpcb *unp, *unp2;
1260c80ea19bSGleb Smirnoff 	struct socket *so2;
1261c80ea19bSGleb Smirnoff 	int error;
1262c80ea19bSGleb Smirnoff 
1263c80ea19bSGleb Smirnoff 	unp = sotounpcb(so);
1264c80ea19bSGleb Smirnoff 
1265a62b4665SMatt Macy 	UNP_PCB_LOCK(unp);
1266100db364SGleb Smirnoff 	if ((unp2 = unp->unp_conn) == NULL) {
1267a62b4665SMatt Macy 		UNP_PCB_UNLOCK(unp);
1268a62b4665SMatt Macy 		goto error;
1269100db364SGleb Smirnoff 	}
1270a62b4665SMatt Macy 	if (unp != unp2) {
1271a62b4665SMatt Macy 		if (UNP_PCB_TRYLOCK(unp2) == 0) {
1272a62b4665SMatt Macy 			unp_pcb_hold(unp2);
1273a62b4665SMatt Macy 			UNP_PCB_UNLOCK(unp);
1274c80ea19bSGleb Smirnoff 			UNP_PCB_LOCK(unp2);
1275a62b4665SMatt Macy 			if (unp_pcb_rele(unp2))
1276a62b4665SMatt Macy 				goto error;
1277a62b4665SMatt Macy 		} else
1278a62b4665SMatt Macy 			UNP_PCB_UNLOCK(unp);
1279a62b4665SMatt Macy 	}
1280c80ea19bSGleb Smirnoff 	so2 = unp2->unp_socket;
1281c80ea19bSGleb Smirnoff 
1282c80ea19bSGleb Smirnoff 	SOCKBUF_LOCK(&so2->so_rcv);
1283c80ea19bSGleb Smirnoff 	if ((error = sbready(&so2->so_rcv, m, count)) == 0)
1284c80ea19bSGleb Smirnoff 		sorwakeup_locked(so2);
1285c80ea19bSGleb Smirnoff 	else
1286c80ea19bSGleb Smirnoff 		SOCKBUF_UNLOCK(&so2->so_rcv);
1287c80ea19bSGleb Smirnoff 
1288c80ea19bSGleb Smirnoff 	UNP_PCB_UNLOCK(unp2);
1289c80ea19bSGleb Smirnoff 
1290c80ea19bSGleb Smirnoff 	return (error);
1291a62b4665SMatt Macy  error:
1292a62b4665SMatt Macy 	for (int i = 0; i < count; i++)
1293a62b4665SMatt Macy 		m = m_free(m);
1294a62b4665SMatt Macy 	return (ECONNRESET);
1295c80ea19bSGleb Smirnoff }
1296c80ea19bSGleb Smirnoff 
1297c80ea19bSGleb Smirnoff static int
1298a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb)
1299a29f300eSGarrett Wollman {
1300c2090e73SAlan Somers 	struct unpcb *unp;
1301a29f300eSGarrett Wollman 
130240f2ac28SRobert Watson 	unp = sotounpcb(so);
13034d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_sense: unp == NULL"));
1304e7c33e29SRobert Watson 
1305a29f300eSGarrett Wollman 	sb->st_blksize = so->so_snd.sb_hiwat;
1306e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
1307f3732fd1SPoul-Henning Kamp 	sb->st_dev = NODEV;
1308df8bae1dSRodney W. Grimes 	if (unp->unp_ino == 0)
13096f782c46SJeffrey Hsu 		unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino;
1310a29f300eSGarrett Wollman 	sb->st_ino = unp->unp_ino;
1311e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
1312df8bae1dSRodney W. Grimes 	return (0);
1313a29f300eSGarrett Wollman }
1314df8bae1dSRodney W. Grimes 
1315a29f300eSGarrett Wollman static int
1316a29f300eSGarrett Wollman uipc_shutdown(struct socket *so)
1317a29f300eSGarrett Wollman {
131840f2ac28SRobert Watson 	struct unpcb *unp;
1319df8bae1dSRodney W. Grimes 
132040f2ac28SRobert Watson 	unp = sotounpcb(so);
13214d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_shutdown: unp == NULL"));
1322e7c33e29SRobert Watson 
1323e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
1324a29f300eSGarrett Wollman 	socantsendmore(so);
1325a29f300eSGarrett Wollman 	unp_shutdown(unp);
1326e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
1327e5aeaa0cSDag-Erling Smørgrav 	return (0);
1328a29f300eSGarrett Wollman }
1329df8bae1dSRodney W. Grimes 
1330a29f300eSGarrett Wollman static int
133157bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam)
1332a29f300eSGarrett Wollman {
133340f2ac28SRobert Watson 	struct unpcb *unp;
13340d9ce3a1SRobert Watson 	const struct sockaddr *sa;
1335a29f300eSGarrett Wollman 
13364d4b555eSRobert Watson 	unp = sotounpcb(so);
13374d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL"));
1338e7c33e29SRobert Watson 
13390d9ce3a1SRobert Watson 	*nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
1340e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
1341fc3fcacfSRobert Watson 	if (unp->unp_addr != NULL)
13420d9ce3a1SRobert Watson 		sa = (struct sockaddr *) unp->unp_addr;
134383f3198bSThomas Moestl 	else
13440d9ce3a1SRobert Watson 		sa = &sun_noname;
13450d9ce3a1SRobert Watson 	bcopy(sa, *nam, sa->sa_len);
1346e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
1347e5aeaa0cSDag-Erling Smørgrav 	return (0);
1348df8bae1dSRodney W. Grimes }
1349a29f300eSGarrett Wollman 
1350fa9402f2SRobert Watson static struct pr_usrreqs uipc_usrreqs_dgram = {
1351756d52a1SPoul-Henning Kamp 	.pru_abort = 		uipc_abort,
1352756d52a1SPoul-Henning Kamp 	.pru_accept =		uipc_accept,
1353756d52a1SPoul-Henning Kamp 	.pru_attach =		uipc_attach,
1354756d52a1SPoul-Henning Kamp 	.pru_bind =		uipc_bind,
13557493f24eSPawel Jakub Dawidek 	.pru_bindat =		uipc_bindat,
1356756d52a1SPoul-Henning Kamp 	.pru_connect =		uipc_connect,
13577493f24eSPawel Jakub Dawidek 	.pru_connectat =	uipc_connectat,
1358756d52a1SPoul-Henning Kamp 	.pru_connect2 =		uipc_connect2,
1359756d52a1SPoul-Henning Kamp 	.pru_detach =		uipc_detach,
1360756d52a1SPoul-Henning Kamp 	.pru_disconnect =	uipc_disconnect,
1361756d52a1SPoul-Henning Kamp 	.pru_listen =		uipc_listen,
1362756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		uipc_peeraddr,
1363756d52a1SPoul-Henning Kamp 	.pru_rcvd =		uipc_rcvd,
1364756d52a1SPoul-Henning Kamp 	.pru_send =		uipc_send,
1365756d52a1SPoul-Henning Kamp 	.pru_sense =		uipc_sense,
1366756d52a1SPoul-Henning Kamp 	.pru_shutdown =		uipc_shutdown,
1367756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		uipc_sockaddr,
1368fa9402f2SRobert Watson 	.pru_soreceive =	soreceive_dgram,
1369fa9402f2SRobert Watson 	.pru_close =		uipc_close,
1370fa9402f2SRobert Watson };
1371fa9402f2SRobert Watson 
137284d61770SRobert Watson static struct pr_usrreqs uipc_usrreqs_seqpacket = {
137384d61770SRobert Watson 	.pru_abort =		uipc_abort,
137484d61770SRobert Watson 	.pru_accept =		uipc_accept,
137584d61770SRobert Watson 	.pru_attach =		uipc_attach,
137684d61770SRobert Watson 	.pru_bind =		uipc_bind,
13777493f24eSPawel Jakub Dawidek 	.pru_bindat =		uipc_bindat,
137884d61770SRobert Watson 	.pru_connect =		uipc_connect,
13797493f24eSPawel Jakub Dawidek 	.pru_connectat =	uipc_connectat,
138084d61770SRobert Watson 	.pru_connect2 =		uipc_connect2,
138184d61770SRobert Watson 	.pru_detach =		uipc_detach,
138284d61770SRobert Watson 	.pru_disconnect =	uipc_disconnect,
138384d61770SRobert Watson 	.pru_listen =		uipc_listen,
138484d61770SRobert Watson 	.pru_peeraddr =		uipc_peeraddr,
138584d61770SRobert Watson 	.pru_rcvd =		uipc_rcvd,
138684d61770SRobert Watson 	.pru_send =		uipc_send,
138784d61770SRobert Watson 	.pru_sense =		uipc_sense,
138884d61770SRobert Watson 	.pru_shutdown =		uipc_shutdown,
138984d61770SRobert Watson 	.pru_sockaddr =		uipc_sockaddr,
139084d61770SRobert Watson 	.pru_soreceive =	soreceive_generic,	/* XXX: or...? */
139184d61770SRobert Watson 	.pru_close =		uipc_close,
139284d61770SRobert Watson };
139384d61770SRobert Watson 
1394fa9402f2SRobert Watson static struct pr_usrreqs uipc_usrreqs_stream = {
1395fa9402f2SRobert Watson 	.pru_abort = 		uipc_abort,
1396fa9402f2SRobert Watson 	.pru_accept =		uipc_accept,
1397fa9402f2SRobert Watson 	.pru_attach =		uipc_attach,
1398fa9402f2SRobert Watson 	.pru_bind =		uipc_bind,
13997493f24eSPawel Jakub Dawidek 	.pru_bindat =		uipc_bindat,
1400fa9402f2SRobert Watson 	.pru_connect =		uipc_connect,
14017493f24eSPawel Jakub Dawidek 	.pru_connectat =	uipc_connectat,
1402fa9402f2SRobert Watson 	.pru_connect2 =		uipc_connect2,
1403fa9402f2SRobert Watson 	.pru_detach =		uipc_detach,
1404fa9402f2SRobert Watson 	.pru_disconnect =	uipc_disconnect,
1405fa9402f2SRobert Watson 	.pru_listen =		uipc_listen,
1406fa9402f2SRobert Watson 	.pru_peeraddr =		uipc_peeraddr,
1407fa9402f2SRobert Watson 	.pru_rcvd =		uipc_rcvd,
1408fa9402f2SRobert Watson 	.pru_send =		uipc_send,
1409c80ea19bSGleb Smirnoff 	.pru_ready =		uipc_ready,
1410fa9402f2SRobert Watson 	.pru_sense =		uipc_sense,
1411fa9402f2SRobert Watson 	.pru_shutdown =		uipc_shutdown,
1412fa9402f2SRobert Watson 	.pru_sockaddr =		uipc_sockaddr,
1413fa9402f2SRobert Watson 	.pru_soreceive =	soreceive_generic,
1414a152f8a3SRobert Watson 	.pru_close =		uipc_close,
1415a29f300eSGarrett Wollman };
1416df8bae1dSRodney W. Grimes 
14170b36cd25SRobert Watson static int
1418892af6b9SRobert Watson uipc_ctloutput(struct socket *so, struct sockopt *sopt)
14190c1bb4fbSDima Dorfman {
142040f2ac28SRobert Watson 	struct unpcb *unp;
14210d9ce3a1SRobert Watson 	struct xucred xu;
14226a2989fdSMatthew N. Dodd 	int error, optval;
14236a2989fdSMatthew N. Dodd 
142496a041b5SMatthew N. Dodd 	if (sopt->sopt_level != 0)
142596a041b5SMatthew N. Dodd 		return (EINVAL);
142696a041b5SMatthew N. Dodd 
14276a2989fdSMatthew N. Dodd 	unp = sotounpcb(so);
14284d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL"));
14296a2989fdSMatthew N. Dodd 	error = 0;
14300c1bb4fbSDima Dorfman 	switch (sopt->sopt_dir) {
14310c1bb4fbSDima Dorfman 	case SOPT_GET:
14320c1bb4fbSDima Dorfman 		switch (sopt->sopt_name) {
14330c1bb4fbSDima Dorfman 		case LOCAL_PEERCRED:
1434e7c33e29SRobert Watson 			UNP_PCB_LOCK(unp);
14350c1bb4fbSDima Dorfman 			if (unp->unp_flags & UNP_HAVEPC)
14360d9ce3a1SRobert Watson 				xu = unp->unp_peercred;
14370c1bb4fbSDima Dorfman 			else {
14380c1bb4fbSDima Dorfman 				if (so->so_type == SOCK_STREAM)
14390c1bb4fbSDima Dorfman 					error = ENOTCONN;
14400c1bb4fbSDima Dorfman 				else
14410c1bb4fbSDima Dorfman 					error = EINVAL;
14420c1bb4fbSDima Dorfman 			}
1443e7c33e29SRobert Watson 			UNP_PCB_UNLOCK(unp);
14440d9ce3a1SRobert Watson 			if (error == 0)
14450d9ce3a1SRobert Watson 				error = sooptcopyout(sopt, &xu, sizeof(xu));
14460c1bb4fbSDima Dorfman 			break;
1447e7c33e29SRobert Watson 
14486a2989fdSMatthew N. Dodd 		case LOCAL_CREDS:
1449a6357845SRobert Watson 			/* Unlocked read. */
14506a2989fdSMatthew N. Dodd 			optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0;
14516a2989fdSMatthew N. Dodd 			error = sooptcopyout(sopt, &optval, sizeof(optval));
14526a2989fdSMatthew N. Dodd 			break;
1453e7c33e29SRobert Watson 
14546a2989fdSMatthew N. Dodd 		case LOCAL_CONNWAIT:
1455a6357845SRobert Watson 			/* Unlocked read. */
14566a2989fdSMatthew N. Dodd 			optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0;
14576a2989fdSMatthew N. Dodd 			error = sooptcopyout(sopt, &optval, sizeof(optval));
14586a2989fdSMatthew N. Dodd 			break;
1459e7c33e29SRobert Watson 
14600c1bb4fbSDima Dorfman 		default:
14610c1bb4fbSDima Dorfman 			error = EOPNOTSUPP;
14620c1bb4fbSDima Dorfman 			break;
14630c1bb4fbSDima Dorfman 		}
14640c1bb4fbSDima Dorfman 		break;
1465e7c33e29SRobert Watson 
14660c1bb4fbSDima Dorfman 	case SOPT_SET:
14676a2989fdSMatthew N. Dodd 		switch (sopt->sopt_name) {
14686a2989fdSMatthew N. Dodd 		case LOCAL_CREDS:
14696a2989fdSMatthew N. Dodd 		case LOCAL_CONNWAIT:
14706a2989fdSMatthew N. Dodd 			error = sooptcopyin(sopt, &optval, sizeof(optval),
14716a2989fdSMatthew N. Dodd 					    sizeof(optval));
14726a2989fdSMatthew N. Dodd 			if (error)
14736a2989fdSMatthew N. Dodd 				break;
14746a2989fdSMatthew N. Dodd 
1475e7c33e29SRobert Watson #define	OPTSET(bit) do {						\
1476e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);						\
14776a2989fdSMatthew N. Dodd 	if (optval)							\
14786a2989fdSMatthew N. Dodd 		unp->unp_flags |= bit;					\
14796a2989fdSMatthew N. Dodd 	else								\
1480e7c33e29SRobert Watson 		unp->unp_flags &= ~bit;					\
1481e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);						\
1482e7c33e29SRobert Watson } while (0)
14836a2989fdSMatthew N. Dodd 
14846a2989fdSMatthew N. Dodd 			switch (sopt->sopt_name) {
14856a2989fdSMatthew N. Dodd 			case LOCAL_CREDS:
14866a2989fdSMatthew N. Dodd 				OPTSET(UNP_WANTCRED);
14876a2989fdSMatthew N. Dodd 				break;
1488e7c33e29SRobert Watson 
14896a2989fdSMatthew N. Dodd 			case LOCAL_CONNWAIT:
14906a2989fdSMatthew N. Dodd 				OPTSET(UNP_CONNWAIT);
14916a2989fdSMatthew N. Dodd 				break;
1492e7c33e29SRobert Watson 
14936a2989fdSMatthew N. Dodd 			default:
14946a2989fdSMatthew N. Dodd 				break;
14956a2989fdSMatthew N. Dodd 			}
14966a2989fdSMatthew N. Dodd 			break;
14976a2989fdSMatthew N. Dodd #undef	OPTSET
14986a2989fdSMatthew N. Dodd 		default:
14996a2989fdSMatthew N. Dodd 			error = ENOPROTOOPT;
15006a2989fdSMatthew N. Dodd 			break;
15016a2989fdSMatthew N. Dodd 		}
1502abb886faSMatthew N. Dodd 		break;
1503e7c33e29SRobert Watson 
15040c1bb4fbSDima Dorfman 	default:
15050c1bb4fbSDima Dorfman 		error = EOPNOTSUPP;
15060c1bb4fbSDima Dorfman 		break;
15070c1bb4fbSDima Dorfman 	}
15080c1bb4fbSDima Dorfman 	return (error);
15090c1bb4fbSDima Dorfman }
15100c1bb4fbSDima Dorfman 
1511f708ef1bSPoul-Henning Kamp static int
1512892af6b9SRobert Watson unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1513df8bae1dSRodney W. Grimes {
15147493f24eSPawel Jakub Dawidek 
15157493f24eSPawel Jakub Dawidek 	return (unp_connectat(AT_FDCWD, so, nam, td));
15167493f24eSPawel Jakub Dawidek }
15177493f24eSPawel Jakub Dawidek 
15187493f24eSPawel Jakub Dawidek static int
15197493f24eSPawel Jakub Dawidek unp_connectat(int fd, struct socket *so, struct sockaddr *nam,
15207493f24eSPawel Jakub Dawidek     struct thread *td)
15217493f24eSPawel Jakub Dawidek {
1522892af6b9SRobert Watson 	struct sockaddr_un *soun = (struct sockaddr_un *)nam;
1523892af6b9SRobert Watson 	struct vnode *vp;
1524779f106aSGleb Smirnoff 	struct socket *so2;
1525b295bdcdSRobert Watson 	struct unpcb *unp, *unp2, *unp3;
1526df8bae1dSRodney W. Grimes 	struct nameidata nd;
152757bf258eSGarrett Wollman 	char buf[SOCK_MAXADDRLEN];
15280d9ce3a1SRobert Watson 	struct sockaddr *sa;
15297008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
153075a67bf3SMatt Macy 	int error, len, freed;
153175a67bf3SMatt Macy 	struct mtx *vplock;
15320d9ce3a1SRobert Watson 
1533cb7df69bSKevin Lo 	if (nam->sa_family != AF_UNIX)
1534cb7df69bSKevin Lo 		return (EAFNOSUPPORT);
1535a06534c3SBjoern A. Zeeb 	if (nam->sa_len > sizeof(struct sockaddr_un))
1536a06534c3SBjoern A. Zeeb 		return (EINVAL);
153757bf258eSGarrett Wollman 	len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
153857bf258eSGarrett Wollman 	if (len <= 0)
1539e5aeaa0cSDag-Erling Smørgrav 		return (EINVAL);
15407928893dSEd Maste 	bcopy(soun->sun_path, buf, len);
15417928893dSEd Maste 	buf[len] = 0;
1542e7c33e29SRobert Watson 
154375a67bf3SMatt Macy 	unp = sotounpcb(so);
1544e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
15454f1f0ef5SRobert Watson 	if (unp->unp_flags & UNP_CONNECTING) {
1546e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
15474f1f0ef5SRobert Watson 		return (EALREADY);
15484f1f0ef5SRobert Watson 	}
154905102f04SRobert Watson 	unp->unp_flags |= UNP_CONNECTING;
1550e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
1551e7c33e29SRobert Watson 
15520d9ce3a1SRobert Watson 	sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
15537493f24eSPawel Jakub Dawidek 	NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
15547008be5bSPawel Jakub Dawidek 	    UIO_SYSSPACE, buf, fd, cap_rights_init(&rights, CAP_CONNECTAT), td);
1555797f2d22SPoul-Henning Kamp 	error = namei(&nd);
1556797f2d22SPoul-Henning Kamp 	if (error)
15570d9ce3a1SRobert Watson 		vp = NULL;
15580d9ce3a1SRobert Watson 	else
1559df8bae1dSRodney W. Grimes 		vp = nd.ni_vp;
15600d9ce3a1SRobert Watson 	ASSERT_VOP_LOCKED(vp, "unp_connect");
1561762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
15620d9ce3a1SRobert Watson 	if (error)
15630d9ce3a1SRobert Watson 		goto bad;
15640d9ce3a1SRobert Watson 
1565df8bae1dSRodney W. Grimes 	if (vp->v_type != VSOCK) {
1566df8bae1dSRodney W. Grimes 		error = ENOTSOCK;
1567df8bae1dSRodney W. Grimes 		goto bad;
1568df8bae1dSRodney W. Grimes 	}
15696fac927cSRobert Watson #ifdef MAC
157030d239bcSRobert Watson 	error = mac_vnode_check_open(td->td_ucred, vp, VWRITE | VREAD);
15716fac927cSRobert Watson 	if (error)
15726fac927cSRobert Watson 		goto bad;
15736fac927cSRobert Watson #endif
1574a854ed98SJohn Baldwin 	error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td);
1575797f2d22SPoul-Henning Kamp 	if (error)
1576df8bae1dSRodney W. Grimes 		goto bad;
1577e7c33e29SRobert Watson 
1578b295bdcdSRobert Watson 	unp = sotounpcb(so);
15794d4b555eSRobert Watson 	KASSERT(unp != NULL, ("unp_connect: unp == NULL"));
1580e7c33e29SRobert Watson 
158175a67bf3SMatt Macy 	vplock = mtx_pool_find(mtxpool_sleep, vp);
158275a67bf3SMatt Macy 	mtx_lock(vplock);
15830c3c207fSGleb Smirnoff 	VOP_UNP_CONNECT(vp, &unp2);
15840c3c207fSGleb Smirnoff 	if (unp2 == NULL) {
1585df8bae1dSRodney W. Grimes 		error = ECONNREFUSED;
15862260c03dSRobert Watson 		goto bad2;
1587df8bae1dSRodney W. Grimes 	}
15880c3c207fSGleb Smirnoff 	so2 = unp2->unp_socket;
1589df8bae1dSRodney W. Grimes 	if (so->so_type != so2->so_type) {
1590df8bae1dSRodney W. Grimes 		error = EPROTOTYPE;
15912260c03dSRobert Watson 		goto bad2;
1592df8bae1dSRodney W. Grimes 	}
1593df8bae1dSRodney W. Grimes 	if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
1594e7c33e29SRobert Watson 		if (so2->so_options & SO_ACCEPTCONN) {
15951fb51a12SBjoern A. Zeeb 			CURVNET_SET(so2->so_vnet);
1596779f106aSGleb Smirnoff 			so2 = sonewconn(so2, 0);
15971fb51a12SBjoern A. Zeeb 			CURVNET_RESTORE();
1598e7c33e29SRobert Watson 		} else
1599779f106aSGleb Smirnoff 			so2 = NULL;
1600779f106aSGleb Smirnoff 		if (so2 == NULL) {
1601df8bae1dSRodney W. Grimes 			error = ECONNREFUSED;
1602cb8f450bSMatt Macy 			goto bad2;
1603df8bae1dSRodney W. Grimes 		}
1604779f106aSGleb Smirnoff 		unp3 = sotounpcb(so2);
1605e10ef65dSMatt Macy 		unp_pcb_lock2(unp2, unp3);
16060d9ce3a1SRobert Watson 		if (unp2->unp_addr != NULL) {
16070d9ce3a1SRobert Watson 			bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len);
16080d9ce3a1SRobert Watson 			unp3->unp_addr = (struct sockaddr_un *) sa;
16090d9ce3a1SRobert Watson 			sa = NULL;
16100d9ce3a1SRobert Watson 		}
1611b523ec24SRobert Watson 
16120c1bb4fbSDima Dorfman 		/*
16137a2b450fSEitan Adler 		 * The connector's (client's) credentials are copied from its
16141c381b19SRobert Watson 		 * process structure at the time of connect() (which is now).
16150c1bb4fbSDima Dorfman 		 */
1616a854ed98SJohn Baldwin 		cru2x(td->td_ucred, &unp3->unp_peercred);
16170c1bb4fbSDima Dorfman 		unp3->unp_flags |= UNP_HAVEPC;
1618b523ec24SRobert Watson 
16190c1bb4fbSDima Dorfman 		/*
16201c381b19SRobert Watson 		 * The receiver's (server's) credentials are copied from the
16211c381b19SRobert Watson 		 * unp_peercred member of socket on which the former called
1622e7c33e29SRobert Watson 		 * listen(); uipc_listen() cached that process's credentials
16231c381b19SRobert Watson 		 * at that time so we can use them now.
16240c1bb4fbSDima Dorfman 		 */
16250c1bb4fbSDima Dorfman 		memcpy(&unp->unp_peercred, &unp2->unp_peercred,
16260c1bb4fbSDima Dorfman 		    sizeof(unp->unp_peercred));
16270c1bb4fbSDima Dorfman 		unp->unp_flags |= UNP_HAVEPC;
1628481f8fe8SMaxim Konovalov 		if (unp2->unp_flags & UNP_WANTCRED)
1629481f8fe8SMaxim Konovalov 			unp3->unp_flags |= UNP_WANTCRED;
1630e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp2);
1631779f106aSGleb Smirnoff 		unp2 = unp3;
163275a67bf3SMatt Macy 		unp_pcb_owned_lock2(unp2, unp, freed);
1633e10ef65dSMatt Macy 		if (__predict_false(freed)) {
1634e10ef65dSMatt Macy 			UNP_PCB_UNLOCK(unp2);
1635e10ef65dSMatt Macy 			error = ECONNREFUSED;
1636e10ef65dSMatt Macy 			goto bad2;
1637e10ef65dSMatt Macy 		}
1638335654d7SRobert Watson #ifdef MAC
1639779f106aSGleb Smirnoff 		mac_socketpeer_set_from_socket(so, so2);
1640779f106aSGleb Smirnoff 		mac_socketpeer_set_from_socket(so2, so);
1641335654d7SRobert Watson #endif
1642a3a73490SMatt Macy 	} else {
1643a3a73490SMatt Macy 		if (unp == unp2)
1644a3a73490SMatt Macy 			UNP_PCB_LOCK(unp);
1645a3a73490SMatt Macy 		else
1646e10ef65dSMatt Macy 			unp_pcb_lock2(unp, unp2);
1647a3a73490SMatt Macy 	}
1648779f106aSGleb Smirnoff 	KASSERT(unp2 != NULL && so2 != NULL && unp2->unp_socket == so2 &&
1649779f106aSGleb Smirnoff 	    sotounpcb(so2) == unp2,
1650779f106aSGleb Smirnoff 	    ("%s: unp2 %p so2 %p", __func__, unp2, so2));
16516a2989fdSMatthew N. Dodd 	error = unp_connect2(so, so2, PRU_CONNECT);
1652a3a73490SMatt Macy 	if (unp != unp2)
1653e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp2);
1654e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
16550d9ce3a1SRobert Watson bad2:
165675a67bf3SMatt Macy 	mtx_unlock(vplock);
1657df8bae1dSRodney W. Grimes bad:
165875a67bf3SMatt Macy 	if (vp != NULL) {
1659df8bae1dSRodney W. Grimes 		vput(vp);
166075a67bf3SMatt Macy 	}
16610d9ce3a1SRobert Watson 	free(sa, M_SONAME);
1662e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
16634f1f0ef5SRobert Watson 	unp->unp_flags &= ~UNP_CONNECTING;
1664e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
1665df8bae1dSRodney W. Grimes 	return (error);
1666df8bae1dSRodney W. Grimes }
1667df8bae1dSRodney W. Grimes 
1668db48c0d2SRobert Watson static int
16696a2989fdSMatthew N. Dodd unp_connect2(struct socket *so, struct socket *so2, int req)
1670df8bae1dSRodney W. Grimes {
1671e7c33e29SRobert Watson 	struct unpcb *unp;
1672892af6b9SRobert Watson 	struct unpcb *unp2;
1673df8bae1dSRodney W. Grimes 
1674e7c33e29SRobert Watson 	unp = sotounpcb(so);
1675e7c33e29SRobert Watson 	KASSERT(unp != NULL, ("unp_connect2: unp == NULL"));
1676e7c33e29SRobert Watson 	unp2 = sotounpcb(so2);
1677e7c33e29SRobert Watson 	KASSERT(unp2 != NULL, ("unp_connect2: unp2 == NULL"));
1678e7c33e29SRobert Watson 
1679e7c33e29SRobert Watson 	UNP_PCB_LOCK_ASSERT(unp);
1680e7c33e29SRobert Watson 	UNP_PCB_LOCK_ASSERT(unp2);
16810d9ce3a1SRobert Watson 
1682df8bae1dSRodney W. Grimes 	if (so2->so_type != so->so_type)
1683df8bae1dSRodney W. Grimes 		return (EPROTOTYPE);
1684434ac8b6SMark Johnston 	unp2->unp_flags &= ~UNP_NASCENT;
1685df8bae1dSRodney W. Grimes 	unp->unp_conn = unp2;
168675a67bf3SMatt Macy 	unp_pcb_hold(unp2);
168775a67bf3SMatt Macy 	unp_pcb_hold(unp);
1688df8bae1dSRodney W. Grimes 	switch (so->so_type) {
1689df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
169075a67bf3SMatt Macy 		UNP_REF_LIST_LOCK();
169198271db4SGarrett Wollman 		LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
169275a67bf3SMatt Macy 		UNP_REF_LIST_UNLOCK();
1693df8bae1dSRodney W. Grimes 		soisconnected(so);
1694df8bae1dSRodney W. Grimes 		break;
1695df8bae1dSRodney W. Grimes 
1696df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
169784d61770SRobert Watson 	case SOCK_SEQPACKET:
1698df8bae1dSRodney W. Grimes 		unp2->unp_conn = unp;
16996a2989fdSMatthew N. Dodd 		if (req == PRU_CONNECT &&
17006a2989fdSMatthew N. Dodd 		    ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT))
17016a2989fdSMatthew N. Dodd 			soisconnecting(so);
17026a2989fdSMatthew N. Dodd 		else
1703df8bae1dSRodney W. Grimes 			soisconnected(so);
1704df8bae1dSRodney W. Grimes 		soisconnected(so2);
1705df8bae1dSRodney W. Grimes 		break;
1706df8bae1dSRodney W. Grimes 
1707df8bae1dSRodney W. Grimes 	default:
1708df8bae1dSRodney W. Grimes 		panic("unp_connect2");
1709df8bae1dSRodney W. Grimes 	}
1710df8bae1dSRodney W. Grimes 	return (0);
1711df8bae1dSRodney W. Grimes }
1712df8bae1dSRodney W. Grimes 
1713f708ef1bSPoul-Henning Kamp static void
1714e7c33e29SRobert Watson unp_disconnect(struct unpcb *unp, struct unpcb *unp2)
1715df8bae1dSRodney W. Grimes {
171675a67bf3SMatt Macy 	struct socket *so, *so2;
17174b06dee1SMatt Macy 	int freed __unused;
1718df8bae1dSRodney W. Grimes 
1719e7c33e29SRobert Watson 	KASSERT(unp2 != NULL, ("unp_disconnect: unp2 == NULL"));
17200d9ce3a1SRobert Watson 
1721e7c33e29SRobert Watson 	UNP_PCB_LOCK_ASSERT(unp);
1722e7c33e29SRobert Watson 	UNP_PCB_LOCK_ASSERT(unp2);
1723e7c33e29SRobert Watson 
172475a67bf3SMatt Macy 	if (unp->unp_conn == NULL && unp2->unp_conn == NULL)
172575a67bf3SMatt Macy 		return;
172675a67bf3SMatt Macy 
172775a67bf3SMatt Macy 	MPASS(unp->unp_conn == unp2);
1728fc3fcacfSRobert Watson 	unp->unp_conn = NULL;
172975a67bf3SMatt Macy 	so = unp->unp_socket;
173075a67bf3SMatt Macy 	so2 = unp2->unp_socket;
1731df8bae1dSRodney W. Grimes 	switch (unp->unp_socket->so_type) {
1732df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
173375a67bf3SMatt Macy 		UNP_REF_LIST_LOCK();
173498271db4SGarrett Wollman 		LIST_REMOVE(unp, unp_reflink);
173575a67bf3SMatt Macy 		UNP_REF_LIST_UNLOCK();
173675a67bf3SMatt Macy 		if (so) {
17371b2e3b4bSRobert Watson 			SOCK_LOCK(so);
17381b2e3b4bSRobert Watson 			so->so_state &= ~SS_ISCONNECTED;
17391b2e3b4bSRobert Watson 			SOCK_UNLOCK(so);
174075a67bf3SMatt Macy 		}
1741df8bae1dSRodney W. Grimes 		break;
1742df8bae1dSRodney W. Grimes 
1743df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
174484d61770SRobert Watson 	case SOCK_SEQPACKET:
174575a67bf3SMatt Macy 		if (so)
174675a67bf3SMatt Macy 			soisdisconnected(so);
174775a67bf3SMatt Macy 		MPASS(unp2->unp_conn == unp);
1748fc3fcacfSRobert Watson 		unp2->unp_conn = NULL;
174975a67bf3SMatt Macy 		if (so2)
175075a67bf3SMatt Macy 			soisdisconnected(so2);
1751df8bae1dSRodney W. Grimes 		break;
1752df8bae1dSRodney W. Grimes 	}
17534b06dee1SMatt Macy 	freed = unp_pcb_rele(unp);
175475a67bf3SMatt Macy 	MPASS(freed == 0);
17554b06dee1SMatt Macy 	freed = unp_pcb_rele(unp2);
175675a67bf3SMatt Macy 	MPASS(freed == 0);
1757df8bae1dSRodney W. Grimes }
1758df8bae1dSRodney W. Grimes 
17590d9ce3a1SRobert Watson /*
1760d7924b70SRobert Watson  * unp_pcblist() walks the global list of struct unpcb's to generate a
1761d7924b70SRobert Watson  * pointer list, bumping the refcount on each unpcb.  It then copies them out
1762d7924b70SRobert Watson  * sequentially, validating the generation number on each to see if it has
1763d7924b70SRobert Watson  * been detached.  All of this is necessary because copyout() may sleep on
1764d7924b70SRobert Watson  * disk I/O.
17650d9ce3a1SRobert Watson  */
176698271db4SGarrett Wollman static int
176782d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS)
176898271db4SGarrett Wollman {
176998271db4SGarrett Wollman 	struct unpcb *unp, **unp_list;
177098271db4SGarrett Wollman 	unp_gen_t gencnt;
17718f364875SJulian Elischer 	struct xunpgen *xug;
177298271db4SGarrett Wollman 	struct unp_head *head;
17738f364875SJulian Elischer 	struct xunpcb *xu;
1774d821d364SPedro F. Giffuni 	u_int i;
177544c514b1SPedro F. Giffuni 	int error, freeunp, n;
177698271db4SGarrett Wollman 
177784d61770SRobert Watson 	switch ((intptr_t)arg1) {
177884d61770SRobert Watson 	case SOCK_STREAM:
177984d61770SRobert Watson 		head = &unp_shead;
178084d61770SRobert Watson 		break;
178184d61770SRobert Watson 
178284d61770SRobert Watson 	case SOCK_DGRAM:
178384d61770SRobert Watson 		head = &unp_dhead;
178484d61770SRobert Watson 		break;
178584d61770SRobert Watson 
178684d61770SRobert Watson 	case SOCK_SEQPACKET:
178784d61770SRobert Watson 		head = &unp_sphead;
178884d61770SRobert Watson 		break;
178984d61770SRobert Watson 
179084d61770SRobert Watson 	default:
1791604f19c9SRobert Watson 		panic("unp_pcblist: arg1 %d", (int)(intptr_t)arg1);
179284d61770SRobert Watson 	}
179398271db4SGarrett Wollman 
179498271db4SGarrett Wollman 	/*
179598271db4SGarrett Wollman 	 * The process of preparing the PCB list is too time-consuming and
179698271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
179798271db4SGarrett Wollman 	 */
1798fc3fcacfSRobert Watson 	if (req->oldptr == NULL) {
179998271db4SGarrett Wollman 		n = unp_count;
18008f364875SJulian Elischer 		req->oldidx = 2 * (sizeof *xug)
180198271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xunpcb);
1802e5aeaa0cSDag-Erling Smørgrav 		return (0);
180398271db4SGarrett Wollman 	}
180498271db4SGarrett Wollman 
1805fc3fcacfSRobert Watson 	if (req->newptr != NULL)
1806e5aeaa0cSDag-Erling Smørgrav 		return (EPERM);
180798271db4SGarrett Wollman 
180898271db4SGarrett Wollman 	/*
180998271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
181098271db4SGarrett Wollman 	 */
1811a163d034SWarner Losh 	xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK);
1812779f106aSGleb Smirnoff 	UNP_LINK_RLOCK();
181398271db4SGarrett Wollman 	gencnt = unp_gencnt;
181498271db4SGarrett Wollman 	n = unp_count;
1815779f106aSGleb Smirnoff 	UNP_LINK_RUNLOCK();
181698271db4SGarrett Wollman 
18178f364875SJulian Elischer 	xug->xug_len = sizeof *xug;
18188f364875SJulian Elischer 	xug->xug_count = n;
18198f364875SJulian Elischer 	xug->xug_gen = gencnt;
18208f364875SJulian Elischer 	xug->xug_sogen = so_gencnt;
18218f364875SJulian Elischer 	error = SYSCTL_OUT(req, xug, sizeof *xug);
18228f364875SJulian Elischer 	if (error) {
18238f364875SJulian Elischer 		free(xug, M_TEMP);
1824e5aeaa0cSDag-Erling Smørgrav 		return (error);
18258f364875SJulian Elischer 	}
182698271db4SGarrett Wollman 
1827a163d034SWarner Losh 	unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
182898271db4SGarrett Wollman 
1829779f106aSGleb Smirnoff 	UNP_LINK_RLOCK();
18302e3c8fcbSPoul-Henning Kamp 	for (unp = LIST_FIRST(head), i = 0; unp && i < n;
18312e3c8fcbSPoul-Henning Kamp 	     unp = LIST_NEXT(unp, unp_link)) {
1832e7c33e29SRobert Watson 		UNP_PCB_LOCK(unp);
18338a7d8cc6SRobert Watson 		if (unp->unp_gencnt <= gencnt) {
1834a854ed98SJohn Baldwin 			if (cr_cansee(req->td->td_ucred,
1835e7c33e29SRobert Watson 			    unp->unp_socket->so_cred)) {
1836e7c33e29SRobert Watson 				UNP_PCB_UNLOCK(unp);
18374787fd37SPaul Saab 				continue;
1838e7c33e29SRobert Watson 			}
183998271db4SGarrett Wollman 			unp_list[i++] = unp;
184075a67bf3SMatt Macy 			unp_pcb_hold(unp);
184198271db4SGarrett Wollman 		}
1842e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
18434787fd37SPaul Saab 	}
1844779f106aSGleb Smirnoff 	UNP_LINK_RUNLOCK();
18451c381b19SRobert Watson 	n = i;			/* In case we lost some during malloc. */
184698271db4SGarrett Wollman 
184798271db4SGarrett Wollman 	error = 0;
1848fe2eee82SColin Percival 	xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO);
184998271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
185098271db4SGarrett Wollman 		unp = unp_list[i];
1851e7c33e29SRobert Watson 		UNP_PCB_LOCK(unp);
185275a67bf3SMatt Macy 		freeunp = unp_pcb_rele(unp);
185375a67bf3SMatt Macy 
185475a67bf3SMatt Macy 		if (freeunp == 0 && unp->unp_gencnt <= gencnt) {
18558f364875SJulian Elischer 			xu->xu_len = sizeof *xu;
1856*3a20f06aSBrooks Davis 			xu->xu_unpp = (uintptr_t)unp;
185798271db4SGarrett Wollman 			/*
185898271db4SGarrett Wollman 			 * XXX - need more locking here to protect against
185998271db4SGarrett Wollman 			 * connect/disconnect races for SMP.
186098271db4SGarrett Wollman 			 */
1861fc3fcacfSRobert Watson 			if (unp->unp_addr != NULL)
18628f364875SJulian Elischer 				bcopy(unp->unp_addr, &xu->xu_addr,
186398271db4SGarrett Wollman 				      unp->unp_addr->sun_len);
18640e229f34SGleb Smirnoff 			else
18650e229f34SGleb Smirnoff 				bzero(&xu->xu_addr, sizeof(xu->xu_addr));
1866fc3fcacfSRobert Watson 			if (unp->unp_conn != NULL &&
1867fc3fcacfSRobert Watson 			    unp->unp_conn->unp_addr != NULL)
186898271db4SGarrett Wollman 				bcopy(unp->unp_conn->unp_addr,
18698f364875SJulian Elischer 				      &xu->xu_caddr,
187098271db4SGarrett Wollman 				      unp->unp_conn->unp_addr->sun_len);
18710e229f34SGleb Smirnoff 			else
18720e229f34SGleb Smirnoff 				bzero(&xu->xu_caddr, sizeof(xu->xu_caddr));
1873*3a20f06aSBrooks Davis 			xu->unp_vnode = (uintptr_t)unp->unp_vnode;
1874*3a20f06aSBrooks Davis 			xu->unp_conn = (uintptr_t)unp->unp_conn;
1875*3a20f06aSBrooks Davis 			xu->xu_firstref = (uintptr_t)LIST_FIRST(&unp->unp_refs);
1876*3a20f06aSBrooks Davis 			xu->xu_nextref = (uintptr_t)LIST_NEXT(unp, unp_reflink);
18770e229f34SGleb Smirnoff 			xu->unp_gencnt = unp->unp_gencnt;
18788f364875SJulian Elischer 			sotoxsocket(unp->unp_socket, &xu->xu_socket);
1879e7c33e29SRobert Watson 			UNP_PCB_UNLOCK(unp);
18808f364875SJulian Elischer 			error = SYSCTL_OUT(req, xu, sizeof *xu);
188175a67bf3SMatt Macy 		} else  if (freeunp == 0)
1882e7c33e29SRobert Watson 			UNP_PCB_UNLOCK(unp);
1883e7c33e29SRobert Watson 	}
18848f364875SJulian Elischer 	free(xu, M_TEMP);
188598271db4SGarrett Wollman 	if (!error) {
188698271db4SGarrett Wollman 		/*
18871c381b19SRobert Watson 		 * Give the user an updated idea of our state.  If the
18881c381b19SRobert Watson 		 * generation differs from what we told her before, she knows
18891c381b19SRobert Watson 		 * that something happened while we were processing this
18901c381b19SRobert Watson 		 * request, and it might be necessary to retry.
189198271db4SGarrett Wollman 		 */
18928f364875SJulian Elischer 		xug->xug_gen = unp_gencnt;
18938f364875SJulian Elischer 		xug->xug_sogen = so_gencnt;
18948f364875SJulian Elischer 		xug->xug_count = unp_count;
18958f364875SJulian Elischer 		error = SYSCTL_OUT(req, xug, sizeof *xug);
189698271db4SGarrett Wollman 	}
189798271db4SGarrett Wollman 	free(unp_list, M_TEMP);
18988f364875SJulian Elischer 	free(xug, M_TEMP);
1899e5aeaa0cSDag-Erling Smørgrav 	return (error);
190098271db4SGarrett Wollman }
190198271db4SGarrett Wollman 
19022fee06f0SMatthew D Fleming SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLTYPE_OPAQUE | CTLFLAG_RD,
19032fee06f0SMatthew D Fleming     (void *)(intptr_t)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
190498271db4SGarrett Wollman     "List of active local datagram sockets");
19052fee06f0SMatthew D Fleming SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLTYPE_OPAQUE | CTLFLAG_RD,
19062fee06f0SMatthew D Fleming     (void *)(intptr_t)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
190798271db4SGarrett Wollman     "List of active local stream sockets");
19082fee06f0SMatthew D Fleming SYSCTL_PROC(_net_local_seqpacket, OID_AUTO, pcblist,
19092fee06f0SMatthew D Fleming     CTLTYPE_OPAQUE | CTLFLAG_RD,
19102fee06f0SMatthew D Fleming     (void *)(intptr_t)SOCK_SEQPACKET, 0, unp_pcblist, "S,xunpcb",
191184d61770SRobert Watson     "List of active local seqpacket sockets");
191298271db4SGarrett Wollman 
1913f708ef1bSPoul-Henning Kamp static void
1914892af6b9SRobert Watson unp_shutdown(struct unpcb *unp)
1915df8bae1dSRodney W. Grimes {
1916e7c33e29SRobert Watson 	struct unpcb *unp2;
1917df8bae1dSRodney W. Grimes 	struct socket *so;
1918df8bae1dSRodney W. Grimes 
1919e7c33e29SRobert Watson 	UNP_PCB_LOCK_ASSERT(unp);
19200d9ce3a1SRobert Watson 
1921e7c33e29SRobert Watson 	unp2 = unp->unp_conn;
192284d61770SRobert Watson 	if ((unp->unp_socket->so_type == SOCK_STREAM ||
192384d61770SRobert Watson 	    (unp->unp_socket->so_type == SOCK_SEQPACKET)) && unp2 != NULL) {
1924e7c33e29SRobert Watson 		so = unp2->unp_socket;
1925e7c33e29SRobert Watson 		if (so != NULL)
1926df8bae1dSRodney W. Grimes 			socantrcvmore(so);
1927df8bae1dSRodney W. Grimes 	}
1928e7c33e29SRobert Watson }
1929df8bae1dSRodney W. Grimes 
1930f708ef1bSPoul-Henning Kamp static void
1931afc055d9SEd Schouten unp_drop(struct unpcb *unp)
1932df8bae1dSRodney W. Grimes {
1933df8bae1dSRodney W. Grimes 	struct socket *so = unp->unp_socket;
1934e7c33e29SRobert Watson 	struct unpcb *unp2;
193575a67bf3SMatt Macy 	int freed;
19360d9ce3a1SRobert Watson 
1937afc055d9SEd Schouten 	/*
1938afc055d9SEd Schouten 	 * Regardless of whether the socket's peer dropped the connection
1939afc055d9SEd Schouten 	 * with this socket by aborting or disconnecting, POSIX requires
1940afc055d9SEd Schouten 	 * that ECONNRESET is returned.
1941afc055d9SEd Schouten 	 */
194275a67bf3SMatt Macy 	/* acquire a reference so that unp isn't freed from underneath us */
194375a67bf3SMatt Macy 
194475a67bf3SMatt Macy 	UNP_PCB_LOCK(unp);
194575a67bf3SMatt Macy 	if (so)
1946afc055d9SEd Schouten 		so->so_error = ECONNRESET;
1947e7c33e29SRobert Watson 	unp2 = unp->unp_conn;
1948acf9fd05SMatt Macy 	if (unp2 == unp) {
1949acf9fd05SMatt Macy 		unp_disconnect(unp, unp2);
1950acf9fd05SMatt Macy 	} else if (unp2 != NULL) {
195175a67bf3SMatt Macy 		unp_pcb_hold(unp2);
195275a67bf3SMatt Macy 		unp_pcb_owned_lock2(unp, unp2, freed);
1953e7c33e29SRobert Watson 		unp_disconnect(unp, unp2);
195475a67bf3SMatt Macy 		if (unp_pcb_rele(unp2) == 0)
1955e7c33e29SRobert Watson 			UNP_PCB_UNLOCK(unp2);
1956df8bae1dSRodney W. Grimes 	}
195775a67bf3SMatt Macy 	if (unp_pcb_rele(unp) == 0)
195875a67bf3SMatt Macy 		UNP_PCB_UNLOCK(unp);
195975a67bf3SMatt Macy }
1960df8bae1dSRodney W. Grimes 
19612bc21ed9SDavid Malone static void
19628cb539f1SPawel Jakub Dawidek unp_freerights(struct filedescent **fdep, int fdcount)
1963df8bae1dSRodney W. Grimes {
19642bc21ed9SDavid Malone 	struct file *fp;
19652609222aSPawel Jakub Dawidek 	int i;
1966df8bae1dSRodney W. Grimes 
196782e825c4SGleb Smirnoff 	KASSERT(fdcount > 0, ("%s: fdcount %d", __func__, fdcount));
196882e825c4SGleb Smirnoff 
19698cb539f1SPawel Jakub Dawidek 	for (i = 0; i < fdcount; i++) {
19708cb539f1SPawel Jakub Dawidek 		fp = fdep[i]->fde_file;
19718cb539f1SPawel Jakub Dawidek 		filecaps_free(&fdep[i]->fde_caps);
19728692c025SYoshinobu Inoue 		unp_discard(fp);
1973df8bae1dSRodney W. Grimes 	}
19748cb539f1SPawel Jakub Dawidek 	free(fdep[0], M_FILECAPS);
19752bc21ed9SDavid Malone }
19762bc21ed9SDavid Malone 
19770b36cd25SRobert Watson static int
1978c2e3c52eSJilles Tjoelker unp_externalize(struct mbuf *control, struct mbuf **controlp, int flags)
19792bc21ed9SDavid Malone {
19802bc21ed9SDavid Malone 	struct thread *td = curthread;		/* XXX */
19812bc21ed9SDavid Malone 	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
19822bc21ed9SDavid Malone 	int i;
19832bc21ed9SDavid Malone 	int *fdp;
19842609222aSPawel Jakub Dawidek 	struct filedesc *fdesc = td->td_proc->p_fd;
1985ea31808cSMateusz Guzik 	struct filedescent **fdep;
19862bc21ed9SDavid Malone 	void *data;
19872bc21ed9SDavid Malone 	socklen_t clen = control->m_len, datalen;
19882bc21ed9SDavid Malone 	int error, newfds;
19892bc21ed9SDavid Malone 	u_int newlen;
19902bc21ed9SDavid Malone 
19913dab55bcSRobert Watson 	UNP_LINK_UNLOCK_ASSERT();
19924c5bc1caSRobert Watson 
19932bc21ed9SDavid Malone 	error = 0;
19942bc21ed9SDavid Malone 	if (controlp != NULL) /* controlp == NULL => free control messages */
19952bc21ed9SDavid Malone 		*controlp = NULL;
19962bc21ed9SDavid Malone 	while (cm != NULL) {
19972bc21ed9SDavid Malone 		if (sizeof(*cm) > clen || cm->cmsg_len > clen) {
19982bc21ed9SDavid Malone 			error = EINVAL;
19992bc21ed9SDavid Malone 			break;
20002bc21ed9SDavid Malone 		}
20012bc21ed9SDavid Malone 		data = CMSG_DATA(cm);
20022bc21ed9SDavid Malone 		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
20032bc21ed9SDavid Malone 		if (cm->cmsg_level == SOL_SOCKET
20042bc21ed9SDavid Malone 		    && cm->cmsg_type == SCM_RIGHTS) {
20052609222aSPawel Jakub Dawidek 			newfds = datalen / sizeof(*fdep);
200682e825c4SGleb Smirnoff 			if (newfds == 0)
200782e825c4SGleb Smirnoff 				goto next;
20082609222aSPawel Jakub Dawidek 			fdep = data;
20092bc21ed9SDavid Malone 
2010e2f9a08bSOlivier Houchard 			/* If we're not outputting the descriptors free them. */
20112bc21ed9SDavid Malone 			if (error || controlp == NULL) {
20122609222aSPawel Jakub Dawidek 				unp_freerights(fdep, newfds);
20132bc21ed9SDavid Malone 				goto next;
20142bc21ed9SDavid Malone 			}
20152609222aSPawel Jakub Dawidek 			FILEDESC_XLOCK(fdesc);
201660a5ef26SRobert Watson 
2017ed5b7817SJulian Elischer 			/*
20181c381b19SRobert Watson 			 * Now change each pointer to an fd in the global
20191c381b19SRobert Watson 			 * table to an integer that is the index to the local
20201c381b19SRobert Watson 			 * fd table entry that we set up to point to the
20211c381b19SRobert Watson 			 * global one we are transferring.
2022ed5b7817SJulian Elischer 			 */
20232bc21ed9SDavid Malone 			newlen = newfds * sizeof(int);
20242bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, newlen,
20252bc21ed9SDavid Malone 			    SCM_RIGHTS, SOL_SOCKET);
20262bc21ed9SDavid Malone 			if (*controlp == NULL) {
20272609222aSPawel Jakub Dawidek 				FILEDESC_XUNLOCK(fdesc);
20282bc21ed9SDavid Malone 				error = E2BIG;
20292609222aSPawel Jakub Dawidek 				unp_freerights(fdep, newfds);
20302bc21ed9SDavid Malone 				goto next;
20312bc21ed9SDavid Malone 			}
20322bc21ed9SDavid Malone 
20332bc21ed9SDavid Malone 			fdp = (int *)
20342bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
2035db8f33fdSMateusz Guzik 			if (fdallocn(td, 0, fdp, newfds) != 0) {
20363331a33aSMateusz Guzik 				FILEDESC_XUNLOCK(fdesc);
2037db8f33fdSMateusz Guzik 				error = EMSGSIZE;
2038db8f33fdSMateusz Guzik 				unp_freerights(fdep, newfds);
2039db8f33fdSMateusz Guzik 				m_freem(*controlp);
2040db8f33fdSMateusz Guzik 				*controlp = NULL;
2041db8f33fdSMateusz Guzik 				goto next;
2042db8f33fdSMateusz Guzik 			}
20438cb539f1SPawel Jakub Dawidek 			for (i = 0; i < newfds; i++, fdp++) {
2044ea31808cSMateusz Guzik 				_finstall(fdesc, fdep[i]->fde_file, *fdp,
2045ea31808cSMateusz Guzik 				    (flags & MSG_CMSG_CLOEXEC) != 0 ? UF_EXCLOSE : 0,
2046ea31808cSMateusz Guzik 				    &fdep[i]->fde_caps);
2047ea31808cSMateusz Guzik 				unp_externalize_fp(fdep[i]->fde_file);
2048df8bae1dSRodney W. Grimes 			}
20492609222aSPawel Jakub Dawidek 			FILEDESC_XUNLOCK(fdesc);
20508cb539f1SPawel Jakub Dawidek 			free(fdep[0], M_FILECAPS);
20511c381b19SRobert Watson 		} else {
20521c381b19SRobert Watson 			/* We can just copy anything else across. */
20532bc21ed9SDavid Malone 			if (error || controlp == NULL)
20542bc21ed9SDavid Malone 				goto next;
20552bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, datalen,
20562bc21ed9SDavid Malone 			    cm->cmsg_type, cm->cmsg_level);
20572bc21ed9SDavid Malone 			if (*controlp == NULL) {
20582bc21ed9SDavid Malone 				error = ENOBUFS;
20592bc21ed9SDavid Malone 				goto next;
20602bc21ed9SDavid Malone 			}
20612bc21ed9SDavid Malone 			bcopy(data,
20622bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *)),
20632bc21ed9SDavid Malone 			    datalen);
20642bc21ed9SDavid Malone 		}
20652bc21ed9SDavid Malone 		controlp = &(*controlp)->m_next;
20662bc21ed9SDavid Malone 
20672bc21ed9SDavid Malone next:
20682bc21ed9SDavid Malone 		if (CMSG_SPACE(datalen) < clen) {
20692bc21ed9SDavid Malone 			clen -= CMSG_SPACE(datalen);
20702bc21ed9SDavid Malone 			cm = (struct cmsghdr *)
20712bc21ed9SDavid Malone 			    ((caddr_t)cm + CMSG_SPACE(datalen));
20728692c025SYoshinobu Inoue 		} else {
20732bc21ed9SDavid Malone 			clen = 0;
20742bc21ed9SDavid Malone 			cm = NULL;
20758692c025SYoshinobu Inoue 		}
20768692c025SYoshinobu Inoue 	}
20778692c025SYoshinobu Inoue 
20782bc21ed9SDavid Malone 	m_freem(control);
20792bc21ed9SDavid Malone 	return (error);
2080df8bae1dSRodney W. Grimes }
2081df8bae1dSRodney W. Grimes 
20824f590175SPaul Saab static void
20834f590175SPaul Saab unp_zone_change(void *tag)
20844f590175SPaul Saab {
20854f590175SPaul Saab 
20864f590175SPaul Saab 	uma_zone_set_max(unp_zone, maxsockets);
20874f590175SPaul Saab }
20884f590175SPaul Saab 
20890b36cd25SRobert Watson static void
209098271db4SGarrett Wollman unp_init(void)
209198271db4SGarrett Wollman {
20921c381b19SRobert Watson 
209321ca7b57SMarko Zec #ifdef VIMAGE
209421ca7b57SMarko Zec 	if (!IS_DEFAULT_VNET(curvnet))
209521ca7b57SMarko Zec 		return;
209621ca7b57SMarko Zec #endif
20979e9d298aSJeff Roberson 	unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL,
209875a67bf3SMatt Macy 	    NULL, NULL, UMA_ALIGN_CACHE, 0);
2099fc3fcacfSRobert Watson 	if (unp_zone == NULL)
210098271db4SGarrett Wollman 		panic("unp_init");
21014f590175SPaul Saab 	uma_zone_set_max(unp_zone, maxsockets);
21026e0b6746SPawel Jakub Dawidek 	uma_zone_set_warning(unp_zone, "kern.ipc.maxsockets limit reached");
21034f590175SPaul Saab 	EVENTHANDLER_REGISTER(maxsockets_change, unp_zone_change,
21044f590175SPaul Saab 	    NULL, EVENTHANDLER_PRI_ANY);
210598271db4SGarrett Wollman 	LIST_INIT(&unp_dhead);
210698271db4SGarrett Wollman 	LIST_INIT(&unp_shead);
210784d61770SRobert Watson 	LIST_INIT(&unp_sphead);
21080cb64678SKonstantin Belousov 	SLIST_INIT(&unp_defers);
2109daee0f0bSKonstantin Belousov 	TIMEOUT_TASK_INIT(taskqueue_thread, &unp_gc_task, 0, unp_gc, NULL);
21100cb64678SKonstantin Belousov 	TASK_INIT(&unp_defer_task, 0, unp_process_defers, NULL);
21113dab55bcSRobert Watson 	UNP_LINK_LOCK_INIT();
21120cb64678SKonstantin Belousov 	UNP_DEFERRED_LOCK_INIT();
211398271db4SGarrett Wollman }
211498271db4SGarrett Wollman 
2115f708ef1bSPoul-Henning Kamp static int
2116892af6b9SRobert Watson unp_internalize(struct mbuf **controlp, struct thread *td)
2117df8bae1dSRodney W. Grimes {
21182bc21ed9SDavid Malone 	struct mbuf *control = *controlp;
2119b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
21202609222aSPawel Jakub Dawidek 	struct filedesc *fdesc = p->p_fd;
2121ab15d803SSergey Kandaurov 	struct bintime *bt;
21222bc21ed9SDavid Malone 	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
21232bc21ed9SDavid Malone 	struct cmsgcred *cmcred;
21248cb539f1SPawel Jakub Dawidek 	struct filedescent *fde, **fdep, *fdev;
21252bc21ed9SDavid Malone 	struct file *fp;
21262bc21ed9SDavid Malone 	struct timeval *tv;
2127339efd75SMaxim Sobolev 	struct timespec *ts;
21286a1cf96bSMateusz Guzik 	int i, *fdp;
21292bc21ed9SDavid Malone 	void *data;
21302bc21ed9SDavid Malone 	socklen_t clen = control->m_len, datalen;
21312bc21ed9SDavid Malone 	int error, oldfds;
21328692c025SYoshinobu Inoue 	u_int newlen;
2133df8bae1dSRodney W. Grimes 
21343dab55bcSRobert Watson 	UNP_LINK_UNLOCK_ASSERT();
21354c5bc1caSRobert Watson 
21362bc21ed9SDavid Malone 	error = 0;
21372bc21ed9SDavid Malone 	*controlp = NULL;
21382bc21ed9SDavid Malone 	while (cm != NULL) {
21392bc21ed9SDavid Malone 		if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET
2140de966666SMateusz Guzik 		    || cm->cmsg_len > clen || cm->cmsg_len < sizeof(*cm)) {
21412bc21ed9SDavid Malone 			error = EINVAL;
21422bc21ed9SDavid Malone 			goto out;
21432bc21ed9SDavid Malone 		}
21442bc21ed9SDavid Malone 		data = CMSG_DATA(cm);
21452bc21ed9SDavid Malone 		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
21462bc21ed9SDavid Malone 
21472bc21ed9SDavid Malone 		switch (cm->cmsg_type) {
21480b788fa1SBill Paul 		/*
21490b788fa1SBill Paul 		 * Fill in credential information.
21500b788fa1SBill Paul 		 */
21512bc21ed9SDavid Malone 		case SCM_CREDS:
21522bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, sizeof(*cmcred),
21532bc21ed9SDavid Malone 			    SCM_CREDS, SOL_SOCKET);
21542bc21ed9SDavid Malone 			if (*controlp == NULL) {
21552bc21ed9SDavid Malone 				error = ENOBUFS;
21562bc21ed9SDavid Malone 				goto out;
21572bc21ed9SDavid Malone 			}
21582bc21ed9SDavid Malone 			cmcred = (struct cmsgcred *)
21592bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
21600b788fa1SBill Paul 			cmcred->cmcred_pid = p->p_pid;
2161a854ed98SJohn Baldwin 			cmcred->cmcred_uid = td->td_ucred->cr_ruid;
2162a854ed98SJohn Baldwin 			cmcred->cmcred_gid = td->td_ucred->cr_rgid;
2163a854ed98SJohn Baldwin 			cmcred->cmcred_euid = td->td_ucred->cr_uid;
2164a854ed98SJohn Baldwin 			cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups,
21650b788fa1SBill Paul 			    CMGROUP_MAX);
21660b788fa1SBill Paul 			for (i = 0; i < cmcred->cmcred_ngroups; i++)
21672bc21ed9SDavid Malone 				cmcred->cmcred_groups[i] =
2168a854ed98SJohn Baldwin 				    td->td_ucred->cr_groups[i];
21692bc21ed9SDavid Malone 			break;
21700b788fa1SBill Paul 
21712bc21ed9SDavid Malone 		case SCM_RIGHTS:
21722bc21ed9SDavid Malone 			oldfds = datalen / sizeof (int);
217382e825c4SGleb Smirnoff 			if (oldfds == 0)
217482e825c4SGleb Smirnoff 				break;
2175ed5b7817SJulian Elischer 			/*
21761c381b19SRobert Watson 			 * Check that all the FDs passed in refer to legal
21771c381b19SRobert Watson 			 * files.  If not, reject the entire operation.
2178ed5b7817SJulian Elischer 			 */
21792bc21ed9SDavid Malone 			fdp = data;
21802609222aSPawel Jakub Dawidek 			FILEDESC_SLOCK(fdesc);
21816a1cf96bSMateusz Guzik 			for (i = 0; i < oldfds; i++, fdp++) {
21826a1cf96bSMateusz Guzik 				fp = fget_locked(fdesc, *fdp);
21836a1cf96bSMateusz Guzik 				if (fp == NULL) {
21842609222aSPawel Jakub Dawidek 					FILEDESC_SUNLOCK(fdesc);
21852bc21ed9SDavid Malone 					error = EBADF;
21862bc21ed9SDavid Malone 					goto out;
21872bc21ed9SDavid Malone 				}
2188e7d6662fSAlfred Perlstein 				if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) {
21892609222aSPawel Jakub Dawidek 					FILEDESC_SUNLOCK(fdesc);
2190e7d6662fSAlfred Perlstein 					error = EOPNOTSUPP;
2191e7d6662fSAlfred Perlstein 					goto out;
2192e7d6662fSAlfred Perlstein 				}
2193e7d6662fSAlfred Perlstein 
2194df8bae1dSRodney W. Grimes 			}
21955e3f7694SRobert Watson 
2196ed5b7817SJulian Elischer 			/*
21970b36cd25SRobert Watson 			 * Now replace the integer FDs with pointers to the
21982609222aSPawel Jakub Dawidek 			 * file structure and capability rights.
2199ed5b7817SJulian Elischer 			 */
22008cb539f1SPawel Jakub Dawidek 			newlen = oldfds * sizeof(fdep[0]);
22012bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, newlen,
22022bc21ed9SDavid Malone 			    SCM_RIGHTS, SOL_SOCKET);
22032bc21ed9SDavid Malone 			if (*controlp == NULL) {
22042609222aSPawel Jakub Dawidek 				FILEDESC_SUNLOCK(fdesc);
22052bc21ed9SDavid Malone 				error = E2BIG;
22062bc21ed9SDavid Malone 				goto out;
22078692c025SYoshinobu Inoue 			}
22082bc21ed9SDavid Malone 			fdp = data;
22098cb539f1SPawel Jakub Dawidek 			fdep = (struct filedescent **)
22102bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
22118cb539f1SPawel Jakub Dawidek 			fdev = malloc(sizeof(*fdev) * oldfds, M_FILECAPS,
22128cb539f1SPawel Jakub Dawidek 			    M_WAITOK);
22138cb539f1SPawel Jakub Dawidek 			for (i = 0; i < oldfds; i++, fdev++, fdp++) {
22142609222aSPawel Jakub Dawidek 				fde = &fdesc->fd_ofiles[*fdp];
22158cb539f1SPawel Jakub Dawidek 				fdep[i] = fdev;
22168cb539f1SPawel Jakub Dawidek 				fdep[i]->fde_file = fde->fde_file;
22178cb539f1SPawel Jakub Dawidek 				filecaps_copy(&fde->fde_caps,
2218d7832811SMateusz Guzik 				    &fdep[i]->fde_caps, true);
22198cb539f1SPawel Jakub Dawidek 				unp_internalize_fp(fdep[i]->fde_file);
2220df8bae1dSRodney W. Grimes 			}
22212609222aSPawel Jakub Dawidek 			FILEDESC_SUNLOCK(fdesc);
22222bc21ed9SDavid Malone 			break;
22232bc21ed9SDavid Malone 
22242bc21ed9SDavid Malone 		case SCM_TIMESTAMP:
22252bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, sizeof(*tv),
22262bc21ed9SDavid Malone 			    SCM_TIMESTAMP, SOL_SOCKET);
22272bc21ed9SDavid Malone 			if (*controlp == NULL) {
22282bc21ed9SDavid Malone 				error = ENOBUFS;
22292bc21ed9SDavid Malone 				goto out;
22308692c025SYoshinobu Inoue 			}
22312bc21ed9SDavid Malone 			tv = (struct timeval *)
22322bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
22332bc21ed9SDavid Malone 			microtime(tv);
22342bc21ed9SDavid Malone 			break;
22352bc21ed9SDavid Malone 
2236ab15d803SSergey Kandaurov 		case SCM_BINTIME:
2237ab15d803SSergey Kandaurov 			*controlp = sbcreatecontrol(NULL, sizeof(*bt),
2238ab15d803SSergey Kandaurov 			    SCM_BINTIME, SOL_SOCKET);
2239ab15d803SSergey Kandaurov 			if (*controlp == NULL) {
2240ab15d803SSergey Kandaurov 				error = ENOBUFS;
2241ab15d803SSergey Kandaurov 				goto out;
2242ab15d803SSergey Kandaurov 			}
2243ab15d803SSergey Kandaurov 			bt = (struct bintime *)
2244ab15d803SSergey Kandaurov 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
2245ab15d803SSergey Kandaurov 			bintime(bt);
2246ab15d803SSergey Kandaurov 			break;
2247ab15d803SSergey Kandaurov 
2248339efd75SMaxim Sobolev 		case SCM_REALTIME:
2249339efd75SMaxim Sobolev 			*controlp = sbcreatecontrol(NULL, sizeof(*ts),
2250339efd75SMaxim Sobolev 			    SCM_REALTIME, SOL_SOCKET);
2251339efd75SMaxim Sobolev 			if (*controlp == NULL) {
2252339efd75SMaxim Sobolev 				error = ENOBUFS;
2253339efd75SMaxim Sobolev 				goto out;
2254339efd75SMaxim Sobolev 			}
2255339efd75SMaxim Sobolev 			ts = (struct timespec *)
2256339efd75SMaxim Sobolev 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
2257339efd75SMaxim Sobolev 			nanotime(ts);
2258339efd75SMaxim Sobolev 			break;
2259339efd75SMaxim Sobolev 
2260339efd75SMaxim Sobolev 		case SCM_MONOTONIC:
2261339efd75SMaxim Sobolev 			*controlp = sbcreatecontrol(NULL, sizeof(*ts),
2262339efd75SMaxim Sobolev 			    SCM_MONOTONIC, SOL_SOCKET);
2263339efd75SMaxim Sobolev 			if (*controlp == NULL) {
2264339efd75SMaxim Sobolev 				error = ENOBUFS;
2265339efd75SMaxim Sobolev 				goto out;
2266339efd75SMaxim Sobolev 			}
2267339efd75SMaxim Sobolev 			ts = (struct timespec *)
2268339efd75SMaxim Sobolev 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
2269339efd75SMaxim Sobolev 			nanouptime(ts);
2270339efd75SMaxim Sobolev 			break;
2271339efd75SMaxim Sobolev 
22722bc21ed9SDavid Malone 		default:
22732bc21ed9SDavid Malone 			error = EINVAL;
22742bc21ed9SDavid Malone 			goto out;
22752bc21ed9SDavid Malone 		}
22762bc21ed9SDavid Malone 
22772bc21ed9SDavid Malone 		controlp = &(*controlp)->m_next;
22782bc21ed9SDavid Malone 		if (CMSG_SPACE(datalen) < clen) {
22792bc21ed9SDavid Malone 			clen -= CMSG_SPACE(datalen);
22802bc21ed9SDavid Malone 			cm = (struct cmsghdr *)
22812bc21ed9SDavid Malone 			    ((caddr_t)cm + CMSG_SPACE(datalen));
22822bc21ed9SDavid Malone 		} else {
22832bc21ed9SDavid Malone 			clen = 0;
22842bc21ed9SDavid Malone 			cm = NULL;
22852bc21ed9SDavid Malone 		}
22862bc21ed9SDavid Malone 	}
22872bc21ed9SDavid Malone 
22882bc21ed9SDavid Malone out:
22892bc21ed9SDavid Malone 	m_freem(control);
22902bc21ed9SDavid Malone 	return (error);
2291df8bae1dSRodney W. Grimes }
2292df8bae1dSRodney W. Grimes 
22935b950deaSRobert Watson static struct mbuf *
22946a2989fdSMatthew N. Dodd unp_addsockcred(struct thread *td, struct mbuf *control)
22956a2989fdSMatthew N. Dodd {
229670df31f4SMaxim Konovalov 	struct mbuf *m, *n, *n_prev;
22976a2989fdSMatthew N. Dodd 	struct sockcred *sc;
229870df31f4SMaxim Konovalov 	const struct cmsghdr *cm;
22996a2989fdSMatthew N. Dodd 	int ngroups;
23006a2989fdSMatthew N. Dodd 	int i;
23016a2989fdSMatthew N. Dodd 
23026a2989fdSMatthew N. Dodd 	ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX);
23036a2989fdSMatthew N. Dodd 	m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET);
23046a2989fdSMatthew N. Dodd 	if (m == NULL)
23056a2989fdSMatthew N. Dodd 		return (control);
23066a2989fdSMatthew N. Dodd 
23076a2989fdSMatthew N. Dodd 	sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *));
23086a2989fdSMatthew N. Dodd 	sc->sc_uid = td->td_ucred->cr_ruid;
23096a2989fdSMatthew N. Dodd 	sc->sc_euid = td->td_ucred->cr_uid;
23106a2989fdSMatthew N. Dodd 	sc->sc_gid = td->td_ucred->cr_rgid;
23116a2989fdSMatthew N. Dodd 	sc->sc_egid = td->td_ucred->cr_gid;
23126a2989fdSMatthew N. Dodd 	sc->sc_ngroups = ngroups;
23136a2989fdSMatthew N. Dodd 	for (i = 0; i < sc->sc_ngroups; i++)
23146a2989fdSMatthew N. Dodd 		sc->sc_groups[i] = td->td_ucred->cr_groups[i];
23156a2989fdSMatthew N. Dodd 
23166a2989fdSMatthew N. Dodd 	/*
23171c381b19SRobert Watson 	 * Unlink SCM_CREDS control messages (struct cmsgcred), since just
23181c381b19SRobert Watson 	 * created SCM_CREDS control message (struct sockcred) has another
23191c381b19SRobert Watson 	 * format.
23206a2989fdSMatthew N. Dodd 	 */
232170df31f4SMaxim Konovalov 	if (control != NULL)
232270df31f4SMaxim Konovalov 		for (n = control, n_prev = NULL; n != NULL;) {
232370df31f4SMaxim Konovalov 			cm = mtod(n, struct cmsghdr *);
232470df31f4SMaxim Konovalov     			if (cm->cmsg_level == SOL_SOCKET &&
232570df31f4SMaxim Konovalov 			    cm->cmsg_type == SCM_CREDS) {
232670df31f4SMaxim Konovalov     				if (n_prev == NULL)
232770df31f4SMaxim Konovalov 					control = n->m_next;
232870df31f4SMaxim Konovalov 				else
232970df31f4SMaxim Konovalov 					n_prev->m_next = n->m_next;
233070df31f4SMaxim Konovalov 				n = m_free(n);
233170df31f4SMaxim Konovalov 			} else {
233270df31f4SMaxim Konovalov 				n_prev = n;
233370df31f4SMaxim Konovalov 				n = n->m_next;
233470df31f4SMaxim Konovalov 			}
233570df31f4SMaxim Konovalov 		}
23366a2989fdSMatthew N. Dodd 
233770df31f4SMaxim Konovalov 	/* Prepend it to the head. */
233870df31f4SMaxim Konovalov 	m->m_next = control;
233970df31f4SMaxim Konovalov 	return (m);
23406a2989fdSMatthew N. Dodd }
23416a2989fdSMatthew N. Dodd 
2342397c19d1SJeff Roberson static struct unpcb *
2343397c19d1SJeff Roberson fptounp(struct file *fp)
2344397c19d1SJeff Roberson {
2345397c19d1SJeff Roberson 	struct socket *so;
2346397c19d1SJeff Roberson 
2347397c19d1SJeff Roberson 	if (fp->f_type != DTYPE_SOCKET)
2348397c19d1SJeff Roberson 		return (NULL);
2349397c19d1SJeff Roberson 	if ((so = fp->f_data) == NULL)
2350397c19d1SJeff Roberson 		return (NULL);
2351397c19d1SJeff Roberson 	if (so->so_proto->pr_domain != &localdomain)
2352397c19d1SJeff Roberson 		return (NULL);
2353397c19d1SJeff Roberson 	return sotounpcb(so);
2354397c19d1SJeff Roberson }
2355397c19d1SJeff Roberson 
2356397c19d1SJeff Roberson static void
2357397c19d1SJeff Roberson unp_discard(struct file *fp)
2358397c19d1SJeff Roberson {
23590cb64678SKonstantin Belousov 	struct unp_defer *dr;
2360397c19d1SJeff Roberson 
23610cb64678SKonstantin Belousov 	if (unp_externalize_fp(fp)) {
23620cb64678SKonstantin Belousov 		dr = malloc(sizeof(*dr), M_TEMP, M_WAITOK);
23630cb64678SKonstantin Belousov 		dr->ud_fp = fp;
23640cb64678SKonstantin Belousov 		UNP_DEFERRED_LOCK();
23650cb64678SKonstantin Belousov 		SLIST_INSERT_HEAD(&unp_defers, dr, ud_link);
23660cb64678SKonstantin Belousov 		UNP_DEFERRED_UNLOCK();
23670cb64678SKonstantin Belousov 		atomic_add_int(&unp_defers_count, 1);
23680cb64678SKonstantin Belousov 		taskqueue_enqueue(taskqueue_thread, &unp_defer_task);
23690cb64678SKonstantin Belousov 	} else
2370397c19d1SJeff Roberson 		(void) closef(fp, (struct thread *)NULL);
2371397c19d1SJeff Roberson }
2372397c19d1SJeff Roberson 
2373397c19d1SJeff Roberson static void
23740cb64678SKonstantin Belousov unp_process_defers(void *arg __unused, int pending)
23750cb64678SKonstantin Belousov {
23760cb64678SKonstantin Belousov 	struct unp_defer *dr;
23770cb64678SKonstantin Belousov 	SLIST_HEAD(, unp_defer) drl;
23780cb64678SKonstantin Belousov 	int count;
23790cb64678SKonstantin Belousov 
23800cb64678SKonstantin Belousov 	SLIST_INIT(&drl);
23810cb64678SKonstantin Belousov 	for (;;) {
23820cb64678SKonstantin Belousov 		UNP_DEFERRED_LOCK();
23830cb64678SKonstantin Belousov 		if (SLIST_FIRST(&unp_defers) == NULL) {
23840cb64678SKonstantin Belousov 			UNP_DEFERRED_UNLOCK();
23850cb64678SKonstantin Belousov 			break;
23860cb64678SKonstantin Belousov 		}
23870cb64678SKonstantin Belousov 		SLIST_SWAP(&unp_defers, &drl, unp_defer);
23880cb64678SKonstantin Belousov 		UNP_DEFERRED_UNLOCK();
23890cb64678SKonstantin Belousov 		count = 0;
23900cb64678SKonstantin Belousov 		while ((dr = SLIST_FIRST(&drl)) != NULL) {
23910cb64678SKonstantin Belousov 			SLIST_REMOVE_HEAD(&drl, ud_link);
23920cb64678SKonstantin Belousov 			closef(dr->ud_fp, NULL);
23930cb64678SKonstantin Belousov 			free(dr, M_TEMP);
23940cb64678SKonstantin Belousov 			count++;
23950cb64678SKonstantin Belousov 		}
23960cb64678SKonstantin Belousov 		atomic_add_int(&unp_defers_count, -count);
23970cb64678SKonstantin Belousov 	}
23980cb64678SKonstantin Belousov }
23990cb64678SKonstantin Belousov 
24000cb64678SKonstantin Belousov static void
2401397c19d1SJeff Roberson unp_internalize_fp(struct file *fp)
2402397c19d1SJeff Roberson {
2403397c19d1SJeff Roberson 	struct unpcb *unp;
2404397c19d1SJeff Roberson 
24053dab55bcSRobert Watson 	UNP_LINK_WLOCK();
2406397c19d1SJeff Roberson 	if ((unp = fptounp(fp)) != NULL) {
2407397c19d1SJeff Roberson 		unp->unp_file = fp;
2408397c19d1SJeff Roberson 		unp->unp_msgcount++;
2409397c19d1SJeff Roberson 	}
241041e0f66dSJeff Roberson 	fhold(fp);
2411397c19d1SJeff Roberson 	unp_rights++;
24123dab55bcSRobert Watson 	UNP_LINK_WUNLOCK();
2413397c19d1SJeff Roberson }
2414397c19d1SJeff Roberson 
24150cb64678SKonstantin Belousov static int
2416397c19d1SJeff Roberson unp_externalize_fp(struct file *fp)
2417397c19d1SJeff Roberson {
2418397c19d1SJeff Roberson 	struct unpcb *unp;
24190cb64678SKonstantin Belousov 	int ret;
2420397c19d1SJeff Roberson 
24213dab55bcSRobert Watson 	UNP_LINK_WLOCK();
24220cb64678SKonstantin Belousov 	if ((unp = fptounp(fp)) != NULL) {
2423397c19d1SJeff Roberson 		unp->unp_msgcount--;
24240cb64678SKonstantin Belousov 		ret = 1;
24250cb64678SKonstantin Belousov 	} else
24260cb64678SKonstantin Belousov 		ret = 0;
2427397c19d1SJeff Roberson 	unp_rights--;
24283dab55bcSRobert Watson 	UNP_LINK_WUNLOCK();
24290cb64678SKonstantin Belousov 	return (ret);
2430397c19d1SJeff Roberson }
2431397c19d1SJeff Roberson 
2432161a0c7cSRobert Watson /*
2433a0ec558aSRobert Watson  * unp_defer indicates whether additional work has been defered for a future
2434a0ec558aSRobert Watson  * pass through unp_gc().  It is thread local and does not require explicit
2435a0ec558aSRobert Watson  * synchronization.
2436161a0c7cSRobert Watson  */
2437397c19d1SJeff Roberson static int	unp_marked;
2438397c19d1SJeff Roberson static int	unp_unreachable;
2439a0ec558aSRobert Watson 
2440397c19d1SJeff Roberson static void
2441be26ba7cSPawel Jakub Dawidek unp_accessable(struct filedescent **fdep, int fdcount)
2442397c19d1SJeff Roberson {
2443397c19d1SJeff Roberson 	struct unpcb *unp;
2444be26ba7cSPawel Jakub Dawidek 	struct file *fp;
2445be26ba7cSPawel Jakub Dawidek 	int i;
2446397c19d1SJeff Roberson 
2447be26ba7cSPawel Jakub Dawidek 	for (i = 0; i < fdcount; i++) {
2448be26ba7cSPawel Jakub Dawidek 		fp = fdep[i]->fde_file;
24496f552cb0SJeff Roberson 		if ((unp = fptounp(fp)) == NULL)
2450be26ba7cSPawel Jakub Dawidek 			continue;
2451397c19d1SJeff Roberson 		if (unp->unp_gcflag & UNPGC_REF)
2452be26ba7cSPawel Jakub Dawidek 			continue;
2453397c19d1SJeff Roberson 		unp->unp_gcflag &= ~UNPGC_DEAD;
2454397c19d1SJeff Roberson 		unp->unp_gcflag |= UNPGC_REF;
2455397c19d1SJeff Roberson 		unp_marked++;
2456397c19d1SJeff Roberson 	}
2457be26ba7cSPawel Jakub Dawidek }
2458397c19d1SJeff Roberson 
2459397c19d1SJeff Roberson static void
2460397c19d1SJeff Roberson unp_gc_process(struct unpcb *unp)
2461397c19d1SJeff Roberson {
2462779f106aSGleb Smirnoff 	struct socket *so, *soa;
2463397c19d1SJeff Roberson 	struct file *fp;
2464397c19d1SJeff Roberson 
2465397c19d1SJeff Roberson 	/* Already processed. */
2466397c19d1SJeff Roberson 	if (unp->unp_gcflag & UNPGC_SCANNED)
2467397c19d1SJeff Roberson 		return;
2468397c19d1SJeff Roberson 	fp = unp->unp_file;
246960a5ef26SRobert Watson 
2470397c19d1SJeff Roberson 	/*
2471397c19d1SJeff Roberson 	 * Check for a socket potentially in a cycle.  It must be in a
2472397c19d1SJeff Roberson 	 * queue as indicated by msgcount, and this must equal the file
2473397c19d1SJeff Roberson 	 * reference count.  Note that when msgcount is 0 the file is NULL.
2474397c19d1SJeff Roberson 	 */
247541e0f66dSJeff Roberson 	if ((unp->unp_gcflag & UNPGC_REF) == 0 && fp &&
247641e0f66dSJeff Roberson 	    unp->unp_msgcount != 0 && fp->f_count == unp->unp_msgcount) {
2477397c19d1SJeff Roberson 		unp->unp_gcflag |= UNPGC_DEAD;
2478397c19d1SJeff Roberson 		unp_unreachable++;
2479397c19d1SJeff Roberson 		return;
2480397c19d1SJeff Roberson 	}
248160a5ef26SRobert Watson 
2482397c19d1SJeff Roberson 	so = unp->unp_socket;
2483779f106aSGleb Smirnoff 	SOCK_LOCK(so);
2484779f106aSGleb Smirnoff 	if (SOLISTENING(so)) {
2485397c19d1SJeff Roberson 		/*
2486397c19d1SJeff Roberson 		 * Mark all sockets in our accept queue.
2487397c19d1SJeff Roberson 		 */
2488779f106aSGleb Smirnoff 		TAILQ_FOREACH(soa, &so->sol_comp, so_list) {
2489779f106aSGleb Smirnoff 			if (sotounpcb(soa)->unp_gcflag & UNPGC_IGNORE_RIGHTS)
24900c40f353SConrad Meyer 				continue;
2491397c19d1SJeff Roberson 			SOCKBUF_LOCK(&soa->so_rcv);
2492397c19d1SJeff Roberson 			unp_scan(soa->so_rcv.sb_mb, unp_accessable);
2493397c19d1SJeff Roberson 			SOCKBUF_UNLOCK(&soa->so_rcv);
2494397c19d1SJeff Roberson 		}
2495779f106aSGleb Smirnoff 	} else {
2496779f106aSGleb Smirnoff 		/*
2497779f106aSGleb Smirnoff 		 * Mark all sockets we reference with RIGHTS.
2498779f106aSGleb Smirnoff 		 */
2499779f106aSGleb Smirnoff 		if ((unp->unp_gcflag & UNPGC_IGNORE_RIGHTS) == 0) {
2500779f106aSGleb Smirnoff 			SOCKBUF_LOCK(&so->so_rcv);
2501779f106aSGleb Smirnoff 			unp_scan(so->so_rcv.sb_mb, unp_accessable);
2502779f106aSGleb Smirnoff 			SOCKBUF_UNLOCK(&so->so_rcv);
2503779f106aSGleb Smirnoff 		}
2504779f106aSGleb Smirnoff 	}
2505779f106aSGleb Smirnoff 	SOCK_UNLOCK(so);
2506397c19d1SJeff Roberson 	unp->unp_gcflag |= UNPGC_SCANNED;
2507397c19d1SJeff Roberson }
2508a0ec558aSRobert Watson 
2509a0ec558aSRobert Watson static int unp_recycled;
2510be6b1304STom Rhodes SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0,
2511be6b1304STom Rhodes     "Number of unreachable sockets claimed by the garbage collector.");
2512df8bae1dSRodney W. Grimes 
2513397c19d1SJeff Roberson static int unp_taskcount;
2514be6b1304STom Rhodes SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0,
2515be6b1304STom Rhodes     "Number of times the garbage collector has run.");
2516397c19d1SJeff Roberson 
2517f708ef1bSPoul-Henning Kamp static void
2518a0ec558aSRobert Watson unp_gc(__unused void *arg, int pending)
2519df8bae1dSRodney W. Grimes {
252084d61770SRobert Watson 	struct unp_head *heads[] = { &unp_dhead, &unp_shead, &unp_sphead,
252184d61770SRobert Watson 				    NULL };
2522397c19d1SJeff Roberson 	struct unp_head **head;
2523f7780c61SKonstantin Belousov 	struct file *f, **unref;
2524397c19d1SJeff Roberson 	struct unpcb *unp;
2525f7780c61SKonstantin Belousov 	int i, total;
2526df8bae1dSRodney W. Grimes 
2527a0ec558aSRobert Watson 	unp_taskcount++;
2528779f106aSGleb Smirnoff 	UNP_LINK_RLOCK();
2529ed5b7817SJulian Elischer 	/*
25300c40f353SConrad Meyer 	 * First clear all gc flags from previous runs, apart from
25310c40f353SConrad Meyer 	 * UNPGC_IGNORE_RIGHTS.
2532ed5b7817SJulian Elischer 	 */
2533397c19d1SJeff Roberson 	for (head = heads; *head != NULL; head++)
2534397c19d1SJeff Roberson 		LIST_FOREACH(unp, *head, unp_link)
25350c40f353SConrad Meyer 			unp->unp_gcflag =
25360c40f353SConrad Meyer 			    (unp->unp_gcflag & UNPGC_IGNORE_RIGHTS);
253760a5ef26SRobert Watson 
2538397c19d1SJeff Roberson 	/*
2539397c19d1SJeff Roberson 	 * Scan marking all reachable sockets with UNPGC_REF.  Once a socket
2540397c19d1SJeff Roberson 	 * is reachable all of the sockets it references are reachable.
2541397c19d1SJeff Roberson 	 * Stop the scan once we do a complete loop without discovering
2542397c19d1SJeff Roberson 	 * a new reachable socket.
2543397c19d1SJeff Roberson 	 */
2544df8bae1dSRodney W. Grimes 	do {
2545397c19d1SJeff Roberson 		unp_unreachable = 0;
2546397c19d1SJeff Roberson 		unp_marked = 0;
2547397c19d1SJeff Roberson 		for (head = heads; *head != NULL; head++)
2548397c19d1SJeff Roberson 			LIST_FOREACH(unp, *head, unp_link)
2549397c19d1SJeff Roberson 				unp_gc_process(unp);
2550397c19d1SJeff Roberson 	} while (unp_marked);
2551779f106aSGleb Smirnoff 	UNP_LINK_RUNLOCK();
2552397c19d1SJeff Roberson 	if (unp_unreachable == 0)
2553397c19d1SJeff Roberson 		return;
255460a5ef26SRobert Watson 
2555ed5b7817SJulian Elischer 	/*
2556397c19d1SJeff Roberson 	 * Allocate space for a local list of dead unpcbs.
2557ed5b7817SJulian Elischer 	 */
2558397c19d1SJeff Roberson 	unref = malloc(unp_unreachable * sizeof(struct file *),
2559397c19d1SJeff Roberson 	    M_TEMP, M_WAITOK);
256060a5ef26SRobert Watson 
2561ed5b7817SJulian Elischer 	/*
2562397c19d1SJeff Roberson 	 * Iterate looking for sockets which have been specifically marked
2563397c19d1SJeff Roberson 	 * as as unreachable and store them locally.
2564ed5b7817SJulian Elischer 	 */
2565f7780c61SKonstantin Belousov 	UNP_LINK_RLOCK();
2566f7780c61SKonstantin Belousov 	for (total = 0, head = heads; *head != NULL; head++)
2567397c19d1SJeff Roberson 		LIST_FOREACH(unp, *head, unp_link)
2568f7780c61SKonstantin Belousov 			if ((unp->unp_gcflag & UNPGC_DEAD) != 0) {
2569f7780c61SKonstantin Belousov 				f = unp->unp_file;
2570f7780c61SKonstantin Belousov 				if (unp->unp_msgcount == 0 || f == NULL ||
2571f7780c61SKonstantin Belousov 				    f->f_count != unp->unp_msgcount)
2572f7780c61SKonstantin Belousov 					continue;
2573f7780c61SKonstantin Belousov 				unref[total++] = f;
2574f7780c61SKonstantin Belousov 				fhold(f);
2575f7780c61SKonstantin Belousov 				KASSERT(total <= unp_unreachable,
2576397c19d1SJeff Roberson 				    ("unp_gc: incorrect unreachable count."));
2577397c19d1SJeff Roberson 			}
2578f7780c61SKonstantin Belousov 	UNP_LINK_RUNLOCK();
257960a5ef26SRobert Watson 
2580ed5b7817SJulian Elischer 	/*
2581397c19d1SJeff Roberson 	 * Now flush all sockets, free'ing rights.  This will free the
2582397c19d1SJeff Roberson 	 * struct files associated with these sockets but leave each socket
2583397c19d1SJeff Roberson 	 * with one remaining ref.
2584ed5b7817SJulian Elischer 	 */
25851fb51a12SBjoern A. Zeeb 	for (i = 0; i < total; i++) {
25861fb51a12SBjoern A. Zeeb 		struct socket *so;
25871fb51a12SBjoern A. Zeeb 
25881fb51a12SBjoern A. Zeeb 		so = unref[i]->f_data;
25891fb51a12SBjoern A. Zeeb 		CURVNET_SET(so->so_vnet);
25901fb51a12SBjoern A. Zeeb 		sorflush(so);
25911fb51a12SBjoern A. Zeeb 		CURVNET_RESTORE();
25921fb51a12SBjoern A. Zeeb 	}
259360a5ef26SRobert Watson 
2594ed5b7817SJulian Elischer 	/*
2595397c19d1SJeff Roberson 	 * And finally release the sockets so they can be reclaimed.
2596ed5b7817SJulian Elischer 	 */
2597f7780c61SKonstantin Belousov 	for (i = 0; i < total; i++)
2598397c19d1SJeff Roberson 		fdrop(unref[i], NULL);
2599f7780c61SKonstantin Belousov 	unp_recycled += total;
2600397c19d1SJeff Roberson 	free(unref, M_TEMP);
2601df8bae1dSRodney W. Grimes }
2602df8bae1dSRodney W. Grimes 
26030b36cd25SRobert Watson static void
260499ab95dbSMark Johnston unp_dispose_mbuf(struct mbuf *m)
2605df8bae1dSRodney W. Grimes {
2606996c772fSJohn Dyson 
2607df8bae1dSRodney W. Grimes 	if (m)
2608be26ba7cSPawel Jakub Dawidek 		unp_scan(m, unp_freerights);
2609df8bae1dSRodney W. Grimes }
2610df8bae1dSRodney W. Grimes 
26110c40f353SConrad Meyer /*
26120c40f353SConrad Meyer  * Synchronize against unp_gc, which can trip over data as we are freeing it.
26130c40f353SConrad Meyer  */
26140c40f353SConrad Meyer static void
261599ab95dbSMark Johnston unp_dispose(struct socket *so)
26160c40f353SConrad Meyer {
26170c40f353SConrad Meyer 	struct unpcb *unp;
26180c40f353SConrad Meyer 
26190c40f353SConrad Meyer 	unp = sotounpcb(so);
2620779f106aSGleb Smirnoff 	UNP_LINK_WLOCK();
26210c40f353SConrad Meyer 	unp->unp_gcflag |= UNPGC_IGNORE_RIGHTS;
2622779f106aSGleb Smirnoff 	UNP_LINK_WUNLOCK();
2623779f106aSGleb Smirnoff 	if (!SOLISTENING(so))
262499ab95dbSMark Johnston 		unp_dispose_mbuf(so->so_rcv.sb_mb);
26250c40f353SConrad Meyer }
26260c40f353SConrad Meyer 
2627f708ef1bSPoul-Henning Kamp static void
2628be26ba7cSPawel Jakub Dawidek unp_scan(struct mbuf *m0, void (*op)(struct filedescent **, int))
2629df8bae1dSRodney W. Grimes {
26302bc21ed9SDavid Malone 	struct mbuf *m;
26312bc21ed9SDavid Malone 	struct cmsghdr *cm;
26322bc21ed9SDavid Malone 	void *data;
26332bc21ed9SDavid Malone 	socklen_t clen, datalen;
2634df8bae1dSRodney W. Grimes 
2635fc3fcacfSRobert Watson 	while (m0 != NULL) {
26362bc21ed9SDavid Malone 		for (m = m0; m; m = m->m_next) {
263712396bdcSDavid Malone 			if (m->m_type != MT_CONTROL)
2638df8bae1dSRodney W. Grimes 				continue;
26392bc21ed9SDavid Malone 
26402bc21ed9SDavid Malone 			cm = mtod(m, struct cmsghdr *);
26412bc21ed9SDavid Malone 			clen = m->m_len;
26422bc21ed9SDavid Malone 
26432bc21ed9SDavid Malone 			while (cm != NULL) {
26442bc21ed9SDavid Malone 				if (sizeof(*cm) > clen || cm->cmsg_len > clen)
26452bc21ed9SDavid Malone 					break;
26462bc21ed9SDavid Malone 
26472bc21ed9SDavid Malone 				data = CMSG_DATA(cm);
26482bc21ed9SDavid Malone 				datalen = (caddr_t)cm + cm->cmsg_len
26492bc21ed9SDavid Malone 				    - (caddr_t)data;
26502bc21ed9SDavid Malone 
26512bc21ed9SDavid Malone 				if (cm->cmsg_level == SOL_SOCKET &&
26522bc21ed9SDavid Malone 				    cm->cmsg_type == SCM_RIGHTS) {
2653be26ba7cSPawel Jakub Dawidek 					(*op)(data, datalen /
2654be26ba7cSPawel Jakub Dawidek 					    sizeof(struct filedescent *));
26552bc21ed9SDavid Malone 				}
26562bc21ed9SDavid Malone 
26572bc21ed9SDavid Malone 				if (CMSG_SPACE(datalen) < clen) {
26582bc21ed9SDavid Malone 					clen -= CMSG_SPACE(datalen);
26592bc21ed9SDavid Malone 					cm = (struct cmsghdr *)
26602bc21ed9SDavid Malone 					    ((caddr_t)cm + CMSG_SPACE(datalen));
26612bc21ed9SDavid Malone 				} else {
26622bc21ed9SDavid Malone 					clen = 0;
26632bc21ed9SDavid Malone 					cm = NULL;
26642bc21ed9SDavid Malone 				}
26652bc21ed9SDavid Malone 			}
2666df8bae1dSRodney W. Grimes 		}
2667c29a3321SKevin Lo 		m0 = m0->m_nextpkt;
2668df8bae1dSRodney W. Grimes 	}
2669df8bae1dSRodney W. Grimes }
2670df8bae1dSRodney W. Grimes 
2671662c901cSMikolaj Golub /*
2672662c901cSMikolaj Golub  * A helper function called by VFS before socket-type vnode reclamation.
2673662c901cSMikolaj Golub  * For an active vnode it clears unp_vnode pointer and decrements unp_vnode
2674662c901cSMikolaj Golub  * use count.
2675662c901cSMikolaj Golub  */
2676662c901cSMikolaj Golub void
2677662c901cSMikolaj Golub vfs_unp_reclaim(struct vnode *vp)
2678662c901cSMikolaj Golub {
2679662c901cSMikolaj Golub 	struct unpcb *unp;
2680662c901cSMikolaj Golub 	int active;
268175a67bf3SMatt Macy 	struct mtx *vplock;
2682662c901cSMikolaj Golub 
2683662c901cSMikolaj Golub 	ASSERT_VOP_ELOCKED(vp, "vfs_unp_reclaim");
2684662c901cSMikolaj Golub 	KASSERT(vp->v_type == VSOCK,
2685662c901cSMikolaj Golub 	    ("vfs_unp_reclaim: vp->v_type != VSOCK"));
2686662c901cSMikolaj Golub 
2687662c901cSMikolaj Golub 	active = 0;
268875a67bf3SMatt Macy 	vplock = mtx_pool_find(mtxpool_sleep, vp);
268975a67bf3SMatt Macy 	mtx_lock(vplock);
26900c3c207fSGleb Smirnoff 	VOP_UNP_CONNECT(vp, &unp);
2691662c901cSMikolaj Golub 	if (unp == NULL)
2692662c901cSMikolaj Golub 		goto done;
2693662c901cSMikolaj Golub 	UNP_PCB_LOCK(unp);
2694c7e41c8bSMikolaj Golub 	if (unp->unp_vnode == vp) {
2695c7e41c8bSMikolaj Golub 		VOP_UNP_DETACH(vp);
2696662c901cSMikolaj Golub 		unp->unp_vnode = NULL;
2697662c901cSMikolaj Golub 		active = 1;
2698662c901cSMikolaj Golub 	}
2699662c901cSMikolaj Golub 	UNP_PCB_UNLOCK(unp);
2700662c901cSMikolaj Golub  done:
270175a67bf3SMatt Macy 	mtx_unlock(vplock);
2702662c901cSMikolaj Golub 	if (active)
2703662c901cSMikolaj Golub 		vunref(vp);
2704662c901cSMikolaj Golub }
2705662c901cSMikolaj Golub 
270603c96c31SRobert Watson #ifdef DDB
270703c96c31SRobert Watson static void
270803c96c31SRobert Watson db_print_indent(int indent)
270903c96c31SRobert Watson {
271003c96c31SRobert Watson 	int i;
271103c96c31SRobert Watson 
271203c96c31SRobert Watson 	for (i = 0; i < indent; i++)
271303c96c31SRobert Watson 		db_printf(" ");
271403c96c31SRobert Watson }
271503c96c31SRobert Watson 
271603c96c31SRobert Watson static void
271703c96c31SRobert Watson db_print_unpflags(int unp_flags)
271803c96c31SRobert Watson {
271903c96c31SRobert Watson 	int comma;
272003c96c31SRobert Watson 
272103c96c31SRobert Watson 	comma = 0;
272203c96c31SRobert Watson 	if (unp_flags & UNP_HAVEPC) {
272303c96c31SRobert Watson 		db_printf("%sUNP_HAVEPC", comma ? ", " : "");
272403c96c31SRobert Watson 		comma = 1;
272503c96c31SRobert Watson 	}
272603c96c31SRobert Watson 	if (unp_flags & UNP_WANTCRED) {
272703c96c31SRobert Watson 		db_printf("%sUNP_WANTCRED", comma ? ", " : "");
272803c96c31SRobert Watson 		comma = 1;
272903c96c31SRobert Watson 	}
273003c96c31SRobert Watson 	if (unp_flags & UNP_CONNWAIT) {
273103c96c31SRobert Watson 		db_printf("%sUNP_CONNWAIT", comma ? ", " : "");
273203c96c31SRobert Watson 		comma = 1;
273303c96c31SRobert Watson 	}
273403c96c31SRobert Watson 	if (unp_flags & UNP_CONNECTING) {
273503c96c31SRobert Watson 		db_printf("%sUNP_CONNECTING", comma ? ", " : "");
273603c96c31SRobert Watson 		comma = 1;
273703c96c31SRobert Watson 	}
273803c96c31SRobert Watson 	if (unp_flags & UNP_BINDING) {
273903c96c31SRobert Watson 		db_printf("%sUNP_BINDING", comma ? ", " : "");
274003c96c31SRobert Watson 		comma = 1;
274103c96c31SRobert Watson 	}
274203c96c31SRobert Watson }
274303c96c31SRobert Watson 
274403c96c31SRobert Watson static void
274503c96c31SRobert Watson db_print_xucred(int indent, struct xucred *xu)
274603c96c31SRobert Watson {
274703c96c31SRobert Watson 	int comma, i;
274803c96c31SRobert Watson 
274903c96c31SRobert Watson 	db_print_indent(indent);
275003c96c31SRobert Watson 	db_printf("cr_version: %u   cr_uid: %u   cr_ngroups: %d\n",
275103c96c31SRobert Watson 	    xu->cr_version, xu->cr_uid, xu->cr_ngroups);
275203c96c31SRobert Watson 	db_print_indent(indent);
275303c96c31SRobert Watson 	db_printf("cr_groups: ");
275403c96c31SRobert Watson 	comma = 0;
275503c96c31SRobert Watson 	for (i = 0; i < xu->cr_ngroups; i++) {
275603c96c31SRobert Watson 		db_printf("%s%u", comma ? ", " : "", xu->cr_groups[i]);
275703c96c31SRobert Watson 		comma = 1;
275803c96c31SRobert Watson 	}
275903c96c31SRobert Watson 	db_printf("\n");
276003c96c31SRobert Watson }
276103c96c31SRobert Watson 
276203c96c31SRobert Watson static void
276303c96c31SRobert Watson db_print_unprefs(int indent, struct unp_head *uh)
276403c96c31SRobert Watson {
276503c96c31SRobert Watson 	struct unpcb *unp;
276603c96c31SRobert Watson 	int counter;
276703c96c31SRobert Watson 
276803c96c31SRobert Watson 	counter = 0;
276903c96c31SRobert Watson 	LIST_FOREACH(unp, uh, unp_reflink) {
277003c96c31SRobert Watson 		if (counter % 4 == 0)
277103c96c31SRobert Watson 			db_print_indent(indent);
277203c96c31SRobert Watson 		db_printf("%p  ", unp);
277303c96c31SRobert Watson 		if (counter % 4 == 3)
277403c96c31SRobert Watson 			db_printf("\n");
277503c96c31SRobert Watson 		counter++;
277603c96c31SRobert Watson 	}
277703c96c31SRobert Watson 	if (counter != 0 && counter % 4 != 0)
277803c96c31SRobert Watson 		db_printf("\n");
277903c96c31SRobert Watson }
278003c96c31SRobert Watson 
278103c96c31SRobert Watson DB_SHOW_COMMAND(unpcb, db_show_unpcb)
278203c96c31SRobert Watson {
278303c96c31SRobert Watson 	struct unpcb *unp;
278403c96c31SRobert Watson 
278503c96c31SRobert Watson         if (!have_addr) {
278603c96c31SRobert Watson                 db_printf("usage: show unpcb <addr>\n");
278703c96c31SRobert Watson                 return;
278803c96c31SRobert Watson         }
278903c96c31SRobert Watson         unp = (struct unpcb *)addr;
279003c96c31SRobert Watson 
279103c96c31SRobert Watson 	db_printf("unp_socket: %p   unp_vnode: %p\n", unp->unp_socket,
279203c96c31SRobert Watson 	    unp->unp_vnode);
279303c96c31SRobert Watson 
2794fc8fdae0SMatthew D Fleming 	db_printf("unp_ino: %ju   unp_conn: %p\n", (uintmax_t)unp->unp_ino,
279503c96c31SRobert Watson 	    unp->unp_conn);
279603c96c31SRobert Watson 
279703c96c31SRobert Watson 	db_printf("unp_refs:\n");
279803c96c31SRobert Watson 	db_print_unprefs(2, &unp->unp_refs);
279903c96c31SRobert Watson 
280003c96c31SRobert Watson 	/* XXXRW: Would be nice to print the full address, if any. */
280103c96c31SRobert Watson 	db_printf("unp_addr: %p\n", unp->unp_addr);
280203c96c31SRobert Watson 
2803c2090e73SAlan Somers 	db_printf("unp_gencnt: %llu\n",
280403c96c31SRobert Watson 	    (unsigned long long)unp->unp_gencnt);
280503c96c31SRobert Watson 
280603c96c31SRobert Watson 	db_printf("unp_flags: %x (", unp->unp_flags);
280703c96c31SRobert Watson 	db_print_unpflags(unp->unp_flags);
280803c96c31SRobert Watson 	db_printf(")\n");
280903c96c31SRobert Watson 
281003c96c31SRobert Watson 	db_printf("unp_peercred:\n");
281103c96c31SRobert Watson 	db_print_xucred(2, &unp->unp_peercred);
281203c96c31SRobert Watson 
281303c96c31SRobert Watson 	db_printf("unp_refcount: %u\n", unp->unp_refcount);
281403c96c31SRobert Watson }
281503c96c31SRobert Watson #endif
2816