uipc_usrreq.c revision 171599
11558Srgrimes/*- 21558Srgrimes * Copyright (c) 1982, 1986, 1989, 1991, 1993 31558Srgrimes * The Regents of the University of California. 41558Srgrimes * Copyright (c) 2004-2007 Robert N. M. Watson 51558Srgrimes * All rights reserved. 61558Srgrimes * 71558Srgrimes * Redistribution and use in source and binary forms, with or without 81558Srgrimes * modification, are permitted provided that the following conditions 91558Srgrimes * are met: 101558Srgrimes * 1. Redistributions of source code must retain the above copyright 111558Srgrimes * notice, this list of conditions and the following disclaimer. 121558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 131558Srgrimes * notice, this list of conditions and the following disclaimer in the 141558Srgrimes * documentation and/or other materials provided with the distribution. 151558Srgrimes * 4. Neither the name of the University nor the names of its contributors 161558Srgrimes * may be used to endorse or promote products derived from this software 171558Srgrimes * without specific prior written permission. 181558Srgrimes * 191558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 201558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 211558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 221558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 231558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 241558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 251558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 261558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 271558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 281558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 291558Srgrimes * SUCH DAMAGE. 30102411Scharnier * 311558Srgrimes * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94 3223675Speter */ 33102411Scharnier 3441477Sjulian/* 35102411Scharnier * UNIX Domain (Local) Sockets 36102411Scharnier * 37102411Scharnier * This is an implementation of UNIX (local) domain sockets. Each socket has 381558Srgrimes * an associated struct unpcb (UNIX protocol control block). Stream sockets 39202109Smckusick * may be connected to 0 or 1 other socket. Datagram sockets may be 4023675Speter * connected to 0, 1, or many other sockets. Sockets may be created and 411558Srgrimes * connected in pairs (socketpair(2)), or bound/connected to using the file 421558Srgrimes * system name space. For most purposes, only the receive socket buffer is 4374556Smckusick * used, as sending on one socket delivers directly to the receive socket 4423799Sbde * buffer of a second socket. 4523675Speter * 46202109Smckusick * The implementation is substantially complicated by the fact that 47101037Smux * "ancillary data", such as file descriptors or credentials, may be passed 481558Srgrimes * across UNIX domain sockets. The potential for passing UNIX domain sockets 4923675Speter * over other UNIX domain sockets requires the implementation of a simple 501558Srgrimes * garbage collector to find and tear down cycles of disconnected sockets. 511558Srgrimes * 521558Srgrimes * TODO: 531558Srgrimes * SEQPACKET, RDM 54202107Smckusick * rethink name space problems 55202107Smckusick * need a proper out-of-band 5692839Simp * lock pushdown 5792839Simp */ 581558Srgrimes 597585Sbde#include <sys/cdefs.h> 6092839Simp__FBSDID("$FreeBSD: head/sys/kern/uipc_usrreq.c 171599 2007-07-26 16:58:09Z pjd $"); 611558Srgrimes 6298542Smckusick#include "opt_ddb.h" 6392806Sobrien#include "opt_mac.h" 641558Srgrimes 651558Srgrimes#include <sys/param.h> 6698542Smckusick#include <sys/domain.h> 6798542Smckusick#include <sys/fcntl.h> 681558Srgrimes#include <sys/malloc.h> /* XXX must be before <sys/file.h> */ 691558Srgrimes#include <sys/eventhandler.h> 7041474Sjulian#include <sys/file.h> 711558Srgrimes#include <sys/filedesc.h> 721558Srgrimes#include <sys/jail.h> 731558Srgrimes#include <sys/kernel.h> 7434266Sjulian#include <sys/lock.h> 7534266Sjulian#include <sys/mbuf.h> 7623675Speter#include <sys/mount.h> 7734266Sjulian#include <sys/mutex.h> 781558Srgrimes#include <sys/namei.h> 7923675Speter#include <sys/proc.h> 801558Srgrimes#include <sys/protosw.h> 811558Srgrimes#include <sys/resourcevar.h> 821558Srgrimes#include <sys/rwlock.h> 831558Srgrimes#include <sys/socket.h> 841558Srgrimes#include <sys/socketvar.h> 851558Srgrimes#include <sys/signalvar.h> 861558Srgrimes#include <sys/stat.h> 8723675Speter#include <sys/sx.h> 881558Srgrimes#include <sys/sysctl.h> 891558Srgrimes#include <sys/systm.h> 9034266Sjulian#include <sys/taskqueue.h> 9134266Sjulian#include <sys/un.h> 9223675Speter#include <sys/unpcb.h> 9334266Sjulian#include <sys/vnode.h> 941558Srgrimes 951558Srgrimes#ifdef DDB 961558Srgrimes#include <ddb/ddb.h> 971558Srgrimes#endif 98136281Struckman 991558Srgrimes#include <security/mac/mac_framework.h> 1001558Srgrimes 1011558Srgrimes#include <vm/uma.h> 1021558Srgrimes 10323675Speterstatic uma_zone_t unp_zone; 1041558Srgrimesstatic unp_gen_t unp_gencnt; 1051558Srgrimesstatic u_int unp_count; /* Count of local sockets. */ 10634266Sjulianstatic ino_t unp_ino; /* Prototype for fake inode numbers. */ 10734266Sjulianstatic int unp_rights; /* File descriptors in flight. */ 10823675Speterstatic struct unp_head unp_shead; /* List of local stream sockets. */ 10934266Sjulianstatic struct unp_head unp_dhead; /* List of local datagram sockets. */ 1101558Srgrimes 111134589Sscottlstatic const struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 112134589Sscottl 1131558Srgrimes/* 1141558Srgrimes * Garbage collection of cyclic file descriptor/socket references occurs 1151558Srgrimes * asynchronously in a taskqueue context in order to avoid recursion and 1161558Srgrimes * reentrance in the UNIX domain socket, file descriptor, and socket layer 117136281Struckman * code. See unp_gc() for a full description. 1181558Srgrimes */ 1191558Srgrimesstatic struct task unp_gc_task; 1201558Srgrimes 12141474Sjulian/* 12241474Sjulian * Both send and receive buffers are allocated PIPSIZ bytes of buffering for 1231558Srgrimes * stream sockets, although the total for sender and receiver is actually 12441474Sjulian * only PIPSIZ. 12596483Sphk * 12696483Sphk * Datagram sockets really use the sendspace as the maximum datagram size, 1271558Srgrimes * and don't really want to reserve the sendspace. Their recvspace should be 1281558Srgrimes * large enough for at least one max-size datagram plus address. 1291558Srgrimes */ 1301558Srgrimes#ifndef PIPSIZ 1311558Srgrimes#define PIPSIZ 8192 1321558Srgrimes#endif 1331558Srgrimesstatic u_long unpst_sendspace = PIPSIZ; 13423675Speterstatic u_long unpst_recvspace = PIPSIZ; 1351558Srgrimesstatic u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 1361558Srgrimesstatic u_long unpdg_recvspace = 4*1024; 1371558Srgrimes 1381558SrgrimesSYSCTL_NODE(_net, PF_LOCAL, local, CTLFLAG_RW, 0, "Local domain"); 13970050SiedowseSYSCTL_NODE(_net_local, SOCK_STREAM, stream, CTLFLAG_RW, 0, "SOCK_STREAM"); 140101037SmuxSYSCTL_NODE(_net_local, SOCK_DGRAM, dgram, CTLFLAG_RW, 0, "SOCK_DGRAM"); 14186514Siedowse 14286514SiedowseSYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 14370050Siedowse &unpst_sendspace, 0, ""); 14470050SiedowseSYSCTL_ULONG(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 145126345Sscottl &unpst_recvspace, 0, ""); 146126345SscottlSYSCTL_ULONG(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 147126345Sscottl &unpdg_sendspace, 0, ""); 148126345SscottlSYSCTL_ULONG(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 149126345Sscottl &unpdg_recvspace, 0, ""); 1501558SrgrimesSYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); 1511558Srgrimes 1521558Srgrimes/*- 1531558Srgrimes * Locking and synchronization: 1541558Srgrimes * 1551558Srgrimes * The global UNIX domain socket rwlock (unp_global_rwlock) protects all 1561558Srgrimes * global variables, including the linked lists tracking the set of allocated 1571558Srgrimes * UNIX domain sockets. The global rwlock also serves to prevent deadlock 158134589Sscottl * when more than one PCB lock is acquired at a time (i.e., during 1591558Srgrimes * connect()). Finally, the global rwlock protects uncounted references from 1601558Srgrimes * vnodes to sockets bound to those vnodes: to safely dereference the 1611558Srgrimes * v_socket pointer, the global rwlock must be held while a full reference is 1621558Srgrimes * acquired. 16334266Sjulian * 164101037Smux * UNIX domain sockets each have an unpcb hung off of their so_pcb pointer, 165101037Smux * allocated in pru_attach() and freed in pru_detach(). The validity of that 166101037Smux * pointer is an invariant, so no lock is required to dereference the so_pcb 16734266Sjulian * pointer if a valid socket reference is held by the caller. In practice, 168101037Smux * this is always true during operations performed on a socket. Each unpcb 169101037Smux * has a back-pointer to its socket, unp_socket, which will be stable under 170101037Smux * the same circumstances. 1711558Srgrimes * 1721558Srgrimes * This pointer may only be safely dereferenced as long as a valid reference 1731558Srgrimes * to the unpcb is held. Typically, this reference will be from the socket, 1741558Srgrimes * or from another unpcb when the referring unpcb's lock is held (in order 1751558Srgrimes * that the reference not be invalidated during use). For example, to follow 176134589Sscottl * unp->unp_conn->unp_socket, you need unlock the lock on unp, not unp_conn, 177134589Sscottl * as unp_socket remains valid as long as the reference to unp_conn is valid. 1781558Srgrimes * 1791558Srgrimes * Fields of unpcbss are locked using a per-unpcb lock, unp_mtx. Individual 1801558Srgrimes * atomic reads without the lock may be performed "lockless", but more 18198542Smckusick * complex reads and read-modify-writes require the mutex to be held. No 18298542Smckusick * lock order is defined between unpcb locks -- multiple unpcb locks may be 183134589Sscottl * acquired at the same time only when holding the global UNIX domain socket 184134589Sscottl * rwlock exclusively, which prevents deadlocks. 18598542Smckusick * 18698542Smckusick * Blocking with UNIX domain sockets is a tricky issue: unlike most network 18798542Smckusick * protocols, bind() is a non-atomic operation, and connect() requires 188134589Sscottl * potential sleeping in the protocol, due to potentially waiting on local or 18998542Smckusick * distributed file systems. We try to separate "lookup" operations, which 19098542Smckusick * may sleep, and the IPC operations themselves, which typically can occur 191134589Sscottl * with relative atomicity as locks can be held over the entire operation. 1921558Srgrimes * 1931558Srgrimes * Another tricky issue is simultaneous multi-threaded or multi-process 1941558Srgrimes * access to a single UNIX domain socket. These are handled by the flags 1951558Srgrimes * UNP_CONNECTING and UNP_BINDING, which prevent concurrent connecting or 1961558Srgrimes * binding, both of which involve dropping UNIX domain socket locks in order 1971558Srgrimes * to perform namei() and other file system operations. 1981558Srgrimes */ 1991558Srgrimesstatic struct rwlock unp_global_rwlock; 2001558Srgrimes 2011558Srgrimes#define UNP_GLOBAL_LOCK_INIT() rw_init(&unp_global_rwlock, \ 2021558Srgrimes "unp_global_rwlock") 2031558Srgrimes 20441474Sjulian#define UNP_GLOBAL_LOCK_ASSERT() rw_assert(&unp_global_rwlock, \ 205136281Struckman RA_LOCKED) 20641474Sjulian#define UNP_GLOBAL_UNLOCK_ASSERT() rw_assert(&unp_global_rwlock, \ 2071558Srgrimes RA_UNLOCKED) 2081558Srgrimes 2091558Srgrimes#define UNP_GLOBAL_WLOCK() rw_wlock(&unp_global_rwlock) 2101558Srgrimes#define UNP_GLOBAL_WUNLOCK() rw_wunlock(&unp_global_rwlock) 2111558Srgrimes#define UNP_GLOBAL_WLOCK_ASSERT() rw_assert(&unp_global_rwlock, \ 2121558Srgrimes RA_WLOCKED) 2131558Srgrimes#define UNP_GLOBAL_WOWNED() rw_wowned(&unp_global_rwlock) 2141558Srgrimes 2151558Srgrimes#define UNP_GLOBAL_RLOCK() rw_rlock(&unp_global_rwlock) 21641474Sjulian#define UNP_GLOBAL_RUNLOCK() rw_runlock(&unp_global_rwlock) 2171558Srgrimes#define UNP_GLOBAL_RLOCK_ASSERT() rw_assert(&unp_global_rwlock, \ 2181558Srgrimes RA_RLOCKED) 219202109Smckusick 220202109Smckusick#define UNP_PCB_LOCK_INIT(unp) mtx_init(&(unp)->unp_mtx, \ 221202109Smckusick "unp_mtx", "unp_mtx", \ 222202109Smckusick MTX_DUPOK|MTX_DEF|MTX_RECURSE) 223202109Smckusick#define UNP_PCB_LOCK_DESTROY(unp) mtx_destroy(&(unp)->unp_mtx) 224202109Smckusick#define UNP_PCB_LOCK(unp) mtx_lock(&(unp)->unp_mtx) 225202109Smckusick#define UNP_PCB_UNLOCK(unp) mtx_unlock(&(unp)->unp_mtx) 226241012Smdf#define UNP_PCB_LOCK_ASSERT(unp) mtx_assert(&(unp)->unp_mtx, MA_OWNED) 227241012Smdf 228202109Smckusickstatic int unp_connect(struct socket *, struct sockaddr *, 229241012Smdf struct thread *); 230241012Smdfstatic int unp_connect2(struct socket *so, struct socket *so2, int); 231202109Smckusickstatic void unp_disconnect(struct unpcb *unp, struct unpcb *unp2); 232241012Smdfstatic void unp_shutdown(struct unpcb *); 233241012Smdfstatic void unp_drop(struct unpcb *, int); 234202109Smckusickstatic void unp_gc(__unused void *, int); 235202109Smckusickstatic void unp_scan(struct mbuf *, void (*)(struct file *)); 236202109Smckusickstatic void unp_mark(struct file *); 237202109Smckusickstatic void unp_discard(struct file *); 238202109Smckusickstatic void unp_freerights(struct file **, int); 239202109Smckusickstatic int unp_internalize(struct mbuf **, struct thread *); 240202109Smckusickstatic struct mbuf *unp_addsockcred(struct thread *, struct mbuf *); 241202109Smckusick 242202109Smckusick/* 243202109Smckusick * Definitions of protocols supported in the LOCAL domain. 244202109Smckusick */ 245202109Smckusickstatic struct domain localdomain; 246202109Smckusickstatic struct protosw localsw[] = { 247202109Smckusick{ 248202109Smckusick .pr_type = SOCK_STREAM, 249202109Smckusick .pr_domain = &localdomain, 250202109Smckusick .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_RIGHTS, 251202109Smckusick .pr_ctloutput = &uipc_ctloutput, 252202109Smckusick .pr_usrreqs = &uipc_usrreqs 253202109Smckusick}, 254202109Smckusick{ 255202109Smckusick .pr_type = SOCK_DGRAM, 256202109Smckusick .pr_domain = &localdomain, 2571558Srgrimes .pr_flags = PR_ATOMIC|PR_ADDR|PR_RIGHTS, 258202109Smckusick .pr_usrreqs = &uipc_usrreqs 259202109Smckusick}, 260202109Smckusick}; 261202109Smckusick 262202109Smckusickstatic struct domain localdomain = { 26341474Sjulian .dom_family = AF_LOCAL, 26441474Sjulian .dom_name = "local", 2651558Srgrimes .dom_init = unp_init, 2661558Srgrimes .dom_externalize = unp_externalize, 2671558Srgrimes .dom_dispose = unp_dispose, 2681558Srgrimes .dom_protosw = localsw, 2691558Srgrimes .dom_protoswNPROTOSW = &localsw[sizeof(localsw)/sizeof(localsw[0])] 2701558Srgrimes}; 2711558SrgrimesDOMAIN_SET(local); 2721558Srgrimes 2731558Srgrimesstatic void 27423675Speteruipc_abort(struct socket *so) 27592839Simp{ 2761558Srgrimes struct unpcb *unp, *unp2; 27792806Sobrien 278208330Smckusick unp = sotounpcb(so); 27992806Sobrien KASSERT(unp != NULL, ("uipc_abort: unp == NULL")); 2801558Srgrimes 28198542Smckusick UNP_GLOBAL_WLOCK(); 282100935Sphk UNP_PCB_LOCK(unp); 2831558Srgrimes unp2 = unp->unp_conn; 2841558Srgrimes if (unp2 != NULL) { 2851558Srgrimes UNP_PCB_LOCK(unp2); 2861558Srgrimes unp_drop(unp2, ECONNABORTED); 2871558Srgrimes UNP_PCB_UNLOCK(unp2); 288176574Sdelphij } 289176574Sdelphij UNP_PCB_UNLOCK(unp); 2901558Srgrimes UNP_GLOBAL_WUNLOCK(); 2911558Srgrimes} 2921558Srgrimes 2931558Srgrimesstatic int 2941558Srgrimesuipc_accept(struct socket *so, struct sockaddr **nam) 2951558Srgrimes{ 2961558Srgrimes struct unpcb *unp, *unp2; 2971558Srgrimes const struct sockaddr *sa; 2981558Srgrimes 29996483Sphk /* 3001558Srgrimes * Pass back name of connected socket, if it was bound and we are 3011558Srgrimes * still connected (our peer may have closed already!). 3021558Srgrimes */ 3031558Srgrimes unp = sotounpcb(so); 3041558Srgrimes KASSERT(unp != NULL, ("uipc_accept: unp == NULL")); 3051558Srgrimes 3061558Srgrimes *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 3071558Srgrimes UNP_GLOBAL_RLOCK(); 3081558Srgrimes unp2 = unp->unp_conn; 30996483Sphk if (unp2 != NULL && unp2->unp_addr != NULL) { 3101558Srgrimes UNP_PCB_LOCK(unp2); 3111558Srgrimes sa = (struct sockaddr *) unp2->unp_addr; 3121558Srgrimes bcopy(sa, *nam, sa->sa_len); 3131558Srgrimes UNP_PCB_UNLOCK(unp2); 3141558Srgrimes } else { 3151558Srgrimes sa = &sun_noname; 3161558Srgrimes bcopy(sa, *nam, sa->sa_len); 3171558Srgrimes } 3181558Srgrimes UNP_GLOBAL_RUNLOCK(); 3191558Srgrimes return (0); 32023675Speter} 3211558Srgrimes 3221558Srgrimesstatic int 3231558Srgrimesuipc_attach(struct socket *so, int proto, struct thread *td) 3241558Srgrimes{ 3251558Srgrimes u_long sendspace, recvspace; 32623675Speter struct unpcb *unp; 3271558Srgrimes int error, locked; 32841474Sjulian 3291558Srgrimes KASSERT(so->so_pcb == NULL, ("uipc_attach: so_pcb != NULL")); 33023675Speter if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 3311558Srgrimes switch (so->so_type) { 3321558Srgrimes case SOCK_STREAM: 3331558Srgrimes sendspace = unpst_sendspace; 3341558Srgrimes recvspace = unpst_recvspace; 3351558Srgrimes break; 3361558Srgrimes 3371558Srgrimes case SOCK_DGRAM: 3381558Srgrimes sendspace = unpdg_sendspace; 3391558Srgrimes recvspace = unpdg_recvspace; 34096483Sphk break; 3411558Srgrimes 3421558Srgrimes default: 3431558Srgrimes panic("uipc_attach"); 3441558Srgrimes } 3451558Srgrimes error = soreserve(so, sendspace, recvspace); 3461558Srgrimes if (error) 3471558Srgrimes return (error); 3481558Srgrimes } 3491558Srgrimes unp = uma_zalloc(unp_zone, M_NOWAIT | M_ZERO); 3501558Srgrimes if (unp == NULL) 35141474Sjulian return (ENOBUFS); 3521558Srgrimes LIST_INIT(&unp->unp_refs); 35323675Speter UNP_PCB_LOCK_INIT(unp); 3541558Srgrimes unp->unp_socket = so; 3551558Srgrimes so->so_pcb = unp; 3561558Srgrimes unp->unp_refcount = 1; 3571558Srgrimes 35896483Sphk /* 3591558Srgrimes * uipc_attach() may be called indirectly from within the UNIX domain 3601558Srgrimes * socket code via sonewconn() in unp_connect(). Since rwlocks can 3611558Srgrimes * not be recursed, we do the closest thing. 3621558Srgrimes */ 3631558Srgrimes locked = 0; 3641558Srgrimes if (!UNP_GLOBAL_WOWNED()) { 3651558Srgrimes UNP_GLOBAL_WLOCK(); 3661558Srgrimes locked = 1; 3671558Srgrimes } 3681558Srgrimes unp->unp_gencnt = ++unp_gencnt; 3691558Srgrimes unp_count++; 3701558Srgrimes LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead : &unp_shead, 3711558Srgrimes unp, unp_link); 3721558Srgrimes if (locked) 3731558Srgrimes UNP_GLOBAL_WUNLOCK(); 3741558Srgrimes 3751558Srgrimes return (0); 3761558Srgrimes} 3771558Srgrimes 3781558Srgrimesstatic int 3791558Srgrimesuipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 3801558Srgrimes{ 3811558Srgrimes struct sockaddr_un *soun = (struct sockaddr_un *)nam; 38223675Speter struct vattr vattr; 3831558Srgrimes int error, namelen, vfslocked; 3841558Srgrimes struct nameidata nd; 3851558Srgrimes struct unpcb *unp; 3861558Srgrimes struct vnode *vp; 3871558Srgrimes struct mount *mp; 38841474Sjulian char *buf; 3891558Srgrimes 3901558Srgrimes unp = sotounpcb(so); 3911558Srgrimes KASSERT(unp != NULL, ("uipc_bind: unp == NULL")); 3921558Srgrimes 3931558Srgrimes namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 3941558Srgrimes if (namelen <= 0) 3951558Srgrimes return (EINVAL); 3961558Srgrimes 3971558Srgrimes /* 3981558Srgrimes * We don't allow simultaneous bind() calls on a single UNIX domain 3991558Srgrimes * socket, so flag in-progress operations, and return an error if an 4001558Srgrimes * operation is already in progress. 4011558Srgrimes * 4021558Srgrimes * Historically, we have not allowed a socket to be rebound, so this 4031558Srgrimes * also returns an error. Not allowing re-binding simplifies the 4041558Srgrimes * implementation and avoids a great many possible failure modes. 4051558Srgrimes */ 4061558Srgrimes UNP_PCB_LOCK(unp); 4071558Srgrimes if (unp->unp_vnode != NULL) { 4081558Srgrimes UNP_PCB_UNLOCK(unp); 4091558Srgrimes return (EINVAL); 4101558Srgrimes } 4111558Srgrimes if (unp->unp_flags & UNP_BINDING) { 4121558Srgrimes UNP_PCB_UNLOCK(unp); 4131558Srgrimes return (EALREADY); 4141558Srgrimes } 4151558Srgrimes unp->unp_flags |= UNP_BINDING; 41696483Sphk UNP_PCB_UNLOCK(unp); 41723675Speter 41823675Speter buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 41923675Speter strlcpy(buf, soun->sun_path, namelen + 1); 42023675Speter 42123675Speterrestart: 42223675Speter vfslocked = 0; 4231558Srgrimes NDINIT(&nd, CREATE, MPSAFE | NOFOLLOW | LOCKPARENT | SAVENAME, 4241558Srgrimes UIO_SYSSPACE, buf, td); 42541474Sjulian/* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 4261558Srgrimes error = namei(&nd); 4271558Srgrimes if (error) 4281558Srgrimes goto error; 4291558Srgrimes vp = nd.ni_vp; 4301558Srgrimes vfslocked = NDHASGIANT(&nd); 4311558Srgrimes if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 4321558Srgrimes NDFREE(&nd, NDF_ONLY_PNBUF); 4331558Srgrimes if (nd.ni_dvp == vp) 4341558Srgrimes vrele(nd.ni_dvp); 4351558Srgrimes else 4361558Srgrimes vput(nd.ni_dvp); 43741474Sjulian if (vp != NULL) { 4381558Srgrimes vrele(vp); 43934266Sjulian error = EADDRINUSE; 4401558Srgrimes goto error; 441208330Smckusick } 4421558Srgrimes error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 4431558Srgrimes if (error) 444208330Smckusick goto error; 445208330Smckusick VFS_UNLOCK_GIANT(vfslocked); 446208330Smckusick goto restart; 447241012Smdf } 448241012Smdf VATTR_NULL(&vattr); 449208330Smckusick vattr.va_type = VSOCK; 450208330Smckusick vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 451208330Smckusick#ifdef MAC 452208330Smckusick error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 453208330Smckusick &vattr); 454208330Smckusick#endif 455208330Smckusick if (error == 0) { 456208330Smckusick VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE); 457208330Smckusick error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 458208330Smckusick } 459208330Smckusick NDFREE(&nd, NDF_ONLY_PNBUF); 460208330Smckusick vput(nd.ni_dvp); 461208330Smckusick if (error) { 462208330Smckusick vn_finished_write(mp); 463208330Smckusick goto error; 464208330Smckusick } 465208330Smckusick vp = nd.ni_vp; 466208330Smckusick ASSERT_VOP_ELOCKED(vp, "uipc_bind"); 467208330Smckusick soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 468208330Smckusick 469208330Smckusick UNP_GLOBAL_WLOCK(); 470208330Smckusick UNP_PCB_LOCK(unp); 471208330Smckusick vp->v_socket = unp->unp_socket; 4721558Srgrimes unp->unp_vnode = vp; 4731558Srgrimes unp->unp_addr = soun; 4741558Srgrimes unp->unp_flags &= ~UNP_BINDING; 4751558Srgrimes UNP_PCB_UNLOCK(unp); 4761558Srgrimes UNP_GLOBAL_WUNLOCK(); 47741474Sjulian VOP_UNLOCK(vp, 0, td); 47898542Smckusick vn_finished_write(mp); 47998542Smckusick VFS_UNLOCK_GIANT(vfslocked); 4801558Srgrimes free(buf, M_TEMP); 4811558Srgrimes return (0); 4821558Srgrimes 483136281Struckmanerror: 48441474Sjulian VFS_UNLOCK_GIANT(vfslocked); 48541474Sjulian UNP_PCB_LOCK(unp); 486102411Scharnier unp->unp_flags &= ~UNP_BINDING; 4871558Srgrimes UNP_PCB_UNLOCK(unp); 4881558Srgrimes free(buf, M_TEMP); 4891558Srgrimes return (error); 490202107Smckusick} 491202107Smckusick 492202107Smckusickstatic int 493202107Smckusickuipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 49474556Smckusick{ 4951558Srgrimes int error; 496102411Scharnier 4971558Srgrimes KASSERT(td == curthread, ("uipc_connect: td != curthread")); 4981558Srgrimes UNP_GLOBAL_WLOCK(); 499136281Struckman error = unp_connect(so, nam, td); 50096483Sphk UNP_GLOBAL_WUNLOCK(); 5011558Srgrimes return (error); 5021558Srgrimes} 50341474Sjulian 5041558Srgrimesstatic void 5051558Srgrimesuipc_close(struct socket *so) 5061558Srgrimes{ 50741474Sjulian struct unpcb *unp, *unp2; 5081558Srgrimes 5091558Srgrimes unp = sotounpcb(so); 5101558Srgrimes KASSERT(unp != NULL, ("uipc_close: unp == NULL")); 511241012Smdf 512241012Smdf UNP_GLOBAL_WLOCK(); 513241012Smdf UNP_PCB_LOCK(unp); 5141558Srgrimes unp2 = unp->unp_conn; 5151558Srgrimes if (unp2 != NULL) { 5161558Srgrimes UNP_PCB_LOCK(unp2); 5171558Srgrimes unp_disconnect(unp, unp2); 5181558Srgrimes UNP_PCB_UNLOCK(unp2); 5191558Srgrimes } 5201558Srgrimes UNP_PCB_UNLOCK(unp); 5211558Srgrimes UNP_GLOBAL_WUNLOCK(); 522202107Smckusick} 523202107Smckusick 524202107Smckusickint 525202109Smckusickuipc_connect2(struct socket *so1, struct socket *so2) 526202107Smckusick{ 527202107Smckusick struct unpcb *unp, *unp2; 528202107Smckusick int error; 529221110Sdes 530202107Smckusick UNP_GLOBAL_WLOCK(); 531202107Smckusick unp = so1->so_pcb; 532202107Smckusick KASSERT(unp != NULL, ("uipc_connect2: unp == NULL")); 533202107Smckusick UNP_PCB_LOCK(unp); 534202107Smckusick unp2 = so2->so_pcb; 535202107Smckusick KASSERT(unp2 != NULL, ("uipc_connect2: unp2 == NULL")); 536202107Smckusick UNP_PCB_LOCK(unp2); 537202107Smckusick error = unp_connect2(so1, so2, PRU_CONNECT2); 538202107Smckusick UNP_PCB_UNLOCK(unp2); 539202107Smckusick UNP_PCB_UNLOCK(unp); 540202107Smckusick UNP_GLOBAL_WUNLOCK(); 541202107Smckusick return (error); 542202107Smckusick} 543202107Smckusick 544202107Smckusick/* control is EOPNOTSUPP */ 545202107Smckusick 546202107Smckusickstatic void 547202107Smckusickuipc_detach(struct socket *so) 548202107Smckusick{ 549202107Smckusick struct unpcb *unp, *unp2; 550202107Smckusick struct sockaddr_un *saved_unp_addr; 551202107Smckusick struct vnode *vp; 552202107Smckusick int freeunp, local_unp_rights; 553202107Smckusick 554202107Smckusick unp = sotounpcb(so); 555202107Smckusick KASSERT(unp != NULL, ("uipc_detach: unp == NULL")); 556202107Smckusick 557202107Smckusick UNP_GLOBAL_WLOCK(); 558202107Smckusick UNP_PCB_LOCK(unp); 559202107Smckusick 560202107Smckusick LIST_REMOVE(unp, unp_link); 561202107Smckusick unp->unp_gencnt = ++unp_gencnt; 562202107Smckusick --unp_count; 563202107Smckusick 564202107Smckusick /* 565202107Smckusick * XXXRW: Should assert vp->v_socket == so. 566202107Smckusick */ 567202109Smckusick if ((vp = unp->unp_vnode) != NULL) { 568202107Smckusick unp->unp_vnode->v_socket = NULL; 569202107Smckusick unp->unp_vnode = NULL; 570202107Smckusick } 571202107Smckusick unp2 = unp->unp_conn; 572202107Smckusick if (unp2 != NULL) { 573202107Smckusick UNP_PCB_LOCK(unp2); 574202107Smckusick unp_disconnect(unp, unp2); 575202109Smckusick UNP_PCB_UNLOCK(unp2); 576202109Smckusick } 577202109Smckusick 578202109Smckusick /* 579202109Smckusick * We hold the global lock, so it's OK to acquire multiple pcb locks 580202109Smckusick * at a time. 581202131Smckusick */ 582202109Smckusick while (!LIST_EMPTY(&unp->unp_refs)) { 583202109Smckusick struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 584202109Smckusick 585202109Smckusick UNP_PCB_LOCK(ref); 586202109Smckusick unp_drop(ref, ECONNRESET); 587202109Smckusick UNP_PCB_UNLOCK(ref); 588202109Smckusick } 589202109Smckusick UNP_GLOBAL_WUNLOCK(); 590202107Smckusick unp->unp_socket->so_pcb = NULL; 591202107Smckusick local_unp_rights = unp_rights; 592202107Smckusick saved_unp_addr = unp->unp_addr; 593202107Smckusick unp->unp_addr = NULL; 594202107Smckusick unp->unp_refcount--; 595202107Smckusick freeunp = (unp->unp_refcount == 0); 596202107Smckusick if (saved_unp_addr != NULL) 597202107Smckusick FREE(saved_unp_addr, M_SONAME); 598202107Smckusick if (freeunp) { 599202107Smckusick UNP_PCB_LOCK_DESTROY(unp); 600202107Smckusick uma_zfree(unp_zone, unp); 601202107Smckusick } else 602202107Smckusick UNP_PCB_UNLOCK(unp); 603202107Smckusick if (vp) { 604202109Smckusick int vfslocked; 605202107Smckusick 606202107Smckusick vfslocked = VFS_LOCK_GIANT(vp->v_mount); 607202107Smckusick vrele(vp); 608202107Smckusick VFS_UNLOCK_GIANT(vfslocked); 609202107Smckusick } 610202107Smckusick if (local_unp_rights) 611202107Smckusick taskqueue_enqueue(taskqueue_thread, &unp_gc_task); 612202109Smckusick} 613202109Smckusick 614202109Smckusickstatic int 615202109Smckusickuipc_disconnect(struct socket *so) 616202109Smckusick{ 617202109Smckusick struct unpcb *unp, *unp2; 618229403Sed 619202109Smckusick unp = sotounpcb(so); 620202109Smckusick KASSERT(unp != NULL, ("uipc_disconnect: unp == NULL")); 621202109Smckusick 622202131Smckusick UNP_GLOBAL_WLOCK(); 623202109Smckusick UNP_PCB_LOCK(unp); 624202109Smckusick unp2 = unp->unp_conn; 625202109Smckusick if (unp2 != NULL) { 626202109Smckusick UNP_PCB_LOCK(unp2); 627202109Smckusick unp_disconnect(unp, unp2); 628202109Smckusick UNP_PCB_UNLOCK(unp2); 629202109Smckusick } 630202109Smckusick UNP_PCB_UNLOCK(unp); 631202109Smckusick UNP_GLOBAL_WUNLOCK(); 632202107Smckusick return (0); 633202107Smckusick} 634202107Smckusick 635202107Smckusickstatic int 636202107Smckusickuipc_listen(struct socket *so, int backlog, struct thread *td) 637202107Smckusick{ 638202107Smckusick struct unpcb *unp; 639202107Smckusick int error; 640202107Smckusick 641202107Smckusick unp = sotounpcb(so); 642202107Smckusick KASSERT(unp != NULL, ("uipc_listen: unp == NULL")); 643202107Smckusick 644202107Smckusick UNP_PCB_LOCK(unp); 645202107Smckusick if (unp->unp_vnode == NULL) { 646202107Smckusick UNP_PCB_UNLOCK(unp); 647202107Smckusick return (EINVAL); 648202107Smckusick } 649202107Smckusick 650202107Smckusick SOCK_LOCK(so); 651202107Smckusick error = solisten_proto_check(so); 652202107Smckusick if (error == 0) { 653202107Smckusick cru2x(td->td_ucred, &unp->unp_peercred); 654202107Smckusick unp->unp_flags |= UNP_HAVEPCCACHED; 655202107Smckusick solisten_proto(so, backlog); 656202107Smckusick } 657202107Smckusick SOCK_UNLOCK(so); 658202107Smckusick UNP_PCB_UNLOCK(unp); 6591558Srgrimes return (error); 6601558Srgrimes} 6611558Srgrimes 66223675Speterstatic int 66392839Simpuipc_peeraddr(struct socket *so, struct sockaddr **nam) 6641558Srgrimes{ 6651558Srgrimes struct unpcb *unp, *unp2; 666100935Sphk const struct sockaddr *sa; 667100935Sphk 6681558Srgrimes unp = sotounpcb(so); 669 KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL")); 670 671 *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 672 UNP_PCB_LOCK(unp); 673 /* 674 * XXX: It seems that this test always fails even when connection is 675 * established. So, this else clause is added as workaround to 676 * return PF_LOCAL sockaddr. 677 */ 678 unp2 = unp->unp_conn; 679 if (unp2 != NULL) { 680 UNP_PCB_LOCK(unp2); 681 if (unp2->unp_addr != NULL) 682 sa = (struct sockaddr *) unp->unp_conn->unp_addr; 683 else 684 sa = &sun_noname; 685 bcopy(sa, *nam, sa->sa_len); 686 UNP_PCB_UNLOCK(unp2); 687 } else { 688 sa = &sun_noname; 689 bcopy(sa, *nam, sa->sa_len); 690 } 691 UNP_PCB_UNLOCK(unp); 692 return (0); 693} 694 695static int 696uipc_rcvd(struct socket *so, int flags) 697{ 698 struct unpcb *unp, *unp2; 699 struct socket *so2; 700 u_int mbcnt, sbcc; 701 u_long newhiwat; 702 703 unp = sotounpcb(so); 704 KASSERT(unp != NULL, ("uipc_rcvd: unp == NULL")); 705 706 if (so->so_type == SOCK_DGRAM) 707 panic("uipc_rcvd DGRAM?"); 708 709 if (so->so_type != SOCK_STREAM) 710 panic("uipc_rcvd unknown socktype"); 711 712 /* 713 * Adjust backpressure on sender and wakeup any waiting to write. 714 * 715 * The unp lock is acquired to maintain the validity of the unp_conn 716 * pointer; no lock on unp2 is required as unp2->unp_socket will be 717 * static as long as we don't permit unp2 to disconnect from unp, 718 * which is prevented by the lock on unp. We cache values from 719 * so_rcv to avoid holding the so_rcv lock over the entire 720 * transaction on the remote so_snd. 721 */ 722 SOCKBUF_LOCK(&so->so_rcv); 723 mbcnt = so->so_rcv.sb_mbcnt; 724 sbcc = so->so_rcv.sb_cc; 725 SOCKBUF_UNLOCK(&so->so_rcv); 726 UNP_PCB_LOCK(unp); 727 unp2 = unp->unp_conn; 728 if (unp2 == NULL) { 729 UNP_PCB_UNLOCK(unp); 730 return (0); 731 } 732 so2 = unp2->unp_socket; 733 SOCKBUF_LOCK(&so2->so_snd); 734 so2->so_snd.sb_mbmax += unp->unp_mbcnt - mbcnt; 735 newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - sbcc; 736 (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat, 737 newhiwat, RLIM_INFINITY); 738 sowwakeup_locked(so2); 739 unp->unp_mbcnt = mbcnt; 740 unp->unp_cc = sbcc; 741 UNP_PCB_UNLOCK(unp); 742 return (0); 743} 744 745/* pru_rcvoob is EOPNOTSUPP */ 746 747static int 748uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 749 struct mbuf *control, struct thread *td) 750{ 751 struct unpcb *unp, *unp2; 752 struct socket *so2; 753 u_int mbcnt, sbcc; 754 u_long newhiwat; 755 int error = 0; 756 757 unp = sotounpcb(so); 758 KASSERT(unp != NULL, ("uipc_send: unp == NULL")); 759 760 if (flags & PRUS_OOB) { 761 error = EOPNOTSUPP; 762 goto release; 763 } 764 765 if (control != NULL && (error = unp_internalize(&control, td))) 766 goto release; 767 768 if ((nam != NULL) || (flags & PRUS_EOF)) 769 UNP_GLOBAL_WLOCK(); 770 else 771 UNP_GLOBAL_RLOCK(); 772 773 switch (so->so_type) { 774 case SOCK_DGRAM: 775 { 776 const struct sockaddr *from; 777 778 unp2 = unp->unp_conn; 779 if (nam != NULL) { 780 UNP_GLOBAL_WLOCK_ASSERT(); 781 if (unp2 != NULL) { 782 error = EISCONN; 783 break; 784 } 785 error = unp_connect(so, nam, td); 786 if (error) 787 break; 788 unp2 = unp->unp_conn; 789 } 790 /* 791 * Because connect() and send() are non-atomic in a sendto() 792 * with a target address, it's possible that the socket will 793 * have disconnected before the send() can run. In that case 794 * return the slightly counter-intuitive but otherwise 795 * correct error that the socket is not connected. 796 */ 797 if (unp2 == NULL) { 798 error = ENOTCONN; 799 break; 800 } 801 /* Lockless read. */ 802 if (unp2->unp_flags & UNP_WANTCRED) 803 control = unp_addsockcred(td, control); 804 UNP_PCB_LOCK(unp); 805 if (unp->unp_addr != NULL) 806 from = (struct sockaddr *)unp->unp_addr; 807 else 808 from = &sun_noname; 809 so2 = unp2->unp_socket; 810 SOCKBUF_LOCK(&so2->so_rcv); 811 if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) { 812 sorwakeup_locked(so2); 813 m = NULL; 814 control = NULL; 815 } else { 816 SOCKBUF_UNLOCK(&so2->so_rcv); 817 error = ENOBUFS; 818 } 819 if (nam != NULL) { 820 UNP_GLOBAL_WLOCK_ASSERT(); 821 UNP_PCB_LOCK(unp2); 822 unp_disconnect(unp, unp2); 823 UNP_PCB_UNLOCK(unp2); 824 } 825 UNP_PCB_UNLOCK(unp); 826 break; 827 } 828 829 case SOCK_STREAM: 830 /* 831 * Connect if not connected yet. 832 * 833 * Note: A better implementation would complain if not equal 834 * to the peer's address. 835 */ 836 if ((so->so_state & SS_ISCONNECTED) == 0) { 837 if (nam != NULL) { 838 UNP_GLOBAL_WLOCK_ASSERT(); 839 error = unp_connect(so, nam, td); 840 if (error) 841 break; /* XXX */ 842 } else { 843 error = ENOTCONN; 844 break; 845 } 846 } 847 848 /* Lockless read. */ 849 if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 850 error = EPIPE; 851 break; 852 } 853 /* 854 * Because connect() and send() are non-atomic in a sendto() 855 * with a target address, it's possible that the socket will 856 * have disconnected before the send() can run. In that case 857 * return the slightly counter-intuitive but otherwise 858 * correct error that the socket is not connected. 859 * 860 * Locking here must be done carefully: the global lock 861 * prevents interconnections between unpcbs from changing, so 862 * we can traverse from unp to unp2 without acquiring unp's 863 * lock. Socket buffer locks follow unpcb locks, so we can 864 * acquire both remote and lock socket buffer locks. 865 */ 866 unp2 = unp->unp_conn; 867 if (unp2 == NULL) { 868 error = ENOTCONN; 869 break; 870 } 871 so2 = unp2->unp_socket; 872 UNP_PCB_LOCK(unp2); 873 SOCKBUF_LOCK(&so2->so_rcv); 874 if (unp2->unp_flags & UNP_WANTCRED) { 875 /* 876 * Credentials are passed only once on SOCK_STREAM. 877 */ 878 unp2->unp_flags &= ~UNP_WANTCRED; 879 control = unp_addsockcred(td, control); 880 } 881 /* 882 * Send to paired receive port, and then reduce send buffer 883 * hiwater marks to maintain backpressure. Wake up readers. 884 */ 885 if (control != NULL) { 886 if (sbappendcontrol_locked(&so2->so_rcv, m, control)) 887 control = NULL; 888 } else 889 sbappend_locked(&so2->so_rcv, m); 890 mbcnt = so2->so_rcv.sb_mbcnt - unp2->unp_mbcnt; 891 unp2->unp_mbcnt = so2->so_rcv.sb_mbcnt; 892 sbcc = so2->so_rcv.sb_cc; 893 sorwakeup_locked(so2); 894 895 SOCKBUF_LOCK(&so->so_snd); 896 newhiwat = so->so_snd.sb_hiwat - (sbcc - unp2->unp_cc); 897 (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat, 898 newhiwat, RLIM_INFINITY); 899 so->so_snd.sb_mbmax -= mbcnt; 900 SOCKBUF_UNLOCK(&so->so_snd); 901 unp2->unp_cc = sbcc; 902 UNP_PCB_UNLOCK(unp2); 903 m = NULL; 904 break; 905 906 default: 907 panic("uipc_send unknown socktype"); 908 } 909 910 /* 911 * SEND_EOF is equivalent to a SEND followed by a SHUTDOWN. 912 */ 913 if (flags & PRUS_EOF) { 914 UNP_PCB_LOCK(unp); 915 socantsendmore(so); 916 unp_shutdown(unp); 917 UNP_PCB_UNLOCK(unp); 918 } 919 920 if ((nam != NULL) || (flags & PRUS_EOF)) 921 UNP_GLOBAL_WUNLOCK(); 922 else 923 UNP_GLOBAL_RUNLOCK(); 924 925 if (control != NULL && error != 0) 926 unp_dispose(control); 927 928release: 929 if (control != NULL) 930 m_freem(control); 931 if (m != NULL) 932 m_freem(m); 933 return (error); 934} 935 936static int 937uipc_sense(struct socket *so, struct stat *sb) 938{ 939 struct unpcb *unp, *unp2; 940 struct socket *so2; 941 942 unp = sotounpcb(so); 943 KASSERT(unp != NULL, ("uipc_sense: unp == NULL")); 944 945 sb->st_blksize = so->so_snd.sb_hiwat; 946 UNP_GLOBAL_RLOCK(); 947 UNP_PCB_LOCK(unp); 948 unp2 = unp->unp_conn; 949 if (so->so_type == SOCK_STREAM && unp2 != NULL) { 950 so2 = unp2->unp_socket; 951 sb->st_blksize += so2->so_rcv.sb_cc; 952 } 953 sb->st_dev = NODEV; 954 if (unp->unp_ino == 0) 955 unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 956 sb->st_ino = unp->unp_ino; 957 UNP_PCB_UNLOCK(unp); 958 UNP_GLOBAL_RUNLOCK(); 959 return (0); 960} 961 962static int 963uipc_shutdown(struct socket *so) 964{ 965 struct unpcb *unp; 966 967 unp = sotounpcb(so); 968 KASSERT(unp != NULL, ("uipc_shutdown: unp == NULL")); 969 970 UNP_GLOBAL_WLOCK(); 971 UNP_PCB_LOCK(unp); 972 socantsendmore(so); 973 unp_shutdown(unp); 974 UNP_PCB_UNLOCK(unp); 975 UNP_GLOBAL_WUNLOCK(); 976 return (0); 977} 978 979static int 980uipc_sockaddr(struct socket *so, struct sockaddr **nam) 981{ 982 struct unpcb *unp; 983 const struct sockaddr *sa; 984 985 unp = sotounpcb(so); 986 KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL")); 987 988 *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 989 UNP_PCB_LOCK(unp); 990 if (unp->unp_addr != NULL) 991 sa = (struct sockaddr *) unp->unp_addr; 992 else 993 sa = &sun_noname; 994 bcopy(sa, *nam, sa->sa_len); 995 UNP_PCB_UNLOCK(unp); 996 return (0); 997} 998 999struct pr_usrreqs uipc_usrreqs = { 1000 .pru_abort = uipc_abort, 1001 .pru_accept = uipc_accept, 1002 .pru_attach = uipc_attach, 1003 .pru_bind = uipc_bind, 1004 .pru_connect = uipc_connect, 1005 .pru_connect2 = uipc_connect2, 1006 .pru_detach = uipc_detach, 1007 .pru_disconnect = uipc_disconnect, 1008 .pru_listen = uipc_listen, 1009 .pru_peeraddr = uipc_peeraddr, 1010 .pru_rcvd = uipc_rcvd, 1011 .pru_send = uipc_send, 1012 .pru_sense = uipc_sense, 1013 .pru_shutdown = uipc_shutdown, 1014 .pru_sockaddr = uipc_sockaddr, 1015 .pru_close = uipc_close, 1016}; 1017 1018int 1019uipc_ctloutput(struct socket *so, struct sockopt *sopt) 1020{ 1021 struct unpcb *unp; 1022 struct xucred xu; 1023 int error, optval; 1024 1025 if (sopt->sopt_level != 0) 1026 return (EINVAL); 1027 1028 unp = sotounpcb(so); 1029 KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL")); 1030 error = 0; 1031 switch (sopt->sopt_dir) { 1032 case SOPT_GET: 1033 switch (sopt->sopt_name) { 1034 case LOCAL_PEERCRED: 1035 UNP_PCB_LOCK(unp); 1036 if (unp->unp_flags & UNP_HAVEPC) 1037 xu = unp->unp_peercred; 1038 else { 1039 if (so->so_type == SOCK_STREAM) 1040 error = ENOTCONN; 1041 else 1042 error = EINVAL; 1043 } 1044 UNP_PCB_UNLOCK(unp); 1045 if (error == 0) 1046 error = sooptcopyout(sopt, &xu, sizeof(xu)); 1047 break; 1048 1049 case LOCAL_CREDS: 1050 /* Unocked read. */ 1051 optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0; 1052 error = sooptcopyout(sopt, &optval, sizeof(optval)); 1053 break; 1054 1055 case LOCAL_CONNWAIT: 1056 /* Unocked read. */ 1057 optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0; 1058 error = sooptcopyout(sopt, &optval, sizeof(optval)); 1059 break; 1060 1061 default: 1062 error = EOPNOTSUPP; 1063 break; 1064 } 1065 break; 1066 1067 case SOPT_SET: 1068 switch (sopt->sopt_name) { 1069 case LOCAL_CREDS: 1070 case LOCAL_CONNWAIT: 1071 error = sooptcopyin(sopt, &optval, sizeof(optval), 1072 sizeof(optval)); 1073 if (error) 1074 break; 1075 1076#define OPTSET(bit) do { \ 1077 UNP_PCB_LOCK(unp); \ 1078 if (optval) \ 1079 unp->unp_flags |= bit; \ 1080 else \ 1081 unp->unp_flags &= ~bit; \ 1082 UNP_PCB_UNLOCK(unp); \ 1083} while (0) 1084 1085 switch (sopt->sopt_name) { 1086 case LOCAL_CREDS: 1087 OPTSET(UNP_WANTCRED); 1088 break; 1089 1090 case LOCAL_CONNWAIT: 1091 OPTSET(UNP_CONNWAIT); 1092 break; 1093 1094 default: 1095 break; 1096 } 1097 break; 1098#undef OPTSET 1099 default: 1100 error = ENOPROTOOPT; 1101 break; 1102 } 1103 break; 1104 1105 default: 1106 error = EOPNOTSUPP; 1107 break; 1108 } 1109 return (error); 1110} 1111 1112static int 1113unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1114{ 1115 struct sockaddr_un *soun = (struct sockaddr_un *)nam; 1116 struct vnode *vp; 1117 struct socket *so2, *so3; 1118 struct unpcb *unp, *unp2, *unp3; 1119 int error, len, vfslocked; 1120 struct nameidata nd; 1121 char buf[SOCK_MAXADDRLEN]; 1122 struct sockaddr *sa; 1123 1124 UNP_GLOBAL_WLOCK_ASSERT(); 1125 UNP_GLOBAL_WUNLOCK(); 1126 1127 unp = sotounpcb(so); 1128 KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 1129 1130 len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 1131 if (len <= 0) 1132 return (EINVAL); 1133 strlcpy(buf, soun->sun_path, len + 1); 1134 1135 UNP_PCB_LOCK(unp); 1136 if (unp->unp_flags & UNP_CONNECTING) { 1137 UNP_PCB_UNLOCK(unp); 1138 return (EALREADY); 1139 } 1140 unp->unp_flags |= UNP_CONNECTING; 1141 UNP_PCB_UNLOCK(unp); 1142 1143 sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 1144 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, 1145 td); 1146 error = namei(&nd); 1147 if (error) 1148 vp = NULL; 1149 else 1150 vp = nd.ni_vp; 1151 ASSERT_VOP_LOCKED(vp, "unp_connect"); 1152 vfslocked = NDHASGIANT(&nd); 1153 NDFREE(&nd, NDF_ONLY_PNBUF); 1154 if (error) 1155 goto bad; 1156 1157 if (vp->v_type != VSOCK) { 1158 error = ENOTSOCK; 1159 goto bad; 1160 } 1161#ifdef MAC 1162 error = mac_check_vnode_open(td->td_ucred, vp, VWRITE | VREAD); 1163 if (error) 1164 goto bad; 1165#endif 1166 error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 1167 if (error) 1168 goto bad; 1169 VFS_UNLOCK_GIANT(vfslocked); 1170 1171 unp = sotounpcb(so); 1172 KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 1173 1174 /* 1175 * Lock global lock for two reasons: make sure v_socket is stable, 1176 * and to protect simultaneous locking of multiple pcbs. 1177 */ 1178 UNP_GLOBAL_WLOCK(); 1179 so2 = vp->v_socket; 1180 if (so2 == NULL) { 1181 error = ECONNREFUSED; 1182 goto bad2; 1183 } 1184 if (so->so_type != so2->so_type) { 1185 error = EPROTOTYPE; 1186 goto bad2; 1187 } 1188 if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 1189 if (so2->so_options & SO_ACCEPTCONN) { 1190 /* 1191 * We can't drop the global lock here or 'so2' may 1192 * become invalid. As a result, we need to handle 1193 * possibly lock recursion in uipc_attach. 1194 */ 1195 so3 = sonewconn(so2, 0); 1196 } else 1197 so3 = NULL; 1198 if (so3 == NULL) { 1199 error = ECONNREFUSED; 1200 goto bad2; 1201 } 1202 unp = sotounpcb(so); 1203 unp2 = sotounpcb(so2); 1204 unp3 = sotounpcb(so3); 1205 UNP_PCB_LOCK(unp); 1206 UNP_PCB_LOCK(unp2); 1207 UNP_PCB_LOCK(unp3); 1208 if (unp2->unp_addr != NULL) { 1209 bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 1210 unp3->unp_addr = (struct sockaddr_un *) sa; 1211 sa = NULL; 1212 } 1213 /* 1214 * unp_peercred management: 1215 * 1216 * The connecter's (client's) credentials are copied from its 1217 * process structure at the time of connect() (which is now). 1218 */ 1219 cru2x(td->td_ucred, &unp3->unp_peercred); 1220 unp3->unp_flags |= UNP_HAVEPC; 1221 /* 1222 * The receiver's (server's) credentials are copied from the 1223 * unp_peercred member of socket on which the former called 1224 * listen(); uipc_listen() cached that process's credentials 1225 * at that time so we can use them now. 1226 */ 1227 KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 1228 ("unp_connect: listener without cached peercred")); 1229 memcpy(&unp->unp_peercred, &unp2->unp_peercred, 1230 sizeof(unp->unp_peercred)); 1231 unp->unp_flags |= UNP_HAVEPC; 1232 if (unp2->unp_flags & UNP_WANTCRED) 1233 unp3->unp_flags |= UNP_WANTCRED; 1234 UNP_PCB_UNLOCK(unp3); 1235 UNP_PCB_UNLOCK(unp2); 1236 UNP_PCB_UNLOCK(unp); 1237#ifdef MAC 1238 SOCK_LOCK(so); 1239 mac_set_socket_peer_from_socket(so, so3); 1240 mac_set_socket_peer_from_socket(so3, so); 1241 SOCK_UNLOCK(so); 1242#endif 1243 1244 so2 = so3; 1245 } 1246 unp = sotounpcb(so); 1247 KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 1248 unp2 = sotounpcb(so2); 1249 KASSERT(unp2 != NULL, ("unp_connect: unp2 == NULL")); 1250 UNP_PCB_LOCK(unp); 1251 UNP_PCB_LOCK(unp2); 1252 error = unp_connect2(so, so2, PRU_CONNECT); 1253 UNP_PCB_UNLOCK(unp2); 1254 UNP_PCB_UNLOCK(unp); 1255bad2: 1256 UNP_GLOBAL_WUNLOCK(); 1257 if (vfslocked) 1258 /* 1259 * Giant has been previously acquired. This means filesystem 1260 * isn't MPSAFE. Do it once again. 1261 */ 1262 mtx_lock(&Giant); 1263bad: 1264 if (vp != NULL) 1265 vput(vp); 1266 VFS_UNLOCK_GIANT(vfslocked); 1267 free(sa, M_SONAME); 1268 UNP_GLOBAL_WLOCK(); 1269 UNP_PCB_LOCK(unp); 1270 unp->unp_flags &= ~UNP_CONNECTING; 1271 UNP_PCB_UNLOCK(unp); 1272 return (error); 1273} 1274 1275static int 1276unp_connect2(struct socket *so, struct socket *so2, int req) 1277{ 1278 struct unpcb *unp; 1279 struct unpcb *unp2; 1280 1281 unp = sotounpcb(so); 1282 KASSERT(unp != NULL, ("unp_connect2: unp == NULL")); 1283 unp2 = sotounpcb(so2); 1284 KASSERT(unp2 != NULL, ("unp_connect2: unp2 == NULL")); 1285 1286 UNP_GLOBAL_WLOCK_ASSERT(); 1287 UNP_PCB_LOCK_ASSERT(unp); 1288 UNP_PCB_LOCK_ASSERT(unp2); 1289 1290 if (so2->so_type != so->so_type) 1291 return (EPROTOTYPE); 1292 unp->unp_conn = unp2; 1293 1294 switch (so->so_type) { 1295 case SOCK_DGRAM: 1296 LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 1297 soisconnected(so); 1298 break; 1299 1300 case SOCK_STREAM: 1301 unp2->unp_conn = unp; 1302 if (req == PRU_CONNECT && 1303 ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT)) 1304 soisconnecting(so); 1305 else 1306 soisconnected(so); 1307 soisconnected(so2); 1308 break; 1309 1310 default: 1311 panic("unp_connect2"); 1312 } 1313 return (0); 1314} 1315 1316static void 1317unp_disconnect(struct unpcb *unp, struct unpcb *unp2) 1318{ 1319 struct socket *so; 1320 1321 KASSERT(unp2 != NULL, ("unp_disconnect: unp2 == NULL")); 1322 1323 UNP_GLOBAL_WLOCK_ASSERT(); 1324 UNP_PCB_LOCK_ASSERT(unp); 1325 UNP_PCB_LOCK_ASSERT(unp2); 1326 1327 unp->unp_conn = NULL; 1328 switch (unp->unp_socket->so_type) { 1329 case SOCK_DGRAM: 1330 LIST_REMOVE(unp, unp_reflink); 1331 so = unp->unp_socket; 1332 SOCK_LOCK(so); 1333 so->so_state &= ~SS_ISCONNECTED; 1334 SOCK_UNLOCK(so); 1335 break; 1336 1337 case SOCK_STREAM: 1338 soisdisconnected(unp->unp_socket); 1339 unp2->unp_conn = NULL; 1340 soisdisconnected(unp2->unp_socket); 1341 break; 1342 } 1343} 1344 1345/* 1346 * unp_pcblist() walks the global list of struct unpcb's to generate a 1347 * pointer list, bumping the refcount on each unpcb. It then copies them out 1348 * sequentially, validating the generation number on each to see if it has 1349 * been detached. All of this is necessary because copyout() may sleep on 1350 * disk I/O. 1351 */ 1352static int 1353unp_pcblist(SYSCTL_HANDLER_ARGS) 1354{ 1355 int error, i, n; 1356 int freeunp; 1357 struct unpcb *unp, **unp_list; 1358 unp_gen_t gencnt; 1359 struct xunpgen *xug; 1360 struct unp_head *head; 1361 struct xunpcb *xu; 1362 1363 head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 1364 1365 /* 1366 * The process of preparing the PCB list is too time-consuming and 1367 * resource-intensive to repeat twice on every request. 1368 */ 1369 if (req->oldptr == NULL) { 1370 n = unp_count; 1371 req->oldidx = 2 * (sizeof *xug) 1372 + (n + n/8) * sizeof(struct xunpcb); 1373 return (0); 1374 } 1375 1376 if (req->newptr != NULL) 1377 return (EPERM); 1378 1379 /* 1380 * OK, now we're committed to doing something. 1381 */ 1382 xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 1383 UNP_GLOBAL_RLOCK(); 1384 gencnt = unp_gencnt; 1385 n = unp_count; 1386 UNP_GLOBAL_RUNLOCK(); 1387 1388 xug->xug_len = sizeof *xug; 1389 xug->xug_count = n; 1390 xug->xug_gen = gencnt; 1391 xug->xug_sogen = so_gencnt; 1392 error = SYSCTL_OUT(req, xug, sizeof *xug); 1393 if (error) { 1394 free(xug, M_TEMP); 1395 return (error); 1396 } 1397 1398 unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 1399 1400 UNP_GLOBAL_RLOCK(); 1401 for (unp = LIST_FIRST(head), i = 0; unp && i < n; 1402 unp = LIST_NEXT(unp, unp_link)) { 1403 UNP_PCB_LOCK(unp); 1404 if (unp->unp_gencnt <= gencnt) { 1405 if (cr_cansee(req->td->td_ucred, 1406 unp->unp_socket->so_cred)) { 1407 UNP_PCB_UNLOCK(unp); 1408 continue; 1409 } 1410 unp_list[i++] = unp; 1411 unp->unp_refcount++; 1412 } 1413 UNP_PCB_UNLOCK(unp); 1414 } 1415 UNP_GLOBAL_RUNLOCK(); 1416 n = i; /* In case we lost some during malloc. */ 1417 1418 error = 0; 1419 xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO); 1420 for (i = 0; i < n; i++) { 1421 unp = unp_list[i]; 1422 UNP_PCB_LOCK(unp); 1423 unp->unp_refcount--; 1424 if (unp->unp_refcount != 0 && unp->unp_gencnt <= gencnt) { 1425 xu->xu_len = sizeof *xu; 1426 xu->xu_unpp = unp; 1427 /* 1428 * XXX - need more locking here to protect against 1429 * connect/disconnect races for SMP. 1430 */ 1431 if (unp->unp_addr != NULL) 1432 bcopy(unp->unp_addr, &xu->xu_addr, 1433 unp->unp_addr->sun_len); 1434 if (unp->unp_conn != NULL && 1435 unp->unp_conn->unp_addr != NULL) 1436 bcopy(unp->unp_conn->unp_addr, 1437 &xu->xu_caddr, 1438 unp->unp_conn->unp_addr->sun_len); 1439 bcopy(unp, &xu->xu_unp, sizeof *unp); 1440 sotoxsocket(unp->unp_socket, &xu->xu_socket); 1441 UNP_PCB_UNLOCK(unp); 1442 error = SYSCTL_OUT(req, xu, sizeof *xu); 1443 } else { 1444 freeunp = (unp->unp_refcount == 0); 1445 UNP_PCB_UNLOCK(unp); 1446 if (freeunp) { 1447 UNP_PCB_LOCK_DESTROY(unp); 1448 uma_zfree(unp_zone, unp); 1449 } 1450 } 1451 } 1452 free(xu, M_TEMP); 1453 if (!error) { 1454 /* 1455 * Give the user an updated idea of our state. If the 1456 * generation differs from what we told her before, she knows 1457 * that something happened while we were processing this 1458 * request, and it might be necessary to retry. 1459 */ 1460 xug->xug_gen = unp_gencnt; 1461 xug->xug_sogen = so_gencnt; 1462 xug->xug_count = unp_count; 1463 error = SYSCTL_OUT(req, xug, sizeof *xug); 1464 } 1465 free(unp_list, M_TEMP); 1466 free(xug, M_TEMP); 1467 return (error); 1468} 1469 1470SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 1471 (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 1472 "List of active local datagram sockets"); 1473SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 1474 (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 1475 "List of active local stream sockets"); 1476 1477static void 1478unp_shutdown(struct unpcb *unp) 1479{ 1480 struct unpcb *unp2; 1481 struct socket *so; 1482 1483 UNP_GLOBAL_WLOCK_ASSERT(); 1484 UNP_PCB_LOCK_ASSERT(unp); 1485 1486 unp2 = unp->unp_conn; 1487 if (unp->unp_socket->so_type == SOCK_STREAM && unp2 != NULL) { 1488 so = unp2->unp_socket; 1489 if (so != NULL) 1490 socantrcvmore(so); 1491 } 1492} 1493 1494static void 1495unp_drop(struct unpcb *unp, int errno) 1496{ 1497 struct socket *so = unp->unp_socket; 1498 struct unpcb *unp2; 1499 1500 UNP_GLOBAL_WLOCK_ASSERT(); 1501 UNP_PCB_LOCK_ASSERT(unp); 1502 1503 so->so_error = errno; 1504 unp2 = unp->unp_conn; 1505 if (unp2 == NULL) 1506 return; 1507 1508 UNP_PCB_LOCK(unp2); 1509 unp_disconnect(unp, unp2); 1510 UNP_PCB_UNLOCK(unp2); 1511} 1512 1513static void 1514unp_freerights(struct file **rp, int fdcount) 1515{ 1516 int i; 1517 struct file *fp; 1518 1519 for (i = 0; i < fdcount; i++) { 1520 /* 1521 * Zero the pointer before calling unp_discard since it may 1522 * end up in unp_gc().. 1523 * 1524 * XXXRW: This is less true than it used to be. 1525 */ 1526 fp = *rp; 1527 *rp++ = NULL; 1528 unp_discard(fp); 1529 } 1530} 1531 1532int 1533unp_externalize(struct mbuf *control, struct mbuf **controlp) 1534{ 1535 struct thread *td = curthread; /* XXX */ 1536 struct cmsghdr *cm = mtod(control, struct cmsghdr *); 1537 int i; 1538 int *fdp; 1539 struct file **rp; 1540 struct file *fp; 1541 void *data; 1542 socklen_t clen = control->m_len, datalen; 1543 int error, newfds; 1544 int f; 1545 u_int newlen; 1546 1547 UNP_GLOBAL_UNLOCK_ASSERT(); 1548 1549 error = 0; 1550 if (controlp != NULL) /* controlp == NULL => free control messages */ 1551 *controlp = NULL; 1552 1553 while (cm != NULL) { 1554 if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 1555 error = EINVAL; 1556 break; 1557 } 1558 1559 data = CMSG_DATA(cm); 1560 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 1561 1562 if (cm->cmsg_level == SOL_SOCKET 1563 && cm->cmsg_type == SCM_RIGHTS) { 1564 newfds = datalen / sizeof(struct file *); 1565 rp = data; 1566 1567 /* If we're not outputting the descriptors free them. */ 1568 if (error || controlp == NULL) { 1569 unp_freerights(rp, newfds); 1570 goto next; 1571 } 1572 FILEDESC_XLOCK(td->td_proc->p_fd); 1573 /* if the new FD's will not fit free them. */ 1574 if (!fdavail(td, newfds)) { 1575 FILEDESC_XUNLOCK(td->td_proc->p_fd); 1576 error = EMSGSIZE; 1577 unp_freerights(rp, newfds); 1578 goto next; 1579 } 1580 /* 1581 * Now change each pointer to an fd in the global 1582 * table to an integer that is the index to the local 1583 * fd table entry that we set up to point to the 1584 * global one we are transferring. 1585 */ 1586 newlen = newfds * sizeof(int); 1587 *controlp = sbcreatecontrol(NULL, newlen, 1588 SCM_RIGHTS, SOL_SOCKET); 1589 if (*controlp == NULL) { 1590 FILEDESC_XUNLOCK(td->td_proc->p_fd); 1591 error = E2BIG; 1592 unp_freerights(rp, newfds); 1593 goto next; 1594 } 1595 1596 fdp = (int *) 1597 CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1598 for (i = 0; i < newfds; i++) { 1599 if (fdalloc(td, 0, &f)) 1600 panic("unp_externalize fdalloc failed"); 1601 fp = *rp++; 1602 td->td_proc->p_fd->fd_ofiles[f] = fp; 1603 FILE_LOCK(fp); 1604 fp->f_msgcount--; 1605 FILE_UNLOCK(fp); 1606 unp_rights--; 1607 *fdp++ = f; 1608 } 1609 FILEDESC_XUNLOCK(td->td_proc->p_fd); 1610 } else { 1611 /* We can just copy anything else across. */ 1612 if (error || controlp == NULL) 1613 goto next; 1614 *controlp = sbcreatecontrol(NULL, datalen, 1615 cm->cmsg_type, cm->cmsg_level); 1616 if (*controlp == NULL) { 1617 error = ENOBUFS; 1618 goto next; 1619 } 1620 bcopy(data, 1621 CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 1622 datalen); 1623 } 1624 1625 controlp = &(*controlp)->m_next; 1626 1627next: 1628 if (CMSG_SPACE(datalen) < clen) { 1629 clen -= CMSG_SPACE(datalen); 1630 cm = (struct cmsghdr *) 1631 ((caddr_t)cm + CMSG_SPACE(datalen)); 1632 } else { 1633 clen = 0; 1634 cm = NULL; 1635 } 1636 } 1637 1638 m_freem(control); 1639 1640 return (error); 1641} 1642 1643static void 1644unp_zone_change(void *tag) 1645{ 1646 1647 uma_zone_set_max(unp_zone, maxsockets); 1648} 1649 1650void 1651unp_init(void) 1652{ 1653 1654 unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 1655 NULL, NULL, UMA_ALIGN_PTR, 0); 1656 if (unp_zone == NULL) 1657 panic("unp_init"); 1658 uma_zone_set_max(unp_zone, maxsockets); 1659 EVENTHANDLER_REGISTER(maxsockets_change, unp_zone_change, 1660 NULL, EVENTHANDLER_PRI_ANY); 1661 LIST_INIT(&unp_dhead); 1662 LIST_INIT(&unp_shead); 1663 TASK_INIT(&unp_gc_task, 0, unp_gc, NULL); 1664 UNP_GLOBAL_LOCK_INIT(); 1665} 1666 1667static int 1668unp_internalize(struct mbuf **controlp, struct thread *td) 1669{ 1670 struct mbuf *control = *controlp; 1671 struct proc *p = td->td_proc; 1672 struct filedesc *fdescp = p->p_fd; 1673 struct cmsghdr *cm = mtod(control, struct cmsghdr *); 1674 struct cmsgcred *cmcred; 1675 struct file **rp; 1676 struct file *fp; 1677 struct timeval *tv; 1678 int i, fd, *fdp; 1679 void *data; 1680 socklen_t clen = control->m_len, datalen; 1681 int error, oldfds; 1682 u_int newlen; 1683 1684 UNP_GLOBAL_UNLOCK_ASSERT(); 1685 1686 error = 0; 1687 *controlp = NULL; 1688 1689 while (cm != NULL) { 1690 if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 1691 || cm->cmsg_len > clen) { 1692 error = EINVAL; 1693 goto out; 1694 } 1695 1696 data = CMSG_DATA(cm); 1697 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 1698 1699 switch (cm->cmsg_type) { 1700 /* 1701 * Fill in credential information. 1702 */ 1703 case SCM_CREDS: 1704 *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 1705 SCM_CREDS, SOL_SOCKET); 1706 if (*controlp == NULL) { 1707 error = ENOBUFS; 1708 goto out; 1709 } 1710 1711 cmcred = (struct cmsgcred *) 1712 CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1713 cmcred->cmcred_pid = p->p_pid; 1714 cmcred->cmcred_uid = td->td_ucred->cr_ruid; 1715 cmcred->cmcred_gid = td->td_ucred->cr_rgid; 1716 cmcred->cmcred_euid = td->td_ucred->cr_uid; 1717 cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 1718 CMGROUP_MAX); 1719 for (i = 0; i < cmcred->cmcred_ngroups; i++) 1720 cmcred->cmcred_groups[i] = 1721 td->td_ucred->cr_groups[i]; 1722 break; 1723 1724 case SCM_RIGHTS: 1725 oldfds = datalen / sizeof (int); 1726 /* 1727 * Check that all the FDs passed in refer to legal 1728 * files. If not, reject the entire operation. 1729 */ 1730 fdp = data; 1731 FILEDESC_SLOCK(fdescp); 1732 for (i = 0; i < oldfds; i++) { 1733 fd = *fdp++; 1734 if ((unsigned)fd >= fdescp->fd_nfiles || 1735 fdescp->fd_ofiles[fd] == NULL) { 1736 FILEDESC_SUNLOCK(fdescp); 1737 error = EBADF; 1738 goto out; 1739 } 1740 fp = fdescp->fd_ofiles[fd]; 1741 if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 1742 FILEDESC_SUNLOCK(fdescp); 1743 error = EOPNOTSUPP; 1744 goto out; 1745 } 1746 1747 } 1748 1749 /* 1750 * Now replace the integer FDs with pointers to 1751 * the associated global file table entry.. 1752 */ 1753 newlen = oldfds * sizeof(struct file *); 1754 *controlp = sbcreatecontrol(NULL, newlen, 1755 SCM_RIGHTS, SOL_SOCKET); 1756 if (*controlp == NULL) { 1757 FILEDESC_SUNLOCK(fdescp); 1758 error = E2BIG; 1759 goto out; 1760 } 1761 1762 fdp = data; 1763 rp = (struct file **) 1764 CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1765 for (i = 0; i < oldfds; i++) { 1766 fp = fdescp->fd_ofiles[*fdp++]; 1767 *rp++ = fp; 1768 FILE_LOCK(fp); 1769 fp->f_count++; 1770 fp->f_msgcount++; 1771 FILE_UNLOCK(fp); 1772 unp_rights++; 1773 } 1774 FILEDESC_SUNLOCK(fdescp); 1775 break; 1776 1777 case SCM_TIMESTAMP: 1778 *controlp = sbcreatecontrol(NULL, sizeof(*tv), 1779 SCM_TIMESTAMP, SOL_SOCKET); 1780 if (*controlp == NULL) { 1781 error = ENOBUFS; 1782 goto out; 1783 } 1784 tv = (struct timeval *) 1785 CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1786 microtime(tv); 1787 break; 1788 1789 default: 1790 error = EINVAL; 1791 goto out; 1792 } 1793 1794 controlp = &(*controlp)->m_next; 1795 1796 if (CMSG_SPACE(datalen) < clen) { 1797 clen -= CMSG_SPACE(datalen); 1798 cm = (struct cmsghdr *) 1799 ((caddr_t)cm + CMSG_SPACE(datalen)); 1800 } else { 1801 clen = 0; 1802 cm = NULL; 1803 } 1804 } 1805 1806out: 1807 m_freem(control); 1808 1809 return (error); 1810} 1811 1812static struct mbuf * 1813unp_addsockcred(struct thread *td, struct mbuf *control) 1814{ 1815 struct mbuf *m, *n, *n_prev; 1816 struct sockcred *sc; 1817 const struct cmsghdr *cm; 1818 int ngroups; 1819 int i; 1820 1821 ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX); 1822 1823 m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET); 1824 if (m == NULL) 1825 return (control); 1826 1827 sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *)); 1828 sc->sc_uid = td->td_ucred->cr_ruid; 1829 sc->sc_euid = td->td_ucred->cr_uid; 1830 sc->sc_gid = td->td_ucred->cr_rgid; 1831 sc->sc_egid = td->td_ucred->cr_gid; 1832 sc->sc_ngroups = ngroups; 1833 for (i = 0; i < sc->sc_ngroups; i++) 1834 sc->sc_groups[i] = td->td_ucred->cr_groups[i]; 1835 1836 /* 1837 * Unlink SCM_CREDS control messages (struct cmsgcred), since just 1838 * created SCM_CREDS control message (struct sockcred) has another 1839 * format. 1840 */ 1841 if (control != NULL) 1842 for (n = control, n_prev = NULL; n != NULL;) { 1843 cm = mtod(n, struct cmsghdr *); 1844 if (cm->cmsg_level == SOL_SOCKET && 1845 cm->cmsg_type == SCM_CREDS) { 1846 if (n_prev == NULL) 1847 control = n->m_next; 1848 else 1849 n_prev->m_next = n->m_next; 1850 n = m_free(n); 1851 } else { 1852 n_prev = n; 1853 n = n->m_next; 1854 } 1855 } 1856 1857 /* Prepend it to the head. */ 1858 m->m_next = control; 1859 1860 return (m); 1861} 1862 1863/* 1864 * unp_defer indicates whether additional work has been defered for a future 1865 * pass through unp_gc(). It is thread local and does not require explicit 1866 * synchronization. 1867 */ 1868static int unp_defer; 1869 1870static int unp_taskcount; 1871SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0, ""); 1872 1873static int unp_recycled; 1874SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0, ""); 1875 1876static void 1877unp_gc(__unused void *arg, int pending) 1878{ 1879 struct file *fp, *nextfp; 1880 struct socket *so; 1881 struct file **extra_ref, **fpp; 1882 int nunref, i; 1883 int nfiles_snap; 1884 int nfiles_slack = 20; 1885 1886 unp_taskcount++; 1887 unp_defer = 0; 1888 /* 1889 * Before going through all this, set all FDs to be NOT deferred and 1890 * NOT externally accessible. 1891 */ 1892 sx_slock(&filelist_lock); 1893 LIST_FOREACH(fp, &filehead, f_list) 1894 fp->f_gcflag &= ~(FMARK|FDEFER); 1895 do { 1896 KASSERT(unp_defer >= 0, ("unp_gc: unp_defer %d", unp_defer)); 1897 LIST_FOREACH(fp, &filehead, f_list) { 1898 FILE_LOCK(fp); 1899 /* 1900 * If the file is not open, skip it -- could be a 1901 * file in the process of being opened, or in the 1902 * process of being closed. If the file is 1903 * "closing", it may have been marked for deferred 1904 * consideration. Clear the flag now if so. 1905 */ 1906 if (fp->f_count == 0) { 1907 if (fp->f_gcflag & FDEFER) 1908 unp_defer--; 1909 fp->f_gcflag &= ~(FMARK|FDEFER); 1910 FILE_UNLOCK(fp); 1911 continue; 1912 } 1913 /* 1914 * If we already marked it as 'defer' in a 1915 * previous pass, then try to process it this 1916 * time and un-mark it. 1917 */ 1918 if (fp->f_gcflag & FDEFER) { 1919 fp->f_gcflag &= ~FDEFER; 1920 unp_defer--; 1921 } else { 1922 /* 1923 * If it's not deferred, then check if it's 1924 * already marked.. if so skip it 1925 */ 1926 if (fp->f_gcflag & FMARK) { 1927 FILE_UNLOCK(fp); 1928 continue; 1929 } 1930 /* 1931 * If all references are from messages in 1932 * transit, then skip it. it's not externally 1933 * accessible. 1934 */ 1935 if (fp->f_count == fp->f_msgcount) { 1936 FILE_UNLOCK(fp); 1937 continue; 1938 } 1939 /* 1940 * If it got this far then it must be 1941 * externally accessible. 1942 */ 1943 fp->f_gcflag |= FMARK; 1944 } 1945 /* 1946 * Either it was deferred, or it is externally 1947 * accessible and not already marked so. Now check 1948 * if it is possibly one of OUR sockets. 1949 */ 1950 if (fp->f_type != DTYPE_SOCKET || 1951 (so = fp->f_data) == NULL) { 1952 FILE_UNLOCK(fp); 1953 continue; 1954 } 1955 if (so->so_proto->pr_domain != &localdomain || 1956 (so->so_proto->pr_flags & PR_RIGHTS) == 0) { 1957 FILE_UNLOCK(fp); 1958 continue; 1959 } 1960 1961 /* 1962 * Tell any other threads that do a subsequent 1963 * fdrop() that we are scanning the message 1964 * buffers. 1965 */ 1966 fp->f_gcflag |= FWAIT; 1967 FILE_UNLOCK(fp); 1968 1969 /* 1970 * So, Ok, it's one of our sockets and it IS 1971 * externally accessible (or was deferred). Now we 1972 * look to see if we hold any file descriptors in its 1973 * message buffers. Follow those links and mark them 1974 * as accessible too. 1975 */ 1976 SOCKBUF_LOCK(&so->so_rcv); 1977 unp_scan(so->so_rcv.sb_mb, unp_mark); 1978 SOCKBUF_UNLOCK(&so->so_rcv); 1979 1980 /* 1981 * Wake up any threads waiting in fdrop(). 1982 */ 1983 FILE_LOCK(fp); 1984 fp->f_gcflag &= ~FWAIT; 1985 wakeup(&fp->f_gcflag); 1986 FILE_UNLOCK(fp); 1987 } 1988 } while (unp_defer); 1989 sx_sunlock(&filelist_lock); 1990 /* 1991 * XXXRW: The following comments need updating for a post-SMPng and 1992 * deferred unp_gc() world, but are still generally accurate. 1993 * 1994 * We grab an extra reference to each of the file table entries that 1995 * are not otherwise accessible and then free the rights that are 1996 * stored in messages on them. 1997 * 1998 * The bug in the orginal code is a little tricky, so I'll describe 1999 * what's wrong with it here. 2000 * 2001 * It is incorrect to simply unp_discard each entry for f_msgcount 2002 * times -- consider the case of sockets A and B that contain 2003 * references to each other. On a last close of some other socket, 2004 * we trigger a gc since the number of outstanding rights (unp_rights) 2005 * is non-zero. If during the sweep phase the gc code unp_discards, 2006 * we end up doing a (full) closef on the descriptor. A closef on A 2007 * results in the following chain. Closef calls soo_close, which 2008 * calls soclose. Soclose calls first (through the switch 2009 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 2010 * returns because the previous instance had set unp_gcing, and we 2011 * return all the way back to soclose, which marks the socket with 2012 * SS_NOFDREF, and then calls sofree. Sofree calls sorflush to free 2013 * up the rights that are queued in messages on the socket A, i.e., 2014 * the reference on B. The sorflush calls via the dom_dispose switch 2015 * unp_dispose, which unp_scans with unp_discard. This second 2016 * instance of unp_discard just calls closef on B. 2017 * 2018 * Well, a similar chain occurs on B, resulting in a sorflush on B, 2019 * which results in another closef on A. Unfortunately, A is already 2020 * being closed, and the descriptor has already been marked with 2021 * SS_NOFDREF, and soclose panics at this point. 2022 * 2023 * Here, we first take an extra reference to each inaccessible 2024 * descriptor. Then, we call sorflush ourself, since we know it is a 2025 * Unix domain socket anyhow. After we destroy all the rights 2026 * carried in messages, we do a last closef to get rid of our extra 2027 * reference. This is the last close, and the unp_detach etc will 2028 * shut down the socket. 2029 * 2030 * 91/09/19, bsy@cs.cmu.edu 2031 */ 2032again: 2033 nfiles_snap = openfiles + nfiles_slack; /* some slack */ 2034 extra_ref = malloc(nfiles_snap * sizeof(struct file *), M_TEMP, 2035 M_WAITOK); 2036 sx_slock(&filelist_lock); 2037 if (nfiles_snap < openfiles) { 2038 sx_sunlock(&filelist_lock); 2039 free(extra_ref, M_TEMP); 2040 nfiles_slack += 20; 2041 goto again; 2042 } 2043 for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; 2044 fp != NULL; fp = nextfp) { 2045 nextfp = LIST_NEXT(fp, f_list); 2046 FILE_LOCK(fp); 2047 /* 2048 * If it's not open, skip it 2049 */ 2050 if (fp->f_count == 0) { 2051 FILE_UNLOCK(fp); 2052 continue; 2053 } 2054 /* 2055 * If all refs are from msgs, and it's not marked accessible 2056 * then it must be referenced from some unreachable cycle of 2057 * (shut-down) FDs, so include it in our list of FDs to 2058 * remove. 2059 */ 2060 if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) { 2061 *fpp++ = fp; 2062 nunref++; 2063 fp->f_count++; 2064 } 2065 FILE_UNLOCK(fp); 2066 } 2067 sx_sunlock(&filelist_lock); 2068 /* 2069 * For each FD on our hit list, do the following two things: 2070 */ 2071 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 2072 struct file *tfp = *fpp; 2073 FILE_LOCK(tfp); 2074 if (tfp->f_type == DTYPE_SOCKET && 2075 tfp->f_data != NULL) { 2076 FILE_UNLOCK(tfp); 2077 sorflush(tfp->f_data); 2078 } else { 2079 FILE_UNLOCK(tfp); 2080 } 2081 } 2082 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 2083 closef(*fpp, (struct thread *) NULL); 2084 unp_recycled++; 2085 } 2086 free(extra_ref, M_TEMP); 2087} 2088 2089void 2090unp_dispose(struct mbuf *m) 2091{ 2092 2093 if (m) 2094 unp_scan(m, unp_discard); 2095} 2096 2097static void 2098unp_scan(struct mbuf *m0, void (*op)(struct file *)) 2099{ 2100 struct mbuf *m; 2101 struct file **rp; 2102 struct cmsghdr *cm; 2103 void *data; 2104 int i; 2105 socklen_t clen, datalen; 2106 int qfds; 2107 2108 while (m0 != NULL) { 2109 for (m = m0; m; m = m->m_next) { 2110 if (m->m_type != MT_CONTROL) 2111 continue; 2112 2113 cm = mtod(m, struct cmsghdr *); 2114 clen = m->m_len; 2115 2116 while (cm != NULL) { 2117 if (sizeof(*cm) > clen || cm->cmsg_len > clen) 2118 break; 2119 2120 data = CMSG_DATA(cm); 2121 datalen = (caddr_t)cm + cm->cmsg_len 2122 - (caddr_t)data; 2123 2124 if (cm->cmsg_level == SOL_SOCKET && 2125 cm->cmsg_type == SCM_RIGHTS) { 2126 qfds = datalen / sizeof (struct file *); 2127 rp = data; 2128 for (i = 0; i < qfds; i++) 2129 (*op)(*rp++); 2130 } 2131 2132 if (CMSG_SPACE(datalen) < clen) { 2133 clen -= CMSG_SPACE(datalen); 2134 cm = (struct cmsghdr *) 2135 ((caddr_t)cm + CMSG_SPACE(datalen)); 2136 } else { 2137 clen = 0; 2138 cm = NULL; 2139 } 2140 } 2141 } 2142 m0 = m0->m_act; 2143 } 2144} 2145 2146static void 2147unp_mark(struct file *fp) 2148{ 2149 2150 /* XXXRW: Should probably assert file list lock here. */ 2151 2152 if (fp->f_gcflag & FMARK) 2153 return; 2154 unp_defer++; 2155 fp->f_gcflag |= (FMARK|FDEFER); 2156} 2157 2158static void 2159unp_discard(struct file *fp) 2160{ 2161 2162 UNP_GLOBAL_WLOCK(); 2163 FILE_LOCK(fp); 2164 fp->f_msgcount--; 2165 unp_rights--; 2166 FILE_UNLOCK(fp); 2167 UNP_GLOBAL_WUNLOCK(); 2168 (void) closef(fp, (struct thread *)NULL); 2169} 2170 2171#ifdef DDB 2172static void 2173db_print_indent(int indent) 2174{ 2175 int i; 2176 2177 for (i = 0; i < indent; i++) 2178 db_printf(" "); 2179} 2180 2181static void 2182db_print_unpflags(int unp_flags) 2183{ 2184 int comma; 2185 2186 comma = 0; 2187 if (unp_flags & UNP_HAVEPC) { 2188 db_printf("%sUNP_HAVEPC", comma ? ", " : ""); 2189 comma = 1; 2190 } 2191 if (unp_flags & UNP_HAVEPCCACHED) { 2192 db_printf("%sUNP_HAVEPCCACHED", comma ? ", " : ""); 2193 comma = 1; 2194 } 2195 if (unp_flags & UNP_WANTCRED) { 2196 db_printf("%sUNP_WANTCRED", comma ? ", " : ""); 2197 comma = 1; 2198 } 2199 if (unp_flags & UNP_CONNWAIT) { 2200 db_printf("%sUNP_CONNWAIT", comma ? ", " : ""); 2201 comma = 1; 2202 } 2203 if (unp_flags & UNP_CONNECTING) { 2204 db_printf("%sUNP_CONNECTING", comma ? ", " : ""); 2205 comma = 1; 2206 } 2207 if (unp_flags & UNP_BINDING) { 2208 db_printf("%sUNP_BINDING", comma ? ", " : ""); 2209 comma = 1; 2210 } 2211} 2212 2213static void 2214db_print_xucred(int indent, struct xucred *xu) 2215{ 2216 int comma, i; 2217 2218 db_print_indent(indent); 2219 db_printf("cr_version: %u cr_uid: %u cr_ngroups: %d\n", 2220 xu->cr_version, xu->cr_uid, xu->cr_ngroups); 2221 db_print_indent(indent); 2222 db_printf("cr_groups: "); 2223 comma = 0; 2224 for (i = 0; i < xu->cr_ngroups; i++) { 2225 db_printf("%s%u", comma ? ", " : "", xu->cr_groups[i]); 2226 comma = 1; 2227 } 2228 db_printf("\n"); 2229} 2230 2231static void 2232db_print_unprefs(int indent, struct unp_head *uh) 2233{ 2234 struct unpcb *unp; 2235 int counter; 2236 2237 counter = 0; 2238 LIST_FOREACH(unp, uh, unp_reflink) { 2239 if (counter % 4 == 0) 2240 db_print_indent(indent); 2241 db_printf("%p ", unp); 2242 if (counter % 4 == 3) 2243 db_printf("\n"); 2244 counter++; 2245 } 2246 if (counter != 0 && counter % 4 != 0) 2247 db_printf("\n"); 2248} 2249 2250DB_SHOW_COMMAND(unpcb, db_show_unpcb) 2251{ 2252 struct unpcb *unp; 2253 2254 if (!have_addr) { 2255 db_printf("usage: show unpcb <addr>\n"); 2256 return; 2257 } 2258 unp = (struct unpcb *)addr; 2259 2260 db_printf("unp_socket: %p unp_vnode: %p\n", unp->unp_socket, 2261 unp->unp_vnode); 2262 2263 db_printf("unp_ino: %d unp_conn: %p\n", unp->unp_ino, 2264 unp->unp_conn); 2265 2266 db_printf("unp_refs:\n"); 2267 db_print_unprefs(2, &unp->unp_refs); 2268 2269 /* XXXRW: Would be nice to print the full address, if any. */ 2270 db_printf("unp_addr: %p\n", unp->unp_addr); 2271 2272 db_printf("unp_cc: %d unp_mbcnt: %d unp_gencnt: %llu\n", 2273 unp->unp_cc, unp->unp_mbcnt, 2274 (unsigned long long)unp->unp_gencnt); 2275 2276 db_printf("unp_flags: %x (", unp->unp_flags); 2277 db_print_unpflags(unp->unp_flags); 2278 db_printf(")\n"); 2279 2280 db_printf("unp_peercred:\n"); 2281 db_print_xucred(2, &unp->unp_peercred); 2282 2283 db_printf("unp_refcount: %u\n", unp->unp_refcount); 2284} 2285#endif 2286