19454b2d8SWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1991, 1993 3e1ac28e2SRobert Watson * The Regents of the University of California. 446a1d9bfSRobert Watson * Copyright (c) 2004-2007 Robert N. M. Watson 5e1ac28e2SRobert Watson * All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 8df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 9df8bae1dSRodney W. Grimes * are met: 10df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 12df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 14df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 15df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 16df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 17df8bae1dSRodney W. Grimes * without specific prior written permission. 18df8bae1dSRodney W. Grimes * 19df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29df8bae1dSRodney W. Grimes * SUCH DAMAGE. 30df8bae1dSRodney W. Grimes * 31748e0b0aSGarrett Wollman * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94 32df8bae1dSRodney W. Grimes */ 33df8bae1dSRodney W. Grimes 34f23929fbSRobert Watson /* 35f23929fbSRobert Watson * UNIX Domain (Local) Sockets 36f23929fbSRobert Watson * 37f23929fbSRobert Watson * This is an implementation of UNIX (local) domain sockets. Each socket has 38f23929fbSRobert Watson * an associated struct unpcb (UNIX protocol control block). Stream sockets 39f23929fbSRobert Watson * may be connected to 0 or 1 other socket. Datagram sockets may be 40f23929fbSRobert Watson * connected to 0, 1, or many other sockets. Sockets may be created and 41f23929fbSRobert Watson * connected in pairs (socketpair(2)), or bound/connected to using the file 42f23929fbSRobert Watson * system name space. For most purposes, only the receive socket buffer is 43f23929fbSRobert Watson * used, as sending on one socket delivers directly to the receive socket 445b950deaSRobert Watson * buffer of a second socket. 455b950deaSRobert Watson * 465b950deaSRobert Watson * The implementation is substantially complicated by the fact that 475b950deaSRobert Watson * "ancillary data", such as file descriptors or credentials, may be passed 485b950deaSRobert Watson * across UNIX domain sockets. The potential for passing UNIX domain sockets 495b950deaSRobert Watson * over other UNIX domain sockets requires the implementation of a simple 505b950deaSRobert Watson * garbage collector to find and tear down cycles of disconnected sockets. 51aea52f1bSRobert Watson * 52aea52f1bSRobert Watson * TODO: 53aea52f1bSRobert Watson * SEQPACKET, RDM 54aea52f1bSRobert Watson * rethink name space problems 55aea52f1bSRobert Watson * need a proper out-of-band 56aea52f1bSRobert Watson * lock pushdown 57f23929fbSRobert Watson */ 58f23929fbSRobert Watson 59677b542eSDavid E. O'Brien #include <sys/cdefs.h> 60677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 61677b542eSDavid E. O'Brien 6203c96c31SRobert Watson #include "opt_ddb.h" 63335654d7SRobert Watson #include "opt_mac.h" 64335654d7SRobert Watson 65df8bae1dSRodney W. Grimes #include <sys/param.h> 66fb919e4dSMark Murray #include <sys/domain.h> 67960ed29cSSeigo Tanimura #include <sys/fcntl.h> 68d826c479SBruce Evans #include <sys/malloc.h> /* XXX must be before <sys/file.h> */ 694f590175SPaul Saab #include <sys/eventhandler.h> 70639acc13SGarrett Wollman #include <sys/file.h> 71960ed29cSSeigo Tanimura #include <sys/filedesc.h> 72960ed29cSSeigo Tanimura #include <sys/jail.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> 81960ed29cSSeigo Tanimura #include <sys/resourcevar.h> 82e7c33e29SRobert Watson #include <sys/rwlock.h> 83df8bae1dSRodney W. Grimes #include <sys/socket.h> 84df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 85960ed29cSSeigo Tanimura #include <sys/signalvar.h> 86df8bae1dSRodney W. Grimes #include <sys/stat.h> 87960ed29cSSeigo Tanimura #include <sys/sx.h> 88639acc13SGarrett Wollman #include <sys/sysctl.h> 89960ed29cSSeigo Tanimura #include <sys/systm.h> 90a0ec558aSRobert Watson #include <sys/taskqueue.h> 91639acc13SGarrett Wollman #include <sys/un.h> 9298271db4SGarrett Wollman #include <sys/unpcb.h> 93639acc13SGarrett Wollman #include <sys/vnode.h> 94df8bae1dSRodney W. Grimes 9503c96c31SRobert Watson #ifdef DDB 9603c96c31SRobert Watson #include <ddb/ddb.h> 9703c96c31SRobert Watson #endif 9803c96c31SRobert Watson 99aed55708SRobert Watson #include <security/mac/mac_framework.h> 100aed55708SRobert Watson 1019e9d298aSJeff Roberson #include <vm/uma.h> 10298271db4SGarrett Wollman 1039e9d298aSJeff Roberson static uma_zone_t unp_zone; 10498271db4SGarrett Wollman static unp_gen_t unp_gencnt; 105aea52f1bSRobert Watson static u_int unp_count; /* Count of local sockets. */ 106aea52f1bSRobert Watson static ino_t unp_ino; /* Prototype for fake inode numbers. */ 107aea52f1bSRobert Watson static int unp_rights; /* File descriptors in flight. */ 108aea52f1bSRobert Watson static struct unp_head unp_shead; /* List of local stream sockets. */ 109aea52f1bSRobert Watson static struct unp_head unp_dhead; /* List of local datagram sockets. */ 11098271db4SGarrett Wollman 111aea52f1bSRobert Watson static const struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 11298271db4SGarrett Wollman 113df8bae1dSRodney W. Grimes /* 114aea52f1bSRobert Watson * Garbage collection of cyclic file descriptor/socket references occurs 115aea52f1bSRobert Watson * asynchronously in a taskqueue context in order to avoid recursion and 116aea52f1bSRobert Watson * reentrance in the UNIX domain socket, file descriptor, and socket layer 117aea52f1bSRobert Watson * code. See unp_gc() for a full description. 118df8bae1dSRodney W. Grimes */ 119aea52f1bSRobert Watson static struct task unp_gc_task; 120f708ef1bSPoul-Henning Kamp 121ce5f32deSRobert Watson /* 1227e711c3aSRobert Watson * Both send and receive buffers are allocated PIPSIZ bytes of buffering for 1237e711c3aSRobert Watson * stream sockets, although the total for sender and receiver is actually 1247e711c3aSRobert Watson * only PIPSIZ. 1257e711c3aSRobert Watson * 1267e711c3aSRobert Watson * Datagram sockets really use the sendspace as the maximum datagram size, 1277e711c3aSRobert Watson * and don't really want to reserve the sendspace. Their recvspace should be 1287e711c3aSRobert Watson * large enough for at least one max-size datagram plus address. 1297e711c3aSRobert Watson */ 1307e711c3aSRobert Watson #ifndef PIPSIZ 1317e711c3aSRobert Watson #define PIPSIZ 8192 1327e711c3aSRobert Watson #endif 1337e711c3aSRobert Watson static u_long unpst_sendspace = PIPSIZ; 1347e711c3aSRobert Watson static u_long unpst_recvspace = PIPSIZ; 1357e711c3aSRobert Watson static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 1367e711c3aSRobert Watson static u_long unpdg_recvspace = 4*1024; 1377e711c3aSRobert Watson 138e4445a03SRobert Watson SYSCTL_NODE(_net, PF_LOCAL, local, CTLFLAG_RW, 0, "Local domain"); 139e4445a03SRobert Watson SYSCTL_NODE(_net_local, SOCK_STREAM, stream, CTLFLAG_RW, 0, "SOCK_STREAM"); 140e4445a03SRobert Watson SYSCTL_NODE(_net_local, SOCK_DGRAM, dgram, CTLFLAG_RW, 0, "SOCK_DGRAM"); 141e4445a03SRobert Watson 1427e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 1437e711c3aSRobert Watson &unpst_sendspace, 0, ""); 1447e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 1457e711c3aSRobert Watson &unpst_recvspace, 0, ""); 1467e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 1477e711c3aSRobert Watson &unpdg_sendspace, 0, ""); 1487e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 1497e711c3aSRobert Watson &unpdg_recvspace, 0, ""); 1507e711c3aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); 1517e711c3aSRobert Watson 152e7c33e29SRobert Watson /*- 153e7c33e29SRobert Watson * Locking and synchronization: 154ce5f32deSRobert Watson * 155e7c33e29SRobert Watson * The global UNIX domain socket rwlock (unp_global_rwlock) protects all 156e7c33e29SRobert Watson * global variables, including the linked lists tracking the set of allocated 157e7c33e29SRobert Watson * UNIX domain sockets. The global rwlock also serves to prevent deadlock 158e7c33e29SRobert Watson * when more than one PCB lock is acquired at a time (i.e., during 159e7c33e29SRobert Watson * connect()). Finally, the global rwlock protects uncounted references from 160e7c33e29SRobert Watson * vnodes to sockets bound to those vnodes: to safely dereference the 161e7c33e29SRobert Watson * v_socket pointer, the global rwlock must be held while a full reference is 162e7c33e29SRobert Watson * acquired. 163ce5f32deSRobert Watson * 164e7c33e29SRobert Watson * UNIX domain sockets each have an unpcb hung off of their so_pcb pointer, 165e7c33e29SRobert Watson * allocated in pru_attach() and freed in pru_detach(). The validity of that 166e7c33e29SRobert Watson * pointer is an invariant, so no lock is required to dereference the so_pcb 167e7c33e29SRobert Watson * pointer if a valid socket reference is held by the caller. In practice, 168e7c33e29SRobert Watson * this is always true during operations performed on a socket. Each unpcb 169e7c33e29SRobert Watson * has a back-pointer to its socket, unp_socket, which will be stable under 170e7c33e29SRobert Watson * the same circumstances. 171e7c33e29SRobert Watson * 172e7c33e29SRobert Watson * This pointer may only be safely dereferenced as long as a valid reference 173e7c33e29SRobert Watson * to the unpcb is held. Typically, this reference will be from the socket, 174e7c33e29SRobert Watson * or from another unpcb when the referring unpcb's lock is held (in order 175e7c33e29SRobert Watson * that the reference not be invalidated during use). For example, to follow 176e7c33e29SRobert Watson * unp->unp_conn->unp_socket, you need unlock the lock on unp, not unp_conn, 177e7c33e29SRobert Watson * as unp_socket remains valid as long as the reference to unp_conn is valid. 178e7c33e29SRobert Watson * 179e7c33e29SRobert Watson * Fields of unpcbss are locked using a per-unpcb lock, unp_mtx. Individual 180e7c33e29SRobert Watson * atomic reads without the lock may be performed "lockless", but more 181e7c33e29SRobert Watson * complex reads and read-modify-writes require the mutex to be held. No 182e7c33e29SRobert Watson * lock order is defined between unpcb locks -- multiple unpcb locks may be 183e7c33e29SRobert Watson * acquired at the same time only when holding the global UNIX domain socket 184e7c33e29SRobert Watson * rwlock exclusively, which prevents deadlocks. 185e7c33e29SRobert Watson * 186e7c33e29SRobert Watson * Blocking with UNIX domain sockets is a tricky issue: unlike most network 187e7c33e29SRobert Watson * protocols, bind() is a non-atomic operation, and connect() requires 188e7c33e29SRobert Watson * potential sleeping in the protocol, due to potentially waiting on local or 189e7c33e29SRobert Watson * distributed file systems. We try to separate "lookup" operations, which 190e7c33e29SRobert Watson * may sleep, and the IPC operations themselves, which typically can occur 191e7c33e29SRobert Watson * with relative atomicity as locks can be held over the entire operation. 192e7c33e29SRobert Watson * 193e7c33e29SRobert Watson * Another tricky issue is simultaneous multi-threaded or multi-process 194e7c33e29SRobert Watson * access to a single UNIX domain socket. These are handled by the flags 195e7c33e29SRobert Watson * UNP_CONNECTING and UNP_BINDING, which prevent concurrent connecting or 196e7c33e29SRobert Watson * binding, both of which involve dropping UNIX domain socket locks in order 197e7c33e29SRobert Watson * to perform namei() and other file system operations. 198ce5f32deSRobert Watson */ 199e7c33e29SRobert Watson static struct rwlock unp_global_rwlock; 200e7c33e29SRobert Watson 201e7c33e29SRobert Watson #define UNP_GLOBAL_LOCK_INIT() rw_init(&unp_global_rwlock, \ 202e7c33e29SRobert Watson "unp_global_rwlock") 203e7c33e29SRobert Watson 204e7c33e29SRobert Watson #define UNP_GLOBAL_LOCK_ASSERT() rw_assert(&unp_global_rwlock, \ 205e7c33e29SRobert Watson RA_LOCKED) 206e7c33e29SRobert Watson #define UNP_GLOBAL_UNLOCK_ASSERT() rw_assert(&unp_global_rwlock, \ 207e7c33e29SRobert Watson RA_UNLOCKED) 208e7c33e29SRobert Watson 209e7c33e29SRobert Watson #define UNP_GLOBAL_WLOCK() rw_wlock(&unp_global_rwlock) 210e7c33e29SRobert Watson #define UNP_GLOBAL_WUNLOCK() rw_wunlock(&unp_global_rwlock) 211e7c33e29SRobert Watson #define UNP_GLOBAL_WLOCK_ASSERT() rw_assert(&unp_global_rwlock, \ 212e7c33e29SRobert Watson RA_WLOCKED) 213e7c33e29SRobert Watson #define UNP_GLOBAL_WOWNED() rw_wowned(&unp_global_rwlock) 214e7c33e29SRobert Watson 215e7c33e29SRobert Watson #define UNP_GLOBAL_RLOCK() rw_rlock(&unp_global_rwlock) 216e7c33e29SRobert Watson #define UNP_GLOBAL_RUNLOCK() rw_runlock(&unp_global_rwlock) 217e7c33e29SRobert Watson #define UNP_GLOBAL_RLOCK_ASSERT() rw_assert(&unp_global_rwlock, \ 218e7c33e29SRobert Watson RA_RLOCKED) 219e7c33e29SRobert Watson 220e7c33e29SRobert Watson #define UNP_PCB_LOCK_INIT(unp) mtx_init(&(unp)->unp_mtx, \ 221e7c33e29SRobert Watson "unp_mtx", "unp_mtx", \ 222e7c33e29SRobert Watson MTX_DUPOK|MTX_DEF|MTX_RECURSE) 223e7c33e29SRobert Watson #define UNP_PCB_LOCK_DESTROY(unp) mtx_destroy(&(unp)->unp_mtx) 224e7c33e29SRobert Watson #define UNP_PCB_LOCK(unp) mtx_lock(&(unp)->unp_mtx) 225e7c33e29SRobert Watson #define UNP_PCB_UNLOCK(unp) mtx_unlock(&(unp)->unp_mtx) 226e7c33e29SRobert Watson #define UNP_PCB_LOCK_ASSERT(unp) mtx_assert(&(unp)->unp_mtx, MA_OWNED) 2270d9ce3a1SRobert Watson 228aea52f1bSRobert Watson static int unp_connect(struct socket *, struct sockaddr *, 229aea52f1bSRobert Watson struct thread *); 2306a2989fdSMatthew N. Dodd static int unp_connect2(struct socket *so, struct socket *so2, int); 231e7c33e29SRobert Watson static void unp_disconnect(struct unpcb *unp, struct unpcb *unp2); 2324d77a549SAlfred Perlstein static void unp_shutdown(struct unpcb *); 2334d77a549SAlfred Perlstein static void unp_drop(struct unpcb *, int); 234a0ec558aSRobert Watson static void unp_gc(__unused void *, int); 2354d77a549SAlfred Perlstein static void unp_scan(struct mbuf *, void (*)(struct file *)); 2364d77a549SAlfred Perlstein static void unp_mark(struct file *); 2374d77a549SAlfred Perlstein static void unp_discard(struct file *); 2384d77a549SAlfred Perlstein static void unp_freerights(struct file **, int); 2394d77a549SAlfred Perlstein static int unp_internalize(struct mbuf **, struct thread *); 2405b950deaSRobert Watson static struct mbuf *unp_addsockcred(struct thread *, struct mbuf *); 241f708ef1bSPoul-Henning Kamp 242e4445a03SRobert Watson /* 243e4445a03SRobert Watson * Definitions of protocols supported in the LOCAL domain. 244e4445a03SRobert Watson */ 245e4445a03SRobert Watson static struct domain localdomain; 246e4445a03SRobert Watson static struct protosw localsw[] = { 247e4445a03SRobert Watson { 248e4445a03SRobert Watson .pr_type = SOCK_STREAM, 249e4445a03SRobert Watson .pr_domain = &localdomain, 250e4445a03SRobert Watson .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_RIGHTS, 251e4445a03SRobert Watson .pr_ctloutput = &uipc_ctloutput, 252e4445a03SRobert Watson .pr_usrreqs = &uipc_usrreqs 253e4445a03SRobert Watson }, 254e4445a03SRobert Watson { 255e4445a03SRobert Watson .pr_type = SOCK_DGRAM, 256e4445a03SRobert Watson .pr_domain = &localdomain, 257e4445a03SRobert Watson .pr_flags = PR_ATOMIC|PR_ADDR|PR_RIGHTS, 258e4445a03SRobert Watson .pr_usrreqs = &uipc_usrreqs 259e4445a03SRobert Watson }, 260e4445a03SRobert Watson }; 261e4445a03SRobert Watson 262e4445a03SRobert Watson static struct domain localdomain = { 263e4445a03SRobert Watson .dom_family = AF_LOCAL, 264e4445a03SRobert Watson .dom_name = "local", 265e4445a03SRobert Watson .dom_init = unp_init, 266e4445a03SRobert Watson .dom_externalize = unp_externalize, 267e4445a03SRobert Watson .dom_dispose = unp_dispose, 268e4445a03SRobert Watson .dom_protosw = localsw, 269e4445a03SRobert Watson .dom_protoswNPROTOSW = &localsw[sizeof(localsw)/sizeof(localsw[0])] 270e4445a03SRobert Watson }; 271e4445a03SRobert Watson DOMAIN_SET(local); 272e4445a03SRobert Watson 273ac45e92fSRobert Watson static void 274a29f300eSGarrett Wollman uipc_abort(struct socket *so) 275df8bae1dSRodney W. Grimes { 276e7c33e29SRobert Watson struct unpcb *unp, *unp2; 277df8bae1dSRodney W. Grimes 27840f2ac28SRobert Watson unp = sotounpcb(so); 2794d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_abort: unp == NULL")); 280e7c33e29SRobert Watson 281e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 282e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 283e7c33e29SRobert Watson unp2 = unp->unp_conn; 284e7c33e29SRobert Watson if (unp2 != NULL) { 285e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 286e7c33e29SRobert Watson unp_drop(unp2, ECONNABORTED); 287e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 288e7c33e29SRobert Watson } 289e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 290e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 291df8bae1dSRodney W. Grimes } 292df8bae1dSRodney W. Grimes 293a29f300eSGarrett Wollman static int 29457bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam) 295a29f300eSGarrett Wollman { 296e7c33e29SRobert Watson struct unpcb *unp, *unp2; 2970d9ce3a1SRobert Watson const struct sockaddr *sa; 298df8bae1dSRodney W. Grimes 299df8bae1dSRodney W. Grimes /* 3001c381b19SRobert Watson * Pass back name of connected socket, if it was bound and we are 3011c381b19SRobert Watson * still connected (our peer may have closed already!). 302df8bae1dSRodney W. Grimes */ 3034d4b555eSRobert Watson unp = sotounpcb(so); 3044d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_accept: unp == NULL")); 305e7c33e29SRobert Watson 3060d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 307e7c33e29SRobert Watson UNP_GLOBAL_RLOCK(); 308e7c33e29SRobert Watson unp2 = unp->unp_conn; 309e7c33e29SRobert Watson if (unp2 != NULL && unp2->unp_addr != NULL) { 310e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 311e7c33e29SRobert Watson sa = (struct sockaddr *) unp2->unp_addr; 312e7c33e29SRobert Watson bcopy(sa, *nam, sa->sa_len); 313e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 314e7c33e29SRobert Watson } else { 3150d9ce3a1SRobert Watson sa = &sun_noname; 3160d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 317e7c33e29SRobert Watson } 318e7c33e29SRobert Watson UNP_GLOBAL_RUNLOCK(); 319e5aeaa0cSDag-Erling Smørgrav return (0); 320a29f300eSGarrett Wollman } 321df8bae1dSRodney W. Grimes 322a29f300eSGarrett Wollman static int 323b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td) 324a29f300eSGarrett Wollman { 325e7c33e29SRobert Watson u_long sendspace, recvspace; 3266d32873cSRobert Watson struct unpcb *unp; 327e7c33e29SRobert Watson int error, locked; 328df8bae1dSRodney W. Grimes 3296d32873cSRobert Watson KASSERT(so->so_pcb == NULL, ("uipc_attach: so_pcb != NULL")); 3306d32873cSRobert Watson if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 3316d32873cSRobert Watson switch (so->so_type) { 3326d32873cSRobert Watson case SOCK_STREAM: 333e7c33e29SRobert Watson sendspace = unpst_sendspace; 334e7c33e29SRobert Watson recvspace = unpst_recvspace; 3356d32873cSRobert Watson break; 3366d32873cSRobert Watson 3376d32873cSRobert Watson case SOCK_DGRAM: 338e7c33e29SRobert Watson sendspace = unpdg_sendspace; 339e7c33e29SRobert Watson recvspace = unpdg_recvspace; 3406d32873cSRobert Watson break; 3416d32873cSRobert Watson 3426d32873cSRobert Watson default: 343e7c33e29SRobert Watson panic("uipc_attach"); 3446d32873cSRobert Watson } 345e7c33e29SRobert Watson error = soreserve(so, sendspace, recvspace); 3466d32873cSRobert Watson if (error) 3476d32873cSRobert Watson return (error); 3486d32873cSRobert Watson } 34946a1d9bfSRobert Watson unp = uma_zalloc(unp_zone, M_NOWAIT | M_ZERO); 3506d32873cSRobert Watson if (unp == NULL) 3516d32873cSRobert Watson return (ENOBUFS); 3526d32873cSRobert Watson LIST_INIT(&unp->unp_refs); 353e7c33e29SRobert Watson UNP_PCB_LOCK_INIT(unp); 3546d32873cSRobert Watson unp->unp_socket = so; 3556d32873cSRobert Watson so->so_pcb = unp; 3569ae328fcSJohn Baldwin unp->unp_refcount = 1; 357e7c33e29SRobert Watson 358e7c33e29SRobert Watson /* 359e7c33e29SRobert Watson * uipc_attach() may be called indirectly from within the UNIX domain 360e7c33e29SRobert Watson * socket code via sonewconn() in unp_connect(). Since rwlocks can 361e7c33e29SRobert Watson * not be recursed, we do the closest thing. 362e7c33e29SRobert Watson */ 363d7924b70SRobert Watson locked = 0; 364e7c33e29SRobert Watson if (!UNP_GLOBAL_WOWNED()) { 365e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 366e7c33e29SRobert Watson locked = 1; 367e7c33e29SRobert Watson } 3686d32873cSRobert Watson unp->unp_gencnt = ++unp_gencnt; 3696d32873cSRobert Watson unp_count++; 370b7e2f3ecSRobert Watson LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead : &unp_shead, 371b7e2f3ecSRobert Watson unp, unp_link); 372e7c33e29SRobert Watson if (locked) 373e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 3746d32873cSRobert Watson 3756d32873cSRobert Watson return (0); 376a29f300eSGarrett Wollman } 377a29f300eSGarrett Wollman 378a29f300eSGarrett Wollman static int 379b40ce416SJulian Elischer uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 380a29f300eSGarrett Wollman { 381dd47f5caSRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam; 382dd47f5caSRobert Watson struct vattr vattr; 3839e289446SWojciech A. Koszek int error, namelen, vfslocked; 384dd47f5caSRobert Watson struct nameidata nd; 38540f2ac28SRobert Watson struct unpcb *unp; 386dd47f5caSRobert Watson struct vnode *vp; 387dd47f5caSRobert Watson struct mount *mp; 388dd47f5caSRobert Watson char *buf; 389a29f300eSGarrett Wollman 39040f2ac28SRobert Watson unp = sotounpcb(so); 3914d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_bind: unp == NULL")); 3924f1f0ef5SRobert Watson 3934f1f0ef5SRobert Watson namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 3944f1f0ef5SRobert Watson if (namelen <= 0) 3954f1f0ef5SRobert Watson return (EINVAL); 396dd47f5caSRobert Watson 397dd47f5caSRobert Watson /* 3984f1f0ef5SRobert Watson * We don't allow simultaneous bind() calls on a single UNIX domain 3994f1f0ef5SRobert Watson * socket, so flag in-progress operations, and return an error if an 4004f1f0ef5SRobert Watson * operation is already in progress. 4014f1f0ef5SRobert Watson * 4024f1f0ef5SRobert Watson * Historically, we have not allowed a socket to be rebound, so this 403d7924b70SRobert Watson * also returns an error. Not allowing re-binding simplifies the 404d7924b70SRobert Watson * implementation and avoids a great many possible failure modes. 405dd47f5caSRobert Watson */ 406e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 407dd47f5caSRobert Watson if (unp->unp_vnode != NULL) { 408e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 409dd47f5caSRobert Watson return (EINVAL); 410dd47f5caSRobert Watson } 4114f1f0ef5SRobert Watson if (unp->unp_flags & UNP_BINDING) { 412e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 4134f1f0ef5SRobert Watson return (EALREADY); 414dd47f5caSRobert Watson } 4154f1f0ef5SRobert Watson unp->unp_flags |= UNP_BINDING; 416e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 417dd47f5caSRobert Watson 418dd47f5caSRobert Watson buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 419dd47f5caSRobert Watson strlcpy(buf, soun->sun_path, namelen + 1); 420dd47f5caSRobert Watson 421dd47f5caSRobert Watson restart: 4229e289446SWojciech A. Koszek vfslocked = 0; 4239e289446SWojciech A. Koszek NDINIT(&nd, CREATE, MPSAFE | NOFOLLOW | LOCKPARENT | SAVENAME, 4249e289446SWojciech A. Koszek UIO_SYSSPACE, buf, td); 425dd47f5caSRobert Watson /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 426dd47f5caSRobert Watson error = namei(&nd); 427dd47f5caSRobert Watson if (error) 4284f1f0ef5SRobert Watson goto error; 429dd47f5caSRobert Watson vp = nd.ni_vp; 4309e289446SWojciech A. Koszek vfslocked = NDHASGIANT(&nd); 431dd47f5caSRobert Watson if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 432dd47f5caSRobert Watson NDFREE(&nd, NDF_ONLY_PNBUF); 433dd47f5caSRobert Watson if (nd.ni_dvp == vp) 434dd47f5caSRobert Watson vrele(nd.ni_dvp); 435dd47f5caSRobert Watson else 436dd47f5caSRobert Watson vput(nd.ni_dvp); 437dd47f5caSRobert Watson if (vp != NULL) { 438dd47f5caSRobert Watson vrele(vp); 439dd47f5caSRobert Watson error = EADDRINUSE; 4404f1f0ef5SRobert Watson goto error; 441dd47f5caSRobert Watson } 442dd47f5caSRobert Watson error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 443dd47f5caSRobert Watson if (error) 4444f1f0ef5SRobert Watson goto error; 4459e289446SWojciech A. Koszek VFS_UNLOCK_GIANT(vfslocked); 446dd47f5caSRobert Watson goto restart; 447dd47f5caSRobert Watson } 448dd47f5caSRobert Watson VATTR_NULL(&vattr); 449dd47f5caSRobert Watson vattr.va_type = VSOCK; 450dd47f5caSRobert Watson vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 451dd47f5caSRobert Watson #ifdef MAC 452dd47f5caSRobert Watson error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 453dd47f5caSRobert Watson &vattr); 454dd47f5caSRobert Watson #endif 455dd47f5caSRobert Watson if (error == 0) { 456dd47f5caSRobert Watson VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE); 457dd47f5caSRobert Watson error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 458dd47f5caSRobert Watson } 459dd47f5caSRobert Watson NDFREE(&nd, NDF_ONLY_PNBUF); 460dd47f5caSRobert Watson vput(nd.ni_dvp); 461dd47f5caSRobert Watson if (error) { 462dd47f5caSRobert Watson vn_finished_write(mp); 4634f1f0ef5SRobert Watson goto error; 464dd47f5caSRobert Watson } 465dd47f5caSRobert Watson vp = nd.ni_vp; 4664f1f0ef5SRobert Watson ASSERT_VOP_LOCKED(vp, "uipc_bind"); 467dd47f5caSRobert Watson soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 468e7c33e29SRobert Watson 469e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 470e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 471dd47f5caSRobert Watson vp->v_socket = unp->unp_socket; 472dd47f5caSRobert Watson unp->unp_vnode = vp; 473dd47f5caSRobert Watson unp->unp_addr = soun; 4744f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_BINDING; 475e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 476e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 477dd47f5caSRobert Watson VOP_UNLOCK(vp, 0, td); 478dd47f5caSRobert Watson vn_finished_write(mp); 4799e289446SWojciech A. Koszek VFS_UNLOCK_GIANT(vfslocked); 4804f1f0ef5SRobert Watson free(buf, M_TEMP); 4814f1f0ef5SRobert Watson return (0); 482e7c33e29SRobert Watson 4834f1f0ef5SRobert Watson error: 4849e289446SWojciech A. Koszek VFS_UNLOCK_GIANT(vfslocked); 485e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 4864f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_BINDING; 487e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 488dd47f5caSRobert Watson free(buf, M_TEMP); 48940f2ac28SRobert Watson return (error); 490a29f300eSGarrett Wollman } 491a29f300eSGarrett Wollman 492a29f300eSGarrett Wollman static int 493b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 494a29f300eSGarrett Wollman { 4950d9ce3a1SRobert Watson int error; 496a29f300eSGarrett Wollman 497fd179ee9SRobert Watson KASSERT(td == curthread, ("uipc_connect: td != curthread")); 498e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 499fd179ee9SRobert Watson error = unp_connect(so, nam, td); 500e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 5010d9ce3a1SRobert Watson return (error); 502a29f300eSGarrett Wollman } 503a29f300eSGarrett Wollman 504a152f8a3SRobert Watson static void 505a152f8a3SRobert Watson uipc_close(struct socket *so) 506a152f8a3SRobert Watson { 507e7c33e29SRobert Watson struct unpcb *unp, *unp2; 508a152f8a3SRobert Watson 509a152f8a3SRobert Watson unp = sotounpcb(so); 510a152f8a3SRobert Watson KASSERT(unp != NULL, ("uipc_close: unp == NULL")); 511e7c33e29SRobert Watson 512e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 513e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 514e7c33e29SRobert Watson unp2 = unp->unp_conn; 515e7c33e29SRobert Watson if (unp2 != NULL) { 516e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 517e7c33e29SRobert Watson unp_disconnect(unp, unp2); 518e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 519e7c33e29SRobert Watson } 520e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 521e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 522a152f8a3SRobert Watson } 523a152f8a3SRobert Watson 524db48c0d2SRobert Watson int 525a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2) 526a29f300eSGarrett Wollman { 527e7c33e29SRobert Watson struct unpcb *unp, *unp2; 5280d9ce3a1SRobert Watson int error; 529a29f300eSGarrett Wollman 530e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 531e7c33e29SRobert Watson unp = so1->so_pcb; 5324d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_connect2: unp == NULL")); 533e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 534e7c33e29SRobert Watson unp2 = so2->so_pcb; 535e7c33e29SRobert Watson KASSERT(unp2 != NULL, ("uipc_connect2: unp2 == NULL")); 536e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 5376a2989fdSMatthew N. Dodd error = unp_connect2(so1, so2, PRU_CONNECT2); 538e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 539e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 540e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 5410d9ce3a1SRobert Watson return (error); 542a29f300eSGarrett Wollman } 543a29f300eSGarrett Wollman 544a29f300eSGarrett Wollman /* control is EOPNOTSUPP */ 545a29f300eSGarrett Wollman 546bc725eafSRobert Watson static void 547a29f300eSGarrett Wollman uipc_detach(struct socket *so) 548a29f300eSGarrett Wollman { 549e7c33e29SRobert Watson struct unpcb *unp, *unp2; 5509ae328fcSJohn Baldwin struct sockaddr_un *saved_unp_addr; 5516d32873cSRobert Watson struct vnode *vp; 5529ae328fcSJohn Baldwin int freeunp, local_unp_rights; 553a29f300eSGarrett Wollman 55440f2ac28SRobert Watson unp = sotounpcb(so); 5554d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_detach: unp == NULL")); 556e7c33e29SRobert Watson 557e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 558e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 559e7c33e29SRobert Watson 5606d32873cSRobert Watson LIST_REMOVE(unp, unp_link); 5616d32873cSRobert Watson unp->unp_gencnt = ++unp_gencnt; 5626d32873cSRobert Watson --unp_count; 563e7c33e29SRobert Watson 564e7c33e29SRobert Watson /* 565e7c33e29SRobert Watson * XXXRW: Should assert vp->v_socket == so. 566e7c33e29SRobert Watson */ 5676d32873cSRobert Watson if ((vp = unp->unp_vnode) != NULL) { 5686d32873cSRobert Watson unp->unp_vnode->v_socket = NULL; 5696d32873cSRobert Watson unp->unp_vnode = NULL; 5706d32873cSRobert Watson } 571e7c33e29SRobert Watson unp2 = unp->unp_conn; 572e7c33e29SRobert Watson if (unp2 != NULL) { 573e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 574e7c33e29SRobert Watson unp_disconnect(unp, unp2); 575e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 576e7c33e29SRobert Watson } 577e7c33e29SRobert Watson 578e7c33e29SRobert Watson /* 579e7c33e29SRobert Watson * We hold the global lock, so it's OK to acquire multiple pcb locks 580e7c33e29SRobert Watson * at a time. 581e7c33e29SRobert Watson */ 5826d32873cSRobert Watson while (!LIST_EMPTY(&unp->unp_refs)) { 5836d32873cSRobert Watson struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 584e7c33e29SRobert Watson 585e7c33e29SRobert Watson UNP_PCB_LOCK(ref); 5866d32873cSRobert Watson unp_drop(ref, ECONNRESET); 587e7c33e29SRobert Watson UNP_PCB_UNLOCK(ref); 5886d32873cSRobert Watson } 589e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 5906d32873cSRobert Watson unp->unp_socket->so_pcb = NULL; 5916d32873cSRobert Watson local_unp_rights = unp_rights; 5929ae328fcSJohn Baldwin saved_unp_addr = unp->unp_addr; 5939ae328fcSJohn Baldwin unp->unp_addr = NULL; 5949ae328fcSJohn Baldwin unp->unp_refcount--; 5959ae328fcSJohn Baldwin freeunp = (unp->unp_refcount == 0); 5969ae328fcSJohn Baldwin if (saved_unp_addr != NULL) 5979ae328fcSJohn Baldwin FREE(saved_unp_addr, M_SONAME); 598e7c33e29SRobert Watson if (freeunp) { 599e7c33e29SRobert Watson UNP_PCB_LOCK_DESTROY(unp); 6006d32873cSRobert Watson uma_zfree(unp_zone, unp); 6016e2faa24SRobert Watson } else 6026e2faa24SRobert Watson UNP_PCB_UNLOCK(unp); 6036d32873cSRobert Watson if (vp) { 6046d32873cSRobert Watson int vfslocked; 6056d32873cSRobert Watson 6066d32873cSRobert Watson vfslocked = VFS_LOCK_GIANT(vp->v_mount); 6076d32873cSRobert Watson vrele(vp); 6086d32873cSRobert Watson VFS_UNLOCK_GIANT(vfslocked); 6096d32873cSRobert Watson } 6106d32873cSRobert Watson if (local_unp_rights) 6116d32873cSRobert Watson taskqueue_enqueue(taskqueue_thread, &unp_gc_task); 612a29f300eSGarrett Wollman } 613a29f300eSGarrett Wollman 614a29f300eSGarrett Wollman static int 615a29f300eSGarrett Wollman uipc_disconnect(struct socket *so) 616a29f300eSGarrett Wollman { 617e7c33e29SRobert Watson struct unpcb *unp, *unp2; 618a29f300eSGarrett Wollman 61940f2ac28SRobert Watson unp = sotounpcb(so); 6204d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_disconnect: unp == NULL")); 621e7c33e29SRobert Watson 622e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 623e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 624e7c33e29SRobert Watson unp2 = unp->unp_conn; 625e7c33e29SRobert Watson if (unp2 != NULL) { 626e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 627e7c33e29SRobert Watson unp_disconnect(unp, unp2); 628e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 629e7c33e29SRobert Watson } 630e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 631e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 632e5aeaa0cSDag-Erling Smørgrav return (0); 633a29f300eSGarrett Wollman } 634a29f300eSGarrett Wollman 635a29f300eSGarrett Wollman static int 636d374e81eSRobert Watson uipc_listen(struct socket *so, int backlog, struct thread *td) 637a29f300eSGarrett Wollman { 63840f2ac28SRobert Watson struct unpcb *unp; 6390d9ce3a1SRobert Watson int error; 640a29f300eSGarrett Wollman 64140f2ac28SRobert Watson unp = sotounpcb(so); 6424d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_listen: unp == NULL")); 643e7c33e29SRobert Watson 644e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 6454d4b555eSRobert Watson if (unp->unp_vnode == NULL) { 646e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 64740f2ac28SRobert Watson return (EINVAL); 64840f2ac28SRobert Watson } 649e7c33e29SRobert Watson 650e7c33e29SRobert Watson SOCK_LOCK(so); 651e7c33e29SRobert Watson error = solisten_proto_check(so); 652e7c33e29SRobert Watson if (error == 0) { 653e7c33e29SRobert Watson cru2x(td->td_ucred, &unp->unp_peercred); 654e7c33e29SRobert Watson unp->unp_flags |= UNP_HAVEPCCACHED; 655e7c33e29SRobert Watson solisten_proto(so, backlog); 656e7c33e29SRobert Watson } 657e7c33e29SRobert Watson SOCK_UNLOCK(so); 658e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 6590d9ce3a1SRobert Watson return (error); 660a29f300eSGarrett Wollman } 661a29f300eSGarrett Wollman 662a29f300eSGarrett Wollman static int 66357bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 664a29f300eSGarrett Wollman { 665e7c33e29SRobert Watson struct unpcb *unp, *unp2; 6660d9ce3a1SRobert Watson const struct sockaddr *sa; 667a29f300eSGarrett Wollman 6684d4b555eSRobert Watson unp = sotounpcb(so); 6694d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL")); 670e7c33e29SRobert Watson 6710d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 672e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 673bdc5f6a3SHajimu UMEMOTO /* 674e7c33e29SRobert Watson * XXX: It seems that this test always fails even when connection is 675e7c33e29SRobert Watson * established. So, this else clause is added as workaround to 676e7c33e29SRobert Watson * return PF_LOCAL sockaddr. 677bdc5f6a3SHajimu UMEMOTO */ 678e7c33e29SRobert Watson unp2 = unp->unp_conn; 679e7c33e29SRobert Watson if (unp2 != NULL) { 680e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 681e7c33e29SRobert Watson if (unp2->unp_addr != NULL) 682e7c33e29SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 683e7c33e29SRobert Watson else 6840d9ce3a1SRobert Watson sa = &sun_noname; 6850d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 686e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 687e7c33e29SRobert Watson } else { 688e7c33e29SRobert Watson sa = &sun_noname; 689e7c33e29SRobert Watson bcopy(sa, *nam, sa->sa_len); 690e7c33e29SRobert Watson } 691e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 692e5aeaa0cSDag-Erling Smørgrav return (0); 693a29f300eSGarrett Wollman } 694a29f300eSGarrett Wollman 695a29f300eSGarrett Wollman static int 696a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 697a29f300eSGarrett Wollman { 698e7c33e29SRobert Watson struct unpcb *unp, *unp2; 699a29f300eSGarrett Wollman struct socket *so2; 700337cc6b6SRobert Watson u_int mbcnt, sbcc; 7016aef685fSBrian Feldman u_long newhiwat; 702a29f300eSGarrett Wollman 70340f2ac28SRobert Watson unp = sotounpcb(so); 7044d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_rcvd: unp == NULL")); 705df8bae1dSRodney W. Grimes 706e7c33e29SRobert Watson if (so->so_type == SOCK_DGRAM) 707e7c33e29SRobert Watson panic("uipc_rcvd DGRAM?"); 708e7c33e29SRobert Watson 709e7c33e29SRobert Watson if (so->so_type != SOCK_STREAM) 710e7c33e29SRobert Watson panic("uipc_rcvd unknown socktype"); 711e7c33e29SRobert Watson 712df8bae1dSRodney W. Grimes /* 713e7c33e29SRobert Watson * Adjust backpressure on sender and wakeup any waiting to write. 714e7c33e29SRobert Watson * 715d7924b70SRobert Watson * The unp lock is acquired to maintain the validity of the unp_conn 716d7924b70SRobert Watson * pointer; no lock on unp2 is required as unp2->unp_socket will be 717d7924b70SRobert Watson * static as long as we don't permit unp2 to disconnect from unp, 718d7924b70SRobert Watson * which is prevented by the lock on unp. We cache values from 719d7924b70SRobert Watson * so_rcv to avoid holding the so_rcv lock over the entire 720d7924b70SRobert Watson * transaction on the remote so_snd. 721df8bae1dSRodney W. Grimes */ 722337cc6b6SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 723337cc6b6SRobert Watson mbcnt = so->so_rcv.sb_mbcnt; 724337cc6b6SRobert Watson sbcc = so->so_rcv.sb_cc; 725337cc6b6SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 726e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 727e7c33e29SRobert Watson unp2 = unp->unp_conn; 728e7c33e29SRobert Watson if (unp2 == NULL) { 729e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 730e7c33e29SRobert Watson return (0); 731337cc6b6SRobert Watson } 732e7c33e29SRobert Watson so2 = unp2->unp_socket; 733337cc6b6SRobert Watson SOCKBUF_LOCK(&so2->so_snd); 734337cc6b6SRobert Watson so2->so_snd.sb_mbmax += unp->unp_mbcnt - mbcnt; 735337cc6b6SRobert Watson newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - sbcc; 736f535380cSDon Lewis (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat, 7376aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 7381e4d7da7SRobert Watson sowwakeup_locked(so2); 739337cc6b6SRobert Watson unp->unp_mbcnt = mbcnt; 740337cc6b6SRobert Watson unp->unp_cc = sbcc; 741e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 742e5aeaa0cSDag-Erling Smørgrav return (0); 743a29f300eSGarrett Wollman } 744df8bae1dSRodney W. Grimes 745a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 746a29f300eSGarrett Wollman 747a29f300eSGarrett Wollman static int 74857bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 749b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 750a29f300eSGarrett Wollman { 751f3f49bbbSRobert Watson struct unpcb *unp, *unp2; 752a29f300eSGarrett Wollman struct socket *so2; 753337cc6b6SRobert Watson u_int mbcnt, sbcc; 7546aef685fSBrian Feldman u_long newhiwat; 755f3f49bbbSRobert Watson int error = 0; 756a29f300eSGarrett Wollman 75740f2ac28SRobert Watson unp = sotounpcb(so); 7584d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_send: unp == NULL")); 759e7c33e29SRobert Watson 760a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 761a29f300eSGarrett Wollman error = EOPNOTSUPP; 762a29f300eSGarrett Wollman goto release; 763a29f300eSGarrett Wollman } 764a29f300eSGarrett Wollman 765fc3fcacfSRobert Watson if (control != NULL && (error = unp_internalize(&control, td))) 766a29f300eSGarrett Wollman goto release; 767df8bae1dSRodney W. Grimes 768e7c33e29SRobert Watson if ((nam != NULL) || (flags & PRUS_EOF)) 769e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 770e7c33e29SRobert Watson else 771e7c33e29SRobert Watson UNP_GLOBAL_RLOCK(); 772e7c33e29SRobert Watson 773a29f300eSGarrett Wollman switch (so->so_type) { 774a29f300eSGarrett Wollman case SOCK_DGRAM: 775a29f300eSGarrett Wollman { 776e7dd9a10SRobert Watson const struct sockaddr *from; 777df8bae1dSRodney W. Grimes 778e7c33e29SRobert Watson unp2 = unp->unp_conn; 779fc3fcacfSRobert Watson if (nam != NULL) { 780ede6e136SRobert Watson UNP_GLOBAL_WLOCK_ASSERT(); 781e7c33e29SRobert Watson if (unp2 != NULL) { 782df8bae1dSRodney W. Grimes error = EISCONN; 783df8bae1dSRodney W. Grimes break; 784df8bae1dSRodney W. Grimes } 785b40ce416SJulian Elischer error = unp_connect(so, nam, td); 786df8bae1dSRodney W. Grimes if (error) 787df8bae1dSRodney W. Grimes break; 788e7c33e29SRobert Watson unp2 = unp->unp_conn; 789df8bae1dSRodney W. Grimes } 790b5ff0914SRobert Watson /* 791b5ff0914SRobert Watson * Because connect() and send() are non-atomic in a sendto() 792b5ff0914SRobert Watson * with a target address, it's possible that the socket will 793b5ff0914SRobert Watson * have disconnected before the send() can run. In that case 794b5ff0914SRobert Watson * return the slightly counter-intuitive but otherwise 795b5ff0914SRobert Watson * correct error that the socket is not connected. 796b5ff0914SRobert Watson */ 797b5ff0914SRobert Watson if (unp2 == NULL) { 798b5ff0914SRobert Watson error = ENOTCONN; 799b5ff0914SRobert Watson break; 800b5ff0914SRobert Watson } 801ede6e136SRobert Watson /* Lockless read. */ 802ede6e136SRobert Watson if (unp2->unp_flags & UNP_WANTCRED) 803ede6e136SRobert Watson control = unp_addsockcred(td, control); 804ede6e136SRobert Watson UNP_PCB_LOCK(unp); 805fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 80657bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 807df8bae1dSRodney W. Grimes else 808df8bae1dSRodney W. Grimes from = &sun_noname; 809ede6e136SRobert Watson so2 = unp2->unp_socket; 810a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 811a34b7046SRobert Watson if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) { 8121e4d7da7SRobert Watson sorwakeup_locked(so2); 813fc3fcacfSRobert Watson m = NULL; 814fc3fcacfSRobert Watson control = NULL; 815e5aeaa0cSDag-Erling Smørgrav } else { 816a34b7046SRobert Watson SOCKBUF_UNLOCK(&so2->so_rcv); 817df8bae1dSRodney W. Grimes error = ENOBUFS; 818e5aeaa0cSDag-Erling Smørgrav } 819ede6e136SRobert Watson if (nam != NULL) { 820ede6e136SRobert Watson UNP_GLOBAL_WLOCK_ASSERT(); 821ede6e136SRobert Watson UNP_PCB_LOCK(unp2); 822e7c33e29SRobert Watson unp_disconnect(unp, unp2); 823e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 824ede6e136SRobert Watson } 825ede6e136SRobert Watson UNP_PCB_UNLOCK(unp); 826df8bae1dSRodney W. Grimes break; 827df8bae1dSRodney W. Grimes } 828df8bae1dSRodney W. Grimes 829df8bae1dSRodney W. Grimes case SOCK_STREAM: 8306b8fda4dSGarrett Wollman /* 8311c381b19SRobert Watson * Connect if not connected yet. 8321c381b19SRobert Watson * 8331c381b19SRobert Watson * Note: A better implementation would complain if not equal 8341c381b19SRobert Watson * to the peer's address. 8356b8fda4dSGarrett Wollman */ 836402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 837fc3fcacfSRobert Watson if (nam != NULL) { 838ede6e136SRobert Watson UNP_GLOBAL_WLOCK_ASSERT(); 839b40ce416SJulian Elischer error = unp_connect(so, nam, td); 840402cc72dSDavid Greenman if (error) 8416b8fda4dSGarrett Wollman break; /* XXX */ 842402cc72dSDavid Greenman } else { 843402cc72dSDavid Greenman error = ENOTCONN; 844402cc72dSDavid Greenman break; 845402cc72dSDavid Greenman } 846ede6e136SRobert Watson } 847402cc72dSDavid Greenman 848337cc6b6SRobert Watson /* Lockless read. */ 849c0b99ffaSRobert Watson if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 850df8bae1dSRodney W. Grimes error = EPIPE; 851df8bae1dSRodney W. Grimes break; 852df8bae1dSRodney W. Grimes } 853b5ff0914SRobert Watson /* 854b5ff0914SRobert Watson * Because connect() and send() are non-atomic in a sendto() 855b5ff0914SRobert Watson * with a target address, it's possible that the socket will 856b5ff0914SRobert Watson * have disconnected before the send() can run. In that case 857b5ff0914SRobert Watson * return the slightly counter-intuitive but otherwise 858b5ff0914SRobert Watson * correct error that the socket is not connected. 859e7c33e29SRobert Watson * 860d7924b70SRobert Watson * Locking here must be done carefully: the global lock 861d7924b70SRobert Watson * prevents interconnections between unpcbs from changing, so 862d7924b70SRobert Watson * we can traverse from unp to unp2 without acquiring unp's 863d7924b70SRobert Watson * lock. Socket buffer locks follow unpcb locks, so we can 864d7924b70SRobert Watson * acquire both remote and lock socket buffer locks. 865b5ff0914SRobert Watson */ 866f3f49bbbSRobert Watson unp2 = unp->unp_conn; 867b5ff0914SRobert Watson if (unp2 == NULL) { 868b5ff0914SRobert Watson error = ENOTCONN; 869b5ff0914SRobert Watson break; 870b5ff0914SRobert Watson } 871f3f49bbbSRobert Watson so2 = unp2->unp_socket; 872ede6e136SRobert Watson UNP_PCB_LOCK(unp2); 873a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 874f3f49bbbSRobert Watson if (unp2->unp_flags & UNP_WANTCRED) { 8756a2989fdSMatthew N. Dodd /* 876ede6e136SRobert Watson * Credentials are passed only once on SOCK_STREAM. 8776a2989fdSMatthew N. Dodd */ 878f3f49bbbSRobert Watson unp2->unp_flags &= ~UNP_WANTCRED; 8796a2989fdSMatthew N. Dodd control = unp_addsockcred(td, control); 8806a2989fdSMatthew N. Dodd } 881df8bae1dSRodney W. Grimes /* 8821c381b19SRobert Watson * Send to paired receive port, and then reduce send buffer 8831c381b19SRobert Watson * hiwater marks to maintain backpressure. Wake up readers. 884df8bae1dSRodney W. Grimes */ 885fc3fcacfSRobert Watson if (control != NULL) { 886a34b7046SRobert Watson if (sbappendcontrol_locked(&so2->so_rcv, m, control)) 887fc3fcacfSRobert Watson control = NULL; 888e7c33e29SRobert Watson } else 889a34b7046SRobert Watson sbappend_locked(&so2->so_rcv, m); 890f3f49bbbSRobert Watson mbcnt = so2->so_rcv.sb_mbcnt - unp2->unp_mbcnt; 891f3f49bbbSRobert Watson unp2->unp_mbcnt = so2->so_rcv.sb_mbcnt; 892337cc6b6SRobert Watson sbcc = so2->so_rcv.sb_cc; 893337cc6b6SRobert Watson sorwakeup_locked(so2); 894337cc6b6SRobert Watson 895337cc6b6SRobert Watson SOCKBUF_LOCK(&so->so_snd); 896f3f49bbbSRobert Watson newhiwat = so->so_snd.sb_hiwat - (sbcc - unp2->unp_cc); 897f535380cSDon Lewis (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat, 8986aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 899337cc6b6SRobert Watson so->so_snd.sb_mbmax -= mbcnt; 9007abe2ac2SAlan Cox SOCKBUF_UNLOCK(&so->so_snd); 901f3f49bbbSRobert Watson unp2->unp_cc = sbcc; 902e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 903fc3fcacfSRobert Watson m = NULL; 904df8bae1dSRodney W. Grimes break; 905df8bae1dSRodney W. Grimes 906df8bae1dSRodney W. Grimes default: 907a29f300eSGarrett Wollman panic("uipc_send unknown socktype"); 908df8bae1dSRodney W. Grimes } 909a29f300eSGarrett Wollman 9106b8fda4dSGarrett Wollman /* 911ede6e136SRobert Watson * SEND_EOF is equivalent to a SEND followed by a SHUTDOWN. 9126b8fda4dSGarrett Wollman */ 913a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 914ede6e136SRobert Watson UNP_PCB_LOCK(unp); 9156b8fda4dSGarrett Wollman socantsendmore(so); 9166b8fda4dSGarrett Wollman unp_shutdown(unp); 917e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 918ede6e136SRobert Watson } 919e7c33e29SRobert Watson 920e7c33e29SRobert Watson if ((nam != NULL) || (flags & PRUS_EOF)) 921e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 922e7c33e29SRobert Watson else 923e7c33e29SRobert Watson UNP_GLOBAL_RUNLOCK(); 924df8bae1dSRodney W. Grimes 925fc3fcacfSRobert Watson if (control != NULL && error != 0) 926bd508d39SDon Lewis unp_dispose(control); 927bd508d39SDon Lewis 928a29f300eSGarrett Wollman release: 929fc3fcacfSRobert Watson if (control != NULL) 930a29f300eSGarrett Wollman m_freem(control); 931fc3fcacfSRobert Watson if (m != NULL) 932a29f300eSGarrett Wollman m_freem(m); 933e5aeaa0cSDag-Erling Smørgrav return (error); 934a29f300eSGarrett Wollman } 935df8bae1dSRodney W. Grimes 936a29f300eSGarrett Wollman static int 937a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 938a29f300eSGarrett Wollman { 939e7c33e29SRobert Watson struct unpcb *unp, *unp2; 940a29f300eSGarrett Wollman struct socket *so2; 941a29f300eSGarrett Wollman 94240f2ac28SRobert Watson unp = sotounpcb(so); 9434d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sense: unp == NULL")); 944e7c33e29SRobert Watson 945a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 946e7c33e29SRobert Watson UNP_GLOBAL_RLOCK(); 947e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 948e7c33e29SRobert Watson unp2 = unp->unp_conn; 949e7c33e29SRobert Watson if (so->so_type == SOCK_STREAM && unp2 != NULL) { 950e7c33e29SRobert Watson so2 = unp2->unp_socket; 951a29f300eSGarrett Wollman sb->st_blksize += so2->so_rcv.sb_cc; 952df8bae1dSRodney W. Grimes } 953f3732fd1SPoul-Henning Kamp sb->st_dev = NODEV; 954df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 9556f782c46SJeffrey Hsu unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 956a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 957e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 958e7c33e29SRobert Watson UNP_GLOBAL_RUNLOCK(); 959df8bae1dSRodney W. Grimes return (0); 960a29f300eSGarrett Wollman } 961df8bae1dSRodney W. Grimes 962a29f300eSGarrett Wollman static int 963a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 964a29f300eSGarrett Wollman { 96540f2ac28SRobert Watson struct unpcb *unp; 966df8bae1dSRodney W. Grimes 96740f2ac28SRobert Watson unp = sotounpcb(so); 9684d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_shutdown: unp == NULL")); 969e7c33e29SRobert Watson 970e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 971e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 972a29f300eSGarrett Wollman socantsendmore(so); 973a29f300eSGarrett Wollman unp_shutdown(unp); 974e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 975e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 976e5aeaa0cSDag-Erling Smørgrav return (0); 977a29f300eSGarrett Wollman } 978df8bae1dSRodney W. Grimes 979a29f300eSGarrett Wollman static int 98057bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 981a29f300eSGarrett Wollman { 98240f2ac28SRobert Watson struct unpcb *unp; 9830d9ce3a1SRobert Watson const struct sockaddr *sa; 984a29f300eSGarrett Wollman 9854d4b555eSRobert Watson unp = sotounpcb(so); 9864d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL")); 987e7c33e29SRobert Watson 9880d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 989e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 990fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 9910d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_addr; 99283f3198bSThomas Moestl else 9930d9ce3a1SRobert Watson sa = &sun_noname; 9940d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 995e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 996e5aeaa0cSDag-Erling Smørgrav return (0); 997df8bae1dSRodney W. Grimes } 998a29f300eSGarrett Wollman 999a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = { 1000756d52a1SPoul-Henning Kamp .pru_abort = uipc_abort, 1001756d52a1SPoul-Henning Kamp .pru_accept = uipc_accept, 1002756d52a1SPoul-Henning Kamp .pru_attach = uipc_attach, 1003756d52a1SPoul-Henning Kamp .pru_bind = uipc_bind, 1004756d52a1SPoul-Henning Kamp .pru_connect = uipc_connect, 1005756d52a1SPoul-Henning Kamp .pru_connect2 = uipc_connect2, 1006756d52a1SPoul-Henning Kamp .pru_detach = uipc_detach, 1007756d52a1SPoul-Henning Kamp .pru_disconnect = uipc_disconnect, 1008756d52a1SPoul-Henning Kamp .pru_listen = uipc_listen, 1009756d52a1SPoul-Henning Kamp .pru_peeraddr = uipc_peeraddr, 1010756d52a1SPoul-Henning Kamp .pru_rcvd = uipc_rcvd, 1011756d52a1SPoul-Henning Kamp .pru_send = uipc_send, 1012756d52a1SPoul-Henning Kamp .pru_sense = uipc_sense, 1013756d52a1SPoul-Henning Kamp .pru_shutdown = uipc_shutdown, 1014756d52a1SPoul-Henning Kamp .pru_sockaddr = uipc_sockaddr, 1015a152f8a3SRobert Watson .pru_close = uipc_close, 1016a29f300eSGarrett Wollman }; 1017df8bae1dSRodney W. Grimes 10180c1bb4fbSDima Dorfman int 1019892af6b9SRobert Watson uipc_ctloutput(struct socket *so, struct sockopt *sopt) 10200c1bb4fbSDima Dorfman { 102140f2ac28SRobert Watson struct unpcb *unp; 10220d9ce3a1SRobert Watson struct xucred xu; 10236a2989fdSMatthew N. Dodd int error, optval; 10246a2989fdSMatthew N. Dodd 102596a041b5SMatthew N. Dodd if (sopt->sopt_level != 0) 102696a041b5SMatthew N. Dodd return (EINVAL); 102796a041b5SMatthew N. Dodd 10286a2989fdSMatthew N. Dodd unp = sotounpcb(so); 10294d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL")); 10306a2989fdSMatthew N. Dodd error = 0; 10310c1bb4fbSDima Dorfman switch (sopt->sopt_dir) { 10320c1bb4fbSDima Dorfman case SOPT_GET: 10330c1bb4fbSDima Dorfman switch (sopt->sopt_name) { 10340c1bb4fbSDima Dorfman case LOCAL_PEERCRED: 1035e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 10360c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC) 10370d9ce3a1SRobert Watson xu = unp->unp_peercred; 10380c1bb4fbSDima Dorfman else { 10390c1bb4fbSDima Dorfman if (so->so_type == SOCK_STREAM) 10400c1bb4fbSDima Dorfman error = ENOTCONN; 10410c1bb4fbSDima Dorfman else 10420c1bb4fbSDima Dorfman error = EINVAL; 10430c1bb4fbSDima Dorfman } 1044e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 10450d9ce3a1SRobert Watson if (error == 0) 10460d9ce3a1SRobert Watson error = sooptcopyout(sopt, &xu, sizeof(xu)); 10470c1bb4fbSDima Dorfman break; 1048e7c33e29SRobert Watson 10496a2989fdSMatthew N. Dodd case LOCAL_CREDS: 10501f837c47SRobert Watson /* Unocked read. */ 10516a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0; 10526a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 10536a2989fdSMatthew N. Dodd break; 1054e7c33e29SRobert Watson 10556a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 10561f837c47SRobert Watson /* Unocked read. */ 10576a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0; 10586a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 10596a2989fdSMatthew N. Dodd break; 1060e7c33e29SRobert Watson 10610c1bb4fbSDima Dorfman default: 10620c1bb4fbSDima Dorfman error = EOPNOTSUPP; 10630c1bb4fbSDima Dorfman break; 10640c1bb4fbSDima Dorfman } 10650c1bb4fbSDima Dorfman break; 1066e7c33e29SRobert Watson 10670c1bb4fbSDima Dorfman case SOPT_SET: 10686a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 10696a2989fdSMatthew N. Dodd case LOCAL_CREDS: 10706a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 10716a2989fdSMatthew N. Dodd error = sooptcopyin(sopt, &optval, sizeof(optval), 10726a2989fdSMatthew N. Dodd sizeof(optval)); 10736a2989fdSMatthew N. Dodd if (error) 10746a2989fdSMatthew N. Dodd break; 10756a2989fdSMatthew N. Dodd 1076e7c33e29SRobert Watson #define OPTSET(bit) do { \ 1077e7c33e29SRobert Watson UNP_PCB_LOCK(unp); \ 10786a2989fdSMatthew N. Dodd if (optval) \ 10796a2989fdSMatthew N. Dodd unp->unp_flags |= bit; \ 10806a2989fdSMatthew N. Dodd else \ 1081e7c33e29SRobert Watson unp->unp_flags &= ~bit; \ 1082e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); \ 1083e7c33e29SRobert Watson } while (0) 10846a2989fdSMatthew N. Dodd 10856a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 10866a2989fdSMatthew N. Dodd case LOCAL_CREDS: 10876a2989fdSMatthew N. Dodd OPTSET(UNP_WANTCRED); 10886a2989fdSMatthew N. Dodd break; 1089e7c33e29SRobert Watson 10906a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 10916a2989fdSMatthew N. Dodd OPTSET(UNP_CONNWAIT); 10926a2989fdSMatthew N. Dodd break; 1093e7c33e29SRobert Watson 10946a2989fdSMatthew N. Dodd default: 10956a2989fdSMatthew N. Dodd break; 10966a2989fdSMatthew N. Dodd } 10976a2989fdSMatthew N. Dodd break; 10986a2989fdSMatthew N. Dodd #undef OPTSET 10996a2989fdSMatthew N. Dodd default: 11006a2989fdSMatthew N. Dodd error = ENOPROTOOPT; 11016a2989fdSMatthew N. Dodd break; 11026a2989fdSMatthew N. Dodd } 1103abb886faSMatthew N. Dodd break; 1104e7c33e29SRobert Watson 11050c1bb4fbSDima Dorfman default: 11060c1bb4fbSDima Dorfman error = EOPNOTSUPP; 11070c1bb4fbSDima Dorfman break; 11080c1bb4fbSDima Dorfman } 11090c1bb4fbSDima Dorfman return (error); 11100c1bb4fbSDima Dorfman } 11110c1bb4fbSDima Dorfman 1112f708ef1bSPoul-Henning Kamp static int 1113892af6b9SRobert Watson unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1114df8bae1dSRodney W. Grimes { 1115892af6b9SRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam; 1116892af6b9SRobert Watson struct vnode *vp; 1117892af6b9SRobert Watson struct socket *so2, *so3; 1118b295bdcdSRobert Watson struct unpcb *unp, *unp2, *unp3; 11199e289446SWojciech A. Koszek int error, len, vfslocked; 1120df8bae1dSRodney W. Grimes struct nameidata nd; 112157bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 11220d9ce3a1SRobert Watson struct sockaddr *sa; 11230d9ce3a1SRobert Watson 1124e7c33e29SRobert Watson UNP_GLOBAL_WLOCK_ASSERT(); 1125e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 1126df8bae1dSRodney W. Grimes 11274d4b555eSRobert Watson unp = sotounpcb(so); 11284d4b555eSRobert Watson KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 1129e7c33e29SRobert Watson 113057bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 113157bf258eSGarrett Wollman if (len <= 0) 1132e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 113355c85568SRobert Drehmel strlcpy(buf, soun->sun_path, len + 1); 1134e7c33e29SRobert Watson 1135e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 11364f1f0ef5SRobert Watson if (unp->unp_flags & UNP_CONNECTING) { 1137e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 11384f1f0ef5SRobert Watson return (EALREADY); 11394f1f0ef5SRobert Watson } 114005102f04SRobert Watson unp->unp_flags |= UNP_CONNECTING; 1141e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1142e7c33e29SRobert Watson 11430d9ce3a1SRobert Watson sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 11449e289446SWojciech A. Koszek NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, 11459e289446SWojciech A. Koszek td); 1146797f2d22SPoul-Henning Kamp error = namei(&nd); 1147797f2d22SPoul-Henning Kamp if (error) 11480d9ce3a1SRobert Watson vp = NULL; 11490d9ce3a1SRobert Watson else 1150df8bae1dSRodney W. Grimes vp = nd.ni_vp; 11510d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_connect"); 11529e289446SWojciech A. Koszek vfslocked = NDHASGIANT(&nd); 1153762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 11540d9ce3a1SRobert Watson if (error) 11550d9ce3a1SRobert Watson goto bad; 11560d9ce3a1SRobert Watson 1157df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 1158df8bae1dSRodney W. Grimes error = ENOTSOCK; 1159df8bae1dSRodney W. Grimes goto bad; 1160df8bae1dSRodney W. Grimes } 11616fac927cSRobert Watson #ifdef MAC 11626fac927cSRobert Watson error = mac_check_vnode_open(td->td_ucred, vp, VWRITE | VREAD); 11636fac927cSRobert Watson if (error) 11646fac927cSRobert Watson goto bad; 11656fac927cSRobert Watson #endif 1166a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 1167797f2d22SPoul-Henning Kamp if (error) 1168df8bae1dSRodney W. Grimes goto bad; 11699e289446SWojciech A. Koszek VFS_UNLOCK_GIANT(vfslocked); 1170e7c33e29SRobert Watson 1171b295bdcdSRobert Watson unp = sotounpcb(so); 11724d4b555eSRobert Watson KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 1173e7c33e29SRobert Watson 1174e7c33e29SRobert Watson /* 1175e7c33e29SRobert Watson * Lock global lock for two reasons: make sure v_socket is stable, 1176e7c33e29SRobert Watson * and to protect simultaneous locking of multiple pcbs. 1177e7c33e29SRobert Watson */ 1178e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 1179df8bae1dSRodney W. Grimes so2 = vp->v_socket; 1180fc3fcacfSRobert Watson if (so2 == NULL) { 1181df8bae1dSRodney W. Grimes error = ECONNREFUSED; 11822260c03dSRobert Watson goto bad2; 1183df8bae1dSRodney W. Grimes } 1184df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 1185df8bae1dSRodney W. Grimes error = EPROTOTYPE; 11862260c03dSRobert Watson goto bad2; 1187df8bae1dSRodney W. Grimes } 1188df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 1189e7c33e29SRobert Watson if (so2->so_options & SO_ACCEPTCONN) { 1190e7c33e29SRobert Watson /* 1191e7c33e29SRobert Watson * We can't drop the global lock here or 'so2' may 1192d7924b70SRobert Watson * become invalid. As a result, we need to handle 1193d7924b70SRobert Watson * possibly lock recursion in uipc_attach. 1194e7c33e29SRobert Watson */ 11950d9ce3a1SRobert Watson so3 = sonewconn(so2, 0); 1196e7c33e29SRobert Watson } else 11970d9ce3a1SRobert Watson so3 = NULL; 11980d9ce3a1SRobert Watson if (so3 == NULL) { 1199df8bae1dSRodney W. Grimes error = ECONNREFUSED; 12000d9ce3a1SRobert Watson goto bad2; 1201df8bae1dSRodney W. Grimes } 12020c1bb4fbSDima Dorfman unp = sotounpcb(so); 1203df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 1204df8bae1dSRodney W. Grimes unp3 = sotounpcb(so3); 1205e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 1206e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 1207e7c33e29SRobert Watson UNP_PCB_LOCK(unp3); 12080d9ce3a1SRobert Watson if (unp2->unp_addr != NULL) { 12090d9ce3a1SRobert Watson bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 12100d9ce3a1SRobert Watson unp3->unp_addr = (struct sockaddr_un *) sa; 12110d9ce3a1SRobert Watson sa = NULL; 12120d9ce3a1SRobert Watson } 12130c1bb4fbSDima Dorfman /* 12140c1bb4fbSDima Dorfman * unp_peercred management: 12150c1bb4fbSDima Dorfman * 12161c381b19SRobert Watson * The connecter's (client's) credentials are copied from its 12171c381b19SRobert Watson * process structure at the time of connect() (which is now). 12180c1bb4fbSDima Dorfman */ 1219a854ed98SJohn Baldwin cru2x(td->td_ucred, &unp3->unp_peercred); 12200c1bb4fbSDima Dorfman unp3->unp_flags |= UNP_HAVEPC; 12210c1bb4fbSDima Dorfman /* 12221c381b19SRobert Watson * The receiver's (server's) credentials are copied from the 12231c381b19SRobert Watson * unp_peercred member of socket on which the former called 1224e7c33e29SRobert Watson * listen(); uipc_listen() cached that process's credentials 12251c381b19SRobert Watson * at that time so we can use them now. 12260c1bb4fbSDima Dorfman */ 12270c1bb4fbSDima Dorfman KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 12280c1bb4fbSDima Dorfman ("unp_connect: listener without cached peercred")); 12290c1bb4fbSDima Dorfman memcpy(&unp->unp_peercred, &unp2->unp_peercred, 12300c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 12310c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPC; 1232481f8fe8SMaxim Konovalov if (unp2->unp_flags & UNP_WANTCRED) 1233481f8fe8SMaxim Konovalov unp3->unp_flags |= UNP_WANTCRED; 1234e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp3); 1235e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1236e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1237335654d7SRobert Watson #ifdef MAC 1238310e7cebSRobert Watson SOCK_LOCK(so); 1239335654d7SRobert Watson mac_set_socket_peer_from_socket(so, so3); 1240335654d7SRobert Watson mac_set_socket_peer_from_socket(so3, so); 1241310e7cebSRobert Watson SOCK_UNLOCK(so); 1242335654d7SRobert Watson #endif 12430c1bb4fbSDima Dorfman 1244df8bae1dSRodney W. Grimes so2 = so3; 1245df8bae1dSRodney W. Grimes } 1246e7c33e29SRobert Watson unp = sotounpcb(so); 1247e7c33e29SRobert Watson KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 1248e7c33e29SRobert Watson unp2 = sotounpcb(so2); 1249e7c33e29SRobert Watson KASSERT(unp2 != NULL, ("unp_connect: unp2 == NULL")); 1250e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 1251e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 12526a2989fdSMatthew N. Dodd error = unp_connect2(so, so2, PRU_CONNECT); 1253e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1254e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 12550d9ce3a1SRobert Watson bad2: 1256e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 12579e289446SWojciech A. Koszek if (vfslocked) 12589e289446SWojciech A. Koszek /* 12599e289446SWojciech A. Koszek * Giant has been previously acquired. This means filesystem 12609e289446SWojciech A. Koszek * isn't MPSAFE. Do it once again. 12619e289446SWojciech A. Koszek */ 12620d9ce3a1SRobert Watson mtx_lock(&Giant); 1263df8bae1dSRodney W. Grimes bad: 12640d9ce3a1SRobert Watson if (vp != NULL) 1265df8bae1dSRodney W. Grimes vput(vp); 12669e289446SWojciech A. Koszek VFS_UNLOCK_GIANT(vfslocked); 12670d9ce3a1SRobert Watson free(sa, M_SONAME); 1268e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 1269e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 12704f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_CONNECTING; 1271e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1272df8bae1dSRodney W. Grimes return (error); 1273df8bae1dSRodney W. Grimes } 1274df8bae1dSRodney W. Grimes 1275db48c0d2SRobert Watson static int 12766a2989fdSMatthew N. Dodd unp_connect2(struct socket *so, struct socket *so2, int req) 1277df8bae1dSRodney W. Grimes { 1278e7c33e29SRobert Watson struct unpcb *unp; 1279892af6b9SRobert Watson struct unpcb *unp2; 1280df8bae1dSRodney W. Grimes 1281e7c33e29SRobert Watson unp = sotounpcb(so); 1282e7c33e29SRobert Watson KASSERT(unp != NULL, ("unp_connect2: unp == NULL")); 1283e7c33e29SRobert Watson unp2 = sotounpcb(so2); 1284e7c33e29SRobert Watson KASSERT(unp2 != NULL, ("unp_connect2: unp2 == NULL")); 1285e7c33e29SRobert Watson 1286e7c33e29SRobert Watson UNP_GLOBAL_WLOCK_ASSERT(); 1287e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 1288e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp2); 12890d9ce3a1SRobert Watson 1290df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 1291df8bae1dSRodney W. Grimes return (EPROTOTYPE); 1292df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 1293e7c33e29SRobert Watson 1294df8bae1dSRodney W. Grimes switch (so->so_type) { 1295df8bae1dSRodney W. Grimes case SOCK_DGRAM: 129698271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 1297df8bae1dSRodney W. Grimes soisconnected(so); 1298df8bae1dSRodney W. Grimes break; 1299df8bae1dSRodney W. Grimes 1300df8bae1dSRodney W. Grimes case SOCK_STREAM: 1301df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 13026a2989fdSMatthew N. Dodd if (req == PRU_CONNECT && 13036a2989fdSMatthew N. Dodd ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT)) 13046a2989fdSMatthew N. Dodd soisconnecting(so); 13056a2989fdSMatthew N. Dodd else 1306df8bae1dSRodney W. Grimes soisconnected(so); 1307df8bae1dSRodney W. Grimes soisconnected(so2); 1308df8bae1dSRodney W. Grimes break; 1309df8bae1dSRodney W. Grimes 1310df8bae1dSRodney W. Grimes default: 1311df8bae1dSRodney W. Grimes panic("unp_connect2"); 1312df8bae1dSRodney W. Grimes } 1313df8bae1dSRodney W. Grimes return (0); 1314df8bae1dSRodney W. Grimes } 1315df8bae1dSRodney W. Grimes 1316f708ef1bSPoul-Henning Kamp static void 1317e7c33e29SRobert Watson unp_disconnect(struct unpcb *unp, struct unpcb *unp2) 1318df8bae1dSRodney W. Grimes { 13191b2e3b4bSRobert Watson struct socket *so; 1320df8bae1dSRodney W. Grimes 1321e7c33e29SRobert Watson KASSERT(unp2 != NULL, ("unp_disconnect: unp2 == NULL")); 13220d9ce3a1SRobert Watson 1323e7c33e29SRobert Watson UNP_GLOBAL_WLOCK_ASSERT(); 1324e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 1325e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp2); 1326e7c33e29SRobert Watson 1327fc3fcacfSRobert Watson unp->unp_conn = NULL; 1328df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 1329df8bae1dSRodney W. Grimes case SOCK_DGRAM: 133098271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 13311b2e3b4bSRobert Watson so = unp->unp_socket; 13321b2e3b4bSRobert Watson SOCK_LOCK(so); 13331b2e3b4bSRobert Watson so->so_state &= ~SS_ISCONNECTED; 13341b2e3b4bSRobert Watson SOCK_UNLOCK(so); 1335df8bae1dSRodney W. Grimes break; 1336df8bae1dSRodney W. Grimes 1337df8bae1dSRodney W. Grimes case SOCK_STREAM: 1338df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 1339fc3fcacfSRobert Watson unp2->unp_conn = NULL; 1340df8bae1dSRodney W. Grimes soisdisconnected(unp2->unp_socket); 1341df8bae1dSRodney W. Grimes break; 1342df8bae1dSRodney W. Grimes } 1343df8bae1dSRodney W. Grimes } 1344df8bae1dSRodney W. Grimes 13450d9ce3a1SRobert Watson /* 1346d7924b70SRobert Watson * unp_pcblist() walks the global list of struct unpcb's to generate a 1347d7924b70SRobert Watson * pointer list, bumping the refcount on each unpcb. It then copies them out 1348d7924b70SRobert Watson * sequentially, validating the generation number on each to see if it has 1349d7924b70SRobert Watson * been detached. All of this is necessary because copyout() may sleep on 1350d7924b70SRobert Watson * disk I/O. 13510d9ce3a1SRobert Watson */ 135298271db4SGarrett Wollman static int 135382d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 135498271db4SGarrett Wollman { 1355f5ef029eSPoul-Henning Kamp int error, i, n; 13569ae328fcSJohn Baldwin int freeunp; 135798271db4SGarrett Wollman struct unpcb *unp, **unp_list; 135898271db4SGarrett Wollman unp_gen_t gencnt; 13598f364875SJulian Elischer struct xunpgen *xug; 136098271db4SGarrett Wollman struct unp_head *head; 13618f364875SJulian Elischer struct xunpcb *xu; 136298271db4SGarrett Wollman 1363a23d65bfSBruce Evans head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 136498271db4SGarrett Wollman 136598271db4SGarrett Wollman /* 136698271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 136798271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 136898271db4SGarrett Wollman */ 1369fc3fcacfSRobert Watson if (req->oldptr == NULL) { 137098271db4SGarrett Wollman n = unp_count; 13718f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug) 137298271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 1373e5aeaa0cSDag-Erling Smørgrav return (0); 137498271db4SGarrett Wollman } 137598271db4SGarrett Wollman 1376fc3fcacfSRobert Watson if (req->newptr != NULL) 1377e5aeaa0cSDag-Erling Smørgrav return (EPERM); 137898271db4SGarrett Wollman 137998271db4SGarrett Wollman /* 138098271db4SGarrett Wollman * OK, now we're committed to doing something. 138198271db4SGarrett Wollman */ 1382a163d034SWarner Losh xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 1383e7c33e29SRobert Watson UNP_GLOBAL_RLOCK(); 138498271db4SGarrett Wollman gencnt = unp_gencnt; 138598271db4SGarrett Wollman n = unp_count; 1386e7c33e29SRobert Watson UNP_GLOBAL_RUNLOCK(); 138798271db4SGarrett Wollman 13888f364875SJulian Elischer xug->xug_len = sizeof *xug; 13898f364875SJulian Elischer xug->xug_count = n; 13908f364875SJulian Elischer xug->xug_gen = gencnt; 13918f364875SJulian Elischer xug->xug_sogen = so_gencnt; 13928f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 13938f364875SJulian Elischer if (error) { 13948f364875SJulian Elischer free(xug, M_TEMP); 1395e5aeaa0cSDag-Erling Smørgrav return (error); 13968f364875SJulian Elischer } 139798271db4SGarrett Wollman 1398a163d034SWarner Losh unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 139998271db4SGarrett Wollman 1400e7c33e29SRobert Watson UNP_GLOBAL_RLOCK(); 14012e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 14022e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 1403e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 14048a7d8cc6SRobert Watson if (unp->unp_gencnt <= gencnt) { 1405a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred, 1406e7c33e29SRobert Watson unp->unp_socket->so_cred)) { 1407e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 14084787fd37SPaul Saab continue; 1409e7c33e29SRobert Watson } 141098271db4SGarrett Wollman unp_list[i++] = unp; 14119ae328fcSJohn Baldwin unp->unp_refcount++; 141298271db4SGarrett Wollman } 1413e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 14144787fd37SPaul Saab } 1415e7c33e29SRobert Watson UNP_GLOBAL_RUNLOCK(); 14161c381b19SRobert Watson n = i; /* In case we lost some during malloc. */ 141798271db4SGarrett Wollman 141898271db4SGarrett Wollman error = 0; 1419fe2eee82SColin Percival xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO); 142098271db4SGarrett Wollman for (i = 0; i < n; i++) { 142198271db4SGarrett Wollman unp = unp_list[i]; 1422e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 14239ae328fcSJohn Baldwin unp->unp_refcount--; 14249ae328fcSJohn Baldwin if (unp->unp_refcount != 0 && unp->unp_gencnt <= gencnt) { 14258f364875SJulian Elischer xu->xu_len = sizeof *xu; 14268f364875SJulian Elischer xu->xu_unpp = unp; 142798271db4SGarrett Wollman /* 142898271db4SGarrett Wollman * XXX - need more locking here to protect against 142998271db4SGarrett Wollman * connect/disconnect races for SMP. 143098271db4SGarrett Wollman */ 1431fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 14328f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr, 143398271db4SGarrett Wollman unp->unp_addr->sun_len); 1434fc3fcacfSRobert Watson if (unp->unp_conn != NULL && 1435fc3fcacfSRobert Watson unp->unp_conn->unp_addr != NULL) 143698271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 14378f364875SJulian Elischer &xu->xu_caddr, 143898271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 14398f364875SJulian Elischer bcopy(unp, &xu->xu_unp, sizeof *unp); 14408f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket); 1441e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 14428f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu); 14439ae328fcSJohn Baldwin } else { 14449ae328fcSJohn Baldwin freeunp = (unp->unp_refcount == 0); 1445e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1446e7c33e29SRobert Watson if (freeunp) { 1447e7c33e29SRobert Watson UNP_PCB_LOCK_DESTROY(unp); 14489ae328fcSJohn Baldwin uma_zfree(unp_zone, unp); 144998271db4SGarrett Wollman } 145098271db4SGarrett Wollman } 1451e7c33e29SRobert Watson } 14528f364875SJulian Elischer free(xu, M_TEMP); 145398271db4SGarrett Wollman if (!error) { 145498271db4SGarrett Wollman /* 14551c381b19SRobert Watson * Give the user an updated idea of our state. If the 14561c381b19SRobert Watson * generation differs from what we told her before, she knows 14571c381b19SRobert Watson * that something happened while we were processing this 14581c381b19SRobert Watson * request, and it might be necessary to retry. 145998271db4SGarrett Wollman */ 14608f364875SJulian Elischer xug->xug_gen = unp_gencnt; 14618f364875SJulian Elischer xug->xug_sogen = so_gencnt; 14628f364875SJulian Elischer xug->xug_count = unp_count; 14638f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 146498271db4SGarrett Wollman } 146598271db4SGarrett Wollman free(unp_list, M_TEMP); 14668f364875SJulian Elischer free(xug, M_TEMP); 1467e5aeaa0cSDag-Erling Smørgrav return (error); 146898271db4SGarrett Wollman } 146998271db4SGarrett Wollman 147098271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 147198271db4SGarrett Wollman (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 147298271db4SGarrett Wollman "List of active local datagram sockets"); 147398271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 147498271db4SGarrett Wollman (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 147598271db4SGarrett Wollman "List of active local stream sockets"); 147698271db4SGarrett Wollman 1477f708ef1bSPoul-Henning Kamp static void 1478892af6b9SRobert Watson unp_shutdown(struct unpcb *unp) 1479df8bae1dSRodney W. Grimes { 1480e7c33e29SRobert Watson struct unpcb *unp2; 1481df8bae1dSRodney W. Grimes struct socket *so; 1482df8bae1dSRodney W. Grimes 1483e7c33e29SRobert Watson UNP_GLOBAL_WLOCK_ASSERT(); 1484e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 14850d9ce3a1SRobert Watson 1486e7c33e29SRobert Watson unp2 = unp->unp_conn; 1487e7c33e29SRobert Watson if (unp->unp_socket->so_type == SOCK_STREAM && unp2 != NULL) { 1488e7c33e29SRobert Watson so = unp2->unp_socket; 1489e7c33e29SRobert Watson if (so != NULL) 1490df8bae1dSRodney W. Grimes socantrcvmore(so); 1491df8bae1dSRodney W. Grimes } 1492e7c33e29SRobert Watson } 1493df8bae1dSRodney W. Grimes 1494f708ef1bSPoul-Henning Kamp static void 1495892af6b9SRobert Watson unp_drop(struct unpcb *unp, int errno) 1496df8bae1dSRodney W. Grimes { 1497df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 1498e7c33e29SRobert Watson struct unpcb *unp2; 1499df8bae1dSRodney W. Grimes 1500e7c33e29SRobert Watson UNP_GLOBAL_WLOCK_ASSERT(); 1501e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 15020d9ce3a1SRobert Watson 1503df8bae1dSRodney W. Grimes so->so_error = errno; 1504e7c33e29SRobert Watson unp2 = unp->unp_conn; 1505e7c33e29SRobert Watson if (unp2 == NULL) 1506e7c33e29SRobert Watson return; 1507e7c33e29SRobert Watson 1508e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 1509e7c33e29SRobert Watson unp_disconnect(unp, unp2); 1510e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1511df8bae1dSRodney W. Grimes } 1512df8bae1dSRodney W. Grimes 15132bc21ed9SDavid Malone static void 1514892af6b9SRobert Watson unp_freerights(struct file **rp, int fdcount) 1515df8bae1dSRodney W. Grimes { 15162bc21ed9SDavid Malone int i; 15172bc21ed9SDavid Malone struct file *fp; 1518df8bae1dSRodney W. Grimes 15192bc21ed9SDavid Malone for (i = 0; i < fdcount; i++) { 15208692c025SYoshinobu Inoue /* 15211c381b19SRobert Watson * Zero the pointer before calling unp_discard since it may 15221c381b19SRobert Watson * end up in unp_gc().. 1523d7dca903SRobert Watson * 1524d7dca903SRobert Watson * XXXRW: This is less true than it used to be. 15258692c025SYoshinobu Inoue */ 1526e7c33e29SRobert Watson fp = *rp; 1527e7c33e29SRobert Watson *rp++ = NULL; 15288692c025SYoshinobu Inoue unp_discard(fp); 1529df8bae1dSRodney W. Grimes } 15302bc21ed9SDavid Malone } 15312bc21ed9SDavid Malone 15322bc21ed9SDavid Malone int 1533892af6b9SRobert Watson unp_externalize(struct mbuf *control, struct mbuf **controlp) 15342bc21ed9SDavid Malone { 15352bc21ed9SDavid Malone struct thread *td = curthread; /* XXX */ 15362bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 15372bc21ed9SDavid Malone int i; 15382bc21ed9SDavid Malone int *fdp; 15392bc21ed9SDavid Malone struct file **rp; 15402bc21ed9SDavid Malone struct file *fp; 15412bc21ed9SDavid Malone void *data; 15422bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 15432bc21ed9SDavid Malone int error, newfds; 15442bc21ed9SDavid Malone int f; 15452bc21ed9SDavid Malone u_int newlen; 15462bc21ed9SDavid Malone 1547e7c33e29SRobert Watson UNP_GLOBAL_UNLOCK_ASSERT(); 15484c5bc1caSRobert Watson 15492bc21ed9SDavid Malone error = 0; 15502bc21ed9SDavid Malone if (controlp != NULL) /* controlp == NULL => free control messages */ 15512bc21ed9SDavid Malone *controlp = NULL; 15522bc21ed9SDavid Malone 15532bc21ed9SDavid Malone while (cm != NULL) { 15542bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 15552bc21ed9SDavid Malone error = EINVAL; 15562bc21ed9SDavid Malone break; 15572bc21ed9SDavid Malone } 15582bc21ed9SDavid Malone 15592bc21ed9SDavid Malone data = CMSG_DATA(cm); 15602bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 15612bc21ed9SDavid Malone 15622bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET 15632bc21ed9SDavid Malone && cm->cmsg_type == SCM_RIGHTS) { 15642bc21ed9SDavid Malone newfds = datalen / sizeof(struct file *); 15652bc21ed9SDavid Malone rp = data; 15662bc21ed9SDavid Malone 1567e2f9a08bSOlivier Houchard /* If we're not outputting the descriptors free them. */ 15682bc21ed9SDavid Malone if (error || controlp == NULL) { 15692bc21ed9SDavid Malone unp_freerights(rp, newfds); 15702bc21ed9SDavid Malone goto next; 15712bc21ed9SDavid Malone } 15725e3f7694SRobert Watson FILEDESC_XLOCK(td->td_proc->p_fd); 15732bc21ed9SDavid Malone /* if the new FD's will not fit free them. */ 15742bc21ed9SDavid Malone if (!fdavail(td, newfds)) { 15755e3f7694SRobert Watson FILEDESC_XUNLOCK(td->td_proc->p_fd); 15762bc21ed9SDavid Malone error = EMSGSIZE; 15772bc21ed9SDavid Malone unp_freerights(rp, newfds); 15782bc21ed9SDavid Malone goto next; 1579df8bae1dSRodney W. Grimes } 1580ed5b7817SJulian Elischer /* 15811c381b19SRobert Watson * Now change each pointer to an fd in the global 15821c381b19SRobert Watson * table to an integer that is the index to the local 15831c381b19SRobert Watson * fd table entry that we set up to point to the 15841c381b19SRobert Watson * global one we are transferring. 1585ed5b7817SJulian Elischer */ 15862bc21ed9SDavid Malone newlen = newfds * sizeof(int); 15872bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 15882bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 15892bc21ed9SDavid Malone if (*controlp == NULL) { 15905e3f7694SRobert Watson FILEDESC_XUNLOCK(td->td_proc->p_fd); 15912bc21ed9SDavid Malone error = E2BIG; 15922bc21ed9SDavid Malone unp_freerights(rp, newfds); 15932bc21ed9SDavid Malone goto next; 15942bc21ed9SDavid Malone } 15952bc21ed9SDavid Malone 15962bc21ed9SDavid Malone fdp = (int *) 15972bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1598df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 1599a6d4491cSDag-Erling Smørgrav if (fdalloc(td, 0, &f)) 16002bc21ed9SDavid Malone panic("unp_externalize fdalloc failed"); 16018692c025SYoshinobu Inoue fp = *rp++; 1602b40ce416SJulian Elischer td->td_proc->p_fd->fd_ofiles[f] = fp; 1603426da3bcSAlfred Perlstein FILE_LOCK(fp); 1604df8bae1dSRodney W. Grimes fp->f_msgcount--; 1605426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1606df8bae1dSRodney W. Grimes unp_rights--; 16078692c025SYoshinobu Inoue *fdp++ = f; 1608df8bae1dSRodney W. Grimes } 16095e3f7694SRobert Watson FILEDESC_XUNLOCK(td->td_proc->p_fd); 16101c381b19SRobert Watson } else { 16111c381b19SRobert Watson /* We can just copy anything else across. */ 16122bc21ed9SDavid Malone if (error || controlp == NULL) 16132bc21ed9SDavid Malone goto next; 16142bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, datalen, 16152bc21ed9SDavid Malone cm->cmsg_type, cm->cmsg_level); 16162bc21ed9SDavid Malone if (*controlp == NULL) { 16172bc21ed9SDavid Malone error = ENOBUFS; 16182bc21ed9SDavid Malone goto next; 16192bc21ed9SDavid Malone } 16202bc21ed9SDavid Malone bcopy(data, 16212bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 16222bc21ed9SDavid Malone datalen); 16232bc21ed9SDavid Malone } 16242bc21ed9SDavid Malone 16252bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 16262bc21ed9SDavid Malone 16272bc21ed9SDavid Malone next: 16282bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 16292bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 16302bc21ed9SDavid Malone cm = (struct cmsghdr *) 16312bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 16328692c025SYoshinobu Inoue } else { 16332bc21ed9SDavid Malone clen = 0; 16342bc21ed9SDavid Malone cm = NULL; 16358692c025SYoshinobu Inoue } 16368692c025SYoshinobu Inoue } 16378692c025SYoshinobu Inoue 16382bc21ed9SDavid Malone m_freem(control); 16392bc21ed9SDavid Malone 16402bc21ed9SDavid Malone return (error); 1641df8bae1dSRodney W. Grimes } 1642df8bae1dSRodney W. Grimes 16434f590175SPaul Saab static void 16444f590175SPaul Saab unp_zone_change(void *tag) 16454f590175SPaul Saab { 16464f590175SPaul Saab 16474f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets); 16484f590175SPaul Saab } 16494f590175SPaul Saab 165098271db4SGarrett Wollman void 165198271db4SGarrett Wollman unp_init(void) 165298271db4SGarrett Wollman { 16531c381b19SRobert Watson 16549e9d298aSJeff Roberson unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 16559ae328fcSJohn Baldwin NULL, NULL, UMA_ALIGN_PTR, 0); 1656fc3fcacfSRobert Watson if (unp_zone == NULL) 165798271db4SGarrett Wollman panic("unp_init"); 16584f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets); 16594f590175SPaul Saab EVENTHANDLER_REGISTER(maxsockets_change, unp_zone_change, 16604f590175SPaul Saab NULL, EVENTHANDLER_PRI_ANY); 166198271db4SGarrett Wollman LIST_INIT(&unp_dhead); 166298271db4SGarrett Wollman LIST_INIT(&unp_shead); 1663a0ec558aSRobert Watson TASK_INIT(&unp_gc_task, 0, unp_gc, NULL); 1664e7c33e29SRobert Watson UNP_GLOBAL_LOCK_INIT(); 166598271db4SGarrett Wollman } 166698271db4SGarrett Wollman 1667f708ef1bSPoul-Henning Kamp static int 1668892af6b9SRobert Watson unp_internalize(struct mbuf **controlp, struct thread *td) 1669df8bae1dSRodney W. Grimes { 16702bc21ed9SDavid Malone struct mbuf *control = *controlp; 1671b40ce416SJulian Elischer struct proc *p = td->td_proc; 16728692c025SYoshinobu Inoue struct filedesc *fdescp = p->p_fd; 16732bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 16742bc21ed9SDavid Malone struct cmsgcred *cmcred; 16752bc21ed9SDavid Malone struct file **rp; 16762bc21ed9SDavid Malone struct file *fp; 16772bc21ed9SDavid Malone struct timeval *tv; 16782bc21ed9SDavid Malone int i, fd, *fdp; 16792bc21ed9SDavid Malone void *data; 16802bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 16812bc21ed9SDavid Malone int error, oldfds; 16828692c025SYoshinobu Inoue u_int newlen; 1683df8bae1dSRodney W. Grimes 1684e7c33e29SRobert Watson UNP_GLOBAL_UNLOCK_ASSERT(); 16854c5bc1caSRobert Watson 16862bc21ed9SDavid Malone error = 0; 16872bc21ed9SDavid Malone *controlp = NULL; 16880b788fa1SBill Paul 16892bc21ed9SDavid Malone while (cm != NULL) { 16902bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 16912bc21ed9SDavid Malone || cm->cmsg_len > clen) { 16922bc21ed9SDavid Malone error = EINVAL; 16932bc21ed9SDavid Malone goto out; 16942bc21ed9SDavid Malone } 16952bc21ed9SDavid Malone 16962bc21ed9SDavid Malone data = CMSG_DATA(cm); 16972bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 16982bc21ed9SDavid Malone 16992bc21ed9SDavid Malone switch (cm->cmsg_type) { 17000b788fa1SBill Paul /* 17010b788fa1SBill Paul * Fill in credential information. 17020b788fa1SBill Paul */ 17032bc21ed9SDavid Malone case SCM_CREDS: 17042bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 17052bc21ed9SDavid Malone SCM_CREDS, SOL_SOCKET); 17062bc21ed9SDavid Malone if (*controlp == NULL) { 17072bc21ed9SDavid Malone error = ENOBUFS; 17082bc21ed9SDavid Malone goto out; 17092bc21ed9SDavid Malone } 17102bc21ed9SDavid Malone 17112bc21ed9SDavid Malone cmcred = (struct cmsgcred *) 17122bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 17130b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 1714a854ed98SJohn Baldwin cmcred->cmcred_uid = td->td_ucred->cr_ruid; 1715a854ed98SJohn Baldwin cmcred->cmcred_gid = td->td_ucred->cr_rgid; 1716a854ed98SJohn Baldwin cmcred->cmcred_euid = td->td_ucred->cr_uid; 1717a854ed98SJohn Baldwin cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 17180b788fa1SBill Paul CMGROUP_MAX); 17190b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 17202bc21ed9SDavid Malone cmcred->cmcred_groups[i] = 1721a854ed98SJohn Baldwin td->td_ucred->cr_groups[i]; 17222bc21ed9SDavid Malone break; 17230b788fa1SBill Paul 17242bc21ed9SDavid Malone case SCM_RIGHTS: 17252bc21ed9SDavid Malone oldfds = datalen / sizeof (int); 1726ed5b7817SJulian Elischer /* 17271c381b19SRobert Watson * Check that all the FDs passed in refer to legal 17281c381b19SRobert Watson * files. If not, reject the entire operation. 1729ed5b7817SJulian Elischer */ 17302bc21ed9SDavid Malone fdp = data; 17315e3f7694SRobert Watson FILEDESC_SLOCK(fdescp); 1732df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 17338692c025SYoshinobu Inoue fd = *fdp++; 17348692c025SYoshinobu Inoue if ((unsigned)fd >= fdescp->fd_nfiles || 17352bc21ed9SDavid Malone fdescp->fd_ofiles[fd] == NULL) { 17365e3f7694SRobert Watson FILEDESC_SUNLOCK(fdescp); 17372bc21ed9SDavid Malone error = EBADF; 17382bc21ed9SDavid Malone goto out; 17392bc21ed9SDavid Malone } 1740e7d6662fSAlfred Perlstein fp = fdescp->fd_ofiles[fd]; 1741e7d6662fSAlfred Perlstein if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 17425e3f7694SRobert Watson FILEDESC_SUNLOCK(fdescp); 1743e7d6662fSAlfred Perlstein error = EOPNOTSUPP; 1744e7d6662fSAlfred Perlstein goto out; 1745e7d6662fSAlfred Perlstein } 1746e7d6662fSAlfred Perlstein 1747df8bae1dSRodney W. Grimes } 17485e3f7694SRobert Watson 1749ed5b7817SJulian Elischer /* 1750e7c33e29SRobert Watson * Now replace the integer FDs with pointers to 1751e7c33e29SRobert Watson * the associated global file table entry.. 1752ed5b7817SJulian Elischer */ 17532bc21ed9SDavid Malone newlen = oldfds * sizeof(struct file *); 17542bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 17552bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 17562bc21ed9SDavid Malone if (*controlp == NULL) { 17575e3f7694SRobert Watson FILEDESC_SUNLOCK(fdescp); 17582bc21ed9SDavid Malone error = E2BIG; 17592bc21ed9SDavid Malone goto out; 17608692c025SYoshinobu Inoue } 17618692c025SYoshinobu Inoue 17622bc21ed9SDavid Malone fdp = data; 17632bc21ed9SDavid Malone rp = (struct file **) 17642bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 17658692c025SYoshinobu Inoue for (i = 0; i < oldfds; i++) { 17668692c025SYoshinobu Inoue fp = fdescp->fd_ofiles[*fdp++]; 1767df8bae1dSRodney W. Grimes *rp++ = fp; 1768426da3bcSAlfred Perlstein FILE_LOCK(fp); 1769df8bae1dSRodney W. Grimes fp->f_count++; 1770df8bae1dSRodney W. Grimes fp->f_msgcount++; 1771426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1772df8bae1dSRodney W. Grimes unp_rights++; 1773df8bae1dSRodney W. Grimes } 17745e3f7694SRobert Watson FILEDESC_SUNLOCK(fdescp); 17752bc21ed9SDavid Malone break; 17762bc21ed9SDavid Malone 17772bc21ed9SDavid Malone case SCM_TIMESTAMP: 17782bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*tv), 17792bc21ed9SDavid Malone SCM_TIMESTAMP, SOL_SOCKET); 17802bc21ed9SDavid Malone if (*controlp == NULL) { 17812bc21ed9SDavid Malone error = ENOBUFS; 17822bc21ed9SDavid Malone goto out; 17838692c025SYoshinobu Inoue } 17842bc21ed9SDavid Malone tv = (struct timeval *) 17852bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 17862bc21ed9SDavid Malone microtime(tv); 17872bc21ed9SDavid Malone break; 17882bc21ed9SDavid Malone 17892bc21ed9SDavid Malone default: 17902bc21ed9SDavid Malone error = EINVAL; 17912bc21ed9SDavid Malone goto out; 17922bc21ed9SDavid Malone } 17932bc21ed9SDavid Malone 17942bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 17952bc21ed9SDavid Malone 17962bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 17972bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 17982bc21ed9SDavid Malone cm = (struct cmsghdr *) 17992bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 18002bc21ed9SDavid Malone } else { 18012bc21ed9SDavid Malone clen = 0; 18022bc21ed9SDavid Malone cm = NULL; 18032bc21ed9SDavid Malone } 18042bc21ed9SDavid Malone } 18052bc21ed9SDavid Malone 18062bc21ed9SDavid Malone out: 18072bc21ed9SDavid Malone m_freem(control); 18082bc21ed9SDavid Malone 18092bc21ed9SDavid Malone return (error); 1810df8bae1dSRodney W. Grimes } 1811df8bae1dSRodney W. Grimes 18125b950deaSRobert Watson static struct mbuf * 18136a2989fdSMatthew N. Dodd unp_addsockcred(struct thread *td, struct mbuf *control) 18146a2989fdSMatthew N. Dodd { 181570df31f4SMaxim Konovalov struct mbuf *m, *n, *n_prev; 18166a2989fdSMatthew N. Dodd struct sockcred *sc; 181770df31f4SMaxim Konovalov const struct cmsghdr *cm; 18186a2989fdSMatthew N. Dodd int ngroups; 18196a2989fdSMatthew N. Dodd int i; 18206a2989fdSMatthew N. Dodd 18216a2989fdSMatthew N. Dodd ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX); 18226a2989fdSMatthew N. Dodd 18236a2989fdSMatthew N. Dodd m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET); 18246a2989fdSMatthew N. Dodd if (m == NULL) 18256a2989fdSMatthew N. Dodd return (control); 18266a2989fdSMatthew N. Dodd 18276a2989fdSMatthew N. Dodd sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *)); 18286a2989fdSMatthew N. Dodd sc->sc_uid = td->td_ucred->cr_ruid; 18296a2989fdSMatthew N. Dodd sc->sc_euid = td->td_ucred->cr_uid; 18306a2989fdSMatthew N. Dodd sc->sc_gid = td->td_ucred->cr_rgid; 18316a2989fdSMatthew N. Dodd sc->sc_egid = td->td_ucred->cr_gid; 18326a2989fdSMatthew N. Dodd sc->sc_ngroups = ngroups; 18336a2989fdSMatthew N. Dodd for (i = 0; i < sc->sc_ngroups; i++) 18346a2989fdSMatthew N. Dodd sc->sc_groups[i] = td->td_ucred->cr_groups[i]; 18356a2989fdSMatthew N. Dodd 18366a2989fdSMatthew N. Dodd /* 18371c381b19SRobert Watson * Unlink SCM_CREDS control messages (struct cmsgcred), since just 18381c381b19SRobert Watson * created SCM_CREDS control message (struct sockcred) has another 18391c381b19SRobert Watson * format. 18406a2989fdSMatthew N. Dodd */ 184170df31f4SMaxim Konovalov if (control != NULL) 184270df31f4SMaxim Konovalov for (n = control, n_prev = NULL; n != NULL;) { 184370df31f4SMaxim Konovalov cm = mtod(n, struct cmsghdr *); 184470df31f4SMaxim Konovalov if (cm->cmsg_level == SOL_SOCKET && 184570df31f4SMaxim Konovalov cm->cmsg_type == SCM_CREDS) { 184670df31f4SMaxim Konovalov if (n_prev == NULL) 184770df31f4SMaxim Konovalov control = n->m_next; 184870df31f4SMaxim Konovalov else 184970df31f4SMaxim Konovalov n_prev->m_next = n->m_next; 185070df31f4SMaxim Konovalov n = m_free(n); 185170df31f4SMaxim Konovalov } else { 185270df31f4SMaxim Konovalov n_prev = n; 185370df31f4SMaxim Konovalov n = n->m_next; 185470df31f4SMaxim Konovalov } 185570df31f4SMaxim Konovalov } 18566a2989fdSMatthew N. Dodd 185770df31f4SMaxim Konovalov /* Prepend it to the head. */ 185870df31f4SMaxim Konovalov m->m_next = control; 185970df31f4SMaxim Konovalov 186070df31f4SMaxim Konovalov return (m); 18616a2989fdSMatthew N. Dodd } 18626a2989fdSMatthew N. Dodd 1863161a0c7cSRobert Watson /* 1864a0ec558aSRobert Watson * unp_defer indicates whether additional work has been defered for a future 1865a0ec558aSRobert Watson * pass through unp_gc(). It is thread local and does not require explicit 1866a0ec558aSRobert Watson * synchronization. 1867161a0c7cSRobert Watson */ 1868a0ec558aSRobert Watson static int unp_defer; 1869a0ec558aSRobert Watson 1870a0ec558aSRobert Watson static int unp_taskcount; 1871a0ec558aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0, ""); 1872a0ec558aSRobert Watson 1873a0ec558aSRobert Watson static int unp_recycled; 1874a0ec558aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0, ""); 1875df8bae1dSRodney W. Grimes 1876f708ef1bSPoul-Henning Kamp static void 1877a0ec558aSRobert Watson unp_gc(__unused void *arg, int pending) 1878df8bae1dSRodney W. Grimes { 1879892af6b9SRobert Watson struct file *fp, *nextfp; 1880892af6b9SRobert Watson struct socket *so; 1881df8bae1dSRodney W. Grimes struct file **extra_ref, **fpp; 1882df8bae1dSRodney W. Grimes int nunref, i; 188395f004dcSAlfred Perlstein int nfiles_snap; 188495f004dcSAlfred Perlstein int nfiles_slack = 20; 1885df8bae1dSRodney W. Grimes 1886a0ec558aSRobert Watson unp_taskcount++; 1887df8bae1dSRodney W. Grimes unp_defer = 0; 1888ed5b7817SJulian Elischer /* 18899ae328fcSJohn Baldwin * Before going through all this, set all FDs to be NOT deferred and 18901c381b19SRobert Watson * NOT externally accessible. 1891ed5b7817SJulian Elischer */ 1892426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 18932e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) 1894426da3bcSAlfred Perlstein fp->f_gcflag &= ~(FMARK|FDEFER); 1895df8bae1dSRodney W. Grimes do { 18965bb84bc8SRobert Watson KASSERT(unp_defer >= 0, ("unp_gc: unp_defer %d", unp_defer)); 18972e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) { 1898426da3bcSAlfred Perlstein FILE_LOCK(fp); 1899ed5b7817SJulian Elischer /* 1900a0ec558aSRobert Watson * If the file is not open, skip it -- could be a 1901a0ec558aSRobert Watson * file in the process of being opened, or in the 1902a0ec558aSRobert Watson * process of being closed. If the file is 1903a0ec558aSRobert Watson * "closing", it may have been marked for deferred 1904a0ec558aSRobert Watson * consideration. Clear the flag now if so. 1905ed5b7817SJulian Elischer */ 1906426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1907a0ec558aSRobert Watson if (fp->f_gcflag & FDEFER) 1908a0ec558aSRobert Watson unp_defer--; 1909a0ec558aSRobert Watson fp->f_gcflag &= ~(FMARK|FDEFER); 1910426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1911df8bae1dSRodney W. Grimes continue; 1912426da3bcSAlfred Perlstein } 1913ed5b7817SJulian Elischer /* 19149ae328fcSJohn Baldwin * If we already marked it as 'defer' in a 19159ae328fcSJohn Baldwin * previous pass, then try to process it this 19169ae328fcSJohn Baldwin * time and un-mark it. 1917ed5b7817SJulian Elischer */ 1918426da3bcSAlfred Perlstein if (fp->f_gcflag & FDEFER) { 1919426da3bcSAlfred Perlstein fp->f_gcflag &= ~FDEFER; 1920df8bae1dSRodney W. Grimes unp_defer--; 1921df8bae1dSRodney W. Grimes } else { 1922ed5b7817SJulian Elischer /* 1923e7c33e29SRobert Watson * If it's not deferred, then check if it's 1924ed5b7817SJulian Elischer * already marked.. if so skip it 1925ed5b7817SJulian Elischer */ 1926426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) { 1927426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1928df8bae1dSRodney W. Grimes continue; 1929426da3bcSAlfred Perlstein } 1930ed5b7817SJulian Elischer /* 19311c381b19SRobert Watson * If all references are from messages in 19321c381b19SRobert Watson * transit, then skip it. it's not externally 19331c381b19SRobert Watson * accessible. 1934ed5b7817SJulian Elischer */ 1935426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount) { 1936426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1937df8bae1dSRodney W. Grimes continue; 1938426da3bcSAlfred Perlstein } 1939ed5b7817SJulian Elischer /* 1940ed5b7817SJulian Elischer * If it got this far then it must be 1941ed5b7817SJulian Elischer * externally accessible. 1942ed5b7817SJulian Elischer */ 1943426da3bcSAlfred Perlstein fp->f_gcflag |= FMARK; 1944df8bae1dSRodney W. Grimes } 1945ed5b7817SJulian Elischer /* 19469ae328fcSJohn Baldwin * Either it was deferred, or it is externally 19471c381b19SRobert Watson * accessible and not already marked so. Now check 19481c381b19SRobert Watson * if it is possibly one of OUR sockets. 1949ed5b7817SJulian Elischer */ 1950df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_SOCKET || 195148e3128bSMatthew Dillon (so = fp->f_data) == NULL) { 1952426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1953df8bae1dSRodney W. Grimes continue; 1954426da3bcSAlfred Perlstein } 1955748e0b0aSGarrett Wollman if (so->so_proto->pr_domain != &localdomain || 19569ae328fcSJohn Baldwin (so->so_proto->pr_flags & PR_RIGHTS) == 0) { 19579ae328fcSJohn Baldwin FILE_UNLOCK(fp); 1958df8bae1dSRodney W. Grimes continue; 19599ae328fcSJohn Baldwin } 19609ae328fcSJohn Baldwin 19619ae328fcSJohn Baldwin /* 19629ae328fcSJohn Baldwin * Tell any other threads that do a subsequent 19639ae328fcSJohn Baldwin * fdrop() that we are scanning the message 19649ae328fcSJohn Baldwin * buffers. 19659ae328fcSJohn Baldwin */ 19669ae328fcSJohn Baldwin fp->f_gcflag |= FWAIT; 19679ae328fcSJohn Baldwin FILE_UNLOCK(fp); 19689ae328fcSJohn Baldwin 1969ed5b7817SJulian Elischer /* 19701c381b19SRobert Watson * So, Ok, it's one of our sockets and it IS 19719ae328fcSJohn Baldwin * externally accessible (or was deferred). Now we 19721c381b19SRobert Watson * look to see if we hold any file descriptors in its 1973ed5b7817SJulian Elischer * message buffers. Follow those links and mark them 1974ed5b7817SJulian Elischer * as accessible too. 1975ed5b7817SJulian Elischer */ 19767717cf07SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1977df8bae1dSRodney W. Grimes unp_scan(so->so_rcv.sb_mb, unp_mark); 19787717cf07SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 19799ae328fcSJohn Baldwin 19809ae328fcSJohn Baldwin /* 19819ae328fcSJohn Baldwin * Wake up any threads waiting in fdrop(). 19829ae328fcSJohn Baldwin */ 19839ae328fcSJohn Baldwin FILE_LOCK(fp); 19849ae328fcSJohn Baldwin fp->f_gcflag &= ~FWAIT; 19859ae328fcSJohn Baldwin wakeup(&fp->f_gcflag); 19869ae328fcSJohn Baldwin FILE_UNLOCK(fp); 1987df8bae1dSRodney W. Grimes } 1988df8bae1dSRodney W. Grimes } while (unp_defer); 1989426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1990df8bae1dSRodney W. Grimes /* 1991a0ec558aSRobert Watson * XXXRW: The following comments need updating for a post-SMPng and 1992a0ec558aSRobert Watson * deferred unp_gc() world, but are still generally accurate. 1993a0ec558aSRobert Watson * 19941c381b19SRobert Watson * We grab an extra reference to each of the file table entries that 19951c381b19SRobert Watson * are not otherwise accessible and then free the rights that are 19961c381b19SRobert Watson * stored in messages on them. 1997df8bae1dSRodney W. Grimes * 1998df8bae1dSRodney W. Grimes * The bug in the orginal code is a little tricky, so I'll describe 1999df8bae1dSRodney W. Grimes * what's wrong with it here. 2000df8bae1dSRodney W. Grimes * 2001df8bae1dSRodney W. Grimes * It is incorrect to simply unp_discard each entry for f_msgcount 2002df8bae1dSRodney W. Grimes * times -- consider the case of sockets A and B that contain 2003df8bae1dSRodney W. Grimes * references to each other. On a last close of some other socket, 2004df8bae1dSRodney W. Grimes * we trigger a gc since the number of outstanding rights (unp_rights) 2005a0ec558aSRobert Watson * is non-zero. If during the sweep phase the gc code unp_discards, 2006df8bae1dSRodney W. Grimes * we end up doing a (full) closef on the descriptor. A closef on A 2007df8bae1dSRodney W. Grimes * results in the following chain. Closef calls soo_close, which 2008df8bae1dSRodney W. Grimes * calls soclose. Soclose calls first (through the switch 2009df8bae1dSRodney W. Grimes * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 20101c381b19SRobert Watson * returns because the previous instance had set unp_gcing, and we 20111c381b19SRobert Watson * return all the way back to soclose, which marks the socket with 20121c381b19SRobert Watson * SS_NOFDREF, and then calls sofree. Sofree calls sorflush to free 20131c381b19SRobert Watson * up the rights that are queued in messages on the socket A, i.e., 20141c381b19SRobert Watson * the reference on B. The sorflush calls via the dom_dispose switch 20151c381b19SRobert Watson * unp_dispose, which unp_scans with unp_discard. This second 2016df8bae1dSRodney W. Grimes * instance of unp_discard just calls closef on B. 2017df8bae1dSRodney W. Grimes * 2018df8bae1dSRodney W. Grimes * Well, a similar chain occurs on B, resulting in a sorflush on B, 2019df8bae1dSRodney W. Grimes * which results in another closef on A. Unfortunately, A is already 2020df8bae1dSRodney W. Grimes * being closed, and the descriptor has already been marked with 2021df8bae1dSRodney W. Grimes * SS_NOFDREF, and soclose panics at this point. 2022df8bae1dSRodney W. Grimes * 2023df8bae1dSRodney W. Grimes * Here, we first take an extra reference to each inaccessible 20241c381b19SRobert Watson * descriptor. Then, we call sorflush ourself, since we know it is a 20251c381b19SRobert Watson * Unix domain socket anyhow. After we destroy all the rights 20261c381b19SRobert Watson * carried in messages, we do a last closef to get rid of our extra 20271c381b19SRobert Watson * reference. This is the last close, and the unp_detach etc will 20281c381b19SRobert Watson * shut down the socket. 2029df8bae1dSRodney W. Grimes * 2030df8bae1dSRodney W. Grimes * 91/09/19, bsy@cs.cmu.edu 2031df8bae1dSRodney W. Grimes */ 203295f004dcSAlfred Perlstein again: 2033e4643c73SPoul-Henning Kamp nfiles_snap = openfiles + nfiles_slack; /* some slack */ 203495f004dcSAlfred Perlstein extra_ref = malloc(nfiles_snap * sizeof(struct file *), M_TEMP, 203595f004dcSAlfred Perlstein M_WAITOK); 2036426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 2037e4643c73SPoul-Henning Kamp if (nfiles_snap < openfiles) { 203895f004dcSAlfred Perlstein sx_sunlock(&filelist_lock); 203995f004dcSAlfred Perlstein free(extra_ref, M_TEMP); 204095f004dcSAlfred Perlstein nfiles_slack += 20; 204195f004dcSAlfred Perlstein goto again; 204295f004dcSAlfred Perlstein } 2043fc3fcacfSRobert Watson for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; 2044fc3fcacfSRobert Watson fp != NULL; fp = nextfp) { 20452e3c8fcbSPoul-Henning Kamp nextfp = LIST_NEXT(fp, f_list); 2046426da3bcSAlfred Perlstein FILE_LOCK(fp); 2047ed5b7817SJulian Elischer /* 2048ed5b7817SJulian Elischer * If it's not open, skip it 2049ed5b7817SJulian Elischer */ 2050426da3bcSAlfred Perlstein if (fp->f_count == 0) { 2051426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 2052df8bae1dSRodney W. Grimes continue; 2053426da3bcSAlfred Perlstein } 2054ed5b7817SJulian Elischer /* 2055ed5b7817SJulian Elischer * If all refs are from msgs, and it's not marked accessible 20561c381b19SRobert Watson * then it must be referenced from some unreachable cycle of 20571c381b19SRobert Watson * (shut-down) FDs, so include it in our list of FDs to 20581c381b19SRobert Watson * remove. 2059ed5b7817SJulian Elischer */ 2060426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) { 2061df8bae1dSRodney W. Grimes *fpp++ = fp; 2062df8bae1dSRodney W. Grimes nunref++; 2063df8bae1dSRodney W. Grimes fp->f_count++; 2064df8bae1dSRodney W. Grimes } 2065426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 2066df8bae1dSRodney W. Grimes } 2067426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 2068ed5b7817SJulian Elischer /* 20691c381b19SRobert Watson * For each FD on our hit list, do the following two things: 2070ed5b7817SJulian Elischer */ 20711c7c3c6aSMatthew Dillon for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 20721c7c3c6aSMatthew Dillon struct file *tfp = *fpp; 2073426da3bcSAlfred Perlstein FILE_LOCK(tfp); 2074cd72f218SMatthew Dillon if (tfp->f_type == DTYPE_SOCKET && 207548e3128bSMatthew Dillon tfp->f_data != NULL) { 2076426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 207748e3128bSMatthew Dillon sorflush(tfp->f_data); 2078e5aeaa0cSDag-Erling Smørgrav } else { 2079426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 20801c7c3c6aSMatthew Dillon } 2081e5aeaa0cSDag-Erling Smørgrav } 2082a0ec558aSRobert Watson for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 2083b40ce416SJulian Elischer closef(*fpp, (struct thread *) NULL); 2084a0ec558aSRobert Watson unp_recycled++; 2085a0ec558aSRobert Watson } 2086210a5a71SAlfred Perlstein free(extra_ref, M_TEMP); 2087df8bae1dSRodney W. Grimes } 2088df8bae1dSRodney W. Grimes 208926f9a767SRodney W. Grimes void 2090892af6b9SRobert Watson unp_dispose(struct mbuf *m) 2091df8bae1dSRodney W. Grimes { 2092996c772fSJohn Dyson 2093df8bae1dSRodney W. Grimes if (m) 2094df8bae1dSRodney W. Grimes unp_scan(m, unp_discard); 2095df8bae1dSRodney W. Grimes } 2096df8bae1dSRodney W. Grimes 2097f708ef1bSPoul-Henning Kamp static void 2098892af6b9SRobert Watson unp_scan(struct mbuf *m0, void (*op)(struct file *)) 2099df8bae1dSRodney W. Grimes { 21002bc21ed9SDavid Malone struct mbuf *m; 21012bc21ed9SDavid Malone struct file **rp; 21022bc21ed9SDavid Malone struct cmsghdr *cm; 21032bc21ed9SDavid Malone void *data; 21042bc21ed9SDavid Malone int i; 21052bc21ed9SDavid Malone socklen_t clen, datalen; 2106df8bae1dSRodney W. Grimes int qfds; 2107df8bae1dSRodney W. Grimes 2108fc3fcacfSRobert Watson while (m0 != NULL) { 21092bc21ed9SDavid Malone for (m = m0; m; m = m->m_next) { 211012396bdcSDavid Malone if (m->m_type != MT_CONTROL) 2111df8bae1dSRodney W. Grimes continue; 21122bc21ed9SDavid Malone 21132bc21ed9SDavid Malone cm = mtod(m, struct cmsghdr *); 21142bc21ed9SDavid Malone clen = m->m_len; 21152bc21ed9SDavid Malone 21162bc21ed9SDavid Malone while (cm != NULL) { 21172bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) 21182bc21ed9SDavid Malone break; 21192bc21ed9SDavid Malone 21202bc21ed9SDavid Malone data = CMSG_DATA(cm); 21212bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len 21222bc21ed9SDavid Malone - (caddr_t)data; 21232bc21ed9SDavid Malone 21242bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET && 21252bc21ed9SDavid Malone cm->cmsg_type == SCM_RIGHTS) { 21262bc21ed9SDavid Malone qfds = datalen / sizeof (struct file *); 21272bc21ed9SDavid Malone rp = data; 2128df8bae1dSRodney W. Grimes for (i = 0; i < qfds; i++) 2129df8bae1dSRodney W. Grimes (*op)(*rp++); 21302bc21ed9SDavid Malone } 21312bc21ed9SDavid Malone 21322bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 21332bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 21342bc21ed9SDavid Malone cm = (struct cmsghdr *) 21352bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 21362bc21ed9SDavid Malone } else { 21372bc21ed9SDavid Malone clen = 0; 21382bc21ed9SDavid Malone cm = NULL; 21392bc21ed9SDavid Malone } 21402bc21ed9SDavid Malone } 2141df8bae1dSRodney W. Grimes } 2142df8bae1dSRodney W. Grimes m0 = m0->m_act; 2143df8bae1dSRodney W. Grimes } 2144df8bae1dSRodney W. Grimes } 2145df8bae1dSRodney W. Grimes 2146f708ef1bSPoul-Henning Kamp static void 2147892af6b9SRobert Watson unp_mark(struct file *fp) 2148df8bae1dSRodney W. Grimes { 2149e7c33e29SRobert Watson 2150e7c33e29SRobert Watson /* XXXRW: Should probably assert file list lock here. */ 2151e7c33e29SRobert Watson 2152426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) 2153df8bae1dSRodney W. Grimes return; 2154df8bae1dSRodney W. Grimes unp_defer++; 2155426da3bcSAlfred Perlstein fp->f_gcflag |= (FMARK|FDEFER); 2156df8bae1dSRodney W. Grimes } 2157df8bae1dSRodney W. Grimes 2158f708ef1bSPoul-Henning Kamp static void 2159892af6b9SRobert Watson unp_discard(struct file *fp) 2160df8bae1dSRodney W. Grimes { 2161e7c33e29SRobert Watson 2162e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 2163426da3bcSAlfred Perlstein FILE_LOCK(fp); 2164df8bae1dSRodney W. Grimes fp->f_msgcount--; 2165df8bae1dSRodney W. Grimes unp_rights--; 2166426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 2167e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 2168b40ce416SJulian Elischer (void) closef(fp, (struct thread *)NULL); 2169df8bae1dSRodney W. Grimes } 217003c96c31SRobert Watson 217103c96c31SRobert Watson #ifdef DDB 217203c96c31SRobert Watson static void 217303c96c31SRobert Watson db_print_indent(int indent) 217403c96c31SRobert Watson { 217503c96c31SRobert Watson int i; 217603c96c31SRobert Watson 217703c96c31SRobert Watson for (i = 0; i < indent; i++) 217803c96c31SRobert Watson db_printf(" "); 217903c96c31SRobert Watson } 218003c96c31SRobert Watson 218103c96c31SRobert Watson static void 218203c96c31SRobert Watson db_print_unpflags(int unp_flags) 218303c96c31SRobert Watson { 218403c96c31SRobert Watson int comma; 218503c96c31SRobert Watson 218603c96c31SRobert Watson comma = 0; 218703c96c31SRobert Watson if (unp_flags & UNP_HAVEPC) { 218803c96c31SRobert Watson db_printf("%sUNP_HAVEPC", comma ? ", " : ""); 218903c96c31SRobert Watson comma = 1; 219003c96c31SRobert Watson } 219103c96c31SRobert Watson if (unp_flags & UNP_HAVEPCCACHED) { 219203c96c31SRobert Watson db_printf("%sUNP_HAVEPCCACHED", comma ? ", " : ""); 219303c96c31SRobert Watson comma = 1; 219403c96c31SRobert Watson } 219503c96c31SRobert Watson if (unp_flags & UNP_WANTCRED) { 219603c96c31SRobert Watson db_printf("%sUNP_WANTCRED", comma ? ", " : ""); 219703c96c31SRobert Watson comma = 1; 219803c96c31SRobert Watson } 219903c96c31SRobert Watson if (unp_flags & UNP_CONNWAIT) { 220003c96c31SRobert Watson db_printf("%sUNP_CONNWAIT", comma ? ", " : ""); 220103c96c31SRobert Watson comma = 1; 220203c96c31SRobert Watson } 220303c96c31SRobert Watson if (unp_flags & UNP_CONNECTING) { 220403c96c31SRobert Watson db_printf("%sUNP_CONNECTING", comma ? ", " : ""); 220503c96c31SRobert Watson comma = 1; 220603c96c31SRobert Watson } 220703c96c31SRobert Watson if (unp_flags & UNP_BINDING) { 220803c96c31SRobert Watson db_printf("%sUNP_BINDING", comma ? ", " : ""); 220903c96c31SRobert Watson comma = 1; 221003c96c31SRobert Watson } 221103c96c31SRobert Watson } 221203c96c31SRobert Watson 221303c96c31SRobert Watson static void 221403c96c31SRobert Watson db_print_xucred(int indent, struct xucred *xu) 221503c96c31SRobert Watson { 221603c96c31SRobert Watson int comma, i; 221703c96c31SRobert Watson 221803c96c31SRobert Watson db_print_indent(indent); 221903c96c31SRobert Watson db_printf("cr_version: %u cr_uid: %u cr_ngroups: %d\n", 222003c96c31SRobert Watson xu->cr_version, xu->cr_uid, xu->cr_ngroups); 222103c96c31SRobert Watson db_print_indent(indent); 222203c96c31SRobert Watson db_printf("cr_groups: "); 222303c96c31SRobert Watson comma = 0; 222403c96c31SRobert Watson for (i = 0; i < xu->cr_ngroups; i++) { 222503c96c31SRobert Watson db_printf("%s%u", comma ? ", " : "", xu->cr_groups[i]); 222603c96c31SRobert Watson comma = 1; 222703c96c31SRobert Watson } 222803c96c31SRobert Watson db_printf("\n"); 222903c96c31SRobert Watson } 223003c96c31SRobert Watson 223103c96c31SRobert Watson static void 223203c96c31SRobert Watson db_print_unprefs(int indent, struct unp_head *uh) 223303c96c31SRobert Watson { 223403c96c31SRobert Watson struct unpcb *unp; 223503c96c31SRobert Watson int counter; 223603c96c31SRobert Watson 223703c96c31SRobert Watson counter = 0; 223803c96c31SRobert Watson LIST_FOREACH(unp, uh, unp_reflink) { 223903c96c31SRobert Watson if (counter % 4 == 0) 224003c96c31SRobert Watson db_print_indent(indent); 224103c96c31SRobert Watson db_printf("%p ", unp); 224203c96c31SRobert Watson if (counter % 4 == 3) 224303c96c31SRobert Watson db_printf("\n"); 224403c96c31SRobert Watson counter++; 224503c96c31SRobert Watson } 224603c96c31SRobert Watson if (counter != 0 && counter % 4 != 0) 224703c96c31SRobert Watson db_printf("\n"); 224803c96c31SRobert Watson } 224903c96c31SRobert Watson 225003c96c31SRobert Watson DB_SHOW_COMMAND(unpcb, db_show_unpcb) 225103c96c31SRobert Watson { 225203c96c31SRobert Watson struct unpcb *unp; 225303c96c31SRobert Watson 225403c96c31SRobert Watson if (!have_addr) { 225503c96c31SRobert Watson db_printf("usage: show unpcb <addr>\n"); 225603c96c31SRobert Watson return; 225703c96c31SRobert Watson } 225803c96c31SRobert Watson unp = (struct unpcb *)addr; 225903c96c31SRobert Watson 226003c96c31SRobert Watson db_printf("unp_socket: %p unp_vnode: %p\n", unp->unp_socket, 226103c96c31SRobert Watson unp->unp_vnode); 226203c96c31SRobert Watson 226303c96c31SRobert Watson db_printf("unp_ino: %d unp_conn: %p\n", unp->unp_ino, 226403c96c31SRobert Watson unp->unp_conn); 226503c96c31SRobert Watson 226603c96c31SRobert Watson db_printf("unp_refs:\n"); 226703c96c31SRobert Watson db_print_unprefs(2, &unp->unp_refs); 226803c96c31SRobert Watson 226903c96c31SRobert Watson /* XXXRW: Would be nice to print the full address, if any. */ 227003c96c31SRobert Watson db_printf("unp_addr: %p\n", unp->unp_addr); 227103c96c31SRobert Watson 227203c96c31SRobert Watson db_printf("unp_cc: %d unp_mbcnt: %d unp_gencnt: %llu\n", 227303c96c31SRobert Watson unp->unp_cc, unp->unp_mbcnt, 227403c96c31SRobert Watson (unsigned long long)unp->unp_gencnt); 227503c96c31SRobert Watson 227603c96c31SRobert Watson db_printf("unp_flags: %x (", unp->unp_flags); 227703c96c31SRobert Watson db_print_unpflags(unp->unp_flags); 227803c96c31SRobert Watson db_printf(")\n"); 227903c96c31SRobert Watson 228003c96c31SRobert Watson db_printf("unp_peercred:\n"); 228103c96c31SRobert Watson db_print_xucred(2, &unp->unp_peercred); 228203c96c31SRobert Watson 228303c96c31SRobert Watson db_printf("unp_refcount: %u\n", unp->unp_refcount); 228403c96c31SRobert Watson } 228503c96c31SRobert Watson #endif 2286