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> */ 43639acc13SGarrett Wollman #include <sys/file.h> 44960ed29cSSeigo Tanimura #include <sys/filedesc.h> 45960ed29cSSeigo Tanimura #include <sys/jail.h> 46960ed29cSSeigo Tanimura #include <sys/kernel.h> 47960ed29cSSeigo Tanimura #include <sys/lock.h> 486ea48a90SRobert Watson #include <sys/mac.h> 49639acc13SGarrett Wollman #include <sys/mbuf.h> 50033eb86eSJeff Roberson #include <sys/mount.h> 51960ed29cSSeigo Tanimura #include <sys/mutex.h> 52639acc13SGarrett Wollman #include <sys/namei.h> 53639acc13SGarrett Wollman #include <sys/proc.h> 54df8bae1dSRodney W. Grimes #include <sys/protosw.h> 55960ed29cSSeigo Tanimura #include <sys/resourcevar.h> 56df8bae1dSRodney W. Grimes #include <sys/socket.h> 57df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 58960ed29cSSeigo Tanimura #include <sys/signalvar.h> 59df8bae1dSRodney W. Grimes #include <sys/stat.h> 60960ed29cSSeigo Tanimura #include <sys/sx.h> 61639acc13SGarrett Wollman #include <sys/sysctl.h> 62960ed29cSSeigo Tanimura #include <sys/systm.h> 63a0ec558aSRobert Watson #include <sys/taskqueue.h> 64639acc13SGarrett Wollman #include <sys/un.h> 6598271db4SGarrett Wollman #include <sys/unpcb.h> 66639acc13SGarrett Wollman #include <sys/vnode.h> 67df8bae1dSRodney W. Grimes 689e9d298aSJeff Roberson #include <vm/uma.h> 6998271db4SGarrett Wollman 709e9d298aSJeff Roberson static uma_zone_t unp_zone; 7198271db4SGarrett Wollman static unp_gen_t unp_gencnt; 7298271db4SGarrett Wollman static u_int unp_count; 7398271db4SGarrett Wollman 7498271db4SGarrett Wollman static struct unp_head unp_shead, unp_dhead; 7598271db4SGarrett Wollman 76df8bae1dSRodney W. Grimes /* 77df8bae1dSRodney W. Grimes * Unix communications domain. 78df8bae1dSRodney W. Grimes * 79df8bae1dSRodney W. Grimes * TODO: 80df8bae1dSRodney W. Grimes * SEQPACKET, RDM 81df8bae1dSRodney W. Grimes * rethink name space problems 82df8bae1dSRodney W. Grimes * need a proper out-of-band 8398271db4SGarrett Wollman * lock pushdown 84df8bae1dSRodney W. Grimes */ 85e7dd9a10SRobert Watson static const struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 86f708ef1bSPoul-Henning Kamp static ino_t unp_ino; /* prototype for fake inode numbers */ 876a2989fdSMatthew N. Dodd struct mbuf *unp_addsockcred(struct thread *, struct mbuf *); 88f708ef1bSPoul-Henning Kamp 89ce5f32deSRobert Watson /* 90ce5f32deSRobert Watson * Currently, UNIX domain sockets are protected by a single subsystem lock, 91ce5f32deSRobert Watson * which covers global data structures and variables, the contents of each 92ce5f32deSRobert Watson * per-socket unpcb structure, and the so_pcb field in sockets attached to 93ce5f32deSRobert Watson * the UNIX domain. This provides for a moderate degree of paralellism, as 94ce5f32deSRobert Watson * receive operations on UNIX domain sockets do not need to acquire the 95ce5f32deSRobert Watson * subsystem lock. Finer grained locking to permit send() without acquiring 96ce5f32deSRobert Watson * a global lock would be a logical next step. 97ce5f32deSRobert Watson * 98ce5f32deSRobert Watson * The UNIX domain socket lock preceds all socket layer locks, including the 99ce5f32deSRobert Watson * socket lock and socket buffer lock, permitting UNIX domain socket code to 100ce5f32deSRobert Watson * call into socket support routines without releasing its locks. 101ce5f32deSRobert Watson * 102ce5f32deSRobert Watson * Some caution is required in areas where the UNIX domain socket code enters 103ce5f32deSRobert Watson * VFS in order to create or find rendezvous points. This results in 104ce5f32deSRobert Watson * dropping of the UNIX domain socket subsystem lock, acquisition of the 105ce5f32deSRobert Watson * Giant lock, and potential sleeping. This increases the chances of races, 106ce5f32deSRobert Watson * and exposes weaknesses in the socket->protocol API by offering poor 107ce5f32deSRobert Watson * failure modes. 108ce5f32deSRobert Watson */ 1090d9ce3a1SRobert Watson static struct mtx unp_mtx; 1100d9ce3a1SRobert Watson #define UNP_LOCK_INIT() \ 1110d9ce3a1SRobert Watson mtx_init(&unp_mtx, "unp", NULL, MTX_DEF) 1120d9ce3a1SRobert Watson #define UNP_LOCK() mtx_lock(&unp_mtx) 1130d9ce3a1SRobert Watson #define UNP_UNLOCK() mtx_unlock(&unp_mtx) 1140d9ce3a1SRobert Watson #define UNP_LOCK_ASSERT() mtx_assert(&unp_mtx, MA_OWNED) 1154c5bc1caSRobert Watson #define UNP_UNLOCK_ASSERT() mtx_assert(&unp_mtx, MA_NOTOWNED) 1160d9ce3a1SRobert Watson 117a0ec558aSRobert Watson /* 118a0ec558aSRobert Watson * Garbage collection of cyclic file descriptor/socket references occurs 119a0ec558aSRobert Watson * asynchronously in a taskqueue context in order to avoid recursion and 120a0ec558aSRobert Watson * reentrance in the UNIX domain socket, file descriptor, and socket layer 121a0ec558aSRobert Watson * code. See unp_gc() for a full description. 122a0ec558aSRobert Watson */ 123a0ec558aSRobert Watson static struct task unp_gc_task; 124a0ec558aSRobert Watson 1254d77a549SAlfred Perlstein static int unp_attach(struct socket *); 1264d77a549SAlfred Perlstein static void unp_detach(struct unpcb *); 1274d77a549SAlfred Perlstein static int unp_bind(struct unpcb *,struct sockaddr *, struct thread *); 12870f52b48SBruce Evans static int unp_connect(struct socket *,struct sockaddr *, struct thread *); 1296a2989fdSMatthew N. Dodd static int unp_connect2(struct socket *so, struct socket *so2, int); 1304d77a549SAlfred Perlstein static void unp_disconnect(struct unpcb *); 1314d77a549SAlfred Perlstein static void unp_shutdown(struct unpcb *); 1324d77a549SAlfred Perlstein static void unp_drop(struct unpcb *, int); 133a0ec558aSRobert Watson static void unp_gc(__unused void *, int); 1344d77a549SAlfred Perlstein static void unp_scan(struct mbuf *, void (*)(struct file *)); 1354d77a549SAlfred Perlstein static void unp_mark(struct file *); 1364d77a549SAlfred Perlstein static void unp_discard(struct file *); 1374d77a549SAlfred Perlstein static void unp_freerights(struct file **, int); 1384d77a549SAlfred Perlstein static int unp_internalize(struct mbuf **, struct thread *); 139d374e81eSRobert Watson static int unp_listen(struct socket *, struct unpcb *, int, 140d374e81eSRobert Watson struct thread *); 141f708ef1bSPoul-Henning Kamp 142ac45e92fSRobert Watson static void 143a29f300eSGarrett Wollman uipc_abort(struct socket *so) 144df8bae1dSRodney W. Grimes { 14540f2ac28SRobert Watson struct unpcb *unp; 146df8bae1dSRodney W. Grimes 14740f2ac28SRobert Watson unp = sotounpcb(so); 1484d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_abort: unp == NULL")); 1494d4b555eSRobert Watson UNP_LOCK(); 150a29f300eSGarrett Wollman unp_drop(unp, ECONNABORTED); 1514c5bc1caSRobert Watson unp_detach(unp); 1524c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 153df8bae1dSRodney W. Grimes } 154df8bae1dSRodney W. Grimes 155a29f300eSGarrett Wollman static int 15657bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam) 157a29f300eSGarrett Wollman { 15840f2ac28SRobert Watson struct unpcb *unp; 1590d9ce3a1SRobert Watson const struct sockaddr *sa; 160df8bae1dSRodney W. Grimes 161df8bae1dSRodney W. Grimes /* 162df8bae1dSRodney W. Grimes * Pass back name of connected socket, 163df8bae1dSRodney W. Grimes * if it was bound and we are still connected 164df8bae1dSRodney W. Grimes * (our peer may have closed already!). 165df8bae1dSRodney W. Grimes */ 1664d4b555eSRobert Watson unp = sotounpcb(so); 1674d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_accept: unp == NULL")); 1680d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 1690d9ce3a1SRobert Watson UNP_LOCK(); 1700d9ce3a1SRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr != NULL) 1710d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 1720d9ce3a1SRobert Watson else 1730d9ce3a1SRobert Watson sa = &sun_noname; 1740d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 1750d9ce3a1SRobert Watson UNP_UNLOCK(); 176e5aeaa0cSDag-Erling Smørgrav return (0); 177a29f300eSGarrett Wollman } 178df8bae1dSRodney W. Grimes 179a29f300eSGarrett Wollman static int 180b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td) 181a29f300eSGarrett Wollman { 182df8bae1dSRodney W. Grimes 183e5aeaa0cSDag-Erling Smørgrav return (unp_attach(so)); 184a29f300eSGarrett Wollman } 185a29f300eSGarrett Wollman 186a29f300eSGarrett Wollman static int 187b40ce416SJulian Elischer uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 188a29f300eSGarrett Wollman { 18940f2ac28SRobert Watson struct unpcb *unp; 19040f2ac28SRobert Watson int error; 191a29f300eSGarrett Wollman 19240f2ac28SRobert Watson unp = sotounpcb(so); 1934d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_bind: unp == NULL")); 1944d4b555eSRobert Watson UNP_LOCK(); 19540f2ac28SRobert Watson error = unp_bind(unp, nam, td); 19640f2ac28SRobert Watson UNP_UNLOCK(); 19740f2ac28SRobert Watson return (error); 198a29f300eSGarrett Wollman } 199a29f300eSGarrett Wollman 200a29f300eSGarrett Wollman static int 201b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 202a29f300eSGarrett Wollman { 203b295bdcdSRobert Watson struct unpcb *unp; 2040d9ce3a1SRobert Watson int error; 205a29f300eSGarrett Wollman 206fd179ee9SRobert Watson KASSERT(td == curthread, ("uipc_connect: td != curthread")); 207b295bdcdSRobert Watson unp = sotounpcb(so); 2084d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_connect: unp == NULL")); 2094d4b555eSRobert Watson UNP_LOCK(); 210fd179ee9SRobert Watson error = unp_connect(so, nam, td); 2110d9ce3a1SRobert Watson UNP_UNLOCK(); 2120d9ce3a1SRobert Watson return (error); 213a29f300eSGarrett Wollman } 214a29f300eSGarrett Wollman 215db48c0d2SRobert Watson int 216a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2) 217a29f300eSGarrett Wollman { 21840f2ac28SRobert Watson struct unpcb *unp; 2190d9ce3a1SRobert Watson int error; 220a29f300eSGarrett Wollman 22140f2ac28SRobert Watson unp = sotounpcb(so1); 2224d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_connect2: unp == NULL")); 2234d4b555eSRobert Watson UNP_LOCK(); 2246a2989fdSMatthew N. Dodd error = unp_connect2(so1, so2, PRU_CONNECT2); 2250d9ce3a1SRobert Watson UNP_UNLOCK(); 2260d9ce3a1SRobert Watson return (error); 227a29f300eSGarrett Wollman } 228a29f300eSGarrett Wollman 229a29f300eSGarrett Wollman /* control is EOPNOTSUPP */ 230a29f300eSGarrett Wollman 231bc725eafSRobert Watson static void 232a29f300eSGarrett Wollman uipc_detach(struct socket *so) 233a29f300eSGarrett Wollman { 23440f2ac28SRobert Watson struct unpcb *unp; 235a29f300eSGarrett Wollman 23640f2ac28SRobert Watson unp = sotounpcb(so); 2374d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_detach: unp == NULL")); 2384d4b555eSRobert Watson UNP_LOCK(); 2394c5bc1caSRobert Watson unp_detach(unp); 2404c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 241a29f300eSGarrett Wollman } 242a29f300eSGarrett Wollman 243a29f300eSGarrett Wollman static int 244a29f300eSGarrett Wollman uipc_disconnect(struct socket *so) 245a29f300eSGarrett Wollman { 24640f2ac28SRobert Watson struct unpcb *unp; 247a29f300eSGarrett Wollman 24840f2ac28SRobert Watson unp = sotounpcb(so); 2494d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_disconnect: unp == NULL")); 2504d4b555eSRobert Watson UNP_LOCK(); 251a29f300eSGarrett Wollman unp_disconnect(unp); 2520d9ce3a1SRobert Watson UNP_UNLOCK(); 253e5aeaa0cSDag-Erling Smørgrav return (0); 254a29f300eSGarrett Wollman } 255a29f300eSGarrett Wollman 256a29f300eSGarrett Wollman static int 257d374e81eSRobert Watson uipc_listen(struct socket *so, int backlog, struct thread *td) 258a29f300eSGarrett Wollman { 25940f2ac28SRobert Watson struct unpcb *unp; 2600d9ce3a1SRobert Watson int error; 261a29f300eSGarrett Wollman 26240f2ac28SRobert Watson unp = sotounpcb(so); 2634d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_listen: unp == NULL")); 2644d4b555eSRobert Watson UNP_LOCK(); 2654d4b555eSRobert Watson if (unp->unp_vnode == NULL) { 26640f2ac28SRobert Watson UNP_UNLOCK(); 26740f2ac28SRobert Watson return (EINVAL); 26840f2ac28SRobert Watson } 269d374e81eSRobert Watson error = unp_listen(so, unp, backlog, td); 2700d9ce3a1SRobert Watson UNP_UNLOCK(); 2710d9ce3a1SRobert Watson return (error); 272a29f300eSGarrett Wollman } 273a29f300eSGarrett Wollman 274a29f300eSGarrett Wollman static int 27557bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 276a29f300eSGarrett Wollman { 27740f2ac28SRobert Watson struct unpcb *unp; 2780d9ce3a1SRobert Watson const struct sockaddr *sa; 279a29f300eSGarrett Wollman 2804d4b555eSRobert Watson unp = sotounpcb(so); 2814d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL")); 2820d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 2830d9ce3a1SRobert Watson UNP_LOCK(); 284fc3fcacfSRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr!= NULL) 2850d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 286bdc5f6a3SHajimu UMEMOTO else { 287bdc5f6a3SHajimu UMEMOTO /* 288bdc5f6a3SHajimu UMEMOTO * XXX: It seems that this test always fails even when 289bdc5f6a3SHajimu UMEMOTO * connection is established. So, this else clause is 290bdc5f6a3SHajimu UMEMOTO * added as workaround to return PF_LOCAL sockaddr. 291bdc5f6a3SHajimu UMEMOTO */ 2920d9ce3a1SRobert Watson sa = &sun_noname; 293bdc5f6a3SHajimu UMEMOTO } 2940d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 2950d9ce3a1SRobert Watson UNP_UNLOCK(); 296e5aeaa0cSDag-Erling Smørgrav return (0); 297a29f300eSGarrett Wollman } 298a29f300eSGarrett Wollman 299a29f300eSGarrett Wollman static int 300a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 301a29f300eSGarrett Wollman { 30240f2ac28SRobert Watson struct unpcb *unp; 303a29f300eSGarrett Wollman struct socket *so2; 3046aef685fSBrian Feldman u_long newhiwat; 305a29f300eSGarrett Wollman 30640f2ac28SRobert Watson unp = sotounpcb(so); 3074d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_rcvd: unp == NULL")); 3084d4b555eSRobert Watson UNP_LOCK(); 309df8bae1dSRodney W. Grimes switch (so->so_type) { 310df8bae1dSRodney W. Grimes case SOCK_DGRAM: 311a29f300eSGarrett Wollman panic("uipc_rcvd DGRAM?"); 312df8bae1dSRodney W. Grimes /*NOTREACHED*/ 313df8bae1dSRodney W. Grimes 314df8bae1dSRodney W. Grimes case SOCK_STREAM: 315fc3fcacfSRobert Watson if (unp->unp_conn == NULL) 316df8bae1dSRodney W. Grimes break; 317df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 318c9f69064SRobert Watson SOCKBUF_LOCK(&so2->so_snd); 319c9f69064SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 320df8bae1dSRodney W. Grimes /* 321df8bae1dSRodney W. Grimes * Adjust backpressure on sender 322df8bae1dSRodney W. Grimes * and wakeup any waiting to write. 323df8bae1dSRodney W. Grimes */ 324ff8b0106SBrian Feldman so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt; 325ff8b0106SBrian Feldman unp->unp_mbcnt = so->so_rcv.sb_mbcnt; 3266aef685fSBrian Feldman newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - 3276aef685fSBrian Feldman so->so_rcv.sb_cc; 328f535380cSDon Lewis (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat, 3296aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 330ff8b0106SBrian Feldman unp->unp_cc = so->so_rcv.sb_cc; 331c9f69064SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 3321e4d7da7SRobert Watson sowwakeup_locked(so2); 333df8bae1dSRodney W. Grimes break; 334df8bae1dSRodney W. Grimes 335df8bae1dSRodney W. Grimes default: 336a29f300eSGarrett Wollman panic("uipc_rcvd unknown socktype"); 337df8bae1dSRodney W. Grimes } 3380d9ce3a1SRobert Watson UNP_UNLOCK(); 339e5aeaa0cSDag-Erling Smørgrav return (0); 340a29f300eSGarrett Wollman } 341df8bae1dSRodney W. Grimes 342a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 343a29f300eSGarrett Wollman 344a29f300eSGarrett Wollman static int 34557bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 346b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 347a29f300eSGarrett Wollman { 348a29f300eSGarrett Wollman int error = 0; 34940f2ac28SRobert Watson struct unpcb *unp; 350a29f300eSGarrett Wollman struct socket *so2; 3516aef685fSBrian Feldman u_long newhiwat; 352a29f300eSGarrett Wollman 35340f2ac28SRobert Watson unp = sotounpcb(so); 3544d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_send: unp == NULL")); 355a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 356a29f300eSGarrett Wollman error = EOPNOTSUPP; 357a29f300eSGarrett Wollman goto release; 358a29f300eSGarrett Wollman } 359a29f300eSGarrett Wollman 360fc3fcacfSRobert Watson if (control != NULL && (error = unp_internalize(&control, td))) 361a29f300eSGarrett Wollman goto release; 362df8bae1dSRodney W. Grimes 3630d9ce3a1SRobert Watson UNP_LOCK(); 364a29f300eSGarrett Wollman switch (so->so_type) { 365a29f300eSGarrett Wollman case SOCK_DGRAM: 366a29f300eSGarrett Wollman { 367e7dd9a10SRobert Watson const struct sockaddr *from; 368df8bae1dSRodney W. Grimes 369fc3fcacfSRobert Watson if (nam != NULL) { 370fc3fcacfSRobert Watson if (unp->unp_conn != NULL) { 371df8bae1dSRodney W. Grimes error = EISCONN; 372df8bae1dSRodney W. Grimes break; 373df8bae1dSRodney W. Grimes } 374b40ce416SJulian Elischer error = unp_connect(so, nam, td); 375df8bae1dSRodney W. Grimes if (error) 376df8bae1dSRodney W. Grimes break; 377df8bae1dSRodney W. Grimes } else { 378fc3fcacfSRobert Watson if (unp->unp_conn == NULL) { 379df8bae1dSRodney W. Grimes error = ENOTCONN; 380df8bae1dSRodney W. Grimes break; 381df8bae1dSRodney W. Grimes } 382df8bae1dSRodney W. Grimes } 383df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 384fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 38557bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 386df8bae1dSRodney W. Grimes else 387df8bae1dSRodney W. Grimes from = &sun_noname; 3886a2989fdSMatthew N. Dodd if (unp->unp_conn->unp_flags & UNP_WANTCRED) 3896a2989fdSMatthew N. Dodd control = unp_addsockcred(td, control); 390a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 391a34b7046SRobert Watson if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) { 3921e4d7da7SRobert Watson sorwakeup_locked(so2); 393fc3fcacfSRobert Watson m = NULL; 394fc3fcacfSRobert Watson control = NULL; 395e5aeaa0cSDag-Erling Smørgrav } else { 396a34b7046SRobert Watson SOCKBUF_UNLOCK(&so2->so_rcv); 397df8bae1dSRodney W. Grimes error = ENOBUFS; 398e5aeaa0cSDag-Erling Smørgrav } 399fc3fcacfSRobert Watson if (nam != NULL) 400df8bae1dSRodney W. Grimes unp_disconnect(unp); 401df8bae1dSRodney W. Grimes break; 402df8bae1dSRodney W. Grimes } 403df8bae1dSRodney W. Grimes 404df8bae1dSRodney W. Grimes case SOCK_STREAM: 4056b8fda4dSGarrett Wollman /* Connect if not connected yet. */ 4066b8fda4dSGarrett Wollman /* 4076b8fda4dSGarrett Wollman * Note: A better implementation would complain 408402cc72dSDavid Greenman * if not equal to the peer's address. 4096b8fda4dSGarrett Wollman */ 410402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 411fc3fcacfSRobert Watson if (nam != NULL) { 412b40ce416SJulian Elischer error = unp_connect(so, nam, td); 413402cc72dSDavid Greenman if (error) 4146b8fda4dSGarrett Wollman break; /* XXX */ 415402cc72dSDavid Greenman } else { 416402cc72dSDavid Greenman error = ENOTCONN; 417402cc72dSDavid Greenman break; 418402cc72dSDavid Greenman } 419402cc72dSDavid Greenman } 420402cc72dSDavid Greenman 4217abe2ac2SAlan Cox SOCKBUF_LOCK(&so->so_snd); 422c0b99ffaSRobert Watson if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 4237abe2ac2SAlan Cox SOCKBUF_UNLOCK(&so->so_snd); 424df8bae1dSRodney W. Grimes error = EPIPE; 425df8bae1dSRodney W. Grimes break; 426df8bae1dSRodney W. Grimes } 427fc3fcacfSRobert Watson if (unp->unp_conn == NULL) 428a29f300eSGarrett Wollman panic("uipc_send connected but no connection?"); 429df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 430a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 4316a2989fdSMatthew N. Dodd if (unp->unp_conn->unp_flags & UNP_WANTCRED) { 4326a2989fdSMatthew N. Dodd /* 4336a2989fdSMatthew N. Dodd * Credentials are passed only once on 4346a2989fdSMatthew N. Dodd * SOCK_STREAM. 4356a2989fdSMatthew N. Dodd */ 4366a2989fdSMatthew N. Dodd unp->unp_conn->unp_flags &= ~UNP_WANTCRED; 4376a2989fdSMatthew N. Dodd control = unp_addsockcred(td, control); 4386a2989fdSMatthew N. Dodd } 439df8bae1dSRodney W. Grimes /* 440df8bae1dSRodney W. Grimes * Send to paired receive port, and then reduce 441df8bae1dSRodney W. Grimes * send buffer hiwater marks to maintain backpressure. 442df8bae1dSRodney W. Grimes * Wake up readers. 443df8bae1dSRodney W. Grimes */ 444fc3fcacfSRobert Watson if (control != NULL) { 445a34b7046SRobert Watson if (sbappendcontrol_locked(&so2->so_rcv, m, control)) 446fc3fcacfSRobert Watson control = NULL; 447e5aeaa0cSDag-Erling Smørgrav } else { 448a34b7046SRobert Watson sbappend_locked(&so2->so_rcv, m); 449e5aeaa0cSDag-Erling Smørgrav } 450ff8b0106SBrian Feldman so->so_snd.sb_mbmax -= 451ff8b0106SBrian Feldman so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt; 452ff8b0106SBrian Feldman unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt; 4536aef685fSBrian Feldman newhiwat = so->so_snd.sb_hiwat - 4546aef685fSBrian Feldman (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc); 455f535380cSDon Lewis (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat, 4566aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 4577abe2ac2SAlan Cox SOCKBUF_UNLOCK(&so->so_snd); 458ff8b0106SBrian Feldman unp->unp_conn->unp_cc = so2->so_rcv.sb_cc; 4591e4d7da7SRobert Watson sorwakeup_locked(so2); 460fc3fcacfSRobert Watson m = NULL; 461df8bae1dSRodney W. Grimes break; 462df8bae1dSRodney W. Grimes 463df8bae1dSRodney W. Grimes default: 464a29f300eSGarrett Wollman panic("uipc_send unknown socktype"); 465df8bae1dSRodney W. Grimes } 466a29f300eSGarrett Wollman 4676b8fda4dSGarrett Wollman /* 4686b8fda4dSGarrett Wollman * SEND_EOF is equivalent to a SEND followed by 4696b8fda4dSGarrett Wollman * a SHUTDOWN. 4706b8fda4dSGarrett Wollman */ 471a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 4726b8fda4dSGarrett Wollman socantsendmore(so); 4736b8fda4dSGarrett Wollman unp_shutdown(unp); 4746b8fda4dSGarrett Wollman } 4750d9ce3a1SRobert Watson UNP_UNLOCK(); 476df8bae1dSRodney W. Grimes 477fc3fcacfSRobert Watson if (control != NULL && error != 0) 478bd508d39SDon Lewis unp_dispose(control); 479bd508d39SDon Lewis 480a29f300eSGarrett Wollman release: 481fc3fcacfSRobert Watson if (control != NULL) 482a29f300eSGarrett Wollman m_freem(control); 483fc3fcacfSRobert Watson if (m != NULL) 484a29f300eSGarrett Wollman m_freem(m); 485e5aeaa0cSDag-Erling Smørgrav return (error); 486a29f300eSGarrett Wollman } 487df8bae1dSRodney W. Grimes 488a29f300eSGarrett Wollman static int 489a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 490a29f300eSGarrett Wollman { 49140f2ac28SRobert Watson struct unpcb *unp; 492a29f300eSGarrett Wollman struct socket *so2; 493a29f300eSGarrett Wollman 49440f2ac28SRobert Watson unp = sotounpcb(so); 4954d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sense: unp == NULL")); 4964d4b555eSRobert Watson UNP_LOCK(); 497a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 498fc3fcacfSRobert Watson if (so->so_type == SOCK_STREAM && unp->unp_conn != NULL) { 499df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 500a29f300eSGarrett Wollman sb->st_blksize += so2->so_rcv.sb_cc; 501df8bae1dSRodney W. Grimes } 502f3732fd1SPoul-Henning Kamp sb->st_dev = NODEV; 503df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 5046f782c46SJeffrey Hsu unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 505a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 5060d9ce3a1SRobert Watson UNP_UNLOCK(); 507df8bae1dSRodney W. Grimes return (0); 508a29f300eSGarrett Wollman } 509df8bae1dSRodney W. Grimes 510a29f300eSGarrett Wollman static int 511a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 512a29f300eSGarrett Wollman { 51340f2ac28SRobert Watson struct unpcb *unp; 514df8bae1dSRodney W. Grimes 51540f2ac28SRobert Watson unp = sotounpcb(so); 5164d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_shutdown: unp == NULL")); 5174d4b555eSRobert Watson UNP_LOCK(); 518a29f300eSGarrett Wollman socantsendmore(so); 519a29f300eSGarrett Wollman unp_shutdown(unp); 5200d9ce3a1SRobert Watson UNP_UNLOCK(); 521e5aeaa0cSDag-Erling Smørgrav return (0); 522a29f300eSGarrett Wollman } 523df8bae1dSRodney W. Grimes 524a29f300eSGarrett Wollman static int 52557bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 526a29f300eSGarrett Wollman { 52740f2ac28SRobert Watson struct unpcb *unp; 5280d9ce3a1SRobert Watson const struct sockaddr *sa; 529a29f300eSGarrett Wollman 5304d4b555eSRobert Watson unp = sotounpcb(so); 5314d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL")); 5320d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 5330d9ce3a1SRobert Watson UNP_LOCK(); 534fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 5350d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_addr; 53683f3198bSThomas Moestl else 5370d9ce3a1SRobert Watson sa = &sun_noname; 5380d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 5390d9ce3a1SRobert Watson UNP_UNLOCK(); 540e5aeaa0cSDag-Erling Smørgrav return (0); 541df8bae1dSRodney W. Grimes } 542a29f300eSGarrett Wollman 543a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = { 544756d52a1SPoul-Henning Kamp .pru_abort = uipc_abort, 545756d52a1SPoul-Henning Kamp .pru_accept = uipc_accept, 546756d52a1SPoul-Henning Kamp .pru_attach = uipc_attach, 547756d52a1SPoul-Henning Kamp .pru_bind = uipc_bind, 548756d52a1SPoul-Henning Kamp .pru_connect = uipc_connect, 549756d52a1SPoul-Henning Kamp .pru_connect2 = uipc_connect2, 550756d52a1SPoul-Henning Kamp .pru_detach = uipc_detach, 551756d52a1SPoul-Henning Kamp .pru_disconnect = uipc_disconnect, 552756d52a1SPoul-Henning Kamp .pru_listen = uipc_listen, 553756d52a1SPoul-Henning Kamp .pru_peeraddr = uipc_peeraddr, 554756d52a1SPoul-Henning Kamp .pru_rcvd = uipc_rcvd, 555756d52a1SPoul-Henning Kamp .pru_send = uipc_send, 556756d52a1SPoul-Henning Kamp .pru_sense = uipc_sense, 557756d52a1SPoul-Henning Kamp .pru_shutdown = uipc_shutdown, 558756d52a1SPoul-Henning Kamp .pru_sockaddr = uipc_sockaddr, 559756d52a1SPoul-Henning Kamp .pru_sosend = sosend, 560756d52a1SPoul-Henning Kamp .pru_soreceive = soreceive, 561756d52a1SPoul-Henning Kamp .pru_sopoll = sopoll, 562a29f300eSGarrett Wollman }; 563df8bae1dSRodney W. Grimes 5640c1bb4fbSDima Dorfman int 565892af6b9SRobert Watson uipc_ctloutput(struct socket *so, struct sockopt *sopt) 5660c1bb4fbSDima Dorfman { 56740f2ac28SRobert Watson struct unpcb *unp; 5680d9ce3a1SRobert Watson struct xucred xu; 5696a2989fdSMatthew N. Dodd int error, optval; 5706a2989fdSMatthew N. Dodd 57196a041b5SMatthew N. Dodd if (sopt->sopt_level != 0) 57296a041b5SMatthew N. Dodd return (EINVAL); 57396a041b5SMatthew N. Dodd 5746a2989fdSMatthew N. Dodd unp = sotounpcb(so); 5754d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL")); 5764d4b555eSRobert Watson UNP_LOCK(); 5776a2989fdSMatthew N. Dodd error = 0; 5780c1bb4fbSDima Dorfman switch (sopt->sopt_dir) { 5790c1bb4fbSDima Dorfman case SOPT_GET: 5800c1bb4fbSDima Dorfman switch (sopt->sopt_name) { 5810c1bb4fbSDima Dorfman case LOCAL_PEERCRED: 5820c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC) 5830d9ce3a1SRobert Watson xu = unp->unp_peercred; 5840c1bb4fbSDima Dorfman else { 5850c1bb4fbSDima Dorfman if (so->so_type == SOCK_STREAM) 5860c1bb4fbSDima Dorfman error = ENOTCONN; 5870c1bb4fbSDima Dorfman else 5880c1bb4fbSDima Dorfman error = EINVAL; 5890c1bb4fbSDima Dorfman } 5900d9ce3a1SRobert Watson if (error == 0) 5910d9ce3a1SRobert Watson error = sooptcopyout(sopt, &xu, sizeof(xu)); 5920c1bb4fbSDima Dorfman break; 5936a2989fdSMatthew N. Dodd case LOCAL_CREDS: 5946a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0; 5956a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 5966a2989fdSMatthew N. Dodd break; 5976a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 5986a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0; 5996a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 6006a2989fdSMatthew N. Dodd break; 6010c1bb4fbSDima Dorfman default: 6020c1bb4fbSDima Dorfman error = EOPNOTSUPP; 6030c1bb4fbSDima Dorfman break; 6040c1bb4fbSDima Dorfman } 6050c1bb4fbSDima Dorfman break; 6060c1bb4fbSDima Dorfman case SOPT_SET: 6076a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 6086a2989fdSMatthew N. Dodd case LOCAL_CREDS: 6096a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 6106a2989fdSMatthew N. Dodd error = sooptcopyin(sopt, &optval, sizeof(optval), 6116a2989fdSMatthew N. Dodd sizeof(optval)); 6126a2989fdSMatthew N. Dodd if (error) 6136a2989fdSMatthew N. Dodd break; 6146a2989fdSMatthew N. Dodd 6156a2989fdSMatthew N. Dodd #define OPTSET(bit) \ 6166a2989fdSMatthew N. Dodd if (optval) \ 6176a2989fdSMatthew N. Dodd unp->unp_flags |= bit; \ 6186a2989fdSMatthew N. Dodd else \ 6196a2989fdSMatthew N. Dodd unp->unp_flags &= ~bit; 6206a2989fdSMatthew N. Dodd 6216a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 6226a2989fdSMatthew N. Dodd case LOCAL_CREDS: 6236a2989fdSMatthew N. Dodd OPTSET(UNP_WANTCRED); 6246a2989fdSMatthew N. Dodd break; 6256a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 6266a2989fdSMatthew N. Dodd OPTSET(UNP_CONNWAIT); 6276a2989fdSMatthew N. Dodd break; 6286a2989fdSMatthew N. Dodd default: 6296a2989fdSMatthew N. Dodd break; 6306a2989fdSMatthew N. Dodd } 6316a2989fdSMatthew N. Dodd break; 6326a2989fdSMatthew N. Dodd #undef OPTSET 6336a2989fdSMatthew N. Dodd default: 6346a2989fdSMatthew N. Dodd error = ENOPROTOOPT; 6356a2989fdSMatthew N. Dodd break; 6366a2989fdSMatthew N. Dodd } 637abb886faSMatthew N. Dodd break; 6380c1bb4fbSDima Dorfman default: 6390c1bb4fbSDima Dorfman error = EOPNOTSUPP; 6400c1bb4fbSDima Dorfman break; 6410c1bb4fbSDima Dorfman } 6426a2989fdSMatthew N. Dodd UNP_UNLOCK(); 6430c1bb4fbSDima Dorfman return (error); 6440c1bb4fbSDima Dorfman } 6450c1bb4fbSDima Dorfman 646df8bae1dSRodney W. Grimes /* 647df8bae1dSRodney W. Grimes * Both send and receive buffers are allocated PIPSIZ bytes of buffering 648df8bae1dSRodney W. Grimes * for stream sockets, although the total for sender and receiver is 649df8bae1dSRodney W. Grimes * actually only PIPSIZ. 650df8bae1dSRodney W. Grimes * Datagram sockets really use the sendspace as the maximum datagram size, 651df8bae1dSRodney W. Grimes * and don't really want to reserve the sendspace. Their recvspace should 652df8bae1dSRodney W. Grimes * be large enough for at least one max-size datagram plus address. 653df8bae1dSRodney W. Grimes */ 6545dce41c5SJohn Dyson #ifndef PIPSIZ 6555dce41c5SJohn Dyson #define PIPSIZ 8192 6565dce41c5SJohn Dyson #endif 657f708ef1bSPoul-Henning Kamp static u_long unpst_sendspace = PIPSIZ; 658f708ef1bSPoul-Henning Kamp static u_long unpst_recvspace = PIPSIZ; 659f708ef1bSPoul-Henning Kamp static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 660f708ef1bSPoul-Henning Kamp static u_long unpdg_recvspace = 4*1024; 661df8bae1dSRodney W. Grimes 662f708ef1bSPoul-Henning Kamp static int unp_rights; /* file descriptors in flight */ 663df8bae1dSRodney W. Grimes 664ce02431fSDoug Rabson SYSCTL_DECL(_net_local_stream); 665e59898ffSMaxime Henrion SYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 666639acc13SGarrett Wollman &unpst_sendspace, 0, ""); 667e59898ffSMaxime Henrion SYSCTL_ULONG(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 668639acc13SGarrett Wollman &unpst_recvspace, 0, ""); 669ce02431fSDoug Rabson SYSCTL_DECL(_net_local_dgram); 670e59898ffSMaxime Henrion SYSCTL_ULONG(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 671639acc13SGarrett Wollman &unpdg_sendspace, 0, ""); 672e59898ffSMaxime Henrion SYSCTL_ULONG(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 673639acc13SGarrett Wollman &unpdg_recvspace, 0, ""); 674ce02431fSDoug Rabson SYSCTL_DECL(_net_local); 675639acc13SGarrett Wollman SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); 676639acc13SGarrett Wollman 677f708ef1bSPoul-Henning Kamp static int 678892af6b9SRobert Watson unp_attach(struct socket *so) 679df8bae1dSRodney W. Grimes { 680892af6b9SRobert Watson struct unpcb *unp; 681df8bae1dSRodney W. Grimes int error; 682df8bae1dSRodney W. Grimes 6834d4b555eSRobert Watson KASSERT(so->so_pcb == NULL, ("unp_attach: so_pcb != NULL")); 684df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 685df8bae1dSRodney W. Grimes switch (so->so_type) { 686df8bae1dSRodney W. Grimes 687df8bae1dSRodney W. Grimes case SOCK_STREAM: 688df8bae1dSRodney W. Grimes error = soreserve(so, unpst_sendspace, unpst_recvspace); 689df8bae1dSRodney W. Grimes break; 690df8bae1dSRodney W. Grimes 691df8bae1dSRodney W. Grimes case SOCK_DGRAM: 692df8bae1dSRodney W. Grimes error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 693df8bae1dSRodney W. Grimes break; 694df8bae1dSRodney W. Grimes 695df8bae1dSRodney W. Grimes default: 696df8bae1dSRodney W. Grimes panic("unp_attach"); 697df8bae1dSRodney W. Grimes } 698df8bae1dSRodney W. Grimes if (error) 699df8bae1dSRodney W. Grimes return (error); 700df8bae1dSRodney W. Grimes } 701d664e4faSRobert Watson unp = uma_zalloc(unp_zone, M_WAITOK | M_ZERO); 70257bf258eSGarrett Wollman if (unp == NULL) 703df8bae1dSRodney W. Grimes return (ENOBUFS); 70498271db4SGarrett Wollman LIST_INIT(&unp->unp_refs); 705df8bae1dSRodney W. Grimes unp->unp_socket = so; 7067301cf23SRobert Watson so->so_pcb = unp; 7070d9ce3a1SRobert Watson 7080d9ce3a1SRobert Watson UNP_LOCK(); 7090d9ce3a1SRobert Watson unp->unp_gencnt = ++unp_gencnt; 7100d9ce3a1SRobert Watson unp_count++; 71198271db4SGarrett Wollman LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead 71298271db4SGarrett Wollman : &unp_shead, unp, unp_link); 7130d9ce3a1SRobert Watson UNP_UNLOCK(); 7140d9ce3a1SRobert Watson 715df8bae1dSRodney W. Grimes return (0); 716df8bae1dSRodney W. Grimes } 717df8bae1dSRodney W. Grimes 718f708ef1bSPoul-Henning Kamp static void 719892af6b9SRobert Watson unp_detach(struct unpcb *unp) 720df8bae1dSRodney W. Grimes { 7210d9ce3a1SRobert Watson struct vnode *vp; 722a0ec558aSRobert Watson int local_unp_rights; 7230d9ce3a1SRobert Watson 7240d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 7250d9ce3a1SRobert Watson 72698271db4SGarrett Wollman LIST_REMOVE(unp, unp_link); 72798271db4SGarrett Wollman unp->unp_gencnt = ++unp_gencnt; 72898271db4SGarrett Wollman --unp_count; 7290d9ce3a1SRobert Watson if ((vp = unp->unp_vnode) != NULL) { 7300d9ce3a1SRobert Watson /* 7310d9ce3a1SRobert Watson * XXXRW: should v_socket be frobbed only while holding 7320d9ce3a1SRobert Watson * Giant? 7330d9ce3a1SRobert Watson */ 734fc3fcacfSRobert Watson unp->unp_vnode->v_socket = NULL; 735fc3fcacfSRobert Watson unp->unp_vnode = NULL; 736df8bae1dSRodney W. Grimes } 737fc3fcacfSRobert Watson if (unp->unp_conn != NULL) 738df8bae1dSRodney W. Grimes unp_disconnect(unp); 7390d9ce3a1SRobert Watson while (!LIST_EMPTY(&unp->unp_refs)) { 7400d9ce3a1SRobert Watson struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 7410d9ce3a1SRobert Watson unp_drop(ref, ECONNRESET); 7420d9ce3a1SRobert Watson } 743df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 744fc3fcacfSRobert Watson unp->unp_socket->so_pcb = NULL; 745a0ec558aSRobert Watson local_unp_rights = unp_rights; 746a5993a97SRobert Watson UNP_UNLOCK(); 747fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 74857bf258eSGarrett Wollman FREE(unp->unp_addr, M_SONAME); 7499e9d298aSJeff Roberson uma_zfree(unp_zone, unp); 7500d9ce3a1SRobert Watson if (vp) { 751033eb86eSJeff Roberson int vfslocked; 752033eb86eSJeff Roberson 753033eb86eSJeff Roberson vfslocked = VFS_LOCK_GIANT(vp->v_mount); 7540d9ce3a1SRobert Watson vrele(vp); 755033eb86eSJeff Roberson VFS_UNLOCK_GIANT(vfslocked); 7560d9ce3a1SRobert Watson } 757a0ec558aSRobert Watson if (local_unp_rights) 758a0ec558aSRobert Watson taskqueue_enqueue(taskqueue_thread, &unp_gc_task); 759df8bae1dSRodney W. Grimes } 760df8bae1dSRodney W. Grimes 761f708ef1bSPoul-Henning Kamp static int 762892af6b9SRobert Watson unp_bind(struct unpcb *unp, struct sockaddr *nam, struct thread *td) 763df8bae1dSRodney W. Grimes { 76457bf258eSGarrett Wollman struct sockaddr_un *soun = (struct sockaddr_un *)nam; 765f2a2857bSKirk McKusick struct vnode *vp; 766f2a2857bSKirk McKusick struct mount *mp; 767df8bae1dSRodney W. Grimes struct vattr vattr; 76857bf258eSGarrett Wollman int error, namelen; 769df8bae1dSRodney W. Grimes struct nameidata nd; 7708f364875SJulian Elischer char *buf; 771df8bae1dSRodney W. Grimes 77240f2ac28SRobert Watson UNP_LOCK_ASSERT(); 77340f2ac28SRobert Watson 7740d9ce3a1SRobert Watson /* 7750d9ce3a1SRobert Watson * XXXRW: This test-and-set of unp_vnode is non-atomic; the 7760d9ce3a1SRobert Watson * unlocked read here is fine, but the value of unp_vnode needs 7770d9ce3a1SRobert Watson * to be tested again after we do all the lookups to see if the 7780d9ce3a1SRobert Watson * pcb is still unbound? 7790d9ce3a1SRobert Watson */ 780df8bae1dSRodney W. Grimes if (unp->unp_vnode != NULL) 781df8bae1dSRodney W. Grimes return (EINVAL); 78255c85568SRobert Drehmel 78357bf258eSGarrett Wollman namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 78457bf258eSGarrett Wollman if (namelen <= 0) 785e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 78655c85568SRobert Drehmel 78740f2ac28SRobert Watson UNP_UNLOCK(); 78840f2ac28SRobert Watson 789a163d034SWarner Losh buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 79055c85568SRobert Drehmel strlcpy(buf, soun->sun_path, namelen + 1); 79155c85568SRobert Drehmel 7920d9ce3a1SRobert Watson mtx_lock(&Giant); 793f2a2857bSKirk McKusick restart: 7940d9ce3a1SRobert Watson mtx_assert(&Giant, MA_OWNED); 795b65f6f6bSRobert Watson NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME, UIO_SYSSPACE, 796b40ce416SJulian Elischer buf, td); 797df8bae1dSRodney W. Grimes /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 798797f2d22SPoul-Henning Kamp error = namei(&nd); 7990d9ce3a1SRobert Watson if (error) 8000d9ce3a1SRobert Watson goto done; 801df8bae1dSRodney W. Grimes vp = nd.ni_vp; 802f2a2857bSKirk McKusick if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 803762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 804df8bae1dSRodney W. Grimes if (nd.ni_dvp == vp) 805df8bae1dSRodney W. Grimes vrele(nd.ni_dvp); 806df8bae1dSRodney W. Grimes else 807df8bae1dSRodney W. Grimes vput(nd.ni_dvp); 808f2a2857bSKirk McKusick if (vp != NULL) { 809df8bae1dSRodney W. Grimes vrele(vp); 8100d9ce3a1SRobert Watson error = EADDRINUSE; 8110d9ce3a1SRobert Watson goto done; 812df8bae1dSRodney W. Grimes } 8138f364875SJulian Elischer error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 8140d9ce3a1SRobert Watson if (error) 8150d9ce3a1SRobert Watson goto done; 816f2a2857bSKirk McKusick goto restart; 817f2a2857bSKirk McKusick } 818df8bae1dSRodney W. Grimes VATTR_NULL(&vattr); 819df8bae1dSRodney W. Grimes vattr.va_type = VSOCK; 820b40ce416SJulian Elischer vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 8216ea48a90SRobert Watson #ifdef MAC 8226ea48a90SRobert Watson error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 8236ea48a90SRobert Watson &vattr); 8246151efaaSRobert Watson #endif 8256ea48a90SRobert Watson if (error == 0) { 826a854ed98SJohn Baldwin VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE); 8277be2d300SMike Smith error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 8286ea48a90SRobert Watson } 829762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 8307be2d300SMike Smith vput(nd.ni_dvp); 831c364c823SRobert Watson if (error) { 832c364c823SRobert Watson vn_finished_write(mp); 8330d9ce3a1SRobert Watson goto done; 834c364c823SRobert Watson } 835df8bae1dSRodney W. Grimes vp = nd.ni_vp; 8360d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_bind"); 8370d9ce3a1SRobert Watson soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 8380d9ce3a1SRobert Watson UNP_LOCK(); 839df8bae1dSRodney W. Grimes vp->v_socket = unp->unp_socket; 840df8bae1dSRodney W. Grimes unp->unp_vnode = vp; 8410d9ce3a1SRobert Watson unp->unp_addr = soun; 8420d9ce3a1SRobert Watson UNP_UNLOCK(); 843b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 844f2a2857bSKirk McKusick vn_finished_write(mp); 8450d9ce3a1SRobert Watson done: 8460d9ce3a1SRobert Watson mtx_unlock(&Giant); 8478f364875SJulian Elischer free(buf, M_TEMP); 84840f2ac28SRobert Watson UNP_LOCK(); 8490d9ce3a1SRobert Watson return (error); 850df8bae1dSRodney W. Grimes } 851df8bae1dSRodney W. Grimes 852f708ef1bSPoul-Henning Kamp static int 853892af6b9SRobert Watson unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 854df8bae1dSRodney W. Grimes { 855892af6b9SRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam; 856892af6b9SRobert Watson struct vnode *vp; 857892af6b9SRobert Watson struct socket *so2, *so3; 858b295bdcdSRobert Watson struct unpcb *unp, *unp2, *unp3; 85957bf258eSGarrett Wollman int error, len; 860df8bae1dSRodney W. Grimes struct nameidata nd; 86157bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 8620d9ce3a1SRobert Watson struct sockaddr *sa; 8630d9ce3a1SRobert Watson 8640d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 865df8bae1dSRodney W. Grimes 8664d4b555eSRobert Watson unp = sotounpcb(so); 8674d4b555eSRobert Watson KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 86857bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 86957bf258eSGarrett Wollman if (len <= 0) 870e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 87155c85568SRobert Drehmel strlcpy(buf, soun->sun_path, len + 1); 8720d9ce3a1SRobert Watson UNP_UNLOCK(); 8730d9ce3a1SRobert Watson sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 8740d9ce3a1SRobert Watson mtx_lock(&Giant); 875b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td); 876797f2d22SPoul-Henning Kamp error = namei(&nd); 877797f2d22SPoul-Henning Kamp if (error) 8780d9ce3a1SRobert Watson vp = NULL; 8790d9ce3a1SRobert Watson else 880df8bae1dSRodney W. Grimes vp = nd.ni_vp; 8810d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_connect"); 882762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 8830d9ce3a1SRobert Watson if (error) 8840d9ce3a1SRobert Watson goto bad; 8850d9ce3a1SRobert Watson 886df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 887df8bae1dSRodney W. Grimes error = ENOTSOCK; 888df8bae1dSRodney W. Grimes goto bad; 889df8bae1dSRodney W. Grimes } 890a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 891797f2d22SPoul-Henning Kamp if (error) 892df8bae1dSRodney W. Grimes goto bad; 8932260c03dSRobert Watson mtx_unlock(&Giant); 8942260c03dSRobert Watson UNP_LOCK(); 895b295bdcdSRobert Watson unp = sotounpcb(so); 8964d4b555eSRobert Watson KASSERT(unp != NULL, ("unp_connect: unp == NULL")); 897df8bae1dSRodney W. Grimes so2 = vp->v_socket; 898fc3fcacfSRobert Watson if (so2 == NULL) { 899df8bae1dSRodney W. Grimes error = ECONNREFUSED; 9002260c03dSRobert Watson goto bad2; 901df8bae1dSRodney W. Grimes } 902df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 903df8bae1dSRodney W. Grimes error = EPROTOTYPE; 9042260c03dSRobert Watson goto bad2; 905df8bae1dSRodney W. Grimes } 906df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 9070d9ce3a1SRobert Watson if (so2->so_options & SO_ACCEPTCONN) { 9080d9ce3a1SRobert Watson /* 9090d9ce3a1SRobert Watson * NB: drop locks here so unp_attach is entered 9100d9ce3a1SRobert Watson * w/o locks; this avoids a recursive lock 9110d9ce3a1SRobert Watson * of the head and holding sleep locks across 9120d9ce3a1SRobert Watson * a (potentially) blocking malloc. 9130d9ce3a1SRobert Watson */ 9140d9ce3a1SRobert Watson UNP_UNLOCK(); 9150d9ce3a1SRobert Watson so3 = sonewconn(so2, 0); 9160d9ce3a1SRobert Watson UNP_LOCK(); 9170d9ce3a1SRobert Watson } else 9180d9ce3a1SRobert Watson so3 = NULL; 9190d9ce3a1SRobert Watson if (so3 == NULL) { 920df8bae1dSRodney W. Grimes error = ECONNREFUSED; 9210d9ce3a1SRobert Watson goto bad2; 922df8bae1dSRodney W. Grimes } 9230c1bb4fbSDima Dorfman unp = sotounpcb(so); 924df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 925df8bae1dSRodney W. Grimes unp3 = sotounpcb(so3); 9260d9ce3a1SRobert Watson if (unp2->unp_addr != NULL) { 9270d9ce3a1SRobert Watson bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 9280d9ce3a1SRobert Watson unp3->unp_addr = (struct sockaddr_un *) sa; 9290d9ce3a1SRobert Watson sa = NULL; 9300d9ce3a1SRobert Watson } 9310c1bb4fbSDima Dorfman /* 9320c1bb4fbSDima Dorfman * unp_peercred management: 9330c1bb4fbSDima Dorfman * 9340c1bb4fbSDima Dorfman * The connecter's (client's) credentials are copied 9350c1bb4fbSDima Dorfman * from its process structure at the time of connect() 9360c1bb4fbSDima Dorfman * (which is now). 9370c1bb4fbSDima Dorfman */ 938a854ed98SJohn Baldwin cru2x(td->td_ucred, &unp3->unp_peercred); 9390c1bb4fbSDima Dorfman unp3->unp_flags |= UNP_HAVEPC; 9400c1bb4fbSDima Dorfman /* 9410c1bb4fbSDima Dorfman * The receiver's (server's) credentials are copied 9420c1bb4fbSDima Dorfman * from the unp_peercred member of socket on which the 9430c1bb4fbSDima Dorfman * former called listen(); unp_listen() cached that 9440c1bb4fbSDima Dorfman * process's credentials at that time so we can use 9450c1bb4fbSDima Dorfman * them now. 9460c1bb4fbSDima Dorfman */ 9470c1bb4fbSDima Dorfman KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 9480c1bb4fbSDima Dorfman ("unp_connect: listener without cached peercred")); 9490c1bb4fbSDima Dorfman memcpy(&unp->unp_peercred, &unp2->unp_peercred, 9500c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 9510c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPC; 952335654d7SRobert Watson #ifdef MAC 953310e7cebSRobert Watson SOCK_LOCK(so); 954335654d7SRobert Watson mac_set_socket_peer_from_socket(so, so3); 955335654d7SRobert Watson mac_set_socket_peer_from_socket(so3, so); 956310e7cebSRobert Watson SOCK_UNLOCK(so); 957335654d7SRobert Watson #endif 9580c1bb4fbSDima Dorfman 959df8bae1dSRodney W. Grimes so2 = so3; 960df8bae1dSRodney W. Grimes } 9616a2989fdSMatthew N. Dodd error = unp_connect2(so, so2, PRU_CONNECT); 9620d9ce3a1SRobert Watson bad2: 9630d9ce3a1SRobert Watson UNP_UNLOCK(); 9640d9ce3a1SRobert Watson mtx_lock(&Giant); 965df8bae1dSRodney W. Grimes bad: 9660d9ce3a1SRobert Watson mtx_assert(&Giant, MA_OWNED); 9670d9ce3a1SRobert Watson if (vp != NULL) 968df8bae1dSRodney W. Grimes vput(vp); 9690d9ce3a1SRobert Watson mtx_unlock(&Giant); 9700d9ce3a1SRobert Watson free(sa, M_SONAME); 9710d9ce3a1SRobert Watson UNP_LOCK(); 972df8bae1dSRodney W. Grimes return (error); 973df8bae1dSRodney W. Grimes } 974df8bae1dSRodney W. Grimes 975db48c0d2SRobert Watson static int 9766a2989fdSMatthew N. Dodd unp_connect2(struct socket *so, struct socket *so2, int req) 977df8bae1dSRodney W. Grimes { 978892af6b9SRobert Watson struct unpcb *unp = sotounpcb(so); 979892af6b9SRobert Watson struct unpcb *unp2; 980df8bae1dSRodney W. Grimes 9810d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 9820d9ce3a1SRobert Watson 983df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 984df8bae1dSRodney W. Grimes return (EPROTOTYPE); 985df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 9864d4b555eSRobert Watson KASSERT(unp2 != NULL, ("unp_connect2: unp2 == NULL")); 987df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 988df8bae1dSRodney W. Grimes switch (so->so_type) { 989df8bae1dSRodney W. Grimes 990df8bae1dSRodney W. Grimes case SOCK_DGRAM: 99198271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 992df8bae1dSRodney W. Grimes soisconnected(so); 993df8bae1dSRodney W. Grimes break; 994df8bae1dSRodney W. Grimes 995df8bae1dSRodney W. Grimes case SOCK_STREAM: 996df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 9976a2989fdSMatthew N. Dodd if (req == PRU_CONNECT && 9986a2989fdSMatthew N. Dodd ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT)) 9996a2989fdSMatthew N. Dodd soisconnecting(so); 10006a2989fdSMatthew N. Dodd else 1001df8bae1dSRodney W. Grimes soisconnected(so); 1002df8bae1dSRodney W. Grimes soisconnected(so2); 1003df8bae1dSRodney W. Grimes break; 1004df8bae1dSRodney W. Grimes 1005df8bae1dSRodney W. Grimes default: 1006df8bae1dSRodney W. Grimes panic("unp_connect2"); 1007df8bae1dSRodney W. Grimes } 1008df8bae1dSRodney W. Grimes return (0); 1009df8bae1dSRodney W. Grimes } 1010df8bae1dSRodney W. Grimes 1011f708ef1bSPoul-Henning Kamp static void 1012892af6b9SRobert Watson unp_disconnect(struct unpcb *unp) 1013df8bae1dSRodney W. Grimes { 1014892af6b9SRobert Watson struct unpcb *unp2 = unp->unp_conn; 10151b2e3b4bSRobert Watson struct socket *so; 1016df8bae1dSRodney W. Grimes 10170d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 10180d9ce3a1SRobert Watson 1019fc3fcacfSRobert Watson if (unp2 == NULL) 1020df8bae1dSRodney W. Grimes return; 1021fc3fcacfSRobert Watson unp->unp_conn = NULL; 1022df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 1023df8bae1dSRodney W. Grimes case SOCK_DGRAM: 102498271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 10251b2e3b4bSRobert Watson so = unp->unp_socket; 10261b2e3b4bSRobert Watson SOCK_LOCK(so); 10271b2e3b4bSRobert Watson so->so_state &= ~SS_ISCONNECTED; 10281b2e3b4bSRobert Watson SOCK_UNLOCK(so); 1029df8bae1dSRodney W. Grimes break; 1030df8bae1dSRodney W. Grimes 1031df8bae1dSRodney W. Grimes case SOCK_STREAM: 1032df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 1033fc3fcacfSRobert Watson unp2->unp_conn = NULL; 1034df8bae1dSRodney W. Grimes soisdisconnected(unp2->unp_socket); 1035df8bae1dSRodney W. Grimes break; 1036df8bae1dSRodney W. Grimes } 1037df8bae1dSRodney W. Grimes } 1038df8bae1dSRodney W. Grimes 1039df8bae1dSRodney W. Grimes #ifdef notdef 104026f9a767SRodney W. Grimes void 1041892af6b9SRobert Watson unp_abort(struct unpcb *unp) 1042df8bae1dSRodney W. Grimes { 1043df8bae1dSRodney W. Grimes 1044df8bae1dSRodney W. Grimes unp_detach(unp); 10454c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 1046df8bae1dSRodney W. Grimes } 1047df8bae1dSRodney W. Grimes #endif 1048df8bae1dSRodney W. Grimes 10490d9ce3a1SRobert Watson /* 10500d9ce3a1SRobert Watson * unp_pcblist() assumes that UNIX domain socket memory is never reclaimed 10510d9ce3a1SRobert Watson * by the zone (UMA_ZONE_NOFREE), and as such potentially stale pointers 10520d9ce3a1SRobert Watson * are safe to reference. It first scans the list of struct unpcb's to 10530d9ce3a1SRobert Watson * generate a pointer list, then it rescans its list one entry at a time to 10540d9ce3a1SRobert Watson * externalize and copyout. It checks the generation number to see if a 10550d9ce3a1SRobert Watson * struct unpcb has been reused, and will skip it if so. 10560d9ce3a1SRobert Watson */ 105798271db4SGarrett Wollman static int 105882d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 105998271db4SGarrett Wollman { 1060f5ef029eSPoul-Henning Kamp int error, i, n; 106198271db4SGarrett Wollman struct unpcb *unp, **unp_list; 106298271db4SGarrett Wollman unp_gen_t gencnt; 10638f364875SJulian Elischer struct xunpgen *xug; 106498271db4SGarrett Wollman struct unp_head *head; 10658f364875SJulian Elischer struct xunpcb *xu; 106698271db4SGarrett Wollman 1067a23d65bfSBruce Evans head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 106898271db4SGarrett Wollman 106998271db4SGarrett Wollman /* 107098271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 107198271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 107298271db4SGarrett Wollman */ 1073fc3fcacfSRobert Watson if (req->oldptr == NULL) { 107498271db4SGarrett Wollman n = unp_count; 10758f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug) 107698271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 1077e5aeaa0cSDag-Erling Smørgrav return (0); 107898271db4SGarrett Wollman } 107998271db4SGarrett Wollman 1080fc3fcacfSRobert Watson if (req->newptr != NULL) 1081e5aeaa0cSDag-Erling Smørgrav return (EPERM); 108298271db4SGarrett Wollman 108398271db4SGarrett Wollman /* 108498271db4SGarrett Wollman * OK, now we're committed to doing something. 108598271db4SGarrett Wollman */ 1086a163d034SWarner Losh xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 10870d9ce3a1SRobert Watson UNP_LOCK(); 108898271db4SGarrett Wollman gencnt = unp_gencnt; 108998271db4SGarrett Wollman n = unp_count; 10900d9ce3a1SRobert Watson UNP_UNLOCK(); 109198271db4SGarrett Wollman 10928f364875SJulian Elischer xug->xug_len = sizeof *xug; 10938f364875SJulian Elischer xug->xug_count = n; 10948f364875SJulian Elischer xug->xug_gen = gencnt; 10958f364875SJulian Elischer xug->xug_sogen = so_gencnt; 10968f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 10978f364875SJulian Elischer if (error) { 10988f364875SJulian Elischer free(xug, M_TEMP); 1099e5aeaa0cSDag-Erling Smørgrav return (error); 11008f364875SJulian Elischer } 110198271db4SGarrett Wollman 1102a163d034SWarner Losh unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 110398271db4SGarrett Wollman 11040d9ce3a1SRobert Watson UNP_LOCK(); 11052e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 11062e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 11078a7d8cc6SRobert Watson if (unp->unp_gencnt <= gencnt) { 1108a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred, 11098a7d8cc6SRobert Watson unp->unp_socket->so_cred)) 11104787fd37SPaul Saab continue; 111198271db4SGarrett Wollman unp_list[i++] = unp; 111298271db4SGarrett Wollman } 11134787fd37SPaul Saab } 11140d9ce3a1SRobert Watson UNP_UNLOCK(); 111598271db4SGarrett Wollman n = i; /* in case we lost some during malloc */ 111698271db4SGarrett Wollman 111798271db4SGarrett Wollman error = 0; 1118fe2eee82SColin Percival xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO); 111998271db4SGarrett Wollman for (i = 0; i < n; i++) { 112098271db4SGarrett Wollman unp = unp_list[i]; 112198271db4SGarrett Wollman if (unp->unp_gencnt <= gencnt) { 11228f364875SJulian Elischer xu->xu_len = sizeof *xu; 11238f364875SJulian Elischer xu->xu_unpp = unp; 112498271db4SGarrett Wollman /* 112598271db4SGarrett Wollman * XXX - need more locking here to protect against 112698271db4SGarrett Wollman * connect/disconnect races for SMP. 112798271db4SGarrett Wollman */ 1128fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 11298f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr, 113098271db4SGarrett Wollman unp->unp_addr->sun_len); 1131fc3fcacfSRobert Watson if (unp->unp_conn != NULL && 1132fc3fcacfSRobert Watson unp->unp_conn->unp_addr != NULL) 113398271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 11348f364875SJulian Elischer &xu->xu_caddr, 113598271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 11368f364875SJulian Elischer bcopy(unp, &xu->xu_unp, sizeof *unp); 11378f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket); 11388f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu); 113998271db4SGarrett Wollman } 114098271db4SGarrett Wollman } 11418f364875SJulian Elischer free(xu, M_TEMP); 114298271db4SGarrett Wollman if (!error) { 114398271db4SGarrett Wollman /* 114498271db4SGarrett Wollman * Give the user an updated idea of our state. 114598271db4SGarrett Wollman * If the generation differs from what we told 114698271db4SGarrett Wollman * her before, she knows that something happened 114798271db4SGarrett Wollman * while we were processing this request, and it 114898271db4SGarrett Wollman * might be necessary to retry. 114998271db4SGarrett Wollman */ 11508f364875SJulian Elischer xug->xug_gen = unp_gencnt; 11518f364875SJulian Elischer xug->xug_sogen = so_gencnt; 11528f364875SJulian Elischer xug->xug_count = unp_count; 11538f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 115498271db4SGarrett Wollman } 115598271db4SGarrett Wollman free(unp_list, M_TEMP); 11568f364875SJulian Elischer free(xug, M_TEMP); 1157e5aeaa0cSDag-Erling Smørgrav return (error); 115898271db4SGarrett Wollman } 115998271db4SGarrett Wollman 116098271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 116198271db4SGarrett Wollman (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 116298271db4SGarrett Wollman "List of active local datagram sockets"); 116398271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 116498271db4SGarrett Wollman (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 116598271db4SGarrett Wollman "List of active local stream sockets"); 116698271db4SGarrett Wollman 1167f708ef1bSPoul-Henning Kamp static void 1168892af6b9SRobert Watson unp_shutdown(struct unpcb *unp) 1169df8bae1dSRodney W. Grimes { 1170df8bae1dSRodney W. Grimes struct socket *so; 1171df8bae1dSRodney W. Grimes 11720d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 11730d9ce3a1SRobert Watson 1174df8bae1dSRodney W. Grimes if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 1175df8bae1dSRodney W. Grimes (so = unp->unp_conn->unp_socket)) 1176df8bae1dSRodney W. Grimes socantrcvmore(so); 1177df8bae1dSRodney W. Grimes } 1178df8bae1dSRodney W. Grimes 1179f708ef1bSPoul-Henning Kamp static void 1180892af6b9SRobert Watson unp_drop(struct unpcb *unp, int errno) 1181df8bae1dSRodney W. Grimes { 1182df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 1183df8bae1dSRodney W. Grimes 11840d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 11850d9ce3a1SRobert Watson 1186df8bae1dSRodney W. Grimes so->so_error = errno; 1187df8bae1dSRodney W. Grimes unp_disconnect(unp); 1188df8bae1dSRodney W. Grimes } 1189df8bae1dSRodney W. Grimes 1190df8bae1dSRodney W. Grimes #ifdef notdef 119126f9a767SRodney W. Grimes void 1192892af6b9SRobert Watson unp_drain(void) 1193df8bae1dSRodney W. Grimes { 1194df8bae1dSRodney W. Grimes 1195df8bae1dSRodney W. Grimes } 1196df8bae1dSRodney W. Grimes #endif 1197df8bae1dSRodney W. Grimes 11982bc21ed9SDavid Malone static void 1199892af6b9SRobert Watson unp_freerights(struct file **rp, int fdcount) 1200df8bae1dSRodney W. Grimes { 12012bc21ed9SDavid Malone int i; 12022bc21ed9SDavid Malone struct file *fp; 1203df8bae1dSRodney W. Grimes 12042bc21ed9SDavid Malone for (i = 0; i < fdcount; i++) { 1205df8bae1dSRodney W. Grimes fp = *rp; 12068692c025SYoshinobu Inoue /* 12072bc21ed9SDavid Malone * zero the pointer before calling 12082bc21ed9SDavid Malone * unp_discard since it may end up 12092bc21ed9SDavid Malone * in unp_gc().. 1210d7dca903SRobert Watson * 1211d7dca903SRobert Watson * XXXRW: This is less true than it used to be. 12128692c025SYoshinobu Inoue */ 1213df8bae1dSRodney W. Grimes *rp++ = 0; 12148692c025SYoshinobu Inoue unp_discard(fp); 1215df8bae1dSRodney W. Grimes } 12162bc21ed9SDavid Malone } 12172bc21ed9SDavid Malone 12182bc21ed9SDavid Malone int 1219892af6b9SRobert Watson unp_externalize(struct mbuf *control, struct mbuf **controlp) 12202bc21ed9SDavid Malone { 12212bc21ed9SDavid Malone struct thread *td = curthread; /* XXX */ 12222bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 12232bc21ed9SDavid Malone int i; 12242bc21ed9SDavid Malone int *fdp; 12252bc21ed9SDavid Malone struct file **rp; 12262bc21ed9SDavid Malone struct file *fp; 12272bc21ed9SDavid Malone void *data; 12282bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 12292bc21ed9SDavid Malone int error, newfds; 12302bc21ed9SDavid Malone int f; 12312bc21ed9SDavid Malone u_int newlen; 12322bc21ed9SDavid Malone 12334c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 12344c5bc1caSRobert Watson 12352bc21ed9SDavid Malone error = 0; 12362bc21ed9SDavid Malone if (controlp != NULL) /* controlp == NULL => free control messages */ 12372bc21ed9SDavid Malone *controlp = NULL; 12382bc21ed9SDavid Malone 12392bc21ed9SDavid Malone while (cm != NULL) { 12402bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 12412bc21ed9SDavid Malone error = EINVAL; 12422bc21ed9SDavid Malone break; 12432bc21ed9SDavid Malone } 12442bc21ed9SDavid Malone 12452bc21ed9SDavid Malone data = CMSG_DATA(cm); 12462bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 12472bc21ed9SDavid Malone 12482bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET 12492bc21ed9SDavid Malone && cm->cmsg_type == SCM_RIGHTS) { 12502bc21ed9SDavid Malone newfds = datalen / sizeof(struct file *); 12512bc21ed9SDavid Malone rp = data; 12522bc21ed9SDavid Malone 1253e2f9a08bSOlivier Houchard /* If we're not outputting the descriptors free them. */ 12542bc21ed9SDavid Malone if (error || controlp == NULL) { 12552bc21ed9SDavid Malone unp_freerights(rp, newfds); 12562bc21ed9SDavid Malone goto next; 12572bc21ed9SDavid Malone } 1258426da3bcSAlfred Perlstein FILEDESC_LOCK(td->td_proc->p_fd); 12592bc21ed9SDavid Malone /* if the new FD's will not fit free them. */ 12602bc21ed9SDavid Malone if (!fdavail(td, newfds)) { 1261426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 12622bc21ed9SDavid Malone error = EMSGSIZE; 12632bc21ed9SDavid Malone unp_freerights(rp, newfds); 12642bc21ed9SDavid Malone goto next; 1265df8bae1dSRodney W. Grimes } 1266ed5b7817SJulian Elischer /* 12672bc21ed9SDavid Malone * now change each pointer to an fd in the global 12682bc21ed9SDavid Malone * table to an integer that is the index to the 12692bc21ed9SDavid Malone * local fd table entry that we set up to point 12702bc21ed9SDavid Malone * to the global one we are transferring. 1271ed5b7817SJulian Elischer */ 12722bc21ed9SDavid Malone newlen = newfds * sizeof(int); 12732bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 12742bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 12752bc21ed9SDavid Malone if (*controlp == NULL) { 1276426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 12772bc21ed9SDavid Malone error = E2BIG; 12782bc21ed9SDavid Malone unp_freerights(rp, newfds); 12792bc21ed9SDavid Malone goto next; 12802bc21ed9SDavid Malone } 12812bc21ed9SDavid Malone 12822bc21ed9SDavid Malone fdp = (int *) 12832bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1284df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 1285a6d4491cSDag-Erling Smørgrav if (fdalloc(td, 0, &f)) 12862bc21ed9SDavid Malone panic("unp_externalize fdalloc failed"); 12878692c025SYoshinobu Inoue fp = *rp++; 1288b40ce416SJulian Elischer td->td_proc->p_fd->fd_ofiles[f] = fp; 1289426da3bcSAlfred Perlstein FILE_LOCK(fp); 1290df8bae1dSRodney W. Grimes fp->f_msgcount--; 1291426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1292df8bae1dSRodney W. Grimes unp_rights--; 12938692c025SYoshinobu Inoue *fdp++ = f; 1294df8bae1dSRodney W. Grimes } 1295426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 12962bc21ed9SDavid Malone } else { /* We can just copy anything else across */ 12972bc21ed9SDavid Malone if (error || controlp == NULL) 12982bc21ed9SDavid Malone goto next; 12992bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, datalen, 13002bc21ed9SDavid Malone cm->cmsg_type, cm->cmsg_level); 13012bc21ed9SDavid Malone if (*controlp == NULL) { 13022bc21ed9SDavid Malone error = ENOBUFS; 13032bc21ed9SDavid Malone goto next; 13042bc21ed9SDavid Malone } 13052bc21ed9SDavid Malone bcopy(data, 13062bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 13072bc21ed9SDavid Malone datalen); 13082bc21ed9SDavid Malone } 13092bc21ed9SDavid Malone 13102bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 13112bc21ed9SDavid Malone 13122bc21ed9SDavid Malone next: 13132bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 13142bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 13152bc21ed9SDavid Malone cm = (struct cmsghdr *) 13162bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 13178692c025SYoshinobu Inoue } else { 13182bc21ed9SDavid Malone clen = 0; 13192bc21ed9SDavid Malone cm = NULL; 13208692c025SYoshinobu Inoue } 13218692c025SYoshinobu Inoue } 13228692c025SYoshinobu Inoue 13232bc21ed9SDavid Malone m_freem(control); 13242bc21ed9SDavid Malone 13252bc21ed9SDavid Malone return (error); 1326df8bae1dSRodney W. Grimes } 1327df8bae1dSRodney W. Grimes 132898271db4SGarrett Wollman void 132998271db4SGarrett Wollman unp_init(void) 133098271db4SGarrett Wollman { 13319e9d298aSJeff Roberson unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 13329e9d298aSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 1333fc3fcacfSRobert Watson if (unp_zone == NULL) 133498271db4SGarrett Wollman panic("unp_init"); 1335b17dd2bcSColin Percival uma_zone_set_max(unp_zone, nmbclusters); 133698271db4SGarrett Wollman LIST_INIT(&unp_dhead); 133798271db4SGarrett Wollman LIST_INIT(&unp_shead); 1338a0ec558aSRobert Watson TASK_INIT(&unp_gc_task, 0, unp_gc, NULL); 13390d9ce3a1SRobert Watson UNP_LOCK_INIT(); 134098271db4SGarrett Wollman } 134198271db4SGarrett Wollman 1342f708ef1bSPoul-Henning Kamp static int 1343892af6b9SRobert Watson unp_internalize(struct mbuf **controlp, struct thread *td) 1344df8bae1dSRodney W. Grimes { 13452bc21ed9SDavid Malone struct mbuf *control = *controlp; 1346b40ce416SJulian Elischer struct proc *p = td->td_proc; 13478692c025SYoshinobu Inoue struct filedesc *fdescp = p->p_fd; 13482bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 13492bc21ed9SDavid Malone struct cmsgcred *cmcred; 13502bc21ed9SDavid Malone struct file **rp; 13512bc21ed9SDavid Malone struct file *fp; 13522bc21ed9SDavid Malone struct timeval *tv; 13532bc21ed9SDavid Malone int i, fd, *fdp; 13542bc21ed9SDavid Malone void *data; 13552bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 13562bc21ed9SDavid Malone int error, oldfds; 13578692c025SYoshinobu Inoue u_int newlen; 1358df8bae1dSRodney W. Grimes 13594c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 13604c5bc1caSRobert Watson 13612bc21ed9SDavid Malone error = 0; 13622bc21ed9SDavid Malone *controlp = NULL; 13630b788fa1SBill Paul 13642bc21ed9SDavid Malone while (cm != NULL) { 13652bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 13662bc21ed9SDavid Malone || cm->cmsg_len > clen) { 13672bc21ed9SDavid Malone error = EINVAL; 13682bc21ed9SDavid Malone goto out; 13692bc21ed9SDavid Malone } 13702bc21ed9SDavid Malone 13712bc21ed9SDavid Malone data = CMSG_DATA(cm); 13722bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 13732bc21ed9SDavid Malone 13742bc21ed9SDavid Malone switch (cm->cmsg_type) { 13750b788fa1SBill Paul /* 13760b788fa1SBill Paul * Fill in credential information. 13770b788fa1SBill Paul */ 13782bc21ed9SDavid Malone case SCM_CREDS: 13792bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 13802bc21ed9SDavid Malone SCM_CREDS, SOL_SOCKET); 13812bc21ed9SDavid Malone if (*controlp == NULL) { 13822bc21ed9SDavid Malone error = ENOBUFS; 13832bc21ed9SDavid Malone goto out; 13842bc21ed9SDavid Malone } 13852bc21ed9SDavid Malone 13862bc21ed9SDavid Malone cmcred = (struct cmsgcred *) 13872bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 13880b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 1389a854ed98SJohn Baldwin cmcred->cmcred_uid = td->td_ucred->cr_ruid; 1390a854ed98SJohn Baldwin cmcred->cmcred_gid = td->td_ucred->cr_rgid; 1391a854ed98SJohn Baldwin cmcred->cmcred_euid = td->td_ucred->cr_uid; 1392a854ed98SJohn Baldwin cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 13930b788fa1SBill Paul CMGROUP_MAX); 13940b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 13952bc21ed9SDavid Malone cmcred->cmcred_groups[i] = 1396a854ed98SJohn Baldwin td->td_ucred->cr_groups[i]; 13972bc21ed9SDavid Malone break; 13980b788fa1SBill Paul 13992bc21ed9SDavid Malone case SCM_RIGHTS: 14002bc21ed9SDavid Malone oldfds = datalen / sizeof (int); 1401ed5b7817SJulian Elischer /* 14022bc21ed9SDavid Malone * check that all the FDs passed in refer to legal files 1403ed5b7817SJulian Elischer * If not, reject the entire operation. 1404ed5b7817SJulian Elischer */ 14052bc21ed9SDavid Malone fdp = data; 1406426da3bcSAlfred Perlstein FILEDESC_LOCK(fdescp); 1407df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 14088692c025SYoshinobu Inoue fd = *fdp++; 14098692c025SYoshinobu Inoue if ((unsigned)fd >= fdescp->fd_nfiles || 14102bc21ed9SDavid Malone fdescp->fd_ofiles[fd] == NULL) { 1411426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 14122bc21ed9SDavid Malone error = EBADF; 14132bc21ed9SDavid Malone goto out; 14142bc21ed9SDavid Malone } 1415e7d6662fSAlfred Perlstein fp = fdescp->fd_ofiles[fd]; 1416e7d6662fSAlfred Perlstein if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 1417e7d6662fSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 1418e7d6662fSAlfred Perlstein error = EOPNOTSUPP; 1419e7d6662fSAlfred Perlstein goto out; 1420e7d6662fSAlfred Perlstein } 1421e7d6662fSAlfred Perlstein 1422df8bae1dSRodney W. Grimes } 1423ed5b7817SJulian Elischer /* 1424ed5b7817SJulian Elischer * Now replace the integer FDs with pointers to 1425ed5b7817SJulian Elischer * the associated global file table entry.. 1426ed5b7817SJulian Elischer */ 14272bc21ed9SDavid Malone newlen = oldfds * sizeof(struct file *); 14282bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 14292bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 14302bc21ed9SDavid Malone if (*controlp == NULL) { 1431426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 14322bc21ed9SDavid Malone error = E2BIG; 14332bc21ed9SDavid Malone goto out; 14348692c025SYoshinobu Inoue } 14358692c025SYoshinobu Inoue 14362bc21ed9SDavid Malone fdp = data; 14372bc21ed9SDavid Malone rp = (struct file **) 14382bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 14398692c025SYoshinobu Inoue for (i = 0; i < oldfds; i++) { 14408692c025SYoshinobu Inoue fp = fdescp->fd_ofiles[*fdp++]; 1441df8bae1dSRodney W. Grimes *rp++ = fp; 1442426da3bcSAlfred Perlstein FILE_LOCK(fp); 1443df8bae1dSRodney W. Grimes fp->f_count++; 1444df8bae1dSRodney W. Grimes fp->f_msgcount++; 1445426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1446df8bae1dSRodney W. Grimes unp_rights++; 1447df8bae1dSRodney W. Grimes } 1448426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 14492bc21ed9SDavid Malone break; 14502bc21ed9SDavid Malone 14512bc21ed9SDavid Malone case SCM_TIMESTAMP: 14522bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*tv), 14532bc21ed9SDavid Malone SCM_TIMESTAMP, SOL_SOCKET); 14542bc21ed9SDavid Malone if (*controlp == NULL) { 14552bc21ed9SDavid Malone error = ENOBUFS; 14562bc21ed9SDavid Malone goto out; 14578692c025SYoshinobu Inoue } 14582bc21ed9SDavid Malone tv = (struct timeval *) 14592bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 14602bc21ed9SDavid Malone microtime(tv); 14612bc21ed9SDavid Malone break; 14622bc21ed9SDavid Malone 14632bc21ed9SDavid Malone default: 14642bc21ed9SDavid Malone error = EINVAL; 14652bc21ed9SDavid Malone goto out; 14662bc21ed9SDavid Malone } 14672bc21ed9SDavid Malone 14682bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 14692bc21ed9SDavid Malone 14702bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 14712bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 14722bc21ed9SDavid Malone cm = (struct cmsghdr *) 14732bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 14742bc21ed9SDavid Malone } else { 14752bc21ed9SDavid Malone clen = 0; 14762bc21ed9SDavid Malone cm = NULL; 14772bc21ed9SDavid Malone } 14782bc21ed9SDavid Malone } 14792bc21ed9SDavid Malone 14802bc21ed9SDavid Malone out: 14812bc21ed9SDavid Malone m_freem(control); 14822bc21ed9SDavid Malone 14832bc21ed9SDavid Malone return (error); 1484df8bae1dSRodney W. Grimes } 1485df8bae1dSRodney W. Grimes 14866a2989fdSMatthew N. Dodd struct mbuf * 14876a2989fdSMatthew N. Dodd unp_addsockcred(struct thread *td, struct mbuf *control) 14886a2989fdSMatthew N. Dodd { 14896a2989fdSMatthew N. Dodd struct mbuf *m, *n; 14906a2989fdSMatthew N. Dodd struct sockcred *sc; 14916a2989fdSMatthew N. Dodd int ngroups; 14926a2989fdSMatthew N. Dodd int i; 14936a2989fdSMatthew N. Dodd 14946a2989fdSMatthew N. Dodd ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX); 14956a2989fdSMatthew N. Dodd 14966a2989fdSMatthew N. Dodd m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET); 14976a2989fdSMatthew N. Dodd if (m == NULL) 14986a2989fdSMatthew N. Dodd return (control); 14996a2989fdSMatthew N. Dodd m->m_next = NULL; 15006a2989fdSMatthew N. Dodd 15016a2989fdSMatthew N. Dodd sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *)); 15026a2989fdSMatthew N. Dodd sc->sc_uid = td->td_ucred->cr_ruid; 15036a2989fdSMatthew N. Dodd sc->sc_euid = td->td_ucred->cr_uid; 15046a2989fdSMatthew N. Dodd sc->sc_gid = td->td_ucred->cr_rgid; 15056a2989fdSMatthew N. Dodd sc->sc_egid = td->td_ucred->cr_gid; 15066a2989fdSMatthew N. Dodd sc->sc_ngroups = ngroups; 15076a2989fdSMatthew N. Dodd for (i = 0; i < sc->sc_ngroups; i++) 15086a2989fdSMatthew N. Dodd sc->sc_groups[i] = td->td_ucred->cr_groups[i]; 15096a2989fdSMatthew N. Dodd 15106a2989fdSMatthew N. Dodd /* 15116a2989fdSMatthew N. Dodd * If a control message already exists, append us to the end. 15126a2989fdSMatthew N. Dodd */ 15136a2989fdSMatthew N. Dodd if (control != NULL) { 15146a2989fdSMatthew N. Dodd for (n = control; n->m_next != NULL; n = n->m_next) 15156a2989fdSMatthew N. Dodd ; 15166a2989fdSMatthew N. Dodd n->m_next = m; 15176a2989fdSMatthew N. Dodd } else 15186a2989fdSMatthew N. Dodd control = m; 15196a2989fdSMatthew N. Dodd 15206a2989fdSMatthew N. Dodd return (control); 15216a2989fdSMatthew N. Dodd } 15226a2989fdSMatthew N. Dodd 1523161a0c7cSRobert Watson /* 1524a0ec558aSRobert Watson * unp_defer indicates whether additional work has been defered for a future 1525a0ec558aSRobert Watson * pass through unp_gc(). It is thread local and does not require explicit 1526a0ec558aSRobert Watson * synchronization. 1527161a0c7cSRobert Watson */ 1528a0ec558aSRobert Watson static int unp_defer; 1529a0ec558aSRobert Watson 1530a0ec558aSRobert Watson static int unp_taskcount; 1531a0ec558aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0, ""); 1532a0ec558aSRobert Watson 1533a0ec558aSRobert Watson static int unp_recycled; 1534a0ec558aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0, ""); 1535df8bae1dSRodney W. Grimes 1536f708ef1bSPoul-Henning Kamp static void 1537a0ec558aSRobert Watson unp_gc(__unused void *arg, int pending) 1538df8bae1dSRodney W. Grimes { 1539892af6b9SRobert Watson struct file *fp, *nextfp; 1540892af6b9SRobert Watson struct socket *so; 1541df8bae1dSRodney W. Grimes struct file **extra_ref, **fpp; 1542df8bae1dSRodney W. Grimes int nunref, i; 154395f004dcSAlfred Perlstein int nfiles_snap; 154495f004dcSAlfred Perlstein int nfiles_slack = 20; 1545df8bae1dSRodney W. Grimes 1546a0ec558aSRobert Watson unp_taskcount++; 1547df8bae1dSRodney W. Grimes unp_defer = 0; 1548ed5b7817SJulian Elischer /* 1549ed5b7817SJulian Elischer * before going through all this, set all FDs to 1550ed5b7817SJulian Elischer * be NOT defered and NOT externally accessible 1551ed5b7817SJulian Elischer */ 1552426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 15532e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) 1554426da3bcSAlfred Perlstein fp->f_gcflag &= ~(FMARK|FDEFER); 1555df8bae1dSRodney W. Grimes do { 15565bb84bc8SRobert Watson KASSERT(unp_defer >= 0, ("unp_gc: unp_defer %d", unp_defer)); 15572e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) { 1558426da3bcSAlfred Perlstein FILE_LOCK(fp); 1559ed5b7817SJulian Elischer /* 1560a0ec558aSRobert Watson * If the file is not open, skip it -- could be a 1561a0ec558aSRobert Watson * file in the process of being opened, or in the 1562a0ec558aSRobert Watson * process of being closed. If the file is 1563a0ec558aSRobert Watson * "closing", it may have been marked for deferred 1564a0ec558aSRobert Watson * consideration. Clear the flag now if so. 1565ed5b7817SJulian Elischer */ 1566426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1567a0ec558aSRobert Watson if (fp->f_gcflag & FDEFER) 1568a0ec558aSRobert Watson unp_defer--; 1569a0ec558aSRobert Watson fp->f_gcflag &= ~(FMARK|FDEFER); 1570426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1571df8bae1dSRodney W. Grimes continue; 1572426da3bcSAlfred Perlstein } 1573ed5b7817SJulian Elischer /* 1574ed5b7817SJulian Elischer * If we already marked it as 'defer' in a 1575ed5b7817SJulian Elischer * previous pass, then try process it this time 1576ed5b7817SJulian Elischer * and un-mark it 1577ed5b7817SJulian Elischer */ 1578426da3bcSAlfred Perlstein if (fp->f_gcflag & FDEFER) { 1579426da3bcSAlfred Perlstein fp->f_gcflag &= ~FDEFER; 1580df8bae1dSRodney W. Grimes unp_defer--; 1581df8bae1dSRodney W. Grimes } else { 1582ed5b7817SJulian Elischer /* 1583ed5b7817SJulian Elischer * if it's not defered, then check if it's 1584ed5b7817SJulian Elischer * already marked.. if so skip it 1585ed5b7817SJulian Elischer */ 1586426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) { 1587426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1588df8bae1dSRodney W. Grimes continue; 1589426da3bcSAlfred Perlstein } 1590ed5b7817SJulian Elischer /* 1591ed5b7817SJulian Elischer * If all references are from messages 1592ed5b7817SJulian Elischer * in transit, then skip it. it's not 1593ed5b7817SJulian Elischer * externally accessible. 1594ed5b7817SJulian Elischer */ 1595426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount) { 1596426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1597df8bae1dSRodney W. Grimes continue; 1598426da3bcSAlfred Perlstein } 1599ed5b7817SJulian Elischer /* 1600ed5b7817SJulian Elischer * If it got this far then it must be 1601ed5b7817SJulian Elischer * externally accessible. 1602ed5b7817SJulian Elischer */ 1603426da3bcSAlfred Perlstein fp->f_gcflag |= FMARK; 1604df8bae1dSRodney W. Grimes } 1605ed5b7817SJulian Elischer /* 1606ed5b7817SJulian Elischer * either it was defered, or it is externally 1607ed5b7817SJulian Elischer * accessible and not already marked so. 1608ed5b7817SJulian Elischer * Now check if it is possibly one of OUR sockets. 1609ed5b7817SJulian Elischer */ 1610df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_SOCKET || 161148e3128bSMatthew Dillon (so = fp->f_data) == NULL) { 1612426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1613df8bae1dSRodney W. Grimes continue; 1614426da3bcSAlfred Perlstein } 1615426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1616748e0b0aSGarrett Wollman if (so->so_proto->pr_domain != &localdomain || 1617df8bae1dSRodney W. Grimes (so->so_proto->pr_flags&PR_RIGHTS) == 0) 1618df8bae1dSRodney W. Grimes continue; 1619ed5b7817SJulian Elischer /* 1620ed5b7817SJulian Elischer * So, Ok, it's one of our sockets and it IS externally 1621ed5b7817SJulian Elischer * accessible (or was defered). Now we look 1622dc733423SDag-Erling Smørgrav * to see if we hold any file descriptors in its 1623ed5b7817SJulian Elischer * message buffers. Follow those links and mark them 1624ed5b7817SJulian Elischer * as accessible too. 1625ed5b7817SJulian Elischer */ 16267717cf07SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1627df8bae1dSRodney W. Grimes unp_scan(so->so_rcv.sb_mb, unp_mark); 16287717cf07SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 1629df8bae1dSRodney W. Grimes } 1630df8bae1dSRodney W. Grimes } while (unp_defer); 1631426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1632df8bae1dSRodney W. Grimes /* 1633a0ec558aSRobert Watson * XXXRW: The following comments need updating for a post-SMPng and 1634a0ec558aSRobert Watson * deferred unp_gc() world, but are still generally accurate. 1635a0ec558aSRobert Watson * 1636df8bae1dSRodney W. Grimes * We grab an extra reference to each of the file table entries 1637df8bae1dSRodney W. Grimes * that are not otherwise accessible and then free the rights 1638df8bae1dSRodney W. Grimes * that are stored in messages on them. 1639df8bae1dSRodney W. Grimes * 1640df8bae1dSRodney W. Grimes * The bug in the orginal code is a little tricky, so I'll describe 1641df8bae1dSRodney W. Grimes * what's wrong with it here. 1642df8bae1dSRodney W. Grimes * 1643df8bae1dSRodney W. Grimes * It is incorrect to simply unp_discard each entry for f_msgcount 1644df8bae1dSRodney W. Grimes * times -- consider the case of sockets A and B that contain 1645df8bae1dSRodney W. Grimes * references to each other. On a last close of some other socket, 1646df8bae1dSRodney W. Grimes * we trigger a gc since the number of outstanding rights (unp_rights) 1647a0ec558aSRobert Watson * is non-zero. If during the sweep phase the gc code unp_discards, 1648df8bae1dSRodney W. Grimes * we end up doing a (full) closef on the descriptor. A closef on A 1649df8bae1dSRodney W. Grimes * results in the following chain. Closef calls soo_close, which 1650df8bae1dSRodney W. Grimes * calls soclose. Soclose calls first (through the switch 1651df8bae1dSRodney W. Grimes * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 1652df8bae1dSRodney W. Grimes * returns because the previous instance had set unp_gcing, and 1653df8bae1dSRodney W. Grimes * we return all the way back to soclose, which marks the socket 1654df8bae1dSRodney W. Grimes * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush 1655df8bae1dSRodney W. Grimes * to free up the rights that are queued in messages on the socket A, 1656df8bae1dSRodney W. Grimes * i.e., the reference on B. The sorflush calls via the dom_dispose 1657df8bae1dSRodney W. Grimes * switch unp_dispose, which unp_scans with unp_discard. This second 1658df8bae1dSRodney W. Grimes * instance of unp_discard just calls closef on B. 1659df8bae1dSRodney W. Grimes * 1660df8bae1dSRodney W. Grimes * Well, a similar chain occurs on B, resulting in a sorflush on B, 1661df8bae1dSRodney W. Grimes * which results in another closef on A. Unfortunately, A is already 1662df8bae1dSRodney W. Grimes * being closed, and the descriptor has already been marked with 1663df8bae1dSRodney W. Grimes * SS_NOFDREF, and soclose panics at this point. 1664df8bae1dSRodney W. Grimes * 1665df8bae1dSRodney W. Grimes * Here, we first take an extra reference to each inaccessible 1666df8bae1dSRodney W. Grimes * descriptor. Then, we call sorflush ourself, since we know 1667df8bae1dSRodney W. Grimes * it is a Unix domain socket anyhow. After we destroy all the 1668df8bae1dSRodney W. Grimes * rights carried in messages, we do a last closef to get rid 1669df8bae1dSRodney W. Grimes * of our extra reference. This is the last close, and the 1670df8bae1dSRodney W. Grimes * unp_detach etc will shut down the socket. 1671df8bae1dSRodney W. Grimes * 1672df8bae1dSRodney W. Grimes * 91/09/19, bsy@cs.cmu.edu 1673df8bae1dSRodney W. Grimes */ 167495f004dcSAlfred Perlstein again: 1675e4643c73SPoul-Henning Kamp nfiles_snap = openfiles + nfiles_slack; /* some slack */ 167695f004dcSAlfred Perlstein extra_ref = malloc(nfiles_snap * sizeof(struct file *), M_TEMP, 167795f004dcSAlfred Perlstein M_WAITOK); 1678426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 1679e4643c73SPoul-Henning Kamp if (nfiles_snap < openfiles) { 168095f004dcSAlfred Perlstein sx_sunlock(&filelist_lock); 168195f004dcSAlfred Perlstein free(extra_ref, M_TEMP); 168295f004dcSAlfred Perlstein nfiles_slack += 20; 168395f004dcSAlfred Perlstein goto again; 168495f004dcSAlfred Perlstein } 1685fc3fcacfSRobert Watson for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; 1686fc3fcacfSRobert Watson fp != NULL; fp = nextfp) { 16872e3c8fcbSPoul-Henning Kamp nextfp = LIST_NEXT(fp, f_list); 1688426da3bcSAlfred Perlstein FILE_LOCK(fp); 1689ed5b7817SJulian Elischer /* 1690ed5b7817SJulian Elischer * If it's not open, skip it 1691ed5b7817SJulian Elischer */ 1692426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1693426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1694df8bae1dSRodney W. Grimes continue; 1695426da3bcSAlfred Perlstein } 1696ed5b7817SJulian Elischer /* 1697ed5b7817SJulian Elischer * If all refs are from msgs, and it's not marked accessible 1698ed5b7817SJulian Elischer * then it must be referenced from some unreachable cycle 1699ed5b7817SJulian Elischer * of (shut-down) FDs, so include it in our 1700ed5b7817SJulian Elischer * list of FDs to remove 1701ed5b7817SJulian Elischer */ 1702426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) { 1703df8bae1dSRodney W. Grimes *fpp++ = fp; 1704df8bae1dSRodney W. Grimes nunref++; 1705df8bae1dSRodney W. Grimes fp->f_count++; 1706df8bae1dSRodney W. Grimes } 1707426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1708df8bae1dSRodney W. Grimes } 1709426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1710ed5b7817SJulian Elischer /* 1711ed5b7817SJulian Elischer * for each FD on our hit list, do the following two things 1712ed5b7817SJulian Elischer */ 17131c7c3c6aSMatthew Dillon for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 17141c7c3c6aSMatthew Dillon struct file *tfp = *fpp; 1715426da3bcSAlfred Perlstein FILE_LOCK(tfp); 1716cd72f218SMatthew Dillon if (tfp->f_type == DTYPE_SOCKET && 171748e3128bSMatthew Dillon tfp->f_data != NULL) { 1718426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 171948e3128bSMatthew Dillon sorflush(tfp->f_data); 1720e5aeaa0cSDag-Erling Smørgrav } else { 1721426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 17221c7c3c6aSMatthew Dillon } 1723e5aeaa0cSDag-Erling Smørgrav } 1724a0ec558aSRobert Watson for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 1725b40ce416SJulian Elischer closef(*fpp, (struct thread *) NULL); 1726a0ec558aSRobert Watson unp_recycled++; 1727a0ec558aSRobert Watson } 1728210a5a71SAlfred Perlstein free(extra_ref, M_TEMP); 1729df8bae1dSRodney W. Grimes } 1730df8bae1dSRodney W. Grimes 173126f9a767SRodney W. Grimes void 1732892af6b9SRobert Watson unp_dispose(struct mbuf *m) 1733df8bae1dSRodney W. Grimes { 1734996c772fSJohn Dyson 1735df8bae1dSRodney W. Grimes if (m) 1736df8bae1dSRodney W. Grimes unp_scan(m, unp_discard); 1737df8bae1dSRodney W. Grimes } 1738df8bae1dSRodney W. Grimes 17390c1bb4fbSDima Dorfman static int 1740d374e81eSRobert Watson unp_listen(struct socket *so, struct unpcb *unp, int backlog, 1741d374e81eSRobert Watson struct thread *td) 17420c1bb4fbSDima Dorfman { 17430daccb9cSRobert Watson int error; 17440daccb9cSRobert Watson 17450d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 17460c1bb4fbSDima Dorfman 17470daccb9cSRobert Watson SOCK_LOCK(so); 17480daccb9cSRobert Watson error = solisten_proto_check(so); 17490daccb9cSRobert Watson if (error == 0) { 17506f105b34SJohn Baldwin cru2x(td->td_ucred, &unp->unp_peercred); 17510c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPCCACHED; 1752d374e81eSRobert Watson solisten_proto(so, backlog); 17530daccb9cSRobert Watson } 17540daccb9cSRobert Watson SOCK_UNLOCK(so); 17550daccb9cSRobert Watson return (error); 17560c1bb4fbSDima Dorfman } 17570c1bb4fbSDima Dorfman 1758f708ef1bSPoul-Henning Kamp static void 1759892af6b9SRobert Watson unp_scan(struct mbuf *m0, void (*op)(struct file *)) 1760df8bae1dSRodney W. Grimes { 17612bc21ed9SDavid Malone struct mbuf *m; 17622bc21ed9SDavid Malone struct file **rp; 17632bc21ed9SDavid Malone struct cmsghdr *cm; 17642bc21ed9SDavid Malone void *data; 17652bc21ed9SDavid Malone int i; 17662bc21ed9SDavid Malone socklen_t clen, datalen; 1767df8bae1dSRodney W. Grimes int qfds; 1768df8bae1dSRodney W. Grimes 1769fc3fcacfSRobert Watson while (m0 != NULL) { 17702bc21ed9SDavid Malone for (m = m0; m; m = m->m_next) { 177112396bdcSDavid Malone if (m->m_type != MT_CONTROL) 1772df8bae1dSRodney W. Grimes continue; 17732bc21ed9SDavid Malone 17742bc21ed9SDavid Malone cm = mtod(m, struct cmsghdr *); 17752bc21ed9SDavid Malone clen = m->m_len; 17762bc21ed9SDavid Malone 17772bc21ed9SDavid Malone while (cm != NULL) { 17782bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) 17792bc21ed9SDavid Malone break; 17802bc21ed9SDavid Malone 17812bc21ed9SDavid Malone data = CMSG_DATA(cm); 17822bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len 17832bc21ed9SDavid Malone - (caddr_t)data; 17842bc21ed9SDavid Malone 17852bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET && 17862bc21ed9SDavid Malone cm->cmsg_type == SCM_RIGHTS) { 17872bc21ed9SDavid Malone qfds = datalen / sizeof (struct file *); 17882bc21ed9SDavid Malone rp = data; 1789df8bae1dSRodney W. Grimes for (i = 0; i < qfds; i++) 1790df8bae1dSRodney W. Grimes (*op)(*rp++); 17912bc21ed9SDavid Malone } 17922bc21ed9SDavid Malone 17932bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 17942bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 17952bc21ed9SDavid Malone cm = (struct cmsghdr *) 17962bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 17972bc21ed9SDavid Malone } else { 17982bc21ed9SDavid Malone clen = 0; 17992bc21ed9SDavid Malone cm = NULL; 18002bc21ed9SDavid Malone } 18012bc21ed9SDavid Malone } 1802df8bae1dSRodney W. Grimes } 1803df8bae1dSRodney W. Grimes m0 = m0->m_act; 1804df8bae1dSRodney W. Grimes } 1805df8bae1dSRodney W. Grimes } 1806df8bae1dSRodney W. Grimes 1807f708ef1bSPoul-Henning Kamp static void 1808892af6b9SRobert Watson unp_mark(struct file *fp) 1809df8bae1dSRodney W. Grimes { 1810426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) 1811df8bae1dSRodney W. Grimes return; 1812df8bae1dSRodney W. Grimes unp_defer++; 1813426da3bcSAlfred Perlstein fp->f_gcflag |= (FMARK|FDEFER); 1814df8bae1dSRodney W. Grimes } 1815df8bae1dSRodney W. Grimes 1816f708ef1bSPoul-Henning Kamp static void 1817892af6b9SRobert Watson unp_discard(struct file *fp) 1818df8bae1dSRodney W. Grimes { 1819a0ec558aSRobert Watson UNP_LOCK(); 1820426da3bcSAlfred Perlstein FILE_LOCK(fp); 1821df8bae1dSRodney W. Grimes fp->f_msgcount--; 1822df8bae1dSRodney W. Grimes unp_rights--; 1823426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1824a0ec558aSRobert Watson UNP_UNLOCK(); 1825b40ce416SJulian Elischer (void) closef(fp, (struct thread *)NULL); 1826df8bae1dSRodney W. Grimes } 1827