1df8bae1dSRodney W. Grimes /* 2ce5f32deSRobert Watson * Copyright 2004 Robert N. M. Watson 3df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1991, 1993 4df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 5df8bae1dSRodney W. Grimes * 6df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 7df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 8df8bae1dSRodney W. Grimes * are met: 9df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 10df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 11df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 12df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 13df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 14df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 15df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 16df8bae1dSRodney W. Grimes * without specific prior written permission. 17df8bae1dSRodney W. Grimes * 18df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28df8bae1dSRodney W. Grimes * SUCH DAMAGE. 29df8bae1dSRodney W. Grimes * 30748e0b0aSGarrett Wollman * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94 31df8bae1dSRodney W. Grimes */ 32df8bae1dSRodney W. Grimes 33677b542eSDavid E. O'Brien #include <sys/cdefs.h> 34677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 35677b542eSDavid E. O'Brien 36335654d7SRobert Watson #include "opt_mac.h" 37335654d7SRobert Watson 38df8bae1dSRodney W. Grimes #include <sys/param.h> 39fb919e4dSMark Murray #include <sys/domain.h> 40960ed29cSSeigo Tanimura #include <sys/fcntl.h> 41d826c479SBruce Evans #include <sys/malloc.h> /* XXX must be before <sys/file.h> */ 42639acc13SGarrett Wollman #include <sys/file.h> 43960ed29cSSeigo Tanimura #include <sys/filedesc.h> 44960ed29cSSeigo Tanimura #include <sys/jail.h> 45960ed29cSSeigo Tanimura #include <sys/kernel.h> 46960ed29cSSeigo Tanimura #include <sys/lock.h> 476ea48a90SRobert Watson #include <sys/mac.h> 48639acc13SGarrett Wollman #include <sys/mbuf.h> 49960ed29cSSeigo Tanimura #include <sys/mutex.h> 50639acc13SGarrett Wollman #include <sys/namei.h> 51639acc13SGarrett Wollman #include <sys/proc.h> 52df8bae1dSRodney W. Grimes #include <sys/protosw.h> 53960ed29cSSeigo Tanimura #include <sys/resourcevar.h> 54df8bae1dSRodney W. Grimes #include <sys/socket.h> 55df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 56960ed29cSSeigo Tanimura #include <sys/signalvar.h> 57df8bae1dSRodney W. Grimes #include <sys/stat.h> 58960ed29cSSeigo Tanimura #include <sys/sx.h> 59639acc13SGarrett Wollman #include <sys/sysctl.h> 60960ed29cSSeigo Tanimura #include <sys/systm.h> 61639acc13SGarrett Wollman #include <sys/un.h> 6298271db4SGarrett Wollman #include <sys/unpcb.h> 63639acc13SGarrett Wollman #include <sys/vnode.h> 64df8bae1dSRodney W. Grimes 659e9d298aSJeff Roberson #include <vm/uma.h> 6698271db4SGarrett Wollman 679e9d298aSJeff Roberson static uma_zone_t unp_zone; 6898271db4SGarrett Wollman static unp_gen_t unp_gencnt; 6998271db4SGarrett Wollman static u_int unp_count; 7098271db4SGarrett Wollman 7198271db4SGarrett Wollman static struct unp_head unp_shead, unp_dhead; 7298271db4SGarrett Wollman 73df8bae1dSRodney W. Grimes /* 74df8bae1dSRodney W. Grimes * Unix communications domain. 75df8bae1dSRodney W. Grimes * 76df8bae1dSRodney W. Grimes * TODO: 77df8bae1dSRodney W. Grimes * SEQPACKET, RDM 78df8bae1dSRodney W. Grimes * rethink name space problems 79df8bae1dSRodney W. Grimes * need a proper out-of-band 8098271db4SGarrett Wollman * lock pushdown 81df8bae1dSRodney W. Grimes */ 82e7dd9a10SRobert Watson static const struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 83f708ef1bSPoul-Henning Kamp static ino_t unp_ino; /* prototype for fake inode numbers */ 84f708ef1bSPoul-Henning Kamp 85ce5f32deSRobert Watson /* 86ce5f32deSRobert Watson * Currently, UNIX domain sockets are protected by a single subsystem lock, 87ce5f32deSRobert Watson * which covers global data structures and variables, the contents of each 88ce5f32deSRobert Watson * per-socket unpcb structure, and the so_pcb field in sockets attached to 89ce5f32deSRobert Watson * the UNIX domain. This provides for a moderate degree of paralellism, as 90ce5f32deSRobert Watson * receive operations on UNIX domain sockets do not need to acquire the 91ce5f32deSRobert Watson * subsystem lock. Finer grained locking to permit send() without acquiring 92ce5f32deSRobert Watson * a global lock would be a logical next step. 93ce5f32deSRobert Watson * 94ce5f32deSRobert Watson * The UNIX domain socket lock preceds all socket layer locks, including the 95ce5f32deSRobert Watson * socket lock and socket buffer lock, permitting UNIX domain socket code to 96ce5f32deSRobert Watson * call into socket support routines without releasing its locks. 97ce5f32deSRobert Watson * 98ce5f32deSRobert Watson * Some caution is required in areas where the UNIX domain socket code enters 99ce5f32deSRobert Watson * VFS in order to create or find rendezvous points. This results in 100ce5f32deSRobert Watson * dropping of the UNIX domain socket subsystem lock, acquisition of the 101ce5f32deSRobert Watson * Giant lock, and potential sleeping. This increases the chances of races, 102ce5f32deSRobert Watson * and exposes weaknesses in the socket->protocol API by offering poor 103ce5f32deSRobert Watson * failure modes. 104ce5f32deSRobert Watson */ 1050d9ce3a1SRobert Watson static struct mtx unp_mtx; 1060d9ce3a1SRobert Watson #define UNP_LOCK_INIT() \ 1070d9ce3a1SRobert Watson mtx_init(&unp_mtx, "unp", NULL, MTX_DEF) 1080d9ce3a1SRobert Watson #define UNP_LOCK() mtx_lock(&unp_mtx) 1090d9ce3a1SRobert Watson #define UNP_UNLOCK() mtx_unlock(&unp_mtx) 1100d9ce3a1SRobert Watson #define UNP_LOCK_ASSERT() mtx_assert(&unp_mtx, MA_OWNED) 1110d9ce3a1SRobert Watson 1124d77a549SAlfred Perlstein static int unp_attach(struct socket *); 1134d77a549SAlfred Perlstein static void unp_detach(struct unpcb *); 1144d77a549SAlfred Perlstein static int unp_bind(struct unpcb *,struct sockaddr *, struct thread *); 11570f52b48SBruce Evans static int unp_connect(struct socket *,struct sockaddr *, struct thread *); 116db48c0d2SRobert Watson static int unp_connect2(struct socket *so, struct socket *so2); 1174d77a549SAlfred Perlstein static void unp_disconnect(struct unpcb *); 1184d77a549SAlfred Perlstein static void unp_shutdown(struct unpcb *); 1194d77a549SAlfred Perlstein static void unp_drop(struct unpcb *, int); 1204d77a549SAlfred Perlstein static void unp_gc(void); 1214d77a549SAlfred Perlstein static void unp_scan(struct mbuf *, void (*)(struct file *)); 1224d77a549SAlfred Perlstein static void unp_mark(struct file *); 1234d77a549SAlfred Perlstein static void unp_discard(struct file *); 1244d77a549SAlfred Perlstein static void unp_freerights(struct file **, int); 1254d77a549SAlfred Perlstein static int unp_internalize(struct mbuf **, struct thread *); 1264d77a549SAlfred Perlstein static int unp_listen(struct unpcb *, struct thread *); 127f708ef1bSPoul-Henning Kamp 128a29f300eSGarrett Wollman static int 129a29f300eSGarrett Wollman uipc_abort(struct socket *so) 130df8bae1dSRodney W. Grimes { 131df8bae1dSRodney W. Grimes struct unpcb *unp = sotounpcb(so); 132df8bae1dSRodney W. Grimes 133fc3fcacfSRobert Watson if (unp == NULL) 134e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 1350d9ce3a1SRobert Watson UNP_LOCK(); 136a29f300eSGarrett Wollman unp_drop(unp, ECONNABORTED); 1370d9ce3a1SRobert Watson unp_detach(unp); /* NB: unlocks */ 138395a08c9SRobert Watson SOCK_LOCK(so); 139ddb7d629SIan Dowse sotryfree(so); 140e5aeaa0cSDag-Erling Smørgrav return (0); 141df8bae1dSRodney W. Grimes } 142df8bae1dSRodney W. Grimes 143a29f300eSGarrett Wollman static int 14457bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam) 145a29f300eSGarrett Wollman { 146a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 1470d9ce3a1SRobert Watson const struct sockaddr *sa; 148df8bae1dSRodney W. Grimes 149fc3fcacfSRobert Watson if (unp == NULL) 150e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 151df8bae1dSRodney W. Grimes 152df8bae1dSRodney W. Grimes /* 153df8bae1dSRodney W. Grimes * Pass back name of connected socket, 154df8bae1dSRodney W. Grimes * if it was bound and we are still connected 155df8bae1dSRodney W. Grimes * (our peer may have closed already!). 156df8bae1dSRodney W. Grimes */ 1570d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 1580d9ce3a1SRobert Watson UNP_LOCK(); 1590d9ce3a1SRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr != NULL) 1600d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 1610d9ce3a1SRobert Watson else 1620d9ce3a1SRobert Watson sa = &sun_noname; 1630d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 1640d9ce3a1SRobert Watson UNP_UNLOCK(); 165e5aeaa0cSDag-Erling Smørgrav return (0); 166a29f300eSGarrett Wollman } 167df8bae1dSRodney W. Grimes 168a29f300eSGarrett Wollman static int 169b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td) 170a29f300eSGarrett Wollman { 171a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 172df8bae1dSRodney W. Grimes 173fc3fcacfSRobert Watson if (unp != NULL) 174e5aeaa0cSDag-Erling Smørgrav return (EISCONN); 175e5aeaa0cSDag-Erling Smørgrav return (unp_attach(so)); 176a29f300eSGarrett Wollman } 177a29f300eSGarrett Wollman 178a29f300eSGarrett Wollman static int 179b40ce416SJulian Elischer uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 180a29f300eSGarrett Wollman { 181a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 182a29f300eSGarrett Wollman 183fc3fcacfSRobert Watson if (unp == NULL) 184e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 185a29f300eSGarrett Wollman 186e5aeaa0cSDag-Erling Smørgrav return (unp_bind(unp, nam, td)); 187a29f300eSGarrett Wollman } 188a29f300eSGarrett Wollman 189a29f300eSGarrett Wollman static int 190b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 191a29f300eSGarrett Wollman { 192b295bdcdSRobert Watson struct unpcb *unp; 1930d9ce3a1SRobert Watson int error; 194a29f300eSGarrett Wollman 195fd179ee9SRobert Watson KASSERT(td == curthread, ("uipc_connect: td != curthread")); 196fd179ee9SRobert Watson 1970d9ce3a1SRobert Watson UNP_LOCK(); 198b295bdcdSRobert Watson unp = sotounpcb(so); 199b295bdcdSRobert Watson if (unp == NULL) { 200b295bdcdSRobert Watson error = EINVAL; 201b295bdcdSRobert Watson goto out; 202b295bdcdSRobert Watson } 203fd179ee9SRobert Watson error = unp_connect(so, nam, td); 204b295bdcdSRobert Watson out: 2050d9ce3a1SRobert Watson UNP_UNLOCK(); 2060d9ce3a1SRobert Watson return (error); 207a29f300eSGarrett Wollman } 208a29f300eSGarrett Wollman 209db48c0d2SRobert Watson int 210a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2) 211a29f300eSGarrett Wollman { 212a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so1); 2130d9ce3a1SRobert Watson int error; 214a29f300eSGarrett Wollman 215fc3fcacfSRobert Watson if (unp == NULL) 216e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 217a29f300eSGarrett Wollman 2180d9ce3a1SRobert Watson UNP_LOCK(); 2190d9ce3a1SRobert Watson error = unp_connect2(so1, so2); 2200d9ce3a1SRobert Watson UNP_UNLOCK(); 2210d9ce3a1SRobert Watson return (error); 222a29f300eSGarrett Wollman } 223a29f300eSGarrett Wollman 224a29f300eSGarrett Wollman /* control is EOPNOTSUPP */ 225a29f300eSGarrett Wollman 226a29f300eSGarrett Wollman static int 227a29f300eSGarrett Wollman uipc_detach(struct socket *so) 228a29f300eSGarrett Wollman { 229a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 230a29f300eSGarrett Wollman 231fc3fcacfSRobert Watson if (unp == NULL) 232e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 233a29f300eSGarrett Wollman 2340d9ce3a1SRobert Watson UNP_LOCK(); 2350d9ce3a1SRobert Watson unp_detach(unp); /* NB: unlocks unp */ 236e5aeaa0cSDag-Erling Smørgrav return (0); 237a29f300eSGarrett Wollman } 238a29f300eSGarrett Wollman 239a29f300eSGarrett Wollman static int 240a29f300eSGarrett Wollman uipc_disconnect(struct socket *so) 241a29f300eSGarrett Wollman { 242a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 243a29f300eSGarrett Wollman 244fc3fcacfSRobert Watson if (unp == NULL) 245e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 2460d9ce3a1SRobert Watson UNP_LOCK(); 247a29f300eSGarrett Wollman unp_disconnect(unp); 2480d9ce3a1SRobert Watson UNP_UNLOCK(); 249e5aeaa0cSDag-Erling Smørgrav return (0); 250a29f300eSGarrett Wollman } 251a29f300eSGarrett Wollman 252a29f300eSGarrett Wollman static int 253b40ce416SJulian Elischer uipc_listen(struct socket *so, struct thread *td) 254a29f300eSGarrett Wollman { 255a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 2560d9ce3a1SRobert Watson int error; 257a29f300eSGarrett Wollman 258fc3fcacfSRobert Watson if (unp == NULL || unp->unp_vnode == NULL) 259e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 2600d9ce3a1SRobert Watson UNP_LOCK(); 2610d9ce3a1SRobert Watson error = unp_listen(unp, td); 2620d9ce3a1SRobert Watson UNP_UNLOCK(); 2630d9ce3a1SRobert Watson return (error); 264a29f300eSGarrett Wollman } 265a29f300eSGarrett Wollman 266a29f300eSGarrett Wollman static int 26757bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 268a29f300eSGarrett Wollman { 269a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 2700d9ce3a1SRobert Watson const struct sockaddr *sa; 271a29f300eSGarrett Wollman 272fc3fcacfSRobert Watson if (unp == NULL) 273e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 2740d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 2750d9ce3a1SRobert Watson UNP_LOCK(); 276fc3fcacfSRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr!= NULL) 2770d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_conn->unp_addr; 278bdc5f6a3SHajimu UMEMOTO else { 279bdc5f6a3SHajimu UMEMOTO /* 280bdc5f6a3SHajimu UMEMOTO * XXX: It seems that this test always fails even when 281bdc5f6a3SHajimu UMEMOTO * connection is established. So, this else clause is 282bdc5f6a3SHajimu UMEMOTO * added as workaround to return PF_LOCAL sockaddr. 283bdc5f6a3SHajimu UMEMOTO */ 2840d9ce3a1SRobert Watson sa = &sun_noname; 285bdc5f6a3SHajimu UMEMOTO } 2860d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 2870d9ce3a1SRobert Watson UNP_UNLOCK(); 288e5aeaa0cSDag-Erling Smørgrav return (0); 289a29f300eSGarrett Wollman } 290a29f300eSGarrett Wollman 291a29f300eSGarrett Wollman static int 292a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 293a29f300eSGarrett Wollman { 294a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 295a29f300eSGarrett Wollman struct socket *so2; 2966aef685fSBrian Feldman u_long newhiwat; 297a29f300eSGarrett Wollman 298fc3fcacfSRobert Watson if (unp == NULL) 299e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 3000d9ce3a1SRobert Watson UNP_LOCK(); 301df8bae1dSRodney W. Grimes switch (so->so_type) { 302df8bae1dSRodney W. Grimes case SOCK_DGRAM: 303a29f300eSGarrett Wollman panic("uipc_rcvd DGRAM?"); 304df8bae1dSRodney W. Grimes /*NOTREACHED*/ 305df8bae1dSRodney W. Grimes 306df8bae1dSRodney W. Grimes case SOCK_STREAM: 307fc3fcacfSRobert Watson if (unp->unp_conn == NULL) 308df8bae1dSRodney W. Grimes break; 309df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 310c9f69064SRobert Watson SOCKBUF_LOCK(&so2->so_snd); 311c9f69064SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 312df8bae1dSRodney W. Grimes /* 313df8bae1dSRodney W. Grimes * Adjust backpressure on sender 314df8bae1dSRodney W. Grimes * and wakeup any waiting to write. 315df8bae1dSRodney W. Grimes */ 316ff8b0106SBrian Feldman so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt; 317ff8b0106SBrian Feldman unp->unp_mbcnt = so->so_rcv.sb_mbcnt; 3186aef685fSBrian Feldman newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - 3196aef685fSBrian Feldman so->so_rcv.sb_cc; 320f535380cSDon Lewis (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat, 3216aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 322ff8b0106SBrian Feldman unp->unp_cc = so->so_rcv.sb_cc; 323c9f69064SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 3241e4d7da7SRobert Watson sowwakeup_locked(so2); 325df8bae1dSRodney W. Grimes break; 326df8bae1dSRodney W. Grimes 327df8bae1dSRodney W. Grimes default: 328a29f300eSGarrett Wollman panic("uipc_rcvd unknown socktype"); 329df8bae1dSRodney W. Grimes } 3300d9ce3a1SRobert Watson UNP_UNLOCK(); 331e5aeaa0cSDag-Erling Smørgrav return (0); 332a29f300eSGarrett Wollman } 333df8bae1dSRodney W. Grimes 334a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 335a29f300eSGarrett Wollman 336a29f300eSGarrett Wollman static int 33757bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 338b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 339a29f300eSGarrett Wollman { 340a29f300eSGarrett Wollman int error = 0; 341a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 342a29f300eSGarrett Wollman struct socket *so2; 3436aef685fSBrian Feldman u_long newhiwat; 344a29f300eSGarrett Wollman 345fc3fcacfSRobert Watson if (unp == NULL) { 346a29f300eSGarrett Wollman error = EINVAL; 347a29f300eSGarrett Wollman goto release; 348a29f300eSGarrett Wollman } 349a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 350a29f300eSGarrett Wollman error = EOPNOTSUPP; 351a29f300eSGarrett Wollman goto release; 352a29f300eSGarrett Wollman } 353a29f300eSGarrett Wollman 354fc3fcacfSRobert Watson if (control != NULL && (error = unp_internalize(&control, td))) 355a29f300eSGarrett Wollman goto release; 356df8bae1dSRodney W. Grimes 3570d9ce3a1SRobert Watson UNP_LOCK(); 358a29f300eSGarrett Wollman switch (so->so_type) { 359a29f300eSGarrett Wollman case SOCK_DGRAM: 360a29f300eSGarrett Wollman { 361e7dd9a10SRobert Watson const struct sockaddr *from; 362df8bae1dSRodney W. Grimes 363fc3fcacfSRobert Watson if (nam != NULL) { 364fc3fcacfSRobert Watson if (unp->unp_conn != NULL) { 365df8bae1dSRodney W. Grimes error = EISCONN; 366df8bae1dSRodney W. Grimes break; 367df8bae1dSRodney W. Grimes } 368b40ce416SJulian Elischer error = unp_connect(so, nam, td); 369df8bae1dSRodney W. Grimes if (error) 370df8bae1dSRodney W. Grimes break; 371df8bae1dSRodney W. Grimes } else { 372fc3fcacfSRobert Watson if (unp->unp_conn == NULL) { 373df8bae1dSRodney W. Grimes error = ENOTCONN; 374df8bae1dSRodney W. Grimes break; 375df8bae1dSRodney W. Grimes } 376df8bae1dSRodney W. Grimes } 377df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 378fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 37957bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 380df8bae1dSRodney W. Grimes else 381df8bae1dSRodney W. Grimes from = &sun_noname; 382a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 383a34b7046SRobert Watson if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) { 3841e4d7da7SRobert Watson sorwakeup_locked(so2); 385fc3fcacfSRobert Watson m = NULL; 386fc3fcacfSRobert Watson control = NULL; 387e5aeaa0cSDag-Erling Smørgrav } else { 388a34b7046SRobert Watson SOCKBUF_UNLOCK(&so2->so_rcv); 389df8bae1dSRodney W. Grimes error = ENOBUFS; 390e5aeaa0cSDag-Erling Smørgrav } 391fc3fcacfSRobert Watson if (nam != NULL) 392df8bae1dSRodney W. Grimes unp_disconnect(unp); 393df8bae1dSRodney W. Grimes break; 394df8bae1dSRodney W. Grimes } 395df8bae1dSRodney W. Grimes 396df8bae1dSRodney W. Grimes case SOCK_STREAM: 3976b8fda4dSGarrett Wollman /* Connect if not connected yet. */ 3986b8fda4dSGarrett Wollman /* 3996b8fda4dSGarrett Wollman * Note: A better implementation would complain 400402cc72dSDavid Greenman * if not equal to the peer's address. 4016b8fda4dSGarrett Wollman */ 402402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 403fc3fcacfSRobert Watson if (nam != NULL) { 404b40ce416SJulian Elischer error = unp_connect(so, nam, td); 405402cc72dSDavid Greenman if (error) 4066b8fda4dSGarrett Wollman break; /* XXX */ 407402cc72dSDavid Greenman } else { 408402cc72dSDavid Greenman error = ENOTCONN; 409402cc72dSDavid Greenman break; 410402cc72dSDavid Greenman } 411402cc72dSDavid Greenman } 412402cc72dSDavid Greenman 413c0b99ffaSRobert Watson if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 414df8bae1dSRodney W. Grimes error = EPIPE; 415df8bae1dSRodney W. Grimes break; 416df8bae1dSRodney W. Grimes } 417fc3fcacfSRobert Watson if (unp->unp_conn == NULL) 418a29f300eSGarrett Wollman panic("uipc_send connected but no connection?"); 419df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 420a34b7046SRobert Watson SOCKBUF_LOCK(&so2->so_rcv); 421df8bae1dSRodney W. Grimes /* 422df8bae1dSRodney W. Grimes * Send to paired receive port, and then reduce 423df8bae1dSRodney W. Grimes * send buffer hiwater marks to maintain backpressure. 424df8bae1dSRodney W. Grimes * Wake up readers. 425df8bae1dSRodney W. Grimes */ 426fc3fcacfSRobert Watson if (control != NULL) { 427a34b7046SRobert Watson if (sbappendcontrol_locked(&so2->so_rcv, m, control)) 428fc3fcacfSRobert Watson control = NULL; 429e5aeaa0cSDag-Erling Smørgrav } else { 430a34b7046SRobert Watson sbappend_locked(&so2->so_rcv, m); 431e5aeaa0cSDag-Erling Smørgrav } 432ff8b0106SBrian Feldman so->so_snd.sb_mbmax -= 433ff8b0106SBrian Feldman so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt; 434ff8b0106SBrian Feldman unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt; 4356aef685fSBrian Feldman newhiwat = so->so_snd.sb_hiwat - 4366aef685fSBrian Feldman (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc); 437f535380cSDon Lewis (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat, 4386aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 439ff8b0106SBrian Feldman unp->unp_conn->unp_cc = so2->so_rcv.sb_cc; 4401e4d7da7SRobert Watson sorwakeup_locked(so2); 441fc3fcacfSRobert Watson m = NULL; 442df8bae1dSRodney W. Grimes break; 443df8bae1dSRodney W. Grimes 444df8bae1dSRodney W. Grimes default: 445a29f300eSGarrett Wollman panic("uipc_send unknown socktype"); 446df8bae1dSRodney W. Grimes } 447a29f300eSGarrett Wollman 4486b8fda4dSGarrett Wollman /* 4496b8fda4dSGarrett Wollman * SEND_EOF is equivalent to a SEND followed by 4506b8fda4dSGarrett Wollman * a SHUTDOWN. 4516b8fda4dSGarrett Wollman */ 452a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 4536b8fda4dSGarrett Wollman socantsendmore(so); 4546b8fda4dSGarrett Wollman unp_shutdown(unp); 4556b8fda4dSGarrett Wollman } 4560d9ce3a1SRobert Watson UNP_UNLOCK(); 457df8bae1dSRodney W. Grimes 458fc3fcacfSRobert Watson if (control != NULL && error != 0) 459bd508d39SDon Lewis unp_dispose(control); 460bd508d39SDon Lewis 461a29f300eSGarrett Wollman release: 462fc3fcacfSRobert Watson if (control != NULL) 463a29f300eSGarrett Wollman m_freem(control); 464fc3fcacfSRobert Watson if (m != NULL) 465a29f300eSGarrett Wollman m_freem(m); 466e5aeaa0cSDag-Erling Smørgrav return (error); 467a29f300eSGarrett Wollman } 468df8bae1dSRodney W. Grimes 469a29f300eSGarrett Wollman static int 470a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 471a29f300eSGarrett Wollman { 472a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 473a29f300eSGarrett Wollman struct socket *so2; 474a29f300eSGarrett Wollman 475fc3fcacfSRobert Watson if (unp == NULL) 476e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 4770d9ce3a1SRobert Watson UNP_LOCK(); 478a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 479fc3fcacfSRobert Watson if (so->so_type == SOCK_STREAM && unp->unp_conn != NULL) { 480df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 481a29f300eSGarrett Wollman sb->st_blksize += so2->so_rcv.sb_cc; 482df8bae1dSRodney W. Grimes } 483f3732fd1SPoul-Henning Kamp sb->st_dev = NODEV; 484df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 4856f782c46SJeffrey Hsu unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 486a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 4870d9ce3a1SRobert Watson UNP_UNLOCK(); 488df8bae1dSRodney W. Grimes return (0); 489a29f300eSGarrett Wollman } 490df8bae1dSRodney W. Grimes 491a29f300eSGarrett Wollman static int 492a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 493a29f300eSGarrett Wollman { 494a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 495df8bae1dSRodney W. Grimes 496fc3fcacfSRobert Watson if (unp == NULL) 497e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 4980d9ce3a1SRobert Watson UNP_LOCK(); 499a29f300eSGarrett Wollman socantsendmore(so); 500a29f300eSGarrett Wollman unp_shutdown(unp); 5010d9ce3a1SRobert Watson UNP_UNLOCK(); 502e5aeaa0cSDag-Erling Smørgrav return (0); 503a29f300eSGarrett Wollman } 504df8bae1dSRodney W. Grimes 505a29f300eSGarrett Wollman static int 50657bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 507a29f300eSGarrett Wollman { 508a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 5090d9ce3a1SRobert Watson const struct sockaddr *sa; 510a29f300eSGarrett Wollman 511fc3fcacfSRobert Watson if (unp == NULL) 512e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 5130d9ce3a1SRobert Watson *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 5140d9ce3a1SRobert Watson UNP_LOCK(); 515fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 5160d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_addr; 51783f3198bSThomas Moestl else 5180d9ce3a1SRobert Watson sa = &sun_noname; 5190d9ce3a1SRobert Watson bcopy(sa, *nam, sa->sa_len); 5200d9ce3a1SRobert Watson UNP_UNLOCK(); 521e5aeaa0cSDag-Erling Smørgrav return (0); 522df8bae1dSRodney W. Grimes } 523a29f300eSGarrett Wollman 524a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = { 525a29f300eSGarrett Wollman uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect, 526a29f300eSGarrett Wollman uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect, 527a29f300eSGarrett Wollman uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp, 528a29f300eSGarrett Wollman uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr, 529a557af22SRobert Watson sosend, soreceive, sopoll, pru_sosetlabel_null 530a29f300eSGarrett Wollman }; 531df8bae1dSRodney W. Grimes 5320c1bb4fbSDima Dorfman int 5330c1bb4fbSDima Dorfman uipc_ctloutput(so, sopt) 5340c1bb4fbSDima Dorfman struct socket *so; 5350c1bb4fbSDima Dorfman struct sockopt *sopt; 5360c1bb4fbSDima Dorfman { 5370c1bb4fbSDima Dorfman struct unpcb *unp = sotounpcb(so); 5380d9ce3a1SRobert Watson struct xucred xu; 5390c1bb4fbSDima Dorfman int error; 5400c1bb4fbSDima Dorfman 5410c1bb4fbSDima Dorfman switch (sopt->sopt_dir) { 5420c1bb4fbSDima Dorfman case SOPT_GET: 5430c1bb4fbSDima Dorfman switch (sopt->sopt_name) { 5440c1bb4fbSDima Dorfman case LOCAL_PEERCRED: 5450d9ce3a1SRobert Watson error = 0; 5460d9ce3a1SRobert Watson UNP_LOCK(); 5470c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC) 5480d9ce3a1SRobert Watson xu = unp->unp_peercred; 5490c1bb4fbSDima Dorfman else { 5500c1bb4fbSDima Dorfman if (so->so_type == SOCK_STREAM) 5510c1bb4fbSDima Dorfman error = ENOTCONN; 5520c1bb4fbSDima Dorfman else 5530c1bb4fbSDima Dorfman error = EINVAL; 5540c1bb4fbSDima Dorfman } 5550d9ce3a1SRobert Watson UNP_UNLOCK(); 5560d9ce3a1SRobert Watson if (error == 0) 5570d9ce3a1SRobert Watson error = sooptcopyout(sopt, &xu, sizeof(xu)); 5580c1bb4fbSDima Dorfman break; 5590c1bb4fbSDima Dorfman default: 5600c1bb4fbSDima Dorfman error = EOPNOTSUPP; 5610c1bb4fbSDima Dorfman break; 5620c1bb4fbSDima Dorfman } 5630c1bb4fbSDima Dorfman break; 5640c1bb4fbSDima Dorfman case SOPT_SET: 5650c1bb4fbSDima Dorfman default: 5660c1bb4fbSDima Dorfman error = EOPNOTSUPP; 5670c1bb4fbSDima Dorfman break; 5680c1bb4fbSDima Dorfman } 5690c1bb4fbSDima Dorfman return (error); 5700c1bb4fbSDima Dorfman } 5710c1bb4fbSDima Dorfman 572df8bae1dSRodney W. Grimes /* 573df8bae1dSRodney W. Grimes * Both send and receive buffers are allocated PIPSIZ bytes of buffering 574df8bae1dSRodney W. Grimes * for stream sockets, although the total for sender and receiver is 575df8bae1dSRodney W. Grimes * actually only PIPSIZ. 576df8bae1dSRodney W. Grimes * Datagram sockets really use the sendspace as the maximum datagram size, 577df8bae1dSRodney W. Grimes * and don't really want to reserve the sendspace. Their recvspace should 578df8bae1dSRodney W. Grimes * be large enough for at least one max-size datagram plus address. 579df8bae1dSRodney W. Grimes */ 5805dce41c5SJohn Dyson #ifndef PIPSIZ 5815dce41c5SJohn Dyson #define PIPSIZ 8192 5825dce41c5SJohn Dyson #endif 583f708ef1bSPoul-Henning Kamp static u_long unpst_sendspace = PIPSIZ; 584f708ef1bSPoul-Henning Kamp static u_long unpst_recvspace = PIPSIZ; 585f708ef1bSPoul-Henning Kamp static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 586f708ef1bSPoul-Henning Kamp static u_long unpdg_recvspace = 4*1024; 587df8bae1dSRodney W. Grimes 588f708ef1bSPoul-Henning Kamp static int unp_rights; /* file descriptors in flight */ 589df8bae1dSRodney W. Grimes 590ce02431fSDoug Rabson SYSCTL_DECL(_net_local_stream); 591639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 592639acc13SGarrett Wollman &unpst_sendspace, 0, ""); 593639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 594639acc13SGarrett Wollman &unpst_recvspace, 0, ""); 595ce02431fSDoug Rabson SYSCTL_DECL(_net_local_dgram); 596639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 597639acc13SGarrett Wollman &unpdg_sendspace, 0, ""); 598639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 599639acc13SGarrett Wollman &unpdg_recvspace, 0, ""); 600ce02431fSDoug Rabson SYSCTL_DECL(_net_local); 601639acc13SGarrett Wollman SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); 602639acc13SGarrett Wollman 603f708ef1bSPoul-Henning Kamp static int 604df8bae1dSRodney W. Grimes unp_attach(so) 605df8bae1dSRodney W. Grimes struct socket *so; 606df8bae1dSRodney W. Grimes { 607df8bae1dSRodney W. Grimes register struct unpcb *unp; 608df8bae1dSRodney W. Grimes int error; 609df8bae1dSRodney W. Grimes 610df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 611df8bae1dSRodney W. Grimes switch (so->so_type) { 612df8bae1dSRodney W. Grimes 613df8bae1dSRodney W. Grimes case SOCK_STREAM: 614df8bae1dSRodney W. Grimes error = soreserve(so, unpst_sendspace, unpst_recvspace); 615df8bae1dSRodney W. Grimes break; 616df8bae1dSRodney W. Grimes 617df8bae1dSRodney W. Grimes case SOCK_DGRAM: 618df8bae1dSRodney W. Grimes error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 619df8bae1dSRodney W. Grimes break; 620df8bae1dSRodney W. Grimes 621df8bae1dSRodney W. Grimes default: 622df8bae1dSRodney W. Grimes panic("unp_attach"); 623df8bae1dSRodney W. Grimes } 624df8bae1dSRodney W. Grimes if (error) 625df8bae1dSRodney W. Grimes return (error); 626df8bae1dSRodney W. Grimes } 627a163d034SWarner Losh unp = uma_zalloc(unp_zone, M_WAITOK); 62857bf258eSGarrett Wollman if (unp == NULL) 629df8bae1dSRodney W. Grimes return (ENOBUFS); 63057bf258eSGarrett Wollman bzero(unp, sizeof *unp); 63198271db4SGarrett Wollman LIST_INIT(&unp->unp_refs); 632df8bae1dSRodney W. Grimes unp->unp_socket = so; 6330d9ce3a1SRobert Watson 6340d9ce3a1SRobert Watson UNP_LOCK(); 6350d9ce3a1SRobert Watson unp->unp_gencnt = ++unp_gencnt; 6360d9ce3a1SRobert Watson unp_count++; 63798271db4SGarrett Wollman LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead 63898271db4SGarrett Wollman : &unp_shead, unp, unp_link); 6390d9ce3a1SRobert Watson UNP_UNLOCK(); 6400d9ce3a1SRobert Watson 641210a5a71SAlfred Perlstein so->so_pcb = unp; 642df8bae1dSRodney W. Grimes return (0); 643df8bae1dSRodney W. Grimes } 644df8bae1dSRodney W. Grimes 645f708ef1bSPoul-Henning Kamp static void 646df8bae1dSRodney W. Grimes unp_detach(unp) 647df8bae1dSRodney W. Grimes register struct unpcb *unp; 648df8bae1dSRodney W. Grimes { 6490d9ce3a1SRobert Watson struct vnode *vp; 6500d9ce3a1SRobert Watson 6510d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 6520d9ce3a1SRobert Watson 65398271db4SGarrett Wollman LIST_REMOVE(unp, unp_link); 65498271db4SGarrett Wollman unp->unp_gencnt = ++unp_gencnt; 65598271db4SGarrett Wollman --unp_count; 6560d9ce3a1SRobert Watson if ((vp = unp->unp_vnode) != NULL) { 6570d9ce3a1SRobert Watson /* 6580d9ce3a1SRobert Watson * XXXRW: should v_socket be frobbed only while holding 6590d9ce3a1SRobert Watson * Giant? 6600d9ce3a1SRobert Watson */ 661fc3fcacfSRobert Watson unp->unp_vnode->v_socket = NULL; 662fc3fcacfSRobert Watson unp->unp_vnode = NULL; 663df8bae1dSRodney W. Grimes } 664fc3fcacfSRobert Watson if (unp->unp_conn != NULL) 665df8bae1dSRodney W. Grimes unp_disconnect(unp); 6660d9ce3a1SRobert Watson while (!LIST_EMPTY(&unp->unp_refs)) { 6670d9ce3a1SRobert Watson struct unpcb *ref = LIST_FIRST(&unp->unp_refs); 6680d9ce3a1SRobert Watson unp_drop(ref, ECONNRESET); 6690d9ce3a1SRobert Watson } 670df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 671fc3fcacfSRobert Watson unp->unp_socket->so_pcb = NULL; 672df8bae1dSRodney W. Grimes if (unp_rights) { 673df8bae1dSRodney W. Grimes /* 674df8bae1dSRodney W. Grimes * Normally the receive buffer is flushed later, 675df8bae1dSRodney W. Grimes * in sofree, but if our receive buffer holds references 676df8bae1dSRodney W. Grimes * to descriptors that are now garbage, we will dispose 677df8bae1dSRodney W. Grimes * of those descriptor references after the garbage collector 678df8bae1dSRodney W. Grimes * gets them (resulting in a "panic: closef: count < 0"). 679df8bae1dSRodney W. Grimes */ 680df8bae1dSRodney W. Grimes sorflush(unp->unp_socket); 681df8bae1dSRodney W. Grimes unp_gc(); 682df8bae1dSRodney W. Grimes } 683a5993a97SRobert Watson UNP_UNLOCK(); 684fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 68557bf258eSGarrett Wollman FREE(unp->unp_addr, M_SONAME); 6869e9d298aSJeff Roberson uma_zfree(unp_zone, unp); 6870d9ce3a1SRobert Watson if (vp) { 6880d9ce3a1SRobert Watson mtx_lock(&Giant); 6890d9ce3a1SRobert Watson vrele(vp); 6900d9ce3a1SRobert Watson mtx_unlock(&Giant); 6910d9ce3a1SRobert Watson } 692df8bae1dSRodney W. Grimes } 693df8bae1dSRodney W. Grimes 694f708ef1bSPoul-Henning Kamp static int 695b40ce416SJulian Elischer unp_bind(unp, nam, td) 696df8bae1dSRodney W. Grimes struct unpcb *unp; 69757bf258eSGarrett Wollman struct sockaddr *nam; 698b40ce416SJulian Elischer struct thread *td; 699df8bae1dSRodney W. Grimes { 70057bf258eSGarrett Wollman struct sockaddr_un *soun = (struct sockaddr_un *)nam; 701f2a2857bSKirk McKusick struct vnode *vp; 702f2a2857bSKirk McKusick struct mount *mp; 703df8bae1dSRodney W. Grimes struct vattr vattr; 70457bf258eSGarrett Wollman int error, namelen; 705df8bae1dSRodney W. Grimes struct nameidata nd; 7068f364875SJulian Elischer char *buf; 707df8bae1dSRodney W. Grimes 7080d9ce3a1SRobert Watson /* 7090d9ce3a1SRobert Watson * XXXRW: This test-and-set of unp_vnode is non-atomic; the 7100d9ce3a1SRobert Watson * unlocked read here is fine, but the value of unp_vnode needs 7110d9ce3a1SRobert Watson * to be tested again after we do all the lookups to see if the 7120d9ce3a1SRobert Watson * pcb is still unbound? 7130d9ce3a1SRobert Watson */ 714df8bae1dSRodney W. Grimes if (unp->unp_vnode != NULL) 715df8bae1dSRodney W. Grimes return (EINVAL); 71655c85568SRobert Drehmel 71757bf258eSGarrett Wollman namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 71857bf258eSGarrett Wollman if (namelen <= 0) 719e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 72055c85568SRobert Drehmel 721a163d034SWarner Losh buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 72255c85568SRobert Drehmel strlcpy(buf, soun->sun_path, namelen + 1); 72355c85568SRobert Drehmel 7240d9ce3a1SRobert Watson mtx_lock(&Giant); 725f2a2857bSKirk McKusick restart: 7260d9ce3a1SRobert Watson mtx_assert(&Giant, MA_OWNED); 727b65f6f6bSRobert Watson NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME, UIO_SYSSPACE, 728b40ce416SJulian Elischer buf, td); 729df8bae1dSRodney W. Grimes /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 730797f2d22SPoul-Henning Kamp error = namei(&nd); 7310d9ce3a1SRobert Watson if (error) 7320d9ce3a1SRobert Watson goto done; 733df8bae1dSRodney W. Grimes vp = nd.ni_vp; 734f2a2857bSKirk McKusick if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 735762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 736df8bae1dSRodney W. Grimes if (nd.ni_dvp == vp) 737df8bae1dSRodney W. Grimes vrele(nd.ni_dvp); 738df8bae1dSRodney W. Grimes else 739df8bae1dSRodney W. Grimes vput(nd.ni_dvp); 740f2a2857bSKirk McKusick if (vp != NULL) { 741df8bae1dSRodney W. Grimes vrele(vp); 7420d9ce3a1SRobert Watson error = EADDRINUSE; 7430d9ce3a1SRobert Watson goto done; 744df8bae1dSRodney W. Grimes } 7458f364875SJulian Elischer error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 7460d9ce3a1SRobert Watson if (error) 7470d9ce3a1SRobert Watson goto done; 748f2a2857bSKirk McKusick goto restart; 749f2a2857bSKirk McKusick } 750df8bae1dSRodney W. Grimes VATTR_NULL(&vattr); 751df8bae1dSRodney W. Grimes vattr.va_type = VSOCK; 752b40ce416SJulian Elischer vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 7536ea48a90SRobert Watson #ifdef MAC 7546ea48a90SRobert Watson error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 7556ea48a90SRobert Watson &vattr); 7566151efaaSRobert Watson #endif 7576ea48a90SRobert Watson if (error == 0) { 758a854ed98SJohn Baldwin VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE); 7597be2d300SMike Smith error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 7606ea48a90SRobert Watson } 761762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 7627be2d300SMike Smith vput(nd.ni_dvp); 7630d9ce3a1SRobert Watson if (error) 7640d9ce3a1SRobert Watson goto done; 765df8bae1dSRodney W. Grimes vp = nd.ni_vp; 7660d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_bind"); 7670d9ce3a1SRobert Watson soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 7680d9ce3a1SRobert Watson UNP_LOCK(); 769df8bae1dSRodney W. Grimes vp->v_socket = unp->unp_socket; 770df8bae1dSRodney W. Grimes unp->unp_vnode = vp; 7710d9ce3a1SRobert Watson unp->unp_addr = soun; 7720d9ce3a1SRobert Watson UNP_UNLOCK(); 773b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 774f2a2857bSKirk McKusick vn_finished_write(mp); 7750d9ce3a1SRobert Watson done: 7760d9ce3a1SRobert Watson mtx_unlock(&Giant); 7778f364875SJulian Elischer free(buf, M_TEMP); 7780d9ce3a1SRobert Watson return (error); 779df8bae1dSRodney W. Grimes } 780df8bae1dSRodney W. Grimes 781f708ef1bSPoul-Henning Kamp static int 782b40ce416SJulian Elischer unp_connect(so, nam, td) 783df8bae1dSRodney W. Grimes struct socket *so; 78457bf258eSGarrett Wollman struct sockaddr *nam; 785b40ce416SJulian Elischer struct thread *td; 786df8bae1dSRodney W. Grimes { 78757bf258eSGarrett Wollman register struct sockaddr_un *soun = (struct sockaddr_un *)nam; 788df8bae1dSRodney W. Grimes register struct vnode *vp; 789df8bae1dSRodney W. Grimes register struct socket *so2, *so3; 790b295bdcdSRobert Watson struct unpcb *unp, *unp2, *unp3; 79157bf258eSGarrett Wollman int error, len; 792df8bae1dSRodney W. Grimes struct nameidata nd; 79357bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 7940d9ce3a1SRobert Watson struct sockaddr *sa; 7950d9ce3a1SRobert Watson 7960d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 797b295bdcdSRobert Watson unp = sotounpcb(so); 798df8bae1dSRodney W. Grimes 79957bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 80057bf258eSGarrett Wollman if (len <= 0) 801e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 80255c85568SRobert Drehmel strlcpy(buf, soun->sun_path, len + 1); 8030d9ce3a1SRobert Watson UNP_UNLOCK(); 8040d9ce3a1SRobert Watson sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK); 8050d9ce3a1SRobert Watson mtx_lock(&Giant); 806b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td); 807797f2d22SPoul-Henning Kamp error = namei(&nd); 808797f2d22SPoul-Henning Kamp if (error) 8090d9ce3a1SRobert Watson vp = NULL; 8100d9ce3a1SRobert Watson else 811df8bae1dSRodney W. Grimes vp = nd.ni_vp; 8120d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_connect"); 813762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 8140d9ce3a1SRobert Watson if (error) 8150d9ce3a1SRobert Watson goto bad; 8160d9ce3a1SRobert Watson 817df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 818df8bae1dSRodney W. Grimes error = ENOTSOCK; 819df8bae1dSRodney W. Grimes goto bad; 820df8bae1dSRodney W. Grimes } 821a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 822797f2d22SPoul-Henning Kamp if (error) 823df8bae1dSRodney W. Grimes goto bad; 8242260c03dSRobert Watson mtx_unlock(&Giant); 8252260c03dSRobert Watson UNP_LOCK(); 826b295bdcdSRobert Watson unp = sotounpcb(so); 827b295bdcdSRobert Watson if (unp == NULL) { 828b295bdcdSRobert Watson /* 829b295bdcdSRobert Watson * XXXRW: Temporary debugging printf. 830b295bdcdSRobert Watson */ 831b295bdcdSRobert Watson printf("unp_connect(): lost race to another thread\n"); 832b295bdcdSRobert Watson error = EINVAL; 833b295bdcdSRobert Watson goto bad2; 834b295bdcdSRobert Watson } 835df8bae1dSRodney W. Grimes so2 = vp->v_socket; 836fc3fcacfSRobert Watson if (so2 == NULL) { 837df8bae1dSRodney W. Grimes error = ECONNREFUSED; 8382260c03dSRobert Watson goto bad2; 839df8bae1dSRodney W. Grimes } 840df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 841df8bae1dSRodney W. Grimes error = EPROTOTYPE; 8422260c03dSRobert Watson goto bad2; 843df8bae1dSRodney W. Grimes } 844df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 8450d9ce3a1SRobert Watson if (so2->so_options & SO_ACCEPTCONN) { 8460d9ce3a1SRobert Watson /* 8470d9ce3a1SRobert Watson * NB: drop locks here so unp_attach is entered 8480d9ce3a1SRobert Watson * w/o locks; this avoids a recursive lock 8490d9ce3a1SRobert Watson * of the head and holding sleep locks across 8500d9ce3a1SRobert Watson * a (potentially) blocking malloc. 8510d9ce3a1SRobert Watson */ 8520d9ce3a1SRobert Watson UNP_UNLOCK(); 8530d9ce3a1SRobert Watson so3 = sonewconn(so2, 0); 8540d9ce3a1SRobert Watson UNP_LOCK(); 8550d9ce3a1SRobert Watson } else 8560d9ce3a1SRobert Watson so3 = NULL; 8570d9ce3a1SRobert Watson if (so3 == NULL) { 858df8bae1dSRodney W. Grimes error = ECONNREFUSED; 8590d9ce3a1SRobert Watson goto bad2; 860df8bae1dSRodney W. Grimes } 8610c1bb4fbSDima Dorfman unp = sotounpcb(so); 862df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 863df8bae1dSRodney W. Grimes unp3 = sotounpcb(so3); 8640d9ce3a1SRobert Watson if (unp2->unp_addr != NULL) { 8650d9ce3a1SRobert Watson bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len); 8660d9ce3a1SRobert Watson unp3->unp_addr = (struct sockaddr_un *) sa; 8670d9ce3a1SRobert Watson sa = NULL; 8680d9ce3a1SRobert Watson } 8690c1bb4fbSDima Dorfman /* 8700c1bb4fbSDima Dorfman * unp_peercred management: 8710c1bb4fbSDima Dorfman * 8720c1bb4fbSDima Dorfman * The connecter's (client's) credentials are copied 8730c1bb4fbSDima Dorfman * from its process structure at the time of connect() 8740c1bb4fbSDima Dorfman * (which is now). 8750c1bb4fbSDima Dorfman */ 876a854ed98SJohn Baldwin cru2x(td->td_ucred, &unp3->unp_peercred); 8770c1bb4fbSDima Dorfman unp3->unp_flags |= UNP_HAVEPC; 8780c1bb4fbSDima Dorfman /* 8790c1bb4fbSDima Dorfman * The receiver's (server's) credentials are copied 8800c1bb4fbSDima Dorfman * from the unp_peercred member of socket on which the 8810c1bb4fbSDima Dorfman * former called listen(); unp_listen() cached that 8820c1bb4fbSDima Dorfman * process's credentials at that time so we can use 8830c1bb4fbSDima Dorfman * them now. 8840c1bb4fbSDima Dorfman */ 8850c1bb4fbSDima Dorfman KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 8860c1bb4fbSDima Dorfman ("unp_connect: listener without cached peercred")); 8870c1bb4fbSDima Dorfman memcpy(&unp->unp_peercred, &unp2->unp_peercred, 8880c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 8890c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPC; 890335654d7SRobert Watson #ifdef MAC 891310e7cebSRobert Watson SOCK_LOCK(so); 892335654d7SRobert Watson mac_set_socket_peer_from_socket(so, so3); 893335654d7SRobert Watson mac_set_socket_peer_from_socket(so3, so); 894310e7cebSRobert Watson SOCK_UNLOCK(so); 895335654d7SRobert Watson #endif 8960c1bb4fbSDima Dorfman 897df8bae1dSRodney W. Grimes so2 = so3; 898df8bae1dSRodney W. Grimes } 899df8bae1dSRodney W. Grimes error = unp_connect2(so, so2); 9000d9ce3a1SRobert Watson bad2: 9010d9ce3a1SRobert Watson UNP_UNLOCK(); 9020d9ce3a1SRobert Watson mtx_lock(&Giant); 903df8bae1dSRodney W. Grimes bad: 9040d9ce3a1SRobert Watson mtx_assert(&Giant, MA_OWNED); 9050d9ce3a1SRobert Watson if (vp != NULL) 906df8bae1dSRodney W. Grimes vput(vp); 9070d9ce3a1SRobert Watson mtx_unlock(&Giant); 9080d9ce3a1SRobert Watson free(sa, M_SONAME); 9090d9ce3a1SRobert Watson UNP_LOCK(); 910df8bae1dSRodney W. Grimes return (error); 911df8bae1dSRodney W. Grimes } 912df8bae1dSRodney W. Grimes 913db48c0d2SRobert Watson static int 914df8bae1dSRodney W. Grimes unp_connect2(so, so2) 915df8bae1dSRodney W. Grimes register struct socket *so; 916df8bae1dSRodney W. Grimes register struct socket *so2; 917df8bae1dSRodney W. Grimes { 918df8bae1dSRodney W. Grimes register struct unpcb *unp = sotounpcb(so); 919df8bae1dSRodney W. Grimes register struct unpcb *unp2; 920df8bae1dSRodney W. Grimes 9210d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 9220d9ce3a1SRobert Watson 923df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 924df8bae1dSRodney W. Grimes return (EPROTOTYPE); 925df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 926df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 927df8bae1dSRodney W. Grimes switch (so->so_type) { 928df8bae1dSRodney W. Grimes 929df8bae1dSRodney W. Grimes case SOCK_DGRAM: 93098271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 931df8bae1dSRodney W. Grimes soisconnected(so); 932df8bae1dSRodney W. Grimes break; 933df8bae1dSRodney W. Grimes 934df8bae1dSRodney W. Grimes case SOCK_STREAM: 935df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 936df8bae1dSRodney W. Grimes soisconnected(so); 937df8bae1dSRodney W. Grimes soisconnected(so2); 938df8bae1dSRodney W. Grimes break; 939df8bae1dSRodney W. Grimes 940df8bae1dSRodney W. Grimes default: 941df8bae1dSRodney W. Grimes panic("unp_connect2"); 942df8bae1dSRodney W. Grimes } 943df8bae1dSRodney W. Grimes return (0); 944df8bae1dSRodney W. Grimes } 945df8bae1dSRodney W. Grimes 946f708ef1bSPoul-Henning Kamp static void 947df8bae1dSRodney W. Grimes unp_disconnect(unp) 948df8bae1dSRodney W. Grimes struct unpcb *unp; 949df8bae1dSRodney W. Grimes { 950df8bae1dSRodney W. Grimes register struct unpcb *unp2 = unp->unp_conn; 9511b2e3b4bSRobert Watson struct socket *so; 952df8bae1dSRodney W. Grimes 9530d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 9540d9ce3a1SRobert Watson 955fc3fcacfSRobert Watson if (unp2 == NULL) 956df8bae1dSRodney W. Grimes return; 957fc3fcacfSRobert Watson unp->unp_conn = NULL; 958df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 959df8bae1dSRodney W. Grimes 960df8bae1dSRodney W. Grimes case SOCK_DGRAM: 96198271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 9621b2e3b4bSRobert Watson so = unp->unp_socket; 9631b2e3b4bSRobert Watson SOCK_LOCK(so); 9641b2e3b4bSRobert Watson so->so_state &= ~SS_ISCONNECTED; 9651b2e3b4bSRobert Watson SOCK_UNLOCK(so); 966df8bae1dSRodney W. Grimes break; 967df8bae1dSRodney W. Grimes 968df8bae1dSRodney W. Grimes case SOCK_STREAM: 969df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 970fc3fcacfSRobert Watson unp2->unp_conn = NULL; 971df8bae1dSRodney W. Grimes soisdisconnected(unp2->unp_socket); 972df8bae1dSRodney W. Grimes break; 973df8bae1dSRodney W. Grimes } 974df8bae1dSRodney W. Grimes } 975df8bae1dSRodney W. Grimes 976df8bae1dSRodney W. Grimes #ifdef notdef 97726f9a767SRodney W. Grimes void 978df8bae1dSRodney W. Grimes unp_abort(unp) 979df8bae1dSRodney W. Grimes struct unpcb *unp; 980df8bae1dSRodney W. Grimes { 981df8bae1dSRodney W. Grimes 982df8bae1dSRodney W. Grimes unp_detach(unp); 983df8bae1dSRodney W. Grimes } 984df8bae1dSRodney W. Grimes #endif 985df8bae1dSRodney W. Grimes 9860d9ce3a1SRobert Watson /* 9870d9ce3a1SRobert Watson * unp_pcblist() assumes that UNIX domain socket memory is never reclaimed 9880d9ce3a1SRobert Watson * by the zone (UMA_ZONE_NOFREE), and as such potentially stale pointers 9890d9ce3a1SRobert Watson * are safe to reference. It first scans the list of struct unpcb's to 9900d9ce3a1SRobert Watson * generate a pointer list, then it rescans its list one entry at a time to 9910d9ce3a1SRobert Watson * externalize and copyout. It checks the generation number to see if a 9920d9ce3a1SRobert Watson * struct unpcb has been reused, and will skip it if so. 9930d9ce3a1SRobert Watson */ 99498271db4SGarrett Wollman static int 99582d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 99698271db4SGarrett Wollman { 997f5ef029eSPoul-Henning Kamp int error, i, n; 99898271db4SGarrett Wollman struct unpcb *unp, **unp_list; 99998271db4SGarrett Wollman unp_gen_t gencnt; 10008f364875SJulian Elischer struct xunpgen *xug; 100198271db4SGarrett Wollman struct unp_head *head; 10028f364875SJulian Elischer struct xunpcb *xu; 100398271db4SGarrett Wollman 1004a23d65bfSBruce Evans head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 100598271db4SGarrett Wollman 100698271db4SGarrett Wollman /* 100798271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 100898271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 100998271db4SGarrett Wollman */ 1010fc3fcacfSRobert Watson if (req->oldptr == NULL) { 101198271db4SGarrett Wollman n = unp_count; 10128f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug) 101398271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 1014e5aeaa0cSDag-Erling Smørgrav return (0); 101598271db4SGarrett Wollman } 101698271db4SGarrett Wollman 1017fc3fcacfSRobert Watson if (req->newptr != NULL) 1018e5aeaa0cSDag-Erling Smørgrav return (EPERM); 101998271db4SGarrett Wollman 102098271db4SGarrett Wollman /* 102198271db4SGarrett Wollman * OK, now we're committed to doing something. 102298271db4SGarrett Wollman */ 1023a163d034SWarner Losh xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 10240d9ce3a1SRobert Watson UNP_LOCK(); 102598271db4SGarrett Wollman gencnt = unp_gencnt; 102698271db4SGarrett Wollman n = unp_count; 10270d9ce3a1SRobert Watson UNP_UNLOCK(); 102898271db4SGarrett Wollman 10298f364875SJulian Elischer xug->xug_len = sizeof *xug; 10308f364875SJulian Elischer xug->xug_count = n; 10318f364875SJulian Elischer xug->xug_gen = gencnt; 10328f364875SJulian Elischer xug->xug_sogen = so_gencnt; 10338f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 10348f364875SJulian Elischer if (error) { 10358f364875SJulian Elischer free(xug, M_TEMP); 1036e5aeaa0cSDag-Erling Smørgrav return (error); 10378f364875SJulian Elischer } 103898271db4SGarrett Wollman 1039a163d034SWarner Losh unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 104098271db4SGarrett Wollman 10410d9ce3a1SRobert Watson UNP_LOCK(); 10422e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 10432e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 10448a7d8cc6SRobert Watson if (unp->unp_gencnt <= gencnt) { 1045a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred, 10468a7d8cc6SRobert Watson unp->unp_socket->so_cred)) 10474787fd37SPaul Saab continue; 104898271db4SGarrett Wollman unp_list[i++] = unp; 104998271db4SGarrett Wollman } 10504787fd37SPaul Saab } 10510d9ce3a1SRobert Watson UNP_UNLOCK(); 105298271db4SGarrett Wollman n = i; /* in case we lost some during malloc */ 105398271db4SGarrett Wollman 105498271db4SGarrett Wollman error = 0; 1055a163d034SWarner Losh xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK); 105698271db4SGarrett Wollman for (i = 0; i < n; i++) { 105798271db4SGarrett Wollman unp = unp_list[i]; 105898271db4SGarrett Wollman if (unp->unp_gencnt <= gencnt) { 10598f364875SJulian Elischer xu->xu_len = sizeof *xu; 10608f364875SJulian Elischer xu->xu_unpp = unp; 106198271db4SGarrett Wollman /* 106298271db4SGarrett Wollman * XXX - need more locking here to protect against 106398271db4SGarrett Wollman * connect/disconnect races for SMP. 106498271db4SGarrett Wollman */ 1065fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 10668f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr, 106798271db4SGarrett Wollman unp->unp_addr->sun_len); 1068fc3fcacfSRobert Watson if (unp->unp_conn != NULL && 1069fc3fcacfSRobert Watson unp->unp_conn->unp_addr != NULL) 107098271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 10718f364875SJulian Elischer &xu->xu_caddr, 107298271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 10738f364875SJulian Elischer bcopy(unp, &xu->xu_unp, sizeof *unp); 10748f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket); 10758f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu); 107698271db4SGarrett Wollman } 107798271db4SGarrett Wollman } 10788f364875SJulian Elischer free(xu, M_TEMP); 107998271db4SGarrett Wollman if (!error) { 108098271db4SGarrett Wollman /* 108198271db4SGarrett Wollman * Give the user an updated idea of our state. 108298271db4SGarrett Wollman * If the generation differs from what we told 108398271db4SGarrett Wollman * her before, she knows that something happened 108498271db4SGarrett Wollman * while we were processing this request, and it 108598271db4SGarrett Wollman * might be necessary to retry. 108698271db4SGarrett Wollman */ 10878f364875SJulian Elischer xug->xug_gen = unp_gencnt; 10888f364875SJulian Elischer xug->xug_sogen = so_gencnt; 10898f364875SJulian Elischer xug->xug_count = unp_count; 10908f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 109198271db4SGarrett Wollman } 109298271db4SGarrett Wollman free(unp_list, M_TEMP); 10938f364875SJulian Elischer free(xug, M_TEMP); 1094e5aeaa0cSDag-Erling Smørgrav return (error); 109598271db4SGarrett Wollman } 109698271db4SGarrett Wollman 109798271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 109898271db4SGarrett Wollman (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 109998271db4SGarrett Wollman "List of active local datagram sockets"); 110098271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 110198271db4SGarrett Wollman (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 110298271db4SGarrett Wollman "List of active local stream sockets"); 110398271db4SGarrett Wollman 1104f708ef1bSPoul-Henning Kamp static void 1105df8bae1dSRodney W. Grimes unp_shutdown(unp) 1106df8bae1dSRodney W. Grimes struct unpcb *unp; 1107df8bae1dSRodney W. Grimes { 1108df8bae1dSRodney W. Grimes struct socket *so; 1109df8bae1dSRodney W. Grimes 11100d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 11110d9ce3a1SRobert Watson 1112df8bae1dSRodney W. Grimes if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 1113df8bae1dSRodney W. Grimes (so = unp->unp_conn->unp_socket)) 1114df8bae1dSRodney W. Grimes socantrcvmore(so); 1115df8bae1dSRodney W. Grimes } 1116df8bae1dSRodney W. Grimes 1117f708ef1bSPoul-Henning Kamp static void 1118df8bae1dSRodney W. Grimes unp_drop(unp, errno) 1119df8bae1dSRodney W. Grimes struct unpcb *unp; 1120df8bae1dSRodney W. Grimes int errno; 1121df8bae1dSRodney W. Grimes { 1122df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 1123df8bae1dSRodney W. Grimes 11240d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 11250d9ce3a1SRobert Watson 1126df8bae1dSRodney W. Grimes so->so_error = errno; 1127df8bae1dSRodney W. Grimes unp_disconnect(unp); 1128df8bae1dSRodney W. Grimes } 1129df8bae1dSRodney W. Grimes 1130df8bae1dSRodney W. Grimes #ifdef notdef 113126f9a767SRodney W. Grimes void 1132df8bae1dSRodney W. Grimes unp_drain() 1133df8bae1dSRodney W. Grimes { 1134df8bae1dSRodney W. Grimes 1135df8bae1dSRodney W. Grimes } 1136df8bae1dSRodney W. Grimes #endif 1137df8bae1dSRodney W. Grimes 11382bc21ed9SDavid Malone static void 11392bc21ed9SDavid Malone unp_freerights(rp, fdcount) 11402bc21ed9SDavid Malone struct file **rp; 11412bc21ed9SDavid Malone int fdcount; 1142df8bae1dSRodney W. Grimes { 11432bc21ed9SDavid Malone int i; 11442bc21ed9SDavid Malone struct file *fp; 1145df8bae1dSRodney W. Grimes 11462bc21ed9SDavid Malone for (i = 0; i < fdcount; i++) { 1147df8bae1dSRodney W. Grimes fp = *rp; 11488692c025SYoshinobu Inoue /* 11492bc21ed9SDavid Malone * zero the pointer before calling 11502bc21ed9SDavid Malone * unp_discard since it may end up 11512bc21ed9SDavid Malone * in unp_gc().. 11528692c025SYoshinobu Inoue */ 1153df8bae1dSRodney W. Grimes *rp++ = 0; 11548692c025SYoshinobu Inoue unp_discard(fp); 1155df8bae1dSRodney W. Grimes } 11562bc21ed9SDavid Malone } 11572bc21ed9SDavid Malone 11582bc21ed9SDavid Malone int 11592bc21ed9SDavid Malone unp_externalize(control, controlp) 11602bc21ed9SDavid Malone struct mbuf *control, **controlp; 11612bc21ed9SDavid Malone { 11622bc21ed9SDavid Malone struct thread *td = curthread; /* XXX */ 11632bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 11642bc21ed9SDavid Malone int i; 11652bc21ed9SDavid Malone int *fdp; 11662bc21ed9SDavid Malone struct file **rp; 11672bc21ed9SDavid Malone struct file *fp; 11682bc21ed9SDavid Malone void *data; 11692bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 11702bc21ed9SDavid Malone int error, newfds; 11712bc21ed9SDavid Malone int f; 11722bc21ed9SDavid Malone u_int newlen; 11732bc21ed9SDavid Malone 11742bc21ed9SDavid Malone error = 0; 11752bc21ed9SDavid Malone if (controlp != NULL) /* controlp == NULL => free control messages */ 11762bc21ed9SDavid Malone *controlp = NULL; 11772bc21ed9SDavid Malone 11782bc21ed9SDavid Malone while (cm != NULL) { 11792bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 11802bc21ed9SDavid Malone error = EINVAL; 11812bc21ed9SDavid Malone break; 11822bc21ed9SDavid Malone } 11832bc21ed9SDavid Malone 11842bc21ed9SDavid Malone data = CMSG_DATA(cm); 11852bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 11862bc21ed9SDavid Malone 11872bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET 11882bc21ed9SDavid Malone && cm->cmsg_type == SCM_RIGHTS) { 11892bc21ed9SDavid Malone newfds = datalen / sizeof(struct file *); 11902bc21ed9SDavid Malone rp = data; 11912bc21ed9SDavid Malone 1192e2f9a08bSOlivier Houchard /* If we're not outputting the descriptors free them. */ 11932bc21ed9SDavid Malone if (error || controlp == NULL) { 11942bc21ed9SDavid Malone unp_freerights(rp, newfds); 11952bc21ed9SDavid Malone goto next; 11962bc21ed9SDavid Malone } 1197426da3bcSAlfred Perlstein FILEDESC_LOCK(td->td_proc->p_fd); 11982bc21ed9SDavid Malone /* if the new FD's will not fit free them. */ 11992bc21ed9SDavid Malone if (!fdavail(td, newfds)) { 1200426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 12012bc21ed9SDavid Malone error = EMSGSIZE; 12022bc21ed9SDavid Malone unp_freerights(rp, newfds); 12032bc21ed9SDavid Malone goto next; 1204df8bae1dSRodney W. Grimes } 1205ed5b7817SJulian Elischer /* 12062bc21ed9SDavid Malone * now change each pointer to an fd in the global 12072bc21ed9SDavid Malone * table to an integer that is the index to the 12082bc21ed9SDavid Malone * local fd table entry that we set up to point 12092bc21ed9SDavid Malone * to the global one we are transferring. 1210ed5b7817SJulian Elischer */ 12112bc21ed9SDavid Malone newlen = newfds * sizeof(int); 12122bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 12132bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 12142bc21ed9SDavid Malone if (*controlp == NULL) { 1215426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 12162bc21ed9SDavid Malone error = E2BIG; 12172bc21ed9SDavid Malone unp_freerights(rp, newfds); 12182bc21ed9SDavid Malone goto next; 12192bc21ed9SDavid Malone } 12202bc21ed9SDavid Malone 12212bc21ed9SDavid Malone fdp = (int *) 12222bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1223df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 1224a6d4491cSDag-Erling Smørgrav if (fdalloc(td, 0, &f)) 12252bc21ed9SDavid Malone panic("unp_externalize fdalloc failed"); 12268692c025SYoshinobu Inoue fp = *rp++; 1227b40ce416SJulian Elischer td->td_proc->p_fd->fd_ofiles[f] = fp; 1228426da3bcSAlfred Perlstein FILE_LOCK(fp); 1229df8bae1dSRodney W. Grimes fp->f_msgcount--; 1230426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1231df8bae1dSRodney W. Grimes unp_rights--; 12328692c025SYoshinobu Inoue *fdp++ = f; 1233df8bae1dSRodney W. Grimes } 1234426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 12352bc21ed9SDavid Malone } else { /* We can just copy anything else across */ 12362bc21ed9SDavid Malone if (error || controlp == NULL) 12372bc21ed9SDavid Malone goto next; 12382bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, datalen, 12392bc21ed9SDavid Malone cm->cmsg_type, cm->cmsg_level); 12402bc21ed9SDavid Malone if (*controlp == NULL) { 12412bc21ed9SDavid Malone error = ENOBUFS; 12422bc21ed9SDavid Malone goto next; 12432bc21ed9SDavid Malone } 12442bc21ed9SDavid Malone bcopy(data, 12452bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 12462bc21ed9SDavid Malone datalen); 12472bc21ed9SDavid Malone } 12482bc21ed9SDavid Malone 12492bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 12502bc21ed9SDavid Malone 12512bc21ed9SDavid Malone next: 12522bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 12532bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 12542bc21ed9SDavid Malone cm = (struct cmsghdr *) 12552bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 12568692c025SYoshinobu Inoue } else { 12572bc21ed9SDavid Malone clen = 0; 12582bc21ed9SDavid Malone cm = NULL; 12598692c025SYoshinobu Inoue } 12608692c025SYoshinobu Inoue } 12618692c025SYoshinobu Inoue 12622bc21ed9SDavid Malone m_freem(control); 12632bc21ed9SDavid Malone 12642bc21ed9SDavid Malone return (error); 1265df8bae1dSRodney W. Grimes } 1266df8bae1dSRodney W. Grimes 126798271db4SGarrett Wollman void 126898271db4SGarrett Wollman unp_init(void) 126998271db4SGarrett Wollman { 12709e9d298aSJeff Roberson unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 12719e9d298aSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 1272fc3fcacfSRobert Watson if (unp_zone == NULL) 127398271db4SGarrett Wollman panic("unp_init"); 1274b17dd2bcSColin Percival uma_zone_set_max(unp_zone, nmbclusters); 127598271db4SGarrett Wollman LIST_INIT(&unp_dhead); 127698271db4SGarrett Wollman LIST_INIT(&unp_shead); 12770d9ce3a1SRobert Watson 12780d9ce3a1SRobert Watson UNP_LOCK_INIT(); 127998271db4SGarrett Wollman } 128098271db4SGarrett Wollman 1281f708ef1bSPoul-Henning Kamp static int 12822bc21ed9SDavid Malone unp_internalize(controlp, td) 12832bc21ed9SDavid Malone struct mbuf **controlp; 1284b40ce416SJulian Elischer struct thread *td; 1285df8bae1dSRodney W. Grimes { 12862bc21ed9SDavid Malone struct mbuf *control = *controlp; 1287b40ce416SJulian Elischer struct proc *p = td->td_proc; 12888692c025SYoshinobu Inoue struct filedesc *fdescp = p->p_fd; 12892bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 12902bc21ed9SDavid Malone struct cmsgcred *cmcred; 12912bc21ed9SDavid Malone struct file **rp; 12922bc21ed9SDavid Malone struct file *fp; 12932bc21ed9SDavid Malone struct timeval *tv; 12942bc21ed9SDavid Malone int i, fd, *fdp; 12952bc21ed9SDavid Malone void *data; 12962bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 12972bc21ed9SDavid Malone int error, oldfds; 12988692c025SYoshinobu Inoue u_int newlen; 1299df8bae1dSRodney W. Grimes 13002bc21ed9SDavid Malone error = 0; 13012bc21ed9SDavid Malone *controlp = NULL; 13020b788fa1SBill Paul 13032bc21ed9SDavid Malone while (cm != NULL) { 13042bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 13052bc21ed9SDavid Malone || cm->cmsg_len > clen) { 13062bc21ed9SDavid Malone error = EINVAL; 13072bc21ed9SDavid Malone goto out; 13082bc21ed9SDavid Malone } 13092bc21ed9SDavid Malone 13102bc21ed9SDavid Malone data = CMSG_DATA(cm); 13112bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 13122bc21ed9SDavid Malone 13132bc21ed9SDavid Malone switch (cm->cmsg_type) { 13140b788fa1SBill Paul /* 13150b788fa1SBill Paul * Fill in credential information. 13160b788fa1SBill Paul */ 13172bc21ed9SDavid Malone case SCM_CREDS: 13182bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 13192bc21ed9SDavid Malone SCM_CREDS, SOL_SOCKET); 13202bc21ed9SDavid Malone if (*controlp == NULL) { 13212bc21ed9SDavid Malone error = ENOBUFS; 13222bc21ed9SDavid Malone goto out; 13232bc21ed9SDavid Malone } 13242bc21ed9SDavid Malone 13252bc21ed9SDavid Malone cmcred = (struct cmsgcred *) 13262bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 13270b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 1328a854ed98SJohn Baldwin cmcred->cmcred_uid = td->td_ucred->cr_ruid; 1329a854ed98SJohn Baldwin cmcred->cmcred_gid = td->td_ucred->cr_rgid; 1330a854ed98SJohn Baldwin cmcred->cmcred_euid = td->td_ucred->cr_uid; 1331a854ed98SJohn Baldwin cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 13320b788fa1SBill Paul CMGROUP_MAX); 13330b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 13342bc21ed9SDavid Malone cmcred->cmcred_groups[i] = 1335a854ed98SJohn Baldwin td->td_ucred->cr_groups[i]; 13362bc21ed9SDavid Malone break; 13370b788fa1SBill Paul 13382bc21ed9SDavid Malone case SCM_RIGHTS: 13392bc21ed9SDavid Malone oldfds = datalen / sizeof (int); 1340ed5b7817SJulian Elischer /* 13412bc21ed9SDavid Malone * check that all the FDs passed in refer to legal files 1342ed5b7817SJulian Elischer * If not, reject the entire operation. 1343ed5b7817SJulian Elischer */ 13442bc21ed9SDavid Malone fdp = data; 1345426da3bcSAlfred Perlstein FILEDESC_LOCK(fdescp); 1346df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 13478692c025SYoshinobu Inoue fd = *fdp++; 13488692c025SYoshinobu Inoue if ((unsigned)fd >= fdescp->fd_nfiles || 13492bc21ed9SDavid Malone fdescp->fd_ofiles[fd] == NULL) { 1350426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 13512bc21ed9SDavid Malone error = EBADF; 13522bc21ed9SDavid Malone goto out; 13532bc21ed9SDavid Malone } 1354e7d6662fSAlfred Perlstein fp = fdescp->fd_ofiles[fd]; 1355e7d6662fSAlfred Perlstein if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 1356e7d6662fSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 1357e7d6662fSAlfred Perlstein error = EOPNOTSUPP; 1358e7d6662fSAlfred Perlstein goto out; 1359e7d6662fSAlfred Perlstein } 1360e7d6662fSAlfred Perlstein 1361df8bae1dSRodney W. Grimes } 1362ed5b7817SJulian Elischer /* 1363ed5b7817SJulian Elischer * Now replace the integer FDs with pointers to 1364ed5b7817SJulian Elischer * the associated global file table entry.. 1365ed5b7817SJulian Elischer */ 13662bc21ed9SDavid Malone newlen = oldfds * sizeof(struct file *); 13672bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 13682bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 13692bc21ed9SDavid Malone if (*controlp == NULL) { 1370426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 13712bc21ed9SDavid Malone error = E2BIG; 13722bc21ed9SDavid Malone goto out; 13738692c025SYoshinobu Inoue } 13748692c025SYoshinobu Inoue 13752bc21ed9SDavid Malone fdp = data; 13762bc21ed9SDavid Malone rp = (struct file **) 13772bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 13788692c025SYoshinobu Inoue for (i = 0; i < oldfds; i++) { 13798692c025SYoshinobu Inoue fp = fdescp->fd_ofiles[*fdp++]; 1380df8bae1dSRodney W. Grimes *rp++ = fp; 1381426da3bcSAlfred Perlstein FILE_LOCK(fp); 1382df8bae1dSRodney W. Grimes fp->f_count++; 1383df8bae1dSRodney W. Grimes fp->f_msgcount++; 1384426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1385df8bae1dSRodney W. Grimes unp_rights++; 1386df8bae1dSRodney W. Grimes } 1387426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 13882bc21ed9SDavid Malone break; 13892bc21ed9SDavid Malone 13902bc21ed9SDavid Malone case SCM_TIMESTAMP: 13912bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*tv), 13922bc21ed9SDavid Malone SCM_TIMESTAMP, SOL_SOCKET); 13932bc21ed9SDavid Malone if (*controlp == NULL) { 13942bc21ed9SDavid Malone error = ENOBUFS; 13952bc21ed9SDavid Malone goto out; 13968692c025SYoshinobu Inoue } 13972bc21ed9SDavid Malone tv = (struct timeval *) 13982bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 13992bc21ed9SDavid Malone microtime(tv); 14002bc21ed9SDavid Malone break; 14012bc21ed9SDavid Malone 14022bc21ed9SDavid Malone default: 14032bc21ed9SDavid Malone error = EINVAL; 14042bc21ed9SDavid Malone goto out; 14052bc21ed9SDavid Malone } 14062bc21ed9SDavid Malone 14072bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 14082bc21ed9SDavid Malone 14092bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 14102bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 14112bc21ed9SDavid Malone cm = (struct cmsghdr *) 14122bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 14132bc21ed9SDavid Malone } else { 14142bc21ed9SDavid Malone clen = 0; 14152bc21ed9SDavid Malone cm = NULL; 14162bc21ed9SDavid Malone } 14172bc21ed9SDavid Malone } 14182bc21ed9SDavid Malone 14192bc21ed9SDavid Malone out: 14202bc21ed9SDavid Malone m_freem(control); 14212bc21ed9SDavid Malone 14222bc21ed9SDavid Malone return (error); 1423df8bae1dSRodney W. Grimes } 1424df8bae1dSRodney W. Grimes 1425f708ef1bSPoul-Henning Kamp static int unp_defer, unp_gcing; 1426df8bae1dSRodney W. Grimes 1427f708ef1bSPoul-Henning Kamp static void 1428df8bae1dSRodney W. Grimes unp_gc() 1429df8bae1dSRodney W. Grimes { 1430df8bae1dSRodney W. Grimes register struct file *fp, *nextfp; 1431df8bae1dSRodney W. Grimes register struct socket *so; 1432df8bae1dSRodney W. Grimes struct file **extra_ref, **fpp; 1433df8bae1dSRodney W. Grimes int nunref, i; 143495f004dcSAlfred Perlstein int nfiles_snap; 143595f004dcSAlfred Perlstein int nfiles_slack = 20; 1436df8bae1dSRodney W. Grimes 14370d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 14380d9ce3a1SRobert Watson 1439df8bae1dSRodney W. Grimes if (unp_gcing) 1440df8bae1dSRodney W. Grimes return; 1441df8bae1dSRodney W. Grimes unp_gcing = 1; 1442df8bae1dSRodney W. Grimes unp_defer = 0; 1443ed5b7817SJulian Elischer /* 1444ed5b7817SJulian Elischer * before going through all this, set all FDs to 1445ed5b7817SJulian Elischer * be NOT defered and NOT externally accessible 1446ed5b7817SJulian Elischer */ 14470d9ce3a1SRobert Watson /* 14480d9ce3a1SRobert Watson * XXXRW: Acquiring a sleep lock while holding UNP 14490d9ce3a1SRobert Watson * mutex cannot be a good thing. 14500d9ce3a1SRobert Watson */ 1451426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 14522e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) 1453426da3bcSAlfred Perlstein fp->f_gcflag &= ~(FMARK|FDEFER); 1454df8bae1dSRodney W. Grimes do { 14552e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) { 1456426da3bcSAlfred Perlstein FILE_LOCK(fp); 1457ed5b7817SJulian Elischer /* 1458ed5b7817SJulian Elischer * If the file is not open, skip it 1459ed5b7817SJulian Elischer */ 1460426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1461426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1462df8bae1dSRodney W. Grimes continue; 1463426da3bcSAlfred Perlstein } 1464ed5b7817SJulian Elischer /* 1465ed5b7817SJulian Elischer * If we already marked it as 'defer' in a 1466ed5b7817SJulian Elischer * previous pass, then try process it this time 1467ed5b7817SJulian Elischer * and un-mark it 1468ed5b7817SJulian Elischer */ 1469426da3bcSAlfred Perlstein if (fp->f_gcflag & FDEFER) { 1470426da3bcSAlfred Perlstein fp->f_gcflag &= ~FDEFER; 1471df8bae1dSRodney W. Grimes unp_defer--; 1472df8bae1dSRodney W. Grimes } else { 1473ed5b7817SJulian Elischer /* 1474ed5b7817SJulian Elischer * if it's not defered, then check if it's 1475ed5b7817SJulian Elischer * already marked.. if so skip it 1476ed5b7817SJulian Elischer */ 1477426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) { 1478426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1479df8bae1dSRodney W. Grimes continue; 1480426da3bcSAlfred Perlstein } 1481ed5b7817SJulian Elischer /* 1482ed5b7817SJulian Elischer * If all references are from messages 1483ed5b7817SJulian Elischer * in transit, then skip it. it's not 1484ed5b7817SJulian Elischer * externally accessible. 1485ed5b7817SJulian Elischer */ 1486426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount) { 1487426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1488df8bae1dSRodney W. Grimes continue; 1489426da3bcSAlfred Perlstein } 1490ed5b7817SJulian Elischer /* 1491ed5b7817SJulian Elischer * If it got this far then it must be 1492ed5b7817SJulian Elischer * externally accessible. 1493ed5b7817SJulian Elischer */ 1494426da3bcSAlfred Perlstein fp->f_gcflag |= FMARK; 1495df8bae1dSRodney W. Grimes } 1496ed5b7817SJulian Elischer /* 1497ed5b7817SJulian Elischer * either it was defered, or it is externally 1498ed5b7817SJulian Elischer * accessible and not already marked so. 1499ed5b7817SJulian Elischer * Now check if it is possibly one of OUR sockets. 1500ed5b7817SJulian Elischer */ 1501df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_SOCKET || 150248e3128bSMatthew Dillon (so = fp->f_data) == NULL) { 1503426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1504df8bae1dSRodney W. Grimes continue; 1505426da3bcSAlfred Perlstein } 1506426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1507748e0b0aSGarrett Wollman if (so->so_proto->pr_domain != &localdomain || 1508df8bae1dSRodney W. Grimes (so->so_proto->pr_flags&PR_RIGHTS) == 0) 1509df8bae1dSRodney W. Grimes continue; 1510df8bae1dSRodney W. Grimes #ifdef notdef 1511df8bae1dSRodney W. Grimes if (so->so_rcv.sb_flags & SB_LOCK) { 1512df8bae1dSRodney W. Grimes /* 1513df8bae1dSRodney W. Grimes * This is problematical; it's not clear 1514df8bae1dSRodney W. Grimes * we need to wait for the sockbuf to be 1515df8bae1dSRodney W. Grimes * unlocked (on a uniprocessor, at least), 1516df8bae1dSRodney W. Grimes * and it's also not clear what to do 1517df8bae1dSRodney W. Grimes * if sbwait returns an error due to receipt 1518df8bae1dSRodney W. Grimes * of a signal. If sbwait does return 1519df8bae1dSRodney W. Grimes * an error, we'll go into an infinite 1520df8bae1dSRodney W. Grimes * loop. Delete all of this for now. 1521df8bae1dSRodney W. Grimes */ 1522df8bae1dSRodney W. Grimes (void) sbwait(&so->so_rcv); 1523df8bae1dSRodney W. Grimes goto restart; 1524df8bae1dSRodney W. Grimes } 1525df8bae1dSRodney W. Grimes #endif 1526ed5b7817SJulian Elischer /* 1527ed5b7817SJulian Elischer * So, Ok, it's one of our sockets and it IS externally 1528ed5b7817SJulian Elischer * accessible (or was defered). Now we look 1529dc733423SDag-Erling Smørgrav * to see if we hold any file descriptors in its 1530ed5b7817SJulian Elischer * message buffers. Follow those links and mark them 1531ed5b7817SJulian Elischer * as accessible too. 1532ed5b7817SJulian Elischer */ 15337717cf07SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1534df8bae1dSRodney W. Grimes unp_scan(so->so_rcv.sb_mb, unp_mark); 15357717cf07SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 1536df8bae1dSRodney W. Grimes } 1537df8bae1dSRodney W. Grimes } while (unp_defer); 1538426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1539df8bae1dSRodney W. Grimes /* 1540df8bae1dSRodney W. Grimes * We grab an extra reference to each of the file table entries 1541df8bae1dSRodney W. Grimes * that are not otherwise accessible and then free the rights 1542df8bae1dSRodney W. Grimes * that are stored in messages on them. 1543df8bae1dSRodney W. Grimes * 1544df8bae1dSRodney W. Grimes * The bug in the orginal code is a little tricky, so I'll describe 1545df8bae1dSRodney W. Grimes * what's wrong with it here. 1546df8bae1dSRodney W. Grimes * 1547df8bae1dSRodney W. Grimes * It is incorrect to simply unp_discard each entry for f_msgcount 1548df8bae1dSRodney W. Grimes * times -- consider the case of sockets A and B that contain 1549df8bae1dSRodney W. Grimes * references to each other. On a last close of some other socket, 1550df8bae1dSRodney W. Grimes * we trigger a gc since the number of outstanding rights (unp_rights) 1551df8bae1dSRodney W. Grimes * is non-zero. If during the sweep phase the gc code un_discards, 1552df8bae1dSRodney W. Grimes * we end up doing a (full) closef on the descriptor. A closef on A 1553df8bae1dSRodney W. Grimes * results in the following chain. Closef calls soo_close, which 1554df8bae1dSRodney W. Grimes * calls soclose. Soclose calls first (through the switch 1555df8bae1dSRodney W. Grimes * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 1556df8bae1dSRodney W. Grimes * returns because the previous instance had set unp_gcing, and 1557df8bae1dSRodney W. Grimes * we return all the way back to soclose, which marks the socket 1558df8bae1dSRodney W. Grimes * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush 1559df8bae1dSRodney W. Grimes * to free up the rights that are queued in messages on the socket A, 1560df8bae1dSRodney W. Grimes * i.e., the reference on B. The sorflush calls via the dom_dispose 1561df8bae1dSRodney W. Grimes * switch unp_dispose, which unp_scans with unp_discard. This second 1562df8bae1dSRodney W. Grimes * instance of unp_discard just calls closef on B. 1563df8bae1dSRodney W. Grimes * 1564df8bae1dSRodney W. Grimes * Well, a similar chain occurs on B, resulting in a sorflush on B, 1565df8bae1dSRodney W. Grimes * which results in another closef on A. Unfortunately, A is already 1566df8bae1dSRodney W. Grimes * being closed, and the descriptor has already been marked with 1567df8bae1dSRodney W. Grimes * SS_NOFDREF, and soclose panics at this point. 1568df8bae1dSRodney W. Grimes * 1569df8bae1dSRodney W. Grimes * Here, we first take an extra reference to each inaccessible 1570df8bae1dSRodney W. Grimes * descriptor. Then, we call sorflush ourself, since we know 1571df8bae1dSRodney W. Grimes * it is a Unix domain socket anyhow. After we destroy all the 1572df8bae1dSRodney W. Grimes * rights carried in messages, we do a last closef to get rid 1573df8bae1dSRodney W. Grimes * of our extra reference. This is the last close, and the 1574df8bae1dSRodney W. Grimes * unp_detach etc will shut down the socket. 1575df8bae1dSRodney W. Grimes * 1576df8bae1dSRodney W. Grimes * 91/09/19, bsy@cs.cmu.edu 1577df8bae1dSRodney W. Grimes */ 157895f004dcSAlfred Perlstein again: 157995f004dcSAlfred Perlstein nfiles_snap = nfiles + nfiles_slack; /* some slack */ 158095f004dcSAlfred Perlstein extra_ref = malloc(nfiles_snap * sizeof(struct file *), M_TEMP, 158195f004dcSAlfred Perlstein M_WAITOK); 1582426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 158395f004dcSAlfred Perlstein if (nfiles_snap < nfiles) { 158495f004dcSAlfred Perlstein sx_sunlock(&filelist_lock); 158595f004dcSAlfred Perlstein free(extra_ref, M_TEMP); 158695f004dcSAlfred Perlstein nfiles_slack += 20; 158795f004dcSAlfred Perlstein goto again; 158895f004dcSAlfred Perlstein } 1589fc3fcacfSRobert Watson for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; 1590fc3fcacfSRobert Watson fp != NULL; fp = nextfp) { 15912e3c8fcbSPoul-Henning Kamp nextfp = LIST_NEXT(fp, f_list); 1592426da3bcSAlfred Perlstein FILE_LOCK(fp); 1593ed5b7817SJulian Elischer /* 1594ed5b7817SJulian Elischer * If it's not open, skip it 1595ed5b7817SJulian Elischer */ 1596426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1597426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1598df8bae1dSRodney W. Grimes continue; 1599426da3bcSAlfred Perlstein } 1600ed5b7817SJulian Elischer /* 1601ed5b7817SJulian Elischer * If all refs are from msgs, and it's not marked accessible 1602ed5b7817SJulian Elischer * then it must be referenced from some unreachable cycle 1603ed5b7817SJulian Elischer * of (shut-down) FDs, so include it in our 1604ed5b7817SJulian Elischer * list of FDs to remove 1605ed5b7817SJulian Elischer */ 1606426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) { 1607df8bae1dSRodney W. Grimes *fpp++ = fp; 1608df8bae1dSRodney W. Grimes nunref++; 1609df8bae1dSRodney W. Grimes fp->f_count++; 1610df8bae1dSRodney W. Grimes } 1611426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1612df8bae1dSRodney W. Grimes } 1613426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1614ed5b7817SJulian Elischer /* 1615ed5b7817SJulian Elischer * for each FD on our hit list, do the following two things 1616ed5b7817SJulian Elischer */ 16171c7c3c6aSMatthew Dillon for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 16181c7c3c6aSMatthew Dillon struct file *tfp = *fpp; 1619426da3bcSAlfred Perlstein FILE_LOCK(tfp); 1620cd72f218SMatthew Dillon if (tfp->f_type == DTYPE_SOCKET && 162148e3128bSMatthew Dillon tfp->f_data != NULL) { 1622426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 162348e3128bSMatthew Dillon sorflush(tfp->f_data); 1624e5aeaa0cSDag-Erling Smørgrav } else { 1625426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 16261c7c3c6aSMatthew Dillon } 1627e5aeaa0cSDag-Erling Smørgrav } 1628df8bae1dSRodney W. Grimes for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) 1629b40ce416SJulian Elischer closef(*fpp, (struct thread *) NULL); 1630210a5a71SAlfred Perlstein free(extra_ref, M_TEMP); 1631df8bae1dSRodney W. Grimes unp_gcing = 0; 1632df8bae1dSRodney W. Grimes } 1633df8bae1dSRodney W. Grimes 163426f9a767SRodney W. Grimes void 1635df8bae1dSRodney W. Grimes unp_dispose(m) 1636df8bae1dSRodney W. Grimes struct mbuf *m; 1637df8bae1dSRodney W. Grimes { 1638996c772fSJohn Dyson 1639df8bae1dSRodney W. Grimes if (m) 1640df8bae1dSRodney W. Grimes unp_scan(m, unp_discard); 1641df8bae1dSRodney W. Grimes } 1642df8bae1dSRodney W. Grimes 16430c1bb4fbSDima Dorfman static int 16446f105b34SJohn Baldwin unp_listen(unp, td) 16450c1bb4fbSDima Dorfman struct unpcb *unp; 16466f105b34SJohn Baldwin struct thread *td; 16470c1bb4fbSDima Dorfman { 16480d9ce3a1SRobert Watson UNP_LOCK_ASSERT(); 16490c1bb4fbSDima Dorfman 16500d9ce3a1SRobert Watson /* 16510d9ce3a1SRobert Watson * XXXRW: Why populate the local peer cred with our own credential? 16520d9ce3a1SRobert Watson */ 16536f105b34SJohn Baldwin cru2x(td->td_ucred, &unp->unp_peercred); 16540c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPCCACHED; 16550c1bb4fbSDima Dorfman return (0); 16560c1bb4fbSDima Dorfman } 16570c1bb4fbSDima Dorfman 1658f708ef1bSPoul-Henning Kamp static void 1659df8bae1dSRodney W. Grimes unp_scan(m0, op) 1660df8bae1dSRodney W. Grimes register struct mbuf *m0; 16614d77a549SAlfred Perlstein void (*op)(struct file *); 1662df8bae1dSRodney W. Grimes { 16632bc21ed9SDavid Malone struct mbuf *m; 16642bc21ed9SDavid Malone struct file **rp; 16652bc21ed9SDavid Malone struct cmsghdr *cm; 16662bc21ed9SDavid Malone void *data; 16672bc21ed9SDavid Malone int i; 16682bc21ed9SDavid Malone socklen_t clen, datalen; 1669df8bae1dSRodney W. Grimes int qfds; 1670df8bae1dSRodney W. Grimes 1671fc3fcacfSRobert Watson while (m0 != NULL) { 16722bc21ed9SDavid Malone for (m = m0; m; m = m->m_next) { 167312396bdcSDavid Malone if (m->m_type != MT_CONTROL) 1674df8bae1dSRodney W. Grimes continue; 16752bc21ed9SDavid Malone 16762bc21ed9SDavid Malone cm = mtod(m, struct cmsghdr *); 16772bc21ed9SDavid Malone clen = m->m_len; 16782bc21ed9SDavid Malone 16792bc21ed9SDavid Malone while (cm != NULL) { 16802bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) 16812bc21ed9SDavid Malone break; 16822bc21ed9SDavid Malone 16832bc21ed9SDavid Malone data = CMSG_DATA(cm); 16842bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len 16852bc21ed9SDavid Malone - (caddr_t)data; 16862bc21ed9SDavid Malone 16872bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET && 16882bc21ed9SDavid Malone cm->cmsg_type == SCM_RIGHTS) { 16892bc21ed9SDavid Malone qfds = datalen / sizeof (struct file *); 16902bc21ed9SDavid Malone rp = data; 1691df8bae1dSRodney W. Grimes for (i = 0; i < qfds; i++) 1692df8bae1dSRodney W. Grimes (*op)(*rp++); 16932bc21ed9SDavid Malone } 16942bc21ed9SDavid Malone 16952bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 16962bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 16972bc21ed9SDavid Malone cm = (struct cmsghdr *) 16982bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 16992bc21ed9SDavid Malone } else { 17002bc21ed9SDavid Malone clen = 0; 17012bc21ed9SDavid Malone cm = NULL; 17022bc21ed9SDavid Malone } 17032bc21ed9SDavid Malone } 1704df8bae1dSRodney W. Grimes } 1705df8bae1dSRodney W. Grimes m0 = m0->m_act; 1706df8bae1dSRodney W. Grimes } 1707df8bae1dSRodney W. Grimes } 1708df8bae1dSRodney W. Grimes 1709f708ef1bSPoul-Henning Kamp static void 1710df8bae1dSRodney W. Grimes unp_mark(fp) 1711df8bae1dSRodney W. Grimes struct file *fp; 1712df8bae1dSRodney W. Grimes { 1713426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) 1714df8bae1dSRodney W. Grimes return; 1715df8bae1dSRodney W. Grimes unp_defer++; 1716426da3bcSAlfred Perlstein fp->f_gcflag |= (FMARK|FDEFER); 1717df8bae1dSRodney W. Grimes } 1718df8bae1dSRodney W. Grimes 1719f708ef1bSPoul-Henning Kamp static void 1720df8bae1dSRodney W. Grimes unp_discard(fp) 1721df8bae1dSRodney W. Grimes struct file *fp; 1722df8bae1dSRodney W. Grimes { 1723426da3bcSAlfred Perlstein FILE_LOCK(fp); 1724df8bae1dSRodney W. Grimes fp->f_msgcount--; 1725df8bae1dSRodney W. Grimes unp_rights--; 1726426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1727b40ce416SJulian Elischer (void) closef(fp, (struct thread *)NULL); 1728df8bae1dSRodney W. Grimes } 1729