19454b2d8SWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1991, 1993 3e1ac28e2SRobert Watson * The Regents of the University of California. 44d4b555eSRobert Watson * Copyright 2004-2006 Robert N. M. Watson 5e1ac28e2SRobert Watson * All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 8df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 9df8bae1dSRodney W. Grimes * are met: 10df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 12df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 14df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 15df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 16df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 17df8bae1dSRodney W. Grimes * without specific prior written permission. 18df8bae1dSRodney W. Grimes * 19df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29df8bae1dSRodney W. Grimes * SUCH DAMAGE. 30df8bae1dSRodney W. Grimes * 31748e0b0aSGarrett Wollman * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94 32df8bae1dSRodney W. Grimes */ 33df8bae1dSRodney W. Grimes 34677b542eSDavid E. O'Brien #include <sys/cdefs.h> 35677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 36677b542eSDavid E. O'Brien 37335654d7SRobert Watson #include "opt_mac.h" 38335654d7SRobert Watson 39df8bae1dSRodney W. Grimes #include <sys/param.h> 40fb919e4dSMark Murray #include <sys/domain.h> 41960ed29cSSeigo Tanimura #include <sys/fcntl.h> 42d826c479SBruce Evans #include <sys/malloc.h> /* XXX must be before <sys/file.h> */ 434f590175SPaul Saab #include <sys/eventhandler.h> 44639acc13SGarrett Wollman #include <sys/file.h> 45960ed29cSSeigo Tanimura #include <sys/filedesc.h> 46960ed29cSSeigo Tanimura #include <sys/jail.h> 47960ed29cSSeigo Tanimura #include <sys/kernel.h> 48960ed29cSSeigo Tanimura #include <sys/lock.h> 496ea48a90SRobert Watson #include <sys/mac.h> 50639acc13SGarrett Wollman #include <sys/mbuf.h> 51033eb86eSJeff Roberson #include <sys/mount.h> 52960ed29cSSeigo Tanimura #include <sys/mutex.h> 53639acc13SGarrett Wollman #include <sys/namei.h> 54639acc13SGarrett Wollman #include <sys/proc.h> 55df8bae1dSRodney W. Grimes #include <sys/protosw.h> 56960ed29cSSeigo Tanimura #include <sys/resourcevar.h> 57df8bae1dSRodney W. Grimes #include <sys/socket.h> 58df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 59960ed29cSSeigo Tanimura #include <sys/signalvar.h> 60df8bae1dSRodney W. Grimes #include <sys/stat.h> 61960ed29cSSeigo Tanimura #include <sys/sx.h> 62639acc13SGarrett Wollman #include <sys/sysctl.h> 63960ed29cSSeigo Tanimura #include <sys/systm.h> 64a0ec558aSRobert Watson #include <sys/taskqueue.h> 65639acc13SGarrett Wollman #include <sys/un.h> 6698271db4SGarrett Wollman #include <sys/unpcb.h> 67639acc13SGarrett Wollman #include <sys/vnode.h> 68df8bae1dSRodney W. Grimes 699e9d298aSJeff Roberson #include <vm/uma.h> 7098271db4SGarrett Wollman 719e9d298aSJeff Roberson static uma_zone_t unp_zone; 7298271db4SGarrett Wollman static unp_gen_t unp_gencnt; 7398271db4SGarrett Wollman static u_int unp_count; 7498271db4SGarrett Wollman 7598271db4SGarrett Wollman static struct unp_head unp_shead, unp_dhead; 7698271db4SGarrett Wollman 77df8bae1dSRodney W. Grimes /* 78df8bae1dSRodney W. Grimes * Unix communications domain. 79df8bae1dSRodney W. Grimes * 80df8bae1dSRodney W. Grimes * TODO: 81df8bae1dSRodney W. Grimes * SEQPACKET, RDM 82df8bae1dSRodney W. Grimes * rethink name space problems 83df8bae1dSRodney W. Grimes * need a proper out-of-band 8498271db4SGarrett Wollman * lock pushdown 85df8bae1dSRodney W. Grimes */ 86e7dd9a10SRobert Watson static const struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 87f708ef1bSPoul-Henning Kamp static ino_t unp_ino; /* prototype for fake inode numbers */ 886a2989fdSMatthew N. Dodd struct mbuf *unp_addsockcred(struct thread *, struct mbuf *); 89f708ef1bSPoul-Henning Kamp 90ce5f32deSRobert Watson /* 917e711c3aSRobert Watson * Both send and receive buffers are allocated PIPSIZ bytes of buffering for 927e711c3aSRobert Watson * stream sockets, although the total for sender and receiver is actually 937e711c3aSRobert Watson * only PIPSIZ. 947e711c3aSRobert Watson * 957e711c3aSRobert Watson * Datagram sockets really use the sendspace as the maximum datagram size, 967e711c3aSRobert Watson * and don't really want to reserve the sendspace. Their recvspace should be 977e711c3aSRobert Watson * large enough for at least one max-size datagram plus address. 987e711c3aSRobert Watson */ 997e711c3aSRobert Watson #ifndef PIPSIZ 1007e711c3aSRobert Watson #define PIPSIZ 8192 1017e711c3aSRobert Watson #endif 1027e711c3aSRobert Watson static u_long unpst_sendspace = PIPSIZ; 1037e711c3aSRobert Watson static u_long unpst_recvspace = PIPSIZ; 1047e711c3aSRobert Watson static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 1057e711c3aSRobert Watson static u_long unpdg_recvspace = 4*1024; 1067e711c3aSRobert Watson 1077e711c3aSRobert Watson static int unp_rights; /* file descriptors in flight */ 1087e711c3aSRobert Watson 1097e711c3aSRobert Watson SYSCTL_DECL(_net_local_stream); 1107e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 1117e711c3aSRobert Watson &unpst_sendspace, 0, ""); 1127e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 1137e711c3aSRobert Watson &unpst_recvspace, 0, ""); 1147e711c3aSRobert Watson SYSCTL_DECL(_net_local_dgram); 1157e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 1167e711c3aSRobert Watson &unpdg_sendspace, 0, ""); 1177e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 1187e711c3aSRobert Watson &unpdg_recvspace, 0, ""); 1197e711c3aSRobert Watson SYSCTL_DECL(_net_local); 1207e711c3aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); 1217e711c3aSRobert Watson 1227e711c3aSRobert Watson /* 123ce5f32deSRobert Watson * Currently, UNIX domain sockets are protected by a single subsystem lock, 124ce5f32deSRobert Watson * which covers global data structures and variables, the contents of each 125ce5f32deSRobert Watson * per-socket unpcb structure, and the so_pcb field in sockets attached to 126ce5f32deSRobert Watson * the UNIX domain. This provides for a moderate degree of paralellism, as 127ce5f32deSRobert Watson * receive operations on UNIX domain sockets do not need to acquire the 128ce5f32deSRobert Watson * subsystem lock. Finer grained locking to permit send() without acquiring 129ce5f32deSRobert Watson * a global lock would be a logical next step. 130ce5f32deSRobert Watson * 131ce5f32deSRobert Watson * The UNIX domain socket lock preceds all socket layer locks, including the 132ce5f32deSRobert Watson * socket lock and socket buffer lock, permitting UNIX domain socket code to 133ce5f32deSRobert Watson * call into socket support routines without releasing its locks. 134ce5f32deSRobert Watson * 135ce5f32deSRobert Watson * Some caution is required in areas where the UNIX domain socket code enters 136ce5f32deSRobert Watson * VFS in order to create or find rendezvous points. This results in 137ce5f32deSRobert Watson * dropping of the UNIX domain socket subsystem lock, acquisition of the 138ce5f32deSRobert Watson * Giant lock, and potential sleeping. This increases the chances of races, 139ce5f32deSRobert Watson * and exposes weaknesses in the socket->protocol API by offering poor 140ce5f32deSRobert Watson * failure modes. 141ce5f32deSRobert Watson */ 1420d9ce3a1SRobert Watson static struct mtx unp_mtx; 1430d9ce3a1SRobert Watson #define UNP_LOCK_INIT() \ 1440d9ce3a1SRobert Watson mtx_init(&unp_mtx, "unp", NULL, MTX_DEF) 1450d9ce3a1SRobert Watson #define UNP_LOCK() mtx_lock(&unp_mtx) 1460d9ce3a1SRobert Watson #define UNP_UNLOCK() mtx_unlock(&unp_mtx) 1470d9ce3a1SRobert Watson #define UNP_LOCK_ASSERT() mtx_assert(&unp_mtx, MA_OWNED) 1484c5bc1caSRobert Watson #define UNP_UNLOCK_ASSERT() mtx_assert(&unp_mtx, MA_NOTOWNED) 1490d9ce3a1SRobert Watson 150a0ec558aSRobert Watson /* 151a0ec558aSRobert Watson * Garbage collection of cyclic file descriptor/socket references occurs 152a0ec558aSRobert Watson * asynchronously in a taskqueue context in order to avoid recursion and 153a0ec558aSRobert Watson * reentrance in the UNIX domain socket, file descriptor, and socket layer 154a0ec558aSRobert Watson * code. See unp_gc() for a full description. 155a0ec558aSRobert Watson */ 156a0ec558aSRobert Watson static struct task unp_gc_task; 157a0ec558aSRobert Watson 15870f52b48SBruce Evans static int unp_connect(struct socket *,struct sockaddr *, struct thread *); 1596a2989fdSMatthew N. Dodd static int unp_connect2(struct socket *so, struct socket *so2, int); 1604d77a549SAlfred Perlstein static void unp_disconnect(struct unpcb *); 1614d77a549SAlfred Perlstein static void unp_shutdown(struct unpcb *); 1624d77a549SAlfred Perlstein static void unp_drop(struct unpcb *, int); 163a0ec558aSRobert Watson static void unp_gc(__unused void *, int); 1644d77a549SAlfred Perlstein static void unp_scan(struct mbuf *, void (*)(struct file *)); 1654d77a549SAlfred Perlstein static void unp_mark(struct file *); 1664d77a549SAlfred Perlstein static void unp_discard(struct file *); 1674d77a549SAlfred Perlstein static void unp_freerights(struct file **, int); 1684d77a549SAlfred Perlstein static int unp_internalize(struct mbuf **, struct thread *); 169d374e81eSRobert Watson static int unp_listen(struct socket *, struct unpcb *, int, 170d374e81eSRobert Watson struct thread *); 171f708ef1bSPoul-Henning Kamp 172ac45e92fSRobert Watson static void 173a29f300eSGarrett Wollman uipc_abort(struct socket *so) 174df8bae1dSRodney W. Grimes { 17540f2ac28SRobert Watson struct unpcb *unp; 176df8bae1dSRodney W. Grimes 17740f2ac28SRobert Watson unp = sotounpcb(so); 1784d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_abort: unp == NULL")); 1794d4b555eSRobert Watson UNP_LOCK(); 180a29f300eSGarrett Wollman unp_drop(unp, ECONNABORTED); 181a152f8a3SRobert Watson UNP_UNLOCK(); 182df8bae1dSRodney W. Grimes } 183df8bae1dSRodney W. Grimes 184a29f300eSGarrett Wollman static int 18557bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam) 186a29f300eSGarrett Wollman { 18740f2ac28SRobert Watson struct unpcb *unp; 1880d9ce3a1SRobert Watson const struct sockaddr *sa; 189df8bae1dSRodney W. Grimes 190df8bae1dSRodney W. Grimes /* 1911c381b19SRobert Watson * Pass back name of connected socket, if it was bound and we are 1921c381b19SRobert Watson * still connected (our peer may have closed already!). 193df8bae1dSRodney W. Grimes */ 1944d4b555eSRobert Watson unp = sotounpcb(so); 1954d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_accept: unp == NULL")); 1960d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 1970d9ce3a1SRobert Watson UNP_LOCK(); 1980d9ce3a1SRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr != NULL) 1990d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 2000d9ce3a1SRobert Watson else 2010d9ce3a1SRobert Watson sa = &sun_noname; 2020d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 2030d9ce3a1SRobert Watson UNP_UNLOCK(); 204e5aeaa0cSDag-Erling Smørgrav return (0); 205a29f300eSGarrett Wollman } 206df8bae1dSRodney W. Grimes 207a29f300eSGarrett Wollman static int 208b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td) 209a29f300eSGarrett Wollman { 2106d32873cSRobert Watson struct unpcb *unp; 2116d32873cSRobert Watson int error; 212df8bae1dSRodney W. Grimes 2136d32873cSRobert Watson KASSERT(so->so_pcb == NULL, ("uipc_attach: so_pcb != NULL")); 2146d32873cSRobert Watson if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 2156d32873cSRobert Watson switch (so->so_type) { 2166d32873cSRobert Watson 2176d32873cSRobert Watson case SOCK_STREAM: 2186d32873cSRobert Watson error = soreserve(so, unpst_sendspace, unpst_recvspace); 2196d32873cSRobert Watson break; 2206d32873cSRobert Watson 2216d32873cSRobert Watson case SOCK_DGRAM: 2226d32873cSRobert Watson error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 2236d32873cSRobert Watson break; 2246d32873cSRobert Watson 2256d32873cSRobert Watson default: 2266d32873cSRobert Watson panic("unp_attach"); 2276d32873cSRobert Watson } 2286d32873cSRobert Watson if (error) 2296d32873cSRobert Watson return (error); 2306d32873cSRobert Watson } 2316d32873cSRobert Watson unp = uma_zalloc(unp_zone, M_WAITOK | M_ZERO); 2326d32873cSRobert Watson if (unp == NULL) 2336d32873cSRobert Watson return (ENOBUFS); 2346d32873cSRobert Watson LIST_INIT(&unp->unp_refs); 2356d32873cSRobert Watson unp->unp_socket = so; 2366d32873cSRobert Watson so->so_pcb = unp; 2376d32873cSRobert Watson 2386d32873cSRobert Watson UNP_LOCK(); 2396d32873cSRobert Watson unp->unp_gencnt = ++unp_gencnt; 2406d32873cSRobert Watson unp_count++; 2416d32873cSRobert Watson LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead 2426d32873cSRobert Watson : &unp_shead, unp, unp_link); 2436d32873cSRobert Watson UNP_UNLOCK(); 2446d32873cSRobert Watson 2456d32873cSRobert Watson return (0); 246a29f300eSGarrett Wollman } 247a29f300eSGarrett Wollman 248a29f300eSGarrett Wollman static int 249b40ce416SJulian Elischer uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 250a29f300eSGarrett Wollman { 251dd47f5caSRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam; 252dd47f5caSRobert Watson struct vattr vattr; 253dd47f5caSRobert Watson int error, namelen; 254dd47f5caSRobert Watson struct nameidata nd; 25540f2ac28SRobert Watson struct unpcb *unp; 256dd47f5caSRobert Watson struct vnode *vp; 257dd47f5caSRobert Watson struct mount *mp; 258dd47f5caSRobert Watson char *buf; 259a29f300eSGarrett Wollman 26040f2ac28SRobert Watson unp = sotounpcb(so); 2614d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_bind: unp == NULL")); 2624d4b555eSRobert Watson UNP_LOCK(); 263dd47f5caSRobert Watson 264dd47f5caSRobert Watson /* 265dd47f5caSRobert Watson * XXXRW: This test-and-set of unp_vnode is non-atomic; the unlocked 266dd47f5caSRobert Watson * read here is fine, but the value of unp_vnode needs to be tested 267dd47f5caSRobert Watson * again after we do all the lookups to see if the pcb is still 268dd47f5caSRobert Watson * unbound? 269dd47f5caSRobert Watson */ 270dd47f5caSRobert Watson if (unp->unp_vnode != NULL) { 27140f2ac28SRobert Watson UNP_UNLOCK(); 272dd47f5caSRobert Watson return (EINVAL); 273dd47f5caSRobert Watson } 274dd47f5caSRobert Watson 275dd47f5caSRobert Watson namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 276dd47f5caSRobert Watson if (namelen <= 0) { 277dd47f5caSRobert Watson UNP_UNLOCK(); 278dd47f5caSRobert Watson return (EINVAL); 279dd47f5caSRobert Watson } 280dd47f5caSRobert Watson 281dd47f5caSRobert Watson UNP_UNLOCK(); 282dd47f5caSRobert Watson 283dd47f5caSRobert Watson buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 284dd47f5caSRobert Watson strlcpy(buf, soun->sun_path, namelen + 1); 285dd47f5caSRobert Watson 286dd47f5caSRobert Watson mtx_lock(&Giant); 287dd47f5caSRobert Watson restart: 288dd47f5caSRobert Watson mtx_assert(&Giant, MA_OWNED); 289dd47f5caSRobert Watson NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME, UIO_SYSSPACE, 290dd47f5caSRobert Watson buf, td); 291dd47f5caSRobert Watson /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 292dd47f5caSRobert Watson error = namei(&nd); 293dd47f5caSRobert Watson if (error) 294dd47f5caSRobert Watson goto done; 295dd47f5caSRobert Watson vp = nd.ni_vp; 296dd47f5caSRobert Watson if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 297dd47f5caSRobert Watson NDFREE(&nd, NDF_ONLY_PNBUF); 298dd47f5caSRobert Watson if (nd.ni_dvp == vp) 299dd47f5caSRobert Watson vrele(nd.ni_dvp); 300dd47f5caSRobert Watson else 301dd47f5caSRobert Watson vput(nd.ni_dvp); 302dd47f5caSRobert Watson if (vp != NULL) { 303dd47f5caSRobert Watson vrele(vp); 304dd47f5caSRobert Watson error = EADDRINUSE; 305dd47f5caSRobert Watson goto done; 306dd47f5caSRobert Watson } 307dd47f5caSRobert Watson error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 308dd47f5caSRobert Watson if (error) 309dd47f5caSRobert Watson goto done; 310dd47f5caSRobert Watson goto restart; 311dd47f5caSRobert Watson } 312dd47f5caSRobert Watson VATTR_NULL(&vattr); 313dd47f5caSRobert Watson vattr.va_type = VSOCK; 314dd47f5caSRobert Watson vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 315dd47f5caSRobert Watson #ifdef MAC 316dd47f5caSRobert Watson error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 317dd47f5caSRobert Watson &vattr); 318dd47f5caSRobert Watson #endif 319dd47f5caSRobert Watson if (error == 0) { 320dd47f5caSRobert Watson VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE); 321dd47f5caSRobert Watson error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 322dd47f5caSRobert Watson } 323dd47f5caSRobert Watson NDFREE(&nd, NDF_ONLY_PNBUF); 324dd47f5caSRobert Watson vput(nd.ni_dvp); 325dd47f5caSRobert Watson if (error) { 326dd47f5caSRobert Watson vn_finished_write(mp); 327dd47f5caSRobert Watson goto done; 328dd47f5caSRobert Watson } 329dd47f5caSRobert Watson vp = nd.ni_vp; 330dd47f5caSRobert Watson ASSERT_VOP_LOCKED(vp, "unp_bind"); 331dd47f5caSRobert Watson soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 332dd47f5caSRobert Watson UNP_LOCK(); 333dd47f5caSRobert Watson vp->v_socket = unp->unp_socket; 334dd47f5caSRobert Watson unp->unp_vnode = vp; 335dd47f5caSRobert Watson unp->unp_addr = soun; 336dd47f5caSRobert Watson UNP_UNLOCK(); 337dd47f5caSRobert Watson VOP_UNLOCK(vp, 0, td); 338dd47f5caSRobert Watson vn_finished_write(mp); 339dd47f5caSRobert Watson done: 340dd47f5caSRobert Watson mtx_unlock(&Giant); 341dd47f5caSRobert Watson free(buf, M_TEMP); 34240f2ac28SRobert Watson return (error); 343a29f300eSGarrett Wollman } 344a29f300eSGarrett Wollman 345a29f300eSGarrett Wollman static int 346b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 347a29f300eSGarrett Wollman { 3480d9ce3a1SRobert Watson int error; 349a29f300eSGarrett Wollman 350fd179ee9SRobert Watson KASSERT(td == curthread, ("uipc_connect: td != curthread")); 3514d4b555eSRobert Watson UNP_LOCK(); 352fd179ee9SRobert Watson error = unp_connect(so, nam, td); 3530d9ce3a1SRobert Watson UNP_UNLOCK(); 3540d9ce3a1SRobert Watson return (error); 355a29f300eSGarrett Wollman } 356a29f300eSGarrett Wollman 357a152f8a3SRobert Watson /* 358a152f8a3SRobert Watson * XXXRW: Should also unbind? 359a152f8a3SRobert Watson */ 360a152f8a3SRobert Watson static void 361a152f8a3SRobert Watson uipc_close(struct socket *so) 362a152f8a3SRobert Watson { 363a152f8a3SRobert Watson struct unpcb *unp; 364a152f8a3SRobert Watson 365a152f8a3SRobert Watson unp = sotounpcb(so); 366a152f8a3SRobert Watson KASSERT(unp != NULL, ("uipc_close: unp == NULL")); 367a152f8a3SRobert Watson UNP_LOCK(); 368a152f8a3SRobert Watson unp_disconnect(unp); 369a152f8a3SRobert Watson UNP_UNLOCK(); 370a152f8a3SRobert Watson } 371a152f8a3SRobert Watson 372db48c0d2SRobert Watson int 373a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2) 374a29f300eSGarrett Wollman { 37540f2ac28SRobert Watson struct unpcb *unp; 3760d9ce3a1SRobert Watson int error; 377a29f300eSGarrett Wollman 37840f2ac28SRobert Watson unp = sotounpcb(so1); 3794d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_connect2: unp == NULL")); 3804d4b555eSRobert Watson UNP_LOCK(); 3816a2989fdSMatthew N. Dodd error = unp_connect2(so1, so2, PRU_CONNECT2); 3820d9ce3a1SRobert Watson UNP_UNLOCK(); 3830d9ce3a1SRobert Watson return (error); 384a29f300eSGarrett Wollman } 385a29f300eSGarrett Wollman 386a29f300eSGarrett Wollman /* control is EOPNOTSUPP */ 387a29f300eSGarrett Wollman 388bc725eafSRobert Watson static void 389a29f300eSGarrett Wollman uipc_detach(struct socket *so) 390a29f300eSGarrett Wollman { 3916d32873cSRobert Watson int local_unp_rights; 39240f2ac28SRobert Watson struct unpcb *unp; 3936d32873cSRobert Watson struct vnode *vp; 394a29f300eSGarrett Wollman 39540f2ac28SRobert Watson unp = sotounpcb(so); 3964d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_detach: unp == NULL")); 3974d4b555eSRobert Watson UNP_LOCK(); 3986d32873cSRobert Watson LIST_REMOVE(unp, unp_link); 3996d32873cSRobert Watson unp->unp_gencnt = ++unp_gencnt; 4006d32873cSRobert Watson --unp_count; 4016d32873cSRobert Watson if ((vp = unp->unp_vnode) != NULL) { 4026d32873cSRobert Watson /* 4036d32873cSRobert Watson * XXXRW: should v_socket be frobbed only while holding 4046d32873cSRobert Watson * Giant? 4056d32873cSRobert Watson */ 4066d32873cSRobert Watson unp->unp_vnode->v_socket = NULL; 4076d32873cSRobert Watson unp->unp_vnode = NULL; 4086d32873cSRobert Watson } 4096d32873cSRobert Watson if (unp->unp_conn != NULL) 4106d32873cSRobert Watson unp_disconnect(unp); 4116d32873cSRobert Watson while (!LIST_EMPTY(&unp->unp_refs)) { 4126d32873cSRobert Watson struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 4136d32873cSRobert Watson unp_drop(ref, ECONNRESET); 4146d32873cSRobert Watson } 4156d32873cSRobert Watson soisdisconnected(unp->unp_socket); 4166d32873cSRobert Watson unp->unp_socket->so_pcb = NULL; 4176d32873cSRobert Watson local_unp_rights = unp_rights; 4186d32873cSRobert Watson UNP_UNLOCK(); 4196d32873cSRobert Watson if (unp->unp_addr != NULL) 4206d32873cSRobert Watson FREE(unp->unp_addr, M_SONAME); 4216d32873cSRobert Watson uma_zfree(unp_zone, unp); 4226d32873cSRobert Watson if (vp) { 4236d32873cSRobert Watson int vfslocked; 4246d32873cSRobert Watson 4256d32873cSRobert Watson vfslocked = VFS_LOCK_GIANT(vp->v_mount); 4266d32873cSRobert Watson vrele(vp); 4276d32873cSRobert Watson VFS_UNLOCK_GIANT(vfslocked); 4286d32873cSRobert Watson } 4296d32873cSRobert Watson if (local_unp_rights) 4306d32873cSRobert Watson taskqueue_enqueue(taskqueue_thread, &unp_gc_task); 431a29f300eSGarrett Wollman } 432a29f300eSGarrett Wollman 433a29f300eSGarrett Wollman static int 434a29f300eSGarrett Wollman uipc_disconnect(struct socket *so) 435a29f300eSGarrett Wollman { 43640f2ac28SRobert Watson struct unpcb *unp; 437a29f300eSGarrett Wollman 43840f2ac28SRobert Watson unp = sotounpcb(so); 4394d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_disconnect: unp == NULL")); 4404d4b555eSRobert Watson UNP_LOCK(); 441a29f300eSGarrett Wollman unp_disconnect(unp); 4420d9ce3a1SRobert Watson UNP_UNLOCK(); 443e5aeaa0cSDag-Erling Smørgrav return (0); 444a29f300eSGarrett Wollman } 445a29f300eSGarrett Wollman 446a29f300eSGarrett Wollman static int 447d374e81eSRobert Watson uipc_listen(struct socket *so, int backlog, struct thread *td) 448a29f300eSGarrett Wollman { 44940f2ac28SRobert Watson struct unpcb *unp; 4500d9ce3a1SRobert Watson int error; 451a29f300eSGarrett Wollman 45240f2ac28SRobert Watson unp = sotounpcb(so); 4534d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_listen: unp == NULL")); 4544d4b555eSRobert Watson UNP_LOCK(); 4554d4b555eSRobert Watson if (unp->unp_vnode == NULL) { 45640f2ac28SRobert Watson UNP_UNLOCK(); 45740f2ac28SRobert Watson return (EINVAL); 45840f2ac28SRobert Watson } 459d374e81eSRobert Watson error = unp_listen(so, unp, backlog, td); 4600d9ce3a1SRobert Watson UNP_UNLOCK(); 4610d9ce3a1SRobert Watson return (error); 462a29f300eSGarrett Wollman } 463a29f300eSGarrett Wollman 464a29f300eSGarrett Wollman static int 46557bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 466a29f300eSGarrett Wollman { 46740f2ac28SRobert Watson struct unpcb *unp; 4680d9ce3a1SRobert Watson const struct sockaddr *sa; 469a29f300eSGarrett Wollman 4704d4b555eSRobert Watson unp = sotounpcb(so); 4714d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL")); 4720d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 4730d9ce3a1SRobert Watson UNP_LOCK(); 474fc3fcacfSRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr!= NULL) 4750d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 476bdc5f6a3SHajimu UMEMOTO else { 477bdc5f6a3SHajimu UMEMOTO /* 478bdc5f6a3SHajimu UMEMOTO * XXX: It seems that this test always fails even when 479bdc5f6a3SHajimu UMEMOTO * connection is established. So, this else clause is 480bdc5f6a3SHajimu UMEMOTO * added as workaround to return PF_LOCAL sockaddr. 481bdc5f6a3SHajimu UMEMOTO */ 4820d9ce3a1SRobert Watson sa = &sun_noname; 483bdc5f6a3SHajimu UMEMOTO } 4840d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 4850d9ce3a1SRobert Watson UNP_UNLOCK(); 486e5aeaa0cSDag-Erling Smørgrav return (0); 487a29f300eSGarrett Wollman } 488a29f300eSGarrett Wollman 489a29f300eSGarrett Wollman static int 490a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 491a29f300eSGarrett Wollman { 49240f2ac28SRobert Watson struct unpcb *unp; 493a29f300eSGarrett Wollman struct socket *so2; 494337cc6b6SRobert Watson u_int mbcnt, sbcc; 4956aef685fSBrian Feldman u_long newhiwat; 496a29f300eSGarrett Wollman 49740f2ac28SRobert Watson unp = sotounpcb(so); 4984d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_rcvd: unp == NULL")); 499df8bae1dSRodney W. Grimes switch (so->so_type) { 500df8bae1dSRodney W. Grimes case SOCK_DGRAM: 501a29f300eSGarrett Wollman panic("uipc_rcvd DGRAM?"); 502df8bae1dSRodney W. Grimes /*NOTREACHED*/ 503df8bae1dSRodney W. Grimes 504df8bae1dSRodney W. Grimes case SOCK_STREAM: 505df8bae1dSRodney W. Grimes /* 5061c381b19SRobert Watson * Adjust backpressure on sender and wakeup any waiting to 5071c381b19SRobert Watson * write. 508df8bae1dSRodney W. Grimes */ 509337cc6b6SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 510337cc6b6SRobert Watson mbcnt = so->so_rcv.sb_mbcnt; 511337cc6b6SRobert Watson sbcc = so->so_rcv.sb_cc; 512337cc6b6SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 513337cc6b6SRobert Watson UNP_LOCK(); 514337cc6b6SRobert Watson if (unp->unp_conn == NULL) { 515337cc6b6SRobert Watson UNP_UNLOCK(); 516337cc6b6SRobert Watson break; 517337cc6b6SRobert Watson } 518337cc6b6SRobert Watson so2 = unp->unp_conn->unp_socket; 519337cc6b6SRobert Watson SOCKBUF_LOCK(&so2->so_snd); 520337cc6b6SRobert Watson so2->so_snd.sb_mbmax += unp->unp_mbcnt - mbcnt; 521337cc6b6SRobert Watson newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - sbcc; 522f535380cSDon Lewis (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat, 5236aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 5241e4d7da7SRobert Watson sowwakeup_locked(so2); 525337cc6b6SRobert Watson unp->unp_mbcnt = mbcnt; 526337cc6b6SRobert Watson unp->unp_cc = sbcc; 527337cc6b6SRobert Watson UNP_UNLOCK(); 528df8bae1dSRodney W. Grimes break; 529df8bae1dSRodney W. Grimes 530df8bae1dSRodney W. Grimes default: 531a29f300eSGarrett Wollman panic("uipc_rcvd unknown socktype"); 532df8bae1dSRodney W. Grimes } 533e5aeaa0cSDag-Erling Smørgrav return (0); 534a29f300eSGarrett Wollman } 535df8bae1dSRodney W. Grimes 536a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 537a29f300eSGarrett Wollman 538a29f300eSGarrett Wollman static int 53957bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 540b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 541a29f300eSGarrett Wollman { 542f3f49bbbSRobert Watson struct unpcb *unp, *unp2; 543a29f300eSGarrett Wollman struct socket *so2; 544337cc6b6SRobert Watson u_int mbcnt, sbcc; 5456aef685fSBrian Feldman u_long newhiwat; 546f3f49bbbSRobert Watson int error = 0; 547a29f300eSGarrett Wollman 54840f2ac28SRobert Watson unp = sotounpcb(so); 5494d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_send: unp == NULL")); 550a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 551a29f300eSGarrett Wollman error = EOPNOTSUPP; 552a29f300eSGarrett Wollman goto release; 553a29f300eSGarrett Wollman } 554a29f300eSGarrett Wollman 555fc3fcacfSRobert Watson if (control != NULL && (error = unp_internalize(&control, td))) 556a29f300eSGarrett Wollman goto release; 557df8bae1dSRodney W. Grimes 5580d9ce3a1SRobert Watson UNP_LOCK(); 559a29f300eSGarrett Wollman switch (so->so_type) { 560a29f300eSGarrett Wollman case SOCK_DGRAM: 561a29f300eSGarrett Wollman { 562e7dd9a10SRobert Watson const struct sockaddr *from; 563df8bae1dSRodney W. Grimes 564fc3fcacfSRobert Watson if (nam != NULL) { 565fc3fcacfSRobert Watson if (unp->unp_conn != NULL) { 566df8bae1dSRodney W. Grimes error = EISCONN; 567df8bae1dSRodney W. Grimes break; 568df8bae1dSRodney W. Grimes } 569b40ce416SJulian Elischer error = unp_connect(so, nam, td); 570df8bae1dSRodney W. Grimes if (error) 571df8bae1dSRodney W. Grimes break; 572df8bae1dSRodney W. Grimes } else { 573fc3fcacfSRobert Watson if (unp->unp_conn == NULL) { 574df8bae1dSRodney W. Grimes error = ENOTCONN; 575df8bae1dSRodney W. Grimes break; 576df8bae1dSRodney W. Grimes } 577df8bae1dSRodney W. Grimes } 578f3f49bbbSRobert Watson unp2 = unp->unp_conn; 579f3f49bbbSRobert Watson so2 = unp2->unp_socket; 580fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 58157bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 582df8bae1dSRodney W. Grimes else 583df8bae1dSRodney W. Grimes from = &sun_noname; 584f3f49bbbSRobert Watson if (unp2->unp_flags & UNP_WANTCRED) 5856a2989fdSMatthew N. Dodd control = unp_addsockcred(td, control); 586a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 587a34b7046SRobert Watson if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) { 5881e4d7da7SRobert Watson sorwakeup_locked(so2); 589fc3fcacfSRobert Watson m = NULL; 590fc3fcacfSRobert Watson control = NULL; 591e5aeaa0cSDag-Erling Smørgrav } else { 592a34b7046SRobert Watson SOCKBUF_UNLOCK(&so2->so_rcv); 593df8bae1dSRodney W. Grimes error = ENOBUFS; 594e5aeaa0cSDag-Erling Smørgrav } 595fc3fcacfSRobert Watson if (nam != NULL) 596df8bae1dSRodney W. Grimes unp_disconnect(unp); 597df8bae1dSRodney W. Grimes break; 598df8bae1dSRodney W. Grimes } 599df8bae1dSRodney W. Grimes 600df8bae1dSRodney W. Grimes case SOCK_STREAM: 6016b8fda4dSGarrett Wollman /* 6021c381b19SRobert Watson * Connect if not connected yet. 6031c381b19SRobert Watson * 6041c381b19SRobert Watson * Note: A better implementation would complain if not equal 6051c381b19SRobert Watson * to the peer's address. 6066b8fda4dSGarrett Wollman */ 607402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 608fc3fcacfSRobert Watson if (nam != NULL) { 609b40ce416SJulian Elischer error = unp_connect(so, nam, td); 610402cc72dSDavid Greenman if (error) 6116b8fda4dSGarrett Wollman break; /* XXX */ 612402cc72dSDavid Greenman } else { 613402cc72dSDavid Greenman error = ENOTCONN; 614402cc72dSDavid Greenman break; 615402cc72dSDavid Greenman } 616402cc72dSDavid Greenman } 617402cc72dSDavid Greenman 618337cc6b6SRobert Watson /* Lockless read. */ 619c0b99ffaSRobert Watson if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 620df8bae1dSRodney W. Grimes error = EPIPE; 621df8bae1dSRodney W. Grimes break; 622df8bae1dSRodney W. Grimes } 623f3f49bbbSRobert Watson unp2 = unp->unp_conn; 624f3f49bbbSRobert Watson if (unp2 == NULL) 625a29f300eSGarrett Wollman panic("uipc_send connected but no connection?"); 626f3f49bbbSRobert Watson so2 = unp2->unp_socket; 627a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 628f3f49bbbSRobert Watson if (unp2->unp_flags & UNP_WANTCRED) { 6296a2989fdSMatthew N. Dodd /* 6306a2989fdSMatthew N. Dodd * Credentials are passed only once on 6316a2989fdSMatthew N. Dodd * SOCK_STREAM. 6326a2989fdSMatthew N. Dodd */ 633f3f49bbbSRobert Watson unp2->unp_flags &= ~UNP_WANTCRED; 6346a2989fdSMatthew N. Dodd control = unp_addsockcred(td, control); 6356a2989fdSMatthew N. Dodd } 636df8bae1dSRodney W. Grimes /* 6371c381b19SRobert Watson * Send to paired receive port, and then reduce send buffer 6381c381b19SRobert Watson * hiwater marks to maintain backpressure. Wake up readers. 639df8bae1dSRodney W. Grimes */ 640fc3fcacfSRobert Watson if (control != NULL) { 641a34b7046SRobert Watson if (sbappendcontrol_locked(&so2->so_rcv, m, control)) 642fc3fcacfSRobert Watson control = NULL; 643e5aeaa0cSDag-Erling Smørgrav } else { 644a34b7046SRobert Watson sbappend_locked(&so2->so_rcv, m); 645e5aeaa0cSDag-Erling Smørgrav } 646f3f49bbbSRobert Watson mbcnt = so2->so_rcv.sb_mbcnt - unp2->unp_mbcnt; 647f3f49bbbSRobert Watson unp2->unp_mbcnt = so2->so_rcv.sb_mbcnt; 648337cc6b6SRobert Watson sbcc = so2->so_rcv.sb_cc; 649337cc6b6SRobert Watson sorwakeup_locked(so2); 650337cc6b6SRobert Watson 651337cc6b6SRobert Watson SOCKBUF_LOCK(&so->so_snd); 652f3f49bbbSRobert Watson newhiwat = so->so_snd.sb_hiwat - (sbcc - unp2->unp_cc); 653f535380cSDon Lewis (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat, 6546aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 655337cc6b6SRobert Watson so->so_snd.sb_mbmax -= mbcnt; 6567abe2ac2SAlan Cox SOCKBUF_UNLOCK(&so->so_snd); 657337cc6b6SRobert Watson 658f3f49bbbSRobert Watson unp2->unp_cc = sbcc; 659fc3fcacfSRobert Watson m = NULL; 660df8bae1dSRodney W. Grimes break; 661df8bae1dSRodney W. Grimes 662df8bae1dSRodney W. Grimes default: 663a29f300eSGarrett Wollman panic("uipc_send unknown socktype"); 664df8bae1dSRodney W. Grimes } 665a29f300eSGarrett Wollman 6666b8fda4dSGarrett Wollman /* 6676b8fda4dSGarrett Wollman * SEND_EOF is equivalent to a SEND followed by 6686b8fda4dSGarrett Wollman * a SHUTDOWN. 6696b8fda4dSGarrett Wollman */ 670a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 6716b8fda4dSGarrett Wollman socantsendmore(so); 6726b8fda4dSGarrett Wollman unp_shutdown(unp); 6736b8fda4dSGarrett Wollman } 6740d9ce3a1SRobert Watson UNP_UNLOCK(); 675df8bae1dSRodney W. Grimes 676fc3fcacfSRobert Watson if (control != NULL && error != 0) 677bd508d39SDon Lewis unp_dispose(control); 678bd508d39SDon Lewis 679a29f300eSGarrett Wollman release: 680fc3fcacfSRobert Watson if (control != NULL) 681a29f300eSGarrett Wollman m_freem(control); 682fc3fcacfSRobert Watson if (m != NULL) 683a29f300eSGarrett Wollman m_freem(m); 684e5aeaa0cSDag-Erling Smørgrav return (error); 685a29f300eSGarrett Wollman } 686df8bae1dSRodney W. Grimes 687a29f300eSGarrett Wollman static int 688a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 689a29f300eSGarrett Wollman { 69040f2ac28SRobert Watson struct unpcb *unp; 691a29f300eSGarrett Wollman struct socket *so2; 692a29f300eSGarrett Wollman 69340f2ac28SRobert Watson unp = sotounpcb(so); 6944d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sense: unp == NULL")); 6954d4b555eSRobert Watson UNP_LOCK(); 696a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 697fc3fcacfSRobert Watson if (so->so_type == SOCK_STREAM && unp->unp_conn != NULL) { 698df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 699a29f300eSGarrett Wollman sb->st_blksize += so2->so_rcv.sb_cc; 700df8bae1dSRodney W. Grimes } 701f3732fd1SPoul-Henning Kamp sb->st_dev = NODEV; 702df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 7036f782c46SJeffrey Hsu unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 704a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 7050d9ce3a1SRobert Watson UNP_UNLOCK(); 706df8bae1dSRodney W. Grimes return (0); 707a29f300eSGarrett Wollman } 708df8bae1dSRodney W. Grimes 709a29f300eSGarrett Wollman static int 710a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 711a29f300eSGarrett Wollman { 71240f2ac28SRobert Watson struct unpcb *unp; 713df8bae1dSRodney W. Grimes 71440f2ac28SRobert Watson unp = sotounpcb(so); 7154d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_shutdown: unp == NULL")); 7164d4b555eSRobert Watson UNP_LOCK(); 717a29f300eSGarrett Wollman socantsendmore(so); 718a29f300eSGarrett Wollman unp_shutdown(unp); 7190d9ce3a1SRobert Watson UNP_UNLOCK(); 720e5aeaa0cSDag-Erling Smørgrav return (0); 721a29f300eSGarrett Wollman } 722df8bae1dSRodney W. Grimes 723a29f300eSGarrett Wollman static int 72457bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 725a29f300eSGarrett Wollman { 72640f2ac28SRobert Watson struct unpcb *unp; 7270d9ce3a1SRobert Watson const struct sockaddr *sa; 728a29f300eSGarrett Wollman 7294d4b555eSRobert Watson unp = sotounpcb(so); 7304d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL")); 7310d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 7320d9ce3a1SRobert Watson UNP_LOCK(); 733fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 7340d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_addr; 73583f3198bSThomas Moestl else 7360d9ce3a1SRobert Watson sa = &sun_noname; 7370d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 7380d9ce3a1SRobert Watson UNP_UNLOCK(); 739e5aeaa0cSDag-Erling Smørgrav return (0); 740df8bae1dSRodney W. Grimes } 741a29f300eSGarrett Wollman 742a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = { 743756d52a1SPoul-Henning Kamp .pru_abort = uipc_abort, 744756d52a1SPoul-Henning Kamp .pru_accept = uipc_accept, 745756d52a1SPoul-Henning Kamp .pru_attach = uipc_attach, 746756d52a1SPoul-Henning Kamp .pru_bind = uipc_bind, 747756d52a1SPoul-Henning Kamp .pru_connect = uipc_connect, 748756d52a1SPoul-Henning Kamp .pru_connect2 = uipc_connect2, 749756d52a1SPoul-Henning Kamp .pru_detach = uipc_detach, 750756d52a1SPoul-Henning Kamp .pru_disconnect = uipc_disconnect, 751756d52a1SPoul-Henning Kamp .pru_listen = uipc_listen, 752756d52a1SPoul-Henning Kamp .pru_peeraddr = uipc_peeraddr, 753756d52a1SPoul-Henning Kamp .pru_rcvd = uipc_rcvd, 754756d52a1SPoul-Henning Kamp .pru_send = uipc_send, 755756d52a1SPoul-Henning Kamp .pru_sense = uipc_sense, 756756d52a1SPoul-Henning Kamp .pru_shutdown = uipc_shutdown, 757756d52a1SPoul-Henning Kamp .pru_sockaddr = uipc_sockaddr, 758756d52a1SPoul-Henning Kamp .pru_sosend = sosend, 759756d52a1SPoul-Henning Kamp .pru_soreceive = soreceive, 760756d52a1SPoul-Henning Kamp .pru_sopoll = sopoll, 761a152f8a3SRobert Watson .pru_close = uipc_close, 762a29f300eSGarrett Wollman }; 763df8bae1dSRodney W. Grimes 7640c1bb4fbSDima Dorfman int 765892af6b9SRobert Watson uipc_ctloutput(struct socket *so, struct sockopt *sopt) 7660c1bb4fbSDima Dorfman { 76740f2ac28SRobert Watson struct unpcb *unp; 7680d9ce3a1SRobert Watson struct xucred xu; 7696a2989fdSMatthew N. Dodd int error, optval; 7706a2989fdSMatthew N. Dodd 77196a041b5SMatthew N. Dodd if (sopt->sopt_level != 0) 77296a041b5SMatthew N. Dodd return (EINVAL); 77396a041b5SMatthew N. Dodd 7746a2989fdSMatthew N. Dodd unp = sotounpcb(so); 7754d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL")); 7764d4b555eSRobert Watson UNP_LOCK(); 7776a2989fdSMatthew N. Dodd error = 0; 7780c1bb4fbSDima Dorfman switch (sopt->sopt_dir) { 7790c1bb4fbSDima Dorfman case SOPT_GET: 7800c1bb4fbSDima Dorfman switch (sopt->sopt_name) { 7810c1bb4fbSDima Dorfman case LOCAL_PEERCRED: 7820c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC) 7830d9ce3a1SRobert Watson xu = unp->unp_peercred; 7840c1bb4fbSDima Dorfman else { 7850c1bb4fbSDima Dorfman if (so->so_type == SOCK_STREAM) 7860c1bb4fbSDima Dorfman error = ENOTCONN; 7870c1bb4fbSDima Dorfman else 7880c1bb4fbSDima Dorfman error = EINVAL; 7890c1bb4fbSDima Dorfman } 7900d9ce3a1SRobert Watson if (error == 0) 7910d9ce3a1SRobert Watson error = sooptcopyout(sopt, &xu, sizeof(xu)); 7920c1bb4fbSDima Dorfman break; 7936a2989fdSMatthew N. Dodd case LOCAL_CREDS: 7946a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0; 7956a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 7966a2989fdSMatthew N. Dodd break; 7976a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 7986a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0; 7996a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 8006a2989fdSMatthew N. Dodd break; 8010c1bb4fbSDima Dorfman default: 8020c1bb4fbSDima Dorfman error = EOPNOTSUPP; 8030c1bb4fbSDima Dorfman break; 8040c1bb4fbSDima Dorfman } 8050c1bb4fbSDima Dorfman break; 8060c1bb4fbSDima Dorfman case SOPT_SET: 8076a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 8086a2989fdSMatthew N. Dodd case LOCAL_CREDS: 8096a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 8106a2989fdSMatthew N. Dodd error = sooptcopyin(sopt, &optval, sizeof(optval), 8116a2989fdSMatthew N. Dodd sizeof(optval)); 8126a2989fdSMatthew N. Dodd if (error) 8136a2989fdSMatthew N. Dodd break; 8146a2989fdSMatthew N. Dodd 8156a2989fdSMatthew N. Dodd #define OPTSET(bit) \ 8166a2989fdSMatthew N. Dodd if (optval) \ 8176a2989fdSMatthew N. Dodd unp->unp_flags |= bit; \ 8186a2989fdSMatthew N. Dodd else \ 8196a2989fdSMatthew N. Dodd unp->unp_flags &= ~bit; 8206a2989fdSMatthew N. Dodd 8216a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 8226a2989fdSMatthew N. Dodd case LOCAL_CREDS: 8236a2989fdSMatthew N. Dodd OPTSET(UNP_WANTCRED); 8246a2989fdSMatthew N. Dodd break; 8256a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 8266a2989fdSMatthew N. Dodd OPTSET(UNP_CONNWAIT); 8276a2989fdSMatthew N. Dodd break; 8286a2989fdSMatthew N. Dodd default: 8296a2989fdSMatthew N. Dodd break; 8306a2989fdSMatthew N. Dodd } 8316a2989fdSMatthew N. Dodd break; 8326a2989fdSMatthew N. Dodd #undef OPTSET 8336a2989fdSMatthew N. Dodd default: 8346a2989fdSMatthew N. Dodd error = ENOPROTOOPT; 8356a2989fdSMatthew N. Dodd break; 8366a2989fdSMatthew N. Dodd } 837abb886faSMatthew N. Dodd break; 8380c1bb4fbSDima Dorfman default: 8390c1bb4fbSDima Dorfman error = EOPNOTSUPP; 8400c1bb4fbSDima Dorfman break; 8410c1bb4fbSDima Dorfman } 8426a2989fdSMatthew N. Dodd UNP_UNLOCK(); 8430c1bb4fbSDima Dorfman return (error); 8440c1bb4fbSDima Dorfman } 8450c1bb4fbSDima Dorfman 846f708ef1bSPoul-Henning Kamp static int 847892af6b9SRobert Watson unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 848df8bae1dSRodney W. Grimes { 849892af6b9SRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam; 850892af6b9SRobert Watson struct vnode *vp; 851892af6b9SRobert Watson struct socket *so2, *so3; 852b295bdcdSRobert Watson struct unpcb *unp, *unp2, *unp3; 85357bf258eSGarrett Wollman int error, len; 854df8bae1dSRodney W. Grimes struct nameidata nd; 85557bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 8560d9ce3a1SRobert Watson struct sockaddr *sa; 8570d9ce3a1SRobert Watson 8580d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 859df8bae1dSRodney W. Grimes 8604d4b555eSRobert Watson unp = sotounpcb(so); 8614d4b555eSRobert Watson KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 86257bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 86357bf258eSGarrett Wollman if (len <= 0) 864e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 86555c85568SRobert Drehmel strlcpy(buf, soun->sun_path, len + 1); 8660d9ce3a1SRobert Watson UNP_UNLOCK(); 8670d9ce3a1SRobert Watson sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 8680d9ce3a1SRobert Watson mtx_lock(&Giant); 869b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td); 870797f2d22SPoul-Henning Kamp error = namei(&nd); 871797f2d22SPoul-Henning Kamp if (error) 8720d9ce3a1SRobert Watson vp = NULL; 8730d9ce3a1SRobert Watson else 874df8bae1dSRodney W. Grimes vp = nd.ni_vp; 8750d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_connect"); 876762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 8770d9ce3a1SRobert Watson if (error) 8780d9ce3a1SRobert Watson goto bad; 8790d9ce3a1SRobert Watson 880df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 881df8bae1dSRodney W. Grimes error = ENOTSOCK; 882df8bae1dSRodney W. Grimes goto bad; 883df8bae1dSRodney W. Grimes } 884a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 885797f2d22SPoul-Henning Kamp if (error) 886df8bae1dSRodney W. Grimes goto bad; 8872260c03dSRobert Watson mtx_unlock(&Giant); 8882260c03dSRobert Watson UNP_LOCK(); 889b295bdcdSRobert Watson unp = sotounpcb(so); 8904d4b555eSRobert Watson KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 891df8bae1dSRodney W. Grimes so2 = vp->v_socket; 892fc3fcacfSRobert Watson if (so2 == NULL) { 893df8bae1dSRodney W. Grimes error = ECONNREFUSED; 8942260c03dSRobert Watson goto bad2; 895df8bae1dSRodney W. Grimes } 896df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 897df8bae1dSRodney W. Grimes error = EPROTOTYPE; 8982260c03dSRobert Watson goto bad2; 899df8bae1dSRodney W. Grimes } 900df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 9010d9ce3a1SRobert Watson if (so2->so_options & SO_ACCEPTCONN) { 9020d9ce3a1SRobert Watson /* 9031c381b19SRobert Watson * NB: drop locks here so unp_attach is entered w/o 9041c381b19SRobert Watson * locks; this avoids a recursive lock of the head 9051c381b19SRobert Watson * and holding sleep locks across a (potentially) 9061c381b19SRobert Watson * blocking malloc. 9070d9ce3a1SRobert Watson */ 9080d9ce3a1SRobert Watson UNP_UNLOCK(); 9090d9ce3a1SRobert Watson so3 = sonewconn(so2, 0); 9100d9ce3a1SRobert Watson UNP_LOCK(); 9110d9ce3a1SRobert Watson } else 9120d9ce3a1SRobert Watson so3 = NULL; 9130d9ce3a1SRobert Watson if (so3 == NULL) { 914df8bae1dSRodney W. Grimes error = ECONNREFUSED; 9150d9ce3a1SRobert Watson goto bad2; 916df8bae1dSRodney W. Grimes } 9170c1bb4fbSDima Dorfman unp = sotounpcb(so); 918df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 919df8bae1dSRodney W. Grimes unp3 = sotounpcb(so3); 9200d9ce3a1SRobert Watson if (unp2->unp_addr != NULL) { 9210d9ce3a1SRobert Watson bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 9220d9ce3a1SRobert Watson unp3->unp_addr = (struct sockaddr_un *) sa; 9230d9ce3a1SRobert Watson sa = NULL; 9240d9ce3a1SRobert Watson } 9250c1bb4fbSDima Dorfman /* 9260c1bb4fbSDima Dorfman * unp_peercred management: 9270c1bb4fbSDima Dorfman * 9281c381b19SRobert Watson * The connecter's (client's) credentials are copied from its 9291c381b19SRobert Watson * process structure at the time of connect() (which is now). 9300c1bb4fbSDima Dorfman */ 931a854ed98SJohn Baldwin cru2x(td->td_ucred, &unp3->unp_peercred); 9320c1bb4fbSDima Dorfman unp3->unp_flags |= UNP_HAVEPC; 9330c1bb4fbSDima Dorfman /* 9341c381b19SRobert Watson * The receiver's (server's) credentials are copied from the 9351c381b19SRobert Watson * unp_peercred member of socket on which the former called 9361c381b19SRobert Watson * listen(); unp_listen() cached that process's credentials 9371c381b19SRobert Watson * at that time so we can use them now. 9380c1bb4fbSDima Dorfman */ 9390c1bb4fbSDima Dorfman KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 9400c1bb4fbSDima Dorfman ("unp_connect: listener without cached peercred")); 9410c1bb4fbSDima Dorfman memcpy(&unp->unp_peercred, &unp2->unp_peercred, 9420c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 9430c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPC; 944481f8fe8SMaxim Konovalov if (unp2->unp_flags & UNP_WANTCRED) 945481f8fe8SMaxim Konovalov unp3->unp_flags |= UNP_WANTCRED; 946335654d7SRobert Watson #ifdef MAC 947310e7cebSRobert Watson SOCK_LOCK(so); 948335654d7SRobert Watson mac_set_socket_peer_from_socket(so, so3); 949335654d7SRobert Watson mac_set_socket_peer_from_socket(so3, so); 950310e7cebSRobert Watson SOCK_UNLOCK(so); 951335654d7SRobert Watson #endif 9520c1bb4fbSDima Dorfman 953df8bae1dSRodney W. Grimes so2 = so3; 954df8bae1dSRodney W. Grimes } 9556a2989fdSMatthew N. Dodd error = unp_connect2(so, so2, PRU_CONNECT); 9560d9ce3a1SRobert Watson bad2: 9570d9ce3a1SRobert Watson UNP_UNLOCK(); 9580d9ce3a1SRobert Watson mtx_lock(&Giant); 959df8bae1dSRodney W. Grimes bad: 9600d9ce3a1SRobert Watson mtx_assert(&Giant, MA_OWNED); 9610d9ce3a1SRobert Watson if (vp != NULL) 962df8bae1dSRodney W. Grimes vput(vp); 9630d9ce3a1SRobert Watson mtx_unlock(&Giant); 9640d9ce3a1SRobert Watson free(sa, M_SONAME); 9650d9ce3a1SRobert Watson UNP_LOCK(); 966df8bae1dSRodney W. Grimes return (error); 967df8bae1dSRodney W. Grimes } 968df8bae1dSRodney W. Grimes 969db48c0d2SRobert Watson static int 9706a2989fdSMatthew N. Dodd unp_connect2(struct socket *so, struct socket *so2, int req) 971df8bae1dSRodney W. Grimes { 972892af6b9SRobert Watson struct unpcb *unp = sotounpcb(so); 973892af6b9SRobert Watson struct unpcb *unp2; 974df8bae1dSRodney W. Grimes 9750d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 9760d9ce3a1SRobert Watson 977df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 978df8bae1dSRodney W. Grimes return (EPROTOTYPE); 979df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 9804d4b555eSRobert Watson KASSERT(unp2 != NULL, ("unp_connect2: unp2 == NULL")); 981df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 982df8bae1dSRodney W. Grimes switch (so->so_type) { 983df8bae1dSRodney W. Grimes 984df8bae1dSRodney W. Grimes case SOCK_DGRAM: 98598271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 986df8bae1dSRodney W. Grimes soisconnected(so); 987df8bae1dSRodney W. Grimes break; 988df8bae1dSRodney W. Grimes 989df8bae1dSRodney W. Grimes case SOCK_STREAM: 990df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 9916a2989fdSMatthew N. Dodd if (req == PRU_CONNECT && 9926a2989fdSMatthew N. Dodd ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT)) 9936a2989fdSMatthew N. Dodd soisconnecting(so); 9946a2989fdSMatthew N. Dodd else 995df8bae1dSRodney W. Grimes soisconnected(so); 996df8bae1dSRodney W. Grimes soisconnected(so2); 997df8bae1dSRodney W. Grimes break; 998df8bae1dSRodney W. Grimes 999df8bae1dSRodney W. Grimes default: 1000df8bae1dSRodney W. Grimes panic("unp_connect2"); 1001df8bae1dSRodney W. Grimes } 1002df8bae1dSRodney W. Grimes return (0); 1003df8bae1dSRodney W. Grimes } 1004df8bae1dSRodney W. Grimes 1005f708ef1bSPoul-Henning Kamp static void 1006892af6b9SRobert Watson unp_disconnect(struct unpcb *unp) 1007df8bae1dSRodney W. Grimes { 1008892af6b9SRobert Watson struct unpcb *unp2 = unp->unp_conn; 10091b2e3b4bSRobert Watson struct socket *so; 1010df8bae1dSRodney W. Grimes 10110d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 10120d9ce3a1SRobert Watson 1013fc3fcacfSRobert Watson if (unp2 == NULL) 1014df8bae1dSRodney W. Grimes return; 1015fc3fcacfSRobert Watson unp->unp_conn = NULL; 1016df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 1017df8bae1dSRodney W. Grimes case SOCK_DGRAM: 101898271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 10191b2e3b4bSRobert Watson so = unp->unp_socket; 10201b2e3b4bSRobert Watson SOCK_LOCK(so); 10211b2e3b4bSRobert Watson so->so_state &= ~SS_ISCONNECTED; 10221b2e3b4bSRobert Watson SOCK_UNLOCK(so); 1023df8bae1dSRodney W. Grimes break; 1024df8bae1dSRodney W. Grimes 1025df8bae1dSRodney W. Grimes case SOCK_STREAM: 1026df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 1027fc3fcacfSRobert Watson unp2->unp_conn = NULL; 1028df8bae1dSRodney W. Grimes soisdisconnected(unp2->unp_socket); 1029df8bae1dSRodney W. Grimes break; 1030df8bae1dSRodney W. Grimes } 1031df8bae1dSRodney W. Grimes } 1032df8bae1dSRodney W. Grimes 10330d9ce3a1SRobert Watson /* 10341c381b19SRobert Watson * unp_pcblist() assumes that UNIX domain socket memory is never reclaimed by 10351c381b19SRobert Watson * the zone (UMA_ZONE_NOFREE), and as such potentially stale pointers are 10361c381b19SRobert Watson * safe to reference. It first scans the list of struct unpcb's to generate 10371c381b19SRobert Watson * a pointer list, then it rescans its list one entry at a time to 10380d9ce3a1SRobert Watson * externalize and copyout. It checks the generation number to see if a 10390d9ce3a1SRobert Watson * struct unpcb has been reused, and will skip it if so. 10400d9ce3a1SRobert Watson */ 104198271db4SGarrett Wollman static int 104282d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 104398271db4SGarrett Wollman { 1044f5ef029eSPoul-Henning Kamp int error, i, n; 104598271db4SGarrett Wollman struct unpcb *unp, **unp_list; 104698271db4SGarrett Wollman unp_gen_t gencnt; 10478f364875SJulian Elischer struct xunpgen *xug; 104898271db4SGarrett Wollman struct unp_head *head; 10498f364875SJulian Elischer struct xunpcb *xu; 105098271db4SGarrett Wollman 1051a23d65bfSBruce Evans head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 105298271db4SGarrett Wollman 105398271db4SGarrett Wollman /* 105498271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 105598271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 105698271db4SGarrett Wollman */ 1057fc3fcacfSRobert Watson if (req->oldptr == NULL) { 105898271db4SGarrett Wollman n = unp_count; 10598f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug) 106098271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 1061e5aeaa0cSDag-Erling Smørgrav return (0); 106298271db4SGarrett Wollman } 106398271db4SGarrett Wollman 1064fc3fcacfSRobert Watson if (req->newptr != NULL) 1065e5aeaa0cSDag-Erling Smørgrav return (EPERM); 106698271db4SGarrett Wollman 106798271db4SGarrett Wollman /* 106898271db4SGarrett Wollman * OK, now we're committed to doing something. 106998271db4SGarrett Wollman */ 1070a163d034SWarner Losh xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 10710d9ce3a1SRobert Watson UNP_LOCK(); 107298271db4SGarrett Wollman gencnt = unp_gencnt; 107398271db4SGarrett Wollman n = unp_count; 10740d9ce3a1SRobert Watson UNP_UNLOCK(); 107598271db4SGarrett Wollman 10768f364875SJulian Elischer xug->xug_len = sizeof *xug; 10778f364875SJulian Elischer xug->xug_count = n; 10788f364875SJulian Elischer xug->xug_gen = gencnt; 10798f364875SJulian Elischer xug->xug_sogen = so_gencnt; 10808f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 10818f364875SJulian Elischer if (error) { 10828f364875SJulian Elischer free(xug, M_TEMP); 1083e5aeaa0cSDag-Erling Smørgrav return (error); 10848f364875SJulian Elischer } 108598271db4SGarrett Wollman 1086a163d034SWarner Losh unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 108798271db4SGarrett Wollman 10880d9ce3a1SRobert Watson UNP_LOCK(); 10892e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 10902e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 10918a7d8cc6SRobert Watson if (unp->unp_gencnt <= gencnt) { 1092a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred, 10938a7d8cc6SRobert Watson unp->unp_socket->so_cred)) 10944787fd37SPaul Saab continue; 109598271db4SGarrett Wollman unp_list[i++] = unp; 109698271db4SGarrett Wollman } 10974787fd37SPaul Saab } 10980d9ce3a1SRobert Watson UNP_UNLOCK(); 10991c381b19SRobert Watson n = i; /* In case we lost some during malloc. */ 110098271db4SGarrett Wollman 110198271db4SGarrett Wollman error = 0; 1102fe2eee82SColin Percival xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO); 110398271db4SGarrett Wollman for (i = 0; i < n; i++) { 110498271db4SGarrett Wollman unp = unp_list[i]; 110598271db4SGarrett Wollman if (unp->unp_gencnt <= gencnt) { 11068f364875SJulian Elischer xu->xu_len = sizeof *xu; 11078f364875SJulian Elischer xu->xu_unpp = unp; 110898271db4SGarrett Wollman /* 110998271db4SGarrett Wollman * XXX - need more locking here to protect against 111098271db4SGarrett Wollman * connect/disconnect races for SMP. 111198271db4SGarrett Wollman */ 1112fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 11138f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr, 111498271db4SGarrett Wollman unp->unp_addr->sun_len); 1115fc3fcacfSRobert Watson if (unp->unp_conn != NULL && 1116fc3fcacfSRobert Watson unp->unp_conn->unp_addr != NULL) 111798271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 11188f364875SJulian Elischer &xu->xu_caddr, 111998271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 11208f364875SJulian Elischer bcopy(unp, &xu->xu_unp, sizeof *unp); 11218f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket); 11228f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu); 112398271db4SGarrett Wollman } 112498271db4SGarrett Wollman } 11258f364875SJulian Elischer free(xu, M_TEMP); 112698271db4SGarrett Wollman if (!error) { 112798271db4SGarrett Wollman /* 11281c381b19SRobert Watson * Give the user an updated idea of our state. If the 11291c381b19SRobert Watson * generation differs from what we told her before, she knows 11301c381b19SRobert Watson * that something happened while we were processing this 11311c381b19SRobert Watson * request, and it might be necessary to retry. 113298271db4SGarrett Wollman */ 11338f364875SJulian Elischer xug->xug_gen = unp_gencnt; 11348f364875SJulian Elischer xug->xug_sogen = so_gencnt; 11358f364875SJulian Elischer xug->xug_count = unp_count; 11368f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 113798271db4SGarrett Wollman } 113898271db4SGarrett Wollman free(unp_list, M_TEMP); 11398f364875SJulian Elischer free(xug, M_TEMP); 1140e5aeaa0cSDag-Erling Smørgrav return (error); 114198271db4SGarrett Wollman } 114298271db4SGarrett Wollman 114398271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 114498271db4SGarrett Wollman (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 114598271db4SGarrett Wollman "List of active local datagram sockets"); 114698271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 114798271db4SGarrett Wollman (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 114898271db4SGarrett Wollman "List of active local stream sockets"); 114998271db4SGarrett Wollman 1150f708ef1bSPoul-Henning Kamp static void 1151892af6b9SRobert Watson unp_shutdown(struct unpcb *unp) 1152df8bae1dSRodney W. Grimes { 1153df8bae1dSRodney W. Grimes struct socket *so; 1154df8bae1dSRodney W. Grimes 11550d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 11560d9ce3a1SRobert Watson 1157df8bae1dSRodney W. Grimes if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 1158df8bae1dSRodney W. Grimes (so = unp->unp_conn->unp_socket)) 1159df8bae1dSRodney W. Grimes socantrcvmore(so); 1160df8bae1dSRodney W. Grimes } 1161df8bae1dSRodney W. Grimes 1162f708ef1bSPoul-Henning Kamp static void 1163892af6b9SRobert Watson unp_drop(struct unpcb *unp, int errno) 1164df8bae1dSRodney W. Grimes { 1165df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 1166df8bae1dSRodney W. Grimes 11670d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 11680d9ce3a1SRobert Watson 1169df8bae1dSRodney W. Grimes so->so_error = errno; 1170df8bae1dSRodney W. Grimes unp_disconnect(unp); 1171df8bae1dSRodney W. Grimes } 1172df8bae1dSRodney W. Grimes 11732bc21ed9SDavid Malone static void 1174892af6b9SRobert Watson unp_freerights(struct file **rp, int fdcount) 1175df8bae1dSRodney W. Grimes { 11762bc21ed9SDavid Malone int i; 11772bc21ed9SDavid Malone struct file *fp; 1178df8bae1dSRodney W. Grimes 11792bc21ed9SDavid Malone for (i = 0; i < fdcount; i++) { 1180df8bae1dSRodney W. Grimes fp = *rp; 11818692c025SYoshinobu Inoue /* 11821c381b19SRobert Watson * Zero the pointer before calling unp_discard since it may 11831c381b19SRobert Watson * end up in unp_gc().. 1184d7dca903SRobert Watson * 1185d7dca903SRobert Watson * XXXRW: This is less true than it used to be. 11868692c025SYoshinobu Inoue */ 1187df8bae1dSRodney W. Grimes *rp++ = 0; 11888692c025SYoshinobu Inoue unp_discard(fp); 1189df8bae1dSRodney W. Grimes } 11902bc21ed9SDavid Malone } 11912bc21ed9SDavid Malone 11922bc21ed9SDavid Malone int 1193892af6b9SRobert Watson unp_externalize(struct mbuf *control, struct mbuf **controlp) 11942bc21ed9SDavid Malone { 11952bc21ed9SDavid Malone struct thread *td = curthread; /* XXX */ 11962bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 11972bc21ed9SDavid Malone int i; 11982bc21ed9SDavid Malone int *fdp; 11992bc21ed9SDavid Malone struct file **rp; 12002bc21ed9SDavid Malone struct file *fp; 12012bc21ed9SDavid Malone void *data; 12022bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 12032bc21ed9SDavid Malone int error, newfds; 12042bc21ed9SDavid Malone int f; 12052bc21ed9SDavid Malone u_int newlen; 12062bc21ed9SDavid Malone 12074c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 12084c5bc1caSRobert Watson 12092bc21ed9SDavid Malone error = 0; 12102bc21ed9SDavid Malone if (controlp != NULL) /* controlp == NULL => free control messages */ 12112bc21ed9SDavid Malone *controlp = NULL; 12122bc21ed9SDavid Malone 12132bc21ed9SDavid Malone while (cm != NULL) { 12142bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 12152bc21ed9SDavid Malone error = EINVAL; 12162bc21ed9SDavid Malone break; 12172bc21ed9SDavid Malone } 12182bc21ed9SDavid Malone 12192bc21ed9SDavid Malone data = CMSG_DATA(cm); 12202bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 12212bc21ed9SDavid Malone 12222bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET 12232bc21ed9SDavid Malone && cm->cmsg_type == SCM_RIGHTS) { 12242bc21ed9SDavid Malone newfds = datalen / sizeof(struct file *); 12252bc21ed9SDavid Malone rp = data; 12262bc21ed9SDavid Malone 1227e2f9a08bSOlivier Houchard /* If we're not outputting the descriptors free them. */ 12282bc21ed9SDavid Malone if (error || controlp == NULL) { 12292bc21ed9SDavid Malone unp_freerights(rp, newfds); 12302bc21ed9SDavid Malone goto next; 12312bc21ed9SDavid Malone } 1232426da3bcSAlfred Perlstein FILEDESC_LOCK(td->td_proc->p_fd); 12332bc21ed9SDavid Malone /* if the new FD's will not fit free them. */ 12342bc21ed9SDavid Malone if (!fdavail(td, newfds)) { 1235426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 12362bc21ed9SDavid Malone error = EMSGSIZE; 12372bc21ed9SDavid Malone unp_freerights(rp, newfds); 12382bc21ed9SDavid Malone goto next; 1239df8bae1dSRodney W. Grimes } 1240ed5b7817SJulian Elischer /* 12411c381b19SRobert Watson * Now change each pointer to an fd in the global 12421c381b19SRobert Watson * table to an integer that is the index to the local 12431c381b19SRobert Watson * fd table entry that we set up to point to the 12441c381b19SRobert Watson * global one we are transferring. 1245ed5b7817SJulian Elischer */ 12462bc21ed9SDavid Malone newlen = newfds * sizeof(int); 12472bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 12482bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 12492bc21ed9SDavid Malone if (*controlp == NULL) { 1250426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 12512bc21ed9SDavid Malone error = E2BIG; 12522bc21ed9SDavid Malone unp_freerights(rp, newfds); 12532bc21ed9SDavid Malone goto next; 12542bc21ed9SDavid Malone } 12552bc21ed9SDavid Malone 12562bc21ed9SDavid Malone fdp = (int *) 12572bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1258df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 1259a6d4491cSDag-Erling Smørgrav if (fdalloc(td, 0, &f)) 12602bc21ed9SDavid Malone panic("unp_externalize fdalloc failed"); 12618692c025SYoshinobu Inoue fp = *rp++; 1262b40ce416SJulian Elischer td->td_proc->p_fd->fd_ofiles[f] = fp; 1263426da3bcSAlfred Perlstein FILE_LOCK(fp); 1264df8bae1dSRodney W. Grimes fp->f_msgcount--; 1265426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1266df8bae1dSRodney W. Grimes unp_rights--; 12678692c025SYoshinobu Inoue *fdp++ = f; 1268df8bae1dSRodney W. Grimes } 1269426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 12701c381b19SRobert Watson } else { 12711c381b19SRobert Watson /* We can just copy anything else across. */ 12722bc21ed9SDavid Malone if (error || controlp == NULL) 12732bc21ed9SDavid Malone goto next; 12742bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, datalen, 12752bc21ed9SDavid Malone cm->cmsg_type, cm->cmsg_level); 12762bc21ed9SDavid Malone if (*controlp == NULL) { 12772bc21ed9SDavid Malone error = ENOBUFS; 12782bc21ed9SDavid Malone goto next; 12792bc21ed9SDavid Malone } 12802bc21ed9SDavid Malone bcopy(data, 12812bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 12822bc21ed9SDavid Malone datalen); 12832bc21ed9SDavid Malone } 12842bc21ed9SDavid Malone 12852bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 12862bc21ed9SDavid Malone 12872bc21ed9SDavid Malone next: 12882bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 12892bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 12902bc21ed9SDavid Malone cm = (struct cmsghdr *) 12912bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 12928692c025SYoshinobu Inoue } else { 12932bc21ed9SDavid Malone clen = 0; 12942bc21ed9SDavid Malone cm = NULL; 12958692c025SYoshinobu Inoue } 12968692c025SYoshinobu Inoue } 12978692c025SYoshinobu Inoue 12982bc21ed9SDavid Malone m_freem(control); 12992bc21ed9SDavid Malone 13002bc21ed9SDavid Malone return (error); 1301df8bae1dSRodney W. Grimes } 1302df8bae1dSRodney W. Grimes 13034f590175SPaul Saab static void 13044f590175SPaul Saab unp_zone_change(void *tag) 13054f590175SPaul Saab { 13064f590175SPaul Saab 13074f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets); 13084f590175SPaul Saab } 13094f590175SPaul Saab 131098271db4SGarrett Wollman void 131198271db4SGarrett Wollman unp_init(void) 131298271db4SGarrett Wollman { 13131c381b19SRobert Watson 13149e9d298aSJeff Roberson unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 13159e9d298aSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 1316fc3fcacfSRobert Watson if (unp_zone == NULL) 131798271db4SGarrett Wollman panic("unp_init"); 13184f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets); 13194f590175SPaul Saab EVENTHANDLER_REGISTER(maxsockets_change, unp_zone_change, 13204f590175SPaul Saab NULL, EVENTHANDLER_PRI_ANY); 132198271db4SGarrett Wollman LIST_INIT(&unp_dhead); 132298271db4SGarrett Wollman LIST_INIT(&unp_shead); 1323a0ec558aSRobert Watson TASK_INIT(&unp_gc_task, 0, unp_gc, NULL); 13240d9ce3a1SRobert Watson UNP_LOCK_INIT(); 132598271db4SGarrett Wollman } 132698271db4SGarrett Wollman 1327f708ef1bSPoul-Henning Kamp static int 1328892af6b9SRobert Watson unp_internalize(struct mbuf **controlp, struct thread *td) 1329df8bae1dSRodney W. Grimes { 13302bc21ed9SDavid Malone struct mbuf *control = *controlp; 1331b40ce416SJulian Elischer struct proc *p = td->td_proc; 13328692c025SYoshinobu Inoue struct filedesc *fdescp = p->p_fd; 13332bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 13342bc21ed9SDavid Malone struct cmsgcred *cmcred; 13352bc21ed9SDavid Malone struct file **rp; 13362bc21ed9SDavid Malone struct file *fp; 13372bc21ed9SDavid Malone struct timeval *tv; 13382bc21ed9SDavid Malone int i, fd, *fdp; 13392bc21ed9SDavid Malone void *data; 13402bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 13412bc21ed9SDavid Malone int error, oldfds; 13428692c025SYoshinobu Inoue u_int newlen; 1343df8bae1dSRodney W. Grimes 13444c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 13454c5bc1caSRobert Watson 13462bc21ed9SDavid Malone error = 0; 13472bc21ed9SDavid Malone *controlp = NULL; 13480b788fa1SBill Paul 13492bc21ed9SDavid Malone while (cm != NULL) { 13502bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 13512bc21ed9SDavid Malone || cm->cmsg_len > clen) { 13522bc21ed9SDavid Malone error = EINVAL; 13532bc21ed9SDavid Malone goto out; 13542bc21ed9SDavid Malone } 13552bc21ed9SDavid Malone 13562bc21ed9SDavid Malone data = CMSG_DATA(cm); 13572bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 13582bc21ed9SDavid Malone 13592bc21ed9SDavid Malone switch (cm->cmsg_type) { 13600b788fa1SBill Paul /* 13610b788fa1SBill Paul * Fill in credential information. 13620b788fa1SBill Paul */ 13632bc21ed9SDavid Malone case SCM_CREDS: 13642bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 13652bc21ed9SDavid Malone SCM_CREDS, SOL_SOCKET); 13662bc21ed9SDavid Malone if (*controlp == NULL) { 13672bc21ed9SDavid Malone error = ENOBUFS; 13682bc21ed9SDavid Malone goto out; 13692bc21ed9SDavid Malone } 13702bc21ed9SDavid Malone 13712bc21ed9SDavid Malone cmcred = (struct cmsgcred *) 13722bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 13730b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 1374a854ed98SJohn Baldwin cmcred->cmcred_uid = td->td_ucred->cr_ruid; 1375a854ed98SJohn Baldwin cmcred->cmcred_gid = td->td_ucred->cr_rgid; 1376a854ed98SJohn Baldwin cmcred->cmcred_euid = td->td_ucred->cr_uid; 1377a854ed98SJohn Baldwin cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 13780b788fa1SBill Paul CMGROUP_MAX); 13790b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 13802bc21ed9SDavid Malone cmcred->cmcred_groups[i] = 1381a854ed98SJohn Baldwin td->td_ucred->cr_groups[i]; 13822bc21ed9SDavid Malone break; 13830b788fa1SBill Paul 13842bc21ed9SDavid Malone case SCM_RIGHTS: 13852bc21ed9SDavid Malone oldfds = datalen / sizeof (int); 1386ed5b7817SJulian Elischer /* 13871c381b19SRobert Watson * Check that all the FDs passed in refer to legal 13881c381b19SRobert Watson * files. If not, reject the entire operation. 1389ed5b7817SJulian Elischer */ 13902bc21ed9SDavid Malone fdp = data; 1391426da3bcSAlfred Perlstein FILEDESC_LOCK(fdescp); 1392df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 13938692c025SYoshinobu Inoue fd = *fdp++; 13948692c025SYoshinobu Inoue if ((unsigned)fd >= fdescp->fd_nfiles || 13952bc21ed9SDavid Malone fdescp->fd_ofiles[fd] == NULL) { 1396426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 13972bc21ed9SDavid Malone error = EBADF; 13982bc21ed9SDavid Malone goto out; 13992bc21ed9SDavid Malone } 1400e7d6662fSAlfred Perlstein fp = fdescp->fd_ofiles[fd]; 1401e7d6662fSAlfred Perlstein if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 1402e7d6662fSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 1403e7d6662fSAlfred Perlstein error = EOPNOTSUPP; 1404e7d6662fSAlfred Perlstein goto out; 1405e7d6662fSAlfred Perlstein } 1406e7d6662fSAlfred Perlstein 1407df8bae1dSRodney W. Grimes } 1408ed5b7817SJulian Elischer /* 14091c381b19SRobert Watson * Now replace the integer FDs with pointers to the 14101c381b19SRobert Watson * associated global file table entry.. 1411ed5b7817SJulian Elischer */ 14122bc21ed9SDavid Malone newlen = oldfds * sizeof(struct file *); 14132bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 14142bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 14152bc21ed9SDavid Malone if (*controlp == NULL) { 1416426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 14172bc21ed9SDavid Malone error = E2BIG; 14182bc21ed9SDavid Malone goto out; 14198692c025SYoshinobu Inoue } 14208692c025SYoshinobu Inoue 14212bc21ed9SDavid Malone fdp = data; 14222bc21ed9SDavid Malone rp = (struct file **) 14232bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 14248692c025SYoshinobu Inoue for (i = 0; i < oldfds; i++) { 14258692c025SYoshinobu Inoue fp = fdescp->fd_ofiles[*fdp++]; 1426df8bae1dSRodney W. Grimes *rp++ = fp; 1427426da3bcSAlfred Perlstein FILE_LOCK(fp); 1428df8bae1dSRodney W. Grimes fp->f_count++; 1429df8bae1dSRodney W. Grimes fp->f_msgcount++; 1430426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1431df8bae1dSRodney W. Grimes unp_rights++; 1432df8bae1dSRodney W. Grimes } 1433426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 14342bc21ed9SDavid Malone break; 14352bc21ed9SDavid Malone 14362bc21ed9SDavid Malone case SCM_TIMESTAMP: 14372bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*tv), 14382bc21ed9SDavid Malone SCM_TIMESTAMP, SOL_SOCKET); 14392bc21ed9SDavid Malone if (*controlp == NULL) { 14402bc21ed9SDavid Malone error = ENOBUFS; 14412bc21ed9SDavid Malone goto out; 14428692c025SYoshinobu Inoue } 14432bc21ed9SDavid Malone tv = (struct timeval *) 14442bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 14452bc21ed9SDavid Malone microtime(tv); 14462bc21ed9SDavid Malone break; 14472bc21ed9SDavid Malone 14482bc21ed9SDavid Malone default: 14492bc21ed9SDavid Malone error = EINVAL; 14502bc21ed9SDavid Malone goto out; 14512bc21ed9SDavid Malone } 14522bc21ed9SDavid Malone 14532bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 14542bc21ed9SDavid Malone 14552bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 14562bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 14572bc21ed9SDavid Malone cm = (struct cmsghdr *) 14582bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 14592bc21ed9SDavid Malone } else { 14602bc21ed9SDavid Malone clen = 0; 14612bc21ed9SDavid Malone cm = NULL; 14622bc21ed9SDavid Malone } 14632bc21ed9SDavid Malone } 14642bc21ed9SDavid Malone 14652bc21ed9SDavid Malone out: 14662bc21ed9SDavid Malone m_freem(control); 14672bc21ed9SDavid Malone 14682bc21ed9SDavid Malone return (error); 1469df8bae1dSRodney W. Grimes } 1470df8bae1dSRodney W. Grimes 14716a2989fdSMatthew N. Dodd struct mbuf * 14726a2989fdSMatthew N. Dodd unp_addsockcred(struct thread *td, struct mbuf *control) 14736a2989fdSMatthew N. Dodd { 147470df31f4SMaxim Konovalov struct mbuf *m, *n, *n_prev; 14756a2989fdSMatthew N. Dodd struct sockcred *sc; 147670df31f4SMaxim Konovalov const struct cmsghdr *cm; 14776a2989fdSMatthew N. Dodd int ngroups; 14786a2989fdSMatthew N. Dodd int i; 14796a2989fdSMatthew N. Dodd 14806a2989fdSMatthew N. Dodd ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX); 14816a2989fdSMatthew N. Dodd 14826a2989fdSMatthew N. Dodd m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET); 14836a2989fdSMatthew N. Dodd if (m == NULL) 14846a2989fdSMatthew N. Dodd return (control); 14856a2989fdSMatthew N. Dodd 14866a2989fdSMatthew N. Dodd sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *)); 14876a2989fdSMatthew N. Dodd sc->sc_uid = td->td_ucred->cr_ruid; 14886a2989fdSMatthew N. Dodd sc->sc_euid = td->td_ucred->cr_uid; 14896a2989fdSMatthew N. Dodd sc->sc_gid = td->td_ucred->cr_rgid; 14906a2989fdSMatthew N. Dodd sc->sc_egid = td->td_ucred->cr_gid; 14916a2989fdSMatthew N. Dodd sc->sc_ngroups = ngroups; 14926a2989fdSMatthew N. Dodd for (i = 0; i < sc->sc_ngroups; i++) 14936a2989fdSMatthew N. Dodd sc->sc_groups[i] = td->td_ucred->cr_groups[i]; 14946a2989fdSMatthew N. Dodd 14956a2989fdSMatthew N. Dodd /* 14961c381b19SRobert Watson * Unlink SCM_CREDS control messages (struct cmsgcred), since just 14971c381b19SRobert Watson * created SCM_CREDS control message (struct sockcred) has another 14981c381b19SRobert Watson * format. 14996a2989fdSMatthew N. Dodd */ 150070df31f4SMaxim Konovalov if (control != NULL) 150170df31f4SMaxim Konovalov for (n = control, n_prev = NULL; n != NULL;) { 150270df31f4SMaxim Konovalov cm = mtod(n, struct cmsghdr *); 150370df31f4SMaxim Konovalov if (cm->cmsg_level == SOL_SOCKET && 150470df31f4SMaxim Konovalov cm->cmsg_type == SCM_CREDS) { 150570df31f4SMaxim Konovalov if (n_prev == NULL) 150670df31f4SMaxim Konovalov control = n->m_next; 150770df31f4SMaxim Konovalov else 150870df31f4SMaxim Konovalov n_prev->m_next = n->m_next; 150970df31f4SMaxim Konovalov n = m_free(n); 151070df31f4SMaxim Konovalov } else { 151170df31f4SMaxim Konovalov n_prev = n; 151270df31f4SMaxim Konovalov n = n->m_next; 151370df31f4SMaxim Konovalov } 151470df31f4SMaxim Konovalov } 15156a2989fdSMatthew N. Dodd 151670df31f4SMaxim Konovalov /* Prepend it to the head. */ 151770df31f4SMaxim Konovalov m->m_next = control; 151870df31f4SMaxim Konovalov 151970df31f4SMaxim Konovalov return (m); 15206a2989fdSMatthew N. Dodd } 15216a2989fdSMatthew N. Dodd 1522161a0c7cSRobert Watson /* 1523a0ec558aSRobert Watson * unp_defer indicates whether additional work has been defered for a future 1524a0ec558aSRobert Watson * pass through unp_gc(). It is thread local and does not require explicit 1525a0ec558aSRobert Watson * synchronization. 1526161a0c7cSRobert Watson */ 1527a0ec558aSRobert Watson static int unp_defer; 1528a0ec558aSRobert Watson 1529a0ec558aSRobert Watson static int unp_taskcount; 1530a0ec558aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0, ""); 1531a0ec558aSRobert Watson 1532a0ec558aSRobert Watson static int unp_recycled; 1533a0ec558aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0, ""); 1534df8bae1dSRodney W. Grimes 1535f708ef1bSPoul-Henning Kamp static void 1536a0ec558aSRobert Watson unp_gc(__unused void *arg, int pending) 1537df8bae1dSRodney W. Grimes { 1538892af6b9SRobert Watson struct file *fp, *nextfp; 1539892af6b9SRobert Watson struct socket *so; 1540df8bae1dSRodney W. Grimes struct file **extra_ref, **fpp; 1541df8bae1dSRodney W. Grimes int nunref, i; 154295f004dcSAlfred Perlstein int nfiles_snap; 154395f004dcSAlfred Perlstein int nfiles_slack = 20; 1544df8bae1dSRodney W. Grimes 1545a0ec558aSRobert Watson unp_taskcount++; 1546df8bae1dSRodney W. Grimes unp_defer = 0; 1547ed5b7817SJulian Elischer /* 15481c381b19SRobert Watson * Before going through all this, set all FDs to be NOT defered and 15491c381b19SRobert Watson * NOT externally accessible. 1550ed5b7817SJulian Elischer */ 1551426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 15522e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) 1553426da3bcSAlfred Perlstein fp->f_gcflag &= ~(FMARK|FDEFER); 1554df8bae1dSRodney W. Grimes do { 15555bb84bc8SRobert Watson KASSERT(unp_defer >= 0, ("unp_gc: unp_defer %d", unp_defer)); 15562e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) { 1557426da3bcSAlfred Perlstein FILE_LOCK(fp); 1558ed5b7817SJulian Elischer /* 1559a0ec558aSRobert Watson * If the file is not open, skip it -- could be a 1560a0ec558aSRobert Watson * file in the process of being opened, or in the 1561a0ec558aSRobert Watson * process of being closed. If the file is 1562a0ec558aSRobert Watson * "closing", it may have been marked for deferred 1563a0ec558aSRobert Watson * consideration. Clear the flag now if so. 1564ed5b7817SJulian Elischer */ 1565426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1566a0ec558aSRobert Watson if (fp->f_gcflag & FDEFER) 1567a0ec558aSRobert Watson unp_defer--; 1568a0ec558aSRobert Watson fp->f_gcflag &= ~(FMARK|FDEFER); 1569426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1570df8bae1dSRodney W. Grimes continue; 1571426da3bcSAlfred Perlstein } 1572ed5b7817SJulian Elischer /* 15731c381b19SRobert Watson * If we already marked it as 'defer' in a previous 15741c381b19SRobert Watson * pass, then try process it this time and un-mark 15751c381b19SRobert Watson * it. 1576ed5b7817SJulian Elischer */ 1577426da3bcSAlfred Perlstein if (fp->f_gcflag & FDEFER) { 1578426da3bcSAlfred Perlstein fp->f_gcflag &= ~FDEFER; 1579df8bae1dSRodney W. Grimes unp_defer--; 1580df8bae1dSRodney W. Grimes } else { 1581ed5b7817SJulian Elischer /* 1582ed5b7817SJulian Elischer * if it's not defered, then check if it's 1583ed5b7817SJulian Elischer * already marked.. if so skip it 1584ed5b7817SJulian Elischer */ 1585426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) { 1586426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1587df8bae1dSRodney W. Grimes continue; 1588426da3bcSAlfred Perlstein } 1589ed5b7817SJulian Elischer /* 15901c381b19SRobert Watson * If all references are from messages in 15911c381b19SRobert Watson * transit, then skip it. it's not externally 15921c381b19SRobert Watson * accessible. 1593ed5b7817SJulian Elischer */ 1594426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount) { 1595426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1596df8bae1dSRodney W. Grimes continue; 1597426da3bcSAlfred Perlstein } 1598ed5b7817SJulian Elischer /* 1599ed5b7817SJulian Elischer * If it got this far then it must be 1600ed5b7817SJulian Elischer * externally accessible. 1601ed5b7817SJulian Elischer */ 1602426da3bcSAlfred Perlstein fp->f_gcflag |= FMARK; 1603df8bae1dSRodney W. Grimes } 1604ed5b7817SJulian Elischer /* 16051c381b19SRobert Watson * Either it was defered, or it is externally 16061c381b19SRobert Watson * accessible and not already marked so. Now check 16071c381b19SRobert Watson * if it is possibly one of OUR sockets. 1608ed5b7817SJulian Elischer */ 1609df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_SOCKET || 161048e3128bSMatthew Dillon (so = fp->f_data) == NULL) { 1611426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1612df8bae1dSRodney W. Grimes continue; 1613426da3bcSAlfred Perlstein } 1614426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1615748e0b0aSGarrett Wollman if (so->so_proto->pr_domain != &localdomain || 1616df8bae1dSRodney W. Grimes (so->so_proto->pr_flags&PR_RIGHTS) == 0) 1617df8bae1dSRodney W. Grimes continue; 1618ed5b7817SJulian Elischer /* 16191c381b19SRobert Watson * So, Ok, it's one of our sockets and it IS 16201c381b19SRobert Watson * externally accessible (or was defered). Now we 16211c381b19SRobert Watson * look to see if we hold any file descriptors in its 1622ed5b7817SJulian Elischer * message buffers. Follow those links and mark them 1623ed5b7817SJulian Elischer * as accessible too. 1624ed5b7817SJulian Elischer */ 16257717cf07SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1626df8bae1dSRodney W. Grimes unp_scan(so->so_rcv.sb_mb, unp_mark); 16277717cf07SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 1628df8bae1dSRodney W. Grimes } 1629df8bae1dSRodney W. Grimes } while (unp_defer); 1630426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1631df8bae1dSRodney W. Grimes /* 1632a0ec558aSRobert Watson * XXXRW: The following comments need updating for a post-SMPng and 1633a0ec558aSRobert Watson * deferred unp_gc() world, but are still generally accurate. 1634a0ec558aSRobert Watson * 16351c381b19SRobert Watson * We grab an extra reference to each of the file table entries that 16361c381b19SRobert Watson * are not otherwise accessible and then free the rights that are 16371c381b19SRobert Watson * stored in messages on them. 1638df8bae1dSRodney W. Grimes * 1639df8bae1dSRodney W. Grimes * The bug in the orginal code is a little tricky, so I'll describe 1640df8bae1dSRodney W. Grimes * what's wrong with it here. 1641df8bae1dSRodney W. Grimes * 1642df8bae1dSRodney W. Grimes * It is incorrect to simply unp_discard each entry for f_msgcount 1643df8bae1dSRodney W. Grimes * times -- consider the case of sockets A and B that contain 1644df8bae1dSRodney W. Grimes * references to each other. On a last close of some other socket, 1645df8bae1dSRodney W. Grimes * we trigger a gc since the number of outstanding rights (unp_rights) 1646a0ec558aSRobert Watson * is non-zero. If during the sweep phase the gc code unp_discards, 1647df8bae1dSRodney W. Grimes * we end up doing a (full) closef on the descriptor. A closef on A 1648df8bae1dSRodney W. Grimes * results in the following chain. Closef calls soo_close, which 1649df8bae1dSRodney W. Grimes * calls soclose. Soclose calls first (through the switch 1650df8bae1dSRodney W. Grimes * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 16511c381b19SRobert Watson * returns because the previous instance had set unp_gcing, and we 16521c381b19SRobert Watson * return all the way back to soclose, which marks the socket with 16531c381b19SRobert Watson * SS_NOFDREF, and then calls sofree. Sofree calls sorflush to free 16541c381b19SRobert Watson * up the rights that are queued in messages on the socket A, i.e., 16551c381b19SRobert Watson * the reference on B. The sorflush calls via the dom_dispose switch 16561c381b19SRobert Watson * unp_dispose, which unp_scans with unp_discard. This second 1657df8bae1dSRodney W. Grimes * instance of unp_discard just calls closef on B. 1658df8bae1dSRodney W. Grimes * 1659df8bae1dSRodney W. Grimes * Well, a similar chain occurs on B, resulting in a sorflush on B, 1660df8bae1dSRodney W. Grimes * which results in another closef on A. Unfortunately, A is already 1661df8bae1dSRodney W. Grimes * being closed, and the descriptor has already been marked with 1662df8bae1dSRodney W. Grimes * SS_NOFDREF, and soclose panics at this point. 1663df8bae1dSRodney W. Grimes * 1664df8bae1dSRodney W. Grimes * Here, we first take an extra reference to each inaccessible 16651c381b19SRobert Watson * descriptor. Then, we call sorflush ourself, since we know it is a 16661c381b19SRobert Watson * Unix domain socket anyhow. After we destroy all the rights 16671c381b19SRobert Watson * carried in messages, we do a last closef to get rid of our extra 16681c381b19SRobert Watson * reference. This is the last close, and the unp_detach etc will 16691c381b19SRobert Watson * shut down the socket. 1670df8bae1dSRodney W. Grimes * 1671df8bae1dSRodney W. Grimes * 91/09/19, bsy@cs.cmu.edu 1672df8bae1dSRodney W. Grimes */ 167395f004dcSAlfred Perlstein again: 1674e4643c73SPoul-Henning Kamp nfiles_snap = openfiles + nfiles_slack; /* some slack */ 167595f004dcSAlfred Perlstein extra_ref = malloc(nfiles_snap * sizeof(struct file *), M_TEMP, 167695f004dcSAlfred Perlstein M_WAITOK); 1677426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 1678e4643c73SPoul-Henning Kamp if (nfiles_snap < openfiles) { 167995f004dcSAlfred Perlstein sx_sunlock(&filelist_lock); 168095f004dcSAlfred Perlstein free(extra_ref, M_TEMP); 168195f004dcSAlfred Perlstein nfiles_slack += 20; 168295f004dcSAlfred Perlstein goto again; 168395f004dcSAlfred Perlstein } 1684fc3fcacfSRobert Watson for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; 1685fc3fcacfSRobert Watson fp != NULL; fp = nextfp) { 16862e3c8fcbSPoul-Henning Kamp nextfp = LIST_NEXT(fp, f_list); 1687426da3bcSAlfred Perlstein FILE_LOCK(fp); 1688ed5b7817SJulian Elischer /* 1689ed5b7817SJulian Elischer * If it's not open, skip it 1690ed5b7817SJulian Elischer */ 1691426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1692426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1693df8bae1dSRodney W. Grimes continue; 1694426da3bcSAlfred Perlstein } 1695ed5b7817SJulian Elischer /* 1696ed5b7817SJulian Elischer * If all refs are from msgs, and it's not marked accessible 16971c381b19SRobert Watson * then it must be referenced from some unreachable cycle of 16981c381b19SRobert Watson * (shut-down) FDs, so include it in our list of FDs to 16991c381b19SRobert Watson * remove. 1700ed5b7817SJulian Elischer */ 1701426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) { 1702df8bae1dSRodney W. Grimes *fpp++ = fp; 1703df8bae1dSRodney W. Grimes nunref++; 1704df8bae1dSRodney W. Grimes fp->f_count++; 1705df8bae1dSRodney W. Grimes } 1706426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1707df8bae1dSRodney W. Grimes } 1708426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1709ed5b7817SJulian Elischer /* 17101c381b19SRobert Watson * For each FD on our hit list, do the following two things: 1711ed5b7817SJulian Elischer */ 17121c7c3c6aSMatthew Dillon for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 17131c7c3c6aSMatthew Dillon struct file *tfp = *fpp; 1714426da3bcSAlfred Perlstein FILE_LOCK(tfp); 1715cd72f218SMatthew Dillon if (tfp->f_type == DTYPE_SOCKET && 171648e3128bSMatthew Dillon tfp->f_data != NULL) { 1717426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 171848e3128bSMatthew Dillon sorflush(tfp->f_data); 1719e5aeaa0cSDag-Erling Smørgrav } else { 1720426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 17211c7c3c6aSMatthew Dillon } 1722e5aeaa0cSDag-Erling Smørgrav } 1723a0ec558aSRobert Watson for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 1724b40ce416SJulian Elischer closef(*fpp, (struct thread *) NULL); 1725a0ec558aSRobert Watson unp_recycled++; 1726a0ec558aSRobert Watson } 1727210a5a71SAlfred Perlstein free(extra_ref, M_TEMP); 1728df8bae1dSRodney W. Grimes } 1729df8bae1dSRodney W. Grimes 173026f9a767SRodney W. Grimes void 1731892af6b9SRobert Watson unp_dispose(struct mbuf *m) 1732df8bae1dSRodney W. Grimes { 1733996c772fSJohn Dyson 1734df8bae1dSRodney W. Grimes if (m) 1735df8bae1dSRodney W. Grimes unp_scan(m, unp_discard); 1736df8bae1dSRodney W. Grimes } 1737df8bae1dSRodney W. Grimes 17380c1bb4fbSDima Dorfman static int 1739d374e81eSRobert Watson unp_listen(struct socket *so, struct unpcb *unp, int backlog, 1740d374e81eSRobert Watson struct thread *td) 17410c1bb4fbSDima Dorfman { 17420daccb9cSRobert Watson int error; 17430daccb9cSRobert Watson 17440d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 17450c1bb4fbSDima Dorfman 17460daccb9cSRobert Watson SOCK_LOCK(so); 17470daccb9cSRobert Watson error = solisten_proto_check(so); 17480daccb9cSRobert Watson if (error == 0) { 17496f105b34SJohn Baldwin cru2x(td->td_ucred, &unp->unp_peercred); 17500c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPCCACHED; 1751d374e81eSRobert Watson solisten_proto(so, backlog); 17520daccb9cSRobert Watson } 17530daccb9cSRobert Watson SOCK_UNLOCK(so); 17540daccb9cSRobert Watson return (error); 17550c1bb4fbSDima Dorfman } 17560c1bb4fbSDima Dorfman 1757f708ef1bSPoul-Henning Kamp static void 1758892af6b9SRobert Watson unp_scan(struct mbuf *m0, void (*op)(struct file *)) 1759df8bae1dSRodney W. Grimes { 17602bc21ed9SDavid Malone struct mbuf *m; 17612bc21ed9SDavid Malone struct file **rp; 17622bc21ed9SDavid Malone struct cmsghdr *cm; 17632bc21ed9SDavid Malone void *data; 17642bc21ed9SDavid Malone int i; 17652bc21ed9SDavid Malone socklen_t clen, datalen; 1766df8bae1dSRodney W. Grimes int qfds; 1767df8bae1dSRodney W. Grimes 1768fc3fcacfSRobert Watson while (m0 != NULL) { 17692bc21ed9SDavid Malone for (m = m0; m; m = m->m_next) { 177012396bdcSDavid Malone if (m->m_type != MT_CONTROL) 1771df8bae1dSRodney W. Grimes continue; 17722bc21ed9SDavid Malone 17732bc21ed9SDavid Malone cm = mtod(m, struct cmsghdr *); 17742bc21ed9SDavid Malone clen = m->m_len; 17752bc21ed9SDavid Malone 17762bc21ed9SDavid Malone while (cm != NULL) { 17772bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) 17782bc21ed9SDavid Malone break; 17792bc21ed9SDavid Malone 17802bc21ed9SDavid Malone data = CMSG_DATA(cm); 17812bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len 17822bc21ed9SDavid Malone - (caddr_t)data; 17832bc21ed9SDavid Malone 17842bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET && 17852bc21ed9SDavid Malone cm->cmsg_type == SCM_RIGHTS) { 17862bc21ed9SDavid Malone qfds = datalen / sizeof (struct file *); 17872bc21ed9SDavid Malone rp = data; 1788df8bae1dSRodney W. Grimes for (i = 0; i < qfds; i++) 1789df8bae1dSRodney W. Grimes (*op)(*rp++); 17902bc21ed9SDavid Malone } 17912bc21ed9SDavid Malone 17922bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 17932bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 17942bc21ed9SDavid Malone cm = (struct cmsghdr *) 17952bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 17962bc21ed9SDavid Malone } else { 17972bc21ed9SDavid Malone clen = 0; 17982bc21ed9SDavid Malone cm = NULL; 17992bc21ed9SDavid Malone } 18002bc21ed9SDavid Malone } 1801df8bae1dSRodney W. Grimes } 1802df8bae1dSRodney W. Grimes m0 = m0->m_act; 1803df8bae1dSRodney W. Grimes } 1804df8bae1dSRodney W. Grimes } 1805df8bae1dSRodney W. Grimes 1806f708ef1bSPoul-Henning Kamp static void 1807892af6b9SRobert Watson unp_mark(struct file *fp) 1808df8bae1dSRodney W. Grimes { 1809426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) 1810df8bae1dSRodney W. Grimes return; 1811df8bae1dSRodney W. Grimes unp_defer++; 1812426da3bcSAlfred Perlstein fp->f_gcflag |= (FMARK|FDEFER); 1813df8bae1dSRodney W. Grimes } 1814df8bae1dSRodney W. Grimes 1815f708ef1bSPoul-Henning Kamp static void 1816892af6b9SRobert Watson unp_discard(struct file *fp) 1817df8bae1dSRodney W. Grimes { 1818a0ec558aSRobert Watson UNP_LOCK(); 1819426da3bcSAlfred Perlstein FILE_LOCK(fp); 1820df8bae1dSRodney W. Grimes fp->f_msgcount--; 1821df8bae1dSRodney W. Grimes unp_rights--; 1822426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1823a0ec558aSRobert Watson UNP_UNLOCK(); 1824b40ce416SJulian Elischer (void) closef(fp, (struct thread *)NULL); 1825df8bae1dSRodney W. Grimes } 1826