1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 29748e0b0aSGarrett Wollman * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94 30df8bae1dSRodney W. Grimes */ 31df8bae1dSRodney W. Grimes 32677b542eSDavid E. O'Brien #include <sys/cdefs.h> 33677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 34677b542eSDavid E. O'Brien 35335654d7SRobert Watson #include "opt_mac.h" 36335654d7SRobert Watson 37df8bae1dSRodney W. Grimes #include <sys/param.h> 38fb919e4dSMark Murray #include <sys/domain.h> 39960ed29cSSeigo Tanimura #include <sys/fcntl.h> 40d826c479SBruce Evans #include <sys/malloc.h> /* XXX must be before <sys/file.h> */ 41639acc13SGarrett Wollman #include <sys/file.h> 42960ed29cSSeigo Tanimura #include <sys/filedesc.h> 43960ed29cSSeigo Tanimura #include <sys/jail.h> 44960ed29cSSeigo Tanimura #include <sys/kernel.h> 45960ed29cSSeigo Tanimura #include <sys/lock.h> 466ea48a90SRobert Watson #include <sys/mac.h> 47639acc13SGarrett Wollman #include <sys/mbuf.h> 48960ed29cSSeigo Tanimura #include <sys/mutex.h> 49639acc13SGarrett Wollman #include <sys/namei.h> 50639acc13SGarrett Wollman #include <sys/proc.h> 51df8bae1dSRodney W. Grimes #include <sys/protosw.h> 52960ed29cSSeigo Tanimura #include <sys/resourcevar.h> 53df8bae1dSRodney W. Grimes #include <sys/socket.h> 54df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 55960ed29cSSeigo Tanimura #include <sys/signalvar.h> 56df8bae1dSRodney W. Grimes #include <sys/stat.h> 57960ed29cSSeigo Tanimura #include <sys/sx.h> 58639acc13SGarrett Wollman #include <sys/sysctl.h> 59960ed29cSSeigo Tanimura #include <sys/systm.h> 60639acc13SGarrett Wollman #include <sys/un.h> 6198271db4SGarrett Wollman #include <sys/unpcb.h> 62639acc13SGarrett Wollman #include <sys/vnode.h> 63df8bae1dSRodney W. Grimes 649e9d298aSJeff Roberson #include <vm/uma.h> 6598271db4SGarrett Wollman 669e9d298aSJeff Roberson static uma_zone_t unp_zone; 6798271db4SGarrett Wollman static unp_gen_t unp_gencnt; 6898271db4SGarrett Wollman static u_int unp_count; 6998271db4SGarrett Wollman 7098271db4SGarrett Wollman static struct unp_head unp_shead, unp_dhead; 7198271db4SGarrett Wollman 72df8bae1dSRodney W. Grimes /* 73df8bae1dSRodney W. Grimes * Unix communications domain. 74df8bae1dSRodney W. Grimes * 75df8bae1dSRodney W. Grimes * TODO: 76df8bae1dSRodney W. Grimes * SEQPACKET, RDM 77df8bae1dSRodney W. Grimes * rethink name space problems 78df8bae1dSRodney W. Grimes * need a proper out-of-band 7998271db4SGarrett Wollman * lock pushdown 80df8bae1dSRodney W. Grimes */ 81e7dd9a10SRobert Watson static const struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 82f708ef1bSPoul-Henning Kamp static ino_t unp_ino; /* prototype for fake inode numbers */ 83f708ef1bSPoul-Henning Kamp 840d9ce3a1SRobert Watson static struct mtx unp_mtx; 850d9ce3a1SRobert Watson #define UNP_LOCK_INIT() \ 860d9ce3a1SRobert Watson mtx_init(&unp_mtx, "unp", NULL, MTX_DEF) 870d9ce3a1SRobert Watson #define UNP_LOCK() mtx_lock(&unp_mtx) 880d9ce3a1SRobert Watson #define UNP_UNLOCK() mtx_unlock(&unp_mtx) 890d9ce3a1SRobert Watson #define UNP_LOCK_ASSERT() mtx_assert(&unp_mtx, MA_OWNED) 900d9ce3a1SRobert Watson 914d77a549SAlfred Perlstein static int unp_attach(struct socket *); 924d77a549SAlfred Perlstein static void unp_detach(struct unpcb *); 934d77a549SAlfred Perlstein static int unp_bind(struct unpcb *,struct sockaddr *, struct thread *); 9470f52b48SBruce Evans static int unp_connect(struct socket *,struct sockaddr *, struct thread *); 95db48c0d2SRobert Watson static int unp_connect2(struct socket *so, struct socket *so2); 964d77a549SAlfred Perlstein static void unp_disconnect(struct unpcb *); 974d77a549SAlfred Perlstein static void unp_shutdown(struct unpcb *); 984d77a549SAlfred Perlstein static void unp_drop(struct unpcb *, int); 994d77a549SAlfred Perlstein static void unp_gc(void); 1004d77a549SAlfred Perlstein static void unp_scan(struct mbuf *, void (*)(struct file *)); 1014d77a549SAlfred Perlstein static void unp_mark(struct file *); 1024d77a549SAlfred Perlstein static void unp_discard(struct file *); 1034d77a549SAlfred Perlstein static void unp_freerights(struct file **, int); 1044d77a549SAlfred Perlstein static int unp_internalize(struct mbuf **, struct thread *); 1054d77a549SAlfred Perlstein static int unp_listen(struct unpcb *, struct thread *); 106f708ef1bSPoul-Henning Kamp 107a29f300eSGarrett Wollman static int 108a29f300eSGarrett Wollman uipc_abort(struct socket *so) 109df8bae1dSRodney W. Grimes { 110df8bae1dSRodney W. Grimes struct unpcb *unp = sotounpcb(so); 111df8bae1dSRodney W. Grimes 112fc3fcacfSRobert Watson if (unp == NULL) 113e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 1140d9ce3a1SRobert Watson UNP_LOCK(); 115a29f300eSGarrett Wollman unp_drop(unp, ECONNABORTED); 1160d9ce3a1SRobert Watson unp_detach(unp); /* NB: unlocks */ 117395a08c9SRobert Watson SOCK_LOCK(so); 118ddb7d629SIan Dowse sotryfree(so); 119e5aeaa0cSDag-Erling Smørgrav return (0); 120df8bae1dSRodney W. Grimes } 121df8bae1dSRodney W. Grimes 122a29f300eSGarrett Wollman static int 12357bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam) 124a29f300eSGarrett Wollman { 125a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 1260d9ce3a1SRobert Watson const struct sockaddr *sa; 127df8bae1dSRodney W. Grimes 128fc3fcacfSRobert Watson if (unp == NULL) 129e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 130df8bae1dSRodney W. Grimes 131df8bae1dSRodney W. Grimes /* 132df8bae1dSRodney W. Grimes * Pass back name of connected socket, 133df8bae1dSRodney W. Grimes * if it was bound and we are still connected 134df8bae1dSRodney W. Grimes * (our peer may have closed already!). 135df8bae1dSRodney W. Grimes */ 1360d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 1370d9ce3a1SRobert Watson UNP_LOCK(); 1380d9ce3a1SRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr != NULL) 1390d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 1400d9ce3a1SRobert Watson else 1410d9ce3a1SRobert Watson sa = &sun_noname; 1420d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 1430d9ce3a1SRobert Watson UNP_UNLOCK(); 144e5aeaa0cSDag-Erling Smørgrav return (0); 145a29f300eSGarrett Wollman } 146df8bae1dSRodney W. Grimes 147a29f300eSGarrett Wollman static int 148b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td) 149a29f300eSGarrett Wollman { 150a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 151df8bae1dSRodney W. Grimes 152fc3fcacfSRobert Watson if (unp != NULL) 153e5aeaa0cSDag-Erling Smørgrav return (EISCONN); 154e5aeaa0cSDag-Erling Smørgrav return (unp_attach(so)); 155a29f300eSGarrett Wollman } 156a29f300eSGarrett Wollman 157a29f300eSGarrett Wollman static int 158b40ce416SJulian Elischer uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 159a29f300eSGarrett Wollman { 160a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 161a29f300eSGarrett Wollman 162fc3fcacfSRobert Watson if (unp == NULL) 163e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 164a29f300eSGarrett Wollman 165e5aeaa0cSDag-Erling Smørgrav return (unp_bind(unp, nam, td)); 166a29f300eSGarrett Wollman } 167a29f300eSGarrett Wollman 168a29f300eSGarrett Wollman static int 169b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 170a29f300eSGarrett Wollman { 171b295bdcdSRobert Watson struct unpcb *unp; 1720d9ce3a1SRobert Watson int error; 173a29f300eSGarrett Wollman 174fd179ee9SRobert Watson KASSERT(td == curthread, ("uipc_connect: td != curthread")); 175fd179ee9SRobert Watson 1760d9ce3a1SRobert Watson UNP_LOCK(); 177b295bdcdSRobert Watson unp = sotounpcb(so); 178b295bdcdSRobert Watson if (unp == NULL) { 179b295bdcdSRobert Watson error = EINVAL; 180b295bdcdSRobert Watson goto out; 181b295bdcdSRobert Watson } 182fd179ee9SRobert Watson error = unp_connect(so, nam, td); 183b295bdcdSRobert Watson out: 1840d9ce3a1SRobert Watson UNP_UNLOCK(); 1850d9ce3a1SRobert Watson return (error); 186a29f300eSGarrett Wollman } 187a29f300eSGarrett Wollman 188db48c0d2SRobert Watson int 189a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2) 190a29f300eSGarrett Wollman { 191a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so1); 1920d9ce3a1SRobert Watson int error; 193a29f300eSGarrett Wollman 194fc3fcacfSRobert Watson if (unp == NULL) 195e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 196a29f300eSGarrett Wollman 1970d9ce3a1SRobert Watson UNP_LOCK(); 1980d9ce3a1SRobert Watson error = unp_connect2(so1, so2); 1990d9ce3a1SRobert Watson UNP_UNLOCK(); 2000d9ce3a1SRobert Watson return (error); 201a29f300eSGarrett Wollman } 202a29f300eSGarrett Wollman 203a29f300eSGarrett Wollman /* control is EOPNOTSUPP */ 204a29f300eSGarrett Wollman 205a29f300eSGarrett Wollman static int 206a29f300eSGarrett Wollman uipc_detach(struct socket *so) 207a29f300eSGarrett Wollman { 208a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 209a29f300eSGarrett Wollman 210fc3fcacfSRobert Watson if (unp == NULL) 211e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 212a29f300eSGarrett Wollman 2130d9ce3a1SRobert Watson UNP_LOCK(); 2140d9ce3a1SRobert Watson unp_detach(unp); /* NB: unlocks unp */ 215e5aeaa0cSDag-Erling Smørgrav return (0); 216a29f300eSGarrett Wollman } 217a29f300eSGarrett Wollman 218a29f300eSGarrett Wollman static int 219a29f300eSGarrett Wollman uipc_disconnect(struct socket *so) 220a29f300eSGarrett Wollman { 221a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 222a29f300eSGarrett Wollman 223fc3fcacfSRobert Watson if (unp == NULL) 224e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 2250d9ce3a1SRobert Watson UNP_LOCK(); 226a29f300eSGarrett Wollman unp_disconnect(unp); 2270d9ce3a1SRobert Watson UNP_UNLOCK(); 228e5aeaa0cSDag-Erling Smørgrav return (0); 229a29f300eSGarrett Wollman } 230a29f300eSGarrett Wollman 231a29f300eSGarrett Wollman static int 232b40ce416SJulian Elischer uipc_listen(struct socket *so, struct thread *td) 233a29f300eSGarrett Wollman { 234a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 2350d9ce3a1SRobert Watson int error; 236a29f300eSGarrett Wollman 237fc3fcacfSRobert Watson if (unp == NULL || unp->unp_vnode == NULL) 238e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 2390d9ce3a1SRobert Watson UNP_LOCK(); 2400d9ce3a1SRobert Watson error = unp_listen(unp, td); 2410d9ce3a1SRobert Watson UNP_UNLOCK(); 2420d9ce3a1SRobert Watson return (error); 243a29f300eSGarrett Wollman } 244a29f300eSGarrett Wollman 245a29f300eSGarrett Wollman static int 24657bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 247a29f300eSGarrett Wollman { 248a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 2490d9ce3a1SRobert Watson const struct sockaddr *sa; 250a29f300eSGarrett Wollman 251fc3fcacfSRobert Watson if (unp == NULL) 252e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 2530d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 2540d9ce3a1SRobert Watson UNP_LOCK(); 255fc3fcacfSRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr!= NULL) 2560d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 257bdc5f6a3SHajimu UMEMOTO else { 258bdc5f6a3SHajimu UMEMOTO /* 259bdc5f6a3SHajimu UMEMOTO * XXX: It seems that this test always fails even when 260bdc5f6a3SHajimu UMEMOTO * connection is established. So, this else clause is 261bdc5f6a3SHajimu UMEMOTO * added as workaround to return PF_LOCAL sockaddr. 262bdc5f6a3SHajimu UMEMOTO */ 2630d9ce3a1SRobert Watson sa = &sun_noname; 264bdc5f6a3SHajimu UMEMOTO } 2650d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 2660d9ce3a1SRobert Watson UNP_UNLOCK(); 267e5aeaa0cSDag-Erling Smørgrav return (0); 268a29f300eSGarrett Wollman } 269a29f300eSGarrett Wollman 270a29f300eSGarrett Wollman static int 271a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 272a29f300eSGarrett Wollman { 273a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 274a29f300eSGarrett Wollman struct socket *so2; 2756aef685fSBrian Feldman u_long newhiwat; 276a29f300eSGarrett Wollman 277fc3fcacfSRobert Watson if (unp == NULL) 278e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 2790d9ce3a1SRobert Watson UNP_LOCK(); 280df8bae1dSRodney W. Grimes switch (so->so_type) { 281df8bae1dSRodney W. Grimes case SOCK_DGRAM: 282a29f300eSGarrett Wollman panic("uipc_rcvd DGRAM?"); 283df8bae1dSRodney W. Grimes /*NOTREACHED*/ 284df8bae1dSRodney W. Grimes 285df8bae1dSRodney W. Grimes case SOCK_STREAM: 286fc3fcacfSRobert Watson if (unp->unp_conn == NULL) 287df8bae1dSRodney W. Grimes break; 288df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 289c9f69064SRobert Watson SOCKBUF_LOCK(&so2->so_snd); 290c9f69064SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 291df8bae1dSRodney W. Grimes /* 292df8bae1dSRodney W. Grimes * Adjust backpressure on sender 293df8bae1dSRodney W. Grimes * and wakeup any waiting to write. 294df8bae1dSRodney W. Grimes */ 295ff8b0106SBrian Feldman so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt; 296ff8b0106SBrian Feldman unp->unp_mbcnt = so->so_rcv.sb_mbcnt; 2976aef685fSBrian Feldman newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - 2986aef685fSBrian Feldman so->so_rcv.sb_cc; 299f535380cSDon Lewis (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat, 3006aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 301ff8b0106SBrian Feldman unp->unp_cc = so->so_rcv.sb_cc; 302c9f69064SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 3031e4d7da7SRobert Watson sowwakeup_locked(so2); 304df8bae1dSRodney W. Grimes break; 305df8bae1dSRodney W. Grimes 306df8bae1dSRodney W. Grimes default: 307a29f300eSGarrett Wollman panic("uipc_rcvd unknown socktype"); 308df8bae1dSRodney W. Grimes } 3090d9ce3a1SRobert Watson UNP_UNLOCK(); 310e5aeaa0cSDag-Erling Smørgrav return (0); 311a29f300eSGarrett Wollman } 312df8bae1dSRodney W. Grimes 313a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 314a29f300eSGarrett Wollman 315a29f300eSGarrett Wollman static int 31657bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 317b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 318a29f300eSGarrett Wollman { 319a29f300eSGarrett Wollman int error = 0; 320a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 321a29f300eSGarrett Wollman struct socket *so2; 3226aef685fSBrian Feldman u_long newhiwat; 323a29f300eSGarrett Wollman 324fc3fcacfSRobert Watson if (unp == NULL) { 325a29f300eSGarrett Wollman error = EINVAL; 326a29f300eSGarrett Wollman goto release; 327a29f300eSGarrett Wollman } 328a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 329a29f300eSGarrett Wollman error = EOPNOTSUPP; 330a29f300eSGarrett Wollman goto release; 331a29f300eSGarrett Wollman } 332a29f300eSGarrett Wollman 333fc3fcacfSRobert Watson if (control != NULL && (error = unp_internalize(&control, td))) 334a29f300eSGarrett Wollman goto release; 335df8bae1dSRodney W. Grimes 3360d9ce3a1SRobert Watson UNP_LOCK(); 337a29f300eSGarrett Wollman switch (so->so_type) { 338a29f300eSGarrett Wollman case SOCK_DGRAM: 339a29f300eSGarrett Wollman { 340e7dd9a10SRobert Watson const struct sockaddr *from; 341df8bae1dSRodney W. Grimes 342fc3fcacfSRobert Watson if (nam != NULL) { 343fc3fcacfSRobert Watson if (unp->unp_conn != NULL) { 344df8bae1dSRodney W. Grimes error = EISCONN; 345df8bae1dSRodney W. Grimes break; 346df8bae1dSRodney W. Grimes } 347b40ce416SJulian Elischer error = unp_connect(so, nam, td); 348df8bae1dSRodney W. Grimes if (error) 349df8bae1dSRodney W. Grimes break; 350df8bae1dSRodney W. Grimes } else { 351fc3fcacfSRobert Watson if (unp->unp_conn == NULL) { 352df8bae1dSRodney W. Grimes error = ENOTCONN; 353df8bae1dSRodney W. Grimes break; 354df8bae1dSRodney W. Grimes } 355df8bae1dSRodney W. Grimes } 356df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 357fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 35857bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 359df8bae1dSRodney W. Grimes else 360df8bae1dSRodney W. Grimes from = &sun_noname; 361a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 362a34b7046SRobert Watson if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) { 3631e4d7da7SRobert Watson sorwakeup_locked(so2); 364fc3fcacfSRobert Watson m = NULL; 365fc3fcacfSRobert Watson control = NULL; 366e5aeaa0cSDag-Erling Smørgrav } else { 367a34b7046SRobert Watson SOCKBUF_UNLOCK(&so2->so_rcv); 368df8bae1dSRodney W. Grimes error = ENOBUFS; 369e5aeaa0cSDag-Erling Smørgrav } 370fc3fcacfSRobert Watson if (nam != NULL) 371df8bae1dSRodney W. Grimes unp_disconnect(unp); 372df8bae1dSRodney W. Grimes break; 373df8bae1dSRodney W. Grimes } 374df8bae1dSRodney W. Grimes 375df8bae1dSRodney W. Grimes case SOCK_STREAM: 3766b8fda4dSGarrett Wollman /* Connect if not connected yet. */ 3776b8fda4dSGarrett Wollman /* 3786b8fda4dSGarrett Wollman * Note: A better implementation would complain 379402cc72dSDavid Greenman * if not equal to the peer's address. 3806b8fda4dSGarrett Wollman */ 381402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 382fc3fcacfSRobert Watson if (nam != NULL) { 383b40ce416SJulian Elischer error = unp_connect(so, nam, td); 384402cc72dSDavid Greenman if (error) 3856b8fda4dSGarrett Wollman break; /* XXX */ 386402cc72dSDavid Greenman } else { 387402cc72dSDavid Greenman error = ENOTCONN; 388402cc72dSDavid Greenman break; 389402cc72dSDavid Greenman } 390402cc72dSDavid Greenman } 391402cc72dSDavid Greenman 392c0b99ffaSRobert Watson if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 393df8bae1dSRodney W. Grimes error = EPIPE; 394df8bae1dSRodney W. Grimes break; 395df8bae1dSRodney W. Grimes } 396fc3fcacfSRobert Watson if (unp->unp_conn == NULL) 397a29f300eSGarrett Wollman panic("uipc_send connected but no connection?"); 398df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 399a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 400df8bae1dSRodney W. Grimes /* 401df8bae1dSRodney W. Grimes * Send to paired receive port, and then reduce 402df8bae1dSRodney W. Grimes * send buffer hiwater marks to maintain backpressure. 403df8bae1dSRodney W. Grimes * Wake up readers. 404df8bae1dSRodney W. Grimes */ 405fc3fcacfSRobert Watson if (control != NULL) { 406a34b7046SRobert Watson if (sbappendcontrol_locked(&so2->so_rcv, m, control)) 407fc3fcacfSRobert Watson control = NULL; 408e5aeaa0cSDag-Erling Smørgrav } else { 409a34b7046SRobert Watson sbappend_locked(&so2->so_rcv, m); 410e5aeaa0cSDag-Erling Smørgrav } 411ff8b0106SBrian Feldman so->so_snd.sb_mbmax -= 412ff8b0106SBrian Feldman so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt; 413ff8b0106SBrian Feldman unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt; 4146aef685fSBrian Feldman newhiwat = so->so_snd.sb_hiwat - 4156aef685fSBrian Feldman (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc); 416f535380cSDon Lewis (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat, 4176aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 418ff8b0106SBrian Feldman unp->unp_conn->unp_cc = so2->so_rcv.sb_cc; 4191e4d7da7SRobert Watson sorwakeup_locked(so2); 420fc3fcacfSRobert Watson m = NULL; 421df8bae1dSRodney W. Grimes break; 422df8bae1dSRodney W. Grimes 423df8bae1dSRodney W. Grimes default: 424a29f300eSGarrett Wollman panic("uipc_send unknown socktype"); 425df8bae1dSRodney W. Grimes } 426a29f300eSGarrett Wollman 4276b8fda4dSGarrett Wollman /* 4286b8fda4dSGarrett Wollman * SEND_EOF is equivalent to a SEND followed by 4296b8fda4dSGarrett Wollman * a SHUTDOWN. 4306b8fda4dSGarrett Wollman */ 431a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 4326b8fda4dSGarrett Wollman socantsendmore(so); 4336b8fda4dSGarrett Wollman unp_shutdown(unp); 4346b8fda4dSGarrett Wollman } 4350d9ce3a1SRobert Watson UNP_UNLOCK(); 436df8bae1dSRodney W. Grimes 437fc3fcacfSRobert Watson if (control != NULL && error != 0) 438bd508d39SDon Lewis unp_dispose(control); 439bd508d39SDon Lewis 440a29f300eSGarrett Wollman release: 441fc3fcacfSRobert Watson if (control != NULL) 442a29f300eSGarrett Wollman m_freem(control); 443fc3fcacfSRobert Watson if (m != NULL) 444a29f300eSGarrett Wollman m_freem(m); 445e5aeaa0cSDag-Erling Smørgrav return (error); 446a29f300eSGarrett Wollman } 447df8bae1dSRodney W. Grimes 448a29f300eSGarrett Wollman static int 449a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 450a29f300eSGarrett Wollman { 451a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 452a29f300eSGarrett Wollman struct socket *so2; 453a29f300eSGarrett Wollman 454fc3fcacfSRobert Watson if (unp == NULL) 455e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 4560d9ce3a1SRobert Watson UNP_LOCK(); 457a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 458fc3fcacfSRobert Watson if (so->so_type == SOCK_STREAM && unp->unp_conn != NULL) { 459df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 460a29f300eSGarrett Wollman sb->st_blksize += so2->so_rcv.sb_cc; 461df8bae1dSRodney W. Grimes } 462f3732fd1SPoul-Henning Kamp sb->st_dev = NODEV; 463df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 4646f782c46SJeffrey Hsu unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 465a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 4660d9ce3a1SRobert Watson UNP_UNLOCK(); 467df8bae1dSRodney W. Grimes return (0); 468a29f300eSGarrett Wollman } 469df8bae1dSRodney W. Grimes 470a29f300eSGarrett Wollman static int 471a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 472a29f300eSGarrett Wollman { 473a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 474df8bae1dSRodney W. Grimes 475fc3fcacfSRobert Watson if (unp == NULL) 476e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 4770d9ce3a1SRobert Watson UNP_LOCK(); 478a29f300eSGarrett Wollman socantsendmore(so); 479a29f300eSGarrett Wollman unp_shutdown(unp); 4800d9ce3a1SRobert Watson UNP_UNLOCK(); 481e5aeaa0cSDag-Erling Smørgrav return (0); 482a29f300eSGarrett Wollman } 483df8bae1dSRodney W. Grimes 484a29f300eSGarrett Wollman static int 48557bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 486a29f300eSGarrett Wollman { 487a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 4880d9ce3a1SRobert Watson const struct sockaddr *sa; 489a29f300eSGarrett Wollman 490fc3fcacfSRobert Watson if (unp == NULL) 491e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 4920d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 4930d9ce3a1SRobert Watson UNP_LOCK(); 494fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 4950d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_addr; 49683f3198bSThomas Moestl else 4970d9ce3a1SRobert Watson sa = &sun_noname; 4980d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 4990d9ce3a1SRobert Watson UNP_UNLOCK(); 500e5aeaa0cSDag-Erling Smørgrav return (0); 501df8bae1dSRodney W. Grimes } 502a29f300eSGarrett Wollman 503a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = { 504a29f300eSGarrett Wollman uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect, 505a29f300eSGarrett Wollman uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect, 506a29f300eSGarrett Wollman uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp, 507a29f300eSGarrett Wollman uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr, 508a557af22SRobert Watson sosend, soreceive, sopoll, pru_sosetlabel_null 509a29f300eSGarrett Wollman }; 510df8bae1dSRodney W. Grimes 5110c1bb4fbSDima Dorfman int 5120c1bb4fbSDima Dorfman uipc_ctloutput(so, sopt) 5130c1bb4fbSDima Dorfman struct socket *so; 5140c1bb4fbSDima Dorfman struct sockopt *sopt; 5150c1bb4fbSDima Dorfman { 5160c1bb4fbSDima Dorfman struct unpcb *unp = sotounpcb(so); 5170d9ce3a1SRobert Watson struct xucred xu; 5180c1bb4fbSDima Dorfman int error; 5190c1bb4fbSDima Dorfman 5200c1bb4fbSDima Dorfman switch (sopt->sopt_dir) { 5210c1bb4fbSDima Dorfman case SOPT_GET: 5220c1bb4fbSDima Dorfman switch (sopt->sopt_name) { 5230c1bb4fbSDima Dorfman case LOCAL_PEERCRED: 5240d9ce3a1SRobert Watson error = 0; 5250d9ce3a1SRobert Watson UNP_LOCK(); 5260c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC) 5270d9ce3a1SRobert Watson xu = unp->unp_peercred; 5280c1bb4fbSDima Dorfman else { 5290c1bb4fbSDima Dorfman if (so->so_type == SOCK_STREAM) 5300c1bb4fbSDima Dorfman error = ENOTCONN; 5310c1bb4fbSDima Dorfman else 5320c1bb4fbSDima Dorfman error = EINVAL; 5330c1bb4fbSDima Dorfman } 5340d9ce3a1SRobert Watson UNP_UNLOCK(); 5350d9ce3a1SRobert Watson if (error == 0) 5360d9ce3a1SRobert Watson error = sooptcopyout(sopt, &xu, sizeof(xu)); 5370c1bb4fbSDima Dorfman break; 5380c1bb4fbSDima Dorfman default: 5390c1bb4fbSDima Dorfman error = EOPNOTSUPP; 5400c1bb4fbSDima Dorfman break; 5410c1bb4fbSDima Dorfman } 5420c1bb4fbSDima Dorfman break; 5430c1bb4fbSDima Dorfman case SOPT_SET: 5440c1bb4fbSDima Dorfman default: 5450c1bb4fbSDima Dorfman error = EOPNOTSUPP; 5460c1bb4fbSDima Dorfman break; 5470c1bb4fbSDima Dorfman } 5480c1bb4fbSDima Dorfman return (error); 5490c1bb4fbSDima Dorfman } 5500c1bb4fbSDima Dorfman 551df8bae1dSRodney W. Grimes /* 552df8bae1dSRodney W. Grimes * Both send and receive buffers are allocated PIPSIZ bytes of buffering 553df8bae1dSRodney W. Grimes * for stream sockets, although the total for sender and receiver is 554df8bae1dSRodney W. Grimes * actually only PIPSIZ. 555df8bae1dSRodney W. Grimes * Datagram sockets really use the sendspace as the maximum datagram size, 556df8bae1dSRodney W. Grimes * and don't really want to reserve the sendspace. Their recvspace should 557df8bae1dSRodney W. Grimes * be large enough for at least one max-size datagram plus address. 558df8bae1dSRodney W. Grimes */ 5595dce41c5SJohn Dyson #ifndef PIPSIZ 5605dce41c5SJohn Dyson #define PIPSIZ 8192 5615dce41c5SJohn Dyson #endif 562f708ef1bSPoul-Henning Kamp static u_long unpst_sendspace = PIPSIZ; 563f708ef1bSPoul-Henning Kamp static u_long unpst_recvspace = PIPSIZ; 564f708ef1bSPoul-Henning Kamp static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 565f708ef1bSPoul-Henning Kamp static u_long unpdg_recvspace = 4*1024; 566df8bae1dSRodney W. Grimes 567f708ef1bSPoul-Henning Kamp static int unp_rights; /* file descriptors in flight */ 568df8bae1dSRodney W. Grimes 569ce02431fSDoug Rabson SYSCTL_DECL(_net_local_stream); 570639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 571639acc13SGarrett Wollman &unpst_sendspace, 0, ""); 572639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 573639acc13SGarrett Wollman &unpst_recvspace, 0, ""); 574ce02431fSDoug Rabson SYSCTL_DECL(_net_local_dgram); 575639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 576639acc13SGarrett Wollman &unpdg_sendspace, 0, ""); 577639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 578639acc13SGarrett Wollman &unpdg_recvspace, 0, ""); 579ce02431fSDoug Rabson SYSCTL_DECL(_net_local); 580639acc13SGarrett Wollman SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); 581639acc13SGarrett Wollman 582f708ef1bSPoul-Henning Kamp static int 583df8bae1dSRodney W. Grimes unp_attach(so) 584df8bae1dSRodney W. Grimes struct socket *so; 585df8bae1dSRodney W. Grimes { 586df8bae1dSRodney W. Grimes register struct unpcb *unp; 587df8bae1dSRodney W. Grimes int error; 588df8bae1dSRodney W. Grimes 589df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 590df8bae1dSRodney W. Grimes switch (so->so_type) { 591df8bae1dSRodney W. Grimes 592df8bae1dSRodney W. Grimes case SOCK_STREAM: 593df8bae1dSRodney W. Grimes error = soreserve(so, unpst_sendspace, unpst_recvspace); 594df8bae1dSRodney W. Grimes break; 595df8bae1dSRodney W. Grimes 596df8bae1dSRodney W. Grimes case SOCK_DGRAM: 597df8bae1dSRodney W. Grimes error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 598df8bae1dSRodney W. Grimes break; 599df8bae1dSRodney W. Grimes 600df8bae1dSRodney W. Grimes default: 601df8bae1dSRodney W. Grimes panic("unp_attach"); 602df8bae1dSRodney W. Grimes } 603df8bae1dSRodney W. Grimes if (error) 604df8bae1dSRodney W. Grimes return (error); 605df8bae1dSRodney W. Grimes } 606a163d034SWarner Losh unp = uma_zalloc(unp_zone, M_WAITOK); 60757bf258eSGarrett Wollman if (unp == NULL) 608df8bae1dSRodney W. Grimes return (ENOBUFS); 60957bf258eSGarrett Wollman bzero(unp, sizeof *unp); 61098271db4SGarrett Wollman LIST_INIT(&unp->unp_refs); 611df8bae1dSRodney W. Grimes unp->unp_socket = so; 6120d9ce3a1SRobert Watson 6130d9ce3a1SRobert Watson UNP_LOCK(); 6140d9ce3a1SRobert Watson unp->unp_gencnt = ++unp_gencnt; 6150d9ce3a1SRobert Watson unp_count++; 61698271db4SGarrett Wollman LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead 61798271db4SGarrett Wollman : &unp_shead, unp, unp_link); 6180d9ce3a1SRobert Watson UNP_UNLOCK(); 6190d9ce3a1SRobert Watson 620210a5a71SAlfred Perlstein so->so_pcb = unp; 621df8bae1dSRodney W. Grimes return (0); 622df8bae1dSRodney W. Grimes } 623df8bae1dSRodney W. Grimes 624f708ef1bSPoul-Henning Kamp static void 625df8bae1dSRodney W. Grimes unp_detach(unp) 626df8bae1dSRodney W. Grimes register struct unpcb *unp; 627df8bae1dSRodney W. Grimes { 6280d9ce3a1SRobert Watson struct vnode *vp; 6290d9ce3a1SRobert Watson 6300d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 6310d9ce3a1SRobert Watson 63298271db4SGarrett Wollman LIST_REMOVE(unp, unp_link); 63398271db4SGarrett Wollman unp->unp_gencnt = ++unp_gencnt; 63498271db4SGarrett Wollman --unp_count; 6350d9ce3a1SRobert Watson if ((vp = unp->unp_vnode) != NULL) { 6360d9ce3a1SRobert Watson /* 6370d9ce3a1SRobert Watson * XXXRW: should v_socket be frobbed only while holding 6380d9ce3a1SRobert Watson * Giant? 6390d9ce3a1SRobert Watson */ 640fc3fcacfSRobert Watson unp->unp_vnode->v_socket = NULL; 641fc3fcacfSRobert Watson unp->unp_vnode = NULL; 642df8bae1dSRodney W. Grimes } 643fc3fcacfSRobert Watson if (unp->unp_conn != NULL) 644df8bae1dSRodney W. Grimes unp_disconnect(unp); 6450d9ce3a1SRobert Watson while (!LIST_EMPTY(&unp->unp_refs)) { 6460d9ce3a1SRobert Watson struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 6470d9ce3a1SRobert Watson unp_drop(ref, ECONNRESET); 6480d9ce3a1SRobert Watson } 649df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 650fc3fcacfSRobert Watson unp->unp_socket->so_pcb = NULL; 651df8bae1dSRodney W. Grimes if (unp_rights) { 652df8bae1dSRodney W. Grimes /* 653df8bae1dSRodney W. Grimes * Normally the receive buffer is flushed later, 654df8bae1dSRodney W. Grimes * in sofree, but if our receive buffer holds references 655df8bae1dSRodney W. Grimes * to descriptors that are now garbage, we will dispose 656df8bae1dSRodney W. Grimes * of those descriptor references after the garbage collector 657df8bae1dSRodney W. Grimes * gets them (resulting in a "panic: closef: count < 0"). 658df8bae1dSRodney W. Grimes */ 659df8bae1dSRodney W. Grimes sorflush(unp->unp_socket); 660df8bae1dSRodney W. Grimes unp_gc(); 661df8bae1dSRodney W. Grimes } 662a5993a97SRobert Watson UNP_UNLOCK(); 663fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 66457bf258eSGarrett Wollman FREE(unp->unp_addr, M_SONAME); 6659e9d298aSJeff Roberson uma_zfree(unp_zone, unp); 6660d9ce3a1SRobert Watson if (vp) { 6670d9ce3a1SRobert Watson mtx_lock(&Giant); 6680d9ce3a1SRobert Watson vrele(vp); 6690d9ce3a1SRobert Watson mtx_unlock(&Giant); 6700d9ce3a1SRobert Watson } 671df8bae1dSRodney W. Grimes } 672df8bae1dSRodney W. Grimes 673f708ef1bSPoul-Henning Kamp static int 674b40ce416SJulian Elischer unp_bind(unp, nam, td) 675df8bae1dSRodney W. Grimes struct unpcb *unp; 67657bf258eSGarrett Wollman struct sockaddr *nam; 677b40ce416SJulian Elischer struct thread *td; 678df8bae1dSRodney W. Grimes { 67957bf258eSGarrett Wollman struct sockaddr_un *soun = (struct sockaddr_un *)nam; 680f2a2857bSKirk McKusick struct vnode *vp; 681f2a2857bSKirk McKusick struct mount *mp; 682df8bae1dSRodney W. Grimes struct vattr vattr; 68357bf258eSGarrett Wollman int error, namelen; 684df8bae1dSRodney W. Grimes struct nameidata nd; 6858f364875SJulian Elischer char *buf; 686df8bae1dSRodney W. Grimes 6870d9ce3a1SRobert Watson /* 6880d9ce3a1SRobert Watson * XXXRW: This test-and-set of unp_vnode is non-atomic; the 6890d9ce3a1SRobert Watson * unlocked read here is fine, but the value of unp_vnode needs 6900d9ce3a1SRobert Watson * to be tested again after we do all the lookups to see if the 6910d9ce3a1SRobert Watson * pcb is still unbound? 6920d9ce3a1SRobert Watson */ 693df8bae1dSRodney W. Grimes if (unp->unp_vnode != NULL) 694df8bae1dSRodney W. Grimes return (EINVAL); 69555c85568SRobert Drehmel 69657bf258eSGarrett Wollman namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 69757bf258eSGarrett Wollman if (namelen <= 0) 698e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 69955c85568SRobert Drehmel 700a163d034SWarner Losh buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 70155c85568SRobert Drehmel strlcpy(buf, soun->sun_path, namelen + 1); 70255c85568SRobert Drehmel 7030d9ce3a1SRobert Watson mtx_lock(&Giant); 704f2a2857bSKirk McKusick restart: 7050d9ce3a1SRobert Watson mtx_assert(&Giant, MA_OWNED); 706b65f6f6bSRobert Watson NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME, UIO_SYSSPACE, 707b40ce416SJulian Elischer buf, td); 708df8bae1dSRodney W. Grimes /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 709797f2d22SPoul-Henning Kamp error = namei(&nd); 7100d9ce3a1SRobert Watson if (error) 7110d9ce3a1SRobert Watson goto done; 712df8bae1dSRodney W. Grimes vp = nd.ni_vp; 713f2a2857bSKirk McKusick if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 714762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 715df8bae1dSRodney W. Grimes if (nd.ni_dvp == vp) 716df8bae1dSRodney W. Grimes vrele(nd.ni_dvp); 717df8bae1dSRodney W. Grimes else 718df8bae1dSRodney W. Grimes vput(nd.ni_dvp); 719f2a2857bSKirk McKusick if (vp != NULL) { 720df8bae1dSRodney W. Grimes vrele(vp); 7210d9ce3a1SRobert Watson error = EADDRINUSE; 7220d9ce3a1SRobert Watson goto done; 723df8bae1dSRodney W. Grimes } 7248f364875SJulian Elischer error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 7250d9ce3a1SRobert Watson if (error) 7260d9ce3a1SRobert Watson goto done; 727f2a2857bSKirk McKusick goto restart; 728f2a2857bSKirk McKusick } 729df8bae1dSRodney W. Grimes VATTR_NULL(&vattr); 730df8bae1dSRodney W. Grimes vattr.va_type = VSOCK; 731b40ce416SJulian Elischer vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 7326ea48a90SRobert Watson #ifdef MAC 7336ea48a90SRobert Watson error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 7346ea48a90SRobert Watson &vattr); 7356151efaaSRobert Watson #endif 7366ea48a90SRobert Watson if (error == 0) { 737a854ed98SJohn Baldwin VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE); 7387be2d300SMike Smith error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 7396ea48a90SRobert Watson } 740762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 7417be2d300SMike Smith vput(nd.ni_dvp); 7420d9ce3a1SRobert Watson if (error) 7430d9ce3a1SRobert Watson goto done; 744df8bae1dSRodney W. Grimes vp = nd.ni_vp; 7450d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_bind"); 7460d9ce3a1SRobert Watson soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 7470d9ce3a1SRobert Watson UNP_LOCK(); 748df8bae1dSRodney W. Grimes vp->v_socket = unp->unp_socket; 749df8bae1dSRodney W. Grimes unp->unp_vnode = vp; 7500d9ce3a1SRobert Watson unp->unp_addr = soun; 7510d9ce3a1SRobert Watson UNP_UNLOCK(); 752b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 753f2a2857bSKirk McKusick vn_finished_write(mp); 7540d9ce3a1SRobert Watson done: 7550d9ce3a1SRobert Watson mtx_unlock(&Giant); 7568f364875SJulian Elischer free(buf, M_TEMP); 7570d9ce3a1SRobert Watson return (error); 758df8bae1dSRodney W. Grimes } 759df8bae1dSRodney W. Grimes 760f708ef1bSPoul-Henning Kamp static int 761b40ce416SJulian Elischer unp_connect(so, nam, td) 762df8bae1dSRodney W. Grimes struct socket *so; 76357bf258eSGarrett Wollman struct sockaddr *nam; 764b40ce416SJulian Elischer struct thread *td; 765df8bae1dSRodney W. Grimes { 76657bf258eSGarrett Wollman register struct sockaddr_un *soun = (struct sockaddr_un *)nam; 767df8bae1dSRodney W. Grimes register struct vnode *vp; 768df8bae1dSRodney W. Grimes register struct socket *so2, *so3; 769b295bdcdSRobert Watson struct unpcb *unp, *unp2, *unp3; 77057bf258eSGarrett Wollman int error, len; 771df8bae1dSRodney W. Grimes struct nameidata nd; 77257bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 7730d9ce3a1SRobert Watson struct sockaddr *sa; 7740d9ce3a1SRobert Watson 7750d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 776b295bdcdSRobert Watson unp = sotounpcb(so); 777df8bae1dSRodney W. Grimes 77857bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 77957bf258eSGarrett Wollman if (len <= 0) 780e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 78155c85568SRobert Drehmel strlcpy(buf, soun->sun_path, len + 1); 7820d9ce3a1SRobert Watson UNP_UNLOCK(); 7830d9ce3a1SRobert Watson sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 7840d9ce3a1SRobert Watson mtx_lock(&Giant); 785b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td); 786797f2d22SPoul-Henning Kamp error = namei(&nd); 787797f2d22SPoul-Henning Kamp if (error) 7880d9ce3a1SRobert Watson vp = NULL; 7890d9ce3a1SRobert Watson else 790df8bae1dSRodney W. Grimes vp = nd.ni_vp; 7910d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_connect"); 792762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 7930d9ce3a1SRobert Watson if (error) 7940d9ce3a1SRobert Watson goto bad; 7950d9ce3a1SRobert Watson 796df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 797df8bae1dSRodney W. Grimes error = ENOTSOCK; 798df8bae1dSRodney W. Grimes goto bad; 799df8bae1dSRodney W. Grimes } 800a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 801797f2d22SPoul-Henning Kamp if (error) 802df8bae1dSRodney W. Grimes goto bad; 8032260c03dSRobert Watson mtx_unlock(&Giant); 8042260c03dSRobert Watson UNP_LOCK(); 805b295bdcdSRobert Watson unp = sotounpcb(so); 806b295bdcdSRobert Watson if (unp == NULL) { 807b295bdcdSRobert Watson /* 808b295bdcdSRobert Watson * XXXRW: Temporary debugging printf. 809b295bdcdSRobert Watson */ 810b295bdcdSRobert Watson printf("unp_connect(): lost race to another thread\n"); 811b295bdcdSRobert Watson error = EINVAL; 812b295bdcdSRobert Watson goto bad2; 813b295bdcdSRobert Watson } 814df8bae1dSRodney W. Grimes so2 = vp->v_socket; 815fc3fcacfSRobert Watson if (so2 == NULL) { 816df8bae1dSRodney W. Grimes error = ECONNREFUSED; 8172260c03dSRobert Watson goto bad2; 818df8bae1dSRodney W. Grimes } 819df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 820df8bae1dSRodney W. Grimes error = EPROTOTYPE; 8212260c03dSRobert Watson goto bad2; 822df8bae1dSRodney W. Grimes } 823df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 8240d9ce3a1SRobert Watson if (so2->so_options & SO_ACCEPTCONN) { 8250d9ce3a1SRobert Watson /* 8260d9ce3a1SRobert Watson * NB: drop locks here so unp_attach is entered 8270d9ce3a1SRobert Watson * w/o locks; this avoids a recursive lock 8280d9ce3a1SRobert Watson * of the head and holding sleep locks across 8290d9ce3a1SRobert Watson * a (potentially) blocking malloc. 8300d9ce3a1SRobert Watson */ 8310d9ce3a1SRobert Watson UNP_UNLOCK(); 8320d9ce3a1SRobert Watson so3 = sonewconn(so2, 0); 8330d9ce3a1SRobert Watson UNP_LOCK(); 8340d9ce3a1SRobert Watson } else 8350d9ce3a1SRobert Watson so3 = NULL; 8360d9ce3a1SRobert Watson if (so3 == NULL) { 837df8bae1dSRodney W. Grimes error = ECONNREFUSED; 8380d9ce3a1SRobert Watson goto bad2; 839df8bae1dSRodney W. Grimes } 8400c1bb4fbSDima Dorfman unp = sotounpcb(so); 841df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 842df8bae1dSRodney W. Grimes unp3 = sotounpcb(so3); 8430d9ce3a1SRobert Watson if (unp2->unp_addr != NULL) { 8440d9ce3a1SRobert Watson bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 8450d9ce3a1SRobert Watson unp3->unp_addr = (struct sockaddr_un *) sa; 8460d9ce3a1SRobert Watson sa = NULL; 8470d9ce3a1SRobert Watson } 8480c1bb4fbSDima Dorfman /* 8490c1bb4fbSDima Dorfman * unp_peercred management: 8500c1bb4fbSDima Dorfman * 8510c1bb4fbSDima Dorfman * The connecter's (client's) credentials are copied 8520c1bb4fbSDima Dorfman * from its process structure at the time of connect() 8530c1bb4fbSDima Dorfman * (which is now). 8540c1bb4fbSDima Dorfman */ 855a854ed98SJohn Baldwin cru2x(td->td_ucred, &unp3->unp_peercred); 8560c1bb4fbSDima Dorfman unp3->unp_flags |= UNP_HAVEPC; 8570c1bb4fbSDima Dorfman /* 8580c1bb4fbSDima Dorfman * The receiver's (server's) credentials are copied 8590c1bb4fbSDima Dorfman * from the unp_peercred member of socket on which the 8600c1bb4fbSDima Dorfman * former called listen(); unp_listen() cached that 8610c1bb4fbSDima Dorfman * process's credentials at that time so we can use 8620c1bb4fbSDima Dorfman * them now. 8630c1bb4fbSDima Dorfman */ 8640c1bb4fbSDima Dorfman KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 8650c1bb4fbSDima Dorfman ("unp_connect: listener without cached peercred")); 8660c1bb4fbSDima Dorfman memcpy(&unp->unp_peercred, &unp2->unp_peercred, 8670c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 8680c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPC; 869335654d7SRobert Watson #ifdef MAC 870310e7cebSRobert Watson SOCK_LOCK(so); 871335654d7SRobert Watson mac_set_socket_peer_from_socket(so, so3); 872335654d7SRobert Watson mac_set_socket_peer_from_socket(so3, so); 873310e7cebSRobert Watson SOCK_UNLOCK(so); 874335654d7SRobert Watson #endif 8750c1bb4fbSDima Dorfman 876df8bae1dSRodney W. Grimes so2 = so3; 877df8bae1dSRodney W. Grimes } 878df8bae1dSRodney W. Grimes error = unp_connect2(so, so2); 8790d9ce3a1SRobert Watson bad2: 8800d9ce3a1SRobert Watson UNP_UNLOCK(); 8810d9ce3a1SRobert Watson mtx_lock(&Giant); 882df8bae1dSRodney W. Grimes bad: 8830d9ce3a1SRobert Watson mtx_assert(&Giant, MA_OWNED); 8840d9ce3a1SRobert Watson if (vp != NULL) 885df8bae1dSRodney W. Grimes vput(vp); 8860d9ce3a1SRobert Watson mtx_unlock(&Giant); 8870d9ce3a1SRobert Watson free(sa, M_SONAME); 8880d9ce3a1SRobert Watson UNP_LOCK(); 889df8bae1dSRodney W. Grimes return (error); 890df8bae1dSRodney W. Grimes } 891df8bae1dSRodney W. Grimes 892db48c0d2SRobert Watson static int 893df8bae1dSRodney W. Grimes unp_connect2(so, so2) 894df8bae1dSRodney W. Grimes register struct socket *so; 895df8bae1dSRodney W. Grimes register struct socket *so2; 896df8bae1dSRodney W. Grimes { 897df8bae1dSRodney W. Grimes register struct unpcb *unp = sotounpcb(so); 898df8bae1dSRodney W. Grimes register struct unpcb *unp2; 899df8bae1dSRodney W. Grimes 9000d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 9010d9ce3a1SRobert Watson 902df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 903df8bae1dSRodney W. Grimes return (EPROTOTYPE); 904df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 905df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 906df8bae1dSRodney W. Grimes switch (so->so_type) { 907df8bae1dSRodney W. Grimes 908df8bae1dSRodney W. Grimes case SOCK_DGRAM: 90998271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 910df8bae1dSRodney W. Grimes soisconnected(so); 911df8bae1dSRodney W. Grimes break; 912df8bae1dSRodney W. Grimes 913df8bae1dSRodney W. Grimes case SOCK_STREAM: 914df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 915df8bae1dSRodney W. Grimes soisconnected(so); 916df8bae1dSRodney W. Grimes soisconnected(so2); 917df8bae1dSRodney W. Grimes break; 918df8bae1dSRodney W. Grimes 919df8bae1dSRodney W. Grimes default: 920df8bae1dSRodney W. Grimes panic("unp_connect2"); 921df8bae1dSRodney W. Grimes } 922df8bae1dSRodney W. Grimes return (0); 923df8bae1dSRodney W. Grimes } 924df8bae1dSRodney W. Grimes 925f708ef1bSPoul-Henning Kamp static void 926df8bae1dSRodney W. Grimes unp_disconnect(unp) 927df8bae1dSRodney W. Grimes struct unpcb *unp; 928df8bae1dSRodney W. Grimes { 929df8bae1dSRodney W. Grimes register struct unpcb *unp2 = unp->unp_conn; 9301b2e3b4bSRobert Watson struct socket *so; 931df8bae1dSRodney W. Grimes 9320d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 9330d9ce3a1SRobert Watson 934fc3fcacfSRobert Watson if (unp2 == NULL) 935df8bae1dSRodney W. Grimes return; 936fc3fcacfSRobert Watson unp->unp_conn = NULL; 937df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 938df8bae1dSRodney W. Grimes 939df8bae1dSRodney W. Grimes case SOCK_DGRAM: 94098271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 9411b2e3b4bSRobert Watson so = unp->unp_socket; 9421b2e3b4bSRobert Watson SOCK_LOCK(so); 9431b2e3b4bSRobert Watson so->so_state &= ~SS_ISCONNECTED; 9441b2e3b4bSRobert Watson SOCK_UNLOCK(so); 945df8bae1dSRodney W. Grimes break; 946df8bae1dSRodney W. Grimes 947df8bae1dSRodney W. Grimes case SOCK_STREAM: 948df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 949fc3fcacfSRobert Watson unp2->unp_conn = NULL; 950df8bae1dSRodney W. Grimes soisdisconnected(unp2->unp_socket); 951df8bae1dSRodney W. Grimes break; 952df8bae1dSRodney W. Grimes } 953df8bae1dSRodney W. Grimes } 954df8bae1dSRodney W. Grimes 955df8bae1dSRodney W. Grimes #ifdef notdef 95626f9a767SRodney W. Grimes void 957df8bae1dSRodney W. Grimes unp_abort(unp) 958df8bae1dSRodney W. Grimes struct unpcb *unp; 959df8bae1dSRodney W. Grimes { 960df8bae1dSRodney W. Grimes 961df8bae1dSRodney W. Grimes unp_detach(unp); 962df8bae1dSRodney W. Grimes } 963df8bae1dSRodney W. Grimes #endif 964df8bae1dSRodney W. Grimes 9650d9ce3a1SRobert Watson /* 9660d9ce3a1SRobert Watson * unp_pcblist() assumes that UNIX domain socket memory is never reclaimed 9670d9ce3a1SRobert Watson * by the zone (UMA_ZONE_NOFREE), and as such potentially stale pointers 9680d9ce3a1SRobert Watson * are safe to reference. It first scans the list of struct unpcb's to 9690d9ce3a1SRobert Watson * generate a pointer list, then it rescans its list one entry at a time to 9700d9ce3a1SRobert Watson * externalize and copyout. It checks the generation number to see if a 9710d9ce3a1SRobert Watson * struct unpcb has been reused, and will skip it if so. 9720d9ce3a1SRobert Watson */ 97398271db4SGarrett Wollman static int 97482d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 97598271db4SGarrett Wollman { 976f5ef029eSPoul-Henning Kamp int error, i, n; 97798271db4SGarrett Wollman struct unpcb *unp, **unp_list; 97898271db4SGarrett Wollman unp_gen_t gencnt; 9798f364875SJulian Elischer struct xunpgen *xug; 98098271db4SGarrett Wollman struct unp_head *head; 9818f364875SJulian Elischer struct xunpcb *xu; 98298271db4SGarrett Wollman 983a23d65bfSBruce Evans head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 98498271db4SGarrett Wollman 98598271db4SGarrett Wollman /* 98698271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 98798271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 98898271db4SGarrett Wollman */ 989fc3fcacfSRobert Watson if (req->oldptr == NULL) { 99098271db4SGarrett Wollman n = unp_count; 9918f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug) 99298271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 993e5aeaa0cSDag-Erling Smørgrav return (0); 99498271db4SGarrett Wollman } 99598271db4SGarrett Wollman 996fc3fcacfSRobert Watson if (req->newptr != NULL) 997e5aeaa0cSDag-Erling Smørgrav return (EPERM); 99898271db4SGarrett Wollman 99998271db4SGarrett Wollman /* 100098271db4SGarrett Wollman * OK, now we're committed to doing something. 100198271db4SGarrett Wollman */ 1002a163d034SWarner Losh xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 10030d9ce3a1SRobert Watson UNP_LOCK(); 100498271db4SGarrett Wollman gencnt = unp_gencnt; 100598271db4SGarrett Wollman n = unp_count; 10060d9ce3a1SRobert Watson UNP_UNLOCK(); 100798271db4SGarrett Wollman 10088f364875SJulian Elischer xug->xug_len = sizeof *xug; 10098f364875SJulian Elischer xug->xug_count = n; 10108f364875SJulian Elischer xug->xug_gen = gencnt; 10118f364875SJulian Elischer xug->xug_sogen = so_gencnt; 10128f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 10138f364875SJulian Elischer if (error) { 10148f364875SJulian Elischer free(xug, M_TEMP); 1015e5aeaa0cSDag-Erling Smørgrav return (error); 10168f364875SJulian Elischer } 101798271db4SGarrett Wollman 1018a163d034SWarner Losh unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 101998271db4SGarrett Wollman 10200d9ce3a1SRobert Watson UNP_LOCK(); 10212e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 10222e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 10238a7d8cc6SRobert Watson if (unp->unp_gencnt <= gencnt) { 1024a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred, 10258a7d8cc6SRobert Watson unp->unp_socket->so_cred)) 10264787fd37SPaul Saab continue; 102798271db4SGarrett Wollman unp_list[i++] = unp; 102898271db4SGarrett Wollman } 10294787fd37SPaul Saab } 10300d9ce3a1SRobert Watson UNP_UNLOCK(); 103198271db4SGarrett Wollman n = i; /* in case we lost some during malloc */ 103298271db4SGarrett Wollman 103398271db4SGarrett Wollman error = 0; 1034a163d034SWarner Losh xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK); 103598271db4SGarrett Wollman for (i = 0; i < n; i++) { 103698271db4SGarrett Wollman unp = unp_list[i]; 103798271db4SGarrett Wollman if (unp->unp_gencnt <= gencnt) { 10388f364875SJulian Elischer xu->xu_len = sizeof *xu; 10398f364875SJulian Elischer xu->xu_unpp = unp; 104098271db4SGarrett Wollman /* 104198271db4SGarrett Wollman * XXX - need more locking here to protect against 104298271db4SGarrett Wollman * connect/disconnect races for SMP. 104398271db4SGarrett Wollman */ 1044fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 10458f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr, 104698271db4SGarrett Wollman unp->unp_addr->sun_len); 1047fc3fcacfSRobert Watson if (unp->unp_conn != NULL && 1048fc3fcacfSRobert Watson unp->unp_conn->unp_addr != NULL) 104998271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 10508f364875SJulian Elischer &xu->xu_caddr, 105198271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 10528f364875SJulian Elischer bcopy(unp, &xu->xu_unp, sizeof *unp); 10538f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket); 10548f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu); 105598271db4SGarrett Wollman } 105698271db4SGarrett Wollman } 10578f364875SJulian Elischer free(xu, M_TEMP); 105898271db4SGarrett Wollman if (!error) { 105998271db4SGarrett Wollman /* 106098271db4SGarrett Wollman * Give the user an updated idea of our state. 106198271db4SGarrett Wollman * If the generation differs from what we told 106298271db4SGarrett Wollman * her before, she knows that something happened 106398271db4SGarrett Wollman * while we were processing this request, and it 106498271db4SGarrett Wollman * might be necessary to retry. 106598271db4SGarrett Wollman */ 10668f364875SJulian Elischer xug->xug_gen = unp_gencnt; 10678f364875SJulian Elischer xug->xug_sogen = so_gencnt; 10688f364875SJulian Elischer xug->xug_count = unp_count; 10698f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 107098271db4SGarrett Wollman } 107198271db4SGarrett Wollman free(unp_list, M_TEMP); 10728f364875SJulian Elischer free(xug, M_TEMP); 1073e5aeaa0cSDag-Erling Smørgrav return (error); 107498271db4SGarrett Wollman } 107598271db4SGarrett Wollman 107698271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 107798271db4SGarrett Wollman (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 107898271db4SGarrett Wollman "List of active local datagram sockets"); 107998271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 108098271db4SGarrett Wollman (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 108198271db4SGarrett Wollman "List of active local stream sockets"); 108298271db4SGarrett Wollman 1083f708ef1bSPoul-Henning Kamp static void 1084df8bae1dSRodney W. Grimes unp_shutdown(unp) 1085df8bae1dSRodney W. Grimes struct unpcb *unp; 1086df8bae1dSRodney W. Grimes { 1087df8bae1dSRodney W. Grimes struct socket *so; 1088df8bae1dSRodney W. Grimes 10890d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 10900d9ce3a1SRobert Watson 1091df8bae1dSRodney W. Grimes if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 1092df8bae1dSRodney W. Grimes (so = unp->unp_conn->unp_socket)) 1093df8bae1dSRodney W. Grimes socantrcvmore(so); 1094df8bae1dSRodney W. Grimes } 1095df8bae1dSRodney W. Grimes 1096f708ef1bSPoul-Henning Kamp static void 1097df8bae1dSRodney W. Grimes unp_drop(unp, errno) 1098df8bae1dSRodney W. Grimes struct unpcb *unp; 1099df8bae1dSRodney W. Grimes int errno; 1100df8bae1dSRodney W. Grimes { 1101df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 1102df8bae1dSRodney W. Grimes 11030d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 11040d9ce3a1SRobert Watson 1105df8bae1dSRodney W. Grimes so->so_error = errno; 1106df8bae1dSRodney W. Grimes unp_disconnect(unp); 1107df8bae1dSRodney W. Grimes } 1108df8bae1dSRodney W. Grimes 1109df8bae1dSRodney W. Grimes #ifdef notdef 111026f9a767SRodney W. Grimes void 1111df8bae1dSRodney W. Grimes unp_drain() 1112df8bae1dSRodney W. Grimes { 1113df8bae1dSRodney W. Grimes 1114df8bae1dSRodney W. Grimes } 1115df8bae1dSRodney W. Grimes #endif 1116df8bae1dSRodney W. Grimes 11172bc21ed9SDavid Malone static void 11182bc21ed9SDavid Malone unp_freerights(rp, fdcount) 11192bc21ed9SDavid Malone struct file **rp; 11202bc21ed9SDavid Malone int fdcount; 1121df8bae1dSRodney W. Grimes { 11222bc21ed9SDavid Malone int i; 11232bc21ed9SDavid Malone struct file *fp; 1124df8bae1dSRodney W. Grimes 11252bc21ed9SDavid Malone for (i = 0; i < fdcount; i++) { 1126df8bae1dSRodney W. Grimes fp = *rp; 11278692c025SYoshinobu Inoue /* 11282bc21ed9SDavid Malone * zero the pointer before calling 11292bc21ed9SDavid Malone * unp_discard since it may end up 11302bc21ed9SDavid Malone * in unp_gc().. 11318692c025SYoshinobu Inoue */ 1132df8bae1dSRodney W. Grimes *rp++ = 0; 11338692c025SYoshinobu Inoue unp_discard(fp); 1134df8bae1dSRodney W. Grimes } 11352bc21ed9SDavid Malone } 11362bc21ed9SDavid Malone 11372bc21ed9SDavid Malone int 11382bc21ed9SDavid Malone unp_externalize(control, controlp) 11392bc21ed9SDavid Malone struct mbuf *control, **controlp; 11402bc21ed9SDavid Malone { 11412bc21ed9SDavid Malone struct thread *td = curthread; /* XXX */ 11422bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 11432bc21ed9SDavid Malone int i; 11442bc21ed9SDavid Malone int *fdp; 11452bc21ed9SDavid Malone struct file **rp; 11462bc21ed9SDavid Malone struct file *fp; 11472bc21ed9SDavid Malone void *data; 11482bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 11492bc21ed9SDavid Malone int error, newfds; 11502bc21ed9SDavid Malone int f; 11512bc21ed9SDavid Malone u_int newlen; 11522bc21ed9SDavid Malone 11532bc21ed9SDavid Malone error = 0; 11542bc21ed9SDavid Malone if (controlp != NULL) /* controlp == NULL => free control messages */ 11552bc21ed9SDavid Malone *controlp = NULL; 11562bc21ed9SDavid Malone 11572bc21ed9SDavid Malone while (cm != NULL) { 11582bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 11592bc21ed9SDavid Malone error = EINVAL; 11602bc21ed9SDavid Malone break; 11612bc21ed9SDavid Malone } 11622bc21ed9SDavid Malone 11632bc21ed9SDavid Malone data = CMSG_DATA(cm); 11642bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 11652bc21ed9SDavid Malone 11662bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET 11672bc21ed9SDavid Malone && cm->cmsg_type == SCM_RIGHTS) { 11682bc21ed9SDavid Malone newfds = datalen / sizeof(struct file *); 11692bc21ed9SDavid Malone rp = data; 11702bc21ed9SDavid Malone 1171e2f9a08bSOlivier Houchard /* If we're not outputting the descriptors free them. */ 11722bc21ed9SDavid Malone if (error || controlp == NULL) { 11732bc21ed9SDavid Malone unp_freerights(rp, newfds); 11742bc21ed9SDavid Malone goto next; 11752bc21ed9SDavid Malone } 1176426da3bcSAlfred Perlstein FILEDESC_LOCK(td->td_proc->p_fd); 11772bc21ed9SDavid Malone /* if the new FD's will not fit free them. */ 11782bc21ed9SDavid Malone if (!fdavail(td, newfds)) { 1179426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 11802bc21ed9SDavid Malone error = EMSGSIZE; 11812bc21ed9SDavid Malone unp_freerights(rp, newfds); 11822bc21ed9SDavid Malone goto next; 1183df8bae1dSRodney W. Grimes } 1184ed5b7817SJulian Elischer /* 11852bc21ed9SDavid Malone * now change each pointer to an fd in the global 11862bc21ed9SDavid Malone * table to an integer that is the index to the 11872bc21ed9SDavid Malone * local fd table entry that we set up to point 11882bc21ed9SDavid Malone * to the global one we are transferring. 1189ed5b7817SJulian Elischer */ 11902bc21ed9SDavid Malone newlen = newfds * sizeof(int); 11912bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 11922bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 11932bc21ed9SDavid Malone if (*controlp == NULL) { 1194426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 11952bc21ed9SDavid Malone error = E2BIG; 11962bc21ed9SDavid Malone unp_freerights(rp, newfds); 11972bc21ed9SDavid Malone goto next; 11982bc21ed9SDavid Malone } 11992bc21ed9SDavid Malone 12002bc21ed9SDavid Malone fdp = (int *) 12012bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1202df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 1203a6d4491cSDag-Erling Smørgrav if (fdalloc(td, 0, &f)) 12042bc21ed9SDavid Malone panic("unp_externalize fdalloc failed"); 12058692c025SYoshinobu Inoue fp = *rp++; 1206b40ce416SJulian Elischer td->td_proc->p_fd->fd_ofiles[f] = fp; 1207426da3bcSAlfred Perlstein FILE_LOCK(fp); 1208df8bae1dSRodney W. Grimes fp->f_msgcount--; 1209426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1210df8bae1dSRodney W. Grimes unp_rights--; 12118692c025SYoshinobu Inoue *fdp++ = f; 1212df8bae1dSRodney W. Grimes } 1213426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 12142bc21ed9SDavid Malone } else { /* We can just copy anything else across */ 12152bc21ed9SDavid Malone if (error || controlp == NULL) 12162bc21ed9SDavid Malone goto next; 12172bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, datalen, 12182bc21ed9SDavid Malone cm->cmsg_type, cm->cmsg_level); 12192bc21ed9SDavid Malone if (*controlp == NULL) { 12202bc21ed9SDavid Malone error = ENOBUFS; 12212bc21ed9SDavid Malone goto next; 12222bc21ed9SDavid Malone } 12232bc21ed9SDavid Malone bcopy(data, 12242bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 12252bc21ed9SDavid Malone datalen); 12262bc21ed9SDavid Malone } 12272bc21ed9SDavid Malone 12282bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 12292bc21ed9SDavid Malone 12302bc21ed9SDavid Malone next: 12312bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 12322bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 12332bc21ed9SDavid Malone cm = (struct cmsghdr *) 12342bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 12358692c025SYoshinobu Inoue } else { 12362bc21ed9SDavid Malone clen = 0; 12372bc21ed9SDavid Malone cm = NULL; 12388692c025SYoshinobu Inoue } 12398692c025SYoshinobu Inoue } 12408692c025SYoshinobu Inoue 12412bc21ed9SDavid Malone m_freem(control); 12422bc21ed9SDavid Malone 12432bc21ed9SDavid Malone return (error); 1244df8bae1dSRodney W. Grimes } 1245df8bae1dSRodney W. Grimes 124698271db4SGarrett Wollman void 124798271db4SGarrett Wollman unp_init(void) 124898271db4SGarrett Wollman { 12499e9d298aSJeff Roberson unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 12509e9d298aSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 1251fc3fcacfSRobert Watson if (unp_zone == NULL) 125298271db4SGarrett Wollman panic("unp_init"); 1253b17dd2bcSColin Percival uma_zone_set_max(unp_zone, nmbclusters); 125498271db4SGarrett Wollman LIST_INIT(&unp_dhead); 125598271db4SGarrett Wollman LIST_INIT(&unp_shead); 12560d9ce3a1SRobert Watson 12570d9ce3a1SRobert Watson UNP_LOCK_INIT(); 125898271db4SGarrett Wollman } 125998271db4SGarrett Wollman 1260f708ef1bSPoul-Henning Kamp static int 12612bc21ed9SDavid Malone unp_internalize(controlp, td) 12622bc21ed9SDavid Malone struct mbuf **controlp; 1263b40ce416SJulian Elischer struct thread *td; 1264df8bae1dSRodney W. Grimes { 12652bc21ed9SDavid Malone struct mbuf *control = *controlp; 1266b40ce416SJulian Elischer struct proc *p = td->td_proc; 12678692c025SYoshinobu Inoue struct filedesc *fdescp = p->p_fd; 12682bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 12692bc21ed9SDavid Malone struct cmsgcred *cmcred; 12702bc21ed9SDavid Malone struct file **rp; 12712bc21ed9SDavid Malone struct file *fp; 12722bc21ed9SDavid Malone struct timeval *tv; 12732bc21ed9SDavid Malone int i, fd, *fdp; 12742bc21ed9SDavid Malone void *data; 12752bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 12762bc21ed9SDavid Malone int error, oldfds; 12778692c025SYoshinobu Inoue u_int newlen; 1278df8bae1dSRodney W. Grimes 12792bc21ed9SDavid Malone error = 0; 12802bc21ed9SDavid Malone *controlp = NULL; 12810b788fa1SBill Paul 12822bc21ed9SDavid Malone while (cm != NULL) { 12832bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 12842bc21ed9SDavid Malone || cm->cmsg_len > clen) { 12852bc21ed9SDavid Malone error = EINVAL; 12862bc21ed9SDavid Malone goto out; 12872bc21ed9SDavid Malone } 12882bc21ed9SDavid Malone 12892bc21ed9SDavid Malone data = CMSG_DATA(cm); 12902bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 12912bc21ed9SDavid Malone 12922bc21ed9SDavid Malone switch (cm->cmsg_type) { 12930b788fa1SBill Paul /* 12940b788fa1SBill Paul * Fill in credential information. 12950b788fa1SBill Paul */ 12962bc21ed9SDavid Malone case SCM_CREDS: 12972bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 12982bc21ed9SDavid Malone SCM_CREDS, SOL_SOCKET); 12992bc21ed9SDavid Malone if (*controlp == NULL) { 13002bc21ed9SDavid Malone error = ENOBUFS; 13012bc21ed9SDavid Malone goto out; 13022bc21ed9SDavid Malone } 13032bc21ed9SDavid Malone 13042bc21ed9SDavid Malone cmcred = (struct cmsgcred *) 13052bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 13060b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 1307a854ed98SJohn Baldwin cmcred->cmcred_uid = td->td_ucred->cr_ruid; 1308a854ed98SJohn Baldwin cmcred->cmcred_gid = td->td_ucred->cr_rgid; 1309a854ed98SJohn Baldwin cmcred->cmcred_euid = td->td_ucred->cr_uid; 1310a854ed98SJohn Baldwin cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 13110b788fa1SBill Paul CMGROUP_MAX); 13120b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 13132bc21ed9SDavid Malone cmcred->cmcred_groups[i] = 1314a854ed98SJohn Baldwin td->td_ucred->cr_groups[i]; 13152bc21ed9SDavid Malone break; 13160b788fa1SBill Paul 13172bc21ed9SDavid Malone case SCM_RIGHTS: 13182bc21ed9SDavid Malone oldfds = datalen / sizeof (int); 1319ed5b7817SJulian Elischer /* 13202bc21ed9SDavid Malone * check that all the FDs passed in refer to legal files 1321ed5b7817SJulian Elischer * If not, reject the entire operation. 1322ed5b7817SJulian Elischer */ 13232bc21ed9SDavid Malone fdp = data; 1324426da3bcSAlfred Perlstein FILEDESC_LOCK(fdescp); 1325df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 13268692c025SYoshinobu Inoue fd = *fdp++; 13278692c025SYoshinobu Inoue if ((unsigned)fd >= fdescp->fd_nfiles || 13282bc21ed9SDavid Malone fdescp->fd_ofiles[fd] == NULL) { 1329426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 13302bc21ed9SDavid Malone error = EBADF; 13312bc21ed9SDavid Malone goto out; 13322bc21ed9SDavid Malone } 1333e7d6662fSAlfred Perlstein fp = fdescp->fd_ofiles[fd]; 1334e7d6662fSAlfred Perlstein if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 1335e7d6662fSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 1336e7d6662fSAlfred Perlstein error = EOPNOTSUPP; 1337e7d6662fSAlfred Perlstein goto out; 1338e7d6662fSAlfred Perlstein } 1339e7d6662fSAlfred Perlstein 1340df8bae1dSRodney W. Grimes } 1341ed5b7817SJulian Elischer /* 1342ed5b7817SJulian Elischer * Now replace the integer FDs with pointers to 1343ed5b7817SJulian Elischer * the associated global file table entry.. 1344ed5b7817SJulian Elischer */ 13452bc21ed9SDavid Malone newlen = oldfds * sizeof(struct file *); 13462bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 13472bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 13482bc21ed9SDavid Malone if (*controlp == NULL) { 1349426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 13502bc21ed9SDavid Malone error = E2BIG; 13512bc21ed9SDavid Malone goto out; 13528692c025SYoshinobu Inoue } 13538692c025SYoshinobu Inoue 13542bc21ed9SDavid Malone fdp = data; 13552bc21ed9SDavid Malone rp = (struct file **) 13562bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 13578692c025SYoshinobu Inoue for (i = 0; i < oldfds; i++) { 13588692c025SYoshinobu Inoue fp = fdescp->fd_ofiles[*fdp++]; 1359df8bae1dSRodney W. Grimes *rp++ = fp; 1360426da3bcSAlfred Perlstein FILE_LOCK(fp); 1361df8bae1dSRodney W. Grimes fp->f_count++; 1362df8bae1dSRodney W. Grimes fp->f_msgcount++; 1363426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1364df8bae1dSRodney W. Grimes unp_rights++; 1365df8bae1dSRodney W. Grimes } 1366426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 13672bc21ed9SDavid Malone break; 13682bc21ed9SDavid Malone 13692bc21ed9SDavid Malone case SCM_TIMESTAMP: 13702bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*tv), 13712bc21ed9SDavid Malone SCM_TIMESTAMP, SOL_SOCKET); 13722bc21ed9SDavid Malone if (*controlp == NULL) { 13732bc21ed9SDavid Malone error = ENOBUFS; 13742bc21ed9SDavid Malone goto out; 13758692c025SYoshinobu Inoue } 13762bc21ed9SDavid Malone tv = (struct timeval *) 13772bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 13782bc21ed9SDavid Malone microtime(tv); 13792bc21ed9SDavid Malone break; 13802bc21ed9SDavid Malone 13812bc21ed9SDavid Malone default: 13822bc21ed9SDavid Malone error = EINVAL; 13832bc21ed9SDavid Malone goto out; 13842bc21ed9SDavid Malone } 13852bc21ed9SDavid Malone 13862bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 13872bc21ed9SDavid Malone 13882bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 13892bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 13902bc21ed9SDavid Malone cm = (struct cmsghdr *) 13912bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 13922bc21ed9SDavid Malone } else { 13932bc21ed9SDavid Malone clen = 0; 13942bc21ed9SDavid Malone cm = NULL; 13952bc21ed9SDavid Malone } 13962bc21ed9SDavid Malone } 13972bc21ed9SDavid Malone 13982bc21ed9SDavid Malone out: 13992bc21ed9SDavid Malone m_freem(control); 14002bc21ed9SDavid Malone 14012bc21ed9SDavid Malone return (error); 1402df8bae1dSRodney W. Grimes } 1403df8bae1dSRodney W. Grimes 1404f708ef1bSPoul-Henning Kamp static int unp_defer, unp_gcing; 1405df8bae1dSRodney W. Grimes 1406f708ef1bSPoul-Henning Kamp static void 1407df8bae1dSRodney W. Grimes unp_gc() 1408df8bae1dSRodney W. Grimes { 1409df8bae1dSRodney W. Grimes register struct file *fp, *nextfp; 1410df8bae1dSRodney W. Grimes register struct socket *so; 1411df8bae1dSRodney W. Grimes struct file **extra_ref, **fpp; 1412df8bae1dSRodney W. Grimes int nunref, i; 141395f004dcSAlfred Perlstein int nfiles_snap; 141495f004dcSAlfred Perlstein int nfiles_slack = 20; 1415df8bae1dSRodney W. Grimes 14160d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 14170d9ce3a1SRobert Watson 1418df8bae1dSRodney W. Grimes if (unp_gcing) 1419df8bae1dSRodney W. Grimes return; 1420df8bae1dSRodney W. Grimes unp_gcing = 1; 1421df8bae1dSRodney W. Grimes unp_defer = 0; 1422ed5b7817SJulian Elischer /* 1423ed5b7817SJulian Elischer * before going through all this, set all FDs to 1424ed5b7817SJulian Elischer * be NOT defered and NOT externally accessible 1425ed5b7817SJulian Elischer */ 14260d9ce3a1SRobert Watson /* 14270d9ce3a1SRobert Watson * XXXRW: Acquiring a sleep lock while holding UNP 14280d9ce3a1SRobert Watson * mutex cannot be a good thing. 14290d9ce3a1SRobert Watson */ 1430426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 14312e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) 1432426da3bcSAlfred Perlstein fp->f_gcflag &= ~(FMARK|FDEFER); 1433df8bae1dSRodney W. Grimes do { 14342e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) { 1435426da3bcSAlfred Perlstein FILE_LOCK(fp); 1436ed5b7817SJulian Elischer /* 1437ed5b7817SJulian Elischer * If the file is not open, skip it 1438ed5b7817SJulian Elischer */ 1439426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1440426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1441df8bae1dSRodney W. Grimes continue; 1442426da3bcSAlfred Perlstein } 1443ed5b7817SJulian Elischer /* 1444ed5b7817SJulian Elischer * If we already marked it as 'defer' in a 1445ed5b7817SJulian Elischer * previous pass, then try process it this time 1446ed5b7817SJulian Elischer * and un-mark it 1447ed5b7817SJulian Elischer */ 1448426da3bcSAlfred Perlstein if (fp->f_gcflag & FDEFER) { 1449426da3bcSAlfred Perlstein fp->f_gcflag &= ~FDEFER; 1450df8bae1dSRodney W. Grimes unp_defer--; 1451df8bae1dSRodney W. Grimes } else { 1452ed5b7817SJulian Elischer /* 1453ed5b7817SJulian Elischer * if it's not defered, then check if it's 1454ed5b7817SJulian Elischer * already marked.. if so skip it 1455ed5b7817SJulian Elischer */ 1456426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) { 1457426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1458df8bae1dSRodney W. Grimes continue; 1459426da3bcSAlfred Perlstein } 1460ed5b7817SJulian Elischer /* 1461ed5b7817SJulian Elischer * If all references are from messages 1462ed5b7817SJulian Elischer * in transit, then skip it. it's not 1463ed5b7817SJulian Elischer * externally accessible. 1464ed5b7817SJulian Elischer */ 1465426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount) { 1466426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1467df8bae1dSRodney W. Grimes continue; 1468426da3bcSAlfred Perlstein } 1469ed5b7817SJulian Elischer /* 1470ed5b7817SJulian Elischer * If it got this far then it must be 1471ed5b7817SJulian Elischer * externally accessible. 1472ed5b7817SJulian Elischer */ 1473426da3bcSAlfred Perlstein fp->f_gcflag |= FMARK; 1474df8bae1dSRodney W. Grimes } 1475ed5b7817SJulian Elischer /* 1476ed5b7817SJulian Elischer * either it was defered, or it is externally 1477ed5b7817SJulian Elischer * accessible and not already marked so. 1478ed5b7817SJulian Elischer * Now check if it is possibly one of OUR sockets. 1479ed5b7817SJulian Elischer */ 1480df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_SOCKET || 148148e3128bSMatthew Dillon (so = fp->f_data) == NULL) { 1482426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1483df8bae1dSRodney W. Grimes continue; 1484426da3bcSAlfred Perlstein } 1485426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1486748e0b0aSGarrett Wollman if (so->so_proto->pr_domain != &localdomain || 1487df8bae1dSRodney W. Grimes (so->so_proto->pr_flags&PR_RIGHTS) == 0) 1488df8bae1dSRodney W. Grimes continue; 1489df8bae1dSRodney W. Grimes #ifdef notdef 1490df8bae1dSRodney W. Grimes if (so->so_rcv.sb_flags & SB_LOCK) { 1491df8bae1dSRodney W. Grimes /* 1492df8bae1dSRodney W. Grimes * This is problematical; it's not clear 1493df8bae1dSRodney W. Grimes * we need to wait for the sockbuf to be 1494df8bae1dSRodney W. Grimes * unlocked (on a uniprocessor, at least), 1495df8bae1dSRodney W. Grimes * and it's also not clear what to do 1496df8bae1dSRodney W. Grimes * if sbwait returns an error due to receipt 1497df8bae1dSRodney W. Grimes * of a signal. If sbwait does return 1498df8bae1dSRodney W. Grimes * an error, we'll go into an infinite 1499df8bae1dSRodney W. Grimes * loop. Delete all of this for now. 1500df8bae1dSRodney W. Grimes */ 1501df8bae1dSRodney W. Grimes (void) sbwait(&so->so_rcv); 1502df8bae1dSRodney W. Grimes goto restart; 1503df8bae1dSRodney W. Grimes } 1504df8bae1dSRodney W. Grimes #endif 1505ed5b7817SJulian Elischer /* 1506ed5b7817SJulian Elischer * So, Ok, it's one of our sockets and it IS externally 1507ed5b7817SJulian Elischer * accessible (or was defered). Now we look 1508dc733423SDag-Erling Smørgrav * to see if we hold any file descriptors in its 1509ed5b7817SJulian Elischer * message buffers. Follow those links and mark them 1510ed5b7817SJulian Elischer * as accessible too. 1511ed5b7817SJulian Elischer */ 15127717cf07SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1513df8bae1dSRodney W. Grimes unp_scan(so->so_rcv.sb_mb, unp_mark); 15147717cf07SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 1515df8bae1dSRodney W. Grimes } 1516df8bae1dSRodney W. Grimes } while (unp_defer); 1517426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1518df8bae1dSRodney W. Grimes /* 1519df8bae1dSRodney W. Grimes * We grab an extra reference to each of the file table entries 1520df8bae1dSRodney W. Grimes * that are not otherwise accessible and then free the rights 1521df8bae1dSRodney W. Grimes * that are stored in messages on them. 1522df8bae1dSRodney W. Grimes * 1523df8bae1dSRodney W. Grimes * The bug in the orginal code is a little tricky, so I'll describe 1524df8bae1dSRodney W. Grimes * what's wrong with it here. 1525df8bae1dSRodney W. Grimes * 1526df8bae1dSRodney W. Grimes * It is incorrect to simply unp_discard each entry for f_msgcount 1527df8bae1dSRodney W. Grimes * times -- consider the case of sockets A and B that contain 1528df8bae1dSRodney W. Grimes * references to each other. On a last close of some other socket, 1529df8bae1dSRodney W. Grimes * we trigger a gc since the number of outstanding rights (unp_rights) 1530df8bae1dSRodney W. Grimes * is non-zero. If during the sweep phase the gc code un_discards, 1531df8bae1dSRodney W. Grimes * we end up doing a (full) closef on the descriptor. A closef on A 1532df8bae1dSRodney W. Grimes * results in the following chain. Closef calls soo_close, which 1533df8bae1dSRodney W. Grimes * calls soclose. Soclose calls first (through the switch 1534df8bae1dSRodney W. Grimes * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 1535df8bae1dSRodney W. Grimes * returns because the previous instance had set unp_gcing, and 1536df8bae1dSRodney W. Grimes * we return all the way back to soclose, which marks the socket 1537df8bae1dSRodney W. Grimes * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush 1538df8bae1dSRodney W. Grimes * to free up the rights that are queued in messages on the socket A, 1539df8bae1dSRodney W. Grimes * i.e., the reference on B. The sorflush calls via the dom_dispose 1540df8bae1dSRodney W. Grimes * switch unp_dispose, which unp_scans with unp_discard. This second 1541df8bae1dSRodney W. Grimes * instance of unp_discard just calls closef on B. 1542df8bae1dSRodney W. Grimes * 1543df8bae1dSRodney W. Grimes * Well, a similar chain occurs on B, resulting in a sorflush on B, 1544df8bae1dSRodney W. Grimes * which results in another closef on A. Unfortunately, A is already 1545df8bae1dSRodney W. Grimes * being closed, and the descriptor has already been marked with 1546df8bae1dSRodney W. Grimes * SS_NOFDREF, and soclose panics at this point. 1547df8bae1dSRodney W. Grimes * 1548df8bae1dSRodney W. Grimes * Here, we first take an extra reference to each inaccessible 1549df8bae1dSRodney W. Grimes * descriptor. Then, we call sorflush ourself, since we know 1550df8bae1dSRodney W. Grimes * it is a Unix domain socket anyhow. After we destroy all the 1551df8bae1dSRodney W. Grimes * rights carried in messages, we do a last closef to get rid 1552df8bae1dSRodney W. Grimes * of our extra reference. This is the last close, and the 1553df8bae1dSRodney W. Grimes * unp_detach etc will shut down the socket. 1554df8bae1dSRodney W. Grimes * 1555df8bae1dSRodney W. Grimes * 91/09/19, bsy@cs.cmu.edu 1556df8bae1dSRodney W. Grimes */ 155795f004dcSAlfred Perlstein again: 155895f004dcSAlfred Perlstein nfiles_snap = nfiles + nfiles_slack; /* some slack */ 155995f004dcSAlfred Perlstein extra_ref = malloc(nfiles_snap * sizeof(struct file *), M_TEMP, 156095f004dcSAlfred Perlstein M_WAITOK); 1561426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 156295f004dcSAlfred Perlstein if (nfiles_snap < nfiles) { 156395f004dcSAlfred Perlstein sx_sunlock(&filelist_lock); 156495f004dcSAlfred Perlstein free(extra_ref, M_TEMP); 156595f004dcSAlfred Perlstein nfiles_slack += 20; 156695f004dcSAlfred Perlstein goto again; 156795f004dcSAlfred Perlstein } 1568fc3fcacfSRobert Watson for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; 1569fc3fcacfSRobert Watson fp != NULL; fp = nextfp) { 15702e3c8fcbSPoul-Henning Kamp nextfp = LIST_NEXT(fp, f_list); 1571426da3bcSAlfred Perlstein FILE_LOCK(fp); 1572ed5b7817SJulian Elischer /* 1573ed5b7817SJulian Elischer * If it's not open, skip it 1574ed5b7817SJulian Elischer */ 1575426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1576426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1577df8bae1dSRodney W. Grimes continue; 1578426da3bcSAlfred Perlstein } 1579ed5b7817SJulian Elischer /* 1580ed5b7817SJulian Elischer * If all refs are from msgs, and it's not marked accessible 1581ed5b7817SJulian Elischer * then it must be referenced from some unreachable cycle 1582ed5b7817SJulian Elischer * of (shut-down) FDs, so include it in our 1583ed5b7817SJulian Elischer * list of FDs to remove 1584ed5b7817SJulian Elischer */ 1585426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) { 1586df8bae1dSRodney W. Grimes *fpp++ = fp; 1587df8bae1dSRodney W. Grimes nunref++; 1588df8bae1dSRodney W. Grimes fp->f_count++; 1589df8bae1dSRodney W. Grimes } 1590426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1591df8bae1dSRodney W. Grimes } 1592426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1593ed5b7817SJulian Elischer /* 1594ed5b7817SJulian Elischer * for each FD on our hit list, do the following two things 1595ed5b7817SJulian Elischer */ 15961c7c3c6aSMatthew Dillon for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 15971c7c3c6aSMatthew Dillon struct file *tfp = *fpp; 1598426da3bcSAlfred Perlstein FILE_LOCK(tfp); 1599cd72f218SMatthew Dillon if (tfp->f_type == DTYPE_SOCKET && 160048e3128bSMatthew Dillon tfp->f_data != NULL) { 1601426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 160248e3128bSMatthew Dillon sorflush(tfp->f_data); 1603e5aeaa0cSDag-Erling Smørgrav } else { 1604426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 16051c7c3c6aSMatthew Dillon } 1606e5aeaa0cSDag-Erling Smørgrav } 1607df8bae1dSRodney W. Grimes for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) 1608b40ce416SJulian Elischer closef(*fpp, (struct thread *) NULL); 1609210a5a71SAlfred Perlstein free(extra_ref, M_TEMP); 1610df8bae1dSRodney W. Grimes unp_gcing = 0; 1611df8bae1dSRodney W. Grimes } 1612df8bae1dSRodney W. Grimes 161326f9a767SRodney W. Grimes void 1614df8bae1dSRodney W. Grimes unp_dispose(m) 1615df8bae1dSRodney W. Grimes struct mbuf *m; 1616df8bae1dSRodney W. Grimes { 1617996c772fSJohn Dyson 1618df8bae1dSRodney W. Grimes if (m) 1619df8bae1dSRodney W. Grimes unp_scan(m, unp_discard); 1620df8bae1dSRodney W. Grimes } 1621df8bae1dSRodney W. Grimes 16220c1bb4fbSDima Dorfman static int 16236f105b34SJohn Baldwin unp_listen(unp, td) 16240c1bb4fbSDima Dorfman struct unpcb *unp; 16256f105b34SJohn Baldwin struct thread *td; 16260c1bb4fbSDima Dorfman { 16270d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 16280c1bb4fbSDima Dorfman 16290d9ce3a1SRobert Watson /* 16300d9ce3a1SRobert Watson * XXXRW: Why populate the local peer cred with our own credential? 16310d9ce3a1SRobert Watson */ 16326f105b34SJohn Baldwin cru2x(td->td_ucred, &unp->unp_peercred); 16330c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPCCACHED; 16340c1bb4fbSDima Dorfman return (0); 16350c1bb4fbSDima Dorfman } 16360c1bb4fbSDima Dorfman 1637f708ef1bSPoul-Henning Kamp static void 1638df8bae1dSRodney W. Grimes unp_scan(m0, op) 1639df8bae1dSRodney W. Grimes register struct mbuf *m0; 16404d77a549SAlfred Perlstein void (*op)(struct file *); 1641df8bae1dSRodney W. Grimes { 16422bc21ed9SDavid Malone struct mbuf *m; 16432bc21ed9SDavid Malone struct file **rp; 16442bc21ed9SDavid Malone struct cmsghdr *cm; 16452bc21ed9SDavid Malone void *data; 16462bc21ed9SDavid Malone int i; 16472bc21ed9SDavid Malone socklen_t clen, datalen; 1648df8bae1dSRodney W. Grimes int qfds; 1649df8bae1dSRodney W. Grimes 1650fc3fcacfSRobert Watson while (m0 != NULL) { 16512bc21ed9SDavid Malone for (m = m0; m; m = m->m_next) { 165212396bdcSDavid Malone if (m->m_type != MT_CONTROL) 1653df8bae1dSRodney W. Grimes continue; 16542bc21ed9SDavid Malone 16552bc21ed9SDavid Malone cm = mtod(m, struct cmsghdr *); 16562bc21ed9SDavid Malone clen = m->m_len; 16572bc21ed9SDavid Malone 16582bc21ed9SDavid Malone while (cm != NULL) { 16592bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) 16602bc21ed9SDavid Malone break; 16612bc21ed9SDavid Malone 16622bc21ed9SDavid Malone data = CMSG_DATA(cm); 16632bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len 16642bc21ed9SDavid Malone - (caddr_t)data; 16652bc21ed9SDavid Malone 16662bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET && 16672bc21ed9SDavid Malone cm->cmsg_type == SCM_RIGHTS) { 16682bc21ed9SDavid Malone qfds = datalen / sizeof (struct file *); 16692bc21ed9SDavid Malone rp = data; 1670df8bae1dSRodney W. Grimes for (i = 0; i < qfds; i++) 1671df8bae1dSRodney W. Grimes (*op)(*rp++); 16722bc21ed9SDavid Malone } 16732bc21ed9SDavid Malone 16742bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 16752bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 16762bc21ed9SDavid Malone cm = (struct cmsghdr *) 16772bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 16782bc21ed9SDavid Malone } else { 16792bc21ed9SDavid Malone clen = 0; 16802bc21ed9SDavid Malone cm = NULL; 16812bc21ed9SDavid Malone } 16822bc21ed9SDavid Malone } 1683df8bae1dSRodney W. Grimes } 1684df8bae1dSRodney W. Grimes m0 = m0->m_act; 1685df8bae1dSRodney W. Grimes } 1686df8bae1dSRodney W. Grimes } 1687df8bae1dSRodney W. Grimes 1688f708ef1bSPoul-Henning Kamp static void 1689df8bae1dSRodney W. Grimes unp_mark(fp) 1690df8bae1dSRodney W. Grimes struct file *fp; 1691df8bae1dSRodney W. Grimes { 1692426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) 1693df8bae1dSRodney W. Grimes return; 1694df8bae1dSRodney W. Grimes unp_defer++; 1695426da3bcSAlfred Perlstein fp->f_gcflag |= (FMARK|FDEFER); 1696df8bae1dSRodney W. Grimes } 1697df8bae1dSRodney W. Grimes 1698f708ef1bSPoul-Henning Kamp static void 1699df8bae1dSRodney W. Grimes unp_discard(fp) 1700df8bae1dSRodney W. Grimes struct file *fp; 1701df8bae1dSRodney W. Grimes { 1702426da3bcSAlfred Perlstein FILE_LOCK(fp); 1703df8bae1dSRodney W. Grimes fp->f_msgcount--; 1704df8bae1dSRodney W. Grimes unp_rights--; 1705426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1706b40ce416SJulian Elischer (void) closef(fp, (struct thread *)NULL); 1707df8bae1dSRodney W. Grimes } 1708