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 62335654d7SRobert Watson #include "opt_mac.h" 63335654d7SRobert Watson 64df8bae1dSRodney W. Grimes #include <sys/param.h> 65fb919e4dSMark Murray #include <sys/domain.h> 66960ed29cSSeigo Tanimura #include <sys/fcntl.h> 67d826c479SBruce Evans #include <sys/malloc.h> /* XXX must be before <sys/file.h> */ 684f590175SPaul Saab #include <sys/eventhandler.h> 69639acc13SGarrett Wollman #include <sys/file.h> 70960ed29cSSeigo Tanimura #include <sys/filedesc.h> 71960ed29cSSeigo Tanimura #include <sys/jail.h> 72960ed29cSSeigo Tanimura #include <sys/kernel.h> 73960ed29cSSeigo Tanimura #include <sys/lock.h> 74639acc13SGarrett Wollman #include <sys/mbuf.h> 75033eb86eSJeff Roberson #include <sys/mount.h> 76960ed29cSSeigo Tanimura #include <sys/mutex.h> 77639acc13SGarrett Wollman #include <sys/namei.h> 78639acc13SGarrett Wollman #include <sys/proc.h> 79df8bae1dSRodney W. Grimes #include <sys/protosw.h> 80960ed29cSSeigo Tanimura #include <sys/resourcevar.h> 81e7c33e29SRobert Watson #include <sys/rwlock.h> 82df8bae1dSRodney W. Grimes #include <sys/socket.h> 83df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 84960ed29cSSeigo Tanimura #include <sys/signalvar.h> 85df8bae1dSRodney W. Grimes #include <sys/stat.h> 86960ed29cSSeigo Tanimura #include <sys/sx.h> 87639acc13SGarrett Wollman #include <sys/sysctl.h> 88960ed29cSSeigo Tanimura #include <sys/systm.h> 89a0ec558aSRobert Watson #include <sys/taskqueue.h> 90639acc13SGarrett Wollman #include <sys/un.h> 9198271db4SGarrett Wollman #include <sys/unpcb.h> 92639acc13SGarrett Wollman #include <sys/vnode.h> 93df8bae1dSRodney W. Grimes 94aed55708SRobert Watson #include <security/mac/mac_framework.h> 95aed55708SRobert Watson 969e9d298aSJeff Roberson #include <vm/uma.h> 9798271db4SGarrett Wollman 989e9d298aSJeff Roberson static uma_zone_t unp_zone; 9998271db4SGarrett Wollman static unp_gen_t unp_gencnt; 100aea52f1bSRobert Watson static u_int unp_count; /* Count of local sockets. */ 101aea52f1bSRobert Watson static ino_t unp_ino; /* Prototype for fake inode numbers. */ 102aea52f1bSRobert Watson static int unp_rights; /* File descriptors in flight. */ 103aea52f1bSRobert Watson static struct unp_head unp_shead; /* List of local stream sockets. */ 104aea52f1bSRobert Watson static struct unp_head unp_dhead; /* List of local datagram sockets. */ 10598271db4SGarrett Wollman 106aea52f1bSRobert Watson static const struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 10798271db4SGarrett Wollman 108df8bae1dSRodney W. Grimes /* 109aea52f1bSRobert Watson * Garbage collection of cyclic file descriptor/socket references occurs 110aea52f1bSRobert Watson * asynchronously in a taskqueue context in order to avoid recursion and 111aea52f1bSRobert Watson * reentrance in the UNIX domain socket, file descriptor, and socket layer 112aea52f1bSRobert Watson * code. See unp_gc() for a full description. 113df8bae1dSRodney W. Grimes */ 114aea52f1bSRobert Watson static struct task unp_gc_task; 115f708ef1bSPoul-Henning Kamp 116ce5f32deSRobert Watson /* 1177e711c3aSRobert Watson * Both send and receive buffers are allocated PIPSIZ bytes of buffering for 1187e711c3aSRobert Watson * stream sockets, although the total for sender and receiver is actually 1197e711c3aSRobert Watson * only PIPSIZ. 1207e711c3aSRobert Watson * 1217e711c3aSRobert Watson * Datagram sockets really use the sendspace as the maximum datagram size, 1227e711c3aSRobert Watson * and don't really want to reserve the sendspace. Their recvspace should be 1237e711c3aSRobert Watson * large enough for at least one max-size datagram plus address. 1247e711c3aSRobert Watson */ 1257e711c3aSRobert Watson #ifndef PIPSIZ 1267e711c3aSRobert Watson #define PIPSIZ 8192 1277e711c3aSRobert Watson #endif 1287e711c3aSRobert Watson static u_long unpst_sendspace = PIPSIZ; 1297e711c3aSRobert Watson static u_long unpst_recvspace = PIPSIZ; 1307e711c3aSRobert Watson static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 1317e711c3aSRobert Watson static u_long unpdg_recvspace = 4*1024; 1327e711c3aSRobert Watson 133e4445a03SRobert Watson SYSCTL_NODE(_net, PF_LOCAL, local, CTLFLAG_RW, 0, "Local domain"); 134e4445a03SRobert Watson SYSCTL_NODE(_net_local, SOCK_STREAM, stream, CTLFLAG_RW, 0, "SOCK_STREAM"); 135e4445a03SRobert Watson SYSCTL_NODE(_net_local, SOCK_DGRAM, dgram, CTLFLAG_RW, 0, "SOCK_DGRAM"); 136e4445a03SRobert Watson 1377e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 1387e711c3aSRobert Watson &unpst_sendspace, 0, ""); 1397e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 1407e711c3aSRobert Watson &unpst_recvspace, 0, ""); 1417e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 1427e711c3aSRobert Watson &unpdg_sendspace, 0, ""); 1437e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 1447e711c3aSRobert Watson &unpdg_recvspace, 0, ""); 1457e711c3aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); 1467e711c3aSRobert Watson 147e7c33e29SRobert Watson /*- 148e7c33e29SRobert Watson * Locking and synchronization: 149ce5f32deSRobert Watson * 150e7c33e29SRobert Watson * The global UNIX domain socket rwlock (unp_global_rwlock) protects all 151e7c33e29SRobert Watson * global variables, including the linked lists tracking the set of allocated 152e7c33e29SRobert Watson * UNIX domain sockets. The global rwlock also serves to prevent deadlock 153e7c33e29SRobert Watson * when more than one PCB lock is acquired at a time (i.e., during 154e7c33e29SRobert Watson * connect()). Finally, the global rwlock protects uncounted references from 155e7c33e29SRobert Watson * vnodes to sockets bound to those vnodes: to safely dereference the 156e7c33e29SRobert Watson * v_socket pointer, the global rwlock must be held while a full reference is 157e7c33e29SRobert Watson * acquired. 158ce5f32deSRobert Watson * 159e7c33e29SRobert Watson * UNIX domain sockets each have an unpcb hung off of their so_pcb pointer, 160e7c33e29SRobert Watson * allocated in pru_attach() and freed in pru_detach(). The validity of that 161e7c33e29SRobert Watson * pointer is an invariant, so no lock is required to dereference the so_pcb 162e7c33e29SRobert Watson * pointer if a valid socket reference is held by the caller. In practice, 163e7c33e29SRobert Watson * this is always true during operations performed on a socket. Each unpcb 164e7c33e29SRobert Watson * has a back-pointer to its socket, unp_socket, which will be stable under 165e7c33e29SRobert Watson * the same circumstances. 166e7c33e29SRobert Watson * 167e7c33e29SRobert Watson * This pointer may only be safely dereferenced as long as a valid reference 168e7c33e29SRobert Watson * to the unpcb is held. Typically, this reference will be from the socket, 169e7c33e29SRobert Watson * or from another unpcb when the referring unpcb's lock is held (in order 170e7c33e29SRobert Watson * that the reference not be invalidated during use). For example, to follow 171e7c33e29SRobert Watson * unp->unp_conn->unp_socket, you need unlock the lock on unp, not unp_conn, 172e7c33e29SRobert Watson * as unp_socket remains valid as long as the reference to unp_conn is valid. 173e7c33e29SRobert Watson * 174e7c33e29SRobert Watson * Fields of unpcbss are locked using a per-unpcb lock, unp_mtx. Individual 175e7c33e29SRobert Watson * atomic reads without the lock may be performed "lockless", but more 176e7c33e29SRobert Watson * complex reads and read-modify-writes require the mutex to be held. No 177e7c33e29SRobert Watson * lock order is defined between unpcb locks -- multiple unpcb locks may be 178e7c33e29SRobert Watson * acquired at the same time only when holding the global UNIX domain socket 179e7c33e29SRobert Watson * rwlock exclusively, which prevents deadlocks. 180e7c33e29SRobert Watson * 181e7c33e29SRobert Watson * Blocking with UNIX domain sockets is a tricky issue: unlike most network 182e7c33e29SRobert Watson * protocols, bind() is a non-atomic operation, and connect() requires 183e7c33e29SRobert Watson * potential sleeping in the protocol, due to potentially waiting on local or 184e7c33e29SRobert Watson * distributed file systems. We try to separate "lookup" operations, which 185e7c33e29SRobert Watson * may sleep, and the IPC operations themselves, which typically can occur 186e7c33e29SRobert Watson * with relative atomicity as locks can be held over the entire operation. 187e7c33e29SRobert Watson * 188e7c33e29SRobert Watson * Another tricky issue is simultaneous multi-threaded or multi-process 189e7c33e29SRobert Watson * access to a single UNIX domain socket. These are handled by the flags 190e7c33e29SRobert Watson * UNP_CONNECTING and UNP_BINDING, which prevent concurrent connecting or 191e7c33e29SRobert Watson * binding, both of which involve dropping UNIX domain socket locks in order 192e7c33e29SRobert Watson * to perform namei() and other file system operations. 193ce5f32deSRobert Watson */ 194e7c33e29SRobert Watson static struct rwlock unp_global_rwlock; 195e7c33e29SRobert Watson 196e7c33e29SRobert Watson #define UNP_GLOBAL_LOCK_INIT() rw_init(&unp_global_rwlock, \ 197e7c33e29SRobert Watson "unp_global_rwlock") 198e7c33e29SRobert Watson 199e7c33e29SRobert Watson #define UNP_GLOBAL_LOCK_ASSERT() rw_assert(&unp_global_rwlock, \ 200e7c33e29SRobert Watson RA_LOCKED) 201e7c33e29SRobert Watson #define UNP_GLOBAL_UNLOCK_ASSERT() rw_assert(&unp_global_rwlock, \ 202e7c33e29SRobert Watson RA_UNLOCKED) 203e7c33e29SRobert Watson 204e7c33e29SRobert Watson #define UNP_GLOBAL_WLOCK() rw_wlock(&unp_global_rwlock) 205e7c33e29SRobert Watson #define UNP_GLOBAL_WUNLOCK() rw_wunlock(&unp_global_rwlock) 206e7c33e29SRobert Watson #define UNP_GLOBAL_WLOCK_ASSERT() rw_assert(&unp_global_rwlock, \ 207e7c33e29SRobert Watson RA_WLOCKED) 208e7c33e29SRobert Watson #define UNP_GLOBAL_WOWNED() rw_wowned(&unp_global_rwlock) 209e7c33e29SRobert Watson 210e7c33e29SRobert Watson #define UNP_GLOBAL_RLOCK() rw_rlock(&unp_global_rwlock) 211e7c33e29SRobert Watson #define UNP_GLOBAL_RUNLOCK() rw_runlock(&unp_global_rwlock) 212e7c33e29SRobert Watson #define UNP_GLOBAL_RLOCK_ASSERT() rw_assert(&unp_global_rwlock, \ 213e7c33e29SRobert Watson RA_RLOCKED) 214e7c33e29SRobert Watson 215e7c33e29SRobert Watson #define UNP_PCB_LOCK_INIT(unp) mtx_init(&(unp)->unp_mtx, \ 216e7c33e29SRobert Watson "unp_mtx", "unp_mtx", \ 217e7c33e29SRobert Watson MTX_DUPOK|MTX_DEF|MTX_RECURSE) 218e7c33e29SRobert Watson #define UNP_PCB_LOCK_DESTROY(unp) mtx_destroy(&(unp)->unp_mtx) 219e7c33e29SRobert Watson #define UNP_PCB_LOCK(unp) mtx_lock(&(unp)->unp_mtx) 220e7c33e29SRobert Watson #define UNP_PCB_UNLOCK(unp) mtx_unlock(&(unp)->unp_mtx) 221e7c33e29SRobert Watson #define UNP_PCB_LOCK_ASSERT(unp) mtx_assert(&(unp)->unp_mtx, MA_OWNED) 2220d9ce3a1SRobert Watson 223aea52f1bSRobert Watson static int unp_connect(struct socket *, struct sockaddr *, 224aea52f1bSRobert Watson struct thread *); 2256a2989fdSMatthew N. Dodd static int unp_connect2(struct socket *so, struct socket *so2, int); 226e7c33e29SRobert Watson static void unp_disconnect(struct unpcb *unp, struct unpcb *unp2); 2274d77a549SAlfred Perlstein static void unp_shutdown(struct unpcb *); 2284d77a549SAlfred Perlstein static void unp_drop(struct unpcb *, int); 229a0ec558aSRobert Watson static void unp_gc(__unused void *, int); 2304d77a549SAlfred Perlstein static void unp_scan(struct mbuf *, void (*)(struct file *)); 2314d77a549SAlfred Perlstein static void unp_mark(struct file *); 2324d77a549SAlfred Perlstein static void unp_discard(struct file *); 2334d77a549SAlfred Perlstein static void unp_freerights(struct file **, int); 2344d77a549SAlfred Perlstein static int unp_internalize(struct mbuf **, struct thread *); 2355b950deaSRobert Watson static struct mbuf *unp_addsockcred(struct thread *, struct mbuf *); 236f708ef1bSPoul-Henning Kamp 237e4445a03SRobert Watson /* 238e4445a03SRobert Watson * Definitions of protocols supported in the LOCAL domain. 239e4445a03SRobert Watson */ 240e4445a03SRobert Watson static struct domain localdomain; 241e4445a03SRobert Watson static struct protosw localsw[] = { 242e4445a03SRobert Watson { 243e4445a03SRobert Watson .pr_type = SOCK_STREAM, 244e4445a03SRobert Watson .pr_domain = &localdomain, 245e4445a03SRobert Watson .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_RIGHTS, 246e4445a03SRobert Watson .pr_ctloutput = &uipc_ctloutput, 247e4445a03SRobert Watson .pr_usrreqs = &uipc_usrreqs 248e4445a03SRobert Watson }, 249e4445a03SRobert Watson { 250e4445a03SRobert Watson .pr_type = SOCK_DGRAM, 251e4445a03SRobert Watson .pr_domain = &localdomain, 252e4445a03SRobert Watson .pr_flags = PR_ATOMIC|PR_ADDR|PR_RIGHTS, 253e4445a03SRobert Watson .pr_usrreqs = &uipc_usrreqs 254e4445a03SRobert Watson }, 255e4445a03SRobert Watson }; 256e4445a03SRobert Watson 257e4445a03SRobert Watson static struct domain localdomain = { 258e4445a03SRobert Watson .dom_family = AF_LOCAL, 259e4445a03SRobert Watson .dom_name = "local", 260e4445a03SRobert Watson .dom_init = unp_init, 261e4445a03SRobert Watson .dom_externalize = unp_externalize, 262e4445a03SRobert Watson .dom_dispose = unp_dispose, 263e4445a03SRobert Watson .dom_protosw = localsw, 264e4445a03SRobert Watson .dom_protoswNPROTOSW = &localsw[sizeof(localsw)/sizeof(localsw[0])] 265e4445a03SRobert Watson }; 266e4445a03SRobert Watson DOMAIN_SET(local); 267e4445a03SRobert Watson 268ac45e92fSRobert Watson static void 269a29f300eSGarrett Wollman uipc_abort(struct socket *so) 270df8bae1dSRodney W. Grimes { 271e7c33e29SRobert Watson struct unpcb *unp, *unp2; 272df8bae1dSRodney W. Grimes 27340f2ac28SRobert Watson unp = sotounpcb(so); 2744d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_abort: unp == NULL")); 275e7c33e29SRobert Watson 276e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 277e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 278e7c33e29SRobert Watson unp2 = unp->unp_conn; 279e7c33e29SRobert Watson if (unp2 != NULL) { 280e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 281e7c33e29SRobert Watson unp_drop(unp2, ECONNABORTED); 282e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 283e7c33e29SRobert Watson } 284e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 285e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 286df8bae1dSRodney W. Grimes } 287df8bae1dSRodney W. Grimes 288a29f300eSGarrett Wollman static int 28957bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam) 290a29f300eSGarrett Wollman { 291e7c33e29SRobert Watson struct unpcb *unp, *unp2; 2920d9ce3a1SRobert Watson const struct sockaddr *sa; 293df8bae1dSRodney W. Grimes 294df8bae1dSRodney W. Grimes /* 2951c381b19SRobert Watson * Pass back name of connected socket, if it was bound and we are 2961c381b19SRobert Watson * still connected (our peer may have closed already!). 297df8bae1dSRodney W. Grimes */ 2984d4b555eSRobert Watson unp = sotounpcb(so); 2994d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_accept: unp == NULL")); 300e7c33e29SRobert Watson 3010d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 302e7c33e29SRobert Watson UNP_GLOBAL_RLOCK(); 303e7c33e29SRobert Watson unp2 = unp->unp_conn; 304e7c33e29SRobert Watson if (unp2 != NULL && unp2->unp_addr != NULL) { 305e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 306e7c33e29SRobert Watson sa = (struct sockaddr *) unp2->unp_addr; 307e7c33e29SRobert Watson bcopy(sa, *nam, sa->sa_len); 308e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 309e7c33e29SRobert Watson } else { 3100d9ce3a1SRobert Watson sa = &sun_noname; 3110d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 312e7c33e29SRobert Watson } 313e7c33e29SRobert Watson UNP_GLOBAL_RUNLOCK(); 314e5aeaa0cSDag-Erling Smørgrav return (0); 315a29f300eSGarrett Wollman } 316df8bae1dSRodney W. Grimes 317a29f300eSGarrett Wollman static int 318b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td) 319a29f300eSGarrett Wollman { 320e7c33e29SRobert Watson u_long sendspace, recvspace; 3216d32873cSRobert Watson struct unpcb *unp; 322e7c33e29SRobert Watson int error, locked; 323df8bae1dSRodney W. Grimes 3246d32873cSRobert Watson KASSERT(so->so_pcb == NULL, ("uipc_attach: so_pcb != NULL")); 3256d32873cSRobert Watson if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 3266d32873cSRobert Watson switch (so->so_type) { 3276d32873cSRobert Watson case SOCK_STREAM: 328e7c33e29SRobert Watson sendspace = unpst_sendspace; 329e7c33e29SRobert Watson recvspace = unpst_recvspace; 3306d32873cSRobert Watson break; 3316d32873cSRobert Watson 3326d32873cSRobert Watson case SOCK_DGRAM: 333e7c33e29SRobert Watson sendspace = unpdg_sendspace; 334e7c33e29SRobert Watson recvspace = unpdg_recvspace; 3356d32873cSRobert Watson break; 3366d32873cSRobert Watson 3376d32873cSRobert Watson default: 338e7c33e29SRobert Watson panic("uipc_attach"); 3396d32873cSRobert Watson } 340e7c33e29SRobert Watson error = soreserve(so, sendspace, recvspace); 3416d32873cSRobert Watson if (error) 3426d32873cSRobert Watson return (error); 3436d32873cSRobert Watson } 34446a1d9bfSRobert Watson unp = uma_zalloc(unp_zone, M_NOWAIT | M_ZERO); 3456d32873cSRobert Watson if (unp == NULL) 3466d32873cSRobert Watson return (ENOBUFS); 3476d32873cSRobert Watson LIST_INIT(&unp->unp_refs); 348e7c33e29SRobert Watson UNP_PCB_LOCK_INIT(unp); 3496d32873cSRobert Watson unp->unp_socket = so; 3506d32873cSRobert Watson so->so_pcb = unp; 3519ae328fcSJohn Baldwin unp->unp_refcount = 1; 352e7c33e29SRobert Watson locked = 0; 353e7c33e29SRobert Watson 354e7c33e29SRobert Watson /* 355e7c33e29SRobert Watson * uipc_attach() may be called indirectly from within the UNIX domain 356e7c33e29SRobert Watson * socket code via sonewconn() in unp_connect(). Since rwlocks can 357e7c33e29SRobert Watson * not be recursed, we do the closest thing. 358e7c33e29SRobert Watson */ 359e7c33e29SRobert Watson if (!UNP_GLOBAL_WOWNED()) { 360e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 361e7c33e29SRobert Watson locked = 1; 362e7c33e29SRobert Watson } 3636d32873cSRobert Watson unp->unp_gencnt = ++unp_gencnt; 3646d32873cSRobert Watson unp_count++; 365b7e2f3ecSRobert Watson LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead : &unp_shead, 366b7e2f3ecSRobert Watson unp, unp_link); 367e7c33e29SRobert Watson if (locked) 368e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 3696d32873cSRobert Watson 3706d32873cSRobert Watson return (0); 371a29f300eSGarrett Wollman } 372a29f300eSGarrett Wollman 373a29f300eSGarrett Wollman static int 374b40ce416SJulian Elischer uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 375a29f300eSGarrett Wollman { 376dd47f5caSRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam; 377dd47f5caSRobert Watson struct vattr vattr; 378dd47f5caSRobert Watson int error, namelen; 379dd47f5caSRobert Watson struct nameidata nd; 38040f2ac28SRobert Watson struct unpcb *unp; 381dd47f5caSRobert Watson struct vnode *vp; 382dd47f5caSRobert Watson struct mount *mp; 383dd47f5caSRobert Watson char *buf; 384a29f300eSGarrett Wollman 38540f2ac28SRobert Watson unp = sotounpcb(so); 3864d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_bind: unp == NULL")); 3874f1f0ef5SRobert Watson 3884f1f0ef5SRobert Watson namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 3894f1f0ef5SRobert Watson if (namelen <= 0) 3904f1f0ef5SRobert Watson return (EINVAL); 391dd47f5caSRobert Watson 392dd47f5caSRobert Watson /* 3934f1f0ef5SRobert Watson * We don't allow simultaneous bind() calls on a single UNIX domain 3944f1f0ef5SRobert Watson * socket, so flag in-progress operations, and return an error if an 3954f1f0ef5SRobert Watson * operation is already in progress. 3964f1f0ef5SRobert Watson * 3974f1f0ef5SRobert Watson * Historically, we have not allowed a socket to be rebound, so this 3984f1f0ef5SRobert Watson * also returns an error. Not allowing re-binding certainly 3994f1f0ef5SRobert Watson * simplifies the implementation and avoids a great many possible 4004f1f0ef5SRobert Watson * failure modes. 401dd47f5caSRobert Watson */ 402e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 403dd47f5caSRobert Watson if (unp->unp_vnode != NULL) { 404e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 405dd47f5caSRobert Watson return (EINVAL); 406dd47f5caSRobert Watson } 4074f1f0ef5SRobert Watson if (unp->unp_flags & UNP_BINDING) { 408e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 4094f1f0ef5SRobert Watson return (EALREADY); 410dd47f5caSRobert Watson } 4114f1f0ef5SRobert Watson unp->unp_flags |= UNP_BINDING; 412e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 413dd47f5caSRobert Watson 414dd47f5caSRobert Watson buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 415dd47f5caSRobert Watson strlcpy(buf, soun->sun_path, namelen + 1); 416dd47f5caSRobert Watson 417dd47f5caSRobert Watson mtx_lock(&Giant); 418dd47f5caSRobert Watson restart: 419dd47f5caSRobert Watson mtx_assert(&Giant, MA_OWNED); 420dd47f5caSRobert Watson NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME, UIO_SYSSPACE, 421dd47f5caSRobert Watson buf, td); 422dd47f5caSRobert Watson /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 423dd47f5caSRobert Watson error = namei(&nd); 424dd47f5caSRobert Watson if (error) 4254f1f0ef5SRobert Watson goto error; 426dd47f5caSRobert Watson vp = nd.ni_vp; 427dd47f5caSRobert Watson if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 428dd47f5caSRobert Watson NDFREE(&nd, NDF_ONLY_PNBUF); 429dd47f5caSRobert Watson if (nd.ni_dvp == vp) 430dd47f5caSRobert Watson vrele(nd.ni_dvp); 431dd47f5caSRobert Watson else 432dd47f5caSRobert Watson vput(nd.ni_dvp); 433dd47f5caSRobert Watson if (vp != NULL) { 434dd47f5caSRobert Watson vrele(vp); 435dd47f5caSRobert Watson error = EADDRINUSE; 4364f1f0ef5SRobert Watson goto error; 437dd47f5caSRobert Watson } 438dd47f5caSRobert Watson error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 439dd47f5caSRobert Watson if (error) 4404f1f0ef5SRobert Watson goto error; 441dd47f5caSRobert Watson goto restart; 442dd47f5caSRobert Watson } 443dd47f5caSRobert Watson VATTR_NULL(&vattr); 444dd47f5caSRobert Watson vattr.va_type = VSOCK; 445dd47f5caSRobert Watson vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 446dd47f5caSRobert Watson #ifdef MAC 447dd47f5caSRobert Watson error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 448dd47f5caSRobert Watson &vattr); 449dd47f5caSRobert Watson #endif 450dd47f5caSRobert Watson if (error == 0) { 451dd47f5caSRobert Watson VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE); 452dd47f5caSRobert Watson error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 453dd47f5caSRobert Watson } 454dd47f5caSRobert Watson NDFREE(&nd, NDF_ONLY_PNBUF); 455dd47f5caSRobert Watson vput(nd.ni_dvp); 456dd47f5caSRobert Watson if (error) { 457dd47f5caSRobert Watson vn_finished_write(mp); 4584f1f0ef5SRobert Watson goto error; 459dd47f5caSRobert Watson } 460dd47f5caSRobert Watson vp = nd.ni_vp; 4614f1f0ef5SRobert Watson ASSERT_VOP_LOCKED(vp, "uipc_bind"); 462dd47f5caSRobert Watson soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 463e7c33e29SRobert Watson 464e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 465e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 466dd47f5caSRobert Watson vp->v_socket = unp->unp_socket; 467dd47f5caSRobert Watson unp->unp_vnode = vp; 468dd47f5caSRobert Watson unp->unp_addr = soun; 4694f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_BINDING; 470e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 471e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 472dd47f5caSRobert Watson VOP_UNLOCK(vp, 0, td); 473dd47f5caSRobert Watson vn_finished_write(mp); 4744f1f0ef5SRobert Watson mtx_unlock(&Giant); 4754f1f0ef5SRobert Watson free(buf, M_TEMP); 4764f1f0ef5SRobert Watson return (0); 477e7c33e29SRobert Watson 4784f1f0ef5SRobert Watson error: 479e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 4804f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_BINDING; 481e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 482dd47f5caSRobert Watson mtx_unlock(&Giant); 483dd47f5caSRobert Watson free(buf, M_TEMP); 48440f2ac28SRobert Watson return (error); 485a29f300eSGarrett Wollman } 486a29f300eSGarrett Wollman 487a29f300eSGarrett Wollman static int 488b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 489a29f300eSGarrett Wollman { 4900d9ce3a1SRobert Watson int error; 491a29f300eSGarrett Wollman 492fd179ee9SRobert Watson KASSERT(td == curthread, ("uipc_connect: td != curthread")); 493e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 494fd179ee9SRobert Watson error = unp_connect(so, nam, td); 495e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 4960d9ce3a1SRobert Watson return (error); 497a29f300eSGarrett Wollman } 498a29f300eSGarrett Wollman 499a152f8a3SRobert Watson static void 500a152f8a3SRobert Watson uipc_close(struct socket *so) 501a152f8a3SRobert Watson { 502e7c33e29SRobert Watson struct unpcb *unp, *unp2; 503a152f8a3SRobert Watson 504a152f8a3SRobert Watson unp = sotounpcb(so); 505a152f8a3SRobert Watson KASSERT(unp != NULL, ("uipc_close: unp == NULL")); 506e7c33e29SRobert Watson 507e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 508e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 509e7c33e29SRobert Watson unp2 = unp->unp_conn; 510e7c33e29SRobert Watson if (unp2 != NULL) { 511e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 512e7c33e29SRobert Watson unp_disconnect(unp, unp2); 513e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 514e7c33e29SRobert Watson } 515e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 516e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 517a152f8a3SRobert Watson } 518a152f8a3SRobert Watson 519db48c0d2SRobert Watson int 520a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2) 521a29f300eSGarrett Wollman { 522e7c33e29SRobert Watson struct unpcb *unp, *unp2; 5230d9ce3a1SRobert Watson int error; 524a29f300eSGarrett Wollman 525e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 526e7c33e29SRobert Watson unp = so1->so_pcb; 5274d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_connect2: unp == NULL")); 528e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 529e7c33e29SRobert Watson unp2 = so2->so_pcb; 530e7c33e29SRobert Watson KASSERT(unp2 != NULL, ("uipc_connect2: unp2 == NULL")); 531e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 5326a2989fdSMatthew N. Dodd error = unp_connect2(so1, so2, PRU_CONNECT2); 533e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 534e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 535e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 5360d9ce3a1SRobert Watson return (error); 537a29f300eSGarrett Wollman } 538a29f300eSGarrett Wollman 539a29f300eSGarrett Wollman /* control is EOPNOTSUPP */ 540a29f300eSGarrett Wollman 541bc725eafSRobert Watson static void 542a29f300eSGarrett Wollman uipc_detach(struct socket *so) 543a29f300eSGarrett Wollman { 544e7c33e29SRobert Watson struct unpcb *unp, *unp2; 5459ae328fcSJohn Baldwin struct sockaddr_un *saved_unp_addr; 5466d32873cSRobert Watson struct vnode *vp; 5479ae328fcSJohn Baldwin int freeunp, local_unp_rights; 548a29f300eSGarrett Wollman 54940f2ac28SRobert Watson unp = sotounpcb(so); 5504d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_detach: unp == NULL")); 551e7c33e29SRobert Watson 552e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 553e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 554e7c33e29SRobert Watson 5556d32873cSRobert Watson LIST_REMOVE(unp, unp_link); 5566d32873cSRobert Watson unp->unp_gencnt = ++unp_gencnt; 5576d32873cSRobert Watson --unp_count; 558e7c33e29SRobert Watson 559e7c33e29SRobert Watson /* 560e7c33e29SRobert Watson * XXXRW: Should assert vp->v_socket == so. 561e7c33e29SRobert Watson */ 5626d32873cSRobert Watson if ((vp = unp->unp_vnode) != NULL) { 5636d32873cSRobert Watson unp->unp_vnode->v_socket = NULL; 5646d32873cSRobert Watson unp->unp_vnode = NULL; 5656d32873cSRobert Watson } 566e7c33e29SRobert Watson unp2 = unp->unp_conn; 567e7c33e29SRobert Watson if (unp2 != NULL) { 568e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 569e7c33e29SRobert Watson unp_disconnect(unp, unp2); 570e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 571e7c33e29SRobert Watson } 572e7c33e29SRobert Watson 573e7c33e29SRobert Watson /* 574e7c33e29SRobert Watson * We hold the global lock, so it's OK to acquire multiple pcb locks 575e7c33e29SRobert Watson * at a time. 576e7c33e29SRobert Watson */ 5776d32873cSRobert Watson while (!LIST_EMPTY(&unp->unp_refs)) { 5786d32873cSRobert Watson struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 579e7c33e29SRobert Watson 580e7c33e29SRobert Watson UNP_PCB_LOCK(ref); 5816d32873cSRobert Watson unp_drop(ref, ECONNRESET); 582e7c33e29SRobert Watson UNP_PCB_UNLOCK(ref); 5836d32873cSRobert Watson } 584e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 5856d32873cSRobert Watson unp->unp_socket->so_pcb = NULL; 5866d32873cSRobert Watson local_unp_rights = unp_rights; 5879ae328fcSJohn Baldwin saved_unp_addr = unp->unp_addr; 5889ae328fcSJohn Baldwin unp->unp_addr = NULL; 5899ae328fcSJohn Baldwin unp->unp_refcount--; 5909ae328fcSJohn Baldwin freeunp = (unp->unp_refcount == 0); 5919ae328fcSJohn Baldwin if (saved_unp_addr != NULL) 5929ae328fcSJohn Baldwin FREE(saved_unp_addr, M_SONAME); 593e7c33e29SRobert Watson if (freeunp) { 594e7c33e29SRobert Watson UNP_PCB_LOCK_DESTROY(unp); 5956d32873cSRobert Watson uma_zfree(unp_zone, unp); 596e7c33e29SRobert Watson } 5976d32873cSRobert Watson if (vp) { 5986d32873cSRobert Watson int vfslocked; 5996d32873cSRobert Watson 6006d32873cSRobert Watson vfslocked = VFS_LOCK_GIANT(vp->v_mount); 6016d32873cSRobert Watson vrele(vp); 6026d32873cSRobert Watson VFS_UNLOCK_GIANT(vfslocked); 6036d32873cSRobert Watson } 6046d32873cSRobert Watson if (local_unp_rights) 6056d32873cSRobert Watson taskqueue_enqueue(taskqueue_thread, &unp_gc_task); 606a29f300eSGarrett Wollman } 607a29f300eSGarrett Wollman 608a29f300eSGarrett Wollman static int 609a29f300eSGarrett Wollman uipc_disconnect(struct socket *so) 610a29f300eSGarrett Wollman { 611e7c33e29SRobert Watson struct unpcb *unp, *unp2; 612a29f300eSGarrett Wollman 61340f2ac28SRobert Watson unp = sotounpcb(so); 6144d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_disconnect: unp == NULL")); 615e7c33e29SRobert Watson 616e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 617e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 618e7c33e29SRobert Watson unp2 = unp->unp_conn; 619e7c33e29SRobert Watson if (unp2 != NULL) { 620e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 621e7c33e29SRobert Watson unp_disconnect(unp, unp2); 622e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 623e7c33e29SRobert Watson } 624e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 625e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 626e5aeaa0cSDag-Erling Smørgrav return (0); 627a29f300eSGarrett Wollman } 628a29f300eSGarrett Wollman 629a29f300eSGarrett Wollman static int 630d374e81eSRobert Watson uipc_listen(struct socket *so, int backlog, struct thread *td) 631a29f300eSGarrett Wollman { 63240f2ac28SRobert Watson struct unpcb *unp; 6330d9ce3a1SRobert Watson int error; 634a29f300eSGarrett Wollman 63540f2ac28SRobert Watson unp = sotounpcb(so); 6364d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_listen: unp == NULL")); 637e7c33e29SRobert Watson 638e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 6394d4b555eSRobert Watson if (unp->unp_vnode == NULL) { 640e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 64140f2ac28SRobert Watson return (EINVAL); 64240f2ac28SRobert Watson } 643e7c33e29SRobert Watson 644e7c33e29SRobert Watson SOCK_LOCK(so); 645e7c33e29SRobert Watson error = solisten_proto_check(so); 646e7c33e29SRobert Watson if (error == 0) { 647e7c33e29SRobert Watson cru2x(td->td_ucred, &unp->unp_peercred); 648e7c33e29SRobert Watson unp->unp_flags |= UNP_HAVEPCCACHED; 649e7c33e29SRobert Watson solisten_proto(so, backlog); 650e7c33e29SRobert Watson } 651e7c33e29SRobert Watson SOCK_UNLOCK(so); 652e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 6530d9ce3a1SRobert Watson return (error); 654a29f300eSGarrett Wollman } 655a29f300eSGarrett Wollman 656a29f300eSGarrett Wollman static int 65757bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 658a29f300eSGarrett Wollman { 659e7c33e29SRobert Watson struct unpcb *unp, *unp2; 6600d9ce3a1SRobert Watson const struct sockaddr *sa; 661a29f300eSGarrett Wollman 6624d4b555eSRobert Watson unp = sotounpcb(so); 6634d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL")); 664e7c33e29SRobert Watson 6650d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 666e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 667bdc5f6a3SHajimu UMEMOTO /* 668e7c33e29SRobert Watson * XXX: It seems that this test always fails even when connection is 669e7c33e29SRobert Watson * established. So, this else clause is added as workaround to 670e7c33e29SRobert Watson * return PF_LOCAL sockaddr. 671bdc5f6a3SHajimu UMEMOTO */ 672e7c33e29SRobert Watson unp2 = unp->unp_conn; 673e7c33e29SRobert Watson if (unp2 != NULL) { 674e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 675e7c33e29SRobert Watson if (unp2->unp_addr != NULL) 676e7c33e29SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 677e7c33e29SRobert Watson else 6780d9ce3a1SRobert Watson sa = &sun_noname; 6790d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 680e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 681e7c33e29SRobert Watson } else { 682e7c33e29SRobert Watson sa = &sun_noname; 683e7c33e29SRobert Watson bcopy(sa, *nam, sa->sa_len); 684e7c33e29SRobert Watson } 685e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 686e5aeaa0cSDag-Erling Smørgrav return (0); 687a29f300eSGarrett Wollman } 688a29f300eSGarrett Wollman 689a29f300eSGarrett Wollman static int 690a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 691a29f300eSGarrett Wollman { 692e7c33e29SRobert Watson struct unpcb *unp, *unp2; 693a29f300eSGarrett Wollman struct socket *so2; 694337cc6b6SRobert Watson u_int mbcnt, sbcc; 6956aef685fSBrian Feldman u_long newhiwat; 696a29f300eSGarrett Wollman 69740f2ac28SRobert Watson unp = sotounpcb(so); 6984d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_rcvd: unp == NULL")); 699df8bae1dSRodney W. Grimes 700e7c33e29SRobert Watson if (so->so_type == SOCK_DGRAM) 701e7c33e29SRobert Watson panic("uipc_rcvd DGRAM?"); 702e7c33e29SRobert Watson 703e7c33e29SRobert Watson if (so->so_type != SOCK_STREAM) 704e7c33e29SRobert Watson panic("uipc_rcvd unknown socktype"); 705e7c33e29SRobert Watson 706df8bae1dSRodney W. Grimes /* 707e7c33e29SRobert Watson * Adjust backpressure on sender and wakeup any waiting to write. 708e7c33e29SRobert Watson * 709e7c33e29SRobert Watson * The consistency requirements here are a bit complex: we must 710e7c33e29SRobert Watson * acquire the lock for our own unpcb in order to prevent it from 711e7c33e29SRobert Watson * disconnecting while in use, changing the unp_conn peer. We do not 712e7c33e29SRobert Watson * need unp2's lock, since the unp2->unp_socket pointer will remain 713e7c33e29SRobert Watson * static as long as the unp2 pcb is valid, which it will be until we 714e7c33e29SRobert Watson * release unp's lock to allow a disconnect. We do need socket 715e7c33e29SRobert Watson * mutexes for both socket endpoints since we manipulate fields in 716e7c33e29SRobert Watson * both; we hold both locks at once since we access both 717e7c33e29SRobert Watson * simultaneously. 718df8bae1dSRodney W. Grimes */ 719337cc6b6SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 720337cc6b6SRobert Watson mbcnt = so->so_rcv.sb_mbcnt; 721337cc6b6SRobert Watson sbcc = so->so_rcv.sb_cc; 722337cc6b6SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 723e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 724e7c33e29SRobert Watson unp2 = unp->unp_conn; 725e7c33e29SRobert Watson if (unp2 == NULL) { 726e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 727e7c33e29SRobert Watson return (0); 728337cc6b6SRobert Watson } 729e7c33e29SRobert Watson so2 = unp2->unp_socket; 730337cc6b6SRobert Watson SOCKBUF_LOCK(&so2->so_snd); 731337cc6b6SRobert Watson so2->so_snd.sb_mbmax += unp->unp_mbcnt - mbcnt; 732337cc6b6SRobert Watson newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - sbcc; 733f535380cSDon Lewis (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat, 7346aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 7351e4d7da7SRobert Watson sowwakeup_locked(so2); 736337cc6b6SRobert Watson unp->unp_mbcnt = mbcnt; 737337cc6b6SRobert Watson unp->unp_cc = sbcc; 738e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 739e5aeaa0cSDag-Erling Smørgrav return (0); 740a29f300eSGarrett Wollman } 741df8bae1dSRodney W. Grimes 742a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 743a29f300eSGarrett Wollman 744a29f300eSGarrett Wollman static int 74557bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 746b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 747a29f300eSGarrett Wollman { 748f3f49bbbSRobert Watson struct unpcb *unp, *unp2; 749a29f300eSGarrett Wollman struct socket *so2; 750337cc6b6SRobert Watson u_int mbcnt, sbcc; 7516aef685fSBrian Feldman u_long newhiwat; 752f3f49bbbSRobert Watson int error = 0; 753a29f300eSGarrett Wollman 75440f2ac28SRobert Watson unp = sotounpcb(so); 7554d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_send: unp == NULL")); 756e7c33e29SRobert Watson 757a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 758a29f300eSGarrett Wollman error = EOPNOTSUPP; 759a29f300eSGarrett Wollman goto release; 760a29f300eSGarrett Wollman } 761a29f300eSGarrett Wollman 762fc3fcacfSRobert Watson if (control != NULL && (error = unp_internalize(&control, td))) 763a29f300eSGarrett Wollman goto release; 764df8bae1dSRodney W. Grimes 765e7c33e29SRobert Watson if ((nam != NULL) || (flags & PRUS_EOF)) 766e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 767e7c33e29SRobert Watson else 768e7c33e29SRobert Watson UNP_GLOBAL_RLOCK(); 769e7c33e29SRobert Watson 770a29f300eSGarrett Wollman switch (so->so_type) { 771a29f300eSGarrett Wollman case SOCK_DGRAM: 772a29f300eSGarrett Wollman { 773e7dd9a10SRobert Watson const struct sockaddr *from; 774df8bae1dSRodney W. Grimes 775e7c33e29SRobert Watson unp2 = unp->unp_conn; 776fc3fcacfSRobert Watson if (nam != NULL) { 777e7c33e29SRobert Watson if (unp2 != NULL) { 778df8bae1dSRodney W. Grimes error = EISCONN; 779e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 780df8bae1dSRodney W. Grimes break; 781df8bae1dSRodney W. Grimes } 782b40ce416SJulian Elischer error = unp_connect(so, nam, td); 783e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 784df8bae1dSRodney W. Grimes if (error) 785df8bae1dSRodney W. Grimes break; 786e7c33e29SRobert Watson unp2 = unp->unp_conn; 787e7c33e29SRobert Watson } else { 788e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 789e7c33e29SRobert Watson if (unp2 == NULL) { 790e7c33e29SRobert Watson error = ENOTCONN; 791e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 792e7c33e29SRobert Watson break; 793e7c33e29SRobert Watson } 794df8bae1dSRodney W. Grimes } 795b5ff0914SRobert Watson /* 796b5ff0914SRobert Watson * Because connect() and send() are non-atomic in a sendto() 797b5ff0914SRobert Watson * with a target address, it's possible that the socket will 798b5ff0914SRobert Watson * have disconnected before the send() can run. In that case 799b5ff0914SRobert Watson * return the slightly counter-intuitive but otherwise 800b5ff0914SRobert Watson * correct error that the socket is not connected. 801b5ff0914SRobert Watson */ 802e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 803e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 804b5ff0914SRobert Watson if (unp2 == NULL) { 805b5ff0914SRobert Watson error = ENOTCONN; 806b5ff0914SRobert Watson break; 807b5ff0914SRobert Watson } 808f3f49bbbSRobert Watson so2 = unp2->unp_socket; 809fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 81057bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 811df8bae1dSRodney W. Grimes else 812df8bae1dSRodney W. Grimes from = &sun_noname; 813f3f49bbbSRobert Watson if (unp2->unp_flags & UNP_WANTCRED) 8146a2989fdSMatthew N. Dodd control = unp_addsockcred(td, control); 815a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 816a34b7046SRobert Watson if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) { 8171e4d7da7SRobert Watson sorwakeup_locked(so2); 818fc3fcacfSRobert Watson m = NULL; 819fc3fcacfSRobert Watson control = NULL; 820e5aeaa0cSDag-Erling Smørgrav } else { 821a34b7046SRobert Watson SOCKBUF_UNLOCK(&so2->so_rcv); 822df8bae1dSRodney W. Grimes error = ENOBUFS; 823e5aeaa0cSDag-Erling Smørgrav } 824fc3fcacfSRobert Watson if (nam != NULL) 825e7c33e29SRobert Watson unp_disconnect(unp, unp2); 826e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 827df8bae1dSRodney W. Grimes break; 828df8bae1dSRodney W. Grimes } 829df8bae1dSRodney W. Grimes 830df8bae1dSRodney W. Grimes case SOCK_STREAM: 8316b8fda4dSGarrett Wollman /* 8321c381b19SRobert Watson * Connect if not connected yet. 8331c381b19SRobert Watson * 8341c381b19SRobert Watson * Note: A better implementation would complain if not equal 8351c381b19SRobert Watson * to the peer's address. 8366b8fda4dSGarrett Wollman */ 837402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 838fc3fcacfSRobert Watson if (nam != NULL) { 839b40ce416SJulian Elischer error = unp_connect(so, nam, td); 840e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 841402cc72dSDavid Greenman if (error) 8426b8fda4dSGarrett Wollman break; /* XXX */ 843402cc72dSDavid Greenman } else { 844402cc72dSDavid Greenman error = ENOTCONN; 845e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 846402cc72dSDavid Greenman break; 847402cc72dSDavid Greenman } 848e7c33e29SRobert Watson } else 849e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 850e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 851402cc72dSDavid Greenman 852337cc6b6SRobert Watson /* Lockless read. */ 853c0b99ffaSRobert Watson if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 854df8bae1dSRodney W. Grimes error = EPIPE; 855df8bae1dSRodney W. Grimes break; 856df8bae1dSRodney W. Grimes } 857b5ff0914SRobert Watson /* 858b5ff0914SRobert Watson * Because connect() and send() are non-atomic in a sendto() 859b5ff0914SRobert Watson * with a target address, it's possible that the socket will 860b5ff0914SRobert Watson * have disconnected before the send() can run. In that case 861b5ff0914SRobert Watson * return the slightly counter-intuitive but otherwise 862b5ff0914SRobert Watson * correct error that the socket is not connected. 863e7c33e29SRobert Watson * 864e7c33e29SRobert Watson * Lock order here has to be handled carefully: we hold the 865e7c33e29SRobert Watson * global lock, so acquiring two unpcb locks is OK. We must 866e7c33e29SRobert Watson * acquire both before acquiring any socket mutexes. We must 867e7c33e29SRobert Watson * also acquire the local socket send mutex before the remote 868e7c33e29SRobert Watson * socket receive mutex. The only tricky thing is making 869e7c33e29SRobert Watson * sure to acquire the unp2 lock before the local socket send 870e7c33e29SRobert Watson * lock, or we will experience deadlocks. 871b5ff0914SRobert Watson */ 872f3f49bbbSRobert Watson unp2 = unp->unp_conn; 873b5ff0914SRobert Watson if (unp2 == NULL) { 874b5ff0914SRobert Watson error = ENOTCONN; 875b5ff0914SRobert Watson break; 876b5ff0914SRobert Watson } 877e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 878f3f49bbbSRobert Watson so2 = unp2->unp_socket; 879a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 880f3f49bbbSRobert Watson if (unp2->unp_flags & UNP_WANTCRED) { 8816a2989fdSMatthew N. Dodd /* 8826a2989fdSMatthew N. Dodd * Credentials are passed only once on 8836a2989fdSMatthew N. Dodd * SOCK_STREAM. 8846a2989fdSMatthew N. Dodd */ 885f3f49bbbSRobert Watson unp2->unp_flags &= ~UNP_WANTCRED; 8866a2989fdSMatthew N. Dodd control = unp_addsockcred(td, control); 8876a2989fdSMatthew N. Dodd } 888df8bae1dSRodney W. Grimes /* 8891c381b19SRobert Watson * Send to paired receive port, and then reduce send buffer 8901c381b19SRobert Watson * hiwater marks to maintain backpressure. Wake up readers. 891df8bae1dSRodney W. Grimes */ 892fc3fcacfSRobert Watson if (control != NULL) { 893a34b7046SRobert Watson if (sbappendcontrol_locked(&so2->so_rcv, m, control)) 894fc3fcacfSRobert Watson control = NULL; 895e7c33e29SRobert Watson } else 896a34b7046SRobert Watson sbappend_locked(&so2->so_rcv, m); 897f3f49bbbSRobert Watson mbcnt = so2->so_rcv.sb_mbcnt - unp2->unp_mbcnt; 898f3f49bbbSRobert Watson unp2->unp_mbcnt = so2->so_rcv.sb_mbcnt; 899337cc6b6SRobert Watson sbcc = so2->so_rcv.sb_cc; 900337cc6b6SRobert Watson sorwakeup_locked(so2); 901337cc6b6SRobert Watson 902337cc6b6SRobert Watson SOCKBUF_LOCK(&so->so_snd); 903f3f49bbbSRobert Watson newhiwat = so->so_snd.sb_hiwat - (sbcc - unp2->unp_cc); 904f535380cSDon Lewis (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat, 9056aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 906337cc6b6SRobert Watson so->so_snd.sb_mbmax -= mbcnt; 9077abe2ac2SAlan Cox SOCKBUF_UNLOCK(&so->so_snd); 908f3f49bbbSRobert Watson unp2->unp_cc = sbcc; 909e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 910fc3fcacfSRobert Watson m = NULL; 911df8bae1dSRodney W. Grimes break; 912df8bae1dSRodney W. Grimes 913df8bae1dSRodney W. Grimes default: 914a29f300eSGarrett Wollman panic("uipc_send unknown socktype"); 915df8bae1dSRodney W. Grimes } 916a29f300eSGarrett Wollman 9176b8fda4dSGarrett Wollman /* 9186b8fda4dSGarrett Wollman * SEND_EOF is equivalent to a SEND followed by 9196b8fda4dSGarrett Wollman * a SHUTDOWN. 9206b8fda4dSGarrett Wollman */ 921a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 9226b8fda4dSGarrett Wollman socantsendmore(so); 9236b8fda4dSGarrett Wollman unp_shutdown(unp); 9246b8fda4dSGarrett Wollman } 925e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 926e7c33e29SRobert Watson 927e7c33e29SRobert Watson if ((nam != NULL) || (flags & PRUS_EOF)) 928e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 929e7c33e29SRobert Watson else 930e7c33e29SRobert Watson UNP_GLOBAL_RUNLOCK(); 931df8bae1dSRodney W. Grimes 932fc3fcacfSRobert Watson if (control != NULL && error != 0) 933bd508d39SDon Lewis unp_dispose(control); 934bd508d39SDon Lewis 935a29f300eSGarrett Wollman release: 936fc3fcacfSRobert Watson if (control != NULL) 937a29f300eSGarrett Wollman m_freem(control); 938fc3fcacfSRobert Watson if (m != NULL) 939a29f300eSGarrett Wollman m_freem(m); 940e5aeaa0cSDag-Erling Smørgrav return (error); 941a29f300eSGarrett Wollman } 942df8bae1dSRodney W. Grimes 943a29f300eSGarrett Wollman static int 944a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 945a29f300eSGarrett Wollman { 946e7c33e29SRobert Watson struct unpcb *unp, *unp2; 947a29f300eSGarrett Wollman struct socket *so2; 948a29f300eSGarrett Wollman 94940f2ac28SRobert Watson unp = sotounpcb(so); 9504d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sense: unp == NULL")); 951e7c33e29SRobert Watson 952a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 953e7c33e29SRobert Watson UNP_GLOBAL_RLOCK(); 954e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 955e7c33e29SRobert Watson unp2 = unp->unp_conn; 956e7c33e29SRobert Watson if (so->so_type == SOCK_STREAM && unp2 != NULL) { 957e7c33e29SRobert Watson so2 = unp2->unp_socket; 958a29f300eSGarrett Wollman sb->st_blksize += so2->so_rcv.sb_cc; 959df8bae1dSRodney W. Grimes } 960f3732fd1SPoul-Henning Kamp sb->st_dev = NODEV; 961df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 9626f782c46SJeffrey Hsu unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 963a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 964e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 965e7c33e29SRobert Watson UNP_GLOBAL_RUNLOCK(); 966df8bae1dSRodney W. Grimes return (0); 967a29f300eSGarrett Wollman } 968df8bae1dSRodney W. Grimes 969a29f300eSGarrett Wollman static int 970a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 971a29f300eSGarrett Wollman { 97240f2ac28SRobert Watson struct unpcb *unp; 973df8bae1dSRodney W. Grimes 97440f2ac28SRobert Watson unp = sotounpcb(so); 9754d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_shutdown: unp == NULL")); 976e7c33e29SRobert Watson 977e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 978e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 979a29f300eSGarrett Wollman socantsendmore(so); 980a29f300eSGarrett Wollman unp_shutdown(unp); 981e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 982e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 983e5aeaa0cSDag-Erling Smørgrav return (0); 984a29f300eSGarrett Wollman } 985df8bae1dSRodney W. Grimes 986a29f300eSGarrett Wollman static int 98757bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 988a29f300eSGarrett Wollman { 98940f2ac28SRobert Watson struct unpcb *unp; 9900d9ce3a1SRobert Watson const struct sockaddr *sa; 991a29f300eSGarrett Wollman 9924d4b555eSRobert Watson unp = sotounpcb(so); 9934d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL")); 994e7c33e29SRobert Watson 9950d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 996e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 997fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 9980d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_addr; 99983f3198bSThomas Moestl else 10000d9ce3a1SRobert Watson sa = &sun_noname; 10010d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 1002e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1003e5aeaa0cSDag-Erling Smørgrav return (0); 1004df8bae1dSRodney W. Grimes } 1005a29f300eSGarrett Wollman 1006a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = { 1007756d52a1SPoul-Henning Kamp .pru_abort = uipc_abort, 1008756d52a1SPoul-Henning Kamp .pru_accept = uipc_accept, 1009756d52a1SPoul-Henning Kamp .pru_attach = uipc_attach, 1010756d52a1SPoul-Henning Kamp .pru_bind = uipc_bind, 1011756d52a1SPoul-Henning Kamp .pru_connect = uipc_connect, 1012756d52a1SPoul-Henning Kamp .pru_connect2 = uipc_connect2, 1013756d52a1SPoul-Henning Kamp .pru_detach = uipc_detach, 1014756d52a1SPoul-Henning Kamp .pru_disconnect = uipc_disconnect, 1015756d52a1SPoul-Henning Kamp .pru_listen = uipc_listen, 1016756d52a1SPoul-Henning Kamp .pru_peeraddr = uipc_peeraddr, 1017756d52a1SPoul-Henning Kamp .pru_rcvd = uipc_rcvd, 1018756d52a1SPoul-Henning Kamp .pru_send = uipc_send, 1019756d52a1SPoul-Henning Kamp .pru_sense = uipc_sense, 1020756d52a1SPoul-Henning Kamp .pru_shutdown = uipc_shutdown, 1021756d52a1SPoul-Henning Kamp .pru_sockaddr = uipc_sockaddr, 1022a152f8a3SRobert Watson .pru_close = uipc_close, 1023a29f300eSGarrett Wollman }; 1024df8bae1dSRodney W. Grimes 10250c1bb4fbSDima Dorfman int 1026892af6b9SRobert Watson uipc_ctloutput(struct socket *so, struct sockopt *sopt) 10270c1bb4fbSDima Dorfman { 102840f2ac28SRobert Watson struct unpcb *unp; 10290d9ce3a1SRobert Watson struct xucred xu; 10306a2989fdSMatthew N. Dodd int error, optval; 10316a2989fdSMatthew N. Dodd 103296a041b5SMatthew N. Dodd if (sopt->sopt_level != 0) 103396a041b5SMatthew N. Dodd return (EINVAL); 103496a041b5SMatthew N. Dodd 10356a2989fdSMatthew N. Dodd unp = sotounpcb(so); 10364d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL")); 10376a2989fdSMatthew N. Dodd error = 0; 10380c1bb4fbSDima Dorfman switch (sopt->sopt_dir) { 10390c1bb4fbSDima Dorfman case SOPT_GET: 10400c1bb4fbSDima Dorfman switch (sopt->sopt_name) { 10410c1bb4fbSDima Dorfman case LOCAL_PEERCRED: 1042e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 10430c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC) 10440d9ce3a1SRobert Watson xu = unp->unp_peercred; 10450c1bb4fbSDima Dorfman else { 10460c1bb4fbSDima Dorfman if (so->so_type == SOCK_STREAM) 10470c1bb4fbSDima Dorfman error = ENOTCONN; 10480c1bb4fbSDima Dorfman else 10490c1bb4fbSDima Dorfman error = EINVAL; 10500c1bb4fbSDima Dorfman } 1051e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 10520d9ce3a1SRobert Watson if (error == 0) 10530d9ce3a1SRobert Watson error = sooptcopyout(sopt, &xu, sizeof(xu)); 10540c1bb4fbSDima Dorfman break; 1055e7c33e29SRobert Watson 10566a2989fdSMatthew N. Dodd case LOCAL_CREDS: 10571f837c47SRobert Watson /* Unocked read. */ 10586a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0; 10596a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 10606a2989fdSMatthew N. Dodd break; 1061e7c33e29SRobert Watson 10626a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 10631f837c47SRobert Watson /* Unocked read. */ 10646a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0; 10656a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 10666a2989fdSMatthew N. Dodd break; 1067e7c33e29SRobert Watson 10680c1bb4fbSDima Dorfman default: 10690c1bb4fbSDima Dorfman error = EOPNOTSUPP; 10700c1bb4fbSDima Dorfman break; 10710c1bb4fbSDima Dorfman } 10720c1bb4fbSDima Dorfman break; 1073e7c33e29SRobert Watson 10740c1bb4fbSDima Dorfman case SOPT_SET: 10756a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 10766a2989fdSMatthew N. Dodd case LOCAL_CREDS: 10776a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 10786a2989fdSMatthew N. Dodd error = sooptcopyin(sopt, &optval, sizeof(optval), 10796a2989fdSMatthew N. Dodd sizeof(optval)); 10806a2989fdSMatthew N. Dodd if (error) 10816a2989fdSMatthew N. Dodd break; 10826a2989fdSMatthew N. Dodd 1083e7c33e29SRobert Watson #define OPTSET(bit) do { \ 1084e7c33e29SRobert Watson UNP_PCB_LOCK(unp); \ 10856a2989fdSMatthew N. Dodd if (optval) \ 10866a2989fdSMatthew N. Dodd unp->unp_flags |= bit; \ 10876a2989fdSMatthew N. Dodd else \ 1088e7c33e29SRobert Watson unp->unp_flags &= ~bit; \ 1089e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); \ 1090e7c33e29SRobert Watson } while (0) 10916a2989fdSMatthew N. Dodd 10926a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 10936a2989fdSMatthew N. Dodd case LOCAL_CREDS: 10946a2989fdSMatthew N. Dodd OPTSET(UNP_WANTCRED); 10956a2989fdSMatthew N. Dodd break; 1096e7c33e29SRobert Watson 10976a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 10986a2989fdSMatthew N. Dodd OPTSET(UNP_CONNWAIT); 10996a2989fdSMatthew N. Dodd break; 1100e7c33e29SRobert Watson 11016a2989fdSMatthew N. Dodd default: 11026a2989fdSMatthew N. Dodd break; 11036a2989fdSMatthew N. Dodd } 11046a2989fdSMatthew N. Dodd break; 11056a2989fdSMatthew N. Dodd #undef OPTSET 11066a2989fdSMatthew N. Dodd default: 11076a2989fdSMatthew N. Dodd error = ENOPROTOOPT; 11086a2989fdSMatthew N. Dodd break; 11096a2989fdSMatthew N. Dodd } 1110abb886faSMatthew N. Dodd break; 1111e7c33e29SRobert Watson 11120c1bb4fbSDima Dorfman default: 11130c1bb4fbSDima Dorfman error = EOPNOTSUPP; 11140c1bb4fbSDima Dorfman break; 11150c1bb4fbSDima Dorfman } 11160c1bb4fbSDima Dorfman return (error); 11170c1bb4fbSDima Dorfman } 11180c1bb4fbSDima Dorfman 1119f708ef1bSPoul-Henning Kamp static int 1120892af6b9SRobert Watson unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1121df8bae1dSRodney W. Grimes { 1122892af6b9SRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam; 1123892af6b9SRobert Watson struct vnode *vp; 1124892af6b9SRobert Watson struct socket *so2, *so3; 1125b295bdcdSRobert Watson struct unpcb *unp, *unp2, *unp3; 112657bf258eSGarrett Wollman int error, len; 1127df8bae1dSRodney W. Grimes struct nameidata nd; 112857bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 11290d9ce3a1SRobert Watson struct sockaddr *sa; 11300d9ce3a1SRobert Watson 1131e7c33e29SRobert Watson UNP_GLOBAL_WLOCK_ASSERT(); 1132e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 1133df8bae1dSRodney W. Grimes 11344d4b555eSRobert Watson unp = sotounpcb(so); 11354d4b555eSRobert Watson KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 1136e7c33e29SRobert Watson 113757bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 113857bf258eSGarrett Wollman if (len <= 0) 1139e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 114055c85568SRobert Drehmel strlcpy(buf, soun->sun_path, len + 1); 1141e7c33e29SRobert Watson 1142e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 11434f1f0ef5SRobert Watson if (unp->unp_flags & UNP_CONNECTING) { 1144e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 11454f1f0ef5SRobert Watson return (EALREADY); 11464f1f0ef5SRobert Watson } 114705102f04SRobert Watson unp->unp_flags |= UNP_CONNECTING; 1148e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1149e7c33e29SRobert Watson 11500d9ce3a1SRobert Watson sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 11510d9ce3a1SRobert Watson mtx_lock(&Giant); 1152b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td); 1153797f2d22SPoul-Henning Kamp error = namei(&nd); 1154797f2d22SPoul-Henning Kamp if (error) 11550d9ce3a1SRobert Watson vp = NULL; 11560d9ce3a1SRobert Watson else 1157df8bae1dSRodney W. Grimes vp = nd.ni_vp; 11580d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_connect"); 1159762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 11600d9ce3a1SRobert Watson if (error) 11610d9ce3a1SRobert Watson goto bad; 11620d9ce3a1SRobert Watson 1163df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 1164df8bae1dSRodney W. Grimes error = ENOTSOCK; 1165df8bae1dSRodney W. Grimes goto bad; 1166df8bae1dSRodney W. Grimes } 11676fac927cSRobert Watson #ifdef MAC 11686fac927cSRobert Watson error = mac_check_vnode_open(td->td_ucred, vp, VWRITE | VREAD); 11696fac927cSRobert Watson if (error) 11706fac927cSRobert Watson goto bad; 11716fac927cSRobert Watson #endif 1172a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 1173797f2d22SPoul-Henning Kamp if (error) 1174df8bae1dSRodney W. Grimes goto bad; 11752260c03dSRobert Watson mtx_unlock(&Giant); 1176e7c33e29SRobert Watson 1177b295bdcdSRobert Watson unp = sotounpcb(so); 11784d4b555eSRobert Watson KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 1179e7c33e29SRobert Watson 1180e7c33e29SRobert Watson /* 1181e7c33e29SRobert Watson * Lock global lock for two reasons: make sure v_socket is stable, 1182e7c33e29SRobert Watson * and to protect simultaneous locking of multiple pcbs. 1183e7c33e29SRobert Watson */ 1184e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 1185df8bae1dSRodney W. Grimes so2 = vp->v_socket; 1186fc3fcacfSRobert Watson if (so2 == NULL) { 1187df8bae1dSRodney W. Grimes error = ECONNREFUSED; 11882260c03dSRobert Watson goto bad2; 1189df8bae1dSRodney W. Grimes } 1190df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 1191df8bae1dSRodney W. Grimes error = EPROTOTYPE; 11922260c03dSRobert Watson goto bad2; 1193df8bae1dSRodney W. Grimes } 1194df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 1195e7c33e29SRobert Watson if (so2->so_options & SO_ACCEPTCONN) { 1196e7c33e29SRobert Watson /* 1197e7c33e29SRobert Watson * We can't drop the global lock here or 'so2' may 1198e7c33e29SRobert Watson * become invalid, meaning that we will later recurse 1199e7c33e29SRobert Watson * back into the UNIX domain socket code while 1200e7c33e29SRobert Watson * holding the global lock. 1201e7c33e29SRobert Watson */ 12020d9ce3a1SRobert Watson so3 = sonewconn(so2, 0); 1203e7c33e29SRobert Watson } else 12040d9ce3a1SRobert Watson so3 = NULL; 12050d9ce3a1SRobert Watson if (so3 == NULL) { 1206df8bae1dSRodney W. Grimes error = ECONNREFUSED; 12070d9ce3a1SRobert Watson goto bad2; 1208df8bae1dSRodney W. Grimes } 12090c1bb4fbSDima Dorfman unp = sotounpcb(so); 1210df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 1211df8bae1dSRodney W. Grimes unp3 = sotounpcb(so3); 1212e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 1213e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 1214e7c33e29SRobert Watson UNP_PCB_LOCK(unp3); 12150d9ce3a1SRobert Watson if (unp2->unp_addr != NULL) { 12160d9ce3a1SRobert Watson bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 12170d9ce3a1SRobert Watson unp3->unp_addr = (struct sockaddr_un *) sa; 12180d9ce3a1SRobert Watson sa = NULL; 12190d9ce3a1SRobert Watson } 12200c1bb4fbSDima Dorfman /* 12210c1bb4fbSDima Dorfman * unp_peercred management: 12220c1bb4fbSDima Dorfman * 12231c381b19SRobert Watson * The connecter's (client's) credentials are copied from its 12241c381b19SRobert Watson * process structure at the time of connect() (which is now). 12250c1bb4fbSDima Dorfman */ 1226a854ed98SJohn Baldwin cru2x(td->td_ucred, &unp3->unp_peercred); 12270c1bb4fbSDima Dorfman unp3->unp_flags |= UNP_HAVEPC; 12280c1bb4fbSDima Dorfman /* 12291c381b19SRobert Watson * The receiver's (server's) credentials are copied from the 12301c381b19SRobert Watson * unp_peercred member of socket on which the former called 1231e7c33e29SRobert Watson * listen(); uipc_listen() cached that process's credentials 12321c381b19SRobert Watson * at that time so we can use them now. 12330c1bb4fbSDima Dorfman */ 12340c1bb4fbSDima Dorfman KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 12350c1bb4fbSDima Dorfman ("unp_connect: listener without cached peercred")); 12360c1bb4fbSDima Dorfman memcpy(&unp->unp_peercred, &unp2->unp_peercred, 12370c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 12380c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPC; 1239481f8fe8SMaxim Konovalov if (unp2->unp_flags & UNP_WANTCRED) 1240481f8fe8SMaxim Konovalov unp3->unp_flags |= UNP_WANTCRED; 1241e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp3); 1242e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1243e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1244335654d7SRobert Watson #ifdef MAC 1245310e7cebSRobert Watson SOCK_LOCK(so); 1246335654d7SRobert Watson mac_set_socket_peer_from_socket(so, so3); 1247335654d7SRobert Watson mac_set_socket_peer_from_socket(so3, so); 1248310e7cebSRobert Watson SOCK_UNLOCK(so); 1249335654d7SRobert Watson #endif 12500c1bb4fbSDima Dorfman 1251df8bae1dSRodney W. Grimes so2 = so3; 1252df8bae1dSRodney W. Grimes } 1253e7c33e29SRobert Watson unp = sotounpcb(so); 1254e7c33e29SRobert Watson KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 1255e7c33e29SRobert Watson unp2 = sotounpcb(so2); 1256e7c33e29SRobert Watson KASSERT(unp2 != NULL, ("unp_connect: unp2 == NULL")); 1257e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 1258e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 12596a2989fdSMatthew N. Dodd error = unp_connect2(so, so2, PRU_CONNECT); 1260e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1261e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 12620d9ce3a1SRobert Watson bad2: 1263e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 12640d9ce3a1SRobert Watson mtx_lock(&Giant); 1265df8bae1dSRodney W. Grimes bad: 12660d9ce3a1SRobert Watson mtx_assert(&Giant, MA_OWNED); 12670d9ce3a1SRobert Watson if (vp != NULL) 1268df8bae1dSRodney W. Grimes vput(vp); 12690d9ce3a1SRobert Watson mtx_unlock(&Giant); 12700d9ce3a1SRobert Watson free(sa, M_SONAME); 1271e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 1272e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 12734f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_CONNECTING; 1274e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1275df8bae1dSRodney W. Grimes return (error); 1276df8bae1dSRodney W. Grimes } 1277df8bae1dSRodney W. Grimes 1278db48c0d2SRobert Watson static int 12796a2989fdSMatthew N. Dodd unp_connect2(struct socket *so, struct socket *so2, int req) 1280df8bae1dSRodney W. Grimes { 1281e7c33e29SRobert Watson struct unpcb *unp; 1282892af6b9SRobert Watson struct unpcb *unp2; 1283df8bae1dSRodney W. Grimes 1284e7c33e29SRobert Watson unp = sotounpcb(so); 1285e7c33e29SRobert Watson KASSERT(unp != NULL, ("unp_connect2: unp == NULL")); 1286e7c33e29SRobert Watson unp2 = sotounpcb(so2); 1287e7c33e29SRobert Watson KASSERT(unp2 != NULL, ("unp_connect2: unp2 == NULL")); 1288e7c33e29SRobert Watson 1289e7c33e29SRobert Watson UNP_GLOBAL_WLOCK_ASSERT(); 1290e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 1291e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp2); 12920d9ce3a1SRobert Watson 1293df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 1294df8bae1dSRodney W. Grimes return (EPROTOTYPE); 1295df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 1296e7c33e29SRobert Watson 1297df8bae1dSRodney W. Grimes switch (so->so_type) { 1298df8bae1dSRodney W. Grimes case SOCK_DGRAM: 129998271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 1300df8bae1dSRodney W. Grimes soisconnected(so); 1301df8bae1dSRodney W. Grimes break; 1302df8bae1dSRodney W. Grimes 1303df8bae1dSRodney W. Grimes case SOCK_STREAM: 1304df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 13056a2989fdSMatthew N. Dodd if (req == PRU_CONNECT && 13066a2989fdSMatthew N. Dodd ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT)) 13076a2989fdSMatthew N. Dodd soisconnecting(so); 13086a2989fdSMatthew N. Dodd else 1309df8bae1dSRodney W. Grimes soisconnected(so); 1310df8bae1dSRodney W. Grimes soisconnected(so2); 1311df8bae1dSRodney W. Grimes break; 1312df8bae1dSRodney W. Grimes 1313df8bae1dSRodney W. Grimes default: 1314df8bae1dSRodney W. Grimes panic("unp_connect2"); 1315df8bae1dSRodney W. Grimes } 1316df8bae1dSRodney W. Grimes return (0); 1317df8bae1dSRodney W. Grimes } 1318df8bae1dSRodney W. Grimes 1319f708ef1bSPoul-Henning Kamp static void 1320e7c33e29SRobert Watson unp_disconnect(struct unpcb *unp, struct unpcb *unp2) 1321df8bae1dSRodney W. Grimes { 13221b2e3b4bSRobert Watson struct socket *so; 1323df8bae1dSRodney W. Grimes 1324e7c33e29SRobert Watson KASSERT(unp2 != NULL, ("unp_disconnect: unp2 == NULL")); 13250d9ce3a1SRobert Watson 1326e7c33e29SRobert Watson UNP_GLOBAL_WLOCK_ASSERT(); 1327e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 1328e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp2); 1329e7c33e29SRobert Watson 1330fc3fcacfSRobert Watson unp->unp_conn = NULL; 1331df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 1332df8bae1dSRodney W. Grimes case SOCK_DGRAM: 133398271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 13341b2e3b4bSRobert Watson so = unp->unp_socket; 13351b2e3b4bSRobert Watson SOCK_LOCK(so); 13361b2e3b4bSRobert Watson so->so_state &= ~SS_ISCONNECTED; 13371b2e3b4bSRobert Watson SOCK_UNLOCK(so); 1338df8bae1dSRodney W. Grimes break; 1339df8bae1dSRodney W. Grimes 1340df8bae1dSRodney W. Grimes case SOCK_STREAM: 1341df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 1342fc3fcacfSRobert Watson unp2->unp_conn = NULL; 1343df8bae1dSRodney W. Grimes soisdisconnected(unp2->unp_socket); 1344df8bae1dSRodney W. Grimes break; 1345df8bae1dSRodney W. Grimes } 1346df8bae1dSRodney W. Grimes } 1347df8bae1dSRodney W. Grimes 13480d9ce3a1SRobert Watson /* 13491c381b19SRobert Watson * unp_pcblist() assumes that UNIX domain socket memory is never reclaimed by 13501c381b19SRobert Watson * the zone (UMA_ZONE_NOFREE), and as such potentially stale pointers are 13511c381b19SRobert Watson * safe to reference. It first scans the list of struct unpcb's to generate 13521c381b19SRobert Watson * a pointer list, then it rescans its list one entry at a time to 13530d9ce3a1SRobert Watson * externalize and copyout. It checks the generation number to see if a 13540d9ce3a1SRobert Watson * struct unpcb has been reused, and will skip it if so. 13550d9ce3a1SRobert Watson */ 135698271db4SGarrett Wollman static int 135782d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 135898271db4SGarrett Wollman { 1359f5ef029eSPoul-Henning Kamp int error, i, n; 13609ae328fcSJohn Baldwin int freeunp; 136198271db4SGarrett Wollman struct unpcb *unp, **unp_list; 136298271db4SGarrett Wollman unp_gen_t gencnt; 13638f364875SJulian Elischer struct xunpgen *xug; 136498271db4SGarrett Wollman struct unp_head *head; 13658f364875SJulian Elischer struct xunpcb *xu; 136698271db4SGarrett Wollman 1367a23d65bfSBruce Evans head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 136898271db4SGarrett Wollman 136998271db4SGarrett Wollman /* 137098271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 137198271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 137298271db4SGarrett Wollman */ 1373fc3fcacfSRobert Watson if (req->oldptr == NULL) { 137498271db4SGarrett Wollman n = unp_count; 13758f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug) 137698271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 1377e5aeaa0cSDag-Erling Smørgrav return (0); 137898271db4SGarrett Wollman } 137998271db4SGarrett Wollman 1380fc3fcacfSRobert Watson if (req->newptr != NULL) 1381e5aeaa0cSDag-Erling Smørgrav return (EPERM); 138298271db4SGarrett Wollman 138398271db4SGarrett Wollman /* 138498271db4SGarrett Wollman * OK, now we're committed to doing something. 138598271db4SGarrett Wollman */ 1386a163d034SWarner Losh xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 1387e7c33e29SRobert Watson UNP_GLOBAL_RLOCK(); 138898271db4SGarrett Wollman gencnt = unp_gencnt; 138998271db4SGarrett Wollman n = unp_count; 1390e7c33e29SRobert Watson UNP_GLOBAL_RUNLOCK(); 139198271db4SGarrett Wollman 13928f364875SJulian Elischer xug->xug_len = sizeof *xug; 13938f364875SJulian Elischer xug->xug_count = n; 13948f364875SJulian Elischer xug->xug_gen = gencnt; 13958f364875SJulian Elischer xug->xug_sogen = so_gencnt; 13968f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 13978f364875SJulian Elischer if (error) { 13988f364875SJulian Elischer free(xug, M_TEMP); 1399e5aeaa0cSDag-Erling Smørgrav return (error); 14008f364875SJulian Elischer } 140198271db4SGarrett Wollman 1402a163d034SWarner Losh unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 140398271db4SGarrett Wollman 1404e7c33e29SRobert Watson /* 1405e7c33e29SRobert Watson * XXXRW: Note, this code relies very explicitly in pcb's being type 1406e7c33e29SRobert Watson * stable. 1407e7c33e29SRobert Watson */ 1408e7c33e29SRobert Watson UNP_GLOBAL_RLOCK(); 14092e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 14102e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 1411e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 14128a7d8cc6SRobert Watson if (unp->unp_gencnt <= gencnt) { 1413a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred, 1414e7c33e29SRobert Watson unp->unp_socket->so_cred)) { 1415e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 14164787fd37SPaul Saab continue; 1417e7c33e29SRobert Watson } 141898271db4SGarrett Wollman unp_list[i++] = unp; 14199ae328fcSJohn Baldwin unp->unp_refcount++; 142098271db4SGarrett Wollman } 1421e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 14224787fd37SPaul Saab } 1423e7c33e29SRobert Watson UNP_GLOBAL_RUNLOCK(); 14241c381b19SRobert Watson n = i; /* In case we lost some during malloc. */ 142598271db4SGarrett Wollman 1426e7c33e29SRobert Watson /* 1427e7c33e29SRobert Watson * XXXRW: The logic below asumes that it is OK to lock a mutex in 1428e7c33e29SRobert Watson * an unpcb that may have been freed. 1429e7c33e29SRobert Watson */ 143098271db4SGarrett Wollman error = 0; 1431fe2eee82SColin Percival xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO); 143298271db4SGarrett Wollman for (i = 0; i < n; i++) { 143398271db4SGarrett Wollman unp = unp_list[i]; 1434e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 14359ae328fcSJohn Baldwin unp->unp_refcount--; 14369ae328fcSJohn Baldwin if (unp->unp_refcount != 0 && unp->unp_gencnt <= gencnt) { 14378f364875SJulian Elischer xu->xu_len = sizeof *xu; 14388f364875SJulian Elischer xu->xu_unpp = unp; 143998271db4SGarrett Wollman /* 144098271db4SGarrett Wollman * XXX - need more locking here to protect against 144198271db4SGarrett Wollman * connect/disconnect races for SMP. 144298271db4SGarrett Wollman */ 1443fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 14448f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr, 144598271db4SGarrett Wollman unp->unp_addr->sun_len); 1446fc3fcacfSRobert Watson if (unp->unp_conn != NULL && 1447fc3fcacfSRobert Watson unp->unp_conn->unp_addr != NULL) 144898271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 14498f364875SJulian Elischer &xu->xu_caddr, 145098271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 14518f364875SJulian Elischer bcopy(unp, &xu->xu_unp, sizeof *unp); 14528f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket); 1453e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 14548f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu); 14559ae328fcSJohn Baldwin } else { 14569ae328fcSJohn Baldwin freeunp = (unp->unp_refcount == 0); 1457e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1458e7c33e29SRobert Watson if (freeunp) { 1459e7c33e29SRobert Watson UNP_PCB_LOCK_DESTROY(unp); 14609ae328fcSJohn Baldwin uma_zfree(unp_zone, unp); 146198271db4SGarrett Wollman } 146298271db4SGarrett Wollman } 1463e7c33e29SRobert Watson } 14648f364875SJulian Elischer free(xu, M_TEMP); 146598271db4SGarrett Wollman if (!error) { 146698271db4SGarrett Wollman /* 14671c381b19SRobert Watson * Give the user an updated idea of our state. If the 14681c381b19SRobert Watson * generation differs from what we told her before, she knows 14691c381b19SRobert Watson * that something happened while we were processing this 14701c381b19SRobert Watson * request, and it might be necessary to retry. 147198271db4SGarrett Wollman */ 14728f364875SJulian Elischer xug->xug_gen = unp_gencnt; 14738f364875SJulian Elischer xug->xug_sogen = so_gencnt; 14748f364875SJulian Elischer xug->xug_count = unp_count; 14758f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 147698271db4SGarrett Wollman } 147798271db4SGarrett Wollman free(unp_list, M_TEMP); 14788f364875SJulian Elischer free(xug, M_TEMP); 1479e5aeaa0cSDag-Erling Smørgrav return (error); 148098271db4SGarrett Wollman } 148198271db4SGarrett Wollman 148298271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 148398271db4SGarrett Wollman (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 148498271db4SGarrett Wollman "List of active local datagram sockets"); 148598271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 148698271db4SGarrett Wollman (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 148798271db4SGarrett Wollman "List of active local stream sockets"); 148898271db4SGarrett Wollman 1489f708ef1bSPoul-Henning Kamp static void 1490892af6b9SRobert Watson unp_shutdown(struct unpcb *unp) 1491df8bae1dSRodney W. Grimes { 1492e7c33e29SRobert Watson struct unpcb *unp2; 1493df8bae1dSRodney W. Grimes struct socket *so; 1494df8bae1dSRodney W. Grimes 1495e7c33e29SRobert Watson UNP_GLOBAL_WLOCK_ASSERT(); 1496e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 14970d9ce3a1SRobert Watson 1498e7c33e29SRobert Watson unp2 = unp->unp_conn; 1499e7c33e29SRobert Watson if (unp->unp_socket->so_type == SOCK_STREAM && unp2 != NULL) { 1500e7c33e29SRobert Watson so = unp2->unp_socket; 1501e7c33e29SRobert Watson if (so != NULL) 1502df8bae1dSRodney W. Grimes socantrcvmore(so); 1503df8bae1dSRodney W. Grimes } 1504e7c33e29SRobert Watson } 1505df8bae1dSRodney W. Grimes 1506f708ef1bSPoul-Henning Kamp static void 1507892af6b9SRobert Watson unp_drop(struct unpcb *unp, int errno) 1508df8bae1dSRodney W. Grimes { 1509df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 1510e7c33e29SRobert Watson struct unpcb *unp2; 1511df8bae1dSRodney W. Grimes 1512e7c33e29SRobert Watson UNP_GLOBAL_WLOCK_ASSERT(); 1513e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 15140d9ce3a1SRobert Watson 1515df8bae1dSRodney W. Grimes so->so_error = errno; 1516e7c33e29SRobert Watson unp2 = unp->unp_conn; 1517e7c33e29SRobert Watson if (unp2 == NULL) 1518e7c33e29SRobert Watson return; 1519e7c33e29SRobert Watson 1520e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 1521e7c33e29SRobert Watson unp_disconnect(unp, unp2); 1522e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1523df8bae1dSRodney W. Grimes } 1524df8bae1dSRodney W. Grimes 15252bc21ed9SDavid Malone static void 1526892af6b9SRobert Watson unp_freerights(struct file **rp, int fdcount) 1527df8bae1dSRodney W. Grimes { 15282bc21ed9SDavid Malone int i; 15292bc21ed9SDavid Malone struct file *fp; 1530df8bae1dSRodney W. Grimes 15312bc21ed9SDavid Malone for (i = 0; i < fdcount; i++) { 15328692c025SYoshinobu Inoue /* 15331c381b19SRobert Watson * Zero the pointer before calling unp_discard since it may 15341c381b19SRobert Watson * end up in unp_gc().. 1535d7dca903SRobert Watson * 1536d7dca903SRobert Watson * XXXRW: This is less true than it used to be. 15378692c025SYoshinobu Inoue */ 1538e7c33e29SRobert Watson fp = *rp; 1539e7c33e29SRobert Watson *rp++ = NULL; 15408692c025SYoshinobu Inoue unp_discard(fp); 1541df8bae1dSRodney W. Grimes } 15422bc21ed9SDavid Malone } 15432bc21ed9SDavid Malone 15442bc21ed9SDavid Malone int 1545892af6b9SRobert Watson unp_externalize(struct mbuf *control, struct mbuf **controlp) 15462bc21ed9SDavid Malone { 15472bc21ed9SDavid Malone struct thread *td = curthread; /* XXX */ 15482bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 15492bc21ed9SDavid Malone int i; 15502bc21ed9SDavid Malone int *fdp; 15512bc21ed9SDavid Malone struct file **rp; 15522bc21ed9SDavid Malone struct file *fp; 15532bc21ed9SDavid Malone void *data; 15542bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 15552bc21ed9SDavid Malone int error, newfds; 15562bc21ed9SDavid Malone int f; 15572bc21ed9SDavid Malone u_int newlen; 15582bc21ed9SDavid Malone 1559e7c33e29SRobert Watson UNP_GLOBAL_UNLOCK_ASSERT(); 15604c5bc1caSRobert Watson 15612bc21ed9SDavid Malone error = 0; 15622bc21ed9SDavid Malone if (controlp != NULL) /* controlp == NULL => free control messages */ 15632bc21ed9SDavid Malone *controlp = NULL; 15642bc21ed9SDavid Malone 15652bc21ed9SDavid Malone while (cm != NULL) { 15662bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 15672bc21ed9SDavid Malone error = EINVAL; 15682bc21ed9SDavid Malone break; 15692bc21ed9SDavid Malone } 15702bc21ed9SDavid Malone 15712bc21ed9SDavid Malone data = CMSG_DATA(cm); 15722bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 15732bc21ed9SDavid Malone 15742bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET 15752bc21ed9SDavid Malone && cm->cmsg_type == SCM_RIGHTS) { 15762bc21ed9SDavid Malone newfds = datalen / sizeof(struct file *); 15772bc21ed9SDavid Malone rp = data; 15782bc21ed9SDavid Malone 1579e2f9a08bSOlivier Houchard /* If we're not outputting the descriptors free them. */ 15802bc21ed9SDavid Malone if (error || controlp == NULL) { 15812bc21ed9SDavid Malone unp_freerights(rp, newfds); 15822bc21ed9SDavid Malone goto next; 15832bc21ed9SDavid Malone } 1584426da3bcSAlfred Perlstein FILEDESC_LOCK(td->td_proc->p_fd); 15852bc21ed9SDavid Malone /* if the new FD's will not fit free them. */ 15862bc21ed9SDavid Malone if (!fdavail(td, newfds)) { 1587426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 15882bc21ed9SDavid Malone error = EMSGSIZE; 15892bc21ed9SDavid Malone unp_freerights(rp, newfds); 15902bc21ed9SDavid Malone goto next; 1591df8bae1dSRodney W. Grimes } 1592ed5b7817SJulian Elischer /* 15931c381b19SRobert Watson * Now change each pointer to an fd in the global 15941c381b19SRobert Watson * table to an integer that is the index to the local 15951c381b19SRobert Watson * fd table entry that we set up to point to the 15961c381b19SRobert Watson * global one we are transferring. 1597ed5b7817SJulian Elischer */ 15982bc21ed9SDavid Malone newlen = newfds * sizeof(int); 15992bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 16002bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 16012bc21ed9SDavid Malone if (*controlp == NULL) { 1602426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 16032bc21ed9SDavid Malone error = E2BIG; 16042bc21ed9SDavid Malone unp_freerights(rp, newfds); 16052bc21ed9SDavid Malone goto next; 16062bc21ed9SDavid Malone } 16072bc21ed9SDavid Malone 16082bc21ed9SDavid Malone fdp = (int *) 16092bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1610df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 1611a6d4491cSDag-Erling Smørgrav if (fdalloc(td, 0, &f)) 16122bc21ed9SDavid Malone panic("unp_externalize fdalloc failed"); 16138692c025SYoshinobu Inoue fp = *rp++; 1614b40ce416SJulian Elischer td->td_proc->p_fd->fd_ofiles[f] = fp; 1615426da3bcSAlfred Perlstein FILE_LOCK(fp); 1616df8bae1dSRodney W. Grimes fp->f_msgcount--; 1617426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1618df8bae1dSRodney W. Grimes unp_rights--; 16198692c025SYoshinobu Inoue *fdp++ = f; 1620df8bae1dSRodney W. Grimes } 1621426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 16221c381b19SRobert Watson } else { 16231c381b19SRobert Watson /* We can just copy anything else across. */ 16242bc21ed9SDavid Malone if (error || controlp == NULL) 16252bc21ed9SDavid Malone goto next; 16262bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, datalen, 16272bc21ed9SDavid Malone cm->cmsg_type, cm->cmsg_level); 16282bc21ed9SDavid Malone if (*controlp == NULL) { 16292bc21ed9SDavid Malone error = ENOBUFS; 16302bc21ed9SDavid Malone goto next; 16312bc21ed9SDavid Malone } 16322bc21ed9SDavid Malone bcopy(data, 16332bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 16342bc21ed9SDavid Malone datalen); 16352bc21ed9SDavid Malone } 16362bc21ed9SDavid Malone 16372bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 16382bc21ed9SDavid Malone 16392bc21ed9SDavid Malone next: 16402bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 16412bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 16422bc21ed9SDavid Malone cm = (struct cmsghdr *) 16432bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 16448692c025SYoshinobu Inoue } else { 16452bc21ed9SDavid Malone clen = 0; 16462bc21ed9SDavid Malone cm = NULL; 16478692c025SYoshinobu Inoue } 16488692c025SYoshinobu Inoue } 16498692c025SYoshinobu Inoue 16502bc21ed9SDavid Malone m_freem(control); 16512bc21ed9SDavid Malone 16522bc21ed9SDavid Malone return (error); 1653df8bae1dSRodney W. Grimes } 1654df8bae1dSRodney W. Grimes 16554f590175SPaul Saab static void 16564f590175SPaul Saab unp_zone_change(void *tag) 16574f590175SPaul Saab { 16584f590175SPaul Saab 16594f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets); 16604f590175SPaul Saab } 16614f590175SPaul Saab 166298271db4SGarrett Wollman void 166398271db4SGarrett Wollman unp_init(void) 166498271db4SGarrett Wollman { 16651c381b19SRobert Watson 16669e9d298aSJeff Roberson unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 16679ae328fcSJohn Baldwin NULL, NULL, UMA_ALIGN_PTR, 0); 1668fc3fcacfSRobert Watson if (unp_zone == NULL) 166998271db4SGarrett Wollman panic("unp_init"); 16704f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets); 16714f590175SPaul Saab EVENTHANDLER_REGISTER(maxsockets_change, unp_zone_change, 16724f590175SPaul Saab NULL, EVENTHANDLER_PRI_ANY); 167398271db4SGarrett Wollman LIST_INIT(&unp_dhead); 167498271db4SGarrett Wollman LIST_INIT(&unp_shead); 1675a0ec558aSRobert Watson TASK_INIT(&unp_gc_task, 0, unp_gc, NULL); 1676e7c33e29SRobert Watson UNP_GLOBAL_LOCK_INIT(); 167798271db4SGarrett Wollman } 167898271db4SGarrett Wollman 1679f708ef1bSPoul-Henning Kamp static int 1680892af6b9SRobert Watson unp_internalize(struct mbuf **controlp, struct thread *td) 1681df8bae1dSRodney W. Grimes { 16822bc21ed9SDavid Malone struct mbuf *control = *controlp; 1683b40ce416SJulian Elischer struct proc *p = td->td_proc; 16848692c025SYoshinobu Inoue struct filedesc *fdescp = p->p_fd; 16852bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 16862bc21ed9SDavid Malone struct cmsgcred *cmcred; 16872bc21ed9SDavid Malone struct file **rp; 16882bc21ed9SDavid Malone struct file *fp; 16892bc21ed9SDavid Malone struct timeval *tv; 16902bc21ed9SDavid Malone int i, fd, *fdp; 16912bc21ed9SDavid Malone void *data; 16922bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 16932bc21ed9SDavid Malone int error, oldfds; 16948692c025SYoshinobu Inoue u_int newlen; 1695df8bae1dSRodney W. Grimes 1696e7c33e29SRobert Watson UNP_GLOBAL_UNLOCK_ASSERT(); 16974c5bc1caSRobert Watson 16982bc21ed9SDavid Malone error = 0; 16992bc21ed9SDavid Malone *controlp = NULL; 17000b788fa1SBill Paul 17012bc21ed9SDavid Malone while (cm != NULL) { 17022bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 17032bc21ed9SDavid Malone || cm->cmsg_len > clen) { 17042bc21ed9SDavid Malone error = EINVAL; 17052bc21ed9SDavid Malone goto out; 17062bc21ed9SDavid Malone } 17072bc21ed9SDavid Malone 17082bc21ed9SDavid Malone data = CMSG_DATA(cm); 17092bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 17102bc21ed9SDavid Malone 17112bc21ed9SDavid Malone switch (cm->cmsg_type) { 17120b788fa1SBill Paul /* 17130b788fa1SBill Paul * Fill in credential information. 17140b788fa1SBill Paul */ 17152bc21ed9SDavid Malone case SCM_CREDS: 17162bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 17172bc21ed9SDavid Malone SCM_CREDS, SOL_SOCKET); 17182bc21ed9SDavid Malone if (*controlp == NULL) { 17192bc21ed9SDavid Malone error = ENOBUFS; 17202bc21ed9SDavid Malone goto out; 17212bc21ed9SDavid Malone } 17222bc21ed9SDavid Malone 17232bc21ed9SDavid Malone cmcred = (struct cmsgcred *) 17242bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 17250b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 1726a854ed98SJohn Baldwin cmcred->cmcred_uid = td->td_ucred->cr_ruid; 1727a854ed98SJohn Baldwin cmcred->cmcred_gid = td->td_ucred->cr_rgid; 1728a854ed98SJohn Baldwin cmcred->cmcred_euid = td->td_ucred->cr_uid; 1729a854ed98SJohn Baldwin cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 17300b788fa1SBill Paul CMGROUP_MAX); 17310b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 17322bc21ed9SDavid Malone cmcred->cmcred_groups[i] = 1733a854ed98SJohn Baldwin td->td_ucred->cr_groups[i]; 17342bc21ed9SDavid Malone break; 17350b788fa1SBill Paul 17362bc21ed9SDavid Malone case SCM_RIGHTS: 17372bc21ed9SDavid Malone oldfds = datalen / sizeof (int); 1738ed5b7817SJulian Elischer /* 17391c381b19SRobert Watson * Check that all the FDs passed in refer to legal 17401c381b19SRobert Watson * files. If not, reject the entire operation. 1741ed5b7817SJulian Elischer */ 17422bc21ed9SDavid Malone fdp = data; 1743426da3bcSAlfred Perlstein FILEDESC_LOCK(fdescp); 1744df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 17458692c025SYoshinobu Inoue fd = *fdp++; 17468692c025SYoshinobu Inoue if ((unsigned)fd >= fdescp->fd_nfiles || 17472bc21ed9SDavid Malone fdescp->fd_ofiles[fd] == NULL) { 1748426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 17492bc21ed9SDavid Malone error = EBADF; 17502bc21ed9SDavid Malone goto out; 17512bc21ed9SDavid Malone } 1752e7d6662fSAlfred Perlstein fp = fdescp->fd_ofiles[fd]; 1753e7d6662fSAlfred Perlstein if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 1754e7d6662fSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 1755e7d6662fSAlfred Perlstein error = EOPNOTSUPP; 1756e7d6662fSAlfred Perlstein goto out; 1757e7d6662fSAlfred Perlstein } 1758e7d6662fSAlfred Perlstein 1759df8bae1dSRodney W. Grimes } 1760ed5b7817SJulian Elischer /* 1761e7c33e29SRobert Watson * Now replace the integer FDs with pointers to 1762e7c33e29SRobert Watson * the associated global file table entry.. 1763ed5b7817SJulian Elischer */ 17642bc21ed9SDavid Malone newlen = oldfds * sizeof(struct file *); 17652bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 17662bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 17672bc21ed9SDavid Malone if (*controlp == NULL) { 1768426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 17692bc21ed9SDavid Malone error = E2BIG; 17702bc21ed9SDavid Malone goto out; 17718692c025SYoshinobu Inoue } 17728692c025SYoshinobu Inoue 17732bc21ed9SDavid Malone fdp = data; 17742bc21ed9SDavid Malone rp = (struct file **) 17752bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 17768692c025SYoshinobu Inoue for (i = 0; i < oldfds; i++) { 17778692c025SYoshinobu Inoue fp = fdescp->fd_ofiles[*fdp++]; 1778df8bae1dSRodney W. Grimes *rp++ = fp; 1779426da3bcSAlfred Perlstein FILE_LOCK(fp); 1780df8bae1dSRodney W. Grimes fp->f_count++; 1781df8bae1dSRodney W. Grimes fp->f_msgcount++; 1782426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1783df8bae1dSRodney W. Grimes unp_rights++; 1784df8bae1dSRodney W. Grimes } 1785426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 17862bc21ed9SDavid Malone break; 17872bc21ed9SDavid Malone 17882bc21ed9SDavid Malone case SCM_TIMESTAMP: 17892bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*tv), 17902bc21ed9SDavid Malone SCM_TIMESTAMP, SOL_SOCKET); 17912bc21ed9SDavid Malone if (*controlp == NULL) { 17922bc21ed9SDavid Malone error = ENOBUFS; 17932bc21ed9SDavid Malone goto out; 17948692c025SYoshinobu Inoue } 17952bc21ed9SDavid Malone tv = (struct timeval *) 17962bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 17972bc21ed9SDavid Malone microtime(tv); 17982bc21ed9SDavid Malone break; 17992bc21ed9SDavid Malone 18002bc21ed9SDavid Malone default: 18012bc21ed9SDavid Malone error = EINVAL; 18022bc21ed9SDavid Malone goto out; 18032bc21ed9SDavid Malone } 18042bc21ed9SDavid Malone 18052bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 18062bc21ed9SDavid Malone 18072bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 18082bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 18092bc21ed9SDavid Malone cm = (struct cmsghdr *) 18102bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 18112bc21ed9SDavid Malone } else { 18122bc21ed9SDavid Malone clen = 0; 18132bc21ed9SDavid Malone cm = NULL; 18142bc21ed9SDavid Malone } 18152bc21ed9SDavid Malone } 18162bc21ed9SDavid Malone 18172bc21ed9SDavid Malone out: 18182bc21ed9SDavid Malone m_freem(control); 18192bc21ed9SDavid Malone 18202bc21ed9SDavid Malone return (error); 1821df8bae1dSRodney W. Grimes } 1822df8bae1dSRodney W. Grimes 18235b950deaSRobert Watson static struct mbuf * 18246a2989fdSMatthew N. Dodd unp_addsockcred(struct thread *td, struct mbuf *control) 18256a2989fdSMatthew N. Dodd { 182670df31f4SMaxim Konovalov struct mbuf *m, *n, *n_prev; 18276a2989fdSMatthew N. Dodd struct sockcred *sc; 182870df31f4SMaxim Konovalov const struct cmsghdr *cm; 18296a2989fdSMatthew N. Dodd int ngroups; 18306a2989fdSMatthew N. Dodd int i; 18316a2989fdSMatthew N. Dodd 18326a2989fdSMatthew N. Dodd ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX); 18336a2989fdSMatthew N. Dodd 18346a2989fdSMatthew N. Dodd m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET); 18356a2989fdSMatthew N. Dodd if (m == NULL) 18366a2989fdSMatthew N. Dodd return (control); 18376a2989fdSMatthew N. Dodd 18386a2989fdSMatthew N. Dodd sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *)); 18396a2989fdSMatthew N. Dodd sc->sc_uid = td->td_ucred->cr_ruid; 18406a2989fdSMatthew N. Dodd sc->sc_euid = td->td_ucred->cr_uid; 18416a2989fdSMatthew N. Dodd sc->sc_gid = td->td_ucred->cr_rgid; 18426a2989fdSMatthew N. Dodd sc->sc_egid = td->td_ucred->cr_gid; 18436a2989fdSMatthew N. Dodd sc->sc_ngroups = ngroups; 18446a2989fdSMatthew N. Dodd for (i = 0; i < sc->sc_ngroups; i++) 18456a2989fdSMatthew N. Dodd sc->sc_groups[i] = td->td_ucred->cr_groups[i]; 18466a2989fdSMatthew N. Dodd 18476a2989fdSMatthew N. Dodd /* 18481c381b19SRobert Watson * Unlink SCM_CREDS control messages (struct cmsgcred), since just 18491c381b19SRobert Watson * created SCM_CREDS control message (struct sockcred) has another 18501c381b19SRobert Watson * format. 18516a2989fdSMatthew N. Dodd */ 185270df31f4SMaxim Konovalov if (control != NULL) 185370df31f4SMaxim Konovalov for (n = control, n_prev = NULL; n != NULL;) { 185470df31f4SMaxim Konovalov cm = mtod(n, struct cmsghdr *); 185570df31f4SMaxim Konovalov if (cm->cmsg_level == SOL_SOCKET && 185670df31f4SMaxim Konovalov cm->cmsg_type == SCM_CREDS) { 185770df31f4SMaxim Konovalov if (n_prev == NULL) 185870df31f4SMaxim Konovalov control = n->m_next; 185970df31f4SMaxim Konovalov else 186070df31f4SMaxim Konovalov n_prev->m_next = n->m_next; 186170df31f4SMaxim Konovalov n = m_free(n); 186270df31f4SMaxim Konovalov } else { 186370df31f4SMaxim Konovalov n_prev = n; 186470df31f4SMaxim Konovalov n = n->m_next; 186570df31f4SMaxim Konovalov } 186670df31f4SMaxim Konovalov } 18676a2989fdSMatthew N. Dodd 186870df31f4SMaxim Konovalov /* Prepend it to the head. */ 186970df31f4SMaxim Konovalov m->m_next = control; 187070df31f4SMaxim Konovalov 187170df31f4SMaxim Konovalov return (m); 18726a2989fdSMatthew N. Dodd } 18736a2989fdSMatthew N. Dodd 1874161a0c7cSRobert Watson /* 1875a0ec558aSRobert Watson * unp_defer indicates whether additional work has been defered for a future 1876a0ec558aSRobert Watson * pass through unp_gc(). It is thread local and does not require explicit 1877a0ec558aSRobert Watson * synchronization. 1878161a0c7cSRobert Watson */ 1879a0ec558aSRobert Watson static int unp_defer; 1880a0ec558aSRobert Watson 1881a0ec558aSRobert Watson static int unp_taskcount; 1882a0ec558aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0, ""); 1883a0ec558aSRobert Watson 1884a0ec558aSRobert Watson static int unp_recycled; 1885a0ec558aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0, ""); 1886df8bae1dSRodney W. Grimes 1887f708ef1bSPoul-Henning Kamp static void 1888a0ec558aSRobert Watson unp_gc(__unused void *arg, int pending) 1889df8bae1dSRodney W. Grimes { 1890892af6b9SRobert Watson struct file *fp, *nextfp; 1891892af6b9SRobert Watson struct socket *so; 1892df8bae1dSRodney W. Grimes struct file **extra_ref, **fpp; 1893df8bae1dSRodney W. Grimes int nunref, i; 189495f004dcSAlfred Perlstein int nfiles_snap; 189595f004dcSAlfred Perlstein int nfiles_slack = 20; 1896df8bae1dSRodney W. Grimes 1897a0ec558aSRobert Watson unp_taskcount++; 1898df8bae1dSRodney W. Grimes unp_defer = 0; 1899ed5b7817SJulian Elischer /* 19009ae328fcSJohn Baldwin * Before going through all this, set all FDs to be NOT deferred and 19011c381b19SRobert Watson * NOT externally accessible. 1902ed5b7817SJulian Elischer */ 1903426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 19042e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) 1905426da3bcSAlfred Perlstein fp->f_gcflag &= ~(FMARK|FDEFER); 1906df8bae1dSRodney W. Grimes do { 19075bb84bc8SRobert Watson KASSERT(unp_defer >= 0, ("unp_gc: unp_defer %d", unp_defer)); 19082e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) { 1909426da3bcSAlfred Perlstein FILE_LOCK(fp); 1910ed5b7817SJulian Elischer /* 1911a0ec558aSRobert Watson * If the file is not open, skip it -- could be a 1912a0ec558aSRobert Watson * file in the process of being opened, or in the 1913a0ec558aSRobert Watson * process of being closed. If the file is 1914a0ec558aSRobert Watson * "closing", it may have been marked for deferred 1915a0ec558aSRobert Watson * consideration. Clear the flag now if so. 1916ed5b7817SJulian Elischer */ 1917426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1918a0ec558aSRobert Watson if (fp->f_gcflag & FDEFER) 1919a0ec558aSRobert Watson unp_defer--; 1920a0ec558aSRobert Watson fp->f_gcflag &= ~(FMARK|FDEFER); 1921426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1922df8bae1dSRodney W. Grimes continue; 1923426da3bcSAlfred Perlstein } 1924ed5b7817SJulian Elischer /* 19259ae328fcSJohn Baldwin * If we already marked it as 'defer' in a 19269ae328fcSJohn Baldwin * previous pass, then try to process it this 19279ae328fcSJohn Baldwin * time and un-mark it. 1928ed5b7817SJulian Elischer */ 1929426da3bcSAlfred Perlstein if (fp->f_gcflag & FDEFER) { 1930426da3bcSAlfred Perlstein fp->f_gcflag &= ~FDEFER; 1931df8bae1dSRodney W. Grimes unp_defer--; 1932df8bae1dSRodney W. Grimes } else { 1933ed5b7817SJulian Elischer /* 1934e7c33e29SRobert Watson * If it's not deferred, then check if it's 1935ed5b7817SJulian Elischer * already marked.. if so skip it 1936ed5b7817SJulian Elischer */ 1937426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) { 1938426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1939df8bae1dSRodney W. Grimes continue; 1940426da3bcSAlfred Perlstein } 1941ed5b7817SJulian Elischer /* 19421c381b19SRobert Watson * If all references are from messages in 19431c381b19SRobert Watson * transit, then skip it. it's not externally 19441c381b19SRobert Watson * accessible. 1945ed5b7817SJulian Elischer */ 1946426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount) { 1947426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1948df8bae1dSRodney W. Grimes continue; 1949426da3bcSAlfred Perlstein } 1950ed5b7817SJulian Elischer /* 1951ed5b7817SJulian Elischer * If it got this far then it must be 1952ed5b7817SJulian Elischer * externally accessible. 1953ed5b7817SJulian Elischer */ 1954426da3bcSAlfred Perlstein fp->f_gcflag |= FMARK; 1955df8bae1dSRodney W. Grimes } 1956ed5b7817SJulian Elischer /* 19579ae328fcSJohn Baldwin * Either it was deferred, or it is externally 19581c381b19SRobert Watson * accessible and not already marked so. Now check 19591c381b19SRobert Watson * if it is possibly one of OUR sockets. 1960ed5b7817SJulian Elischer */ 1961df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_SOCKET || 196248e3128bSMatthew Dillon (so = fp->f_data) == NULL) { 1963426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1964df8bae1dSRodney W. Grimes continue; 1965426da3bcSAlfred Perlstein } 1966748e0b0aSGarrett Wollman if (so->so_proto->pr_domain != &localdomain || 19679ae328fcSJohn Baldwin (so->so_proto->pr_flags & PR_RIGHTS) == 0) { 19689ae328fcSJohn Baldwin FILE_UNLOCK(fp); 1969df8bae1dSRodney W. Grimes continue; 19709ae328fcSJohn Baldwin } 19719ae328fcSJohn Baldwin 19729ae328fcSJohn Baldwin /* 19739ae328fcSJohn Baldwin * Tell any other threads that do a subsequent 19749ae328fcSJohn Baldwin * fdrop() that we are scanning the message 19759ae328fcSJohn Baldwin * buffers. 19769ae328fcSJohn Baldwin */ 19779ae328fcSJohn Baldwin fp->f_gcflag |= FWAIT; 19789ae328fcSJohn Baldwin FILE_UNLOCK(fp); 19799ae328fcSJohn Baldwin 1980ed5b7817SJulian Elischer /* 19811c381b19SRobert Watson * So, Ok, it's one of our sockets and it IS 19829ae328fcSJohn Baldwin * externally accessible (or was deferred). Now we 19831c381b19SRobert Watson * look to see if we hold any file descriptors in its 1984ed5b7817SJulian Elischer * message buffers. Follow those links and mark them 1985ed5b7817SJulian Elischer * as accessible too. 1986ed5b7817SJulian Elischer */ 19877717cf07SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1988df8bae1dSRodney W. Grimes unp_scan(so->so_rcv.sb_mb, unp_mark); 19897717cf07SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 19909ae328fcSJohn Baldwin 19919ae328fcSJohn Baldwin /* 19929ae328fcSJohn Baldwin * Wake up any threads waiting in fdrop(). 19939ae328fcSJohn Baldwin */ 19949ae328fcSJohn Baldwin FILE_LOCK(fp); 19959ae328fcSJohn Baldwin fp->f_gcflag &= ~FWAIT; 19969ae328fcSJohn Baldwin wakeup(&fp->f_gcflag); 19979ae328fcSJohn Baldwin FILE_UNLOCK(fp); 1998df8bae1dSRodney W. Grimes } 1999df8bae1dSRodney W. Grimes } while (unp_defer); 2000426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 2001df8bae1dSRodney W. Grimes /* 2002a0ec558aSRobert Watson * XXXRW: The following comments need updating for a post-SMPng and 2003a0ec558aSRobert Watson * deferred unp_gc() world, but are still generally accurate. 2004a0ec558aSRobert Watson * 20051c381b19SRobert Watson * We grab an extra reference to each of the file table entries that 20061c381b19SRobert Watson * are not otherwise accessible and then free the rights that are 20071c381b19SRobert Watson * stored in messages on them. 2008df8bae1dSRodney W. Grimes * 2009df8bae1dSRodney W. Grimes * The bug in the orginal code is a little tricky, so I'll describe 2010df8bae1dSRodney W. Grimes * what's wrong with it here. 2011df8bae1dSRodney W. Grimes * 2012df8bae1dSRodney W. Grimes * It is incorrect to simply unp_discard each entry for f_msgcount 2013df8bae1dSRodney W. Grimes * times -- consider the case of sockets A and B that contain 2014df8bae1dSRodney W. Grimes * references to each other. On a last close of some other socket, 2015df8bae1dSRodney W. Grimes * we trigger a gc since the number of outstanding rights (unp_rights) 2016a0ec558aSRobert Watson * is non-zero. If during the sweep phase the gc code unp_discards, 2017df8bae1dSRodney W. Grimes * we end up doing a (full) closef on the descriptor. A closef on A 2018df8bae1dSRodney W. Grimes * results in the following chain. Closef calls soo_close, which 2019df8bae1dSRodney W. Grimes * calls soclose. Soclose calls first (through the switch 2020df8bae1dSRodney W. Grimes * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 20211c381b19SRobert Watson * returns because the previous instance had set unp_gcing, and we 20221c381b19SRobert Watson * return all the way back to soclose, which marks the socket with 20231c381b19SRobert Watson * SS_NOFDREF, and then calls sofree. Sofree calls sorflush to free 20241c381b19SRobert Watson * up the rights that are queued in messages on the socket A, i.e., 20251c381b19SRobert Watson * the reference on B. The sorflush calls via the dom_dispose switch 20261c381b19SRobert Watson * unp_dispose, which unp_scans with unp_discard. This second 2027df8bae1dSRodney W. Grimes * instance of unp_discard just calls closef on B. 2028df8bae1dSRodney W. Grimes * 2029df8bae1dSRodney W. Grimes * Well, a similar chain occurs on B, resulting in a sorflush on B, 2030df8bae1dSRodney W. Grimes * which results in another closef on A. Unfortunately, A is already 2031df8bae1dSRodney W. Grimes * being closed, and the descriptor has already been marked with 2032df8bae1dSRodney W. Grimes * SS_NOFDREF, and soclose panics at this point. 2033df8bae1dSRodney W. Grimes * 2034df8bae1dSRodney W. Grimes * Here, we first take an extra reference to each inaccessible 20351c381b19SRobert Watson * descriptor. Then, we call sorflush ourself, since we know it is a 20361c381b19SRobert Watson * Unix domain socket anyhow. After we destroy all the rights 20371c381b19SRobert Watson * carried in messages, we do a last closef to get rid of our extra 20381c381b19SRobert Watson * reference. This is the last close, and the unp_detach etc will 20391c381b19SRobert Watson * shut down the socket. 2040df8bae1dSRodney W. Grimes * 2041df8bae1dSRodney W. Grimes * 91/09/19, bsy@cs.cmu.edu 2042df8bae1dSRodney W. Grimes */ 204395f004dcSAlfred Perlstein again: 2044e4643c73SPoul-Henning Kamp nfiles_snap = openfiles + nfiles_slack; /* some slack */ 204595f004dcSAlfred Perlstein extra_ref = malloc(nfiles_snap * sizeof(struct file *), M_TEMP, 204695f004dcSAlfred Perlstein M_WAITOK); 2047426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 2048e4643c73SPoul-Henning Kamp if (nfiles_snap < openfiles) { 204995f004dcSAlfred Perlstein sx_sunlock(&filelist_lock); 205095f004dcSAlfred Perlstein free(extra_ref, M_TEMP); 205195f004dcSAlfred Perlstein nfiles_slack += 20; 205295f004dcSAlfred Perlstein goto again; 205395f004dcSAlfred Perlstein } 2054fc3fcacfSRobert Watson for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; 2055fc3fcacfSRobert Watson fp != NULL; fp = nextfp) { 20562e3c8fcbSPoul-Henning Kamp nextfp = LIST_NEXT(fp, f_list); 2057426da3bcSAlfred Perlstein FILE_LOCK(fp); 2058ed5b7817SJulian Elischer /* 2059ed5b7817SJulian Elischer * If it's not open, skip it 2060ed5b7817SJulian Elischer */ 2061426da3bcSAlfred Perlstein if (fp->f_count == 0) { 2062426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 2063df8bae1dSRodney W. Grimes continue; 2064426da3bcSAlfred Perlstein } 2065ed5b7817SJulian Elischer /* 2066ed5b7817SJulian Elischer * If all refs are from msgs, and it's not marked accessible 20671c381b19SRobert Watson * then it must be referenced from some unreachable cycle of 20681c381b19SRobert Watson * (shut-down) FDs, so include it in our list of FDs to 20691c381b19SRobert Watson * remove. 2070ed5b7817SJulian Elischer */ 2071426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) { 2072df8bae1dSRodney W. Grimes *fpp++ = fp; 2073df8bae1dSRodney W. Grimes nunref++; 2074df8bae1dSRodney W. Grimes fp->f_count++; 2075df8bae1dSRodney W. Grimes } 2076426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 2077df8bae1dSRodney W. Grimes } 2078426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 2079ed5b7817SJulian Elischer /* 20801c381b19SRobert Watson * For each FD on our hit list, do the following two things: 2081ed5b7817SJulian Elischer */ 20821c7c3c6aSMatthew Dillon for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 20831c7c3c6aSMatthew Dillon struct file *tfp = *fpp; 2084426da3bcSAlfred Perlstein FILE_LOCK(tfp); 2085cd72f218SMatthew Dillon if (tfp->f_type == DTYPE_SOCKET && 208648e3128bSMatthew Dillon tfp->f_data != NULL) { 2087426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 208848e3128bSMatthew Dillon sorflush(tfp->f_data); 2089e5aeaa0cSDag-Erling Smørgrav } else { 2090426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 20911c7c3c6aSMatthew Dillon } 2092e5aeaa0cSDag-Erling Smørgrav } 2093a0ec558aSRobert Watson for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 2094b40ce416SJulian Elischer closef(*fpp, (struct thread *) NULL); 2095a0ec558aSRobert Watson unp_recycled++; 2096a0ec558aSRobert Watson } 2097210a5a71SAlfred Perlstein free(extra_ref, M_TEMP); 2098df8bae1dSRodney W. Grimes } 2099df8bae1dSRodney W. Grimes 210026f9a767SRodney W. Grimes void 2101892af6b9SRobert Watson unp_dispose(struct mbuf *m) 2102df8bae1dSRodney W. Grimes { 2103996c772fSJohn Dyson 2104df8bae1dSRodney W. Grimes if (m) 2105df8bae1dSRodney W. Grimes unp_scan(m, unp_discard); 2106df8bae1dSRodney W. Grimes } 2107df8bae1dSRodney W. Grimes 2108f708ef1bSPoul-Henning Kamp static void 2109892af6b9SRobert Watson unp_scan(struct mbuf *m0, void (*op)(struct file *)) 2110df8bae1dSRodney W. Grimes { 21112bc21ed9SDavid Malone struct mbuf *m; 21122bc21ed9SDavid Malone struct file **rp; 21132bc21ed9SDavid Malone struct cmsghdr *cm; 21142bc21ed9SDavid Malone void *data; 21152bc21ed9SDavid Malone int i; 21162bc21ed9SDavid Malone socklen_t clen, datalen; 2117df8bae1dSRodney W. Grimes int qfds; 2118df8bae1dSRodney W. Grimes 2119fc3fcacfSRobert Watson while (m0 != NULL) { 21202bc21ed9SDavid Malone for (m = m0; m; m = m->m_next) { 212112396bdcSDavid Malone if (m->m_type != MT_CONTROL) 2122df8bae1dSRodney W. Grimes continue; 21232bc21ed9SDavid Malone 21242bc21ed9SDavid Malone cm = mtod(m, struct cmsghdr *); 21252bc21ed9SDavid Malone clen = m->m_len; 21262bc21ed9SDavid Malone 21272bc21ed9SDavid Malone while (cm != NULL) { 21282bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) 21292bc21ed9SDavid Malone break; 21302bc21ed9SDavid Malone 21312bc21ed9SDavid Malone data = CMSG_DATA(cm); 21322bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len 21332bc21ed9SDavid Malone - (caddr_t)data; 21342bc21ed9SDavid Malone 21352bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET && 21362bc21ed9SDavid Malone cm->cmsg_type == SCM_RIGHTS) { 21372bc21ed9SDavid Malone qfds = datalen / sizeof (struct file *); 21382bc21ed9SDavid Malone rp = data; 2139df8bae1dSRodney W. Grimes for (i = 0; i < qfds; i++) 2140df8bae1dSRodney W. Grimes (*op)(*rp++); 21412bc21ed9SDavid Malone } 21422bc21ed9SDavid Malone 21432bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 21442bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 21452bc21ed9SDavid Malone cm = (struct cmsghdr *) 21462bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 21472bc21ed9SDavid Malone } else { 21482bc21ed9SDavid Malone clen = 0; 21492bc21ed9SDavid Malone cm = NULL; 21502bc21ed9SDavid Malone } 21512bc21ed9SDavid Malone } 2152df8bae1dSRodney W. Grimes } 2153df8bae1dSRodney W. Grimes m0 = m0->m_act; 2154df8bae1dSRodney W. Grimes } 2155df8bae1dSRodney W. Grimes } 2156df8bae1dSRodney W. Grimes 2157f708ef1bSPoul-Henning Kamp static void 2158892af6b9SRobert Watson unp_mark(struct file *fp) 2159df8bae1dSRodney W. Grimes { 2160e7c33e29SRobert Watson 2161e7c33e29SRobert Watson /* XXXRW: Should probably assert file list lock here. */ 2162e7c33e29SRobert Watson 2163426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) 2164df8bae1dSRodney W. Grimes return; 2165df8bae1dSRodney W. Grimes unp_defer++; 2166426da3bcSAlfred Perlstein fp->f_gcflag |= (FMARK|FDEFER); 2167df8bae1dSRodney W. Grimes } 2168df8bae1dSRodney W. Grimes 2169f708ef1bSPoul-Henning Kamp static void 2170892af6b9SRobert Watson unp_discard(struct file *fp) 2171df8bae1dSRodney W. Grimes { 2172e7c33e29SRobert Watson 2173e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 2174426da3bcSAlfred Perlstein FILE_LOCK(fp); 2175df8bae1dSRodney W. Grimes fp->f_msgcount--; 2176df8bae1dSRodney W. Grimes unp_rights--; 2177426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 2178e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 2179b40ce416SJulian Elischer (void) closef(fp, (struct thread *)NULL); 2180df8bae1dSRodney W. Grimes } 2181