19454b2d8SWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1991, 1993 3e1ac28e2SRobert Watson * The Regents of the University of California. 44d4b555eSRobert Watson * Copyright 2004-2006 Robert N. M. Watson 5e1ac28e2SRobert Watson * All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 8df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 9df8bae1dSRodney W. Grimes * are met: 10df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 12df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 14df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 15df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 16df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 17df8bae1dSRodney W. Grimes * without specific prior written permission. 18df8bae1dSRodney W. Grimes * 19df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29df8bae1dSRodney W. Grimes * SUCH DAMAGE. 30df8bae1dSRodney W. Grimes * 31748e0b0aSGarrett Wollman * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94 32df8bae1dSRodney W. Grimes */ 33df8bae1dSRodney W. Grimes 34f23929fbSRobert Watson /* 35f23929fbSRobert Watson * UNIX Domain (Local) Sockets 36f23929fbSRobert Watson * 37f23929fbSRobert Watson * This is an implementation of UNIX (local) domain sockets. Each socket has 38f23929fbSRobert Watson * an associated struct unpcb (UNIX protocol control block). Stream sockets 39f23929fbSRobert Watson * may be connected to 0 or 1 other socket. Datagram sockets may be 40f23929fbSRobert Watson * connected to 0, 1, or many other sockets. Sockets may be created and 41f23929fbSRobert Watson * connected in pairs (socketpair(2)), or bound/connected to using the file 42f23929fbSRobert Watson * system name space. For most purposes, only the receive socket buffer is 43f23929fbSRobert Watson * used, as sending on one socket delivers directly to the receive socket 44f23929fbSRobert Watson * buffer of a second socket. The implementation is substantially 45f23929fbSRobert Watson * complicated by the fact that "ancillary data", such as file descriptors or 46f23929fbSRobert Watson * or credentials, may be passed across UNIX domain sockets. The potential 47f23929fbSRobert Watson * for passing UNIX domain sockets over other UNIX domain sockets requires 48f23929fbSRobert Watson * the implementation of a simple garbage collector to find and tear down 49f23929fbSRobert Watson * cycles of disconnected sockets. 50f23929fbSRobert Watson */ 51f23929fbSRobert Watson 52677b542eSDavid E. O'Brien #include <sys/cdefs.h> 53677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 54677b542eSDavid E. O'Brien 55335654d7SRobert Watson #include "opt_mac.h" 56335654d7SRobert Watson 57df8bae1dSRodney W. Grimes #include <sys/param.h> 58fb919e4dSMark Murray #include <sys/domain.h> 59960ed29cSSeigo Tanimura #include <sys/fcntl.h> 60d826c479SBruce Evans #include <sys/malloc.h> /* XXX must be before <sys/file.h> */ 614f590175SPaul Saab #include <sys/eventhandler.h> 62639acc13SGarrett Wollman #include <sys/file.h> 63960ed29cSSeigo Tanimura #include <sys/filedesc.h> 64960ed29cSSeigo Tanimura #include <sys/jail.h> 65960ed29cSSeigo Tanimura #include <sys/kernel.h> 66960ed29cSSeigo Tanimura #include <sys/lock.h> 676ea48a90SRobert Watson #include <sys/mac.h> 68639acc13SGarrett Wollman #include <sys/mbuf.h> 69033eb86eSJeff Roberson #include <sys/mount.h> 70960ed29cSSeigo Tanimura #include <sys/mutex.h> 71639acc13SGarrett Wollman #include <sys/namei.h> 72639acc13SGarrett Wollman #include <sys/proc.h> 73df8bae1dSRodney W. Grimes #include <sys/protosw.h> 74960ed29cSSeigo Tanimura #include <sys/resourcevar.h> 75df8bae1dSRodney W. Grimes #include <sys/socket.h> 76df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 77960ed29cSSeigo Tanimura #include <sys/signalvar.h> 78df8bae1dSRodney W. Grimes #include <sys/stat.h> 79960ed29cSSeigo Tanimura #include <sys/sx.h> 80639acc13SGarrett Wollman #include <sys/sysctl.h> 81960ed29cSSeigo Tanimura #include <sys/systm.h> 82a0ec558aSRobert Watson #include <sys/taskqueue.h> 83639acc13SGarrett Wollman #include <sys/un.h> 8498271db4SGarrett Wollman #include <sys/unpcb.h> 85639acc13SGarrett Wollman #include <sys/vnode.h> 86df8bae1dSRodney W. Grimes 879e9d298aSJeff Roberson #include <vm/uma.h> 8898271db4SGarrett Wollman 899e9d298aSJeff Roberson static uma_zone_t unp_zone; 9098271db4SGarrett Wollman static unp_gen_t unp_gencnt; 9198271db4SGarrett Wollman static u_int unp_count; 9298271db4SGarrett Wollman 9398271db4SGarrett Wollman static struct unp_head unp_shead, unp_dhead; 9498271db4SGarrett Wollman 95df8bae1dSRodney W. Grimes /* 96df8bae1dSRodney W. Grimes * Unix communications domain. 97df8bae1dSRodney W. Grimes * 98df8bae1dSRodney W. Grimes * TODO: 99df8bae1dSRodney W. Grimes * SEQPACKET, RDM 100df8bae1dSRodney W. Grimes * rethink name space problems 101df8bae1dSRodney W. Grimes * need a proper out-of-band 10298271db4SGarrett Wollman * lock pushdown 103df8bae1dSRodney W. Grimes */ 104e7dd9a10SRobert Watson static const struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 105f708ef1bSPoul-Henning Kamp static ino_t unp_ino; /* prototype for fake inode numbers */ 1066a2989fdSMatthew N. Dodd struct mbuf *unp_addsockcred(struct thread *, struct mbuf *); 107f708ef1bSPoul-Henning Kamp 108ce5f32deSRobert Watson /* 1097e711c3aSRobert Watson * Both send and receive buffers are allocated PIPSIZ bytes of buffering for 1107e711c3aSRobert Watson * stream sockets, although the total for sender and receiver is actually 1117e711c3aSRobert Watson * only PIPSIZ. 1127e711c3aSRobert Watson * 1137e711c3aSRobert Watson * Datagram sockets really use the sendspace as the maximum datagram size, 1147e711c3aSRobert Watson * and don't really want to reserve the sendspace. Their recvspace should be 1157e711c3aSRobert Watson * large enough for at least one max-size datagram plus address. 1167e711c3aSRobert Watson */ 1177e711c3aSRobert Watson #ifndef PIPSIZ 1187e711c3aSRobert Watson #define PIPSIZ 8192 1197e711c3aSRobert Watson #endif 1207e711c3aSRobert Watson static u_long unpst_sendspace = PIPSIZ; 1217e711c3aSRobert Watson static u_long unpst_recvspace = PIPSIZ; 1227e711c3aSRobert Watson static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 1237e711c3aSRobert Watson static u_long unpdg_recvspace = 4*1024; 1247e711c3aSRobert Watson 1257e711c3aSRobert Watson static int unp_rights; /* file descriptors in flight */ 1267e711c3aSRobert Watson 1277e711c3aSRobert Watson SYSCTL_DECL(_net_local_stream); 1287e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 1297e711c3aSRobert Watson &unpst_sendspace, 0, ""); 1307e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 1317e711c3aSRobert Watson &unpst_recvspace, 0, ""); 1327e711c3aSRobert Watson SYSCTL_DECL(_net_local_dgram); 1337e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 1347e711c3aSRobert Watson &unpdg_sendspace, 0, ""); 1357e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 1367e711c3aSRobert Watson &unpdg_recvspace, 0, ""); 1377e711c3aSRobert Watson SYSCTL_DECL(_net_local); 1387e711c3aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); 1397e711c3aSRobert Watson 1407e711c3aSRobert Watson /* 141ce5f32deSRobert Watson * Currently, UNIX domain sockets are protected by a single subsystem lock, 142ce5f32deSRobert Watson * which covers global data structures and variables, the contents of each 143ce5f32deSRobert Watson * per-socket unpcb structure, and the so_pcb field in sockets attached to 144ce5f32deSRobert Watson * the UNIX domain. This provides for a moderate degree of paralellism, as 145ce5f32deSRobert Watson * receive operations on UNIX domain sockets do not need to acquire the 146ce5f32deSRobert Watson * subsystem lock. Finer grained locking to permit send() without acquiring 147ce5f32deSRobert Watson * a global lock would be a logical next step. 148ce5f32deSRobert Watson * 149ce5f32deSRobert Watson * The UNIX domain socket lock preceds all socket layer locks, including the 150ce5f32deSRobert Watson * socket lock and socket buffer lock, permitting UNIX domain socket code to 151ce5f32deSRobert Watson * call into socket support routines without releasing its locks. 152ce5f32deSRobert Watson * 153ce5f32deSRobert Watson * Some caution is required in areas where the UNIX domain socket code enters 154ce5f32deSRobert Watson * VFS in order to create or find rendezvous points. This results in 155ce5f32deSRobert Watson * dropping of the UNIX domain socket subsystem lock, acquisition of the 156ce5f32deSRobert Watson * Giant lock, and potential sleeping. This increases the chances of races, 157ce5f32deSRobert Watson * and exposes weaknesses in the socket->protocol API by offering poor 158ce5f32deSRobert Watson * failure modes. 159ce5f32deSRobert Watson */ 1600d9ce3a1SRobert Watson static struct mtx unp_mtx; 1610d9ce3a1SRobert Watson #define UNP_LOCK_INIT() \ 1620d9ce3a1SRobert Watson mtx_init(&unp_mtx, "unp", NULL, MTX_DEF) 1630d9ce3a1SRobert Watson #define UNP_LOCK() mtx_lock(&unp_mtx) 1640d9ce3a1SRobert Watson #define UNP_UNLOCK() mtx_unlock(&unp_mtx) 1650d9ce3a1SRobert Watson #define UNP_LOCK_ASSERT() mtx_assert(&unp_mtx, MA_OWNED) 1664c5bc1caSRobert Watson #define UNP_UNLOCK_ASSERT() mtx_assert(&unp_mtx, MA_NOTOWNED) 1670d9ce3a1SRobert Watson 168a0ec558aSRobert Watson /* 169a0ec558aSRobert Watson * Garbage collection of cyclic file descriptor/socket references occurs 170a0ec558aSRobert Watson * asynchronously in a taskqueue context in order to avoid recursion and 171a0ec558aSRobert Watson * reentrance in the UNIX domain socket, file descriptor, and socket layer 172a0ec558aSRobert Watson * code. See unp_gc() for a full description. 173a0ec558aSRobert Watson */ 174a0ec558aSRobert Watson static struct task unp_gc_task; 175a0ec558aSRobert Watson 17670f52b48SBruce Evans static int unp_connect(struct socket *,struct sockaddr *, struct thread *); 1776a2989fdSMatthew N. Dodd static int unp_connect2(struct socket *so, struct socket *so2, int); 1784d77a549SAlfred Perlstein static void unp_disconnect(struct unpcb *); 1794d77a549SAlfred Perlstein static void unp_shutdown(struct unpcb *); 1804d77a549SAlfred Perlstein static void unp_drop(struct unpcb *, int); 181a0ec558aSRobert Watson static void unp_gc(__unused void *, int); 1824d77a549SAlfred Perlstein static void unp_scan(struct mbuf *, void (*)(struct file *)); 1834d77a549SAlfred Perlstein static void unp_mark(struct file *); 1844d77a549SAlfred Perlstein static void unp_discard(struct file *); 1854d77a549SAlfred Perlstein static void unp_freerights(struct file **, int); 1864d77a549SAlfred Perlstein static int unp_internalize(struct mbuf **, struct thread *); 187d374e81eSRobert Watson static int unp_listen(struct socket *, struct unpcb *, int, 188d374e81eSRobert Watson struct thread *); 189f708ef1bSPoul-Henning Kamp 190ac45e92fSRobert Watson static void 191a29f300eSGarrett Wollman uipc_abort(struct socket *so) 192df8bae1dSRodney W. Grimes { 19340f2ac28SRobert Watson struct unpcb *unp; 194df8bae1dSRodney W. Grimes 19540f2ac28SRobert Watson unp = sotounpcb(so); 1964d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_abort: unp == NULL")); 1974d4b555eSRobert Watson UNP_LOCK(); 198a29f300eSGarrett Wollman unp_drop(unp, ECONNABORTED); 199a152f8a3SRobert Watson UNP_UNLOCK(); 200df8bae1dSRodney W. Grimes } 201df8bae1dSRodney W. Grimes 202a29f300eSGarrett Wollman static int 20357bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam) 204a29f300eSGarrett Wollman { 20540f2ac28SRobert Watson struct unpcb *unp; 2060d9ce3a1SRobert Watson const struct sockaddr *sa; 207df8bae1dSRodney W. Grimes 208df8bae1dSRodney W. Grimes /* 2091c381b19SRobert Watson * Pass back name of connected socket, if it was bound and we are 2101c381b19SRobert Watson * still connected (our peer may have closed already!). 211df8bae1dSRodney W. Grimes */ 2124d4b555eSRobert Watson unp = sotounpcb(so); 2134d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_accept: unp == NULL")); 2140d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 2150d9ce3a1SRobert Watson UNP_LOCK(); 2160d9ce3a1SRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr != NULL) 2170d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 2180d9ce3a1SRobert Watson else 2190d9ce3a1SRobert Watson sa = &sun_noname; 2200d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 2210d9ce3a1SRobert Watson UNP_UNLOCK(); 222e5aeaa0cSDag-Erling Smørgrav return (0); 223a29f300eSGarrett Wollman } 224df8bae1dSRodney W. Grimes 225a29f300eSGarrett Wollman static int 226b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td) 227a29f300eSGarrett Wollman { 2286d32873cSRobert Watson struct unpcb *unp; 2296d32873cSRobert Watson int error; 230df8bae1dSRodney W. Grimes 2316d32873cSRobert Watson KASSERT(so->so_pcb == NULL, ("uipc_attach: so_pcb != NULL")); 2326d32873cSRobert Watson if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 2336d32873cSRobert Watson switch (so->so_type) { 2346d32873cSRobert Watson 2356d32873cSRobert Watson case SOCK_STREAM: 2366d32873cSRobert Watson error = soreserve(so, unpst_sendspace, unpst_recvspace); 2376d32873cSRobert Watson break; 2386d32873cSRobert Watson 2396d32873cSRobert Watson case SOCK_DGRAM: 2406d32873cSRobert Watson error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 2416d32873cSRobert Watson break; 2426d32873cSRobert Watson 2436d32873cSRobert Watson default: 2446d32873cSRobert Watson panic("unp_attach"); 2456d32873cSRobert Watson } 2466d32873cSRobert Watson if (error) 2476d32873cSRobert Watson return (error); 2486d32873cSRobert Watson } 2496d32873cSRobert Watson unp = uma_zalloc(unp_zone, M_WAITOK | M_ZERO); 2506d32873cSRobert Watson if (unp == NULL) 2516d32873cSRobert Watson return (ENOBUFS); 2526d32873cSRobert Watson LIST_INIT(&unp->unp_refs); 2536d32873cSRobert Watson unp->unp_socket = so; 2546d32873cSRobert Watson so->so_pcb = unp; 2556d32873cSRobert Watson 2566d32873cSRobert Watson UNP_LOCK(); 2576d32873cSRobert Watson unp->unp_gencnt = ++unp_gencnt; 2586d32873cSRobert Watson unp_count++; 2596d32873cSRobert Watson LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead 2606d32873cSRobert Watson : &unp_shead, unp, unp_link); 2616d32873cSRobert Watson UNP_UNLOCK(); 2626d32873cSRobert Watson 2636d32873cSRobert Watson return (0); 264a29f300eSGarrett Wollman } 265a29f300eSGarrett Wollman 266a29f300eSGarrett Wollman static int 267b40ce416SJulian Elischer uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 268a29f300eSGarrett Wollman { 269dd47f5caSRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam; 270dd47f5caSRobert Watson struct vattr vattr; 271dd47f5caSRobert Watson int error, namelen; 272dd47f5caSRobert Watson struct nameidata nd; 27340f2ac28SRobert Watson struct unpcb *unp; 274dd47f5caSRobert Watson struct vnode *vp; 275dd47f5caSRobert Watson struct mount *mp; 276dd47f5caSRobert Watson char *buf; 277a29f300eSGarrett Wollman 27840f2ac28SRobert Watson unp = sotounpcb(so); 2794d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_bind: unp == NULL")); 2804f1f0ef5SRobert Watson 2814f1f0ef5SRobert Watson namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 2824f1f0ef5SRobert Watson if (namelen <= 0) 2834f1f0ef5SRobert Watson return (EINVAL); 284dd47f5caSRobert Watson 285dd47f5caSRobert Watson /* 2864f1f0ef5SRobert Watson * We don't allow simultaneous bind() calls on a single UNIX domain 2874f1f0ef5SRobert Watson * socket, so flag in-progress operations, and return an error if an 2884f1f0ef5SRobert Watson * operation is already in progress. 2894f1f0ef5SRobert Watson * 2904f1f0ef5SRobert Watson * Historically, we have not allowed a socket to be rebound, so this 2914f1f0ef5SRobert Watson * also returns an error. Not allowing re-binding certainly 2924f1f0ef5SRobert Watson * simplifies the implementation and avoids a great many possible 2934f1f0ef5SRobert Watson * failure modes. 294dd47f5caSRobert Watson */ 2954f1f0ef5SRobert Watson UNP_LOCK(); 296dd47f5caSRobert Watson if (unp->unp_vnode != NULL) { 29740f2ac28SRobert Watson UNP_UNLOCK(); 298dd47f5caSRobert Watson return (EINVAL); 299dd47f5caSRobert Watson } 3004f1f0ef5SRobert Watson if (unp->unp_flags & UNP_BINDING) { 301dd47f5caSRobert Watson UNP_UNLOCK(); 3024f1f0ef5SRobert Watson return (EALREADY); 303dd47f5caSRobert Watson } 3044f1f0ef5SRobert Watson unp->unp_flags |= UNP_BINDING; 305dd47f5caSRobert Watson UNP_UNLOCK(); 306dd47f5caSRobert Watson 307dd47f5caSRobert Watson buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 308dd47f5caSRobert Watson strlcpy(buf, soun->sun_path, namelen + 1); 309dd47f5caSRobert Watson 310dd47f5caSRobert Watson mtx_lock(&Giant); 311dd47f5caSRobert Watson restart: 312dd47f5caSRobert Watson mtx_assert(&Giant, MA_OWNED); 313dd47f5caSRobert Watson NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME, UIO_SYSSPACE, 314dd47f5caSRobert Watson buf, td); 315dd47f5caSRobert Watson /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 316dd47f5caSRobert Watson error = namei(&nd); 317dd47f5caSRobert Watson if (error) 3184f1f0ef5SRobert Watson goto error; 319dd47f5caSRobert Watson vp = nd.ni_vp; 320dd47f5caSRobert Watson if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 321dd47f5caSRobert Watson NDFREE(&nd, NDF_ONLY_PNBUF); 322dd47f5caSRobert Watson if (nd.ni_dvp == vp) 323dd47f5caSRobert Watson vrele(nd.ni_dvp); 324dd47f5caSRobert Watson else 325dd47f5caSRobert Watson vput(nd.ni_dvp); 326dd47f5caSRobert Watson if (vp != NULL) { 327dd47f5caSRobert Watson vrele(vp); 328dd47f5caSRobert Watson error = EADDRINUSE; 3294f1f0ef5SRobert Watson goto error; 330dd47f5caSRobert Watson } 331dd47f5caSRobert Watson error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 332dd47f5caSRobert Watson if (error) 3334f1f0ef5SRobert Watson goto error; 334dd47f5caSRobert Watson goto restart; 335dd47f5caSRobert Watson } 336dd47f5caSRobert Watson VATTR_NULL(&vattr); 337dd47f5caSRobert Watson vattr.va_type = VSOCK; 338dd47f5caSRobert Watson vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 339dd47f5caSRobert Watson #ifdef MAC 340dd47f5caSRobert Watson error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 341dd47f5caSRobert Watson &vattr); 342dd47f5caSRobert Watson #endif 343dd47f5caSRobert Watson if (error == 0) { 344dd47f5caSRobert Watson VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE); 345dd47f5caSRobert Watson error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 346dd47f5caSRobert Watson } 347dd47f5caSRobert Watson NDFREE(&nd, NDF_ONLY_PNBUF); 348dd47f5caSRobert Watson vput(nd.ni_dvp); 349dd47f5caSRobert Watson if (error) { 350dd47f5caSRobert Watson vn_finished_write(mp); 3514f1f0ef5SRobert Watson goto error; 352dd47f5caSRobert Watson } 353dd47f5caSRobert Watson vp = nd.ni_vp; 3544f1f0ef5SRobert Watson ASSERT_VOP_LOCKED(vp, "uipc_bind"); 355dd47f5caSRobert Watson soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 356dd47f5caSRobert Watson UNP_LOCK(); 357dd47f5caSRobert Watson vp->v_socket = unp->unp_socket; 358dd47f5caSRobert Watson unp->unp_vnode = vp; 359dd47f5caSRobert Watson unp->unp_addr = soun; 3604f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_BINDING; 361dd47f5caSRobert Watson UNP_UNLOCK(); 362dd47f5caSRobert Watson VOP_UNLOCK(vp, 0, td); 363dd47f5caSRobert Watson vn_finished_write(mp); 3644f1f0ef5SRobert Watson mtx_unlock(&Giant); 3654f1f0ef5SRobert Watson free(buf, M_TEMP); 3664f1f0ef5SRobert Watson return (0); 3674f1f0ef5SRobert Watson error: 3684f1f0ef5SRobert Watson UNP_LOCK(); 3694f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_BINDING; 3704f1f0ef5SRobert Watson UNP_UNLOCK(); 371dd47f5caSRobert Watson mtx_unlock(&Giant); 372dd47f5caSRobert Watson free(buf, M_TEMP); 37340f2ac28SRobert Watson return (error); 374a29f300eSGarrett Wollman } 375a29f300eSGarrett Wollman 376a29f300eSGarrett Wollman static int 377b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 378a29f300eSGarrett Wollman { 3790d9ce3a1SRobert Watson int error; 380a29f300eSGarrett Wollman 381fd179ee9SRobert Watson KASSERT(td == curthread, ("uipc_connect: td != curthread")); 3824d4b555eSRobert Watson UNP_LOCK(); 383fd179ee9SRobert Watson error = unp_connect(so, nam, td); 3840d9ce3a1SRobert Watson UNP_UNLOCK(); 3850d9ce3a1SRobert Watson return (error); 386a29f300eSGarrett Wollman } 387a29f300eSGarrett Wollman 388a152f8a3SRobert Watson /* 389a152f8a3SRobert Watson * XXXRW: Should also unbind? 390a152f8a3SRobert Watson */ 391a152f8a3SRobert Watson static void 392a152f8a3SRobert Watson uipc_close(struct socket *so) 393a152f8a3SRobert Watson { 394a152f8a3SRobert Watson struct unpcb *unp; 395a152f8a3SRobert Watson 396a152f8a3SRobert Watson unp = sotounpcb(so); 397a152f8a3SRobert Watson KASSERT(unp != NULL, ("uipc_close: unp == NULL")); 398a152f8a3SRobert Watson UNP_LOCK(); 399a152f8a3SRobert Watson unp_disconnect(unp); 400a152f8a3SRobert Watson UNP_UNLOCK(); 401a152f8a3SRobert Watson } 402a152f8a3SRobert Watson 403db48c0d2SRobert Watson int 404a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2) 405a29f300eSGarrett Wollman { 40640f2ac28SRobert Watson struct unpcb *unp; 4070d9ce3a1SRobert Watson int error; 408a29f300eSGarrett Wollman 40940f2ac28SRobert Watson unp = sotounpcb(so1); 4104d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_connect2: unp == NULL")); 4114d4b555eSRobert Watson UNP_LOCK(); 4126a2989fdSMatthew N. Dodd error = unp_connect2(so1, so2, PRU_CONNECT2); 4130d9ce3a1SRobert Watson UNP_UNLOCK(); 4140d9ce3a1SRobert Watson return (error); 415a29f300eSGarrett Wollman } 416a29f300eSGarrett Wollman 417a29f300eSGarrett Wollman /* control is EOPNOTSUPP */ 418a29f300eSGarrett Wollman 419bc725eafSRobert Watson static void 420a29f300eSGarrett Wollman uipc_detach(struct socket *so) 421a29f300eSGarrett Wollman { 4226d32873cSRobert Watson int local_unp_rights; 42340f2ac28SRobert Watson struct unpcb *unp; 4246d32873cSRobert Watson struct vnode *vp; 425a29f300eSGarrett Wollman 42640f2ac28SRobert Watson unp = sotounpcb(so); 4274d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_detach: unp == NULL")); 4284d4b555eSRobert Watson UNP_LOCK(); 4296d32873cSRobert Watson LIST_REMOVE(unp, unp_link); 4306d32873cSRobert Watson unp->unp_gencnt = ++unp_gencnt; 4316d32873cSRobert Watson --unp_count; 4326d32873cSRobert Watson if ((vp = unp->unp_vnode) != NULL) { 4336d32873cSRobert Watson /* 4346d32873cSRobert Watson * XXXRW: should v_socket be frobbed only while holding 4356d32873cSRobert Watson * Giant? 4366d32873cSRobert Watson */ 4376d32873cSRobert Watson unp->unp_vnode->v_socket = NULL; 4386d32873cSRobert Watson unp->unp_vnode = NULL; 4396d32873cSRobert Watson } 4406d32873cSRobert Watson if (unp->unp_conn != NULL) 4416d32873cSRobert Watson unp_disconnect(unp); 4426d32873cSRobert Watson while (!LIST_EMPTY(&unp->unp_refs)) { 4436d32873cSRobert Watson struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 4446d32873cSRobert Watson unp_drop(ref, ECONNRESET); 4456d32873cSRobert Watson } 4466d32873cSRobert Watson soisdisconnected(unp->unp_socket); 4476d32873cSRobert Watson unp->unp_socket->so_pcb = NULL; 4486d32873cSRobert Watson local_unp_rights = unp_rights; 4496d32873cSRobert Watson UNP_UNLOCK(); 4506d32873cSRobert Watson if (unp->unp_addr != NULL) 4516d32873cSRobert Watson FREE(unp->unp_addr, M_SONAME); 4526d32873cSRobert Watson uma_zfree(unp_zone, unp); 4536d32873cSRobert Watson if (vp) { 4546d32873cSRobert Watson int vfslocked; 4556d32873cSRobert Watson 4566d32873cSRobert Watson vfslocked = VFS_LOCK_GIANT(vp->v_mount); 4576d32873cSRobert Watson vrele(vp); 4586d32873cSRobert Watson VFS_UNLOCK_GIANT(vfslocked); 4596d32873cSRobert Watson } 4606d32873cSRobert Watson if (local_unp_rights) 4616d32873cSRobert Watson taskqueue_enqueue(taskqueue_thread, &unp_gc_task); 462a29f300eSGarrett Wollman } 463a29f300eSGarrett Wollman 464a29f300eSGarrett Wollman static int 465a29f300eSGarrett Wollman uipc_disconnect(struct socket *so) 466a29f300eSGarrett Wollman { 46740f2ac28SRobert Watson struct unpcb *unp; 468a29f300eSGarrett Wollman 46940f2ac28SRobert Watson unp = sotounpcb(so); 4704d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_disconnect: unp == NULL")); 4714d4b555eSRobert Watson UNP_LOCK(); 472a29f300eSGarrett Wollman unp_disconnect(unp); 4730d9ce3a1SRobert Watson UNP_UNLOCK(); 474e5aeaa0cSDag-Erling Smørgrav return (0); 475a29f300eSGarrett Wollman } 476a29f300eSGarrett Wollman 477a29f300eSGarrett Wollman static int 478d374e81eSRobert Watson uipc_listen(struct socket *so, int backlog, struct thread *td) 479a29f300eSGarrett Wollman { 48040f2ac28SRobert Watson struct unpcb *unp; 4810d9ce3a1SRobert Watson int error; 482a29f300eSGarrett Wollman 48340f2ac28SRobert Watson unp = sotounpcb(so); 4844d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_listen: unp == NULL")); 4854d4b555eSRobert Watson UNP_LOCK(); 4864d4b555eSRobert Watson if (unp->unp_vnode == NULL) { 48740f2ac28SRobert Watson UNP_UNLOCK(); 48840f2ac28SRobert Watson return (EINVAL); 48940f2ac28SRobert Watson } 490d374e81eSRobert Watson error = unp_listen(so, unp, backlog, td); 4910d9ce3a1SRobert Watson UNP_UNLOCK(); 4920d9ce3a1SRobert Watson return (error); 493a29f300eSGarrett Wollman } 494a29f300eSGarrett Wollman 495a29f300eSGarrett Wollman static int 49657bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 497a29f300eSGarrett Wollman { 49840f2ac28SRobert Watson struct unpcb *unp; 4990d9ce3a1SRobert Watson const struct sockaddr *sa; 500a29f300eSGarrett Wollman 5014d4b555eSRobert Watson unp = sotounpcb(so); 5024d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL")); 5030d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 5040d9ce3a1SRobert Watson UNP_LOCK(); 505fc3fcacfSRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr!= NULL) 5060d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 507bdc5f6a3SHajimu UMEMOTO else { 508bdc5f6a3SHajimu UMEMOTO /* 509bdc5f6a3SHajimu UMEMOTO * XXX: It seems that this test always fails even when 510bdc5f6a3SHajimu UMEMOTO * connection is established. So, this else clause is 511bdc5f6a3SHajimu UMEMOTO * added as workaround to return PF_LOCAL sockaddr. 512bdc5f6a3SHajimu UMEMOTO */ 5130d9ce3a1SRobert Watson sa = &sun_noname; 514bdc5f6a3SHajimu UMEMOTO } 5150d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 5160d9ce3a1SRobert Watson UNP_UNLOCK(); 517e5aeaa0cSDag-Erling Smørgrav return (0); 518a29f300eSGarrett Wollman } 519a29f300eSGarrett Wollman 520a29f300eSGarrett Wollman static int 521a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 522a29f300eSGarrett Wollman { 52340f2ac28SRobert Watson struct unpcb *unp; 524a29f300eSGarrett Wollman struct socket *so2; 525337cc6b6SRobert Watson u_int mbcnt, sbcc; 5266aef685fSBrian Feldman u_long newhiwat; 527a29f300eSGarrett Wollman 52840f2ac28SRobert Watson unp = sotounpcb(so); 5294d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_rcvd: unp == NULL")); 530df8bae1dSRodney W. Grimes switch (so->so_type) { 531df8bae1dSRodney W. Grimes case SOCK_DGRAM: 532a29f300eSGarrett Wollman panic("uipc_rcvd DGRAM?"); 533df8bae1dSRodney W. Grimes /*NOTREACHED*/ 534df8bae1dSRodney W. Grimes 535df8bae1dSRodney W. Grimes case SOCK_STREAM: 536df8bae1dSRodney W. Grimes /* 5371c381b19SRobert Watson * Adjust backpressure on sender and wakeup any waiting to 5381c381b19SRobert Watson * write. 539df8bae1dSRodney W. Grimes */ 540337cc6b6SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 541337cc6b6SRobert Watson mbcnt = so->so_rcv.sb_mbcnt; 542337cc6b6SRobert Watson sbcc = so->so_rcv.sb_cc; 543337cc6b6SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 544337cc6b6SRobert Watson UNP_LOCK(); 545337cc6b6SRobert Watson if (unp->unp_conn == NULL) { 546337cc6b6SRobert Watson UNP_UNLOCK(); 547337cc6b6SRobert Watson break; 548337cc6b6SRobert Watson } 549337cc6b6SRobert Watson so2 = unp->unp_conn->unp_socket; 550337cc6b6SRobert Watson SOCKBUF_LOCK(&so2->so_snd); 551337cc6b6SRobert Watson so2->so_snd.sb_mbmax += unp->unp_mbcnt - mbcnt; 552337cc6b6SRobert Watson newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - sbcc; 553f535380cSDon Lewis (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat, 5546aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 5551e4d7da7SRobert Watson sowwakeup_locked(so2); 556337cc6b6SRobert Watson unp->unp_mbcnt = mbcnt; 557337cc6b6SRobert Watson unp->unp_cc = sbcc; 558337cc6b6SRobert Watson UNP_UNLOCK(); 559df8bae1dSRodney W. Grimes break; 560df8bae1dSRodney W. Grimes 561df8bae1dSRodney W. Grimes default: 562a29f300eSGarrett Wollman panic("uipc_rcvd unknown socktype"); 563df8bae1dSRodney W. Grimes } 564e5aeaa0cSDag-Erling Smørgrav return (0); 565a29f300eSGarrett Wollman } 566df8bae1dSRodney W. Grimes 567a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 568a29f300eSGarrett Wollman 569a29f300eSGarrett Wollman static int 57057bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 571b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 572a29f300eSGarrett Wollman { 573f3f49bbbSRobert Watson struct unpcb *unp, *unp2; 574a29f300eSGarrett Wollman struct socket *so2; 575337cc6b6SRobert Watson u_int mbcnt, sbcc; 5766aef685fSBrian Feldman u_long newhiwat; 577f3f49bbbSRobert Watson int error = 0; 578a29f300eSGarrett Wollman 57940f2ac28SRobert Watson unp = sotounpcb(so); 5804d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_send: unp == NULL")); 581a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 582a29f300eSGarrett Wollman error = EOPNOTSUPP; 583a29f300eSGarrett Wollman goto release; 584a29f300eSGarrett Wollman } 585a29f300eSGarrett Wollman 586fc3fcacfSRobert Watson if (control != NULL && (error = unp_internalize(&control, td))) 587a29f300eSGarrett Wollman goto release; 588df8bae1dSRodney W. Grimes 5890d9ce3a1SRobert Watson UNP_LOCK(); 590a29f300eSGarrett Wollman switch (so->so_type) { 591a29f300eSGarrett Wollman case SOCK_DGRAM: 592a29f300eSGarrett Wollman { 593e7dd9a10SRobert Watson const struct sockaddr *from; 594df8bae1dSRodney W. Grimes 595fc3fcacfSRobert Watson if (nam != NULL) { 596fc3fcacfSRobert Watson if (unp->unp_conn != NULL) { 597df8bae1dSRodney W. Grimes error = EISCONN; 598df8bae1dSRodney W. Grimes break; 599df8bae1dSRodney W. Grimes } 600b40ce416SJulian Elischer error = unp_connect(so, nam, td); 601df8bae1dSRodney W. Grimes if (error) 602df8bae1dSRodney W. Grimes break; 603df8bae1dSRodney W. Grimes } else { 604fc3fcacfSRobert Watson if (unp->unp_conn == NULL) { 605df8bae1dSRodney W. Grimes error = ENOTCONN; 606df8bae1dSRodney W. Grimes break; 607df8bae1dSRodney W. Grimes } 608df8bae1dSRodney W. Grimes } 609f3f49bbbSRobert Watson unp2 = unp->unp_conn; 610f3f49bbbSRobert Watson so2 = unp2->unp_socket; 611fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 61257bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 613df8bae1dSRodney W. Grimes else 614df8bae1dSRodney W. Grimes from = &sun_noname; 615f3f49bbbSRobert Watson if (unp2->unp_flags & UNP_WANTCRED) 6166a2989fdSMatthew N. Dodd control = unp_addsockcred(td, control); 617a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 618a34b7046SRobert Watson if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) { 6191e4d7da7SRobert Watson sorwakeup_locked(so2); 620fc3fcacfSRobert Watson m = NULL; 621fc3fcacfSRobert Watson control = NULL; 622e5aeaa0cSDag-Erling Smørgrav } else { 623a34b7046SRobert Watson SOCKBUF_UNLOCK(&so2->so_rcv); 624df8bae1dSRodney W. Grimes error = ENOBUFS; 625e5aeaa0cSDag-Erling Smørgrav } 626fc3fcacfSRobert Watson if (nam != NULL) 627df8bae1dSRodney W. Grimes unp_disconnect(unp); 628df8bae1dSRodney W. Grimes break; 629df8bae1dSRodney W. Grimes } 630df8bae1dSRodney W. Grimes 631df8bae1dSRodney W. Grimes case SOCK_STREAM: 6326b8fda4dSGarrett Wollman /* 6331c381b19SRobert Watson * Connect if not connected yet. 6341c381b19SRobert Watson * 6351c381b19SRobert Watson * Note: A better implementation would complain if not equal 6361c381b19SRobert Watson * to the peer's address. 6376b8fda4dSGarrett Wollman */ 638402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 639fc3fcacfSRobert Watson if (nam != NULL) { 640b40ce416SJulian Elischer error = unp_connect(so, nam, td); 641402cc72dSDavid Greenman if (error) 6426b8fda4dSGarrett Wollman break; /* XXX */ 643402cc72dSDavid Greenman } else { 644402cc72dSDavid Greenman error = ENOTCONN; 645402cc72dSDavid Greenman break; 646402cc72dSDavid Greenman } 647402cc72dSDavid Greenman } 648402cc72dSDavid Greenman 649337cc6b6SRobert Watson /* Lockless read. */ 650c0b99ffaSRobert Watson if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 651df8bae1dSRodney W. Grimes error = EPIPE; 652df8bae1dSRodney W. Grimes break; 653df8bae1dSRodney W. Grimes } 654f3f49bbbSRobert Watson unp2 = unp->unp_conn; 655f3f49bbbSRobert Watson if (unp2 == NULL) 656a29f300eSGarrett Wollman panic("uipc_send connected but no connection?"); 657f3f49bbbSRobert Watson so2 = unp2->unp_socket; 658a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 659f3f49bbbSRobert Watson if (unp2->unp_flags & UNP_WANTCRED) { 6606a2989fdSMatthew N. Dodd /* 6616a2989fdSMatthew N. Dodd * Credentials are passed only once on 6626a2989fdSMatthew N. Dodd * SOCK_STREAM. 6636a2989fdSMatthew N. Dodd */ 664f3f49bbbSRobert Watson unp2->unp_flags &= ~UNP_WANTCRED; 6656a2989fdSMatthew N. Dodd control = unp_addsockcred(td, control); 6666a2989fdSMatthew N. Dodd } 667df8bae1dSRodney W. Grimes /* 6681c381b19SRobert Watson * Send to paired receive port, and then reduce send buffer 6691c381b19SRobert Watson * hiwater marks to maintain backpressure. Wake up readers. 670df8bae1dSRodney W. Grimes */ 671fc3fcacfSRobert Watson if (control != NULL) { 672a34b7046SRobert Watson if (sbappendcontrol_locked(&so2->so_rcv, m, control)) 673fc3fcacfSRobert Watson control = NULL; 674e5aeaa0cSDag-Erling Smørgrav } else { 675a34b7046SRobert Watson sbappend_locked(&so2->so_rcv, m); 676e5aeaa0cSDag-Erling Smørgrav } 677f3f49bbbSRobert Watson mbcnt = so2->so_rcv.sb_mbcnt - unp2->unp_mbcnt; 678f3f49bbbSRobert Watson unp2->unp_mbcnt = so2->so_rcv.sb_mbcnt; 679337cc6b6SRobert Watson sbcc = so2->so_rcv.sb_cc; 680337cc6b6SRobert Watson sorwakeup_locked(so2); 681337cc6b6SRobert Watson 682337cc6b6SRobert Watson SOCKBUF_LOCK(&so->so_snd); 683f3f49bbbSRobert Watson newhiwat = so->so_snd.sb_hiwat - (sbcc - unp2->unp_cc); 684f535380cSDon Lewis (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat, 6856aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 686337cc6b6SRobert Watson so->so_snd.sb_mbmax -= mbcnt; 6877abe2ac2SAlan Cox SOCKBUF_UNLOCK(&so->so_snd); 688337cc6b6SRobert Watson 689f3f49bbbSRobert Watson unp2->unp_cc = sbcc; 690fc3fcacfSRobert Watson m = NULL; 691df8bae1dSRodney W. Grimes break; 692df8bae1dSRodney W. Grimes 693df8bae1dSRodney W. Grimes default: 694a29f300eSGarrett Wollman panic("uipc_send unknown socktype"); 695df8bae1dSRodney W. Grimes } 696a29f300eSGarrett Wollman 6976b8fda4dSGarrett Wollman /* 6986b8fda4dSGarrett Wollman * SEND_EOF is equivalent to a SEND followed by 6996b8fda4dSGarrett Wollman * a SHUTDOWN. 7006b8fda4dSGarrett Wollman */ 701a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 7026b8fda4dSGarrett Wollman socantsendmore(so); 7036b8fda4dSGarrett Wollman unp_shutdown(unp); 7046b8fda4dSGarrett Wollman } 7050d9ce3a1SRobert Watson UNP_UNLOCK(); 706df8bae1dSRodney W. Grimes 707fc3fcacfSRobert Watson if (control != NULL && error != 0) 708bd508d39SDon Lewis unp_dispose(control); 709bd508d39SDon Lewis 710a29f300eSGarrett Wollman release: 711fc3fcacfSRobert Watson if (control != NULL) 712a29f300eSGarrett Wollman m_freem(control); 713fc3fcacfSRobert Watson if (m != NULL) 714a29f300eSGarrett Wollman m_freem(m); 715e5aeaa0cSDag-Erling Smørgrav return (error); 716a29f300eSGarrett Wollman } 717df8bae1dSRodney W. Grimes 718a29f300eSGarrett Wollman static int 719a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 720a29f300eSGarrett Wollman { 72140f2ac28SRobert Watson struct unpcb *unp; 722a29f300eSGarrett Wollman struct socket *so2; 723a29f300eSGarrett Wollman 72440f2ac28SRobert Watson unp = sotounpcb(so); 7254d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sense: unp == NULL")); 7264d4b555eSRobert Watson UNP_LOCK(); 727a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 728fc3fcacfSRobert Watson if (so->so_type == SOCK_STREAM && unp->unp_conn != NULL) { 729df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 730a29f300eSGarrett Wollman sb->st_blksize += so2->so_rcv.sb_cc; 731df8bae1dSRodney W. Grimes } 732f3732fd1SPoul-Henning Kamp sb->st_dev = NODEV; 733df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 7346f782c46SJeffrey Hsu unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 735a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 7360d9ce3a1SRobert Watson UNP_UNLOCK(); 737df8bae1dSRodney W. Grimes return (0); 738a29f300eSGarrett Wollman } 739df8bae1dSRodney W. Grimes 740a29f300eSGarrett Wollman static int 741a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 742a29f300eSGarrett Wollman { 74340f2ac28SRobert Watson struct unpcb *unp; 744df8bae1dSRodney W. Grimes 74540f2ac28SRobert Watson unp = sotounpcb(so); 7464d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_shutdown: unp == NULL")); 7474d4b555eSRobert Watson UNP_LOCK(); 748a29f300eSGarrett Wollman socantsendmore(so); 749a29f300eSGarrett Wollman unp_shutdown(unp); 7500d9ce3a1SRobert Watson UNP_UNLOCK(); 751e5aeaa0cSDag-Erling Smørgrav return (0); 752a29f300eSGarrett Wollman } 753df8bae1dSRodney W. Grimes 754a29f300eSGarrett Wollman static int 75557bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 756a29f300eSGarrett Wollman { 75740f2ac28SRobert Watson struct unpcb *unp; 7580d9ce3a1SRobert Watson const struct sockaddr *sa; 759a29f300eSGarrett Wollman 7604d4b555eSRobert Watson unp = sotounpcb(so); 7614d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL")); 7620d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 7630d9ce3a1SRobert Watson UNP_LOCK(); 764fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 7650d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_addr; 76683f3198bSThomas Moestl else 7670d9ce3a1SRobert Watson sa = &sun_noname; 7680d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 7690d9ce3a1SRobert Watson UNP_UNLOCK(); 770e5aeaa0cSDag-Erling Smørgrav return (0); 771df8bae1dSRodney W. Grimes } 772a29f300eSGarrett Wollman 773a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = { 774756d52a1SPoul-Henning Kamp .pru_abort = uipc_abort, 775756d52a1SPoul-Henning Kamp .pru_accept = uipc_accept, 776756d52a1SPoul-Henning Kamp .pru_attach = uipc_attach, 777756d52a1SPoul-Henning Kamp .pru_bind = uipc_bind, 778756d52a1SPoul-Henning Kamp .pru_connect = uipc_connect, 779756d52a1SPoul-Henning Kamp .pru_connect2 = uipc_connect2, 780756d52a1SPoul-Henning Kamp .pru_detach = uipc_detach, 781756d52a1SPoul-Henning Kamp .pru_disconnect = uipc_disconnect, 782756d52a1SPoul-Henning Kamp .pru_listen = uipc_listen, 783756d52a1SPoul-Henning Kamp .pru_peeraddr = uipc_peeraddr, 784756d52a1SPoul-Henning Kamp .pru_rcvd = uipc_rcvd, 785756d52a1SPoul-Henning Kamp .pru_send = uipc_send, 786756d52a1SPoul-Henning Kamp .pru_sense = uipc_sense, 787756d52a1SPoul-Henning Kamp .pru_shutdown = uipc_shutdown, 788756d52a1SPoul-Henning Kamp .pru_sockaddr = uipc_sockaddr, 789756d52a1SPoul-Henning Kamp .pru_sosend = sosend, 790756d52a1SPoul-Henning Kamp .pru_soreceive = soreceive, 791756d52a1SPoul-Henning Kamp .pru_sopoll = sopoll, 792a152f8a3SRobert Watson .pru_close = uipc_close, 793a29f300eSGarrett Wollman }; 794df8bae1dSRodney W. Grimes 7950c1bb4fbSDima Dorfman int 796892af6b9SRobert Watson uipc_ctloutput(struct socket *so, struct sockopt *sopt) 7970c1bb4fbSDima Dorfman { 79840f2ac28SRobert Watson struct unpcb *unp; 7990d9ce3a1SRobert Watson struct xucred xu; 8006a2989fdSMatthew N. Dodd int error, optval; 8016a2989fdSMatthew N. Dodd 80296a041b5SMatthew N. Dodd if (sopt->sopt_level != 0) 80396a041b5SMatthew N. Dodd return (EINVAL); 80496a041b5SMatthew N. Dodd 8056a2989fdSMatthew N. Dodd unp = sotounpcb(so); 8064d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL")); 8074d4b555eSRobert Watson UNP_LOCK(); 8086a2989fdSMatthew N. Dodd error = 0; 8090c1bb4fbSDima Dorfman switch (sopt->sopt_dir) { 8100c1bb4fbSDima Dorfman case SOPT_GET: 8110c1bb4fbSDima Dorfman switch (sopt->sopt_name) { 8120c1bb4fbSDima Dorfman case LOCAL_PEERCRED: 8130c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC) 8140d9ce3a1SRobert Watson xu = unp->unp_peercred; 8150c1bb4fbSDima Dorfman else { 8160c1bb4fbSDima Dorfman if (so->so_type == SOCK_STREAM) 8170c1bb4fbSDima Dorfman error = ENOTCONN; 8180c1bb4fbSDima Dorfman else 8190c1bb4fbSDima Dorfman error = EINVAL; 8200c1bb4fbSDima Dorfman } 8210d9ce3a1SRobert Watson if (error == 0) 8220d9ce3a1SRobert Watson error = sooptcopyout(sopt, &xu, sizeof(xu)); 8230c1bb4fbSDima Dorfman break; 8246a2989fdSMatthew N. Dodd case LOCAL_CREDS: 8256a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0; 8266a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 8276a2989fdSMatthew N. Dodd break; 8286a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 8296a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0; 8306a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 8316a2989fdSMatthew N. Dodd break; 8320c1bb4fbSDima Dorfman default: 8330c1bb4fbSDima Dorfman error = EOPNOTSUPP; 8340c1bb4fbSDima Dorfman break; 8350c1bb4fbSDima Dorfman } 8360c1bb4fbSDima Dorfman break; 8370c1bb4fbSDima Dorfman case SOPT_SET: 8386a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 8396a2989fdSMatthew N. Dodd case LOCAL_CREDS: 8406a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 8416a2989fdSMatthew N. Dodd error = sooptcopyin(sopt, &optval, sizeof(optval), 8426a2989fdSMatthew N. Dodd sizeof(optval)); 8436a2989fdSMatthew N. Dodd if (error) 8446a2989fdSMatthew N. Dodd break; 8456a2989fdSMatthew N. Dodd 8466a2989fdSMatthew N. Dodd #define OPTSET(bit) \ 8476a2989fdSMatthew N. Dodd if (optval) \ 8486a2989fdSMatthew N. Dodd unp->unp_flags |= bit; \ 8496a2989fdSMatthew N. Dodd else \ 8506a2989fdSMatthew N. Dodd unp->unp_flags &= ~bit; 8516a2989fdSMatthew N. Dodd 8526a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 8536a2989fdSMatthew N. Dodd case LOCAL_CREDS: 8546a2989fdSMatthew N. Dodd OPTSET(UNP_WANTCRED); 8556a2989fdSMatthew N. Dodd break; 8566a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 8576a2989fdSMatthew N. Dodd OPTSET(UNP_CONNWAIT); 8586a2989fdSMatthew N. Dodd break; 8596a2989fdSMatthew N. Dodd default: 8606a2989fdSMatthew N. Dodd break; 8616a2989fdSMatthew N. Dodd } 8626a2989fdSMatthew N. Dodd break; 8636a2989fdSMatthew N. Dodd #undef OPTSET 8646a2989fdSMatthew N. Dodd default: 8656a2989fdSMatthew N. Dodd error = ENOPROTOOPT; 8666a2989fdSMatthew N. Dodd break; 8676a2989fdSMatthew N. Dodd } 868abb886faSMatthew N. Dodd break; 8690c1bb4fbSDima Dorfman default: 8700c1bb4fbSDima Dorfman error = EOPNOTSUPP; 8710c1bb4fbSDima Dorfman break; 8720c1bb4fbSDima Dorfman } 8736a2989fdSMatthew N. Dodd UNP_UNLOCK(); 8740c1bb4fbSDima Dorfman return (error); 8750c1bb4fbSDima Dorfman } 8760c1bb4fbSDima Dorfman 877f708ef1bSPoul-Henning Kamp static int 878892af6b9SRobert Watson unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 879df8bae1dSRodney W. Grimes { 880892af6b9SRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam; 881892af6b9SRobert Watson struct vnode *vp; 882892af6b9SRobert Watson struct socket *so2, *so3; 883b295bdcdSRobert Watson struct unpcb *unp, *unp2, *unp3; 88457bf258eSGarrett Wollman int error, len; 885df8bae1dSRodney W. Grimes struct nameidata nd; 88657bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 8870d9ce3a1SRobert Watson struct sockaddr *sa; 8880d9ce3a1SRobert Watson 8890d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 890df8bae1dSRodney W. Grimes 8914d4b555eSRobert Watson unp = sotounpcb(so); 8924d4b555eSRobert Watson KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 89357bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 89457bf258eSGarrett Wollman if (len <= 0) 895e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 89655c85568SRobert Drehmel strlcpy(buf, soun->sun_path, len + 1); 8974f1f0ef5SRobert Watson if (unp->unp_flags & UNP_CONNECTING) { 8984f1f0ef5SRobert Watson UNP_UNLOCK(); 8994f1f0ef5SRobert Watson return (EALREADY); 9004f1f0ef5SRobert Watson } 9010d9ce3a1SRobert Watson UNP_UNLOCK(); 9020d9ce3a1SRobert Watson sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 9030d9ce3a1SRobert Watson mtx_lock(&Giant); 904b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td); 905797f2d22SPoul-Henning Kamp error = namei(&nd); 906797f2d22SPoul-Henning Kamp if (error) 9070d9ce3a1SRobert Watson vp = NULL; 9080d9ce3a1SRobert Watson else 909df8bae1dSRodney W. Grimes vp = nd.ni_vp; 9100d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_connect"); 911762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 9120d9ce3a1SRobert Watson if (error) 9130d9ce3a1SRobert Watson goto bad; 9140d9ce3a1SRobert Watson 915df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 916df8bae1dSRodney W. Grimes error = ENOTSOCK; 917df8bae1dSRodney W. Grimes goto bad; 918df8bae1dSRodney W. Grimes } 919a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 920797f2d22SPoul-Henning Kamp if (error) 921df8bae1dSRodney W. Grimes goto bad; 9222260c03dSRobert Watson mtx_unlock(&Giant); 9232260c03dSRobert Watson UNP_LOCK(); 924b295bdcdSRobert Watson unp = sotounpcb(so); 9254d4b555eSRobert Watson KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 926df8bae1dSRodney W. Grimes so2 = vp->v_socket; 927fc3fcacfSRobert Watson if (so2 == NULL) { 928df8bae1dSRodney W. Grimes error = ECONNREFUSED; 9292260c03dSRobert Watson goto bad2; 930df8bae1dSRodney W. Grimes } 931df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 932df8bae1dSRodney W. Grimes error = EPROTOTYPE; 9332260c03dSRobert Watson goto bad2; 934df8bae1dSRodney W. Grimes } 935df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 9360d9ce3a1SRobert Watson if (so2->so_options & SO_ACCEPTCONN) { 9370d9ce3a1SRobert Watson /* 9381c381b19SRobert Watson * NB: drop locks here so unp_attach is entered w/o 9391c381b19SRobert Watson * locks; this avoids a recursive lock of the head 9401c381b19SRobert Watson * and holding sleep locks across a (potentially) 9411c381b19SRobert Watson * blocking malloc. 9420d9ce3a1SRobert Watson */ 9430d9ce3a1SRobert Watson UNP_UNLOCK(); 9440d9ce3a1SRobert Watson so3 = sonewconn(so2, 0); 9450d9ce3a1SRobert Watson UNP_LOCK(); 9460d9ce3a1SRobert Watson } else 9470d9ce3a1SRobert Watson so3 = NULL; 9480d9ce3a1SRobert Watson if (so3 == NULL) { 949df8bae1dSRodney W. Grimes error = ECONNREFUSED; 9500d9ce3a1SRobert Watson goto bad2; 951df8bae1dSRodney W. Grimes } 9520c1bb4fbSDima Dorfman unp = sotounpcb(so); 953df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 954df8bae1dSRodney W. Grimes unp3 = sotounpcb(so3); 9550d9ce3a1SRobert Watson if (unp2->unp_addr != NULL) { 9560d9ce3a1SRobert Watson bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 9570d9ce3a1SRobert Watson unp3->unp_addr = (struct sockaddr_un *) sa; 9580d9ce3a1SRobert Watson sa = NULL; 9590d9ce3a1SRobert Watson } 9600c1bb4fbSDima Dorfman /* 9610c1bb4fbSDima Dorfman * unp_peercred management: 9620c1bb4fbSDima Dorfman * 9631c381b19SRobert Watson * The connecter's (client's) credentials are copied from its 9641c381b19SRobert Watson * process structure at the time of connect() (which is now). 9650c1bb4fbSDima Dorfman */ 966a854ed98SJohn Baldwin cru2x(td->td_ucred, &unp3->unp_peercred); 9670c1bb4fbSDima Dorfman unp3->unp_flags |= UNP_HAVEPC; 9680c1bb4fbSDima Dorfman /* 9691c381b19SRobert Watson * The receiver's (server's) credentials are copied from the 9701c381b19SRobert Watson * unp_peercred member of socket on which the former called 9711c381b19SRobert Watson * listen(); unp_listen() cached that process's credentials 9721c381b19SRobert Watson * at that time so we can use them now. 9730c1bb4fbSDima Dorfman */ 9740c1bb4fbSDima Dorfman KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 9750c1bb4fbSDima Dorfman ("unp_connect: listener without cached peercred")); 9760c1bb4fbSDima Dorfman memcpy(&unp->unp_peercred, &unp2->unp_peercred, 9770c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 9780c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPC; 979481f8fe8SMaxim Konovalov if (unp2->unp_flags & UNP_WANTCRED) 980481f8fe8SMaxim Konovalov unp3->unp_flags |= UNP_WANTCRED; 981335654d7SRobert Watson #ifdef MAC 982310e7cebSRobert Watson SOCK_LOCK(so); 983335654d7SRobert Watson mac_set_socket_peer_from_socket(so, so3); 984335654d7SRobert Watson mac_set_socket_peer_from_socket(so3, so); 985310e7cebSRobert Watson SOCK_UNLOCK(so); 986335654d7SRobert Watson #endif 9870c1bb4fbSDima Dorfman 988df8bae1dSRodney W. Grimes so2 = so3; 989df8bae1dSRodney W. Grimes } 9906a2989fdSMatthew N. Dodd error = unp_connect2(so, so2, PRU_CONNECT); 9910d9ce3a1SRobert Watson bad2: 9920d9ce3a1SRobert Watson UNP_UNLOCK(); 9930d9ce3a1SRobert Watson mtx_lock(&Giant); 994df8bae1dSRodney W. Grimes bad: 9950d9ce3a1SRobert Watson mtx_assert(&Giant, MA_OWNED); 9960d9ce3a1SRobert Watson if (vp != NULL) 997df8bae1dSRodney W. Grimes vput(vp); 9980d9ce3a1SRobert Watson mtx_unlock(&Giant); 9990d9ce3a1SRobert Watson free(sa, M_SONAME); 10000d9ce3a1SRobert Watson UNP_LOCK(); 10014f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_CONNECTING; 1002df8bae1dSRodney W. Grimes return (error); 1003df8bae1dSRodney W. Grimes } 1004df8bae1dSRodney W. Grimes 1005db48c0d2SRobert Watson static int 10066a2989fdSMatthew N. Dodd unp_connect2(struct socket *so, struct socket *so2, int req) 1007df8bae1dSRodney W. Grimes { 1008892af6b9SRobert Watson struct unpcb *unp = sotounpcb(so); 1009892af6b9SRobert Watson struct unpcb *unp2; 1010df8bae1dSRodney W. Grimes 10110d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 10120d9ce3a1SRobert Watson 1013df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 1014df8bae1dSRodney W. Grimes return (EPROTOTYPE); 1015df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 10164d4b555eSRobert Watson KASSERT(unp2 != NULL, ("unp_connect2: unp2 == NULL")); 1017df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 1018df8bae1dSRodney W. Grimes switch (so->so_type) { 1019df8bae1dSRodney W. Grimes 1020df8bae1dSRodney W. Grimes case SOCK_DGRAM: 102198271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 1022df8bae1dSRodney W. Grimes soisconnected(so); 1023df8bae1dSRodney W. Grimes break; 1024df8bae1dSRodney W. Grimes 1025df8bae1dSRodney W. Grimes case SOCK_STREAM: 1026df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 10276a2989fdSMatthew N. Dodd if (req == PRU_CONNECT && 10286a2989fdSMatthew N. Dodd ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT)) 10296a2989fdSMatthew N. Dodd soisconnecting(so); 10306a2989fdSMatthew N. Dodd else 1031df8bae1dSRodney W. Grimes soisconnected(so); 1032df8bae1dSRodney W. Grimes soisconnected(so2); 1033df8bae1dSRodney W. Grimes break; 1034df8bae1dSRodney W. Grimes 1035df8bae1dSRodney W. Grimes default: 1036df8bae1dSRodney W. Grimes panic("unp_connect2"); 1037df8bae1dSRodney W. Grimes } 1038df8bae1dSRodney W. Grimes return (0); 1039df8bae1dSRodney W. Grimes } 1040df8bae1dSRodney W. Grimes 1041f708ef1bSPoul-Henning Kamp static void 1042892af6b9SRobert Watson unp_disconnect(struct unpcb *unp) 1043df8bae1dSRodney W. Grimes { 1044892af6b9SRobert Watson struct unpcb *unp2 = unp->unp_conn; 10451b2e3b4bSRobert Watson struct socket *so; 1046df8bae1dSRodney W. Grimes 10470d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 10480d9ce3a1SRobert Watson 1049fc3fcacfSRobert Watson if (unp2 == NULL) 1050df8bae1dSRodney W. Grimes return; 1051fc3fcacfSRobert Watson unp->unp_conn = NULL; 1052df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 1053df8bae1dSRodney W. Grimes case SOCK_DGRAM: 105498271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 10551b2e3b4bSRobert Watson so = unp->unp_socket; 10561b2e3b4bSRobert Watson SOCK_LOCK(so); 10571b2e3b4bSRobert Watson so->so_state &= ~SS_ISCONNECTED; 10581b2e3b4bSRobert Watson SOCK_UNLOCK(so); 1059df8bae1dSRodney W. Grimes break; 1060df8bae1dSRodney W. Grimes 1061df8bae1dSRodney W. Grimes case SOCK_STREAM: 1062df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 1063fc3fcacfSRobert Watson unp2->unp_conn = NULL; 1064df8bae1dSRodney W. Grimes soisdisconnected(unp2->unp_socket); 1065df8bae1dSRodney W. Grimes break; 1066df8bae1dSRodney W. Grimes } 1067df8bae1dSRodney W. Grimes } 1068df8bae1dSRodney W. Grimes 10690d9ce3a1SRobert Watson /* 10701c381b19SRobert Watson * unp_pcblist() assumes that UNIX domain socket memory is never reclaimed by 10711c381b19SRobert Watson * the zone (UMA_ZONE_NOFREE), and as such potentially stale pointers are 10721c381b19SRobert Watson * safe to reference. It first scans the list of struct unpcb's to generate 10731c381b19SRobert Watson * a pointer list, then it rescans its list one entry at a time to 10740d9ce3a1SRobert Watson * externalize and copyout. It checks the generation number to see if a 10750d9ce3a1SRobert Watson * struct unpcb has been reused, and will skip it if so. 10760d9ce3a1SRobert Watson */ 107798271db4SGarrett Wollman static int 107882d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 107998271db4SGarrett Wollman { 1080f5ef029eSPoul-Henning Kamp int error, i, n; 108198271db4SGarrett Wollman struct unpcb *unp, **unp_list; 108298271db4SGarrett Wollman unp_gen_t gencnt; 10838f364875SJulian Elischer struct xunpgen *xug; 108498271db4SGarrett Wollman struct unp_head *head; 10858f364875SJulian Elischer struct xunpcb *xu; 108698271db4SGarrett Wollman 1087a23d65bfSBruce Evans head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 108898271db4SGarrett Wollman 108998271db4SGarrett Wollman /* 109098271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 109198271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 109298271db4SGarrett Wollman */ 1093fc3fcacfSRobert Watson if (req->oldptr == NULL) { 109498271db4SGarrett Wollman n = unp_count; 10958f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug) 109698271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 1097e5aeaa0cSDag-Erling Smørgrav return (0); 109898271db4SGarrett Wollman } 109998271db4SGarrett Wollman 1100fc3fcacfSRobert Watson if (req->newptr != NULL) 1101e5aeaa0cSDag-Erling Smørgrav return (EPERM); 110298271db4SGarrett Wollman 110398271db4SGarrett Wollman /* 110498271db4SGarrett Wollman * OK, now we're committed to doing something. 110598271db4SGarrett Wollman */ 1106a163d034SWarner Losh xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 11070d9ce3a1SRobert Watson UNP_LOCK(); 110898271db4SGarrett Wollman gencnt = unp_gencnt; 110998271db4SGarrett Wollman n = unp_count; 11100d9ce3a1SRobert Watson UNP_UNLOCK(); 111198271db4SGarrett Wollman 11128f364875SJulian Elischer xug->xug_len = sizeof *xug; 11138f364875SJulian Elischer xug->xug_count = n; 11148f364875SJulian Elischer xug->xug_gen = gencnt; 11158f364875SJulian Elischer xug->xug_sogen = so_gencnt; 11168f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 11178f364875SJulian Elischer if (error) { 11188f364875SJulian Elischer free(xug, M_TEMP); 1119e5aeaa0cSDag-Erling Smørgrav return (error); 11208f364875SJulian Elischer } 112198271db4SGarrett Wollman 1122a163d034SWarner Losh unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 112398271db4SGarrett Wollman 11240d9ce3a1SRobert Watson UNP_LOCK(); 11252e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 11262e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 11278a7d8cc6SRobert Watson if (unp->unp_gencnt <= gencnt) { 1128a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred, 11298a7d8cc6SRobert Watson unp->unp_socket->so_cred)) 11304787fd37SPaul Saab continue; 113198271db4SGarrett Wollman unp_list[i++] = unp; 113298271db4SGarrett Wollman } 11334787fd37SPaul Saab } 11340d9ce3a1SRobert Watson UNP_UNLOCK(); 11351c381b19SRobert Watson n = i; /* In case we lost some during malloc. */ 113698271db4SGarrett Wollman 113798271db4SGarrett Wollman error = 0; 1138fe2eee82SColin Percival xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO); 113998271db4SGarrett Wollman for (i = 0; i < n; i++) { 114098271db4SGarrett Wollman unp = unp_list[i]; 114198271db4SGarrett Wollman if (unp->unp_gencnt <= gencnt) { 11428f364875SJulian Elischer xu->xu_len = sizeof *xu; 11438f364875SJulian Elischer xu->xu_unpp = unp; 114498271db4SGarrett Wollman /* 114598271db4SGarrett Wollman * XXX - need more locking here to protect against 114698271db4SGarrett Wollman * connect/disconnect races for SMP. 114798271db4SGarrett Wollman */ 1148fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 11498f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr, 115098271db4SGarrett Wollman unp->unp_addr->sun_len); 1151fc3fcacfSRobert Watson if (unp->unp_conn != NULL && 1152fc3fcacfSRobert Watson unp->unp_conn->unp_addr != NULL) 115398271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 11548f364875SJulian Elischer &xu->xu_caddr, 115598271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 11568f364875SJulian Elischer bcopy(unp, &xu->xu_unp, sizeof *unp); 11578f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket); 11588f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu); 115998271db4SGarrett Wollman } 116098271db4SGarrett Wollman } 11618f364875SJulian Elischer free(xu, M_TEMP); 116298271db4SGarrett Wollman if (!error) { 116398271db4SGarrett Wollman /* 11641c381b19SRobert Watson * Give the user an updated idea of our state. If the 11651c381b19SRobert Watson * generation differs from what we told her before, she knows 11661c381b19SRobert Watson * that something happened while we were processing this 11671c381b19SRobert Watson * request, and it might be necessary to retry. 116898271db4SGarrett Wollman */ 11698f364875SJulian Elischer xug->xug_gen = unp_gencnt; 11708f364875SJulian Elischer xug->xug_sogen = so_gencnt; 11718f364875SJulian Elischer xug->xug_count = unp_count; 11728f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 117398271db4SGarrett Wollman } 117498271db4SGarrett Wollman free(unp_list, M_TEMP); 11758f364875SJulian Elischer free(xug, M_TEMP); 1176e5aeaa0cSDag-Erling Smørgrav return (error); 117798271db4SGarrett Wollman } 117898271db4SGarrett Wollman 117998271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 118098271db4SGarrett Wollman (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 118198271db4SGarrett Wollman "List of active local datagram sockets"); 118298271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 118398271db4SGarrett Wollman (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 118498271db4SGarrett Wollman "List of active local stream sockets"); 118598271db4SGarrett Wollman 1186f708ef1bSPoul-Henning Kamp static void 1187892af6b9SRobert Watson unp_shutdown(struct unpcb *unp) 1188df8bae1dSRodney W. Grimes { 1189df8bae1dSRodney W. Grimes struct socket *so; 1190df8bae1dSRodney W. Grimes 11910d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 11920d9ce3a1SRobert Watson 1193df8bae1dSRodney W. Grimes if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 1194df8bae1dSRodney W. Grimes (so = unp->unp_conn->unp_socket)) 1195df8bae1dSRodney W. Grimes socantrcvmore(so); 1196df8bae1dSRodney W. Grimes } 1197df8bae1dSRodney W. Grimes 1198f708ef1bSPoul-Henning Kamp static void 1199892af6b9SRobert Watson unp_drop(struct unpcb *unp, int errno) 1200df8bae1dSRodney W. Grimes { 1201df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 1202df8bae1dSRodney W. Grimes 12030d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 12040d9ce3a1SRobert Watson 1205df8bae1dSRodney W. Grimes so->so_error = errno; 1206df8bae1dSRodney W. Grimes unp_disconnect(unp); 1207df8bae1dSRodney W. Grimes } 1208df8bae1dSRodney W. Grimes 12092bc21ed9SDavid Malone static void 1210892af6b9SRobert Watson unp_freerights(struct file **rp, int fdcount) 1211df8bae1dSRodney W. Grimes { 12122bc21ed9SDavid Malone int i; 12132bc21ed9SDavid Malone struct file *fp; 1214df8bae1dSRodney W. Grimes 12152bc21ed9SDavid Malone for (i = 0; i < fdcount; i++) { 1216df8bae1dSRodney W. Grimes fp = *rp; 12178692c025SYoshinobu Inoue /* 12181c381b19SRobert Watson * Zero the pointer before calling unp_discard since it may 12191c381b19SRobert Watson * end up in unp_gc().. 1220d7dca903SRobert Watson * 1221d7dca903SRobert Watson * XXXRW: This is less true than it used to be. 12228692c025SYoshinobu Inoue */ 1223df8bae1dSRodney W. Grimes *rp++ = 0; 12248692c025SYoshinobu Inoue unp_discard(fp); 1225df8bae1dSRodney W. Grimes } 12262bc21ed9SDavid Malone } 12272bc21ed9SDavid Malone 12282bc21ed9SDavid Malone int 1229892af6b9SRobert Watson unp_externalize(struct mbuf *control, struct mbuf **controlp) 12302bc21ed9SDavid Malone { 12312bc21ed9SDavid Malone struct thread *td = curthread; /* XXX */ 12322bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 12332bc21ed9SDavid Malone int i; 12342bc21ed9SDavid Malone int *fdp; 12352bc21ed9SDavid Malone struct file **rp; 12362bc21ed9SDavid Malone struct file *fp; 12372bc21ed9SDavid Malone void *data; 12382bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 12392bc21ed9SDavid Malone int error, newfds; 12402bc21ed9SDavid Malone int f; 12412bc21ed9SDavid Malone u_int newlen; 12422bc21ed9SDavid Malone 12434c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 12444c5bc1caSRobert Watson 12452bc21ed9SDavid Malone error = 0; 12462bc21ed9SDavid Malone if (controlp != NULL) /* controlp == NULL => free control messages */ 12472bc21ed9SDavid Malone *controlp = NULL; 12482bc21ed9SDavid Malone 12492bc21ed9SDavid Malone while (cm != NULL) { 12502bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 12512bc21ed9SDavid Malone error = EINVAL; 12522bc21ed9SDavid Malone break; 12532bc21ed9SDavid Malone } 12542bc21ed9SDavid Malone 12552bc21ed9SDavid Malone data = CMSG_DATA(cm); 12562bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 12572bc21ed9SDavid Malone 12582bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET 12592bc21ed9SDavid Malone && cm->cmsg_type == SCM_RIGHTS) { 12602bc21ed9SDavid Malone newfds = datalen / sizeof(struct file *); 12612bc21ed9SDavid Malone rp = data; 12622bc21ed9SDavid Malone 1263e2f9a08bSOlivier Houchard /* If we're not outputting the descriptors free them. */ 12642bc21ed9SDavid Malone if (error || controlp == NULL) { 12652bc21ed9SDavid Malone unp_freerights(rp, newfds); 12662bc21ed9SDavid Malone goto next; 12672bc21ed9SDavid Malone } 1268426da3bcSAlfred Perlstein FILEDESC_LOCK(td->td_proc->p_fd); 12692bc21ed9SDavid Malone /* if the new FD's will not fit free them. */ 12702bc21ed9SDavid Malone if (!fdavail(td, newfds)) { 1271426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 12722bc21ed9SDavid Malone error = EMSGSIZE; 12732bc21ed9SDavid Malone unp_freerights(rp, newfds); 12742bc21ed9SDavid Malone goto next; 1275df8bae1dSRodney W. Grimes } 1276ed5b7817SJulian Elischer /* 12771c381b19SRobert Watson * Now change each pointer to an fd in the global 12781c381b19SRobert Watson * table to an integer that is the index to the local 12791c381b19SRobert Watson * fd table entry that we set up to point to the 12801c381b19SRobert Watson * global one we are transferring. 1281ed5b7817SJulian Elischer */ 12822bc21ed9SDavid Malone newlen = newfds * sizeof(int); 12832bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 12842bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 12852bc21ed9SDavid Malone if (*controlp == NULL) { 1286426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 12872bc21ed9SDavid Malone error = E2BIG; 12882bc21ed9SDavid Malone unp_freerights(rp, newfds); 12892bc21ed9SDavid Malone goto next; 12902bc21ed9SDavid Malone } 12912bc21ed9SDavid Malone 12922bc21ed9SDavid Malone fdp = (int *) 12932bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1294df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 1295a6d4491cSDag-Erling Smørgrav if (fdalloc(td, 0, &f)) 12962bc21ed9SDavid Malone panic("unp_externalize fdalloc failed"); 12978692c025SYoshinobu Inoue fp = *rp++; 1298b40ce416SJulian Elischer td->td_proc->p_fd->fd_ofiles[f] = fp; 1299426da3bcSAlfred Perlstein FILE_LOCK(fp); 1300df8bae1dSRodney W. Grimes fp->f_msgcount--; 1301426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1302df8bae1dSRodney W. Grimes unp_rights--; 13038692c025SYoshinobu Inoue *fdp++ = f; 1304df8bae1dSRodney W. Grimes } 1305426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 13061c381b19SRobert Watson } else { 13071c381b19SRobert Watson /* We can just copy anything else across. */ 13082bc21ed9SDavid Malone if (error || controlp == NULL) 13092bc21ed9SDavid Malone goto next; 13102bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, datalen, 13112bc21ed9SDavid Malone cm->cmsg_type, cm->cmsg_level); 13122bc21ed9SDavid Malone if (*controlp == NULL) { 13132bc21ed9SDavid Malone error = ENOBUFS; 13142bc21ed9SDavid Malone goto next; 13152bc21ed9SDavid Malone } 13162bc21ed9SDavid Malone bcopy(data, 13172bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 13182bc21ed9SDavid Malone datalen); 13192bc21ed9SDavid Malone } 13202bc21ed9SDavid Malone 13212bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 13222bc21ed9SDavid Malone 13232bc21ed9SDavid Malone next: 13242bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 13252bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 13262bc21ed9SDavid Malone cm = (struct cmsghdr *) 13272bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 13288692c025SYoshinobu Inoue } else { 13292bc21ed9SDavid Malone clen = 0; 13302bc21ed9SDavid Malone cm = NULL; 13318692c025SYoshinobu Inoue } 13328692c025SYoshinobu Inoue } 13338692c025SYoshinobu Inoue 13342bc21ed9SDavid Malone m_freem(control); 13352bc21ed9SDavid Malone 13362bc21ed9SDavid Malone return (error); 1337df8bae1dSRodney W. Grimes } 1338df8bae1dSRodney W. Grimes 13394f590175SPaul Saab static void 13404f590175SPaul Saab unp_zone_change(void *tag) 13414f590175SPaul Saab { 13424f590175SPaul Saab 13434f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets); 13444f590175SPaul Saab } 13454f590175SPaul Saab 134698271db4SGarrett Wollman void 134798271db4SGarrett Wollman unp_init(void) 134898271db4SGarrett Wollman { 13491c381b19SRobert Watson 13509e9d298aSJeff Roberson unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 13519e9d298aSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 1352fc3fcacfSRobert Watson if (unp_zone == NULL) 135398271db4SGarrett Wollman panic("unp_init"); 13544f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets); 13554f590175SPaul Saab EVENTHANDLER_REGISTER(maxsockets_change, unp_zone_change, 13564f590175SPaul Saab NULL, EVENTHANDLER_PRI_ANY); 135798271db4SGarrett Wollman LIST_INIT(&unp_dhead); 135898271db4SGarrett Wollman LIST_INIT(&unp_shead); 1359a0ec558aSRobert Watson TASK_INIT(&unp_gc_task, 0, unp_gc, NULL); 13600d9ce3a1SRobert Watson UNP_LOCK_INIT(); 136198271db4SGarrett Wollman } 136298271db4SGarrett Wollman 1363f708ef1bSPoul-Henning Kamp static int 1364892af6b9SRobert Watson unp_internalize(struct mbuf **controlp, struct thread *td) 1365df8bae1dSRodney W. Grimes { 13662bc21ed9SDavid Malone struct mbuf *control = *controlp; 1367b40ce416SJulian Elischer struct proc *p = td->td_proc; 13688692c025SYoshinobu Inoue struct filedesc *fdescp = p->p_fd; 13692bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 13702bc21ed9SDavid Malone struct cmsgcred *cmcred; 13712bc21ed9SDavid Malone struct file **rp; 13722bc21ed9SDavid Malone struct file *fp; 13732bc21ed9SDavid Malone struct timeval *tv; 13742bc21ed9SDavid Malone int i, fd, *fdp; 13752bc21ed9SDavid Malone void *data; 13762bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 13772bc21ed9SDavid Malone int error, oldfds; 13788692c025SYoshinobu Inoue u_int newlen; 1379df8bae1dSRodney W. Grimes 13804c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 13814c5bc1caSRobert Watson 13822bc21ed9SDavid Malone error = 0; 13832bc21ed9SDavid Malone *controlp = NULL; 13840b788fa1SBill Paul 13852bc21ed9SDavid Malone while (cm != NULL) { 13862bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 13872bc21ed9SDavid Malone || cm->cmsg_len > clen) { 13882bc21ed9SDavid Malone error = EINVAL; 13892bc21ed9SDavid Malone goto out; 13902bc21ed9SDavid Malone } 13912bc21ed9SDavid Malone 13922bc21ed9SDavid Malone data = CMSG_DATA(cm); 13932bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 13942bc21ed9SDavid Malone 13952bc21ed9SDavid Malone switch (cm->cmsg_type) { 13960b788fa1SBill Paul /* 13970b788fa1SBill Paul * Fill in credential information. 13980b788fa1SBill Paul */ 13992bc21ed9SDavid Malone case SCM_CREDS: 14002bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 14012bc21ed9SDavid Malone SCM_CREDS, SOL_SOCKET); 14022bc21ed9SDavid Malone if (*controlp == NULL) { 14032bc21ed9SDavid Malone error = ENOBUFS; 14042bc21ed9SDavid Malone goto out; 14052bc21ed9SDavid Malone } 14062bc21ed9SDavid Malone 14072bc21ed9SDavid Malone cmcred = (struct cmsgcred *) 14082bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 14090b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 1410a854ed98SJohn Baldwin cmcred->cmcred_uid = td->td_ucred->cr_ruid; 1411a854ed98SJohn Baldwin cmcred->cmcred_gid = td->td_ucred->cr_rgid; 1412a854ed98SJohn Baldwin cmcred->cmcred_euid = td->td_ucred->cr_uid; 1413a854ed98SJohn Baldwin cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 14140b788fa1SBill Paul CMGROUP_MAX); 14150b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 14162bc21ed9SDavid Malone cmcred->cmcred_groups[i] = 1417a854ed98SJohn Baldwin td->td_ucred->cr_groups[i]; 14182bc21ed9SDavid Malone break; 14190b788fa1SBill Paul 14202bc21ed9SDavid Malone case SCM_RIGHTS: 14212bc21ed9SDavid Malone oldfds = datalen / sizeof (int); 1422ed5b7817SJulian Elischer /* 14231c381b19SRobert Watson * Check that all the FDs passed in refer to legal 14241c381b19SRobert Watson * files. If not, reject the entire operation. 1425ed5b7817SJulian Elischer */ 14262bc21ed9SDavid Malone fdp = data; 1427426da3bcSAlfred Perlstein FILEDESC_LOCK(fdescp); 1428df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 14298692c025SYoshinobu Inoue fd = *fdp++; 14308692c025SYoshinobu Inoue if ((unsigned)fd >= fdescp->fd_nfiles || 14312bc21ed9SDavid Malone fdescp->fd_ofiles[fd] == NULL) { 1432426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 14332bc21ed9SDavid Malone error = EBADF; 14342bc21ed9SDavid Malone goto out; 14352bc21ed9SDavid Malone } 1436e7d6662fSAlfred Perlstein fp = fdescp->fd_ofiles[fd]; 1437e7d6662fSAlfred Perlstein if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 1438e7d6662fSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 1439e7d6662fSAlfred Perlstein error = EOPNOTSUPP; 1440e7d6662fSAlfred Perlstein goto out; 1441e7d6662fSAlfred Perlstein } 1442e7d6662fSAlfred Perlstein 1443df8bae1dSRodney W. Grimes } 1444ed5b7817SJulian Elischer /* 14451c381b19SRobert Watson * Now replace the integer FDs with pointers to the 14461c381b19SRobert Watson * associated global file table entry.. 1447ed5b7817SJulian Elischer */ 14482bc21ed9SDavid Malone newlen = oldfds * sizeof(struct file *); 14492bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 14502bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 14512bc21ed9SDavid Malone if (*controlp == NULL) { 1452426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 14532bc21ed9SDavid Malone error = E2BIG; 14542bc21ed9SDavid Malone goto out; 14558692c025SYoshinobu Inoue } 14568692c025SYoshinobu Inoue 14572bc21ed9SDavid Malone fdp = data; 14582bc21ed9SDavid Malone rp = (struct file **) 14592bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 14608692c025SYoshinobu Inoue for (i = 0; i < oldfds; i++) { 14618692c025SYoshinobu Inoue fp = fdescp->fd_ofiles[*fdp++]; 1462df8bae1dSRodney W. Grimes *rp++ = fp; 1463426da3bcSAlfred Perlstein FILE_LOCK(fp); 1464df8bae1dSRodney W. Grimes fp->f_count++; 1465df8bae1dSRodney W. Grimes fp->f_msgcount++; 1466426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1467df8bae1dSRodney W. Grimes unp_rights++; 1468df8bae1dSRodney W. Grimes } 1469426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 14702bc21ed9SDavid Malone break; 14712bc21ed9SDavid Malone 14722bc21ed9SDavid Malone case SCM_TIMESTAMP: 14732bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*tv), 14742bc21ed9SDavid Malone SCM_TIMESTAMP, SOL_SOCKET); 14752bc21ed9SDavid Malone if (*controlp == NULL) { 14762bc21ed9SDavid Malone error = ENOBUFS; 14772bc21ed9SDavid Malone goto out; 14788692c025SYoshinobu Inoue } 14792bc21ed9SDavid Malone tv = (struct timeval *) 14802bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 14812bc21ed9SDavid Malone microtime(tv); 14822bc21ed9SDavid Malone break; 14832bc21ed9SDavid Malone 14842bc21ed9SDavid Malone default: 14852bc21ed9SDavid Malone error = EINVAL; 14862bc21ed9SDavid Malone goto out; 14872bc21ed9SDavid Malone } 14882bc21ed9SDavid Malone 14892bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 14902bc21ed9SDavid Malone 14912bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 14922bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 14932bc21ed9SDavid Malone cm = (struct cmsghdr *) 14942bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 14952bc21ed9SDavid Malone } else { 14962bc21ed9SDavid Malone clen = 0; 14972bc21ed9SDavid Malone cm = NULL; 14982bc21ed9SDavid Malone } 14992bc21ed9SDavid Malone } 15002bc21ed9SDavid Malone 15012bc21ed9SDavid Malone out: 15022bc21ed9SDavid Malone m_freem(control); 15032bc21ed9SDavid Malone 15042bc21ed9SDavid Malone return (error); 1505df8bae1dSRodney W. Grimes } 1506df8bae1dSRodney W. Grimes 15076a2989fdSMatthew N. Dodd struct mbuf * 15086a2989fdSMatthew N. Dodd unp_addsockcred(struct thread *td, struct mbuf *control) 15096a2989fdSMatthew N. Dodd { 151070df31f4SMaxim Konovalov struct mbuf *m, *n, *n_prev; 15116a2989fdSMatthew N. Dodd struct sockcred *sc; 151270df31f4SMaxim Konovalov const struct cmsghdr *cm; 15136a2989fdSMatthew N. Dodd int ngroups; 15146a2989fdSMatthew N. Dodd int i; 15156a2989fdSMatthew N. Dodd 15166a2989fdSMatthew N. Dodd ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX); 15176a2989fdSMatthew N. Dodd 15186a2989fdSMatthew N. Dodd m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET); 15196a2989fdSMatthew N. Dodd if (m == NULL) 15206a2989fdSMatthew N. Dodd return (control); 15216a2989fdSMatthew N. Dodd 15226a2989fdSMatthew N. Dodd sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *)); 15236a2989fdSMatthew N. Dodd sc->sc_uid = td->td_ucred->cr_ruid; 15246a2989fdSMatthew N. Dodd sc->sc_euid = td->td_ucred->cr_uid; 15256a2989fdSMatthew N. Dodd sc->sc_gid = td->td_ucred->cr_rgid; 15266a2989fdSMatthew N. Dodd sc->sc_egid = td->td_ucred->cr_gid; 15276a2989fdSMatthew N. Dodd sc->sc_ngroups = ngroups; 15286a2989fdSMatthew N. Dodd for (i = 0; i < sc->sc_ngroups; i++) 15296a2989fdSMatthew N. Dodd sc->sc_groups[i] = td->td_ucred->cr_groups[i]; 15306a2989fdSMatthew N. Dodd 15316a2989fdSMatthew N. Dodd /* 15321c381b19SRobert Watson * Unlink SCM_CREDS control messages (struct cmsgcred), since just 15331c381b19SRobert Watson * created SCM_CREDS control message (struct sockcred) has another 15341c381b19SRobert Watson * format. 15356a2989fdSMatthew N. Dodd */ 153670df31f4SMaxim Konovalov if (control != NULL) 153770df31f4SMaxim Konovalov for (n = control, n_prev = NULL; n != NULL;) { 153870df31f4SMaxim Konovalov cm = mtod(n, struct cmsghdr *); 153970df31f4SMaxim Konovalov if (cm->cmsg_level == SOL_SOCKET && 154070df31f4SMaxim Konovalov cm->cmsg_type == SCM_CREDS) { 154170df31f4SMaxim Konovalov if (n_prev == NULL) 154270df31f4SMaxim Konovalov control = n->m_next; 154370df31f4SMaxim Konovalov else 154470df31f4SMaxim Konovalov n_prev->m_next = n->m_next; 154570df31f4SMaxim Konovalov n = m_free(n); 154670df31f4SMaxim Konovalov } else { 154770df31f4SMaxim Konovalov n_prev = n; 154870df31f4SMaxim Konovalov n = n->m_next; 154970df31f4SMaxim Konovalov } 155070df31f4SMaxim Konovalov } 15516a2989fdSMatthew N. Dodd 155270df31f4SMaxim Konovalov /* Prepend it to the head. */ 155370df31f4SMaxim Konovalov m->m_next = control; 155470df31f4SMaxim Konovalov 155570df31f4SMaxim Konovalov return (m); 15566a2989fdSMatthew N. Dodd } 15576a2989fdSMatthew N. Dodd 1558161a0c7cSRobert Watson /* 1559a0ec558aSRobert Watson * unp_defer indicates whether additional work has been defered for a future 1560a0ec558aSRobert Watson * pass through unp_gc(). It is thread local and does not require explicit 1561a0ec558aSRobert Watson * synchronization. 1562161a0c7cSRobert Watson */ 1563a0ec558aSRobert Watson static int unp_defer; 1564a0ec558aSRobert Watson 1565a0ec558aSRobert Watson static int unp_taskcount; 1566a0ec558aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0, ""); 1567a0ec558aSRobert Watson 1568a0ec558aSRobert Watson static int unp_recycled; 1569a0ec558aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0, ""); 1570df8bae1dSRodney W. Grimes 1571f708ef1bSPoul-Henning Kamp static void 1572a0ec558aSRobert Watson unp_gc(__unused void *arg, int pending) 1573df8bae1dSRodney W. Grimes { 1574892af6b9SRobert Watson struct file *fp, *nextfp; 1575892af6b9SRobert Watson struct socket *so; 1576df8bae1dSRodney W. Grimes struct file **extra_ref, **fpp; 1577df8bae1dSRodney W. Grimes int nunref, i; 157895f004dcSAlfred Perlstein int nfiles_snap; 157995f004dcSAlfred Perlstein int nfiles_slack = 20; 1580df8bae1dSRodney W. Grimes 1581a0ec558aSRobert Watson unp_taskcount++; 1582df8bae1dSRodney W. Grimes unp_defer = 0; 1583ed5b7817SJulian Elischer /* 15841c381b19SRobert Watson * Before going through all this, set all FDs to be NOT defered and 15851c381b19SRobert Watson * NOT externally accessible. 1586ed5b7817SJulian Elischer */ 1587426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 15882e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) 1589426da3bcSAlfred Perlstein fp->f_gcflag &= ~(FMARK|FDEFER); 1590df8bae1dSRodney W. Grimes do { 15915bb84bc8SRobert Watson KASSERT(unp_defer >= 0, ("unp_gc: unp_defer %d", unp_defer)); 15922e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) { 1593426da3bcSAlfred Perlstein FILE_LOCK(fp); 1594ed5b7817SJulian Elischer /* 1595a0ec558aSRobert Watson * If the file is not open, skip it -- could be a 1596a0ec558aSRobert Watson * file in the process of being opened, or in the 1597a0ec558aSRobert Watson * process of being closed. If the file is 1598a0ec558aSRobert Watson * "closing", it may have been marked for deferred 1599a0ec558aSRobert Watson * consideration. Clear the flag now if so. 1600ed5b7817SJulian Elischer */ 1601426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1602a0ec558aSRobert Watson if (fp->f_gcflag & FDEFER) 1603a0ec558aSRobert Watson unp_defer--; 1604a0ec558aSRobert Watson fp->f_gcflag &= ~(FMARK|FDEFER); 1605426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1606df8bae1dSRodney W. Grimes continue; 1607426da3bcSAlfred Perlstein } 1608ed5b7817SJulian Elischer /* 16091c381b19SRobert Watson * If we already marked it as 'defer' in a previous 16101c381b19SRobert Watson * pass, then try process it this time and un-mark 16111c381b19SRobert Watson * it. 1612ed5b7817SJulian Elischer */ 1613426da3bcSAlfred Perlstein if (fp->f_gcflag & FDEFER) { 1614426da3bcSAlfred Perlstein fp->f_gcflag &= ~FDEFER; 1615df8bae1dSRodney W. Grimes unp_defer--; 1616df8bae1dSRodney W. Grimes } else { 1617ed5b7817SJulian Elischer /* 1618ed5b7817SJulian Elischer * if it's not defered, then check if it's 1619ed5b7817SJulian Elischer * already marked.. if so skip it 1620ed5b7817SJulian Elischer */ 1621426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) { 1622426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1623df8bae1dSRodney W. Grimes continue; 1624426da3bcSAlfred Perlstein } 1625ed5b7817SJulian Elischer /* 16261c381b19SRobert Watson * If all references are from messages in 16271c381b19SRobert Watson * transit, then skip it. it's not externally 16281c381b19SRobert Watson * accessible. 1629ed5b7817SJulian Elischer */ 1630426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount) { 1631426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1632df8bae1dSRodney W. Grimes continue; 1633426da3bcSAlfred Perlstein } 1634ed5b7817SJulian Elischer /* 1635ed5b7817SJulian Elischer * If it got this far then it must be 1636ed5b7817SJulian Elischer * externally accessible. 1637ed5b7817SJulian Elischer */ 1638426da3bcSAlfred Perlstein fp->f_gcflag |= FMARK; 1639df8bae1dSRodney W. Grimes } 1640ed5b7817SJulian Elischer /* 16411c381b19SRobert Watson * Either it was defered, or it is externally 16421c381b19SRobert Watson * accessible and not already marked so. Now check 16431c381b19SRobert Watson * if it is possibly one of OUR sockets. 1644ed5b7817SJulian Elischer */ 1645df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_SOCKET || 164648e3128bSMatthew Dillon (so = fp->f_data) == NULL) { 1647426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1648df8bae1dSRodney W. Grimes continue; 1649426da3bcSAlfred Perlstein } 1650426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1651748e0b0aSGarrett Wollman if (so->so_proto->pr_domain != &localdomain || 1652df8bae1dSRodney W. Grimes (so->so_proto->pr_flags&PR_RIGHTS) == 0) 1653df8bae1dSRodney W. Grimes continue; 1654ed5b7817SJulian Elischer /* 16551c381b19SRobert Watson * So, Ok, it's one of our sockets and it IS 16561c381b19SRobert Watson * externally accessible (or was defered). Now we 16571c381b19SRobert Watson * look to see if we hold any file descriptors in its 1658ed5b7817SJulian Elischer * message buffers. Follow those links and mark them 1659ed5b7817SJulian Elischer * as accessible too. 1660ed5b7817SJulian Elischer */ 16617717cf07SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1662df8bae1dSRodney W. Grimes unp_scan(so->so_rcv.sb_mb, unp_mark); 16637717cf07SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 1664df8bae1dSRodney W. Grimes } 1665df8bae1dSRodney W. Grimes } while (unp_defer); 1666426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1667df8bae1dSRodney W. Grimes /* 1668a0ec558aSRobert Watson * XXXRW: The following comments need updating for a post-SMPng and 1669a0ec558aSRobert Watson * deferred unp_gc() world, but are still generally accurate. 1670a0ec558aSRobert Watson * 16711c381b19SRobert Watson * We grab an extra reference to each of the file table entries that 16721c381b19SRobert Watson * are not otherwise accessible and then free the rights that are 16731c381b19SRobert Watson * stored in messages on them. 1674df8bae1dSRodney W. Grimes * 1675df8bae1dSRodney W. Grimes * The bug in the orginal code is a little tricky, so I'll describe 1676df8bae1dSRodney W. Grimes * what's wrong with it here. 1677df8bae1dSRodney W. Grimes * 1678df8bae1dSRodney W. Grimes * It is incorrect to simply unp_discard each entry for f_msgcount 1679df8bae1dSRodney W. Grimes * times -- consider the case of sockets A and B that contain 1680df8bae1dSRodney W. Grimes * references to each other. On a last close of some other socket, 1681df8bae1dSRodney W. Grimes * we trigger a gc since the number of outstanding rights (unp_rights) 1682a0ec558aSRobert Watson * is non-zero. If during the sweep phase the gc code unp_discards, 1683df8bae1dSRodney W. Grimes * we end up doing a (full) closef on the descriptor. A closef on A 1684df8bae1dSRodney W. Grimes * results in the following chain. Closef calls soo_close, which 1685df8bae1dSRodney W. Grimes * calls soclose. Soclose calls first (through the switch 1686df8bae1dSRodney W. Grimes * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 16871c381b19SRobert Watson * returns because the previous instance had set unp_gcing, and we 16881c381b19SRobert Watson * return all the way back to soclose, which marks the socket with 16891c381b19SRobert Watson * SS_NOFDREF, and then calls sofree. Sofree calls sorflush to free 16901c381b19SRobert Watson * up the rights that are queued in messages on the socket A, i.e., 16911c381b19SRobert Watson * the reference on B. The sorflush calls via the dom_dispose switch 16921c381b19SRobert Watson * unp_dispose, which unp_scans with unp_discard. This second 1693df8bae1dSRodney W. Grimes * instance of unp_discard just calls closef on B. 1694df8bae1dSRodney W. Grimes * 1695df8bae1dSRodney W. Grimes * Well, a similar chain occurs on B, resulting in a sorflush on B, 1696df8bae1dSRodney W. Grimes * which results in another closef on A. Unfortunately, A is already 1697df8bae1dSRodney W. Grimes * being closed, and the descriptor has already been marked with 1698df8bae1dSRodney W. Grimes * SS_NOFDREF, and soclose panics at this point. 1699df8bae1dSRodney W. Grimes * 1700df8bae1dSRodney W. Grimes * Here, we first take an extra reference to each inaccessible 17011c381b19SRobert Watson * descriptor. Then, we call sorflush ourself, since we know it is a 17021c381b19SRobert Watson * Unix domain socket anyhow. After we destroy all the rights 17031c381b19SRobert Watson * carried in messages, we do a last closef to get rid of our extra 17041c381b19SRobert Watson * reference. This is the last close, and the unp_detach etc will 17051c381b19SRobert Watson * shut down the socket. 1706df8bae1dSRodney W. Grimes * 1707df8bae1dSRodney W. Grimes * 91/09/19, bsy@cs.cmu.edu 1708df8bae1dSRodney W. Grimes */ 170995f004dcSAlfred Perlstein again: 1710e4643c73SPoul-Henning Kamp nfiles_snap = openfiles + nfiles_slack; /* some slack */ 171195f004dcSAlfred Perlstein extra_ref = malloc(nfiles_snap * sizeof(struct file *), M_TEMP, 171295f004dcSAlfred Perlstein M_WAITOK); 1713426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 1714e4643c73SPoul-Henning Kamp if (nfiles_snap < openfiles) { 171595f004dcSAlfred Perlstein sx_sunlock(&filelist_lock); 171695f004dcSAlfred Perlstein free(extra_ref, M_TEMP); 171795f004dcSAlfred Perlstein nfiles_slack += 20; 171895f004dcSAlfred Perlstein goto again; 171995f004dcSAlfred Perlstein } 1720fc3fcacfSRobert Watson for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; 1721fc3fcacfSRobert Watson fp != NULL; fp = nextfp) { 17222e3c8fcbSPoul-Henning Kamp nextfp = LIST_NEXT(fp, f_list); 1723426da3bcSAlfred Perlstein FILE_LOCK(fp); 1724ed5b7817SJulian Elischer /* 1725ed5b7817SJulian Elischer * If it's not open, skip it 1726ed5b7817SJulian Elischer */ 1727426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1728426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1729df8bae1dSRodney W. Grimes continue; 1730426da3bcSAlfred Perlstein } 1731ed5b7817SJulian Elischer /* 1732ed5b7817SJulian Elischer * If all refs are from msgs, and it's not marked accessible 17331c381b19SRobert Watson * then it must be referenced from some unreachable cycle of 17341c381b19SRobert Watson * (shut-down) FDs, so include it in our list of FDs to 17351c381b19SRobert Watson * remove. 1736ed5b7817SJulian Elischer */ 1737426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) { 1738df8bae1dSRodney W. Grimes *fpp++ = fp; 1739df8bae1dSRodney W. Grimes nunref++; 1740df8bae1dSRodney W. Grimes fp->f_count++; 1741df8bae1dSRodney W. Grimes } 1742426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1743df8bae1dSRodney W. Grimes } 1744426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1745ed5b7817SJulian Elischer /* 17461c381b19SRobert Watson * For each FD on our hit list, do the following two things: 1747ed5b7817SJulian Elischer */ 17481c7c3c6aSMatthew Dillon for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 17491c7c3c6aSMatthew Dillon struct file *tfp = *fpp; 1750426da3bcSAlfred Perlstein FILE_LOCK(tfp); 1751cd72f218SMatthew Dillon if (tfp->f_type == DTYPE_SOCKET && 175248e3128bSMatthew Dillon tfp->f_data != NULL) { 1753426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 175448e3128bSMatthew Dillon sorflush(tfp->f_data); 1755e5aeaa0cSDag-Erling Smørgrav } else { 1756426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 17571c7c3c6aSMatthew Dillon } 1758e5aeaa0cSDag-Erling Smørgrav } 1759a0ec558aSRobert Watson for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 1760b40ce416SJulian Elischer closef(*fpp, (struct thread *) NULL); 1761a0ec558aSRobert Watson unp_recycled++; 1762a0ec558aSRobert Watson } 1763210a5a71SAlfred Perlstein free(extra_ref, M_TEMP); 1764df8bae1dSRodney W. Grimes } 1765df8bae1dSRodney W. Grimes 176626f9a767SRodney W. Grimes void 1767892af6b9SRobert Watson unp_dispose(struct mbuf *m) 1768df8bae1dSRodney W. Grimes { 1769996c772fSJohn Dyson 1770df8bae1dSRodney W. Grimes if (m) 1771df8bae1dSRodney W. Grimes unp_scan(m, unp_discard); 1772df8bae1dSRodney W. Grimes } 1773df8bae1dSRodney W. Grimes 17740c1bb4fbSDima Dorfman static int 1775d374e81eSRobert Watson unp_listen(struct socket *so, struct unpcb *unp, int backlog, 1776d374e81eSRobert Watson struct thread *td) 17770c1bb4fbSDima Dorfman { 17780daccb9cSRobert Watson int error; 17790daccb9cSRobert Watson 17800d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 17810c1bb4fbSDima Dorfman 17820daccb9cSRobert Watson SOCK_LOCK(so); 17830daccb9cSRobert Watson error = solisten_proto_check(so); 17840daccb9cSRobert Watson if (error == 0) { 17856f105b34SJohn Baldwin cru2x(td->td_ucred, &unp->unp_peercred); 17860c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPCCACHED; 1787d374e81eSRobert Watson solisten_proto(so, backlog); 17880daccb9cSRobert Watson } 17890daccb9cSRobert Watson SOCK_UNLOCK(so); 17900daccb9cSRobert Watson return (error); 17910c1bb4fbSDima Dorfman } 17920c1bb4fbSDima Dorfman 1793f708ef1bSPoul-Henning Kamp static void 1794892af6b9SRobert Watson unp_scan(struct mbuf *m0, void (*op)(struct file *)) 1795df8bae1dSRodney W. Grimes { 17962bc21ed9SDavid Malone struct mbuf *m; 17972bc21ed9SDavid Malone struct file **rp; 17982bc21ed9SDavid Malone struct cmsghdr *cm; 17992bc21ed9SDavid Malone void *data; 18002bc21ed9SDavid Malone int i; 18012bc21ed9SDavid Malone socklen_t clen, datalen; 1802df8bae1dSRodney W. Grimes int qfds; 1803df8bae1dSRodney W. Grimes 1804fc3fcacfSRobert Watson while (m0 != NULL) { 18052bc21ed9SDavid Malone for (m = m0; m; m = m->m_next) { 180612396bdcSDavid Malone if (m->m_type != MT_CONTROL) 1807df8bae1dSRodney W. Grimes continue; 18082bc21ed9SDavid Malone 18092bc21ed9SDavid Malone cm = mtod(m, struct cmsghdr *); 18102bc21ed9SDavid Malone clen = m->m_len; 18112bc21ed9SDavid Malone 18122bc21ed9SDavid Malone while (cm != NULL) { 18132bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) 18142bc21ed9SDavid Malone break; 18152bc21ed9SDavid Malone 18162bc21ed9SDavid Malone data = CMSG_DATA(cm); 18172bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len 18182bc21ed9SDavid Malone - (caddr_t)data; 18192bc21ed9SDavid Malone 18202bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET && 18212bc21ed9SDavid Malone cm->cmsg_type == SCM_RIGHTS) { 18222bc21ed9SDavid Malone qfds = datalen / sizeof (struct file *); 18232bc21ed9SDavid Malone rp = data; 1824df8bae1dSRodney W. Grimes for (i = 0; i < qfds; i++) 1825df8bae1dSRodney W. Grimes (*op)(*rp++); 18262bc21ed9SDavid Malone } 18272bc21ed9SDavid Malone 18282bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 18292bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 18302bc21ed9SDavid Malone cm = (struct cmsghdr *) 18312bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 18322bc21ed9SDavid Malone } else { 18332bc21ed9SDavid Malone clen = 0; 18342bc21ed9SDavid Malone cm = NULL; 18352bc21ed9SDavid Malone } 18362bc21ed9SDavid Malone } 1837df8bae1dSRodney W. Grimes } 1838df8bae1dSRodney W. Grimes m0 = m0->m_act; 1839df8bae1dSRodney W. Grimes } 1840df8bae1dSRodney W. Grimes } 1841df8bae1dSRodney W. Grimes 1842f708ef1bSPoul-Henning Kamp static void 1843892af6b9SRobert Watson unp_mark(struct file *fp) 1844df8bae1dSRodney W. Grimes { 1845426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) 1846df8bae1dSRodney W. Grimes return; 1847df8bae1dSRodney W. Grimes unp_defer++; 1848426da3bcSAlfred Perlstein fp->f_gcflag |= (FMARK|FDEFER); 1849df8bae1dSRodney W. Grimes } 1850df8bae1dSRodney W. Grimes 1851f708ef1bSPoul-Henning Kamp static void 1852892af6b9SRobert Watson unp_discard(struct file *fp) 1853df8bae1dSRodney W. Grimes { 1854a0ec558aSRobert Watson UNP_LOCK(); 1855426da3bcSAlfred Perlstein FILE_LOCK(fp); 1856df8bae1dSRodney W. Grimes fp->f_msgcount--; 1857df8bae1dSRodney W. Grimes unp_rights--; 1858426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1859a0ec558aSRobert Watson UNP_UNLOCK(); 1860b40ce416SJulian Elischer (void) closef(fp, (struct thread *)NULL); 1861df8bae1dSRodney W. Grimes } 1862