19454b2d8SWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1991, 1993 5f344fb0bSWarner Losh * The Regents of the University of California. All Rights Reserved. 6f344fb0bSWarner Losh * Copyright (c) 2004-2009 Robert N. M. Watson All Rights Reserved. 7c0874c34SMatt Macy * Copyright (c) 2018 Matthew Macy 8df8bae1dSRodney W. Grimes * 9df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 10df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 11df8bae1dSRodney W. Grimes * are met: 12df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 14df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 15df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 16df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1769a28758SEd Maste * 3. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 33748e0b0aSGarrett Wollman * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94 34df8bae1dSRodney W. Grimes */ 35df8bae1dSRodney W. Grimes 36f23929fbSRobert Watson /* 37f23929fbSRobert Watson * UNIX Domain (Local) Sockets 38f23929fbSRobert Watson * 39f23929fbSRobert Watson * This is an implementation of UNIX (local) domain sockets. Each socket has 40f23929fbSRobert Watson * an associated struct unpcb (UNIX protocol control block). Stream sockets 41f23929fbSRobert Watson * may be connected to 0 or 1 other socket. Datagram sockets may be 42f23929fbSRobert Watson * connected to 0, 1, or many other sockets. Sockets may be created and 43f23929fbSRobert Watson * connected in pairs (socketpair(2)), or bound/connected to using the file 44f23929fbSRobert Watson * system name space. For most purposes, only the receive socket buffer is 45f23929fbSRobert Watson * used, as sending on one socket delivers directly to the receive socket 465b950deaSRobert Watson * buffer of a second socket. 475b950deaSRobert Watson * 485b950deaSRobert Watson * The implementation is substantially complicated by the fact that 495b950deaSRobert Watson * "ancillary data", such as file descriptors or credentials, may be passed 505b950deaSRobert Watson * across UNIX domain sockets. The potential for passing UNIX domain sockets 515b950deaSRobert Watson * over other UNIX domain sockets requires the implementation of a simple 525b950deaSRobert Watson * garbage collector to find and tear down cycles of disconnected sockets. 53aea52f1bSRobert Watson * 54aea52f1bSRobert Watson * TODO: 5584d61770SRobert Watson * RDM 56aea52f1bSRobert Watson * rethink name space problems 57aea52f1bSRobert Watson * need a proper out-of-band 58f23929fbSRobert Watson */ 59f23929fbSRobert Watson 60677b542eSDavid E. O'Brien #include <sys/cdefs.h> 61677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 62677b542eSDavid E. O'Brien 6303c96c31SRobert Watson #include "opt_ddb.h" 64335654d7SRobert Watson 65df8bae1dSRodney W. Grimes #include <sys/param.h> 664a144410SRobert Watson #include <sys/capsicum.h> 67fb919e4dSMark Murray #include <sys/domain.h> 68960ed29cSSeigo Tanimura #include <sys/fcntl.h> 69d826c479SBruce Evans #include <sys/malloc.h> /* XXX must be before <sys/file.h> */ 704f590175SPaul Saab #include <sys/eventhandler.h> 71639acc13SGarrett Wollman #include <sys/file.h> 72960ed29cSSeigo Tanimura #include <sys/filedesc.h> 73960ed29cSSeigo Tanimura #include <sys/kernel.h> 74960ed29cSSeigo Tanimura #include <sys/lock.h> 75639acc13SGarrett Wollman #include <sys/mbuf.h> 76033eb86eSJeff Roberson #include <sys/mount.h> 77960ed29cSSeigo Tanimura #include <sys/mutex.h> 78639acc13SGarrett Wollman #include <sys/namei.h> 79639acc13SGarrett Wollman #include <sys/proc.h> 80df8bae1dSRodney W. Grimes #include <sys/protosw.h> 810cb64678SKonstantin Belousov #include <sys/queue.h> 82960ed29cSSeigo Tanimura #include <sys/resourcevar.h> 83e7c33e29SRobert Watson #include <sys/rwlock.h> 84df8bae1dSRodney W. Grimes #include <sys/socket.h> 85df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 86960ed29cSSeigo Tanimura #include <sys/signalvar.h> 87df8bae1dSRodney W. Grimes #include <sys/stat.h> 88960ed29cSSeigo Tanimura #include <sys/sx.h> 89639acc13SGarrett Wollman #include <sys/sysctl.h> 90960ed29cSSeigo Tanimura #include <sys/systm.h> 91a0ec558aSRobert Watson #include <sys/taskqueue.h> 92639acc13SGarrett Wollman #include <sys/un.h> 9398271db4SGarrett Wollman #include <sys/unpcb.h> 94639acc13SGarrett Wollman #include <sys/vnode.h> 95530c0060SRobert Watson 96530c0060SRobert Watson #include <net/vnet.h> 97df8bae1dSRodney W. Grimes 9803c96c31SRobert Watson #ifdef DDB 9903c96c31SRobert Watson #include <ddb/ddb.h> 10003c96c31SRobert Watson #endif 10103c96c31SRobert Watson 102aed55708SRobert Watson #include <security/mac/mac_framework.h> 103aed55708SRobert Watson 1049e9d298aSJeff Roberson #include <vm/uma.h> 10598271db4SGarrett Wollman 1068cb539f1SPawel Jakub Dawidek MALLOC_DECLARE(M_FILECAPS); 1078cb539f1SPawel Jakub Dawidek 1083dab55bcSRobert Watson /* 1093dab55bcSRobert Watson * Locking key: 1103dab55bcSRobert Watson * (l) Locked using list lock 1113dab55bcSRobert Watson * (g) Locked using linkage lock 1123dab55bcSRobert Watson */ 1133dab55bcSRobert Watson 1149e9d298aSJeff Roberson static uma_zone_t unp_zone; 1153dab55bcSRobert Watson static unp_gen_t unp_gencnt; /* (l) */ 1163dab55bcSRobert Watson static u_int unp_count; /* (l) Count of local sockets. */ 117aea52f1bSRobert Watson static ino_t unp_ino; /* Prototype for fake inode numbers. */ 1183dab55bcSRobert Watson static int unp_rights; /* (g) File descriptors in flight. */ 1193dab55bcSRobert Watson static struct unp_head unp_shead; /* (l) List of stream sockets. */ 1203dab55bcSRobert Watson static struct unp_head unp_dhead; /* (l) List of datagram sockets. */ 12184d61770SRobert Watson static struct unp_head unp_sphead; /* (l) List of seqpacket sockets. */ 12298271db4SGarrett Wollman 1230cb64678SKonstantin Belousov struct unp_defer { 1240cb64678SKonstantin Belousov SLIST_ENTRY(unp_defer) ud_link; 1250cb64678SKonstantin Belousov struct file *ud_fp; 1260cb64678SKonstantin Belousov }; 1270cb64678SKonstantin Belousov static SLIST_HEAD(, unp_defer) unp_defers; 1280cb64678SKonstantin Belousov static int unp_defers_count; 1290cb64678SKonstantin Belousov 130aea52f1bSRobert Watson static const struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 13198271db4SGarrett Wollman 132df8bae1dSRodney W. Grimes /* 133aea52f1bSRobert Watson * Garbage collection of cyclic file descriptor/socket references occurs 134aea52f1bSRobert Watson * asynchronously in a taskqueue context in order to avoid recursion and 135aea52f1bSRobert Watson * reentrance in the UNIX domain socket, file descriptor, and socket layer 136aea52f1bSRobert Watson * code. See unp_gc() for a full description. 137df8bae1dSRodney W. Grimes */ 138daee0f0bSKonstantin Belousov static struct timeout_task unp_gc_task; 139f708ef1bSPoul-Henning Kamp 140ce5f32deSRobert Watson /* 1410cb64678SKonstantin Belousov * The close of unix domain sockets attached as SCM_RIGHTS is 1420cb64678SKonstantin Belousov * postponed to the taskqueue, to avoid arbitrary recursion depth. 1430cb64678SKonstantin Belousov * The attached sockets might have another sockets attached. 1440cb64678SKonstantin Belousov */ 1450cb64678SKonstantin Belousov static struct task unp_defer_task; 1460cb64678SKonstantin Belousov 1470cb64678SKonstantin Belousov /* 1487e711c3aSRobert Watson * Both send and receive buffers are allocated PIPSIZ bytes of buffering for 1497e711c3aSRobert Watson * stream sockets, although the total for sender and receiver is actually 1507e711c3aSRobert Watson * only PIPSIZ. 1517e711c3aSRobert Watson * 1527e711c3aSRobert Watson * Datagram sockets really use the sendspace as the maximum datagram size, 1537e711c3aSRobert Watson * and don't really want to reserve the sendspace. Their recvspace should be 1547e711c3aSRobert Watson * large enough for at least one max-size datagram plus address. 1557e711c3aSRobert Watson */ 1567e711c3aSRobert Watson #ifndef PIPSIZ 1577e711c3aSRobert Watson #define PIPSIZ 8192 1587e711c3aSRobert Watson #endif 1597e711c3aSRobert Watson static u_long unpst_sendspace = PIPSIZ; 1607e711c3aSRobert Watson static u_long unpst_recvspace = PIPSIZ; 1617e711c3aSRobert Watson static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 1627e711c3aSRobert Watson static u_long unpdg_recvspace = 4*1024; 16384d61770SRobert Watson static u_long unpsp_sendspace = PIPSIZ; /* really max datagram size */ 16484d61770SRobert Watson static u_long unpsp_recvspace = PIPSIZ; 1657e711c3aSRobert Watson 1666472ac3dSEd Schouten static SYSCTL_NODE(_net, PF_LOCAL, local, CTLFLAG_RW, 0, "Local domain"); 1676472ac3dSEd Schouten static SYSCTL_NODE(_net_local, SOCK_STREAM, stream, CTLFLAG_RW, 0, 1686472ac3dSEd Schouten "SOCK_STREAM"); 1696472ac3dSEd Schouten static SYSCTL_NODE(_net_local, SOCK_DGRAM, dgram, CTLFLAG_RW, 0, "SOCK_DGRAM"); 1706472ac3dSEd Schouten static SYSCTL_NODE(_net_local, SOCK_SEQPACKET, seqpacket, CTLFLAG_RW, 0, 17184d61770SRobert Watson "SOCK_SEQPACKET"); 172e4445a03SRobert Watson 1737e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 174be6b1304STom Rhodes &unpst_sendspace, 0, "Default stream send space."); 1757e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 176be6b1304STom Rhodes &unpst_recvspace, 0, "Default stream receive space."); 1777e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 178be6b1304STom Rhodes &unpdg_sendspace, 0, "Default datagram send space."); 1797e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 180be6b1304STom Rhodes &unpdg_recvspace, 0, "Default datagram receive space."); 18184d61770SRobert Watson SYSCTL_ULONG(_net_local_seqpacket, OID_AUTO, maxseqpacket, CTLFLAG_RW, 18284d61770SRobert Watson &unpsp_sendspace, 0, "Default seqpacket send space."); 18384d61770SRobert Watson SYSCTL_ULONG(_net_local_seqpacket, OID_AUTO, recvspace, CTLFLAG_RW, 18484d61770SRobert Watson &unpsp_recvspace, 0, "Default seqpacket receive space."); 185be6b1304STom Rhodes SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, 186be6b1304STom Rhodes "File descriptors in flight."); 1870cb64678SKonstantin Belousov SYSCTL_INT(_net_local, OID_AUTO, deferred, CTLFLAG_RD, 1880cb64678SKonstantin Belousov &unp_defers_count, 0, 1890cb64678SKonstantin Belousov "File descriptors deferred to taskqueue for close."); 1907e711c3aSRobert Watson 191175389cfSEdward Tomasz Napierala /* 192e7c33e29SRobert Watson * Locking and synchronization: 193ce5f32deSRobert Watson * 19475a67bf3SMatt Macy * Three types of locks exist in the local domain socket implementation: a 19575a67bf3SMatt Macy * a global linkage rwlock, the mtxpool lock, and per-unpcb mutexes. 19675a67bf3SMatt Macy * The linkage lock protects the socket count, global generation number, 19775a67bf3SMatt Macy * and stream/datagram global lists. 19875a67bf3SMatt Macy * 19975a67bf3SMatt Macy * The mtxpool lock protects the vnode from being modified while referenced. 20075a67bf3SMatt Macy * Lock ordering requires that it be acquired before any unpcb locks. 20175a67bf3SMatt Macy * 20275a67bf3SMatt Macy * The unpcb lock (unp_mtx) protects all fields in the unpcb. Of particular 20375a67bf3SMatt Macy * note is that this includes the unp_conn field. So long as the unpcb lock 20475a67bf3SMatt Macy * is held the reference to the unpcb pointed to by unp_conn is valid. If we 20575a67bf3SMatt Macy * require that the unpcb pointed to by unp_conn remain live in cases where 20675a67bf3SMatt Macy * we need to drop the unp_mtx as when we need to acquire the lock for a 20775a67bf3SMatt Macy * second unpcb the caller must first acquire an additional reference on the 20875a67bf3SMatt Macy * second unpcb and then revalidate any state (typically check that unp_conn 20975a67bf3SMatt Macy * is non-NULL) upon requiring the initial unpcb lock. The lock ordering 21075a67bf3SMatt Macy * between unpcbs is the conventional ascending address order. Two helper 21175a67bf3SMatt Macy * routines exist for this: 21275a67bf3SMatt Macy * 21375a67bf3SMatt Macy * - unp_pcb_lock2(unp, unp2) - which just acquires the two locks in the 21475a67bf3SMatt Macy * safe ordering. 21575a67bf3SMatt Macy * 21675a67bf3SMatt Macy * - unp_pcb_owned_lock2(unp, unp2, freed) - the lock for unp is held 21775a67bf3SMatt Macy * when called. If unp is unlocked and unp2 is subsequently freed 21875a67bf3SMatt Macy * freed will be set to 1. 21975a67bf3SMatt Macy * 22075a67bf3SMatt Macy * The helper routines for references are: 22175a67bf3SMatt Macy * 22275a67bf3SMatt Macy * - unp_pcb_hold(unp): Can be called any time we currently hold a valid 22375a67bf3SMatt Macy * reference to unp. 22475a67bf3SMatt Macy * 22575a67bf3SMatt Macy * - unp_pcb_rele(unp): The caller must hold the unp lock. If we are 22675a67bf3SMatt Macy * releasing the last reference, detach must have been called thus 22775a67bf3SMatt Macy * unp->unp_socket be NULL. 228ce5f32deSRobert Watson * 229e7c33e29SRobert Watson * UNIX domain sockets each have an unpcb hung off of their so_pcb pointer, 230e7c33e29SRobert Watson * allocated in pru_attach() and freed in pru_detach(). The validity of that 231e7c33e29SRobert Watson * pointer is an invariant, so no lock is required to dereference the so_pcb 232e7c33e29SRobert Watson * pointer if a valid socket reference is held by the caller. In practice, 233e7c33e29SRobert Watson * this is always true during operations performed on a socket. Each unpcb 234e7c33e29SRobert Watson * has a back-pointer to its socket, unp_socket, which will be stable under 235e7c33e29SRobert Watson * the same circumstances. 236e7c33e29SRobert Watson * 237e7c33e29SRobert Watson * This pointer may only be safely dereferenced as long as a valid reference 238e7c33e29SRobert Watson * to the unpcb is held. Typically, this reference will be from the socket, 239e7c33e29SRobert Watson * or from another unpcb when the referring unpcb's lock is held (in order 240e7c33e29SRobert Watson * that the reference not be invalidated during use). For example, to follow 24175a67bf3SMatt Macy * unp->unp_conn->unp_socket, you need to hold a lock on unp_conn to guarantee 24275a67bf3SMatt Macy * that detach is not run clearing unp_socket. 243e7c33e29SRobert Watson * 244e7c33e29SRobert Watson * Blocking with UNIX domain sockets is a tricky issue: unlike most network 245e7c33e29SRobert Watson * protocols, bind() is a non-atomic operation, and connect() requires 246e7c33e29SRobert Watson * potential sleeping in the protocol, due to potentially waiting on local or 247e7c33e29SRobert Watson * distributed file systems. We try to separate "lookup" operations, which 248e7c33e29SRobert Watson * may sleep, and the IPC operations themselves, which typically can occur 249e7c33e29SRobert Watson * with relative atomicity as locks can be held over the entire operation. 250e7c33e29SRobert Watson * 251e7c33e29SRobert Watson * Another tricky issue is simultaneous multi-threaded or multi-process 252e7c33e29SRobert Watson * access to a single UNIX domain socket. These are handled by the flags 253e7c33e29SRobert Watson * UNP_CONNECTING and UNP_BINDING, which prevent concurrent connecting or 254e7c33e29SRobert Watson * binding, both of which involve dropping UNIX domain socket locks in order 255e7c33e29SRobert Watson * to perform namei() and other file system operations. 256ce5f32deSRobert Watson */ 2573dab55bcSRobert Watson static struct rwlock unp_link_rwlock; 2580cb64678SKonstantin Belousov static struct mtx unp_defers_lock; 259e7c33e29SRobert Watson 2603dab55bcSRobert Watson #define UNP_LINK_LOCK_INIT() rw_init(&unp_link_rwlock, \ 2613dab55bcSRobert Watson "unp_link_rwlock") 262e7c33e29SRobert Watson 2633dab55bcSRobert Watson #define UNP_LINK_LOCK_ASSERT() rw_assert(&unp_link_rwlock, \ 264e7c33e29SRobert Watson RA_LOCKED) 2653dab55bcSRobert Watson #define UNP_LINK_UNLOCK_ASSERT() rw_assert(&unp_link_rwlock, \ 266e7c33e29SRobert Watson RA_UNLOCKED) 267e7c33e29SRobert Watson 2683dab55bcSRobert Watson #define UNP_LINK_RLOCK() rw_rlock(&unp_link_rwlock) 2693dab55bcSRobert Watson #define UNP_LINK_RUNLOCK() rw_runlock(&unp_link_rwlock) 2703dab55bcSRobert Watson #define UNP_LINK_WLOCK() rw_wlock(&unp_link_rwlock) 2713dab55bcSRobert Watson #define UNP_LINK_WUNLOCK() rw_wunlock(&unp_link_rwlock) 2723dab55bcSRobert Watson #define UNP_LINK_WLOCK_ASSERT() rw_assert(&unp_link_rwlock, \ 273e7c33e29SRobert Watson RA_WLOCKED) 274779f106aSGleb Smirnoff #define UNP_LINK_WOWNED() rw_wowned(&unp_link_rwlock) 275e7c33e29SRobert Watson 2760cb64678SKonstantin Belousov #define UNP_DEFERRED_LOCK_INIT() mtx_init(&unp_defers_lock, \ 2770cb64678SKonstantin Belousov "unp_defer", NULL, MTX_DEF) 2780cb64678SKonstantin Belousov #define UNP_DEFERRED_LOCK() mtx_lock(&unp_defers_lock) 2790cb64678SKonstantin Belousov #define UNP_DEFERRED_UNLOCK() mtx_unlock(&unp_defers_lock) 2800cb64678SKonstantin Belousov 28175a67bf3SMatt Macy #define UNP_REF_LIST_LOCK() UNP_DEFERRED_LOCK(); 28275a67bf3SMatt Macy #define UNP_REF_LIST_UNLOCK() UNP_DEFERRED_UNLOCK(); 28375a67bf3SMatt Macy 284e7c33e29SRobert Watson #define UNP_PCB_LOCK_INIT(unp) mtx_init(&(unp)->unp_mtx, \ 285d9525340SMatt Macy "unp", "unp", \ 28675a67bf3SMatt Macy MTX_DUPOK|MTX_DEF) 287e7c33e29SRobert Watson #define UNP_PCB_LOCK_DESTROY(unp) mtx_destroy(&(unp)->unp_mtx) 288e7c33e29SRobert Watson #define UNP_PCB_LOCK(unp) mtx_lock(&(unp)->unp_mtx) 28975a67bf3SMatt Macy #define UNP_PCB_TRYLOCK(unp) mtx_trylock(&(unp)->unp_mtx) 290e7c33e29SRobert Watson #define UNP_PCB_UNLOCK(unp) mtx_unlock(&(unp)->unp_mtx) 29175a67bf3SMatt Macy #define UNP_PCB_OWNED(unp) mtx_owned(&(unp)->unp_mtx) 292e7c33e29SRobert Watson #define UNP_PCB_LOCK_ASSERT(unp) mtx_assert(&(unp)->unp_mtx, MA_OWNED) 29375a67bf3SMatt Macy #define UNP_PCB_UNLOCK_ASSERT(unp) mtx_assert(&(unp)->unp_mtx, MA_NOTOWNED) 2940d9ce3a1SRobert Watson 2952c899584SRobert Watson static int uipc_connect2(struct socket *, struct socket *); 2960b36cd25SRobert Watson static int uipc_ctloutput(struct socket *, struct sockopt *); 297aea52f1bSRobert Watson static int unp_connect(struct socket *, struct sockaddr *, 298aea52f1bSRobert Watson struct thread *); 2997493f24eSPawel Jakub Dawidek static int unp_connectat(int, struct socket *, struct sockaddr *, 3007493f24eSPawel Jakub Dawidek struct thread *); 3016a2989fdSMatthew N. Dodd static int unp_connect2(struct socket *so, struct socket *so2, int); 302e7c33e29SRobert Watson static void unp_disconnect(struct unpcb *unp, struct unpcb *unp2); 30399ab95dbSMark Johnston static void unp_dispose(struct socket *so); 30499ab95dbSMark Johnston static void unp_dispose_mbuf(struct mbuf *); 3054d77a549SAlfred Perlstein static void unp_shutdown(struct unpcb *); 306afc055d9SEd Schouten static void unp_drop(struct unpcb *); 307a0ec558aSRobert Watson static void unp_gc(__unused void *, int); 308be26ba7cSPawel Jakub Dawidek static void unp_scan(struct mbuf *, void (*)(struct filedescent **, int)); 3094d77a549SAlfred Perlstein static void unp_discard(struct file *); 3108cb539f1SPawel Jakub Dawidek static void unp_freerights(struct filedescent **, int); 3110b36cd25SRobert Watson static void unp_init(void); 3124d77a549SAlfred Perlstein static int unp_internalize(struct mbuf **, struct thread *); 313397c19d1SJeff Roberson static void unp_internalize_fp(struct file *); 314c2e3c52eSJilles Tjoelker static int unp_externalize(struct mbuf *, struct mbuf **, int); 3150cb64678SKonstantin Belousov static int unp_externalize_fp(struct file *); 3165b950deaSRobert Watson static struct mbuf *unp_addsockcred(struct thread *, struct mbuf *); 3170cb64678SKonstantin Belousov static void unp_process_defers(void * __unused, int); 318f708ef1bSPoul-Henning Kamp 31975a67bf3SMatt Macy 32075a67bf3SMatt Macy static void 32175a67bf3SMatt Macy unp_pcb_hold(struct unpcb *unp) 32275a67bf3SMatt Macy { 32375a67bf3SMatt Macy MPASS(unp->unp_refcount); 32475a67bf3SMatt Macy refcount_acquire(&unp->unp_refcount); 32575a67bf3SMatt Macy } 32675a67bf3SMatt Macy 32775a67bf3SMatt Macy static int 32875a67bf3SMatt Macy unp_pcb_rele(struct unpcb *unp) 32975a67bf3SMatt Macy { 33075a67bf3SMatt Macy int freed; 33175a67bf3SMatt Macy 33275a67bf3SMatt Macy UNP_PCB_LOCK_ASSERT(unp); 33375a67bf3SMatt Macy MPASS(unp->unp_refcount); 33475a67bf3SMatt Macy if ((freed = refcount_release(&unp->unp_refcount))) { 33575a67bf3SMatt Macy /* we got here with having detached? */ 33675a67bf3SMatt Macy MPASS(unp->unp_socket == NULL); 33775a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 33875a67bf3SMatt Macy UNP_PCB_LOCK_DESTROY(unp); 33975a67bf3SMatt Macy uma_zfree(unp_zone, unp); 34075a67bf3SMatt Macy } 34175a67bf3SMatt Macy return (freed); 34275a67bf3SMatt Macy } 34375a67bf3SMatt Macy 34475a67bf3SMatt Macy static void 34575a67bf3SMatt Macy unp_pcb_lock2(struct unpcb *unp, struct unpcb *unp2) 34675a67bf3SMatt Macy { 34716529dacSMatt Macy MPASS(unp != unp2); 34875a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT(unp); 34975a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT(unp2); 35075a67bf3SMatt Macy if ((uintptr_t)unp2 > (uintptr_t)unp) { 35175a67bf3SMatt Macy UNP_PCB_LOCK(unp); 35275a67bf3SMatt Macy UNP_PCB_LOCK(unp2); 35375a67bf3SMatt Macy } else { 35475a67bf3SMatt Macy UNP_PCB_LOCK(unp2); 35575a67bf3SMatt Macy UNP_PCB_LOCK(unp); 35675a67bf3SMatt Macy } 35775a67bf3SMatt Macy } 35875a67bf3SMatt Macy 35975a67bf3SMatt Macy static __noinline void 360e62ca80bSMark Johnston unp_pcb_owned_lock2_slowpath(struct unpcb *unp, struct unpcb **unp2p, 361e62ca80bSMark Johnston int *freed) 36275a67bf3SMatt Macy { 36375a67bf3SMatt Macy struct unpcb *unp2; 36475a67bf3SMatt Macy 36575a67bf3SMatt Macy unp2 = *unp2p; 366e62ca80bSMark Johnston unp_pcb_hold(unp2); 367e62ca80bSMark Johnston UNP_PCB_UNLOCK(unp); 368e62ca80bSMark Johnston UNP_PCB_LOCK(unp2); 369e62ca80bSMark Johnston UNP_PCB_LOCK(unp); 370e62ca80bSMark Johnston *freed = unp_pcb_rele(unp2); 37175a67bf3SMatt Macy if (*freed) 37275a67bf3SMatt Macy *unp2p = NULL; 37375a67bf3SMatt Macy } 37475a67bf3SMatt Macy 37575a67bf3SMatt Macy #define unp_pcb_owned_lock2(unp, unp2, freed) do { \ 37675a67bf3SMatt Macy freed = 0; \ 377e62ca80bSMark Johnston UNP_PCB_LOCK_ASSERT(unp); \ 378e62ca80bSMark Johnston UNP_PCB_UNLOCK_ASSERT(unp2); \ 379e62ca80bSMark Johnston MPASS((unp) != (unp2)); \ 380e62ca80bSMark Johnston if (__predict_true(UNP_PCB_TRYLOCK(unp2))) \ 38175a67bf3SMatt Macy break; \ 38275a67bf3SMatt Macy else if ((uintptr_t)(unp2) > (uintptr_t)(unp)) \ 383e62ca80bSMark Johnston UNP_PCB_LOCK(unp2); \ 384e62ca80bSMark Johnston else \ 38575a67bf3SMatt Macy unp_pcb_owned_lock2_slowpath((unp), &(unp2), &freed); \ 38675a67bf3SMatt Macy } while (0) 38775a67bf3SMatt Macy 38875a67bf3SMatt Macy 389e4445a03SRobert Watson /* 390e4445a03SRobert Watson * Definitions of protocols supported in the LOCAL domain. 391e4445a03SRobert Watson */ 392e4445a03SRobert Watson static struct domain localdomain; 393fa9402f2SRobert Watson static struct pr_usrreqs uipc_usrreqs_dgram, uipc_usrreqs_stream; 39484d61770SRobert Watson static struct pr_usrreqs uipc_usrreqs_seqpacket; 395e4445a03SRobert Watson static struct protosw localsw[] = { 396e4445a03SRobert Watson { 397e4445a03SRobert Watson .pr_type = SOCK_STREAM, 398e4445a03SRobert Watson .pr_domain = &localdomain, 399e4445a03SRobert Watson .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_RIGHTS, 400e4445a03SRobert Watson .pr_ctloutput = &uipc_ctloutput, 401fa9402f2SRobert Watson .pr_usrreqs = &uipc_usrreqs_stream 402e4445a03SRobert Watson }, 403e4445a03SRobert Watson { 404e4445a03SRobert Watson .pr_type = SOCK_DGRAM, 405e4445a03SRobert Watson .pr_domain = &localdomain, 406e4445a03SRobert Watson .pr_flags = PR_ATOMIC|PR_ADDR|PR_RIGHTS, 407aaf63435SGleb Smirnoff .pr_ctloutput = &uipc_ctloutput, 408fa9402f2SRobert Watson .pr_usrreqs = &uipc_usrreqs_dgram 409e4445a03SRobert Watson }, 41084d61770SRobert Watson { 41184d61770SRobert Watson .pr_type = SOCK_SEQPACKET, 41284d61770SRobert Watson .pr_domain = &localdomain, 41384d61770SRobert Watson 41484d61770SRobert Watson /* 41584d61770SRobert Watson * XXXRW: For now, PR_ADDR because soreceive will bump into them 41684d61770SRobert Watson * due to our use of sbappendaddr. A new sbappend variants is needed 41784d61770SRobert Watson * that supports both atomic record writes and control data. 41884d61770SRobert Watson */ 41984d61770SRobert Watson .pr_flags = PR_ADDR|PR_ATOMIC|PR_CONNREQUIRED|PR_WANTRCVD| 42084d61770SRobert Watson PR_RIGHTS, 421e0643280SGleb Smirnoff .pr_ctloutput = &uipc_ctloutput, 42284d61770SRobert Watson .pr_usrreqs = &uipc_usrreqs_seqpacket, 42384d61770SRobert Watson }, 424e4445a03SRobert Watson }; 425e4445a03SRobert Watson 426e4445a03SRobert Watson static struct domain localdomain = { 427e4445a03SRobert Watson .dom_family = AF_LOCAL, 428e4445a03SRobert Watson .dom_name = "local", 429e4445a03SRobert Watson .dom_init = unp_init, 430e4445a03SRobert Watson .dom_externalize = unp_externalize, 43199ab95dbSMark Johnston .dom_dispose = unp_dispose, 432e4445a03SRobert Watson .dom_protosw = localsw, 43302abd400SPedro F. Giffuni .dom_protoswNPROTOSW = &localsw[nitems(localsw)] 434e4445a03SRobert Watson }; 435e4445a03SRobert Watson DOMAIN_SET(local); 436e4445a03SRobert Watson 437ac45e92fSRobert Watson static void 438a29f300eSGarrett Wollman uipc_abort(struct socket *so) 439df8bae1dSRodney W. Grimes { 440e7c33e29SRobert Watson struct unpcb *unp, *unp2; 441df8bae1dSRodney W. Grimes 44240f2ac28SRobert Watson unp = sotounpcb(so); 4434d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_abort: unp == NULL")); 44475a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT(unp); 445e7c33e29SRobert Watson 446e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 447e7c33e29SRobert Watson unp2 = unp->unp_conn; 448e7c33e29SRobert Watson if (unp2 != NULL) { 44975a67bf3SMatt Macy unp_pcb_hold(unp2); 450e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 45175a67bf3SMatt Macy unp_drop(unp2); 45275a67bf3SMatt Macy } else 45375a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 454df8bae1dSRodney W. Grimes } 455df8bae1dSRodney W. Grimes 456a29f300eSGarrett Wollman static int 45757bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam) 458a29f300eSGarrett Wollman { 459e7c33e29SRobert Watson struct unpcb *unp, *unp2; 4600d9ce3a1SRobert Watson const struct sockaddr *sa; 461df8bae1dSRodney W. Grimes 462df8bae1dSRodney W. Grimes /* 4631c381b19SRobert Watson * Pass back name of connected socket, if it was bound and we are 4641c381b19SRobert Watson * still connected (our peer may have closed already!). 465df8bae1dSRodney W. Grimes */ 4664d4b555eSRobert Watson unp = sotounpcb(so); 4674d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_accept: unp == NULL")); 468e7c33e29SRobert Watson 4690d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 4703dab55bcSRobert Watson UNP_LINK_RLOCK(); 471e7c33e29SRobert Watson unp2 = unp->unp_conn; 472e7c33e29SRobert Watson if (unp2 != NULL && unp2->unp_addr != NULL) { 473e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 474e7c33e29SRobert Watson sa = (struct sockaddr *) unp2->unp_addr; 475e7c33e29SRobert Watson bcopy(sa, *nam, sa->sa_len); 476e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 477e7c33e29SRobert Watson } else { 4780d9ce3a1SRobert Watson sa = &sun_noname; 4790d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 480e7c33e29SRobert Watson } 4813dab55bcSRobert Watson UNP_LINK_RUNLOCK(); 482e5aeaa0cSDag-Erling Smørgrav return (0); 483a29f300eSGarrett Wollman } 484df8bae1dSRodney W. Grimes 485a29f300eSGarrett Wollman static int 486b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td) 487a29f300eSGarrett Wollman { 488e7c33e29SRobert Watson u_long sendspace, recvspace; 4896d32873cSRobert Watson struct unpcb *unp; 4903dab55bcSRobert Watson int error; 491779f106aSGleb Smirnoff bool locked; 492df8bae1dSRodney W. Grimes 4936d32873cSRobert Watson KASSERT(so->so_pcb == NULL, ("uipc_attach: so_pcb != NULL")); 4946d32873cSRobert Watson if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 4956d32873cSRobert Watson switch (so->so_type) { 4966d32873cSRobert Watson case SOCK_STREAM: 497e7c33e29SRobert Watson sendspace = unpst_sendspace; 498e7c33e29SRobert Watson recvspace = unpst_recvspace; 4996d32873cSRobert Watson break; 5006d32873cSRobert Watson 5016d32873cSRobert Watson case SOCK_DGRAM: 502e7c33e29SRobert Watson sendspace = unpdg_sendspace; 503e7c33e29SRobert Watson recvspace = unpdg_recvspace; 5046d32873cSRobert Watson break; 5056d32873cSRobert Watson 50684d61770SRobert Watson case SOCK_SEQPACKET: 50784d61770SRobert Watson sendspace = unpsp_sendspace; 50884d61770SRobert Watson recvspace = unpsp_recvspace; 50984d61770SRobert Watson break; 51084d61770SRobert Watson 5116d32873cSRobert Watson default: 512e7c33e29SRobert Watson panic("uipc_attach"); 5136d32873cSRobert Watson } 514e7c33e29SRobert Watson error = soreserve(so, sendspace, recvspace); 5156d32873cSRobert Watson if (error) 5166d32873cSRobert Watson return (error); 5176d32873cSRobert Watson } 51846a1d9bfSRobert Watson unp = uma_zalloc(unp_zone, M_NOWAIT | M_ZERO); 5196d32873cSRobert Watson if (unp == NULL) 5206d32873cSRobert Watson return (ENOBUFS); 5216d32873cSRobert Watson LIST_INIT(&unp->unp_refs); 522e7c33e29SRobert Watson UNP_PCB_LOCK_INIT(unp); 5236d32873cSRobert Watson unp->unp_socket = so; 5246d32873cSRobert Watson so->so_pcb = unp; 5259ae328fcSJohn Baldwin unp->unp_refcount = 1; 526779f106aSGleb Smirnoff if (so->so_listen != NULL) 527434ac8b6SMark Johnston unp->unp_flags |= UNP_NASCENT; 528e7c33e29SRobert Watson 529779f106aSGleb Smirnoff if ((locked = UNP_LINK_WOWNED()) == false) 530779f106aSGleb Smirnoff UNP_LINK_WLOCK(); 531779f106aSGleb Smirnoff 5326d32873cSRobert Watson unp->unp_gencnt = ++unp_gencnt; 533f218ac50SMateusz Guzik unp->unp_ino = ++unp_ino; 5346d32873cSRobert Watson unp_count++; 53584d61770SRobert Watson switch (so->so_type) { 53684d61770SRobert Watson case SOCK_STREAM: 53784d61770SRobert Watson LIST_INSERT_HEAD(&unp_shead, unp, unp_link); 53884d61770SRobert Watson break; 53984d61770SRobert Watson 54084d61770SRobert Watson case SOCK_DGRAM: 54184d61770SRobert Watson LIST_INSERT_HEAD(&unp_dhead, unp, unp_link); 54284d61770SRobert Watson break; 54384d61770SRobert Watson 54484d61770SRobert Watson case SOCK_SEQPACKET: 54584d61770SRobert Watson LIST_INSERT_HEAD(&unp_sphead, unp, unp_link); 54684d61770SRobert Watson break; 54784d61770SRobert Watson 54884d61770SRobert Watson default: 54984d61770SRobert Watson panic("uipc_attach"); 55084d61770SRobert Watson } 551779f106aSGleb Smirnoff 552779f106aSGleb Smirnoff if (locked == false) 553779f106aSGleb Smirnoff UNP_LINK_WUNLOCK(); 5546d32873cSRobert Watson 5556d32873cSRobert Watson return (0); 556a29f300eSGarrett Wollman } 557a29f300eSGarrett Wollman 558a29f300eSGarrett Wollman static int 5597493f24eSPawel Jakub Dawidek uipc_bindat(int fd, struct socket *so, struct sockaddr *nam, struct thread *td) 560a29f300eSGarrett Wollman { 561dd47f5caSRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam; 562dd47f5caSRobert Watson struct vattr vattr; 5635050aa86SKonstantin Belousov int error, namelen; 564dd47f5caSRobert Watson struct nameidata nd; 56540f2ac28SRobert Watson struct unpcb *unp; 566dd47f5caSRobert Watson struct vnode *vp; 567dd47f5caSRobert Watson struct mount *mp; 5687008be5bSPawel Jakub Dawidek cap_rights_t rights; 569dd47f5caSRobert Watson char *buf; 570a29f300eSGarrett Wollman 571cb7df69bSKevin Lo if (nam->sa_family != AF_UNIX) 572cb7df69bSKevin Lo return (EAFNOSUPPORT); 573cb7df69bSKevin Lo 57440f2ac28SRobert Watson unp = sotounpcb(so); 5754d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_bind: unp == NULL")); 5764f1f0ef5SRobert Watson 577a06534c3SBjoern A. Zeeb if (soun->sun_len > sizeof(struct sockaddr_un)) 578a06534c3SBjoern A. Zeeb return (EINVAL); 5794f1f0ef5SRobert Watson namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 5804f1f0ef5SRobert Watson if (namelen <= 0) 5814f1f0ef5SRobert Watson return (EINVAL); 582dd47f5caSRobert Watson 583dd47f5caSRobert Watson /* 5844f1f0ef5SRobert Watson * We don't allow simultaneous bind() calls on a single UNIX domain 5854f1f0ef5SRobert Watson * socket, so flag in-progress operations, and return an error if an 5864f1f0ef5SRobert Watson * operation is already in progress. 5874f1f0ef5SRobert Watson * 5884f1f0ef5SRobert Watson * Historically, we have not allowed a socket to be rebound, so this 589d7924b70SRobert Watson * also returns an error. Not allowing re-binding simplifies the 590d7924b70SRobert Watson * implementation and avoids a great many possible failure modes. 591dd47f5caSRobert Watson */ 592e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 593dd47f5caSRobert Watson if (unp->unp_vnode != NULL) { 594e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 595dd47f5caSRobert Watson return (EINVAL); 596dd47f5caSRobert Watson } 5974f1f0ef5SRobert Watson if (unp->unp_flags & UNP_BINDING) { 598e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 5994f1f0ef5SRobert Watson return (EALREADY); 600dd47f5caSRobert Watson } 6014f1f0ef5SRobert Watson unp->unp_flags |= UNP_BINDING; 602e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 603dd47f5caSRobert Watson 604dd47f5caSRobert Watson buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 6057928893dSEd Maste bcopy(soun->sun_path, buf, namelen); 6067928893dSEd Maste buf[namelen] = 0; 607dd47f5caSRobert Watson 608dd47f5caSRobert Watson restart: 6096c21f6edSKonstantin Belousov NDINIT_ATRIGHTS(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME | NOCACHE, 6107008be5bSPawel Jakub Dawidek UIO_SYSSPACE, buf, fd, cap_rights_init(&rights, CAP_BINDAT), td); 611dd47f5caSRobert Watson /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 612dd47f5caSRobert Watson error = namei(&nd); 613dd47f5caSRobert Watson if (error) 6144f1f0ef5SRobert Watson goto error; 615dd47f5caSRobert Watson vp = nd.ni_vp; 616dd47f5caSRobert Watson if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 617dd47f5caSRobert Watson NDFREE(&nd, NDF_ONLY_PNBUF); 618dd47f5caSRobert Watson if (nd.ni_dvp == vp) 619dd47f5caSRobert Watson vrele(nd.ni_dvp); 620dd47f5caSRobert Watson else 621dd47f5caSRobert Watson vput(nd.ni_dvp); 622dd47f5caSRobert Watson if (vp != NULL) { 623dd47f5caSRobert Watson vrele(vp); 624dd47f5caSRobert Watson error = EADDRINUSE; 6254f1f0ef5SRobert Watson goto error; 626dd47f5caSRobert Watson } 627dd47f5caSRobert Watson error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 628dd47f5caSRobert Watson if (error) 6294f1f0ef5SRobert Watson goto error; 630dd47f5caSRobert Watson goto restart; 631dd47f5caSRobert Watson } 632dd47f5caSRobert Watson VATTR_NULL(&vattr); 633dd47f5caSRobert Watson vattr.va_type = VSOCK; 634dd47f5caSRobert Watson vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 635dd47f5caSRobert Watson #ifdef MAC 63630d239bcSRobert Watson error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 637dd47f5caSRobert Watson &vattr); 638dd47f5caSRobert Watson #endif 639885868cdSRobert Watson if (error == 0) 640dd47f5caSRobert Watson error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 641dd47f5caSRobert Watson NDFREE(&nd, NDF_ONLY_PNBUF); 642dd47f5caSRobert Watson vput(nd.ni_dvp); 643dd47f5caSRobert Watson if (error) { 644dd47f5caSRobert Watson vn_finished_write(mp); 6454f1f0ef5SRobert Watson goto error; 646dd47f5caSRobert Watson } 647dd47f5caSRobert Watson vp = nd.ni_vp; 64857fd3d55SPawel Jakub Dawidek ASSERT_VOP_ELOCKED(vp, "uipc_bind"); 649dd47f5caSRobert Watson soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 650e7c33e29SRobert Watson 651e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 6520c3c207fSGleb Smirnoff VOP_UNP_BIND(vp, unp); 653dd47f5caSRobert Watson unp->unp_vnode = vp; 654dd47f5caSRobert Watson unp->unp_addr = soun; 6554f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_BINDING; 656e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 65722db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 658dd47f5caSRobert Watson vn_finished_write(mp); 6594f1f0ef5SRobert Watson free(buf, M_TEMP); 6604f1f0ef5SRobert Watson return (0); 661e7c33e29SRobert Watson 6624f1f0ef5SRobert Watson error: 663e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 6644f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_BINDING; 665e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 666dd47f5caSRobert Watson free(buf, M_TEMP); 66740f2ac28SRobert Watson return (error); 668a29f300eSGarrett Wollman } 669a29f300eSGarrett Wollman 670a29f300eSGarrett Wollman static int 6717493f24eSPawel Jakub Dawidek uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 6727493f24eSPawel Jakub Dawidek { 6737493f24eSPawel Jakub Dawidek 6747493f24eSPawel Jakub Dawidek return (uipc_bindat(AT_FDCWD, so, nam, td)); 6757493f24eSPawel Jakub Dawidek } 6767493f24eSPawel Jakub Dawidek 6777493f24eSPawel Jakub Dawidek static int 678b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 679a29f300eSGarrett Wollman { 6800d9ce3a1SRobert Watson int error; 681a29f300eSGarrett Wollman 682fd179ee9SRobert Watson KASSERT(td == curthread, ("uipc_connect: td != curthread")); 683fd179ee9SRobert Watson error = unp_connect(so, nam, td); 6840d9ce3a1SRobert Watson return (error); 685a29f300eSGarrett Wollman } 686a29f300eSGarrett Wollman 6877493f24eSPawel Jakub Dawidek static int 6887493f24eSPawel Jakub Dawidek uipc_connectat(int fd, struct socket *so, struct sockaddr *nam, 6897493f24eSPawel Jakub Dawidek struct thread *td) 6907493f24eSPawel Jakub Dawidek { 6917493f24eSPawel Jakub Dawidek int error; 6927493f24eSPawel Jakub Dawidek 6937493f24eSPawel Jakub Dawidek KASSERT(td == curthread, ("uipc_connectat: td != curthread")); 6947493f24eSPawel Jakub Dawidek error = unp_connectat(fd, so, nam, td); 6957493f24eSPawel Jakub Dawidek return (error); 6967493f24eSPawel Jakub Dawidek } 6977493f24eSPawel Jakub Dawidek 698a152f8a3SRobert Watson static void 699a152f8a3SRobert Watson uipc_close(struct socket *so) 700a152f8a3SRobert Watson { 701e7c33e29SRobert Watson struct unpcb *unp, *unp2; 702779f106aSGleb Smirnoff struct vnode *vp = NULL; 70375a67bf3SMatt Macy struct mtx *vplock; 70475a67bf3SMatt Macy int freed; 705a152f8a3SRobert Watson unp = sotounpcb(so); 706a152f8a3SRobert Watson KASSERT(unp != NULL, ("uipc_close: unp == NULL")); 707e7c33e29SRobert Watson 70875a67bf3SMatt Macy 70975a67bf3SMatt Macy vplock = NULL; 71075a67bf3SMatt Macy if ((vp = unp->unp_vnode) != NULL) { 71175a67bf3SMatt Macy vplock = mtx_pool_find(mtxpool_sleep, vp); 71275a67bf3SMatt Macy mtx_lock(vplock); 713e7c33e29SRobert Watson } 71475a67bf3SMatt Macy UNP_PCB_LOCK(unp); 71575a67bf3SMatt Macy if (vp && unp->unp_vnode == NULL) { 71675a67bf3SMatt Macy mtx_unlock(vplock); 71775a67bf3SMatt Macy vp = NULL; 71875a67bf3SMatt Macy } 71975a67bf3SMatt Macy if (vp != NULL) { 720779f106aSGleb Smirnoff VOP_UNP_DETACH(vp); 721779f106aSGleb Smirnoff unp->unp_vnode = NULL; 722779f106aSGleb Smirnoff } 72375a67bf3SMatt Macy unp2 = unp->unp_conn; 72475a67bf3SMatt Macy unp_pcb_hold(unp); 725acf9fd05SMatt Macy if (__predict_false(unp == unp2)) { 726acf9fd05SMatt Macy unp_disconnect(unp, unp2); 727acf9fd05SMatt Macy } else if (unp2 != NULL) { 72875a67bf3SMatt Macy unp_pcb_hold(unp2); 72975a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freed); 73075a67bf3SMatt Macy unp_disconnect(unp, unp2); 73175a67bf3SMatt Macy if (unp_pcb_rele(unp2) == 0) 73275a67bf3SMatt Macy UNP_PCB_UNLOCK(unp2); 73375a67bf3SMatt Macy } 73475a67bf3SMatt Macy if (unp_pcb_rele(unp) == 0) 735e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 73675a67bf3SMatt Macy if (vp) { 73775a67bf3SMatt Macy mtx_unlock(vplock); 738779f106aSGleb Smirnoff vrele(vp); 739a152f8a3SRobert Watson } 74075a67bf3SMatt Macy } 741a152f8a3SRobert Watson 7422c899584SRobert Watson static int 743a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2) 744a29f300eSGarrett Wollman { 745e7c33e29SRobert Watson struct unpcb *unp, *unp2; 7460d9ce3a1SRobert Watson int error; 747a29f300eSGarrett Wollman 748e7c33e29SRobert Watson unp = so1->so_pcb; 7494d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_connect2: unp == NULL")); 750e7c33e29SRobert Watson unp2 = so2->so_pcb; 751e7c33e29SRobert Watson KASSERT(unp2 != NULL, ("uipc_connect2: unp2 == NULL")); 752acf9fd05SMatt Macy if (unp != unp2) 75375a67bf3SMatt Macy unp_pcb_lock2(unp, unp2); 754acf9fd05SMatt Macy else 755acf9fd05SMatt Macy UNP_PCB_LOCK(unp); 7566a2989fdSMatthew N. Dodd error = unp_connect2(so1, so2, PRU_CONNECT2); 757acf9fd05SMatt Macy if (unp != unp2) 758e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 759e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 7600d9ce3a1SRobert Watson return (error); 761a29f300eSGarrett Wollman } 762a29f300eSGarrett Wollman 763bc725eafSRobert Watson static void 764a29f300eSGarrett Wollman uipc_detach(struct socket *so) 765a29f300eSGarrett Wollman { 766e7c33e29SRobert Watson struct unpcb *unp, *unp2; 76775a67bf3SMatt Macy struct mtx *vplock; 7689ae328fcSJohn Baldwin struct sockaddr_un *saved_unp_addr; 7696d32873cSRobert Watson struct vnode *vp; 7709ae328fcSJohn Baldwin int freeunp, local_unp_rights; 771a29f300eSGarrett Wollman 77240f2ac28SRobert Watson unp = sotounpcb(so); 7734d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_detach: unp == NULL")); 774e7c33e29SRobert Watson 775434ac8b6SMark Johnston vp = NULL; 776c0874c34SMatt Macy vplock = NULL; 777434ac8b6SMark Johnston local_unp_rights = 0; 778434ac8b6SMark Johnston 779779f106aSGleb Smirnoff UNP_LINK_WLOCK(); 7806d32873cSRobert Watson LIST_REMOVE(unp, unp_link); 7816d32873cSRobert Watson unp->unp_gencnt = ++unp_gencnt; 7826d32873cSRobert Watson --unp_count; 78375a67bf3SMatt Macy UNP_LINK_WUNLOCK(); 784434ac8b6SMark Johnston 78575a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT(unp); 78675a67bf3SMatt Macy restart: 78775a67bf3SMatt Macy if ((vp = unp->unp_vnode) != NULL) { 78875a67bf3SMatt Macy vplock = mtx_pool_find(mtxpool_sleep, vp); 78975a67bf3SMatt Macy mtx_lock(vplock); 79075a67bf3SMatt Macy } 79175a67bf3SMatt Macy UNP_PCB_LOCK(unp); 79275a67bf3SMatt Macy if (unp->unp_vnode != vp && 79375a67bf3SMatt Macy unp->unp_vnode != NULL) { 794c0874c34SMatt Macy if (vplock) 79575a67bf3SMatt Macy mtx_unlock(vplock); 79675a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 79775a67bf3SMatt Macy goto restart; 79875a67bf3SMatt Macy } 79975a67bf3SMatt Macy if ((unp->unp_flags & UNP_NASCENT) != 0) { 80075a67bf3SMatt Macy goto teardown; 80175a67bf3SMatt Macy } 8026d32873cSRobert Watson if ((vp = unp->unp_vnode) != NULL) { 803c7e41c8bSMikolaj Golub VOP_UNP_DETACH(vp); 8046d32873cSRobert Watson unp->unp_vnode = NULL; 8056d32873cSRobert Watson } 806acf9fd05SMatt Macy if (__predict_false(unp == unp->unp_conn)) { 807acf9fd05SMatt Macy unp_disconnect(unp, unp); 808acf9fd05SMatt Macy unp2 = NULL; 809acf9fd05SMatt Macy goto connect_self; 810acf9fd05SMatt Macy } 811acf9fd05SMatt Macy if ((unp2 = unp->unp_conn) != NULL) { 812acf9fd05SMatt Macy unp_pcb_owned_lock2(unp, unp2, freeunp); 813acf9fd05SMatt Macy if (freeunp) 814acf9fd05SMatt Macy unp2 = NULL; 815acf9fd05SMatt Macy } 81675a67bf3SMatt Macy unp_pcb_hold(unp); 817e7c33e29SRobert Watson if (unp2 != NULL) { 81875a67bf3SMatt Macy unp_pcb_hold(unp2); 819e7c33e29SRobert Watson unp_disconnect(unp, unp2); 82075a67bf3SMatt Macy if (unp_pcb_rele(unp2) == 0) 821e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 822e7c33e29SRobert Watson } 823acf9fd05SMatt Macy connect_self: 82475a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 82575a67bf3SMatt Macy UNP_REF_LIST_LOCK(); 8266d32873cSRobert Watson while (!LIST_EMPTY(&unp->unp_refs)) { 8276d32873cSRobert Watson struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 828e7c33e29SRobert Watson 82975a67bf3SMatt Macy unp_pcb_hold(ref); 83075a67bf3SMatt Macy UNP_REF_LIST_UNLOCK(); 83175a67bf3SMatt Macy 83275a67bf3SMatt Macy MPASS(ref != unp); 83375a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT(ref); 834afc055d9SEd Schouten unp_drop(ref); 83575a67bf3SMatt Macy UNP_REF_LIST_LOCK(); 8366d32873cSRobert Watson } 83775a67bf3SMatt Macy 83875a67bf3SMatt Macy UNP_REF_LIST_UNLOCK(); 83975a67bf3SMatt Macy UNP_PCB_LOCK(unp); 84075a67bf3SMatt Macy freeunp = unp_pcb_rele(unp); 84175a67bf3SMatt Macy MPASS(freeunp == 0); 842397c19d1SJeff Roberson local_unp_rights = unp_rights; 843434ac8b6SMark Johnston teardown: 8446d32873cSRobert Watson unp->unp_socket->so_pcb = NULL; 8459ae328fcSJohn Baldwin saved_unp_addr = unp->unp_addr; 8469ae328fcSJohn Baldwin unp->unp_addr = NULL; 84775a67bf3SMatt Macy unp->unp_socket = NULL; 84875a67bf3SMatt Macy freeunp = unp_pcb_rele(unp); 8499ae328fcSJohn Baldwin if (saved_unp_addr != NULL) 8501ede983cSDag-Erling Smørgrav free(saved_unp_addr, M_SONAME); 85175a67bf3SMatt Macy if (!freeunp) 8526e2faa24SRobert Watson UNP_PCB_UNLOCK(unp); 85375a67bf3SMatt Macy if (vp) { 85475a67bf3SMatt Macy mtx_unlock(vplock); 8556d32873cSRobert Watson vrele(vp); 85675a67bf3SMatt Macy } 8576d32873cSRobert Watson if (local_unp_rights) 858daee0f0bSKonstantin Belousov taskqueue_enqueue_timeout(taskqueue_thread, &unp_gc_task, -1); 859a29f300eSGarrett Wollman } 860a29f300eSGarrett Wollman 861a29f300eSGarrett Wollman static int 862a29f300eSGarrett Wollman uipc_disconnect(struct socket *so) 863a29f300eSGarrett Wollman { 864e7c33e29SRobert Watson struct unpcb *unp, *unp2; 86575a67bf3SMatt Macy int freed; 866a29f300eSGarrett Wollman 86740f2ac28SRobert Watson unp = sotounpcb(so); 8684d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_disconnect: unp == NULL")); 869e7c33e29SRobert Watson 870e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 87175a67bf3SMatt Macy if ((unp2 = unp->unp_conn) == NULL) { 872e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 87375a67bf3SMatt Macy return (0); 87475a67bf3SMatt Macy } 875fcabd541SMatt Macy if (__predict_true(unp != unp2)) { 87675a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freed); 87775a67bf3SMatt Macy if (__predict_false(freed)) { 87875a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 87975a67bf3SMatt Macy return (0); 88075a67bf3SMatt Macy } 88175a67bf3SMatt Macy unp_pcb_hold(unp2); 882fcabd541SMatt Macy } 88375a67bf3SMatt Macy unp_pcb_hold(unp); 88475a67bf3SMatt Macy unp_disconnect(unp, unp2); 88575a67bf3SMatt Macy if (unp_pcb_rele(unp) == 0) 88675a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 887fcabd541SMatt Macy if ((unp != unp2) && unp_pcb_rele(unp2) == 0) 88875a67bf3SMatt Macy UNP_PCB_UNLOCK(unp2); 889e5aeaa0cSDag-Erling Smørgrav return (0); 890a29f300eSGarrett Wollman } 891a29f300eSGarrett Wollman 892a29f300eSGarrett Wollman static int 893d374e81eSRobert Watson uipc_listen(struct socket *so, int backlog, struct thread *td) 894a29f300eSGarrett Wollman { 89540f2ac28SRobert Watson struct unpcb *unp; 8960d9ce3a1SRobert Watson int error; 897a29f300eSGarrett Wollman 898beb4b312SGleb Smirnoff if (so->so_type != SOCK_STREAM && so->so_type != SOCK_SEQPACKET) 899beb4b312SGleb Smirnoff return (EOPNOTSUPP); 900beb4b312SGleb Smirnoff 90140f2ac28SRobert Watson unp = sotounpcb(so); 9024d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_listen: unp == NULL")); 903e7c33e29SRobert Watson 904e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 9054d4b555eSRobert Watson if (unp->unp_vnode == NULL) { 90647a84387SEd Schouten /* Already connected or not bound to an address. */ 90747a84387SEd Schouten error = unp->unp_conn != NULL ? EINVAL : EDESTADDRREQ; 908e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 90947a84387SEd Schouten return (error); 91040f2ac28SRobert Watson } 911e7c33e29SRobert Watson 912e7c33e29SRobert Watson SOCK_LOCK(so); 913e7c33e29SRobert Watson error = solisten_proto_check(so); 914e7c33e29SRobert Watson if (error == 0) { 915e7c33e29SRobert Watson cru2x(td->td_ucred, &unp->unp_peercred); 916e7c33e29SRobert Watson solisten_proto(so, backlog); 917e7c33e29SRobert Watson } 918e7c33e29SRobert Watson SOCK_UNLOCK(so); 919e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 9200d9ce3a1SRobert Watson return (error); 921a29f300eSGarrett Wollman } 922a29f300eSGarrett Wollman 923a29f300eSGarrett Wollman static int 92457bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 925a29f300eSGarrett Wollman { 926e7c33e29SRobert Watson struct unpcb *unp, *unp2; 9270d9ce3a1SRobert Watson const struct sockaddr *sa; 928a29f300eSGarrett Wollman 9294d4b555eSRobert Watson unp = sotounpcb(so); 9304d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL")); 931e7c33e29SRobert Watson 9320d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 933afd9f91cSJohn Baldwin UNP_LINK_RLOCK(); 934bdc5f6a3SHajimu UMEMOTO /* 935e7c33e29SRobert Watson * XXX: It seems that this test always fails even when connection is 936e7c33e29SRobert Watson * established. So, this else clause is added as workaround to 937e7c33e29SRobert Watson * return PF_LOCAL sockaddr. 938bdc5f6a3SHajimu UMEMOTO */ 939e7c33e29SRobert Watson unp2 = unp->unp_conn; 940e7c33e29SRobert Watson if (unp2 != NULL) { 941e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 942e7c33e29SRobert Watson if (unp2->unp_addr != NULL) 943afd9f91cSJohn Baldwin sa = (struct sockaddr *) unp2->unp_addr; 944e7c33e29SRobert Watson else 9450d9ce3a1SRobert Watson sa = &sun_noname; 9460d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 947e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 948e7c33e29SRobert Watson } else { 949e7c33e29SRobert Watson sa = &sun_noname; 950e7c33e29SRobert Watson bcopy(sa, *nam, sa->sa_len); 951e7c33e29SRobert Watson } 952afd9f91cSJohn Baldwin UNP_LINK_RUNLOCK(); 953e5aeaa0cSDag-Erling Smørgrav return (0); 954a29f300eSGarrett Wollman } 955a29f300eSGarrett Wollman 956a29f300eSGarrett Wollman static int 957a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 958a29f300eSGarrett Wollman { 959e7c33e29SRobert Watson struct unpcb *unp, *unp2; 960a29f300eSGarrett Wollman struct socket *so2; 961337cc6b6SRobert Watson u_int mbcnt, sbcc; 962a29f300eSGarrett Wollman 96340f2ac28SRobert Watson unp = sotounpcb(so); 9642b21d0e8SGleb Smirnoff KASSERT(unp != NULL, ("%s: unp == NULL", __func__)); 9652b21d0e8SGleb Smirnoff KASSERT(so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET, 9662b21d0e8SGleb Smirnoff ("%s: socktype %d", __func__, so->so_type)); 967e7c33e29SRobert Watson 968df8bae1dSRodney W. Grimes /* 969e7c33e29SRobert Watson * Adjust backpressure on sender and wakeup any waiting to write. 970e7c33e29SRobert Watson * 971d7924b70SRobert Watson * The unp lock is acquired to maintain the validity of the unp_conn 972d7924b70SRobert Watson * pointer; no lock on unp2 is required as unp2->unp_socket will be 973d7924b70SRobert Watson * static as long as we don't permit unp2 to disconnect from unp, 974d7924b70SRobert Watson * which is prevented by the lock on unp. We cache values from 975d7924b70SRobert Watson * so_rcv to avoid holding the so_rcv lock over the entire 976d7924b70SRobert Watson * transaction on the remote so_snd. 977df8bae1dSRodney W. Grimes */ 978337cc6b6SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 979337cc6b6SRobert Watson mbcnt = so->so_rcv.sb_mbcnt; 9802b21d0e8SGleb Smirnoff sbcc = sbavail(&so->so_rcv); 981337cc6b6SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 982c2090e73SAlan Somers /* 983c2090e73SAlan Somers * There is a benign race condition at this point. If we're planning to 984c2090e73SAlan Somers * clear SB_STOP, but uipc_send is called on the connected socket at 985c2090e73SAlan Somers * this instant, it might add data to the sockbuf and set SB_STOP. Then 986c2090e73SAlan Somers * we would erroneously clear SB_STOP below, even though the sockbuf is 987c2090e73SAlan Somers * full. The race is benign because the only ill effect is to allow the 988c2090e73SAlan Somers * sockbuf to exceed its size limit, and the size limits are not 989c2090e73SAlan Somers * strictly guaranteed anyway. 990c2090e73SAlan Somers */ 991e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 992e7c33e29SRobert Watson unp2 = unp->unp_conn; 993e7c33e29SRobert Watson if (unp2 == NULL) { 994e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 995e7c33e29SRobert Watson return (0); 996337cc6b6SRobert Watson } 997e7c33e29SRobert Watson so2 = unp2->unp_socket; 998337cc6b6SRobert Watson SOCKBUF_LOCK(&so2->so_snd); 999c2090e73SAlan Somers if (sbcc < so2->so_snd.sb_hiwat && mbcnt < so2->so_snd.sb_mbmax) 1000c2090e73SAlan Somers so2->so_snd.sb_flags &= ~SB_STOP; 10011e4d7da7SRobert Watson sowwakeup_locked(so2); 1002e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1003e5aeaa0cSDag-Erling Smørgrav return (0); 1004a29f300eSGarrett Wollman } 1005df8bae1dSRodney W. Grimes 1006a29f300eSGarrett Wollman static int 100775a67bf3SMatt Macy connect_internal(struct socket *so, struct sockaddr *nam, struct thread *td) 100875a67bf3SMatt Macy { 100975a67bf3SMatt Macy int error; 101075a67bf3SMatt Macy struct unpcb *unp; 101175a67bf3SMatt Macy 101275a67bf3SMatt Macy unp = so->so_pcb; 101375a67bf3SMatt Macy if (unp->unp_conn != NULL) 101475a67bf3SMatt Macy return (EISCONN); 101575a67bf3SMatt Macy error = unp_connect(so, nam, td); 101675a67bf3SMatt Macy if (error) 101775a67bf3SMatt Macy return (error); 101875a67bf3SMatt Macy UNP_PCB_LOCK(unp); 101975a67bf3SMatt Macy if (unp->unp_conn == NULL) { 102075a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 102175a67bf3SMatt Macy if (error == 0) 102275a67bf3SMatt Macy error = ENOTCONN; 102375a67bf3SMatt Macy } 102475a67bf3SMatt Macy return (error); 102575a67bf3SMatt Macy } 102675a67bf3SMatt Macy 102775a67bf3SMatt Macy 102875a67bf3SMatt Macy static int 102957bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 1030b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 1031a29f300eSGarrett Wollman { 1032f3f49bbbSRobert Watson struct unpcb *unp, *unp2; 1033a29f300eSGarrett Wollman struct socket *so2; 1034c2090e73SAlan Somers u_int mbcnt, sbcc; 103575a67bf3SMatt Macy int freed, error; 1036a29f300eSGarrett Wollman 103740f2ac28SRobert Watson unp = sotounpcb(so); 10382b21d0e8SGleb Smirnoff KASSERT(unp != NULL, ("%s: unp == NULL", __func__)); 10392b21d0e8SGleb Smirnoff KASSERT(so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM || 10402b21d0e8SGleb Smirnoff so->so_type == SOCK_SEQPACKET, 10412b21d0e8SGleb Smirnoff ("%s: socktype %d", __func__, so->so_type)); 1042e7c33e29SRobert Watson 104375a67bf3SMatt Macy freed = error = 0; 1044a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 1045a29f300eSGarrett Wollman error = EOPNOTSUPP; 1046a29f300eSGarrett Wollman goto release; 1047a29f300eSGarrett Wollman } 1048fc3fcacfSRobert Watson if (control != NULL && (error = unp_internalize(&control, td))) 1049a29f300eSGarrett Wollman goto release; 105075a67bf3SMatt Macy 105175a67bf3SMatt Macy unp2 = NULL; 1052a29f300eSGarrett Wollman switch (so->so_type) { 1053a29f300eSGarrett Wollman case SOCK_DGRAM: 1054a29f300eSGarrett Wollman { 1055e7dd9a10SRobert Watson const struct sockaddr *from; 1056df8bae1dSRodney W. Grimes 1057fc3fcacfSRobert Watson if (nam != NULL) { 105875a67bf3SMatt Macy /* 105975a67bf3SMatt Macy * We return with UNP_PCB_LOCK_HELD so we know that 106075a67bf3SMatt Macy * the reference is live if the pointer is valid. 106175a67bf3SMatt Macy */ 106275a67bf3SMatt Macy if ((error = connect_internal(so, nam, td))) 1063df8bae1dSRodney W. Grimes break; 106475a67bf3SMatt Macy MPASS(unp->unp_conn != NULL); 1065e7c33e29SRobert Watson unp2 = unp->unp_conn; 106675a67bf3SMatt Macy } else { 106775a67bf3SMatt Macy UNP_PCB_LOCK(unp); 106860a5ef26SRobert Watson 1069b5ff0914SRobert Watson /* 1070b5ff0914SRobert Watson * Because connect() and send() are non-atomic in a sendto() 1071b5ff0914SRobert Watson * with a target address, it's possible that the socket will 1072b5ff0914SRobert Watson * have disconnected before the send() can run. In that case 1073b5ff0914SRobert Watson * return the slightly counter-intuitive but otherwise 1074b5ff0914SRobert Watson * correct error that the socket is not connected. 1075b5ff0914SRobert Watson */ 107675a67bf3SMatt Macy if ((unp2 = unp->unp_conn) == NULL) { 107775a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 1078b5ff0914SRobert Watson error = ENOTCONN; 1079b5ff0914SRobert Watson break; 1080b5ff0914SRobert Watson } 108175a67bf3SMatt Macy } 1082c684c14cSMatt Macy if (__predict_false(unp == unp2)) { 1083c684c14cSMatt Macy if (unp->unp_socket == NULL) { 1084c684c14cSMatt Macy error = ENOTCONN; 1085c684c14cSMatt Macy break; 1086c684c14cSMatt Macy } 1087c684c14cSMatt Macy goto connect_self; 1088c684c14cSMatt Macy } 108975a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freed); 109075a67bf3SMatt Macy if (__predict_false(freed)) { 109175a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 109275a67bf3SMatt Macy error = ENOTCONN; 109375a67bf3SMatt Macy break; 109475a67bf3SMatt Macy } 109575a67bf3SMatt Macy /* 109675a67bf3SMatt Macy * The socket referencing unp2 may have been closed 109775a67bf3SMatt Macy * or unp may have been disconnected if the unp lock 109875a67bf3SMatt Macy * was dropped to acquire unp2. 109975a67bf3SMatt Macy */ 110075a67bf3SMatt Macy if (__predict_false(unp->unp_conn == NULL) || 110175a67bf3SMatt Macy unp2->unp_socket == NULL) { 110275a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 110375a67bf3SMatt Macy if (unp_pcb_rele(unp2) == 0) 110475a67bf3SMatt Macy UNP_PCB_UNLOCK(unp2); 110575a67bf3SMatt Macy error = ENOTCONN; 110675a67bf3SMatt Macy break; 110775a67bf3SMatt Macy } 1108c684c14cSMatt Macy connect_self: 1109ede6e136SRobert Watson if (unp2->unp_flags & UNP_WANTCRED) 1110ede6e136SRobert Watson control = unp_addsockcred(td, control); 1111fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 111257bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 1113df8bae1dSRodney W. Grimes else 1114df8bae1dSRodney W. Grimes from = &sun_noname; 1115ede6e136SRobert Watson so2 = unp2->unp_socket; 1116a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 11176dde7ecbSPeter Wemm if (sbappendaddr_locked(&so2->so_rcv, from, m, 11188de34a88SAlan Somers control)) { 11191e4d7da7SRobert Watson sorwakeup_locked(so2); 1120fc3fcacfSRobert Watson m = NULL; 1121fc3fcacfSRobert Watson control = NULL; 1122e5aeaa0cSDag-Erling Smørgrav } else { 1123a34b7046SRobert Watson SOCKBUF_UNLOCK(&so2->so_rcv); 1124df8bae1dSRodney W. Grimes error = ENOBUFS; 1125e5aeaa0cSDag-Erling Smørgrav } 112675a67bf3SMatt Macy if (nam != NULL) 1127e7c33e29SRobert Watson unp_disconnect(unp, unp2); 1128c684c14cSMatt Macy if (__predict_true(unp != unp2)) 1129e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1130ede6e136SRobert Watson UNP_PCB_UNLOCK(unp); 1131df8bae1dSRodney W. Grimes break; 1132df8bae1dSRodney W. Grimes } 1133df8bae1dSRodney W. Grimes 113484d61770SRobert Watson case SOCK_SEQPACKET: 1135df8bae1dSRodney W. Grimes case SOCK_STREAM: 1136402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 1137fc3fcacfSRobert Watson if (nam != NULL) { 113875a67bf3SMatt Macy if ((error = connect_internal(so, nam, td))) 113975a67bf3SMatt Macy break; 1140402cc72dSDavid Greenman } else { 1141402cc72dSDavid Greenman error = ENOTCONN; 1142402cc72dSDavid Greenman break; 1143402cc72dSDavid Greenman } 114475a67bf3SMatt Macy } else if ((unp2 = unp->unp_conn) == NULL) { 114575a67bf3SMatt Macy error = ENOTCONN; 114675a67bf3SMatt Macy break; 114775a67bf3SMatt Macy } else if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 1148df8bae1dSRodney W. Grimes error = EPIPE; 1149df8bae1dSRodney W. Grimes break; 115075a67bf3SMatt Macy } else { 115175a67bf3SMatt Macy UNP_PCB_LOCK(unp); 115275a67bf3SMatt Macy if ((unp2 = unp->unp_conn) == NULL) { 115375a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 1154b5ff0914SRobert Watson error = ENOTCONN; 1155b5ff0914SRobert Watson break; 1156b5ff0914SRobert Watson } 115775a67bf3SMatt Macy } 115875a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freed); 115975a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 116075a67bf3SMatt Macy if (__predict_false(freed)) { 116175a67bf3SMatt Macy error = ENOTCONN; 116275a67bf3SMatt Macy break; 116375a67bf3SMatt Macy } 116475a67bf3SMatt Macy if ((so2 = unp2->unp_socket) == NULL) { 116575a67bf3SMatt Macy UNP_PCB_UNLOCK(unp2); 116675a67bf3SMatt Macy error = ENOTCONN; 116775a67bf3SMatt Macy break; 116875a67bf3SMatt Macy } 1169a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 1170f3f49bbbSRobert Watson if (unp2->unp_flags & UNP_WANTCRED) { 11716a2989fdSMatthew N. Dodd /* 1172716963cbSGleb Smirnoff * Credentials are passed only once on SOCK_STREAM 1173716963cbSGleb Smirnoff * and SOCK_SEQPACKET. 11746a2989fdSMatthew N. Dodd */ 1175f3f49bbbSRobert Watson unp2->unp_flags &= ~UNP_WANTCRED; 11766a2989fdSMatthew N. Dodd control = unp_addsockcred(td, control); 11776a2989fdSMatthew N. Dodd } 11785b0480f2SMark Johnston 1179df8bae1dSRodney W. Grimes /* 11805b0480f2SMark Johnston * Send to paired receive port and wake up readers. Don't 11815b0480f2SMark Johnston * check for space available in the receive buffer if we're 11825b0480f2SMark Johnston * attaching ancillary data; Unix domain sockets only check 11835b0480f2SMark Johnston * for space in the sending sockbuf, and that check is 11845b0480f2SMark Johnston * performed one level up the stack. At that level we cannot 11855b0480f2SMark Johnston * precisely account for the amount of buffer space used 11865b0480f2SMark Johnston * (e.g., because control messages are not yet internalized). 1187df8bae1dSRodney W. Grimes */ 118884d61770SRobert Watson switch (so->so_type) { 118984d61770SRobert Watson case SOCK_STREAM: 1190fc3fcacfSRobert Watson if (control != NULL) { 11915b0480f2SMark Johnston sbappendcontrol_locked(&so2->so_rcv, m, 11925b0480f2SMark Johnston control); 1193fc3fcacfSRobert Watson control = NULL; 1194e7c33e29SRobert Watson } else 1195829fae90SGleb Smirnoff sbappend_locked(&so2->so_rcv, m, flags); 119684d61770SRobert Watson break; 119784d61770SRobert Watson 119884d61770SRobert Watson case SOCK_SEQPACKET: { 119984d61770SRobert Watson const struct sockaddr *from; 120084d61770SRobert Watson 120184d61770SRobert Watson from = &sun_noname; 12028de34a88SAlan Somers if (sbappendaddr_nospacecheck_locked(&so2->so_rcv, 12038de34a88SAlan Somers from, m, control)) 120484d61770SRobert Watson control = NULL; 120584d61770SRobert Watson break; 120684d61770SRobert Watson } 120784d61770SRobert Watson } 120884d61770SRobert Watson 1209c2090e73SAlan Somers mbcnt = so2->so_rcv.sb_mbcnt; 12102b21d0e8SGleb Smirnoff sbcc = sbavail(&so2->so_rcv); 12112b21d0e8SGleb Smirnoff if (sbcc) 1212337cc6b6SRobert Watson sorwakeup_locked(so2); 12132b21d0e8SGleb Smirnoff else 12142b21d0e8SGleb Smirnoff SOCKBUF_UNLOCK(&so2->so_rcv); 1215337cc6b6SRobert Watson 1216c2090e73SAlan Somers /* 1217c2090e73SAlan Somers * The PCB lock on unp2 protects the SB_STOP flag. Without it, 1218c2090e73SAlan Somers * it would be possible for uipc_rcvd to be called at this 1219c2090e73SAlan Somers * point, drain the receiving sockbuf, clear SB_STOP, and then 1220c2090e73SAlan Somers * we would set SB_STOP below. That could lead to an empty 1221c2090e73SAlan Somers * sockbuf having SB_STOP set 1222c2090e73SAlan Somers */ 1223337cc6b6SRobert Watson SOCKBUF_LOCK(&so->so_snd); 1224c2090e73SAlan Somers if (sbcc >= so->so_snd.sb_hiwat || mbcnt >= so->so_snd.sb_mbmax) 1225c2090e73SAlan Somers so->so_snd.sb_flags |= SB_STOP; 12267abe2ac2SAlan Cox SOCKBUF_UNLOCK(&so->so_snd); 1227e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1228fc3fcacfSRobert Watson m = NULL; 1229df8bae1dSRodney W. Grimes break; 1230df8bae1dSRodney W. Grimes } 1231a29f300eSGarrett Wollman 12326b8fda4dSGarrett Wollman /* 123360a5ef26SRobert Watson * PRUS_EOF is equivalent to pru_send followed by pru_shutdown. 12346b8fda4dSGarrett Wollman */ 1235a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 1236ede6e136SRobert Watson UNP_PCB_LOCK(unp); 12376b8fda4dSGarrett Wollman socantsendmore(so); 12386b8fda4dSGarrett Wollman unp_shutdown(unp); 1239e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1240ede6e136SRobert Watson } 1241fc3fcacfSRobert Watson if (control != NULL && error != 0) 124299ab95dbSMark Johnston unp_dispose_mbuf(control); 1243bd508d39SDon Lewis 1244a29f300eSGarrett Wollman release: 1245fc3fcacfSRobert Watson if (control != NULL) 1246a29f300eSGarrett Wollman m_freem(control); 1247100db364SGleb Smirnoff /* 1248100db364SGleb Smirnoff * In case of PRUS_NOTREADY, uipc_ready() is responsible 1249100db364SGleb Smirnoff * for freeing memory. 1250100db364SGleb Smirnoff */ 1251100db364SGleb Smirnoff if (m != NULL && (flags & PRUS_NOTREADY) == 0) 1252a29f300eSGarrett Wollman m_freem(m); 1253e5aeaa0cSDag-Erling Smørgrav return (error); 1254a29f300eSGarrett Wollman } 1255df8bae1dSRodney W. Grimes 1256a29f300eSGarrett Wollman static int 1257c80ea19bSGleb Smirnoff uipc_ready(struct socket *so, struct mbuf *m, int count) 1258c80ea19bSGleb Smirnoff { 1259c80ea19bSGleb Smirnoff struct unpcb *unp, *unp2; 1260c80ea19bSGleb Smirnoff struct socket *so2; 1261c80ea19bSGleb Smirnoff int error; 1262c80ea19bSGleb Smirnoff 1263c80ea19bSGleb Smirnoff unp = sotounpcb(so); 1264c80ea19bSGleb Smirnoff 1265a62b4665SMatt Macy UNP_PCB_LOCK(unp); 1266100db364SGleb Smirnoff if ((unp2 = unp->unp_conn) == NULL) { 1267a62b4665SMatt Macy UNP_PCB_UNLOCK(unp); 1268a62b4665SMatt Macy goto error; 1269100db364SGleb Smirnoff } 1270a62b4665SMatt Macy if (unp != unp2) { 1271a62b4665SMatt Macy if (UNP_PCB_TRYLOCK(unp2) == 0) { 1272a62b4665SMatt Macy unp_pcb_hold(unp2); 1273a62b4665SMatt Macy UNP_PCB_UNLOCK(unp); 1274c80ea19bSGleb Smirnoff UNP_PCB_LOCK(unp2); 1275a62b4665SMatt Macy if (unp_pcb_rele(unp2)) 1276a62b4665SMatt Macy goto error; 1277a62b4665SMatt Macy } else 1278a62b4665SMatt Macy UNP_PCB_UNLOCK(unp); 1279a62b4665SMatt Macy } 1280c80ea19bSGleb Smirnoff so2 = unp2->unp_socket; 1281c80ea19bSGleb Smirnoff 1282c80ea19bSGleb Smirnoff SOCKBUF_LOCK(&so2->so_rcv); 1283c80ea19bSGleb Smirnoff if ((error = sbready(&so2->so_rcv, m, count)) == 0) 1284c80ea19bSGleb Smirnoff sorwakeup_locked(so2); 1285c80ea19bSGleb Smirnoff else 1286c80ea19bSGleb Smirnoff SOCKBUF_UNLOCK(&so2->so_rcv); 1287c80ea19bSGleb Smirnoff 1288c80ea19bSGleb Smirnoff UNP_PCB_UNLOCK(unp2); 1289c80ea19bSGleb Smirnoff 1290c80ea19bSGleb Smirnoff return (error); 1291a62b4665SMatt Macy error: 1292a62b4665SMatt Macy for (int i = 0; i < count; i++) 1293a62b4665SMatt Macy m = m_free(m); 1294a62b4665SMatt Macy return (ECONNRESET); 1295c80ea19bSGleb Smirnoff } 1296c80ea19bSGleb Smirnoff 1297c80ea19bSGleb Smirnoff static int 1298a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 1299a29f300eSGarrett Wollman { 1300c2090e73SAlan Somers struct unpcb *unp; 1301a29f300eSGarrett Wollman 130240f2ac28SRobert Watson unp = sotounpcb(so); 13034d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sense: unp == NULL")); 1304e7c33e29SRobert Watson 1305a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 1306f3732fd1SPoul-Henning Kamp sb->st_dev = NODEV; 1307a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 1308df8bae1dSRodney W. Grimes return (0); 1309a29f300eSGarrett Wollman } 1310df8bae1dSRodney W. Grimes 1311a29f300eSGarrett Wollman static int 1312a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 1313a29f300eSGarrett Wollman { 131440f2ac28SRobert Watson struct unpcb *unp; 1315df8bae1dSRodney W. Grimes 131640f2ac28SRobert Watson unp = sotounpcb(so); 13174d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_shutdown: unp == NULL")); 1318e7c33e29SRobert Watson 1319e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 1320a29f300eSGarrett Wollman socantsendmore(so); 1321a29f300eSGarrett Wollman unp_shutdown(unp); 1322e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1323e5aeaa0cSDag-Erling Smørgrav return (0); 1324a29f300eSGarrett Wollman } 1325df8bae1dSRodney W. Grimes 1326a29f300eSGarrett Wollman static int 132757bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 1328a29f300eSGarrett Wollman { 132940f2ac28SRobert Watson struct unpcb *unp; 13300d9ce3a1SRobert Watson const struct sockaddr *sa; 1331a29f300eSGarrett Wollman 13324d4b555eSRobert Watson unp = sotounpcb(so); 13334d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL")); 1334e7c33e29SRobert Watson 13350d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 1336e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 1337fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 13380d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_addr; 133983f3198bSThomas Moestl else 13400d9ce3a1SRobert Watson sa = &sun_noname; 13410d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 1342e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1343e5aeaa0cSDag-Erling Smørgrav return (0); 1344df8bae1dSRodney W. Grimes } 1345a29f300eSGarrett Wollman 1346fa9402f2SRobert Watson static struct pr_usrreqs uipc_usrreqs_dgram = { 1347756d52a1SPoul-Henning Kamp .pru_abort = uipc_abort, 1348756d52a1SPoul-Henning Kamp .pru_accept = uipc_accept, 1349756d52a1SPoul-Henning Kamp .pru_attach = uipc_attach, 1350756d52a1SPoul-Henning Kamp .pru_bind = uipc_bind, 13517493f24eSPawel Jakub Dawidek .pru_bindat = uipc_bindat, 1352756d52a1SPoul-Henning Kamp .pru_connect = uipc_connect, 13537493f24eSPawel Jakub Dawidek .pru_connectat = uipc_connectat, 1354756d52a1SPoul-Henning Kamp .pru_connect2 = uipc_connect2, 1355756d52a1SPoul-Henning Kamp .pru_detach = uipc_detach, 1356756d52a1SPoul-Henning Kamp .pru_disconnect = uipc_disconnect, 1357756d52a1SPoul-Henning Kamp .pru_listen = uipc_listen, 1358756d52a1SPoul-Henning Kamp .pru_peeraddr = uipc_peeraddr, 1359756d52a1SPoul-Henning Kamp .pru_rcvd = uipc_rcvd, 1360756d52a1SPoul-Henning Kamp .pru_send = uipc_send, 1361756d52a1SPoul-Henning Kamp .pru_sense = uipc_sense, 1362756d52a1SPoul-Henning Kamp .pru_shutdown = uipc_shutdown, 1363756d52a1SPoul-Henning Kamp .pru_sockaddr = uipc_sockaddr, 1364fa9402f2SRobert Watson .pru_soreceive = soreceive_dgram, 1365fa9402f2SRobert Watson .pru_close = uipc_close, 1366fa9402f2SRobert Watson }; 1367fa9402f2SRobert Watson 136884d61770SRobert Watson static struct pr_usrreqs uipc_usrreqs_seqpacket = { 136984d61770SRobert Watson .pru_abort = uipc_abort, 137084d61770SRobert Watson .pru_accept = uipc_accept, 137184d61770SRobert Watson .pru_attach = uipc_attach, 137284d61770SRobert Watson .pru_bind = uipc_bind, 13737493f24eSPawel Jakub Dawidek .pru_bindat = uipc_bindat, 137484d61770SRobert Watson .pru_connect = uipc_connect, 13757493f24eSPawel Jakub Dawidek .pru_connectat = uipc_connectat, 137684d61770SRobert Watson .pru_connect2 = uipc_connect2, 137784d61770SRobert Watson .pru_detach = uipc_detach, 137884d61770SRobert Watson .pru_disconnect = uipc_disconnect, 137984d61770SRobert Watson .pru_listen = uipc_listen, 138084d61770SRobert Watson .pru_peeraddr = uipc_peeraddr, 138184d61770SRobert Watson .pru_rcvd = uipc_rcvd, 138284d61770SRobert Watson .pru_send = uipc_send, 138384d61770SRobert Watson .pru_sense = uipc_sense, 138484d61770SRobert Watson .pru_shutdown = uipc_shutdown, 138584d61770SRobert Watson .pru_sockaddr = uipc_sockaddr, 138684d61770SRobert Watson .pru_soreceive = soreceive_generic, /* XXX: or...? */ 138784d61770SRobert Watson .pru_close = uipc_close, 138884d61770SRobert Watson }; 138984d61770SRobert Watson 1390fa9402f2SRobert Watson static struct pr_usrreqs uipc_usrreqs_stream = { 1391fa9402f2SRobert Watson .pru_abort = uipc_abort, 1392fa9402f2SRobert Watson .pru_accept = uipc_accept, 1393fa9402f2SRobert Watson .pru_attach = uipc_attach, 1394fa9402f2SRobert Watson .pru_bind = uipc_bind, 13957493f24eSPawel Jakub Dawidek .pru_bindat = uipc_bindat, 1396fa9402f2SRobert Watson .pru_connect = uipc_connect, 13977493f24eSPawel Jakub Dawidek .pru_connectat = uipc_connectat, 1398fa9402f2SRobert Watson .pru_connect2 = uipc_connect2, 1399fa9402f2SRobert Watson .pru_detach = uipc_detach, 1400fa9402f2SRobert Watson .pru_disconnect = uipc_disconnect, 1401fa9402f2SRobert Watson .pru_listen = uipc_listen, 1402fa9402f2SRobert Watson .pru_peeraddr = uipc_peeraddr, 1403fa9402f2SRobert Watson .pru_rcvd = uipc_rcvd, 1404fa9402f2SRobert Watson .pru_send = uipc_send, 1405c80ea19bSGleb Smirnoff .pru_ready = uipc_ready, 1406fa9402f2SRobert Watson .pru_sense = uipc_sense, 1407fa9402f2SRobert Watson .pru_shutdown = uipc_shutdown, 1408fa9402f2SRobert Watson .pru_sockaddr = uipc_sockaddr, 1409fa9402f2SRobert Watson .pru_soreceive = soreceive_generic, 1410a152f8a3SRobert Watson .pru_close = uipc_close, 1411a29f300eSGarrett Wollman }; 1412df8bae1dSRodney W. Grimes 14130b36cd25SRobert Watson static int 1414892af6b9SRobert Watson uipc_ctloutput(struct socket *so, struct sockopt *sopt) 14150c1bb4fbSDima Dorfman { 141640f2ac28SRobert Watson struct unpcb *unp; 14170d9ce3a1SRobert Watson struct xucred xu; 14186a2989fdSMatthew N. Dodd int error, optval; 14196a2989fdSMatthew N. Dodd 142096a041b5SMatthew N. Dodd if (sopt->sopt_level != 0) 142196a041b5SMatthew N. Dodd return (EINVAL); 142296a041b5SMatthew N. Dodd 14236a2989fdSMatthew N. Dodd unp = sotounpcb(so); 14244d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL")); 14256a2989fdSMatthew N. Dodd error = 0; 14260c1bb4fbSDima Dorfman switch (sopt->sopt_dir) { 14270c1bb4fbSDima Dorfman case SOPT_GET: 14280c1bb4fbSDima Dorfman switch (sopt->sopt_name) { 14290c1bb4fbSDima Dorfman case LOCAL_PEERCRED: 1430e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 14310c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC) 14320d9ce3a1SRobert Watson xu = unp->unp_peercred; 14330c1bb4fbSDima Dorfman else { 14340c1bb4fbSDima Dorfman if (so->so_type == SOCK_STREAM) 14350c1bb4fbSDima Dorfman error = ENOTCONN; 14360c1bb4fbSDima Dorfman else 14370c1bb4fbSDima Dorfman error = EINVAL; 14380c1bb4fbSDima Dorfman } 1439e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 14400d9ce3a1SRobert Watson if (error == 0) 14410d9ce3a1SRobert Watson error = sooptcopyout(sopt, &xu, sizeof(xu)); 14420c1bb4fbSDima Dorfman break; 1443e7c33e29SRobert Watson 14446a2989fdSMatthew N. Dodd case LOCAL_CREDS: 1445a6357845SRobert Watson /* Unlocked read. */ 14466a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0; 14476a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 14486a2989fdSMatthew N. Dodd break; 1449e7c33e29SRobert Watson 14506a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 1451a6357845SRobert Watson /* Unlocked read. */ 14526a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0; 14536a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 14546a2989fdSMatthew N. Dodd break; 1455e7c33e29SRobert Watson 14560c1bb4fbSDima Dorfman default: 14570c1bb4fbSDima Dorfman error = EOPNOTSUPP; 14580c1bb4fbSDima Dorfman break; 14590c1bb4fbSDima Dorfman } 14600c1bb4fbSDima Dorfman break; 1461e7c33e29SRobert Watson 14620c1bb4fbSDima Dorfman case SOPT_SET: 14636a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 14646a2989fdSMatthew N. Dodd case LOCAL_CREDS: 14656a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 14666a2989fdSMatthew N. Dodd error = sooptcopyin(sopt, &optval, sizeof(optval), 14676a2989fdSMatthew N. Dodd sizeof(optval)); 14686a2989fdSMatthew N. Dodd if (error) 14696a2989fdSMatthew N. Dodd break; 14706a2989fdSMatthew N. Dodd 1471e7c33e29SRobert Watson #define OPTSET(bit) do { \ 1472e7c33e29SRobert Watson UNP_PCB_LOCK(unp); \ 14736a2989fdSMatthew N. Dodd if (optval) \ 14746a2989fdSMatthew N. Dodd unp->unp_flags |= bit; \ 14756a2989fdSMatthew N. Dodd else \ 1476e7c33e29SRobert Watson unp->unp_flags &= ~bit; \ 1477e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); \ 1478e7c33e29SRobert Watson } while (0) 14796a2989fdSMatthew N. Dodd 14806a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 14816a2989fdSMatthew N. Dodd case LOCAL_CREDS: 14826a2989fdSMatthew N. Dodd OPTSET(UNP_WANTCRED); 14836a2989fdSMatthew N. Dodd break; 1484e7c33e29SRobert Watson 14856a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 14866a2989fdSMatthew N. Dodd OPTSET(UNP_CONNWAIT); 14876a2989fdSMatthew N. Dodd break; 1488e7c33e29SRobert Watson 14896a2989fdSMatthew N. Dodd default: 14906a2989fdSMatthew N. Dodd break; 14916a2989fdSMatthew N. Dodd } 14926a2989fdSMatthew N. Dodd break; 14936a2989fdSMatthew N. Dodd #undef OPTSET 14946a2989fdSMatthew N. Dodd default: 14956a2989fdSMatthew N. Dodd error = ENOPROTOOPT; 14966a2989fdSMatthew N. Dodd break; 14976a2989fdSMatthew N. Dodd } 1498abb886faSMatthew N. Dodd break; 1499e7c33e29SRobert Watson 15000c1bb4fbSDima Dorfman default: 15010c1bb4fbSDima Dorfman error = EOPNOTSUPP; 15020c1bb4fbSDima Dorfman break; 15030c1bb4fbSDima Dorfman } 15040c1bb4fbSDima Dorfman return (error); 15050c1bb4fbSDima Dorfman } 15060c1bb4fbSDima Dorfman 1507f708ef1bSPoul-Henning Kamp static int 1508892af6b9SRobert Watson unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1509df8bae1dSRodney W. Grimes { 15107493f24eSPawel Jakub Dawidek 15117493f24eSPawel Jakub Dawidek return (unp_connectat(AT_FDCWD, so, nam, td)); 15127493f24eSPawel Jakub Dawidek } 15137493f24eSPawel Jakub Dawidek 15147493f24eSPawel Jakub Dawidek static int 15157493f24eSPawel Jakub Dawidek unp_connectat(int fd, struct socket *so, struct sockaddr *nam, 15167493f24eSPawel Jakub Dawidek struct thread *td) 15177493f24eSPawel Jakub Dawidek { 1518892af6b9SRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam; 1519892af6b9SRobert Watson struct vnode *vp; 1520779f106aSGleb Smirnoff struct socket *so2; 1521b295bdcdSRobert Watson struct unpcb *unp, *unp2, *unp3; 1522df8bae1dSRodney W. Grimes struct nameidata nd; 152357bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 15240d9ce3a1SRobert Watson struct sockaddr *sa; 15257008be5bSPawel Jakub Dawidek cap_rights_t rights; 152675a67bf3SMatt Macy int error, len, freed; 152775a67bf3SMatt Macy struct mtx *vplock; 15280d9ce3a1SRobert Watson 1529cb7df69bSKevin Lo if (nam->sa_family != AF_UNIX) 1530cb7df69bSKevin Lo return (EAFNOSUPPORT); 1531a06534c3SBjoern A. Zeeb if (nam->sa_len > sizeof(struct sockaddr_un)) 1532a06534c3SBjoern A. Zeeb return (EINVAL); 153357bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 153457bf258eSGarrett Wollman if (len <= 0) 1535e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 15367928893dSEd Maste bcopy(soun->sun_path, buf, len); 15377928893dSEd Maste buf[len] = 0; 1538e7c33e29SRobert Watson 153975a67bf3SMatt Macy unp = sotounpcb(so); 1540e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 15414f1f0ef5SRobert Watson if (unp->unp_flags & UNP_CONNECTING) { 1542e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 15434f1f0ef5SRobert Watson return (EALREADY); 15444f1f0ef5SRobert Watson } 154505102f04SRobert Watson unp->unp_flags |= UNP_CONNECTING; 1546e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1547e7c33e29SRobert Watson 15480d9ce3a1SRobert Watson sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 15497493f24eSPawel Jakub Dawidek NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, 15507008be5bSPawel Jakub Dawidek UIO_SYSSPACE, buf, fd, cap_rights_init(&rights, CAP_CONNECTAT), td); 1551797f2d22SPoul-Henning Kamp error = namei(&nd); 1552797f2d22SPoul-Henning Kamp if (error) 15530d9ce3a1SRobert Watson vp = NULL; 15540d9ce3a1SRobert Watson else 1555df8bae1dSRodney W. Grimes vp = nd.ni_vp; 15560d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_connect"); 1557762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 15580d9ce3a1SRobert Watson if (error) 15590d9ce3a1SRobert Watson goto bad; 15600d9ce3a1SRobert Watson 1561df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 1562df8bae1dSRodney W. Grimes error = ENOTSOCK; 1563df8bae1dSRodney W. Grimes goto bad; 1564df8bae1dSRodney W. Grimes } 15656fac927cSRobert Watson #ifdef MAC 156630d239bcSRobert Watson error = mac_vnode_check_open(td->td_ucred, vp, VWRITE | VREAD); 15676fac927cSRobert Watson if (error) 15686fac927cSRobert Watson goto bad; 15696fac927cSRobert Watson #endif 1570a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 1571797f2d22SPoul-Henning Kamp if (error) 1572df8bae1dSRodney W. Grimes goto bad; 1573e7c33e29SRobert Watson 1574b295bdcdSRobert Watson unp = sotounpcb(so); 15754d4b555eSRobert Watson KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 1576e7c33e29SRobert Watson 157775a67bf3SMatt Macy vplock = mtx_pool_find(mtxpool_sleep, vp); 157875a67bf3SMatt Macy mtx_lock(vplock); 15790c3c207fSGleb Smirnoff VOP_UNP_CONNECT(vp, &unp2); 15800c3c207fSGleb Smirnoff if (unp2 == NULL) { 1581df8bae1dSRodney W. Grimes error = ECONNREFUSED; 15822260c03dSRobert Watson goto bad2; 1583df8bae1dSRodney W. Grimes } 15840c3c207fSGleb Smirnoff so2 = unp2->unp_socket; 1585df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 1586df8bae1dSRodney W. Grimes error = EPROTOTYPE; 15872260c03dSRobert Watson goto bad2; 1588df8bae1dSRodney W. Grimes } 1589df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 1590e7c33e29SRobert Watson if (so2->so_options & SO_ACCEPTCONN) { 15911fb51a12SBjoern A. Zeeb CURVNET_SET(so2->so_vnet); 1592779f106aSGleb Smirnoff so2 = sonewconn(so2, 0); 15931fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 1594e7c33e29SRobert Watson } else 1595779f106aSGleb Smirnoff so2 = NULL; 1596779f106aSGleb Smirnoff if (so2 == NULL) { 1597df8bae1dSRodney W. Grimes error = ECONNREFUSED; 1598cb8f450bSMatt Macy goto bad2; 1599df8bae1dSRodney W. Grimes } 1600779f106aSGleb Smirnoff unp3 = sotounpcb(so2); 1601e10ef65dSMatt Macy unp_pcb_lock2(unp2, unp3); 16020d9ce3a1SRobert Watson if (unp2->unp_addr != NULL) { 16030d9ce3a1SRobert Watson bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 16040d9ce3a1SRobert Watson unp3->unp_addr = (struct sockaddr_un *) sa; 16050d9ce3a1SRobert Watson sa = NULL; 16060d9ce3a1SRobert Watson } 1607b523ec24SRobert Watson 1608da446550SAlan Somers unp_copy_peercred(td, unp3, unp, unp2); 1609b523ec24SRobert Watson 1610e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1611779f106aSGleb Smirnoff unp2 = unp3; 161275a67bf3SMatt Macy unp_pcb_owned_lock2(unp2, unp, freed); 1613e10ef65dSMatt Macy if (__predict_false(freed)) { 1614e10ef65dSMatt Macy UNP_PCB_UNLOCK(unp2); 1615e10ef65dSMatt Macy error = ECONNREFUSED; 1616e10ef65dSMatt Macy goto bad2; 1617e10ef65dSMatt Macy } 1618335654d7SRobert Watson #ifdef MAC 1619779f106aSGleb Smirnoff mac_socketpeer_set_from_socket(so, so2); 1620779f106aSGleb Smirnoff mac_socketpeer_set_from_socket(so2, so); 1621335654d7SRobert Watson #endif 1622a3a73490SMatt Macy } else { 1623a3a73490SMatt Macy if (unp == unp2) 1624a3a73490SMatt Macy UNP_PCB_LOCK(unp); 1625a3a73490SMatt Macy else 1626e10ef65dSMatt Macy unp_pcb_lock2(unp, unp2); 1627a3a73490SMatt Macy } 1628779f106aSGleb Smirnoff KASSERT(unp2 != NULL && so2 != NULL && unp2->unp_socket == so2 && 1629779f106aSGleb Smirnoff sotounpcb(so2) == unp2, 1630779f106aSGleb Smirnoff ("%s: unp2 %p so2 %p", __func__, unp2, so2)); 16316a2989fdSMatthew N. Dodd error = unp_connect2(so, so2, PRU_CONNECT); 1632a3a73490SMatt Macy if (unp != unp2) 1633e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1634e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 16350d9ce3a1SRobert Watson bad2: 163675a67bf3SMatt Macy mtx_unlock(vplock); 1637df8bae1dSRodney W. Grimes bad: 163875a67bf3SMatt Macy if (vp != NULL) { 1639df8bae1dSRodney W. Grimes vput(vp); 164075a67bf3SMatt Macy } 16410d9ce3a1SRobert Watson free(sa, M_SONAME); 1642e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 16434f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_CONNECTING; 1644e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1645df8bae1dSRodney W. Grimes return (error); 1646df8bae1dSRodney W. Grimes } 1647df8bae1dSRodney W. Grimes 1648da446550SAlan Somers /* 1649da446550SAlan Somers * Set socket peer credentials at connection time. 1650da446550SAlan Somers * 1651da446550SAlan Somers * The client's PCB credentials are copied from its process structure. The 1652da446550SAlan Somers * server's PCB credentials are copied from the socket on which it called 1653da446550SAlan Somers * listen(2). uipc_listen cached that process's credentials at the time. 1654da446550SAlan Somers */ 1655da446550SAlan Somers void 1656da446550SAlan Somers unp_copy_peercred(struct thread *td, struct unpcb *client_unp, 1657da446550SAlan Somers struct unpcb *server_unp, struct unpcb *listen_unp) 1658da446550SAlan Somers { 1659da446550SAlan Somers cru2x(td->td_ucred, &client_unp->unp_peercred); 1660da446550SAlan Somers client_unp->unp_flags |= UNP_HAVEPC; 1661da446550SAlan Somers 1662da446550SAlan Somers memcpy(&server_unp->unp_peercred, &listen_unp->unp_peercred, 1663da446550SAlan Somers sizeof(server_unp->unp_peercred)); 1664da446550SAlan Somers server_unp->unp_flags |= UNP_HAVEPC; 1665da446550SAlan Somers if (listen_unp->unp_flags & UNP_WANTCRED) 1666da446550SAlan Somers client_unp->unp_flags |= UNP_WANTCRED; 1667da446550SAlan Somers } 1668da446550SAlan Somers 1669db48c0d2SRobert Watson static int 16706a2989fdSMatthew N. Dodd unp_connect2(struct socket *so, struct socket *so2, int req) 1671df8bae1dSRodney W. Grimes { 1672e7c33e29SRobert Watson struct unpcb *unp; 1673892af6b9SRobert Watson struct unpcb *unp2; 1674df8bae1dSRodney W. Grimes 1675e7c33e29SRobert Watson unp = sotounpcb(so); 1676e7c33e29SRobert Watson KASSERT(unp != NULL, ("unp_connect2: unp == NULL")); 1677e7c33e29SRobert Watson unp2 = sotounpcb(so2); 1678e7c33e29SRobert Watson KASSERT(unp2 != NULL, ("unp_connect2: unp2 == NULL")); 1679e7c33e29SRobert Watson 1680e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 1681e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp2); 16820d9ce3a1SRobert Watson 1683df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 1684df8bae1dSRodney W. Grimes return (EPROTOTYPE); 1685434ac8b6SMark Johnston unp2->unp_flags &= ~UNP_NASCENT; 1686df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 168775a67bf3SMatt Macy unp_pcb_hold(unp2); 168875a67bf3SMatt Macy unp_pcb_hold(unp); 1689df8bae1dSRodney W. Grimes switch (so->so_type) { 1690df8bae1dSRodney W. Grimes case SOCK_DGRAM: 169175a67bf3SMatt Macy UNP_REF_LIST_LOCK(); 169298271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 169375a67bf3SMatt Macy UNP_REF_LIST_UNLOCK(); 1694df8bae1dSRodney W. Grimes soisconnected(so); 1695df8bae1dSRodney W. Grimes break; 1696df8bae1dSRodney W. Grimes 1697df8bae1dSRodney W. Grimes case SOCK_STREAM: 169884d61770SRobert Watson case SOCK_SEQPACKET: 1699df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 17006a2989fdSMatthew N. Dodd if (req == PRU_CONNECT && 17016a2989fdSMatthew N. Dodd ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT)) 17026a2989fdSMatthew N. Dodd soisconnecting(so); 17036a2989fdSMatthew N. Dodd else 1704df8bae1dSRodney W. Grimes soisconnected(so); 1705df8bae1dSRodney W. Grimes soisconnected(so2); 1706df8bae1dSRodney W. Grimes break; 1707df8bae1dSRodney W. Grimes 1708df8bae1dSRodney W. Grimes default: 1709df8bae1dSRodney W. Grimes panic("unp_connect2"); 1710df8bae1dSRodney W. Grimes } 1711df8bae1dSRodney W. Grimes return (0); 1712df8bae1dSRodney W. Grimes } 1713df8bae1dSRodney W. Grimes 1714f708ef1bSPoul-Henning Kamp static void 1715e7c33e29SRobert Watson unp_disconnect(struct unpcb *unp, struct unpcb *unp2) 1716df8bae1dSRodney W. Grimes { 171775a67bf3SMatt Macy struct socket *so, *so2; 17184b06dee1SMatt Macy int freed __unused; 1719df8bae1dSRodney W. Grimes 1720e7c33e29SRobert Watson KASSERT(unp2 != NULL, ("unp_disconnect: unp2 == NULL")); 17210d9ce3a1SRobert Watson 1722e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 1723e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp2); 1724e7c33e29SRobert Watson 172575a67bf3SMatt Macy if (unp->unp_conn == NULL && unp2->unp_conn == NULL) 172675a67bf3SMatt Macy return; 172775a67bf3SMatt Macy 172875a67bf3SMatt Macy MPASS(unp->unp_conn == unp2); 1729fc3fcacfSRobert Watson unp->unp_conn = NULL; 173075a67bf3SMatt Macy so = unp->unp_socket; 173175a67bf3SMatt Macy so2 = unp2->unp_socket; 1732df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 1733df8bae1dSRodney W. Grimes case SOCK_DGRAM: 173475a67bf3SMatt Macy UNP_REF_LIST_LOCK(); 173598271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 173675a67bf3SMatt Macy UNP_REF_LIST_UNLOCK(); 173775a67bf3SMatt Macy if (so) { 17381b2e3b4bSRobert Watson SOCK_LOCK(so); 17391b2e3b4bSRobert Watson so->so_state &= ~SS_ISCONNECTED; 17401b2e3b4bSRobert Watson SOCK_UNLOCK(so); 174175a67bf3SMatt Macy } 1742df8bae1dSRodney W. Grimes break; 1743df8bae1dSRodney W. Grimes 1744df8bae1dSRodney W. Grimes case SOCK_STREAM: 174584d61770SRobert Watson case SOCK_SEQPACKET: 174675a67bf3SMatt Macy if (so) 174775a67bf3SMatt Macy soisdisconnected(so); 174875a67bf3SMatt Macy MPASS(unp2->unp_conn == unp); 1749fc3fcacfSRobert Watson unp2->unp_conn = NULL; 175075a67bf3SMatt Macy if (so2) 175175a67bf3SMatt Macy soisdisconnected(so2); 1752df8bae1dSRodney W. Grimes break; 1753df8bae1dSRodney W. Grimes } 17544b06dee1SMatt Macy freed = unp_pcb_rele(unp); 175575a67bf3SMatt Macy MPASS(freed == 0); 17564b06dee1SMatt Macy freed = unp_pcb_rele(unp2); 175775a67bf3SMatt Macy MPASS(freed == 0); 1758df8bae1dSRodney W. Grimes } 1759df8bae1dSRodney W. Grimes 17600d9ce3a1SRobert Watson /* 1761d7924b70SRobert Watson * unp_pcblist() walks the global list of struct unpcb's to generate a 1762d7924b70SRobert Watson * pointer list, bumping the refcount on each unpcb. It then copies them out 1763d7924b70SRobert Watson * sequentially, validating the generation number on each to see if it has 1764d7924b70SRobert Watson * been detached. All of this is necessary because copyout() may sleep on 1765d7924b70SRobert Watson * disk I/O. 17660d9ce3a1SRobert Watson */ 176798271db4SGarrett Wollman static int 176882d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 176998271db4SGarrett Wollman { 177098271db4SGarrett Wollman struct unpcb *unp, **unp_list; 177198271db4SGarrett Wollman unp_gen_t gencnt; 17728f364875SJulian Elischer struct xunpgen *xug; 177398271db4SGarrett Wollman struct unp_head *head; 17748f364875SJulian Elischer struct xunpcb *xu; 1775d821d364SPedro F. Giffuni u_int i; 177644c514b1SPedro F. Giffuni int error, freeunp, n; 177798271db4SGarrett Wollman 177884d61770SRobert Watson switch ((intptr_t)arg1) { 177984d61770SRobert Watson case SOCK_STREAM: 178084d61770SRobert Watson head = &unp_shead; 178184d61770SRobert Watson break; 178284d61770SRobert Watson 178384d61770SRobert Watson case SOCK_DGRAM: 178484d61770SRobert Watson head = &unp_dhead; 178584d61770SRobert Watson break; 178684d61770SRobert Watson 178784d61770SRobert Watson case SOCK_SEQPACKET: 178884d61770SRobert Watson head = &unp_sphead; 178984d61770SRobert Watson break; 179084d61770SRobert Watson 179184d61770SRobert Watson default: 1792604f19c9SRobert Watson panic("unp_pcblist: arg1 %d", (int)(intptr_t)arg1); 179384d61770SRobert Watson } 179498271db4SGarrett Wollman 179598271db4SGarrett Wollman /* 179698271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 179798271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 179898271db4SGarrett Wollman */ 1799fc3fcacfSRobert Watson if (req->oldptr == NULL) { 180098271db4SGarrett Wollman n = unp_count; 18018f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug) 180298271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 1803e5aeaa0cSDag-Erling Smørgrav return (0); 180498271db4SGarrett Wollman } 180598271db4SGarrett Wollman 1806fc3fcacfSRobert Watson if (req->newptr != NULL) 1807e5aeaa0cSDag-Erling Smørgrav return (EPERM); 180898271db4SGarrett Wollman 180998271db4SGarrett Wollman /* 181098271db4SGarrett Wollman * OK, now we're committed to doing something. 181198271db4SGarrett Wollman */ 1812*79db6fe7SMark Johnston xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK | M_ZERO); 1813779f106aSGleb Smirnoff UNP_LINK_RLOCK(); 181498271db4SGarrett Wollman gencnt = unp_gencnt; 181598271db4SGarrett Wollman n = unp_count; 1816779f106aSGleb Smirnoff UNP_LINK_RUNLOCK(); 181798271db4SGarrett Wollman 18188f364875SJulian Elischer xug->xug_len = sizeof *xug; 18198f364875SJulian Elischer xug->xug_count = n; 18208f364875SJulian Elischer xug->xug_gen = gencnt; 18218f364875SJulian Elischer xug->xug_sogen = so_gencnt; 18228f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 18238f364875SJulian Elischer if (error) { 18248f364875SJulian Elischer free(xug, M_TEMP); 1825e5aeaa0cSDag-Erling Smørgrav return (error); 18268f364875SJulian Elischer } 182798271db4SGarrett Wollman 1828a163d034SWarner Losh unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 182998271db4SGarrett Wollman 1830779f106aSGleb Smirnoff UNP_LINK_RLOCK(); 18312e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 18322e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 1833e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 18348a7d8cc6SRobert Watson if (unp->unp_gencnt <= gencnt) { 1835a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred, 1836e7c33e29SRobert Watson unp->unp_socket->so_cred)) { 1837e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 18384787fd37SPaul Saab continue; 1839e7c33e29SRobert Watson } 184098271db4SGarrett Wollman unp_list[i++] = unp; 184175a67bf3SMatt Macy unp_pcb_hold(unp); 184298271db4SGarrett Wollman } 1843e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 18444787fd37SPaul Saab } 1845779f106aSGleb Smirnoff UNP_LINK_RUNLOCK(); 18461c381b19SRobert Watson n = i; /* In case we lost some during malloc. */ 184798271db4SGarrett Wollman 184898271db4SGarrett Wollman error = 0; 1849fe2eee82SColin Percival xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO); 185098271db4SGarrett Wollman for (i = 0; i < n; i++) { 185198271db4SGarrett Wollman unp = unp_list[i]; 1852e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 185375a67bf3SMatt Macy freeunp = unp_pcb_rele(unp); 185475a67bf3SMatt Macy 185575a67bf3SMatt Macy if (freeunp == 0 && unp->unp_gencnt <= gencnt) { 18568f364875SJulian Elischer xu->xu_len = sizeof *xu; 18573a20f06aSBrooks Davis xu->xu_unpp = (uintptr_t)unp; 185898271db4SGarrett Wollman /* 185998271db4SGarrett Wollman * XXX - need more locking here to protect against 186098271db4SGarrett Wollman * connect/disconnect races for SMP. 186198271db4SGarrett Wollman */ 1862fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 18638f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr, 186498271db4SGarrett Wollman unp->unp_addr->sun_len); 18650e229f34SGleb Smirnoff else 18660e229f34SGleb Smirnoff bzero(&xu->xu_addr, sizeof(xu->xu_addr)); 1867fc3fcacfSRobert Watson if (unp->unp_conn != NULL && 1868fc3fcacfSRobert Watson unp->unp_conn->unp_addr != NULL) 186998271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 18708f364875SJulian Elischer &xu->xu_caddr, 187198271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 18720e229f34SGleb Smirnoff else 18730e229f34SGleb Smirnoff bzero(&xu->xu_caddr, sizeof(xu->xu_caddr)); 18743a20f06aSBrooks Davis xu->unp_vnode = (uintptr_t)unp->unp_vnode; 18753a20f06aSBrooks Davis xu->unp_conn = (uintptr_t)unp->unp_conn; 18763a20f06aSBrooks Davis xu->xu_firstref = (uintptr_t)LIST_FIRST(&unp->unp_refs); 18773a20f06aSBrooks Davis xu->xu_nextref = (uintptr_t)LIST_NEXT(unp, unp_reflink); 18780e229f34SGleb Smirnoff xu->unp_gencnt = unp->unp_gencnt; 18798f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket); 1880e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 18818f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu); 188275a67bf3SMatt Macy } else if (freeunp == 0) 1883e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1884e7c33e29SRobert Watson } 18858f364875SJulian Elischer free(xu, M_TEMP); 188698271db4SGarrett Wollman if (!error) { 188798271db4SGarrett Wollman /* 18881c381b19SRobert Watson * Give the user an updated idea of our state. If the 18891c381b19SRobert Watson * generation differs from what we told her before, she knows 18901c381b19SRobert Watson * that something happened while we were processing this 18911c381b19SRobert Watson * request, and it might be necessary to retry. 189298271db4SGarrett Wollman */ 18938f364875SJulian Elischer xug->xug_gen = unp_gencnt; 18948f364875SJulian Elischer xug->xug_sogen = so_gencnt; 18958f364875SJulian Elischer xug->xug_count = unp_count; 18968f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 189798271db4SGarrett Wollman } 189898271db4SGarrett Wollman free(unp_list, M_TEMP); 18998f364875SJulian Elischer free(xug, M_TEMP); 1900e5aeaa0cSDag-Erling Smørgrav return (error); 190198271db4SGarrett Wollman } 190298271db4SGarrett Wollman 19032fee06f0SMatthew D Fleming SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLTYPE_OPAQUE | CTLFLAG_RD, 19042fee06f0SMatthew D Fleming (void *)(intptr_t)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 190598271db4SGarrett Wollman "List of active local datagram sockets"); 19062fee06f0SMatthew D Fleming SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLTYPE_OPAQUE | CTLFLAG_RD, 19072fee06f0SMatthew D Fleming (void *)(intptr_t)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 190898271db4SGarrett Wollman "List of active local stream sockets"); 19092fee06f0SMatthew D Fleming SYSCTL_PROC(_net_local_seqpacket, OID_AUTO, pcblist, 19102fee06f0SMatthew D Fleming CTLTYPE_OPAQUE | CTLFLAG_RD, 19112fee06f0SMatthew D Fleming (void *)(intptr_t)SOCK_SEQPACKET, 0, unp_pcblist, "S,xunpcb", 191284d61770SRobert Watson "List of active local seqpacket sockets"); 191398271db4SGarrett Wollman 1914f708ef1bSPoul-Henning Kamp static void 1915892af6b9SRobert Watson unp_shutdown(struct unpcb *unp) 1916df8bae1dSRodney W. Grimes { 1917e7c33e29SRobert Watson struct unpcb *unp2; 1918df8bae1dSRodney W. Grimes struct socket *so; 1919df8bae1dSRodney W. Grimes 1920e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 19210d9ce3a1SRobert Watson 1922e7c33e29SRobert Watson unp2 = unp->unp_conn; 192384d61770SRobert Watson if ((unp->unp_socket->so_type == SOCK_STREAM || 192484d61770SRobert Watson (unp->unp_socket->so_type == SOCK_SEQPACKET)) && unp2 != NULL) { 1925e7c33e29SRobert Watson so = unp2->unp_socket; 1926e7c33e29SRobert Watson if (so != NULL) 1927df8bae1dSRodney W. Grimes socantrcvmore(so); 1928df8bae1dSRodney W. Grimes } 1929e7c33e29SRobert Watson } 1930df8bae1dSRodney W. Grimes 1931f708ef1bSPoul-Henning Kamp static void 1932afc055d9SEd Schouten unp_drop(struct unpcb *unp) 1933df8bae1dSRodney W. Grimes { 1934df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 1935e7c33e29SRobert Watson struct unpcb *unp2; 193675a67bf3SMatt Macy int freed; 19370d9ce3a1SRobert Watson 1938afc055d9SEd Schouten /* 1939afc055d9SEd Schouten * Regardless of whether the socket's peer dropped the connection 1940afc055d9SEd Schouten * with this socket by aborting or disconnecting, POSIX requires 1941afc055d9SEd Schouten * that ECONNRESET is returned. 1942afc055d9SEd Schouten */ 194375a67bf3SMatt Macy /* acquire a reference so that unp isn't freed from underneath us */ 194475a67bf3SMatt Macy 194575a67bf3SMatt Macy UNP_PCB_LOCK(unp); 194675a67bf3SMatt Macy if (so) 1947afc055d9SEd Schouten so->so_error = ECONNRESET; 1948e7c33e29SRobert Watson unp2 = unp->unp_conn; 1949acf9fd05SMatt Macy if (unp2 == unp) { 1950acf9fd05SMatt Macy unp_disconnect(unp, unp2); 1951acf9fd05SMatt Macy } else if (unp2 != NULL) { 195275a67bf3SMatt Macy unp_pcb_hold(unp2); 195375a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freed); 1954e7c33e29SRobert Watson unp_disconnect(unp, unp2); 195575a67bf3SMatt Macy if (unp_pcb_rele(unp2) == 0) 1956e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1957df8bae1dSRodney W. Grimes } 195875a67bf3SMatt Macy if (unp_pcb_rele(unp) == 0) 195975a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 196075a67bf3SMatt Macy } 1961df8bae1dSRodney W. Grimes 19622bc21ed9SDavid Malone static void 19638cb539f1SPawel Jakub Dawidek unp_freerights(struct filedescent **fdep, int fdcount) 1964df8bae1dSRodney W. Grimes { 19652bc21ed9SDavid Malone struct file *fp; 19662609222aSPawel Jakub Dawidek int i; 1967df8bae1dSRodney W. Grimes 196882e825c4SGleb Smirnoff KASSERT(fdcount > 0, ("%s: fdcount %d", __func__, fdcount)); 196982e825c4SGleb Smirnoff 19708cb539f1SPawel Jakub Dawidek for (i = 0; i < fdcount; i++) { 19718cb539f1SPawel Jakub Dawidek fp = fdep[i]->fde_file; 19728cb539f1SPawel Jakub Dawidek filecaps_free(&fdep[i]->fde_caps); 19738692c025SYoshinobu Inoue unp_discard(fp); 1974df8bae1dSRodney W. Grimes } 19758cb539f1SPawel Jakub Dawidek free(fdep[0], M_FILECAPS); 19762bc21ed9SDavid Malone } 19772bc21ed9SDavid Malone 19780b36cd25SRobert Watson static int 1979c2e3c52eSJilles Tjoelker unp_externalize(struct mbuf *control, struct mbuf **controlp, int flags) 19802bc21ed9SDavid Malone { 19812bc21ed9SDavid Malone struct thread *td = curthread; /* XXX */ 19822bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 19832bc21ed9SDavid Malone int i; 19842bc21ed9SDavid Malone int *fdp; 19852609222aSPawel Jakub Dawidek struct filedesc *fdesc = td->td_proc->p_fd; 1986ea31808cSMateusz Guzik struct filedescent **fdep; 19872bc21ed9SDavid Malone void *data; 19882bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 19892bc21ed9SDavid Malone int error, newfds; 19902bc21ed9SDavid Malone u_int newlen; 19912bc21ed9SDavid Malone 19923dab55bcSRobert Watson UNP_LINK_UNLOCK_ASSERT(); 19934c5bc1caSRobert Watson 19942bc21ed9SDavid Malone error = 0; 19952bc21ed9SDavid Malone if (controlp != NULL) /* controlp == NULL => free control messages */ 19962bc21ed9SDavid Malone *controlp = NULL; 19972bc21ed9SDavid Malone while (cm != NULL) { 19982bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 19992bc21ed9SDavid Malone error = EINVAL; 20002bc21ed9SDavid Malone break; 20012bc21ed9SDavid Malone } 20022bc21ed9SDavid Malone data = CMSG_DATA(cm); 20032bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 20042bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET 20052bc21ed9SDavid Malone && cm->cmsg_type == SCM_RIGHTS) { 20062609222aSPawel Jakub Dawidek newfds = datalen / sizeof(*fdep); 200782e825c4SGleb Smirnoff if (newfds == 0) 200882e825c4SGleb Smirnoff goto next; 20092609222aSPawel Jakub Dawidek fdep = data; 20102bc21ed9SDavid Malone 2011e2f9a08bSOlivier Houchard /* If we're not outputting the descriptors free them. */ 20122bc21ed9SDavid Malone if (error || controlp == NULL) { 20132609222aSPawel Jakub Dawidek unp_freerights(fdep, newfds); 20142bc21ed9SDavid Malone goto next; 20152bc21ed9SDavid Malone } 20162609222aSPawel Jakub Dawidek FILEDESC_XLOCK(fdesc); 201760a5ef26SRobert Watson 2018ed5b7817SJulian Elischer /* 20191c381b19SRobert Watson * Now change each pointer to an fd in the global 20201c381b19SRobert Watson * table to an integer that is the index to the local 20211c381b19SRobert Watson * fd table entry that we set up to point to the 20221c381b19SRobert Watson * global one we are transferring. 2023ed5b7817SJulian Elischer */ 20242bc21ed9SDavid Malone newlen = newfds * sizeof(int); 20252bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 20262bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 20272bc21ed9SDavid Malone if (*controlp == NULL) { 20282609222aSPawel Jakub Dawidek FILEDESC_XUNLOCK(fdesc); 20292bc21ed9SDavid Malone error = E2BIG; 20302609222aSPawel Jakub Dawidek unp_freerights(fdep, newfds); 20312bc21ed9SDavid Malone goto next; 20322bc21ed9SDavid Malone } 20332bc21ed9SDavid Malone 20342bc21ed9SDavid Malone fdp = (int *) 20352bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2036db8f33fdSMateusz Guzik if (fdallocn(td, 0, fdp, newfds) != 0) { 20373331a33aSMateusz Guzik FILEDESC_XUNLOCK(fdesc); 2038db8f33fdSMateusz Guzik error = EMSGSIZE; 2039db8f33fdSMateusz Guzik unp_freerights(fdep, newfds); 2040db8f33fdSMateusz Guzik m_freem(*controlp); 2041db8f33fdSMateusz Guzik *controlp = NULL; 2042db8f33fdSMateusz Guzik goto next; 2043db8f33fdSMateusz Guzik } 20448cb539f1SPawel Jakub Dawidek for (i = 0; i < newfds; i++, fdp++) { 2045ea31808cSMateusz Guzik _finstall(fdesc, fdep[i]->fde_file, *fdp, 2046ea31808cSMateusz Guzik (flags & MSG_CMSG_CLOEXEC) != 0 ? UF_EXCLOSE : 0, 2047ea31808cSMateusz Guzik &fdep[i]->fde_caps); 2048ea31808cSMateusz Guzik unp_externalize_fp(fdep[i]->fde_file); 2049df8bae1dSRodney W. Grimes } 2050c7902fbeSMark Johnston 2051c7902fbeSMark Johnston /* 2052c7902fbeSMark Johnston * The new type indicates that the mbuf data refers to 2053c7902fbeSMark Johnston * kernel resources that may need to be released before 2054c7902fbeSMark Johnston * the mbuf is freed. 2055c7902fbeSMark Johnston */ 2056c7902fbeSMark Johnston m_chtype(*controlp, MT_EXTCONTROL); 20572609222aSPawel Jakub Dawidek FILEDESC_XUNLOCK(fdesc); 20588cb539f1SPawel Jakub Dawidek free(fdep[0], M_FILECAPS); 20591c381b19SRobert Watson } else { 20601c381b19SRobert Watson /* We can just copy anything else across. */ 20612bc21ed9SDavid Malone if (error || controlp == NULL) 20622bc21ed9SDavid Malone goto next; 20632bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, datalen, 20642bc21ed9SDavid Malone cm->cmsg_type, cm->cmsg_level); 20652bc21ed9SDavid Malone if (*controlp == NULL) { 20662bc21ed9SDavid Malone error = ENOBUFS; 20672bc21ed9SDavid Malone goto next; 20682bc21ed9SDavid Malone } 20692bc21ed9SDavid Malone bcopy(data, 20702bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 20712bc21ed9SDavid Malone datalen); 20722bc21ed9SDavid Malone } 20732bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 20742bc21ed9SDavid Malone 20752bc21ed9SDavid Malone next: 20762bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 20772bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 20782bc21ed9SDavid Malone cm = (struct cmsghdr *) 20792bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 20808692c025SYoshinobu Inoue } else { 20812bc21ed9SDavid Malone clen = 0; 20822bc21ed9SDavid Malone cm = NULL; 20838692c025SYoshinobu Inoue } 20848692c025SYoshinobu Inoue } 20858692c025SYoshinobu Inoue 20862bc21ed9SDavid Malone m_freem(control); 20872bc21ed9SDavid Malone return (error); 2088df8bae1dSRodney W. Grimes } 2089df8bae1dSRodney W. Grimes 20904f590175SPaul Saab static void 20914f590175SPaul Saab unp_zone_change(void *tag) 20924f590175SPaul Saab { 20934f590175SPaul Saab 20944f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets); 20954f590175SPaul Saab } 20964f590175SPaul Saab 20970b36cd25SRobert Watson static void 209898271db4SGarrett Wollman unp_init(void) 209998271db4SGarrett Wollman { 21001c381b19SRobert Watson 210121ca7b57SMarko Zec #ifdef VIMAGE 210221ca7b57SMarko Zec if (!IS_DEFAULT_VNET(curvnet)) 210321ca7b57SMarko Zec return; 210421ca7b57SMarko Zec #endif 21059e9d298aSJeff Roberson unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 210675a67bf3SMatt Macy NULL, NULL, UMA_ALIGN_CACHE, 0); 2107fc3fcacfSRobert Watson if (unp_zone == NULL) 210898271db4SGarrett Wollman panic("unp_init"); 21094f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets); 21106e0b6746SPawel Jakub Dawidek uma_zone_set_warning(unp_zone, "kern.ipc.maxsockets limit reached"); 21114f590175SPaul Saab EVENTHANDLER_REGISTER(maxsockets_change, unp_zone_change, 21124f590175SPaul Saab NULL, EVENTHANDLER_PRI_ANY); 211398271db4SGarrett Wollman LIST_INIT(&unp_dhead); 211498271db4SGarrett Wollman LIST_INIT(&unp_shead); 211584d61770SRobert Watson LIST_INIT(&unp_sphead); 21160cb64678SKonstantin Belousov SLIST_INIT(&unp_defers); 2117daee0f0bSKonstantin Belousov TIMEOUT_TASK_INIT(taskqueue_thread, &unp_gc_task, 0, unp_gc, NULL); 21180cb64678SKonstantin Belousov TASK_INIT(&unp_defer_task, 0, unp_process_defers, NULL); 21193dab55bcSRobert Watson UNP_LINK_LOCK_INIT(); 21200cb64678SKonstantin Belousov UNP_DEFERRED_LOCK_INIT(); 212198271db4SGarrett Wollman } 212298271db4SGarrett Wollman 2123f708ef1bSPoul-Henning Kamp static int 2124892af6b9SRobert Watson unp_internalize(struct mbuf **controlp, struct thread *td) 2125df8bae1dSRodney W. Grimes { 21262bc21ed9SDavid Malone struct mbuf *control = *controlp; 2127b40ce416SJulian Elischer struct proc *p = td->td_proc; 21282609222aSPawel Jakub Dawidek struct filedesc *fdesc = p->p_fd; 2129ab15d803SSergey Kandaurov struct bintime *bt; 21302bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 21312bc21ed9SDavid Malone struct cmsgcred *cmcred; 21328cb539f1SPawel Jakub Dawidek struct filedescent *fde, **fdep, *fdev; 21332bc21ed9SDavid Malone struct file *fp; 21342bc21ed9SDavid Malone struct timeval *tv; 2135339efd75SMaxim Sobolev struct timespec *ts; 21366a1cf96bSMateusz Guzik int i, *fdp; 21372bc21ed9SDavid Malone void *data; 21382bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 21392bc21ed9SDavid Malone int error, oldfds; 21408692c025SYoshinobu Inoue u_int newlen; 2141df8bae1dSRodney W. Grimes 21423dab55bcSRobert Watson UNP_LINK_UNLOCK_ASSERT(); 21434c5bc1caSRobert Watson 21442bc21ed9SDavid Malone error = 0; 21452bc21ed9SDavid Malone *controlp = NULL; 21462bc21ed9SDavid Malone while (cm != NULL) { 21472bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 2148de966666SMateusz Guzik || cm->cmsg_len > clen || cm->cmsg_len < sizeof(*cm)) { 21492bc21ed9SDavid Malone error = EINVAL; 21502bc21ed9SDavid Malone goto out; 21512bc21ed9SDavid Malone } 21522bc21ed9SDavid Malone data = CMSG_DATA(cm); 21532bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 21542bc21ed9SDavid Malone 21552bc21ed9SDavid Malone switch (cm->cmsg_type) { 21560b788fa1SBill Paul /* 21570b788fa1SBill Paul * Fill in credential information. 21580b788fa1SBill Paul */ 21592bc21ed9SDavid Malone case SCM_CREDS: 21602bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 21612bc21ed9SDavid Malone SCM_CREDS, SOL_SOCKET); 21622bc21ed9SDavid Malone if (*controlp == NULL) { 21632bc21ed9SDavid Malone error = ENOBUFS; 21642bc21ed9SDavid Malone goto out; 21652bc21ed9SDavid Malone } 21662bc21ed9SDavid Malone cmcred = (struct cmsgcred *) 21672bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 21680b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 2169a854ed98SJohn Baldwin cmcred->cmcred_uid = td->td_ucred->cr_ruid; 2170a854ed98SJohn Baldwin cmcred->cmcred_gid = td->td_ucred->cr_rgid; 2171a854ed98SJohn Baldwin cmcred->cmcred_euid = td->td_ucred->cr_uid; 2172a854ed98SJohn Baldwin cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 21730b788fa1SBill Paul CMGROUP_MAX); 21740b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 21752bc21ed9SDavid Malone cmcred->cmcred_groups[i] = 2176a854ed98SJohn Baldwin td->td_ucred->cr_groups[i]; 21772bc21ed9SDavid Malone break; 21780b788fa1SBill Paul 21792bc21ed9SDavid Malone case SCM_RIGHTS: 21802bc21ed9SDavid Malone oldfds = datalen / sizeof (int); 218182e825c4SGleb Smirnoff if (oldfds == 0) 218282e825c4SGleb Smirnoff break; 2183ed5b7817SJulian Elischer /* 21841c381b19SRobert Watson * Check that all the FDs passed in refer to legal 21851c381b19SRobert Watson * files. If not, reject the entire operation. 2186ed5b7817SJulian Elischer */ 21872bc21ed9SDavid Malone fdp = data; 21882609222aSPawel Jakub Dawidek FILEDESC_SLOCK(fdesc); 21896a1cf96bSMateusz Guzik for (i = 0; i < oldfds; i++, fdp++) { 21906a1cf96bSMateusz Guzik fp = fget_locked(fdesc, *fdp); 21916a1cf96bSMateusz Guzik if (fp == NULL) { 21922609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdesc); 21932bc21ed9SDavid Malone error = EBADF; 21942bc21ed9SDavid Malone goto out; 21952bc21ed9SDavid Malone } 2196e7d6662fSAlfred Perlstein if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 21972609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdesc); 2198e7d6662fSAlfred Perlstein error = EOPNOTSUPP; 2199e7d6662fSAlfred Perlstein goto out; 2200e7d6662fSAlfred Perlstein } 2201e7d6662fSAlfred Perlstein 2202df8bae1dSRodney W. Grimes } 22035e3f7694SRobert Watson 2204ed5b7817SJulian Elischer /* 22050b36cd25SRobert Watson * Now replace the integer FDs with pointers to the 22062609222aSPawel Jakub Dawidek * file structure and capability rights. 2207ed5b7817SJulian Elischer */ 22088cb539f1SPawel Jakub Dawidek newlen = oldfds * sizeof(fdep[0]); 22092bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 22102bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 22112bc21ed9SDavid Malone if (*controlp == NULL) { 22122609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdesc); 22132bc21ed9SDavid Malone error = E2BIG; 22142bc21ed9SDavid Malone goto out; 22158692c025SYoshinobu Inoue } 22162bc21ed9SDavid Malone fdp = data; 22178cb539f1SPawel Jakub Dawidek fdep = (struct filedescent **) 22182bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 22198cb539f1SPawel Jakub Dawidek fdev = malloc(sizeof(*fdev) * oldfds, M_FILECAPS, 22208cb539f1SPawel Jakub Dawidek M_WAITOK); 22218cb539f1SPawel Jakub Dawidek for (i = 0; i < oldfds; i++, fdev++, fdp++) { 22222609222aSPawel Jakub Dawidek fde = &fdesc->fd_ofiles[*fdp]; 22238cb539f1SPawel Jakub Dawidek fdep[i] = fdev; 22248cb539f1SPawel Jakub Dawidek fdep[i]->fde_file = fde->fde_file; 22258cb539f1SPawel Jakub Dawidek filecaps_copy(&fde->fde_caps, 2226d7832811SMateusz Guzik &fdep[i]->fde_caps, true); 22278cb539f1SPawel Jakub Dawidek unp_internalize_fp(fdep[i]->fde_file); 2228df8bae1dSRodney W. Grimes } 22292609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdesc); 22302bc21ed9SDavid Malone break; 22312bc21ed9SDavid Malone 22322bc21ed9SDavid Malone case SCM_TIMESTAMP: 22332bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*tv), 22342bc21ed9SDavid Malone SCM_TIMESTAMP, SOL_SOCKET); 22352bc21ed9SDavid Malone if (*controlp == NULL) { 22362bc21ed9SDavid Malone error = ENOBUFS; 22372bc21ed9SDavid Malone goto out; 22388692c025SYoshinobu Inoue } 22392bc21ed9SDavid Malone tv = (struct timeval *) 22402bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 22412bc21ed9SDavid Malone microtime(tv); 22422bc21ed9SDavid Malone break; 22432bc21ed9SDavid Malone 2244ab15d803SSergey Kandaurov case SCM_BINTIME: 2245ab15d803SSergey Kandaurov *controlp = sbcreatecontrol(NULL, sizeof(*bt), 2246ab15d803SSergey Kandaurov SCM_BINTIME, SOL_SOCKET); 2247ab15d803SSergey Kandaurov if (*controlp == NULL) { 2248ab15d803SSergey Kandaurov error = ENOBUFS; 2249ab15d803SSergey Kandaurov goto out; 2250ab15d803SSergey Kandaurov } 2251ab15d803SSergey Kandaurov bt = (struct bintime *) 2252ab15d803SSergey Kandaurov CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2253ab15d803SSergey Kandaurov bintime(bt); 2254ab15d803SSergey Kandaurov break; 2255ab15d803SSergey Kandaurov 2256339efd75SMaxim Sobolev case SCM_REALTIME: 2257339efd75SMaxim Sobolev *controlp = sbcreatecontrol(NULL, sizeof(*ts), 2258339efd75SMaxim Sobolev SCM_REALTIME, SOL_SOCKET); 2259339efd75SMaxim Sobolev if (*controlp == NULL) { 2260339efd75SMaxim Sobolev error = ENOBUFS; 2261339efd75SMaxim Sobolev goto out; 2262339efd75SMaxim Sobolev } 2263339efd75SMaxim Sobolev ts = (struct timespec *) 2264339efd75SMaxim Sobolev CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2265339efd75SMaxim Sobolev nanotime(ts); 2266339efd75SMaxim Sobolev break; 2267339efd75SMaxim Sobolev 2268339efd75SMaxim Sobolev case SCM_MONOTONIC: 2269339efd75SMaxim Sobolev *controlp = sbcreatecontrol(NULL, sizeof(*ts), 2270339efd75SMaxim Sobolev SCM_MONOTONIC, SOL_SOCKET); 2271339efd75SMaxim Sobolev if (*controlp == NULL) { 2272339efd75SMaxim Sobolev error = ENOBUFS; 2273339efd75SMaxim Sobolev goto out; 2274339efd75SMaxim Sobolev } 2275339efd75SMaxim Sobolev ts = (struct timespec *) 2276339efd75SMaxim Sobolev CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2277339efd75SMaxim Sobolev nanouptime(ts); 2278339efd75SMaxim Sobolev break; 2279339efd75SMaxim Sobolev 22802bc21ed9SDavid Malone default: 22812bc21ed9SDavid Malone error = EINVAL; 22822bc21ed9SDavid Malone goto out; 22832bc21ed9SDavid Malone } 22842bc21ed9SDavid Malone 22852bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 22862bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 22872bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 22882bc21ed9SDavid Malone cm = (struct cmsghdr *) 22892bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 22902bc21ed9SDavid Malone } else { 22912bc21ed9SDavid Malone clen = 0; 22922bc21ed9SDavid Malone cm = NULL; 22932bc21ed9SDavid Malone } 22942bc21ed9SDavid Malone } 22952bc21ed9SDavid Malone 22962bc21ed9SDavid Malone out: 22972bc21ed9SDavid Malone m_freem(control); 22982bc21ed9SDavid Malone return (error); 2299df8bae1dSRodney W. Grimes } 2300df8bae1dSRodney W. Grimes 23015b950deaSRobert Watson static struct mbuf * 23026a2989fdSMatthew N. Dodd unp_addsockcred(struct thread *td, struct mbuf *control) 23036a2989fdSMatthew N. Dodd { 230470df31f4SMaxim Konovalov struct mbuf *m, *n, *n_prev; 23056a2989fdSMatthew N. Dodd struct sockcred *sc; 230670df31f4SMaxim Konovalov const struct cmsghdr *cm; 23076a2989fdSMatthew N. Dodd int ngroups; 23086a2989fdSMatthew N. Dodd int i; 23096a2989fdSMatthew N. Dodd 23106a2989fdSMatthew N. Dodd ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX); 23116a2989fdSMatthew N. Dodd m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET); 23126a2989fdSMatthew N. Dodd if (m == NULL) 23136a2989fdSMatthew N. Dodd return (control); 23146a2989fdSMatthew N. Dodd 23156a2989fdSMatthew N. Dodd sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *)); 23166a2989fdSMatthew N. Dodd sc->sc_uid = td->td_ucred->cr_ruid; 23176a2989fdSMatthew N. Dodd sc->sc_euid = td->td_ucred->cr_uid; 23186a2989fdSMatthew N. Dodd sc->sc_gid = td->td_ucred->cr_rgid; 23196a2989fdSMatthew N. Dodd sc->sc_egid = td->td_ucred->cr_gid; 23206a2989fdSMatthew N. Dodd sc->sc_ngroups = ngroups; 23216a2989fdSMatthew N. Dodd for (i = 0; i < sc->sc_ngroups; i++) 23226a2989fdSMatthew N. Dodd sc->sc_groups[i] = td->td_ucred->cr_groups[i]; 23236a2989fdSMatthew N. Dodd 23246a2989fdSMatthew N. Dodd /* 23251c381b19SRobert Watson * Unlink SCM_CREDS control messages (struct cmsgcred), since just 23261c381b19SRobert Watson * created SCM_CREDS control message (struct sockcred) has another 23271c381b19SRobert Watson * format. 23286a2989fdSMatthew N. Dodd */ 232970df31f4SMaxim Konovalov if (control != NULL) 233070df31f4SMaxim Konovalov for (n = control, n_prev = NULL; n != NULL;) { 233170df31f4SMaxim Konovalov cm = mtod(n, struct cmsghdr *); 233270df31f4SMaxim Konovalov if (cm->cmsg_level == SOL_SOCKET && 233370df31f4SMaxim Konovalov cm->cmsg_type == SCM_CREDS) { 233470df31f4SMaxim Konovalov if (n_prev == NULL) 233570df31f4SMaxim Konovalov control = n->m_next; 233670df31f4SMaxim Konovalov else 233770df31f4SMaxim Konovalov n_prev->m_next = n->m_next; 233870df31f4SMaxim Konovalov n = m_free(n); 233970df31f4SMaxim Konovalov } else { 234070df31f4SMaxim Konovalov n_prev = n; 234170df31f4SMaxim Konovalov n = n->m_next; 234270df31f4SMaxim Konovalov } 234370df31f4SMaxim Konovalov } 23446a2989fdSMatthew N. Dodd 234570df31f4SMaxim Konovalov /* Prepend it to the head. */ 234670df31f4SMaxim Konovalov m->m_next = control; 234770df31f4SMaxim Konovalov return (m); 23486a2989fdSMatthew N. Dodd } 23496a2989fdSMatthew N. Dodd 2350397c19d1SJeff Roberson static struct unpcb * 2351397c19d1SJeff Roberson fptounp(struct file *fp) 2352397c19d1SJeff Roberson { 2353397c19d1SJeff Roberson struct socket *so; 2354397c19d1SJeff Roberson 2355397c19d1SJeff Roberson if (fp->f_type != DTYPE_SOCKET) 2356397c19d1SJeff Roberson return (NULL); 2357397c19d1SJeff Roberson if ((so = fp->f_data) == NULL) 2358397c19d1SJeff Roberson return (NULL); 2359397c19d1SJeff Roberson if (so->so_proto->pr_domain != &localdomain) 2360397c19d1SJeff Roberson return (NULL); 2361397c19d1SJeff Roberson return sotounpcb(so); 2362397c19d1SJeff Roberson } 2363397c19d1SJeff Roberson 2364397c19d1SJeff Roberson static void 2365397c19d1SJeff Roberson unp_discard(struct file *fp) 2366397c19d1SJeff Roberson { 23670cb64678SKonstantin Belousov struct unp_defer *dr; 2368397c19d1SJeff Roberson 23690cb64678SKonstantin Belousov if (unp_externalize_fp(fp)) { 23700cb64678SKonstantin Belousov dr = malloc(sizeof(*dr), M_TEMP, M_WAITOK); 23710cb64678SKonstantin Belousov dr->ud_fp = fp; 23720cb64678SKonstantin Belousov UNP_DEFERRED_LOCK(); 23730cb64678SKonstantin Belousov SLIST_INSERT_HEAD(&unp_defers, dr, ud_link); 23740cb64678SKonstantin Belousov UNP_DEFERRED_UNLOCK(); 23750cb64678SKonstantin Belousov atomic_add_int(&unp_defers_count, 1); 23760cb64678SKonstantin Belousov taskqueue_enqueue(taskqueue_thread, &unp_defer_task); 23770cb64678SKonstantin Belousov } else 2378397c19d1SJeff Roberson (void) closef(fp, (struct thread *)NULL); 2379397c19d1SJeff Roberson } 2380397c19d1SJeff Roberson 2381397c19d1SJeff Roberson static void 23820cb64678SKonstantin Belousov unp_process_defers(void *arg __unused, int pending) 23830cb64678SKonstantin Belousov { 23840cb64678SKonstantin Belousov struct unp_defer *dr; 23850cb64678SKonstantin Belousov SLIST_HEAD(, unp_defer) drl; 23860cb64678SKonstantin Belousov int count; 23870cb64678SKonstantin Belousov 23880cb64678SKonstantin Belousov SLIST_INIT(&drl); 23890cb64678SKonstantin Belousov for (;;) { 23900cb64678SKonstantin Belousov UNP_DEFERRED_LOCK(); 23910cb64678SKonstantin Belousov if (SLIST_FIRST(&unp_defers) == NULL) { 23920cb64678SKonstantin Belousov UNP_DEFERRED_UNLOCK(); 23930cb64678SKonstantin Belousov break; 23940cb64678SKonstantin Belousov } 23950cb64678SKonstantin Belousov SLIST_SWAP(&unp_defers, &drl, unp_defer); 23960cb64678SKonstantin Belousov UNP_DEFERRED_UNLOCK(); 23970cb64678SKonstantin Belousov count = 0; 23980cb64678SKonstantin Belousov while ((dr = SLIST_FIRST(&drl)) != NULL) { 23990cb64678SKonstantin Belousov SLIST_REMOVE_HEAD(&drl, ud_link); 24000cb64678SKonstantin Belousov closef(dr->ud_fp, NULL); 24010cb64678SKonstantin Belousov free(dr, M_TEMP); 24020cb64678SKonstantin Belousov count++; 24030cb64678SKonstantin Belousov } 24040cb64678SKonstantin Belousov atomic_add_int(&unp_defers_count, -count); 24050cb64678SKonstantin Belousov } 24060cb64678SKonstantin Belousov } 24070cb64678SKonstantin Belousov 24080cb64678SKonstantin Belousov static void 2409397c19d1SJeff Roberson unp_internalize_fp(struct file *fp) 2410397c19d1SJeff Roberson { 2411397c19d1SJeff Roberson struct unpcb *unp; 2412397c19d1SJeff Roberson 24133dab55bcSRobert Watson UNP_LINK_WLOCK(); 2414397c19d1SJeff Roberson if ((unp = fptounp(fp)) != NULL) { 2415397c19d1SJeff Roberson unp->unp_file = fp; 2416397c19d1SJeff Roberson unp->unp_msgcount++; 2417397c19d1SJeff Roberson } 241841e0f66dSJeff Roberson fhold(fp); 2419397c19d1SJeff Roberson unp_rights++; 24203dab55bcSRobert Watson UNP_LINK_WUNLOCK(); 2421397c19d1SJeff Roberson } 2422397c19d1SJeff Roberson 24230cb64678SKonstantin Belousov static int 2424397c19d1SJeff Roberson unp_externalize_fp(struct file *fp) 2425397c19d1SJeff Roberson { 2426397c19d1SJeff Roberson struct unpcb *unp; 24270cb64678SKonstantin Belousov int ret; 2428397c19d1SJeff Roberson 24293dab55bcSRobert Watson UNP_LINK_WLOCK(); 24300cb64678SKonstantin Belousov if ((unp = fptounp(fp)) != NULL) { 2431397c19d1SJeff Roberson unp->unp_msgcount--; 24320cb64678SKonstantin Belousov ret = 1; 24330cb64678SKonstantin Belousov } else 24340cb64678SKonstantin Belousov ret = 0; 2435397c19d1SJeff Roberson unp_rights--; 24363dab55bcSRobert Watson UNP_LINK_WUNLOCK(); 24370cb64678SKonstantin Belousov return (ret); 2438397c19d1SJeff Roberson } 2439397c19d1SJeff Roberson 2440161a0c7cSRobert Watson /* 2441a0ec558aSRobert Watson * unp_defer indicates whether additional work has been defered for a future 2442a0ec558aSRobert Watson * pass through unp_gc(). It is thread local and does not require explicit 2443a0ec558aSRobert Watson * synchronization. 2444161a0c7cSRobert Watson */ 2445397c19d1SJeff Roberson static int unp_marked; 2446397c19d1SJeff Roberson static int unp_unreachable; 2447a0ec558aSRobert Watson 2448397c19d1SJeff Roberson static void 2449be26ba7cSPawel Jakub Dawidek unp_accessable(struct filedescent **fdep, int fdcount) 2450397c19d1SJeff Roberson { 2451397c19d1SJeff Roberson struct unpcb *unp; 2452be26ba7cSPawel Jakub Dawidek struct file *fp; 2453be26ba7cSPawel Jakub Dawidek int i; 2454397c19d1SJeff Roberson 2455be26ba7cSPawel Jakub Dawidek for (i = 0; i < fdcount; i++) { 2456be26ba7cSPawel Jakub Dawidek fp = fdep[i]->fde_file; 24576f552cb0SJeff Roberson if ((unp = fptounp(fp)) == NULL) 2458be26ba7cSPawel Jakub Dawidek continue; 2459397c19d1SJeff Roberson if (unp->unp_gcflag & UNPGC_REF) 2460be26ba7cSPawel Jakub Dawidek continue; 2461397c19d1SJeff Roberson unp->unp_gcflag &= ~UNPGC_DEAD; 2462397c19d1SJeff Roberson unp->unp_gcflag |= UNPGC_REF; 2463397c19d1SJeff Roberson unp_marked++; 2464397c19d1SJeff Roberson } 2465be26ba7cSPawel Jakub Dawidek } 2466397c19d1SJeff Roberson 2467397c19d1SJeff Roberson static void 2468397c19d1SJeff Roberson unp_gc_process(struct unpcb *unp) 2469397c19d1SJeff Roberson { 2470779f106aSGleb Smirnoff struct socket *so, *soa; 2471397c19d1SJeff Roberson struct file *fp; 2472397c19d1SJeff Roberson 2473397c19d1SJeff Roberson /* Already processed. */ 2474397c19d1SJeff Roberson if (unp->unp_gcflag & UNPGC_SCANNED) 2475397c19d1SJeff Roberson return; 2476397c19d1SJeff Roberson fp = unp->unp_file; 247760a5ef26SRobert Watson 2478397c19d1SJeff Roberson /* 2479397c19d1SJeff Roberson * Check for a socket potentially in a cycle. It must be in a 2480397c19d1SJeff Roberson * queue as indicated by msgcount, and this must equal the file 2481397c19d1SJeff Roberson * reference count. Note that when msgcount is 0 the file is NULL. 2482397c19d1SJeff Roberson */ 248341e0f66dSJeff Roberson if ((unp->unp_gcflag & UNPGC_REF) == 0 && fp && 248441e0f66dSJeff Roberson unp->unp_msgcount != 0 && fp->f_count == unp->unp_msgcount) { 2485397c19d1SJeff Roberson unp->unp_gcflag |= UNPGC_DEAD; 2486397c19d1SJeff Roberson unp_unreachable++; 2487397c19d1SJeff Roberson return; 2488397c19d1SJeff Roberson } 248960a5ef26SRobert Watson 2490397c19d1SJeff Roberson so = unp->unp_socket; 2491779f106aSGleb Smirnoff SOCK_LOCK(so); 2492779f106aSGleb Smirnoff if (SOLISTENING(so)) { 2493397c19d1SJeff Roberson /* 2494397c19d1SJeff Roberson * Mark all sockets in our accept queue. 2495397c19d1SJeff Roberson */ 2496779f106aSGleb Smirnoff TAILQ_FOREACH(soa, &so->sol_comp, so_list) { 2497779f106aSGleb Smirnoff if (sotounpcb(soa)->unp_gcflag & UNPGC_IGNORE_RIGHTS) 24980c40f353SConrad Meyer continue; 2499397c19d1SJeff Roberson SOCKBUF_LOCK(&soa->so_rcv); 2500397c19d1SJeff Roberson unp_scan(soa->so_rcv.sb_mb, unp_accessable); 2501397c19d1SJeff Roberson SOCKBUF_UNLOCK(&soa->so_rcv); 2502397c19d1SJeff Roberson } 2503779f106aSGleb Smirnoff } else { 2504779f106aSGleb Smirnoff /* 2505779f106aSGleb Smirnoff * Mark all sockets we reference with RIGHTS. 2506779f106aSGleb Smirnoff */ 2507779f106aSGleb Smirnoff if ((unp->unp_gcflag & UNPGC_IGNORE_RIGHTS) == 0) { 2508779f106aSGleb Smirnoff SOCKBUF_LOCK(&so->so_rcv); 2509779f106aSGleb Smirnoff unp_scan(so->so_rcv.sb_mb, unp_accessable); 2510779f106aSGleb Smirnoff SOCKBUF_UNLOCK(&so->so_rcv); 2511779f106aSGleb Smirnoff } 2512779f106aSGleb Smirnoff } 2513779f106aSGleb Smirnoff SOCK_UNLOCK(so); 2514397c19d1SJeff Roberson unp->unp_gcflag |= UNPGC_SCANNED; 2515397c19d1SJeff Roberson } 2516a0ec558aSRobert Watson 2517a0ec558aSRobert Watson static int unp_recycled; 2518be6b1304STom Rhodes SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0, 2519be6b1304STom Rhodes "Number of unreachable sockets claimed by the garbage collector."); 2520df8bae1dSRodney W. Grimes 2521397c19d1SJeff Roberson static int unp_taskcount; 2522be6b1304STom Rhodes SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0, 2523be6b1304STom Rhodes "Number of times the garbage collector has run."); 2524397c19d1SJeff Roberson 2525f708ef1bSPoul-Henning Kamp static void 2526a0ec558aSRobert Watson unp_gc(__unused void *arg, int pending) 2527df8bae1dSRodney W. Grimes { 252884d61770SRobert Watson struct unp_head *heads[] = { &unp_dhead, &unp_shead, &unp_sphead, 252984d61770SRobert Watson NULL }; 2530397c19d1SJeff Roberson struct unp_head **head; 2531f7780c61SKonstantin Belousov struct file *f, **unref; 2532397c19d1SJeff Roberson struct unpcb *unp; 2533f7780c61SKonstantin Belousov int i, total; 2534df8bae1dSRodney W. Grimes 2535a0ec558aSRobert Watson unp_taskcount++; 2536779f106aSGleb Smirnoff UNP_LINK_RLOCK(); 2537ed5b7817SJulian Elischer /* 25380c40f353SConrad Meyer * First clear all gc flags from previous runs, apart from 25390c40f353SConrad Meyer * UNPGC_IGNORE_RIGHTS. 2540ed5b7817SJulian Elischer */ 2541397c19d1SJeff Roberson for (head = heads; *head != NULL; head++) 2542397c19d1SJeff Roberson LIST_FOREACH(unp, *head, unp_link) 25430c40f353SConrad Meyer unp->unp_gcflag = 25440c40f353SConrad Meyer (unp->unp_gcflag & UNPGC_IGNORE_RIGHTS); 254560a5ef26SRobert Watson 2546397c19d1SJeff Roberson /* 2547397c19d1SJeff Roberson * Scan marking all reachable sockets with UNPGC_REF. Once a socket 2548397c19d1SJeff Roberson * is reachable all of the sockets it references are reachable. 2549397c19d1SJeff Roberson * Stop the scan once we do a complete loop without discovering 2550397c19d1SJeff Roberson * a new reachable socket. 2551397c19d1SJeff Roberson */ 2552df8bae1dSRodney W. Grimes do { 2553397c19d1SJeff Roberson unp_unreachable = 0; 2554397c19d1SJeff Roberson unp_marked = 0; 2555397c19d1SJeff Roberson for (head = heads; *head != NULL; head++) 2556397c19d1SJeff Roberson LIST_FOREACH(unp, *head, unp_link) 2557397c19d1SJeff Roberson unp_gc_process(unp); 2558397c19d1SJeff Roberson } while (unp_marked); 2559779f106aSGleb Smirnoff UNP_LINK_RUNLOCK(); 2560397c19d1SJeff Roberson if (unp_unreachable == 0) 2561397c19d1SJeff Roberson return; 256260a5ef26SRobert Watson 2563ed5b7817SJulian Elischer /* 2564397c19d1SJeff Roberson * Allocate space for a local list of dead unpcbs. 2565ed5b7817SJulian Elischer */ 2566397c19d1SJeff Roberson unref = malloc(unp_unreachable * sizeof(struct file *), 2567397c19d1SJeff Roberson M_TEMP, M_WAITOK); 256860a5ef26SRobert Watson 2569ed5b7817SJulian Elischer /* 2570397c19d1SJeff Roberson * Iterate looking for sockets which have been specifically marked 2571397c19d1SJeff Roberson * as as unreachable and store them locally. 2572ed5b7817SJulian Elischer */ 2573f7780c61SKonstantin Belousov UNP_LINK_RLOCK(); 2574f7780c61SKonstantin Belousov for (total = 0, head = heads; *head != NULL; head++) 2575397c19d1SJeff Roberson LIST_FOREACH(unp, *head, unp_link) 2576f7780c61SKonstantin Belousov if ((unp->unp_gcflag & UNPGC_DEAD) != 0) { 2577f7780c61SKonstantin Belousov f = unp->unp_file; 2578f7780c61SKonstantin Belousov if (unp->unp_msgcount == 0 || f == NULL || 2579f7780c61SKonstantin Belousov f->f_count != unp->unp_msgcount) 2580f7780c61SKonstantin Belousov continue; 2581f7780c61SKonstantin Belousov unref[total++] = f; 2582f7780c61SKonstantin Belousov fhold(f); 2583f7780c61SKonstantin Belousov KASSERT(total <= unp_unreachable, 2584397c19d1SJeff Roberson ("unp_gc: incorrect unreachable count.")); 2585397c19d1SJeff Roberson } 2586f7780c61SKonstantin Belousov UNP_LINK_RUNLOCK(); 258760a5ef26SRobert Watson 2588ed5b7817SJulian Elischer /* 2589397c19d1SJeff Roberson * Now flush all sockets, free'ing rights. This will free the 2590397c19d1SJeff Roberson * struct files associated with these sockets but leave each socket 2591397c19d1SJeff Roberson * with one remaining ref. 2592ed5b7817SJulian Elischer */ 25931fb51a12SBjoern A. Zeeb for (i = 0; i < total; i++) { 25941fb51a12SBjoern A. Zeeb struct socket *so; 25951fb51a12SBjoern A. Zeeb 25961fb51a12SBjoern A. Zeeb so = unref[i]->f_data; 25971fb51a12SBjoern A. Zeeb CURVNET_SET(so->so_vnet); 25981fb51a12SBjoern A. Zeeb sorflush(so); 25991fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 26001fb51a12SBjoern A. Zeeb } 260160a5ef26SRobert Watson 2602ed5b7817SJulian Elischer /* 2603397c19d1SJeff Roberson * And finally release the sockets so they can be reclaimed. 2604ed5b7817SJulian Elischer */ 2605f7780c61SKonstantin Belousov for (i = 0; i < total; i++) 2606397c19d1SJeff Roberson fdrop(unref[i], NULL); 2607f7780c61SKonstantin Belousov unp_recycled += total; 2608397c19d1SJeff Roberson free(unref, M_TEMP); 2609df8bae1dSRodney W. Grimes } 2610df8bae1dSRodney W. Grimes 26110b36cd25SRobert Watson static void 261299ab95dbSMark Johnston unp_dispose_mbuf(struct mbuf *m) 2613df8bae1dSRodney W. Grimes { 2614996c772fSJohn Dyson 2615df8bae1dSRodney W. Grimes if (m) 2616be26ba7cSPawel Jakub Dawidek unp_scan(m, unp_freerights); 2617df8bae1dSRodney W. Grimes } 2618df8bae1dSRodney W. Grimes 26190c40f353SConrad Meyer /* 26200c40f353SConrad Meyer * Synchronize against unp_gc, which can trip over data as we are freeing it. 26210c40f353SConrad Meyer */ 26220c40f353SConrad Meyer static void 262399ab95dbSMark Johnston unp_dispose(struct socket *so) 26240c40f353SConrad Meyer { 26250c40f353SConrad Meyer struct unpcb *unp; 26260c40f353SConrad Meyer 26270c40f353SConrad Meyer unp = sotounpcb(so); 2628779f106aSGleb Smirnoff UNP_LINK_WLOCK(); 26290c40f353SConrad Meyer unp->unp_gcflag |= UNPGC_IGNORE_RIGHTS; 2630779f106aSGleb Smirnoff UNP_LINK_WUNLOCK(); 2631779f106aSGleb Smirnoff if (!SOLISTENING(so)) 263299ab95dbSMark Johnston unp_dispose_mbuf(so->so_rcv.sb_mb); 26330c40f353SConrad Meyer } 26340c40f353SConrad Meyer 2635f708ef1bSPoul-Henning Kamp static void 2636be26ba7cSPawel Jakub Dawidek unp_scan(struct mbuf *m0, void (*op)(struct filedescent **, int)) 2637df8bae1dSRodney W. Grimes { 26382bc21ed9SDavid Malone struct mbuf *m; 26392bc21ed9SDavid Malone struct cmsghdr *cm; 26402bc21ed9SDavid Malone void *data; 26412bc21ed9SDavid Malone socklen_t clen, datalen; 2642df8bae1dSRodney W. Grimes 2643fc3fcacfSRobert Watson while (m0 != NULL) { 26442bc21ed9SDavid Malone for (m = m0; m; m = m->m_next) { 264512396bdcSDavid Malone if (m->m_type != MT_CONTROL) 2646df8bae1dSRodney W. Grimes continue; 26472bc21ed9SDavid Malone 26482bc21ed9SDavid Malone cm = mtod(m, struct cmsghdr *); 26492bc21ed9SDavid Malone clen = m->m_len; 26502bc21ed9SDavid Malone 26512bc21ed9SDavid Malone while (cm != NULL) { 26522bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) 26532bc21ed9SDavid Malone break; 26542bc21ed9SDavid Malone 26552bc21ed9SDavid Malone data = CMSG_DATA(cm); 26562bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len 26572bc21ed9SDavid Malone - (caddr_t)data; 26582bc21ed9SDavid Malone 26592bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET && 26602bc21ed9SDavid Malone cm->cmsg_type == SCM_RIGHTS) { 2661be26ba7cSPawel Jakub Dawidek (*op)(data, datalen / 2662be26ba7cSPawel Jakub Dawidek sizeof(struct filedescent *)); 26632bc21ed9SDavid Malone } 26642bc21ed9SDavid Malone 26652bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 26662bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 26672bc21ed9SDavid Malone cm = (struct cmsghdr *) 26682bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 26692bc21ed9SDavid Malone } else { 26702bc21ed9SDavid Malone clen = 0; 26712bc21ed9SDavid Malone cm = NULL; 26722bc21ed9SDavid Malone } 26732bc21ed9SDavid Malone } 2674df8bae1dSRodney W. Grimes } 2675c29a3321SKevin Lo m0 = m0->m_nextpkt; 2676df8bae1dSRodney W. Grimes } 2677df8bae1dSRodney W. Grimes } 2678df8bae1dSRodney W. Grimes 2679662c901cSMikolaj Golub /* 2680662c901cSMikolaj Golub * A helper function called by VFS before socket-type vnode reclamation. 2681662c901cSMikolaj Golub * For an active vnode it clears unp_vnode pointer and decrements unp_vnode 2682662c901cSMikolaj Golub * use count. 2683662c901cSMikolaj Golub */ 2684662c901cSMikolaj Golub void 2685662c901cSMikolaj Golub vfs_unp_reclaim(struct vnode *vp) 2686662c901cSMikolaj Golub { 2687662c901cSMikolaj Golub struct unpcb *unp; 2688662c901cSMikolaj Golub int active; 268975a67bf3SMatt Macy struct mtx *vplock; 2690662c901cSMikolaj Golub 2691662c901cSMikolaj Golub ASSERT_VOP_ELOCKED(vp, "vfs_unp_reclaim"); 2692662c901cSMikolaj Golub KASSERT(vp->v_type == VSOCK, 2693662c901cSMikolaj Golub ("vfs_unp_reclaim: vp->v_type != VSOCK")); 2694662c901cSMikolaj Golub 2695662c901cSMikolaj Golub active = 0; 269675a67bf3SMatt Macy vplock = mtx_pool_find(mtxpool_sleep, vp); 269775a67bf3SMatt Macy mtx_lock(vplock); 26980c3c207fSGleb Smirnoff VOP_UNP_CONNECT(vp, &unp); 2699662c901cSMikolaj Golub if (unp == NULL) 2700662c901cSMikolaj Golub goto done; 2701662c901cSMikolaj Golub UNP_PCB_LOCK(unp); 2702c7e41c8bSMikolaj Golub if (unp->unp_vnode == vp) { 2703c7e41c8bSMikolaj Golub VOP_UNP_DETACH(vp); 2704662c901cSMikolaj Golub unp->unp_vnode = NULL; 2705662c901cSMikolaj Golub active = 1; 2706662c901cSMikolaj Golub } 2707662c901cSMikolaj Golub UNP_PCB_UNLOCK(unp); 2708662c901cSMikolaj Golub done: 270975a67bf3SMatt Macy mtx_unlock(vplock); 2710662c901cSMikolaj Golub if (active) 2711662c901cSMikolaj Golub vunref(vp); 2712662c901cSMikolaj Golub } 2713662c901cSMikolaj Golub 271403c96c31SRobert Watson #ifdef DDB 271503c96c31SRobert Watson static void 271603c96c31SRobert Watson db_print_indent(int indent) 271703c96c31SRobert Watson { 271803c96c31SRobert Watson int i; 271903c96c31SRobert Watson 272003c96c31SRobert Watson for (i = 0; i < indent; i++) 272103c96c31SRobert Watson db_printf(" "); 272203c96c31SRobert Watson } 272303c96c31SRobert Watson 272403c96c31SRobert Watson static void 272503c96c31SRobert Watson db_print_unpflags(int unp_flags) 272603c96c31SRobert Watson { 272703c96c31SRobert Watson int comma; 272803c96c31SRobert Watson 272903c96c31SRobert Watson comma = 0; 273003c96c31SRobert Watson if (unp_flags & UNP_HAVEPC) { 273103c96c31SRobert Watson db_printf("%sUNP_HAVEPC", comma ? ", " : ""); 273203c96c31SRobert Watson comma = 1; 273303c96c31SRobert Watson } 273403c96c31SRobert Watson if (unp_flags & UNP_WANTCRED) { 273503c96c31SRobert Watson db_printf("%sUNP_WANTCRED", comma ? ", " : ""); 273603c96c31SRobert Watson comma = 1; 273703c96c31SRobert Watson } 273803c96c31SRobert Watson if (unp_flags & UNP_CONNWAIT) { 273903c96c31SRobert Watson db_printf("%sUNP_CONNWAIT", comma ? ", " : ""); 274003c96c31SRobert Watson comma = 1; 274103c96c31SRobert Watson } 274203c96c31SRobert Watson if (unp_flags & UNP_CONNECTING) { 274303c96c31SRobert Watson db_printf("%sUNP_CONNECTING", comma ? ", " : ""); 274403c96c31SRobert Watson comma = 1; 274503c96c31SRobert Watson } 274603c96c31SRobert Watson if (unp_flags & UNP_BINDING) { 274703c96c31SRobert Watson db_printf("%sUNP_BINDING", comma ? ", " : ""); 274803c96c31SRobert Watson comma = 1; 274903c96c31SRobert Watson } 275003c96c31SRobert Watson } 275103c96c31SRobert Watson 275203c96c31SRobert Watson static void 275303c96c31SRobert Watson db_print_xucred(int indent, struct xucred *xu) 275403c96c31SRobert Watson { 275503c96c31SRobert Watson int comma, i; 275603c96c31SRobert Watson 275703c96c31SRobert Watson db_print_indent(indent); 275803c96c31SRobert Watson db_printf("cr_version: %u cr_uid: %u cr_ngroups: %d\n", 275903c96c31SRobert Watson xu->cr_version, xu->cr_uid, xu->cr_ngroups); 276003c96c31SRobert Watson db_print_indent(indent); 276103c96c31SRobert Watson db_printf("cr_groups: "); 276203c96c31SRobert Watson comma = 0; 276303c96c31SRobert Watson for (i = 0; i < xu->cr_ngroups; i++) { 276403c96c31SRobert Watson db_printf("%s%u", comma ? ", " : "", xu->cr_groups[i]); 276503c96c31SRobert Watson comma = 1; 276603c96c31SRobert Watson } 276703c96c31SRobert Watson db_printf("\n"); 276803c96c31SRobert Watson } 276903c96c31SRobert Watson 277003c96c31SRobert Watson static void 277103c96c31SRobert Watson db_print_unprefs(int indent, struct unp_head *uh) 277203c96c31SRobert Watson { 277303c96c31SRobert Watson struct unpcb *unp; 277403c96c31SRobert Watson int counter; 277503c96c31SRobert Watson 277603c96c31SRobert Watson counter = 0; 277703c96c31SRobert Watson LIST_FOREACH(unp, uh, unp_reflink) { 277803c96c31SRobert Watson if (counter % 4 == 0) 277903c96c31SRobert Watson db_print_indent(indent); 278003c96c31SRobert Watson db_printf("%p ", unp); 278103c96c31SRobert Watson if (counter % 4 == 3) 278203c96c31SRobert Watson db_printf("\n"); 278303c96c31SRobert Watson counter++; 278403c96c31SRobert Watson } 278503c96c31SRobert Watson if (counter != 0 && counter % 4 != 0) 278603c96c31SRobert Watson db_printf("\n"); 278703c96c31SRobert Watson } 278803c96c31SRobert Watson 278903c96c31SRobert Watson DB_SHOW_COMMAND(unpcb, db_show_unpcb) 279003c96c31SRobert Watson { 279103c96c31SRobert Watson struct unpcb *unp; 279203c96c31SRobert Watson 279303c96c31SRobert Watson if (!have_addr) { 279403c96c31SRobert Watson db_printf("usage: show unpcb <addr>\n"); 279503c96c31SRobert Watson return; 279603c96c31SRobert Watson } 279703c96c31SRobert Watson unp = (struct unpcb *)addr; 279803c96c31SRobert Watson 279903c96c31SRobert Watson db_printf("unp_socket: %p unp_vnode: %p\n", unp->unp_socket, 280003c96c31SRobert Watson unp->unp_vnode); 280103c96c31SRobert Watson 2802fc8fdae0SMatthew D Fleming db_printf("unp_ino: %ju unp_conn: %p\n", (uintmax_t)unp->unp_ino, 280303c96c31SRobert Watson unp->unp_conn); 280403c96c31SRobert Watson 280503c96c31SRobert Watson db_printf("unp_refs:\n"); 280603c96c31SRobert Watson db_print_unprefs(2, &unp->unp_refs); 280703c96c31SRobert Watson 280803c96c31SRobert Watson /* XXXRW: Would be nice to print the full address, if any. */ 280903c96c31SRobert Watson db_printf("unp_addr: %p\n", unp->unp_addr); 281003c96c31SRobert Watson 2811c2090e73SAlan Somers db_printf("unp_gencnt: %llu\n", 281203c96c31SRobert Watson (unsigned long long)unp->unp_gencnt); 281303c96c31SRobert Watson 281403c96c31SRobert Watson db_printf("unp_flags: %x (", unp->unp_flags); 281503c96c31SRobert Watson db_print_unpflags(unp->unp_flags); 281603c96c31SRobert Watson db_printf(")\n"); 281703c96c31SRobert Watson 281803c96c31SRobert Watson db_printf("unp_peercred:\n"); 281903c96c31SRobert Watson db_print_xucred(2, &unp->unp_peercred); 282003c96c31SRobert Watson 282103c96c31SRobert Watson db_printf("unp_refcount: %u\n", unp->unp_refcount); 282203c96c31SRobert Watson } 282303c96c31SRobert Watson #endif 2824