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 7e1ac28e2SRobert Watson * All rights reserved. 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 * 194*75a67bf3SMatt Macy * Three types of locks exist in the local domain socket implementation: a 195*75a67bf3SMatt Macy * a global linkage rwlock, the mtxpool lock, and per-unpcb mutexes. 196*75a67bf3SMatt Macy * The linkage lock protects the socket count, global generation number, 197*75a67bf3SMatt Macy * and stream/datagram global lists. 198*75a67bf3SMatt Macy * 199*75a67bf3SMatt Macy * The mtxpool lock protects the vnode from being modified while referenced. 200*75a67bf3SMatt Macy * Lock ordering requires that it be acquired before any unpcb locks. 201*75a67bf3SMatt Macy * 202*75a67bf3SMatt Macy * The unpcb lock (unp_mtx) protects all fields in the unpcb. Of particular 203*75a67bf3SMatt Macy * note is that this includes the unp_conn field. So long as the unpcb lock 204*75a67bf3SMatt Macy * is held the reference to the unpcb pointed to by unp_conn is valid. If we 205*75a67bf3SMatt Macy * require that the unpcb pointed to by unp_conn remain live in cases where 206*75a67bf3SMatt Macy * we need to drop the unp_mtx as when we need to acquire the lock for a 207*75a67bf3SMatt Macy * second unpcb the caller must first acquire an additional reference on the 208*75a67bf3SMatt Macy * second unpcb and then revalidate any state (typically check that unp_conn 209*75a67bf3SMatt Macy * is non-NULL) upon requiring the initial unpcb lock. The lock ordering 210*75a67bf3SMatt Macy * between unpcbs is the conventional ascending address order. Two helper 211*75a67bf3SMatt Macy * routines exist for this: 212*75a67bf3SMatt Macy * 213*75a67bf3SMatt Macy * - unp_pcb_lock2(unp, unp2) - which just acquires the two locks in the 214*75a67bf3SMatt Macy * safe ordering. 215*75a67bf3SMatt Macy * 216*75a67bf3SMatt Macy * - unp_pcb_owned_lock2(unp, unp2, freed) - the lock for unp is held 217*75a67bf3SMatt Macy * when called. If unp is unlocked and unp2 is subsequently freed 218*75a67bf3SMatt Macy * freed will be set to 1. 219*75a67bf3SMatt Macy * 220*75a67bf3SMatt Macy * The helper routines for references are: 221*75a67bf3SMatt Macy * 222*75a67bf3SMatt Macy * - unp_pcb_hold(unp): Can be called any time we currently hold a valid 223*75a67bf3SMatt Macy * reference to unp. 224*75a67bf3SMatt Macy * 225*75a67bf3SMatt Macy * - unp_pcb_rele(unp): The caller must hold the unp lock. If we are 226*75a67bf3SMatt Macy * releasing the last reference, detach must have been called thus 227*75a67bf3SMatt 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 241*75a67bf3SMatt Macy * unp->unp_conn->unp_socket, you need to hold a lock on unp_conn to guarantee 242*75a67bf3SMatt 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 281*75a67bf3SMatt Macy #define UNP_REF_LIST_LOCK() UNP_DEFERRED_LOCK(); 282*75a67bf3SMatt Macy #define UNP_REF_LIST_UNLOCK() UNP_DEFERRED_UNLOCK(); 283*75a67bf3SMatt Macy 284e7c33e29SRobert Watson #define UNP_PCB_LOCK_INIT(unp) mtx_init(&(unp)->unp_mtx, \ 285e7c33e29SRobert Watson "unp_mtx", "unp_mtx", \ 286*75a67bf3SMatt 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) 289*75a67bf3SMatt Macy #define UNP_PCB_TRYLOCK(unp) mtx_trylock(&(unp)->unp_mtx) 290e7c33e29SRobert Watson #define UNP_PCB_UNLOCK(unp) mtx_unlock(&(unp)->unp_mtx) 291*75a67bf3SMatt 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) 293*75a67bf3SMatt 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 319*75a67bf3SMatt Macy 320*75a67bf3SMatt Macy static void 321*75a67bf3SMatt Macy unp_pcb_hold(struct unpcb *unp) 322*75a67bf3SMatt Macy { 323*75a67bf3SMatt Macy MPASS(unp->unp_refcount); 324*75a67bf3SMatt Macy refcount_acquire(&unp->unp_refcount); 325*75a67bf3SMatt Macy } 326*75a67bf3SMatt Macy 327*75a67bf3SMatt Macy static int 328*75a67bf3SMatt Macy unp_pcb_rele(struct unpcb *unp) 329*75a67bf3SMatt Macy { 330*75a67bf3SMatt Macy int freed; 331*75a67bf3SMatt Macy 332*75a67bf3SMatt Macy UNP_PCB_LOCK_ASSERT(unp); 333*75a67bf3SMatt Macy MPASS(unp->unp_refcount); 334*75a67bf3SMatt Macy if ((freed = refcount_release(&unp->unp_refcount))) { 335*75a67bf3SMatt Macy /* we got here with having detached? */ 336*75a67bf3SMatt Macy MPASS(unp->unp_socket == NULL); 337*75a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 338*75a67bf3SMatt Macy UNP_PCB_LOCK_DESTROY(unp); 339*75a67bf3SMatt Macy uma_zfree(unp_zone, unp); 340*75a67bf3SMatt Macy } 341*75a67bf3SMatt Macy return (freed); 342*75a67bf3SMatt Macy } 343*75a67bf3SMatt Macy 344*75a67bf3SMatt Macy static void 345*75a67bf3SMatt Macy unp_pcb_lock2(struct unpcb *unp, struct unpcb *unp2) 346*75a67bf3SMatt Macy { 347*75a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT(unp); 348*75a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT(unp2); 349*75a67bf3SMatt Macy if ((uintptr_t)unp2 > (uintptr_t)unp) { 350*75a67bf3SMatt Macy UNP_PCB_LOCK(unp); 351*75a67bf3SMatt Macy UNP_PCB_LOCK(unp2); 352*75a67bf3SMatt Macy } else { 353*75a67bf3SMatt Macy UNP_PCB_LOCK(unp2); 354*75a67bf3SMatt Macy UNP_PCB_LOCK(unp); 355*75a67bf3SMatt Macy } 356*75a67bf3SMatt Macy } 357*75a67bf3SMatt Macy 358*75a67bf3SMatt Macy static __noinline void 359*75a67bf3SMatt Macy unp_pcb_owned_lock2_slowpath(struct unpcb *unp, struct unpcb **unp2p, int *freed) 360*75a67bf3SMatt Macy 361*75a67bf3SMatt Macy { 362*75a67bf3SMatt Macy struct unpcb *unp2; 363*75a67bf3SMatt Macy 364*75a67bf3SMatt Macy unp2 = *unp2p; 365*75a67bf3SMatt Macy unp_pcb_hold((unp2)); 366*75a67bf3SMatt Macy UNP_PCB_UNLOCK((unp)); 367*75a67bf3SMatt Macy UNP_PCB_LOCK((unp2)); 368*75a67bf3SMatt Macy UNP_PCB_LOCK((unp)); 369*75a67bf3SMatt Macy *freed = unp_pcb_rele((unp2)); 370*75a67bf3SMatt Macy if (*freed) 371*75a67bf3SMatt Macy *unp2p = NULL; 372*75a67bf3SMatt Macy } 373*75a67bf3SMatt Macy 374*75a67bf3SMatt Macy #define unp_pcb_owned_lock2(unp, unp2, freed) do { \ 375*75a67bf3SMatt Macy freed = 0; \ 376*75a67bf3SMatt Macy UNP_PCB_LOCK_ASSERT((unp)); \ 377*75a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT((unp2)); \ 378*75a67bf3SMatt Macy if (__predict_true(UNP_PCB_TRYLOCK((unp2)))) \ 379*75a67bf3SMatt Macy break; \ 380*75a67bf3SMatt Macy else if ((uintptr_t)(unp2) > (uintptr_t)(unp)) \ 381*75a67bf3SMatt Macy UNP_PCB_LOCK((unp2)); \ 382*75a67bf3SMatt Macy else { \ 383*75a67bf3SMatt Macy unp_pcb_owned_lock2_slowpath((unp), &(unp2), &freed); \ 384*75a67bf3SMatt Macy } \ 385*75a67bf3SMatt Macy } while (0) 386*75a67bf3SMatt Macy 387*75a67bf3SMatt 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")); 443*75a67bf3SMatt 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) { 448*75a67bf3SMatt Macy unp_pcb_hold(unp2); 449e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 450*75a67bf3SMatt Macy unp_drop(unp2); 451*75a67bf3SMatt Macy } else 452*75a67bf3SMatt 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; 701*75a67bf3SMatt Macy struct mtx *vplock; 702*75a67bf3SMatt Macy int freed; 703a152f8a3SRobert Watson unp = sotounpcb(so); 704a152f8a3SRobert Watson KASSERT(unp != NULL, ("uipc_close: unp == NULL")); 705e7c33e29SRobert Watson 706*75a67bf3SMatt Macy 707*75a67bf3SMatt Macy vplock = NULL; 708*75a67bf3SMatt Macy if ((vp = unp->unp_vnode) != NULL) { 709*75a67bf3SMatt Macy vplock = mtx_pool_find(mtxpool_sleep, vp); 710*75a67bf3SMatt Macy mtx_lock(vplock); 711e7c33e29SRobert Watson } 712*75a67bf3SMatt Macy UNP_PCB_LOCK(unp); 713*75a67bf3SMatt Macy if (vp && unp->unp_vnode == NULL) { 714*75a67bf3SMatt Macy mtx_unlock(vplock); 715*75a67bf3SMatt Macy vp = NULL; 716*75a67bf3SMatt Macy } 717*75a67bf3SMatt Macy if (vp != NULL) { 718779f106aSGleb Smirnoff VOP_UNP_DETACH(vp); 719779f106aSGleb Smirnoff unp->unp_vnode = NULL; 720779f106aSGleb Smirnoff } 721*75a67bf3SMatt Macy unp2 = unp->unp_conn; 722*75a67bf3SMatt Macy unp_pcb_hold(unp); 723*75a67bf3SMatt Macy if (unp2 != NULL) { 724*75a67bf3SMatt Macy unp_pcb_hold(unp2); 725*75a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freed); 726*75a67bf3SMatt Macy unp_disconnect(unp, unp2); 727*75a67bf3SMatt Macy if (unp_pcb_rele(unp2) == 0) 728*75a67bf3SMatt Macy UNP_PCB_UNLOCK(unp2); 729*75a67bf3SMatt Macy } 730*75a67bf3SMatt Macy if (unp_pcb_rele(unp) == 0) 731e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 732*75a67bf3SMatt Macy if (vp) { 733*75a67bf3SMatt Macy mtx_unlock(vplock); 734779f106aSGleb Smirnoff vrele(vp); 735a152f8a3SRobert Watson } 736*75a67bf3SMatt 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")); 748*75a67bf3SMatt 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; 759*75a67bf3SMatt 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; 768434ac8b6SMark Johnston local_unp_rights = 0; 769434ac8b6SMark Johnston 770779f106aSGleb Smirnoff UNP_LINK_WLOCK(); 7716d32873cSRobert Watson LIST_REMOVE(unp, unp_link); 7726d32873cSRobert Watson unp->unp_gencnt = ++unp_gencnt; 7736d32873cSRobert Watson --unp_count; 774*75a67bf3SMatt Macy UNP_LINK_WUNLOCK(); 775434ac8b6SMark Johnston 776*75a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT(unp); 777*75a67bf3SMatt Macy restart: 778*75a67bf3SMatt Macy if ((vp = unp->unp_vnode) != NULL) { 779*75a67bf3SMatt Macy vplock = mtx_pool_find(mtxpool_sleep, vp); 780*75a67bf3SMatt Macy mtx_lock(vplock); 781*75a67bf3SMatt Macy } 782*75a67bf3SMatt Macy UNP_PCB_LOCK(unp); 783*75a67bf3SMatt Macy if ((unp2 = unp->unp_conn) != NULL) { 784*75a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freeunp); 785*75a67bf3SMatt Macy if (freeunp) 786*75a67bf3SMatt Macy unp2 = NULL; 787*75a67bf3SMatt Macy } 788*75a67bf3SMatt Macy if (unp->unp_vnode != vp && 789*75a67bf3SMatt Macy unp->unp_vnode != NULL) { 790*75a67bf3SMatt Macy mtx_unlock(vplock); 791*75a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 792*75a67bf3SMatt Macy if (unp2) 793*75a67bf3SMatt Macy UNP_PCB_UNLOCK(unp2); 794*75a67bf3SMatt Macy goto restart; 795*75a67bf3SMatt Macy } 796*75a67bf3SMatt Macy if ((unp->unp_flags & UNP_NASCENT) != 0) { 797*75a67bf3SMatt Macy if (unp2) 798*75a67bf3SMatt Macy UNP_PCB_UNLOCK(unp2); 799*75a67bf3SMatt Macy goto teardown; 800*75a67bf3SMatt Macy } 8016d32873cSRobert Watson if ((vp = unp->unp_vnode) != NULL) { 802c7e41c8bSMikolaj Golub VOP_UNP_DETACH(vp); 8036d32873cSRobert Watson unp->unp_vnode = NULL; 8046d32873cSRobert Watson } 805*75a67bf3SMatt Macy unp_pcb_hold(unp); 806e7c33e29SRobert Watson if (unp2 != NULL) { 807*75a67bf3SMatt Macy unp_pcb_hold(unp2); 808e7c33e29SRobert Watson unp_disconnect(unp, unp2); 809*75a67bf3SMatt Macy if (unp_pcb_rele(unp2) == 0) 810e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 811e7c33e29SRobert Watson } 812*75a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 813*75a67bf3SMatt Macy UNP_REF_LIST_LOCK(); 8146d32873cSRobert Watson while (!LIST_EMPTY(&unp->unp_refs)) { 8156d32873cSRobert Watson struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 816e7c33e29SRobert Watson 817*75a67bf3SMatt Macy unp_pcb_hold(ref); 818*75a67bf3SMatt Macy UNP_REF_LIST_UNLOCK(); 819*75a67bf3SMatt Macy 820*75a67bf3SMatt Macy MPASS(ref != unp); 821*75a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT(ref); 822afc055d9SEd Schouten unp_drop(ref); 823*75a67bf3SMatt Macy UNP_REF_LIST_LOCK(); 8246d32873cSRobert Watson } 825*75a67bf3SMatt Macy 826*75a67bf3SMatt Macy UNP_REF_LIST_UNLOCK(); 827*75a67bf3SMatt Macy UNP_PCB_LOCK(unp); 828*75a67bf3SMatt Macy freeunp = unp_pcb_rele(unp); 829*75a67bf3SMatt Macy MPASS(freeunp == 0); 830397c19d1SJeff Roberson local_unp_rights = unp_rights; 831434ac8b6SMark Johnston teardown: 8326d32873cSRobert Watson unp->unp_socket->so_pcb = NULL; 8339ae328fcSJohn Baldwin saved_unp_addr = unp->unp_addr; 8349ae328fcSJohn Baldwin unp->unp_addr = NULL; 835*75a67bf3SMatt Macy unp->unp_socket = NULL; 836*75a67bf3SMatt Macy freeunp = unp_pcb_rele(unp); 8379ae328fcSJohn Baldwin if (saved_unp_addr != NULL) 8381ede983cSDag-Erling Smørgrav free(saved_unp_addr, M_SONAME); 839*75a67bf3SMatt Macy if (!freeunp) 8406e2faa24SRobert Watson UNP_PCB_UNLOCK(unp); 841*75a67bf3SMatt Macy if (vp) { 842*75a67bf3SMatt Macy mtx_unlock(vplock); 8436d32873cSRobert Watson vrele(vp); 844*75a67bf3SMatt Macy } 8456d32873cSRobert Watson if (local_unp_rights) 846daee0f0bSKonstantin Belousov taskqueue_enqueue_timeout(taskqueue_thread, &unp_gc_task, -1); 847a29f300eSGarrett Wollman } 848a29f300eSGarrett Wollman 849a29f300eSGarrett Wollman static int 850a29f300eSGarrett Wollman uipc_disconnect(struct socket *so) 851a29f300eSGarrett Wollman { 852e7c33e29SRobert Watson struct unpcb *unp, *unp2; 853*75a67bf3SMatt Macy int freed; 854a29f300eSGarrett Wollman 85540f2ac28SRobert Watson unp = sotounpcb(so); 8564d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_disconnect: unp == NULL")); 857e7c33e29SRobert Watson 858e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 859*75a67bf3SMatt Macy if ((unp2 = unp->unp_conn) == NULL) { 860e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 861*75a67bf3SMatt Macy return (0); 862*75a67bf3SMatt Macy } 863*75a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freed); 864*75a67bf3SMatt Macy if (__predict_false(freed)) { 865*75a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 866*75a67bf3SMatt Macy return (0); 867*75a67bf3SMatt Macy } 868*75a67bf3SMatt Macy unp_pcb_hold(unp2); 869*75a67bf3SMatt Macy unp_pcb_hold(unp); 870*75a67bf3SMatt Macy unp_disconnect(unp, unp2); 871*75a67bf3SMatt Macy if (unp_pcb_rele(unp) == 0) 872*75a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 873*75a67bf3SMatt Macy if (unp_pcb_rele(unp2) == 0) 874*75a67bf3SMatt Macy UNP_PCB_UNLOCK(unp2); 875e5aeaa0cSDag-Erling Smørgrav return (0); 876a29f300eSGarrett Wollman } 877a29f300eSGarrett Wollman 878a29f300eSGarrett Wollman static int 879d374e81eSRobert Watson uipc_listen(struct socket *so, int backlog, struct thread *td) 880a29f300eSGarrett Wollman { 88140f2ac28SRobert Watson struct unpcb *unp; 8820d9ce3a1SRobert Watson int error; 883a29f300eSGarrett Wollman 884beb4b312SGleb Smirnoff if (so->so_type != SOCK_STREAM && so->so_type != SOCK_SEQPACKET) 885beb4b312SGleb Smirnoff return (EOPNOTSUPP); 886beb4b312SGleb Smirnoff 88740f2ac28SRobert Watson unp = sotounpcb(so); 8884d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_listen: unp == NULL")); 889e7c33e29SRobert Watson 890e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 8914d4b555eSRobert Watson if (unp->unp_vnode == NULL) { 89247a84387SEd Schouten /* Already connected or not bound to an address. */ 89347a84387SEd Schouten error = unp->unp_conn != NULL ? EINVAL : EDESTADDRREQ; 894e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 89547a84387SEd Schouten return (error); 89640f2ac28SRobert Watson } 897e7c33e29SRobert Watson 898e7c33e29SRobert Watson SOCK_LOCK(so); 899e7c33e29SRobert Watson error = solisten_proto_check(so); 900e7c33e29SRobert Watson if (error == 0) { 901e7c33e29SRobert Watson cru2x(td->td_ucred, &unp->unp_peercred); 902e7c33e29SRobert Watson solisten_proto(so, backlog); 903e7c33e29SRobert Watson } 904e7c33e29SRobert Watson SOCK_UNLOCK(so); 905e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 9060d9ce3a1SRobert Watson return (error); 907a29f300eSGarrett Wollman } 908a29f300eSGarrett Wollman 909a29f300eSGarrett Wollman static int 91057bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 911a29f300eSGarrett Wollman { 912e7c33e29SRobert Watson struct unpcb *unp, *unp2; 9130d9ce3a1SRobert Watson const struct sockaddr *sa; 914a29f300eSGarrett Wollman 9154d4b555eSRobert Watson unp = sotounpcb(so); 9164d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL")); 917e7c33e29SRobert Watson 9180d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 919afd9f91cSJohn Baldwin UNP_LINK_RLOCK(); 920bdc5f6a3SHajimu UMEMOTO /* 921e7c33e29SRobert Watson * XXX: It seems that this test always fails even when connection is 922e7c33e29SRobert Watson * established. So, this else clause is added as workaround to 923e7c33e29SRobert Watson * return PF_LOCAL sockaddr. 924bdc5f6a3SHajimu UMEMOTO */ 925e7c33e29SRobert Watson unp2 = unp->unp_conn; 926e7c33e29SRobert Watson if (unp2 != NULL) { 927e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 928e7c33e29SRobert Watson if (unp2->unp_addr != NULL) 929afd9f91cSJohn Baldwin sa = (struct sockaddr *) unp2->unp_addr; 930e7c33e29SRobert Watson else 9310d9ce3a1SRobert Watson sa = &sun_noname; 9320d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 933e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 934e7c33e29SRobert Watson } else { 935e7c33e29SRobert Watson sa = &sun_noname; 936e7c33e29SRobert Watson bcopy(sa, *nam, sa->sa_len); 937e7c33e29SRobert Watson } 938afd9f91cSJohn Baldwin UNP_LINK_RUNLOCK(); 939e5aeaa0cSDag-Erling Smørgrav return (0); 940a29f300eSGarrett Wollman } 941a29f300eSGarrett Wollman 942a29f300eSGarrett Wollman static int 943a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 944a29f300eSGarrett Wollman { 945e7c33e29SRobert Watson struct unpcb *unp, *unp2; 946a29f300eSGarrett Wollman struct socket *so2; 947337cc6b6SRobert Watson u_int mbcnt, sbcc; 948a29f300eSGarrett Wollman 94940f2ac28SRobert Watson unp = sotounpcb(so); 9502b21d0e8SGleb Smirnoff KASSERT(unp != NULL, ("%s: unp == NULL", __func__)); 9512b21d0e8SGleb Smirnoff KASSERT(so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET, 9522b21d0e8SGleb Smirnoff ("%s: socktype %d", __func__, so->so_type)); 953e7c33e29SRobert Watson 954df8bae1dSRodney W. Grimes /* 955e7c33e29SRobert Watson * Adjust backpressure on sender and wakeup any waiting to write. 956e7c33e29SRobert Watson * 957d7924b70SRobert Watson * The unp lock is acquired to maintain the validity of the unp_conn 958d7924b70SRobert Watson * pointer; no lock on unp2 is required as unp2->unp_socket will be 959d7924b70SRobert Watson * static as long as we don't permit unp2 to disconnect from unp, 960d7924b70SRobert Watson * which is prevented by the lock on unp. We cache values from 961d7924b70SRobert Watson * so_rcv to avoid holding the so_rcv lock over the entire 962d7924b70SRobert Watson * transaction on the remote so_snd. 963df8bae1dSRodney W. Grimes */ 964337cc6b6SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 965337cc6b6SRobert Watson mbcnt = so->so_rcv.sb_mbcnt; 9662b21d0e8SGleb Smirnoff sbcc = sbavail(&so->so_rcv); 967337cc6b6SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 968c2090e73SAlan Somers /* 969c2090e73SAlan Somers * There is a benign race condition at this point. If we're planning to 970c2090e73SAlan Somers * clear SB_STOP, but uipc_send is called on the connected socket at 971c2090e73SAlan Somers * this instant, it might add data to the sockbuf and set SB_STOP. Then 972c2090e73SAlan Somers * we would erroneously clear SB_STOP below, even though the sockbuf is 973c2090e73SAlan Somers * full. The race is benign because the only ill effect is to allow the 974c2090e73SAlan Somers * sockbuf to exceed its size limit, and the size limits are not 975c2090e73SAlan Somers * strictly guaranteed anyway. 976c2090e73SAlan Somers */ 977e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 978e7c33e29SRobert Watson unp2 = unp->unp_conn; 979e7c33e29SRobert Watson if (unp2 == NULL) { 980e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 981e7c33e29SRobert Watson return (0); 982337cc6b6SRobert Watson } 983e7c33e29SRobert Watson so2 = unp2->unp_socket; 984337cc6b6SRobert Watson SOCKBUF_LOCK(&so2->so_snd); 985c2090e73SAlan Somers if (sbcc < so2->so_snd.sb_hiwat && mbcnt < so2->so_snd.sb_mbmax) 986c2090e73SAlan Somers so2->so_snd.sb_flags &= ~SB_STOP; 9871e4d7da7SRobert Watson sowwakeup_locked(so2); 988e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 989e5aeaa0cSDag-Erling Smørgrav return (0); 990a29f300eSGarrett Wollman } 991df8bae1dSRodney W. Grimes 992a29f300eSGarrett Wollman static int 993*75a67bf3SMatt Macy connect_internal(struct socket *so, struct sockaddr *nam, struct thread *td) 994*75a67bf3SMatt Macy { 995*75a67bf3SMatt Macy int error; 996*75a67bf3SMatt Macy struct unpcb *unp; 997*75a67bf3SMatt Macy 998*75a67bf3SMatt Macy unp = so->so_pcb; 999*75a67bf3SMatt Macy if (unp->unp_conn != NULL) 1000*75a67bf3SMatt Macy return (EISCONN); 1001*75a67bf3SMatt Macy error = unp_connect(so, nam, td); 1002*75a67bf3SMatt Macy if (error) 1003*75a67bf3SMatt Macy return (error); 1004*75a67bf3SMatt Macy UNP_PCB_LOCK(unp); 1005*75a67bf3SMatt Macy if (unp->unp_conn == NULL) { 1006*75a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 1007*75a67bf3SMatt Macy if (error == 0) 1008*75a67bf3SMatt Macy error = ENOTCONN; 1009*75a67bf3SMatt Macy } 1010*75a67bf3SMatt Macy return (error); 1011*75a67bf3SMatt Macy } 1012*75a67bf3SMatt Macy 1013*75a67bf3SMatt Macy 1014*75a67bf3SMatt Macy static int 101557bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 1016b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 1017a29f300eSGarrett Wollman { 1018f3f49bbbSRobert Watson struct unpcb *unp, *unp2; 1019a29f300eSGarrett Wollman struct socket *so2; 1020c2090e73SAlan Somers u_int mbcnt, sbcc; 1021*75a67bf3SMatt Macy int freed, error; 1022a29f300eSGarrett Wollman 102340f2ac28SRobert Watson unp = sotounpcb(so); 10242b21d0e8SGleb Smirnoff KASSERT(unp != NULL, ("%s: unp == NULL", __func__)); 10252b21d0e8SGleb Smirnoff KASSERT(so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM || 10262b21d0e8SGleb Smirnoff so->so_type == SOCK_SEQPACKET, 10272b21d0e8SGleb Smirnoff ("%s: socktype %d", __func__, so->so_type)); 1028e7c33e29SRobert Watson 1029*75a67bf3SMatt Macy freed = error = 0; 1030a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 1031a29f300eSGarrett Wollman error = EOPNOTSUPP; 1032a29f300eSGarrett Wollman goto release; 1033a29f300eSGarrett Wollman } 1034fc3fcacfSRobert Watson if (control != NULL && (error = unp_internalize(&control, td))) 1035a29f300eSGarrett Wollman goto release; 1036*75a67bf3SMatt Macy 1037*75a67bf3SMatt Macy unp2 = NULL; 1038a29f300eSGarrett Wollman switch (so->so_type) { 1039a29f300eSGarrett Wollman case SOCK_DGRAM: 1040a29f300eSGarrett Wollman { 1041e7dd9a10SRobert Watson const struct sockaddr *from; 1042df8bae1dSRodney W. Grimes 1043fc3fcacfSRobert Watson if (nam != NULL) { 1044*75a67bf3SMatt Macy /* 1045*75a67bf3SMatt Macy * We return with UNP_PCB_LOCK_HELD so we know that 1046*75a67bf3SMatt Macy * the reference is live if the pointer is valid. 1047*75a67bf3SMatt Macy */ 1048*75a67bf3SMatt Macy if ((error = connect_internal(so, nam, td))) 1049df8bae1dSRodney W. Grimes break; 1050*75a67bf3SMatt Macy MPASS(unp->unp_conn != NULL); 1051e7c33e29SRobert Watson unp2 = unp->unp_conn; 1052*75a67bf3SMatt Macy } else { 1053*75a67bf3SMatt Macy UNP_PCB_LOCK(unp); 105460a5ef26SRobert Watson 1055b5ff0914SRobert Watson /* 1056b5ff0914SRobert Watson * Because connect() and send() are non-atomic in a sendto() 1057b5ff0914SRobert Watson * with a target address, it's possible that the socket will 1058b5ff0914SRobert Watson * have disconnected before the send() can run. In that case 1059b5ff0914SRobert Watson * return the slightly counter-intuitive but otherwise 1060b5ff0914SRobert Watson * correct error that the socket is not connected. 1061b5ff0914SRobert Watson */ 1062*75a67bf3SMatt Macy if ((unp2 = unp->unp_conn) == NULL) { 1063*75a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 1064b5ff0914SRobert Watson error = ENOTCONN; 1065b5ff0914SRobert Watson break; 1066b5ff0914SRobert Watson } 1067*75a67bf3SMatt Macy } 1068*75a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freed); 1069*75a67bf3SMatt Macy if (__predict_false(freed)) { 1070*75a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 1071*75a67bf3SMatt Macy error = ENOTCONN; 1072*75a67bf3SMatt Macy break; 1073*75a67bf3SMatt Macy } 1074*75a67bf3SMatt Macy /* 1075*75a67bf3SMatt Macy * The socket referencing unp2 may have been closed 1076*75a67bf3SMatt Macy * or unp may have been disconnected if the unp lock 1077*75a67bf3SMatt Macy * was dropped to acquire unp2. 1078*75a67bf3SMatt Macy */ 1079*75a67bf3SMatt Macy if (__predict_false(unp->unp_conn == NULL) || 1080*75a67bf3SMatt Macy unp2->unp_socket == NULL) { 1081*75a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 1082*75a67bf3SMatt Macy if (unp_pcb_rele(unp2) == 0) 1083*75a67bf3SMatt Macy UNP_PCB_UNLOCK(unp2); 1084*75a67bf3SMatt Macy error = ENOTCONN; 1085*75a67bf3SMatt Macy break; 1086*75a67bf3SMatt Macy } 1087ede6e136SRobert Watson if (unp2->unp_flags & UNP_WANTCRED) 1088ede6e136SRobert Watson control = unp_addsockcred(td, control); 1089fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 109057bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 1091df8bae1dSRodney W. Grimes else 1092df8bae1dSRodney W. Grimes from = &sun_noname; 1093ede6e136SRobert Watson so2 = unp2->unp_socket; 1094a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 10956dde7ecbSPeter Wemm if (sbappendaddr_locked(&so2->so_rcv, from, m, 10968de34a88SAlan Somers control)) { 10971e4d7da7SRobert Watson sorwakeup_locked(so2); 1098fc3fcacfSRobert Watson m = NULL; 1099fc3fcacfSRobert Watson control = NULL; 1100e5aeaa0cSDag-Erling Smørgrav } else { 1101a34b7046SRobert Watson SOCKBUF_UNLOCK(&so2->so_rcv); 1102df8bae1dSRodney W. Grimes error = ENOBUFS; 1103e5aeaa0cSDag-Erling Smørgrav } 1104*75a67bf3SMatt Macy if (nam != NULL) 1105e7c33e29SRobert Watson unp_disconnect(unp, unp2); 1106e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1107ede6e136SRobert Watson UNP_PCB_UNLOCK(unp); 1108df8bae1dSRodney W. Grimes break; 1109df8bae1dSRodney W. Grimes } 1110df8bae1dSRodney W. Grimes 111184d61770SRobert Watson case SOCK_SEQPACKET: 1112df8bae1dSRodney W. Grimes case SOCK_STREAM: 1113402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 1114fc3fcacfSRobert Watson if (nam != NULL) { 1115*75a67bf3SMatt Macy if ((error = connect_internal(so, nam, td))) 1116*75a67bf3SMatt Macy break; 1117402cc72dSDavid Greenman } else { 1118402cc72dSDavid Greenman error = ENOTCONN; 1119402cc72dSDavid Greenman break; 1120402cc72dSDavid Greenman } 1121*75a67bf3SMatt Macy } else if ((unp2 = unp->unp_conn) == NULL) { 1122*75a67bf3SMatt Macy error = ENOTCONN; 1123*75a67bf3SMatt Macy break; 1124*75a67bf3SMatt Macy } else if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 1125df8bae1dSRodney W. Grimes error = EPIPE; 1126df8bae1dSRodney W. Grimes break; 1127*75a67bf3SMatt Macy } else { 1128*75a67bf3SMatt Macy UNP_PCB_LOCK(unp); 1129*75a67bf3SMatt Macy if ((unp2 = unp->unp_conn) == NULL) { 1130*75a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 1131b5ff0914SRobert Watson error = ENOTCONN; 1132b5ff0914SRobert Watson break; 1133b5ff0914SRobert Watson } 1134*75a67bf3SMatt Macy } 1135*75a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freed); 1136*75a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 1137*75a67bf3SMatt Macy if (__predict_false(freed)) { 1138*75a67bf3SMatt Macy error = ENOTCONN; 1139*75a67bf3SMatt Macy break; 1140*75a67bf3SMatt Macy } 1141*75a67bf3SMatt Macy if ((so2 = unp2->unp_socket) == NULL) { 1142*75a67bf3SMatt Macy UNP_PCB_UNLOCK(unp2); 1143*75a67bf3SMatt Macy error = ENOTCONN; 1144*75a67bf3SMatt Macy break; 1145*75a67bf3SMatt Macy } 1146a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 1147f3f49bbbSRobert Watson if (unp2->unp_flags & UNP_WANTCRED) { 11486a2989fdSMatthew N. Dodd /* 1149716963cbSGleb Smirnoff * Credentials are passed only once on SOCK_STREAM 1150716963cbSGleb Smirnoff * and SOCK_SEQPACKET. 11516a2989fdSMatthew N. Dodd */ 1152f3f49bbbSRobert Watson unp2->unp_flags &= ~UNP_WANTCRED; 11536a2989fdSMatthew N. Dodd control = unp_addsockcred(td, control); 11546a2989fdSMatthew N. Dodd } 1155df8bae1dSRodney W. Grimes /* 11561c381b19SRobert Watson * Send to paired receive port, and then reduce send buffer 11571c381b19SRobert Watson * hiwater marks to maintain backpressure. Wake up readers. 1158df8bae1dSRodney W. Grimes */ 115984d61770SRobert Watson switch (so->so_type) { 116084d61770SRobert Watson case SOCK_STREAM: 1161fc3fcacfSRobert Watson if (control != NULL) { 116284d61770SRobert Watson if (sbappendcontrol_locked(&so2->so_rcv, m, 116384d61770SRobert Watson control)) 1164fc3fcacfSRobert Watson control = NULL; 1165e7c33e29SRobert Watson } else 1166829fae90SGleb Smirnoff sbappend_locked(&so2->so_rcv, m, flags); 116784d61770SRobert Watson break; 116884d61770SRobert Watson 116984d61770SRobert Watson case SOCK_SEQPACKET: { 117084d61770SRobert Watson const struct sockaddr *from; 117184d61770SRobert Watson 117284d61770SRobert Watson from = &sun_noname; 11738de34a88SAlan Somers /* 11748de34a88SAlan Somers * Don't check for space available in so2->so_rcv. 11758de34a88SAlan Somers * Unix domain sockets only check for space in the 11768de34a88SAlan Somers * sending sockbuf, and that check is performed one 11778de34a88SAlan Somers * level up the stack. 11788de34a88SAlan Somers */ 11798de34a88SAlan Somers if (sbappendaddr_nospacecheck_locked(&so2->so_rcv, 11808de34a88SAlan Somers from, m, control)) 118184d61770SRobert Watson control = NULL; 118284d61770SRobert Watson break; 118384d61770SRobert Watson } 118484d61770SRobert Watson } 118584d61770SRobert Watson 1186c2090e73SAlan Somers mbcnt = so2->so_rcv.sb_mbcnt; 11872b21d0e8SGleb Smirnoff sbcc = sbavail(&so2->so_rcv); 11882b21d0e8SGleb Smirnoff if (sbcc) 1189337cc6b6SRobert Watson sorwakeup_locked(so2); 11902b21d0e8SGleb Smirnoff else 11912b21d0e8SGleb Smirnoff SOCKBUF_UNLOCK(&so2->so_rcv); 1192337cc6b6SRobert Watson 1193c2090e73SAlan Somers /* 1194c2090e73SAlan Somers * The PCB lock on unp2 protects the SB_STOP flag. Without it, 1195c2090e73SAlan Somers * it would be possible for uipc_rcvd to be called at this 1196c2090e73SAlan Somers * point, drain the receiving sockbuf, clear SB_STOP, and then 1197c2090e73SAlan Somers * we would set SB_STOP below. That could lead to an empty 1198c2090e73SAlan Somers * sockbuf having SB_STOP set 1199c2090e73SAlan Somers */ 1200337cc6b6SRobert Watson SOCKBUF_LOCK(&so->so_snd); 1201c2090e73SAlan Somers if (sbcc >= so->so_snd.sb_hiwat || mbcnt >= so->so_snd.sb_mbmax) 1202c2090e73SAlan Somers so->so_snd.sb_flags |= SB_STOP; 12037abe2ac2SAlan Cox SOCKBUF_UNLOCK(&so->so_snd); 1204e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1205fc3fcacfSRobert Watson m = NULL; 1206df8bae1dSRodney W. Grimes break; 1207df8bae1dSRodney W. Grimes } 1208a29f300eSGarrett Wollman 12096b8fda4dSGarrett Wollman /* 121060a5ef26SRobert Watson * PRUS_EOF is equivalent to pru_send followed by pru_shutdown. 12116b8fda4dSGarrett Wollman */ 1212a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 1213ede6e136SRobert Watson UNP_PCB_LOCK(unp); 12146b8fda4dSGarrett Wollman socantsendmore(so); 12156b8fda4dSGarrett Wollman unp_shutdown(unp); 1216e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1217ede6e136SRobert Watson } 1218fc3fcacfSRobert Watson if (control != NULL && error != 0) 121999ab95dbSMark Johnston unp_dispose_mbuf(control); 1220bd508d39SDon Lewis 1221a29f300eSGarrett Wollman release: 1222fc3fcacfSRobert Watson if (control != NULL) 1223a29f300eSGarrett Wollman m_freem(control); 1224100db364SGleb Smirnoff /* 1225100db364SGleb Smirnoff * In case of PRUS_NOTREADY, uipc_ready() is responsible 1226100db364SGleb Smirnoff * for freeing memory. 1227100db364SGleb Smirnoff */ 1228100db364SGleb Smirnoff if (m != NULL && (flags & PRUS_NOTREADY) == 0) 1229a29f300eSGarrett Wollman m_freem(m); 1230e5aeaa0cSDag-Erling Smørgrav return (error); 1231a29f300eSGarrett Wollman } 1232df8bae1dSRodney W. Grimes 1233a29f300eSGarrett Wollman static int 1234c80ea19bSGleb Smirnoff uipc_ready(struct socket *so, struct mbuf *m, int count) 1235c80ea19bSGleb Smirnoff { 1236c80ea19bSGleb Smirnoff struct unpcb *unp, *unp2; 1237c80ea19bSGleb Smirnoff struct socket *so2; 1238c80ea19bSGleb Smirnoff int error; 1239c80ea19bSGleb Smirnoff 1240c80ea19bSGleb Smirnoff unp = sotounpcb(so); 1241c80ea19bSGleb Smirnoff 1242c80ea19bSGleb Smirnoff UNP_LINK_RLOCK(); 1243100db364SGleb Smirnoff if ((unp2 = unp->unp_conn) == NULL) { 1244100db364SGleb Smirnoff UNP_LINK_RUNLOCK(); 1245100db364SGleb Smirnoff for (int i = 0; i < count; i++) 1246100db364SGleb Smirnoff m = m_free(m); 1247100db364SGleb Smirnoff return (ECONNRESET); 1248100db364SGleb Smirnoff } 1249c80ea19bSGleb Smirnoff UNP_PCB_LOCK(unp2); 1250c80ea19bSGleb Smirnoff so2 = unp2->unp_socket; 1251c80ea19bSGleb Smirnoff 1252c80ea19bSGleb Smirnoff SOCKBUF_LOCK(&so2->so_rcv); 1253c80ea19bSGleb Smirnoff if ((error = sbready(&so2->so_rcv, m, count)) == 0) 1254c80ea19bSGleb Smirnoff sorwakeup_locked(so2); 1255c80ea19bSGleb Smirnoff else 1256c80ea19bSGleb Smirnoff SOCKBUF_UNLOCK(&so2->so_rcv); 1257c80ea19bSGleb Smirnoff 1258c80ea19bSGleb Smirnoff UNP_PCB_UNLOCK(unp2); 1259c80ea19bSGleb Smirnoff UNP_LINK_RUNLOCK(); 1260c80ea19bSGleb Smirnoff 1261c80ea19bSGleb Smirnoff return (error); 1262c80ea19bSGleb Smirnoff } 1263c80ea19bSGleb Smirnoff 1264c80ea19bSGleb Smirnoff static int 1265a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 1266a29f300eSGarrett Wollman { 1267c2090e73SAlan Somers struct unpcb *unp; 1268a29f300eSGarrett Wollman 126940f2ac28SRobert Watson unp = sotounpcb(so); 12704d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sense: unp == NULL")); 1271e7c33e29SRobert Watson 1272a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 1273e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 1274f3732fd1SPoul-Henning Kamp sb->st_dev = NODEV; 1275df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 12766f782c46SJeffrey Hsu unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 1277a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 1278e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1279df8bae1dSRodney W. Grimes return (0); 1280a29f300eSGarrett Wollman } 1281df8bae1dSRodney W. Grimes 1282a29f300eSGarrett Wollman static int 1283a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 1284a29f300eSGarrett Wollman { 128540f2ac28SRobert Watson struct unpcb *unp; 1286df8bae1dSRodney W. Grimes 128740f2ac28SRobert Watson unp = sotounpcb(so); 12884d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_shutdown: unp == NULL")); 1289e7c33e29SRobert Watson 1290e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 1291a29f300eSGarrett Wollman socantsendmore(so); 1292a29f300eSGarrett Wollman unp_shutdown(unp); 1293e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1294e5aeaa0cSDag-Erling Smørgrav return (0); 1295a29f300eSGarrett Wollman } 1296df8bae1dSRodney W. Grimes 1297a29f300eSGarrett Wollman static int 129857bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 1299a29f300eSGarrett Wollman { 130040f2ac28SRobert Watson struct unpcb *unp; 13010d9ce3a1SRobert Watson const struct sockaddr *sa; 1302a29f300eSGarrett Wollman 13034d4b555eSRobert Watson unp = sotounpcb(so); 13044d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL")); 1305e7c33e29SRobert Watson 13060d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 1307e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 1308fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 13090d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_addr; 131083f3198bSThomas Moestl else 13110d9ce3a1SRobert Watson sa = &sun_noname; 13120d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 1313e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1314e5aeaa0cSDag-Erling Smørgrav return (0); 1315df8bae1dSRodney W. Grimes } 1316a29f300eSGarrett Wollman 1317fa9402f2SRobert Watson static struct pr_usrreqs uipc_usrreqs_dgram = { 1318756d52a1SPoul-Henning Kamp .pru_abort = uipc_abort, 1319756d52a1SPoul-Henning Kamp .pru_accept = uipc_accept, 1320756d52a1SPoul-Henning Kamp .pru_attach = uipc_attach, 1321756d52a1SPoul-Henning Kamp .pru_bind = uipc_bind, 13227493f24eSPawel Jakub Dawidek .pru_bindat = uipc_bindat, 1323756d52a1SPoul-Henning Kamp .pru_connect = uipc_connect, 13247493f24eSPawel Jakub Dawidek .pru_connectat = uipc_connectat, 1325756d52a1SPoul-Henning Kamp .pru_connect2 = uipc_connect2, 1326756d52a1SPoul-Henning Kamp .pru_detach = uipc_detach, 1327756d52a1SPoul-Henning Kamp .pru_disconnect = uipc_disconnect, 1328756d52a1SPoul-Henning Kamp .pru_listen = uipc_listen, 1329756d52a1SPoul-Henning Kamp .pru_peeraddr = uipc_peeraddr, 1330756d52a1SPoul-Henning Kamp .pru_rcvd = uipc_rcvd, 1331756d52a1SPoul-Henning Kamp .pru_send = uipc_send, 1332756d52a1SPoul-Henning Kamp .pru_sense = uipc_sense, 1333756d52a1SPoul-Henning Kamp .pru_shutdown = uipc_shutdown, 1334756d52a1SPoul-Henning Kamp .pru_sockaddr = uipc_sockaddr, 1335fa9402f2SRobert Watson .pru_soreceive = soreceive_dgram, 1336fa9402f2SRobert Watson .pru_close = uipc_close, 1337fa9402f2SRobert Watson }; 1338fa9402f2SRobert Watson 133984d61770SRobert Watson static struct pr_usrreqs uipc_usrreqs_seqpacket = { 134084d61770SRobert Watson .pru_abort = uipc_abort, 134184d61770SRobert Watson .pru_accept = uipc_accept, 134284d61770SRobert Watson .pru_attach = uipc_attach, 134384d61770SRobert Watson .pru_bind = uipc_bind, 13447493f24eSPawel Jakub Dawidek .pru_bindat = uipc_bindat, 134584d61770SRobert Watson .pru_connect = uipc_connect, 13467493f24eSPawel Jakub Dawidek .pru_connectat = uipc_connectat, 134784d61770SRobert Watson .pru_connect2 = uipc_connect2, 134884d61770SRobert Watson .pru_detach = uipc_detach, 134984d61770SRobert Watson .pru_disconnect = uipc_disconnect, 135084d61770SRobert Watson .pru_listen = uipc_listen, 135184d61770SRobert Watson .pru_peeraddr = uipc_peeraddr, 135284d61770SRobert Watson .pru_rcvd = uipc_rcvd, 135384d61770SRobert Watson .pru_send = uipc_send, 135484d61770SRobert Watson .pru_sense = uipc_sense, 135584d61770SRobert Watson .pru_shutdown = uipc_shutdown, 135684d61770SRobert Watson .pru_sockaddr = uipc_sockaddr, 135784d61770SRobert Watson .pru_soreceive = soreceive_generic, /* XXX: or...? */ 135884d61770SRobert Watson .pru_close = uipc_close, 135984d61770SRobert Watson }; 136084d61770SRobert Watson 1361fa9402f2SRobert Watson static struct pr_usrreqs uipc_usrreqs_stream = { 1362fa9402f2SRobert Watson .pru_abort = uipc_abort, 1363fa9402f2SRobert Watson .pru_accept = uipc_accept, 1364fa9402f2SRobert Watson .pru_attach = uipc_attach, 1365fa9402f2SRobert Watson .pru_bind = uipc_bind, 13667493f24eSPawel Jakub Dawidek .pru_bindat = uipc_bindat, 1367fa9402f2SRobert Watson .pru_connect = uipc_connect, 13687493f24eSPawel Jakub Dawidek .pru_connectat = uipc_connectat, 1369fa9402f2SRobert Watson .pru_connect2 = uipc_connect2, 1370fa9402f2SRobert Watson .pru_detach = uipc_detach, 1371fa9402f2SRobert Watson .pru_disconnect = uipc_disconnect, 1372fa9402f2SRobert Watson .pru_listen = uipc_listen, 1373fa9402f2SRobert Watson .pru_peeraddr = uipc_peeraddr, 1374fa9402f2SRobert Watson .pru_rcvd = uipc_rcvd, 1375fa9402f2SRobert Watson .pru_send = uipc_send, 1376c80ea19bSGleb Smirnoff .pru_ready = uipc_ready, 1377fa9402f2SRobert Watson .pru_sense = uipc_sense, 1378fa9402f2SRobert Watson .pru_shutdown = uipc_shutdown, 1379fa9402f2SRobert Watson .pru_sockaddr = uipc_sockaddr, 1380fa9402f2SRobert Watson .pru_soreceive = soreceive_generic, 1381a152f8a3SRobert Watson .pru_close = uipc_close, 1382a29f300eSGarrett Wollman }; 1383df8bae1dSRodney W. Grimes 13840b36cd25SRobert Watson static int 1385892af6b9SRobert Watson uipc_ctloutput(struct socket *so, struct sockopt *sopt) 13860c1bb4fbSDima Dorfman { 138740f2ac28SRobert Watson struct unpcb *unp; 13880d9ce3a1SRobert Watson struct xucred xu; 13896a2989fdSMatthew N. Dodd int error, optval; 13906a2989fdSMatthew N. Dodd 139196a041b5SMatthew N. Dodd if (sopt->sopt_level != 0) 139296a041b5SMatthew N. Dodd return (EINVAL); 139396a041b5SMatthew N. Dodd 13946a2989fdSMatthew N. Dodd unp = sotounpcb(so); 13954d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL")); 13966a2989fdSMatthew N. Dodd error = 0; 13970c1bb4fbSDima Dorfman switch (sopt->sopt_dir) { 13980c1bb4fbSDima Dorfman case SOPT_GET: 13990c1bb4fbSDima Dorfman switch (sopt->sopt_name) { 14000c1bb4fbSDima Dorfman case LOCAL_PEERCRED: 1401e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 14020c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC) 14030d9ce3a1SRobert Watson xu = unp->unp_peercred; 14040c1bb4fbSDima Dorfman else { 14050c1bb4fbSDima Dorfman if (so->so_type == SOCK_STREAM) 14060c1bb4fbSDima Dorfman error = ENOTCONN; 14070c1bb4fbSDima Dorfman else 14080c1bb4fbSDima Dorfman error = EINVAL; 14090c1bb4fbSDima Dorfman } 1410e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 14110d9ce3a1SRobert Watson if (error == 0) 14120d9ce3a1SRobert Watson error = sooptcopyout(sopt, &xu, sizeof(xu)); 14130c1bb4fbSDima Dorfman break; 1414e7c33e29SRobert Watson 14156a2989fdSMatthew N. Dodd case LOCAL_CREDS: 1416a6357845SRobert Watson /* Unlocked read. */ 14176a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0; 14186a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 14196a2989fdSMatthew N. Dodd break; 1420e7c33e29SRobert Watson 14216a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 1422a6357845SRobert Watson /* Unlocked read. */ 14236a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0; 14246a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 14256a2989fdSMatthew N. Dodd break; 1426e7c33e29SRobert Watson 14270c1bb4fbSDima Dorfman default: 14280c1bb4fbSDima Dorfman error = EOPNOTSUPP; 14290c1bb4fbSDima Dorfman break; 14300c1bb4fbSDima Dorfman } 14310c1bb4fbSDima Dorfman break; 1432e7c33e29SRobert Watson 14330c1bb4fbSDima Dorfman case SOPT_SET: 14346a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 14356a2989fdSMatthew N. Dodd case LOCAL_CREDS: 14366a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 14376a2989fdSMatthew N. Dodd error = sooptcopyin(sopt, &optval, sizeof(optval), 14386a2989fdSMatthew N. Dodd sizeof(optval)); 14396a2989fdSMatthew N. Dodd if (error) 14406a2989fdSMatthew N. Dodd break; 14416a2989fdSMatthew N. Dodd 1442e7c33e29SRobert Watson #define OPTSET(bit) do { \ 1443e7c33e29SRobert Watson UNP_PCB_LOCK(unp); \ 14446a2989fdSMatthew N. Dodd if (optval) \ 14456a2989fdSMatthew N. Dodd unp->unp_flags |= bit; \ 14466a2989fdSMatthew N. Dodd else \ 1447e7c33e29SRobert Watson unp->unp_flags &= ~bit; \ 1448e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); \ 1449e7c33e29SRobert Watson } while (0) 14506a2989fdSMatthew N. Dodd 14516a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 14526a2989fdSMatthew N. Dodd case LOCAL_CREDS: 14536a2989fdSMatthew N. Dodd OPTSET(UNP_WANTCRED); 14546a2989fdSMatthew N. Dodd break; 1455e7c33e29SRobert Watson 14566a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 14576a2989fdSMatthew N. Dodd OPTSET(UNP_CONNWAIT); 14586a2989fdSMatthew N. Dodd break; 1459e7c33e29SRobert Watson 14606a2989fdSMatthew N. Dodd default: 14616a2989fdSMatthew N. Dodd break; 14626a2989fdSMatthew N. Dodd } 14636a2989fdSMatthew N. Dodd break; 14646a2989fdSMatthew N. Dodd #undef OPTSET 14656a2989fdSMatthew N. Dodd default: 14666a2989fdSMatthew N. Dodd error = ENOPROTOOPT; 14676a2989fdSMatthew N. Dodd break; 14686a2989fdSMatthew N. Dodd } 1469abb886faSMatthew N. Dodd break; 1470e7c33e29SRobert Watson 14710c1bb4fbSDima Dorfman default: 14720c1bb4fbSDima Dorfman error = EOPNOTSUPP; 14730c1bb4fbSDima Dorfman break; 14740c1bb4fbSDima Dorfman } 14750c1bb4fbSDima Dorfman return (error); 14760c1bb4fbSDima Dorfman } 14770c1bb4fbSDima Dorfman 1478f708ef1bSPoul-Henning Kamp static int 1479892af6b9SRobert Watson unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1480df8bae1dSRodney W. Grimes { 14817493f24eSPawel Jakub Dawidek 14827493f24eSPawel Jakub Dawidek return (unp_connectat(AT_FDCWD, so, nam, td)); 14837493f24eSPawel Jakub Dawidek } 14847493f24eSPawel Jakub Dawidek 14857493f24eSPawel Jakub Dawidek static int 14867493f24eSPawel Jakub Dawidek unp_connectat(int fd, struct socket *so, struct sockaddr *nam, 14877493f24eSPawel Jakub Dawidek struct thread *td) 14887493f24eSPawel Jakub Dawidek { 1489892af6b9SRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam; 1490892af6b9SRobert Watson struct vnode *vp; 1491779f106aSGleb Smirnoff struct socket *so2; 1492b295bdcdSRobert Watson struct unpcb *unp, *unp2, *unp3; 1493df8bae1dSRodney W. Grimes struct nameidata nd; 149457bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 14950d9ce3a1SRobert Watson struct sockaddr *sa; 14967008be5bSPawel Jakub Dawidek cap_rights_t rights; 1497*75a67bf3SMatt Macy int error, len, freed; 1498*75a67bf3SMatt Macy struct mtx *vplock; 14990d9ce3a1SRobert Watson 1500cb7df69bSKevin Lo if (nam->sa_family != AF_UNIX) 1501cb7df69bSKevin Lo return (EAFNOSUPPORT); 1502a06534c3SBjoern A. Zeeb if (nam->sa_len > sizeof(struct sockaddr_un)) 1503a06534c3SBjoern A. Zeeb return (EINVAL); 150457bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 150557bf258eSGarrett Wollman if (len <= 0) 1506e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 15077928893dSEd Maste bcopy(soun->sun_path, buf, len); 15087928893dSEd Maste buf[len] = 0; 1509e7c33e29SRobert Watson 1510*75a67bf3SMatt Macy unp = sotounpcb(so); 1511e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 15124f1f0ef5SRobert Watson if (unp->unp_flags & UNP_CONNECTING) { 1513e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 15144f1f0ef5SRobert Watson return (EALREADY); 15154f1f0ef5SRobert Watson } 151605102f04SRobert Watson unp->unp_flags |= UNP_CONNECTING; 1517e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1518e7c33e29SRobert Watson 15190d9ce3a1SRobert Watson sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 15207493f24eSPawel Jakub Dawidek NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, 15217008be5bSPawel Jakub Dawidek UIO_SYSSPACE, buf, fd, cap_rights_init(&rights, CAP_CONNECTAT), td); 1522797f2d22SPoul-Henning Kamp error = namei(&nd); 1523797f2d22SPoul-Henning Kamp if (error) 15240d9ce3a1SRobert Watson vp = NULL; 15250d9ce3a1SRobert Watson else 1526df8bae1dSRodney W. Grimes vp = nd.ni_vp; 15270d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_connect"); 1528762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 15290d9ce3a1SRobert Watson if (error) 15300d9ce3a1SRobert Watson goto bad; 15310d9ce3a1SRobert Watson 1532df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 1533df8bae1dSRodney W. Grimes error = ENOTSOCK; 1534df8bae1dSRodney W. Grimes goto bad; 1535df8bae1dSRodney W. Grimes } 15366fac927cSRobert Watson #ifdef MAC 153730d239bcSRobert Watson error = mac_vnode_check_open(td->td_ucred, vp, VWRITE | VREAD); 15386fac927cSRobert Watson if (error) 15396fac927cSRobert Watson goto bad; 15406fac927cSRobert Watson #endif 1541a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 1542797f2d22SPoul-Henning Kamp if (error) 1543df8bae1dSRodney W. Grimes goto bad; 1544e7c33e29SRobert Watson 1545b295bdcdSRobert Watson unp = sotounpcb(so); 15464d4b555eSRobert Watson KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 1547e7c33e29SRobert Watson 1548*75a67bf3SMatt Macy vplock = mtx_pool_find(mtxpool_sleep, vp); 1549*75a67bf3SMatt Macy mtx_lock(vplock); 15500c3c207fSGleb Smirnoff VOP_UNP_CONNECT(vp, &unp2); 15510c3c207fSGleb Smirnoff if (unp2 == NULL) { 1552df8bae1dSRodney W. Grimes error = ECONNREFUSED; 15532260c03dSRobert Watson goto bad2; 1554df8bae1dSRodney W. Grimes } 15550c3c207fSGleb Smirnoff so2 = unp2->unp_socket; 1556df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 1557df8bae1dSRodney W. Grimes error = EPROTOTYPE; 15582260c03dSRobert Watson goto bad2; 1559df8bae1dSRodney W. Grimes } 1560*75a67bf3SMatt Macy unp_pcb_lock2(unp, unp2); 1561df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 1562e7c33e29SRobert Watson if (so2->so_options & SO_ACCEPTCONN) { 15631fb51a12SBjoern A. Zeeb CURVNET_SET(so2->so_vnet); 1564779f106aSGleb Smirnoff so2 = sonewconn(so2, 0); 15651fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 1566e7c33e29SRobert Watson } else 1567779f106aSGleb Smirnoff so2 = NULL; 1568779f106aSGleb Smirnoff if (so2 == NULL) { 1569df8bae1dSRodney W. Grimes error = ECONNREFUSED; 1570779f106aSGleb Smirnoff goto bad3; 1571df8bae1dSRodney W. Grimes } 1572779f106aSGleb Smirnoff unp3 = sotounpcb(so2); 1573*75a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 1574*75a67bf3SMatt Macy unp_pcb_owned_lock2(unp2, unp3, freed); 1575*75a67bf3SMatt Macy MPASS(!freed); 15760d9ce3a1SRobert Watson if (unp2->unp_addr != NULL) { 15770d9ce3a1SRobert Watson bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 15780d9ce3a1SRobert Watson unp3->unp_addr = (struct sockaddr_un *) sa; 15790d9ce3a1SRobert Watson sa = NULL; 15800d9ce3a1SRobert Watson } 1581b523ec24SRobert Watson 15820c1bb4fbSDima Dorfman /* 15837a2b450fSEitan Adler * The connector's (client's) credentials are copied from its 15841c381b19SRobert Watson * process structure at the time of connect() (which is now). 15850c1bb4fbSDima Dorfman */ 1586a854ed98SJohn Baldwin cru2x(td->td_ucred, &unp3->unp_peercred); 15870c1bb4fbSDima Dorfman unp3->unp_flags |= UNP_HAVEPC; 1588b523ec24SRobert Watson 15890c1bb4fbSDima Dorfman /* 15901c381b19SRobert Watson * The receiver's (server's) credentials are copied from the 15911c381b19SRobert Watson * unp_peercred member of socket on which the former called 1592e7c33e29SRobert Watson * listen(); uipc_listen() cached that process's credentials 15931c381b19SRobert Watson * at that time so we can use them now. 15940c1bb4fbSDima Dorfman */ 15950c1bb4fbSDima Dorfman memcpy(&unp->unp_peercred, &unp2->unp_peercred, 15960c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 15970c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPC; 1598481f8fe8SMaxim Konovalov if (unp2->unp_flags & UNP_WANTCRED) 1599481f8fe8SMaxim Konovalov unp3->unp_flags |= UNP_WANTCRED; 1600e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1601779f106aSGleb Smirnoff unp2 = unp3; 1602*75a67bf3SMatt Macy unp_pcb_owned_lock2(unp2, unp, freed); 1603*75a67bf3SMatt Macy MPASS(!freed); 1604335654d7SRobert Watson #ifdef MAC 1605779f106aSGleb Smirnoff mac_socketpeer_set_from_socket(so, so2); 1606779f106aSGleb Smirnoff mac_socketpeer_set_from_socket(so2, so); 1607335654d7SRobert Watson #endif 1608df8bae1dSRodney W. Grimes } 1609779f106aSGleb Smirnoff 1610779f106aSGleb Smirnoff KASSERT(unp2 != NULL && so2 != NULL && unp2->unp_socket == so2 && 1611779f106aSGleb Smirnoff sotounpcb(so2) == unp2, 1612779f106aSGleb Smirnoff ("%s: unp2 %p so2 %p", __func__, unp2, so2)); 16136a2989fdSMatthew N. Dodd error = unp_connect2(so, so2, PRU_CONNECT); 1614779f106aSGleb Smirnoff bad3: 1615e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1616e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 16170d9ce3a1SRobert Watson bad2: 1618*75a67bf3SMatt Macy mtx_unlock(vplock); 1619df8bae1dSRodney W. Grimes bad: 1620*75a67bf3SMatt Macy if (vp != NULL) { 1621df8bae1dSRodney W. Grimes vput(vp); 1622*75a67bf3SMatt Macy } 16230d9ce3a1SRobert Watson free(sa, M_SONAME); 1624e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 16254f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_CONNECTING; 1626e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1627df8bae1dSRodney W. Grimes return (error); 1628df8bae1dSRodney W. Grimes } 1629df8bae1dSRodney W. Grimes 1630db48c0d2SRobert Watson static int 16316a2989fdSMatthew N. Dodd unp_connect2(struct socket *so, struct socket *so2, int req) 1632df8bae1dSRodney W. Grimes { 1633e7c33e29SRobert Watson struct unpcb *unp; 1634892af6b9SRobert Watson struct unpcb *unp2; 1635df8bae1dSRodney W. Grimes 1636e7c33e29SRobert Watson unp = sotounpcb(so); 1637e7c33e29SRobert Watson KASSERT(unp != NULL, ("unp_connect2: unp == NULL")); 1638e7c33e29SRobert Watson unp2 = sotounpcb(so2); 1639e7c33e29SRobert Watson KASSERT(unp2 != NULL, ("unp_connect2: unp2 == NULL")); 1640e7c33e29SRobert Watson 1641e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 1642e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp2); 16430d9ce3a1SRobert Watson 1644df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 1645df8bae1dSRodney W. Grimes return (EPROTOTYPE); 1646434ac8b6SMark Johnston unp2->unp_flags &= ~UNP_NASCENT; 1647df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 1648*75a67bf3SMatt Macy unp_pcb_hold(unp2); 1649*75a67bf3SMatt Macy unp_pcb_hold(unp); 1650df8bae1dSRodney W. Grimes switch (so->so_type) { 1651df8bae1dSRodney W. Grimes case SOCK_DGRAM: 1652*75a67bf3SMatt Macy UNP_REF_LIST_LOCK(); 165398271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 1654*75a67bf3SMatt Macy UNP_REF_LIST_UNLOCK(); 1655df8bae1dSRodney W. Grimes soisconnected(so); 1656df8bae1dSRodney W. Grimes break; 1657df8bae1dSRodney W. Grimes 1658df8bae1dSRodney W. Grimes case SOCK_STREAM: 165984d61770SRobert Watson case SOCK_SEQPACKET: 1660df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 16616a2989fdSMatthew N. Dodd if (req == PRU_CONNECT && 16626a2989fdSMatthew N. Dodd ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT)) 16636a2989fdSMatthew N. Dodd soisconnecting(so); 16646a2989fdSMatthew N. Dodd else 1665df8bae1dSRodney W. Grimes soisconnected(so); 1666df8bae1dSRodney W. Grimes soisconnected(so2); 1667df8bae1dSRodney W. Grimes break; 1668df8bae1dSRodney W. Grimes 1669df8bae1dSRodney W. Grimes default: 1670df8bae1dSRodney W. Grimes panic("unp_connect2"); 1671df8bae1dSRodney W. Grimes } 1672df8bae1dSRodney W. Grimes return (0); 1673df8bae1dSRodney W. Grimes } 1674df8bae1dSRodney W. Grimes 1675f708ef1bSPoul-Henning Kamp static void 1676e7c33e29SRobert Watson unp_disconnect(struct unpcb *unp, struct unpcb *unp2) 1677df8bae1dSRodney W. Grimes { 1678*75a67bf3SMatt Macy struct socket *so, *so2; 1679*75a67bf3SMatt Macy int rele, freed; 1680df8bae1dSRodney W. Grimes 1681e7c33e29SRobert Watson KASSERT(unp2 != NULL, ("unp_disconnect: unp2 == NULL")); 16820d9ce3a1SRobert Watson 1683e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 1684e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp2); 1685e7c33e29SRobert Watson 1686*75a67bf3SMatt Macy if (unp->unp_conn == NULL && unp2->unp_conn == NULL) 1687*75a67bf3SMatt Macy return; 1688*75a67bf3SMatt Macy 1689*75a67bf3SMatt Macy MPASS(unp->unp_conn == unp2); 1690fc3fcacfSRobert Watson unp->unp_conn = NULL; 1691*75a67bf3SMatt Macy rele = 0; 1692*75a67bf3SMatt Macy so = unp->unp_socket; 1693*75a67bf3SMatt Macy so2 = unp2->unp_socket; 1694df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 1695df8bae1dSRodney W. Grimes case SOCK_DGRAM: 1696*75a67bf3SMatt Macy UNP_REF_LIST_LOCK(); 169798271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 1698*75a67bf3SMatt Macy UNP_REF_LIST_UNLOCK(); 1699*75a67bf3SMatt Macy if (so) { 17001b2e3b4bSRobert Watson SOCK_LOCK(so); 17011b2e3b4bSRobert Watson so->so_state &= ~SS_ISCONNECTED; 17021b2e3b4bSRobert Watson SOCK_UNLOCK(so); 1703*75a67bf3SMatt Macy } 1704df8bae1dSRodney W. Grimes break; 1705df8bae1dSRodney W. Grimes 1706df8bae1dSRodney W. Grimes case SOCK_STREAM: 170784d61770SRobert Watson case SOCK_SEQPACKET: 1708*75a67bf3SMatt Macy if (so) 1709*75a67bf3SMatt Macy soisdisconnected(so); 1710*75a67bf3SMatt Macy MPASS(unp2->unp_conn == unp); 1711fc3fcacfSRobert Watson unp2->unp_conn = NULL; 1712*75a67bf3SMatt Macy if (so2) 1713*75a67bf3SMatt Macy soisdisconnected(so2); 1714df8bae1dSRodney W. Grimes break; 1715df8bae1dSRodney W. Grimes } 1716*75a67bf3SMatt Macy freed = unp_pcb_rele(unp); 1717*75a67bf3SMatt Macy MPASS(freed == 0); 1718*75a67bf3SMatt Macy freed = unp_pcb_rele(unp2); 1719*75a67bf3SMatt Macy MPASS(freed == 0); 1720df8bae1dSRodney W. Grimes } 1721df8bae1dSRodney W. Grimes 17220d9ce3a1SRobert Watson /* 1723d7924b70SRobert Watson * unp_pcblist() walks the global list of struct unpcb's to generate a 1724d7924b70SRobert Watson * pointer list, bumping the refcount on each unpcb. It then copies them out 1725d7924b70SRobert Watson * sequentially, validating the generation number on each to see if it has 1726d7924b70SRobert Watson * been detached. All of this is necessary because copyout() may sleep on 1727d7924b70SRobert Watson * disk I/O. 17280d9ce3a1SRobert Watson */ 172998271db4SGarrett Wollman static int 173082d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 173198271db4SGarrett Wollman { 173298271db4SGarrett Wollman struct unpcb *unp, **unp_list; 173398271db4SGarrett Wollman unp_gen_t gencnt; 17348f364875SJulian Elischer struct xunpgen *xug; 173598271db4SGarrett Wollman struct unp_head *head; 17368f364875SJulian Elischer struct xunpcb *xu; 1737d821d364SPedro F. Giffuni u_int i; 173844c514b1SPedro F. Giffuni int error, freeunp, n; 173998271db4SGarrett Wollman 174084d61770SRobert Watson switch ((intptr_t)arg1) { 174184d61770SRobert Watson case SOCK_STREAM: 174284d61770SRobert Watson head = &unp_shead; 174384d61770SRobert Watson break; 174484d61770SRobert Watson 174584d61770SRobert Watson case SOCK_DGRAM: 174684d61770SRobert Watson head = &unp_dhead; 174784d61770SRobert Watson break; 174884d61770SRobert Watson 174984d61770SRobert Watson case SOCK_SEQPACKET: 175084d61770SRobert Watson head = &unp_sphead; 175184d61770SRobert Watson break; 175284d61770SRobert Watson 175384d61770SRobert Watson default: 1754604f19c9SRobert Watson panic("unp_pcblist: arg1 %d", (int)(intptr_t)arg1); 175584d61770SRobert Watson } 175698271db4SGarrett Wollman 175798271db4SGarrett Wollman /* 175898271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 175998271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 176098271db4SGarrett Wollman */ 1761fc3fcacfSRobert Watson if (req->oldptr == NULL) { 176298271db4SGarrett Wollman n = unp_count; 17638f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug) 176498271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 1765e5aeaa0cSDag-Erling Smørgrav return (0); 176698271db4SGarrett Wollman } 176798271db4SGarrett Wollman 1768fc3fcacfSRobert Watson if (req->newptr != NULL) 1769e5aeaa0cSDag-Erling Smørgrav return (EPERM); 177098271db4SGarrett Wollman 177198271db4SGarrett Wollman /* 177298271db4SGarrett Wollman * OK, now we're committed to doing something. 177398271db4SGarrett Wollman */ 1774a163d034SWarner Losh xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 1775779f106aSGleb Smirnoff UNP_LINK_RLOCK(); 177698271db4SGarrett Wollman gencnt = unp_gencnt; 177798271db4SGarrett Wollman n = unp_count; 1778779f106aSGleb Smirnoff UNP_LINK_RUNLOCK(); 177998271db4SGarrett Wollman 17808f364875SJulian Elischer xug->xug_len = sizeof *xug; 17818f364875SJulian Elischer xug->xug_count = n; 17828f364875SJulian Elischer xug->xug_gen = gencnt; 17838f364875SJulian Elischer xug->xug_sogen = so_gencnt; 17848f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 17858f364875SJulian Elischer if (error) { 17868f364875SJulian Elischer free(xug, M_TEMP); 1787e5aeaa0cSDag-Erling Smørgrav return (error); 17888f364875SJulian Elischer } 178998271db4SGarrett Wollman 1790a163d034SWarner Losh unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 179198271db4SGarrett Wollman 1792779f106aSGleb Smirnoff UNP_LINK_RLOCK(); 17932e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 17942e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 1795e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 17968a7d8cc6SRobert Watson if (unp->unp_gencnt <= gencnt) { 1797a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred, 1798e7c33e29SRobert Watson unp->unp_socket->so_cred)) { 1799e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 18004787fd37SPaul Saab continue; 1801e7c33e29SRobert Watson } 180298271db4SGarrett Wollman unp_list[i++] = unp; 1803*75a67bf3SMatt Macy unp_pcb_hold(unp); 180498271db4SGarrett Wollman } 1805e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 18064787fd37SPaul Saab } 1807779f106aSGleb Smirnoff UNP_LINK_RUNLOCK(); 18081c381b19SRobert Watson n = i; /* In case we lost some during malloc. */ 180998271db4SGarrett Wollman 181098271db4SGarrett Wollman error = 0; 1811fe2eee82SColin Percival xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO); 181298271db4SGarrett Wollman for (i = 0; i < n; i++) { 181398271db4SGarrett Wollman unp = unp_list[i]; 1814e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 1815*75a67bf3SMatt Macy freeunp = unp_pcb_rele(unp); 1816*75a67bf3SMatt Macy 1817*75a67bf3SMatt Macy if (freeunp == 0 && unp->unp_gencnt <= gencnt) { 18188f364875SJulian Elischer xu->xu_len = sizeof *xu; 18198f364875SJulian Elischer xu->xu_unpp = unp; 182098271db4SGarrett Wollman /* 182198271db4SGarrett Wollman * XXX - need more locking here to protect against 182298271db4SGarrett Wollman * connect/disconnect races for SMP. 182398271db4SGarrett Wollman */ 1824fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 18258f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr, 182698271db4SGarrett Wollman unp->unp_addr->sun_len); 18270e229f34SGleb Smirnoff else 18280e229f34SGleb Smirnoff bzero(&xu->xu_addr, sizeof(xu->xu_addr)); 1829fc3fcacfSRobert Watson if (unp->unp_conn != NULL && 1830fc3fcacfSRobert Watson unp->unp_conn->unp_addr != NULL) 183198271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 18328f364875SJulian Elischer &xu->xu_caddr, 183398271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 18340e229f34SGleb Smirnoff else 18350e229f34SGleb Smirnoff bzero(&xu->xu_caddr, sizeof(xu->xu_caddr)); 18360e229f34SGleb Smirnoff xu->unp_vnode = unp->unp_vnode; 18370e229f34SGleb Smirnoff xu->unp_conn = unp->unp_conn; 18380e229f34SGleb Smirnoff xu->xu_firstref = LIST_FIRST(&unp->unp_refs); 18390e229f34SGleb Smirnoff xu->xu_nextref = LIST_NEXT(unp, unp_reflink); 18400e229f34SGleb Smirnoff xu->unp_gencnt = unp->unp_gencnt; 18418f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket); 1842e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 18438f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu); 1844*75a67bf3SMatt Macy } else if (freeunp == 0) 1845e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1846e7c33e29SRobert Watson } 18478f364875SJulian Elischer free(xu, M_TEMP); 184898271db4SGarrett Wollman if (!error) { 184998271db4SGarrett Wollman /* 18501c381b19SRobert Watson * Give the user an updated idea of our state. If the 18511c381b19SRobert Watson * generation differs from what we told her before, she knows 18521c381b19SRobert Watson * that something happened while we were processing this 18531c381b19SRobert Watson * request, and it might be necessary to retry. 185498271db4SGarrett Wollman */ 18558f364875SJulian Elischer xug->xug_gen = unp_gencnt; 18568f364875SJulian Elischer xug->xug_sogen = so_gencnt; 18578f364875SJulian Elischer xug->xug_count = unp_count; 18588f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 185998271db4SGarrett Wollman } 186098271db4SGarrett Wollman free(unp_list, M_TEMP); 18618f364875SJulian Elischer free(xug, M_TEMP); 1862e5aeaa0cSDag-Erling Smørgrav return (error); 186398271db4SGarrett Wollman } 186498271db4SGarrett Wollman 18652fee06f0SMatthew D Fleming SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLTYPE_OPAQUE | CTLFLAG_RD, 18662fee06f0SMatthew D Fleming (void *)(intptr_t)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 186798271db4SGarrett Wollman "List of active local datagram sockets"); 18682fee06f0SMatthew D Fleming SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLTYPE_OPAQUE | CTLFLAG_RD, 18692fee06f0SMatthew D Fleming (void *)(intptr_t)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 187098271db4SGarrett Wollman "List of active local stream sockets"); 18712fee06f0SMatthew D Fleming SYSCTL_PROC(_net_local_seqpacket, OID_AUTO, pcblist, 18722fee06f0SMatthew D Fleming CTLTYPE_OPAQUE | CTLFLAG_RD, 18732fee06f0SMatthew D Fleming (void *)(intptr_t)SOCK_SEQPACKET, 0, unp_pcblist, "S,xunpcb", 187484d61770SRobert Watson "List of active local seqpacket sockets"); 187598271db4SGarrett Wollman 1876f708ef1bSPoul-Henning Kamp static void 1877892af6b9SRobert Watson unp_shutdown(struct unpcb *unp) 1878df8bae1dSRodney W. Grimes { 1879e7c33e29SRobert Watson struct unpcb *unp2; 1880df8bae1dSRodney W. Grimes struct socket *so; 1881df8bae1dSRodney W. Grimes 1882e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 18830d9ce3a1SRobert Watson 1884e7c33e29SRobert Watson unp2 = unp->unp_conn; 188584d61770SRobert Watson if ((unp->unp_socket->so_type == SOCK_STREAM || 188684d61770SRobert Watson (unp->unp_socket->so_type == SOCK_SEQPACKET)) && unp2 != NULL) { 1887e7c33e29SRobert Watson so = unp2->unp_socket; 1888e7c33e29SRobert Watson if (so != NULL) 1889df8bae1dSRodney W. Grimes socantrcvmore(so); 1890df8bae1dSRodney W. Grimes } 1891e7c33e29SRobert Watson } 1892df8bae1dSRodney W. Grimes 1893f708ef1bSPoul-Henning Kamp static void 1894afc055d9SEd Schouten unp_drop(struct unpcb *unp) 1895df8bae1dSRodney W. Grimes { 1896df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 1897e7c33e29SRobert Watson struct unpcb *unp2; 1898*75a67bf3SMatt Macy int freed; 18990d9ce3a1SRobert Watson 1900afc055d9SEd Schouten /* 1901afc055d9SEd Schouten * Regardless of whether the socket's peer dropped the connection 1902afc055d9SEd Schouten * with this socket by aborting or disconnecting, POSIX requires 1903afc055d9SEd Schouten * that ECONNRESET is returned. 1904afc055d9SEd Schouten */ 1905*75a67bf3SMatt Macy /* acquire a reference so that unp isn't freed from underneath us */ 1906*75a67bf3SMatt Macy 1907*75a67bf3SMatt Macy UNP_PCB_LOCK(unp); 1908*75a67bf3SMatt Macy if (so) 1909afc055d9SEd Schouten so->so_error = ECONNRESET; 1910e7c33e29SRobert Watson unp2 = unp->unp_conn; 1911*75a67bf3SMatt Macy if (unp2 != NULL) { 1912*75a67bf3SMatt Macy unp_pcb_hold(unp2); 1913*75a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freed); 1914e7c33e29SRobert Watson unp_disconnect(unp, unp2); 1915*75a67bf3SMatt Macy if (unp_pcb_rele(unp2) == 0) 1916e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1917df8bae1dSRodney W. Grimes } 1918*75a67bf3SMatt Macy if (unp_pcb_rele(unp) == 0) 1919*75a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 1920*75a67bf3SMatt Macy } 1921df8bae1dSRodney W. Grimes 19222bc21ed9SDavid Malone static void 19238cb539f1SPawel Jakub Dawidek unp_freerights(struct filedescent **fdep, int fdcount) 1924df8bae1dSRodney W. Grimes { 19252bc21ed9SDavid Malone struct file *fp; 19262609222aSPawel Jakub Dawidek int i; 1927df8bae1dSRodney W. Grimes 192882e825c4SGleb Smirnoff KASSERT(fdcount > 0, ("%s: fdcount %d", __func__, fdcount)); 192982e825c4SGleb Smirnoff 19308cb539f1SPawel Jakub Dawidek for (i = 0; i < fdcount; i++) { 19318cb539f1SPawel Jakub Dawidek fp = fdep[i]->fde_file; 19328cb539f1SPawel Jakub Dawidek filecaps_free(&fdep[i]->fde_caps); 19338692c025SYoshinobu Inoue unp_discard(fp); 1934df8bae1dSRodney W. Grimes } 19358cb539f1SPawel Jakub Dawidek free(fdep[0], M_FILECAPS); 19362bc21ed9SDavid Malone } 19372bc21ed9SDavid Malone 19380b36cd25SRobert Watson static int 1939c2e3c52eSJilles Tjoelker unp_externalize(struct mbuf *control, struct mbuf **controlp, int flags) 19402bc21ed9SDavid Malone { 19412bc21ed9SDavid Malone struct thread *td = curthread; /* XXX */ 19422bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 19432bc21ed9SDavid Malone int i; 19442bc21ed9SDavid Malone int *fdp; 19452609222aSPawel Jakub Dawidek struct filedesc *fdesc = td->td_proc->p_fd; 1946ea31808cSMateusz Guzik struct filedescent **fdep; 19472bc21ed9SDavid Malone void *data; 19482bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 19492bc21ed9SDavid Malone int error, newfds; 19502bc21ed9SDavid Malone u_int newlen; 19512bc21ed9SDavid Malone 19523dab55bcSRobert Watson UNP_LINK_UNLOCK_ASSERT(); 19534c5bc1caSRobert Watson 19542bc21ed9SDavid Malone error = 0; 19552bc21ed9SDavid Malone if (controlp != NULL) /* controlp == NULL => free control messages */ 19562bc21ed9SDavid Malone *controlp = NULL; 19572bc21ed9SDavid Malone while (cm != NULL) { 19582bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 19592bc21ed9SDavid Malone error = EINVAL; 19602bc21ed9SDavid Malone break; 19612bc21ed9SDavid Malone } 19622bc21ed9SDavid Malone data = CMSG_DATA(cm); 19632bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 19642bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET 19652bc21ed9SDavid Malone && cm->cmsg_type == SCM_RIGHTS) { 19662609222aSPawel Jakub Dawidek newfds = datalen / sizeof(*fdep); 196782e825c4SGleb Smirnoff if (newfds == 0) 196882e825c4SGleb Smirnoff goto next; 19692609222aSPawel Jakub Dawidek fdep = data; 19702bc21ed9SDavid Malone 1971e2f9a08bSOlivier Houchard /* If we're not outputting the descriptors free them. */ 19722bc21ed9SDavid Malone if (error || controlp == NULL) { 19732609222aSPawel Jakub Dawidek unp_freerights(fdep, newfds); 19742bc21ed9SDavid Malone goto next; 19752bc21ed9SDavid Malone } 19762609222aSPawel Jakub Dawidek FILEDESC_XLOCK(fdesc); 197760a5ef26SRobert Watson 1978ed5b7817SJulian Elischer /* 19791c381b19SRobert Watson * Now change each pointer to an fd in the global 19801c381b19SRobert Watson * table to an integer that is the index to the local 19811c381b19SRobert Watson * fd table entry that we set up to point to the 19821c381b19SRobert Watson * global one we are transferring. 1983ed5b7817SJulian Elischer */ 19842bc21ed9SDavid Malone newlen = newfds * sizeof(int); 19852bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 19862bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 19872bc21ed9SDavid Malone if (*controlp == NULL) { 19882609222aSPawel Jakub Dawidek FILEDESC_XUNLOCK(fdesc); 19892bc21ed9SDavid Malone error = E2BIG; 19902609222aSPawel Jakub Dawidek unp_freerights(fdep, newfds); 19912bc21ed9SDavid Malone goto next; 19922bc21ed9SDavid Malone } 19932bc21ed9SDavid Malone 19942bc21ed9SDavid Malone fdp = (int *) 19952bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1996db8f33fdSMateusz Guzik if (fdallocn(td, 0, fdp, newfds) != 0) { 19973331a33aSMateusz Guzik FILEDESC_XUNLOCK(fdesc); 1998db8f33fdSMateusz Guzik error = EMSGSIZE; 1999db8f33fdSMateusz Guzik unp_freerights(fdep, newfds); 2000db8f33fdSMateusz Guzik m_freem(*controlp); 2001db8f33fdSMateusz Guzik *controlp = NULL; 2002db8f33fdSMateusz Guzik goto next; 2003db8f33fdSMateusz Guzik } 20048cb539f1SPawel Jakub Dawidek for (i = 0; i < newfds; i++, fdp++) { 2005ea31808cSMateusz Guzik _finstall(fdesc, fdep[i]->fde_file, *fdp, 2006ea31808cSMateusz Guzik (flags & MSG_CMSG_CLOEXEC) != 0 ? UF_EXCLOSE : 0, 2007ea31808cSMateusz Guzik &fdep[i]->fde_caps); 2008ea31808cSMateusz Guzik unp_externalize_fp(fdep[i]->fde_file); 2009df8bae1dSRodney W. Grimes } 20102609222aSPawel Jakub Dawidek FILEDESC_XUNLOCK(fdesc); 20118cb539f1SPawel Jakub Dawidek free(fdep[0], M_FILECAPS); 20121c381b19SRobert Watson } else { 20131c381b19SRobert Watson /* We can just copy anything else across. */ 20142bc21ed9SDavid Malone if (error || controlp == NULL) 20152bc21ed9SDavid Malone goto next; 20162bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, datalen, 20172bc21ed9SDavid Malone cm->cmsg_type, cm->cmsg_level); 20182bc21ed9SDavid Malone if (*controlp == NULL) { 20192bc21ed9SDavid Malone error = ENOBUFS; 20202bc21ed9SDavid Malone goto next; 20212bc21ed9SDavid Malone } 20222bc21ed9SDavid Malone bcopy(data, 20232bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 20242bc21ed9SDavid Malone datalen); 20252bc21ed9SDavid Malone } 20262bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 20272bc21ed9SDavid Malone 20282bc21ed9SDavid Malone next: 20292bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 20302bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 20312bc21ed9SDavid Malone cm = (struct cmsghdr *) 20322bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 20338692c025SYoshinobu Inoue } else { 20342bc21ed9SDavid Malone clen = 0; 20352bc21ed9SDavid Malone cm = NULL; 20368692c025SYoshinobu Inoue } 20378692c025SYoshinobu Inoue } 20388692c025SYoshinobu Inoue 20392bc21ed9SDavid Malone m_freem(control); 20402bc21ed9SDavid Malone return (error); 2041df8bae1dSRodney W. Grimes } 2042df8bae1dSRodney W. Grimes 20434f590175SPaul Saab static void 20444f590175SPaul Saab unp_zone_change(void *tag) 20454f590175SPaul Saab { 20464f590175SPaul Saab 20474f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets); 20484f590175SPaul Saab } 20494f590175SPaul Saab 20500b36cd25SRobert Watson static void 205198271db4SGarrett Wollman unp_init(void) 205298271db4SGarrett Wollman { 20531c381b19SRobert Watson 205421ca7b57SMarko Zec #ifdef VIMAGE 205521ca7b57SMarko Zec if (!IS_DEFAULT_VNET(curvnet)) 205621ca7b57SMarko Zec return; 205721ca7b57SMarko Zec #endif 20589e9d298aSJeff Roberson unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 2059*75a67bf3SMatt Macy NULL, NULL, UMA_ALIGN_CACHE, 0); 2060fc3fcacfSRobert Watson if (unp_zone == NULL) 206198271db4SGarrett Wollman panic("unp_init"); 20624f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets); 20636e0b6746SPawel Jakub Dawidek uma_zone_set_warning(unp_zone, "kern.ipc.maxsockets limit reached"); 20644f590175SPaul Saab EVENTHANDLER_REGISTER(maxsockets_change, unp_zone_change, 20654f590175SPaul Saab NULL, EVENTHANDLER_PRI_ANY); 206698271db4SGarrett Wollman LIST_INIT(&unp_dhead); 206798271db4SGarrett Wollman LIST_INIT(&unp_shead); 206884d61770SRobert Watson LIST_INIT(&unp_sphead); 20690cb64678SKonstantin Belousov SLIST_INIT(&unp_defers); 2070daee0f0bSKonstantin Belousov TIMEOUT_TASK_INIT(taskqueue_thread, &unp_gc_task, 0, unp_gc, NULL); 20710cb64678SKonstantin Belousov TASK_INIT(&unp_defer_task, 0, unp_process_defers, NULL); 20723dab55bcSRobert Watson UNP_LINK_LOCK_INIT(); 20730cb64678SKonstantin Belousov UNP_DEFERRED_LOCK_INIT(); 207498271db4SGarrett Wollman } 207598271db4SGarrett Wollman 2076f708ef1bSPoul-Henning Kamp static int 2077892af6b9SRobert Watson unp_internalize(struct mbuf **controlp, struct thread *td) 2078df8bae1dSRodney W. Grimes { 20792bc21ed9SDavid Malone struct mbuf *control = *controlp; 2080b40ce416SJulian Elischer struct proc *p = td->td_proc; 20812609222aSPawel Jakub Dawidek struct filedesc *fdesc = p->p_fd; 2082ab15d803SSergey Kandaurov struct bintime *bt; 20832bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 20842bc21ed9SDavid Malone struct cmsgcred *cmcred; 20858cb539f1SPawel Jakub Dawidek struct filedescent *fde, **fdep, *fdev; 20862bc21ed9SDavid Malone struct file *fp; 20872bc21ed9SDavid Malone struct timeval *tv; 2088339efd75SMaxim Sobolev struct timespec *ts; 20896a1cf96bSMateusz Guzik int i, *fdp; 20902bc21ed9SDavid Malone void *data; 20912bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 20922bc21ed9SDavid Malone int error, oldfds; 20938692c025SYoshinobu Inoue u_int newlen; 2094df8bae1dSRodney W. Grimes 20953dab55bcSRobert Watson UNP_LINK_UNLOCK_ASSERT(); 20964c5bc1caSRobert Watson 20972bc21ed9SDavid Malone error = 0; 20982bc21ed9SDavid Malone *controlp = NULL; 20992bc21ed9SDavid Malone while (cm != NULL) { 21002bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 2101de966666SMateusz Guzik || cm->cmsg_len > clen || cm->cmsg_len < sizeof(*cm)) { 21022bc21ed9SDavid Malone error = EINVAL; 21032bc21ed9SDavid Malone goto out; 21042bc21ed9SDavid Malone } 21052bc21ed9SDavid Malone data = CMSG_DATA(cm); 21062bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 21072bc21ed9SDavid Malone 21082bc21ed9SDavid Malone switch (cm->cmsg_type) { 21090b788fa1SBill Paul /* 21100b788fa1SBill Paul * Fill in credential information. 21110b788fa1SBill Paul */ 21122bc21ed9SDavid Malone case SCM_CREDS: 21132bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 21142bc21ed9SDavid Malone SCM_CREDS, SOL_SOCKET); 21152bc21ed9SDavid Malone if (*controlp == NULL) { 21162bc21ed9SDavid Malone error = ENOBUFS; 21172bc21ed9SDavid Malone goto out; 21182bc21ed9SDavid Malone } 21192bc21ed9SDavid Malone cmcred = (struct cmsgcred *) 21202bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 21210b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 2122a854ed98SJohn Baldwin cmcred->cmcred_uid = td->td_ucred->cr_ruid; 2123a854ed98SJohn Baldwin cmcred->cmcred_gid = td->td_ucred->cr_rgid; 2124a854ed98SJohn Baldwin cmcred->cmcred_euid = td->td_ucred->cr_uid; 2125a854ed98SJohn Baldwin cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 21260b788fa1SBill Paul CMGROUP_MAX); 21270b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 21282bc21ed9SDavid Malone cmcred->cmcred_groups[i] = 2129a854ed98SJohn Baldwin td->td_ucred->cr_groups[i]; 21302bc21ed9SDavid Malone break; 21310b788fa1SBill Paul 21322bc21ed9SDavid Malone case SCM_RIGHTS: 21332bc21ed9SDavid Malone oldfds = datalen / sizeof (int); 213482e825c4SGleb Smirnoff if (oldfds == 0) 213582e825c4SGleb Smirnoff break; 2136ed5b7817SJulian Elischer /* 21371c381b19SRobert Watson * Check that all the FDs passed in refer to legal 21381c381b19SRobert Watson * files. If not, reject the entire operation. 2139ed5b7817SJulian Elischer */ 21402bc21ed9SDavid Malone fdp = data; 21412609222aSPawel Jakub Dawidek FILEDESC_SLOCK(fdesc); 21426a1cf96bSMateusz Guzik for (i = 0; i < oldfds; i++, fdp++) { 21436a1cf96bSMateusz Guzik fp = fget_locked(fdesc, *fdp); 21446a1cf96bSMateusz Guzik if (fp == NULL) { 21452609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdesc); 21462bc21ed9SDavid Malone error = EBADF; 21472bc21ed9SDavid Malone goto out; 21482bc21ed9SDavid Malone } 2149e7d6662fSAlfred Perlstein if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 21502609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdesc); 2151e7d6662fSAlfred Perlstein error = EOPNOTSUPP; 2152e7d6662fSAlfred Perlstein goto out; 2153e7d6662fSAlfred Perlstein } 2154e7d6662fSAlfred Perlstein 2155df8bae1dSRodney W. Grimes } 21565e3f7694SRobert Watson 2157ed5b7817SJulian Elischer /* 21580b36cd25SRobert Watson * Now replace the integer FDs with pointers to the 21592609222aSPawel Jakub Dawidek * file structure and capability rights. 2160ed5b7817SJulian Elischer */ 21618cb539f1SPawel Jakub Dawidek newlen = oldfds * sizeof(fdep[0]); 21622bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 21632bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 21642bc21ed9SDavid Malone if (*controlp == NULL) { 21652609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdesc); 21662bc21ed9SDavid Malone error = E2BIG; 21672bc21ed9SDavid Malone goto out; 21688692c025SYoshinobu Inoue } 21692bc21ed9SDavid Malone fdp = data; 21708cb539f1SPawel Jakub Dawidek fdep = (struct filedescent **) 21712bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 21728cb539f1SPawel Jakub Dawidek fdev = malloc(sizeof(*fdev) * oldfds, M_FILECAPS, 21738cb539f1SPawel Jakub Dawidek M_WAITOK); 21748cb539f1SPawel Jakub Dawidek for (i = 0; i < oldfds; i++, fdev++, fdp++) { 21752609222aSPawel Jakub Dawidek fde = &fdesc->fd_ofiles[*fdp]; 21768cb539f1SPawel Jakub Dawidek fdep[i] = fdev; 21778cb539f1SPawel Jakub Dawidek fdep[i]->fde_file = fde->fde_file; 21788cb539f1SPawel Jakub Dawidek filecaps_copy(&fde->fde_caps, 2179d7832811SMateusz Guzik &fdep[i]->fde_caps, true); 21808cb539f1SPawel Jakub Dawidek unp_internalize_fp(fdep[i]->fde_file); 2181df8bae1dSRodney W. Grimes } 21822609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdesc); 21832bc21ed9SDavid Malone break; 21842bc21ed9SDavid Malone 21852bc21ed9SDavid Malone case SCM_TIMESTAMP: 21862bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*tv), 21872bc21ed9SDavid Malone SCM_TIMESTAMP, SOL_SOCKET); 21882bc21ed9SDavid Malone if (*controlp == NULL) { 21892bc21ed9SDavid Malone error = ENOBUFS; 21902bc21ed9SDavid Malone goto out; 21918692c025SYoshinobu Inoue } 21922bc21ed9SDavid Malone tv = (struct timeval *) 21932bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 21942bc21ed9SDavid Malone microtime(tv); 21952bc21ed9SDavid Malone break; 21962bc21ed9SDavid Malone 2197ab15d803SSergey Kandaurov case SCM_BINTIME: 2198ab15d803SSergey Kandaurov *controlp = sbcreatecontrol(NULL, sizeof(*bt), 2199ab15d803SSergey Kandaurov SCM_BINTIME, SOL_SOCKET); 2200ab15d803SSergey Kandaurov if (*controlp == NULL) { 2201ab15d803SSergey Kandaurov error = ENOBUFS; 2202ab15d803SSergey Kandaurov goto out; 2203ab15d803SSergey Kandaurov } 2204ab15d803SSergey Kandaurov bt = (struct bintime *) 2205ab15d803SSergey Kandaurov CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2206ab15d803SSergey Kandaurov bintime(bt); 2207ab15d803SSergey Kandaurov break; 2208ab15d803SSergey Kandaurov 2209339efd75SMaxim Sobolev case SCM_REALTIME: 2210339efd75SMaxim Sobolev *controlp = sbcreatecontrol(NULL, sizeof(*ts), 2211339efd75SMaxim Sobolev SCM_REALTIME, SOL_SOCKET); 2212339efd75SMaxim Sobolev if (*controlp == NULL) { 2213339efd75SMaxim Sobolev error = ENOBUFS; 2214339efd75SMaxim Sobolev goto out; 2215339efd75SMaxim Sobolev } 2216339efd75SMaxim Sobolev ts = (struct timespec *) 2217339efd75SMaxim Sobolev CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2218339efd75SMaxim Sobolev nanotime(ts); 2219339efd75SMaxim Sobolev break; 2220339efd75SMaxim Sobolev 2221339efd75SMaxim Sobolev case SCM_MONOTONIC: 2222339efd75SMaxim Sobolev *controlp = sbcreatecontrol(NULL, sizeof(*ts), 2223339efd75SMaxim Sobolev SCM_MONOTONIC, SOL_SOCKET); 2224339efd75SMaxim Sobolev if (*controlp == NULL) { 2225339efd75SMaxim Sobolev error = ENOBUFS; 2226339efd75SMaxim Sobolev goto out; 2227339efd75SMaxim Sobolev } 2228339efd75SMaxim Sobolev ts = (struct timespec *) 2229339efd75SMaxim Sobolev CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2230339efd75SMaxim Sobolev nanouptime(ts); 2231339efd75SMaxim Sobolev break; 2232339efd75SMaxim Sobolev 22332bc21ed9SDavid Malone default: 22342bc21ed9SDavid Malone error = EINVAL; 22352bc21ed9SDavid Malone goto out; 22362bc21ed9SDavid Malone } 22372bc21ed9SDavid Malone 22382bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 22392bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 22402bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 22412bc21ed9SDavid Malone cm = (struct cmsghdr *) 22422bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 22432bc21ed9SDavid Malone } else { 22442bc21ed9SDavid Malone clen = 0; 22452bc21ed9SDavid Malone cm = NULL; 22462bc21ed9SDavid Malone } 22472bc21ed9SDavid Malone } 22482bc21ed9SDavid Malone 22492bc21ed9SDavid Malone out: 22502bc21ed9SDavid Malone m_freem(control); 22512bc21ed9SDavid Malone return (error); 2252df8bae1dSRodney W. Grimes } 2253df8bae1dSRodney W. Grimes 22545b950deaSRobert Watson static struct mbuf * 22556a2989fdSMatthew N. Dodd unp_addsockcred(struct thread *td, struct mbuf *control) 22566a2989fdSMatthew N. Dodd { 225770df31f4SMaxim Konovalov struct mbuf *m, *n, *n_prev; 22586a2989fdSMatthew N. Dodd struct sockcred *sc; 225970df31f4SMaxim Konovalov const struct cmsghdr *cm; 22606a2989fdSMatthew N. Dodd int ngroups; 22616a2989fdSMatthew N. Dodd int i; 22626a2989fdSMatthew N. Dodd 22636a2989fdSMatthew N. Dodd ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX); 22646a2989fdSMatthew N. Dodd m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET); 22656a2989fdSMatthew N. Dodd if (m == NULL) 22666a2989fdSMatthew N. Dodd return (control); 22676a2989fdSMatthew N. Dodd 22686a2989fdSMatthew N. Dodd sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *)); 22696a2989fdSMatthew N. Dodd sc->sc_uid = td->td_ucred->cr_ruid; 22706a2989fdSMatthew N. Dodd sc->sc_euid = td->td_ucred->cr_uid; 22716a2989fdSMatthew N. Dodd sc->sc_gid = td->td_ucred->cr_rgid; 22726a2989fdSMatthew N. Dodd sc->sc_egid = td->td_ucred->cr_gid; 22736a2989fdSMatthew N. Dodd sc->sc_ngroups = ngroups; 22746a2989fdSMatthew N. Dodd for (i = 0; i < sc->sc_ngroups; i++) 22756a2989fdSMatthew N. Dodd sc->sc_groups[i] = td->td_ucred->cr_groups[i]; 22766a2989fdSMatthew N. Dodd 22776a2989fdSMatthew N. Dodd /* 22781c381b19SRobert Watson * Unlink SCM_CREDS control messages (struct cmsgcred), since just 22791c381b19SRobert Watson * created SCM_CREDS control message (struct sockcred) has another 22801c381b19SRobert Watson * format. 22816a2989fdSMatthew N. Dodd */ 228270df31f4SMaxim Konovalov if (control != NULL) 228370df31f4SMaxim Konovalov for (n = control, n_prev = NULL; n != NULL;) { 228470df31f4SMaxim Konovalov cm = mtod(n, struct cmsghdr *); 228570df31f4SMaxim Konovalov if (cm->cmsg_level == SOL_SOCKET && 228670df31f4SMaxim Konovalov cm->cmsg_type == SCM_CREDS) { 228770df31f4SMaxim Konovalov if (n_prev == NULL) 228870df31f4SMaxim Konovalov control = n->m_next; 228970df31f4SMaxim Konovalov else 229070df31f4SMaxim Konovalov n_prev->m_next = n->m_next; 229170df31f4SMaxim Konovalov n = m_free(n); 229270df31f4SMaxim Konovalov } else { 229370df31f4SMaxim Konovalov n_prev = n; 229470df31f4SMaxim Konovalov n = n->m_next; 229570df31f4SMaxim Konovalov } 229670df31f4SMaxim Konovalov } 22976a2989fdSMatthew N. Dodd 229870df31f4SMaxim Konovalov /* Prepend it to the head. */ 229970df31f4SMaxim Konovalov m->m_next = control; 230070df31f4SMaxim Konovalov return (m); 23016a2989fdSMatthew N. Dodd } 23026a2989fdSMatthew N. Dodd 2303397c19d1SJeff Roberson static struct unpcb * 2304397c19d1SJeff Roberson fptounp(struct file *fp) 2305397c19d1SJeff Roberson { 2306397c19d1SJeff Roberson struct socket *so; 2307397c19d1SJeff Roberson 2308397c19d1SJeff Roberson if (fp->f_type != DTYPE_SOCKET) 2309397c19d1SJeff Roberson return (NULL); 2310397c19d1SJeff Roberson if ((so = fp->f_data) == NULL) 2311397c19d1SJeff Roberson return (NULL); 2312397c19d1SJeff Roberson if (so->so_proto->pr_domain != &localdomain) 2313397c19d1SJeff Roberson return (NULL); 2314397c19d1SJeff Roberson return sotounpcb(so); 2315397c19d1SJeff Roberson } 2316397c19d1SJeff Roberson 2317397c19d1SJeff Roberson static void 2318397c19d1SJeff Roberson unp_discard(struct file *fp) 2319397c19d1SJeff Roberson { 23200cb64678SKonstantin Belousov struct unp_defer *dr; 2321397c19d1SJeff Roberson 23220cb64678SKonstantin Belousov if (unp_externalize_fp(fp)) { 23230cb64678SKonstantin Belousov dr = malloc(sizeof(*dr), M_TEMP, M_WAITOK); 23240cb64678SKonstantin Belousov dr->ud_fp = fp; 23250cb64678SKonstantin Belousov UNP_DEFERRED_LOCK(); 23260cb64678SKonstantin Belousov SLIST_INSERT_HEAD(&unp_defers, dr, ud_link); 23270cb64678SKonstantin Belousov UNP_DEFERRED_UNLOCK(); 23280cb64678SKonstantin Belousov atomic_add_int(&unp_defers_count, 1); 23290cb64678SKonstantin Belousov taskqueue_enqueue(taskqueue_thread, &unp_defer_task); 23300cb64678SKonstantin Belousov } else 2331397c19d1SJeff Roberson (void) closef(fp, (struct thread *)NULL); 2332397c19d1SJeff Roberson } 2333397c19d1SJeff Roberson 2334397c19d1SJeff Roberson static void 23350cb64678SKonstantin Belousov unp_process_defers(void *arg __unused, int pending) 23360cb64678SKonstantin Belousov { 23370cb64678SKonstantin Belousov struct unp_defer *dr; 23380cb64678SKonstantin Belousov SLIST_HEAD(, unp_defer) drl; 23390cb64678SKonstantin Belousov int count; 23400cb64678SKonstantin Belousov 23410cb64678SKonstantin Belousov SLIST_INIT(&drl); 23420cb64678SKonstantin Belousov for (;;) { 23430cb64678SKonstantin Belousov UNP_DEFERRED_LOCK(); 23440cb64678SKonstantin Belousov if (SLIST_FIRST(&unp_defers) == NULL) { 23450cb64678SKonstantin Belousov UNP_DEFERRED_UNLOCK(); 23460cb64678SKonstantin Belousov break; 23470cb64678SKonstantin Belousov } 23480cb64678SKonstantin Belousov SLIST_SWAP(&unp_defers, &drl, unp_defer); 23490cb64678SKonstantin Belousov UNP_DEFERRED_UNLOCK(); 23500cb64678SKonstantin Belousov count = 0; 23510cb64678SKonstantin Belousov while ((dr = SLIST_FIRST(&drl)) != NULL) { 23520cb64678SKonstantin Belousov SLIST_REMOVE_HEAD(&drl, ud_link); 23530cb64678SKonstantin Belousov closef(dr->ud_fp, NULL); 23540cb64678SKonstantin Belousov free(dr, M_TEMP); 23550cb64678SKonstantin Belousov count++; 23560cb64678SKonstantin Belousov } 23570cb64678SKonstantin Belousov atomic_add_int(&unp_defers_count, -count); 23580cb64678SKonstantin Belousov } 23590cb64678SKonstantin Belousov } 23600cb64678SKonstantin Belousov 23610cb64678SKonstantin Belousov static void 2362397c19d1SJeff Roberson unp_internalize_fp(struct file *fp) 2363397c19d1SJeff Roberson { 2364397c19d1SJeff Roberson struct unpcb *unp; 2365397c19d1SJeff Roberson 23663dab55bcSRobert Watson UNP_LINK_WLOCK(); 2367397c19d1SJeff Roberson if ((unp = fptounp(fp)) != NULL) { 2368397c19d1SJeff Roberson unp->unp_file = fp; 2369397c19d1SJeff Roberson unp->unp_msgcount++; 2370397c19d1SJeff Roberson } 237141e0f66dSJeff Roberson fhold(fp); 2372397c19d1SJeff Roberson unp_rights++; 23733dab55bcSRobert Watson UNP_LINK_WUNLOCK(); 2374397c19d1SJeff Roberson } 2375397c19d1SJeff Roberson 23760cb64678SKonstantin Belousov static int 2377397c19d1SJeff Roberson unp_externalize_fp(struct file *fp) 2378397c19d1SJeff Roberson { 2379397c19d1SJeff Roberson struct unpcb *unp; 23800cb64678SKonstantin Belousov int ret; 2381397c19d1SJeff Roberson 23823dab55bcSRobert Watson UNP_LINK_WLOCK(); 23830cb64678SKonstantin Belousov if ((unp = fptounp(fp)) != NULL) { 2384397c19d1SJeff Roberson unp->unp_msgcount--; 23850cb64678SKonstantin Belousov ret = 1; 23860cb64678SKonstantin Belousov } else 23870cb64678SKonstantin Belousov ret = 0; 2388397c19d1SJeff Roberson unp_rights--; 23893dab55bcSRobert Watson UNP_LINK_WUNLOCK(); 23900cb64678SKonstantin Belousov return (ret); 2391397c19d1SJeff Roberson } 2392397c19d1SJeff Roberson 2393161a0c7cSRobert Watson /* 2394a0ec558aSRobert Watson * unp_defer indicates whether additional work has been defered for a future 2395a0ec558aSRobert Watson * pass through unp_gc(). It is thread local and does not require explicit 2396a0ec558aSRobert Watson * synchronization. 2397161a0c7cSRobert Watson */ 2398397c19d1SJeff Roberson static int unp_marked; 2399397c19d1SJeff Roberson static int unp_unreachable; 2400a0ec558aSRobert Watson 2401397c19d1SJeff Roberson static void 2402be26ba7cSPawel Jakub Dawidek unp_accessable(struct filedescent **fdep, int fdcount) 2403397c19d1SJeff Roberson { 2404397c19d1SJeff Roberson struct unpcb *unp; 2405be26ba7cSPawel Jakub Dawidek struct file *fp; 2406be26ba7cSPawel Jakub Dawidek int i; 2407397c19d1SJeff Roberson 2408be26ba7cSPawel Jakub Dawidek for (i = 0; i < fdcount; i++) { 2409be26ba7cSPawel Jakub Dawidek fp = fdep[i]->fde_file; 24106f552cb0SJeff Roberson if ((unp = fptounp(fp)) == NULL) 2411be26ba7cSPawel Jakub Dawidek continue; 2412397c19d1SJeff Roberson if (unp->unp_gcflag & UNPGC_REF) 2413be26ba7cSPawel Jakub Dawidek continue; 2414397c19d1SJeff Roberson unp->unp_gcflag &= ~UNPGC_DEAD; 2415397c19d1SJeff Roberson unp->unp_gcflag |= UNPGC_REF; 2416397c19d1SJeff Roberson unp_marked++; 2417397c19d1SJeff Roberson } 2418be26ba7cSPawel Jakub Dawidek } 2419397c19d1SJeff Roberson 2420397c19d1SJeff Roberson static void 2421397c19d1SJeff Roberson unp_gc_process(struct unpcb *unp) 2422397c19d1SJeff Roberson { 2423779f106aSGleb Smirnoff struct socket *so, *soa; 2424397c19d1SJeff Roberson struct file *fp; 2425397c19d1SJeff Roberson 2426397c19d1SJeff Roberson /* Already processed. */ 2427397c19d1SJeff Roberson if (unp->unp_gcflag & UNPGC_SCANNED) 2428397c19d1SJeff Roberson return; 2429397c19d1SJeff Roberson fp = unp->unp_file; 243060a5ef26SRobert Watson 2431397c19d1SJeff Roberson /* 2432397c19d1SJeff Roberson * Check for a socket potentially in a cycle. It must be in a 2433397c19d1SJeff Roberson * queue as indicated by msgcount, and this must equal the file 2434397c19d1SJeff Roberson * reference count. Note that when msgcount is 0 the file is NULL. 2435397c19d1SJeff Roberson */ 243641e0f66dSJeff Roberson if ((unp->unp_gcflag & UNPGC_REF) == 0 && fp && 243741e0f66dSJeff Roberson unp->unp_msgcount != 0 && fp->f_count == unp->unp_msgcount) { 2438397c19d1SJeff Roberson unp->unp_gcflag |= UNPGC_DEAD; 2439397c19d1SJeff Roberson unp_unreachable++; 2440397c19d1SJeff Roberson return; 2441397c19d1SJeff Roberson } 244260a5ef26SRobert Watson 2443397c19d1SJeff Roberson so = unp->unp_socket; 2444779f106aSGleb Smirnoff SOCK_LOCK(so); 2445779f106aSGleb Smirnoff if (SOLISTENING(so)) { 2446397c19d1SJeff Roberson /* 2447397c19d1SJeff Roberson * Mark all sockets in our accept queue. 2448397c19d1SJeff Roberson */ 2449779f106aSGleb Smirnoff TAILQ_FOREACH(soa, &so->sol_comp, so_list) { 2450779f106aSGleb Smirnoff if (sotounpcb(soa)->unp_gcflag & UNPGC_IGNORE_RIGHTS) 24510c40f353SConrad Meyer continue; 2452397c19d1SJeff Roberson SOCKBUF_LOCK(&soa->so_rcv); 2453397c19d1SJeff Roberson unp_scan(soa->so_rcv.sb_mb, unp_accessable); 2454397c19d1SJeff Roberson SOCKBUF_UNLOCK(&soa->so_rcv); 2455397c19d1SJeff Roberson } 2456779f106aSGleb Smirnoff } else { 2457779f106aSGleb Smirnoff /* 2458779f106aSGleb Smirnoff * Mark all sockets we reference with RIGHTS. 2459779f106aSGleb Smirnoff */ 2460779f106aSGleb Smirnoff if ((unp->unp_gcflag & UNPGC_IGNORE_RIGHTS) == 0) { 2461779f106aSGleb Smirnoff SOCKBUF_LOCK(&so->so_rcv); 2462779f106aSGleb Smirnoff unp_scan(so->so_rcv.sb_mb, unp_accessable); 2463779f106aSGleb Smirnoff SOCKBUF_UNLOCK(&so->so_rcv); 2464779f106aSGleb Smirnoff } 2465779f106aSGleb Smirnoff } 2466779f106aSGleb Smirnoff SOCK_UNLOCK(so); 2467397c19d1SJeff Roberson unp->unp_gcflag |= UNPGC_SCANNED; 2468397c19d1SJeff Roberson } 2469a0ec558aSRobert Watson 2470a0ec558aSRobert Watson static int unp_recycled; 2471be6b1304STom Rhodes SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0, 2472be6b1304STom Rhodes "Number of unreachable sockets claimed by the garbage collector."); 2473df8bae1dSRodney W. Grimes 2474397c19d1SJeff Roberson static int unp_taskcount; 2475be6b1304STom Rhodes SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0, 2476be6b1304STom Rhodes "Number of times the garbage collector has run."); 2477397c19d1SJeff Roberson 2478f708ef1bSPoul-Henning Kamp static void 2479a0ec558aSRobert Watson unp_gc(__unused void *arg, int pending) 2480df8bae1dSRodney W. Grimes { 248184d61770SRobert Watson struct unp_head *heads[] = { &unp_dhead, &unp_shead, &unp_sphead, 248284d61770SRobert Watson NULL }; 2483397c19d1SJeff Roberson struct unp_head **head; 2484f7780c61SKonstantin Belousov struct file *f, **unref; 2485397c19d1SJeff Roberson struct unpcb *unp; 2486f7780c61SKonstantin Belousov int i, total; 2487df8bae1dSRodney W. Grimes 2488a0ec558aSRobert Watson unp_taskcount++; 2489779f106aSGleb Smirnoff UNP_LINK_RLOCK(); 2490ed5b7817SJulian Elischer /* 24910c40f353SConrad Meyer * First clear all gc flags from previous runs, apart from 24920c40f353SConrad Meyer * UNPGC_IGNORE_RIGHTS. 2493ed5b7817SJulian Elischer */ 2494397c19d1SJeff Roberson for (head = heads; *head != NULL; head++) 2495397c19d1SJeff Roberson LIST_FOREACH(unp, *head, unp_link) 24960c40f353SConrad Meyer unp->unp_gcflag = 24970c40f353SConrad Meyer (unp->unp_gcflag & UNPGC_IGNORE_RIGHTS); 249860a5ef26SRobert Watson 2499397c19d1SJeff Roberson /* 2500397c19d1SJeff Roberson * Scan marking all reachable sockets with UNPGC_REF. Once a socket 2501397c19d1SJeff Roberson * is reachable all of the sockets it references are reachable. 2502397c19d1SJeff Roberson * Stop the scan once we do a complete loop without discovering 2503397c19d1SJeff Roberson * a new reachable socket. 2504397c19d1SJeff Roberson */ 2505df8bae1dSRodney W. Grimes do { 2506397c19d1SJeff Roberson unp_unreachable = 0; 2507397c19d1SJeff Roberson unp_marked = 0; 2508397c19d1SJeff Roberson for (head = heads; *head != NULL; head++) 2509397c19d1SJeff Roberson LIST_FOREACH(unp, *head, unp_link) 2510397c19d1SJeff Roberson unp_gc_process(unp); 2511397c19d1SJeff Roberson } while (unp_marked); 2512779f106aSGleb Smirnoff UNP_LINK_RUNLOCK(); 2513397c19d1SJeff Roberson if (unp_unreachable == 0) 2514397c19d1SJeff Roberson return; 251560a5ef26SRobert Watson 2516ed5b7817SJulian Elischer /* 2517397c19d1SJeff Roberson * Allocate space for a local list of dead unpcbs. 2518ed5b7817SJulian Elischer */ 2519397c19d1SJeff Roberson unref = malloc(unp_unreachable * sizeof(struct file *), 2520397c19d1SJeff Roberson M_TEMP, M_WAITOK); 252160a5ef26SRobert Watson 2522ed5b7817SJulian Elischer /* 2523397c19d1SJeff Roberson * Iterate looking for sockets which have been specifically marked 2524397c19d1SJeff Roberson * as as unreachable and store them locally. 2525ed5b7817SJulian Elischer */ 2526f7780c61SKonstantin Belousov UNP_LINK_RLOCK(); 2527f7780c61SKonstantin Belousov for (total = 0, head = heads; *head != NULL; head++) 2528397c19d1SJeff Roberson LIST_FOREACH(unp, *head, unp_link) 2529f7780c61SKonstantin Belousov if ((unp->unp_gcflag & UNPGC_DEAD) != 0) { 2530f7780c61SKonstantin Belousov f = unp->unp_file; 2531f7780c61SKonstantin Belousov if (unp->unp_msgcount == 0 || f == NULL || 2532f7780c61SKonstantin Belousov f->f_count != unp->unp_msgcount) 2533f7780c61SKonstantin Belousov continue; 2534f7780c61SKonstantin Belousov unref[total++] = f; 2535f7780c61SKonstantin Belousov fhold(f); 2536f7780c61SKonstantin Belousov KASSERT(total <= unp_unreachable, 2537397c19d1SJeff Roberson ("unp_gc: incorrect unreachable count.")); 2538397c19d1SJeff Roberson } 2539f7780c61SKonstantin Belousov UNP_LINK_RUNLOCK(); 254060a5ef26SRobert Watson 2541ed5b7817SJulian Elischer /* 2542397c19d1SJeff Roberson * Now flush all sockets, free'ing rights. This will free the 2543397c19d1SJeff Roberson * struct files associated with these sockets but leave each socket 2544397c19d1SJeff Roberson * with one remaining ref. 2545ed5b7817SJulian Elischer */ 25461fb51a12SBjoern A. Zeeb for (i = 0; i < total; i++) { 25471fb51a12SBjoern A. Zeeb struct socket *so; 25481fb51a12SBjoern A. Zeeb 25491fb51a12SBjoern A. Zeeb so = unref[i]->f_data; 25501fb51a12SBjoern A. Zeeb CURVNET_SET(so->so_vnet); 25511fb51a12SBjoern A. Zeeb sorflush(so); 25521fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 25531fb51a12SBjoern A. Zeeb } 255460a5ef26SRobert Watson 2555ed5b7817SJulian Elischer /* 2556397c19d1SJeff Roberson * And finally release the sockets so they can be reclaimed. 2557ed5b7817SJulian Elischer */ 2558f7780c61SKonstantin Belousov for (i = 0; i < total; i++) 2559397c19d1SJeff Roberson fdrop(unref[i], NULL); 2560f7780c61SKonstantin Belousov unp_recycled += total; 2561397c19d1SJeff Roberson free(unref, M_TEMP); 2562df8bae1dSRodney W. Grimes } 2563df8bae1dSRodney W. Grimes 25640b36cd25SRobert Watson static void 256599ab95dbSMark Johnston unp_dispose_mbuf(struct mbuf *m) 2566df8bae1dSRodney W. Grimes { 2567996c772fSJohn Dyson 2568df8bae1dSRodney W. Grimes if (m) 2569be26ba7cSPawel Jakub Dawidek unp_scan(m, unp_freerights); 2570df8bae1dSRodney W. Grimes } 2571df8bae1dSRodney W. Grimes 25720c40f353SConrad Meyer /* 25730c40f353SConrad Meyer * Synchronize against unp_gc, which can trip over data as we are freeing it. 25740c40f353SConrad Meyer */ 25750c40f353SConrad Meyer static void 257699ab95dbSMark Johnston unp_dispose(struct socket *so) 25770c40f353SConrad Meyer { 25780c40f353SConrad Meyer struct unpcb *unp; 25790c40f353SConrad Meyer 25800c40f353SConrad Meyer unp = sotounpcb(so); 2581779f106aSGleb Smirnoff UNP_LINK_WLOCK(); 25820c40f353SConrad Meyer unp->unp_gcflag |= UNPGC_IGNORE_RIGHTS; 2583779f106aSGleb Smirnoff UNP_LINK_WUNLOCK(); 2584779f106aSGleb Smirnoff if (!SOLISTENING(so)) 258599ab95dbSMark Johnston unp_dispose_mbuf(so->so_rcv.sb_mb); 25860c40f353SConrad Meyer } 25870c40f353SConrad Meyer 2588f708ef1bSPoul-Henning Kamp static void 2589be26ba7cSPawel Jakub Dawidek unp_scan(struct mbuf *m0, void (*op)(struct filedescent **, int)) 2590df8bae1dSRodney W. Grimes { 25912bc21ed9SDavid Malone struct mbuf *m; 25922bc21ed9SDavid Malone struct cmsghdr *cm; 25932bc21ed9SDavid Malone void *data; 25942bc21ed9SDavid Malone socklen_t clen, datalen; 2595df8bae1dSRodney W. Grimes 2596fc3fcacfSRobert Watson while (m0 != NULL) { 25972bc21ed9SDavid Malone for (m = m0; m; m = m->m_next) { 259812396bdcSDavid Malone if (m->m_type != MT_CONTROL) 2599df8bae1dSRodney W. Grimes continue; 26002bc21ed9SDavid Malone 26012bc21ed9SDavid Malone cm = mtod(m, struct cmsghdr *); 26022bc21ed9SDavid Malone clen = m->m_len; 26032bc21ed9SDavid Malone 26042bc21ed9SDavid Malone while (cm != NULL) { 26052bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) 26062bc21ed9SDavid Malone break; 26072bc21ed9SDavid Malone 26082bc21ed9SDavid Malone data = CMSG_DATA(cm); 26092bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len 26102bc21ed9SDavid Malone - (caddr_t)data; 26112bc21ed9SDavid Malone 26122bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET && 26132bc21ed9SDavid Malone cm->cmsg_type == SCM_RIGHTS) { 2614be26ba7cSPawel Jakub Dawidek (*op)(data, datalen / 2615be26ba7cSPawel Jakub Dawidek sizeof(struct filedescent *)); 26162bc21ed9SDavid Malone } 26172bc21ed9SDavid Malone 26182bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 26192bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 26202bc21ed9SDavid Malone cm = (struct cmsghdr *) 26212bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 26222bc21ed9SDavid Malone } else { 26232bc21ed9SDavid Malone clen = 0; 26242bc21ed9SDavid Malone cm = NULL; 26252bc21ed9SDavid Malone } 26262bc21ed9SDavid Malone } 2627df8bae1dSRodney W. Grimes } 2628c29a3321SKevin Lo m0 = m0->m_nextpkt; 2629df8bae1dSRodney W. Grimes } 2630df8bae1dSRodney W. Grimes } 2631df8bae1dSRodney W. Grimes 2632662c901cSMikolaj Golub /* 2633662c901cSMikolaj Golub * A helper function called by VFS before socket-type vnode reclamation. 2634662c901cSMikolaj Golub * For an active vnode it clears unp_vnode pointer and decrements unp_vnode 2635662c901cSMikolaj Golub * use count. 2636662c901cSMikolaj Golub */ 2637662c901cSMikolaj Golub void 2638662c901cSMikolaj Golub vfs_unp_reclaim(struct vnode *vp) 2639662c901cSMikolaj Golub { 2640662c901cSMikolaj Golub struct unpcb *unp; 2641662c901cSMikolaj Golub int active; 2642*75a67bf3SMatt Macy struct mtx *vplock; 2643662c901cSMikolaj Golub 2644662c901cSMikolaj Golub ASSERT_VOP_ELOCKED(vp, "vfs_unp_reclaim"); 2645662c901cSMikolaj Golub KASSERT(vp->v_type == VSOCK, 2646662c901cSMikolaj Golub ("vfs_unp_reclaim: vp->v_type != VSOCK")); 2647662c901cSMikolaj Golub 2648662c901cSMikolaj Golub active = 0; 2649*75a67bf3SMatt Macy vplock = mtx_pool_find(mtxpool_sleep, vp); 2650*75a67bf3SMatt Macy mtx_lock(vplock); 26510c3c207fSGleb Smirnoff VOP_UNP_CONNECT(vp, &unp); 2652662c901cSMikolaj Golub if (unp == NULL) 2653662c901cSMikolaj Golub goto done; 2654662c901cSMikolaj Golub UNP_PCB_LOCK(unp); 2655c7e41c8bSMikolaj Golub if (unp->unp_vnode == vp) { 2656c7e41c8bSMikolaj Golub VOP_UNP_DETACH(vp); 2657662c901cSMikolaj Golub unp->unp_vnode = NULL; 2658662c901cSMikolaj Golub active = 1; 2659662c901cSMikolaj Golub } 2660662c901cSMikolaj Golub UNP_PCB_UNLOCK(unp); 2661662c901cSMikolaj Golub done: 2662*75a67bf3SMatt Macy mtx_unlock(vplock); 2663662c901cSMikolaj Golub if (active) 2664662c901cSMikolaj Golub vunref(vp); 2665662c901cSMikolaj Golub } 2666662c901cSMikolaj Golub 266703c96c31SRobert Watson #ifdef DDB 266803c96c31SRobert Watson static void 266903c96c31SRobert Watson db_print_indent(int indent) 267003c96c31SRobert Watson { 267103c96c31SRobert Watson int i; 267203c96c31SRobert Watson 267303c96c31SRobert Watson for (i = 0; i < indent; i++) 267403c96c31SRobert Watson db_printf(" "); 267503c96c31SRobert Watson } 267603c96c31SRobert Watson 267703c96c31SRobert Watson static void 267803c96c31SRobert Watson db_print_unpflags(int unp_flags) 267903c96c31SRobert Watson { 268003c96c31SRobert Watson int comma; 268103c96c31SRobert Watson 268203c96c31SRobert Watson comma = 0; 268303c96c31SRobert Watson if (unp_flags & UNP_HAVEPC) { 268403c96c31SRobert Watson db_printf("%sUNP_HAVEPC", comma ? ", " : ""); 268503c96c31SRobert Watson comma = 1; 268603c96c31SRobert Watson } 268703c96c31SRobert Watson if (unp_flags & UNP_WANTCRED) { 268803c96c31SRobert Watson db_printf("%sUNP_WANTCRED", comma ? ", " : ""); 268903c96c31SRobert Watson comma = 1; 269003c96c31SRobert Watson } 269103c96c31SRobert Watson if (unp_flags & UNP_CONNWAIT) { 269203c96c31SRobert Watson db_printf("%sUNP_CONNWAIT", comma ? ", " : ""); 269303c96c31SRobert Watson comma = 1; 269403c96c31SRobert Watson } 269503c96c31SRobert Watson if (unp_flags & UNP_CONNECTING) { 269603c96c31SRobert Watson db_printf("%sUNP_CONNECTING", comma ? ", " : ""); 269703c96c31SRobert Watson comma = 1; 269803c96c31SRobert Watson } 269903c96c31SRobert Watson if (unp_flags & UNP_BINDING) { 270003c96c31SRobert Watson db_printf("%sUNP_BINDING", comma ? ", " : ""); 270103c96c31SRobert Watson comma = 1; 270203c96c31SRobert Watson } 270303c96c31SRobert Watson } 270403c96c31SRobert Watson 270503c96c31SRobert Watson static void 270603c96c31SRobert Watson db_print_xucred(int indent, struct xucred *xu) 270703c96c31SRobert Watson { 270803c96c31SRobert Watson int comma, i; 270903c96c31SRobert Watson 271003c96c31SRobert Watson db_print_indent(indent); 271103c96c31SRobert Watson db_printf("cr_version: %u cr_uid: %u cr_ngroups: %d\n", 271203c96c31SRobert Watson xu->cr_version, xu->cr_uid, xu->cr_ngroups); 271303c96c31SRobert Watson db_print_indent(indent); 271403c96c31SRobert Watson db_printf("cr_groups: "); 271503c96c31SRobert Watson comma = 0; 271603c96c31SRobert Watson for (i = 0; i < xu->cr_ngroups; i++) { 271703c96c31SRobert Watson db_printf("%s%u", comma ? ", " : "", xu->cr_groups[i]); 271803c96c31SRobert Watson comma = 1; 271903c96c31SRobert Watson } 272003c96c31SRobert Watson db_printf("\n"); 272103c96c31SRobert Watson } 272203c96c31SRobert Watson 272303c96c31SRobert Watson static void 272403c96c31SRobert Watson db_print_unprefs(int indent, struct unp_head *uh) 272503c96c31SRobert Watson { 272603c96c31SRobert Watson struct unpcb *unp; 272703c96c31SRobert Watson int counter; 272803c96c31SRobert Watson 272903c96c31SRobert Watson counter = 0; 273003c96c31SRobert Watson LIST_FOREACH(unp, uh, unp_reflink) { 273103c96c31SRobert Watson if (counter % 4 == 0) 273203c96c31SRobert Watson db_print_indent(indent); 273303c96c31SRobert Watson db_printf("%p ", unp); 273403c96c31SRobert Watson if (counter % 4 == 3) 273503c96c31SRobert Watson db_printf("\n"); 273603c96c31SRobert Watson counter++; 273703c96c31SRobert Watson } 273803c96c31SRobert Watson if (counter != 0 && counter % 4 != 0) 273903c96c31SRobert Watson db_printf("\n"); 274003c96c31SRobert Watson } 274103c96c31SRobert Watson 274203c96c31SRobert Watson DB_SHOW_COMMAND(unpcb, db_show_unpcb) 274303c96c31SRobert Watson { 274403c96c31SRobert Watson struct unpcb *unp; 274503c96c31SRobert Watson 274603c96c31SRobert Watson if (!have_addr) { 274703c96c31SRobert Watson db_printf("usage: show unpcb <addr>\n"); 274803c96c31SRobert Watson return; 274903c96c31SRobert Watson } 275003c96c31SRobert Watson unp = (struct unpcb *)addr; 275103c96c31SRobert Watson 275203c96c31SRobert Watson db_printf("unp_socket: %p unp_vnode: %p\n", unp->unp_socket, 275303c96c31SRobert Watson unp->unp_vnode); 275403c96c31SRobert Watson 2755fc8fdae0SMatthew D Fleming db_printf("unp_ino: %ju unp_conn: %p\n", (uintmax_t)unp->unp_ino, 275603c96c31SRobert Watson unp->unp_conn); 275703c96c31SRobert Watson 275803c96c31SRobert Watson db_printf("unp_refs:\n"); 275903c96c31SRobert Watson db_print_unprefs(2, &unp->unp_refs); 276003c96c31SRobert Watson 276103c96c31SRobert Watson /* XXXRW: Would be nice to print the full address, if any. */ 276203c96c31SRobert Watson db_printf("unp_addr: %p\n", unp->unp_addr); 276303c96c31SRobert Watson 2764c2090e73SAlan Somers db_printf("unp_gencnt: %llu\n", 276503c96c31SRobert Watson (unsigned long long)unp->unp_gencnt); 276603c96c31SRobert Watson 276703c96c31SRobert Watson db_printf("unp_flags: %x (", unp->unp_flags); 276803c96c31SRobert Watson db_print_unpflags(unp->unp_flags); 276903c96c31SRobert Watson db_printf(")\n"); 277003c96c31SRobert Watson 277103c96c31SRobert Watson db_printf("unp_peercred:\n"); 277203c96c31SRobert Watson db_print_xucred(2, &unp->unp_peercred); 277303c96c31SRobert Watson 277403c96c31SRobert Watson db_printf("unp_refcount: %u\n", unp->unp_refcount); 277503c96c31SRobert Watson } 277603c96c31SRobert Watson #endif 2777