19454b2d8SWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1991, 1993 3e1ac28e2SRobert Watson * The Regents of the University of California. 4e1ac28e2SRobert Watson * Copyright 2004-2005 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 142a29f300eSGarrett Wollman static int 143a29f300eSGarrett Wollman uipc_abort(struct socket *so) 144df8bae1dSRodney W. Grimes { 14540f2ac28SRobert Watson struct unpcb *unp; 146df8bae1dSRodney W. Grimes 1470d9ce3a1SRobert Watson UNP_LOCK(); 14840f2ac28SRobert Watson unp = sotounpcb(so); 14940f2ac28SRobert Watson if (unp == NULL) { 15040f2ac28SRobert Watson UNP_UNLOCK(); 15140f2ac28SRobert Watson return (EINVAL); 15240f2ac28SRobert Watson } 153a29f300eSGarrett Wollman unp_drop(unp, ECONNABORTED); 1544c5bc1caSRobert Watson unp_detach(unp); 1554c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 15681158452SRobert Watson ACCEPT_LOCK(); 157395a08c9SRobert Watson SOCK_LOCK(so); 158ddb7d629SIan Dowse sotryfree(so); 159e5aeaa0cSDag-Erling Smørgrav return (0); 160df8bae1dSRodney W. Grimes } 161df8bae1dSRodney W. Grimes 162a29f300eSGarrett Wollman static int 16357bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam) 164a29f300eSGarrett Wollman { 16540f2ac28SRobert Watson struct unpcb *unp; 1660d9ce3a1SRobert Watson const struct sockaddr *sa; 167df8bae1dSRodney W. Grimes 168df8bae1dSRodney W. Grimes /* 169df8bae1dSRodney W. Grimes * Pass back name of connected socket, 170df8bae1dSRodney W. Grimes * if it was bound and we are still connected 171df8bae1dSRodney W. Grimes * (our peer may have closed already!). 172df8bae1dSRodney W. Grimes */ 1730d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 1740d9ce3a1SRobert Watson UNP_LOCK(); 17540f2ac28SRobert Watson unp = sotounpcb(so); 17640f2ac28SRobert Watson if (unp == NULL) { 17740f2ac28SRobert Watson UNP_UNLOCK(); 17840f2ac28SRobert Watson free(*nam, M_SONAME); 17940f2ac28SRobert Watson *nam = NULL; 18040f2ac28SRobert Watson return (EINVAL); 18140f2ac28SRobert Watson } 1820d9ce3a1SRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr != NULL) 1830d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 1840d9ce3a1SRobert Watson else 1850d9ce3a1SRobert Watson sa = &sun_noname; 1860d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 1870d9ce3a1SRobert Watson UNP_UNLOCK(); 188e5aeaa0cSDag-Erling Smørgrav return (0); 189a29f300eSGarrett Wollman } 190df8bae1dSRodney W. Grimes 191a29f300eSGarrett Wollman static int 192b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td) 193a29f300eSGarrett Wollman { 194a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 195df8bae1dSRodney W. Grimes 196fc3fcacfSRobert Watson if (unp != NULL) 197e5aeaa0cSDag-Erling Smørgrav return (EISCONN); 198e5aeaa0cSDag-Erling Smørgrav return (unp_attach(so)); 199a29f300eSGarrett Wollman } 200a29f300eSGarrett Wollman 201a29f300eSGarrett Wollman static int 202b40ce416SJulian Elischer uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 203a29f300eSGarrett Wollman { 20440f2ac28SRobert Watson struct unpcb *unp; 20540f2ac28SRobert Watson int error; 206a29f300eSGarrett Wollman 20740f2ac28SRobert Watson UNP_LOCK(); 20840f2ac28SRobert Watson unp = sotounpcb(so); 20940f2ac28SRobert Watson if (unp == NULL) { 21040f2ac28SRobert Watson UNP_UNLOCK(); 211e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 21240f2ac28SRobert Watson } 21340f2ac28SRobert Watson error = unp_bind(unp, nam, td); 21440f2ac28SRobert Watson UNP_UNLOCK(); 21540f2ac28SRobert Watson return (error); 216a29f300eSGarrett Wollman } 217a29f300eSGarrett Wollman 218a29f300eSGarrett Wollman static int 219b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 220a29f300eSGarrett Wollman { 221b295bdcdSRobert Watson struct unpcb *unp; 2220d9ce3a1SRobert Watson int error; 223a29f300eSGarrett Wollman 224fd179ee9SRobert Watson KASSERT(td == curthread, ("uipc_connect: td != curthread")); 225fd179ee9SRobert Watson 2260d9ce3a1SRobert Watson UNP_LOCK(); 227b295bdcdSRobert Watson unp = sotounpcb(so); 228b295bdcdSRobert Watson if (unp == NULL) { 22940f2ac28SRobert Watson UNP_UNLOCK(); 23040f2ac28SRobert Watson return (EINVAL); 231b295bdcdSRobert Watson } 232fd179ee9SRobert Watson error = unp_connect(so, nam, td); 2330d9ce3a1SRobert Watson UNP_UNLOCK(); 2340d9ce3a1SRobert Watson return (error); 235a29f300eSGarrett Wollman } 236a29f300eSGarrett Wollman 237db48c0d2SRobert Watson int 238a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2) 239a29f300eSGarrett Wollman { 24040f2ac28SRobert Watson struct unpcb *unp; 2410d9ce3a1SRobert Watson int error; 242a29f300eSGarrett Wollman 2430d9ce3a1SRobert Watson UNP_LOCK(); 24440f2ac28SRobert Watson unp = sotounpcb(so1); 24540f2ac28SRobert Watson if (unp == NULL) { 24640f2ac28SRobert Watson UNP_UNLOCK(); 24740f2ac28SRobert Watson return (EINVAL); 24840f2ac28SRobert Watson } 2496a2989fdSMatthew N. Dodd error = unp_connect2(so1, so2, PRU_CONNECT2); 2500d9ce3a1SRobert Watson UNP_UNLOCK(); 2510d9ce3a1SRobert Watson return (error); 252a29f300eSGarrett Wollman } 253a29f300eSGarrett Wollman 254a29f300eSGarrett Wollman /* control is EOPNOTSUPP */ 255a29f300eSGarrett Wollman 256a29f300eSGarrett Wollman static int 257a29f300eSGarrett Wollman uipc_detach(struct socket *so) 258a29f300eSGarrett Wollman { 25940f2ac28SRobert Watson struct unpcb *unp; 260a29f300eSGarrett Wollman 2610d9ce3a1SRobert Watson UNP_LOCK(); 26240f2ac28SRobert Watson unp = sotounpcb(so); 26340f2ac28SRobert Watson if (unp == NULL) { 26440f2ac28SRobert Watson UNP_UNLOCK(); 26540f2ac28SRobert Watson return (EINVAL); 26640f2ac28SRobert Watson } 2674c5bc1caSRobert Watson unp_detach(unp); 2684c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 269e5aeaa0cSDag-Erling Smørgrav return (0); 270a29f300eSGarrett Wollman } 271a29f300eSGarrett Wollman 272a29f300eSGarrett Wollman static int 273a29f300eSGarrett Wollman uipc_disconnect(struct socket *so) 274a29f300eSGarrett Wollman { 27540f2ac28SRobert Watson struct unpcb *unp; 276a29f300eSGarrett Wollman 2770d9ce3a1SRobert Watson UNP_LOCK(); 27840f2ac28SRobert Watson unp = sotounpcb(so); 27940f2ac28SRobert Watson if (unp == NULL) { 28040f2ac28SRobert Watson UNP_UNLOCK(); 28140f2ac28SRobert Watson return (EINVAL); 28240f2ac28SRobert Watson } 283a29f300eSGarrett Wollman unp_disconnect(unp); 2840d9ce3a1SRobert Watson UNP_UNLOCK(); 285e5aeaa0cSDag-Erling Smørgrav return (0); 286a29f300eSGarrett Wollman } 287a29f300eSGarrett Wollman 288a29f300eSGarrett Wollman static int 289d374e81eSRobert Watson uipc_listen(struct socket *so, int backlog, struct thread *td) 290a29f300eSGarrett Wollman { 29140f2ac28SRobert Watson struct unpcb *unp; 2920d9ce3a1SRobert Watson int error; 293a29f300eSGarrett Wollman 2940d9ce3a1SRobert Watson UNP_LOCK(); 29540f2ac28SRobert Watson unp = sotounpcb(so); 29640f2ac28SRobert Watson if (unp == NULL || unp->unp_vnode == NULL) { 29740f2ac28SRobert Watson UNP_UNLOCK(); 29840f2ac28SRobert Watson return (EINVAL); 29940f2ac28SRobert Watson } 300d374e81eSRobert Watson error = unp_listen(so, unp, backlog, td); 3010d9ce3a1SRobert Watson UNP_UNLOCK(); 3020d9ce3a1SRobert Watson return (error); 303a29f300eSGarrett Wollman } 304a29f300eSGarrett Wollman 305a29f300eSGarrett Wollman static int 30657bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 307a29f300eSGarrett Wollman { 30840f2ac28SRobert Watson struct unpcb *unp; 3090d9ce3a1SRobert Watson const struct sockaddr *sa; 310a29f300eSGarrett Wollman 3110d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 3120d9ce3a1SRobert Watson UNP_LOCK(); 31340f2ac28SRobert Watson unp = sotounpcb(so); 31440f2ac28SRobert Watson if (unp == NULL) { 31540f2ac28SRobert Watson UNP_UNLOCK(); 31640f2ac28SRobert Watson free(*nam, M_SONAME); 31740f2ac28SRobert Watson *nam = NULL; 31840f2ac28SRobert Watson return (EINVAL); 31940f2ac28SRobert Watson } 320fc3fcacfSRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr!= NULL) 3210d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 322bdc5f6a3SHajimu UMEMOTO else { 323bdc5f6a3SHajimu UMEMOTO /* 324bdc5f6a3SHajimu UMEMOTO * XXX: It seems that this test always fails even when 325bdc5f6a3SHajimu UMEMOTO * connection is established. So, this else clause is 326bdc5f6a3SHajimu UMEMOTO * added as workaround to return PF_LOCAL sockaddr. 327bdc5f6a3SHajimu UMEMOTO */ 3280d9ce3a1SRobert Watson sa = &sun_noname; 329bdc5f6a3SHajimu UMEMOTO } 3300d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 3310d9ce3a1SRobert Watson UNP_UNLOCK(); 332e5aeaa0cSDag-Erling Smørgrav return (0); 333a29f300eSGarrett Wollman } 334a29f300eSGarrett Wollman 335a29f300eSGarrett Wollman static int 336a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 337a29f300eSGarrett Wollman { 33840f2ac28SRobert Watson struct unpcb *unp; 339a29f300eSGarrett Wollman struct socket *so2; 3406aef685fSBrian Feldman u_long newhiwat; 341a29f300eSGarrett Wollman 3420d9ce3a1SRobert Watson UNP_LOCK(); 34340f2ac28SRobert Watson unp = sotounpcb(so); 34440f2ac28SRobert Watson if (unp == NULL) { 34540f2ac28SRobert Watson UNP_UNLOCK(); 34640f2ac28SRobert Watson return (EINVAL); 34740f2ac28SRobert Watson } 348df8bae1dSRodney W. Grimes switch (so->so_type) { 349df8bae1dSRodney W. Grimes case SOCK_DGRAM: 350a29f300eSGarrett Wollman panic("uipc_rcvd DGRAM?"); 351df8bae1dSRodney W. Grimes /*NOTREACHED*/ 352df8bae1dSRodney W. Grimes 353df8bae1dSRodney W. Grimes case SOCK_STREAM: 354fc3fcacfSRobert Watson if (unp->unp_conn == NULL) 355df8bae1dSRodney W. Grimes break; 356df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 357c9f69064SRobert Watson SOCKBUF_LOCK(&so2->so_snd); 358c9f69064SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 359df8bae1dSRodney W. Grimes /* 360df8bae1dSRodney W. Grimes * Adjust backpressure on sender 361df8bae1dSRodney W. Grimes * and wakeup any waiting to write. 362df8bae1dSRodney W. Grimes */ 363ff8b0106SBrian Feldman so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt; 364ff8b0106SBrian Feldman unp->unp_mbcnt = so->so_rcv.sb_mbcnt; 3656aef685fSBrian Feldman newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - 3666aef685fSBrian Feldman so->so_rcv.sb_cc; 367f535380cSDon Lewis (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat, 3686aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 369ff8b0106SBrian Feldman unp->unp_cc = so->so_rcv.sb_cc; 370c9f69064SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 3711e4d7da7SRobert Watson sowwakeup_locked(so2); 372df8bae1dSRodney W. Grimes break; 373df8bae1dSRodney W. Grimes 374df8bae1dSRodney W. Grimes default: 375a29f300eSGarrett Wollman panic("uipc_rcvd unknown socktype"); 376df8bae1dSRodney W. Grimes } 3770d9ce3a1SRobert Watson UNP_UNLOCK(); 378e5aeaa0cSDag-Erling Smørgrav return (0); 379a29f300eSGarrett Wollman } 380df8bae1dSRodney W. Grimes 381a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 382a29f300eSGarrett Wollman 383a29f300eSGarrett Wollman static int 38457bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 385b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 386a29f300eSGarrett Wollman { 387a29f300eSGarrett Wollman int error = 0; 38840f2ac28SRobert Watson struct unpcb *unp; 389a29f300eSGarrett Wollman struct socket *so2; 3906aef685fSBrian Feldman u_long newhiwat; 391a29f300eSGarrett Wollman 39240f2ac28SRobert Watson unp = sotounpcb(so); 393fc3fcacfSRobert Watson if (unp == NULL) { 394a29f300eSGarrett Wollman error = EINVAL; 395a29f300eSGarrett Wollman goto release; 396a29f300eSGarrett Wollman } 397a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 398a29f300eSGarrett Wollman error = EOPNOTSUPP; 399a29f300eSGarrett Wollman goto release; 400a29f300eSGarrett Wollman } 401a29f300eSGarrett Wollman 402fc3fcacfSRobert Watson if (control != NULL && (error = unp_internalize(&control, td))) 403a29f300eSGarrett Wollman goto release; 404df8bae1dSRodney W. Grimes 4050d9ce3a1SRobert Watson UNP_LOCK(); 40640f2ac28SRobert Watson unp = sotounpcb(so); 40740f2ac28SRobert Watson if (unp == NULL) { 40840f2ac28SRobert Watson UNP_UNLOCK(); 40940f2ac28SRobert Watson error = EINVAL; 41040f2ac28SRobert Watson goto dispose_release; 41140f2ac28SRobert Watson } 41240f2ac28SRobert Watson 413a29f300eSGarrett Wollman switch (so->so_type) { 414a29f300eSGarrett Wollman case SOCK_DGRAM: 415a29f300eSGarrett Wollman { 416e7dd9a10SRobert Watson const struct sockaddr *from; 417df8bae1dSRodney W. Grimes 418fc3fcacfSRobert Watson if (nam != NULL) { 419fc3fcacfSRobert Watson if (unp->unp_conn != NULL) { 420df8bae1dSRodney W. Grimes error = EISCONN; 421df8bae1dSRodney W. Grimes break; 422df8bae1dSRodney W. Grimes } 423b40ce416SJulian Elischer error = unp_connect(so, nam, td); 424df8bae1dSRodney W. Grimes if (error) 425df8bae1dSRodney W. Grimes break; 426df8bae1dSRodney W. Grimes } else { 427fc3fcacfSRobert Watson if (unp->unp_conn == NULL) { 428df8bae1dSRodney W. Grimes error = ENOTCONN; 429df8bae1dSRodney W. Grimes break; 430df8bae1dSRodney W. Grimes } 431df8bae1dSRodney W. Grimes } 432df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 433fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 43457bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 435df8bae1dSRodney W. Grimes else 436df8bae1dSRodney W. Grimes from = &sun_noname; 4376a2989fdSMatthew N. Dodd if (unp->unp_conn->unp_flags & UNP_WANTCRED) 4386a2989fdSMatthew N. Dodd control = unp_addsockcred(td, control); 439a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 440a34b7046SRobert Watson if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) { 4411e4d7da7SRobert Watson sorwakeup_locked(so2); 442fc3fcacfSRobert Watson m = NULL; 443fc3fcacfSRobert Watson control = NULL; 444e5aeaa0cSDag-Erling Smørgrav } else { 445a34b7046SRobert Watson SOCKBUF_UNLOCK(&so2->so_rcv); 446df8bae1dSRodney W. Grimes error = ENOBUFS; 447e5aeaa0cSDag-Erling Smørgrav } 448fc3fcacfSRobert Watson if (nam != NULL) 449df8bae1dSRodney W. Grimes unp_disconnect(unp); 450df8bae1dSRodney W. Grimes break; 451df8bae1dSRodney W. Grimes } 452df8bae1dSRodney W. Grimes 453df8bae1dSRodney W. Grimes case SOCK_STREAM: 4546b8fda4dSGarrett Wollman /* Connect if not connected yet. */ 4556b8fda4dSGarrett Wollman /* 4566b8fda4dSGarrett Wollman * Note: A better implementation would complain 457402cc72dSDavid Greenman * if not equal to the peer's address. 4586b8fda4dSGarrett Wollman */ 459402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 460fc3fcacfSRobert Watson if (nam != NULL) { 461b40ce416SJulian Elischer error = unp_connect(so, nam, td); 462402cc72dSDavid Greenman if (error) 4636b8fda4dSGarrett Wollman break; /* XXX */ 464402cc72dSDavid Greenman } else { 465402cc72dSDavid Greenman error = ENOTCONN; 466402cc72dSDavid Greenman break; 467402cc72dSDavid Greenman } 468402cc72dSDavid Greenman } 469402cc72dSDavid Greenman 4707abe2ac2SAlan Cox SOCKBUF_LOCK(&so->so_snd); 471c0b99ffaSRobert Watson if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 4727abe2ac2SAlan Cox SOCKBUF_UNLOCK(&so->so_snd); 473df8bae1dSRodney W. Grimes error = EPIPE; 474df8bae1dSRodney W. Grimes break; 475df8bae1dSRodney W. Grimes } 476fc3fcacfSRobert Watson if (unp->unp_conn == NULL) 477a29f300eSGarrett Wollman panic("uipc_send connected but no connection?"); 478df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 479a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 4806a2989fdSMatthew N. Dodd if (unp->unp_conn->unp_flags & UNP_WANTCRED) { 4816a2989fdSMatthew N. Dodd /* 4826a2989fdSMatthew N. Dodd * Credentials are passed only once on 4836a2989fdSMatthew N. Dodd * SOCK_STREAM. 4846a2989fdSMatthew N. Dodd */ 4856a2989fdSMatthew N. Dodd unp->unp_conn->unp_flags &= ~UNP_WANTCRED; 4866a2989fdSMatthew N. Dodd control = unp_addsockcred(td, control); 4876a2989fdSMatthew N. Dodd } 488df8bae1dSRodney W. Grimes /* 489df8bae1dSRodney W. Grimes * Send to paired receive port, and then reduce 490df8bae1dSRodney W. Grimes * send buffer hiwater marks to maintain backpressure. 491df8bae1dSRodney W. Grimes * Wake up readers. 492df8bae1dSRodney W. Grimes */ 493fc3fcacfSRobert Watson if (control != NULL) { 494a34b7046SRobert Watson if (sbappendcontrol_locked(&so2->so_rcv, m, control)) 495fc3fcacfSRobert Watson control = NULL; 496e5aeaa0cSDag-Erling Smørgrav } else { 497a34b7046SRobert Watson sbappend_locked(&so2->so_rcv, m); 498e5aeaa0cSDag-Erling Smørgrav } 499ff8b0106SBrian Feldman so->so_snd.sb_mbmax -= 500ff8b0106SBrian Feldman so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt; 501ff8b0106SBrian Feldman unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt; 5026aef685fSBrian Feldman newhiwat = so->so_snd.sb_hiwat - 5036aef685fSBrian Feldman (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc); 504f535380cSDon Lewis (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat, 5056aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 5067abe2ac2SAlan Cox SOCKBUF_UNLOCK(&so->so_snd); 507ff8b0106SBrian Feldman unp->unp_conn->unp_cc = so2->so_rcv.sb_cc; 5081e4d7da7SRobert Watson sorwakeup_locked(so2); 509fc3fcacfSRobert Watson m = NULL; 510df8bae1dSRodney W. Grimes break; 511df8bae1dSRodney W. Grimes 512df8bae1dSRodney W. Grimes default: 513a29f300eSGarrett Wollman panic("uipc_send unknown socktype"); 514df8bae1dSRodney W. Grimes } 515a29f300eSGarrett Wollman 5166b8fda4dSGarrett Wollman /* 5176b8fda4dSGarrett Wollman * SEND_EOF is equivalent to a SEND followed by 5186b8fda4dSGarrett Wollman * a SHUTDOWN. 5196b8fda4dSGarrett Wollman */ 520a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 5216b8fda4dSGarrett Wollman socantsendmore(so); 5226b8fda4dSGarrett Wollman unp_shutdown(unp); 5236b8fda4dSGarrett Wollman } 5240d9ce3a1SRobert Watson UNP_UNLOCK(); 525df8bae1dSRodney W. Grimes 52640f2ac28SRobert Watson dispose_release: 527fc3fcacfSRobert Watson if (control != NULL && error != 0) 528bd508d39SDon Lewis unp_dispose(control); 529bd508d39SDon Lewis 530a29f300eSGarrett Wollman release: 531fc3fcacfSRobert Watson if (control != NULL) 532a29f300eSGarrett Wollman m_freem(control); 533fc3fcacfSRobert Watson if (m != NULL) 534a29f300eSGarrett Wollman m_freem(m); 535e5aeaa0cSDag-Erling Smørgrav return (error); 536a29f300eSGarrett Wollman } 537df8bae1dSRodney W. Grimes 538a29f300eSGarrett Wollman static int 539a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 540a29f300eSGarrett Wollman { 54140f2ac28SRobert Watson struct unpcb *unp; 542a29f300eSGarrett Wollman struct socket *so2; 543a29f300eSGarrett Wollman 5440d9ce3a1SRobert Watson UNP_LOCK(); 54540f2ac28SRobert Watson unp = sotounpcb(so); 54640f2ac28SRobert Watson if (unp == NULL) { 54740f2ac28SRobert Watson UNP_UNLOCK(); 54840f2ac28SRobert Watson return (EINVAL); 54940f2ac28SRobert Watson } 550a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 551fc3fcacfSRobert Watson if (so->so_type == SOCK_STREAM && unp->unp_conn != NULL) { 552df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 553a29f300eSGarrett Wollman sb->st_blksize += so2->so_rcv.sb_cc; 554df8bae1dSRodney W. Grimes } 555f3732fd1SPoul-Henning Kamp sb->st_dev = NODEV; 556df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 5576f782c46SJeffrey Hsu unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 558a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 5590d9ce3a1SRobert Watson UNP_UNLOCK(); 560df8bae1dSRodney W. Grimes return (0); 561a29f300eSGarrett Wollman } 562df8bae1dSRodney W. Grimes 563a29f300eSGarrett Wollman static int 564a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 565a29f300eSGarrett Wollman { 56640f2ac28SRobert Watson struct unpcb *unp; 567df8bae1dSRodney W. Grimes 5680d9ce3a1SRobert Watson UNP_LOCK(); 56940f2ac28SRobert Watson unp = sotounpcb(so); 57040f2ac28SRobert Watson if (unp == NULL) { 57140f2ac28SRobert Watson UNP_UNLOCK(); 57240f2ac28SRobert Watson return (EINVAL); 57340f2ac28SRobert Watson } 574a29f300eSGarrett Wollman socantsendmore(so); 575a29f300eSGarrett Wollman unp_shutdown(unp); 5760d9ce3a1SRobert Watson UNP_UNLOCK(); 577e5aeaa0cSDag-Erling Smørgrav return (0); 578a29f300eSGarrett Wollman } 579df8bae1dSRodney W. Grimes 580a29f300eSGarrett Wollman static int 58157bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 582a29f300eSGarrett Wollman { 58340f2ac28SRobert Watson struct unpcb *unp; 5840d9ce3a1SRobert Watson const struct sockaddr *sa; 585a29f300eSGarrett Wollman 5860d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 5870d9ce3a1SRobert Watson UNP_LOCK(); 58840f2ac28SRobert Watson unp = sotounpcb(so); 58940f2ac28SRobert Watson if (unp == NULL) { 59040f2ac28SRobert Watson UNP_UNLOCK(); 59140f2ac28SRobert Watson free(*nam, M_SONAME); 59240f2ac28SRobert Watson *nam = NULL; 59340f2ac28SRobert Watson return (EINVAL); 59440f2ac28SRobert Watson } 595fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 5960d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_addr; 59783f3198bSThomas Moestl else 5980d9ce3a1SRobert Watson sa = &sun_noname; 5990d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 6000d9ce3a1SRobert Watson UNP_UNLOCK(); 601e5aeaa0cSDag-Erling Smørgrav return (0); 602df8bae1dSRodney W. Grimes } 603a29f300eSGarrett Wollman 604a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = { 605756d52a1SPoul-Henning Kamp .pru_abort = uipc_abort, 606756d52a1SPoul-Henning Kamp .pru_accept = uipc_accept, 607756d52a1SPoul-Henning Kamp .pru_attach = uipc_attach, 608756d52a1SPoul-Henning Kamp .pru_bind = uipc_bind, 609756d52a1SPoul-Henning Kamp .pru_connect = uipc_connect, 610756d52a1SPoul-Henning Kamp .pru_connect2 = uipc_connect2, 611756d52a1SPoul-Henning Kamp .pru_detach = uipc_detach, 612756d52a1SPoul-Henning Kamp .pru_disconnect = uipc_disconnect, 613756d52a1SPoul-Henning Kamp .pru_listen = uipc_listen, 614756d52a1SPoul-Henning Kamp .pru_peeraddr = uipc_peeraddr, 615756d52a1SPoul-Henning Kamp .pru_rcvd = uipc_rcvd, 616756d52a1SPoul-Henning Kamp .pru_send = uipc_send, 617756d52a1SPoul-Henning Kamp .pru_sense = uipc_sense, 618756d52a1SPoul-Henning Kamp .pru_shutdown = uipc_shutdown, 619756d52a1SPoul-Henning Kamp .pru_sockaddr = uipc_sockaddr, 620756d52a1SPoul-Henning Kamp .pru_sosend = sosend, 621756d52a1SPoul-Henning Kamp .pru_soreceive = soreceive, 622756d52a1SPoul-Henning Kamp .pru_sopoll = sopoll, 623a29f300eSGarrett Wollman }; 624df8bae1dSRodney W. Grimes 6250c1bb4fbSDima Dorfman int 626892af6b9SRobert Watson uipc_ctloutput(struct socket *so, struct sockopt *sopt) 6270c1bb4fbSDima Dorfman { 62840f2ac28SRobert Watson struct unpcb *unp; 6290d9ce3a1SRobert Watson struct xucred xu; 6306a2989fdSMatthew N. Dodd int error, optval; 6316a2989fdSMatthew N. Dodd 63296a041b5SMatthew N. Dodd if (sopt->sopt_level != 0) 63396a041b5SMatthew N. Dodd return (EINVAL); 63496a041b5SMatthew N. Dodd 6356a2989fdSMatthew N. Dodd UNP_LOCK(); 6366a2989fdSMatthew N. Dodd unp = sotounpcb(so); 6376a2989fdSMatthew N. Dodd if (unp == NULL) { 6386a2989fdSMatthew N. Dodd UNP_UNLOCK(); 6396a2989fdSMatthew N. Dodd return (EINVAL); 6406a2989fdSMatthew N. Dodd } 6416a2989fdSMatthew N. Dodd error = 0; 6420c1bb4fbSDima Dorfman 6430c1bb4fbSDima Dorfman switch (sopt->sopt_dir) { 6440c1bb4fbSDima Dorfman case SOPT_GET: 6450c1bb4fbSDima Dorfman switch (sopt->sopt_name) { 6460c1bb4fbSDima Dorfman case LOCAL_PEERCRED: 6470c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC) 6480d9ce3a1SRobert Watson xu = unp->unp_peercred; 6490c1bb4fbSDima Dorfman else { 6500c1bb4fbSDima Dorfman if (so->so_type == SOCK_STREAM) 6510c1bb4fbSDima Dorfman error = ENOTCONN; 6520c1bb4fbSDima Dorfman else 6530c1bb4fbSDima Dorfman error = EINVAL; 6540c1bb4fbSDima Dorfman } 6550d9ce3a1SRobert Watson if (error == 0) 6560d9ce3a1SRobert Watson error = sooptcopyout(sopt, &xu, sizeof(xu)); 6570c1bb4fbSDima Dorfman break; 6586a2989fdSMatthew N. Dodd case LOCAL_CREDS: 6596a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0; 6606a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 6616a2989fdSMatthew N. Dodd break; 6626a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 6636a2989fdSMatthew N. Dodd optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0; 6646a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval)); 6656a2989fdSMatthew N. Dodd break; 6660c1bb4fbSDima Dorfman default: 6670c1bb4fbSDima Dorfman error = EOPNOTSUPP; 6680c1bb4fbSDima Dorfman break; 6690c1bb4fbSDima Dorfman } 6700c1bb4fbSDima Dorfman break; 6710c1bb4fbSDima Dorfman case SOPT_SET: 6726a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 6736a2989fdSMatthew N. Dodd case LOCAL_CREDS: 6746a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 6756a2989fdSMatthew N. Dodd error = sooptcopyin(sopt, &optval, sizeof(optval), 6766a2989fdSMatthew N. Dodd sizeof(optval)); 6776a2989fdSMatthew N. Dodd if (error) 6786a2989fdSMatthew N. Dodd break; 6796a2989fdSMatthew N. Dodd 6806a2989fdSMatthew N. Dodd #define OPTSET(bit) \ 6816a2989fdSMatthew N. Dodd if (optval) \ 6826a2989fdSMatthew N. Dodd unp->unp_flags |= bit; \ 6836a2989fdSMatthew N. Dodd else \ 6846a2989fdSMatthew N. Dodd unp->unp_flags &= ~bit; 6856a2989fdSMatthew N. Dodd 6866a2989fdSMatthew N. Dodd switch (sopt->sopt_name) { 6876a2989fdSMatthew N. Dodd case LOCAL_CREDS: 6886a2989fdSMatthew N. Dodd OPTSET(UNP_WANTCRED); 6896a2989fdSMatthew N. Dodd break; 6906a2989fdSMatthew N. Dodd case LOCAL_CONNWAIT: 6916a2989fdSMatthew N. Dodd OPTSET(UNP_CONNWAIT); 6926a2989fdSMatthew N. Dodd break; 6936a2989fdSMatthew N. Dodd default: 6946a2989fdSMatthew N. Dodd break; 6956a2989fdSMatthew N. Dodd } 6966a2989fdSMatthew N. Dodd break; 6976a2989fdSMatthew N. Dodd #undef OPTSET 6986a2989fdSMatthew N. Dodd default: 6996a2989fdSMatthew N. Dodd error = ENOPROTOOPT; 7006a2989fdSMatthew N. Dodd break; 7016a2989fdSMatthew N. Dodd } 702abb886faSMatthew N. Dodd break; 7030c1bb4fbSDima Dorfman default: 7040c1bb4fbSDima Dorfman error = EOPNOTSUPP; 7050c1bb4fbSDima Dorfman break; 7060c1bb4fbSDima Dorfman } 7076a2989fdSMatthew N. Dodd UNP_UNLOCK(); 7080c1bb4fbSDima Dorfman return (error); 7090c1bb4fbSDima Dorfman } 7100c1bb4fbSDima Dorfman 711df8bae1dSRodney W. Grimes /* 712df8bae1dSRodney W. Grimes * Both send and receive buffers are allocated PIPSIZ bytes of buffering 713df8bae1dSRodney W. Grimes * for stream sockets, although the total for sender and receiver is 714df8bae1dSRodney W. Grimes * actually only PIPSIZ. 715df8bae1dSRodney W. Grimes * Datagram sockets really use the sendspace as the maximum datagram size, 716df8bae1dSRodney W. Grimes * and don't really want to reserve the sendspace. Their recvspace should 717df8bae1dSRodney W. Grimes * be large enough for at least one max-size datagram plus address. 718df8bae1dSRodney W. Grimes */ 7195dce41c5SJohn Dyson #ifndef PIPSIZ 7205dce41c5SJohn Dyson #define PIPSIZ 8192 7215dce41c5SJohn Dyson #endif 722f708ef1bSPoul-Henning Kamp static u_long unpst_sendspace = PIPSIZ; 723f708ef1bSPoul-Henning Kamp static u_long unpst_recvspace = PIPSIZ; 724f708ef1bSPoul-Henning Kamp static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 725f708ef1bSPoul-Henning Kamp static u_long unpdg_recvspace = 4*1024; 726df8bae1dSRodney W. Grimes 727f708ef1bSPoul-Henning Kamp static int unp_rights; /* file descriptors in flight */ 728df8bae1dSRodney W. Grimes 729ce02431fSDoug Rabson SYSCTL_DECL(_net_local_stream); 730e59898ffSMaxime Henrion SYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 731639acc13SGarrett Wollman &unpst_sendspace, 0, ""); 732e59898ffSMaxime Henrion SYSCTL_ULONG(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 733639acc13SGarrett Wollman &unpst_recvspace, 0, ""); 734ce02431fSDoug Rabson SYSCTL_DECL(_net_local_dgram); 735e59898ffSMaxime Henrion SYSCTL_ULONG(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 736639acc13SGarrett Wollman &unpdg_sendspace, 0, ""); 737e59898ffSMaxime Henrion SYSCTL_ULONG(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 738639acc13SGarrett Wollman &unpdg_recvspace, 0, ""); 739ce02431fSDoug Rabson SYSCTL_DECL(_net_local); 740639acc13SGarrett Wollman SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); 741639acc13SGarrett Wollman 742f708ef1bSPoul-Henning Kamp static int 743892af6b9SRobert Watson unp_attach(struct socket *so) 744df8bae1dSRodney W. Grimes { 745892af6b9SRobert Watson struct unpcb *unp; 746df8bae1dSRodney W. Grimes int error; 747df8bae1dSRodney W. Grimes 748df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 749df8bae1dSRodney W. Grimes switch (so->so_type) { 750df8bae1dSRodney W. Grimes 751df8bae1dSRodney W. Grimes case SOCK_STREAM: 752df8bae1dSRodney W. Grimes error = soreserve(so, unpst_sendspace, unpst_recvspace); 753df8bae1dSRodney W. Grimes break; 754df8bae1dSRodney W. Grimes 755df8bae1dSRodney W. Grimes case SOCK_DGRAM: 756df8bae1dSRodney W. Grimes error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 757df8bae1dSRodney W. Grimes break; 758df8bae1dSRodney W. Grimes 759df8bae1dSRodney W. Grimes default: 760df8bae1dSRodney W. Grimes panic("unp_attach"); 761df8bae1dSRodney W. Grimes } 762df8bae1dSRodney W. Grimes if (error) 763df8bae1dSRodney W. Grimes return (error); 764df8bae1dSRodney W. Grimes } 765d664e4faSRobert Watson unp = uma_zalloc(unp_zone, M_WAITOK | M_ZERO); 76657bf258eSGarrett Wollman if (unp == NULL) 767df8bae1dSRodney W. Grimes return (ENOBUFS); 76898271db4SGarrett Wollman LIST_INIT(&unp->unp_refs); 769df8bae1dSRodney W. Grimes unp->unp_socket = so; 7707301cf23SRobert Watson so->so_pcb = unp; 7710d9ce3a1SRobert Watson 7720d9ce3a1SRobert Watson UNP_LOCK(); 7730d9ce3a1SRobert Watson unp->unp_gencnt = ++unp_gencnt; 7740d9ce3a1SRobert Watson unp_count++; 77598271db4SGarrett Wollman LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead 77698271db4SGarrett Wollman : &unp_shead, unp, unp_link); 7770d9ce3a1SRobert Watson UNP_UNLOCK(); 7780d9ce3a1SRobert Watson 779df8bae1dSRodney W. Grimes return (0); 780df8bae1dSRodney W. Grimes } 781df8bae1dSRodney W. Grimes 782f708ef1bSPoul-Henning Kamp static void 783892af6b9SRobert Watson unp_detach(struct unpcb *unp) 784df8bae1dSRodney W. Grimes { 7850d9ce3a1SRobert Watson struct vnode *vp; 786a0ec558aSRobert Watson int local_unp_rights; 7870d9ce3a1SRobert Watson 7880d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 7890d9ce3a1SRobert Watson 79098271db4SGarrett Wollman LIST_REMOVE(unp, unp_link); 79198271db4SGarrett Wollman unp->unp_gencnt = ++unp_gencnt; 79298271db4SGarrett Wollman --unp_count; 7930d9ce3a1SRobert Watson if ((vp = unp->unp_vnode) != NULL) { 7940d9ce3a1SRobert Watson /* 7950d9ce3a1SRobert Watson * XXXRW: should v_socket be frobbed only while holding 7960d9ce3a1SRobert Watson * Giant? 7970d9ce3a1SRobert Watson */ 798fc3fcacfSRobert Watson unp->unp_vnode->v_socket = NULL; 799fc3fcacfSRobert Watson unp->unp_vnode = NULL; 800df8bae1dSRodney W. Grimes } 801fc3fcacfSRobert Watson if (unp->unp_conn != NULL) 802df8bae1dSRodney W. Grimes unp_disconnect(unp); 8030d9ce3a1SRobert Watson while (!LIST_EMPTY(&unp->unp_refs)) { 8040d9ce3a1SRobert Watson struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 8050d9ce3a1SRobert Watson unp_drop(ref, ECONNRESET); 8060d9ce3a1SRobert Watson } 807df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 808fc3fcacfSRobert Watson unp->unp_socket->so_pcb = NULL; 809a0ec558aSRobert Watson local_unp_rights = unp_rights; 810a5993a97SRobert Watson UNP_UNLOCK(); 811fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 81257bf258eSGarrett Wollman FREE(unp->unp_addr, M_SONAME); 8139e9d298aSJeff Roberson uma_zfree(unp_zone, unp); 8140d9ce3a1SRobert Watson if (vp) { 815033eb86eSJeff Roberson int vfslocked; 816033eb86eSJeff Roberson 817033eb86eSJeff Roberson vfslocked = VFS_LOCK_GIANT(vp->v_mount); 8180d9ce3a1SRobert Watson vrele(vp); 819033eb86eSJeff Roberson VFS_UNLOCK_GIANT(vfslocked); 8200d9ce3a1SRobert Watson } 821a0ec558aSRobert Watson if (local_unp_rights) 822a0ec558aSRobert Watson taskqueue_enqueue(taskqueue_thread, &unp_gc_task); 823df8bae1dSRodney W. Grimes } 824df8bae1dSRodney W. Grimes 825f708ef1bSPoul-Henning Kamp static int 826892af6b9SRobert Watson unp_bind(struct unpcb *unp, struct sockaddr *nam, struct thread *td) 827df8bae1dSRodney W. Grimes { 82857bf258eSGarrett Wollman struct sockaddr_un *soun = (struct sockaddr_un *)nam; 829f2a2857bSKirk McKusick struct vnode *vp; 830f2a2857bSKirk McKusick struct mount *mp; 831df8bae1dSRodney W. Grimes struct vattr vattr; 83257bf258eSGarrett Wollman int error, namelen; 833df8bae1dSRodney W. Grimes struct nameidata nd; 8348f364875SJulian Elischer char *buf; 835df8bae1dSRodney W. Grimes 83640f2ac28SRobert Watson UNP_LOCK_ASSERT(); 83740f2ac28SRobert Watson 8380d9ce3a1SRobert Watson /* 8390d9ce3a1SRobert Watson * XXXRW: This test-and-set of unp_vnode is non-atomic; the 8400d9ce3a1SRobert Watson * unlocked read here is fine, but the value of unp_vnode needs 8410d9ce3a1SRobert Watson * to be tested again after we do all the lookups to see if the 8420d9ce3a1SRobert Watson * pcb is still unbound? 8430d9ce3a1SRobert Watson */ 844df8bae1dSRodney W. Grimes if (unp->unp_vnode != NULL) 845df8bae1dSRodney W. Grimes return (EINVAL); 84655c85568SRobert Drehmel 84757bf258eSGarrett Wollman namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 84857bf258eSGarrett Wollman if (namelen <= 0) 849e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 85055c85568SRobert Drehmel 85140f2ac28SRobert Watson UNP_UNLOCK(); 85240f2ac28SRobert Watson 853a163d034SWarner Losh buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 85455c85568SRobert Drehmel strlcpy(buf, soun->sun_path, namelen + 1); 85555c85568SRobert Drehmel 8560d9ce3a1SRobert Watson mtx_lock(&Giant); 857f2a2857bSKirk McKusick restart: 8580d9ce3a1SRobert Watson mtx_assert(&Giant, MA_OWNED); 859b65f6f6bSRobert Watson NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME, UIO_SYSSPACE, 860b40ce416SJulian Elischer buf, td); 861df8bae1dSRodney W. Grimes /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 862797f2d22SPoul-Henning Kamp error = namei(&nd); 8630d9ce3a1SRobert Watson if (error) 8640d9ce3a1SRobert Watson goto done; 865df8bae1dSRodney W. Grimes vp = nd.ni_vp; 866f2a2857bSKirk McKusick if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 867762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 868df8bae1dSRodney W. Grimes if (nd.ni_dvp == vp) 869df8bae1dSRodney W. Grimes vrele(nd.ni_dvp); 870df8bae1dSRodney W. Grimes else 871df8bae1dSRodney W. Grimes vput(nd.ni_dvp); 872f2a2857bSKirk McKusick if (vp != NULL) { 873df8bae1dSRodney W. Grimes vrele(vp); 8740d9ce3a1SRobert Watson error = EADDRINUSE; 8750d9ce3a1SRobert Watson goto done; 876df8bae1dSRodney W. Grimes } 8778f364875SJulian Elischer error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 8780d9ce3a1SRobert Watson if (error) 8790d9ce3a1SRobert Watson goto done; 880f2a2857bSKirk McKusick goto restart; 881f2a2857bSKirk McKusick } 882df8bae1dSRodney W. Grimes VATTR_NULL(&vattr); 883df8bae1dSRodney W. Grimes vattr.va_type = VSOCK; 884b40ce416SJulian Elischer vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 8856ea48a90SRobert Watson #ifdef MAC 8866ea48a90SRobert Watson error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 8876ea48a90SRobert Watson &vattr); 8886151efaaSRobert Watson #endif 8896ea48a90SRobert Watson if (error == 0) { 890a854ed98SJohn Baldwin VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE); 8917be2d300SMike Smith error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 8926ea48a90SRobert Watson } 893762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 8947be2d300SMike Smith vput(nd.ni_dvp); 895c364c823SRobert Watson if (error) { 896c364c823SRobert Watson vn_finished_write(mp); 8970d9ce3a1SRobert Watson goto done; 898c364c823SRobert Watson } 899df8bae1dSRodney W. Grimes vp = nd.ni_vp; 9000d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_bind"); 9010d9ce3a1SRobert Watson soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 9020d9ce3a1SRobert Watson UNP_LOCK(); 903df8bae1dSRodney W. Grimes vp->v_socket = unp->unp_socket; 904df8bae1dSRodney W. Grimes unp->unp_vnode = vp; 9050d9ce3a1SRobert Watson unp->unp_addr = soun; 9060d9ce3a1SRobert Watson UNP_UNLOCK(); 907b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 908f2a2857bSKirk McKusick vn_finished_write(mp); 9090d9ce3a1SRobert Watson done: 9100d9ce3a1SRobert Watson mtx_unlock(&Giant); 9118f364875SJulian Elischer free(buf, M_TEMP); 91240f2ac28SRobert Watson UNP_LOCK(); 9130d9ce3a1SRobert Watson return (error); 914df8bae1dSRodney W. Grimes } 915df8bae1dSRodney W. Grimes 916f708ef1bSPoul-Henning Kamp static int 917892af6b9SRobert Watson unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 918df8bae1dSRodney W. Grimes { 919892af6b9SRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam; 920892af6b9SRobert Watson struct vnode *vp; 921892af6b9SRobert Watson struct socket *so2, *so3; 922b295bdcdSRobert Watson struct unpcb *unp, *unp2, *unp3; 92357bf258eSGarrett Wollman int error, len; 924df8bae1dSRodney W. Grimes struct nameidata nd; 92557bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 9260d9ce3a1SRobert Watson struct sockaddr *sa; 9270d9ce3a1SRobert Watson 9280d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 929b295bdcdSRobert Watson unp = sotounpcb(so); 930df8bae1dSRodney W. Grimes 93157bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 93257bf258eSGarrett Wollman if (len <= 0) 933e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 93455c85568SRobert Drehmel strlcpy(buf, soun->sun_path, len + 1); 9350d9ce3a1SRobert Watson UNP_UNLOCK(); 9360d9ce3a1SRobert Watson sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 9370d9ce3a1SRobert Watson mtx_lock(&Giant); 938b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td); 939797f2d22SPoul-Henning Kamp error = namei(&nd); 940797f2d22SPoul-Henning Kamp if (error) 9410d9ce3a1SRobert Watson vp = NULL; 9420d9ce3a1SRobert Watson else 943df8bae1dSRodney W. Grimes vp = nd.ni_vp; 9440d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_connect"); 945762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 9460d9ce3a1SRobert Watson if (error) 9470d9ce3a1SRobert Watson goto bad; 9480d9ce3a1SRobert Watson 949df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 950df8bae1dSRodney W. Grimes error = ENOTSOCK; 951df8bae1dSRodney W. Grimes goto bad; 952df8bae1dSRodney W. Grimes } 953a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 954797f2d22SPoul-Henning Kamp if (error) 955df8bae1dSRodney W. Grimes goto bad; 9562260c03dSRobert Watson mtx_unlock(&Giant); 9572260c03dSRobert Watson UNP_LOCK(); 958b295bdcdSRobert Watson unp = sotounpcb(so); 959b295bdcdSRobert Watson if (unp == NULL) { 960b295bdcdSRobert Watson error = EINVAL; 961b295bdcdSRobert Watson goto bad2; 962b295bdcdSRobert Watson } 963df8bae1dSRodney W. Grimes so2 = vp->v_socket; 964fc3fcacfSRobert Watson if (so2 == NULL) { 965df8bae1dSRodney W. Grimes error = ECONNREFUSED; 9662260c03dSRobert Watson goto bad2; 967df8bae1dSRodney W. Grimes } 968df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 969df8bae1dSRodney W. Grimes error = EPROTOTYPE; 9702260c03dSRobert Watson goto bad2; 971df8bae1dSRodney W. Grimes } 972df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 9730d9ce3a1SRobert Watson if (so2->so_options & SO_ACCEPTCONN) { 9740d9ce3a1SRobert Watson /* 9750d9ce3a1SRobert Watson * NB: drop locks here so unp_attach is entered 9760d9ce3a1SRobert Watson * w/o locks; this avoids a recursive lock 9770d9ce3a1SRobert Watson * of the head and holding sleep locks across 9780d9ce3a1SRobert Watson * a (potentially) blocking malloc. 9790d9ce3a1SRobert Watson */ 9800d9ce3a1SRobert Watson UNP_UNLOCK(); 9810d9ce3a1SRobert Watson so3 = sonewconn(so2, 0); 9820d9ce3a1SRobert Watson UNP_LOCK(); 9830d9ce3a1SRobert Watson } else 9840d9ce3a1SRobert Watson so3 = NULL; 9850d9ce3a1SRobert Watson if (so3 == NULL) { 986df8bae1dSRodney W. Grimes error = ECONNREFUSED; 9870d9ce3a1SRobert Watson goto bad2; 988df8bae1dSRodney W. Grimes } 9890c1bb4fbSDima Dorfman unp = sotounpcb(so); 990df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 991df8bae1dSRodney W. Grimes unp3 = sotounpcb(so3); 9920d9ce3a1SRobert Watson if (unp2->unp_addr != NULL) { 9930d9ce3a1SRobert Watson bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 9940d9ce3a1SRobert Watson unp3->unp_addr = (struct sockaddr_un *) sa; 9950d9ce3a1SRobert Watson sa = NULL; 9960d9ce3a1SRobert Watson } 9970c1bb4fbSDima Dorfman /* 9980c1bb4fbSDima Dorfman * unp_peercred management: 9990c1bb4fbSDima Dorfman * 10000c1bb4fbSDima Dorfman * The connecter's (client's) credentials are copied 10010c1bb4fbSDima Dorfman * from its process structure at the time of connect() 10020c1bb4fbSDima Dorfman * (which is now). 10030c1bb4fbSDima Dorfman */ 1004a854ed98SJohn Baldwin cru2x(td->td_ucred, &unp3->unp_peercred); 10050c1bb4fbSDima Dorfman unp3->unp_flags |= UNP_HAVEPC; 10060c1bb4fbSDima Dorfman /* 10070c1bb4fbSDima Dorfman * The receiver's (server's) credentials are copied 10080c1bb4fbSDima Dorfman * from the unp_peercred member of socket on which the 10090c1bb4fbSDima Dorfman * former called listen(); unp_listen() cached that 10100c1bb4fbSDima Dorfman * process's credentials at that time so we can use 10110c1bb4fbSDima Dorfman * them now. 10120c1bb4fbSDima Dorfman */ 10130c1bb4fbSDima Dorfman KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 10140c1bb4fbSDima Dorfman ("unp_connect: listener without cached peercred")); 10150c1bb4fbSDima Dorfman memcpy(&unp->unp_peercred, &unp2->unp_peercred, 10160c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 10170c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPC; 1018335654d7SRobert Watson #ifdef MAC 1019310e7cebSRobert Watson SOCK_LOCK(so); 1020335654d7SRobert Watson mac_set_socket_peer_from_socket(so, so3); 1021335654d7SRobert Watson mac_set_socket_peer_from_socket(so3, so); 1022310e7cebSRobert Watson SOCK_UNLOCK(so); 1023335654d7SRobert Watson #endif 10240c1bb4fbSDima Dorfman 1025df8bae1dSRodney W. Grimes so2 = so3; 1026df8bae1dSRodney W. Grimes } 10276a2989fdSMatthew N. Dodd error = unp_connect2(so, so2, PRU_CONNECT); 10280d9ce3a1SRobert Watson bad2: 10290d9ce3a1SRobert Watson UNP_UNLOCK(); 10300d9ce3a1SRobert Watson mtx_lock(&Giant); 1031df8bae1dSRodney W. Grimes bad: 10320d9ce3a1SRobert Watson mtx_assert(&Giant, MA_OWNED); 10330d9ce3a1SRobert Watson if (vp != NULL) 1034df8bae1dSRodney W. Grimes vput(vp); 10350d9ce3a1SRobert Watson mtx_unlock(&Giant); 10360d9ce3a1SRobert Watson free(sa, M_SONAME); 10370d9ce3a1SRobert Watson UNP_LOCK(); 1038df8bae1dSRodney W. Grimes return (error); 1039df8bae1dSRodney W. Grimes } 1040df8bae1dSRodney W. Grimes 1041db48c0d2SRobert Watson static int 10426a2989fdSMatthew N. Dodd unp_connect2(struct socket *so, struct socket *so2, int req) 1043df8bae1dSRodney W. Grimes { 1044892af6b9SRobert Watson struct unpcb *unp = sotounpcb(so); 1045892af6b9SRobert Watson struct unpcb *unp2; 1046df8bae1dSRodney W. Grimes 10470d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 10480d9ce3a1SRobert Watson 1049df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 1050df8bae1dSRodney W. Grimes return (EPROTOTYPE); 1051df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 1052df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 1053df8bae1dSRodney W. Grimes switch (so->so_type) { 1054df8bae1dSRodney W. Grimes 1055df8bae1dSRodney W. Grimes case SOCK_DGRAM: 105698271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 1057df8bae1dSRodney W. Grimes soisconnected(so); 1058df8bae1dSRodney W. Grimes break; 1059df8bae1dSRodney W. Grimes 1060df8bae1dSRodney W. Grimes case SOCK_STREAM: 1061df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 10626a2989fdSMatthew N. Dodd if (req == PRU_CONNECT && 10636a2989fdSMatthew N. Dodd ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT)) 10646a2989fdSMatthew N. Dodd soisconnecting(so); 10656a2989fdSMatthew N. Dodd else 1066df8bae1dSRodney W. Grimes soisconnected(so); 1067df8bae1dSRodney W. Grimes soisconnected(so2); 1068df8bae1dSRodney W. Grimes break; 1069df8bae1dSRodney W. Grimes 1070df8bae1dSRodney W. Grimes default: 1071df8bae1dSRodney W. Grimes panic("unp_connect2"); 1072df8bae1dSRodney W. Grimes } 1073df8bae1dSRodney W. Grimes return (0); 1074df8bae1dSRodney W. Grimes } 1075df8bae1dSRodney W. Grimes 1076f708ef1bSPoul-Henning Kamp static void 1077892af6b9SRobert Watson unp_disconnect(struct unpcb *unp) 1078df8bae1dSRodney W. Grimes { 1079892af6b9SRobert Watson struct unpcb *unp2 = unp->unp_conn; 10801b2e3b4bSRobert Watson struct socket *so; 1081df8bae1dSRodney W. Grimes 10820d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 10830d9ce3a1SRobert Watson 1084fc3fcacfSRobert Watson if (unp2 == NULL) 1085df8bae1dSRodney W. Grimes return; 1086fc3fcacfSRobert Watson unp->unp_conn = NULL; 1087df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 1088df8bae1dSRodney W. Grimes 1089df8bae1dSRodney W. Grimes case SOCK_DGRAM: 109098271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 10911b2e3b4bSRobert Watson so = unp->unp_socket; 10921b2e3b4bSRobert Watson SOCK_LOCK(so); 10931b2e3b4bSRobert Watson so->so_state &= ~SS_ISCONNECTED; 10941b2e3b4bSRobert Watson SOCK_UNLOCK(so); 1095df8bae1dSRodney W. Grimes break; 1096df8bae1dSRodney W. Grimes 1097df8bae1dSRodney W. Grimes case SOCK_STREAM: 1098df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 1099fc3fcacfSRobert Watson unp2->unp_conn = NULL; 1100df8bae1dSRodney W. Grimes soisdisconnected(unp2->unp_socket); 1101df8bae1dSRodney W. Grimes break; 1102df8bae1dSRodney W. Grimes } 1103df8bae1dSRodney W. Grimes } 1104df8bae1dSRodney W. Grimes 1105df8bae1dSRodney W. Grimes #ifdef notdef 110626f9a767SRodney W. Grimes void 1107892af6b9SRobert Watson unp_abort(struct unpcb *unp) 1108df8bae1dSRodney W. Grimes { 1109df8bae1dSRodney W. Grimes 1110df8bae1dSRodney W. Grimes unp_detach(unp); 11114c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 1112df8bae1dSRodney W. Grimes } 1113df8bae1dSRodney W. Grimes #endif 1114df8bae1dSRodney W. Grimes 11150d9ce3a1SRobert Watson /* 11160d9ce3a1SRobert Watson * unp_pcblist() assumes that UNIX domain socket memory is never reclaimed 11170d9ce3a1SRobert Watson * by the zone (UMA_ZONE_NOFREE), and as such potentially stale pointers 11180d9ce3a1SRobert Watson * are safe to reference. It first scans the list of struct unpcb's to 11190d9ce3a1SRobert Watson * generate a pointer list, then it rescans its list one entry at a time to 11200d9ce3a1SRobert Watson * externalize and copyout. It checks the generation number to see if a 11210d9ce3a1SRobert Watson * struct unpcb has been reused, and will skip it if so. 11220d9ce3a1SRobert Watson */ 112398271db4SGarrett Wollman static int 112482d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 112598271db4SGarrett Wollman { 1126f5ef029eSPoul-Henning Kamp int error, i, n; 112798271db4SGarrett Wollman struct unpcb *unp, **unp_list; 112898271db4SGarrett Wollman unp_gen_t gencnt; 11298f364875SJulian Elischer struct xunpgen *xug; 113098271db4SGarrett Wollman struct unp_head *head; 11318f364875SJulian Elischer struct xunpcb *xu; 113298271db4SGarrett Wollman 1133a23d65bfSBruce Evans head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 113498271db4SGarrett Wollman 113598271db4SGarrett Wollman /* 113698271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 113798271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 113898271db4SGarrett Wollman */ 1139fc3fcacfSRobert Watson if (req->oldptr == NULL) { 114098271db4SGarrett Wollman n = unp_count; 11418f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug) 114298271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 1143e5aeaa0cSDag-Erling Smørgrav return (0); 114498271db4SGarrett Wollman } 114598271db4SGarrett Wollman 1146fc3fcacfSRobert Watson if (req->newptr != NULL) 1147e5aeaa0cSDag-Erling Smørgrav return (EPERM); 114898271db4SGarrett Wollman 114998271db4SGarrett Wollman /* 115098271db4SGarrett Wollman * OK, now we're committed to doing something. 115198271db4SGarrett Wollman */ 1152a163d034SWarner Losh xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 11530d9ce3a1SRobert Watson UNP_LOCK(); 115498271db4SGarrett Wollman gencnt = unp_gencnt; 115598271db4SGarrett Wollman n = unp_count; 11560d9ce3a1SRobert Watson UNP_UNLOCK(); 115798271db4SGarrett Wollman 11588f364875SJulian Elischer xug->xug_len = sizeof *xug; 11598f364875SJulian Elischer xug->xug_count = n; 11608f364875SJulian Elischer xug->xug_gen = gencnt; 11618f364875SJulian Elischer xug->xug_sogen = so_gencnt; 11628f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 11638f364875SJulian Elischer if (error) { 11648f364875SJulian Elischer free(xug, M_TEMP); 1165e5aeaa0cSDag-Erling Smørgrav return (error); 11668f364875SJulian Elischer } 116798271db4SGarrett Wollman 1168a163d034SWarner Losh unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 116998271db4SGarrett Wollman 11700d9ce3a1SRobert Watson UNP_LOCK(); 11712e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 11722e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 11738a7d8cc6SRobert Watson if (unp->unp_gencnt <= gencnt) { 1174a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred, 11758a7d8cc6SRobert Watson unp->unp_socket->so_cred)) 11764787fd37SPaul Saab continue; 117798271db4SGarrett Wollman unp_list[i++] = unp; 117898271db4SGarrett Wollman } 11794787fd37SPaul Saab } 11800d9ce3a1SRobert Watson UNP_UNLOCK(); 118198271db4SGarrett Wollman n = i; /* in case we lost some during malloc */ 118298271db4SGarrett Wollman 118398271db4SGarrett Wollman error = 0; 1184fe2eee82SColin Percival xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO); 118598271db4SGarrett Wollman for (i = 0; i < n; i++) { 118698271db4SGarrett Wollman unp = unp_list[i]; 118798271db4SGarrett Wollman if (unp->unp_gencnt <= gencnt) { 11888f364875SJulian Elischer xu->xu_len = sizeof *xu; 11898f364875SJulian Elischer xu->xu_unpp = unp; 119098271db4SGarrett Wollman /* 119198271db4SGarrett Wollman * XXX - need more locking here to protect against 119298271db4SGarrett Wollman * connect/disconnect races for SMP. 119398271db4SGarrett Wollman */ 1194fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 11958f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr, 119698271db4SGarrett Wollman unp->unp_addr->sun_len); 1197fc3fcacfSRobert Watson if (unp->unp_conn != NULL && 1198fc3fcacfSRobert Watson unp->unp_conn->unp_addr != NULL) 119998271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 12008f364875SJulian Elischer &xu->xu_caddr, 120198271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 12028f364875SJulian Elischer bcopy(unp, &xu->xu_unp, sizeof *unp); 12038f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket); 12048f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu); 120598271db4SGarrett Wollman } 120698271db4SGarrett Wollman } 12078f364875SJulian Elischer free(xu, M_TEMP); 120898271db4SGarrett Wollman if (!error) { 120998271db4SGarrett Wollman /* 121098271db4SGarrett Wollman * Give the user an updated idea of our state. 121198271db4SGarrett Wollman * If the generation differs from what we told 121298271db4SGarrett Wollman * her before, she knows that something happened 121398271db4SGarrett Wollman * while we were processing this request, and it 121498271db4SGarrett Wollman * might be necessary to retry. 121598271db4SGarrett Wollman */ 12168f364875SJulian Elischer xug->xug_gen = unp_gencnt; 12178f364875SJulian Elischer xug->xug_sogen = so_gencnt; 12188f364875SJulian Elischer xug->xug_count = unp_count; 12198f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 122098271db4SGarrett Wollman } 122198271db4SGarrett Wollman free(unp_list, M_TEMP); 12228f364875SJulian Elischer free(xug, M_TEMP); 1223e5aeaa0cSDag-Erling Smørgrav return (error); 122498271db4SGarrett Wollman } 122598271db4SGarrett Wollman 122698271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 122798271db4SGarrett Wollman (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 122898271db4SGarrett Wollman "List of active local datagram sockets"); 122998271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 123098271db4SGarrett Wollman (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 123198271db4SGarrett Wollman "List of active local stream sockets"); 123298271db4SGarrett Wollman 1233f708ef1bSPoul-Henning Kamp static void 1234892af6b9SRobert Watson unp_shutdown(struct unpcb *unp) 1235df8bae1dSRodney W. Grimes { 1236df8bae1dSRodney W. Grimes struct socket *so; 1237df8bae1dSRodney W. Grimes 12380d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 12390d9ce3a1SRobert Watson 1240df8bae1dSRodney W. Grimes if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 1241df8bae1dSRodney W. Grimes (so = unp->unp_conn->unp_socket)) 1242df8bae1dSRodney W. Grimes socantrcvmore(so); 1243df8bae1dSRodney W. Grimes } 1244df8bae1dSRodney W. Grimes 1245f708ef1bSPoul-Henning Kamp static void 1246892af6b9SRobert Watson unp_drop(struct unpcb *unp, int errno) 1247df8bae1dSRodney W. Grimes { 1248df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 1249df8bae1dSRodney W. Grimes 12500d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 12510d9ce3a1SRobert Watson 1252df8bae1dSRodney W. Grimes so->so_error = errno; 1253df8bae1dSRodney W. Grimes unp_disconnect(unp); 1254df8bae1dSRodney W. Grimes } 1255df8bae1dSRodney W. Grimes 1256df8bae1dSRodney W. Grimes #ifdef notdef 125726f9a767SRodney W. Grimes void 1258892af6b9SRobert Watson unp_drain(void) 1259df8bae1dSRodney W. Grimes { 1260df8bae1dSRodney W. Grimes 1261df8bae1dSRodney W. Grimes } 1262df8bae1dSRodney W. Grimes #endif 1263df8bae1dSRodney W. Grimes 12642bc21ed9SDavid Malone static void 1265892af6b9SRobert Watson unp_freerights(struct file **rp, int fdcount) 1266df8bae1dSRodney W. Grimes { 12672bc21ed9SDavid Malone int i; 12682bc21ed9SDavid Malone struct file *fp; 1269df8bae1dSRodney W. Grimes 12702bc21ed9SDavid Malone for (i = 0; i < fdcount; i++) { 1271df8bae1dSRodney W. Grimes fp = *rp; 12728692c025SYoshinobu Inoue /* 12732bc21ed9SDavid Malone * zero the pointer before calling 12742bc21ed9SDavid Malone * unp_discard since it may end up 12752bc21ed9SDavid Malone * in unp_gc().. 1276d7dca903SRobert Watson * 1277d7dca903SRobert Watson * XXXRW: This is less true than it used to be. 12788692c025SYoshinobu Inoue */ 1279df8bae1dSRodney W. Grimes *rp++ = 0; 12808692c025SYoshinobu Inoue unp_discard(fp); 1281df8bae1dSRodney W. Grimes } 12822bc21ed9SDavid Malone } 12832bc21ed9SDavid Malone 12842bc21ed9SDavid Malone int 1285892af6b9SRobert Watson unp_externalize(struct mbuf *control, struct mbuf **controlp) 12862bc21ed9SDavid Malone { 12872bc21ed9SDavid Malone struct thread *td = curthread; /* XXX */ 12882bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 12892bc21ed9SDavid Malone int i; 12902bc21ed9SDavid Malone int *fdp; 12912bc21ed9SDavid Malone struct file **rp; 12922bc21ed9SDavid Malone struct file *fp; 12932bc21ed9SDavid Malone void *data; 12942bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 12952bc21ed9SDavid Malone int error, newfds; 12962bc21ed9SDavid Malone int f; 12972bc21ed9SDavid Malone u_int newlen; 12982bc21ed9SDavid Malone 12994c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 13004c5bc1caSRobert Watson 13012bc21ed9SDavid Malone error = 0; 13022bc21ed9SDavid Malone if (controlp != NULL) /* controlp == NULL => free control messages */ 13032bc21ed9SDavid Malone *controlp = NULL; 13042bc21ed9SDavid Malone 13052bc21ed9SDavid Malone while (cm != NULL) { 13062bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 13072bc21ed9SDavid Malone error = EINVAL; 13082bc21ed9SDavid Malone break; 13092bc21ed9SDavid Malone } 13102bc21ed9SDavid Malone 13112bc21ed9SDavid Malone data = CMSG_DATA(cm); 13122bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 13132bc21ed9SDavid Malone 13142bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET 13152bc21ed9SDavid Malone && cm->cmsg_type == SCM_RIGHTS) { 13162bc21ed9SDavid Malone newfds = datalen / sizeof(struct file *); 13172bc21ed9SDavid Malone rp = data; 13182bc21ed9SDavid Malone 1319e2f9a08bSOlivier Houchard /* If we're not outputting the descriptors free them. */ 13202bc21ed9SDavid Malone if (error || controlp == NULL) { 13212bc21ed9SDavid Malone unp_freerights(rp, newfds); 13222bc21ed9SDavid Malone goto next; 13232bc21ed9SDavid Malone } 1324426da3bcSAlfred Perlstein FILEDESC_LOCK(td->td_proc->p_fd); 13252bc21ed9SDavid Malone /* if the new FD's will not fit free them. */ 13262bc21ed9SDavid Malone if (!fdavail(td, newfds)) { 1327426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 13282bc21ed9SDavid Malone error = EMSGSIZE; 13292bc21ed9SDavid Malone unp_freerights(rp, newfds); 13302bc21ed9SDavid Malone goto next; 1331df8bae1dSRodney W. Grimes } 1332ed5b7817SJulian Elischer /* 13332bc21ed9SDavid Malone * now change each pointer to an fd in the global 13342bc21ed9SDavid Malone * table to an integer that is the index to the 13352bc21ed9SDavid Malone * local fd table entry that we set up to point 13362bc21ed9SDavid Malone * to the global one we are transferring. 1337ed5b7817SJulian Elischer */ 13382bc21ed9SDavid Malone newlen = newfds * sizeof(int); 13392bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 13402bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 13412bc21ed9SDavid Malone if (*controlp == NULL) { 1342426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 13432bc21ed9SDavid Malone error = E2BIG; 13442bc21ed9SDavid Malone unp_freerights(rp, newfds); 13452bc21ed9SDavid Malone goto next; 13462bc21ed9SDavid Malone } 13472bc21ed9SDavid Malone 13482bc21ed9SDavid Malone fdp = (int *) 13492bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1350df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 1351a6d4491cSDag-Erling Smørgrav if (fdalloc(td, 0, &f)) 13522bc21ed9SDavid Malone panic("unp_externalize fdalloc failed"); 13538692c025SYoshinobu Inoue fp = *rp++; 1354b40ce416SJulian Elischer td->td_proc->p_fd->fd_ofiles[f] = fp; 1355426da3bcSAlfred Perlstein FILE_LOCK(fp); 1356df8bae1dSRodney W. Grimes fp->f_msgcount--; 1357426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1358df8bae1dSRodney W. Grimes unp_rights--; 13598692c025SYoshinobu Inoue *fdp++ = f; 1360df8bae1dSRodney W. Grimes } 1361426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 13622bc21ed9SDavid Malone } else { /* We can just copy anything else across */ 13632bc21ed9SDavid Malone if (error || controlp == NULL) 13642bc21ed9SDavid Malone goto next; 13652bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, datalen, 13662bc21ed9SDavid Malone cm->cmsg_type, cm->cmsg_level); 13672bc21ed9SDavid Malone if (*controlp == NULL) { 13682bc21ed9SDavid Malone error = ENOBUFS; 13692bc21ed9SDavid Malone goto next; 13702bc21ed9SDavid Malone } 13712bc21ed9SDavid Malone bcopy(data, 13722bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 13732bc21ed9SDavid Malone datalen); 13742bc21ed9SDavid Malone } 13752bc21ed9SDavid Malone 13762bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 13772bc21ed9SDavid Malone 13782bc21ed9SDavid Malone next: 13792bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 13802bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 13812bc21ed9SDavid Malone cm = (struct cmsghdr *) 13822bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 13838692c025SYoshinobu Inoue } else { 13842bc21ed9SDavid Malone clen = 0; 13852bc21ed9SDavid Malone cm = NULL; 13868692c025SYoshinobu Inoue } 13878692c025SYoshinobu Inoue } 13888692c025SYoshinobu Inoue 13892bc21ed9SDavid Malone m_freem(control); 13902bc21ed9SDavid Malone 13912bc21ed9SDavid Malone return (error); 1392df8bae1dSRodney W. Grimes } 1393df8bae1dSRodney W. Grimes 139498271db4SGarrett Wollman void 139598271db4SGarrett Wollman unp_init(void) 139698271db4SGarrett Wollman { 13979e9d298aSJeff Roberson unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 13989e9d298aSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 1399fc3fcacfSRobert Watson if (unp_zone == NULL) 140098271db4SGarrett Wollman panic("unp_init"); 1401b17dd2bcSColin Percival uma_zone_set_max(unp_zone, nmbclusters); 140298271db4SGarrett Wollman LIST_INIT(&unp_dhead); 140398271db4SGarrett Wollman LIST_INIT(&unp_shead); 1404a0ec558aSRobert Watson TASK_INIT(&unp_gc_task, 0, unp_gc, NULL); 14050d9ce3a1SRobert Watson UNP_LOCK_INIT(); 140698271db4SGarrett Wollman } 140798271db4SGarrett Wollman 1408f708ef1bSPoul-Henning Kamp static int 1409892af6b9SRobert Watson unp_internalize(struct mbuf **controlp, struct thread *td) 1410df8bae1dSRodney W. Grimes { 14112bc21ed9SDavid Malone struct mbuf *control = *controlp; 1412b40ce416SJulian Elischer struct proc *p = td->td_proc; 14138692c025SYoshinobu Inoue struct filedesc *fdescp = p->p_fd; 14142bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 14152bc21ed9SDavid Malone struct cmsgcred *cmcred; 14162bc21ed9SDavid Malone struct file **rp; 14172bc21ed9SDavid Malone struct file *fp; 14182bc21ed9SDavid Malone struct timeval *tv; 14192bc21ed9SDavid Malone int i, fd, *fdp; 14202bc21ed9SDavid Malone void *data; 14212bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 14222bc21ed9SDavid Malone int error, oldfds; 14238692c025SYoshinobu Inoue u_int newlen; 1424df8bae1dSRodney W. Grimes 14254c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 14264c5bc1caSRobert Watson 14272bc21ed9SDavid Malone error = 0; 14282bc21ed9SDavid Malone *controlp = NULL; 14290b788fa1SBill Paul 14302bc21ed9SDavid Malone while (cm != NULL) { 14312bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 14322bc21ed9SDavid Malone || cm->cmsg_len > clen) { 14332bc21ed9SDavid Malone error = EINVAL; 14342bc21ed9SDavid Malone goto out; 14352bc21ed9SDavid Malone } 14362bc21ed9SDavid Malone 14372bc21ed9SDavid Malone data = CMSG_DATA(cm); 14382bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 14392bc21ed9SDavid Malone 14402bc21ed9SDavid Malone switch (cm->cmsg_type) { 14410b788fa1SBill Paul /* 14420b788fa1SBill Paul * Fill in credential information. 14430b788fa1SBill Paul */ 14442bc21ed9SDavid Malone case SCM_CREDS: 14452bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 14462bc21ed9SDavid Malone SCM_CREDS, SOL_SOCKET); 14472bc21ed9SDavid Malone if (*controlp == NULL) { 14482bc21ed9SDavid Malone error = ENOBUFS; 14492bc21ed9SDavid Malone goto out; 14502bc21ed9SDavid Malone } 14512bc21ed9SDavid Malone 14522bc21ed9SDavid Malone cmcred = (struct cmsgcred *) 14532bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 14540b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 1455a854ed98SJohn Baldwin cmcred->cmcred_uid = td->td_ucred->cr_ruid; 1456a854ed98SJohn Baldwin cmcred->cmcred_gid = td->td_ucred->cr_rgid; 1457a854ed98SJohn Baldwin cmcred->cmcred_euid = td->td_ucred->cr_uid; 1458a854ed98SJohn Baldwin cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 14590b788fa1SBill Paul CMGROUP_MAX); 14600b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 14612bc21ed9SDavid Malone cmcred->cmcred_groups[i] = 1462a854ed98SJohn Baldwin td->td_ucred->cr_groups[i]; 14632bc21ed9SDavid Malone break; 14640b788fa1SBill Paul 14652bc21ed9SDavid Malone case SCM_RIGHTS: 14662bc21ed9SDavid Malone oldfds = datalen / sizeof (int); 1467ed5b7817SJulian Elischer /* 14682bc21ed9SDavid Malone * check that all the FDs passed in refer to legal files 1469ed5b7817SJulian Elischer * If not, reject the entire operation. 1470ed5b7817SJulian Elischer */ 14712bc21ed9SDavid Malone fdp = data; 1472426da3bcSAlfred Perlstein FILEDESC_LOCK(fdescp); 1473df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 14748692c025SYoshinobu Inoue fd = *fdp++; 14758692c025SYoshinobu Inoue if ((unsigned)fd >= fdescp->fd_nfiles || 14762bc21ed9SDavid Malone fdescp->fd_ofiles[fd] == NULL) { 1477426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 14782bc21ed9SDavid Malone error = EBADF; 14792bc21ed9SDavid Malone goto out; 14802bc21ed9SDavid Malone } 1481e7d6662fSAlfred Perlstein fp = fdescp->fd_ofiles[fd]; 1482e7d6662fSAlfred Perlstein if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 1483e7d6662fSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 1484e7d6662fSAlfred Perlstein error = EOPNOTSUPP; 1485e7d6662fSAlfred Perlstein goto out; 1486e7d6662fSAlfred Perlstein } 1487e7d6662fSAlfred Perlstein 1488df8bae1dSRodney W. Grimes } 1489ed5b7817SJulian Elischer /* 1490ed5b7817SJulian Elischer * Now replace the integer FDs with pointers to 1491ed5b7817SJulian Elischer * the associated global file table entry.. 1492ed5b7817SJulian Elischer */ 14932bc21ed9SDavid Malone newlen = oldfds * sizeof(struct file *); 14942bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 14952bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 14962bc21ed9SDavid Malone if (*controlp == NULL) { 1497426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 14982bc21ed9SDavid Malone error = E2BIG; 14992bc21ed9SDavid Malone goto out; 15008692c025SYoshinobu Inoue } 15018692c025SYoshinobu Inoue 15022bc21ed9SDavid Malone fdp = data; 15032bc21ed9SDavid Malone rp = (struct file **) 15042bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 15058692c025SYoshinobu Inoue for (i = 0; i < oldfds; i++) { 15068692c025SYoshinobu Inoue fp = fdescp->fd_ofiles[*fdp++]; 1507df8bae1dSRodney W. Grimes *rp++ = fp; 1508426da3bcSAlfred Perlstein FILE_LOCK(fp); 1509df8bae1dSRodney W. Grimes fp->f_count++; 1510df8bae1dSRodney W. Grimes fp->f_msgcount++; 1511426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1512df8bae1dSRodney W. Grimes unp_rights++; 1513df8bae1dSRodney W. Grimes } 1514426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 15152bc21ed9SDavid Malone break; 15162bc21ed9SDavid Malone 15172bc21ed9SDavid Malone case SCM_TIMESTAMP: 15182bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*tv), 15192bc21ed9SDavid Malone SCM_TIMESTAMP, SOL_SOCKET); 15202bc21ed9SDavid Malone if (*controlp == NULL) { 15212bc21ed9SDavid Malone error = ENOBUFS; 15222bc21ed9SDavid Malone goto out; 15238692c025SYoshinobu Inoue } 15242bc21ed9SDavid Malone tv = (struct timeval *) 15252bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 15262bc21ed9SDavid Malone microtime(tv); 15272bc21ed9SDavid Malone break; 15282bc21ed9SDavid Malone 15292bc21ed9SDavid Malone default: 15302bc21ed9SDavid Malone error = EINVAL; 15312bc21ed9SDavid Malone goto out; 15322bc21ed9SDavid Malone } 15332bc21ed9SDavid Malone 15342bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 15352bc21ed9SDavid Malone 15362bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 15372bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 15382bc21ed9SDavid Malone cm = (struct cmsghdr *) 15392bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 15402bc21ed9SDavid Malone } else { 15412bc21ed9SDavid Malone clen = 0; 15422bc21ed9SDavid Malone cm = NULL; 15432bc21ed9SDavid Malone } 15442bc21ed9SDavid Malone } 15452bc21ed9SDavid Malone 15462bc21ed9SDavid Malone out: 15472bc21ed9SDavid Malone m_freem(control); 15482bc21ed9SDavid Malone 15492bc21ed9SDavid Malone return (error); 1550df8bae1dSRodney W. Grimes } 1551df8bae1dSRodney W. Grimes 15526a2989fdSMatthew N. Dodd struct mbuf * 15536a2989fdSMatthew N. Dodd unp_addsockcred(struct thread *td, struct mbuf *control) 15546a2989fdSMatthew N. Dodd { 15556a2989fdSMatthew N. Dodd struct mbuf *m, *n; 15566a2989fdSMatthew N. Dodd struct sockcred *sc; 15576a2989fdSMatthew N. Dodd int ngroups; 15586a2989fdSMatthew N. Dodd int i; 15596a2989fdSMatthew N. Dodd 15606a2989fdSMatthew N. Dodd ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX); 15616a2989fdSMatthew N. Dodd 15626a2989fdSMatthew N. Dodd m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET); 15636a2989fdSMatthew N. Dodd if (m == NULL) 15646a2989fdSMatthew N. Dodd return (control); 15656a2989fdSMatthew N. Dodd m->m_next = NULL; 15666a2989fdSMatthew N. Dodd 15676a2989fdSMatthew N. Dodd sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *)); 15686a2989fdSMatthew N. Dodd sc->sc_uid = td->td_ucred->cr_ruid; 15696a2989fdSMatthew N. Dodd sc->sc_euid = td->td_ucred->cr_uid; 15706a2989fdSMatthew N. Dodd sc->sc_gid = td->td_ucred->cr_rgid; 15716a2989fdSMatthew N. Dodd sc->sc_egid = td->td_ucred->cr_gid; 15726a2989fdSMatthew N. Dodd sc->sc_ngroups = ngroups; 15736a2989fdSMatthew N. Dodd for (i = 0; i < sc->sc_ngroups; i++) 15746a2989fdSMatthew N. Dodd sc->sc_groups[i] = td->td_ucred->cr_groups[i]; 15756a2989fdSMatthew N. Dodd 15766a2989fdSMatthew N. Dodd /* 15776a2989fdSMatthew N. Dodd * If a control message already exists, append us to the end. 15786a2989fdSMatthew N. Dodd */ 15796a2989fdSMatthew N. Dodd if (control != NULL) { 15806a2989fdSMatthew N. Dodd for (n = control; n->m_next != NULL; n = n->m_next) 15816a2989fdSMatthew N. Dodd ; 15826a2989fdSMatthew N. Dodd n->m_next = m; 15836a2989fdSMatthew N. Dodd } else 15846a2989fdSMatthew N. Dodd control = m; 15856a2989fdSMatthew N. Dodd 15866a2989fdSMatthew N. Dodd return (control); 15876a2989fdSMatthew N. Dodd } 15886a2989fdSMatthew N. Dodd 1589161a0c7cSRobert Watson /* 1590a0ec558aSRobert Watson * unp_defer indicates whether additional work has been defered for a future 1591a0ec558aSRobert Watson * pass through unp_gc(). It is thread local and does not require explicit 1592a0ec558aSRobert Watson * synchronization. 1593161a0c7cSRobert Watson */ 1594a0ec558aSRobert Watson static int unp_defer; 1595a0ec558aSRobert Watson 1596a0ec558aSRobert Watson static int unp_taskcount; 1597a0ec558aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0, ""); 1598a0ec558aSRobert Watson 1599a0ec558aSRobert Watson static int unp_recycled; 1600a0ec558aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0, ""); 1601df8bae1dSRodney W. Grimes 1602f708ef1bSPoul-Henning Kamp static void 1603a0ec558aSRobert Watson unp_gc(__unused void *arg, int pending) 1604df8bae1dSRodney W. Grimes { 1605892af6b9SRobert Watson struct file *fp, *nextfp; 1606892af6b9SRobert Watson struct socket *so; 1607df8bae1dSRodney W. Grimes struct file **extra_ref, **fpp; 1608df8bae1dSRodney W. Grimes int nunref, i; 160995f004dcSAlfred Perlstein int nfiles_snap; 161095f004dcSAlfred Perlstein int nfiles_slack = 20; 1611df8bae1dSRodney W. Grimes 1612a0ec558aSRobert Watson unp_taskcount++; 1613df8bae1dSRodney W. Grimes unp_defer = 0; 1614ed5b7817SJulian Elischer /* 1615ed5b7817SJulian Elischer * before going through all this, set all FDs to 1616ed5b7817SJulian Elischer * be NOT defered and NOT externally accessible 1617ed5b7817SJulian Elischer */ 1618426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 16192e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) 1620426da3bcSAlfred Perlstein fp->f_gcflag &= ~(FMARK|FDEFER); 1621df8bae1dSRodney W. Grimes do { 16225bb84bc8SRobert Watson KASSERT(unp_defer >= 0, ("unp_gc: unp_defer %d", unp_defer)); 16232e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) { 1624426da3bcSAlfred Perlstein FILE_LOCK(fp); 1625ed5b7817SJulian Elischer /* 1626a0ec558aSRobert Watson * If the file is not open, skip it -- could be a 1627a0ec558aSRobert Watson * file in the process of being opened, or in the 1628a0ec558aSRobert Watson * process of being closed. If the file is 1629a0ec558aSRobert Watson * "closing", it may have been marked for deferred 1630a0ec558aSRobert Watson * consideration. Clear the flag now if so. 1631ed5b7817SJulian Elischer */ 1632426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1633a0ec558aSRobert Watson if (fp->f_gcflag & FDEFER) 1634a0ec558aSRobert Watson unp_defer--; 1635a0ec558aSRobert Watson fp->f_gcflag &= ~(FMARK|FDEFER); 1636426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1637df8bae1dSRodney W. Grimes continue; 1638426da3bcSAlfred Perlstein } 1639ed5b7817SJulian Elischer /* 1640ed5b7817SJulian Elischer * If we already marked it as 'defer' in a 1641ed5b7817SJulian Elischer * previous pass, then try process it this time 1642ed5b7817SJulian Elischer * and un-mark it 1643ed5b7817SJulian Elischer */ 1644426da3bcSAlfred Perlstein if (fp->f_gcflag & FDEFER) { 1645426da3bcSAlfred Perlstein fp->f_gcflag &= ~FDEFER; 1646df8bae1dSRodney W. Grimes unp_defer--; 1647df8bae1dSRodney W. Grimes } else { 1648ed5b7817SJulian Elischer /* 1649ed5b7817SJulian Elischer * if it's not defered, then check if it's 1650ed5b7817SJulian Elischer * already marked.. if so skip it 1651ed5b7817SJulian Elischer */ 1652426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) { 1653426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1654df8bae1dSRodney W. Grimes continue; 1655426da3bcSAlfred Perlstein } 1656ed5b7817SJulian Elischer /* 1657ed5b7817SJulian Elischer * If all references are from messages 1658ed5b7817SJulian Elischer * in transit, then skip it. it's not 1659ed5b7817SJulian Elischer * externally accessible. 1660ed5b7817SJulian Elischer */ 1661426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount) { 1662426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1663df8bae1dSRodney W. Grimes continue; 1664426da3bcSAlfred Perlstein } 1665ed5b7817SJulian Elischer /* 1666ed5b7817SJulian Elischer * If it got this far then it must be 1667ed5b7817SJulian Elischer * externally accessible. 1668ed5b7817SJulian Elischer */ 1669426da3bcSAlfred Perlstein fp->f_gcflag |= FMARK; 1670df8bae1dSRodney W. Grimes } 1671ed5b7817SJulian Elischer /* 1672ed5b7817SJulian Elischer * either it was defered, or it is externally 1673ed5b7817SJulian Elischer * accessible and not already marked so. 1674ed5b7817SJulian Elischer * Now check if it is possibly one of OUR sockets. 1675ed5b7817SJulian Elischer */ 1676df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_SOCKET || 167748e3128bSMatthew Dillon (so = fp->f_data) == NULL) { 1678426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1679df8bae1dSRodney W. Grimes continue; 1680426da3bcSAlfred Perlstein } 1681426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1682748e0b0aSGarrett Wollman if (so->so_proto->pr_domain != &localdomain || 1683df8bae1dSRodney W. Grimes (so->so_proto->pr_flags&PR_RIGHTS) == 0) 1684df8bae1dSRodney W. Grimes continue; 1685ed5b7817SJulian Elischer /* 1686ed5b7817SJulian Elischer * So, Ok, it's one of our sockets and it IS externally 1687ed5b7817SJulian Elischer * accessible (or was defered). Now we look 1688dc733423SDag-Erling Smørgrav * to see if we hold any file descriptors in its 1689ed5b7817SJulian Elischer * message buffers. Follow those links and mark them 1690ed5b7817SJulian Elischer * as accessible too. 1691ed5b7817SJulian Elischer */ 16927717cf07SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1693df8bae1dSRodney W. Grimes unp_scan(so->so_rcv.sb_mb, unp_mark); 16947717cf07SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 1695df8bae1dSRodney W. Grimes } 1696df8bae1dSRodney W. Grimes } while (unp_defer); 1697426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1698df8bae1dSRodney W. Grimes /* 1699a0ec558aSRobert Watson * XXXRW: The following comments need updating for a post-SMPng and 1700a0ec558aSRobert Watson * deferred unp_gc() world, but are still generally accurate. 1701a0ec558aSRobert Watson * 1702df8bae1dSRodney W. Grimes * We grab an extra reference to each of the file table entries 1703df8bae1dSRodney W. Grimes * that are not otherwise accessible and then free the rights 1704df8bae1dSRodney W. Grimes * that are stored in messages on them. 1705df8bae1dSRodney W. Grimes * 1706df8bae1dSRodney W. Grimes * The bug in the orginal code is a little tricky, so I'll describe 1707df8bae1dSRodney W. Grimes * what's wrong with it here. 1708df8bae1dSRodney W. Grimes * 1709df8bae1dSRodney W. Grimes * It is incorrect to simply unp_discard each entry for f_msgcount 1710df8bae1dSRodney W. Grimes * times -- consider the case of sockets A and B that contain 1711df8bae1dSRodney W. Grimes * references to each other. On a last close of some other socket, 1712df8bae1dSRodney W. Grimes * we trigger a gc since the number of outstanding rights (unp_rights) 1713a0ec558aSRobert Watson * is non-zero. If during the sweep phase the gc code unp_discards, 1714df8bae1dSRodney W. Grimes * we end up doing a (full) closef on the descriptor. A closef on A 1715df8bae1dSRodney W. Grimes * results in the following chain. Closef calls soo_close, which 1716df8bae1dSRodney W. Grimes * calls soclose. Soclose calls first (through the switch 1717df8bae1dSRodney W. Grimes * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 1718df8bae1dSRodney W. Grimes * returns because the previous instance had set unp_gcing, and 1719df8bae1dSRodney W. Grimes * we return all the way back to soclose, which marks the socket 1720df8bae1dSRodney W. Grimes * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush 1721df8bae1dSRodney W. Grimes * to free up the rights that are queued in messages on the socket A, 1722df8bae1dSRodney W. Grimes * i.e., the reference on B. The sorflush calls via the dom_dispose 1723df8bae1dSRodney W. Grimes * switch unp_dispose, which unp_scans with unp_discard. This second 1724df8bae1dSRodney W. Grimes * instance of unp_discard just calls closef on B. 1725df8bae1dSRodney W. Grimes * 1726df8bae1dSRodney W. Grimes * Well, a similar chain occurs on B, resulting in a sorflush on B, 1727df8bae1dSRodney W. Grimes * which results in another closef on A. Unfortunately, A is already 1728df8bae1dSRodney W. Grimes * being closed, and the descriptor has already been marked with 1729df8bae1dSRodney W. Grimes * SS_NOFDREF, and soclose panics at this point. 1730df8bae1dSRodney W. Grimes * 1731df8bae1dSRodney W. Grimes * Here, we first take an extra reference to each inaccessible 1732df8bae1dSRodney W. Grimes * descriptor. Then, we call sorflush ourself, since we know 1733df8bae1dSRodney W. Grimes * it is a Unix domain socket anyhow. After we destroy all the 1734df8bae1dSRodney W. Grimes * rights carried in messages, we do a last closef to get rid 1735df8bae1dSRodney W. Grimes * of our extra reference. This is the last close, and the 1736df8bae1dSRodney W. Grimes * unp_detach etc will shut down the socket. 1737df8bae1dSRodney W. Grimes * 1738df8bae1dSRodney W. Grimes * 91/09/19, bsy@cs.cmu.edu 1739df8bae1dSRodney W. Grimes */ 174095f004dcSAlfred Perlstein again: 1741e4643c73SPoul-Henning Kamp nfiles_snap = openfiles + nfiles_slack; /* some slack */ 174295f004dcSAlfred Perlstein extra_ref = malloc(nfiles_snap * sizeof(struct file *), M_TEMP, 174395f004dcSAlfred Perlstein M_WAITOK); 1744426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 1745e4643c73SPoul-Henning Kamp if (nfiles_snap < openfiles) { 174695f004dcSAlfred Perlstein sx_sunlock(&filelist_lock); 174795f004dcSAlfred Perlstein free(extra_ref, M_TEMP); 174895f004dcSAlfred Perlstein nfiles_slack += 20; 174995f004dcSAlfred Perlstein goto again; 175095f004dcSAlfred Perlstein } 1751fc3fcacfSRobert Watson for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; 1752fc3fcacfSRobert Watson fp != NULL; fp = nextfp) { 17532e3c8fcbSPoul-Henning Kamp nextfp = LIST_NEXT(fp, f_list); 1754426da3bcSAlfred Perlstein FILE_LOCK(fp); 1755ed5b7817SJulian Elischer /* 1756ed5b7817SJulian Elischer * If it's not open, skip it 1757ed5b7817SJulian Elischer */ 1758426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1759426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1760df8bae1dSRodney W. Grimes continue; 1761426da3bcSAlfred Perlstein } 1762ed5b7817SJulian Elischer /* 1763ed5b7817SJulian Elischer * If all refs are from msgs, and it's not marked accessible 1764ed5b7817SJulian Elischer * then it must be referenced from some unreachable cycle 1765ed5b7817SJulian Elischer * of (shut-down) FDs, so include it in our 1766ed5b7817SJulian Elischer * list of FDs to remove 1767ed5b7817SJulian Elischer */ 1768426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) { 1769df8bae1dSRodney W. Grimes *fpp++ = fp; 1770df8bae1dSRodney W. Grimes nunref++; 1771df8bae1dSRodney W. Grimes fp->f_count++; 1772df8bae1dSRodney W. Grimes } 1773426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1774df8bae1dSRodney W. Grimes } 1775426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1776ed5b7817SJulian Elischer /* 1777ed5b7817SJulian Elischer * for each FD on our hit list, do the following two things 1778ed5b7817SJulian Elischer */ 17791c7c3c6aSMatthew Dillon for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 17801c7c3c6aSMatthew Dillon struct file *tfp = *fpp; 1781426da3bcSAlfred Perlstein FILE_LOCK(tfp); 1782cd72f218SMatthew Dillon if (tfp->f_type == DTYPE_SOCKET && 178348e3128bSMatthew Dillon tfp->f_data != NULL) { 1784426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 178548e3128bSMatthew Dillon sorflush(tfp->f_data); 1786e5aeaa0cSDag-Erling Smørgrav } else { 1787426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 17881c7c3c6aSMatthew Dillon } 1789e5aeaa0cSDag-Erling Smørgrav } 1790a0ec558aSRobert Watson for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 1791b40ce416SJulian Elischer closef(*fpp, (struct thread *) NULL); 1792a0ec558aSRobert Watson unp_recycled++; 1793a0ec558aSRobert Watson } 1794210a5a71SAlfred Perlstein free(extra_ref, M_TEMP); 1795df8bae1dSRodney W. Grimes } 1796df8bae1dSRodney W. Grimes 179726f9a767SRodney W. Grimes void 1798892af6b9SRobert Watson unp_dispose(struct mbuf *m) 1799df8bae1dSRodney W. Grimes { 1800996c772fSJohn Dyson 1801df8bae1dSRodney W. Grimes if (m) 1802df8bae1dSRodney W. Grimes unp_scan(m, unp_discard); 1803df8bae1dSRodney W. Grimes } 1804df8bae1dSRodney W. Grimes 18050c1bb4fbSDima Dorfman static int 1806d374e81eSRobert Watson unp_listen(struct socket *so, struct unpcb *unp, int backlog, 1807d374e81eSRobert Watson struct thread *td) 18080c1bb4fbSDima Dorfman { 18090daccb9cSRobert Watson int error; 18100daccb9cSRobert Watson 18110d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 18120c1bb4fbSDima Dorfman 18130daccb9cSRobert Watson SOCK_LOCK(so); 18140daccb9cSRobert Watson error = solisten_proto_check(so); 18150daccb9cSRobert Watson if (error == 0) { 18166f105b34SJohn Baldwin cru2x(td->td_ucred, &unp->unp_peercred); 18170c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPCCACHED; 1818d374e81eSRobert Watson solisten_proto(so, backlog); 18190daccb9cSRobert Watson } 18200daccb9cSRobert Watson SOCK_UNLOCK(so); 18210daccb9cSRobert Watson return (error); 18220c1bb4fbSDima Dorfman } 18230c1bb4fbSDima Dorfman 1824f708ef1bSPoul-Henning Kamp static void 1825892af6b9SRobert Watson unp_scan(struct mbuf *m0, void (*op)(struct file *)) 1826df8bae1dSRodney W. Grimes { 18272bc21ed9SDavid Malone struct mbuf *m; 18282bc21ed9SDavid Malone struct file **rp; 18292bc21ed9SDavid Malone struct cmsghdr *cm; 18302bc21ed9SDavid Malone void *data; 18312bc21ed9SDavid Malone int i; 18322bc21ed9SDavid Malone socklen_t clen, datalen; 1833df8bae1dSRodney W. Grimes int qfds; 1834df8bae1dSRodney W. Grimes 1835fc3fcacfSRobert Watson while (m0 != NULL) { 18362bc21ed9SDavid Malone for (m = m0; m; m = m->m_next) { 183712396bdcSDavid Malone if (m->m_type != MT_CONTROL) 1838df8bae1dSRodney W. Grimes continue; 18392bc21ed9SDavid Malone 18402bc21ed9SDavid Malone cm = mtod(m, struct cmsghdr *); 18412bc21ed9SDavid Malone clen = m->m_len; 18422bc21ed9SDavid Malone 18432bc21ed9SDavid Malone while (cm != NULL) { 18442bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) 18452bc21ed9SDavid Malone break; 18462bc21ed9SDavid Malone 18472bc21ed9SDavid Malone data = CMSG_DATA(cm); 18482bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len 18492bc21ed9SDavid Malone - (caddr_t)data; 18502bc21ed9SDavid Malone 18512bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET && 18522bc21ed9SDavid Malone cm->cmsg_type == SCM_RIGHTS) { 18532bc21ed9SDavid Malone qfds = datalen / sizeof (struct file *); 18542bc21ed9SDavid Malone rp = data; 1855df8bae1dSRodney W. Grimes for (i = 0; i < qfds; i++) 1856df8bae1dSRodney W. Grimes (*op)(*rp++); 18572bc21ed9SDavid Malone } 18582bc21ed9SDavid Malone 18592bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 18602bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 18612bc21ed9SDavid Malone cm = (struct cmsghdr *) 18622bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 18632bc21ed9SDavid Malone } else { 18642bc21ed9SDavid Malone clen = 0; 18652bc21ed9SDavid Malone cm = NULL; 18662bc21ed9SDavid Malone } 18672bc21ed9SDavid Malone } 1868df8bae1dSRodney W. Grimes } 1869df8bae1dSRodney W. Grimes m0 = m0->m_act; 1870df8bae1dSRodney W. Grimes } 1871df8bae1dSRodney W. Grimes } 1872df8bae1dSRodney W. Grimes 1873f708ef1bSPoul-Henning Kamp static void 1874892af6b9SRobert Watson unp_mark(struct file *fp) 1875df8bae1dSRodney W. Grimes { 1876426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) 1877df8bae1dSRodney W. Grimes return; 1878df8bae1dSRodney W. Grimes unp_defer++; 1879426da3bcSAlfred Perlstein fp->f_gcflag |= (FMARK|FDEFER); 1880df8bae1dSRodney W. Grimes } 1881df8bae1dSRodney W. Grimes 1882f708ef1bSPoul-Henning Kamp static void 1883892af6b9SRobert Watson unp_discard(struct file *fp) 1884df8bae1dSRodney W. Grimes { 1885a0ec558aSRobert Watson UNP_LOCK(); 1886426da3bcSAlfred Perlstein FILE_LOCK(fp); 1887df8bae1dSRodney W. Grimes fp->f_msgcount--; 1888df8bae1dSRodney W. Grimes unp_rights--; 1889426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1890a0ec558aSRobert Watson UNP_UNLOCK(); 1891b40ce416SJulian Elischer (void) closef(fp, (struct thread *)NULL); 1892df8bae1dSRodney W. Grimes } 1893