19454b2d8SWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1991, 1993 3e1ac28e2SRobert Watson * The Regents of the University of California. 44d4b555eSRobert Watson * Copyright 2004-2006 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 44f23929fbSRobert Watson * buffer of a second socket. The implementation is substantially 45f23929fbSRobert Watson * complicated by the fact that "ancillary data", such as file descriptors or 46ca948c5eSRobert Watson * credentials, may be passed across UNIX domain sockets. The potential for 47ca948c5eSRobert Watson * passing UNIX domain sockets over other UNIX domain sockets requires the 48ca948c5eSRobert Watson * implementation of a simple garbage collector to find and tear down cycles 49ca948c5eSRobert Watson * of disconnected sockets. 50f23929fbSRobert Watson */ 51f23929fbSRobert Watson 52677b542eSDavid E. O'Brien #include <sys/cdefs.h> 53677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 54677b542eSDavid E. O'Brien 55335654d7SRobert Watson #include "opt_mac.h" 56335654d7SRobert Watson 57df8bae1dSRodney W. Grimes #include <sys/param.h> 58fb919e4dSMark Murray #include <sys/domain.h> 59960ed29cSSeigo Tanimura #include <sys/fcntl.h> 60d826c479SBruce Evans #include <sys/malloc.h> /* XXX must be before <sys/file.h> */ 614f590175SPaul Saab #include <sys/eventhandler.h> 62639acc13SGarrett Wollman #include <sys/file.h> 63960ed29cSSeigo Tanimura #include <sys/filedesc.h> 64960ed29cSSeigo Tanimura #include <sys/jail.h> 65960ed29cSSeigo Tanimura #include <sys/kernel.h> 66960ed29cSSeigo Tanimura #include <sys/lock.h> 67639acc13SGarrett Wollman #include <sys/mbuf.h> 68033eb86eSJeff Roberson #include <sys/mount.h> 69960ed29cSSeigo Tanimura #include <sys/mutex.h> 70639acc13SGarrett Wollman #include <sys/namei.h> 71639acc13SGarrett Wollman #include <sys/proc.h> 72df8bae1dSRodney W. Grimes #include <sys/protosw.h> 73960ed29cSSeigo Tanimura #include <sys/resourcevar.h> 74df8bae1dSRodney W. Grimes #include <sys/socket.h> 75df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 76960ed29cSSeigo Tanimura #include <sys/signalvar.h> 77df8bae1dSRodney W. Grimes #include <sys/stat.h> 78960ed29cSSeigo Tanimura #include <sys/sx.h> 79639acc13SGarrett Wollman #include <sys/sysctl.h> 80960ed29cSSeigo Tanimura #include <sys/systm.h> 81a0ec558aSRobert Watson #include <sys/taskqueue.h> 82639acc13SGarrett Wollman #include <sys/un.h> 8398271db4SGarrett Wollman #include <sys/unpcb.h> 84639acc13SGarrett Wollman #include <sys/vnode.h> 85df8bae1dSRodney W. Grimes 86aed55708SRobert Watson #include <security/mac/mac_framework.h> 87aed55708SRobert Watson 889e9d298aSJeff Roberson #include <vm/uma.h> 8998271db4SGarrett Wollman 909e9d298aSJeff Roberson static uma_zone_t unp_zone; 9198271db4SGarrett Wollman static unp_gen_t unp_gencnt; 9298271db4SGarrett Wollman static u_int unp_count; 9398271db4SGarrett Wollman 9498271db4SGarrett Wollman static struct unp_head unp_shead, unp_dhead; 9598271db4SGarrett Wollman 96df8bae1dSRodney W. Grimes /* 97df8bae1dSRodney W. Grimes * Unix communications domain. 98df8bae1dSRodney W. Grimes * 99df8bae1dSRodney W. Grimes * TODO: 100df8bae1dSRodney W. Grimes * SEQPACKET, RDM 101df8bae1dSRodney W. Grimes * rethink name space problems 102df8bae1dSRodney W. Grimes * need a proper out-of-band 10398271db4SGarrett Wollman * lock pushdown 104df8bae1dSRodney W. Grimes */ 105e7dd9a10SRobert Watson static const struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 106f708ef1bSPoul-Henning Kamp static ino_t unp_ino; /* prototype for fake inode numbers */ 1076a2989fdSMatthew N. Dodd struct mbuf *unp_addsockcred(struct thread *, struct mbuf *); 108f708ef1bSPoul-Henning Kamp 109ce5f32deSRobert Watson /* 1107e711c3aSRobert Watson * Both send and receive buffers are allocated PIPSIZ bytes of buffering for 1117e711c3aSRobert Watson * stream sockets, although the total for sender and receiver is actually 1127e711c3aSRobert Watson * only PIPSIZ. 1137e711c3aSRobert Watson * 1147e711c3aSRobert Watson * Datagram sockets really use the sendspace as the maximum datagram size, 1157e711c3aSRobert Watson * and don't really want to reserve the sendspace. Their recvspace should be 1167e711c3aSRobert Watson * large enough for at least one max-size datagram plus address. 1177e711c3aSRobert Watson */ 1187e711c3aSRobert Watson #ifndef PIPSIZ 1197e711c3aSRobert Watson #define PIPSIZ 8192 1207e711c3aSRobert Watson #endif 1217e711c3aSRobert Watson static u_long unpst_sendspace = PIPSIZ; 1227e711c3aSRobert Watson static u_long unpst_recvspace = PIPSIZ; 1237e711c3aSRobert Watson static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 1247e711c3aSRobert Watson static u_long unpdg_recvspace = 4*1024; 1257e711c3aSRobert Watson 1267e711c3aSRobert Watson static int unp_rights; /* file descriptors in flight */ 1277e711c3aSRobert Watson 128e4445a03SRobert Watson SYSCTL_NODE(_net, PF_LOCAL, local, CTLFLAG_RW, 0, "Local domain"); 129e4445a03SRobert Watson SYSCTL_NODE(_net_local, SOCK_STREAM, stream, CTLFLAG_RW, 0, "SOCK_STREAM"); 130e4445a03SRobert Watson SYSCTL_NODE(_net_local, SOCK_DGRAM, dgram, CTLFLAG_RW, 0, "SOCK_DGRAM"); 131e4445a03SRobert Watson 1327e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 1337e711c3aSRobert Watson &unpst_sendspace, 0, ""); 1347e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 1357e711c3aSRobert Watson &unpst_recvspace, 0, ""); 1367e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 1377e711c3aSRobert Watson &unpdg_sendspace, 0, ""); 1387e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 1397e711c3aSRobert Watson &unpdg_recvspace, 0, ""); 1407e711c3aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); 1417e711c3aSRobert Watson 1427e711c3aSRobert Watson /* 143ce5f32deSRobert Watson * Currently, UNIX domain sockets are protected by a single subsystem lock, 144ce5f32deSRobert Watson * which covers global data structures and variables, the contents of each 145ce5f32deSRobert Watson * per-socket unpcb structure, and the so_pcb field in sockets attached to 146ce5f32deSRobert Watson * the UNIX domain. This provides for a moderate degree of paralellism, as 147ce5f32deSRobert Watson * receive operations on UNIX domain sockets do not need to acquire the 148ce5f32deSRobert Watson * subsystem lock. Finer grained locking to permit send() without acquiring 149ce5f32deSRobert Watson * a global lock would be a logical next step. 150ce5f32deSRobert Watson * 151ce5f32deSRobert Watson * The UNIX domain socket lock preceds all socket layer locks, including the 152ce5f32deSRobert Watson * socket lock and socket buffer lock, permitting UNIX domain socket code to 153ce5f32deSRobert Watson * call into socket support routines without releasing its locks. 154ce5f32deSRobert Watson * 155ce5f32deSRobert Watson * Some caution is required in areas where the UNIX domain socket code enters 156ce5f32deSRobert Watson * VFS in order to create or find rendezvous points. This results in 157ce5f32deSRobert Watson * dropping of the UNIX domain socket subsystem lock, acquisition of the 158ce5f32deSRobert Watson * Giant lock, and potential sleeping. This increases the chances of races, 159ce5f32deSRobert Watson * and exposes weaknesses in the socket->protocol API by offering poor 160ce5f32deSRobert Watson * failure modes. 161ce5f32deSRobert Watson */ 1620d9ce3a1SRobert Watson static struct mtx unp_mtx; 1630d9ce3a1SRobert Watson #define UNP_LOCK_INIT() \ 1640d9ce3a1SRobert Watson mtx_init(&unp_mtx, "unp", NULL, MTX_DEF) 1650d9ce3a1SRobert Watson #define UNP_LOCK() mtx_lock(&unp_mtx) 1660d9ce3a1SRobert Watson #define UNP_UNLOCK() mtx_unlock(&unp_mtx) 1670d9ce3a1SRobert Watson #define UNP_LOCK_ASSERT() mtx_assert(&unp_mtx, MA_OWNED) 1684c5bc1caSRobert Watson #define UNP_UNLOCK_ASSERT() mtx_assert(&unp_mtx, MA_NOTOWNED) 1690d9ce3a1SRobert Watson 170a0ec558aSRobert Watson /* 171a0ec558aSRobert Watson * Garbage collection of cyclic file descriptor/socket references occurs 172a0ec558aSRobert Watson * asynchronously in a taskqueue context in order to avoid recursion and 173a0ec558aSRobert Watson * reentrance in the UNIX domain socket, file descriptor, and socket layer 174a0ec558aSRobert Watson * code. See unp_gc() for a full description. 175a0ec558aSRobert Watson */ 176a0ec558aSRobert Watson static struct task unp_gc_task; 177a0ec558aSRobert Watson 17870f52b48SBruce Evans static int unp_connect(struct socket *,struct sockaddr *, struct thread *); 1796a2989fdSMatthew N. Dodd static int unp_connect2(struct socket *so, struct socket *so2, int); 1804d77a549SAlfred Perlstein static void unp_disconnect(struct unpcb *); 1814d77a549SAlfred Perlstein static void unp_shutdown(struct unpcb *); 1824d77a549SAlfred Perlstein static void unp_drop(struct unpcb *, int); 183a0ec558aSRobert Watson static void unp_gc(__unused void *, int); 1844d77a549SAlfred Perlstein static void unp_scan(struct mbuf *, void (*)(struct file *)); 1854d77a549SAlfred Perlstein static void unp_mark(struct file *); 1864d77a549SAlfred Perlstein static void unp_discard(struct file *); 1874d77a549SAlfred Perlstein static void unp_freerights(struct file **, int); 1884d77a549SAlfred Perlstein static int unp_internalize(struct mbuf **, struct thread *); 189d374e81eSRobert Watson static int unp_listen(struct socket *, struct unpcb *, int, 190d374e81eSRobert Watson struct thread *); 191f708ef1bSPoul-Henning Kamp 192e4445a03SRobert Watson /* 193e4445a03SRobert Watson * Definitions of protocols supported in the LOCAL domain. 194e4445a03SRobert Watson */ 195e4445a03SRobert Watson static struct domain localdomain; 196e4445a03SRobert Watson static struct protosw localsw[] = { 197e4445a03SRobert Watson { 198e4445a03SRobert Watson .pr_type = SOCK_STREAM, 199e4445a03SRobert Watson .pr_domain = &localdomain, 200e4445a03SRobert Watson .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_RIGHTS, 201e4445a03SRobert Watson .pr_ctloutput = &uipc_ctloutput, 202e4445a03SRobert Watson .pr_usrreqs = &uipc_usrreqs 203e4445a03SRobert Watson }, 204e4445a03SRobert Watson { 205e4445a03SRobert Watson .pr_type = SOCK_DGRAM, 206e4445a03SRobert Watson .pr_domain = &localdomain, 207e4445a03SRobert Watson .pr_flags = PR_ATOMIC|PR_ADDR|PR_RIGHTS, 208e4445a03SRobert Watson .pr_usrreqs = &uipc_usrreqs 209e4445a03SRobert Watson }, 210e4445a03SRobert Watson }; 211e4445a03SRobert Watson 212e4445a03SRobert Watson static struct domain localdomain = { 213e4445a03SRobert Watson .dom_family = AF_LOCAL, 214e4445a03SRobert Watson .dom_name = "local", 215e4445a03SRobert Watson .dom_init = unp_init, 216e4445a03SRobert Watson .dom_externalize = unp_externalize, 217e4445a03SRobert Watson .dom_dispose = unp_dispose, 218e4445a03SRobert Watson .dom_protosw = localsw, 219e4445a03SRobert Watson .dom_protoswNPROTOSW = &localsw[sizeof(localsw)/sizeof(localsw[0])] 220e4445a03SRobert Watson }; 221e4445a03SRobert Watson DOMAIN_SET(local); 222e4445a03SRobert Watson 223ac45e92fSRobert Watson static void 224a29f300eSGarrett Wollman uipc_abort(struct socket *so) 225df8bae1dSRodney W. Grimes { 22640f2ac28SRobert Watson struct unpcb *unp; 227df8bae1dSRodney W. Grimes 22840f2ac28SRobert Watson unp = sotounpcb(so); 2294d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_abort: unp == NULL")); 2304d4b555eSRobert Watson UNP_LOCK(); 231a29f300eSGarrett Wollman unp_drop(unp, ECONNABORTED); 232a152f8a3SRobert Watson UNP_UNLOCK(); 233df8bae1dSRodney W. Grimes } 234df8bae1dSRodney W. Grimes 235a29f300eSGarrett Wollman static int 23657bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam) 237a29f300eSGarrett Wollman { 23840f2ac28SRobert Watson struct unpcb *unp; 2390d9ce3a1SRobert Watson const struct sockaddr *sa; 240df8bae1dSRodney W. Grimes 241df8bae1dSRodney W. Grimes /* 2421c381b19SRobert Watson * Pass back name of connected socket, if it was bound and we are 2431c381b19SRobert Watson * still connected (our peer may have closed already!). 244df8bae1dSRodney W. Grimes */ 2454d4b555eSRobert Watson unp = sotounpcb(so); 2464d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_accept: unp == NULL")); 2470d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 2480d9ce3a1SRobert Watson UNP_LOCK(); 2490d9ce3a1SRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr != NULL) 2500d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 2510d9ce3a1SRobert Watson else 2520d9ce3a1SRobert Watson sa = &sun_noname; 2530d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 2540d9ce3a1SRobert Watson UNP_UNLOCK(); 255e5aeaa0cSDag-Erling Smørgrav return (0); 256a29f300eSGarrett Wollman } 257df8bae1dSRodney W. Grimes 258a29f300eSGarrett Wollman static int 259b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td) 260a29f300eSGarrett Wollman { 2616d32873cSRobert Watson struct unpcb *unp; 2626d32873cSRobert Watson int error; 263df8bae1dSRodney W. Grimes 2646d32873cSRobert Watson KASSERT(so->so_pcb == NULL, ("uipc_attach: so_pcb != NULL")); 2656d32873cSRobert Watson if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 2666d32873cSRobert Watson switch (so->so_type) { 2676d32873cSRobert Watson case SOCK_STREAM: 2686d32873cSRobert Watson error = soreserve(so, unpst_sendspace, unpst_recvspace); 2696d32873cSRobert Watson break; 2706d32873cSRobert Watson 2716d32873cSRobert Watson case SOCK_DGRAM: 2726d32873cSRobert Watson error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 2736d32873cSRobert Watson break; 2746d32873cSRobert Watson 2756d32873cSRobert Watson default: 2766d32873cSRobert Watson panic("unp_attach"); 2776d32873cSRobert Watson } 2786d32873cSRobert Watson if (error) 2796d32873cSRobert Watson return (error); 2806d32873cSRobert Watson } 2816d32873cSRobert Watson unp = uma_zalloc(unp_zone, M_WAITOK | M_ZERO); 2826d32873cSRobert Watson if (unp == NULL) 2836d32873cSRobert Watson return (ENOBUFS); 2846d32873cSRobert Watson LIST_INIT(&unp->unp_refs); 2856d32873cSRobert Watson unp->unp_socket = so; 2866d32873cSRobert Watson so->so_pcb = unp; 2876d32873cSRobert Watson 2886d32873cSRobert Watson UNP_LOCK(); 2896d32873cSRobert Watson unp->unp_gencnt = ++unp_gencnt; 2906d32873cSRobert Watson unp_count++; 291b7e2f3ecSRobert Watson LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead : &unp_shead, 292b7e2f3ecSRobert Watson unp, unp_link); 2936d32873cSRobert Watson UNP_UNLOCK(); 2946d32873cSRobert Watson 2956d32873cSRobert Watson return (0); 296a29f300eSGarrett Wollman } 297a29f300eSGarrett Wollman 298a29f300eSGarrett Wollman static int 299b40ce416SJulian Elischer uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 300a29f300eSGarrett Wollman { 301dd47f5caSRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam; 302dd47f5caSRobert Watson struct vattr vattr; 303dd47f5caSRobert Watson int error, namelen; 304dd47f5caSRobert Watson struct nameidata nd; 30540f2ac28SRobert Watson struct unpcb *unp; 306dd47f5caSRobert Watson struct vnode *vp; 307dd47f5caSRobert Watson struct mount *mp; 308dd47f5caSRobert Watson char *buf; 309a29f300eSGarrett Wollman 31040f2ac28SRobert Watson unp = sotounpcb(so); 3114d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_bind: unp == NULL")); 3124f1f0ef5SRobert Watson 3134f1f0ef5SRobert Watson namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 3144f1f0ef5SRobert Watson if (namelen <= 0) 3154f1f0ef5SRobert Watson return (EINVAL); 316dd47f5caSRobert Watson 317dd47f5caSRobert Watson /* 3184f1f0ef5SRobert Watson * We don't allow simultaneous bind() calls on a single UNIX domain 3194f1f0ef5SRobert Watson * socket, so flag in-progress operations, and return an error if an 3204f1f0ef5SRobert Watson * operation is already in progress. 3214f1f0ef5SRobert Watson * 3224f1f0ef5SRobert Watson * Historically, we have not allowed a socket to be rebound, so this 3234f1f0ef5SRobert Watson * also returns an error. Not allowing re-binding certainly 3244f1f0ef5SRobert Watson * simplifies the implementation and avoids a great many possible 3254f1f0ef5SRobert Watson * failure modes. 326dd47f5caSRobert Watson */ 3274f1f0ef5SRobert Watson UNP_LOCK(); 328dd47f5caSRobert Watson if (unp->unp_vnode != NULL) { 32940f2ac28SRobert Watson UNP_UNLOCK(); 330dd47f5caSRobert Watson return (EINVAL); 331dd47f5caSRobert Watson } 3324f1f0ef5SRobert Watson if (unp->unp_flags & UNP_BINDING) { 333dd47f5caSRobert Watson UNP_UNLOCK(); 3344f1f0ef5SRobert Watson return (EALREADY); 335dd47f5caSRobert Watson } 3364f1f0ef5SRobert Watson unp->unp_flags |= UNP_BINDING; 337dd47f5caSRobert Watson UNP_UNLOCK(); 338dd47f5caSRobert Watson 339dd47f5caSRobert Watson buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 340dd47f5caSRobert Watson strlcpy(buf, soun->sun_path, namelen + 1); 341dd47f5caSRobert Watson 342dd47f5caSRobert Watson mtx_lock(&Giant); 343dd47f5caSRobert Watson restart: 344dd47f5caSRobert Watson mtx_assert(&Giant, MA_OWNED); 345dd47f5caSRobert Watson NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME, UIO_SYSSPACE, 346dd47f5caSRobert Watson buf, td); 347dd47f5caSRobert Watson /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 348dd47f5caSRobert Watson error = namei(&nd); 349dd47f5caSRobert Watson if (error) 3504f1f0ef5SRobert Watson goto error; 351dd47f5caSRobert Watson vp = nd.ni_vp; 352dd47f5caSRobert Watson if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 353dd47f5caSRobert Watson NDFREE(&nd, NDF_ONLY_PNBUF); 354dd47f5caSRobert Watson if (nd.ni_dvp == vp) 355dd47f5caSRobert Watson vrele(nd.ni_dvp); 356dd47f5caSRobert Watson else 357dd47f5caSRobert Watson vput(nd.ni_dvp); 358dd47f5caSRobert Watson if (vp != NULL) { 359dd47f5caSRobert Watson vrele(vp); 360dd47f5caSRobert Watson error = EADDRINUSE; 3614f1f0ef5SRobert Watson goto error; 362dd47f5caSRobert Watson } 363dd47f5caSRobert Watson error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 364dd47f5caSRobert Watson if (error) 3654f1f0ef5SRobert Watson goto error; 366dd47f5caSRobert Watson goto restart; 367dd47f5caSRobert Watson } 368dd47f5caSRobert Watson VATTR_NULL(&vattr); 369dd47f5caSRobert Watson vattr.va_type = VSOCK; 370dd47f5caSRobert Watson vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 371dd47f5caSRobert Watson #ifdef MAC 372dd47f5caSRobert Watson error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 373dd47f5caSRobert Watson &vattr); 374dd47f5caSRobert Watson #endif 375dd47f5caSRobert Watson if (error == 0) { 376dd47f5caSRobert Watson VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE); 377dd47f5caSRobert Watson error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 378dd47f5caSRobert Watson } 379dd47f5caSRobert Watson NDFREE(&nd, NDF_ONLY_PNBUF); 380dd47f5caSRobert Watson vput(nd.ni_dvp); 381dd47f5caSRobert Watson if (error) { 382dd47f5caSRobert Watson vn_finished_write(mp); 3834f1f0ef5SRobert Watson goto error; 384dd47f5caSRobert Watson } 385dd47f5caSRobert Watson vp = nd.ni_vp; 3864f1f0ef5SRobert Watson ASSERT_VOP_LOCKED(vp, "uipc_bind"); 387dd47f5caSRobert Watson soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 388dd47f5caSRobert Watson UNP_LOCK(); 389dd47f5caSRobert Watson vp->v_socket = unp->unp_socket; 390dd47f5caSRobert Watson unp->unp_vnode = vp; 391dd47f5caSRobert Watson unp->unp_addr = soun; 3924f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_BINDING; 393dd47f5caSRobert Watson UNP_UNLOCK(); 394dd47f5caSRobert Watson VOP_UNLOCK(vp, 0, td); 395dd47f5caSRobert Watson vn_finished_write(mp); 3964f1f0ef5SRobert Watson mtx_unlock(&Giant); 3974f1f0ef5SRobert Watson free(buf, M_TEMP); 3984f1f0ef5SRobert Watson return (0); 3994f1f0ef5SRobert Watson error: 4004f1f0ef5SRobert Watson UNP_LOCK(); 4014f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_BINDING; 4024f1f0ef5SRobert Watson UNP_UNLOCK(); 403dd47f5caSRobert Watson mtx_unlock(&Giant); 404dd47f5caSRobert Watson free(buf, M_TEMP); 40540f2ac28SRobert Watson return (error); 406a29f300eSGarrett Wollman } 407a29f300eSGarrett Wollman 408a29f300eSGarrett Wollman static int 409b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 410a29f300eSGarrett Wollman { 4110d9ce3a1SRobert Watson int error; 412a29f300eSGarrett Wollman 413fd179ee9SRobert Watson KASSERT(td == curthread, ("uipc_connect: td != curthread")); 4144d4b555eSRobert Watson UNP_LOCK(); 415fd179ee9SRobert Watson error = unp_connect(so, nam, td); 4160d9ce3a1SRobert Watson UNP_UNLOCK(); 4170d9ce3a1SRobert Watson return (error); 418a29f300eSGarrett Wollman } 419a29f300eSGarrett Wollman 420a152f8a3SRobert Watson /* 421a152f8a3SRobert Watson * XXXRW: Should also unbind? 422a152f8a3SRobert Watson */ 423a152f8a3SRobert Watson static void 424a152f8a3SRobert Watson uipc_close(struct socket *so) 425a152f8a3SRobert Watson { 426a152f8a3SRobert Watson struct unpcb *unp; 427a152f8a3SRobert Watson 428a152f8a3SRobert Watson unp = sotounpcb(so); 429a152f8a3SRobert Watson KASSERT(unp != NULL, ("uipc_close: unp == NULL")); 430a152f8a3SRobert Watson UNP_LOCK(); 431a152f8a3SRobert Watson unp_disconnect(unp); 432a152f8a3SRobert Watson UNP_UNLOCK(); 433a152f8a3SRobert Watson } 434a152f8a3SRobert Watson 435db48c0d2SRobert Watson int 436a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2) 437a29f300eSGarrett Wollman { 43840f2ac28SRobert Watson struct unpcb *unp; 4390d9ce3a1SRobert Watson int error; 440a29f300eSGarrett Wollman 44140f2ac28SRobert Watson unp = sotounpcb(so1); 4424d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_connect2: unp == NULL")); 4434d4b555eSRobert Watson UNP_LOCK(); 4446a2989fdSMatthew N. Dodd error = unp_connect2(so1, so2, PRU_CONNECT2); 4450d9ce3a1SRobert Watson UNP_UNLOCK(); 4460d9ce3a1SRobert Watson return (error); 447a29f300eSGarrett Wollman } 448a29f300eSGarrett Wollman 449a29f300eSGarrett Wollman /* control is EOPNOTSUPP */ 450a29f300eSGarrett Wollman 451bc725eafSRobert Watson static void 452a29f300eSGarrett Wollman uipc_detach(struct socket *so) 453a29f300eSGarrett Wollman { 4546d32873cSRobert Watson int local_unp_rights; 45540f2ac28SRobert Watson struct unpcb *unp; 4566d32873cSRobert Watson struct vnode *vp; 457a29f300eSGarrett Wollman 45840f2ac28SRobert Watson unp = sotounpcb(so); 4594d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_detach: unp == NULL")); 4604d4b555eSRobert Watson UNP_LOCK(); 4616d32873cSRobert Watson LIST_REMOVE(unp, unp_link); 4626d32873cSRobert Watson unp->unp_gencnt = ++unp_gencnt; 4636d32873cSRobert Watson --unp_count; 4646d32873cSRobert Watson if ((vp = unp->unp_vnode) != NULL) { 4656d32873cSRobert Watson /* 4666d32873cSRobert Watson * XXXRW: should v_socket be frobbed only while holding 4676d32873cSRobert Watson * Giant? 4686d32873cSRobert Watson */ 4696d32873cSRobert Watson unp->unp_vnode->v_socket = NULL; 4706d32873cSRobert Watson unp->unp_vnode = NULL; 4716d32873cSRobert Watson } 4726d32873cSRobert Watson if (unp->unp_conn != NULL) 4736d32873cSRobert Watson unp_disconnect(unp); 4746d32873cSRobert Watson while (!LIST_EMPTY(&unp->unp_refs)) { 4756d32873cSRobert Watson struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 4766d32873cSRobert Watson unp_drop(ref, ECONNRESET); 4776d32873cSRobert Watson } 4786d32873cSRobert Watson unp->unp_socket->so_pcb = NULL; 4796d32873cSRobert Watson local_unp_rights = unp_rights; 4806d32873cSRobert Watson UNP_UNLOCK(); 4816d32873cSRobert Watson if (unp->unp_addr != NULL) 4826d32873cSRobert Watson FREE(unp->unp_addr, M_SONAME); 4836d32873cSRobert Watson uma_zfree(unp_zone, unp); 4846d32873cSRobert Watson if (vp) { 4856d32873cSRobert Watson int vfslocked; 4866d32873cSRobert Watson 4876d32873cSRobert Watson vfslocked = VFS_LOCK_GIANT(vp->v_mount); 4886d32873cSRobert Watson vrele(vp); 4896d32873cSRobert Watson VFS_UNLOCK_GIANT(vfslocked); 4906d32873cSRobert Watson } 4916d32873cSRobert Watson if (local_unp_rights) 4926d32873cSRobert Watson taskqueue_enqueue(taskqueue_thread, &unp_gc_task); 493a29f300eSGarrett Wollman } 494a29f300eSGarrett Wollman 495a29f300eSGarrett Wollman static int 496a29f300eSGarrett Wollman uipc_disconnect(struct socket *so) 497a29f300eSGarrett Wollman { 49840f2ac28SRobert Watson struct unpcb *unp; 499a29f300eSGarrett Wollman 50040f2ac28SRobert Watson unp = sotounpcb(so); 5014d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_disconnect: unp == NULL")); 5024d4b555eSRobert Watson UNP_LOCK(); 503a29f300eSGarrett Wollman unp_disconnect(unp); 5040d9ce3a1SRobert Watson UNP_UNLOCK(); 505e5aeaa0cSDag-Erling Smørgrav return (0); 506a29f300eSGarrett Wollman } 507a29f300eSGarrett Wollman 508a29f300eSGarrett Wollman static int 509d374e81eSRobert Watson uipc_listen(struct socket *so, int backlog, struct thread *td) 510a29f300eSGarrett Wollman { 51140f2ac28SRobert Watson struct unpcb *unp; 5120d9ce3a1SRobert Watson int error; 513a29f300eSGarrett Wollman 51440f2ac28SRobert Watson unp = sotounpcb(so); 5154d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_listen: unp == NULL")); 5164d4b555eSRobert Watson UNP_LOCK(); 5174d4b555eSRobert Watson if (unp->unp_vnode == NULL) { 51840f2ac28SRobert Watson UNP_UNLOCK(); 51940f2ac28SRobert Watson return (EINVAL); 52040f2ac28SRobert Watson } 521d374e81eSRobert Watson error = unp_listen(so, unp, backlog, td); 5220d9ce3a1SRobert Watson UNP_UNLOCK(); 5230d9ce3a1SRobert Watson return (error); 524a29f300eSGarrett Wollman } 525a29f300eSGarrett Wollman 526a29f300eSGarrett Wollman static int 52757bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 528a29f300eSGarrett Wollman { 52940f2ac28SRobert Watson struct unpcb *unp; 5300d9ce3a1SRobert Watson const struct sockaddr *sa; 531a29f300eSGarrett Wollman 5324d4b555eSRobert Watson unp = sotounpcb(so); 5334d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL")); 5340d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 5350d9ce3a1SRobert Watson UNP_LOCK(); 536fc3fcacfSRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr!= NULL) 5370d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 538bdc5f6a3SHajimu UMEMOTO else { 539bdc5f6a3SHajimu UMEMOTO /* 540bdc5f6a3SHajimu UMEMOTO * XXX: It seems that this test always fails even when 541bdc5f6a3SHajimu UMEMOTO * connection is established. So, this else clause is 542bdc5f6a3SHajimu UMEMOTO * added as workaround to return PF_LOCAL sockaddr. 543bdc5f6a3SHajimu UMEMOTO */ 5440d9ce3a1SRobert Watson sa = &sun_noname; 545bdc5f6a3SHajimu UMEMOTO } 5460d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 5470d9ce3a1SRobert Watson UNP_UNLOCK(); 548e5aeaa0cSDag-Erling Smørgrav return (0); 549a29f300eSGarrett Wollman } 550a29f300eSGarrett Wollman 551a29f300eSGarrett Wollman static int 552a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 553a29f300eSGarrett Wollman { 55440f2ac28SRobert Watson struct unpcb *unp; 555a29f300eSGarrett Wollman struct socket *so2; 556337cc6b6SRobert Watson u_int mbcnt, sbcc; 5576aef685fSBrian Feldman u_long newhiwat; 558a29f300eSGarrett Wollman 55940f2ac28SRobert Watson unp = sotounpcb(so); 5604d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_rcvd: unp == NULL")); 561df8bae1dSRodney W. Grimes switch (so->so_type) { 562df8bae1dSRodney W. Grimes case SOCK_DGRAM: 563a29f300eSGarrett Wollman panic("uipc_rcvd DGRAM?"); 564df8bae1dSRodney W. Grimes /*NOTREACHED*/ 565df8bae1dSRodney W. Grimes 566df8bae1dSRodney W. Grimes case SOCK_STREAM: 567df8bae1dSRodney W. Grimes /* 5681c381b19SRobert Watson * Adjust backpressure on sender and wakeup any waiting to 5691c381b19SRobert Watson * write. 570df8bae1dSRodney W. Grimes */ 571337cc6b6SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 572337cc6b6SRobert Watson mbcnt = so->so_rcv.sb_mbcnt; 573337cc6b6SRobert Watson sbcc = so->so_rcv.sb_cc; 574337cc6b6SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 575337cc6b6SRobert Watson UNP_LOCK(); 576337cc6b6SRobert Watson if (unp->unp_conn == NULL) { 577337cc6b6SRobert Watson UNP_UNLOCK(); 578337cc6b6SRobert Watson break; 579337cc6b6SRobert Watson } 580337cc6b6SRobert Watson so2 = unp->unp_conn->unp_socket; 581337cc6b6SRobert Watson SOCKBUF_LOCK(&so2->so_snd); 582337cc6b6SRobert Watson so2->so_snd.sb_mbmax += unp->unp_mbcnt - mbcnt; 583337cc6b6SRobert Watson newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - sbcc; 584f535380cSDon Lewis (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat, 5856aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 5861e4d7da7SRobert Watson sowwakeup_locked(so2); 587337cc6b6SRobert Watson unp->unp_mbcnt = mbcnt; 588337cc6b6SRobert Watson unp->unp_cc = sbcc; 589337cc6b6SRobert Watson UNP_UNLOCK(); 590df8bae1dSRodney W. Grimes break; 591df8bae1dSRodney W. Grimes 592df8bae1dSRodney W. Grimes default: 593a29f300eSGarrett Wollman panic("uipc_rcvd unknown socktype"); 594df8bae1dSRodney W. Grimes } 595e5aeaa0cSDag-Erling Smørgrav return (0); 596a29f300eSGarrett Wollman } 597df8bae1dSRodney W. Grimes 598a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 599a29f300eSGarrett Wollman 600a29f300eSGarrett Wollman static int 60157bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 602b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 603a29f300eSGarrett Wollman { 604f3f49bbbSRobert Watson struct unpcb *unp, *unp2; 605a29f300eSGarrett Wollman struct socket *so2; 606337cc6b6SRobert Watson u_int mbcnt, sbcc; 6076aef685fSBrian Feldman u_long newhiwat; 608f3f49bbbSRobert Watson int error = 0; 609a29f300eSGarrett Wollman 61040f2ac28SRobert Watson unp = sotounpcb(so); 6114d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_send: unp == NULL")); 612a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 613a29f300eSGarrett Wollman error = EOPNOTSUPP; 614a29f300eSGarrett Wollman goto release; 615a29f300eSGarrett Wollman } 616a29f300eSGarrett Wollman 617fc3fcacfSRobert Watson if (control != NULL && (error = unp_internalize(&control, td))) 618a29f300eSGarrett Wollman goto release; 619df8bae1dSRodney W. Grimes 6200d9ce3a1SRobert Watson UNP_LOCK(); 621a29f300eSGarrett Wollman switch (so->so_type) { 622a29f300eSGarrett Wollman case SOCK_DGRAM: 623a29f300eSGarrett Wollman { 624e7dd9a10SRobert Watson const struct sockaddr *from; 625df8bae1dSRodney W. Grimes 626fc3fcacfSRobert Watson if (nam != NULL) { 627fc3fcacfSRobert Watson if (unp->unp_conn != NULL) { 628df8bae1dSRodney W. Grimes error = EISCONN; 629df8bae1dSRodney W. Grimes break; 630df8bae1dSRodney W. Grimes } 631b40ce416SJulian Elischer error = unp_connect(so, nam, td); 632df8bae1dSRodney W. Grimes if (error) 633df8bae1dSRodney W. Grimes break; 634df8bae1dSRodney W. Grimes } 635b5ff0914SRobert Watson /* 636b5ff0914SRobert Watson * Because connect() and send() are non-atomic in a sendto() 637b5ff0914SRobert Watson * with a target address, it's possible that the socket will 638b5ff0914SRobert Watson * have disconnected before the send() can run. In that case 639b5ff0914SRobert Watson * return the slightly counter-intuitive but otherwise 640b5ff0914SRobert Watson * correct error that the socket is not connected. 641b5ff0914SRobert Watson */ 642f3f49bbbSRobert Watson unp2 = unp->unp_conn; 643b5ff0914SRobert Watson if (unp2 == NULL) { 644b5ff0914SRobert Watson error = ENOTCONN; 645b5ff0914SRobert Watson break; 646b5ff0914SRobert Watson } 647f3f49bbbSRobert Watson so2 = unp2->unp_socket; 648fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 64957bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 650df8bae1dSRodney W. Grimes else 651df8bae1dSRodney W. Grimes from = &sun_noname; 652f3f49bbbSRobert Watson if (unp2->unp_flags & UNP_WANTCRED) 6536a2989fdSMatthew N. Dodd control = unp_addsockcred(td, control); 654a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 655a34b7046SRobert Watson if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) { 6561e4d7da7SRobert Watson sorwakeup_locked(so2); 657fc3fcacfSRobert Watson m = NULL; 658fc3fcacfSRobert Watson control = NULL; 659e5aeaa0cSDag-Erling Smørgrav } else { 660a34b7046SRobert Watson SOCKBUF_UNLOCK(&so2->so_rcv); 661df8bae1dSRodney W. Grimes error = ENOBUFS; 662e5aeaa0cSDag-Erling Smørgrav } 663fc3fcacfSRobert Watson if (nam != NULL) 664df8bae1dSRodney W. Grimes unp_disconnect(unp); 665df8bae1dSRodney W. Grimes break; 666df8bae1dSRodney W. Grimes } 667df8bae1dSRodney W. Grimes 668df8bae1dSRodney W. Grimes case SOCK_STREAM: 6696b8fda4dSGarrett Wollman /* 6701c381b19SRobert Watson * Connect if not connected yet. 6711c381b19SRobert Watson * 6721c381b19SRobert Watson * Note: A better implementation would complain if not equal 6731c381b19SRobert Watson * to the peer's address. 6746b8fda4dSGarrett Wollman */ 675402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 676fc3fcacfSRobert Watson if (nam != NULL) { 677b40ce416SJulian Elischer error = unp_connect(so, nam, td); 678402cc72dSDavid Greenman if (error) 6796b8fda4dSGarrett Wollman break; /* XXX */ 680402cc72dSDavid Greenman } else { 681402cc72dSDavid Greenman error = ENOTCONN; 682402cc72dSDavid Greenman break; 683402cc72dSDavid Greenman } 684402cc72dSDavid Greenman } 685402cc72dSDavid Greenman 686337cc6b6SRobert Watson /* Lockless read. */ 687c0b99ffaSRobert Watson if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 688df8bae1dSRodney W. Grimes error = EPIPE; 689df8bae1dSRodney W. Grimes break; 690df8bae1dSRodney W. Grimes } 691b5ff0914SRobert Watson /* 692b5ff0914SRobert Watson * Because connect() and send() are non-atomic in a sendto() 693b5ff0914SRobert Watson * with a target address, it's possible that the socket will 694b5ff0914SRobert Watson * have disconnected before the send() can run. In that case 695b5ff0914SRobert Watson * return the slightly counter-intuitive but otherwise 696b5ff0914SRobert Watson * correct error that the socket is not connected. 697b5ff0914SRobert Watson */ 698f3f49bbbSRobert Watson unp2 = unp->unp_conn; 699b5ff0914SRobert Watson if (unp2 == NULL) { 700b5ff0914SRobert Watson error = ENOTCONN; 701b5ff0914SRobert Watson break; 702b5ff0914SRobert Watson } 703f3f49bbbSRobert Watson so2 = unp2->unp_socket; 704a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 705f3f49bbbSRobert Watson if (unp2->unp_flags & UNP_WANTCRED) { 7066a2989fdSMatthew N. Dodd /* 7076a2989fdSMatthew N. Dodd * Credentials are passed only once on 7086a2989fdSMatthew N. Dodd * SOCK_STREAM. 7096a2989fdSMatthew N. Dodd */ 710f3f49bbbSRobert Watson unp2->unp_flags &= ~UNP_WANTCRED; 7116a2989fdSMatthew N. Dodd control = unp_addsockcred(td, control); 7126a2989fdSMatthew N. Dodd } 713df8bae1dSRodney W. Grimes /* 7141c381b19SRobert Watson * Send to paired receive port, and then reduce send buffer 7151c381b19SRobert Watson * hiwater marks to maintain backpressure. Wake up readers. 716df8bae1dSRodney W. Grimes */ 717fc3fcacfSRobert Watson if (control != NULL) { 718a34b7046SRobert Watson if (sbappendcontrol_locked(&so2->so_rcv, m, control)) 719fc3fcacfSRobert Watson control = NULL; 720e5aeaa0cSDag-Erling Smørgrav } else { 721a34b7046SRobert Watson sbappend_locked(&so2->so_rcv, m); 722e5aeaa0cSDag-Erling Smørgrav } 723f3f49bbbSRobert Watson mbcnt = so2->so_rcv.sb_mbcnt - unp2->unp_mbcnt; 724f3f49bbbSRobert Watson unp2->unp_mbcnt = so2->so_rcv.sb_mbcnt; 725337cc6b6SRobert Watson sbcc = so2->so_rcv.sb_cc; 726337cc6b6SRobert Watson sorwakeup_locked(so2); 727337cc6b6SRobert Watson 728337cc6b6SRobert Watson SOCKBUF_LOCK(&so->so_snd); 729f3f49bbbSRobert Watson newhiwat = so->so_snd.sb_hiwat - (sbcc - unp2->unp_cc); 730f535380cSDon Lewis (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat, 7316aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 732337cc6b6SRobert Watson so->so_snd.sb_mbmax -= mbcnt; 7337abe2ac2SAlan Cox SOCKBUF_UNLOCK(&so->so_snd); 734337cc6b6SRobert Watson 735f3f49bbbSRobert Watson unp2->unp_cc = sbcc; 736fc3fcacfSRobert Watson m = NULL; 737df8bae1dSRodney W. Grimes break; 738df8bae1dSRodney W. Grimes 739df8bae1dSRodney W. Grimes default: 740a29f300eSGarrett Wollman panic("uipc_send unknown socktype"); 741df8bae1dSRodney W. Grimes } 742a29f300eSGarrett Wollman 7436b8fda4dSGarrett Wollman /* 7446b8fda4dSGarrett Wollman * SEND_EOF is equivalent to a SEND followed by 7456b8fda4dSGarrett Wollman * a SHUTDOWN. 7466b8fda4dSGarrett Wollman */ 747a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 7486b8fda4dSGarrett Wollman socantsendmore(so); 7496b8fda4dSGarrett Wollman unp_shutdown(unp); 7506b8fda4dSGarrett Wollman } 7510d9ce3a1SRobert Watson UNP_UNLOCK(); 752df8bae1dSRodney W. Grimes 753fc3fcacfSRobert Watson if (control != NULL && error != 0) 754bd508d39SDon Lewis unp_dispose(control); 755bd508d39SDon Lewis 756a29f300eSGarrett Wollman release: 757fc3fcacfSRobert Watson if (control != NULL) 758a29f300eSGarrett Wollman m_freem(control); 759fc3fcacfSRobert Watson if (m != NULL) 760a29f300eSGarrett Wollman m_freem(m); 761e5aeaa0cSDag-Erling Smørgrav return (error); 762a29f300eSGarrett Wollman } 763df8bae1dSRodney W. Grimes 764a29f300eSGarrett Wollman static int 765a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 766a29f300eSGarrett Wollman { 76740f2ac28SRobert Watson struct unpcb *unp; 768a29f300eSGarrett Wollman struct socket *so2; 769a29f300eSGarrett Wollman 77040f2ac28SRobert Watson unp = sotounpcb(so); 7714d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sense: unp == NULL")); 7724d4b555eSRobert Watson UNP_LOCK(); 773a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 774fc3fcacfSRobert Watson if (so->so_type == SOCK_STREAM && unp->unp_conn != NULL) { 775df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 776a29f300eSGarrett Wollman sb->st_blksize += so2->so_rcv.sb_cc; 777df8bae1dSRodney W. Grimes } 778f3732fd1SPoul-Henning Kamp sb->st_dev = NODEV; 779df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 7806f782c46SJeffrey Hsu unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 781a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 7820d9ce3a1SRobert Watson UNP_UNLOCK(); 783df8bae1dSRodney W. Grimes return (0); 784a29f300eSGarrett Wollman } 785df8bae1dSRodney W. Grimes 786a29f300eSGarrett Wollman static int 787a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 788a29f300eSGarrett Wollman { 78940f2ac28SRobert Watson struct unpcb *unp; 790df8bae1dSRodney W. Grimes 79140f2ac28SRobert Watson unp = sotounpcb(so); 7924d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_shutdown: unp == NULL")); 7934d4b555eSRobert Watson UNP_LOCK(); 794a29f300eSGarrett Wollman socantsendmore(so); 795a29f300eSGarrett Wollman unp_shutdown(unp); 7960d9ce3a1SRobert Watson UNP_UNLOCK(); 797e5aeaa0cSDag-Erling Smørgrav return (0); 798a29f300eSGarrett Wollman } 799df8bae1dSRodney W. Grimes 800a29f300eSGarrett Wollman static int 80157bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 802a29f300eSGarrett Wollman { 80340f2ac28SRobert Watson struct unpcb *unp; 8040d9ce3a1SRobert Watson const struct sockaddr *sa; 805a29f300eSGarrett Wollman 8064d4b555eSRobert Watson unp = sotounpcb(so); 8074d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL")); 8080d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 8090d9ce3a1SRobert Watson UNP_LOCK(); 810fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 8110d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_addr; 81283f3198bSThomas Moestl else 8130d9ce3a1SRobert Watson sa = &sun_noname; 8140d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 8150d9ce3a1SRobert Watson UNP_UNLOCK(); 816e5aeaa0cSDag-Erling Smørgrav return (0); 817df8bae1dSRodney W. Grimes } 818a29f300eSGarrett Wollman 819a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = { 820756d52a1SPoul-Henning Kamp .pru_abort = uipc_abort, 821756d52a1SPoul-Henning Kamp .pru_accept = uipc_accept, 822756d52a1SPoul-Henning Kamp .pru_attach = uipc_attach, 823756d52a1SPoul-Henning Kamp .pru_bind = uipc_bind, 824756d52a1SPoul-Henning Kamp .pru_connect = uipc_connect, 825756d52a1SPoul-Henning Kamp .pru_connect2 = uipc_connect2, 826756d52a1SPoul-Henning Kamp .pru_detach = uipc_detach, 827756d52a1SPoul-Henning Kamp .pru_disconnect = uipc_disconnect, 828756d52a1SPoul-Henning Kamp .pru_listen = uipc_listen, 829756d52a1SPoul-Henning Kamp .pru_peeraddr = uipc_peeraddr, 830756d52a1SPoul-Henning Kamp .pru_rcvd = uipc_rcvd, 831756d52a1SPoul-Henning Kamp .pru_send = uipc_send, 832756d52a1SPoul-Henning Kamp .pru_sense = uipc_sense, 833756d52a1SPoul-Henning Kamp .pru_shutdown = uipc_shutdown, 834756d52a1SPoul-Henning Kamp .pru_sockaddr = uipc_sockaddr, 835a152f8a3SRobert Watson .pru_close = uipc_close, 836a29f300eSGarrett Wollman }; 837df8bae1dSRodney W. Grimes 8380c1bb4fbSDima Dorfman int 839892af6b9SRobert Watson uipc_ctloutput(struct socket *so, struct sockopt *sopt) 8400c1bb4fbSDima Dorfman { 84140f2ac28SRobert Watson struct unpcb *unp; 8420d9ce3a1SRobert Watson struct xucred xu; 8436a2989fdSMatthew N. Dodd int error, optval; 8446a2989fdSMatthew N. Dodd 84596a041b5SMatthew N. Dodd if (sopt->sopt_level != 0) 84696a041b5SMatthew N. Dodd return (EINVAL); 84796a041b5SMatthew N. Dodd 8486a2989fdSMatthew N. Dodd unp = sotounpcb(so); 8494d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL")); 8504d4b555eSRobert Watson UNP_LOCK(); 8516a2989fdSMatthew N. Dodd error = 0; 8520c1bb4fbSDima Dorfman switch (sopt->sopt_dir) { 8530c1bb4fbSDima Dorfman case SOPT_GET: 8540c1bb4fbSDima Dorfman switch (sopt->sopt_name) { 8550c1bb4fbSDima Dorfman case LOCAL_PEERCRED: 8560c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC) 8570d9ce3a1SRobert Watson xu = unp->unp_peercred; 8580c1bb4fbSDima Dorfman else { 8590c1bb4fbSDima Dorfman if (so->so_type == SOCK_STREAM) 8600c1bb4fbSDima Dorfman error = ENOTCONN; 8610c1bb4fbSDima Dorfman else 8620c1bb4fbSDima Dorfman error = EINVAL; 8630c1bb4fbSDima Dorfman } 8640d9ce3a1SRobert Watson if (error == 0) 8650d9ce3a1SRobert Watson error = sooptcopyout(sopt, &xu, sizeof(xu)); 8660c1bb4fbSDima Dorfman break; 8676a2989fdSMatthew N. Dodd case LOCAL_CREDS: 8686a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0; 8696a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 8706a2989fdSMatthew N. Dodd break; 8716a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 8726a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0; 8736a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 8746a2989fdSMatthew N. Dodd break; 8750c1bb4fbSDima Dorfman default: 8760c1bb4fbSDima Dorfman error = EOPNOTSUPP; 8770c1bb4fbSDima Dorfman break; 8780c1bb4fbSDima Dorfman } 8790c1bb4fbSDima Dorfman break; 8800c1bb4fbSDima Dorfman case SOPT_SET: 8816a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 8826a2989fdSMatthew N. Dodd case LOCAL_CREDS: 8836a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 8846a2989fdSMatthew N. Dodd error = sooptcopyin(sopt, &optval, sizeof(optval), 8856a2989fdSMatthew N. Dodd sizeof(optval)); 8866a2989fdSMatthew N. Dodd if (error) 8876a2989fdSMatthew N. Dodd break; 8886a2989fdSMatthew N. Dodd 8896a2989fdSMatthew N. Dodd #define OPTSET(bit) \ 8906a2989fdSMatthew N. Dodd if (optval) \ 8916a2989fdSMatthew N. Dodd unp->unp_flags |= bit; \ 8926a2989fdSMatthew N. Dodd else \ 8936a2989fdSMatthew N. Dodd unp->unp_flags &= ~bit; 8946a2989fdSMatthew N. Dodd 8956a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 8966a2989fdSMatthew N. Dodd case LOCAL_CREDS: 8976a2989fdSMatthew N. Dodd OPTSET(UNP_WANTCRED); 8986a2989fdSMatthew N. Dodd break; 8996a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 9006a2989fdSMatthew N. Dodd OPTSET(UNP_CONNWAIT); 9016a2989fdSMatthew N. Dodd break; 9026a2989fdSMatthew N. Dodd default: 9036a2989fdSMatthew N. Dodd break; 9046a2989fdSMatthew N. Dodd } 9056a2989fdSMatthew N. Dodd break; 9066a2989fdSMatthew N. Dodd #undef OPTSET 9076a2989fdSMatthew N. Dodd default: 9086a2989fdSMatthew N. Dodd error = ENOPROTOOPT; 9096a2989fdSMatthew N. Dodd break; 9106a2989fdSMatthew N. Dodd } 911abb886faSMatthew N. Dodd break; 9120c1bb4fbSDima Dorfman default: 9130c1bb4fbSDima Dorfman error = EOPNOTSUPP; 9140c1bb4fbSDima Dorfman break; 9150c1bb4fbSDima Dorfman } 9166a2989fdSMatthew N. Dodd UNP_UNLOCK(); 9170c1bb4fbSDima Dorfman return (error); 9180c1bb4fbSDima Dorfman } 9190c1bb4fbSDima Dorfman 920f708ef1bSPoul-Henning Kamp static int 921892af6b9SRobert Watson unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 922df8bae1dSRodney W. Grimes { 923892af6b9SRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam; 924892af6b9SRobert Watson struct vnode *vp; 925892af6b9SRobert Watson struct socket *so2, *so3; 926b295bdcdSRobert Watson struct unpcb *unp, *unp2, *unp3; 92757bf258eSGarrett Wollman int error, len; 928df8bae1dSRodney W. Grimes struct nameidata nd; 92957bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 9300d9ce3a1SRobert Watson struct sockaddr *sa; 9310d9ce3a1SRobert Watson 9320d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 933df8bae1dSRodney W. Grimes 9344d4b555eSRobert Watson unp = sotounpcb(so); 9354d4b555eSRobert Watson KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 93657bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 93757bf258eSGarrett Wollman if (len <= 0) 938e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 93955c85568SRobert Drehmel strlcpy(buf, soun->sun_path, len + 1); 9404f1f0ef5SRobert Watson if (unp->unp_flags & UNP_CONNECTING) { 9414f1f0ef5SRobert Watson UNP_UNLOCK(); 9424f1f0ef5SRobert Watson return (EALREADY); 9434f1f0ef5SRobert Watson } 9440d9ce3a1SRobert Watson UNP_UNLOCK(); 9450d9ce3a1SRobert Watson sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 9460d9ce3a1SRobert Watson mtx_lock(&Giant); 947b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td); 948797f2d22SPoul-Henning Kamp error = namei(&nd); 949797f2d22SPoul-Henning Kamp if (error) 9500d9ce3a1SRobert Watson vp = NULL; 9510d9ce3a1SRobert Watson else 952df8bae1dSRodney W. Grimes vp = nd.ni_vp; 9530d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_connect"); 954762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 9550d9ce3a1SRobert Watson if (error) 9560d9ce3a1SRobert Watson goto bad; 9570d9ce3a1SRobert Watson 958df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 959df8bae1dSRodney W. Grimes error = ENOTSOCK; 960df8bae1dSRodney W. Grimes goto bad; 961df8bae1dSRodney W. Grimes } 962a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 963797f2d22SPoul-Henning Kamp if (error) 964df8bae1dSRodney W. Grimes goto bad; 9652260c03dSRobert Watson mtx_unlock(&Giant); 9662260c03dSRobert Watson UNP_LOCK(); 967b295bdcdSRobert Watson unp = sotounpcb(so); 9684d4b555eSRobert Watson KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 969df8bae1dSRodney W. Grimes so2 = vp->v_socket; 970fc3fcacfSRobert Watson if (so2 == NULL) { 971df8bae1dSRodney W. Grimes error = ECONNREFUSED; 9722260c03dSRobert Watson goto bad2; 973df8bae1dSRodney W. Grimes } 974df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 975df8bae1dSRodney W. Grimes error = EPROTOTYPE; 9762260c03dSRobert Watson goto bad2; 977df8bae1dSRodney W. Grimes } 978df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 9790d9ce3a1SRobert Watson if (so2->so_options & SO_ACCEPTCONN) { 9800d9ce3a1SRobert Watson /* 9811c381b19SRobert Watson * NB: drop locks here so unp_attach is entered w/o 9821c381b19SRobert Watson * locks; this avoids a recursive lock of the head 9831c381b19SRobert Watson * and holding sleep locks across a (potentially) 9841c381b19SRobert Watson * blocking malloc. 9850d9ce3a1SRobert Watson */ 9860d9ce3a1SRobert Watson UNP_UNLOCK(); 9870d9ce3a1SRobert Watson so3 = sonewconn(so2, 0); 9880d9ce3a1SRobert Watson UNP_LOCK(); 9890d9ce3a1SRobert Watson } else 9900d9ce3a1SRobert Watson so3 = NULL; 9910d9ce3a1SRobert Watson if (so3 == NULL) { 992df8bae1dSRodney W. Grimes error = ECONNREFUSED; 9930d9ce3a1SRobert Watson goto bad2; 994df8bae1dSRodney W. Grimes } 9950c1bb4fbSDima Dorfman unp = sotounpcb(so); 996df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 997df8bae1dSRodney W. Grimes unp3 = sotounpcb(so3); 9980d9ce3a1SRobert Watson if (unp2->unp_addr != NULL) { 9990d9ce3a1SRobert Watson bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 10000d9ce3a1SRobert Watson unp3->unp_addr = (struct sockaddr_un *) sa; 10010d9ce3a1SRobert Watson sa = NULL; 10020d9ce3a1SRobert Watson } 10030c1bb4fbSDima Dorfman /* 10040c1bb4fbSDima Dorfman * unp_peercred management: 10050c1bb4fbSDima Dorfman * 10061c381b19SRobert Watson * The connecter's (client's) credentials are copied from its 10071c381b19SRobert Watson * process structure at the time of connect() (which is now). 10080c1bb4fbSDima Dorfman */ 1009a854ed98SJohn Baldwin cru2x(td->td_ucred, &unp3->unp_peercred); 10100c1bb4fbSDima Dorfman unp3->unp_flags |= UNP_HAVEPC; 10110c1bb4fbSDima Dorfman /* 10121c381b19SRobert Watson * The receiver's (server's) credentials are copied from the 10131c381b19SRobert Watson * unp_peercred member of socket on which the former called 10141c381b19SRobert Watson * listen(); unp_listen() cached that process's credentials 10151c381b19SRobert Watson * at that time so we can use them now. 10160c1bb4fbSDima Dorfman */ 10170c1bb4fbSDima Dorfman KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 10180c1bb4fbSDima Dorfman ("unp_connect: listener without cached peercred")); 10190c1bb4fbSDima Dorfman memcpy(&unp->unp_peercred, &unp2->unp_peercred, 10200c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 10210c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPC; 1022481f8fe8SMaxim Konovalov if (unp2->unp_flags & UNP_WANTCRED) 1023481f8fe8SMaxim Konovalov unp3->unp_flags |= UNP_WANTCRED; 1024335654d7SRobert Watson #ifdef MAC 1025310e7cebSRobert Watson SOCK_LOCK(so); 1026335654d7SRobert Watson mac_set_socket_peer_from_socket(so, so3); 1027335654d7SRobert Watson mac_set_socket_peer_from_socket(so3, so); 1028310e7cebSRobert Watson SOCK_UNLOCK(so); 1029335654d7SRobert Watson #endif 10300c1bb4fbSDima Dorfman 1031df8bae1dSRodney W. Grimes so2 = so3; 1032df8bae1dSRodney W. Grimes } 10336a2989fdSMatthew N. Dodd error = unp_connect2(so, so2, PRU_CONNECT); 10340d9ce3a1SRobert Watson bad2: 10350d9ce3a1SRobert Watson UNP_UNLOCK(); 10360d9ce3a1SRobert Watson mtx_lock(&Giant); 1037df8bae1dSRodney W. Grimes bad: 10380d9ce3a1SRobert Watson mtx_assert(&Giant, MA_OWNED); 10390d9ce3a1SRobert Watson if (vp != NULL) 1040df8bae1dSRodney W. Grimes vput(vp); 10410d9ce3a1SRobert Watson mtx_unlock(&Giant); 10420d9ce3a1SRobert Watson free(sa, M_SONAME); 10430d9ce3a1SRobert Watson UNP_LOCK(); 10444f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_CONNECTING; 1045df8bae1dSRodney W. Grimes return (error); 1046df8bae1dSRodney W. Grimes } 1047df8bae1dSRodney W. Grimes 1048db48c0d2SRobert Watson static int 10496a2989fdSMatthew N. Dodd unp_connect2(struct socket *so, struct socket *so2, int req) 1050df8bae1dSRodney W. Grimes { 1051892af6b9SRobert Watson struct unpcb *unp = sotounpcb(so); 1052892af6b9SRobert Watson struct unpcb *unp2; 1053df8bae1dSRodney W. Grimes 10540d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 10550d9ce3a1SRobert Watson 1056df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 1057df8bae1dSRodney W. Grimes return (EPROTOTYPE); 1058df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 10594d4b555eSRobert Watson KASSERT(unp2 != NULL, ("unp_connect2: unp2 == NULL")); 1060df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 1061df8bae1dSRodney W. Grimes switch (so->so_type) { 1062df8bae1dSRodney W. Grimes case SOCK_DGRAM: 106398271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 1064df8bae1dSRodney W. Grimes soisconnected(so); 1065df8bae1dSRodney W. Grimes break; 1066df8bae1dSRodney W. Grimes 1067df8bae1dSRodney W. Grimes case SOCK_STREAM: 1068df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 10696a2989fdSMatthew N. Dodd if (req == PRU_CONNECT && 10706a2989fdSMatthew N. Dodd ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT)) 10716a2989fdSMatthew N. Dodd soisconnecting(so); 10726a2989fdSMatthew N. Dodd else 1073df8bae1dSRodney W. Grimes soisconnected(so); 1074df8bae1dSRodney W. Grimes soisconnected(so2); 1075df8bae1dSRodney W. Grimes break; 1076df8bae1dSRodney W. Grimes 1077df8bae1dSRodney W. Grimes default: 1078df8bae1dSRodney W. Grimes panic("unp_connect2"); 1079df8bae1dSRodney W. Grimes } 1080df8bae1dSRodney W. Grimes return (0); 1081df8bae1dSRodney W. Grimes } 1082df8bae1dSRodney W. Grimes 1083f708ef1bSPoul-Henning Kamp static void 1084892af6b9SRobert Watson unp_disconnect(struct unpcb *unp) 1085df8bae1dSRodney W. Grimes { 1086892af6b9SRobert Watson struct unpcb *unp2 = unp->unp_conn; 10871b2e3b4bSRobert Watson struct socket *so; 1088df8bae1dSRodney W. Grimes 10890d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 10900d9ce3a1SRobert Watson 1091fc3fcacfSRobert Watson if (unp2 == NULL) 1092df8bae1dSRodney W. Grimes return; 1093fc3fcacfSRobert Watson unp->unp_conn = NULL; 1094df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 1095df8bae1dSRodney W. Grimes case SOCK_DGRAM: 109698271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 10971b2e3b4bSRobert Watson so = unp->unp_socket; 10981b2e3b4bSRobert Watson SOCK_LOCK(so); 10991b2e3b4bSRobert Watson so->so_state &= ~SS_ISCONNECTED; 11001b2e3b4bSRobert Watson SOCK_UNLOCK(so); 1101df8bae1dSRodney W. Grimes break; 1102df8bae1dSRodney W. Grimes 1103df8bae1dSRodney W. Grimes case SOCK_STREAM: 1104df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 1105fc3fcacfSRobert Watson unp2->unp_conn = NULL; 1106df8bae1dSRodney W. Grimes soisdisconnected(unp2->unp_socket); 1107df8bae1dSRodney W. Grimes break; 1108df8bae1dSRodney W. Grimes } 1109df8bae1dSRodney W. Grimes } 1110df8bae1dSRodney W. Grimes 11110d9ce3a1SRobert Watson /* 11121c381b19SRobert Watson * unp_pcblist() assumes that UNIX domain socket memory is never reclaimed by 11131c381b19SRobert Watson * the zone (UMA_ZONE_NOFREE), and as such potentially stale pointers are 11141c381b19SRobert Watson * safe to reference. It first scans the list of struct unpcb's to generate 11151c381b19SRobert Watson * a pointer list, then it rescans its list one entry at a time to 11160d9ce3a1SRobert Watson * externalize and copyout. It checks the generation number to see if a 11170d9ce3a1SRobert Watson * struct unpcb has been reused, and will skip it if so. 11180d9ce3a1SRobert Watson */ 111998271db4SGarrett Wollman static int 112082d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 112198271db4SGarrett Wollman { 1122f5ef029eSPoul-Henning Kamp int error, i, n; 112398271db4SGarrett Wollman struct unpcb *unp, **unp_list; 112498271db4SGarrett Wollman unp_gen_t gencnt; 11258f364875SJulian Elischer struct xunpgen *xug; 112698271db4SGarrett Wollman struct unp_head *head; 11278f364875SJulian Elischer struct xunpcb *xu; 112898271db4SGarrett Wollman 1129a23d65bfSBruce Evans head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 113098271db4SGarrett Wollman 113198271db4SGarrett Wollman /* 113298271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 113398271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 113498271db4SGarrett Wollman */ 1135fc3fcacfSRobert Watson if (req->oldptr == NULL) { 113698271db4SGarrett Wollman n = unp_count; 11378f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug) 113898271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 1139e5aeaa0cSDag-Erling Smørgrav return (0); 114098271db4SGarrett Wollman } 114198271db4SGarrett Wollman 1142fc3fcacfSRobert Watson if (req->newptr != NULL) 1143e5aeaa0cSDag-Erling Smørgrav return (EPERM); 114498271db4SGarrett Wollman 114598271db4SGarrett Wollman /* 114698271db4SGarrett Wollman * OK, now we're committed to doing something. 114798271db4SGarrett Wollman */ 1148a163d034SWarner Losh xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 11490d9ce3a1SRobert Watson UNP_LOCK(); 115098271db4SGarrett Wollman gencnt = unp_gencnt; 115198271db4SGarrett Wollman n = unp_count; 11520d9ce3a1SRobert Watson UNP_UNLOCK(); 115398271db4SGarrett Wollman 11548f364875SJulian Elischer xug->xug_len = sizeof *xug; 11558f364875SJulian Elischer xug->xug_count = n; 11568f364875SJulian Elischer xug->xug_gen = gencnt; 11578f364875SJulian Elischer xug->xug_sogen = so_gencnt; 11588f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 11598f364875SJulian Elischer if (error) { 11608f364875SJulian Elischer free(xug, M_TEMP); 1161e5aeaa0cSDag-Erling Smørgrav return (error); 11628f364875SJulian Elischer } 116398271db4SGarrett Wollman 1164a163d034SWarner Losh unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 116598271db4SGarrett Wollman 11660d9ce3a1SRobert Watson UNP_LOCK(); 11672e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 11682e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 11698a7d8cc6SRobert Watson if (unp->unp_gencnt <= gencnt) { 1170a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred, 11718a7d8cc6SRobert Watson unp->unp_socket->so_cred)) 11724787fd37SPaul Saab continue; 117398271db4SGarrett Wollman unp_list[i++] = unp; 117498271db4SGarrett Wollman } 11754787fd37SPaul Saab } 11760d9ce3a1SRobert Watson UNP_UNLOCK(); 11771c381b19SRobert Watson n = i; /* In case we lost some during malloc. */ 117898271db4SGarrett Wollman 117998271db4SGarrett Wollman error = 0; 1180fe2eee82SColin Percival xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO); 118198271db4SGarrett Wollman for (i = 0; i < n; i++) { 118298271db4SGarrett Wollman unp = unp_list[i]; 118398271db4SGarrett Wollman if (unp->unp_gencnt <= gencnt) { 11848f364875SJulian Elischer xu->xu_len = sizeof *xu; 11858f364875SJulian Elischer xu->xu_unpp = unp; 118698271db4SGarrett Wollman /* 118798271db4SGarrett Wollman * XXX - need more locking here to protect against 118898271db4SGarrett Wollman * connect/disconnect races for SMP. 118998271db4SGarrett Wollman */ 1190fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 11918f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr, 119298271db4SGarrett Wollman unp->unp_addr->sun_len); 1193fc3fcacfSRobert Watson if (unp->unp_conn != NULL && 1194fc3fcacfSRobert Watson unp->unp_conn->unp_addr != NULL) 119598271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 11968f364875SJulian Elischer &xu->xu_caddr, 119798271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 11988f364875SJulian Elischer bcopy(unp, &xu->xu_unp, sizeof *unp); 11998f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket); 12008f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu); 120198271db4SGarrett Wollman } 120298271db4SGarrett Wollman } 12038f364875SJulian Elischer free(xu, M_TEMP); 120498271db4SGarrett Wollman if (!error) { 120598271db4SGarrett Wollman /* 12061c381b19SRobert Watson * Give the user an updated idea of our state. If the 12071c381b19SRobert Watson * generation differs from what we told her before, she knows 12081c381b19SRobert Watson * that something happened while we were processing this 12091c381b19SRobert Watson * request, and it might be necessary to retry. 121098271db4SGarrett Wollman */ 12118f364875SJulian Elischer xug->xug_gen = unp_gencnt; 12128f364875SJulian Elischer xug->xug_sogen = so_gencnt; 12138f364875SJulian Elischer xug->xug_count = unp_count; 12148f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 121598271db4SGarrett Wollman } 121698271db4SGarrett Wollman free(unp_list, M_TEMP); 12178f364875SJulian Elischer free(xug, M_TEMP); 1218e5aeaa0cSDag-Erling Smørgrav return (error); 121998271db4SGarrett Wollman } 122098271db4SGarrett Wollman 122198271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 122298271db4SGarrett Wollman (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 122398271db4SGarrett Wollman "List of active local datagram sockets"); 122498271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 122598271db4SGarrett Wollman (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 122698271db4SGarrett Wollman "List of active local stream sockets"); 122798271db4SGarrett Wollman 1228f708ef1bSPoul-Henning Kamp static void 1229892af6b9SRobert Watson unp_shutdown(struct unpcb *unp) 1230df8bae1dSRodney W. Grimes { 1231df8bae1dSRodney W. Grimes struct socket *so; 1232df8bae1dSRodney W. Grimes 12330d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 12340d9ce3a1SRobert Watson 1235df8bae1dSRodney W. Grimes if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 1236df8bae1dSRodney W. Grimes (so = unp->unp_conn->unp_socket)) 1237df8bae1dSRodney W. Grimes socantrcvmore(so); 1238df8bae1dSRodney W. Grimes } 1239df8bae1dSRodney W. Grimes 1240f708ef1bSPoul-Henning Kamp static void 1241892af6b9SRobert Watson unp_drop(struct unpcb *unp, int errno) 1242df8bae1dSRodney W. Grimes { 1243df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 1244df8bae1dSRodney W. Grimes 12450d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 12460d9ce3a1SRobert Watson 1247df8bae1dSRodney W. Grimes so->so_error = errno; 1248df8bae1dSRodney W. Grimes unp_disconnect(unp); 1249df8bae1dSRodney W. Grimes } 1250df8bae1dSRodney W. Grimes 12512bc21ed9SDavid Malone static void 1252892af6b9SRobert Watson unp_freerights(struct file **rp, int fdcount) 1253df8bae1dSRodney W. Grimes { 12542bc21ed9SDavid Malone int i; 12552bc21ed9SDavid Malone struct file *fp; 1256df8bae1dSRodney W. Grimes 12572bc21ed9SDavid Malone for (i = 0; i < fdcount; i++) { 1258df8bae1dSRodney W. Grimes fp = *rp; 12598692c025SYoshinobu Inoue /* 12601c381b19SRobert Watson * Zero the pointer before calling unp_discard since it may 12611c381b19SRobert Watson * end up in unp_gc().. 1262d7dca903SRobert Watson * 1263d7dca903SRobert Watson * XXXRW: This is less true than it used to be. 12648692c025SYoshinobu Inoue */ 1265df8bae1dSRodney W. Grimes *rp++ = 0; 12668692c025SYoshinobu Inoue unp_discard(fp); 1267df8bae1dSRodney W. Grimes } 12682bc21ed9SDavid Malone } 12692bc21ed9SDavid Malone 12702bc21ed9SDavid Malone int 1271892af6b9SRobert Watson unp_externalize(struct mbuf *control, struct mbuf **controlp) 12722bc21ed9SDavid Malone { 12732bc21ed9SDavid Malone struct thread *td = curthread; /* XXX */ 12742bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 12752bc21ed9SDavid Malone int i; 12762bc21ed9SDavid Malone int *fdp; 12772bc21ed9SDavid Malone struct file **rp; 12782bc21ed9SDavid Malone struct file *fp; 12792bc21ed9SDavid Malone void *data; 12802bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 12812bc21ed9SDavid Malone int error, newfds; 12822bc21ed9SDavid Malone int f; 12832bc21ed9SDavid Malone u_int newlen; 12842bc21ed9SDavid Malone 12854c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 12864c5bc1caSRobert Watson 12872bc21ed9SDavid Malone error = 0; 12882bc21ed9SDavid Malone if (controlp != NULL) /* controlp == NULL => free control messages */ 12892bc21ed9SDavid Malone *controlp = NULL; 12902bc21ed9SDavid Malone 12912bc21ed9SDavid Malone while (cm != NULL) { 12922bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 12932bc21ed9SDavid Malone error = EINVAL; 12942bc21ed9SDavid Malone break; 12952bc21ed9SDavid Malone } 12962bc21ed9SDavid Malone 12972bc21ed9SDavid Malone data = CMSG_DATA(cm); 12982bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 12992bc21ed9SDavid Malone 13002bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET 13012bc21ed9SDavid Malone && cm->cmsg_type == SCM_RIGHTS) { 13022bc21ed9SDavid Malone newfds = datalen / sizeof(struct file *); 13032bc21ed9SDavid Malone rp = data; 13042bc21ed9SDavid Malone 1305e2f9a08bSOlivier Houchard /* If we're not outputting the descriptors free them. */ 13062bc21ed9SDavid Malone if (error || controlp == NULL) { 13072bc21ed9SDavid Malone unp_freerights(rp, newfds); 13082bc21ed9SDavid Malone goto next; 13092bc21ed9SDavid Malone } 1310426da3bcSAlfred Perlstein FILEDESC_LOCK(td->td_proc->p_fd); 13112bc21ed9SDavid Malone /* if the new FD's will not fit free them. */ 13122bc21ed9SDavid Malone if (!fdavail(td, newfds)) { 1313426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 13142bc21ed9SDavid Malone error = EMSGSIZE; 13152bc21ed9SDavid Malone unp_freerights(rp, newfds); 13162bc21ed9SDavid Malone goto next; 1317df8bae1dSRodney W. Grimes } 1318ed5b7817SJulian Elischer /* 13191c381b19SRobert Watson * Now change each pointer to an fd in the global 13201c381b19SRobert Watson * table to an integer that is the index to the local 13211c381b19SRobert Watson * fd table entry that we set up to point to the 13221c381b19SRobert Watson * global one we are transferring. 1323ed5b7817SJulian Elischer */ 13242bc21ed9SDavid Malone newlen = newfds * sizeof(int); 13252bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 13262bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 13272bc21ed9SDavid Malone if (*controlp == NULL) { 1328426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 13292bc21ed9SDavid Malone error = E2BIG; 13302bc21ed9SDavid Malone unp_freerights(rp, newfds); 13312bc21ed9SDavid Malone goto next; 13322bc21ed9SDavid Malone } 13332bc21ed9SDavid Malone 13342bc21ed9SDavid Malone fdp = (int *) 13352bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1336df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 1337a6d4491cSDag-Erling Smørgrav if (fdalloc(td, 0, &f)) 13382bc21ed9SDavid Malone panic("unp_externalize fdalloc failed"); 13398692c025SYoshinobu Inoue fp = *rp++; 1340b40ce416SJulian Elischer td->td_proc->p_fd->fd_ofiles[f] = fp; 1341426da3bcSAlfred Perlstein FILE_LOCK(fp); 1342df8bae1dSRodney W. Grimes fp->f_msgcount--; 1343426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1344df8bae1dSRodney W. Grimes unp_rights--; 13458692c025SYoshinobu Inoue *fdp++ = f; 1346df8bae1dSRodney W. Grimes } 1347426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 13481c381b19SRobert Watson } else { 13491c381b19SRobert Watson /* We can just copy anything else across. */ 13502bc21ed9SDavid Malone if (error || controlp == NULL) 13512bc21ed9SDavid Malone goto next; 13522bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, datalen, 13532bc21ed9SDavid Malone cm->cmsg_type, cm->cmsg_level); 13542bc21ed9SDavid Malone if (*controlp == NULL) { 13552bc21ed9SDavid Malone error = ENOBUFS; 13562bc21ed9SDavid Malone goto next; 13572bc21ed9SDavid Malone } 13582bc21ed9SDavid Malone bcopy(data, 13592bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 13602bc21ed9SDavid Malone datalen); 13612bc21ed9SDavid Malone } 13622bc21ed9SDavid Malone 13632bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 13642bc21ed9SDavid Malone 13652bc21ed9SDavid Malone next: 13662bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 13672bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 13682bc21ed9SDavid Malone cm = (struct cmsghdr *) 13692bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 13708692c025SYoshinobu Inoue } else { 13712bc21ed9SDavid Malone clen = 0; 13722bc21ed9SDavid Malone cm = NULL; 13738692c025SYoshinobu Inoue } 13748692c025SYoshinobu Inoue } 13758692c025SYoshinobu Inoue 13762bc21ed9SDavid Malone m_freem(control); 13772bc21ed9SDavid Malone 13782bc21ed9SDavid Malone return (error); 1379df8bae1dSRodney W. Grimes } 1380df8bae1dSRodney W. Grimes 13814f590175SPaul Saab static void 13824f590175SPaul Saab unp_zone_change(void *tag) 13834f590175SPaul Saab { 13844f590175SPaul Saab 13854f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets); 13864f590175SPaul Saab } 13874f590175SPaul Saab 138898271db4SGarrett Wollman void 138998271db4SGarrett Wollman unp_init(void) 139098271db4SGarrett Wollman { 13911c381b19SRobert Watson 13929e9d298aSJeff Roberson unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 13939e9d298aSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 1394fc3fcacfSRobert Watson if (unp_zone == NULL) 139598271db4SGarrett Wollman panic("unp_init"); 13964f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets); 13974f590175SPaul Saab EVENTHANDLER_REGISTER(maxsockets_change, unp_zone_change, 13984f590175SPaul Saab NULL, EVENTHANDLER_PRI_ANY); 139998271db4SGarrett Wollman LIST_INIT(&unp_dhead); 140098271db4SGarrett Wollman LIST_INIT(&unp_shead); 1401a0ec558aSRobert Watson TASK_INIT(&unp_gc_task, 0, unp_gc, NULL); 14020d9ce3a1SRobert Watson UNP_LOCK_INIT(); 140398271db4SGarrett Wollman } 140498271db4SGarrett Wollman 1405f708ef1bSPoul-Henning Kamp static int 1406892af6b9SRobert Watson unp_internalize(struct mbuf **controlp, struct thread *td) 1407df8bae1dSRodney W. Grimes { 14082bc21ed9SDavid Malone struct mbuf *control = *controlp; 1409b40ce416SJulian Elischer struct proc *p = td->td_proc; 14108692c025SYoshinobu Inoue struct filedesc *fdescp = p->p_fd; 14112bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 14122bc21ed9SDavid Malone struct cmsgcred *cmcred; 14132bc21ed9SDavid Malone struct file **rp; 14142bc21ed9SDavid Malone struct file *fp; 14152bc21ed9SDavid Malone struct timeval *tv; 14162bc21ed9SDavid Malone int i, fd, *fdp; 14172bc21ed9SDavid Malone void *data; 14182bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 14192bc21ed9SDavid Malone int error, oldfds; 14208692c025SYoshinobu Inoue u_int newlen; 1421df8bae1dSRodney W. Grimes 14224c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 14234c5bc1caSRobert Watson 14242bc21ed9SDavid Malone error = 0; 14252bc21ed9SDavid Malone *controlp = NULL; 14260b788fa1SBill Paul 14272bc21ed9SDavid Malone while (cm != NULL) { 14282bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 14292bc21ed9SDavid Malone || cm->cmsg_len > clen) { 14302bc21ed9SDavid Malone error = EINVAL; 14312bc21ed9SDavid Malone goto out; 14322bc21ed9SDavid Malone } 14332bc21ed9SDavid Malone 14342bc21ed9SDavid Malone data = CMSG_DATA(cm); 14352bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 14362bc21ed9SDavid Malone 14372bc21ed9SDavid Malone switch (cm->cmsg_type) { 14380b788fa1SBill Paul /* 14390b788fa1SBill Paul * Fill in credential information. 14400b788fa1SBill Paul */ 14412bc21ed9SDavid Malone case SCM_CREDS: 14422bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 14432bc21ed9SDavid Malone SCM_CREDS, SOL_SOCKET); 14442bc21ed9SDavid Malone if (*controlp == NULL) { 14452bc21ed9SDavid Malone error = ENOBUFS; 14462bc21ed9SDavid Malone goto out; 14472bc21ed9SDavid Malone } 14482bc21ed9SDavid Malone 14492bc21ed9SDavid Malone cmcred = (struct cmsgcred *) 14502bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 14510b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 1452a854ed98SJohn Baldwin cmcred->cmcred_uid = td->td_ucred->cr_ruid; 1453a854ed98SJohn Baldwin cmcred->cmcred_gid = td->td_ucred->cr_rgid; 1454a854ed98SJohn Baldwin cmcred->cmcred_euid = td->td_ucred->cr_uid; 1455a854ed98SJohn Baldwin cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 14560b788fa1SBill Paul CMGROUP_MAX); 14570b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 14582bc21ed9SDavid Malone cmcred->cmcred_groups[i] = 1459a854ed98SJohn Baldwin td->td_ucred->cr_groups[i]; 14602bc21ed9SDavid Malone break; 14610b788fa1SBill Paul 14622bc21ed9SDavid Malone case SCM_RIGHTS: 14632bc21ed9SDavid Malone oldfds = datalen / sizeof (int); 1464ed5b7817SJulian Elischer /* 14651c381b19SRobert Watson * Check that all the FDs passed in refer to legal 14661c381b19SRobert Watson * files. If not, reject the entire operation. 1467ed5b7817SJulian Elischer */ 14682bc21ed9SDavid Malone fdp = data; 1469426da3bcSAlfred Perlstein FILEDESC_LOCK(fdescp); 1470df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 14718692c025SYoshinobu Inoue fd = *fdp++; 14728692c025SYoshinobu Inoue if ((unsigned)fd >= fdescp->fd_nfiles || 14732bc21ed9SDavid Malone fdescp->fd_ofiles[fd] == NULL) { 1474426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 14752bc21ed9SDavid Malone error = EBADF; 14762bc21ed9SDavid Malone goto out; 14772bc21ed9SDavid Malone } 1478e7d6662fSAlfred Perlstein fp = fdescp->fd_ofiles[fd]; 1479e7d6662fSAlfred Perlstein if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 1480e7d6662fSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 1481e7d6662fSAlfred Perlstein error = EOPNOTSUPP; 1482e7d6662fSAlfred Perlstein goto out; 1483e7d6662fSAlfred Perlstein } 1484e7d6662fSAlfred Perlstein 1485df8bae1dSRodney W. Grimes } 1486ed5b7817SJulian Elischer /* 14871c381b19SRobert Watson * Now replace the integer FDs with pointers to the 14881c381b19SRobert Watson * associated global file table entry.. 1489ed5b7817SJulian Elischer */ 14902bc21ed9SDavid Malone newlen = oldfds * sizeof(struct file *); 14912bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 14922bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 14932bc21ed9SDavid Malone if (*controlp == NULL) { 1494426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 14952bc21ed9SDavid Malone error = E2BIG; 14962bc21ed9SDavid Malone goto out; 14978692c025SYoshinobu Inoue } 14988692c025SYoshinobu Inoue 14992bc21ed9SDavid Malone fdp = data; 15002bc21ed9SDavid Malone rp = (struct file **) 15012bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 15028692c025SYoshinobu Inoue for (i = 0; i < oldfds; i++) { 15038692c025SYoshinobu Inoue fp = fdescp->fd_ofiles[*fdp++]; 1504df8bae1dSRodney W. Grimes *rp++ = fp; 1505426da3bcSAlfred Perlstein FILE_LOCK(fp); 1506df8bae1dSRodney W. Grimes fp->f_count++; 1507df8bae1dSRodney W. Grimes fp->f_msgcount++; 1508426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1509df8bae1dSRodney W. Grimes unp_rights++; 1510df8bae1dSRodney W. Grimes } 1511426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 15122bc21ed9SDavid Malone break; 15132bc21ed9SDavid Malone 15142bc21ed9SDavid Malone case SCM_TIMESTAMP: 15152bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*tv), 15162bc21ed9SDavid Malone SCM_TIMESTAMP, SOL_SOCKET); 15172bc21ed9SDavid Malone if (*controlp == NULL) { 15182bc21ed9SDavid Malone error = ENOBUFS; 15192bc21ed9SDavid Malone goto out; 15208692c025SYoshinobu Inoue } 15212bc21ed9SDavid Malone tv = (struct timeval *) 15222bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 15232bc21ed9SDavid Malone microtime(tv); 15242bc21ed9SDavid Malone break; 15252bc21ed9SDavid Malone 15262bc21ed9SDavid Malone default: 15272bc21ed9SDavid Malone error = EINVAL; 15282bc21ed9SDavid Malone goto out; 15292bc21ed9SDavid Malone } 15302bc21ed9SDavid Malone 15312bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 15322bc21ed9SDavid Malone 15332bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 15342bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 15352bc21ed9SDavid Malone cm = (struct cmsghdr *) 15362bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 15372bc21ed9SDavid Malone } else { 15382bc21ed9SDavid Malone clen = 0; 15392bc21ed9SDavid Malone cm = NULL; 15402bc21ed9SDavid Malone } 15412bc21ed9SDavid Malone } 15422bc21ed9SDavid Malone 15432bc21ed9SDavid Malone out: 15442bc21ed9SDavid Malone m_freem(control); 15452bc21ed9SDavid Malone 15462bc21ed9SDavid Malone return (error); 1547df8bae1dSRodney W. Grimes } 1548df8bae1dSRodney W. Grimes 15496a2989fdSMatthew N. Dodd struct mbuf * 15506a2989fdSMatthew N. Dodd unp_addsockcred(struct thread *td, struct mbuf *control) 15516a2989fdSMatthew N. Dodd { 155270df31f4SMaxim Konovalov struct mbuf *m, *n, *n_prev; 15536a2989fdSMatthew N. Dodd struct sockcred *sc; 155470df31f4SMaxim Konovalov const struct cmsghdr *cm; 15556a2989fdSMatthew N. Dodd int ngroups; 15566a2989fdSMatthew N. Dodd int i; 15576a2989fdSMatthew N. Dodd 15586a2989fdSMatthew N. Dodd ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX); 15596a2989fdSMatthew N. Dodd 15606a2989fdSMatthew N. Dodd m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET); 15616a2989fdSMatthew N. Dodd if (m == NULL) 15626a2989fdSMatthew N. Dodd return (control); 15636a2989fdSMatthew N. Dodd 15646a2989fdSMatthew N. Dodd sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *)); 15656a2989fdSMatthew N. Dodd sc->sc_uid = td->td_ucred->cr_ruid; 15666a2989fdSMatthew N. Dodd sc->sc_euid = td->td_ucred->cr_uid; 15676a2989fdSMatthew N. Dodd sc->sc_gid = td->td_ucred->cr_rgid; 15686a2989fdSMatthew N. Dodd sc->sc_egid = td->td_ucred->cr_gid; 15696a2989fdSMatthew N. Dodd sc->sc_ngroups = ngroups; 15706a2989fdSMatthew N. Dodd for (i = 0; i < sc->sc_ngroups; i++) 15716a2989fdSMatthew N. Dodd sc->sc_groups[i] = td->td_ucred->cr_groups[i]; 15726a2989fdSMatthew N. Dodd 15736a2989fdSMatthew N. Dodd /* 15741c381b19SRobert Watson * Unlink SCM_CREDS control messages (struct cmsgcred), since just 15751c381b19SRobert Watson * created SCM_CREDS control message (struct sockcred) has another 15761c381b19SRobert Watson * format. 15776a2989fdSMatthew N. Dodd */ 157870df31f4SMaxim Konovalov if (control != NULL) 157970df31f4SMaxim Konovalov for (n = control, n_prev = NULL; n != NULL;) { 158070df31f4SMaxim Konovalov cm = mtod(n, struct cmsghdr *); 158170df31f4SMaxim Konovalov if (cm->cmsg_level == SOL_SOCKET && 158270df31f4SMaxim Konovalov cm->cmsg_type == SCM_CREDS) { 158370df31f4SMaxim Konovalov if (n_prev == NULL) 158470df31f4SMaxim Konovalov control = n->m_next; 158570df31f4SMaxim Konovalov else 158670df31f4SMaxim Konovalov n_prev->m_next = n->m_next; 158770df31f4SMaxim Konovalov n = m_free(n); 158870df31f4SMaxim Konovalov } else { 158970df31f4SMaxim Konovalov n_prev = n; 159070df31f4SMaxim Konovalov n = n->m_next; 159170df31f4SMaxim Konovalov } 159270df31f4SMaxim Konovalov } 15936a2989fdSMatthew N. Dodd 159470df31f4SMaxim Konovalov /* Prepend it to the head. */ 159570df31f4SMaxim Konovalov m->m_next = control; 159670df31f4SMaxim Konovalov 159770df31f4SMaxim Konovalov return (m); 15986a2989fdSMatthew N. Dodd } 15996a2989fdSMatthew N. Dodd 1600161a0c7cSRobert Watson /* 1601a0ec558aSRobert Watson * unp_defer indicates whether additional work has been defered for a future 1602a0ec558aSRobert Watson * pass through unp_gc(). It is thread local and does not require explicit 1603a0ec558aSRobert Watson * synchronization. 1604161a0c7cSRobert Watson */ 1605a0ec558aSRobert Watson static int unp_defer; 1606a0ec558aSRobert Watson 1607a0ec558aSRobert Watson static int unp_taskcount; 1608a0ec558aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0, ""); 1609a0ec558aSRobert Watson 1610a0ec558aSRobert Watson static int unp_recycled; 1611a0ec558aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0, ""); 1612df8bae1dSRodney W. Grimes 1613f708ef1bSPoul-Henning Kamp static void 1614a0ec558aSRobert Watson unp_gc(__unused void *arg, int pending) 1615df8bae1dSRodney W. Grimes { 1616892af6b9SRobert Watson struct file *fp, *nextfp; 1617892af6b9SRobert Watson struct socket *so; 1618df8bae1dSRodney W. Grimes struct file **extra_ref, **fpp; 1619df8bae1dSRodney W. Grimes int nunref, i; 162095f004dcSAlfred Perlstein int nfiles_snap; 162195f004dcSAlfred Perlstein int nfiles_slack = 20; 1622df8bae1dSRodney W. Grimes 1623a0ec558aSRobert Watson unp_taskcount++; 1624df8bae1dSRodney W. Grimes unp_defer = 0; 1625ed5b7817SJulian Elischer /* 16261c381b19SRobert Watson * Before going through all this, set all FDs to be NOT defered and 16271c381b19SRobert Watson * NOT externally accessible. 1628ed5b7817SJulian Elischer */ 1629426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 16302e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) 1631426da3bcSAlfred Perlstein fp->f_gcflag &= ~(FMARK|FDEFER); 1632df8bae1dSRodney W. Grimes do { 16335bb84bc8SRobert Watson KASSERT(unp_defer >= 0, ("unp_gc: unp_defer %d", unp_defer)); 16342e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) { 1635426da3bcSAlfred Perlstein FILE_LOCK(fp); 1636ed5b7817SJulian Elischer /* 1637a0ec558aSRobert Watson * If the file is not open, skip it -- could be a 1638a0ec558aSRobert Watson * file in the process of being opened, or in the 1639a0ec558aSRobert Watson * process of being closed. If the file is 1640a0ec558aSRobert Watson * "closing", it may have been marked for deferred 1641a0ec558aSRobert Watson * consideration. Clear the flag now if so. 1642ed5b7817SJulian Elischer */ 1643426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1644a0ec558aSRobert Watson if (fp->f_gcflag & FDEFER) 1645a0ec558aSRobert Watson unp_defer--; 1646a0ec558aSRobert Watson fp->f_gcflag &= ~(FMARK|FDEFER); 1647426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1648df8bae1dSRodney W. Grimes continue; 1649426da3bcSAlfred Perlstein } 1650ed5b7817SJulian Elischer /* 16511c381b19SRobert Watson * If we already marked it as 'defer' in a previous 16521c381b19SRobert Watson * pass, then try process it this time and un-mark 16531c381b19SRobert Watson * it. 1654ed5b7817SJulian Elischer */ 1655426da3bcSAlfred Perlstein if (fp->f_gcflag & FDEFER) { 1656426da3bcSAlfred Perlstein fp->f_gcflag &= ~FDEFER; 1657df8bae1dSRodney W. Grimes unp_defer--; 1658df8bae1dSRodney W. Grimes } else { 1659ed5b7817SJulian Elischer /* 1660ed5b7817SJulian Elischer * if it's not defered, then check if it's 1661ed5b7817SJulian Elischer * already marked.. if so skip it 1662ed5b7817SJulian Elischer */ 1663426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) { 1664426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1665df8bae1dSRodney W. Grimes continue; 1666426da3bcSAlfred Perlstein } 1667ed5b7817SJulian Elischer /* 16681c381b19SRobert Watson * If all references are from messages in 16691c381b19SRobert Watson * transit, then skip it. it's not externally 16701c381b19SRobert Watson * accessible. 1671ed5b7817SJulian Elischer */ 1672426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount) { 1673426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1674df8bae1dSRodney W. Grimes continue; 1675426da3bcSAlfred Perlstein } 1676ed5b7817SJulian Elischer /* 1677ed5b7817SJulian Elischer * If it got this far then it must be 1678ed5b7817SJulian Elischer * externally accessible. 1679ed5b7817SJulian Elischer */ 1680426da3bcSAlfred Perlstein fp->f_gcflag |= FMARK; 1681df8bae1dSRodney W. Grimes } 1682ed5b7817SJulian Elischer /* 16831c381b19SRobert Watson * Either it was defered, or it is externally 16841c381b19SRobert Watson * accessible and not already marked so. Now check 16851c381b19SRobert Watson * if it is possibly one of OUR sockets. 1686ed5b7817SJulian Elischer */ 1687df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_SOCKET || 168848e3128bSMatthew Dillon (so = fp->f_data) == NULL) { 1689426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1690df8bae1dSRodney W. Grimes continue; 1691426da3bcSAlfred Perlstein } 1692426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1693748e0b0aSGarrett Wollman if (so->so_proto->pr_domain != &localdomain || 1694df8bae1dSRodney W. Grimes (so->so_proto->pr_flags&PR_RIGHTS) == 0) 1695df8bae1dSRodney W. Grimes continue; 1696ed5b7817SJulian Elischer /* 16971c381b19SRobert Watson * So, Ok, it's one of our sockets and it IS 16981c381b19SRobert Watson * externally accessible (or was defered). Now we 16991c381b19SRobert Watson * look to see if we hold any file descriptors in its 1700ed5b7817SJulian Elischer * message buffers. Follow those links and mark them 1701ed5b7817SJulian Elischer * as accessible too. 1702ed5b7817SJulian Elischer */ 17037717cf07SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1704df8bae1dSRodney W. Grimes unp_scan(so->so_rcv.sb_mb, unp_mark); 17057717cf07SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 1706df8bae1dSRodney W. Grimes } 1707df8bae1dSRodney W. Grimes } while (unp_defer); 1708426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1709df8bae1dSRodney W. Grimes /* 1710a0ec558aSRobert Watson * XXXRW: The following comments need updating for a post-SMPng and 1711a0ec558aSRobert Watson * deferred unp_gc() world, but are still generally accurate. 1712a0ec558aSRobert Watson * 17131c381b19SRobert Watson * We grab an extra reference to each of the file table entries that 17141c381b19SRobert Watson * are not otherwise accessible and then free the rights that are 17151c381b19SRobert Watson * stored in messages on them. 1716df8bae1dSRodney W. Grimes * 1717df8bae1dSRodney W. Grimes * The bug in the orginal code is a little tricky, so I'll describe 1718df8bae1dSRodney W. Grimes * what's wrong with it here. 1719df8bae1dSRodney W. Grimes * 1720df8bae1dSRodney W. Grimes * It is incorrect to simply unp_discard each entry for f_msgcount 1721df8bae1dSRodney W. Grimes * times -- consider the case of sockets A and B that contain 1722df8bae1dSRodney W. Grimes * references to each other. On a last close of some other socket, 1723df8bae1dSRodney W. Grimes * we trigger a gc since the number of outstanding rights (unp_rights) 1724a0ec558aSRobert Watson * is non-zero. If during the sweep phase the gc code unp_discards, 1725df8bae1dSRodney W. Grimes * we end up doing a (full) closef on the descriptor. A closef on A 1726df8bae1dSRodney W. Grimes * results in the following chain. Closef calls soo_close, which 1727df8bae1dSRodney W. Grimes * calls soclose. Soclose calls first (through the switch 1728df8bae1dSRodney W. Grimes * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 17291c381b19SRobert Watson * returns because the previous instance had set unp_gcing, and we 17301c381b19SRobert Watson * return all the way back to soclose, which marks the socket with 17311c381b19SRobert Watson * SS_NOFDREF, and then calls sofree. Sofree calls sorflush to free 17321c381b19SRobert Watson * up the rights that are queued in messages on the socket A, i.e., 17331c381b19SRobert Watson * the reference on B. The sorflush calls via the dom_dispose switch 17341c381b19SRobert Watson * unp_dispose, which unp_scans with unp_discard. This second 1735df8bae1dSRodney W. Grimes * instance of unp_discard just calls closef on B. 1736df8bae1dSRodney W. Grimes * 1737df8bae1dSRodney W. Grimes * Well, a similar chain occurs on B, resulting in a sorflush on B, 1738df8bae1dSRodney W. Grimes * which results in another closef on A. Unfortunately, A is already 1739df8bae1dSRodney W. Grimes * being closed, and the descriptor has already been marked with 1740df8bae1dSRodney W. Grimes * SS_NOFDREF, and soclose panics at this point. 1741df8bae1dSRodney W. Grimes * 1742df8bae1dSRodney W. Grimes * Here, we first take an extra reference to each inaccessible 17431c381b19SRobert Watson * descriptor. Then, we call sorflush ourself, since we know it is a 17441c381b19SRobert Watson * Unix domain socket anyhow. After we destroy all the rights 17451c381b19SRobert Watson * carried in messages, we do a last closef to get rid of our extra 17461c381b19SRobert Watson * reference. This is the last close, and the unp_detach etc will 17471c381b19SRobert Watson * shut down the socket. 1748df8bae1dSRodney W. Grimes * 1749df8bae1dSRodney W. Grimes * 91/09/19, bsy@cs.cmu.edu 1750df8bae1dSRodney W. Grimes */ 175195f004dcSAlfred Perlstein again: 1752e4643c73SPoul-Henning Kamp nfiles_snap = openfiles + nfiles_slack; /* some slack */ 175395f004dcSAlfred Perlstein extra_ref = malloc(nfiles_snap * sizeof(struct file *), M_TEMP, 175495f004dcSAlfred Perlstein M_WAITOK); 1755426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 1756e4643c73SPoul-Henning Kamp if (nfiles_snap < openfiles) { 175795f004dcSAlfred Perlstein sx_sunlock(&filelist_lock); 175895f004dcSAlfred Perlstein free(extra_ref, M_TEMP); 175995f004dcSAlfred Perlstein nfiles_slack += 20; 176095f004dcSAlfred Perlstein goto again; 176195f004dcSAlfred Perlstein } 1762fc3fcacfSRobert Watson for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; 1763fc3fcacfSRobert Watson fp != NULL; fp = nextfp) { 17642e3c8fcbSPoul-Henning Kamp nextfp = LIST_NEXT(fp, f_list); 1765426da3bcSAlfred Perlstein FILE_LOCK(fp); 1766ed5b7817SJulian Elischer /* 1767ed5b7817SJulian Elischer * If it's not open, skip it 1768ed5b7817SJulian Elischer */ 1769426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1770426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1771df8bae1dSRodney W. Grimes continue; 1772426da3bcSAlfred Perlstein } 1773ed5b7817SJulian Elischer /* 1774ed5b7817SJulian Elischer * If all refs are from msgs, and it's not marked accessible 17751c381b19SRobert Watson * then it must be referenced from some unreachable cycle of 17761c381b19SRobert Watson * (shut-down) FDs, so include it in our list of FDs to 17771c381b19SRobert Watson * remove. 1778ed5b7817SJulian Elischer */ 1779426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) { 1780df8bae1dSRodney W. Grimes *fpp++ = fp; 1781df8bae1dSRodney W. Grimes nunref++; 1782df8bae1dSRodney W. Grimes fp->f_count++; 1783df8bae1dSRodney W. Grimes } 1784426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1785df8bae1dSRodney W. Grimes } 1786426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1787ed5b7817SJulian Elischer /* 17881c381b19SRobert Watson * For each FD on our hit list, do the following two things: 1789ed5b7817SJulian Elischer */ 17901c7c3c6aSMatthew Dillon for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 17911c7c3c6aSMatthew Dillon struct file *tfp = *fpp; 1792426da3bcSAlfred Perlstein FILE_LOCK(tfp); 1793cd72f218SMatthew Dillon if (tfp->f_type == DTYPE_SOCKET && 179448e3128bSMatthew Dillon tfp->f_data != NULL) { 1795426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 179648e3128bSMatthew Dillon sorflush(tfp->f_data); 1797e5aeaa0cSDag-Erling Smørgrav } else { 1798426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 17991c7c3c6aSMatthew Dillon } 1800e5aeaa0cSDag-Erling Smørgrav } 1801a0ec558aSRobert Watson for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 1802b40ce416SJulian Elischer closef(*fpp, (struct thread *) NULL); 1803a0ec558aSRobert Watson unp_recycled++; 1804a0ec558aSRobert Watson } 1805210a5a71SAlfred Perlstein free(extra_ref, M_TEMP); 1806df8bae1dSRodney W. Grimes } 1807df8bae1dSRodney W. Grimes 180826f9a767SRodney W. Grimes void 1809892af6b9SRobert Watson unp_dispose(struct mbuf *m) 1810df8bae1dSRodney W. Grimes { 1811996c772fSJohn Dyson 1812df8bae1dSRodney W. Grimes if (m) 1813df8bae1dSRodney W. Grimes unp_scan(m, unp_discard); 1814df8bae1dSRodney W. Grimes } 1815df8bae1dSRodney W. Grimes 18160c1bb4fbSDima Dorfman static int 1817d374e81eSRobert Watson unp_listen(struct socket *so, struct unpcb *unp, int backlog, 1818d374e81eSRobert Watson struct thread *td) 18190c1bb4fbSDima Dorfman { 18200daccb9cSRobert Watson int error; 18210daccb9cSRobert Watson 18220d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 18230c1bb4fbSDima Dorfman 18240daccb9cSRobert Watson SOCK_LOCK(so); 18250daccb9cSRobert Watson error = solisten_proto_check(so); 18260daccb9cSRobert Watson if (error == 0) { 18276f105b34SJohn Baldwin cru2x(td->td_ucred, &unp->unp_peercred); 18280c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPCCACHED; 1829d374e81eSRobert Watson solisten_proto(so, backlog); 18300daccb9cSRobert Watson } 18310daccb9cSRobert Watson SOCK_UNLOCK(so); 18320daccb9cSRobert Watson return (error); 18330c1bb4fbSDima Dorfman } 18340c1bb4fbSDima Dorfman 1835f708ef1bSPoul-Henning Kamp static void 1836892af6b9SRobert Watson unp_scan(struct mbuf *m0, void (*op)(struct file *)) 1837df8bae1dSRodney W. Grimes { 18382bc21ed9SDavid Malone struct mbuf *m; 18392bc21ed9SDavid Malone struct file **rp; 18402bc21ed9SDavid Malone struct cmsghdr *cm; 18412bc21ed9SDavid Malone void *data; 18422bc21ed9SDavid Malone int i; 18432bc21ed9SDavid Malone socklen_t clen, datalen; 1844df8bae1dSRodney W. Grimes int qfds; 1845df8bae1dSRodney W. Grimes 1846fc3fcacfSRobert Watson while (m0 != NULL) { 18472bc21ed9SDavid Malone for (m = m0; m; m = m->m_next) { 184812396bdcSDavid Malone if (m->m_type != MT_CONTROL) 1849df8bae1dSRodney W. Grimes continue; 18502bc21ed9SDavid Malone 18512bc21ed9SDavid Malone cm = mtod(m, struct cmsghdr *); 18522bc21ed9SDavid Malone clen = m->m_len; 18532bc21ed9SDavid Malone 18542bc21ed9SDavid Malone while (cm != NULL) { 18552bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) 18562bc21ed9SDavid Malone break; 18572bc21ed9SDavid Malone 18582bc21ed9SDavid Malone data = CMSG_DATA(cm); 18592bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len 18602bc21ed9SDavid Malone - (caddr_t)data; 18612bc21ed9SDavid Malone 18622bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET && 18632bc21ed9SDavid Malone cm->cmsg_type == SCM_RIGHTS) { 18642bc21ed9SDavid Malone qfds = datalen / sizeof (struct file *); 18652bc21ed9SDavid Malone rp = data; 1866df8bae1dSRodney W. Grimes for (i = 0; i < qfds; i++) 1867df8bae1dSRodney W. Grimes (*op)(*rp++); 18682bc21ed9SDavid Malone } 18692bc21ed9SDavid Malone 18702bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 18712bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 18722bc21ed9SDavid Malone cm = (struct cmsghdr *) 18732bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 18742bc21ed9SDavid Malone } else { 18752bc21ed9SDavid Malone clen = 0; 18762bc21ed9SDavid Malone cm = NULL; 18772bc21ed9SDavid Malone } 18782bc21ed9SDavid Malone } 1879df8bae1dSRodney W. Grimes } 1880df8bae1dSRodney W. Grimes m0 = m0->m_act; 1881df8bae1dSRodney W. Grimes } 1882df8bae1dSRodney W. Grimes } 1883df8bae1dSRodney W. Grimes 1884f708ef1bSPoul-Henning Kamp static void 1885892af6b9SRobert Watson unp_mark(struct file *fp) 1886df8bae1dSRodney W. Grimes { 1887426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) 1888df8bae1dSRodney W. Grimes return; 1889df8bae1dSRodney W. Grimes unp_defer++; 1890426da3bcSAlfred Perlstein fp->f_gcflag |= (FMARK|FDEFER); 1891df8bae1dSRodney W. Grimes } 1892df8bae1dSRodney W. Grimes 1893f708ef1bSPoul-Henning Kamp static void 1894892af6b9SRobert Watson unp_discard(struct file *fp) 1895df8bae1dSRodney W. Grimes { 1896a0ec558aSRobert Watson UNP_LOCK(); 1897426da3bcSAlfred Perlstein FILE_LOCK(fp); 1898df8bae1dSRodney W. Grimes fp->f_msgcount--; 1899df8bae1dSRodney W. Grimes unp_rights--; 1900426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1901a0ec558aSRobert Watson UNP_UNLOCK(); 1902b40ce416SJulian Elischer (void) closef(fp, (struct thread *)NULL); 1903df8bae1dSRodney W. Grimes } 1904