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 1667029da5cSPawel Biernacki static SYSCTL_NODE(_net, PF_LOCAL, local, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1677029da5cSPawel Biernacki "Local domain"); 1687029da5cSPawel Biernacki static SYSCTL_NODE(_net_local, SOCK_STREAM, stream, 1697029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1706472ac3dSEd Schouten "SOCK_STREAM"); 1717029da5cSPawel Biernacki static SYSCTL_NODE(_net_local, SOCK_DGRAM, dgram, 1727029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1737029da5cSPawel Biernacki "SOCK_DGRAM"); 1747029da5cSPawel Biernacki static SYSCTL_NODE(_net_local, SOCK_SEQPACKET, seqpacket, 1757029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 17684d61770SRobert Watson "SOCK_SEQPACKET"); 177e4445a03SRobert Watson 1787e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 179be6b1304STom Rhodes &unpst_sendspace, 0, "Default stream send space."); 1807e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 181be6b1304STom Rhodes &unpst_recvspace, 0, "Default stream receive space."); 1827e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 183be6b1304STom Rhodes &unpdg_sendspace, 0, "Default datagram send space."); 1847e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 185be6b1304STom Rhodes &unpdg_recvspace, 0, "Default datagram receive space."); 18684d61770SRobert Watson SYSCTL_ULONG(_net_local_seqpacket, OID_AUTO, maxseqpacket, CTLFLAG_RW, 18784d61770SRobert Watson &unpsp_sendspace, 0, "Default seqpacket send space."); 18884d61770SRobert Watson SYSCTL_ULONG(_net_local_seqpacket, OID_AUTO, recvspace, CTLFLAG_RW, 18984d61770SRobert Watson &unpsp_recvspace, 0, "Default seqpacket receive space."); 190be6b1304STom Rhodes SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, 191be6b1304STom Rhodes "File descriptors in flight."); 1920cb64678SKonstantin Belousov SYSCTL_INT(_net_local, OID_AUTO, deferred, CTLFLAG_RD, 1930cb64678SKonstantin Belousov &unp_defers_count, 0, 1940cb64678SKonstantin Belousov "File descriptors deferred to taskqueue for close."); 1957e711c3aSRobert Watson 196175389cfSEdward Tomasz Napierala /* 197e7c33e29SRobert Watson * Locking and synchronization: 198ce5f32deSRobert Watson * 19975a67bf3SMatt Macy * Three types of locks exist in the local domain socket implementation: a 20075a67bf3SMatt Macy * a global linkage rwlock, the mtxpool lock, and per-unpcb mutexes. 20175a67bf3SMatt Macy * The linkage lock protects the socket count, global generation number, 20275a67bf3SMatt Macy * and stream/datagram global lists. 20375a67bf3SMatt Macy * 20475a67bf3SMatt Macy * The mtxpool lock protects the vnode from being modified while referenced. 20575a67bf3SMatt Macy * Lock ordering requires that it be acquired before any unpcb locks. 20675a67bf3SMatt Macy * 20775a67bf3SMatt Macy * The unpcb lock (unp_mtx) protects all fields in the unpcb. Of particular 20875a67bf3SMatt Macy * note is that this includes the unp_conn field. So long as the unpcb lock 20975a67bf3SMatt Macy * is held the reference to the unpcb pointed to by unp_conn is valid. If we 21075a67bf3SMatt Macy * require that the unpcb pointed to by unp_conn remain live in cases where 21175a67bf3SMatt Macy * we need to drop the unp_mtx as when we need to acquire the lock for a 21275a67bf3SMatt Macy * second unpcb the caller must first acquire an additional reference on the 21375a67bf3SMatt Macy * second unpcb and then revalidate any state (typically check that unp_conn 21475a67bf3SMatt Macy * is non-NULL) upon requiring the initial unpcb lock. The lock ordering 21575a67bf3SMatt Macy * between unpcbs is the conventional ascending address order. Two helper 21675a67bf3SMatt Macy * routines exist for this: 21775a67bf3SMatt Macy * 21875a67bf3SMatt Macy * - unp_pcb_lock2(unp, unp2) - which just acquires the two locks in the 21975a67bf3SMatt Macy * safe ordering. 22075a67bf3SMatt Macy * 22175a67bf3SMatt Macy * - unp_pcb_owned_lock2(unp, unp2, freed) - the lock for unp is held 22275a67bf3SMatt Macy * when called. If unp is unlocked and unp2 is subsequently freed 22375a67bf3SMatt Macy * freed will be set to 1. 22475a67bf3SMatt Macy * 22575a67bf3SMatt Macy * The helper routines for references are: 22675a67bf3SMatt Macy * 22775a67bf3SMatt Macy * - unp_pcb_hold(unp): Can be called any time we currently hold a valid 22875a67bf3SMatt Macy * reference to unp. 22975a67bf3SMatt Macy * 23075a67bf3SMatt Macy * - unp_pcb_rele(unp): The caller must hold the unp lock. If we are 23175a67bf3SMatt Macy * releasing the last reference, detach must have been called thus 23275a67bf3SMatt Macy * unp->unp_socket be NULL. 233ce5f32deSRobert Watson * 234e7c33e29SRobert Watson * UNIX domain sockets each have an unpcb hung off of their so_pcb pointer, 235e7c33e29SRobert Watson * allocated in pru_attach() and freed in pru_detach(). The validity of that 236e7c33e29SRobert Watson * pointer is an invariant, so no lock is required to dereference the so_pcb 237e7c33e29SRobert Watson * pointer if a valid socket reference is held by the caller. In practice, 238e7c33e29SRobert Watson * this is always true during operations performed on a socket. Each unpcb 239e7c33e29SRobert Watson * has a back-pointer to its socket, unp_socket, which will be stable under 240e7c33e29SRobert Watson * the same circumstances. 241e7c33e29SRobert Watson * 242e7c33e29SRobert Watson * This pointer may only be safely dereferenced as long as a valid reference 243e7c33e29SRobert Watson * to the unpcb is held. Typically, this reference will be from the socket, 244e7c33e29SRobert Watson * or from another unpcb when the referring unpcb's lock is held (in order 245e7c33e29SRobert Watson * that the reference not be invalidated during use). For example, to follow 24675a67bf3SMatt Macy * unp->unp_conn->unp_socket, you need to hold a lock on unp_conn to guarantee 24775a67bf3SMatt Macy * that detach is not run clearing unp_socket. 248e7c33e29SRobert Watson * 249e7c33e29SRobert Watson * Blocking with UNIX domain sockets is a tricky issue: unlike most network 250e7c33e29SRobert Watson * protocols, bind() is a non-atomic operation, and connect() requires 251e7c33e29SRobert Watson * potential sleeping in the protocol, due to potentially waiting on local or 252e7c33e29SRobert Watson * distributed file systems. We try to separate "lookup" operations, which 253e7c33e29SRobert Watson * may sleep, and the IPC operations themselves, which typically can occur 254e7c33e29SRobert Watson * with relative atomicity as locks can be held over the entire operation. 255e7c33e29SRobert Watson * 256e7c33e29SRobert Watson * Another tricky issue is simultaneous multi-threaded or multi-process 257e7c33e29SRobert Watson * access to a single UNIX domain socket. These are handled by the flags 258e7c33e29SRobert Watson * UNP_CONNECTING and UNP_BINDING, which prevent concurrent connecting or 259e7c33e29SRobert Watson * binding, both of which involve dropping UNIX domain socket locks in order 260e7c33e29SRobert Watson * to perform namei() and other file system operations. 261ce5f32deSRobert Watson */ 2623dab55bcSRobert Watson static struct rwlock unp_link_rwlock; 2630cb64678SKonstantin Belousov static struct mtx unp_defers_lock; 264e7c33e29SRobert Watson 2653dab55bcSRobert Watson #define UNP_LINK_LOCK_INIT() rw_init(&unp_link_rwlock, \ 2663dab55bcSRobert Watson "unp_link_rwlock") 267e7c33e29SRobert Watson 2683dab55bcSRobert Watson #define UNP_LINK_LOCK_ASSERT() rw_assert(&unp_link_rwlock, \ 269e7c33e29SRobert Watson RA_LOCKED) 2703dab55bcSRobert Watson #define UNP_LINK_UNLOCK_ASSERT() rw_assert(&unp_link_rwlock, \ 271e7c33e29SRobert Watson RA_UNLOCKED) 272e7c33e29SRobert Watson 2733dab55bcSRobert Watson #define UNP_LINK_RLOCK() rw_rlock(&unp_link_rwlock) 2743dab55bcSRobert Watson #define UNP_LINK_RUNLOCK() rw_runlock(&unp_link_rwlock) 2753dab55bcSRobert Watson #define UNP_LINK_WLOCK() rw_wlock(&unp_link_rwlock) 2763dab55bcSRobert Watson #define UNP_LINK_WUNLOCK() rw_wunlock(&unp_link_rwlock) 2773dab55bcSRobert Watson #define UNP_LINK_WLOCK_ASSERT() rw_assert(&unp_link_rwlock, \ 278e7c33e29SRobert Watson RA_WLOCKED) 279779f106aSGleb Smirnoff #define UNP_LINK_WOWNED() rw_wowned(&unp_link_rwlock) 280e7c33e29SRobert Watson 2810cb64678SKonstantin Belousov #define UNP_DEFERRED_LOCK_INIT() mtx_init(&unp_defers_lock, \ 2820cb64678SKonstantin Belousov "unp_defer", NULL, MTX_DEF) 2830cb64678SKonstantin Belousov #define UNP_DEFERRED_LOCK() mtx_lock(&unp_defers_lock) 2840cb64678SKonstantin Belousov #define UNP_DEFERRED_UNLOCK() mtx_unlock(&unp_defers_lock) 2850cb64678SKonstantin Belousov 28675a67bf3SMatt Macy #define UNP_REF_LIST_LOCK() UNP_DEFERRED_LOCK(); 28775a67bf3SMatt Macy #define UNP_REF_LIST_UNLOCK() UNP_DEFERRED_UNLOCK(); 28875a67bf3SMatt Macy 289e7c33e29SRobert Watson #define UNP_PCB_LOCK_INIT(unp) mtx_init(&(unp)->unp_mtx, \ 290d9525340SMatt Macy "unp", "unp", \ 29175a67bf3SMatt Macy MTX_DUPOK|MTX_DEF) 292e7c33e29SRobert Watson #define UNP_PCB_LOCK_DESTROY(unp) mtx_destroy(&(unp)->unp_mtx) 293e7c33e29SRobert Watson #define UNP_PCB_LOCK(unp) mtx_lock(&(unp)->unp_mtx) 29475a67bf3SMatt Macy #define UNP_PCB_TRYLOCK(unp) mtx_trylock(&(unp)->unp_mtx) 295e7c33e29SRobert Watson #define UNP_PCB_UNLOCK(unp) mtx_unlock(&(unp)->unp_mtx) 29675a67bf3SMatt Macy #define UNP_PCB_OWNED(unp) mtx_owned(&(unp)->unp_mtx) 297e7c33e29SRobert Watson #define UNP_PCB_LOCK_ASSERT(unp) mtx_assert(&(unp)->unp_mtx, MA_OWNED) 29875a67bf3SMatt Macy #define UNP_PCB_UNLOCK_ASSERT(unp) mtx_assert(&(unp)->unp_mtx, MA_NOTOWNED) 2990d9ce3a1SRobert Watson 3002c899584SRobert Watson static int uipc_connect2(struct socket *, struct socket *); 3010b36cd25SRobert Watson static int uipc_ctloutput(struct socket *, struct sockopt *); 302aea52f1bSRobert Watson static int unp_connect(struct socket *, struct sockaddr *, 303aea52f1bSRobert Watson struct thread *); 3047493f24eSPawel Jakub Dawidek static int unp_connectat(int, struct socket *, struct sockaddr *, 3057493f24eSPawel Jakub Dawidek struct thread *); 3066a2989fdSMatthew N. Dodd static int unp_connect2(struct socket *so, struct socket *so2, int); 307e7c33e29SRobert Watson static void unp_disconnect(struct unpcb *unp, struct unpcb *unp2); 30899ab95dbSMark Johnston static void unp_dispose(struct socket *so); 30999ab95dbSMark Johnston static void unp_dispose_mbuf(struct mbuf *); 3104d77a549SAlfred Perlstein static void unp_shutdown(struct unpcb *); 311afc055d9SEd Schouten static void unp_drop(struct unpcb *); 312a0ec558aSRobert Watson static void unp_gc(__unused void *, int); 313be26ba7cSPawel Jakub Dawidek static void unp_scan(struct mbuf *, void (*)(struct filedescent **, int)); 3144d77a549SAlfred Perlstein static void unp_discard(struct file *); 3158cb539f1SPawel Jakub Dawidek static void unp_freerights(struct filedescent **, int); 3160b36cd25SRobert Watson static void unp_init(void); 3174d77a549SAlfred Perlstein static int unp_internalize(struct mbuf **, struct thread *); 318397c19d1SJeff Roberson static void unp_internalize_fp(struct file *); 319c2e3c52eSJilles Tjoelker static int unp_externalize(struct mbuf *, struct mbuf **, int); 3200cb64678SKonstantin Belousov static int unp_externalize_fp(struct file *); 3215b950deaSRobert Watson static struct mbuf *unp_addsockcred(struct thread *, struct mbuf *); 3220cb64678SKonstantin Belousov static void unp_process_defers(void * __unused, int); 323f708ef1bSPoul-Henning Kamp 32475a67bf3SMatt Macy static void 32575a67bf3SMatt Macy unp_pcb_hold(struct unpcb *unp) 32675a67bf3SMatt Macy { 32775a67bf3SMatt Macy MPASS(unp->unp_refcount); 32875a67bf3SMatt Macy refcount_acquire(&unp->unp_refcount); 32975a67bf3SMatt Macy } 33075a67bf3SMatt Macy 33175a67bf3SMatt Macy static int 33275a67bf3SMatt Macy unp_pcb_rele(struct unpcb *unp) 33375a67bf3SMatt Macy { 33475a67bf3SMatt Macy int freed; 33575a67bf3SMatt Macy 33675a67bf3SMatt Macy UNP_PCB_LOCK_ASSERT(unp); 33775a67bf3SMatt Macy MPASS(unp->unp_refcount); 33875a67bf3SMatt Macy if ((freed = refcount_release(&unp->unp_refcount))) { 33975a67bf3SMatt Macy /* we got here with having detached? */ 34075a67bf3SMatt Macy MPASS(unp->unp_socket == NULL); 34175a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 34275a67bf3SMatt Macy UNP_PCB_LOCK_DESTROY(unp); 34375a67bf3SMatt Macy uma_zfree(unp_zone, unp); 34475a67bf3SMatt Macy } 34575a67bf3SMatt Macy return (freed); 34675a67bf3SMatt Macy } 34775a67bf3SMatt Macy 34875a67bf3SMatt Macy static void 34975a67bf3SMatt Macy unp_pcb_lock2(struct unpcb *unp, struct unpcb *unp2) 35075a67bf3SMatt Macy { 35116529dacSMatt Macy MPASS(unp != unp2); 35275a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT(unp); 35375a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT(unp2); 35475a67bf3SMatt Macy if ((uintptr_t)unp2 > (uintptr_t)unp) { 35575a67bf3SMatt Macy UNP_PCB_LOCK(unp); 35675a67bf3SMatt Macy UNP_PCB_LOCK(unp2); 35775a67bf3SMatt Macy } else { 35875a67bf3SMatt Macy UNP_PCB_LOCK(unp2); 35975a67bf3SMatt Macy UNP_PCB_LOCK(unp); 36075a67bf3SMatt Macy } 36175a67bf3SMatt Macy } 36275a67bf3SMatt Macy 36375a67bf3SMatt Macy static __noinline void 364e62ca80bSMark Johnston unp_pcb_owned_lock2_slowpath(struct unpcb *unp, struct unpcb **unp2p, 365e62ca80bSMark Johnston int *freed) 36675a67bf3SMatt Macy { 36775a67bf3SMatt Macy struct unpcb *unp2; 36875a67bf3SMatt Macy 36975a67bf3SMatt Macy unp2 = *unp2p; 370e62ca80bSMark Johnston unp_pcb_hold(unp2); 371e62ca80bSMark Johnston UNP_PCB_UNLOCK(unp); 372e62ca80bSMark Johnston UNP_PCB_LOCK(unp2); 373e62ca80bSMark Johnston UNP_PCB_LOCK(unp); 374e62ca80bSMark Johnston *freed = unp_pcb_rele(unp2); 37575a67bf3SMatt Macy if (*freed) 37675a67bf3SMatt Macy *unp2p = NULL; 37775a67bf3SMatt Macy } 37875a67bf3SMatt Macy 37975a67bf3SMatt Macy #define unp_pcb_owned_lock2(unp, unp2, freed) do { \ 38075a67bf3SMatt Macy freed = 0; \ 381e62ca80bSMark Johnston UNP_PCB_LOCK_ASSERT(unp); \ 382e62ca80bSMark Johnston UNP_PCB_UNLOCK_ASSERT(unp2); \ 383e62ca80bSMark Johnston MPASS((unp) != (unp2)); \ 384e62ca80bSMark Johnston if (__predict_true(UNP_PCB_TRYLOCK(unp2))) \ 38575a67bf3SMatt Macy break; \ 38675a67bf3SMatt Macy else if ((uintptr_t)(unp2) > (uintptr_t)(unp)) \ 387e62ca80bSMark Johnston UNP_PCB_LOCK(unp2); \ 388e62ca80bSMark Johnston else \ 38975a67bf3SMatt Macy unp_pcb_owned_lock2_slowpath((unp), &(unp2), &freed); \ 39075a67bf3SMatt Macy } while (0) 39175a67bf3SMatt Macy 392e4445a03SRobert Watson /* 393e4445a03SRobert Watson * Definitions of protocols supported in the LOCAL domain. 394e4445a03SRobert Watson */ 395e4445a03SRobert Watson static struct domain localdomain; 396fa9402f2SRobert Watson static struct pr_usrreqs uipc_usrreqs_dgram, uipc_usrreqs_stream; 39784d61770SRobert Watson static struct pr_usrreqs uipc_usrreqs_seqpacket; 398e4445a03SRobert Watson static struct protosw localsw[] = { 399e4445a03SRobert Watson { 400e4445a03SRobert Watson .pr_type = SOCK_STREAM, 401e4445a03SRobert Watson .pr_domain = &localdomain, 402e4445a03SRobert Watson .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_RIGHTS, 403e4445a03SRobert Watson .pr_ctloutput = &uipc_ctloutput, 404fa9402f2SRobert Watson .pr_usrreqs = &uipc_usrreqs_stream 405e4445a03SRobert Watson }, 406e4445a03SRobert Watson { 407e4445a03SRobert Watson .pr_type = SOCK_DGRAM, 408e4445a03SRobert Watson .pr_domain = &localdomain, 409e4445a03SRobert Watson .pr_flags = PR_ATOMIC|PR_ADDR|PR_RIGHTS, 410aaf63435SGleb Smirnoff .pr_ctloutput = &uipc_ctloutput, 411fa9402f2SRobert Watson .pr_usrreqs = &uipc_usrreqs_dgram 412e4445a03SRobert Watson }, 41384d61770SRobert Watson { 41484d61770SRobert Watson .pr_type = SOCK_SEQPACKET, 41584d61770SRobert Watson .pr_domain = &localdomain, 41684d61770SRobert Watson 41784d61770SRobert Watson /* 41884d61770SRobert Watson * XXXRW: For now, PR_ADDR because soreceive will bump into them 41984d61770SRobert Watson * due to our use of sbappendaddr. A new sbappend variants is needed 42084d61770SRobert Watson * that supports both atomic record writes and control data. 42184d61770SRobert Watson */ 42284d61770SRobert Watson .pr_flags = PR_ADDR|PR_ATOMIC|PR_CONNREQUIRED|PR_WANTRCVD| 42384d61770SRobert Watson PR_RIGHTS, 424e0643280SGleb Smirnoff .pr_ctloutput = &uipc_ctloutput, 42584d61770SRobert Watson .pr_usrreqs = &uipc_usrreqs_seqpacket, 42684d61770SRobert Watson }, 427e4445a03SRobert Watson }; 428e4445a03SRobert Watson 429e4445a03SRobert Watson static struct domain localdomain = { 430e4445a03SRobert Watson .dom_family = AF_LOCAL, 431e4445a03SRobert Watson .dom_name = "local", 432e4445a03SRobert Watson .dom_init = unp_init, 433e4445a03SRobert Watson .dom_externalize = unp_externalize, 43499ab95dbSMark Johnston .dom_dispose = unp_dispose, 435e4445a03SRobert Watson .dom_protosw = localsw, 43602abd400SPedro F. Giffuni .dom_protoswNPROTOSW = &localsw[nitems(localsw)] 437e4445a03SRobert Watson }; 438e4445a03SRobert Watson DOMAIN_SET(local); 439e4445a03SRobert Watson 440ac45e92fSRobert Watson static void 441a29f300eSGarrett Wollman uipc_abort(struct socket *so) 442df8bae1dSRodney W. Grimes { 443e7c33e29SRobert Watson struct unpcb *unp, *unp2; 444df8bae1dSRodney W. Grimes 44540f2ac28SRobert Watson unp = sotounpcb(so); 4464d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_abort: unp == NULL")); 44775a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT(unp); 448e7c33e29SRobert Watson 449e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 450e7c33e29SRobert Watson unp2 = unp->unp_conn; 451e7c33e29SRobert Watson if (unp2 != NULL) { 45275a67bf3SMatt Macy unp_pcb_hold(unp2); 453e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 45475a67bf3SMatt Macy unp_drop(unp2); 45575a67bf3SMatt Macy } else 45675a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 457df8bae1dSRodney W. Grimes } 458df8bae1dSRodney W. Grimes 459a29f300eSGarrett Wollman static int 46057bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam) 461a29f300eSGarrett Wollman { 462e7c33e29SRobert Watson struct unpcb *unp, *unp2; 4630d9ce3a1SRobert Watson const struct sockaddr *sa; 464df8bae1dSRodney W. Grimes 465df8bae1dSRodney W. Grimes /* 4661c381b19SRobert Watson * Pass back name of connected socket, if it was bound and we are 4671c381b19SRobert Watson * still connected (our peer may have closed already!). 468df8bae1dSRodney W. Grimes */ 4694d4b555eSRobert Watson unp = sotounpcb(so); 4704d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_accept: unp == NULL")); 471e7c33e29SRobert Watson 4720d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 4733dab55bcSRobert Watson UNP_LINK_RLOCK(); 474e7c33e29SRobert Watson unp2 = unp->unp_conn; 475e7c33e29SRobert Watson if (unp2 != NULL && unp2->unp_addr != NULL) { 476e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 477e7c33e29SRobert Watson sa = (struct sockaddr *) unp2->unp_addr; 478e7c33e29SRobert Watson bcopy(sa, *nam, sa->sa_len); 479e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 480e7c33e29SRobert Watson } else { 4810d9ce3a1SRobert Watson sa = &sun_noname; 4820d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 483e7c33e29SRobert Watson } 4843dab55bcSRobert Watson UNP_LINK_RUNLOCK(); 485e5aeaa0cSDag-Erling Smørgrav return (0); 486a29f300eSGarrett Wollman } 487df8bae1dSRodney W. Grimes 488a29f300eSGarrett Wollman static int 489b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td) 490a29f300eSGarrett Wollman { 491e7c33e29SRobert Watson u_long sendspace, recvspace; 4926d32873cSRobert Watson struct unpcb *unp; 4933dab55bcSRobert Watson int error; 494779f106aSGleb Smirnoff bool locked; 495df8bae1dSRodney W. Grimes 4966d32873cSRobert Watson KASSERT(so->so_pcb == NULL, ("uipc_attach: so_pcb != NULL")); 4976d32873cSRobert Watson if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 4986d32873cSRobert Watson switch (so->so_type) { 4996d32873cSRobert Watson case SOCK_STREAM: 500e7c33e29SRobert Watson sendspace = unpst_sendspace; 501e7c33e29SRobert Watson recvspace = unpst_recvspace; 5026d32873cSRobert Watson break; 5036d32873cSRobert Watson 5046d32873cSRobert Watson case SOCK_DGRAM: 505e7c33e29SRobert Watson sendspace = unpdg_sendspace; 506e7c33e29SRobert Watson recvspace = unpdg_recvspace; 5076d32873cSRobert Watson break; 5086d32873cSRobert Watson 50984d61770SRobert Watson case SOCK_SEQPACKET: 51084d61770SRobert Watson sendspace = unpsp_sendspace; 51184d61770SRobert Watson recvspace = unpsp_recvspace; 51284d61770SRobert Watson break; 51384d61770SRobert Watson 5146d32873cSRobert Watson default: 515e7c33e29SRobert Watson panic("uipc_attach"); 5166d32873cSRobert Watson } 517e7c33e29SRobert Watson error = soreserve(so, sendspace, recvspace); 5186d32873cSRobert Watson if (error) 5196d32873cSRobert Watson return (error); 5206d32873cSRobert Watson } 52146a1d9bfSRobert Watson unp = uma_zalloc(unp_zone, M_NOWAIT | M_ZERO); 5226d32873cSRobert Watson if (unp == NULL) 5236d32873cSRobert Watson return (ENOBUFS); 5246d32873cSRobert Watson LIST_INIT(&unp->unp_refs); 525e7c33e29SRobert Watson UNP_PCB_LOCK_INIT(unp); 5266d32873cSRobert Watson unp->unp_socket = so; 5276d32873cSRobert Watson so->so_pcb = unp; 5289ae328fcSJohn Baldwin unp->unp_refcount = 1; 529e7c33e29SRobert Watson 530779f106aSGleb Smirnoff if ((locked = UNP_LINK_WOWNED()) == false) 531779f106aSGleb Smirnoff UNP_LINK_WLOCK(); 532779f106aSGleb Smirnoff 5336d32873cSRobert Watson unp->unp_gencnt = ++unp_gencnt; 534f218ac50SMateusz Guzik unp->unp_ino = ++unp_ino; 5356d32873cSRobert Watson unp_count++; 53684d61770SRobert Watson switch (so->so_type) { 53784d61770SRobert Watson case SOCK_STREAM: 53884d61770SRobert Watson LIST_INSERT_HEAD(&unp_shead, unp, unp_link); 53984d61770SRobert Watson break; 54084d61770SRobert Watson 54184d61770SRobert Watson case SOCK_DGRAM: 54284d61770SRobert Watson LIST_INSERT_HEAD(&unp_dhead, unp, unp_link); 54384d61770SRobert Watson break; 54484d61770SRobert Watson 54584d61770SRobert Watson case SOCK_SEQPACKET: 54684d61770SRobert Watson LIST_INSERT_HEAD(&unp_sphead, unp, unp_link); 54784d61770SRobert Watson break; 54884d61770SRobert Watson 54984d61770SRobert Watson default: 55084d61770SRobert Watson panic("uipc_attach"); 55184d61770SRobert Watson } 552779f106aSGleb Smirnoff 553779f106aSGleb Smirnoff if (locked == false) 554779f106aSGleb Smirnoff UNP_LINK_WUNLOCK(); 5556d32873cSRobert Watson 5566d32873cSRobert Watson return (0); 557a29f300eSGarrett Wollman } 558a29f300eSGarrett Wollman 559a29f300eSGarrett Wollman static int 5607493f24eSPawel Jakub Dawidek uipc_bindat(int fd, struct socket *so, struct sockaddr *nam, struct thread *td) 561a29f300eSGarrett Wollman { 562dd47f5caSRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam; 563dd47f5caSRobert Watson struct vattr vattr; 5645050aa86SKonstantin Belousov int error, namelen; 565dd47f5caSRobert Watson struct nameidata nd; 56640f2ac28SRobert Watson struct unpcb *unp; 567dd47f5caSRobert Watson struct vnode *vp; 568dd47f5caSRobert Watson struct mount *mp; 5697008be5bSPawel Jakub Dawidek cap_rights_t rights; 570dd47f5caSRobert Watson char *buf; 571a29f300eSGarrett Wollman 572cb7df69bSKevin Lo if (nam->sa_family != AF_UNIX) 573cb7df69bSKevin Lo return (EAFNOSUPPORT); 574cb7df69bSKevin Lo 57540f2ac28SRobert Watson unp = sotounpcb(so); 5764d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_bind: unp == NULL")); 5774f1f0ef5SRobert Watson 578a06534c3SBjoern A. Zeeb if (soun->sun_len > sizeof(struct sockaddr_un)) 579a06534c3SBjoern A. Zeeb return (EINVAL); 5804f1f0ef5SRobert Watson namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 5814f1f0ef5SRobert Watson if (namelen <= 0) 5824f1f0ef5SRobert Watson return (EINVAL); 583dd47f5caSRobert Watson 584dd47f5caSRobert Watson /* 5854f1f0ef5SRobert Watson * We don't allow simultaneous bind() calls on a single UNIX domain 5864f1f0ef5SRobert Watson * socket, so flag in-progress operations, and return an error if an 5874f1f0ef5SRobert Watson * operation is already in progress. 5884f1f0ef5SRobert Watson * 5894f1f0ef5SRobert Watson * Historically, we have not allowed a socket to be rebound, so this 590d7924b70SRobert Watson * also returns an error. Not allowing re-binding simplifies the 591d7924b70SRobert Watson * implementation and avoids a great many possible failure modes. 592dd47f5caSRobert Watson */ 593e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 594dd47f5caSRobert Watson if (unp->unp_vnode != NULL) { 595e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 596dd47f5caSRobert Watson return (EINVAL); 597dd47f5caSRobert Watson } 5984f1f0ef5SRobert Watson if (unp->unp_flags & UNP_BINDING) { 599e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 6004f1f0ef5SRobert Watson return (EALREADY); 601dd47f5caSRobert Watson } 6024f1f0ef5SRobert Watson unp->unp_flags |= UNP_BINDING; 603e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 604dd47f5caSRobert Watson 605dd47f5caSRobert Watson buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 6067928893dSEd Maste bcopy(soun->sun_path, buf, namelen); 6077928893dSEd Maste buf[namelen] = 0; 608dd47f5caSRobert Watson 609dd47f5caSRobert Watson restart: 6106c21f6edSKonstantin Belousov NDINIT_ATRIGHTS(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME | NOCACHE, 6117008be5bSPawel Jakub Dawidek UIO_SYSSPACE, buf, fd, cap_rights_init(&rights, CAP_BINDAT), td); 612dd47f5caSRobert Watson /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 613dd47f5caSRobert Watson error = namei(&nd); 614dd47f5caSRobert Watson if (error) 6154f1f0ef5SRobert Watson goto error; 616dd47f5caSRobert Watson vp = nd.ni_vp; 617dd47f5caSRobert Watson if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 618dd47f5caSRobert Watson NDFREE(&nd, NDF_ONLY_PNBUF); 619dd47f5caSRobert Watson if (nd.ni_dvp == vp) 620dd47f5caSRobert Watson vrele(nd.ni_dvp); 621dd47f5caSRobert Watson else 622dd47f5caSRobert Watson vput(nd.ni_dvp); 623dd47f5caSRobert Watson if (vp != NULL) { 624dd47f5caSRobert Watson vrele(vp); 625dd47f5caSRobert Watson error = EADDRINUSE; 6264f1f0ef5SRobert Watson goto error; 627dd47f5caSRobert Watson } 628dd47f5caSRobert Watson error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 629dd47f5caSRobert Watson if (error) 6304f1f0ef5SRobert Watson goto error; 631dd47f5caSRobert Watson goto restart; 632dd47f5caSRobert Watson } 633dd47f5caSRobert Watson VATTR_NULL(&vattr); 634dd47f5caSRobert Watson vattr.va_type = VSOCK; 635dd47f5caSRobert Watson vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 636dd47f5caSRobert Watson #ifdef MAC 63730d239bcSRobert Watson error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 638dd47f5caSRobert Watson &vattr); 639dd47f5caSRobert Watson #endif 640885868cdSRobert Watson if (error == 0) 641dd47f5caSRobert Watson error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 642dd47f5caSRobert Watson NDFREE(&nd, NDF_ONLY_PNBUF); 643dd47f5caSRobert Watson vput(nd.ni_dvp); 644dd47f5caSRobert Watson if (error) { 645dd47f5caSRobert Watson vn_finished_write(mp); 6464f1f0ef5SRobert Watson goto error; 647dd47f5caSRobert Watson } 648dd47f5caSRobert Watson vp = nd.ni_vp; 64957fd3d55SPawel Jakub Dawidek ASSERT_VOP_ELOCKED(vp, "uipc_bind"); 650dd47f5caSRobert Watson soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 651e7c33e29SRobert Watson 652e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 6530c3c207fSGleb Smirnoff VOP_UNP_BIND(vp, unp); 654dd47f5caSRobert Watson unp->unp_vnode = vp; 655dd47f5caSRobert Watson unp->unp_addr = soun; 6564f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_BINDING; 657e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 658b249ce48SMateusz Guzik VOP_UNLOCK(vp); 659dd47f5caSRobert Watson vn_finished_write(mp); 6604f1f0ef5SRobert Watson free(buf, M_TEMP); 6614f1f0ef5SRobert Watson return (0); 662e7c33e29SRobert Watson 6634f1f0ef5SRobert Watson error: 664e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 6654f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_BINDING; 666e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 667dd47f5caSRobert Watson free(buf, M_TEMP); 66840f2ac28SRobert Watson return (error); 669a29f300eSGarrett Wollman } 670a29f300eSGarrett Wollman 671a29f300eSGarrett Wollman static int 6727493f24eSPawel Jakub Dawidek uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 6737493f24eSPawel Jakub Dawidek { 6747493f24eSPawel Jakub Dawidek 6757493f24eSPawel Jakub Dawidek return (uipc_bindat(AT_FDCWD, so, nam, td)); 6767493f24eSPawel Jakub Dawidek } 6777493f24eSPawel Jakub Dawidek 6787493f24eSPawel Jakub Dawidek static int 679b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 680a29f300eSGarrett Wollman { 6810d9ce3a1SRobert Watson int error; 682a29f300eSGarrett Wollman 683fd179ee9SRobert Watson KASSERT(td == curthread, ("uipc_connect: td != curthread")); 684fd179ee9SRobert Watson error = unp_connect(so, nam, td); 6850d9ce3a1SRobert Watson return (error); 686a29f300eSGarrett Wollman } 687a29f300eSGarrett Wollman 6887493f24eSPawel Jakub Dawidek static int 6897493f24eSPawel Jakub Dawidek uipc_connectat(int fd, struct socket *so, struct sockaddr *nam, 6907493f24eSPawel Jakub Dawidek struct thread *td) 6917493f24eSPawel Jakub Dawidek { 6927493f24eSPawel Jakub Dawidek int error; 6937493f24eSPawel Jakub Dawidek 6947493f24eSPawel Jakub Dawidek KASSERT(td == curthread, ("uipc_connectat: td != curthread")); 6957493f24eSPawel Jakub Dawidek error = unp_connectat(fd, so, nam, td); 6967493f24eSPawel Jakub Dawidek return (error); 6977493f24eSPawel Jakub Dawidek } 6987493f24eSPawel Jakub Dawidek 699a152f8a3SRobert Watson static void 700a152f8a3SRobert Watson uipc_close(struct socket *so) 701a152f8a3SRobert Watson { 702e7c33e29SRobert Watson struct unpcb *unp, *unp2; 703779f106aSGleb Smirnoff struct vnode *vp = NULL; 70475a67bf3SMatt Macy struct mtx *vplock; 70575a67bf3SMatt Macy int freed; 706a152f8a3SRobert Watson unp = sotounpcb(so); 707a152f8a3SRobert Watson KASSERT(unp != NULL, ("uipc_close: unp == NULL")); 708e7c33e29SRobert Watson 70975a67bf3SMatt Macy vplock = NULL; 71075a67bf3SMatt Macy if ((vp = unp->unp_vnode) != NULL) { 71175a67bf3SMatt Macy vplock = mtx_pool_find(mtxpool_sleep, vp); 71275a67bf3SMatt Macy mtx_lock(vplock); 713e7c33e29SRobert Watson } 71475a67bf3SMatt Macy UNP_PCB_LOCK(unp); 71575a67bf3SMatt Macy if (vp && unp->unp_vnode == NULL) { 71675a67bf3SMatt Macy mtx_unlock(vplock); 71775a67bf3SMatt Macy vp = NULL; 71875a67bf3SMatt Macy } 71975a67bf3SMatt Macy if (vp != NULL) { 720779f106aSGleb Smirnoff VOP_UNP_DETACH(vp); 721779f106aSGleb Smirnoff unp->unp_vnode = NULL; 722779f106aSGleb Smirnoff } 72375a67bf3SMatt Macy unp2 = unp->unp_conn; 72475a67bf3SMatt Macy unp_pcb_hold(unp); 725acf9fd05SMatt Macy if (__predict_false(unp == unp2)) { 726acf9fd05SMatt Macy unp_disconnect(unp, unp2); 727acf9fd05SMatt Macy } else if (unp2 != NULL) { 72875a67bf3SMatt Macy unp_pcb_hold(unp2); 72975a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freed); 73075a67bf3SMatt Macy unp_disconnect(unp, unp2); 73175a67bf3SMatt Macy if (unp_pcb_rele(unp2) == 0) 73275a67bf3SMatt Macy UNP_PCB_UNLOCK(unp2); 73375a67bf3SMatt Macy } 73475a67bf3SMatt Macy if (unp_pcb_rele(unp) == 0) 735e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 73675a67bf3SMatt Macy if (vp) { 73775a67bf3SMatt Macy mtx_unlock(vplock); 738779f106aSGleb Smirnoff vrele(vp); 739a152f8a3SRobert Watson } 74075a67bf3SMatt Macy } 741a152f8a3SRobert Watson 7422c899584SRobert Watson static int 743a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2) 744a29f300eSGarrett Wollman { 745e7c33e29SRobert Watson struct unpcb *unp, *unp2; 7460d9ce3a1SRobert Watson int error; 747a29f300eSGarrett Wollman 748e7c33e29SRobert Watson unp = so1->so_pcb; 7494d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_connect2: unp == NULL")); 750e7c33e29SRobert Watson unp2 = so2->so_pcb; 751e7c33e29SRobert Watson KASSERT(unp2 != NULL, ("uipc_connect2: unp2 == NULL")); 752acf9fd05SMatt Macy if (unp != unp2) 75375a67bf3SMatt Macy unp_pcb_lock2(unp, unp2); 754acf9fd05SMatt Macy else 755acf9fd05SMatt Macy UNP_PCB_LOCK(unp); 7566a2989fdSMatthew N. Dodd error = unp_connect2(so1, so2, PRU_CONNECT2); 757acf9fd05SMatt Macy if (unp != unp2) 758e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 759e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 7600d9ce3a1SRobert Watson return (error); 761a29f300eSGarrett Wollman } 762a29f300eSGarrett Wollman 763bc725eafSRobert Watson static void 764a29f300eSGarrett Wollman uipc_detach(struct socket *so) 765a29f300eSGarrett Wollman { 766e7c33e29SRobert Watson struct unpcb *unp, *unp2; 76775a67bf3SMatt Macy struct mtx *vplock; 7686d32873cSRobert Watson struct vnode *vp; 7699ae328fcSJohn Baldwin int freeunp, local_unp_rights; 770a29f300eSGarrett Wollman 77140f2ac28SRobert Watson unp = sotounpcb(so); 7724d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_detach: unp == NULL")); 773e7c33e29SRobert Watson 774434ac8b6SMark Johnston vp = NULL; 775c0874c34SMatt Macy vplock = NULL; 776434ac8b6SMark Johnston local_unp_rights = 0; 777434ac8b6SMark Johnston 778779f106aSGleb Smirnoff UNP_LINK_WLOCK(); 7796d32873cSRobert Watson LIST_REMOVE(unp, unp_link); 780a9aa06f7SJason A. Harmening if (unp->unp_gcflag & UNPGC_DEAD) 781a9aa06f7SJason A. Harmening LIST_REMOVE(unp, unp_dead); 7826d32873cSRobert Watson unp->unp_gencnt = ++unp_gencnt; 7836d32873cSRobert Watson --unp_count; 78475a67bf3SMatt Macy UNP_LINK_WUNLOCK(); 785434ac8b6SMark Johnston 78675a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT(unp); 78775a67bf3SMatt Macy restart: 78875a67bf3SMatt Macy if ((vp = unp->unp_vnode) != NULL) { 78975a67bf3SMatt Macy vplock = mtx_pool_find(mtxpool_sleep, vp); 79075a67bf3SMatt Macy mtx_lock(vplock); 79175a67bf3SMatt Macy } 79275a67bf3SMatt Macy UNP_PCB_LOCK(unp); 793*db38b699SMark Johnston if (unp->unp_vnode != vp && unp->unp_vnode != NULL) { 794c0874c34SMatt Macy if (vplock) 79575a67bf3SMatt Macy mtx_unlock(vplock); 79675a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 79775a67bf3SMatt Macy goto restart; 79875a67bf3SMatt Macy } 7996d32873cSRobert Watson if ((vp = unp->unp_vnode) != NULL) { 800c7e41c8bSMikolaj Golub VOP_UNP_DETACH(vp); 8016d32873cSRobert Watson unp->unp_vnode = NULL; 8026d32873cSRobert Watson } 803acf9fd05SMatt Macy if (__predict_false(unp == unp->unp_conn)) { 804acf9fd05SMatt Macy unp_disconnect(unp, unp); 805acf9fd05SMatt Macy unp2 = NULL; 806*db38b699SMark Johnston } else { 807acf9fd05SMatt Macy if ((unp2 = unp->unp_conn) != NULL) { 808acf9fd05SMatt Macy unp_pcb_owned_lock2(unp, unp2, freeunp); 809acf9fd05SMatt Macy if (freeunp) 810acf9fd05SMatt Macy unp2 = NULL; 811acf9fd05SMatt Macy } 81275a67bf3SMatt Macy unp_pcb_hold(unp); 813e7c33e29SRobert Watson if (unp2 != NULL) { 81475a67bf3SMatt Macy unp_pcb_hold(unp2); 815e7c33e29SRobert Watson unp_disconnect(unp, unp2); 81675a67bf3SMatt Macy if (unp_pcb_rele(unp2) == 0) 817e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 818e7c33e29SRobert Watson } 819*db38b699SMark Johnston } 82075a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 82175a67bf3SMatt Macy UNP_REF_LIST_LOCK(); 8226d32873cSRobert Watson while (!LIST_EMPTY(&unp->unp_refs)) { 8236d32873cSRobert Watson struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 824e7c33e29SRobert Watson 82575a67bf3SMatt Macy unp_pcb_hold(ref); 82675a67bf3SMatt Macy UNP_REF_LIST_UNLOCK(); 82775a67bf3SMatt Macy 82875a67bf3SMatt Macy MPASS(ref != unp); 82975a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT(ref); 830afc055d9SEd Schouten unp_drop(ref); 83175a67bf3SMatt Macy UNP_REF_LIST_LOCK(); 8326d32873cSRobert Watson } 83375a67bf3SMatt Macy 83475a67bf3SMatt Macy UNP_REF_LIST_UNLOCK(); 83575a67bf3SMatt Macy UNP_PCB_LOCK(unp); 83675a67bf3SMatt Macy freeunp = unp_pcb_rele(unp); 83775a67bf3SMatt Macy MPASS(freeunp == 0); 838397c19d1SJeff Roberson local_unp_rights = unp_rights; 8396d32873cSRobert Watson unp->unp_socket->so_pcb = NULL; 84075a67bf3SMatt Macy unp->unp_socket = NULL; 841*db38b699SMark Johnston free(unp->unp_addr, M_SONAME); 842*db38b699SMark Johnston unp->unp_addr = NULL; 843*db38b699SMark Johnston if (!unp_pcb_rele(unp)) 8446e2faa24SRobert Watson UNP_PCB_UNLOCK(unp); 84575a67bf3SMatt Macy if (vp) { 84675a67bf3SMatt Macy mtx_unlock(vplock); 8476d32873cSRobert Watson vrele(vp); 84875a67bf3SMatt Macy } 8496d32873cSRobert Watson if (local_unp_rights) 850daee0f0bSKonstantin Belousov taskqueue_enqueue_timeout(taskqueue_thread, &unp_gc_task, -1); 851a29f300eSGarrett Wollman } 852a29f300eSGarrett Wollman 853a29f300eSGarrett Wollman static int 854a29f300eSGarrett Wollman uipc_disconnect(struct socket *so) 855a29f300eSGarrett Wollman { 856e7c33e29SRobert Watson struct unpcb *unp, *unp2; 85775a67bf3SMatt Macy int freed; 858a29f300eSGarrett Wollman 85940f2ac28SRobert Watson unp = sotounpcb(so); 8604d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_disconnect: unp == NULL")); 861e7c33e29SRobert Watson 862e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 86375a67bf3SMatt Macy if ((unp2 = unp->unp_conn) == NULL) { 864e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 86575a67bf3SMatt Macy return (0); 86675a67bf3SMatt Macy } 867fcabd541SMatt Macy if (__predict_true(unp != unp2)) { 86875a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freed); 86975a67bf3SMatt Macy if (__predict_false(freed)) { 87075a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 87175a67bf3SMatt Macy return (0); 87275a67bf3SMatt Macy } 87375a67bf3SMatt Macy unp_pcb_hold(unp2); 874fcabd541SMatt Macy } 87575a67bf3SMatt Macy unp_pcb_hold(unp); 87675a67bf3SMatt Macy unp_disconnect(unp, unp2); 87775a67bf3SMatt Macy if (unp_pcb_rele(unp) == 0) 87875a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 879fcabd541SMatt Macy if ((unp != unp2) && unp_pcb_rele(unp2) == 0) 88075a67bf3SMatt Macy UNP_PCB_UNLOCK(unp2); 881e5aeaa0cSDag-Erling Smørgrav return (0); 882a29f300eSGarrett Wollman } 883a29f300eSGarrett Wollman 884a29f300eSGarrett Wollman static int 885d374e81eSRobert Watson uipc_listen(struct socket *so, int backlog, struct thread *td) 886a29f300eSGarrett Wollman { 88740f2ac28SRobert Watson struct unpcb *unp; 8880d9ce3a1SRobert Watson int error; 889a29f300eSGarrett Wollman 890beb4b312SGleb Smirnoff if (so->so_type != SOCK_STREAM && so->so_type != SOCK_SEQPACKET) 891beb4b312SGleb Smirnoff return (EOPNOTSUPP); 892beb4b312SGleb Smirnoff 89340f2ac28SRobert Watson unp = sotounpcb(so); 8944d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_listen: unp == NULL")); 895e7c33e29SRobert Watson 896e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 8974d4b555eSRobert Watson if (unp->unp_vnode == NULL) { 89847a84387SEd Schouten /* Already connected or not bound to an address. */ 89947a84387SEd Schouten error = unp->unp_conn != NULL ? EINVAL : EDESTADDRREQ; 900e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 90147a84387SEd Schouten return (error); 90240f2ac28SRobert Watson } 903e7c33e29SRobert Watson 904e7c33e29SRobert Watson SOCK_LOCK(so); 905e7c33e29SRobert Watson error = solisten_proto_check(so); 906e7c33e29SRobert Watson if (error == 0) { 907c5afec6eSDmitry Chagin cru2xt(td, &unp->unp_peercred); 908e7c33e29SRobert Watson solisten_proto(so, backlog); 909e7c33e29SRobert Watson } 910e7c33e29SRobert Watson SOCK_UNLOCK(so); 911e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 9120d9ce3a1SRobert Watson return (error); 913a29f300eSGarrett Wollman } 914a29f300eSGarrett Wollman 915a29f300eSGarrett Wollman static int 91657bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 917a29f300eSGarrett Wollman { 918e7c33e29SRobert Watson struct unpcb *unp, *unp2; 9190d9ce3a1SRobert Watson const struct sockaddr *sa; 920a29f300eSGarrett Wollman 9214d4b555eSRobert Watson unp = sotounpcb(so); 9224d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL")); 923e7c33e29SRobert Watson 9240d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 925afd9f91cSJohn Baldwin UNP_LINK_RLOCK(); 926bdc5f6a3SHajimu UMEMOTO /* 927e7c33e29SRobert Watson * XXX: It seems that this test always fails even when connection is 928e7c33e29SRobert Watson * established. So, this else clause is added as workaround to 929e7c33e29SRobert Watson * return PF_LOCAL sockaddr. 930bdc5f6a3SHajimu UMEMOTO */ 931e7c33e29SRobert Watson unp2 = unp->unp_conn; 932e7c33e29SRobert Watson if (unp2 != NULL) { 933e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 934e7c33e29SRobert Watson if (unp2->unp_addr != NULL) 935afd9f91cSJohn Baldwin sa = (struct sockaddr *) unp2->unp_addr; 936e7c33e29SRobert Watson else 9370d9ce3a1SRobert Watson sa = &sun_noname; 9380d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 939e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 940e7c33e29SRobert Watson } else { 941e7c33e29SRobert Watson sa = &sun_noname; 942e7c33e29SRobert Watson bcopy(sa, *nam, sa->sa_len); 943e7c33e29SRobert Watson } 944afd9f91cSJohn Baldwin UNP_LINK_RUNLOCK(); 945e5aeaa0cSDag-Erling Smørgrav return (0); 946a29f300eSGarrett Wollman } 947a29f300eSGarrett Wollman 948a29f300eSGarrett Wollman static int 949a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 950a29f300eSGarrett Wollman { 951e7c33e29SRobert Watson struct unpcb *unp, *unp2; 952a29f300eSGarrett Wollman struct socket *so2; 953337cc6b6SRobert Watson u_int mbcnt, sbcc; 954a29f300eSGarrett Wollman 95540f2ac28SRobert Watson unp = sotounpcb(so); 9562b21d0e8SGleb Smirnoff KASSERT(unp != NULL, ("%s: unp == NULL", __func__)); 9572b21d0e8SGleb Smirnoff KASSERT(so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET, 9582b21d0e8SGleb Smirnoff ("%s: socktype %d", __func__, so->so_type)); 959e7c33e29SRobert Watson 960df8bae1dSRodney W. Grimes /* 961e7c33e29SRobert Watson * Adjust backpressure on sender and wakeup any waiting to write. 962e7c33e29SRobert Watson * 963d7924b70SRobert Watson * The unp lock is acquired to maintain the validity of the unp_conn 964d7924b70SRobert Watson * pointer; no lock on unp2 is required as unp2->unp_socket will be 965d7924b70SRobert Watson * static as long as we don't permit unp2 to disconnect from unp, 966d7924b70SRobert Watson * which is prevented by the lock on unp. We cache values from 967d7924b70SRobert Watson * so_rcv to avoid holding the so_rcv lock over the entire 968d7924b70SRobert Watson * transaction on the remote so_snd. 969df8bae1dSRodney W. Grimes */ 970337cc6b6SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 971337cc6b6SRobert Watson mbcnt = so->so_rcv.sb_mbcnt; 9722b21d0e8SGleb Smirnoff sbcc = sbavail(&so->so_rcv); 973337cc6b6SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 974c2090e73SAlan Somers /* 975c2090e73SAlan Somers * There is a benign race condition at this point. If we're planning to 976c2090e73SAlan Somers * clear SB_STOP, but uipc_send is called on the connected socket at 977c2090e73SAlan Somers * this instant, it might add data to the sockbuf and set SB_STOP. Then 978c2090e73SAlan Somers * we would erroneously clear SB_STOP below, even though the sockbuf is 979c2090e73SAlan Somers * full. The race is benign because the only ill effect is to allow the 980c2090e73SAlan Somers * sockbuf to exceed its size limit, and the size limits are not 981c2090e73SAlan Somers * strictly guaranteed anyway. 982c2090e73SAlan Somers */ 983e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 984e7c33e29SRobert Watson unp2 = unp->unp_conn; 985e7c33e29SRobert Watson if (unp2 == NULL) { 986e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 987e7c33e29SRobert Watson return (0); 988337cc6b6SRobert Watson } 989e7c33e29SRobert Watson so2 = unp2->unp_socket; 990337cc6b6SRobert Watson SOCKBUF_LOCK(&so2->so_snd); 991c2090e73SAlan Somers if (sbcc < so2->so_snd.sb_hiwat && mbcnt < so2->so_snd.sb_mbmax) 992c2090e73SAlan Somers so2->so_snd.sb_flags &= ~SB_STOP; 9931e4d7da7SRobert Watson sowwakeup_locked(so2); 994e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 995e5aeaa0cSDag-Erling Smørgrav return (0); 996a29f300eSGarrett Wollman } 997df8bae1dSRodney W. Grimes 998a29f300eSGarrett Wollman static int 99975a67bf3SMatt Macy connect_internal(struct socket *so, struct sockaddr *nam, struct thread *td) 100075a67bf3SMatt Macy { 100175a67bf3SMatt Macy int error; 100275a67bf3SMatt Macy struct unpcb *unp; 100375a67bf3SMatt Macy 100475a67bf3SMatt Macy unp = so->so_pcb; 100575a67bf3SMatt Macy if (unp->unp_conn != NULL) 100675a67bf3SMatt Macy return (EISCONN); 100775a67bf3SMatt Macy error = unp_connect(so, nam, td); 100875a67bf3SMatt Macy if (error) 100975a67bf3SMatt Macy return (error); 101075a67bf3SMatt Macy UNP_PCB_LOCK(unp); 101175a67bf3SMatt Macy if (unp->unp_conn == NULL) { 101275a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 101375a67bf3SMatt Macy if (error == 0) 101475a67bf3SMatt Macy error = ENOTCONN; 101575a67bf3SMatt Macy } 101675a67bf3SMatt Macy return (error); 101775a67bf3SMatt Macy } 101875a67bf3SMatt Macy 101975a67bf3SMatt Macy static int 102057bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 1021b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 1022a29f300eSGarrett Wollman { 1023f3f49bbbSRobert Watson struct unpcb *unp, *unp2; 1024a29f300eSGarrett Wollman struct socket *so2; 1025c2090e73SAlan Somers u_int mbcnt, sbcc; 102675a67bf3SMatt Macy int freed, error; 1027a29f300eSGarrett Wollman 102840f2ac28SRobert Watson unp = sotounpcb(so); 10292b21d0e8SGleb Smirnoff KASSERT(unp != NULL, ("%s: unp == NULL", __func__)); 10302b21d0e8SGleb Smirnoff KASSERT(so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM || 10312b21d0e8SGleb Smirnoff so->so_type == SOCK_SEQPACKET, 10322b21d0e8SGleb Smirnoff ("%s: socktype %d", __func__, so->so_type)); 1033e7c33e29SRobert Watson 103475a67bf3SMatt Macy freed = error = 0; 1035a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 1036a29f300eSGarrett Wollman error = EOPNOTSUPP; 1037a29f300eSGarrett Wollman goto release; 1038a29f300eSGarrett Wollman } 1039fc3fcacfSRobert Watson if (control != NULL && (error = unp_internalize(&control, td))) 1040a29f300eSGarrett Wollman goto release; 104175a67bf3SMatt Macy 104275a67bf3SMatt Macy unp2 = NULL; 1043a29f300eSGarrett Wollman switch (so->so_type) { 1044a29f300eSGarrett Wollman case SOCK_DGRAM: 1045a29f300eSGarrett Wollman { 1046e7dd9a10SRobert Watson const struct sockaddr *from; 1047df8bae1dSRodney W. Grimes 1048fc3fcacfSRobert Watson if (nam != NULL) { 104975a67bf3SMatt Macy /* 105075a67bf3SMatt Macy * We return with UNP_PCB_LOCK_HELD so we know that 105175a67bf3SMatt Macy * the reference is live if the pointer is valid. 105275a67bf3SMatt Macy */ 105375a67bf3SMatt Macy if ((error = connect_internal(so, nam, td))) 1054df8bae1dSRodney W. Grimes break; 105575a67bf3SMatt Macy MPASS(unp->unp_conn != NULL); 1056e7c33e29SRobert Watson unp2 = unp->unp_conn; 105775a67bf3SMatt Macy } else { 105875a67bf3SMatt Macy UNP_PCB_LOCK(unp); 105960a5ef26SRobert Watson 1060b5ff0914SRobert Watson /* 1061b5ff0914SRobert Watson * Because connect() and send() are non-atomic in a sendto() 1062b5ff0914SRobert Watson * with a target address, it's possible that the socket will 1063b5ff0914SRobert Watson * have disconnected before the send() can run. In that case 1064b5ff0914SRobert Watson * return the slightly counter-intuitive but otherwise 1065b5ff0914SRobert Watson * correct error that the socket is not connected. 1066b5ff0914SRobert Watson */ 106775a67bf3SMatt Macy if ((unp2 = unp->unp_conn) == NULL) { 106875a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 1069b5ff0914SRobert Watson error = ENOTCONN; 1070b5ff0914SRobert Watson break; 1071b5ff0914SRobert Watson } 107275a67bf3SMatt Macy } 1073c684c14cSMatt Macy if (__predict_false(unp == unp2)) { 1074c684c14cSMatt Macy if (unp->unp_socket == NULL) { 1075c684c14cSMatt Macy error = ENOTCONN; 1076c684c14cSMatt Macy break; 1077c684c14cSMatt Macy } 1078c684c14cSMatt Macy goto connect_self; 1079c684c14cSMatt Macy } 108075a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freed); 108175a67bf3SMatt Macy if (__predict_false(freed)) { 108275a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 108375a67bf3SMatt Macy error = ENOTCONN; 108475a67bf3SMatt Macy break; 108575a67bf3SMatt Macy } 108675a67bf3SMatt Macy /* 108775a67bf3SMatt Macy * The socket referencing unp2 may have been closed 108875a67bf3SMatt Macy * or unp may have been disconnected if the unp lock 108975a67bf3SMatt Macy * was dropped to acquire unp2. 109075a67bf3SMatt Macy */ 109175a67bf3SMatt Macy if (__predict_false(unp->unp_conn == NULL) || 109275a67bf3SMatt Macy unp2->unp_socket == NULL) { 109375a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 109475a67bf3SMatt Macy if (unp_pcb_rele(unp2) == 0) 109575a67bf3SMatt Macy UNP_PCB_UNLOCK(unp2); 109675a67bf3SMatt Macy error = ENOTCONN; 109775a67bf3SMatt Macy break; 109875a67bf3SMatt Macy } 1099c684c14cSMatt Macy connect_self: 1100ede6e136SRobert Watson if (unp2->unp_flags & UNP_WANTCRED) 1101ede6e136SRobert Watson control = unp_addsockcred(td, control); 1102fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 110357bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 1104df8bae1dSRodney W. Grimes else 1105df8bae1dSRodney W. Grimes from = &sun_noname; 1106ede6e136SRobert Watson so2 = unp2->unp_socket; 1107a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 11086dde7ecbSPeter Wemm if (sbappendaddr_locked(&so2->so_rcv, from, m, 11098de34a88SAlan Somers control)) { 11101e4d7da7SRobert Watson sorwakeup_locked(so2); 1111fc3fcacfSRobert Watson m = NULL; 1112fc3fcacfSRobert Watson control = NULL; 1113e5aeaa0cSDag-Erling Smørgrav } else { 1114a34b7046SRobert Watson SOCKBUF_UNLOCK(&so2->so_rcv); 1115df8bae1dSRodney W. Grimes error = ENOBUFS; 1116e5aeaa0cSDag-Erling Smørgrav } 111775a67bf3SMatt Macy if (nam != NULL) 1118e7c33e29SRobert Watson unp_disconnect(unp, unp2); 1119c684c14cSMatt Macy if (__predict_true(unp != unp2)) 1120e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1121ede6e136SRobert Watson UNP_PCB_UNLOCK(unp); 1122df8bae1dSRodney W. Grimes break; 1123df8bae1dSRodney W. Grimes } 1124df8bae1dSRodney W. Grimes 112584d61770SRobert Watson case SOCK_SEQPACKET: 1126df8bae1dSRodney W. Grimes case SOCK_STREAM: 1127402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 1128fc3fcacfSRobert Watson if (nam != NULL) { 112975a67bf3SMatt Macy if ((error = connect_internal(so, nam, td))) 113075a67bf3SMatt Macy break; 1131402cc72dSDavid Greenman } else { 1132402cc72dSDavid Greenman error = ENOTCONN; 1133402cc72dSDavid Greenman break; 1134402cc72dSDavid Greenman } 113575a67bf3SMatt Macy } else if ((unp2 = unp->unp_conn) == NULL) { 113675a67bf3SMatt Macy error = ENOTCONN; 113775a67bf3SMatt Macy break; 113875a67bf3SMatt Macy } else if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 1139df8bae1dSRodney W. Grimes error = EPIPE; 1140df8bae1dSRodney W. Grimes break; 114175a67bf3SMatt Macy } else { 114275a67bf3SMatt Macy UNP_PCB_LOCK(unp); 114375a67bf3SMatt Macy if ((unp2 = unp->unp_conn) == NULL) { 114475a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 1145b5ff0914SRobert Watson error = ENOTCONN; 1146b5ff0914SRobert Watson break; 1147b5ff0914SRobert Watson } 114875a67bf3SMatt Macy } 114975a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freed); 115075a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 115175a67bf3SMatt Macy if (__predict_false(freed)) { 115275a67bf3SMatt Macy error = ENOTCONN; 115375a67bf3SMatt Macy break; 115475a67bf3SMatt Macy } 115575a67bf3SMatt Macy if ((so2 = unp2->unp_socket) == NULL) { 115675a67bf3SMatt Macy UNP_PCB_UNLOCK(unp2); 115775a67bf3SMatt Macy error = ENOTCONN; 115875a67bf3SMatt Macy break; 115975a67bf3SMatt Macy } 1160a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 1161f3f49bbbSRobert Watson if (unp2->unp_flags & UNP_WANTCRED) { 11626a2989fdSMatthew N. Dodd /* 1163716963cbSGleb Smirnoff * Credentials are passed only once on SOCK_STREAM 1164716963cbSGleb Smirnoff * and SOCK_SEQPACKET. 11656a2989fdSMatthew N. Dodd */ 1166f3f49bbbSRobert Watson unp2->unp_flags &= ~UNP_WANTCRED; 11676a2989fdSMatthew N. Dodd control = unp_addsockcred(td, control); 11686a2989fdSMatthew N. Dodd } 11695b0480f2SMark Johnston 1170df8bae1dSRodney W. Grimes /* 11715b0480f2SMark Johnston * Send to paired receive port and wake up readers. Don't 11725b0480f2SMark Johnston * check for space available in the receive buffer if we're 11735b0480f2SMark Johnston * attaching ancillary data; Unix domain sockets only check 11745b0480f2SMark Johnston * for space in the sending sockbuf, and that check is 11755b0480f2SMark Johnston * performed one level up the stack. At that level we cannot 11765b0480f2SMark Johnston * precisely account for the amount of buffer space used 11775b0480f2SMark Johnston * (e.g., because control messages are not yet internalized). 1178df8bae1dSRodney W. Grimes */ 117984d61770SRobert Watson switch (so->so_type) { 118084d61770SRobert Watson case SOCK_STREAM: 1181fc3fcacfSRobert Watson if (control != NULL) { 11825b0480f2SMark Johnston sbappendcontrol_locked(&so2->so_rcv, m, 11835b0480f2SMark Johnston control); 1184fc3fcacfSRobert Watson control = NULL; 1185e7c33e29SRobert Watson } else 1186829fae90SGleb Smirnoff sbappend_locked(&so2->so_rcv, m, flags); 118784d61770SRobert Watson break; 118884d61770SRobert Watson 118984d61770SRobert Watson case SOCK_SEQPACKET: { 119084d61770SRobert Watson const struct sockaddr *from; 119184d61770SRobert Watson 119284d61770SRobert Watson from = &sun_noname; 11938de34a88SAlan Somers if (sbappendaddr_nospacecheck_locked(&so2->so_rcv, 11948de34a88SAlan Somers from, m, control)) 119584d61770SRobert Watson control = NULL; 119684d61770SRobert Watson break; 119784d61770SRobert Watson } 119884d61770SRobert Watson } 119984d61770SRobert Watson 1200c2090e73SAlan Somers mbcnt = so2->so_rcv.sb_mbcnt; 12012b21d0e8SGleb Smirnoff sbcc = sbavail(&so2->so_rcv); 12022b21d0e8SGleb Smirnoff if (sbcc) 1203337cc6b6SRobert Watson sorwakeup_locked(so2); 12042b21d0e8SGleb Smirnoff else 12052b21d0e8SGleb Smirnoff SOCKBUF_UNLOCK(&so2->so_rcv); 1206337cc6b6SRobert Watson 1207c2090e73SAlan Somers /* 1208c2090e73SAlan Somers * The PCB lock on unp2 protects the SB_STOP flag. Without it, 1209c2090e73SAlan Somers * it would be possible for uipc_rcvd to be called at this 1210c2090e73SAlan Somers * point, drain the receiving sockbuf, clear SB_STOP, and then 1211c2090e73SAlan Somers * we would set SB_STOP below. That could lead to an empty 1212c2090e73SAlan Somers * sockbuf having SB_STOP set 1213c2090e73SAlan Somers */ 1214337cc6b6SRobert Watson SOCKBUF_LOCK(&so->so_snd); 1215c2090e73SAlan Somers if (sbcc >= so->so_snd.sb_hiwat || mbcnt >= so->so_snd.sb_mbmax) 1216c2090e73SAlan Somers so->so_snd.sb_flags |= SB_STOP; 12177abe2ac2SAlan Cox SOCKBUF_UNLOCK(&so->so_snd); 1218e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1219fc3fcacfSRobert Watson m = NULL; 1220df8bae1dSRodney W. Grimes break; 1221df8bae1dSRodney W. Grimes } 1222a29f300eSGarrett Wollman 12236b8fda4dSGarrett Wollman /* 122460a5ef26SRobert Watson * PRUS_EOF is equivalent to pru_send followed by pru_shutdown. 12256b8fda4dSGarrett Wollman */ 1226a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 1227ede6e136SRobert Watson UNP_PCB_LOCK(unp); 12286b8fda4dSGarrett Wollman socantsendmore(so); 12296b8fda4dSGarrett Wollman unp_shutdown(unp); 1230e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1231ede6e136SRobert Watson } 1232fc3fcacfSRobert Watson if (control != NULL && error != 0) 123399ab95dbSMark Johnston unp_dispose_mbuf(control); 1234bd508d39SDon Lewis 1235a29f300eSGarrett Wollman release: 1236fc3fcacfSRobert Watson if (control != NULL) 1237a29f300eSGarrett Wollman m_freem(control); 1238100db364SGleb Smirnoff /* 1239100db364SGleb Smirnoff * In case of PRUS_NOTREADY, uipc_ready() is responsible 1240100db364SGleb Smirnoff * for freeing memory. 1241100db364SGleb Smirnoff */ 1242100db364SGleb Smirnoff if (m != NULL && (flags & PRUS_NOTREADY) == 0) 1243a29f300eSGarrett Wollman m_freem(m); 1244e5aeaa0cSDag-Erling Smørgrav return (error); 1245a29f300eSGarrett Wollman } 1246df8bae1dSRodney W. Grimes 1247a29f300eSGarrett Wollman static int 1248c80ea19bSGleb Smirnoff uipc_ready(struct socket *so, struct mbuf *m, int count) 1249c80ea19bSGleb Smirnoff { 1250c80ea19bSGleb Smirnoff struct unpcb *unp, *unp2; 1251c80ea19bSGleb Smirnoff struct socket *so2; 1252c80ea19bSGleb Smirnoff int error; 1253c80ea19bSGleb Smirnoff 1254c80ea19bSGleb Smirnoff unp = sotounpcb(so); 1255c80ea19bSGleb Smirnoff 1256a62b4665SMatt Macy UNP_PCB_LOCK(unp); 1257100db364SGleb Smirnoff if ((unp2 = unp->unp_conn) == NULL) { 1258a62b4665SMatt Macy UNP_PCB_UNLOCK(unp); 1259a62b4665SMatt Macy goto error; 1260100db364SGleb Smirnoff } 1261a62b4665SMatt Macy if (unp != unp2) { 1262a62b4665SMatt Macy if (UNP_PCB_TRYLOCK(unp2) == 0) { 1263a62b4665SMatt Macy unp_pcb_hold(unp2); 1264a62b4665SMatt Macy UNP_PCB_UNLOCK(unp); 1265c80ea19bSGleb Smirnoff UNP_PCB_LOCK(unp2); 1266a62b4665SMatt Macy if (unp_pcb_rele(unp2)) 1267a62b4665SMatt Macy goto error; 1268a62b4665SMatt Macy } else 1269a62b4665SMatt Macy UNP_PCB_UNLOCK(unp); 1270a62b4665SMatt Macy } 1271c80ea19bSGleb Smirnoff so2 = unp2->unp_socket; 1272c80ea19bSGleb Smirnoff 1273c80ea19bSGleb Smirnoff SOCKBUF_LOCK(&so2->so_rcv); 1274c80ea19bSGleb Smirnoff if ((error = sbready(&so2->so_rcv, m, count)) == 0) 1275c80ea19bSGleb Smirnoff sorwakeup_locked(so2); 1276c80ea19bSGleb Smirnoff else 1277c80ea19bSGleb Smirnoff SOCKBUF_UNLOCK(&so2->so_rcv); 1278c80ea19bSGleb Smirnoff 1279c80ea19bSGleb Smirnoff UNP_PCB_UNLOCK(unp2); 1280c80ea19bSGleb Smirnoff 1281c80ea19bSGleb Smirnoff return (error); 1282a62b4665SMatt Macy error: 1283a62b4665SMatt Macy for (int i = 0; i < count; i++) 1284a62b4665SMatt Macy m = m_free(m); 1285a62b4665SMatt Macy return (ECONNRESET); 1286c80ea19bSGleb Smirnoff } 1287c80ea19bSGleb Smirnoff 1288c80ea19bSGleb Smirnoff static int 1289a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 1290a29f300eSGarrett Wollman { 1291c2090e73SAlan Somers struct unpcb *unp; 1292a29f300eSGarrett Wollman 129340f2ac28SRobert Watson unp = sotounpcb(so); 12944d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sense: unp == NULL")); 1295e7c33e29SRobert Watson 1296a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 1297f3732fd1SPoul-Henning Kamp sb->st_dev = NODEV; 1298a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 1299df8bae1dSRodney W. Grimes return (0); 1300a29f300eSGarrett Wollman } 1301df8bae1dSRodney W. Grimes 1302a29f300eSGarrett Wollman static int 1303a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 1304a29f300eSGarrett Wollman { 130540f2ac28SRobert Watson struct unpcb *unp; 1306df8bae1dSRodney W. Grimes 130740f2ac28SRobert Watson unp = sotounpcb(so); 13084d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_shutdown: unp == NULL")); 1309e7c33e29SRobert Watson 1310e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 1311a29f300eSGarrett Wollman socantsendmore(so); 1312a29f300eSGarrett Wollman unp_shutdown(unp); 1313e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1314e5aeaa0cSDag-Erling Smørgrav return (0); 1315a29f300eSGarrett Wollman } 1316df8bae1dSRodney W. Grimes 1317a29f300eSGarrett Wollman static int 131857bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 1319a29f300eSGarrett Wollman { 132040f2ac28SRobert Watson struct unpcb *unp; 13210d9ce3a1SRobert Watson const struct sockaddr *sa; 1322a29f300eSGarrett Wollman 13234d4b555eSRobert Watson unp = sotounpcb(so); 13244d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL")); 1325e7c33e29SRobert Watson 13260d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 1327e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 1328fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 13290d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_addr; 133083f3198bSThomas Moestl else 13310d9ce3a1SRobert Watson sa = &sun_noname; 13320d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 1333e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1334e5aeaa0cSDag-Erling Smørgrav return (0); 1335df8bae1dSRodney W. Grimes } 1336a29f300eSGarrett Wollman 1337fa9402f2SRobert Watson static struct pr_usrreqs uipc_usrreqs_dgram = { 1338756d52a1SPoul-Henning Kamp .pru_abort = uipc_abort, 1339756d52a1SPoul-Henning Kamp .pru_accept = uipc_accept, 1340756d52a1SPoul-Henning Kamp .pru_attach = uipc_attach, 1341756d52a1SPoul-Henning Kamp .pru_bind = uipc_bind, 13427493f24eSPawel Jakub Dawidek .pru_bindat = uipc_bindat, 1343756d52a1SPoul-Henning Kamp .pru_connect = uipc_connect, 13447493f24eSPawel Jakub Dawidek .pru_connectat = uipc_connectat, 1345756d52a1SPoul-Henning Kamp .pru_connect2 = uipc_connect2, 1346756d52a1SPoul-Henning Kamp .pru_detach = uipc_detach, 1347756d52a1SPoul-Henning Kamp .pru_disconnect = uipc_disconnect, 1348756d52a1SPoul-Henning Kamp .pru_listen = uipc_listen, 1349756d52a1SPoul-Henning Kamp .pru_peeraddr = uipc_peeraddr, 1350756d52a1SPoul-Henning Kamp .pru_rcvd = uipc_rcvd, 1351756d52a1SPoul-Henning Kamp .pru_send = uipc_send, 1352756d52a1SPoul-Henning Kamp .pru_sense = uipc_sense, 1353756d52a1SPoul-Henning Kamp .pru_shutdown = uipc_shutdown, 1354756d52a1SPoul-Henning Kamp .pru_sockaddr = uipc_sockaddr, 1355fa9402f2SRobert Watson .pru_soreceive = soreceive_dgram, 1356fa9402f2SRobert Watson .pru_close = uipc_close, 1357fa9402f2SRobert Watson }; 1358fa9402f2SRobert Watson 135984d61770SRobert Watson static struct pr_usrreqs uipc_usrreqs_seqpacket = { 136084d61770SRobert Watson .pru_abort = uipc_abort, 136184d61770SRobert Watson .pru_accept = uipc_accept, 136284d61770SRobert Watson .pru_attach = uipc_attach, 136384d61770SRobert Watson .pru_bind = uipc_bind, 13647493f24eSPawel Jakub Dawidek .pru_bindat = uipc_bindat, 136584d61770SRobert Watson .pru_connect = uipc_connect, 13667493f24eSPawel Jakub Dawidek .pru_connectat = uipc_connectat, 136784d61770SRobert Watson .pru_connect2 = uipc_connect2, 136884d61770SRobert Watson .pru_detach = uipc_detach, 136984d61770SRobert Watson .pru_disconnect = uipc_disconnect, 137084d61770SRobert Watson .pru_listen = uipc_listen, 137184d61770SRobert Watson .pru_peeraddr = uipc_peeraddr, 137284d61770SRobert Watson .pru_rcvd = uipc_rcvd, 137384d61770SRobert Watson .pru_send = uipc_send, 137484d61770SRobert Watson .pru_sense = uipc_sense, 137584d61770SRobert Watson .pru_shutdown = uipc_shutdown, 137684d61770SRobert Watson .pru_sockaddr = uipc_sockaddr, 137784d61770SRobert Watson .pru_soreceive = soreceive_generic, /* XXX: or...? */ 137884d61770SRobert Watson .pru_close = uipc_close, 137984d61770SRobert Watson }; 138084d61770SRobert Watson 1381fa9402f2SRobert Watson static struct pr_usrreqs uipc_usrreqs_stream = { 1382fa9402f2SRobert Watson .pru_abort = uipc_abort, 1383fa9402f2SRobert Watson .pru_accept = uipc_accept, 1384fa9402f2SRobert Watson .pru_attach = uipc_attach, 1385fa9402f2SRobert Watson .pru_bind = uipc_bind, 13867493f24eSPawel Jakub Dawidek .pru_bindat = uipc_bindat, 1387fa9402f2SRobert Watson .pru_connect = uipc_connect, 13887493f24eSPawel Jakub Dawidek .pru_connectat = uipc_connectat, 1389fa9402f2SRobert Watson .pru_connect2 = uipc_connect2, 1390fa9402f2SRobert Watson .pru_detach = uipc_detach, 1391fa9402f2SRobert Watson .pru_disconnect = uipc_disconnect, 1392fa9402f2SRobert Watson .pru_listen = uipc_listen, 1393fa9402f2SRobert Watson .pru_peeraddr = uipc_peeraddr, 1394fa9402f2SRobert Watson .pru_rcvd = uipc_rcvd, 1395fa9402f2SRobert Watson .pru_send = uipc_send, 1396c80ea19bSGleb Smirnoff .pru_ready = uipc_ready, 1397fa9402f2SRobert Watson .pru_sense = uipc_sense, 1398fa9402f2SRobert Watson .pru_shutdown = uipc_shutdown, 1399fa9402f2SRobert Watson .pru_sockaddr = uipc_sockaddr, 1400fa9402f2SRobert Watson .pru_soreceive = soreceive_generic, 1401a152f8a3SRobert Watson .pru_close = uipc_close, 1402a29f300eSGarrett Wollman }; 1403df8bae1dSRodney W. Grimes 14040b36cd25SRobert Watson static int 1405892af6b9SRobert Watson uipc_ctloutput(struct socket *so, struct sockopt *sopt) 14060c1bb4fbSDima Dorfman { 140740f2ac28SRobert Watson struct unpcb *unp; 14080d9ce3a1SRobert Watson struct xucred xu; 14096a2989fdSMatthew N. Dodd int error, optval; 14106a2989fdSMatthew N. Dodd 141196a041b5SMatthew N. Dodd if (sopt->sopt_level != 0) 141296a041b5SMatthew N. Dodd return (EINVAL); 141396a041b5SMatthew N. Dodd 14146a2989fdSMatthew N. Dodd unp = sotounpcb(so); 14154d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL")); 14166a2989fdSMatthew N. Dodd error = 0; 14170c1bb4fbSDima Dorfman switch (sopt->sopt_dir) { 14180c1bb4fbSDima Dorfman case SOPT_GET: 14190c1bb4fbSDima Dorfman switch (sopt->sopt_name) { 14200c1bb4fbSDima Dorfman case LOCAL_PEERCRED: 1421e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 14220c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC) 14230d9ce3a1SRobert Watson xu = unp->unp_peercred; 14240c1bb4fbSDima Dorfman else { 14250c1bb4fbSDima Dorfman if (so->so_type == SOCK_STREAM) 14260c1bb4fbSDima Dorfman error = ENOTCONN; 14270c1bb4fbSDima Dorfman else 14280c1bb4fbSDima Dorfman error = EINVAL; 14290c1bb4fbSDima Dorfman } 1430e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 14310d9ce3a1SRobert Watson if (error == 0) 14320d9ce3a1SRobert Watson error = sooptcopyout(sopt, &xu, sizeof(xu)); 14330c1bb4fbSDima Dorfman break; 1434e7c33e29SRobert Watson 14356a2989fdSMatthew N. Dodd case LOCAL_CREDS: 1436a6357845SRobert Watson /* Unlocked read. */ 14376a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0; 14386a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 14396a2989fdSMatthew N. Dodd break; 1440e7c33e29SRobert Watson 14416a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 1442a6357845SRobert Watson /* Unlocked read. */ 14436a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0; 14446a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 14456a2989fdSMatthew N. Dodd break; 1446e7c33e29SRobert Watson 14470c1bb4fbSDima Dorfman default: 14480c1bb4fbSDima Dorfman error = EOPNOTSUPP; 14490c1bb4fbSDima Dorfman break; 14500c1bb4fbSDima Dorfman } 14510c1bb4fbSDima Dorfman break; 1452e7c33e29SRobert Watson 14530c1bb4fbSDima Dorfman case SOPT_SET: 14546a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 14556a2989fdSMatthew N. Dodd case LOCAL_CREDS: 14566a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 14576a2989fdSMatthew N. Dodd error = sooptcopyin(sopt, &optval, sizeof(optval), 14586a2989fdSMatthew N. Dodd sizeof(optval)); 14596a2989fdSMatthew N. Dodd if (error) 14606a2989fdSMatthew N. Dodd break; 14616a2989fdSMatthew N. Dodd 1462e7c33e29SRobert Watson #define OPTSET(bit) do { \ 1463e7c33e29SRobert Watson UNP_PCB_LOCK(unp); \ 14646a2989fdSMatthew N. Dodd if (optval) \ 14656a2989fdSMatthew N. Dodd unp->unp_flags |= bit; \ 14666a2989fdSMatthew N. Dodd else \ 1467e7c33e29SRobert Watson unp->unp_flags &= ~bit; \ 1468e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); \ 1469e7c33e29SRobert Watson } while (0) 14706a2989fdSMatthew N. Dodd 14716a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 14726a2989fdSMatthew N. Dodd case LOCAL_CREDS: 14736a2989fdSMatthew N. Dodd OPTSET(UNP_WANTCRED); 14746a2989fdSMatthew N. Dodd break; 1475e7c33e29SRobert Watson 14766a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 14776a2989fdSMatthew N. Dodd OPTSET(UNP_CONNWAIT); 14786a2989fdSMatthew N. Dodd break; 1479e7c33e29SRobert Watson 14806a2989fdSMatthew N. Dodd default: 14816a2989fdSMatthew N. Dodd break; 14826a2989fdSMatthew N. Dodd } 14836a2989fdSMatthew N. Dodd break; 14846a2989fdSMatthew N. Dodd #undef OPTSET 14856a2989fdSMatthew N. Dodd default: 14866a2989fdSMatthew N. Dodd error = ENOPROTOOPT; 14876a2989fdSMatthew N. Dodd break; 14886a2989fdSMatthew N. Dodd } 1489abb886faSMatthew N. Dodd break; 1490e7c33e29SRobert Watson 14910c1bb4fbSDima Dorfman default: 14920c1bb4fbSDima Dorfman error = EOPNOTSUPP; 14930c1bb4fbSDima Dorfman break; 14940c1bb4fbSDima Dorfman } 14950c1bb4fbSDima Dorfman return (error); 14960c1bb4fbSDima Dorfman } 14970c1bb4fbSDima Dorfman 1498f708ef1bSPoul-Henning Kamp static int 1499892af6b9SRobert Watson unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1500df8bae1dSRodney W. Grimes { 15017493f24eSPawel Jakub Dawidek 15027493f24eSPawel Jakub Dawidek return (unp_connectat(AT_FDCWD, so, nam, td)); 15037493f24eSPawel Jakub Dawidek } 15047493f24eSPawel Jakub Dawidek 15057493f24eSPawel Jakub Dawidek static int 15067493f24eSPawel Jakub Dawidek unp_connectat(int fd, struct socket *so, struct sockaddr *nam, 15077493f24eSPawel Jakub Dawidek struct thread *td) 15087493f24eSPawel Jakub Dawidek { 1509892af6b9SRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam; 1510892af6b9SRobert Watson struct vnode *vp; 1511779f106aSGleb Smirnoff struct socket *so2; 1512b295bdcdSRobert Watson struct unpcb *unp, *unp2, *unp3; 1513df8bae1dSRodney W. Grimes struct nameidata nd; 151457bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 15150d9ce3a1SRobert Watson struct sockaddr *sa; 15167008be5bSPawel Jakub Dawidek cap_rights_t rights; 151775a67bf3SMatt Macy int error, len, freed; 151875a67bf3SMatt Macy struct mtx *vplock; 15190d9ce3a1SRobert Watson 1520cb7df69bSKevin Lo if (nam->sa_family != AF_UNIX) 1521cb7df69bSKevin Lo return (EAFNOSUPPORT); 1522a06534c3SBjoern A. Zeeb if (nam->sa_len > sizeof(struct sockaddr_un)) 1523a06534c3SBjoern A. Zeeb return (EINVAL); 152457bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 152557bf258eSGarrett Wollman if (len <= 0) 1526e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 15277928893dSEd Maste bcopy(soun->sun_path, buf, len); 15287928893dSEd Maste buf[len] = 0; 1529e7c33e29SRobert Watson 153075a67bf3SMatt Macy unp = sotounpcb(so); 1531e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 15324f1f0ef5SRobert Watson if (unp->unp_flags & UNP_CONNECTING) { 1533e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 15344f1f0ef5SRobert Watson return (EALREADY); 15354f1f0ef5SRobert Watson } 153605102f04SRobert Watson unp->unp_flags |= UNP_CONNECTING; 1537e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1538e7c33e29SRobert Watson 15390d9ce3a1SRobert Watson sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 15407493f24eSPawel Jakub Dawidek NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, 15417008be5bSPawel Jakub Dawidek UIO_SYSSPACE, buf, fd, cap_rights_init(&rights, CAP_CONNECTAT), td); 1542797f2d22SPoul-Henning Kamp error = namei(&nd); 1543797f2d22SPoul-Henning Kamp if (error) 15440d9ce3a1SRobert Watson vp = NULL; 15450d9ce3a1SRobert Watson else 1546df8bae1dSRodney W. Grimes vp = nd.ni_vp; 15470d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_connect"); 1548762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 15490d9ce3a1SRobert Watson if (error) 15500d9ce3a1SRobert Watson goto bad; 15510d9ce3a1SRobert Watson 1552df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 1553df8bae1dSRodney W. Grimes error = ENOTSOCK; 1554df8bae1dSRodney W. Grimes goto bad; 1555df8bae1dSRodney W. Grimes } 15566fac927cSRobert Watson #ifdef MAC 155730d239bcSRobert Watson error = mac_vnode_check_open(td->td_ucred, vp, VWRITE | VREAD); 15586fac927cSRobert Watson if (error) 15596fac927cSRobert Watson goto bad; 15606fac927cSRobert Watson #endif 1561a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 1562797f2d22SPoul-Henning Kamp if (error) 1563df8bae1dSRodney W. Grimes goto bad; 1564e7c33e29SRobert Watson 1565b295bdcdSRobert Watson unp = sotounpcb(so); 15664d4b555eSRobert Watson KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 1567e7c33e29SRobert Watson 156875a67bf3SMatt Macy vplock = mtx_pool_find(mtxpool_sleep, vp); 156975a67bf3SMatt Macy mtx_lock(vplock); 15700c3c207fSGleb Smirnoff VOP_UNP_CONNECT(vp, &unp2); 15710c3c207fSGleb Smirnoff if (unp2 == NULL) { 1572df8bae1dSRodney W. Grimes error = ECONNREFUSED; 15732260c03dSRobert Watson goto bad2; 1574df8bae1dSRodney W. Grimes } 15750c3c207fSGleb Smirnoff so2 = unp2->unp_socket; 1576df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 1577df8bae1dSRodney W. Grimes error = EPROTOTYPE; 15782260c03dSRobert Watson goto bad2; 1579df8bae1dSRodney W. Grimes } 1580df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 1581e7c33e29SRobert Watson if (so2->so_options & SO_ACCEPTCONN) { 15821fb51a12SBjoern A. Zeeb CURVNET_SET(so2->so_vnet); 1583779f106aSGleb Smirnoff so2 = sonewconn(so2, 0); 15841fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 1585e7c33e29SRobert Watson } else 1586779f106aSGleb Smirnoff so2 = NULL; 1587779f106aSGleb Smirnoff if (so2 == NULL) { 1588df8bae1dSRodney W. Grimes error = ECONNREFUSED; 1589cb8f450bSMatt Macy goto bad2; 1590df8bae1dSRodney W. Grimes } 1591779f106aSGleb Smirnoff unp3 = sotounpcb(so2); 1592e10ef65dSMatt Macy unp_pcb_lock2(unp2, unp3); 15930d9ce3a1SRobert Watson if (unp2->unp_addr != NULL) { 15940d9ce3a1SRobert Watson bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 15950d9ce3a1SRobert Watson unp3->unp_addr = (struct sockaddr_un *) sa; 15960d9ce3a1SRobert Watson sa = NULL; 15970d9ce3a1SRobert Watson } 1598b523ec24SRobert Watson 1599da446550SAlan Somers unp_copy_peercred(td, unp3, unp, unp2); 1600b523ec24SRobert Watson 1601e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1602779f106aSGleb Smirnoff unp2 = unp3; 160375a67bf3SMatt Macy unp_pcb_owned_lock2(unp2, unp, freed); 1604e10ef65dSMatt Macy if (__predict_false(freed)) { 1605e10ef65dSMatt Macy UNP_PCB_UNLOCK(unp2); 1606e10ef65dSMatt Macy error = ECONNREFUSED; 1607e10ef65dSMatt Macy goto bad2; 1608e10ef65dSMatt Macy } 1609335654d7SRobert Watson #ifdef MAC 1610779f106aSGleb Smirnoff mac_socketpeer_set_from_socket(so, so2); 1611779f106aSGleb Smirnoff mac_socketpeer_set_from_socket(so2, so); 1612335654d7SRobert Watson #endif 1613a3a73490SMatt Macy } else { 1614a3a73490SMatt Macy if (unp == unp2) 1615a3a73490SMatt Macy UNP_PCB_LOCK(unp); 1616a3a73490SMatt Macy else 1617e10ef65dSMatt Macy unp_pcb_lock2(unp, unp2); 1618a3a73490SMatt Macy } 1619779f106aSGleb Smirnoff KASSERT(unp2 != NULL && so2 != NULL && unp2->unp_socket == so2 && 1620779f106aSGleb Smirnoff sotounpcb(so2) == unp2, 1621779f106aSGleb Smirnoff ("%s: unp2 %p so2 %p", __func__, unp2, so2)); 16226a2989fdSMatthew N. Dodd error = unp_connect2(so, so2, PRU_CONNECT); 1623a3a73490SMatt Macy if (unp != unp2) 1624e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1625e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 16260d9ce3a1SRobert Watson bad2: 162775a67bf3SMatt Macy mtx_unlock(vplock); 1628df8bae1dSRodney W. Grimes bad: 162975a67bf3SMatt Macy if (vp != NULL) { 1630df8bae1dSRodney W. Grimes vput(vp); 163175a67bf3SMatt Macy } 16320d9ce3a1SRobert Watson free(sa, M_SONAME); 1633e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 16344f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_CONNECTING; 1635e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1636df8bae1dSRodney W. Grimes return (error); 1637df8bae1dSRodney W. Grimes } 1638df8bae1dSRodney W. Grimes 1639da446550SAlan Somers /* 1640da446550SAlan Somers * Set socket peer credentials at connection time. 1641da446550SAlan Somers * 1642da446550SAlan Somers * The client's PCB credentials are copied from its process structure. The 1643da446550SAlan Somers * server's PCB credentials are copied from the socket on which it called 1644da446550SAlan Somers * listen(2). uipc_listen cached that process's credentials at the time. 1645da446550SAlan Somers */ 1646da446550SAlan Somers void 1647da446550SAlan Somers unp_copy_peercred(struct thread *td, struct unpcb *client_unp, 1648da446550SAlan Somers struct unpcb *server_unp, struct unpcb *listen_unp) 1649da446550SAlan Somers { 1650c5afec6eSDmitry Chagin cru2xt(td, &client_unp->unp_peercred); 1651da446550SAlan Somers client_unp->unp_flags |= UNP_HAVEPC; 1652da446550SAlan Somers 1653da446550SAlan Somers memcpy(&server_unp->unp_peercred, &listen_unp->unp_peercred, 1654da446550SAlan Somers sizeof(server_unp->unp_peercred)); 1655da446550SAlan Somers server_unp->unp_flags |= UNP_HAVEPC; 1656da446550SAlan Somers if (listen_unp->unp_flags & UNP_WANTCRED) 1657da446550SAlan Somers client_unp->unp_flags |= UNP_WANTCRED; 1658da446550SAlan Somers } 1659da446550SAlan Somers 1660db48c0d2SRobert Watson static int 16616a2989fdSMatthew N. Dodd unp_connect2(struct socket *so, struct socket *so2, int req) 1662df8bae1dSRodney W. Grimes { 1663e7c33e29SRobert Watson struct unpcb *unp; 1664892af6b9SRobert Watson struct unpcb *unp2; 1665df8bae1dSRodney W. Grimes 1666e7c33e29SRobert Watson unp = sotounpcb(so); 1667e7c33e29SRobert Watson KASSERT(unp != NULL, ("unp_connect2: unp == NULL")); 1668e7c33e29SRobert Watson unp2 = sotounpcb(so2); 1669e7c33e29SRobert Watson KASSERT(unp2 != NULL, ("unp_connect2: unp2 == NULL")); 1670e7c33e29SRobert Watson 1671e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 1672e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp2); 16730d9ce3a1SRobert Watson 1674df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 1675df8bae1dSRodney W. Grimes return (EPROTOTYPE); 1676df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 167775a67bf3SMatt Macy unp_pcb_hold(unp2); 167875a67bf3SMatt Macy unp_pcb_hold(unp); 1679df8bae1dSRodney W. Grimes switch (so->so_type) { 1680df8bae1dSRodney W. Grimes case SOCK_DGRAM: 168175a67bf3SMatt Macy UNP_REF_LIST_LOCK(); 168298271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 168375a67bf3SMatt Macy UNP_REF_LIST_UNLOCK(); 1684df8bae1dSRodney W. Grimes soisconnected(so); 1685df8bae1dSRodney W. Grimes break; 1686df8bae1dSRodney W. Grimes 1687df8bae1dSRodney W. Grimes case SOCK_STREAM: 168884d61770SRobert Watson case SOCK_SEQPACKET: 1689df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 16906a2989fdSMatthew N. Dodd if (req == PRU_CONNECT && 16916a2989fdSMatthew N. Dodd ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT)) 16926a2989fdSMatthew N. Dodd soisconnecting(so); 16936a2989fdSMatthew N. Dodd else 1694df8bae1dSRodney W. Grimes soisconnected(so); 1695df8bae1dSRodney W. Grimes soisconnected(so2); 1696df8bae1dSRodney W. Grimes break; 1697df8bae1dSRodney W. Grimes 1698df8bae1dSRodney W. Grimes default: 1699df8bae1dSRodney W. Grimes panic("unp_connect2"); 1700df8bae1dSRodney W. Grimes } 1701df8bae1dSRodney W. Grimes return (0); 1702df8bae1dSRodney W. Grimes } 1703df8bae1dSRodney W. Grimes 1704f708ef1bSPoul-Henning Kamp static void 1705e7c33e29SRobert Watson unp_disconnect(struct unpcb *unp, struct unpcb *unp2) 1706df8bae1dSRodney W. Grimes { 170775a67bf3SMatt Macy struct socket *so, *so2; 17084b06dee1SMatt Macy int freed __unused; 1709df8bae1dSRodney W. Grimes 1710e7c33e29SRobert Watson KASSERT(unp2 != NULL, ("unp_disconnect: unp2 == NULL")); 17110d9ce3a1SRobert Watson 1712e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 1713e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp2); 1714e7c33e29SRobert Watson 171575a67bf3SMatt Macy if (unp->unp_conn == NULL && unp2->unp_conn == NULL) 171675a67bf3SMatt Macy return; 171775a67bf3SMatt Macy 171875a67bf3SMatt Macy MPASS(unp->unp_conn == unp2); 1719fc3fcacfSRobert Watson unp->unp_conn = NULL; 172075a67bf3SMatt Macy so = unp->unp_socket; 172175a67bf3SMatt Macy so2 = unp2->unp_socket; 1722df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 1723df8bae1dSRodney W. Grimes case SOCK_DGRAM: 172475a67bf3SMatt Macy UNP_REF_LIST_LOCK(); 172598271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 172675a67bf3SMatt Macy UNP_REF_LIST_UNLOCK(); 172775a67bf3SMatt Macy if (so) { 17281b2e3b4bSRobert Watson SOCK_LOCK(so); 17291b2e3b4bSRobert Watson so->so_state &= ~SS_ISCONNECTED; 17301b2e3b4bSRobert Watson SOCK_UNLOCK(so); 173175a67bf3SMatt Macy } 1732df8bae1dSRodney W. Grimes break; 1733df8bae1dSRodney W. Grimes 1734df8bae1dSRodney W. Grimes case SOCK_STREAM: 173584d61770SRobert Watson case SOCK_SEQPACKET: 173675a67bf3SMatt Macy if (so) 173775a67bf3SMatt Macy soisdisconnected(so); 173875a67bf3SMatt Macy MPASS(unp2->unp_conn == unp); 1739fc3fcacfSRobert Watson unp2->unp_conn = NULL; 174075a67bf3SMatt Macy if (so2) 174175a67bf3SMatt Macy soisdisconnected(so2); 1742df8bae1dSRodney W. Grimes break; 1743df8bae1dSRodney W. Grimes } 17444b06dee1SMatt Macy freed = unp_pcb_rele(unp); 174575a67bf3SMatt Macy MPASS(freed == 0); 17464b06dee1SMatt Macy freed = unp_pcb_rele(unp2); 174775a67bf3SMatt Macy MPASS(freed == 0); 1748df8bae1dSRodney W. Grimes } 1749df8bae1dSRodney W. Grimes 17500d9ce3a1SRobert Watson /* 1751d7924b70SRobert Watson * unp_pcblist() walks the global list of struct unpcb's to generate a 1752d7924b70SRobert Watson * pointer list, bumping the refcount on each unpcb. It then copies them out 1753d7924b70SRobert Watson * sequentially, validating the generation number on each to see if it has 1754d7924b70SRobert Watson * been detached. All of this is necessary because copyout() may sleep on 1755d7924b70SRobert Watson * disk I/O. 17560d9ce3a1SRobert Watson */ 175798271db4SGarrett Wollman static int 175882d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 175998271db4SGarrett Wollman { 176098271db4SGarrett Wollman struct unpcb *unp, **unp_list; 176198271db4SGarrett Wollman unp_gen_t gencnt; 17628f364875SJulian Elischer struct xunpgen *xug; 176398271db4SGarrett Wollman struct unp_head *head; 17648f364875SJulian Elischer struct xunpcb *xu; 1765d821d364SPedro F. Giffuni u_int i; 176644c514b1SPedro F. Giffuni int error, freeunp, n; 176798271db4SGarrett Wollman 176884d61770SRobert Watson switch ((intptr_t)arg1) { 176984d61770SRobert Watson case SOCK_STREAM: 177084d61770SRobert Watson head = &unp_shead; 177184d61770SRobert Watson break; 177284d61770SRobert Watson 177384d61770SRobert Watson case SOCK_DGRAM: 177484d61770SRobert Watson head = &unp_dhead; 177584d61770SRobert Watson break; 177684d61770SRobert Watson 177784d61770SRobert Watson case SOCK_SEQPACKET: 177884d61770SRobert Watson head = &unp_sphead; 177984d61770SRobert Watson break; 178084d61770SRobert Watson 178184d61770SRobert Watson default: 1782604f19c9SRobert Watson panic("unp_pcblist: arg1 %d", (int)(intptr_t)arg1); 178384d61770SRobert Watson } 178498271db4SGarrett Wollman 178598271db4SGarrett Wollman /* 178698271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 178798271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 178898271db4SGarrett Wollman */ 1789fc3fcacfSRobert Watson if (req->oldptr == NULL) { 179098271db4SGarrett Wollman n = unp_count; 17918f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug) 179298271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 1793e5aeaa0cSDag-Erling Smørgrav return (0); 179498271db4SGarrett Wollman } 179598271db4SGarrett Wollman 1796fc3fcacfSRobert Watson if (req->newptr != NULL) 1797e5aeaa0cSDag-Erling Smørgrav return (EPERM); 179898271db4SGarrett Wollman 179998271db4SGarrett Wollman /* 180098271db4SGarrett Wollman * OK, now we're committed to doing something. 180198271db4SGarrett Wollman */ 180279db6fe7SMark Johnston xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK | M_ZERO); 1803779f106aSGleb Smirnoff UNP_LINK_RLOCK(); 180498271db4SGarrett Wollman gencnt = unp_gencnt; 180598271db4SGarrett Wollman n = unp_count; 1806779f106aSGleb Smirnoff UNP_LINK_RUNLOCK(); 180798271db4SGarrett Wollman 18088f364875SJulian Elischer xug->xug_len = sizeof *xug; 18098f364875SJulian Elischer xug->xug_count = n; 18108f364875SJulian Elischer xug->xug_gen = gencnt; 18118f364875SJulian Elischer xug->xug_sogen = so_gencnt; 18128f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 18138f364875SJulian Elischer if (error) { 18148f364875SJulian Elischer free(xug, M_TEMP); 1815e5aeaa0cSDag-Erling Smørgrav return (error); 18168f364875SJulian Elischer } 181798271db4SGarrett Wollman 1818a163d034SWarner Losh unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 181998271db4SGarrett Wollman 1820779f106aSGleb Smirnoff UNP_LINK_RLOCK(); 18212e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 18222e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 1823e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 18248a7d8cc6SRobert Watson if (unp->unp_gencnt <= gencnt) { 1825a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred, 1826e7c33e29SRobert Watson unp->unp_socket->so_cred)) { 1827e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 18284787fd37SPaul Saab continue; 1829e7c33e29SRobert Watson } 183098271db4SGarrett Wollman unp_list[i++] = unp; 183175a67bf3SMatt Macy unp_pcb_hold(unp); 183298271db4SGarrett Wollman } 1833e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 18344787fd37SPaul Saab } 1835779f106aSGleb Smirnoff UNP_LINK_RUNLOCK(); 18361c381b19SRobert Watson n = i; /* In case we lost some during malloc. */ 183798271db4SGarrett Wollman 183898271db4SGarrett Wollman error = 0; 1839fe2eee82SColin Percival xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO); 184098271db4SGarrett Wollman for (i = 0; i < n; i++) { 184198271db4SGarrett Wollman unp = unp_list[i]; 1842e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 184375a67bf3SMatt Macy freeunp = unp_pcb_rele(unp); 184475a67bf3SMatt Macy 184575a67bf3SMatt Macy if (freeunp == 0 && unp->unp_gencnt <= gencnt) { 18468f364875SJulian Elischer xu->xu_len = sizeof *xu; 18473a20f06aSBrooks Davis xu->xu_unpp = (uintptr_t)unp; 184898271db4SGarrett Wollman /* 184998271db4SGarrett Wollman * XXX - need more locking here to protect against 185098271db4SGarrett Wollman * connect/disconnect races for SMP. 185198271db4SGarrett Wollman */ 1852fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 18538f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr, 185498271db4SGarrett Wollman unp->unp_addr->sun_len); 18550e229f34SGleb Smirnoff else 18560e229f34SGleb Smirnoff bzero(&xu->xu_addr, sizeof(xu->xu_addr)); 1857fc3fcacfSRobert Watson if (unp->unp_conn != NULL && 1858fc3fcacfSRobert Watson unp->unp_conn->unp_addr != NULL) 185998271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 18608f364875SJulian Elischer &xu->xu_caddr, 186198271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 18620e229f34SGleb Smirnoff else 18630e229f34SGleb Smirnoff bzero(&xu->xu_caddr, sizeof(xu->xu_caddr)); 18643a20f06aSBrooks Davis xu->unp_vnode = (uintptr_t)unp->unp_vnode; 18653a20f06aSBrooks Davis xu->unp_conn = (uintptr_t)unp->unp_conn; 18663a20f06aSBrooks Davis xu->xu_firstref = (uintptr_t)LIST_FIRST(&unp->unp_refs); 18673a20f06aSBrooks Davis xu->xu_nextref = (uintptr_t)LIST_NEXT(unp, unp_reflink); 18680e229f34SGleb Smirnoff xu->unp_gencnt = unp->unp_gencnt; 18698f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket); 1870e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 18718f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu); 187275a67bf3SMatt Macy } else if (freeunp == 0) 1873e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1874e7c33e29SRobert Watson } 18758f364875SJulian Elischer free(xu, M_TEMP); 187698271db4SGarrett Wollman if (!error) { 187798271db4SGarrett Wollman /* 18781c381b19SRobert Watson * Give the user an updated idea of our state. If the 18791c381b19SRobert Watson * generation differs from what we told her before, she knows 18801c381b19SRobert Watson * that something happened while we were processing this 18811c381b19SRobert Watson * request, and it might be necessary to retry. 188298271db4SGarrett Wollman */ 18838f364875SJulian Elischer xug->xug_gen = unp_gencnt; 18848f364875SJulian Elischer xug->xug_sogen = so_gencnt; 18858f364875SJulian Elischer xug->xug_count = unp_count; 18868f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 188798271db4SGarrett Wollman } 188898271db4SGarrett Wollman free(unp_list, M_TEMP); 18898f364875SJulian Elischer free(xug, M_TEMP); 1890e5aeaa0cSDag-Erling Smørgrav return (error); 189198271db4SGarrett Wollman } 189298271db4SGarrett Wollman 18937029da5cSPawel Biernacki SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, 18947029da5cSPawel Biernacki CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, 18952fee06f0SMatthew D Fleming (void *)(intptr_t)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 189698271db4SGarrett Wollman "List of active local datagram sockets"); 18977029da5cSPawel Biernacki SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, 18987029da5cSPawel Biernacki CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, 18992fee06f0SMatthew D Fleming (void *)(intptr_t)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 190098271db4SGarrett Wollman "List of active local stream sockets"); 19012fee06f0SMatthew D Fleming SYSCTL_PROC(_net_local_seqpacket, OID_AUTO, pcblist, 19027029da5cSPawel Biernacki CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, 19032fee06f0SMatthew D Fleming (void *)(intptr_t)SOCK_SEQPACKET, 0, unp_pcblist, "S,xunpcb", 190484d61770SRobert Watson "List of active local seqpacket sockets"); 190598271db4SGarrett Wollman 1906f708ef1bSPoul-Henning Kamp static void 1907892af6b9SRobert Watson unp_shutdown(struct unpcb *unp) 1908df8bae1dSRodney W. Grimes { 1909e7c33e29SRobert Watson struct unpcb *unp2; 1910df8bae1dSRodney W. Grimes struct socket *so; 1911df8bae1dSRodney W. Grimes 1912e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 19130d9ce3a1SRobert Watson 1914e7c33e29SRobert Watson unp2 = unp->unp_conn; 191584d61770SRobert Watson if ((unp->unp_socket->so_type == SOCK_STREAM || 191684d61770SRobert Watson (unp->unp_socket->so_type == SOCK_SEQPACKET)) && unp2 != NULL) { 1917e7c33e29SRobert Watson so = unp2->unp_socket; 1918e7c33e29SRobert Watson if (so != NULL) 1919df8bae1dSRodney W. Grimes socantrcvmore(so); 1920df8bae1dSRodney W. Grimes } 1921e7c33e29SRobert Watson } 1922df8bae1dSRodney W. Grimes 1923f708ef1bSPoul-Henning Kamp static void 1924afc055d9SEd Schouten unp_drop(struct unpcb *unp) 1925df8bae1dSRodney W. Grimes { 1926df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 1927e7c33e29SRobert Watson struct unpcb *unp2; 192875a67bf3SMatt Macy int freed; 19290d9ce3a1SRobert Watson 1930afc055d9SEd Schouten /* 1931afc055d9SEd Schouten * Regardless of whether the socket's peer dropped the connection 1932afc055d9SEd Schouten * with this socket by aborting or disconnecting, POSIX requires 1933afc055d9SEd Schouten * that ECONNRESET is returned. 1934afc055d9SEd Schouten */ 193575a67bf3SMatt Macy /* acquire a reference so that unp isn't freed from underneath us */ 193675a67bf3SMatt Macy 193775a67bf3SMatt Macy UNP_PCB_LOCK(unp); 193875a67bf3SMatt Macy if (so) 1939afc055d9SEd Schouten so->so_error = ECONNRESET; 1940e7c33e29SRobert Watson unp2 = unp->unp_conn; 1941acf9fd05SMatt Macy if (unp2 == unp) { 1942acf9fd05SMatt Macy unp_disconnect(unp, unp2); 1943acf9fd05SMatt Macy } else if (unp2 != NULL) { 194475a67bf3SMatt Macy unp_pcb_hold(unp2); 194575a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freed); 1946e7c33e29SRobert Watson unp_disconnect(unp, unp2); 194775a67bf3SMatt Macy if (unp_pcb_rele(unp2) == 0) 1948e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1949df8bae1dSRodney W. Grimes } 195075a67bf3SMatt Macy if (unp_pcb_rele(unp) == 0) 195175a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 195275a67bf3SMatt Macy } 1953df8bae1dSRodney W. Grimes 19542bc21ed9SDavid Malone static void 19558cb539f1SPawel Jakub Dawidek unp_freerights(struct filedescent **fdep, int fdcount) 1956df8bae1dSRodney W. Grimes { 19572bc21ed9SDavid Malone struct file *fp; 19582609222aSPawel Jakub Dawidek int i; 1959df8bae1dSRodney W. Grimes 196082e825c4SGleb Smirnoff KASSERT(fdcount > 0, ("%s: fdcount %d", __func__, fdcount)); 196182e825c4SGleb Smirnoff 19628cb539f1SPawel Jakub Dawidek for (i = 0; i < fdcount; i++) { 19638cb539f1SPawel Jakub Dawidek fp = fdep[i]->fde_file; 19648cb539f1SPawel Jakub Dawidek filecaps_free(&fdep[i]->fde_caps); 19658692c025SYoshinobu Inoue unp_discard(fp); 1966df8bae1dSRodney W. Grimes } 19678cb539f1SPawel Jakub Dawidek free(fdep[0], M_FILECAPS); 19682bc21ed9SDavid Malone } 19692bc21ed9SDavid Malone 19700b36cd25SRobert Watson static int 1971c2e3c52eSJilles Tjoelker unp_externalize(struct mbuf *control, struct mbuf **controlp, int flags) 19722bc21ed9SDavid Malone { 19732bc21ed9SDavid Malone struct thread *td = curthread; /* XXX */ 19742bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 19752bc21ed9SDavid Malone int i; 19762bc21ed9SDavid Malone int *fdp; 19772609222aSPawel Jakub Dawidek struct filedesc *fdesc = td->td_proc->p_fd; 1978ea31808cSMateusz Guzik struct filedescent **fdep; 19792bc21ed9SDavid Malone void *data; 19802bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 19812bc21ed9SDavid Malone int error, newfds; 19822bc21ed9SDavid Malone u_int newlen; 19832bc21ed9SDavid Malone 19843dab55bcSRobert Watson UNP_LINK_UNLOCK_ASSERT(); 19854c5bc1caSRobert Watson 19862bc21ed9SDavid Malone error = 0; 19872bc21ed9SDavid Malone if (controlp != NULL) /* controlp == NULL => free control messages */ 19882bc21ed9SDavid Malone *controlp = NULL; 19892bc21ed9SDavid Malone while (cm != NULL) { 19902bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 19912bc21ed9SDavid Malone error = EINVAL; 19922bc21ed9SDavid Malone break; 19932bc21ed9SDavid Malone } 19942bc21ed9SDavid Malone data = CMSG_DATA(cm); 19952bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 19962bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET 19972bc21ed9SDavid Malone && cm->cmsg_type == SCM_RIGHTS) { 19982609222aSPawel Jakub Dawidek newfds = datalen / sizeof(*fdep); 199982e825c4SGleb Smirnoff if (newfds == 0) 200082e825c4SGleb Smirnoff goto next; 20012609222aSPawel Jakub Dawidek fdep = data; 20022bc21ed9SDavid Malone 2003e2f9a08bSOlivier Houchard /* If we're not outputting the descriptors free them. */ 20042bc21ed9SDavid Malone if (error || controlp == NULL) { 20052609222aSPawel Jakub Dawidek unp_freerights(fdep, newfds); 20062bc21ed9SDavid Malone goto next; 20072bc21ed9SDavid Malone } 20082609222aSPawel Jakub Dawidek FILEDESC_XLOCK(fdesc); 200960a5ef26SRobert Watson 2010ed5b7817SJulian Elischer /* 20111c381b19SRobert Watson * Now change each pointer to an fd in the global 20121c381b19SRobert Watson * table to an integer that is the index to the local 20131c381b19SRobert Watson * fd table entry that we set up to point to the 20141c381b19SRobert Watson * global one we are transferring. 2015ed5b7817SJulian Elischer */ 20162bc21ed9SDavid Malone newlen = newfds * sizeof(int); 20172bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 20182bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 20192bc21ed9SDavid Malone if (*controlp == NULL) { 20202609222aSPawel Jakub Dawidek FILEDESC_XUNLOCK(fdesc); 20212bc21ed9SDavid Malone error = E2BIG; 20222609222aSPawel Jakub Dawidek unp_freerights(fdep, newfds); 20232bc21ed9SDavid Malone goto next; 20242bc21ed9SDavid Malone } 20252bc21ed9SDavid Malone 20262bc21ed9SDavid Malone fdp = (int *) 20272bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2028db8f33fdSMateusz Guzik if (fdallocn(td, 0, fdp, newfds) != 0) { 20293331a33aSMateusz Guzik FILEDESC_XUNLOCK(fdesc); 2030db8f33fdSMateusz Guzik error = EMSGSIZE; 2031db8f33fdSMateusz Guzik unp_freerights(fdep, newfds); 2032db8f33fdSMateusz Guzik m_freem(*controlp); 2033db8f33fdSMateusz Guzik *controlp = NULL; 2034db8f33fdSMateusz Guzik goto next; 2035db8f33fdSMateusz Guzik } 20368cb539f1SPawel Jakub Dawidek for (i = 0; i < newfds; i++, fdp++) { 2037ea31808cSMateusz Guzik _finstall(fdesc, fdep[i]->fde_file, *fdp, 2038ea31808cSMateusz Guzik (flags & MSG_CMSG_CLOEXEC) != 0 ? UF_EXCLOSE : 0, 2039ea31808cSMateusz Guzik &fdep[i]->fde_caps); 2040ea31808cSMateusz Guzik unp_externalize_fp(fdep[i]->fde_file); 2041df8bae1dSRodney W. Grimes } 2042c7902fbeSMark Johnston 2043c7902fbeSMark Johnston /* 2044c7902fbeSMark Johnston * The new type indicates that the mbuf data refers to 2045c7902fbeSMark Johnston * kernel resources that may need to be released before 2046c7902fbeSMark Johnston * the mbuf is freed. 2047c7902fbeSMark Johnston */ 2048c7902fbeSMark Johnston m_chtype(*controlp, MT_EXTCONTROL); 20492609222aSPawel Jakub Dawidek FILEDESC_XUNLOCK(fdesc); 20508cb539f1SPawel Jakub Dawidek free(fdep[0], M_FILECAPS); 20511c381b19SRobert Watson } else { 20521c381b19SRobert Watson /* We can just copy anything else across. */ 20532bc21ed9SDavid Malone if (error || controlp == NULL) 20542bc21ed9SDavid Malone goto next; 20552bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, datalen, 20562bc21ed9SDavid Malone cm->cmsg_type, cm->cmsg_level); 20572bc21ed9SDavid Malone if (*controlp == NULL) { 20582bc21ed9SDavid Malone error = ENOBUFS; 20592bc21ed9SDavid Malone goto next; 20602bc21ed9SDavid Malone } 20612bc21ed9SDavid Malone bcopy(data, 20622bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 20632bc21ed9SDavid Malone datalen); 20642bc21ed9SDavid Malone } 20652bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 20662bc21ed9SDavid Malone 20672bc21ed9SDavid Malone next: 20682bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 20692bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 20702bc21ed9SDavid Malone cm = (struct cmsghdr *) 20712bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 20728692c025SYoshinobu Inoue } else { 20732bc21ed9SDavid Malone clen = 0; 20742bc21ed9SDavid Malone cm = NULL; 20758692c025SYoshinobu Inoue } 20768692c025SYoshinobu Inoue } 20778692c025SYoshinobu Inoue 20782bc21ed9SDavid Malone m_freem(control); 20792bc21ed9SDavid Malone return (error); 2080df8bae1dSRodney W. Grimes } 2081df8bae1dSRodney W. Grimes 20824f590175SPaul Saab static void 20834f590175SPaul Saab unp_zone_change(void *tag) 20844f590175SPaul Saab { 20854f590175SPaul Saab 20864f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets); 20874f590175SPaul Saab } 20884f590175SPaul Saab 20890b36cd25SRobert Watson static void 209098271db4SGarrett Wollman unp_init(void) 209198271db4SGarrett Wollman { 20921c381b19SRobert Watson 209321ca7b57SMarko Zec #ifdef VIMAGE 209421ca7b57SMarko Zec if (!IS_DEFAULT_VNET(curvnet)) 209521ca7b57SMarko Zec return; 209621ca7b57SMarko Zec #endif 20979e9d298aSJeff Roberson unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 209875a67bf3SMatt Macy NULL, NULL, UMA_ALIGN_CACHE, 0); 2099fc3fcacfSRobert Watson if (unp_zone == NULL) 210098271db4SGarrett Wollman panic("unp_init"); 21014f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets); 21026e0b6746SPawel Jakub Dawidek uma_zone_set_warning(unp_zone, "kern.ipc.maxsockets limit reached"); 21034f590175SPaul Saab EVENTHANDLER_REGISTER(maxsockets_change, unp_zone_change, 21044f590175SPaul Saab NULL, EVENTHANDLER_PRI_ANY); 210598271db4SGarrett Wollman LIST_INIT(&unp_dhead); 210698271db4SGarrett Wollman LIST_INIT(&unp_shead); 210784d61770SRobert Watson LIST_INIT(&unp_sphead); 21080cb64678SKonstantin Belousov SLIST_INIT(&unp_defers); 2109daee0f0bSKonstantin Belousov TIMEOUT_TASK_INIT(taskqueue_thread, &unp_gc_task, 0, unp_gc, NULL); 21100cb64678SKonstantin Belousov TASK_INIT(&unp_defer_task, 0, unp_process_defers, NULL); 21113dab55bcSRobert Watson UNP_LINK_LOCK_INIT(); 21120cb64678SKonstantin Belousov UNP_DEFERRED_LOCK_INIT(); 211398271db4SGarrett Wollman } 211498271db4SGarrett Wollman 211547c3450eSKonstantin Belousov static void 211647c3450eSKonstantin Belousov unp_internalize_cleanup_rights(struct mbuf *control) 211747c3450eSKonstantin Belousov { 211847c3450eSKonstantin Belousov struct cmsghdr *cp; 211947c3450eSKonstantin Belousov struct mbuf *m; 212047c3450eSKonstantin Belousov void *data; 212147c3450eSKonstantin Belousov socklen_t datalen; 212247c3450eSKonstantin Belousov 212347c3450eSKonstantin Belousov for (m = control; m != NULL; m = m->m_next) { 212447c3450eSKonstantin Belousov cp = mtod(m, struct cmsghdr *); 212547c3450eSKonstantin Belousov if (cp->cmsg_level != SOL_SOCKET || 212647c3450eSKonstantin Belousov cp->cmsg_type != SCM_RIGHTS) 212747c3450eSKonstantin Belousov continue; 212847c3450eSKonstantin Belousov data = CMSG_DATA(cp); 212947c3450eSKonstantin Belousov datalen = (caddr_t)cp + cp->cmsg_len - (caddr_t)data; 213047c3450eSKonstantin Belousov unp_freerights(data, datalen / sizeof(struct filedesc *)); 213147c3450eSKonstantin Belousov } 213247c3450eSKonstantin Belousov } 213347c3450eSKonstantin Belousov 2134f708ef1bSPoul-Henning Kamp static int 2135892af6b9SRobert Watson unp_internalize(struct mbuf **controlp, struct thread *td) 2136df8bae1dSRodney W. Grimes { 213747c3450eSKonstantin Belousov struct mbuf *control, **initial_controlp; 213847c3450eSKonstantin Belousov struct proc *p; 213947c3450eSKonstantin Belousov struct filedesc *fdesc; 2140ab15d803SSergey Kandaurov struct bintime *bt; 214147c3450eSKonstantin Belousov struct cmsghdr *cm; 21422bc21ed9SDavid Malone struct cmsgcred *cmcred; 21438cb539f1SPawel Jakub Dawidek struct filedescent *fde, **fdep, *fdev; 21442bc21ed9SDavid Malone struct file *fp; 21452bc21ed9SDavid Malone struct timeval *tv; 2146339efd75SMaxim Sobolev struct timespec *ts; 21472bc21ed9SDavid Malone void *data; 214847c3450eSKonstantin Belousov socklen_t clen, datalen; 2149f1cf2b9dSKonstantin Belousov int i, j, error, *fdp, oldfds; 21508692c025SYoshinobu Inoue u_int newlen; 2151df8bae1dSRodney W. Grimes 21523dab55bcSRobert Watson UNP_LINK_UNLOCK_ASSERT(); 21534c5bc1caSRobert Watson 215447c3450eSKonstantin Belousov p = td->td_proc; 215547c3450eSKonstantin Belousov fdesc = p->p_fd; 21562bc21ed9SDavid Malone error = 0; 215747c3450eSKonstantin Belousov control = *controlp; 215847c3450eSKonstantin Belousov clen = control->m_len; 21592bc21ed9SDavid Malone *controlp = NULL; 216047c3450eSKonstantin Belousov initial_controlp = controlp; 216147c3450eSKonstantin Belousov for (cm = mtod(control, struct cmsghdr *); cm != NULL;) { 21622bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 2163de966666SMateusz Guzik || cm->cmsg_len > clen || cm->cmsg_len < sizeof(*cm)) { 21642bc21ed9SDavid Malone error = EINVAL; 21652bc21ed9SDavid Malone goto out; 21662bc21ed9SDavid Malone } 21672bc21ed9SDavid Malone data = CMSG_DATA(cm); 21682bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 21692bc21ed9SDavid Malone 21702bc21ed9SDavid Malone switch (cm->cmsg_type) { 21710b788fa1SBill Paul /* 21720b788fa1SBill Paul * Fill in credential information. 21730b788fa1SBill Paul */ 21742bc21ed9SDavid Malone case SCM_CREDS: 21752bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 21762bc21ed9SDavid Malone SCM_CREDS, SOL_SOCKET); 21772bc21ed9SDavid Malone if (*controlp == NULL) { 21782bc21ed9SDavid Malone error = ENOBUFS; 21792bc21ed9SDavid Malone goto out; 21802bc21ed9SDavid Malone } 21812bc21ed9SDavid Malone cmcred = (struct cmsgcred *) 21822bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 21830b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 2184a854ed98SJohn Baldwin cmcred->cmcred_uid = td->td_ucred->cr_ruid; 2185a854ed98SJohn Baldwin cmcred->cmcred_gid = td->td_ucred->cr_rgid; 2186a854ed98SJohn Baldwin cmcred->cmcred_euid = td->td_ucred->cr_uid; 2187a854ed98SJohn Baldwin cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 21880b788fa1SBill Paul CMGROUP_MAX); 21890b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 21902bc21ed9SDavid Malone cmcred->cmcred_groups[i] = 2191a854ed98SJohn Baldwin td->td_ucred->cr_groups[i]; 21922bc21ed9SDavid Malone break; 21930b788fa1SBill Paul 21942bc21ed9SDavid Malone case SCM_RIGHTS: 21952bc21ed9SDavid Malone oldfds = datalen / sizeof (int); 219682e825c4SGleb Smirnoff if (oldfds == 0) 219782e825c4SGleb Smirnoff break; 2198ed5b7817SJulian Elischer /* 21991c381b19SRobert Watson * Check that all the FDs passed in refer to legal 22001c381b19SRobert Watson * files. If not, reject the entire operation. 2201ed5b7817SJulian Elischer */ 22022bc21ed9SDavid Malone fdp = data; 22032609222aSPawel Jakub Dawidek FILEDESC_SLOCK(fdesc); 22046a1cf96bSMateusz Guzik for (i = 0; i < oldfds; i++, fdp++) { 22056a1cf96bSMateusz Guzik fp = fget_locked(fdesc, *fdp); 22066a1cf96bSMateusz Guzik if (fp == NULL) { 22072609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdesc); 22082bc21ed9SDavid Malone error = EBADF; 22092bc21ed9SDavid Malone goto out; 22102bc21ed9SDavid Malone } 2211e7d6662fSAlfred Perlstein if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 22122609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdesc); 2213e7d6662fSAlfred Perlstein error = EOPNOTSUPP; 2214e7d6662fSAlfred Perlstein goto out; 2215e7d6662fSAlfred Perlstein } 2216e7d6662fSAlfred Perlstein 2217df8bae1dSRodney W. Grimes } 22185e3f7694SRobert Watson 2219ed5b7817SJulian Elischer /* 22200b36cd25SRobert Watson * Now replace the integer FDs with pointers to the 22212609222aSPawel Jakub Dawidek * file structure and capability rights. 2222ed5b7817SJulian Elischer */ 22238cb539f1SPawel Jakub Dawidek newlen = oldfds * sizeof(fdep[0]); 22242bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 22252bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 22262bc21ed9SDavid Malone if (*controlp == NULL) { 22272609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdesc); 22282bc21ed9SDavid Malone error = E2BIG; 22292bc21ed9SDavid Malone goto out; 22308692c025SYoshinobu Inoue } 22312bc21ed9SDavid Malone fdp = data; 2232f1cf2b9dSKonstantin Belousov for (i = 0; i < oldfds; i++, fdp++) { 2233f1cf2b9dSKonstantin Belousov if (!fhold(fdesc->fd_ofiles[*fdp].fde_file)) { 2234f1cf2b9dSKonstantin Belousov fdp = data; 2235f1cf2b9dSKonstantin Belousov for (j = 0; j < i; j++, fdp++) { 2236f1cf2b9dSKonstantin Belousov fdrop(fdesc->fd_ofiles[*fdp]. 2237f1cf2b9dSKonstantin Belousov fde_file, td); 2238f1cf2b9dSKonstantin Belousov } 2239f1cf2b9dSKonstantin Belousov FILEDESC_SUNLOCK(fdesc); 2240f1cf2b9dSKonstantin Belousov error = EBADF; 2241f1cf2b9dSKonstantin Belousov goto out; 2242f1cf2b9dSKonstantin Belousov } 2243f1cf2b9dSKonstantin Belousov } 2244f1cf2b9dSKonstantin Belousov fdp = data; 22458cb539f1SPawel Jakub Dawidek fdep = (struct filedescent **) 22462bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 22478cb539f1SPawel Jakub Dawidek fdev = malloc(sizeof(*fdev) * oldfds, M_FILECAPS, 22488cb539f1SPawel Jakub Dawidek M_WAITOK); 22498cb539f1SPawel Jakub Dawidek for (i = 0; i < oldfds; i++, fdev++, fdp++) { 22502609222aSPawel Jakub Dawidek fde = &fdesc->fd_ofiles[*fdp]; 22518cb539f1SPawel Jakub Dawidek fdep[i] = fdev; 22528cb539f1SPawel Jakub Dawidek fdep[i]->fde_file = fde->fde_file; 22538cb539f1SPawel Jakub Dawidek filecaps_copy(&fde->fde_caps, 2254d7832811SMateusz Guzik &fdep[i]->fde_caps, true); 22558cb539f1SPawel Jakub Dawidek unp_internalize_fp(fdep[i]->fde_file); 2256df8bae1dSRodney W. Grimes } 22572609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdesc); 22582bc21ed9SDavid Malone break; 22592bc21ed9SDavid Malone 22602bc21ed9SDavid Malone case SCM_TIMESTAMP: 22612bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*tv), 22622bc21ed9SDavid Malone SCM_TIMESTAMP, SOL_SOCKET); 22632bc21ed9SDavid Malone if (*controlp == NULL) { 22642bc21ed9SDavid Malone error = ENOBUFS; 22652bc21ed9SDavid Malone goto out; 22668692c025SYoshinobu Inoue } 22672bc21ed9SDavid Malone tv = (struct timeval *) 22682bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 22692bc21ed9SDavid Malone microtime(tv); 22702bc21ed9SDavid Malone break; 22712bc21ed9SDavid Malone 2272ab15d803SSergey Kandaurov case SCM_BINTIME: 2273ab15d803SSergey Kandaurov *controlp = sbcreatecontrol(NULL, sizeof(*bt), 2274ab15d803SSergey Kandaurov SCM_BINTIME, SOL_SOCKET); 2275ab15d803SSergey Kandaurov if (*controlp == NULL) { 2276ab15d803SSergey Kandaurov error = ENOBUFS; 2277ab15d803SSergey Kandaurov goto out; 2278ab15d803SSergey Kandaurov } 2279ab15d803SSergey Kandaurov bt = (struct bintime *) 2280ab15d803SSergey Kandaurov CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2281ab15d803SSergey Kandaurov bintime(bt); 2282ab15d803SSergey Kandaurov break; 2283ab15d803SSergey Kandaurov 2284339efd75SMaxim Sobolev case SCM_REALTIME: 2285339efd75SMaxim Sobolev *controlp = sbcreatecontrol(NULL, sizeof(*ts), 2286339efd75SMaxim Sobolev SCM_REALTIME, SOL_SOCKET); 2287339efd75SMaxim Sobolev if (*controlp == NULL) { 2288339efd75SMaxim Sobolev error = ENOBUFS; 2289339efd75SMaxim Sobolev goto out; 2290339efd75SMaxim Sobolev } 2291339efd75SMaxim Sobolev ts = (struct timespec *) 2292339efd75SMaxim Sobolev CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2293339efd75SMaxim Sobolev nanotime(ts); 2294339efd75SMaxim Sobolev break; 2295339efd75SMaxim Sobolev 2296339efd75SMaxim Sobolev case SCM_MONOTONIC: 2297339efd75SMaxim Sobolev *controlp = sbcreatecontrol(NULL, sizeof(*ts), 2298339efd75SMaxim Sobolev SCM_MONOTONIC, SOL_SOCKET); 2299339efd75SMaxim Sobolev if (*controlp == NULL) { 2300339efd75SMaxim Sobolev error = ENOBUFS; 2301339efd75SMaxim Sobolev goto out; 2302339efd75SMaxim Sobolev } 2303339efd75SMaxim Sobolev ts = (struct timespec *) 2304339efd75SMaxim Sobolev CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2305339efd75SMaxim Sobolev nanouptime(ts); 2306339efd75SMaxim Sobolev break; 2307339efd75SMaxim Sobolev 23082bc21ed9SDavid Malone default: 23092bc21ed9SDavid Malone error = EINVAL; 23102bc21ed9SDavid Malone goto out; 23112bc21ed9SDavid Malone } 23122bc21ed9SDavid Malone 23134013d726SMark Johnston if (*controlp != NULL) 23142bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 23152bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 23162bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 23172bc21ed9SDavid Malone cm = (struct cmsghdr *) 23182bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 23192bc21ed9SDavid Malone } else { 23202bc21ed9SDavid Malone clen = 0; 23212bc21ed9SDavid Malone cm = NULL; 23222bc21ed9SDavid Malone } 23232bc21ed9SDavid Malone } 23242bc21ed9SDavid Malone 23252bc21ed9SDavid Malone out: 232647c3450eSKonstantin Belousov if (error != 0 && initial_controlp != NULL) 232747c3450eSKonstantin Belousov unp_internalize_cleanup_rights(*initial_controlp); 23282bc21ed9SDavid Malone m_freem(control); 23292bc21ed9SDavid Malone return (error); 2330df8bae1dSRodney W. Grimes } 2331df8bae1dSRodney W. Grimes 23325b950deaSRobert Watson static struct mbuf * 23336a2989fdSMatthew N. Dodd unp_addsockcred(struct thread *td, struct mbuf *control) 23346a2989fdSMatthew N. Dodd { 233570df31f4SMaxim Konovalov struct mbuf *m, *n, *n_prev; 23366a2989fdSMatthew N. Dodd struct sockcred *sc; 233770df31f4SMaxim Konovalov const struct cmsghdr *cm; 23386a2989fdSMatthew N. Dodd int ngroups; 23396a2989fdSMatthew N. Dodd int i; 23406a2989fdSMatthew N. Dodd 23416a2989fdSMatthew N. Dodd ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX); 23426a2989fdSMatthew N. Dodd m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET); 23436a2989fdSMatthew N. Dodd if (m == NULL) 23446a2989fdSMatthew N. Dodd return (control); 23456a2989fdSMatthew N. Dodd 23466a2989fdSMatthew N. Dodd sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *)); 23476a2989fdSMatthew N. Dodd sc->sc_uid = td->td_ucred->cr_ruid; 23486a2989fdSMatthew N. Dodd sc->sc_euid = td->td_ucred->cr_uid; 23496a2989fdSMatthew N. Dodd sc->sc_gid = td->td_ucred->cr_rgid; 23506a2989fdSMatthew N. Dodd sc->sc_egid = td->td_ucred->cr_gid; 23516a2989fdSMatthew N. Dodd sc->sc_ngroups = ngroups; 23526a2989fdSMatthew N. Dodd for (i = 0; i < sc->sc_ngroups; i++) 23536a2989fdSMatthew N. Dodd sc->sc_groups[i] = td->td_ucred->cr_groups[i]; 23546a2989fdSMatthew N. Dodd 23556a2989fdSMatthew N. Dodd /* 23561c381b19SRobert Watson * Unlink SCM_CREDS control messages (struct cmsgcred), since just 23571c381b19SRobert Watson * created SCM_CREDS control message (struct sockcred) has another 23581c381b19SRobert Watson * format. 23596a2989fdSMatthew N. Dodd */ 236070df31f4SMaxim Konovalov if (control != NULL) 236170df31f4SMaxim Konovalov for (n = control, n_prev = NULL; n != NULL;) { 236270df31f4SMaxim Konovalov cm = mtod(n, struct cmsghdr *); 236370df31f4SMaxim Konovalov if (cm->cmsg_level == SOL_SOCKET && 236470df31f4SMaxim Konovalov cm->cmsg_type == SCM_CREDS) { 236570df31f4SMaxim Konovalov if (n_prev == NULL) 236670df31f4SMaxim Konovalov control = n->m_next; 236770df31f4SMaxim Konovalov else 236870df31f4SMaxim Konovalov n_prev->m_next = n->m_next; 236970df31f4SMaxim Konovalov n = m_free(n); 237070df31f4SMaxim Konovalov } else { 237170df31f4SMaxim Konovalov n_prev = n; 237270df31f4SMaxim Konovalov n = n->m_next; 237370df31f4SMaxim Konovalov } 237470df31f4SMaxim Konovalov } 23756a2989fdSMatthew N. Dodd 237670df31f4SMaxim Konovalov /* Prepend it to the head. */ 237770df31f4SMaxim Konovalov m->m_next = control; 237870df31f4SMaxim Konovalov return (m); 23796a2989fdSMatthew N. Dodd } 23806a2989fdSMatthew N. Dodd 2381397c19d1SJeff Roberson static struct unpcb * 2382397c19d1SJeff Roberson fptounp(struct file *fp) 2383397c19d1SJeff Roberson { 2384397c19d1SJeff Roberson struct socket *so; 2385397c19d1SJeff Roberson 2386397c19d1SJeff Roberson if (fp->f_type != DTYPE_SOCKET) 2387397c19d1SJeff Roberson return (NULL); 2388397c19d1SJeff Roberson if ((so = fp->f_data) == NULL) 2389397c19d1SJeff Roberson return (NULL); 2390397c19d1SJeff Roberson if (so->so_proto->pr_domain != &localdomain) 2391397c19d1SJeff Roberson return (NULL); 2392397c19d1SJeff Roberson return sotounpcb(so); 2393397c19d1SJeff Roberson } 2394397c19d1SJeff Roberson 2395397c19d1SJeff Roberson static void 2396397c19d1SJeff Roberson unp_discard(struct file *fp) 2397397c19d1SJeff Roberson { 23980cb64678SKonstantin Belousov struct unp_defer *dr; 2399397c19d1SJeff Roberson 24000cb64678SKonstantin Belousov if (unp_externalize_fp(fp)) { 24010cb64678SKonstantin Belousov dr = malloc(sizeof(*dr), M_TEMP, M_WAITOK); 24020cb64678SKonstantin Belousov dr->ud_fp = fp; 24030cb64678SKonstantin Belousov UNP_DEFERRED_LOCK(); 24040cb64678SKonstantin Belousov SLIST_INSERT_HEAD(&unp_defers, dr, ud_link); 24050cb64678SKonstantin Belousov UNP_DEFERRED_UNLOCK(); 24060cb64678SKonstantin Belousov atomic_add_int(&unp_defers_count, 1); 24070cb64678SKonstantin Belousov taskqueue_enqueue(taskqueue_thread, &unp_defer_task); 24080cb64678SKonstantin Belousov } else 2409397c19d1SJeff Roberson (void) closef(fp, (struct thread *)NULL); 2410397c19d1SJeff Roberson } 2411397c19d1SJeff Roberson 2412397c19d1SJeff Roberson static void 24130cb64678SKonstantin Belousov unp_process_defers(void *arg __unused, int pending) 24140cb64678SKonstantin Belousov { 24150cb64678SKonstantin Belousov struct unp_defer *dr; 24160cb64678SKonstantin Belousov SLIST_HEAD(, unp_defer) drl; 24170cb64678SKonstantin Belousov int count; 24180cb64678SKonstantin Belousov 24190cb64678SKonstantin Belousov SLIST_INIT(&drl); 24200cb64678SKonstantin Belousov for (;;) { 24210cb64678SKonstantin Belousov UNP_DEFERRED_LOCK(); 24220cb64678SKonstantin Belousov if (SLIST_FIRST(&unp_defers) == NULL) { 24230cb64678SKonstantin Belousov UNP_DEFERRED_UNLOCK(); 24240cb64678SKonstantin Belousov break; 24250cb64678SKonstantin Belousov } 24260cb64678SKonstantin Belousov SLIST_SWAP(&unp_defers, &drl, unp_defer); 24270cb64678SKonstantin Belousov UNP_DEFERRED_UNLOCK(); 24280cb64678SKonstantin Belousov count = 0; 24290cb64678SKonstantin Belousov while ((dr = SLIST_FIRST(&drl)) != NULL) { 24300cb64678SKonstantin Belousov SLIST_REMOVE_HEAD(&drl, ud_link); 24310cb64678SKonstantin Belousov closef(dr->ud_fp, NULL); 24320cb64678SKonstantin Belousov free(dr, M_TEMP); 24330cb64678SKonstantin Belousov count++; 24340cb64678SKonstantin Belousov } 24350cb64678SKonstantin Belousov atomic_add_int(&unp_defers_count, -count); 24360cb64678SKonstantin Belousov } 24370cb64678SKonstantin Belousov } 24380cb64678SKonstantin Belousov 24390cb64678SKonstantin Belousov static void 2440397c19d1SJeff Roberson unp_internalize_fp(struct file *fp) 2441397c19d1SJeff Roberson { 2442397c19d1SJeff Roberson struct unpcb *unp; 2443397c19d1SJeff Roberson 24443dab55bcSRobert Watson UNP_LINK_WLOCK(); 2445397c19d1SJeff Roberson if ((unp = fptounp(fp)) != NULL) { 2446397c19d1SJeff Roberson unp->unp_file = fp; 2447397c19d1SJeff Roberson unp->unp_msgcount++; 2448397c19d1SJeff Roberson } 2449397c19d1SJeff Roberson unp_rights++; 24503dab55bcSRobert Watson UNP_LINK_WUNLOCK(); 2451397c19d1SJeff Roberson } 2452397c19d1SJeff Roberson 24530cb64678SKonstantin Belousov static int 2454397c19d1SJeff Roberson unp_externalize_fp(struct file *fp) 2455397c19d1SJeff Roberson { 2456397c19d1SJeff Roberson struct unpcb *unp; 24570cb64678SKonstantin Belousov int ret; 2458397c19d1SJeff Roberson 24593dab55bcSRobert Watson UNP_LINK_WLOCK(); 24600cb64678SKonstantin Belousov if ((unp = fptounp(fp)) != NULL) { 2461397c19d1SJeff Roberson unp->unp_msgcount--; 24620cb64678SKonstantin Belousov ret = 1; 24630cb64678SKonstantin Belousov } else 24640cb64678SKonstantin Belousov ret = 0; 2465397c19d1SJeff Roberson unp_rights--; 24663dab55bcSRobert Watson UNP_LINK_WUNLOCK(); 24670cb64678SKonstantin Belousov return (ret); 2468397c19d1SJeff Roberson } 2469397c19d1SJeff Roberson 2470161a0c7cSRobert Watson /* 2471a0ec558aSRobert Watson * unp_defer indicates whether additional work has been defered for a future 2472a0ec558aSRobert Watson * pass through unp_gc(). It is thread local and does not require explicit 2473a0ec558aSRobert Watson * synchronization. 2474161a0c7cSRobert Watson */ 2475397c19d1SJeff Roberson static int unp_marked; 2476a0ec558aSRobert Watson 2477397c19d1SJeff Roberson static void 2478a9aa06f7SJason A. Harmening unp_remove_dead_ref(struct filedescent **fdep, int fdcount) 2479397c19d1SJeff Roberson { 2480397c19d1SJeff Roberson struct unpcb *unp; 2481be26ba7cSPawel Jakub Dawidek struct file *fp; 2482be26ba7cSPawel Jakub Dawidek int i; 2483397c19d1SJeff Roberson 2484a9aa06f7SJason A. Harmening /* 2485a9aa06f7SJason A. Harmening * This function can only be called from the gc task. 2486a9aa06f7SJason A. Harmening */ 2487a9aa06f7SJason A. Harmening KASSERT(taskqueue_member(taskqueue_thread, curthread) != 0, 2488a9aa06f7SJason A. Harmening ("%s: not on gc callout", __func__)); 2489a9aa06f7SJason A. Harmening UNP_LINK_LOCK_ASSERT(); 2490a9aa06f7SJason A. Harmening 2491be26ba7cSPawel Jakub Dawidek for (i = 0; i < fdcount; i++) { 2492be26ba7cSPawel Jakub Dawidek fp = fdep[i]->fde_file; 24936f552cb0SJeff Roberson if ((unp = fptounp(fp)) == NULL) 2494be26ba7cSPawel Jakub Dawidek continue; 2495a9aa06f7SJason A. Harmening if ((unp->unp_gcflag & UNPGC_DEAD) == 0) 2496be26ba7cSPawel Jakub Dawidek continue; 2497a9aa06f7SJason A. Harmening unp->unp_gcrefs--; 2498a9aa06f7SJason A. Harmening } 2499a9aa06f7SJason A. Harmening } 2500a9aa06f7SJason A. Harmening 2501a9aa06f7SJason A. Harmening static void 2502a9aa06f7SJason A. Harmening unp_restore_undead_ref(struct filedescent **fdep, int fdcount) 2503a9aa06f7SJason A. Harmening { 2504a9aa06f7SJason A. Harmening struct unpcb *unp; 2505a9aa06f7SJason A. Harmening struct file *fp; 2506a9aa06f7SJason A. Harmening int i; 2507a9aa06f7SJason A. Harmening 2508a9aa06f7SJason A. Harmening /* 2509a9aa06f7SJason A. Harmening * This function can only be called from the gc task. 2510a9aa06f7SJason A. Harmening */ 2511a9aa06f7SJason A. Harmening KASSERT(taskqueue_member(taskqueue_thread, curthread) != 0, 2512a9aa06f7SJason A. Harmening ("%s: not on gc callout", __func__)); 2513a9aa06f7SJason A. Harmening UNP_LINK_LOCK_ASSERT(); 2514a9aa06f7SJason A. Harmening 2515a9aa06f7SJason A. Harmening for (i = 0; i < fdcount; i++) { 2516a9aa06f7SJason A. Harmening fp = fdep[i]->fde_file; 2517a9aa06f7SJason A. Harmening if ((unp = fptounp(fp)) == NULL) 2518a9aa06f7SJason A. Harmening continue; 2519a9aa06f7SJason A. Harmening if ((unp->unp_gcflag & UNPGC_DEAD) == 0) 2520a9aa06f7SJason A. Harmening continue; 2521a9aa06f7SJason A. Harmening unp->unp_gcrefs++; 2522397c19d1SJeff Roberson unp_marked++; 2523397c19d1SJeff Roberson } 2524be26ba7cSPawel Jakub Dawidek } 2525397c19d1SJeff Roberson 2526397c19d1SJeff Roberson static void 2527a9aa06f7SJason A. Harmening unp_gc_scan(struct unpcb *unp, void (*op)(struct filedescent **, int)) 2528397c19d1SJeff Roberson { 2529779f106aSGleb Smirnoff struct socket *so, *soa; 253060a5ef26SRobert Watson 2531397c19d1SJeff Roberson so = unp->unp_socket; 2532779f106aSGleb Smirnoff SOCK_LOCK(so); 2533779f106aSGleb Smirnoff if (SOLISTENING(so)) { 2534397c19d1SJeff Roberson /* 2535397c19d1SJeff Roberson * Mark all sockets in our accept queue. 2536397c19d1SJeff Roberson */ 2537779f106aSGleb Smirnoff TAILQ_FOREACH(soa, &so->sol_comp, so_list) { 2538779f106aSGleb Smirnoff if (sotounpcb(soa)->unp_gcflag & UNPGC_IGNORE_RIGHTS) 25390c40f353SConrad Meyer continue; 2540397c19d1SJeff Roberson SOCKBUF_LOCK(&soa->so_rcv); 2541a9aa06f7SJason A. Harmening unp_scan(soa->so_rcv.sb_mb, op); 2542397c19d1SJeff Roberson SOCKBUF_UNLOCK(&soa->so_rcv); 2543397c19d1SJeff Roberson } 2544779f106aSGleb Smirnoff } else { 2545779f106aSGleb Smirnoff /* 2546779f106aSGleb Smirnoff * Mark all sockets we reference with RIGHTS. 2547779f106aSGleb Smirnoff */ 2548779f106aSGleb Smirnoff if ((unp->unp_gcflag & UNPGC_IGNORE_RIGHTS) == 0) { 2549779f106aSGleb Smirnoff SOCKBUF_LOCK(&so->so_rcv); 2550a9aa06f7SJason A. Harmening unp_scan(so->so_rcv.sb_mb, op); 2551779f106aSGleb Smirnoff SOCKBUF_UNLOCK(&so->so_rcv); 2552779f106aSGleb Smirnoff } 2553779f106aSGleb Smirnoff } 2554779f106aSGleb Smirnoff SOCK_UNLOCK(so); 2555397c19d1SJeff Roberson } 2556a0ec558aSRobert Watson 2557a0ec558aSRobert Watson static int unp_recycled; 2558be6b1304STom Rhodes SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0, 2559be6b1304STom Rhodes "Number of unreachable sockets claimed by the garbage collector."); 2560df8bae1dSRodney W. Grimes 2561397c19d1SJeff Roberson static int unp_taskcount; 2562be6b1304STom Rhodes SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0, 2563be6b1304STom Rhodes "Number of times the garbage collector has run."); 2564397c19d1SJeff Roberson 2565a9aa06f7SJason A. Harmening SYSCTL_UINT(_net_local, OID_AUTO, sockcount, CTLFLAG_RD, &unp_count, 0, 2566a9aa06f7SJason A. Harmening "Number of active local sockets."); 2567a9aa06f7SJason A. Harmening 2568f708ef1bSPoul-Henning Kamp static void 2569a0ec558aSRobert Watson unp_gc(__unused void *arg, int pending) 2570df8bae1dSRodney W. Grimes { 257184d61770SRobert Watson struct unp_head *heads[] = { &unp_dhead, &unp_shead, &unp_sphead, 257284d61770SRobert Watson NULL }; 2573397c19d1SJeff Roberson struct unp_head **head; 2574a9aa06f7SJason A. Harmening struct unp_head unp_deadhead; /* List of potentially-dead sockets. */ 2575f7780c61SKonstantin Belousov struct file *f, **unref; 2576a9aa06f7SJason A. Harmening struct unpcb *unp, *unptmp; 2577a9aa06f7SJason A. Harmening int i, total, unp_unreachable; 2578df8bae1dSRodney W. Grimes 2579a9aa06f7SJason A. Harmening LIST_INIT(&unp_deadhead); 2580a0ec558aSRobert Watson unp_taskcount++; 2581779f106aSGleb Smirnoff UNP_LINK_RLOCK(); 2582ed5b7817SJulian Elischer /* 2583a9aa06f7SJason A. Harmening * First determine which sockets may be in cycles. 2584ed5b7817SJulian Elischer */ 2585a9aa06f7SJason A. Harmening unp_unreachable = 0; 2586a9aa06f7SJason A. Harmening 2587397c19d1SJeff Roberson for (head = heads; *head != NULL; head++) 2588a9aa06f7SJason A. Harmening LIST_FOREACH(unp, *head, unp_link) { 2589a9aa06f7SJason A. Harmening 2590a9aa06f7SJason A. Harmening KASSERT((unp->unp_gcflag & ~UNPGC_IGNORE_RIGHTS) == 0, 2591a9aa06f7SJason A. Harmening ("%s: unp %p has unexpected gc flags 0x%x", 2592a9aa06f7SJason A. Harmening __func__, unp, (unsigned int)unp->unp_gcflag)); 2593a9aa06f7SJason A. Harmening 2594a9aa06f7SJason A. Harmening f = unp->unp_file; 259560a5ef26SRobert Watson 2596397c19d1SJeff Roberson /* 2597a9aa06f7SJason A. Harmening * Check for an unreachable socket potentially in a 2598a9aa06f7SJason A. Harmening * cycle. It must be in a queue as indicated by 2599a9aa06f7SJason A. Harmening * msgcount, and this must equal the file reference 2600a9aa06f7SJason A. Harmening * count. Note that when msgcount is 0 the file is 2601a9aa06f7SJason A. Harmening * NULL. 2602a9aa06f7SJason A. Harmening */ 2603a9aa06f7SJason A. Harmening if (f != NULL && unp->unp_msgcount != 0 && 2604a9aa06f7SJason A. Harmening f->f_count == unp->unp_msgcount) { 2605a9aa06f7SJason A. Harmening LIST_INSERT_HEAD(&unp_deadhead, unp, unp_dead); 2606a9aa06f7SJason A. Harmening unp->unp_gcflag |= UNPGC_DEAD; 2607a9aa06f7SJason A. Harmening unp->unp_gcrefs = unp->unp_msgcount; 2608a9aa06f7SJason A. Harmening unp_unreachable++; 2609a9aa06f7SJason A. Harmening } 2610a9aa06f7SJason A. Harmening } 2611a9aa06f7SJason A. Harmening 2612a9aa06f7SJason A. Harmening /* 2613a9aa06f7SJason A. Harmening * Scan all sockets previously marked as potentially being in a cycle 2614a9aa06f7SJason A. Harmening * and remove the references each socket holds on any UNPGC_DEAD 2615a9aa06f7SJason A. Harmening * sockets in its queue. After this step, all remaining references on 2616a9aa06f7SJason A. Harmening * sockets marked UNPGC_DEAD should not be part of any cycle. 2617a9aa06f7SJason A. Harmening */ 2618a9aa06f7SJason A. Harmening LIST_FOREACH(unp, &unp_deadhead, unp_dead) 2619a9aa06f7SJason A. Harmening unp_gc_scan(unp, unp_remove_dead_ref); 2620a9aa06f7SJason A. Harmening 2621a9aa06f7SJason A. Harmening /* 2622a9aa06f7SJason A. Harmening * If a socket still has a non-negative refcount, it cannot be in a 2623a9aa06f7SJason A. Harmening * cycle. In this case increment refcount of all children iteratively. 2624397c19d1SJeff Roberson * Stop the scan once we do a complete loop without discovering 2625397c19d1SJeff Roberson * a new reachable socket. 2626397c19d1SJeff Roberson */ 2627df8bae1dSRodney W. Grimes do { 2628397c19d1SJeff Roberson unp_marked = 0; 2629a9aa06f7SJason A. Harmening LIST_FOREACH_SAFE(unp, &unp_deadhead, unp_dead, unptmp) 2630a9aa06f7SJason A. Harmening if (unp->unp_gcrefs > 0) { 2631a9aa06f7SJason A. Harmening unp->unp_gcflag &= ~UNPGC_DEAD; 2632a9aa06f7SJason A. Harmening LIST_REMOVE(unp, unp_dead); 2633a9aa06f7SJason A. Harmening KASSERT(unp_unreachable > 0, 2634a9aa06f7SJason A. Harmening ("%s: unp_unreachable underflow.", 2635a9aa06f7SJason A. Harmening __func__)); 2636a9aa06f7SJason A. Harmening unp_unreachable--; 2637a9aa06f7SJason A. Harmening unp_gc_scan(unp, unp_restore_undead_ref); 2638a9aa06f7SJason A. Harmening } 2639397c19d1SJeff Roberson } while (unp_marked); 2640a9aa06f7SJason A. Harmening 2641779f106aSGleb Smirnoff UNP_LINK_RUNLOCK(); 2642a9aa06f7SJason A. Harmening 2643397c19d1SJeff Roberson if (unp_unreachable == 0) 2644397c19d1SJeff Roberson return; 264560a5ef26SRobert Watson 2646ed5b7817SJulian Elischer /* 2647a9aa06f7SJason A. Harmening * Allocate space for a local array of dead unpcbs. 2648a9aa06f7SJason A. Harmening * TODO: can this path be simplified by instead using the local 2649a9aa06f7SJason A. Harmening * dead list at unp_deadhead, after taking out references 2650a9aa06f7SJason A. Harmening * on the file object and/or unpcb and dropping the link lock? 2651ed5b7817SJulian Elischer */ 2652397c19d1SJeff Roberson unref = malloc(unp_unreachable * sizeof(struct file *), 2653397c19d1SJeff Roberson M_TEMP, M_WAITOK); 265460a5ef26SRobert Watson 2655ed5b7817SJulian Elischer /* 2656397c19d1SJeff Roberson * Iterate looking for sockets which have been specifically marked 2657a9aa06f7SJason A. Harmening * as unreachable and store them locally. 2658ed5b7817SJulian Elischer */ 2659f7780c61SKonstantin Belousov UNP_LINK_RLOCK(); 2660a9aa06f7SJason A. Harmening total = 0; 2661a9aa06f7SJason A. Harmening LIST_FOREACH(unp, &unp_deadhead, unp_dead) { 2662a9aa06f7SJason A. Harmening KASSERT((unp->unp_gcflag & UNPGC_DEAD) != 0, 2663a9aa06f7SJason A. Harmening ("%s: unp %p not marked UNPGC_DEAD", __func__, unp)); 2664a9aa06f7SJason A. Harmening unp->unp_gcflag &= ~UNPGC_DEAD; 2665f7780c61SKonstantin Belousov f = unp->unp_file; 2666f7780c61SKonstantin Belousov if (unp->unp_msgcount == 0 || f == NULL || 2667f1cf2b9dSKonstantin Belousov f->f_count != unp->unp_msgcount || 2668f1cf2b9dSKonstantin Belousov !fhold(f)) 2669f7780c61SKonstantin Belousov continue; 2670f7780c61SKonstantin Belousov unref[total++] = f; 2671f7780c61SKonstantin Belousov KASSERT(total <= unp_unreachable, 2672a9aa06f7SJason A. Harmening ("%s: incorrect unreachable count.", __func__)); 2673397c19d1SJeff Roberson } 2674f7780c61SKonstantin Belousov UNP_LINK_RUNLOCK(); 267560a5ef26SRobert Watson 2676ed5b7817SJulian Elischer /* 2677397c19d1SJeff Roberson * Now flush all sockets, free'ing rights. This will free the 2678397c19d1SJeff Roberson * struct files associated with these sockets but leave each socket 2679397c19d1SJeff Roberson * with one remaining ref. 2680ed5b7817SJulian Elischer */ 26811fb51a12SBjoern A. Zeeb for (i = 0; i < total; i++) { 26821fb51a12SBjoern A. Zeeb struct socket *so; 26831fb51a12SBjoern A. Zeeb 26841fb51a12SBjoern A. Zeeb so = unref[i]->f_data; 26851fb51a12SBjoern A. Zeeb CURVNET_SET(so->so_vnet); 26861fb51a12SBjoern A. Zeeb sorflush(so); 26871fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 26881fb51a12SBjoern A. Zeeb } 268960a5ef26SRobert Watson 2690ed5b7817SJulian Elischer /* 2691397c19d1SJeff Roberson * And finally release the sockets so they can be reclaimed. 2692ed5b7817SJulian Elischer */ 2693f7780c61SKonstantin Belousov for (i = 0; i < total; i++) 2694397c19d1SJeff Roberson fdrop(unref[i], NULL); 2695f7780c61SKonstantin Belousov unp_recycled += total; 2696397c19d1SJeff Roberson free(unref, M_TEMP); 2697df8bae1dSRodney W. Grimes } 2698df8bae1dSRodney W. Grimes 26990b36cd25SRobert Watson static void 270099ab95dbSMark Johnston unp_dispose_mbuf(struct mbuf *m) 2701df8bae1dSRodney W. Grimes { 2702996c772fSJohn Dyson 2703df8bae1dSRodney W. Grimes if (m) 2704be26ba7cSPawel Jakub Dawidek unp_scan(m, unp_freerights); 2705df8bae1dSRodney W. Grimes } 2706df8bae1dSRodney W. Grimes 27070c40f353SConrad Meyer /* 27080c40f353SConrad Meyer * Synchronize against unp_gc, which can trip over data as we are freeing it. 27090c40f353SConrad Meyer */ 27100c40f353SConrad Meyer static void 271199ab95dbSMark Johnston unp_dispose(struct socket *so) 27120c40f353SConrad Meyer { 27130c40f353SConrad Meyer struct unpcb *unp; 27140c40f353SConrad Meyer 27150c40f353SConrad Meyer unp = sotounpcb(so); 2716779f106aSGleb Smirnoff UNP_LINK_WLOCK(); 27170c40f353SConrad Meyer unp->unp_gcflag |= UNPGC_IGNORE_RIGHTS; 2718779f106aSGleb Smirnoff UNP_LINK_WUNLOCK(); 2719779f106aSGleb Smirnoff if (!SOLISTENING(so)) 272099ab95dbSMark Johnston unp_dispose_mbuf(so->so_rcv.sb_mb); 27210c40f353SConrad Meyer } 27220c40f353SConrad Meyer 2723f708ef1bSPoul-Henning Kamp static void 2724be26ba7cSPawel Jakub Dawidek unp_scan(struct mbuf *m0, void (*op)(struct filedescent **, int)) 2725df8bae1dSRodney W. Grimes { 27262bc21ed9SDavid Malone struct mbuf *m; 27272bc21ed9SDavid Malone struct cmsghdr *cm; 27282bc21ed9SDavid Malone void *data; 27292bc21ed9SDavid Malone socklen_t clen, datalen; 2730df8bae1dSRodney W. Grimes 2731fc3fcacfSRobert Watson while (m0 != NULL) { 27322bc21ed9SDavid Malone for (m = m0; m; m = m->m_next) { 273312396bdcSDavid Malone if (m->m_type != MT_CONTROL) 2734df8bae1dSRodney W. Grimes continue; 27352bc21ed9SDavid Malone 27362bc21ed9SDavid Malone cm = mtod(m, struct cmsghdr *); 27372bc21ed9SDavid Malone clen = m->m_len; 27382bc21ed9SDavid Malone 27392bc21ed9SDavid Malone while (cm != NULL) { 27402bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) 27412bc21ed9SDavid Malone break; 27422bc21ed9SDavid Malone 27432bc21ed9SDavid Malone data = CMSG_DATA(cm); 27442bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len 27452bc21ed9SDavid Malone - (caddr_t)data; 27462bc21ed9SDavid Malone 27472bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET && 27482bc21ed9SDavid Malone cm->cmsg_type == SCM_RIGHTS) { 2749be26ba7cSPawel Jakub Dawidek (*op)(data, datalen / 2750be26ba7cSPawel Jakub Dawidek sizeof(struct filedescent *)); 27512bc21ed9SDavid Malone } 27522bc21ed9SDavid Malone 27532bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 27542bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 27552bc21ed9SDavid Malone cm = (struct cmsghdr *) 27562bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 27572bc21ed9SDavid Malone } else { 27582bc21ed9SDavid Malone clen = 0; 27592bc21ed9SDavid Malone cm = NULL; 27602bc21ed9SDavid Malone } 27612bc21ed9SDavid Malone } 2762df8bae1dSRodney W. Grimes } 2763c29a3321SKevin Lo m0 = m0->m_nextpkt; 2764df8bae1dSRodney W. Grimes } 2765df8bae1dSRodney W. Grimes } 2766df8bae1dSRodney W. Grimes 2767662c901cSMikolaj Golub /* 2768662c901cSMikolaj Golub * A helper function called by VFS before socket-type vnode reclamation. 2769662c901cSMikolaj Golub * For an active vnode it clears unp_vnode pointer and decrements unp_vnode 2770662c901cSMikolaj Golub * use count. 2771662c901cSMikolaj Golub */ 2772662c901cSMikolaj Golub void 2773662c901cSMikolaj Golub vfs_unp_reclaim(struct vnode *vp) 2774662c901cSMikolaj Golub { 2775662c901cSMikolaj Golub struct unpcb *unp; 2776662c901cSMikolaj Golub int active; 277775a67bf3SMatt Macy struct mtx *vplock; 2778662c901cSMikolaj Golub 2779662c901cSMikolaj Golub ASSERT_VOP_ELOCKED(vp, "vfs_unp_reclaim"); 2780662c901cSMikolaj Golub KASSERT(vp->v_type == VSOCK, 2781662c901cSMikolaj Golub ("vfs_unp_reclaim: vp->v_type != VSOCK")); 2782662c901cSMikolaj Golub 2783662c901cSMikolaj Golub active = 0; 278475a67bf3SMatt Macy vplock = mtx_pool_find(mtxpool_sleep, vp); 278575a67bf3SMatt Macy mtx_lock(vplock); 27860c3c207fSGleb Smirnoff VOP_UNP_CONNECT(vp, &unp); 2787662c901cSMikolaj Golub if (unp == NULL) 2788662c901cSMikolaj Golub goto done; 2789662c901cSMikolaj Golub UNP_PCB_LOCK(unp); 2790c7e41c8bSMikolaj Golub if (unp->unp_vnode == vp) { 2791c7e41c8bSMikolaj Golub VOP_UNP_DETACH(vp); 2792662c901cSMikolaj Golub unp->unp_vnode = NULL; 2793662c901cSMikolaj Golub active = 1; 2794662c901cSMikolaj Golub } 2795662c901cSMikolaj Golub UNP_PCB_UNLOCK(unp); 2796662c901cSMikolaj Golub done: 279775a67bf3SMatt Macy mtx_unlock(vplock); 2798662c901cSMikolaj Golub if (active) 2799662c901cSMikolaj Golub vunref(vp); 2800662c901cSMikolaj Golub } 2801662c901cSMikolaj Golub 280203c96c31SRobert Watson #ifdef DDB 280303c96c31SRobert Watson static void 280403c96c31SRobert Watson db_print_indent(int indent) 280503c96c31SRobert Watson { 280603c96c31SRobert Watson int i; 280703c96c31SRobert Watson 280803c96c31SRobert Watson for (i = 0; i < indent; i++) 280903c96c31SRobert Watson db_printf(" "); 281003c96c31SRobert Watson } 281103c96c31SRobert Watson 281203c96c31SRobert Watson static void 281303c96c31SRobert Watson db_print_unpflags(int unp_flags) 281403c96c31SRobert Watson { 281503c96c31SRobert Watson int comma; 281603c96c31SRobert Watson 281703c96c31SRobert Watson comma = 0; 281803c96c31SRobert Watson if (unp_flags & UNP_HAVEPC) { 281903c96c31SRobert Watson db_printf("%sUNP_HAVEPC", comma ? ", " : ""); 282003c96c31SRobert Watson comma = 1; 282103c96c31SRobert Watson } 282203c96c31SRobert Watson if (unp_flags & UNP_WANTCRED) { 282303c96c31SRobert Watson db_printf("%sUNP_WANTCRED", comma ? ", " : ""); 282403c96c31SRobert Watson comma = 1; 282503c96c31SRobert Watson } 282603c96c31SRobert Watson if (unp_flags & UNP_CONNWAIT) { 282703c96c31SRobert Watson db_printf("%sUNP_CONNWAIT", comma ? ", " : ""); 282803c96c31SRobert Watson comma = 1; 282903c96c31SRobert Watson } 283003c96c31SRobert Watson if (unp_flags & UNP_CONNECTING) { 283103c96c31SRobert Watson db_printf("%sUNP_CONNECTING", comma ? ", " : ""); 283203c96c31SRobert Watson comma = 1; 283303c96c31SRobert Watson } 283403c96c31SRobert Watson if (unp_flags & UNP_BINDING) { 283503c96c31SRobert Watson db_printf("%sUNP_BINDING", comma ? ", " : ""); 283603c96c31SRobert Watson comma = 1; 283703c96c31SRobert Watson } 283803c96c31SRobert Watson } 283903c96c31SRobert Watson 284003c96c31SRobert Watson static void 284103c96c31SRobert Watson db_print_xucred(int indent, struct xucred *xu) 284203c96c31SRobert Watson { 284303c96c31SRobert Watson int comma, i; 284403c96c31SRobert Watson 284503c96c31SRobert Watson db_print_indent(indent); 2846c5afec6eSDmitry Chagin db_printf("cr_version: %u cr_uid: %u cr_pid: %d cr_ngroups: %d\n", 2847c5afec6eSDmitry Chagin xu->cr_version, xu->cr_uid, xu->cr_pid, xu->cr_ngroups); 284803c96c31SRobert Watson db_print_indent(indent); 284903c96c31SRobert Watson db_printf("cr_groups: "); 285003c96c31SRobert Watson comma = 0; 285103c96c31SRobert Watson for (i = 0; i < xu->cr_ngroups; i++) { 285203c96c31SRobert Watson db_printf("%s%u", comma ? ", " : "", xu->cr_groups[i]); 285303c96c31SRobert Watson comma = 1; 285403c96c31SRobert Watson } 285503c96c31SRobert Watson db_printf("\n"); 285603c96c31SRobert Watson } 285703c96c31SRobert Watson 285803c96c31SRobert Watson static void 285903c96c31SRobert Watson db_print_unprefs(int indent, struct unp_head *uh) 286003c96c31SRobert Watson { 286103c96c31SRobert Watson struct unpcb *unp; 286203c96c31SRobert Watson int counter; 286303c96c31SRobert Watson 286403c96c31SRobert Watson counter = 0; 286503c96c31SRobert Watson LIST_FOREACH(unp, uh, unp_reflink) { 286603c96c31SRobert Watson if (counter % 4 == 0) 286703c96c31SRobert Watson db_print_indent(indent); 286803c96c31SRobert Watson db_printf("%p ", unp); 286903c96c31SRobert Watson if (counter % 4 == 3) 287003c96c31SRobert Watson db_printf("\n"); 287103c96c31SRobert Watson counter++; 287203c96c31SRobert Watson } 287303c96c31SRobert Watson if (counter != 0 && counter % 4 != 0) 287403c96c31SRobert Watson db_printf("\n"); 287503c96c31SRobert Watson } 287603c96c31SRobert Watson 287703c96c31SRobert Watson DB_SHOW_COMMAND(unpcb, db_show_unpcb) 287803c96c31SRobert Watson { 287903c96c31SRobert Watson struct unpcb *unp; 288003c96c31SRobert Watson 288103c96c31SRobert Watson if (!have_addr) { 288203c96c31SRobert Watson db_printf("usage: show unpcb <addr>\n"); 288303c96c31SRobert Watson return; 288403c96c31SRobert Watson } 288503c96c31SRobert Watson unp = (struct unpcb *)addr; 288603c96c31SRobert Watson 288703c96c31SRobert Watson db_printf("unp_socket: %p unp_vnode: %p\n", unp->unp_socket, 288803c96c31SRobert Watson unp->unp_vnode); 288903c96c31SRobert Watson 2890fc8fdae0SMatthew D Fleming db_printf("unp_ino: %ju unp_conn: %p\n", (uintmax_t)unp->unp_ino, 289103c96c31SRobert Watson unp->unp_conn); 289203c96c31SRobert Watson 289303c96c31SRobert Watson db_printf("unp_refs:\n"); 289403c96c31SRobert Watson db_print_unprefs(2, &unp->unp_refs); 289503c96c31SRobert Watson 289603c96c31SRobert Watson /* XXXRW: Would be nice to print the full address, if any. */ 289703c96c31SRobert Watson db_printf("unp_addr: %p\n", unp->unp_addr); 289803c96c31SRobert Watson 2899c2090e73SAlan Somers db_printf("unp_gencnt: %llu\n", 290003c96c31SRobert Watson (unsigned long long)unp->unp_gencnt); 290103c96c31SRobert Watson 290203c96c31SRobert Watson db_printf("unp_flags: %x (", unp->unp_flags); 290303c96c31SRobert Watson db_print_unpflags(unp->unp_flags); 290403c96c31SRobert Watson db_printf(")\n"); 290503c96c31SRobert Watson 290603c96c31SRobert Watson db_printf("unp_peercred:\n"); 290703c96c31SRobert Watson db_print_xucred(2, &unp->unp_peercred); 290803c96c31SRobert Watson 290903c96c31SRobert Watson db_printf("unp_refcount: %u\n", unp->unp_refcount); 291003c96c31SRobert Watson } 291103c96c31SRobert Watson #endif 2912