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 36075a67bf3SMatt Macy unp_pcb_owned_lock2_slowpath(struct unpcb *unp, struct unpcb **unp2p, int *freed) 36175a67bf3SMatt Macy 36275a67bf3SMatt Macy { 36375a67bf3SMatt Macy struct unpcb *unp2; 36475a67bf3SMatt Macy 36575a67bf3SMatt Macy unp2 = *unp2p; 36675a67bf3SMatt Macy unp_pcb_hold((unp2)); 36775a67bf3SMatt Macy UNP_PCB_UNLOCK((unp)); 36875a67bf3SMatt Macy UNP_PCB_LOCK((unp2)); 36975a67bf3SMatt Macy UNP_PCB_LOCK((unp)); 37075a67bf3SMatt Macy *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; \ 37775a67bf3SMatt Macy UNP_PCB_LOCK_ASSERT((unp)); \ 37875a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT((unp2)); \ 37916529dacSMatt Macy MPASS(unp != unp2); \ 38075a67bf3SMatt Macy if (__predict_true(UNP_PCB_TRYLOCK((unp2)))) \ 38175a67bf3SMatt Macy break; \ 38275a67bf3SMatt Macy else if ((uintptr_t)(unp2) > (uintptr_t)(unp)) \ 38375a67bf3SMatt Macy UNP_PCB_LOCK((unp2)); \ 38475a67bf3SMatt Macy else { \ 38575a67bf3SMatt Macy unp_pcb_owned_lock2_slowpath((unp), &(unp2), &freed); \ 38675a67bf3SMatt Macy } \ 38775a67bf3SMatt Macy } while (0) 38875a67bf3SMatt Macy 38975a67bf3SMatt Macy 390e4445a03SRobert Watson /* 391e4445a03SRobert Watson * Definitions of protocols supported in the LOCAL domain. 392e4445a03SRobert Watson */ 393e4445a03SRobert Watson static struct domain localdomain; 394fa9402f2SRobert Watson static struct pr_usrreqs uipc_usrreqs_dgram, uipc_usrreqs_stream; 39584d61770SRobert Watson static struct pr_usrreqs uipc_usrreqs_seqpacket; 396e4445a03SRobert Watson static struct protosw localsw[] = { 397e4445a03SRobert Watson { 398e4445a03SRobert Watson .pr_type = SOCK_STREAM, 399e4445a03SRobert Watson .pr_domain = &localdomain, 400e4445a03SRobert Watson .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_RIGHTS, 401e4445a03SRobert Watson .pr_ctloutput = &uipc_ctloutput, 402fa9402f2SRobert Watson .pr_usrreqs = &uipc_usrreqs_stream 403e4445a03SRobert Watson }, 404e4445a03SRobert Watson { 405e4445a03SRobert Watson .pr_type = SOCK_DGRAM, 406e4445a03SRobert Watson .pr_domain = &localdomain, 407e4445a03SRobert Watson .pr_flags = PR_ATOMIC|PR_ADDR|PR_RIGHTS, 408aaf63435SGleb Smirnoff .pr_ctloutput = &uipc_ctloutput, 409fa9402f2SRobert Watson .pr_usrreqs = &uipc_usrreqs_dgram 410e4445a03SRobert Watson }, 41184d61770SRobert Watson { 41284d61770SRobert Watson .pr_type = SOCK_SEQPACKET, 41384d61770SRobert Watson .pr_domain = &localdomain, 41484d61770SRobert Watson 41584d61770SRobert Watson /* 41684d61770SRobert Watson * XXXRW: For now, PR_ADDR because soreceive will bump into them 41784d61770SRobert Watson * due to our use of sbappendaddr. A new sbappend variants is needed 41884d61770SRobert Watson * that supports both atomic record writes and control data. 41984d61770SRobert Watson */ 42084d61770SRobert Watson .pr_flags = PR_ADDR|PR_ATOMIC|PR_CONNREQUIRED|PR_WANTRCVD| 42184d61770SRobert Watson PR_RIGHTS, 422e0643280SGleb Smirnoff .pr_ctloutput = &uipc_ctloutput, 42384d61770SRobert Watson .pr_usrreqs = &uipc_usrreqs_seqpacket, 42484d61770SRobert Watson }, 425e4445a03SRobert Watson }; 426e4445a03SRobert Watson 427e4445a03SRobert Watson static struct domain localdomain = { 428e4445a03SRobert Watson .dom_family = AF_LOCAL, 429e4445a03SRobert Watson .dom_name = "local", 430e4445a03SRobert Watson .dom_init = unp_init, 431e4445a03SRobert Watson .dom_externalize = unp_externalize, 43299ab95dbSMark Johnston .dom_dispose = unp_dispose, 433e4445a03SRobert Watson .dom_protosw = localsw, 43402abd400SPedro F. Giffuni .dom_protoswNPROTOSW = &localsw[nitems(localsw)] 435e4445a03SRobert Watson }; 436e4445a03SRobert Watson DOMAIN_SET(local); 437e4445a03SRobert Watson 438ac45e92fSRobert Watson static void 439a29f300eSGarrett Wollman uipc_abort(struct socket *so) 440df8bae1dSRodney W. Grimes { 441e7c33e29SRobert Watson struct unpcb *unp, *unp2; 442df8bae1dSRodney W. Grimes 44340f2ac28SRobert Watson unp = sotounpcb(so); 4444d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_abort: unp == NULL")); 44575a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT(unp); 446e7c33e29SRobert Watson 447e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 448e7c33e29SRobert Watson unp2 = unp->unp_conn; 449e7c33e29SRobert Watson if (unp2 != NULL) { 45075a67bf3SMatt Macy unp_pcb_hold(unp2); 451e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 45275a67bf3SMatt Macy unp_drop(unp2); 45375a67bf3SMatt Macy } else 45475a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 455df8bae1dSRodney W. Grimes } 456df8bae1dSRodney W. Grimes 457a29f300eSGarrett Wollman static int 45857bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam) 459a29f300eSGarrett Wollman { 460e7c33e29SRobert Watson struct unpcb *unp, *unp2; 4610d9ce3a1SRobert Watson const struct sockaddr *sa; 462df8bae1dSRodney W. Grimes 463df8bae1dSRodney W. Grimes /* 4641c381b19SRobert Watson * Pass back name of connected socket, if it was bound and we are 4651c381b19SRobert Watson * still connected (our peer may have closed already!). 466df8bae1dSRodney W. Grimes */ 4674d4b555eSRobert Watson unp = sotounpcb(so); 4684d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_accept: unp == NULL")); 469e7c33e29SRobert Watson 4700d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 4713dab55bcSRobert Watson UNP_LINK_RLOCK(); 472e7c33e29SRobert Watson unp2 = unp->unp_conn; 473e7c33e29SRobert Watson if (unp2 != NULL && unp2->unp_addr != NULL) { 474e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 475e7c33e29SRobert Watson sa = (struct sockaddr *) unp2->unp_addr; 476e7c33e29SRobert Watson bcopy(sa, *nam, sa->sa_len); 477e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 478e7c33e29SRobert Watson } else { 4790d9ce3a1SRobert Watson sa = &sun_noname; 4800d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 481e7c33e29SRobert Watson } 4823dab55bcSRobert Watson UNP_LINK_RUNLOCK(); 483e5aeaa0cSDag-Erling Smørgrav return (0); 484a29f300eSGarrett Wollman } 485df8bae1dSRodney W. Grimes 486a29f300eSGarrett Wollman static int 487b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td) 488a29f300eSGarrett Wollman { 489e7c33e29SRobert Watson u_long sendspace, recvspace; 4906d32873cSRobert Watson struct unpcb *unp; 4913dab55bcSRobert Watson int error; 492779f106aSGleb Smirnoff bool locked; 493df8bae1dSRodney W. Grimes 4946d32873cSRobert Watson KASSERT(so->so_pcb == NULL, ("uipc_attach: so_pcb != NULL")); 4956d32873cSRobert Watson if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 4966d32873cSRobert Watson switch (so->so_type) { 4976d32873cSRobert Watson case SOCK_STREAM: 498e7c33e29SRobert Watson sendspace = unpst_sendspace; 499e7c33e29SRobert Watson recvspace = unpst_recvspace; 5006d32873cSRobert Watson break; 5016d32873cSRobert Watson 5026d32873cSRobert Watson case SOCK_DGRAM: 503e7c33e29SRobert Watson sendspace = unpdg_sendspace; 504e7c33e29SRobert Watson recvspace = unpdg_recvspace; 5056d32873cSRobert Watson break; 5066d32873cSRobert Watson 50784d61770SRobert Watson case SOCK_SEQPACKET: 50884d61770SRobert Watson sendspace = unpsp_sendspace; 50984d61770SRobert Watson recvspace = unpsp_recvspace; 51084d61770SRobert Watson break; 51184d61770SRobert Watson 5126d32873cSRobert Watson default: 513e7c33e29SRobert Watson panic("uipc_attach"); 5146d32873cSRobert Watson } 515e7c33e29SRobert Watson error = soreserve(so, sendspace, recvspace); 5166d32873cSRobert Watson if (error) 5176d32873cSRobert Watson return (error); 5186d32873cSRobert Watson } 51946a1d9bfSRobert Watson unp = uma_zalloc(unp_zone, M_NOWAIT | M_ZERO); 5206d32873cSRobert Watson if (unp == NULL) 5216d32873cSRobert Watson return (ENOBUFS); 5226d32873cSRobert Watson LIST_INIT(&unp->unp_refs); 523e7c33e29SRobert Watson UNP_PCB_LOCK_INIT(unp); 5246d32873cSRobert Watson unp->unp_socket = so; 5256d32873cSRobert Watson so->so_pcb = unp; 5269ae328fcSJohn Baldwin unp->unp_refcount = 1; 527779f106aSGleb Smirnoff if (so->so_listen != NULL) 528434ac8b6SMark Johnston unp->unp_flags |= UNP_NASCENT; 529e7c33e29SRobert Watson 530779f106aSGleb Smirnoff if ((locked = UNP_LINK_WOWNED()) == false) 531779f106aSGleb Smirnoff UNP_LINK_WLOCK(); 532779f106aSGleb Smirnoff 5336d32873cSRobert Watson unp->unp_gencnt = ++unp_gencnt; 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); 725*acf9fd05SMatt Macy if (__predict_false(unp == unp2)) { 726*acf9fd05SMatt Macy unp_disconnect(unp, unp2); 727*acf9fd05SMatt 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")); 752*acf9fd05SMatt Macy if (unp != unp2) 75375a67bf3SMatt Macy unp_pcb_lock2(unp, unp2); 754*acf9fd05SMatt Macy else 755*acf9fd05SMatt Macy UNP_PCB_LOCK(unp); 7566a2989fdSMatthew N. Dodd error = unp_connect2(so1, so2, PRU_CONNECT2); 757*acf9fd05SMatt 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 } 806*acf9fd05SMatt Macy if (__predict_false(unp == unp->unp_conn)) { 807*acf9fd05SMatt Macy unp_disconnect(unp, unp); 808*acf9fd05SMatt Macy unp2 = NULL; 809*acf9fd05SMatt Macy goto connect_self; 810*acf9fd05SMatt Macy } 811*acf9fd05SMatt Macy if ((unp2 = unp->unp_conn) != NULL) { 812*acf9fd05SMatt Macy unp_pcb_owned_lock2(unp, unp2, freeunp); 813*acf9fd05SMatt Macy if (freeunp) 814*acf9fd05SMatt Macy unp2 = NULL; 815*acf9fd05SMatt 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 } 823*acf9fd05SMatt 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 } 875*acf9fd05SMatt Macy if (unp == unp2) { 876*acf9fd05SMatt Macy if (unp_pcb_rele(unp) == 0) 877*acf9fd05SMatt Macy UNP_PCB_UNLOCK(unp); 878*acf9fd05SMatt Macy } 87975a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freed); 88075a67bf3SMatt Macy if (__predict_false(freed)) { 88175a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 88275a67bf3SMatt Macy return (0); 88375a67bf3SMatt Macy } 88475a67bf3SMatt Macy unp_pcb_hold(unp2); 88575a67bf3SMatt Macy unp_pcb_hold(unp); 88675a67bf3SMatt Macy unp_disconnect(unp, unp2); 88775a67bf3SMatt Macy if (unp_pcb_rele(unp) == 0) 88875a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 88975a67bf3SMatt Macy if (unp_pcb_rele(unp2) == 0) 89075a67bf3SMatt Macy UNP_PCB_UNLOCK(unp2); 891e5aeaa0cSDag-Erling Smørgrav return (0); 892a29f300eSGarrett Wollman } 893a29f300eSGarrett Wollman 894a29f300eSGarrett Wollman static int 895d374e81eSRobert Watson uipc_listen(struct socket *so, int backlog, struct thread *td) 896a29f300eSGarrett Wollman { 89740f2ac28SRobert Watson struct unpcb *unp; 8980d9ce3a1SRobert Watson int error; 899a29f300eSGarrett Wollman 900beb4b312SGleb Smirnoff if (so->so_type != SOCK_STREAM && so->so_type != SOCK_SEQPACKET) 901beb4b312SGleb Smirnoff return (EOPNOTSUPP); 902beb4b312SGleb Smirnoff 90340f2ac28SRobert Watson unp = sotounpcb(so); 9044d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_listen: unp == NULL")); 905e7c33e29SRobert Watson 906e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 9074d4b555eSRobert Watson if (unp->unp_vnode == NULL) { 90847a84387SEd Schouten /* Already connected or not bound to an address. */ 90947a84387SEd Schouten error = unp->unp_conn != NULL ? EINVAL : EDESTADDRREQ; 910e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 91147a84387SEd Schouten return (error); 91240f2ac28SRobert Watson } 913e7c33e29SRobert Watson 914e7c33e29SRobert Watson SOCK_LOCK(so); 915e7c33e29SRobert Watson error = solisten_proto_check(so); 916e7c33e29SRobert Watson if (error == 0) { 917e7c33e29SRobert Watson cru2x(td->td_ucred, &unp->unp_peercred); 918e7c33e29SRobert Watson solisten_proto(so, backlog); 919e7c33e29SRobert Watson } 920e7c33e29SRobert Watson SOCK_UNLOCK(so); 921e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 9220d9ce3a1SRobert Watson return (error); 923a29f300eSGarrett Wollman } 924a29f300eSGarrett Wollman 925a29f300eSGarrett Wollman static int 92657bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 927a29f300eSGarrett Wollman { 928e7c33e29SRobert Watson struct unpcb *unp, *unp2; 9290d9ce3a1SRobert Watson const struct sockaddr *sa; 930a29f300eSGarrett Wollman 9314d4b555eSRobert Watson unp = sotounpcb(so); 9324d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL")); 933e7c33e29SRobert Watson 9340d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 935afd9f91cSJohn Baldwin UNP_LINK_RLOCK(); 936bdc5f6a3SHajimu UMEMOTO /* 937e7c33e29SRobert Watson * XXX: It seems that this test always fails even when connection is 938e7c33e29SRobert Watson * established. So, this else clause is added as workaround to 939e7c33e29SRobert Watson * return PF_LOCAL sockaddr. 940bdc5f6a3SHajimu UMEMOTO */ 941e7c33e29SRobert Watson unp2 = unp->unp_conn; 942e7c33e29SRobert Watson if (unp2 != NULL) { 943e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 944e7c33e29SRobert Watson if (unp2->unp_addr != NULL) 945afd9f91cSJohn Baldwin sa = (struct sockaddr *) unp2->unp_addr; 946e7c33e29SRobert Watson else 9470d9ce3a1SRobert Watson sa = &sun_noname; 9480d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 949e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 950e7c33e29SRobert Watson } else { 951e7c33e29SRobert Watson sa = &sun_noname; 952e7c33e29SRobert Watson bcopy(sa, *nam, sa->sa_len); 953e7c33e29SRobert Watson } 954afd9f91cSJohn Baldwin UNP_LINK_RUNLOCK(); 955e5aeaa0cSDag-Erling Smørgrav return (0); 956a29f300eSGarrett Wollman } 957a29f300eSGarrett Wollman 958a29f300eSGarrett Wollman static int 959a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 960a29f300eSGarrett Wollman { 961e7c33e29SRobert Watson struct unpcb *unp, *unp2; 962a29f300eSGarrett Wollman struct socket *so2; 963337cc6b6SRobert Watson u_int mbcnt, sbcc; 964a29f300eSGarrett Wollman 96540f2ac28SRobert Watson unp = sotounpcb(so); 9662b21d0e8SGleb Smirnoff KASSERT(unp != NULL, ("%s: unp == NULL", __func__)); 9672b21d0e8SGleb Smirnoff KASSERT(so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET, 9682b21d0e8SGleb Smirnoff ("%s: socktype %d", __func__, so->so_type)); 969e7c33e29SRobert Watson 970df8bae1dSRodney W. Grimes /* 971e7c33e29SRobert Watson * Adjust backpressure on sender and wakeup any waiting to write. 972e7c33e29SRobert Watson * 973d7924b70SRobert Watson * The unp lock is acquired to maintain the validity of the unp_conn 974d7924b70SRobert Watson * pointer; no lock on unp2 is required as unp2->unp_socket will be 975d7924b70SRobert Watson * static as long as we don't permit unp2 to disconnect from unp, 976d7924b70SRobert Watson * which is prevented by the lock on unp. We cache values from 977d7924b70SRobert Watson * so_rcv to avoid holding the so_rcv lock over the entire 978d7924b70SRobert Watson * transaction on the remote so_snd. 979df8bae1dSRodney W. Grimes */ 980337cc6b6SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 981337cc6b6SRobert Watson mbcnt = so->so_rcv.sb_mbcnt; 9822b21d0e8SGleb Smirnoff sbcc = sbavail(&so->so_rcv); 983337cc6b6SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 984c2090e73SAlan Somers /* 985c2090e73SAlan Somers * There is a benign race condition at this point. If we're planning to 986c2090e73SAlan Somers * clear SB_STOP, but uipc_send is called on the connected socket at 987c2090e73SAlan Somers * this instant, it might add data to the sockbuf and set SB_STOP. Then 988c2090e73SAlan Somers * we would erroneously clear SB_STOP below, even though the sockbuf is 989c2090e73SAlan Somers * full. The race is benign because the only ill effect is to allow the 990c2090e73SAlan Somers * sockbuf to exceed its size limit, and the size limits are not 991c2090e73SAlan Somers * strictly guaranteed anyway. 992c2090e73SAlan Somers */ 993e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 994e7c33e29SRobert Watson unp2 = unp->unp_conn; 995e7c33e29SRobert Watson if (unp2 == NULL) { 996e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 997e7c33e29SRobert Watson return (0); 998337cc6b6SRobert Watson } 999e7c33e29SRobert Watson so2 = unp2->unp_socket; 1000337cc6b6SRobert Watson SOCKBUF_LOCK(&so2->so_snd); 1001c2090e73SAlan Somers if (sbcc < so2->so_snd.sb_hiwat && mbcnt < so2->so_snd.sb_mbmax) 1002c2090e73SAlan Somers so2->so_snd.sb_flags &= ~SB_STOP; 10031e4d7da7SRobert Watson sowwakeup_locked(so2); 1004e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1005e5aeaa0cSDag-Erling Smørgrav return (0); 1006a29f300eSGarrett Wollman } 1007df8bae1dSRodney W. Grimes 1008a29f300eSGarrett Wollman static int 100975a67bf3SMatt Macy connect_internal(struct socket *so, struct sockaddr *nam, struct thread *td) 101075a67bf3SMatt Macy { 101175a67bf3SMatt Macy int error; 101275a67bf3SMatt Macy struct unpcb *unp; 101375a67bf3SMatt Macy 101475a67bf3SMatt Macy unp = so->so_pcb; 101575a67bf3SMatt Macy if (unp->unp_conn != NULL) 101675a67bf3SMatt Macy return (EISCONN); 101775a67bf3SMatt Macy error = unp_connect(so, nam, td); 101875a67bf3SMatt Macy if (error) 101975a67bf3SMatt Macy return (error); 102075a67bf3SMatt Macy UNP_PCB_LOCK(unp); 102175a67bf3SMatt Macy if (unp->unp_conn == NULL) { 102275a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 102375a67bf3SMatt Macy if (error == 0) 102475a67bf3SMatt Macy error = ENOTCONN; 102575a67bf3SMatt Macy } 102675a67bf3SMatt Macy return (error); 102775a67bf3SMatt Macy } 102875a67bf3SMatt Macy 102975a67bf3SMatt Macy 103075a67bf3SMatt Macy static int 103157bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 1032b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 1033a29f300eSGarrett Wollman { 1034f3f49bbbSRobert Watson struct unpcb *unp, *unp2; 1035a29f300eSGarrett Wollman struct socket *so2; 1036c2090e73SAlan Somers u_int mbcnt, sbcc; 103775a67bf3SMatt Macy int freed, error; 1038a29f300eSGarrett Wollman 103940f2ac28SRobert Watson unp = sotounpcb(so); 10402b21d0e8SGleb Smirnoff KASSERT(unp != NULL, ("%s: unp == NULL", __func__)); 10412b21d0e8SGleb Smirnoff KASSERT(so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM || 10422b21d0e8SGleb Smirnoff so->so_type == SOCK_SEQPACKET, 10432b21d0e8SGleb Smirnoff ("%s: socktype %d", __func__, so->so_type)); 1044e7c33e29SRobert Watson 104575a67bf3SMatt Macy freed = error = 0; 1046a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 1047a29f300eSGarrett Wollman error = EOPNOTSUPP; 1048a29f300eSGarrett Wollman goto release; 1049a29f300eSGarrett Wollman } 1050fc3fcacfSRobert Watson if (control != NULL && (error = unp_internalize(&control, td))) 1051a29f300eSGarrett Wollman goto release; 105275a67bf3SMatt Macy 105375a67bf3SMatt Macy unp2 = NULL; 1054a29f300eSGarrett Wollman switch (so->so_type) { 1055a29f300eSGarrett Wollman case SOCK_DGRAM: 1056a29f300eSGarrett Wollman { 1057e7dd9a10SRobert Watson const struct sockaddr *from; 1058df8bae1dSRodney W. Grimes 1059fc3fcacfSRobert Watson if (nam != NULL) { 106075a67bf3SMatt Macy /* 106175a67bf3SMatt Macy * We return with UNP_PCB_LOCK_HELD so we know that 106275a67bf3SMatt Macy * the reference is live if the pointer is valid. 106375a67bf3SMatt Macy */ 106475a67bf3SMatt Macy if ((error = connect_internal(so, nam, td))) 1065df8bae1dSRodney W. Grimes break; 106675a67bf3SMatt Macy MPASS(unp->unp_conn != NULL); 1067e7c33e29SRobert Watson unp2 = unp->unp_conn; 106875a67bf3SMatt Macy } else { 106975a67bf3SMatt Macy UNP_PCB_LOCK(unp); 107060a5ef26SRobert Watson 1071b5ff0914SRobert Watson /* 1072b5ff0914SRobert Watson * Because connect() and send() are non-atomic in a sendto() 1073b5ff0914SRobert Watson * with a target address, it's possible that the socket will 1074b5ff0914SRobert Watson * have disconnected before the send() can run. In that case 1075b5ff0914SRobert Watson * return the slightly counter-intuitive but otherwise 1076b5ff0914SRobert Watson * correct error that the socket is not connected. 1077b5ff0914SRobert Watson */ 107875a67bf3SMatt Macy if ((unp2 = unp->unp_conn) == NULL) { 107975a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 1080b5ff0914SRobert Watson error = ENOTCONN; 1081b5ff0914SRobert Watson break; 1082b5ff0914SRobert Watson } 108375a67bf3SMatt Macy } 1084c684c14cSMatt Macy if (__predict_false(unp == unp2)) { 1085c684c14cSMatt Macy if (unp->unp_socket == NULL) { 1086c684c14cSMatt Macy error = ENOTCONN; 1087c684c14cSMatt Macy break; 1088c684c14cSMatt Macy } 1089c684c14cSMatt Macy goto connect_self; 1090c684c14cSMatt Macy } 109175a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freed); 109275a67bf3SMatt Macy if (__predict_false(freed)) { 109375a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 109475a67bf3SMatt Macy error = ENOTCONN; 109575a67bf3SMatt Macy break; 109675a67bf3SMatt Macy } 109775a67bf3SMatt Macy /* 109875a67bf3SMatt Macy * The socket referencing unp2 may have been closed 109975a67bf3SMatt Macy * or unp may have been disconnected if the unp lock 110075a67bf3SMatt Macy * was dropped to acquire unp2. 110175a67bf3SMatt Macy */ 110275a67bf3SMatt Macy if (__predict_false(unp->unp_conn == NULL) || 110375a67bf3SMatt Macy unp2->unp_socket == NULL) { 110475a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 110575a67bf3SMatt Macy if (unp_pcb_rele(unp2) == 0) 110675a67bf3SMatt Macy UNP_PCB_UNLOCK(unp2); 110775a67bf3SMatt Macy error = ENOTCONN; 110875a67bf3SMatt Macy break; 110975a67bf3SMatt Macy } 1110c684c14cSMatt Macy connect_self: 1111ede6e136SRobert Watson if (unp2->unp_flags & UNP_WANTCRED) 1112ede6e136SRobert Watson control = unp_addsockcred(td, control); 1113fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 111457bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 1115df8bae1dSRodney W. Grimes else 1116df8bae1dSRodney W. Grimes from = &sun_noname; 1117ede6e136SRobert Watson so2 = unp2->unp_socket; 1118a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 11196dde7ecbSPeter Wemm if (sbappendaddr_locked(&so2->so_rcv, from, m, 11208de34a88SAlan Somers control)) { 11211e4d7da7SRobert Watson sorwakeup_locked(so2); 1122fc3fcacfSRobert Watson m = NULL; 1123fc3fcacfSRobert Watson control = NULL; 1124e5aeaa0cSDag-Erling Smørgrav } else { 1125a34b7046SRobert Watson SOCKBUF_UNLOCK(&so2->so_rcv); 1126df8bae1dSRodney W. Grimes error = ENOBUFS; 1127e5aeaa0cSDag-Erling Smørgrav } 112875a67bf3SMatt Macy if (nam != NULL) 1129e7c33e29SRobert Watson unp_disconnect(unp, unp2); 1130c684c14cSMatt Macy if (__predict_true(unp != unp2)) 1131e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1132ede6e136SRobert Watson UNP_PCB_UNLOCK(unp); 1133df8bae1dSRodney W. Grimes break; 1134df8bae1dSRodney W. Grimes } 1135df8bae1dSRodney W. Grimes 113684d61770SRobert Watson case SOCK_SEQPACKET: 1137df8bae1dSRodney W. Grimes case SOCK_STREAM: 1138402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 1139fc3fcacfSRobert Watson if (nam != NULL) { 114075a67bf3SMatt Macy if ((error = connect_internal(so, nam, td))) 114175a67bf3SMatt Macy break; 1142402cc72dSDavid Greenman } else { 1143402cc72dSDavid Greenman error = ENOTCONN; 1144402cc72dSDavid Greenman break; 1145402cc72dSDavid Greenman } 114675a67bf3SMatt Macy } else if ((unp2 = unp->unp_conn) == NULL) { 114775a67bf3SMatt Macy error = ENOTCONN; 114875a67bf3SMatt Macy break; 114975a67bf3SMatt Macy } else if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 1150df8bae1dSRodney W. Grimes error = EPIPE; 1151df8bae1dSRodney W. Grimes break; 115275a67bf3SMatt Macy } else { 115375a67bf3SMatt Macy UNP_PCB_LOCK(unp); 115475a67bf3SMatt Macy if ((unp2 = unp->unp_conn) == NULL) { 115575a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 1156b5ff0914SRobert Watson error = ENOTCONN; 1157b5ff0914SRobert Watson break; 1158b5ff0914SRobert Watson } 115975a67bf3SMatt Macy } 116075a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freed); 116175a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 116275a67bf3SMatt Macy if (__predict_false(freed)) { 116375a67bf3SMatt Macy error = ENOTCONN; 116475a67bf3SMatt Macy break; 116575a67bf3SMatt Macy } 116675a67bf3SMatt Macy if ((so2 = unp2->unp_socket) == NULL) { 116775a67bf3SMatt Macy UNP_PCB_UNLOCK(unp2); 116875a67bf3SMatt Macy error = ENOTCONN; 116975a67bf3SMatt Macy break; 117075a67bf3SMatt Macy } 1171a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 1172f3f49bbbSRobert Watson if (unp2->unp_flags & UNP_WANTCRED) { 11736a2989fdSMatthew N. Dodd /* 1174716963cbSGleb Smirnoff * Credentials are passed only once on SOCK_STREAM 1175716963cbSGleb Smirnoff * and SOCK_SEQPACKET. 11766a2989fdSMatthew N. Dodd */ 1177f3f49bbbSRobert Watson unp2->unp_flags &= ~UNP_WANTCRED; 11786a2989fdSMatthew N. Dodd control = unp_addsockcred(td, control); 11796a2989fdSMatthew N. Dodd } 1180df8bae1dSRodney W. Grimes /* 11811c381b19SRobert Watson * Send to paired receive port, and then reduce send buffer 11821c381b19SRobert Watson * hiwater marks to maintain backpressure. Wake up readers. 1183df8bae1dSRodney W. Grimes */ 118484d61770SRobert Watson switch (so->so_type) { 118584d61770SRobert Watson case SOCK_STREAM: 1186fc3fcacfSRobert Watson if (control != NULL) { 118784d61770SRobert Watson if (sbappendcontrol_locked(&so2->so_rcv, m, 118884d61770SRobert Watson control)) 1189fc3fcacfSRobert Watson control = NULL; 1190e7c33e29SRobert Watson } else 1191829fae90SGleb Smirnoff sbappend_locked(&so2->so_rcv, m, flags); 119284d61770SRobert Watson break; 119384d61770SRobert Watson 119484d61770SRobert Watson case SOCK_SEQPACKET: { 119584d61770SRobert Watson const struct sockaddr *from; 119684d61770SRobert Watson 119784d61770SRobert Watson from = &sun_noname; 11988de34a88SAlan Somers /* 11998de34a88SAlan Somers * Don't check for space available in so2->so_rcv. 12008de34a88SAlan Somers * Unix domain sockets only check for space in the 12018de34a88SAlan Somers * sending sockbuf, and that check is performed one 12028de34a88SAlan Somers * level up the stack. 12038de34a88SAlan Somers */ 12048de34a88SAlan Somers if (sbappendaddr_nospacecheck_locked(&so2->so_rcv, 12058de34a88SAlan Somers from, m, control)) 120684d61770SRobert Watson control = NULL; 120784d61770SRobert Watson break; 120884d61770SRobert Watson } 120984d61770SRobert Watson } 121084d61770SRobert Watson 1211c2090e73SAlan Somers mbcnt = so2->so_rcv.sb_mbcnt; 12122b21d0e8SGleb Smirnoff sbcc = sbavail(&so2->so_rcv); 12132b21d0e8SGleb Smirnoff if (sbcc) 1214337cc6b6SRobert Watson sorwakeup_locked(so2); 12152b21d0e8SGleb Smirnoff else 12162b21d0e8SGleb Smirnoff SOCKBUF_UNLOCK(&so2->so_rcv); 1217337cc6b6SRobert Watson 1218c2090e73SAlan Somers /* 1219c2090e73SAlan Somers * The PCB lock on unp2 protects the SB_STOP flag. Without it, 1220c2090e73SAlan Somers * it would be possible for uipc_rcvd to be called at this 1221c2090e73SAlan Somers * point, drain the receiving sockbuf, clear SB_STOP, and then 1222c2090e73SAlan Somers * we would set SB_STOP below. That could lead to an empty 1223c2090e73SAlan Somers * sockbuf having SB_STOP set 1224c2090e73SAlan Somers */ 1225337cc6b6SRobert Watson SOCKBUF_LOCK(&so->so_snd); 1226c2090e73SAlan Somers if (sbcc >= so->so_snd.sb_hiwat || mbcnt >= so->so_snd.sb_mbmax) 1227c2090e73SAlan Somers so->so_snd.sb_flags |= SB_STOP; 12287abe2ac2SAlan Cox SOCKBUF_UNLOCK(&so->so_snd); 1229e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1230fc3fcacfSRobert Watson m = NULL; 1231df8bae1dSRodney W. Grimes break; 1232df8bae1dSRodney W. Grimes } 1233a29f300eSGarrett Wollman 12346b8fda4dSGarrett Wollman /* 123560a5ef26SRobert Watson * PRUS_EOF is equivalent to pru_send followed by pru_shutdown. 12366b8fda4dSGarrett Wollman */ 1237a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 1238ede6e136SRobert Watson UNP_PCB_LOCK(unp); 12396b8fda4dSGarrett Wollman socantsendmore(so); 12406b8fda4dSGarrett Wollman unp_shutdown(unp); 1241e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1242ede6e136SRobert Watson } 1243fc3fcacfSRobert Watson if (control != NULL && error != 0) 124499ab95dbSMark Johnston unp_dispose_mbuf(control); 1245bd508d39SDon Lewis 1246a29f300eSGarrett Wollman release: 1247fc3fcacfSRobert Watson if (control != NULL) 1248a29f300eSGarrett Wollman m_freem(control); 1249100db364SGleb Smirnoff /* 1250100db364SGleb Smirnoff * In case of PRUS_NOTREADY, uipc_ready() is responsible 1251100db364SGleb Smirnoff * for freeing memory. 1252100db364SGleb Smirnoff */ 1253100db364SGleb Smirnoff if (m != NULL && (flags & PRUS_NOTREADY) == 0) 1254a29f300eSGarrett Wollman m_freem(m); 1255e5aeaa0cSDag-Erling Smørgrav return (error); 1256a29f300eSGarrett Wollman } 1257df8bae1dSRodney W. Grimes 1258a29f300eSGarrett Wollman static int 1259c80ea19bSGleb Smirnoff uipc_ready(struct socket *so, struct mbuf *m, int count) 1260c80ea19bSGleb Smirnoff { 1261c80ea19bSGleb Smirnoff struct unpcb *unp, *unp2; 1262c80ea19bSGleb Smirnoff struct socket *so2; 1263c80ea19bSGleb Smirnoff int error; 1264c80ea19bSGleb Smirnoff 1265c80ea19bSGleb Smirnoff unp = sotounpcb(so); 1266c80ea19bSGleb Smirnoff 1267c80ea19bSGleb Smirnoff UNP_LINK_RLOCK(); 1268100db364SGleb Smirnoff if ((unp2 = unp->unp_conn) == NULL) { 1269100db364SGleb Smirnoff UNP_LINK_RUNLOCK(); 1270100db364SGleb Smirnoff for (int i = 0; i < count; i++) 1271100db364SGleb Smirnoff m = m_free(m); 1272100db364SGleb Smirnoff return (ECONNRESET); 1273100db364SGleb Smirnoff } 1274c80ea19bSGleb Smirnoff UNP_PCB_LOCK(unp2); 1275c80ea19bSGleb Smirnoff so2 = unp2->unp_socket; 1276c80ea19bSGleb Smirnoff 1277c80ea19bSGleb Smirnoff SOCKBUF_LOCK(&so2->so_rcv); 1278c80ea19bSGleb Smirnoff if ((error = sbready(&so2->so_rcv, m, count)) == 0) 1279c80ea19bSGleb Smirnoff sorwakeup_locked(so2); 1280c80ea19bSGleb Smirnoff else 1281c80ea19bSGleb Smirnoff SOCKBUF_UNLOCK(&so2->so_rcv); 1282c80ea19bSGleb Smirnoff 1283c80ea19bSGleb Smirnoff UNP_PCB_UNLOCK(unp2); 1284c80ea19bSGleb Smirnoff UNP_LINK_RUNLOCK(); 1285c80ea19bSGleb Smirnoff 1286c80ea19bSGleb Smirnoff return (error); 1287c80ea19bSGleb Smirnoff } 1288c80ea19bSGleb Smirnoff 1289c80ea19bSGleb Smirnoff static int 1290a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 1291a29f300eSGarrett Wollman { 1292c2090e73SAlan Somers struct unpcb *unp; 1293a29f300eSGarrett Wollman 129440f2ac28SRobert Watson unp = sotounpcb(so); 12954d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sense: unp == NULL")); 1296e7c33e29SRobert Watson 1297a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 1298e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 1299f3732fd1SPoul-Henning Kamp sb->st_dev = NODEV; 1300df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 13016f782c46SJeffrey Hsu unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 1302a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 1303e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1304df8bae1dSRodney W. Grimes return (0); 1305a29f300eSGarrett Wollman } 1306df8bae1dSRodney W. Grimes 1307a29f300eSGarrett Wollman static int 1308a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 1309a29f300eSGarrett Wollman { 131040f2ac28SRobert Watson struct unpcb *unp; 1311df8bae1dSRodney W. Grimes 131240f2ac28SRobert Watson unp = sotounpcb(so); 13134d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_shutdown: unp == NULL")); 1314e7c33e29SRobert Watson 1315e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 1316a29f300eSGarrett Wollman socantsendmore(so); 1317a29f300eSGarrett Wollman unp_shutdown(unp); 1318e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1319e5aeaa0cSDag-Erling Smørgrav return (0); 1320a29f300eSGarrett Wollman } 1321df8bae1dSRodney W. Grimes 1322a29f300eSGarrett Wollman static int 132357bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 1324a29f300eSGarrett Wollman { 132540f2ac28SRobert Watson struct unpcb *unp; 13260d9ce3a1SRobert Watson const struct sockaddr *sa; 1327a29f300eSGarrett Wollman 13284d4b555eSRobert Watson unp = sotounpcb(so); 13294d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL")); 1330e7c33e29SRobert Watson 13310d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 1332e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 1333fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 13340d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_addr; 133583f3198bSThomas Moestl else 13360d9ce3a1SRobert Watson sa = &sun_noname; 13370d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 1338e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1339e5aeaa0cSDag-Erling Smørgrav return (0); 1340df8bae1dSRodney W. Grimes } 1341a29f300eSGarrett Wollman 1342fa9402f2SRobert Watson static struct pr_usrreqs uipc_usrreqs_dgram = { 1343756d52a1SPoul-Henning Kamp .pru_abort = uipc_abort, 1344756d52a1SPoul-Henning Kamp .pru_accept = uipc_accept, 1345756d52a1SPoul-Henning Kamp .pru_attach = uipc_attach, 1346756d52a1SPoul-Henning Kamp .pru_bind = uipc_bind, 13477493f24eSPawel Jakub Dawidek .pru_bindat = uipc_bindat, 1348756d52a1SPoul-Henning Kamp .pru_connect = uipc_connect, 13497493f24eSPawel Jakub Dawidek .pru_connectat = uipc_connectat, 1350756d52a1SPoul-Henning Kamp .pru_connect2 = uipc_connect2, 1351756d52a1SPoul-Henning Kamp .pru_detach = uipc_detach, 1352756d52a1SPoul-Henning Kamp .pru_disconnect = uipc_disconnect, 1353756d52a1SPoul-Henning Kamp .pru_listen = uipc_listen, 1354756d52a1SPoul-Henning Kamp .pru_peeraddr = uipc_peeraddr, 1355756d52a1SPoul-Henning Kamp .pru_rcvd = uipc_rcvd, 1356756d52a1SPoul-Henning Kamp .pru_send = uipc_send, 1357756d52a1SPoul-Henning Kamp .pru_sense = uipc_sense, 1358756d52a1SPoul-Henning Kamp .pru_shutdown = uipc_shutdown, 1359756d52a1SPoul-Henning Kamp .pru_sockaddr = uipc_sockaddr, 1360fa9402f2SRobert Watson .pru_soreceive = soreceive_dgram, 1361fa9402f2SRobert Watson .pru_close = uipc_close, 1362fa9402f2SRobert Watson }; 1363fa9402f2SRobert Watson 136484d61770SRobert Watson static struct pr_usrreqs uipc_usrreqs_seqpacket = { 136584d61770SRobert Watson .pru_abort = uipc_abort, 136684d61770SRobert Watson .pru_accept = uipc_accept, 136784d61770SRobert Watson .pru_attach = uipc_attach, 136884d61770SRobert Watson .pru_bind = uipc_bind, 13697493f24eSPawel Jakub Dawidek .pru_bindat = uipc_bindat, 137084d61770SRobert Watson .pru_connect = uipc_connect, 13717493f24eSPawel Jakub Dawidek .pru_connectat = uipc_connectat, 137284d61770SRobert Watson .pru_connect2 = uipc_connect2, 137384d61770SRobert Watson .pru_detach = uipc_detach, 137484d61770SRobert Watson .pru_disconnect = uipc_disconnect, 137584d61770SRobert Watson .pru_listen = uipc_listen, 137684d61770SRobert Watson .pru_peeraddr = uipc_peeraddr, 137784d61770SRobert Watson .pru_rcvd = uipc_rcvd, 137884d61770SRobert Watson .pru_send = uipc_send, 137984d61770SRobert Watson .pru_sense = uipc_sense, 138084d61770SRobert Watson .pru_shutdown = uipc_shutdown, 138184d61770SRobert Watson .pru_sockaddr = uipc_sockaddr, 138284d61770SRobert Watson .pru_soreceive = soreceive_generic, /* XXX: or...? */ 138384d61770SRobert Watson .pru_close = uipc_close, 138484d61770SRobert Watson }; 138584d61770SRobert Watson 1386fa9402f2SRobert Watson static struct pr_usrreqs uipc_usrreqs_stream = { 1387fa9402f2SRobert Watson .pru_abort = uipc_abort, 1388fa9402f2SRobert Watson .pru_accept = uipc_accept, 1389fa9402f2SRobert Watson .pru_attach = uipc_attach, 1390fa9402f2SRobert Watson .pru_bind = uipc_bind, 13917493f24eSPawel Jakub Dawidek .pru_bindat = uipc_bindat, 1392fa9402f2SRobert Watson .pru_connect = uipc_connect, 13937493f24eSPawel Jakub Dawidek .pru_connectat = uipc_connectat, 1394fa9402f2SRobert Watson .pru_connect2 = uipc_connect2, 1395fa9402f2SRobert Watson .pru_detach = uipc_detach, 1396fa9402f2SRobert Watson .pru_disconnect = uipc_disconnect, 1397fa9402f2SRobert Watson .pru_listen = uipc_listen, 1398fa9402f2SRobert Watson .pru_peeraddr = uipc_peeraddr, 1399fa9402f2SRobert Watson .pru_rcvd = uipc_rcvd, 1400fa9402f2SRobert Watson .pru_send = uipc_send, 1401c80ea19bSGleb Smirnoff .pru_ready = uipc_ready, 1402fa9402f2SRobert Watson .pru_sense = uipc_sense, 1403fa9402f2SRobert Watson .pru_shutdown = uipc_shutdown, 1404fa9402f2SRobert Watson .pru_sockaddr = uipc_sockaddr, 1405fa9402f2SRobert Watson .pru_soreceive = soreceive_generic, 1406a152f8a3SRobert Watson .pru_close = uipc_close, 1407a29f300eSGarrett Wollman }; 1408df8bae1dSRodney W. Grimes 14090b36cd25SRobert Watson static int 1410892af6b9SRobert Watson uipc_ctloutput(struct socket *so, struct sockopt *sopt) 14110c1bb4fbSDima Dorfman { 141240f2ac28SRobert Watson struct unpcb *unp; 14130d9ce3a1SRobert Watson struct xucred xu; 14146a2989fdSMatthew N. Dodd int error, optval; 14156a2989fdSMatthew N. Dodd 141696a041b5SMatthew N. Dodd if (sopt->sopt_level != 0) 141796a041b5SMatthew N. Dodd return (EINVAL); 141896a041b5SMatthew N. Dodd 14196a2989fdSMatthew N. Dodd unp = sotounpcb(so); 14204d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL")); 14216a2989fdSMatthew N. Dodd error = 0; 14220c1bb4fbSDima Dorfman switch (sopt->sopt_dir) { 14230c1bb4fbSDima Dorfman case SOPT_GET: 14240c1bb4fbSDima Dorfman switch (sopt->sopt_name) { 14250c1bb4fbSDima Dorfman case LOCAL_PEERCRED: 1426e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 14270c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC) 14280d9ce3a1SRobert Watson xu = unp->unp_peercred; 14290c1bb4fbSDima Dorfman else { 14300c1bb4fbSDima Dorfman if (so->so_type == SOCK_STREAM) 14310c1bb4fbSDima Dorfman error = ENOTCONN; 14320c1bb4fbSDima Dorfman else 14330c1bb4fbSDima Dorfman error = EINVAL; 14340c1bb4fbSDima Dorfman } 1435e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 14360d9ce3a1SRobert Watson if (error == 0) 14370d9ce3a1SRobert Watson error = sooptcopyout(sopt, &xu, sizeof(xu)); 14380c1bb4fbSDima Dorfman break; 1439e7c33e29SRobert Watson 14406a2989fdSMatthew N. Dodd case LOCAL_CREDS: 1441a6357845SRobert Watson /* Unlocked read. */ 14426a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0; 14436a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 14446a2989fdSMatthew N. Dodd break; 1445e7c33e29SRobert Watson 14466a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 1447a6357845SRobert Watson /* Unlocked read. */ 14486a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0; 14496a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 14506a2989fdSMatthew N. Dodd break; 1451e7c33e29SRobert Watson 14520c1bb4fbSDima Dorfman default: 14530c1bb4fbSDima Dorfman error = EOPNOTSUPP; 14540c1bb4fbSDima Dorfman break; 14550c1bb4fbSDima Dorfman } 14560c1bb4fbSDima Dorfman break; 1457e7c33e29SRobert Watson 14580c1bb4fbSDima Dorfman case SOPT_SET: 14596a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 14606a2989fdSMatthew N. Dodd case LOCAL_CREDS: 14616a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 14626a2989fdSMatthew N. Dodd error = sooptcopyin(sopt, &optval, sizeof(optval), 14636a2989fdSMatthew N. Dodd sizeof(optval)); 14646a2989fdSMatthew N. Dodd if (error) 14656a2989fdSMatthew N. Dodd break; 14666a2989fdSMatthew N. Dodd 1467e7c33e29SRobert Watson #define OPTSET(bit) do { \ 1468e7c33e29SRobert Watson UNP_PCB_LOCK(unp); \ 14696a2989fdSMatthew N. Dodd if (optval) \ 14706a2989fdSMatthew N. Dodd unp->unp_flags |= bit; \ 14716a2989fdSMatthew N. Dodd else \ 1472e7c33e29SRobert Watson unp->unp_flags &= ~bit; \ 1473e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); \ 1474e7c33e29SRobert Watson } while (0) 14756a2989fdSMatthew N. Dodd 14766a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 14776a2989fdSMatthew N. Dodd case LOCAL_CREDS: 14786a2989fdSMatthew N. Dodd OPTSET(UNP_WANTCRED); 14796a2989fdSMatthew N. Dodd break; 1480e7c33e29SRobert Watson 14816a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 14826a2989fdSMatthew N. Dodd OPTSET(UNP_CONNWAIT); 14836a2989fdSMatthew N. Dodd break; 1484e7c33e29SRobert Watson 14856a2989fdSMatthew N. Dodd default: 14866a2989fdSMatthew N. Dodd break; 14876a2989fdSMatthew N. Dodd } 14886a2989fdSMatthew N. Dodd break; 14896a2989fdSMatthew N. Dodd #undef OPTSET 14906a2989fdSMatthew N. Dodd default: 14916a2989fdSMatthew N. Dodd error = ENOPROTOOPT; 14926a2989fdSMatthew N. Dodd break; 14936a2989fdSMatthew N. Dodd } 1494abb886faSMatthew N. Dodd break; 1495e7c33e29SRobert Watson 14960c1bb4fbSDima Dorfman default: 14970c1bb4fbSDima Dorfman error = EOPNOTSUPP; 14980c1bb4fbSDima Dorfman break; 14990c1bb4fbSDima Dorfman } 15000c1bb4fbSDima Dorfman return (error); 15010c1bb4fbSDima Dorfman } 15020c1bb4fbSDima Dorfman 1503f708ef1bSPoul-Henning Kamp static int 1504892af6b9SRobert Watson unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1505df8bae1dSRodney W. Grimes { 15067493f24eSPawel Jakub Dawidek 15077493f24eSPawel Jakub Dawidek return (unp_connectat(AT_FDCWD, so, nam, td)); 15087493f24eSPawel Jakub Dawidek } 15097493f24eSPawel Jakub Dawidek 15107493f24eSPawel Jakub Dawidek static int 15117493f24eSPawel Jakub Dawidek unp_connectat(int fd, struct socket *so, struct sockaddr *nam, 15127493f24eSPawel Jakub Dawidek struct thread *td) 15137493f24eSPawel Jakub Dawidek { 1514892af6b9SRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam; 1515892af6b9SRobert Watson struct vnode *vp; 1516779f106aSGleb Smirnoff struct socket *so2; 1517b295bdcdSRobert Watson struct unpcb *unp, *unp2, *unp3; 1518df8bae1dSRodney W. Grimes struct nameidata nd; 151957bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 15200d9ce3a1SRobert Watson struct sockaddr *sa; 15217008be5bSPawel Jakub Dawidek cap_rights_t rights; 152275a67bf3SMatt Macy int error, len, freed; 152375a67bf3SMatt Macy struct mtx *vplock; 15240d9ce3a1SRobert Watson 1525cb7df69bSKevin Lo if (nam->sa_family != AF_UNIX) 1526cb7df69bSKevin Lo return (EAFNOSUPPORT); 1527a06534c3SBjoern A. Zeeb if (nam->sa_len > sizeof(struct sockaddr_un)) 1528a06534c3SBjoern A. Zeeb return (EINVAL); 152957bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 153057bf258eSGarrett Wollman if (len <= 0) 1531e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 15327928893dSEd Maste bcopy(soun->sun_path, buf, len); 15337928893dSEd Maste buf[len] = 0; 1534e7c33e29SRobert Watson 153575a67bf3SMatt Macy unp = sotounpcb(so); 1536e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 15374f1f0ef5SRobert Watson if (unp->unp_flags & UNP_CONNECTING) { 1538e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 15394f1f0ef5SRobert Watson return (EALREADY); 15404f1f0ef5SRobert Watson } 154105102f04SRobert Watson unp->unp_flags |= UNP_CONNECTING; 1542e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1543e7c33e29SRobert Watson 15440d9ce3a1SRobert Watson sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 15457493f24eSPawel Jakub Dawidek NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, 15467008be5bSPawel Jakub Dawidek UIO_SYSSPACE, buf, fd, cap_rights_init(&rights, CAP_CONNECTAT), td); 1547797f2d22SPoul-Henning Kamp error = namei(&nd); 1548797f2d22SPoul-Henning Kamp if (error) 15490d9ce3a1SRobert Watson vp = NULL; 15500d9ce3a1SRobert Watson else 1551df8bae1dSRodney W. Grimes vp = nd.ni_vp; 15520d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_connect"); 1553762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 15540d9ce3a1SRobert Watson if (error) 15550d9ce3a1SRobert Watson goto bad; 15560d9ce3a1SRobert Watson 1557df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 1558df8bae1dSRodney W. Grimes error = ENOTSOCK; 1559df8bae1dSRodney W. Grimes goto bad; 1560df8bae1dSRodney W. Grimes } 15616fac927cSRobert Watson #ifdef MAC 156230d239bcSRobert Watson error = mac_vnode_check_open(td->td_ucred, vp, VWRITE | VREAD); 15636fac927cSRobert Watson if (error) 15646fac927cSRobert Watson goto bad; 15656fac927cSRobert Watson #endif 1566a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 1567797f2d22SPoul-Henning Kamp if (error) 1568df8bae1dSRodney W. Grimes goto bad; 1569e7c33e29SRobert Watson 1570b295bdcdSRobert Watson unp = sotounpcb(so); 15714d4b555eSRobert Watson KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 1572e7c33e29SRobert Watson 157375a67bf3SMatt Macy vplock = mtx_pool_find(mtxpool_sleep, vp); 157475a67bf3SMatt Macy mtx_lock(vplock); 15750c3c207fSGleb Smirnoff VOP_UNP_CONNECT(vp, &unp2); 15760c3c207fSGleb Smirnoff if (unp2 == NULL) { 1577df8bae1dSRodney W. Grimes error = ECONNREFUSED; 15782260c03dSRobert Watson goto bad2; 1579df8bae1dSRodney W. Grimes } 15800c3c207fSGleb Smirnoff so2 = unp2->unp_socket; 1581df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 1582df8bae1dSRodney W. Grimes error = EPROTOTYPE; 15832260c03dSRobert Watson goto bad2; 1584df8bae1dSRodney W. Grimes } 1585df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 1586e7c33e29SRobert Watson if (so2->so_options & SO_ACCEPTCONN) { 15871fb51a12SBjoern A. Zeeb CURVNET_SET(so2->so_vnet); 1588779f106aSGleb Smirnoff so2 = sonewconn(so2, 0); 15891fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 1590e7c33e29SRobert Watson } else 1591779f106aSGleb Smirnoff so2 = NULL; 1592779f106aSGleb Smirnoff if (so2 == NULL) { 1593df8bae1dSRodney W. Grimes error = ECONNREFUSED; 1594cb8f450bSMatt Macy goto bad2; 1595df8bae1dSRodney W. Grimes } 1596779f106aSGleb Smirnoff unp3 = sotounpcb(so2); 1597e10ef65dSMatt Macy unp_pcb_lock2(unp2, unp3); 15980d9ce3a1SRobert Watson if (unp2->unp_addr != NULL) { 15990d9ce3a1SRobert Watson bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 16000d9ce3a1SRobert Watson unp3->unp_addr = (struct sockaddr_un *) sa; 16010d9ce3a1SRobert Watson sa = NULL; 16020d9ce3a1SRobert Watson } 1603b523ec24SRobert Watson 16040c1bb4fbSDima Dorfman /* 16057a2b450fSEitan Adler * The connector's (client's) credentials are copied from its 16061c381b19SRobert Watson * process structure at the time of connect() (which is now). 16070c1bb4fbSDima Dorfman */ 1608a854ed98SJohn Baldwin cru2x(td->td_ucred, &unp3->unp_peercred); 16090c1bb4fbSDima Dorfman unp3->unp_flags |= UNP_HAVEPC; 1610b523ec24SRobert Watson 16110c1bb4fbSDima Dorfman /* 16121c381b19SRobert Watson * The receiver's (server's) credentials are copied from the 16131c381b19SRobert Watson * unp_peercred member of socket on which the former called 1614e7c33e29SRobert Watson * listen(); uipc_listen() cached that process's credentials 16151c381b19SRobert Watson * at that time so we can use them now. 16160c1bb4fbSDima Dorfman */ 16170c1bb4fbSDima Dorfman memcpy(&unp->unp_peercred, &unp2->unp_peercred, 16180c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 16190c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPC; 1620481f8fe8SMaxim Konovalov if (unp2->unp_flags & UNP_WANTCRED) 1621481f8fe8SMaxim Konovalov unp3->unp_flags |= UNP_WANTCRED; 1622e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1623779f106aSGleb Smirnoff unp2 = unp3; 162475a67bf3SMatt Macy unp_pcb_owned_lock2(unp2, unp, freed); 1625e10ef65dSMatt Macy if (__predict_false(freed)) { 1626e10ef65dSMatt Macy UNP_PCB_UNLOCK(unp2); 1627e10ef65dSMatt Macy error = ECONNREFUSED; 1628e10ef65dSMatt Macy goto bad2; 1629e10ef65dSMatt Macy } 1630335654d7SRobert Watson #ifdef MAC 1631779f106aSGleb Smirnoff mac_socketpeer_set_from_socket(so, so2); 1632779f106aSGleb Smirnoff mac_socketpeer_set_from_socket(so2, so); 1633335654d7SRobert Watson #endif 1634a3a73490SMatt Macy } else { 1635a3a73490SMatt Macy if (unp == unp2) 1636a3a73490SMatt Macy UNP_PCB_LOCK(unp); 1637a3a73490SMatt Macy else 1638e10ef65dSMatt Macy unp_pcb_lock2(unp, unp2); 1639a3a73490SMatt Macy } 1640779f106aSGleb Smirnoff KASSERT(unp2 != NULL && so2 != NULL && unp2->unp_socket == so2 && 1641779f106aSGleb Smirnoff sotounpcb(so2) == unp2, 1642779f106aSGleb Smirnoff ("%s: unp2 %p so2 %p", __func__, unp2, so2)); 16436a2989fdSMatthew N. Dodd error = unp_connect2(so, so2, PRU_CONNECT); 1644a3a73490SMatt Macy if (unp != unp2) 1645e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1646e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 16470d9ce3a1SRobert Watson bad2: 164875a67bf3SMatt Macy mtx_unlock(vplock); 1649df8bae1dSRodney W. Grimes bad: 165075a67bf3SMatt Macy if (vp != NULL) { 1651df8bae1dSRodney W. Grimes vput(vp); 165275a67bf3SMatt Macy } 16530d9ce3a1SRobert Watson free(sa, M_SONAME); 1654e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 16554f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_CONNECTING; 1656e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1657df8bae1dSRodney W. Grimes return (error); 1658df8bae1dSRodney W. Grimes } 1659df8bae1dSRodney W. Grimes 1660db48c0d2SRobert Watson static int 16616a2989fdSMatthew N. Dodd unp_connect2(struct socket *so, struct socket *so2, int req) 1662df8bae1dSRodney W. Grimes { 1663e7c33e29SRobert Watson struct unpcb *unp; 1664892af6b9SRobert Watson struct unpcb *unp2; 1665df8bae1dSRodney W. Grimes 1666e7c33e29SRobert Watson unp = sotounpcb(so); 1667e7c33e29SRobert Watson KASSERT(unp != NULL, ("unp_connect2: unp == NULL")); 1668e7c33e29SRobert Watson unp2 = sotounpcb(so2); 1669e7c33e29SRobert Watson KASSERT(unp2 != NULL, ("unp_connect2: unp2 == NULL")); 1670e7c33e29SRobert Watson 1671e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 1672e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp2); 16730d9ce3a1SRobert Watson 1674df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 1675df8bae1dSRodney W. Grimes return (EPROTOTYPE); 1676434ac8b6SMark Johnston unp2->unp_flags &= ~UNP_NASCENT; 1677df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 167875a67bf3SMatt Macy unp_pcb_hold(unp2); 167975a67bf3SMatt Macy unp_pcb_hold(unp); 1680df8bae1dSRodney W. Grimes switch (so->so_type) { 1681df8bae1dSRodney W. Grimes case SOCK_DGRAM: 168275a67bf3SMatt Macy UNP_REF_LIST_LOCK(); 168398271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 168475a67bf3SMatt Macy UNP_REF_LIST_UNLOCK(); 1685df8bae1dSRodney W. Grimes soisconnected(so); 1686df8bae1dSRodney W. Grimes break; 1687df8bae1dSRodney W. Grimes 1688df8bae1dSRodney W. Grimes case SOCK_STREAM: 168984d61770SRobert Watson case SOCK_SEQPACKET: 1690df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 16916a2989fdSMatthew N. Dodd if (req == PRU_CONNECT && 16926a2989fdSMatthew N. Dodd ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT)) 16936a2989fdSMatthew N. Dodd soisconnecting(so); 16946a2989fdSMatthew N. Dodd else 1695df8bae1dSRodney W. Grimes soisconnected(so); 1696df8bae1dSRodney W. Grimes soisconnected(so2); 1697df8bae1dSRodney W. Grimes break; 1698df8bae1dSRodney W. Grimes 1699df8bae1dSRodney W. Grimes default: 1700df8bae1dSRodney W. Grimes panic("unp_connect2"); 1701df8bae1dSRodney W. Grimes } 1702df8bae1dSRodney W. Grimes return (0); 1703df8bae1dSRodney W. Grimes } 1704df8bae1dSRodney W. Grimes 1705f708ef1bSPoul-Henning Kamp static void 1706e7c33e29SRobert Watson unp_disconnect(struct unpcb *unp, struct unpcb *unp2) 1707df8bae1dSRodney W. Grimes { 170875a67bf3SMatt Macy struct socket *so, *so2; 17094b06dee1SMatt Macy int freed __unused; 1710df8bae1dSRodney W. Grimes 1711e7c33e29SRobert Watson KASSERT(unp2 != NULL, ("unp_disconnect: unp2 == NULL")); 17120d9ce3a1SRobert Watson 1713e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 1714e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp2); 1715e7c33e29SRobert Watson 171675a67bf3SMatt Macy if (unp->unp_conn == NULL && unp2->unp_conn == NULL) 171775a67bf3SMatt Macy return; 171875a67bf3SMatt Macy 171975a67bf3SMatt Macy MPASS(unp->unp_conn == unp2); 1720fc3fcacfSRobert Watson unp->unp_conn = NULL; 172175a67bf3SMatt Macy so = unp->unp_socket; 172275a67bf3SMatt Macy so2 = unp2->unp_socket; 1723df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 1724df8bae1dSRodney W. Grimes case SOCK_DGRAM: 172575a67bf3SMatt Macy UNP_REF_LIST_LOCK(); 172698271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 172775a67bf3SMatt Macy UNP_REF_LIST_UNLOCK(); 172875a67bf3SMatt Macy if (so) { 17291b2e3b4bSRobert Watson SOCK_LOCK(so); 17301b2e3b4bSRobert Watson so->so_state &= ~SS_ISCONNECTED; 17311b2e3b4bSRobert Watson SOCK_UNLOCK(so); 173275a67bf3SMatt Macy } 1733df8bae1dSRodney W. Grimes break; 1734df8bae1dSRodney W. Grimes 1735df8bae1dSRodney W. Grimes case SOCK_STREAM: 173684d61770SRobert Watson case SOCK_SEQPACKET: 173775a67bf3SMatt Macy if (so) 173875a67bf3SMatt Macy soisdisconnected(so); 173975a67bf3SMatt Macy MPASS(unp2->unp_conn == unp); 1740fc3fcacfSRobert Watson unp2->unp_conn = NULL; 174175a67bf3SMatt Macy if (so2) 174275a67bf3SMatt Macy soisdisconnected(so2); 1743df8bae1dSRodney W. Grimes break; 1744df8bae1dSRodney W. Grimes } 17454b06dee1SMatt Macy freed = unp_pcb_rele(unp); 174675a67bf3SMatt Macy MPASS(freed == 0); 17474b06dee1SMatt Macy freed = unp_pcb_rele(unp2); 174875a67bf3SMatt Macy MPASS(freed == 0); 1749df8bae1dSRodney W. Grimes } 1750df8bae1dSRodney W. Grimes 17510d9ce3a1SRobert Watson /* 1752d7924b70SRobert Watson * unp_pcblist() walks the global list of struct unpcb's to generate a 1753d7924b70SRobert Watson * pointer list, bumping the refcount on each unpcb. It then copies them out 1754d7924b70SRobert Watson * sequentially, validating the generation number on each to see if it has 1755d7924b70SRobert Watson * been detached. All of this is necessary because copyout() may sleep on 1756d7924b70SRobert Watson * disk I/O. 17570d9ce3a1SRobert Watson */ 175898271db4SGarrett Wollman static int 175982d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 176098271db4SGarrett Wollman { 176198271db4SGarrett Wollman struct unpcb *unp, **unp_list; 176298271db4SGarrett Wollman unp_gen_t gencnt; 17638f364875SJulian Elischer struct xunpgen *xug; 176498271db4SGarrett Wollman struct unp_head *head; 17658f364875SJulian Elischer struct xunpcb *xu; 1766d821d364SPedro F. Giffuni u_int i; 176744c514b1SPedro F. Giffuni int error, freeunp, n; 176898271db4SGarrett Wollman 176984d61770SRobert Watson switch ((intptr_t)arg1) { 177084d61770SRobert Watson case SOCK_STREAM: 177184d61770SRobert Watson head = &unp_shead; 177284d61770SRobert Watson break; 177384d61770SRobert Watson 177484d61770SRobert Watson case SOCK_DGRAM: 177584d61770SRobert Watson head = &unp_dhead; 177684d61770SRobert Watson break; 177784d61770SRobert Watson 177884d61770SRobert Watson case SOCK_SEQPACKET: 177984d61770SRobert Watson head = &unp_sphead; 178084d61770SRobert Watson break; 178184d61770SRobert Watson 178284d61770SRobert Watson default: 1783604f19c9SRobert Watson panic("unp_pcblist: arg1 %d", (int)(intptr_t)arg1); 178484d61770SRobert Watson } 178598271db4SGarrett Wollman 178698271db4SGarrett Wollman /* 178798271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 178898271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 178998271db4SGarrett Wollman */ 1790fc3fcacfSRobert Watson if (req->oldptr == NULL) { 179198271db4SGarrett Wollman n = unp_count; 17928f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug) 179398271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 1794e5aeaa0cSDag-Erling Smørgrav return (0); 179598271db4SGarrett Wollman } 179698271db4SGarrett Wollman 1797fc3fcacfSRobert Watson if (req->newptr != NULL) 1798e5aeaa0cSDag-Erling Smørgrav return (EPERM); 179998271db4SGarrett Wollman 180098271db4SGarrett Wollman /* 180198271db4SGarrett Wollman * OK, now we're committed to doing something. 180298271db4SGarrett Wollman */ 1803a163d034SWarner Losh xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 1804779f106aSGleb Smirnoff UNP_LINK_RLOCK(); 180598271db4SGarrett Wollman gencnt = unp_gencnt; 180698271db4SGarrett Wollman n = unp_count; 1807779f106aSGleb Smirnoff UNP_LINK_RUNLOCK(); 180898271db4SGarrett Wollman 18098f364875SJulian Elischer xug->xug_len = sizeof *xug; 18108f364875SJulian Elischer xug->xug_count = n; 18118f364875SJulian Elischer xug->xug_gen = gencnt; 18128f364875SJulian Elischer xug->xug_sogen = so_gencnt; 18138f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 18148f364875SJulian Elischer if (error) { 18158f364875SJulian Elischer free(xug, M_TEMP); 1816e5aeaa0cSDag-Erling Smørgrav return (error); 18178f364875SJulian Elischer } 181898271db4SGarrett Wollman 1819a163d034SWarner Losh unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 182098271db4SGarrett Wollman 1821779f106aSGleb Smirnoff UNP_LINK_RLOCK(); 18222e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 18232e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 1824e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 18258a7d8cc6SRobert Watson if (unp->unp_gencnt <= gencnt) { 1826a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred, 1827e7c33e29SRobert Watson unp->unp_socket->so_cred)) { 1828e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 18294787fd37SPaul Saab continue; 1830e7c33e29SRobert Watson } 183198271db4SGarrett Wollman unp_list[i++] = unp; 183275a67bf3SMatt Macy unp_pcb_hold(unp); 183398271db4SGarrett Wollman } 1834e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 18354787fd37SPaul Saab } 1836779f106aSGleb Smirnoff UNP_LINK_RUNLOCK(); 18371c381b19SRobert Watson n = i; /* In case we lost some during malloc. */ 183898271db4SGarrett Wollman 183998271db4SGarrett Wollman error = 0; 1840fe2eee82SColin Percival xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO); 184198271db4SGarrett Wollman for (i = 0; i < n; i++) { 184298271db4SGarrett Wollman unp = unp_list[i]; 1843e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 184475a67bf3SMatt Macy freeunp = unp_pcb_rele(unp); 184575a67bf3SMatt Macy 184675a67bf3SMatt Macy if (freeunp == 0 && unp->unp_gencnt <= gencnt) { 18478f364875SJulian Elischer xu->xu_len = sizeof *xu; 18488f364875SJulian Elischer xu->xu_unpp = unp; 184998271db4SGarrett Wollman /* 185098271db4SGarrett Wollman * XXX - need more locking here to protect against 185198271db4SGarrett Wollman * connect/disconnect races for SMP. 185298271db4SGarrett Wollman */ 1853fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 18548f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr, 185598271db4SGarrett Wollman unp->unp_addr->sun_len); 18560e229f34SGleb Smirnoff else 18570e229f34SGleb Smirnoff bzero(&xu->xu_addr, sizeof(xu->xu_addr)); 1858fc3fcacfSRobert Watson if (unp->unp_conn != NULL && 1859fc3fcacfSRobert Watson unp->unp_conn->unp_addr != NULL) 186098271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 18618f364875SJulian Elischer &xu->xu_caddr, 186298271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 18630e229f34SGleb Smirnoff else 18640e229f34SGleb Smirnoff bzero(&xu->xu_caddr, sizeof(xu->xu_caddr)); 18650e229f34SGleb Smirnoff xu->unp_vnode = unp->unp_vnode; 18660e229f34SGleb Smirnoff xu->unp_conn = unp->unp_conn; 18670e229f34SGleb Smirnoff xu->xu_firstref = LIST_FIRST(&unp->unp_refs); 18680e229f34SGleb Smirnoff xu->xu_nextref = LIST_NEXT(unp, unp_reflink); 18690e229f34SGleb Smirnoff xu->unp_gencnt = unp->unp_gencnt; 18708f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket); 1871e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 18728f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu); 187375a67bf3SMatt Macy } else if (freeunp == 0) 1874e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1875e7c33e29SRobert Watson } 18768f364875SJulian Elischer free(xu, M_TEMP); 187798271db4SGarrett Wollman if (!error) { 187898271db4SGarrett Wollman /* 18791c381b19SRobert Watson * Give the user an updated idea of our state. If the 18801c381b19SRobert Watson * generation differs from what we told her before, she knows 18811c381b19SRobert Watson * that something happened while we were processing this 18821c381b19SRobert Watson * request, and it might be necessary to retry. 188398271db4SGarrett Wollman */ 18848f364875SJulian Elischer xug->xug_gen = unp_gencnt; 18858f364875SJulian Elischer xug->xug_sogen = so_gencnt; 18868f364875SJulian Elischer xug->xug_count = unp_count; 18878f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 188898271db4SGarrett Wollman } 188998271db4SGarrett Wollman free(unp_list, M_TEMP); 18908f364875SJulian Elischer free(xug, M_TEMP); 1891e5aeaa0cSDag-Erling Smørgrav return (error); 189298271db4SGarrett Wollman } 189398271db4SGarrett Wollman 18942fee06f0SMatthew D Fleming SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLTYPE_OPAQUE | CTLFLAG_RD, 18952fee06f0SMatthew D Fleming (void *)(intptr_t)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 189698271db4SGarrett Wollman "List of active local datagram sockets"); 18972fee06f0SMatthew D Fleming SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLTYPE_OPAQUE | CTLFLAG_RD, 18982fee06f0SMatthew D Fleming (void *)(intptr_t)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 189998271db4SGarrett Wollman "List of active local stream sockets"); 19002fee06f0SMatthew D Fleming SYSCTL_PROC(_net_local_seqpacket, OID_AUTO, pcblist, 19012fee06f0SMatthew D Fleming CTLTYPE_OPAQUE | CTLFLAG_RD, 19022fee06f0SMatthew D Fleming (void *)(intptr_t)SOCK_SEQPACKET, 0, unp_pcblist, "S,xunpcb", 190384d61770SRobert Watson "List of active local seqpacket sockets"); 190498271db4SGarrett Wollman 1905f708ef1bSPoul-Henning Kamp static void 1906892af6b9SRobert Watson unp_shutdown(struct unpcb *unp) 1907df8bae1dSRodney W. Grimes { 1908e7c33e29SRobert Watson struct unpcb *unp2; 1909df8bae1dSRodney W. Grimes struct socket *so; 1910df8bae1dSRodney W. Grimes 1911e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 19120d9ce3a1SRobert Watson 1913e7c33e29SRobert Watson unp2 = unp->unp_conn; 191484d61770SRobert Watson if ((unp->unp_socket->so_type == SOCK_STREAM || 191584d61770SRobert Watson (unp->unp_socket->so_type == SOCK_SEQPACKET)) && unp2 != NULL) { 1916e7c33e29SRobert Watson so = unp2->unp_socket; 1917e7c33e29SRobert Watson if (so != NULL) 1918df8bae1dSRodney W. Grimes socantrcvmore(so); 1919df8bae1dSRodney W. Grimes } 1920e7c33e29SRobert Watson } 1921df8bae1dSRodney W. Grimes 1922f708ef1bSPoul-Henning Kamp static void 1923afc055d9SEd Schouten unp_drop(struct unpcb *unp) 1924df8bae1dSRodney W. Grimes { 1925df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 1926e7c33e29SRobert Watson struct unpcb *unp2; 192775a67bf3SMatt Macy int freed; 19280d9ce3a1SRobert Watson 1929afc055d9SEd Schouten /* 1930afc055d9SEd Schouten * Regardless of whether the socket's peer dropped the connection 1931afc055d9SEd Schouten * with this socket by aborting or disconnecting, POSIX requires 1932afc055d9SEd Schouten * that ECONNRESET is returned. 1933afc055d9SEd Schouten */ 193475a67bf3SMatt Macy /* acquire a reference so that unp isn't freed from underneath us */ 193575a67bf3SMatt Macy 193675a67bf3SMatt Macy UNP_PCB_LOCK(unp); 193775a67bf3SMatt Macy if (so) 1938afc055d9SEd Schouten so->so_error = ECONNRESET; 1939e7c33e29SRobert Watson unp2 = unp->unp_conn; 1940*acf9fd05SMatt Macy if (unp2 == unp) { 1941*acf9fd05SMatt Macy unp_disconnect(unp, unp2); 1942*acf9fd05SMatt Macy } else if (unp2 != NULL) { 194375a67bf3SMatt Macy unp_pcb_hold(unp2); 194475a67bf3SMatt Macy unp_pcb_owned_lock2(unp, unp2, freed); 1945e7c33e29SRobert Watson unp_disconnect(unp, unp2); 194675a67bf3SMatt Macy if (unp_pcb_rele(unp2) == 0) 1947e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1948df8bae1dSRodney W. Grimes } 194975a67bf3SMatt Macy if (unp_pcb_rele(unp) == 0) 195075a67bf3SMatt Macy UNP_PCB_UNLOCK(unp); 195175a67bf3SMatt Macy } 1952df8bae1dSRodney W. Grimes 19532bc21ed9SDavid Malone static void 19548cb539f1SPawel Jakub Dawidek unp_freerights(struct filedescent **fdep, int fdcount) 1955df8bae1dSRodney W. Grimes { 19562bc21ed9SDavid Malone struct file *fp; 19572609222aSPawel Jakub Dawidek int i; 1958df8bae1dSRodney W. Grimes 195982e825c4SGleb Smirnoff KASSERT(fdcount > 0, ("%s: fdcount %d", __func__, fdcount)); 196082e825c4SGleb Smirnoff 19618cb539f1SPawel Jakub Dawidek for (i = 0; i < fdcount; i++) { 19628cb539f1SPawel Jakub Dawidek fp = fdep[i]->fde_file; 19638cb539f1SPawel Jakub Dawidek filecaps_free(&fdep[i]->fde_caps); 19648692c025SYoshinobu Inoue unp_discard(fp); 1965df8bae1dSRodney W. Grimes } 19668cb539f1SPawel Jakub Dawidek free(fdep[0], M_FILECAPS); 19672bc21ed9SDavid Malone } 19682bc21ed9SDavid Malone 19690b36cd25SRobert Watson static int 1970c2e3c52eSJilles Tjoelker unp_externalize(struct mbuf *control, struct mbuf **controlp, int flags) 19712bc21ed9SDavid Malone { 19722bc21ed9SDavid Malone struct thread *td = curthread; /* XXX */ 19732bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 19742bc21ed9SDavid Malone int i; 19752bc21ed9SDavid Malone int *fdp; 19762609222aSPawel Jakub Dawidek struct filedesc *fdesc = td->td_proc->p_fd; 1977ea31808cSMateusz Guzik struct filedescent **fdep; 19782bc21ed9SDavid Malone void *data; 19792bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 19802bc21ed9SDavid Malone int error, newfds; 19812bc21ed9SDavid Malone u_int newlen; 19822bc21ed9SDavid Malone 19833dab55bcSRobert Watson UNP_LINK_UNLOCK_ASSERT(); 19844c5bc1caSRobert Watson 19852bc21ed9SDavid Malone error = 0; 19862bc21ed9SDavid Malone if (controlp != NULL) /* controlp == NULL => free control messages */ 19872bc21ed9SDavid Malone *controlp = NULL; 19882bc21ed9SDavid Malone while (cm != NULL) { 19892bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 19902bc21ed9SDavid Malone error = EINVAL; 19912bc21ed9SDavid Malone break; 19922bc21ed9SDavid Malone } 19932bc21ed9SDavid Malone data = CMSG_DATA(cm); 19942bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 19952bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET 19962bc21ed9SDavid Malone && cm->cmsg_type == SCM_RIGHTS) { 19972609222aSPawel Jakub Dawidek newfds = datalen / sizeof(*fdep); 199882e825c4SGleb Smirnoff if (newfds == 0) 199982e825c4SGleb Smirnoff goto next; 20002609222aSPawel Jakub Dawidek fdep = data; 20012bc21ed9SDavid Malone 2002e2f9a08bSOlivier Houchard /* If we're not outputting the descriptors free them. */ 20032bc21ed9SDavid Malone if (error || controlp == NULL) { 20042609222aSPawel Jakub Dawidek unp_freerights(fdep, newfds); 20052bc21ed9SDavid Malone goto next; 20062bc21ed9SDavid Malone } 20072609222aSPawel Jakub Dawidek FILEDESC_XLOCK(fdesc); 200860a5ef26SRobert Watson 2009ed5b7817SJulian Elischer /* 20101c381b19SRobert Watson * Now change each pointer to an fd in the global 20111c381b19SRobert Watson * table to an integer that is the index to the local 20121c381b19SRobert Watson * fd table entry that we set up to point to the 20131c381b19SRobert Watson * global one we are transferring. 2014ed5b7817SJulian Elischer */ 20152bc21ed9SDavid Malone newlen = newfds * sizeof(int); 20162bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 20172bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 20182bc21ed9SDavid Malone if (*controlp == NULL) { 20192609222aSPawel Jakub Dawidek FILEDESC_XUNLOCK(fdesc); 20202bc21ed9SDavid Malone error = E2BIG; 20212609222aSPawel Jakub Dawidek unp_freerights(fdep, newfds); 20222bc21ed9SDavid Malone goto next; 20232bc21ed9SDavid Malone } 20242bc21ed9SDavid Malone 20252bc21ed9SDavid Malone fdp = (int *) 20262bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2027db8f33fdSMateusz Guzik if (fdallocn(td, 0, fdp, newfds) != 0) { 20283331a33aSMateusz Guzik FILEDESC_XUNLOCK(fdesc); 2029db8f33fdSMateusz Guzik error = EMSGSIZE; 2030db8f33fdSMateusz Guzik unp_freerights(fdep, newfds); 2031db8f33fdSMateusz Guzik m_freem(*controlp); 2032db8f33fdSMateusz Guzik *controlp = NULL; 2033db8f33fdSMateusz Guzik goto next; 2034db8f33fdSMateusz Guzik } 20358cb539f1SPawel Jakub Dawidek for (i = 0; i < newfds; i++, fdp++) { 2036ea31808cSMateusz Guzik _finstall(fdesc, fdep[i]->fde_file, *fdp, 2037ea31808cSMateusz Guzik (flags & MSG_CMSG_CLOEXEC) != 0 ? UF_EXCLOSE : 0, 2038ea31808cSMateusz Guzik &fdep[i]->fde_caps); 2039ea31808cSMateusz Guzik unp_externalize_fp(fdep[i]->fde_file); 2040df8bae1dSRodney W. Grimes } 20412609222aSPawel Jakub Dawidek FILEDESC_XUNLOCK(fdesc); 20428cb539f1SPawel Jakub Dawidek free(fdep[0], M_FILECAPS); 20431c381b19SRobert Watson } else { 20441c381b19SRobert Watson /* We can just copy anything else across. */ 20452bc21ed9SDavid Malone if (error || controlp == NULL) 20462bc21ed9SDavid Malone goto next; 20472bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, datalen, 20482bc21ed9SDavid Malone cm->cmsg_type, cm->cmsg_level); 20492bc21ed9SDavid Malone if (*controlp == NULL) { 20502bc21ed9SDavid Malone error = ENOBUFS; 20512bc21ed9SDavid Malone goto next; 20522bc21ed9SDavid Malone } 20532bc21ed9SDavid Malone bcopy(data, 20542bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 20552bc21ed9SDavid Malone datalen); 20562bc21ed9SDavid Malone } 20572bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 20582bc21ed9SDavid Malone 20592bc21ed9SDavid Malone next: 20602bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 20612bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 20622bc21ed9SDavid Malone cm = (struct cmsghdr *) 20632bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 20648692c025SYoshinobu Inoue } else { 20652bc21ed9SDavid Malone clen = 0; 20662bc21ed9SDavid Malone cm = NULL; 20678692c025SYoshinobu Inoue } 20688692c025SYoshinobu Inoue } 20698692c025SYoshinobu Inoue 20702bc21ed9SDavid Malone m_freem(control); 20712bc21ed9SDavid Malone return (error); 2072df8bae1dSRodney W. Grimes } 2073df8bae1dSRodney W. Grimes 20744f590175SPaul Saab static void 20754f590175SPaul Saab unp_zone_change(void *tag) 20764f590175SPaul Saab { 20774f590175SPaul Saab 20784f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets); 20794f590175SPaul Saab } 20804f590175SPaul Saab 20810b36cd25SRobert Watson static void 208298271db4SGarrett Wollman unp_init(void) 208398271db4SGarrett Wollman { 20841c381b19SRobert Watson 208521ca7b57SMarko Zec #ifdef VIMAGE 208621ca7b57SMarko Zec if (!IS_DEFAULT_VNET(curvnet)) 208721ca7b57SMarko Zec return; 208821ca7b57SMarko Zec #endif 20899e9d298aSJeff Roberson unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 209075a67bf3SMatt Macy NULL, NULL, UMA_ALIGN_CACHE, 0); 2091fc3fcacfSRobert Watson if (unp_zone == NULL) 209298271db4SGarrett Wollman panic("unp_init"); 20934f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets); 20946e0b6746SPawel Jakub Dawidek uma_zone_set_warning(unp_zone, "kern.ipc.maxsockets limit reached"); 20954f590175SPaul Saab EVENTHANDLER_REGISTER(maxsockets_change, unp_zone_change, 20964f590175SPaul Saab NULL, EVENTHANDLER_PRI_ANY); 209798271db4SGarrett Wollman LIST_INIT(&unp_dhead); 209898271db4SGarrett Wollman LIST_INIT(&unp_shead); 209984d61770SRobert Watson LIST_INIT(&unp_sphead); 21000cb64678SKonstantin Belousov SLIST_INIT(&unp_defers); 2101daee0f0bSKonstantin Belousov TIMEOUT_TASK_INIT(taskqueue_thread, &unp_gc_task, 0, unp_gc, NULL); 21020cb64678SKonstantin Belousov TASK_INIT(&unp_defer_task, 0, unp_process_defers, NULL); 21033dab55bcSRobert Watson UNP_LINK_LOCK_INIT(); 21040cb64678SKonstantin Belousov UNP_DEFERRED_LOCK_INIT(); 210598271db4SGarrett Wollman } 210698271db4SGarrett Wollman 2107f708ef1bSPoul-Henning Kamp static int 2108892af6b9SRobert Watson unp_internalize(struct mbuf **controlp, struct thread *td) 2109df8bae1dSRodney W. Grimes { 21102bc21ed9SDavid Malone struct mbuf *control = *controlp; 2111b40ce416SJulian Elischer struct proc *p = td->td_proc; 21122609222aSPawel Jakub Dawidek struct filedesc *fdesc = p->p_fd; 2113ab15d803SSergey Kandaurov struct bintime *bt; 21142bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 21152bc21ed9SDavid Malone struct cmsgcred *cmcred; 21168cb539f1SPawel Jakub Dawidek struct filedescent *fde, **fdep, *fdev; 21172bc21ed9SDavid Malone struct file *fp; 21182bc21ed9SDavid Malone struct timeval *tv; 2119339efd75SMaxim Sobolev struct timespec *ts; 21206a1cf96bSMateusz Guzik int i, *fdp; 21212bc21ed9SDavid Malone void *data; 21222bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 21232bc21ed9SDavid Malone int error, oldfds; 21248692c025SYoshinobu Inoue u_int newlen; 2125df8bae1dSRodney W. Grimes 21263dab55bcSRobert Watson UNP_LINK_UNLOCK_ASSERT(); 21274c5bc1caSRobert Watson 21282bc21ed9SDavid Malone error = 0; 21292bc21ed9SDavid Malone *controlp = NULL; 21302bc21ed9SDavid Malone while (cm != NULL) { 21312bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 2132de966666SMateusz Guzik || cm->cmsg_len > clen || cm->cmsg_len < sizeof(*cm)) { 21332bc21ed9SDavid Malone error = EINVAL; 21342bc21ed9SDavid Malone goto out; 21352bc21ed9SDavid Malone } 21362bc21ed9SDavid Malone data = CMSG_DATA(cm); 21372bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 21382bc21ed9SDavid Malone 21392bc21ed9SDavid Malone switch (cm->cmsg_type) { 21400b788fa1SBill Paul /* 21410b788fa1SBill Paul * Fill in credential information. 21420b788fa1SBill Paul */ 21432bc21ed9SDavid Malone case SCM_CREDS: 21442bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 21452bc21ed9SDavid Malone SCM_CREDS, SOL_SOCKET); 21462bc21ed9SDavid Malone if (*controlp == NULL) { 21472bc21ed9SDavid Malone error = ENOBUFS; 21482bc21ed9SDavid Malone goto out; 21492bc21ed9SDavid Malone } 21502bc21ed9SDavid Malone cmcred = (struct cmsgcred *) 21512bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 21520b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 2153a854ed98SJohn Baldwin cmcred->cmcred_uid = td->td_ucred->cr_ruid; 2154a854ed98SJohn Baldwin cmcred->cmcred_gid = td->td_ucred->cr_rgid; 2155a854ed98SJohn Baldwin cmcred->cmcred_euid = td->td_ucred->cr_uid; 2156a854ed98SJohn Baldwin cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 21570b788fa1SBill Paul CMGROUP_MAX); 21580b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 21592bc21ed9SDavid Malone cmcred->cmcred_groups[i] = 2160a854ed98SJohn Baldwin td->td_ucred->cr_groups[i]; 21612bc21ed9SDavid Malone break; 21620b788fa1SBill Paul 21632bc21ed9SDavid Malone case SCM_RIGHTS: 21642bc21ed9SDavid Malone oldfds = datalen / sizeof (int); 216582e825c4SGleb Smirnoff if (oldfds == 0) 216682e825c4SGleb Smirnoff break; 2167ed5b7817SJulian Elischer /* 21681c381b19SRobert Watson * Check that all the FDs passed in refer to legal 21691c381b19SRobert Watson * files. If not, reject the entire operation. 2170ed5b7817SJulian Elischer */ 21712bc21ed9SDavid Malone fdp = data; 21722609222aSPawel Jakub Dawidek FILEDESC_SLOCK(fdesc); 21736a1cf96bSMateusz Guzik for (i = 0; i < oldfds; i++, fdp++) { 21746a1cf96bSMateusz Guzik fp = fget_locked(fdesc, *fdp); 21756a1cf96bSMateusz Guzik if (fp == NULL) { 21762609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdesc); 21772bc21ed9SDavid Malone error = EBADF; 21782bc21ed9SDavid Malone goto out; 21792bc21ed9SDavid Malone } 2180e7d6662fSAlfred Perlstein if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 21812609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdesc); 2182e7d6662fSAlfred Perlstein error = EOPNOTSUPP; 2183e7d6662fSAlfred Perlstein goto out; 2184e7d6662fSAlfred Perlstein } 2185e7d6662fSAlfred Perlstein 2186df8bae1dSRodney W. Grimes } 21875e3f7694SRobert Watson 2188ed5b7817SJulian Elischer /* 21890b36cd25SRobert Watson * Now replace the integer FDs with pointers to the 21902609222aSPawel Jakub Dawidek * file structure and capability rights. 2191ed5b7817SJulian Elischer */ 21928cb539f1SPawel Jakub Dawidek newlen = oldfds * sizeof(fdep[0]); 21932bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 21942bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 21952bc21ed9SDavid Malone if (*controlp == NULL) { 21962609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdesc); 21972bc21ed9SDavid Malone error = E2BIG; 21982bc21ed9SDavid Malone goto out; 21998692c025SYoshinobu Inoue } 22002bc21ed9SDavid Malone fdp = data; 22018cb539f1SPawel Jakub Dawidek fdep = (struct filedescent **) 22022bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 22038cb539f1SPawel Jakub Dawidek fdev = malloc(sizeof(*fdev) * oldfds, M_FILECAPS, 22048cb539f1SPawel Jakub Dawidek M_WAITOK); 22058cb539f1SPawel Jakub Dawidek for (i = 0; i < oldfds; i++, fdev++, fdp++) { 22062609222aSPawel Jakub Dawidek fde = &fdesc->fd_ofiles[*fdp]; 22078cb539f1SPawel Jakub Dawidek fdep[i] = fdev; 22088cb539f1SPawel Jakub Dawidek fdep[i]->fde_file = fde->fde_file; 22098cb539f1SPawel Jakub Dawidek filecaps_copy(&fde->fde_caps, 2210d7832811SMateusz Guzik &fdep[i]->fde_caps, true); 22118cb539f1SPawel Jakub Dawidek unp_internalize_fp(fdep[i]->fde_file); 2212df8bae1dSRodney W. Grimes } 22132609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdesc); 22142bc21ed9SDavid Malone break; 22152bc21ed9SDavid Malone 22162bc21ed9SDavid Malone case SCM_TIMESTAMP: 22172bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*tv), 22182bc21ed9SDavid Malone SCM_TIMESTAMP, SOL_SOCKET); 22192bc21ed9SDavid Malone if (*controlp == NULL) { 22202bc21ed9SDavid Malone error = ENOBUFS; 22212bc21ed9SDavid Malone goto out; 22228692c025SYoshinobu Inoue } 22232bc21ed9SDavid Malone tv = (struct timeval *) 22242bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 22252bc21ed9SDavid Malone microtime(tv); 22262bc21ed9SDavid Malone break; 22272bc21ed9SDavid Malone 2228ab15d803SSergey Kandaurov case SCM_BINTIME: 2229ab15d803SSergey Kandaurov *controlp = sbcreatecontrol(NULL, sizeof(*bt), 2230ab15d803SSergey Kandaurov SCM_BINTIME, SOL_SOCKET); 2231ab15d803SSergey Kandaurov if (*controlp == NULL) { 2232ab15d803SSergey Kandaurov error = ENOBUFS; 2233ab15d803SSergey Kandaurov goto out; 2234ab15d803SSergey Kandaurov } 2235ab15d803SSergey Kandaurov bt = (struct bintime *) 2236ab15d803SSergey Kandaurov CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2237ab15d803SSergey Kandaurov bintime(bt); 2238ab15d803SSergey Kandaurov break; 2239ab15d803SSergey Kandaurov 2240339efd75SMaxim Sobolev case SCM_REALTIME: 2241339efd75SMaxim Sobolev *controlp = sbcreatecontrol(NULL, sizeof(*ts), 2242339efd75SMaxim Sobolev SCM_REALTIME, SOL_SOCKET); 2243339efd75SMaxim Sobolev if (*controlp == NULL) { 2244339efd75SMaxim Sobolev error = ENOBUFS; 2245339efd75SMaxim Sobolev goto out; 2246339efd75SMaxim Sobolev } 2247339efd75SMaxim Sobolev ts = (struct timespec *) 2248339efd75SMaxim Sobolev CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2249339efd75SMaxim Sobolev nanotime(ts); 2250339efd75SMaxim Sobolev break; 2251339efd75SMaxim Sobolev 2252339efd75SMaxim Sobolev case SCM_MONOTONIC: 2253339efd75SMaxim Sobolev *controlp = sbcreatecontrol(NULL, sizeof(*ts), 2254339efd75SMaxim Sobolev SCM_MONOTONIC, SOL_SOCKET); 2255339efd75SMaxim Sobolev if (*controlp == NULL) { 2256339efd75SMaxim Sobolev error = ENOBUFS; 2257339efd75SMaxim Sobolev goto out; 2258339efd75SMaxim Sobolev } 2259339efd75SMaxim Sobolev ts = (struct timespec *) 2260339efd75SMaxim Sobolev CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 2261339efd75SMaxim Sobolev nanouptime(ts); 2262339efd75SMaxim Sobolev break; 2263339efd75SMaxim Sobolev 22642bc21ed9SDavid Malone default: 22652bc21ed9SDavid Malone error = EINVAL; 22662bc21ed9SDavid Malone goto out; 22672bc21ed9SDavid Malone } 22682bc21ed9SDavid Malone 22692bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 22702bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 22712bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 22722bc21ed9SDavid Malone cm = (struct cmsghdr *) 22732bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 22742bc21ed9SDavid Malone } else { 22752bc21ed9SDavid Malone clen = 0; 22762bc21ed9SDavid Malone cm = NULL; 22772bc21ed9SDavid Malone } 22782bc21ed9SDavid Malone } 22792bc21ed9SDavid Malone 22802bc21ed9SDavid Malone out: 22812bc21ed9SDavid Malone m_freem(control); 22822bc21ed9SDavid Malone return (error); 2283df8bae1dSRodney W. Grimes } 2284df8bae1dSRodney W. Grimes 22855b950deaSRobert Watson static struct mbuf * 22866a2989fdSMatthew N. Dodd unp_addsockcred(struct thread *td, struct mbuf *control) 22876a2989fdSMatthew N. Dodd { 228870df31f4SMaxim Konovalov struct mbuf *m, *n, *n_prev; 22896a2989fdSMatthew N. Dodd struct sockcred *sc; 229070df31f4SMaxim Konovalov const struct cmsghdr *cm; 22916a2989fdSMatthew N. Dodd int ngroups; 22926a2989fdSMatthew N. Dodd int i; 22936a2989fdSMatthew N. Dodd 22946a2989fdSMatthew N. Dodd ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX); 22956a2989fdSMatthew N. Dodd m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET); 22966a2989fdSMatthew N. Dodd if (m == NULL) 22976a2989fdSMatthew N. Dodd return (control); 22986a2989fdSMatthew N. Dodd 22996a2989fdSMatthew N. Dodd sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *)); 23006a2989fdSMatthew N. Dodd sc->sc_uid = td->td_ucred->cr_ruid; 23016a2989fdSMatthew N. Dodd sc->sc_euid = td->td_ucred->cr_uid; 23026a2989fdSMatthew N. Dodd sc->sc_gid = td->td_ucred->cr_rgid; 23036a2989fdSMatthew N. Dodd sc->sc_egid = td->td_ucred->cr_gid; 23046a2989fdSMatthew N. Dodd sc->sc_ngroups = ngroups; 23056a2989fdSMatthew N. Dodd for (i = 0; i < sc->sc_ngroups; i++) 23066a2989fdSMatthew N. Dodd sc->sc_groups[i] = td->td_ucred->cr_groups[i]; 23076a2989fdSMatthew N. Dodd 23086a2989fdSMatthew N. Dodd /* 23091c381b19SRobert Watson * Unlink SCM_CREDS control messages (struct cmsgcred), since just 23101c381b19SRobert Watson * created SCM_CREDS control message (struct sockcred) has another 23111c381b19SRobert Watson * format. 23126a2989fdSMatthew N. Dodd */ 231370df31f4SMaxim Konovalov if (control != NULL) 231470df31f4SMaxim Konovalov for (n = control, n_prev = NULL; n != NULL;) { 231570df31f4SMaxim Konovalov cm = mtod(n, struct cmsghdr *); 231670df31f4SMaxim Konovalov if (cm->cmsg_level == SOL_SOCKET && 231770df31f4SMaxim Konovalov cm->cmsg_type == SCM_CREDS) { 231870df31f4SMaxim Konovalov if (n_prev == NULL) 231970df31f4SMaxim Konovalov control = n->m_next; 232070df31f4SMaxim Konovalov else 232170df31f4SMaxim Konovalov n_prev->m_next = n->m_next; 232270df31f4SMaxim Konovalov n = m_free(n); 232370df31f4SMaxim Konovalov } else { 232470df31f4SMaxim Konovalov n_prev = n; 232570df31f4SMaxim Konovalov n = n->m_next; 232670df31f4SMaxim Konovalov } 232770df31f4SMaxim Konovalov } 23286a2989fdSMatthew N. Dodd 232970df31f4SMaxim Konovalov /* Prepend it to the head. */ 233070df31f4SMaxim Konovalov m->m_next = control; 233170df31f4SMaxim Konovalov return (m); 23326a2989fdSMatthew N. Dodd } 23336a2989fdSMatthew N. Dodd 2334397c19d1SJeff Roberson static struct unpcb * 2335397c19d1SJeff Roberson fptounp(struct file *fp) 2336397c19d1SJeff Roberson { 2337397c19d1SJeff Roberson struct socket *so; 2338397c19d1SJeff Roberson 2339397c19d1SJeff Roberson if (fp->f_type != DTYPE_SOCKET) 2340397c19d1SJeff Roberson return (NULL); 2341397c19d1SJeff Roberson if ((so = fp->f_data) == NULL) 2342397c19d1SJeff Roberson return (NULL); 2343397c19d1SJeff Roberson if (so->so_proto->pr_domain != &localdomain) 2344397c19d1SJeff Roberson return (NULL); 2345397c19d1SJeff Roberson return sotounpcb(so); 2346397c19d1SJeff Roberson } 2347397c19d1SJeff Roberson 2348397c19d1SJeff Roberson static void 2349397c19d1SJeff Roberson unp_discard(struct file *fp) 2350397c19d1SJeff Roberson { 23510cb64678SKonstantin Belousov struct unp_defer *dr; 2352397c19d1SJeff Roberson 23530cb64678SKonstantin Belousov if (unp_externalize_fp(fp)) { 23540cb64678SKonstantin Belousov dr = malloc(sizeof(*dr), M_TEMP, M_WAITOK); 23550cb64678SKonstantin Belousov dr->ud_fp = fp; 23560cb64678SKonstantin Belousov UNP_DEFERRED_LOCK(); 23570cb64678SKonstantin Belousov SLIST_INSERT_HEAD(&unp_defers, dr, ud_link); 23580cb64678SKonstantin Belousov UNP_DEFERRED_UNLOCK(); 23590cb64678SKonstantin Belousov atomic_add_int(&unp_defers_count, 1); 23600cb64678SKonstantin Belousov taskqueue_enqueue(taskqueue_thread, &unp_defer_task); 23610cb64678SKonstantin Belousov } else 2362397c19d1SJeff Roberson (void) closef(fp, (struct thread *)NULL); 2363397c19d1SJeff Roberson } 2364397c19d1SJeff Roberson 2365397c19d1SJeff Roberson static void 23660cb64678SKonstantin Belousov unp_process_defers(void *arg __unused, int pending) 23670cb64678SKonstantin Belousov { 23680cb64678SKonstantin Belousov struct unp_defer *dr; 23690cb64678SKonstantin Belousov SLIST_HEAD(, unp_defer) drl; 23700cb64678SKonstantin Belousov int count; 23710cb64678SKonstantin Belousov 23720cb64678SKonstantin Belousov SLIST_INIT(&drl); 23730cb64678SKonstantin Belousov for (;;) { 23740cb64678SKonstantin Belousov UNP_DEFERRED_LOCK(); 23750cb64678SKonstantin Belousov if (SLIST_FIRST(&unp_defers) == NULL) { 23760cb64678SKonstantin Belousov UNP_DEFERRED_UNLOCK(); 23770cb64678SKonstantin Belousov break; 23780cb64678SKonstantin Belousov } 23790cb64678SKonstantin Belousov SLIST_SWAP(&unp_defers, &drl, unp_defer); 23800cb64678SKonstantin Belousov UNP_DEFERRED_UNLOCK(); 23810cb64678SKonstantin Belousov count = 0; 23820cb64678SKonstantin Belousov while ((dr = SLIST_FIRST(&drl)) != NULL) { 23830cb64678SKonstantin Belousov SLIST_REMOVE_HEAD(&drl, ud_link); 23840cb64678SKonstantin Belousov closef(dr->ud_fp, NULL); 23850cb64678SKonstantin Belousov free(dr, M_TEMP); 23860cb64678SKonstantin Belousov count++; 23870cb64678SKonstantin Belousov } 23880cb64678SKonstantin Belousov atomic_add_int(&unp_defers_count, -count); 23890cb64678SKonstantin Belousov } 23900cb64678SKonstantin Belousov } 23910cb64678SKonstantin Belousov 23920cb64678SKonstantin Belousov static void 2393397c19d1SJeff Roberson unp_internalize_fp(struct file *fp) 2394397c19d1SJeff Roberson { 2395397c19d1SJeff Roberson struct unpcb *unp; 2396397c19d1SJeff Roberson 23973dab55bcSRobert Watson UNP_LINK_WLOCK(); 2398397c19d1SJeff Roberson if ((unp = fptounp(fp)) != NULL) { 2399397c19d1SJeff Roberson unp->unp_file = fp; 2400397c19d1SJeff Roberson unp->unp_msgcount++; 2401397c19d1SJeff Roberson } 240241e0f66dSJeff Roberson fhold(fp); 2403397c19d1SJeff Roberson unp_rights++; 24043dab55bcSRobert Watson UNP_LINK_WUNLOCK(); 2405397c19d1SJeff Roberson } 2406397c19d1SJeff Roberson 24070cb64678SKonstantin Belousov static int 2408397c19d1SJeff Roberson unp_externalize_fp(struct file *fp) 2409397c19d1SJeff Roberson { 2410397c19d1SJeff Roberson struct unpcb *unp; 24110cb64678SKonstantin Belousov int ret; 2412397c19d1SJeff Roberson 24133dab55bcSRobert Watson UNP_LINK_WLOCK(); 24140cb64678SKonstantin Belousov if ((unp = fptounp(fp)) != NULL) { 2415397c19d1SJeff Roberson unp->unp_msgcount--; 24160cb64678SKonstantin Belousov ret = 1; 24170cb64678SKonstantin Belousov } else 24180cb64678SKonstantin Belousov ret = 0; 2419397c19d1SJeff Roberson unp_rights--; 24203dab55bcSRobert Watson UNP_LINK_WUNLOCK(); 24210cb64678SKonstantin Belousov return (ret); 2422397c19d1SJeff Roberson } 2423397c19d1SJeff Roberson 2424161a0c7cSRobert Watson /* 2425a0ec558aSRobert Watson * unp_defer indicates whether additional work has been defered for a future 2426a0ec558aSRobert Watson * pass through unp_gc(). It is thread local and does not require explicit 2427a0ec558aSRobert Watson * synchronization. 2428161a0c7cSRobert Watson */ 2429397c19d1SJeff Roberson static int unp_marked; 2430397c19d1SJeff Roberson static int unp_unreachable; 2431a0ec558aSRobert Watson 2432397c19d1SJeff Roberson static void 2433be26ba7cSPawel Jakub Dawidek unp_accessable(struct filedescent **fdep, int fdcount) 2434397c19d1SJeff Roberson { 2435397c19d1SJeff Roberson struct unpcb *unp; 2436be26ba7cSPawel Jakub Dawidek struct file *fp; 2437be26ba7cSPawel Jakub Dawidek int i; 2438397c19d1SJeff Roberson 2439be26ba7cSPawel Jakub Dawidek for (i = 0; i < fdcount; i++) { 2440be26ba7cSPawel Jakub Dawidek fp = fdep[i]->fde_file; 24416f552cb0SJeff Roberson if ((unp = fptounp(fp)) == NULL) 2442be26ba7cSPawel Jakub Dawidek continue; 2443397c19d1SJeff Roberson if (unp->unp_gcflag & UNPGC_REF) 2444be26ba7cSPawel Jakub Dawidek continue; 2445397c19d1SJeff Roberson unp->unp_gcflag &= ~UNPGC_DEAD; 2446397c19d1SJeff Roberson unp->unp_gcflag |= UNPGC_REF; 2447397c19d1SJeff Roberson unp_marked++; 2448397c19d1SJeff Roberson } 2449be26ba7cSPawel Jakub Dawidek } 2450397c19d1SJeff Roberson 2451397c19d1SJeff Roberson static void 2452397c19d1SJeff Roberson unp_gc_process(struct unpcb *unp) 2453397c19d1SJeff Roberson { 2454779f106aSGleb Smirnoff struct socket *so, *soa; 2455397c19d1SJeff Roberson struct file *fp; 2456397c19d1SJeff Roberson 2457397c19d1SJeff Roberson /* Already processed. */ 2458397c19d1SJeff Roberson if (unp->unp_gcflag & UNPGC_SCANNED) 2459397c19d1SJeff Roberson return; 2460397c19d1SJeff Roberson fp = unp->unp_file; 246160a5ef26SRobert Watson 2462397c19d1SJeff Roberson /* 2463397c19d1SJeff Roberson * Check for a socket potentially in a cycle. It must be in a 2464397c19d1SJeff Roberson * queue as indicated by msgcount, and this must equal the file 2465397c19d1SJeff Roberson * reference count. Note that when msgcount is 0 the file is NULL. 2466397c19d1SJeff Roberson */ 246741e0f66dSJeff Roberson if ((unp->unp_gcflag & UNPGC_REF) == 0 && fp && 246841e0f66dSJeff Roberson unp->unp_msgcount != 0 && fp->f_count == unp->unp_msgcount) { 2469397c19d1SJeff Roberson unp->unp_gcflag |= UNPGC_DEAD; 2470397c19d1SJeff Roberson unp_unreachable++; 2471397c19d1SJeff Roberson return; 2472397c19d1SJeff Roberson } 247360a5ef26SRobert Watson 2474397c19d1SJeff Roberson so = unp->unp_socket; 2475779f106aSGleb Smirnoff SOCK_LOCK(so); 2476779f106aSGleb Smirnoff if (SOLISTENING(so)) { 2477397c19d1SJeff Roberson /* 2478397c19d1SJeff Roberson * Mark all sockets in our accept queue. 2479397c19d1SJeff Roberson */ 2480779f106aSGleb Smirnoff TAILQ_FOREACH(soa, &so->sol_comp, so_list) { 2481779f106aSGleb Smirnoff if (sotounpcb(soa)->unp_gcflag & UNPGC_IGNORE_RIGHTS) 24820c40f353SConrad Meyer continue; 2483397c19d1SJeff Roberson SOCKBUF_LOCK(&soa->so_rcv); 2484397c19d1SJeff Roberson unp_scan(soa->so_rcv.sb_mb, unp_accessable); 2485397c19d1SJeff Roberson SOCKBUF_UNLOCK(&soa->so_rcv); 2486397c19d1SJeff Roberson } 2487779f106aSGleb Smirnoff } else { 2488779f106aSGleb Smirnoff /* 2489779f106aSGleb Smirnoff * Mark all sockets we reference with RIGHTS. 2490779f106aSGleb Smirnoff */ 2491779f106aSGleb Smirnoff if ((unp->unp_gcflag & UNPGC_IGNORE_RIGHTS) == 0) { 2492779f106aSGleb Smirnoff SOCKBUF_LOCK(&so->so_rcv); 2493779f106aSGleb Smirnoff unp_scan(so->so_rcv.sb_mb, unp_accessable); 2494779f106aSGleb Smirnoff SOCKBUF_UNLOCK(&so->so_rcv); 2495779f106aSGleb Smirnoff } 2496779f106aSGleb Smirnoff } 2497779f106aSGleb Smirnoff SOCK_UNLOCK(so); 2498397c19d1SJeff Roberson unp->unp_gcflag |= UNPGC_SCANNED; 2499397c19d1SJeff Roberson } 2500a0ec558aSRobert Watson 2501a0ec558aSRobert Watson static int unp_recycled; 2502be6b1304STom Rhodes SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0, 2503be6b1304STom Rhodes "Number of unreachable sockets claimed by the garbage collector."); 2504df8bae1dSRodney W. Grimes 2505397c19d1SJeff Roberson static int unp_taskcount; 2506be6b1304STom Rhodes SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0, 2507be6b1304STom Rhodes "Number of times the garbage collector has run."); 2508397c19d1SJeff Roberson 2509f708ef1bSPoul-Henning Kamp static void 2510a0ec558aSRobert Watson unp_gc(__unused void *arg, int pending) 2511df8bae1dSRodney W. Grimes { 251284d61770SRobert Watson struct unp_head *heads[] = { &unp_dhead, &unp_shead, &unp_sphead, 251384d61770SRobert Watson NULL }; 2514397c19d1SJeff Roberson struct unp_head **head; 2515f7780c61SKonstantin Belousov struct file *f, **unref; 2516397c19d1SJeff Roberson struct unpcb *unp; 2517f7780c61SKonstantin Belousov int i, total; 2518df8bae1dSRodney W. Grimes 2519a0ec558aSRobert Watson unp_taskcount++; 2520779f106aSGleb Smirnoff UNP_LINK_RLOCK(); 2521ed5b7817SJulian Elischer /* 25220c40f353SConrad Meyer * First clear all gc flags from previous runs, apart from 25230c40f353SConrad Meyer * UNPGC_IGNORE_RIGHTS. 2524ed5b7817SJulian Elischer */ 2525397c19d1SJeff Roberson for (head = heads; *head != NULL; head++) 2526397c19d1SJeff Roberson LIST_FOREACH(unp, *head, unp_link) 25270c40f353SConrad Meyer unp->unp_gcflag = 25280c40f353SConrad Meyer (unp->unp_gcflag & UNPGC_IGNORE_RIGHTS); 252960a5ef26SRobert Watson 2530397c19d1SJeff Roberson /* 2531397c19d1SJeff Roberson * Scan marking all reachable sockets with UNPGC_REF. Once a socket 2532397c19d1SJeff Roberson * is reachable all of the sockets it references are reachable. 2533397c19d1SJeff Roberson * Stop the scan once we do a complete loop without discovering 2534397c19d1SJeff Roberson * a new reachable socket. 2535397c19d1SJeff Roberson */ 2536df8bae1dSRodney W. Grimes do { 2537397c19d1SJeff Roberson unp_unreachable = 0; 2538397c19d1SJeff Roberson unp_marked = 0; 2539397c19d1SJeff Roberson for (head = heads; *head != NULL; head++) 2540397c19d1SJeff Roberson LIST_FOREACH(unp, *head, unp_link) 2541397c19d1SJeff Roberson unp_gc_process(unp); 2542397c19d1SJeff Roberson } while (unp_marked); 2543779f106aSGleb Smirnoff UNP_LINK_RUNLOCK(); 2544397c19d1SJeff Roberson if (unp_unreachable == 0) 2545397c19d1SJeff Roberson return; 254660a5ef26SRobert Watson 2547ed5b7817SJulian Elischer /* 2548397c19d1SJeff Roberson * Allocate space for a local list of dead unpcbs. 2549ed5b7817SJulian Elischer */ 2550397c19d1SJeff Roberson unref = malloc(unp_unreachable * sizeof(struct file *), 2551397c19d1SJeff Roberson M_TEMP, M_WAITOK); 255260a5ef26SRobert Watson 2553ed5b7817SJulian Elischer /* 2554397c19d1SJeff Roberson * Iterate looking for sockets which have been specifically marked 2555397c19d1SJeff Roberson * as as unreachable and store them locally. 2556ed5b7817SJulian Elischer */ 2557f7780c61SKonstantin Belousov UNP_LINK_RLOCK(); 2558f7780c61SKonstantin Belousov for (total = 0, head = heads; *head != NULL; head++) 2559397c19d1SJeff Roberson LIST_FOREACH(unp, *head, unp_link) 2560f7780c61SKonstantin Belousov if ((unp->unp_gcflag & UNPGC_DEAD) != 0) { 2561f7780c61SKonstantin Belousov f = unp->unp_file; 2562f7780c61SKonstantin Belousov if (unp->unp_msgcount == 0 || f == NULL || 2563f7780c61SKonstantin Belousov f->f_count != unp->unp_msgcount) 2564f7780c61SKonstantin Belousov continue; 2565f7780c61SKonstantin Belousov unref[total++] = f; 2566f7780c61SKonstantin Belousov fhold(f); 2567f7780c61SKonstantin Belousov KASSERT(total <= unp_unreachable, 2568397c19d1SJeff Roberson ("unp_gc: incorrect unreachable count.")); 2569397c19d1SJeff Roberson } 2570f7780c61SKonstantin Belousov UNP_LINK_RUNLOCK(); 257160a5ef26SRobert Watson 2572ed5b7817SJulian Elischer /* 2573397c19d1SJeff Roberson * Now flush all sockets, free'ing rights. This will free the 2574397c19d1SJeff Roberson * struct files associated with these sockets but leave each socket 2575397c19d1SJeff Roberson * with one remaining ref. 2576ed5b7817SJulian Elischer */ 25771fb51a12SBjoern A. Zeeb for (i = 0; i < total; i++) { 25781fb51a12SBjoern A. Zeeb struct socket *so; 25791fb51a12SBjoern A. Zeeb 25801fb51a12SBjoern A. Zeeb so = unref[i]->f_data; 25811fb51a12SBjoern A. Zeeb CURVNET_SET(so->so_vnet); 25821fb51a12SBjoern A. Zeeb sorflush(so); 25831fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 25841fb51a12SBjoern A. Zeeb } 258560a5ef26SRobert Watson 2586ed5b7817SJulian Elischer /* 2587397c19d1SJeff Roberson * And finally release the sockets so they can be reclaimed. 2588ed5b7817SJulian Elischer */ 2589f7780c61SKonstantin Belousov for (i = 0; i < total; i++) 2590397c19d1SJeff Roberson fdrop(unref[i], NULL); 2591f7780c61SKonstantin Belousov unp_recycled += total; 2592397c19d1SJeff Roberson free(unref, M_TEMP); 2593df8bae1dSRodney W. Grimes } 2594df8bae1dSRodney W. Grimes 25950b36cd25SRobert Watson static void 259699ab95dbSMark Johnston unp_dispose_mbuf(struct mbuf *m) 2597df8bae1dSRodney W. Grimes { 2598996c772fSJohn Dyson 2599df8bae1dSRodney W. Grimes if (m) 2600be26ba7cSPawel Jakub Dawidek unp_scan(m, unp_freerights); 2601df8bae1dSRodney W. Grimes } 2602df8bae1dSRodney W. Grimes 26030c40f353SConrad Meyer /* 26040c40f353SConrad Meyer * Synchronize against unp_gc, which can trip over data as we are freeing it. 26050c40f353SConrad Meyer */ 26060c40f353SConrad Meyer static void 260799ab95dbSMark Johnston unp_dispose(struct socket *so) 26080c40f353SConrad Meyer { 26090c40f353SConrad Meyer struct unpcb *unp; 26100c40f353SConrad Meyer 26110c40f353SConrad Meyer unp = sotounpcb(so); 2612779f106aSGleb Smirnoff UNP_LINK_WLOCK(); 26130c40f353SConrad Meyer unp->unp_gcflag |= UNPGC_IGNORE_RIGHTS; 2614779f106aSGleb Smirnoff UNP_LINK_WUNLOCK(); 2615779f106aSGleb Smirnoff if (!SOLISTENING(so)) 261699ab95dbSMark Johnston unp_dispose_mbuf(so->so_rcv.sb_mb); 26170c40f353SConrad Meyer } 26180c40f353SConrad Meyer 2619f708ef1bSPoul-Henning Kamp static void 2620be26ba7cSPawel Jakub Dawidek unp_scan(struct mbuf *m0, void (*op)(struct filedescent **, int)) 2621df8bae1dSRodney W. Grimes { 26222bc21ed9SDavid Malone struct mbuf *m; 26232bc21ed9SDavid Malone struct cmsghdr *cm; 26242bc21ed9SDavid Malone void *data; 26252bc21ed9SDavid Malone socklen_t clen, datalen; 2626df8bae1dSRodney W. Grimes 2627fc3fcacfSRobert Watson while (m0 != NULL) { 26282bc21ed9SDavid Malone for (m = m0; m; m = m->m_next) { 262912396bdcSDavid Malone if (m->m_type != MT_CONTROL) 2630df8bae1dSRodney W. Grimes continue; 26312bc21ed9SDavid Malone 26322bc21ed9SDavid Malone cm = mtod(m, struct cmsghdr *); 26332bc21ed9SDavid Malone clen = m->m_len; 26342bc21ed9SDavid Malone 26352bc21ed9SDavid Malone while (cm != NULL) { 26362bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) 26372bc21ed9SDavid Malone break; 26382bc21ed9SDavid Malone 26392bc21ed9SDavid Malone data = CMSG_DATA(cm); 26402bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len 26412bc21ed9SDavid Malone - (caddr_t)data; 26422bc21ed9SDavid Malone 26432bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET && 26442bc21ed9SDavid Malone cm->cmsg_type == SCM_RIGHTS) { 2645be26ba7cSPawel Jakub Dawidek (*op)(data, datalen / 2646be26ba7cSPawel Jakub Dawidek sizeof(struct filedescent *)); 26472bc21ed9SDavid Malone } 26482bc21ed9SDavid Malone 26492bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 26502bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 26512bc21ed9SDavid Malone cm = (struct cmsghdr *) 26522bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 26532bc21ed9SDavid Malone } else { 26542bc21ed9SDavid Malone clen = 0; 26552bc21ed9SDavid Malone cm = NULL; 26562bc21ed9SDavid Malone } 26572bc21ed9SDavid Malone } 2658df8bae1dSRodney W. Grimes } 2659c29a3321SKevin Lo m0 = m0->m_nextpkt; 2660df8bae1dSRodney W. Grimes } 2661df8bae1dSRodney W. Grimes } 2662df8bae1dSRodney W. Grimes 2663662c901cSMikolaj Golub /* 2664662c901cSMikolaj Golub * A helper function called by VFS before socket-type vnode reclamation. 2665662c901cSMikolaj Golub * For an active vnode it clears unp_vnode pointer and decrements unp_vnode 2666662c901cSMikolaj Golub * use count. 2667662c901cSMikolaj Golub */ 2668662c901cSMikolaj Golub void 2669662c901cSMikolaj Golub vfs_unp_reclaim(struct vnode *vp) 2670662c901cSMikolaj Golub { 2671662c901cSMikolaj Golub struct unpcb *unp; 2672662c901cSMikolaj Golub int active; 267375a67bf3SMatt Macy struct mtx *vplock; 2674662c901cSMikolaj Golub 2675662c901cSMikolaj Golub ASSERT_VOP_ELOCKED(vp, "vfs_unp_reclaim"); 2676662c901cSMikolaj Golub KASSERT(vp->v_type == VSOCK, 2677662c901cSMikolaj Golub ("vfs_unp_reclaim: vp->v_type != VSOCK")); 2678662c901cSMikolaj Golub 2679662c901cSMikolaj Golub active = 0; 268075a67bf3SMatt Macy vplock = mtx_pool_find(mtxpool_sleep, vp); 268175a67bf3SMatt Macy mtx_lock(vplock); 26820c3c207fSGleb Smirnoff VOP_UNP_CONNECT(vp, &unp); 2683662c901cSMikolaj Golub if (unp == NULL) 2684662c901cSMikolaj Golub goto done; 2685662c901cSMikolaj Golub UNP_PCB_LOCK(unp); 2686c7e41c8bSMikolaj Golub if (unp->unp_vnode == vp) { 2687c7e41c8bSMikolaj Golub VOP_UNP_DETACH(vp); 2688662c901cSMikolaj Golub unp->unp_vnode = NULL; 2689662c901cSMikolaj Golub active = 1; 2690662c901cSMikolaj Golub } 2691662c901cSMikolaj Golub UNP_PCB_UNLOCK(unp); 2692662c901cSMikolaj Golub done: 269375a67bf3SMatt Macy mtx_unlock(vplock); 2694662c901cSMikolaj Golub if (active) 2695662c901cSMikolaj Golub vunref(vp); 2696662c901cSMikolaj Golub } 2697662c901cSMikolaj Golub 269803c96c31SRobert Watson #ifdef DDB 269903c96c31SRobert Watson static void 270003c96c31SRobert Watson db_print_indent(int indent) 270103c96c31SRobert Watson { 270203c96c31SRobert Watson int i; 270303c96c31SRobert Watson 270403c96c31SRobert Watson for (i = 0; i < indent; i++) 270503c96c31SRobert Watson db_printf(" "); 270603c96c31SRobert Watson } 270703c96c31SRobert Watson 270803c96c31SRobert Watson static void 270903c96c31SRobert Watson db_print_unpflags(int unp_flags) 271003c96c31SRobert Watson { 271103c96c31SRobert Watson int comma; 271203c96c31SRobert Watson 271303c96c31SRobert Watson comma = 0; 271403c96c31SRobert Watson if (unp_flags & UNP_HAVEPC) { 271503c96c31SRobert Watson db_printf("%sUNP_HAVEPC", comma ? ", " : ""); 271603c96c31SRobert Watson comma = 1; 271703c96c31SRobert Watson } 271803c96c31SRobert Watson if (unp_flags & UNP_WANTCRED) { 271903c96c31SRobert Watson db_printf("%sUNP_WANTCRED", comma ? ", " : ""); 272003c96c31SRobert Watson comma = 1; 272103c96c31SRobert Watson } 272203c96c31SRobert Watson if (unp_flags & UNP_CONNWAIT) { 272303c96c31SRobert Watson db_printf("%sUNP_CONNWAIT", comma ? ", " : ""); 272403c96c31SRobert Watson comma = 1; 272503c96c31SRobert Watson } 272603c96c31SRobert Watson if (unp_flags & UNP_CONNECTING) { 272703c96c31SRobert Watson db_printf("%sUNP_CONNECTING", comma ? ", " : ""); 272803c96c31SRobert Watson comma = 1; 272903c96c31SRobert Watson } 273003c96c31SRobert Watson if (unp_flags & UNP_BINDING) { 273103c96c31SRobert Watson db_printf("%sUNP_BINDING", comma ? ", " : ""); 273203c96c31SRobert Watson comma = 1; 273303c96c31SRobert Watson } 273403c96c31SRobert Watson } 273503c96c31SRobert Watson 273603c96c31SRobert Watson static void 273703c96c31SRobert Watson db_print_xucred(int indent, struct xucred *xu) 273803c96c31SRobert Watson { 273903c96c31SRobert Watson int comma, i; 274003c96c31SRobert Watson 274103c96c31SRobert Watson db_print_indent(indent); 274203c96c31SRobert Watson db_printf("cr_version: %u cr_uid: %u cr_ngroups: %d\n", 274303c96c31SRobert Watson xu->cr_version, xu->cr_uid, xu->cr_ngroups); 274403c96c31SRobert Watson db_print_indent(indent); 274503c96c31SRobert Watson db_printf("cr_groups: "); 274603c96c31SRobert Watson comma = 0; 274703c96c31SRobert Watson for (i = 0; i < xu->cr_ngroups; i++) { 274803c96c31SRobert Watson db_printf("%s%u", comma ? ", " : "", xu->cr_groups[i]); 274903c96c31SRobert Watson comma = 1; 275003c96c31SRobert Watson } 275103c96c31SRobert Watson db_printf("\n"); 275203c96c31SRobert Watson } 275303c96c31SRobert Watson 275403c96c31SRobert Watson static void 275503c96c31SRobert Watson db_print_unprefs(int indent, struct unp_head *uh) 275603c96c31SRobert Watson { 275703c96c31SRobert Watson struct unpcb *unp; 275803c96c31SRobert Watson int counter; 275903c96c31SRobert Watson 276003c96c31SRobert Watson counter = 0; 276103c96c31SRobert Watson LIST_FOREACH(unp, uh, unp_reflink) { 276203c96c31SRobert Watson if (counter % 4 == 0) 276303c96c31SRobert Watson db_print_indent(indent); 276403c96c31SRobert Watson db_printf("%p ", unp); 276503c96c31SRobert Watson if (counter % 4 == 3) 276603c96c31SRobert Watson db_printf("\n"); 276703c96c31SRobert Watson counter++; 276803c96c31SRobert Watson } 276903c96c31SRobert Watson if (counter != 0 && counter % 4 != 0) 277003c96c31SRobert Watson db_printf("\n"); 277103c96c31SRobert Watson } 277203c96c31SRobert Watson 277303c96c31SRobert Watson DB_SHOW_COMMAND(unpcb, db_show_unpcb) 277403c96c31SRobert Watson { 277503c96c31SRobert Watson struct unpcb *unp; 277603c96c31SRobert Watson 277703c96c31SRobert Watson if (!have_addr) { 277803c96c31SRobert Watson db_printf("usage: show unpcb <addr>\n"); 277903c96c31SRobert Watson return; 278003c96c31SRobert Watson } 278103c96c31SRobert Watson unp = (struct unpcb *)addr; 278203c96c31SRobert Watson 278303c96c31SRobert Watson db_printf("unp_socket: %p unp_vnode: %p\n", unp->unp_socket, 278403c96c31SRobert Watson unp->unp_vnode); 278503c96c31SRobert Watson 2786fc8fdae0SMatthew D Fleming db_printf("unp_ino: %ju unp_conn: %p\n", (uintmax_t)unp->unp_ino, 278703c96c31SRobert Watson unp->unp_conn); 278803c96c31SRobert Watson 278903c96c31SRobert Watson db_printf("unp_refs:\n"); 279003c96c31SRobert Watson db_print_unprefs(2, &unp->unp_refs); 279103c96c31SRobert Watson 279203c96c31SRobert Watson /* XXXRW: Would be nice to print the full address, if any. */ 279303c96c31SRobert Watson db_printf("unp_addr: %p\n", unp->unp_addr); 279403c96c31SRobert Watson 2795c2090e73SAlan Somers db_printf("unp_gencnt: %llu\n", 279603c96c31SRobert Watson (unsigned long long)unp->unp_gencnt); 279703c96c31SRobert Watson 279803c96c31SRobert Watson db_printf("unp_flags: %x (", unp->unp_flags); 279903c96c31SRobert Watson db_print_unpflags(unp->unp_flags); 280003c96c31SRobert Watson db_printf(")\n"); 280103c96c31SRobert Watson 280203c96c31SRobert Watson db_printf("unp_peercred:\n"); 280303c96c31SRobert Watson db_print_xucred(2, &unp->unp_peercred); 280403c96c31SRobert Watson 280503c96c31SRobert Watson db_printf("unp_refcount: %u\n", unp->unp_refcount); 280603c96c31SRobert Watson } 280703c96c31SRobert Watson #endif 2808