xref: /freebsd/sys/kern/uipc_usrreq.c (revision cb8f450b94866f622e4a62c3846bec48d6295c48)
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 {
34775a67bf3SMatt Macy 	UNP_PCB_UNLOCK_ASSERT(unp);
34875a67bf3SMatt Macy 	UNP_PCB_UNLOCK_ASSERT(unp2);
34975a67bf3SMatt Macy 	if ((uintptr_t)unp2 > (uintptr_t)unp) {
35075a67bf3SMatt Macy 		UNP_PCB_LOCK(unp);
35175a67bf3SMatt Macy 		UNP_PCB_LOCK(unp2);
35275a67bf3SMatt Macy 	} else {
35375a67bf3SMatt Macy 		UNP_PCB_LOCK(unp2);
35475a67bf3SMatt Macy 		UNP_PCB_LOCK(unp);
35575a67bf3SMatt Macy 	}
35675a67bf3SMatt Macy }
35775a67bf3SMatt Macy 
35875a67bf3SMatt Macy static __noinline void
35975a67bf3SMatt Macy unp_pcb_owned_lock2_slowpath(struct unpcb *unp, struct unpcb **unp2p, int *freed)
36075a67bf3SMatt Macy 
36175a67bf3SMatt Macy {
36275a67bf3SMatt Macy 	struct unpcb *unp2;
36375a67bf3SMatt Macy 
36475a67bf3SMatt Macy 	unp2 = *unp2p;
36575a67bf3SMatt Macy 	unp_pcb_hold((unp2));
36675a67bf3SMatt Macy 	UNP_PCB_UNLOCK((unp));
36775a67bf3SMatt Macy 	UNP_PCB_LOCK((unp2));
36875a67bf3SMatt Macy 	UNP_PCB_LOCK((unp));
36975a67bf3SMatt Macy 	*freed = unp_pcb_rele((unp2));
37075a67bf3SMatt Macy 	if (*freed)
37175a67bf3SMatt Macy 		*unp2p = NULL;
37275a67bf3SMatt Macy }
37375a67bf3SMatt Macy 
37475a67bf3SMatt Macy #define unp_pcb_owned_lock2(unp, unp2, freed) do {					\
37575a67bf3SMatt Macy 		freed = 0;													\
37675a67bf3SMatt Macy 		UNP_PCB_LOCK_ASSERT((unp));									\
37775a67bf3SMatt Macy 		UNP_PCB_UNLOCK_ASSERT((unp2));								\
37875a67bf3SMatt Macy 		if (__predict_true(UNP_PCB_TRYLOCK((unp2))))				\
37975a67bf3SMatt Macy 			break;													\
38075a67bf3SMatt Macy 		else if ((uintptr_t)(unp2) > (uintptr_t)(unp))				\
38175a67bf3SMatt Macy 			UNP_PCB_LOCK((unp2));									\
38275a67bf3SMatt Macy 		else {														\
38375a67bf3SMatt Macy 			unp_pcb_owned_lock2_slowpath((unp), &(unp2), &freed);	\
38475a67bf3SMatt Macy 		}															\
38575a67bf3SMatt Macy } while (0)
38675a67bf3SMatt Macy 
38775a67bf3SMatt Macy 
388e4445a03SRobert Watson /*
389e4445a03SRobert Watson  * Definitions of protocols supported in the LOCAL domain.
390e4445a03SRobert Watson  */
391e4445a03SRobert Watson static struct domain localdomain;
392fa9402f2SRobert Watson static struct pr_usrreqs uipc_usrreqs_dgram, uipc_usrreqs_stream;
39384d61770SRobert Watson static struct pr_usrreqs uipc_usrreqs_seqpacket;
394e4445a03SRobert Watson static struct protosw localsw[] = {
395e4445a03SRobert Watson {
396e4445a03SRobert Watson 	.pr_type =		SOCK_STREAM,
397e4445a03SRobert Watson 	.pr_domain =		&localdomain,
398e4445a03SRobert Watson 	.pr_flags =		PR_CONNREQUIRED|PR_WANTRCVD|PR_RIGHTS,
399e4445a03SRobert Watson 	.pr_ctloutput =		&uipc_ctloutput,
400fa9402f2SRobert Watson 	.pr_usrreqs =		&uipc_usrreqs_stream
401e4445a03SRobert Watson },
402e4445a03SRobert Watson {
403e4445a03SRobert Watson 	.pr_type =		SOCK_DGRAM,
404e4445a03SRobert Watson 	.pr_domain =		&localdomain,
405e4445a03SRobert Watson 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_RIGHTS,
406aaf63435SGleb Smirnoff 	.pr_ctloutput =		&uipc_ctloutput,
407fa9402f2SRobert Watson 	.pr_usrreqs =		&uipc_usrreqs_dgram
408e4445a03SRobert Watson },
40984d61770SRobert Watson {
41084d61770SRobert Watson 	.pr_type =		SOCK_SEQPACKET,
41184d61770SRobert Watson 	.pr_domain =		&localdomain,
41284d61770SRobert Watson 
41384d61770SRobert Watson 	/*
41484d61770SRobert Watson 	 * XXXRW: For now, PR_ADDR because soreceive will bump into them
41584d61770SRobert Watson 	 * due to our use of sbappendaddr.  A new sbappend variants is needed
41684d61770SRobert Watson 	 * that supports both atomic record writes and control data.
41784d61770SRobert Watson 	 */
41884d61770SRobert Watson 	.pr_flags =		PR_ADDR|PR_ATOMIC|PR_CONNREQUIRED|PR_WANTRCVD|
41984d61770SRobert Watson 				    PR_RIGHTS,
420e0643280SGleb Smirnoff 	.pr_ctloutput =		&uipc_ctloutput,
42184d61770SRobert Watson 	.pr_usrreqs =		&uipc_usrreqs_seqpacket,
42284d61770SRobert Watson },
423e4445a03SRobert Watson };
424e4445a03SRobert Watson 
425e4445a03SRobert Watson static struct domain localdomain = {
426e4445a03SRobert Watson 	.dom_family =		AF_LOCAL,
427e4445a03SRobert Watson 	.dom_name =		"local",
428e4445a03SRobert Watson 	.dom_init =		unp_init,
429e4445a03SRobert Watson 	.dom_externalize =	unp_externalize,
43099ab95dbSMark Johnston 	.dom_dispose =		unp_dispose,
431e4445a03SRobert Watson 	.dom_protosw =		localsw,
43202abd400SPedro F. Giffuni 	.dom_protoswNPROTOSW =	&localsw[nitems(localsw)]
433e4445a03SRobert Watson };
434e4445a03SRobert Watson DOMAIN_SET(local);
435e4445a03SRobert Watson 
436ac45e92fSRobert Watson static void
437a29f300eSGarrett Wollman uipc_abort(struct socket *so)
438df8bae1dSRodney W. Grimes {
439e7c33e29SRobert Watson 	struct unpcb *unp, *unp2;
440df8bae1dSRodney W. Grimes 
44140f2ac28SRobert Watson 	unp = sotounpcb(so);
4424d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_abort: unp == NULL"));
44375a67bf3SMatt Macy 	UNP_PCB_UNLOCK_ASSERT(unp);
444e7c33e29SRobert Watson 
445e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
446e7c33e29SRobert Watson 	unp2 = unp->unp_conn;
447e7c33e29SRobert Watson 	if (unp2 != NULL) {
44875a67bf3SMatt Macy 		unp_pcb_hold(unp2);
449e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
45075a67bf3SMatt Macy 		unp_drop(unp2);
45175a67bf3SMatt Macy 	} else
45275a67bf3SMatt Macy 		UNP_PCB_UNLOCK(unp);
453df8bae1dSRodney W. Grimes }
454df8bae1dSRodney W. Grimes 
455a29f300eSGarrett Wollman static int
45657bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam)
457a29f300eSGarrett Wollman {
458e7c33e29SRobert Watson 	struct unpcb *unp, *unp2;
4590d9ce3a1SRobert Watson 	const struct sockaddr *sa;
460df8bae1dSRodney W. Grimes 
461df8bae1dSRodney W. Grimes 	/*
4621c381b19SRobert Watson 	 * Pass back name of connected socket, if it was bound and we are
4631c381b19SRobert Watson 	 * still connected (our peer may have closed already!).
464df8bae1dSRodney W. Grimes 	 */
4654d4b555eSRobert Watson 	unp = sotounpcb(so);
4664d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_accept: unp == NULL"));
467e7c33e29SRobert Watson 
4680d9ce3a1SRobert Watson 	*nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
4693dab55bcSRobert Watson 	UNP_LINK_RLOCK();
470e7c33e29SRobert Watson 	unp2 = unp->unp_conn;
471e7c33e29SRobert Watson 	if (unp2 != NULL && unp2->unp_addr != NULL) {
472e7c33e29SRobert Watson 		UNP_PCB_LOCK(unp2);
473e7c33e29SRobert Watson 		sa = (struct sockaddr *) unp2->unp_addr;
474e7c33e29SRobert Watson 		bcopy(sa, *nam, sa->sa_len);
475e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp2);
476e7c33e29SRobert Watson 	} else {
4770d9ce3a1SRobert Watson 		sa = &sun_noname;
4780d9ce3a1SRobert Watson 		bcopy(sa, *nam, sa->sa_len);
479e7c33e29SRobert Watson 	}
4803dab55bcSRobert Watson 	UNP_LINK_RUNLOCK();
481e5aeaa0cSDag-Erling Smørgrav 	return (0);
482a29f300eSGarrett Wollman }
483df8bae1dSRodney W. Grimes 
484a29f300eSGarrett Wollman static int
485b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td)
486a29f300eSGarrett Wollman {
487e7c33e29SRobert Watson 	u_long sendspace, recvspace;
4886d32873cSRobert Watson 	struct unpcb *unp;
4893dab55bcSRobert Watson 	int error;
490779f106aSGleb Smirnoff 	bool locked;
491df8bae1dSRodney W. Grimes 
4926d32873cSRobert Watson 	KASSERT(so->so_pcb == NULL, ("uipc_attach: so_pcb != NULL"));
4936d32873cSRobert Watson 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
4946d32873cSRobert Watson 		switch (so->so_type) {
4956d32873cSRobert Watson 		case SOCK_STREAM:
496e7c33e29SRobert Watson 			sendspace = unpst_sendspace;
497e7c33e29SRobert Watson 			recvspace = unpst_recvspace;
4986d32873cSRobert Watson 			break;
4996d32873cSRobert Watson 
5006d32873cSRobert Watson 		case SOCK_DGRAM:
501e7c33e29SRobert Watson 			sendspace = unpdg_sendspace;
502e7c33e29SRobert Watson 			recvspace = unpdg_recvspace;
5036d32873cSRobert Watson 			break;
5046d32873cSRobert Watson 
50584d61770SRobert Watson 		case SOCK_SEQPACKET:
50684d61770SRobert Watson 			sendspace = unpsp_sendspace;
50784d61770SRobert Watson 			recvspace = unpsp_recvspace;
50884d61770SRobert Watson 			break;
50984d61770SRobert Watson 
5106d32873cSRobert Watson 		default:
511e7c33e29SRobert Watson 			panic("uipc_attach");
5126d32873cSRobert Watson 		}
513e7c33e29SRobert Watson 		error = soreserve(so, sendspace, recvspace);
5146d32873cSRobert Watson 		if (error)
5156d32873cSRobert Watson 			return (error);
5166d32873cSRobert Watson 	}
51746a1d9bfSRobert Watson 	unp = uma_zalloc(unp_zone, M_NOWAIT | M_ZERO);
5186d32873cSRobert Watson 	if (unp == NULL)
5196d32873cSRobert Watson 		return (ENOBUFS);
5206d32873cSRobert Watson 	LIST_INIT(&unp->unp_refs);
521e7c33e29SRobert Watson 	UNP_PCB_LOCK_INIT(unp);
5226d32873cSRobert Watson 	unp->unp_socket = so;
5236d32873cSRobert Watson 	so->so_pcb = unp;
5249ae328fcSJohn Baldwin 	unp->unp_refcount = 1;
525779f106aSGleb Smirnoff 	if (so->so_listen != NULL)
526434ac8b6SMark Johnston 		unp->unp_flags |= UNP_NASCENT;
527e7c33e29SRobert Watson 
528779f106aSGleb Smirnoff 	if ((locked = UNP_LINK_WOWNED()) == false)
529779f106aSGleb Smirnoff 		UNP_LINK_WLOCK();
530779f106aSGleb Smirnoff 
5316d32873cSRobert Watson 	unp->unp_gencnt = ++unp_gencnt;
5326d32873cSRobert Watson 	unp_count++;
53384d61770SRobert Watson 	switch (so->so_type) {
53484d61770SRobert Watson 	case SOCK_STREAM:
53584d61770SRobert Watson 		LIST_INSERT_HEAD(&unp_shead, unp, unp_link);
53684d61770SRobert Watson 		break;
53784d61770SRobert Watson 
53884d61770SRobert Watson 	case SOCK_DGRAM:
53984d61770SRobert Watson 		LIST_INSERT_HEAD(&unp_dhead, unp, unp_link);
54084d61770SRobert Watson 		break;
54184d61770SRobert Watson 
54284d61770SRobert Watson 	case SOCK_SEQPACKET:
54384d61770SRobert Watson 		LIST_INSERT_HEAD(&unp_sphead, unp, unp_link);
54484d61770SRobert Watson 		break;
54584d61770SRobert Watson 
54684d61770SRobert Watson 	default:
54784d61770SRobert Watson 		panic("uipc_attach");
54884d61770SRobert Watson 	}
549779f106aSGleb Smirnoff 
550779f106aSGleb Smirnoff 	if (locked == false)
551779f106aSGleb Smirnoff 		UNP_LINK_WUNLOCK();
5526d32873cSRobert Watson 
5536d32873cSRobert Watson 	return (0);
554a29f300eSGarrett Wollman }
555a29f300eSGarrett Wollman 
556a29f300eSGarrett Wollman static int
5577493f24eSPawel Jakub Dawidek uipc_bindat(int fd, struct socket *so, struct sockaddr *nam, struct thread *td)
558a29f300eSGarrett Wollman {
559dd47f5caSRobert Watson 	struct sockaddr_un *soun = (struct sockaddr_un *)nam;
560dd47f5caSRobert Watson 	struct vattr vattr;
5615050aa86SKonstantin Belousov 	int error, namelen;
562dd47f5caSRobert Watson 	struct nameidata nd;
56340f2ac28SRobert Watson 	struct unpcb *unp;
564dd47f5caSRobert Watson 	struct vnode *vp;
565dd47f5caSRobert Watson 	struct mount *mp;
5667008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
567dd47f5caSRobert Watson 	char *buf;
568a29f300eSGarrett Wollman 
569cb7df69bSKevin Lo 	if (nam->sa_family != AF_UNIX)
570cb7df69bSKevin Lo 		return (EAFNOSUPPORT);
571cb7df69bSKevin Lo 
57240f2ac28SRobert Watson 	unp = sotounpcb(so);
5734d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_bind: unp == NULL"));
5744f1f0ef5SRobert Watson 
575a06534c3SBjoern A. Zeeb 	if (soun->sun_len > sizeof(struct sockaddr_un))
576a06534c3SBjoern A. Zeeb 		return (EINVAL);
5774f1f0ef5SRobert Watson 	namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
5784f1f0ef5SRobert Watson 	if (namelen <= 0)
5794f1f0ef5SRobert Watson 		return (EINVAL);
580dd47f5caSRobert Watson 
581dd47f5caSRobert Watson 	/*
5824f1f0ef5SRobert Watson 	 * We don't allow simultaneous bind() calls on a single UNIX domain
5834f1f0ef5SRobert Watson 	 * socket, so flag in-progress operations, and return an error if an
5844f1f0ef5SRobert Watson 	 * operation is already in progress.
5854f1f0ef5SRobert Watson 	 *
5864f1f0ef5SRobert Watson 	 * Historically, we have not allowed a socket to be rebound, so this
587d7924b70SRobert Watson 	 * also returns an error.  Not allowing re-binding simplifies the
588d7924b70SRobert Watson 	 * implementation and avoids a great many possible failure modes.
589dd47f5caSRobert Watson 	 */
590e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
591dd47f5caSRobert Watson 	if (unp->unp_vnode != NULL) {
592e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
593dd47f5caSRobert Watson 		return (EINVAL);
594dd47f5caSRobert Watson 	}
5954f1f0ef5SRobert Watson 	if (unp->unp_flags & UNP_BINDING) {
596e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
5974f1f0ef5SRobert Watson 		return (EALREADY);
598dd47f5caSRobert Watson 	}
5994f1f0ef5SRobert Watson 	unp->unp_flags |= UNP_BINDING;
600e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
601dd47f5caSRobert Watson 
602dd47f5caSRobert Watson 	buf = malloc(namelen + 1, M_TEMP, M_WAITOK);
6037928893dSEd Maste 	bcopy(soun->sun_path, buf, namelen);
6047928893dSEd Maste 	buf[namelen] = 0;
605dd47f5caSRobert Watson 
606dd47f5caSRobert Watson restart:
6076c21f6edSKonstantin Belousov 	NDINIT_ATRIGHTS(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME | NOCACHE,
6087008be5bSPawel Jakub Dawidek 	    UIO_SYSSPACE, buf, fd, cap_rights_init(&rights, CAP_BINDAT), td);
609dd47f5caSRobert Watson /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
610dd47f5caSRobert Watson 	error = namei(&nd);
611dd47f5caSRobert Watson 	if (error)
6124f1f0ef5SRobert Watson 		goto error;
613dd47f5caSRobert Watson 	vp = nd.ni_vp;
614dd47f5caSRobert Watson 	if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
615dd47f5caSRobert Watson 		NDFREE(&nd, NDF_ONLY_PNBUF);
616dd47f5caSRobert Watson 		if (nd.ni_dvp == vp)
617dd47f5caSRobert Watson 			vrele(nd.ni_dvp);
618dd47f5caSRobert Watson 		else
619dd47f5caSRobert Watson 			vput(nd.ni_dvp);
620dd47f5caSRobert Watson 		if (vp != NULL) {
621dd47f5caSRobert Watson 			vrele(vp);
622dd47f5caSRobert Watson 			error = EADDRINUSE;
6234f1f0ef5SRobert Watson 			goto error;
624dd47f5caSRobert Watson 		}
625dd47f5caSRobert Watson 		error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
626dd47f5caSRobert Watson 		if (error)
6274f1f0ef5SRobert Watson 			goto error;
628dd47f5caSRobert Watson 		goto restart;
629dd47f5caSRobert Watson 	}
630dd47f5caSRobert Watson 	VATTR_NULL(&vattr);
631dd47f5caSRobert Watson 	vattr.va_type = VSOCK;
632dd47f5caSRobert Watson 	vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask);
633dd47f5caSRobert Watson #ifdef MAC
63430d239bcSRobert Watson 	error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
635dd47f5caSRobert Watson 	    &vattr);
636dd47f5caSRobert Watson #endif
637885868cdSRobert Watson 	if (error == 0)
638dd47f5caSRobert Watson 		error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
639dd47f5caSRobert Watson 	NDFREE(&nd, NDF_ONLY_PNBUF);
640dd47f5caSRobert Watson 	vput(nd.ni_dvp);
641dd47f5caSRobert Watson 	if (error) {
642dd47f5caSRobert Watson 		vn_finished_write(mp);
6434f1f0ef5SRobert Watson 		goto error;
644dd47f5caSRobert Watson 	}
645dd47f5caSRobert Watson 	vp = nd.ni_vp;
64657fd3d55SPawel Jakub Dawidek 	ASSERT_VOP_ELOCKED(vp, "uipc_bind");
647dd47f5caSRobert Watson 	soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK);
648e7c33e29SRobert Watson 
649e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
6500c3c207fSGleb Smirnoff 	VOP_UNP_BIND(vp, unp);
651dd47f5caSRobert Watson 	unp->unp_vnode = vp;
652dd47f5caSRobert Watson 	unp->unp_addr = soun;
6534f1f0ef5SRobert Watson 	unp->unp_flags &= ~UNP_BINDING;
654e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
65522db15c0SAttilio Rao 	VOP_UNLOCK(vp, 0);
656dd47f5caSRobert Watson 	vn_finished_write(mp);
6574f1f0ef5SRobert Watson 	free(buf, M_TEMP);
6584f1f0ef5SRobert Watson 	return (0);
659e7c33e29SRobert Watson 
6604f1f0ef5SRobert Watson error:
661e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
6624f1f0ef5SRobert Watson 	unp->unp_flags &= ~UNP_BINDING;
663e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
664dd47f5caSRobert Watson 	free(buf, M_TEMP);
66540f2ac28SRobert Watson 	return (error);
666a29f300eSGarrett Wollman }
667a29f300eSGarrett Wollman 
668a29f300eSGarrett Wollman static int
6697493f24eSPawel Jakub Dawidek uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
6707493f24eSPawel Jakub Dawidek {
6717493f24eSPawel Jakub Dawidek 
6727493f24eSPawel Jakub Dawidek 	return (uipc_bindat(AT_FDCWD, so, nam, td));
6737493f24eSPawel Jakub Dawidek }
6747493f24eSPawel Jakub Dawidek 
6757493f24eSPawel Jakub Dawidek static int
676b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
677a29f300eSGarrett Wollman {
6780d9ce3a1SRobert Watson 	int error;
679a29f300eSGarrett Wollman 
680fd179ee9SRobert Watson 	KASSERT(td == curthread, ("uipc_connect: td != curthread"));
681fd179ee9SRobert Watson 	error = unp_connect(so, nam, td);
6820d9ce3a1SRobert Watson 	return (error);
683a29f300eSGarrett Wollman }
684a29f300eSGarrett Wollman 
6857493f24eSPawel Jakub Dawidek static int
6867493f24eSPawel Jakub Dawidek uipc_connectat(int fd, struct socket *so, struct sockaddr *nam,
6877493f24eSPawel Jakub Dawidek     struct thread *td)
6887493f24eSPawel Jakub Dawidek {
6897493f24eSPawel Jakub Dawidek 	int error;
6907493f24eSPawel Jakub Dawidek 
6917493f24eSPawel Jakub Dawidek 	KASSERT(td == curthread, ("uipc_connectat: td != curthread"));
6927493f24eSPawel Jakub Dawidek 	error = unp_connectat(fd, so, nam, td);
6937493f24eSPawel Jakub Dawidek 	return (error);
6947493f24eSPawel Jakub Dawidek }
6957493f24eSPawel Jakub Dawidek 
696a152f8a3SRobert Watson static void
697a152f8a3SRobert Watson uipc_close(struct socket *so)
698a152f8a3SRobert Watson {
699e7c33e29SRobert Watson 	struct unpcb *unp, *unp2;
700779f106aSGleb Smirnoff 	struct vnode *vp = NULL;
70175a67bf3SMatt Macy 	struct mtx *vplock;
70275a67bf3SMatt Macy 	int freed;
703a152f8a3SRobert Watson 	unp = sotounpcb(so);
704a152f8a3SRobert Watson 	KASSERT(unp != NULL, ("uipc_close: unp == NULL"));
705e7c33e29SRobert Watson 
70675a67bf3SMatt Macy 
70775a67bf3SMatt Macy 	vplock = NULL;
70875a67bf3SMatt Macy 	if ((vp = unp->unp_vnode) != NULL) {
70975a67bf3SMatt Macy 		vplock = mtx_pool_find(mtxpool_sleep, vp);
71075a67bf3SMatt Macy 		mtx_lock(vplock);
711e7c33e29SRobert Watson 	}
71275a67bf3SMatt Macy 	UNP_PCB_LOCK(unp);
71375a67bf3SMatt Macy 	if (vp && unp->unp_vnode == NULL) {
71475a67bf3SMatt Macy 		mtx_unlock(vplock);
71575a67bf3SMatt Macy 		vp = NULL;
71675a67bf3SMatt Macy 	}
71775a67bf3SMatt Macy 	if (vp != NULL) {
718779f106aSGleb Smirnoff 		VOP_UNP_DETACH(vp);
719779f106aSGleb Smirnoff 		unp->unp_vnode = NULL;
720779f106aSGleb Smirnoff 	}
72175a67bf3SMatt Macy 	unp2 = unp->unp_conn;
72275a67bf3SMatt Macy 	unp_pcb_hold(unp);
72375a67bf3SMatt Macy 	if (unp2 != NULL) {
72475a67bf3SMatt Macy 		unp_pcb_hold(unp2);
72575a67bf3SMatt Macy 		unp_pcb_owned_lock2(unp, unp2, freed);
72675a67bf3SMatt Macy 		unp_disconnect(unp, unp2);
72775a67bf3SMatt Macy 		if (unp_pcb_rele(unp2) == 0)
72875a67bf3SMatt Macy 			UNP_PCB_UNLOCK(unp2);
72975a67bf3SMatt Macy 	}
73075a67bf3SMatt Macy 	if (unp_pcb_rele(unp) == 0)
731e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
73275a67bf3SMatt Macy 	if (vp) {
73375a67bf3SMatt Macy 		mtx_unlock(vplock);
734779f106aSGleb Smirnoff 		vrele(vp);
735a152f8a3SRobert Watson 	}
73675a67bf3SMatt Macy }
737a152f8a3SRobert Watson 
7382c899584SRobert Watson static int
739a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2)
740a29f300eSGarrett Wollman {
741e7c33e29SRobert Watson 	struct unpcb *unp, *unp2;
7420d9ce3a1SRobert Watson 	int error;
743a29f300eSGarrett Wollman 
744e7c33e29SRobert Watson 	unp = so1->so_pcb;
7454d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_connect2: unp == NULL"));
746e7c33e29SRobert Watson 	unp2 = so2->so_pcb;
747e7c33e29SRobert Watson 	KASSERT(unp2 != NULL, ("uipc_connect2: unp2 == NULL"));
74875a67bf3SMatt Macy 	unp_pcb_lock2(unp, unp2);
7496a2989fdSMatthew N. Dodd 	error = unp_connect2(so1, so2, PRU_CONNECT2);
750e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp2);
751e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
7520d9ce3a1SRobert Watson 	return (error);
753a29f300eSGarrett Wollman }
754a29f300eSGarrett Wollman 
755bc725eafSRobert Watson static void
756a29f300eSGarrett Wollman uipc_detach(struct socket *so)
757a29f300eSGarrett Wollman {
758e7c33e29SRobert Watson 	struct unpcb *unp, *unp2;
75975a67bf3SMatt Macy 	struct mtx *vplock;
7609ae328fcSJohn Baldwin 	struct sockaddr_un *saved_unp_addr;
7616d32873cSRobert Watson 	struct vnode *vp;
7629ae328fcSJohn Baldwin 	int freeunp, local_unp_rights;
763a29f300eSGarrett Wollman 
76440f2ac28SRobert Watson 	unp = sotounpcb(so);
7654d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_detach: unp == NULL"));
766e7c33e29SRobert Watson 
767434ac8b6SMark Johnston 	vp = NULL;
768c0874c34SMatt Macy 	vplock = NULL;
769434ac8b6SMark Johnston 	local_unp_rights = 0;
770434ac8b6SMark Johnston 
771779f106aSGleb Smirnoff 	UNP_LINK_WLOCK();
7726d32873cSRobert Watson 	LIST_REMOVE(unp, unp_link);
7736d32873cSRobert Watson 	unp->unp_gencnt = ++unp_gencnt;
7746d32873cSRobert Watson 	--unp_count;
77575a67bf3SMatt Macy 	UNP_LINK_WUNLOCK();
776434ac8b6SMark Johnston 
77775a67bf3SMatt Macy 	UNP_PCB_UNLOCK_ASSERT(unp);
77875a67bf3SMatt Macy  restart:
77975a67bf3SMatt Macy 	if ((vp = unp->unp_vnode) != NULL) {
78075a67bf3SMatt Macy 		vplock = mtx_pool_find(mtxpool_sleep, vp);
78175a67bf3SMatt Macy 		mtx_lock(vplock);
78275a67bf3SMatt Macy 	}
78375a67bf3SMatt Macy 	UNP_PCB_LOCK(unp);
78475a67bf3SMatt Macy 	if ((unp2 = unp->unp_conn) != NULL) {
78575a67bf3SMatt Macy 		unp_pcb_owned_lock2(unp, unp2, freeunp);
78675a67bf3SMatt Macy 		if (freeunp)
78775a67bf3SMatt Macy 			unp2 = NULL;
78875a67bf3SMatt Macy 	}
78975a67bf3SMatt Macy 	if (unp->unp_vnode != vp &&
79075a67bf3SMatt Macy 		unp->unp_vnode != NULL) {
791c0874c34SMatt Macy 		if (vplock)
79275a67bf3SMatt Macy 			mtx_unlock(vplock);
79375a67bf3SMatt Macy 		UNP_PCB_UNLOCK(unp);
79475a67bf3SMatt Macy 		if (unp2)
79575a67bf3SMatt Macy 			UNP_PCB_UNLOCK(unp2);
79675a67bf3SMatt Macy 		goto restart;
79775a67bf3SMatt Macy 	}
79875a67bf3SMatt Macy 	if ((unp->unp_flags & UNP_NASCENT) != 0) {
79975a67bf3SMatt Macy 		if (unp2)
80075a67bf3SMatt Macy 			UNP_PCB_UNLOCK(unp2);
80175a67bf3SMatt Macy 		goto teardown;
80275a67bf3SMatt Macy 	}
8036d32873cSRobert Watson 	if ((vp = unp->unp_vnode) != NULL) {
804c7e41c8bSMikolaj Golub 		VOP_UNP_DETACH(vp);
8056d32873cSRobert Watson 		unp->unp_vnode = NULL;
8066d32873cSRobert Watson 	}
80775a67bf3SMatt Macy 	unp_pcb_hold(unp);
808e7c33e29SRobert Watson 	if (unp2 != NULL) {
80975a67bf3SMatt Macy 		unp_pcb_hold(unp2);
810e7c33e29SRobert Watson 		unp_disconnect(unp, unp2);
81175a67bf3SMatt Macy 		if (unp_pcb_rele(unp2) == 0)
812e7c33e29SRobert Watson 			UNP_PCB_UNLOCK(unp2);
813e7c33e29SRobert Watson 	}
81475a67bf3SMatt Macy 	UNP_PCB_UNLOCK(unp);
81575a67bf3SMatt Macy 	UNP_REF_LIST_LOCK();
8166d32873cSRobert Watson 	while (!LIST_EMPTY(&unp->unp_refs)) {
8176d32873cSRobert Watson 		struct unpcb *ref = LIST_FIRST(&unp->unp_refs);
818e7c33e29SRobert Watson 
81975a67bf3SMatt Macy 		unp_pcb_hold(ref);
82075a67bf3SMatt Macy 		UNP_REF_LIST_UNLOCK();
82175a67bf3SMatt Macy 
82275a67bf3SMatt Macy 		MPASS(ref != unp);
82375a67bf3SMatt Macy 		UNP_PCB_UNLOCK_ASSERT(ref);
824afc055d9SEd Schouten 		unp_drop(ref);
82575a67bf3SMatt Macy 		UNP_REF_LIST_LOCK();
8266d32873cSRobert Watson 	}
82775a67bf3SMatt Macy 
82875a67bf3SMatt Macy 	UNP_REF_LIST_UNLOCK();
82975a67bf3SMatt Macy 	UNP_PCB_LOCK(unp);
83075a67bf3SMatt Macy 	freeunp = unp_pcb_rele(unp);
83175a67bf3SMatt Macy 	MPASS(freeunp == 0);
832397c19d1SJeff Roberson 	local_unp_rights = unp_rights;
833434ac8b6SMark Johnston teardown:
8346d32873cSRobert Watson 	unp->unp_socket->so_pcb = NULL;
8359ae328fcSJohn Baldwin 	saved_unp_addr = unp->unp_addr;
8369ae328fcSJohn Baldwin 	unp->unp_addr = NULL;
83775a67bf3SMatt Macy 	unp->unp_socket = NULL;
83875a67bf3SMatt Macy 	freeunp = unp_pcb_rele(unp);
8399ae328fcSJohn Baldwin 	if (saved_unp_addr != NULL)
8401ede983cSDag-Erling Smørgrav 		free(saved_unp_addr, M_SONAME);
84175a67bf3SMatt Macy 	if (!freeunp)
8426e2faa24SRobert Watson 		UNP_PCB_UNLOCK(unp);
84375a67bf3SMatt Macy 	if (vp) {
84475a67bf3SMatt Macy 		mtx_unlock(vplock);
8456d32873cSRobert Watson 		vrele(vp);
84675a67bf3SMatt Macy 	}
8476d32873cSRobert Watson 	if (local_unp_rights)
848daee0f0bSKonstantin Belousov 		taskqueue_enqueue_timeout(taskqueue_thread, &unp_gc_task, -1);
849a29f300eSGarrett Wollman }
850a29f300eSGarrett Wollman 
851a29f300eSGarrett Wollman static int
852a29f300eSGarrett Wollman uipc_disconnect(struct socket *so)
853a29f300eSGarrett Wollman {
854e7c33e29SRobert Watson 	struct unpcb *unp, *unp2;
85575a67bf3SMatt Macy 	int freed;
856a29f300eSGarrett Wollman 
85740f2ac28SRobert Watson 	unp = sotounpcb(so);
8584d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_disconnect: unp == NULL"));
859e7c33e29SRobert Watson 
860e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
86175a67bf3SMatt Macy 	if ((unp2 = unp->unp_conn) == NULL) {
862e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
86375a67bf3SMatt Macy 		return (0);
86475a67bf3SMatt Macy 	}
86575a67bf3SMatt Macy 	unp_pcb_owned_lock2(unp, unp2, freed);
86675a67bf3SMatt Macy 	if (__predict_false(freed)) {
86775a67bf3SMatt Macy 		UNP_PCB_UNLOCK(unp);
86875a67bf3SMatt Macy 		return (0);
86975a67bf3SMatt Macy 	}
87075a67bf3SMatt Macy 	unp_pcb_hold(unp2);
87175a67bf3SMatt Macy 	unp_pcb_hold(unp);
87275a67bf3SMatt Macy 	unp_disconnect(unp, unp2);
87375a67bf3SMatt Macy 	if (unp_pcb_rele(unp) == 0)
87475a67bf3SMatt Macy 		UNP_PCB_UNLOCK(unp);
87575a67bf3SMatt Macy 	if (unp_pcb_rele(unp2) == 0)
87675a67bf3SMatt Macy 		UNP_PCB_UNLOCK(unp2);
877e5aeaa0cSDag-Erling Smørgrav 	return (0);
878a29f300eSGarrett Wollman }
879a29f300eSGarrett Wollman 
880a29f300eSGarrett Wollman static int
881d374e81eSRobert Watson uipc_listen(struct socket *so, int backlog, struct thread *td)
882a29f300eSGarrett Wollman {
88340f2ac28SRobert Watson 	struct unpcb *unp;
8840d9ce3a1SRobert Watson 	int error;
885a29f300eSGarrett Wollman 
886beb4b312SGleb Smirnoff 	if (so->so_type != SOCK_STREAM && so->so_type != SOCK_SEQPACKET)
887beb4b312SGleb Smirnoff 		return (EOPNOTSUPP);
888beb4b312SGleb Smirnoff 
88940f2ac28SRobert Watson 	unp = sotounpcb(so);
8904d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_listen: unp == NULL"));
891e7c33e29SRobert Watson 
892e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
8934d4b555eSRobert Watson 	if (unp->unp_vnode == NULL) {
89447a84387SEd Schouten 		/* Already connected or not bound to an address. */
89547a84387SEd Schouten 		error = unp->unp_conn != NULL ? EINVAL : EDESTADDRREQ;
896e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
89747a84387SEd Schouten 		return (error);
89840f2ac28SRobert Watson 	}
899e7c33e29SRobert Watson 
900e7c33e29SRobert Watson 	SOCK_LOCK(so);
901e7c33e29SRobert Watson 	error = solisten_proto_check(so);
902e7c33e29SRobert Watson 	if (error == 0) {
903e7c33e29SRobert Watson 		cru2x(td->td_ucred, &unp->unp_peercred);
904e7c33e29SRobert Watson 		solisten_proto(so, backlog);
905e7c33e29SRobert Watson 	}
906e7c33e29SRobert Watson 	SOCK_UNLOCK(so);
907e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
9080d9ce3a1SRobert Watson 	return (error);
909a29f300eSGarrett Wollman }
910a29f300eSGarrett Wollman 
911a29f300eSGarrett Wollman static int
91257bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam)
913a29f300eSGarrett Wollman {
914e7c33e29SRobert Watson 	struct unpcb *unp, *unp2;
9150d9ce3a1SRobert Watson 	const struct sockaddr *sa;
916a29f300eSGarrett Wollman 
9174d4b555eSRobert Watson 	unp = sotounpcb(so);
9184d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL"));
919e7c33e29SRobert Watson 
9200d9ce3a1SRobert Watson 	*nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
921afd9f91cSJohn Baldwin 	UNP_LINK_RLOCK();
922bdc5f6a3SHajimu UMEMOTO 	/*
923e7c33e29SRobert Watson 	 * XXX: It seems that this test always fails even when connection is
924e7c33e29SRobert Watson 	 * established.  So, this else clause is added as workaround to
925e7c33e29SRobert Watson 	 * return PF_LOCAL sockaddr.
926bdc5f6a3SHajimu UMEMOTO 	 */
927e7c33e29SRobert Watson 	unp2 = unp->unp_conn;
928e7c33e29SRobert Watson 	if (unp2 != NULL) {
929e7c33e29SRobert Watson 		UNP_PCB_LOCK(unp2);
930e7c33e29SRobert Watson 		if (unp2->unp_addr != NULL)
931afd9f91cSJohn Baldwin 			sa = (struct sockaddr *) unp2->unp_addr;
932e7c33e29SRobert Watson 		else
9330d9ce3a1SRobert Watson 			sa = &sun_noname;
9340d9ce3a1SRobert Watson 		bcopy(sa, *nam, sa->sa_len);
935e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp2);
936e7c33e29SRobert Watson 	} else {
937e7c33e29SRobert Watson 		sa = &sun_noname;
938e7c33e29SRobert Watson 		bcopy(sa, *nam, sa->sa_len);
939e7c33e29SRobert Watson 	}
940afd9f91cSJohn Baldwin 	UNP_LINK_RUNLOCK();
941e5aeaa0cSDag-Erling Smørgrav 	return (0);
942a29f300eSGarrett Wollman }
943a29f300eSGarrett Wollman 
944a29f300eSGarrett Wollman static int
945a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags)
946a29f300eSGarrett Wollman {
947e7c33e29SRobert Watson 	struct unpcb *unp, *unp2;
948a29f300eSGarrett Wollman 	struct socket *so2;
949337cc6b6SRobert Watson 	u_int mbcnt, sbcc;
950a29f300eSGarrett Wollman 
95140f2ac28SRobert Watson 	unp = sotounpcb(so);
9522b21d0e8SGleb Smirnoff 	KASSERT(unp != NULL, ("%s: unp == NULL", __func__));
9532b21d0e8SGleb Smirnoff 	KASSERT(so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET,
9542b21d0e8SGleb Smirnoff 	    ("%s: socktype %d", __func__, so->so_type));
955e7c33e29SRobert Watson 
956df8bae1dSRodney W. Grimes 	/*
957e7c33e29SRobert Watson 	 * Adjust backpressure on sender and wakeup any waiting to write.
958e7c33e29SRobert Watson 	 *
959d7924b70SRobert Watson 	 * The unp lock is acquired to maintain the validity of the unp_conn
960d7924b70SRobert Watson 	 * pointer; no lock on unp2 is required as unp2->unp_socket will be
961d7924b70SRobert Watson 	 * static as long as we don't permit unp2 to disconnect from unp,
962d7924b70SRobert Watson 	 * which is prevented by the lock on unp.  We cache values from
963d7924b70SRobert Watson 	 * so_rcv to avoid holding the so_rcv lock over the entire
964d7924b70SRobert Watson 	 * transaction on the remote so_snd.
965df8bae1dSRodney W. Grimes 	 */
966337cc6b6SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
967337cc6b6SRobert Watson 	mbcnt = so->so_rcv.sb_mbcnt;
9682b21d0e8SGleb Smirnoff 	sbcc = sbavail(&so->so_rcv);
969337cc6b6SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
970c2090e73SAlan Somers 	/*
971c2090e73SAlan Somers 	 * There is a benign race condition at this point.  If we're planning to
972c2090e73SAlan Somers 	 * clear SB_STOP, but uipc_send is called on the connected socket at
973c2090e73SAlan Somers 	 * this instant, it might add data to the sockbuf and set SB_STOP.  Then
974c2090e73SAlan Somers 	 * we would erroneously clear SB_STOP below, even though the sockbuf is
975c2090e73SAlan Somers 	 * full.  The race is benign because the only ill effect is to allow the
976c2090e73SAlan Somers 	 * sockbuf to exceed its size limit, and the size limits are not
977c2090e73SAlan Somers 	 * strictly guaranteed anyway.
978c2090e73SAlan Somers 	 */
979e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
980e7c33e29SRobert Watson 	unp2 = unp->unp_conn;
981e7c33e29SRobert Watson 	if (unp2 == NULL) {
982e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
983e7c33e29SRobert Watson 		return (0);
984337cc6b6SRobert Watson 	}
985e7c33e29SRobert Watson 	so2 = unp2->unp_socket;
986337cc6b6SRobert Watson 	SOCKBUF_LOCK(&so2->so_snd);
987c2090e73SAlan Somers 	if (sbcc < so2->so_snd.sb_hiwat && mbcnt < so2->so_snd.sb_mbmax)
988c2090e73SAlan Somers 		so2->so_snd.sb_flags &= ~SB_STOP;
9891e4d7da7SRobert Watson 	sowwakeup_locked(so2);
990e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
991e5aeaa0cSDag-Erling Smørgrav 	return (0);
992a29f300eSGarrett Wollman }
993df8bae1dSRodney W. Grimes 
994a29f300eSGarrett Wollman static int
99575a67bf3SMatt Macy connect_internal(struct socket *so, struct sockaddr *nam, struct thread *td)
99675a67bf3SMatt Macy {
99775a67bf3SMatt Macy 	int error;
99875a67bf3SMatt Macy 	struct unpcb *unp;
99975a67bf3SMatt Macy 
100075a67bf3SMatt Macy 	unp = so->so_pcb;
100175a67bf3SMatt Macy 	if (unp->unp_conn != NULL)
100275a67bf3SMatt Macy 		return (EISCONN);
100375a67bf3SMatt Macy 	error = unp_connect(so, nam, td);
100475a67bf3SMatt Macy 	if (error)
100575a67bf3SMatt Macy 		return (error);
100675a67bf3SMatt Macy 	UNP_PCB_LOCK(unp);
100775a67bf3SMatt Macy 	if (unp->unp_conn == NULL) {
100875a67bf3SMatt Macy 		UNP_PCB_UNLOCK(unp);
100975a67bf3SMatt Macy 		if (error == 0)
101075a67bf3SMatt Macy 			error = ENOTCONN;
101175a67bf3SMatt Macy 	}
101275a67bf3SMatt Macy 	return (error);
101375a67bf3SMatt Macy }
101475a67bf3SMatt Macy 
101575a67bf3SMatt Macy 
101675a67bf3SMatt Macy static int
101757bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
1018b40ce416SJulian Elischer     struct mbuf *control, struct thread *td)
1019a29f300eSGarrett Wollman {
1020f3f49bbbSRobert Watson 	struct unpcb *unp, *unp2;
1021a29f300eSGarrett Wollman 	struct socket *so2;
1022c2090e73SAlan Somers 	u_int mbcnt, sbcc;
102375a67bf3SMatt Macy 	int freed, error;
1024a29f300eSGarrett Wollman 
102540f2ac28SRobert Watson 	unp = sotounpcb(so);
10262b21d0e8SGleb Smirnoff 	KASSERT(unp != NULL, ("%s: unp == NULL", __func__));
10272b21d0e8SGleb Smirnoff 	KASSERT(so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM ||
10282b21d0e8SGleb Smirnoff 	    so->so_type == SOCK_SEQPACKET,
10292b21d0e8SGleb Smirnoff 	    ("%s: socktype %d", __func__, so->so_type));
1030e7c33e29SRobert Watson 
103175a67bf3SMatt Macy 	freed = error = 0;
1032a29f300eSGarrett Wollman 	if (flags & PRUS_OOB) {
1033a29f300eSGarrett Wollman 		error = EOPNOTSUPP;
1034a29f300eSGarrett Wollman 		goto release;
1035a29f300eSGarrett Wollman 	}
1036fc3fcacfSRobert Watson 	if (control != NULL && (error = unp_internalize(&control, td)))
1037a29f300eSGarrett Wollman 		goto release;
103875a67bf3SMatt Macy 
103975a67bf3SMatt Macy 	unp2 = NULL;
1040a29f300eSGarrett Wollman 	switch (so->so_type) {
1041a29f300eSGarrett Wollman 	case SOCK_DGRAM:
1042a29f300eSGarrett Wollman 	{
1043e7dd9a10SRobert Watson 		const struct sockaddr *from;
1044df8bae1dSRodney W. Grimes 
1045fc3fcacfSRobert Watson 		if (nam != NULL) {
104675a67bf3SMatt Macy 			/*
104775a67bf3SMatt Macy 			 * We return with UNP_PCB_LOCK_HELD so we know that
104875a67bf3SMatt Macy 			 * the reference is live if the pointer is valid.
104975a67bf3SMatt Macy 			 */
105075a67bf3SMatt Macy 			if ((error = connect_internal(so, nam, td)))
1051df8bae1dSRodney W. Grimes 				break;
105275a67bf3SMatt Macy 			MPASS(unp->unp_conn != NULL);
1053e7c33e29SRobert Watson 			unp2 = unp->unp_conn;
105475a67bf3SMatt Macy 		} else  {
105575a67bf3SMatt Macy 			UNP_PCB_LOCK(unp);
105660a5ef26SRobert Watson 
1057b5ff0914SRobert Watson 			/*
1058b5ff0914SRobert Watson 			 * Because connect() and send() are non-atomic in a sendto()
1059b5ff0914SRobert Watson 			 * with a target address, it's possible that the socket will
1060b5ff0914SRobert Watson 			 * have disconnected before the send() can run.  In that case
1061b5ff0914SRobert Watson 			 * return the slightly counter-intuitive but otherwise
1062b5ff0914SRobert Watson 			 * correct error that the socket is not connected.
1063b5ff0914SRobert Watson 			 */
106475a67bf3SMatt Macy 			if ((unp2 = unp->unp_conn)  == NULL) {
106575a67bf3SMatt Macy 				UNP_PCB_UNLOCK(unp);
1066b5ff0914SRobert Watson 				error = ENOTCONN;
1067b5ff0914SRobert Watson 				break;
1068b5ff0914SRobert Watson 			}
106975a67bf3SMatt Macy 		}
107075a67bf3SMatt Macy 		unp_pcb_owned_lock2(unp, unp2, freed);
107175a67bf3SMatt Macy 		if (__predict_false(freed)) {
107275a67bf3SMatt Macy 			UNP_PCB_UNLOCK(unp);
107375a67bf3SMatt Macy 			error = ENOTCONN;
107475a67bf3SMatt Macy 			break;
107575a67bf3SMatt Macy 		}
107675a67bf3SMatt Macy 		/*
107775a67bf3SMatt Macy 		 * The socket referencing unp2 may have been closed
107875a67bf3SMatt Macy 		 * or unp may have been disconnected if the unp lock
107975a67bf3SMatt Macy 		 * was dropped to acquire unp2.
108075a67bf3SMatt Macy 		 */
108175a67bf3SMatt Macy 		if (__predict_false(unp->unp_conn == NULL) ||
108275a67bf3SMatt Macy 			unp2->unp_socket == NULL) {
108375a67bf3SMatt Macy 			UNP_PCB_UNLOCK(unp);
108475a67bf3SMatt Macy 			if (unp_pcb_rele(unp2) == 0)
108575a67bf3SMatt Macy 				UNP_PCB_UNLOCK(unp2);
108675a67bf3SMatt Macy 			error = ENOTCONN;
108775a67bf3SMatt Macy 			break;
108875a67bf3SMatt Macy 		}
1089ede6e136SRobert Watson 		if (unp2->unp_flags & UNP_WANTCRED)
1090ede6e136SRobert Watson 			control = unp_addsockcred(td, control);
1091fc3fcacfSRobert Watson 		if (unp->unp_addr != NULL)
109257bf258eSGarrett Wollman 			from = (struct sockaddr *)unp->unp_addr;
1093df8bae1dSRodney W. Grimes 		else
1094df8bae1dSRodney W. Grimes 			from = &sun_noname;
1095ede6e136SRobert Watson 		so2 = unp2->unp_socket;
1096a34b7046SRobert Watson 		SOCKBUF_LOCK(&so2->so_rcv);
10976dde7ecbSPeter Wemm 		if (sbappendaddr_locked(&so2->so_rcv, from, m,
10988de34a88SAlan Somers 		    control)) {
10991e4d7da7SRobert Watson 			sorwakeup_locked(so2);
1100fc3fcacfSRobert Watson 			m = NULL;
1101fc3fcacfSRobert Watson 			control = NULL;
1102e5aeaa0cSDag-Erling Smørgrav 		} else {
1103a34b7046SRobert Watson 			SOCKBUF_UNLOCK(&so2->so_rcv);
1104df8bae1dSRodney W. Grimes 			error = ENOBUFS;
1105e5aeaa0cSDag-Erling Smørgrav 		}
110675a67bf3SMatt Macy 		if (nam != NULL)
1107e7c33e29SRobert Watson 			unp_disconnect(unp, unp2);
1108e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp2);
1109ede6e136SRobert Watson 		UNP_PCB_UNLOCK(unp);
1110df8bae1dSRodney W. Grimes 		break;
1111df8bae1dSRodney W. Grimes 	}
1112df8bae1dSRodney W. Grimes 
111384d61770SRobert Watson 	case SOCK_SEQPACKET:
1114df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
1115402cc72dSDavid Greenman 		if ((so->so_state & SS_ISCONNECTED) == 0) {
1116fc3fcacfSRobert Watson 			if (nam != NULL) {
111775a67bf3SMatt Macy 				if ((error = connect_internal(so, nam, td)))
111875a67bf3SMatt Macy 					break;
1119402cc72dSDavid Greenman 			} else  {
1120402cc72dSDavid Greenman 				error = ENOTCONN;
1121402cc72dSDavid Greenman 				break;
1122402cc72dSDavid Greenman 			}
112375a67bf3SMatt Macy 		} else if ((unp2 = unp->unp_conn) == NULL) {
112475a67bf3SMatt Macy 			error = ENOTCONN;
112575a67bf3SMatt Macy 			break;
112675a67bf3SMatt Macy 		} else if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1127df8bae1dSRodney W. Grimes 			error = EPIPE;
1128df8bae1dSRodney W. Grimes 			break;
112975a67bf3SMatt Macy 		} else {
113075a67bf3SMatt Macy 			UNP_PCB_LOCK(unp);
113175a67bf3SMatt Macy 			if ((unp2 = unp->unp_conn) == NULL) {
113275a67bf3SMatt Macy 				UNP_PCB_UNLOCK(unp);
1133b5ff0914SRobert Watson 				error = ENOTCONN;
1134b5ff0914SRobert Watson 				break;
1135b5ff0914SRobert Watson 			}
113675a67bf3SMatt Macy 		}
113775a67bf3SMatt Macy 		unp_pcb_owned_lock2(unp, unp2, freed);
113875a67bf3SMatt Macy 		UNP_PCB_UNLOCK(unp);
113975a67bf3SMatt Macy 		if (__predict_false(freed)) {
114075a67bf3SMatt Macy 			error = ENOTCONN;
114175a67bf3SMatt Macy 			break;
114275a67bf3SMatt Macy 		}
114375a67bf3SMatt Macy 		if ((so2 = unp2->unp_socket) == NULL) {
114475a67bf3SMatt Macy 			UNP_PCB_UNLOCK(unp2);
114575a67bf3SMatt Macy 			error = ENOTCONN;
114675a67bf3SMatt Macy 			break;
114775a67bf3SMatt Macy 		}
1148a34b7046SRobert Watson 		SOCKBUF_LOCK(&so2->so_rcv);
1149f3f49bbbSRobert Watson 		if (unp2->unp_flags & UNP_WANTCRED) {
11506a2989fdSMatthew N. Dodd 			/*
1151716963cbSGleb Smirnoff 			 * Credentials are passed only once on SOCK_STREAM
1152716963cbSGleb Smirnoff 			 * and SOCK_SEQPACKET.
11536a2989fdSMatthew N. Dodd 			 */
1154f3f49bbbSRobert Watson 			unp2->unp_flags &= ~UNP_WANTCRED;
11556a2989fdSMatthew N. Dodd 			control = unp_addsockcred(td, control);
11566a2989fdSMatthew N. Dodd 		}
1157df8bae1dSRodney W. Grimes 		/*
11581c381b19SRobert Watson 		 * Send to paired receive port, and then reduce send buffer
11591c381b19SRobert Watson 		 * hiwater marks to maintain backpressure.  Wake up readers.
1160df8bae1dSRodney W. Grimes 		 */
116184d61770SRobert Watson 		switch (so->so_type) {
116284d61770SRobert Watson 		case SOCK_STREAM:
1163fc3fcacfSRobert Watson 			if (control != NULL) {
116484d61770SRobert Watson 				if (sbappendcontrol_locked(&so2->so_rcv, m,
116584d61770SRobert Watson 				    control))
1166fc3fcacfSRobert Watson 					control = NULL;
1167e7c33e29SRobert Watson 			} else
1168829fae90SGleb Smirnoff 				sbappend_locked(&so2->so_rcv, m, flags);
116984d61770SRobert Watson 			break;
117084d61770SRobert Watson 
117184d61770SRobert Watson 		case SOCK_SEQPACKET: {
117284d61770SRobert Watson 			const struct sockaddr *from;
117384d61770SRobert Watson 
117484d61770SRobert Watson 			from = &sun_noname;
11758de34a88SAlan Somers 			/*
11768de34a88SAlan Somers 			 * Don't check for space available in so2->so_rcv.
11778de34a88SAlan Somers 			 * Unix domain sockets only check for space in the
11788de34a88SAlan Somers 			 * sending sockbuf, and that check is performed one
11798de34a88SAlan Somers 			 * level up the stack.
11808de34a88SAlan Somers 			 */
11818de34a88SAlan Somers 			if (sbappendaddr_nospacecheck_locked(&so2->so_rcv,
11828de34a88SAlan Somers 				from, m, control))
118384d61770SRobert Watson 				control = NULL;
118484d61770SRobert Watson 			break;
118584d61770SRobert Watson 			}
118684d61770SRobert Watson 		}
118784d61770SRobert Watson 
1188c2090e73SAlan Somers 		mbcnt = so2->so_rcv.sb_mbcnt;
11892b21d0e8SGleb Smirnoff 		sbcc = sbavail(&so2->so_rcv);
11902b21d0e8SGleb Smirnoff 		if (sbcc)
1191337cc6b6SRobert Watson 			sorwakeup_locked(so2);
11922b21d0e8SGleb Smirnoff 		else
11932b21d0e8SGleb Smirnoff 			SOCKBUF_UNLOCK(&so2->so_rcv);
1194337cc6b6SRobert Watson 
1195c2090e73SAlan Somers 		/*
1196c2090e73SAlan Somers 		 * The PCB lock on unp2 protects the SB_STOP flag.  Without it,
1197c2090e73SAlan Somers 		 * it would be possible for uipc_rcvd to be called at this
1198c2090e73SAlan Somers 		 * point, drain the receiving sockbuf, clear SB_STOP, and then
1199c2090e73SAlan Somers 		 * we would set SB_STOP below.  That could lead to an empty
1200c2090e73SAlan Somers 		 * sockbuf having SB_STOP set
1201c2090e73SAlan Somers 		 */
1202337cc6b6SRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
1203c2090e73SAlan Somers 		if (sbcc >= so->so_snd.sb_hiwat || mbcnt >= so->so_snd.sb_mbmax)
1204c2090e73SAlan Somers 			so->so_snd.sb_flags |= SB_STOP;
12057abe2ac2SAlan Cox 		SOCKBUF_UNLOCK(&so->so_snd);
1206e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp2);
1207fc3fcacfSRobert Watson 		m = NULL;
1208df8bae1dSRodney W. Grimes 		break;
1209df8bae1dSRodney W. Grimes 	}
1210a29f300eSGarrett Wollman 
12116b8fda4dSGarrett Wollman 	/*
121260a5ef26SRobert Watson 	 * PRUS_EOF is equivalent to pru_send followed by pru_shutdown.
12136b8fda4dSGarrett Wollman 	 */
1214a29f300eSGarrett Wollman 	if (flags & PRUS_EOF) {
1215ede6e136SRobert Watson 		UNP_PCB_LOCK(unp);
12166b8fda4dSGarrett Wollman 		socantsendmore(so);
12176b8fda4dSGarrett Wollman 		unp_shutdown(unp);
1218e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
1219ede6e136SRobert Watson 	}
1220fc3fcacfSRobert Watson 	if (control != NULL && error != 0)
122199ab95dbSMark Johnston 		unp_dispose_mbuf(control);
1222bd508d39SDon Lewis 
1223a29f300eSGarrett Wollman release:
1224fc3fcacfSRobert Watson 	if (control != NULL)
1225a29f300eSGarrett Wollman 		m_freem(control);
1226100db364SGleb Smirnoff 	/*
1227100db364SGleb Smirnoff 	 * In case of PRUS_NOTREADY, uipc_ready() is responsible
1228100db364SGleb Smirnoff 	 * for freeing memory.
1229100db364SGleb Smirnoff 	 */
1230100db364SGleb Smirnoff 	if (m != NULL && (flags & PRUS_NOTREADY) == 0)
1231a29f300eSGarrett Wollman 		m_freem(m);
1232e5aeaa0cSDag-Erling Smørgrav 	return (error);
1233a29f300eSGarrett Wollman }
1234df8bae1dSRodney W. Grimes 
1235a29f300eSGarrett Wollman static int
1236c80ea19bSGleb Smirnoff uipc_ready(struct socket *so, struct mbuf *m, int count)
1237c80ea19bSGleb Smirnoff {
1238c80ea19bSGleb Smirnoff 	struct unpcb *unp, *unp2;
1239c80ea19bSGleb Smirnoff 	struct socket *so2;
1240c80ea19bSGleb Smirnoff 	int error;
1241c80ea19bSGleb Smirnoff 
1242c80ea19bSGleb Smirnoff 	unp = sotounpcb(so);
1243c80ea19bSGleb Smirnoff 
1244c80ea19bSGleb Smirnoff 	UNP_LINK_RLOCK();
1245100db364SGleb Smirnoff 	if ((unp2 = unp->unp_conn) == NULL) {
1246100db364SGleb Smirnoff 		UNP_LINK_RUNLOCK();
1247100db364SGleb Smirnoff 		for (int i = 0; i < count; i++)
1248100db364SGleb Smirnoff 			m = m_free(m);
1249100db364SGleb Smirnoff 		return (ECONNRESET);
1250100db364SGleb Smirnoff 	}
1251c80ea19bSGleb Smirnoff 	UNP_PCB_LOCK(unp2);
1252c80ea19bSGleb Smirnoff 	so2 = unp2->unp_socket;
1253c80ea19bSGleb Smirnoff 
1254c80ea19bSGleb Smirnoff 	SOCKBUF_LOCK(&so2->so_rcv);
1255c80ea19bSGleb Smirnoff 	if ((error = sbready(&so2->so_rcv, m, count)) == 0)
1256c80ea19bSGleb Smirnoff 		sorwakeup_locked(so2);
1257c80ea19bSGleb Smirnoff 	else
1258c80ea19bSGleb Smirnoff 		SOCKBUF_UNLOCK(&so2->so_rcv);
1259c80ea19bSGleb Smirnoff 
1260c80ea19bSGleb Smirnoff 	UNP_PCB_UNLOCK(unp2);
1261c80ea19bSGleb Smirnoff 	UNP_LINK_RUNLOCK();
1262c80ea19bSGleb Smirnoff 
1263c80ea19bSGleb Smirnoff 	return (error);
1264c80ea19bSGleb Smirnoff }
1265c80ea19bSGleb Smirnoff 
1266c80ea19bSGleb Smirnoff static int
1267a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb)
1268a29f300eSGarrett Wollman {
1269c2090e73SAlan Somers 	struct unpcb *unp;
1270a29f300eSGarrett Wollman 
127140f2ac28SRobert Watson 	unp = sotounpcb(so);
12724d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_sense: unp == NULL"));
1273e7c33e29SRobert Watson 
1274a29f300eSGarrett Wollman 	sb->st_blksize = so->so_snd.sb_hiwat;
1275e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
1276f3732fd1SPoul-Henning Kamp 	sb->st_dev = NODEV;
1277df8bae1dSRodney W. Grimes 	if (unp->unp_ino == 0)
12786f782c46SJeffrey Hsu 		unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino;
1279a29f300eSGarrett Wollman 	sb->st_ino = unp->unp_ino;
1280e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
1281df8bae1dSRodney W. Grimes 	return (0);
1282a29f300eSGarrett Wollman }
1283df8bae1dSRodney W. Grimes 
1284a29f300eSGarrett Wollman static int
1285a29f300eSGarrett Wollman uipc_shutdown(struct socket *so)
1286a29f300eSGarrett Wollman {
128740f2ac28SRobert Watson 	struct unpcb *unp;
1288df8bae1dSRodney W. Grimes 
128940f2ac28SRobert Watson 	unp = sotounpcb(so);
12904d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_shutdown: unp == NULL"));
1291e7c33e29SRobert Watson 
1292e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
1293a29f300eSGarrett Wollman 	socantsendmore(so);
1294a29f300eSGarrett Wollman 	unp_shutdown(unp);
1295e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
1296e5aeaa0cSDag-Erling Smørgrav 	return (0);
1297a29f300eSGarrett Wollman }
1298df8bae1dSRodney W. Grimes 
1299a29f300eSGarrett Wollman static int
130057bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam)
1301a29f300eSGarrett Wollman {
130240f2ac28SRobert Watson 	struct unpcb *unp;
13030d9ce3a1SRobert Watson 	const struct sockaddr *sa;
1304a29f300eSGarrett Wollman 
13054d4b555eSRobert Watson 	unp = sotounpcb(so);
13064d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL"));
1307e7c33e29SRobert Watson 
13080d9ce3a1SRobert Watson 	*nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
1309e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
1310fc3fcacfSRobert Watson 	if (unp->unp_addr != NULL)
13110d9ce3a1SRobert Watson 		sa = (struct sockaddr *) unp->unp_addr;
131283f3198bSThomas Moestl 	else
13130d9ce3a1SRobert Watson 		sa = &sun_noname;
13140d9ce3a1SRobert Watson 	bcopy(sa, *nam, sa->sa_len);
1315e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
1316e5aeaa0cSDag-Erling Smørgrav 	return (0);
1317df8bae1dSRodney W. Grimes }
1318a29f300eSGarrett Wollman 
1319fa9402f2SRobert Watson static struct pr_usrreqs uipc_usrreqs_dgram = {
1320756d52a1SPoul-Henning Kamp 	.pru_abort = 		uipc_abort,
1321756d52a1SPoul-Henning Kamp 	.pru_accept =		uipc_accept,
1322756d52a1SPoul-Henning Kamp 	.pru_attach =		uipc_attach,
1323756d52a1SPoul-Henning Kamp 	.pru_bind =		uipc_bind,
13247493f24eSPawel Jakub Dawidek 	.pru_bindat =		uipc_bindat,
1325756d52a1SPoul-Henning Kamp 	.pru_connect =		uipc_connect,
13267493f24eSPawel Jakub Dawidek 	.pru_connectat =	uipc_connectat,
1327756d52a1SPoul-Henning Kamp 	.pru_connect2 =		uipc_connect2,
1328756d52a1SPoul-Henning Kamp 	.pru_detach =		uipc_detach,
1329756d52a1SPoul-Henning Kamp 	.pru_disconnect =	uipc_disconnect,
1330756d52a1SPoul-Henning Kamp 	.pru_listen =		uipc_listen,
1331756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		uipc_peeraddr,
1332756d52a1SPoul-Henning Kamp 	.pru_rcvd =		uipc_rcvd,
1333756d52a1SPoul-Henning Kamp 	.pru_send =		uipc_send,
1334756d52a1SPoul-Henning Kamp 	.pru_sense =		uipc_sense,
1335756d52a1SPoul-Henning Kamp 	.pru_shutdown =		uipc_shutdown,
1336756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		uipc_sockaddr,
1337fa9402f2SRobert Watson 	.pru_soreceive =	soreceive_dgram,
1338fa9402f2SRobert Watson 	.pru_close =		uipc_close,
1339fa9402f2SRobert Watson };
1340fa9402f2SRobert Watson 
134184d61770SRobert Watson static struct pr_usrreqs uipc_usrreqs_seqpacket = {
134284d61770SRobert Watson 	.pru_abort =		uipc_abort,
134384d61770SRobert Watson 	.pru_accept =		uipc_accept,
134484d61770SRobert Watson 	.pru_attach =		uipc_attach,
134584d61770SRobert Watson 	.pru_bind =		uipc_bind,
13467493f24eSPawel Jakub Dawidek 	.pru_bindat =		uipc_bindat,
134784d61770SRobert Watson 	.pru_connect =		uipc_connect,
13487493f24eSPawel Jakub Dawidek 	.pru_connectat =	uipc_connectat,
134984d61770SRobert Watson 	.pru_connect2 =		uipc_connect2,
135084d61770SRobert Watson 	.pru_detach =		uipc_detach,
135184d61770SRobert Watson 	.pru_disconnect =	uipc_disconnect,
135284d61770SRobert Watson 	.pru_listen =		uipc_listen,
135384d61770SRobert Watson 	.pru_peeraddr =		uipc_peeraddr,
135484d61770SRobert Watson 	.pru_rcvd =		uipc_rcvd,
135584d61770SRobert Watson 	.pru_send =		uipc_send,
135684d61770SRobert Watson 	.pru_sense =		uipc_sense,
135784d61770SRobert Watson 	.pru_shutdown =		uipc_shutdown,
135884d61770SRobert Watson 	.pru_sockaddr =		uipc_sockaddr,
135984d61770SRobert Watson 	.pru_soreceive =	soreceive_generic,	/* XXX: or...? */
136084d61770SRobert Watson 	.pru_close =		uipc_close,
136184d61770SRobert Watson };
136284d61770SRobert Watson 
1363fa9402f2SRobert Watson static struct pr_usrreqs uipc_usrreqs_stream = {
1364fa9402f2SRobert Watson 	.pru_abort = 		uipc_abort,
1365fa9402f2SRobert Watson 	.pru_accept =		uipc_accept,
1366fa9402f2SRobert Watson 	.pru_attach =		uipc_attach,
1367fa9402f2SRobert Watson 	.pru_bind =		uipc_bind,
13687493f24eSPawel Jakub Dawidek 	.pru_bindat =		uipc_bindat,
1369fa9402f2SRobert Watson 	.pru_connect =		uipc_connect,
13707493f24eSPawel Jakub Dawidek 	.pru_connectat =	uipc_connectat,
1371fa9402f2SRobert Watson 	.pru_connect2 =		uipc_connect2,
1372fa9402f2SRobert Watson 	.pru_detach =		uipc_detach,
1373fa9402f2SRobert Watson 	.pru_disconnect =	uipc_disconnect,
1374fa9402f2SRobert Watson 	.pru_listen =		uipc_listen,
1375fa9402f2SRobert Watson 	.pru_peeraddr =		uipc_peeraddr,
1376fa9402f2SRobert Watson 	.pru_rcvd =		uipc_rcvd,
1377fa9402f2SRobert Watson 	.pru_send =		uipc_send,
1378c80ea19bSGleb Smirnoff 	.pru_ready =		uipc_ready,
1379fa9402f2SRobert Watson 	.pru_sense =		uipc_sense,
1380fa9402f2SRobert Watson 	.pru_shutdown =		uipc_shutdown,
1381fa9402f2SRobert Watson 	.pru_sockaddr =		uipc_sockaddr,
1382fa9402f2SRobert Watson 	.pru_soreceive =	soreceive_generic,
1383a152f8a3SRobert Watson 	.pru_close =		uipc_close,
1384a29f300eSGarrett Wollman };
1385df8bae1dSRodney W. Grimes 
13860b36cd25SRobert Watson static int
1387892af6b9SRobert Watson uipc_ctloutput(struct socket *so, struct sockopt *sopt)
13880c1bb4fbSDima Dorfman {
138940f2ac28SRobert Watson 	struct unpcb *unp;
13900d9ce3a1SRobert Watson 	struct xucred xu;
13916a2989fdSMatthew N. Dodd 	int error, optval;
13926a2989fdSMatthew N. Dodd 
139396a041b5SMatthew N. Dodd 	if (sopt->sopt_level != 0)
139496a041b5SMatthew N. Dodd 		return (EINVAL);
139596a041b5SMatthew N. Dodd 
13966a2989fdSMatthew N. Dodd 	unp = sotounpcb(so);
13974d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL"));
13986a2989fdSMatthew N. Dodd 	error = 0;
13990c1bb4fbSDima Dorfman 	switch (sopt->sopt_dir) {
14000c1bb4fbSDima Dorfman 	case SOPT_GET:
14010c1bb4fbSDima Dorfman 		switch (sopt->sopt_name) {
14020c1bb4fbSDima Dorfman 		case LOCAL_PEERCRED:
1403e7c33e29SRobert Watson 			UNP_PCB_LOCK(unp);
14040c1bb4fbSDima Dorfman 			if (unp->unp_flags & UNP_HAVEPC)
14050d9ce3a1SRobert Watson 				xu = unp->unp_peercred;
14060c1bb4fbSDima Dorfman 			else {
14070c1bb4fbSDima Dorfman 				if (so->so_type == SOCK_STREAM)
14080c1bb4fbSDima Dorfman 					error = ENOTCONN;
14090c1bb4fbSDima Dorfman 				else
14100c1bb4fbSDima Dorfman 					error = EINVAL;
14110c1bb4fbSDima Dorfman 			}
1412e7c33e29SRobert Watson 			UNP_PCB_UNLOCK(unp);
14130d9ce3a1SRobert Watson 			if (error == 0)
14140d9ce3a1SRobert Watson 				error = sooptcopyout(sopt, &xu, sizeof(xu));
14150c1bb4fbSDima Dorfman 			break;
1416e7c33e29SRobert Watson 
14176a2989fdSMatthew N. Dodd 		case LOCAL_CREDS:
1418a6357845SRobert Watson 			/* Unlocked read. */
14196a2989fdSMatthew N. Dodd 			optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0;
14206a2989fdSMatthew N. Dodd 			error = sooptcopyout(sopt, &optval, sizeof(optval));
14216a2989fdSMatthew N. Dodd 			break;
1422e7c33e29SRobert Watson 
14236a2989fdSMatthew N. Dodd 		case LOCAL_CONNWAIT:
1424a6357845SRobert Watson 			/* Unlocked read. */
14256a2989fdSMatthew N. Dodd 			optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0;
14266a2989fdSMatthew N. Dodd 			error = sooptcopyout(sopt, &optval, sizeof(optval));
14276a2989fdSMatthew N. Dodd 			break;
1428e7c33e29SRobert Watson 
14290c1bb4fbSDima Dorfman 		default:
14300c1bb4fbSDima Dorfman 			error = EOPNOTSUPP;
14310c1bb4fbSDima Dorfman 			break;
14320c1bb4fbSDima Dorfman 		}
14330c1bb4fbSDima Dorfman 		break;
1434e7c33e29SRobert Watson 
14350c1bb4fbSDima Dorfman 	case SOPT_SET:
14366a2989fdSMatthew N. Dodd 		switch (sopt->sopt_name) {
14376a2989fdSMatthew N. Dodd 		case LOCAL_CREDS:
14386a2989fdSMatthew N. Dodd 		case LOCAL_CONNWAIT:
14396a2989fdSMatthew N. Dodd 			error = sooptcopyin(sopt, &optval, sizeof(optval),
14406a2989fdSMatthew N. Dodd 					    sizeof(optval));
14416a2989fdSMatthew N. Dodd 			if (error)
14426a2989fdSMatthew N. Dodd 				break;
14436a2989fdSMatthew N. Dodd 
1444e7c33e29SRobert Watson #define	OPTSET(bit) do {						\
1445e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);						\
14466a2989fdSMatthew N. Dodd 	if (optval)							\
14476a2989fdSMatthew N. Dodd 		unp->unp_flags |= bit;					\
14486a2989fdSMatthew N. Dodd 	else								\
1449e7c33e29SRobert Watson 		unp->unp_flags &= ~bit;					\
1450e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);						\
1451e7c33e29SRobert Watson } while (0)
14526a2989fdSMatthew N. Dodd 
14536a2989fdSMatthew N. Dodd 			switch (sopt->sopt_name) {
14546a2989fdSMatthew N. Dodd 			case LOCAL_CREDS:
14556a2989fdSMatthew N. Dodd 				OPTSET(UNP_WANTCRED);
14566a2989fdSMatthew N. Dodd 				break;
1457e7c33e29SRobert Watson 
14586a2989fdSMatthew N. Dodd 			case LOCAL_CONNWAIT:
14596a2989fdSMatthew N. Dodd 				OPTSET(UNP_CONNWAIT);
14606a2989fdSMatthew N. Dodd 				break;
1461e7c33e29SRobert Watson 
14626a2989fdSMatthew N. Dodd 			default:
14636a2989fdSMatthew N. Dodd 				break;
14646a2989fdSMatthew N. Dodd 			}
14656a2989fdSMatthew N. Dodd 			break;
14666a2989fdSMatthew N. Dodd #undef	OPTSET
14676a2989fdSMatthew N. Dodd 		default:
14686a2989fdSMatthew N. Dodd 			error = ENOPROTOOPT;
14696a2989fdSMatthew N. Dodd 			break;
14706a2989fdSMatthew N. Dodd 		}
1471abb886faSMatthew N. Dodd 		break;
1472e7c33e29SRobert Watson 
14730c1bb4fbSDima Dorfman 	default:
14740c1bb4fbSDima Dorfman 		error = EOPNOTSUPP;
14750c1bb4fbSDima Dorfman 		break;
14760c1bb4fbSDima Dorfman 	}
14770c1bb4fbSDima Dorfman 	return (error);
14780c1bb4fbSDima Dorfman }
14790c1bb4fbSDima Dorfman 
1480f708ef1bSPoul-Henning Kamp static int
1481892af6b9SRobert Watson unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1482df8bae1dSRodney W. Grimes {
14837493f24eSPawel Jakub Dawidek 
14847493f24eSPawel Jakub Dawidek 	return (unp_connectat(AT_FDCWD, so, nam, td));
14857493f24eSPawel Jakub Dawidek }
14867493f24eSPawel Jakub Dawidek 
14877493f24eSPawel Jakub Dawidek static int
14887493f24eSPawel Jakub Dawidek unp_connectat(int fd, struct socket *so, struct sockaddr *nam,
14897493f24eSPawel Jakub Dawidek     struct thread *td)
14907493f24eSPawel Jakub Dawidek {
1491892af6b9SRobert Watson 	struct sockaddr_un *soun = (struct sockaddr_un *)nam;
1492892af6b9SRobert Watson 	struct vnode *vp;
1493779f106aSGleb Smirnoff 	struct socket *so2;
1494b295bdcdSRobert Watson 	struct unpcb *unp, *unp2, *unp3;
1495df8bae1dSRodney W. Grimes 	struct nameidata nd;
149657bf258eSGarrett Wollman 	char buf[SOCK_MAXADDRLEN];
14970d9ce3a1SRobert Watson 	struct sockaddr *sa;
14987008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
149975a67bf3SMatt Macy 	int error, len, freed;
150075a67bf3SMatt Macy 	struct mtx *vplock;
15010d9ce3a1SRobert Watson 
1502cb7df69bSKevin Lo 	if (nam->sa_family != AF_UNIX)
1503cb7df69bSKevin Lo 		return (EAFNOSUPPORT);
1504a06534c3SBjoern A. Zeeb 	if (nam->sa_len > sizeof(struct sockaddr_un))
1505a06534c3SBjoern A. Zeeb 		return (EINVAL);
150657bf258eSGarrett Wollman 	len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
150757bf258eSGarrett Wollman 	if (len <= 0)
1508e5aeaa0cSDag-Erling Smørgrav 		return (EINVAL);
15097928893dSEd Maste 	bcopy(soun->sun_path, buf, len);
15107928893dSEd Maste 	buf[len] = 0;
1511e7c33e29SRobert Watson 
151275a67bf3SMatt Macy 	unp = sotounpcb(so);
1513e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
15144f1f0ef5SRobert Watson 	if (unp->unp_flags & UNP_CONNECTING) {
1515e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
15164f1f0ef5SRobert Watson 		return (EALREADY);
15174f1f0ef5SRobert Watson 	}
151805102f04SRobert Watson 	unp->unp_flags |= UNP_CONNECTING;
1519e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
1520e7c33e29SRobert Watson 
15210d9ce3a1SRobert Watson 	sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
15227493f24eSPawel Jakub Dawidek 	NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
15237008be5bSPawel Jakub Dawidek 	    UIO_SYSSPACE, buf, fd, cap_rights_init(&rights, CAP_CONNECTAT), td);
1524797f2d22SPoul-Henning Kamp 	error = namei(&nd);
1525797f2d22SPoul-Henning Kamp 	if (error)
15260d9ce3a1SRobert Watson 		vp = NULL;
15270d9ce3a1SRobert Watson 	else
1528df8bae1dSRodney W. Grimes 		vp = nd.ni_vp;
15290d9ce3a1SRobert Watson 	ASSERT_VOP_LOCKED(vp, "unp_connect");
1530762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
15310d9ce3a1SRobert Watson 	if (error)
15320d9ce3a1SRobert Watson 		goto bad;
15330d9ce3a1SRobert Watson 
1534df8bae1dSRodney W. Grimes 	if (vp->v_type != VSOCK) {
1535df8bae1dSRodney W. Grimes 		error = ENOTSOCK;
1536df8bae1dSRodney W. Grimes 		goto bad;
1537df8bae1dSRodney W. Grimes 	}
15386fac927cSRobert Watson #ifdef MAC
153930d239bcSRobert Watson 	error = mac_vnode_check_open(td->td_ucred, vp, VWRITE | VREAD);
15406fac927cSRobert Watson 	if (error)
15416fac927cSRobert Watson 		goto bad;
15426fac927cSRobert Watson #endif
1543a854ed98SJohn Baldwin 	error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td);
1544797f2d22SPoul-Henning Kamp 	if (error)
1545df8bae1dSRodney W. Grimes 		goto bad;
1546e7c33e29SRobert Watson 
1547b295bdcdSRobert Watson 	unp = sotounpcb(so);
15484d4b555eSRobert Watson 	KASSERT(unp != NULL, ("unp_connect: unp == NULL"));
1549e7c33e29SRobert Watson 
155075a67bf3SMatt Macy 	vplock = mtx_pool_find(mtxpool_sleep, vp);
155175a67bf3SMatt Macy 	mtx_lock(vplock);
15520c3c207fSGleb Smirnoff 	VOP_UNP_CONNECT(vp, &unp2);
15530c3c207fSGleb Smirnoff 	if (unp2 == NULL) {
1554df8bae1dSRodney W. Grimes 		error = ECONNREFUSED;
15552260c03dSRobert Watson 		goto bad2;
1556df8bae1dSRodney W. Grimes 	}
15570c3c207fSGleb Smirnoff 	so2 = unp2->unp_socket;
1558df8bae1dSRodney W. Grimes 	if (so->so_type != so2->so_type) {
1559df8bae1dSRodney W. Grimes 		error = EPROTOTYPE;
15602260c03dSRobert Watson 		goto bad2;
1561df8bae1dSRodney W. Grimes 	}
1562df8bae1dSRodney W. Grimes 	if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
1563e7c33e29SRobert Watson 		if (so2->so_options & SO_ACCEPTCONN) {
15641fb51a12SBjoern A. Zeeb 			CURVNET_SET(so2->so_vnet);
1565779f106aSGleb Smirnoff 			so2 = sonewconn(so2, 0);
15661fb51a12SBjoern A. Zeeb 			CURVNET_RESTORE();
1567e7c33e29SRobert Watson 		} else
1568779f106aSGleb Smirnoff 			so2 = NULL;
1569779f106aSGleb Smirnoff 		if (so2 == NULL) {
1570df8bae1dSRodney W. Grimes 			error = ECONNREFUSED;
1571*cb8f450bSMatt Macy 			goto bad2;
1572df8bae1dSRodney W. Grimes 		}
1573779f106aSGleb Smirnoff 		unp3 = sotounpcb(so2);
1574e10ef65dSMatt Macy 		unp_pcb_lock2(unp2, unp3);
15750d9ce3a1SRobert Watson 		if (unp2->unp_addr != NULL) {
15760d9ce3a1SRobert Watson 			bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len);
15770d9ce3a1SRobert Watson 			unp3->unp_addr = (struct sockaddr_un *) sa;
15780d9ce3a1SRobert Watson 			sa = NULL;
15790d9ce3a1SRobert Watson 		}
1580b523ec24SRobert Watson 
15810c1bb4fbSDima Dorfman 		/*
15827a2b450fSEitan Adler 		 * The connector's (client's) credentials are copied from its
15831c381b19SRobert Watson 		 * process structure at the time of connect() (which is now).
15840c1bb4fbSDima Dorfman 		 */
1585a854ed98SJohn Baldwin 		cru2x(td->td_ucred, &unp3->unp_peercred);
15860c1bb4fbSDima Dorfman 		unp3->unp_flags |= UNP_HAVEPC;
1587b523ec24SRobert Watson 
15880c1bb4fbSDima Dorfman 		/*
15891c381b19SRobert Watson 		 * The receiver's (server's) credentials are copied from the
15901c381b19SRobert Watson 		 * unp_peercred member of socket on which the former called
1591e7c33e29SRobert Watson 		 * listen(); uipc_listen() cached that process's credentials
15921c381b19SRobert Watson 		 * at that time so we can use them now.
15930c1bb4fbSDima Dorfman 		 */
15940c1bb4fbSDima Dorfman 		memcpy(&unp->unp_peercred, &unp2->unp_peercred,
15950c1bb4fbSDima Dorfman 		    sizeof(unp->unp_peercred));
15960c1bb4fbSDima Dorfman 		unp->unp_flags |= UNP_HAVEPC;
1597481f8fe8SMaxim Konovalov 		if (unp2->unp_flags & UNP_WANTCRED)
1598481f8fe8SMaxim Konovalov 			unp3->unp_flags |= UNP_WANTCRED;
1599e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp2);
1600779f106aSGleb Smirnoff 		unp2 = unp3;
160175a67bf3SMatt Macy 		unp_pcb_owned_lock2(unp2, unp, freed);
1602e10ef65dSMatt Macy 		if (__predict_false(freed)) {
1603e10ef65dSMatt Macy 			UNP_PCB_UNLOCK(unp2);
1604e10ef65dSMatt Macy 			error = ECONNREFUSED;
1605e10ef65dSMatt Macy 			goto bad2;
1606e10ef65dSMatt Macy 		}
1607335654d7SRobert Watson #ifdef MAC
1608779f106aSGleb Smirnoff 		mac_socketpeer_set_from_socket(so, so2);
1609779f106aSGleb Smirnoff 		mac_socketpeer_set_from_socket(so2, so);
1610335654d7SRobert Watson #endif
1611e10ef65dSMatt Macy 	} else
1612e10ef65dSMatt Macy 		unp_pcb_lock2(unp, unp2);
1613779f106aSGleb Smirnoff 
1614779f106aSGleb Smirnoff 	KASSERT(unp2 != NULL && so2 != NULL && unp2->unp_socket == so2 &&
1615779f106aSGleb Smirnoff 	    sotounpcb(so2) == unp2,
1616779f106aSGleb Smirnoff 	    ("%s: unp2 %p so2 %p", __func__, unp2, so2));
16176a2989fdSMatthew N. Dodd 	error = unp_connect2(so, so2, PRU_CONNECT);
1618779f106aSGleb Smirnoff bad3:
1619e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp2);
1620e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
16210d9ce3a1SRobert Watson bad2:
162275a67bf3SMatt Macy 	mtx_unlock(vplock);
1623df8bae1dSRodney W. Grimes bad:
162475a67bf3SMatt Macy 	if (vp != NULL) {
1625df8bae1dSRodney W. Grimes 		vput(vp);
162675a67bf3SMatt Macy 	}
16270d9ce3a1SRobert Watson 	free(sa, M_SONAME);
1628e7c33e29SRobert Watson 	UNP_PCB_LOCK(unp);
16294f1f0ef5SRobert Watson 	unp->unp_flags &= ~UNP_CONNECTING;
1630e7c33e29SRobert Watson 	UNP_PCB_UNLOCK(unp);
1631df8bae1dSRodney W. Grimes 	return (error);
1632df8bae1dSRodney W. Grimes }
1633df8bae1dSRodney W. Grimes 
1634db48c0d2SRobert Watson static int
16356a2989fdSMatthew N. Dodd unp_connect2(struct socket *so, struct socket *so2, int req)
1636df8bae1dSRodney W. Grimes {
1637e7c33e29SRobert Watson 	struct unpcb *unp;
1638892af6b9SRobert Watson 	struct unpcb *unp2;
1639df8bae1dSRodney W. Grimes 
1640e7c33e29SRobert Watson 	unp = sotounpcb(so);
1641e7c33e29SRobert Watson 	KASSERT(unp != NULL, ("unp_connect2: unp == NULL"));
1642e7c33e29SRobert Watson 	unp2 = sotounpcb(so2);
1643e7c33e29SRobert Watson 	KASSERT(unp2 != NULL, ("unp_connect2: unp2 == NULL"));
1644e7c33e29SRobert Watson 
1645e7c33e29SRobert Watson 	UNP_PCB_LOCK_ASSERT(unp);
1646e7c33e29SRobert Watson 	UNP_PCB_LOCK_ASSERT(unp2);
16470d9ce3a1SRobert Watson 
1648df8bae1dSRodney W. Grimes 	if (so2->so_type != so->so_type)
1649df8bae1dSRodney W. Grimes 		return (EPROTOTYPE);
1650434ac8b6SMark Johnston 	unp2->unp_flags &= ~UNP_NASCENT;
1651df8bae1dSRodney W. Grimes 	unp->unp_conn = unp2;
165275a67bf3SMatt Macy 	unp_pcb_hold(unp2);
165375a67bf3SMatt Macy 	unp_pcb_hold(unp);
1654df8bae1dSRodney W. Grimes 	switch (so->so_type) {
1655df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
165675a67bf3SMatt Macy 		UNP_REF_LIST_LOCK();
165798271db4SGarrett Wollman 		LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
165875a67bf3SMatt Macy 		UNP_REF_LIST_UNLOCK();
1659df8bae1dSRodney W. Grimes 		soisconnected(so);
1660df8bae1dSRodney W. Grimes 		break;
1661df8bae1dSRodney W. Grimes 
1662df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
166384d61770SRobert Watson 	case SOCK_SEQPACKET:
1664df8bae1dSRodney W. Grimes 		unp2->unp_conn = unp;
16656a2989fdSMatthew N. Dodd 		if (req == PRU_CONNECT &&
16666a2989fdSMatthew N. Dodd 		    ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT))
16676a2989fdSMatthew N. Dodd 			soisconnecting(so);
16686a2989fdSMatthew N. Dodd 		else
1669df8bae1dSRodney W. Grimes 			soisconnected(so);
1670df8bae1dSRodney W. Grimes 		soisconnected(so2);
1671df8bae1dSRodney W. Grimes 		break;
1672df8bae1dSRodney W. Grimes 
1673df8bae1dSRodney W. Grimes 	default:
1674df8bae1dSRodney W. Grimes 		panic("unp_connect2");
1675df8bae1dSRodney W. Grimes 	}
1676df8bae1dSRodney W. Grimes 	return (0);
1677df8bae1dSRodney W. Grimes }
1678df8bae1dSRodney W. Grimes 
1679f708ef1bSPoul-Henning Kamp static void
1680e7c33e29SRobert Watson unp_disconnect(struct unpcb *unp, struct unpcb *unp2)
1681df8bae1dSRodney W. Grimes {
168275a67bf3SMatt Macy 	struct socket *so, *so2;
16834b06dee1SMatt Macy 	int freed __unused;
1684df8bae1dSRodney W. Grimes 
1685e7c33e29SRobert Watson 	KASSERT(unp2 != NULL, ("unp_disconnect: unp2 == NULL"));
16860d9ce3a1SRobert Watson 
1687e7c33e29SRobert Watson 	UNP_PCB_LOCK_ASSERT(unp);
1688e7c33e29SRobert Watson 	UNP_PCB_LOCK_ASSERT(unp2);
1689e7c33e29SRobert Watson 
169075a67bf3SMatt Macy 	if (unp->unp_conn == NULL && unp2->unp_conn == NULL)
169175a67bf3SMatt Macy 		return;
169275a67bf3SMatt Macy 
169375a67bf3SMatt Macy 	MPASS(unp->unp_conn == unp2);
1694fc3fcacfSRobert Watson 	unp->unp_conn = NULL;
169575a67bf3SMatt Macy 	so = unp->unp_socket;
169675a67bf3SMatt Macy 	so2 = unp2->unp_socket;
1697df8bae1dSRodney W. Grimes 	switch (unp->unp_socket->so_type) {
1698df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
169975a67bf3SMatt Macy 		UNP_REF_LIST_LOCK();
170098271db4SGarrett Wollman 		LIST_REMOVE(unp, unp_reflink);
170175a67bf3SMatt Macy 		UNP_REF_LIST_UNLOCK();
170275a67bf3SMatt Macy 		if (so) {
17031b2e3b4bSRobert Watson 			SOCK_LOCK(so);
17041b2e3b4bSRobert Watson 			so->so_state &= ~SS_ISCONNECTED;
17051b2e3b4bSRobert Watson 			SOCK_UNLOCK(so);
170675a67bf3SMatt Macy 		}
1707df8bae1dSRodney W. Grimes 		break;
1708df8bae1dSRodney W. Grimes 
1709df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
171084d61770SRobert Watson 	case SOCK_SEQPACKET:
171175a67bf3SMatt Macy 		if (so)
171275a67bf3SMatt Macy 			soisdisconnected(so);
171375a67bf3SMatt Macy 		MPASS(unp2->unp_conn == unp);
1714fc3fcacfSRobert Watson 		unp2->unp_conn = NULL;
171575a67bf3SMatt Macy 		if (so2)
171675a67bf3SMatt Macy 			soisdisconnected(so2);
1717df8bae1dSRodney W. Grimes 		break;
1718df8bae1dSRodney W. Grimes 	}
17194b06dee1SMatt Macy 	freed = unp_pcb_rele(unp);
172075a67bf3SMatt Macy 	MPASS(freed == 0);
17214b06dee1SMatt Macy 	freed = unp_pcb_rele(unp2);
172275a67bf3SMatt Macy 	MPASS(freed == 0);
1723df8bae1dSRodney W. Grimes }
1724df8bae1dSRodney W. Grimes 
17250d9ce3a1SRobert Watson /*
1726d7924b70SRobert Watson  * unp_pcblist() walks the global list of struct unpcb's to generate a
1727d7924b70SRobert Watson  * pointer list, bumping the refcount on each unpcb.  It then copies them out
1728d7924b70SRobert Watson  * sequentially, validating the generation number on each to see if it has
1729d7924b70SRobert Watson  * been detached.  All of this is necessary because copyout() may sleep on
1730d7924b70SRobert Watson  * disk I/O.
17310d9ce3a1SRobert Watson  */
173298271db4SGarrett Wollman static int
173382d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS)
173498271db4SGarrett Wollman {
173598271db4SGarrett Wollman 	struct unpcb *unp, **unp_list;
173698271db4SGarrett Wollman 	unp_gen_t gencnt;
17378f364875SJulian Elischer 	struct xunpgen *xug;
173898271db4SGarrett Wollman 	struct unp_head *head;
17398f364875SJulian Elischer 	struct xunpcb *xu;
1740d821d364SPedro F. Giffuni 	u_int i;
174144c514b1SPedro F. Giffuni 	int error, freeunp, n;
174298271db4SGarrett Wollman 
174384d61770SRobert Watson 	switch ((intptr_t)arg1) {
174484d61770SRobert Watson 	case SOCK_STREAM:
174584d61770SRobert Watson 		head = &unp_shead;
174684d61770SRobert Watson 		break;
174784d61770SRobert Watson 
174884d61770SRobert Watson 	case SOCK_DGRAM:
174984d61770SRobert Watson 		head = &unp_dhead;
175084d61770SRobert Watson 		break;
175184d61770SRobert Watson 
175284d61770SRobert Watson 	case SOCK_SEQPACKET:
175384d61770SRobert Watson 		head = &unp_sphead;
175484d61770SRobert Watson 		break;
175584d61770SRobert Watson 
175684d61770SRobert Watson 	default:
1757604f19c9SRobert Watson 		panic("unp_pcblist: arg1 %d", (int)(intptr_t)arg1);
175884d61770SRobert Watson 	}
175998271db4SGarrett Wollman 
176098271db4SGarrett Wollman 	/*
176198271db4SGarrett Wollman 	 * The process of preparing the PCB list is too time-consuming and
176298271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
176398271db4SGarrett Wollman 	 */
1764fc3fcacfSRobert Watson 	if (req->oldptr == NULL) {
176598271db4SGarrett Wollman 		n = unp_count;
17668f364875SJulian Elischer 		req->oldidx = 2 * (sizeof *xug)
176798271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xunpcb);
1768e5aeaa0cSDag-Erling Smørgrav 		return (0);
176998271db4SGarrett Wollman 	}
177098271db4SGarrett Wollman 
1771fc3fcacfSRobert Watson 	if (req->newptr != NULL)
1772e5aeaa0cSDag-Erling Smørgrav 		return (EPERM);
177398271db4SGarrett Wollman 
177498271db4SGarrett Wollman 	/*
177598271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
177698271db4SGarrett Wollman 	 */
1777a163d034SWarner Losh 	xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK);
1778779f106aSGleb Smirnoff 	UNP_LINK_RLOCK();
177998271db4SGarrett Wollman 	gencnt = unp_gencnt;
178098271db4SGarrett Wollman 	n = unp_count;
1781779f106aSGleb Smirnoff 	UNP_LINK_RUNLOCK();
178298271db4SGarrett Wollman 
17838f364875SJulian Elischer 	xug->xug_len = sizeof *xug;
17848f364875SJulian Elischer 	xug->xug_count = n;
17858f364875SJulian Elischer 	xug->xug_gen = gencnt;
17868f364875SJulian Elischer 	xug->xug_sogen = so_gencnt;
17878f364875SJulian Elischer 	error = SYSCTL_OUT(req, xug, sizeof *xug);
17888f364875SJulian Elischer 	if (error) {
17898f364875SJulian Elischer 		free(xug, M_TEMP);
1790e5aeaa0cSDag-Erling Smørgrav 		return (error);
17918f364875SJulian Elischer 	}
179298271db4SGarrett Wollman 
1793a163d034SWarner Losh 	unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
179498271db4SGarrett Wollman 
1795779f106aSGleb Smirnoff 	UNP_LINK_RLOCK();
17962e3c8fcbSPoul-Henning Kamp 	for (unp = LIST_FIRST(head), i = 0; unp && i < n;
17972e3c8fcbSPoul-Henning Kamp 	     unp = LIST_NEXT(unp, unp_link)) {
1798e7c33e29SRobert Watson 		UNP_PCB_LOCK(unp);
17998a7d8cc6SRobert Watson 		if (unp->unp_gencnt <= gencnt) {
1800a854ed98SJohn Baldwin 			if (cr_cansee(req->td->td_ucred,
1801e7c33e29SRobert Watson 			    unp->unp_socket->so_cred)) {
1802e7c33e29SRobert Watson 				UNP_PCB_UNLOCK(unp);
18034787fd37SPaul Saab 				continue;
1804e7c33e29SRobert Watson 			}
180598271db4SGarrett Wollman 			unp_list[i++] = unp;
180675a67bf3SMatt Macy 			unp_pcb_hold(unp);
180798271db4SGarrett Wollman 		}
1808e7c33e29SRobert Watson 		UNP_PCB_UNLOCK(unp);
18094787fd37SPaul Saab 	}
1810779f106aSGleb Smirnoff 	UNP_LINK_RUNLOCK();
18111c381b19SRobert Watson 	n = i;			/* In case we lost some during malloc. */
181298271db4SGarrett Wollman 
181398271db4SGarrett Wollman 	error = 0;
1814fe2eee82SColin Percival 	xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO);
181598271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
181698271db4SGarrett Wollman 		unp = unp_list[i];
1817e7c33e29SRobert Watson 		UNP_PCB_LOCK(unp);
181875a67bf3SMatt Macy 		freeunp = unp_pcb_rele(unp);
181975a67bf3SMatt Macy 
182075a67bf3SMatt Macy 		if (freeunp == 0 && unp->unp_gencnt <= gencnt) {
18218f364875SJulian Elischer 			xu->xu_len = sizeof *xu;
18228f364875SJulian Elischer 			xu->xu_unpp = unp;
182398271db4SGarrett Wollman 			/*
182498271db4SGarrett Wollman 			 * XXX - need more locking here to protect against
182598271db4SGarrett Wollman 			 * connect/disconnect races for SMP.
182698271db4SGarrett Wollman 			 */
1827fc3fcacfSRobert Watson 			if (unp->unp_addr != NULL)
18288f364875SJulian Elischer 				bcopy(unp->unp_addr, &xu->xu_addr,
182998271db4SGarrett Wollman 				      unp->unp_addr->sun_len);
18300e229f34SGleb Smirnoff 			else
18310e229f34SGleb Smirnoff 				bzero(&xu->xu_addr, sizeof(xu->xu_addr));
1832fc3fcacfSRobert Watson 			if (unp->unp_conn != NULL &&
1833fc3fcacfSRobert Watson 			    unp->unp_conn->unp_addr != NULL)
183498271db4SGarrett Wollman 				bcopy(unp->unp_conn->unp_addr,
18358f364875SJulian Elischer 				      &xu->xu_caddr,
183698271db4SGarrett Wollman 				      unp->unp_conn->unp_addr->sun_len);
18370e229f34SGleb Smirnoff 			else
18380e229f34SGleb Smirnoff 				bzero(&xu->xu_caddr, sizeof(xu->xu_caddr));
18390e229f34SGleb Smirnoff 			xu->unp_vnode = unp->unp_vnode;
18400e229f34SGleb Smirnoff 			xu->unp_conn = unp->unp_conn;
18410e229f34SGleb Smirnoff 			xu->xu_firstref = LIST_FIRST(&unp->unp_refs);
18420e229f34SGleb Smirnoff 			xu->xu_nextref = LIST_NEXT(unp, unp_reflink);
18430e229f34SGleb Smirnoff 			xu->unp_gencnt = unp->unp_gencnt;
18448f364875SJulian Elischer 			sotoxsocket(unp->unp_socket, &xu->xu_socket);
1845e7c33e29SRobert Watson 			UNP_PCB_UNLOCK(unp);
18468f364875SJulian Elischer 			error = SYSCTL_OUT(req, xu, sizeof *xu);
184775a67bf3SMatt Macy 		} else  if (freeunp == 0)
1848e7c33e29SRobert Watson 			UNP_PCB_UNLOCK(unp);
1849e7c33e29SRobert Watson 	}
18508f364875SJulian Elischer 	free(xu, M_TEMP);
185198271db4SGarrett Wollman 	if (!error) {
185298271db4SGarrett Wollman 		/*
18531c381b19SRobert Watson 		 * Give the user an updated idea of our state.  If the
18541c381b19SRobert Watson 		 * generation differs from what we told her before, she knows
18551c381b19SRobert Watson 		 * that something happened while we were processing this
18561c381b19SRobert Watson 		 * request, and it might be necessary to retry.
185798271db4SGarrett Wollman 		 */
18588f364875SJulian Elischer 		xug->xug_gen = unp_gencnt;
18598f364875SJulian Elischer 		xug->xug_sogen = so_gencnt;
18608f364875SJulian Elischer 		xug->xug_count = unp_count;
18618f364875SJulian Elischer 		error = SYSCTL_OUT(req, xug, sizeof *xug);
186298271db4SGarrett Wollman 	}
186398271db4SGarrett Wollman 	free(unp_list, M_TEMP);
18648f364875SJulian Elischer 	free(xug, M_TEMP);
1865e5aeaa0cSDag-Erling Smørgrav 	return (error);
186698271db4SGarrett Wollman }
186798271db4SGarrett Wollman 
18682fee06f0SMatthew D Fleming SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLTYPE_OPAQUE | CTLFLAG_RD,
18692fee06f0SMatthew D Fleming     (void *)(intptr_t)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
187098271db4SGarrett Wollman     "List of active local datagram sockets");
18712fee06f0SMatthew D Fleming SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLTYPE_OPAQUE | CTLFLAG_RD,
18722fee06f0SMatthew D Fleming     (void *)(intptr_t)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
187398271db4SGarrett Wollman     "List of active local stream sockets");
18742fee06f0SMatthew D Fleming SYSCTL_PROC(_net_local_seqpacket, OID_AUTO, pcblist,
18752fee06f0SMatthew D Fleming     CTLTYPE_OPAQUE | CTLFLAG_RD,
18762fee06f0SMatthew D Fleming     (void *)(intptr_t)SOCK_SEQPACKET, 0, unp_pcblist, "S,xunpcb",
187784d61770SRobert Watson     "List of active local seqpacket sockets");
187898271db4SGarrett Wollman 
1879f708ef1bSPoul-Henning Kamp static void
1880892af6b9SRobert Watson unp_shutdown(struct unpcb *unp)
1881df8bae1dSRodney W. Grimes {
1882e7c33e29SRobert Watson 	struct unpcb *unp2;
1883df8bae1dSRodney W. Grimes 	struct socket *so;
1884df8bae1dSRodney W. Grimes 
1885e7c33e29SRobert Watson 	UNP_PCB_LOCK_ASSERT(unp);
18860d9ce3a1SRobert Watson 
1887e7c33e29SRobert Watson 	unp2 = unp->unp_conn;
188884d61770SRobert Watson 	if ((unp->unp_socket->so_type == SOCK_STREAM ||
188984d61770SRobert Watson 	    (unp->unp_socket->so_type == SOCK_SEQPACKET)) && unp2 != NULL) {
1890e7c33e29SRobert Watson 		so = unp2->unp_socket;
1891e7c33e29SRobert Watson 		if (so != NULL)
1892df8bae1dSRodney W. Grimes 			socantrcvmore(so);
1893df8bae1dSRodney W. Grimes 	}
1894e7c33e29SRobert Watson }
1895df8bae1dSRodney W. Grimes 
1896f708ef1bSPoul-Henning Kamp static void
1897afc055d9SEd Schouten unp_drop(struct unpcb *unp)
1898df8bae1dSRodney W. Grimes {
1899df8bae1dSRodney W. Grimes 	struct socket *so = unp->unp_socket;
1900e7c33e29SRobert Watson 	struct unpcb *unp2;
190175a67bf3SMatt Macy 	int freed;
19020d9ce3a1SRobert Watson 
1903afc055d9SEd Schouten 	/*
1904afc055d9SEd Schouten 	 * Regardless of whether the socket's peer dropped the connection
1905afc055d9SEd Schouten 	 * with this socket by aborting or disconnecting, POSIX requires
1906afc055d9SEd Schouten 	 * that ECONNRESET is returned.
1907afc055d9SEd Schouten 	 */
190875a67bf3SMatt Macy 	/* acquire a reference so that unp isn't freed from underneath us */
190975a67bf3SMatt Macy 
191075a67bf3SMatt Macy 	UNP_PCB_LOCK(unp);
191175a67bf3SMatt Macy 	if (so)
1912afc055d9SEd Schouten 		so->so_error = ECONNRESET;
1913e7c33e29SRobert Watson 	unp2 = unp->unp_conn;
191475a67bf3SMatt Macy 	if (unp2 != NULL) {
191575a67bf3SMatt Macy 		unp_pcb_hold(unp2);
191675a67bf3SMatt Macy 		unp_pcb_owned_lock2(unp, unp2, freed);
1917e7c33e29SRobert Watson 		unp_disconnect(unp, unp2);
191875a67bf3SMatt Macy 		if (unp_pcb_rele(unp2) == 0)
1919e7c33e29SRobert Watson 			UNP_PCB_UNLOCK(unp2);
1920df8bae1dSRodney W. Grimes 	}
192175a67bf3SMatt Macy 	if (unp_pcb_rele(unp) == 0)
192275a67bf3SMatt Macy 		UNP_PCB_UNLOCK(unp);
192375a67bf3SMatt Macy }
1924df8bae1dSRodney W. Grimes 
19252bc21ed9SDavid Malone static void
19268cb539f1SPawel Jakub Dawidek unp_freerights(struct filedescent **fdep, int fdcount)
1927df8bae1dSRodney W. Grimes {
19282bc21ed9SDavid Malone 	struct file *fp;
19292609222aSPawel Jakub Dawidek 	int i;
1930df8bae1dSRodney W. Grimes 
193182e825c4SGleb Smirnoff 	KASSERT(fdcount > 0, ("%s: fdcount %d", __func__, fdcount));
193282e825c4SGleb Smirnoff 
19338cb539f1SPawel Jakub Dawidek 	for (i = 0; i < fdcount; i++) {
19348cb539f1SPawel Jakub Dawidek 		fp = fdep[i]->fde_file;
19358cb539f1SPawel Jakub Dawidek 		filecaps_free(&fdep[i]->fde_caps);
19368692c025SYoshinobu Inoue 		unp_discard(fp);
1937df8bae1dSRodney W. Grimes 	}
19388cb539f1SPawel Jakub Dawidek 	free(fdep[0], M_FILECAPS);
19392bc21ed9SDavid Malone }
19402bc21ed9SDavid Malone 
19410b36cd25SRobert Watson static int
1942c2e3c52eSJilles Tjoelker unp_externalize(struct mbuf *control, struct mbuf **controlp, int flags)
19432bc21ed9SDavid Malone {
19442bc21ed9SDavid Malone 	struct thread *td = curthread;		/* XXX */
19452bc21ed9SDavid Malone 	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
19462bc21ed9SDavid Malone 	int i;
19472bc21ed9SDavid Malone 	int *fdp;
19482609222aSPawel Jakub Dawidek 	struct filedesc *fdesc = td->td_proc->p_fd;
1949ea31808cSMateusz Guzik 	struct filedescent **fdep;
19502bc21ed9SDavid Malone 	void *data;
19512bc21ed9SDavid Malone 	socklen_t clen = control->m_len, datalen;
19522bc21ed9SDavid Malone 	int error, newfds;
19532bc21ed9SDavid Malone 	u_int newlen;
19542bc21ed9SDavid Malone 
19553dab55bcSRobert Watson 	UNP_LINK_UNLOCK_ASSERT();
19564c5bc1caSRobert Watson 
19572bc21ed9SDavid Malone 	error = 0;
19582bc21ed9SDavid Malone 	if (controlp != NULL) /* controlp == NULL => free control messages */
19592bc21ed9SDavid Malone 		*controlp = NULL;
19602bc21ed9SDavid Malone 	while (cm != NULL) {
19612bc21ed9SDavid Malone 		if (sizeof(*cm) > clen || cm->cmsg_len > clen) {
19622bc21ed9SDavid Malone 			error = EINVAL;
19632bc21ed9SDavid Malone 			break;
19642bc21ed9SDavid Malone 		}
19652bc21ed9SDavid Malone 		data = CMSG_DATA(cm);
19662bc21ed9SDavid Malone 		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
19672bc21ed9SDavid Malone 		if (cm->cmsg_level == SOL_SOCKET
19682bc21ed9SDavid Malone 		    && cm->cmsg_type == SCM_RIGHTS) {
19692609222aSPawel Jakub Dawidek 			newfds = datalen / sizeof(*fdep);
197082e825c4SGleb Smirnoff 			if (newfds == 0)
197182e825c4SGleb Smirnoff 				goto next;
19722609222aSPawel Jakub Dawidek 			fdep = data;
19732bc21ed9SDavid Malone 
1974e2f9a08bSOlivier Houchard 			/* If we're not outputting the descriptors free them. */
19752bc21ed9SDavid Malone 			if (error || controlp == NULL) {
19762609222aSPawel Jakub Dawidek 				unp_freerights(fdep, newfds);
19772bc21ed9SDavid Malone 				goto next;
19782bc21ed9SDavid Malone 			}
19792609222aSPawel Jakub Dawidek 			FILEDESC_XLOCK(fdesc);
198060a5ef26SRobert Watson 
1981ed5b7817SJulian Elischer 			/*
19821c381b19SRobert Watson 			 * Now change each pointer to an fd in the global
19831c381b19SRobert Watson 			 * table to an integer that is the index to the local
19841c381b19SRobert Watson 			 * fd table entry that we set up to point to the
19851c381b19SRobert Watson 			 * global one we are transferring.
1986ed5b7817SJulian Elischer 			 */
19872bc21ed9SDavid Malone 			newlen = newfds * sizeof(int);
19882bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, newlen,
19892bc21ed9SDavid Malone 			    SCM_RIGHTS, SOL_SOCKET);
19902bc21ed9SDavid Malone 			if (*controlp == NULL) {
19912609222aSPawel Jakub Dawidek 				FILEDESC_XUNLOCK(fdesc);
19922bc21ed9SDavid Malone 				error = E2BIG;
19932609222aSPawel Jakub Dawidek 				unp_freerights(fdep, newfds);
19942bc21ed9SDavid Malone 				goto next;
19952bc21ed9SDavid Malone 			}
19962bc21ed9SDavid Malone 
19972bc21ed9SDavid Malone 			fdp = (int *)
19982bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1999db8f33fdSMateusz Guzik 			if (fdallocn(td, 0, fdp, newfds) != 0) {
20003331a33aSMateusz Guzik 				FILEDESC_XUNLOCK(fdesc);
2001db8f33fdSMateusz Guzik 				error = EMSGSIZE;
2002db8f33fdSMateusz Guzik 				unp_freerights(fdep, newfds);
2003db8f33fdSMateusz Guzik 				m_freem(*controlp);
2004db8f33fdSMateusz Guzik 				*controlp = NULL;
2005db8f33fdSMateusz Guzik 				goto next;
2006db8f33fdSMateusz Guzik 			}
20078cb539f1SPawel Jakub Dawidek 			for (i = 0; i < newfds; i++, fdp++) {
2008ea31808cSMateusz Guzik 				_finstall(fdesc, fdep[i]->fde_file, *fdp,
2009ea31808cSMateusz Guzik 				    (flags & MSG_CMSG_CLOEXEC) != 0 ? UF_EXCLOSE : 0,
2010ea31808cSMateusz Guzik 				    &fdep[i]->fde_caps);
2011ea31808cSMateusz Guzik 				unp_externalize_fp(fdep[i]->fde_file);
2012df8bae1dSRodney W. Grimes 			}
20132609222aSPawel Jakub Dawidek 			FILEDESC_XUNLOCK(fdesc);
20148cb539f1SPawel Jakub Dawidek 			free(fdep[0], M_FILECAPS);
20151c381b19SRobert Watson 		} else {
20161c381b19SRobert Watson 			/* We can just copy anything else across. */
20172bc21ed9SDavid Malone 			if (error || controlp == NULL)
20182bc21ed9SDavid Malone 				goto next;
20192bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, datalen,
20202bc21ed9SDavid Malone 			    cm->cmsg_type, cm->cmsg_level);
20212bc21ed9SDavid Malone 			if (*controlp == NULL) {
20222bc21ed9SDavid Malone 				error = ENOBUFS;
20232bc21ed9SDavid Malone 				goto next;
20242bc21ed9SDavid Malone 			}
20252bc21ed9SDavid Malone 			bcopy(data,
20262bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *)),
20272bc21ed9SDavid Malone 			    datalen);
20282bc21ed9SDavid Malone 		}
20292bc21ed9SDavid Malone 		controlp = &(*controlp)->m_next;
20302bc21ed9SDavid Malone 
20312bc21ed9SDavid Malone next:
20322bc21ed9SDavid Malone 		if (CMSG_SPACE(datalen) < clen) {
20332bc21ed9SDavid Malone 			clen -= CMSG_SPACE(datalen);
20342bc21ed9SDavid Malone 			cm = (struct cmsghdr *)
20352bc21ed9SDavid Malone 			    ((caddr_t)cm + CMSG_SPACE(datalen));
20368692c025SYoshinobu Inoue 		} else {
20372bc21ed9SDavid Malone 			clen = 0;
20382bc21ed9SDavid Malone 			cm = NULL;
20398692c025SYoshinobu Inoue 		}
20408692c025SYoshinobu Inoue 	}
20418692c025SYoshinobu Inoue 
20422bc21ed9SDavid Malone 	m_freem(control);
20432bc21ed9SDavid Malone 	return (error);
2044df8bae1dSRodney W. Grimes }
2045df8bae1dSRodney W. Grimes 
20464f590175SPaul Saab static void
20474f590175SPaul Saab unp_zone_change(void *tag)
20484f590175SPaul Saab {
20494f590175SPaul Saab 
20504f590175SPaul Saab 	uma_zone_set_max(unp_zone, maxsockets);
20514f590175SPaul Saab }
20524f590175SPaul Saab 
20530b36cd25SRobert Watson static void
205498271db4SGarrett Wollman unp_init(void)
205598271db4SGarrett Wollman {
20561c381b19SRobert Watson 
205721ca7b57SMarko Zec #ifdef VIMAGE
205821ca7b57SMarko Zec 	if (!IS_DEFAULT_VNET(curvnet))
205921ca7b57SMarko Zec 		return;
206021ca7b57SMarko Zec #endif
20619e9d298aSJeff Roberson 	unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL,
206275a67bf3SMatt Macy 	    NULL, NULL, UMA_ALIGN_CACHE, 0);
2063fc3fcacfSRobert Watson 	if (unp_zone == NULL)
206498271db4SGarrett Wollman 		panic("unp_init");
20654f590175SPaul Saab 	uma_zone_set_max(unp_zone, maxsockets);
20666e0b6746SPawel Jakub Dawidek 	uma_zone_set_warning(unp_zone, "kern.ipc.maxsockets limit reached");
20674f590175SPaul Saab 	EVENTHANDLER_REGISTER(maxsockets_change, unp_zone_change,
20684f590175SPaul Saab 	    NULL, EVENTHANDLER_PRI_ANY);
206998271db4SGarrett Wollman 	LIST_INIT(&unp_dhead);
207098271db4SGarrett Wollman 	LIST_INIT(&unp_shead);
207184d61770SRobert Watson 	LIST_INIT(&unp_sphead);
20720cb64678SKonstantin Belousov 	SLIST_INIT(&unp_defers);
2073daee0f0bSKonstantin Belousov 	TIMEOUT_TASK_INIT(taskqueue_thread, &unp_gc_task, 0, unp_gc, NULL);
20740cb64678SKonstantin Belousov 	TASK_INIT(&unp_defer_task, 0, unp_process_defers, NULL);
20753dab55bcSRobert Watson 	UNP_LINK_LOCK_INIT();
20760cb64678SKonstantin Belousov 	UNP_DEFERRED_LOCK_INIT();
207798271db4SGarrett Wollman }
207898271db4SGarrett Wollman 
2079f708ef1bSPoul-Henning Kamp static int
2080892af6b9SRobert Watson unp_internalize(struct mbuf **controlp, struct thread *td)
2081df8bae1dSRodney W. Grimes {
20822bc21ed9SDavid Malone 	struct mbuf *control = *controlp;
2083b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
20842609222aSPawel Jakub Dawidek 	struct filedesc *fdesc = p->p_fd;
2085ab15d803SSergey Kandaurov 	struct bintime *bt;
20862bc21ed9SDavid Malone 	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
20872bc21ed9SDavid Malone 	struct cmsgcred *cmcred;
20888cb539f1SPawel Jakub Dawidek 	struct filedescent *fde, **fdep, *fdev;
20892bc21ed9SDavid Malone 	struct file *fp;
20902bc21ed9SDavid Malone 	struct timeval *tv;
2091339efd75SMaxim Sobolev 	struct timespec *ts;
20926a1cf96bSMateusz Guzik 	int i, *fdp;
20932bc21ed9SDavid Malone 	void *data;
20942bc21ed9SDavid Malone 	socklen_t clen = control->m_len, datalen;
20952bc21ed9SDavid Malone 	int error, oldfds;
20968692c025SYoshinobu Inoue 	u_int newlen;
2097df8bae1dSRodney W. Grimes 
20983dab55bcSRobert Watson 	UNP_LINK_UNLOCK_ASSERT();
20994c5bc1caSRobert Watson 
21002bc21ed9SDavid Malone 	error = 0;
21012bc21ed9SDavid Malone 	*controlp = NULL;
21022bc21ed9SDavid Malone 	while (cm != NULL) {
21032bc21ed9SDavid Malone 		if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET
2104de966666SMateusz Guzik 		    || cm->cmsg_len > clen || cm->cmsg_len < sizeof(*cm)) {
21052bc21ed9SDavid Malone 			error = EINVAL;
21062bc21ed9SDavid Malone 			goto out;
21072bc21ed9SDavid Malone 		}
21082bc21ed9SDavid Malone 		data = CMSG_DATA(cm);
21092bc21ed9SDavid Malone 		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
21102bc21ed9SDavid Malone 
21112bc21ed9SDavid Malone 		switch (cm->cmsg_type) {
21120b788fa1SBill Paul 		/*
21130b788fa1SBill Paul 		 * Fill in credential information.
21140b788fa1SBill Paul 		 */
21152bc21ed9SDavid Malone 		case SCM_CREDS:
21162bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, sizeof(*cmcred),
21172bc21ed9SDavid Malone 			    SCM_CREDS, SOL_SOCKET);
21182bc21ed9SDavid Malone 			if (*controlp == NULL) {
21192bc21ed9SDavid Malone 				error = ENOBUFS;
21202bc21ed9SDavid Malone 				goto out;
21212bc21ed9SDavid Malone 			}
21222bc21ed9SDavid Malone 			cmcred = (struct cmsgcred *)
21232bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
21240b788fa1SBill Paul 			cmcred->cmcred_pid = p->p_pid;
2125a854ed98SJohn Baldwin 			cmcred->cmcred_uid = td->td_ucred->cr_ruid;
2126a854ed98SJohn Baldwin 			cmcred->cmcred_gid = td->td_ucred->cr_rgid;
2127a854ed98SJohn Baldwin 			cmcred->cmcred_euid = td->td_ucred->cr_uid;
2128a854ed98SJohn Baldwin 			cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups,
21290b788fa1SBill Paul 			    CMGROUP_MAX);
21300b788fa1SBill Paul 			for (i = 0; i < cmcred->cmcred_ngroups; i++)
21312bc21ed9SDavid Malone 				cmcred->cmcred_groups[i] =
2132a854ed98SJohn Baldwin 				    td->td_ucred->cr_groups[i];
21332bc21ed9SDavid Malone 			break;
21340b788fa1SBill Paul 
21352bc21ed9SDavid Malone 		case SCM_RIGHTS:
21362bc21ed9SDavid Malone 			oldfds = datalen / sizeof (int);
213782e825c4SGleb Smirnoff 			if (oldfds == 0)
213882e825c4SGleb Smirnoff 				break;
2139ed5b7817SJulian Elischer 			/*
21401c381b19SRobert Watson 			 * Check that all the FDs passed in refer to legal
21411c381b19SRobert Watson 			 * files.  If not, reject the entire operation.
2142ed5b7817SJulian Elischer 			 */
21432bc21ed9SDavid Malone 			fdp = data;
21442609222aSPawel Jakub Dawidek 			FILEDESC_SLOCK(fdesc);
21456a1cf96bSMateusz Guzik 			for (i = 0; i < oldfds; i++, fdp++) {
21466a1cf96bSMateusz Guzik 				fp = fget_locked(fdesc, *fdp);
21476a1cf96bSMateusz Guzik 				if (fp == NULL) {
21482609222aSPawel Jakub Dawidek 					FILEDESC_SUNLOCK(fdesc);
21492bc21ed9SDavid Malone 					error = EBADF;
21502bc21ed9SDavid Malone 					goto out;
21512bc21ed9SDavid Malone 				}
2152e7d6662fSAlfred Perlstein 				if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) {
21532609222aSPawel Jakub Dawidek 					FILEDESC_SUNLOCK(fdesc);
2154e7d6662fSAlfred Perlstein 					error = EOPNOTSUPP;
2155e7d6662fSAlfred Perlstein 					goto out;
2156e7d6662fSAlfred Perlstein 				}
2157e7d6662fSAlfred Perlstein 
2158df8bae1dSRodney W. Grimes 			}
21595e3f7694SRobert Watson 
2160ed5b7817SJulian Elischer 			/*
21610b36cd25SRobert Watson 			 * Now replace the integer FDs with pointers to the
21622609222aSPawel Jakub Dawidek 			 * file structure and capability rights.
2163ed5b7817SJulian Elischer 			 */
21648cb539f1SPawel Jakub Dawidek 			newlen = oldfds * sizeof(fdep[0]);
21652bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, newlen,
21662bc21ed9SDavid Malone 			    SCM_RIGHTS, SOL_SOCKET);
21672bc21ed9SDavid Malone 			if (*controlp == NULL) {
21682609222aSPawel Jakub Dawidek 				FILEDESC_SUNLOCK(fdesc);
21692bc21ed9SDavid Malone 				error = E2BIG;
21702bc21ed9SDavid Malone 				goto out;
21718692c025SYoshinobu Inoue 			}
21722bc21ed9SDavid Malone 			fdp = data;
21738cb539f1SPawel Jakub Dawidek 			fdep = (struct filedescent **)
21742bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
21758cb539f1SPawel Jakub Dawidek 			fdev = malloc(sizeof(*fdev) * oldfds, M_FILECAPS,
21768cb539f1SPawel Jakub Dawidek 			    M_WAITOK);
21778cb539f1SPawel Jakub Dawidek 			for (i = 0; i < oldfds; i++, fdev++, fdp++) {
21782609222aSPawel Jakub Dawidek 				fde = &fdesc->fd_ofiles[*fdp];
21798cb539f1SPawel Jakub Dawidek 				fdep[i] = fdev;
21808cb539f1SPawel Jakub Dawidek 				fdep[i]->fde_file = fde->fde_file;
21818cb539f1SPawel Jakub Dawidek 				filecaps_copy(&fde->fde_caps,
2182d7832811SMateusz Guzik 				    &fdep[i]->fde_caps, true);
21838cb539f1SPawel Jakub Dawidek 				unp_internalize_fp(fdep[i]->fde_file);
2184df8bae1dSRodney W. Grimes 			}
21852609222aSPawel Jakub Dawidek 			FILEDESC_SUNLOCK(fdesc);
21862bc21ed9SDavid Malone 			break;
21872bc21ed9SDavid Malone 
21882bc21ed9SDavid Malone 		case SCM_TIMESTAMP:
21892bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, sizeof(*tv),
21902bc21ed9SDavid Malone 			    SCM_TIMESTAMP, SOL_SOCKET);
21912bc21ed9SDavid Malone 			if (*controlp == NULL) {
21922bc21ed9SDavid Malone 				error = ENOBUFS;
21932bc21ed9SDavid Malone 				goto out;
21948692c025SYoshinobu Inoue 			}
21952bc21ed9SDavid Malone 			tv = (struct timeval *)
21962bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
21972bc21ed9SDavid Malone 			microtime(tv);
21982bc21ed9SDavid Malone 			break;
21992bc21ed9SDavid Malone 
2200ab15d803SSergey Kandaurov 		case SCM_BINTIME:
2201ab15d803SSergey Kandaurov 			*controlp = sbcreatecontrol(NULL, sizeof(*bt),
2202ab15d803SSergey Kandaurov 			    SCM_BINTIME, SOL_SOCKET);
2203ab15d803SSergey Kandaurov 			if (*controlp == NULL) {
2204ab15d803SSergey Kandaurov 				error = ENOBUFS;
2205ab15d803SSergey Kandaurov 				goto out;
2206ab15d803SSergey Kandaurov 			}
2207ab15d803SSergey Kandaurov 			bt = (struct bintime *)
2208ab15d803SSergey Kandaurov 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
2209ab15d803SSergey Kandaurov 			bintime(bt);
2210ab15d803SSergey Kandaurov 			break;
2211ab15d803SSergey Kandaurov 
2212339efd75SMaxim Sobolev 		case SCM_REALTIME:
2213339efd75SMaxim Sobolev 			*controlp = sbcreatecontrol(NULL, sizeof(*ts),
2214339efd75SMaxim Sobolev 			    SCM_REALTIME, SOL_SOCKET);
2215339efd75SMaxim Sobolev 			if (*controlp == NULL) {
2216339efd75SMaxim Sobolev 				error = ENOBUFS;
2217339efd75SMaxim Sobolev 				goto out;
2218339efd75SMaxim Sobolev 			}
2219339efd75SMaxim Sobolev 			ts = (struct timespec *)
2220339efd75SMaxim Sobolev 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
2221339efd75SMaxim Sobolev 			nanotime(ts);
2222339efd75SMaxim Sobolev 			break;
2223339efd75SMaxim Sobolev 
2224339efd75SMaxim Sobolev 		case SCM_MONOTONIC:
2225339efd75SMaxim Sobolev 			*controlp = sbcreatecontrol(NULL, sizeof(*ts),
2226339efd75SMaxim Sobolev 			    SCM_MONOTONIC, SOL_SOCKET);
2227339efd75SMaxim Sobolev 			if (*controlp == NULL) {
2228339efd75SMaxim Sobolev 				error = ENOBUFS;
2229339efd75SMaxim Sobolev 				goto out;
2230339efd75SMaxim Sobolev 			}
2231339efd75SMaxim Sobolev 			ts = (struct timespec *)
2232339efd75SMaxim Sobolev 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
2233339efd75SMaxim Sobolev 			nanouptime(ts);
2234339efd75SMaxim Sobolev 			break;
2235339efd75SMaxim Sobolev 
22362bc21ed9SDavid Malone 		default:
22372bc21ed9SDavid Malone 			error = EINVAL;
22382bc21ed9SDavid Malone 			goto out;
22392bc21ed9SDavid Malone 		}
22402bc21ed9SDavid Malone 
22412bc21ed9SDavid Malone 		controlp = &(*controlp)->m_next;
22422bc21ed9SDavid Malone 		if (CMSG_SPACE(datalen) < clen) {
22432bc21ed9SDavid Malone 			clen -= CMSG_SPACE(datalen);
22442bc21ed9SDavid Malone 			cm = (struct cmsghdr *)
22452bc21ed9SDavid Malone 			    ((caddr_t)cm + CMSG_SPACE(datalen));
22462bc21ed9SDavid Malone 		} else {
22472bc21ed9SDavid Malone 			clen = 0;
22482bc21ed9SDavid Malone 			cm = NULL;
22492bc21ed9SDavid Malone 		}
22502bc21ed9SDavid Malone 	}
22512bc21ed9SDavid Malone 
22522bc21ed9SDavid Malone out:
22532bc21ed9SDavid Malone 	m_freem(control);
22542bc21ed9SDavid Malone 	return (error);
2255df8bae1dSRodney W. Grimes }
2256df8bae1dSRodney W. Grimes 
22575b950deaSRobert Watson static struct mbuf *
22586a2989fdSMatthew N. Dodd unp_addsockcred(struct thread *td, struct mbuf *control)
22596a2989fdSMatthew N. Dodd {
226070df31f4SMaxim Konovalov 	struct mbuf *m, *n, *n_prev;
22616a2989fdSMatthew N. Dodd 	struct sockcred *sc;
226270df31f4SMaxim Konovalov 	const struct cmsghdr *cm;
22636a2989fdSMatthew N. Dodd 	int ngroups;
22646a2989fdSMatthew N. Dodd 	int i;
22656a2989fdSMatthew N. Dodd 
22666a2989fdSMatthew N. Dodd 	ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX);
22676a2989fdSMatthew N. Dodd 	m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET);
22686a2989fdSMatthew N. Dodd 	if (m == NULL)
22696a2989fdSMatthew N. Dodd 		return (control);
22706a2989fdSMatthew N. Dodd 
22716a2989fdSMatthew N. Dodd 	sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *));
22726a2989fdSMatthew N. Dodd 	sc->sc_uid = td->td_ucred->cr_ruid;
22736a2989fdSMatthew N. Dodd 	sc->sc_euid = td->td_ucred->cr_uid;
22746a2989fdSMatthew N. Dodd 	sc->sc_gid = td->td_ucred->cr_rgid;
22756a2989fdSMatthew N. Dodd 	sc->sc_egid = td->td_ucred->cr_gid;
22766a2989fdSMatthew N. Dodd 	sc->sc_ngroups = ngroups;
22776a2989fdSMatthew N. Dodd 	for (i = 0; i < sc->sc_ngroups; i++)
22786a2989fdSMatthew N. Dodd 		sc->sc_groups[i] = td->td_ucred->cr_groups[i];
22796a2989fdSMatthew N. Dodd 
22806a2989fdSMatthew N. Dodd 	/*
22811c381b19SRobert Watson 	 * Unlink SCM_CREDS control messages (struct cmsgcred), since just
22821c381b19SRobert Watson 	 * created SCM_CREDS control message (struct sockcred) has another
22831c381b19SRobert Watson 	 * format.
22846a2989fdSMatthew N. Dodd 	 */
228570df31f4SMaxim Konovalov 	if (control != NULL)
228670df31f4SMaxim Konovalov 		for (n = control, n_prev = NULL; n != NULL;) {
228770df31f4SMaxim Konovalov 			cm = mtod(n, struct cmsghdr *);
228870df31f4SMaxim Konovalov     			if (cm->cmsg_level == SOL_SOCKET &&
228970df31f4SMaxim Konovalov 			    cm->cmsg_type == SCM_CREDS) {
229070df31f4SMaxim Konovalov     				if (n_prev == NULL)
229170df31f4SMaxim Konovalov 					control = n->m_next;
229270df31f4SMaxim Konovalov 				else
229370df31f4SMaxim Konovalov 					n_prev->m_next = n->m_next;
229470df31f4SMaxim Konovalov 				n = m_free(n);
229570df31f4SMaxim Konovalov 			} else {
229670df31f4SMaxim Konovalov 				n_prev = n;
229770df31f4SMaxim Konovalov 				n = n->m_next;
229870df31f4SMaxim Konovalov 			}
229970df31f4SMaxim Konovalov 		}
23006a2989fdSMatthew N. Dodd 
230170df31f4SMaxim Konovalov 	/* Prepend it to the head. */
230270df31f4SMaxim Konovalov 	m->m_next = control;
230370df31f4SMaxim Konovalov 	return (m);
23046a2989fdSMatthew N. Dodd }
23056a2989fdSMatthew N. Dodd 
2306397c19d1SJeff Roberson static struct unpcb *
2307397c19d1SJeff Roberson fptounp(struct file *fp)
2308397c19d1SJeff Roberson {
2309397c19d1SJeff Roberson 	struct socket *so;
2310397c19d1SJeff Roberson 
2311397c19d1SJeff Roberson 	if (fp->f_type != DTYPE_SOCKET)
2312397c19d1SJeff Roberson 		return (NULL);
2313397c19d1SJeff Roberson 	if ((so = fp->f_data) == NULL)
2314397c19d1SJeff Roberson 		return (NULL);
2315397c19d1SJeff Roberson 	if (so->so_proto->pr_domain != &localdomain)
2316397c19d1SJeff Roberson 		return (NULL);
2317397c19d1SJeff Roberson 	return sotounpcb(so);
2318397c19d1SJeff Roberson }
2319397c19d1SJeff Roberson 
2320397c19d1SJeff Roberson static void
2321397c19d1SJeff Roberson unp_discard(struct file *fp)
2322397c19d1SJeff Roberson {
23230cb64678SKonstantin Belousov 	struct unp_defer *dr;
2324397c19d1SJeff Roberson 
23250cb64678SKonstantin Belousov 	if (unp_externalize_fp(fp)) {
23260cb64678SKonstantin Belousov 		dr = malloc(sizeof(*dr), M_TEMP, M_WAITOK);
23270cb64678SKonstantin Belousov 		dr->ud_fp = fp;
23280cb64678SKonstantin Belousov 		UNP_DEFERRED_LOCK();
23290cb64678SKonstantin Belousov 		SLIST_INSERT_HEAD(&unp_defers, dr, ud_link);
23300cb64678SKonstantin Belousov 		UNP_DEFERRED_UNLOCK();
23310cb64678SKonstantin Belousov 		atomic_add_int(&unp_defers_count, 1);
23320cb64678SKonstantin Belousov 		taskqueue_enqueue(taskqueue_thread, &unp_defer_task);
23330cb64678SKonstantin Belousov 	} else
2334397c19d1SJeff Roberson 		(void) closef(fp, (struct thread *)NULL);
2335397c19d1SJeff Roberson }
2336397c19d1SJeff Roberson 
2337397c19d1SJeff Roberson static void
23380cb64678SKonstantin Belousov unp_process_defers(void *arg __unused, int pending)
23390cb64678SKonstantin Belousov {
23400cb64678SKonstantin Belousov 	struct unp_defer *dr;
23410cb64678SKonstantin Belousov 	SLIST_HEAD(, unp_defer) drl;
23420cb64678SKonstantin Belousov 	int count;
23430cb64678SKonstantin Belousov 
23440cb64678SKonstantin Belousov 	SLIST_INIT(&drl);
23450cb64678SKonstantin Belousov 	for (;;) {
23460cb64678SKonstantin Belousov 		UNP_DEFERRED_LOCK();
23470cb64678SKonstantin Belousov 		if (SLIST_FIRST(&unp_defers) == NULL) {
23480cb64678SKonstantin Belousov 			UNP_DEFERRED_UNLOCK();
23490cb64678SKonstantin Belousov 			break;
23500cb64678SKonstantin Belousov 		}
23510cb64678SKonstantin Belousov 		SLIST_SWAP(&unp_defers, &drl, unp_defer);
23520cb64678SKonstantin Belousov 		UNP_DEFERRED_UNLOCK();
23530cb64678SKonstantin Belousov 		count = 0;
23540cb64678SKonstantin Belousov 		while ((dr = SLIST_FIRST(&drl)) != NULL) {
23550cb64678SKonstantin Belousov 			SLIST_REMOVE_HEAD(&drl, ud_link);
23560cb64678SKonstantin Belousov 			closef(dr->ud_fp, NULL);
23570cb64678SKonstantin Belousov 			free(dr, M_TEMP);
23580cb64678SKonstantin Belousov 			count++;
23590cb64678SKonstantin Belousov 		}
23600cb64678SKonstantin Belousov 		atomic_add_int(&unp_defers_count, -count);
23610cb64678SKonstantin Belousov 	}
23620cb64678SKonstantin Belousov }
23630cb64678SKonstantin Belousov 
23640cb64678SKonstantin Belousov static void
2365397c19d1SJeff Roberson unp_internalize_fp(struct file *fp)
2366397c19d1SJeff Roberson {
2367397c19d1SJeff Roberson 	struct unpcb *unp;
2368397c19d1SJeff Roberson 
23693dab55bcSRobert Watson 	UNP_LINK_WLOCK();
2370397c19d1SJeff Roberson 	if ((unp = fptounp(fp)) != NULL) {
2371397c19d1SJeff Roberson 		unp->unp_file = fp;
2372397c19d1SJeff Roberson 		unp->unp_msgcount++;
2373397c19d1SJeff Roberson 	}
237441e0f66dSJeff Roberson 	fhold(fp);
2375397c19d1SJeff Roberson 	unp_rights++;
23763dab55bcSRobert Watson 	UNP_LINK_WUNLOCK();
2377397c19d1SJeff Roberson }
2378397c19d1SJeff Roberson 
23790cb64678SKonstantin Belousov static int
2380397c19d1SJeff Roberson unp_externalize_fp(struct file *fp)
2381397c19d1SJeff Roberson {
2382397c19d1SJeff Roberson 	struct unpcb *unp;
23830cb64678SKonstantin Belousov 	int ret;
2384397c19d1SJeff Roberson 
23853dab55bcSRobert Watson 	UNP_LINK_WLOCK();
23860cb64678SKonstantin Belousov 	if ((unp = fptounp(fp)) != NULL) {
2387397c19d1SJeff Roberson 		unp->unp_msgcount--;
23880cb64678SKonstantin Belousov 		ret = 1;
23890cb64678SKonstantin Belousov 	} else
23900cb64678SKonstantin Belousov 		ret = 0;
2391397c19d1SJeff Roberson 	unp_rights--;
23923dab55bcSRobert Watson 	UNP_LINK_WUNLOCK();
23930cb64678SKonstantin Belousov 	return (ret);
2394397c19d1SJeff Roberson }
2395397c19d1SJeff Roberson 
2396161a0c7cSRobert Watson /*
2397a0ec558aSRobert Watson  * unp_defer indicates whether additional work has been defered for a future
2398a0ec558aSRobert Watson  * pass through unp_gc().  It is thread local and does not require explicit
2399a0ec558aSRobert Watson  * synchronization.
2400161a0c7cSRobert Watson  */
2401397c19d1SJeff Roberson static int	unp_marked;
2402397c19d1SJeff Roberson static int	unp_unreachable;
2403a0ec558aSRobert Watson 
2404397c19d1SJeff Roberson static void
2405be26ba7cSPawel Jakub Dawidek unp_accessable(struct filedescent **fdep, int fdcount)
2406397c19d1SJeff Roberson {
2407397c19d1SJeff Roberson 	struct unpcb *unp;
2408be26ba7cSPawel Jakub Dawidek 	struct file *fp;
2409be26ba7cSPawel Jakub Dawidek 	int i;
2410397c19d1SJeff Roberson 
2411be26ba7cSPawel Jakub Dawidek 	for (i = 0; i < fdcount; i++) {
2412be26ba7cSPawel Jakub Dawidek 		fp = fdep[i]->fde_file;
24136f552cb0SJeff Roberson 		if ((unp = fptounp(fp)) == NULL)
2414be26ba7cSPawel Jakub Dawidek 			continue;
2415397c19d1SJeff Roberson 		if (unp->unp_gcflag & UNPGC_REF)
2416be26ba7cSPawel Jakub Dawidek 			continue;
2417397c19d1SJeff Roberson 		unp->unp_gcflag &= ~UNPGC_DEAD;
2418397c19d1SJeff Roberson 		unp->unp_gcflag |= UNPGC_REF;
2419397c19d1SJeff Roberson 		unp_marked++;
2420397c19d1SJeff Roberson 	}
2421be26ba7cSPawel Jakub Dawidek }
2422397c19d1SJeff Roberson 
2423397c19d1SJeff Roberson static void
2424397c19d1SJeff Roberson unp_gc_process(struct unpcb *unp)
2425397c19d1SJeff Roberson {
2426779f106aSGleb Smirnoff 	struct socket *so, *soa;
2427397c19d1SJeff Roberson 	struct file *fp;
2428397c19d1SJeff Roberson 
2429397c19d1SJeff Roberson 	/* Already processed. */
2430397c19d1SJeff Roberson 	if (unp->unp_gcflag & UNPGC_SCANNED)
2431397c19d1SJeff Roberson 		return;
2432397c19d1SJeff Roberson 	fp = unp->unp_file;
243360a5ef26SRobert Watson 
2434397c19d1SJeff Roberson 	/*
2435397c19d1SJeff Roberson 	 * Check for a socket potentially in a cycle.  It must be in a
2436397c19d1SJeff Roberson 	 * queue as indicated by msgcount, and this must equal the file
2437397c19d1SJeff Roberson 	 * reference count.  Note that when msgcount is 0 the file is NULL.
2438397c19d1SJeff Roberson 	 */
243941e0f66dSJeff Roberson 	if ((unp->unp_gcflag & UNPGC_REF) == 0 && fp &&
244041e0f66dSJeff Roberson 	    unp->unp_msgcount != 0 && fp->f_count == unp->unp_msgcount) {
2441397c19d1SJeff Roberson 		unp->unp_gcflag |= UNPGC_DEAD;
2442397c19d1SJeff Roberson 		unp_unreachable++;
2443397c19d1SJeff Roberson 		return;
2444397c19d1SJeff Roberson 	}
244560a5ef26SRobert Watson 
2446397c19d1SJeff Roberson 	so = unp->unp_socket;
2447779f106aSGleb Smirnoff 	SOCK_LOCK(so);
2448779f106aSGleb Smirnoff 	if (SOLISTENING(so)) {
2449397c19d1SJeff Roberson 		/*
2450397c19d1SJeff Roberson 		 * Mark all sockets in our accept queue.
2451397c19d1SJeff Roberson 		 */
2452779f106aSGleb Smirnoff 		TAILQ_FOREACH(soa, &so->sol_comp, so_list) {
2453779f106aSGleb Smirnoff 			if (sotounpcb(soa)->unp_gcflag & UNPGC_IGNORE_RIGHTS)
24540c40f353SConrad Meyer 				continue;
2455397c19d1SJeff Roberson 			SOCKBUF_LOCK(&soa->so_rcv);
2456397c19d1SJeff Roberson 			unp_scan(soa->so_rcv.sb_mb, unp_accessable);
2457397c19d1SJeff Roberson 			SOCKBUF_UNLOCK(&soa->so_rcv);
2458397c19d1SJeff Roberson 		}
2459779f106aSGleb Smirnoff 	} else {
2460779f106aSGleb Smirnoff 		/*
2461779f106aSGleb Smirnoff 		 * Mark all sockets we reference with RIGHTS.
2462779f106aSGleb Smirnoff 		 */
2463779f106aSGleb Smirnoff 		if ((unp->unp_gcflag & UNPGC_IGNORE_RIGHTS) == 0) {
2464779f106aSGleb Smirnoff 			SOCKBUF_LOCK(&so->so_rcv);
2465779f106aSGleb Smirnoff 			unp_scan(so->so_rcv.sb_mb, unp_accessable);
2466779f106aSGleb Smirnoff 			SOCKBUF_UNLOCK(&so->so_rcv);
2467779f106aSGleb Smirnoff 		}
2468779f106aSGleb Smirnoff 	}
2469779f106aSGleb Smirnoff 	SOCK_UNLOCK(so);
2470397c19d1SJeff Roberson 	unp->unp_gcflag |= UNPGC_SCANNED;
2471397c19d1SJeff Roberson }
2472a0ec558aSRobert Watson 
2473a0ec558aSRobert Watson static int unp_recycled;
2474be6b1304STom Rhodes SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0,
2475be6b1304STom Rhodes     "Number of unreachable sockets claimed by the garbage collector.");
2476df8bae1dSRodney W. Grimes 
2477397c19d1SJeff Roberson static int unp_taskcount;
2478be6b1304STom Rhodes SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0,
2479be6b1304STom Rhodes     "Number of times the garbage collector has run.");
2480397c19d1SJeff Roberson 
2481f708ef1bSPoul-Henning Kamp static void
2482a0ec558aSRobert Watson unp_gc(__unused void *arg, int pending)
2483df8bae1dSRodney W. Grimes {
248484d61770SRobert Watson 	struct unp_head *heads[] = { &unp_dhead, &unp_shead, &unp_sphead,
248584d61770SRobert Watson 				    NULL };
2486397c19d1SJeff Roberson 	struct unp_head **head;
2487f7780c61SKonstantin Belousov 	struct file *f, **unref;
2488397c19d1SJeff Roberson 	struct unpcb *unp;
2489f7780c61SKonstantin Belousov 	int i, total;
2490df8bae1dSRodney W. Grimes 
2491a0ec558aSRobert Watson 	unp_taskcount++;
2492779f106aSGleb Smirnoff 	UNP_LINK_RLOCK();
2493ed5b7817SJulian Elischer 	/*
24940c40f353SConrad Meyer 	 * First clear all gc flags from previous runs, apart from
24950c40f353SConrad Meyer 	 * UNPGC_IGNORE_RIGHTS.
2496ed5b7817SJulian Elischer 	 */
2497397c19d1SJeff Roberson 	for (head = heads; *head != NULL; head++)
2498397c19d1SJeff Roberson 		LIST_FOREACH(unp, *head, unp_link)
24990c40f353SConrad Meyer 			unp->unp_gcflag =
25000c40f353SConrad Meyer 			    (unp->unp_gcflag & UNPGC_IGNORE_RIGHTS);
250160a5ef26SRobert Watson 
2502397c19d1SJeff Roberson 	/*
2503397c19d1SJeff Roberson 	 * Scan marking all reachable sockets with UNPGC_REF.  Once a socket
2504397c19d1SJeff Roberson 	 * is reachable all of the sockets it references are reachable.
2505397c19d1SJeff Roberson 	 * Stop the scan once we do a complete loop without discovering
2506397c19d1SJeff Roberson 	 * a new reachable socket.
2507397c19d1SJeff Roberson 	 */
2508df8bae1dSRodney W. Grimes 	do {
2509397c19d1SJeff Roberson 		unp_unreachable = 0;
2510397c19d1SJeff Roberson 		unp_marked = 0;
2511397c19d1SJeff Roberson 		for (head = heads; *head != NULL; head++)
2512397c19d1SJeff Roberson 			LIST_FOREACH(unp, *head, unp_link)
2513397c19d1SJeff Roberson 				unp_gc_process(unp);
2514397c19d1SJeff Roberson 	} while (unp_marked);
2515779f106aSGleb Smirnoff 	UNP_LINK_RUNLOCK();
2516397c19d1SJeff Roberson 	if (unp_unreachable == 0)
2517397c19d1SJeff Roberson 		return;
251860a5ef26SRobert Watson 
2519ed5b7817SJulian Elischer 	/*
2520397c19d1SJeff Roberson 	 * Allocate space for a local list of dead unpcbs.
2521ed5b7817SJulian Elischer 	 */
2522397c19d1SJeff Roberson 	unref = malloc(unp_unreachable * sizeof(struct file *),
2523397c19d1SJeff Roberson 	    M_TEMP, M_WAITOK);
252460a5ef26SRobert Watson 
2525ed5b7817SJulian Elischer 	/*
2526397c19d1SJeff Roberson 	 * Iterate looking for sockets which have been specifically marked
2527397c19d1SJeff Roberson 	 * as as unreachable and store them locally.
2528ed5b7817SJulian Elischer 	 */
2529f7780c61SKonstantin Belousov 	UNP_LINK_RLOCK();
2530f7780c61SKonstantin Belousov 	for (total = 0, head = heads; *head != NULL; head++)
2531397c19d1SJeff Roberson 		LIST_FOREACH(unp, *head, unp_link)
2532f7780c61SKonstantin Belousov 			if ((unp->unp_gcflag & UNPGC_DEAD) != 0) {
2533f7780c61SKonstantin Belousov 				f = unp->unp_file;
2534f7780c61SKonstantin Belousov 				if (unp->unp_msgcount == 0 || f == NULL ||
2535f7780c61SKonstantin Belousov 				    f->f_count != unp->unp_msgcount)
2536f7780c61SKonstantin Belousov 					continue;
2537f7780c61SKonstantin Belousov 				unref[total++] = f;
2538f7780c61SKonstantin Belousov 				fhold(f);
2539f7780c61SKonstantin Belousov 				KASSERT(total <= unp_unreachable,
2540397c19d1SJeff Roberson 				    ("unp_gc: incorrect unreachable count."));
2541397c19d1SJeff Roberson 			}
2542f7780c61SKonstantin Belousov 	UNP_LINK_RUNLOCK();
254360a5ef26SRobert Watson 
2544ed5b7817SJulian Elischer 	/*
2545397c19d1SJeff Roberson 	 * Now flush all sockets, free'ing rights.  This will free the
2546397c19d1SJeff Roberson 	 * struct files associated with these sockets but leave each socket
2547397c19d1SJeff Roberson 	 * with one remaining ref.
2548ed5b7817SJulian Elischer 	 */
25491fb51a12SBjoern A. Zeeb 	for (i = 0; i < total; i++) {
25501fb51a12SBjoern A. Zeeb 		struct socket *so;
25511fb51a12SBjoern A. Zeeb 
25521fb51a12SBjoern A. Zeeb 		so = unref[i]->f_data;
25531fb51a12SBjoern A. Zeeb 		CURVNET_SET(so->so_vnet);
25541fb51a12SBjoern A. Zeeb 		sorflush(so);
25551fb51a12SBjoern A. Zeeb 		CURVNET_RESTORE();
25561fb51a12SBjoern A. Zeeb 	}
255760a5ef26SRobert Watson 
2558ed5b7817SJulian Elischer 	/*
2559397c19d1SJeff Roberson 	 * And finally release the sockets so they can be reclaimed.
2560ed5b7817SJulian Elischer 	 */
2561f7780c61SKonstantin Belousov 	for (i = 0; i < total; i++)
2562397c19d1SJeff Roberson 		fdrop(unref[i], NULL);
2563f7780c61SKonstantin Belousov 	unp_recycled += total;
2564397c19d1SJeff Roberson 	free(unref, M_TEMP);
2565df8bae1dSRodney W. Grimes }
2566df8bae1dSRodney W. Grimes 
25670b36cd25SRobert Watson static void
256899ab95dbSMark Johnston unp_dispose_mbuf(struct mbuf *m)
2569df8bae1dSRodney W. Grimes {
2570996c772fSJohn Dyson 
2571df8bae1dSRodney W. Grimes 	if (m)
2572be26ba7cSPawel Jakub Dawidek 		unp_scan(m, unp_freerights);
2573df8bae1dSRodney W. Grimes }
2574df8bae1dSRodney W. Grimes 
25750c40f353SConrad Meyer /*
25760c40f353SConrad Meyer  * Synchronize against unp_gc, which can trip over data as we are freeing it.
25770c40f353SConrad Meyer  */
25780c40f353SConrad Meyer static void
257999ab95dbSMark Johnston unp_dispose(struct socket *so)
25800c40f353SConrad Meyer {
25810c40f353SConrad Meyer 	struct unpcb *unp;
25820c40f353SConrad Meyer 
25830c40f353SConrad Meyer 	unp = sotounpcb(so);
2584779f106aSGleb Smirnoff 	UNP_LINK_WLOCK();
25850c40f353SConrad Meyer 	unp->unp_gcflag |= UNPGC_IGNORE_RIGHTS;
2586779f106aSGleb Smirnoff 	UNP_LINK_WUNLOCK();
2587779f106aSGleb Smirnoff 	if (!SOLISTENING(so))
258899ab95dbSMark Johnston 		unp_dispose_mbuf(so->so_rcv.sb_mb);
25890c40f353SConrad Meyer }
25900c40f353SConrad Meyer 
2591f708ef1bSPoul-Henning Kamp static void
2592be26ba7cSPawel Jakub Dawidek unp_scan(struct mbuf *m0, void (*op)(struct filedescent **, int))
2593df8bae1dSRodney W. Grimes {
25942bc21ed9SDavid Malone 	struct mbuf *m;
25952bc21ed9SDavid Malone 	struct cmsghdr *cm;
25962bc21ed9SDavid Malone 	void *data;
25972bc21ed9SDavid Malone 	socklen_t clen, datalen;
2598df8bae1dSRodney W. Grimes 
2599fc3fcacfSRobert Watson 	while (m0 != NULL) {
26002bc21ed9SDavid Malone 		for (m = m0; m; m = m->m_next) {
260112396bdcSDavid Malone 			if (m->m_type != MT_CONTROL)
2602df8bae1dSRodney W. Grimes 				continue;
26032bc21ed9SDavid Malone 
26042bc21ed9SDavid Malone 			cm = mtod(m, struct cmsghdr *);
26052bc21ed9SDavid Malone 			clen = m->m_len;
26062bc21ed9SDavid Malone 
26072bc21ed9SDavid Malone 			while (cm != NULL) {
26082bc21ed9SDavid Malone 				if (sizeof(*cm) > clen || cm->cmsg_len > clen)
26092bc21ed9SDavid Malone 					break;
26102bc21ed9SDavid Malone 
26112bc21ed9SDavid Malone 				data = CMSG_DATA(cm);
26122bc21ed9SDavid Malone 				datalen = (caddr_t)cm + cm->cmsg_len
26132bc21ed9SDavid Malone 				    - (caddr_t)data;
26142bc21ed9SDavid Malone 
26152bc21ed9SDavid Malone 				if (cm->cmsg_level == SOL_SOCKET &&
26162bc21ed9SDavid Malone 				    cm->cmsg_type == SCM_RIGHTS) {
2617be26ba7cSPawel Jakub Dawidek 					(*op)(data, datalen /
2618be26ba7cSPawel Jakub Dawidek 					    sizeof(struct filedescent *));
26192bc21ed9SDavid Malone 				}
26202bc21ed9SDavid Malone 
26212bc21ed9SDavid Malone 				if (CMSG_SPACE(datalen) < clen) {
26222bc21ed9SDavid Malone 					clen -= CMSG_SPACE(datalen);
26232bc21ed9SDavid Malone 					cm = (struct cmsghdr *)
26242bc21ed9SDavid Malone 					    ((caddr_t)cm + CMSG_SPACE(datalen));
26252bc21ed9SDavid Malone 				} else {
26262bc21ed9SDavid Malone 					clen = 0;
26272bc21ed9SDavid Malone 					cm = NULL;
26282bc21ed9SDavid Malone 				}
26292bc21ed9SDavid Malone 			}
2630df8bae1dSRodney W. Grimes 		}
2631c29a3321SKevin Lo 		m0 = m0->m_nextpkt;
2632df8bae1dSRodney W. Grimes 	}
2633df8bae1dSRodney W. Grimes }
2634df8bae1dSRodney W. Grimes 
2635662c901cSMikolaj Golub /*
2636662c901cSMikolaj Golub  * A helper function called by VFS before socket-type vnode reclamation.
2637662c901cSMikolaj Golub  * For an active vnode it clears unp_vnode pointer and decrements unp_vnode
2638662c901cSMikolaj Golub  * use count.
2639662c901cSMikolaj Golub  */
2640662c901cSMikolaj Golub void
2641662c901cSMikolaj Golub vfs_unp_reclaim(struct vnode *vp)
2642662c901cSMikolaj Golub {
2643662c901cSMikolaj Golub 	struct unpcb *unp;
2644662c901cSMikolaj Golub 	int active;
264575a67bf3SMatt Macy 	struct mtx *vplock;
2646662c901cSMikolaj Golub 
2647662c901cSMikolaj Golub 	ASSERT_VOP_ELOCKED(vp, "vfs_unp_reclaim");
2648662c901cSMikolaj Golub 	KASSERT(vp->v_type == VSOCK,
2649662c901cSMikolaj Golub 	    ("vfs_unp_reclaim: vp->v_type != VSOCK"));
2650662c901cSMikolaj Golub 
2651662c901cSMikolaj Golub 	active = 0;
265275a67bf3SMatt Macy 	vplock = mtx_pool_find(mtxpool_sleep, vp);
265375a67bf3SMatt Macy 	mtx_lock(vplock);
26540c3c207fSGleb Smirnoff 	VOP_UNP_CONNECT(vp, &unp);
2655662c901cSMikolaj Golub 	if (unp == NULL)
2656662c901cSMikolaj Golub 		goto done;
2657662c901cSMikolaj Golub 	UNP_PCB_LOCK(unp);
2658c7e41c8bSMikolaj Golub 	if (unp->unp_vnode == vp) {
2659c7e41c8bSMikolaj Golub 		VOP_UNP_DETACH(vp);
2660662c901cSMikolaj Golub 		unp->unp_vnode = NULL;
2661662c901cSMikolaj Golub 		active = 1;
2662662c901cSMikolaj Golub 	}
2663662c901cSMikolaj Golub 	UNP_PCB_UNLOCK(unp);
2664662c901cSMikolaj Golub  done:
266575a67bf3SMatt Macy 	mtx_unlock(vplock);
2666662c901cSMikolaj Golub 	if (active)
2667662c901cSMikolaj Golub 		vunref(vp);
2668662c901cSMikolaj Golub }
2669662c901cSMikolaj Golub 
267003c96c31SRobert Watson #ifdef DDB
267103c96c31SRobert Watson static void
267203c96c31SRobert Watson db_print_indent(int indent)
267303c96c31SRobert Watson {
267403c96c31SRobert Watson 	int i;
267503c96c31SRobert Watson 
267603c96c31SRobert Watson 	for (i = 0; i < indent; i++)
267703c96c31SRobert Watson 		db_printf(" ");
267803c96c31SRobert Watson }
267903c96c31SRobert Watson 
268003c96c31SRobert Watson static void
268103c96c31SRobert Watson db_print_unpflags(int unp_flags)
268203c96c31SRobert Watson {
268303c96c31SRobert Watson 	int comma;
268403c96c31SRobert Watson 
268503c96c31SRobert Watson 	comma = 0;
268603c96c31SRobert Watson 	if (unp_flags & UNP_HAVEPC) {
268703c96c31SRobert Watson 		db_printf("%sUNP_HAVEPC", comma ? ", " : "");
268803c96c31SRobert Watson 		comma = 1;
268903c96c31SRobert Watson 	}
269003c96c31SRobert Watson 	if (unp_flags & UNP_WANTCRED) {
269103c96c31SRobert Watson 		db_printf("%sUNP_WANTCRED", comma ? ", " : "");
269203c96c31SRobert Watson 		comma = 1;
269303c96c31SRobert Watson 	}
269403c96c31SRobert Watson 	if (unp_flags & UNP_CONNWAIT) {
269503c96c31SRobert Watson 		db_printf("%sUNP_CONNWAIT", comma ? ", " : "");
269603c96c31SRobert Watson 		comma = 1;
269703c96c31SRobert Watson 	}
269803c96c31SRobert Watson 	if (unp_flags & UNP_CONNECTING) {
269903c96c31SRobert Watson 		db_printf("%sUNP_CONNECTING", comma ? ", " : "");
270003c96c31SRobert Watson 		comma = 1;
270103c96c31SRobert Watson 	}
270203c96c31SRobert Watson 	if (unp_flags & UNP_BINDING) {
270303c96c31SRobert Watson 		db_printf("%sUNP_BINDING", comma ? ", " : "");
270403c96c31SRobert Watson 		comma = 1;
270503c96c31SRobert Watson 	}
270603c96c31SRobert Watson }
270703c96c31SRobert Watson 
270803c96c31SRobert Watson static void
270903c96c31SRobert Watson db_print_xucred(int indent, struct xucred *xu)
271003c96c31SRobert Watson {
271103c96c31SRobert Watson 	int comma, i;
271203c96c31SRobert Watson 
271303c96c31SRobert Watson 	db_print_indent(indent);
271403c96c31SRobert Watson 	db_printf("cr_version: %u   cr_uid: %u   cr_ngroups: %d\n",
271503c96c31SRobert Watson 	    xu->cr_version, xu->cr_uid, xu->cr_ngroups);
271603c96c31SRobert Watson 	db_print_indent(indent);
271703c96c31SRobert Watson 	db_printf("cr_groups: ");
271803c96c31SRobert Watson 	comma = 0;
271903c96c31SRobert Watson 	for (i = 0; i < xu->cr_ngroups; i++) {
272003c96c31SRobert Watson 		db_printf("%s%u", comma ? ", " : "", xu->cr_groups[i]);
272103c96c31SRobert Watson 		comma = 1;
272203c96c31SRobert Watson 	}
272303c96c31SRobert Watson 	db_printf("\n");
272403c96c31SRobert Watson }
272503c96c31SRobert Watson 
272603c96c31SRobert Watson static void
272703c96c31SRobert Watson db_print_unprefs(int indent, struct unp_head *uh)
272803c96c31SRobert Watson {
272903c96c31SRobert Watson 	struct unpcb *unp;
273003c96c31SRobert Watson 	int counter;
273103c96c31SRobert Watson 
273203c96c31SRobert Watson 	counter = 0;
273303c96c31SRobert Watson 	LIST_FOREACH(unp, uh, unp_reflink) {
273403c96c31SRobert Watson 		if (counter % 4 == 0)
273503c96c31SRobert Watson 			db_print_indent(indent);
273603c96c31SRobert Watson 		db_printf("%p  ", unp);
273703c96c31SRobert Watson 		if (counter % 4 == 3)
273803c96c31SRobert Watson 			db_printf("\n");
273903c96c31SRobert Watson 		counter++;
274003c96c31SRobert Watson 	}
274103c96c31SRobert Watson 	if (counter != 0 && counter % 4 != 0)
274203c96c31SRobert Watson 		db_printf("\n");
274303c96c31SRobert Watson }
274403c96c31SRobert Watson 
274503c96c31SRobert Watson DB_SHOW_COMMAND(unpcb, db_show_unpcb)
274603c96c31SRobert Watson {
274703c96c31SRobert Watson 	struct unpcb *unp;
274803c96c31SRobert Watson 
274903c96c31SRobert Watson         if (!have_addr) {
275003c96c31SRobert Watson                 db_printf("usage: show unpcb <addr>\n");
275103c96c31SRobert Watson                 return;
275203c96c31SRobert Watson         }
275303c96c31SRobert Watson         unp = (struct unpcb *)addr;
275403c96c31SRobert Watson 
275503c96c31SRobert Watson 	db_printf("unp_socket: %p   unp_vnode: %p\n", unp->unp_socket,
275603c96c31SRobert Watson 	    unp->unp_vnode);
275703c96c31SRobert Watson 
2758fc8fdae0SMatthew D Fleming 	db_printf("unp_ino: %ju   unp_conn: %p\n", (uintmax_t)unp->unp_ino,
275903c96c31SRobert Watson 	    unp->unp_conn);
276003c96c31SRobert Watson 
276103c96c31SRobert Watson 	db_printf("unp_refs:\n");
276203c96c31SRobert Watson 	db_print_unprefs(2, &unp->unp_refs);
276303c96c31SRobert Watson 
276403c96c31SRobert Watson 	/* XXXRW: Would be nice to print the full address, if any. */
276503c96c31SRobert Watson 	db_printf("unp_addr: %p\n", unp->unp_addr);
276603c96c31SRobert Watson 
2767c2090e73SAlan Somers 	db_printf("unp_gencnt: %llu\n",
276803c96c31SRobert Watson 	    (unsigned long long)unp->unp_gencnt);
276903c96c31SRobert Watson 
277003c96c31SRobert Watson 	db_printf("unp_flags: %x (", unp->unp_flags);
277103c96c31SRobert Watson 	db_print_unpflags(unp->unp_flags);
277203c96c31SRobert Watson 	db_printf(")\n");
277303c96c31SRobert Watson 
277403c96c31SRobert Watson 	db_printf("unp_peercred:\n");
277503c96c31SRobert Watson 	db_print_xucred(2, &unp->unp_peercred);
277603c96c31SRobert Watson 
277703c96c31SRobert Watson 	db_printf("unp_refcount: %u\n", unp->unp_refcount);
277803c96c31SRobert Watson }
277903c96c31SRobert Watson #endif
2780