1df8bae1dSRodney W. Grimes /* 2ce5f32deSRobert Watson * Copyright 2004 Robert N. M. Watson 3df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1991, 1993 4df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 5df8bae1dSRodney W. Grimes * 6df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 7df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 8df8bae1dSRodney W. Grimes * are met: 9df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 10df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 11df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 12df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 13df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 14df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 15df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 16df8bae1dSRodney W. Grimes * without specific prior written permission. 17df8bae1dSRodney W. Grimes * 18df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28df8bae1dSRodney W. Grimes * SUCH DAMAGE. 29df8bae1dSRodney W. Grimes * 30748e0b0aSGarrett Wollman * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94 31df8bae1dSRodney W. Grimes */ 32df8bae1dSRodney W. Grimes 33677b542eSDavid E. O'Brien #include <sys/cdefs.h> 34677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 35677b542eSDavid E. O'Brien 36335654d7SRobert Watson #include "opt_mac.h" 37335654d7SRobert Watson 38df8bae1dSRodney W. Grimes #include <sys/param.h> 39fb919e4dSMark Murray #include <sys/domain.h> 40960ed29cSSeigo Tanimura #include <sys/fcntl.h> 41d826c479SBruce Evans #include <sys/malloc.h> /* XXX must be before <sys/file.h> */ 42639acc13SGarrett Wollman #include <sys/file.h> 43960ed29cSSeigo Tanimura #include <sys/filedesc.h> 44960ed29cSSeigo Tanimura #include <sys/jail.h> 45960ed29cSSeigo Tanimura #include <sys/kernel.h> 46960ed29cSSeigo Tanimura #include <sys/lock.h> 476ea48a90SRobert Watson #include <sys/mac.h> 48639acc13SGarrett Wollman #include <sys/mbuf.h> 49960ed29cSSeigo Tanimura #include <sys/mutex.h> 50639acc13SGarrett Wollman #include <sys/namei.h> 51639acc13SGarrett Wollman #include <sys/proc.h> 52df8bae1dSRodney W. Grimes #include <sys/protosw.h> 53960ed29cSSeigo Tanimura #include <sys/resourcevar.h> 54df8bae1dSRodney W. Grimes #include <sys/socket.h> 55df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 56960ed29cSSeigo Tanimura #include <sys/signalvar.h> 57df8bae1dSRodney W. Grimes #include <sys/stat.h> 58960ed29cSSeigo Tanimura #include <sys/sx.h> 59639acc13SGarrett Wollman #include <sys/sysctl.h> 60960ed29cSSeigo Tanimura #include <sys/systm.h> 61639acc13SGarrett Wollman #include <sys/un.h> 6298271db4SGarrett Wollman #include <sys/unpcb.h> 63639acc13SGarrett Wollman #include <sys/vnode.h> 64df8bae1dSRodney W. Grimes 659e9d298aSJeff Roberson #include <vm/uma.h> 6698271db4SGarrett Wollman 679e9d298aSJeff Roberson static uma_zone_t unp_zone; 6898271db4SGarrett Wollman static unp_gen_t unp_gencnt; 6998271db4SGarrett Wollman static u_int unp_count; 7098271db4SGarrett Wollman 7198271db4SGarrett Wollman static struct unp_head unp_shead, unp_dhead; 7298271db4SGarrett Wollman 73df8bae1dSRodney W. Grimes /* 74df8bae1dSRodney W. Grimes * Unix communications domain. 75df8bae1dSRodney W. Grimes * 76df8bae1dSRodney W. Grimes * TODO: 77df8bae1dSRodney W. Grimes * SEQPACKET, RDM 78df8bae1dSRodney W. Grimes * rethink name space problems 79df8bae1dSRodney W. Grimes * need a proper out-of-band 8098271db4SGarrett Wollman * lock pushdown 81df8bae1dSRodney W. Grimes */ 82e7dd9a10SRobert Watson static const struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 83f708ef1bSPoul-Henning Kamp static ino_t unp_ino; /* prototype for fake inode numbers */ 84f708ef1bSPoul-Henning Kamp 85ce5f32deSRobert Watson /* 86ce5f32deSRobert Watson * Currently, UNIX domain sockets are protected by a single subsystem lock, 87ce5f32deSRobert Watson * which covers global data structures and variables, the contents of each 88ce5f32deSRobert Watson * per-socket unpcb structure, and the so_pcb field in sockets attached to 89ce5f32deSRobert Watson * the UNIX domain. This provides for a moderate degree of paralellism, as 90ce5f32deSRobert Watson * receive operations on UNIX domain sockets do not need to acquire the 91ce5f32deSRobert Watson * subsystem lock. Finer grained locking to permit send() without acquiring 92ce5f32deSRobert Watson * a global lock would be a logical next step. 93ce5f32deSRobert Watson * 94ce5f32deSRobert Watson * The UNIX domain socket lock preceds all socket layer locks, including the 95ce5f32deSRobert Watson * socket lock and socket buffer lock, permitting UNIX domain socket code to 96ce5f32deSRobert Watson * call into socket support routines without releasing its locks. 97ce5f32deSRobert Watson * 98ce5f32deSRobert Watson * Some caution is required in areas where the UNIX domain socket code enters 99ce5f32deSRobert Watson * VFS in order to create or find rendezvous points. This results in 100ce5f32deSRobert Watson * dropping of the UNIX domain socket subsystem lock, acquisition of the 101ce5f32deSRobert Watson * Giant lock, and potential sleeping. This increases the chances of races, 102ce5f32deSRobert Watson * and exposes weaknesses in the socket->protocol API by offering poor 103ce5f32deSRobert Watson * failure modes. 104ce5f32deSRobert Watson */ 1050d9ce3a1SRobert Watson static struct mtx unp_mtx; 1060d9ce3a1SRobert Watson #define UNP_LOCK_INIT() \ 1070d9ce3a1SRobert Watson mtx_init(&unp_mtx, "unp", NULL, MTX_DEF) 1080d9ce3a1SRobert Watson #define UNP_LOCK() mtx_lock(&unp_mtx) 1090d9ce3a1SRobert Watson #define UNP_UNLOCK() mtx_unlock(&unp_mtx) 1100d9ce3a1SRobert Watson #define UNP_LOCK_ASSERT() mtx_assert(&unp_mtx, MA_OWNED) 1110d9ce3a1SRobert Watson 1124d77a549SAlfred Perlstein static int unp_attach(struct socket *); 1134d77a549SAlfred Perlstein static void unp_detach(struct unpcb *); 1144d77a549SAlfred Perlstein static int unp_bind(struct unpcb *,struct sockaddr *, struct thread *); 11570f52b48SBruce Evans static int unp_connect(struct socket *,struct sockaddr *, struct thread *); 116db48c0d2SRobert Watson static int unp_connect2(struct socket *so, struct socket *so2); 1174d77a549SAlfred Perlstein static void unp_disconnect(struct unpcb *); 1184d77a549SAlfred Perlstein static void unp_shutdown(struct unpcb *); 1194d77a549SAlfred Perlstein static void unp_drop(struct unpcb *, int); 1204d77a549SAlfred Perlstein static void unp_gc(void); 1214d77a549SAlfred Perlstein static void unp_scan(struct mbuf *, void (*)(struct file *)); 1224d77a549SAlfred Perlstein static void unp_mark(struct file *); 1234d77a549SAlfred Perlstein static void unp_discard(struct file *); 1244d77a549SAlfred Perlstein static void unp_freerights(struct file **, int); 1254d77a549SAlfred Perlstein static int unp_internalize(struct mbuf **, struct thread *); 1264d77a549SAlfred Perlstein static int unp_listen(struct unpcb *, struct thread *); 127f708ef1bSPoul-Henning Kamp 128a29f300eSGarrett Wollman static int 129a29f300eSGarrett Wollman uipc_abort(struct socket *so) 130df8bae1dSRodney W. Grimes { 13140f2ac28SRobert Watson struct unpcb *unp; 132df8bae1dSRodney W. Grimes 1330d9ce3a1SRobert Watson UNP_LOCK(); 13440f2ac28SRobert Watson unp = sotounpcb(so); 13540f2ac28SRobert Watson if (unp == NULL) { 13640f2ac28SRobert Watson UNP_UNLOCK(); 13740f2ac28SRobert Watson return (EINVAL); 13840f2ac28SRobert Watson } 139a29f300eSGarrett Wollman unp_drop(unp, ECONNABORTED); 1400d9ce3a1SRobert Watson unp_detach(unp); /* NB: unlocks */ 141395a08c9SRobert Watson SOCK_LOCK(so); 142ddb7d629SIan Dowse sotryfree(so); 143e5aeaa0cSDag-Erling Smørgrav return (0); 144df8bae1dSRodney W. Grimes } 145df8bae1dSRodney W. Grimes 146a29f300eSGarrett Wollman static int 14757bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam) 148a29f300eSGarrett Wollman { 14940f2ac28SRobert Watson struct unpcb *unp; 1500d9ce3a1SRobert Watson const struct sockaddr *sa; 151df8bae1dSRodney W. Grimes 152df8bae1dSRodney W. Grimes /* 153df8bae1dSRodney W. Grimes * Pass back name of connected socket, 154df8bae1dSRodney W. Grimes * if it was bound and we are still connected 155df8bae1dSRodney W. Grimes * (our peer may have closed already!). 156df8bae1dSRodney W. Grimes */ 1570d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 1580d9ce3a1SRobert Watson UNP_LOCK(); 15940f2ac28SRobert Watson unp = sotounpcb(so); 16040f2ac28SRobert Watson if (unp == NULL) { 16140f2ac28SRobert Watson UNP_UNLOCK(); 16240f2ac28SRobert Watson free(*nam, M_SONAME); 16340f2ac28SRobert Watson *nam = NULL; 16440f2ac28SRobert Watson return (EINVAL); 16540f2ac28SRobert Watson } 1660d9ce3a1SRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr != NULL) 1670d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 1680d9ce3a1SRobert Watson else 1690d9ce3a1SRobert Watson sa = &sun_noname; 1700d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 1710d9ce3a1SRobert Watson UNP_UNLOCK(); 172e5aeaa0cSDag-Erling Smørgrav return (0); 173a29f300eSGarrett Wollman } 174df8bae1dSRodney W. Grimes 175a29f300eSGarrett Wollman static int 176b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td) 177a29f300eSGarrett Wollman { 178a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 179df8bae1dSRodney W. Grimes 180fc3fcacfSRobert Watson if (unp != NULL) 181e5aeaa0cSDag-Erling Smørgrav return (EISCONN); 182e5aeaa0cSDag-Erling Smørgrav return (unp_attach(so)); 183a29f300eSGarrett Wollman } 184a29f300eSGarrett Wollman 185a29f300eSGarrett Wollman static int 186b40ce416SJulian Elischer uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 187a29f300eSGarrett Wollman { 18840f2ac28SRobert Watson struct unpcb *unp; 18940f2ac28SRobert Watson int error; 190a29f300eSGarrett Wollman 19140f2ac28SRobert Watson UNP_LOCK(); 19240f2ac28SRobert Watson unp = sotounpcb(so); 19340f2ac28SRobert Watson if (unp == NULL) { 19440f2ac28SRobert Watson UNP_UNLOCK(); 195e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 19640f2ac28SRobert Watson } 19740f2ac28SRobert Watson error = unp_bind(unp, nam, td); 19840f2ac28SRobert Watson UNP_UNLOCK(); 19940f2ac28SRobert Watson return (error); 200a29f300eSGarrett Wollman } 201a29f300eSGarrett Wollman 202a29f300eSGarrett Wollman static int 203b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 204a29f300eSGarrett Wollman { 205b295bdcdSRobert Watson struct unpcb *unp; 2060d9ce3a1SRobert Watson int error; 207a29f300eSGarrett Wollman 208fd179ee9SRobert Watson KASSERT(td == curthread, ("uipc_connect: td != curthread")); 209fd179ee9SRobert Watson 2100d9ce3a1SRobert Watson UNP_LOCK(); 211b295bdcdSRobert Watson unp = sotounpcb(so); 212b295bdcdSRobert Watson if (unp == NULL) { 21340f2ac28SRobert Watson UNP_UNLOCK(); 21440f2ac28SRobert Watson return (EINVAL); 215b295bdcdSRobert Watson } 216fd179ee9SRobert Watson error = unp_connect(so, nam, td); 2170d9ce3a1SRobert Watson UNP_UNLOCK(); 2180d9ce3a1SRobert Watson return (error); 219a29f300eSGarrett Wollman } 220a29f300eSGarrett Wollman 221db48c0d2SRobert Watson int 222a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2) 223a29f300eSGarrett Wollman { 22440f2ac28SRobert Watson struct unpcb *unp; 2250d9ce3a1SRobert Watson int error; 226a29f300eSGarrett Wollman 2270d9ce3a1SRobert Watson UNP_LOCK(); 22840f2ac28SRobert Watson unp = sotounpcb(so1); 22940f2ac28SRobert Watson if (unp == NULL) { 23040f2ac28SRobert Watson UNP_UNLOCK(); 23140f2ac28SRobert Watson return (EINVAL); 23240f2ac28SRobert Watson } 2330d9ce3a1SRobert Watson error = unp_connect2(so1, so2); 2340d9ce3a1SRobert Watson UNP_UNLOCK(); 2350d9ce3a1SRobert Watson return (error); 236a29f300eSGarrett Wollman } 237a29f300eSGarrett Wollman 238a29f300eSGarrett Wollman /* control is EOPNOTSUPP */ 239a29f300eSGarrett Wollman 240a29f300eSGarrett Wollman static int 241a29f300eSGarrett Wollman uipc_detach(struct socket *so) 242a29f300eSGarrett Wollman { 24340f2ac28SRobert Watson struct unpcb *unp; 244a29f300eSGarrett Wollman 2450d9ce3a1SRobert Watson UNP_LOCK(); 24640f2ac28SRobert Watson unp = sotounpcb(so); 24740f2ac28SRobert Watson if (unp == NULL) { 24840f2ac28SRobert Watson UNP_UNLOCK(); 24940f2ac28SRobert Watson return (EINVAL); 25040f2ac28SRobert Watson } 2510d9ce3a1SRobert Watson unp_detach(unp); /* NB: unlocks unp */ 252e5aeaa0cSDag-Erling Smørgrav return (0); 253a29f300eSGarrett Wollman } 254a29f300eSGarrett Wollman 255a29f300eSGarrett Wollman static int 256a29f300eSGarrett Wollman uipc_disconnect(struct socket *so) 257a29f300eSGarrett Wollman { 25840f2ac28SRobert Watson struct unpcb *unp; 259a29f300eSGarrett Wollman 2600d9ce3a1SRobert Watson UNP_LOCK(); 26140f2ac28SRobert Watson unp = sotounpcb(so); 26240f2ac28SRobert Watson if (unp == NULL) { 26340f2ac28SRobert Watson UNP_UNLOCK(); 26440f2ac28SRobert Watson return (EINVAL); 26540f2ac28SRobert Watson } 266a29f300eSGarrett Wollman unp_disconnect(unp); 2670d9ce3a1SRobert Watson UNP_UNLOCK(); 268e5aeaa0cSDag-Erling Smørgrav return (0); 269a29f300eSGarrett Wollman } 270a29f300eSGarrett Wollman 271a29f300eSGarrett Wollman static int 272b40ce416SJulian Elischer uipc_listen(struct socket *so, struct thread *td) 273a29f300eSGarrett Wollman { 27440f2ac28SRobert Watson struct unpcb *unp; 2750d9ce3a1SRobert Watson int error; 276a29f300eSGarrett Wollman 2770d9ce3a1SRobert Watson UNP_LOCK(); 27840f2ac28SRobert Watson unp = sotounpcb(so); 27940f2ac28SRobert Watson if (unp == NULL || unp->unp_vnode == NULL) { 28040f2ac28SRobert Watson UNP_UNLOCK(); 28140f2ac28SRobert Watson return (EINVAL); 28240f2ac28SRobert Watson } 2830d9ce3a1SRobert Watson error = unp_listen(unp, td); 2840d9ce3a1SRobert Watson UNP_UNLOCK(); 2850d9ce3a1SRobert Watson return (error); 286a29f300eSGarrett Wollman } 287a29f300eSGarrett Wollman 288a29f300eSGarrett Wollman static int 28957bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 290a29f300eSGarrett Wollman { 29140f2ac28SRobert Watson struct unpcb *unp; 2920d9ce3a1SRobert Watson const struct sockaddr *sa; 293a29f300eSGarrett Wollman 2940d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 2950d9ce3a1SRobert Watson UNP_LOCK(); 29640f2ac28SRobert Watson unp = sotounpcb(so); 29740f2ac28SRobert Watson if (unp == NULL) { 29840f2ac28SRobert Watson UNP_UNLOCK(); 29940f2ac28SRobert Watson free(*nam, M_SONAME); 30040f2ac28SRobert Watson *nam = NULL; 30140f2ac28SRobert Watson return (EINVAL); 30240f2ac28SRobert Watson } 303fc3fcacfSRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr!= NULL) 3040d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 305bdc5f6a3SHajimu UMEMOTO else { 306bdc5f6a3SHajimu UMEMOTO /* 307bdc5f6a3SHajimu UMEMOTO * XXX: It seems that this test always fails even when 308bdc5f6a3SHajimu UMEMOTO * connection is established. So, this else clause is 309bdc5f6a3SHajimu UMEMOTO * added as workaround to return PF_LOCAL sockaddr. 310bdc5f6a3SHajimu UMEMOTO */ 3110d9ce3a1SRobert Watson sa = &sun_noname; 312bdc5f6a3SHajimu UMEMOTO } 3130d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 3140d9ce3a1SRobert Watson UNP_UNLOCK(); 315e5aeaa0cSDag-Erling Smørgrav return (0); 316a29f300eSGarrett Wollman } 317a29f300eSGarrett Wollman 318a29f300eSGarrett Wollman static int 319a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 320a29f300eSGarrett Wollman { 32140f2ac28SRobert Watson struct unpcb *unp; 322a29f300eSGarrett Wollman struct socket *so2; 3236aef685fSBrian Feldman u_long newhiwat; 324a29f300eSGarrett Wollman 3250d9ce3a1SRobert Watson UNP_LOCK(); 32640f2ac28SRobert Watson unp = sotounpcb(so); 32740f2ac28SRobert Watson if (unp == NULL) { 32840f2ac28SRobert Watson UNP_UNLOCK(); 32940f2ac28SRobert Watson return (EINVAL); 33040f2ac28SRobert Watson } 331df8bae1dSRodney W. Grimes switch (so->so_type) { 332df8bae1dSRodney W. Grimes case SOCK_DGRAM: 333a29f300eSGarrett Wollman panic("uipc_rcvd DGRAM?"); 334df8bae1dSRodney W. Grimes /*NOTREACHED*/ 335df8bae1dSRodney W. Grimes 336df8bae1dSRodney W. Grimes case SOCK_STREAM: 337fc3fcacfSRobert Watson if (unp->unp_conn == NULL) 338df8bae1dSRodney W. Grimes break; 339df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 340c9f69064SRobert Watson SOCKBUF_LOCK(&so2->so_snd); 341c9f69064SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 342df8bae1dSRodney W. Grimes /* 343df8bae1dSRodney W. Grimes * Adjust backpressure on sender 344df8bae1dSRodney W. Grimes * and wakeup any waiting to write. 345df8bae1dSRodney W. Grimes */ 346ff8b0106SBrian Feldman so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt; 347ff8b0106SBrian Feldman unp->unp_mbcnt = so->so_rcv.sb_mbcnt; 3486aef685fSBrian Feldman newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - 3496aef685fSBrian Feldman so->so_rcv.sb_cc; 350f535380cSDon Lewis (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat, 3516aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 352ff8b0106SBrian Feldman unp->unp_cc = so->so_rcv.sb_cc; 353c9f69064SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 3541e4d7da7SRobert Watson sowwakeup_locked(so2); 355df8bae1dSRodney W. Grimes break; 356df8bae1dSRodney W. Grimes 357df8bae1dSRodney W. Grimes default: 358a29f300eSGarrett Wollman panic("uipc_rcvd unknown socktype"); 359df8bae1dSRodney W. Grimes } 3600d9ce3a1SRobert Watson UNP_UNLOCK(); 361e5aeaa0cSDag-Erling Smørgrav return (0); 362a29f300eSGarrett Wollman } 363df8bae1dSRodney W. Grimes 364a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 365a29f300eSGarrett Wollman 366a29f300eSGarrett Wollman static int 36757bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 368b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 369a29f300eSGarrett Wollman { 370a29f300eSGarrett Wollman int error = 0; 37140f2ac28SRobert Watson struct unpcb *unp; 372a29f300eSGarrett Wollman struct socket *so2; 3736aef685fSBrian Feldman u_long newhiwat; 374a29f300eSGarrett Wollman 37540f2ac28SRobert Watson unp = sotounpcb(so); 376fc3fcacfSRobert Watson if (unp == NULL) { 377a29f300eSGarrett Wollman error = EINVAL; 378a29f300eSGarrett Wollman goto release; 379a29f300eSGarrett Wollman } 380a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 381a29f300eSGarrett Wollman error = EOPNOTSUPP; 382a29f300eSGarrett Wollman goto release; 383a29f300eSGarrett Wollman } 384a29f300eSGarrett Wollman 385fc3fcacfSRobert Watson if (control != NULL && (error = unp_internalize(&control, td))) 386a29f300eSGarrett Wollman goto release; 387df8bae1dSRodney W. Grimes 3880d9ce3a1SRobert Watson UNP_LOCK(); 38940f2ac28SRobert Watson unp = sotounpcb(so); 39040f2ac28SRobert Watson if (unp == NULL) { 39140f2ac28SRobert Watson UNP_UNLOCK(); 39240f2ac28SRobert Watson error = EINVAL; 39340f2ac28SRobert Watson goto dispose_release; 39440f2ac28SRobert Watson } 39540f2ac28SRobert Watson 396a29f300eSGarrett Wollman switch (so->so_type) { 397a29f300eSGarrett Wollman case SOCK_DGRAM: 398a29f300eSGarrett Wollman { 399e7dd9a10SRobert Watson const struct sockaddr *from; 400df8bae1dSRodney W. Grimes 401fc3fcacfSRobert Watson if (nam != NULL) { 402fc3fcacfSRobert Watson if (unp->unp_conn != NULL) { 403df8bae1dSRodney W. Grimes error = EISCONN; 404df8bae1dSRodney W. Grimes break; 405df8bae1dSRodney W. Grimes } 406b40ce416SJulian Elischer error = unp_connect(so, nam, td); 407df8bae1dSRodney W. Grimes if (error) 408df8bae1dSRodney W. Grimes break; 409df8bae1dSRodney W. Grimes } else { 410fc3fcacfSRobert Watson if (unp->unp_conn == NULL) { 411df8bae1dSRodney W. Grimes error = ENOTCONN; 412df8bae1dSRodney W. Grimes break; 413df8bae1dSRodney W. Grimes } 414df8bae1dSRodney W. Grimes } 415df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 416fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 41757bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 418df8bae1dSRodney W. Grimes else 419df8bae1dSRodney W. Grimes from = &sun_noname; 420a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 421a34b7046SRobert Watson if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) { 4221e4d7da7SRobert Watson sorwakeup_locked(so2); 423fc3fcacfSRobert Watson m = NULL; 424fc3fcacfSRobert Watson control = NULL; 425e5aeaa0cSDag-Erling Smørgrav } else { 426a34b7046SRobert Watson SOCKBUF_UNLOCK(&so2->so_rcv); 427df8bae1dSRodney W. Grimes error = ENOBUFS; 428e5aeaa0cSDag-Erling Smørgrav } 429fc3fcacfSRobert Watson if (nam != NULL) 430df8bae1dSRodney W. Grimes unp_disconnect(unp); 431df8bae1dSRodney W. Grimes break; 432df8bae1dSRodney W. Grimes } 433df8bae1dSRodney W. Grimes 434df8bae1dSRodney W. Grimes case SOCK_STREAM: 4356b8fda4dSGarrett Wollman /* Connect if not connected yet. */ 4366b8fda4dSGarrett Wollman /* 4376b8fda4dSGarrett Wollman * Note: A better implementation would complain 438402cc72dSDavid Greenman * if not equal to the peer's address. 4396b8fda4dSGarrett Wollman */ 440402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 441fc3fcacfSRobert Watson if (nam != NULL) { 442b40ce416SJulian Elischer error = unp_connect(so, nam, td); 443402cc72dSDavid Greenman if (error) 4446b8fda4dSGarrett Wollman break; /* XXX */ 445402cc72dSDavid Greenman } else { 446402cc72dSDavid Greenman error = ENOTCONN; 447402cc72dSDavid Greenman break; 448402cc72dSDavid Greenman } 449402cc72dSDavid Greenman } 450402cc72dSDavid Greenman 451c0b99ffaSRobert Watson if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 452df8bae1dSRodney W. Grimes error = EPIPE; 453df8bae1dSRodney W. Grimes break; 454df8bae1dSRodney W. Grimes } 455fc3fcacfSRobert Watson if (unp->unp_conn == NULL) 456a29f300eSGarrett Wollman panic("uipc_send connected but no connection?"); 457df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 458a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 459df8bae1dSRodney W. Grimes /* 460df8bae1dSRodney W. Grimes * Send to paired receive port, and then reduce 461df8bae1dSRodney W. Grimes * send buffer hiwater marks to maintain backpressure. 462df8bae1dSRodney W. Grimes * Wake up readers. 463df8bae1dSRodney W. Grimes */ 464fc3fcacfSRobert Watson if (control != NULL) { 465a34b7046SRobert Watson if (sbappendcontrol_locked(&so2->so_rcv, m, control)) 466fc3fcacfSRobert Watson control = NULL; 467e5aeaa0cSDag-Erling Smørgrav } else { 468a34b7046SRobert Watson sbappend_locked(&so2->so_rcv, m); 469e5aeaa0cSDag-Erling Smørgrav } 470ff8b0106SBrian Feldman so->so_snd.sb_mbmax -= 471ff8b0106SBrian Feldman so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt; 472ff8b0106SBrian Feldman unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt; 4736aef685fSBrian Feldman newhiwat = so->so_snd.sb_hiwat - 4746aef685fSBrian Feldman (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc); 475f535380cSDon Lewis (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat, 4766aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 477ff8b0106SBrian Feldman unp->unp_conn->unp_cc = so2->so_rcv.sb_cc; 4781e4d7da7SRobert Watson sorwakeup_locked(so2); 479fc3fcacfSRobert Watson m = NULL; 480df8bae1dSRodney W. Grimes break; 481df8bae1dSRodney W. Grimes 482df8bae1dSRodney W. Grimes default: 483a29f300eSGarrett Wollman panic("uipc_send unknown socktype"); 484df8bae1dSRodney W. Grimes } 485a29f300eSGarrett Wollman 4866b8fda4dSGarrett Wollman /* 4876b8fda4dSGarrett Wollman * SEND_EOF is equivalent to a SEND followed by 4886b8fda4dSGarrett Wollman * a SHUTDOWN. 4896b8fda4dSGarrett Wollman */ 490a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 4916b8fda4dSGarrett Wollman socantsendmore(so); 4926b8fda4dSGarrett Wollman unp_shutdown(unp); 4936b8fda4dSGarrett Wollman } 4940d9ce3a1SRobert Watson UNP_UNLOCK(); 495df8bae1dSRodney W. Grimes 49640f2ac28SRobert Watson dispose_release: 497fc3fcacfSRobert Watson if (control != NULL && error != 0) 498bd508d39SDon Lewis unp_dispose(control); 499bd508d39SDon Lewis 500a29f300eSGarrett Wollman release: 501fc3fcacfSRobert Watson if (control != NULL) 502a29f300eSGarrett Wollman m_freem(control); 503fc3fcacfSRobert Watson if (m != NULL) 504a29f300eSGarrett Wollman m_freem(m); 505e5aeaa0cSDag-Erling Smørgrav return (error); 506a29f300eSGarrett Wollman } 507df8bae1dSRodney W. Grimes 508a29f300eSGarrett Wollman static int 509a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 510a29f300eSGarrett Wollman { 51140f2ac28SRobert Watson struct unpcb *unp; 512a29f300eSGarrett Wollman struct socket *so2; 513a29f300eSGarrett Wollman 5140d9ce3a1SRobert Watson UNP_LOCK(); 51540f2ac28SRobert Watson unp = sotounpcb(so); 51640f2ac28SRobert Watson if (unp == NULL) { 51740f2ac28SRobert Watson UNP_UNLOCK(); 51840f2ac28SRobert Watson return (EINVAL); 51940f2ac28SRobert Watson } 520a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 521fc3fcacfSRobert Watson if (so->so_type == SOCK_STREAM && unp->unp_conn != NULL) { 522df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 523a29f300eSGarrett Wollman sb->st_blksize += so2->so_rcv.sb_cc; 524df8bae1dSRodney W. Grimes } 525f3732fd1SPoul-Henning Kamp sb->st_dev = NODEV; 526df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 5276f782c46SJeffrey Hsu unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 528a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 5290d9ce3a1SRobert Watson UNP_UNLOCK(); 530df8bae1dSRodney W. Grimes return (0); 531a29f300eSGarrett Wollman } 532df8bae1dSRodney W. Grimes 533a29f300eSGarrett Wollman static int 534a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 535a29f300eSGarrett Wollman { 53640f2ac28SRobert Watson struct unpcb *unp; 537df8bae1dSRodney W. Grimes 5380d9ce3a1SRobert Watson UNP_LOCK(); 53940f2ac28SRobert Watson unp = sotounpcb(so); 54040f2ac28SRobert Watson if (unp == NULL) { 54140f2ac28SRobert Watson UNP_UNLOCK(); 54240f2ac28SRobert Watson return (EINVAL); 54340f2ac28SRobert Watson } 544a29f300eSGarrett Wollman socantsendmore(so); 545a29f300eSGarrett Wollman unp_shutdown(unp); 5460d9ce3a1SRobert Watson UNP_UNLOCK(); 547e5aeaa0cSDag-Erling Smørgrav return (0); 548a29f300eSGarrett Wollman } 549df8bae1dSRodney W. Grimes 550a29f300eSGarrett Wollman static int 55157bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 552a29f300eSGarrett Wollman { 55340f2ac28SRobert Watson struct unpcb *unp; 5540d9ce3a1SRobert Watson const struct sockaddr *sa; 555a29f300eSGarrett Wollman 5560d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 5570d9ce3a1SRobert Watson UNP_LOCK(); 55840f2ac28SRobert Watson unp = sotounpcb(so); 55940f2ac28SRobert Watson if (unp == NULL) { 56040f2ac28SRobert Watson UNP_UNLOCK(); 56140f2ac28SRobert Watson free(*nam, M_SONAME); 56240f2ac28SRobert Watson *nam = NULL; 56340f2ac28SRobert Watson return (EINVAL); 56440f2ac28SRobert Watson } 565fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 5660d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_addr; 56783f3198bSThomas Moestl else 5680d9ce3a1SRobert Watson sa = &sun_noname; 5690d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 5700d9ce3a1SRobert Watson UNP_UNLOCK(); 571e5aeaa0cSDag-Erling Smørgrav return (0); 572df8bae1dSRodney W. Grimes } 573a29f300eSGarrett Wollman 574a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = { 575a29f300eSGarrett Wollman uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect, 576a29f300eSGarrett Wollman uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect, 577a29f300eSGarrett Wollman uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp, 578a29f300eSGarrett Wollman uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr, 579a557af22SRobert Watson sosend, soreceive, sopoll, pru_sosetlabel_null 580a29f300eSGarrett Wollman }; 581df8bae1dSRodney W. Grimes 5820c1bb4fbSDima Dorfman int 5830c1bb4fbSDima Dorfman uipc_ctloutput(so, sopt) 5840c1bb4fbSDima Dorfman struct socket *so; 5850c1bb4fbSDima Dorfman struct sockopt *sopt; 5860c1bb4fbSDima Dorfman { 58740f2ac28SRobert Watson struct unpcb *unp; 5880d9ce3a1SRobert Watson struct xucred xu; 5890c1bb4fbSDima Dorfman int error; 5900c1bb4fbSDima Dorfman 5910c1bb4fbSDima Dorfman switch (sopt->sopt_dir) { 5920c1bb4fbSDima Dorfman case SOPT_GET: 5930c1bb4fbSDima Dorfman switch (sopt->sopt_name) { 5940c1bb4fbSDima Dorfman case LOCAL_PEERCRED: 5950d9ce3a1SRobert Watson error = 0; 5960d9ce3a1SRobert Watson UNP_LOCK(); 59740f2ac28SRobert Watson unp = sotounpcb(so); 59840f2ac28SRobert Watson if (unp == NULL) { 59940f2ac28SRobert Watson UNP_UNLOCK(); 60040f2ac28SRobert Watson error = EINVAL; 60140f2ac28SRobert Watson break; 60240f2ac28SRobert Watson } 6030c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC) 6040d9ce3a1SRobert Watson xu = unp->unp_peercred; 6050c1bb4fbSDima Dorfman else { 6060c1bb4fbSDima Dorfman if (so->so_type == SOCK_STREAM) 6070c1bb4fbSDima Dorfman error = ENOTCONN; 6080c1bb4fbSDima Dorfman else 6090c1bb4fbSDima Dorfman error = EINVAL; 6100c1bb4fbSDima Dorfman } 6110d9ce3a1SRobert Watson UNP_UNLOCK(); 6120d9ce3a1SRobert Watson if (error == 0) 6130d9ce3a1SRobert Watson error = sooptcopyout(sopt, &xu, sizeof(xu)); 6140c1bb4fbSDima Dorfman break; 6150c1bb4fbSDima Dorfman default: 6160c1bb4fbSDima Dorfman error = EOPNOTSUPP; 6170c1bb4fbSDima Dorfman break; 6180c1bb4fbSDima Dorfman } 6190c1bb4fbSDima Dorfman break; 6200c1bb4fbSDima Dorfman case SOPT_SET: 6210c1bb4fbSDima Dorfman default: 6220c1bb4fbSDima Dorfman error = EOPNOTSUPP; 6230c1bb4fbSDima Dorfman break; 6240c1bb4fbSDima Dorfman } 6250c1bb4fbSDima Dorfman return (error); 6260c1bb4fbSDima Dorfman } 6270c1bb4fbSDima Dorfman 628df8bae1dSRodney W. Grimes /* 629df8bae1dSRodney W. Grimes * Both send and receive buffers are allocated PIPSIZ bytes of buffering 630df8bae1dSRodney W. Grimes * for stream sockets, although the total for sender and receiver is 631df8bae1dSRodney W. Grimes * actually only PIPSIZ. 632df8bae1dSRodney W. Grimes * Datagram sockets really use the sendspace as the maximum datagram size, 633df8bae1dSRodney W. Grimes * and don't really want to reserve the sendspace. Their recvspace should 634df8bae1dSRodney W. Grimes * be large enough for at least one max-size datagram plus address. 635df8bae1dSRodney W. Grimes */ 6365dce41c5SJohn Dyson #ifndef PIPSIZ 6375dce41c5SJohn Dyson #define PIPSIZ 8192 6385dce41c5SJohn Dyson #endif 639f708ef1bSPoul-Henning Kamp static u_long unpst_sendspace = PIPSIZ; 640f708ef1bSPoul-Henning Kamp static u_long unpst_recvspace = PIPSIZ; 641f708ef1bSPoul-Henning Kamp static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 642f708ef1bSPoul-Henning Kamp static u_long unpdg_recvspace = 4*1024; 643df8bae1dSRodney W. Grimes 644f708ef1bSPoul-Henning Kamp static int unp_rights; /* file descriptors in flight */ 645df8bae1dSRodney W. Grimes 646ce02431fSDoug Rabson SYSCTL_DECL(_net_local_stream); 647639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 648639acc13SGarrett Wollman &unpst_sendspace, 0, ""); 649639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 650639acc13SGarrett Wollman &unpst_recvspace, 0, ""); 651ce02431fSDoug Rabson SYSCTL_DECL(_net_local_dgram); 652639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 653639acc13SGarrett Wollman &unpdg_sendspace, 0, ""); 654639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 655639acc13SGarrett Wollman &unpdg_recvspace, 0, ""); 656ce02431fSDoug Rabson SYSCTL_DECL(_net_local); 657639acc13SGarrett Wollman SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); 658639acc13SGarrett Wollman 659f708ef1bSPoul-Henning Kamp static int 660df8bae1dSRodney W. Grimes unp_attach(so) 661df8bae1dSRodney W. Grimes struct socket *so; 662df8bae1dSRodney W. Grimes { 663df8bae1dSRodney W. Grimes register struct unpcb *unp; 664df8bae1dSRodney W. Grimes int error; 665df8bae1dSRodney W. Grimes 666df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 667df8bae1dSRodney W. Grimes switch (so->so_type) { 668df8bae1dSRodney W. Grimes 669df8bae1dSRodney W. Grimes case SOCK_STREAM: 670df8bae1dSRodney W. Grimes error = soreserve(so, unpst_sendspace, unpst_recvspace); 671df8bae1dSRodney W. Grimes break; 672df8bae1dSRodney W. Grimes 673df8bae1dSRodney W. Grimes case SOCK_DGRAM: 674df8bae1dSRodney W. Grimes error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 675df8bae1dSRodney W. Grimes break; 676df8bae1dSRodney W. Grimes 677df8bae1dSRodney W. Grimes default: 678df8bae1dSRodney W. Grimes panic("unp_attach"); 679df8bae1dSRodney W. Grimes } 680df8bae1dSRodney W. Grimes if (error) 681df8bae1dSRodney W. Grimes return (error); 682df8bae1dSRodney W. Grimes } 683a163d034SWarner Losh unp = uma_zalloc(unp_zone, M_WAITOK); 68457bf258eSGarrett Wollman if (unp == NULL) 685df8bae1dSRodney W. Grimes return (ENOBUFS); 68657bf258eSGarrett Wollman bzero(unp, sizeof *unp); 68798271db4SGarrett Wollman LIST_INIT(&unp->unp_refs); 688df8bae1dSRodney W. Grimes unp->unp_socket = so; 6890d9ce3a1SRobert Watson 6900d9ce3a1SRobert Watson UNP_LOCK(); 6910d9ce3a1SRobert Watson unp->unp_gencnt = ++unp_gencnt; 6920d9ce3a1SRobert Watson unp_count++; 69398271db4SGarrett Wollman LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead 69498271db4SGarrett Wollman : &unp_shead, unp, unp_link); 69540f2ac28SRobert Watson so->so_pcb = unp; 6960d9ce3a1SRobert Watson UNP_UNLOCK(); 6970d9ce3a1SRobert Watson 698df8bae1dSRodney W. Grimes return (0); 699df8bae1dSRodney W. Grimes } 700df8bae1dSRodney W. Grimes 701f708ef1bSPoul-Henning Kamp static void 702df8bae1dSRodney W. Grimes unp_detach(unp) 703df8bae1dSRodney W. Grimes register struct unpcb *unp; 704df8bae1dSRodney W. Grimes { 7050d9ce3a1SRobert Watson struct vnode *vp; 7060d9ce3a1SRobert Watson 7070d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 7080d9ce3a1SRobert Watson 70998271db4SGarrett Wollman LIST_REMOVE(unp, unp_link); 71098271db4SGarrett Wollman unp->unp_gencnt = ++unp_gencnt; 71198271db4SGarrett Wollman --unp_count; 7120d9ce3a1SRobert Watson if ((vp = unp->unp_vnode) != NULL) { 7130d9ce3a1SRobert Watson /* 7140d9ce3a1SRobert Watson * XXXRW: should v_socket be frobbed only while holding 7150d9ce3a1SRobert Watson * Giant? 7160d9ce3a1SRobert Watson */ 717fc3fcacfSRobert Watson unp->unp_vnode->v_socket = NULL; 718fc3fcacfSRobert Watson unp->unp_vnode = NULL; 719df8bae1dSRodney W. Grimes } 720fc3fcacfSRobert Watson if (unp->unp_conn != NULL) 721df8bae1dSRodney W. Grimes unp_disconnect(unp); 7220d9ce3a1SRobert Watson while (!LIST_EMPTY(&unp->unp_refs)) { 7230d9ce3a1SRobert Watson struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 7240d9ce3a1SRobert Watson unp_drop(ref, ECONNRESET); 7250d9ce3a1SRobert Watson } 726df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 727fc3fcacfSRobert Watson unp->unp_socket->so_pcb = NULL; 728df8bae1dSRodney W. Grimes if (unp_rights) { 729df8bae1dSRodney W. Grimes /* 730df8bae1dSRodney W. Grimes * Normally the receive buffer is flushed later, 731df8bae1dSRodney W. Grimes * in sofree, but if our receive buffer holds references 732df8bae1dSRodney W. Grimes * to descriptors that are now garbage, we will dispose 733df8bae1dSRodney W. Grimes * of those descriptor references after the garbage collector 734df8bae1dSRodney W. Grimes * gets them (resulting in a "panic: closef: count < 0"). 735df8bae1dSRodney W. Grimes */ 736df8bae1dSRodney W. Grimes sorflush(unp->unp_socket); 737df8bae1dSRodney W. Grimes unp_gc(); 738df8bae1dSRodney W. Grimes } 739a5993a97SRobert Watson UNP_UNLOCK(); 740fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 74157bf258eSGarrett Wollman FREE(unp->unp_addr, M_SONAME); 7429e9d298aSJeff Roberson uma_zfree(unp_zone, unp); 7430d9ce3a1SRobert Watson if (vp) { 7440d9ce3a1SRobert Watson mtx_lock(&Giant); 7450d9ce3a1SRobert Watson vrele(vp); 7460d9ce3a1SRobert Watson mtx_unlock(&Giant); 7470d9ce3a1SRobert Watson } 748df8bae1dSRodney W. Grimes } 749df8bae1dSRodney W. Grimes 750f708ef1bSPoul-Henning Kamp static int 751b40ce416SJulian Elischer unp_bind(unp, nam, td) 752df8bae1dSRodney W. Grimes struct unpcb *unp; 75357bf258eSGarrett Wollman struct sockaddr *nam; 754b40ce416SJulian Elischer struct thread *td; 755df8bae1dSRodney W. Grimes { 75657bf258eSGarrett Wollman struct sockaddr_un *soun = (struct sockaddr_un *)nam; 757f2a2857bSKirk McKusick struct vnode *vp; 758f2a2857bSKirk McKusick struct mount *mp; 759df8bae1dSRodney W. Grimes struct vattr vattr; 76057bf258eSGarrett Wollman int error, namelen; 761df8bae1dSRodney W. Grimes struct nameidata nd; 7628f364875SJulian Elischer char *buf; 763df8bae1dSRodney W. Grimes 76440f2ac28SRobert Watson UNP_LOCK_ASSERT(); 76540f2ac28SRobert Watson 7660d9ce3a1SRobert Watson /* 7670d9ce3a1SRobert Watson * XXXRW: This test-and-set of unp_vnode is non-atomic; the 7680d9ce3a1SRobert Watson * unlocked read here is fine, but the value of unp_vnode needs 7690d9ce3a1SRobert Watson * to be tested again after we do all the lookups to see if the 7700d9ce3a1SRobert Watson * pcb is still unbound? 7710d9ce3a1SRobert Watson */ 772df8bae1dSRodney W. Grimes if (unp->unp_vnode != NULL) 773df8bae1dSRodney W. Grimes return (EINVAL); 77455c85568SRobert Drehmel 77557bf258eSGarrett Wollman namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 77657bf258eSGarrett Wollman if (namelen <= 0) 777e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 77855c85568SRobert Drehmel 77940f2ac28SRobert Watson UNP_UNLOCK(); 78040f2ac28SRobert Watson 781a163d034SWarner Losh buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 78255c85568SRobert Drehmel strlcpy(buf, soun->sun_path, namelen + 1); 78355c85568SRobert Drehmel 7840d9ce3a1SRobert Watson mtx_lock(&Giant); 785f2a2857bSKirk McKusick restart: 7860d9ce3a1SRobert Watson mtx_assert(&Giant, MA_OWNED); 787b65f6f6bSRobert Watson NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME, UIO_SYSSPACE, 788b40ce416SJulian Elischer buf, td); 789df8bae1dSRodney W. Grimes /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 790797f2d22SPoul-Henning Kamp error = namei(&nd); 7910d9ce3a1SRobert Watson if (error) 7920d9ce3a1SRobert Watson goto done; 793df8bae1dSRodney W. Grimes vp = nd.ni_vp; 794f2a2857bSKirk McKusick if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 795762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 796df8bae1dSRodney W. Grimes if (nd.ni_dvp == vp) 797df8bae1dSRodney W. Grimes vrele(nd.ni_dvp); 798df8bae1dSRodney W. Grimes else 799df8bae1dSRodney W. Grimes vput(nd.ni_dvp); 800f2a2857bSKirk McKusick if (vp != NULL) { 801df8bae1dSRodney W. Grimes vrele(vp); 8020d9ce3a1SRobert Watson error = EADDRINUSE; 8030d9ce3a1SRobert Watson goto done; 804df8bae1dSRodney W. Grimes } 8058f364875SJulian Elischer error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 8060d9ce3a1SRobert Watson if (error) 8070d9ce3a1SRobert Watson goto done; 808f2a2857bSKirk McKusick goto restart; 809f2a2857bSKirk McKusick } 810df8bae1dSRodney W. Grimes VATTR_NULL(&vattr); 811df8bae1dSRodney W. Grimes vattr.va_type = VSOCK; 812b40ce416SJulian Elischer vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 8136ea48a90SRobert Watson #ifdef MAC 8146ea48a90SRobert Watson error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 8156ea48a90SRobert Watson &vattr); 8166151efaaSRobert Watson #endif 8176ea48a90SRobert Watson if (error == 0) { 818a854ed98SJohn Baldwin VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE); 8197be2d300SMike Smith error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 8206ea48a90SRobert Watson } 821762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 8227be2d300SMike Smith vput(nd.ni_dvp); 8230d9ce3a1SRobert Watson if (error) 8240d9ce3a1SRobert Watson goto done; 825df8bae1dSRodney W. Grimes vp = nd.ni_vp; 8260d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_bind"); 8270d9ce3a1SRobert Watson soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 8280d9ce3a1SRobert Watson UNP_LOCK(); 829df8bae1dSRodney W. Grimes vp->v_socket = unp->unp_socket; 830df8bae1dSRodney W. Grimes unp->unp_vnode = vp; 8310d9ce3a1SRobert Watson unp->unp_addr = soun; 8320d9ce3a1SRobert Watson UNP_UNLOCK(); 833b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 834f2a2857bSKirk McKusick vn_finished_write(mp); 8350d9ce3a1SRobert Watson done: 8360d9ce3a1SRobert Watson mtx_unlock(&Giant); 8378f364875SJulian Elischer free(buf, M_TEMP); 83840f2ac28SRobert Watson UNP_LOCK(); 8390d9ce3a1SRobert Watson return (error); 840df8bae1dSRodney W. Grimes } 841df8bae1dSRodney W. Grimes 842f708ef1bSPoul-Henning Kamp static int 843b40ce416SJulian Elischer unp_connect(so, nam, td) 844df8bae1dSRodney W. Grimes struct socket *so; 84557bf258eSGarrett Wollman struct sockaddr *nam; 846b40ce416SJulian Elischer struct thread *td; 847df8bae1dSRodney W. Grimes { 84857bf258eSGarrett Wollman register struct sockaddr_un *soun = (struct sockaddr_un *)nam; 849df8bae1dSRodney W. Grimes register struct vnode *vp; 850df8bae1dSRodney W. Grimes register struct socket *so2, *so3; 851b295bdcdSRobert Watson struct unpcb *unp, *unp2, *unp3; 85257bf258eSGarrett Wollman int error, len; 853df8bae1dSRodney W. Grimes struct nameidata nd; 85457bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 8550d9ce3a1SRobert Watson struct sockaddr *sa; 8560d9ce3a1SRobert Watson 8570d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 858b295bdcdSRobert Watson unp = sotounpcb(so); 859df8bae1dSRodney W. Grimes 86057bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 86157bf258eSGarrett Wollman if (len <= 0) 862e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 86355c85568SRobert Drehmel strlcpy(buf, soun->sun_path, len + 1); 8640d9ce3a1SRobert Watson UNP_UNLOCK(); 8650d9ce3a1SRobert Watson sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 8660d9ce3a1SRobert Watson mtx_lock(&Giant); 867b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td); 868797f2d22SPoul-Henning Kamp error = namei(&nd); 869797f2d22SPoul-Henning Kamp if (error) 8700d9ce3a1SRobert Watson vp = NULL; 8710d9ce3a1SRobert Watson else 872df8bae1dSRodney W. Grimes vp = nd.ni_vp; 8730d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_connect"); 874762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 8750d9ce3a1SRobert Watson if (error) 8760d9ce3a1SRobert Watson goto bad; 8770d9ce3a1SRobert Watson 878df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 879df8bae1dSRodney W. Grimes error = ENOTSOCK; 880df8bae1dSRodney W. Grimes goto bad; 881df8bae1dSRodney W. Grimes } 882a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 883797f2d22SPoul-Henning Kamp if (error) 884df8bae1dSRodney W. Grimes goto bad; 8852260c03dSRobert Watson mtx_unlock(&Giant); 8862260c03dSRobert Watson UNP_LOCK(); 887b295bdcdSRobert Watson unp = sotounpcb(so); 888b295bdcdSRobert Watson if (unp == NULL) { 889b295bdcdSRobert Watson /* 890b295bdcdSRobert Watson * XXXRW: Temporary debugging printf. 891b295bdcdSRobert Watson */ 892b295bdcdSRobert Watson printf("unp_connect(): lost race to another thread\n"); 893b295bdcdSRobert Watson error = EINVAL; 894b295bdcdSRobert Watson goto bad2; 895b295bdcdSRobert Watson } 896df8bae1dSRodney W. Grimes so2 = vp->v_socket; 897fc3fcacfSRobert Watson if (so2 == NULL) { 898df8bae1dSRodney W. Grimes error = ECONNREFUSED; 8992260c03dSRobert Watson goto bad2; 900df8bae1dSRodney W. Grimes } 901df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 902df8bae1dSRodney W. Grimes error = EPROTOTYPE; 9032260c03dSRobert Watson goto bad2; 904df8bae1dSRodney W. Grimes } 905df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 9060d9ce3a1SRobert Watson if (so2->so_options & SO_ACCEPTCONN) { 9070d9ce3a1SRobert Watson /* 9080d9ce3a1SRobert Watson * NB: drop locks here so unp_attach is entered 9090d9ce3a1SRobert Watson * w/o locks; this avoids a recursive lock 9100d9ce3a1SRobert Watson * of the head and holding sleep locks across 9110d9ce3a1SRobert Watson * a (potentially) blocking malloc. 9120d9ce3a1SRobert Watson */ 9130d9ce3a1SRobert Watson UNP_UNLOCK(); 9140d9ce3a1SRobert Watson so3 = sonewconn(so2, 0); 9150d9ce3a1SRobert Watson UNP_LOCK(); 9160d9ce3a1SRobert Watson } else 9170d9ce3a1SRobert Watson so3 = NULL; 9180d9ce3a1SRobert Watson if (so3 == NULL) { 919df8bae1dSRodney W. Grimes error = ECONNREFUSED; 9200d9ce3a1SRobert Watson goto bad2; 921df8bae1dSRodney W. Grimes } 9220c1bb4fbSDima Dorfman unp = sotounpcb(so); 923df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 924df8bae1dSRodney W. Grimes unp3 = sotounpcb(so3); 9250d9ce3a1SRobert Watson if (unp2->unp_addr != NULL) { 9260d9ce3a1SRobert Watson bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 9270d9ce3a1SRobert Watson unp3->unp_addr = (struct sockaddr_un *) sa; 9280d9ce3a1SRobert Watson sa = NULL; 9290d9ce3a1SRobert Watson } 9300c1bb4fbSDima Dorfman /* 9310c1bb4fbSDima Dorfman * unp_peercred management: 9320c1bb4fbSDima Dorfman * 9330c1bb4fbSDima Dorfman * The connecter's (client's) credentials are copied 9340c1bb4fbSDima Dorfman * from its process structure at the time of connect() 9350c1bb4fbSDima Dorfman * (which is now). 9360c1bb4fbSDima Dorfman */ 937a854ed98SJohn Baldwin cru2x(td->td_ucred, &unp3->unp_peercred); 9380c1bb4fbSDima Dorfman unp3->unp_flags |= UNP_HAVEPC; 9390c1bb4fbSDima Dorfman /* 9400c1bb4fbSDima Dorfman * The receiver's (server's) credentials are copied 9410c1bb4fbSDima Dorfman * from the unp_peercred member of socket on which the 9420c1bb4fbSDima Dorfman * former called listen(); unp_listen() cached that 9430c1bb4fbSDima Dorfman * process's credentials at that time so we can use 9440c1bb4fbSDima Dorfman * them now. 9450c1bb4fbSDima Dorfman */ 9460c1bb4fbSDima Dorfman KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 9470c1bb4fbSDima Dorfman ("unp_connect: listener without cached peercred")); 9480c1bb4fbSDima Dorfman memcpy(&unp->unp_peercred, &unp2->unp_peercred, 9490c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 9500c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPC; 951335654d7SRobert Watson #ifdef MAC 952310e7cebSRobert Watson SOCK_LOCK(so); 953335654d7SRobert Watson mac_set_socket_peer_from_socket(so, so3); 954335654d7SRobert Watson mac_set_socket_peer_from_socket(so3, so); 955310e7cebSRobert Watson SOCK_UNLOCK(so); 956335654d7SRobert Watson #endif 9570c1bb4fbSDima Dorfman 958df8bae1dSRodney W. Grimes so2 = so3; 959df8bae1dSRodney W. Grimes } 960df8bae1dSRodney W. Grimes error = unp_connect2(so, so2); 9610d9ce3a1SRobert Watson bad2: 9620d9ce3a1SRobert Watson UNP_UNLOCK(); 9630d9ce3a1SRobert Watson mtx_lock(&Giant); 964df8bae1dSRodney W. Grimes bad: 9650d9ce3a1SRobert Watson mtx_assert(&Giant, MA_OWNED); 9660d9ce3a1SRobert Watson if (vp != NULL) 967df8bae1dSRodney W. Grimes vput(vp); 9680d9ce3a1SRobert Watson mtx_unlock(&Giant); 9690d9ce3a1SRobert Watson free(sa, M_SONAME); 9700d9ce3a1SRobert Watson UNP_LOCK(); 971df8bae1dSRodney W. Grimes return (error); 972df8bae1dSRodney W. Grimes } 973df8bae1dSRodney W. Grimes 974db48c0d2SRobert Watson static int 975df8bae1dSRodney W. Grimes unp_connect2(so, so2) 976df8bae1dSRodney W. Grimes register struct socket *so; 977df8bae1dSRodney W. Grimes register struct socket *so2; 978df8bae1dSRodney W. Grimes { 979df8bae1dSRodney W. Grimes register struct unpcb *unp = sotounpcb(so); 980df8bae1dSRodney W. Grimes register struct unpcb *unp2; 981df8bae1dSRodney W. Grimes 9820d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 9830d9ce3a1SRobert Watson 984df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 985df8bae1dSRodney W. Grimes return (EPROTOTYPE); 986df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 987df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 988df8bae1dSRodney W. Grimes switch (so->so_type) { 989df8bae1dSRodney W. Grimes 990df8bae1dSRodney W. Grimes case SOCK_DGRAM: 99198271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 992df8bae1dSRodney W. Grimes soisconnected(so); 993df8bae1dSRodney W. Grimes break; 994df8bae1dSRodney W. Grimes 995df8bae1dSRodney W. Grimes case SOCK_STREAM: 996df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 997df8bae1dSRodney W. Grimes soisconnected(so); 998df8bae1dSRodney W. Grimes soisconnected(so2); 999df8bae1dSRodney W. Grimes break; 1000df8bae1dSRodney W. Grimes 1001df8bae1dSRodney W. Grimes default: 1002df8bae1dSRodney W. Grimes panic("unp_connect2"); 1003df8bae1dSRodney W. Grimes } 1004df8bae1dSRodney W. Grimes return (0); 1005df8bae1dSRodney W. Grimes } 1006df8bae1dSRodney W. Grimes 1007f708ef1bSPoul-Henning Kamp static void 1008df8bae1dSRodney W. Grimes unp_disconnect(unp) 1009df8bae1dSRodney W. Grimes struct unpcb *unp; 1010df8bae1dSRodney W. Grimes { 1011df8bae1dSRodney W. Grimes register struct unpcb *unp2 = unp->unp_conn; 10121b2e3b4bSRobert Watson struct socket *so; 1013df8bae1dSRodney W. Grimes 10140d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 10150d9ce3a1SRobert Watson 1016fc3fcacfSRobert Watson if (unp2 == NULL) 1017df8bae1dSRodney W. Grimes return; 1018fc3fcacfSRobert Watson unp->unp_conn = NULL; 1019df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 1020df8bae1dSRodney W. Grimes 1021df8bae1dSRodney W. Grimes case SOCK_DGRAM: 102298271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 10231b2e3b4bSRobert Watson so = unp->unp_socket; 10241b2e3b4bSRobert Watson SOCK_LOCK(so); 10251b2e3b4bSRobert Watson so->so_state &= ~SS_ISCONNECTED; 10261b2e3b4bSRobert Watson SOCK_UNLOCK(so); 1027df8bae1dSRodney W. Grimes break; 1028df8bae1dSRodney W. Grimes 1029df8bae1dSRodney W. Grimes case SOCK_STREAM: 1030df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 1031fc3fcacfSRobert Watson unp2->unp_conn = NULL; 1032df8bae1dSRodney W. Grimes soisdisconnected(unp2->unp_socket); 1033df8bae1dSRodney W. Grimes break; 1034df8bae1dSRodney W. Grimes } 1035df8bae1dSRodney W. Grimes } 1036df8bae1dSRodney W. Grimes 1037df8bae1dSRodney W. Grimes #ifdef notdef 103826f9a767SRodney W. Grimes void 1039df8bae1dSRodney W. Grimes unp_abort(unp) 1040df8bae1dSRodney W. Grimes struct unpcb *unp; 1041df8bae1dSRodney W. Grimes { 1042df8bae1dSRodney W. Grimes 1043df8bae1dSRodney W. Grimes unp_detach(unp); 1044df8bae1dSRodney W. Grimes } 1045df8bae1dSRodney W. Grimes #endif 1046df8bae1dSRodney W. Grimes 10470d9ce3a1SRobert Watson /* 10480d9ce3a1SRobert Watson * unp_pcblist() assumes that UNIX domain socket memory is never reclaimed 10490d9ce3a1SRobert Watson * by the zone (UMA_ZONE_NOFREE), and as such potentially stale pointers 10500d9ce3a1SRobert Watson * are safe to reference. It first scans the list of struct unpcb's to 10510d9ce3a1SRobert Watson * generate a pointer list, then it rescans its list one entry at a time to 10520d9ce3a1SRobert Watson * externalize and copyout. It checks the generation number to see if a 10530d9ce3a1SRobert Watson * struct unpcb has been reused, and will skip it if so. 10540d9ce3a1SRobert Watson */ 105598271db4SGarrett Wollman static int 105682d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 105798271db4SGarrett Wollman { 1058f5ef029eSPoul-Henning Kamp int error, i, n; 105998271db4SGarrett Wollman struct unpcb *unp, **unp_list; 106098271db4SGarrett Wollman unp_gen_t gencnt; 10618f364875SJulian Elischer struct xunpgen *xug; 106298271db4SGarrett Wollman struct unp_head *head; 10638f364875SJulian Elischer struct xunpcb *xu; 106498271db4SGarrett Wollman 1065a23d65bfSBruce Evans head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 106698271db4SGarrett Wollman 106798271db4SGarrett Wollman /* 106898271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 106998271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 107098271db4SGarrett Wollman */ 1071fc3fcacfSRobert Watson if (req->oldptr == NULL) { 107298271db4SGarrett Wollman n = unp_count; 10738f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug) 107498271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 1075e5aeaa0cSDag-Erling Smørgrav return (0); 107698271db4SGarrett Wollman } 107798271db4SGarrett Wollman 1078fc3fcacfSRobert Watson if (req->newptr != NULL) 1079e5aeaa0cSDag-Erling Smørgrav return (EPERM); 108098271db4SGarrett Wollman 108198271db4SGarrett Wollman /* 108298271db4SGarrett Wollman * OK, now we're committed to doing something. 108398271db4SGarrett Wollman */ 1084a163d034SWarner Losh xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 10850d9ce3a1SRobert Watson UNP_LOCK(); 108698271db4SGarrett Wollman gencnt = unp_gencnt; 108798271db4SGarrett Wollman n = unp_count; 10880d9ce3a1SRobert Watson UNP_UNLOCK(); 108998271db4SGarrett Wollman 10908f364875SJulian Elischer xug->xug_len = sizeof *xug; 10918f364875SJulian Elischer xug->xug_count = n; 10928f364875SJulian Elischer xug->xug_gen = gencnt; 10938f364875SJulian Elischer xug->xug_sogen = so_gencnt; 10948f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 10958f364875SJulian Elischer if (error) { 10968f364875SJulian Elischer free(xug, M_TEMP); 1097e5aeaa0cSDag-Erling Smørgrav return (error); 10988f364875SJulian Elischer } 109998271db4SGarrett Wollman 1100a163d034SWarner Losh unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 110198271db4SGarrett Wollman 11020d9ce3a1SRobert Watson UNP_LOCK(); 11032e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 11042e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 11058a7d8cc6SRobert Watson if (unp->unp_gencnt <= gencnt) { 1106a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred, 11078a7d8cc6SRobert Watson unp->unp_socket->so_cred)) 11084787fd37SPaul Saab continue; 110998271db4SGarrett Wollman unp_list[i++] = unp; 111098271db4SGarrett Wollman } 11114787fd37SPaul Saab } 11120d9ce3a1SRobert Watson UNP_UNLOCK(); 111398271db4SGarrett Wollman n = i; /* in case we lost some during malloc */ 111498271db4SGarrett Wollman 111598271db4SGarrett Wollman error = 0; 1116a163d034SWarner Losh xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK); 111798271db4SGarrett Wollman for (i = 0; i < n; i++) { 111898271db4SGarrett Wollman unp = unp_list[i]; 111998271db4SGarrett Wollman if (unp->unp_gencnt <= gencnt) { 11208f364875SJulian Elischer xu->xu_len = sizeof *xu; 11218f364875SJulian Elischer xu->xu_unpp = unp; 112298271db4SGarrett Wollman /* 112398271db4SGarrett Wollman * XXX - need more locking here to protect against 112498271db4SGarrett Wollman * connect/disconnect races for SMP. 112598271db4SGarrett Wollman */ 1126fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 11278f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr, 112898271db4SGarrett Wollman unp->unp_addr->sun_len); 1129fc3fcacfSRobert Watson if (unp->unp_conn != NULL && 1130fc3fcacfSRobert Watson unp->unp_conn->unp_addr != NULL) 113198271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 11328f364875SJulian Elischer &xu->xu_caddr, 113398271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 11348f364875SJulian Elischer bcopy(unp, &xu->xu_unp, sizeof *unp); 11358f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket); 11368f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu); 113798271db4SGarrett Wollman } 113898271db4SGarrett Wollman } 11398f364875SJulian Elischer free(xu, M_TEMP); 114098271db4SGarrett Wollman if (!error) { 114198271db4SGarrett Wollman /* 114298271db4SGarrett Wollman * Give the user an updated idea of our state. 114398271db4SGarrett Wollman * If the generation differs from what we told 114498271db4SGarrett Wollman * her before, she knows that something happened 114598271db4SGarrett Wollman * while we were processing this request, and it 114698271db4SGarrett Wollman * might be necessary to retry. 114798271db4SGarrett Wollman */ 11488f364875SJulian Elischer xug->xug_gen = unp_gencnt; 11498f364875SJulian Elischer xug->xug_sogen = so_gencnt; 11508f364875SJulian Elischer xug->xug_count = unp_count; 11518f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 115298271db4SGarrett Wollman } 115398271db4SGarrett Wollman free(unp_list, M_TEMP); 11548f364875SJulian Elischer free(xug, M_TEMP); 1155e5aeaa0cSDag-Erling Smørgrav return (error); 115698271db4SGarrett Wollman } 115798271db4SGarrett Wollman 115898271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 115998271db4SGarrett Wollman (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 116098271db4SGarrett Wollman "List of active local datagram sockets"); 116198271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 116298271db4SGarrett Wollman (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 116398271db4SGarrett Wollman "List of active local stream sockets"); 116498271db4SGarrett Wollman 1165f708ef1bSPoul-Henning Kamp static void 1166df8bae1dSRodney W. Grimes unp_shutdown(unp) 1167df8bae1dSRodney W. Grimes struct unpcb *unp; 1168df8bae1dSRodney W. Grimes { 1169df8bae1dSRodney W. Grimes struct socket *so; 1170df8bae1dSRodney W. Grimes 11710d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 11720d9ce3a1SRobert Watson 1173df8bae1dSRodney W. Grimes if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 1174df8bae1dSRodney W. Grimes (so = unp->unp_conn->unp_socket)) 1175df8bae1dSRodney W. Grimes socantrcvmore(so); 1176df8bae1dSRodney W. Grimes } 1177df8bae1dSRodney W. Grimes 1178f708ef1bSPoul-Henning Kamp static void 1179df8bae1dSRodney W. Grimes unp_drop(unp, errno) 1180df8bae1dSRodney W. Grimes struct unpcb *unp; 1181df8bae1dSRodney W. Grimes int errno; 1182df8bae1dSRodney W. Grimes { 1183df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 1184df8bae1dSRodney W. Grimes 11850d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 11860d9ce3a1SRobert Watson 1187df8bae1dSRodney W. Grimes so->so_error = errno; 1188df8bae1dSRodney W. Grimes unp_disconnect(unp); 1189df8bae1dSRodney W. Grimes } 1190df8bae1dSRodney W. Grimes 1191df8bae1dSRodney W. Grimes #ifdef notdef 119226f9a767SRodney W. Grimes void 1193df8bae1dSRodney W. Grimes unp_drain() 1194df8bae1dSRodney W. Grimes { 1195df8bae1dSRodney W. Grimes 1196df8bae1dSRodney W. Grimes } 1197df8bae1dSRodney W. Grimes #endif 1198df8bae1dSRodney W. Grimes 11992bc21ed9SDavid Malone static void 12002bc21ed9SDavid Malone unp_freerights(rp, fdcount) 12012bc21ed9SDavid Malone struct file **rp; 12022bc21ed9SDavid Malone int fdcount; 1203df8bae1dSRodney W. Grimes { 12042bc21ed9SDavid Malone int i; 12052bc21ed9SDavid Malone struct file *fp; 1206df8bae1dSRodney W. Grimes 12072bc21ed9SDavid Malone for (i = 0; i < fdcount; i++) { 1208df8bae1dSRodney W. Grimes fp = *rp; 12098692c025SYoshinobu Inoue /* 12102bc21ed9SDavid Malone * zero the pointer before calling 12112bc21ed9SDavid Malone * unp_discard since it may end up 12122bc21ed9SDavid Malone * in unp_gc().. 12138692c025SYoshinobu Inoue */ 1214df8bae1dSRodney W. Grimes *rp++ = 0; 12158692c025SYoshinobu Inoue unp_discard(fp); 1216df8bae1dSRodney W. Grimes } 12172bc21ed9SDavid Malone } 12182bc21ed9SDavid Malone 12192bc21ed9SDavid Malone int 12202bc21ed9SDavid Malone unp_externalize(control, controlp) 12212bc21ed9SDavid Malone struct mbuf *control, **controlp; 12222bc21ed9SDavid Malone { 12232bc21ed9SDavid Malone struct thread *td = curthread; /* XXX */ 12242bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 12252bc21ed9SDavid Malone int i; 12262bc21ed9SDavid Malone int *fdp; 12272bc21ed9SDavid Malone struct file **rp; 12282bc21ed9SDavid Malone struct file *fp; 12292bc21ed9SDavid Malone void *data; 12302bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 12312bc21ed9SDavid Malone int error, newfds; 12322bc21ed9SDavid Malone int f; 12332bc21ed9SDavid Malone u_int newlen; 12342bc21ed9SDavid Malone 12352bc21ed9SDavid Malone error = 0; 12362bc21ed9SDavid Malone if (controlp != NULL) /* controlp == NULL => free control messages */ 12372bc21ed9SDavid Malone *controlp = NULL; 12382bc21ed9SDavid Malone 12392bc21ed9SDavid Malone while (cm != NULL) { 12402bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 12412bc21ed9SDavid Malone error = EINVAL; 12422bc21ed9SDavid Malone break; 12432bc21ed9SDavid Malone } 12442bc21ed9SDavid Malone 12452bc21ed9SDavid Malone data = CMSG_DATA(cm); 12462bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 12472bc21ed9SDavid Malone 12482bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET 12492bc21ed9SDavid Malone && cm->cmsg_type == SCM_RIGHTS) { 12502bc21ed9SDavid Malone newfds = datalen / sizeof(struct file *); 12512bc21ed9SDavid Malone rp = data; 12522bc21ed9SDavid Malone 1253e2f9a08bSOlivier Houchard /* If we're not outputting the descriptors free them. */ 12542bc21ed9SDavid Malone if (error || controlp == NULL) { 12552bc21ed9SDavid Malone unp_freerights(rp, newfds); 12562bc21ed9SDavid Malone goto next; 12572bc21ed9SDavid Malone } 1258426da3bcSAlfred Perlstein FILEDESC_LOCK(td->td_proc->p_fd); 12592bc21ed9SDavid Malone /* if the new FD's will not fit free them. */ 12602bc21ed9SDavid Malone if (!fdavail(td, newfds)) { 1261426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 12622bc21ed9SDavid Malone error = EMSGSIZE; 12632bc21ed9SDavid Malone unp_freerights(rp, newfds); 12642bc21ed9SDavid Malone goto next; 1265df8bae1dSRodney W. Grimes } 1266ed5b7817SJulian Elischer /* 12672bc21ed9SDavid Malone * now change each pointer to an fd in the global 12682bc21ed9SDavid Malone * table to an integer that is the index to the 12692bc21ed9SDavid Malone * local fd table entry that we set up to point 12702bc21ed9SDavid Malone * to the global one we are transferring. 1271ed5b7817SJulian Elischer */ 12722bc21ed9SDavid Malone newlen = newfds * sizeof(int); 12732bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 12742bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 12752bc21ed9SDavid Malone if (*controlp == NULL) { 1276426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 12772bc21ed9SDavid Malone error = E2BIG; 12782bc21ed9SDavid Malone unp_freerights(rp, newfds); 12792bc21ed9SDavid Malone goto next; 12802bc21ed9SDavid Malone } 12812bc21ed9SDavid Malone 12822bc21ed9SDavid Malone fdp = (int *) 12832bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1284df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 1285a6d4491cSDag-Erling Smørgrav if (fdalloc(td, 0, &f)) 12862bc21ed9SDavid Malone panic("unp_externalize fdalloc failed"); 12878692c025SYoshinobu Inoue fp = *rp++; 1288b40ce416SJulian Elischer td->td_proc->p_fd->fd_ofiles[f] = fp; 1289426da3bcSAlfred Perlstein FILE_LOCK(fp); 1290df8bae1dSRodney W. Grimes fp->f_msgcount--; 1291426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1292df8bae1dSRodney W. Grimes unp_rights--; 12938692c025SYoshinobu Inoue *fdp++ = f; 1294df8bae1dSRodney W. Grimes } 1295426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 12962bc21ed9SDavid Malone } else { /* We can just copy anything else across */ 12972bc21ed9SDavid Malone if (error || controlp == NULL) 12982bc21ed9SDavid Malone goto next; 12992bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, datalen, 13002bc21ed9SDavid Malone cm->cmsg_type, cm->cmsg_level); 13012bc21ed9SDavid Malone if (*controlp == NULL) { 13022bc21ed9SDavid Malone error = ENOBUFS; 13032bc21ed9SDavid Malone goto next; 13042bc21ed9SDavid Malone } 13052bc21ed9SDavid Malone bcopy(data, 13062bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 13072bc21ed9SDavid Malone datalen); 13082bc21ed9SDavid Malone } 13092bc21ed9SDavid Malone 13102bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 13112bc21ed9SDavid Malone 13122bc21ed9SDavid Malone next: 13132bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 13142bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 13152bc21ed9SDavid Malone cm = (struct cmsghdr *) 13162bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 13178692c025SYoshinobu Inoue } else { 13182bc21ed9SDavid Malone clen = 0; 13192bc21ed9SDavid Malone cm = NULL; 13208692c025SYoshinobu Inoue } 13218692c025SYoshinobu Inoue } 13228692c025SYoshinobu Inoue 13232bc21ed9SDavid Malone m_freem(control); 13242bc21ed9SDavid Malone 13252bc21ed9SDavid Malone return (error); 1326df8bae1dSRodney W. Grimes } 1327df8bae1dSRodney W. Grimes 132898271db4SGarrett Wollman void 132998271db4SGarrett Wollman unp_init(void) 133098271db4SGarrett Wollman { 13319e9d298aSJeff Roberson unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 13329e9d298aSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 1333fc3fcacfSRobert Watson if (unp_zone == NULL) 133498271db4SGarrett Wollman panic("unp_init"); 1335b17dd2bcSColin Percival uma_zone_set_max(unp_zone, nmbclusters); 133698271db4SGarrett Wollman LIST_INIT(&unp_dhead); 133798271db4SGarrett Wollman LIST_INIT(&unp_shead); 13380d9ce3a1SRobert Watson 13390d9ce3a1SRobert Watson UNP_LOCK_INIT(); 134098271db4SGarrett Wollman } 134198271db4SGarrett Wollman 1342f708ef1bSPoul-Henning Kamp static int 13432bc21ed9SDavid Malone unp_internalize(controlp, td) 13442bc21ed9SDavid Malone struct mbuf **controlp; 1345b40ce416SJulian Elischer struct thread *td; 1346df8bae1dSRodney W. Grimes { 13472bc21ed9SDavid Malone struct mbuf *control = *controlp; 1348b40ce416SJulian Elischer struct proc *p = td->td_proc; 13498692c025SYoshinobu Inoue struct filedesc *fdescp = p->p_fd; 13502bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 13512bc21ed9SDavid Malone struct cmsgcred *cmcred; 13522bc21ed9SDavid Malone struct file **rp; 13532bc21ed9SDavid Malone struct file *fp; 13542bc21ed9SDavid Malone struct timeval *tv; 13552bc21ed9SDavid Malone int i, fd, *fdp; 13562bc21ed9SDavid Malone void *data; 13572bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 13582bc21ed9SDavid Malone int error, oldfds; 13598692c025SYoshinobu Inoue u_int newlen; 1360df8bae1dSRodney W. Grimes 13612bc21ed9SDavid Malone error = 0; 13622bc21ed9SDavid Malone *controlp = NULL; 13630b788fa1SBill Paul 13642bc21ed9SDavid Malone while (cm != NULL) { 13652bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 13662bc21ed9SDavid Malone || cm->cmsg_len > clen) { 13672bc21ed9SDavid Malone error = EINVAL; 13682bc21ed9SDavid Malone goto out; 13692bc21ed9SDavid Malone } 13702bc21ed9SDavid Malone 13712bc21ed9SDavid Malone data = CMSG_DATA(cm); 13722bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 13732bc21ed9SDavid Malone 13742bc21ed9SDavid Malone switch (cm->cmsg_type) { 13750b788fa1SBill Paul /* 13760b788fa1SBill Paul * Fill in credential information. 13770b788fa1SBill Paul */ 13782bc21ed9SDavid Malone case SCM_CREDS: 13792bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 13802bc21ed9SDavid Malone SCM_CREDS, SOL_SOCKET); 13812bc21ed9SDavid Malone if (*controlp == NULL) { 13822bc21ed9SDavid Malone error = ENOBUFS; 13832bc21ed9SDavid Malone goto out; 13842bc21ed9SDavid Malone } 13852bc21ed9SDavid Malone 13862bc21ed9SDavid Malone cmcred = (struct cmsgcred *) 13872bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 13880b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 1389a854ed98SJohn Baldwin cmcred->cmcred_uid = td->td_ucred->cr_ruid; 1390a854ed98SJohn Baldwin cmcred->cmcred_gid = td->td_ucred->cr_rgid; 1391a854ed98SJohn Baldwin cmcred->cmcred_euid = td->td_ucred->cr_uid; 1392a854ed98SJohn Baldwin cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 13930b788fa1SBill Paul CMGROUP_MAX); 13940b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 13952bc21ed9SDavid Malone cmcred->cmcred_groups[i] = 1396a854ed98SJohn Baldwin td->td_ucred->cr_groups[i]; 13972bc21ed9SDavid Malone break; 13980b788fa1SBill Paul 13992bc21ed9SDavid Malone case SCM_RIGHTS: 14002bc21ed9SDavid Malone oldfds = datalen / sizeof (int); 1401ed5b7817SJulian Elischer /* 14022bc21ed9SDavid Malone * check that all the FDs passed in refer to legal files 1403ed5b7817SJulian Elischer * If not, reject the entire operation. 1404ed5b7817SJulian Elischer */ 14052bc21ed9SDavid Malone fdp = data; 1406426da3bcSAlfred Perlstein FILEDESC_LOCK(fdescp); 1407df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 14088692c025SYoshinobu Inoue fd = *fdp++; 14098692c025SYoshinobu Inoue if ((unsigned)fd >= fdescp->fd_nfiles || 14102bc21ed9SDavid Malone fdescp->fd_ofiles[fd] == NULL) { 1411426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 14122bc21ed9SDavid Malone error = EBADF; 14132bc21ed9SDavid Malone goto out; 14142bc21ed9SDavid Malone } 1415e7d6662fSAlfred Perlstein fp = fdescp->fd_ofiles[fd]; 1416e7d6662fSAlfred Perlstein if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 1417e7d6662fSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 1418e7d6662fSAlfred Perlstein error = EOPNOTSUPP; 1419e7d6662fSAlfred Perlstein goto out; 1420e7d6662fSAlfred Perlstein } 1421e7d6662fSAlfred Perlstein 1422df8bae1dSRodney W. Grimes } 1423ed5b7817SJulian Elischer /* 1424ed5b7817SJulian Elischer * Now replace the integer FDs with pointers to 1425ed5b7817SJulian Elischer * the associated global file table entry.. 1426ed5b7817SJulian Elischer */ 14272bc21ed9SDavid Malone newlen = oldfds * sizeof(struct file *); 14282bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 14292bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 14302bc21ed9SDavid Malone if (*controlp == NULL) { 1431426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 14322bc21ed9SDavid Malone error = E2BIG; 14332bc21ed9SDavid Malone goto out; 14348692c025SYoshinobu Inoue } 14358692c025SYoshinobu Inoue 14362bc21ed9SDavid Malone fdp = data; 14372bc21ed9SDavid Malone rp = (struct file **) 14382bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 14398692c025SYoshinobu Inoue for (i = 0; i < oldfds; i++) { 14408692c025SYoshinobu Inoue fp = fdescp->fd_ofiles[*fdp++]; 1441df8bae1dSRodney W. Grimes *rp++ = fp; 1442426da3bcSAlfred Perlstein FILE_LOCK(fp); 1443df8bae1dSRodney W. Grimes fp->f_count++; 1444df8bae1dSRodney W. Grimes fp->f_msgcount++; 1445426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1446df8bae1dSRodney W. Grimes unp_rights++; 1447df8bae1dSRodney W. Grimes } 1448426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 14492bc21ed9SDavid Malone break; 14502bc21ed9SDavid Malone 14512bc21ed9SDavid Malone case SCM_TIMESTAMP: 14522bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*tv), 14532bc21ed9SDavid Malone SCM_TIMESTAMP, SOL_SOCKET); 14542bc21ed9SDavid Malone if (*controlp == NULL) { 14552bc21ed9SDavid Malone error = ENOBUFS; 14562bc21ed9SDavid Malone goto out; 14578692c025SYoshinobu Inoue } 14582bc21ed9SDavid Malone tv = (struct timeval *) 14592bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 14602bc21ed9SDavid Malone microtime(tv); 14612bc21ed9SDavid Malone break; 14622bc21ed9SDavid Malone 14632bc21ed9SDavid Malone default: 14642bc21ed9SDavid Malone error = EINVAL; 14652bc21ed9SDavid Malone goto out; 14662bc21ed9SDavid Malone } 14672bc21ed9SDavid Malone 14682bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 14692bc21ed9SDavid Malone 14702bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 14712bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 14722bc21ed9SDavid Malone cm = (struct cmsghdr *) 14732bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 14742bc21ed9SDavid Malone } else { 14752bc21ed9SDavid Malone clen = 0; 14762bc21ed9SDavid Malone cm = NULL; 14772bc21ed9SDavid Malone } 14782bc21ed9SDavid Malone } 14792bc21ed9SDavid Malone 14802bc21ed9SDavid Malone out: 14812bc21ed9SDavid Malone m_freem(control); 14822bc21ed9SDavid Malone 14832bc21ed9SDavid Malone return (error); 1484df8bae1dSRodney W. Grimes } 1485df8bae1dSRodney W. Grimes 1486f708ef1bSPoul-Henning Kamp static int unp_defer, unp_gcing; 1487df8bae1dSRodney W. Grimes 1488f708ef1bSPoul-Henning Kamp static void 1489df8bae1dSRodney W. Grimes unp_gc() 1490df8bae1dSRodney W. Grimes { 1491df8bae1dSRodney W. Grimes register struct file *fp, *nextfp; 1492df8bae1dSRodney W. Grimes register struct socket *so; 1493df8bae1dSRodney W. Grimes struct file **extra_ref, **fpp; 1494df8bae1dSRodney W. Grimes int nunref, i; 149595f004dcSAlfred Perlstein int nfiles_snap; 149695f004dcSAlfred Perlstein int nfiles_slack = 20; 1497df8bae1dSRodney W. Grimes 14980d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 14990d9ce3a1SRobert Watson 1500df8bae1dSRodney W. Grimes if (unp_gcing) 1501df8bae1dSRodney W. Grimes return; 1502df8bae1dSRodney W. Grimes unp_gcing = 1; 1503df8bae1dSRodney W. Grimes unp_defer = 0; 1504ed5b7817SJulian Elischer /* 1505ed5b7817SJulian Elischer * before going through all this, set all FDs to 1506ed5b7817SJulian Elischer * be NOT defered and NOT externally accessible 1507ed5b7817SJulian Elischer */ 15080d9ce3a1SRobert Watson /* 15090d9ce3a1SRobert Watson * XXXRW: Acquiring a sleep lock while holding UNP 15100d9ce3a1SRobert Watson * mutex cannot be a good thing. 15110d9ce3a1SRobert Watson */ 1512426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 15132e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) 1514426da3bcSAlfred Perlstein fp->f_gcflag &= ~(FMARK|FDEFER); 1515df8bae1dSRodney W. Grimes do { 15162e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) { 1517426da3bcSAlfred Perlstein FILE_LOCK(fp); 1518ed5b7817SJulian Elischer /* 1519ed5b7817SJulian Elischer * If the file is not open, skip it 1520ed5b7817SJulian Elischer */ 1521426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1522426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1523df8bae1dSRodney W. Grimes continue; 1524426da3bcSAlfred Perlstein } 1525ed5b7817SJulian Elischer /* 1526ed5b7817SJulian Elischer * If we already marked it as 'defer' in a 1527ed5b7817SJulian Elischer * previous pass, then try process it this time 1528ed5b7817SJulian Elischer * and un-mark it 1529ed5b7817SJulian Elischer */ 1530426da3bcSAlfred Perlstein if (fp->f_gcflag & FDEFER) { 1531426da3bcSAlfred Perlstein fp->f_gcflag &= ~FDEFER; 1532df8bae1dSRodney W. Grimes unp_defer--; 1533df8bae1dSRodney W. Grimes } else { 1534ed5b7817SJulian Elischer /* 1535ed5b7817SJulian Elischer * if it's not defered, then check if it's 1536ed5b7817SJulian Elischer * already marked.. if so skip it 1537ed5b7817SJulian Elischer */ 1538426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) { 1539426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1540df8bae1dSRodney W. Grimes continue; 1541426da3bcSAlfred Perlstein } 1542ed5b7817SJulian Elischer /* 1543ed5b7817SJulian Elischer * If all references are from messages 1544ed5b7817SJulian Elischer * in transit, then skip it. it's not 1545ed5b7817SJulian Elischer * externally accessible. 1546ed5b7817SJulian Elischer */ 1547426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount) { 1548426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1549df8bae1dSRodney W. Grimes continue; 1550426da3bcSAlfred Perlstein } 1551ed5b7817SJulian Elischer /* 1552ed5b7817SJulian Elischer * If it got this far then it must be 1553ed5b7817SJulian Elischer * externally accessible. 1554ed5b7817SJulian Elischer */ 1555426da3bcSAlfred Perlstein fp->f_gcflag |= FMARK; 1556df8bae1dSRodney W. Grimes } 1557ed5b7817SJulian Elischer /* 1558ed5b7817SJulian Elischer * either it was defered, or it is externally 1559ed5b7817SJulian Elischer * accessible and not already marked so. 1560ed5b7817SJulian Elischer * Now check if it is possibly one of OUR sockets. 1561ed5b7817SJulian Elischer */ 1562df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_SOCKET || 156348e3128bSMatthew Dillon (so = fp->f_data) == NULL) { 1564426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1565df8bae1dSRodney W. Grimes continue; 1566426da3bcSAlfred Perlstein } 1567426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1568748e0b0aSGarrett Wollman if (so->so_proto->pr_domain != &localdomain || 1569df8bae1dSRodney W. Grimes (so->so_proto->pr_flags&PR_RIGHTS) == 0) 1570df8bae1dSRodney W. Grimes continue; 1571df8bae1dSRodney W. Grimes #ifdef notdef 1572df8bae1dSRodney W. Grimes if (so->so_rcv.sb_flags & SB_LOCK) { 1573df8bae1dSRodney W. Grimes /* 1574df8bae1dSRodney W. Grimes * This is problematical; it's not clear 1575df8bae1dSRodney W. Grimes * we need to wait for the sockbuf to be 1576df8bae1dSRodney W. Grimes * unlocked (on a uniprocessor, at least), 1577df8bae1dSRodney W. Grimes * and it's also not clear what to do 1578df8bae1dSRodney W. Grimes * if sbwait returns an error due to receipt 1579df8bae1dSRodney W. Grimes * of a signal. If sbwait does return 1580df8bae1dSRodney W. Grimes * an error, we'll go into an infinite 1581df8bae1dSRodney W. Grimes * loop. Delete all of this for now. 1582df8bae1dSRodney W. Grimes */ 1583df8bae1dSRodney W. Grimes (void) sbwait(&so->so_rcv); 1584df8bae1dSRodney W. Grimes goto restart; 1585df8bae1dSRodney W. Grimes } 1586df8bae1dSRodney W. Grimes #endif 1587ed5b7817SJulian Elischer /* 1588ed5b7817SJulian Elischer * So, Ok, it's one of our sockets and it IS externally 1589ed5b7817SJulian Elischer * accessible (or was defered). Now we look 1590dc733423SDag-Erling Smørgrav * to see if we hold any file descriptors in its 1591ed5b7817SJulian Elischer * message buffers. Follow those links and mark them 1592ed5b7817SJulian Elischer * as accessible too. 1593ed5b7817SJulian Elischer */ 15947717cf07SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1595df8bae1dSRodney W. Grimes unp_scan(so->so_rcv.sb_mb, unp_mark); 15967717cf07SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 1597df8bae1dSRodney W. Grimes } 1598df8bae1dSRodney W. Grimes } while (unp_defer); 1599426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1600df8bae1dSRodney W. Grimes /* 1601df8bae1dSRodney W. Grimes * We grab an extra reference to each of the file table entries 1602df8bae1dSRodney W. Grimes * that are not otherwise accessible and then free the rights 1603df8bae1dSRodney W. Grimes * that are stored in messages on them. 1604df8bae1dSRodney W. Grimes * 1605df8bae1dSRodney W. Grimes * The bug in the orginal code is a little tricky, so I'll describe 1606df8bae1dSRodney W. Grimes * what's wrong with it here. 1607df8bae1dSRodney W. Grimes * 1608df8bae1dSRodney W. Grimes * It is incorrect to simply unp_discard each entry for f_msgcount 1609df8bae1dSRodney W. Grimes * times -- consider the case of sockets A and B that contain 1610df8bae1dSRodney W. Grimes * references to each other. On a last close of some other socket, 1611df8bae1dSRodney W. Grimes * we trigger a gc since the number of outstanding rights (unp_rights) 1612df8bae1dSRodney W. Grimes * is non-zero. If during the sweep phase the gc code un_discards, 1613df8bae1dSRodney W. Grimes * we end up doing a (full) closef on the descriptor. A closef on A 1614df8bae1dSRodney W. Grimes * results in the following chain. Closef calls soo_close, which 1615df8bae1dSRodney W. Grimes * calls soclose. Soclose calls first (through the switch 1616df8bae1dSRodney W. Grimes * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 1617df8bae1dSRodney W. Grimes * returns because the previous instance had set unp_gcing, and 1618df8bae1dSRodney W. Grimes * we return all the way back to soclose, which marks the socket 1619df8bae1dSRodney W. Grimes * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush 1620df8bae1dSRodney W. Grimes * to free up the rights that are queued in messages on the socket A, 1621df8bae1dSRodney W. Grimes * i.e., the reference on B. The sorflush calls via the dom_dispose 1622df8bae1dSRodney W. Grimes * switch unp_dispose, which unp_scans with unp_discard. This second 1623df8bae1dSRodney W. Grimes * instance of unp_discard just calls closef on B. 1624df8bae1dSRodney W. Grimes * 1625df8bae1dSRodney W. Grimes * Well, a similar chain occurs on B, resulting in a sorflush on B, 1626df8bae1dSRodney W. Grimes * which results in another closef on A. Unfortunately, A is already 1627df8bae1dSRodney W. Grimes * being closed, and the descriptor has already been marked with 1628df8bae1dSRodney W. Grimes * SS_NOFDREF, and soclose panics at this point. 1629df8bae1dSRodney W. Grimes * 1630df8bae1dSRodney W. Grimes * Here, we first take an extra reference to each inaccessible 1631df8bae1dSRodney W. Grimes * descriptor. Then, we call sorflush ourself, since we know 1632df8bae1dSRodney W. Grimes * it is a Unix domain socket anyhow. After we destroy all the 1633df8bae1dSRodney W. Grimes * rights carried in messages, we do a last closef to get rid 1634df8bae1dSRodney W. Grimes * of our extra reference. This is the last close, and the 1635df8bae1dSRodney W. Grimes * unp_detach etc will shut down the socket. 1636df8bae1dSRodney W. Grimes * 1637df8bae1dSRodney W. Grimes * 91/09/19, bsy@cs.cmu.edu 1638df8bae1dSRodney W. Grimes */ 163995f004dcSAlfred Perlstein again: 164095f004dcSAlfred Perlstein nfiles_snap = nfiles + nfiles_slack; /* some slack */ 164195f004dcSAlfred Perlstein extra_ref = malloc(nfiles_snap * sizeof(struct file *), M_TEMP, 164295f004dcSAlfred Perlstein M_WAITOK); 1643426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 164495f004dcSAlfred Perlstein if (nfiles_snap < nfiles) { 164595f004dcSAlfred Perlstein sx_sunlock(&filelist_lock); 164695f004dcSAlfred Perlstein free(extra_ref, M_TEMP); 164795f004dcSAlfred Perlstein nfiles_slack += 20; 164895f004dcSAlfred Perlstein goto again; 164995f004dcSAlfred Perlstein } 1650fc3fcacfSRobert Watson for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; 1651fc3fcacfSRobert Watson fp != NULL; fp = nextfp) { 16522e3c8fcbSPoul-Henning Kamp nextfp = LIST_NEXT(fp, f_list); 1653426da3bcSAlfred Perlstein FILE_LOCK(fp); 1654ed5b7817SJulian Elischer /* 1655ed5b7817SJulian Elischer * If it's not open, skip it 1656ed5b7817SJulian Elischer */ 1657426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1658426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1659df8bae1dSRodney W. Grimes continue; 1660426da3bcSAlfred Perlstein } 1661ed5b7817SJulian Elischer /* 1662ed5b7817SJulian Elischer * If all refs are from msgs, and it's not marked accessible 1663ed5b7817SJulian Elischer * then it must be referenced from some unreachable cycle 1664ed5b7817SJulian Elischer * of (shut-down) FDs, so include it in our 1665ed5b7817SJulian Elischer * list of FDs to remove 1666ed5b7817SJulian Elischer */ 1667426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) { 1668df8bae1dSRodney W. Grimes *fpp++ = fp; 1669df8bae1dSRodney W. Grimes nunref++; 1670df8bae1dSRodney W. Grimes fp->f_count++; 1671df8bae1dSRodney W. Grimes } 1672426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1673df8bae1dSRodney W. Grimes } 1674426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1675ed5b7817SJulian Elischer /* 1676ed5b7817SJulian Elischer * for each FD on our hit list, do the following two things 1677ed5b7817SJulian Elischer */ 16781c7c3c6aSMatthew Dillon for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 16791c7c3c6aSMatthew Dillon struct file *tfp = *fpp; 1680426da3bcSAlfred Perlstein FILE_LOCK(tfp); 1681cd72f218SMatthew Dillon if (tfp->f_type == DTYPE_SOCKET && 168248e3128bSMatthew Dillon tfp->f_data != NULL) { 1683426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 168448e3128bSMatthew Dillon sorflush(tfp->f_data); 1685e5aeaa0cSDag-Erling Smørgrav } else { 1686426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 16871c7c3c6aSMatthew Dillon } 1688e5aeaa0cSDag-Erling Smørgrav } 1689df8bae1dSRodney W. Grimes for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) 1690b40ce416SJulian Elischer closef(*fpp, (struct thread *) NULL); 1691210a5a71SAlfred Perlstein free(extra_ref, M_TEMP); 1692df8bae1dSRodney W. Grimes unp_gcing = 0; 1693df8bae1dSRodney W. Grimes } 1694df8bae1dSRodney W. Grimes 169526f9a767SRodney W. Grimes void 1696df8bae1dSRodney W. Grimes unp_dispose(m) 1697df8bae1dSRodney W. Grimes struct mbuf *m; 1698df8bae1dSRodney W. Grimes { 1699996c772fSJohn Dyson 1700df8bae1dSRodney W. Grimes if (m) 1701df8bae1dSRodney W. Grimes unp_scan(m, unp_discard); 1702df8bae1dSRodney W. Grimes } 1703df8bae1dSRodney W. Grimes 17040c1bb4fbSDima Dorfman static int 17056f105b34SJohn Baldwin unp_listen(unp, td) 17060c1bb4fbSDima Dorfman struct unpcb *unp; 17076f105b34SJohn Baldwin struct thread *td; 17080c1bb4fbSDima Dorfman { 17090d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 17100c1bb4fbSDima Dorfman 17110d9ce3a1SRobert Watson /* 17120d9ce3a1SRobert Watson * XXXRW: Why populate the local peer cred with our own credential? 17130d9ce3a1SRobert Watson */ 17146f105b34SJohn Baldwin cru2x(td->td_ucred, &unp->unp_peercred); 17150c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPCCACHED; 17160c1bb4fbSDima Dorfman return (0); 17170c1bb4fbSDima Dorfman } 17180c1bb4fbSDima Dorfman 1719f708ef1bSPoul-Henning Kamp static void 1720df8bae1dSRodney W. Grimes unp_scan(m0, op) 1721df8bae1dSRodney W. Grimes register struct mbuf *m0; 17224d77a549SAlfred Perlstein void (*op)(struct file *); 1723df8bae1dSRodney W. Grimes { 17242bc21ed9SDavid Malone struct mbuf *m; 17252bc21ed9SDavid Malone struct file **rp; 17262bc21ed9SDavid Malone struct cmsghdr *cm; 17272bc21ed9SDavid Malone void *data; 17282bc21ed9SDavid Malone int i; 17292bc21ed9SDavid Malone socklen_t clen, datalen; 1730df8bae1dSRodney W. Grimes int qfds; 1731df8bae1dSRodney W. Grimes 1732fc3fcacfSRobert Watson while (m0 != NULL) { 17332bc21ed9SDavid Malone for (m = m0; m; m = m->m_next) { 173412396bdcSDavid Malone if (m->m_type != MT_CONTROL) 1735df8bae1dSRodney W. Grimes continue; 17362bc21ed9SDavid Malone 17372bc21ed9SDavid Malone cm = mtod(m, struct cmsghdr *); 17382bc21ed9SDavid Malone clen = m->m_len; 17392bc21ed9SDavid Malone 17402bc21ed9SDavid Malone while (cm != NULL) { 17412bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) 17422bc21ed9SDavid Malone break; 17432bc21ed9SDavid Malone 17442bc21ed9SDavid Malone data = CMSG_DATA(cm); 17452bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len 17462bc21ed9SDavid Malone - (caddr_t)data; 17472bc21ed9SDavid Malone 17482bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET && 17492bc21ed9SDavid Malone cm->cmsg_type == SCM_RIGHTS) { 17502bc21ed9SDavid Malone qfds = datalen / sizeof (struct file *); 17512bc21ed9SDavid Malone rp = data; 1752df8bae1dSRodney W. Grimes for (i = 0; i < qfds; i++) 1753df8bae1dSRodney W. Grimes (*op)(*rp++); 17542bc21ed9SDavid Malone } 17552bc21ed9SDavid Malone 17562bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 17572bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 17582bc21ed9SDavid Malone cm = (struct cmsghdr *) 17592bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 17602bc21ed9SDavid Malone } else { 17612bc21ed9SDavid Malone clen = 0; 17622bc21ed9SDavid Malone cm = NULL; 17632bc21ed9SDavid Malone } 17642bc21ed9SDavid Malone } 1765df8bae1dSRodney W. Grimes } 1766df8bae1dSRodney W. Grimes m0 = m0->m_act; 1767df8bae1dSRodney W. Grimes } 1768df8bae1dSRodney W. Grimes } 1769df8bae1dSRodney W. Grimes 1770f708ef1bSPoul-Henning Kamp static void 1771df8bae1dSRodney W. Grimes unp_mark(fp) 1772df8bae1dSRodney W. Grimes struct file *fp; 1773df8bae1dSRodney W. Grimes { 1774426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) 1775df8bae1dSRodney W. Grimes return; 1776df8bae1dSRodney W. Grimes unp_defer++; 1777426da3bcSAlfred Perlstein fp->f_gcflag |= (FMARK|FDEFER); 1778df8bae1dSRodney W. Grimes } 1779df8bae1dSRodney W. Grimes 1780f708ef1bSPoul-Henning Kamp static void 1781df8bae1dSRodney W. Grimes unp_discard(fp) 1782df8bae1dSRodney W. Grimes struct file *fp; 1783df8bae1dSRodney W. Grimes { 1784426da3bcSAlfred Perlstein FILE_LOCK(fp); 1785df8bae1dSRodney W. Grimes fp->f_msgcount--; 1786df8bae1dSRodney W. Grimes unp_rights--; 1787426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1788b40ce416SJulian Elischer (void) closef(fp, (struct thread *)NULL); 1789df8bae1dSRodney W. Grimes } 1790