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 174fd179ee9SRobert Watson KASSERT(td == curthread, ("uipc_connect: td != curthread")); 175fd179ee9SRobert Watson 176fc3fcacfSRobert Watson if (unp == NULL) 177e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 1780d9ce3a1SRobert Watson UNP_LOCK(); 179fd179ee9SRobert Watson error = unp_connect(so, nam, td); 1800d9ce3a1SRobert Watson UNP_UNLOCK(); 1810d9ce3a1SRobert Watson return (error); 182a29f300eSGarrett Wollman } 183a29f300eSGarrett Wollman 184db48c0d2SRobert Watson int 185a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2) 186a29f300eSGarrett Wollman { 187a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so1); 1880d9ce3a1SRobert Watson int error; 189a29f300eSGarrett Wollman 190fc3fcacfSRobert Watson if (unp == NULL) 191e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 192a29f300eSGarrett Wollman 1930d9ce3a1SRobert Watson UNP_LOCK(); 1940d9ce3a1SRobert Watson error = unp_connect2(so1, so2); 1950d9ce3a1SRobert Watson UNP_UNLOCK(); 1960d9ce3a1SRobert Watson return (error); 197a29f300eSGarrett Wollman } 198a29f300eSGarrett Wollman 199a29f300eSGarrett Wollman /* control is EOPNOTSUPP */ 200a29f300eSGarrett Wollman 201a29f300eSGarrett Wollman static int 202a29f300eSGarrett Wollman uipc_detach(struct socket *so) 203a29f300eSGarrett Wollman { 204a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 205a29f300eSGarrett Wollman 206fc3fcacfSRobert Watson if (unp == NULL) 207e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 208a29f300eSGarrett Wollman 2090d9ce3a1SRobert Watson UNP_LOCK(); 2100d9ce3a1SRobert Watson unp_detach(unp); /* NB: unlocks unp */ 211e5aeaa0cSDag-Erling Smørgrav return (0); 212a29f300eSGarrett Wollman } 213a29f300eSGarrett Wollman 214a29f300eSGarrett Wollman static int 215a29f300eSGarrett Wollman uipc_disconnect(struct socket *so) 216a29f300eSGarrett Wollman { 217a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 218a29f300eSGarrett Wollman 219fc3fcacfSRobert Watson if (unp == NULL) 220e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 2210d9ce3a1SRobert Watson UNP_LOCK(); 222a29f300eSGarrett Wollman unp_disconnect(unp); 2230d9ce3a1SRobert Watson UNP_UNLOCK(); 224e5aeaa0cSDag-Erling Smørgrav return (0); 225a29f300eSGarrett Wollman } 226a29f300eSGarrett Wollman 227a29f300eSGarrett Wollman static int 228b40ce416SJulian Elischer uipc_listen(struct socket *so, struct thread *td) 229a29f300eSGarrett Wollman { 230a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 2310d9ce3a1SRobert Watson int error; 232a29f300eSGarrett Wollman 233fc3fcacfSRobert Watson if (unp == NULL || unp->unp_vnode == NULL) 234e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 2350d9ce3a1SRobert Watson UNP_LOCK(); 2360d9ce3a1SRobert Watson error = unp_listen(unp, td); 2370d9ce3a1SRobert Watson UNP_UNLOCK(); 2380d9ce3a1SRobert Watson return (error); 239a29f300eSGarrett Wollman } 240a29f300eSGarrett Wollman 241a29f300eSGarrett Wollman static int 24257bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 243a29f300eSGarrett Wollman { 244a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 2450d9ce3a1SRobert Watson const struct sockaddr *sa; 246a29f300eSGarrett Wollman 247fc3fcacfSRobert Watson if (unp == NULL) 248e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 2490d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 2500d9ce3a1SRobert Watson UNP_LOCK(); 251fc3fcacfSRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr!= NULL) 2520d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 253bdc5f6a3SHajimu UMEMOTO else { 254bdc5f6a3SHajimu UMEMOTO /* 255bdc5f6a3SHajimu UMEMOTO * XXX: It seems that this test always fails even when 256bdc5f6a3SHajimu UMEMOTO * connection is established. So, this else clause is 257bdc5f6a3SHajimu UMEMOTO * added as workaround to return PF_LOCAL sockaddr. 258bdc5f6a3SHajimu UMEMOTO */ 2590d9ce3a1SRobert Watson sa = &sun_noname; 260bdc5f6a3SHajimu UMEMOTO } 2610d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 2620d9ce3a1SRobert Watson UNP_UNLOCK(); 263e5aeaa0cSDag-Erling Smørgrav return (0); 264a29f300eSGarrett Wollman } 265a29f300eSGarrett Wollman 266a29f300eSGarrett Wollman static int 267a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 268a29f300eSGarrett Wollman { 269a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 270a29f300eSGarrett Wollman struct socket *so2; 2716aef685fSBrian Feldman u_long newhiwat; 272a29f300eSGarrett Wollman 273fc3fcacfSRobert Watson if (unp == NULL) 274e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 2750d9ce3a1SRobert Watson UNP_LOCK(); 276df8bae1dSRodney W. Grimes switch (so->so_type) { 277df8bae1dSRodney W. Grimes case SOCK_DGRAM: 278a29f300eSGarrett Wollman panic("uipc_rcvd DGRAM?"); 279df8bae1dSRodney W. Grimes /*NOTREACHED*/ 280df8bae1dSRodney W. Grimes 281df8bae1dSRodney W. Grimes case SOCK_STREAM: 282fc3fcacfSRobert Watson if (unp->unp_conn == NULL) 283df8bae1dSRodney W. Grimes break; 284df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 285c9f69064SRobert Watson SOCKBUF_LOCK(&so2->so_snd); 286c9f69064SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 287df8bae1dSRodney W. Grimes /* 288df8bae1dSRodney W. Grimes * Adjust backpressure on sender 289df8bae1dSRodney W. Grimes * and wakeup any waiting to write. 290df8bae1dSRodney W. Grimes */ 291ff8b0106SBrian Feldman so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt; 292ff8b0106SBrian Feldman unp->unp_mbcnt = so->so_rcv.sb_mbcnt; 2936aef685fSBrian Feldman newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - 2946aef685fSBrian Feldman so->so_rcv.sb_cc; 295f535380cSDon Lewis (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat, 2966aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 297ff8b0106SBrian Feldman unp->unp_cc = so->so_rcv.sb_cc; 298c9f69064SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2991e4d7da7SRobert Watson sowwakeup_locked(so2); 300df8bae1dSRodney W. Grimes break; 301df8bae1dSRodney W. Grimes 302df8bae1dSRodney W. Grimes default: 303a29f300eSGarrett Wollman panic("uipc_rcvd unknown socktype"); 304df8bae1dSRodney W. Grimes } 3050d9ce3a1SRobert Watson UNP_UNLOCK(); 306e5aeaa0cSDag-Erling Smørgrav return (0); 307a29f300eSGarrett Wollman } 308df8bae1dSRodney W. Grimes 309a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 310a29f300eSGarrett Wollman 311a29f300eSGarrett Wollman static int 31257bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 313b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 314a29f300eSGarrett Wollman { 315a29f300eSGarrett Wollman int error = 0; 316a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 317a29f300eSGarrett Wollman struct socket *so2; 3186aef685fSBrian Feldman u_long newhiwat; 319a29f300eSGarrett Wollman 320fc3fcacfSRobert Watson if (unp == NULL) { 321a29f300eSGarrett Wollman error = EINVAL; 322a29f300eSGarrett Wollman goto release; 323a29f300eSGarrett Wollman } 324a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 325a29f300eSGarrett Wollman error = EOPNOTSUPP; 326a29f300eSGarrett Wollman goto release; 327a29f300eSGarrett Wollman } 328a29f300eSGarrett Wollman 329fc3fcacfSRobert Watson if (control != NULL && (error = unp_internalize(&control, td))) 330a29f300eSGarrett Wollman goto release; 331df8bae1dSRodney W. Grimes 3320d9ce3a1SRobert Watson UNP_LOCK(); 333a29f300eSGarrett Wollman switch (so->so_type) { 334a29f300eSGarrett Wollman case SOCK_DGRAM: 335a29f300eSGarrett Wollman { 336e7dd9a10SRobert Watson const struct sockaddr *from; 337df8bae1dSRodney W. Grimes 338fc3fcacfSRobert Watson if (nam != NULL) { 339fc3fcacfSRobert Watson if (unp->unp_conn != NULL) { 340df8bae1dSRodney W. Grimes error = EISCONN; 341df8bae1dSRodney W. Grimes break; 342df8bae1dSRodney W. Grimes } 343b40ce416SJulian Elischer error = unp_connect(so, nam, td); 344df8bae1dSRodney W. Grimes if (error) 345df8bae1dSRodney W. Grimes break; 346df8bae1dSRodney W. Grimes } else { 347fc3fcacfSRobert Watson if (unp->unp_conn == NULL) { 348df8bae1dSRodney W. Grimes error = ENOTCONN; 349df8bae1dSRodney W. Grimes break; 350df8bae1dSRodney W. Grimes } 351df8bae1dSRodney W. Grimes } 352df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 353fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 35457bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 355df8bae1dSRodney W. Grimes else 356df8bae1dSRodney W. Grimes from = &sun_noname; 357a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 358a34b7046SRobert Watson if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) { 3591e4d7da7SRobert Watson sorwakeup_locked(so2); 360fc3fcacfSRobert Watson m = NULL; 361fc3fcacfSRobert Watson control = NULL; 362e5aeaa0cSDag-Erling Smørgrav } else { 363a34b7046SRobert Watson SOCKBUF_UNLOCK(&so2->so_rcv); 364df8bae1dSRodney W. Grimes error = ENOBUFS; 365e5aeaa0cSDag-Erling Smørgrav } 366fc3fcacfSRobert Watson if (nam != NULL) 367df8bae1dSRodney W. Grimes unp_disconnect(unp); 368df8bae1dSRodney W. Grimes break; 369df8bae1dSRodney W. Grimes } 370df8bae1dSRodney W. Grimes 371df8bae1dSRodney W. Grimes case SOCK_STREAM: 3726b8fda4dSGarrett Wollman /* Connect if not connected yet. */ 3736b8fda4dSGarrett Wollman /* 3746b8fda4dSGarrett Wollman * Note: A better implementation would complain 375402cc72dSDavid Greenman * if not equal to the peer's address. 3766b8fda4dSGarrett Wollman */ 377402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 378fc3fcacfSRobert Watson if (nam != NULL) { 379b40ce416SJulian Elischer error = unp_connect(so, nam, td); 380402cc72dSDavid Greenman if (error) 3816b8fda4dSGarrett Wollman break; /* XXX */ 382402cc72dSDavid Greenman } else { 383402cc72dSDavid Greenman error = ENOTCONN; 384402cc72dSDavid Greenman break; 385402cc72dSDavid Greenman } 386402cc72dSDavid Greenman } 387402cc72dSDavid Greenman 388c0b99ffaSRobert Watson if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 389df8bae1dSRodney W. Grimes error = EPIPE; 390df8bae1dSRodney W. Grimes break; 391df8bae1dSRodney W. Grimes } 392fc3fcacfSRobert Watson if (unp->unp_conn == NULL) 393a29f300eSGarrett Wollman panic("uipc_send connected but no connection?"); 394df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 395a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 396df8bae1dSRodney W. Grimes /* 397df8bae1dSRodney W. Grimes * Send to paired receive port, and then reduce 398df8bae1dSRodney W. Grimes * send buffer hiwater marks to maintain backpressure. 399df8bae1dSRodney W. Grimes * Wake up readers. 400df8bae1dSRodney W. Grimes */ 401fc3fcacfSRobert Watson if (control != NULL) { 402a34b7046SRobert Watson if (sbappendcontrol_locked(&so2->so_rcv, m, control)) 403fc3fcacfSRobert Watson control = NULL; 404e5aeaa0cSDag-Erling Smørgrav } else { 405a34b7046SRobert Watson sbappend_locked(&so2->so_rcv, m); 406e5aeaa0cSDag-Erling Smørgrav } 407ff8b0106SBrian Feldman so->so_snd.sb_mbmax -= 408ff8b0106SBrian Feldman so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt; 409ff8b0106SBrian Feldman unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt; 4106aef685fSBrian Feldman newhiwat = so->so_snd.sb_hiwat - 4116aef685fSBrian Feldman (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc); 412f535380cSDon Lewis (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat, 4136aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 414ff8b0106SBrian Feldman unp->unp_conn->unp_cc = so2->so_rcv.sb_cc; 4151e4d7da7SRobert Watson sorwakeup_locked(so2); 416fc3fcacfSRobert Watson m = NULL; 417df8bae1dSRodney W. Grimes break; 418df8bae1dSRodney W. Grimes 419df8bae1dSRodney W. Grimes default: 420a29f300eSGarrett Wollman panic("uipc_send unknown socktype"); 421df8bae1dSRodney W. Grimes } 422a29f300eSGarrett Wollman 4236b8fda4dSGarrett Wollman /* 4246b8fda4dSGarrett Wollman * SEND_EOF is equivalent to a SEND followed by 4256b8fda4dSGarrett Wollman * a SHUTDOWN. 4266b8fda4dSGarrett Wollman */ 427a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 4286b8fda4dSGarrett Wollman socantsendmore(so); 4296b8fda4dSGarrett Wollman unp_shutdown(unp); 4306b8fda4dSGarrett Wollman } 4310d9ce3a1SRobert Watson UNP_UNLOCK(); 432df8bae1dSRodney W. Grimes 433fc3fcacfSRobert Watson if (control != NULL && error != 0) 434bd508d39SDon Lewis unp_dispose(control); 435bd508d39SDon Lewis 436a29f300eSGarrett Wollman release: 437fc3fcacfSRobert Watson if (control != NULL) 438a29f300eSGarrett Wollman m_freem(control); 439fc3fcacfSRobert Watson if (m != NULL) 440a29f300eSGarrett Wollman m_freem(m); 441e5aeaa0cSDag-Erling Smørgrav return (error); 442a29f300eSGarrett Wollman } 443df8bae1dSRodney W. Grimes 444a29f300eSGarrett Wollman static int 445a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 446a29f300eSGarrett Wollman { 447a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 448a29f300eSGarrett Wollman struct socket *so2; 449a29f300eSGarrett Wollman 450fc3fcacfSRobert Watson if (unp == NULL) 451e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 4520d9ce3a1SRobert Watson UNP_LOCK(); 453a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 454fc3fcacfSRobert Watson if (so->so_type == SOCK_STREAM && unp->unp_conn != NULL) { 455df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 456a29f300eSGarrett Wollman sb->st_blksize += so2->so_rcv.sb_cc; 457df8bae1dSRodney W. Grimes } 458f3732fd1SPoul-Henning Kamp sb->st_dev = NODEV; 459df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 4606f782c46SJeffrey Hsu unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 461a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 4620d9ce3a1SRobert Watson UNP_UNLOCK(); 463df8bae1dSRodney W. Grimes return (0); 464a29f300eSGarrett Wollman } 465df8bae1dSRodney W. Grimes 466a29f300eSGarrett Wollman static int 467a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 468a29f300eSGarrett Wollman { 469a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 470df8bae1dSRodney W. Grimes 471fc3fcacfSRobert Watson if (unp == NULL) 472e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 4730d9ce3a1SRobert Watson UNP_LOCK(); 474a29f300eSGarrett Wollman socantsendmore(so); 475a29f300eSGarrett Wollman unp_shutdown(unp); 4760d9ce3a1SRobert Watson UNP_UNLOCK(); 477e5aeaa0cSDag-Erling Smørgrav return (0); 478a29f300eSGarrett Wollman } 479df8bae1dSRodney W. Grimes 480a29f300eSGarrett Wollman static int 48157bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 482a29f300eSGarrett Wollman { 483a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 4840d9ce3a1SRobert Watson const struct sockaddr *sa; 485a29f300eSGarrett Wollman 486fc3fcacfSRobert Watson if (unp == NULL) 487e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 4880d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 4890d9ce3a1SRobert Watson UNP_LOCK(); 490fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 4910d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_addr; 49283f3198bSThomas Moestl else 4930d9ce3a1SRobert Watson sa = &sun_noname; 4940d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 4950d9ce3a1SRobert Watson UNP_UNLOCK(); 496e5aeaa0cSDag-Erling Smørgrav return (0); 497df8bae1dSRodney W. Grimes } 498a29f300eSGarrett Wollman 499a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = { 500a29f300eSGarrett Wollman uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect, 501a29f300eSGarrett Wollman uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect, 502a29f300eSGarrett Wollman uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp, 503a29f300eSGarrett Wollman uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr, 504a557af22SRobert Watson sosend, soreceive, sopoll, pru_sosetlabel_null 505a29f300eSGarrett Wollman }; 506df8bae1dSRodney W. Grimes 5070c1bb4fbSDima Dorfman int 5080c1bb4fbSDima Dorfman uipc_ctloutput(so, sopt) 5090c1bb4fbSDima Dorfman struct socket *so; 5100c1bb4fbSDima Dorfman struct sockopt *sopt; 5110c1bb4fbSDima Dorfman { 5120c1bb4fbSDima Dorfman struct unpcb *unp = sotounpcb(so); 5130d9ce3a1SRobert Watson struct xucred xu; 5140c1bb4fbSDima Dorfman int error; 5150c1bb4fbSDima Dorfman 5160c1bb4fbSDima Dorfman switch (sopt->sopt_dir) { 5170c1bb4fbSDima Dorfman case SOPT_GET: 5180c1bb4fbSDima Dorfman switch (sopt->sopt_name) { 5190c1bb4fbSDima Dorfman case LOCAL_PEERCRED: 5200d9ce3a1SRobert Watson error = 0; 5210d9ce3a1SRobert Watson UNP_LOCK(); 5220c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC) 5230d9ce3a1SRobert Watson xu = unp->unp_peercred; 5240c1bb4fbSDima Dorfman else { 5250c1bb4fbSDima Dorfman if (so->so_type == SOCK_STREAM) 5260c1bb4fbSDima Dorfman error = ENOTCONN; 5270c1bb4fbSDima Dorfman else 5280c1bb4fbSDima Dorfman error = EINVAL; 5290c1bb4fbSDima Dorfman } 5300d9ce3a1SRobert Watson UNP_UNLOCK(); 5310d9ce3a1SRobert Watson if (error == 0) 5320d9ce3a1SRobert Watson error = sooptcopyout(sopt, &xu, sizeof(xu)); 5330c1bb4fbSDima Dorfman break; 5340c1bb4fbSDima Dorfman default: 5350c1bb4fbSDima Dorfman error = EOPNOTSUPP; 5360c1bb4fbSDima Dorfman break; 5370c1bb4fbSDima Dorfman } 5380c1bb4fbSDima Dorfman break; 5390c1bb4fbSDima Dorfman case SOPT_SET: 5400c1bb4fbSDima Dorfman default: 5410c1bb4fbSDima Dorfman error = EOPNOTSUPP; 5420c1bb4fbSDima Dorfman break; 5430c1bb4fbSDima Dorfman } 5440c1bb4fbSDima Dorfman return (error); 5450c1bb4fbSDima Dorfman } 5460c1bb4fbSDima Dorfman 547df8bae1dSRodney W. Grimes /* 548df8bae1dSRodney W. Grimes * Both send and receive buffers are allocated PIPSIZ bytes of buffering 549df8bae1dSRodney W. Grimes * for stream sockets, although the total for sender and receiver is 550df8bae1dSRodney W. Grimes * actually only PIPSIZ. 551df8bae1dSRodney W. Grimes * Datagram sockets really use the sendspace as the maximum datagram size, 552df8bae1dSRodney W. Grimes * and don't really want to reserve the sendspace. Their recvspace should 553df8bae1dSRodney W. Grimes * be large enough for at least one max-size datagram plus address. 554df8bae1dSRodney W. Grimes */ 5555dce41c5SJohn Dyson #ifndef PIPSIZ 5565dce41c5SJohn Dyson #define PIPSIZ 8192 5575dce41c5SJohn Dyson #endif 558f708ef1bSPoul-Henning Kamp static u_long unpst_sendspace = PIPSIZ; 559f708ef1bSPoul-Henning Kamp static u_long unpst_recvspace = PIPSIZ; 560f708ef1bSPoul-Henning Kamp static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 561f708ef1bSPoul-Henning Kamp static u_long unpdg_recvspace = 4*1024; 562df8bae1dSRodney W. Grimes 563f708ef1bSPoul-Henning Kamp static int unp_rights; /* file descriptors in flight */ 564df8bae1dSRodney W. Grimes 565ce02431fSDoug Rabson SYSCTL_DECL(_net_local_stream); 566639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 567639acc13SGarrett Wollman &unpst_sendspace, 0, ""); 568639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 569639acc13SGarrett Wollman &unpst_recvspace, 0, ""); 570ce02431fSDoug Rabson SYSCTL_DECL(_net_local_dgram); 571639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 572639acc13SGarrett Wollman &unpdg_sendspace, 0, ""); 573639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 574639acc13SGarrett Wollman &unpdg_recvspace, 0, ""); 575ce02431fSDoug Rabson SYSCTL_DECL(_net_local); 576639acc13SGarrett Wollman SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); 577639acc13SGarrett Wollman 578f708ef1bSPoul-Henning Kamp static int 579df8bae1dSRodney W. Grimes unp_attach(so) 580df8bae1dSRodney W. Grimes struct socket *so; 581df8bae1dSRodney W. Grimes { 582df8bae1dSRodney W. Grimes register struct unpcb *unp; 583df8bae1dSRodney W. Grimes int error; 584df8bae1dSRodney W. Grimes 585df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 586df8bae1dSRodney W. Grimes switch (so->so_type) { 587df8bae1dSRodney W. Grimes 588df8bae1dSRodney W. Grimes case SOCK_STREAM: 589df8bae1dSRodney W. Grimes error = soreserve(so, unpst_sendspace, unpst_recvspace); 590df8bae1dSRodney W. Grimes break; 591df8bae1dSRodney W. Grimes 592df8bae1dSRodney W. Grimes case SOCK_DGRAM: 593df8bae1dSRodney W. Grimes error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 594df8bae1dSRodney W. Grimes break; 595df8bae1dSRodney W. Grimes 596df8bae1dSRodney W. Grimes default: 597df8bae1dSRodney W. Grimes panic("unp_attach"); 598df8bae1dSRodney W. Grimes } 599df8bae1dSRodney W. Grimes if (error) 600df8bae1dSRodney W. Grimes return (error); 601df8bae1dSRodney W. Grimes } 602a163d034SWarner Losh unp = uma_zalloc(unp_zone, M_WAITOK); 60357bf258eSGarrett Wollman if (unp == NULL) 604df8bae1dSRodney W. Grimes return (ENOBUFS); 60557bf258eSGarrett Wollman bzero(unp, sizeof *unp); 60698271db4SGarrett Wollman LIST_INIT(&unp->unp_refs); 607df8bae1dSRodney W. Grimes unp->unp_socket = so; 6080d9ce3a1SRobert Watson 6090d9ce3a1SRobert Watson UNP_LOCK(); 6100d9ce3a1SRobert Watson unp->unp_gencnt = ++unp_gencnt; 6110d9ce3a1SRobert Watson unp_count++; 61298271db4SGarrett Wollman LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead 61398271db4SGarrett Wollman : &unp_shead, unp, unp_link); 6140d9ce3a1SRobert Watson UNP_UNLOCK(); 6150d9ce3a1SRobert Watson 616210a5a71SAlfred Perlstein so->so_pcb = unp; 617df8bae1dSRodney W. Grimes return (0); 618df8bae1dSRodney W. Grimes } 619df8bae1dSRodney W. Grimes 620f708ef1bSPoul-Henning Kamp static void 621df8bae1dSRodney W. Grimes unp_detach(unp) 622df8bae1dSRodney W. Grimes register struct unpcb *unp; 623df8bae1dSRodney W. Grimes { 6240d9ce3a1SRobert Watson struct vnode *vp; 6250d9ce3a1SRobert Watson 6260d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 6270d9ce3a1SRobert Watson 62898271db4SGarrett Wollman LIST_REMOVE(unp, unp_link); 62998271db4SGarrett Wollman unp->unp_gencnt = ++unp_gencnt; 63098271db4SGarrett Wollman --unp_count; 6310d9ce3a1SRobert Watson if ((vp = unp->unp_vnode) != NULL) { 6320d9ce3a1SRobert Watson /* 6330d9ce3a1SRobert Watson * XXXRW: should v_socket be frobbed only while holding 6340d9ce3a1SRobert Watson * Giant? 6350d9ce3a1SRobert Watson */ 636fc3fcacfSRobert Watson unp->unp_vnode->v_socket = NULL; 637fc3fcacfSRobert Watson unp->unp_vnode = NULL; 638df8bae1dSRodney W. Grimes } 639fc3fcacfSRobert Watson if (unp->unp_conn != NULL) 640df8bae1dSRodney W. Grimes unp_disconnect(unp); 6410d9ce3a1SRobert Watson while (!LIST_EMPTY(&unp->unp_refs)) { 6420d9ce3a1SRobert Watson struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 6430d9ce3a1SRobert Watson unp_drop(ref, ECONNRESET); 6440d9ce3a1SRobert Watson } 645df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 646fc3fcacfSRobert Watson unp->unp_socket->so_pcb = NULL; 647df8bae1dSRodney W. Grimes if (unp_rights) { 648df8bae1dSRodney W. Grimes /* 649df8bae1dSRodney W. Grimes * Normally the receive buffer is flushed later, 650df8bae1dSRodney W. Grimes * in sofree, but if our receive buffer holds references 651df8bae1dSRodney W. Grimes * to descriptors that are now garbage, we will dispose 652df8bae1dSRodney W. Grimes * of those descriptor references after the garbage collector 653df8bae1dSRodney W. Grimes * gets them (resulting in a "panic: closef: count < 0"). 654df8bae1dSRodney W. Grimes */ 655df8bae1dSRodney W. Grimes sorflush(unp->unp_socket); 656df8bae1dSRodney W. Grimes unp_gc(); 657df8bae1dSRodney W. Grimes } 658a5993a97SRobert Watson UNP_UNLOCK(); 659fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 66057bf258eSGarrett Wollman FREE(unp->unp_addr, M_SONAME); 6619e9d298aSJeff Roberson uma_zfree(unp_zone, unp); 6620d9ce3a1SRobert Watson if (vp) { 6630d9ce3a1SRobert Watson mtx_lock(&Giant); 6640d9ce3a1SRobert Watson vrele(vp); 6650d9ce3a1SRobert Watson mtx_unlock(&Giant); 6660d9ce3a1SRobert Watson } 667df8bae1dSRodney W. Grimes } 668df8bae1dSRodney W. Grimes 669f708ef1bSPoul-Henning Kamp static int 670b40ce416SJulian Elischer unp_bind(unp, nam, td) 671df8bae1dSRodney W. Grimes struct unpcb *unp; 67257bf258eSGarrett Wollman struct sockaddr *nam; 673b40ce416SJulian Elischer struct thread *td; 674df8bae1dSRodney W. Grimes { 67557bf258eSGarrett Wollman struct sockaddr_un *soun = (struct sockaddr_un *)nam; 676f2a2857bSKirk McKusick struct vnode *vp; 677f2a2857bSKirk McKusick struct mount *mp; 678df8bae1dSRodney W. Grimes struct vattr vattr; 67957bf258eSGarrett Wollman int error, namelen; 680df8bae1dSRodney W. Grimes struct nameidata nd; 6818f364875SJulian Elischer char *buf; 682df8bae1dSRodney W. Grimes 6830d9ce3a1SRobert Watson /* 6840d9ce3a1SRobert Watson * XXXRW: This test-and-set of unp_vnode is non-atomic; the 6850d9ce3a1SRobert Watson * unlocked read here is fine, but the value of unp_vnode needs 6860d9ce3a1SRobert Watson * to be tested again after we do all the lookups to see if the 6870d9ce3a1SRobert Watson * pcb is still unbound? 6880d9ce3a1SRobert Watson */ 689df8bae1dSRodney W. Grimes if (unp->unp_vnode != NULL) 690df8bae1dSRodney W. Grimes return (EINVAL); 69155c85568SRobert Drehmel 69257bf258eSGarrett Wollman namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 69357bf258eSGarrett Wollman if (namelen <= 0) 694e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 69555c85568SRobert Drehmel 696a163d034SWarner Losh buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 69755c85568SRobert Drehmel strlcpy(buf, soun->sun_path, namelen + 1); 69855c85568SRobert Drehmel 6990d9ce3a1SRobert Watson mtx_lock(&Giant); 700f2a2857bSKirk McKusick restart: 7010d9ce3a1SRobert Watson mtx_assert(&Giant, MA_OWNED); 702b65f6f6bSRobert Watson NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME, UIO_SYSSPACE, 703b40ce416SJulian Elischer buf, td); 704df8bae1dSRodney W. Grimes /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 705797f2d22SPoul-Henning Kamp error = namei(&nd); 7060d9ce3a1SRobert Watson if (error) 7070d9ce3a1SRobert Watson goto done; 708df8bae1dSRodney W. Grimes vp = nd.ni_vp; 709f2a2857bSKirk McKusick if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 710762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 711df8bae1dSRodney W. Grimes if (nd.ni_dvp == vp) 712df8bae1dSRodney W. Grimes vrele(nd.ni_dvp); 713df8bae1dSRodney W. Grimes else 714df8bae1dSRodney W. Grimes vput(nd.ni_dvp); 715f2a2857bSKirk McKusick if (vp != NULL) { 716df8bae1dSRodney W. Grimes vrele(vp); 7170d9ce3a1SRobert Watson error = EADDRINUSE; 7180d9ce3a1SRobert Watson goto done; 719df8bae1dSRodney W. Grimes } 7208f364875SJulian Elischer error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 7210d9ce3a1SRobert Watson if (error) 7220d9ce3a1SRobert Watson goto done; 723f2a2857bSKirk McKusick goto restart; 724f2a2857bSKirk McKusick } 725df8bae1dSRodney W. Grimes VATTR_NULL(&vattr); 726df8bae1dSRodney W. Grimes vattr.va_type = VSOCK; 727b40ce416SJulian Elischer vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 7286ea48a90SRobert Watson #ifdef MAC 7296ea48a90SRobert Watson error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 7306ea48a90SRobert Watson &vattr); 7316151efaaSRobert Watson #endif 7326ea48a90SRobert Watson if (error == 0) { 733a854ed98SJohn Baldwin VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE); 7347be2d300SMike Smith error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 7356ea48a90SRobert Watson } 736762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 7377be2d300SMike Smith vput(nd.ni_dvp); 7380d9ce3a1SRobert Watson if (error) 7390d9ce3a1SRobert Watson goto done; 740df8bae1dSRodney W. Grimes vp = nd.ni_vp; 7410d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_bind"); 7420d9ce3a1SRobert Watson soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 7430d9ce3a1SRobert Watson UNP_LOCK(); 744df8bae1dSRodney W. Grimes vp->v_socket = unp->unp_socket; 745df8bae1dSRodney W. Grimes unp->unp_vnode = vp; 7460d9ce3a1SRobert Watson unp->unp_addr = soun; 7470d9ce3a1SRobert Watson UNP_UNLOCK(); 748b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 749f2a2857bSKirk McKusick vn_finished_write(mp); 7500d9ce3a1SRobert Watson done: 7510d9ce3a1SRobert Watson mtx_unlock(&Giant); 7528f364875SJulian Elischer free(buf, M_TEMP); 7530d9ce3a1SRobert Watson return (error); 754df8bae1dSRodney W. Grimes } 755df8bae1dSRodney W. Grimes 756f708ef1bSPoul-Henning Kamp static int 757b40ce416SJulian Elischer unp_connect(so, nam, td) 758df8bae1dSRodney W. Grimes struct socket *so; 75957bf258eSGarrett Wollman struct sockaddr *nam; 760b40ce416SJulian Elischer struct thread *td; 761df8bae1dSRodney W. Grimes { 76257bf258eSGarrett Wollman register struct sockaddr_un *soun = (struct sockaddr_un *)nam; 763df8bae1dSRodney W. Grimes register struct vnode *vp; 764df8bae1dSRodney W. Grimes register struct socket *so2, *so3; 7650d9ce3a1SRobert Watson struct unpcb *unp = sotounpcb(so); 7660d9ce3a1SRobert Watson struct unpcb *unp2, *unp3; 76757bf258eSGarrett Wollman int error, len; 768df8bae1dSRodney W. Grimes struct nameidata nd; 76957bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 7700d9ce3a1SRobert Watson struct sockaddr *sa; 7710d9ce3a1SRobert Watson 7720d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 773df8bae1dSRodney W. Grimes 77457bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 77557bf258eSGarrett Wollman if (len <= 0) 776e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 77755c85568SRobert Drehmel strlcpy(buf, soun->sun_path, len + 1); 7780d9ce3a1SRobert Watson UNP_UNLOCK(); 7790d9ce3a1SRobert Watson sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 7800d9ce3a1SRobert Watson mtx_lock(&Giant); 781b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td); 782797f2d22SPoul-Henning Kamp error = namei(&nd); 783797f2d22SPoul-Henning Kamp if (error) 7840d9ce3a1SRobert Watson vp = NULL; 7850d9ce3a1SRobert Watson else 786df8bae1dSRodney W. Grimes vp = nd.ni_vp; 7870d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_connect"); 788762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 7890d9ce3a1SRobert Watson if (error) 7900d9ce3a1SRobert Watson goto bad; 7910d9ce3a1SRobert Watson 792df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 793df8bae1dSRodney W. Grimes error = ENOTSOCK; 794df8bae1dSRodney W. Grimes goto bad; 795df8bae1dSRodney W. Grimes } 796a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 797797f2d22SPoul-Henning Kamp if (error) 798df8bae1dSRodney W. Grimes goto bad; 7992260c03dSRobert Watson mtx_unlock(&Giant); 8002260c03dSRobert Watson UNP_LOCK(); 801df8bae1dSRodney W. Grimes so2 = vp->v_socket; 802fc3fcacfSRobert Watson if (so2 == NULL) { 803df8bae1dSRodney W. Grimes error = ECONNREFUSED; 8042260c03dSRobert Watson goto bad2; 805df8bae1dSRodney W. Grimes } 806df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 807df8bae1dSRodney W. Grimes error = EPROTOTYPE; 8082260c03dSRobert Watson goto bad2; 809df8bae1dSRodney W. Grimes } 810df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 8110d9ce3a1SRobert Watson if (so2->so_options & SO_ACCEPTCONN) { 8120d9ce3a1SRobert Watson /* 8130d9ce3a1SRobert Watson * NB: drop locks here so unp_attach is entered 8140d9ce3a1SRobert Watson * w/o locks; this avoids a recursive lock 8150d9ce3a1SRobert Watson * of the head and holding sleep locks across 8160d9ce3a1SRobert Watson * a (potentially) blocking malloc. 8170d9ce3a1SRobert Watson */ 8180d9ce3a1SRobert Watson UNP_UNLOCK(); 8190d9ce3a1SRobert Watson so3 = sonewconn(so2, 0); 8200d9ce3a1SRobert Watson UNP_LOCK(); 8210d9ce3a1SRobert Watson } else 8220d9ce3a1SRobert Watson so3 = NULL; 8230d9ce3a1SRobert Watson if (so3 == NULL) { 824df8bae1dSRodney W. Grimes error = ECONNREFUSED; 8250d9ce3a1SRobert Watson goto bad2; 826df8bae1dSRodney W. Grimes } 8270c1bb4fbSDima Dorfman unp = sotounpcb(so); 828df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 829df8bae1dSRodney W. Grimes unp3 = sotounpcb(so3); 8300d9ce3a1SRobert Watson if (unp2->unp_addr != NULL) { 8310d9ce3a1SRobert Watson bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 8320d9ce3a1SRobert Watson unp3->unp_addr = (struct sockaddr_un *) sa; 8330d9ce3a1SRobert Watson sa = NULL; 8340d9ce3a1SRobert Watson } 8350c1bb4fbSDima Dorfman /* 8360c1bb4fbSDima Dorfman * unp_peercred management: 8370c1bb4fbSDima Dorfman * 8380c1bb4fbSDima Dorfman * The connecter's (client's) credentials are copied 8390c1bb4fbSDima Dorfman * from its process structure at the time of connect() 8400c1bb4fbSDima Dorfman * (which is now). 8410c1bb4fbSDima Dorfman */ 842a854ed98SJohn Baldwin cru2x(td->td_ucred, &unp3->unp_peercred); 8430c1bb4fbSDima Dorfman unp3->unp_flags |= UNP_HAVEPC; 8440c1bb4fbSDima Dorfman /* 8450c1bb4fbSDima Dorfman * The receiver's (server's) credentials are copied 8460c1bb4fbSDima Dorfman * from the unp_peercred member of socket on which the 8470c1bb4fbSDima Dorfman * former called listen(); unp_listen() cached that 8480c1bb4fbSDima Dorfman * process's credentials at that time so we can use 8490c1bb4fbSDima Dorfman * them now. 8500c1bb4fbSDima Dorfman */ 8510c1bb4fbSDima Dorfman KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 8520c1bb4fbSDima Dorfman ("unp_connect: listener without cached peercred")); 8530c1bb4fbSDima Dorfman memcpy(&unp->unp_peercred, &unp2->unp_peercred, 8540c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 8550c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPC; 856335654d7SRobert Watson #ifdef MAC 857310e7cebSRobert Watson SOCK_LOCK(so); 858335654d7SRobert Watson mac_set_socket_peer_from_socket(so, so3); 859335654d7SRobert Watson mac_set_socket_peer_from_socket(so3, so); 860310e7cebSRobert Watson SOCK_UNLOCK(so); 861335654d7SRobert Watson #endif 8620c1bb4fbSDima Dorfman 863df8bae1dSRodney W. Grimes so2 = so3; 864df8bae1dSRodney W. Grimes } 865df8bae1dSRodney W. Grimes error = unp_connect2(so, so2); 8660d9ce3a1SRobert Watson bad2: 8670d9ce3a1SRobert Watson UNP_UNLOCK(); 8680d9ce3a1SRobert Watson mtx_lock(&Giant); 869df8bae1dSRodney W. Grimes bad: 8700d9ce3a1SRobert Watson mtx_assert(&Giant, MA_OWNED); 8710d9ce3a1SRobert Watson if (vp != NULL) 872df8bae1dSRodney W. Grimes vput(vp); 8730d9ce3a1SRobert Watson mtx_unlock(&Giant); 8740d9ce3a1SRobert Watson free(sa, M_SONAME); 8750d9ce3a1SRobert Watson UNP_LOCK(); 876df8bae1dSRodney W. Grimes return (error); 877df8bae1dSRodney W. Grimes } 878df8bae1dSRodney W. Grimes 879db48c0d2SRobert Watson static int 880df8bae1dSRodney W. Grimes unp_connect2(so, so2) 881df8bae1dSRodney W. Grimes register struct socket *so; 882df8bae1dSRodney W. Grimes register struct socket *so2; 883df8bae1dSRodney W. Grimes { 884df8bae1dSRodney W. Grimes register struct unpcb *unp = sotounpcb(so); 885df8bae1dSRodney W. Grimes register struct unpcb *unp2; 886df8bae1dSRodney W. Grimes 8870d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 8880d9ce3a1SRobert Watson 889df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 890df8bae1dSRodney W. Grimes return (EPROTOTYPE); 891df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 892df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 893df8bae1dSRodney W. Grimes switch (so->so_type) { 894df8bae1dSRodney W. Grimes 895df8bae1dSRodney W. Grimes case SOCK_DGRAM: 89698271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 897df8bae1dSRodney W. Grimes soisconnected(so); 898df8bae1dSRodney W. Grimes break; 899df8bae1dSRodney W. Grimes 900df8bae1dSRodney W. Grimes case SOCK_STREAM: 901df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 902df8bae1dSRodney W. Grimes soisconnected(so); 903df8bae1dSRodney W. Grimes soisconnected(so2); 904df8bae1dSRodney W. Grimes break; 905df8bae1dSRodney W. Grimes 906df8bae1dSRodney W. Grimes default: 907df8bae1dSRodney W. Grimes panic("unp_connect2"); 908df8bae1dSRodney W. Grimes } 909df8bae1dSRodney W. Grimes return (0); 910df8bae1dSRodney W. Grimes } 911df8bae1dSRodney W. Grimes 912f708ef1bSPoul-Henning Kamp static void 913df8bae1dSRodney W. Grimes unp_disconnect(unp) 914df8bae1dSRodney W. Grimes struct unpcb *unp; 915df8bae1dSRodney W. Grimes { 916df8bae1dSRodney W. Grimes register struct unpcb *unp2 = unp->unp_conn; 9171b2e3b4bSRobert Watson struct socket *so; 918df8bae1dSRodney W. Grimes 9190d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 9200d9ce3a1SRobert Watson 921fc3fcacfSRobert Watson if (unp2 == NULL) 922df8bae1dSRodney W. Grimes return; 923fc3fcacfSRobert Watson unp->unp_conn = NULL; 924df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 925df8bae1dSRodney W. Grimes 926df8bae1dSRodney W. Grimes case SOCK_DGRAM: 92798271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 9281b2e3b4bSRobert Watson so = unp->unp_socket; 9291b2e3b4bSRobert Watson SOCK_LOCK(so); 9301b2e3b4bSRobert Watson so->so_state &= ~SS_ISCONNECTED; 9311b2e3b4bSRobert Watson SOCK_UNLOCK(so); 932df8bae1dSRodney W. Grimes break; 933df8bae1dSRodney W. Grimes 934df8bae1dSRodney W. Grimes case SOCK_STREAM: 935df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 936fc3fcacfSRobert Watson unp2->unp_conn = NULL; 937df8bae1dSRodney W. Grimes soisdisconnected(unp2->unp_socket); 938df8bae1dSRodney W. Grimes break; 939df8bae1dSRodney W. Grimes } 940df8bae1dSRodney W. Grimes } 941df8bae1dSRodney W. Grimes 942df8bae1dSRodney W. Grimes #ifdef notdef 94326f9a767SRodney W. Grimes void 944df8bae1dSRodney W. Grimes unp_abort(unp) 945df8bae1dSRodney W. Grimes struct unpcb *unp; 946df8bae1dSRodney W. Grimes { 947df8bae1dSRodney W. Grimes 948df8bae1dSRodney W. Grimes unp_detach(unp); 949df8bae1dSRodney W. Grimes } 950df8bae1dSRodney W. Grimes #endif 951df8bae1dSRodney W. Grimes 9520d9ce3a1SRobert Watson /* 9530d9ce3a1SRobert Watson * unp_pcblist() assumes that UNIX domain socket memory is never reclaimed 9540d9ce3a1SRobert Watson * by the zone (UMA_ZONE_NOFREE), and as such potentially stale pointers 9550d9ce3a1SRobert Watson * are safe to reference. It first scans the list of struct unpcb's to 9560d9ce3a1SRobert Watson * generate a pointer list, then it rescans its list one entry at a time to 9570d9ce3a1SRobert Watson * externalize and copyout. It checks the generation number to see if a 9580d9ce3a1SRobert Watson * struct unpcb has been reused, and will skip it if so. 9590d9ce3a1SRobert Watson */ 96098271db4SGarrett Wollman static int 96182d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 96298271db4SGarrett Wollman { 963f5ef029eSPoul-Henning Kamp int error, i, n; 96498271db4SGarrett Wollman struct unpcb *unp, **unp_list; 96598271db4SGarrett Wollman unp_gen_t gencnt; 9668f364875SJulian Elischer struct xunpgen *xug; 96798271db4SGarrett Wollman struct unp_head *head; 9688f364875SJulian Elischer struct xunpcb *xu; 96998271db4SGarrett Wollman 970a23d65bfSBruce Evans head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 97198271db4SGarrett Wollman 97298271db4SGarrett Wollman /* 97398271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 97498271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 97598271db4SGarrett Wollman */ 976fc3fcacfSRobert Watson if (req->oldptr == NULL) { 97798271db4SGarrett Wollman n = unp_count; 9788f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug) 97998271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 980e5aeaa0cSDag-Erling Smørgrav return (0); 98198271db4SGarrett Wollman } 98298271db4SGarrett Wollman 983fc3fcacfSRobert Watson if (req->newptr != NULL) 984e5aeaa0cSDag-Erling Smørgrav return (EPERM); 98598271db4SGarrett Wollman 98698271db4SGarrett Wollman /* 98798271db4SGarrett Wollman * OK, now we're committed to doing something. 98898271db4SGarrett Wollman */ 989a163d034SWarner Losh xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 9900d9ce3a1SRobert Watson UNP_LOCK(); 99198271db4SGarrett Wollman gencnt = unp_gencnt; 99298271db4SGarrett Wollman n = unp_count; 9930d9ce3a1SRobert Watson UNP_UNLOCK(); 99498271db4SGarrett Wollman 9958f364875SJulian Elischer xug->xug_len = sizeof *xug; 9968f364875SJulian Elischer xug->xug_count = n; 9978f364875SJulian Elischer xug->xug_gen = gencnt; 9988f364875SJulian Elischer xug->xug_sogen = so_gencnt; 9998f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 10008f364875SJulian Elischer if (error) { 10018f364875SJulian Elischer free(xug, M_TEMP); 1002e5aeaa0cSDag-Erling Smørgrav return (error); 10038f364875SJulian Elischer } 100498271db4SGarrett Wollman 1005a163d034SWarner Losh unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 100698271db4SGarrett Wollman 10070d9ce3a1SRobert Watson UNP_LOCK(); 10082e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 10092e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 10108a7d8cc6SRobert Watson if (unp->unp_gencnt <= gencnt) { 1011a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred, 10128a7d8cc6SRobert Watson unp->unp_socket->so_cred)) 10134787fd37SPaul Saab continue; 101498271db4SGarrett Wollman unp_list[i++] = unp; 101598271db4SGarrett Wollman } 10164787fd37SPaul Saab } 10170d9ce3a1SRobert Watson UNP_UNLOCK(); 101898271db4SGarrett Wollman n = i; /* in case we lost some during malloc */ 101998271db4SGarrett Wollman 102098271db4SGarrett Wollman error = 0; 1021a163d034SWarner Losh xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK); 102298271db4SGarrett Wollman for (i = 0; i < n; i++) { 102398271db4SGarrett Wollman unp = unp_list[i]; 102498271db4SGarrett Wollman if (unp->unp_gencnt <= gencnt) { 10258f364875SJulian Elischer xu->xu_len = sizeof *xu; 10268f364875SJulian Elischer xu->xu_unpp = unp; 102798271db4SGarrett Wollman /* 102898271db4SGarrett Wollman * XXX - need more locking here to protect against 102998271db4SGarrett Wollman * connect/disconnect races for SMP. 103098271db4SGarrett Wollman */ 1031fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 10328f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr, 103398271db4SGarrett Wollman unp->unp_addr->sun_len); 1034fc3fcacfSRobert Watson if (unp->unp_conn != NULL && 1035fc3fcacfSRobert Watson unp->unp_conn->unp_addr != NULL) 103698271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 10378f364875SJulian Elischer &xu->xu_caddr, 103898271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 10398f364875SJulian Elischer bcopy(unp, &xu->xu_unp, sizeof *unp); 10408f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket); 10418f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu); 104298271db4SGarrett Wollman } 104398271db4SGarrett Wollman } 10448f364875SJulian Elischer free(xu, M_TEMP); 104598271db4SGarrett Wollman if (!error) { 104698271db4SGarrett Wollman /* 104798271db4SGarrett Wollman * Give the user an updated idea of our state. 104898271db4SGarrett Wollman * If the generation differs from what we told 104998271db4SGarrett Wollman * her before, she knows that something happened 105098271db4SGarrett Wollman * while we were processing this request, and it 105198271db4SGarrett Wollman * might be necessary to retry. 105298271db4SGarrett Wollman */ 10538f364875SJulian Elischer xug->xug_gen = unp_gencnt; 10548f364875SJulian Elischer xug->xug_sogen = so_gencnt; 10558f364875SJulian Elischer xug->xug_count = unp_count; 10568f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 105798271db4SGarrett Wollman } 105898271db4SGarrett Wollman free(unp_list, M_TEMP); 10598f364875SJulian Elischer free(xug, M_TEMP); 1060e5aeaa0cSDag-Erling Smørgrav return (error); 106198271db4SGarrett Wollman } 106298271db4SGarrett Wollman 106398271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 106498271db4SGarrett Wollman (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 106598271db4SGarrett Wollman "List of active local datagram sockets"); 106698271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 106798271db4SGarrett Wollman (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 106898271db4SGarrett Wollman "List of active local stream sockets"); 106998271db4SGarrett Wollman 1070f708ef1bSPoul-Henning Kamp static void 1071df8bae1dSRodney W. Grimes unp_shutdown(unp) 1072df8bae1dSRodney W. Grimes struct unpcb *unp; 1073df8bae1dSRodney W. Grimes { 1074df8bae1dSRodney W. Grimes struct socket *so; 1075df8bae1dSRodney W. Grimes 10760d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 10770d9ce3a1SRobert Watson 1078df8bae1dSRodney W. Grimes if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 1079df8bae1dSRodney W. Grimes (so = unp->unp_conn->unp_socket)) 1080df8bae1dSRodney W. Grimes socantrcvmore(so); 1081df8bae1dSRodney W. Grimes } 1082df8bae1dSRodney W. Grimes 1083f708ef1bSPoul-Henning Kamp static void 1084df8bae1dSRodney W. Grimes unp_drop(unp, errno) 1085df8bae1dSRodney W. Grimes struct unpcb *unp; 1086df8bae1dSRodney W. Grimes int errno; 1087df8bae1dSRodney W. Grimes { 1088df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 1089df8bae1dSRodney W. Grimes 10900d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 10910d9ce3a1SRobert Watson 1092df8bae1dSRodney W. Grimes so->so_error = errno; 1093df8bae1dSRodney W. Grimes unp_disconnect(unp); 1094df8bae1dSRodney W. Grimes } 1095df8bae1dSRodney W. Grimes 1096df8bae1dSRodney W. Grimes #ifdef notdef 109726f9a767SRodney W. Grimes void 1098df8bae1dSRodney W. Grimes unp_drain() 1099df8bae1dSRodney W. Grimes { 1100df8bae1dSRodney W. Grimes 1101df8bae1dSRodney W. Grimes } 1102df8bae1dSRodney W. Grimes #endif 1103df8bae1dSRodney W. Grimes 11042bc21ed9SDavid Malone static void 11052bc21ed9SDavid Malone unp_freerights(rp, fdcount) 11062bc21ed9SDavid Malone struct file **rp; 11072bc21ed9SDavid Malone int fdcount; 1108df8bae1dSRodney W. Grimes { 11092bc21ed9SDavid Malone int i; 11102bc21ed9SDavid Malone struct file *fp; 1111df8bae1dSRodney W. Grimes 11122bc21ed9SDavid Malone for (i = 0; i < fdcount; i++) { 1113df8bae1dSRodney W. Grimes fp = *rp; 11148692c025SYoshinobu Inoue /* 11152bc21ed9SDavid Malone * zero the pointer before calling 11162bc21ed9SDavid Malone * unp_discard since it may end up 11172bc21ed9SDavid Malone * in unp_gc().. 11188692c025SYoshinobu Inoue */ 1119df8bae1dSRodney W. Grimes *rp++ = 0; 11208692c025SYoshinobu Inoue unp_discard(fp); 1121df8bae1dSRodney W. Grimes } 11222bc21ed9SDavid Malone } 11232bc21ed9SDavid Malone 11242bc21ed9SDavid Malone int 11252bc21ed9SDavid Malone unp_externalize(control, controlp) 11262bc21ed9SDavid Malone struct mbuf *control, **controlp; 11272bc21ed9SDavid Malone { 11282bc21ed9SDavid Malone struct thread *td = curthread; /* XXX */ 11292bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 11302bc21ed9SDavid Malone int i; 11312bc21ed9SDavid Malone int *fdp; 11322bc21ed9SDavid Malone struct file **rp; 11332bc21ed9SDavid Malone struct file *fp; 11342bc21ed9SDavid Malone void *data; 11352bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 11362bc21ed9SDavid Malone int error, newfds; 11372bc21ed9SDavid Malone int f; 11382bc21ed9SDavid Malone u_int newlen; 11392bc21ed9SDavid Malone 11402bc21ed9SDavid Malone error = 0; 11412bc21ed9SDavid Malone if (controlp != NULL) /* controlp == NULL => free control messages */ 11422bc21ed9SDavid Malone *controlp = NULL; 11432bc21ed9SDavid Malone 11442bc21ed9SDavid Malone while (cm != NULL) { 11452bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 11462bc21ed9SDavid Malone error = EINVAL; 11472bc21ed9SDavid Malone break; 11482bc21ed9SDavid Malone } 11492bc21ed9SDavid Malone 11502bc21ed9SDavid Malone data = CMSG_DATA(cm); 11512bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 11522bc21ed9SDavid Malone 11532bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET 11542bc21ed9SDavid Malone && cm->cmsg_type == SCM_RIGHTS) { 11552bc21ed9SDavid Malone newfds = datalen / sizeof(struct file *); 11562bc21ed9SDavid Malone rp = data; 11572bc21ed9SDavid Malone 1158e2f9a08bSOlivier Houchard /* If we're not outputting the descriptors free them. */ 11592bc21ed9SDavid Malone if (error || controlp == NULL) { 11602bc21ed9SDavid Malone unp_freerights(rp, newfds); 11612bc21ed9SDavid Malone goto next; 11622bc21ed9SDavid Malone } 1163426da3bcSAlfred Perlstein FILEDESC_LOCK(td->td_proc->p_fd); 11642bc21ed9SDavid Malone /* if the new FD's will not fit free them. */ 11652bc21ed9SDavid Malone if (!fdavail(td, newfds)) { 1166426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 11672bc21ed9SDavid Malone error = EMSGSIZE; 11682bc21ed9SDavid Malone unp_freerights(rp, newfds); 11692bc21ed9SDavid Malone goto next; 1170df8bae1dSRodney W. Grimes } 1171ed5b7817SJulian Elischer /* 11722bc21ed9SDavid Malone * now change each pointer to an fd in the global 11732bc21ed9SDavid Malone * table to an integer that is the index to the 11742bc21ed9SDavid Malone * local fd table entry that we set up to point 11752bc21ed9SDavid Malone * to the global one we are transferring. 1176ed5b7817SJulian Elischer */ 11772bc21ed9SDavid Malone newlen = newfds * sizeof(int); 11782bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 11792bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 11802bc21ed9SDavid Malone if (*controlp == NULL) { 1181426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 11822bc21ed9SDavid Malone error = E2BIG; 11832bc21ed9SDavid Malone unp_freerights(rp, newfds); 11842bc21ed9SDavid Malone goto next; 11852bc21ed9SDavid Malone } 11862bc21ed9SDavid Malone 11872bc21ed9SDavid Malone fdp = (int *) 11882bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1189df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 1190a6d4491cSDag-Erling Smørgrav if (fdalloc(td, 0, &f)) 11912bc21ed9SDavid Malone panic("unp_externalize fdalloc failed"); 11928692c025SYoshinobu Inoue fp = *rp++; 1193b40ce416SJulian Elischer td->td_proc->p_fd->fd_ofiles[f] = fp; 1194426da3bcSAlfred Perlstein FILE_LOCK(fp); 1195df8bae1dSRodney W. Grimes fp->f_msgcount--; 1196426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1197df8bae1dSRodney W. Grimes unp_rights--; 11988692c025SYoshinobu Inoue *fdp++ = f; 1199df8bae1dSRodney W. Grimes } 1200426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 12012bc21ed9SDavid Malone } else { /* We can just copy anything else across */ 12022bc21ed9SDavid Malone if (error || controlp == NULL) 12032bc21ed9SDavid Malone goto next; 12042bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, datalen, 12052bc21ed9SDavid Malone cm->cmsg_type, cm->cmsg_level); 12062bc21ed9SDavid Malone if (*controlp == NULL) { 12072bc21ed9SDavid Malone error = ENOBUFS; 12082bc21ed9SDavid Malone goto next; 12092bc21ed9SDavid Malone } 12102bc21ed9SDavid Malone bcopy(data, 12112bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 12122bc21ed9SDavid Malone datalen); 12132bc21ed9SDavid Malone } 12142bc21ed9SDavid Malone 12152bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 12162bc21ed9SDavid Malone 12172bc21ed9SDavid Malone next: 12182bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 12192bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 12202bc21ed9SDavid Malone cm = (struct cmsghdr *) 12212bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 12228692c025SYoshinobu Inoue } else { 12232bc21ed9SDavid Malone clen = 0; 12242bc21ed9SDavid Malone cm = NULL; 12258692c025SYoshinobu Inoue } 12268692c025SYoshinobu Inoue } 12278692c025SYoshinobu Inoue 12282bc21ed9SDavid Malone m_freem(control); 12292bc21ed9SDavid Malone 12302bc21ed9SDavid Malone return (error); 1231df8bae1dSRodney W. Grimes } 1232df8bae1dSRodney W. Grimes 123398271db4SGarrett Wollman void 123498271db4SGarrett Wollman unp_init(void) 123598271db4SGarrett Wollman { 12369e9d298aSJeff Roberson unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 12379e9d298aSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 1238fc3fcacfSRobert Watson if (unp_zone == NULL) 123998271db4SGarrett Wollman panic("unp_init"); 1240b17dd2bcSColin Percival uma_zone_set_max(unp_zone, nmbclusters); 124198271db4SGarrett Wollman LIST_INIT(&unp_dhead); 124298271db4SGarrett Wollman LIST_INIT(&unp_shead); 12430d9ce3a1SRobert Watson 12440d9ce3a1SRobert Watson UNP_LOCK_INIT(); 124598271db4SGarrett Wollman } 124698271db4SGarrett Wollman 1247f708ef1bSPoul-Henning Kamp static int 12482bc21ed9SDavid Malone unp_internalize(controlp, td) 12492bc21ed9SDavid Malone struct mbuf **controlp; 1250b40ce416SJulian Elischer struct thread *td; 1251df8bae1dSRodney W. Grimes { 12522bc21ed9SDavid Malone struct mbuf *control = *controlp; 1253b40ce416SJulian Elischer struct proc *p = td->td_proc; 12548692c025SYoshinobu Inoue struct filedesc *fdescp = p->p_fd; 12552bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 12562bc21ed9SDavid Malone struct cmsgcred *cmcred; 12572bc21ed9SDavid Malone struct file **rp; 12582bc21ed9SDavid Malone struct file *fp; 12592bc21ed9SDavid Malone struct timeval *tv; 12602bc21ed9SDavid Malone int i, fd, *fdp; 12612bc21ed9SDavid Malone void *data; 12622bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 12632bc21ed9SDavid Malone int error, oldfds; 12648692c025SYoshinobu Inoue u_int newlen; 1265df8bae1dSRodney W. Grimes 12662bc21ed9SDavid Malone error = 0; 12672bc21ed9SDavid Malone *controlp = NULL; 12680b788fa1SBill Paul 12692bc21ed9SDavid Malone while (cm != NULL) { 12702bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 12712bc21ed9SDavid Malone || cm->cmsg_len > clen) { 12722bc21ed9SDavid Malone error = EINVAL; 12732bc21ed9SDavid Malone goto out; 12742bc21ed9SDavid Malone } 12752bc21ed9SDavid Malone 12762bc21ed9SDavid Malone data = CMSG_DATA(cm); 12772bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 12782bc21ed9SDavid Malone 12792bc21ed9SDavid Malone switch (cm->cmsg_type) { 12800b788fa1SBill Paul /* 12810b788fa1SBill Paul * Fill in credential information. 12820b788fa1SBill Paul */ 12832bc21ed9SDavid Malone case SCM_CREDS: 12842bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 12852bc21ed9SDavid Malone SCM_CREDS, SOL_SOCKET); 12862bc21ed9SDavid Malone if (*controlp == NULL) { 12872bc21ed9SDavid Malone error = ENOBUFS; 12882bc21ed9SDavid Malone goto out; 12892bc21ed9SDavid Malone } 12902bc21ed9SDavid Malone 12912bc21ed9SDavid Malone cmcred = (struct cmsgcred *) 12922bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 12930b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 1294a854ed98SJohn Baldwin cmcred->cmcred_uid = td->td_ucred->cr_ruid; 1295a854ed98SJohn Baldwin cmcred->cmcred_gid = td->td_ucred->cr_rgid; 1296a854ed98SJohn Baldwin cmcred->cmcred_euid = td->td_ucred->cr_uid; 1297a854ed98SJohn Baldwin cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 12980b788fa1SBill Paul CMGROUP_MAX); 12990b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 13002bc21ed9SDavid Malone cmcred->cmcred_groups[i] = 1301a854ed98SJohn Baldwin td->td_ucred->cr_groups[i]; 13022bc21ed9SDavid Malone break; 13030b788fa1SBill Paul 13042bc21ed9SDavid Malone case SCM_RIGHTS: 13052bc21ed9SDavid Malone oldfds = datalen / sizeof (int); 1306ed5b7817SJulian Elischer /* 13072bc21ed9SDavid Malone * check that all the FDs passed in refer to legal files 1308ed5b7817SJulian Elischer * If not, reject the entire operation. 1309ed5b7817SJulian Elischer */ 13102bc21ed9SDavid Malone fdp = data; 1311426da3bcSAlfred Perlstein FILEDESC_LOCK(fdescp); 1312df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 13138692c025SYoshinobu Inoue fd = *fdp++; 13148692c025SYoshinobu Inoue if ((unsigned)fd >= fdescp->fd_nfiles || 13152bc21ed9SDavid Malone fdescp->fd_ofiles[fd] == NULL) { 1316426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 13172bc21ed9SDavid Malone error = EBADF; 13182bc21ed9SDavid Malone goto out; 13192bc21ed9SDavid Malone } 1320e7d6662fSAlfred Perlstein fp = fdescp->fd_ofiles[fd]; 1321e7d6662fSAlfred Perlstein if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 1322e7d6662fSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 1323e7d6662fSAlfred Perlstein error = EOPNOTSUPP; 1324e7d6662fSAlfred Perlstein goto out; 1325e7d6662fSAlfred Perlstein } 1326e7d6662fSAlfred Perlstein 1327df8bae1dSRodney W. Grimes } 1328ed5b7817SJulian Elischer /* 1329ed5b7817SJulian Elischer * Now replace the integer FDs with pointers to 1330ed5b7817SJulian Elischer * the associated global file table entry.. 1331ed5b7817SJulian Elischer */ 13322bc21ed9SDavid Malone newlen = oldfds * sizeof(struct file *); 13332bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 13342bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 13352bc21ed9SDavid Malone if (*controlp == NULL) { 1336426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 13372bc21ed9SDavid Malone error = E2BIG; 13382bc21ed9SDavid Malone goto out; 13398692c025SYoshinobu Inoue } 13408692c025SYoshinobu Inoue 13412bc21ed9SDavid Malone fdp = data; 13422bc21ed9SDavid Malone rp = (struct file **) 13432bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 13448692c025SYoshinobu Inoue for (i = 0; i < oldfds; i++) { 13458692c025SYoshinobu Inoue fp = fdescp->fd_ofiles[*fdp++]; 1346df8bae1dSRodney W. Grimes *rp++ = fp; 1347426da3bcSAlfred Perlstein FILE_LOCK(fp); 1348df8bae1dSRodney W. Grimes fp->f_count++; 1349df8bae1dSRodney W. Grimes fp->f_msgcount++; 1350426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1351df8bae1dSRodney W. Grimes unp_rights++; 1352df8bae1dSRodney W. Grimes } 1353426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 13542bc21ed9SDavid Malone break; 13552bc21ed9SDavid Malone 13562bc21ed9SDavid Malone case SCM_TIMESTAMP: 13572bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*tv), 13582bc21ed9SDavid Malone SCM_TIMESTAMP, SOL_SOCKET); 13592bc21ed9SDavid Malone if (*controlp == NULL) { 13602bc21ed9SDavid Malone error = ENOBUFS; 13612bc21ed9SDavid Malone goto out; 13628692c025SYoshinobu Inoue } 13632bc21ed9SDavid Malone tv = (struct timeval *) 13642bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 13652bc21ed9SDavid Malone microtime(tv); 13662bc21ed9SDavid Malone break; 13672bc21ed9SDavid Malone 13682bc21ed9SDavid Malone default: 13692bc21ed9SDavid Malone error = EINVAL; 13702bc21ed9SDavid Malone goto out; 13712bc21ed9SDavid Malone } 13722bc21ed9SDavid Malone 13732bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 13742bc21ed9SDavid Malone 13752bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 13762bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 13772bc21ed9SDavid Malone cm = (struct cmsghdr *) 13782bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 13792bc21ed9SDavid Malone } else { 13802bc21ed9SDavid Malone clen = 0; 13812bc21ed9SDavid Malone cm = NULL; 13822bc21ed9SDavid Malone } 13832bc21ed9SDavid Malone } 13842bc21ed9SDavid Malone 13852bc21ed9SDavid Malone out: 13862bc21ed9SDavid Malone m_freem(control); 13872bc21ed9SDavid Malone 13882bc21ed9SDavid Malone return (error); 1389df8bae1dSRodney W. Grimes } 1390df8bae1dSRodney W. Grimes 1391f708ef1bSPoul-Henning Kamp static int unp_defer, unp_gcing; 1392df8bae1dSRodney W. Grimes 1393f708ef1bSPoul-Henning Kamp static void 1394df8bae1dSRodney W. Grimes unp_gc() 1395df8bae1dSRodney W. Grimes { 1396df8bae1dSRodney W. Grimes register struct file *fp, *nextfp; 1397df8bae1dSRodney W. Grimes register struct socket *so; 1398df8bae1dSRodney W. Grimes struct file **extra_ref, **fpp; 1399df8bae1dSRodney W. Grimes int nunref, i; 140095f004dcSAlfred Perlstein int nfiles_snap; 140195f004dcSAlfred Perlstein int nfiles_slack = 20; 1402df8bae1dSRodney W. Grimes 14030d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 14040d9ce3a1SRobert Watson 1405df8bae1dSRodney W. Grimes if (unp_gcing) 1406df8bae1dSRodney W. Grimes return; 1407df8bae1dSRodney W. Grimes unp_gcing = 1; 1408df8bae1dSRodney W. Grimes unp_defer = 0; 1409ed5b7817SJulian Elischer /* 1410ed5b7817SJulian Elischer * before going through all this, set all FDs to 1411ed5b7817SJulian Elischer * be NOT defered and NOT externally accessible 1412ed5b7817SJulian Elischer */ 14130d9ce3a1SRobert Watson /* 14140d9ce3a1SRobert Watson * XXXRW: Acquiring a sleep lock while holding UNP 14150d9ce3a1SRobert Watson * mutex cannot be a good thing. 14160d9ce3a1SRobert Watson */ 1417426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 14182e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) 1419426da3bcSAlfred Perlstein fp->f_gcflag &= ~(FMARK|FDEFER); 1420df8bae1dSRodney W. Grimes do { 14212e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) { 1422426da3bcSAlfred Perlstein FILE_LOCK(fp); 1423ed5b7817SJulian Elischer /* 1424ed5b7817SJulian Elischer * If the file is not open, skip it 1425ed5b7817SJulian Elischer */ 1426426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1427426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1428df8bae1dSRodney W. Grimes continue; 1429426da3bcSAlfred Perlstein } 1430ed5b7817SJulian Elischer /* 1431ed5b7817SJulian Elischer * If we already marked it as 'defer' in a 1432ed5b7817SJulian Elischer * previous pass, then try process it this time 1433ed5b7817SJulian Elischer * and un-mark it 1434ed5b7817SJulian Elischer */ 1435426da3bcSAlfred Perlstein if (fp->f_gcflag & FDEFER) { 1436426da3bcSAlfred Perlstein fp->f_gcflag &= ~FDEFER; 1437df8bae1dSRodney W. Grimes unp_defer--; 1438df8bae1dSRodney W. Grimes } else { 1439ed5b7817SJulian Elischer /* 1440ed5b7817SJulian Elischer * if it's not defered, then check if it's 1441ed5b7817SJulian Elischer * already marked.. if so skip it 1442ed5b7817SJulian Elischer */ 1443426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) { 1444426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1445df8bae1dSRodney W. Grimes continue; 1446426da3bcSAlfred Perlstein } 1447ed5b7817SJulian Elischer /* 1448ed5b7817SJulian Elischer * If all references are from messages 1449ed5b7817SJulian Elischer * in transit, then skip it. it's not 1450ed5b7817SJulian Elischer * externally accessible. 1451ed5b7817SJulian Elischer */ 1452426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount) { 1453426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1454df8bae1dSRodney W. Grimes continue; 1455426da3bcSAlfred Perlstein } 1456ed5b7817SJulian Elischer /* 1457ed5b7817SJulian Elischer * If it got this far then it must be 1458ed5b7817SJulian Elischer * externally accessible. 1459ed5b7817SJulian Elischer */ 1460426da3bcSAlfred Perlstein fp->f_gcflag |= FMARK; 1461df8bae1dSRodney W. Grimes } 1462ed5b7817SJulian Elischer /* 1463ed5b7817SJulian Elischer * either it was defered, or it is externally 1464ed5b7817SJulian Elischer * accessible and not already marked so. 1465ed5b7817SJulian Elischer * Now check if it is possibly one of OUR sockets. 1466ed5b7817SJulian Elischer */ 1467df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_SOCKET || 146848e3128bSMatthew Dillon (so = fp->f_data) == NULL) { 1469426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1470df8bae1dSRodney W. Grimes continue; 1471426da3bcSAlfred Perlstein } 1472426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1473748e0b0aSGarrett Wollman if (so->so_proto->pr_domain != &localdomain || 1474df8bae1dSRodney W. Grimes (so->so_proto->pr_flags&PR_RIGHTS) == 0) 1475df8bae1dSRodney W. Grimes continue; 1476df8bae1dSRodney W. Grimes #ifdef notdef 1477df8bae1dSRodney W. Grimes if (so->so_rcv.sb_flags & SB_LOCK) { 1478df8bae1dSRodney W. Grimes /* 1479df8bae1dSRodney W. Grimes * This is problematical; it's not clear 1480df8bae1dSRodney W. Grimes * we need to wait for the sockbuf to be 1481df8bae1dSRodney W. Grimes * unlocked (on a uniprocessor, at least), 1482df8bae1dSRodney W. Grimes * and it's also not clear what to do 1483df8bae1dSRodney W. Grimes * if sbwait returns an error due to receipt 1484df8bae1dSRodney W. Grimes * of a signal. If sbwait does return 1485df8bae1dSRodney W. Grimes * an error, we'll go into an infinite 1486df8bae1dSRodney W. Grimes * loop. Delete all of this for now. 1487df8bae1dSRodney W. Grimes */ 1488df8bae1dSRodney W. Grimes (void) sbwait(&so->so_rcv); 1489df8bae1dSRodney W. Grimes goto restart; 1490df8bae1dSRodney W. Grimes } 1491df8bae1dSRodney W. Grimes #endif 1492ed5b7817SJulian Elischer /* 1493ed5b7817SJulian Elischer * So, Ok, it's one of our sockets and it IS externally 1494ed5b7817SJulian Elischer * accessible (or was defered). Now we look 1495dc733423SDag-Erling Smørgrav * to see if we hold any file descriptors in its 1496ed5b7817SJulian Elischer * message buffers. Follow those links and mark them 1497ed5b7817SJulian Elischer * as accessible too. 1498ed5b7817SJulian Elischer */ 14997717cf07SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1500df8bae1dSRodney W. Grimes unp_scan(so->so_rcv.sb_mb, unp_mark); 15017717cf07SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 1502df8bae1dSRodney W. Grimes } 1503df8bae1dSRodney W. Grimes } while (unp_defer); 1504426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1505df8bae1dSRodney W. Grimes /* 1506df8bae1dSRodney W. Grimes * We grab an extra reference to each of the file table entries 1507df8bae1dSRodney W. Grimes * that are not otherwise accessible and then free the rights 1508df8bae1dSRodney W. Grimes * that are stored in messages on them. 1509df8bae1dSRodney W. Grimes * 1510df8bae1dSRodney W. Grimes * The bug in the orginal code is a little tricky, so I'll describe 1511df8bae1dSRodney W. Grimes * what's wrong with it here. 1512df8bae1dSRodney W. Grimes * 1513df8bae1dSRodney W. Grimes * It is incorrect to simply unp_discard each entry for f_msgcount 1514df8bae1dSRodney W. Grimes * times -- consider the case of sockets A and B that contain 1515df8bae1dSRodney W. Grimes * references to each other. On a last close of some other socket, 1516df8bae1dSRodney W. Grimes * we trigger a gc since the number of outstanding rights (unp_rights) 1517df8bae1dSRodney W. Grimes * is non-zero. If during the sweep phase the gc code un_discards, 1518df8bae1dSRodney W. Grimes * we end up doing a (full) closef on the descriptor. A closef on A 1519df8bae1dSRodney W. Grimes * results in the following chain. Closef calls soo_close, which 1520df8bae1dSRodney W. Grimes * calls soclose. Soclose calls first (through the switch 1521df8bae1dSRodney W. Grimes * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 1522df8bae1dSRodney W. Grimes * returns because the previous instance had set unp_gcing, and 1523df8bae1dSRodney W. Grimes * we return all the way back to soclose, which marks the socket 1524df8bae1dSRodney W. Grimes * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush 1525df8bae1dSRodney W. Grimes * to free up the rights that are queued in messages on the socket A, 1526df8bae1dSRodney W. Grimes * i.e., the reference on B. The sorflush calls via the dom_dispose 1527df8bae1dSRodney W. Grimes * switch unp_dispose, which unp_scans with unp_discard. This second 1528df8bae1dSRodney W. Grimes * instance of unp_discard just calls closef on B. 1529df8bae1dSRodney W. Grimes * 1530df8bae1dSRodney W. Grimes * Well, a similar chain occurs on B, resulting in a sorflush on B, 1531df8bae1dSRodney W. Grimes * which results in another closef on A. Unfortunately, A is already 1532df8bae1dSRodney W. Grimes * being closed, and the descriptor has already been marked with 1533df8bae1dSRodney W. Grimes * SS_NOFDREF, and soclose panics at this point. 1534df8bae1dSRodney W. Grimes * 1535df8bae1dSRodney W. Grimes * Here, we first take an extra reference to each inaccessible 1536df8bae1dSRodney W. Grimes * descriptor. Then, we call sorflush ourself, since we know 1537df8bae1dSRodney W. Grimes * it is a Unix domain socket anyhow. After we destroy all the 1538df8bae1dSRodney W. Grimes * rights carried in messages, we do a last closef to get rid 1539df8bae1dSRodney W. Grimes * of our extra reference. This is the last close, and the 1540df8bae1dSRodney W. Grimes * unp_detach etc will shut down the socket. 1541df8bae1dSRodney W. Grimes * 1542df8bae1dSRodney W. Grimes * 91/09/19, bsy@cs.cmu.edu 1543df8bae1dSRodney W. Grimes */ 154495f004dcSAlfred Perlstein again: 154595f004dcSAlfred Perlstein nfiles_snap = nfiles + nfiles_slack; /* some slack */ 154695f004dcSAlfred Perlstein extra_ref = malloc(nfiles_snap * sizeof(struct file *), M_TEMP, 154795f004dcSAlfred Perlstein M_WAITOK); 1548426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 154995f004dcSAlfred Perlstein if (nfiles_snap < nfiles) { 155095f004dcSAlfred Perlstein sx_sunlock(&filelist_lock); 155195f004dcSAlfred Perlstein free(extra_ref, M_TEMP); 155295f004dcSAlfred Perlstein nfiles_slack += 20; 155395f004dcSAlfred Perlstein goto again; 155495f004dcSAlfred Perlstein } 1555fc3fcacfSRobert Watson for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; 1556fc3fcacfSRobert Watson fp != NULL; fp = nextfp) { 15572e3c8fcbSPoul-Henning Kamp nextfp = LIST_NEXT(fp, f_list); 1558426da3bcSAlfred Perlstein FILE_LOCK(fp); 1559ed5b7817SJulian Elischer /* 1560ed5b7817SJulian Elischer * If it's not open, skip it 1561ed5b7817SJulian Elischer */ 1562426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1563426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1564df8bae1dSRodney W. Grimes continue; 1565426da3bcSAlfred Perlstein } 1566ed5b7817SJulian Elischer /* 1567ed5b7817SJulian Elischer * If all refs are from msgs, and it's not marked accessible 1568ed5b7817SJulian Elischer * then it must be referenced from some unreachable cycle 1569ed5b7817SJulian Elischer * of (shut-down) FDs, so include it in our 1570ed5b7817SJulian Elischer * list of FDs to remove 1571ed5b7817SJulian Elischer */ 1572426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) { 1573df8bae1dSRodney W. Grimes *fpp++ = fp; 1574df8bae1dSRodney W. Grimes nunref++; 1575df8bae1dSRodney W. Grimes fp->f_count++; 1576df8bae1dSRodney W. Grimes } 1577426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1578df8bae1dSRodney W. Grimes } 1579426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1580ed5b7817SJulian Elischer /* 1581ed5b7817SJulian Elischer * for each FD on our hit list, do the following two things 1582ed5b7817SJulian Elischer */ 15831c7c3c6aSMatthew Dillon for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 15841c7c3c6aSMatthew Dillon struct file *tfp = *fpp; 1585426da3bcSAlfred Perlstein FILE_LOCK(tfp); 1586cd72f218SMatthew Dillon if (tfp->f_type == DTYPE_SOCKET && 158748e3128bSMatthew Dillon tfp->f_data != NULL) { 1588426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 158948e3128bSMatthew Dillon sorflush(tfp->f_data); 1590e5aeaa0cSDag-Erling Smørgrav } else { 1591426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 15921c7c3c6aSMatthew Dillon } 1593e5aeaa0cSDag-Erling Smørgrav } 1594df8bae1dSRodney W. Grimes for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) 1595b40ce416SJulian Elischer closef(*fpp, (struct thread *) NULL); 1596210a5a71SAlfred Perlstein free(extra_ref, M_TEMP); 1597df8bae1dSRodney W. Grimes unp_gcing = 0; 1598df8bae1dSRodney W. Grimes } 1599df8bae1dSRodney W. Grimes 160026f9a767SRodney W. Grimes void 1601df8bae1dSRodney W. Grimes unp_dispose(m) 1602df8bae1dSRodney W. Grimes struct mbuf *m; 1603df8bae1dSRodney W. Grimes { 1604996c772fSJohn Dyson 1605df8bae1dSRodney W. Grimes if (m) 1606df8bae1dSRodney W. Grimes unp_scan(m, unp_discard); 1607df8bae1dSRodney W. Grimes } 1608df8bae1dSRodney W. Grimes 16090c1bb4fbSDima Dorfman static int 16106f105b34SJohn Baldwin unp_listen(unp, td) 16110c1bb4fbSDima Dorfman struct unpcb *unp; 16126f105b34SJohn Baldwin struct thread *td; 16130c1bb4fbSDima Dorfman { 16140d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 16150c1bb4fbSDima Dorfman 16160d9ce3a1SRobert Watson /* 16170d9ce3a1SRobert Watson * XXXRW: Why populate the local peer cred with our own credential? 16180d9ce3a1SRobert Watson */ 16196f105b34SJohn Baldwin cru2x(td->td_ucred, &unp->unp_peercred); 16200c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPCCACHED; 16210c1bb4fbSDima Dorfman return (0); 16220c1bb4fbSDima Dorfman } 16230c1bb4fbSDima Dorfman 1624f708ef1bSPoul-Henning Kamp static void 1625df8bae1dSRodney W. Grimes unp_scan(m0, op) 1626df8bae1dSRodney W. Grimes register struct mbuf *m0; 16274d77a549SAlfred Perlstein void (*op)(struct file *); 1628df8bae1dSRodney W. Grimes { 16292bc21ed9SDavid Malone struct mbuf *m; 16302bc21ed9SDavid Malone struct file **rp; 16312bc21ed9SDavid Malone struct cmsghdr *cm; 16322bc21ed9SDavid Malone void *data; 16332bc21ed9SDavid Malone int i; 16342bc21ed9SDavid Malone socklen_t clen, datalen; 1635df8bae1dSRodney W. Grimes int qfds; 1636df8bae1dSRodney W. Grimes 1637fc3fcacfSRobert Watson while (m0 != NULL) { 16382bc21ed9SDavid Malone for (m = m0; m; m = m->m_next) { 163912396bdcSDavid Malone if (m->m_type != MT_CONTROL) 1640df8bae1dSRodney W. Grimes continue; 16412bc21ed9SDavid Malone 16422bc21ed9SDavid Malone cm = mtod(m, struct cmsghdr *); 16432bc21ed9SDavid Malone clen = m->m_len; 16442bc21ed9SDavid Malone 16452bc21ed9SDavid Malone while (cm != NULL) { 16462bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) 16472bc21ed9SDavid Malone break; 16482bc21ed9SDavid Malone 16492bc21ed9SDavid Malone data = CMSG_DATA(cm); 16502bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len 16512bc21ed9SDavid Malone - (caddr_t)data; 16522bc21ed9SDavid Malone 16532bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET && 16542bc21ed9SDavid Malone cm->cmsg_type == SCM_RIGHTS) { 16552bc21ed9SDavid Malone qfds = datalen / sizeof (struct file *); 16562bc21ed9SDavid Malone rp = data; 1657df8bae1dSRodney W. Grimes for (i = 0; i < qfds; i++) 1658df8bae1dSRodney W. Grimes (*op)(*rp++); 16592bc21ed9SDavid Malone } 16602bc21ed9SDavid Malone 16612bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 16622bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 16632bc21ed9SDavid Malone cm = (struct cmsghdr *) 16642bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 16652bc21ed9SDavid Malone } else { 16662bc21ed9SDavid Malone clen = 0; 16672bc21ed9SDavid Malone cm = NULL; 16682bc21ed9SDavid Malone } 16692bc21ed9SDavid Malone } 1670df8bae1dSRodney W. Grimes } 1671df8bae1dSRodney W. Grimes m0 = m0->m_act; 1672df8bae1dSRodney W. Grimes } 1673df8bae1dSRodney W. Grimes } 1674df8bae1dSRodney W. Grimes 1675f708ef1bSPoul-Henning Kamp static void 1676df8bae1dSRodney W. Grimes unp_mark(fp) 1677df8bae1dSRodney W. Grimes struct file *fp; 1678df8bae1dSRodney W. Grimes { 1679426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) 1680df8bae1dSRodney W. Grimes return; 1681df8bae1dSRodney W. Grimes unp_defer++; 1682426da3bcSAlfred Perlstein fp->f_gcflag |= (FMARK|FDEFER); 1683df8bae1dSRodney W. Grimes } 1684df8bae1dSRodney W. Grimes 1685f708ef1bSPoul-Henning Kamp static void 1686df8bae1dSRodney W. Grimes unp_discard(fp) 1687df8bae1dSRodney W. Grimes struct file *fp; 1688df8bae1dSRodney W. Grimes { 1689426da3bcSAlfred Perlstein FILE_LOCK(fp); 1690df8bae1dSRodney W. Grimes fp->f_msgcount--; 1691df8bae1dSRodney W. Grimes unp_rights--; 1692426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1693b40ce416SJulian Elischer (void) closef(fp, (struct thread *)NULL); 1694df8bae1dSRodney W. Grimes } 1695