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 34677b542eSDavid E. O'Brien #include <sys/cdefs.h> 35677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 36677b542eSDavid E. O'Brien 37335654d7SRobert Watson #include "opt_mac.h" 38335654d7SRobert Watson 39df8bae1dSRodney W. Grimes #include <sys/param.h> 40fb919e4dSMark Murray #include <sys/domain.h> 41960ed29cSSeigo Tanimura #include <sys/fcntl.h> 42d826c479SBruce Evans #include <sys/malloc.h> /* XXX must be before <sys/file.h> */ 434f590175SPaul Saab #include <sys/eventhandler.h> 44639acc13SGarrett Wollman #include <sys/file.h> 45960ed29cSSeigo Tanimura #include <sys/filedesc.h> 46960ed29cSSeigo Tanimura #include <sys/jail.h> 47960ed29cSSeigo Tanimura #include <sys/kernel.h> 48960ed29cSSeigo Tanimura #include <sys/lock.h> 496ea48a90SRobert Watson #include <sys/mac.h> 50639acc13SGarrett Wollman #include <sys/mbuf.h> 51033eb86eSJeff Roberson #include <sys/mount.h> 52960ed29cSSeigo Tanimura #include <sys/mutex.h> 53639acc13SGarrett Wollman #include <sys/namei.h> 54639acc13SGarrett Wollman #include <sys/proc.h> 55df8bae1dSRodney W. Grimes #include <sys/protosw.h> 56960ed29cSSeigo Tanimura #include <sys/resourcevar.h> 57df8bae1dSRodney W. Grimes #include <sys/socket.h> 58df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 59960ed29cSSeigo Tanimura #include <sys/signalvar.h> 60df8bae1dSRodney W. Grimes #include <sys/stat.h> 61960ed29cSSeigo Tanimura #include <sys/sx.h> 62639acc13SGarrett Wollman #include <sys/sysctl.h> 63960ed29cSSeigo Tanimura #include <sys/systm.h> 64a0ec558aSRobert Watson #include <sys/taskqueue.h> 65639acc13SGarrett Wollman #include <sys/un.h> 6698271db4SGarrett Wollman #include <sys/unpcb.h> 67639acc13SGarrett Wollman #include <sys/vnode.h> 68df8bae1dSRodney W. Grimes 699e9d298aSJeff Roberson #include <vm/uma.h> 7098271db4SGarrett Wollman 719e9d298aSJeff Roberson static uma_zone_t unp_zone; 7298271db4SGarrett Wollman static unp_gen_t unp_gencnt; 7398271db4SGarrett Wollman static u_int unp_count; 7498271db4SGarrett Wollman 7598271db4SGarrett Wollman static struct unp_head unp_shead, unp_dhead; 7698271db4SGarrett Wollman 77df8bae1dSRodney W. Grimes /* 78df8bae1dSRodney W. Grimes * Unix communications domain. 79df8bae1dSRodney W. Grimes * 80df8bae1dSRodney W. Grimes * TODO: 81df8bae1dSRodney W. Grimes * SEQPACKET, RDM 82df8bae1dSRodney W. Grimes * rethink name space problems 83df8bae1dSRodney W. Grimes * need a proper out-of-band 8498271db4SGarrett Wollman * lock pushdown 85df8bae1dSRodney W. Grimes */ 86e7dd9a10SRobert Watson static const struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 87f708ef1bSPoul-Henning Kamp static ino_t unp_ino; /* prototype for fake inode numbers */ 886a2989fdSMatthew N. Dodd struct mbuf *unp_addsockcred(struct thread *, struct mbuf *); 89f708ef1bSPoul-Henning Kamp 90ce5f32deSRobert Watson /* 917e711c3aSRobert Watson * Both send and receive buffers are allocated PIPSIZ bytes of buffering for 927e711c3aSRobert Watson * stream sockets, although the total for sender and receiver is actually 937e711c3aSRobert Watson * only PIPSIZ. 947e711c3aSRobert Watson * 957e711c3aSRobert Watson * Datagram sockets really use the sendspace as the maximum datagram size, 967e711c3aSRobert Watson * and don't really want to reserve the sendspace. Their recvspace should be 977e711c3aSRobert Watson * large enough for at least one max-size datagram plus address. 987e711c3aSRobert Watson */ 997e711c3aSRobert Watson #ifndef PIPSIZ 1007e711c3aSRobert Watson #define PIPSIZ 8192 1017e711c3aSRobert Watson #endif 1027e711c3aSRobert Watson static u_long unpst_sendspace = PIPSIZ; 1037e711c3aSRobert Watson static u_long unpst_recvspace = PIPSIZ; 1047e711c3aSRobert Watson static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 1057e711c3aSRobert Watson static u_long unpdg_recvspace = 4*1024; 1067e711c3aSRobert Watson 1077e711c3aSRobert Watson static int unp_rights; /* file descriptors in flight */ 1087e711c3aSRobert Watson 1097e711c3aSRobert Watson SYSCTL_DECL(_net_local_stream); 1107e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 1117e711c3aSRobert Watson &unpst_sendspace, 0, ""); 1127e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 1137e711c3aSRobert Watson &unpst_recvspace, 0, ""); 1147e711c3aSRobert Watson SYSCTL_DECL(_net_local_dgram); 1157e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 1167e711c3aSRobert Watson &unpdg_sendspace, 0, ""); 1177e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 1187e711c3aSRobert Watson &unpdg_recvspace, 0, ""); 1197e711c3aSRobert Watson SYSCTL_DECL(_net_local); 1207e711c3aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); 1217e711c3aSRobert Watson 1227e711c3aSRobert Watson /* 123ce5f32deSRobert Watson * Currently, UNIX domain sockets are protected by a single subsystem lock, 124ce5f32deSRobert Watson * which covers global data structures and variables, the contents of each 125ce5f32deSRobert Watson * per-socket unpcb structure, and the so_pcb field in sockets attached to 126ce5f32deSRobert Watson * the UNIX domain. This provides for a moderate degree of paralellism, as 127ce5f32deSRobert Watson * receive operations on UNIX domain sockets do not need to acquire the 128ce5f32deSRobert Watson * subsystem lock. Finer grained locking to permit send() without acquiring 129ce5f32deSRobert Watson * a global lock would be a logical next step. 130ce5f32deSRobert Watson * 131ce5f32deSRobert Watson * The UNIX domain socket lock preceds all socket layer locks, including the 132ce5f32deSRobert Watson * socket lock and socket buffer lock, permitting UNIX domain socket code to 133ce5f32deSRobert Watson * call into socket support routines without releasing its locks. 134ce5f32deSRobert Watson * 135ce5f32deSRobert Watson * Some caution is required in areas where the UNIX domain socket code enters 136ce5f32deSRobert Watson * VFS in order to create or find rendezvous points. This results in 137ce5f32deSRobert Watson * dropping of the UNIX domain socket subsystem lock, acquisition of the 138ce5f32deSRobert Watson * Giant lock, and potential sleeping. This increases the chances of races, 139ce5f32deSRobert Watson * and exposes weaknesses in the socket->protocol API by offering poor 140ce5f32deSRobert Watson * failure modes. 141ce5f32deSRobert Watson */ 1420d9ce3a1SRobert Watson static struct mtx unp_mtx; 1430d9ce3a1SRobert Watson #define UNP_LOCK_INIT() \ 1440d9ce3a1SRobert Watson mtx_init(&unp_mtx, "unp", NULL, MTX_DEF) 1450d9ce3a1SRobert Watson #define UNP_LOCK() mtx_lock(&unp_mtx) 1460d9ce3a1SRobert Watson #define UNP_UNLOCK() mtx_unlock(&unp_mtx) 1470d9ce3a1SRobert Watson #define UNP_LOCK_ASSERT() mtx_assert(&unp_mtx, MA_OWNED) 1484c5bc1caSRobert Watson #define UNP_UNLOCK_ASSERT() mtx_assert(&unp_mtx, MA_NOTOWNED) 1490d9ce3a1SRobert Watson 150a0ec558aSRobert Watson /* 151a0ec558aSRobert Watson * Garbage collection of cyclic file descriptor/socket references occurs 152a0ec558aSRobert Watson * asynchronously in a taskqueue context in order to avoid recursion and 153a0ec558aSRobert Watson * reentrance in the UNIX domain socket, file descriptor, and socket layer 154a0ec558aSRobert Watson * code. See unp_gc() for a full description. 155a0ec558aSRobert Watson */ 156a0ec558aSRobert Watson static struct task unp_gc_task; 157a0ec558aSRobert Watson 15870f52b48SBruce Evans static int unp_connect(struct socket *,struct sockaddr *, struct thread *); 1596a2989fdSMatthew N. Dodd static int unp_connect2(struct socket *so, struct socket *so2, int); 1604d77a549SAlfred Perlstein static void unp_disconnect(struct unpcb *); 1614d77a549SAlfred Perlstein static void unp_shutdown(struct unpcb *); 1624d77a549SAlfred Perlstein static void unp_drop(struct unpcb *, int); 163a0ec558aSRobert Watson static void unp_gc(__unused void *, int); 1644d77a549SAlfred Perlstein static void unp_scan(struct mbuf *, void (*)(struct file *)); 1654d77a549SAlfred Perlstein static void unp_mark(struct file *); 1664d77a549SAlfred Perlstein static void unp_discard(struct file *); 1674d77a549SAlfred Perlstein static void unp_freerights(struct file **, int); 1684d77a549SAlfred Perlstein static int unp_internalize(struct mbuf **, struct thread *); 169d374e81eSRobert Watson static int unp_listen(struct socket *, struct unpcb *, int, 170d374e81eSRobert Watson struct thread *); 171f708ef1bSPoul-Henning Kamp 172ac45e92fSRobert Watson static void 173a29f300eSGarrett Wollman uipc_abort(struct socket *so) 174df8bae1dSRodney W. Grimes { 17540f2ac28SRobert Watson struct unpcb *unp; 176df8bae1dSRodney W. Grimes 17740f2ac28SRobert Watson unp = sotounpcb(so); 1784d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_abort: unp == NULL")); 1794d4b555eSRobert Watson UNP_LOCK(); 180a29f300eSGarrett Wollman unp_drop(unp, ECONNABORTED); 181a152f8a3SRobert Watson UNP_UNLOCK(); 182df8bae1dSRodney W. Grimes } 183df8bae1dSRodney W. Grimes 184a29f300eSGarrett Wollman static int 18557bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam) 186a29f300eSGarrett Wollman { 18740f2ac28SRobert Watson struct unpcb *unp; 1880d9ce3a1SRobert Watson const struct sockaddr *sa; 189df8bae1dSRodney W. Grimes 190df8bae1dSRodney W. Grimes /* 1911c381b19SRobert Watson * Pass back name of connected socket, if it was bound and we are 1921c381b19SRobert Watson * still connected (our peer may have closed already!). 193df8bae1dSRodney W. Grimes */ 1944d4b555eSRobert Watson unp = sotounpcb(so); 1954d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_accept: unp == NULL")); 1960d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 1970d9ce3a1SRobert Watson UNP_LOCK(); 1980d9ce3a1SRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr != NULL) 1990d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 2000d9ce3a1SRobert Watson else 2010d9ce3a1SRobert Watson sa = &sun_noname; 2020d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 2030d9ce3a1SRobert Watson UNP_UNLOCK(); 204e5aeaa0cSDag-Erling Smørgrav return (0); 205a29f300eSGarrett Wollman } 206df8bae1dSRodney W. Grimes 207a29f300eSGarrett Wollman static int 208b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td) 209a29f300eSGarrett Wollman { 2106d32873cSRobert Watson struct unpcb *unp; 2116d32873cSRobert Watson int error; 212df8bae1dSRodney W. Grimes 2136d32873cSRobert Watson KASSERT(so->so_pcb == NULL, ("uipc_attach: so_pcb != NULL")); 2146d32873cSRobert Watson if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 2156d32873cSRobert Watson switch (so->so_type) { 2166d32873cSRobert Watson 2176d32873cSRobert Watson case SOCK_STREAM: 2186d32873cSRobert Watson error = soreserve(so, unpst_sendspace, unpst_recvspace); 2196d32873cSRobert Watson break; 2206d32873cSRobert Watson 2216d32873cSRobert Watson case SOCK_DGRAM: 2226d32873cSRobert Watson error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 2236d32873cSRobert Watson break; 2246d32873cSRobert Watson 2256d32873cSRobert Watson default: 2266d32873cSRobert Watson panic("unp_attach"); 2276d32873cSRobert Watson } 2286d32873cSRobert Watson if (error) 2296d32873cSRobert Watson return (error); 2306d32873cSRobert Watson } 2316d32873cSRobert Watson unp = uma_zalloc(unp_zone, M_WAITOK | M_ZERO); 2326d32873cSRobert Watson if (unp == NULL) 2336d32873cSRobert Watson return (ENOBUFS); 2346d32873cSRobert Watson LIST_INIT(&unp->unp_refs); 2356d32873cSRobert Watson unp->unp_socket = so; 2366d32873cSRobert Watson so->so_pcb = unp; 2376d32873cSRobert Watson 2386d32873cSRobert Watson UNP_LOCK(); 2396d32873cSRobert Watson unp->unp_gencnt = ++unp_gencnt; 2406d32873cSRobert Watson unp_count++; 2416d32873cSRobert Watson LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead 2426d32873cSRobert Watson : &unp_shead, unp, unp_link); 2436d32873cSRobert Watson UNP_UNLOCK(); 2446d32873cSRobert Watson 2456d32873cSRobert Watson return (0); 246a29f300eSGarrett Wollman } 247a29f300eSGarrett Wollman 248a29f300eSGarrett Wollman static int 249b40ce416SJulian Elischer uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 250a29f300eSGarrett Wollman { 251dd47f5caSRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam; 252dd47f5caSRobert Watson struct vattr vattr; 253dd47f5caSRobert Watson int error, namelen; 254dd47f5caSRobert Watson struct nameidata nd; 25540f2ac28SRobert Watson struct unpcb *unp; 256dd47f5caSRobert Watson struct vnode *vp; 257dd47f5caSRobert Watson struct mount *mp; 258dd47f5caSRobert Watson char *buf; 259a29f300eSGarrett Wollman 26040f2ac28SRobert Watson unp = sotounpcb(so); 2614d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_bind: unp == NULL")); 2624f1f0ef5SRobert Watson 2634f1f0ef5SRobert Watson namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 2644f1f0ef5SRobert Watson if (namelen <= 0) 2654f1f0ef5SRobert Watson return (EINVAL); 266dd47f5caSRobert Watson 267dd47f5caSRobert Watson /* 2684f1f0ef5SRobert Watson * We don't allow simultaneous bind() calls on a single UNIX domain 2694f1f0ef5SRobert Watson * socket, so flag in-progress operations, and return an error if an 2704f1f0ef5SRobert Watson * operation is already in progress. 2714f1f0ef5SRobert Watson * 2724f1f0ef5SRobert Watson * Historically, we have not allowed a socket to be rebound, so this 2734f1f0ef5SRobert Watson * also returns an error. Not allowing re-binding certainly 2744f1f0ef5SRobert Watson * simplifies the implementation and avoids a great many possible 2754f1f0ef5SRobert Watson * failure modes. 276dd47f5caSRobert Watson */ 2774f1f0ef5SRobert Watson UNP_LOCK(); 278dd47f5caSRobert Watson if (unp->unp_vnode != NULL) { 27940f2ac28SRobert Watson UNP_UNLOCK(); 280dd47f5caSRobert Watson return (EINVAL); 281dd47f5caSRobert Watson } 2824f1f0ef5SRobert Watson if (unp->unp_flags & UNP_BINDING) { 283dd47f5caSRobert Watson UNP_UNLOCK(); 2844f1f0ef5SRobert Watson return (EALREADY); 285dd47f5caSRobert Watson } 2864f1f0ef5SRobert Watson unp->unp_flags |= UNP_BINDING; 287dd47f5caSRobert Watson UNP_UNLOCK(); 288dd47f5caSRobert Watson 289dd47f5caSRobert Watson buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 290dd47f5caSRobert Watson strlcpy(buf, soun->sun_path, namelen + 1); 291dd47f5caSRobert Watson 292dd47f5caSRobert Watson mtx_lock(&Giant); 293dd47f5caSRobert Watson restart: 294dd47f5caSRobert Watson mtx_assert(&Giant, MA_OWNED); 295dd47f5caSRobert Watson NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME, UIO_SYSSPACE, 296dd47f5caSRobert Watson buf, td); 297dd47f5caSRobert Watson /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 298dd47f5caSRobert Watson error = namei(&nd); 299dd47f5caSRobert Watson if (error) 3004f1f0ef5SRobert Watson goto error; 301dd47f5caSRobert Watson vp = nd.ni_vp; 302dd47f5caSRobert Watson if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 303dd47f5caSRobert Watson NDFREE(&nd, NDF_ONLY_PNBUF); 304dd47f5caSRobert Watson if (nd.ni_dvp == vp) 305dd47f5caSRobert Watson vrele(nd.ni_dvp); 306dd47f5caSRobert Watson else 307dd47f5caSRobert Watson vput(nd.ni_dvp); 308dd47f5caSRobert Watson if (vp != NULL) { 309dd47f5caSRobert Watson vrele(vp); 310dd47f5caSRobert Watson error = EADDRINUSE; 3114f1f0ef5SRobert Watson goto error; 312dd47f5caSRobert Watson } 313dd47f5caSRobert Watson error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 314dd47f5caSRobert Watson if (error) 3154f1f0ef5SRobert Watson goto error; 316dd47f5caSRobert Watson goto restart; 317dd47f5caSRobert Watson } 318dd47f5caSRobert Watson VATTR_NULL(&vattr); 319dd47f5caSRobert Watson vattr.va_type = VSOCK; 320dd47f5caSRobert Watson vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 321dd47f5caSRobert Watson #ifdef MAC 322dd47f5caSRobert Watson error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 323dd47f5caSRobert Watson &vattr); 324dd47f5caSRobert Watson #endif 325dd47f5caSRobert Watson if (error == 0) { 326dd47f5caSRobert Watson VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE); 327dd47f5caSRobert Watson error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 328dd47f5caSRobert Watson } 329dd47f5caSRobert Watson NDFREE(&nd, NDF_ONLY_PNBUF); 330dd47f5caSRobert Watson vput(nd.ni_dvp); 331dd47f5caSRobert Watson if (error) { 332dd47f5caSRobert Watson vn_finished_write(mp); 3334f1f0ef5SRobert Watson goto error; 334dd47f5caSRobert Watson } 335dd47f5caSRobert Watson vp = nd.ni_vp; 3364f1f0ef5SRobert Watson ASSERT_VOP_LOCKED(vp, "uipc_bind"); 337dd47f5caSRobert Watson soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 338dd47f5caSRobert Watson UNP_LOCK(); 339dd47f5caSRobert Watson vp->v_socket = unp->unp_socket; 340dd47f5caSRobert Watson unp->unp_vnode = vp; 341dd47f5caSRobert Watson unp->unp_addr = soun; 3424f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_BINDING; 343dd47f5caSRobert Watson UNP_UNLOCK(); 344dd47f5caSRobert Watson VOP_UNLOCK(vp, 0, td); 345dd47f5caSRobert Watson vn_finished_write(mp); 3464f1f0ef5SRobert Watson mtx_unlock(&Giant); 3474f1f0ef5SRobert Watson free(buf, M_TEMP); 3484f1f0ef5SRobert Watson return (0); 3494f1f0ef5SRobert Watson error: 3504f1f0ef5SRobert Watson UNP_LOCK(); 3514f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_BINDING; 3524f1f0ef5SRobert Watson UNP_UNLOCK(); 353dd47f5caSRobert Watson mtx_unlock(&Giant); 354dd47f5caSRobert Watson free(buf, M_TEMP); 35540f2ac28SRobert Watson return (error); 356a29f300eSGarrett Wollman } 357a29f300eSGarrett Wollman 358a29f300eSGarrett Wollman static int 359b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 360a29f300eSGarrett Wollman { 3610d9ce3a1SRobert Watson int error; 362a29f300eSGarrett Wollman 363fd179ee9SRobert Watson KASSERT(td == curthread, ("uipc_connect: td != curthread")); 3644d4b555eSRobert Watson UNP_LOCK(); 365fd179ee9SRobert Watson error = unp_connect(so, nam, td); 3660d9ce3a1SRobert Watson UNP_UNLOCK(); 3670d9ce3a1SRobert Watson return (error); 368a29f300eSGarrett Wollman } 369a29f300eSGarrett Wollman 370a152f8a3SRobert Watson /* 371a152f8a3SRobert Watson * XXXRW: Should also unbind? 372a152f8a3SRobert Watson */ 373a152f8a3SRobert Watson static void 374a152f8a3SRobert Watson uipc_close(struct socket *so) 375a152f8a3SRobert Watson { 376a152f8a3SRobert Watson struct unpcb *unp; 377a152f8a3SRobert Watson 378a152f8a3SRobert Watson unp = sotounpcb(so); 379a152f8a3SRobert Watson KASSERT(unp != NULL, ("uipc_close: unp == NULL")); 380a152f8a3SRobert Watson UNP_LOCK(); 381a152f8a3SRobert Watson unp_disconnect(unp); 382a152f8a3SRobert Watson UNP_UNLOCK(); 383a152f8a3SRobert Watson } 384a152f8a3SRobert Watson 385db48c0d2SRobert Watson int 386a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2) 387a29f300eSGarrett Wollman { 38840f2ac28SRobert Watson struct unpcb *unp; 3890d9ce3a1SRobert Watson int error; 390a29f300eSGarrett Wollman 39140f2ac28SRobert Watson unp = sotounpcb(so1); 3924d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_connect2: unp == NULL")); 3934d4b555eSRobert Watson UNP_LOCK(); 3946a2989fdSMatthew N. Dodd error = unp_connect2(so1, so2, PRU_CONNECT2); 3950d9ce3a1SRobert Watson UNP_UNLOCK(); 3960d9ce3a1SRobert Watson return (error); 397a29f300eSGarrett Wollman } 398a29f300eSGarrett Wollman 399a29f300eSGarrett Wollman /* control is EOPNOTSUPP */ 400a29f300eSGarrett Wollman 401bc725eafSRobert Watson static void 402a29f300eSGarrett Wollman uipc_detach(struct socket *so) 403a29f300eSGarrett Wollman { 4046d32873cSRobert Watson int local_unp_rights; 40540f2ac28SRobert Watson struct unpcb *unp; 4066d32873cSRobert Watson struct vnode *vp; 407a29f300eSGarrett Wollman 40840f2ac28SRobert Watson unp = sotounpcb(so); 4094d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_detach: unp == NULL")); 4104d4b555eSRobert Watson UNP_LOCK(); 4116d32873cSRobert Watson LIST_REMOVE(unp, unp_link); 4126d32873cSRobert Watson unp->unp_gencnt = ++unp_gencnt; 4136d32873cSRobert Watson --unp_count; 4146d32873cSRobert Watson if ((vp = unp->unp_vnode) != NULL) { 4156d32873cSRobert Watson /* 4166d32873cSRobert Watson * XXXRW: should v_socket be frobbed only while holding 4176d32873cSRobert Watson * Giant? 4186d32873cSRobert Watson */ 4196d32873cSRobert Watson unp->unp_vnode->v_socket = NULL; 4206d32873cSRobert Watson unp->unp_vnode = NULL; 4216d32873cSRobert Watson } 4226d32873cSRobert Watson if (unp->unp_conn != NULL) 4236d32873cSRobert Watson unp_disconnect(unp); 4246d32873cSRobert Watson while (!LIST_EMPTY(&unp->unp_refs)) { 4256d32873cSRobert Watson struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 4266d32873cSRobert Watson unp_drop(ref, ECONNRESET); 4276d32873cSRobert Watson } 4286d32873cSRobert Watson soisdisconnected(unp->unp_socket); 4296d32873cSRobert Watson unp->unp_socket->so_pcb = NULL; 4306d32873cSRobert Watson local_unp_rights = unp_rights; 4316d32873cSRobert Watson UNP_UNLOCK(); 4326d32873cSRobert Watson if (unp->unp_addr != NULL) 4336d32873cSRobert Watson FREE(unp->unp_addr, M_SONAME); 4346d32873cSRobert Watson uma_zfree(unp_zone, unp); 4356d32873cSRobert Watson if (vp) { 4366d32873cSRobert Watson int vfslocked; 4376d32873cSRobert Watson 4386d32873cSRobert Watson vfslocked = VFS_LOCK_GIANT(vp->v_mount); 4396d32873cSRobert Watson vrele(vp); 4406d32873cSRobert Watson VFS_UNLOCK_GIANT(vfslocked); 4416d32873cSRobert Watson } 4426d32873cSRobert Watson if (local_unp_rights) 4436d32873cSRobert Watson taskqueue_enqueue(taskqueue_thread, &unp_gc_task); 444a29f300eSGarrett Wollman } 445a29f300eSGarrett Wollman 446a29f300eSGarrett Wollman static int 447a29f300eSGarrett Wollman uipc_disconnect(struct socket *so) 448a29f300eSGarrett Wollman { 44940f2ac28SRobert Watson struct unpcb *unp; 450a29f300eSGarrett Wollman 45140f2ac28SRobert Watson unp = sotounpcb(so); 4524d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_disconnect: unp == NULL")); 4534d4b555eSRobert Watson UNP_LOCK(); 454a29f300eSGarrett Wollman unp_disconnect(unp); 4550d9ce3a1SRobert Watson UNP_UNLOCK(); 456e5aeaa0cSDag-Erling Smørgrav return (0); 457a29f300eSGarrett Wollman } 458a29f300eSGarrett Wollman 459a29f300eSGarrett Wollman static int 460d374e81eSRobert Watson uipc_listen(struct socket *so, int backlog, struct thread *td) 461a29f300eSGarrett Wollman { 46240f2ac28SRobert Watson struct unpcb *unp; 4630d9ce3a1SRobert Watson int error; 464a29f300eSGarrett Wollman 46540f2ac28SRobert Watson unp = sotounpcb(so); 4664d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_listen: unp == NULL")); 4674d4b555eSRobert Watson UNP_LOCK(); 4684d4b555eSRobert Watson if (unp->unp_vnode == NULL) { 46940f2ac28SRobert Watson UNP_UNLOCK(); 47040f2ac28SRobert Watson return (EINVAL); 47140f2ac28SRobert Watson } 472d374e81eSRobert Watson error = unp_listen(so, unp, backlog, td); 4730d9ce3a1SRobert Watson UNP_UNLOCK(); 4740d9ce3a1SRobert Watson return (error); 475a29f300eSGarrett Wollman } 476a29f300eSGarrett Wollman 477a29f300eSGarrett Wollman static int 47857bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 479a29f300eSGarrett Wollman { 48040f2ac28SRobert Watson struct unpcb *unp; 4810d9ce3a1SRobert Watson const struct sockaddr *sa; 482a29f300eSGarrett Wollman 4834d4b555eSRobert Watson unp = sotounpcb(so); 4844d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL")); 4850d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 4860d9ce3a1SRobert Watson UNP_LOCK(); 487fc3fcacfSRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr!= NULL) 4880d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 489bdc5f6a3SHajimu UMEMOTO else { 490bdc5f6a3SHajimu UMEMOTO /* 491bdc5f6a3SHajimu UMEMOTO * XXX: It seems that this test always fails even when 492bdc5f6a3SHajimu UMEMOTO * connection is established. So, this else clause is 493bdc5f6a3SHajimu UMEMOTO * added as workaround to return PF_LOCAL sockaddr. 494bdc5f6a3SHajimu UMEMOTO */ 4950d9ce3a1SRobert Watson sa = &sun_noname; 496bdc5f6a3SHajimu UMEMOTO } 4970d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 4980d9ce3a1SRobert Watson UNP_UNLOCK(); 499e5aeaa0cSDag-Erling Smørgrav return (0); 500a29f300eSGarrett Wollman } 501a29f300eSGarrett Wollman 502a29f300eSGarrett Wollman static int 503a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 504a29f300eSGarrett Wollman { 50540f2ac28SRobert Watson struct unpcb *unp; 506a29f300eSGarrett Wollman struct socket *so2; 507337cc6b6SRobert Watson u_int mbcnt, sbcc; 5086aef685fSBrian Feldman u_long newhiwat; 509a29f300eSGarrett Wollman 51040f2ac28SRobert Watson unp = sotounpcb(so); 5114d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_rcvd: unp == NULL")); 512df8bae1dSRodney W. Grimes switch (so->so_type) { 513df8bae1dSRodney W. Grimes case SOCK_DGRAM: 514a29f300eSGarrett Wollman panic("uipc_rcvd DGRAM?"); 515df8bae1dSRodney W. Grimes /*NOTREACHED*/ 516df8bae1dSRodney W. Grimes 517df8bae1dSRodney W. Grimes case SOCK_STREAM: 518df8bae1dSRodney W. Grimes /* 5191c381b19SRobert Watson * Adjust backpressure on sender and wakeup any waiting to 5201c381b19SRobert Watson * write. 521df8bae1dSRodney W. Grimes */ 522337cc6b6SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 523337cc6b6SRobert Watson mbcnt = so->so_rcv.sb_mbcnt; 524337cc6b6SRobert Watson sbcc = so->so_rcv.sb_cc; 525337cc6b6SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 526337cc6b6SRobert Watson UNP_LOCK(); 527337cc6b6SRobert Watson if (unp->unp_conn == NULL) { 528337cc6b6SRobert Watson UNP_UNLOCK(); 529337cc6b6SRobert Watson break; 530337cc6b6SRobert Watson } 531337cc6b6SRobert Watson so2 = unp->unp_conn->unp_socket; 532337cc6b6SRobert Watson SOCKBUF_LOCK(&so2->so_snd); 533337cc6b6SRobert Watson so2->so_snd.sb_mbmax += unp->unp_mbcnt - mbcnt; 534337cc6b6SRobert Watson newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - sbcc; 535f535380cSDon Lewis (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat, 5366aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 5371e4d7da7SRobert Watson sowwakeup_locked(so2); 538337cc6b6SRobert Watson unp->unp_mbcnt = mbcnt; 539337cc6b6SRobert Watson unp->unp_cc = sbcc; 540337cc6b6SRobert Watson UNP_UNLOCK(); 541df8bae1dSRodney W. Grimes break; 542df8bae1dSRodney W. Grimes 543df8bae1dSRodney W. Grimes default: 544a29f300eSGarrett Wollman panic("uipc_rcvd unknown socktype"); 545df8bae1dSRodney W. Grimes } 546e5aeaa0cSDag-Erling Smørgrav return (0); 547a29f300eSGarrett Wollman } 548df8bae1dSRodney W. Grimes 549a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 550a29f300eSGarrett Wollman 551a29f300eSGarrett Wollman static int 55257bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 553b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 554a29f300eSGarrett Wollman { 555f3f49bbbSRobert Watson struct unpcb *unp, *unp2; 556a29f300eSGarrett Wollman struct socket *so2; 557337cc6b6SRobert Watson u_int mbcnt, sbcc; 5586aef685fSBrian Feldman u_long newhiwat; 559f3f49bbbSRobert Watson int error = 0; 560a29f300eSGarrett Wollman 56140f2ac28SRobert Watson unp = sotounpcb(so); 5624d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_send: unp == NULL")); 563a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 564a29f300eSGarrett Wollman error = EOPNOTSUPP; 565a29f300eSGarrett Wollman goto release; 566a29f300eSGarrett Wollman } 567a29f300eSGarrett Wollman 568fc3fcacfSRobert Watson if (control != NULL && (error = unp_internalize(&control, td))) 569a29f300eSGarrett Wollman goto release; 570df8bae1dSRodney W. Grimes 5710d9ce3a1SRobert Watson UNP_LOCK(); 572a29f300eSGarrett Wollman switch (so->so_type) { 573a29f300eSGarrett Wollman case SOCK_DGRAM: 574a29f300eSGarrett Wollman { 575e7dd9a10SRobert Watson const struct sockaddr *from; 576df8bae1dSRodney W. Grimes 577fc3fcacfSRobert Watson if (nam != NULL) { 578fc3fcacfSRobert Watson if (unp->unp_conn != NULL) { 579df8bae1dSRodney W. Grimes error = EISCONN; 580df8bae1dSRodney W. Grimes break; 581df8bae1dSRodney W. Grimes } 582b40ce416SJulian Elischer error = unp_connect(so, nam, td); 583df8bae1dSRodney W. Grimes if (error) 584df8bae1dSRodney W. Grimes break; 585df8bae1dSRodney W. Grimes } else { 586fc3fcacfSRobert Watson if (unp->unp_conn == NULL) { 587df8bae1dSRodney W. Grimes error = ENOTCONN; 588df8bae1dSRodney W. Grimes break; 589df8bae1dSRodney W. Grimes } 590df8bae1dSRodney W. Grimes } 591f3f49bbbSRobert Watson unp2 = unp->unp_conn; 592f3f49bbbSRobert Watson so2 = unp2->unp_socket; 593fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 59457bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 595df8bae1dSRodney W. Grimes else 596df8bae1dSRodney W. Grimes from = &sun_noname; 597f3f49bbbSRobert Watson if (unp2->unp_flags & UNP_WANTCRED) 5986a2989fdSMatthew N. Dodd control = unp_addsockcred(td, control); 599a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 600a34b7046SRobert Watson if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) { 6011e4d7da7SRobert Watson sorwakeup_locked(so2); 602fc3fcacfSRobert Watson m = NULL; 603fc3fcacfSRobert Watson control = NULL; 604e5aeaa0cSDag-Erling Smørgrav } else { 605a34b7046SRobert Watson SOCKBUF_UNLOCK(&so2->so_rcv); 606df8bae1dSRodney W. Grimes error = ENOBUFS; 607e5aeaa0cSDag-Erling Smørgrav } 608fc3fcacfSRobert Watson if (nam != NULL) 609df8bae1dSRodney W. Grimes unp_disconnect(unp); 610df8bae1dSRodney W. Grimes break; 611df8bae1dSRodney W. Grimes } 612df8bae1dSRodney W. Grimes 613df8bae1dSRodney W. Grimes case SOCK_STREAM: 6146b8fda4dSGarrett Wollman /* 6151c381b19SRobert Watson * Connect if not connected yet. 6161c381b19SRobert Watson * 6171c381b19SRobert Watson * Note: A better implementation would complain if not equal 6181c381b19SRobert Watson * to the peer's address. 6196b8fda4dSGarrett Wollman */ 620402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 621fc3fcacfSRobert Watson if (nam != NULL) { 622b40ce416SJulian Elischer error = unp_connect(so, nam, td); 623402cc72dSDavid Greenman if (error) 6246b8fda4dSGarrett Wollman break; /* XXX */ 625402cc72dSDavid Greenman } else { 626402cc72dSDavid Greenman error = ENOTCONN; 627402cc72dSDavid Greenman break; 628402cc72dSDavid Greenman } 629402cc72dSDavid Greenman } 630402cc72dSDavid Greenman 631337cc6b6SRobert Watson /* Lockless read. */ 632c0b99ffaSRobert Watson if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 633df8bae1dSRodney W. Grimes error = EPIPE; 634df8bae1dSRodney W. Grimes break; 635df8bae1dSRodney W. Grimes } 636f3f49bbbSRobert Watson unp2 = unp->unp_conn; 637f3f49bbbSRobert Watson if (unp2 == NULL) 638a29f300eSGarrett Wollman panic("uipc_send connected but no connection?"); 639f3f49bbbSRobert Watson so2 = unp2->unp_socket; 640a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 641f3f49bbbSRobert Watson if (unp2->unp_flags & UNP_WANTCRED) { 6426a2989fdSMatthew N. Dodd /* 6436a2989fdSMatthew N. Dodd * Credentials are passed only once on 6446a2989fdSMatthew N. Dodd * SOCK_STREAM. 6456a2989fdSMatthew N. Dodd */ 646f3f49bbbSRobert Watson unp2->unp_flags &= ~UNP_WANTCRED; 6476a2989fdSMatthew N. Dodd control = unp_addsockcred(td, control); 6486a2989fdSMatthew N. Dodd } 649df8bae1dSRodney W. Grimes /* 6501c381b19SRobert Watson * Send to paired receive port, and then reduce send buffer 6511c381b19SRobert Watson * hiwater marks to maintain backpressure. Wake up readers. 652df8bae1dSRodney W. Grimes */ 653fc3fcacfSRobert Watson if (control != NULL) { 654a34b7046SRobert Watson if (sbappendcontrol_locked(&so2->so_rcv, m, control)) 655fc3fcacfSRobert Watson control = NULL; 656e5aeaa0cSDag-Erling Smørgrav } else { 657a34b7046SRobert Watson sbappend_locked(&so2->so_rcv, m); 658e5aeaa0cSDag-Erling Smørgrav } 659f3f49bbbSRobert Watson mbcnt = so2->so_rcv.sb_mbcnt - unp2->unp_mbcnt; 660f3f49bbbSRobert Watson unp2->unp_mbcnt = so2->so_rcv.sb_mbcnt; 661337cc6b6SRobert Watson sbcc = so2->so_rcv.sb_cc; 662337cc6b6SRobert Watson sorwakeup_locked(so2); 663337cc6b6SRobert Watson 664337cc6b6SRobert Watson SOCKBUF_LOCK(&so->so_snd); 665f3f49bbbSRobert Watson newhiwat = so->so_snd.sb_hiwat - (sbcc - unp2->unp_cc); 666f535380cSDon Lewis (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat, 6676aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 668337cc6b6SRobert Watson so->so_snd.sb_mbmax -= mbcnt; 6697abe2ac2SAlan Cox SOCKBUF_UNLOCK(&so->so_snd); 670337cc6b6SRobert Watson 671f3f49bbbSRobert Watson unp2->unp_cc = sbcc; 672fc3fcacfSRobert Watson m = NULL; 673df8bae1dSRodney W. Grimes break; 674df8bae1dSRodney W. Grimes 675df8bae1dSRodney W. Grimes default: 676a29f300eSGarrett Wollman panic("uipc_send unknown socktype"); 677df8bae1dSRodney W. Grimes } 678a29f300eSGarrett Wollman 6796b8fda4dSGarrett Wollman /* 6806b8fda4dSGarrett Wollman * SEND_EOF is equivalent to a SEND followed by 6816b8fda4dSGarrett Wollman * a SHUTDOWN. 6826b8fda4dSGarrett Wollman */ 683a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 6846b8fda4dSGarrett Wollman socantsendmore(so); 6856b8fda4dSGarrett Wollman unp_shutdown(unp); 6866b8fda4dSGarrett Wollman } 6870d9ce3a1SRobert Watson UNP_UNLOCK(); 688df8bae1dSRodney W. Grimes 689fc3fcacfSRobert Watson if (control != NULL && error != 0) 690bd508d39SDon Lewis unp_dispose(control); 691bd508d39SDon Lewis 692a29f300eSGarrett Wollman release: 693fc3fcacfSRobert Watson if (control != NULL) 694a29f300eSGarrett Wollman m_freem(control); 695fc3fcacfSRobert Watson if (m != NULL) 696a29f300eSGarrett Wollman m_freem(m); 697e5aeaa0cSDag-Erling Smørgrav return (error); 698a29f300eSGarrett Wollman } 699df8bae1dSRodney W. Grimes 700a29f300eSGarrett Wollman static int 701a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 702a29f300eSGarrett Wollman { 70340f2ac28SRobert Watson struct unpcb *unp; 704a29f300eSGarrett Wollman struct socket *so2; 705a29f300eSGarrett Wollman 70640f2ac28SRobert Watson unp = sotounpcb(so); 7074d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sense: unp == NULL")); 7084d4b555eSRobert Watson UNP_LOCK(); 709a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 710fc3fcacfSRobert Watson if (so->so_type == SOCK_STREAM && unp->unp_conn != NULL) { 711df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 712a29f300eSGarrett Wollman sb->st_blksize += so2->so_rcv.sb_cc; 713df8bae1dSRodney W. Grimes } 714f3732fd1SPoul-Henning Kamp sb->st_dev = NODEV; 715df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 7166f782c46SJeffrey Hsu unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 717a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 7180d9ce3a1SRobert Watson UNP_UNLOCK(); 719df8bae1dSRodney W. Grimes return (0); 720a29f300eSGarrett Wollman } 721df8bae1dSRodney W. Grimes 722a29f300eSGarrett Wollman static int 723a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 724a29f300eSGarrett Wollman { 72540f2ac28SRobert Watson struct unpcb *unp; 726df8bae1dSRodney W. Grimes 72740f2ac28SRobert Watson unp = sotounpcb(so); 7284d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_shutdown: unp == NULL")); 7294d4b555eSRobert Watson UNP_LOCK(); 730a29f300eSGarrett Wollman socantsendmore(so); 731a29f300eSGarrett Wollman unp_shutdown(unp); 7320d9ce3a1SRobert Watson UNP_UNLOCK(); 733e5aeaa0cSDag-Erling Smørgrav return (0); 734a29f300eSGarrett Wollman } 735df8bae1dSRodney W. Grimes 736a29f300eSGarrett Wollman static int 73757bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 738a29f300eSGarrett Wollman { 73940f2ac28SRobert Watson struct unpcb *unp; 7400d9ce3a1SRobert Watson const struct sockaddr *sa; 741a29f300eSGarrett Wollman 7424d4b555eSRobert Watson unp = sotounpcb(so); 7434d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL")); 7440d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 7450d9ce3a1SRobert Watson UNP_LOCK(); 746fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 7470d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_addr; 74883f3198bSThomas Moestl else 7490d9ce3a1SRobert Watson sa = &sun_noname; 7500d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 7510d9ce3a1SRobert Watson UNP_UNLOCK(); 752e5aeaa0cSDag-Erling Smørgrav return (0); 753df8bae1dSRodney W. Grimes } 754a29f300eSGarrett Wollman 755a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = { 756756d52a1SPoul-Henning Kamp .pru_abort = uipc_abort, 757756d52a1SPoul-Henning Kamp .pru_accept = uipc_accept, 758756d52a1SPoul-Henning Kamp .pru_attach = uipc_attach, 759756d52a1SPoul-Henning Kamp .pru_bind = uipc_bind, 760756d52a1SPoul-Henning Kamp .pru_connect = uipc_connect, 761756d52a1SPoul-Henning Kamp .pru_connect2 = uipc_connect2, 762756d52a1SPoul-Henning Kamp .pru_detach = uipc_detach, 763756d52a1SPoul-Henning Kamp .pru_disconnect = uipc_disconnect, 764756d52a1SPoul-Henning Kamp .pru_listen = uipc_listen, 765756d52a1SPoul-Henning Kamp .pru_peeraddr = uipc_peeraddr, 766756d52a1SPoul-Henning Kamp .pru_rcvd = uipc_rcvd, 767756d52a1SPoul-Henning Kamp .pru_send = uipc_send, 768756d52a1SPoul-Henning Kamp .pru_sense = uipc_sense, 769756d52a1SPoul-Henning Kamp .pru_shutdown = uipc_shutdown, 770756d52a1SPoul-Henning Kamp .pru_sockaddr = uipc_sockaddr, 771756d52a1SPoul-Henning Kamp .pru_sosend = sosend, 772756d52a1SPoul-Henning Kamp .pru_soreceive = soreceive, 773756d52a1SPoul-Henning Kamp .pru_sopoll = sopoll, 774a152f8a3SRobert Watson .pru_close = uipc_close, 775a29f300eSGarrett Wollman }; 776df8bae1dSRodney W. Grimes 7770c1bb4fbSDima Dorfman int 778892af6b9SRobert Watson uipc_ctloutput(struct socket *so, struct sockopt *sopt) 7790c1bb4fbSDima Dorfman { 78040f2ac28SRobert Watson struct unpcb *unp; 7810d9ce3a1SRobert Watson struct xucred xu; 7826a2989fdSMatthew N. Dodd int error, optval; 7836a2989fdSMatthew N. Dodd 78496a041b5SMatthew N. Dodd if (sopt->sopt_level != 0) 78596a041b5SMatthew N. Dodd return (EINVAL); 78696a041b5SMatthew N. Dodd 7876a2989fdSMatthew N. Dodd unp = sotounpcb(so); 7884d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL")); 7894d4b555eSRobert Watson UNP_LOCK(); 7906a2989fdSMatthew N. Dodd error = 0; 7910c1bb4fbSDima Dorfman switch (sopt->sopt_dir) { 7920c1bb4fbSDima Dorfman case SOPT_GET: 7930c1bb4fbSDima Dorfman switch (sopt->sopt_name) { 7940c1bb4fbSDima Dorfman case LOCAL_PEERCRED: 7950c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC) 7960d9ce3a1SRobert Watson xu = unp->unp_peercred; 7970c1bb4fbSDima Dorfman else { 7980c1bb4fbSDima Dorfman if (so->so_type == SOCK_STREAM) 7990c1bb4fbSDima Dorfman error = ENOTCONN; 8000c1bb4fbSDima Dorfman else 8010c1bb4fbSDima Dorfman error = EINVAL; 8020c1bb4fbSDima Dorfman } 8030d9ce3a1SRobert Watson if (error == 0) 8040d9ce3a1SRobert Watson error = sooptcopyout(sopt, &xu, sizeof(xu)); 8050c1bb4fbSDima Dorfman break; 8066a2989fdSMatthew N. Dodd case LOCAL_CREDS: 8076a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0; 8086a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 8096a2989fdSMatthew N. Dodd break; 8106a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 8116a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0; 8126a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 8136a2989fdSMatthew N. Dodd break; 8140c1bb4fbSDima Dorfman default: 8150c1bb4fbSDima Dorfman error = EOPNOTSUPP; 8160c1bb4fbSDima Dorfman break; 8170c1bb4fbSDima Dorfman } 8180c1bb4fbSDima Dorfman break; 8190c1bb4fbSDima Dorfman case SOPT_SET: 8206a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 8216a2989fdSMatthew N. Dodd case LOCAL_CREDS: 8226a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 8236a2989fdSMatthew N. Dodd error = sooptcopyin(sopt, &optval, sizeof(optval), 8246a2989fdSMatthew N. Dodd sizeof(optval)); 8256a2989fdSMatthew N. Dodd if (error) 8266a2989fdSMatthew N. Dodd break; 8276a2989fdSMatthew N. Dodd 8286a2989fdSMatthew N. Dodd #define OPTSET(bit) \ 8296a2989fdSMatthew N. Dodd if (optval) \ 8306a2989fdSMatthew N. Dodd unp->unp_flags |= bit; \ 8316a2989fdSMatthew N. Dodd else \ 8326a2989fdSMatthew N. Dodd unp->unp_flags &= ~bit; 8336a2989fdSMatthew N. Dodd 8346a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 8356a2989fdSMatthew N. Dodd case LOCAL_CREDS: 8366a2989fdSMatthew N. Dodd OPTSET(UNP_WANTCRED); 8376a2989fdSMatthew N. Dodd break; 8386a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 8396a2989fdSMatthew N. Dodd OPTSET(UNP_CONNWAIT); 8406a2989fdSMatthew N. Dodd break; 8416a2989fdSMatthew N. Dodd default: 8426a2989fdSMatthew N. Dodd break; 8436a2989fdSMatthew N. Dodd } 8446a2989fdSMatthew N. Dodd break; 8456a2989fdSMatthew N. Dodd #undef OPTSET 8466a2989fdSMatthew N. Dodd default: 8476a2989fdSMatthew N. Dodd error = ENOPROTOOPT; 8486a2989fdSMatthew N. Dodd break; 8496a2989fdSMatthew N. Dodd } 850abb886faSMatthew N. Dodd break; 8510c1bb4fbSDima Dorfman default: 8520c1bb4fbSDima Dorfman error = EOPNOTSUPP; 8530c1bb4fbSDima Dorfman break; 8540c1bb4fbSDima Dorfman } 8556a2989fdSMatthew N. Dodd UNP_UNLOCK(); 8560c1bb4fbSDima Dorfman return (error); 8570c1bb4fbSDima Dorfman } 8580c1bb4fbSDima Dorfman 859f708ef1bSPoul-Henning Kamp static int 860892af6b9SRobert Watson unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 861df8bae1dSRodney W. Grimes { 862892af6b9SRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam; 863892af6b9SRobert Watson struct vnode *vp; 864892af6b9SRobert Watson struct socket *so2, *so3; 865b295bdcdSRobert Watson struct unpcb *unp, *unp2, *unp3; 86657bf258eSGarrett Wollman int error, len; 867df8bae1dSRodney W. Grimes struct nameidata nd; 86857bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 8690d9ce3a1SRobert Watson struct sockaddr *sa; 8700d9ce3a1SRobert Watson 8710d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 872df8bae1dSRodney W. Grimes 8734d4b555eSRobert Watson unp = sotounpcb(so); 8744d4b555eSRobert Watson KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 87557bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 87657bf258eSGarrett Wollman if (len <= 0) 877e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 87855c85568SRobert Drehmel strlcpy(buf, soun->sun_path, len + 1); 8794f1f0ef5SRobert Watson if (unp->unp_flags & UNP_CONNECTING) { 8804f1f0ef5SRobert Watson UNP_UNLOCK(); 8814f1f0ef5SRobert Watson return (EALREADY); 8824f1f0ef5SRobert Watson } 8830d9ce3a1SRobert Watson UNP_UNLOCK(); 8840d9ce3a1SRobert Watson sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 8850d9ce3a1SRobert Watson mtx_lock(&Giant); 886b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td); 887797f2d22SPoul-Henning Kamp error = namei(&nd); 888797f2d22SPoul-Henning Kamp if (error) 8890d9ce3a1SRobert Watson vp = NULL; 8900d9ce3a1SRobert Watson else 891df8bae1dSRodney W. Grimes vp = nd.ni_vp; 8920d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_connect"); 893762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 8940d9ce3a1SRobert Watson if (error) 8950d9ce3a1SRobert Watson goto bad; 8960d9ce3a1SRobert Watson 897df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 898df8bae1dSRodney W. Grimes error = ENOTSOCK; 899df8bae1dSRodney W. Grimes goto bad; 900df8bae1dSRodney W. Grimes } 901a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 902797f2d22SPoul-Henning Kamp if (error) 903df8bae1dSRodney W. Grimes goto bad; 9042260c03dSRobert Watson mtx_unlock(&Giant); 9052260c03dSRobert Watson UNP_LOCK(); 906b295bdcdSRobert Watson unp = sotounpcb(so); 9074d4b555eSRobert Watson KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 908df8bae1dSRodney W. Grimes so2 = vp->v_socket; 909fc3fcacfSRobert Watson if (so2 == NULL) { 910df8bae1dSRodney W. Grimes error = ECONNREFUSED; 9112260c03dSRobert Watson goto bad2; 912df8bae1dSRodney W. Grimes } 913df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 914df8bae1dSRodney W. Grimes error = EPROTOTYPE; 9152260c03dSRobert Watson goto bad2; 916df8bae1dSRodney W. Grimes } 917df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 9180d9ce3a1SRobert Watson if (so2->so_options & SO_ACCEPTCONN) { 9190d9ce3a1SRobert Watson /* 9201c381b19SRobert Watson * NB: drop locks here so unp_attach is entered w/o 9211c381b19SRobert Watson * locks; this avoids a recursive lock of the head 9221c381b19SRobert Watson * and holding sleep locks across a (potentially) 9231c381b19SRobert Watson * blocking malloc. 9240d9ce3a1SRobert Watson */ 9250d9ce3a1SRobert Watson UNP_UNLOCK(); 9260d9ce3a1SRobert Watson so3 = sonewconn(so2, 0); 9270d9ce3a1SRobert Watson UNP_LOCK(); 9280d9ce3a1SRobert Watson } else 9290d9ce3a1SRobert Watson so3 = NULL; 9300d9ce3a1SRobert Watson if (so3 == NULL) { 931df8bae1dSRodney W. Grimes error = ECONNREFUSED; 9320d9ce3a1SRobert Watson goto bad2; 933df8bae1dSRodney W. Grimes } 9340c1bb4fbSDima Dorfman unp = sotounpcb(so); 935df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 936df8bae1dSRodney W. Grimes unp3 = sotounpcb(so3); 9370d9ce3a1SRobert Watson if (unp2->unp_addr != NULL) { 9380d9ce3a1SRobert Watson bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 9390d9ce3a1SRobert Watson unp3->unp_addr = (struct sockaddr_un *) sa; 9400d9ce3a1SRobert Watson sa = NULL; 9410d9ce3a1SRobert Watson } 9420c1bb4fbSDima Dorfman /* 9430c1bb4fbSDima Dorfman * unp_peercred management: 9440c1bb4fbSDima Dorfman * 9451c381b19SRobert Watson * The connecter's (client's) credentials are copied from its 9461c381b19SRobert Watson * process structure at the time of connect() (which is now). 9470c1bb4fbSDima Dorfman */ 948a854ed98SJohn Baldwin cru2x(td->td_ucred, &unp3->unp_peercred); 9490c1bb4fbSDima Dorfman unp3->unp_flags |= UNP_HAVEPC; 9500c1bb4fbSDima Dorfman /* 9511c381b19SRobert Watson * The receiver's (server's) credentials are copied from the 9521c381b19SRobert Watson * unp_peercred member of socket on which the former called 9531c381b19SRobert Watson * listen(); unp_listen() cached that process's credentials 9541c381b19SRobert Watson * at that time so we can use them now. 9550c1bb4fbSDima Dorfman */ 9560c1bb4fbSDima Dorfman KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 9570c1bb4fbSDima Dorfman ("unp_connect: listener without cached peercred")); 9580c1bb4fbSDima Dorfman memcpy(&unp->unp_peercred, &unp2->unp_peercred, 9590c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 9600c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPC; 961481f8fe8SMaxim Konovalov if (unp2->unp_flags & UNP_WANTCRED) 962481f8fe8SMaxim Konovalov unp3->unp_flags |= UNP_WANTCRED; 963335654d7SRobert Watson #ifdef MAC 964310e7cebSRobert Watson SOCK_LOCK(so); 965335654d7SRobert Watson mac_set_socket_peer_from_socket(so, so3); 966335654d7SRobert Watson mac_set_socket_peer_from_socket(so3, so); 967310e7cebSRobert Watson SOCK_UNLOCK(so); 968335654d7SRobert Watson #endif 9690c1bb4fbSDima Dorfman 970df8bae1dSRodney W. Grimes so2 = so3; 971df8bae1dSRodney W. Grimes } 9726a2989fdSMatthew N. Dodd error = unp_connect2(so, so2, PRU_CONNECT); 9730d9ce3a1SRobert Watson bad2: 9740d9ce3a1SRobert Watson UNP_UNLOCK(); 9750d9ce3a1SRobert Watson mtx_lock(&Giant); 976df8bae1dSRodney W. Grimes bad: 9770d9ce3a1SRobert Watson mtx_assert(&Giant, MA_OWNED); 9780d9ce3a1SRobert Watson if (vp != NULL) 979df8bae1dSRodney W. Grimes vput(vp); 9800d9ce3a1SRobert Watson mtx_unlock(&Giant); 9810d9ce3a1SRobert Watson free(sa, M_SONAME); 9820d9ce3a1SRobert Watson UNP_LOCK(); 9834f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_CONNECTING; 984df8bae1dSRodney W. Grimes return (error); 985df8bae1dSRodney W. Grimes } 986df8bae1dSRodney W. Grimes 987db48c0d2SRobert Watson static int 9886a2989fdSMatthew N. Dodd unp_connect2(struct socket *so, struct socket *so2, int req) 989df8bae1dSRodney W. Grimes { 990892af6b9SRobert Watson struct unpcb *unp = sotounpcb(so); 991892af6b9SRobert Watson struct unpcb *unp2; 992df8bae1dSRodney W. Grimes 9930d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 9940d9ce3a1SRobert Watson 995df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 996df8bae1dSRodney W. Grimes return (EPROTOTYPE); 997df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 9984d4b555eSRobert Watson KASSERT(unp2 != NULL, ("unp_connect2: unp2 == NULL")); 999df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 1000df8bae1dSRodney W. Grimes switch (so->so_type) { 1001df8bae1dSRodney W. Grimes 1002df8bae1dSRodney W. Grimes case SOCK_DGRAM: 100398271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 1004df8bae1dSRodney W. Grimes soisconnected(so); 1005df8bae1dSRodney W. Grimes break; 1006df8bae1dSRodney W. Grimes 1007df8bae1dSRodney W. Grimes case SOCK_STREAM: 1008df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 10096a2989fdSMatthew N. Dodd if (req == PRU_CONNECT && 10106a2989fdSMatthew N. Dodd ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT)) 10116a2989fdSMatthew N. Dodd soisconnecting(so); 10126a2989fdSMatthew N. Dodd else 1013df8bae1dSRodney W. Grimes soisconnected(so); 1014df8bae1dSRodney W. Grimes soisconnected(so2); 1015df8bae1dSRodney W. Grimes break; 1016df8bae1dSRodney W. Grimes 1017df8bae1dSRodney W. Grimes default: 1018df8bae1dSRodney W. Grimes panic("unp_connect2"); 1019df8bae1dSRodney W. Grimes } 1020df8bae1dSRodney W. Grimes return (0); 1021df8bae1dSRodney W. Grimes } 1022df8bae1dSRodney W. Grimes 1023f708ef1bSPoul-Henning Kamp static void 1024892af6b9SRobert Watson unp_disconnect(struct unpcb *unp) 1025df8bae1dSRodney W. Grimes { 1026892af6b9SRobert Watson struct unpcb *unp2 = unp->unp_conn; 10271b2e3b4bSRobert Watson struct socket *so; 1028df8bae1dSRodney W. Grimes 10290d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 10300d9ce3a1SRobert Watson 1031fc3fcacfSRobert Watson if (unp2 == NULL) 1032df8bae1dSRodney W. Grimes return; 1033fc3fcacfSRobert Watson unp->unp_conn = NULL; 1034df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 1035df8bae1dSRodney W. Grimes case SOCK_DGRAM: 103698271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 10371b2e3b4bSRobert Watson so = unp->unp_socket; 10381b2e3b4bSRobert Watson SOCK_LOCK(so); 10391b2e3b4bSRobert Watson so->so_state &= ~SS_ISCONNECTED; 10401b2e3b4bSRobert Watson SOCK_UNLOCK(so); 1041df8bae1dSRodney W. Grimes break; 1042df8bae1dSRodney W. Grimes 1043df8bae1dSRodney W. Grimes case SOCK_STREAM: 1044df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 1045fc3fcacfSRobert Watson unp2->unp_conn = NULL; 1046df8bae1dSRodney W. Grimes soisdisconnected(unp2->unp_socket); 1047df8bae1dSRodney W. Grimes break; 1048df8bae1dSRodney W. Grimes } 1049df8bae1dSRodney W. Grimes } 1050df8bae1dSRodney W. Grimes 10510d9ce3a1SRobert Watson /* 10521c381b19SRobert Watson * unp_pcblist() assumes that UNIX domain socket memory is never reclaimed by 10531c381b19SRobert Watson * the zone (UMA_ZONE_NOFREE), and as such potentially stale pointers are 10541c381b19SRobert Watson * safe to reference. It first scans the list of struct unpcb's to generate 10551c381b19SRobert Watson * a pointer list, then it rescans its list one entry at a time to 10560d9ce3a1SRobert Watson * externalize and copyout. It checks the generation number to see if a 10570d9ce3a1SRobert Watson * struct unpcb has been reused, and will skip it if so. 10580d9ce3a1SRobert Watson */ 105998271db4SGarrett Wollman static int 106082d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 106198271db4SGarrett Wollman { 1062f5ef029eSPoul-Henning Kamp int error, i, n; 106398271db4SGarrett Wollman struct unpcb *unp, **unp_list; 106498271db4SGarrett Wollman unp_gen_t gencnt; 10658f364875SJulian Elischer struct xunpgen *xug; 106698271db4SGarrett Wollman struct unp_head *head; 10678f364875SJulian Elischer struct xunpcb *xu; 106898271db4SGarrett Wollman 1069a23d65bfSBruce Evans head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 107098271db4SGarrett Wollman 107198271db4SGarrett Wollman /* 107298271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 107398271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 107498271db4SGarrett Wollman */ 1075fc3fcacfSRobert Watson if (req->oldptr == NULL) { 107698271db4SGarrett Wollman n = unp_count; 10778f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug) 107898271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 1079e5aeaa0cSDag-Erling Smørgrav return (0); 108098271db4SGarrett Wollman } 108198271db4SGarrett Wollman 1082fc3fcacfSRobert Watson if (req->newptr != NULL) 1083e5aeaa0cSDag-Erling Smørgrav return (EPERM); 108498271db4SGarrett Wollman 108598271db4SGarrett Wollman /* 108698271db4SGarrett Wollman * OK, now we're committed to doing something. 108798271db4SGarrett Wollman */ 1088a163d034SWarner Losh xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 10890d9ce3a1SRobert Watson UNP_LOCK(); 109098271db4SGarrett Wollman gencnt = unp_gencnt; 109198271db4SGarrett Wollman n = unp_count; 10920d9ce3a1SRobert Watson UNP_UNLOCK(); 109398271db4SGarrett Wollman 10948f364875SJulian Elischer xug->xug_len = sizeof *xug; 10958f364875SJulian Elischer xug->xug_count = n; 10968f364875SJulian Elischer xug->xug_gen = gencnt; 10978f364875SJulian Elischer xug->xug_sogen = so_gencnt; 10988f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 10998f364875SJulian Elischer if (error) { 11008f364875SJulian Elischer free(xug, M_TEMP); 1101e5aeaa0cSDag-Erling Smørgrav return (error); 11028f364875SJulian Elischer } 110398271db4SGarrett Wollman 1104a163d034SWarner Losh unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 110598271db4SGarrett Wollman 11060d9ce3a1SRobert Watson UNP_LOCK(); 11072e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 11082e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 11098a7d8cc6SRobert Watson if (unp->unp_gencnt <= gencnt) { 1110a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred, 11118a7d8cc6SRobert Watson unp->unp_socket->so_cred)) 11124787fd37SPaul Saab continue; 111398271db4SGarrett Wollman unp_list[i++] = unp; 111498271db4SGarrett Wollman } 11154787fd37SPaul Saab } 11160d9ce3a1SRobert Watson UNP_UNLOCK(); 11171c381b19SRobert Watson n = i; /* In case we lost some during malloc. */ 111898271db4SGarrett Wollman 111998271db4SGarrett Wollman error = 0; 1120fe2eee82SColin Percival xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO); 112198271db4SGarrett Wollman for (i = 0; i < n; i++) { 112298271db4SGarrett Wollman unp = unp_list[i]; 112398271db4SGarrett Wollman if (unp->unp_gencnt <= gencnt) { 11248f364875SJulian Elischer xu->xu_len = sizeof *xu; 11258f364875SJulian Elischer xu->xu_unpp = unp; 112698271db4SGarrett Wollman /* 112798271db4SGarrett Wollman * XXX - need more locking here to protect against 112898271db4SGarrett Wollman * connect/disconnect races for SMP. 112998271db4SGarrett Wollman */ 1130fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 11318f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr, 113298271db4SGarrett Wollman unp->unp_addr->sun_len); 1133fc3fcacfSRobert Watson if (unp->unp_conn != NULL && 1134fc3fcacfSRobert Watson unp->unp_conn->unp_addr != NULL) 113598271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 11368f364875SJulian Elischer &xu->xu_caddr, 113798271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 11388f364875SJulian Elischer bcopy(unp, &xu->xu_unp, sizeof *unp); 11398f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket); 11408f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu); 114198271db4SGarrett Wollman } 114298271db4SGarrett Wollman } 11438f364875SJulian Elischer free(xu, M_TEMP); 114498271db4SGarrett Wollman if (!error) { 114598271db4SGarrett Wollman /* 11461c381b19SRobert Watson * Give the user an updated idea of our state. If the 11471c381b19SRobert Watson * generation differs from what we told her before, she knows 11481c381b19SRobert Watson * that something happened while we were processing this 11491c381b19SRobert Watson * request, and it might be necessary to retry. 115098271db4SGarrett Wollman */ 11518f364875SJulian Elischer xug->xug_gen = unp_gencnt; 11528f364875SJulian Elischer xug->xug_sogen = so_gencnt; 11538f364875SJulian Elischer xug->xug_count = unp_count; 11548f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 115598271db4SGarrett Wollman } 115698271db4SGarrett Wollman free(unp_list, M_TEMP); 11578f364875SJulian Elischer free(xug, M_TEMP); 1158e5aeaa0cSDag-Erling Smørgrav return (error); 115998271db4SGarrett Wollman } 116098271db4SGarrett Wollman 116198271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 116298271db4SGarrett Wollman (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 116398271db4SGarrett Wollman "List of active local datagram sockets"); 116498271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 116598271db4SGarrett Wollman (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 116698271db4SGarrett Wollman "List of active local stream sockets"); 116798271db4SGarrett Wollman 1168f708ef1bSPoul-Henning Kamp static void 1169892af6b9SRobert Watson unp_shutdown(struct unpcb *unp) 1170df8bae1dSRodney W. Grimes { 1171df8bae1dSRodney W. Grimes struct socket *so; 1172df8bae1dSRodney W. Grimes 11730d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 11740d9ce3a1SRobert Watson 1175df8bae1dSRodney W. Grimes if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 1176df8bae1dSRodney W. Grimes (so = unp->unp_conn->unp_socket)) 1177df8bae1dSRodney W. Grimes socantrcvmore(so); 1178df8bae1dSRodney W. Grimes } 1179df8bae1dSRodney W. Grimes 1180f708ef1bSPoul-Henning Kamp static void 1181892af6b9SRobert Watson unp_drop(struct unpcb *unp, int errno) 1182df8bae1dSRodney W. Grimes { 1183df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 1184df8bae1dSRodney W. Grimes 11850d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 11860d9ce3a1SRobert Watson 1187df8bae1dSRodney W. Grimes so->so_error = errno; 1188df8bae1dSRodney W. Grimes unp_disconnect(unp); 1189df8bae1dSRodney W. Grimes } 1190df8bae1dSRodney W. Grimes 11912bc21ed9SDavid Malone static void 1192892af6b9SRobert Watson unp_freerights(struct file **rp, int fdcount) 1193df8bae1dSRodney W. Grimes { 11942bc21ed9SDavid Malone int i; 11952bc21ed9SDavid Malone struct file *fp; 1196df8bae1dSRodney W. Grimes 11972bc21ed9SDavid Malone for (i = 0; i < fdcount; i++) { 1198df8bae1dSRodney W. Grimes fp = *rp; 11998692c025SYoshinobu Inoue /* 12001c381b19SRobert Watson * Zero the pointer before calling unp_discard since it may 12011c381b19SRobert Watson * end up in unp_gc().. 1202d7dca903SRobert Watson * 1203d7dca903SRobert Watson * XXXRW: This is less true than it used to be. 12048692c025SYoshinobu Inoue */ 1205df8bae1dSRodney W. Grimes *rp++ = 0; 12068692c025SYoshinobu Inoue unp_discard(fp); 1207df8bae1dSRodney W. Grimes } 12082bc21ed9SDavid Malone } 12092bc21ed9SDavid Malone 12102bc21ed9SDavid Malone int 1211892af6b9SRobert Watson unp_externalize(struct mbuf *control, struct mbuf **controlp) 12122bc21ed9SDavid Malone { 12132bc21ed9SDavid Malone struct thread *td = curthread; /* XXX */ 12142bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 12152bc21ed9SDavid Malone int i; 12162bc21ed9SDavid Malone int *fdp; 12172bc21ed9SDavid Malone struct file **rp; 12182bc21ed9SDavid Malone struct file *fp; 12192bc21ed9SDavid Malone void *data; 12202bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 12212bc21ed9SDavid Malone int error, newfds; 12222bc21ed9SDavid Malone int f; 12232bc21ed9SDavid Malone u_int newlen; 12242bc21ed9SDavid Malone 12254c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 12264c5bc1caSRobert Watson 12272bc21ed9SDavid Malone error = 0; 12282bc21ed9SDavid Malone if (controlp != NULL) /* controlp == NULL => free control messages */ 12292bc21ed9SDavid Malone *controlp = NULL; 12302bc21ed9SDavid Malone 12312bc21ed9SDavid Malone while (cm != NULL) { 12322bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 12332bc21ed9SDavid Malone error = EINVAL; 12342bc21ed9SDavid Malone break; 12352bc21ed9SDavid Malone } 12362bc21ed9SDavid Malone 12372bc21ed9SDavid Malone data = CMSG_DATA(cm); 12382bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 12392bc21ed9SDavid Malone 12402bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET 12412bc21ed9SDavid Malone && cm->cmsg_type == SCM_RIGHTS) { 12422bc21ed9SDavid Malone newfds = datalen / sizeof(struct file *); 12432bc21ed9SDavid Malone rp = data; 12442bc21ed9SDavid Malone 1245e2f9a08bSOlivier Houchard /* If we're not outputting the descriptors free them. */ 12462bc21ed9SDavid Malone if (error || controlp == NULL) { 12472bc21ed9SDavid Malone unp_freerights(rp, newfds); 12482bc21ed9SDavid Malone goto next; 12492bc21ed9SDavid Malone } 1250426da3bcSAlfred Perlstein FILEDESC_LOCK(td->td_proc->p_fd); 12512bc21ed9SDavid Malone /* if the new FD's will not fit free them. */ 12522bc21ed9SDavid Malone if (!fdavail(td, newfds)) { 1253426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 12542bc21ed9SDavid Malone error = EMSGSIZE; 12552bc21ed9SDavid Malone unp_freerights(rp, newfds); 12562bc21ed9SDavid Malone goto next; 1257df8bae1dSRodney W. Grimes } 1258ed5b7817SJulian Elischer /* 12591c381b19SRobert Watson * Now change each pointer to an fd in the global 12601c381b19SRobert Watson * table to an integer that is the index to the local 12611c381b19SRobert Watson * fd table entry that we set up to point to the 12621c381b19SRobert Watson * global one we are transferring. 1263ed5b7817SJulian Elischer */ 12642bc21ed9SDavid Malone newlen = newfds * sizeof(int); 12652bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 12662bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 12672bc21ed9SDavid Malone if (*controlp == NULL) { 1268426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 12692bc21ed9SDavid Malone error = E2BIG; 12702bc21ed9SDavid Malone unp_freerights(rp, newfds); 12712bc21ed9SDavid Malone goto next; 12722bc21ed9SDavid Malone } 12732bc21ed9SDavid Malone 12742bc21ed9SDavid Malone fdp = (int *) 12752bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1276df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 1277a6d4491cSDag-Erling Smørgrav if (fdalloc(td, 0, &f)) 12782bc21ed9SDavid Malone panic("unp_externalize fdalloc failed"); 12798692c025SYoshinobu Inoue fp = *rp++; 1280b40ce416SJulian Elischer td->td_proc->p_fd->fd_ofiles[f] = fp; 1281426da3bcSAlfred Perlstein FILE_LOCK(fp); 1282df8bae1dSRodney W. Grimes fp->f_msgcount--; 1283426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1284df8bae1dSRodney W. Grimes unp_rights--; 12858692c025SYoshinobu Inoue *fdp++ = f; 1286df8bae1dSRodney W. Grimes } 1287426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 12881c381b19SRobert Watson } else { 12891c381b19SRobert Watson /* We can just copy anything else across. */ 12902bc21ed9SDavid Malone if (error || controlp == NULL) 12912bc21ed9SDavid Malone goto next; 12922bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, datalen, 12932bc21ed9SDavid Malone cm->cmsg_type, cm->cmsg_level); 12942bc21ed9SDavid Malone if (*controlp == NULL) { 12952bc21ed9SDavid Malone error = ENOBUFS; 12962bc21ed9SDavid Malone goto next; 12972bc21ed9SDavid Malone } 12982bc21ed9SDavid Malone bcopy(data, 12992bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 13002bc21ed9SDavid Malone datalen); 13012bc21ed9SDavid Malone } 13022bc21ed9SDavid Malone 13032bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 13042bc21ed9SDavid Malone 13052bc21ed9SDavid Malone next: 13062bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 13072bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 13082bc21ed9SDavid Malone cm = (struct cmsghdr *) 13092bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 13108692c025SYoshinobu Inoue } else { 13112bc21ed9SDavid Malone clen = 0; 13122bc21ed9SDavid Malone cm = NULL; 13138692c025SYoshinobu Inoue } 13148692c025SYoshinobu Inoue } 13158692c025SYoshinobu Inoue 13162bc21ed9SDavid Malone m_freem(control); 13172bc21ed9SDavid Malone 13182bc21ed9SDavid Malone return (error); 1319df8bae1dSRodney W. Grimes } 1320df8bae1dSRodney W. Grimes 13214f590175SPaul Saab static void 13224f590175SPaul Saab unp_zone_change(void *tag) 13234f590175SPaul Saab { 13244f590175SPaul Saab 13254f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets); 13264f590175SPaul Saab } 13274f590175SPaul Saab 132898271db4SGarrett Wollman void 132998271db4SGarrett Wollman unp_init(void) 133098271db4SGarrett Wollman { 13311c381b19SRobert Watson 13329e9d298aSJeff Roberson unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 13339e9d298aSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 1334fc3fcacfSRobert Watson if (unp_zone == NULL) 133598271db4SGarrett Wollman panic("unp_init"); 13364f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets); 13374f590175SPaul Saab EVENTHANDLER_REGISTER(maxsockets_change, unp_zone_change, 13384f590175SPaul Saab NULL, EVENTHANDLER_PRI_ANY); 133998271db4SGarrett Wollman LIST_INIT(&unp_dhead); 134098271db4SGarrett Wollman LIST_INIT(&unp_shead); 1341a0ec558aSRobert Watson TASK_INIT(&unp_gc_task, 0, unp_gc, NULL); 13420d9ce3a1SRobert Watson UNP_LOCK_INIT(); 134398271db4SGarrett Wollman } 134498271db4SGarrett Wollman 1345f708ef1bSPoul-Henning Kamp static int 1346892af6b9SRobert Watson unp_internalize(struct mbuf **controlp, struct thread *td) 1347df8bae1dSRodney W. Grimes { 13482bc21ed9SDavid Malone struct mbuf *control = *controlp; 1349b40ce416SJulian Elischer struct proc *p = td->td_proc; 13508692c025SYoshinobu Inoue struct filedesc *fdescp = p->p_fd; 13512bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 13522bc21ed9SDavid Malone struct cmsgcred *cmcred; 13532bc21ed9SDavid Malone struct file **rp; 13542bc21ed9SDavid Malone struct file *fp; 13552bc21ed9SDavid Malone struct timeval *tv; 13562bc21ed9SDavid Malone int i, fd, *fdp; 13572bc21ed9SDavid Malone void *data; 13582bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 13592bc21ed9SDavid Malone int error, oldfds; 13608692c025SYoshinobu Inoue u_int newlen; 1361df8bae1dSRodney W. Grimes 13624c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 13634c5bc1caSRobert Watson 13642bc21ed9SDavid Malone error = 0; 13652bc21ed9SDavid Malone *controlp = NULL; 13660b788fa1SBill Paul 13672bc21ed9SDavid Malone while (cm != NULL) { 13682bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 13692bc21ed9SDavid Malone || cm->cmsg_len > clen) { 13702bc21ed9SDavid Malone error = EINVAL; 13712bc21ed9SDavid Malone goto out; 13722bc21ed9SDavid Malone } 13732bc21ed9SDavid Malone 13742bc21ed9SDavid Malone data = CMSG_DATA(cm); 13752bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 13762bc21ed9SDavid Malone 13772bc21ed9SDavid Malone switch (cm->cmsg_type) { 13780b788fa1SBill Paul /* 13790b788fa1SBill Paul * Fill in credential information. 13800b788fa1SBill Paul */ 13812bc21ed9SDavid Malone case SCM_CREDS: 13822bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 13832bc21ed9SDavid Malone SCM_CREDS, SOL_SOCKET); 13842bc21ed9SDavid Malone if (*controlp == NULL) { 13852bc21ed9SDavid Malone error = ENOBUFS; 13862bc21ed9SDavid Malone goto out; 13872bc21ed9SDavid Malone } 13882bc21ed9SDavid Malone 13892bc21ed9SDavid Malone cmcred = (struct cmsgcred *) 13902bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 13910b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 1392a854ed98SJohn Baldwin cmcred->cmcred_uid = td->td_ucred->cr_ruid; 1393a854ed98SJohn Baldwin cmcred->cmcred_gid = td->td_ucred->cr_rgid; 1394a854ed98SJohn Baldwin cmcred->cmcred_euid = td->td_ucred->cr_uid; 1395a854ed98SJohn Baldwin cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 13960b788fa1SBill Paul CMGROUP_MAX); 13970b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 13982bc21ed9SDavid Malone cmcred->cmcred_groups[i] = 1399a854ed98SJohn Baldwin td->td_ucred->cr_groups[i]; 14002bc21ed9SDavid Malone break; 14010b788fa1SBill Paul 14022bc21ed9SDavid Malone case SCM_RIGHTS: 14032bc21ed9SDavid Malone oldfds = datalen / sizeof (int); 1404ed5b7817SJulian Elischer /* 14051c381b19SRobert Watson * Check that all the FDs passed in refer to legal 14061c381b19SRobert Watson * files. If not, reject the entire operation. 1407ed5b7817SJulian Elischer */ 14082bc21ed9SDavid Malone fdp = data; 1409426da3bcSAlfred Perlstein FILEDESC_LOCK(fdescp); 1410df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 14118692c025SYoshinobu Inoue fd = *fdp++; 14128692c025SYoshinobu Inoue if ((unsigned)fd >= fdescp->fd_nfiles || 14132bc21ed9SDavid Malone fdescp->fd_ofiles[fd] == NULL) { 1414426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 14152bc21ed9SDavid Malone error = EBADF; 14162bc21ed9SDavid Malone goto out; 14172bc21ed9SDavid Malone } 1418e7d6662fSAlfred Perlstein fp = fdescp->fd_ofiles[fd]; 1419e7d6662fSAlfred Perlstein if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 1420e7d6662fSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 1421e7d6662fSAlfred Perlstein error = EOPNOTSUPP; 1422e7d6662fSAlfred Perlstein goto out; 1423e7d6662fSAlfred Perlstein } 1424e7d6662fSAlfred Perlstein 1425df8bae1dSRodney W. Grimes } 1426ed5b7817SJulian Elischer /* 14271c381b19SRobert Watson * Now replace the integer FDs with pointers to the 14281c381b19SRobert Watson * associated global file table entry.. 1429ed5b7817SJulian Elischer */ 14302bc21ed9SDavid Malone newlen = oldfds * sizeof(struct file *); 14312bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 14322bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 14332bc21ed9SDavid Malone if (*controlp == NULL) { 1434426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 14352bc21ed9SDavid Malone error = E2BIG; 14362bc21ed9SDavid Malone goto out; 14378692c025SYoshinobu Inoue } 14388692c025SYoshinobu Inoue 14392bc21ed9SDavid Malone fdp = data; 14402bc21ed9SDavid Malone rp = (struct file **) 14412bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 14428692c025SYoshinobu Inoue for (i = 0; i < oldfds; i++) { 14438692c025SYoshinobu Inoue fp = fdescp->fd_ofiles[*fdp++]; 1444df8bae1dSRodney W. Grimes *rp++ = fp; 1445426da3bcSAlfred Perlstein FILE_LOCK(fp); 1446df8bae1dSRodney W. Grimes fp->f_count++; 1447df8bae1dSRodney W. Grimes fp->f_msgcount++; 1448426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1449df8bae1dSRodney W. Grimes unp_rights++; 1450df8bae1dSRodney W. Grimes } 1451426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 14522bc21ed9SDavid Malone break; 14532bc21ed9SDavid Malone 14542bc21ed9SDavid Malone case SCM_TIMESTAMP: 14552bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*tv), 14562bc21ed9SDavid Malone SCM_TIMESTAMP, SOL_SOCKET); 14572bc21ed9SDavid Malone if (*controlp == NULL) { 14582bc21ed9SDavid Malone error = ENOBUFS; 14592bc21ed9SDavid Malone goto out; 14608692c025SYoshinobu Inoue } 14612bc21ed9SDavid Malone tv = (struct timeval *) 14622bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 14632bc21ed9SDavid Malone microtime(tv); 14642bc21ed9SDavid Malone break; 14652bc21ed9SDavid Malone 14662bc21ed9SDavid Malone default: 14672bc21ed9SDavid Malone error = EINVAL; 14682bc21ed9SDavid Malone goto out; 14692bc21ed9SDavid Malone } 14702bc21ed9SDavid Malone 14712bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 14722bc21ed9SDavid Malone 14732bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 14742bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 14752bc21ed9SDavid Malone cm = (struct cmsghdr *) 14762bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 14772bc21ed9SDavid Malone } else { 14782bc21ed9SDavid Malone clen = 0; 14792bc21ed9SDavid Malone cm = NULL; 14802bc21ed9SDavid Malone } 14812bc21ed9SDavid Malone } 14822bc21ed9SDavid Malone 14832bc21ed9SDavid Malone out: 14842bc21ed9SDavid Malone m_freem(control); 14852bc21ed9SDavid Malone 14862bc21ed9SDavid Malone return (error); 1487df8bae1dSRodney W. Grimes } 1488df8bae1dSRodney W. Grimes 14896a2989fdSMatthew N. Dodd struct mbuf * 14906a2989fdSMatthew N. Dodd unp_addsockcred(struct thread *td, struct mbuf *control) 14916a2989fdSMatthew N. Dodd { 149270df31f4SMaxim Konovalov struct mbuf *m, *n, *n_prev; 14936a2989fdSMatthew N. Dodd struct sockcred *sc; 149470df31f4SMaxim Konovalov const struct cmsghdr *cm; 14956a2989fdSMatthew N. Dodd int ngroups; 14966a2989fdSMatthew N. Dodd int i; 14976a2989fdSMatthew N. Dodd 14986a2989fdSMatthew N. Dodd ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX); 14996a2989fdSMatthew N. Dodd 15006a2989fdSMatthew N. Dodd m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET); 15016a2989fdSMatthew N. Dodd if (m == NULL) 15026a2989fdSMatthew N. Dodd return (control); 15036a2989fdSMatthew N. Dodd 15046a2989fdSMatthew N. Dodd sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *)); 15056a2989fdSMatthew N. Dodd sc->sc_uid = td->td_ucred->cr_ruid; 15066a2989fdSMatthew N. Dodd sc->sc_euid = td->td_ucred->cr_uid; 15076a2989fdSMatthew N. Dodd sc->sc_gid = td->td_ucred->cr_rgid; 15086a2989fdSMatthew N. Dodd sc->sc_egid = td->td_ucred->cr_gid; 15096a2989fdSMatthew N. Dodd sc->sc_ngroups = ngroups; 15106a2989fdSMatthew N. Dodd for (i = 0; i < sc->sc_ngroups; i++) 15116a2989fdSMatthew N. Dodd sc->sc_groups[i] = td->td_ucred->cr_groups[i]; 15126a2989fdSMatthew N. Dodd 15136a2989fdSMatthew N. Dodd /* 15141c381b19SRobert Watson * Unlink SCM_CREDS control messages (struct cmsgcred), since just 15151c381b19SRobert Watson * created SCM_CREDS control message (struct sockcred) has another 15161c381b19SRobert Watson * format. 15176a2989fdSMatthew N. Dodd */ 151870df31f4SMaxim Konovalov if (control != NULL) 151970df31f4SMaxim Konovalov for (n = control, n_prev = NULL; n != NULL;) { 152070df31f4SMaxim Konovalov cm = mtod(n, struct cmsghdr *); 152170df31f4SMaxim Konovalov if (cm->cmsg_level == SOL_SOCKET && 152270df31f4SMaxim Konovalov cm->cmsg_type == SCM_CREDS) { 152370df31f4SMaxim Konovalov if (n_prev == NULL) 152470df31f4SMaxim Konovalov control = n->m_next; 152570df31f4SMaxim Konovalov else 152670df31f4SMaxim Konovalov n_prev->m_next = n->m_next; 152770df31f4SMaxim Konovalov n = m_free(n); 152870df31f4SMaxim Konovalov } else { 152970df31f4SMaxim Konovalov n_prev = n; 153070df31f4SMaxim Konovalov n = n->m_next; 153170df31f4SMaxim Konovalov } 153270df31f4SMaxim Konovalov } 15336a2989fdSMatthew N. Dodd 153470df31f4SMaxim Konovalov /* Prepend it to the head. */ 153570df31f4SMaxim Konovalov m->m_next = control; 153670df31f4SMaxim Konovalov 153770df31f4SMaxim Konovalov return (m); 15386a2989fdSMatthew N. Dodd } 15396a2989fdSMatthew N. Dodd 1540161a0c7cSRobert Watson /* 1541a0ec558aSRobert Watson * unp_defer indicates whether additional work has been defered for a future 1542a0ec558aSRobert Watson * pass through unp_gc(). It is thread local and does not require explicit 1543a0ec558aSRobert Watson * synchronization. 1544161a0c7cSRobert Watson */ 1545a0ec558aSRobert Watson static int unp_defer; 1546a0ec558aSRobert Watson 1547a0ec558aSRobert Watson static int unp_taskcount; 1548a0ec558aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0, ""); 1549a0ec558aSRobert Watson 1550a0ec558aSRobert Watson static int unp_recycled; 1551a0ec558aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0, ""); 1552df8bae1dSRodney W. Grimes 1553f708ef1bSPoul-Henning Kamp static void 1554a0ec558aSRobert Watson unp_gc(__unused void *arg, int pending) 1555df8bae1dSRodney W. Grimes { 1556892af6b9SRobert Watson struct file *fp, *nextfp; 1557892af6b9SRobert Watson struct socket *so; 1558df8bae1dSRodney W. Grimes struct file **extra_ref, **fpp; 1559df8bae1dSRodney W. Grimes int nunref, i; 156095f004dcSAlfred Perlstein int nfiles_snap; 156195f004dcSAlfred Perlstein int nfiles_slack = 20; 1562df8bae1dSRodney W. Grimes 1563a0ec558aSRobert Watson unp_taskcount++; 1564df8bae1dSRodney W. Grimes unp_defer = 0; 1565ed5b7817SJulian Elischer /* 15661c381b19SRobert Watson * Before going through all this, set all FDs to be NOT defered and 15671c381b19SRobert Watson * NOT externally accessible. 1568ed5b7817SJulian Elischer */ 1569426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 15702e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) 1571426da3bcSAlfred Perlstein fp->f_gcflag &= ~(FMARK|FDEFER); 1572df8bae1dSRodney W. Grimes do { 15735bb84bc8SRobert Watson KASSERT(unp_defer >= 0, ("unp_gc: unp_defer %d", unp_defer)); 15742e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) { 1575426da3bcSAlfred Perlstein FILE_LOCK(fp); 1576ed5b7817SJulian Elischer /* 1577a0ec558aSRobert Watson * If the file is not open, skip it -- could be a 1578a0ec558aSRobert Watson * file in the process of being opened, or in the 1579a0ec558aSRobert Watson * process of being closed. If the file is 1580a0ec558aSRobert Watson * "closing", it may have been marked for deferred 1581a0ec558aSRobert Watson * consideration. Clear the flag now if so. 1582ed5b7817SJulian Elischer */ 1583426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1584a0ec558aSRobert Watson if (fp->f_gcflag & FDEFER) 1585a0ec558aSRobert Watson unp_defer--; 1586a0ec558aSRobert Watson fp->f_gcflag &= ~(FMARK|FDEFER); 1587426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1588df8bae1dSRodney W. Grimes continue; 1589426da3bcSAlfred Perlstein } 1590ed5b7817SJulian Elischer /* 15911c381b19SRobert Watson * If we already marked it as 'defer' in a previous 15921c381b19SRobert Watson * pass, then try process it this time and un-mark 15931c381b19SRobert Watson * it. 1594ed5b7817SJulian Elischer */ 1595426da3bcSAlfred Perlstein if (fp->f_gcflag & FDEFER) { 1596426da3bcSAlfred Perlstein fp->f_gcflag &= ~FDEFER; 1597df8bae1dSRodney W. Grimes unp_defer--; 1598df8bae1dSRodney W. Grimes } else { 1599ed5b7817SJulian Elischer /* 1600ed5b7817SJulian Elischer * if it's not defered, then check if it's 1601ed5b7817SJulian Elischer * already marked.. if so skip it 1602ed5b7817SJulian Elischer */ 1603426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) { 1604426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1605df8bae1dSRodney W. Grimes continue; 1606426da3bcSAlfred Perlstein } 1607ed5b7817SJulian Elischer /* 16081c381b19SRobert Watson * If all references are from messages in 16091c381b19SRobert Watson * transit, then skip it. it's not externally 16101c381b19SRobert Watson * accessible. 1611ed5b7817SJulian Elischer */ 1612426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount) { 1613426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1614df8bae1dSRodney W. Grimes continue; 1615426da3bcSAlfred Perlstein } 1616ed5b7817SJulian Elischer /* 1617ed5b7817SJulian Elischer * If it got this far then it must be 1618ed5b7817SJulian Elischer * externally accessible. 1619ed5b7817SJulian Elischer */ 1620426da3bcSAlfred Perlstein fp->f_gcflag |= FMARK; 1621df8bae1dSRodney W. Grimes } 1622ed5b7817SJulian Elischer /* 16231c381b19SRobert Watson * Either it was defered, or it is externally 16241c381b19SRobert Watson * accessible and not already marked so. Now check 16251c381b19SRobert Watson * if it is possibly one of OUR sockets. 1626ed5b7817SJulian Elischer */ 1627df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_SOCKET || 162848e3128bSMatthew Dillon (so = fp->f_data) == NULL) { 1629426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1630df8bae1dSRodney W. Grimes continue; 1631426da3bcSAlfred Perlstein } 1632426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1633748e0b0aSGarrett Wollman if (so->so_proto->pr_domain != &localdomain || 1634df8bae1dSRodney W. Grimes (so->so_proto->pr_flags&PR_RIGHTS) == 0) 1635df8bae1dSRodney W. Grimes continue; 1636ed5b7817SJulian Elischer /* 16371c381b19SRobert Watson * So, Ok, it's one of our sockets and it IS 16381c381b19SRobert Watson * externally accessible (or was defered). Now we 16391c381b19SRobert Watson * look to see if we hold any file descriptors in its 1640ed5b7817SJulian Elischer * message buffers. Follow those links and mark them 1641ed5b7817SJulian Elischer * as accessible too. 1642ed5b7817SJulian Elischer */ 16437717cf07SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1644df8bae1dSRodney W. Grimes unp_scan(so->so_rcv.sb_mb, unp_mark); 16457717cf07SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 1646df8bae1dSRodney W. Grimes } 1647df8bae1dSRodney W. Grimes } while (unp_defer); 1648426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1649df8bae1dSRodney W. Grimes /* 1650a0ec558aSRobert Watson * XXXRW: The following comments need updating for a post-SMPng and 1651a0ec558aSRobert Watson * deferred unp_gc() world, but are still generally accurate. 1652a0ec558aSRobert Watson * 16531c381b19SRobert Watson * We grab an extra reference to each of the file table entries that 16541c381b19SRobert Watson * are not otherwise accessible and then free the rights that are 16551c381b19SRobert Watson * stored in messages on them. 1656df8bae1dSRodney W. Grimes * 1657df8bae1dSRodney W. Grimes * The bug in the orginal code is a little tricky, so I'll describe 1658df8bae1dSRodney W. Grimes * what's wrong with it here. 1659df8bae1dSRodney W. Grimes * 1660df8bae1dSRodney W. Grimes * It is incorrect to simply unp_discard each entry for f_msgcount 1661df8bae1dSRodney W. Grimes * times -- consider the case of sockets A and B that contain 1662df8bae1dSRodney W. Grimes * references to each other. On a last close of some other socket, 1663df8bae1dSRodney W. Grimes * we trigger a gc since the number of outstanding rights (unp_rights) 1664a0ec558aSRobert Watson * is non-zero. If during the sweep phase the gc code unp_discards, 1665df8bae1dSRodney W. Grimes * we end up doing a (full) closef on the descriptor. A closef on A 1666df8bae1dSRodney W. Grimes * results in the following chain. Closef calls soo_close, which 1667df8bae1dSRodney W. Grimes * calls soclose. Soclose calls first (through the switch 1668df8bae1dSRodney W. Grimes * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 16691c381b19SRobert Watson * returns because the previous instance had set unp_gcing, and we 16701c381b19SRobert Watson * return all the way back to soclose, which marks the socket with 16711c381b19SRobert Watson * SS_NOFDREF, and then calls sofree. Sofree calls sorflush to free 16721c381b19SRobert Watson * up the rights that are queued in messages on the socket A, i.e., 16731c381b19SRobert Watson * the reference on B. The sorflush calls via the dom_dispose switch 16741c381b19SRobert Watson * unp_dispose, which unp_scans with unp_discard. This second 1675df8bae1dSRodney W. Grimes * instance of unp_discard just calls closef on B. 1676df8bae1dSRodney W. Grimes * 1677df8bae1dSRodney W. Grimes * Well, a similar chain occurs on B, resulting in a sorflush on B, 1678df8bae1dSRodney W. Grimes * which results in another closef on A. Unfortunately, A is already 1679df8bae1dSRodney W. Grimes * being closed, and the descriptor has already been marked with 1680df8bae1dSRodney W. Grimes * SS_NOFDREF, and soclose panics at this point. 1681df8bae1dSRodney W. Grimes * 1682df8bae1dSRodney W. Grimes * Here, we first take an extra reference to each inaccessible 16831c381b19SRobert Watson * descriptor. Then, we call sorflush ourself, since we know it is a 16841c381b19SRobert Watson * Unix domain socket anyhow. After we destroy all the rights 16851c381b19SRobert Watson * carried in messages, we do a last closef to get rid of our extra 16861c381b19SRobert Watson * reference. This is the last close, and the unp_detach etc will 16871c381b19SRobert Watson * shut down the socket. 1688df8bae1dSRodney W. Grimes * 1689df8bae1dSRodney W. Grimes * 91/09/19, bsy@cs.cmu.edu 1690df8bae1dSRodney W. Grimes */ 169195f004dcSAlfred Perlstein again: 1692e4643c73SPoul-Henning Kamp nfiles_snap = openfiles + nfiles_slack; /* some slack */ 169395f004dcSAlfred Perlstein extra_ref = malloc(nfiles_snap * sizeof(struct file *), M_TEMP, 169495f004dcSAlfred Perlstein M_WAITOK); 1695426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 1696e4643c73SPoul-Henning Kamp if (nfiles_snap < openfiles) { 169795f004dcSAlfred Perlstein sx_sunlock(&filelist_lock); 169895f004dcSAlfred Perlstein free(extra_ref, M_TEMP); 169995f004dcSAlfred Perlstein nfiles_slack += 20; 170095f004dcSAlfred Perlstein goto again; 170195f004dcSAlfred Perlstein } 1702fc3fcacfSRobert Watson for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; 1703fc3fcacfSRobert Watson fp != NULL; fp = nextfp) { 17042e3c8fcbSPoul-Henning Kamp nextfp = LIST_NEXT(fp, f_list); 1705426da3bcSAlfred Perlstein FILE_LOCK(fp); 1706ed5b7817SJulian Elischer /* 1707ed5b7817SJulian Elischer * If it's not open, skip it 1708ed5b7817SJulian Elischer */ 1709426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1710426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1711df8bae1dSRodney W. Grimes continue; 1712426da3bcSAlfred Perlstein } 1713ed5b7817SJulian Elischer /* 1714ed5b7817SJulian Elischer * If all refs are from msgs, and it's not marked accessible 17151c381b19SRobert Watson * then it must be referenced from some unreachable cycle of 17161c381b19SRobert Watson * (shut-down) FDs, so include it in our list of FDs to 17171c381b19SRobert Watson * remove. 1718ed5b7817SJulian Elischer */ 1719426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) { 1720df8bae1dSRodney W. Grimes *fpp++ = fp; 1721df8bae1dSRodney W. Grimes nunref++; 1722df8bae1dSRodney W. Grimes fp->f_count++; 1723df8bae1dSRodney W. Grimes } 1724426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1725df8bae1dSRodney W. Grimes } 1726426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1727ed5b7817SJulian Elischer /* 17281c381b19SRobert Watson * For each FD on our hit list, do the following two things: 1729ed5b7817SJulian Elischer */ 17301c7c3c6aSMatthew Dillon for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 17311c7c3c6aSMatthew Dillon struct file *tfp = *fpp; 1732426da3bcSAlfred Perlstein FILE_LOCK(tfp); 1733cd72f218SMatthew Dillon if (tfp->f_type == DTYPE_SOCKET && 173448e3128bSMatthew Dillon tfp->f_data != NULL) { 1735426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 173648e3128bSMatthew Dillon sorflush(tfp->f_data); 1737e5aeaa0cSDag-Erling Smørgrav } else { 1738426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 17391c7c3c6aSMatthew Dillon } 1740e5aeaa0cSDag-Erling Smørgrav } 1741a0ec558aSRobert Watson for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 1742b40ce416SJulian Elischer closef(*fpp, (struct thread *) NULL); 1743a0ec558aSRobert Watson unp_recycled++; 1744a0ec558aSRobert Watson } 1745210a5a71SAlfred Perlstein free(extra_ref, M_TEMP); 1746df8bae1dSRodney W. Grimes } 1747df8bae1dSRodney W. Grimes 174826f9a767SRodney W. Grimes void 1749892af6b9SRobert Watson unp_dispose(struct mbuf *m) 1750df8bae1dSRodney W. Grimes { 1751996c772fSJohn Dyson 1752df8bae1dSRodney W. Grimes if (m) 1753df8bae1dSRodney W. Grimes unp_scan(m, unp_discard); 1754df8bae1dSRodney W. Grimes } 1755df8bae1dSRodney W. Grimes 17560c1bb4fbSDima Dorfman static int 1757d374e81eSRobert Watson unp_listen(struct socket *so, struct unpcb *unp, int backlog, 1758d374e81eSRobert Watson struct thread *td) 17590c1bb4fbSDima Dorfman { 17600daccb9cSRobert Watson int error; 17610daccb9cSRobert Watson 17620d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 17630c1bb4fbSDima Dorfman 17640daccb9cSRobert Watson SOCK_LOCK(so); 17650daccb9cSRobert Watson error = solisten_proto_check(so); 17660daccb9cSRobert Watson if (error == 0) { 17676f105b34SJohn Baldwin cru2x(td->td_ucred, &unp->unp_peercred); 17680c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPCCACHED; 1769d374e81eSRobert Watson solisten_proto(so, backlog); 17700daccb9cSRobert Watson } 17710daccb9cSRobert Watson SOCK_UNLOCK(so); 17720daccb9cSRobert Watson return (error); 17730c1bb4fbSDima Dorfman } 17740c1bb4fbSDima Dorfman 1775f708ef1bSPoul-Henning Kamp static void 1776892af6b9SRobert Watson unp_scan(struct mbuf *m0, void (*op)(struct file *)) 1777df8bae1dSRodney W. Grimes { 17782bc21ed9SDavid Malone struct mbuf *m; 17792bc21ed9SDavid Malone struct file **rp; 17802bc21ed9SDavid Malone struct cmsghdr *cm; 17812bc21ed9SDavid Malone void *data; 17822bc21ed9SDavid Malone int i; 17832bc21ed9SDavid Malone socklen_t clen, datalen; 1784df8bae1dSRodney W. Grimes int qfds; 1785df8bae1dSRodney W. Grimes 1786fc3fcacfSRobert Watson while (m0 != NULL) { 17872bc21ed9SDavid Malone for (m = m0; m; m = m->m_next) { 178812396bdcSDavid Malone if (m->m_type != MT_CONTROL) 1789df8bae1dSRodney W. Grimes continue; 17902bc21ed9SDavid Malone 17912bc21ed9SDavid Malone cm = mtod(m, struct cmsghdr *); 17922bc21ed9SDavid Malone clen = m->m_len; 17932bc21ed9SDavid Malone 17942bc21ed9SDavid Malone while (cm != NULL) { 17952bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) 17962bc21ed9SDavid Malone break; 17972bc21ed9SDavid Malone 17982bc21ed9SDavid Malone data = CMSG_DATA(cm); 17992bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len 18002bc21ed9SDavid Malone - (caddr_t)data; 18012bc21ed9SDavid Malone 18022bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET && 18032bc21ed9SDavid Malone cm->cmsg_type == SCM_RIGHTS) { 18042bc21ed9SDavid Malone qfds = datalen / sizeof (struct file *); 18052bc21ed9SDavid Malone rp = data; 1806df8bae1dSRodney W. Grimes for (i = 0; i < qfds; i++) 1807df8bae1dSRodney W. Grimes (*op)(*rp++); 18082bc21ed9SDavid Malone } 18092bc21ed9SDavid Malone 18102bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 18112bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 18122bc21ed9SDavid Malone cm = (struct cmsghdr *) 18132bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 18142bc21ed9SDavid Malone } else { 18152bc21ed9SDavid Malone clen = 0; 18162bc21ed9SDavid Malone cm = NULL; 18172bc21ed9SDavid Malone } 18182bc21ed9SDavid Malone } 1819df8bae1dSRodney W. Grimes } 1820df8bae1dSRodney W. Grimes m0 = m0->m_act; 1821df8bae1dSRodney W. Grimes } 1822df8bae1dSRodney W. Grimes } 1823df8bae1dSRodney W. Grimes 1824f708ef1bSPoul-Henning Kamp static void 1825892af6b9SRobert Watson unp_mark(struct file *fp) 1826df8bae1dSRodney W. Grimes { 1827426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) 1828df8bae1dSRodney W. Grimes return; 1829df8bae1dSRodney W. Grimes unp_defer++; 1830426da3bcSAlfred Perlstein fp->f_gcflag |= (FMARK|FDEFER); 1831df8bae1dSRodney W. Grimes } 1832df8bae1dSRodney W. Grimes 1833f708ef1bSPoul-Henning Kamp static void 1834892af6b9SRobert Watson unp_discard(struct file *fp) 1835df8bae1dSRodney W. Grimes { 1836a0ec558aSRobert Watson UNP_LOCK(); 1837426da3bcSAlfred Perlstein FILE_LOCK(fp); 1838df8bae1dSRodney W. Grimes fp->f_msgcount--; 1839df8bae1dSRodney W. Grimes unp_rights--; 1840426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1841a0ec558aSRobert Watson UNP_UNLOCK(); 1842b40ce416SJulian Elischer (void) closef(fp, (struct thread *)NULL); 1843df8bae1dSRodney W. Grimes } 1844