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 34c3aac50fSPeter Wemm * $FreeBSD$ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37df8bae1dSRodney W. Grimes #include <sys/param.h> 38df8bae1dSRodney W. Grimes #include <sys/systm.h> 39639acc13SGarrett Wollman #include <sys/kernel.h> 40df8bae1dSRodney W. Grimes #include <sys/domain.h> 413ac4d1efSBruce Evans #include <sys/fcntl.h> 42d826c479SBruce Evans #include <sys/malloc.h> /* XXX must be before <sys/file.h> */ 43639acc13SGarrett Wollman #include <sys/file.h> 44639acc13SGarrett Wollman #include <sys/filedesc.h> 45639acc13SGarrett Wollman #include <sys/mbuf.h> 46639acc13SGarrett Wollman #include <sys/namei.h> 47639acc13SGarrett Wollman #include <sys/proc.h> 48df8bae1dSRodney W. Grimes #include <sys/protosw.h> 49df8bae1dSRodney W. Grimes #include <sys/socket.h> 50df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 51c6362551SAlfred Perlstein #include <sys/resourcevar.h> 52df8bae1dSRodney W. Grimes #include <sys/stat.h> 53639acc13SGarrett Wollman #include <sys/sysctl.h> 54639acc13SGarrett Wollman #include <sys/un.h> 5598271db4SGarrett Wollman #include <sys/unpcb.h> 56639acc13SGarrett Wollman #include <sys/vnode.h> 57df8bae1dSRodney W. Grimes 5898271db4SGarrett Wollman #include <vm/vm_zone.h> 5998271db4SGarrett Wollman 60632a035fSEivind Eklund static struct vm_zone *unp_zone; 6198271db4SGarrett Wollman static unp_gen_t unp_gencnt; 6298271db4SGarrett Wollman static u_int unp_count; 6398271db4SGarrett Wollman 6498271db4SGarrett Wollman static struct unp_head unp_shead, unp_dhead; 6598271db4SGarrett Wollman 66df8bae1dSRodney W. Grimes /* 67df8bae1dSRodney W. Grimes * Unix communications domain. 68df8bae1dSRodney W. Grimes * 69df8bae1dSRodney W. Grimes * TODO: 70df8bae1dSRodney W. Grimes * SEQPACKET, RDM 71df8bae1dSRodney W. Grimes * rethink name space problems 72df8bae1dSRodney W. Grimes * need a proper out-of-band 7398271db4SGarrett Wollman * lock pushdown 74df8bae1dSRodney W. Grimes */ 75f708ef1bSPoul-Henning Kamp static struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 76f708ef1bSPoul-Henning Kamp static ino_t unp_ino; /* prototype for fake inode numbers */ 77f708ef1bSPoul-Henning Kamp 78f708ef1bSPoul-Henning Kamp static int unp_attach __P((struct socket *)); 79f708ef1bSPoul-Henning Kamp static void unp_detach __P((struct unpcb *)); 8057bf258eSGarrett Wollman static int unp_bind __P((struct unpcb *,struct sockaddr *, struct proc *)); 8157bf258eSGarrett Wollman static int unp_connect __P((struct socket *,struct sockaddr *, 8257bf258eSGarrett Wollman struct proc *)); 83f708ef1bSPoul-Henning Kamp static void unp_disconnect __P((struct unpcb *)); 84f708ef1bSPoul-Henning Kamp static void unp_shutdown __P((struct unpcb *)); 85f708ef1bSPoul-Henning Kamp static void unp_drop __P((struct unpcb *, int)); 86f708ef1bSPoul-Henning Kamp static void unp_gc __P((void)); 87f708ef1bSPoul-Henning Kamp static void unp_scan __P((struct mbuf *, void (*)(struct file *))); 88f708ef1bSPoul-Henning Kamp static void unp_mark __P((struct file *)); 89f708ef1bSPoul-Henning Kamp static void unp_discard __P((struct file *)); 90f708ef1bSPoul-Henning Kamp static int unp_internalize __P((struct mbuf *, struct proc *)); 91f708ef1bSPoul-Henning Kamp 92a29f300eSGarrett Wollman static int 93a29f300eSGarrett Wollman uipc_abort(struct socket *so) 94df8bae1dSRodney W. Grimes { 95df8bae1dSRodney W. Grimes struct unpcb *unp = sotounpcb(so); 96df8bae1dSRodney W. Grimes 97a29f300eSGarrett Wollman if (unp == 0) 98a29f300eSGarrett Wollman return EINVAL; 99a29f300eSGarrett Wollman unp_drop(unp, ECONNABORTED); 100a29f300eSGarrett Wollman return 0; 101df8bae1dSRodney W. Grimes } 102df8bae1dSRodney W. Grimes 103a29f300eSGarrett Wollman static int 10457bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam) 105a29f300eSGarrett Wollman { 106a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 107df8bae1dSRodney W. Grimes 108a29f300eSGarrett Wollman if (unp == 0) 109a29f300eSGarrett Wollman return EINVAL; 110df8bae1dSRodney W. Grimes 111df8bae1dSRodney W. Grimes /* 112df8bae1dSRodney W. Grimes * Pass back name of connected socket, 113df8bae1dSRodney W. Grimes * if it was bound and we are still connected 114df8bae1dSRodney W. Grimes * (our peer may have closed already!). 115df8bae1dSRodney W. Grimes */ 116df8bae1dSRodney W. Grimes if (unp->unp_conn && unp->unp_conn->unp_addr) { 11757bf258eSGarrett Wollman *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr, 11857bf258eSGarrett Wollman 1); 119df8bae1dSRodney W. Grimes } else { 12057bf258eSGarrett Wollman *nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1); 121df8bae1dSRodney W. Grimes } 122a29f300eSGarrett Wollman return 0; 123a29f300eSGarrett Wollman } 124df8bae1dSRodney W. Grimes 125a29f300eSGarrett Wollman static int 126a29f300eSGarrett Wollman uipc_attach(struct socket *so, int proto, struct proc *p) 127a29f300eSGarrett Wollman { 128a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 129df8bae1dSRodney W. Grimes 130a29f300eSGarrett Wollman if (unp != 0) 131a29f300eSGarrett Wollman return EISCONN; 132a29f300eSGarrett Wollman return unp_attach(so); 133a29f300eSGarrett Wollman } 134a29f300eSGarrett Wollman 135a29f300eSGarrett Wollman static int 13657bf258eSGarrett Wollman uipc_bind(struct socket *so, struct sockaddr *nam, struct proc *p) 137a29f300eSGarrett Wollman { 138a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 139a29f300eSGarrett Wollman 140a29f300eSGarrett Wollman if (unp == 0) 141a29f300eSGarrett Wollman return EINVAL; 142a29f300eSGarrett Wollman 143a29f300eSGarrett Wollman return unp_bind(unp, nam, p); 144a29f300eSGarrett Wollman } 145a29f300eSGarrett Wollman 146a29f300eSGarrett Wollman static int 14757bf258eSGarrett Wollman uipc_connect(struct socket *so, struct sockaddr *nam, struct proc *p) 148a29f300eSGarrett Wollman { 149a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 150a29f300eSGarrett Wollman 151a29f300eSGarrett Wollman if (unp == 0) 152a29f300eSGarrett Wollman return EINVAL; 153a29f300eSGarrett Wollman return unp_connect(so, nam, curproc); 154a29f300eSGarrett Wollman } 155a29f300eSGarrett Wollman 156a29f300eSGarrett Wollman static int 157a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2) 158a29f300eSGarrett Wollman { 159a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so1); 160a29f300eSGarrett Wollman 161a29f300eSGarrett Wollman if (unp == 0) 162a29f300eSGarrett Wollman return EINVAL; 163a29f300eSGarrett Wollman 164a29f300eSGarrett Wollman return unp_connect2(so1, so2); 165a29f300eSGarrett Wollman } 166a29f300eSGarrett Wollman 167a29f300eSGarrett Wollman /* control is EOPNOTSUPP */ 168a29f300eSGarrett Wollman 169a29f300eSGarrett Wollman static int 170a29f300eSGarrett Wollman uipc_detach(struct socket *so) 171a29f300eSGarrett Wollman { 172a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 173a29f300eSGarrett Wollman 174a29f300eSGarrett Wollman if (unp == 0) 175a29f300eSGarrett Wollman return EINVAL; 176a29f300eSGarrett Wollman 177a29f300eSGarrett Wollman unp_detach(unp); 178a29f300eSGarrett Wollman return 0; 179a29f300eSGarrett Wollman } 180a29f300eSGarrett Wollman 181a29f300eSGarrett Wollman static int 182a29f300eSGarrett Wollman uipc_disconnect(struct socket *so) 183a29f300eSGarrett Wollman { 184a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 185a29f300eSGarrett Wollman 186a29f300eSGarrett Wollman if (unp == 0) 187a29f300eSGarrett Wollman return EINVAL; 188a29f300eSGarrett Wollman unp_disconnect(unp); 189a29f300eSGarrett Wollman return 0; 190a29f300eSGarrett Wollman } 191a29f300eSGarrett Wollman 192a29f300eSGarrett Wollman static int 193a29f300eSGarrett Wollman uipc_listen(struct socket *so, struct proc *p) 194a29f300eSGarrett Wollman { 195a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 196a29f300eSGarrett Wollman 197a29f300eSGarrett Wollman if (unp == 0 || unp->unp_vnode == 0) 198a29f300eSGarrett Wollman return EINVAL; 199a29f300eSGarrett Wollman return 0; 200a29f300eSGarrett Wollman } 201a29f300eSGarrett Wollman 202a29f300eSGarrett Wollman static int 20357bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 204a29f300eSGarrett Wollman { 205a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 206a29f300eSGarrett Wollman 207a29f300eSGarrett Wollman if (unp == 0) 208a29f300eSGarrett Wollman return EINVAL; 20957bf258eSGarrett Wollman if (unp->unp_conn && unp->unp_conn->unp_addr) 21057bf258eSGarrett Wollman *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr, 21157bf258eSGarrett Wollman 1); 212a29f300eSGarrett Wollman return 0; 213a29f300eSGarrett Wollman } 214a29f300eSGarrett Wollman 215a29f300eSGarrett Wollman static int 216a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 217a29f300eSGarrett Wollman { 218a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 219a29f300eSGarrett Wollman struct socket *so2; 220a29f300eSGarrett Wollman 221a29f300eSGarrett Wollman if (unp == 0) 222a29f300eSGarrett Wollman return EINVAL; 223df8bae1dSRodney W. Grimes switch (so->so_type) { 224df8bae1dSRodney W. Grimes case SOCK_DGRAM: 225a29f300eSGarrett Wollman panic("uipc_rcvd DGRAM?"); 226df8bae1dSRodney W. Grimes /*NOTREACHED*/ 227df8bae1dSRodney W. Grimes 228df8bae1dSRodney W. Grimes case SOCK_STREAM: 229df8bae1dSRodney W. Grimes if (unp->unp_conn == 0) 230df8bae1dSRodney W. Grimes break; 231df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 232df8bae1dSRodney W. Grimes /* 233df8bae1dSRodney W. Grimes * Adjust backpressure on sender 234df8bae1dSRodney W. Grimes * and wakeup any waiting to write. 235df8bae1dSRodney W. Grimes */ 236ff8b0106SBrian Feldman so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt; 237ff8b0106SBrian Feldman unp->unp_mbcnt = so->so_rcv.sb_mbcnt; 238ff8b0106SBrian Feldman so2->so_snd.sb_hiwat += unp->unp_cc - so->so_rcv.sb_cc; 239ecf72308SBrian Feldman (void)chgsbsize(so2->so_cred->cr_uid, 240c6362551SAlfred Perlstein (rlim_t)unp->unp_cc - so->so_rcv.sb_cc, RLIM_INFINITY); 241ff8b0106SBrian Feldman unp->unp_cc = so->so_rcv.sb_cc; 242df8bae1dSRodney W. Grimes sowwakeup(so2); 243df8bae1dSRodney W. Grimes break; 244df8bae1dSRodney W. Grimes 245df8bae1dSRodney W. Grimes default: 246a29f300eSGarrett Wollman panic("uipc_rcvd unknown socktype"); 247df8bae1dSRodney W. Grimes } 248a29f300eSGarrett Wollman return 0; 249a29f300eSGarrett Wollman } 250df8bae1dSRodney W. Grimes 251a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 252a29f300eSGarrett Wollman 253a29f300eSGarrett Wollman static int 25457bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 255a29f300eSGarrett Wollman struct mbuf *control, struct proc *p) 256a29f300eSGarrett Wollman { 257a29f300eSGarrett Wollman int error = 0; 258a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 259a29f300eSGarrett Wollman struct socket *so2; 260a29f300eSGarrett Wollman 261a29f300eSGarrett Wollman if (unp == 0) { 262a29f300eSGarrett Wollman error = EINVAL; 263a29f300eSGarrett Wollman goto release; 264a29f300eSGarrett Wollman } 265a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 266a29f300eSGarrett Wollman error = EOPNOTSUPP; 267a29f300eSGarrett Wollman goto release; 268a29f300eSGarrett Wollman } 269a29f300eSGarrett Wollman 270df8bae1dSRodney W. Grimes if (control && (error = unp_internalize(control, p))) 271a29f300eSGarrett Wollman goto release; 272df8bae1dSRodney W. Grimes 273a29f300eSGarrett Wollman switch (so->so_type) { 274a29f300eSGarrett Wollman case SOCK_DGRAM: 275a29f300eSGarrett Wollman { 276df8bae1dSRodney W. Grimes struct sockaddr *from; 277df8bae1dSRodney W. Grimes 278df8bae1dSRodney W. Grimes if (nam) { 279df8bae1dSRodney W. Grimes if (unp->unp_conn) { 280df8bae1dSRodney W. Grimes error = EISCONN; 281df8bae1dSRodney W. Grimes break; 282df8bae1dSRodney W. Grimes } 283df8bae1dSRodney W. Grimes error = unp_connect(so, nam, p); 284df8bae1dSRodney W. Grimes if (error) 285df8bae1dSRodney W. Grimes break; 286df8bae1dSRodney W. Grimes } else { 287df8bae1dSRodney W. Grimes if (unp->unp_conn == 0) { 288df8bae1dSRodney W. Grimes error = ENOTCONN; 289df8bae1dSRodney W. Grimes break; 290df8bae1dSRodney W. Grimes } 291df8bae1dSRodney W. Grimes } 292df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 293df8bae1dSRodney W. Grimes if (unp->unp_addr) 29457bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 295df8bae1dSRodney W. Grimes else 296df8bae1dSRodney W. Grimes from = &sun_noname; 297df8bae1dSRodney W. Grimes if (sbappendaddr(&so2->so_rcv, from, m, control)) { 298df8bae1dSRodney W. Grimes sorwakeup(so2); 299df8bae1dSRodney W. Grimes m = 0; 300df8bae1dSRodney W. Grimes control = 0; 301df8bae1dSRodney W. Grimes } else 302df8bae1dSRodney W. Grimes error = ENOBUFS; 303df8bae1dSRodney W. Grimes if (nam) 304df8bae1dSRodney W. Grimes unp_disconnect(unp); 305df8bae1dSRodney W. Grimes break; 306df8bae1dSRodney W. Grimes } 307df8bae1dSRodney W. Grimes 308df8bae1dSRodney W. Grimes case SOCK_STREAM: 3096b8fda4dSGarrett Wollman /* Connect if not connected yet. */ 3106b8fda4dSGarrett Wollman /* 3116b8fda4dSGarrett Wollman * Note: A better implementation would complain 312402cc72dSDavid Greenman * if not equal to the peer's address. 3136b8fda4dSGarrett Wollman */ 314402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 315402cc72dSDavid Greenman if (nam) { 316402cc72dSDavid Greenman error = unp_connect(so, nam, p); 317402cc72dSDavid Greenman if (error) 3186b8fda4dSGarrett Wollman break; /* XXX */ 319402cc72dSDavid Greenman } else { 320402cc72dSDavid Greenman error = ENOTCONN; 321402cc72dSDavid Greenman break; 322402cc72dSDavid Greenman } 323402cc72dSDavid Greenman } 324402cc72dSDavid Greenman 325df8bae1dSRodney W. Grimes if (so->so_state & SS_CANTSENDMORE) { 326df8bae1dSRodney W. Grimes error = EPIPE; 327df8bae1dSRodney W. Grimes break; 328df8bae1dSRodney W. Grimes } 329df8bae1dSRodney W. Grimes if (unp->unp_conn == 0) 330a29f300eSGarrett Wollman panic("uipc_send connected but no connection?"); 331df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 332df8bae1dSRodney W. Grimes /* 333df8bae1dSRodney W. Grimes * Send to paired receive port, and then reduce 334df8bae1dSRodney W. Grimes * send buffer hiwater marks to maintain backpressure. 335df8bae1dSRodney W. Grimes * Wake up readers. 336df8bae1dSRodney W. Grimes */ 337df8bae1dSRodney W. Grimes if (control) { 338ff8b0106SBrian Feldman if (sbappendcontrol(&so2->so_rcv, m, control)) 339df8bae1dSRodney W. Grimes control = 0; 340df8bae1dSRodney W. Grimes } else 341ff8b0106SBrian Feldman sbappend(&so2->so_rcv, m); 342ff8b0106SBrian Feldman so->so_snd.sb_mbmax -= 343ff8b0106SBrian Feldman so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt; 344ff8b0106SBrian Feldman unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt; 345ff8b0106SBrian Feldman so->so_snd.sb_hiwat -= 346ff8b0106SBrian Feldman so2->so_rcv.sb_cc - unp->unp_conn->unp_cc; 347ecf72308SBrian Feldman (void)chgsbsize(so->so_cred->cr_uid, 348c6362551SAlfred Perlstein (rlim_t)unp->unp_conn->unp_cc - so2->so_rcv.sb_cc, RLIM_INFINITY); 349ff8b0106SBrian Feldman unp->unp_conn->unp_cc = so2->so_rcv.sb_cc; 350df8bae1dSRodney W. Grimes sorwakeup(so2); 351df8bae1dSRodney W. Grimes m = 0; 352df8bae1dSRodney W. Grimes break; 353df8bae1dSRodney W. Grimes 354df8bae1dSRodney W. Grimes default: 355a29f300eSGarrett Wollman panic("uipc_send unknown socktype"); 356df8bae1dSRodney W. Grimes } 357a29f300eSGarrett Wollman 3586b8fda4dSGarrett Wollman /* 3596b8fda4dSGarrett Wollman * SEND_EOF is equivalent to a SEND followed by 3606b8fda4dSGarrett Wollman * a SHUTDOWN. 3616b8fda4dSGarrett Wollman */ 362a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 3636b8fda4dSGarrett Wollman socantsendmore(so); 3646b8fda4dSGarrett Wollman unp_shutdown(unp); 3656b8fda4dSGarrett Wollman } 366df8bae1dSRodney W. Grimes 367bd508d39SDon Lewis if (control && error != 0) 368bd508d39SDon Lewis unp_dispose(control); 369bd508d39SDon Lewis 370a29f300eSGarrett Wollman release: 371a29f300eSGarrett Wollman if (control) 372a29f300eSGarrett Wollman m_freem(control); 373a29f300eSGarrett Wollman if (m) 374a29f300eSGarrett Wollman m_freem(m); 375a29f300eSGarrett Wollman return error; 376a29f300eSGarrett Wollman } 377df8bae1dSRodney W. Grimes 378a29f300eSGarrett Wollman static int 379a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 380a29f300eSGarrett Wollman { 381a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 382a29f300eSGarrett Wollman struct socket *so2; 383a29f300eSGarrett Wollman 384a29f300eSGarrett Wollman if (unp == 0) 385a29f300eSGarrett Wollman return EINVAL; 386a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 387df8bae1dSRodney W. Grimes if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) { 388df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 389a29f300eSGarrett Wollman sb->st_blksize += so2->so_rcv.sb_cc; 390df8bae1dSRodney W. Grimes } 391bfbb9ce6SPoul-Henning Kamp sb->st_dev = NOUDEV; 392df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 393df8bae1dSRodney W. Grimes unp->unp_ino = unp_ino++; 394a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 395df8bae1dSRodney W. Grimes return (0); 396a29f300eSGarrett Wollman } 397df8bae1dSRodney W. Grimes 398a29f300eSGarrett Wollman static int 399a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 400a29f300eSGarrett Wollman { 401a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 402df8bae1dSRodney W. Grimes 403a29f300eSGarrett Wollman if (unp == 0) 404a29f300eSGarrett Wollman return EINVAL; 405a29f300eSGarrett Wollman socantsendmore(so); 406a29f300eSGarrett Wollman unp_shutdown(unp); 407a29f300eSGarrett Wollman return 0; 408a29f300eSGarrett Wollman } 409df8bae1dSRodney W. Grimes 410a29f300eSGarrett Wollman static int 41157bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 412a29f300eSGarrett Wollman { 413a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 414a29f300eSGarrett Wollman 415a29f300eSGarrett Wollman if (unp == 0) 416a29f300eSGarrett Wollman return EINVAL; 41757bf258eSGarrett Wollman if (unp->unp_addr) 41857bf258eSGarrett Wollman *nam = dup_sockaddr((struct sockaddr *)unp->unp_addr, 1); 419a29f300eSGarrett Wollman return 0; 420df8bae1dSRodney W. Grimes } 421a29f300eSGarrett Wollman 422a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = { 423a29f300eSGarrett Wollman uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect, 424a29f300eSGarrett Wollman uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect, 425a29f300eSGarrett Wollman uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp, 426a29f300eSGarrett Wollman uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr, 42751338ea8SPeter Wemm sosend, soreceive, sopoll 428a29f300eSGarrett Wollman }; 429df8bae1dSRodney W. Grimes 430df8bae1dSRodney W. Grimes /* 431df8bae1dSRodney W. Grimes * Both send and receive buffers are allocated PIPSIZ bytes of buffering 432df8bae1dSRodney W. Grimes * for stream sockets, although the total for sender and receiver is 433df8bae1dSRodney W. Grimes * actually only PIPSIZ. 434df8bae1dSRodney W. Grimes * Datagram sockets really use the sendspace as the maximum datagram size, 435df8bae1dSRodney W. Grimes * and don't really want to reserve the sendspace. Their recvspace should 436df8bae1dSRodney W. Grimes * be large enough for at least one max-size datagram plus address. 437df8bae1dSRodney W. Grimes */ 4385dce41c5SJohn Dyson #ifndef PIPSIZ 4395dce41c5SJohn Dyson #define PIPSIZ 8192 4405dce41c5SJohn Dyson #endif 441f708ef1bSPoul-Henning Kamp static u_long unpst_sendspace = PIPSIZ; 442f708ef1bSPoul-Henning Kamp static u_long unpst_recvspace = PIPSIZ; 443f708ef1bSPoul-Henning Kamp static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 444f708ef1bSPoul-Henning Kamp static u_long unpdg_recvspace = 4*1024; 445df8bae1dSRodney W. Grimes 446f708ef1bSPoul-Henning Kamp static int unp_rights; /* file descriptors in flight */ 447df8bae1dSRodney W. Grimes 448ce02431fSDoug Rabson SYSCTL_DECL(_net_local_stream); 449639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 450639acc13SGarrett Wollman &unpst_sendspace, 0, ""); 451639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 452639acc13SGarrett Wollman &unpst_recvspace, 0, ""); 453ce02431fSDoug Rabson SYSCTL_DECL(_net_local_dgram); 454639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 455639acc13SGarrett Wollman &unpdg_sendspace, 0, ""); 456639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 457639acc13SGarrett Wollman &unpdg_recvspace, 0, ""); 458ce02431fSDoug Rabson SYSCTL_DECL(_net_local); 459639acc13SGarrett Wollman SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); 460639acc13SGarrett Wollman 461f708ef1bSPoul-Henning Kamp static int 462df8bae1dSRodney W. Grimes unp_attach(so) 463df8bae1dSRodney W. Grimes struct socket *so; 464df8bae1dSRodney W. Grimes { 465df8bae1dSRodney W. Grimes register struct unpcb *unp; 466df8bae1dSRodney W. Grimes int error; 467df8bae1dSRodney W. Grimes 468df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 469df8bae1dSRodney W. Grimes switch (so->so_type) { 470df8bae1dSRodney W. Grimes 471df8bae1dSRodney W. Grimes case SOCK_STREAM: 472df8bae1dSRodney W. Grimes error = soreserve(so, unpst_sendspace, unpst_recvspace); 473df8bae1dSRodney W. Grimes break; 474df8bae1dSRodney W. Grimes 475df8bae1dSRodney W. Grimes case SOCK_DGRAM: 476df8bae1dSRodney W. Grimes error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 477df8bae1dSRodney W. Grimes break; 478df8bae1dSRodney W. Grimes 479df8bae1dSRodney W. Grimes default: 480df8bae1dSRodney W. Grimes panic("unp_attach"); 481df8bae1dSRodney W. Grimes } 482df8bae1dSRodney W. Grimes if (error) 483df8bae1dSRodney W. Grimes return (error); 484df8bae1dSRodney W. Grimes } 48598271db4SGarrett Wollman unp = zalloc(unp_zone); 48657bf258eSGarrett Wollman if (unp == NULL) 487df8bae1dSRodney W. Grimes return (ENOBUFS); 48857bf258eSGarrett Wollman bzero(unp, sizeof *unp); 48998271db4SGarrett Wollman unp->unp_gencnt = ++unp_gencnt; 49098271db4SGarrett Wollman unp_count++; 49198271db4SGarrett Wollman LIST_INIT(&unp->unp_refs); 492df8bae1dSRodney W. Grimes unp->unp_socket = so; 49375c13541SPoul-Henning Kamp unp->unp_rvnode = curproc->p_fd->fd_rdir; 49498271db4SGarrett Wollman LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead 49598271db4SGarrett Wollman : &unp_shead, unp, unp_link); 49698271db4SGarrett Wollman so->so_pcb = (caddr_t)unp; 497df8bae1dSRodney W. Grimes return (0); 498df8bae1dSRodney W. Grimes } 499df8bae1dSRodney W. Grimes 500f708ef1bSPoul-Henning Kamp static void 501df8bae1dSRodney W. Grimes unp_detach(unp) 502df8bae1dSRodney W. Grimes register struct unpcb *unp; 503df8bae1dSRodney W. Grimes { 50498271db4SGarrett Wollman LIST_REMOVE(unp, unp_link); 50598271db4SGarrett Wollman unp->unp_gencnt = ++unp_gencnt; 50698271db4SGarrett Wollman --unp_count; 507df8bae1dSRodney W. Grimes if (unp->unp_vnode) { 508df8bae1dSRodney W. Grimes unp->unp_vnode->v_socket = 0; 509df8bae1dSRodney W. Grimes vrele(unp->unp_vnode); 510df8bae1dSRodney W. Grimes unp->unp_vnode = 0; 511df8bae1dSRodney W. Grimes } 512df8bae1dSRodney W. Grimes if (unp->unp_conn) 513df8bae1dSRodney W. Grimes unp_disconnect(unp); 5142e3c8fcbSPoul-Henning Kamp while (!LIST_EMPTY(&unp->unp_refs)) 5152e3c8fcbSPoul-Henning Kamp unp_drop(LIST_FIRST(&unp->unp_refs), ECONNRESET); 516df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 517df8bae1dSRodney W. Grimes unp->unp_socket->so_pcb = 0; 518df8bae1dSRodney W. Grimes if (unp_rights) { 519df8bae1dSRodney W. Grimes /* 520df8bae1dSRodney W. Grimes * Normally the receive buffer is flushed later, 521df8bae1dSRodney W. Grimes * in sofree, but if our receive buffer holds references 522df8bae1dSRodney W. Grimes * to descriptors that are now garbage, we will dispose 523df8bae1dSRodney W. Grimes * of those descriptor references after the garbage collector 524df8bae1dSRodney W. Grimes * gets them (resulting in a "panic: closef: count < 0"). 525df8bae1dSRodney W. Grimes */ 526df8bae1dSRodney W. Grimes sorflush(unp->unp_socket); 527df8bae1dSRodney W. Grimes unp_gc(); 528df8bae1dSRodney W. Grimes } 52957bf258eSGarrett Wollman if (unp->unp_addr) 53057bf258eSGarrett Wollman FREE(unp->unp_addr, M_SONAME); 53198271db4SGarrett Wollman zfree(unp_zone, unp); 532df8bae1dSRodney W. Grimes } 533df8bae1dSRodney W. Grimes 534f708ef1bSPoul-Henning Kamp static int 535df8bae1dSRodney W. Grimes unp_bind(unp, nam, p) 536df8bae1dSRodney W. Grimes struct unpcb *unp; 53757bf258eSGarrett Wollman struct sockaddr *nam; 538df8bae1dSRodney W. Grimes struct proc *p; 539df8bae1dSRodney W. Grimes { 54057bf258eSGarrett Wollman struct sockaddr_un *soun = (struct sockaddr_un *)nam; 541df8bae1dSRodney W. Grimes register struct vnode *vp; 542df8bae1dSRodney W. Grimes struct vattr vattr; 54357bf258eSGarrett Wollman int error, namelen; 544df8bae1dSRodney W. Grimes struct nameidata nd; 54557bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 546df8bae1dSRodney W. Grimes 547df8bae1dSRodney W. Grimes if (unp->unp_vnode != NULL) 548df8bae1dSRodney W. Grimes return (EINVAL); 54957bf258eSGarrett Wollman #define offsetof(s, e) ((char *)&((s *)0)->e - (char *)((s *)0)) 55057bf258eSGarrett Wollman namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 55157bf258eSGarrett Wollman if (namelen <= 0) 55257bf258eSGarrett Wollman return EINVAL; 55357bf258eSGarrett Wollman strncpy(buf, soun->sun_path, namelen); 55457bf258eSGarrett Wollman buf[namelen] = 0; /* null-terminate the string */ 555974784e8SGuido van Rooij NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT, UIO_SYSSPACE, 55657bf258eSGarrett Wollman buf, p); 557df8bae1dSRodney W. Grimes /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 558797f2d22SPoul-Henning Kamp error = namei(&nd); 559797f2d22SPoul-Henning Kamp if (error) 560df8bae1dSRodney W. Grimes return (error); 561df8bae1dSRodney W. Grimes vp = nd.ni_vp; 562df8bae1dSRodney W. Grimes if (vp != NULL) { 563762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 564df8bae1dSRodney W. Grimes if (nd.ni_dvp == vp) 565df8bae1dSRodney W. Grimes vrele(nd.ni_dvp); 566df8bae1dSRodney W. Grimes else 567df8bae1dSRodney W. Grimes vput(nd.ni_dvp); 568df8bae1dSRodney W. Grimes vrele(vp); 569df8bae1dSRodney W. Grimes return (EADDRINUSE); 570df8bae1dSRodney W. Grimes } 571df8bae1dSRodney W. Grimes VATTR_NULL(&vattr); 572df8bae1dSRodney W. Grimes vattr.va_type = VSOCK; 573a29f300eSGarrett Wollman vattr.va_mode = (ACCESSPERMS & ~p->p_fd->fd_cmask); 574996c772fSJohn Dyson VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); 5757be2d300SMike Smith error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 576762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 5777be2d300SMike Smith vput(nd.ni_dvp); 5787be2d300SMike Smith if (error) 579df8bae1dSRodney W. Grimes return (error); 580df8bae1dSRodney W. Grimes vp = nd.ni_vp; 581df8bae1dSRodney W. Grimes vp->v_socket = unp->unp_socket; 582df8bae1dSRodney W. Grimes unp->unp_vnode = vp; 58357bf258eSGarrett Wollman unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam, 1); 584996c772fSJohn Dyson VOP_UNLOCK(vp, 0, p); 585df8bae1dSRodney W. Grimes return (0); 586df8bae1dSRodney W. Grimes } 587df8bae1dSRodney W. Grimes 588f708ef1bSPoul-Henning Kamp static int 589df8bae1dSRodney W. Grimes unp_connect(so, nam, p) 590df8bae1dSRodney W. Grimes struct socket *so; 59157bf258eSGarrett Wollman struct sockaddr *nam; 592df8bae1dSRodney W. Grimes struct proc *p; 593df8bae1dSRodney W. Grimes { 59457bf258eSGarrett Wollman register struct sockaddr_un *soun = (struct sockaddr_un *)nam; 595df8bae1dSRodney W. Grimes register struct vnode *vp; 596df8bae1dSRodney W. Grimes register struct socket *so2, *so3; 597df8bae1dSRodney W. Grimes struct unpcb *unp2, *unp3; 59857bf258eSGarrett Wollman int error, len; 599df8bae1dSRodney W. Grimes struct nameidata nd; 60057bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 601df8bae1dSRodney W. Grimes 60257bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 60357bf258eSGarrett Wollman if (len <= 0) 60457bf258eSGarrett Wollman return EINVAL; 60557bf258eSGarrett Wollman strncpy(buf, soun->sun_path, len); 60657bf258eSGarrett Wollman buf[len] = 0; 60757bf258eSGarrett Wollman 60857bf258eSGarrett Wollman NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, p); 609797f2d22SPoul-Henning Kamp error = namei(&nd); 610797f2d22SPoul-Henning Kamp if (error) 611df8bae1dSRodney W. Grimes return (error); 612df8bae1dSRodney W. Grimes vp = nd.ni_vp; 613762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 614df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 615df8bae1dSRodney W. Grimes error = ENOTSOCK; 616df8bae1dSRodney W. Grimes goto bad; 617df8bae1dSRodney W. Grimes } 618797f2d22SPoul-Henning Kamp error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p); 619797f2d22SPoul-Henning Kamp if (error) 620df8bae1dSRodney W. Grimes goto bad; 621df8bae1dSRodney W. Grimes so2 = vp->v_socket; 622df8bae1dSRodney W. Grimes if (so2 == 0) { 623df8bae1dSRodney W. Grimes error = ECONNREFUSED; 624df8bae1dSRodney W. Grimes goto bad; 625df8bae1dSRodney W. Grimes } 626df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 627df8bae1dSRodney W. Grimes error = EPROTOTYPE; 628df8bae1dSRodney W. Grimes goto bad; 629df8bae1dSRodney W. Grimes } 630df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 631df8bae1dSRodney W. Grimes if ((so2->so_options & SO_ACCEPTCONN) == 0 || 6322f9a2132SBrian Feldman (so3 = sonewconn3(so2, 0, p)) == 0) { 633df8bae1dSRodney W. Grimes error = ECONNREFUSED; 634df8bae1dSRodney W. Grimes goto bad; 635df8bae1dSRodney W. Grimes } 636df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 637df8bae1dSRodney W. Grimes unp3 = sotounpcb(so3); 638df8bae1dSRodney W. Grimes if (unp2->unp_addr) 63957bf258eSGarrett Wollman unp3->unp_addr = (struct sockaddr_un *) 64057bf258eSGarrett Wollman dup_sockaddr((struct sockaddr *) 64157bf258eSGarrett Wollman unp2->unp_addr, 1); 642df8bae1dSRodney W. Grimes so2 = so3; 643df8bae1dSRodney W. Grimes } 644df8bae1dSRodney W. Grimes error = unp_connect2(so, so2); 645df8bae1dSRodney W. Grimes bad: 646df8bae1dSRodney W. Grimes vput(vp); 647df8bae1dSRodney W. Grimes return (error); 648df8bae1dSRodney W. Grimes } 649df8bae1dSRodney W. Grimes 65026f9a767SRodney W. Grimes int 651df8bae1dSRodney W. Grimes unp_connect2(so, so2) 652df8bae1dSRodney W. Grimes register struct socket *so; 653df8bae1dSRodney W. Grimes register struct socket *so2; 654df8bae1dSRodney W. Grimes { 655df8bae1dSRodney W. Grimes register struct unpcb *unp = sotounpcb(so); 656df8bae1dSRodney W. Grimes register struct unpcb *unp2; 657df8bae1dSRodney W. Grimes 658df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 659df8bae1dSRodney W. Grimes return (EPROTOTYPE); 660df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 661df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 662df8bae1dSRodney W. Grimes switch (so->so_type) { 663df8bae1dSRodney W. Grimes 664df8bae1dSRodney W. Grimes case SOCK_DGRAM: 66598271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 666df8bae1dSRodney W. Grimes soisconnected(so); 667df8bae1dSRodney W. Grimes break; 668df8bae1dSRodney W. Grimes 669df8bae1dSRodney W. Grimes case SOCK_STREAM: 670df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 671df8bae1dSRodney W. Grimes soisconnected(so); 672df8bae1dSRodney W. Grimes soisconnected(so2); 673df8bae1dSRodney W. Grimes break; 674df8bae1dSRodney W. Grimes 675df8bae1dSRodney W. Grimes default: 676df8bae1dSRodney W. Grimes panic("unp_connect2"); 677df8bae1dSRodney W. Grimes } 678df8bae1dSRodney W. Grimes return (0); 679df8bae1dSRodney W. Grimes } 680df8bae1dSRodney W. Grimes 681f708ef1bSPoul-Henning Kamp static void 682df8bae1dSRodney W. Grimes unp_disconnect(unp) 683df8bae1dSRodney W. Grimes struct unpcb *unp; 684df8bae1dSRodney W. Grimes { 685df8bae1dSRodney W. Grimes register struct unpcb *unp2 = unp->unp_conn; 686df8bae1dSRodney W. Grimes 687df8bae1dSRodney W. Grimes if (unp2 == 0) 688df8bae1dSRodney W. Grimes return; 689df8bae1dSRodney W. Grimes unp->unp_conn = 0; 690df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 691df8bae1dSRodney W. Grimes 692df8bae1dSRodney W. Grimes case SOCK_DGRAM: 69398271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 694df8bae1dSRodney W. Grimes unp->unp_socket->so_state &= ~SS_ISCONNECTED; 695df8bae1dSRodney W. Grimes break; 696df8bae1dSRodney W. Grimes 697df8bae1dSRodney W. Grimes case SOCK_STREAM: 698df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 699df8bae1dSRodney W. Grimes unp2->unp_conn = 0; 700df8bae1dSRodney W. Grimes soisdisconnected(unp2->unp_socket); 701df8bae1dSRodney W. Grimes break; 702df8bae1dSRodney W. Grimes } 703df8bae1dSRodney W. Grimes } 704df8bae1dSRodney W. Grimes 705df8bae1dSRodney W. Grimes #ifdef notdef 70626f9a767SRodney W. Grimes void 707df8bae1dSRodney W. Grimes unp_abort(unp) 708df8bae1dSRodney W. Grimes struct unpcb *unp; 709df8bae1dSRodney W. Grimes { 710df8bae1dSRodney W. Grimes 711df8bae1dSRodney W. Grimes unp_detach(unp); 712df8bae1dSRodney W. Grimes } 713df8bae1dSRodney W. Grimes #endif 714df8bae1dSRodney W. Grimes 71598271db4SGarrett Wollman static int 71675c13541SPoul-Henning Kamp prison_unpcb(struct proc *p, struct unpcb *unp) 71775c13541SPoul-Henning Kamp { 71875c13541SPoul-Henning Kamp if (!p->p_prison) 71975c13541SPoul-Henning Kamp return (0); 72075c13541SPoul-Henning Kamp if (p->p_fd->fd_rdir == unp->unp_rvnode) 72175c13541SPoul-Henning Kamp return (0); 72275c13541SPoul-Henning Kamp return (1); 72375c13541SPoul-Henning Kamp } 72475c13541SPoul-Henning Kamp 72575c13541SPoul-Henning Kamp static int 72682d9ae4eSPoul-Henning Kamp unp_pcblist (SYSCTL_HANDLER_ARGS) 72798271db4SGarrett Wollman { 728f5ef029eSPoul-Henning Kamp int error, i, n; 72998271db4SGarrett Wollman struct unpcb *unp, **unp_list; 73098271db4SGarrett Wollman unp_gen_t gencnt; 73198271db4SGarrett Wollman struct xunpgen xug; 73298271db4SGarrett Wollman struct unp_head *head; 73398271db4SGarrett Wollman 734a23d65bfSBruce Evans head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 73598271db4SGarrett Wollman 73698271db4SGarrett Wollman /* 73798271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 73898271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 73998271db4SGarrett Wollman */ 74098271db4SGarrett Wollman if (req->oldptr == 0) { 74198271db4SGarrett Wollman n = unp_count; 74298271db4SGarrett Wollman req->oldidx = 2 * (sizeof xug) 74398271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 74498271db4SGarrett Wollman return 0; 74598271db4SGarrett Wollman } 74698271db4SGarrett Wollman 74798271db4SGarrett Wollman if (req->newptr != 0) 74898271db4SGarrett Wollman return EPERM; 74998271db4SGarrett Wollman 75098271db4SGarrett Wollman /* 75198271db4SGarrett Wollman * OK, now we're committed to doing something. 75298271db4SGarrett Wollman */ 75398271db4SGarrett Wollman gencnt = unp_gencnt; 75498271db4SGarrett Wollman n = unp_count; 75598271db4SGarrett Wollman 75698271db4SGarrett Wollman xug.xug_len = sizeof xug; 75798271db4SGarrett Wollman xug.xug_count = n; 75898271db4SGarrett Wollman xug.xug_gen = gencnt; 75998271db4SGarrett Wollman xug.xug_sogen = so_gencnt; 76098271db4SGarrett Wollman error = SYSCTL_OUT(req, &xug, sizeof xug); 76198271db4SGarrett Wollman if (error) 76298271db4SGarrett Wollman return error; 76398271db4SGarrett Wollman 76498271db4SGarrett Wollman unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 76598271db4SGarrett Wollman if (unp_list == 0) 76698271db4SGarrett Wollman return ENOMEM; 76798271db4SGarrett Wollman 7682e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 7692e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 77075c13541SPoul-Henning Kamp if (unp->unp_gencnt <= gencnt && !prison_unpcb(req->p, unp)) 77198271db4SGarrett Wollman unp_list[i++] = unp; 77298271db4SGarrett Wollman } 77398271db4SGarrett Wollman n = i; /* in case we lost some during malloc */ 77498271db4SGarrett Wollman 77598271db4SGarrett Wollman error = 0; 77698271db4SGarrett Wollman for (i = 0; i < n; i++) { 77798271db4SGarrett Wollman unp = unp_list[i]; 77898271db4SGarrett Wollman if (unp->unp_gencnt <= gencnt) { 77998271db4SGarrett Wollman struct xunpcb xu; 78098271db4SGarrett Wollman xu.xu_len = sizeof xu; 78198271db4SGarrett Wollman xu.xu_unpp = unp; 78298271db4SGarrett Wollman /* 78398271db4SGarrett Wollman * XXX - need more locking here to protect against 78498271db4SGarrett Wollman * connect/disconnect races for SMP. 78598271db4SGarrett Wollman */ 78698271db4SGarrett Wollman if (unp->unp_addr) 78798271db4SGarrett Wollman bcopy(unp->unp_addr, &xu.xu_addr, 78898271db4SGarrett Wollman unp->unp_addr->sun_len); 78998271db4SGarrett Wollman if (unp->unp_conn && unp->unp_conn->unp_addr) 79098271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 79198271db4SGarrett Wollman &xu.xu_caddr, 79298271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 79398271db4SGarrett Wollman bcopy(unp, &xu.xu_unp, sizeof *unp); 79498271db4SGarrett Wollman sotoxsocket(unp->unp_socket, &xu.xu_socket); 79598271db4SGarrett Wollman error = SYSCTL_OUT(req, &xu, sizeof xu); 79698271db4SGarrett Wollman } 79798271db4SGarrett Wollman } 79898271db4SGarrett Wollman if (!error) { 79998271db4SGarrett Wollman /* 80098271db4SGarrett Wollman * Give the user an updated idea of our state. 80198271db4SGarrett Wollman * If the generation differs from what we told 80298271db4SGarrett Wollman * her before, she knows that something happened 80398271db4SGarrett Wollman * while we were processing this request, and it 80498271db4SGarrett Wollman * might be necessary to retry. 80598271db4SGarrett Wollman */ 80698271db4SGarrett Wollman xug.xug_gen = unp_gencnt; 80798271db4SGarrett Wollman xug.xug_sogen = so_gencnt; 80898271db4SGarrett Wollman xug.xug_count = unp_count; 80998271db4SGarrett Wollman error = SYSCTL_OUT(req, &xug, sizeof xug); 81098271db4SGarrett Wollman } 81198271db4SGarrett Wollman free(unp_list, M_TEMP); 81298271db4SGarrett Wollman return error; 81398271db4SGarrett Wollman } 81498271db4SGarrett Wollman 81598271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 81698271db4SGarrett Wollman (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 81798271db4SGarrett Wollman "List of active local datagram sockets"); 81898271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 81998271db4SGarrett Wollman (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 82098271db4SGarrett Wollman "List of active local stream sockets"); 82198271db4SGarrett Wollman 822f708ef1bSPoul-Henning Kamp static void 823df8bae1dSRodney W. Grimes unp_shutdown(unp) 824df8bae1dSRodney W. Grimes struct unpcb *unp; 825df8bae1dSRodney W. Grimes { 826df8bae1dSRodney W. Grimes struct socket *so; 827df8bae1dSRodney W. Grimes 828df8bae1dSRodney W. Grimes if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 829df8bae1dSRodney W. Grimes (so = unp->unp_conn->unp_socket)) 830df8bae1dSRodney W. Grimes socantrcvmore(so); 831df8bae1dSRodney W. Grimes } 832df8bae1dSRodney W. Grimes 833f708ef1bSPoul-Henning Kamp static void 834df8bae1dSRodney W. Grimes unp_drop(unp, errno) 835df8bae1dSRodney W. Grimes struct unpcb *unp; 836df8bae1dSRodney W. Grimes int errno; 837df8bae1dSRodney W. Grimes { 838df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 839df8bae1dSRodney W. Grimes 840df8bae1dSRodney W. Grimes so->so_error = errno; 841df8bae1dSRodney W. Grimes unp_disconnect(unp); 842df8bae1dSRodney W. Grimes if (so->so_head) { 84398271db4SGarrett Wollman LIST_REMOVE(unp, unp_link); 84498271db4SGarrett Wollman unp->unp_gencnt = ++unp_gencnt; 84598271db4SGarrett Wollman unp_count--; 846df8bae1dSRodney W. Grimes so->so_pcb = (caddr_t) 0; 84757bf258eSGarrett Wollman if (unp->unp_addr) 84857bf258eSGarrett Wollman FREE(unp->unp_addr, M_SONAME); 84998271db4SGarrett Wollman zfree(unp_zone, unp); 850df8bae1dSRodney W. Grimes sofree(so); 851df8bae1dSRodney W. Grimes } 852df8bae1dSRodney W. Grimes } 853df8bae1dSRodney W. Grimes 854df8bae1dSRodney W. Grimes #ifdef notdef 85526f9a767SRodney W. Grimes void 856df8bae1dSRodney W. Grimes unp_drain() 857df8bae1dSRodney W. Grimes { 858df8bae1dSRodney W. Grimes 859df8bae1dSRodney W. Grimes } 860df8bae1dSRodney W. Grimes #endif 861df8bae1dSRodney W. Grimes 86226f9a767SRodney W. Grimes int 863df8bae1dSRodney W. Grimes unp_externalize(rights) 864df8bae1dSRodney W. Grimes struct mbuf *rights; 865df8bae1dSRodney W. Grimes { 866df8bae1dSRodney W. Grimes struct proc *p = curproc; /* XXX */ 867df8bae1dSRodney W. Grimes register int i; 868df8bae1dSRodney W. Grimes register struct cmsghdr *cm = mtod(rights, struct cmsghdr *); 8698692c025SYoshinobu Inoue register int *fdp; 8708692c025SYoshinobu Inoue register struct file **rp; 871df8bae1dSRodney W. Grimes register struct file *fp; 8728692c025SYoshinobu Inoue int newfds = (cm->cmsg_len - (CMSG_DATA(cm) - (u_char *)cm)) 8738692c025SYoshinobu Inoue / sizeof (struct file *); 874df8bae1dSRodney W. Grimes int f; 875df8bae1dSRodney W. Grimes 876ed5b7817SJulian Elischer /* 877ed5b7817SJulian Elischer * if the new FD's will not fit, then we free them all 878ed5b7817SJulian Elischer */ 879df8bae1dSRodney W. Grimes if (!fdavail(p, newfds)) { 8808692c025SYoshinobu Inoue rp = (struct file **)CMSG_DATA(cm); 881df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 882df8bae1dSRodney W. Grimes fp = *rp; 8838692c025SYoshinobu Inoue /* 8848692c025SYoshinobu Inoue * zero the pointer before calling unp_discard, 8858692c025SYoshinobu Inoue * since it may end up in unp_gc().. 8868692c025SYoshinobu Inoue */ 887df8bae1dSRodney W. Grimes *rp++ = 0; 8888692c025SYoshinobu Inoue unp_discard(fp); 889df8bae1dSRodney W. Grimes } 890df8bae1dSRodney W. Grimes return (EMSGSIZE); 891df8bae1dSRodney W. Grimes } 892ed5b7817SJulian Elischer /* 893ed5b7817SJulian Elischer * now change each pointer to an fd in the global table to 894ed5b7817SJulian Elischer * an integer that is the index to the local fd table entry 895ed5b7817SJulian Elischer * that we set up to point to the global one we are transferring. 8968692c025SYoshinobu Inoue * If sizeof (struct file *) is bigger than or equal to sizeof int, 8978692c025SYoshinobu Inoue * then do it in forward order. In that case, an integer will 8988692c025SYoshinobu Inoue * always come in the same place or before its corresponding 8998692c025SYoshinobu Inoue * struct file pointer. 9008692c025SYoshinobu Inoue * If sizeof (struct file *) is smaller than sizeof int, then 9018692c025SYoshinobu Inoue * do it in reverse order. 902ed5b7817SJulian Elischer */ 9038692c025SYoshinobu Inoue if (sizeof (struct file *) >= sizeof (int)) { 9048692c025SYoshinobu Inoue fdp = (int *)(cm + 1); 9058692c025SYoshinobu Inoue rp = (struct file **)CMSG_DATA(cm); 906df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 907df8bae1dSRodney W. Grimes if (fdalloc(p, 0, &f)) 908df8bae1dSRodney W. Grimes panic("unp_externalize"); 9098692c025SYoshinobu Inoue fp = *rp++; 910df8bae1dSRodney W. Grimes p->p_fd->fd_ofiles[f] = fp; 911df8bae1dSRodney W. Grimes fp->f_msgcount--; 912df8bae1dSRodney W. Grimes unp_rights--; 9138692c025SYoshinobu Inoue *fdp++ = f; 914df8bae1dSRodney W. Grimes } 9158692c025SYoshinobu Inoue } else { 9168692c025SYoshinobu Inoue fdp = (int *)(cm + 1) + newfds - 1; 9178692c025SYoshinobu Inoue rp = (struct file **)CMSG_DATA(cm) + newfds - 1; 9188692c025SYoshinobu Inoue for (i = 0; i < newfds; i++) { 9198692c025SYoshinobu Inoue if (fdalloc(p, 0, &f)) 9208692c025SYoshinobu Inoue panic("unp_externalize"); 9218692c025SYoshinobu Inoue fp = *rp--; 9228692c025SYoshinobu Inoue p->p_fd->fd_ofiles[f] = fp; 9238692c025SYoshinobu Inoue fp->f_msgcount--; 9248692c025SYoshinobu Inoue unp_rights--; 9258692c025SYoshinobu Inoue *fdp-- = f; 9268692c025SYoshinobu Inoue } 9278692c025SYoshinobu Inoue } 9288692c025SYoshinobu Inoue 9298692c025SYoshinobu Inoue /* 9308692c025SYoshinobu Inoue * Adjust length, in case sizeof(struct file *) and sizeof(int) 9318692c025SYoshinobu Inoue * differs. 9328692c025SYoshinobu Inoue */ 9338692c025SYoshinobu Inoue cm->cmsg_len = CMSG_LEN(newfds * sizeof(int)); 9348692c025SYoshinobu Inoue rights->m_len = cm->cmsg_len; 935df8bae1dSRodney W. Grimes return (0); 936df8bae1dSRodney W. Grimes } 937df8bae1dSRodney W. Grimes 93898271db4SGarrett Wollman void 93998271db4SGarrett Wollman unp_init(void) 94098271db4SGarrett Wollman { 94198271db4SGarrett Wollman unp_zone = zinit("unpcb", sizeof(struct unpcb), nmbclusters, 0, 0); 94298271db4SGarrett Wollman if (unp_zone == 0) 94398271db4SGarrett Wollman panic("unp_init"); 94498271db4SGarrett Wollman LIST_INIT(&unp_dhead); 94598271db4SGarrett Wollman LIST_INIT(&unp_shead); 94698271db4SGarrett Wollman } 94798271db4SGarrett Wollman 9480b788fa1SBill Paul #ifndef MIN 9490b788fa1SBill Paul #define MIN(a,b) (((a)<(b))?(a):(b)) 9500b788fa1SBill Paul #endif 9510b788fa1SBill Paul 952f708ef1bSPoul-Henning Kamp static int 953df8bae1dSRodney W. Grimes unp_internalize(control, p) 954df8bae1dSRodney W. Grimes struct mbuf *control; 955df8bae1dSRodney W. Grimes struct proc *p; 956df8bae1dSRodney W. Grimes { 9578692c025SYoshinobu Inoue struct filedesc *fdescp = p->p_fd; 958df8bae1dSRodney W. Grimes register struct cmsghdr *cm = mtod(control, struct cmsghdr *); 959df8bae1dSRodney W. Grimes register struct file **rp; 960df8bae1dSRodney W. Grimes register struct file *fp; 9618692c025SYoshinobu Inoue register int i, fd, *fdp; 9620b788fa1SBill Paul register struct cmsgcred *cmcred; 963df8bae1dSRodney W. Grimes int oldfds; 9648692c025SYoshinobu Inoue u_int newlen; 965df8bae1dSRodney W. Grimes 9660b788fa1SBill Paul if ((cm->cmsg_type != SCM_RIGHTS && cm->cmsg_type != SCM_CREDS) || 9670b788fa1SBill Paul cm->cmsg_level != SOL_SOCKET || cm->cmsg_len != control->m_len) 968df8bae1dSRodney W. Grimes return (EINVAL); 9690b788fa1SBill Paul 9700b788fa1SBill Paul /* 9710b788fa1SBill Paul * Fill in credential information. 9720b788fa1SBill Paul */ 9730b788fa1SBill Paul if (cm->cmsg_type == SCM_CREDS) { 9740b788fa1SBill Paul cmcred = (struct cmsgcred *)(cm + 1); 9750b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 9760b788fa1SBill Paul cmcred->cmcred_uid = p->p_cred->p_ruid; 9770b788fa1SBill Paul cmcred->cmcred_gid = p->p_cred->p_rgid; 9780b788fa1SBill Paul cmcred->cmcred_euid = p->p_ucred->cr_uid; 9790b788fa1SBill Paul cmcred->cmcred_ngroups = MIN(p->p_ucred->cr_ngroups, 9800b788fa1SBill Paul CMGROUP_MAX); 9810b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 9820b788fa1SBill Paul cmcred->cmcred_groups[i] = p->p_ucred->cr_groups[i]; 9830b788fa1SBill Paul return(0); 9840b788fa1SBill Paul } 9850b788fa1SBill Paul 986df8bae1dSRodney W. Grimes oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int); 987ed5b7817SJulian Elischer /* 988ed5b7817SJulian Elischer * check that all the FDs passed in refer to legal OPEN files 989ed5b7817SJulian Elischer * If not, reject the entire operation. 990ed5b7817SJulian Elischer */ 9918692c025SYoshinobu Inoue fdp = (int *)(cm + 1); 992df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 9938692c025SYoshinobu Inoue fd = *fdp++; 9948692c025SYoshinobu Inoue if ((unsigned)fd >= fdescp->fd_nfiles || 9958692c025SYoshinobu Inoue fdescp->fd_ofiles[fd] == NULL) 996df8bae1dSRodney W. Grimes return (EBADF); 997df8bae1dSRodney W. Grimes } 998ed5b7817SJulian Elischer /* 999ed5b7817SJulian Elischer * Now replace the integer FDs with pointers to 1000ed5b7817SJulian Elischer * the associated global file table entry.. 10018692c025SYoshinobu Inoue * Allocate a bigger buffer as necessary. But if an cluster is not 10028692c025SYoshinobu Inoue * enough, return E2BIG. 1003ed5b7817SJulian Elischer */ 10048692c025SYoshinobu Inoue newlen = CMSG_LEN(oldfds * sizeof(struct file *)); 10058692c025SYoshinobu Inoue if (newlen > MCLBYTES) 10068692c025SYoshinobu Inoue return (E2BIG); 10078692c025SYoshinobu Inoue if (newlen - control->m_len > M_TRAILINGSPACE(control)) { 10088692c025SYoshinobu Inoue if (control->m_flags & M_EXT) 10098692c025SYoshinobu Inoue return (E2BIG); 10108692c025SYoshinobu Inoue MCLGET(control, M_WAIT); 10118692c025SYoshinobu Inoue if ((control->m_flags & M_EXT) == 0) 10128692c025SYoshinobu Inoue return (ENOBUFS); 10138692c025SYoshinobu Inoue 10148692c025SYoshinobu Inoue /* copy the data to the cluster */ 10158692c025SYoshinobu Inoue memcpy(mtod(control, char *), cm, cm->cmsg_len); 10168692c025SYoshinobu Inoue cm = mtod(control, struct cmsghdr *); 10178692c025SYoshinobu Inoue } 10188692c025SYoshinobu Inoue 10198692c025SYoshinobu Inoue /* 10208692c025SYoshinobu Inoue * Adjust length, in case sizeof(struct file *) and sizeof(int) 10218692c025SYoshinobu Inoue * differs. 10228692c025SYoshinobu Inoue */ 10238692c025SYoshinobu Inoue control->m_len = cm->cmsg_len = newlen; 10248692c025SYoshinobu Inoue 10258692c025SYoshinobu Inoue /* 10268692c025SYoshinobu Inoue * Transform the file descriptors into struct file pointers. 10278692c025SYoshinobu Inoue * If sizeof (struct file *) is bigger than or equal to sizeof int, 10288692c025SYoshinobu Inoue * then do it in reverse order so that the int won't get until 10298692c025SYoshinobu Inoue * we're done. 10308692c025SYoshinobu Inoue * If sizeof (struct file *) is smaller than sizeof int, then 10318692c025SYoshinobu Inoue * do it in forward order. 10328692c025SYoshinobu Inoue */ 10338692c025SYoshinobu Inoue if (sizeof (struct file *) >= sizeof (int)) { 10348692c025SYoshinobu Inoue fdp = (int *)(cm + 1) + oldfds - 1; 10358692c025SYoshinobu Inoue rp = (struct file **)CMSG_DATA(cm) + oldfds - 1; 1036df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 10378692c025SYoshinobu Inoue fp = fdescp->fd_ofiles[*fdp--]; 10388692c025SYoshinobu Inoue *rp-- = fp; 10398692c025SYoshinobu Inoue fp->f_count++; 10408692c025SYoshinobu Inoue fp->f_msgcount++; 10418692c025SYoshinobu Inoue unp_rights++; 10428692c025SYoshinobu Inoue } 10438692c025SYoshinobu Inoue } else { 10448692c025SYoshinobu Inoue fdp = (int *)(cm + 1); 10458692c025SYoshinobu Inoue rp = (struct file **)CMSG_DATA(cm); 10468692c025SYoshinobu Inoue for (i = 0; i < oldfds; i++) { 10478692c025SYoshinobu Inoue fp = fdescp->fd_ofiles[*fdp++]; 1048df8bae1dSRodney W. Grimes *rp++ = fp; 1049df8bae1dSRodney W. Grimes fp->f_count++; 1050df8bae1dSRodney W. Grimes fp->f_msgcount++; 1051df8bae1dSRodney W. Grimes unp_rights++; 1052df8bae1dSRodney W. Grimes } 10538692c025SYoshinobu Inoue } 1054df8bae1dSRodney W. Grimes return (0); 1055df8bae1dSRodney W. Grimes } 1056df8bae1dSRodney W. Grimes 1057f708ef1bSPoul-Henning Kamp static int unp_defer, unp_gcing; 1058df8bae1dSRodney W. Grimes 1059f708ef1bSPoul-Henning Kamp static void 1060df8bae1dSRodney W. Grimes unp_gc() 1061df8bae1dSRodney W. Grimes { 1062df8bae1dSRodney W. Grimes register struct file *fp, *nextfp; 1063df8bae1dSRodney W. Grimes register struct socket *so; 1064df8bae1dSRodney W. Grimes struct file **extra_ref, **fpp; 1065df8bae1dSRodney W. Grimes int nunref, i; 1066df8bae1dSRodney W. Grimes 1067df8bae1dSRodney W. Grimes if (unp_gcing) 1068df8bae1dSRodney W. Grimes return; 1069df8bae1dSRodney W. Grimes unp_gcing = 1; 1070df8bae1dSRodney W. Grimes unp_defer = 0; 1071ed5b7817SJulian Elischer /* 1072ed5b7817SJulian Elischer * before going through all this, set all FDs to 1073ed5b7817SJulian Elischer * be NOT defered and NOT externally accessible 1074ed5b7817SJulian Elischer */ 10752e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) 1076df8bae1dSRodney W. Grimes fp->f_flag &= ~(FMARK|FDEFER); 1077df8bae1dSRodney W. Grimes do { 10782e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) { 1079ed5b7817SJulian Elischer /* 1080ed5b7817SJulian Elischer * If the file is not open, skip it 1081ed5b7817SJulian Elischer */ 1082df8bae1dSRodney W. Grimes if (fp->f_count == 0) 1083df8bae1dSRodney W. Grimes continue; 1084ed5b7817SJulian Elischer /* 1085ed5b7817SJulian Elischer * If we already marked it as 'defer' in a 1086ed5b7817SJulian Elischer * previous pass, then try process it this time 1087ed5b7817SJulian Elischer * and un-mark it 1088ed5b7817SJulian Elischer */ 1089df8bae1dSRodney W. Grimes if (fp->f_flag & FDEFER) { 1090df8bae1dSRodney W. Grimes fp->f_flag &= ~FDEFER; 1091df8bae1dSRodney W. Grimes unp_defer--; 1092df8bae1dSRodney W. Grimes } else { 1093ed5b7817SJulian Elischer /* 1094ed5b7817SJulian Elischer * if it's not defered, then check if it's 1095ed5b7817SJulian Elischer * already marked.. if so skip it 1096ed5b7817SJulian Elischer */ 1097df8bae1dSRodney W. Grimes if (fp->f_flag & FMARK) 1098df8bae1dSRodney W. Grimes continue; 1099ed5b7817SJulian Elischer /* 1100ed5b7817SJulian Elischer * If all references are from messages 1101ed5b7817SJulian Elischer * in transit, then skip it. it's not 1102ed5b7817SJulian Elischer * externally accessible. 1103ed5b7817SJulian Elischer */ 1104df8bae1dSRodney W. Grimes if (fp->f_count == fp->f_msgcount) 1105df8bae1dSRodney W. Grimes continue; 1106ed5b7817SJulian Elischer /* 1107ed5b7817SJulian Elischer * If it got this far then it must be 1108ed5b7817SJulian Elischer * externally accessible. 1109ed5b7817SJulian Elischer */ 1110df8bae1dSRodney W. Grimes fp->f_flag |= FMARK; 1111df8bae1dSRodney W. Grimes } 1112ed5b7817SJulian Elischer /* 1113ed5b7817SJulian Elischer * either it was defered, or it is externally 1114ed5b7817SJulian Elischer * accessible and not already marked so. 1115ed5b7817SJulian Elischer * Now check if it is possibly one of OUR sockets. 1116ed5b7817SJulian Elischer */ 1117df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_SOCKET || 1118df8bae1dSRodney W. Grimes (so = (struct socket *)fp->f_data) == 0) 1119df8bae1dSRodney W. Grimes continue; 1120748e0b0aSGarrett Wollman if (so->so_proto->pr_domain != &localdomain || 1121df8bae1dSRodney W. Grimes (so->so_proto->pr_flags&PR_RIGHTS) == 0) 1122df8bae1dSRodney W. Grimes continue; 1123df8bae1dSRodney W. Grimes #ifdef notdef 1124df8bae1dSRodney W. Grimes if (so->so_rcv.sb_flags & SB_LOCK) { 1125df8bae1dSRodney W. Grimes /* 1126df8bae1dSRodney W. Grimes * This is problematical; it's not clear 1127df8bae1dSRodney W. Grimes * we need to wait for the sockbuf to be 1128df8bae1dSRodney W. Grimes * unlocked (on a uniprocessor, at least), 1129df8bae1dSRodney W. Grimes * and it's also not clear what to do 1130df8bae1dSRodney W. Grimes * if sbwait returns an error due to receipt 1131df8bae1dSRodney W. Grimes * of a signal. If sbwait does return 1132df8bae1dSRodney W. Grimes * an error, we'll go into an infinite 1133df8bae1dSRodney W. Grimes * loop. Delete all of this for now. 1134df8bae1dSRodney W. Grimes */ 1135df8bae1dSRodney W. Grimes (void) sbwait(&so->so_rcv); 1136df8bae1dSRodney W. Grimes goto restart; 1137df8bae1dSRodney W. Grimes } 1138df8bae1dSRodney W. Grimes #endif 1139ed5b7817SJulian Elischer /* 1140ed5b7817SJulian Elischer * So, Ok, it's one of our sockets and it IS externally 1141ed5b7817SJulian Elischer * accessible (or was defered). Now we look 1142dc733423SDag-Erling Smørgrav * to see if we hold any file descriptors in its 1143ed5b7817SJulian Elischer * message buffers. Follow those links and mark them 1144ed5b7817SJulian Elischer * as accessible too. 1145ed5b7817SJulian Elischer */ 1146df8bae1dSRodney W. Grimes unp_scan(so->so_rcv.sb_mb, unp_mark); 1147df8bae1dSRodney W. Grimes } 1148df8bae1dSRodney W. Grimes } while (unp_defer); 1149df8bae1dSRodney W. Grimes /* 1150df8bae1dSRodney W. Grimes * We grab an extra reference to each of the file table entries 1151df8bae1dSRodney W. Grimes * that are not otherwise accessible and then free the rights 1152df8bae1dSRodney W. Grimes * that are stored in messages on them. 1153df8bae1dSRodney W. Grimes * 1154df8bae1dSRodney W. Grimes * The bug in the orginal code is a little tricky, so I'll describe 1155df8bae1dSRodney W. Grimes * what's wrong with it here. 1156df8bae1dSRodney W. Grimes * 1157df8bae1dSRodney W. Grimes * It is incorrect to simply unp_discard each entry for f_msgcount 1158df8bae1dSRodney W. Grimes * times -- consider the case of sockets A and B that contain 1159df8bae1dSRodney W. Grimes * references to each other. On a last close of some other socket, 1160df8bae1dSRodney W. Grimes * we trigger a gc since the number of outstanding rights (unp_rights) 1161df8bae1dSRodney W. Grimes * is non-zero. If during the sweep phase the gc code un_discards, 1162df8bae1dSRodney W. Grimes * we end up doing a (full) closef on the descriptor. A closef on A 1163df8bae1dSRodney W. Grimes * results in the following chain. Closef calls soo_close, which 1164df8bae1dSRodney W. Grimes * calls soclose. Soclose calls first (through the switch 1165df8bae1dSRodney W. Grimes * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 1166df8bae1dSRodney W. Grimes * returns because the previous instance had set unp_gcing, and 1167df8bae1dSRodney W. Grimes * we return all the way back to soclose, which marks the socket 1168df8bae1dSRodney W. Grimes * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush 1169df8bae1dSRodney W. Grimes * to free up the rights that are queued in messages on the socket A, 1170df8bae1dSRodney W. Grimes * i.e., the reference on B. The sorflush calls via the dom_dispose 1171df8bae1dSRodney W. Grimes * switch unp_dispose, which unp_scans with unp_discard. This second 1172df8bae1dSRodney W. Grimes * instance of unp_discard just calls closef on B. 1173df8bae1dSRodney W. Grimes * 1174df8bae1dSRodney W. Grimes * Well, a similar chain occurs on B, resulting in a sorflush on B, 1175df8bae1dSRodney W. Grimes * which results in another closef on A. Unfortunately, A is already 1176df8bae1dSRodney W. Grimes * being closed, and the descriptor has already been marked with 1177df8bae1dSRodney W. Grimes * SS_NOFDREF, and soclose panics at this point. 1178df8bae1dSRodney W. Grimes * 1179df8bae1dSRodney W. Grimes * Here, we first take an extra reference to each inaccessible 1180df8bae1dSRodney W. Grimes * descriptor. Then, we call sorflush ourself, since we know 1181df8bae1dSRodney W. Grimes * it is a Unix domain socket anyhow. After we destroy all the 1182df8bae1dSRodney W. Grimes * rights carried in messages, we do a last closef to get rid 1183df8bae1dSRodney W. Grimes * of our extra reference. This is the last close, and the 1184df8bae1dSRodney W. Grimes * unp_detach etc will shut down the socket. 1185df8bae1dSRodney W. Grimes * 1186df8bae1dSRodney W. Grimes * 91/09/19, bsy@cs.cmu.edu 1187df8bae1dSRodney W. Grimes */ 1188df8bae1dSRodney W. Grimes extra_ref = malloc(nfiles * sizeof(struct file *), M_FILE, M_WAITOK); 11892e3c8fcbSPoul-Henning Kamp for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; fp != 0; 1190bc6f0e79SJeffrey Hsu fp = nextfp) { 11912e3c8fcbSPoul-Henning Kamp nextfp = LIST_NEXT(fp, f_list); 1192ed5b7817SJulian Elischer /* 1193ed5b7817SJulian Elischer * If it's not open, skip it 1194ed5b7817SJulian Elischer */ 1195df8bae1dSRodney W. Grimes if (fp->f_count == 0) 1196df8bae1dSRodney W. Grimes continue; 1197ed5b7817SJulian Elischer /* 1198ed5b7817SJulian Elischer * If all refs are from msgs, and it's not marked accessible 1199ed5b7817SJulian Elischer * then it must be referenced from some unreachable cycle 1200ed5b7817SJulian Elischer * of (shut-down) FDs, so include it in our 1201ed5b7817SJulian Elischer * list of FDs to remove 1202ed5b7817SJulian Elischer */ 1203df8bae1dSRodney W. Grimes if (fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) { 1204df8bae1dSRodney W. Grimes *fpp++ = fp; 1205df8bae1dSRodney W. Grimes nunref++; 1206df8bae1dSRodney W. Grimes fp->f_count++; 1207df8bae1dSRodney W. Grimes } 1208df8bae1dSRodney W. Grimes } 1209ed5b7817SJulian Elischer /* 1210ed5b7817SJulian Elischer * for each FD on our hit list, do the following two things 1211ed5b7817SJulian Elischer */ 12121c7c3c6aSMatthew Dillon for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 12131c7c3c6aSMatthew Dillon struct file *tfp = *fpp; 12141c7c3c6aSMatthew Dillon if (tfp->f_type == DTYPE_SOCKET && tfp->f_data != NULL) 12151c7c3c6aSMatthew Dillon sorflush((struct socket *)(tfp->f_data)); 12161c7c3c6aSMatthew Dillon } 1217df8bae1dSRodney W. Grimes for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) 121892cbac68SPoul-Henning Kamp closef(*fpp, (struct proc *) NULL); 1219df8bae1dSRodney W. Grimes free((caddr_t)extra_ref, M_FILE); 1220df8bae1dSRodney W. Grimes unp_gcing = 0; 1221df8bae1dSRodney W. Grimes } 1222df8bae1dSRodney W. Grimes 122326f9a767SRodney W. Grimes void 1224df8bae1dSRodney W. Grimes unp_dispose(m) 1225df8bae1dSRodney W. Grimes struct mbuf *m; 1226df8bae1dSRodney W. Grimes { 1227996c772fSJohn Dyson 1228df8bae1dSRodney W. Grimes if (m) 1229df8bae1dSRodney W. Grimes unp_scan(m, unp_discard); 1230df8bae1dSRodney W. Grimes } 1231df8bae1dSRodney W. Grimes 1232f708ef1bSPoul-Henning Kamp static void 1233df8bae1dSRodney W. Grimes unp_scan(m0, op) 1234df8bae1dSRodney W. Grimes register struct mbuf *m0; 1235996c772fSJohn Dyson void (*op) __P((struct file *)); 1236df8bae1dSRodney W. Grimes { 1237df8bae1dSRodney W. Grimes register struct mbuf *m; 1238df8bae1dSRodney W. Grimes register struct file **rp; 1239df8bae1dSRodney W. Grimes register struct cmsghdr *cm; 1240df8bae1dSRodney W. Grimes register int i; 1241df8bae1dSRodney W. Grimes int qfds; 1242df8bae1dSRodney W. Grimes 1243df8bae1dSRodney W. Grimes while (m0) { 1244df8bae1dSRodney W. Grimes for (m = m0; m; m = m->m_next) 1245df8bae1dSRodney W. Grimes if (m->m_type == MT_CONTROL && 1246df8bae1dSRodney W. Grimes m->m_len >= sizeof(*cm)) { 1247df8bae1dSRodney W. Grimes cm = mtod(m, struct cmsghdr *); 1248df8bae1dSRodney W. Grimes if (cm->cmsg_level != SOL_SOCKET || 1249df8bae1dSRodney W. Grimes cm->cmsg_type != SCM_RIGHTS) 1250df8bae1dSRodney W. Grimes continue; 12518692c025SYoshinobu Inoue qfds = (cm->cmsg_len - 12528692c025SYoshinobu Inoue (CMSG_DATA(cm) - (u_char *)cm)) 1253df8bae1dSRodney W. Grimes / sizeof (struct file *); 12548692c025SYoshinobu Inoue rp = (struct file **)CMSG_DATA(cm); 1255df8bae1dSRodney W. Grimes for (i = 0; i < qfds; i++) 1256df8bae1dSRodney W. Grimes (*op)(*rp++); 1257df8bae1dSRodney W. Grimes break; /* XXX, but saves time */ 1258df8bae1dSRodney W. Grimes } 1259df8bae1dSRodney W. Grimes m0 = m0->m_act; 1260df8bae1dSRodney W. Grimes } 1261df8bae1dSRodney W. Grimes } 1262df8bae1dSRodney W. Grimes 1263f708ef1bSPoul-Henning Kamp static void 1264df8bae1dSRodney W. Grimes unp_mark(fp) 1265df8bae1dSRodney W. Grimes struct file *fp; 1266df8bae1dSRodney W. Grimes { 1267df8bae1dSRodney W. Grimes 1268df8bae1dSRodney W. Grimes if (fp->f_flag & FMARK) 1269df8bae1dSRodney W. Grimes return; 1270df8bae1dSRodney W. Grimes unp_defer++; 1271df8bae1dSRodney W. Grimes fp->f_flag |= (FMARK|FDEFER); 1272df8bae1dSRodney W. Grimes } 1273df8bae1dSRodney W. Grimes 1274f708ef1bSPoul-Henning Kamp static void 1275df8bae1dSRodney W. Grimes unp_discard(fp) 1276df8bae1dSRodney W. Grimes struct file *fp; 1277df8bae1dSRodney W. Grimes { 1278df8bae1dSRodney W. Grimes 1279df8bae1dSRodney W. Grimes fp->f_msgcount--; 1280df8bae1dSRodney W. Grimes unp_rights--; 1281df8bae1dSRodney W. Grimes (void) closef(fp, (struct proc *)NULL); 1282df8bae1dSRodney W. Grimes } 1283