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> 51df8bae1dSRodney W. Grimes #include <sys/stat.h> 52639acc13SGarrett Wollman #include <sys/sysctl.h> 53639acc13SGarrett Wollman #include <sys/un.h> 5498271db4SGarrett Wollman #include <sys/unpcb.h> 55639acc13SGarrett Wollman #include <sys/vnode.h> 56df8bae1dSRodney W. Grimes 5798271db4SGarrett Wollman #include <vm/vm_zone.h> 5898271db4SGarrett Wollman 59632a035fSEivind Eklund static struct vm_zone *unp_zone; 6098271db4SGarrett Wollman static unp_gen_t unp_gencnt; 6198271db4SGarrett Wollman static u_int unp_count; 6298271db4SGarrett Wollman 6398271db4SGarrett Wollman static struct unp_head unp_shead, unp_dhead; 6498271db4SGarrett Wollman 65df8bae1dSRodney W. Grimes /* 66df8bae1dSRodney W. Grimes * Unix communications domain. 67df8bae1dSRodney W. Grimes * 68df8bae1dSRodney W. Grimes * TODO: 69df8bae1dSRodney W. Grimes * SEQPACKET, RDM 70df8bae1dSRodney W. Grimes * rethink name space problems 71df8bae1dSRodney W. Grimes * need a proper out-of-band 7298271db4SGarrett Wollman * lock pushdown 73df8bae1dSRodney W. Grimes */ 74f708ef1bSPoul-Henning Kamp static struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 75f708ef1bSPoul-Henning Kamp static ino_t unp_ino; /* prototype for fake inode numbers */ 76f708ef1bSPoul-Henning Kamp 77f708ef1bSPoul-Henning Kamp static int unp_attach __P((struct socket *)); 78f708ef1bSPoul-Henning Kamp static void unp_detach __P((struct unpcb *)); 7957bf258eSGarrett Wollman static int unp_bind __P((struct unpcb *,struct sockaddr *, struct proc *)); 8057bf258eSGarrett Wollman static int unp_connect __P((struct socket *,struct sockaddr *, 8157bf258eSGarrett Wollman struct proc *)); 82f708ef1bSPoul-Henning Kamp static void unp_disconnect __P((struct unpcb *)); 83f708ef1bSPoul-Henning Kamp static void unp_shutdown __P((struct unpcb *)); 84f708ef1bSPoul-Henning Kamp static void unp_drop __P((struct unpcb *, int)); 85f708ef1bSPoul-Henning Kamp static void unp_gc __P((void)); 86f708ef1bSPoul-Henning Kamp static void unp_scan __P((struct mbuf *, void (*)(struct file *))); 87f708ef1bSPoul-Henning Kamp static void unp_mark __P((struct file *)); 88f708ef1bSPoul-Henning Kamp static void unp_discard __P((struct file *)); 89f708ef1bSPoul-Henning Kamp static int unp_internalize __P((struct mbuf *, struct proc *)); 90f708ef1bSPoul-Henning Kamp 91a29f300eSGarrett Wollman static int 92a29f300eSGarrett Wollman uipc_abort(struct socket *so) 93df8bae1dSRodney W. Grimes { 94df8bae1dSRodney W. Grimes struct unpcb *unp = sotounpcb(so); 95df8bae1dSRodney W. Grimes 96a29f300eSGarrett Wollman if (unp == 0) 97a29f300eSGarrett Wollman return EINVAL; 98a29f300eSGarrett Wollman unp_drop(unp, ECONNABORTED); 99a29f300eSGarrett Wollman return 0; 100df8bae1dSRodney W. Grimes } 101df8bae1dSRodney W. Grimes 102a29f300eSGarrett Wollman static int 10357bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam) 104a29f300eSGarrett Wollman { 105a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 106df8bae1dSRodney W. Grimes 107a29f300eSGarrett Wollman if (unp == 0) 108a29f300eSGarrett Wollman return EINVAL; 109df8bae1dSRodney W. Grimes 110df8bae1dSRodney W. Grimes /* 111df8bae1dSRodney W. Grimes * Pass back name of connected socket, 112df8bae1dSRodney W. Grimes * if it was bound and we are still connected 113df8bae1dSRodney W. Grimes * (our peer may have closed already!). 114df8bae1dSRodney W. Grimes */ 115df8bae1dSRodney W. Grimes if (unp->unp_conn && unp->unp_conn->unp_addr) { 11657bf258eSGarrett Wollman *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr, 11757bf258eSGarrett Wollman 1); 118df8bae1dSRodney W. Grimes } else { 11957bf258eSGarrett Wollman *nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1); 120df8bae1dSRodney W. Grimes } 121a29f300eSGarrett Wollman return 0; 122a29f300eSGarrett Wollman } 123df8bae1dSRodney W. Grimes 124a29f300eSGarrett Wollman static int 125a29f300eSGarrett Wollman uipc_attach(struct socket *so, int proto, struct proc *p) 126a29f300eSGarrett Wollman { 127a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 128df8bae1dSRodney W. Grimes 129a29f300eSGarrett Wollman if (unp != 0) 130a29f300eSGarrett Wollman return EISCONN; 131a29f300eSGarrett Wollman return unp_attach(so); 132a29f300eSGarrett Wollman } 133a29f300eSGarrett Wollman 134a29f300eSGarrett Wollman static int 13557bf258eSGarrett Wollman uipc_bind(struct socket *so, struct sockaddr *nam, struct proc *p) 136a29f300eSGarrett Wollman { 137a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 138a29f300eSGarrett Wollman 139a29f300eSGarrett Wollman if (unp == 0) 140a29f300eSGarrett Wollman return EINVAL; 141a29f300eSGarrett Wollman 142a29f300eSGarrett Wollman return unp_bind(unp, nam, p); 143a29f300eSGarrett Wollman } 144a29f300eSGarrett Wollman 145a29f300eSGarrett Wollman static int 14657bf258eSGarrett Wollman uipc_connect(struct socket *so, struct sockaddr *nam, struct proc *p) 147a29f300eSGarrett Wollman { 148a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 149a29f300eSGarrett Wollman 150a29f300eSGarrett Wollman if (unp == 0) 151a29f300eSGarrett Wollman return EINVAL; 152a29f300eSGarrett Wollman return unp_connect(so, nam, curproc); 153a29f300eSGarrett Wollman } 154a29f300eSGarrett Wollman 155a29f300eSGarrett Wollman static int 156a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2) 157a29f300eSGarrett Wollman { 158a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so1); 159a29f300eSGarrett Wollman 160a29f300eSGarrett Wollman if (unp == 0) 161a29f300eSGarrett Wollman return EINVAL; 162a29f300eSGarrett Wollman 163a29f300eSGarrett Wollman return unp_connect2(so1, so2); 164a29f300eSGarrett Wollman } 165a29f300eSGarrett Wollman 166a29f300eSGarrett Wollman /* control is EOPNOTSUPP */ 167a29f300eSGarrett Wollman 168a29f300eSGarrett Wollman static int 169a29f300eSGarrett Wollman uipc_detach(struct socket *so) 170a29f300eSGarrett Wollman { 171a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 172a29f300eSGarrett Wollman 173a29f300eSGarrett Wollman if (unp == 0) 174a29f300eSGarrett Wollman return EINVAL; 175a29f300eSGarrett Wollman 176a29f300eSGarrett Wollman unp_detach(unp); 177a29f300eSGarrett Wollman return 0; 178a29f300eSGarrett Wollman } 179a29f300eSGarrett Wollman 180a29f300eSGarrett Wollman static int 181a29f300eSGarrett Wollman uipc_disconnect(struct socket *so) 182a29f300eSGarrett Wollman { 183a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 184a29f300eSGarrett Wollman 185a29f300eSGarrett Wollman if (unp == 0) 186a29f300eSGarrett Wollman return EINVAL; 187a29f300eSGarrett Wollman unp_disconnect(unp); 188a29f300eSGarrett Wollman return 0; 189a29f300eSGarrett Wollman } 190a29f300eSGarrett Wollman 191a29f300eSGarrett Wollman static int 192a29f300eSGarrett Wollman uipc_listen(struct socket *so, struct proc *p) 193a29f300eSGarrett Wollman { 194a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 195a29f300eSGarrett Wollman 196a29f300eSGarrett Wollman if (unp == 0 || unp->unp_vnode == 0) 197a29f300eSGarrett Wollman return EINVAL; 198a29f300eSGarrett Wollman return 0; 199a29f300eSGarrett Wollman } 200a29f300eSGarrett Wollman 201a29f300eSGarrett Wollman static int 20257bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 203a29f300eSGarrett Wollman { 204a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 205a29f300eSGarrett Wollman 206a29f300eSGarrett Wollman if (unp == 0) 207a29f300eSGarrett Wollman return EINVAL; 20857bf258eSGarrett Wollman if (unp->unp_conn && unp->unp_conn->unp_addr) 20957bf258eSGarrett Wollman *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr, 21057bf258eSGarrett Wollman 1); 211a29f300eSGarrett Wollman return 0; 212a29f300eSGarrett Wollman } 213a29f300eSGarrett Wollman 214a29f300eSGarrett Wollman static int 215a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 216a29f300eSGarrett Wollman { 217a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 218a29f300eSGarrett Wollman struct socket *so2; 219a29f300eSGarrett Wollman 220a29f300eSGarrett Wollman if (unp == 0) 221a29f300eSGarrett Wollman return EINVAL; 222df8bae1dSRodney W. Grimes switch (so->so_type) { 223df8bae1dSRodney W. Grimes case SOCK_DGRAM: 224a29f300eSGarrett Wollman panic("uipc_rcvd DGRAM?"); 225df8bae1dSRodney W. Grimes /*NOTREACHED*/ 226df8bae1dSRodney W. Grimes 227df8bae1dSRodney W. Grimes case SOCK_STREAM: 228df8bae1dSRodney W. Grimes if (unp->unp_conn == 0) 229df8bae1dSRodney W. Grimes break; 230df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 231df8bae1dSRodney W. Grimes /* 232df8bae1dSRodney W. Grimes * Adjust backpressure on sender 233df8bae1dSRodney W. Grimes * and wakeup any waiting to write. 234df8bae1dSRodney W. Grimes */ 235ff8b0106SBrian Feldman so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt; 236ff8b0106SBrian Feldman unp->unp_mbcnt = so->so_rcv.sb_mbcnt; 237ff8b0106SBrian Feldman so2->so_snd.sb_hiwat += unp->unp_cc - so->so_rcv.sb_cc; 238ecf72308SBrian Feldman (void)chgsbsize(so2->so_cred->cr_uid, 239ecf72308SBrian Feldman (rlim_t)unp->unp_cc - so->so_rcv.sb_cc); 240ff8b0106SBrian Feldman unp->unp_cc = so->so_rcv.sb_cc; 241df8bae1dSRodney W. Grimes sowwakeup(so2); 242df8bae1dSRodney W. Grimes break; 243df8bae1dSRodney W. Grimes 244df8bae1dSRodney W. Grimes default: 245a29f300eSGarrett Wollman panic("uipc_rcvd unknown socktype"); 246df8bae1dSRodney W. Grimes } 247a29f300eSGarrett Wollman return 0; 248a29f300eSGarrett Wollman } 249df8bae1dSRodney W. Grimes 250a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 251a29f300eSGarrett Wollman 252a29f300eSGarrett Wollman static int 25357bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 254a29f300eSGarrett Wollman struct mbuf *control, struct proc *p) 255a29f300eSGarrett Wollman { 256a29f300eSGarrett Wollman int error = 0; 257a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 258a29f300eSGarrett Wollman struct socket *so2; 259a29f300eSGarrett Wollman 260a29f300eSGarrett Wollman if (unp == 0) { 261a29f300eSGarrett Wollman error = EINVAL; 262a29f300eSGarrett Wollman goto release; 263a29f300eSGarrett Wollman } 264a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 265a29f300eSGarrett Wollman error = EOPNOTSUPP; 266a29f300eSGarrett Wollman goto release; 267a29f300eSGarrett Wollman } 268a29f300eSGarrett Wollman 269df8bae1dSRodney W. Grimes if (control && (error = unp_internalize(control, p))) 270a29f300eSGarrett Wollman goto release; 271df8bae1dSRodney W. Grimes 272a29f300eSGarrett Wollman switch (so->so_type) { 273a29f300eSGarrett Wollman case SOCK_DGRAM: 274a29f300eSGarrett Wollman { 275df8bae1dSRodney W. Grimes struct sockaddr *from; 276df8bae1dSRodney W. Grimes 277df8bae1dSRodney W. Grimes if (nam) { 278df8bae1dSRodney W. Grimes if (unp->unp_conn) { 279df8bae1dSRodney W. Grimes error = EISCONN; 280df8bae1dSRodney W. Grimes break; 281df8bae1dSRodney W. Grimes } 282df8bae1dSRodney W. Grimes error = unp_connect(so, nam, p); 283df8bae1dSRodney W. Grimes if (error) 284df8bae1dSRodney W. Grimes break; 285df8bae1dSRodney W. Grimes } else { 286df8bae1dSRodney W. Grimes if (unp->unp_conn == 0) { 287df8bae1dSRodney W. Grimes error = ENOTCONN; 288df8bae1dSRodney W. Grimes break; 289df8bae1dSRodney W. Grimes } 290df8bae1dSRodney W. Grimes } 291df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 292df8bae1dSRodney W. Grimes if (unp->unp_addr) 29357bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 294df8bae1dSRodney W. Grimes else 295df8bae1dSRodney W. Grimes from = &sun_noname; 296df8bae1dSRodney W. Grimes if (sbappendaddr(&so2->so_rcv, from, m, control)) { 297df8bae1dSRodney W. Grimes sorwakeup(so2); 298df8bae1dSRodney W. Grimes m = 0; 299df8bae1dSRodney W. Grimes control = 0; 300df8bae1dSRodney W. Grimes } else 301df8bae1dSRodney W. Grimes error = ENOBUFS; 302df8bae1dSRodney W. Grimes if (nam) 303df8bae1dSRodney W. Grimes unp_disconnect(unp); 304df8bae1dSRodney W. Grimes break; 305df8bae1dSRodney W. Grimes } 306df8bae1dSRodney W. Grimes 307df8bae1dSRodney W. Grimes case SOCK_STREAM: 3086b8fda4dSGarrett Wollman /* Connect if not connected yet. */ 3096b8fda4dSGarrett Wollman /* 3106b8fda4dSGarrett Wollman * Note: A better implementation would complain 311402cc72dSDavid Greenman * if not equal to the peer's address. 3126b8fda4dSGarrett Wollman */ 313402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 314402cc72dSDavid Greenman if (nam) { 315402cc72dSDavid Greenman error = unp_connect(so, nam, p); 316402cc72dSDavid Greenman if (error) 3176b8fda4dSGarrett Wollman break; /* XXX */ 318402cc72dSDavid Greenman } else { 319402cc72dSDavid Greenman error = ENOTCONN; 320402cc72dSDavid Greenman break; 321402cc72dSDavid Greenman } 322402cc72dSDavid Greenman } 323402cc72dSDavid Greenman 324df8bae1dSRodney W. Grimes if (so->so_state & SS_CANTSENDMORE) { 325df8bae1dSRodney W. Grimes error = EPIPE; 326df8bae1dSRodney W. Grimes break; 327df8bae1dSRodney W. Grimes } 328df8bae1dSRodney W. Grimes if (unp->unp_conn == 0) 329a29f300eSGarrett Wollman panic("uipc_send connected but no connection?"); 330df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 331df8bae1dSRodney W. Grimes /* 332df8bae1dSRodney W. Grimes * Send to paired receive port, and then reduce 333df8bae1dSRodney W. Grimes * send buffer hiwater marks to maintain backpressure. 334df8bae1dSRodney W. Grimes * Wake up readers. 335df8bae1dSRodney W. Grimes */ 336df8bae1dSRodney W. Grimes if (control) { 337ff8b0106SBrian Feldman if (sbappendcontrol(&so2->so_rcv, m, control)) 338df8bae1dSRodney W. Grimes control = 0; 339df8bae1dSRodney W. Grimes } else 340ff8b0106SBrian Feldman sbappend(&so2->so_rcv, m); 341ff8b0106SBrian Feldman so->so_snd.sb_mbmax -= 342ff8b0106SBrian Feldman so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt; 343ff8b0106SBrian Feldman unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt; 344ff8b0106SBrian Feldman so->so_snd.sb_hiwat -= 345ff8b0106SBrian Feldman so2->so_rcv.sb_cc - unp->unp_conn->unp_cc; 346ecf72308SBrian Feldman (void)chgsbsize(so->so_cred->cr_uid, 347ecf72308SBrian Feldman (rlim_t)unp->unp_conn->unp_cc - so2->so_rcv.sb_cc); 348ff8b0106SBrian Feldman unp->unp_conn->unp_cc = so2->so_rcv.sb_cc; 349df8bae1dSRodney W. Grimes sorwakeup(so2); 350df8bae1dSRodney W. Grimes m = 0; 351df8bae1dSRodney W. Grimes break; 352df8bae1dSRodney W. Grimes 353df8bae1dSRodney W. Grimes default: 354a29f300eSGarrett Wollman panic("uipc_send unknown socktype"); 355df8bae1dSRodney W. Grimes } 356a29f300eSGarrett Wollman 3576b8fda4dSGarrett Wollman /* 3586b8fda4dSGarrett Wollman * SEND_EOF is equivalent to a SEND followed by 3596b8fda4dSGarrett Wollman * a SHUTDOWN. 3606b8fda4dSGarrett Wollman */ 361a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 3626b8fda4dSGarrett Wollman socantsendmore(so); 3636b8fda4dSGarrett Wollman unp_shutdown(unp); 3646b8fda4dSGarrett Wollman } 365df8bae1dSRodney W. Grimes 366bd508d39SDon Lewis if (control && error != 0) 367bd508d39SDon Lewis unp_dispose(control); 368bd508d39SDon Lewis 369a29f300eSGarrett Wollman release: 370a29f300eSGarrett Wollman if (control) 371a29f300eSGarrett Wollman m_freem(control); 372a29f300eSGarrett Wollman if (m) 373a29f300eSGarrett Wollman m_freem(m); 374a29f300eSGarrett Wollman return error; 375a29f300eSGarrett Wollman } 376df8bae1dSRodney W. Grimes 377a29f300eSGarrett Wollman static int 378a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 379a29f300eSGarrett Wollman { 380a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 381a29f300eSGarrett Wollman struct socket *so2; 382a29f300eSGarrett Wollman 383a29f300eSGarrett Wollman if (unp == 0) 384a29f300eSGarrett Wollman return EINVAL; 385a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 386df8bae1dSRodney W. Grimes if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) { 387df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 388a29f300eSGarrett Wollman sb->st_blksize += so2->so_rcv.sb_cc; 389df8bae1dSRodney W. Grimes } 390bfbb9ce6SPoul-Henning Kamp sb->st_dev = NOUDEV; 391df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 392df8bae1dSRodney W. Grimes unp->unp_ino = unp_ino++; 393a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 394df8bae1dSRodney W. Grimes return (0); 395a29f300eSGarrett Wollman } 396df8bae1dSRodney W. Grimes 397a29f300eSGarrett Wollman static int 398a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 399a29f300eSGarrett Wollman { 400a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 401df8bae1dSRodney W. Grimes 402a29f300eSGarrett Wollman if (unp == 0) 403a29f300eSGarrett Wollman return EINVAL; 404a29f300eSGarrett Wollman socantsendmore(so); 405a29f300eSGarrett Wollman unp_shutdown(unp); 406a29f300eSGarrett Wollman return 0; 407a29f300eSGarrett Wollman } 408df8bae1dSRodney W. Grimes 409a29f300eSGarrett Wollman static int 41057bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 411a29f300eSGarrett Wollman { 412a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 413a29f300eSGarrett Wollman 414a29f300eSGarrett Wollman if (unp == 0) 415a29f300eSGarrett Wollman return EINVAL; 41657bf258eSGarrett Wollman if (unp->unp_addr) 41757bf258eSGarrett Wollman *nam = dup_sockaddr((struct sockaddr *)unp->unp_addr, 1); 418a29f300eSGarrett Wollman return 0; 419df8bae1dSRodney W. Grimes } 420a29f300eSGarrett Wollman 421a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = { 422a29f300eSGarrett Wollman uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect, 423a29f300eSGarrett Wollman uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect, 424a29f300eSGarrett Wollman uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp, 425a29f300eSGarrett Wollman uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr, 42651338ea8SPeter Wemm sosend, soreceive, sopoll 427a29f300eSGarrett Wollman }; 428df8bae1dSRodney W. Grimes 429df8bae1dSRodney W. Grimes /* 430df8bae1dSRodney W. Grimes * Both send and receive buffers are allocated PIPSIZ bytes of buffering 431df8bae1dSRodney W. Grimes * for stream sockets, although the total for sender and receiver is 432df8bae1dSRodney W. Grimes * actually only PIPSIZ. 433df8bae1dSRodney W. Grimes * Datagram sockets really use the sendspace as the maximum datagram size, 434df8bae1dSRodney W. Grimes * and don't really want to reserve the sendspace. Their recvspace should 435df8bae1dSRodney W. Grimes * be large enough for at least one max-size datagram plus address. 436df8bae1dSRodney W. Grimes */ 4375dce41c5SJohn Dyson #ifndef PIPSIZ 4385dce41c5SJohn Dyson #define PIPSIZ 8192 4395dce41c5SJohn Dyson #endif 440f708ef1bSPoul-Henning Kamp static u_long unpst_sendspace = PIPSIZ; 441f708ef1bSPoul-Henning Kamp static u_long unpst_recvspace = PIPSIZ; 442f708ef1bSPoul-Henning Kamp static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 443f708ef1bSPoul-Henning Kamp static u_long unpdg_recvspace = 4*1024; 444df8bae1dSRodney W. Grimes 445f708ef1bSPoul-Henning Kamp static int unp_rights; /* file descriptors in flight */ 446df8bae1dSRodney W. Grimes 447ce02431fSDoug Rabson SYSCTL_DECL(_net_local_stream); 448639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 449639acc13SGarrett Wollman &unpst_sendspace, 0, ""); 450639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 451639acc13SGarrett Wollman &unpst_recvspace, 0, ""); 452ce02431fSDoug Rabson SYSCTL_DECL(_net_local_dgram); 453639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 454639acc13SGarrett Wollman &unpdg_sendspace, 0, ""); 455639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 456639acc13SGarrett Wollman &unpdg_recvspace, 0, ""); 457ce02431fSDoug Rabson SYSCTL_DECL(_net_local); 458639acc13SGarrett Wollman SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); 459639acc13SGarrett Wollman 460f708ef1bSPoul-Henning Kamp static int 461df8bae1dSRodney W. Grimes unp_attach(so) 462df8bae1dSRodney W. Grimes struct socket *so; 463df8bae1dSRodney W. Grimes { 464df8bae1dSRodney W. Grimes register struct unpcb *unp; 465df8bae1dSRodney W. Grimes int error; 466df8bae1dSRodney W. Grimes 467df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 468df8bae1dSRodney W. Grimes switch (so->so_type) { 469df8bae1dSRodney W. Grimes 470df8bae1dSRodney W. Grimes case SOCK_STREAM: 471df8bae1dSRodney W. Grimes error = soreserve(so, unpst_sendspace, unpst_recvspace); 472df8bae1dSRodney W. Grimes break; 473df8bae1dSRodney W. Grimes 474df8bae1dSRodney W. Grimes case SOCK_DGRAM: 475df8bae1dSRodney W. Grimes error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 476df8bae1dSRodney W. Grimes break; 477df8bae1dSRodney W. Grimes 478df8bae1dSRodney W. Grimes default: 479df8bae1dSRodney W. Grimes panic("unp_attach"); 480df8bae1dSRodney W. Grimes } 481df8bae1dSRodney W. Grimes if (error) 482df8bae1dSRodney W. Grimes return (error); 483df8bae1dSRodney W. Grimes } 48498271db4SGarrett Wollman unp = zalloc(unp_zone); 48557bf258eSGarrett Wollman if (unp == NULL) 486df8bae1dSRodney W. Grimes return (ENOBUFS); 48757bf258eSGarrett Wollman bzero(unp, sizeof *unp); 48898271db4SGarrett Wollman unp->unp_gencnt = ++unp_gencnt; 48998271db4SGarrett Wollman unp_count++; 49098271db4SGarrett Wollman LIST_INIT(&unp->unp_refs); 491df8bae1dSRodney W. Grimes unp->unp_socket = so; 49275c13541SPoul-Henning Kamp unp->unp_rvnode = curproc->p_fd->fd_rdir; 49398271db4SGarrett Wollman LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead 49498271db4SGarrett Wollman : &unp_shead, unp, unp_link); 49598271db4SGarrett Wollman so->so_pcb = (caddr_t)unp; 496df8bae1dSRodney W. Grimes return (0); 497df8bae1dSRodney W. Grimes } 498df8bae1dSRodney W. Grimes 499f708ef1bSPoul-Henning Kamp static void 500df8bae1dSRodney W. Grimes unp_detach(unp) 501df8bae1dSRodney W. Grimes register struct unpcb *unp; 502df8bae1dSRodney W. Grimes { 50398271db4SGarrett Wollman LIST_REMOVE(unp, unp_link); 50498271db4SGarrett Wollman unp->unp_gencnt = ++unp_gencnt; 50598271db4SGarrett Wollman --unp_count; 506df8bae1dSRodney W. Grimes if (unp->unp_vnode) { 507df8bae1dSRodney W. Grimes unp->unp_vnode->v_socket = 0; 508df8bae1dSRodney W. Grimes vrele(unp->unp_vnode); 509df8bae1dSRodney W. Grimes unp->unp_vnode = 0; 510df8bae1dSRodney W. Grimes } 511df8bae1dSRodney W. Grimes if (unp->unp_conn) 512df8bae1dSRodney W. Grimes unp_disconnect(unp); 5132e3c8fcbSPoul-Henning Kamp while (!LIST_EMPTY(&unp->unp_refs)) 5142e3c8fcbSPoul-Henning Kamp unp_drop(LIST_FIRST(&unp->unp_refs), ECONNRESET); 515df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 516df8bae1dSRodney W. Grimes unp->unp_socket->so_pcb = 0; 517df8bae1dSRodney W. Grimes if (unp_rights) { 518df8bae1dSRodney W. Grimes /* 519df8bae1dSRodney W. Grimes * Normally the receive buffer is flushed later, 520df8bae1dSRodney W. Grimes * in sofree, but if our receive buffer holds references 521df8bae1dSRodney W. Grimes * to descriptors that are now garbage, we will dispose 522df8bae1dSRodney W. Grimes * of those descriptor references after the garbage collector 523df8bae1dSRodney W. Grimes * gets them (resulting in a "panic: closef: count < 0"). 524df8bae1dSRodney W. Grimes */ 525df8bae1dSRodney W. Grimes sorflush(unp->unp_socket); 526df8bae1dSRodney W. Grimes unp_gc(); 527df8bae1dSRodney W. Grimes } 52857bf258eSGarrett Wollman if (unp->unp_addr) 52957bf258eSGarrett Wollman FREE(unp->unp_addr, M_SONAME); 53098271db4SGarrett Wollman zfree(unp_zone, unp); 531df8bae1dSRodney W. Grimes } 532df8bae1dSRodney W. Grimes 533f708ef1bSPoul-Henning Kamp static int 534df8bae1dSRodney W. Grimes unp_bind(unp, nam, p) 535df8bae1dSRodney W. Grimes struct unpcb *unp; 53657bf258eSGarrett Wollman struct sockaddr *nam; 537df8bae1dSRodney W. Grimes struct proc *p; 538df8bae1dSRodney W. Grimes { 53957bf258eSGarrett Wollman struct sockaddr_un *soun = (struct sockaddr_un *)nam; 540df8bae1dSRodney W. Grimes register struct vnode *vp; 541df8bae1dSRodney W. Grimes struct vattr vattr; 54257bf258eSGarrett Wollman int error, namelen; 543df8bae1dSRodney W. Grimes struct nameidata nd; 54457bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 545df8bae1dSRodney W. Grimes 546df8bae1dSRodney W. Grimes if (unp->unp_vnode != NULL) 547df8bae1dSRodney W. Grimes return (EINVAL); 54857bf258eSGarrett Wollman #define offsetof(s, e) ((char *)&((s *)0)->e - (char *)((s *)0)) 54957bf258eSGarrett Wollman namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 55057bf258eSGarrett Wollman if (namelen <= 0) 55157bf258eSGarrett Wollman return EINVAL; 55257bf258eSGarrett Wollman strncpy(buf, soun->sun_path, namelen); 55357bf258eSGarrett Wollman buf[namelen] = 0; /* null-terminate the string */ 554974784e8SGuido van Rooij NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT, UIO_SYSSPACE, 55557bf258eSGarrett Wollman buf, p); 556df8bae1dSRodney W. Grimes /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 557797f2d22SPoul-Henning Kamp error = namei(&nd); 558797f2d22SPoul-Henning Kamp if (error) 559df8bae1dSRodney W. Grimes return (error); 560df8bae1dSRodney W. Grimes vp = nd.ni_vp; 561df8bae1dSRodney W. Grimes if (vp != NULL) { 562df8bae1dSRodney W. Grimes VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 563df8bae1dSRodney W. Grimes if (nd.ni_dvp == vp) 564df8bae1dSRodney W. Grimes vrele(nd.ni_dvp); 565df8bae1dSRodney W. Grimes else 566df8bae1dSRodney W. Grimes vput(nd.ni_dvp); 567df8bae1dSRodney W. Grimes vrele(vp); 568df8bae1dSRodney W. Grimes return (EADDRINUSE); 569df8bae1dSRodney W. Grimes } 570df8bae1dSRodney W. Grimes VATTR_NULL(&vattr); 571df8bae1dSRodney W. Grimes vattr.va_type = VSOCK; 572a29f300eSGarrett Wollman vattr.va_mode = (ACCESSPERMS & ~p->p_fd->fd_cmask); 573996c772fSJohn Dyson VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); 5747be2d300SMike Smith error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 5757be2d300SMike Smith vput(nd.ni_dvp); 5767be2d300SMike Smith if (error) 577df8bae1dSRodney W. Grimes return (error); 578df8bae1dSRodney W. Grimes vp = nd.ni_vp; 579df8bae1dSRodney W. Grimes vp->v_socket = unp->unp_socket; 580df8bae1dSRodney W. Grimes unp->unp_vnode = vp; 58157bf258eSGarrett Wollman unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam, 1); 582996c772fSJohn Dyson VOP_UNLOCK(vp, 0, p); 583df8bae1dSRodney W. Grimes return (0); 584df8bae1dSRodney W. Grimes } 585df8bae1dSRodney W. Grimes 586f708ef1bSPoul-Henning Kamp static int 587df8bae1dSRodney W. Grimes unp_connect(so, nam, p) 588df8bae1dSRodney W. Grimes struct socket *so; 58957bf258eSGarrett Wollman struct sockaddr *nam; 590df8bae1dSRodney W. Grimes struct proc *p; 591df8bae1dSRodney W. Grimes { 59257bf258eSGarrett Wollman register struct sockaddr_un *soun = (struct sockaddr_un *)nam; 593df8bae1dSRodney W. Grimes register struct vnode *vp; 594df8bae1dSRodney W. Grimes register struct socket *so2, *so3; 595df8bae1dSRodney W. Grimes struct unpcb *unp2, *unp3; 59657bf258eSGarrett Wollman int error, len; 597df8bae1dSRodney W. Grimes struct nameidata nd; 59857bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 599df8bae1dSRodney W. Grimes 60057bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 60157bf258eSGarrett Wollman if (len <= 0) 60257bf258eSGarrett Wollman return EINVAL; 60357bf258eSGarrett Wollman strncpy(buf, soun->sun_path, len); 60457bf258eSGarrett Wollman buf[len] = 0; 60557bf258eSGarrett Wollman 60657bf258eSGarrett Wollman NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, p); 607797f2d22SPoul-Henning Kamp error = namei(&nd); 608797f2d22SPoul-Henning Kamp if (error) 609df8bae1dSRodney W. Grimes return (error); 610df8bae1dSRodney W. Grimes vp = nd.ni_vp; 611df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 612df8bae1dSRodney W. Grimes error = ENOTSOCK; 613df8bae1dSRodney W. Grimes goto bad; 614df8bae1dSRodney W. Grimes } 615797f2d22SPoul-Henning Kamp error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p); 616797f2d22SPoul-Henning Kamp if (error) 617df8bae1dSRodney W. Grimes goto bad; 618df8bae1dSRodney W. Grimes so2 = vp->v_socket; 619df8bae1dSRodney W. Grimes if (so2 == 0) { 620df8bae1dSRodney W. Grimes error = ECONNREFUSED; 621df8bae1dSRodney W. Grimes goto bad; 622df8bae1dSRodney W. Grimes } 623df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 624df8bae1dSRodney W. Grimes error = EPROTOTYPE; 625df8bae1dSRodney W. Grimes goto bad; 626df8bae1dSRodney W. Grimes } 627df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 628df8bae1dSRodney W. Grimes if ((so2->so_options & SO_ACCEPTCONN) == 0 || 6292f9a2132SBrian Feldman (so3 = sonewconn3(so2, 0, p)) == 0) { 630df8bae1dSRodney W. Grimes error = ECONNREFUSED; 631df8bae1dSRodney W. Grimes goto bad; 632df8bae1dSRodney W. Grimes } 633df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 634df8bae1dSRodney W. Grimes unp3 = sotounpcb(so3); 635df8bae1dSRodney W. Grimes if (unp2->unp_addr) 63657bf258eSGarrett Wollman unp3->unp_addr = (struct sockaddr_un *) 63757bf258eSGarrett Wollman dup_sockaddr((struct sockaddr *) 63857bf258eSGarrett Wollman unp2->unp_addr, 1); 639df8bae1dSRodney W. Grimes so2 = so3; 640df8bae1dSRodney W. Grimes } 641df8bae1dSRodney W. Grimes error = unp_connect2(so, so2); 642df8bae1dSRodney W. Grimes bad: 643df8bae1dSRodney W. Grimes vput(vp); 644df8bae1dSRodney W. Grimes return (error); 645df8bae1dSRodney W. Grimes } 646df8bae1dSRodney W. Grimes 64726f9a767SRodney W. Grimes int 648df8bae1dSRodney W. Grimes unp_connect2(so, so2) 649df8bae1dSRodney W. Grimes register struct socket *so; 650df8bae1dSRodney W. Grimes register struct socket *so2; 651df8bae1dSRodney W. Grimes { 652df8bae1dSRodney W. Grimes register struct unpcb *unp = sotounpcb(so); 653df8bae1dSRodney W. Grimes register struct unpcb *unp2; 654df8bae1dSRodney W. Grimes 655df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 656df8bae1dSRodney W. Grimes return (EPROTOTYPE); 657df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 658df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 659df8bae1dSRodney W. Grimes switch (so->so_type) { 660df8bae1dSRodney W. Grimes 661df8bae1dSRodney W. Grimes case SOCK_DGRAM: 66298271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 663df8bae1dSRodney W. Grimes soisconnected(so); 664df8bae1dSRodney W. Grimes break; 665df8bae1dSRodney W. Grimes 666df8bae1dSRodney W. Grimes case SOCK_STREAM: 667df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 668df8bae1dSRodney W. Grimes soisconnected(so); 669df8bae1dSRodney W. Grimes soisconnected(so2); 670df8bae1dSRodney W. Grimes break; 671df8bae1dSRodney W. Grimes 672df8bae1dSRodney W. Grimes default: 673df8bae1dSRodney W. Grimes panic("unp_connect2"); 674df8bae1dSRodney W. Grimes } 675df8bae1dSRodney W. Grimes return (0); 676df8bae1dSRodney W. Grimes } 677df8bae1dSRodney W. Grimes 678f708ef1bSPoul-Henning Kamp static void 679df8bae1dSRodney W. Grimes unp_disconnect(unp) 680df8bae1dSRodney W. Grimes struct unpcb *unp; 681df8bae1dSRodney W. Grimes { 682df8bae1dSRodney W. Grimes register struct unpcb *unp2 = unp->unp_conn; 683df8bae1dSRodney W. Grimes 684df8bae1dSRodney W. Grimes if (unp2 == 0) 685df8bae1dSRodney W. Grimes return; 686df8bae1dSRodney W. Grimes unp->unp_conn = 0; 687df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 688df8bae1dSRodney W. Grimes 689df8bae1dSRodney W. Grimes case SOCK_DGRAM: 69098271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 691df8bae1dSRodney W. Grimes unp->unp_socket->so_state &= ~SS_ISCONNECTED; 692df8bae1dSRodney W. Grimes break; 693df8bae1dSRodney W. Grimes 694df8bae1dSRodney W. Grimes case SOCK_STREAM: 695df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 696df8bae1dSRodney W. Grimes unp2->unp_conn = 0; 697df8bae1dSRodney W. Grimes soisdisconnected(unp2->unp_socket); 698df8bae1dSRodney W. Grimes break; 699df8bae1dSRodney W. Grimes } 700df8bae1dSRodney W. Grimes } 701df8bae1dSRodney W. Grimes 702df8bae1dSRodney W. Grimes #ifdef notdef 70326f9a767SRodney W. Grimes void 704df8bae1dSRodney W. Grimes unp_abort(unp) 705df8bae1dSRodney W. Grimes struct unpcb *unp; 706df8bae1dSRodney W. Grimes { 707df8bae1dSRodney W. Grimes 708df8bae1dSRodney W. Grimes unp_detach(unp); 709df8bae1dSRodney W. Grimes } 710df8bae1dSRodney W. Grimes #endif 711df8bae1dSRodney W. Grimes 71298271db4SGarrett Wollman static int 71375c13541SPoul-Henning Kamp prison_unpcb(struct proc *p, struct unpcb *unp) 71475c13541SPoul-Henning Kamp { 71575c13541SPoul-Henning Kamp if (!p->p_prison) 71675c13541SPoul-Henning Kamp return (0); 71775c13541SPoul-Henning Kamp if (p->p_fd->fd_rdir == unp->unp_rvnode) 71875c13541SPoul-Henning Kamp return (0); 71975c13541SPoul-Henning Kamp return (1); 72075c13541SPoul-Henning Kamp } 72175c13541SPoul-Henning Kamp 72275c13541SPoul-Henning Kamp static int 72398271db4SGarrett Wollman unp_pcblist SYSCTL_HANDLER_ARGS 72498271db4SGarrett Wollman { 725f5ef029eSPoul-Henning Kamp int error, i, n; 72698271db4SGarrett Wollman struct unpcb *unp, **unp_list; 72798271db4SGarrett Wollman unp_gen_t gencnt; 72898271db4SGarrett Wollman struct xunpgen xug; 72998271db4SGarrett Wollman struct unp_head *head; 73098271db4SGarrett Wollman 731a23d65bfSBruce Evans head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 73298271db4SGarrett Wollman 73398271db4SGarrett Wollman /* 73498271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 73598271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 73698271db4SGarrett Wollman */ 73798271db4SGarrett Wollman if (req->oldptr == 0) { 73898271db4SGarrett Wollman n = unp_count; 73998271db4SGarrett Wollman req->oldidx = 2 * (sizeof xug) 74098271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 74198271db4SGarrett Wollman return 0; 74298271db4SGarrett Wollman } 74398271db4SGarrett Wollman 74498271db4SGarrett Wollman if (req->newptr != 0) 74598271db4SGarrett Wollman return EPERM; 74698271db4SGarrett Wollman 74798271db4SGarrett Wollman /* 74898271db4SGarrett Wollman * OK, now we're committed to doing something. 74998271db4SGarrett Wollman */ 75098271db4SGarrett Wollman gencnt = unp_gencnt; 75198271db4SGarrett Wollman n = unp_count; 75298271db4SGarrett Wollman 75398271db4SGarrett Wollman xug.xug_len = sizeof xug; 75498271db4SGarrett Wollman xug.xug_count = n; 75598271db4SGarrett Wollman xug.xug_gen = gencnt; 75698271db4SGarrett Wollman xug.xug_sogen = so_gencnt; 75798271db4SGarrett Wollman error = SYSCTL_OUT(req, &xug, sizeof xug); 75898271db4SGarrett Wollman if (error) 75998271db4SGarrett Wollman return error; 76098271db4SGarrett Wollman 76198271db4SGarrett Wollman unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 76298271db4SGarrett Wollman if (unp_list == 0) 76398271db4SGarrett Wollman return ENOMEM; 76498271db4SGarrett Wollman 7652e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 7662e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 76775c13541SPoul-Henning Kamp if (unp->unp_gencnt <= gencnt && !prison_unpcb(req->p, unp)) 76898271db4SGarrett Wollman unp_list[i++] = unp; 76998271db4SGarrett Wollman } 77098271db4SGarrett Wollman n = i; /* in case we lost some during malloc */ 77198271db4SGarrett Wollman 77298271db4SGarrett Wollman error = 0; 77398271db4SGarrett Wollman for (i = 0; i < n; i++) { 77498271db4SGarrett Wollman unp = unp_list[i]; 77598271db4SGarrett Wollman if (unp->unp_gencnt <= gencnt) { 77698271db4SGarrett Wollman struct xunpcb xu; 77798271db4SGarrett Wollman xu.xu_len = sizeof xu; 77898271db4SGarrett Wollman xu.xu_unpp = unp; 77998271db4SGarrett Wollman /* 78098271db4SGarrett Wollman * XXX - need more locking here to protect against 78198271db4SGarrett Wollman * connect/disconnect races for SMP. 78298271db4SGarrett Wollman */ 78398271db4SGarrett Wollman if (unp->unp_addr) 78498271db4SGarrett Wollman bcopy(unp->unp_addr, &xu.xu_addr, 78598271db4SGarrett Wollman unp->unp_addr->sun_len); 78698271db4SGarrett Wollman if (unp->unp_conn && unp->unp_conn->unp_addr) 78798271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 78898271db4SGarrett Wollman &xu.xu_caddr, 78998271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 79098271db4SGarrett Wollman bcopy(unp, &xu.xu_unp, sizeof *unp); 79198271db4SGarrett Wollman sotoxsocket(unp->unp_socket, &xu.xu_socket); 79298271db4SGarrett Wollman error = SYSCTL_OUT(req, &xu, sizeof xu); 79398271db4SGarrett Wollman } 79498271db4SGarrett Wollman } 79598271db4SGarrett Wollman if (!error) { 79698271db4SGarrett Wollman /* 79798271db4SGarrett Wollman * Give the user an updated idea of our state. 79898271db4SGarrett Wollman * If the generation differs from what we told 79998271db4SGarrett Wollman * her before, she knows that something happened 80098271db4SGarrett Wollman * while we were processing this request, and it 80198271db4SGarrett Wollman * might be necessary to retry. 80298271db4SGarrett Wollman */ 80398271db4SGarrett Wollman xug.xug_gen = unp_gencnt; 80498271db4SGarrett Wollman xug.xug_sogen = so_gencnt; 80598271db4SGarrett Wollman xug.xug_count = unp_count; 80698271db4SGarrett Wollman error = SYSCTL_OUT(req, &xug, sizeof xug); 80798271db4SGarrett Wollman } 80898271db4SGarrett Wollman free(unp_list, M_TEMP); 80998271db4SGarrett Wollman return error; 81098271db4SGarrett Wollman } 81198271db4SGarrett Wollman 81298271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 81398271db4SGarrett Wollman (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 81498271db4SGarrett Wollman "List of active local datagram sockets"); 81598271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 81698271db4SGarrett Wollman (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 81798271db4SGarrett Wollman "List of active local stream sockets"); 81898271db4SGarrett Wollman 819f708ef1bSPoul-Henning Kamp static void 820df8bae1dSRodney W. Grimes unp_shutdown(unp) 821df8bae1dSRodney W. Grimes struct unpcb *unp; 822df8bae1dSRodney W. Grimes { 823df8bae1dSRodney W. Grimes struct socket *so; 824df8bae1dSRodney W. Grimes 825df8bae1dSRodney W. Grimes if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 826df8bae1dSRodney W. Grimes (so = unp->unp_conn->unp_socket)) 827df8bae1dSRodney W. Grimes socantrcvmore(so); 828df8bae1dSRodney W. Grimes } 829df8bae1dSRodney W. Grimes 830f708ef1bSPoul-Henning Kamp static void 831df8bae1dSRodney W. Grimes unp_drop(unp, errno) 832df8bae1dSRodney W. Grimes struct unpcb *unp; 833df8bae1dSRodney W. Grimes int errno; 834df8bae1dSRodney W. Grimes { 835df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 836df8bae1dSRodney W. Grimes 837df8bae1dSRodney W. Grimes so->so_error = errno; 838df8bae1dSRodney W. Grimes unp_disconnect(unp); 839df8bae1dSRodney W. Grimes if (so->so_head) { 84098271db4SGarrett Wollman LIST_REMOVE(unp, unp_link); 84198271db4SGarrett Wollman unp->unp_gencnt = ++unp_gencnt; 84298271db4SGarrett Wollman unp_count--; 843df8bae1dSRodney W. Grimes so->so_pcb = (caddr_t) 0; 84457bf258eSGarrett Wollman if (unp->unp_addr) 84557bf258eSGarrett Wollman FREE(unp->unp_addr, M_SONAME); 84698271db4SGarrett Wollman zfree(unp_zone, unp); 847df8bae1dSRodney W. Grimes sofree(so); 848df8bae1dSRodney W. Grimes } 849df8bae1dSRodney W. Grimes } 850df8bae1dSRodney W. Grimes 851df8bae1dSRodney W. Grimes #ifdef notdef 85226f9a767SRodney W. Grimes void 853df8bae1dSRodney W. Grimes unp_drain() 854df8bae1dSRodney W. Grimes { 855df8bae1dSRodney W. Grimes 856df8bae1dSRodney W. Grimes } 857df8bae1dSRodney W. Grimes #endif 858df8bae1dSRodney W. Grimes 85926f9a767SRodney W. Grimes int 860df8bae1dSRodney W. Grimes unp_externalize(rights) 861df8bae1dSRodney W. Grimes struct mbuf *rights; 862df8bae1dSRodney W. Grimes { 863df8bae1dSRodney W. Grimes struct proc *p = curproc; /* XXX */ 864df8bae1dSRodney W. Grimes register int i; 865df8bae1dSRodney W. Grimes register struct cmsghdr *cm = mtod(rights, struct cmsghdr *); 866df8bae1dSRodney W. Grimes register struct file **rp = (struct file **)(cm + 1); 867df8bae1dSRodney W. Grimes register struct file *fp; 868df8bae1dSRodney W. Grimes int newfds = (cm->cmsg_len - sizeof(*cm)) / sizeof (int); 869df8bae1dSRodney W. Grimes int f; 870df8bae1dSRodney W. Grimes 871ed5b7817SJulian Elischer /* 872ed5b7817SJulian Elischer * if the new FD's will not fit, then we free them all 873ed5b7817SJulian Elischer */ 874df8bae1dSRodney W. Grimes if (!fdavail(p, newfds)) { 875df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 876df8bae1dSRodney W. Grimes fp = *rp; 877df8bae1dSRodney W. Grimes unp_discard(fp); 878df8bae1dSRodney W. Grimes *rp++ = 0; 879df8bae1dSRodney W. Grimes } 880df8bae1dSRodney W. Grimes return (EMSGSIZE); 881df8bae1dSRodney W. Grimes } 882ed5b7817SJulian Elischer /* 883ed5b7817SJulian Elischer * now change each pointer to an fd in the global table to 884ed5b7817SJulian Elischer * an integer that is the index to the local fd table entry 885ed5b7817SJulian Elischer * that we set up to point to the global one we are transferring. 886ed5b7817SJulian Elischer * XXX this assumes a pointer and int are the same size...! 887ed5b7817SJulian Elischer */ 888df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 889df8bae1dSRodney W. Grimes if (fdalloc(p, 0, &f)) 890df8bae1dSRodney W. Grimes panic("unp_externalize"); 891df8bae1dSRodney W. Grimes fp = *rp; 892df8bae1dSRodney W. Grimes p->p_fd->fd_ofiles[f] = fp; 893df8bae1dSRodney W. Grimes fp->f_msgcount--; 894df8bae1dSRodney W. Grimes unp_rights--; 895df8bae1dSRodney W. Grimes *(int *)rp++ = f; 896df8bae1dSRodney W. Grimes } 897df8bae1dSRodney W. Grimes return (0); 898df8bae1dSRodney W. Grimes } 899df8bae1dSRodney W. Grimes 90098271db4SGarrett Wollman void 90198271db4SGarrett Wollman unp_init(void) 90298271db4SGarrett Wollman { 90398271db4SGarrett Wollman unp_zone = zinit("unpcb", sizeof(struct unpcb), nmbclusters, 0, 0); 90498271db4SGarrett Wollman if (unp_zone == 0) 90598271db4SGarrett Wollman panic("unp_init"); 90698271db4SGarrett Wollman LIST_INIT(&unp_dhead); 90798271db4SGarrett Wollman LIST_INIT(&unp_shead); 90898271db4SGarrett Wollman } 90998271db4SGarrett Wollman 9100b788fa1SBill Paul #ifndef MIN 9110b788fa1SBill Paul #define MIN(a,b) (((a)<(b))?(a):(b)) 9120b788fa1SBill Paul #endif 9130b788fa1SBill Paul 914f708ef1bSPoul-Henning Kamp static int 915df8bae1dSRodney W. Grimes unp_internalize(control, p) 916df8bae1dSRodney W. Grimes struct mbuf *control; 917df8bae1dSRodney W. Grimes struct proc *p; 918df8bae1dSRodney W. Grimes { 919df8bae1dSRodney W. Grimes struct filedesc *fdp = p->p_fd; 920df8bae1dSRodney W. Grimes register struct cmsghdr *cm = mtod(control, struct cmsghdr *); 921df8bae1dSRodney W. Grimes register struct file **rp; 922df8bae1dSRodney W. Grimes register struct file *fp; 923df8bae1dSRodney W. Grimes register int i, fd; 9240b788fa1SBill Paul register struct cmsgcred *cmcred; 925df8bae1dSRodney W. Grimes int oldfds; 926df8bae1dSRodney W. Grimes 9270b788fa1SBill Paul if ((cm->cmsg_type != SCM_RIGHTS && cm->cmsg_type != SCM_CREDS) || 9280b788fa1SBill Paul cm->cmsg_level != SOL_SOCKET || cm->cmsg_len != control->m_len) 929df8bae1dSRodney W. Grimes return (EINVAL); 9300b788fa1SBill Paul 9310b788fa1SBill Paul /* 9320b788fa1SBill Paul * Fill in credential information. 9330b788fa1SBill Paul */ 9340b788fa1SBill Paul if (cm->cmsg_type == SCM_CREDS) { 9350b788fa1SBill Paul cmcred = (struct cmsgcred *)(cm + 1); 9360b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 9370b788fa1SBill Paul cmcred->cmcred_uid = p->p_cred->p_ruid; 9380b788fa1SBill Paul cmcred->cmcred_gid = p->p_cred->p_rgid; 9390b788fa1SBill Paul cmcred->cmcred_euid = p->p_ucred->cr_uid; 9400b788fa1SBill Paul cmcred->cmcred_ngroups = MIN(p->p_ucred->cr_ngroups, 9410b788fa1SBill Paul CMGROUP_MAX); 9420b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 9430b788fa1SBill Paul cmcred->cmcred_groups[i] = p->p_ucred->cr_groups[i]; 9440b788fa1SBill Paul return(0); 9450b788fa1SBill Paul } 9460b788fa1SBill Paul 947df8bae1dSRodney W. Grimes oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int); 948ed5b7817SJulian Elischer /* 949ed5b7817SJulian Elischer * check that all the FDs passed in refer to legal OPEN files 950ed5b7817SJulian Elischer * If not, reject the entire operation. 951ed5b7817SJulian Elischer */ 952df8bae1dSRodney W. Grimes rp = (struct file **)(cm + 1); 953df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 954df8bae1dSRodney W. Grimes fd = *(int *)rp++; 955df8bae1dSRodney W. Grimes if ((unsigned)fd >= fdp->fd_nfiles || 956df8bae1dSRodney W. Grimes fdp->fd_ofiles[fd] == NULL) 957df8bae1dSRodney W. Grimes return (EBADF); 958df8bae1dSRodney W. Grimes } 959ed5b7817SJulian Elischer /* 960ed5b7817SJulian Elischer * Now replace the integer FDs with pointers to 961ed5b7817SJulian Elischer * the associated global file table entry.. 962ed5b7817SJulian Elischer * XXX this assumes a pointer and an int are the same size! 963ed5b7817SJulian Elischer */ 964df8bae1dSRodney W. Grimes rp = (struct file **)(cm + 1); 965df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 966df8bae1dSRodney W. Grimes fp = fdp->fd_ofiles[*(int *)rp]; 967df8bae1dSRodney W. Grimes *rp++ = fp; 968df8bae1dSRodney W. Grimes fp->f_count++; 969df8bae1dSRodney W. Grimes fp->f_msgcount++; 970df8bae1dSRodney W. Grimes unp_rights++; 971df8bae1dSRodney W. Grimes } 972df8bae1dSRodney W. Grimes return (0); 973df8bae1dSRodney W. Grimes } 974df8bae1dSRodney W. Grimes 975f708ef1bSPoul-Henning Kamp static int unp_defer, unp_gcing; 976df8bae1dSRodney W. Grimes 977f708ef1bSPoul-Henning Kamp static void 978df8bae1dSRodney W. Grimes unp_gc() 979df8bae1dSRodney W. Grimes { 980df8bae1dSRodney W. Grimes register struct file *fp, *nextfp; 981df8bae1dSRodney W. Grimes register struct socket *so; 982df8bae1dSRodney W. Grimes struct file **extra_ref, **fpp; 983df8bae1dSRodney W. Grimes int nunref, i; 984df8bae1dSRodney W. Grimes 985df8bae1dSRodney W. Grimes if (unp_gcing) 986df8bae1dSRodney W. Grimes return; 987df8bae1dSRodney W. Grimes unp_gcing = 1; 988df8bae1dSRodney W. Grimes unp_defer = 0; 989ed5b7817SJulian Elischer /* 990ed5b7817SJulian Elischer * before going through all this, set all FDs to 991ed5b7817SJulian Elischer * be NOT defered and NOT externally accessible 992ed5b7817SJulian Elischer */ 9932e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) 994df8bae1dSRodney W. Grimes fp->f_flag &= ~(FMARK|FDEFER); 995df8bae1dSRodney W. Grimes do { 9962e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) { 997ed5b7817SJulian Elischer /* 998ed5b7817SJulian Elischer * If the file is not open, skip it 999ed5b7817SJulian Elischer */ 1000df8bae1dSRodney W. Grimes if (fp->f_count == 0) 1001df8bae1dSRodney W. Grimes continue; 1002ed5b7817SJulian Elischer /* 1003ed5b7817SJulian Elischer * If we already marked it as 'defer' in a 1004ed5b7817SJulian Elischer * previous pass, then try process it this time 1005ed5b7817SJulian Elischer * and un-mark it 1006ed5b7817SJulian Elischer */ 1007df8bae1dSRodney W. Grimes if (fp->f_flag & FDEFER) { 1008df8bae1dSRodney W. Grimes fp->f_flag &= ~FDEFER; 1009df8bae1dSRodney W. Grimes unp_defer--; 1010df8bae1dSRodney W. Grimes } else { 1011ed5b7817SJulian Elischer /* 1012ed5b7817SJulian Elischer * if it's not defered, then check if it's 1013ed5b7817SJulian Elischer * already marked.. if so skip it 1014ed5b7817SJulian Elischer */ 1015df8bae1dSRodney W. Grimes if (fp->f_flag & FMARK) 1016df8bae1dSRodney W. Grimes continue; 1017ed5b7817SJulian Elischer /* 1018ed5b7817SJulian Elischer * If all references are from messages 1019ed5b7817SJulian Elischer * in transit, then skip it. it's not 1020ed5b7817SJulian Elischer * externally accessible. 1021ed5b7817SJulian Elischer */ 1022df8bae1dSRodney W. Grimes if (fp->f_count == fp->f_msgcount) 1023df8bae1dSRodney W. Grimes continue; 1024ed5b7817SJulian Elischer /* 1025ed5b7817SJulian Elischer * If it got this far then it must be 1026ed5b7817SJulian Elischer * externally accessible. 1027ed5b7817SJulian Elischer */ 1028df8bae1dSRodney W. Grimes fp->f_flag |= FMARK; 1029df8bae1dSRodney W. Grimes } 1030ed5b7817SJulian Elischer /* 1031ed5b7817SJulian Elischer * either it was defered, or it is externally 1032ed5b7817SJulian Elischer * accessible and not already marked so. 1033ed5b7817SJulian Elischer * Now check if it is possibly one of OUR sockets. 1034ed5b7817SJulian Elischer */ 1035df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_SOCKET || 1036df8bae1dSRodney W. Grimes (so = (struct socket *)fp->f_data) == 0) 1037df8bae1dSRodney W. Grimes continue; 1038748e0b0aSGarrett Wollman if (so->so_proto->pr_domain != &localdomain || 1039df8bae1dSRodney W. Grimes (so->so_proto->pr_flags&PR_RIGHTS) == 0) 1040df8bae1dSRodney W. Grimes continue; 1041df8bae1dSRodney W. Grimes #ifdef notdef 1042df8bae1dSRodney W. Grimes if (so->so_rcv.sb_flags & SB_LOCK) { 1043df8bae1dSRodney W. Grimes /* 1044df8bae1dSRodney W. Grimes * This is problematical; it's not clear 1045df8bae1dSRodney W. Grimes * we need to wait for the sockbuf to be 1046df8bae1dSRodney W. Grimes * unlocked (on a uniprocessor, at least), 1047df8bae1dSRodney W. Grimes * and it's also not clear what to do 1048df8bae1dSRodney W. Grimes * if sbwait returns an error due to receipt 1049df8bae1dSRodney W. Grimes * of a signal. If sbwait does return 1050df8bae1dSRodney W. Grimes * an error, we'll go into an infinite 1051df8bae1dSRodney W. Grimes * loop. Delete all of this for now. 1052df8bae1dSRodney W. Grimes */ 1053df8bae1dSRodney W. Grimes (void) sbwait(&so->so_rcv); 1054df8bae1dSRodney W. Grimes goto restart; 1055df8bae1dSRodney W. Grimes } 1056df8bae1dSRodney W. Grimes #endif 1057ed5b7817SJulian Elischer /* 1058ed5b7817SJulian Elischer * So, Ok, it's one of our sockets and it IS externally 1059ed5b7817SJulian Elischer * accessible (or was defered). Now we look 1060dc733423SDag-Erling Smørgrav * to see if we hold any file descriptors in its 1061ed5b7817SJulian Elischer * message buffers. Follow those links and mark them 1062ed5b7817SJulian Elischer * as accessible too. 1063ed5b7817SJulian Elischer */ 1064df8bae1dSRodney W. Grimes unp_scan(so->so_rcv.sb_mb, unp_mark); 1065df8bae1dSRodney W. Grimes } 1066df8bae1dSRodney W. Grimes } while (unp_defer); 1067df8bae1dSRodney W. Grimes /* 1068df8bae1dSRodney W. Grimes * We grab an extra reference to each of the file table entries 1069df8bae1dSRodney W. Grimes * that are not otherwise accessible and then free the rights 1070df8bae1dSRodney W. Grimes * that are stored in messages on them. 1071df8bae1dSRodney W. Grimes * 1072df8bae1dSRodney W. Grimes * The bug in the orginal code is a little tricky, so I'll describe 1073df8bae1dSRodney W. Grimes * what's wrong with it here. 1074df8bae1dSRodney W. Grimes * 1075df8bae1dSRodney W. Grimes * It is incorrect to simply unp_discard each entry for f_msgcount 1076df8bae1dSRodney W. Grimes * times -- consider the case of sockets A and B that contain 1077df8bae1dSRodney W. Grimes * references to each other. On a last close of some other socket, 1078df8bae1dSRodney W. Grimes * we trigger a gc since the number of outstanding rights (unp_rights) 1079df8bae1dSRodney W. Grimes * is non-zero. If during the sweep phase the gc code un_discards, 1080df8bae1dSRodney W. Grimes * we end up doing a (full) closef on the descriptor. A closef on A 1081df8bae1dSRodney W. Grimes * results in the following chain. Closef calls soo_close, which 1082df8bae1dSRodney W. Grimes * calls soclose. Soclose calls first (through the switch 1083df8bae1dSRodney W. Grimes * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 1084df8bae1dSRodney W. Grimes * returns because the previous instance had set unp_gcing, and 1085df8bae1dSRodney W. Grimes * we return all the way back to soclose, which marks the socket 1086df8bae1dSRodney W. Grimes * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush 1087df8bae1dSRodney W. Grimes * to free up the rights that are queued in messages on the socket A, 1088df8bae1dSRodney W. Grimes * i.e., the reference on B. The sorflush calls via the dom_dispose 1089df8bae1dSRodney W. Grimes * switch unp_dispose, which unp_scans with unp_discard. This second 1090df8bae1dSRodney W. Grimes * instance of unp_discard just calls closef on B. 1091df8bae1dSRodney W. Grimes * 1092df8bae1dSRodney W. Grimes * Well, a similar chain occurs on B, resulting in a sorflush on B, 1093df8bae1dSRodney W. Grimes * which results in another closef on A. Unfortunately, A is already 1094df8bae1dSRodney W. Grimes * being closed, and the descriptor has already been marked with 1095df8bae1dSRodney W. Grimes * SS_NOFDREF, and soclose panics at this point. 1096df8bae1dSRodney W. Grimes * 1097df8bae1dSRodney W. Grimes * Here, we first take an extra reference to each inaccessible 1098df8bae1dSRodney W. Grimes * descriptor. Then, we call sorflush ourself, since we know 1099df8bae1dSRodney W. Grimes * it is a Unix domain socket anyhow. After we destroy all the 1100df8bae1dSRodney W. Grimes * rights carried in messages, we do a last closef to get rid 1101df8bae1dSRodney W. Grimes * of our extra reference. This is the last close, and the 1102df8bae1dSRodney W. Grimes * unp_detach etc will shut down the socket. 1103df8bae1dSRodney W. Grimes * 1104df8bae1dSRodney W. Grimes * 91/09/19, bsy@cs.cmu.edu 1105df8bae1dSRodney W. Grimes */ 1106df8bae1dSRodney W. Grimes extra_ref = malloc(nfiles * sizeof(struct file *), M_FILE, M_WAITOK); 11072e3c8fcbSPoul-Henning Kamp for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; fp != 0; 1108bc6f0e79SJeffrey Hsu fp = nextfp) { 11092e3c8fcbSPoul-Henning Kamp nextfp = LIST_NEXT(fp, f_list); 1110ed5b7817SJulian Elischer /* 1111ed5b7817SJulian Elischer * If it's not open, skip it 1112ed5b7817SJulian Elischer */ 1113df8bae1dSRodney W. Grimes if (fp->f_count == 0) 1114df8bae1dSRodney W. Grimes continue; 1115ed5b7817SJulian Elischer /* 1116ed5b7817SJulian Elischer * If all refs are from msgs, and it's not marked accessible 1117ed5b7817SJulian Elischer * then it must be referenced from some unreachable cycle 1118ed5b7817SJulian Elischer * of (shut-down) FDs, so include it in our 1119ed5b7817SJulian Elischer * list of FDs to remove 1120ed5b7817SJulian Elischer */ 1121df8bae1dSRodney W. Grimes if (fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) { 1122df8bae1dSRodney W. Grimes *fpp++ = fp; 1123df8bae1dSRodney W. Grimes nunref++; 1124df8bae1dSRodney W. Grimes fp->f_count++; 1125df8bae1dSRodney W. Grimes } 1126df8bae1dSRodney W. Grimes } 1127ed5b7817SJulian Elischer /* 1128ed5b7817SJulian Elischer * for each FD on our hit list, do the following two things 1129ed5b7817SJulian Elischer */ 11301c7c3c6aSMatthew Dillon for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 11311c7c3c6aSMatthew Dillon struct file *tfp = *fpp; 11321c7c3c6aSMatthew Dillon if (tfp->f_type == DTYPE_SOCKET && tfp->f_data != NULL) 11331c7c3c6aSMatthew Dillon sorflush((struct socket *)(tfp->f_data)); 11341c7c3c6aSMatthew Dillon } 1135df8bae1dSRodney W. Grimes for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) 113692cbac68SPoul-Henning Kamp closef(*fpp, (struct proc *) NULL); 1137df8bae1dSRodney W. Grimes free((caddr_t)extra_ref, M_FILE); 1138df8bae1dSRodney W. Grimes unp_gcing = 0; 1139df8bae1dSRodney W. Grimes } 1140df8bae1dSRodney W. Grimes 114126f9a767SRodney W. Grimes void 1142df8bae1dSRodney W. Grimes unp_dispose(m) 1143df8bae1dSRodney W. Grimes struct mbuf *m; 1144df8bae1dSRodney W. Grimes { 1145996c772fSJohn Dyson 1146df8bae1dSRodney W. Grimes if (m) 1147df8bae1dSRodney W. Grimes unp_scan(m, unp_discard); 1148df8bae1dSRodney W. Grimes } 1149df8bae1dSRodney W. Grimes 1150f708ef1bSPoul-Henning Kamp static void 1151df8bae1dSRodney W. Grimes unp_scan(m0, op) 1152df8bae1dSRodney W. Grimes register struct mbuf *m0; 1153996c772fSJohn Dyson void (*op) __P((struct file *)); 1154df8bae1dSRodney W. Grimes { 1155df8bae1dSRodney W. Grimes register struct mbuf *m; 1156df8bae1dSRodney W. Grimes register struct file **rp; 1157df8bae1dSRodney W. Grimes register struct cmsghdr *cm; 1158df8bae1dSRodney W. Grimes register int i; 1159df8bae1dSRodney W. Grimes int qfds; 1160df8bae1dSRodney W. Grimes 1161df8bae1dSRodney W. Grimes while (m0) { 1162df8bae1dSRodney W. Grimes for (m = m0; m; m = m->m_next) 1163df8bae1dSRodney W. Grimes if (m->m_type == MT_CONTROL && 1164df8bae1dSRodney W. Grimes m->m_len >= sizeof(*cm)) { 1165df8bae1dSRodney W. Grimes cm = mtod(m, struct cmsghdr *); 1166df8bae1dSRodney W. Grimes if (cm->cmsg_level != SOL_SOCKET || 1167df8bae1dSRodney W. Grimes cm->cmsg_type != SCM_RIGHTS) 1168df8bae1dSRodney W. Grimes continue; 1169df8bae1dSRodney W. Grimes qfds = (cm->cmsg_len - sizeof *cm) 1170df8bae1dSRodney W. Grimes / sizeof (struct file *); 1171df8bae1dSRodney W. Grimes rp = (struct file **)(cm + 1); 1172df8bae1dSRodney W. Grimes for (i = 0; i < qfds; i++) 1173df8bae1dSRodney W. Grimes (*op)(*rp++); 1174df8bae1dSRodney W. Grimes break; /* XXX, but saves time */ 1175df8bae1dSRodney W. Grimes } 1176df8bae1dSRodney W. Grimes m0 = m0->m_act; 1177df8bae1dSRodney W. Grimes } 1178df8bae1dSRodney W. Grimes } 1179df8bae1dSRodney W. Grimes 1180f708ef1bSPoul-Henning Kamp static void 1181df8bae1dSRodney W. Grimes unp_mark(fp) 1182df8bae1dSRodney W. Grimes struct file *fp; 1183df8bae1dSRodney W. Grimes { 1184df8bae1dSRodney W. Grimes 1185df8bae1dSRodney W. Grimes if (fp->f_flag & FMARK) 1186df8bae1dSRodney W. Grimes return; 1187df8bae1dSRodney W. Grimes unp_defer++; 1188df8bae1dSRodney W. Grimes fp->f_flag |= (FMARK|FDEFER); 1189df8bae1dSRodney W. Grimes } 1190df8bae1dSRodney W. Grimes 1191f708ef1bSPoul-Henning Kamp static void 1192df8bae1dSRodney W. Grimes unp_discard(fp) 1193df8bae1dSRodney W. Grimes struct file *fp; 1194df8bae1dSRodney W. Grimes { 1195df8bae1dSRodney W. Grimes 1196df8bae1dSRodney W. Grimes fp->f_msgcount--; 1197df8bae1dSRodney W. Grimes unp_rights--; 1198df8bae1dSRodney W. Grimes (void) closef(fp, (struct proc *)NULL); 1199df8bae1dSRodney W. Grimes } 1200