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) 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(); 143395a08c9SRobert Watson SOCK_LOCK(so); 144ddb7d629SIan Dowse sotryfree(so); 145e5aeaa0cSDag-Erling Smørgrav return (0); 146df8bae1dSRodney W. Grimes } 147df8bae1dSRodney W. Grimes 148a29f300eSGarrett Wollman static int 14957bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam) 150a29f300eSGarrett Wollman { 15140f2ac28SRobert Watson struct unpcb *unp; 1520d9ce3a1SRobert Watson const struct sockaddr *sa; 153df8bae1dSRodney W. Grimes 154df8bae1dSRodney W. Grimes /* 155df8bae1dSRodney W. Grimes * Pass back name of connected socket, 156df8bae1dSRodney W. Grimes * if it was bound and we are still connected 157df8bae1dSRodney W. Grimes * (our peer may have closed already!). 158df8bae1dSRodney W. Grimes */ 1590d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 1600d9ce3a1SRobert Watson UNP_LOCK(); 16140f2ac28SRobert Watson unp = sotounpcb(so); 16240f2ac28SRobert Watson if (unp == NULL) { 16340f2ac28SRobert Watson UNP_UNLOCK(); 16440f2ac28SRobert Watson free(*nam, M_SONAME); 16540f2ac28SRobert Watson *nam = NULL; 16640f2ac28SRobert Watson return (EINVAL); 16740f2ac28SRobert Watson } 1680d9ce3a1SRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr != NULL) 1690d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 1700d9ce3a1SRobert Watson else 1710d9ce3a1SRobert Watson sa = &sun_noname; 1720d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 1730d9ce3a1SRobert Watson UNP_UNLOCK(); 174e5aeaa0cSDag-Erling Smørgrav return (0); 175a29f300eSGarrett Wollman } 176df8bae1dSRodney W. Grimes 177a29f300eSGarrett Wollman static int 178b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td) 179a29f300eSGarrett Wollman { 180a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 181df8bae1dSRodney W. Grimes 182fc3fcacfSRobert Watson if (unp != NULL) 183e5aeaa0cSDag-Erling Smørgrav return (EISCONN); 184e5aeaa0cSDag-Erling Smørgrav return (unp_attach(so)); 185a29f300eSGarrett Wollman } 186a29f300eSGarrett Wollman 187a29f300eSGarrett Wollman static int 188b40ce416SJulian Elischer uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 189a29f300eSGarrett Wollman { 19040f2ac28SRobert Watson struct unpcb *unp; 19140f2ac28SRobert Watson int error; 192a29f300eSGarrett Wollman 19340f2ac28SRobert Watson UNP_LOCK(); 19440f2ac28SRobert Watson unp = sotounpcb(so); 19540f2ac28SRobert Watson if (unp == NULL) { 19640f2ac28SRobert Watson UNP_UNLOCK(); 197e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 19840f2ac28SRobert Watson } 19940f2ac28SRobert Watson error = unp_bind(unp, nam, td); 20040f2ac28SRobert Watson UNP_UNLOCK(); 20140f2ac28SRobert Watson return (error); 202a29f300eSGarrett Wollman } 203a29f300eSGarrett Wollman 204a29f300eSGarrett Wollman static int 205b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 206a29f300eSGarrett Wollman { 207b295bdcdSRobert Watson struct unpcb *unp; 2080d9ce3a1SRobert Watson int error; 209a29f300eSGarrett Wollman 210fd179ee9SRobert Watson KASSERT(td == curthread, ("uipc_connect: td != curthread")); 211fd179ee9SRobert Watson 2120d9ce3a1SRobert Watson UNP_LOCK(); 213b295bdcdSRobert Watson unp = sotounpcb(so); 214b295bdcdSRobert Watson if (unp == NULL) { 21540f2ac28SRobert Watson UNP_UNLOCK(); 21640f2ac28SRobert Watson return (EINVAL); 217b295bdcdSRobert Watson } 218fd179ee9SRobert Watson error = unp_connect(so, nam, td); 2190d9ce3a1SRobert Watson UNP_UNLOCK(); 2200d9ce3a1SRobert Watson return (error); 221a29f300eSGarrett Wollman } 222a29f300eSGarrett Wollman 223db48c0d2SRobert Watson int 224a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2) 225a29f300eSGarrett Wollman { 22640f2ac28SRobert Watson struct unpcb *unp; 2270d9ce3a1SRobert Watson int error; 228a29f300eSGarrett Wollman 2290d9ce3a1SRobert Watson UNP_LOCK(); 23040f2ac28SRobert Watson unp = sotounpcb(so1); 23140f2ac28SRobert Watson if (unp == NULL) { 23240f2ac28SRobert Watson UNP_UNLOCK(); 23340f2ac28SRobert Watson return (EINVAL); 23440f2ac28SRobert Watson } 2350d9ce3a1SRobert Watson error = unp_connect2(so1, so2); 2360d9ce3a1SRobert Watson UNP_UNLOCK(); 2370d9ce3a1SRobert Watson return (error); 238a29f300eSGarrett Wollman } 239a29f300eSGarrett Wollman 240a29f300eSGarrett Wollman /* control is EOPNOTSUPP */ 241a29f300eSGarrett Wollman 242a29f300eSGarrett Wollman static int 243a29f300eSGarrett Wollman uipc_detach(struct socket *so) 244a29f300eSGarrett Wollman { 24540f2ac28SRobert Watson struct unpcb *unp; 246a29f300eSGarrett Wollman 2470d9ce3a1SRobert Watson UNP_LOCK(); 24840f2ac28SRobert Watson unp = sotounpcb(so); 24940f2ac28SRobert Watson if (unp == NULL) { 25040f2ac28SRobert Watson UNP_UNLOCK(); 25140f2ac28SRobert Watson return (EINVAL); 25240f2ac28SRobert Watson } 2534c5bc1caSRobert Watson unp_detach(unp); 2544c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 255e5aeaa0cSDag-Erling Smørgrav return (0); 256a29f300eSGarrett Wollman } 257a29f300eSGarrett Wollman 258a29f300eSGarrett Wollman static int 259a29f300eSGarrett Wollman uipc_disconnect(struct socket *so) 260a29f300eSGarrett Wollman { 26140f2ac28SRobert Watson struct unpcb *unp; 262a29f300eSGarrett Wollman 2630d9ce3a1SRobert Watson UNP_LOCK(); 26440f2ac28SRobert Watson unp = sotounpcb(so); 26540f2ac28SRobert Watson if (unp == NULL) { 26640f2ac28SRobert Watson UNP_UNLOCK(); 26740f2ac28SRobert Watson return (EINVAL); 26840f2ac28SRobert Watson } 269a29f300eSGarrett Wollman unp_disconnect(unp); 2700d9ce3a1SRobert Watson UNP_UNLOCK(); 271e5aeaa0cSDag-Erling Smørgrav return (0); 272a29f300eSGarrett Wollman } 273a29f300eSGarrett Wollman 274a29f300eSGarrett Wollman static int 275b40ce416SJulian Elischer uipc_listen(struct socket *so, struct thread *td) 276a29f300eSGarrett Wollman { 27740f2ac28SRobert Watson struct unpcb *unp; 2780d9ce3a1SRobert Watson int error; 279a29f300eSGarrett Wollman 2800d9ce3a1SRobert Watson UNP_LOCK(); 28140f2ac28SRobert Watson unp = sotounpcb(so); 28240f2ac28SRobert Watson if (unp == NULL || unp->unp_vnode == NULL) { 28340f2ac28SRobert Watson UNP_UNLOCK(); 28440f2ac28SRobert Watson return (EINVAL); 28540f2ac28SRobert Watson } 2860d9ce3a1SRobert Watson error = unp_listen(unp, td); 2870d9ce3a1SRobert Watson UNP_UNLOCK(); 2880d9ce3a1SRobert Watson return (error); 289a29f300eSGarrett Wollman } 290a29f300eSGarrett Wollman 291a29f300eSGarrett Wollman static int 29257bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 293a29f300eSGarrett Wollman { 29440f2ac28SRobert Watson struct unpcb *unp; 2950d9ce3a1SRobert Watson const struct sockaddr *sa; 296a29f300eSGarrett Wollman 2970d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 2980d9ce3a1SRobert Watson UNP_LOCK(); 29940f2ac28SRobert Watson unp = sotounpcb(so); 30040f2ac28SRobert Watson if (unp == NULL) { 30140f2ac28SRobert Watson UNP_UNLOCK(); 30240f2ac28SRobert Watson free(*nam, M_SONAME); 30340f2ac28SRobert Watson *nam = NULL; 30440f2ac28SRobert Watson return (EINVAL); 30540f2ac28SRobert Watson } 306fc3fcacfSRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr!= NULL) 3070d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 308bdc5f6a3SHajimu UMEMOTO else { 309bdc5f6a3SHajimu UMEMOTO /* 310bdc5f6a3SHajimu UMEMOTO * XXX: It seems that this test always fails even when 311bdc5f6a3SHajimu UMEMOTO * connection is established. So, this else clause is 312bdc5f6a3SHajimu UMEMOTO * added as workaround to return PF_LOCAL sockaddr. 313bdc5f6a3SHajimu UMEMOTO */ 3140d9ce3a1SRobert Watson sa = &sun_noname; 315bdc5f6a3SHajimu UMEMOTO } 3160d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 3170d9ce3a1SRobert Watson UNP_UNLOCK(); 318e5aeaa0cSDag-Erling Smørgrav return (0); 319a29f300eSGarrett Wollman } 320a29f300eSGarrett Wollman 321a29f300eSGarrett Wollman static int 322a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 323a29f300eSGarrett Wollman { 32440f2ac28SRobert Watson struct unpcb *unp; 325a29f300eSGarrett Wollman struct socket *so2; 3266aef685fSBrian Feldman u_long newhiwat; 327a29f300eSGarrett Wollman 3280d9ce3a1SRobert Watson UNP_LOCK(); 32940f2ac28SRobert Watson unp = sotounpcb(so); 33040f2ac28SRobert Watson if (unp == NULL) { 33140f2ac28SRobert Watson UNP_UNLOCK(); 33240f2ac28SRobert Watson return (EINVAL); 33340f2ac28SRobert Watson } 334df8bae1dSRodney W. Grimes switch (so->so_type) { 335df8bae1dSRodney W. Grimes case SOCK_DGRAM: 336a29f300eSGarrett Wollman panic("uipc_rcvd DGRAM?"); 337df8bae1dSRodney W. Grimes /*NOTREACHED*/ 338df8bae1dSRodney W. Grimes 339df8bae1dSRodney W. Grimes case SOCK_STREAM: 340fc3fcacfSRobert Watson if (unp->unp_conn == NULL) 341df8bae1dSRodney W. Grimes break; 342df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 343c9f69064SRobert Watson SOCKBUF_LOCK(&so2->so_snd); 344c9f69064SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 345df8bae1dSRodney W. Grimes /* 346df8bae1dSRodney W. Grimes * Adjust backpressure on sender 347df8bae1dSRodney W. Grimes * and wakeup any waiting to write. 348df8bae1dSRodney W. Grimes */ 349ff8b0106SBrian Feldman so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt; 350ff8b0106SBrian Feldman unp->unp_mbcnt = so->so_rcv.sb_mbcnt; 3516aef685fSBrian Feldman newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - 3526aef685fSBrian Feldman so->so_rcv.sb_cc; 353f535380cSDon Lewis (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat, 3546aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 355ff8b0106SBrian Feldman unp->unp_cc = so->so_rcv.sb_cc; 356c9f69064SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 3571e4d7da7SRobert Watson sowwakeup_locked(so2); 358df8bae1dSRodney W. Grimes break; 359df8bae1dSRodney W. Grimes 360df8bae1dSRodney W. Grimes default: 361a29f300eSGarrett Wollman panic("uipc_rcvd unknown socktype"); 362df8bae1dSRodney W. Grimes } 3630d9ce3a1SRobert Watson UNP_UNLOCK(); 364e5aeaa0cSDag-Erling Smørgrav return (0); 365a29f300eSGarrett Wollman } 366df8bae1dSRodney W. Grimes 367a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 368a29f300eSGarrett Wollman 369a29f300eSGarrett Wollman static int 37057bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 371b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 372a29f300eSGarrett Wollman { 373a29f300eSGarrett Wollman int error = 0; 37440f2ac28SRobert Watson struct unpcb *unp; 375a29f300eSGarrett Wollman struct socket *so2; 3766aef685fSBrian Feldman u_long newhiwat; 377a29f300eSGarrett Wollman 37840f2ac28SRobert Watson unp = sotounpcb(so); 379fc3fcacfSRobert Watson if (unp == NULL) { 380a29f300eSGarrett Wollman error = EINVAL; 381a29f300eSGarrett Wollman goto release; 382a29f300eSGarrett Wollman } 383a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 384a29f300eSGarrett Wollman error = EOPNOTSUPP; 385a29f300eSGarrett Wollman goto release; 386a29f300eSGarrett Wollman } 387a29f300eSGarrett Wollman 388fc3fcacfSRobert Watson if (control != NULL && (error = unp_internalize(&control, td))) 389a29f300eSGarrett Wollman goto release; 390df8bae1dSRodney W. Grimes 3910d9ce3a1SRobert Watson UNP_LOCK(); 39240f2ac28SRobert Watson unp = sotounpcb(so); 39340f2ac28SRobert Watson if (unp == NULL) { 39440f2ac28SRobert Watson UNP_UNLOCK(); 39540f2ac28SRobert Watson error = EINVAL; 39640f2ac28SRobert Watson goto dispose_release; 39740f2ac28SRobert Watson } 39840f2ac28SRobert Watson 399a29f300eSGarrett Wollman switch (so->so_type) { 400a29f300eSGarrett Wollman case SOCK_DGRAM: 401a29f300eSGarrett Wollman { 402e7dd9a10SRobert Watson const struct sockaddr *from; 403df8bae1dSRodney W. Grimes 404fc3fcacfSRobert Watson if (nam != NULL) { 405fc3fcacfSRobert Watson if (unp->unp_conn != NULL) { 406df8bae1dSRodney W. Grimes error = EISCONN; 407df8bae1dSRodney W. Grimes break; 408df8bae1dSRodney W. Grimes } 409b40ce416SJulian Elischer error = unp_connect(so, nam, td); 410df8bae1dSRodney W. Grimes if (error) 411df8bae1dSRodney W. Grimes break; 412df8bae1dSRodney W. Grimes } else { 413fc3fcacfSRobert Watson if (unp->unp_conn == NULL) { 414df8bae1dSRodney W. Grimes error = ENOTCONN; 415df8bae1dSRodney W. Grimes break; 416df8bae1dSRodney W. Grimes } 417df8bae1dSRodney W. Grimes } 418df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 419fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 42057bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 421df8bae1dSRodney W. Grimes else 422df8bae1dSRodney W. Grimes from = &sun_noname; 423a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 424a34b7046SRobert Watson if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) { 4251e4d7da7SRobert Watson sorwakeup_locked(so2); 426fc3fcacfSRobert Watson m = NULL; 427fc3fcacfSRobert Watson control = NULL; 428e5aeaa0cSDag-Erling Smørgrav } else { 429a34b7046SRobert Watson SOCKBUF_UNLOCK(&so2->so_rcv); 430df8bae1dSRodney W. Grimes error = ENOBUFS; 431e5aeaa0cSDag-Erling Smørgrav } 432fc3fcacfSRobert Watson if (nam != NULL) 433df8bae1dSRodney W. Grimes unp_disconnect(unp); 434df8bae1dSRodney W. Grimes break; 435df8bae1dSRodney W. Grimes } 436df8bae1dSRodney W. Grimes 437df8bae1dSRodney W. Grimes case SOCK_STREAM: 4386b8fda4dSGarrett Wollman /* Connect if not connected yet. */ 4396b8fda4dSGarrett Wollman /* 4406b8fda4dSGarrett Wollman * Note: A better implementation would complain 441402cc72dSDavid Greenman * if not equal to the peer's address. 4426b8fda4dSGarrett Wollman */ 443402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 444fc3fcacfSRobert Watson if (nam != NULL) { 445b40ce416SJulian Elischer error = unp_connect(so, nam, td); 446402cc72dSDavid Greenman if (error) 4476b8fda4dSGarrett Wollman break; /* XXX */ 448402cc72dSDavid Greenman } else { 449402cc72dSDavid Greenman error = ENOTCONN; 450402cc72dSDavid Greenman break; 451402cc72dSDavid Greenman } 452402cc72dSDavid Greenman } 453402cc72dSDavid Greenman 454c0b99ffaSRobert Watson if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 455df8bae1dSRodney W. Grimes error = EPIPE; 456df8bae1dSRodney W. Grimes break; 457df8bae1dSRodney W. Grimes } 458fc3fcacfSRobert Watson if (unp->unp_conn == NULL) 459a29f300eSGarrett Wollman panic("uipc_send connected but no connection?"); 460df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 461a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 462df8bae1dSRodney W. Grimes /* 463df8bae1dSRodney W. Grimes * Send to paired receive port, and then reduce 464df8bae1dSRodney W. Grimes * send buffer hiwater marks to maintain backpressure. 465df8bae1dSRodney W. Grimes * Wake up readers. 466df8bae1dSRodney W. Grimes */ 467fc3fcacfSRobert Watson if (control != NULL) { 468a34b7046SRobert Watson if (sbappendcontrol_locked(&so2->so_rcv, m, control)) 469fc3fcacfSRobert Watson control = NULL; 470e5aeaa0cSDag-Erling Smørgrav } else { 471a34b7046SRobert Watson sbappend_locked(&so2->so_rcv, m); 472e5aeaa0cSDag-Erling Smørgrav } 473ff8b0106SBrian Feldman so->so_snd.sb_mbmax -= 474ff8b0106SBrian Feldman so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt; 475ff8b0106SBrian Feldman unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt; 4766aef685fSBrian Feldman newhiwat = so->so_snd.sb_hiwat - 4776aef685fSBrian Feldman (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc); 478f535380cSDon Lewis (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat, 4796aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 480ff8b0106SBrian Feldman unp->unp_conn->unp_cc = so2->so_rcv.sb_cc; 4811e4d7da7SRobert Watson sorwakeup_locked(so2); 482fc3fcacfSRobert Watson m = NULL; 483df8bae1dSRodney W. Grimes break; 484df8bae1dSRodney W. Grimes 485df8bae1dSRodney W. Grimes default: 486a29f300eSGarrett Wollman panic("uipc_send unknown socktype"); 487df8bae1dSRodney W. Grimes } 488a29f300eSGarrett Wollman 4896b8fda4dSGarrett Wollman /* 4906b8fda4dSGarrett Wollman * SEND_EOF is equivalent to a SEND followed by 4916b8fda4dSGarrett Wollman * a SHUTDOWN. 4926b8fda4dSGarrett Wollman */ 493a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 4946b8fda4dSGarrett Wollman socantsendmore(so); 4956b8fda4dSGarrett Wollman unp_shutdown(unp); 4966b8fda4dSGarrett Wollman } 4970d9ce3a1SRobert Watson UNP_UNLOCK(); 498df8bae1dSRodney W. Grimes 49940f2ac28SRobert Watson dispose_release: 500fc3fcacfSRobert Watson if (control != NULL && error != 0) 501bd508d39SDon Lewis unp_dispose(control); 502bd508d39SDon Lewis 503a29f300eSGarrett Wollman release: 504fc3fcacfSRobert Watson if (control != NULL) 505a29f300eSGarrett Wollman m_freem(control); 506fc3fcacfSRobert Watson if (m != NULL) 507a29f300eSGarrett Wollman m_freem(m); 508e5aeaa0cSDag-Erling Smørgrav return (error); 509a29f300eSGarrett Wollman } 510df8bae1dSRodney W. Grimes 511a29f300eSGarrett Wollman static int 512a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 513a29f300eSGarrett Wollman { 51440f2ac28SRobert Watson struct unpcb *unp; 515a29f300eSGarrett Wollman struct socket *so2; 516a29f300eSGarrett Wollman 5170d9ce3a1SRobert Watson UNP_LOCK(); 51840f2ac28SRobert Watson unp = sotounpcb(so); 51940f2ac28SRobert Watson if (unp == NULL) { 52040f2ac28SRobert Watson UNP_UNLOCK(); 52140f2ac28SRobert Watson return (EINVAL); 52240f2ac28SRobert Watson } 523a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 524fc3fcacfSRobert Watson if (so->so_type == SOCK_STREAM && unp->unp_conn != NULL) { 525df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 526a29f300eSGarrett Wollman sb->st_blksize += so2->so_rcv.sb_cc; 527df8bae1dSRodney W. Grimes } 528f3732fd1SPoul-Henning Kamp sb->st_dev = NODEV; 529df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 5306f782c46SJeffrey Hsu unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 531a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 5320d9ce3a1SRobert Watson UNP_UNLOCK(); 533df8bae1dSRodney W. Grimes return (0); 534a29f300eSGarrett Wollman } 535df8bae1dSRodney W. Grimes 536a29f300eSGarrett Wollman static int 537a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 538a29f300eSGarrett Wollman { 53940f2ac28SRobert Watson struct unpcb *unp; 540df8bae1dSRodney W. Grimes 5410d9ce3a1SRobert Watson UNP_LOCK(); 54240f2ac28SRobert Watson unp = sotounpcb(so); 54340f2ac28SRobert Watson if (unp == NULL) { 54440f2ac28SRobert Watson UNP_UNLOCK(); 54540f2ac28SRobert Watson return (EINVAL); 54640f2ac28SRobert Watson } 547a29f300eSGarrett Wollman socantsendmore(so); 548a29f300eSGarrett Wollman unp_shutdown(unp); 5490d9ce3a1SRobert Watson UNP_UNLOCK(); 550e5aeaa0cSDag-Erling Smørgrav return (0); 551a29f300eSGarrett Wollman } 552df8bae1dSRodney W. Grimes 553a29f300eSGarrett Wollman static int 55457bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 555a29f300eSGarrett Wollman { 55640f2ac28SRobert Watson struct unpcb *unp; 5570d9ce3a1SRobert Watson const struct sockaddr *sa; 558a29f300eSGarrett Wollman 5590d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 5600d9ce3a1SRobert Watson UNP_LOCK(); 56140f2ac28SRobert Watson unp = sotounpcb(so); 56240f2ac28SRobert Watson if (unp == NULL) { 56340f2ac28SRobert Watson UNP_UNLOCK(); 56440f2ac28SRobert Watson free(*nam, M_SONAME); 56540f2ac28SRobert Watson *nam = NULL; 56640f2ac28SRobert Watson return (EINVAL); 56740f2ac28SRobert Watson } 568fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 5690d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_addr; 57083f3198bSThomas Moestl else 5710d9ce3a1SRobert Watson sa = &sun_noname; 5720d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 5730d9ce3a1SRobert Watson UNP_UNLOCK(); 574e5aeaa0cSDag-Erling Smørgrav return (0); 575df8bae1dSRodney W. Grimes } 576a29f300eSGarrett Wollman 577a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = { 578a29f300eSGarrett Wollman uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect, 579a29f300eSGarrett Wollman uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect, 580a29f300eSGarrett Wollman uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp, 581a29f300eSGarrett Wollman uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr, 582a557af22SRobert Watson sosend, soreceive, sopoll, pru_sosetlabel_null 583a29f300eSGarrett Wollman }; 584df8bae1dSRodney W. Grimes 5850c1bb4fbSDima Dorfman int 5860c1bb4fbSDima Dorfman uipc_ctloutput(so, sopt) 5870c1bb4fbSDima Dorfman struct socket *so; 5880c1bb4fbSDima Dorfman struct sockopt *sopt; 5890c1bb4fbSDima Dorfman { 59040f2ac28SRobert Watson struct unpcb *unp; 5910d9ce3a1SRobert Watson struct xucred xu; 5920c1bb4fbSDima Dorfman int error; 5930c1bb4fbSDima Dorfman 5940c1bb4fbSDima Dorfman switch (sopt->sopt_dir) { 5950c1bb4fbSDima Dorfman case SOPT_GET: 5960c1bb4fbSDima Dorfman switch (sopt->sopt_name) { 5970c1bb4fbSDima Dorfman case LOCAL_PEERCRED: 5980d9ce3a1SRobert Watson error = 0; 5990d9ce3a1SRobert Watson UNP_LOCK(); 60040f2ac28SRobert Watson unp = sotounpcb(so); 60140f2ac28SRobert Watson if (unp == NULL) { 60240f2ac28SRobert Watson UNP_UNLOCK(); 60340f2ac28SRobert Watson error = EINVAL; 60440f2ac28SRobert Watson break; 60540f2ac28SRobert Watson } 6060c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC) 6070d9ce3a1SRobert Watson xu = unp->unp_peercred; 6080c1bb4fbSDima Dorfman else { 6090c1bb4fbSDima Dorfman if (so->so_type == SOCK_STREAM) 6100c1bb4fbSDima Dorfman error = ENOTCONN; 6110c1bb4fbSDima Dorfman else 6120c1bb4fbSDima Dorfman error = EINVAL; 6130c1bb4fbSDima Dorfman } 6140d9ce3a1SRobert Watson UNP_UNLOCK(); 6150d9ce3a1SRobert Watson if (error == 0) 6160d9ce3a1SRobert Watson error = sooptcopyout(sopt, &xu, sizeof(xu)); 6170c1bb4fbSDima Dorfman break; 6180c1bb4fbSDima Dorfman default: 6190c1bb4fbSDima Dorfman error = EOPNOTSUPP; 6200c1bb4fbSDima Dorfman break; 6210c1bb4fbSDima Dorfman } 6220c1bb4fbSDima Dorfman break; 6230c1bb4fbSDima Dorfman case SOPT_SET: 6240c1bb4fbSDima Dorfman default: 6250c1bb4fbSDima Dorfman error = EOPNOTSUPP; 6260c1bb4fbSDima Dorfman break; 6270c1bb4fbSDima Dorfman } 6280c1bb4fbSDima Dorfman return (error); 6290c1bb4fbSDima Dorfman } 6300c1bb4fbSDima Dorfman 631df8bae1dSRodney W. Grimes /* 632df8bae1dSRodney W. Grimes * Both send and receive buffers are allocated PIPSIZ bytes of buffering 633df8bae1dSRodney W. Grimes * for stream sockets, although the total for sender and receiver is 634df8bae1dSRodney W. Grimes * actually only PIPSIZ. 635df8bae1dSRodney W. Grimes * Datagram sockets really use the sendspace as the maximum datagram size, 636df8bae1dSRodney W. Grimes * and don't really want to reserve the sendspace. Their recvspace should 637df8bae1dSRodney W. Grimes * be large enough for at least one max-size datagram plus address. 638df8bae1dSRodney W. Grimes */ 6395dce41c5SJohn Dyson #ifndef PIPSIZ 6405dce41c5SJohn Dyson #define PIPSIZ 8192 6415dce41c5SJohn Dyson #endif 642f708ef1bSPoul-Henning Kamp static u_long unpst_sendspace = PIPSIZ; 643f708ef1bSPoul-Henning Kamp static u_long unpst_recvspace = PIPSIZ; 644f708ef1bSPoul-Henning Kamp static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 645f708ef1bSPoul-Henning Kamp static u_long unpdg_recvspace = 4*1024; 646df8bae1dSRodney W. Grimes 647f708ef1bSPoul-Henning Kamp static int unp_rights; /* file descriptors in flight */ 648df8bae1dSRodney W. Grimes 649ce02431fSDoug Rabson SYSCTL_DECL(_net_local_stream); 650639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 651639acc13SGarrett Wollman &unpst_sendspace, 0, ""); 652639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 653639acc13SGarrett Wollman &unpst_recvspace, 0, ""); 654ce02431fSDoug Rabson SYSCTL_DECL(_net_local_dgram); 655639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 656639acc13SGarrett Wollman &unpdg_sendspace, 0, ""); 657639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 658639acc13SGarrett Wollman &unpdg_recvspace, 0, ""); 659ce02431fSDoug Rabson SYSCTL_DECL(_net_local); 660639acc13SGarrett Wollman SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); 661639acc13SGarrett Wollman 662f708ef1bSPoul-Henning Kamp static int 663df8bae1dSRodney W. Grimes unp_attach(so) 664df8bae1dSRodney W. Grimes struct socket *so; 665df8bae1dSRodney W. Grimes { 666df8bae1dSRodney W. Grimes register struct unpcb *unp; 667df8bae1dSRodney W. Grimes int error; 668df8bae1dSRodney W. Grimes 669df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 670df8bae1dSRodney W. Grimes switch (so->so_type) { 671df8bae1dSRodney W. Grimes 672df8bae1dSRodney W. Grimes case SOCK_STREAM: 673df8bae1dSRodney W. Grimes error = soreserve(so, unpst_sendspace, unpst_recvspace); 674df8bae1dSRodney W. Grimes break; 675df8bae1dSRodney W. Grimes 676df8bae1dSRodney W. Grimes case SOCK_DGRAM: 677df8bae1dSRodney W. Grimes error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 678df8bae1dSRodney W. Grimes break; 679df8bae1dSRodney W. Grimes 680df8bae1dSRodney W. Grimes default: 681df8bae1dSRodney W. Grimes panic("unp_attach"); 682df8bae1dSRodney W. Grimes } 683df8bae1dSRodney W. Grimes if (error) 684df8bae1dSRodney W. Grimes return (error); 685df8bae1dSRodney W. Grimes } 686a163d034SWarner Losh unp = uma_zalloc(unp_zone, M_WAITOK); 68757bf258eSGarrett Wollman if (unp == NULL) 688df8bae1dSRodney W. Grimes return (ENOBUFS); 68957bf258eSGarrett Wollman bzero(unp, sizeof *unp); 69098271db4SGarrett Wollman LIST_INIT(&unp->unp_refs); 691df8bae1dSRodney W. Grimes unp->unp_socket = so; 6920d9ce3a1SRobert Watson 6930d9ce3a1SRobert Watson UNP_LOCK(); 6940d9ce3a1SRobert Watson unp->unp_gencnt = ++unp_gencnt; 6950d9ce3a1SRobert Watson unp_count++; 69698271db4SGarrett Wollman LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead 69798271db4SGarrett Wollman : &unp_shead, unp, unp_link); 69840f2ac28SRobert Watson so->so_pcb = unp; 6990d9ce3a1SRobert Watson UNP_UNLOCK(); 7000d9ce3a1SRobert Watson 701df8bae1dSRodney W. Grimes return (0); 702df8bae1dSRodney W. Grimes } 703df8bae1dSRodney W. Grimes 704f708ef1bSPoul-Henning Kamp static void 705df8bae1dSRodney W. Grimes unp_detach(unp) 706df8bae1dSRodney W. Grimes register struct unpcb *unp; 707df8bae1dSRodney W. Grimes { 7080d9ce3a1SRobert Watson struct vnode *vp; 7090d9ce3a1SRobert Watson 7100d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 7110d9ce3a1SRobert Watson 71298271db4SGarrett Wollman LIST_REMOVE(unp, unp_link); 71398271db4SGarrett Wollman unp->unp_gencnt = ++unp_gencnt; 71498271db4SGarrett Wollman --unp_count; 7150d9ce3a1SRobert Watson if ((vp = unp->unp_vnode) != NULL) { 7160d9ce3a1SRobert Watson /* 7170d9ce3a1SRobert Watson * XXXRW: should v_socket be frobbed only while holding 7180d9ce3a1SRobert Watson * Giant? 7190d9ce3a1SRobert Watson */ 720fc3fcacfSRobert Watson unp->unp_vnode->v_socket = NULL; 721fc3fcacfSRobert Watson unp->unp_vnode = NULL; 722df8bae1dSRodney W. Grimes } 723fc3fcacfSRobert Watson if (unp->unp_conn != NULL) 724df8bae1dSRodney W. Grimes unp_disconnect(unp); 7250d9ce3a1SRobert Watson while (!LIST_EMPTY(&unp->unp_refs)) { 7260d9ce3a1SRobert Watson struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 7270d9ce3a1SRobert Watson unp_drop(ref, ECONNRESET); 7280d9ce3a1SRobert Watson } 729df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 730fc3fcacfSRobert Watson unp->unp_socket->so_pcb = NULL; 731df8bae1dSRodney W. Grimes if (unp_rights) { 732df8bae1dSRodney W. Grimes /* 733df8bae1dSRodney W. Grimes * Normally the receive buffer is flushed later, 734df8bae1dSRodney W. Grimes * in sofree, but if our receive buffer holds references 735df8bae1dSRodney W. Grimes * to descriptors that are now garbage, we will dispose 736df8bae1dSRodney W. Grimes * of those descriptor references after the garbage collector 737df8bae1dSRodney W. Grimes * gets them (resulting in a "panic: closef: count < 0"). 738df8bae1dSRodney W. Grimes */ 739df8bae1dSRodney W. Grimes sorflush(unp->unp_socket); 740161a0c7cSRobert Watson unp_gc(); /* Will unlock UNP. */ 741161a0c7cSRobert Watson } else 742a5993a97SRobert Watson UNP_UNLOCK(); 743161a0c7cSRobert Watson UNP_UNLOCK_ASSERT(); 744fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 74557bf258eSGarrett Wollman FREE(unp->unp_addr, M_SONAME); 7469e9d298aSJeff Roberson uma_zfree(unp_zone, unp); 7470d9ce3a1SRobert Watson if (vp) { 7480d9ce3a1SRobert Watson mtx_lock(&Giant); 7490d9ce3a1SRobert Watson vrele(vp); 7500d9ce3a1SRobert Watson mtx_unlock(&Giant); 7510d9ce3a1SRobert Watson } 752df8bae1dSRodney W. Grimes } 753df8bae1dSRodney W. Grimes 754f708ef1bSPoul-Henning Kamp static int 755b40ce416SJulian Elischer unp_bind(unp, nam, td) 756df8bae1dSRodney W. Grimes struct unpcb *unp; 75757bf258eSGarrett Wollman struct sockaddr *nam; 758b40ce416SJulian Elischer struct thread *td; 759df8bae1dSRodney W. Grimes { 76057bf258eSGarrett Wollman struct sockaddr_un *soun = (struct sockaddr_un *)nam; 761f2a2857bSKirk McKusick struct vnode *vp; 762f2a2857bSKirk McKusick struct mount *mp; 763df8bae1dSRodney W. Grimes struct vattr vattr; 76457bf258eSGarrett Wollman int error, namelen; 765df8bae1dSRodney W. Grimes struct nameidata nd; 7668f364875SJulian Elischer char *buf; 767df8bae1dSRodney W. Grimes 76840f2ac28SRobert Watson UNP_LOCK_ASSERT(); 76940f2ac28SRobert Watson 7700d9ce3a1SRobert Watson /* 7710d9ce3a1SRobert Watson * XXXRW: This test-and-set of unp_vnode is non-atomic; the 7720d9ce3a1SRobert Watson * unlocked read here is fine, but the value of unp_vnode needs 7730d9ce3a1SRobert Watson * to be tested again after we do all the lookups to see if the 7740d9ce3a1SRobert Watson * pcb is still unbound? 7750d9ce3a1SRobert Watson */ 776df8bae1dSRodney W. Grimes if (unp->unp_vnode != NULL) 777df8bae1dSRodney W. Grimes return (EINVAL); 77855c85568SRobert Drehmel 77957bf258eSGarrett Wollman namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 78057bf258eSGarrett Wollman if (namelen <= 0) 781e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 78255c85568SRobert Drehmel 78340f2ac28SRobert Watson UNP_UNLOCK(); 78440f2ac28SRobert Watson 785a163d034SWarner Losh buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 78655c85568SRobert Drehmel strlcpy(buf, soun->sun_path, namelen + 1); 78755c85568SRobert Drehmel 7880d9ce3a1SRobert Watson mtx_lock(&Giant); 789f2a2857bSKirk McKusick restart: 7900d9ce3a1SRobert Watson mtx_assert(&Giant, MA_OWNED); 791b65f6f6bSRobert Watson NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME, UIO_SYSSPACE, 792b40ce416SJulian Elischer buf, td); 793df8bae1dSRodney W. Grimes /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 794797f2d22SPoul-Henning Kamp error = namei(&nd); 7950d9ce3a1SRobert Watson if (error) 7960d9ce3a1SRobert Watson goto done; 797df8bae1dSRodney W. Grimes vp = nd.ni_vp; 798f2a2857bSKirk McKusick if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 799762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 800df8bae1dSRodney W. Grimes if (nd.ni_dvp == vp) 801df8bae1dSRodney W. Grimes vrele(nd.ni_dvp); 802df8bae1dSRodney W. Grimes else 803df8bae1dSRodney W. Grimes vput(nd.ni_dvp); 804f2a2857bSKirk McKusick if (vp != NULL) { 805df8bae1dSRodney W. Grimes vrele(vp); 8060d9ce3a1SRobert Watson error = EADDRINUSE; 8070d9ce3a1SRobert Watson goto done; 808df8bae1dSRodney W. Grimes } 8098f364875SJulian Elischer error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 8100d9ce3a1SRobert Watson if (error) 8110d9ce3a1SRobert Watson goto done; 812f2a2857bSKirk McKusick goto restart; 813f2a2857bSKirk McKusick } 814df8bae1dSRodney W. Grimes VATTR_NULL(&vattr); 815df8bae1dSRodney W. Grimes vattr.va_type = VSOCK; 816b40ce416SJulian Elischer vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 8176ea48a90SRobert Watson #ifdef MAC 8186ea48a90SRobert Watson error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 8196ea48a90SRobert Watson &vattr); 8206151efaaSRobert Watson #endif 8216ea48a90SRobert Watson if (error == 0) { 822a854ed98SJohn Baldwin VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE); 8237be2d300SMike Smith error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 8246ea48a90SRobert Watson } 825762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 8267be2d300SMike Smith vput(nd.ni_dvp); 8270d9ce3a1SRobert Watson if (error) 8280d9ce3a1SRobert Watson goto done; 829df8bae1dSRodney W. Grimes vp = nd.ni_vp; 8300d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_bind"); 8310d9ce3a1SRobert Watson soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 8320d9ce3a1SRobert Watson UNP_LOCK(); 833df8bae1dSRodney W. Grimes vp->v_socket = unp->unp_socket; 834df8bae1dSRodney W. Grimes unp->unp_vnode = vp; 8350d9ce3a1SRobert Watson unp->unp_addr = soun; 8360d9ce3a1SRobert Watson UNP_UNLOCK(); 837b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 838f2a2857bSKirk McKusick vn_finished_write(mp); 8390d9ce3a1SRobert Watson done: 8400d9ce3a1SRobert Watson mtx_unlock(&Giant); 8418f364875SJulian Elischer free(buf, M_TEMP); 84240f2ac28SRobert Watson UNP_LOCK(); 8430d9ce3a1SRobert Watson return (error); 844df8bae1dSRodney W. Grimes } 845df8bae1dSRodney W. Grimes 846f708ef1bSPoul-Henning Kamp static int 847b40ce416SJulian Elischer unp_connect(so, nam, td) 848df8bae1dSRodney W. Grimes struct socket *so; 84957bf258eSGarrett Wollman struct sockaddr *nam; 850b40ce416SJulian Elischer struct thread *td; 851df8bae1dSRodney W. Grimes { 85257bf258eSGarrett Wollman register struct sockaddr_un *soun = (struct sockaddr_un *)nam; 853df8bae1dSRodney W. Grimes register struct vnode *vp; 854df8bae1dSRodney W. Grimes register struct socket *so2, *so3; 855b295bdcdSRobert Watson struct unpcb *unp, *unp2, *unp3; 85657bf258eSGarrett Wollman int error, len; 857df8bae1dSRodney W. Grimes struct nameidata nd; 85857bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 8590d9ce3a1SRobert Watson struct sockaddr *sa; 8600d9ce3a1SRobert Watson 8610d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 862b295bdcdSRobert Watson unp = sotounpcb(so); 863df8bae1dSRodney W. Grimes 86457bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 86557bf258eSGarrett Wollman if (len <= 0) 866e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 86755c85568SRobert Drehmel strlcpy(buf, soun->sun_path, len + 1); 8680d9ce3a1SRobert Watson UNP_UNLOCK(); 8690d9ce3a1SRobert Watson sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 8700d9ce3a1SRobert Watson mtx_lock(&Giant); 871b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td); 872797f2d22SPoul-Henning Kamp error = namei(&nd); 873797f2d22SPoul-Henning Kamp if (error) 8740d9ce3a1SRobert Watson vp = NULL; 8750d9ce3a1SRobert Watson else 876df8bae1dSRodney W. Grimes vp = nd.ni_vp; 8770d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_connect"); 878762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 8790d9ce3a1SRobert Watson if (error) 8800d9ce3a1SRobert Watson goto bad; 8810d9ce3a1SRobert Watson 882df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 883df8bae1dSRodney W. Grimes error = ENOTSOCK; 884df8bae1dSRodney W. Grimes goto bad; 885df8bae1dSRodney W. Grimes } 886a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 887797f2d22SPoul-Henning Kamp if (error) 888df8bae1dSRodney W. Grimes goto bad; 8892260c03dSRobert Watson mtx_unlock(&Giant); 8902260c03dSRobert Watson UNP_LOCK(); 891b295bdcdSRobert Watson unp = sotounpcb(so); 892b295bdcdSRobert Watson if (unp == NULL) { 893b295bdcdSRobert Watson /* 894b295bdcdSRobert Watson * XXXRW: Temporary debugging printf. 895b295bdcdSRobert Watson */ 896b295bdcdSRobert Watson printf("unp_connect(): lost race to another thread\n"); 897b295bdcdSRobert Watson error = EINVAL; 898b295bdcdSRobert Watson goto bad2; 899b295bdcdSRobert Watson } 900df8bae1dSRodney W. Grimes so2 = vp->v_socket; 901fc3fcacfSRobert Watson if (so2 == NULL) { 902df8bae1dSRodney W. Grimes error = ECONNREFUSED; 9032260c03dSRobert Watson goto bad2; 904df8bae1dSRodney W. Grimes } 905df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 906df8bae1dSRodney W. Grimes error = EPROTOTYPE; 9072260c03dSRobert Watson goto bad2; 908df8bae1dSRodney W. Grimes } 909df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 9100d9ce3a1SRobert Watson if (so2->so_options & SO_ACCEPTCONN) { 9110d9ce3a1SRobert Watson /* 9120d9ce3a1SRobert Watson * NB: drop locks here so unp_attach is entered 9130d9ce3a1SRobert Watson * w/o locks; this avoids a recursive lock 9140d9ce3a1SRobert Watson * of the head and holding sleep locks across 9150d9ce3a1SRobert Watson * a (potentially) blocking malloc. 9160d9ce3a1SRobert Watson */ 9170d9ce3a1SRobert Watson UNP_UNLOCK(); 9180d9ce3a1SRobert Watson so3 = sonewconn(so2, 0); 9190d9ce3a1SRobert Watson UNP_LOCK(); 9200d9ce3a1SRobert Watson } else 9210d9ce3a1SRobert Watson so3 = NULL; 9220d9ce3a1SRobert Watson if (so3 == NULL) { 923df8bae1dSRodney W. Grimes error = ECONNREFUSED; 9240d9ce3a1SRobert Watson goto bad2; 925df8bae1dSRodney W. Grimes } 9260c1bb4fbSDima Dorfman unp = sotounpcb(so); 927df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 928df8bae1dSRodney W. Grimes unp3 = sotounpcb(so3); 9290d9ce3a1SRobert Watson if (unp2->unp_addr != NULL) { 9300d9ce3a1SRobert Watson bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 9310d9ce3a1SRobert Watson unp3->unp_addr = (struct sockaddr_un *) sa; 9320d9ce3a1SRobert Watson sa = NULL; 9330d9ce3a1SRobert Watson } 9340c1bb4fbSDima Dorfman /* 9350c1bb4fbSDima Dorfman * unp_peercred management: 9360c1bb4fbSDima Dorfman * 9370c1bb4fbSDima Dorfman * The connecter's (client's) credentials are copied 9380c1bb4fbSDima Dorfman * from its process structure at the time of connect() 9390c1bb4fbSDima Dorfman * (which is now). 9400c1bb4fbSDima Dorfman */ 941a854ed98SJohn Baldwin cru2x(td->td_ucred, &unp3->unp_peercred); 9420c1bb4fbSDima Dorfman unp3->unp_flags |= UNP_HAVEPC; 9430c1bb4fbSDima Dorfman /* 9440c1bb4fbSDima Dorfman * The receiver's (server's) credentials are copied 9450c1bb4fbSDima Dorfman * from the unp_peercred member of socket on which the 9460c1bb4fbSDima Dorfman * former called listen(); unp_listen() cached that 9470c1bb4fbSDima Dorfman * process's credentials at that time so we can use 9480c1bb4fbSDima Dorfman * them now. 9490c1bb4fbSDima Dorfman */ 9500c1bb4fbSDima Dorfman KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 9510c1bb4fbSDima Dorfman ("unp_connect: listener without cached peercred")); 9520c1bb4fbSDima Dorfman memcpy(&unp->unp_peercred, &unp2->unp_peercred, 9530c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 9540c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPC; 955335654d7SRobert Watson #ifdef MAC 956310e7cebSRobert Watson SOCK_LOCK(so); 957335654d7SRobert Watson mac_set_socket_peer_from_socket(so, so3); 958335654d7SRobert Watson mac_set_socket_peer_from_socket(so3, so); 959310e7cebSRobert Watson SOCK_UNLOCK(so); 960335654d7SRobert Watson #endif 9610c1bb4fbSDima Dorfman 962df8bae1dSRodney W. Grimes so2 = so3; 963df8bae1dSRodney W. Grimes } 964df8bae1dSRodney W. Grimes error = unp_connect2(so, so2); 9650d9ce3a1SRobert Watson bad2: 9660d9ce3a1SRobert Watson UNP_UNLOCK(); 9670d9ce3a1SRobert Watson mtx_lock(&Giant); 968df8bae1dSRodney W. Grimes bad: 9690d9ce3a1SRobert Watson mtx_assert(&Giant, MA_OWNED); 9700d9ce3a1SRobert Watson if (vp != NULL) 971df8bae1dSRodney W. Grimes vput(vp); 9720d9ce3a1SRobert Watson mtx_unlock(&Giant); 9730d9ce3a1SRobert Watson free(sa, M_SONAME); 9740d9ce3a1SRobert Watson UNP_LOCK(); 975df8bae1dSRodney W. Grimes return (error); 976df8bae1dSRodney W. Grimes } 977df8bae1dSRodney W. Grimes 978db48c0d2SRobert Watson static int 979df8bae1dSRodney W. Grimes unp_connect2(so, so2) 980df8bae1dSRodney W. Grimes register struct socket *so; 981df8bae1dSRodney W. Grimes register struct socket *so2; 982df8bae1dSRodney W. Grimes { 983df8bae1dSRodney W. Grimes register struct unpcb *unp = sotounpcb(so); 984df8bae1dSRodney W. Grimes register 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 1012df8bae1dSRodney W. Grimes unp_disconnect(unp) 1013df8bae1dSRodney W. Grimes struct unpcb *unp; 1014df8bae1dSRodney W. Grimes { 1015df8bae1dSRodney W. Grimes register struct unpcb *unp2 = unp->unp_conn; 10161b2e3b4bSRobert Watson struct socket *so; 1017df8bae1dSRodney W. Grimes 10180d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 10190d9ce3a1SRobert Watson 1020fc3fcacfSRobert Watson if (unp2 == NULL) 1021df8bae1dSRodney W. Grimes return; 1022fc3fcacfSRobert Watson unp->unp_conn = NULL; 1023df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 1024df8bae1dSRodney W. Grimes 1025df8bae1dSRodney W. Grimes case SOCK_DGRAM: 102698271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 10271b2e3b4bSRobert Watson so = unp->unp_socket; 10281b2e3b4bSRobert Watson SOCK_LOCK(so); 10291b2e3b4bSRobert Watson so->so_state &= ~SS_ISCONNECTED; 10301b2e3b4bSRobert Watson SOCK_UNLOCK(so); 1031df8bae1dSRodney W. Grimes break; 1032df8bae1dSRodney W. Grimes 1033df8bae1dSRodney W. Grimes case SOCK_STREAM: 1034df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 1035fc3fcacfSRobert Watson unp2->unp_conn = NULL; 1036df8bae1dSRodney W. Grimes soisdisconnected(unp2->unp_socket); 1037df8bae1dSRodney W. Grimes break; 1038df8bae1dSRodney W. Grimes } 1039df8bae1dSRodney W. Grimes } 1040df8bae1dSRodney W. Grimes 1041df8bae1dSRodney W. Grimes #ifdef notdef 104226f9a767SRodney W. Grimes void 1043df8bae1dSRodney W. Grimes unp_abort(unp) 1044df8bae1dSRodney W. Grimes struct unpcb *unp; 1045df8bae1dSRodney W. Grimes { 1046df8bae1dSRodney W. Grimes 1047df8bae1dSRodney W. Grimes unp_detach(unp); 10484c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 1049df8bae1dSRodney W. Grimes } 1050df8bae1dSRodney W. Grimes #endif 1051df8bae1dSRodney W. Grimes 10520d9ce3a1SRobert Watson /* 10530d9ce3a1SRobert Watson * unp_pcblist() assumes that UNIX domain socket memory is never reclaimed 10540d9ce3a1SRobert Watson * by the zone (UMA_ZONE_NOFREE), and as such potentially stale pointers 10550d9ce3a1SRobert Watson * are safe to reference. It first scans the list of struct unpcb's to 10560d9ce3a1SRobert Watson * generate a pointer list, then it rescans its list one entry at a time to 10570d9ce3a1SRobert Watson * externalize and copyout. It checks the generation number to see if a 10580d9ce3a1SRobert Watson * struct unpcb has been reused, and will skip it if so. 10590d9ce3a1SRobert Watson */ 106098271db4SGarrett Wollman static int 106182d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 106298271db4SGarrett Wollman { 1063f5ef029eSPoul-Henning Kamp int error, i, n; 106498271db4SGarrett Wollman struct unpcb *unp, **unp_list; 106598271db4SGarrett Wollman unp_gen_t gencnt; 10668f364875SJulian Elischer struct xunpgen *xug; 106798271db4SGarrett Wollman struct unp_head *head; 10688f364875SJulian Elischer struct xunpcb *xu; 106998271db4SGarrett Wollman 1070a23d65bfSBruce Evans head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 107198271db4SGarrett Wollman 107298271db4SGarrett Wollman /* 107398271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 107498271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 107598271db4SGarrett Wollman */ 1076fc3fcacfSRobert Watson if (req->oldptr == NULL) { 107798271db4SGarrett Wollman n = unp_count; 10788f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug) 107998271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 1080e5aeaa0cSDag-Erling Smørgrav return (0); 108198271db4SGarrett Wollman } 108298271db4SGarrett Wollman 1083fc3fcacfSRobert Watson if (req->newptr != NULL) 1084e5aeaa0cSDag-Erling Smørgrav return (EPERM); 108598271db4SGarrett Wollman 108698271db4SGarrett Wollman /* 108798271db4SGarrett Wollman * OK, now we're committed to doing something. 108898271db4SGarrett Wollman */ 1089a163d034SWarner Losh xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 10900d9ce3a1SRobert Watson UNP_LOCK(); 109198271db4SGarrett Wollman gencnt = unp_gencnt; 109298271db4SGarrett Wollman n = unp_count; 10930d9ce3a1SRobert Watson UNP_UNLOCK(); 109498271db4SGarrett Wollman 10958f364875SJulian Elischer xug->xug_len = sizeof *xug; 10968f364875SJulian Elischer xug->xug_count = n; 10978f364875SJulian Elischer xug->xug_gen = gencnt; 10988f364875SJulian Elischer xug->xug_sogen = so_gencnt; 10998f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 11008f364875SJulian Elischer if (error) { 11018f364875SJulian Elischer free(xug, M_TEMP); 1102e5aeaa0cSDag-Erling Smørgrav return (error); 11038f364875SJulian Elischer } 110498271db4SGarrett Wollman 1105a163d034SWarner Losh unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 110698271db4SGarrett Wollman 11070d9ce3a1SRobert Watson UNP_LOCK(); 11082e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 11092e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 11108a7d8cc6SRobert Watson if (unp->unp_gencnt <= gencnt) { 1111a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred, 11128a7d8cc6SRobert Watson unp->unp_socket->so_cred)) 11134787fd37SPaul Saab continue; 111498271db4SGarrett Wollman unp_list[i++] = unp; 111598271db4SGarrett Wollman } 11164787fd37SPaul Saab } 11170d9ce3a1SRobert Watson UNP_UNLOCK(); 111898271db4SGarrett Wollman n = i; /* in case we lost some during malloc */ 111998271db4SGarrett Wollman 112098271db4SGarrett Wollman error = 0; 1121a163d034SWarner Losh xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK); 112298271db4SGarrett Wollman for (i = 0; i < n; i++) { 112398271db4SGarrett Wollman unp = unp_list[i]; 112498271db4SGarrett Wollman if (unp->unp_gencnt <= gencnt) { 11258f364875SJulian Elischer xu->xu_len = sizeof *xu; 11268f364875SJulian Elischer xu->xu_unpp = unp; 112798271db4SGarrett Wollman /* 112898271db4SGarrett Wollman * XXX - need more locking here to protect against 112998271db4SGarrett Wollman * connect/disconnect races for SMP. 113098271db4SGarrett Wollman */ 1131fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 11328f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr, 113398271db4SGarrett Wollman unp->unp_addr->sun_len); 1134fc3fcacfSRobert Watson if (unp->unp_conn != NULL && 1135fc3fcacfSRobert Watson unp->unp_conn->unp_addr != NULL) 113698271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 11378f364875SJulian Elischer &xu->xu_caddr, 113898271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 11398f364875SJulian Elischer bcopy(unp, &xu->xu_unp, sizeof *unp); 11408f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket); 11418f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu); 114298271db4SGarrett Wollman } 114398271db4SGarrett Wollman } 11448f364875SJulian Elischer free(xu, M_TEMP); 114598271db4SGarrett Wollman if (!error) { 114698271db4SGarrett Wollman /* 114798271db4SGarrett Wollman * Give the user an updated idea of our state. 114898271db4SGarrett Wollman * If the generation differs from what we told 114998271db4SGarrett Wollman * her before, she knows that something happened 115098271db4SGarrett Wollman * while we were processing this request, and it 115198271db4SGarrett Wollman * might be necessary to retry. 115298271db4SGarrett Wollman */ 11538f364875SJulian Elischer xug->xug_gen = unp_gencnt; 11548f364875SJulian Elischer xug->xug_sogen = so_gencnt; 11558f364875SJulian Elischer xug->xug_count = unp_count; 11568f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 115798271db4SGarrett Wollman } 115898271db4SGarrett Wollman free(unp_list, M_TEMP); 11598f364875SJulian Elischer free(xug, M_TEMP); 1160e5aeaa0cSDag-Erling Smørgrav return (error); 116198271db4SGarrett Wollman } 116298271db4SGarrett Wollman 116398271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 116498271db4SGarrett Wollman (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 116598271db4SGarrett Wollman "List of active local datagram sockets"); 116698271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 116798271db4SGarrett Wollman (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 116898271db4SGarrett Wollman "List of active local stream sockets"); 116998271db4SGarrett Wollman 1170f708ef1bSPoul-Henning Kamp static void 1171df8bae1dSRodney W. Grimes unp_shutdown(unp) 1172df8bae1dSRodney W. Grimes struct unpcb *unp; 1173df8bae1dSRodney W. Grimes { 1174df8bae1dSRodney W. Grimes struct socket *so; 1175df8bae1dSRodney W. Grimes 11760d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 11770d9ce3a1SRobert Watson 1178df8bae1dSRodney W. Grimes if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 1179df8bae1dSRodney W. Grimes (so = unp->unp_conn->unp_socket)) 1180df8bae1dSRodney W. Grimes socantrcvmore(so); 1181df8bae1dSRodney W. Grimes } 1182df8bae1dSRodney W. Grimes 1183f708ef1bSPoul-Henning Kamp static void 1184df8bae1dSRodney W. Grimes unp_drop(unp, errno) 1185df8bae1dSRodney W. Grimes struct unpcb *unp; 1186df8bae1dSRodney W. Grimes int errno; 1187df8bae1dSRodney W. Grimes { 1188df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 1189df8bae1dSRodney W. Grimes 11900d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 11910d9ce3a1SRobert Watson 1192df8bae1dSRodney W. Grimes so->so_error = errno; 1193df8bae1dSRodney W. Grimes unp_disconnect(unp); 1194df8bae1dSRodney W. Grimes } 1195df8bae1dSRodney W. Grimes 1196df8bae1dSRodney W. Grimes #ifdef notdef 119726f9a767SRodney W. Grimes void 1198df8bae1dSRodney W. Grimes unp_drain() 1199df8bae1dSRodney W. Grimes { 1200df8bae1dSRodney W. Grimes 1201df8bae1dSRodney W. Grimes } 1202df8bae1dSRodney W. Grimes #endif 1203df8bae1dSRodney W. Grimes 12042bc21ed9SDavid Malone static void 12052bc21ed9SDavid Malone unp_freerights(rp, fdcount) 12062bc21ed9SDavid Malone struct file **rp; 12072bc21ed9SDavid Malone int fdcount; 1208df8bae1dSRodney W. Grimes { 12092bc21ed9SDavid Malone int i; 12102bc21ed9SDavid Malone struct file *fp; 1211df8bae1dSRodney W. Grimes 12122bc21ed9SDavid Malone for (i = 0; i < fdcount; i++) { 1213df8bae1dSRodney W. Grimes fp = *rp; 12148692c025SYoshinobu Inoue /* 12152bc21ed9SDavid Malone * zero the pointer before calling 12162bc21ed9SDavid Malone * unp_discard since it may end up 12172bc21ed9SDavid Malone * in unp_gc().. 12188692c025SYoshinobu Inoue */ 1219df8bae1dSRodney W. Grimes *rp++ = 0; 12208692c025SYoshinobu Inoue unp_discard(fp); 1221df8bae1dSRodney W. Grimes } 12222bc21ed9SDavid Malone } 12232bc21ed9SDavid Malone 12242bc21ed9SDavid Malone int 12252bc21ed9SDavid Malone unp_externalize(control, controlp) 12262bc21ed9SDavid Malone struct mbuf *control, **controlp; 12272bc21ed9SDavid Malone { 12282bc21ed9SDavid Malone struct thread *td = curthread; /* XXX */ 12292bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 12302bc21ed9SDavid Malone int i; 12312bc21ed9SDavid Malone int *fdp; 12322bc21ed9SDavid Malone struct file **rp; 12332bc21ed9SDavid Malone struct file *fp; 12342bc21ed9SDavid Malone void *data; 12352bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 12362bc21ed9SDavid Malone int error, newfds; 12372bc21ed9SDavid Malone int f; 12382bc21ed9SDavid Malone u_int newlen; 12392bc21ed9SDavid Malone 12404c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 12414c5bc1caSRobert Watson 12422bc21ed9SDavid Malone error = 0; 12432bc21ed9SDavid Malone if (controlp != NULL) /* controlp == NULL => free control messages */ 12442bc21ed9SDavid Malone *controlp = NULL; 12452bc21ed9SDavid Malone 12462bc21ed9SDavid Malone while (cm != NULL) { 12472bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 12482bc21ed9SDavid Malone error = EINVAL; 12492bc21ed9SDavid Malone break; 12502bc21ed9SDavid Malone } 12512bc21ed9SDavid Malone 12522bc21ed9SDavid Malone data = CMSG_DATA(cm); 12532bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 12542bc21ed9SDavid Malone 12552bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET 12562bc21ed9SDavid Malone && cm->cmsg_type == SCM_RIGHTS) { 12572bc21ed9SDavid Malone newfds = datalen / sizeof(struct file *); 12582bc21ed9SDavid Malone rp = data; 12592bc21ed9SDavid Malone 1260e2f9a08bSOlivier Houchard /* If we're not outputting the descriptors free them. */ 12612bc21ed9SDavid Malone if (error || controlp == NULL) { 12622bc21ed9SDavid Malone unp_freerights(rp, newfds); 12632bc21ed9SDavid Malone goto next; 12642bc21ed9SDavid Malone } 1265426da3bcSAlfred Perlstein FILEDESC_LOCK(td->td_proc->p_fd); 12662bc21ed9SDavid Malone /* if the new FD's will not fit free them. */ 12672bc21ed9SDavid Malone if (!fdavail(td, newfds)) { 1268426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 12692bc21ed9SDavid Malone error = EMSGSIZE; 12702bc21ed9SDavid Malone unp_freerights(rp, newfds); 12712bc21ed9SDavid Malone goto next; 1272df8bae1dSRodney W. Grimes } 1273ed5b7817SJulian Elischer /* 12742bc21ed9SDavid Malone * now change each pointer to an fd in the global 12752bc21ed9SDavid Malone * table to an integer that is the index to the 12762bc21ed9SDavid Malone * local fd table entry that we set up to point 12772bc21ed9SDavid Malone * to the global one we are transferring. 1278ed5b7817SJulian Elischer */ 12792bc21ed9SDavid Malone newlen = newfds * sizeof(int); 12802bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 12812bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 12822bc21ed9SDavid Malone if (*controlp == NULL) { 1283426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 12842bc21ed9SDavid Malone error = E2BIG; 12852bc21ed9SDavid Malone unp_freerights(rp, newfds); 12862bc21ed9SDavid Malone goto next; 12872bc21ed9SDavid Malone } 12882bc21ed9SDavid Malone 12892bc21ed9SDavid Malone fdp = (int *) 12902bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1291df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 1292a6d4491cSDag-Erling Smørgrav if (fdalloc(td, 0, &f)) 12932bc21ed9SDavid Malone panic("unp_externalize fdalloc failed"); 12948692c025SYoshinobu Inoue fp = *rp++; 1295b40ce416SJulian Elischer td->td_proc->p_fd->fd_ofiles[f] = fp; 1296426da3bcSAlfred Perlstein FILE_LOCK(fp); 1297df8bae1dSRodney W. Grimes fp->f_msgcount--; 1298426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1299df8bae1dSRodney W. Grimes unp_rights--; 13008692c025SYoshinobu Inoue *fdp++ = f; 1301df8bae1dSRodney W. Grimes } 1302426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 13032bc21ed9SDavid Malone } else { /* We can just copy anything else across */ 13042bc21ed9SDavid Malone if (error || controlp == NULL) 13052bc21ed9SDavid Malone goto next; 13062bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, datalen, 13072bc21ed9SDavid Malone cm->cmsg_type, cm->cmsg_level); 13082bc21ed9SDavid Malone if (*controlp == NULL) { 13092bc21ed9SDavid Malone error = ENOBUFS; 13102bc21ed9SDavid Malone goto next; 13112bc21ed9SDavid Malone } 13122bc21ed9SDavid Malone bcopy(data, 13132bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 13142bc21ed9SDavid Malone datalen); 13152bc21ed9SDavid Malone } 13162bc21ed9SDavid Malone 13172bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 13182bc21ed9SDavid Malone 13192bc21ed9SDavid Malone next: 13202bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 13212bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 13222bc21ed9SDavid Malone cm = (struct cmsghdr *) 13232bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 13248692c025SYoshinobu Inoue } else { 13252bc21ed9SDavid Malone clen = 0; 13262bc21ed9SDavid Malone cm = NULL; 13278692c025SYoshinobu Inoue } 13288692c025SYoshinobu Inoue } 13298692c025SYoshinobu Inoue 13302bc21ed9SDavid Malone m_freem(control); 13312bc21ed9SDavid Malone 13322bc21ed9SDavid Malone return (error); 1333df8bae1dSRodney W. Grimes } 1334df8bae1dSRodney W. Grimes 133598271db4SGarrett Wollman void 133698271db4SGarrett Wollman unp_init(void) 133798271db4SGarrett Wollman { 13389e9d298aSJeff Roberson unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 13399e9d298aSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 1340fc3fcacfSRobert Watson if (unp_zone == NULL) 134198271db4SGarrett Wollman panic("unp_init"); 1342b17dd2bcSColin Percival uma_zone_set_max(unp_zone, nmbclusters); 134398271db4SGarrett Wollman LIST_INIT(&unp_dhead); 134498271db4SGarrett Wollman LIST_INIT(&unp_shead); 13450d9ce3a1SRobert Watson 13460d9ce3a1SRobert Watson UNP_LOCK_INIT(); 134798271db4SGarrett Wollman } 134898271db4SGarrett Wollman 1349f708ef1bSPoul-Henning Kamp static int 13502bc21ed9SDavid Malone unp_internalize(controlp, td) 13512bc21ed9SDavid Malone struct mbuf **controlp; 1352b40ce416SJulian Elischer struct thread *td; 1353df8bae1dSRodney W. Grimes { 13542bc21ed9SDavid Malone struct mbuf *control = *controlp; 1355b40ce416SJulian Elischer struct proc *p = td->td_proc; 13568692c025SYoshinobu Inoue struct filedesc *fdescp = p->p_fd; 13572bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 13582bc21ed9SDavid Malone struct cmsgcred *cmcred; 13592bc21ed9SDavid Malone struct file **rp; 13602bc21ed9SDavid Malone struct file *fp; 13612bc21ed9SDavid Malone struct timeval *tv; 13622bc21ed9SDavid Malone int i, fd, *fdp; 13632bc21ed9SDavid Malone void *data; 13642bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 13652bc21ed9SDavid Malone int error, oldfds; 13668692c025SYoshinobu Inoue u_int newlen; 1367df8bae1dSRodney W. Grimes 13684c5bc1caSRobert Watson UNP_UNLOCK_ASSERT(); 13694c5bc1caSRobert Watson 13702bc21ed9SDavid Malone error = 0; 13712bc21ed9SDavid Malone *controlp = NULL; 13720b788fa1SBill Paul 13732bc21ed9SDavid Malone while (cm != NULL) { 13742bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 13752bc21ed9SDavid Malone || cm->cmsg_len > clen) { 13762bc21ed9SDavid Malone error = EINVAL; 13772bc21ed9SDavid Malone goto out; 13782bc21ed9SDavid Malone } 13792bc21ed9SDavid Malone 13802bc21ed9SDavid Malone data = CMSG_DATA(cm); 13812bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 13822bc21ed9SDavid Malone 13832bc21ed9SDavid Malone switch (cm->cmsg_type) { 13840b788fa1SBill Paul /* 13850b788fa1SBill Paul * Fill in credential information. 13860b788fa1SBill Paul */ 13872bc21ed9SDavid Malone case SCM_CREDS: 13882bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 13892bc21ed9SDavid Malone SCM_CREDS, SOL_SOCKET); 13902bc21ed9SDavid Malone if (*controlp == NULL) { 13912bc21ed9SDavid Malone error = ENOBUFS; 13922bc21ed9SDavid Malone goto out; 13932bc21ed9SDavid Malone } 13942bc21ed9SDavid Malone 13952bc21ed9SDavid Malone cmcred = (struct cmsgcred *) 13962bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 13970b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 1398a854ed98SJohn Baldwin cmcred->cmcred_uid = td->td_ucred->cr_ruid; 1399a854ed98SJohn Baldwin cmcred->cmcred_gid = td->td_ucred->cr_rgid; 1400a854ed98SJohn Baldwin cmcred->cmcred_euid = td->td_ucred->cr_uid; 1401a854ed98SJohn Baldwin cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 14020b788fa1SBill Paul CMGROUP_MAX); 14030b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 14042bc21ed9SDavid Malone cmcred->cmcred_groups[i] = 1405a854ed98SJohn Baldwin td->td_ucred->cr_groups[i]; 14062bc21ed9SDavid Malone break; 14070b788fa1SBill Paul 14082bc21ed9SDavid Malone case SCM_RIGHTS: 14092bc21ed9SDavid Malone oldfds = datalen / sizeof (int); 1410ed5b7817SJulian Elischer /* 14112bc21ed9SDavid Malone * check that all the FDs passed in refer to legal files 1412ed5b7817SJulian Elischer * If not, reject the entire operation. 1413ed5b7817SJulian Elischer */ 14142bc21ed9SDavid Malone fdp = data; 1415426da3bcSAlfred Perlstein FILEDESC_LOCK(fdescp); 1416df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 14178692c025SYoshinobu Inoue fd = *fdp++; 14188692c025SYoshinobu Inoue if ((unsigned)fd >= fdescp->fd_nfiles || 14192bc21ed9SDavid Malone fdescp->fd_ofiles[fd] == NULL) { 1420426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 14212bc21ed9SDavid Malone error = EBADF; 14222bc21ed9SDavid Malone goto out; 14232bc21ed9SDavid Malone } 1424e7d6662fSAlfred Perlstein fp = fdescp->fd_ofiles[fd]; 1425e7d6662fSAlfred Perlstein if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 1426e7d6662fSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 1427e7d6662fSAlfred Perlstein error = EOPNOTSUPP; 1428e7d6662fSAlfred Perlstein goto out; 1429e7d6662fSAlfred Perlstein } 1430e7d6662fSAlfred Perlstein 1431df8bae1dSRodney W. Grimes } 1432ed5b7817SJulian Elischer /* 1433ed5b7817SJulian Elischer * Now replace the integer FDs with pointers to 1434ed5b7817SJulian Elischer * the associated global file table entry.. 1435ed5b7817SJulian Elischer */ 14362bc21ed9SDavid Malone newlen = oldfds * sizeof(struct file *); 14372bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 14382bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 14392bc21ed9SDavid Malone if (*controlp == NULL) { 1440426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 14412bc21ed9SDavid Malone error = E2BIG; 14422bc21ed9SDavid Malone goto out; 14438692c025SYoshinobu Inoue } 14448692c025SYoshinobu Inoue 14452bc21ed9SDavid Malone fdp = data; 14462bc21ed9SDavid Malone rp = (struct file **) 14472bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 14488692c025SYoshinobu Inoue for (i = 0; i < oldfds; i++) { 14498692c025SYoshinobu Inoue fp = fdescp->fd_ofiles[*fdp++]; 1450df8bae1dSRodney W. Grimes *rp++ = fp; 1451426da3bcSAlfred Perlstein FILE_LOCK(fp); 1452df8bae1dSRodney W. Grimes fp->f_count++; 1453df8bae1dSRodney W. Grimes fp->f_msgcount++; 1454426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1455df8bae1dSRodney W. Grimes unp_rights++; 1456df8bae1dSRodney W. Grimes } 1457426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 14582bc21ed9SDavid Malone break; 14592bc21ed9SDavid Malone 14602bc21ed9SDavid Malone case SCM_TIMESTAMP: 14612bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*tv), 14622bc21ed9SDavid Malone SCM_TIMESTAMP, SOL_SOCKET); 14632bc21ed9SDavid Malone if (*controlp == NULL) { 14642bc21ed9SDavid Malone error = ENOBUFS; 14652bc21ed9SDavid Malone goto out; 14668692c025SYoshinobu Inoue } 14672bc21ed9SDavid Malone tv = (struct timeval *) 14682bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 14692bc21ed9SDavid Malone microtime(tv); 14702bc21ed9SDavid Malone break; 14712bc21ed9SDavid Malone 14722bc21ed9SDavid Malone default: 14732bc21ed9SDavid Malone error = EINVAL; 14742bc21ed9SDavid Malone goto out; 14752bc21ed9SDavid Malone } 14762bc21ed9SDavid Malone 14772bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 14782bc21ed9SDavid Malone 14792bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 14802bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 14812bc21ed9SDavid Malone cm = (struct cmsghdr *) 14822bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 14832bc21ed9SDavid Malone } else { 14842bc21ed9SDavid Malone clen = 0; 14852bc21ed9SDavid Malone cm = NULL; 14862bc21ed9SDavid Malone } 14872bc21ed9SDavid Malone } 14882bc21ed9SDavid Malone 14892bc21ed9SDavid Malone out: 14902bc21ed9SDavid Malone m_freem(control); 14912bc21ed9SDavid Malone 14922bc21ed9SDavid Malone return (error); 1493df8bae1dSRodney W. Grimes } 1494df8bae1dSRodney W. Grimes 1495161a0c7cSRobert Watson /* 1496161a0c7cSRobert Watson * unp_defer is thread-local during garbage collection, and does not require 1497161a0c7cSRobert Watson * explicit synchronization. unp_gcing prevents other threads from entering 1498161a0c7cSRobert Watson * garbage collection, and perhaps should be an sx lock instead. 1499161a0c7cSRobert Watson */ 1500f708ef1bSPoul-Henning Kamp static int unp_defer, unp_gcing; 1501df8bae1dSRodney W. Grimes 1502f708ef1bSPoul-Henning Kamp static void 1503df8bae1dSRodney W. Grimes unp_gc() 1504df8bae1dSRodney W. Grimes { 1505df8bae1dSRodney W. Grimes register struct file *fp, *nextfp; 1506df8bae1dSRodney W. Grimes register struct socket *so; 1507df8bae1dSRodney W. Grimes struct file **extra_ref, **fpp; 1508df8bae1dSRodney W. Grimes int nunref, i; 150995f004dcSAlfred Perlstein int nfiles_snap; 151095f004dcSAlfred Perlstein int nfiles_slack = 20; 1511df8bae1dSRodney W. Grimes 15120d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 15130d9ce3a1SRobert Watson 1514161a0c7cSRobert Watson if (unp_gcing) { 1515161a0c7cSRobert Watson UNP_UNLOCK(); 1516df8bae1dSRodney W. Grimes return; 1517161a0c7cSRobert Watson } 1518df8bae1dSRodney W. Grimes unp_gcing = 1; 1519df8bae1dSRodney W. Grimes unp_defer = 0; 1520161a0c7cSRobert Watson UNP_UNLOCK(); 1521ed5b7817SJulian Elischer /* 1522ed5b7817SJulian Elischer * before going through all this, set all FDs to 1523ed5b7817SJulian Elischer * be NOT defered and NOT externally accessible 1524ed5b7817SJulian Elischer */ 1525426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 15262e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) 1527426da3bcSAlfred Perlstein fp->f_gcflag &= ~(FMARK|FDEFER); 1528df8bae1dSRodney W. Grimes do { 15292e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) { 1530426da3bcSAlfred Perlstein FILE_LOCK(fp); 1531ed5b7817SJulian Elischer /* 1532ed5b7817SJulian Elischer * If the file is not open, skip it 1533ed5b7817SJulian Elischer */ 1534426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1535426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1536df8bae1dSRodney W. Grimes continue; 1537426da3bcSAlfred Perlstein } 1538ed5b7817SJulian Elischer /* 1539ed5b7817SJulian Elischer * If we already marked it as 'defer' in a 1540ed5b7817SJulian Elischer * previous pass, then try process it this time 1541ed5b7817SJulian Elischer * and un-mark it 1542ed5b7817SJulian Elischer */ 1543426da3bcSAlfred Perlstein if (fp->f_gcflag & FDEFER) { 1544426da3bcSAlfred Perlstein fp->f_gcflag &= ~FDEFER; 1545df8bae1dSRodney W. Grimes unp_defer--; 1546df8bae1dSRodney W. Grimes } else { 1547ed5b7817SJulian Elischer /* 1548ed5b7817SJulian Elischer * if it's not defered, then check if it's 1549ed5b7817SJulian Elischer * already marked.. if so skip it 1550ed5b7817SJulian Elischer */ 1551426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) { 1552426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1553df8bae1dSRodney W. Grimes continue; 1554426da3bcSAlfred Perlstein } 1555ed5b7817SJulian Elischer /* 1556ed5b7817SJulian Elischer * If all references are from messages 1557ed5b7817SJulian Elischer * in transit, then skip it. it's not 1558ed5b7817SJulian Elischer * externally accessible. 1559ed5b7817SJulian Elischer */ 1560426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount) { 1561426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1562df8bae1dSRodney W. Grimes continue; 1563426da3bcSAlfred Perlstein } 1564ed5b7817SJulian Elischer /* 1565ed5b7817SJulian Elischer * If it got this far then it must be 1566ed5b7817SJulian Elischer * externally accessible. 1567ed5b7817SJulian Elischer */ 1568426da3bcSAlfred Perlstein fp->f_gcflag |= FMARK; 1569df8bae1dSRodney W. Grimes } 1570ed5b7817SJulian Elischer /* 1571ed5b7817SJulian Elischer * either it was defered, or it is externally 1572ed5b7817SJulian Elischer * accessible and not already marked so. 1573ed5b7817SJulian Elischer * Now check if it is possibly one of OUR sockets. 1574ed5b7817SJulian Elischer */ 1575df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_SOCKET || 157648e3128bSMatthew Dillon (so = fp->f_data) == NULL) { 1577426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1578df8bae1dSRodney W. Grimes continue; 1579426da3bcSAlfred Perlstein } 1580426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1581748e0b0aSGarrett Wollman if (so->so_proto->pr_domain != &localdomain || 1582df8bae1dSRodney W. Grimes (so->so_proto->pr_flags&PR_RIGHTS) == 0) 1583df8bae1dSRodney W. Grimes continue; 1584df8bae1dSRodney W. Grimes #ifdef notdef 1585df8bae1dSRodney W. Grimes if (so->so_rcv.sb_flags & SB_LOCK) { 1586df8bae1dSRodney W. Grimes /* 1587df8bae1dSRodney W. Grimes * This is problematical; it's not clear 1588df8bae1dSRodney W. Grimes * we need to wait for the sockbuf to be 1589df8bae1dSRodney W. Grimes * unlocked (on a uniprocessor, at least), 1590df8bae1dSRodney W. Grimes * and it's also not clear what to do 1591df8bae1dSRodney W. Grimes * if sbwait returns an error due to receipt 1592df8bae1dSRodney W. Grimes * of a signal. If sbwait does return 1593df8bae1dSRodney W. Grimes * an error, we'll go into an infinite 1594df8bae1dSRodney W. Grimes * loop. Delete all of this for now. 1595df8bae1dSRodney W. Grimes */ 1596df8bae1dSRodney W. Grimes (void) sbwait(&so->so_rcv); 1597df8bae1dSRodney W. Grimes goto restart; 1598df8bae1dSRodney W. Grimes } 1599df8bae1dSRodney W. Grimes #endif 1600ed5b7817SJulian Elischer /* 1601ed5b7817SJulian Elischer * So, Ok, it's one of our sockets and it IS externally 1602ed5b7817SJulian Elischer * accessible (or was defered). Now we look 1603dc733423SDag-Erling Smørgrav * to see if we hold any file descriptors in its 1604ed5b7817SJulian Elischer * message buffers. Follow those links and mark them 1605ed5b7817SJulian Elischer * as accessible too. 1606ed5b7817SJulian Elischer */ 16077717cf07SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1608df8bae1dSRodney W. Grimes unp_scan(so->so_rcv.sb_mb, unp_mark); 16097717cf07SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 1610df8bae1dSRodney W. Grimes } 1611df8bae1dSRodney W. Grimes } while (unp_defer); 1612426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1613df8bae1dSRodney W. Grimes /* 1614df8bae1dSRodney W. Grimes * We grab an extra reference to each of the file table entries 1615df8bae1dSRodney W. Grimes * that are not otherwise accessible and then free the rights 1616df8bae1dSRodney W. Grimes * that are stored in messages on them. 1617df8bae1dSRodney W. Grimes * 1618df8bae1dSRodney W. Grimes * The bug in the orginal code is a little tricky, so I'll describe 1619df8bae1dSRodney W. Grimes * what's wrong with it here. 1620df8bae1dSRodney W. Grimes * 1621df8bae1dSRodney W. Grimes * It is incorrect to simply unp_discard each entry for f_msgcount 1622df8bae1dSRodney W. Grimes * times -- consider the case of sockets A and B that contain 1623df8bae1dSRodney W. Grimes * references to each other. On a last close of some other socket, 1624df8bae1dSRodney W. Grimes * we trigger a gc since the number of outstanding rights (unp_rights) 1625df8bae1dSRodney W. Grimes * is non-zero. If during the sweep phase the gc code un_discards, 1626df8bae1dSRodney W. Grimes * we end up doing a (full) closef on the descriptor. A closef on A 1627df8bae1dSRodney W. Grimes * results in the following chain. Closef calls soo_close, which 1628df8bae1dSRodney W. Grimes * calls soclose. Soclose calls first (through the switch 1629df8bae1dSRodney W. Grimes * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 1630df8bae1dSRodney W. Grimes * returns because the previous instance had set unp_gcing, and 1631df8bae1dSRodney W. Grimes * we return all the way back to soclose, which marks the socket 1632df8bae1dSRodney W. Grimes * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush 1633df8bae1dSRodney W. Grimes * to free up the rights that are queued in messages on the socket A, 1634df8bae1dSRodney W. Grimes * i.e., the reference on B. The sorflush calls via the dom_dispose 1635df8bae1dSRodney W. Grimes * switch unp_dispose, which unp_scans with unp_discard. This second 1636df8bae1dSRodney W. Grimes * instance of unp_discard just calls closef on B. 1637df8bae1dSRodney W. Grimes * 1638df8bae1dSRodney W. Grimes * Well, a similar chain occurs on B, resulting in a sorflush on B, 1639df8bae1dSRodney W. Grimes * which results in another closef on A. Unfortunately, A is already 1640df8bae1dSRodney W. Grimes * being closed, and the descriptor has already been marked with 1641df8bae1dSRodney W. Grimes * SS_NOFDREF, and soclose panics at this point. 1642df8bae1dSRodney W. Grimes * 1643df8bae1dSRodney W. Grimes * Here, we first take an extra reference to each inaccessible 1644df8bae1dSRodney W. Grimes * descriptor. Then, we call sorflush ourself, since we know 1645df8bae1dSRodney W. Grimes * it is a Unix domain socket anyhow. After we destroy all the 1646df8bae1dSRodney W. Grimes * rights carried in messages, we do a last closef to get rid 1647df8bae1dSRodney W. Grimes * of our extra reference. This is the last close, and the 1648df8bae1dSRodney W. Grimes * unp_detach etc will shut down the socket. 1649df8bae1dSRodney W. Grimes * 1650df8bae1dSRodney W. Grimes * 91/09/19, bsy@cs.cmu.edu 1651df8bae1dSRodney W. Grimes */ 165295f004dcSAlfred Perlstein again: 165395f004dcSAlfred Perlstein nfiles_snap = nfiles + nfiles_slack; /* some slack */ 165495f004dcSAlfred Perlstein extra_ref = malloc(nfiles_snap * sizeof(struct file *), M_TEMP, 165595f004dcSAlfred Perlstein M_WAITOK); 1656426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 165795f004dcSAlfred Perlstein if (nfiles_snap < nfiles) { 165895f004dcSAlfred Perlstein sx_sunlock(&filelist_lock); 165995f004dcSAlfred Perlstein free(extra_ref, M_TEMP); 166095f004dcSAlfred Perlstein nfiles_slack += 20; 166195f004dcSAlfred Perlstein goto again; 166295f004dcSAlfred Perlstein } 1663fc3fcacfSRobert Watson for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; 1664fc3fcacfSRobert Watson fp != NULL; fp = nextfp) { 16652e3c8fcbSPoul-Henning Kamp nextfp = LIST_NEXT(fp, f_list); 1666426da3bcSAlfred Perlstein FILE_LOCK(fp); 1667ed5b7817SJulian Elischer /* 1668ed5b7817SJulian Elischer * If it's not open, skip it 1669ed5b7817SJulian Elischer */ 1670426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1671426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1672df8bae1dSRodney W. Grimes continue; 1673426da3bcSAlfred Perlstein } 1674ed5b7817SJulian Elischer /* 1675ed5b7817SJulian Elischer * If all refs are from msgs, and it's not marked accessible 1676ed5b7817SJulian Elischer * then it must be referenced from some unreachable cycle 1677ed5b7817SJulian Elischer * of (shut-down) FDs, so include it in our 1678ed5b7817SJulian Elischer * list of FDs to remove 1679ed5b7817SJulian Elischer */ 1680426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) { 1681df8bae1dSRodney W. Grimes *fpp++ = fp; 1682df8bae1dSRodney W. Grimes nunref++; 1683df8bae1dSRodney W. Grimes fp->f_count++; 1684df8bae1dSRodney W. Grimes } 1685426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1686df8bae1dSRodney W. Grimes } 1687426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1688ed5b7817SJulian Elischer /* 1689ed5b7817SJulian Elischer * for each FD on our hit list, do the following two things 1690ed5b7817SJulian Elischer */ 16911c7c3c6aSMatthew Dillon for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 16921c7c3c6aSMatthew Dillon struct file *tfp = *fpp; 1693426da3bcSAlfred Perlstein FILE_LOCK(tfp); 1694cd72f218SMatthew Dillon if (tfp->f_type == DTYPE_SOCKET && 169548e3128bSMatthew Dillon tfp->f_data != NULL) { 1696426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 169748e3128bSMatthew Dillon sorflush(tfp->f_data); 1698e5aeaa0cSDag-Erling Smørgrav } else { 1699426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 17001c7c3c6aSMatthew Dillon } 1701e5aeaa0cSDag-Erling Smørgrav } 1702df8bae1dSRodney W. Grimes for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) 1703b40ce416SJulian Elischer closef(*fpp, (struct thread *) NULL); 1704210a5a71SAlfred Perlstein free(extra_ref, M_TEMP); 1705df8bae1dSRodney W. Grimes unp_gcing = 0; 1706161a0c7cSRobert Watson 1707161a0c7cSRobert Watson UNP_UNLOCK_ASSERT(); 1708df8bae1dSRodney W. Grimes } 1709df8bae1dSRodney W. Grimes 171026f9a767SRodney W. Grimes void 1711df8bae1dSRodney W. Grimes unp_dispose(m) 1712df8bae1dSRodney W. Grimes struct mbuf *m; 1713df8bae1dSRodney W. Grimes { 1714996c772fSJohn Dyson 1715df8bae1dSRodney W. Grimes if (m) 1716df8bae1dSRodney W. Grimes unp_scan(m, unp_discard); 1717df8bae1dSRodney W. Grimes } 1718df8bae1dSRodney W. Grimes 17190c1bb4fbSDima Dorfman static int 17206f105b34SJohn Baldwin unp_listen(unp, td) 17210c1bb4fbSDima Dorfman struct unpcb *unp; 17226f105b34SJohn Baldwin struct thread *td; 17230c1bb4fbSDima Dorfman { 17240d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 17250c1bb4fbSDima Dorfman 17260d9ce3a1SRobert Watson /* 17270d9ce3a1SRobert Watson * XXXRW: Why populate the local peer cred with our own credential? 17280d9ce3a1SRobert Watson */ 17296f105b34SJohn Baldwin cru2x(td->td_ucred, &unp->unp_peercred); 17300c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPCCACHED; 17310c1bb4fbSDima Dorfman return (0); 17320c1bb4fbSDima Dorfman } 17330c1bb4fbSDima Dorfman 1734f708ef1bSPoul-Henning Kamp static void 1735df8bae1dSRodney W. Grimes unp_scan(m0, op) 1736df8bae1dSRodney W. Grimes register struct mbuf *m0; 17374d77a549SAlfred Perlstein void (*op)(struct file *); 1738df8bae1dSRodney W. Grimes { 17392bc21ed9SDavid Malone struct mbuf *m; 17402bc21ed9SDavid Malone struct file **rp; 17412bc21ed9SDavid Malone struct cmsghdr *cm; 17422bc21ed9SDavid Malone void *data; 17432bc21ed9SDavid Malone int i; 17442bc21ed9SDavid Malone socklen_t clen, datalen; 1745df8bae1dSRodney W. Grimes int qfds; 1746df8bae1dSRodney W. Grimes 1747fc3fcacfSRobert Watson while (m0 != NULL) { 17482bc21ed9SDavid Malone for (m = m0; m; m = m->m_next) { 174912396bdcSDavid Malone if (m->m_type != MT_CONTROL) 1750df8bae1dSRodney W. Grimes continue; 17512bc21ed9SDavid Malone 17522bc21ed9SDavid Malone cm = mtod(m, struct cmsghdr *); 17532bc21ed9SDavid Malone clen = m->m_len; 17542bc21ed9SDavid Malone 17552bc21ed9SDavid Malone while (cm != NULL) { 17562bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) 17572bc21ed9SDavid Malone break; 17582bc21ed9SDavid Malone 17592bc21ed9SDavid Malone data = CMSG_DATA(cm); 17602bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len 17612bc21ed9SDavid Malone - (caddr_t)data; 17622bc21ed9SDavid Malone 17632bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET && 17642bc21ed9SDavid Malone cm->cmsg_type == SCM_RIGHTS) { 17652bc21ed9SDavid Malone qfds = datalen / sizeof (struct file *); 17662bc21ed9SDavid Malone rp = data; 1767df8bae1dSRodney W. Grimes for (i = 0; i < qfds; i++) 1768df8bae1dSRodney W. Grimes (*op)(*rp++); 17692bc21ed9SDavid Malone } 17702bc21ed9SDavid Malone 17712bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 17722bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 17732bc21ed9SDavid Malone cm = (struct cmsghdr *) 17742bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 17752bc21ed9SDavid Malone } else { 17762bc21ed9SDavid Malone clen = 0; 17772bc21ed9SDavid Malone cm = NULL; 17782bc21ed9SDavid Malone } 17792bc21ed9SDavid Malone } 1780df8bae1dSRodney W. Grimes } 1781df8bae1dSRodney W. Grimes m0 = m0->m_act; 1782df8bae1dSRodney W. Grimes } 1783df8bae1dSRodney W. Grimes } 1784df8bae1dSRodney W. Grimes 1785f708ef1bSPoul-Henning Kamp static void 1786df8bae1dSRodney W. Grimes unp_mark(fp) 1787df8bae1dSRodney W. Grimes struct file *fp; 1788df8bae1dSRodney W. Grimes { 1789426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) 1790df8bae1dSRodney W. Grimes return; 1791df8bae1dSRodney W. Grimes unp_defer++; 1792426da3bcSAlfred Perlstein fp->f_gcflag |= (FMARK|FDEFER); 1793df8bae1dSRodney W. Grimes } 1794df8bae1dSRodney W. Grimes 1795f708ef1bSPoul-Henning Kamp static void 1796df8bae1dSRodney W. Grimes unp_discard(fp) 1797df8bae1dSRodney W. Grimes struct file *fp; 1798df8bae1dSRodney W. Grimes { 1799426da3bcSAlfred Perlstein FILE_LOCK(fp); 1800df8bae1dSRodney W. Grimes fp->f_msgcount--; 1801df8bae1dSRodney W. Grimes unp_rights--; 1802426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1803b40ce416SJulian Elischer (void) closef(fp, (struct thread *)NULL); 1804df8bae1dSRodney W. Grimes } 1805