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 * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 33748e0b0aSGarrett Wollman * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94 34df8bae1dSRodney W. Grimes */ 35df8bae1dSRodney W. Grimes 36677b542eSDavid E. O'Brien #include <sys/cdefs.h> 37677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 38677b542eSDavid E. O'Brien 39335654d7SRobert Watson #include "opt_mac.h" 40335654d7SRobert Watson 41df8bae1dSRodney W. Grimes #include <sys/param.h> 42fb919e4dSMark Murray #include <sys/domain.h> 43960ed29cSSeigo Tanimura #include <sys/fcntl.h> 44d826c479SBruce Evans #include <sys/malloc.h> /* XXX must be before <sys/file.h> */ 45639acc13SGarrett Wollman #include <sys/file.h> 46960ed29cSSeigo Tanimura #include <sys/filedesc.h> 47960ed29cSSeigo Tanimura #include <sys/jail.h> 48960ed29cSSeigo Tanimura #include <sys/kernel.h> 49960ed29cSSeigo Tanimura #include <sys/lock.h> 506ea48a90SRobert Watson #include <sys/mac.h> 51639acc13SGarrett Wollman #include <sys/mbuf.h> 52960ed29cSSeigo Tanimura #include <sys/mutex.h> 53639acc13SGarrett Wollman #include <sys/namei.h> 54639acc13SGarrett Wollman #include <sys/proc.h> 55df8bae1dSRodney W. Grimes #include <sys/protosw.h> 56960ed29cSSeigo Tanimura #include <sys/resourcevar.h> 57df8bae1dSRodney W. Grimes #include <sys/socket.h> 58df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 59960ed29cSSeigo Tanimura #include <sys/signalvar.h> 60df8bae1dSRodney W. Grimes #include <sys/stat.h> 61960ed29cSSeigo Tanimura #include <sys/sx.h> 62639acc13SGarrett Wollman #include <sys/sysctl.h> 63960ed29cSSeigo Tanimura #include <sys/systm.h> 64639acc13SGarrett Wollman #include <sys/un.h> 6598271db4SGarrett Wollman #include <sys/unpcb.h> 66639acc13SGarrett Wollman #include <sys/vnode.h> 67df8bae1dSRodney W. Grimes 689e9d298aSJeff Roberson #include <vm/uma.h> 6998271db4SGarrett Wollman 709e9d298aSJeff Roberson static uma_zone_t unp_zone; 7198271db4SGarrett Wollman static unp_gen_t unp_gencnt; 7298271db4SGarrett Wollman static u_int unp_count; 7398271db4SGarrett Wollman 7498271db4SGarrett Wollman static struct unp_head unp_shead, unp_dhead; 7598271db4SGarrett Wollman 76df8bae1dSRodney W. Grimes /* 77df8bae1dSRodney W. Grimes * Unix communications domain. 78df8bae1dSRodney W. Grimes * 79df8bae1dSRodney W. Grimes * TODO: 80df8bae1dSRodney W. Grimes * SEQPACKET, RDM 81df8bae1dSRodney W. Grimes * rethink name space problems 82df8bae1dSRodney W. Grimes * need a proper out-of-band 8398271db4SGarrett Wollman * lock pushdown 84df8bae1dSRodney W. Grimes */ 85f708ef1bSPoul-Henning Kamp static struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 86f708ef1bSPoul-Henning Kamp static ino_t unp_ino; /* prototype for fake inode numbers */ 87f708ef1bSPoul-Henning Kamp 884d77a549SAlfred Perlstein static int unp_attach(struct socket *); 894d77a549SAlfred Perlstein static void unp_detach(struct unpcb *); 904d77a549SAlfred Perlstein static int unp_bind(struct unpcb *,struct sockaddr *, struct thread *); 9170f52b48SBruce Evans static int unp_connect(struct socket *,struct sockaddr *, struct thread *); 924d77a549SAlfred Perlstein static void unp_disconnect(struct unpcb *); 934d77a549SAlfred Perlstein static void unp_shutdown(struct unpcb *); 944d77a549SAlfred Perlstein static void unp_drop(struct unpcb *, int); 954d77a549SAlfred Perlstein static void unp_gc(void); 964d77a549SAlfred Perlstein static void unp_scan(struct mbuf *, void (*)(struct file *)); 974d77a549SAlfred Perlstein static void unp_mark(struct file *); 984d77a549SAlfred Perlstein static void unp_discard(struct file *); 994d77a549SAlfred Perlstein static void unp_freerights(struct file **, int); 1004d77a549SAlfred Perlstein static int unp_internalize(struct mbuf **, struct thread *); 1014d77a549SAlfred Perlstein static int unp_listen(struct unpcb *, struct thread *); 102f708ef1bSPoul-Henning Kamp 103a29f300eSGarrett Wollman static int 104a29f300eSGarrett Wollman uipc_abort(struct socket *so) 105df8bae1dSRodney W. Grimes { 106df8bae1dSRodney W. Grimes struct unpcb *unp = sotounpcb(so); 107df8bae1dSRodney W. Grimes 108fc3fcacfSRobert Watson if (unp == NULL) 109e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 110a29f300eSGarrett Wollman unp_drop(unp, ECONNABORTED); 111ddb7d629SIan Dowse unp_detach(unp); 112ddb7d629SIan Dowse sotryfree(so); 113e5aeaa0cSDag-Erling Smørgrav return (0); 114df8bae1dSRodney W. Grimes } 115df8bae1dSRodney W. Grimes 116a29f300eSGarrett Wollman static int 11757bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam) 118a29f300eSGarrett Wollman { 119a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 120df8bae1dSRodney W. Grimes 121fc3fcacfSRobert Watson if (unp == NULL) 122e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 123df8bae1dSRodney W. Grimes 124df8bae1dSRodney W. Grimes /* 125df8bae1dSRodney W. Grimes * Pass back name of connected socket, 126df8bae1dSRodney W. Grimes * if it was bound and we are still connected 127df8bae1dSRodney W. Grimes * (our peer may have closed already!). 128df8bae1dSRodney W. Grimes */ 129fc3fcacfSRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr != NULL) { 130746e5bf0SRobert Watson *nam = sodupsockaddr( 131746e5bf0SRobert Watson (struct sockaddr *)unp->unp_conn->unp_addr, M_WAITOK); 132df8bae1dSRodney W. Grimes } else { 133746e5bf0SRobert Watson *nam = sodupsockaddr((struct sockaddr *)&sun_noname, 134746e5bf0SRobert Watson M_WAITOK); 135df8bae1dSRodney W. Grimes } 136e5aeaa0cSDag-Erling Smørgrav return (0); 137a29f300eSGarrett Wollman } 138df8bae1dSRodney W. Grimes 139a29f300eSGarrett Wollman static int 140b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td) 141a29f300eSGarrett Wollman { 142a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 143df8bae1dSRodney W. Grimes 144fc3fcacfSRobert Watson if (unp != NULL) 145e5aeaa0cSDag-Erling Smørgrav return (EISCONN); 146e5aeaa0cSDag-Erling Smørgrav return (unp_attach(so)); 147a29f300eSGarrett Wollman } 148a29f300eSGarrett Wollman 149a29f300eSGarrett Wollman static int 150b40ce416SJulian Elischer uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 151a29f300eSGarrett Wollman { 152a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 153a29f300eSGarrett Wollman 154fc3fcacfSRobert Watson if (unp == NULL) 155e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 156a29f300eSGarrett Wollman 157e5aeaa0cSDag-Erling Smørgrav return (unp_bind(unp, nam, td)); 158a29f300eSGarrett Wollman } 159a29f300eSGarrett Wollman 160a29f300eSGarrett Wollman static int 161b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 162a29f300eSGarrett Wollman { 163a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 164a29f300eSGarrett Wollman 165fc3fcacfSRobert Watson if (unp == NULL) 166e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 167e5aeaa0cSDag-Erling Smørgrav return (unp_connect(so, nam, curthread)); 168a29f300eSGarrett Wollman } 169a29f300eSGarrett Wollman 170a29f300eSGarrett Wollman static int 171a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2) 172a29f300eSGarrett Wollman { 173a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so1); 174a29f300eSGarrett Wollman 175fc3fcacfSRobert Watson if (unp == NULL) 176e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 177a29f300eSGarrett Wollman 178e5aeaa0cSDag-Erling Smørgrav return (unp_connect2(so1, so2)); 179a29f300eSGarrett Wollman } 180a29f300eSGarrett Wollman 181a29f300eSGarrett Wollman /* control is EOPNOTSUPP */ 182a29f300eSGarrett Wollman 183a29f300eSGarrett Wollman static int 184a29f300eSGarrett Wollman uipc_detach(struct socket *so) 185a29f300eSGarrett Wollman { 186a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 187a29f300eSGarrett Wollman 188fc3fcacfSRobert Watson if (unp == NULL) 189e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 190a29f300eSGarrett Wollman 191a29f300eSGarrett Wollman unp_detach(unp); 192e5aeaa0cSDag-Erling Smørgrav return (0); 193a29f300eSGarrett Wollman } 194a29f300eSGarrett Wollman 195a29f300eSGarrett Wollman static int 196a29f300eSGarrett Wollman uipc_disconnect(struct socket *so) 197a29f300eSGarrett Wollman { 198a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 199a29f300eSGarrett Wollman 200fc3fcacfSRobert Watson if (unp == NULL) 201e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 202a29f300eSGarrett Wollman unp_disconnect(unp); 203e5aeaa0cSDag-Erling Smørgrav return (0); 204a29f300eSGarrett Wollman } 205a29f300eSGarrett Wollman 206a29f300eSGarrett Wollman static int 207b40ce416SJulian Elischer uipc_listen(struct socket *so, struct thread *td) 208a29f300eSGarrett Wollman { 209a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 210a29f300eSGarrett Wollman 211fc3fcacfSRobert Watson if (unp == NULL || unp->unp_vnode == NULL) 212e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 213e5aeaa0cSDag-Erling Smørgrav return (unp_listen(unp, td)); 214a29f300eSGarrett Wollman } 215a29f300eSGarrett Wollman 216a29f300eSGarrett Wollman static int 21757bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 218a29f300eSGarrett Wollman { 219a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 220a29f300eSGarrett Wollman 221fc3fcacfSRobert Watson if (unp == NULL) 222e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 223fc3fcacfSRobert Watson if (unp->unp_conn != NULL && unp->unp_conn->unp_addr != NULL) 224746e5bf0SRobert Watson *nam = sodupsockaddr( 225746e5bf0SRobert Watson (struct sockaddr *)unp->unp_conn->unp_addr, M_WAITOK); 226bdc5f6a3SHajimu UMEMOTO else { 227bdc5f6a3SHajimu UMEMOTO /* 228bdc5f6a3SHajimu UMEMOTO * XXX: It seems that this test always fails even when 229bdc5f6a3SHajimu UMEMOTO * connection is established. So, this else clause is 230bdc5f6a3SHajimu UMEMOTO * added as workaround to return PF_LOCAL sockaddr. 231bdc5f6a3SHajimu UMEMOTO */ 232746e5bf0SRobert Watson *nam = sodupsockaddr((struct sockaddr *)&sun_noname, 233746e5bf0SRobert Watson M_WAITOK); 234bdc5f6a3SHajimu UMEMOTO } 235e5aeaa0cSDag-Erling Smørgrav return (0); 236a29f300eSGarrett Wollman } 237a29f300eSGarrett Wollman 238a29f300eSGarrett Wollman static int 239a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 240a29f300eSGarrett Wollman { 241a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 242a29f300eSGarrett Wollman struct socket *so2; 2436aef685fSBrian Feldman u_long newhiwat; 244a29f300eSGarrett Wollman 245fc3fcacfSRobert Watson if (unp == NULL) 246e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 247df8bae1dSRodney W. Grimes switch (so->so_type) { 248df8bae1dSRodney W. Grimes case SOCK_DGRAM: 249a29f300eSGarrett Wollman panic("uipc_rcvd DGRAM?"); 250df8bae1dSRodney W. Grimes /*NOTREACHED*/ 251df8bae1dSRodney W. Grimes 252df8bae1dSRodney W. Grimes case SOCK_STREAM: 253fc3fcacfSRobert Watson if (unp->unp_conn == NULL) 254df8bae1dSRodney W. Grimes break; 255df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 256df8bae1dSRodney W. Grimes /* 257df8bae1dSRodney W. Grimes * Adjust backpressure on sender 258df8bae1dSRodney W. Grimes * and wakeup any waiting to write. 259df8bae1dSRodney W. Grimes */ 260ff8b0106SBrian Feldman so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt; 261ff8b0106SBrian Feldman unp->unp_mbcnt = so->so_rcv.sb_mbcnt; 2626aef685fSBrian Feldman newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - 2636aef685fSBrian Feldman so->so_rcv.sb_cc; 264f535380cSDon Lewis (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat, 2656aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 266ff8b0106SBrian Feldman unp->unp_cc = so->so_rcv.sb_cc; 267df8bae1dSRodney W. Grimes sowwakeup(so2); 268df8bae1dSRodney W. Grimes break; 269df8bae1dSRodney W. Grimes 270df8bae1dSRodney W. Grimes default: 271a29f300eSGarrett Wollman panic("uipc_rcvd unknown socktype"); 272df8bae1dSRodney W. Grimes } 273e5aeaa0cSDag-Erling Smørgrav return (0); 274a29f300eSGarrett Wollman } 275df8bae1dSRodney W. Grimes 276a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 277a29f300eSGarrett Wollman 278a29f300eSGarrett Wollman static int 27957bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 280b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 281a29f300eSGarrett Wollman { 282a29f300eSGarrett Wollman int error = 0; 283a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 284a29f300eSGarrett Wollman struct socket *so2; 2856aef685fSBrian Feldman u_long newhiwat; 286a29f300eSGarrett Wollman 287fc3fcacfSRobert Watson if (unp == NULL) { 288a29f300eSGarrett Wollman error = EINVAL; 289a29f300eSGarrett Wollman goto release; 290a29f300eSGarrett Wollman } 291a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 292a29f300eSGarrett Wollman error = EOPNOTSUPP; 293a29f300eSGarrett Wollman goto release; 294a29f300eSGarrett Wollman } 295a29f300eSGarrett Wollman 296fc3fcacfSRobert Watson if (control != NULL && (error = unp_internalize(&control, td))) 297a29f300eSGarrett Wollman goto release; 298df8bae1dSRodney W. Grimes 299a29f300eSGarrett Wollman switch (so->so_type) { 300a29f300eSGarrett Wollman case SOCK_DGRAM: 301a29f300eSGarrett Wollman { 302df8bae1dSRodney W. Grimes struct sockaddr *from; 303df8bae1dSRodney W. Grimes 304fc3fcacfSRobert Watson if (nam != NULL) { 305fc3fcacfSRobert Watson if (unp->unp_conn != NULL) { 306df8bae1dSRodney W. Grimes error = EISCONN; 307df8bae1dSRodney W. Grimes break; 308df8bae1dSRodney W. Grimes } 309b40ce416SJulian Elischer error = unp_connect(so, nam, td); 310df8bae1dSRodney W. Grimes if (error) 311df8bae1dSRodney W. Grimes break; 312df8bae1dSRodney W. Grimes } else { 313fc3fcacfSRobert Watson if (unp->unp_conn == NULL) { 314df8bae1dSRodney W. Grimes error = ENOTCONN; 315df8bae1dSRodney W. Grimes break; 316df8bae1dSRodney W. Grimes } 317df8bae1dSRodney W. Grimes } 318df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 319fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 32057bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 321df8bae1dSRodney W. Grimes else 322df8bae1dSRodney W. Grimes from = &sun_noname; 323df8bae1dSRodney W. Grimes if (sbappendaddr(&so2->so_rcv, from, m, control)) { 324df8bae1dSRodney W. Grimes sorwakeup(so2); 325fc3fcacfSRobert Watson m = NULL; 326fc3fcacfSRobert Watson control = NULL; 327e5aeaa0cSDag-Erling Smørgrav } else { 328df8bae1dSRodney W. Grimes error = ENOBUFS; 329e5aeaa0cSDag-Erling Smørgrav } 330fc3fcacfSRobert Watson if (nam != NULL) 331df8bae1dSRodney W. Grimes unp_disconnect(unp); 332df8bae1dSRodney W. Grimes break; 333df8bae1dSRodney W. Grimes } 334df8bae1dSRodney W. Grimes 335df8bae1dSRodney W. Grimes case SOCK_STREAM: 3366b8fda4dSGarrett Wollman /* Connect if not connected yet. */ 3376b8fda4dSGarrett Wollman /* 3386b8fda4dSGarrett Wollman * Note: A better implementation would complain 339402cc72dSDavid Greenman * if not equal to the peer's address. 3406b8fda4dSGarrett Wollman */ 341402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 342fc3fcacfSRobert Watson if (nam != NULL) { 343b40ce416SJulian Elischer error = unp_connect(so, nam, td); 344402cc72dSDavid Greenman if (error) 3456b8fda4dSGarrett Wollman break; /* XXX */ 346402cc72dSDavid Greenman } else { 347402cc72dSDavid Greenman error = ENOTCONN; 348402cc72dSDavid Greenman break; 349402cc72dSDavid Greenman } 350402cc72dSDavid Greenman } 351402cc72dSDavid Greenman 352df8bae1dSRodney W. Grimes if (so->so_state & SS_CANTSENDMORE) { 353df8bae1dSRodney W. Grimes error = EPIPE; 354df8bae1dSRodney W. Grimes break; 355df8bae1dSRodney W. Grimes } 356fc3fcacfSRobert Watson if (unp->unp_conn == NULL) 357a29f300eSGarrett Wollman panic("uipc_send connected but no connection?"); 358df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 359df8bae1dSRodney W. Grimes /* 360df8bae1dSRodney W. Grimes * Send to paired receive port, and then reduce 361df8bae1dSRodney W. Grimes * send buffer hiwater marks to maintain backpressure. 362df8bae1dSRodney W. Grimes * Wake up readers. 363df8bae1dSRodney W. Grimes */ 364fc3fcacfSRobert Watson if (control != NULL) { 365ff8b0106SBrian Feldman if (sbappendcontrol(&so2->so_rcv, m, control)) 366fc3fcacfSRobert Watson control = NULL; 367e5aeaa0cSDag-Erling Smørgrav } else { 368ff8b0106SBrian Feldman sbappend(&so2->so_rcv, m); 369e5aeaa0cSDag-Erling Smørgrav } 370ff8b0106SBrian Feldman so->so_snd.sb_mbmax -= 371ff8b0106SBrian Feldman so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt; 372ff8b0106SBrian Feldman unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt; 3736aef685fSBrian Feldman newhiwat = so->so_snd.sb_hiwat - 3746aef685fSBrian Feldman (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc); 375f535380cSDon Lewis (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat, 3766aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 377ff8b0106SBrian Feldman unp->unp_conn->unp_cc = so2->so_rcv.sb_cc; 378df8bae1dSRodney W. Grimes sorwakeup(so2); 379fc3fcacfSRobert Watson m = NULL; 380df8bae1dSRodney W. Grimes break; 381df8bae1dSRodney W. Grimes 382df8bae1dSRodney W. Grimes default: 383a29f300eSGarrett Wollman panic("uipc_send unknown socktype"); 384df8bae1dSRodney W. Grimes } 385a29f300eSGarrett Wollman 3866b8fda4dSGarrett Wollman /* 3876b8fda4dSGarrett Wollman * SEND_EOF is equivalent to a SEND followed by 3886b8fda4dSGarrett Wollman * a SHUTDOWN. 3896b8fda4dSGarrett Wollman */ 390a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 3916b8fda4dSGarrett Wollman socantsendmore(so); 3926b8fda4dSGarrett Wollman unp_shutdown(unp); 3936b8fda4dSGarrett Wollman } 394df8bae1dSRodney W. Grimes 395fc3fcacfSRobert Watson if (control != NULL && error != 0) 396bd508d39SDon Lewis unp_dispose(control); 397bd508d39SDon Lewis 398a29f300eSGarrett Wollman release: 399fc3fcacfSRobert Watson if (control != NULL) 400a29f300eSGarrett Wollman m_freem(control); 401fc3fcacfSRobert Watson if (m != NULL) 402a29f300eSGarrett Wollman m_freem(m); 403e5aeaa0cSDag-Erling Smørgrav return (error); 404a29f300eSGarrett Wollman } 405df8bae1dSRodney W. Grimes 406a29f300eSGarrett Wollman static int 407a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 408a29f300eSGarrett Wollman { 409a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 410a29f300eSGarrett Wollman struct socket *so2; 411a29f300eSGarrett Wollman 412fc3fcacfSRobert Watson if (unp == NULL) 413e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 414a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 415fc3fcacfSRobert Watson if (so->so_type == SOCK_STREAM && unp->unp_conn != NULL) { 416df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 417a29f300eSGarrett Wollman sb->st_blksize += so2->so_rcv.sb_cc; 418df8bae1dSRodney W. Grimes } 419bfbb9ce6SPoul-Henning Kamp sb->st_dev = NOUDEV; 420df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 4216f782c46SJeffrey Hsu unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 422a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 423df8bae1dSRodney W. Grimes return (0); 424a29f300eSGarrett Wollman } 425df8bae1dSRodney W. Grimes 426a29f300eSGarrett Wollman static int 427a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 428a29f300eSGarrett Wollman { 429a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 430df8bae1dSRodney W. Grimes 431fc3fcacfSRobert Watson if (unp == NULL) 432e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 433a29f300eSGarrett Wollman socantsendmore(so); 434a29f300eSGarrett Wollman unp_shutdown(unp); 435e5aeaa0cSDag-Erling Smørgrav return (0); 436a29f300eSGarrett Wollman } 437df8bae1dSRodney W. Grimes 438a29f300eSGarrett Wollman static int 43957bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 440a29f300eSGarrett Wollman { 441a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 442a29f300eSGarrett Wollman 443fc3fcacfSRobert Watson if (unp == NULL) 444e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 445fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 446746e5bf0SRobert Watson *nam = sodupsockaddr((struct sockaddr *)unp->unp_addr, 447746e5bf0SRobert Watson M_WAITOK); 44883f3198bSThomas Moestl else 449746e5bf0SRobert Watson *nam = sodupsockaddr((struct sockaddr *)&sun_noname, 450746e5bf0SRobert Watson M_WAITOK); 451e5aeaa0cSDag-Erling Smørgrav return (0); 452df8bae1dSRodney W. Grimes } 453a29f300eSGarrett Wollman 454a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = { 455a29f300eSGarrett Wollman uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect, 456a29f300eSGarrett Wollman uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect, 457a29f300eSGarrett Wollman uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp, 458a29f300eSGarrett Wollman uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr, 459a557af22SRobert Watson sosend, soreceive, sopoll, pru_sosetlabel_null 460a29f300eSGarrett Wollman }; 461df8bae1dSRodney W. Grimes 4620c1bb4fbSDima Dorfman int 4630c1bb4fbSDima Dorfman uipc_ctloutput(so, sopt) 4640c1bb4fbSDima Dorfman struct socket *so; 4650c1bb4fbSDima Dorfman struct sockopt *sopt; 4660c1bb4fbSDima Dorfman { 4670c1bb4fbSDima Dorfman struct unpcb *unp = sotounpcb(so); 4680c1bb4fbSDima Dorfman int error; 4690c1bb4fbSDima Dorfman 4700c1bb4fbSDima Dorfman switch (sopt->sopt_dir) { 4710c1bb4fbSDima Dorfman case SOPT_GET: 4720c1bb4fbSDima Dorfman switch (sopt->sopt_name) { 4730c1bb4fbSDima Dorfman case LOCAL_PEERCRED: 4740c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC) 4750c1bb4fbSDima Dorfman error = sooptcopyout(sopt, &unp->unp_peercred, 4760c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 4770c1bb4fbSDima Dorfman else { 4780c1bb4fbSDima Dorfman if (so->so_type == SOCK_STREAM) 4790c1bb4fbSDima Dorfman error = ENOTCONN; 4800c1bb4fbSDima Dorfman else 4810c1bb4fbSDima Dorfman error = EINVAL; 4820c1bb4fbSDima Dorfman } 4830c1bb4fbSDima Dorfman break; 4840c1bb4fbSDima Dorfman default: 4850c1bb4fbSDima Dorfman error = EOPNOTSUPP; 4860c1bb4fbSDima Dorfman break; 4870c1bb4fbSDima Dorfman } 4880c1bb4fbSDima Dorfman break; 4890c1bb4fbSDima Dorfman case SOPT_SET: 4900c1bb4fbSDima Dorfman default: 4910c1bb4fbSDima Dorfman error = EOPNOTSUPP; 4920c1bb4fbSDima Dorfman break; 4930c1bb4fbSDima Dorfman } 4940c1bb4fbSDima Dorfman return (error); 4950c1bb4fbSDima Dorfman } 4960c1bb4fbSDima Dorfman 497df8bae1dSRodney W. Grimes /* 498df8bae1dSRodney W. Grimes * Both send and receive buffers are allocated PIPSIZ bytes of buffering 499df8bae1dSRodney W. Grimes * for stream sockets, although the total for sender and receiver is 500df8bae1dSRodney W. Grimes * actually only PIPSIZ. 501df8bae1dSRodney W. Grimes * Datagram sockets really use the sendspace as the maximum datagram size, 502df8bae1dSRodney W. Grimes * and don't really want to reserve the sendspace. Their recvspace should 503df8bae1dSRodney W. Grimes * be large enough for at least one max-size datagram plus address. 504df8bae1dSRodney W. Grimes */ 5055dce41c5SJohn Dyson #ifndef PIPSIZ 5065dce41c5SJohn Dyson #define PIPSIZ 8192 5075dce41c5SJohn Dyson #endif 508f708ef1bSPoul-Henning Kamp static u_long unpst_sendspace = PIPSIZ; 509f708ef1bSPoul-Henning Kamp static u_long unpst_recvspace = PIPSIZ; 510f708ef1bSPoul-Henning Kamp static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 511f708ef1bSPoul-Henning Kamp static u_long unpdg_recvspace = 4*1024; 512df8bae1dSRodney W. Grimes 513f708ef1bSPoul-Henning Kamp static int unp_rights; /* file descriptors in flight */ 514df8bae1dSRodney W. Grimes 515ce02431fSDoug Rabson SYSCTL_DECL(_net_local_stream); 516639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 517639acc13SGarrett Wollman &unpst_sendspace, 0, ""); 518639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 519639acc13SGarrett Wollman &unpst_recvspace, 0, ""); 520ce02431fSDoug Rabson SYSCTL_DECL(_net_local_dgram); 521639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 522639acc13SGarrett Wollman &unpdg_sendspace, 0, ""); 523639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 524639acc13SGarrett Wollman &unpdg_recvspace, 0, ""); 525ce02431fSDoug Rabson SYSCTL_DECL(_net_local); 526639acc13SGarrett Wollman SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); 527639acc13SGarrett Wollman 528f708ef1bSPoul-Henning Kamp static int 529df8bae1dSRodney W. Grimes unp_attach(so) 530df8bae1dSRodney W. Grimes struct socket *so; 531df8bae1dSRodney W. Grimes { 532df8bae1dSRodney W. Grimes register struct unpcb *unp; 533df8bae1dSRodney W. Grimes int error; 534df8bae1dSRodney W. Grimes 535df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 536df8bae1dSRodney W. Grimes switch (so->so_type) { 537df8bae1dSRodney W. Grimes 538df8bae1dSRodney W. Grimes case SOCK_STREAM: 539df8bae1dSRodney W. Grimes error = soreserve(so, unpst_sendspace, unpst_recvspace); 540df8bae1dSRodney W. Grimes break; 541df8bae1dSRodney W. Grimes 542df8bae1dSRodney W. Grimes case SOCK_DGRAM: 543df8bae1dSRodney W. Grimes error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 544df8bae1dSRodney W. Grimes break; 545df8bae1dSRodney W. Grimes 546df8bae1dSRodney W. Grimes default: 547df8bae1dSRodney W. Grimes panic("unp_attach"); 548df8bae1dSRodney W. Grimes } 549df8bae1dSRodney W. Grimes if (error) 550df8bae1dSRodney W. Grimes return (error); 551df8bae1dSRodney W. Grimes } 552a163d034SWarner Losh unp = uma_zalloc(unp_zone, M_WAITOK); 55357bf258eSGarrett Wollman if (unp == NULL) 554df8bae1dSRodney W. Grimes return (ENOBUFS); 55557bf258eSGarrett Wollman bzero(unp, sizeof *unp); 55698271db4SGarrett Wollman unp->unp_gencnt = ++unp_gencnt; 55798271db4SGarrett Wollman unp_count++; 55898271db4SGarrett Wollman LIST_INIT(&unp->unp_refs); 559df8bae1dSRodney W. Grimes unp->unp_socket = so; 56098271db4SGarrett Wollman LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead 56198271db4SGarrett Wollman : &unp_shead, unp, unp_link); 562210a5a71SAlfred Perlstein so->so_pcb = unp; 563df8bae1dSRodney W. Grimes return (0); 564df8bae1dSRodney W. Grimes } 565df8bae1dSRodney W. Grimes 566f708ef1bSPoul-Henning Kamp static void 567df8bae1dSRodney W. Grimes unp_detach(unp) 568df8bae1dSRodney W. Grimes register struct unpcb *unp; 569df8bae1dSRodney W. Grimes { 57098271db4SGarrett Wollman LIST_REMOVE(unp, unp_link); 57198271db4SGarrett Wollman unp->unp_gencnt = ++unp_gencnt; 57298271db4SGarrett Wollman --unp_count; 573fc3fcacfSRobert Watson if (unp->unp_vnode != NULL) { 574fc3fcacfSRobert Watson unp->unp_vnode->v_socket = NULL; 575df8bae1dSRodney W. Grimes vrele(unp->unp_vnode); 576fc3fcacfSRobert Watson unp->unp_vnode = NULL; 577df8bae1dSRodney W. Grimes } 578fc3fcacfSRobert Watson if (unp->unp_conn != NULL) 579df8bae1dSRodney W. Grimes unp_disconnect(unp); 5802e3c8fcbSPoul-Henning Kamp while (!LIST_EMPTY(&unp->unp_refs)) 5812e3c8fcbSPoul-Henning Kamp unp_drop(LIST_FIRST(&unp->unp_refs), ECONNRESET); 582df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 583fc3fcacfSRobert Watson unp->unp_socket->so_pcb = NULL; 584df8bae1dSRodney W. Grimes if (unp_rights) { 585df8bae1dSRodney W. Grimes /* 586df8bae1dSRodney W. Grimes * Normally the receive buffer is flushed later, 587df8bae1dSRodney W. Grimes * in sofree, but if our receive buffer holds references 588df8bae1dSRodney W. Grimes * to descriptors that are now garbage, we will dispose 589df8bae1dSRodney W. Grimes * of those descriptor references after the garbage collector 590df8bae1dSRodney W. Grimes * gets them (resulting in a "panic: closef: count < 0"). 591df8bae1dSRodney W. Grimes */ 592df8bae1dSRodney W. Grimes sorflush(unp->unp_socket); 593df8bae1dSRodney W. Grimes unp_gc(); 594df8bae1dSRodney W. Grimes } 595fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 59657bf258eSGarrett Wollman FREE(unp->unp_addr, M_SONAME); 5979e9d298aSJeff Roberson uma_zfree(unp_zone, unp); 598df8bae1dSRodney W. Grimes } 599df8bae1dSRodney W. Grimes 600f708ef1bSPoul-Henning Kamp static int 601b40ce416SJulian Elischer unp_bind(unp, nam, td) 602df8bae1dSRodney W. Grimes struct unpcb *unp; 60357bf258eSGarrett Wollman struct sockaddr *nam; 604b40ce416SJulian Elischer struct thread *td; 605df8bae1dSRodney W. Grimes { 60657bf258eSGarrett Wollman struct sockaddr_un *soun = (struct sockaddr_un *)nam; 607f2a2857bSKirk McKusick struct vnode *vp; 608f2a2857bSKirk McKusick struct mount *mp; 609df8bae1dSRodney W. Grimes struct vattr vattr; 61057bf258eSGarrett Wollman int error, namelen; 611df8bae1dSRodney W. Grimes struct nameidata nd; 6128f364875SJulian Elischer char *buf; 613df8bae1dSRodney W. Grimes 614df8bae1dSRodney W. Grimes if (unp->unp_vnode != NULL) 615df8bae1dSRodney W. Grimes return (EINVAL); 61655c85568SRobert Drehmel 61757bf258eSGarrett Wollman namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 61857bf258eSGarrett Wollman if (namelen <= 0) 619e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 62055c85568SRobert Drehmel 621a163d034SWarner Losh buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 62255c85568SRobert Drehmel strlcpy(buf, soun->sun_path, namelen + 1); 62355c85568SRobert Drehmel 624f2a2857bSKirk McKusick restart: 625b65f6f6bSRobert Watson NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME, UIO_SYSSPACE, 626b40ce416SJulian Elischer buf, td); 627df8bae1dSRodney W. Grimes /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 628797f2d22SPoul-Henning Kamp error = namei(&nd); 6298f364875SJulian Elischer if (error) { 6308f364875SJulian Elischer free(buf, M_TEMP); 631ad4ff090SJulian Elischer return (error); 6328f364875SJulian Elischer } 633df8bae1dSRodney W. Grimes vp = nd.ni_vp; 634f2a2857bSKirk McKusick if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 635762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 636df8bae1dSRodney W. Grimes if (nd.ni_dvp == vp) 637df8bae1dSRodney W. Grimes vrele(nd.ni_dvp); 638df8bae1dSRodney W. Grimes else 639df8bae1dSRodney W. Grimes vput(nd.ni_dvp); 640f2a2857bSKirk McKusick if (vp != NULL) { 641df8bae1dSRodney W. Grimes vrele(vp); 6428f364875SJulian Elischer free(buf, M_TEMP); 643df8bae1dSRodney W. Grimes return (EADDRINUSE); 644df8bae1dSRodney W. Grimes } 6458f364875SJulian Elischer error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 6468f364875SJulian Elischer if (error) { 6478f364875SJulian Elischer free(buf, M_TEMP); 648f2a2857bSKirk McKusick return (error); 6498f364875SJulian Elischer } 650f2a2857bSKirk McKusick goto restart; 651f2a2857bSKirk McKusick } 652df8bae1dSRodney W. Grimes VATTR_NULL(&vattr); 653df8bae1dSRodney W. Grimes vattr.va_type = VSOCK; 654b40ce416SJulian Elischer vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 6556ea48a90SRobert Watson #ifdef MAC 6566ea48a90SRobert Watson error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 6576ea48a90SRobert Watson &vattr); 6586151efaaSRobert Watson #endif 6596ea48a90SRobert Watson if (error == 0) { 660a854ed98SJohn Baldwin VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE); 6617be2d300SMike Smith error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 6626ea48a90SRobert Watson } 663762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 6647be2d300SMike Smith vput(nd.ni_dvp); 6658f364875SJulian Elischer if (error) { 6668f364875SJulian Elischer free(buf, M_TEMP); 667df8bae1dSRodney W. Grimes return (error); 6688f364875SJulian Elischer } 669df8bae1dSRodney W. Grimes vp = nd.ni_vp; 670df8bae1dSRodney W. Grimes vp->v_socket = unp->unp_socket; 671df8bae1dSRodney W. Grimes unp->unp_vnode = vp; 672746e5bf0SRobert Watson unp->unp_addr = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK); 673b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 674f2a2857bSKirk McKusick vn_finished_write(mp); 6758f364875SJulian Elischer free(buf, M_TEMP); 676df8bae1dSRodney W. Grimes return (0); 677df8bae1dSRodney W. Grimes } 678df8bae1dSRodney W. Grimes 679f708ef1bSPoul-Henning Kamp static int 680b40ce416SJulian Elischer unp_connect(so, nam, td) 681df8bae1dSRodney W. Grimes struct socket *so; 68257bf258eSGarrett Wollman struct sockaddr *nam; 683b40ce416SJulian Elischer struct thread *td; 684df8bae1dSRodney W. Grimes { 68557bf258eSGarrett Wollman register struct sockaddr_un *soun = (struct sockaddr_un *)nam; 686df8bae1dSRodney W. Grimes register struct vnode *vp; 687df8bae1dSRodney W. Grimes register struct socket *so2, *so3; 6880c1bb4fbSDima Dorfman struct unpcb *unp, *unp2, *unp3; 68957bf258eSGarrett Wollman int error, len; 690df8bae1dSRodney W. Grimes struct nameidata nd; 69157bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 692df8bae1dSRodney W. Grimes 69357bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 69457bf258eSGarrett Wollman if (len <= 0) 695e5aeaa0cSDag-Erling Smørgrav return (EINVAL); 69655c85568SRobert Drehmel strlcpy(buf, soun->sun_path, len + 1); 69757bf258eSGarrett Wollman 698b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td); 699797f2d22SPoul-Henning Kamp error = namei(&nd); 700797f2d22SPoul-Henning Kamp if (error) 701df8bae1dSRodney W. Grimes return (error); 702df8bae1dSRodney W. Grimes vp = nd.ni_vp; 703762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 704df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 705df8bae1dSRodney W. Grimes error = ENOTSOCK; 706df8bae1dSRodney W. Grimes goto bad; 707df8bae1dSRodney W. Grimes } 708a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 709797f2d22SPoul-Henning Kamp if (error) 710df8bae1dSRodney W. Grimes goto bad; 711df8bae1dSRodney W. Grimes so2 = vp->v_socket; 712fc3fcacfSRobert Watson if (so2 == NULL) { 713df8bae1dSRodney W. Grimes error = ECONNREFUSED; 714df8bae1dSRodney W. Grimes goto bad; 715df8bae1dSRodney W. Grimes } 716df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 717df8bae1dSRodney W. Grimes error = EPROTOTYPE; 718df8bae1dSRodney W. Grimes goto bad; 719df8bae1dSRodney W. Grimes } 720df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 7214cc20ab1SSeigo Tanimura if ((so2->so_options & SO_ACCEPTCONN) == 0 || 722fc3fcacfSRobert Watson (so3 = sonewconn(so2, 0)) == NULL) { 723df8bae1dSRodney W. Grimes error = ECONNREFUSED; 724df8bae1dSRodney W. Grimes goto bad; 725df8bae1dSRodney W. Grimes } 7260c1bb4fbSDima Dorfman unp = sotounpcb(so); 727df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 728df8bae1dSRodney W. Grimes unp3 = sotounpcb(so3); 729fc3fcacfSRobert Watson if (unp2->unp_addr != NULL) 73057bf258eSGarrett Wollman unp3->unp_addr = (struct sockaddr_un *) 731746e5bf0SRobert Watson sodupsockaddr((struct sockaddr *)unp2->unp_addr, 732746e5bf0SRobert Watson M_WAITOK); 7330c1bb4fbSDima Dorfman 7340c1bb4fbSDima Dorfman /* 7350c1bb4fbSDima Dorfman * unp_peercred management: 7360c1bb4fbSDima Dorfman * 7370c1bb4fbSDima Dorfman * The connecter's (client's) credentials are copied 7380c1bb4fbSDima Dorfman * from its process structure at the time of connect() 7390c1bb4fbSDima Dorfman * (which is now). 7400c1bb4fbSDima Dorfman */ 741a854ed98SJohn Baldwin cru2x(td->td_ucred, &unp3->unp_peercred); 7420c1bb4fbSDima Dorfman unp3->unp_flags |= UNP_HAVEPC; 7430c1bb4fbSDima Dorfman /* 7440c1bb4fbSDima Dorfman * The receiver's (server's) credentials are copied 7450c1bb4fbSDima Dorfman * from the unp_peercred member of socket on which the 7460c1bb4fbSDima Dorfman * former called listen(); unp_listen() cached that 7470c1bb4fbSDima Dorfman * process's credentials at that time so we can use 7480c1bb4fbSDima Dorfman * them now. 7490c1bb4fbSDima Dorfman */ 7500c1bb4fbSDima Dorfman KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 7510c1bb4fbSDima Dorfman ("unp_connect: listener without cached peercred")); 7520c1bb4fbSDima Dorfman memcpy(&unp->unp_peercred, &unp2->unp_peercred, 7530c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 7540c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPC; 755335654d7SRobert Watson #ifdef MAC 756335654d7SRobert Watson mac_set_socket_peer_from_socket(so, so3); 757335654d7SRobert Watson mac_set_socket_peer_from_socket(so3, so); 758335654d7SRobert Watson #endif 7590c1bb4fbSDima Dorfman 760df8bae1dSRodney W. Grimes so2 = so3; 761df8bae1dSRodney W. Grimes } 762df8bae1dSRodney W. Grimes error = unp_connect2(so, so2); 763df8bae1dSRodney W. Grimes bad: 764df8bae1dSRodney W. Grimes vput(vp); 765df8bae1dSRodney W. Grimes return (error); 766df8bae1dSRodney W. Grimes } 767df8bae1dSRodney W. Grimes 76826f9a767SRodney W. Grimes int 769df8bae1dSRodney W. Grimes unp_connect2(so, so2) 770df8bae1dSRodney W. Grimes register struct socket *so; 771df8bae1dSRodney W. Grimes register struct socket *so2; 772df8bae1dSRodney W. Grimes { 773df8bae1dSRodney W. Grimes register struct unpcb *unp = sotounpcb(so); 774df8bae1dSRodney W. Grimes register struct unpcb *unp2; 775df8bae1dSRodney W. Grimes 776df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 777df8bae1dSRodney W. Grimes return (EPROTOTYPE); 778df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 779df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 780df8bae1dSRodney W. Grimes switch (so->so_type) { 781df8bae1dSRodney W. Grimes 782df8bae1dSRodney W. Grimes case SOCK_DGRAM: 78398271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 784df8bae1dSRodney W. Grimes soisconnected(so); 785df8bae1dSRodney W. Grimes break; 786df8bae1dSRodney W. Grimes 787df8bae1dSRodney W. Grimes case SOCK_STREAM: 788df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 789df8bae1dSRodney W. Grimes soisconnected(so); 790df8bae1dSRodney W. Grimes soisconnected(so2); 791df8bae1dSRodney W. Grimes break; 792df8bae1dSRodney W. Grimes 793df8bae1dSRodney W. Grimes default: 794df8bae1dSRodney W. Grimes panic("unp_connect2"); 795df8bae1dSRodney W. Grimes } 796df8bae1dSRodney W. Grimes return (0); 797df8bae1dSRodney W. Grimes } 798df8bae1dSRodney W. Grimes 799f708ef1bSPoul-Henning Kamp static void 800df8bae1dSRodney W. Grimes unp_disconnect(unp) 801df8bae1dSRodney W. Grimes struct unpcb *unp; 802df8bae1dSRodney W. Grimes { 803df8bae1dSRodney W. Grimes register struct unpcb *unp2 = unp->unp_conn; 804df8bae1dSRodney W. Grimes 805fc3fcacfSRobert Watson if (unp2 == NULL) 806df8bae1dSRodney W. Grimes return; 807fc3fcacfSRobert Watson unp->unp_conn = NULL; 808df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 809df8bae1dSRodney W. Grimes 810df8bae1dSRodney W. Grimes case SOCK_DGRAM: 81198271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 812df8bae1dSRodney W. Grimes unp->unp_socket->so_state &= ~SS_ISCONNECTED; 813df8bae1dSRodney W. Grimes break; 814df8bae1dSRodney W. Grimes 815df8bae1dSRodney W. Grimes case SOCK_STREAM: 816df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 817fc3fcacfSRobert Watson unp2->unp_conn = NULL; 818df8bae1dSRodney W. Grimes soisdisconnected(unp2->unp_socket); 819df8bae1dSRodney W. Grimes break; 820df8bae1dSRodney W. Grimes } 821df8bae1dSRodney W. Grimes } 822df8bae1dSRodney W. Grimes 823df8bae1dSRodney W. Grimes #ifdef notdef 82426f9a767SRodney W. Grimes void 825df8bae1dSRodney W. Grimes unp_abort(unp) 826df8bae1dSRodney W. Grimes struct unpcb *unp; 827df8bae1dSRodney W. Grimes { 828df8bae1dSRodney W. Grimes 829df8bae1dSRodney W. Grimes unp_detach(unp); 830df8bae1dSRodney W. Grimes } 831df8bae1dSRodney W. Grimes #endif 832df8bae1dSRodney W. Grimes 83398271db4SGarrett Wollman static int 83482d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 83598271db4SGarrett Wollman { 836f5ef029eSPoul-Henning Kamp int error, i, n; 83798271db4SGarrett Wollman struct unpcb *unp, **unp_list; 83898271db4SGarrett Wollman unp_gen_t gencnt; 8398f364875SJulian Elischer struct xunpgen *xug; 84098271db4SGarrett Wollman struct unp_head *head; 8418f364875SJulian Elischer struct xunpcb *xu; 84298271db4SGarrett Wollman 843a23d65bfSBruce Evans head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 84498271db4SGarrett Wollman 84598271db4SGarrett Wollman /* 84698271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 84798271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 84898271db4SGarrett Wollman */ 849fc3fcacfSRobert Watson if (req->oldptr == NULL) { 85098271db4SGarrett Wollman n = unp_count; 8518f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug) 85298271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 853e5aeaa0cSDag-Erling Smørgrav return (0); 85498271db4SGarrett Wollman } 85598271db4SGarrett Wollman 856fc3fcacfSRobert Watson if (req->newptr != NULL) 857e5aeaa0cSDag-Erling Smørgrav return (EPERM); 85898271db4SGarrett Wollman 85998271db4SGarrett Wollman /* 86098271db4SGarrett Wollman * OK, now we're committed to doing something. 86198271db4SGarrett Wollman */ 862a163d034SWarner Losh xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 86398271db4SGarrett Wollman gencnt = unp_gencnt; 86498271db4SGarrett Wollman n = unp_count; 86598271db4SGarrett Wollman 8668f364875SJulian Elischer xug->xug_len = sizeof *xug; 8678f364875SJulian Elischer xug->xug_count = n; 8688f364875SJulian Elischer xug->xug_gen = gencnt; 8698f364875SJulian Elischer xug->xug_sogen = so_gencnt; 8708f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 8718f364875SJulian Elischer if (error) { 8728f364875SJulian Elischer free(xug, M_TEMP); 873e5aeaa0cSDag-Erling Smørgrav return (error); 8748f364875SJulian Elischer } 87598271db4SGarrett Wollman 876a163d034SWarner Losh unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 87798271db4SGarrett Wollman 8782e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 8792e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 8808a7d8cc6SRobert Watson if (unp->unp_gencnt <= gencnt) { 881a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred, 8828a7d8cc6SRobert Watson unp->unp_socket->so_cred)) 8834787fd37SPaul Saab continue; 88498271db4SGarrett Wollman unp_list[i++] = unp; 88598271db4SGarrett Wollman } 8864787fd37SPaul Saab } 88798271db4SGarrett Wollman n = i; /* in case we lost some during malloc */ 88898271db4SGarrett Wollman 88998271db4SGarrett Wollman error = 0; 890a163d034SWarner Losh xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK); 89198271db4SGarrett Wollman for (i = 0; i < n; i++) { 89298271db4SGarrett Wollman unp = unp_list[i]; 89398271db4SGarrett Wollman if (unp->unp_gencnt <= gencnt) { 8948f364875SJulian Elischer xu->xu_len = sizeof *xu; 8958f364875SJulian Elischer xu->xu_unpp = unp; 89698271db4SGarrett Wollman /* 89798271db4SGarrett Wollman * XXX - need more locking here to protect against 89898271db4SGarrett Wollman * connect/disconnect races for SMP. 89998271db4SGarrett Wollman */ 900fc3fcacfSRobert Watson if (unp->unp_addr != NULL) 9018f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr, 90298271db4SGarrett Wollman unp->unp_addr->sun_len); 903fc3fcacfSRobert Watson if (unp->unp_conn != NULL && 904fc3fcacfSRobert Watson unp->unp_conn->unp_addr != NULL) 90598271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 9068f364875SJulian Elischer &xu->xu_caddr, 90798271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 9088f364875SJulian Elischer bcopy(unp, &xu->xu_unp, sizeof *unp); 9098f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket); 9108f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu); 91198271db4SGarrett Wollman } 91298271db4SGarrett Wollman } 9138f364875SJulian Elischer free(xu, M_TEMP); 91498271db4SGarrett Wollman if (!error) { 91598271db4SGarrett Wollman /* 91698271db4SGarrett Wollman * Give the user an updated idea of our state. 91798271db4SGarrett Wollman * If the generation differs from what we told 91898271db4SGarrett Wollman * her before, she knows that something happened 91998271db4SGarrett Wollman * while we were processing this request, and it 92098271db4SGarrett Wollman * might be necessary to retry. 92198271db4SGarrett Wollman */ 9228f364875SJulian Elischer xug->xug_gen = unp_gencnt; 9238f364875SJulian Elischer xug->xug_sogen = so_gencnt; 9248f364875SJulian Elischer xug->xug_count = unp_count; 9258f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 92698271db4SGarrett Wollman } 92798271db4SGarrett Wollman free(unp_list, M_TEMP); 9288f364875SJulian Elischer free(xug, M_TEMP); 929e5aeaa0cSDag-Erling Smørgrav return (error); 93098271db4SGarrett Wollman } 93198271db4SGarrett Wollman 93298271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 93398271db4SGarrett Wollman (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 93498271db4SGarrett Wollman "List of active local datagram sockets"); 93598271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 93698271db4SGarrett Wollman (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 93798271db4SGarrett Wollman "List of active local stream sockets"); 93898271db4SGarrett Wollman 939f708ef1bSPoul-Henning Kamp static void 940df8bae1dSRodney W. Grimes unp_shutdown(unp) 941df8bae1dSRodney W. Grimes struct unpcb *unp; 942df8bae1dSRodney W. Grimes { 943df8bae1dSRodney W. Grimes struct socket *so; 944df8bae1dSRodney W. Grimes 945df8bae1dSRodney W. Grimes if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 946df8bae1dSRodney W. Grimes (so = unp->unp_conn->unp_socket)) 947df8bae1dSRodney W. Grimes socantrcvmore(so); 948df8bae1dSRodney W. Grimes } 949df8bae1dSRodney W. Grimes 950f708ef1bSPoul-Henning Kamp static void 951df8bae1dSRodney W. Grimes unp_drop(unp, errno) 952df8bae1dSRodney W. Grimes struct unpcb *unp; 953df8bae1dSRodney W. Grimes int errno; 954df8bae1dSRodney W. Grimes { 955df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 956df8bae1dSRodney W. Grimes 957df8bae1dSRodney W. Grimes so->so_error = errno; 958df8bae1dSRodney W. Grimes unp_disconnect(unp); 959df8bae1dSRodney W. Grimes } 960df8bae1dSRodney W. Grimes 961df8bae1dSRodney W. Grimes #ifdef notdef 96226f9a767SRodney W. Grimes void 963df8bae1dSRodney W. Grimes unp_drain() 964df8bae1dSRodney W. Grimes { 965df8bae1dSRodney W. Grimes 966df8bae1dSRodney W. Grimes } 967df8bae1dSRodney W. Grimes #endif 968df8bae1dSRodney W. Grimes 9692bc21ed9SDavid Malone static void 9702bc21ed9SDavid Malone unp_freerights(rp, fdcount) 9712bc21ed9SDavid Malone struct file **rp; 9722bc21ed9SDavid Malone int fdcount; 973df8bae1dSRodney W. Grimes { 9742bc21ed9SDavid Malone int i; 9752bc21ed9SDavid Malone struct file *fp; 976df8bae1dSRodney W. Grimes 9772bc21ed9SDavid Malone for (i = 0; i < fdcount; i++) { 978df8bae1dSRodney W. Grimes fp = *rp; 9798692c025SYoshinobu Inoue /* 9802bc21ed9SDavid Malone * zero the pointer before calling 9812bc21ed9SDavid Malone * unp_discard since it may end up 9822bc21ed9SDavid Malone * in unp_gc().. 9838692c025SYoshinobu Inoue */ 984df8bae1dSRodney W. Grimes *rp++ = 0; 9858692c025SYoshinobu Inoue unp_discard(fp); 986df8bae1dSRodney W. Grimes } 9872bc21ed9SDavid Malone } 9882bc21ed9SDavid Malone 9892bc21ed9SDavid Malone int 9902bc21ed9SDavid Malone unp_externalize(control, controlp) 9912bc21ed9SDavid Malone struct mbuf *control, **controlp; 9922bc21ed9SDavid Malone { 9932bc21ed9SDavid Malone struct thread *td = curthread; /* XXX */ 9942bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 9952bc21ed9SDavid Malone int i; 9962bc21ed9SDavid Malone int *fdp; 9972bc21ed9SDavid Malone struct file **rp; 9982bc21ed9SDavid Malone struct file *fp; 9992bc21ed9SDavid Malone void *data; 10002bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 10012bc21ed9SDavid Malone int error, newfds; 10022bc21ed9SDavid Malone int f; 10032bc21ed9SDavid Malone u_int newlen; 10042bc21ed9SDavid Malone 10052bc21ed9SDavid Malone error = 0; 10062bc21ed9SDavid Malone if (controlp != NULL) /* controlp == NULL => free control messages */ 10072bc21ed9SDavid Malone *controlp = NULL; 10082bc21ed9SDavid Malone 10092bc21ed9SDavid Malone while (cm != NULL) { 10102bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 10112bc21ed9SDavid Malone error = EINVAL; 10122bc21ed9SDavid Malone break; 10132bc21ed9SDavid Malone } 10142bc21ed9SDavid Malone 10152bc21ed9SDavid Malone data = CMSG_DATA(cm); 10162bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 10172bc21ed9SDavid Malone 10182bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET 10192bc21ed9SDavid Malone && cm->cmsg_type == SCM_RIGHTS) { 10202bc21ed9SDavid Malone newfds = datalen / sizeof(struct file *); 10212bc21ed9SDavid Malone rp = data; 10222bc21ed9SDavid Malone 1023e2f9a08bSOlivier Houchard /* If we're not outputting the descriptors free them. */ 10242bc21ed9SDavid Malone if (error || controlp == NULL) { 10252bc21ed9SDavid Malone unp_freerights(rp, newfds); 10262bc21ed9SDavid Malone goto next; 10272bc21ed9SDavid Malone } 1028426da3bcSAlfred Perlstein FILEDESC_LOCK(td->td_proc->p_fd); 10292bc21ed9SDavid Malone /* if the new FD's will not fit free them. */ 10302bc21ed9SDavid Malone if (!fdavail(td, newfds)) { 1031426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 10322bc21ed9SDavid Malone error = EMSGSIZE; 10332bc21ed9SDavid Malone unp_freerights(rp, newfds); 10342bc21ed9SDavid Malone goto next; 1035df8bae1dSRodney W. Grimes } 1036ed5b7817SJulian Elischer /* 10372bc21ed9SDavid Malone * now change each pointer to an fd in the global 10382bc21ed9SDavid Malone * table to an integer that is the index to the 10392bc21ed9SDavid Malone * local fd table entry that we set up to point 10402bc21ed9SDavid Malone * to the global one we are transferring. 1041ed5b7817SJulian Elischer */ 10422bc21ed9SDavid Malone newlen = newfds * sizeof(int); 10432bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 10442bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 10452bc21ed9SDavid Malone if (*controlp == NULL) { 1046426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 10472bc21ed9SDavid Malone error = E2BIG; 10482bc21ed9SDavid Malone unp_freerights(rp, newfds); 10492bc21ed9SDavid Malone goto next; 10502bc21ed9SDavid Malone } 10512bc21ed9SDavid Malone 10522bc21ed9SDavid Malone fdp = (int *) 10532bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1054df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 1055a6d4491cSDag-Erling Smørgrav if (fdalloc(td, 0, &f)) 10562bc21ed9SDavid Malone panic("unp_externalize fdalloc failed"); 10578692c025SYoshinobu Inoue fp = *rp++; 1058b40ce416SJulian Elischer td->td_proc->p_fd->fd_ofiles[f] = fp; 1059426da3bcSAlfred Perlstein FILE_LOCK(fp); 1060df8bae1dSRodney W. Grimes fp->f_msgcount--; 1061426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1062df8bae1dSRodney W. Grimes unp_rights--; 10638692c025SYoshinobu Inoue *fdp++ = f; 1064df8bae1dSRodney W. Grimes } 1065426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 10662bc21ed9SDavid Malone } else { /* We can just copy anything else across */ 10672bc21ed9SDavid Malone if (error || controlp == NULL) 10682bc21ed9SDavid Malone goto next; 10692bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, datalen, 10702bc21ed9SDavid Malone cm->cmsg_type, cm->cmsg_level); 10712bc21ed9SDavid Malone if (*controlp == NULL) { 10722bc21ed9SDavid Malone error = ENOBUFS; 10732bc21ed9SDavid Malone goto next; 10742bc21ed9SDavid Malone } 10752bc21ed9SDavid Malone bcopy(data, 10762bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 10772bc21ed9SDavid Malone datalen); 10782bc21ed9SDavid Malone } 10792bc21ed9SDavid Malone 10802bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 10812bc21ed9SDavid Malone 10822bc21ed9SDavid Malone next: 10832bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 10842bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 10852bc21ed9SDavid Malone cm = (struct cmsghdr *) 10862bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 10878692c025SYoshinobu Inoue } else { 10882bc21ed9SDavid Malone clen = 0; 10892bc21ed9SDavid Malone cm = NULL; 10908692c025SYoshinobu Inoue } 10918692c025SYoshinobu Inoue } 10928692c025SYoshinobu Inoue 10932bc21ed9SDavid Malone m_freem(control); 10942bc21ed9SDavid Malone 10952bc21ed9SDavid Malone return (error); 1096df8bae1dSRodney W. Grimes } 1097df8bae1dSRodney W. Grimes 109898271db4SGarrett Wollman void 109998271db4SGarrett Wollman unp_init(void) 110098271db4SGarrett Wollman { 11019e9d298aSJeff Roberson unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 11029e9d298aSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 1103fc3fcacfSRobert Watson if (unp_zone == NULL) 110498271db4SGarrett Wollman panic("unp_init"); 1105b17dd2bcSColin Percival uma_zone_set_max(unp_zone, nmbclusters); 110698271db4SGarrett Wollman LIST_INIT(&unp_dhead); 110798271db4SGarrett Wollman LIST_INIT(&unp_shead); 110898271db4SGarrett Wollman } 110998271db4SGarrett Wollman 1110f708ef1bSPoul-Henning Kamp static int 11112bc21ed9SDavid Malone unp_internalize(controlp, td) 11122bc21ed9SDavid Malone struct mbuf **controlp; 1113b40ce416SJulian Elischer struct thread *td; 1114df8bae1dSRodney W. Grimes { 11152bc21ed9SDavid Malone struct mbuf *control = *controlp; 1116b40ce416SJulian Elischer struct proc *p = td->td_proc; 11178692c025SYoshinobu Inoue struct filedesc *fdescp = p->p_fd; 11182bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 11192bc21ed9SDavid Malone struct cmsgcred *cmcred; 11202bc21ed9SDavid Malone struct file **rp; 11212bc21ed9SDavid Malone struct file *fp; 11222bc21ed9SDavid Malone struct timeval *tv; 11232bc21ed9SDavid Malone int i, fd, *fdp; 11242bc21ed9SDavid Malone void *data; 11252bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 11262bc21ed9SDavid Malone int error, oldfds; 11278692c025SYoshinobu Inoue u_int newlen; 1128df8bae1dSRodney W. Grimes 11292bc21ed9SDavid Malone error = 0; 11302bc21ed9SDavid Malone *controlp = NULL; 11310b788fa1SBill Paul 11322bc21ed9SDavid Malone while (cm != NULL) { 11332bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 11342bc21ed9SDavid Malone || cm->cmsg_len > clen) { 11352bc21ed9SDavid Malone error = EINVAL; 11362bc21ed9SDavid Malone goto out; 11372bc21ed9SDavid Malone } 11382bc21ed9SDavid Malone 11392bc21ed9SDavid Malone data = CMSG_DATA(cm); 11402bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 11412bc21ed9SDavid Malone 11422bc21ed9SDavid Malone switch (cm->cmsg_type) { 11430b788fa1SBill Paul /* 11440b788fa1SBill Paul * Fill in credential information. 11450b788fa1SBill Paul */ 11462bc21ed9SDavid Malone case SCM_CREDS: 11472bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 11482bc21ed9SDavid Malone SCM_CREDS, SOL_SOCKET); 11492bc21ed9SDavid Malone if (*controlp == NULL) { 11502bc21ed9SDavid Malone error = ENOBUFS; 11512bc21ed9SDavid Malone goto out; 11522bc21ed9SDavid Malone } 11532bc21ed9SDavid Malone 11542bc21ed9SDavid Malone cmcred = (struct cmsgcred *) 11552bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 11560b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 1157a854ed98SJohn Baldwin cmcred->cmcred_uid = td->td_ucred->cr_ruid; 1158a854ed98SJohn Baldwin cmcred->cmcred_gid = td->td_ucred->cr_rgid; 1159a854ed98SJohn Baldwin cmcred->cmcred_euid = td->td_ucred->cr_uid; 1160a854ed98SJohn Baldwin cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 11610b788fa1SBill Paul CMGROUP_MAX); 11620b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 11632bc21ed9SDavid Malone cmcred->cmcred_groups[i] = 1164a854ed98SJohn Baldwin td->td_ucred->cr_groups[i]; 11652bc21ed9SDavid Malone break; 11660b788fa1SBill Paul 11672bc21ed9SDavid Malone case SCM_RIGHTS: 11682bc21ed9SDavid Malone oldfds = datalen / sizeof (int); 1169ed5b7817SJulian Elischer /* 11702bc21ed9SDavid Malone * check that all the FDs passed in refer to legal files 1171ed5b7817SJulian Elischer * If not, reject the entire operation. 1172ed5b7817SJulian Elischer */ 11732bc21ed9SDavid Malone fdp = data; 1174426da3bcSAlfred Perlstein FILEDESC_LOCK(fdescp); 1175df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 11768692c025SYoshinobu Inoue fd = *fdp++; 11778692c025SYoshinobu Inoue if ((unsigned)fd >= fdescp->fd_nfiles || 11782bc21ed9SDavid Malone fdescp->fd_ofiles[fd] == NULL) { 1179426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 11802bc21ed9SDavid Malone error = EBADF; 11812bc21ed9SDavid Malone goto out; 11822bc21ed9SDavid Malone } 1183e7d6662fSAlfred Perlstein fp = fdescp->fd_ofiles[fd]; 1184e7d6662fSAlfred Perlstein if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 1185e7d6662fSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 1186e7d6662fSAlfred Perlstein error = EOPNOTSUPP; 1187e7d6662fSAlfred Perlstein goto out; 1188e7d6662fSAlfred Perlstein } 1189e7d6662fSAlfred Perlstein 1190df8bae1dSRodney W. Grimes } 1191ed5b7817SJulian Elischer /* 1192ed5b7817SJulian Elischer * Now replace the integer FDs with pointers to 1193ed5b7817SJulian Elischer * the associated global file table entry.. 1194ed5b7817SJulian Elischer */ 11952bc21ed9SDavid Malone newlen = oldfds * sizeof(struct file *); 11962bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 11972bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 11982bc21ed9SDavid Malone if (*controlp == NULL) { 1199426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 12002bc21ed9SDavid Malone error = E2BIG; 12012bc21ed9SDavid Malone goto out; 12028692c025SYoshinobu Inoue } 12038692c025SYoshinobu Inoue 12042bc21ed9SDavid Malone fdp = data; 12052bc21ed9SDavid Malone rp = (struct file **) 12062bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 12078692c025SYoshinobu Inoue for (i = 0; i < oldfds; i++) { 12088692c025SYoshinobu Inoue fp = fdescp->fd_ofiles[*fdp++]; 1209df8bae1dSRodney W. Grimes *rp++ = fp; 1210426da3bcSAlfred Perlstein FILE_LOCK(fp); 1211df8bae1dSRodney W. Grimes fp->f_count++; 1212df8bae1dSRodney W. Grimes fp->f_msgcount++; 1213426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1214df8bae1dSRodney W. Grimes unp_rights++; 1215df8bae1dSRodney W. Grimes } 1216426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 12172bc21ed9SDavid Malone break; 12182bc21ed9SDavid Malone 12192bc21ed9SDavid Malone case SCM_TIMESTAMP: 12202bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*tv), 12212bc21ed9SDavid Malone SCM_TIMESTAMP, SOL_SOCKET); 12222bc21ed9SDavid Malone if (*controlp == NULL) { 12232bc21ed9SDavid Malone error = ENOBUFS; 12242bc21ed9SDavid Malone goto out; 12258692c025SYoshinobu Inoue } 12262bc21ed9SDavid Malone tv = (struct timeval *) 12272bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 12282bc21ed9SDavid Malone microtime(tv); 12292bc21ed9SDavid Malone break; 12302bc21ed9SDavid Malone 12312bc21ed9SDavid Malone default: 12322bc21ed9SDavid Malone error = EINVAL; 12332bc21ed9SDavid Malone goto out; 12342bc21ed9SDavid Malone } 12352bc21ed9SDavid Malone 12362bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 12372bc21ed9SDavid Malone 12382bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 12392bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 12402bc21ed9SDavid Malone cm = (struct cmsghdr *) 12412bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 12422bc21ed9SDavid Malone } else { 12432bc21ed9SDavid Malone clen = 0; 12442bc21ed9SDavid Malone cm = NULL; 12452bc21ed9SDavid Malone } 12462bc21ed9SDavid Malone } 12472bc21ed9SDavid Malone 12482bc21ed9SDavid Malone out: 12492bc21ed9SDavid Malone m_freem(control); 12502bc21ed9SDavid Malone 12512bc21ed9SDavid Malone return (error); 1252df8bae1dSRodney W. Grimes } 1253df8bae1dSRodney W. Grimes 1254f708ef1bSPoul-Henning Kamp static int unp_defer, unp_gcing; 1255df8bae1dSRodney W. Grimes 1256f708ef1bSPoul-Henning Kamp static void 1257df8bae1dSRodney W. Grimes unp_gc() 1258df8bae1dSRodney W. Grimes { 1259df8bae1dSRodney W. Grimes register struct file *fp, *nextfp; 1260df8bae1dSRodney W. Grimes register struct socket *so; 1261df8bae1dSRodney W. Grimes struct file **extra_ref, **fpp; 1262df8bae1dSRodney W. Grimes int nunref, i; 1263df8bae1dSRodney W. Grimes 1264df8bae1dSRodney W. Grimes if (unp_gcing) 1265df8bae1dSRodney W. Grimes return; 1266df8bae1dSRodney W. Grimes unp_gcing = 1; 1267df8bae1dSRodney W. Grimes unp_defer = 0; 1268ed5b7817SJulian Elischer /* 1269ed5b7817SJulian Elischer * before going through all this, set all FDs to 1270ed5b7817SJulian Elischer * be NOT defered and NOT externally accessible 1271ed5b7817SJulian Elischer */ 1272426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 12732e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) 1274426da3bcSAlfred Perlstein fp->f_gcflag &= ~(FMARK|FDEFER); 1275df8bae1dSRodney W. Grimes do { 12762e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) { 1277426da3bcSAlfred Perlstein FILE_LOCK(fp); 1278ed5b7817SJulian Elischer /* 1279ed5b7817SJulian Elischer * If the file is not open, skip it 1280ed5b7817SJulian Elischer */ 1281426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1282426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1283df8bae1dSRodney W. Grimes continue; 1284426da3bcSAlfred Perlstein } 1285ed5b7817SJulian Elischer /* 1286ed5b7817SJulian Elischer * If we already marked it as 'defer' in a 1287ed5b7817SJulian Elischer * previous pass, then try process it this time 1288ed5b7817SJulian Elischer * and un-mark it 1289ed5b7817SJulian Elischer */ 1290426da3bcSAlfred Perlstein if (fp->f_gcflag & FDEFER) { 1291426da3bcSAlfred Perlstein fp->f_gcflag &= ~FDEFER; 1292df8bae1dSRodney W. Grimes unp_defer--; 1293df8bae1dSRodney W. Grimes } else { 1294ed5b7817SJulian Elischer /* 1295ed5b7817SJulian Elischer * if it's not defered, then check if it's 1296ed5b7817SJulian Elischer * already marked.. if so skip it 1297ed5b7817SJulian Elischer */ 1298426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) { 1299426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1300df8bae1dSRodney W. Grimes continue; 1301426da3bcSAlfred Perlstein } 1302ed5b7817SJulian Elischer /* 1303ed5b7817SJulian Elischer * If all references are from messages 1304ed5b7817SJulian Elischer * in transit, then skip it. it's not 1305ed5b7817SJulian Elischer * externally accessible. 1306ed5b7817SJulian Elischer */ 1307426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount) { 1308426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1309df8bae1dSRodney W. Grimes continue; 1310426da3bcSAlfred Perlstein } 1311ed5b7817SJulian Elischer /* 1312ed5b7817SJulian Elischer * If it got this far then it must be 1313ed5b7817SJulian Elischer * externally accessible. 1314ed5b7817SJulian Elischer */ 1315426da3bcSAlfred Perlstein fp->f_gcflag |= FMARK; 1316df8bae1dSRodney W. Grimes } 1317ed5b7817SJulian Elischer /* 1318ed5b7817SJulian Elischer * either it was defered, or it is externally 1319ed5b7817SJulian Elischer * accessible and not already marked so. 1320ed5b7817SJulian Elischer * Now check if it is possibly one of OUR sockets. 1321ed5b7817SJulian Elischer */ 1322df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_SOCKET || 132348e3128bSMatthew Dillon (so = fp->f_data) == NULL) { 1324426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1325df8bae1dSRodney W. Grimes continue; 1326426da3bcSAlfred Perlstein } 1327426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1328748e0b0aSGarrett Wollman if (so->so_proto->pr_domain != &localdomain || 1329df8bae1dSRodney W. Grimes (so->so_proto->pr_flags&PR_RIGHTS) == 0) 1330df8bae1dSRodney W. Grimes continue; 1331df8bae1dSRodney W. Grimes #ifdef notdef 1332df8bae1dSRodney W. Grimes if (so->so_rcv.sb_flags & SB_LOCK) { 1333df8bae1dSRodney W. Grimes /* 1334df8bae1dSRodney W. Grimes * This is problematical; it's not clear 1335df8bae1dSRodney W. Grimes * we need to wait for the sockbuf to be 1336df8bae1dSRodney W. Grimes * unlocked (on a uniprocessor, at least), 1337df8bae1dSRodney W. Grimes * and it's also not clear what to do 1338df8bae1dSRodney W. Grimes * if sbwait returns an error due to receipt 1339df8bae1dSRodney W. Grimes * of a signal. If sbwait does return 1340df8bae1dSRodney W. Grimes * an error, we'll go into an infinite 1341df8bae1dSRodney W. Grimes * loop. Delete all of this for now. 1342df8bae1dSRodney W. Grimes */ 1343df8bae1dSRodney W. Grimes (void) sbwait(&so->so_rcv); 1344df8bae1dSRodney W. Grimes goto restart; 1345df8bae1dSRodney W. Grimes } 1346df8bae1dSRodney W. Grimes #endif 1347ed5b7817SJulian Elischer /* 1348ed5b7817SJulian Elischer * So, Ok, it's one of our sockets and it IS externally 1349ed5b7817SJulian Elischer * accessible (or was defered). Now we look 1350dc733423SDag-Erling Smørgrav * to see if we hold any file descriptors in its 1351ed5b7817SJulian Elischer * message buffers. Follow those links and mark them 1352ed5b7817SJulian Elischer * as accessible too. 1353ed5b7817SJulian Elischer */ 1354df8bae1dSRodney W. Grimes unp_scan(so->so_rcv.sb_mb, unp_mark); 1355df8bae1dSRodney W. Grimes } 1356df8bae1dSRodney W. Grimes } while (unp_defer); 1357426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1358df8bae1dSRodney W. Grimes /* 1359df8bae1dSRodney W. Grimes * We grab an extra reference to each of the file table entries 1360df8bae1dSRodney W. Grimes * that are not otherwise accessible and then free the rights 1361df8bae1dSRodney W. Grimes * that are stored in messages on them. 1362df8bae1dSRodney W. Grimes * 1363df8bae1dSRodney W. Grimes * The bug in the orginal code is a little tricky, so I'll describe 1364df8bae1dSRodney W. Grimes * what's wrong with it here. 1365df8bae1dSRodney W. Grimes * 1366df8bae1dSRodney W. Grimes * It is incorrect to simply unp_discard each entry for f_msgcount 1367df8bae1dSRodney W. Grimes * times -- consider the case of sockets A and B that contain 1368df8bae1dSRodney W. Grimes * references to each other. On a last close of some other socket, 1369df8bae1dSRodney W. Grimes * we trigger a gc since the number of outstanding rights (unp_rights) 1370df8bae1dSRodney W. Grimes * is non-zero. If during the sweep phase the gc code un_discards, 1371df8bae1dSRodney W. Grimes * we end up doing a (full) closef on the descriptor. A closef on A 1372df8bae1dSRodney W. Grimes * results in the following chain. Closef calls soo_close, which 1373df8bae1dSRodney W. Grimes * calls soclose. Soclose calls first (through the switch 1374df8bae1dSRodney W. Grimes * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 1375df8bae1dSRodney W. Grimes * returns because the previous instance had set unp_gcing, and 1376df8bae1dSRodney W. Grimes * we return all the way back to soclose, which marks the socket 1377df8bae1dSRodney W. Grimes * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush 1378df8bae1dSRodney W. Grimes * to free up the rights that are queued in messages on the socket A, 1379df8bae1dSRodney W. Grimes * i.e., the reference on B. The sorflush calls via the dom_dispose 1380df8bae1dSRodney W. Grimes * switch unp_dispose, which unp_scans with unp_discard. This second 1381df8bae1dSRodney W. Grimes * instance of unp_discard just calls closef on B. 1382df8bae1dSRodney W. Grimes * 1383df8bae1dSRodney W. Grimes * Well, a similar chain occurs on B, resulting in a sorflush on B, 1384df8bae1dSRodney W. Grimes * which results in another closef on A. Unfortunately, A is already 1385df8bae1dSRodney W. Grimes * being closed, and the descriptor has already been marked with 1386df8bae1dSRodney W. Grimes * SS_NOFDREF, and soclose panics at this point. 1387df8bae1dSRodney W. Grimes * 1388df8bae1dSRodney W. Grimes * Here, we first take an extra reference to each inaccessible 1389df8bae1dSRodney W. Grimes * descriptor. Then, we call sorflush ourself, since we know 1390df8bae1dSRodney W. Grimes * it is a Unix domain socket anyhow. After we destroy all the 1391df8bae1dSRodney W. Grimes * rights carried in messages, we do a last closef to get rid 1392df8bae1dSRodney W. Grimes * of our extra reference. This is the last close, and the 1393df8bae1dSRodney W. Grimes * unp_detach etc will shut down the socket. 1394df8bae1dSRodney W. Grimes * 1395df8bae1dSRodney W. Grimes * 91/09/19, bsy@cs.cmu.edu 1396df8bae1dSRodney W. Grimes */ 1397a163d034SWarner Losh extra_ref = malloc(nfiles * sizeof(struct file *), M_TEMP, M_WAITOK); 1398426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 1399fc3fcacfSRobert Watson for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; 1400fc3fcacfSRobert Watson fp != NULL; fp = nextfp) { 14012e3c8fcbSPoul-Henning Kamp nextfp = LIST_NEXT(fp, f_list); 1402426da3bcSAlfred Perlstein FILE_LOCK(fp); 1403ed5b7817SJulian Elischer /* 1404ed5b7817SJulian Elischer * If it's not open, skip it 1405ed5b7817SJulian Elischer */ 1406426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1407426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1408df8bae1dSRodney W. Grimes continue; 1409426da3bcSAlfred Perlstein } 1410ed5b7817SJulian Elischer /* 1411ed5b7817SJulian Elischer * If all refs are from msgs, and it's not marked accessible 1412ed5b7817SJulian Elischer * then it must be referenced from some unreachable cycle 1413ed5b7817SJulian Elischer * of (shut-down) FDs, so include it in our 1414ed5b7817SJulian Elischer * list of FDs to remove 1415ed5b7817SJulian Elischer */ 1416426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) { 1417df8bae1dSRodney W. Grimes *fpp++ = fp; 1418df8bae1dSRodney W. Grimes nunref++; 1419df8bae1dSRodney W. Grimes fp->f_count++; 1420df8bae1dSRodney W. Grimes } 1421426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1422df8bae1dSRodney W. Grimes } 1423426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1424ed5b7817SJulian Elischer /* 1425ed5b7817SJulian Elischer * for each FD on our hit list, do the following two things 1426ed5b7817SJulian Elischer */ 14271c7c3c6aSMatthew Dillon for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 14281c7c3c6aSMatthew Dillon struct file *tfp = *fpp; 1429426da3bcSAlfred Perlstein FILE_LOCK(tfp); 1430cd72f218SMatthew Dillon if (tfp->f_type == DTYPE_SOCKET && 143148e3128bSMatthew Dillon tfp->f_data != NULL) { 1432426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 143348e3128bSMatthew Dillon sorflush(tfp->f_data); 1434e5aeaa0cSDag-Erling Smørgrav } else { 1435426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 14361c7c3c6aSMatthew Dillon } 1437e5aeaa0cSDag-Erling Smørgrav } 1438df8bae1dSRodney W. Grimes for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) 1439b40ce416SJulian Elischer closef(*fpp, (struct thread *) NULL); 1440210a5a71SAlfred Perlstein free(extra_ref, M_TEMP); 1441df8bae1dSRodney W. Grimes unp_gcing = 0; 1442df8bae1dSRodney W. Grimes } 1443df8bae1dSRodney W. Grimes 144426f9a767SRodney W. Grimes void 1445df8bae1dSRodney W. Grimes unp_dispose(m) 1446df8bae1dSRodney W. Grimes struct mbuf *m; 1447df8bae1dSRodney W. Grimes { 1448996c772fSJohn Dyson 1449df8bae1dSRodney W. Grimes if (m) 1450df8bae1dSRodney W. Grimes unp_scan(m, unp_discard); 1451df8bae1dSRodney W. Grimes } 1452df8bae1dSRodney W. Grimes 14530c1bb4fbSDima Dorfman static int 14546f105b34SJohn Baldwin unp_listen(unp, td) 14550c1bb4fbSDima Dorfman struct unpcb *unp; 14566f105b34SJohn Baldwin struct thread *td; 14570c1bb4fbSDima Dorfman { 14580c1bb4fbSDima Dorfman 14596f105b34SJohn Baldwin cru2x(td->td_ucred, &unp->unp_peercred); 14600c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPCCACHED; 14610c1bb4fbSDima Dorfman return (0); 14620c1bb4fbSDima Dorfman } 14630c1bb4fbSDima Dorfman 1464f708ef1bSPoul-Henning Kamp static void 1465df8bae1dSRodney W. Grimes unp_scan(m0, op) 1466df8bae1dSRodney W. Grimes register struct mbuf *m0; 14674d77a549SAlfred Perlstein void (*op)(struct file *); 1468df8bae1dSRodney W. Grimes { 14692bc21ed9SDavid Malone struct mbuf *m; 14702bc21ed9SDavid Malone struct file **rp; 14712bc21ed9SDavid Malone struct cmsghdr *cm; 14722bc21ed9SDavid Malone void *data; 14732bc21ed9SDavid Malone int i; 14742bc21ed9SDavid Malone socklen_t clen, datalen; 1475df8bae1dSRodney W. Grimes int qfds; 1476df8bae1dSRodney W. Grimes 1477fc3fcacfSRobert Watson while (m0 != NULL) { 14782bc21ed9SDavid Malone for (m = m0; m; m = m->m_next) { 147912396bdcSDavid Malone if (m->m_type != MT_CONTROL) 1480df8bae1dSRodney W. Grimes continue; 14812bc21ed9SDavid Malone 14822bc21ed9SDavid Malone cm = mtod(m, struct cmsghdr *); 14832bc21ed9SDavid Malone clen = m->m_len; 14842bc21ed9SDavid Malone 14852bc21ed9SDavid Malone while (cm != NULL) { 14862bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) 14872bc21ed9SDavid Malone break; 14882bc21ed9SDavid Malone 14892bc21ed9SDavid Malone data = CMSG_DATA(cm); 14902bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len 14912bc21ed9SDavid Malone - (caddr_t)data; 14922bc21ed9SDavid Malone 14932bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET && 14942bc21ed9SDavid Malone cm->cmsg_type == SCM_RIGHTS) { 14952bc21ed9SDavid Malone qfds = datalen / sizeof (struct file *); 14962bc21ed9SDavid Malone rp = data; 1497df8bae1dSRodney W. Grimes for (i = 0; i < qfds; i++) 1498df8bae1dSRodney W. Grimes (*op)(*rp++); 14992bc21ed9SDavid Malone } 15002bc21ed9SDavid Malone 15012bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 15022bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 15032bc21ed9SDavid Malone cm = (struct cmsghdr *) 15042bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 15052bc21ed9SDavid Malone } else { 15062bc21ed9SDavid Malone clen = 0; 15072bc21ed9SDavid Malone cm = NULL; 15082bc21ed9SDavid Malone } 15092bc21ed9SDavid Malone } 1510df8bae1dSRodney W. Grimes } 1511df8bae1dSRodney W. Grimes m0 = m0->m_act; 1512df8bae1dSRodney W. Grimes } 1513df8bae1dSRodney W. Grimes } 1514df8bae1dSRodney W. Grimes 1515f708ef1bSPoul-Henning Kamp static void 1516df8bae1dSRodney W. Grimes unp_mark(fp) 1517df8bae1dSRodney W. Grimes struct file *fp; 1518df8bae1dSRodney W. Grimes { 1519426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) 1520df8bae1dSRodney W. Grimes return; 1521df8bae1dSRodney W. Grimes unp_defer++; 1522426da3bcSAlfred Perlstein fp->f_gcflag |= (FMARK|FDEFER); 1523df8bae1dSRodney W. Grimes } 1524df8bae1dSRodney W. Grimes 1525f708ef1bSPoul-Henning Kamp static void 1526df8bae1dSRodney W. Grimes unp_discard(fp) 1527df8bae1dSRodney W. Grimes struct file *fp; 1528df8bae1dSRodney W. Grimes { 1529426da3bcSAlfred Perlstein FILE_LOCK(fp); 1530df8bae1dSRodney W. Grimes fp->f_msgcount--; 1531df8bae1dSRodney W. Grimes unp_rights--; 1532426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1533b40ce416SJulian Elischer (void) closef(fp, (struct thread *)NULL); 1534df8bae1dSRodney W. Grimes } 1535