19454b2d8SWarner Losh /*- 2d664e4faSRobert Watson * Copyright 2004-2005 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) 1114c5bc1caSRobert Watson #define UNP_UNLOCK_ASSERT() mtx_assert(&unp_mtx, MA_NOTOWNED) 1120d9ce3a1SRobert Watson 1134d77a549SAlfred Perlstein static int unp_attach(struct socket *); 1144d77a549SAlfred Perlstein static void unp_detach(struct unpcb *); 1154d77a549SAlfred Perlstein static int unp_bind(struct unpcb *,struct sockaddr *, struct thread *); 11670f52b48SBruce Evans static int unp_connect(struct socket *,struct sockaddr *, struct thread *); 117db48c0d2SRobert Watson static int unp_connect2(struct socket *so, struct socket *so2); 1184d77a549SAlfred Perlstein static void unp_disconnect(struct unpcb *); 1194d77a549SAlfred Perlstein static void unp_shutdown(struct unpcb *); 1204d77a549SAlfred Perlstein static void unp_drop(struct unpcb *, int); 1214d77a549SAlfred Perlstein static void unp_gc(void); 1224d77a549SAlfred Perlstein static void unp_scan(struct mbuf *, void (*)(struct file *)); 1234d77a549SAlfred Perlstein static void unp_mark(struct file *); 1244d77a549SAlfred Perlstein static void unp_discard(struct file *); 1254d77a549SAlfred Perlstein static void unp_freerights(struct file **, int); 1264d77a549SAlfred Perlstein static int unp_internalize(struct mbuf **, struct thread *); 1274d77a549SAlfred Perlstein static int unp_listen(struct unpcb *, struct thread *); 128f708ef1bSPoul-Henning Kamp 129a29f300eSGarrett Wollman static int 130a29f300eSGarrett Wollman uipc_abort(struct socket *so) 131df8bae1dSRodney W. Grimes { 13240f2ac28SRobert Watson struct unpcb *unp; 133df8bae1dSRodney W. Grimes 1340d9ce3a1SRobert Watson UNP_LOCK(); 13540f2ac28SRobert Watson unp = sotounpcb(so); 13640f2ac28SRobert Watson if (unp == NULL) { 13740f2ac28SRobert Watson UNP_UNLOCK(); 13840f2ac28SRobert Watson return (EINVAL); 13940f2ac28SRobert Watson } 140a29f300eSGarrett Wollman unp_drop(unp, ECONNABORTED); 1414c5bc1caSRobert Watson unp_detach(unp); 1424c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 14381158452SRobert Watson ACCEPT_LOCK(); 144395a08c9SRobert Watson SOCK_LOCK(so); 145ddb7d629SIan Dowse sotryfree(so); 146e5aeaa0cSDag-Erling Smørgrav return (0); 147df8bae1dSRodney W. Grimes } 148df8bae1dSRodney W. Grimes 149a29f300eSGarrett Wollman static int 15057bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam) 151a29f300eSGarrett Wollman { 15240f2ac28SRobert Watson struct unpcb *unp; 1530d9ce3a1SRobert Watson const struct sockaddr *sa; 154df8bae1dSRodney W. Grimes 155df8bae1dSRodney W. Grimes /* 156df8bae1dSRodney W. Grimes * Pass back name of connected socket, 157df8bae1dSRodney W. Grimes * if it was bound and we are still connected 158df8bae1dSRodney W. Grimes * (our peer may have closed already!). 159df8bae1dSRodney W. Grimes */ 1600d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 1610d9ce3a1SRobert Watson UNP_LOCK(); 16240f2ac28SRobert Watson unp = sotounpcb(so); 16340f2ac28SRobert Watson if (unp == NULL) { 16440f2ac28SRobert Watson UNP_UNLOCK(); 16540f2ac28SRobert Watson free(*nam, M_SONAME); 16640f2ac28SRobert Watson *nam = NULL; 16740f2ac28SRobert Watson return (EINVAL); 16840f2ac28SRobert Watson } 1690d9ce3a1SRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr != NULL) 1700d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 1710d9ce3a1SRobert Watson else 1720d9ce3a1SRobert Watson sa = &sun_noname; 1730d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 1740d9ce3a1SRobert Watson UNP_UNLOCK(); 175e5aeaa0cSDag-Erling Smørgrav return (0); 176a29f300eSGarrett Wollman } 177df8bae1dSRodney W. Grimes 178a29f300eSGarrett Wollman static int 179b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td) 180a29f300eSGarrett Wollman { 181a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 182df8bae1dSRodney W. Grimes 183fc3fcacfSRobert Watson if (unp != NULL) 184e5aeaa0cSDag-Erling Smørgrav return (EISCONN); 185e5aeaa0cSDag-Erling Smørgrav return (unp_attach(so)); 186a29f300eSGarrett Wollman } 187a29f300eSGarrett Wollman 188a29f300eSGarrett Wollman static int 189b40ce416SJulian Elischer uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 190a29f300eSGarrett Wollman { 19140f2ac28SRobert Watson struct unpcb *unp; 19240f2ac28SRobert Watson int error; 193a29f300eSGarrett Wollman 19440f2ac28SRobert Watson UNP_LOCK(); 19540f2ac28SRobert Watson unp = sotounpcb(so); 19640f2ac28SRobert Watson if (unp == NULL) { 19740f2ac28SRobert Watson UNP_UNLOCK(); 198e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 19940f2ac28SRobert Watson } 20040f2ac28SRobert Watson error = unp_bind(unp, nam, td); 20140f2ac28SRobert Watson UNP_UNLOCK(); 20240f2ac28SRobert Watson return (error); 203a29f300eSGarrett Wollman } 204a29f300eSGarrett Wollman 205a29f300eSGarrett Wollman static int 206b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 207a29f300eSGarrett Wollman { 208b295bdcdSRobert Watson struct unpcb *unp; 2090d9ce3a1SRobert Watson int error; 210a29f300eSGarrett Wollman 211fd179ee9SRobert Watson KASSERT(td == curthread, ("uipc_connect: td != curthread")); 212fd179ee9SRobert Watson 2130d9ce3a1SRobert Watson UNP_LOCK(); 214b295bdcdSRobert Watson unp = sotounpcb(so); 215b295bdcdSRobert Watson if (unp == NULL) { 21640f2ac28SRobert Watson UNP_UNLOCK(); 21740f2ac28SRobert Watson return (EINVAL); 218b295bdcdSRobert Watson } 219fd179ee9SRobert Watson error = unp_connect(so, nam, td); 2200d9ce3a1SRobert Watson UNP_UNLOCK(); 2210d9ce3a1SRobert Watson return (error); 222a29f300eSGarrett Wollman } 223a29f300eSGarrett Wollman 224db48c0d2SRobert Watson int 225a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2) 226a29f300eSGarrett Wollman { 22740f2ac28SRobert Watson struct unpcb *unp; 2280d9ce3a1SRobert Watson int error; 229a29f300eSGarrett Wollman 2300d9ce3a1SRobert Watson UNP_LOCK(); 23140f2ac28SRobert Watson unp = sotounpcb(so1); 23240f2ac28SRobert Watson if (unp == NULL) { 23340f2ac28SRobert Watson UNP_UNLOCK(); 23440f2ac28SRobert Watson return (EINVAL); 23540f2ac28SRobert Watson } 2360d9ce3a1SRobert Watson error = unp_connect2(so1, so2); 2370d9ce3a1SRobert Watson UNP_UNLOCK(); 2380d9ce3a1SRobert Watson return (error); 239a29f300eSGarrett Wollman } 240a29f300eSGarrett Wollman 241a29f300eSGarrett Wollman /* control is EOPNOTSUPP */ 242a29f300eSGarrett Wollman 243a29f300eSGarrett Wollman static int 244a29f300eSGarrett Wollman uipc_detach(struct socket *so) 245a29f300eSGarrett Wollman { 24640f2ac28SRobert Watson struct unpcb *unp; 247a29f300eSGarrett Wollman 2480d9ce3a1SRobert Watson UNP_LOCK(); 24940f2ac28SRobert Watson unp = sotounpcb(so); 25040f2ac28SRobert Watson if (unp == NULL) { 25140f2ac28SRobert Watson UNP_UNLOCK(); 25240f2ac28SRobert Watson return (EINVAL); 25340f2ac28SRobert Watson } 2544c5bc1caSRobert Watson unp_detach(unp); 2554c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 256e5aeaa0cSDag-Erling Smørgrav return (0); 257a29f300eSGarrett Wollman } 258a29f300eSGarrett Wollman 259a29f300eSGarrett Wollman static int 260a29f300eSGarrett Wollman uipc_disconnect(struct socket *so) 261a29f300eSGarrett Wollman { 26240f2ac28SRobert Watson struct unpcb *unp; 263a29f300eSGarrett Wollman 2640d9ce3a1SRobert Watson UNP_LOCK(); 26540f2ac28SRobert Watson unp = sotounpcb(so); 26640f2ac28SRobert Watson if (unp == NULL) { 26740f2ac28SRobert Watson UNP_UNLOCK(); 26840f2ac28SRobert Watson return (EINVAL); 26940f2ac28SRobert Watson } 270a29f300eSGarrett Wollman unp_disconnect(unp); 2710d9ce3a1SRobert Watson UNP_UNLOCK(); 272e5aeaa0cSDag-Erling Smørgrav return (0); 273a29f300eSGarrett Wollman } 274a29f300eSGarrett Wollman 275a29f300eSGarrett Wollman static int 276b40ce416SJulian Elischer uipc_listen(struct socket *so, struct thread *td) 277a29f300eSGarrett Wollman { 27840f2ac28SRobert Watson struct unpcb *unp; 2790d9ce3a1SRobert Watson int error; 280a29f300eSGarrett Wollman 2810d9ce3a1SRobert Watson UNP_LOCK(); 28240f2ac28SRobert Watson unp = sotounpcb(so); 28340f2ac28SRobert Watson if (unp == NULL || unp->unp_vnode == NULL) { 28440f2ac28SRobert Watson UNP_UNLOCK(); 28540f2ac28SRobert Watson return (EINVAL); 28640f2ac28SRobert Watson } 2870d9ce3a1SRobert Watson error = unp_listen(unp, td); 2880d9ce3a1SRobert Watson UNP_UNLOCK(); 2890d9ce3a1SRobert Watson return (error); 290a29f300eSGarrett Wollman } 291a29f300eSGarrett Wollman 292a29f300eSGarrett Wollman static int 29357bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 294a29f300eSGarrett Wollman { 29540f2ac28SRobert Watson struct unpcb *unp; 2960d9ce3a1SRobert Watson const struct sockaddr *sa; 297a29f300eSGarrett Wollman 2980d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 2990d9ce3a1SRobert Watson UNP_LOCK(); 30040f2ac28SRobert Watson unp = sotounpcb(so); 30140f2ac28SRobert Watson if (unp == NULL) { 30240f2ac28SRobert Watson UNP_UNLOCK(); 30340f2ac28SRobert Watson free(*nam, M_SONAME); 30440f2ac28SRobert Watson *nam = NULL; 30540f2ac28SRobert Watson return (EINVAL); 30640f2ac28SRobert Watson } 307fc3fcacfSRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr!= NULL) 3080d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 309bdc5f6a3SHajimu UMEMOTO else { 310bdc5f6a3SHajimu UMEMOTO /* 311bdc5f6a3SHajimu UMEMOTO * XXX: It seems that this test always fails even when 312bdc5f6a3SHajimu UMEMOTO * connection is established. So, this else clause is 313bdc5f6a3SHajimu UMEMOTO * added as workaround to return PF_LOCAL sockaddr. 314bdc5f6a3SHajimu UMEMOTO */ 3150d9ce3a1SRobert Watson sa = &sun_noname; 316bdc5f6a3SHajimu UMEMOTO } 3170d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 3180d9ce3a1SRobert Watson UNP_UNLOCK(); 319e5aeaa0cSDag-Erling Smørgrav return (0); 320a29f300eSGarrett Wollman } 321a29f300eSGarrett Wollman 322a29f300eSGarrett Wollman static int 323a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 324a29f300eSGarrett Wollman { 32540f2ac28SRobert Watson struct unpcb *unp; 326a29f300eSGarrett Wollman struct socket *so2; 3276aef685fSBrian Feldman u_long newhiwat; 328a29f300eSGarrett Wollman 3290d9ce3a1SRobert Watson UNP_LOCK(); 33040f2ac28SRobert Watson unp = sotounpcb(so); 33140f2ac28SRobert Watson if (unp == NULL) { 33240f2ac28SRobert Watson UNP_UNLOCK(); 33340f2ac28SRobert Watson return (EINVAL); 33440f2ac28SRobert Watson } 335df8bae1dSRodney W. Grimes switch (so->so_type) { 336df8bae1dSRodney W. Grimes case SOCK_DGRAM: 337a29f300eSGarrett Wollman panic("uipc_rcvd DGRAM?"); 338df8bae1dSRodney W. Grimes /*NOTREACHED*/ 339df8bae1dSRodney W. Grimes 340df8bae1dSRodney W. Grimes case SOCK_STREAM: 341fc3fcacfSRobert Watson if (unp->unp_conn == NULL) 342df8bae1dSRodney W. Grimes break; 343df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 344c9f69064SRobert Watson SOCKBUF_LOCK(&so2->so_snd); 345c9f69064SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 346df8bae1dSRodney W. Grimes /* 347df8bae1dSRodney W. Grimes * Adjust backpressure on sender 348df8bae1dSRodney W. Grimes * and wakeup any waiting to write. 349df8bae1dSRodney W. Grimes */ 350ff8b0106SBrian Feldman so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt; 351ff8b0106SBrian Feldman unp->unp_mbcnt = so->so_rcv.sb_mbcnt; 3526aef685fSBrian Feldman newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - 3536aef685fSBrian Feldman so->so_rcv.sb_cc; 354f535380cSDon Lewis (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat, 3556aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 356ff8b0106SBrian Feldman unp->unp_cc = so->so_rcv.sb_cc; 357c9f69064SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 3581e4d7da7SRobert Watson sowwakeup_locked(so2); 359df8bae1dSRodney W. Grimes break; 360df8bae1dSRodney W. Grimes 361df8bae1dSRodney W. Grimes default: 362a29f300eSGarrett Wollman panic("uipc_rcvd unknown socktype"); 363df8bae1dSRodney W. Grimes } 3640d9ce3a1SRobert Watson UNP_UNLOCK(); 365e5aeaa0cSDag-Erling Smørgrav return (0); 366a29f300eSGarrett Wollman } 367df8bae1dSRodney W. Grimes 368a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 369a29f300eSGarrett Wollman 370a29f300eSGarrett Wollman static int 37157bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 372b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 373a29f300eSGarrett Wollman { 374a29f300eSGarrett Wollman int error = 0; 37540f2ac28SRobert Watson struct unpcb *unp; 376a29f300eSGarrett Wollman struct socket *so2; 3776aef685fSBrian Feldman u_long newhiwat; 378a29f300eSGarrett Wollman 37940f2ac28SRobert Watson unp = sotounpcb(so); 380fc3fcacfSRobert Watson if (unp == NULL) { 381a29f300eSGarrett Wollman error = EINVAL; 382a29f300eSGarrett Wollman goto release; 383a29f300eSGarrett Wollman } 384a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 385a29f300eSGarrett Wollman error = EOPNOTSUPP; 386a29f300eSGarrett Wollman goto release; 387a29f300eSGarrett Wollman } 388a29f300eSGarrett Wollman 389fc3fcacfSRobert Watson if (control != NULL && (error = unp_internalize(&control, td))) 390a29f300eSGarrett Wollman goto release; 391df8bae1dSRodney W. Grimes 3920d9ce3a1SRobert Watson UNP_LOCK(); 39340f2ac28SRobert Watson unp = sotounpcb(so); 39440f2ac28SRobert Watson if (unp == NULL) { 39540f2ac28SRobert Watson UNP_UNLOCK(); 39640f2ac28SRobert Watson error = EINVAL; 39740f2ac28SRobert Watson goto dispose_release; 39840f2ac28SRobert Watson } 39940f2ac28SRobert Watson 400a29f300eSGarrett Wollman switch (so->so_type) { 401a29f300eSGarrett Wollman case SOCK_DGRAM: 402a29f300eSGarrett Wollman { 403e7dd9a10SRobert Watson const struct sockaddr *from; 404df8bae1dSRodney W. Grimes 405fc3fcacfSRobert Watson if (nam != NULL) { 406fc3fcacfSRobert Watson if (unp->unp_conn != NULL) { 407df8bae1dSRodney W. Grimes error = EISCONN; 408df8bae1dSRodney W. Grimes break; 409df8bae1dSRodney W. Grimes } 410b40ce416SJulian Elischer error = unp_connect(so, nam, td); 411df8bae1dSRodney W. Grimes if (error) 412df8bae1dSRodney W. Grimes break; 413df8bae1dSRodney W. Grimes } else { 414fc3fcacfSRobert Watson if (unp->unp_conn == NULL) { 415df8bae1dSRodney W. Grimes error = ENOTCONN; 416df8bae1dSRodney W. Grimes break; 417df8bae1dSRodney W. Grimes } 418df8bae1dSRodney W. Grimes } 419df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 420fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 42157bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 422df8bae1dSRodney W. Grimes else 423df8bae1dSRodney W. Grimes from = &sun_noname; 424a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 425a34b7046SRobert Watson if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) { 4261e4d7da7SRobert Watson sorwakeup_locked(so2); 427fc3fcacfSRobert Watson m = NULL; 428fc3fcacfSRobert Watson control = NULL; 429e5aeaa0cSDag-Erling Smørgrav } else { 430a34b7046SRobert Watson SOCKBUF_UNLOCK(&so2->so_rcv); 431df8bae1dSRodney W. Grimes error = ENOBUFS; 432e5aeaa0cSDag-Erling Smørgrav } 433fc3fcacfSRobert Watson if (nam != NULL) 434df8bae1dSRodney W. Grimes unp_disconnect(unp); 435df8bae1dSRodney W. Grimes break; 436df8bae1dSRodney W. Grimes } 437df8bae1dSRodney W. Grimes 438df8bae1dSRodney W. Grimes case SOCK_STREAM: 4396b8fda4dSGarrett Wollman /* Connect if not connected yet. */ 4406b8fda4dSGarrett Wollman /* 4416b8fda4dSGarrett Wollman * Note: A better implementation would complain 442402cc72dSDavid Greenman * if not equal to the peer's address. 4436b8fda4dSGarrett Wollman */ 444402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 445fc3fcacfSRobert Watson if (nam != NULL) { 446b40ce416SJulian Elischer error = unp_connect(so, nam, td); 447402cc72dSDavid Greenman if (error) 4486b8fda4dSGarrett Wollman break; /* XXX */ 449402cc72dSDavid Greenman } else { 450402cc72dSDavid Greenman error = ENOTCONN; 451402cc72dSDavid Greenman break; 452402cc72dSDavid Greenman } 453402cc72dSDavid Greenman } 454402cc72dSDavid Greenman 4557abe2ac2SAlan Cox SOCKBUF_LOCK(&so->so_snd); 456c0b99ffaSRobert Watson if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 4577abe2ac2SAlan Cox SOCKBUF_UNLOCK(&so->so_snd); 458df8bae1dSRodney W. Grimes error = EPIPE; 459df8bae1dSRodney W. Grimes break; 460df8bae1dSRodney W. Grimes } 461fc3fcacfSRobert Watson if (unp->unp_conn == NULL) 462a29f300eSGarrett Wollman panic("uipc_send connected but no connection?"); 463df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 464a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 465df8bae1dSRodney W. Grimes /* 466df8bae1dSRodney W. Grimes * Send to paired receive port, and then reduce 467df8bae1dSRodney W. Grimes * send buffer hiwater marks to maintain backpressure. 468df8bae1dSRodney W. Grimes * Wake up readers. 469df8bae1dSRodney W. Grimes */ 470fc3fcacfSRobert Watson if (control != NULL) { 471a34b7046SRobert Watson if (sbappendcontrol_locked(&so2->so_rcv, m, control)) 472fc3fcacfSRobert Watson control = NULL; 473e5aeaa0cSDag-Erling Smørgrav } else { 474a34b7046SRobert Watson sbappend_locked(&so2->so_rcv, m); 475e5aeaa0cSDag-Erling Smørgrav } 476ff8b0106SBrian Feldman so->so_snd.sb_mbmax -= 477ff8b0106SBrian Feldman so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt; 478ff8b0106SBrian Feldman unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt; 4796aef685fSBrian Feldman newhiwat = so->so_snd.sb_hiwat - 4806aef685fSBrian Feldman (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc); 481f535380cSDon Lewis (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat, 4826aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 4837abe2ac2SAlan Cox SOCKBUF_UNLOCK(&so->so_snd); 484ff8b0106SBrian Feldman unp->unp_conn->unp_cc = so2->so_rcv.sb_cc; 4851e4d7da7SRobert Watson sorwakeup_locked(so2); 486fc3fcacfSRobert Watson m = NULL; 487df8bae1dSRodney W. Grimes break; 488df8bae1dSRodney W. Grimes 489df8bae1dSRodney W. Grimes default: 490a29f300eSGarrett Wollman panic("uipc_send unknown socktype"); 491df8bae1dSRodney W. Grimes } 492a29f300eSGarrett Wollman 4936b8fda4dSGarrett Wollman /* 4946b8fda4dSGarrett Wollman * SEND_EOF is equivalent to a SEND followed by 4956b8fda4dSGarrett Wollman * a SHUTDOWN. 4966b8fda4dSGarrett Wollman */ 497a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 4986b8fda4dSGarrett Wollman socantsendmore(so); 4996b8fda4dSGarrett Wollman unp_shutdown(unp); 5006b8fda4dSGarrett Wollman } 5010d9ce3a1SRobert Watson UNP_UNLOCK(); 502df8bae1dSRodney W. Grimes 50340f2ac28SRobert Watson dispose_release: 504fc3fcacfSRobert Watson if (control != NULL && error != 0) 505bd508d39SDon Lewis unp_dispose(control); 506bd508d39SDon Lewis 507a29f300eSGarrett Wollman release: 508fc3fcacfSRobert Watson if (control != NULL) 509a29f300eSGarrett Wollman m_freem(control); 510fc3fcacfSRobert Watson if (m != NULL) 511a29f300eSGarrett Wollman m_freem(m); 512e5aeaa0cSDag-Erling Smørgrav return (error); 513a29f300eSGarrett Wollman } 514df8bae1dSRodney W. Grimes 515a29f300eSGarrett Wollman static int 516a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 517a29f300eSGarrett Wollman { 51840f2ac28SRobert Watson struct unpcb *unp; 519a29f300eSGarrett Wollman struct socket *so2; 520a29f300eSGarrett Wollman 5210d9ce3a1SRobert Watson UNP_LOCK(); 52240f2ac28SRobert Watson unp = sotounpcb(so); 52340f2ac28SRobert Watson if (unp == NULL) { 52440f2ac28SRobert Watson UNP_UNLOCK(); 52540f2ac28SRobert Watson return (EINVAL); 52640f2ac28SRobert Watson } 527a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 528fc3fcacfSRobert Watson if (so->so_type == SOCK_STREAM && unp->unp_conn != NULL) { 529df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 530a29f300eSGarrett Wollman sb->st_blksize += so2->so_rcv.sb_cc; 531df8bae1dSRodney W. Grimes } 532f3732fd1SPoul-Henning Kamp sb->st_dev = NODEV; 533df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 5346f782c46SJeffrey Hsu unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 535a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 5360d9ce3a1SRobert Watson UNP_UNLOCK(); 537df8bae1dSRodney W. Grimes return (0); 538a29f300eSGarrett Wollman } 539df8bae1dSRodney W. Grimes 540a29f300eSGarrett Wollman static int 541a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 542a29f300eSGarrett Wollman { 54340f2ac28SRobert Watson struct unpcb *unp; 544df8bae1dSRodney W. Grimes 5450d9ce3a1SRobert Watson UNP_LOCK(); 54640f2ac28SRobert Watson unp = sotounpcb(so); 54740f2ac28SRobert Watson if (unp == NULL) { 54840f2ac28SRobert Watson UNP_UNLOCK(); 54940f2ac28SRobert Watson return (EINVAL); 55040f2ac28SRobert Watson } 551a29f300eSGarrett Wollman socantsendmore(so); 552a29f300eSGarrett Wollman unp_shutdown(unp); 5530d9ce3a1SRobert Watson UNP_UNLOCK(); 554e5aeaa0cSDag-Erling Smørgrav return (0); 555a29f300eSGarrett Wollman } 556df8bae1dSRodney W. Grimes 557a29f300eSGarrett Wollman static int 55857bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 559a29f300eSGarrett Wollman { 56040f2ac28SRobert Watson struct unpcb *unp; 5610d9ce3a1SRobert Watson const struct sockaddr *sa; 562a29f300eSGarrett Wollman 5630d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 5640d9ce3a1SRobert Watson UNP_LOCK(); 56540f2ac28SRobert Watson unp = sotounpcb(so); 56640f2ac28SRobert Watson if (unp == NULL) { 56740f2ac28SRobert Watson UNP_UNLOCK(); 56840f2ac28SRobert Watson free(*nam, M_SONAME); 56940f2ac28SRobert Watson *nam = NULL; 57040f2ac28SRobert Watson return (EINVAL); 57140f2ac28SRobert Watson } 572fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 5730d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_addr; 57483f3198bSThomas Moestl else 5750d9ce3a1SRobert Watson sa = &sun_noname; 5760d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 5770d9ce3a1SRobert Watson UNP_UNLOCK(); 578e5aeaa0cSDag-Erling Smørgrav return (0); 579df8bae1dSRodney W. Grimes } 580a29f300eSGarrett Wollman 581a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = { 582756d52a1SPoul-Henning Kamp .pru_abort = uipc_abort, 583756d52a1SPoul-Henning Kamp .pru_accept = uipc_accept, 584756d52a1SPoul-Henning Kamp .pru_attach = uipc_attach, 585756d52a1SPoul-Henning Kamp .pru_bind = uipc_bind, 586756d52a1SPoul-Henning Kamp .pru_connect = uipc_connect, 587756d52a1SPoul-Henning Kamp .pru_connect2 = uipc_connect2, 588756d52a1SPoul-Henning Kamp .pru_detach = uipc_detach, 589756d52a1SPoul-Henning Kamp .pru_disconnect = uipc_disconnect, 590756d52a1SPoul-Henning Kamp .pru_listen = uipc_listen, 591756d52a1SPoul-Henning Kamp .pru_peeraddr = uipc_peeraddr, 592756d52a1SPoul-Henning Kamp .pru_rcvd = uipc_rcvd, 593756d52a1SPoul-Henning Kamp .pru_send = uipc_send, 594756d52a1SPoul-Henning Kamp .pru_sense = uipc_sense, 595756d52a1SPoul-Henning Kamp .pru_shutdown = uipc_shutdown, 596756d52a1SPoul-Henning Kamp .pru_sockaddr = uipc_sockaddr, 597756d52a1SPoul-Henning Kamp .pru_sosend = sosend, 598756d52a1SPoul-Henning Kamp .pru_soreceive = soreceive, 599756d52a1SPoul-Henning Kamp .pru_sopoll = sopoll, 600a29f300eSGarrett Wollman }; 601df8bae1dSRodney W. Grimes 6020c1bb4fbSDima Dorfman int 603892af6b9SRobert Watson uipc_ctloutput(struct socket *so, struct sockopt *sopt) 6040c1bb4fbSDima Dorfman { 60540f2ac28SRobert Watson struct unpcb *unp; 6060d9ce3a1SRobert Watson struct xucred xu; 6070c1bb4fbSDima Dorfman int error; 6080c1bb4fbSDima Dorfman 6090c1bb4fbSDima Dorfman switch (sopt->sopt_dir) { 6100c1bb4fbSDima Dorfman case SOPT_GET: 6110c1bb4fbSDima Dorfman switch (sopt->sopt_name) { 6120c1bb4fbSDima Dorfman case LOCAL_PEERCRED: 6130d9ce3a1SRobert Watson error = 0; 6140d9ce3a1SRobert Watson UNP_LOCK(); 61540f2ac28SRobert Watson unp = sotounpcb(so); 61640f2ac28SRobert Watson if (unp == NULL) { 61740f2ac28SRobert Watson UNP_UNLOCK(); 61840f2ac28SRobert Watson error = EINVAL; 61940f2ac28SRobert Watson break; 62040f2ac28SRobert Watson } 6210c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC) 6220d9ce3a1SRobert Watson xu = unp->unp_peercred; 6230c1bb4fbSDima Dorfman else { 6240c1bb4fbSDima Dorfman if (so->so_type == SOCK_STREAM) 6250c1bb4fbSDima Dorfman error = ENOTCONN; 6260c1bb4fbSDima Dorfman else 6270c1bb4fbSDima Dorfman error = EINVAL; 6280c1bb4fbSDima Dorfman } 6290d9ce3a1SRobert Watson UNP_UNLOCK(); 6300d9ce3a1SRobert Watson if (error == 0) 6310d9ce3a1SRobert Watson error = sooptcopyout(sopt, &xu, sizeof(xu)); 6320c1bb4fbSDima Dorfman break; 6330c1bb4fbSDima Dorfman default: 6340c1bb4fbSDima Dorfman error = EOPNOTSUPP; 6350c1bb4fbSDima Dorfman break; 6360c1bb4fbSDima Dorfman } 6370c1bb4fbSDima Dorfman break; 6380c1bb4fbSDima Dorfman case SOPT_SET: 6390c1bb4fbSDima Dorfman default: 6400c1bb4fbSDima Dorfman error = EOPNOTSUPP; 6410c1bb4fbSDima Dorfman break; 6420c1bb4fbSDima Dorfman } 6430c1bb4fbSDima Dorfman return (error); 6440c1bb4fbSDima Dorfman } 6450c1bb4fbSDima Dorfman 646df8bae1dSRodney W. Grimes /* 647df8bae1dSRodney W. Grimes * Both send and receive buffers are allocated PIPSIZ bytes of buffering 648df8bae1dSRodney W. Grimes * for stream sockets, although the total for sender and receiver is 649df8bae1dSRodney W. Grimes * actually only PIPSIZ. 650df8bae1dSRodney W. Grimes * Datagram sockets really use the sendspace as the maximum datagram size, 651df8bae1dSRodney W. Grimes * and don't really want to reserve the sendspace. Their recvspace should 652df8bae1dSRodney W. Grimes * be large enough for at least one max-size datagram plus address. 653df8bae1dSRodney W. Grimes */ 6545dce41c5SJohn Dyson #ifndef PIPSIZ 6555dce41c5SJohn Dyson #define PIPSIZ 8192 6565dce41c5SJohn Dyson #endif 657f708ef1bSPoul-Henning Kamp static u_long unpst_sendspace = PIPSIZ; 658f708ef1bSPoul-Henning Kamp static u_long unpst_recvspace = PIPSIZ; 659f708ef1bSPoul-Henning Kamp static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 660f708ef1bSPoul-Henning Kamp static u_long unpdg_recvspace = 4*1024; 661df8bae1dSRodney W. Grimes 662f708ef1bSPoul-Henning Kamp static int unp_rights; /* file descriptors in flight */ 663df8bae1dSRodney W. Grimes 664ce02431fSDoug Rabson SYSCTL_DECL(_net_local_stream); 665639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 666639acc13SGarrett Wollman &unpst_sendspace, 0, ""); 667639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 668639acc13SGarrett Wollman &unpst_recvspace, 0, ""); 669ce02431fSDoug Rabson SYSCTL_DECL(_net_local_dgram); 670639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 671639acc13SGarrett Wollman &unpdg_sendspace, 0, ""); 672639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 673639acc13SGarrett Wollman &unpdg_recvspace, 0, ""); 674ce02431fSDoug Rabson SYSCTL_DECL(_net_local); 675639acc13SGarrett Wollman SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); 676639acc13SGarrett Wollman 677f708ef1bSPoul-Henning Kamp static int 678892af6b9SRobert Watson unp_attach(struct socket *so) 679df8bae1dSRodney W. Grimes { 680892af6b9SRobert Watson struct unpcb *unp; 681df8bae1dSRodney W. Grimes int error; 682df8bae1dSRodney W. Grimes 683df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 684df8bae1dSRodney W. Grimes switch (so->so_type) { 685df8bae1dSRodney W. Grimes 686df8bae1dSRodney W. Grimes case SOCK_STREAM: 687df8bae1dSRodney W. Grimes error = soreserve(so, unpst_sendspace, unpst_recvspace); 688df8bae1dSRodney W. Grimes break; 689df8bae1dSRodney W. Grimes 690df8bae1dSRodney W. Grimes case SOCK_DGRAM: 691df8bae1dSRodney W. Grimes error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 692df8bae1dSRodney W. Grimes break; 693df8bae1dSRodney W. Grimes 694df8bae1dSRodney W. Grimes default: 695df8bae1dSRodney W. Grimes panic("unp_attach"); 696df8bae1dSRodney W. Grimes } 697df8bae1dSRodney W. Grimes if (error) 698df8bae1dSRodney W. Grimes return (error); 699df8bae1dSRodney W. Grimes } 700d664e4faSRobert Watson unp = uma_zalloc(unp_zone, M_WAITOK | M_ZERO); 70157bf258eSGarrett Wollman if (unp == NULL) 702df8bae1dSRodney W. Grimes return (ENOBUFS); 70398271db4SGarrett Wollman LIST_INIT(&unp->unp_refs); 704df8bae1dSRodney W. Grimes unp->unp_socket = so; 7057301cf23SRobert Watson so->so_pcb = unp; 7060d9ce3a1SRobert Watson 7070d9ce3a1SRobert Watson UNP_LOCK(); 7080d9ce3a1SRobert Watson unp->unp_gencnt = ++unp_gencnt; 7090d9ce3a1SRobert Watson unp_count++; 71098271db4SGarrett Wollman LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead 71198271db4SGarrett Wollman : &unp_shead, unp, unp_link); 7120d9ce3a1SRobert Watson UNP_UNLOCK(); 7130d9ce3a1SRobert Watson 714df8bae1dSRodney W. Grimes return (0); 715df8bae1dSRodney W. Grimes } 716df8bae1dSRodney W. Grimes 717f708ef1bSPoul-Henning Kamp static void 718892af6b9SRobert Watson unp_detach(struct unpcb *unp) 719df8bae1dSRodney W. Grimes { 7200d9ce3a1SRobert Watson struct vnode *vp; 7210d9ce3a1SRobert Watson 7220d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 7230d9ce3a1SRobert Watson 72498271db4SGarrett Wollman LIST_REMOVE(unp, unp_link); 72598271db4SGarrett Wollman unp->unp_gencnt = ++unp_gencnt; 72698271db4SGarrett Wollman --unp_count; 7270d9ce3a1SRobert Watson if ((vp = unp->unp_vnode) != NULL) { 7280d9ce3a1SRobert Watson /* 7290d9ce3a1SRobert Watson * XXXRW: should v_socket be frobbed only while holding 7300d9ce3a1SRobert Watson * Giant? 7310d9ce3a1SRobert Watson */ 732fc3fcacfSRobert Watson unp->unp_vnode->v_socket = NULL; 733fc3fcacfSRobert Watson unp->unp_vnode = NULL; 734df8bae1dSRodney W. Grimes } 735fc3fcacfSRobert Watson if (unp->unp_conn != NULL) 736df8bae1dSRodney W. Grimes unp_disconnect(unp); 7370d9ce3a1SRobert Watson while (!LIST_EMPTY(&unp->unp_refs)) { 7380d9ce3a1SRobert Watson struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 7390d9ce3a1SRobert Watson unp_drop(ref, ECONNRESET); 7400d9ce3a1SRobert Watson } 741df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 742fc3fcacfSRobert Watson unp->unp_socket->so_pcb = NULL; 743df8bae1dSRodney W. Grimes if (unp_rights) { 744df8bae1dSRodney W. Grimes /* 745df8bae1dSRodney W. Grimes * Normally the receive buffer is flushed later, 746df8bae1dSRodney W. Grimes * in sofree, but if our receive buffer holds references 747df8bae1dSRodney W. Grimes * to descriptors that are now garbage, we will dispose 748df8bae1dSRodney W. Grimes * of those descriptor references after the garbage collector 749df8bae1dSRodney W. Grimes * gets them (resulting in a "panic: closef: count < 0"). 750df8bae1dSRodney W. Grimes */ 751df8bae1dSRodney W. Grimes sorflush(unp->unp_socket); 752161a0c7cSRobert Watson unp_gc(); /* Will unlock UNP. */ 753161a0c7cSRobert Watson } else 754a5993a97SRobert Watson UNP_UNLOCK(); 755161a0c7cSRobert Watson UNP_UNLOCK_ASSERT(); 756fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 75757bf258eSGarrett Wollman FREE(unp->unp_addr, M_SONAME); 7589e9d298aSJeff Roberson uma_zfree(unp_zone, unp); 7590d9ce3a1SRobert Watson if (vp) { 7600d9ce3a1SRobert Watson mtx_lock(&Giant); 7610d9ce3a1SRobert Watson vrele(vp); 7620d9ce3a1SRobert Watson mtx_unlock(&Giant); 7630d9ce3a1SRobert Watson } 764df8bae1dSRodney W. Grimes } 765df8bae1dSRodney W. Grimes 766f708ef1bSPoul-Henning Kamp static int 767892af6b9SRobert Watson unp_bind(struct unpcb *unp, struct sockaddr *nam, struct thread *td) 768df8bae1dSRodney W. Grimes { 76957bf258eSGarrett Wollman struct sockaddr_un *soun = (struct sockaddr_un *)nam; 770f2a2857bSKirk McKusick struct vnode *vp; 771f2a2857bSKirk McKusick struct mount *mp; 772df8bae1dSRodney W. Grimes struct vattr vattr; 77357bf258eSGarrett Wollman int error, namelen; 774df8bae1dSRodney W. Grimes struct nameidata nd; 7758f364875SJulian Elischer char *buf; 776df8bae1dSRodney W. Grimes 77740f2ac28SRobert Watson UNP_LOCK_ASSERT(); 77840f2ac28SRobert Watson 7790d9ce3a1SRobert Watson /* 7800d9ce3a1SRobert Watson * XXXRW: This test-and-set of unp_vnode is non-atomic; the 7810d9ce3a1SRobert Watson * unlocked read here is fine, but the value of unp_vnode needs 7820d9ce3a1SRobert Watson * to be tested again after we do all the lookups to see if the 7830d9ce3a1SRobert Watson * pcb is still unbound? 7840d9ce3a1SRobert Watson */ 785df8bae1dSRodney W. Grimes if (unp->unp_vnode != NULL) 786df8bae1dSRodney W. Grimes return (EINVAL); 78755c85568SRobert Drehmel 78857bf258eSGarrett Wollman namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 78957bf258eSGarrett Wollman if (namelen <= 0) 790e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 79155c85568SRobert Drehmel 79240f2ac28SRobert Watson UNP_UNLOCK(); 79340f2ac28SRobert Watson 794a163d034SWarner Losh buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 79555c85568SRobert Drehmel strlcpy(buf, soun->sun_path, namelen + 1); 79655c85568SRobert Drehmel 7970d9ce3a1SRobert Watson mtx_lock(&Giant); 798f2a2857bSKirk McKusick restart: 7990d9ce3a1SRobert Watson mtx_assert(&Giant, MA_OWNED); 800b65f6f6bSRobert Watson NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME, UIO_SYSSPACE, 801b40ce416SJulian Elischer buf, td); 802df8bae1dSRodney W. Grimes /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 803797f2d22SPoul-Henning Kamp error = namei(&nd); 8040d9ce3a1SRobert Watson if (error) 8050d9ce3a1SRobert Watson goto done; 806df8bae1dSRodney W. Grimes vp = nd.ni_vp; 807f2a2857bSKirk McKusick if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 808762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 809df8bae1dSRodney W. Grimes if (nd.ni_dvp == vp) 810df8bae1dSRodney W. Grimes vrele(nd.ni_dvp); 811df8bae1dSRodney W. Grimes else 812df8bae1dSRodney W. Grimes vput(nd.ni_dvp); 813f2a2857bSKirk McKusick if (vp != NULL) { 814df8bae1dSRodney W. Grimes vrele(vp); 8150d9ce3a1SRobert Watson error = EADDRINUSE; 8160d9ce3a1SRobert Watson goto done; 817df8bae1dSRodney W. Grimes } 8188f364875SJulian Elischer error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 8190d9ce3a1SRobert Watson if (error) 8200d9ce3a1SRobert Watson goto done; 821f2a2857bSKirk McKusick goto restart; 822f2a2857bSKirk McKusick } 823df8bae1dSRodney W. Grimes VATTR_NULL(&vattr); 824df8bae1dSRodney W. Grimes vattr.va_type = VSOCK; 825b40ce416SJulian Elischer vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 8266ea48a90SRobert Watson #ifdef MAC 8276ea48a90SRobert Watson error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 8286ea48a90SRobert Watson &vattr); 8296151efaaSRobert Watson #endif 8306ea48a90SRobert Watson if (error == 0) { 831a854ed98SJohn Baldwin VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE); 8327be2d300SMike Smith error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 8336ea48a90SRobert Watson } 834762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 8357be2d300SMike Smith vput(nd.ni_dvp); 8360d9ce3a1SRobert Watson if (error) 8370d9ce3a1SRobert Watson goto done; 838df8bae1dSRodney W. Grimes vp = nd.ni_vp; 8390d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_bind"); 8400d9ce3a1SRobert Watson soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 8410d9ce3a1SRobert Watson UNP_LOCK(); 842df8bae1dSRodney W. Grimes vp->v_socket = unp->unp_socket; 843df8bae1dSRodney W. Grimes unp->unp_vnode = vp; 8440d9ce3a1SRobert Watson unp->unp_addr = soun; 8450d9ce3a1SRobert Watson UNP_UNLOCK(); 846b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 847f2a2857bSKirk McKusick vn_finished_write(mp); 8480d9ce3a1SRobert Watson done: 8490d9ce3a1SRobert Watson mtx_unlock(&Giant); 8508f364875SJulian Elischer free(buf, M_TEMP); 85140f2ac28SRobert Watson UNP_LOCK(); 8520d9ce3a1SRobert Watson return (error); 853df8bae1dSRodney W. Grimes } 854df8bae1dSRodney W. Grimes 855f708ef1bSPoul-Henning Kamp static int 856892af6b9SRobert Watson unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 857df8bae1dSRodney W. Grimes { 858892af6b9SRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam; 859892af6b9SRobert Watson struct vnode *vp; 860892af6b9SRobert Watson struct socket *so2, *so3; 861b295bdcdSRobert Watson struct unpcb *unp, *unp2, *unp3; 86257bf258eSGarrett Wollman int error, len; 863df8bae1dSRodney W. Grimes struct nameidata nd; 86457bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 8650d9ce3a1SRobert Watson struct sockaddr *sa; 8660d9ce3a1SRobert Watson 8670d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 868b295bdcdSRobert Watson unp = sotounpcb(so); 869df8bae1dSRodney W. Grimes 87057bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 87157bf258eSGarrett Wollman if (len <= 0) 872e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 87355c85568SRobert Drehmel strlcpy(buf, soun->sun_path, len + 1); 8740d9ce3a1SRobert Watson UNP_UNLOCK(); 8750d9ce3a1SRobert Watson sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 8760d9ce3a1SRobert Watson mtx_lock(&Giant); 877b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td); 878797f2d22SPoul-Henning Kamp error = namei(&nd); 879797f2d22SPoul-Henning Kamp if (error) 8800d9ce3a1SRobert Watson vp = NULL; 8810d9ce3a1SRobert Watson else 882df8bae1dSRodney W. Grimes vp = nd.ni_vp; 8830d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_connect"); 884762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 8850d9ce3a1SRobert Watson if (error) 8860d9ce3a1SRobert Watson goto bad; 8870d9ce3a1SRobert Watson 888df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 889df8bae1dSRodney W. Grimes error = ENOTSOCK; 890df8bae1dSRodney W. Grimes goto bad; 891df8bae1dSRodney W. Grimes } 892a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 893797f2d22SPoul-Henning Kamp if (error) 894df8bae1dSRodney W. Grimes goto bad; 8952260c03dSRobert Watson mtx_unlock(&Giant); 8962260c03dSRobert Watson UNP_LOCK(); 897b295bdcdSRobert Watson unp = sotounpcb(so); 898b295bdcdSRobert Watson if (unp == NULL) { 899b295bdcdSRobert Watson error = EINVAL; 900b295bdcdSRobert Watson goto bad2; 901b295bdcdSRobert Watson } 902df8bae1dSRodney W. Grimes so2 = vp->v_socket; 903fc3fcacfSRobert Watson if (so2 == NULL) { 904df8bae1dSRodney W. Grimes error = ECONNREFUSED; 9052260c03dSRobert Watson goto bad2; 906df8bae1dSRodney W. Grimes } 907df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 908df8bae1dSRodney W. Grimes error = EPROTOTYPE; 9092260c03dSRobert Watson goto bad2; 910df8bae1dSRodney W. Grimes } 911df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 9120d9ce3a1SRobert Watson if (so2->so_options & SO_ACCEPTCONN) { 9130d9ce3a1SRobert Watson /* 9140d9ce3a1SRobert Watson * NB: drop locks here so unp_attach is entered 9150d9ce3a1SRobert Watson * w/o locks; this avoids a recursive lock 9160d9ce3a1SRobert Watson * of the head and holding sleep locks across 9170d9ce3a1SRobert Watson * a (potentially) blocking malloc. 9180d9ce3a1SRobert Watson */ 9190d9ce3a1SRobert Watson UNP_UNLOCK(); 9200d9ce3a1SRobert Watson so3 = sonewconn(so2, 0); 9210d9ce3a1SRobert Watson UNP_LOCK(); 9220d9ce3a1SRobert Watson } else 9230d9ce3a1SRobert Watson so3 = NULL; 9240d9ce3a1SRobert Watson if (so3 == NULL) { 925df8bae1dSRodney W. Grimes error = ECONNREFUSED; 9260d9ce3a1SRobert Watson goto bad2; 927df8bae1dSRodney W. Grimes } 9280c1bb4fbSDima Dorfman unp = sotounpcb(so); 929df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 930df8bae1dSRodney W. Grimes unp3 = sotounpcb(so3); 9310d9ce3a1SRobert Watson if (unp2->unp_addr != NULL) { 9320d9ce3a1SRobert Watson bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 9330d9ce3a1SRobert Watson unp3->unp_addr = (struct sockaddr_un *) sa; 9340d9ce3a1SRobert Watson sa = NULL; 9350d9ce3a1SRobert Watson } 9360c1bb4fbSDima Dorfman /* 9370c1bb4fbSDima Dorfman * unp_peercred management: 9380c1bb4fbSDima Dorfman * 9390c1bb4fbSDima Dorfman * The connecter's (client's) credentials are copied 9400c1bb4fbSDima Dorfman * from its process structure at the time of connect() 9410c1bb4fbSDima Dorfman * (which is now). 9420c1bb4fbSDima Dorfman */ 943a854ed98SJohn Baldwin cru2x(td->td_ucred, &unp3->unp_peercred); 9440c1bb4fbSDima Dorfman unp3->unp_flags |= UNP_HAVEPC; 9450c1bb4fbSDima Dorfman /* 9460c1bb4fbSDima Dorfman * The receiver's (server's) credentials are copied 9470c1bb4fbSDima Dorfman * from the unp_peercred member of socket on which the 9480c1bb4fbSDima Dorfman * former called listen(); unp_listen() cached that 9490c1bb4fbSDima Dorfman * process's credentials at that time so we can use 9500c1bb4fbSDima Dorfman * them now. 9510c1bb4fbSDima Dorfman */ 9520c1bb4fbSDima Dorfman KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 9530c1bb4fbSDima Dorfman ("unp_connect: listener without cached peercred")); 9540c1bb4fbSDima Dorfman memcpy(&unp->unp_peercred, &unp2->unp_peercred, 9550c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 9560c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPC; 957335654d7SRobert Watson #ifdef MAC 958310e7cebSRobert Watson SOCK_LOCK(so); 959335654d7SRobert Watson mac_set_socket_peer_from_socket(so, so3); 960335654d7SRobert Watson mac_set_socket_peer_from_socket(so3, so); 961310e7cebSRobert Watson SOCK_UNLOCK(so); 962335654d7SRobert Watson #endif 9630c1bb4fbSDima Dorfman 964df8bae1dSRodney W. Grimes so2 = so3; 965df8bae1dSRodney W. Grimes } 966df8bae1dSRodney W. Grimes error = unp_connect2(so, so2); 9670d9ce3a1SRobert Watson bad2: 9680d9ce3a1SRobert Watson UNP_UNLOCK(); 9690d9ce3a1SRobert Watson mtx_lock(&Giant); 970df8bae1dSRodney W. Grimes bad: 9710d9ce3a1SRobert Watson mtx_assert(&Giant, MA_OWNED); 9720d9ce3a1SRobert Watson if (vp != NULL) 973df8bae1dSRodney W. Grimes vput(vp); 9740d9ce3a1SRobert Watson mtx_unlock(&Giant); 9750d9ce3a1SRobert Watson free(sa, M_SONAME); 9760d9ce3a1SRobert Watson UNP_LOCK(); 977df8bae1dSRodney W. Grimes return (error); 978df8bae1dSRodney W. Grimes } 979df8bae1dSRodney W. Grimes 980db48c0d2SRobert Watson static int 981892af6b9SRobert Watson unp_connect2(struct socket *so, struct socket *so2) 982df8bae1dSRodney W. Grimes { 983892af6b9SRobert Watson struct unpcb *unp = sotounpcb(so); 984892af6b9SRobert Watson struct unpcb *unp2; 985df8bae1dSRodney W. Grimes 9860d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 9870d9ce3a1SRobert Watson 988df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 989df8bae1dSRodney W. Grimes return (EPROTOTYPE); 990df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 991df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 992df8bae1dSRodney W. Grimes switch (so->so_type) { 993df8bae1dSRodney W. Grimes 994df8bae1dSRodney W. Grimes case SOCK_DGRAM: 99598271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 996df8bae1dSRodney W. Grimes soisconnected(so); 997df8bae1dSRodney W. Grimes break; 998df8bae1dSRodney W. Grimes 999df8bae1dSRodney W. Grimes case SOCK_STREAM: 1000df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 1001df8bae1dSRodney W. Grimes soisconnected(so); 1002df8bae1dSRodney W. Grimes soisconnected(so2); 1003df8bae1dSRodney W. Grimes break; 1004df8bae1dSRodney W. Grimes 1005df8bae1dSRodney W. Grimes default: 1006df8bae1dSRodney W. Grimes panic("unp_connect2"); 1007df8bae1dSRodney W. Grimes } 1008df8bae1dSRodney W. Grimes return (0); 1009df8bae1dSRodney W. Grimes } 1010df8bae1dSRodney W. Grimes 1011f708ef1bSPoul-Henning Kamp static void 1012892af6b9SRobert Watson unp_disconnect(struct unpcb *unp) 1013df8bae1dSRodney W. Grimes { 1014892af6b9SRobert Watson struct unpcb *unp2 = unp->unp_conn; 10151b2e3b4bSRobert Watson struct socket *so; 1016df8bae1dSRodney W. Grimes 10170d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 10180d9ce3a1SRobert Watson 1019fc3fcacfSRobert Watson if (unp2 == NULL) 1020df8bae1dSRodney W. Grimes return; 1021fc3fcacfSRobert Watson unp->unp_conn = NULL; 1022df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 1023df8bae1dSRodney W. Grimes 1024df8bae1dSRodney W. Grimes case SOCK_DGRAM: 102598271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 10261b2e3b4bSRobert Watson so = unp->unp_socket; 10271b2e3b4bSRobert Watson SOCK_LOCK(so); 10281b2e3b4bSRobert Watson so->so_state &= ~SS_ISCONNECTED; 10291b2e3b4bSRobert Watson SOCK_UNLOCK(so); 1030df8bae1dSRodney W. Grimes break; 1031df8bae1dSRodney W. Grimes 1032df8bae1dSRodney W. Grimes case SOCK_STREAM: 1033df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 1034fc3fcacfSRobert Watson unp2->unp_conn = NULL; 1035df8bae1dSRodney W. Grimes soisdisconnected(unp2->unp_socket); 1036df8bae1dSRodney W. Grimes break; 1037df8bae1dSRodney W. Grimes } 1038df8bae1dSRodney W. Grimes } 1039df8bae1dSRodney W. Grimes 1040df8bae1dSRodney W. Grimes #ifdef notdef 104126f9a767SRodney W. Grimes void 1042892af6b9SRobert Watson unp_abort(struct unpcb *unp) 1043df8bae1dSRodney W. Grimes { 1044df8bae1dSRodney W. Grimes 1045df8bae1dSRodney W. Grimes unp_detach(unp); 10464c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 1047df8bae1dSRodney W. Grimes } 1048df8bae1dSRodney W. Grimes #endif 1049df8bae1dSRodney W. Grimes 10500d9ce3a1SRobert Watson /* 10510d9ce3a1SRobert Watson * unp_pcblist() assumes that UNIX domain socket memory is never reclaimed 10520d9ce3a1SRobert Watson * by the zone (UMA_ZONE_NOFREE), and as such potentially stale pointers 10530d9ce3a1SRobert Watson * are safe to reference. It first scans the list of struct unpcb's to 10540d9ce3a1SRobert Watson * generate a pointer list, then it rescans its list one entry at a time to 10550d9ce3a1SRobert Watson * externalize and copyout. It checks the generation number to see if a 10560d9ce3a1SRobert Watson * struct unpcb has been reused, and will skip it if so. 10570d9ce3a1SRobert Watson */ 105898271db4SGarrett Wollman static int 105982d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 106098271db4SGarrett Wollman { 1061f5ef029eSPoul-Henning Kamp int error, i, n; 106298271db4SGarrett Wollman struct unpcb *unp, **unp_list; 106398271db4SGarrett Wollman unp_gen_t gencnt; 10648f364875SJulian Elischer struct xunpgen *xug; 106598271db4SGarrett Wollman struct unp_head *head; 10668f364875SJulian Elischer struct xunpcb *xu; 106798271db4SGarrett Wollman 1068a23d65bfSBruce Evans head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 106998271db4SGarrett Wollman 107098271db4SGarrett Wollman /* 107198271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 107298271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 107398271db4SGarrett Wollman */ 1074fc3fcacfSRobert Watson if (req->oldptr == NULL) { 107598271db4SGarrett Wollman n = unp_count; 10768f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug) 107798271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 1078e5aeaa0cSDag-Erling Smørgrav return (0); 107998271db4SGarrett Wollman } 108098271db4SGarrett Wollman 1081fc3fcacfSRobert Watson if (req->newptr != NULL) 1082e5aeaa0cSDag-Erling Smørgrav return (EPERM); 108398271db4SGarrett Wollman 108498271db4SGarrett Wollman /* 108598271db4SGarrett Wollman * OK, now we're committed to doing something. 108698271db4SGarrett Wollman */ 1087a163d034SWarner Losh xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 10880d9ce3a1SRobert Watson UNP_LOCK(); 108998271db4SGarrett Wollman gencnt = unp_gencnt; 109098271db4SGarrett Wollman n = unp_count; 10910d9ce3a1SRobert Watson UNP_UNLOCK(); 109298271db4SGarrett Wollman 10938f364875SJulian Elischer xug->xug_len = sizeof *xug; 10948f364875SJulian Elischer xug->xug_count = n; 10958f364875SJulian Elischer xug->xug_gen = gencnt; 10968f364875SJulian Elischer xug->xug_sogen = so_gencnt; 10978f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 10988f364875SJulian Elischer if (error) { 10998f364875SJulian Elischer free(xug, M_TEMP); 1100e5aeaa0cSDag-Erling Smørgrav return (error); 11018f364875SJulian Elischer } 110298271db4SGarrett Wollman 1103a163d034SWarner Losh unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 110498271db4SGarrett Wollman 11050d9ce3a1SRobert Watson UNP_LOCK(); 11062e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 11072e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 11088a7d8cc6SRobert Watson if (unp->unp_gencnt <= gencnt) { 1109a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred, 11108a7d8cc6SRobert Watson unp->unp_socket->so_cred)) 11114787fd37SPaul Saab continue; 111298271db4SGarrett Wollman unp_list[i++] = unp; 111398271db4SGarrett Wollman } 11144787fd37SPaul Saab } 11150d9ce3a1SRobert Watson UNP_UNLOCK(); 111698271db4SGarrett Wollman n = i; /* in case we lost some during malloc */ 111798271db4SGarrett Wollman 111898271db4SGarrett Wollman error = 0; 1119a163d034SWarner Losh xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK); 112098271db4SGarrett Wollman for (i = 0; i < n; i++) { 112198271db4SGarrett Wollman unp = unp_list[i]; 112298271db4SGarrett Wollman if (unp->unp_gencnt <= gencnt) { 11238f364875SJulian Elischer xu->xu_len = sizeof *xu; 11248f364875SJulian Elischer xu->xu_unpp = unp; 112598271db4SGarrett Wollman /* 112698271db4SGarrett Wollman * XXX - need more locking here to protect against 112798271db4SGarrett Wollman * connect/disconnect races for SMP. 112898271db4SGarrett Wollman */ 1129fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 11308f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr, 113198271db4SGarrett Wollman unp->unp_addr->sun_len); 1132fc3fcacfSRobert Watson if (unp->unp_conn != NULL && 1133fc3fcacfSRobert Watson unp->unp_conn->unp_addr != NULL) 113498271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 11358f364875SJulian Elischer &xu->xu_caddr, 113698271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 11378f364875SJulian Elischer bcopy(unp, &xu->xu_unp, sizeof *unp); 11388f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket); 11398f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu); 114098271db4SGarrett Wollman } 114198271db4SGarrett Wollman } 11428f364875SJulian Elischer free(xu, M_TEMP); 114398271db4SGarrett Wollman if (!error) { 114498271db4SGarrett Wollman /* 114598271db4SGarrett Wollman * Give the user an updated idea of our state. 114698271db4SGarrett Wollman * If the generation differs from what we told 114798271db4SGarrett Wollman * her before, she knows that something happened 114898271db4SGarrett Wollman * while we were processing this request, and it 114998271db4SGarrett Wollman * might be necessary to retry. 115098271db4SGarrett Wollman */ 11518f364875SJulian Elischer xug->xug_gen = unp_gencnt; 11528f364875SJulian Elischer xug->xug_sogen = so_gencnt; 11538f364875SJulian Elischer xug->xug_count = unp_count; 11548f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 115598271db4SGarrett Wollman } 115698271db4SGarrett Wollman free(unp_list, M_TEMP); 11578f364875SJulian Elischer free(xug, M_TEMP); 1158e5aeaa0cSDag-Erling Smørgrav return (error); 115998271db4SGarrett Wollman } 116098271db4SGarrett Wollman 116198271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 116298271db4SGarrett Wollman (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 116398271db4SGarrett Wollman "List of active local datagram sockets"); 116498271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 116598271db4SGarrett Wollman (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 116698271db4SGarrett Wollman "List of active local stream sockets"); 116798271db4SGarrett Wollman 1168f708ef1bSPoul-Henning Kamp static void 1169892af6b9SRobert Watson unp_shutdown(struct unpcb *unp) 1170df8bae1dSRodney W. Grimes { 1171df8bae1dSRodney W. Grimes struct socket *so; 1172df8bae1dSRodney W. Grimes 11730d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 11740d9ce3a1SRobert Watson 1175df8bae1dSRodney W. Grimes if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 1176df8bae1dSRodney W. Grimes (so = unp->unp_conn->unp_socket)) 1177df8bae1dSRodney W. Grimes socantrcvmore(so); 1178df8bae1dSRodney W. Grimes } 1179df8bae1dSRodney W. Grimes 1180f708ef1bSPoul-Henning Kamp static void 1181892af6b9SRobert Watson unp_drop(struct unpcb *unp, 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 1193892af6b9SRobert Watson unp_drain(void) 1194df8bae1dSRodney W. Grimes { 1195df8bae1dSRodney W. Grimes 1196df8bae1dSRodney W. Grimes } 1197df8bae1dSRodney W. Grimes #endif 1198df8bae1dSRodney W. Grimes 11992bc21ed9SDavid Malone static void 1200892af6b9SRobert Watson unp_freerights(struct file **rp, int fdcount) 1201df8bae1dSRodney W. Grimes { 12022bc21ed9SDavid Malone int i; 12032bc21ed9SDavid Malone struct file *fp; 1204df8bae1dSRodney W. Grimes 12052bc21ed9SDavid Malone for (i = 0; i < fdcount; i++) { 1206df8bae1dSRodney W. Grimes fp = *rp; 12078692c025SYoshinobu Inoue /* 12082bc21ed9SDavid Malone * zero the pointer before calling 12092bc21ed9SDavid Malone * unp_discard since it may end up 12102bc21ed9SDavid Malone * in unp_gc().. 12118692c025SYoshinobu Inoue */ 1212df8bae1dSRodney W. Grimes *rp++ = 0; 12138692c025SYoshinobu Inoue unp_discard(fp); 1214df8bae1dSRodney W. Grimes } 12152bc21ed9SDavid Malone } 12162bc21ed9SDavid Malone 12172bc21ed9SDavid Malone int 1218892af6b9SRobert Watson unp_externalize(struct mbuf *control, struct mbuf **controlp) 12192bc21ed9SDavid Malone { 12202bc21ed9SDavid Malone struct thread *td = curthread; /* XXX */ 12212bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 12222bc21ed9SDavid Malone int i; 12232bc21ed9SDavid Malone int *fdp; 12242bc21ed9SDavid Malone struct file **rp; 12252bc21ed9SDavid Malone struct file *fp; 12262bc21ed9SDavid Malone void *data; 12272bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 12282bc21ed9SDavid Malone int error, newfds; 12292bc21ed9SDavid Malone int f; 12302bc21ed9SDavid Malone u_int newlen; 12312bc21ed9SDavid Malone 12324c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 12334c5bc1caSRobert Watson 12342bc21ed9SDavid Malone error = 0; 12352bc21ed9SDavid Malone if (controlp != NULL) /* controlp == NULL => free control messages */ 12362bc21ed9SDavid Malone *controlp = NULL; 12372bc21ed9SDavid Malone 12382bc21ed9SDavid Malone while (cm != NULL) { 12392bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 12402bc21ed9SDavid Malone error = EINVAL; 12412bc21ed9SDavid Malone break; 12422bc21ed9SDavid Malone } 12432bc21ed9SDavid Malone 12442bc21ed9SDavid Malone data = CMSG_DATA(cm); 12452bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 12462bc21ed9SDavid Malone 12472bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET 12482bc21ed9SDavid Malone && cm->cmsg_type == SCM_RIGHTS) { 12492bc21ed9SDavid Malone newfds = datalen / sizeof(struct file *); 12502bc21ed9SDavid Malone rp = data; 12512bc21ed9SDavid Malone 1252e2f9a08bSOlivier Houchard /* If we're not outputting the descriptors free them. */ 12532bc21ed9SDavid Malone if (error || controlp == NULL) { 12542bc21ed9SDavid Malone unp_freerights(rp, newfds); 12552bc21ed9SDavid Malone goto next; 12562bc21ed9SDavid Malone } 1257426da3bcSAlfred Perlstein FILEDESC_LOCK(td->td_proc->p_fd); 12582bc21ed9SDavid Malone /* if the new FD's will not fit free them. */ 12592bc21ed9SDavid Malone if (!fdavail(td, newfds)) { 1260426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 12612bc21ed9SDavid Malone error = EMSGSIZE; 12622bc21ed9SDavid Malone unp_freerights(rp, newfds); 12632bc21ed9SDavid Malone goto next; 1264df8bae1dSRodney W. Grimes } 1265ed5b7817SJulian Elischer /* 12662bc21ed9SDavid Malone * now change each pointer to an fd in the global 12672bc21ed9SDavid Malone * table to an integer that is the index to the 12682bc21ed9SDavid Malone * local fd table entry that we set up to point 12692bc21ed9SDavid Malone * to the global one we are transferring. 1270ed5b7817SJulian Elischer */ 12712bc21ed9SDavid Malone newlen = newfds * sizeof(int); 12722bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 12732bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 12742bc21ed9SDavid Malone if (*controlp == NULL) { 1275426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 12762bc21ed9SDavid Malone error = E2BIG; 12772bc21ed9SDavid Malone unp_freerights(rp, newfds); 12782bc21ed9SDavid Malone goto next; 12792bc21ed9SDavid Malone } 12802bc21ed9SDavid Malone 12812bc21ed9SDavid Malone fdp = (int *) 12822bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1283df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 1284a6d4491cSDag-Erling Smørgrav if (fdalloc(td, 0, &f)) 12852bc21ed9SDavid Malone panic("unp_externalize fdalloc failed"); 12868692c025SYoshinobu Inoue fp = *rp++; 1287b40ce416SJulian Elischer td->td_proc->p_fd->fd_ofiles[f] = fp; 1288426da3bcSAlfred Perlstein FILE_LOCK(fp); 1289df8bae1dSRodney W. Grimes fp->f_msgcount--; 1290426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1291df8bae1dSRodney W. Grimes unp_rights--; 12928692c025SYoshinobu Inoue *fdp++ = f; 1293df8bae1dSRodney W. Grimes } 1294426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 12952bc21ed9SDavid Malone } else { /* We can just copy anything else across */ 12962bc21ed9SDavid Malone if (error || controlp == NULL) 12972bc21ed9SDavid Malone goto next; 12982bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, datalen, 12992bc21ed9SDavid Malone cm->cmsg_type, cm->cmsg_level); 13002bc21ed9SDavid Malone if (*controlp == NULL) { 13012bc21ed9SDavid Malone error = ENOBUFS; 13022bc21ed9SDavid Malone goto next; 13032bc21ed9SDavid Malone } 13042bc21ed9SDavid Malone bcopy(data, 13052bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 13062bc21ed9SDavid Malone datalen); 13072bc21ed9SDavid Malone } 13082bc21ed9SDavid Malone 13092bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 13102bc21ed9SDavid Malone 13112bc21ed9SDavid Malone next: 13122bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 13132bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 13142bc21ed9SDavid Malone cm = (struct cmsghdr *) 13152bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 13168692c025SYoshinobu Inoue } else { 13172bc21ed9SDavid Malone clen = 0; 13182bc21ed9SDavid Malone cm = NULL; 13198692c025SYoshinobu Inoue } 13208692c025SYoshinobu Inoue } 13218692c025SYoshinobu Inoue 13222bc21ed9SDavid Malone m_freem(control); 13232bc21ed9SDavid Malone 13242bc21ed9SDavid Malone return (error); 1325df8bae1dSRodney W. Grimes } 1326df8bae1dSRodney W. Grimes 132798271db4SGarrett Wollman void 132898271db4SGarrett Wollman unp_init(void) 132998271db4SGarrett Wollman { 13309e9d298aSJeff Roberson unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 13319e9d298aSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 1332fc3fcacfSRobert Watson if (unp_zone == NULL) 133398271db4SGarrett Wollman panic("unp_init"); 1334b17dd2bcSColin Percival uma_zone_set_max(unp_zone, nmbclusters); 133598271db4SGarrett Wollman LIST_INIT(&unp_dhead); 133698271db4SGarrett Wollman LIST_INIT(&unp_shead); 13370d9ce3a1SRobert Watson 13380d9ce3a1SRobert Watson UNP_LOCK_INIT(); 133998271db4SGarrett Wollman } 134098271db4SGarrett Wollman 1341f708ef1bSPoul-Henning Kamp static int 1342892af6b9SRobert Watson unp_internalize(struct mbuf **controlp, struct thread *td) 1343df8bae1dSRodney W. Grimes { 13442bc21ed9SDavid Malone struct mbuf *control = *controlp; 1345b40ce416SJulian Elischer struct proc *p = td->td_proc; 13468692c025SYoshinobu Inoue struct filedesc *fdescp = p->p_fd; 13472bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 13482bc21ed9SDavid Malone struct cmsgcred *cmcred; 13492bc21ed9SDavid Malone struct file **rp; 13502bc21ed9SDavid Malone struct file *fp; 13512bc21ed9SDavid Malone struct timeval *tv; 13522bc21ed9SDavid Malone int i, fd, *fdp; 13532bc21ed9SDavid Malone void *data; 13542bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 13552bc21ed9SDavid Malone int error, oldfds; 13568692c025SYoshinobu Inoue u_int newlen; 1357df8bae1dSRodney W. Grimes 13584c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 13594c5bc1caSRobert Watson 13602bc21ed9SDavid Malone error = 0; 13612bc21ed9SDavid Malone *controlp = NULL; 13620b788fa1SBill Paul 13632bc21ed9SDavid Malone while (cm != NULL) { 13642bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 13652bc21ed9SDavid Malone || cm->cmsg_len > clen) { 13662bc21ed9SDavid Malone error = EINVAL; 13672bc21ed9SDavid Malone goto out; 13682bc21ed9SDavid Malone } 13692bc21ed9SDavid Malone 13702bc21ed9SDavid Malone data = CMSG_DATA(cm); 13712bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 13722bc21ed9SDavid Malone 13732bc21ed9SDavid Malone switch (cm->cmsg_type) { 13740b788fa1SBill Paul /* 13750b788fa1SBill Paul * Fill in credential information. 13760b788fa1SBill Paul */ 13772bc21ed9SDavid Malone case SCM_CREDS: 13782bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 13792bc21ed9SDavid Malone SCM_CREDS, SOL_SOCKET); 13802bc21ed9SDavid Malone if (*controlp == NULL) { 13812bc21ed9SDavid Malone error = ENOBUFS; 13822bc21ed9SDavid Malone goto out; 13832bc21ed9SDavid Malone } 13842bc21ed9SDavid Malone 13852bc21ed9SDavid Malone cmcred = (struct cmsgcred *) 13862bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 13870b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 1388a854ed98SJohn Baldwin cmcred->cmcred_uid = td->td_ucred->cr_ruid; 1389a854ed98SJohn Baldwin cmcred->cmcred_gid = td->td_ucred->cr_rgid; 1390a854ed98SJohn Baldwin cmcred->cmcred_euid = td->td_ucred->cr_uid; 1391a854ed98SJohn Baldwin cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 13920b788fa1SBill Paul CMGROUP_MAX); 13930b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 13942bc21ed9SDavid Malone cmcred->cmcred_groups[i] = 1395a854ed98SJohn Baldwin td->td_ucred->cr_groups[i]; 13962bc21ed9SDavid Malone break; 13970b788fa1SBill Paul 13982bc21ed9SDavid Malone case SCM_RIGHTS: 13992bc21ed9SDavid Malone oldfds = datalen / sizeof (int); 1400ed5b7817SJulian Elischer /* 14012bc21ed9SDavid Malone * check that all the FDs passed in refer to legal files 1402ed5b7817SJulian Elischer * If not, reject the entire operation. 1403ed5b7817SJulian Elischer */ 14042bc21ed9SDavid Malone fdp = data; 1405426da3bcSAlfred Perlstein FILEDESC_LOCK(fdescp); 1406df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 14078692c025SYoshinobu Inoue fd = *fdp++; 14088692c025SYoshinobu Inoue if ((unsigned)fd >= fdescp->fd_nfiles || 14092bc21ed9SDavid Malone fdescp->fd_ofiles[fd] == NULL) { 1410426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 14112bc21ed9SDavid Malone error = EBADF; 14122bc21ed9SDavid Malone goto out; 14132bc21ed9SDavid Malone } 1414e7d6662fSAlfred Perlstein fp = fdescp->fd_ofiles[fd]; 1415e7d6662fSAlfred Perlstein if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 1416e7d6662fSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 1417e7d6662fSAlfred Perlstein error = EOPNOTSUPP; 1418e7d6662fSAlfred Perlstein goto out; 1419e7d6662fSAlfred Perlstein } 1420e7d6662fSAlfred Perlstein 1421df8bae1dSRodney W. Grimes } 1422ed5b7817SJulian Elischer /* 1423ed5b7817SJulian Elischer * Now replace the integer FDs with pointers to 1424ed5b7817SJulian Elischer * the associated global file table entry.. 1425ed5b7817SJulian Elischer */ 14262bc21ed9SDavid Malone newlen = oldfds * sizeof(struct file *); 14272bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 14282bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 14292bc21ed9SDavid Malone if (*controlp == NULL) { 1430426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 14312bc21ed9SDavid Malone error = E2BIG; 14322bc21ed9SDavid Malone goto out; 14338692c025SYoshinobu Inoue } 14348692c025SYoshinobu Inoue 14352bc21ed9SDavid Malone fdp = data; 14362bc21ed9SDavid Malone rp = (struct file **) 14372bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 14388692c025SYoshinobu Inoue for (i = 0; i < oldfds; i++) { 14398692c025SYoshinobu Inoue fp = fdescp->fd_ofiles[*fdp++]; 1440df8bae1dSRodney W. Grimes *rp++ = fp; 1441426da3bcSAlfred Perlstein FILE_LOCK(fp); 1442df8bae1dSRodney W. Grimes fp->f_count++; 1443df8bae1dSRodney W. Grimes fp->f_msgcount++; 1444426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1445df8bae1dSRodney W. Grimes unp_rights++; 1446df8bae1dSRodney W. Grimes } 1447426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 14482bc21ed9SDavid Malone break; 14492bc21ed9SDavid Malone 14502bc21ed9SDavid Malone case SCM_TIMESTAMP: 14512bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*tv), 14522bc21ed9SDavid Malone SCM_TIMESTAMP, SOL_SOCKET); 14532bc21ed9SDavid Malone if (*controlp == NULL) { 14542bc21ed9SDavid Malone error = ENOBUFS; 14552bc21ed9SDavid Malone goto out; 14568692c025SYoshinobu Inoue } 14572bc21ed9SDavid Malone tv = (struct timeval *) 14582bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 14592bc21ed9SDavid Malone microtime(tv); 14602bc21ed9SDavid Malone break; 14612bc21ed9SDavid Malone 14622bc21ed9SDavid Malone default: 14632bc21ed9SDavid Malone error = EINVAL; 14642bc21ed9SDavid Malone goto out; 14652bc21ed9SDavid Malone } 14662bc21ed9SDavid Malone 14672bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 14682bc21ed9SDavid Malone 14692bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 14702bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 14712bc21ed9SDavid Malone cm = (struct cmsghdr *) 14722bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 14732bc21ed9SDavid Malone } else { 14742bc21ed9SDavid Malone clen = 0; 14752bc21ed9SDavid Malone cm = NULL; 14762bc21ed9SDavid Malone } 14772bc21ed9SDavid Malone } 14782bc21ed9SDavid Malone 14792bc21ed9SDavid Malone out: 14802bc21ed9SDavid Malone m_freem(control); 14812bc21ed9SDavid Malone 14822bc21ed9SDavid Malone return (error); 1483df8bae1dSRodney W. Grimes } 1484df8bae1dSRodney W. Grimes 1485161a0c7cSRobert Watson /* 1486161a0c7cSRobert Watson * unp_defer is thread-local during garbage collection, and does not require 1487161a0c7cSRobert Watson * explicit synchronization. unp_gcing prevents other threads from entering 1488161a0c7cSRobert Watson * garbage collection, and perhaps should be an sx lock instead. 1489161a0c7cSRobert Watson */ 1490f708ef1bSPoul-Henning Kamp static int unp_defer, unp_gcing; 1491df8bae1dSRodney W. Grimes 1492f708ef1bSPoul-Henning Kamp static void 1493892af6b9SRobert Watson unp_gc(void) 1494df8bae1dSRodney W. Grimes { 1495892af6b9SRobert Watson struct file *fp, *nextfp; 1496892af6b9SRobert Watson struct socket *so; 1497df8bae1dSRodney W. Grimes struct file **extra_ref, **fpp; 1498df8bae1dSRodney W. Grimes int nunref, i; 149995f004dcSAlfred Perlstein int nfiles_snap; 150095f004dcSAlfred Perlstein int nfiles_slack = 20; 1501df8bae1dSRodney W. Grimes 15020d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 15030d9ce3a1SRobert Watson 1504161a0c7cSRobert Watson if (unp_gcing) { 1505161a0c7cSRobert Watson UNP_UNLOCK(); 1506df8bae1dSRodney W. Grimes return; 1507161a0c7cSRobert Watson } 1508df8bae1dSRodney W. Grimes unp_gcing = 1; 1509df8bae1dSRodney W. Grimes unp_defer = 0; 1510161a0c7cSRobert Watson UNP_UNLOCK(); 1511ed5b7817SJulian Elischer /* 1512ed5b7817SJulian Elischer * before going through all this, set all FDs to 1513ed5b7817SJulian Elischer * be NOT defered and NOT externally accessible 1514ed5b7817SJulian Elischer */ 1515426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 15162e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) 1517426da3bcSAlfred Perlstein fp->f_gcflag &= ~(FMARK|FDEFER); 1518df8bae1dSRodney W. Grimes do { 15192e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) { 1520426da3bcSAlfred Perlstein FILE_LOCK(fp); 1521ed5b7817SJulian Elischer /* 1522ed5b7817SJulian Elischer * If the file is not open, skip it 1523ed5b7817SJulian Elischer */ 1524426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1525426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1526df8bae1dSRodney W. Grimes continue; 1527426da3bcSAlfred Perlstein } 1528ed5b7817SJulian Elischer /* 1529ed5b7817SJulian Elischer * If we already marked it as 'defer' in a 1530ed5b7817SJulian Elischer * previous pass, then try process it this time 1531ed5b7817SJulian Elischer * and un-mark it 1532ed5b7817SJulian Elischer */ 1533426da3bcSAlfred Perlstein if (fp->f_gcflag & FDEFER) { 1534426da3bcSAlfred Perlstein fp->f_gcflag &= ~FDEFER; 1535df8bae1dSRodney W. Grimes unp_defer--; 1536df8bae1dSRodney W. Grimes } else { 1537ed5b7817SJulian Elischer /* 1538ed5b7817SJulian Elischer * if it's not defered, then check if it's 1539ed5b7817SJulian Elischer * already marked.. if so skip it 1540ed5b7817SJulian Elischer */ 1541426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) { 1542426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1543df8bae1dSRodney W. Grimes continue; 1544426da3bcSAlfred Perlstein } 1545ed5b7817SJulian Elischer /* 1546ed5b7817SJulian Elischer * If all references are from messages 1547ed5b7817SJulian Elischer * in transit, then skip it. it's not 1548ed5b7817SJulian Elischer * externally accessible. 1549ed5b7817SJulian Elischer */ 1550426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount) { 1551426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1552df8bae1dSRodney W. Grimes continue; 1553426da3bcSAlfred Perlstein } 1554ed5b7817SJulian Elischer /* 1555ed5b7817SJulian Elischer * If it got this far then it must be 1556ed5b7817SJulian Elischer * externally accessible. 1557ed5b7817SJulian Elischer */ 1558426da3bcSAlfred Perlstein fp->f_gcflag |= FMARK; 1559df8bae1dSRodney W. Grimes } 1560ed5b7817SJulian Elischer /* 1561ed5b7817SJulian Elischer * either it was defered, or it is externally 1562ed5b7817SJulian Elischer * accessible and not already marked so. 1563ed5b7817SJulian Elischer * Now check if it is possibly one of OUR sockets. 1564ed5b7817SJulian Elischer */ 1565df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_SOCKET || 156648e3128bSMatthew Dillon (so = fp->f_data) == NULL) { 1567426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1568df8bae1dSRodney W. Grimes continue; 1569426da3bcSAlfred Perlstein } 1570426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1571748e0b0aSGarrett Wollman if (so->so_proto->pr_domain != &localdomain || 1572df8bae1dSRodney W. Grimes (so->so_proto->pr_flags&PR_RIGHTS) == 0) 1573df8bae1dSRodney W. Grimes continue; 1574df8bae1dSRodney W. Grimes #ifdef notdef 1575df8bae1dSRodney W. Grimes if (so->so_rcv.sb_flags & SB_LOCK) { 1576df8bae1dSRodney W. Grimes /* 1577df8bae1dSRodney W. Grimes * This is problematical; it's not clear 1578df8bae1dSRodney W. Grimes * we need to wait for the sockbuf to be 1579df8bae1dSRodney W. Grimes * unlocked (on a uniprocessor, at least), 1580df8bae1dSRodney W. Grimes * and it's also not clear what to do 1581df8bae1dSRodney W. Grimes * if sbwait returns an error due to receipt 1582df8bae1dSRodney W. Grimes * of a signal. If sbwait does return 1583df8bae1dSRodney W. Grimes * an error, we'll go into an infinite 1584df8bae1dSRodney W. Grimes * loop. Delete all of this for now. 1585df8bae1dSRodney W. Grimes */ 1586df8bae1dSRodney W. Grimes (void) sbwait(&so->so_rcv); 1587df8bae1dSRodney W. Grimes goto restart; 1588df8bae1dSRodney W. Grimes } 1589df8bae1dSRodney W. Grimes #endif 1590ed5b7817SJulian Elischer /* 1591ed5b7817SJulian Elischer * So, Ok, it's one of our sockets and it IS externally 1592ed5b7817SJulian Elischer * accessible (or was defered). Now we look 1593dc733423SDag-Erling Smørgrav * to see if we hold any file descriptors in its 1594ed5b7817SJulian Elischer * message buffers. Follow those links and mark them 1595ed5b7817SJulian Elischer * as accessible too. 1596ed5b7817SJulian Elischer */ 15977717cf07SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1598df8bae1dSRodney W. Grimes unp_scan(so->so_rcv.sb_mb, unp_mark); 15997717cf07SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 1600df8bae1dSRodney W. Grimes } 1601df8bae1dSRodney W. Grimes } while (unp_defer); 1602426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1603df8bae1dSRodney W. Grimes /* 1604df8bae1dSRodney W. Grimes * We grab an extra reference to each of the file table entries 1605df8bae1dSRodney W. Grimes * that are not otherwise accessible and then free the rights 1606df8bae1dSRodney W. Grimes * that are stored in messages on them. 1607df8bae1dSRodney W. Grimes * 1608df8bae1dSRodney W. Grimes * The bug in the orginal code is a little tricky, so I'll describe 1609df8bae1dSRodney W. Grimes * what's wrong with it here. 1610df8bae1dSRodney W. Grimes * 1611df8bae1dSRodney W. Grimes * It is incorrect to simply unp_discard each entry for f_msgcount 1612df8bae1dSRodney W. Grimes * times -- consider the case of sockets A and B that contain 1613df8bae1dSRodney W. Grimes * references to each other. On a last close of some other socket, 1614df8bae1dSRodney W. Grimes * we trigger a gc since the number of outstanding rights (unp_rights) 1615df8bae1dSRodney W. Grimes * is non-zero. If during the sweep phase the gc code un_discards, 1616df8bae1dSRodney W. Grimes * we end up doing a (full) closef on the descriptor. A closef on A 1617df8bae1dSRodney W. Grimes * results in the following chain. Closef calls soo_close, which 1618df8bae1dSRodney W. Grimes * calls soclose. Soclose calls first (through the switch 1619df8bae1dSRodney W. Grimes * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 1620df8bae1dSRodney W. Grimes * returns because the previous instance had set unp_gcing, and 1621df8bae1dSRodney W. Grimes * we return all the way back to soclose, which marks the socket 1622df8bae1dSRodney W. Grimes * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush 1623df8bae1dSRodney W. Grimes * to free up the rights that are queued in messages on the socket A, 1624df8bae1dSRodney W. Grimes * i.e., the reference on B. The sorflush calls via the dom_dispose 1625df8bae1dSRodney W. Grimes * switch unp_dispose, which unp_scans with unp_discard. This second 1626df8bae1dSRodney W. Grimes * instance of unp_discard just calls closef on B. 1627df8bae1dSRodney W. Grimes * 1628df8bae1dSRodney W. Grimes * Well, a similar chain occurs on B, resulting in a sorflush on B, 1629df8bae1dSRodney W. Grimes * which results in another closef on A. Unfortunately, A is already 1630df8bae1dSRodney W. Grimes * being closed, and the descriptor has already been marked with 1631df8bae1dSRodney W. Grimes * SS_NOFDREF, and soclose panics at this point. 1632df8bae1dSRodney W. Grimes * 1633df8bae1dSRodney W. Grimes * Here, we first take an extra reference to each inaccessible 1634df8bae1dSRodney W. Grimes * descriptor. Then, we call sorflush ourself, since we know 1635df8bae1dSRodney W. Grimes * it is a Unix domain socket anyhow. After we destroy all the 1636df8bae1dSRodney W. Grimes * rights carried in messages, we do a last closef to get rid 1637df8bae1dSRodney W. Grimes * of our extra reference. This is the last close, and the 1638df8bae1dSRodney W. Grimes * unp_detach etc will shut down the socket. 1639df8bae1dSRodney W. Grimes * 1640df8bae1dSRodney W. Grimes * 91/09/19, bsy@cs.cmu.edu 1641df8bae1dSRodney W. Grimes */ 164295f004dcSAlfred Perlstein again: 1643e4643c73SPoul-Henning Kamp nfiles_snap = openfiles + nfiles_slack; /* some slack */ 164495f004dcSAlfred Perlstein extra_ref = malloc(nfiles_snap * sizeof(struct file *), M_TEMP, 164595f004dcSAlfred Perlstein M_WAITOK); 1646426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 1647e4643c73SPoul-Henning Kamp if (nfiles_snap < openfiles) { 164895f004dcSAlfred Perlstein sx_sunlock(&filelist_lock); 164995f004dcSAlfred Perlstein free(extra_ref, M_TEMP); 165095f004dcSAlfred Perlstein nfiles_slack += 20; 165195f004dcSAlfred Perlstein goto again; 165295f004dcSAlfred Perlstein } 1653fc3fcacfSRobert Watson for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; 1654fc3fcacfSRobert Watson fp != NULL; fp = nextfp) { 16552e3c8fcbSPoul-Henning Kamp nextfp = LIST_NEXT(fp, f_list); 1656426da3bcSAlfred Perlstein FILE_LOCK(fp); 1657ed5b7817SJulian Elischer /* 1658ed5b7817SJulian Elischer * If it's not open, skip it 1659ed5b7817SJulian Elischer */ 1660426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1661426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1662df8bae1dSRodney W. Grimes continue; 1663426da3bcSAlfred Perlstein } 1664ed5b7817SJulian Elischer /* 1665ed5b7817SJulian Elischer * If all refs are from msgs, and it's not marked accessible 1666ed5b7817SJulian Elischer * then it must be referenced from some unreachable cycle 1667ed5b7817SJulian Elischer * of (shut-down) FDs, so include it in our 1668ed5b7817SJulian Elischer * list of FDs to remove 1669ed5b7817SJulian Elischer */ 1670426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) { 1671df8bae1dSRodney W. Grimes *fpp++ = fp; 1672df8bae1dSRodney W. Grimes nunref++; 1673df8bae1dSRodney W. Grimes fp->f_count++; 1674df8bae1dSRodney W. Grimes } 1675426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1676df8bae1dSRodney W. Grimes } 1677426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1678ed5b7817SJulian Elischer /* 1679ed5b7817SJulian Elischer * for each FD on our hit list, do the following two things 1680ed5b7817SJulian Elischer */ 16811c7c3c6aSMatthew Dillon for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 16821c7c3c6aSMatthew Dillon struct file *tfp = *fpp; 1683426da3bcSAlfred Perlstein FILE_LOCK(tfp); 1684cd72f218SMatthew Dillon if (tfp->f_type == DTYPE_SOCKET && 168548e3128bSMatthew Dillon tfp->f_data != NULL) { 1686426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 168748e3128bSMatthew Dillon sorflush(tfp->f_data); 1688e5aeaa0cSDag-Erling Smørgrav } else { 1689426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 16901c7c3c6aSMatthew Dillon } 1691e5aeaa0cSDag-Erling Smørgrav } 1692df8bae1dSRodney W. Grimes for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) 1693b40ce416SJulian Elischer closef(*fpp, (struct thread *) NULL); 1694210a5a71SAlfred Perlstein free(extra_ref, M_TEMP); 1695df8bae1dSRodney W. Grimes unp_gcing = 0; 1696161a0c7cSRobert Watson 1697161a0c7cSRobert Watson UNP_UNLOCK_ASSERT(); 1698df8bae1dSRodney W. Grimes } 1699df8bae1dSRodney W. Grimes 170026f9a767SRodney W. Grimes void 1701892af6b9SRobert Watson unp_dispose(struct mbuf *m) 1702df8bae1dSRodney W. Grimes { 1703996c772fSJohn Dyson 1704df8bae1dSRodney W. Grimes if (m) 1705df8bae1dSRodney W. Grimes unp_scan(m, unp_discard); 1706df8bae1dSRodney W. Grimes } 1707df8bae1dSRodney W. Grimes 17080c1bb4fbSDima Dorfman static int 1709892af6b9SRobert Watson unp_listen(struct unpcb *unp, struct thread *td) 17100c1bb4fbSDima Dorfman { 17110d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 17120c1bb4fbSDima Dorfman 17130d9ce3a1SRobert Watson /* 17140d9ce3a1SRobert Watson * XXXRW: Why populate the local peer cred with our own credential? 17150d9ce3a1SRobert Watson */ 17166f105b34SJohn Baldwin cru2x(td->td_ucred, &unp->unp_peercred); 17170c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPCCACHED; 17180c1bb4fbSDima Dorfman return (0); 17190c1bb4fbSDima Dorfman } 17200c1bb4fbSDima Dorfman 1721f708ef1bSPoul-Henning Kamp static void 1722892af6b9SRobert Watson unp_scan(struct mbuf *m0, 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 1771892af6b9SRobert Watson unp_mark(struct file *fp) 1772df8bae1dSRodney W. Grimes { 1773426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) 1774df8bae1dSRodney W. Grimes return; 1775df8bae1dSRodney W. Grimes unp_defer++; 1776426da3bcSAlfred Perlstein fp->f_gcflag |= (FMARK|FDEFER); 1777df8bae1dSRodney W. Grimes } 1778df8bae1dSRodney W. Grimes 1779f708ef1bSPoul-Henning Kamp static void 1780892af6b9SRobert Watson unp_discard(struct file *fp) 1781df8bae1dSRodney W. Grimes { 1782426da3bcSAlfred Perlstein FILE_LOCK(fp); 1783df8bae1dSRodney W. Grimes fp->f_msgcount--; 1784df8bae1dSRodney W. Grimes unp_rights--; 1785426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1786b40ce416SJulian Elischer (void) closef(fp, (struct thread *)NULL); 1787df8bae1dSRodney W. Grimes } 1788