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 56f23929fbSRobert Watson */ 57f23929fbSRobert Watson 58677b542eSDavid E. O'Brien #include <sys/cdefs.h> 59677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 60677b542eSDavid E. O'Brien 6103c96c31SRobert Watson #include "opt_ddb.h" 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 9403c96c31SRobert Watson #ifdef DDB 9503c96c31SRobert Watson #include <ddb/ddb.h> 9603c96c31SRobert Watson #endif 9703c96c31SRobert Watson 98aed55708SRobert Watson #include <security/mac/mac_framework.h> 99aed55708SRobert Watson 1009e9d298aSJeff Roberson #include <vm/uma.h> 10198271db4SGarrett Wollman 1029e9d298aSJeff Roberson static uma_zone_t unp_zone; 10398271db4SGarrett Wollman static unp_gen_t unp_gencnt; 104aea52f1bSRobert Watson static u_int unp_count; /* Count of local sockets. */ 105aea52f1bSRobert Watson static ino_t unp_ino; /* Prototype for fake inode numbers. */ 106aea52f1bSRobert Watson static int unp_rights; /* File descriptors in flight. */ 107aea52f1bSRobert Watson static struct unp_head unp_shead; /* List of local stream sockets. */ 108aea52f1bSRobert Watson static struct unp_head unp_dhead; /* List of local datagram sockets. */ 10998271db4SGarrett Wollman 110aea52f1bSRobert Watson static const struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 11198271db4SGarrett Wollman 112df8bae1dSRodney W. Grimes /* 113aea52f1bSRobert Watson * Garbage collection of cyclic file descriptor/socket references occurs 114aea52f1bSRobert Watson * asynchronously in a taskqueue context in order to avoid recursion and 115aea52f1bSRobert Watson * reentrance in the UNIX domain socket, file descriptor, and socket layer 116aea52f1bSRobert Watson * code. See unp_gc() for a full description. 117df8bae1dSRodney W. Grimes */ 118aea52f1bSRobert Watson static struct task unp_gc_task; 119f708ef1bSPoul-Henning Kamp 120ce5f32deSRobert Watson /* 1217e711c3aSRobert Watson * Both send and receive buffers are allocated PIPSIZ bytes of buffering for 1227e711c3aSRobert Watson * stream sockets, although the total for sender and receiver is actually 1237e711c3aSRobert Watson * only PIPSIZ. 1247e711c3aSRobert Watson * 1257e711c3aSRobert Watson * Datagram sockets really use the sendspace as the maximum datagram size, 1267e711c3aSRobert Watson * and don't really want to reserve the sendspace. Their recvspace should be 1277e711c3aSRobert Watson * large enough for at least one max-size datagram plus address. 1287e711c3aSRobert Watson */ 1297e711c3aSRobert Watson #ifndef PIPSIZ 1307e711c3aSRobert Watson #define PIPSIZ 8192 1317e711c3aSRobert Watson #endif 1327e711c3aSRobert Watson static u_long unpst_sendspace = PIPSIZ; 1337e711c3aSRobert Watson static u_long unpst_recvspace = PIPSIZ; 1347e711c3aSRobert Watson static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 1357e711c3aSRobert Watson static u_long unpdg_recvspace = 4*1024; 1367e711c3aSRobert Watson 137e4445a03SRobert Watson SYSCTL_NODE(_net, PF_LOCAL, local, CTLFLAG_RW, 0, "Local domain"); 138e4445a03SRobert Watson SYSCTL_NODE(_net_local, SOCK_STREAM, stream, CTLFLAG_RW, 0, "SOCK_STREAM"); 139e4445a03SRobert Watson SYSCTL_NODE(_net_local, SOCK_DGRAM, dgram, CTLFLAG_RW, 0, "SOCK_DGRAM"); 140e4445a03SRobert Watson 1417e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 142be6b1304STom Rhodes &unpst_sendspace, 0, "Default stream send space."); 1437e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 144be6b1304STom Rhodes &unpst_recvspace, 0, "Default stream receive space."); 1457e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 146be6b1304STom Rhodes &unpdg_sendspace, 0, "Default datagram send space."); 1477e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 148be6b1304STom Rhodes &unpdg_recvspace, 0, "Default datagram receive space."); 149be6b1304STom Rhodes SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, 150be6b1304STom Rhodes "File descriptors in flight."); 1517e711c3aSRobert Watson 152e7c33e29SRobert Watson /*- 153e7c33e29SRobert Watson * Locking and synchronization: 154ce5f32deSRobert Watson * 155e7c33e29SRobert Watson * The global UNIX domain socket rwlock (unp_global_rwlock) protects all 156e7c33e29SRobert Watson * global variables, including the linked lists tracking the set of allocated 157e7c33e29SRobert Watson * UNIX domain sockets. The global rwlock also serves to prevent deadlock 158e7c33e29SRobert Watson * when more than one PCB lock is acquired at a time (i.e., during 159e7c33e29SRobert Watson * connect()). Finally, the global rwlock protects uncounted references from 160e7c33e29SRobert Watson * vnodes to sockets bound to those vnodes: to safely dereference the 161e7c33e29SRobert Watson * v_socket pointer, the global rwlock must be held while a full reference is 162e7c33e29SRobert Watson * acquired. 163ce5f32deSRobert Watson * 164e7c33e29SRobert Watson * UNIX domain sockets each have an unpcb hung off of their so_pcb pointer, 165e7c33e29SRobert Watson * allocated in pru_attach() and freed in pru_detach(). The validity of that 166e7c33e29SRobert Watson * pointer is an invariant, so no lock is required to dereference the so_pcb 167e7c33e29SRobert Watson * pointer if a valid socket reference is held by the caller. In practice, 168e7c33e29SRobert Watson * this is always true during operations performed on a socket. Each unpcb 169e7c33e29SRobert Watson * has a back-pointer to its socket, unp_socket, which will be stable under 170e7c33e29SRobert Watson * the same circumstances. 171e7c33e29SRobert Watson * 172e7c33e29SRobert Watson * This pointer may only be safely dereferenced as long as a valid reference 173e7c33e29SRobert Watson * to the unpcb is held. Typically, this reference will be from the socket, 174e7c33e29SRobert Watson * or from another unpcb when the referring unpcb's lock is held (in order 175e7c33e29SRobert Watson * that the reference not be invalidated during use). For example, to follow 176e7c33e29SRobert Watson * unp->unp_conn->unp_socket, you need unlock the lock on unp, not unp_conn, 177e7c33e29SRobert Watson * as unp_socket remains valid as long as the reference to unp_conn is valid. 178e7c33e29SRobert Watson * 179e7c33e29SRobert Watson * Fields of unpcbss are locked using a per-unpcb lock, unp_mtx. Individual 180e7c33e29SRobert Watson * atomic reads without the lock may be performed "lockless", but more 181e7c33e29SRobert Watson * complex reads and read-modify-writes require the mutex to be held. No 182e7c33e29SRobert Watson * lock order is defined between unpcb locks -- multiple unpcb locks may be 183e7c33e29SRobert Watson * acquired at the same time only when holding the global UNIX domain socket 184e7c33e29SRobert Watson * rwlock exclusively, which prevents deadlocks. 185e7c33e29SRobert Watson * 186e7c33e29SRobert Watson * Blocking with UNIX domain sockets is a tricky issue: unlike most network 187e7c33e29SRobert Watson * protocols, bind() is a non-atomic operation, and connect() requires 188e7c33e29SRobert Watson * potential sleeping in the protocol, due to potentially waiting on local or 189e7c33e29SRobert Watson * distributed file systems. We try to separate "lookup" operations, which 190e7c33e29SRobert Watson * may sleep, and the IPC operations themselves, which typically can occur 191e7c33e29SRobert Watson * with relative atomicity as locks can be held over the entire operation. 192e7c33e29SRobert Watson * 193e7c33e29SRobert Watson * Another tricky issue is simultaneous multi-threaded or multi-process 194e7c33e29SRobert Watson * access to a single UNIX domain socket. These are handled by the flags 195e7c33e29SRobert Watson * UNP_CONNECTING and UNP_BINDING, which prevent concurrent connecting or 196e7c33e29SRobert Watson * binding, both of which involve dropping UNIX domain socket locks in order 197e7c33e29SRobert Watson * to perform namei() and other file system operations. 198ce5f32deSRobert Watson */ 199e7c33e29SRobert Watson static struct rwlock unp_global_rwlock; 200e7c33e29SRobert Watson 201e7c33e29SRobert Watson #define UNP_GLOBAL_LOCK_INIT() rw_init(&unp_global_rwlock, \ 202e7c33e29SRobert Watson "unp_global_rwlock") 203e7c33e29SRobert Watson 204e7c33e29SRobert Watson #define UNP_GLOBAL_LOCK_ASSERT() rw_assert(&unp_global_rwlock, \ 205e7c33e29SRobert Watson RA_LOCKED) 206e7c33e29SRobert Watson #define UNP_GLOBAL_UNLOCK_ASSERT() rw_assert(&unp_global_rwlock, \ 207e7c33e29SRobert Watson RA_UNLOCKED) 208e7c33e29SRobert Watson 209e7c33e29SRobert Watson #define UNP_GLOBAL_WLOCK() rw_wlock(&unp_global_rwlock) 210e7c33e29SRobert Watson #define UNP_GLOBAL_WUNLOCK() rw_wunlock(&unp_global_rwlock) 211e7c33e29SRobert Watson #define UNP_GLOBAL_WLOCK_ASSERT() rw_assert(&unp_global_rwlock, \ 212e7c33e29SRobert Watson RA_WLOCKED) 213e7c33e29SRobert Watson #define UNP_GLOBAL_WOWNED() rw_wowned(&unp_global_rwlock) 214e7c33e29SRobert Watson 215e7c33e29SRobert Watson #define UNP_GLOBAL_RLOCK() rw_rlock(&unp_global_rwlock) 216e7c33e29SRobert Watson #define UNP_GLOBAL_RUNLOCK() rw_runlock(&unp_global_rwlock) 217e7c33e29SRobert Watson #define UNP_GLOBAL_RLOCK_ASSERT() rw_assert(&unp_global_rwlock, \ 218e7c33e29SRobert Watson RA_RLOCKED) 219e7c33e29SRobert Watson 220e7c33e29SRobert Watson #define UNP_PCB_LOCK_INIT(unp) mtx_init(&(unp)->unp_mtx, \ 221e7c33e29SRobert Watson "unp_mtx", "unp_mtx", \ 222e7c33e29SRobert Watson MTX_DUPOK|MTX_DEF|MTX_RECURSE) 223e7c33e29SRobert Watson #define UNP_PCB_LOCK_DESTROY(unp) mtx_destroy(&(unp)->unp_mtx) 224e7c33e29SRobert Watson #define UNP_PCB_LOCK(unp) mtx_lock(&(unp)->unp_mtx) 225e7c33e29SRobert Watson #define UNP_PCB_UNLOCK(unp) mtx_unlock(&(unp)->unp_mtx) 226e7c33e29SRobert Watson #define UNP_PCB_LOCK_ASSERT(unp) mtx_assert(&(unp)->unp_mtx, MA_OWNED) 2270d9ce3a1SRobert Watson 228aea52f1bSRobert Watson static int unp_connect(struct socket *, struct sockaddr *, 229aea52f1bSRobert Watson struct thread *); 2306a2989fdSMatthew N. Dodd static int unp_connect2(struct socket *so, struct socket *so2, int); 231e7c33e29SRobert Watson static void unp_disconnect(struct unpcb *unp, struct unpcb *unp2); 2324d77a549SAlfred Perlstein static void unp_shutdown(struct unpcb *); 2334d77a549SAlfred Perlstein static void unp_drop(struct unpcb *, int); 234a0ec558aSRobert Watson static void unp_gc(__unused void *, int); 2354d77a549SAlfred Perlstein static void unp_scan(struct mbuf *, void (*)(struct file *)); 2364d77a549SAlfred Perlstein static void unp_discard(struct file *); 2374d77a549SAlfred Perlstein static void unp_freerights(struct file **, int); 2384d77a549SAlfred Perlstein static int unp_internalize(struct mbuf **, struct thread *); 239397c19d1SJeff Roberson static void unp_internalize_fp(struct file *); 240397c19d1SJeff Roberson static void unp_externalize_fp(struct file *); 2415b950deaSRobert Watson static struct mbuf *unp_addsockcred(struct thread *, struct mbuf *); 242f708ef1bSPoul-Henning Kamp 243e4445a03SRobert Watson /* 244e4445a03SRobert Watson * Definitions of protocols supported in the LOCAL domain. 245e4445a03SRobert Watson */ 246e4445a03SRobert Watson static struct domain localdomain; 247e4445a03SRobert Watson static struct protosw localsw[] = { 248e4445a03SRobert Watson { 249e4445a03SRobert Watson .pr_type = SOCK_STREAM, 250e4445a03SRobert Watson .pr_domain = &localdomain, 251e4445a03SRobert Watson .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_RIGHTS, 252e4445a03SRobert Watson .pr_ctloutput = &uipc_ctloutput, 253e4445a03SRobert Watson .pr_usrreqs = &uipc_usrreqs 254e4445a03SRobert Watson }, 255e4445a03SRobert Watson { 256e4445a03SRobert Watson .pr_type = SOCK_DGRAM, 257e4445a03SRobert Watson .pr_domain = &localdomain, 258e4445a03SRobert Watson .pr_flags = PR_ATOMIC|PR_ADDR|PR_RIGHTS, 259e4445a03SRobert Watson .pr_usrreqs = &uipc_usrreqs 260e4445a03SRobert Watson }, 261e4445a03SRobert Watson }; 262e4445a03SRobert Watson 263e4445a03SRobert Watson static struct domain localdomain = { 264e4445a03SRobert Watson .dom_family = AF_LOCAL, 265e4445a03SRobert Watson .dom_name = "local", 266e4445a03SRobert Watson .dom_init = unp_init, 267e4445a03SRobert Watson .dom_externalize = unp_externalize, 268e4445a03SRobert Watson .dom_dispose = unp_dispose, 269e4445a03SRobert Watson .dom_protosw = localsw, 270e4445a03SRobert Watson .dom_protoswNPROTOSW = &localsw[sizeof(localsw)/sizeof(localsw[0])] 271e4445a03SRobert Watson }; 272e4445a03SRobert Watson DOMAIN_SET(local); 273e4445a03SRobert Watson 274ac45e92fSRobert Watson static void 275a29f300eSGarrett Wollman uipc_abort(struct socket *so) 276df8bae1dSRodney W. Grimes { 277e7c33e29SRobert Watson struct unpcb *unp, *unp2; 278df8bae1dSRodney W. Grimes 27940f2ac28SRobert Watson unp = sotounpcb(so); 2804d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_abort: unp == NULL")); 281e7c33e29SRobert Watson 282e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 283e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 284e7c33e29SRobert Watson unp2 = unp->unp_conn; 285e7c33e29SRobert Watson if (unp2 != NULL) { 286e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 287e7c33e29SRobert Watson unp_drop(unp2, ECONNABORTED); 288e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 289e7c33e29SRobert Watson } 290e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 291e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 292df8bae1dSRodney W. Grimes } 293df8bae1dSRodney W. Grimes 294a29f300eSGarrett Wollman static int 29557bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam) 296a29f300eSGarrett Wollman { 297e7c33e29SRobert Watson struct unpcb *unp, *unp2; 2980d9ce3a1SRobert Watson const struct sockaddr *sa; 299df8bae1dSRodney W. Grimes 300df8bae1dSRodney W. Grimes /* 3011c381b19SRobert Watson * Pass back name of connected socket, if it was bound and we are 3021c381b19SRobert Watson * still connected (our peer may have closed already!). 303df8bae1dSRodney W. Grimes */ 3044d4b555eSRobert Watson unp = sotounpcb(so); 3054d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_accept: unp == NULL")); 306e7c33e29SRobert Watson 3070d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 308e7c33e29SRobert Watson UNP_GLOBAL_RLOCK(); 309e7c33e29SRobert Watson unp2 = unp->unp_conn; 310e7c33e29SRobert Watson if (unp2 != NULL && unp2->unp_addr != NULL) { 311e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 312e7c33e29SRobert Watson sa = (struct sockaddr *) unp2->unp_addr; 313e7c33e29SRobert Watson bcopy(sa, *nam, sa->sa_len); 314e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 315e7c33e29SRobert Watson } else { 3160d9ce3a1SRobert Watson sa = &sun_noname; 3170d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 318e7c33e29SRobert Watson } 319e7c33e29SRobert Watson UNP_GLOBAL_RUNLOCK(); 320e5aeaa0cSDag-Erling Smørgrav return (0); 321a29f300eSGarrett Wollman } 322df8bae1dSRodney W. Grimes 323a29f300eSGarrett Wollman static int 324b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td) 325a29f300eSGarrett Wollman { 326e7c33e29SRobert Watson u_long sendspace, recvspace; 3276d32873cSRobert Watson struct unpcb *unp; 328e7c33e29SRobert Watson int error, locked; 329df8bae1dSRodney W. Grimes 3306d32873cSRobert Watson KASSERT(so->so_pcb == NULL, ("uipc_attach: so_pcb != NULL")); 3316d32873cSRobert Watson if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 3326d32873cSRobert Watson switch (so->so_type) { 3336d32873cSRobert Watson case SOCK_STREAM: 334e7c33e29SRobert Watson sendspace = unpst_sendspace; 335e7c33e29SRobert Watson recvspace = unpst_recvspace; 3366d32873cSRobert Watson break; 3376d32873cSRobert Watson 3386d32873cSRobert Watson case SOCK_DGRAM: 339e7c33e29SRobert Watson sendspace = unpdg_sendspace; 340e7c33e29SRobert Watson recvspace = unpdg_recvspace; 3416d32873cSRobert Watson break; 3426d32873cSRobert Watson 3436d32873cSRobert Watson default: 344e7c33e29SRobert Watson panic("uipc_attach"); 3456d32873cSRobert Watson } 346e7c33e29SRobert Watson error = soreserve(so, sendspace, recvspace); 3476d32873cSRobert Watson if (error) 3486d32873cSRobert Watson return (error); 3496d32873cSRobert Watson } 35046a1d9bfSRobert Watson unp = uma_zalloc(unp_zone, M_NOWAIT | M_ZERO); 3516d32873cSRobert Watson if (unp == NULL) 3526d32873cSRobert Watson return (ENOBUFS); 3536d32873cSRobert Watson LIST_INIT(&unp->unp_refs); 354e7c33e29SRobert Watson UNP_PCB_LOCK_INIT(unp); 3556d32873cSRobert Watson unp->unp_socket = so; 3566d32873cSRobert Watson so->so_pcb = unp; 3579ae328fcSJohn Baldwin unp->unp_refcount = 1; 358e7c33e29SRobert Watson 359e7c33e29SRobert Watson /* 360e7c33e29SRobert Watson * uipc_attach() may be called indirectly from within the UNIX domain 361e7c33e29SRobert Watson * socket code via sonewconn() in unp_connect(). Since rwlocks can 362e7c33e29SRobert Watson * not be recursed, we do the closest thing. 363e7c33e29SRobert Watson */ 364d7924b70SRobert Watson locked = 0; 365e7c33e29SRobert Watson if (!UNP_GLOBAL_WOWNED()) { 366e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 367e7c33e29SRobert Watson locked = 1; 368e7c33e29SRobert Watson } 3696d32873cSRobert Watson unp->unp_gencnt = ++unp_gencnt; 3706d32873cSRobert Watson unp_count++; 371b7e2f3ecSRobert Watson LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead : &unp_shead, 372b7e2f3ecSRobert Watson unp, unp_link); 373e7c33e29SRobert Watson if (locked) 374e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 3756d32873cSRobert Watson 3766d32873cSRobert Watson return (0); 377a29f300eSGarrett Wollman } 378a29f300eSGarrett Wollman 379a29f300eSGarrett Wollman static int 380b40ce416SJulian Elischer uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 381a29f300eSGarrett Wollman { 382dd47f5caSRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam; 383dd47f5caSRobert Watson struct vattr vattr; 3849e289446SWojciech A. Koszek int error, namelen, vfslocked; 385dd47f5caSRobert Watson struct nameidata nd; 38640f2ac28SRobert Watson struct unpcb *unp; 387dd47f5caSRobert Watson struct vnode *vp; 388dd47f5caSRobert Watson struct mount *mp; 389dd47f5caSRobert Watson char *buf; 390a29f300eSGarrett Wollman 39140f2ac28SRobert Watson unp = sotounpcb(so); 3924d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_bind: unp == NULL")); 3934f1f0ef5SRobert Watson 3944f1f0ef5SRobert Watson namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 3954f1f0ef5SRobert Watson if (namelen <= 0) 3964f1f0ef5SRobert Watson return (EINVAL); 397dd47f5caSRobert Watson 398dd47f5caSRobert Watson /* 3994f1f0ef5SRobert Watson * We don't allow simultaneous bind() calls on a single UNIX domain 4004f1f0ef5SRobert Watson * socket, so flag in-progress operations, and return an error if an 4014f1f0ef5SRobert Watson * operation is already in progress. 4024f1f0ef5SRobert Watson * 4034f1f0ef5SRobert Watson * Historically, we have not allowed a socket to be rebound, so this 404d7924b70SRobert Watson * also returns an error. Not allowing re-binding simplifies the 405d7924b70SRobert Watson * implementation and avoids a great many possible failure modes. 406dd47f5caSRobert Watson */ 407e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 408dd47f5caSRobert Watson if (unp->unp_vnode != NULL) { 409e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 410dd47f5caSRobert Watson return (EINVAL); 411dd47f5caSRobert Watson } 4124f1f0ef5SRobert Watson if (unp->unp_flags & UNP_BINDING) { 413e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 4144f1f0ef5SRobert Watson return (EALREADY); 415dd47f5caSRobert Watson } 4164f1f0ef5SRobert Watson unp->unp_flags |= UNP_BINDING; 417e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 418dd47f5caSRobert Watson 419dd47f5caSRobert Watson buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 4207928893dSEd Maste bcopy(soun->sun_path, buf, namelen); 4217928893dSEd Maste buf[namelen] = 0; 422dd47f5caSRobert Watson 423dd47f5caSRobert Watson restart: 4249e289446SWojciech A. Koszek vfslocked = 0; 4259e289446SWojciech A. Koszek NDINIT(&nd, CREATE, MPSAFE | NOFOLLOW | LOCKPARENT | SAVENAME, 4269e289446SWojciech A. Koszek UIO_SYSSPACE, buf, td); 427dd47f5caSRobert Watson /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 428dd47f5caSRobert Watson error = namei(&nd); 429dd47f5caSRobert Watson if (error) 4304f1f0ef5SRobert Watson goto error; 431dd47f5caSRobert Watson vp = nd.ni_vp; 4329e289446SWojciech A. Koszek vfslocked = NDHASGIANT(&nd); 433dd47f5caSRobert Watson if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 434dd47f5caSRobert Watson NDFREE(&nd, NDF_ONLY_PNBUF); 435dd47f5caSRobert Watson if (nd.ni_dvp == vp) 436dd47f5caSRobert Watson vrele(nd.ni_dvp); 437dd47f5caSRobert Watson else 438dd47f5caSRobert Watson vput(nd.ni_dvp); 439dd47f5caSRobert Watson if (vp != NULL) { 440dd47f5caSRobert Watson vrele(vp); 441dd47f5caSRobert Watson error = EADDRINUSE; 4424f1f0ef5SRobert Watson goto error; 443dd47f5caSRobert Watson } 444dd47f5caSRobert Watson error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 445dd47f5caSRobert Watson if (error) 4464f1f0ef5SRobert Watson goto error; 4479e289446SWojciech A. Koszek VFS_UNLOCK_GIANT(vfslocked); 448dd47f5caSRobert Watson goto restart; 449dd47f5caSRobert Watson } 450dd47f5caSRobert Watson VATTR_NULL(&vattr); 451dd47f5caSRobert Watson vattr.va_type = VSOCK; 452dd47f5caSRobert Watson vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 453dd47f5caSRobert Watson #ifdef MAC 45430d239bcSRobert Watson error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 455dd47f5caSRobert Watson &vattr); 456dd47f5caSRobert Watson #endif 457dd47f5caSRobert Watson if (error == 0) { 458dd47f5caSRobert Watson VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE); 459dd47f5caSRobert Watson error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 460dd47f5caSRobert Watson } 461dd47f5caSRobert Watson NDFREE(&nd, NDF_ONLY_PNBUF); 462dd47f5caSRobert Watson vput(nd.ni_dvp); 463dd47f5caSRobert Watson if (error) { 464dd47f5caSRobert Watson vn_finished_write(mp); 4654f1f0ef5SRobert Watson goto error; 466dd47f5caSRobert Watson } 467dd47f5caSRobert Watson vp = nd.ni_vp; 46857fd3d55SPawel Jakub Dawidek ASSERT_VOP_ELOCKED(vp, "uipc_bind"); 469dd47f5caSRobert Watson soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 470e7c33e29SRobert Watson 471e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 472e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 473dd47f5caSRobert Watson vp->v_socket = unp->unp_socket; 474dd47f5caSRobert Watson unp->unp_vnode = vp; 475dd47f5caSRobert Watson unp->unp_addr = soun; 4764f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_BINDING; 477e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 478e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 47922db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 480dd47f5caSRobert Watson vn_finished_write(mp); 4819e289446SWojciech A. Koszek VFS_UNLOCK_GIANT(vfslocked); 4824f1f0ef5SRobert Watson free(buf, M_TEMP); 4834f1f0ef5SRobert Watson return (0); 484e7c33e29SRobert Watson 4854f1f0ef5SRobert Watson error: 4869e289446SWojciech A. Koszek VFS_UNLOCK_GIANT(vfslocked); 487e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 4884f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_BINDING; 489e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 490dd47f5caSRobert Watson free(buf, M_TEMP); 49140f2ac28SRobert Watson return (error); 492a29f300eSGarrett Wollman } 493a29f300eSGarrett Wollman 494a29f300eSGarrett Wollman static int 495b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 496a29f300eSGarrett Wollman { 4970d9ce3a1SRobert Watson int error; 498a29f300eSGarrett Wollman 499fd179ee9SRobert Watson KASSERT(td == curthread, ("uipc_connect: td != curthread")); 500e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 501fd179ee9SRobert Watson error = unp_connect(so, nam, td); 502e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 5030d9ce3a1SRobert Watson return (error); 504a29f300eSGarrett Wollman } 505a29f300eSGarrett Wollman 506a152f8a3SRobert Watson static void 507a152f8a3SRobert Watson uipc_close(struct socket *so) 508a152f8a3SRobert Watson { 509e7c33e29SRobert Watson struct unpcb *unp, *unp2; 510a152f8a3SRobert Watson 511a152f8a3SRobert Watson unp = sotounpcb(so); 512a152f8a3SRobert Watson KASSERT(unp != NULL, ("uipc_close: unp == NULL")); 513e7c33e29SRobert Watson 514e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 515e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 516e7c33e29SRobert Watson unp2 = unp->unp_conn; 517e7c33e29SRobert Watson if (unp2 != NULL) { 518e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 519e7c33e29SRobert Watson unp_disconnect(unp, unp2); 520e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 521e7c33e29SRobert Watson } 522e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 523e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 524a152f8a3SRobert Watson } 525a152f8a3SRobert Watson 526db48c0d2SRobert Watson int 527a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2) 528a29f300eSGarrett Wollman { 529e7c33e29SRobert Watson struct unpcb *unp, *unp2; 5300d9ce3a1SRobert Watson int error; 531a29f300eSGarrett Wollman 532e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 533e7c33e29SRobert Watson unp = so1->so_pcb; 5344d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_connect2: unp == NULL")); 535e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 536e7c33e29SRobert Watson unp2 = so2->so_pcb; 537e7c33e29SRobert Watson KASSERT(unp2 != NULL, ("uipc_connect2: unp2 == NULL")); 538e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 5396a2989fdSMatthew N. Dodd error = unp_connect2(so1, so2, PRU_CONNECT2); 540e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 541e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 542e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 5430d9ce3a1SRobert Watson return (error); 544a29f300eSGarrett Wollman } 545a29f300eSGarrett Wollman 546a29f300eSGarrett Wollman /* control is EOPNOTSUPP */ 547a29f300eSGarrett Wollman 548bc725eafSRobert Watson static void 549a29f300eSGarrett Wollman uipc_detach(struct socket *so) 550a29f300eSGarrett Wollman { 551e7c33e29SRobert Watson struct unpcb *unp, *unp2; 5529ae328fcSJohn Baldwin struct sockaddr_un *saved_unp_addr; 5536d32873cSRobert Watson struct vnode *vp; 5549ae328fcSJohn Baldwin int freeunp, local_unp_rights; 555a29f300eSGarrett Wollman 55640f2ac28SRobert Watson unp = sotounpcb(so); 5574d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_detach: unp == NULL")); 558e7c33e29SRobert Watson 559e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 560e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 561e7c33e29SRobert Watson 5626d32873cSRobert Watson LIST_REMOVE(unp, unp_link); 5636d32873cSRobert Watson unp->unp_gencnt = ++unp_gencnt; 5646d32873cSRobert Watson --unp_count; 565e7c33e29SRobert Watson 566e7c33e29SRobert Watson /* 567e7c33e29SRobert Watson * XXXRW: Should assert vp->v_socket == so. 568e7c33e29SRobert Watson */ 5696d32873cSRobert Watson if ((vp = unp->unp_vnode) != NULL) { 5706d32873cSRobert Watson unp->unp_vnode->v_socket = NULL; 5716d32873cSRobert Watson unp->unp_vnode = NULL; 5726d32873cSRobert Watson } 573e7c33e29SRobert Watson unp2 = unp->unp_conn; 574e7c33e29SRobert Watson if (unp2 != NULL) { 575e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 576e7c33e29SRobert Watson unp_disconnect(unp, unp2); 577e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 578e7c33e29SRobert Watson } 579e7c33e29SRobert Watson 580e7c33e29SRobert Watson /* 581e7c33e29SRobert Watson * We hold the global lock, so it's OK to acquire multiple pcb locks 582e7c33e29SRobert Watson * at a time. 583e7c33e29SRobert Watson */ 5846d32873cSRobert Watson while (!LIST_EMPTY(&unp->unp_refs)) { 5856d32873cSRobert Watson struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 586e7c33e29SRobert Watson 587e7c33e29SRobert Watson UNP_PCB_LOCK(ref); 5886d32873cSRobert Watson unp_drop(ref, ECONNRESET); 589e7c33e29SRobert Watson UNP_PCB_UNLOCK(ref); 5906d32873cSRobert Watson } 591397c19d1SJeff Roberson local_unp_rights = unp_rights; 592e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 5936d32873cSRobert Watson unp->unp_socket->so_pcb = NULL; 5949ae328fcSJohn Baldwin saved_unp_addr = unp->unp_addr; 5959ae328fcSJohn Baldwin unp->unp_addr = NULL; 5969ae328fcSJohn Baldwin unp->unp_refcount--; 5979ae328fcSJohn Baldwin freeunp = (unp->unp_refcount == 0); 5989ae328fcSJohn Baldwin if (saved_unp_addr != NULL) 5999ae328fcSJohn Baldwin FREE(saved_unp_addr, M_SONAME); 600e7c33e29SRobert Watson if (freeunp) { 601e7c33e29SRobert Watson UNP_PCB_LOCK_DESTROY(unp); 6026d32873cSRobert Watson uma_zfree(unp_zone, unp); 6036e2faa24SRobert Watson } else 6046e2faa24SRobert Watson UNP_PCB_UNLOCK(unp); 6056d32873cSRobert Watson if (vp) { 6066d32873cSRobert Watson int vfslocked; 6076d32873cSRobert Watson 6086d32873cSRobert Watson vfslocked = VFS_LOCK_GIANT(vp->v_mount); 6096d32873cSRobert Watson vrele(vp); 6106d32873cSRobert Watson VFS_UNLOCK_GIANT(vfslocked); 6116d32873cSRobert Watson } 6126d32873cSRobert Watson if (local_unp_rights) 6136d32873cSRobert Watson taskqueue_enqueue(taskqueue_thread, &unp_gc_task); 614a29f300eSGarrett Wollman } 615a29f300eSGarrett Wollman 616a29f300eSGarrett Wollman static int 617a29f300eSGarrett Wollman uipc_disconnect(struct socket *so) 618a29f300eSGarrett Wollman { 619e7c33e29SRobert Watson struct unpcb *unp, *unp2; 620a29f300eSGarrett Wollman 62140f2ac28SRobert Watson unp = sotounpcb(so); 6224d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_disconnect: unp == NULL")); 623e7c33e29SRobert Watson 624e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 625e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 626e7c33e29SRobert Watson unp2 = unp->unp_conn; 627e7c33e29SRobert Watson if (unp2 != NULL) { 628e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 629e7c33e29SRobert Watson unp_disconnect(unp, unp2); 630e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 631e7c33e29SRobert Watson } 632e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 633e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 634e5aeaa0cSDag-Erling Smørgrav return (0); 635a29f300eSGarrett Wollman } 636a29f300eSGarrett Wollman 637a29f300eSGarrett Wollman static int 638d374e81eSRobert Watson uipc_listen(struct socket *so, int backlog, struct thread *td) 639a29f300eSGarrett Wollman { 64040f2ac28SRobert Watson struct unpcb *unp; 6410d9ce3a1SRobert Watson int error; 642a29f300eSGarrett Wollman 64340f2ac28SRobert Watson unp = sotounpcb(so); 6444d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_listen: unp == NULL")); 645e7c33e29SRobert Watson 646e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 6474d4b555eSRobert Watson if (unp->unp_vnode == NULL) { 648e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 64940f2ac28SRobert Watson return (EINVAL); 65040f2ac28SRobert Watson } 651e7c33e29SRobert Watson 652e7c33e29SRobert Watson SOCK_LOCK(so); 653e7c33e29SRobert Watson error = solisten_proto_check(so); 654e7c33e29SRobert Watson if (error == 0) { 655e7c33e29SRobert Watson cru2x(td->td_ucred, &unp->unp_peercred); 656e7c33e29SRobert Watson unp->unp_flags |= UNP_HAVEPCCACHED; 657e7c33e29SRobert Watson solisten_proto(so, backlog); 658e7c33e29SRobert Watson } 659e7c33e29SRobert Watson SOCK_UNLOCK(so); 660e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 6610d9ce3a1SRobert Watson return (error); 662a29f300eSGarrett Wollman } 663a29f300eSGarrett Wollman 664a29f300eSGarrett Wollman static int 66557bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 666a29f300eSGarrett Wollman { 667e7c33e29SRobert Watson struct unpcb *unp, *unp2; 6680d9ce3a1SRobert Watson const struct sockaddr *sa; 669a29f300eSGarrett Wollman 6704d4b555eSRobert Watson unp = sotounpcb(so); 6714d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL")); 672e7c33e29SRobert Watson 6730d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 674e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 675bdc5f6a3SHajimu UMEMOTO /* 676e7c33e29SRobert Watson * XXX: It seems that this test always fails even when connection is 677e7c33e29SRobert Watson * established. So, this else clause is added as workaround to 678e7c33e29SRobert Watson * return PF_LOCAL sockaddr. 679bdc5f6a3SHajimu UMEMOTO */ 680e7c33e29SRobert Watson unp2 = unp->unp_conn; 681e7c33e29SRobert Watson if (unp2 != NULL) { 682e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 683e7c33e29SRobert Watson if (unp2->unp_addr != NULL) 684e7c33e29SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 685e7c33e29SRobert Watson else 6860d9ce3a1SRobert Watson sa = &sun_noname; 6870d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 688e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 689e7c33e29SRobert Watson } else { 690e7c33e29SRobert Watson sa = &sun_noname; 691e7c33e29SRobert Watson bcopy(sa, *nam, sa->sa_len); 692e7c33e29SRobert Watson } 693e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 694e5aeaa0cSDag-Erling Smørgrav return (0); 695a29f300eSGarrett Wollman } 696a29f300eSGarrett Wollman 697a29f300eSGarrett Wollman static int 698a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 699a29f300eSGarrett Wollman { 700e7c33e29SRobert Watson struct unpcb *unp, *unp2; 701a29f300eSGarrett Wollman struct socket *so2; 702337cc6b6SRobert Watson u_int mbcnt, sbcc; 7036aef685fSBrian Feldman u_long newhiwat; 704a29f300eSGarrett Wollman 70540f2ac28SRobert Watson unp = sotounpcb(so); 7064d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_rcvd: unp == NULL")); 707df8bae1dSRodney W. Grimes 708e7c33e29SRobert Watson if (so->so_type == SOCK_DGRAM) 709e7c33e29SRobert Watson panic("uipc_rcvd DGRAM?"); 710e7c33e29SRobert Watson 711e7c33e29SRobert Watson if (so->so_type != SOCK_STREAM) 712e7c33e29SRobert Watson panic("uipc_rcvd unknown socktype"); 713e7c33e29SRobert Watson 714df8bae1dSRodney W. Grimes /* 715e7c33e29SRobert Watson * Adjust backpressure on sender and wakeup any waiting to write. 716e7c33e29SRobert Watson * 717d7924b70SRobert Watson * The unp lock is acquired to maintain the validity of the unp_conn 718d7924b70SRobert Watson * pointer; no lock on unp2 is required as unp2->unp_socket will be 719d7924b70SRobert Watson * static as long as we don't permit unp2 to disconnect from unp, 720d7924b70SRobert Watson * which is prevented by the lock on unp. We cache values from 721d7924b70SRobert Watson * so_rcv to avoid holding the so_rcv lock over the entire 722d7924b70SRobert Watson * transaction on the remote so_snd. 723df8bae1dSRodney W. Grimes */ 724337cc6b6SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 725337cc6b6SRobert Watson mbcnt = so->so_rcv.sb_mbcnt; 726337cc6b6SRobert Watson sbcc = so->so_rcv.sb_cc; 727337cc6b6SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 728e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 729e7c33e29SRobert Watson unp2 = unp->unp_conn; 730e7c33e29SRobert Watson if (unp2 == NULL) { 731e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 732e7c33e29SRobert Watson return (0); 733337cc6b6SRobert Watson } 734e7c33e29SRobert Watson so2 = unp2->unp_socket; 735337cc6b6SRobert Watson SOCKBUF_LOCK(&so2->so_snd); 736337cc6b6SRobert Watson so2->so_snd.sb_mbmax += unp->unp_mbcnt - mbcnt; 737337cc6b6SRobert Watson newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - sbcc; 738f535380cSDon Lewis (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat, 7396aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 7401e4d7da7SRobert Watson sowwakeup_locked(so2); 741337cc6b6SRobert Watson unp->unp_mbcnt = mbcnt; 742337cc6b6SRobert Watson unp->unp_cc = sbcc; 743e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 744e5aeaa0cSDag-Erling Smørgrav return (0); 745a29f300eSGarrett Wollman } 746df8bae1dSRodney W. Grimes 747a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 748a29f300eSGarrett Wollman 749a29f300eSGarrett Wollman static int 75057bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 751b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 752a29f300eSGarrett Wollman { 753f3f49bbbSRobert Watson struct unpcb *unp, *unp2; 754a29f300eSGarrett Wollman struct socket *so2; 755337cc6b6SRobert Watson u_int mbcnt, sbcc; 7566aef685fSBrian Feldman u_long newhiwat; 757f3f49bbbSRobert Watson int error = 0; 758a29f300eSGarrett Wollman 75940f2ac28SRobert Watson unp = sotounpcb(so); 7604d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_send: unp == NULL")); 761e7c33e29SRobert Watson 762a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 763a29f300eSGarrett Wollman error = EOPNOTSUPP; 764a29f300eSGarrett Wollman goto release; 765a29f300eSGarrett Wollman } 766a29f300eSGarrett Wollman 767fc3fcacfSRobert Watson if (control != NULL && (error = unp_internalize(&control, td))) 768a29f300eSGarrett Wollman goto release; 769df8bae1dSRodney W. Grimes 770e7c33e29SRobert Watson if ((nam != NULL) || (flags & PRUS_EOF)) 771e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 772e7c33e29SRobert Watson else 773e7c33e29SRobert Watson UNP_GLOBAL_RLOCK(); 774e7c33e29SRobert Watson 775a29f300eSGarrett Wollman switch (so->so_type) { 776a29f300eSGarrett Wollman case SOCK_DGRAM: 777a29f300eSGarrett Wollman { 778e7dd9a10SRobert Watson const struct sockaddr *from; 779df8bae1dSRodney W. Grimes 780e7c33e29SRobert Watson unp2 = unp->unp_conn; 781fc3fcacfSRobert Watson if (nam != NULL) { 782ede6e136SRobert Watson UNP_GLOBAL_WLOCK_ASSERT(); 783e7c33e29SRobert Watson if (unp2 != NULL) { 784df8bae1dSRodney W. Grimes error = EISCONN; 785df8bae1dSRodney W. Grimes break; 786df8bae1dSRodney W. Grimes } 787b40ce416SJulian Elischer error = unp_connect(so, nam, td); 788df8bae1dSRodney W. Grimes if (error) 789df8bae1dSRodney W. Grimes break; 790e7c33e29SRobert Watson unp2 = unp->unp_conn; 791df8bae1dSRodney W. Grimes } 792b5ff0914SRobert Watson /* 793b5ff0914SRobert Watson * Because connect() and send() are non-atomic in a sendto() 794b5ff0914SRobert Watson * with a target address, it's possible that the socket will 795b5ff0914SRobert Watson * have disconnected before the send() can run. In that case 796b5ff0914SRobert Watson * return the slightly counter-intuitive but otherwise 797b5ff0914SRobert Watson * correct error that the socket is not connected. 798b5ff0914SRobert Watson */ 799b5ff0914SRobert Watson if (unp2 == NULL) { 800b5ff0914SRobert Watson error = ENOTCONN; 801b5ff0914SRobert Watson break; 802b5ff0914SRobert Watson } 803ede6e136SRobert Watson /* Lockless read. */ 804ede6e136SRobert Watson if (unp2->unp_flags & UNP_WANTCRED) 805ede6e136SRobert Watson control = unp_addsockcred(td, control); 806ede6e136SRobert Watson UNP_PCB_LOCK(unp); 807fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 80857bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 809df8bae1dSRodney W. Grimes else 810df8bae1dSRodney W. Grimes from = &sun_noname; 811ede6e136SRobert Watson so2 = unp2->unp_socket; 812a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 813a34b7046SRobert Watson if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) { 8141e4d7da7SRobert Watson sorwakeup_locked(so2); 815fc3fcacfSRobert Watson m = NULL; 816fc3fcacfSRobert Watson control = NULL; 817e5aeaa0cSDag-Erling Smørgrav } else { 818a34b7046SRobert Watson SOCKBUF_UNLOCK(&so2->so_rcv); 819df8bae1dSRodney W. Grimes error = ENOBUFS; 820e5aeaa0cSDag-Erling Smørgrav } 821ede6e136SRobert Watson if (nam != NULL) { 822ede6e136SRobert Watson UNP_GLOBAL_WLOCK_ASSERT(); 823ede6e136SRobert Watson UNP_PCB_LOCK(unp2); 824e7c33e29SRobert Watson unp_disconnect(unp, unp2); 825e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 826ede6e136SRobert Watson } 827ede6e136SRobert Watson UNP_PCB_UNLOCK(unp); 828df8bae1dSRodney W. Grimes break; 829df8bae1dSRodney W. Grimes } 830df8bae1dSRodney W. Grimes 831df8bae1dSRodney W. Grimes case SOCK_STREAM: 8326b8fda4dSGarrett Wollman /* 8331c381b19SRobert Watson * Connect if not connected yet. 8341c381b19SRobert Watson * 8351c381b19SRobert Watson * Note: A better implementation would complain if not equal 8361c381b19SRobert Watson * to the peer's address. 8376b8fda4dSGarrett Wollman */ 838402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 839fc3fcacfSRobert Watson if (nam != NULL) { 840ede6e136SRobert Watson UNP_GLOBAL_WLOCK_ASSERT(); 841b40ce416SJulian Elischer error = unp_connect(so, nam, td); 842402cc72dSDavid Greenman if (error) 8436b8fda4dSGarrett Wollman break; /* XXX */ 844402cc72dSDavid Greenman } else { 845402cc72dSDavid Greenman error = ENOTCONN; 846402cc72dSDavid Greenman break; 847402cc72dSDavid Greenman } 848ede6e136SRobert Watson } 849402cc72dSDavid Greenman 850337cc6b6SRobert Watson /* Lockless read. */ 851c0b99ffaSRobert Watson if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 852df8bae1dSRodney W. Grimes error = EPIPE; 853df8bae1dSRodney W. Grimes break; 854df8bae1dSRodney W. Grimes } 855b5ff0914SRobert Watson /* 856b5ff0914SRobert Watson * Because connect() and send() are non-atomic in a sendto() 857b5ff0914SRobert Watson * with a target address, it's possible that the socket will 858b5ff0914SRobert Watson * have disconnected before the send() can run. In that case 859b5ff0914SRobert Watson * return the slightly counter-intuitive but otherwise 860b5ff0914SRobert Watson * correct error that the socket is not connected. 861e7c33e29SRobert Watson * 862d7924b70SRobert Watson * Locking here must be done carefully: the global lock 863d7924b70SRobert Watson * prevents interconnections between unpcbs from changing, so 864d7924b70SRobert Watson * we can traverse from unp to unp2 without acquiring unp's 865d7924b70SRobert Watson * lock. Socket buffer locks follow unpcb locks, so we can 866d7924b70SRobert Watson * acquire both remote and lock socket buffer locks. 867b5ff0914SRobert Watson */ 868f3f49bbbSRobert Watson unp2 = unp->unp_conn; 869b5ff0914SRobert Watson if (unp2 == NULL) { 870b5ff0914SRobert Watson error = ENOTCONN; 871b5ff0914SRobert Watson break; 872b5ff0914SRobert Watson } 873f3f49bbbSRobert Watson so2 = unp2->unp_socket; 874ede6e136SRobert Watson UNP_PCB_LOCK(unp2); 875a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 876f3f49bbbSRobert Watson if (unp2->unp_flags & UNP_WANTCRED) { 8776a2989fdSMatthew N. Dodd /* 878ede6e136SRobert Watson * Credentials are passed only once on SOCK_STREAM. 8796a2989fdSMatthew N. Dodd */ 880f3f49bbbSRobert Watson unp2->unp_flags &= ~UNP_WANTCRED; 8816a2989fdSMatthew N. Dodd control = unp_addsockcred(td, control); 8826a2989fdSMatthew N. Dodd } 883df8bae1dSRodney W. Grimes /* 8841c381b19SRobert Watson * Send to paired receive port, and then reduce send buffer 8851c381b19SRobert Watson * hiwater marks to maintain backpressure. Wake up readers. 886df8bae1dSRodney W. Grimes */ 887fc3fcacfSRobert Watson if (control != NULL) { 888a34b7046SRobert Watson if (sbappendcontrol_locked(&so2->so_rcv, m, control)) 889fc3fcacfSRobert Watson control = NULL; 890e7c33e29SRobert Watson } else 891a34b7046SRobert Watson sbappend_locked(&so2->so_rcv, m); 892f3f49bbbSRobert Watson mbcnt = so2->so_rcv.sb_mbcnt - unp2->unp_mbcnt; 893f3f49bbbSRobert Watson unp2->unp_mbcnt = so2->so_rcv.sb_mbcnt; 894337cc6b6SRobert Watson sbcc = so2->so_rcv.sb_cc; 895337cc6b6SRobert Watson sorwakeup_locked(so2); 896337cc6b6SRobert Watson 897337cc6b6SRobert Watson SOCKBUF_LOCK(&so->so_snd); 898f3f49bbbSRobert Watson newhiwat = so->so_snd.sb_hiwat - (sbcc - unp2->unp_cc); 899f535380cSDon Lewis (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat, 9006aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 901337cc6b6SRobert Watson so->so_snd.sb_mbmax -= mbcnt; 9027abe2ac2SAlan Cox SOCKBUF_UNLOCK(&so->so_snd); 903f3f49bbbSRobert Watson unp2->unp_cc = sbcc; 904e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 905fc3fcacfSRobert Watson m = NULL; 906df8bae1dSRodney W. Grimes break; 907df8bae1dSRodney W. Grimes 908df8bae1dSRodney W. Grimes default: 909a29f300eSGarrett Wollman panic("uipc_send unknown socktype"); 910df8bae1dSRodney W. Grimes } 911a29f300eSGarrett Wollman 9126b8fda4dSGarrett Wollman /* 913ede6e136SRobert Watson * SEND_EOF is equivalent to a SEND followed by a SHUTDOWN. 9146b8fda4dSGarrett Wollman */ 915a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 916ede6e136SRobert Watson UNP_PCB_LOCK(unp); 9176b8fda4dSGarrett Wollman socantsendmore(so); 9186b8fda4dSGarrett Wollman unp_shutdown(unp); 919e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 920ede6e136SRobert Watson } 921e7c33e29SRobert Watson 922e7c33e29SRobert Watson if ((nam != NULL) || (flags & PRUS_EOF)) 923e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 924e7c33e29SRobert Watson else 925e7c33e29SRobert Watson UNP_GLOBAL_RUNLOCK(); 926df8bae1dSRodney W. Grimes 927fc3fcacfSRobert Watson if (control != NULL && error != 0) 928bd508d39SDon Lewis unp_dispose(control); 929bd508d39SDon Lewis 930a29f300eSGarrett Wollman release: 931fc3fcacfSRobert Watson if (control != NULL) 932a29f300eSGarrett Wollman m_freem(control); 933fc3fcacfSRobert Watson if (m != NULL) 934a29f300eSGarrett Wollman m_freem(m); 935e5aeaa0cSDag-Erling Smørgrav return (error); 936a29f300eSGarrett Wollman } 937df8bae1dSRodney W. Grimes 938a29f300eSGarrett Wollman static int 939a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 940a29f300eSGarrett Wollman { 941e7c33e29SRobert Watson struct unpcb *unp, *unp2; 942a29f300eSGarrett Wollman struct socket *so2; 943a29f300eSGarrett Wollman 94440f2ac28SRobert Watson unp = sotounpcb(so); 9454d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sense: unp == NULL")); 946e7c33e29SRobert Watson 947a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 948e7c33e29SRobert Watson UNP_GLOBAL_RLOCK(); 949e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 950e7c33e29SRobert Watson unp2 = unp->unp_conn; 951e7c33e29SRobert Watson if (so->so_type == SOCK_STREAM && unp2 != NULL) { 952e7c33e29SRobert Watson so2 = unp2->unp_socket; 953a29f300eSGarrett Wollman sb->st_blksize += so2->so_rcv.sb_cc; 954df8bae1dSRodney W. Grimes } 955f3732fd1SPoul-Henning Kamp sb->st_dev = NODEV; 956df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 9576f782c46SJeffrey Hsu unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 958a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 959e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 960e7c33e29SRobert Watson UNP_GLOBAL_RUNLOCK(); 961df8bae1dSRodney W. Grimes return (0); 962a29f300eSGarrett Wollman } 963df8bae1dSRodney W. Grimes 964a29f300eSGarrett Wollman static int 965a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 966a29f300eSGarrett Wollman { 96740f2ac28SRobert Watson struct unpcb *unp; 968df8bae1dSRodney W. Grimes 96940f2ac28SRobert Watson unp = sotounpcb(so); 9704d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_shutdown: unp == NULL")); 971e7c33e29SRobert Watson 972e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 973e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 974a29f300eSGarrett Wollman socantsendmore(so); 975a29f300eSGarrett Wollman unp_shutdown(unp); 976e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 977e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 978e5aeaa0cSDag-Erling Smørgrav return (0); 979a29f300eSGarrett Wollman } 980df8bae1dSRodney W. Grimes 981a29f300eSGarrett Wollman static int 98257bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 983a29f300eSGarrett Wollman { 98440f2ac28SRobert Watson struct unpcb *unp; 9850d9ce3a1SRobert Watson const struct sockaddr *sa; 986a29f300eSGarrett Wollman 9874d4b555eSRobert Watson unp = sotounpcb(so); 9884d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL")); 989e7c33e29SRobert Watson 9900d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 991e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 992fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 9930d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_addr; 99483f3198bSThomas Moestl else 9950d9ce3a1SRobert Watson sa = &sun_noname; 9960d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 997e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 998e5aeaa0cSDag-Erling Smørgrav return (0); 999df8bae1dSRodney W. Grimes } 1000a29f300eSGarrett Wollman 1001a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = { 1002756d52a1SPoul-Henning Kamp .pru_abort = uipc_abort, 1003756d52a1SPoul-Henning Kamp .pru_accept = uipc_accept, 1004756d52a1SPoul-Henning Kamp .pru_attach = uipc_attach, 1005756d52a1SPoul-Henning Kamp .pru_bind = uipc_bind, 1006756d52a1SPoul-Henning Kamp .pru_connect = uipc_connect, 1007756d52a1SPoul-Henning Kamp .pru_connect2 = uipc_connect2, 1008756d52a1SPoul-Henning Kamp .pru_detach = uipc_detach, 1009756d52a1SPoul-Henning Kamp .pru_disconnect = uipc_disconnect, 1010756d52a1SPoul-Henning Kamp .pru_listen = uipc_listen, 1011756d52a1SPoul-Henning Kamp .pru_peeraddr = uipc_peeraddr, 1012756d52a1SPoul-Henning Kamp .pru_rcvd = uipc_rcvd, 1013756d52a1SPoul-Henning Kamp .pru_send = uipc_send, 1014756d52a1SPoul-Henning Kamp .pru_sense = uipc_sense, 1015756d52a1SPoul-Henning Kamp .pru_shutdown = uipc_shutdown, 1016756d52a1SPoul-Henning Kamp .pru_sockaddr = uipc_sockaddr, 1017a152f8a3SRobert Watson .pru_close = uipc_close, 1018a29f300eSGarrett Wollman }; 1019df8bae1dSRodney W. Grimes 10200c1bb4fbSDima Dorfman int 1021892af6b9SRobert Watson uipc_ctloutput(struct socket *so, struct sockopt *sopt) 10220c1bb4fbSDima Dorfman { 102340f2ac28SRobert Watson struct unpcb *unp; 10240d9ce3a1SRobert Watson struct xucred xu; 10256a2989fdSMatthew N. Dodd int error, optval; 10266a2989fdSMatthew N. Dodd 102796a041b5SMatthew N. Dodd if (sopt->sopt_level != 0) 102896a041b5SMatthew N. Dodd return (EINVAL); 102996a041b5SMatthew N. Dodd 10306a2989fdSMatthew N. Dodd unp = sotounpcb(so); 10314d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL")); 10326a2989fdSMatthew N. Dodd error = 0; 10330c1bb4fbSDima Dorfman switch (sopt->sopt_dir) { 10340c1bb4fbSDima Dorfman case SOPT_GET: 10350c1bb4fbSDima Dorfman switch (sopt->sopt_name) { 10360c1bb4fbSDima Dorfman case LOCAL_PEERCRED: 1037e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 10380c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC) 10390d9ce3a1SRobert Watson xu = unp->unp_peercred; 10400c1bb4fbSDima Dorfman else { 10410c1bb4fbSDima Dorfman if (so->so_type == SOCK_STREAM) 10420c1bb4fbSDima Dorfman error = ENOTCONN; 10430c1bb4fbSDima Dorfman else 10440c1bb4fbSDima Dorfman error = EINVAL; 10450c1bb4fbSDima Dorfman } 1046e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 10470d9ce3a1SRobert Watson if (error == 0) 10480d9ce3a1SRobert Watson error = sooptcopyout(sopt, &xu, sizeof(xu)); 10490c1bb4fbSDima Dorfman break; 1050e7c33e29SRobert Watson 10516a2989fdSMatthew N. Dodd case LOCAL_CREDS: 1052a6357845SRobert Watson /* Unlocked read. */ 10536a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0; 10546a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 10556a2989fdSMatthew N. Dodd break; 1056e7c33e29SRobert Watson 10576a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 1058a6357845SRobert Watson /* Unlocked read. */ 10596a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0; 10606a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 10616a2989fdSMatthew N. Dodd break; 1062e7c33e29SRobert Watson 10630c1bb4fbSDima Dorfman default: 10640c1bb4fbSDima Dorfman error = EOPNOTSUPP; 10650c1bb4fbSDima Dorfman break; 10660c1bb4fbSDima Dorfman } 10670c1bb4fbSDima Dorfman break; 1068e7c33e29SRobert Watson 10690c1bb4fbSDima Dorfman case SOPT_SET: 10706a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 10716a2989fdSMatthew N. Dodd case LOCAL_CREDS: 10726a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 10736a2989fdSMatthew N. Dodd error = sooptcopyin(sopt, &optval, sizeof(optval), 10746a2989fdSMatthew N. Dodd sizeof(optval)); 10756a2989fdSMatthew N. Dodd if (error) 10766a2989fdSMatthew N. Dodd break; 10776a2989fdSMatthew N. Dodd 1078e7c33e29SRobert Watson #define OPTSET(bit) do { \ 1079e7c33e29SRobert Watson UNP_PCB_LOCK(unp); \ 10806a2989fdSMatthew N. Dodd if (optval) \ 10816a2989fdSMatthew N. Dodd unp->unp_flags |= bit; \ 10826a2989fdSMatthew N. Dodd else \ 1083e7c33e29SRobert Watson unp->unp_flags &= ~bit; \ 1084e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); \ 1085e7c33e29SRobert Watson } while (0) 10866a2989fdSMatthew N. Dodd 10876a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 10886a2989fdSMatthew N. Dodd case LOCAL_CREDS: 10896a2989fdSMatthew N. Dodd OPTSET(UNP_WANTCRED); 10906a2989fdSMatthew N. Dodd break; 1091e7c33e29SRobert Watson 10926a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 10936a2989fdSMatthew N. Dodd OPTSET(UNP_CONNWAIT); 10946a2989fdSMatthew N. Dodd break; 1095e7c33e29SRobert Watson 10966a2989fdSMatthew N. Dodd default: 10976a2989fdSMatthew N. Dodd break; 10986a2989fdSMatthew N. Dodd } 10996a2989fdSMatthew N. Dodd break; 11006a2989fdSMatthew N. Dodd #undef OPTSET 11016a2989fdSMatthew N. Dodd default: 11026a2989fdSMatthew N. Dodd error = ENOPROTOOPT; 11036a2989fdSMatthew N. Dodd break; 11046a2989fdSMatthew N. Dodd } 1105abb886faSMatthew N. Dodd break; 1106e7c33e29SRobert Watson 11070c1bb4fbSDima Dorfman default: 11080c1bb4fbSDima Dorfman error = EOPNOTSUPP; 11090c1bb4fbSDima Dorfman break; 11100c1bb4fbSDima Dorfman } 11110c1bb4fbSDima Dorfman return (error); 11120c1bb4fbSDima Dorfman } 11130c1bb4fbSDima Dorfman 1114f708ef1bSPoul-Henning Kamp static int 1115892af6b9SRobert Watson unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1116df8bae1dSRodney W. Grimes { 1117892af6b9SRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam; 1118892af6b9SRobert Watson struct vnode *vp; 1119892af6b9SRobert Watson struct socket *so2, *so3; 1120b295bdcdSRobert Watson struct unpcb *unp, *unp2, *unp3; 11219e289446SWojciech A. Koszek int error, len, vfslocked; 1122df8bae1dSRodney W. Grimes struct nameidata nd; 112357bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 11240d9ce3a1SRobert Watson struct sockaddr *sa; 11250d9ce3a1SRobert Watson 1126e7c33e29SRobert Watson UNP_GLOBAL_WLOCK_ASSERT(); 1127df8bae1dSRodney W. Grimes 11284d4b555eSRobert Watson unp = sotounpcb(so); 11294d4b555eSRobert Watson KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 1130e7c33e29SRobert Watson 113157bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 113257bf258eSGarrett Wollman if (len <= 0) 1133e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 11347928893dSEd Maste bcopy(soun->sun_path, buf, len); 11357928893dSEd Maste buf[len] = 0; 1136e7c33e29SRobert Watson 1137e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 11384f1f0ef5SRobert Watson if (unp->unp_flags & UNP_CONNECTING) { 1139e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 11404f1f0ef5SRobert Watson return (EALREADY); 11414f1f0ef5SRobert Watson } 11428c96f9c1SRobert Watson UNP_GLOBAL_WUNLOCK(); 114305102f04SRobert Watson unp->unp_flags |= UNP_CONNECTING; 1144e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1145e7c33e29SRobert Watson 11460d9ce3a1SRobert Watson sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 11479e289446SWojciech A. Koszek NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, 11489e289446SWojciech A. Koszek td); 1149797f2d22SPoul-Henning Kamp error = namei(&nd); 1150797f2d22SPoul-Henning Kamp if (error) 11510d9ce3a1SRobert Watson vp = NULL; 11520d9ce3a1SRobert Watson else 1153df8bae1dSRodney W. Grimes vp = nd.ni_vp; 11540d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_connect"); 11559e289446SWojciech A. Koszek vfslocked = NDHASGIANT(&nd); 1156762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 11570d9ce3a1SRobert Watson if (error) 11580d9ce3a1SRobert Watson goto bad; 11590d9ce3a1SRobert Watson 1160df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 1161df8bae1dSRodney W. Grimes error = ENOTSOCK; 1162df8bae1dSRodney W. Grimes goto bad; 1163df8bae1dSRodney W. Grimes } 11646fac927cSRobert Watson #ifdef MAC 116530d239bcSRobert Watson error = mac_vnode_check_open(td->td_ucred, vp, VWRITE | VREAD); 11666fac927cSRobert Watson if (error) 11676fac927cSRobert Watson goto bad; 11686fac927cSRobert Watson #endif 1169a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 1170797f2d22SPoul-Henning Kamp if (error) 1171df8bae1dSRodney W. Grimes goto bad; 11729e289446SWojciech A. Koszek VFS_UNLOCK_GIANT(vfslocked); 1173e7c33e29SRobert Watson 1174b295bdcdSRobert Watson unp = sotounpcb(so); 11754d4b555eSRobert Watson KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 1176e7c33e29SRobert Watson 1177e7c33e29SRobert Watson /* 1178e7c33e29SRobert Watson * Lock global lock for two reasons: make sure v_socket is stable, 1179e7c33e29SRobert Watson * and to protect simultaneous locking of multiple pcbs. 1180e7c33e29SRobert Watson */ 1181e7c33e29SRobert Watson UNP_GLOBAL_WLOCK(); 1182df8bae1dSRodney W. Grimes so2 = vp->v_socket; 1183fc3fcacfSRobert Watson if (so2 == NULL) { 1184df8bae1dSRodney W. Grimes error = ECONNREFUSED; 11852260c03dSRobert Watson goto bad2; 1186df8bae1dSRodney W. Grimes } 1187df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 1188df8bae1dSRodney W. Grimes error = EPROTOTYPE; 11892260c03dSRobert Watson goto bad2; 1190df8bae1dSRodney W. Grimes } 1191df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 1192e7c33e29SRobert Watson if (so2->so_options & SO_ACCEPTCONN) { 1193e7c33e29SRobert Watson /* 1194e7c33e29SRobert Watson * We can't drop the global lock here or 'so2' may 1195d7924b70SRobert Watson * become invalid. As a result, we need to handle 1196d7924b70SRobert Watson * possibly lock recursion in uipc_attach. 1197e7c33e29SRobert Watson */ 11980d9ce3a1SRobert Watson so3 = sonewconn(so2, 0); 1199e7c33e29SRobert Watson } else 12000d9ce3a1SRobert Watson so3 = NULL; 12010d9ce3a1SRobert Watson if (so3 == NULL) { 1202df8bae1dSRodney W. Grimes error = ECONNREFUSED; 12030d9ce3a1SRobert Watson goto bad2; 1204df8bae1dSRodney W. Grimes } 12050c1bb4fbSDima Dorfman unp = sotounpcb(so); 1206df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 1207df8bae1dSRodney W. Grimes unp3 = sotounpcb(so3); 1208e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 1209e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 1210e7c33e29SRobert Watson UNP_PCB_LOCK(unp3); 12110d9ce3a1SRobert Watson if (unp2->unp_addr != NULL) { 12120d9ce3a1SRobert Watson bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 12130d9ce3a1SRobert Watson unp3->unp_addr = (struct sockaddr_un *) sa; 12140d9ce3a1SRobert Watson sa = NULL; 12150d9ce3a1SRobert Watson } 12160c1bb4fbSDima Dorfman /* 12170c1bb4fbSDima Dorfman * unp_peercred management: 12180c1bb4fbSDima Dorfman * 12191c381b19SRobert Watson * The connecter's (client's) credentials are copied from its 12201c381b19SRobert Watson * process structure at the time of connect() (which is now). 12210c1bb4fbSDima Dorfman */ 1222a854ed98SJohn Baldwin cru2x(td->td_ucred, &unp3->unp_peercred); 12230c1bb4fbSDima Dorfman unp3->unp_flags |= UNP_HAVEPC; 12240c1bb4fbSDima Dorfman /* 12251c381b19SRobert Watson * The receiver's (server's) credentials are copied from the 12261c381b19SRobert Watson * unp_peercred member of socket on which the former called 1227e7c33e29SRobert Watson * listen(); uipc_listen() cached that process's credentials 12281c381b19SRobert Watson * at that time so we can use them now. 12290c1bb4fbSDima Dorfman */ 12300c1bb4fbSDima Dorfman KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 12310c1bb4fbSDima Dorfman ("unp_connect: listener without cached peercred")); 12320c1bb4fbSDima Dorfman memcpy(&unp->unp_peercred, &unp2->unp_peercred, 12330c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 12340c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPC; 1235481f8fe8SMaxim Konovalov if (unp2->unp_flags & UNP_WANTCRED) 1236481f8fe8SMaxim Konovalov unp3->unp_flags |= UNP_WANTCRED; 1237e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp3); 1238e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1239e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1240335654d7SRobert Watson #ifdef MAC 1241310e7cebSRobert Watson SOCK_LOCK(so); 124230d239bcSRobert Watson mac_socketpeer_set_from_socket(so, so3); 124330d239bcSRobert Watson mac_socketpeer_set_from_socket(so3, so); 1244310e7cebSRobert Watson SOCK_UNLOCK(so); 1245335654d7SRobert Watson #endif 12460c1bb4fbSDima Dorfman 1247df8bae1dSRodney W. Grimes so2 = so3; 1248df8bae1dSRodney W. Grimes } 1249e7c33e29SRobert Watson unp = sotounpcb(so); 1250e7c33e29SRobert Watson KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 1251e7c33e29SRobert Watson unp2 = sotounpcb(so2); 1252e7c33e29SRobert Watson KASSERT(unp2 != NULL, ("unp_connect: unp2 == NULL")); 1253e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 1254e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 12556a2989fdSMatthew N. Dodd error = unp_connect2(so, so2, PRU_CONNECT); 1256e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1257e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 12580d9ce3a1SRobert Watson bad2: 1259e7c33e29SRobert Watson UNP_GLOBAL_WUNLOCK(); 12609e289446SWojciech A. Koszek if (vfslocked) 12619e289446SWojciech A. Koszek /* 12629e289446SWojciech A. Koszek * Giant has been previously acquired. This means filesystem 12639e289446SWojciech A. Koszek * isn't MPSAFE. Do it once again. 12649e289446SWojciech A. Koszek */ 12650d9ce3a1SRobert Watson mtx_lock(&Giant); 1266df8bae1dSRodney W. Grimes bad: 12670d9ce3a1SRobert Watson if (vp != NULL) 1268df8bae1dSRodney W. Grimes vput(vp); 12699e289446SWojciech A. Koszek VFS_UNLOCK_GIANT(vfslocked); 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 /* 1349d7924b70SRobert Watson * unp_pcblist() walks the global list of struct unpcb's to generate a 1350d7924b70SRobert Watson * pointer list, bumping the refcount on each unpcb. It then copies them out 1351d7924b70SRobert Watson * sequentially, validating the generation number on each to see if it has 1352d7924b70SRobert Watson * been detached. All of this is necessary because copyout() may sleep on 1353d7924b70SRobert Watson * disk I/O. 13540d9ce3a1SRobert Watson */ 135598271db4SGarrett Wollman static int 135682d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 135798271db4SGarrett Wollman { 1358f5ef029eSPoul-Henning Kamp int error, i, n; 13599ae328fcSJohn Baldwin int freeunp; 136098271db4SGarrett Wollman struct unpcb *unp, **unp_list; 136198271db4SGarrett Wollman unp_gen_t gencnt; 13628f364875SJulian Elischer struct xunpgen *xug; 136398271db4SGarrett Wollman struct unp_head *head; 13648f364875SJulian Elischer struct xunpcb *xu; 136598271db4SGarrett Wollman 1366a23d65bfSBruce Evans head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 136798271db4SGarrett Wollman 136898271db4SGarrett Wollman /* 136998271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 137098271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 137198271db4SGarrett Wollman */ 1372fc3fcacfSRobert Watson if (req->oldptr == NULL) { 137398271db4SGarrett Wollman n = unp_count; 13748f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug) 137598271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 1376e5aeaa0cSDag-Erling Smørgrav return (0); 137798271db4SGarrett Wollman } 137898271db4SGarrett Wollman 1379fc3fcacfSRobert Watson if (req->newptr != NULL) 1380e5aeaa0cSDag-Erling Smørgrav return (EPERM); 138198271db4SGarrett Wollman 138298271db4SGarrett Wollman /* 138398271db4SGarrett Wollman * OK, now we're committed to doing something. 138498271db4SGarrett Wollman */ 1385a163d034SWarner Losh xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 1386e7c33e29SRobert Watson UNP_GLOBAL_RLOCK(); 138798271db4SGarrett Wollman gencnt = unp_gencnt; 138898271db4SGarrett Wollman n = unp_count; 1389e7c33e29SRobert Watson UNP_GLOBAL_RUNLOCK(); 139098271db4SGarrett Wollman 13918f364875SJulian Elischer xug->xug_len = sizeof *xug; 13928f364875SJulian Elischer xug->xug_count = n; 13938f364875SJulian Elischer xug->xug_gen = gencnt; 13948f364875SJulian Elischer xug->xug_sogen = so_gencnt; 13958f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 13968f364875SJulian Elischer if (error) { 13978f364875SJulian Elischer free(xug, M_TEMP); 1398e5aeaa0cSDag-Erling Smørgrav return (error); 13998f364875SJulian Elischer } 140098271db4SGarrett Wollman 1401a163d034SWarner Losh unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 140298271db4SGarrett Wollman 1403e7c33e29SRobert Watson UNP_GLOBAL_RLOCK(); 14042e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 14052e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 1406e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 14078a7d8cc6SRobert Watson if (unp->unp_gencnt <= gencnt) { 1408a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred, 1409e7c33e29SRobert Watson unp->unp_socket->so_cred)) { 1410e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 14114787fd37SPaul Saab continue; 1412e7c33e29SRobert Watson } 141398271db4SGarrett Wollman unp_list[i++] = unp; 14149ae328fcSJohn Baldwin unp->unp_refcount++; 141598271db4SGarrett Wollman } 1416e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 14174787fd37SPaul Saab } 1418e7c33e29SRobert Watson UNP_GLOBAL_RUNLOCK(); 14191c381b19SRobert Watson n = i; /* In case we lost some during malloc. */ 142098271db4SGarrett Wollman 142198271db4SGarrett Wollman error = 0; 1422fe2eee82SColin Percival xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO); 142398271db4SGarrett Wollman for (i = 0; i < n; i++) { 142498271db4SGarrett Wollman unp = unp_list[i]; 1425e7c33e29SRobert Watson UNP_PCB_LOCK(unp); 14269ae328fcSJohn Baldwin unp->unp_refcount--; 14279ae328fcSJohn Baldwin if (unp->unp_refcount != 0 && unp->unp_gencnt <= gencnt) { 14288f364875SJulian Elischer xu->xu_len = sizeof *xu; 14298f364875SJulian Elischer xu->xu_unpp = unp; 143098271db4SGarrett Wollman /* 143198271db4SGarrett Wollman * XXX - need more locking here to protect against 143298271db4SGarrett Wollman * connect/disconnect races for SMP. 143398271db4SGarrett Wollman */ 1434fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 14358f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr, 143698271db4SGarrett Wollman unp->unp_addr->sun_len); 1437fc3fcacfSRobert Watson if (unp->unp_conn != NULL && 1438fc3fcacfSRobert Watson unp->unp_conn->unp_addr != NULL) 143998271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 14408f364875SJulian Elischer &xu->xu_caddr, 144198271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 14428f364875SJulian Elischer bcopy(unp, &xu->xu_unp, sizeof *unp); 14438f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket); 1444e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 14458f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu); 14469ae328fcSJohn Baldwin } else { 14479ae328fcSJohn Baldwin freeunp = (unp->unp_refcount == 0); 1448e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); 1449e7c33e29SRobert Watson if (freeunp) { 1450e7c33e29SRobert Watson UNP_PCB_LOCK_DESTROY(unp); 14519ae328fcSJohn Baldwin uma_zfree(unp_zone, unp); 145298271db4SGarrett Wollman } 145398271db4SGarrett Wollman } 1454e7c33e29SRobert Watson } 14558f364875SJulian Elischer free(xu, M_TEMP); 145698271db4SGarrett Wollman if (!error) { 145798271db4SGarrett Wollman /* 14581c381b19SRobert Watson * Give the user an updated idea of our state. If the 14591c381b19SRobert Watson * generation differs from what we told her before, she knows 14601c381b19SRobert Watson * that something happened while we were processing this 14611c381b19SRobert Watson * request, and it might be necessary to retry. 146298271db4SGarrett Wollman */ 14638f364875SJulian Elischer xug->xug_gen = unp_gencnt; 14648f364875SJulian Elischer xug->xug_sogen = so_gencnt; 14658f364875SJulian Elischer xug->xug_count = unp_count; 14668f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 146798271db4SGarrett Wollman } 146898271db4SGarrett Wollman free(unp_list, M_TEMP); 14698f364875SJulian Elischer free(xug, M_TEMP); 1470e5aeaa0cSDag-Erling Smørgrav return (error); 147198271db4SGarrett Wollman } 147298271db4SGarrett Wollman 147398271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 147498271db4SGarrett Wollman (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 147598271db4SGarrett Wollman "List of active local datagram sockets"); 147698271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 147798271db4SGarrett Wollman (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 147898271db4SGarrett Wollman "List of active local stream sockets"); 147998271db4SGarrett Wollman 1480f708ef1bSPoul-Henning Kamp static void 1481892af6b9SRobert Watson unp_shutdown(struct unpcb *unp) 1482df8bae1dSRodney W. Grimes { 1483e7c33e29SRobert Watson struct unpcb *unp2; 1484df8bae1dSRodney W. Grimes struct socket *so; 1485df8bae1dSRodney W. Grimes 1486e7c33e29SRobert Watson UNP_GLOBAL_WLOCK_ASSERT(); 1487e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 14880d9ce3a1SRobert Watson 1489e7c33e29SRobert Watson unp2 = unp->unp_conn; 1490e7c33e29SRobert Watson if (unp->unp_socket->so_type == SOCK_STREAM && unp2 != NULL) { 1491e7c33e29SRobert Watson so = unp2->unp_socket; 1492e7c33e29SRobert Watson if (so != NULL) 1493df8bae1dSRodney W. Grimes socantrcvmore(so); 1494df8bae1dSRodney W. Grimes } 1495e7c33e29SRobert Watson } 1496df8bae1dSRodney W. Grimes 1497f708ef1bSPoul-Henning Kamp static void 1498892af6b9SRobert Watson unp_drop(struct unpcb *unp, int errno) 1499df8bae1dSRodney W. Grimes { 1500df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 1501e7c33e29SRobert Watson struct unpcb *unp2; 1502df8bae1dSRodney W. Grimes 1503e7c33e29SRobert Watson UNP_GLOBAL_WLOCK_ASSERT(); 1504e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp); 15050d9ce3a1SRobert Watson 1506df8bae1dSRodney W. Grimes so->so_error = errno; 1507e7c33e29SRobert Watson unp2 = unp->unp_conn; 1508e7c33e29SRobert Watson if (unp2 == NULL) 1509e7c33e29SRobert Watson return; 1510e7c33e29SRobert Watson 1511e7c33e29SRobert Watson UNP_PCB_LOCK(unp2); 1512e7c33e29SRobert Watson unp_disconnect(unp, unp2); 1513e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2); 1514df8bae1dSRodney W. Grimes } 1515df8bae1dSRodney W. Grimes 15162bc21ed9SDavid Malone static void 1517892af6b9SRobert Watson unp_freerights(struct file **rp, int fdcount) 1518df8bae1dSRodney W. Grimes { 15192bc21ed9SDavid Malone int i; 15202bc21ed9SDavid Malone struct file *fp; 1521df8bae1dSRodney W. Grimes 15222bc21ed9SDavid Malone for (i = 0; i < fdcount; i++) { 15238692c025SYoshinobu Inoue /* 15241c381b19SRobert Watson * Zero the pointer before calling unp_discard since it may 15251c381b19SRobert Watson * end up in unp_gc().. 1526d7dca903SRobert Watson * 1527d7dca903SRobert Watson * XXXRW: This is less true than it used to be. 15288692c025SYoshinobu Inoue */ 1529e7c33e29SRobert Watson fp = *rp; 1530e7c33e29SRobert Watson *rp++ = NULL; 15318692c025SYoshinobu Inoue unp_discard(fp); 1532df8bae1dSRodney W. Grimes } 15332bc21ed9SDavid Malone } 15342bc21ed9SDavid Malone 15352bc21ed9SDavid Malone int 1536892af6b9SRobert Watson unp_externalize(struct mbuf *control, struct mbuf **controlp) 15372bc21ed9SDavid Malone { 15382bc21ed9SDavid Malone struct thread *td = curthread; /* XXX */ 15392bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 15402bc21ed9SDavid Malone int i; 15412bc21ed9SDavid Malone int *fdp; 15422bc21ed9SDavid Malone struct file **rp; 15432bc21ed9SDavid Malone struct file *fp; 15442bc21ed9SDavid Malone void *data; 15452bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 15462bc21ed9SDavid Malone int error, newfds; 15472bc21ed9SDavid Malone int f; 15482bc21ed9SDavid Malone u_int newlen; 15492bc21ed9SDavid Malone 1550e7c33e29SRobert Watson UNP_GLOBAL_UNLOCK_ASSERT(); 15514c5bc1caSRobert Watson 15522bc21ed9SDavid Malone error = 0; 15532bc21ed9SDavid Malone if (controlp != NULL) /* controlp == NULL => free control messages */ 15542bc21ed9SDavid Malone *controlp = NULL; 15552bc21ed9SDavid Malone 15562bc21ed9SDavid Malone while (cm != NULL) { 15572bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 15582bc21ed9SDavid Malone error = EINVAL; 15592bc21ed9SDavid Malone break; 15602bc21ed9SDavid Malone } 15612bc21ed9SDavid Malone 15622bc21ed9SDavid Malone data = CMSG_DATA(cm); 15632bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 15642bc21ed9SDavid Malone 15652bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET 15662bc21ed9SDavid Malone && cm->cmsg_type == SCM_RIGHTS) { 15672bc21ed9SDavid Malone newfds = datalen / sizeof(struct file *); 15682bc21ed9SDavid Malone rp = data; 15692bc21ed9SDavid Malone 1570e2f9a08bSOlivier Houchard /* If we're not outputting the descriptors free them. */ 15712bc21ed9SDavid Malone if (error || controlp == NULL) { 15722bc21ed9SDavid Malone unp_freerights(rp, newfds); 15732bc21ed9SDavid Malone goto next; 15742bc21ed9SDavid Malone } 15755e3f7694SRobert Watson FILEDESC_XLOCK(td->td_proc->p_fd); 15762bc21ed9SDavid Malone /* if the new FD's will not fit free them. */ 15772bc21ed9SDavid Malone if (!fdavail(td, newfds)) { 15785e3f7694SRobert Watson FILEDESC_XUNLOCK(td->td_proc->p_fd); 15792bc21ed9SDavid Malone error = EMSGSIZE; 15802bc21ed9SDavid Malone unp_freerights(rp, newfds); 15812bc21ed9SDavid Malone goto next; 1582df8bae1dSRodney W. Grimes } 1583ed5b7817SJulian Elischer /* 15841c381b19SRobert Watson * Now change each pointer to an fd in the global 15851c381b19SRobert Watson * table to an integer that is the index to the local 15861c381b19SRobert Watson * fd table entry that we set up to point to the 15871c381b19SRobert Watson * global one we are transferring. 1588ed5b7817SJulian Elischer */ 15892bc21ed9SDavid Malone newlen = newfds * sizeof(int); 15902bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 15912bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 15922bc21ed9SDavid Malone if (*controlp == NULL) { 15935e3f7694SRobert Watson FILEDESC_XUNLOCK(td->td_proc->p_fd); 15942bc21ed9SDavid Malone error = E2BIG; 15952bc21ed9SDavid Malone unp_freerights(rp, newfds); 15962bc21ed9SDavid Malone goto next; 15972bc21ed9SDavid Malone } 15982bc21ed9SDavid Malone 15992bc21ed9SDavid Malone fdp = (int *) 16002bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1601df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 1602a6d4491cSDag-Erling Smørgrav if (fdalloc(td, 0, &f)) 16032bc21ed9SDavid Malone panic("unp_externalize fdalloc failed"); 16048692c025SYoshinobu Inoue fp = *rp++; 1605b40ce416SJulian Elischer td->td_proc->p_fd->fd_ofiles[f] = fp; 1606397c19d1SJeff Roberson unp_externalize_fp(fp); 16078692c025SYoshinobu Inoue *fdp++ = f; 1608df8bae1dSRodney W. Grimes } 16095e3f7694SRobert Watson FILEDESC_XUNLOCK(td->td_proc->p_fd); 16101c381b19SRobert Watson } else { 16111c381b19SRobert Watson /* We can just copy anything else across. */ 16122bc21ed9SDavid Malone if (error || controlp == NULL) 16132bc21ed9SDavid Malone goto next; 16142bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, datalen, 16152bc21ed9SDavid Malone cm->cmsg_type, cm->cmsg_level); 16162bc21ed9SDavid Malone if (*controlp == NULL) { 16172bc21ed9SDavid Malone error = ENOBUFS; 16182bc21ed9SDavid Malone goto next; 16192bc21ed9SDavid Malone } 16202bc21ed9SDavid Malone bcopy(data, 16212bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 16222bc21ed9SDavid Malone datalen); 16232bc21ed9SDavid Malone } 16242bc21ed9SDavid Malone 16252bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 16262bc21ed9SDavid Malone 16272bc21ed9SDavid Malone next: 16282bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 16292bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 16302bc21ed9SDavid Malone cm = (struct cmsghdr *) 16312bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 16328692c025SYoshinobu Inoue } else { 16332bc21ed9SDavid Malone clen = 0; 16342bc21ed9SDavid Malone cm = NULL; 16358692c025SYoshinobu Inoue } 16368692c025SYoshinobu Inoue } 16378692c025SYoshinobu Inoue 16382bc21ed9SDavid Malone m_freem(control); 16392bc21ed9SDavid Malone 16402bc21ed9SDavid Malone return (error); 1641df8bae1dSRodney W. Grimes } 1642df8bae1dSRodney W. Grimes 16434f590175SPaul Saab static void 16444f590175SPaul Saab unp_zone_change(void *tag) 16454f590175SPaul Saab { 16464f590175SPaul Saab 16474f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets); 16484f590175SPaul Saab } 16494f590175SPaul Saab 165098271db4SGarrett Wollman void 165198271db4SGarrett Wollman unp_init(void) 165298271db4SGarrett Wollman { 16531c381b19SRobert Watson 16549e9d298aSJeff Roberson unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 16559ae328fcSJohn Baldwin NULL, NULL, UMA_ALIGN_PTR, 0); 1656fc3fcacfSRobert Watson if (unp_zone == NULL) 165798271db4SGarrett Wollman panic("unp_init"); 16584f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets); 16594f590175SPaul Saab EVENTHANDLER_REGISTER(maxsockets_change, unp_zone_change, 16604f590175SPaul Saab NULL, EVENTHANDLER_PRI_ANY); 166198271db4SGarrett Wollman LIST_INIT(&unp_dhead); 166298271db4SGarrett Wollman LIST_INIT(&unp_shead); 1663a0ec558aSRobert Watson TASK_INIT(&unp_gc_task, 0, unp_gc, NULL); 1664e7c33e29SRobert Watson UNP_GLOBAL_LOCK_INIT(); 166598271db4SGarrett Wollman } 166698271db4SGarrett Wollman 1667f708ef1bSPoul-Henning Kamp static int 1668892af6b9SRobert Watson unp_internalize(struct mbuf **controlp, struct thread *td) 1669df8bae1dSRodney W. Grimes { 16702bc21ed9SDavid Malone struct mbuf *control = *controlp; 1671b40ce416SJulian Elischer struct proc *p = td->td_proc; 16728692c025SYoshinobu Inoue struct filedesc *fdescp = p->p_fd; 16732bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 16742bc21ed9SDavid Malone struct cmsgcred *cmcred; 16752bc21ed9SDavid Malone struct file **rp; 16762bc21ed9SDavid Malone struct file *fp; 16772bc21ed9SDavid Malone struct timeval *tv; 16782bc21ed9SDavid Malone int i, fd, *fdp; 16792bc21ed9SDavid Malone void *data; 16802bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 16812bc21ed9SDavid Malone int error, oldfds; 16828692c025SYoshinobu Inoue u_int newlen; 1683df8bae1dSRodney W. Grimes 1684e7c33e29SRobert Watson UNP_GLOBAL_UNLOCK_ASSERT(); 16854c5bc1caSRobert Watson 16862bc21ed9SDavid Malone error = 0; 16872bc21ed9SDavid Malone *controlp = NULL; 16880b788fa1SBill Paul 16892bc21ed9SDavid Malone while (cm != NULL) { 16902bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 16912bc21ed9SDavid Malone || cm->cmsg_len > clen) { 16922bc21ed9SDavid Malone error = EINVAL; 16932bc21ed9SDavid Malone goto out; 16942bc21ed9SDavid Malone } 16952bc21ed9SDavid Malone 16962bc21ed9SDavid Malone data = CMSG_DATA(cm); 16972bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 16982bc21ed9SDavid Malone 16992bc21ed9SDavid Malone switch (cm->cmsg_type) { 17000b788fa1SBill Paul /* 17010b788fa1SBill Paul * Fill in credential information. 17020b788fa1SBill Paul */ 17032bc21ed9SDavid Malone case SCM_CREDS: 17042bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 17052bc21ed9SDavid Malone SCM_CREDS, SOL_SOCKET); 17062bc21ed9SDavid Malone if (*controlp == NULL) { 17072bc21ed9SDavid Malone error = ENOBUFS; 17082bc21ed9SDavid Malone goto out; 17092bc21ed9SDavid Malone } 17102bc21ed9SDavid Malone 17112bc21ed9SDavid Malone cmcred = (struct cmsgcred *) 17122bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 17130b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 1714a854ed98SJohn Baldwin cmcred->cmcred_uid = td->td_ucred->cr_ruid; 1715a854ed98SJohn Baldwin cmcred->cmcred_gid = td->td_ucred->cr_rgid; 1716a854ed98SJohn Baldwin cmcred->cmcred_euid = td->td_ucred->cr_uid; 1717a854ed98SJohn Baldwin cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 17180b788fa1SBill Paul CMGROUP_MAX); 17190b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 17202bc21ed9SDavid Malone cmcred->cmcred_groups[i] = 1721a854ed98SJohn Baldwin td->td_ucred->cr_groups[i]; 17222bc21ed9SDavid Malone break; 17230b788fa1SBill Paul 17242bc21ed9SDavid Malone case SCM_RIGHTS: 17252bc21ed9SDavid Malone oldfds = datalen / sizeof (int); 1726ed5b7817SJulian Elischer /* 17271c381b19SRobert Watson * Check that all the FDs passed in refer to legal 17281c381b19SRobert Watson * files. If not, reject the entire operation. 1729ed5b7817SJulian Elischer */ 17302bc21ed9SDavid Malone fdp = data; 17315e3f7694SRobert Watson FILEDESC_SLOCK(fdescp); 1732df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 17338692c025SYoshinobu Inoue fd = *fdp++; 17348692c025SYoshinobu Inoue if ((unsigned)fd >= fdescp->fd_nfiles || 17352bc21ed9SDavid Malone fdescp->fd_ofiles[fd] == NULL) { 17365e3f7694SRobert Watson FILEDESC_SUNLOCK(fdescp); 17372bc21ed9SDavid Malone error = EBADF; 17382bc21ed9SDavid Malone goto out; 17392bc21ed9SDavid Malone } 1740e7d6662fSAlfred Perlstein fp = fdescp->fd_ofiles[fd]; 1741e7d6662fSAlfred Perlstein if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 17425e3f7694SRobert Watson FILEDESC_SUNLOCK(fdescp); 1743e7d6662fSAlfred Perlstein error = EOPNOTSUPP; 1744e7d6662fSAlfred Perlstein goto out; 1745e7d6662fSAlfred Perlstein } 1746e7d6662fSAlfred Perlstein 1747df8bae1dSRodney W. Grimes } 17485e3f7694SRobert Watson 1749ed5b7817SJulian Elischer /* 1750e7c33e29SRobert Watson * Now replace the integer FDs with pointers to 1751e7c33e29SRobert Watson * the associated global file table entry.. 1752ed5b7817SJulian Elischer */ 17532bc21ed9SDavid Malone newlen = oldfds * sizeof(struct file *); 17542bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 17552bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 17562bc21ed9SDavid Malone if (*controlp == NULL) { 17575e3f7694SRobert Watson FILEDESC_SUNLOCK(fdescp); 17582bc21ed9SDavid Malone error = E2BIG; 17592bc21ed9SDavid Malone goto out; 17608692c025SYoshinobu Inoue } 17618692c025SYoshinobu Inoue 17622bc21ed9SDavid Malone fdp = data; 17632bc21ed9SDavid Malone rp = (struct file **) 17642bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 17658692c025SYoshinobu Inoue for (i = 0; i < oldfds; i++) { 17668692c025SYoshinobu Inoue fp = fdescp->fd_ofiles[*fdp++]; 1767df8bae1dSRodney W. Grimes *rp++ = fp; 1768397c19d1SJeff Roberson unp_internalize_fp(fp); 1769df8bae1dSRodney W. Grimes } 17705e3f7694SRobert Watson FILEDESC_SUNLOCK(fdescp); 17712bc21ed9SDavid Malone break; 17722bc21ed9SDavid Malone 17732bc21ed9SDavid Malone case SCM_TIMESTAMP: 17742bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*tv), 17752bc21ed9SDavid Malone SCM_TIMESTAMP, SOL_SOCKET); 17762bc21ed9SDavid Malone if (*controlp == NULL) { 17772bc21ed9SDavid Malone error = ENOBUFS; 17782bc21ed9SDavid Malone goto out; 17798692c025SYoshinobu Inoue } 17802bc21ed9SDavid Malone tv = (struct timeval *) 17812bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 17822bc21ed9SDavid Malone microtime(tv); 17832bc21ed9SDavid Malone break; 17842bc21ed9SDavid Malone 17852bc21ed9SDavid Malone default: 17862bc21ed9SDavid Malone error = EINVAL; 17872bc21ed9SDavid Malone goto out; 17882bc21ed9SDavid Malone } 17892bc21ed9SDavid Malone 17902bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 17912bc21ed9SDavid Malone 17922bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 17932bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 17942bc21ed9SDavid Malone cm = (struct cmsghdr *) 17952bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 17962bc21ed9SDavid Malone } else { 17972bc21ed9SDavid Malone clen = 0; 17982bc21ed9SDavid Malone cm = NULL; 17992bc21ed9SDavid Malone } 18002bc21ed9SDavid Malone } 18012bc21ed9SDavid Malone 18022bc21ed9SDavid Malone out: 18032bc21ed9SDavid Malone m_freem(control); 18042bc21ed9SDavid Malone 18052bc21ed9SDavid Malone return (error); 1806df8bae1dSRodney W. Grimes } 1807df8bae1dSRodney W. Grimes 18085b950deaSRobert Watson static struct mbuf * 18096a2989fdSMatthew N. Dodd unp_addsockcred(struct thread *td, struct mbuf *control) 18106a2989fdSMatthew N. Dodd { 181170df31f4SMaxim Konovalov struct mbuf *m, *n, *n_prev; 18126a2989fdSMatthew N. Dodd struct sockcred *sc; 181370df31f4SMaxim Konovalov const struct cmsghdr *cm; 18146a2989fdSMatthew N. Dodd int ngroups; 18156a2989fdSMatthew N. Dodd int i; 18166a2989fdSMatthew N. Dodd 18176a2989fdSMatthew N. Dodd ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX); 18186a2989fdSMatthew N. Dodd 18196a2989fdSMatthew N. Dodd m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET); 18206a2989fdSMatthew N. Dodd if (m == NULL) 18216a2989fdSMatthew N. Dodd return (control); 18226a2989fdSMatthew N. Dodd 18236a2989fdSMatthew N. Dodd sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *)); 18246a2989fdSMatthew N. Dodd sc->sc_uid = td->td_ucred->cr_ruid; 18256a2989fdSMatthew N. Dodd sc->sc_euid = td->td_ucred->cr_uid; 18266a2989fdSMatthew N. Dodd sc->sc_gid = td->td_ucred->cr_rgid; 18276a2989fdSMatthew N. Dodd sc->sc_egid = td->td_ucred->cr_gid; 18286a2989fdSMatthew N. Dodd sc->sc_ngroups = ngroups; 18296a2989fdSMatthew N. Dodd for (i = 0; i < sc->sc_ngroups; i++) 18306a2989fdSMatthew N. Dodd sc->sc_groups[i] = td->td_ucred->cr_groups[i]; 18316a2989fdSMatthew N. Dodd 18326a2989fdSMatthew N. Dodd /* 18331c381b19SRobert Watson * Unlink SCM_CREDS control messages (struct cmsgcred), since just 18341c381b19SRobert Watson * created SCM_CREDS control message (struct sockcred) has another 18351c381b19SRobert Watson * format. 18366a2989fdSMatthew N. Dodd */ 183770df31f4SMaxim Konovalov if (control != NULL) 183870df31f4SMaxim Konovalov for (n = control, n_prev = NULL; n != NULL;) { 183970df31f4SMaxim Konovalov cm = mtod(n, struct cmsghdr *); 184070df31f4SMaxim Konovalov if (cm->cmsg_level == SOL_SOCKET && 184170df31f4SMaxim Konovalov cm->cmsg_type == SCM_CREDS) { 184270df31f4SMaxim Konovalov if (n_prev == NULL) 184370df31f4SMaxim Konovalov control = n->m_next; 184470df31f4SMaxim Konovalov else 184570df31f4SMaxim Konovalov n_prev->m_next = n->m_next; 184670df31f4SMaxim Konovalov n = m_free(n); 184770df31f4SMaxim Konovalov } else { 184870df31f4SMaxim Konovalov n_prev = n; 184970df31f4SMaxim Konovalov n = n->m_next; 185070df31f4SMaxim Konovalov } 185170df31f4SMaxim Konovalov } 18526a2989fdSMatthew N. Dodd 185370df31f4SMaxim Konovalov /* Prepend it to the head. */ 185470df31f4SMaxim Konovalov m->m_next = control; 185570df31f4SMaxim Konovalov 185670df31f4SMaxim Konovalov return (m); 18576a2989fdSMatthew N. Dodd } 18586a2989fdSMatthew N. Dodd 1859397c19d1SJeff Roberson static struct unpcb * 1860397c19d1SJeff Roberson fptounp(struct file *fp) 1861397c19d1SJeff Roberson { 1862397c19d1SJeff Roberson struct socket *so; 1863397c19d1SJeff Roberson 1864397c19d1SJeff Roberson if (fp->f_type != DTYPE_SOCKET) 1865397c19d1SJeff Roberson return (NULL); 1866397c19d1SJeff Roberson if ((so = fp->f_data) == NULL) 1867397c19d1SJeff Roberson return (NULL); 1868397c19d1SJeff Roberson if (so->so_proto->pr_domain != &localdomain) 1869397c19d1SJeff Roberson return (NULL); 1870397c19d1SJeff Roberson return sotounpcb(so); 1871397c19d1SJeff Roberson } 1872397c19d1SJeff Roberson 1873397c19d1SJeff Roberson static void 1874397c19d1SJeff Roberson unp_discard(struct file *fp) 1875397c19d1SJeff Roberson { 1876397c19d1SJeff Roberson 1877397c19d1SJeff Roberson unp_externalize_fp(fp); 1878397c19d1SJeff Roberson (void) closef(fp, (struct thread *)NULL); 1879397c19d1SJeff Roberson } 1880397c19d1SJeff Roberson 1881397c19d1SJeff Roberson static void 1882397c19d1SJeff Roberson unp_internalize_fp(struct file *fp) 1883397c19d1SJeff Roberson { 1884397c19d1SJeff Roberson struct unpcb *unp; 1885397c19d1SJeff Roberson 1886397c19d1SJeff Roberson UNP_GLOBAL_WLOCK(); 1887397c19d1SJeff Roberson if ((unp = fptounp(fp)) != NULL) { 1888397c19d1SJeff Roberson unp->unp_file = fp; 1889397c19d1SJeff Roberson unp->unp_msgcount++; 1890397c19d1SJeff Roberson } 189141e0f66dSJeff Roberson fhold(fp); 1892397c19d1SJeff Roberson unp_rights++; 1893397c19d1SJeff Roberson UNP_GLOBAL_WUNLOCK(); 1894397c19d1SJeff Roberson } 1895397c19d1SJeff Roberson 1896397c19d1SJeff Roberson static void 1897397c19d1SJeff Roberson unp_externalize_fp(struct file *fp) 1898397c19d1SJeff Roberson { 1899397c19d1SJeff Roberson struct unpcb *unp; 1900397c19d1SJeff Roberson 1901397c19d1SJeff Roberson UNP_GLOBAL_WLOCK(); 1902397c19d1SJeff Roberson if ((unp = fptounp(fp)) != NULL) 1903397c19d1SJeff Roberson unp->unp_msgcount--; 1904397c19d1SJeff Roberson unp_rights--; 1905397c19d1SJeff Roberson UNP_GLOBAL_WUNLOCK(); 1906397c19d1SJeff Roberson } 1907397c19d1SJeff Roberson 1908161a0c7cSRobert Watson /* 1909a0ec558aSRobert Watson * unp_defer indicates whether additional work has been defered for a future 1910a0ec558aSRobert Watson * pass through unp_gc(). It is thread local and does not require explicit 1911a0ec558aSRobert Watson * synchronization. 1912161a0c7cSRobert Watson */ 1913397c19d1SJeff Roberson static int unp_marked; 1914397c19d1SJeff Roberson static int unp_unreachable; 1915a0ec558aSRobert Watson 1916397c19d1SJeff Roberson static void 1917397c19d1SJeff Roberson unp_accessable(struct file *fp) 1918397c19d1SJeff Roberson { 1919397c19d1SJeff Roberson struct unpcb *unp; 1920397c19d1SJeff Roberson 19216f552cb0SJeff Roberson if ((unp = fptounp(fp)) == NULL) 1922397c19d1SJeff Roberson return; 1923397c19d1SJeff Roberson if (unp->unp_gcflag & UNPGC_REF) 1924397c19d1SJeff Roberson return; 1925397c19d1SJeff Roberson unp->unp_gcflag &= ~UNPGC_DEAD; 1926397c19d1SJeff Roberson unp->unp_gcflag |= UNPGC_REF; 1927397c19d1SJeff Roberson unp_marked++; 1928397c19d1SJeff Roberson } 1929397c19d1SJeff Roberson 1930397c19d1SJeff Roberson static void 1931397c19d1SJeff Roberson unp_gc_process(struct unpcb *unp) 1932397c19d1SJeff Roberson { 1933397c19d1SJeff Roberson struct socket *soa; 1934397c19d1SJeff Roberson struct socket *so; 1935397c19d1SJeff Roberson struct file *fp; 1936397c19d1SJeff Roberson 1937397c19d1SJeff Roberson /* Already processed. */ 1938397c19d1SJeff Roberson if (unp->unp_gcflag & UNPGC_SCANNED) 1939397c19d1SJeff Roberson return; 1940397c19d1SJeff Roberson fp = unp->unp_file; 1941397c19d1SJeff Roberson /* 1942397c19d1SJeff Roberson * Check for a socket potentially in a cycle. It must be in a 1943397c19d1SJeff Roberson * queue as indicated by msgcount, and this must equal the file 1944397c19d1SJeff Roberson * reference count. Note that when msgcount is 0 the file is NULL. 1945397c19d1SJeff Roberson */ 194641e0f66dSJeff Roberson if ((unp->unp_gcflag & UNPGC_REF) == 0 && fp && 194741e0f66dSJeff Roberson unp->unp_msgcount != 0 && fp->f_count == unp->unp_msgcount) { 1948397c19d1SJeff Roberson unp->unp_gcflag |= UNPGC_DEAD; 1949397c19d1SJeff Roberson unp_unreachable++; 1950397c19d1SJeff Roberson return; 1951397c19d1SJeff Roberson } 1952397c19d1SJeff Roberson /* 1953397c19d1SJeff Roberson * Mark all sockets we reference with RIGHTS. 1954397c19d1SJeff Roberson */ 1955397c19d1SJeff Roberson so = unp->unp_socket; 1956397c19d1SJeff Roberson SOCKBUF_LOCK(&so->so_rcv); 1957397c19d1SJeff Roberson unp_scan(so->so_rcv.sb_mb, unp_accessable); 1958397c19d1SJeff Roberson SOCKBUF_UNLOCK(&so->so_rcv); 1959397c19d1SJeff Roberson /* 1960397c19d1SJeff Roberson * Mark all sockets in our accept queue. 1961397c19d1SJeff Roberson */ 1962397c19d1SJeff Roberson ACCEPT_LOCK(); 1963397c19d1SJeff Roberson TAILQ_FOREACH(soa, &so->so_comp, so_list) { 1964397c19d1SJeff Roberson SOCKBUF_LOCK(&soa->so_rcv); 1965397c19d1SJeff Roberson unp_scan(soa->so_rcv.sb_mb, unp_accessable); 1966397c19d1SJeff Roberson SOCKBUF_UNLOCK(&soa->so_rcv); 1967397c19d1SJeff Roberson } 1968397c19d1SJeff Roberson ACCEPT_UNLOCK(); 1969397c19d1SJeff Roberson unp->unp_gcflag |= UNPGC_SCANNED; 1970397c19d1SJeff Roberson } 1971a0ec558aSRobert Watson 1972a0ec558aSRobert Watson static int unp_recycled; 1973be6b1304STom Rhodes SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0, 1974be6b1304STom Rhodes "Number of unreachable sockets claimed by the garbage collector."); 1975df8bae1dSRodney W. Grimes 1976397c19d1SJeff Roberson static int unp_taskcount; 1977be6b1304STom Rhodes SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0, 1978be6b1304STom Rhodes "Number of times the garbage collector has run."); 1979397c19d1SJeff Roberson 1980f708ef1bSPoul-Henning Kamp static void 1981a0ec558aSRobert Watson unp_gc(__unused void *arg, int pending) 1982df8bae1dSRodney W. Grimes { 1983397c19d1SJeff Roberson struct unp_head *heads[] = { &unp_dhead, &unp_shead, NULL }; 1984397c19d1SJeff Roberson struct unp_head **head; 1985397c19d1SJeff Roberson struct file **unref; 1986397c19d1SJeff Roberson struct unpcb *unp; 1987397c19d1SJeff Roberson int i; 1988df8bae1dSRodney W. Grimes 1989a0ec558aSRobert Watson unp_taskcount++; 1990397c19d1SJeff Roberson UNP_GLOBAL_RLOCK(); 1991ed5b7817SJulian Elischer /* 1992397c19d1SJeff Roberson * First clear all gc flags from previous runs. 1993ed5b7817SJulian Elischer */ 1994397c19d1SJeff Roberson for (head = heads; *head != NULL; head++) 1995397c19d1SJeff Roberson LIST_FOREACH(unp, *head, unp_link) 199641e0f66dSJeff Roberson unp->unp_gcflag = 0; 1997397c19d1SJeff Roberson /* 1998397c19d1SJeff Roberson * Scan marking all reachable sockets with UNPGC_REF. Once a socket 1999397c19d1SJeff Roberson * is reachable all of the sockets it references are reachable. 2000397c19d1SJeff Roberson * Stop the scan once we do a complete loop without discovering 2001397c19d1SJeff Roberson * a new reachable socket. 2002397c19d1SJeff Roberson */ 2003df8bae1dSRodney W. Grimes do { 2004397c19d1SJeff Roberson unp_unreachable = 0; 2005397c19d1SJeff Roberson unp_marked = 0; 2006397c19d1SJeff Roberson for (head = heads; *head != NULL; head++) 2007397c19d1SJeff Roberson LIST_FOREACH(unp, *head, unp_link) 2008397c19d1SJeff Roberson unp_gc_process(unp); 2009397c19d1SJeff Roberson } while (unp_marked); 2010397c19d1SJeff Roberson UNP_GLOBAL_RUNLOCK(); 2011397c19d1SJeff Roberson if (unp_unreachable == 0) 2012397c19d1SJeff Roberson return; 2013ed5b7817SJulian Elischer /* 2014397c19d1SJeff Roberson * Allocate space for a local list of dead unpcbs. 2015ed5b7817SJulian Elischer */ 2016397c19d1SJeff Roberson unref = malloc(unp_unreachable * sizeof(struct file *), 2017397c19d1SJeff Roberson M_TEMP, M_WAITOK); 2018ed5b7817SJulian Elischer /* 2019397c19d1SJeff Roberson * Iterate looking for sockets which have been specifically marked 2020397c19d1SJeff Roberson * as as unreachable and store them locally. 2021ed5b7817SJulian Elischer */ 2022397c19d1SJeff Roberson UNP_GLOBAL_RLOCK(); 2023397c19d1SJeff Roberson for (i = 0, head = heads; *head != NULL; head++) 2024397c19d1SJeff Roberson LIST_FOREACH(unp, *head, unp_link) 2025397c19d1SJeff Roberson if (unp->unp_gcflag & UNPGC_DEAD) { 2026397c19d1SJeff Roberson unref[i++] = unp->unp_file; 202741e0f66dSJeff Roberson fhold(unp->unp_file); 2028397c19d1SJeff Roberson KASSERT(unp->unp_file != NULL, 2029397c19d1SJeff Roberson ("unp_gc: Invalid unpcb.")); 2030397c19d1SJeff Roberson KASSERT(i <= unp_unreachable, 2031397c19d1SJeff Roberson ("unp_gc: incorrect unreachable count.")); 2032397c19d1SJeff Roberson } 2033397c19d1SJeff Roberson UNP_GLOBAL_RUNLOCK(); 2034ed5b7817SJulian Elischer /* 2035397c19d1SJeff Roberson * Now flush all sockets, free'ing rights. This will free the 2036397c19d1SJeff Roberson * struct files associated with these sockets but leave each socket 2037397c19d1SJeff Roberson * with one remaining ref. 2038ed5b7817SJulian Elischer */ 2039397c19d1SJeff Roberson for (i = 0; i < unp_unreachable; i++) 2040397c19d1SJeff Roberson sorflush(unref[i]->f_data); 2041ed5b7817SJulian Elischer /* 2042397c19d1SJeff Roberson * And finally release the sockets so they can be reclaimed. 2043ed5b7817SJulian Elischer */ 2044397c19d1SJeff Roberson for (i = 0; i < unp_unreachable; i++) 2045397c19d1SJeff Roberson fdrop(unref[i], NULL); 2046397c19d1SJeff Roberson unp_recycled += unp_unreachable; 2047397c19d1SJeff Roberson free(unref, M_TEMP); 2048df8bae1dSRodney W. Grimes } 2049df8bae1dSRodney W. Grimes 205026f9a767SRodney W. Grimes void 2051892af6b9SRobert Watson unp_dispose(struct mbuf *m) 2052df8bae1dSRodney W. Grimes { 2053996c772fSJohn Dyson 2054df8bae1dSRodney W. Grimes if (m) 2055df8bae1dSRodney W. Grimes unp_scan(m, unp_discard); 2056df8bae1dSRodney W. Grimes } 2057df8bae1dSRodney W. Grimes 2058f708ef1bSPoul-Henning Kamp static void 2059892af6b9SRobert Watson unp_scan(struct mbuf *m0, void (*op)(struct file *)) 2060df8bae1dSRodney W. Grimes { 20612bc21ed9SDavid Malone struct mbuf *m; 20622bc21ed9SDavid Malone struct file **rp; 20632bc21ed9SDavid Malone struct cmsghdr *cm; 20642bc21ed9SDavid Malone void *data; 20652bc21ed9SDavid Malone int i; 20662bc21ed9SDavid Malone socklen_t clen, datalen; 2067df8bae1dSRodney W. Grimes int qfds; 2068df8bae1dSRodney W. Grimes 2069fc3fcacfSRobert Watson while (m0 != NULL) { 20702bc21ed9SDavid Malone for (m = m0; m; m = m->m_next) { 207112396bdcSDavid Malone if (m->m_type != MT_CONTROL) 2072df8bae1dSRodney W. Grimes continue; 20732bc21ed9SDavid Malone 20742bc21ed9SDavid Malone cm = mtod(m, struct cmsghdr *); 20752bc21ed9SDavid Malone clen = m->m_len; 20762bc21ed9SDavid Malone 20772bc21ed9SDavid Malone while (cm != NULL) { 20782bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) 20792bc21ed9SDavid Malone break; 20802bc21ed9SDavid Malone 20812bc21ed9SDavid Malone data = CMSG_DATA(cm); 20822bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len 20832bc21ed9SDavid Malone - (caddr_t)data; 20842bc21ed9SDavid Malone 20852bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET && 20862bc21ed9SDavid Malone cm->cmsg_type == SCM_RIGHTS) { 20872bc21ed9SDavid Malone qfds = datalen / sizeof (struct file *); 20882bc21ed9SDavid Malone rp = data; 2089df8bae1dSRodney W. Grimes for (i = 0; i < qfds; i++) 2090df8bae1dSRodney W. Grimes (*op)(*rp++); 20912bc21ed9SDavid Malone } 20922bc21ed9SDavid Malone 20932bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 20942bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 20952bc21ed9SDavid Malone cm = (struct cmsghdr *) 20962bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 20972bc21ed9SDavid Malone } else { 20982bc21ed9SDavid Malone clen = 0; 20992bc21ed9SDavid Malone cm = NULL; 21002bc21ed9SDavid Malone } 21012bc21ed9SDavid Malone } 2102df8bae1dSRodney W. Grimes } 2103df8bae1dSRodney W. Grimes m0 = m0->m_act; 2104df8bae1dSRodney W. Grimes } 2105df8bae1dSRodney W. Grimes } 2106df8bae1dSRodney W. Grimes 210703c96c31SRobert Watson #ifdef DDB 210803c96c31SRobert Watson static void 210903c96c31SRobert Watson db_print_indent(int indent) 211003c96c31SRobert Watson { 211103c96c31SRobert Watson int i; 211203c96c31SRobert Watson 211303c96c31SRobert Watson for (i = 0; i < indent; i++) 211403c96c31SRobert Watson db_printf(" "); 211503c96c31SRobert Watson } 211603c96c31SRobert Watson 211703c96c31SRobert Watson static void 211803c96c31SRobert Watson db_print_unpflags(int unp_flags) 211903c96c31SRobert Watson { 212003c96c31SRobert Watson int comma; 212103c96c31SRobert Watson 212203c96c31SRobert Watson comma = 0; 212303c96c31SRobert Watson if (unp_flags & UNP_HAVEPC) { 212403c96c31SRobert Watson db_printf("%sUNP_HAVEPC", comma ? ", " : ""); 212503c96c31SRobert Watson comma = 1; 212603c96c31SRobert Watson } 212703c96c31SRobert Watson if (unp_flags & UNP_HAVEPCCACHED) { 212803c96c31SRobert Watson db_printf("%sUNP_HAVEPCCACHED", comma ? ", " : ""); 212903c96c31SRobert Watson comma = 1; 213003c96c31SRobert Watson } 213103c96c31SRobert Watson if (unp_flags & UNP_WANTCRED) { 213203c96c31SRobert Watson db_printf("%sUNP_WANTCRED", comma ? ", " : ""); 213303c96c31SRobert Watson comma = 1; 213403c96c31SRobert Watson } 213503c96c31SRobert Watson if (unp_flags & UNP_CONNWAIT) { 213603c96c31SRobert Watson db_printf("%sUNP_CONNWAIT", comma ? ", " : ""); 213703c96c31SRobert Watson comma = 1; 213803c96c31SRobert Watson } 213903c96c31SRobert Watson if (unp_flags & UNP_CONNECTING) { 214003c96c31SRobert Watson db_printf("%sUNP_CONNECTING", comma ? ", " : ""); 214103c96c31SRobert Watson comma = 1; 214203c96c31SRobert Watson } 214303c96c31SRobert Watson if (unp_flags & UNP_BINDING) { 214403c96c31SRobert Watson db_printf("%sUNP_BINDING", comma ? ", " : ""); 214503c96c31SRobert Watson comma = 1; 214603c96c31SRobert Watson } 214703c96c31SRobert Watson } 214803c96c31SRobert Watson 214903c96c31SRobert Watson static void 215003c96c31SRobert Watson db_print_xucred(int indent, struct xucred *xu) 215103c96c31SRobert Watson { 215203c96c31SRobert Watson int comma, i; 215303c96c31SRobert Watson 215403c96c31SRobert Watson db_print_indent(indent); 215503c96c31SRobert Watson db_printf("cr_version: %u cr_uid: %u cr_ngroups: %d\n", 215603c96c31SRobert Watson xu->cr_version, xu->cr_uid, xu->cr_ngroups); 215703c96c31SRobert Watson db_print_indent(indent); 215803c96c31SRobert Watson db_printf("cr_groups: "); 215903c96c31SRobert Watson comma = 0; 216003c96c31SRobert Watson for (i = 0; i < xu->cr_ngroups; i++) { 216103c96c31SRobert Watson db_printf("%s%u", comma ? ", " : "", xu->cr_groups[i]); 216203c96c31SRobert Watson comma = 1; 216303c96c31SRobert Watson } 216403c96c31SRobert Watson db_printf("\n"); 216503c96c31SRobert Watson } 216603c96c31SRobert Watson 216703c96c31SRobert Watson static void 216803c96c31SRobert Watson db_print_unprefs(int indent, struct unp_head *uh) 216903c96c31SRobert Watson { 217003c96c31SRobert Watson struct unpcb *unp; 217103c96c31SRobert Watson int counter; 217203c96c31SRobert Watson 217303c96c31SRobert Watson counter = 0; 217403c96c31SRobert Watson LIST_FOREACH(unp, uh, unp_reflink) { 217503c96c31SRobert Watson if (counter % 4 == 0) 217603c96c31SRobert Watson db_print_indent(indent); 217703c96c31SRobert Watson db_printf("%p ", unp); 217803c96c31SRobert Watson if (counter % 4 == 3) 217903c96c31SRobert Watson db_printf("\n"); 218003c96c31SRobert Watson counter++; 218103c96c31SRobert Watson } 218203c96c31SRobert Watson if (counter != 0 && counter % 4 != 0) 218303c96c31SRobert Watson db_printf("\n"); 218403c96c31SRobert Watson } 218503c96c31SRobert Watson 218603c96c31SRobert Watson DB_SHOW_COMMAND(unpcb, db_show_unpcb) 218703c96c31SRobert Watson { 218803c96c31SRobert Watson struct unpcb *unp; 218903c96c31SRobert Watson 219003c96c31SRobert Watson if (!have_addr) { 219103c96c31SRobert Watson db_printf("usage: show unpcb <addr>\n"); 219203c96c31SRobert Watson return; 219303c96c31SRobert Watson } 219403c96c31SRobert Watson unp = (struct unpcb *)addr; 219503c96c31SRobert Watson 219603c96c31SRobert Watson db_printf("unp_socket: %p unp_vnode: %p\n", unp->unp_socket, 219703c96c31SRobert Watson unp->unp_vnode); 219803c96c31SRobert Watson 219903c96c31SRobert Watson db_printf("unp_ino: %d unp_conn: %p\n", unp->unp_ino, 220003c96c31SRobert Watson unp->unp_conn); 220103c96c31SRobert Watson 220203c96c31SRobert Watson db_printf("unp_refs:\n"); 220303c96c31SRobert Watson db_print_unprefs(2, &unp->unp_refs); 220403c96c31SRobert Watson 220503c96c31SRobert Watson /* XXXRW: Would be nice to print the full address, if any. */ 220603c96c31SRobert Watson db_printf("unp_addr: %p\n", unp->unp_addr); 220703c96c31SRobert Watson 220803c96c31SRobert Watson db_printf("unp_cc: %d unp_mbcnt: %d unp_gencnt: %llu\n", 220903c96c31SRobert Watson unp->unp_cc, unp->unp_mbcnt, 221003c96c31SRobert Watson (unsigned long long)unp->unp_gencnt); 221103c96c31SRobert Watson 221203c96c31SRobert Watson db_printf("unp_flags: %x (", unp->unp_flags); 221303c96c31SRobert Watson db_print_unpflags(unp->unp_flags); 221403c96c31SRobert Watson db_printf(")\n"); 221503c96c31SRobert Watson 221603c96c31SRobert Watson db_printf("unp_peercred:\n"); 221703c96c31SRobert Watson db_print_xucred(2, &unp->unp_peercred); 221803c96c31SRobert Watson 221903c96c31SRobert Watson db_printf("unp_refcount: %u\n", unp->unp_refcount); 222003c96c31SRobert Watson } 222103c96c31SRobert Watson #endif 2222