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; 283df8bae1dSRodney W. Grimes /* 284df8bae1dSRodney W. Grimes * Adjust backpressure on sender 285df8bae1dSRodney W. Grimes * and wakeup any waiting to write. 286df8bae1dSRodney W. Grimes */ 287ff8b0106SBrian Feldman so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt; 288ff8b0106SBrian Feldman unp->unp_mbcnt = so->so_rcv.sb_mbcnt; 2896aef685fSBrian Feldman newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - 2906aef685fSBrian Feldman so->so_rcv.sb_cc; 291f535380cSDon Lewis (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat, 2926aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 293ff8b0106SBrian Feldman unp->unp_cc = so->so_rcv.sb_cc; 294df8bae1dSRodney W. Grimes sowwakeup(so2); 295df8bae1dSRodney W. Grimes break; 296df8bae1dSRodney W. Grimes 297df8bae1dSRodney W. Grimes default: 298a29f300eSGarrett Wollman panic("uipc_rcvd unknown socktype"); 299df8bae1dSRodney W. Grimes } 3000d9ce3a1SRobert Watson UNP_UNLOCK(); 301e5aeaa0cSDag-Erling Smørgrav return (0); 302a29f300eSGarrett Wollman } 303df8bae1dSRodney W. Grimes 304a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 305a29f300eSGarrett Wollman 306a29f300eSGarrett Wollman static int 30757bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 308b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 309a29f300eSGarrett Wollman { 310a29f300eSGarrett Wollman int error = 0; 311a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 312a29f300eSGarrett Wollman struct socket *so2; 3136aef685fSBrian Feldman u_long newhiwat; 314a29f300eSGarrett Wollman 315fc3fcacfSRobert Watson if (unp == NULL) { 316a29f300eSGarrett Wollman error = EINVAL; 317a29f300eSGarrett Wollman goto release; 318a29f300eSGarrett Wollman } 319a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 320a29f300eSGarrett Wollman error = EOPNOTSUPP; 321a29f300eSGarrett Wollman goto release; 322a29f300eSGarrett Wollman } 323a29f300eSGarrett Wollman 324fc3fcacfSRobert Watson if (control != NULL && (error = unp_internalize(&control, td))) 325a29f300eSGarrett Wollman goto release; 326df8bae1dSRodney W. Grimes 3270d9ce3a1SRobert Watson UNP_LOCK(); 328a29f300eSGarrett Wollman switch (so->so_type) { 329a29f300eSGarrett Wollman case SOCK_DGRAM: 330a29f300eSGarrett Wollman { 331e7dd9a10SRobert Watson const struct sockaddr *from; 332df8bae1dSRodney W. Grimes 333fc3fcacfSRobert Watson if (nam != NULL) { 334fc3fcacfSRobert Watson if (unp->unp_conn != NULL) { 335df8bae1dSRodney W. Grimes error = EISCONN; 336df8bae1dSRodney W. Grimes break; 337df8bae1dSRodney W. Grimes } 338b40ce416SJulian Elischer error = unp_connect(so, nam, td); 339df8bae1dSRodney W. Grimes if (error) 340df8bae1dSRodney W. Grimes break; 341df8bae1dSRodney W. Grimes } else { 342fc3fcacfSRobert Watson if (unp->unp_conn == NULL) { 343df8bae1dSRodney W. Grimes error = ENOTCONN; 344df8bae1dSRodney W. Grimes break; 345df8bae1dSRodney W. Grimes } 346df8bae1dSRodney W. Grimes } 347df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 348fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 34957bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 350df8bae1dSRodney W. Grimes else 351df8bae1dSRodney W. Grimes from = &sun_noname; 352df8bae1dSRodney W. Grimes if (sbappendaddr(&so2->so_rcv, from, m, control)) { 353df8bae1dSRodney W. Grimes sorwakeup(so2); 354fc3fcacfSRobert Watson m = NULL; 355fc3fcacfSRobert Watson control = NULL; 356e5aeaa0cSDag-Erling Smørgrav } else { 357df8bae1dSRodney W. Grimes error = ENOBUFS; 358e5aeaa0cSDag-Erling Smørgrav } 359fc3fcacfSRobert Watson if (nam != NULL) 360df8bae1dSRodney W. Grimes unp_disconnect(unp); 361df8bae1dSRodney W. Grimes break; 362df8bae1dSRodney W. Grimes } 363df8bae1dSRodney W. Grimes 364df8bae1dSRodney W. Grimes case SOCK_STREAM: 3656b8fda4dSGarrett Wollman /* Connect if not connected yet. */ 3666b8fda4dSGarrett Wollman /* 3676b8fda4dSGarrett Wollman * Note: A better implementation would complain 368402cc72dSDavid Greenman * if not equal to the peer's address. 3696b8fda4dSGarrett Wollman */ 370402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 371fc3fcacfSRobert Watson if (nam != NULL) { 372b40ce416SJulian Elischer error = unp_connect(so, nam, td); 373402cc72dSDavid Greenman if (error) 3746b8fda4dSGarrett Wollman break; /* XXX */ 375402cc72dSDavid Greenman } else { 376402cc72dSDavid Greenman error = ENOTCONN; 377402cc72dSDavid Greenman break; 378402cc72dSDavid Greenman } 379402cc72dSDavid Greenman } 380402cc72dSDavid Greenman 381c0b99ffaSRobert Watson if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 382df8bae1dSRodney W. Grimes error = EPIPE; 383df8bae1dSRodney W. Grimes break; 384df8bae1dSRodney W. Grimes } 385fc3fcacfSRobert Watson if (unp->unp_conn == NULL) 386a29f300eSGarrett Wollman panic("uipc_send connected but no connection?"); 387df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 388df8bae1dSRodney W. Grimes /* 389df8bae1dSRodney W. Grimes * Send to paired receive port, and then reduce 390df8bae1dSRodney W. Grimes * send buffer hiwater marks to maintain backpressure. 391df8bae1dSRodney W. Grimes * Wake up readers. 392df8bae1dSRodney W. Grimes */ 393fc3fcacfSRobert Watson if (control != NULL) { 394ff8b0106SBrian Feldman if (sbappendcontrol(&so2->so_rcv, m, control)) 395fc3fcacfSRobert Watson control = NULL; 396e5aeaa0cSDag-Erling Smørgrav } else { 397ff8b0106SBrian Feldman sbappend(&so2->so_rcv, m); 398e5aeaa0cSDag-Erling Smørgrav } 399ff8b0106SBrian Feldman so->so_snd.sb_mbmax -= 400ff8b0106SBrian Feldman so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt; 401ff8b0106SBrian Feldman unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt; 4026aef685fSBrian Feldman newhiwat = so->so_snd.sb_hiwat - 4036aef685fSBrian Feldman (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc); 404f535380cSDon Lewis (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat, 4056aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 406ff8b0106SBrian Feldman unp->unp_conn->unp_cc = so2->so_rcv.sb_cc; 407df8bae1dSRodney W. Grimes sorwakeup(so2); 408fc3fcacfSRobert Watson m = NULL; 409df8bae1dSRodney W. Grimes break; 410df8bae1dSRodney W. Grimes 411df8bae1dSRodney W. Grimes default: 412a29f300eSGarrett Wollman panic("uipc_send unknown socktype"); 413df8bae1dSRodney W. Grimes } 414a29f300eSGarrett Wollman 4156b8fda4dSGarrett Wollman /* 4166b8fda4dSGarrett Wollman * SEND_EOF is equivalent to a SEND followed by 4176b8fda4dSGarrett Wollman * a SHUTDOWN. 4186b8fda4dSGarrett Wollman */ 419a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 4206b8fda4dSGarrett Wollman socantsendmore(so); 4216b8fda4dSGarrett Wollman unp_shutdown(unp); 4226b8fda4dSGarrett Wollman } 4230d9ce3a1SRobert Watson UNP_UNLOCK(); 424df8bae1dSRodney W. Grimes 425fc3fcacfSRobert Watson if (control != NULL && error != 0) 426bd508d39SDon Lewis unp_dispose(control); 427bd508d39SDon Lewis 428a29f300eSGarrett Wollman release: 429fc3fcacfSRobert Watson if (control != NULL) 430a29f300eSGarrett Wollman m_freem(control); 431fc3fcacfSRobert Watson if (m != NULL) 432a29f300eSGarrett Wollman m_freem(m); 433e5aeaa0cSDag-Erling Smørgrav return (error); 434a29f300eSGarrett Wollman } 435df8bae1dSRodney W. Grimes 436a29f300eSGarrett Wollman static int 437a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 438a29f300eSGarrett Wollman { 439a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 440a29f300eSGarrett Wollman struct socket *so2; 441a29f300eSGarrett Wollman 442fc3fcacfSRobert Watson if (unp == NULL) 443e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 4440d9ce3a1SRobert Watson UNP_LOCK(); 445a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 446fc3fcacfSRobert Watson if (so->so_type == SOCK_STREAM && unp->unp_conn != NULL) { 447df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 448a29f300eSGarrett Wollman sb->st_blksize += so2->so_rcv.sb_cc; 449df8bae1dSRodney W. Grimes } 450f3732fd1SPoul-Henning Kamp sb->st_dev = NODEV; 451df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 4526f782c46SJeffrey Hsu unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 453a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 4540d9ce3a1SRobert Watson UNP_UNLOCK(); 455df8bae1dSRodney W. Grimes return (0); 456a29f300eSGarrett Wollman } 457df8bae1dSRodney W. Grimes 458a29f300eSGarrett Wollman static int 459a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 460a29f300eSGarrett Wollman { 461a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 462df8bae1dSRodney W. Grimes 463fc3fcacfSRobert Watson if (unp == NULL) 464e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 4650d9ce3a1SRobert Watson UNP_LOCK(); 466a29f300eSGarrett Wollman socantsendmore(so); 467a29f300eSGarrett Wollman unp_shutdown(unp); 4680d9ce3a1SRobert Watson UNP_UNLOCK(); 469e5aeaa0cSDag-Erling Smørgrav return (0); 470a29f300eSGarrett Wollman } 471df8bae1dSRodney W. Grimes 472a29f300eSGarrett Wollman static int 47357bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 474a29f300eSGarrett Wollman { 475a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 4760d9ce3a1SRobert Watson const struct sockaddr *sa; 477a29f300eSGarrett Wollman 478fc3fcacfSRobert Watson if (unp == NULL) 479e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 4800d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 4810d9ce3a1SRobert Watson UNP_LOCK(); 482fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 4830d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_addr; 48483f3198bSThomas Moestl else 4850d9ce3a1SRobert Watson sa = &sun_noname; 4860d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 4870d9ce3a1SRobert Watson UNP_UNLOCK(); 488e5aeaa0cSDag-Erling Smørgrav return (0); 489df8bae1dSRodney W. Grimes } 490a29f300eSGarrett Wollman 491a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = { 492a29f300eSGarrett Wollman uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect, 493a29f300eSGarrett Wollman uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect, 494a29f300eSGarrett Wollman uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp, 495a29f300eSGarrett Wollman uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr, 496a557af22SRobert Watson sosend, soreceive, sopoll, pru_sosetlabel_null 497a29f300eSGarrett Wollman }; 498df8bae1dSRodney W. Grimes 4990c1bb4fbSDima Dorfman int 5000c1bb4fbSDima Dorfman uipc_ctloutput(so, sopt) 5010c1bb4fbSDima Dorfman struct socket *so; 5020c1bb4fbSDima Dorfman struct sockopt *sopt; 5030c1bb4fbSDima Dorfman { 5040c1bb4fbSDima Dorfman struct unpcb *unp = sotounpcb(so); 5050d9ce3a1SRobert Watson struct xucred xu; 5060c1bb4fbSDima Dorfman int error; 5070c1bb4fbSDima Dorfman 5080c1bb4fbSDima Dorfman switch (sopt->sopt_dir) { 5090c1bb4fbSDima Dorfman case SOPT_GET: 5100c1bb4fbSDima Dorfman switch (sopt->sopt_name) { 5110c1bb4fbSDima Dorfman case LOCAL_PEERCRED: 5120d9ce3a1SRobert Watson error = 0; 5130d9ce3a1SRobert Watson UNP_LOCK(); 5140c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC) 5150d9ce3a1SRobert Watson xu = unp->unp_peercred; 5160c1bb4fbSDima Dorfman else { 5170c1bb4fbSDima Dorfman if (so->so_type == SOCK_STREAM) 5180c1bb4fbSDima Dorfman error = ENOTCONN; 5190c1bb4fbSDima Dorfman else 5200c1bb4fbSDima Dorfman error = EINVAL; 5210c1bb4fbSDima Dorfman } 5220d9ce3a1SRobert Watson UNP_UNLOCK(); 5230d9ce3a1SRobert Watson if (error == 0) 5240d9ce3a1SRobert Watson error = sooptcopyout(sopt, &xu, sizeof(xu)); 5250c1bb4fbSDima Dorfman break; 5260c1bb4fbSDima Dorfman default: 5270c1bb4fbSDima Dorfman error = EOPNOTSUPP; 5280c1bb4fbSDima Dorfman break; 5290c1bb4fbSDima Dorfman } 5300c1bb4fbSDima Dorfman break; 5310c1bb4fbSDima Dorfman case SOPT_SET: 5320c1bb4fbSDima Dorfman default: 5330c1bb4fbSDima Dorfman error = EOPNOTSUPP; 5340c1bb4fbSDima Dorfman break; 5350c1bb4fbSDima Dorfman } 5360c1bb4fbSDima Dorfman return (error); 5370c1bb4fbSDima Dorfman } 5380c1bb4fbSDima Dorfman 539df8bae1dSRodney W. Grimes /* 540df8bae1dSRodney W. Grimes * Both send and receive buffers are allocated PIPSIZ bytes of buffering 541df8bae1dSRodney W. Grimes * for stream sockets, although the total for sender and receiver is 542df8bae1dSRodney W. Grimes * actually only PIPSIZ. 543df8bae1dSRodney W. Grimes * Datagram sockets really use the sendspace as the maximum datagram size, 544df8bae1dSRodney W. Grimes * and don't really want to reserve the sendspace. Their recvspace should 545df8bae1dSRodney W. Grimes * be large enough for at least one max-size datagram plus address. 546df8bae1dSRodney W. Grimes */ 5475dce41c5SJohn Dyson #ifndef PIPSIZ 5485dce41c5SJohn Dyson #define PIPSIZ 8192 5495dce41c5SJohn Dyson #endif 550f708ef1bSPoul-Henning Kamp static u_long unpst_sendspace = PIPSIZ; 551f708ef1bSPoul-Henning Kamp static u_long unpst_recvspace = PIPSIZ; 552f708ef1bSPoul-Henning Kamp static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 553f708ef1bSPoul-Henning Kamp static u_long unpdg_recvspace = 4*1024; 554df8bae1dSRodney W. Grimes 555f708ef1bSPoul-Henning Kamp static int unp_rights; /* file descriptors in flight */ 556df8bae1dSRodney W. Grimes 557ce02431fSDoug Rabson SYSCTL_DECL(_net_local_stream); 558639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 559639acc13SGarrett Wollman &unpst_sendspace, 0, ""); 560639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 561639acc13SGarrett Wollman &unpst_recvspace, 0, ""); 562ce02431fSDoug Rabson SYSCTL_DECL(_net_local_dgram); 563639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 564639acc13SGarrett Wollman &unpdg_sendspace, 0, ""); 565639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 566639acc13SGarrett Wollman &unpdg_recvspace, 0, ""); 567ce02431fSDoug Rabson SYSCTL_DECL(_net_local); 568639acc13SGarrett Wollman SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); 569639acc13SGarrett Wollman 570f708ef1bSPoul-Henning Kamp static int 571df8bae1dSRodney W. Grimes unp_attach(so) 572df8bae1dSRodney W. Grimes struct socket *so; 573df8bae1dSRodney W. Grimes { 574df8bae1dSRodney W. Grimes register struct unpcb *unp; 575df8bae1dSRodney W. Grimes int error; 576df8bae1dSRodney W. Grimes 577df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 578df8bae1dSRodney W. Grimes switch (so->so_type) { 579df8bae1dSRodney W. Grimes 580df8bae1dSRodney W. Grimes case SOCK_STREAM: 581df8bae1dSRodney W. Grimes error = soreserve(so, unpst_sendspace, unpst_recvspace); 582df8bae1dSRodney W. Grimes break; 583df8bae1dSRodney W. Grimes 584df8bae1dSRodney W. Grimes case SOCK_DGRAM: 585df8bae1dSRodney W. Grimes error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 586df8bae1dSRodney W. Grimes break; 587df8bae1dSRodney W. Grimes 588df8bae1dSRodney W. Grimes default: 589df8bae1dSRodney W. Grimes panic("unp_attach"); 590df8bae1dSRodney W. Grimes } 591df8bae1dSRodney W. Grimes if (error) 592df8bae1dSRodney W. Grimes return (error); 593df8bae1dSRodney W. Grimes } 594a163d034SWarner Losh unp = uma_zalloc(unp_zone, M_WAITOK); 59557bf258eSGarrett Wollman if (unp == NULL) 596df8bae1dSRodney W. Grimes return (ENOBUFS); 59757bf258eSGarrett Wollman bzero(unp, sizeof *unp); 59898271db4SGarrett Wollman LIST_INIT(&unp->unp_refs); 599df8bae1dSRodney W. Grimes unp->unp_socket = so; 6000d9ce3a1SRobert Watson 6010d9ce3a1SRobert Watson UNP_LOCK(); 6020d9ce3a1SRobert Watson unp->unp_gencnt = ++unp_gencnt; 6030d9ce3a1SRobert Watson unp_count++; 60498271db4SGarrett Wollman LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead 60598271db4SGarrett Wollman : &unp_shead, unp, unp_link); 6060d9ce3a1SRobert Watson UNP_UNLOCK(); 6070d9ce3a1SRobert Watson 608210a5a71SAlfred Perlstein so->so_pcb = unp; 609df8bae1dSRodney W. Grimes return (0); 610df8bae1dSRodney W. Grimes } 611df8bae1dSRodney W. Grimes 612f708ef1bSPoul-Henning Kamp static void 613df8bae1dSRodney W. Grimes unp_detach(unp) 614df8bae1dSRodney W. Grimes register struct unpcb *unp; 615df8bae1dSRodney W. Grimes { 6160d9ce3a1SRobert Watson struct vnode *vp; 6170d9ce3a1SRobert Watson 6180d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 6190d9ce3a1SRobert Watson 62098271db4SGarrett Wollman LIST_REMOVE(unp, unp_link); 62198271db4SGarrett Wollman unp->unp_gencnt = ++unp_gencnt; 62298271db4SGarrett Wollman --unp_count; 6230d9ce3a1SRobert Watson if ((vp = unp->unp_vnode) != NULL) { 6240d9ce3a1SRobert Watson /* 6250d9ce3a1SRobert Watson * XXXRW: should v_socket be frobbed only while holding 6260d9ce3a1SRobert Watson * Giant? 6270d9ce3a1SRobert Watson */ 628fc3fcacfSRobert Watson unp->unp_vnode->v_socket = NULL; 629fc3fcacfSRobert Watson unp->unp_vnode = NULL; 630df8bae1dSRodney W. Grimes } 631fc3fcacfSRobert Watson if (unp->unp_conn != NULL) 632df8bae1dSRodney W. Grimes unp_disconnect(unp); 6330d9ce3a1SRobert Watson while (!LIST_EMPTY(&unp->unp_refs)) { 6340d9ce3a1SRobert Watson struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 6350d9ce3a1SRobert Watson unp_drop(ref, ECONNRESET); 6360d9ce3a1SRobert Watson } 637df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 638fc3fcacfSRobert Watson unp->unp_socket->so_pcb = NULL; 639df8bae1dSRodney W. Grimes if (unp_rights) { 640df8bae1dSRodney W. Grimes /* 641df8bae1dSRodney W. Grimes * Normally the receive buffer is flushed later, 642df8bae1dSRodney W. Grimes * in sofree, but if our receive buffer holds references 643df8bae1dSRodney W. Grimes * to descriptors that are now garbage, we will dispose 644df8bae1dSRodney W. Grimes * of those descriptor references after the garbage collector 645df8bae1dSRodney W. Grimes * gets them (resulting in a "panic: closef: count < 0"). 646df8bae1dSRodney W. Grimes */ 647df8bae1dSRodney W. Grimes sorflush(unp->unp_socket); 648df8bae1dSRodney W. Grimes unp_gc(); 649df8bae1dSRodney W. Grimes } 650fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 65157bf258eSGarrett Wollman FREE(unp->unp_addr, M_SONAME); 6520d9ce3a1SRobert Watson UNP_UNLOCK(); 6539e9d298aSJeff Roberson uma_zfree(unp_zone, unp); 6540d9ce3a1SRobert Watson if (vp) { 6550d9ce3a1SRobert Watson mtx_lock(&Giant); 6560d9ce3a1SRobert Watson vrele(vp); 6570d9ce3a1SRobert Watson mtx_unlock(&Giant); 6580d9ce3a1SRobert Watson } 659df8bae1dSRodney W. Grimes } 660df8bae1dSRodney W. Grimes 661f708ef1bSPoul-Henning Kamp static int 662b40ce416SJulian Elischer unp_bind(unp, nam, td) 663df8bae1dSRodney W. Grimes struct unpcb *unp; 66457bf258eSGarrett Wollman struct sockaddr *nam; 665b40ce416SJulian Elischer struct thread *td; 666df8bae1dSRodney W. Grimes { 66757bf258eSGarrett Wollman struct sockaddr_un *soun = (struct sockaddr_un *)nam; 668f2a2857bSKirk McKusick struct vnode *vp; 669f2a2857bSKirk McKusick struct mount *mp; 670df8bae1dSRodney W. Grimes struct vattr vattr; 67157bf258eSGarrett Wollman int error, namelen; 672df8bae1dSRodney W. Grimes struct nameidata nd; 6738f364875SJulian Elischer char *buf; 674df8bae1dSRodney W. Grimes 6750d9ce3a1SRobert Watson /* 6760d9ce3a1SRobert Watson * XXXRW: This test-and-set of unp_vnode is non-atomic; the 6770d9ce3a1SRobert Watson * unlocked read here is fine, but the value of unp_vnode needs 6780d9ce3a1SRobert Watson * to be tested again after we do all the lookups to see if the 6790d9ce3a1SRobert Watson * pcb is still unbound? 6800d9ce3a1SRobert Watson */ 681df8bae1dSRodney W. Grimes if (unp->unp_vnode != NULL) 682df8bae1dSRodney W. Grimes return (EINVAL); 68355c85568SRobert Drehmel 68457bf258eSGarrett Wollman namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 68557bf258eSGarrett Wollman if (namelen <= 0) 686e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 68755c85568SRobert Drehmel 688a163d034SWarner Losh buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 68955c85568SRobert Drehmel strlcpy(buf, soun->sun_path, namelen + 1); 69055c85568SRobert Drehmel 6910d9ce3a1SRobert Watson mtx_lock(&Giant); 692f2a2857bSKirk McKusick restart: 6930d9ce3a1SRobert Watson mtx_assert(&Giant, MA_OWNED); 694b65f6f6bSRobert Watson NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME, UIO_SYSSPACE, 695b40ce416SJulian Elischer buf, td); 696df8bae1dSRodney W. Grimes /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 697797f2d22SPoul-Henning Kamp error = namei(&nd); 6980d9ce3a1SRobert Watson if (error) 6990d9ce3a1SRobert Watson goto done; 700df8bae1dSRodney W. Grimes vp = nd.ni_vp; 701f2a2857bSKirk McKusick if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 702762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 703df8bae1dSRodney W. Grimes if (nd.ni_dvp == vp) 704df8bae1dSRodney W. Grimes vrele(nd.ni_dvp); 705df8bae1dSRodney W. Grimes else 706df8bae1dSRodney W. Grimes vput(nd.ni_dvp); 707f2a2857bSKirk McKusick if (vp != NULL) { 708df8bae1dSRodney W. Grimes vrele(vp); 7090d9ce3a1SRobert Watson error = EADDRINUSE; 7100d9ce3a1SRobert Watson goto done; 711df8bae1dSRodney W. Grimes } 7128f364875SJulian Elischer error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 7130d9ce3a1SRobert Watson if (error) 7140d9ce3a1SRobert Watson goto done; 715f2a2857bSKirk McKusick goto restart; 716f2a2857bSKirk McKusick } 717df8bae1dSRodney W. Grimes VATTR_NULL(&vattr); 718df8bae1dSRodney W. Grimes vattr.va_type = VSOCK; 719b40ce416SJulian Elischer vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 7206ea48a90SRobert Watson #ifdef MAC 7216ea48a90SRobert Watson error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 7226ea48a90SRobert Watson &vattr); 7236151efaaSRobert Watson #endif 7246ea48a90SRobert Watson if (error == 0) { 725a854ed98SJohn Baldwin VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE); 7267be2d300SMike Smith error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 7276ea48a90SRobert Watson } 728762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 7297be2d300SMike Smith vput(nd.ni_dvp); 7300d9ce3a1SRobert Watson if (error) 7310d9ce3a1SRobert Watson goto done; 732df8bae1dSRodney W. Grimes vp = nd.ni_vp; 7330d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_bind"); 7340d9ce3a1SRobert Watson soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 7350d9ce3a1SRobert Watson UNP_LOCK(); 736df8bae1dSRodney W. Grimes vp->v_socket = unp->unp_socket; 737df8bae1dSRodney W. Grimes unp->unp_vnode = vp; 7380d9ce3a1SRobert Watson unp->unp_addr = soun; 7390d9ce3a1SRobert Watson UNP_UNLOCK(); 740b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 741f2a2857bSKirk McKusick vn_finished_write(mp); 7420d9ce3a1SRobert Watson done: 7430d9ce3a1SRobert Watson mtx_unlock(&Giant); 7448f364875SJulian Elischer free(buf, M_TEMP); 7450d9ce3a1SRobert Watson return (error); 746df8bae1dSRodney W. Grimes } 747df8bae1dSRodney W. Grimes 748f708ef1bSPoul-Henning Kamp static int 749b40ce416SJulian Elischer unp_connect(so, nam, td) 750df8bae1dSRodney W. Grimes struct socket *so; 75157bf258eSGarrett Wollman struct sockaddr *nam; 752b40ce416SJulian Elischer struct thread *td; 753df8bae1dSRodney W. Grimes { 75457bf258eSGarrett Wollman register struct sockaddr_un *soun = (struct sockaddr_un *)nam; 755df8bae1dSRodney W. Grimes register struct vnode *vp; 756df8bae1dSRodney W. Grimes register struct socket *so2, *so3; 7570d9ce3a1SRobert Watson struct unpcb *unp = sotounpcb(so); 7580d9ce3a1SRobert Watson struct unpcb *unp2, *unp3; 75957bf258eSGarrett Wollman int error, len; 760df8bae1dSRodney W. Grimes struct nameidata nd; 76157bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 7620d9ce3a1SRobert Watson struct sockaddr *sa; 7630d9ce3a1SRobert Watson 7640d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 765df8bae1dSRodney W. Grimes 76657bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 76757bf258eSGarrett Wollman if (len <= 0) 768e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 76955c85568SRobert Drehmel strlcpy(buf, soun->sun_path, len + 1); 7700d9ce3a1SRobert Watson UNP_UNLOCK(); 7710d9ce3a1SRobert Watson sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 7720d9ce3a1SRobert Watson mtx_lock(&Giant); 773b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td); 774797f2d22SPoul-Henning Kamp error = namei(&nd); 775797f2d22SPoul-Henning Kamp if (error) 7760d9ce3a1SRobert Watson vp = NULL; 7770d9ce3a1SRobert Watson else 778df8bae1dSRodney W. Grimes vp = nd.ni_vp; 7790d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_connect"); 780762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 7810d9ce3a1SRobert Watson if (error) 7820d9ce3a1SRobert Watson goto bad; 7830d9ce3a1SRobert Watson 784df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 785df8bae1dSRodney W. Grimes error = ENOTSOCK; 786df8bae1dSRodney W. Grimes goto bad; 787df8bae1dSRodney W. Grimes } 788a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 789797f2d22SPoul-Henning Kamp if (error) 790df8bae1dSRodney W. Grimes goto bad; 791df8bae1dSRodney W. Grimes so2 = vp->v_socket; 792fc3fcacfSRobert Watson if (so2 == NULL) { 793df8bae1dSRodney W. Grimes error = ECONNREFUSED; 794df8bae1dSRodney W. Grimes goto bad; 795df8bae1dSRodney W. Grimes } 796df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 797df8bae1dSRodney W. Grimes error = EPROTOTYPE; 798df8bae1dSRodney W. Grimes goto bad; 799df8bae1dSRodney W. Grimes } 8000d9ce3a1SRobert Watson mtx_unlock(&Giant); 8010d9ce3a1SRobert Watson UNP_LOCK(); 802df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 8030d9ce3a1SRobert Watson if (so2->so_options & SO_ACCEPTCONN) { 8040d9ce3a1SRobert Watson /* 8050d9ce3a1SRobert Watson * NB: drop locks here so unp_attach is entered 8060d9ce3a1SRobert Watson * w/o locks; this avoids a recursive lock 8070d9ce3a1SRobert Watson * of the head and holding sleep locks across 8080d9ce3a1SRobert Watson * a (potentially) blocking malloc. 8090d9ce3a1SRobert Watson */ 8100d9ce3a1SRobert Watson UNP_UNLOCK(); 8110d9ce3a1SRobert Watson so3 = sonewconn(so2, 0); 8120d9ce3a1SRobert Watson UNP_LOCK(); 8130d9ce3a1SRobert Watson } else 8140d9ce3a1SRobert Watson so3 = NULL; 8150d9ce3a1SRobert Watson if (so3 == NULL) { 816df8bae1dSRodney W. Grimes error = ECONNREFUSED; 8170d9ce3a1SRobert Watson goto bad2; 818df8bae1dSRodney W. Grimes } 8190c1bb4fbSDima Dorfman unp = sotounpcb(so); 820df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 821df8bae1dSRodney W. Grimes unp3 = sotounpcb(so3); 8220d9ce3a1SRobert Watson if (unp2->unp_addr != NULL) { 8230d9ce3a1SRobert Watson bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 8240d9ce3a1SRobert Watson unp3->unp_addr = (struct sockaddr_un *) sa; 8250d9ce3a1SRobert Watson sa = NULL; 8260d9ce3a1SRobert Watson } 8270c1bb4fbSDima Dorfman /* 8280c1bb4fbSDima Dorfman * unp_peercred management: 8290c1bb4fbSDima Dorfman * 8300c1bb4fbSDima Dorfman * The connecter's (client's) credentials are copied 8310c1bb4fbSDima Dorfman * from its process structure at the time of connect() 8320c1bb4fbSDima Dorfman * (which is now). 8330c1bb4fbSDima Dorfman */ 834a854ed98SJohn Baldwin cru2x(td->td_ucred, &unp3->unp_peercred); 8350c1bb4fbSDima Dorfman unp3->unp_flags |= UNP_HAVEPC; 8360c1bb4fbSDima Dorfman /* 8370c1bb4fbSDima Dorfman * The receiver's (server's) credentials are copied 8380c1bb4fbSDima Dorfman * from the unp_peercred member of socket on which the 8390c1bb4fbSDima Dorfman * former called listen(); unp_listen() cached that 8400c1bb4fbSDima Dorfman * process's credentials at that time so we can use 8410c1bb4fbSDima Dorfman * them now. 8420c1bb4fbSDima Dorfman */ 8430c1bb4fbSDima Dorfman KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 8440c1bb4fbSDima Dorfman ("unp_connect: listener without cached peercred")); 8450c1bb4fbSDima Dorfman memcpy(&unp->unp_peercred, &unp2->unp_peercred, 8460c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 8470c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPC; 848335654d7SRobert Watson #ifdef MAC 849310e7cebSRobert Watson SOCK_LOCK(so); 850335654d7SRobert Watson mac_set_socket_peer_from_socket(so, so3); 851335654d7SRobert Watson mac_set_socket_peer_from_socket(so3, so); 852310e7cebSRobert Watson SOCK_UNLOCK(so); 853335654d7SRobert Watson #endif 8540c1bb4fbSDima Dorfman 855df8bae1dSRodney W. Grimes so2 = so3; 856df8bae1dSRodney W. Grimes } 857df8bae1dSRodney W. Grimes error = unp_connect2(so, so2); 8580d9ce3a1SRobert Watson bad2: 8590d9ce3a1SRobert Watson UNP_UNLOCK(); 8600d9ce3a1SRobert Watson mtx_lock(&Giant); 861df8bae1dSRodney W. Grimes bad: 8620d9ce3a1SRobert Watson mtx_assert(&Giant, MA_OWNED); 8630d9ce3a1SRobert Watson if (vp != NULL) 864df8bae1dSRodney W. Grimes vput(vp); 8650d9ce3a1SRobert Watson mtx_unlock(&Giant); 8660d9ce3a1SRobert Watson free(sa, M_SONAME); 8670d9ce3a1SRobert Watson UNP_LOCK(); 868df8bae1dSRodney W. Grimes return (error); 869df8bae1dSRodney W. Grimes } 870df8bae1dSRodney W. Grimes 871db48c0d2SRobert Watson static int 872df8bae1dSRodney W. Grimes unp_connect2(so, so2) 873df8bae1dSRodney W. Grimes register struct socket *so; 874df8bae1dSRodney W. Grimes register struct socket *so2; 875df8bae1dSRodney W. Grimes { 876df8bae1dSRodney W. Grimes register struct unpcb *unp = sotounpcb(so); 877df8bae1dSRodney W. Grimes register struct unpcb *unp2; 878df8bae1dSRodney W. Grimes 8790d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 8800d9ce3a1SRobert Watson 881df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 882df8bae1dSRodney W. Grimes return (EPROTOTYPE); 883df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 884df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 885df8bae1dSRodney W. Grimes switch (so->so_type) { 886df8bae1dSRodney W. Grimes 887df8bae1dSRodney W. Grimes case SOCK_DGRAM: 88898271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 889df8bae1dSRodney W. Grimes soisconnected(so); 890df8bae1dSRodney W. Grimes break; 891df8bae1dSRodney W. Grimes 892df8bae1dSRodney W. Grimes case SOCK_STREAM: 893df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 894df8bae1dSRodney W. Grimes soisconnected(so); 895df8bae1dSRodney W. Grimes soisconnected(so2); 896df8bae1dSRodney W. Grimes break; 897df8bae1dSRodney W. Grimes 898df8bae1dSRodney W. Grimes default: 899df8bae1dSRodney W. Grimes panic("unp_connect2"); 900df8bae1dSRodney W. Grimes } 901df8bae1dSRodney W. Grimes return (0); 902df8bae1dSRodney W. Grimes } 903df8bae1dSRodney W. Grimes 904f708ef1bSPoul-Henning Kamp static void 905df8bae1dSRodney W. Grimes unp_disconnect(unp) 906df8bae1dSRodney W. Grimes struct unpcb *unp; 907df8bae1dSRodney W. Grimes { 908df8bae1dSRodney W. Grimes register struct unpcb *unp2 = unp->unp_conn; 909df8bae1dSRodney W. Grimes 9100d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 9110d9ce3a1SRobert Watson 912fc3fcacfSRobert Watson if (unp2 == NULL) 913df8bae1dSRodney W. Grimes return; 914fc3fcacfSRobert Watson unp->unp_conn = NULL; 915df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 916df8bae1dSRodney W. Grimes 917df8bae1dSRodney W. Grimes case SOCK_DGRAM: 91898271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 919df8bae1dSRodney W. Grimes unp->unp_socket->so_state &= ~SS_ISCONNECTED; 920df8bae1dSRodney W. Grimes break; 921df8bae1dSRodney W. Grimes 922df8bae1dSRodney W. Grimes case SOCK_STREAM: 923df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 924fc3fcacfSRobert Watson unp2->unp_conn = NULL; 925df8bae1dSRodney W. Grimes soisdisconnected(unp2->unp_socket); 926df8bae1dSRodney W. Grimes break; 927df8bae1dSRodney W. Grimes } 928df8bae1dSRodney W. Grimes } 929df8bae1dSRodney W. Grimes 930df8bae1dSRodney W. Grimes #ifdef notdef 93126f9a767SRodney W. Grimes void 932df8bae1dSRodney W. Grimes unp_abort(unp) 933df8bae1dSRodney W. Grimes struct unpcb *unp; 934df8bae1dSRodney W. Grimes { 935df8bae1dSRodney W. Grimes 936df8bae1dSRodney W. Grimes unp_detach(unp); 937df8bae1dSRodney W. Grimes } 938df8bae1dSRodney W. Grimes #endif 939df8bae1dSRodney W. Grimes 9400d9ce3a1SRobert Watson /* 9410d9ce3a1SRobert Watson * unp_pcblist() assumes that UNIX domain socket memory is never reclaimed 9420d9ce3a1SRobert Watson * by the zone (UMA_ZONE_NOFREE), and as such potentially stale pointers 9430d9ce3a1SRobert Watson * are safe to reference. It first scans the list of struct unpcb's to 9440d9ce3a1SRobert Watson * generate a pointer list, then it rescans its list one entry at a time to 9450d9ce3a1SRobert Watson * externalize and copyout. It checks the generation number to see if a 9460d9ce3a1SRobert Watson * struct unpcb has been reused, and will skip it if so. 9470d9ce3a1SRobert Watson */ 94898271db4SGarrett Wollman static int 94982d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 95098271db4SGarrett Wollman { 951f5ef029eSPoul-Henning Kamp int error, i, n; 95298271db4SGarrett Wollman struct unpcb *unp, **unp_list; 95398271db4SGarrett Wollman unp_gen_t gencnt; 9548f364875SJulian Elischer struct xunpgen *xug; 95598271db4SGarrett Wollman struct unp_head *head; 9568f364875SJulian Elischer struct xunpcb *xu; 95798271db4SGarrett Wollman 958a23d65bfSBruce Evans head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 95998271db4SGarrett Wollman 96098271db4SGarrett Wollman /* 96198271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 96298271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 96398271db4SGarrett Wollman */ 964fc3fcacfSRobert Watson if (req->oldptr == NULL) { 96598271db4SGarrett Wollman n = unp_count; 9668f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug) 96798271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 968e5aeaa0cSDag-Erling Smørgrav return (0); 96998271db4SGarrett Wollman } 97098271db4SGarrett Wollman 971fc3fcacfSRobert Watson if (req->newptr != NULL) 972e5aeaa0cSDag-Erling Smørgrav return (EPERM); 97398271db4SGarrett Wollman 97498271db4SGarrett Wollman /* 97598271db4SGarrett Wollman * OK, now we're committed to doing something. 97698271db4SGarrett Wollman */ 977a163d034SWarner Losh xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 9780d9ce3a1SRobert Watson UNP_LOCK(); 97998271db4SGarrett Wollman gencnt = unp_gencnt; 98098271db4SGarrett Wollman n = unp_count; 9810d9ce3a1SRobert Watson UNP_UNLOCK(); 98298271db4SGarrett Wollman 9838f364875SJulian Elischer xug->xug_len = sizeof *xug; 9848f364875SJulian Elischer xug->xug_count = n; 9858f364875SJulian Elischer xug->xug_gen = gencnt; 9868f364875SJulian Elischer xug->xug_sogen = so_gencnt; 9878f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 9888f364875SJulian Elischer if (error) { 9898f364875SJulian Elischer free(xug, M_TEMP); 990e5aeaa0cSDag-Erling Smørgrav return (error); 9918f364875SJulian Elischer } 99298271db4SGarrett Wollman 993a163d034SWarner Losh unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 99498271db4SGarrett Wollman 9950d9ce3a1SRobert Watson UNP_LOCK(); 9962e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 9972e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 9988a7d8cc6SRobert Watson if (unp->unp_gencnt <= gencnt) { 999a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred, 10008a7d8cc6SRobert Watson unp->unp_socket->so_cred)) 10014787fd37SPaul Saab continue; 100298271db4SGarrett Wollman unp_list[i++] = unp; 100398271db4SGarrett Wollman } 10044787fd37SPaul Saab } 10050d9ce3a1SRobert Watson UNP_UNLOCK(); 100698271db4SGarrett Wollman n = i; /* in case we lost some during malloc */ 100798271db4SGarrett Wollman 100898271db4SGarrett Wollman error = 0; 1009a163d034SWarner Losh xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK); 101098271db4SGarrett Wollman for (i = 0; i < n; i++) { 101198271db4SGarrett Wollman unp = unp_list[i]; 101298271db4SGarrett Wollman if (unp->unp_gencnt <= gencnt) { 10138f364875SJulian Elischer xu->xu_len = sizeof *xu; 10148f364875SJulian Elischer xu->xu_unpp = unp; 101598271db4SGarrett Wollman /* 101698271db4SGarrett Wollman * XXX - need more locking here to protect against 101798271db4SGarrett Wollman * connect/disconnect races for SMP. 101898271db4SGarrett Wollman */ 1019fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 10208f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr, 102198271db4SGarrett Wollman unp->unp_addr->sun_len); 1022fc3fcacfSRobert Watson if (unp->unp_conn != NULL && 1023fc3fcacfSRobert Watson unp->unp_conn->unp_addr != NULL) 102498271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 10258f364875SJulian Elischer &xu->xu_caddr, 102698271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 10278f364875SJulian Elischer bcopy(unp, &xu->xu_unp, sizeof *unp); 10288f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket); 10298f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu); 103098271db4SGarrett Wollman } 103198271db4SGarrett Wollman } 10328f364875SJulian Elischer free(xu, M_TEMP); 103398271db4SGarrett Wollman if (!error) { 103498271db4SGarrett Wollman /* 103598271db4SGarrett Wollman * Give the user an updated idea of our state. 103698271db4SGarrett Wollman * If the generation differs from what we told 103798271db4SGarrett Wollman * her before, she knows that something happened 103898271db4SGarrett Wollman * while we were processing this request, and it 103998271db4SGarrett Wollman * might be necessary to retry. 104098271db4SGarrett Wollman */ 10418f364875SJulian Elischer xug->xug_gen = unp_gencnt; 10428f364875SJulian Elischer xug->xug_sogen = so_gencnt; 10438f364875SJulian Elischer xug->xug_count = unp_count; 10448f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 104598271db4SGarrett Wollman } 104698271db4SGarrett Wollman free(unp_list, M_TEMP); 10478f364875SJulian Elischer free(xug, M_TEMP); 1048e5aeaa0cSDag-Erling Smørgrav return (error); 104998271db4SGarrett Wollman } 105098271db4SGarrett Wollman 105198271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 105298271db4SGarrett Wollman (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 105398271db4SGarrett Wollman "List of active local datagram sockets"); 105498271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 105598271db4SGarrett Wollman (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 105698271db4SGarrett Wollman "List of active local stream sockets"); 105798271db4SGarrett Wollman 1058f708ef1bSPoul-Henning Kamp static void 1059df8bae1dSRodney W. Grimes unp_shutdown(unp) 1060df8bae1dSRodney W. Grimes struct unpcb *unp; 1061df8bae1dSRodney W. Grimes { 1062df8bae1dSRodney W. Grimes struct socket *so; 1063df8bae1dSRodney W. Grimes 10640d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 10650d9ce3a1SRobert Watson 1066df8bae1dSRodney W. Grimes if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 1067df8bae1dSRodney W. Grimes (so = unp->unp_conn->unp_socket)) 1068df8bae1dSRodney W. Grimes socantrcvmore(so); 1069df8bae1dSRodney W. Grimes } 1070df8bae1dSRodney W. Grimes 1071f708ef1bSPoul-Henning Kamp static void 1072df8bae1dSRodney W. Grimes unp_drop(unp, errno) 1073df8bae1dSRodney W. Grimes struct unpcb *unp; 1074df8bae1dSRodney W. Grimes int errno; 1075df8bae1dSRodney W. Grimes { 1076df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 1077df8bae1dSRodney W. Grimes 10780d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 10790d9ce3a1SRobert Watson 1080df8bae1dSRodney W. Grimes so->so_error = errno; 1081df8bae1dSRodney W. Grimes unp_disconnect(unp); 1082df8bae1dSRodney W. Grimes } 1083df8bae1dSRodney W. Grimes 1084df8bae1dSRodney W. Grimes #ifdef notdef 108526f9a767SRodney W. Grimes void 1086df8bae1dSRodney W. Grimes unp_drain() 1087df8bae1dSRodney W. Grimes { 1088df8bae1dSRodney W. Grimes 1089df8bae1dSRodney W. Grimes } 1090df8bae1dSRodney W. Grimes #endif 1091df8bae1dSRodney W. Grimes 10922bc21ed9SDavid Malone static void 10932bc21ed9SDavid Malone unp_freerights(rp, fdcount) 10942bc21ed9SDavid Malone struct file **rp; 10952bc21ed9SDavid Malone int fdcount; 1096df8bae1dSRodney W. Grimes { 10972bc21ed9SDavid Malone int i; 10982bc21ed9SDavid Malone struct file *fp; 1099df8bae1dSRodney W. Grimes 11002bc21ed9SDavid Malone for (i = 0; i < fdcount; i++) { 1101df8bae1dSRodney W. Grimes fp = *rp; 11028692c025SYoshinobu Inoue /* 11032bc21ed9SDavid Malone * zero the pointer before calling 11042bc21ed9SDavid Malone * unp_discard since it may end up 11052bc21ed9SDavid Malone * in unp_gc().. 11068692c025SYoshinobu Inoue */ 1107df8bae1dSRodney W. Grimes *rp++ = 0; 11088692c025SYoshinobu Inoue unp_discard(fp); 1109df8bae1dSRodney W. Grimes } 11102bc21ed9SDavid Malone } 11112bc21ed9SDavid Malone 11122bc21ed9SDavid Malone int 11132bc21ed9SDavid Malone unp_externalize(control, controlp) 11142bc21ed9SDavid Malone struct mbuf *control, **controlp; 11152bc21ed9SDavid Malone { 11162bc21ed9SDavid Malone struct thread *td = curthread; /* XXX */ 11172bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 11182bc21ed9SDavid Malone int i; 11192bc21ed9SDavid Malone int *fdp; 11202bc21ed9SDavid Malone struct file **rp; 11212bc21ed9SDavid Malone struct file *fp; 11222bc21ed9SDavid Malone void *data; 11232bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 11242bc21ed9SDavid Malone int error, newfds; 11252bc21ed9SDavid Malone int f; 11262bc21ed9SDavid Malone u_int newlen; 11272bc21ed9SDavid Malone 11282bc21ed9SDavid Malone error = 0; 11292bc21ed9SDavid Malone if (controlp != NULL) /* controlp == NULL => free control messages */ 11302bc21ed9SDavid Malone *controlp = NULL; 11312bc21ed9SDavid Malone 11322bc21ed9SDavid Malone while (cm != NULL) { 11332bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 11342bc21ed9SDavid Malone error = EINVAL; 11352bc21ed9SDavid Malone break; 11362bc21ed9SDavid Malone } 11372bc21ed9SDavid Malone 11382bc21ed9SDavid Malone data = CMSG_DATA(cm); 11392bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 11402bc21ed9SDavid Malone 11412bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET 11422bc21ed9SDavid Malone && cm->cmsg_type == SCM_RIGHTS) { 11432bc21ed9SDavid Malone newfds = datalen / sizeof(struct file *); 11442bc21ed9SDavid Malone rp = data; 11452bc21ed9SDavid Malone 1146e2f9a08bSOlivier Houchard /* If we're not outputting the descriptors free them. */ 11472bc21ed9SDavid Malone if (error || controlp == NULL) { 11482bc21ed9SDavid Malone unp_freerights(rp, newfds); 11492bc21ed9SDavid Malone goto next; 11502bc21ed9SDavid Malone } 1151426da3bcSAlfred Perlstein FILEDESC_LOCK(td->td_proc->p_fd); 11522bc21ed9SDavid Malone /* if the new FD's will not fit free them. */ 11532bc21ed9SDavid Malone if (!fdavail(td, newfds)) { 1154426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 11552bc21ed9SDavid Malone error = EMSGSIZE; 11562bc21ed9SDavid Malone unp_freerights(rp, newfds); 11572bc21ed9SDavid Malone goto next; 1158df8bae1dSRodney W. Grimes } 1159ed5b7817SJulian Elischer /* 11602bc21ed9SDavid Malone * now change each pointer to an fd in the global 11612bc21ed9SDavid Malone * table to an integer that is the index to the 11622bc21ed9SDavid Malone * local fd table entry that we set up to point 11632bc21ed9SDavid Malone * to the global one we are transferring. 1164ed5b7817SJulian Elischer */ 11652bc21ed9SDavid Malone newlen = newfds * sizeof(int); 11662bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 11672bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 11682bc21ed9SDavid Malone if (*controlp == NULL) { 1169426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 11702bc21ed9SDavid Malone error = E2BIG; 11712bc21ed9SDavid Malone unp_freerights(rp, newfds); 11722bc21ed9SDavid Malone goto next; 11732bc21ed9SDavid Malone } 11742bc21ed9SDavid Malone 11752bc21ed9SDavid Malone fdp = (int *) 11762bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1177df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 1178a6d4491cSDag-Erling Smørgrav if (fdalloc(td, 0, &f)) 11792bc21ed9SDavid Malone panic("unp_externalize fdalloc failed"); 11808692c025SYoshinobu Inoue fp = *rp++; 1181b40ce416SJulian Elischer td->td_proc->p_fd->fd_ofiles[f] = fp; 1182426da3bcSAlfred Perlstein FILE_LOCK(fp); 1183df8bae1dSRodney W. Grimes fp->f_msgcount--; 1184426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1185df8bae1dSRodney W. Grimes unp_rights--; 11868692c025SYoshinobu Inoue *fdp++ = f; 1187df8bae1dSRodney W. Grimes } 1188426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 11892bc21ed9SDavid Malone } else { /* We can just copy anything else across */ 11902bc21ed9SDavid Malone if (error || controlp == NULL) 11912bc21ed9SDavid Malone goto next; 11922bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, datalen, 11932bc21ed9SDavid Malone cm->cmsg_type, cm->cmsg_level); 11942bc21ed9SDavid Malone if (*controlp == NULL) { 11952bc21ed9SDavid Malone error = ENOBUFS; 11962bc21ed9SDavid Malone goto next; 11972bc21ed9SDavid Malone } 11982bc21ed9SDavid Malone bcopy(data, 11992bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 12002bc21ed9SDavid Malone datalen); 12012bc21ed9SDavid Malone } 12022bc21ed9SDavid Malone 12032bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 12042bc21ed9SDavid Malone 12052bc21ed9SDavid Malone next: 12062bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 12072bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 12082bc21ed9SDavid Malone cm = (struct cmsghdr *) 12092bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 12108692c025SYoshinobu Inoue } else { 12112bc21ed9SDavid Malone clen = 0; 12122bc21ed9SDavid Malone cm = NULL; 12138692c025SYoshinobu Inoue } 12148692c025SYoshinobu Inoue } 12158692c025SYoshinobu Inoue 12162bc21ed9SDavid Malone m_freem(control); 12172bc21ed9SDavid Malone 12182bc21ed9SDavid Malone return (error); 1219df8bae1dSRodney W. Grimes } 1220df8bae1dSRodney W. Grimes 122198271db4SGarrett Wollman void 122298271db4SGarrett Wollman unp_init(void) 122398271db4SGarrett Wollman { 12249e9d298aSJeff Roberson unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 12259e9d298aSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 1226fc3fcacfSRobert Watson if (unp_zone == NULL) 122798271db4SGarrett Wollman panic("unp_init"); 1228b17dd2bcSColin Percival uma_zone_set_max(unp_zone, nmbclusters); 122998271db4SGarrett Wollman LIST_INIT(&unp_dhead); 123098271db4SGarrett Wollman LIST_INIT(&unp_shead); 12310d9ce3a1SRobert Watson 12320d9ce3a1SRobert Watson UNP_LOCK_INIT(); 123398271db4SGarrett Wollman } 123498271db4SGarrett Wollman 1235f708ef1bSPoul-Henning Kamp static int 12362bc21ed9SDavid Malone unp_internalize(controlp, td) 12372bc21ed9SDavid Malone struct mbuf **controlp; 1238b40ce416SJulian Elischer struct thread *td; 1239df8bae1dSRodney W. Grimes { 12402bc21ed9SDavid Malone struct mbuf *control = *controlp; 1241b40ce416SJulian Elischer struct proc *p = td->td_proc; 12428692c025SYoshinobu Inoue struct filedesc *fdescp = p->p_fd; 12432bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 12442bc21ed9SDavid Malone struct cmsgcred *cmcred; 12452bc21ed9SDavid Malone struct file **rp; 12462bc21ed9SDavid Malone struct file *fp; 12472bc21ed9SDavid Malone struct timeval *tv; 12482bc21ed9SDavid Malone int i, fd, *fdp; 12492bc21ed9SDavid Malone void *data; 12502bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 12512bc21ed9SDavid Malone int error, oldfds; 12528692c025SYoshinobu Inoue u_int newlen; 1253df8bae1dSRodney W. Grimes 12542bc21ed9SDavid Malone error = 0; 12552bc21ed9SDavid Malone *controlp = NULL; 12560b788fa1SBill Paul 12572bc21ed9SDavid Malone while (cm != NULL) { 12582bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 12592bc21ed9SDavid Malone || cm->cmsg_len > clen) { 12602bc21ed9SDavid Malone error = EINVAL; 12612bc21ed9SDavid Malone goto out; 12622bc21ed9SDavid Malone } 12632bc21ed9SDavid Malone 12642bc21ed9SDavid Malone data = CMSG_DATA(cm); 12652bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 12662bc21ed9SDavid Malone 12672bc21ed9SDavid Malone switch (cm->cmsg_type) { 12680b788fa1SBill Paul /* 12690b788fa1SBill Paul * Fill in credential information. 12700b788fa1SBill Paul */ 12712bc21ed9SDavid Malone case SCM_CREDS: 12722bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 12732bc21ed9SDavid Malone SCM_CREDS, SOL_SOCKET); 12742bc21ed9SDavid Malone if (*controlp == NULL) { 12752bc21ed9SDavid Malone error = ENOBUFS; 12762bc21ed9SDavid Malone goto out; 12772bc21ed9SDavid Malone } 12782bc21ed9SDavid Malone 12792bc21ed9SDavid Malone cmcred = (struct cmsgcred *) 12802bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 12810b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 1282a854ed98SJohn Baldwin cmcred->cmcred_uid = td->td_ucred->cr_ruid; 1283a854ed98SJohn Baldwin cmcred->cmcred_gid = td->td_ucred->cr_rgid; 1284a854ed98SJohn Baldwin cmcred->cmcred_euid = td->td_ucred->cr_uid; 1285a854ed98SJohn Baldwin cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 12860b788fa1SBill Paul CMGROUP_MAX); 12870b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 12882bc21ed9SDavid Malone cmcred->cmcred_groups[i] = 1289a854ed98SJohn Baldwin td->td_ucred->cr_groups[i]; 12902bc21ed9SDavid Malone break; 12910b788fa1SBill Paul 12922bc21ed9SDavid Malone case SCM_RIGHTS: 12932bc21ed9SDavid Malone oldfds = datalen / sizeof (int); 1294ed5b7817SJulian Elischer /* 12952bc21ed9SDavid Malone * check that all the FDs passed in refer to legal files 1296ed5b7817SJulian Elischer * If not, reject the entire operation. 1297ed5b7817SJulian Elischer */ 12982bc21ed9SDavid Malone fdp = data; 1299426da3bcSAlfred Perlstein FILEDESC_LOCK(fdescp); 1300df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 13018692c025SYoshinobu Inoue fd = *fdp++; 13028692c025SYoshinobu Inoue if ((unsigned)fd >= fdescp->fd_nfiles || 13032bc21ed9SDavid Malone fdescp->fd_ofiles[fd] == NULL) { 1304426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 13052bc21ed9SDavid Malone error = EBADF; 13062bc21ed9SDavid Malone goto out; 13072bc21ed9SDavid Malone } 1308e7d6662fSAlfred Perlstein fp = fdescp->fd_ofiles[fd]; 1309e7d6662fSAlfred Perlstein if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 1310e7d6662fSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 1311e7d6662fSAlfred Perlstein error = EOPNOTSUPP; 1312e7d6662fSAlfred Perlstein goto out; 1313e7d6662fSAlfred Perlstein } 1314e7d6662fSAlfred Perlstein 1315df8bae1dSRodney W. Grimes } 1316ed5b7817SJulian Elischer /* 1317ed5b7817SJulian Elischer * Now replace the integer FDs with pointers to 1318ed5b7817SJulian Elischer * the associated global file table entry.. 1319ed5b7817SJulian Elischer */ 13202bc21ed9SDavid Malone newlen = oldfds * sizeof(struct file *); 13212bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 13222bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 13232bc21ed9SDavid Malone if (*controlp == NULL) { 1324426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 13252bc21ed9SDavid Malone error = E2BIG; 13262bc21ed9SDavid Malone goto out; 13278692c025SYoshinobu Inoue } 13288692c025SYoshinobu Inoue 13292bc21ed9SDavid Malone fdp = data; 13302bc21ed9SDavid Malone rp = (struct file **) 13312bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 13328692c025SYoshinobu Inoue for (i = 0; i < oldfds; i++) { 13338692c025SYoshinobu Inoue fp = fdescp->fd_ofiles[*fdp++]; 1334df8bae1dSRodney W. Grimes *rp++ = fp; 1335426da3bcSAlfred Perlstein FILE_LOCK(fp); 1336df8bae1dSRodney W. Grimes fp->f_count++; 1337df8bae1dSRodney W. Grimes fp->f_msgcount++; 1338426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1339df8bae1dSRodney W. Grimes unp_rights++; 1340df8bae1dSRodney W. Grimes } 1341426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 13422bc21ed9SDavid Malone break; 13432bc21ed9SDavid Malone 13442bc21ed9SDavid Malone case SCM_TIMESTAMP: 13452bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*tv), 13462bc21ed9SDavid Malone SCM_TIMESTAMP, SOL_SOCKET); 13472bc21ed9SDavid Malone if (*controlp == NULL) { 13482bc21ed9SDavid Malone error = ENOBUFS; 13492bc21ed9SDavid Malone goto out; 13508692c025SYoshinobu Inoue } 13512bc21ed9SDavid Malone tv = (struct timeval *) 13522bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 13532bc21ed9SDavid Malone microtime(tv); 13542bc21ed9SDavid Malone break; 13552bc21ed9SDavid Malone 13562bc21ed9SDavid Malone default: 13572bc21ed9SDavid Malone error = EINVAL; 13582bc21ed9SDavid Malone goto out; 13592bc21ed9SDavid Malone } 13602bc21ed9SDavid Malone 13612bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 13622bc21ed9SDavid Malone 13632bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 13642bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 13652bc21ed9SDavid Malone cm = (struct cmsghdr *) 13662bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 13672bc21ed9SDavid Malone } else { 13682bc21ed9SDavid Malone clen = 0; 13692bc21ed9SDavid Malone cm = NULL; 13702bc21ed9SDavid Malone } 13712bc21ed9SDavid Malone } 13722bc21ed9SDavid Malone 13732bc21ed9SDavid Malone out: 13742bc21ed9SDavid Malone m_freem(control); 13752bc21ed9SDavid Malone 13762bc21ed9SDavid Malone return (error); 1377df8bae1dSRodney W. Grimes } 1378df8bae1dSRodney W. Grimes 1379f708ef1bSPoul-Henning Kamp static int unp_defer, unp_gcing; 1380df8bae1dSRodney W. Grimes 1381f708ef1bSPoul-Henning Kamp static void 1382df8bae1dSRodney W. Grimes unp_gc() 1383df8bae1dSRodney W. Grimes { 1384df8bae1dSRodney W. Grimes register struct file *fp, *nextfp; 1385df8bae1dSRodney W. Grimes register struct socket *so; 1386df8bae1dSRodney W. Grimes struct file **extra_ref, **fpp; 1387df8bae1dSRodney W. Grimes int nunref, i; 1388df8bae1dSRodney W. Grimes 13890d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 13900d9ce3a1SRobert Watson 1391df8bae1dSRodney W. Grimes if (unp_gcing) 1392df8bae1dSRodney W. Grimes return; 1393df8bae1dSRodney W. Grimes unp_gcing = 1; 1394df8bae1dSRodney W. Grimes unp_defer = 0; 1395ed5b7817SJulian Elischer /* 1396ed5b7817SJulian Elischer * before going through all this, set all FDs to 1397ed5b7817SJulian Elischer * be NOT defered and NOT externally accessible 1398ed5b7817SJulian Elischer */ 13990d9ce3a1SRobert Watson /* 14000d9ce3a1SRobert Watson * XXXRW: Acquiring a sleep lock while holding UNP 14010d9ce3a1SRobert Watson * mutex cannot be a good thing. 14020d9ce3a1SRobert Watson */ 1403426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 14042e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) 1405426da3bcSAlfred Perlstein fp->f_gcflag &= ~(FMARK|FDEFER); 1406df8bae1dSRodney W. Grimes do { 14072e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) { 1408426da3bcSAlfred Perlstein FILE_LOCK(fp); 1409ed5b7817SJulian Elischer /* 1410ed5b7817SJulian Elischer * If the file is not open, skip it 1411ed5b7817SJulian Elischer */ 1412426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1413426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1414df8bae1dSRodney W. Grimes continue; 1415426da3bcSAlfred Perlstein } 1416ed5b7817SJulian Elischer /* 1417ed5b7817SJulian Elischer * If we already marked it as 'defer' in a 1418ed5b7817SJulian Elischer * previous pass, then try process it this time 1419ed5b7817SJulian Elischer * and un-mark it 1420ed5b7817SJulian Elischer */ 1421426da3bcSAlfred Perlstein if (fp->f_gcflag & FDEFER) { 1422426da3bcSAlfred Perlstein fp->f_gcflag &= ~FDEFER; 1423df8bae1dSRodney W. Grimes unp_defer--; 1424df8bae1dSRodney W. Grimes } else { 1425ed5b7817SJulian Elischer /* 1426ed5b7817SJulian Elischer * if it's not defered, then check if it's 1427ed5b7817SJulian Elischer * already marked.. if so skip it 1428ed5b7817SJulian Elischer */ 1429426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) { 1430426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1431df8bae1dSRodney W. Grimes continue; 1432426da3bcSAlfred Perlstein } 1433ed5b7817SJulian Elischer /* 1434ed5b7817SJulian Elischer * If all references are from messages 1435ed5b7817SJulian Elischer * in transit, then skip it. it's not 1436ed5b7817SJulian Elischer * externally accessible. 1437ed5b7817SJulian Elischer */ 1438426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount) { 1439426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1440df8bae1dSRodney W. Grimes continue; 1441426da3bcSAlfred Perlstein } 1442ed5b7817SJulian Elischer /* 1443ed5b7817SJulian Elischer * If it got this far then it must be 1444ed5b7817SJulian Elischer * externally accessible. 1445ed5b7817SJulian Elischer */ 1446426da3bcSAlfred Perlstein fp->f_gcflag |= FMARK; 1447df8bae1dSRodney W. Grimes } 1448ed5b7817SJulian Elischer /* 1449ed5b7817SJulian Elischer * either it was defered, or it is externally 1450ed5b7817SJulian Elischer * accessible and not already marked so. 1451ed5b7817SJulian Elischer * Now check if it is possibly one of OUR sockets. 1452ed5b7817SJulian Elischer */ 1453df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_SOCKET || 145448e3128bSMatthew Dillon (so = fp->f_data) == NULL) { 1455426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1456df8bae1dSRodney W. Grimes continue; 1457426da3bcSAlfred Perlstein } 1458426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1459748e0b0aSGarrett Wollman if (so->so_proto->pr_domain != &localdomain || 1460df8bae1dSRodney W. Grimes (so->so_proto->pr_flags&PR_RIGHTS) == 0) 1461df8bae1dSRodney W. Grimes continue; 1462df8bae1dSRodney W. Grimes #ifdef notdef 1463df8bae1dSRodney W. Grimes if (so->so_rcv.sb_flags & SB_LOCK) { 1464df8bae1dSRodney W. Grimes /* 1465df8bae1dSRodney W. Grimes * This is problematical; it's not clear 1466df8bae1dSRodney W. Grimes * we need to wait for the sockbuf to be 1467df8bae1dSRodney W. Grimes * unlocked (on a uniprocessor, at least), 1468df8bae1dSRodney W. Grimes * and it's also not clear what to do 1469df8bae1dSRodney W. Grimes * if sbwait returns an error due to receipt 1470df8bae1dSRodney W. Grimes * of a signal. If sbwait does return 1471df8bae1dSRodney W. Grimes * an error, we'll go into an infinite 1472df8bae1dSRodney W. Grimes * loop. Delete all of this for now. 1473df8bae1dSRodney W. Grimes */ 1474df8bae1dSRodney W. Grimes (void) sbwait(&so->so_rcv); 1475df8bae1dSRodney W. Grimes goto restart; 1476df8bae1dSRodney W. Grimes } 1477df8bae1dSRodney W. Grimes #endif 1478ed5b7817SJulian Elischer /* 1479ed5b7817SJulian Elischer * So, Ok, it's one of our sockets and it IS externally 1480ed5b7817SJulian Elischer * accessible (or was defered). Now we look 1481dc733423SDag-Erling Smørgrav * to see if we hold any file descriptors in its 1482ed5b7817SJulian Elischer * message buffers. Follow those links and mark them 1483ed5b7817SJulian Elischer * as accessible too. 1484ed5b7817SJulian Elischer */ 1485df8bae1dSRodney W. Grimes unp_scan(so->so_rcv.sb_mb, unp_mark); 1486df8bae1dSRodney W. Grimes } 1487df8bae1dSRodney W. Grimes } while (unp_defer); 1488426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1489df8bae1dSRodney W. Grimes /* 1490df8bae1dSRodney W. Grimes * We grab an extra reference to each of the file table entries 1491df8bae1dSRodney W. Grimes * that are not otherwise accessible and then free the rights 1492df8bae1dSRodney W. Grimes * that are stored in messages on them. 1493df8bae1dSRodney W. Grimes * 1494df8bae1dSRodney W. Grimes * The bug in the orginal code is a little tricky, so I'll describe 1495df8bae1dSRodney W. Grimes * what's wrong with it here. 1496df8bae1dSRodney W. Grimes * 1497df8bae1dSRodney W. Grimes * It is incorrect to simply unp_discard each entry for f_msgcount 1498df8bae1dSRodney W. Grimes * times -- consider the case of sockets A and B that contain 1499df8bae1dSRodney W. Grimes * references to each other. On a last close of some other socket, 1500df8bae1dSRodney W. Grimes * we trigger a gc since the number of outstanding rights (unp_rights) 1501df8bae1dSRodney W. Grimes * is non-zero. If during the sweep phase the gc code un_discards, 1502df8bae1dSRodney W. Grimes * we end up doing a (full) closef on the descriptor. A closef on A 1503df8bae1dSRodney W. Grimes * results in the following chain. Closef calls soo_close, which 1504df8bae1dSRodney W. Grimes * calls soclose. Soclose calls first (through the switch 1505df8bae1dSRodney W. Grimes * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 1506df8bae1dSRodney W. Grimes * returns because the previous instance had set unp_gcing, and 1507df8bae1dSRodney W. Grimes * we return all the way back to soclose, which marks the socket 1508df8bae1dSRodney W. Grimes * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush 1509df8bae1dSRodney W. Grimes * to free up the rights that are queued in messages on the socket A, 1510df8bae1dSRodney W. Grimes * i.e., the reference on B. The sorflush calls via the dom_dispose 1511df8bae1dSRodney W. Grimes * switch unp_dispose, which unp_scans with unp_discard. This second 1512df8bae1dSRodney W. Grimes * instance of unp_discard just calls closef on B. 1513df8bae1dSRodney W. Grimes * 1514df8bae1dSRodney W. Grimes * Well, a similar chain occurs on B, resulting in a sorflush on B, 1515df8bae1dSRodney W. Grimes * which results in another closef on A. Unfortunately, A is already 1516df8bae1dSRodney W. Grimes * being closed, and the descriptor has already been marked with 1517df8bae1dSRodney W. Grimes * SS_NOFDREF, and soclose panics at this point. 1518df8bae1dSRodney W. Grimes * 1519df8bae1dSRodney W. Grimes * Here, we first take an extra reference to each inaccessible 1520df8bae1dSRodney W. Grimes * descriptor. Then, we call sorflush ourself, since we know 1521df8bae1dSRodney W. Grimes * it is a Unix domain socket anyhow. After we destroy all the 1522df8bae1dSRodney W. Grimes * rights carried in messages, we do a last closef to get rid 1523df8bae1dSRodney W. Grimes * of our extra reference. This is the last close, and the 1524df8bae1dSRodney W. Grimes * unp_detach etc will shut down the socket. 1525df8bae1dSRodney W. Grimes * 1526df8bae1dSRodney W. Grimes * 91/09/19, bsy@cs.cmu.edu 1527df8bae1dSRodney W. Grimes */ 1528a163d034SWarner Losh extra_ref = malloc(nfiles * sizeof(struct file *), M_TEMP, M_WAITOK); 1529426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 1530fc3fcacfSRobert Watson for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; 1531fc3fcacfSRobert Watson fp != NULL; fp = nextfp) { 15322e3c8fcbSPoul-Henning Kamp nextfp = LIST_NEXT(fp, f_list); 1533426da3bcSAlfred Perlstein FILE_LOCK(fp); 1534ed5b7817SJulian Elischer /* 1535ed5b7817SJulian Elischer * If it's not open, skip it 1536ed5b7817SJulian Elischer */ 1537426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1538426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1539df8bae1dSRodney W. Grimes continue; 1540426da3bcSAlfred Perlstein } 1541ed5b7817SJulian Elischer /* 1542ed5b7817SJulian Elischer * If all refs are from msgs, and it's not marked accessible 1543ed5b7817SJulian Elischer * then it must be referenced from some unreachable cycle 1544ed5b7817SJulian Elischer * of (shut-down) FDs, so include it in our 1545ed5b7817SJulian Elischer * list of FDs to remove 1546ed5b7817SJulian Elischer */ 1547426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) { 1548df8bae1dSRodney W. Grimes *fpp++ = fp; 1549df8bae1dSRodney W. Grimes nunref++; 1550df8bae1dSRodney W. Grimes fp->f_count++; 1551df8bae1dSRodney W. Grimes } 1552426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1553df8bae1dSRodney W. Grimes } 1554426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1555ed5b7817SJulian Elischer /* 1556ed5b7817SJulian Elischer * for each FD on our hit list, do the following two things 1557ed5b7817SJulian Elischer */ 15581c7c3c6aSMatthew Dillon for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 15591c7c3c6aSMatthew Dillon struct file *tfp = *fpp; 1560426da3bcSAlfred Perlstein FILE_LOCK(tfp); 1561cd72f218SMatthew Dillon if (tfp->f_type == DTYPE_SOCKET && 156248e3128bSMatthew Dillon tfp->f_data != NULL) { 1563426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 156448e3128bSMatthew Dillon sorflush(tfp->f_data); 1565e5aeaa0cSDag-Erling Smørgrav } else { 1566426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 15671c7c3c6aSMatthew Dillon } 1568e5aeaa0cSDag-Erling Smørgrav } 1569df8bae1dSRodney W. Grimes for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) 1570b40ce416SJulian Elischer closef(*fpp, (struct thread *) NULL); 1571210a5a71SAlfred Perlstein free(extra_ref, M_TEMP); 1572df8bae1dSRodney W. Grimes unp_gcing = 0; 1573df8bae1dSRodney W. Grimes } 1574df8bae1dSRodney W. Grimes 157526f9a767SRodney W. Grimes void 1576df8bae1dSRodney W. Grimes unp_dispose(m) 1577df8bae1dSRodney W. Grimes struct mbuf *m; 1578df8bae1dSRodney W. Grimes { 1579996c772fSJohn Dyson 1580df8bae1dSRodney W. Grimes if (m) 1581df8bae1dSRodney W. Grimes unp_scan(m, unp_discard); 1582df8bae1dSRodney W. Grimes } 1583df8bae1dSRodney W. Grimes 15840c1bb4fbSDima Dorfman static int 15856f105b34SJohn Baldwin unp_listen(unp, td) 15860c1bb4fbSDima Dorfman struct unpcb *unp; 15876f105b34SJohn Baldwin struct thread *td; 15880c1bb4fbSDima Dorfman { 15890d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 15900c1bb4fbSDima Dorfman 15910d9ce3a1SRobert Watson /* 15920d9ce3a1SRobert Watson * XXXRW: Why populate the local peer cred with our own credential? 15930d9ce3a1SRobert Watson */ 15946f105b34SJohn Baldwin cru2x(td->td_ucred, &unp->unp_peercred); 15950c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPCCACHED; 15960c1bb4fbSDima Dorfman return (0); 15970c1bb4fbSDima Dorfman } 15980c1bb4fbSDima Dorfman 1599f708ef1bSPoul-Henning Kamp static void 1600df8bae1dSRodney W. Grimes unp_scan(m0, op) 1601df8bae1dSRodney W. Grimes register struct mbuf *m0; 16024d77a549SAlfred Perlstein void (*op)(struct file *); 1603df8bae1dSRodney W. Grimes { 16042bc21ed9SDavid Malone struct mbuf *m; 16052bc21ed9SDavid Malone struct file **rp; 16062bc21ed9SDavid Malone struct cmsghdr *cm; 16072bc21ed9SDavid Malone void *data; 16082bc21ed9SDavid Malone int i; 16092bc21ed9SDavid Malone socklen_t clen, datalen; 1610df8bae1dSRodney W. Grimes int qfds; 1611df8bae1dSRodney W. Grimes 1612fc3fcacfSRobert Watson while (m0 != NULL) { 16132bc21ed9SDavid Malone for (m = m0; m; m = m->m_next) { 161412396bdcSDavid Malone if (m->m_type != MT_CONTROL) 1615df8bae1dSRodney W. Grimes continue; 16162bc21ed9SDavid Malone 16172bc21ed9SDavid Malone cm = mtod(m, struct cmsghdr *); 16182bc21ed9SDavid Malone clen = m->m_len; 16192bc21ed9SDavid Malone 16202bc21ed9SDavid Malone while (cm != NULL) { 16212bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) 16222bc21ed9SDavid Malone break; 16232bc21ed9SDavid Malone 16242bc21ed9SDavid Malone data = CMSG_DATA(cm); 16252bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len 16262bc21ed9SDavid Malone - (caddr_t)data; 16272bc21ed9SDavid Malone 16282bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET && 16292bc21ed9SDavid Malone cm->cmsg_type == SCM_RIGHTS) { 16302bc21ed9SDavid Malone qfds = datalen / sizeof (struct file *); 16312bc21ed9SDavid Malone rp = data; 1632df8bae1dSRodney W. Grimes for (i = 0; i < qfds; i++) 1633df8bae1dSRodney W. Grimes (*op)(*rp++); 16342bc21ed9SDavid Malone } 16352bc21ed9SDavid Malone 16362bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 16372bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 16382bc21ed9SDavid Malone cm = (struct cmsghdr *) 16392bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 16402bc21ed9SDavid Malone } else { 16412bc21ed9SDavid Malone clen = 0; 16422bc21ed9SDavid Malone cm = NULL; 16432bc21ed9SDavid Malone } 16442bc21ed9SDavid Malone } 1645df8bae1dSRodney W. Grimes } 1646df8bae1dSRodney W. Grimes m0 = m0->m_act; 1647df8bae1dSRodney W. Grimes } 1648df8bae1dSRodney W. Grimes } 1649df8bae1dSRodney W. Grimes 1650f708ef1bSPoul-Henning Kamp static void 1651df8bae1dSRodney W. Grimes unp_mark(fp) 1652df8bae1dSRodney W. Grimes struct file *fp; 1653df8bae1dSRodney W. Grimes { 1654426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) 1655df8bae1dSRodney W. Grimes return; 1656df8bae1dSRodney W. Grimes unp_defer++; 1657426da3bcSAlfred Perlstein fp->f_gcflag |= (FMARK|FDEFER); 1658df8bae1dSRodney W. Grimes } 1659df8bae1dSRodney W. Grimes 1660f708ef1bSPoul-Henning Kamp static void 1661df8bae1dSRodney W. Grimes unp_discard(fp) 1662df8bae1dSRodney W. Grimes struct file *fp; 1663df8bae1dSRodney W. Grimes { 1664426da3bcSAlfred Perlstein FILE_LOCK(fp); 1665df8bae1dSRodney W. Grimes fp->f_msgcount--; 1666df8bae1dSRodney W. Grimes unp_rights--; 1667426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1668b40ce416SJulian Elischer (void) closef(fp, (struct thread *)NULL); 1669df8bae1dSRodney W. Grimes } 1670