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 { 171a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 1720d9ce3a1SRobert Watson int error; 173a29f300eSGarrett Wollman 174fc3fcacfSRobert Watson if (unp == NULL) 175e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 1760d9ce3a1SRobert Watson UNP_LOCK(); 1770d9ce3a1SRobert Watson error = unp_connect(so, nam, curthread); 1780d9ce3a1SRobert Watson UNP_UNLOCK(); 1790d9ce3a1SRobert Watson return (error); 180a29f300eSGarrett Wollman } 181a29f300eSGarrett Wollman 182db48c0d2SRobert Watson int 183a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2) 184a29f300eSGarrett Wollman { 185a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so1); 1860d9ce3a1SRobert Watson int error; 187a29f300eSGarrett Wollman 188fc3fcacfSRobert Watson if (unp == NULL) 189e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 190a29f300eSGarrett Wollman 1910d9ce3a1SRobert Watson UNP_LOCK(); 1920d9ce3a1SRobert Watson error = unp_connect2(so1, so2); 1930d9ce3a1SRobert Watson UNP_UNLOCK(); 1940d9ce3a1SRobert Watson return (error); 195a29f300eSGarrett Wollman } 196a29f300eSGarrett Wollman 197a29f300eSGarrett Wollman /* control is EOPNOTSUPP */ 198a29f300eSGarrett Wollman 199a29f300eSGarrett Wollman static int 200a29f300eSGarrett Wollman uipc_detach(struct socket *so) 201a29f300eSGarrett Wollman { 202a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 203a29f300eSGarrett Wollman 204fc3fcacfSRobert Watson if (unp == NULL) 205e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 206a29f300eSGarrett Wollman 2070d9ce3a1SRobert Watson UNP_LOCK(); 2080d9ce3a1SRobert Watson unp_detach(unp); /* NB: unlocks unp */ 209e5aeaa0cSDag-Erling Smørgrav return (0); 210a29f300eSGarrett Wollman } 211a29f300eSGarrett Wollman 212a29f300eSGarrett Wollman static int 213a29f300eSGarrett Wollman uipc_disconnect(struct socket *so) 214a29f300eSGarrett Wollman { 215a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 216a29f300eSGarrett Wollman 217fc3fcacfSRobert Watson if (unp == NULL) 218e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 2190d9ce3a1SRobert Watson UNP_LOCK(); 220a29f300eSGarrett Wollman unp_disconnect(unp); 2210d9ce3a1SRobert Watson UNP_UNLOCK(); 222e5aeaa0cSDag-Erling Smørgrav return (0); 223a29f300eSGarrett Wollman } 224a29f300eSGarrett Wollman 225a29f300eSGarrett Wollman static int 226b40ce416SJulian Elischer uipc_listen(struct socket *so, struct thread *td) 227a29f300eSGarrett Wollman { 228a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 2290d9ce3a1SRobert Watson int error; 230a29f300eSGarrett Wollman 231fc3fcacfSRobert Watson if (unp == NULL || unp->unp_vnode == NULL) 232e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 2330d9ce3a1SRobert Watson UNP_LOCK(); 2340d9ce3a1SRobert Watson error = unp_listen(unp, td); 2350d9ce3a1SRobert Watson UNP_UNLOCK(); 2360d9ce3a1SRobert Watson return (error); 237a29f300eSGarrett Wollman } 238a29f300eSGarrett Wollman 239a29f300eSGarrett Wollman static int 24057bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 241a29f300eSGarrett Wollman { 242a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 2430d9ce3a1SRobert Watson const struct sockaddr *sa; 244a29f300eSGarrett Wollman 245fc3fcacfSRobert Watson if (unp == NULL) 246e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 2470d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 2480d9ce3a1SRobert Watson UNP_LOCK(); 249fc3fcacfSRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr!= NULL) 2500d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 251bdc5f6a3SHajimu UMEMOTO else { 252bdc5f6a3SHajimu UMEMOTO /* 253bdc5f6a3SHajimu UMEMOTO * XXX: It seems that this test always fails even when 254bdc5f6a3SHajimu UMEMOTO * connection is established. So, this else clause is 255bdc5f6a3SHajimu UMEMOTO * added as workaround to return PF_LOCAL sockaddr. 256bdc5f6a3SHajimu UMEMOTO */ 2570d9ce3a1SRobert Watson sa = &sun_noname; 258bdc5f6a3SHajimu UMEMOTO } 2590d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 2600d9ce3a1SRobert Watson UNP_UNLOCK(); 261e5aeaa0cSDag-Erling Smørgrav return (0); 262a29f300eSGarrett Wollman } 263a29f300eSGarrett Wollman 264a29f300eSGarrett Wollman static int 265a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 266a29f300eSGarrett Wollman { 267a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 268a29f300eSGarrett Wollman struct socket *so2; 2696aef685fSBrian Feldman u_long newhiwat; 270a29f300eSGarrett Wollman 271fc3fcacfSRobert Watson if (unp == NULL) 272e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 2730d9ce3a1SRobert Watson UNP_LOCK(); 274df8bae1dSRodney W. Grimes switch (so->so_type) { 275df8bae1dSRodney W. Grimes case SOCK_DGRAM: 276a29f300eSGarrett Wollman panic("uipc_rcvd DGRAM?"); 277df8bae1dSRodney W. Grimes /*NOTREACHED*/ 278df8bae1dSRodney W. Grimes 279df8bae1dSRodney W. Grimes case SOCK_STREAM: 280fc3fcacfSRobert Watson if (unp->unp_conn == NULL) 281df8bae1dSRodney W. Grimes break; 282df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 283c9f69064SRobert Watson SOCKBUF_LOCK(&so2->so_snd); 284c9f69064SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 285df8bae1dSRodney W. Grimes /* 286df8bae1dSRodney W. Grimes * Adjust backpressure on sender 287df8bae1dSRodney W. Grimes * and wakeup any waiting to write. 288df8bae1dSRodney W. Grimes */ 289ff8b0106SBrian Feldman so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt; 290ff8b0106SBrian Feldman unp->unp_mbcnt = so->so_rcv.sb_mbcnt; 2916aef685fSBrian Feldman newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - 2926aef685fSBrian Feldman so->so_rcv.sb_cc; 293f535380cSDon Lewis (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat, 2946aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 295ff8b0106SBrian Feldman unp->unp_cc = so->so_rcv.sb_cc; 296c9f69064SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2971e4d7da7SRobert Watson sowwakeup_locked(so2); 298df8bae1dSRodney W. Grimes break; 299df8bae1dSRodney W. Grimes 300df8bae1dSRodney W. Grimes default: 301a29f300eSGarrett Wollman panic("uipc_rcvd unknown socktype"); 302df8bae1dSRodney W. Grimes } 3030d9ce3a1SRobert Watson UNP_UNLOCK(); 304e5aeaa0cSDag-Erling Smørgrav return (0); 305a29f300eSGarrett Wollman } 306df8bae1dSRodney W. Grimes 307a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 308a29f300eSGarrett Wollman 309a29f300eSGarrett Wollman static int 31057bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 311b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 312a29f300eSGarrett Wollman { 313a29f300eSGarrett Wollman int error = 0; 314a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 315a29f300eSGarrett Wollman struct socket *so2; 3166aef685fSBrian Feldman u_long newhiwat; 317a29f300eSGarrett Wollman 318fc3fcacfSRobert Watson if (unp == NULL) { 319a29f300eSGarrett Wollman error = EINVAL; 320a29f300eSGarrett Wollman goto release; 321a29f300eSGarrett Wollman } 322a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 323a29f300eSGarrett Wollman error = EOPNOTSUPP; 324a29f300eSGarrett Wollman goto release; 325a29f300eSGarrett Wollman } 326a29f300eSGarrett Wollman 327fc3fcacfSRobert Watson if (control != NULL && (error = unp_internalize(&control, td))) 328a29f300eSGarrett Wollman goto release; 329df8bae1dSRodney W. Grimes 3300d9ce3a1SRobert Watson UNP_LOCK(); 331a29f300eSGarrett Wollman switch (so->so_type) { 332a29f300eSGarrett Wollman case SOCK_DGRAM: 333a29f300eSGarrett Wollman { 334e7dd9a10SRobert Watson const struct sockaddr *from; 335df8bae1dSRodney W. Grimes 336fc3fcacfSRobert Watson if (nam != NULL) { 337fc3fcacfSRobert Watson if (unp->unp_conn != NULL) { 338df8bae1dSRodney W. Grimes error = EISCONN; 339df8bae1dSRodney W. Grimes break; 340df8bae1dSRodney W. Grimes } 341b40ce416SJulian Elischer error = unp_connect(so, nam, td); 342df8bae1dSRodney W. Grimes if (error) 343df8bae1dSRodney W. Grimes break; 344df8bae1dSRodney W. Grimes } else { 345fc3fcacfSRobert Watson if (unp->unp_conn == NULL) { 346df8bae1dSRodney W. Grimes error = ENOTCONN; 347df8bae1dSRodney W. Grimes break; 348df8bae1dSRodney W. Grimes } 349df8bae1dSRodney W. Grimes } 350df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 351fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 35257bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 353df8bae1dSRodney W. Grimes else 354df8bae1dSRodney W. Grimes from = &sun_noname; 355a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 356a34b7046SRobert Watson if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) { 3571e4d7da7SRobert Watson sorwakeup_locked(so2); 358fc3fcacfSRobert Watson m = NULL; 359fc3fcacfSRobert Watson control = NULL; 360e5aeaa0cSDag-Erling Smørgrav } else { 361a34b7046SRobert Watson SOCKBUF_UNLOCK(&so2->so_rcv); 362df8bae1dSRodney W. Grimes error = ENOBUFS; 363e5aeaa0cSDag-Erling Smørgrav } 364fc3fcacfSRobert Watson if (nam != NULL) 365df8bae1dSRodney W. Grimes unp_disconnect(unp); 366df8bae1dSRodney W. Grimes break; 367df8bae1dSRodney W. Grimes } 368df8bae1dSRodney W. Grimes 369df8bae1dSRodney W. Grimes case SOCK_STREAM: 3706b8fda4dSGarrett Wollman /* Connect if not connected yet. */ 3716b8fda4dSGarrett Wollman /* 3726b8fda4dSGarrett Wollman * Note: A better implementation would complain 373402cc72dSDavid Greenman * if not equal to the peer's address. 3746b8fda4dSGarrett Wollman */ 375402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 376fc3fcacfSRobert Watson if (nam != NULL) { 377b40ce416SJulian Elischer error = unp_connect(so, nam, td); 378402cc72dSDavid Greenman if (error) 3796b8fda4dSGarrett Wollman break; /* XXX */ 380402cc72dSDavid Greenman } else { 381402cc72dSDavid Greenman error = ENOTCONN; 382402cc72dSDavid Greenman break; 383402cc72dSDavid Greenman } 384402cc72dSDavid Greenman } 385402cc72dSDavid Greenman 386c0b99ffaSRobert Watson if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 387df8bae1dSRodney W. Grimes error = EPIPE; 388df8bae1dSRodney W. Grimes break; 389df8bae1dSRodney W. Grimes } 390fc3fcacfSRobert Watson if (unp->unp_conn == NULL) 391a29f300eSGarrett Wollman panic("uipc_send connected but no connection?"); 392df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 393a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 394df8bae1dSRodney W. Grimes /* 395df8bae1dSRodney W. Grimes * Send to paired receive port, and then reduce 396df8bae1dSRodney W. Grimes * send buffer hiwater marks to maintain backpressure. 397df8bae1dSRodney W. Grimes * Wake up readers. 398df8bae1dSRodney W. Grimes */ 399fc3fcacfSRobert Watson if (control != NULL) { 400a34b7046SRobert Watson if (sbappendcontrol_locked(&so2->so_rcv, m, control)) 401fc3fcacfSRobert Watson control = NULL; 402e5aeaa0cSDag-Erling Smørgrav } else { 403a34b7046SRobert Watson sbappend_locked(&so2->so_rcv, m); 404e5aeaa0cSDag-Erling Smørgrav } 405ff8b0106SBrian Feldman so->so_snd.sb_mbmax -= 406ff8b0106SBrian Feldman so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt; 407ff8b0106SBrian Feldman unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt; 4086aef685fSBrian Feldman newhiwat = so->so_snd.sb_hiwat - 4096aef685fSBrian Feldman (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc); 410f535380cSDon Lewis (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat, 4116aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 412ff8b0106SBrian Feldman unp->unp_conn->unp_cc = so2->so_rcv.sb_cc; 4131e4d7da7SRobert Watson sorwakeup_locked(so2); 414fc3fcacfSRobert Watson m = NULL; 415df8bae1dSRodney W. Grimes break; 416df8bae1dSRodney W. Grimes 417df8bae1dSRodney W. Grimes default: 418a29f300eSGarrett Wollman panic("uipc_send unknown socktype"); 419df8bae1dSRodney W. Grimes } 420a29f300eSGarrett Wollman 4216b8fda4dSGarrett Wollman /* 4226b8fda4dSGarrett Wollman * SEND_EOF is equivalent to a SEND followed by 4236b8fda4dSGarrett Wollman * a SHUTDOWN. 4246b8fda4dSGarrett Wollman */ 425a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 4266b8fda4dSGarrett Wollman socantsendmore(so); 4276b8fda4dSGarrett Wollman unp_shutdown(unp); 4286b8fda4dSGarrett Wollman } 4290d9ce3a1SRobert Watson UNP_UNLOCK(); 430df8bae1dSRodney W. Grimes 431fc3fcacfSRobert Watson if (control != NULL && error != 0) 432bd508d39SDon Lewis unp_dispose(control); 433bd508d39SDon Lewis 434a29f300eSGarrett Wollman release: 435fc3fcacfSRobert Watson if (control != NULL) 436a29f300eSGarrett Wollman m_freem(control); 437fc3fcacfSRobert Watson if (m != NULL) 438a29f300eSGarrett Wollman m_freem(m); 439e5aeaa0cSDag-Erling Smørgrav return (error); 440a29f300eSGarrett Wollman } 441df8bae1dSRodney W. Grimes 442a29f300eSGarrett Wollman static int 443a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 444a29f300eSGarrett Wollman { 445a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 446a29f300eSGarrett Wollman struct socket *so2; 447a29f300eSGarrett Wollman 448fc3fcacfSRobert Watson if (unp == NULL) 449e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 4500d9ce3a1SRobert Watson UNP_LOCK(); 451a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 452fc3fcacfSRobert Watson if (so->so_type == SOCK_STREAM && unp->unp_conn != NULL) { 453df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 454a29f300eSGarrett Wollman sb->st_blksize += so2->so_rcv.sb_cc; 455df8bae1dSRodney W. Grimes } 456f3732fd1SPoul-Henning Kamp sb->st_dev = NODEV; 457df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 4586f782c46SJeffrey Hsu unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 459a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 4600d9ce3a1SRobert Watson UNP_UNLOCK(); 461df8bae1dSRodney W. Grimes return (0); 462a29f300eSGarrett Wollman } 463df8bae1dSRodney W. Grimes 464a29f300eSGarrett Wollman static int 465a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 466a29f300eSGarrett Wollman { 467a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 468df8bae1dSRodney W. Grimes 469fc3fcacfSRobert Watson if (unp == NULL) 470e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 4710d9ce3a1SRobert Watson UNP_LOCK(); 472a29f300eSGarrett Wollman socantsendmore(so); 473a29f300eSGarrett Wollman unp_shutdown(unp); 4740d9ce3a1SRobert Watson UNP_UNLOCK(); 475e5aeaa0cSDag-Erling Smørgrav return (0); 476a29f300eSGarrett Wollman } 477df8bae1dSRodney W. Grimes 478a29f300eSGarrett Wollman static int 47957bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 480a29f300eSGarrett Wollman { 481a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 4820d9ce3a1SRobert Watson const struct sockaddr *sa; 483a29f300eSGarrett Wollman 484fc3fcacfSRobert Watson if (unp == NULL) 485e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 4860d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 4870d9ce3a1SRobert Watson UNP_LOCK(); 488fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 4890d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_addr; 49083f3198bSThomas Moestl else 4910d9ce3a1SRobert Watson sa = &sun_noname; 4920d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 4930d9ce3a1SRobert Watson UNP_UNLOCK(); 494e5aeaa0cSDag-Erling Smørgrav return (0); 495df8bae1dSRodney W. Grimes } 496a29f300eSGarrett Wollman 497a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = { 498a29f300eSGarrett Wollman uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect, 499a29f300eSGarrett Wollman uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect, 500a29f300eSGarrett Wollman uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp, 501a29f300eSGarrett Wollman uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr, 502a557af22SRobert Watson sosend, soreceive, sopoll, pru_sosetlabel_null 503a29f300eSGarrett Wollman }; 504df8bae1dSRodney W. Grimes 5050c1bb4fbSDima Dorfman int 5060c1bb4fbSDima Dorfman uipc_ctloutput(so, sopt) 5070c1bb4fbSDima Dorfman struct socket *so; 5080c1bb4fbSDima Dorfman struct sockopt *sopt; 5090c1bb4fbSDima Dorfman { 5100c1bb4fbSDima Dorfman struct unpcb *unp = sotounpcb(so); 5110d9ce3a1SRobert Watson struct xucred xu; 5120c1bb4fbSDima Dorfman int error; 5130c1bb4fbSDima Dorfman 5140c1bb4fbSDima Dorfman switch (sopt->sopt_dir) { 5150c1bb4fbSDima Dorfman case SOPT_GET: 5160c1bb4fbSDima Dorfman switch (sopt->sopt_name) { 5170c1bb4fbSDima Dorfman case LOCAL_PEERCRED: 5180d9ce3a1SRobert Watson error = 0; 5190d9ce3a1SRobert Watson UNP_LOCK(); 5200c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC) 5210d9ce3a1SRobert Watson xu = unp->unp_peercred; 5220c1bb4fbSDima Dorfman else { 5230c1bb4fbSDima Dorfman if (so->so_type == SOCK_STREAM) 5240c1bb4fbSDima Dorfman error = ENOTCONN; 5250c1bb4fbSDima Dorfman else 5260c1bb4fbSDima Dorfman error = EINVAL; 5270c1bb4fbSDima Dorfman } 5280d9ce3a1SRobert Watson UNP_UNLOCK(); 5290d9ce3a1SRobert Watson if (error == 0) 5300d9ce3a1SRobert Watson error = sooptcopyout(sopt, &xu, sizeof(xu)); 5310c1bb4fbSDima Dorfman break; 5320c1bb4fbSDima Dorfman default: 5330c1bb4fbSDima Dorfman error = EOPNOTSUPP; 5340c1bb4fbSDima Dorfman break; 5350c1bb4fbSDima Dorfman } 5360c1bb4fbSDima Dorfman break; 5370c1bb4fbSDima Dorfman case SOPT_SET: 5380c1bb4fbSDima Dorfman default: 5390c1bb4fbSDima Dorfman error = EOPNOTSUPP; 5400c1bb4fbSDima Dorfman break; 5410c1bb4fbSDima Dorfman } 5420c1bb4fbSDima Dorfman return (error); 5430c1bb4fbSDima Dorfman } 5440c1bb4fbSDima Dorfman 545df8bae1dSRodney W. Grimes /* 546df8bae1dSRodney W. Grimes * Both send and receive buffers are allocated PIPSIZ bytes of buffering 547df8bae1dSRodney W. Grimes * for stream sockets, although the total for sender and receiver is 548df8bae1dSRodney W. Grimes * actually only PIPSIZ. 549df8bae1dSRodney W. Grimes * Datagram sockets really use the sendspace as the maximum datagram size, 550df8bae1dSRodney W. Grimes * and don't really want to reserve the sendspace. Their recvspace should 551df8bae1dSRodney W. Grimes * be large enough for at least one max-size datagram plus address. 552df8bae1dSRodney W. Grimes */ 5535dce41c5SJohn Dyson #ifndef PIPSIZ 5545dce41c5SJohn Dyson #define PIPSIZ 8192 5555dce41c5SJohn Dyson #endif 556f708ef1bSPoul-Henning Kamp static u_long unpst_sendspace = PIPSIZ; 557f708ef1bSPoul-Henning Kamp static u_long unpst_recvspace = PIPSIZ; 558f708ef1bSPoul-Henning Kamp static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 559f708ef1bSPoul-Henning Kamp static u_long unpdg_recvspace = 4*1024; 560df8bae1dSRodney W. Grimes 561f708ef1bSPoul-Henning Kamp static int unp_rights; /* file descriptors in flight */ 562df8bae1dSRodney W. Grimes 563ce02431fSDoug Rabson SYSCTL_DECL(_net_local_stream); 564639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 565639acc13SGarrett Wollman &unpst_sendspace, 0, ""); 566639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 567639acc13SGarrett Wollman &unpst_recvspace, 0, ""); 568ce02431fSDoug Rabson SYSCTL_DECL(_net_local_dgram); 569639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 570639acc13SGarrett Wollman &unpdg_sendspace, 0, ""); 571639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 572639acc13SGarrett Wollman &unpdg_recvspace, 0, ""); 573ce02431fSDoug Rabson SYSCTL_DECL(_net_local); 574639acc13SGarrett Wollman SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); 575639acc13SGarrett Wollman 576f708ef1bSPoul-Henning Kamp static int 577df8bae1dSRodney W. Grimes unp_attach(so) 578df8bae1dSRodney W. Grimes struct socket *so; 579df8bae1dSRodney W. Grimes { 580df8bae1dSRodney W. Grimes register struct unpcb *unp; 581df8bae1dSRodney W. Grimes int error; 582df8bae1dSRodney W. Grimes 583df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 584df8bae1dSRodney W. Grimes switch (so->so_type) { 585df8bae1dSRodney W. Grimes 586df8bae1dSRodney W. Grimes case SOCK_STREAM: 587df8bae1dSRodney W. Grimes error = soreserve(so, unpst_sendspace, unpst_recvspace); 588df8bae1dSRodney W. Grimes break; 589df8bae1dSRodney W. Grimes 590df8bae1dSRodney W. Grimes case SOCK_DGRAM: 591df8bae1dSRodney W. Grimes error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 592df8bae1dSRodney W. Grimes break; 593df8bae1dSRodney W. Grimes 594df8bae1dSRodney W. Grimes default: 595df8bae1dSRodney W. Grimes panic("unp_attach"); 596df8bae1dSRodney W. Grimes } 597df8bae1dSRodney W. Grimes if (error) 598df8bae1dSRodney W. Grimes return (error); 599df8bae1dSRodney W. Grimes } 600a163d034SWarner Losh unp = uma_zalloc(unp_zone, M_WAITOK); 60157bf258eSGarrett Wollman if (unp == NULL) 602df8bae1dSRodney W. Grimes return (ENOBUFS); 60357bf258eSGarrett Wollman bzero(unp, sizeof *unp); 60498271db4SGarrett Wollman LIST_INIT(&unp->unp_refs); 605df8bae1dSRodney W. Grimes unp->unp_socket = so; 6060d9ce3a1SRobert Watson 6070d9ce3a1SRobert Watson UNP_LOCK(); 6080d9ce3a1SRobert Watson unp->unp_gencnt = ++unp_gencnt; 6090d9ce3a1SRobert Watson unp_count++; 61098271db4SGarrett Wollman LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead 61198271db4SGarrett Wollman : &unp_shead, unp, unp_link); 6120d9ce3a1SRobert Watson UNP_UNLOCK(); 6130d9ce3a1SRobert Watson 614210a5a71SAlfred Perlstein so->so_pcb = unp; 615df8bae1dSRodney W. Grimes return (0); 616df8bae1dSRodney W. Grimes } 617df8bae1dSRodney W. Grimes 618f708ef1bSPoul-Henning Kamp static void 619df8bae1dSRodney W. Grimes unp_detach(unp) 620df8bae1dSRodney W. Grimes register struct unpcb *unp; 621df8bae1dSRodney W. Grimes { 6220d9ce3a1SRobert Watson struct vnode *vp; 6230d9ce3a1SRobert Watson 6240d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 6250d9ce3a1SRobert Watson 62698271db4SGarrett Wollman LIST_REMOVE(unp, unp_link); 62798271db4SGarrett Wollman unp->unp_gencnt = ++unp_gencnt; 62898271db4SGarrett Wollman --unp_count; 6290d9ce3a1SRobert Watson if ((vp = unp->unp_vnode) != NULL) { 6300d9ce3a1SRobert Watson /* 6310d9ce3a1SRobert Watson * XXXRW: should v_socket be frobbed only while holding 6320d9ce3a1SRobert Watson * Giant? 6330d9ce3a1SRobert Watson */ 634fc3fcacfSRobert Watson unp->unp_vnode->v_socket = NULL; 635fc3fcacfSRobert Watson unp->unp_vnode = NULL; 636df8bae1dSRodney W. Grimes } 637fc3fcacfSRobert Watson if (unp->unp_conn != NULL) 638df8bae1dSRodney W. Grimes unp_disconnect(unp); 6390d9ce3a1SRobert Watson while (!LIST_EMPTY(&unp->unp_refs)) { 6400d9ce3a1SRobert Watson struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 6410d9ce3a1SRobert Watson unp_drop(ref, ECONNRESET); 6420d9ce3a1SRobert Watson } 643df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 644fc3fcacfSRobert Watson unp->unp_socket->so_pcb = NULL; 645df8bae1dSRodney W. Grimes if (unp_rights) { 646df8bae1dSRodney W. Grimes /* 647df8bae1dSRodney W. Grimes * Normally the receive buffer is flushed later, 648df8bae1dSRodney W. Grimes * in sofree, but if our receive buffer holds references 649df8bae1dSRodney W. Grimes * to descriptors that are now garbage, we will dispose 650df8bae1dSRodney W. Grimes * of those descriptor references after the garbage collector 651df8bae1dSRodney W. Grimes * gets them (resulting in a "panic: closef: count < 0"). 652df8bae1dSRodney W. Grimes */ 653df8bae1dSRodney W. Grimes sorflush(unp->unp_socket); 654df8bae1dSRodney W. Grimes unp_gc(); 655df8bae1dSRodney W. Grimes } 656a5993a97SRobert Watson UNP_UNLOCK(); 657fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 65857bf258eSGarrett Wollman FREE(unp->unp_addr, M_SONAME); 6599e9d298aSJeff Roberson uma_zfree(unp_zone, unp); 6600d9ce3a1SRobert Watson if (vp) { 6610d9ce3a1SRobert Watson mtx_lock(&Giant); 6620d9ce3a1SRobert Watson vrele(vp); 6630d9ce3a1SRobert Watson mtx_unlock(&Giant); 6640d9ce3a1SRobert Watson } 665df8bae1dSRodney W. Grimes } 666df8bae1dSRodney W. Grimes 667f708ef1bSPoul-Henning Kamp static int 668b40ce416SJulian Elischer unp_bind(unp, nam, td) 669df8bae1dSRodney W. Grimes struct unpcb *unp; 67057bf258eSGarrett Wollman struct sockaddr *nam; 671b40ce416SJulian Elischer struct thread *td; 672df8bae1dSRodney W. Grimes { 67357bf258eSGarrett Wollman struct sockaddr_un *soun = (struct sockaddr_un *)nam; 674f2a2857bSKirk McKusick struct vnode *vp; 675f2a2857bSKirk McKusick struct mount *mp; 676df8bae1dSRodney W. Grimes struct vattr vattr; 67757bf258eSGarrett Wollman int error, namelen; 678df8bae1dSRodney W. Grimes struct nameidata nd; 6798f364875SJulian Elischer char *buf; 680df8bae1dSRodney W. Grimes 6810d9ce3a1SRobert Watson /* 6820d9ce3a1SRobert Watson * XXXRW: This test-and-set of unp_vnode is non-atomic; the 6830d9ce3a1SRobert Watson * unlocked read here is fine, but the value of unp_vnode needs 6840d9ce3a1SRobert Watson * to be tested again after we do all the lookups to see if the 6850d9ce3a1SRobert Watson * pcb is still unbound? 6860d9ce3a1SRobert Watson */ 687df8bae1dSRodney W. Grimes if (unp->unp_vnode != NULL) 688df8bae1dSRodney W. Grimes return (EINVAL); 68955c85568SRobert Drehmel 69057bf258eSGarrett Wollman namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 69157bf258eSGarrett Wollman if (namelen <= 0) 692e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 69355c85568SRobert Drehmel 694a163d034SWarner Losh buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 69555c85568SRobert Drehmel strlcpy(buf, soun->sun_path, namelen + 1); 69655c85568SRobert Drehmel 6970d9ce3a1SRobert Watson mtx_lock(&Giant); 698f2a2857bSKirk McKusick restart: 6990d9ce3a1SRobert Watson mtx_assert(&Giant, MA_OWNED); 700b65f6f6bSRobert Watson NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME, UIO_SYSSPACE, 701b40ce416SJulian Elischer buf, td); 702df8bae1dSRodney W. Grimes /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 703797f2d22SPoul-Henning Kamp error = namei(&nd); 7040d9ce3a1SRobert Watson if (error) 7050d9ce3a1SRobert Watson goto done; 706df8bae1dSRodney W. Grimes vp = nd.ni_vp; 707f2a2857bSKirk McKusick if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 708762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 709df8bae1dSRodney W. Grimes if (nd.ni_dvp == vp) 710df8bae1dSRodney W. Grimes vrele(nd.ni_dvp); 711df8bae1dSRodney W. Grimes else 712df8bae1dSRodney W. Grimes vput(nd.ni_dvp); 713f2a2857bSKirk McKusick if (vp != NULL) { 714df8bae1dSRodney W. Grimes vrele(vp); 7150d9ce3a1SRobert Watson error = EADDRINUSE; 7160d9ce3a1SRobert Watson goto done; 717df8bae1dSRodney W. Grimes } 7188f364875SJulian Elischer error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 7190d9ce3a1SRobert Watson if (error) 7200d9ce3a1SRobert Watson goto done; 721f2a2857bSKirk McKusick goto restart; 722f2a2857bSKirk McKusick } 723df8bae1dSRodney W. Grimes VATTR_NULL(&vattr); 724df8bae1dSRodney W. Grimes vattr.va_type = VSOCK; 725b40ce416SJulian Elischer vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 7266ea48a90SRobert Watson #ifdef MAC 7276ea48a90SRobert Watson error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 7286ea48a90SRobert Watson &vattr); 7296151efaaSRobert Watson #endif 7306ea48a90SRobert Watson if (error == 0) { 731a854ed98SJohn Baldwin VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE); 7327be2d300SMike Smith error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 7336ea48a90SRobert Watson } 734762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 7357be2d300SMike Smith vput(nd.ni_dvp); 7360d9ce3a1SRobert Watson if (error) 7370d9ce3a1SRobert Watson goto done; 738df8bae1dSRodney W. Grimes vp = nd.ni_vp; 7390d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_bind"); 7400d9ce3a1SRobert Watson soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 7410d9ce3a1SRobert Watson UNP_LOCK(); 742df8bae1dSRodney W. Grimes vp->v_socket = unp->unp_socket; 743df8bae1dSRodney W. Grimes unp->unp_vnode = vp; 7440d9ce3a1SRobert Watson unp->unp_addr = soun; 7450d9ce3a1SRobert Watson UNP_UNLOCK(); 746b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 747f2a2857bSKirk McKusick vn_finished_write(mp); 7480d9ce3a1SRobert Watson done: 7490d9ce3a1SRobert Watson mtx_unlock(&Giant); 7508f364875SJulian Elischer free(buf, M_TEMP); 7510d9ce3a1SRobert Watson return (error); 752df8bae1dSRodney W. Grimes } 753df8bae1dSRodney W. Grimes 754f708ef1bSPoul-Henning Kamp static int 755b40ce416SJulian Elischer unp_connect(so, nam, td) 756df8bae1dSRodney W. Grimes struct socket *so; 75757bf258eSGarrett Wollman struct sockaddr *nam; 758b40ce416SJulian Elischer struct thread *td; 759df8bae1dSRodney W. Grimes { 76057bf258eSGarrett Wollman register struct sockaddr_un *soun = (struct sockaddr_un *)nam; 761df8bae1dSRodney W. Grimes register struct vnode *vp; 762df8bae1dSRodney W. Grimes register struct socket *so2, *so3; 7630d9ce3a1SRobert Watson struct unpcb *unp = sotounpcb(so); 7640d9ce3a1SRobert Watson struct unpcb *unp2, *unp3; 76557bf258eSGarrett Wollman int error, len; 766df8bae1dSRodney W. Grimes struct nameidata nd; 76757bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 7680d9ce3a1SRobert Watson struct sockaddr *sa; 7690d9ce3a1SRobert Watson 7700d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 771df8bae1dSRodney W. Grimes 77257bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 77357bf258eSGarrett Wollman if (len <= 0) 774e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 77555c85568SRobert Drehmel strlcpy(buf, soun->sun_path, len + 1); 7760d9ce3a1SRobert Watson UNP_UNLOCK(); 7770d9ce3a1SRobert Watson sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 7780d9ce3a1SRobert Watson mtx_lock(&Giant); 779b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td); 780797f2d22SPoul-Henning Kamp error = namei(&nd); 781797f2d22SPoul-Henning Kamp if (error) 7820d9ce3a1SRobert Watson vp = NULL; 7830d9ce3a1SRobert Watson else 784df8bae1dSRodney W. Grimes vp = nd.ni_vp; 7850d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_connect"); 786762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 7870d9ce3a1SRobert Watson if (error) 7880d9ce3a1SRobert Watson goto bad; 7890d9ce3a1SRobert Watson 790df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 791df8bae1dSRodney W. Grimes error = ENOTSOCK; 792df8bae1dSRodney W. Grimes goto bad; 793df8bae1dSRodney W. Grimes } 794a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 795797f2d22SPoul-Henning Kamp if (error) 796df8bae1dSRodney W. Grimes goto bad; 7972260c03dSRobert Watson mtx_unlock(&Giant); 7982260c03dSRobert Watson UNP_LOCK(); 799df8bae1dSRodney W. Grimes so2 = vp->v_socket; 800fc3fcacfSRobert Watson if (so2 == NULL) { 801df8bae1dSRodney W. Grimes error = ECONNREFUSED; 8022260c03dSRobert Watson goto bad2; 803df8bae1dSRodney W. Grimes } 804df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 805df8bae1dSRodney W. Grimes error = EPROTOTYPE; 8062260c03dSRobert Watson goto bad2; 807df8bae1dSRodney W. Grimes } 808df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 8090d9ce3a1SRobert Watson if (so2->so_options & SO_ACCEPTCONN) { 8100d9ce3a1SRobert Watson /* 8110d9ce3a1SRobert Watson * NB: drop locks here so unp_attach is entered 8120d9ce3a1SRobert Watson * w/o locks; this avoids a recursive lock 8130d9ce3a1SRobert Watson * of the head and holding sleep locks across 8140d9ce3a1SRobert Watson * a (potentially) blocking malloc. 8150d9ce3a1SRobert Watson */ 8160d9ce3a1SRobert Watson UNP_UNLOCK(); 8170d9ce3a1SRobert Watson so3 = sonewconn(so2, 0); 8180d9ce3a1SRobert Watson UNP_LOCK(); 8190d9ce3a1SRobert Watson } else 8200d9ce3a1SRobert Watson so3 = NULL; 8210d9ce3a1SRobert Watson if (so3 == NULL) { 822df8bae1dSRodney W. Grimes error = ECONNREFUSED; 8230d9ce3a1SRobert Watson goto bad2; 824df8bae1dSRodney W. Grimes } 8250c1bb4fbSDima Dorfman unp = sotounpcb(so); 826df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 827df8bae1dSRodney W. Grimes unp3 = sotounpcb(so3); 8280d9ce3a1SRobert Watson if (unp2->unp_addr != NULL) { 8290d9ce3a1SRobert Watson bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 8300d9ce3a1SRobert Watson unp3->unp_addr = (struct sockaddr_un *) sa; 8310d9ce3a1SRobert Watson sa = NULL; 8320d9ce3a1SRobert Watson } 8330c1bb4fbSDima Dorfman /* 8340c1bb4fbSDima Dorfman * unp_peercred management: 8350c1bb4fbSDima Dorfman * 8360c1bb4fbSDima Dorfman * The connecter's (client's) credentials are copied 8370c1bb4fbSDima Dorfman * from its process structure at the time of connect() 8380c1bb4fbSDima Dorfman * (which is now). 8390c1bb4fbSDima Dorfman */ 840a854ed98SJohn Baldwin cru2x(td->td_ucred, &unp3->unp_peercred); 8410c1bb4fbSDima Dorfman unp3->unp_flags |= UNP_HAVEPC; 8420c1bb4fbSDima Dorfman /* 8430c1bb4fbSDima Dorfman * The receiver's (server's) credentials are copied 8440c1bb4fbSDima Dorfman * from the unp_peercred member of socket on which the 8450c1bb4fbSDima Dorfman * former called listen(); unp_listen() cached that 8460c1bb4fbSDima Dorfman * process's credentials at that time so we can use 8470c1bb4fbSDima Dorfman * them now. 8480c1bb4fbSDima Dorfman */ 8490c1bb4fbSDima Dorfman KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 8500c1bb4fbSDima Dorfman ("unp_connect: listener without cached peercred")); 8510c1bb4fbSDima Dorfman memcpy(&unp->unp_peercred, &unp2->unp_peercred, 8520c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 8530c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPC; 854335654d7SRobert Watson #ifdef MAC 855310e7cebSRobert Watson SOCK_LOCK(so); 856335654d7SRobert Watson mac_set_socket_peer_from_socket(so, so3); 857335654d7SRobert Watson mac_set_socket_peer_from_socket(so3, so); 858310e7cebSRobert Watson SOCK_UNLOCK(so); 859335654d7SRobert Watson #endif 8600c1bb4fbSDima Dorfman 861df8bae1dSRodney W. Grimes so2 = so3; 862df8bae1dSRodney W. Grimes } 863df8bae1dSRodney W. Grimes error = unp_connect2(so, so2); 8640d9ce3a1SRobert Watson bad2: 8650d9ce3a1SRobert Watson UNP_UNLOCK(); 8660d9ce3a1SRobert Watson mtx_lock(&Giant); 867df8bae1dSRodney W. Grimes bad: 8680d9ce3a1SRobert Watson mtx_assert(&Giant, MA_OWNED); 8690d9ce3a1SRobert Watson if (vp != NULL) 870df8bae1dSRodney W. Grimes vput(vp); 8710d9ce3a1SRobert Watson mtx_unlock(&Giant); 8720d9ce3a1SRobert Watson free(sa, M_SONAME); 8730d9ce3a1SRobert Watson UNP_LOCK(); 874df8bae1dSRodney W. Grimes return (error); 875df8bae1dSRodney W. Grimes } 876df8bae1dSRodney W. Grimes 877db48c0d2SRobert Watson static int 878df8bae1dSRodney W. Grimes unp_connect2(so, so2) 879df8bae1dSRodney W. Grimes register struct socket *so; 880df8bae1dSRodney W. Grimes register struct socket *so2; 881df8bae1dSRodney W. Grimes { 882df8bae1dSRodney W. Grimes register struct unpcb *unp = sotounpcb(so); 883df8bae1dSRodney W. Grimes register struct unpcb *unp2; 884df8bae1dSRodney W. Grimes 8850d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 8860d9ce3a1SRobert Watson 887df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 888df8bae1dSRodney W. Grimes return (EPROTOTYPE); 889df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 890df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 891df8bae1dSRodney W. Grimes switch (so->so_type) { 892df8bae1dSRodney W. Grimes 893df8bae1dSRodney W. Grimes case SOCK_DGRAM: 89498271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 895df8bae1dSRodney W. Grimes soisconnected(so); 896df8bae1dSRodney W. Grimes break; 897df8bae1dSRodney W. Grimes 898df8bae1dSRodney W. Grimes case SOCK_STREAM: 899df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 900df8bae1dSRodney W. Grimes soisconnected(so); 901df8bae1dSRodney W. Grimes soisconnected(so2); 902df8bae1dSRodney W. Grimes break; 903df8bae1dSRodney W. Grimes 904df8bae1dSRodney W. Grimes default: 905df8bae1dSRodney W. Grimes panic("unp_connect2"); 906df8bae1dSRodney W. Grimes } 907df8bae1dSRodney W. Grimes return (0); 908df8bae1dSRodney W. Grimes } 909df8bae1dSRodney W. Grimes 910f708ef1bSPoul-Henning Kamp static void 911df8bae1dSRodney W. Grimes unp_disconnect(unp) 912df8bae1dSRodney W. Grimes struct unpcb *unp; 913df8bae1dSRodney W. Grimes { 914df8bae1dSRodney W. Grimes register struct unpcb *unp2 = unp->unp_conn; 9151b2e3b4bSRobert Watson struct socket *so; 916df8bae1dSRodney W. Grimes 9170d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 9180d9ce3a1SRobert Watson 919fc3fcacfSRobert Watson if (unp2 == NULL) 920df8bae1dSRodney W. Grimes return; 921fc3fcacfSRobert Watson unp->unp_conn = NULL; 922df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 923df8bae1dSRodney W. Grimes 924df8bae1dSRodney W. Grimes case SOCK_DGRAM: 92598271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 9261b2e3b4bSRobert Watson so = unp->unp_socket; 9271b2e3b4bSRobert Watson SOCK_LOCK(so); 9281b2e3b4bSRobert Watson so->so_state &= ~SS_ISCONNECTED; 9291b2e3b4bSRobert Watson SOCK_UNLOCK(so); 930df8bae1dSRodney W. Grimes break; 931df8bae1dSRodney W. Grimes 932df8bae1dSRodney W. Grimes case SOCK_STREAM: 933df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 934fc3fcacfSRobert Watson unp2->unp_conn = NULL; 935df8bae1dSRodney W. Grimes soisdisconnected(unp2->unp_socket); 936df8bae1dSRodney W. Grimes break; 937df8bae1dSRodney W. Grimes } 938df8bae1dSRodney W. Grimes } 939df8bae1dSRodney W. Grimes 940df8bae1dSRodney W. Grimes #ifdef notdef 94126f9a767SRodney W. Grimes void 942df8bae1dSRodney W. Grimes unp_abort(unp) 943df8bae1dSRodney W. Grimes struct unpcb *unp; 944df8bae1dSRodney W. Grimes { 945df8bae1dSRodney W. Grimes 946df8bae1dSRodney W. Grimes unp_detach(unp); 947df8bae1dSRodney W. Grimes } 948df8bae1dSRodney W. Grimes #endif 949df8bae1dSRodney W. Grimes 9500d9ce3a1SRobert Watson /* 9510d9ce3a1SRobert Watson * unp_pcblist() assumes that UNIX domain socket memory is never reclaimed 9520d9ce3a1SRobert Watson * by the zone (UMA_ZONE_NOFREE), and as such potentially stale pointers 9530d9ce3a1SRobert Watson * are safe to reference. It first scans the list of struct unpcb's to 9540d9ce3a1SRobert Watson * generate a pointer list, then it rescans its list one entry at a time to 9550d9ce3a1SRobert Watson * externalize and copyout. It checks the generation number to see if a 9560d9ce3a1SRobert Watson * struct unpcb has been reused, and will skip it if so. 9570d9ce3a1SRobert Watson */ 95898271db4SGarrett Wollman static int 95982d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 96098271db4SGarrett Wollman { 961f5ef029eSPoul-Henning Kamp int error, i, n; 96298271db4SGarrett Wollman struct unpcb *unp, **unp_list; 96398271db4SGarrett Wollman unp_gen_t gencnt; 9648f364875SJulian Elischer struct xunpgen *xug; 96598271db4SGarrett Wollman struct unp_head *head; 9668f364875SJulian Elischer struct xunpcb *xu; 96798271db4SGarrett Wollman 968a23d65bfSBruce Evans head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 96998271db4SGarrett Wollman 97098271db4SGarrett Wollman /* 97198271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 97298271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 97398271db4SGarrett Wollman */ 974fc3fcacfSRobert Watson if (req->oldptr == NULL) { 97598271db4SGarrett Wollman n = unp_count; 9768f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug) 97798271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 978e5aeaa0cSDag-Erling Smørgrav return (0); 97998271db4SGarrett Wollman } 98098271db4SGarrett Wollman 981fc3fcacfSRobert Watson if (req->newptr != NULL) 982e5aeaa0cSDag-Erling Smørgrav return (EPERM); 98398271db4SGarrett Wollman 98498271db4SGarrett Wollman /* 98598271db4SGarrett Wollman * OK, now we're committed to doing something. 98698271db4SGarrett Wollman */ 987a163d034SWarner Losh xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 9880d9ce3a1SRobert Watson UNP_LOCK(); 98998271db4SGarrett Wollman gencnt = unp_gencnt; 99098271db4SGarrett Wollman n = unp_count; 9910d9ce3a1SRobert Watson UNP_UNLOCK(); 99298271db4SGarrett Wollman 9938f364875SJulian Elischer xug->xug_len = sizeof *xug; 9948f364875SJulian Elischer xug->xug_count = n; 9958f364875SJulian Elischer xug->xug_gen = gencnt; 9968f364875SJulian Elischer xug->xug_sogen = so_gencnt; 9978f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 9988f364875SJulian Elischer if (error) { 9998f364875SJulian Elischer free(xug, M_TEMP); 1000e5aeaa0cSDag-Erling Smørgrav return (error); 10018f364875SJulian Elischer } 100298271db4SGarrett Wollman 1003a163d034SWarner Losh unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 100498271db4SGarrett Wollman 10050d9ce3a1SRobert Watson UNP_LOCK(); 10062e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 10072e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 10088a7d8cc6SRobert Watson if (unp->unp_gencnt <= gencnt) { 1009a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred, 10108a7d8cc6SRobert Watson unp->unp_socket->so_cred)) 10114787fd37SPaul Saab continue; 101298271db4SGarrett Wollman unp_list[i++] = unp; 101398271db4SGarrett Wollman } 10144787fd37SPaul Saab } 10150d9ce3a1SRobert Watson UNP_UNLOCK(); 101698271db4SGarrett Wollman n = i; /* in case we lost some during malloc */ 101798271db4SGarrett Wollman 101898271db4SGarrett Wollman error = 0; 1019a163d034SWarner Losh xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK); 102098271db4SGarrett Wollman for (i = 0; i < n; i++) { 102198271db4SGarrett Wollman unp = unp_list[i]; 102298271db4SGarrett Wollman if (unp->unp_gencnt <= gencnt) { 10238f364875SJulian Elischer xu->xu_len = sizeof *xu; 10248f364875SJulian Elischer xu->xu_unpp = unp; 102598271db4SGarrett Wollman /* 102698271db4SGarrett Wollman * XXX - need more locking here to protect against 102798271db4SGarrett Wollman * connect/disconnect races for SMP. 102898271db4SGarrett Wollman */ 1029fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 10308f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr, 103198271db4SGarrett Wollman unp->unp_addr->sun_len); 1032fc3fcacfSRobert Watson if (unp->unp_conn != NULL && 1033fc3fcacfSRobert Watson unp->unp_conn->unp_addr != NULL) 103498271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 10358f364875SJulian Elischer &xu->xu_caddr, 103698271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 10378f364875SJulian Elischer bcopy(unp, &xu->xu_unp, sizeof *unp); 10388f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket); 10398f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu); 104098271db4SGarrett Wollman } 104198271db4SGarrett Wollman } 10428f364875SJulian Elischer free(xu, M_TEMP); 104398271db4SGarrett Wollman if (!error) { 104498271db4SGarrett Wollman /* 104598271db4SGarrett Wollman * Give the user an updated idea of our state. 104698271db4SGarrett Wollman * If the generation differs from what we told 104798271db4SGarrett Wollman * her before, she knows that something happened 104898271db4SGarrett Wollman * while we were processing this request, and it 104998271db4SGarrett Wollman * might be necessary to retry. 105098271db4SGarrett Wollman */ 10518f364875SJulian Elischer xug->xug_gen = unp_gencnt; 10528f364875SJulian Elischer xug->xug_sogen = so_gencnt; 10538f364875SJulian Elischer xug->xug_count = unp_count; 10548f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 105598271db4SGarrett Wollman } 105698271db4SGarrett Wollman free(unp_list, M_TEMP); 10578f364875SJulian Elischer free(xug, M_TEMP); 1058e5aeaa0cSDag-Erling Smørgrav return (error); 105998271db4SGarrett Wollman } 106098271db4SGarrett Wollman 106198271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 106298271db4SGarrett Wollman (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 106398271db4SGarrett Wollman "List of active local datagram sockets"); 106498271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 106598271db4SGarrett Wollman (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 106698271db4SGarrett Wollman "List of active local stream sockets"); 106798271db4SGarrett Wollman 1068f708ef1bSPoul-Henning Kamp static void 1069df8bae1dSRodney W. Grimes unp_shutdown(unp) 1070df8bae1dSRodney W. Grimes struct unpcb *unp; 1071df8bae1dSRodney W. Grimes { 1072df8bae1dSRodney W. Grimes struct socket *so; 1073df8bae1dSRodney W. Grimes 10740d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 10750d9ce3a1SRobert Watson 1076df8bae1dSRodney W. Grimes if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 1077df8bae1dSRodney W. Grimes (so = unp->unp_conn->unp_socket)) 1078df8bae1dSRodney W. Grimes socantrcvmore(so); 1079df8bae1dSRodney W. Grimes } 1080df8bae1dSRodney W. Grimes 1081f708ef1bSPoul-Henning Kamp static void 1082df8bae1dSRodney W. Grimes unp_drop(unp, errno) 1083df8bae1dSRodney W. Grimes struct unpcb *unp; 1084df8bae1dSRodney W. Grimes int errno; 1085df8bae1dSRodney W. Grimes { 1086df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 1087df8bae1dSRodney W. Grimes 10880d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 10890d9ce3a1SRobert Watson 1090df8bae1dSRodney W. Grimes so->so_error = errno; 1091df8bae1dSRodney W. Grimes unp_disconnect(unp); 1092df8bae1dSRodney W. Grimes } 1093df8bae1dSRodney W. Grimes 1094df8bae1dSRodney W. Grimes #ifdef notdef 109526f9a767SRodney W. Grimes void 1096df8bae1dSRodney W. Grimes unp_drain() 1097df8bae1dSRodney W. Grimes { 1098df8bae1dSRodney W. Grimes 1099df8bae1dSRodney W. Grimes } 1100df8bae1dSRodney W. Grimes #endif 1101df8bae1dSRodney W. Grimes 11022bc21ed9SDavid Malone static void 11032bc21ed9SDavid Malone unp_freerights(rp, fdcount) 11042bc21ed9SDavid Malone struct file **rp; 11052bc21ed9SDavid Malone int fdcount; 1106df8bae1dSRodney W. Grimes { 11072bc21ed9SDavid Malone int i; 11082bc21ed9SDavid Malone struct file *fp; 1109df8bae1dSRodney W. Grimes 11102bc21ed9SDavid Malone for (i = 0; i < fdcount; i++) { 1111df8bae1dSRodney W. Grimes fp = *rp; 11128692c025SYoshinobu Inoue /* 11132bc21ed9SDavid Malone * zero the pointer before calling 11142bc21ed9SDavid Malone * unp_discard since it may end up 11152bc21ed9SDavid Malone * in unp_gc().. 11168692c025SYoshinobu Inoue */ 1117df8bae1dSRodney W. Grimes *rp++ = 0; 11188692c025SYoshinobu Inoue unp_discard(fp); 1119df8bae1dSRodney W. Grimes } 11202bc21ed9SDavid Malone } 11212bc21ed9SDavid Malone 11222bc21ed9SDavid Malone int 11232bc21ed9SDavid Malone unp_externalize(control, controlp) 11242bc21ed9SDavid Malone struct mbuf *control, **controlp; 11252bc21ed9SDavid Malone { 11262bc21ed9SDavid Malone struct thread *td = curthread; /* XXX */ 11272bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 11282bc21ed9SDavid Malone int i; 11292bc21ed9SDavid Malone int *fdp; 11302bc21ed9SDavid Malone struct file **rp; 11312bc21ed9SDavid Malone struct file *fp; 11322bc21ed9SDavid Malone void *data; 11332bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 11342bc21ed9SDavid Malone int error, newfds; 11352bc21ed9SDavid Malone int f; 11362bc21ed9SDavid Malone u_int newlen; 11372bc21ed9SDavid Malone 11382bc21ed9SDavid Malone error = 0; 11392bc21ed9SDavid Malone if (controlp != NULL) /* controlp == NULL => free control messages */ 11402bc21ed9SDavid Malone *controlp = NULL; 11412bc21ed9SDavid Malone 11422bc21ed9SDavid Malone while (cm != NULL) { 11432bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 11442bc21ed9SDavid Malone error = EINVAL; 11452bc21ed9SDavid Malone break; 11462bc21ed9SDavid Malone } 11472bc21ed9SDavid Malone 11482bc21ed9SDavid Malone data = CMSG_DATA(cm); 11492bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 11502bc21ed9SDavid Malone 11512bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET 11522bc21ed9SDavid Malone && cm->cmsg_type == SCM_RIGHTS) { 11532bc21ed9SDavid Malone newfds = datalen / sizeof(struct file *); 11542bc21ed9SDavid Malone rp = data; 11552bc21ed9SDavid Malone 1156e2f9a08bSOlivier Houchard /* If we're not outputting the descriptors free them. */ 11572bc21ed9SDavid Malone if (error || controlp == NULL) { 11582bc21ed9SDavid Malone unp_freerights(rp, newfds); 11592bc21ed9SDavid Malone goto next; 11602bc21ed9SDavid Malone } 1161426da3bcSAlfred Perlstein FILEDESC_LOCK(td->td_proc->p_fd); 11622bc21ed9SDavid Malone /* if the new FD's will not fit free them. */ 11632bc21ed9SDavid Malone if (!fdavail(td, newfds)) { 1164426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 11652bc21ed9SDavid Malone error = EMSGSIZE; 11662bc21ed9SDavid Malone unp_freerights(rp, newfds); 11672bc21ed9SDavid Malone goto next; 1168df8bae1dSRodney W. Grimes } 1169ed5b7817SJulian Elischer /* 11702bc21ed9SDavid Malone * now change each pointer to an fd in the global 11712bc21ed9SDavid Malone * table to an integer that is the index to the 11722bc21ed9SDavid Malone * local fd table entry that we set up to point 11732bc21ed9SDavid Malone * to the global one we are transferring. 1174ed5b7817SJulian Elischer */ 11752bc21ed9SDavid Malone newlen = newfds * sizeof(int); 11762bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 11772bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 11782bc21ed9SDavid Malone if (*controlp == NULL) { 1179426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 11802bc21ed9SDavid Malone error = E2BIG; 11812bc21ed9SDavid Malone unp_freerights(rp, newfds); 11822bc21ed9SDavid Malone goto next; 11832bc21ed9SDavid Malone } 11842bc21ed9SDavid Malone 11852bc21ed9SDavid Malone fdp = (int *) 11862bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1187df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 1188a6d4491cSDag-Erling Smørgrav if (fdalloc(td, 0, &f)) 11892bc21ed9SDavid Malone panic("unp_externalize fdalloc failed"); 11908692c025SYoshinobu Inoue fp = *rp++; 1191b40ce416SJulian Elischer td->td_proc->p_fd->fd_ofiles[f] = fp; 1192426da3bcSAlfred Perlstein FILE_LOCK(fp); 1193df8bae1dSRodney W. Grimes fp->f_msgcount--; 1194426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1195df8bae1dSRodney W. Grimes unp_rights--; 11968692c025SYoshinobu Inoue *fdp++ = f; 1197df8bae1dSRodney W. Grimes } 1198426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 11992bc21ed9SDavid Malone } else { /* We can just copy anything else across */ 12002bc21ed9SDavid Malone if (error || controlp == NULL) 12012bc21ed9SDavid Malone goto next; 12022bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, datalen, 12032bc21ed9SDavid Malone cm->cmsg_type, cm->cmsg_level); 12042bc21ed9SDavid Malone if (*controlp == NULL) { 12052bc21ed9SDavid Malone error = ENOBUFS; 12062bc21ed9SDavid Malone goto next; 12072bc21ed9SDavid Malone } 12082bc21ed9SDavid Malone bcopy(data, 12092bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 12102bc21ed9SDavid Malone datalen); 12112bc21ed9SDavid Malone } 12122bc21ed9SDavid Malone 12132bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 12142bc21ed9SDavid Malone 12152bc21ed9SDavid Malone next: 12162bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 12172bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 12182bc21ed9SDavid Malone cm = (struct cmsghdr *) 12192bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 12208692c025SYoshinobu Inoue } else { 12212bc21ed9SDavid Malone clen = 0; 12222bc21ed9SDavid Malone cm = NULL; 12238692c025SYoshinobu Inoue } 12248692c025SYoshinobu Inoue } 12258692c025SYoshinobu Inoue 12262bc21ed9SDavid Malone m_freem(control); 12272bc21ed9SDavid Malone 12282bc21ed9SDavid Malone return (error); 1229df8bae1dSRodney W. Grimes } 1230df8bae1dSRodney W. Grimes 123198271db4SGarrett Wollman void 123298271db4SGarrett Wollman unp_init(void) 123398271db4SGarrett Wollman { 12349e9d298aSJeff Roberson unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 12359e9d298aSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 1236fc3fcacfSRobert Watson if (unp_zone == NULL) 123798271db4SGarrett Wollman panic("unp_init"); 1238b17dd2bcSColin Percival uma_zone_set_max(unp_zone, nmbclusters); 123998271db4SGarrett Wollman LIST_INIT(&unp_dhead); 124098271db4SGarrett Wollman LIST_INIT(&unp_shead); 12410d9ce3a1SRobert Watson 12420d9ce3a1SRobert Watson UNP_LOCK_INIT(); 124398271db4SGarrett Wollman } 124498271db4SGarrett Wollman 1245f708ef1bSPoul-Henning Kamp static int 12462bc21ed9SDavid Malone unp_internalize(controlp, td) 12472bc21ed9SDavid Malone struct mbuf **controlp; 1248b40ce416SJulian Elischer struct thread *td; 1249df8bae1dSRodney W. Grimes { 12502bc21ed9SDavid Malone struct mbuf *control = *controlp; 1251b40ce416SJulian Elischer struct proc *p = td->td_proc; 12528692c025SYoshinobu Inoue struct filedesc *fdescp = p->p_fd; 12532bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 12542bc21ed9SDavid Malone struct cmsgcred *cmcred; 12552bc21ed9SDavid Malone struct file **rp; 12562bc21ed9SDavid Malone struct file *fp; 12572bc21ed9SDavid Malone struct timeval *tv; 12582bc21ed9SDavid Malone int i, fd, *fdp; 12592bc21ed9SDavid Malone void *data; 12602bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 12612bc21ed9SDavid Malone int error, oldfds; 12628692c025SYoshinobu Inoue u_int newlen; 1263df8bae1dSRodney W. Grimes 12642bc21ed9SDavid Malone error = 0; 12652bc21ed9SDavid Malone *controlp = NULL; 12660b788fa1SBill Paul 12672bc21ed9SDavid Malone while (cm != NULL) { 12682bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 12692bc21ed9SDavid Malone || cm->cmsg_len > clen) { 12702bc21ed9SDavid Malone error = EINVAL; 12712bc21ed9SDavid Malone goto out; 12722bc21ed9SDavid Malone } 12732bc21ed9SDavid Malone 12742bc21ed9SDavid Malone data = CMSG_DATA(cm); 12752bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 12762bc21ed9SDavid Malone 12772bc21ed9SDavid Malone switch (cm->cmsg_type) { 12780b788fa1SBill Paul /* 12790b788fa1SBill Paul * Fill in credential information. 12800b788fa1SBill Paul */ 12812bc21ed9SDavid Malone case SCM_CREDS: 12822bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 12832bc21ed9SDavid Malone SCM_CREDS, SOL_SOCKET); 12842bc21ed9SDavid Malone if (*controlp == NULL) { 12852bc21ed9SDavid Malone error = ENOBUFS; 12862bc21ed9SDavid Malone goto out; 12872bc21ed9SDavid Malone } 12882bc21ed9SDavid Malone 12892bc21ed9SDavid Malone cmcred = (struct cmsgcred *) 12902bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 12910b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 1292a854ed98SJohn Baldwin cmcred->cmcred_uid = td->td_ucred->cr_ruid; 1293a854ed98SJohn Baldwin cmcred->cmcred_gid = td->td_ucred->cr_rgid; 1294a854ed98SJohn Baldwin cmcred->cmcred_euid = td->td_ucred->cr_uid; 1295a854ed98SJohn Baldwin cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 12960b788fa1SBill Paul CMGROUP_MAX); 12970b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 12982bc21ed9SDavid Malone cmcred->cmcred_groups[i] = 1299a854ed98SJohn Baldwin td->td_ucred->cr_groups[i]; 13002bc21ed9SDavid Malone break; 13010b788fa1SBill Paul 13022bc21ed9SDavid Malone case SCM_RIGHTS: 13032bc21ed9SDavid Malone oldfds = datalen / sizeof (int); 1304ed5b7817SJulian Elischer /* 13052bc21ed9SDavid Malone * check that all the FDs passed in refer to legal files 1306ed5b7817SJulian Elischer * If not, reject the entire operation. 1307ed5b7817SJulian Elischer */ 13082bc21ed9SDavid Malone fdp = data; 1309426da3bcSAlfred Perlstein FILEDESC_LOCK(fdescp); 1310df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 13118692c025SYoshinobu Inoue fd = *fdp++; 13128692c025SYoshinobu Inoue if ((unsigned)fd >= fdescp->fd_nfiles || 13132bc21ed9SDavid Malone fdescp->fd_ofiles[fd] == NULL) { 1314426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 13152bc21ed9SDavid Malone error = EBADF; 13162bc21ed9SDavid Malone goto out; 13172bc21ed9SDavid Malone } 1318e7d6662fSAlfred Perlstein fp = fdescp->fd_ofiles[fd]; 1319e7d6662fSAlfred Perlstein if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 1320e7d6662fSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 1321e7d6662fSAlfred Perlstein error = EOPNOTSUPP; 1322e7d6662fSAlfred Perlstein goto out; 1323e7d6662fSAlfred Perlstein } 1324e7d6662fSAlfred Perlstein 1325df8bae1dSRodney W. Grimes } 1326ed5b7817SJulian Elischer /* 1327ed5b7817SJulian Elischer * Now replace the integer FDs with pointers to 1328ed5b7817SJulian Elischer * the associated global file table entry.. 1329ed5b7817SJulian Elischer */ 13302bc21ed9SDavid Malone newlen = oldfds * sizeof(struct file *); 13312bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 13322bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 13332bc21ed9SDavid Malone if (*controlp == NULL) { 1334426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 13352bc21ed9SDavid Malone error = E2BIG; 13362bc21ed9SDavid Malone goto out; 13378692c025SYoshinobu Inoue } 13388692c025SYoshinobu Inoue 13392bc21ed9SDavid Malone fdp = data; 13402bc21ed9SDavid Malone rp = (struct file **) 13412bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 13428692c025SYoshinobu Inoue for (i = 0; i < oldfds; i++) { 13438692c025SYoshinobu Inoue fp = fdescp->fd_ofiles[*fdp++]; 1344df8bae1dSRodney W. Grimes *rp++ = fp; 1345426da3bcSAlfred Perlstein FILE_LOCK(fp); 1346df8bae1dSRodney W. Grimes fp->f_count++; 1347df8bae1dSRodney W. Grimes fp->f_msgcount++; 1348426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1349df8bae1dSRodney W. Grimes unp_rights++; 1350df8bae1dSRodney W. Grimes } 1351426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 13522bc21ed9SDavid Malone break; 13532bc21ed9SDavid Malone 13542bc21ed9SDavid Malone case SCM_TIMESTAMP: 13552bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*tv), 13562bc21ed9SDavid Malone SCM_TIMESTAMP, SOL_SOCKET); 13572bc21ed9SDavid Malone if (*controlp == NULL) { 13582bc21ed9SDavid Malone error = ENOBUFS; 13592bc21ed9SDavid Malone goto out; 13608692c025SYoshinobu Inoue } 13612bc21ed9SDavid Malone tv = (struct timeval *) 13622bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 13632bc21ed9SDavid Malone microtime(tv); 13642bc21ed9SDavid Malone break; 13652bc21ed9SDavid Malone 13662bc21ed9SDavid Malone default: 13672bc21ed9SDavid Malone error = EINVAL; 13682bc21ed9SDavid Malone goto out; 13692bc21ed9SDavid Malone } 13702bc21ed9SDavid Malone 13712bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 13722bc21ed9SDavid Malone 13732bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 13742bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 13752bc21ed9SDavid Malone cm = (struct cmsghdr *) 13762bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 13772bc21ed9SDavid Malone } else { 13782bc21ed9SDavid Malone clen = 0; 13792bc21ed9SDavid Malone cm = NULL; 13802bc21ed9SDavid Malone } 13812bc21ed9SDavid Malone } 13822bc21ed9SDavid Malone 13832bc21ed9SDavid Malone out: 13842bc21ed9SDavid Malone m_freem(control); 13852bc21ed9SDavid Malone 13862bc21ed9SDavid Malone return (error); 1387df8bae1dSRodney W. Grimes } 1388df8bae1dSRodney W. Grimes 1389f708ef1bSPoul-Henning Kamp static int unp_defer, unp_gcing; 1390df8bae1dSRodney W. Grimes 1391f708ef1bSPoul-Henning Kamp static void 1392df8bae1dSRodney W. Grimes unp_gc() 1393df8bae1dSRodney W. Grimes { 1394df8bae1dSRodney W. Grimes register struct file *fp, *nextfp; 1395df8bae1dSRodney W. Grimes register struct socket *so; 1396df8bae1dSRodney W. Grimes struct file **extra_ref, **fpp; 1397df8bae1dSRodney W. Grimes int nunref, i; 139895f004dcSAlfred Perlstein int nfiles_snap; 139995f004dcSAlfred Perlstein int nfiles_slack = 20; 1400df8bae1dSRodney W. Grimes 14010d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 14020d9ce3a1SRobert Watson 1403df8bae1dSRodney W. Grimes if (unp_gcing) 1404df8bae1dSRodney W. Grimes return; 1405df8bae1dSRodney W. Grimes unp_gcing = 1; 1406df8bae1dSRodney W. Grimes unp_defer = 0; 1407ed5b7817SJulian Elischer /* 1408ed5b7817SJulian Elischer * before going through all this, set all FDs to 1409ed5b7817SJulian Elischer * be NOT defered and NOT externally accessible 1410ed5b7817SJulian Elischer */ 14110d9ce3a1SRobert Watson /* 14120d9ce3a1SRobert Watson * XXXRW: Acquiring a sleep lock while holding UNP 14130d9ce3a1SRobert Watson * mutex cannot be a good thing. 14140d9ce3a1SRobert Watson */ 1415426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 14162e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) 1417426da3bcSAlfred Perlstein fp->f_gcflag &= ~(FMARK|FDEFER); 1418df8bae1dSRodney W. Grimes do { 14192e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) { 1420426da3bcSAlfred Perlstein FILE_LOCK(fp); 1421ed5b7817SJulian Elischer /* 1422ed5b7817SJulian Elischer * If the file is not open, skip it 1423ed5b7817SJulian Elischer */ 1424426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1425426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1426df8bae1dSRodney W. Grimes continue; 1427426da3bcSAlfred Perlstein } 1428ed5b7817SJulian Elischer /* 1429ed5b7817SJulian Elischer * If we already marked it as 'defer' in a 1430ed5b7817SJulian Elischer * previous pass, then try process it this time 1431ed5b7817SJulian Elischer * and un-mark it 1432ed5b7817SJulian Elischer */ 1433426da3bcSAlfred Perlstein if (fp->f_gcflag & FDEFER) { 1434426da3bcSAlfred Perlstein fp->f_gcflag &= ~FDEFER; 1435df8bae1dSRodney W. Grimes unp_defer--; 1436df8bae1dSRodney W. Grimes } else { 1437ed5b7817SJulian Elischer /* 1438ed5b7817SJulian Elischer * if it's not defered, then check if it's 1439ed5b7817SJulian Elischer * already marked.. if so skip it 1440ed5b7817SJulian Elischer */ 1441426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) { 1442426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1443df8bae1dSRodney W. Grimes continue; 1444426da3bcSAlfred Perlstein } 1445ed5b7817SJulian Elischer /* 1446ed5b7817SJulian Elischer * If all references are from messages 1447ed5b7817SJulian Elischer * in transit, then skip it. it's not 1448ed5b7817SJulian Elischer * externally accessible. 1449ed5b7817SJulian Elischer */ 1450426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount) { 1451426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1452df8bae1dSRodney W. Grimes continue; 1453426da3bcSAlfred Perlstein } 1454ed5b7817SJulian Elischer /* 1455ed5b7817SJulian Elischer * If it got this far then it must be 1456ed5b7817SJulian Elischer * externally accessible. 1457ed5b7817SJulian Elischer */ 1458426da3bcSAlfred Perlstein fp->f_gcflag |= FMARK; 1459df8bae1dSRodney W. Grimes } 1460ed5b7817SJulian Elischer /* 1461ed5b7817SJulian Elischer * either it was defered, or it is externally 1462ed5b7817SJulian Elischer * accessible and not already marked so. 1463ed5b7817SJulian Elischer * Now check if it is possibly one of OUR sockets. 1464ed5b7817SJulian Elischer */ 1465df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_SOCKET || 146648e3128bSMatthew Dillon (so = fp->f_data) == NULL) { 1467426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1468df8bae1dSRodney W. Grimes continue; 1469426da3bcSAlfred Perlstein } 1470426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1471748e0b0aSGarrett Wollman if (so->so_proto->pr_domain != &localdomain || 1472df8bae1dSRodney W. Grimes (so->so_proto->pr_flags&PR_RIGHTS) == 0) 1473df8bae1dSRodney W. Grimes continue; 1474df8bae1dSRodney W. Grimes #ifdef notdef 1475df8bae1dSRodney W. Grimes if (so->so_rcv.sb_flags & SB_LOCK) { 1476df8bae1dSRodney W. Grimes /* 1477df8bae1dSRodney W. Grimes * This is problematical; it's not clear 1478df8bae1dSRodney W. Grimes * we need to wait for the sockbuf to be 1479df8bae1dSRodney W. Grimes * unlocked (on a uniprocessor, at least), 1480df8bae1dSRodney W. Grimes * and it's also not clear what to do 1481df8bae1dSRodney W. Grimes * if sbwait returns an error due to receipt 1482df8bae1dSRodney W. Grimes * of a signal. If sbwait does return 1483df8bae1dSRodney W. Grimes * an error, we'll go into an infinite 1484df8bae1dSRodney W. Grimes * loop. Delete all of this for now. 1485df8bae1dSRodney W. Grimes */ 1486df8bae1dSRodney W. Grimes (void) sbwait(&so->so_rcv); 1487df8bae1dSRodney W. Grimes goto restart; 1488df8bae1dSRodney W. Grimes } 1489df8bae1dSRodney W. Grimes #endif 1490ed5b7817SJulian Elischer /* 1491ed5b7817SJulian Elischer * So, Ok, it's one of our sockets and it IS externally 1492ed5b7817SJulian Elischer * accessible (or was defered). Now we look 1493dc733423SDag-Erling Smørgrav * to see if we hold any file descriptors in its 1494ed5b7817SJulian Elischer * message buffers. Follow those links and mark them 1495ed5b7817SJulian Elischer * as accessible too. 1496ed5b7817SJulian Elischer */ 14977717cf07SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1498df8bae1dSRodney W. Grimes unp_scan(so->so_rcv.sb_mb, unp_mark); 14997717cf07SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 1500df8bae1dSRodney W. Grimes } 1501df8bae1dSRodney W. Grimes } while (unp_defer); 1502426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1503df8bae1dSRodney W. Grimes /* 1504df8bae1dSRodney W. Grimes * We grab an extra reference to each of the file table entries 1505df8bae1dSRodney W. Grimes * that are not otherwise accessible and then free the rights 1506df8bae1dSRodney W. Grimes * that are stored in messages on them. 1507df8bae1dSRodney W. Grimes * 1508df8bae1dSRodney W. Grimes * The bug in the orginal code is a little tricky, so I'll describe 1509df8bae1dSRodney W. Grimes * what's wrong with it here. 1510df8bae1dSRodney W. Grimes * 1511df8bae1dSRodney W. Grimes * It is incorrect to simply unp_discard each entry for f_msgcount 1512df8bae1dSRodney W. Grimes * times -- consider the case of sockets A and B that contain 1513df8bae1dSRodney W. Grimes * references to each other. On a last close of some other socket, 1514df8bae1dSRodney W. Grimes * we trigger a gc since the number of outstanding rights (unp_rights) 1515df8bae1dSRodney W. Grimes * is non-zero. If during the sweep phase the gc code un_discards, 1516df8bae1dSRodney W. Grimes * we end up doing a (full) closef on the descriptor. A closef on A 1517df8bae1dSRodney W. Grimes * results in the following chain. Closef calls soo_close, which 1518df8bae1dSRodney W. Grimes * calls soclose. Soclose calls first (through the switch 1519df8bae1dSRodney W. Grimes * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 1520df8bae1dSRodney W. Grimes * returns because the previous instance had set unp_gcing, and 1521df8bae1dSRodney W. Grimes * we return all the way back to soclose, which marks the socket 1522df8bae1dSRodney W. Grimes * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush 1523df8bae1dSRodney W. Grimes * to free up the rights that are queued in messages on the socket A, 1524df8bae1dSRodney W. Grimes * i.e., the reference on B. The sorflush calls via the dom_dispose 1525df8bae1dSRodney W. Grimes * switch unp_dispose, which unp_scans with unp_discard. This second 1526df8bae1dSRodney W. Grimes * instance of unp_discard just calls closef on B. 1527df8bae1dSRodney W. Grimes * 1528df8bae1dSRodney W. Grimes * Well, a similar chain occurs on B, resulting in a sorflush on B, 1529df8bae1dSRodney W. Grimes * which results in another closef on A. Unfortunately, A is already 1530df8bae1dSRodney W. Grimes * being closed, and the descriptor has already been marked with 1531df8bae1dSRodney W. Grimes * SS_NOFDREF, and soclose panics at this point. 1532df8bae1dSRodney W. Grimes * 1533df8bae1dSRodney W. Grimes * Here, we first take an extra reference to each inaccessible 1534df8bae1dSRodney W. Grimes * descriptor. Then, we call sorflush ourself, since we know 1535df8bae1dSRodney W. Grimes * it is a Unix domain socket anyhow. After we destroy all the 1536df8bae1dSRodney W. Grimes * rights carried in messages, we do a last closef to get rid 1537df8bae1dSRodney W. Grimes * of our extra reference. This is the last close, and the 1538df8bae1dSRodney W. Grimes * unp_detach etc will shut down the socket. 1539df8bae1dSRodney W. Grimes * 1540df8bae1dSRodney W. Grimes * 91/09/19, bsy@cs.cmu.edu 1541df8bae1dSRodney W. Grimes */ 154295f004dcSAlfred Perlstein again: 154395f004dcSAlfred Perlstein nfiles_snap = nfiles + nfiles_slack; /* some slack */ 154495f004dcSAlfred Perlstein extra_ref = malloc(nfiles_snap * sizeof(struct file *), M_TEMP, 154595f004dcSAlfred Perlstein M_WAITOK); 1546426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 154795f004dcSAlfred Perlstein if (nfiles_snap < nfiles) { 154895f004dcSAlfred Perlstein sx_sunlock(&filelist_lock); 154995f004dcSAlfred Perlstein free(extra_ref, M_TEMP); 155095f004dcSAlfred Perlstein nfiles_slack += 20; 155195f004dcSAlfred Perlstein goto again; 155295f004dcSAlfred Perlstein } 1553fc3fcacfSRobert Watson for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; 1554fc3fcacfSRobert Watson fp != NULL; fp = nextfp) { 15552e3c8fcbSPoul-Henning Kamp nextfp = LIST_NEXT(fp, f_list); 1556426da3bcSAlfred Perlstein FILE_LOCK(fp); 1557ed5b7817SJulian Elischer /* 1558ed5b7817SJulian Elischer * If it's not open, skip it 1559ed5b7817SJulian Elischer */ 1560426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1561426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1562df8bae1dSRodney W. Grimes continue; 1563426da3bcSAlfred Perlstein } 1564ed5b7817SJulian Elischer /* 1565ed5b7817SJulian Elischer * If all refs are from msgs, and it's not marked accessible 1566ed5b7817SJulian Elischer * then it must be referenced from some unreachable cycle 1567ed5b7817SJulian Elischer * of (shut-down) FDs, so include it in our 1568ed5b7817SJulian Elischer * list of FDs to remove 1569ed5b7817SJulian Elischer */ 1570426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) { 1571df8bae1dSRodney W. Grimes *fpp++ = fp; 1572df8bae1dSRodney W. Grimes nunref++; 1573df8bae1dSRodney W. Grimes fp->f_count++; 1574df8bae1dSRodney W. Grimes } 1575426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1576df8bae1dSRodney W. Grimes } 1577426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1578ed5b7817SJulian Elischer /* 1579ed5b7817SJulian Elischer * for each FD on our hit list, do the following two things 1580ed5b7817SJulian Elischer */ 15811c7c3c6aSMatthew Dillon for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 15821c7c3c6aSMatthew Dillon struct file *tfp = *fpp; 1583426da3bcSAlfred Perlstein FILE_LOCK(tfp); 1584cd72f218SMatthew Dillon if (tfp->f_type == DTYPE_SOCKET && 158548e3128bSMatthew Dillon tfp->f_data != NULL) { 1586426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 158748e3128bSMatthew Dillon sorflush(tfp->f_data); 1588e5aeaa0cSDag-Erling Smørgrav } else { 1589426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 15901c7c3c6aSMatthew Dillon } 1591e5aeaa0cSDag-Erling Smørgrav } 1592df8bae1dSRodney W. Grimes for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) 1593b40ce416SJulian Elischer closef(*fpp, (struct thread *) NULL); 1594210a5a71SAlfred Perlstein free(extra_ref, M_TEMP); 1595df8bae1dSRodney W. Grimes unp_gcing = 0; 1596df8bae1dSRodney W. Grimes } 1597df8bae1dSRodney W. Grimes 159826f9a767SRodney W. Grimes void 1599df8bae1dSRodney W. Grimes unp_dispose(m) 1600df8bae1dSRodney W. Grimes struct mbuf *m; 1601df8bae1dSRodney W. Grimes { 1602996c772fSJohn Dyson 1603df8bae1dSRodney W. Grimes if (m) 1604df8bae1dSRodney W. Grimes unp_scan(m, unp_discard); 1605df8bae1dSRodney W. Grimes } 1606df8bae1dSRodney W. Grimes 16070c1bb4fbSDima Dorfman static int 16086f105b34SJohn Baldwin unp_listen(unp, td) 16090c1bb4fbSDima Dorfman struct unpcb *unp; 16106f105b34SJohn Baldwin struct thread *td; 16110c1bb4fbSDima Dorfman { 16120d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 16130c1bb4fbSDima Dorfman 16140d9ce3a1SRobert Watson /* 16150d9ce3a1SRobert Watson * XXXRW: Why populate the local peer cred with our own credential? 16160d9ce3a1SRobert Watson */ 16176f105b34SJohn Baldwin cru2x(td->td_ucred, &unp->unp_peercred); 16180c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPCCACHED; 16190c1bb4fbSDima Dorfman return (0); 16200c1bb4fbSDima Dorfman } 16210c1bb4fbSDima Dorfman 1622f708ef1bSPoul-Henning Kamp static void 1623df8bae1dSRodney W. Grimes unp_scan(m0, op) 1624df8bae1dSRodney W. Grimes register struct mbuf *m0; 16254d77a549SAlfred Perlstein void (*op)(struct file *); 1626df8bae1dSRodney W. Grimes { 16272bc21ed9SDavid Malone struct mbuf *m; 16282bc21ed9SDavid Malone struct file **rp; 16292bc21ed9SDavid Malone struct cmsghdr *cm; 16302bc21ed9SDavid Malone void *data; 16312bc21ed9SDavid Malone int i; 16322bc21ed9SDavid Malone socklen_t clen, datalen; 1633df8bae1dSRodney W. Grimes int qfds; 1634df8bae1dSRodney W. Grimes 1635fc3fcacfSRobert Watson while (m0 != NULL) { 16362bc21ed9SDavid Malone for (m = m0; m; m = m->m_next) { 163712396bdcSDavid Malone if (m->m_type != MT_CONTROL) 1638df8bae1dSRodney W. Grimes continue; 16392bc21ed9SDavid Malone 16402bc21ed9SDavid Malone cm = mtod(m, struct cmsghdr *); 16412bc21ed9SDavid Malone clen = m->m_len; 16422bc21ed9SDavid Malone 16432bc21ed9SDavid Malone while (cm != NULL) { 16442bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) 16452bc21ed9SDavid Malone break; 16462bc21ed9SDavid Malone 16472bc21ed9SDavid Malone data = CMSG_DATA(cm); 16482bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len 16492bc21ed9SDavid Malone - (caddr_t)data; 16502bc21ed9SDavid Malone 16512bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET && 16522bc21ed9SDavid Malone cm->cmsg_type == SCM_RIGHTS) { 16532bc21ed9SDavid Malone qfds = datalen / sizeof (struct file *); 16542bc21ed9SDavid Malone rp = data; 1655df8bae1dSRodney W. Grimes for (i = 0; i < qfds; i++) 1656df8bae1dSRodney W. Grimes (*op)(*rp++); 16572bc21ed9SDavid Malone } 16582bc21ed9SDavid Malone 16592bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 16602bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 16612bc21ed9SDavid Malone cm = (struct cmsghdr *) 16622bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 16632bc21ed9SDavid Malone } else { 16642bc21ed9SDavid Malone clen = 0; 16652bc21ed9SDavid Malone cm = NULL; 16662bc21ed9SDavid Malone } 16672bc21ed9SDavid Malone } 1668df8bae1dSRodney W. Grimes } 1669df8bae1dSRodney W. Grimes m0 = m0->m_act; 1670df8bae1dSRodney W. Grimes } 1671df8bae1dSRodney W. Grimes } 1672df8bae1dSRodney W. Grimes 1673f708ef1bSPoul-Henning Kamp static void 1674df8bae1dSRodney W. Grimes unp_mark(fp) 1675df8bae1dSRodney W. Grimes struct file *fp; 1676df8bae1dSRodney W. Grimes { 1677426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) 1678df8bae1dSRodney W. Grimes return; 1679df8bae1dSRodney W. Grimes unp_defer++; 1680426da3bcSAlfred Perlstein fp->f_gcflag |= (FMARK|FDEFER); 1681df8bae1dSRodney W. Grimes } 1682df8bae1dSRodney W. Grimes 1683f708ef1bSPoul-Henning Kamp static void 1684df8bae1dSRodney W. Grimes unp_discard(fp) 1685df8bae1dSRodney W. Grimes struct file *fp; 1686df8bae1dSRodney W. Grimes { 1687426da3bcSAlfred Perlstein FILE_LOCK(fp); 1688df8bae1dSRodney W. Grimes fp->f_msgcount--; 1689df8bae1dSRodney W. Grimes unp_rights--; 1690426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1691b40ce416SJulian Elischer (void) closef(fp, (struct thread *)NULL); 1692df8bae1dSRodney W. Grimes } 1693