19454b2d8SWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1991, 1993 5e1ac28e2SRobert Watson * The Regents of the University of California. 63dab55bcSRobert Watson * Copyright (c) 2004-2009 Robert N. M. Watson 7*c0874c34SMatt Macy * Copyright (c) 2018 Matthew Macy 8df8bae1dSRodney W. Grimes * 9df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 10df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 11df8bae1dSRodney W. Grimes * are met: 12df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 14df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 15df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 16df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1769a28758SEd Maste * 3. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 33748e0b0aSGarrett Wollman * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94 34df8bae1dSRodney W. Grimes */ 35df8bae1dSRodney W. Grimes 36f23929fbSRobert Watson /* 37f23929fbSRobert Watson * UNIX Domain (Local) Sockets 38f23929fbSRobert Watson * 39f23929fbSRobert Watson * This is an implementation of UNIX (local) domain sockets. Each socket has 40f23929fbSRobert Watson * an associated struct unpcb (UNIX protocol control block). Stream sockets 41f23929fbSRobert Watson * may be connected to 0 or 1 other socket. Datagram sockets may be 42f23929fbSRobert Watson * connected to 0, 1, or many other sockets. Sockets may be created and 43f23929fbSRobert Watson * connected in pairs (socketpair(2)), or bound/connected to using the file 44f23929fbSRobert Watson * system name space. For most purposes, only the receive socket buffer is 45f23929fbSRobert Watson * used, as sending on one socket delivers directly to the receive socket 465b950deaSRobert Watson * buffer of a second socket. 475b950deaSRobert Watson * 485b950deaSRobert Watson * The implementation is substantially complicated by the fact that 495b950deaSRobert Watson * "ancillary data", such as file descriptors or credentials, may be passed 505b950deaSRobert Watson * across UNIX domain sockets. The potential for passing UNIX domain sockets 515b950deaSRobert Watson * over other UNIX domain sockets requires the implementation of a simple 525b950deaSRobert Watson * garbage collector to find and tear down cycles of disconnected sockets. 53aea52f1bSRobert Watson * 54aea52f1bSRobert Watson * TODO: 5584d61770SRobert Watson * RDM 56aea52f1bSRobert Watson * rethink name space problems 57aea52f1bSRobert Watson * need a proper out-of-band 58f23929fbSRobert Watson */ 59f23929fbSRobert Watson 60677b542eSDavid E. O'Brien #include <sys/cdefs.h> 61677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 62677b542eSDavid E. O'Brien 6303c96c31SRobert Watson #include "opt_ddb.h" 64335654d7SRobert Watson 65df8bae1dSRodney W. Grimes #include <sys/param.h> 664a144410SRobert Watson #include <sys/capsicum.h> 67fb919e4dSMark Murray #include <sys/domain.h> 68960ed29cSSeigo Tanimura #include <sys/fcntl.h> 69d826c479SBruce Evans #include <sys/malloc.h> /* XXX must be before <sys/file.h> */ 704f590175SPaul Saab #include <sys/eventhandler.h> 71639acc13SGarrett Wollman #include <sys/file.h> 72960ed29cSSeigo Tanimura #include <sys/filedesc.h> 73960ed29cSSeigo Tanimura #include <sys/kernel.h> 74960ed29cSSeigo Tanimura #include <sys/lock.h> 75639acc13SGarrett Wollman #include <sys/mbuf.h> 76033eb86eSJeff Roberson #include <sys/mount.h> 77960ed29cSSeigo Tanimura #include <sys/mutex.h> 78639acc13SGarrett Wollman #include <sys/namei.h> 79639acc13SGarrett Wollman #include <sys/proc.h> 80df8bae1dSRodney W. Grimes #include <sys/protosw.h> 810cb64678SKonstantin Belousov #include <sys/queue.h> 82960ed29cSSeigo Tanimura #include <sys/resourcevar.h> 83e7c33e29SRobert Watson #include <sys/rwlock.h> 84df8bae1dSRodney W. Grimes #include <sys/socket.h> 85df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 86960ed29cSSeigo Tanimura #include <sys/signalvar.h> 87df8bae1dSRodney W. Grimes #include <sys/stat.h> 88960ed29cSSeigo Tanimura #include <sys/sx.h> 89639acc13SGarrett Wollman #include <sys/sysctl.h> 90960ed29cSSeigo Tanimura #include <sys/systm.h> 91a0ec558aSRobert Watson #include <sys/taskqueue.h> 92639acc13SGarrett Wollman #include <sys/un.h> 9398271db4SGarrett Wollman #include <sys/unpcb.h> 94639acc13SGarrett Wollman #include <sys/vnode.h> 95530c0060SRobert Watson 96530c0060SRobert Watson #include <net/vnet.h> 97df8bae1dSRodney W. Grimes 9803c96c31SRobert Watson #ifdef DDB 9903c96c31SRobert Watson #include <ddb/ddb.h> 10003c96c31SRobert Watson #endif 10103c96c31SRobert Watson 102aed55708SRobert Watson #include <security/mac/mac_framework.h> 103aed55708SRobert Watson 1049e9d298aSJeff Roberson #include <vm/uma.h> 10598271db4SGarrett Wollman 1068cb539f1SPawel Jakub Dawidek MALLOC_DECLARE(M_FILECAPS); 1078cb539f1SPawel Jakub Dawidek 1083dab55bcSRobert Watson /* 1093dab55bcSRobert Watson * Locking key: 1103dab55bcSRobert Watson * (l) Locked using list lock 1113dab55bcSRobert Watson * (g) Locked using linkage lock 1123dab55bcSRobert Watson */ 1133dab55bcSRobert Watson 1149e9d298aSJeff Roberson static uma_zone_t unp_zone; 1153dab55bcSRobert Watson static unp_gen_t unp_gencnt; /* (l) */ 1163dab55bcSRobert Watson static u_int unp_count; /* (l) Count of local sockets. */ 117aea52f1bSRobert Watson static ino_t unp_ino; /* Prototype for fake inode numbers. */ 1183dab55bcSRobert Watson static int unp_rights; /* (g) File descriptors in flight. */ 1193dab55bcSRobert Watson static struct unp_head unp_shead; /* (l) List of stream sockets. */ 1203dab55bcSRobert Watson static struct unp_head unp_dhead; /* (l) List of datagram sockets. */ 12184d61770SRobert Watson static struct unp_head unp_sphead; /* (l) List of seqpacket sockets. */ 12298271db4SGarrett Wollman 1230cb64678SKonstantin Belousov struct unp_defer { 1240cb64678SKonstantin Belousov SLIST_ENTRY(unp_defer) ud_link; 1250cb64678SKonstantin Belousov struct file *ud_fp; 1260cb64678SKonstantin Belousov }; 1270cb64678SKonstantin Belousov static SLIST_HEAD(, unp_defer) unp_defers; 1280cb64678SKonstantin Belousov static int unp_defers_count; 1290cb64678SKonstantin Belousov 130aea52f1bSRobert Watson static const struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 13198271db4SGarrett Wollman 132df8bae1dSRodney W. Grimes /* 133aea52f1bSRobert Watson * Garbage collection of cyclic file descriptor/socket references occurs 134aea52f1bSRobert Watson * asynchronously in a taskqueue context in order to avoid recursion and 135aea52f1bSRobert Watson * reentrance in the UNIX domain socket, file descriptor, and socket layer 136aea52f1bSRobert Watson * code. See unp_gc() for a full description. 137df8bae1dSRodney W. Grimes */ 138daee0f0bSKonstantin Belousov static struct timeout_task unp_gc_task; 139f708ef1bSPoul-Henning Kamp 140ce5f32deSRobert Watson /* 1410cb64678SKonstantin Belousov * The close of unix domain sockets attached as SCM_RIGHTS is 1420cb64678SKonstantin Belousov * postponed to the taskqueue, to avoid arbitrary recursion depth. 1430cb64678SKonstantin Belousov * The attached sockets might have another sockets attached. 1440cb64678SKonstantin Belousov */ 1450cb64678SKonstantin Belousov static struct task unp_defer_task; 1460cb64678SKonstantin Belousov 1470cb64678SKonstantin Belousov /* 1487e711c3aSRobert Watson * Both send and receive buffers are allocated PIPSIZ bytes of buffering for 1497e711c3aSRobert Watson * stream sockets, although the total for sender and receiver is actually 1507e711c3aSRobert Watson * only PIPSIZ. 1517e711c3aSRobert Watson * 1527e711c3aSRobert Watson * Datagram sockets really use the sendspace as the maximum datagram size, 1537e711c3aSRobert Watson * and don't really want to reserve the sendspace. Their recvspace should be 1547e711c3aSRobert Watson * large enough for at least one max-size datagram plus address. 1557e711c3aSRobert Watson */ 1567e711c3aSRobert Watson #ifndef PIPSIZ 1577e711c3aSRobert Watson #define PIPSIZ 8192 1587e711c3aSRobert Watson #endif 1597e711c3aSRobert Watson static u_long unpst_sendspace = PIPSIZ; 1607e711c3aSRobert Watson static u_long unpst_recvspace = PIPSIZ; 1617e711c3aSRobert Watson static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 1627e711c3aSRobert Watson static u_long unpdg_recvspace = 4*1024; 16384d61770SRobert Watson static u_long unpsp_sendspace = PIPSIZ; /* really max datagram size */ 16484d61770SRobert Watson static u_long unpsp_recvspace = PIPSIZ; 1657e711c3aSRobert Watson 1666472ac3dSEd Schouten static SYSCTL_NODE(_net, PF_LOCAL, local, CTLFLAG_RW, 0, "Local domain"); 1676472ac3dSEd Schouten static SYSCTL_NODE(_net_local, SOCK_STREAM, stream, CTLFLAG_RW, 0, 1686472ac3dSEd Schouten "SOCK_STREAM"); 1696472ac3dSEd Schouten static SYSCTL_NODE(_net_local, SOCK_DGRAM, dgram, CTLFLAG_RW, 0, "SOCK_DGRAM"); 1706472ac3dSEd Schouten static SYSCTL_NODE(_net_local, SOCK_SEQPACKET, seqpacket, CTLFLAG_RW, 0, 17184d61770SRobert Watson "SOCK_SEQPACKET"); 172e4445a03SRobert Watson 1737e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 174be6b1304STom Rhodes &unpst_sendspace, 0, "Default stream send space."); 1757e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 176be6b1304STom Rhodes &unpst_recvspace, 0, "Default stream receive space."); 1777e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 178be6b1304STom Rhodes &unpdg_sendspace, 0, "Default datagram send space."); 1797e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 180be6b1304STom Rhodes &unpdg_recvspace, 0, "Default datagram receive space."); 18184d61770SRobert Watson SYSCTL_ULONG(_net_local_seqpacket, OID_AUTO, maxseqpacket, CTLFLAG_RW, 18284d61770SRobert Watson &unpsp_sendspace, 0, "Default seqpacket send space."); 18384d61770SRobert Watson SYSCTL_ULONG(_net_local_seqpacket, OID_AUTO, recvspace, CTLFLAG_RW, 18484d61770SRobert Watson &unpsp_recvspace, 0, "Default seqpacket receive space."); 185be6b1304STom Rhodes SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, 186be6b1304STom Rhodes "File descriptors in flight."); 1870cb64678SKonstantin Belousov SYSCTL_INT(_net_local, OID_AUTO, deferred, CTLFLAG_RD, 1880cb64678SKonstantin Belousov &unp_defers_count, 0, 1890cb64678SKonstantin Belousov "File descriptors deferred to taskqueue for close."); 1907e711c3aSRobert Watson 191175389cfSEdward Tomasz Napierala /* 192e7c33e29SRobert Watson * Locking and synchronization: 193ce5f32deSRobert Watson * 19475a67bf3SMatt Macy * Three types of locks exist in the local domain socket implementation: a 19575a67bf3SMatt Macy * a global linkage rwlock, the mtxpool lock, and per-unpcb mutexes. 19675a67bf3SMatt Macy * The linkage lock protects the socket count, global generation number, 19775a67bf3SMatt Macy * and stream/datagram global lists. 19875a67bf3SMatt Macy * 19975a67bf3SMatt Macy * The mtxpool lock protects the vnode from being modified while referenced. 20075a67bf3SMatt Macy * Lock ordering requires that it be acquired before any unpcb locks. 20175a67bf3SMatt Macy * 20275a67bf3SMatt Macy * The unpcb lock (unp_mtx) protects all fields in the unpcb. Of particular 20375a67bf3SMatt Macy * note is that this includes the unp_conn field. So long as the unpcb lock 20475a67bf3SMatt Macy * is held the reference to the unpcb pointed to by unp_conn is valid. If we 20575a67bf3SMatt Macy * require that the unpcb pointed to by unp_conn remain live in cases where 20675a67bf3SMatt Macy * we need to drop the unp_mtx as when we need to acquire the lock for a 20775a67bf3SMatt Macy * second unpcb the caller must first acquire an additional reference on the 20875a67bf3SMatt Macy * second unpcb and then revalidate any state (typically check that unp_conn 20975a67bf3SMatt Macy * is non-NULL) upon requiring the initial unpcb lock. The lock ordering 21075a67bf3SMatt Macy * between unpcbs is the conventional ascending address order. Two helper 21175a67bf3SMatt Macy * routines exist for this: 21275a67bf3SMatt Macy * 21375a67bf3SMatt Macy * - unp_pcb_lock2(unp, unp2) - which just acquires the two locks in the 21475a67bf3SMatt Macy * safe ordering. 21575a67bf3SMatt Macy * 21675a67bf3SMatt Macy * - unp_pcb_owned_lock2(unp, unp2, freed) - the lock for unp is held 21775a67bf3SMatt Macy * when called. If unp is unlocked and unp2 is subsequently freed 21875a67bf3SMatt Macy * freed will be set to 1. 21975a67bf3SMatt Macy * 22075a67bf3SMatt Macy * The helper routines for references are: 22175a67bf3SMatt Macy * 22275a67bf3SMatt Macy * - unp_pcb_hold(unp): Can be called any time we currently hold a valid 22375a67bf3SMatt Macy * reference to unp. 22475a67bf3SMatt Macy * 22575a67bf3SMatt Macy * - unp_pcb_rele(unp): The caller must hold the unp lock. If we are 22675a67bf3SMatt Macy * releasing the last reference, detach must have been called thus 22775a67bf3SMatt Macy * unp->unp_socket be NULL. 228ce5f32deSRobert Watson * 229e7c33e29SRobert Watson * UNIX domain sockets each have an unpcb hung off of their so_pcb pointer, 230e7c33e29SRobert Watson * allocated in pru_attach() and freed in pru_detach(). The validity of that 231e7c33e29SRobert Watson * pointer is an invariant, so no lock is required to dereference the so_pcb 232e7c33e29SRobert Watson * pointer if a valid socket reference is held by the caller. In practice, 233e7c33e29SRobert Watson * this is always true during operations performed on a socket. Each unpcb 234e7c33e29SRobert Watson * has a back-pointer to its socket, unp_socket, which will be stable under 235e7c33e29SRobert Watson * the same circumstances. 236e7c33e29SRobert Watson * 237e7c33e29SRobert Watson * This pointer may only be safely dereferenced as long as a valid reference 238e7c33e29SRobert Watson * to the unpcb is held. Typically, this reference will be from the socket, 239e7c33e29SRobert Watson * or from another unpcb when the referring unpcb's lock is held (in order 240e7c33e29SRobert Watson * that the reference not be invalidated during use). For example, to follow 24175a67bf3SMatt Macy * unp->unp_conn->unp_socket, you need to hold a lock on unp_conn to guarantee 24275a67bf3SMatt Macy * that detach is not run clearing unp_socket. 243e7c33e29SRobert Watson * 244e7c33e29SRobert Watson * Blocking with UNIX domain sockets is a tricky issue: unlike most network 245e7c33e29SRobert Watson * protocols, bind() is a non-atomic operation, and connect() requires 246e7c33e29SRobert Watson * potential sleeping in the protocol, due to potentially waiting on local or 247e7c33e29SRobert Watson * distributed file systems. We try to separate "lookup" operations, which 248e7c33e29SRobert Watson * may sleep, and the IPC operations themselves, which typically can occur 249e7c33e29SRobert Watson * with relative atomicity as locks can be held over the entire operation. 250e7c33e29SRobert Watson * 251e7c33e29SRobert Watson * Another tricky issue is simultaneous multi-threaded or multi-process 252e7c33e29SRobert Watson * access to a single UNIX domain socket. These are handled by the flags 253e7c33e29SRobert Watson * UNP_CONNECTING and UNP_BINDING, which prevent concurrent connecting or 254e7c33e29SRobert Watson * binding, both of which involve dropping UNIX domain socket locks in order 255e7c33e29SRobert Watson * to perform namei() and other file system operations. 256ce5f32deSRobert Watson */ 2573dab55bcSRobert Watson static struct rwlock unp_link_rwlock; 2580cb64678SKonstantin Belousov static struct mtx unp_defers_lock; 259e7c33e29SRobert Watson 2603dab55bcSRobert Watson #define UNP_LINK_LOCK_INIT() rw_init(&unp_link_rwlock, \ 2613dab55bcSRobert Watson "unp_link_rwlock") 262e7c33e29SRobert Watson 2633dab55bcSRobert Watson #define UNP_LINK_LOCK_ASSERT() rw_assert(&unp_link_rwlock, \ 264e7c33e29SRobert Watson RA_LOCKED) 2653dab55bcSRobert Watson #define UNP_LINK_UNLOCK_ASSERT() rw_assert(&unp_link_rwlock, \ 266e7c33e29SRobert Watson RA_UNLOCKED) 267e7c33e29SRobert Watson 2683dab55bcSRobert Watson #define UNP_LINK_RLOCK() rw_rlock(&unp_link_rwlock) 2693dab55bcSRobert Watson #define UNP_LINK_RUNLOCK() rw_runlock(&unp_link_rwlock) 2703dab55bcSRobert Watson #define UNP_LINK_WLOCK() rw_wlock(&unp_link_rwlock) 2713dab55bcSRobert Watson #define UNP_LINK_WUNLOCK() rw_wunlock(&unp_link_rwlock) 2723dab55bcSRobert Watson #define UNP_LINK_WLOCK_ASSERT() rw_assert(&unp_link_rwlock, \ 273e7c33e29SRobert Watson RA_WLOCKED) 274779f106aSGleb Smirnoff #define UNP_LINK_WOWNED() rw_wowned(&unp_link_rwlock) 275e7c33e29SRobert Watson 2760cb64678SKonstantin Belousov #define UNP_DEFERRED_LOCK_INIT() mtx_init(&unp_defers_lock, \ 2770cb64678SKonstantin Belousov "unp_defer", NULL, MTX_DEF) 2780cb64678SKonstantin Belousov #define UNP_DEFERRED_LOCK() mtx_lock(&unp_defers_lock) 2790cb64678SKonstantin Belousov #define UNP_DEFERRED_UNLOCK() mtx_unlock(&unp_defers_lock) 2800cb64678SKonstantin Belousov 28175a67bf3SMatt Macy #define UNP_REF_LIST_LOCK() UNP_DEFERRED_LOCK(); 28275a67bf3SMatt Macy #define UNP_REF_LIST_UNLOCK() UNP_DEFERRED_UNLOCK(); 28375a67bf3SMatt Macy 284e7c33e29SRobert Watson #define UNP_PCB_LOCK_INIT(unp) mtx_init(&(unp)->unp_mtx, \ 285e7c33e29SRobert Watson "unp_mtx", "unp_mtx", \ 28675a67bf3SMatt Macy MTX_DUPOK|MTX_DEF) 287e7c33e29SRobert Watson #define UNP_PCB_LOCK_DESTROY(unp) mtx_destroy(&(unp)->unp_mtx) 288e7c33e29SRobert Watson #define UNP_PCB_LOCK(unp) mtx_lock(&(unp)->unp_mtx) 28975a67bf3SMatt Macy #define UNP_PCB_TRYLOCK(unp) mtx_trylock(&(unp)->unp_mtx) 290e7c33e29SRobert Watson #define UNP_PCB_UNLOCK(unp) mtx_unlock(&(unp)->unp_mtx) 29175a67bf3SMatt Macy #define UNP_PCB_OWNED(unp) mtx_owned(&(unp)->unp_mtx) 292e7c33e29SRobert Watson #define UNP_PCB_LOCK_ASSERT(unp) mtx_assert(&(unp)->unp_mtx, MA_OWNED) 29375a67bf3SMatt Macy #define UNP_PCB_UNLOCK_ASSERT(unp) mtx_assert(&(unp)->unp_mtx, MA_NOTOWNED) 2940d9ce3a1SRobert Watson 2952c899584SRobert Watson static int uipc_connect2(struct socket *, struct socket *); 2960b36cd25SRobert Watson static int uipc_ctloutput(struct socket *, struct sockopt *); 297aea52f1bSRobert Watson static int unp_connect(struct socket *, struct sockaddr *, 298aea52f1bSRobert Watson struct thread *); 2997493f24eSPawel Jakub Dawidek static int unp_connectat(int, struct socket *, struct sockaddr *, 3007493f24eSPawel Jakub Dawidek struct thread *); 3016a2989fdSMatthew N. Dodd static int unp_connect2(struct socket *so, struct socket *so2, int); 302e7c33e29SRobert Watson static void unp_disconnect(struct unpcb *unp, struct unpcb *unp2); 30399ab95dbSMark Johnston static void unp_dispose(struct socket *so); 30499ab95dbSMark Johnston static void unp_dispose_mbuf(struct mbuf *); 3054d77a549SAlfred Perlstein static void unp_shutdown(struct unpcb *); 306afc055d9SEd Schouten static void unp_drop(struct unpcb *); 307a0ec558aSRobert Watson static void unp_gc(__unused void *, int); 308be26ba7cSPawel Jakub Dawidek static void unp_scan(struct mbuf *, void (*)(struct filedescent **, int)); 3094d77a549SAlfred Perlstein static void unp_discard(struct file *); 3108cb539f1SPawel Jakub Dawidek static void unp_freerights(struct filedescent **, int); 3110b36cd25SRobert Watson static void unp_init(void); 3124d77a549SAlfred Perlstein static int unp_internalize(struct mbuf **, struct thread *); 313397c19d1SJeff Roberson static void unp_internalize_fp(struct file *); 314c2e3c52eSJilles Tjoelker static int unp_externalize(struct mbuf *, struct mbuf **, int); 3150cb64678SKonstantin Belousov static int unp_externalize_fp(struct file *); 3165b950deaSRobert Watson static struct mbuf *unp_addsockcred(struct thread *, struct mbuf *); 3170cb64678SKonstantin Belousov static void unp_process_defers(void * __unused, int); 318f708ef1bSPoul-Henning Kamp 31975a67bf3SMatt Macy 32075a67bf3SMatt Macy static void 32175a67bf3SMatt Macy unp_pcb_hold(struct unpcb *unp) 32275a67bf3SMatt Macy { 32375a67bf3SMatt Macy MPASS(unp->unp_refcount); 32475a67bf3SMatt Macy refcount_acquire(&unp->unp_refcount); 32575a67bf3SMatt Macy } 32675a67bf3SMatt Macy 32775a67bf3SMatt Macy static int 32875a67bf3SMatt Macy unp_pcb_rele(struct unpcb *unp) 32975a67bf3SMatt Macy { 33075a67bf3SMatt Macy int freed; 33175a67bf3SMatt Macy 33275a67bf3SMatt Macy UNP_PCB_LOCK_ASSERT(unp); 33375a67bf3SMatt Macy MPASS(unp->unp_refcount); 33475a67bf3SMatt Macy if ((freed = refcount_release(&unp->unp_refcount))) { 33575a67bf3SMatt Macy /* we got here with having detached? */ 33675a67bf3SMatt Macy MPASS(unp->unp_socket == NULL); 33775a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 33875a67bf3SMatt Macy UNP_PCB_LOCK_DESTROY(unp); 33975a67bf3SMatt Macy uma_zfree(unp_zone, unp); 34075a67bf3SMatt Macy } 34175a67bf3SMatt Macy return (freed); 34275a67bf3SMatt Macy } 34375a67bf3SMatt Macy 34475a67bf3SMatt Macy static void 34575a67bf3SMatt Macy unp_pcb_lock2(struct unpcb *unp, struct unpcb *unp2) 34675a67bf3SMatt Macy { 34775a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT(unp); 34875a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT(unp2); 34975a67bf3SMatt Macy if ((uintptr_t)unp2 > (uintptr_t)unp) { 35075a67bf3SMatt Macy UNP_PCB_LOCK(unp); 35175a67bf3SMatt Macy UNP_PCB_LOCK(unp2); 35275a67bf3SMatt Macy } else { 35375a67bf3SMatt Macy UNP_PCB_LOCK(unp2); 35475a67bf3SMatt Macy UNP_PCB_LOCK(unp); 35575a67bf3SMatt Macy } 35675a67bf3SMatt Macy } 35775a67bf3SMatt Macy 35875a67bf3SMatt Macy static __noinline void 35975a67bf3SMatt Macy unp_pcb_owned_lock2_slowpath(struct unpcb *unp, struct unpcb **unp2p, int *freed) 36075a67bf3SMatt Macy 36175a67bf3SMatt Macy { 36275a67bf3SMatt Macy struct unpcb *unp2; 36375a67bf3SMatt Macy 36475a67bf3SMatt Macy unp2 = *unp2p; 36575a67bf3SMatt Macy unp_pcb_hold((unp2)); 36675a67bf3SMatt Macy UNP_PCB_UNLOCK((unp)); 36775a67bf3SMatt Macy UNP_PCB_LOCK((unp2)); 36875a67bf3SMatt Macy UNP_PCB_LOCK((unp)); 36975a67bf3SMatt Macy *freed = unp_pcb_rele((unp2)); 37075a67bf3SMatt Macy if (*freed) 37175a67bf3SMatt Macy *unp2p = NULL; 37275a67bf3SMatt Macy } 37375a67bf3SMatt Macy 37475a67bf3SMatt Macy #define unp_pcb_owned_lock2(unp, unp2, freed) do { \ 37575a67bf3SMatt Macy freed = 0; \ 37675a67bf3SMatt Macy UNP_PCB_LOCK_ASSERT((unp)); \ 37775a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT((unp2)); \ 37875a67bf3SMatt Macy if (__predict_true(UNP_PCB_TRYLOCK((unp2)))) \ 37975a67bf3SMatt Macy break; \ 38075a67bf3SMatt Macy else if ((uintptr_t)(unp2) > (uintptr_t)(unp)) \ 38175a67bf3SMatt Macy UNP_PCB_LOCK((unp2)); \ 38275a67bf3SMatt Macy else { \ 38375a67bf3SMatt Macy unp_pcb_owned_lock2_slowpath((unp), &(unp2), &freed); \ 38475a67bf3SMatt Macy } \ 38575a67bf3SMatt Macy } while (0) 38675a67bf3SMatt Macy 38775a67bf3SMatt Macy 388e4445a03SRobert Watson /* 389e4445a03SRobert Watson * Definitions of protocols supported in the LOCAL domain. 390e4445a03SRobert Watson */ 391e4445a03SRobert Watson static struct domain localdomain; 392fa9402f2SRobert Watson static struct pr_usrreqs uipc_usrreqs_dgram, uipc_usrreqs_stream; 39384d61770SRobert Watson static struct pr_usrreqs uipc_usrreqs_seqpacket; 394e4445a03SRobert Watson static struct protosw localsw[] = { 395e4445a03SRobert Watson { 396e4445a03SRobert Watson .pr_type = SOCK_STREAM, 397e4445a03SRobert Watson .pr_domain = &localdomain, 398e4445a03SRobert Watson .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_RIGHTS, 399e4445a03SRobert Watson .pr_ctloutput = &uipc_ctloutput, 400fa9402f2SRobert Watson .pr_usrreqs = &uipc_usrreqs_stream 401e4445a03SRobert Watson }, 402e4445a03SRobert Watson { 403e4445a03SRobert Watson .pr_type = SOCK_DGRAM, 404e4445a03SRobert Watson .pr_domain = &localdomain, 405e4445a03SRobert Watson .pr_flags = PR_ATOMIC|PR_ADDR|PR_RIGHTS, 406aaf63435SGleb Smirnoff .pr_ctloutput = &uipc_ctloutput, 407fa9402f2SRobert Watson .pr_usrreqs = &uipc_usrreqs_dgram 408e4445a03SRobert Watson }, 40984d61770SRobert Watson { 41084d61770SRobert Watson .pr_type = SOCK_SEQPACKET, 41184d61770SRobert Watson .pr_domain = &localdomain, 41284d61770SRobert Watson 41384d61770SRobert Watson /* 41484d61770SRobert Watson * XXXRW: For now, PR_ADDR because soreceive will bump into them 41584d61770SRobert Watson * due to our use of sbappendaddr. A new sbappend variants is needed 41684d61770SRobert Watson * that supports both atomic record writes and control data. 41784d61770SRobert Watson */ 41884d61770SRobert Watson .pr_flags = PR_ADDR|PR_ATOMIC|PR_CONNREQUIRED|PR_WANTRCVD| 41984d61770SRobert Watson PR_RIGHTS, 420e0643280SGleb Smirnoff .pr_ctloutput = &uipc_ctloutput, 42184d61770SRobert Watson .pr_usrreqs = &uipc_usrreqs_seqpacket, 42284d61770SRobert Watson }, 423e4445a03SRobert Watson }; 424e4445a03SRobert Watson 425e4445a03SRobert Watson static struct domain localdomain = { 426e4445a03SRobert Watson .dom_family = AF_LOCAL, 427e4445a03SRobert Watson .dom_name = "local", 428e4445a03SRobert Watson .dom_init = unp_init, 429e4445a03SRobert Watson .dom_externalize = unp_externalize, 43099ab95dbSMark Johnston .dom_dispose = unp_dispose, 431e4445a03SRobert Watson .dom_protosw = localsw, 43202abd400SPedro F. Giffuni .dom_protoswNPROTOSW = &localsw[nitems(localsw)] 433e4445a03SRobert Watson }; 434e4445a03SRobert Watson DOMAIN_SET(local); 435e4445a03SRobert Watson 436ac45e92fSRobert Watson static void 437a29f300eSGarrett Wollman uipc_abort(struct socket *so) 438df8bae1dSRodney W. Grimes { 439e7c33e29SRobert Watson struct unpcb *unp, *unp2; 440df8bae1dSRodney W. Grimes 44140f2ac28SRobert Watson unp = sotounpcb(so); 4424d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_abort: unp == NULL")); 44375a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT(unp); 444e7c33e29SRobert Watson 445e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 446e7c33e29SRobert Watson unp2 = unp->unp_conn; 447e7c33e29SRobert Watson if (unp2 != NULL) { 44875a67bf3SMatt Macy unp_pcb_hold(unp2); 449e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 45075a67bf3SMatt Macy unp_drop(unp2); 45175a67bf3SMatt Macy } else 45275a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 453df8bae1dSRodney W. Grimes } 454df8bae1dSRodney W. Grimes 455a29f300eSGarrett Wollman static int 45657bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam) 457a29f300eSGarrett Wollman { 458e7c33e29SRobert Watson struct unpcb *unp, *unp2; 4590d9ce3a1SRobert Watson const struct sockaddr *sa; 460df8bae1dSRodney W. Grimes 461df8bae1dSRodney W. Grimes /* 4621c381b19SRobert Watson * Pass back name of connected socket, if it was bound and we are 4631c381b19SRobert Watson * still connected (our peer may have closed already!). 464df8bae1dSRodney W. Grimes */ 4654d4b555eSRobert Watson unp = sotounpcb(so); 4664d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_accept: unp == NULL")); 467e7c33e29SRobert Watson 4680d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 4693dab55bcSRobert Watson UNP_LINK_RLOCK(); 470e7c33e29SRobert Watson unp2 = unp->unp_conn; 471e7c33e29SRobert Watson if (unp2 != NULL && unp2->unp_addr != NULL) { 472e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 473e7c33e29SRobert Watson sa = (struct sockaddr *) unp2->unp_addr; 474e7c33e29SRobert Watson bcopy(sa, *nam, sa->sa_len); 475e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 476e7c33e29SRobert Watson } else { 4770d9ce3a1SRobert Watson sa = &sun_noname; 4780d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 479e7c33e29SRobert Watson } 4803dab55bcSRobert Watson UNP_LINK_RUNLOCK(); 481e5aeaa0cSDag-Erling Smørgrav return (0); 482a29f300eSGarrett Wollman } 483df8bae1dSRodney W. Grimes 484a29f300eSGarrett Wollman static int 485b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td) 486a29f300eSGarrett Wollman { 487e7c33e29SRobert Watson u_long sendspace, recvspace; 4886d32873cSRobert Watson struct unpcb *unp; 4893dab55bcSRobert Watson int error; 490779f106aSGleb Smirnoff bool locked; 491df8bae1dSRodney W. Grimes 4926d32873cSRobert Watson KASSERT(so->so_pcb == NULL, ("uipc_attach: so_pcb != NULL")); 4936d32873cSRobert Watson if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 4946d32873cSRobert Watson switch (so->so_type) { 4956d32873cSRobert Watson case SOCK_STREAM: 496e7c33e29SRobert Watson sendspace = unpst_sendspace; 497e7c33e29SRobert Watson recvspace = unpst_recvspace; 4986d32873cSRobert Watson break; 4996d32873cSRobert Watson 5006d32873cSRobert Watson case SOCK_DGRAM: 501e7c33e29SRobert Watson sendspace = unpdg_sendspace; 502e7c33e29SRobert Watson recvspace = unpdg_recvspace; 5036d32873cSRobert Watson break; 5046d32873cSRobert Watson 50584d61770SRobert Watson case SOCK_SEQPACKET: 50684d61770SRobert Watson sendspace = unpsp_sendspace; 50784d61770SRobert Watson recvspace = unpsp_recvspace; 50884d61770SRobert Watson break; 50984d61770SRobert Watson 5106d32873cSRobert Watson default: 511e7c33e29SRobert Watson panic("uipc_attach"); 5126d32873cSRobert Watson } 513e7c33e29SRobert Watson error = soreserve(so, sendspace, recvspace); 5146d32873cSRobert Watson if (error) 5156d32873cSRobert Watson return (error); 5166d32873cSRobert Watson } 51746a1d9bfSRobert Watson unp = uma_zalloc(unp_zone, M_NOWAIT | M_ZERO); 5186d32873cSRobert Watson if (unp == NULL) 5196d32873cSRobert Watson return (ENOBUFS); 5206d32873cSRobert Watson LIST_INIT(&unp->unp_refs); 521e7c33e29SRobert Watson UNP_PCB_LOCK_INIT(unp); 5226d32873cSRobert Watson unp->unp_socket = so; 5236d32873cSRobert Watson so->so_pcb = unp; 5249ae328fcSJohn Baldwin unp->unp_refcount = 1; 525779f106aSGleb Smirnoff if (so->so_listen != NULL) 526434ac8b6SMark Johnston unp->unp_flags |= UNP_NASCENT; 527e7c33e29SRobert Watson 528779f106aSGleb Smirnoff if ((locked = UNP_LINK_WOWNED()) == false) 529779f106aSGleb Smirnoff UNP_LINK_WLOCK(); 530779f106aSGleb Smirnoff 5316d32873cSRobert Watson unp->unp_gencnt = ++unp_gencnt; 5326d32873cSRobert Watson unp_count++; 53384d61770SRobert Watson switch (so->so_type) { 53484d61770SRobert Watson case SOCK_STREAM: 53584d61770SRobert Watson LIST_INSERT_HEAD(&unp_shead, unp, unp_link); 53684d61770SRobert Watson break; 53784d61770SRobert Watson 53884d61770SRobert Watson case SOCK_DGRAM: 53984d61770SRobert Watson LIST_INSERT_HEAD(&unp_dhead, unp, unp_link); 54084d61770SRobert Watson break; 54184d61770SRobert Watson 54284d61770SRobert Watson case SOCK_SEQPACKET: 54384d61770SRobert Watson LIST_INSERT_HEAD(&unp_sphead, unp, unp_link); 54484d61770SRobert Watson break; 54584d61770SRobert Watson 54684d61770SRobert Watson default: 54784d61770SRobert Watson panic("uipc_attach"); 54884d61770SRobert Watson } 549779f106aSGleb Smirnoff 550779f106aSGleb Smirnoff if (locked == false) 551779f106aSGleb Smirnoff UNP_LINK_WUNLOCK(); 5526d32873cSRobert Watson 5536d32873cSRobert Watson return (0); 554a29f300eSGarrett Wollman } 555a29f300eSGarrett Wollman 556a29f300eSGarrett Wollman static int 5577493f24eSPawel Jakub Dawidek uipc_bindat(int fd, struct socket *so, struct sockaddr *nam, struct thread *td) 558a29f300eSGarrett Wollman { 559dd47f5caSRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam; 560dd47f5caSRobert Watson struct vattr vattr; 5615050aa86SKonstantin Belousov int error, namelen; 562dd47f5caSRobert Watson struct nameidata nd; 56340f2ac28SRobert Watson struct unpcb *unp; 564dd47f5caSRobert Watson struct vnode *vp; 565dd47f5caSRobert Watson struct mount *mp; 5667008be5bSPawel Jakub Dawidek cap_rights_t rights; 567dd47f5caSRobert Watson char *buf; 568a29f300eSGarrett Wollman 569cb7df69bSKevin Lo if (nam->sa_family != AF_UNIX) 570cb7df69bSKevin Lo return (EAFNOSUPPORT); 571cb7df69bSKevin Lo 57240f2ac28SRobert Watson unp = sotounpcb(so); 5734d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_bind: unp == NULL")); 5744f1f0ef5SRobert Watson 575a06534c3SBjoern A. Zeeb if (soun->sun_len > sizeof(struct sockaddr_un)) 576a06534c3SBjoern A. Zeeb return (EINVAL); 5774f1f0ef5SRobert Watson namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 5784f1f0ef5SRobert Watson if (namelen <= 0) 5794f1f0ef5SRobert Watson return (EINVAL); 580dd47f5caSRobert Watson 581dd47f5caSRobert Watson /* 5824f1f0ef5SRobert Watson * We don't allow simultaneous bind() calls on a single UNIX domain 5834f1f0ef5SRobert Watson * socket, so flag in-progress operations, and return an error if an 5844f1f0ef5SRobert Watson * operation is already in progress. 5854f1f0ef5SRobert Watson * 5864f1f0ef5SRobert Watson * Historically, we have not allowed a socket to be rebound, so this 587d7924b70SRobert Watson * also returns an error. Not allowing re-binding simplifies the 588d7924b70SRobert Watson * implementation and avoids a great many possible failure modes. 589dd47f5caSRobert Watson */ 590e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 591dd47f5caSRobert Watson if (unp->unp_vnode != NULL) { 592e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 593dd47f5caSRobert Watson return (EINVAL); 594dd47f5caSRobert Watson } 5954f1f0ef5SRobert Watson if (unp->unp_flags & UNP_BINDING) { 596e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 5974f1f0ef5SRobert Watson return (EALREADY); 598dd47f5caSRobert Watson } 5994f1f0ef5SRobert Watson unp->unp_flags |= UNP_BINDING; 600e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 601dd47f5caSRobert Watson 602dd47f5caSRobert Watson buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 6037928893dSEd Maste bcopy(soun->sun_path, buf, namelen); 6047928893dSEd Maste buf[namelen] = 0; 605dd47f5caSRobert Watson 606dd47f5caSRobert Watson restart: 6076c21f6edSKonstantin Belousov NDINIT_ATRIGHTS(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME | NOCACHE, 6087008be5bSPawel Jakub Dawidek UIO_SYSSPACE, buf, fd, cap_rights_init(&rights, CAP_BINDAT), td); 609dd47f5caSRobert Watson /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 610dd47f5caSRobert Watson error = namei(&nd); 611dd47f5caSRobert Watson if (error) 6124f1f0ef5SRobert Watson goto error; 613dd47f5caSRobert Watson vp = nd.ni_vp; 614dd47f5caSRobert Watson if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 615dd47f5caSRobert Watson NDFREE(&nd, NDF_ONLY_PNBUF); 616dd47f5caSRobert Watson if (nd.ni_dvp == vp) 617dd47f5caSRobert Watson vrele(nd.ni_dvp); 618dd47f5caSRobert Watson else 619dd47f5caSRobert Watson vput(nd.ni_dvp); 620dd47f5caSRobert Watson if (vp != NULL) { 621dd47f5caSRobert Watson vrele(vp); 622dd47f5caSRobert Watson error = EADDRINUSE; 6234f1f0ef5SRobert Watson goto error; 624dd47f5caSRobert Watson } 625dd47f5caSRobert Watson error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 626dd47f5caSRobert Watson if (error) 6274f1f0ef5SRobert Watson goto error; 628dd47f5caSRobert Watson goto restart; 629dd47f5caSRobert Watson } 630dd47f5caSRobert Watson VATTR_NULL(&vattr); 631dd47f5caSRobert Watson vattr.va_type = VSOCK; 632dd47f5caSRobert Watson vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 633dd47f5caSRobert Watson #ifdef MAC 63430d239bcSRobert Watson error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 635dd47f5caSRobert Watson &vattr); 636dd47f5caSRobert Watson #endif 637885868cdSRobert Watson if (error == 0) 638dd47f5caSRobert Watson error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 639dd47f5caSRobert Watson NDFREE(&nd, NDF_ONLY_PNBUF); 640dd47f5caSRobert Watson vput(nd.ni_dvp); 641dd47f5caSRobert Watson if (error) { 642dd47f5caSRobert Watson vn_finished_write(mp); 6434f1f0ef5SRobert Watson goto error; 644dd47f5caSRobert Watson } 645dd47f5caSRobert Watson vp = nd.ni_vp; 64657fd3d55SPawel Jakub Dawidek ASSERT_VOP_ELOCKED(vp, "uipc_bind"); 647dd47f5caSRobert Watson soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 648e7c33e29SRobert Watson 649e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 6500c3c207fSGleb Smirnoff VOP_UNP_BIND(vp, unp); 651dd47f5caSRobert Watson unp->unp_vnode = vp; 652dd47f5caSRobert Watson unp->unp_addr = soun; 6534f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_BINDING; 654e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 65522db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 656dd47f5caSRobert Watson vn_finished_write(mp); 6574f1f0ef5SRobert Watson free(buf, M_TEMP); 6584f1f0ef5SRobert Watson return (0); 659e7c33e29SRobert Watson 6604f1f0ef5SRobert Watson error: 661e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 6624f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_BINDING; 663e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 664dd47f5caSRobert Watson free(buf, M_TEMP); 66540f2ac28SRobert Watson return (error); 666a29f300eSGarrett Wollman } 667a29f300eSGarrett Wollman 668a29f300eSGarrett Wollman static int 6697493f24eSPawel Jakub Dawidek uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 6707493f24eSPawel Jakub Dawidek { 6717493f24eSPawel Jakub Dawidek 6727493f24eSPawel Jakub Dawidek return (uipc_bindat(AT_FDCWD, so, nam, td)); 6737493f24eSPawel Jakub Dawidek } 6747493f24eSPawel Jakub Dawidek 6757493f24eSPawel Jakub Dawidek static int 676b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 677a29f300eSGarrett Wollman { 6780d9ce3a1SRobert Watson int error; 679a29f300eSGarrett Wollman 680fd179ee9SRobert Watson KASSERT(td == curthread, ("uipc_connect: td != curthread")); 681fd179ee9SRobert Watson error = unp_connect(so, nam, td); 6820d9ce3a1SRobert Watson return (error); 683a29f300eSGarrett Wollman } 684a29f300eSGarrett Wollman 6857493f24eSPawel Jakub Dawidek static int 6867493f24eSPawel Jakub Dawidek uipc_connectat(int fd, struct socket *so, struct sockaddr *nam, 6877493f24eSPawel Jakub Dawidek struct thread *td) 6887493f24eSPawel Jakub Dawidek { 6897493f24eSPawel Jakub Dawidek int error; 6907493f24eSPawel Jakub Dawidek 6917493f24eSPawel Jakub Dawidek KASSERT(td == curthread, ("uipc_connectat: td != curthread")); 6927493f24eSPawel Jakub Dawidek error = unp_connectat(fd, so, nam, td); 6937493f24eSPawel Jakub Dawidek return (error); 6947493f24eSPawel Jakub Dawidek } 6957493f24eSPawel Jakub Dawidek 696a152f8a3SRobert Watson static void 697a152f8a3SRobert Watson uipc_close(struct socket *so) 698a152f8a3SRobert Watson { 699e7c33e29SRobert Watson struct unpcb *unp, *unp2; 700779f106aSGleb Smirnoff struct vnode *vp = NULL; 70175a67bf3SMatt Macy struct mtx *vplock; 70275a67bf3SMatt Macy int freed; 703a152f8a3SRobert Watson unp = sotounpcb(so); 704a152f8a3SRobert Watson KASSERT(unp != NULL, ("uipc_close: unp == NULL")); 705e7c33e29SRobert Watson 70675a67bf3SMatt Macy 70775a67bf3SMatt Macy vplock = NULL; 70875a67bf3SMatt Macy if ((vp = unp->unp_vnode) != NULL) { 70975a67bf3SMatt Macy vplock = mtx_pool_find(mtxpool_sleep, vp); 71075a67bf3SMatt Macy mtx_lock(vplock); 711e7c33e29SRobert Watson } 71275a67bf3SMatt Macy UNP_PCB_LOCK(unp); 71375a67bf3SMatt Macy if (vp && unp->unp_vnode == NULL) { 71475a67bf3SMatt Macy mtx_unlock(vplock); 71575a67bf3SMatt Macy vp = NULL; 71675a67bf3SMatt Macy } 71775a67bf3SMatt Macy if (vp != NULL) { 718779f106aSGleb Smirnoff VOP_UNP_DETACH(vp); 719779f106aSGleb Smirnoff unp->unp_vnode = NULL; 720779f106aSGleb Smirnoff } 72175a67bf3SMatt Macy unp2 = unp->unp_conn; 72275a67bf3SMatt Macy unp_pcb_hold(unp); 72375a67bf3SMatt Macy if (unp2 != NULL) { 72475a67bf3SMatt Macy unp_pcb_hold(unp2); 72575a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freed); 72675a67bf3SMatt Macy unp_disconnect(unp, unp2); 72775a67bf3SMatt Macy if (unp_pcb_rele(unp2) == 0) 72875a67bf3SMatt Macy UNP_PCB_UNLOCK(unp2); 72975a67bf3SMatt Macy } 73075a67bf3SMatt Macy if (unp_pcb_rele(unp) == 0) 731e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 73275a67bf3SMatt Macy if (vp) { 73375a67bf3SMatt Macy mtx_unlock(vplock); 734779f106aSGleb Smirnoff vrele(vp); 735a152f8a3SRobert Watson } 73675a67bf3SMatt Macy } 737a152f8a3SRobert Watson 7382c899584SRobert Watson static int 739a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2) 740a29f300eSGarrett Wollman { 741e7c33e29SRobert Watson struct unpcb *unp, *unp2; 7420d9ce3a1SRobert Watson int error; 743a29f300eSGarrett Wollman 744e7c33e29SRobert Watson unp = so1->so_pcb; 7454d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_connect2: unp == NULL")); 746e7c33e29SRobert Watson unp2 = so2->so_pcb; 747e7c33e29SRobert Watson KASSERT(unp2 != NULL, ("uipc_connect2: unp2 == NULL")); 74875a67bf3SMatt Macy unp_pcb_lock2(unp, unp2); 7496a2989fdSMatthew N. Dodd error = unp_connect2(so1, so2, PRU_CONNECT2); 750e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 751e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 7520d9ce3a1SRobert Watson return (error); 753a29f300eSGarrett Wollman } 754a29f300eSGarrett Wollman 755bc725eafSRobert Watson static void 756a29f300eSGarrett Wollman uipc_detach(struct socket *so) 757a29f300eSGarrett Wollman { 758e7c33e29SRobert Watson struct unpcb *unp, *unp2; 75975a67bf3SMatt Macy struct mtx *vplock; 7609ae328fcSJohn Baldwin struct sockaddr_un *saved_unp_addr; 7616d32873cSRobert Watson struct vnode *vp; 7629ae328fcSJohn Baldwin int freeunp, local_unp_rights; 763a29f300eSGarrett Wollman 76440f2ac28SRobert Watson unp = sotounpcb(so); 7654d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_detach: unp == NULL")); 766e7c33e29SRobert Watson 767434ac8b6SMark Johnston vp = NULL; 768*c0874c34SMatt Macy vplock = NULL; 769434ac8b6SMark Johnston local_unp_rights = 0; 770434ac8b6SMark Johnston 771779f106aSGleb Smirnoff UNP_LINK_WLOCK(); 7726d32873cSRobert Watson LIST_REMOVE(unp, unp_link); 7736d32873cSRobert Watson unp->unp_gencnt = ++unp_gencnt; 7746d32873cSRobert Watson --unp_count; 77575a67bf3SMatt Macy UNP_LINK_WUNLOCK(); 776434ac8b6SMark Johnston 77775a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT(unp); 77875a67bf3SMatt Macy restart: 77975a67bf3SMatt Macy if ((vp = unp->unp_vnode) != NULL) { 78075a67bf3SMatt Macy vplock = mtx_pool_find(mtxpool_sleep, vp); 78175a67bf3SMatt Macy mtx_lock(vplock); 78275a67bf3SMatt Macy } 78375a67bf3SMatt Macy UNP_PCB_LOCK(unp); 78475a67bf3SMatt Macy if ((unp2 = unp->unp_conn) != NULL) { 78575a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freeunp); 78675a67bf3SMatt Macy if (freeunp) 78775a67bf3SMatt Macy unp2 = NULL; 78875a67bf3SMatt Macy } 78975a67bf3SMatt Macy if (unp->unp_vnode != vp && 79075a67bf3SMatt Macy unp->unp_vnode != NULL) { 791*c0874c34SMatt Macy if (vplock) 79275a67bf3SMatt Macy mtx_unlock(vplock); 79375a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 79475a67bf3SMatt Macy if (unp2) 79575a67bf3SMatt Macy UNP_PCB_UNLOCK(unp2); 79675a67bf3SMatt Macy goto restart; 79775a67bf3SMatt Macy } 79875a67bf3SMatt Macy if ((unp->unp_flags & UNP_NASCENT) != 0) { 79975a67bf3SMatt Macy if (unp2) 80075a67bf3SMatt Macy UNP_PCB_UNLOCK(unp2); 80175a67bf3SMatt Macy goto teardown; 80275a67bf3SMatt Macy } 8036d32873cSRobert Watson if ((vp = unp->unp_vnode) != NULL) { 804c7e41c8bSMikolaj Golub VOP_UNP_DETACH(vp); 8056d32873cSRobert Watson unp->unp_vnode = NULL; 8066d32873cSRobert Watson } 80775a67bf3SMatt Macy unp_pcb_hold(unp); 808e7c33e29SRobert Watson if (unp2 != NULL) { 80975a67bf3SMatt Macy unp_pcb_hold(unp2); 810e7c33e29SRobert Watson unp_disconnect(unp, unp2); 81175a67bf3SMatt Macy if (unp_pcb_rele(unp2) == 0) 812e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 813e7c33e29SRobert Watson } 81475a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 81575a67bf3SMatt Macy UNP_REF_LIST_LOCK(); 8166d32873cSRobert Watson while (!LIST_EMPTY(&unp->unp_refs)) { 8176d32873cSRobert Watson struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 818e7c33e29SRobert Watson 81975a67bf3SMatt Macy unp_pcb_hold(ref); 82075a67bf3SMatt Macy UNP_REF_LIST_UNLOCK(); 82175a67bf3SMatt Macy 82275a67bf3SMatt Macy MPASS(ref != unp); 82375a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT(ref); 824afc055d9SEd Schouten unp_drop(ref); 82575a67bf3SMatt Macy UNP_REF_LIST_LOCK(); 8266d32873cSRobert Watson } 82775a67bf3SMatt Macy 82875a67bf3SMatt Macy UNP_REF_LIST_UNLOCK(); 82975a67bf3SMatt Macy UNP_PCB_LOCK(unp); 83075a67bf3SMatt Macy freeunp = unp_pcb_rele(unp); 83175a67bf3SMatt Macy MPASS(freeunp == 0); 832397c19d1SJeff Roberson local_unp_rights = unp_rights; 833434ac8b6SMark Johnston teardown: 8346d32873cSRobert Watson unp->unp_socket->so_pcb = NULL; 8359ae328fcSJohn Baldwin saved_unp_addr = unp->unp_addr; 8369ae328fcSJohn Baldwin unp->unp_addr = NULL; 83775a67bf3SMatt Macy unp->unp_socket = NULL; 83875a67bf3SMatt Macy freeunp = unp_pcb_rele(unp); 8399ae328fcSJohn Baldwin if (saved_unp_addr != NULL) 8401ede983cSDag-Erling Smørgrav free(saved_unp_addr, M_SONAME); 84175a67bf3SMatt Macy if (!freeunp) 8426e2faa24SRobert Watson UNP_PCB_UNLOCK(unp); 84375a67bf3SMatt Macy if (vp) { 84475a67bf3SMatt Macy mtx_unlock(vplock); 8456d32873cSRobert Watson vrele(vp); 84675a67bf3SMatt Macy } 8476d32873cSRobert Watson if (local_unp_rights) 848daee0f0bSKonstantin Belousov taskqueue_enqueue_timeout(taskqueue_thread, &unp_gc_task, -1); 849a29f300eSGarrett Wollman } 850a29f300eSGarrett Wollman 851a29f300eSGarrett Wollman static int 852a29f300eSGarrett Wollman uipc_disconnect(struct socket *so) 853a29f300eSGarrett Wollman { 854e7c33e29SRobert Watson struct unpcb *unp, *unp2; 85575a67bf3SMatt Macy int freed; 856a29f300eSGarrett Wollman 85740f2ac28SRobert Watson unp = sotounpcb(so); 8584d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_disconnect: unp == NULL")); 859e7c33e29SRobert Watson 860e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 86175a67bf3SMatt Macy if ((unp2 = unp->unp_conn) == NULL) { 862e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 86375a67bf3SMatt Macy return (0); 86475a67bf3SMatt Macy } 86575a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freed); 86675a67bf3SMatt Macy if (__predict_false(freed)) { 86775a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 86875a67bf3SMatt Macy return (0); 86975a67bf3SMatt Macy } 87075a67bf3SMatt Macy unp_pcb_hold(unp2); 87175a67bf3SMatt Macy unp_pcb_hold(unp); 87275a67bf3SMatt Macy unp_disconnect(unp, unp2); 87375a67bf3SMatt Macy if (unp_pcb_rele(unp) == 0) 87475a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 87575a67bf3SMatt Macy if (unp_pcb_rele(unp2) == 0) 87675a67bf3SMatt Macy UNP_PCB_UNLOCK(unp2); 877e5aeaa0cSDag-Erling Smørgrav return (0); 878a29f300eSGarrett Wollman } 879a29f300eSGarrett Wollman 880a29f300eSGarrett Wollman static int 881d374e81eSRobert Watson uipc_listen(struct socket *so, int backlog, struct thread *td) 882a29f300eSGarrett Wollman { 88340f2ac28SRobert Watson struct unpcb *unp; 8840d9ce3a1SRobert Watson int error; 885a29f300eSGarrett Wollman 886beb4b312SGleb Smirnoff if (so->so_type != SOCK_STREAM && so->so_type != SOCK_SEQPACKET) 887beb4b312SGleb Smirnoff return (EOPNOTSUPP); 888beb4b312SGleb Smirnoff 88940f2ac28SRobert Watson unp = sotounpcb(so); 8904d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_listen: unp == NULL")); 891e7c33e29SRobert Watson 892e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 8934d4b555eSRobert Watson if (unp->unp_vnode == NULL) { 89447a84387SEd Schouten /* Already connected or not bound to an address. */ 89547a84387SEd Schouten error = unp->unp_conn != NULL ? EINVAL : EDESTADDRREQ; 896e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 89747a84387SEd Schouten return (error); 89840f2ac28SRobert Watson } 899e7c33e29SRobert Watson 900e7c33e29SRobert Watson SOCK_LOCK(so); 901e7c33e29SRobert Watson error = solisten_proto_check(so); 902e7c33e29SRobert Watson if (error == 0) { 903e7c33e29SRobert Watson cru2x(td->td_ucred, &unp->unp_peercred); 904e7c33e29SRobert Watson solisten_proto(so, backlog); 905e7c33e29SRobert Watson } 906e7c33e29SRobert Watson SOCK_UNLOCK(so); 907e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 9080d9ce3a1SRobert Watson return (error); 909a29f300eSGarrett Wollman } 910a29f300eSGarrett Wollman 911a29f300eSGarrett Wollman static int 91257bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 913a29f300eSGarrett Wollman { 914e7c33e29SRobert Watson struct unpcb *unp, *unp2; 9150d9ce3a1SRobert Watson const struct sockaddr *sa; 916a29f300eSGarrett Wollman 9174d4b555eSRobert Watson unp = sotounpcb(so); 9184d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL")); 919e7c33e29SRobert Watson 9200d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 921afd9f91cSJohn Baldwin UNP_LINK_RLOCK(); 922bdc5f6a3SHajimu UMEMOTO /* 923e7c33e29SRobert Watson * XXX: It seems that this test always fails even when connection is 924e7c33e29SRobert Watson * established. So, this else clause is added as workaround to 925e7c33e29SRobert Watson * return PF_LOCAL sockaddr. 926bdc5f6a3SHajimu UMEMOTO */ 927e7c33e29SRobert Watson unp2 = unp->unp_conn; 928e7c33e29SRobert Watson if (unp2 != NULL) { 929e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 930e7c33e29SRobert Watson if (unp2->unp_addr != NULL) 931afd9f91cSJohn Baldwin sa = (struct sockaddr *) unp2->unp_addr; 932e7c33e29SRobert Watson else 9330d9ce3a1SRobert Watson sa = &sun_noname; 9340d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 935e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 936e7c33e29SRobert Watson } else { 937e7c33e29SRobert Watson sa = &sun_noname; 938e7c33e29SRobert Watson bcopy(sa, *nam, sa->sa_len); 939e7c33e29SRobert Watson } 940afd9f91cSJohn Baldwin UNP_LINK_RUNLOCK(); 941e5aeaa0cSDag-Erling Smørgrav return (0); 942a29f300eSGarrett Wollman } 943a29f300eSGarrett Wollman 944a29f300eSGarrett Wollman static int 945a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 946a29f300eSGarrett Wollman { 947e7c33e29SRobert Watson struct unpcb *unp, *unp2; 948a29f300eSGarrett Wollman struct socket *so2; 949337cc6b6SRobert Watson u_int mbcnt, sbcc; 950a29f300eSGarrett Wollman 95140f2ac28SRobert Watson unp = sotounpcb(so); 9522b21d0e8SGleb Smirnoff KASSERT(unp != NULL, ("%s: unp == NULL", __func__)); 9532b21d0e8SGleb Smirnoff KASSERT(so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET, 9542b21d0e8SGleb Smirnoff ("%s: socktype %d", __func__, so->so_type)); 955e7c33e29SRobert Watson 956df8bae1dSRodney W. Grimes /* 957e7c33e29SRobert Watson * Adjust backpressure on sender and wakeup any waiting to write. 958e7c33e29SRobert Watson * 959d7924b70SRobert Watson * The unp lock is acquired to maintain the validity of the unp_conn 960d7924b70SRobert Watson * pointer; no lock on unp2 is required as unp2->unp_socket will be 961d7924b70SRobert Watson * static as long as we don't permit unp2 to disconnect from unp, 962d7924b70SRobert Watson * which is prevented by the lock on unp. We cache values from 963d7924b70SRobert Watson * so_rcv to avoid holding the so_rcv lock over the entire 964d7924b70SRobert Watson * transaction on the remote so_snd. 965df8bae1dSRodney W. Grimes */ 966337cc6b6SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 967337cc6b6SRobert Watson mbcnt = so->so_rcv.sb_mbcnt; 9682b21d0e8SGleb Smirnoff sbcc = sbavail(&so->so_rcv); 969337cc6b6SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 970c2090e73SAlan Somers /* 971c2090e73SAlan Somers * There is a benign race condition at this point. If we're planning to 972c2090e73SAlan Somers * clear SB_STOP, but uipc_send is called on the connected socket at 973c2090e73SAlan Somers * this instant, it might add data to the sockbuf and set SB_STOP. Then 974c2090e73SAlan Somers * we would erroneously clear SB_STOP below, even though the sockbuf is 975c2090e73SAlan Somers * full. The race is benign because the only ill effect is to allow the 976c2090e73SAlan Somers * sockbuf to exceed its size limit, and the size limits are not 977c2090e73SAlan Somers * strictly guaranteed anyway. 978c2090e73SAlan Somers */ 979e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 980e7c33e29SRobert Watson unp2 = unp->unp_conn; 981e7c33e29SRobert Watson if (unp2 == NULL) { 982e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 983e7c33e29SRobert Watson return (0); 984337cc6b6SRobert Watson } 985e7c33e29SRobert Watson so2 = unp2->unp_socket; 986337cc6b6SRobert Watson SOCKBUF_LOCK(&so2->so_snd); 987c2090e73SAlan Somers if (sbcc < so2->so_snd.sb_hiwat && mbcnt < so2->so_snd.sb_mbmax) 988c2090e73SAlan Somers so2->so_snd.sb_flags &= ~SB_STOP; 9891e4d7da7SRobert Watson sowwakeup_locked(so2); 990e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 991e5aeaa0cSDag-Erling Smørgrav return (0); 992a29f300eSGarrett Wollman } 993df8bae1dSRodney W. Grimes 994a29f300eSGarrett Wollman static int 99575a67bf3SMatt Macy connect_internal(struct socket *so, struct sockaddr *nam, struct thread *td) 99675a67bf3SMatt Macy { 99775a67bf3SMatt Macy int error; 99875a67bf3SMatt Macy struct unpcb *unp; 99975a67bf3SMatt Macy 100075a67bf3SMatt Macy unp = so->so_pcb; 100175a67bf3SMatt Macy if (unp->unp_conn != NULL) 100275a67bf3SMatt Macy return (EISCONN); 100375a67bf3SMatt Macy error = unp_connect(so, nam, td); 100475a67bf3SMatt Macy if (error) 100575a67bf3SMatt Macy return (error); 100675a67bf3SMatt Macy UNP_PCB_LOCK(unp); 100775a67bf3SMatt Macy if (unp->unp_conn == NULL) { 100875a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 100975a67bf3SMatt Macy if (error == 0) 101075a67bf3SMatt Macy error = ENOTCONN; 101175a67bf3SMatt Macy } 101275a67bf3SMatt Macy return (error); 101375a67bf3SMatt Macy } 101475a67bf3SMatt Macy 101575a67bf3SMatt Macy 101675a67bf3SMatt Macy static int 101757bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 1018b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 1019a29f300eSGarrett Wollman { 1020f3f49bbbSRobert Watson struct unpcb *unp, *unp2; 1021a29f300eSGarrett Wollman struct socket *so2; 1022c2090e73SAlan Somers u_int mbcnt, sbcc; 102375a67bf3SMatt Macy int freed, error; 1024a29f300eSGarrett Wollman 102540f2ac28SRobert Watson unp = sotounpcb(so); 10262b21d0e8SGleb Smirnoff KASSERT(unp != NULL, ("%s: unp == NULL", __func__)); 10272b21d0e8SGleb Smirnoff KASSERT(so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM || 10282b21d0e8SGleb Smirnoff so->so_type == SOCK_SEQPACKET, 10292b21d0e8SGleb Smirnoff ("%s: socktype %d", __func__, so->so_type)); 1030e7c33e29SRobert Watson 103175a67bf3SMatt Macy freed = error = 0; 1032a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 1033a29f300eSGarrett Wollman error = EOPNOTSUPP; 1034a29f300eSGarrett Wollman goto release; 1035a29f300eSGarrett Wollman } 1036fc3fcacfSRobert Watson if (control != NULL && (error = unp_internalize(&control, td))) 1037a29f300eSGarrett Wollman goto release; 103875a67bf3SMatt Macy 103975a67bf3SMatt Macy unp2 = NULL; 1040a29f300eSGarrett Wollman switch (so->so_type) { 1041a29f300eSGarrett Wollman case SOCK_DGRAM: 1042a29f300eSGarrett Wollman { 1043e7dd9a10SRobert Watson const struct sockaddr *from; 1044df8bae1dSRodney W. Grimes 1045fc3fcacfSRobert Watson if (nam != NULL) { 104675a67bf3SMatt Macy /* 104775a67bf3SMatt Macy * We return with UNP_PCB_LOCK_HELD so we know that 104875a67bf3SMatt Macy * the reference is live if the pointer is valid. 104975a67bf3SMatt Macy */ 105075a67bf3SMatt Macy if ((error = connect_internal(so, nam, td))) 1051df8bae1dSRodney W. Grimes break; 105275a67bf3SMatt Macy MPASS(unp->unp_conn != NULL); 1053e7c33e29SRobert Watson unp2 = unp->unp_conn; 105475a67bf3SMatt Macy } else { 105575a67bf3SMatt Macy UNP_PCB_LOCK(unp); 105660a5ef26SRobert Watson 1057b5ff0914SRobert Watson /* 1058b5ff0914SRobert Watson * Because connect() and send() are non-atomic in a sendto() 1059b5ff0914SRobert Watson * with a target address, it's possible that the socket will 1060b5ff0914SRobert Watson * have disconnected before the send() can run. In that case 1061b5ff0914SRobert Watson * return the slightly counter-intuitive but otherwise 1062b5ff0914SRobert Watson * correct error that the socket is not connected. 1063b5ff0914SRobert Watson */ 106475a67bf3SMatt Macy if ((unp2 = unp->unp_conn) == NULL) { 106575a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 1066b5ff0914SRobert Watson error = ENOTCONN; 1067b5ff0914SRobert Watson break; 1068b5ff0914SRobert Watson } 106975a67bf3SMatt Macy } 107075a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freed); 107175a67bf3SMatt Macy if (__predict_false(freed)) { 107275a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 107375a67bf3SMatt Macy error = ENOTCONN; 107475a67bf3SMatt Macy break; 107575a67bf3SMatt Macy } 107675a67bf3SMatt Macy /* 107775a67bf3SMatt Macy * The socket referencing unp2 may have been closed 107875a67bf3SMatt Macy * or unp may have been disconnected if the unp lock 107975a67bf3SMatt Macy * was dropped to acquire unp2. 108075a67bf3SMatt Macy */ 108175a67bf3SMatt Macy if (__predict_false(unp->unp_conn == NULL) || 108275a67bf3SMatt Macy unp2->unp_socket == NULL) { 108375a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 108475a67bf3SMatt Macy if (unp_pcb_rele(unp2) == 0) 108575a67bf3SMatt Macy UNP_PCB_UNLOCK(unp2); 108675a67bf3SMatt Macy error = ENOTCONN; 108775a67bf3SMatt Macy break; 108875a67bf3SMatt Macy } 1089ede6e136SRobert Watson if (unp2->unp_flags & UNP_WANTCRED) 1090ede6e136SRobert Watson control = unp_addsockcred(td, control); 1091fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 109257bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 1093df8bae1dSRodney W. Grimes else 1094df8bae1dSRodney W. Grimes from = &sun_noname; 1095ede6e136SRobert Watson so2 = unp2->unp_socket; 1096a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 10976dde7ecbSPeter Wemm if (sbappendaddr_locked(&so2->so_rcv, from, m, 10988de34a88SAlan Somers control)) { 10991e4d7da7SRobert Watson sorwakeup_locked(so2); 1100fc3fcacfSRobert Watson m = NULL; 1101fc3fcacfSRobert Watson control = NULL; 1102e5aeaa0cSDag-Erling Smørgrav } else { 1103a34b7046SRobert Watson SOCKBUF_UNLOCK(&so2->so_rcv); 1104df8bae1dSRodney W. Grimes error = ENOBUFS; 1105e5aeaa0cSDag-Erling Smørgrav } 110675a67bf3SMatt Macy if (nam != NULL) 1107e7c33e29SRobert Watson unp_disconnect(unp, unp2); 1108e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1109ede6e136SRobert Watson UNP_PCB_UNLOCK(unp); 1110df8bae1dSRodney W. Grimes break; 1111df8bae1dSRodney W. Grimes } 1112df8bae1dSRodney W. Grimes 111384d61770SRobert Watson case SOCK_SEQPACKET: 1114df8bae1dSRodney W. Grimes case SOCK_STREAM: 1115402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 1116fc3fcacfSRobert Watson if (nam != NULL) { 111775a67bf3SMatt Macy if ((error = connect_internal(so, nam, td))) 111875a67bf3SMatt Macy break; 1119402cc72dSDavid Greenman } else { 1120402cc72dSDavid Greenman error = ENOTCONN; 1121402cc72dSDavid Greenman break; 1122402cc72dSDavid Greenman } 112375a67bf3SMatt Macy } else if ((unp2 = unp->unp_conn) == NULL) { 112475a67bf3SMatt Macy error = ENOTCONN; 112575a67bf3SMatt Macy break; 112675a67bf3SMatt Macy } else if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 1127df8bae1dSRodney W. Grimes error = EPIPE; 1128df8bae1dSRodney W. Grimes break; 112975a67bf3SMatt Macy } else { 113075a67bf3SMatt Macy UNP_PCB_LOCK(unp); 113175a67bf3SMatt Macy if ((unp2 = unp->unp_conn) == NULL) { 113275a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 1133b5ff0914SRobert Watson error = ENOTCONN; 1134b5ff0914SRobert Watson break; 1135b5ff0914SRobert Watson } 113675a67bf3SMatt Macy } 113775a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freed); 113875a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 113975a67bf3SMatt Macy if (__predict_false(freed)) { 114075a67bf3SMatt Macy error = ENOTCONN; 114175a67bf3SMatt Macy break; 114275a67bf3SMatt Macy } 114375a67bf3SMatt Macy if ((so2 = unp2->unp_socket) == NULL) { 114475a67bf3SMatt Macy UNP_PCB_UNLOCK(unp2); 114575a67bf3SMatt Macy error = ENOTCONN; 114675a67bf3SMatt Macy break; 114775a67bf3SMatt Macy } 1148a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 1149f3f49bbbSRobert Watson if (unp2->unp_flags & UNP_WANTCRED) { 11506a2989fdSMatthew N. Dodd /* 1151716963cbSGleb Smirnoff * Credentials are passed only once on SOCK_STREAM 1152716963cbSGleb Smirnoff * and SOCK_SEQPACKET. 11536a2989fdSMatthew N. Dodd */ 1154f3f49bbbSRobert Watson unp2->unp_flags &= ~UNP_WANTCRED; 11556a2989fdSMatthew N. Dodd control = unp_addsockcred(td, control); 11566a2989fdSMatthew N. Dodd } 1157df8bae1dSRodney W. Grimes /* 11581c381b19SRobert Watson * Send to paired receive port, and then reduce send buffer 11591c381b19SRobert Watson * hiwater marks to maintain backpressure. Wake up readers. 1160df8bae1dSRodney W. Grimes */ 116184d61770SRobert Watson switch (so->so_type) { 116284d61770SRobert Watson case SOCK_STREAM: 1163fc3fcacfSRobert Watson if (control != NULL) { 116484d61770SRobert Watson if (sbappendcontrol_locked(&so2->so_rcv, m, 116584d61770SRobert Watson control)) 1166fc3fcacfSRobert Watson control = NULL; 1167e7c33e29SRobert Watson } else 1168829fae90SGleb Smirnoff sbappend_locked(&so2->so_rcv, m, flags); 116984d61770SRobert Watson break; 117084d61770SRobert Watson 117184d61770SRobert Watson case SOCK_SEQPACKET: { 117284d61770SRobert Watson const struct sockaddr *from; 117384d61770SRobert Watson 117484d61770SRobert Watson from = &sun_noname; 11758de34a88SAlan Somers /* 11768de34a88SAlan Somers * Don't check for space available in so2->so_rcv. 11778de34a88SAlan Somers * Unix domain sockets only check for space in the 11788de34a88SAlan Somers * sending sockbuf, and that check is performed one 11798de34a88SAlan Somers * level up the stack. 11808de34a88SAlan Somers */ 11818de34a88SAlan Somers if (sbappendaddr_nospacecheck_locked(&so2->so_rcv, 11828de34a88SAlan Somers from, m, control)) 118384d61770SRobert Watson control = NULL; 118484d61770SRobert Watson break; 118584d61770SRobert Watson } 118684d61770SRobert Watson } 118784d61770SRobert Watson 1188c2090e73SAlan Somers mbcnt = so2->so_rcv.sb_mbcnt; 11892b21d0e8SGleb Smirnoff sbcc = sbavail(&so2->so_rcv); 11902b21d0e8SGleb Smirnoff if (sbcc) 1191337cc6b6SRobert Watson sorwakeup_locked(so2); 11922b21d0e8SGleb Smirnoff else 11932b21d0e8SGleb Smirnoff SOCKBUF_UNLOCK(&so2->so_rcv); 1194337cc6b6SRobert Watson 1195c2090e73SAlan Somers /* 1196c2090e73SAlan Somers * The PCB lock on unp2 protects the SB_STOP flag. Without it, 1197c2090e73SAlan Somers * it would be possible for uipc_rcvd to be called at this 1198c2090e73SAlan Somers * point, drain the receiving sockbuf, clear SB_STOP, and then 1199c2090e73SAlan Somers * we would set SB_STOP below. That could lead to an empty 1200c2090e73SAlan Somers * sockbuf having SB_STOP set 1201c2090e73SAlan Somers */ 1202337cc6b6SRobert Watson SOCKBUF_LOCK(&so->so_snd); 1203c2090e73SAlan Somers if (sbcc >= so->so_snd.sb_hiwat || mbcnt >= so->so_snd.sb_mbmax) 1204c2090e73SAlan Somers so->so_snd.sb_flags |= SB_STOP; 12057abe2ac2SAlan Cox SOCKBUF_UNLOCK(&so->so_snd); 1206e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1207fc3fcacfSRobert Watson m = NULL; 1208df8bae1dSRodney W. Grimes break; 1209df8bae1dSRodney W. Grimes } 1210a29f300eSGarrett Wollman 12116b8fda4dSGarrett Wollman /* 121260a5ef26SRobert Watson * PRUS_EOF is equivalent to pru_send followed by pru_shutdown. 12136b8fda4dSGarrett Wollman */ 1214a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 1215ede6e136SRobert Watson UNP_PCB_LOCK(unp); 12166b8fda4dSGarrett Wollman socantsendmore(so); 12176b8fda4dSGarrett Wollman unp_shutdown(unp); 1218e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1219ede6e136SRobert Watson } 1220fc3fcacfSRobert Watson if (control != NULL && error != 0) 122199ab95dbSMark Johnston unp_dispose_mbuf(control); 1222bd508d39SDon Lewis 1223a29f300eSGarrett Wollman release: 1224fc3fcacfSRobert Watson if (control != NULL) 1225a29f300eSGarrett Wollman m_freem(control); 1226100db364SGleb Smirnoff /* 1227100db364SGleb Smirnoff * In case of PRUS_NOTREADY, uipc_ready() is responsible 1228100db364SGleb Smirnoff * for freeing memory. 1229100db364SGleb Smirnoff */ 1230100db364SGleb Smirnoff if (m != NULL && (flags & PRUS_NOTREADY) == 0) 1231a29f300eSGarrett Wollman m_freem(m); 1232e5aeaa0cSDag-Erling Smørgrav return (error); 1233a29f300eSGarrett Wollman } 1234df8bae1dSRodney W. Grimes 1235a29f300eSGarrett Wollman static int 1236c80ea19bSGleb Smirnoff uipc_ready(struct socket *so, struct mbuf *m, int count) 1237c80ea19bSGleb Smirnoff { 1238c80ea19bSGleb Smirnoff struct unpcb *unp, *unp2; 1239c80ea19bSGleb Smirnoff struct socket *so2; 1240c80ea19bSGleb Smirnoff int error; 1241c80ea19bSGleb Smirnoff 1242c80ea19bSGleb Smirnoff unp = sotounpcb(so); 1243c80ea19bSGleb Smirnoff 1244c80ea19bSGleb Smirnoff UNP_LINK_RLOCK(); 1245100db364SGleb Smirnoff if ((unp2 = unp->unp_conn) == NULL) { 1246100db364SGleb Smirnoff UNP_LINK_RUNLOCK(); 1247100db364SGleb Smirnoff for (int i = 0; i < count; i++) 1248100db364SGleb Smirnoff m = m_free(m); 1249100db364SGleb Smirnoff return (ECONNRESET); 1250100db364SGleb Smirnoff } 1251c80ea19bSGleb Smirnoff UNP_PCB_LOCK(unp2); 1252c80ea19bSGleb Smirnoff so2 = unp2->unp_socket; 1253c80ea19bSGleb Smirnoff 1254c80ea19bSGleb Smirnoff SOCKBUF_LOCK(&so2->so_rcv); 1255c80ea19bSGleb Smirnoff if ((error = sbready(&so2->so_rcv, m, count)) == 0) 1256c80ea19bSGleb Smirnoff sorwakeup_locked(so2); 1257c80ea19bSGleb Smirnoff else 1258c80ea19bSGleb Smirnoff SOCKBUF_UNLOCK(&so2->so_rcv); 1259c80ea19bSGleb Smirnoff 1260c80ea19bSGleb Smirnoff UNP_PCB_UNLOCK(unp2); 1261c80ea19bSGleb Smirnoff UNP_LINK_RUNLOCK(); 1262c80ea19bSGleb Smirnoff 1263c80ea19bSGleb Smirnoff return (error); 1264c80ea19bSGleb Smirnoff } 1265c80ea19bSGleb Smirnoff 1266c80ea19bSGleb Smirnoff static int 1267a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 1268a29f300eSGarrett Wollman { 1269c2090e73SAlan Somers struct unpcb *unp; 1270a29f300eSGarrett Wollman 127140f2ac28SRobert Watson unp = sotounpcb(so); 12724d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sense: unp == NULL")); 1273e7c33e29SRobert Watson 1274a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 1275e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 1276f3732fd1SPoul-Henning Kamp sb->st_dev = NODEV; 1277df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 12786f782c46SJeffrey Hsu unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 1279a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 1280e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1281df8bae1dSRodney W. Grimes return (0); 1282a29f300eSGarrett Wollman } 1283df8bae1dSRodney W. Grimes 1284a29f300eSGarrett Wollman static int 1285a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 1286a29f300eSGarrett Wollman { 128740f2ac28SRobert Watson struct unpcb *unp; 1288df8bae1dSRodney W. Grimes 128940f2ac28SRobert Watson unp = sotounpcb(so); 12904d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_shutdown: unp == NULL")); 1291e7c33e29SRobert Watson 1292e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 1293a29f300eSGarrett Wollman socantsendmore(so); 1294a29f300eSGarrett Wollman unp_shutdown(unp); 1295e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1296e5aeaa0cSDag-Erling Smørgrav return (0); 1297a29f300eSGarrett Wollman } 1298df8bae1dSRodney W. Grimes 1299a29f300eSGarrett Wollman static int 130057bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 1301a29f300eSGarrett Wollman { 130240f2ac28SRobert Watson struct unpcb *unp; 13030d9ce3a1SRobert Watson const struct sockaddr *sa; 1304a29f300eSGarrett Wollman 13054d4b555eSRobert Watson unp = sotounpcb(so); 13064d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL")); 1307e7c33e29SRobert Watson 13080d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 1309e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 1310fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 13110d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_addr; 131283f3198bSThomas Moestl else 13130d9ce3a1SRobert Watson sa = &sun_noname; 13140d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 1315e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1316e5aeaa0cSDag-Erling Smørgrav return (0); 1317df8bae1dSRodney W. Grimes } 1318a29f300eSGarrett Wollman 1319fa9402f2SRobert Watson static struct pr_usrreqs uipc_usrreqs_dgram = { 1320756d52a1SPoul-Henning Kamp .pru_abort = uipc_abort, 1321756d52a1SPoul-Henning Kamp .pru_accept = uipc_accept, 1322756d52a1SPoul-Henning Kamp .pru_attach = uipc_attach, 1323756d52a1SPoul-Henning Kamp .pru_bind = uipc_bind, 13247493f24eSPawel Jakub Dawidek .pru_bindat = uipc_bindat, 1325756d52a1SPoul-Henning Kamp .pru_connect = uipc_connect, 13267493f24eSPawel Jakub Dawidek .pru_connectat = uipc_connectat, 1327756d52a1SPoul-Henning Kamp .pru_connect2 = uipc_connect2, 1328756d52a1SPoul-Henning Kamp .pru_detach = uipc_detach, 1329756d52a1SPoul-Henning Kamp .pru_disconnect = uipc_disconnect, 1330756d52a1SPoul-Henning Kamp .pru_listen = uipc_listen, 1331756d52a1SPoul-Henning Kamp .pru_peeraddr = uipc_peeraddr, 1332756d52a1SPoul-Henning Kamp .pru_rcvd = uipc_rcvd, 1333756d52a1SPoul-Henning Kamp .pru_send = uipc_send, 1334756d52a1SPoul-Henning Kamp .pru_sense = uipc_sense, 1335756d52a1SPoul-Henning Kamp .pru_shutdown = uipc_shutdown, 1336756d52a1SPoul-Henning Kamp .pru_sockaddr = uipc_sockaddr, 1337fa9402f2SRobert Watson .pru_soreceive = soreceive_dgram, 1338fa9402f2SRobert Watson .pru_close = uipc_close, 1339fa9402f2SRobert Watson }; 1340fa9402f2SRobert Watson 134184d61770SRobert Watson static struct pr_usrreqs uipc_usrreqs_seqpacket = { 134284d61770SRobert Watson .pru_abort = uipc_abort, 134384d61770SRobert Watson .pru_accept = uipc_accept, 134484d61770SRobert Watson .pru_attach = uipc_attach, 134584d61770SRobert Watson .pru_bind = uipc_bind, 13467493f24eSPawel Jakub Dawidek .pru_bindat = uipc_bindat, 134784d61770SRobert Watson .pru_connect = uipc_connect, 13487493f24eSPawel Jakub Dawidek .pru_connectat = uipc_connectat, 134984d61770SRobert Watson .pru_connect2 = uipc_connect2, 135084d61770SRobert Watson .pru_detach = uipc_detach, 135184d61770SRobert Watson .pru_disconnect = uipc_disconnect, 135284d61770SRobert Watson .pru_listen = uipc_listen, 135384d61770SRobert Watson .pru_peeraddr = uipc_peeraddr, 135484d61770SRobert Watson .pru_rcvd = uipc_rcvd, 135584d61770SRobert Watson .pru_send = uipc_send, 135684d61770SRobert Watson .pru_sense = uipc_sense, 135784d61770SRobert Watson .pru_shutdown = uipc_shutdown, 135884d61770SRobert Watson .pru_sockaddr = uipc_sockaddr, 135984d61770SRobert Watson .pru_soreceive = soreceive_generic, /* XXX: or...? */ 136084d61770SRobert Watson .pru_close = uipc_close, 136184d61770SRobert Watson }; 136284d61770SRobert Watson 1363fa9402f2SRobert Watson static struct pr_usrreqs uipc_usrreqs_stream = { 1364fa9402f2SRobert Watson .pru_abort = uipc_abort, 1365fa9402f2SRobert Watson .pru_accept = uipc_accept, 1366fa9402f2SRobert Watson .pru_attach = uipc_attach, 1367fa9402f2SRobert Watson .pru_bind = uipc_bind, 13687493f24eSPawel Jakub Dawidek .pru_bindat = uipc_bindat, 1369fa9402f2SRobert Watson .pru_connect = uipc_connect, 13707493f24eSPawel Jakub Dawidek .pru_connectat = uipc_connectat, 1371fa9402f2SRobert Watson .pru_connect2 = uipc_connect2, 1372fa9402f2SRobert Watson .pru_detach = uipc_detach, 1373fa9402f2SRobert Watson .pru_disconnect = uipc_disconnect, 1374fa9402f2SRobert Watson .pru_listen = uipc_listen, 1375fa9402f2SRobert Watson .pru_peeraddr = uipc_peeraddr, 1376fa9402f2SRobert Watson .pru_rcvd = uipc_rcvd, 1377fa9402f2SRobert Watson .pru_send = uipc_send, 1378c80ea19bSGleb Smirnoff .pru_ready = uipc_ready, 1379fa9402f2SRobert Watson .pru_sense = uipc_sense, 1380fa9402f2SRobert Watson .pru_shutdown = uipc_shutdown, 1381fa9402f2SRobert Watson .pru_sockaddr = uipc_sockaddr, 1382fa9402f2SRobert Watson .pru_soreceive = soreceive_generic, 1383a152f8a3SRobert Watson .pru_close = uipc_close, 1384a29f300eSGarrett Wollman }; 1385df8bae1dSRodney W. Grimes 13860b36cd25SRobert Watson static int 1387892af6b9SRobert Watson uipc_ctloutput(struct socket *so, struct sockopt *sopt) 13880c1bb4fbSDima Dorfman { 138940f2ac28SRobert Watson struct unpcb *unp; 13900d9ce3a1SRobert Watson struct xucred xu; 13916a2989fdSMatthew N. Dodd int error, optval; 13926a2989fdSMatthew N. Dodd 139396a041b5SMatthew N. Dodd if (sopt->sopt_level != 0) 139496a041b5SMatthew N. Dodd return (EINVAL); 139596a041b5SMatthew N. Dodd 13966a2989fdSMatthew N. Dodd unp = sotounpcb(so); 13974d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL")); 13986a2989fdSMatthew N. Dodd error = 0; 13990c1bb4fbSDima Dorfman switch (sopt->sopt_dir) { 14000c1bb4fbSDima Dorfman case SOPT_GET: 14010c1bb4fbSDima Dorfman switch (sopt->sopt_name) { 14020c1bb4fbSDima Dorfman case LOCAL_PEERCRED: 1403e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 14040c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC) 14050d9ce3a1SRobert Watson xu = unp->unp_peercred; 14060c1bb4fbSDima Dorfman else { 14070c1bb4fbSDima Dorfman if (so->so_type == SOCK_STREAM) 14080c1bb4fbSDima Dorfman error = ENOTCONN; 14090c1bb4fbSDima Dorfman else 14100c1bb4fbSDima Dorfman error = EINVAL; 14110c1bb4fbSDima Dorfman } 1412e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 14130d9ce3a1SRobert Watson if (error == 0) 14140d9ce3a1SRobert Watson error = sooptcopyout(sopt, &xu, sizeof(xu)); 14150c1bb4fbSDima Dorfman break; 1416e7c33e29SRobert Watson 14176a2989fdSMatthew N. Dodd case LOCAL_CREDS: 1418a6357845SRobert Watson /* Unlocked read. */ 14196a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0; 14206a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 14216a2989fdSMatthew N. Dodd break; 1422e7c33e29SRobert Watson 14236a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 1424a6357845SRobert Watson /* Unlocked read. */ 14256a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0; 14266a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 14276a2989fdSMatthew N. Dodd break; 1428e7c33e29SRobert Watson 14290c1bb4fbSDima Dorfman default: 14300c1bb4fbSDima Dorfman error = EOPNOTSUPP; 14310c1bb4fbSDima Dorfman break; 14320c1bb4fbSDima Dorfman } 14330c1bb4fbSDima Dorfman break; 1434e7c33e29SRobert Watson 14350c1bb4fbSDima Dorfman case SOPT_SET: 14366a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 14376a2989fdSMatthew N. Dodd case LOCAL_CREDS: 14386a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 14396a2989fdSMatthew N. Dodd error = sooptcopyin(sopt, &optval, sizeof(optval), 14406a2989fdSMatthew N. Dodd sizeof(optval)); 14416a2989fdSMatthew N. Dodd if (error) 14426a2989fdSMatthew N. Dodd break; 14436a2989fdSMatthew N. Dodd 1444e7c33e29SRobert Watson #define OPTSET(bit) do { \ 1445e7c33e29SRobert Watson UNP_PCB_LOCK(unp); \ 14466a2989fdSMatthew N. Dodd if (optval) \ 14476a2989fdSMatthew N. Dodd unp->unp_flags |= bit; \ 14486a2989fdSMatthew N. Dodd else \ 1449e7c33e29SRobert Watson unp->unp_flags &= ~bit; \ 1450e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); \ 1451e7c33e29SRobert Watson } while (0) 14526a2989fdSMatthew N. Dodd 14536a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 14546a2989fdSMatthew N. Dodd case LOCAL_CREDS: 14556a2989fdSMatthew N. Dodd OPTSET(UNP_WANTCRED); 14566a2989fdSMatthew N. Dodd break; 1457e7c33e29SRobert Watson 14586a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 14596a2989fdSMatthew N. Dodd OPTSET(UNP_CONNWAIT); 14606a2989fdSMatthew N. Dodd break; 1461e7c33e29SRobert Watson 14626a2989fdSMatthew N. Dodd default: 14636a2989fdSMatthew N. Dodd break; 14646a2989fdSMatthew N. Dodd } 14656a2989fdSMatthew N. Dodd break; 14666a2989fdSMatthew N. Dodd #undef OPTSET 14676a2989fdSMatthew N. Dodd default: 14686a2989fdSMatthew N. Dodd error = ENOPROTOOPT; 14696a2989fdSMatthew N. Dodd break; 14706a2989fdSMatthew N. Dodd } 1471abb886faSMatthew N. Dodd break; 1472e7c33e29SRobert Watson 14730c1bb4fbSDima Dorfman default: 14740c1bb4fbSDima Dorfman error = EOPNOTSUPP; 14750c1bb4fbSDima Dorfman break; 14760c1bb4fbSDima Dorfman } 14770c1bb4fbSDima Dorfman return (error); 14780c1bb4fbSDima Dorfman } 14790c1bb4fbSDima Dorfman 1480f708ef1bSPoul-Henning Kamp static int 1481892af6b9SRobert Watson unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1482df8bae1dSRodney W. Grimes { 14837493f24eSPawel Jakub Dawidek 14847493f24eSPawel Jakub Dawidek return (unp_connectat(AT_FDCWD, so, nam, td)); 14857493f24eSPawel Jakub Dawidek } 14867493f24eSPawel Jakub Dawidek 14877493f24eSPawel Jakub Dawidek static int 14887493f24eSPawel Jakub Dawidek unp_connectat(int fd, struct socket *so, struct sockaddr *nam, 14897493f24eSPawel Jakub Dawidek struct thread *td) 14907493f24eSPawel Jakub Dawidek { 1491892af6b9SRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam; 1492892af6b9SRobert Watson struct vnode *vp; 1493779f106aSGleb Smirnoff struct socket *so2; 1494b295bdcdSRobert Watson struct unpcb *unp, *unp2, *unp3; 1495df8bae1dSRodney W. Grimes struct nameidata nd; 149657bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 14970d9ce3a1SRobert Watson struct sockaddr *sa; 14987008be5bSPawel Jakub Dawidek cap_rights_t rights; 149975a67bf3SMatt Macy int error, len, freed; 150075a67bf3SMatt Macy struct mtx *vplock; 15010d9ce3a1SRobert Watson 1502cb7df69bSKevin Lo if (nam->sa_family != AF_UNIX) 1503cb7df69bSKevin Lo return (EAFNOSUPPORT); 1504a06534c3SBjoern A. Zeeb if (nam->sa_len > sizeof(struct sockaddr_un)) 1505a06534c3SBjoern A. Zeeb return (EINVAL); 150657bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 150757bf258eSGarrett Wollman if (len <= 0) 1508e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 15097928893dSEd Maste bcopy(soun->sun_path, buf, len); 15107928893dSEd Maste buf[len] = 0; 1511e7c33e29SRobert Watson 151275a67bf3SMatt Macy unp = sotounpcb(so); 1513e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 15144f1f0ef5SRobert Watson if (unp->unp_flags & UNP_CONNECTING) { 1515e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 15164f1f0ef5SRobert Watson return (EALREADY); 15174f1f0ef5SRobert Watson } 151805102f04SRobert Watson unp->unp_flags |= UNP_CONNECTING; 1519e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1520e7c33e29SRobert Watson 15210d9ce3a1SRobert Watson sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 15227493f24eSPawel Jakub Dawidek NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, 15237008be5bSPawel Jakub Dawidek UIO_SYSSPACE, buf, fd, cap_rights_init(&rights, CAP_CONNECTAT), td); 1524797f2d22SPoul-Henning Kamp error = namei(&nd); 1525797f2d22SPoul-Henning Kamp if (error) 15260d9ce3a1SRobert Watson vp = NULL; 15270d9ce3a1SRobert Watson else 1528df8bae1dSRodney W. Grimes vp = nd.ni_vp; 15290d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_connect"); 1530762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 15310d9ce3a1SRobert Watson if (error) 15320d9ce3a1SRobert Watson goto bad; 15330d9ce3a1SRobert Watson 1534df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 1535df8bae1dSRodney W. Grimes error = ENOTSOCK; 1536df8bae1dSRodney W. Grimes goto bad; 1537df8bae1dSRodney W. Grimes } 15386fac927cSRobert Watson #ifdef MAC 153930d239bcSRobert Watson error = mac_vnode_check_open(td->td_ucred, vp, VWRITE | VREAD); 15406fac927cSRobert Watson if (error) 15416fac927cSRobert Watson goto bad; 15426fac927cSRobert Watson #endif 1543a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 1544797f2d22SPoul-Henning Kamp if (error) 1545df8bae1dSRodney W. Grimes goto bad; 1546e7c33e29SRobert Watson 1547b295bdcdSRobert Watson unp = sotounpcb(so); 15484d4b555eSRobert Watson KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 1549e7c33e29SRobert Watson 155075a67bf3SMatt Macy vplock = mtx_pool_find(mtxpool_sleep, vp); 155175a67bf3SMatt Macy mtx_lock(vplock); 15520c3c207fSGleb Smirnoff VOP_UNP_CONNECT(vp, &unp2); 15530c3c207fSGleb Smirnoff if (unp2 == NULL) { 1554df8bae1dSRodney W. Grimes error = ECONNREFUSED; 15552260c03dSRobert Watson goto bad2; 1556df8bae1dSRodney W. Grimes } 15570c3c207fSGleb Smirnoff so2 = unp2->unp_socket; 1558df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 1559df8bae1dSRodney W. Grimes error = EPROTOTYPE; 15602260c03dSRobert Watson goto bad2; 1561df8bae1dSRodney W. Grimes } 156275a67bf3SMatt Macy unp_pcb_lock2(unp, unp2); 1563df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 1564e7c33e29SRobert Watson if (so2->so_options & SO_ACCEPTCONN) { 15651fb51a12SBjoern A. Zeeb CURVNET_SET(so2->so_vnet); 1566779f106aSGleb Smirnoff so2 = sonewconn(so2, 0); 15671fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 1568e7c33e29SRobert Watson } else 1569779f106aSGleb Smirnoff so2 = NULL; 1570779f106aSGleb Smirnoff if (so2 == NULL) { 1571df8bae1dSRodney W. Grimes error = ECONNREFUSED; 1572779f106aSGleb Smirnoff goto bad3; 1573df8bae1dSRodney W. Grimes } 1574779f106aSGleb Smirnoff unp3 = sotounpcb(so2); 157575a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 157675a67bf3SMatt Macy unp_pcb_owned_lock2(unp2, unp3, freed); 157775a67bf3SMatt Macy MPASS(!freed); 15780d9ce3a1SRobert Watson if (unp2->unp_addr != NULL) { 15790d9ce3a1SRobert Watson bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 15800d9ce3a1SRobert Watson unp3->unp_addr = (struct sockaddr_un *) sa; 15810d9ce3a1SRobert Watson sa = NULL; 15820d9ce3a1SRobert Watson } 1583b523ec24SRobert Watson 15840c1bb4fbSDima Dorfman /* 15857a2b450fSEitan Adler * The connector's (client's) credentials are copied from its 15861c381b19SRobert Watson * process structure at the time of connect() (which is now). 15870c1bb4fbSDima Dorfman */ 1588a854ed98SJohn Baldwin cru2x(td->td_ucred, &unp3->unp_peercred); 15890c1bb4fbSDima Dorfman unp3->unp_flags |= UNP_HAVEPC; 1590b523ec24SRobert Watson 15910c1bb4fbSDima Dorfman /* 15921c381b19SRobert Watson * The receiver's (server's) credentials are copied from the 15931c381b19SRobert Watson * unp_peercred member of socket on which the former called 1594e7c33e29SRobert Watson * listen(); uipc_listen() cached that process's credentials 15951c381b19SRobert Watson * at that time so we can use them now. 15960c1bb4fbSDima Dorfman */ 15970c1bb4fbSDima Dorfman memcpy(&unp->unp_peercred, &unp2->unp_peercred, 15980c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 15990c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPC; 1600481f8fe8SMaxim Konovalov if (unp2->unp_flags & UNP_WANTCRED) 1601481f8fe8SMaxim Konovalov unp3->unp_flags |= UNP_WANTCRED; 1602e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1603779f106aSGleb Smirnoff unp2 = unp3; 160475a67bf3SMatt Macy unp_pcb_owned_lock2(unp2, unp, freed); 160575a67bf3SMatt Macy MPASS(!freed); 1606335654d7SRobert Watson #ifdef MAC 1607779f106aSGleb Smirnoff mac_socketpeer_set_from_socket(so, so2); 1608779f106aSGleb Smirnoff mac_socketpeer_set_from_socket(so2, so); 1609335654d7SRobert Watson #endif 1610df8bae1dSRodney W. Grimes } 1611779f106aSGleb Smirnoff 1612779f106aSGleb Smirnoff KASSERT(unp2 != NULL && so2 != NULL && unp2->unp_socket == so2 && 1613779f106aSGleb Smirnoff sotounpcb(so2) == unp2, 1614779f106aSGleb Smirnoff ("%s: unp2 %p so2 %p", __func__, unp2, so2)); 16156a2989fdSMatthew N. Dodd error = unp_connect2(so, so2, PRU_CONNECT); 1616779f106aSGleb Smirnoff bad3: 1617e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1618e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 16190d9ce3a1SRobert Watson bad2: 162075a67bf3SMatt Macy mtx_unlock(vplock); 1621df8bae1dSRodney W. Grimes bad: 162275a67bf3SMatt Macy if (vp != NULL) { 1623df8bae1dSRodney W. Grimes vput(vp); 162475a67bf3SMatt Macy } 16250d9ce3a1SRobert Watson free(sa, M_SONAME); 1626e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 16274f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_CONNECTING; 1628e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1629df8bae1dSRodney W. Grimes return (error); 1630df8bae1dSRodney W. Grimes } 1631df8bae1dSRodney W. Grimes 1632db48c0d2SRobert Watson static int 16336a2989fdSMatthew N. Dodd unp_connect2(struct socket *so, struct socket *so2, int req) 1634df8bae1dSRodney W. Grimes { 1635e7c33e29SRobert Watson struct unpcb *unp; 1636892af6b9SRobert Watson struct unpcb *unp2; 1637df8bae1dSRodney W. Grimes 1638e7c33e29SRobert Watson unp = sotounpcb(so); 1639e7c33e29SRobert Watson KASSERT(unp != NULL, ("unp_connect2: unp == NULL")); 1640e7c33e29SRobert Watson unp2 = sotounpcb(so2); 1641e7c33e29SRobert Watson KASSERT(unp2 != NULL, ("unp_connect2: unp2 == NULL")); 1642e7c33e29SRobert Watson 1643e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 1644e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp2); 16450d9ce3a1SRobert Watson 1646df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 1647df8bae1dSRodney W. Grimes return (EPROTOTYPE); 1648434ac8b6SMark Johnston unp2->unp_flags &= ~UNP_NASCENT; 1649df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 165075a67bf3SMatt Macy unp_pcb_hold(unp2); 165175a67bf3SMatt Macy unp_pcb_hold(unp); 1652df8bae1dSRodney W. Grimes switch (so->so_type) { 1653df8bae1dSRodney W. Grimes case SOCK_DGRAM: 165475a67bf3SMatt Macy UNP_REF_LIST_LOCK(); 165598271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 165675a67bf3SMatt Macy UNP_REF_LIST_UNLOCK(); 1657df8bae1dSRodney W. Grimes soisconnected(so); 1658df8bae1dSRodney W. Grimes break; 1659df8bae1dSRodney W. Grimes 1660df8bae1dSRodney W. Grimes case SOCK_STREAM: 166184d61770SRobert Watson case SOCK_SEQPACKET: 1662df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 16636a2989fdSMatthew N. Dodd if (req == PRU_CONNECT && 16646a2989fdSMatthew N. Dodd ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT)) 16656a2989fdSMatthew N. Dodd soisconnecting(so); 16666a2989fdSMatthew N. Dodd else 1667df8bae1dSRodney W. Grimes soisconnected(so); 1668df8bae1dSRodney W. Grimes soisconnected(so2); 1669df8bae1dSRodney W. Grimes break; 1670df8bae1dSRodney W. Grimes 1671df8bae1dSRodney W. Grimes default: 1672df8bae1dSRodney W. Grimes panic("unp_connect2"); 1673df8bae1dSRodney W. Grimes } 1674df8bae1dSRodney W. Grimes return (0); 1675df8bae1dSRodney W. Grimes } 1676df8bae1dSRodney W. Grimes 1677f708ef1bSPoul-Henning Kamp static void 1678e7c33e29SRobert Watson unp_disconnect(struct unpcb *unp, struct unpcb *unp2) 1679df8bae1dSRodney W. Grimes { 168075a67bf3SMatt Macy struct socket *so, *so2; 1681*c0874c34SMatt Macy #ifdef INVARIANTS 1682*c0874c34SMatt Macy int freed; 1683*c0874c34SMatt Macy #endif 1684df8bae1dSRodney W. Grimes 1685e7c33e29SRobert Watson KASSERT(unp2 != NULL, ("unp_disconnect: unp2 == NULL")); 16860d9ce3a1SRobert Watson 1687e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 1688e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp2); 1689e7c33e29SRobert Watson 169075a67bf3SMatt Macy if (unp->unp_conn == NULL && unp2->unp_conn == NULL) 169175a67bf3SMatt Macy return; 169275a67bf3SMatt Macy 169375a67bf3SMatt Macy MPASS(unp->unp_conn == unp2); 1694fc3fcacfSRobert Watson unp->unp_conn = NULL; 169575a67bf3SMatt Macy so = unp->unp_socket; 169675a67bf3SMatt Macy so2 = unp2->unp_socket; 1697df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 1698df8bae1dSRodney W. Grimes case SOCK_DGRAM: 169975a67bf3SMatt Macy UNP_REF_LIST_LOCK(); 170098271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 170175a67bf3SMatt Macy UNP_REF_LIST_UNLOCK(); 170275a67bf3SMatt Macy if (so) { 17031b2e3b4bSRobert Watson SOCK_LOCK(so); 17041b2e3b4bSRobert Watson so->so_state &= ~SS_ISCONNECTED; 17051b2e3b4bSRobert Watson SOCK_UNLOCK(so); 170675a67bf3SMatt Macy } 1707df8bae1dSRodney W. Grimes break; 1708df8bae1dSRodney W. Grimes 1709df8bae1dSRodney W. Grimes case SOCK_STREAM: 171084d61770SRobert Watson case SOCK_SEQPACKET: 171175a67bf3SMatt Macy if (so) 171275a67bf3SMatt Macy soisdisconnected(so); 171375a67bf3SMatt Macy MPASS(unp2->unp_conn == unp); 1714fc3fcacfSRobert Watson unp2->unp_conn = NULL; 171575a67bf3SMatt Macy if (so2) 171675a67bf3SMatt Macy soisdisconnected(so2); 1717df8bae1dSRodney W. Grimes break; 1718df8bae1dSRodney W. Grimes } 1719*c0874c34SMatt Macy #ifdef INVARIANTS 1720*c0874c34SMatt Macy freed = 1721*c0874c34SMatt Macy #endif 1722*c0874c34SMatt Macy unp_pcb_rele(unp); 172375a67bf3SMatt Macy MPASS(freed == 0); 1724*c0874c34SMatt Macy #ifdef INVARIANTS 1725*c0874c34SMatt Macy freed = 1726*c0874c34SMatt Macy #endif 1727*c0874c34SMatt Macy unp_pcb_rele(unp2); 172875a67bf3SMatt Macy MPASS(freed == 0); 1729df8bae1dSRodney W. Grimes } 1730df8bae1dSRodney W. Grimes 17310d9ce3a1SRobert Watson /* 1732d7924b70SRobert Watson * unp_pcblist() walks the global list of struct unpcb's to generate a 1733d7924b70SRobert Watson * pointer list, bumping the refcount on each unpcb. It then copies them out 1734d7924b70SRobert Watson * sequentially, validating the generation number on each to see if it has 1735d7924b70SRobert Watson * been detached. All of this is necessary because copyout() may sleep on 1736d7924b70SRobert Watson * disk I/O. 17370d9ce3a1SRobert Watson */ 173898271db4SGarrett Wollman static int 173982d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 174098271db4SGarrett Wollman { 174198271db4SGarrett Wollman struct unpcb *unp, **unp_list; 174298271db4SGarrett Wollman unp_gen_t gencnt; 17438f364875SJulian Elischer struct xunpgen *xug; 174498271db4SGarrett Wollman struct unp_head *head; 17458f364875SJulian Elischer struct xunpcb *xu; 1746d821d364SPedro F. Giffuni u_int i; 174744c514b1SPedro F. Giffuni int error, freeunp, n; 174898271db4SGarrett Wollman 174984d61770SRobert Watson switch ((intptr_t)arg1) { 175084d61770SRobert Watson case SOCK_STREAM: 175184d61770SRobert Watson head = &unp_shead; 175284d61770SRobert Watson break; 175384d61770SRobert Watson 175484d61770SRobert Watson case SOCK_DGRAM: 175584d61770SRobert Watson head = &unp_dhead; 175684d61770SRobert Watson break; 175784d61770SRobert Watson 175884d61770SRobert Watson case SOCK_SEQPACKET: 175984d61770SRobert Watson head = &unp_sphead; 176084d61770SRobert Watson break; 176184d61770SRobert Watson 176284d61770SRobert Watson default: 1763604f19c9SRobert Watson panic("unp_pcblist: arg1 %d", (int)(intptr_t)arg1); 176484d61770SRobert Watson } 176598271db4SGarrett Wollman 176698271db4SGarrett Wollman /* 176798271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 176898271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 176998271db4SGarrett Wollman */ 1770fc3fcacfSRobert Watson if (req->oldptr == NULL) { 177198271db4SGarrett Wollman n = unp_count; 17728f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug) 177398271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 1774e5aeaa0cSDag-Erling Smørgrav return (0); 177598271db4SGarrett Wollman } 177698271db4SGarrett Wollman 1777fc3fcacfSRobert Watson if (req->newptr != NULL) 1778e5aeaa0cSDag-Erling Smørgrav return (EPERM); 177998271db4SGarrett Wollman 178098271db4SGarrett Wollman /* 178198271db4SGarrett Wollman * OK, now we're committed to doing something. 178298271db4SGarrett Wollman */ 1783a163d034SWarner Losh xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 1784779f106aSGleb Smirnoff UNP_LINK_RLOCK(); 178598271db4SGarrett Wollman gencnt = unp_gencnt; 178698271db4SGarrett Wollman n = unp_count; 1787779f106aSGleb Smirnoff UNP_LINK_RUNLOCK(); 178898271db4SGarrett Wollman 17898f364875SJulian Elischer xug->xug_len = sizeof *xug; 17908f364875SJulian Elischer xug->xug_count = n; 17918f364875SJulian Elischer xug->xug_gen = gencnt; 17928f364875SJulian Elischer xug->xug_sogen = so_gencnt; 17938f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 17948f364875SJulian Elischer if (error) { 17958f364875SJulian Elischer free(xug, M_TEMP); 1796e5aeaa0cSDag-Erling Smørgrav return (error); 17978f364875SJulian Elischer } 179898271db4SGarrett Wollman 1799a163d034SWarner Losh unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 180098271db4SGarrett Wollman 1801779f106aSGleb Smirnoff UNP_LINK_RLOCK(); 18022e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 18032e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 1804e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 18058a7d8cc6SRobert Watson if (unp->unp_gencnt <= gencnt) { 1806a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred, 1807e7c33e29SRobert Watson unp->unp_socket->so_cred)) { 1808e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 18094787fd37SPaul Saab continue; 1810e7c33e29SRobert Watson } 181198271db4SGarrett Wollman unp_list[i++] = unp; 181275a67bf3SMatt Macy unp_pcb_hold(unp); 181398271db4SGarrett Wollman } 1814e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 18154787fd37SPaul Saab } 1816779f106aSGleb Smirnoff UNP_LINK_RUNLOCK(); 18171c381b19SRobert Watson n = i; /* In case we lost some during malloc. */ 181898271db4SGarrett Wollman 181998271db4SGarrett Wollman error = 0; 1820fe2eee82SColin Percival xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO); 182198271db4SGarrett Wollman for (i = 0; i < n; i++) { 182298271db4SGarrett Wollman unp = unp_list[i]; 1823e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 182475a67bf3SMatt Macy freeunp = unp_pcb_rele(unp); 182575a67bf3SMatt Macy 182675a67bf3SMatt Macy if (freeunp == 0 && unp->unp_gencnt <= gencnt) { 18278f364875SJulian Elischer xu->xu_len = sizeof *xu; 18288f364875SJulian Elischer xu->xu_unpp = unp; 182998271db4SGarrett Wollman /* 183098271db4SGarrett Wollman * XXX - need more locking here to protect against 183198271db4SGarrett Wollman * connect/disconnect races for SMP. 183298271db4SGarrett Wollman */ 1833fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 18348f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr, 183598271db4SGarrett Wollman unp->unp_addr->sun_len); 18360e229f34SGleb Smirnoff else 18370e229f34SGleb Smirnoff bzero(&xu->xu_addr, sizeof(xu->xu_addr)); 1838fc3fcacfSRobert Watson if (unp->unp_conn != NULL && 1839fc3fcacfSRobert Watson unp->unp_conn->unp_addr != NULL) 184098271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 18418f364875SJulian Elischer &xu->xu_caddr, 184298271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 18430e229f34SGleb Smirnoff else 18440e229f34SGleb Smirnoff bzero(&xu->xu_caddr, sizeof(xu->xu_caddr)); 18450e229f34SGleb Smirnoff xu->unp_vnode = unp->unp_vnode; 18460e229f34SGleb Smirnoff xu->unp_conn = unp->unp_conn; 18470e229f34SGleb Smirnoff xu->xu_firstref = LIST_FIRST(&unp->unp_refs); 18480e229f34SGleb Smirnoff xu->xu_nextref = LIST_NEXT(unp, unp_reflink); 18490e229f34SGleb Smirnoff xu->unp_gencnt = unp->unp_gencnt; 18508f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket); 1851e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 18528f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu); 185375a67bf3SMatt Macy } else if (freeunp == 0) 1854e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1855e7c33e29SRobert Watson } 18568f364875SJulian Elischer free(xu, M_TEMP); 185798271db4SGarrett Wollman if (!error) { 185898271db4SGarrett Wollman /* 18591c381b19SRobert Watson * Give the user an updated idea of our state. If the 18601c381b19SRobert Watson * generation differs from what we told her before, she knows 18611c381b19SRobert Watson * that something happened while we were processing this 18621c381b19SRobert Watson * request, and it might be necessary to retry. 186398271db4SGarrett Wollman */ 18648f364875SJulian Elischer xug->xug_gen = unp_gencnt; 18658f364875SJulian Elischer xug->xug_sogen = so_gencnt; 18668f364875SJulian Elischer xug->xug_count = unp_count; 18678f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 186898271db4SGarrett Wollman } 186998271db4SGarrett Wollman free(unp_list, M_TEMP); 18708f364875SJulian Elischer free(xug, M_TEMP); 1871e5aeaa0cSDag-Erling Smørgrav return (error); 187298271db4SGarrett Wollman } 187398271db4SGarrett Wollman 18742fee06f0SMatthew D Fleming SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLTYPE_OPAQUE | CTLFLAG_RD, 18752fee06f0SMatthew D Fleming (void *)(intptr_t)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 187698271db4SGarrett Wollman "List of active local datagram sockets"); 18772fee06f0SMatthew D Fleming SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLTYPE_OPAQUE | CTLFLAG_RD, 18782fee06f0SMatthew D Fleming (void *)(intptr_t)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 187998271db4SGarrett Wollman "List of active local stream sockets"); 18802fee06f0SMatthew D Fleming SYSCTL_PROC(_net_local_seqpacket, OID_AUTO, pcblist, 18812fee06f0SMatthew D Fleming CTLTYPE_OPAQUE | CTLFLAG_RD, 18822fee06f0SMatthew D Fleming (void *)(intptr_t)SOCK_SEQPACKET, 0, unp_pcblist, "S,xunpcb", 188384d61770SRobert Watson "List of active local seqpacket sockets"); 188498271db4SGarrett Wollman 1885f708ef1bSPoul-Henning Kamp static void 1886892af6b9SRobert Watson unp_shutdown(struct unpcb *unp) 1887df8bae1dSRodney W. Grimes { 1888e7c33e29SRobert Watson struct unpcb *unp2; 1889df8bae1dSRodney W. Grimes struct socket *so; 1890df8bae1dSRodney W. Grimes 1891e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 18920d9ce3a1SRobert Watson 1893e7c33e29SRobert Watson unp2 = unp->unp_conn; 189484d61770SRobert Watson if ((unp->unp_socket->so_type == SOCK_STREAM || 189584d61770SRobert Watson (unp->unp_socket->so_type == SOCK_SEQPACKET)) && unp2 != NULL) { 1896e7c33e29SRobert Watson so = unp2->unp_socket; 1897e7c33e29SRobert Watson if (so != NULL) 1898df8bae1dSRodney W. Grimes socantrcvmore(so); 1899df8bae1dSRodney W. Grimes } 1900e7c33e29SRobert Watson } 1901df8bae1dSRodney W. Grimes 1902f708ef1bSPoul-Henning Kamp static void 1903afc055d9SEd Schouten unp_drop(struct unpcb *unp) 1904df8bae1dSRodney W. Grimes { 1905df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 1906e7c33e29SRobert Watson struct unpcb *unp2; 190775a67bf3SMatt Macy int freed; 19080d9ce3a1SRobert Watson 1909afc055d9SEd Schouten /* 1910afc055d9SEd Schouten * Regardless of whether the socket's peer dropped the connection 1911afc055d9SEd Schouten * with this socket by aborting or disconnecting, POSIX requires 1912afc055d9SEd Schouten * that ECONNRESET is returned. 1913afc055d9SEd Schouten */ 191475a67bf3SMatt Macy /* acquire a reference so that unp isn't freed from underneath us */ 191575a67bf3SMatt Macy 191675a67bf3SMatt Macy UNP_PCB_LOCK(unp); 191775a67bf3SMatt Macy if (so) 1918afc055d9SEd Schouten so->so_error = ECONNRESET; 1919e7c33e29SRobert Watson unp2 = unp->unp_conn; 192075a67bf3SMatt Macy if (unp2 != NULL) { 192175a67bf3SMatt Macy unp_pcb_hold(unp2); 192275a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freed); 1923e7c33e29SRobert Watson unp_disconnect(unp, unp2); 192475a67bf3SMatt Macy if (unp_pcb_rele(unp2) == 0) 1925e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1926df8bae1dSRodney W. Grimes } 192775a67bf3SMatt Macy if (unp_pcb_rele(unp) == 0) 192875a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 192975a67bf3SMatt Macy } 1930df8bae1dSRodney W. Grimes 19312bc21ed9SDavid Malone static void 19328cb539f1SPawel Jakub Dawidek unp_freerights(struct filedescent **fdep, int fdcount) 1933df8bae1dSRodney W. Grimes { 19342bc21ed9SDavid Malone struct file *fp; 19352609222aSPawel Jakub Dawidek int i; 1936df8bae1dSRodney W. Grimes 193782e825c4SGleb Smirnoff KASSERT(fdcount > 0, ("%s: fdcount %d", __func__, fdcount)); 193882e825c4SGleb Smirnoff 19398cb539f1SPawel Jakub Dawidek for (i = 0; i < fdcount; i++) { 19408cb539f1SPawel Jakub Dawidek fp = fdep[i]->fde_file; 19418cb539f1SPawel Jakub Dawidek filecaps_free(&fdep[i]->fde_caps); 19428692c025SYoshinobu Inoue unp_discard(fp); 1943df8bae1dSRodney W. Grimes } 19448cb539f1SPawel Jakub Dawidek free(fdep[0], M_FILECAPS); 19452bc21ed9SDavid Malone } 19462bc21ed9SDavid Malone 19470b36cd25SRobert Watson static int 1948c2e3c52eSJilles Tjoelker unp_externalize(struct mbuf *control, struct mbuf **controlp, int flags) 19492bc21ed9SDavid Malone { 19502bc21ed9SDavid Malone struct thread *td = curthread; /* XXX */ 19512bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 19522bc21ed9SDavid Malone int i; 19532bc21ed9SDavid Malone int *fdp; 19542609222aSPawel Jakub Dawidek struct filedesc *fdesc = td->td_proc->p_fd; 1955ea31808cSMateusz Guzik struct filedescent **fdep; 19562bc21ed9SDavid Malone void *data; 19572bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 19582bc21ed9SDavid Malone int error, newfds; 19592bc21ed9SDavid Malone u_int newlen; 19602bc21ed9SDavid Malone 19613dab55bcSRobert Watson UNP_LINK_UNLOCK_ASSERT(); 19624c5bc1caSRobert Watson 19632bc21ed9SDavid Malone error = 0; 19642bc21ed9SDavid Malone if (controlp != NULL) /* controlp == NULL => free control messages */ 19652bc21ed9SDavid Malone *controlp = NULL; 19662bc21ed9SDavid Malone while (cm != NULL) { 19672bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 19682bc21ed9SDavid Malone error = EINVAL; 19692bc21ed9SDavid Malone break; 19702bc21ed9SDavid Malone } 19712bc21ed9SDavid Malone data = CMSG_DATA(cm); 19722bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 19732bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET 19742bc21ed9SDavid Malone && cm->cmsg_type == SCM_RIGHTS) { 19752609222aSPawel Jakub Dawidek newfds = datalen / sizeof(*fdep); 197682e825c4SGleb Smirnoff if (newfds == 0) 197782e825c4SGleb Smirnoff goto next; 19782609222aSPawel Jakub Dawidek fdep = data; 19792bc21ed9SDavid Malone 1980e2f9a08bSOlivier Houchard /* If we're not outputting the descriptors free them. */ 19812bc21ed9SDavid Malone if (error || controlp == NULL) { 19822609222aSPawel Jakub Dawidek unp_freerights(fdep, newfds); 19832bc21ed9SDavid Malone goto next; 19842bc21ed9SDavid Malone } 19852609222aSPawel Jakub Dawidek FILEDESC_XLOCK(fdesc); 198660a5ef26SRobert Watson 1987ed5b7817SJulian Elischer /* 19881c381b19SRobert Watson * Now change each pointer to an fd in the global 19891c381b19SRobert Watson * table to an integer that is the index to the local 19901c381b19SRobert Watson * fd table entry that we set up to point to the 19911c381b19SRobert Watson * global one we are transferring. 1992ed5b7817SJulian Elischer */ 19932bc21ed9SDavid Malone newlen = newfds * sizeof(int); 19942bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 19952bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 19962bc21ed9SDavid Malone if (*controlp == NULL) { 19972609222aSPawel Jakub Dawidek FILEDESC_XUNLOCK(fdesc); 19982bc21ed9SDavid Malone error = E2BIG; 19992609222aSPawel Jakub Dawidek unp_freerights(fdep, newfds); 20002bc21ed9SDavid Malone goto next; 20012bc21ed9SDavid Malone } 20022bc21ed9SDavid Malone 20032bc21ed9SDavid Malone fdp = (int *) 20042bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2005db8f33fdSMateusz Guzik if (fdallocn(td, 0, fdp, newfds) != 0) { 20063331a33aSMateusz Guzik FILEDESC_XUNLOCK(fdesc); 2007db8f33fdSMateusz Guzik error = EMSGSIZE; 2008db8f33fdSMateusz Guzik unp_freerights(fdep, newfds); 2009db8f33fdSMateusz Guzik m_freem(*controlp); 2010db8f33fdSMateusz Guzik *controlp = NULL; 2011db8f33fdSMateusz Guzik goto next; 2012db8f33fdSMateusz Guzik } 20138cb539f1SPawel Jakub Dawidek for (i = 0; i < newfds; i++, fdp++) { 2014ea31808cSMateusz Guzik _finstall(fdesc, fdep[i]->fde_file, *fdp, 2015ea31808cSMateusz Guzik (flags & MSG_CMSG_CLOEXEC) != 0 ? UF_EXCLOSE : 0, 2016ea31808cSMateusz Guzik &fdep[i]->fde_caps); 2017ea31808cSMateusz Guzik unp_externalize_fp(fdep[i]->fde_file); 2018df8bae1dSRodney W. Grimes } 20192609222aSPawel Jakub Dawidek FILEDESC_XUNLOCK(fdesc); 20208cb539f1SPawel Jakub Dawidek free(fdep[0], M_FILECAPS); 20211c381b19SRobert Watson } else { 20221c381b19SRobert Watson /* We can just copy anything else across. */ 20232bc21ed9SDavid Malone if (error || controlp == NULL) 20242bc21ed9SDavid Malone goto next; 20252bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, datalen, 20262bc21ed9SDavid Malone cm->cmsg_type, cm->cmsg_level); 20272bc21ed9SDavid Malone if (*controlp == NULL) { 20282bc21ed9SDavid Malone error = ENOBUFS; 20292bc21ed9SDavid Malone goto next; 20302bc21ed9SDavid Malone } 20312bc21ed9SDavid Malone bcopy(data, 20322bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 20332bc21ed9SDavid Malone datalen); 20342bc21ed9SDavid Malone } 20352bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 20362bc21ed9SDavid Malone 20372bc21ed9SDavid Malone next: 20382bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 20392bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 20402bc21ed9SDavid Malone cm = (struct cmsghdr *) 20412bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 20428692c025SYoshinobu Inoue } else { 20432bc21ed9SDavid Malone clen = 0; 20442bc21ed9SDavid Malone cm = NULL; 20458692c025SYoshinobu Inoue } 20468692c025SYoshinobu Inoue } 20478692c025SYoshinobu Inoue 20482bc21ed9SDavid Malone m_freem(control); 20492bc21ed9SDavid Malone return (error); 2050df8bae1dSRodney W. Grimes } 2051df8bae1dSRodney W. Grimes 20524f590175SPaul Saab static void 20534f590175SPaul Saab unp_zone_change(void *tag) 20544f590175SPaul Saab { 20554f590175SPaul Saab 20564f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets); 20574f590175SPaul Saab } 20584f590175SPaul Saab 20590b36cd25SRobert Watson static void 206098271db4SGarrett Wollman unp_init(void) 206198271db4SGarrett Wollman { 20621c381b19SRobert Watson 206321ca7b57SMarko Zec #ifdef VIMAGE 206421ca7b57SMarko Zec if (!IS_DEFAULT_VNET(curvnet)) 206521ca7b57SMarko Zec return; 206621ca7b57SMarko Zec #endif 20679e9d298aSJeff Roberson unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 206875a67bf3SMatt Macy NULL, NULL, UMA_ALIGN_CACHE, 0); 2069fc3fcacfSRobert Watson if (unp_zone == NULL) 207098271db4SGarrett Wollman panic("unp_init"); 20714f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets); 20726e0b6746SPawel Jakub Dawidek uma_zone_set_warning(unp_zone, "kern.ipc.maxsockets limit reached"); 20734f590175SPaul Saab EVENTHANDLER_REGISTER(maxsockets_change, unp_zone_change, 20744f590175SPaul Saab NULL, EVENTHANDLER_PRI_ANY); 207598271db4SGarrett Wollman LIST_INIT(&unp_dhead); 207698271db4SGarrett Wollman LIST_INIT(&unp_shead); 207784d61770SRobert Watson LIST_INIT(&unp_sphead); 20780cb64678SKonstantin Belousov SLIST_INIT(&unp_defers); 2079daee0f0bSKonstantin Belousov TIMEOUT_TASK_INIT(taskqueue_thread, &unp_gc_task, 0, unp_gc, NULL); 20800cb64678SKonstantin Belousov TASK_INIT(&unp_defer_task, 0, unp_process_defers, NULL); 20813dab55bcSRobert Watson UNP_LINK_LOCK_INIT(); 20820cb64678SKonstantin Belousov UNP_DEFERRED_LOCK_INIT(); 208398271db4SGarrett Wollman } 208498271db4SGarrett Wollman 2085f708ef1bSPoul-Henning Kamp static int 2086892af6b9SRobert Watson unp_internalize(struct mbuf **controlp, struct thread *td) 2087df8bae1dSRodney W. Grimes { 20882bc21ed9SDavid Malone struct mbuf *control = *controlp; 2089b40ce416SJulian Elischer struct proc *p = td->td_proc; 20902609222aSPawel Jakub Dawidek struct filedesc *fdesc = p->p_fd; 2091ab15d803SSergey Kandaurov struct bintime *bt; 20922bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 20932bc21ed9SDavid Malone struct cmsgcred *cmcred; 20948cb539f1SPawel Jakub Dawidek struct filedescent *fde, **fdep, *fdev; 20952bc21ed9SDavid Malone struct file *fp; 20962bc21ed9SDavid Malone struct timeval *tv; 2097339efd75SMaxim Sobolev struct timespec *ts; 20986a1cf96bSMateusz Guzik int i, *fdp; 20992bc21ed9SDavid Malone void *data; 21002bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 21012bc21ed9SDavid Malone int error, oldfds; 21028692c025SYoshinobu Inoue u_int newlen; 2103df8bae1dSRodney W. Grimes 21043dab55bcSRobert Watson UNP_LINK_UNLOCK_ASSERT(); 21054c5bc1caSRobert Watson 21062bc21ed9SDavid Malone error = 0; 21072bc21ed9SDavid Malone *controlp = NULL; 21082bc21ed9SDavid Malone while (cm != NULL) { 21092bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 2110de966666SMateusz Guzik || cm->cmsg_len > clen || cm->cmsg_len < sizeof(*cm)) { 21112bc21ed9SDavid Malone error = EINVAL; 21122bc21ed9SDavid Malone goto out; 21132bc21ed9SDavid Malone } 21142bc21ed9SDavid Malone data = CMSG_DATA(cm); 21152bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 21162bc21ed9SDavid Malone 21172bc21ed9SDavid Malone switch (cm->cmsg_type) { 21180b788fa1SBill Paul /* 21190b788fa1SBill Paul * Fill in credential information. 21200b788fa1SBill Paul */ 21212bc21ed9SDavid Malone case SCM_CREDS: 21222bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 21232bc21ed9SDavid Malone SCM_CREDS, SOL_SOCKET); 21242bc21ed9SDavid Malone if (*controlp == NULL) { 21252bc21ed9SDavid Malone error = ENOBUFS; 21262bc21ed9SDavid Malone goto out; 21272bc21ed9SDavid Malone } 21282bc21ed9SDavid Malone cmcred = (struct cmsgcred *) 21292bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 21300b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 2131a854ed98SJohn Baldwin cmcred->cmcred_uid = td->td_ucred->cr_ruid; 2132a854ed98SJohn Baldwin cmcred->cmcred_gid = td->td_ucred->cr_rgid; 2133a854ed98SJohn Baldwin cmcred->cmcred_euid = td->td_ucred->cr_uid; 2134a854ed98SJohn Baldwin cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 21350b788fa1SBill Paul CMGROUP_MAX); 21360b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 21372bc21ed9SDavid Malone cmcred->cmcred_groups[i] = 2138a854ed98SJohn Baldwin td->td_ucred->cr_groups[i]; 21392bc21ed9SDavid Malone break; 21400b788fa1SBill Paul 21412bc21ed9SDavid Malone case SCM_RIGHTS: 21422bc21ed9SDavid Malone oldfds = datalen / sizeof (int); 214382e825c4SGleb Smirnoff if (oldfds == 0) 214482e825c4SGleb Smirnoff break; 2145ed5b7817SJulian Elischer /* 21461c381b19SRobert Watson * Check that all the FDs passed in refer to legal 21471c381b19SRobert Watson * files. If not, reject the entire operation. 2148ed5b7817SJulian Elischer */ 21492bc21ed9SDavid Malone fdp = data; 21502609222aSPawel Jakub Dawidek FILEDESC_SLOCK(fdesc); 21516a1cf96bSMateusz Guzik for (i = 0; i < oldfds; i++, fdp++) { 21526a1cf96bSMateusz Guzik fp = fget_locked(fdesc, *fdp); 21536a1cf96bSMateusz Guzik if (fp == NULL) { 21542609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdesc); 21552bc21ed9SDavid Malone error = EBADF; 21562bc21ed9SDavid Malone goto out; 21572bc21ed9SDavid Malone } 2158e7d6662fSAlfred Perlstein if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 21592609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdesc); 2160e7d6662fSAlfred Perlstein error = EOPNOTSUPP; 2161e7d6662fSAlfred Perlstein goto out; 2162e7d6662fSAlfred Perlstein } 2163e7d6662fSAlfred Perlstein 2164df8bae1dSRodney W. Grimes } 21655e3f7694SRobert Watson 2166ed5b7817SJulian Elischer /* 21670b36cd25SRobert Watson * Now replace the integer FDs with pointers to the 21682609222aSPawel Jakub Dawidek * file structure and capability rights. 2169ed5b7817SJulian Elischer */ 21708cb539f1SPawel Jakub Dawidek newlen = oldfds * sizeof(fdep[0]); 21712bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 21722bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 21732bc21ed9SDavid Malone if (*controlp == NULL) { 21742609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdesc); 21752bc21ed9SDavid Malone error = E2BIG; 21762bc21ed9SDavid Malone goto out; 21778692c025SYoshinobu Inoue } 21782bc21ed9SDavid Malone fdp = data; 21798cb539f1SPawel Jakub Dawidek fdep = (struct filedescent **) 21802bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 21818cb539f1SPawel Jakub Dawidek fdev = malloc(sizeof(*fdev) * oldfds, M_FILECAPS, 21828cb539f1SPawel Jakub Dawidek M_WAITOK); 21838cb539f1SPawel Jakub Dawidek for (i = 0; i < oldfds; i++, fdev++, fdp++) { 21842609222aSPawel Jakub Dawidek fde = &fdesc->fd_ofiles[*fdp]; 21858cb539f1SPawel Jakub Dawidek fdep[i] = fdev; 21868cb539f1SPawel Jakub Dawidek fdep[i]->fde_file = fde->fde_file; 21878cb539f1SPawel Jakub Dawidek filecaps_copy(&fde->fde_caps, 2188d7832811SMateusz Guzik &fdep[i]->fde_caps, true); 21898cb539f1SPawel Jakub Dawidek unp_internalize_fp(fdep[i]->fde_file); 2190df8bae1dSRodney W. Grimes } 21912609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdesc); 21922bc21ed9SDavid Malone break; 21932bc21ed9SDavid Malone 21942bc21ed9SDavid Malone case SCM_TIMESTAMP: 21952bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*tv), 21962bc21ed9SDavid Malone SCM_TIMESTAMP, SOL_SOCKET); 21972bc21ed9SDavid Malone if (*controlp == NULL) { 21982bc21ed9SDavid Malone error = ENOBUFS; 21992bc21ed9SDavid Malone goto out; 22008692c025SYoshinobu Inoue } 22012bc21ed9SDavid Malone tv = (struct timeval *) 22022bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 22032bc21ed9SDavid Malone microtime(tv); 22042bc21ed9SDavid Malone break; 22052bc21ed9SDavid Malone 2206ab15d803SSergey Kandaurov case SCM_BINTIME: 2207ab15d803SSergey Kandaurov *controlp = sbcreatecontrol(NULL, sizeof(*bt), 2208ab15d803SSergey Kandaurov SCM_BINTIME, SOL_SOCKET); 2209ab15d803SSergey Kandaurov if (*controlp == NULL) { 2210ab15d803SSergey Kandaurov error = ENOBUFS; 2211ab15d803SSergey Kandaurov goto out; 2212ab15d803SSergey Kandaurov } 2213ab15d803SSergey Kandaurov bt = (struct bintime *) 2214ab15d803SSergey Kandaurov CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2215ab15d803SSergey Kandaurov bintime(bt); 2216ab15d803SSergey Kandaurov break; 2217ab15d803SSergey Kandaurov 2218339efd75SMaxim Sobolev case SCM_REALTIME: 2219339efd75SMaxim Sobolev *controlp = sbcreatecontrol(NULL, sizeof(*ts), 2220339efd75SMaxim Sobolev SCM_REALTIME, SOL_SOCKET); 2221339efd75SMaxim Sobolev if (*controlp == NULL) { 2222339efd75SMaxim Sobolev error = ENOBUFS; 2223339efd75SMaxim Sobolev goto out; 2224339efd75SMaxim Sobolev } 2225339efd75SMaxim Sobolev ts = (struct timespec *) 2226339efd75SMaxim Sobolev CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2227339efd75SMaxim Sobolev nanotime(ts); 2228339efd75SMaxim Sobolev break; 2229339efd75SMaxim Sobolev 2230339efd75SMaxim Sobolev case SCM_MONOTONIC: 2231339efd75SMaxim Sobolev *controlp = sbcreatecontrol(NULL, sizeof(*ts), 2232339efd75SMaxim Sobolev SCM_MONOTONIC, SOL_SOCKET); 2233339efd75SMaxim Sobolev if (*controlp == NULL) { 2234339efd75SMaxim Sobolev error = ENOBUFS; 2235339efd75SMaxim Sobolev goto out; 2236339efd75SMaxim Sobolev } 2237339efd75SMaxim Sobolev ts = (struct timespec *) 2238339efd75SMaxim Sobolev CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2239339efd75SMaxim Sobolev nanouptime(ts); 2240339efd75SMaxim Sobolev break; 2241339efd75SMaxim Sobolev 22422bc21ed9SDavid Malone default: 22432bc21ed9SDavid Malone error = EINVAL; 22442bc21ed9SDavid Malone goto out; 22452bc21ed9SDavid Malone } 22462bc21ed9SDavid Malone 22472bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 22482bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 22492bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 22502bc21ed9SDavid Malone cm = (struct cmsghdr *) 22512bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 22522bc21ed9SDavid Malone } else { 22532bc21ed9SDavid Malone clen = 0; 22542bc21ed9SDavid Malone cm = NULL; 22552bc21ed9SDavid Malone } 22562bc21ed9SDavid Malone } 22572bc21ed9SDavid Malone 22582bc21ed9SDavid Malone out: 22592bc21ed9SDavid Malone m_freem(control); 22602bc21ed9SDavid Malone return (error); 2261df8bae1dSRodney W. Grimes } 2262df8bae1dSRodney W. Grimes 22635b950deaSRobert Watson static struct mbuf * 22646a2989fdSMatthew N. Dodd unp_addsockcred(struct thread *td, struct mbuf *control) 22656a2989fdSMatthew N. Dodd { 226670df31f4SMaxim Konovalov struct mbuf *m, *n, *n_prev; 22676a2989fdSMatthew N. Dodd struct sockcred *sc; 226870df31f4SMaxim Konovalov const struct cmsghdr *cm; 22696a2989fdSMatthew N. Dodd int ngroups; 22706a2989fdSMatthew N. Dodd int i; 22716a2989fdSMatthew N. Dodd 22726a2989fdSMatthew N. Dodd ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX); 22736a2989fdSMatthew N. Dodd m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET); 22746a2989fdSMatthew N. Dodd if (m == NULL) 22756a2989fdSMatthew N. Dodd return (control); 22766a2989fdSMatthew N. Dodd 22776a2989fdSMatthew N. Dodd sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *)); 22786a2989fdSMatthew N. Dodd sc->sc_uid = td->td_ucred->cr_ruid; 22796a2989fdSMatthew N. Dodd sc->sc_euid = td->td_ucred->cr_uid; 22806a2989fdSMatthew N. Dodd sc->sc_gid = td->td_ucred->cr_rgid; 22816a2989fdSMatthew N. Dodd sc->sc_egid = td->td_ucred->cr_gid; 22826a2989fdSMatthew N. Dodd sc->sc_ngroups = ngroups; 22836a2989fdSMatthew N. Dodd for (i = 0; i < sc->sc_ngroups; i++) 22846a2989fdSMatthew N. Dodd sc->sc_groups[i] = td->td_ucred->cr_groups[i]; 22856a2989fdSMatthew N. Dodd 22866a2989fdSMatthew N. Dodd /* 22871c381b19SRobert Watson * Unlink SCM_CREDS control messages (struct cmsgcred), since just 22881c381b19SRobert Watson * created SCM_CREDS control message (struct sockcred) has another 22891c381b19SRobert Watson * format. 22906a2989fdSMatthew N. Dodd */ 229170df31f4SMaxim Konovalov if (control != NULL) 229270df31f4SMaxim Konovalov for (n = control, n_prev = NULL; n != NULL;) { 229370df31f4SMaxim Konovalov cm = mtod(n, struct cmsghdr *); 229470df31f4SMaxim Konovalov if (cm->cmsg_level == SOL_SOCKET && 229570df31f4SMaxim Konovalov cm->cmsg_type == SCM_CREDS) { 229670df31f4SMaxim Konovalov if (n_prev == NULL) 229770df31f4SMaxim Konovalov control = n->m_next; 229870df31f4SMaxim Konovalov else 229970df31f4SMaxim Konovalov n_prev->m_next = n->m_next; 230070df31f4SMaxim Konovalov n = m_free(n); 230170df31f4SMaxim Konovalov } else { 230270df31f4SMaxim Konovalov n_prev = n; 230370df31f4SMaxim Konovalov n = n->m_next; 230470df31f4SMaxim Konovalov } 230570df31f4SMaxim Konovalov } 23066a2989fdSMatthew N. Dodd 230770df31f4SMaxim Konovalov /* Prepend it to the head. */ 230870df31f4SMaxim Konovalov m->m_next = control; 230970df31f4SMaxim Konovalov return (m); 23106a2989fdSMatthew N. Dodd } 23116a2989fdSMatthew N. Dodd 2312397c19d1SJeff Roberson static struct unpcb * 2313397c19d1SJeff Roberson fptounp(struct file *fp) 2314397c19d1SJeff Roberson { 2315397c19d1SJeff Roberson struct socket *so; 2316397c19d1SJeff Roberson 2317397c19d1SJeff Roberson if (fp->f_type != DTYPE_SOCKET) 2318397c19d1SJeff Roberson return (NULL); 2319397c19d1SJeff Roberson if ((so = fp->f_data) == NULL) 2320397c19d1SJeff Roberson return (NULL); 2321397c19d1SJeff Roberson if (so->so_proto->pr_domain != &localdomain) 2322397c19d1SJeff Roberson return (NULL); 2323397c19d1SJeff Roberson return sotounpcb(so); 2324397c19d1SJeff Roberson } 2325397c19d1SJeff Roberson 2326397c19d1SJeff Roberson static void 2327397c19d1SJeff Roberson unp_discard(struct file *fp) 2328397c19d1SJeff Roberson { 23290cb64678SKonstantin Belousov struct unp_defer *dr; 2330397c19d1SJeff Roberson 23310cb64678SKonstantin Belousov if (unp_externalize_fp(fp)) { 23320cb64678SKonstantin Belousov dr = malloc(sizeof(*dr), M_TEMP, M_WAITOK); 23330cb64678SKonstantin Belousov dr->ud_fp = fp; 23340cb64678SKonstantin Belousov UNP_DEFERRED_LOCK(); 23350cb64678SKonstantin Belousov SLIST_INSERT_HEAD(&unp_defers, dr, ud_link); 23360cb64678SKonstantin Belousov UNP_DEFERRED_UNLOCK(); 23370cb64678SKonstantin Belousov atomic_add_int(&unp_defers_count, 1); 23380cb64678SKonstantin Belousov taskqueue_enqueue(taskqueue_thread, &unp_defer_task); 23390cb64678SKonstantin Belousov } else 2340397c19d1SJeff Roberson (void) closef(fp, (struct thread *)NULL); 2341397c19d1SJeff Roberson } 2342397c19d1SJeff Roberson 2343397c19d1SJeff Roberson static void 23440cb64678SKonstantin Belousov unp_process_defers(void *arg __unused, int pending) 23450cb64678SKonstantin Belousov { 23460cb64678SKonstantin Belousov struct unp_defer *dr; 23470cb64678SKonstantin Belousov SLIST_HEAD(, unp_defer) drl; 23480cb64678SKonstantin Belousov int count; 23490cb64678SKonstantin Belousov 23500cb64678SKonstantin Belousov SLIST_INIT(&drl); 23510cb64678SKonstantin Belousov for (;;) { 23520cb64678SKonstantin Belousov UNP_DEFERRED_LOCK(); 23530cb64678SKonstantin Belousov if (SLIST_FIRST(&unp_defers) == NULL) { 23540cb64678SKonstantin Belousov UNP_DEFERRED_UNLOCK(); 23550cb64678SKonstantin Belousov break; 23560cb64678SKonstantin Belousov } 23570cb64678SKonstantin Belousov SLIST_SWAP(&unp_defers, &drl, unp_defer); 23580cb64678SKonstantin Belousov UNP_DEFERRED_UNLOCK(); 23590cb64678SKonstantin Belousov count = 0; 23600cb64678SKonstantin Belousov while ((dr = SLIST_FIRST(&drl)) != NULL) { 23610cb64678SKonstantin Belousov SLIST_REMOVE_HEAD(&drl, ud_link); 23620cb64678SKonstantin Belousov closef(dr->ud_fp, NULL); 23630cb64678SKonstantin Belousov free(dr, M_TEMP); 23640cb64678SKonstantin Belousov count++; 23650cb64678SKonstantin Belousov } 23660cb64678SKonstantin Belousov atomic_add_int(&unp_defers_count, -count); 23670cb64678SKonstantin Belousov } 23680cb64678SKonstantin Belousov } 23690cb64678SKonstantin Belousov 23700cb64678SKonstantin Belousov static void 2371397c19d1SJeff Roberson unp_internalize_fp(struct file *fp) 2372397c19d1SJeff Roberson { 2373397c19d1SJeff Roberson struct unpcb *unp; 2374397c19d1SJeff Roberson 23753dab55bcSRobert Watson UNP_LINK_WLOCK(); 2376397c19d1SJeff Roberson if ((unp = fptounp(fp)) != NULL) { 2377397c19d1SJeff Roberson unp->unp_file = fp; 2378397c19d1SJeff Roberson unp->unp_msgcount++; 2379397c19d1SJeff Roberson } 238041e0f66dSJeff Roberson fhold(fp); 2381397c19d1SJeff Roberson unp_rights++; 23823dab55bcSRobert Watson UNP_LINK_WUNLOCK(); 2383397c19d1SJeff Roberson } 2384397c19d1SJeff Roberson 23850cb64678SKonstantin Belousov static int 2386397c19d1SJeff Roberson unp_externalize_fp(struct file *fp) 2387397c19d1SJeff Roberson { 2388397c19d1SJeff Roberson struct unpcb *unp; 23890cb64678SKonstantin Belousov int ret; 2390397c19d1SJeff Roberson 23913dab55bcSRobert Watson UNP_LINK_WLOCK(); 23920cb64678SKonstantin Belousov if ((unp = fptounp(fp)) != NULL) { 2393397c19d1SJeff Roberson unp->unp_msgcount--; 23940cb64678SKonstantin Belousov ret = 1; 23950cb64678SKonstantin Belousov } else 23960cb64678SKonstantin Belousov ret = 0; 2397397c19d1SJeff Roberson unp_rights--; 23983dab55bcSRobert Watson UNP_LINK_WUNLOCK(); 23990cb64678SKonstantin Belousov return (ret); 2400397c19d1SJeff Roberson } 2401397c19d1SJeff Roberson 2402161a0c7cSRobert Watson /* 2403a0ec558aSRobert Watson * unp_defer indicates whether additional work has been defered for a future 2404a0ec558aSRobert Watson * pass through unp_gc(). It is thread local and does not require explicit 2405a0ec558aSRobert Watson * synchronization. 2406161a0c7cSRobert Watson */ 2407397c19d1SJeff Roberson static int unp_marked; 2408397c19d1SJeff Roberson static int unp_unreachable; 2409a0ec558aSRobert Watson 2410397c19d1SJeff Roberson static void 2411be26ba7cSPawel Jakub Dawidek unp_accessable(struct filedescent **fdep, int fdcount) 2412397c19d1SJeff Roberson { 2413397c19d1SJeff Roberson struct unpcb *unp; 2414be26ba7cSPawel Jakub Dawidek struct file *fp; 2415be26ba7cSPawel Jakub Dawidek int i; 2416397c19d1SJeff Roberson 2417be26ba7cSPawel Jakub Dawidek for (i = 0; i < fdcount; i++) { 2418be26ba7cSPawel Jakub Dawidek fp = fdep[i]->fde_file; 24196f552cb0SJeff Roberson if ((unp = fptounp(fp)) == NULL) 2420be26ba7cSPawel Jakub Dawidek continue; 2421397c19d1SJeff Roberson if (unp->unp_gcflag & UNPGC_REF) 2422be26ba7cSPawel Jakub Dawidek continue; 2423397c19d1SJeff Roberson unp->unp_gcflag &= ~UNPGC_DEAD; 2424397c19d1SJeff Roberson unp->unp_gcflag |= UNPGC_REF; 2425397c19d1SJeff Roberson unp_marked++; 2426397c19d1SJeff Roberson } 2427be26ba7cSPawel Jakub Dawidek } 2428397c19d1SJeff Roberson 2429397c19d1SJeff Roberson static void 2430397c19d1SJeff Roberson unp_gc_process(struct unpcb *unp) 2431397c19d1SJeff Roberson { 2432779f106aSGleb Smirnoff struct socket *so, *soa; 2433397c19d1SJeff Roberson struct file *fp; 2434397c19d1SJeff Roberson 2435397c19d1SJeff Roberson /* Already processed. */ 2436397c19d1SJeff Roberson if (unp->unp_gcflag & UNPGC_SCANNED) 2437397c19d1SJeff Roberson return; 2438397c19d1SJeff Roberson fp = unp->unp_file; 243960a5ef26SRobert Watson 2440397c19d1SJeff Roberson /* 2441397c19d1SJeff Roberson * Check for a socket potentially in a cycle. It must be in a 2442397c19d1SJeff Roberson * queue as indicated by msgcount, and this must equal the file 2443397c19d1SJeff Roberson * reference count. Note that when msgcount is 0 the file is NULL. 2444397c19d1SJeff Roberson */ 244541e0f66dSJeff Roberson if ((unp->unp_gcflag & UNPGC_REF) == 0 && fp && 244641e0f66dSJeff Roberson unp->unp_msgcount != 0 && fp->f_count == unp->unp_msgcount) { 2447397c19d1SJeff Roberson unp->unp_gcflag |= UNPGC_DEAD; 2448397c19d1SJeff Roberson unp_unreachable++; 2449397c19d1SJeff Roberson return; 2450397c19d1SJeff Roberson } 245160a5ef26SRobert Watson 2452397c19d1SJeff Roberson so = unp->unp_socket; 2453779f106aSGleb Smirnoff SOCK_LOCK(so); 2454779f106aSGleb Smirnoff if (SOLISTENING(so)) { 2455397c19d1SJeff Roberson /* 2456397c19d1SJeff Roberson * Mark all sockets in our accept queue. 2457397c19d1SJeff Roberson */ 2458779f106aSGleb Smirnoff TAILQ_FOREACH(soa, &so->sol_comp, so_list) { 2459779f106aSGleb Smirnoff if (sotounpcb(soa)->unp_gcflag & UNPGC_IGNORE_RIGHTS) 24600c40f353SConrad Meyer continue; 2461397c19d1SJeff Roberson SOCKBUF_LOCK(&soa->so_rcv); 2462397c19d1SJeff Roberson unp_scan(soa->so_rcv.sb_mb, unp_accessable); 2463397c19d1SJeff Roberson SOCKBUF_UNLOCK(&soa->so_rcv); 2464397c19d1SJeff Roberson } 2465779f106aSGleb Smirnoff } else { 2466779f106aSGleb Smirnoff /* 2467779f106aSGleb Smirnoff * Mark all sockets we reference with RIGHTS. 2468779f106aSGleb Smirnoff */ 2469779f106aSGleb Smirnoff if ((unp->unp_gcflag & UNPGC_IGNORE_RIGHTS) == 0) { 2470779f106aSGleb Smirnoff SOCKBUF_LOCK(&so->so_rcv); 2471779f106aSGleb Smirnoff unp_scan(so->so_rcv.sb_mb, unp_accessable); 2472779f106aSGleb Smirnoff SOCKBUF_UNLOCK(&so->so_rcv); 2473779f106aSGleb Smirnoff } 2474779f106aSGleb Smirnoff } 2475779f106aSGleb Smirnoff SOCK_UNLOCK(so); 2476397c19d1SJeff Roberson unp->unp_gcflag |= UNPGC_SCANNED; 2477397c19d1SJeff Roberson } 2478a0ec558aSRobert Watson 2479a0ec558aSRobert Watson static int unp_recycled; 2480be6b1304STom Rhodes SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0, 2481be6b1304STom Rhodes "Number of unreachable sockets claimed by the garbage collector."); 2482df8bae1dSRodney W. Grimes 2483397c19d1SJeff Roberson static int unp_taskcount; 2484be6b1304STom Rhodes SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0, 2485be6b1304STom Rhodes "Number of times the garbage collector has run."); 2486397c19d1SJeff Roberson 2487f708ef1bSPoul-Henning Kamp static void 2488a0ec558aSRobert Watson unp_gc(__unused void *arg, int pending) 2489df8bae1dSRodney W. Grimes { 249084d61770SRobert Watson struct unp_head *heads[] = { &unp_dhead, &unp_shead, &unp_sphead, 249184d61770SRobert Watson NULL }; 2492397c19d1SJeff Roberson struct unp_head **head; 2493f7780c61SKonstantin Belousov struct file *f, **unref; 2494397c19d1SJeff Roberson struct unpcb *unp; 2495f7780c61SKonstantin Belousov int i, total; 2496df8bae1dSRodney W. Grimes 2497a0ec558aSRobert Watson unp_taskcount++; 2498779f106aSGleb Smirnoff UNP_LINK_RLOCK(); 2499ed5b7817SJulian Elischer /* 25000c40f353SConrad Meyer * First clear all gc flags from previous runs, apart from 25010c40f353SConrad Meyer * UNPGC_IGNORE_RIGHTS. 2502ed5b7817SJulian Elischer */ 2503397c19d1SJeff Roberson for (head = heads; *head != NULL; head++) 2504397c19d1SJeff Roberson LIST_FOREACH(unp, *head, unp_link) 25050c40f353SConrad Meyer unp->unp_gcflag = 25060c40f353SConrad Meyer (unp->unp_gcflag & UNPGC_IGNORE_RIGHTS); 250760a5ef26SRobert Watson 2508397c19d1SJeff Roberson /* 2509397c19d1SJeff Roberson * Scan marking all reachable sockets with UNPGC_REF. Once a socket 2510397c19d1SJeff Roberson * is reachable all of the sockets it references are reachable. 2511397c19d1SJeff Roberson * Stop the scan once we do a complete loop without discovering 2512397c19d1SJeff Roberson * a new reachable socket. 2513397c19d1SJeff Roberson */ 2514df8bae1dSRodney W. Grimes do { 2515397c19d1SJeff Roberson unp_unreachable = 0; 2516397c19d1SJeff Roberson unp_marked = 0; 2517397c19d1SJeff Roberson for (head = heads; *head != NULL; head++) 2518397c19d1SJeff Roberson LIST_FOREACH(unp, *head, unp_link) 2519397c19d1SJeff Roberson unp_gc_process(unp); 2520397c19d1SJeff Roberson } while (unp_marked); 2521779f106aSGleb Smirnoff UNP_LINK_RUNLOCK(); 2522397c19d1SJeff Roberson if (unp_unreachable == 0) 2523397c19d1SJeff Roberson return; 252460a5ef26SRobert Watson 2525ed5b7817SJulian Elischer /* 2526397c19d1SJeff Roberson * Allocate space for a local list of dead unpcbs. 2527ed5b7817SJulian Elischer */ 2528397c19d1SJeff Roberson unref = malloc(unp_unreachable * sizeof(struct file *), 2529397c19d1SJeff Roberson M_TEMP, M_WAITOK); 253060a5ef26SRobert Watson 2531ed5b7817SJulian Elischer /* 2532397c19d1SJeff Roberson * Iterate looking for sockets which have been specifically marked 2533397c19d1SJeff Roberson * as as unreachable and store them locally. 2534ed5b7817SJulian Elischer */ 2535f7780c61SKonstantin Belousov UNP_LINK_RLOCK(); 2536f7780c61SKonstantin Belousov for (total = 0, head = heads; *head != NULL; head++) 2537397c19d1SJeff Roberson LIST_FOREACH(unp, *head, unp_link) 2538f7780c61SKonstantin Belousov if ((unp->unp_gcflag & UNPGC_DEAD) != 0) { 2539f7780c61SKonstantin Belousov f = unp->unp_file; 2540f7780c61SKonstantin Belousov if (unp->unp_msgcount == 0 || f == NULL || 2541f7780c61SKonstantin Belousov f->f_count != unp->unp_msgcount) 2542f7780c61SKonstantin Belousov continue; 2543f7780c61SKonstantin Belousov unref[total++] = f; 2544f7780c61SKonstantin Belousov fhold(f); 2545f7780c61SKonstantin Belousov KASSERT(total <= unp_unreachable, 2546397c19d1SJeff Roberson ("unp_gc: incorrect unreachable count.")); 2547397c19d1SJeff Roberson } 2548f7780c61SKonstantin Belousov UNP_LINK_RUNLOCK(); 254960a5ef26SRobert Watson 2550ed5b7817SJulian Elischer /* 2551397c19d1SJeff Roberson * Now flush all sockets, free'ing rights. This will free the 2552397c19d1SJeff Roberson * struct files associated with these sockets but leave each socket 2553397c19d1SJeff Roberson * with one remaining ref. 2554ed5b7817SJulian Elischer */ 25551fb51a12SBjoern A. Zeeb for (i = 0; i < total; i++) { 25561fb51a12SBjoern A. Zeeb struct socket *so; 25571fb51a12SBjoern A. Zeeb 25581fb51a12SBjoern A. Zeeb so = unref[i]->f_data; 25591fb51a12SBjoern A. Zeeb CURVNET_SET(so->so_vnet); 25601fb51a12SBjoern A. Zeeb sorflush(so); 25611fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 25621fb51a12SBjoern A. Zeeb } 256360a5ef26SRobert Watson 2564ed5b7817SJulian Elischer /* 2565397c19d1SJeff Roberson * And finally release the sockets so they can be reclaimed. 2566ed5b7817SJulian Elischer */ 2567f7780c61SKonstantin Belousov for (i = 0; i < total; i++) 2568397c19d1SJeff Roberson fdrop(unref[i], NULL); 2569f7780c61SKonstantin Belousov unp_recycled += total; 2570397c19d1SJeff Roberson free(unref, M_TEMP); 2571df8bae1dSRodney W. Grimes } 2572df8bae1dSRodney W. Grimes 25730b36cd25SRobert Watson static void 257499ab95dbSMark Johnston unp_dispose_mbuf(struct mbuf *m) 2575df8bae1dSRodney W. Grimes { 2576996c772fSJohn Dyson 2577df8bae1dSRodney W. Grimes if (m) 2578be26ba7cSPawel Jakub Dawidek unp_scan(m, unp_freerights); 2579df8bae1dSRodney W. Grimes } 2580df8bae1dSRodney W. Grimes 25810c40f353SConrad Meyer /* 25820c40f353SConrad Meyer * Synchronize against unp_gc, which can trip over data as we are freeing it. 25830c40f353SConrad Meyer */ 25840c40f353SConrad Meyer static void 258599ab95dbSMark Johnston unp_dispose(struct socket *so) 25860c40f353SConrad Meyer { 25870c40f353SConrad Meyer struct unpcb *unp; 25880c40f353SConrad Meyer 25890c40f353SConrad Meyer unp = sotounpcb(so); 2590779f106aSGleb Smirnoff UNP_LINK_WLOCK(); 25910c40f353SConrad Meyer unp->unp_gcflag |= UNPGC_IGNORE_RIGHTS; 2592779f106aSGleb Smirnoff UNP_LINK_WUNLOCK(); 2593779f106aSGleb Smirnoff if (!SOLISTENING(so)) 259499ab95dbSMark Johnston unp_dispose_mbuf(so->so_rcv.sb_mb); 25950c40f353SConrad Meyer } 25960c40f353SConrad Meyer 2597f708ef1bSPoul-Henning Kamp static void 2598be26ba7cSPawel Jakub Dawidek unp_scan(struct mbuf *m0, void (*op)(struct filedescent **, int)) 2599df8bae1dSRodney W. Grimes { 26002bc21ed9SDavid Malone struct mbuf *m; 26012bc21ed9SDavid Malone struct cmsghdr *cm; 26022bc21ed9SDavid Malone void *data; 26032bc21ed9SDavid Malone socklen_t clen, datalen; 2604df8bae1dSRodney W. Grimes 2605fc3fcacfSRobert Watson while (m0 != NULL) { 26062bc21ed9SDavid Malone for (m = m0; m; m = m->m_next) { 260712396bdcSDavid Malone if (m->m_type != MT_CONTROL) 2608df8bae1dSRodney W. Grimes continue; 26092bc21ed9SDavid Malone 26102bc21ed9SDavid Malone cm = mtod(m, struct cmsghdr *); 26112bc21ed9SDavid Malone clen = m->m_len; 26122bc21ed9SDavid Malone 26132bc21ed9SDavid Malone while (cm != NULL) { 26142bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) 26152bc21ed9SDavid Malone break; 26162bc21ed9SDavid Malone 26172bc21ed9SDavid Malone data = CMSG_DATA(cm); 26182bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len 26192bc21ed9SDavid Malone - (caddr_t)data; 26202bc21ed9SDavid Malone 26212bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET && 26222bc21ed9SDavid Malone cm->cmsg_type == SCM_RIGHTS) { 2623be26ba7cSPawel Jakub Dawidek (*op)(data, datalen / 2624be26ba7cSPawel Jakub Dawidek sizeof(struct filedescent *)); 26252bc21ed9SDavid Malone } 26262bc21ed9SDavid Malone 26272bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 26282bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 26292bc21ed9SDavid Malone cm = (struct cmsghdr *) 26302bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 26312bc21ed9SDavid Malone } else { 26322bc21ed9SDavid Malone clen = 0; 26332bc21ed9SDavid Malone cm = NULL; 26342bc21ed9SDavid Malone } 26352bc21ed9SDavid Malone } 2636df8bae1dSRodney W. Grimes } 2637c29a3321SKevin Lo m0 = m0->m_nextpkt; 2638df8bae1dSRodney W. Grimes } 2639df8bae1dSRodney W. Grimes } 2640df8bae1dSRodney W. Grimes 2641662c901cSMikolaj Golub /* 2642662c901cSMikolaj Golub * A helper function called by VFS before socket-type vnode reclamation. 2643662c901cSMikolaj Golub * For an active vnode it clears unp_vnode pointer and decrements unp_vnode 2644662c901cSMikolaj Golub * use count. 2645662c901cSMikolaj Golub */ 2646662c901cSMikolaj Golub void 2647662c901cSMikolaj Golub vfs_unp_reclaim(struct vnode *vp) 2648662c901cSMikolaj Golub { 2649662c901cSMikolaj Golub struct unpcb *unp; 2650662c901cSMikolaj Golub int active; 265175a67bf3SMatt Macy struct mtx *vplock; 2652662c901cSMikolaj Golub 2653662c901cSMikolaj Golub ASSERT_VOP_ELOCKED(vp, "vfs_unp_reclaim"); 2654662c901cSMikolaj Golub KASSERT(vp->v_type == VSOCK, 2655662c901cSMikolaj Golub ("vfs_unp_reclaim: vp->v_type != VSOCK")); 2656662c901cSMikolaj Golub 2657662c901cSMikolaj Golub active = 0; 265875a67bf3SMatt Macy vplock = mtx_pool_find(mtxpool_sleep, vp); 265975a67bf3SMatt Macy mtx_lock(vplock); 26600c3c207fSGleb Smirnoff VOP_UNP_CONNECT(vp, &unp); 2661662c901cSMikolaj Golub if (unp == NULL) 2662662c901cSMikolaj Golub goto done; 2663662c901cSMikolaj Golub UNP_PCB_LOCK(unp); 2664c7e41c8bSMikolaj Golub if (unp->unp_vnode == vp) { 2665c7e41c8bSMikolaj Golub VOP_UNP_DETACH(vp); 2666662c901cSMikolaj Golub unp->unp_vnode = NULL; 2667662c901cSMikolaj Golub active = 1; 2668662c901cSMikolaj Golub } 2669662c901cSMikolaj Golub UNP_PCB_UNLOCK(unp); 2670662c901cSMikolaj Golub done: 267175a67bf3SMatt Macy mtx_unlock(vplock); 2672662c901cSMikolaj Golub if (active) 2673662c901cSMikolaj Golub vunref(vp); 2674662c901cSMikolaj Golub } 2675662c901cSMikolaj Golub 267603c96c31SRobert Watson #ifdef DDB 267703c96c31SRobert Watson static void 267803c96c31SRobert Watson db_print_indent(int indent) 267903c96c31SRobert Watson { 268003c96c31SRobert Watson int i; 268103c96c31SRobert Watson 268203c96c31SRobert Watson for (i = 0; i < indent; i++) 268303c96c31SRobert Watson db_printf(" "); 268403c96c31SRobert Watson } 268503c96c31SRobert Watson 268603c96c31SRobert Watson static void 268703c96c31SRobert Watson db_print_unpflags(int unp_flags) 268803c96c31SRobert Watson { 268903c96c31SRobert Watson int comma; 269003c96c31SRobert Watson 269103c96c31SRobert Watson comma = 0; 269203c96c31SRobert Watson if (unp_flags & UNP_HAVEPC) { 269303c96c31SRobert Watson db_printf("%sUNP_HAVEPC", comma ? ", " : ""); 269403c96c31SRobert Watson comma = 1; 269503c96c31SRobert Watson } 269603c96c31SRobert Watson if (unp_flags & UNP_WANTCRED) { 269703c96c31SRobert Watson db_printf("%sUNP_WANTCRED", comma ? ", " : ""); 269803c96c31SRobert Watson comma = 1; 269903c96c31SRobert Watson } 270003c96c31SRobert Watson if (unp_flags & UNP_CONNWAIT) { 270103c96c31SRobert Watson db_printf("%sUNP_CONNWAIT", comma ? ", " : ""); 270203c96c31SRobert Watson comma = 1; 270303c96c31SRobert Watson } 270403c96c31SRobert Watson if (unp_flags & UNP_CONNECTING) { 270503c96c31SRobert Watson db_printf("%sUNP_CONNECTING", comma ? ", " : ""); 270603c96c31SRobert Watson comma = 1; 270703c96c31SRobert Watson } 270803c96c31SRobert Watson if (unp_flags & UNP_BINDING) { 270903c96c31SRobert Watson db_printf("%sUNP_BINDING", comma ? ", " : ""); 271003c96c31SRobert Watson comma = 1; 271103c96c31SRobert Watson } 271203c96c31SRobert Watson } 271303c96c31SRobert Watson 271403c96c31SRobert Watson static void 271503c96c31SRobert Watson db_print_xucred(int indent, struct xucred *xu) 271603c96c31SRobert Watson { 271703c96c31SRobert Watson int comma, i; 271803c96c31SRobert Watson 271903c96c31SRobert Watson db_print_indent(indent); 272003c96c31SRobert Watson db_printf("cr_version: %u cr_uid: %u cr_ngroups: %d\n", 272103c96c31SRobert Watson xu->cr_version, xu->cr_uid, xu->cr_ngroups); 272203c96c31SRobert Watson db_print_indent(indent); 272303c96c31SRobert Watson db_printf("cr_groups: "); 272403c96c31SRobert Watson comma = 0; 272503c96c31SRobert Watson for (i = 0; i < xu->cr_ngroups; i++) { 272603c96c31SRobert Watson db_printf("%s%u", comma ? ", " : "", xu->cr_groups[i]); 272703c96c31SRobert Watson comma = 1; 272803c96c31SRobert Watson } 272903c96c31SRobert Watson db_printf("\n"); 273003c96c31SRobert Watson } 273103c96c31SRobert Watson 273203c96c31SRobert Watson static void 273303c96c31SRobert Watson db_print_unprefs(int indent, struct unp_head *uh) 273403c96c31SRobert Watson { 273503c96c31SRobert Watson struct unpcb *unp; 273603c96c31SRobert Watson int counter; 273703c96c31SRobert Watson 273803c96c31SRobert Watson counter = 0; 273903c96c31SRobert Watson LIST_FOREACH(unp, uh, unp_reflink) { 274003c96c31SRobert Watson if (counter % 4 == 0) 274103c96c31SRobert Watson db_print_indent(indent); 274203c96c31SRobert Watson db_printf("%p ", unp); 274303c96c31SRobert Watson if (counter % 4 == 3) 274403c96c31SRobert Watson db_printf("\n"); 274503c96c31SRobert Watson counter++; 274603c96c31SRobert Watson } 274703c96c31SRobert Watson if (counter != 0 && counter % 4 != 0) 274803c96c31SRobert Watson db_printf("\n"); 274903c96c31SRobert Watson } 275003c96c31SRobert Watson 275103c96c31SRobert Watson DB_SHOW_COMMAND(unpcb, db_show_unpcb) 275203c96c31SRobert Watson { 275303c96c31SRobert Watson struct unpcb *unp; 275403c96c31SRobert Watson 275503c96c31SRobert Watson if (!have_addr) { 275603c96c31SRobert Watson db_printf("usage: show unpcb <addr>\n"); 275703c96c31SRobert Watson return; 275803c96c31SRobert Watson } 275903c96c31SRobert Watson unp = (struct unpcb *)addr; 276003c96c31SRobert Watson 276103c96c31SRobert Watson db_printf("unp_socket: %p unp_vnode: %p\n", unp->unp_socket, 276203c96c31SRobert Watson unp->unp_vnode); 276303c96c31SRobert Watson 2764fc8fdae0SMatthew D Fleming db_printf("unp_ino: %ju unp_conn: %p\n", (uintmax_t)unp->unp_ino, 276503c96c31SRobert Watson unp->unp_conn); 276603c96c31SRobert Watson 276703c96c31SRobert Watson db_printf("unp_refs:\n"); 276803c96c31SRobert Watson db_print_unprefs(2, &unp->unp_refs); 276903c96c31SRobert Watson 277003c96c31SRobert Watson /* XXXRW: Would be nice to print the full address, if any. */ 277103c96c31SRobert Watson db_printf("unp_addr: %p\n", unp->unp_addr); 277203c96c31SRobert Watson 2773c2090e73SAlan Somers db_printf("unp_gencnt: %llu\n", 277403c96c31SRobert Watson (unsigned long long)unp->unp_gencnt); 277503c96c31SRobert Watson 277603c96c31SRobert Watson db_printf("unp_flags: %x (", unp->unp_flags); 277703c96c31SRobert Watson db_print_unpflags(unp->unp_flags); 277803c96c31SRobert Watson db_printf(")\n"); 277903c96c31SRobert Watson 278003c96c31SRobert Watson db_printf("unp_peercred:\n"); 278103c96c31SRobert Watson db_print_xucred(2, &unp->unp_peercred); 278203c96c31SRobert Watson 278303c96c31SRobert Watson db_printf("unp_refcount: %u\n", unp->unp_refcount); 278403c96c31SRobert Watson } 278503c96c31SRobert Watson #endif 2786