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> 403ac4d1efSBruce Evans #include <sys/fcntl.h> 41fb919e4dSMark Murray #include <sys/domain.h> 42fb919e4dSMark Murray #include <sys/filedesc.h> 43fb919e4dSMark Murray #include <sys/lock.h> 44d826c479SBruce Evans #include <sys/malloc.h> /* XXX must be before <sys/file.h> */ 45639acc13SGarrett Wollman #include <sys/file.h> 46fb919e4dSMark Murray #include <sys/mutex.h> 47639acc13SGarrett Wollman #include <sys/mbuf.h> 48639acc13SGarrett Wollman #include <sys/namei.h> 49639acc13SGarrett Wollman #include <sys/proc.h> 50df8bae1dSRodney W. Grimes #include <sys/protosw.h> 51df8bae1dSRodney W. Grimes #include <sys/socket.h> 52df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 53c6362551SAlfred Perlstein #include <sys/resourcevar.h> 54df8bae1dSRodney W. Grimes #include <sys/stat.h> 55639acc13SGarrett Wollman #include <sys/sysctl.h> 56639acc13SGarrett Wollman #include <sys/un.h> 5798271db4SGarrett Wollman #include <sys/unpcb.h> 58639acc13SGarrett Wollman #include <sys/vnode.h> 5991421ba2SRobert Watson #include <sys/jail.h> 60df8bae1dSRodney W. Grimes 6198271db4SGarrett Wollman #include <vm/vm_zone.h> 6298271db4SGarrett Wollman 63632a035fSEivind Eklund static struct vm_zone *unp_zone; 6498271db4SGarrett Wollman static unp_gen_t unp_gencnt; 6598271db4SGarrett Wollman static u_int unp_count; 6698271db4SGarrett Wollman 6798271db4SGarrett Wollman static struct unp_head unp_shead, unp_dhead; 6898271db4SGarrett Wollman 69df8bae1dSRodney W. Grimes /* 70df8bae1dSRodney W. Grimes * Unix communications domain. 71df8bae1dSRodney W. Grimes * 72df8bae1dSRodney W. Grimes * TODO: 73df8bae1dSRodney W. Grimes * SEQPACKET, RDM 74df8bae1dSRodney W. Grimes * rethink name space problems 75df8bae1dSRodney W. Grimes * need a proper out-of-band 7698271db4SGarrett Wollman * lock pushdown 77df8bae1dSRodney W. Grimes */ 78f708ef1bSPoul-Henning Kamp static struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 79f708ef1bSPoul-Henning Kamp static ino_t unp_ino; /* prototype for fake inode numbers */ 80f708ef1bSPoul-Henning Kamp 81f708ef1bSPoul-Henning Kamp static int unp_attach __P((struct socket *)); 82f708ef1bSPoul-Henning Kamp static void unp_detach __P((struct unpcb *)); 8357bf258eSGarrett Wollman static int unp_bind __P((struct unpcb *,struct sockaddr *, struct proc *)); 8457bf258eSGarrett Wollman static int unp_connect __P((struct socket *,struct sockaddr *, 8557bf258eSGarrett Wollman struct proc *)); 86f708ef1bSPoul-Henning Kamp static void unp_disconnect __P((struct unpcb *)); 87f708ef1bSPoul-Henning Kamp static void unp_shutdown __P((struct unpcb *)); 88f708ef1bSPoul-Henning Kamp static void unp_drop __P((struct unpcb *, int)); 89f708ef1bSPoul-Henning Kamp static void unp_gc __P((void)); 90f708ef1bSPoul-Henning Kamp static void unp_scan __P((struct mbuf *, void (*)(struct file *))); 91f708ef1bSPoul-Henning Kamp static void unp_mark __P((struct file *)); 92f708ef1bSPoul-Henning Kamp static void unp_discard __P((struct file *)); 93f708ef1bSPoul-Henning Kamp static int unp_internalize __P((struct mbuf *, struct proc *)); 940c1bb4fbSDima Dorfman static int unp_listen __P((struct unpcb *, struct proc *)); 95f708ef1bSPoul-Henning Kamp 96a29f300eSGarrett Wollman static int 97a29f300eSGarrett Wollman uipc_abort(struct socket *so) 98df8bae1dSRodney W. Grimes { 99df8bae1dSRodney W. Grimes struct unpcb *unp = sotounpcb(so); 100df8bae1dSRodney W. Grimes 101a29f300eSGarrett Wollman if (unp == 0) 102a29f300eSGarrett Wollman return EINVAL; 103a29f300eSGarrett Wollman unp_drop(unp, ECONNABORTED); 104a29f300eSGarrett Wollman return 0; 105df8bae1dSRodney W. Grimes } 106df8bae1dSRodney W. Grimes 107a29f300eSGarrett Wollman static int 10857bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam) 109a29f300eSGarrett Wollman { 110a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 111df8bae1dSRodney W. Grimes 112a29f300eSGarrett Wollman if (unp == 0) 113a29f300eSGarrett Wollman return EINVAL; 114df8bae1dSRodney W. Grimes 115df8bae1dSRodney W. Grimes /* 116df8bae1dSRodney W. Grimes * Pass back name of connected socket, 117df8bae1dSRodney W. Grimes * if it was bound and we are still connected 118df8bae1dSRodney W. Grimes * (our peer may have closed already!). 119df8bae1dSRodney W. Grimes */ 120df8bae1dSRodney W. Grimes if (unp->unp_conn && unp->unp_conn->unp_addr) { 12157bf258eSGarrett Wollman *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr, 12257bf258eSGarrett Wollman 1); 123df8bae1dSRodney W. Grimes } else { 12457bf258eSGarrett Wollman *nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1); 125df8bae1dSRodney W. Grimes } 126a29f300eSGarrett Wollman return 0; 127a29f300eSGarrett Wollman } 128df8bae1dSRodney W. Grimes 129a29f300eSGarrett Wollman static int 130a29f300eSGarrett Wollman uipc_attach(struct socket *so, int proto, struct proc *p) 131a29f300eSGarrett Wollman { 132a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 133df8bae1dSRodney W. Grimes 134a29f300eSGarrett Wollman if (unp != 0) 135a29f300eSGarrett Wollman return EISCONN; 136a29f300eSGarrett Wollman return unp_attach(so); 137a29f300eSGarrett Wollman } 138a29f300eSGarrett Wollman 139a29f300eSGarrett Wollman static int 14057bf258eSGarrett Wollman uipc_bind(struct socket *so, struct sockaddr *nam, struct proc *p) 141a29f300eSGarrett Wollman { 142a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 143a29f300eSGarrett Wollman 144a29f300eSGarrett Wollman if (unp == 0) 145a29f300eSGarrett Wollman return EINVAL; 146a29f300eSGarrett Wollman 147a29f300eSGarrett Wollman return unp_bind(unp, nam, p); 148a29f300eSGarrett Wollman } 149a29f300eSGarrett Wollman 150a29f300eSGarrett Wollman static int 15157bf258eSGarrett Wollman uipc_connect(struct socket *so, struct sockaddr *nam, struct proc *p) 152a29f300eSGarrett Wollman { 153a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 154a29f300eSGarrett Wollman 155a29f300eSGarrett Wollman if (unp == 0) 156a29f300eSGarrett Wollman return EINVAL; 157a29f300eSGarrett Wollman return unp_connect(so, nam, curproc); 158a29f300eSGarrett Wollman } 159a29f300eSGarrett Wollman 160a29f300eSGarrett Wollman static int 161a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2) 162a29f300eSGarrett Wollman { 163a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so1); 164a29f300eSGarrett Wollman 165a29f300eSGarrett Wollman if (unp == 0) 166a29f300eSGarrett Wollman return EINVAL; 167a29f300eSGarrett Wollman 168a29f300eSGarrett Wollman return unp_connect2(so1, so2); 169a29f300eSGarrett Wollman } 170a29f300eSGarrett Wollman 171a29f300eSGarrett Wollman /* control is EOPNOTSUPP */ 172a29f300eSGarrett Wollman 173a29f300eSGarrett Wollman static int 174a29f300eSGarrett Wollman uipc_detach(struct socket *so) 175a29f300eSGarrett Wollman { 176a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 177a29f300eSGarrett Wollman 178a29f300eSGarrett Wollman if (unp == 0) 179a29f300eSGarrett Wollman return EINVAL; 180a29f300eSGarrett Wollman 181a29f300eSGarrett Wollman unp_detach(unp); 182a29f300eSGarrett Wollman return 0; 183a29f300eSGarrett Wollman } 184a29f300eSGarrett Wollman 185a29f300eSGarrett Wollman static int 186a29f300eSGarrett Wollman uipc_disconnect(struct socket *so) 187a29f300eSGarrett Wollman { 188a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 189a29f300eSGarrett Wollman 190a29f300eSGarrett Wollman if (unp == 0) 191a29f300eSGarrett Wollman return EINVAL; 192a29f300eSGarrett Wollman unp_disconnect(unp); 193a29f300eSGarrett Wollman return 0; 194a29f300eSGarrett Wollman } 195a29f300eSGarrett Wollman 196a29f300eSGarrett Wollman static int 197a29f300eSGarrett Wollman uipc_listen(struct socket *so, struct proc *p) 198a29f300eSGarrett Wollman { 199a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 200a29f300eSGarrett Wollman 201a29f300eSGarrett Wollman if (unp == 0 || unp->unp_vnode == 0) 202a29f300eSGarrett Wollman return EINVAL; 2030c1bb4fbSDima Dorfman return unp_listen(unp, p); 204a29f300eSGarrett Wollman } 205a29f300eSGarrett Wollman 206a29f300eSGarrett Wollman static int 20757bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 208a29f300eSGarrett Wollman { 209a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 210a29f300eSGarrett Wollman 211a29f300eSGarrett Wollman if (unp == 0) 212a29f300eSGarrett Wollman return EINVAL; 21357bf258eSGarrett Wollman if (unp->unp_conn && unp->unp_conn->unp_addr) 21457bf258eSGarrett Wollman *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr, 21557bf258eSGarrett Wollman 1); 216a29f300eSGarrett Wollman return 0; 217a29f300eSGarrett Wollman } 218a29f300eSGarrett Wollman 219a29f300eSGarrett Wollman static int 220a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 221a29f300eSGarrett Wollman { 222a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 223a29f300eSGarrett Wollman struct socket *so2; 2246aef685fSBrian Feldman u_long newhiwat; 225a29f300eSGarrett Wollman 226a29f300eSGarrett Wollman if (unp == 0) 227a29f300eSGarrett Wollman return EINVAL; 228df8bae1dSRodney W. Grimes switch (so->so_type) { 229df8bae1dSRodney W. Grimes case SOCK_DGRAM: 230a29f300eSGarrett Wollman panic("uipc_rcvd DGRAM?"); 231df8bae1dSRodney W. Grimes /*NOTREACHED*/ 232df8bae1dSRodney W. Grimes 233df8bae1dSRodney W. Grimes case SOCK_STREAM: 234df8bae1dSRodney W. Grimes if (unp->unp_conn == 0) 235df8bae1dSRodney W. Grimes break; 236df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 237df8bae1dSRodney W. Grimes /* 238df8bae1dSRodney W. Grimes * Adjust backpressure on sender 239df8bae1dSRodney W. Grimes * and wakeup any waiting to write. 240df8bae1dSRodney W. Grimes */ 241ff8b0106SBrian Feldman so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt; 242ff8b0106SBrian Feldman unp->unp_mbcnt = so->so_rcv.sb_mbcnt; 2436aef685fSBrian Feldman newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - 2446aef685fSBrian Feldman so->so_rcv.sb_cc; 245f535380cSDon Lewis (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat, 2466aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 247ff8b0106SBrian Feldman unp->unp_cc = so->so_rcv.sb_cc; 248df8bae1dSRodney W. Grimes sowwakeup(so2); 249df8bae1dSRodney W. Grimes break; 250df8bae1dSRodney W. Grimes 251df8bae1dSRodney W. Grimes default: 252a29f300eSGarrett Wollman panic("uipc_rcvd unknown socktype"); 253df8bae1dSRodney W. Grimes } 254a29f300eSGarrett Wollman return 0; 255a29f300eSGarrett Wollman } 256df8bae1dSRodney W. Grimes 257a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 258a29f300eSGarrett Wollman 259a29f300eSGarrett Wollman static int 26057bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 261a29f300eSGarrett Wollman struct mbuf *control, struct proc *p) 262a29f300eSGarrett Wollman { 263a29f300eSGarrett Wollman int error = 0; 264a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 265a29f300eSGarrett Wollman struct socket *so2; 2666aef685fSBrian Feldman u_long newhiwat; 267a29f300eSGarrett Wollman 268a29f300eSGarrett Wollman if (unp == 0) { 269a29f300eSGarrett Wollman error = EINVAL; 270a29f300eSGarrett Wollman goto release; 271a29f300eSGarrett Wollman } 272a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 273a29f300eSGarrett Wollman error = EOPNOTSUPP; 274a29f300eSGarrett Wollman goto release; 275a29f300eSGarrett Wollman } 276a29f300eSGarrett Wollman 277df8bae1dSRodney W. Grimes if (control && (error = unp_internalize(control, p))) 278a29f300eSGarrett Wollman goto release; 279df8bae1dSRodney W. Grimes 280a29f300eSGarrett Wollman switch (so->so_type) { 281a29f300eSGarrett Wollman case SOCK_DGRAM: 282a29f300eSGarrett Wollman { 283df8bae1dSRodney W. Grimes struct sockaddr *from; 284df8bae1dSRodney W. Grimes 285df8bae1dSRodney W. Grimes if (nam) { 286df8bae1dSRodney W. Grimes if (unp->unp_conn) { 287df8bae1dSRodney W. Grimes error = EISCONN; 288df8bae1dSRodney W. Grimes break; 289df8bae1dSRodney W. Grimes } 290df8bae1dSRodney W. Grimes error = unp_connect(so, nam, p); 291df8bae1dSRodney W. Grimes if (error) 292df8bae1dSRodney W. Grimes break; 293df8bae1dSRodney W. Grimes } else { 294df8bae1dSRodney W. Grimes if (unp->unp_conn == 0) { 295df8bae1dSRodney W. Grimes error = ENOTCONN; 296df8bae1dSRodney W. Grimes break; 297df8bae1dSRodney W. Grimes } 298df8bae1dSRodney W. Grimes } 299df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 300df8bae1dSRodney W. Grimes if (unp->unp_addr) 30157bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 302df8bae1dSRodney W. Grimes else 303df8bae1dSRodney W. Grimes from = &sun_noname; 304df8bae1dSRodney W. Grimes if (sbappendaddr(&so2->so_rcv, from, m, control)) { 305df8bae1dSRodney W. Grimes sorwakeup(so2); 306df8bae1dSRodney W. Grimes m = 0; 307df8bae1dSRodney W. Grimes control = 0; 308df8bae1dSRodney W. Grimes } else 309df8bae1dSRodney W. Grimes error = ENOBUFS; 310df8bae1dSRodney W. Grimes if (nam) 311df8bae1dSRodney W. Grimes unp_disconnect(unp); 312df8bae1dSRodney W. Grimes break; 313df8bae1dSRodney W. Grimes } 314df8bae1dSRodney W. Grimes 315df8bae1dSRodney W. Grimes case SOCK_STREAM: 3166b8fda4dSGarrett Wollman /* Connect if not connected yet. */ 3176b8fda4dSGarrett Wollman /* 3186b8fda4dSGarrett Wollman * Note: A better implementation would complain 319402cc72dSDavid Greenman * if not equal to the peer's address. 3206b8fda4dSGarrett Wollman */ 321402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 322402cc72dSDavid Greenman if (nam) { 323402cc72dSDavid Greenman error = unp_connect(so, nam, p); 324402cc72dSDavid Greenman if (error) 3256b8fda4dSGarrett Wollman break; /* XXX */ 326402cc72dSDavid Greenman } else { 327402cc72dSDavid Greenman error = ENOTCONN; 328402cc72dSDavid Greenman break; 329402cc72dSDavid Greenman } 330402cc72dSDavid Greenman } 331402cc72dSDavid Greenman 332df8bae1dSRodney W. Grimes if (so->so_state & SS_CANTSENDMORE) { 333df8bae1dSRodney W. Grimes error = EPIPE; 334df8bae1dSRodney W. Grimes break; 335df8bae1dSRodney W. Grimes } 336df8bae1dSRodney W. Grimes if (unp->unp_conn == 0) 337a29f300eSGarrett Wollman panic("uipc_send connected but no connection?"); 338df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 339df8bae1dSRodney W. Grimes /* 340df8bae1dSRodney W. Grimes * Send to paired receive port, and then reduce 341df8bae1dSRodney W. Grimes * send buffer hiwater marks to maintain backpressure. 342df8bae1dSRodney W. Grimes * Wake up readers. 343df8bae1dSRodney W. Grimes */ 344df8bae1dSRodney W. Grimes if (control) { 345ff8b0106SBrian Feldman if (sbappendcontrol(&so2->so_rcv, m, control)) 346df8bae1dSRodney W. Grimes control = 0; 347df8bae1dSRodney W. Grimes } else 348ff8b0106SBrian Feldman sbappend(&so2->so_rcv, m); 349ff8b0106SBrian Feldman so->so_snd.sb_mbmax -= 350ff8b0106SBrian Feldman so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt; 351ff8b0106SBrian Feldman unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt; 3526aef685fSBrian Feldman newhiwat = so->so_snd.sb_hiwat - 3536aef685fSBrian Feldman (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc); 354f535380cSDon Lewis (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat, 3556aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 356ff8b0106SBrian Feldman unp->unp_conn->unp_cc = so2->so_rcv.sb_cc; 357df8bae1dSRodney W. Grimes sorwakeup(so2); 358df8bae1dSRodney W. Grimes m = 0; 359df8bae1dSRodney W. Grimes break; 360df8bae1dSRodney W. Grimes 361df8bae1dSRodney W. Grimes default: 362a29f300eSGarrett Wollman panic("uipc_send unknown socktype"); 363df8bae1dSRodney W. Grimes } 364a29f300eSGarrett Wollman 3656b8fda4dSGarrett Wollman /* 3666b8fda4dSGarrett Wollman * SEND_EOF is equivalent to a SEND followed by 3676b8fda4dSGarrett Wollman * a SHUTDOWN. 3686b8fda4dSGarrett Wollman */ 369a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 3706b8fda4dSGarrett Wollman socantsendmore(so); 3716b8fda4dSGarrett Wollman unp_shutdown(unp); 3726b8fda4dSGarrett Wollman } 373df8bae1dSRodney W. Grimes 374bd508d39SDon Lewis if (control && error != 0) 375bd508d39SDon Lewis unp_dispose(control); 376bd508d39SDon Lewis 377a29f300eSGarrett Wollman release: 378a29f300eSGarrett Wollman if (control) 379a29f300eSGarrett Wollman m_freem(control); 380a29f300eSGarrett Wollman if (m) 381a29f300eSGarrett Wollman m_freem(m); 382a29f300eSGarrett Wollman return error; 383a29f300eSGarrett Wollman } 384df8bae1dSRodney W. Grimes 385a29f300eSGarrett Wollman static int 386a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 387a29f300eSGarrett Wollman { 388a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 389a29f300eSGarrett Wollman struct socket *so2; 390a29f300eSGarrett Wollman 391a29f300eSGarrett Wollman if (unp == 0) 392a29f300eSGarrett Wollman return EINVAL; 393a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 394df8bae1dSRodney W. Grimes if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) { 395df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 396a29f300eSGarrett Wollman sb->st_blksize += so2->so_rcv.sb_cc; 397df8bae1dSRodney W. Grimes } 398bfbb9ce6SPoul-Henning Kamp sb->st_dev = NOUDEV; 399df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 400df8bae1dSRodney W. Grimes unp->unp_ino = unp_ino++; 401a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 402df8bae1dSRodney W. Grimes return (0); 403a29f300eSGarrett Wollman } 404df8bae1dSRodney W. Grimes 405a29f300eSGarrett Wollman static int 406a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 407a29f300eSGarrett Wollman { 408a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 409df8bae1dSRodney W. Grimes 410a29f300eSGarrett Wollman if (unp == 0) 411a29f300eSGarrett Wollman return EINVAL; 412a29f300eSGarrett Wollman socantsendmore(so); 413a29f300eSGarrett Wollman unp_shutdown(unp); 414a29f300eSGarrett Wollman return 0; 415a29f300eSGarrett Wollman } 416df8bae1dSRodney W. Grimes 417a29f300eSGarrett Wollman static int 41857bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 419a29f300eSGarrett Wollman { 420a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 421a29f300eSGarrett Wollman 422a29f300eSGarrett Wollman if (unp == 0) 423a29f300eSGarrett Wollman return EINVAL; 42457bf258eSGarrett Wollman if (unp->unp_addr) 42557bf258eSGarrett Wollman *nam = dup_sockaddr((struct sockaddr *)unp->unp_addr, 1); 42683f3198bSThomas Moestl else 42783f3198bSThomas Moestl *nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1); 428a29f300eSGarrett Wollman return 0; 429df8bae1dSRodney W. Grimes } 430a29f300eSGarrett Wollman 431a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = { 432a29f300eSGarrett Wollman uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect, 433a29f300eSGarrett Wollman uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect, 434a29f300eSGarrett Wollman uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp, 435a29f300eSGarrett Wollman uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr, 43651338ea8SPeter Wemm sosend, soreceive, sopoll 437a29f300eSGarrett Wollman }; 438df8bae1dSRodney W. Grimes 4390c1bb4fbSDima Dorfman int 4400c1bb4fbSDima Dorfman uipc_ctloutput(so, sopt) 4410c1bb4fbSDima Dorfman struct socket *so; 4420c1bb4fbSDima Dorfman struct sockopt *sopt; 4430c1bb4fbSDima Dorfman { 4440c1bb4fbSDima Dorfman struct unpcb *unp = sotounpcb(so); 4450c1bb4fbSDima Dorfman int error; 4460c1bb4fbSDima Dorfman 4470c1bb4fbSDima Dorfman switch (sopt->sopt_dir) { 4480c1bb4fbSDima Dorfman case SOPT_GET: 4490c1bb4fbSDima Dorfman switch (sopt->sopt_name) { 4500c1bb4fbSDima Dorfman case LOCAL_PEERCRED: 4510c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC) 4520c1bb4fbSDima Dorfman error = sooptcopyout(sopt, &unp->unp_peercred, 4530c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 4540c1bb4fbSDima Dorfman else { 4550c1bb4fbSDima Dorfman if (so->so_type == SOCK_STREAM) 4560c1bb4fbSDima Dorfman error = ENOTCONN; 4570c1bb4fbSDima Dorfman else 4580c1bb4fbSDima Dorfman error = EINVAL; 4590c1bb4fbSDima Dorfman } 4600c1bb4fbSDima Dorfman break; 4610c1bb4fbSDima Dorfman default: 4620c1bb4fbSDima Dorfman error = EOPNOTSUPP; 4630c1bb4fbSDima Dorfman break; 4640c1bb4fbSDima Dorfman } 4650c1bb4fbSDima Dorfman break; 4660c1bb4fbSDima Dorfman case SOPT_SET: 4670c1bb4fbSDima Dorfman default: 4680c1bb4fbSDima Dorfman error = EOPNOTSUPP; 4690c1bb4fbSDima Dorfman break; 4700c1bb4fbSDima Dorfman } 4710c1bb4fbSDima Dorfman return (error); 4720c1bb4fbSDima Dorfman } 4730c1bb4fbSDima Dorfman 474df8bae1dSRodney W. Grimes /* 475df8bae1dSRodney W. Grimes * Both send and receive buffers are allocated PIPSIZ bytes of buffering 476df8bae1dSRodney W. Grimes * for stream sockets, although the total for sender and receiver is 477df8bae1dSRodney W. Grimes * actually only PIPSIZ. 478df8bae1dSRodney W. Grimes * Datagram sockets really use the sendspace as the maximum datagram size, 479df8bae1dSRodney W. Grimes * and don't really want to reserve the sendspace. Their recvspace should 480df8bae1dSRodney W. Grimes * be large enough for at least one max-size datagram plus address. 481df8bae1dSRodney W. Grimes */ 4825dce41c5SJohn Dyson #ifndef PIPSIZ 4835dce41c5SJohn Dyson #define PIPSIZ 8192 4845dce41c5SJohn Dyson #endif 485f708ef1bSPoul-Henning Kamp static u_long unpst_sendspace = PIPSIZ; 486f708ef1bSPoul-Henning Kamp static u_long unpst_recvspace = PIPSIZ; 487f708ef1bSPoul-Henning Kamp static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 488f708ef1bSPoul-Henning Kamp static u_long unpdg_recvspace = 4*1024; 489df8bae1dSRodney W. Grimes 490f708ef1bSPoul-Henning Kamp static int unp_rights; /* file descriptors in flight */ 491df8bae1dSRodney W. Grimes 492ce02431fSDoug Rabson SYSCTL_DECL(_net_local_stream); 493639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 494639acc13SGarrett Wollman &unpst_sendspace, 0, ""); 495639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 496639acc13SGarrett Wollman &unpst_recvspace, 0, ""); 497ce02431fSDoug Rabson SYSCTL_DECL(_net_local_dgram); 498639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 499639acc13SGarrett Wollman &unpdg_sendspace, 0, ""); 500639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 501639acc13SGarrett Wollman &unpdg_recvspace, 0, ""); 502ce02431fSDoug Rabson SYSCTL_DECL(_net_local); 503639acc13SGarrett Wollman SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); 504639acc13SGarrett Wollman 505f708ef1bSPoul-Henning Kamp static int 506df8bae1dSRodney W. Grimes unp_attach(so) 507df8bae1dSRodney W. Grimes struct socket *so; 508df8bae1dSRodney W. Grimes { 509df8bae1dSRodney W. Grimes register struct unpcb *unp; 510df8bae1dSRodney W. Grimes int error; 511df8bae1dSRodney W. Grimes 512df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 513df8bae1dSRodney W. Grimes switch (so->so_type) { 514df8bae1dSRodney W. Grimes 515df8bae1dSRodney W. Grimes case SOCK_STREAM: 516df8bae1dSRodney W. Grimes error = soreserve(so, unpst_sendspace, unpst_recvspace); 517df8bae1dSRodney W. Grimes break; 518df8bae1dSRodney W. Grimes 519df8bae1dSRodney W. Grimes case SOCK_DGRAM: 520df8bae1dSRodney W. Grimes error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 521df8bae1dSRodney W. Grimes break; 522df8bae1dSRodney W. Grimes 523df8bae1dSRodney W. Grimes default: 524df8bae1dSRodney W. Grimes panic("unp_attach"); 525df8bae1dSRodney W. Grimes } 526df8bae1dSRodney W. Grimes if (error) 527df8bae1dSRodney W. Grimes return (error); 528df8bae1dSRodney W. Grimes } 52998271db4SGarrett Wollman unp = zalloc(unp_zone); 53057bf258eSGarrett Wollman if (unp == NULL) 531df8bae1dSRodney W. Grimes return (ENOBUFS); 53257bf258eSGarrett Wollman bzero(unp, sizeof *unp); 53398271db4SGarrett Wollman unp->unp_gencnt = ++unp_gencnt; 53498271db4SGarrett Wollman unp_count++; 53598271db4SGarrett Wollman LIST_INIT(&unp->unp_refs); 536df8bae1dSRodney W. Grimes unp->unp_socket = so; 53775c13541SPoul-Henning Kamp unp->unp_rvnode = curproc->p_fd->fd_rdir; 53898271db4SGarrett Wollman LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead 53998271db4SGarrett Wollman : &unp_shead, unp, unp_link); 54098271db4SGarrett Wollman so->so_pcb = (caddr_t)unp; 541df8bae1dSRodney W. Grimes return (0); 542df8bae1dSRodney W. Grimes } 543df8bae1dSRodney W. Grimes 544f708ef1bSPoul-Henning Kamp static void 545df8bae1dSRodney W. Grimes unp_detach(unp) 546df8bae1dSRodney W. Grimes register struct unpcb *unp; 547df8bae1dSRodney W. Grimes { 54898271db4SGarrett Wollman LIST_REMOVE(unp, unp_link); 54998271db4SGarrett Wollman unp->unp_gencnt = ++unp_gencnt; 55098271db4SGarrett Wollman --unp_count; 551df8bae1dSRodney W. Grimes if (unp->unp_vnode) { 552df8bae1dSRodney W. Grimes unp->unp_vnode->v_socket = 0; 553df8bae1dSRodney W. Grimes vrele(unp->unp_vnode); 554df8bae1dSRodney W. Grimes unp->unp_vnode = 0; 555df8bae1dSRodney W. Grimes } 556df8bae1dSRodney W. Grimes if (unp->unp_conn) 557df8bae1dSRodney W. Grimes unp_disconnect(unp); 5582e3c8fcbSPoul-Henning Kamp while (!LIST_EMPTY(&unp->unp_refs)) 5592e3c8fcbSPoul-Henning Kamp unp_drop(LIST_FIRST(&unp->unp_refs), ECONNRESET); 560df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 561df8bae1dSRodney W. Grimes unp->unp_socket->so_pcb = 0; 562df8bae1dSRodney W. Grimes if (unp_rights) { 563df8bae1dSRodney W. Grimes /* 564df8bae1dSRodney W. Grimes * Normally the receive buffer is flushed later, 565df8bae1dSRodney W. Grimes * in sofree, but if our receive buffer holds references 566df8bae1dSRodney W. Grimes * to descriptors that are now garbage, we will dispose 567df8bae1dSRodney W. Grimes * of those descriptor references after the garbage collector 568df8bae1dSRodney W. Grimes * gets them (resulting in a "panic: closef: count < 0"). 569df8bae1dSRodney W. Grimes */ 570df8bae1dSRodney W. Grimes sorflush(unp->unp_socket); 571df8bae1dSRodney W. Grimes unp_gc(); 572df8bae1dSRodney W. Grimes } 57357bf258eSGarrett Wollman if (unp->unp_addr) 57457bf258eSGarrett Wollman FREE(unp->unp_addr, M_SONAME); 57598271db4SGarrett Wollman zfree(unp_zone, unp); 576df8bae1dSRodney W. Grimes } 577df8bae1dSRodney W. Grimes 578f708ef1bSPoul-Henning Kamp static int 579df8bae1dSRodney W. Grimes unp_bind(unp, nam, p) 580df8bae1dSRodney W. Grimes struct unpcb *unp; 58157bf258eSGarrett Wollman struct sockaddr *nam; 582df8bae1dSRodney W. Grimes struct proc *p; 583df8bae1dSRodney W. Grimes { 58457bf258eSGarrett Wollman struct sockaddr_un *soun = (struct sockaddr_un *)nam; 585f2a2857bSKirk McKusick struct vnode *vp; 586f2a2857bSKirk McKusick struct mount *mp; 587df8bae1dSRodney W. Grimes struct vattr vattr; 58857bf258eSGarrett Wollman int error, namelen; 589df8bae1dSRodney W. Grimes struct nameidata nd; 5908f364875SJulian Elischer char *buf; 591df8bae1dSRodney W. Grimes 592df8bae1dSRodney W. Grimes if (unp->unp_vnode != NULL) 593df8bae1dSRodney W. Grimes return (EINVAL); 59457bf258eSGarrett Wollman namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 59557bf258eSGarrett Wollman if (namelen <= 0) 59657bf258eSGarrett Wollman return EINVAL; 5978f364875SJulian Elischer buf = malloc(SOCK_MAXADDRLEN, M_TEMP, M_WAITOK); 59857bf258eSGarrett Wollman strncpy(buf, soun->sun_path, namelen); 59957bf258eSGarrett Wollman buf[namelen] = 0; /* null-terminate the string */ 600f2a2857bSKirk McKusick restart: 601974784e8SGuido van Rooij NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT, UIO_SYSSPACE, 60257bf258eSGarrett Wollman buf, p); 603df8bae1dSRodney W. Grimes /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 604797f2d22SPoul-Henning Kamp error = namei(&nd); 6058f364875SJulian Elischer if (error) { 606df8bae1dSRodney W. Grimes return (error); 6078f364875SJulian Elischer free(buf, M_TEMP); 6088f364875SJulian Elischer } 609df8bae1dSRodney W. Grimes vp = nd.ni_vp; 610f2a2857bSKirk McKusick if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 611762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 612df8bae1dSRodney W. Grimes if (nd.ni_dvp == vp) 613df8bae1dSRodney W. Grimes vrele(nd.ni_dvp); 614df8bae1dSRodney W. Grimes else 615df8bae1dSRodney W. Grimes vput(nd.ni_dvp); 616f2a2857bSKirk McKusick if (vp != NULL) { 617df8bae1dSRodney W. Grimes vrele(vp); 6188f364875SJulian Elischer free(buf, M_TEMP); 619df8bae1dSRodney W. Grimes return (EADDRINUSE); 620df8bae1dSRodney W. Grimes } 6218f364875SJulian Elischer error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 6228f364875SJulian Elischer if (error) { 6238f364875SJulian Elischer free(buf, M_TEMP); 624f2a2857bSKirk McKusick return (error); 6258f364875SJulian Elischer } 626f2a2857bSKirk McKusick goto restart; 627f2a2857bSKirk McKusick } 628df8bae1dSRodney W. Grimes VATTR_NULL(&vattr); 629df8bae1dSRodney W. Grimes vattr.va_type = VSOCK; 630a29f300eSGarrett Wollman vattr.va_mode = (ACCESSPERMS & ~p->p_fd->fd_cmask); 631996c772fSJohn Dyson VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); 6327be2d300SMike Smith error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 633762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 6347be2d300SMike Smith vput(nd.ni_dvp); 6358f364875SJulian Elischer if (error) { 6368f364875SJulian Elischer free(buf, M_TEMP); 637df8bae1dSRodney W. Grimes return (error); 6388f364875SJulian Elischer } 639df8bae1dSRodney W. Grimes vp = nd.ni_vp; 640df8bae1dSRodney W. Grimes vp->v_socket = unp->unp_socket; 641df8bae1dSRodney W. Grimes unp->unp_vnode = vp; 64257bf258eSGarrett Wollman unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam, 1); 643996c772fSJohn Dyson VOP_UNLOCK(vp, 0, p); 644f2a2857bSKirk McKusick vn_finished_write(mp); 6458f364875SJulian Elischer free(buf, M_TEMP); 646df8bae1dSRodney W. Grimes return (0); 647df8bae1dSRodney W. Grimes } 648df8bae1dSRodney W. Grimes 649f708ef1bSPoul-Henning Kamp static int 650df8bae1dSRodney W. Grimes unp_connect(so, nam, p) 651df8bae1dSRodney W. Grimes struct socket *so; 65257bf258eSGarrett Wollman struct sockaddr *nam; 653df8bae1dSRodney W. Grimes struct proc *p; 654df8bae1dSRodney W. Grimes { 65557bf258eSGarrett Wollman register struct sockaddr_un *soun = (struct sockaddr_un *)nam; 656df8bae1dSRodney W. Grimes register struct vnode *vp; 657df8bae1dSRodney W. Grimes register struct socket *so2, *so3; 6580c1bb4fbSDima Dorfman struct unpcb *unp, *unp2, *unp3; 65957bf258eSGarrett Wollman int error, len; 660df8bae1dSRodney W. Grimes struct nameidata nd; 66157bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 662df8bae1dSRodney W. Grimes 66357bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 66457bf258eSGarrett Wollman if (len <= 0) 66557bf258eSGarrett Wollman return EINVAL; 66657bf258eSGarrett Wollman strncpy(buf, soun->sun_path, len); 66757bf258eSGarrett Wollman buf[len] = 0; 66857bf258eSGarrett Wollman 66957bf258eSGarrett Wollman NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, p); 670797f2d22SPoul-Henning Kamp error = namei(&nd); 671797f2d22SPoul-Henning Kamp if (error) 672df8bae1dSRodney W. Grimes return (error); 673df8bae1dSRodney W. Grimes vp = nd.ni_vp; 674762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 675df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 676df8bae1dSRodney W. Grimes error = ENOTSOCK; 677df8bae1dSRodney W. Grimes goto bad; 678df8bae1dSRodney W. Grimes } 679797f2d22SPoul-Henning Kamp error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p); 680797f2d22SPoul-Henning Kamp if (error) 681df8bae1dSRodney W. Grimes goto bad; 682df8bae1dSRodney W. Grimes so2 = vp->v_socket; 683df8bae1dSRodney W. Grimes if (so2 == 0) { 684df8bae1dSRodney W. Grimes error = ECONNREFUSED; 685df8bae1dSRodney W. Grimes goto bad; 686df8bae1dSRodney W. Grimes } 687df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 688df8bae1dSRodney W. Grimes error = EPROTOTYPE; 689df8bae1dSRodney W. Grimes goto bad; 690df8bae1dSRodney W. Grimes } 691df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 692df8bae1dSRodney W. Grimes if ((so2->so_options & SO_ACCEPTCONN) == 0 || 6932f9a2132SBrian Feldman (so3 = sonewconn3(so2, 0, p)) == 0) { 694df8bae1dSRodney W. Grimes error = ECONNREFUSED; 695df8bae1dSRodney W. Grimes goto bad; 696df8bae1dSRodney W. Grimes } 6970c1bb4fbSDima Dorfman unp = sotounpcb(so); 698df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 699df8bae1dSRodney W. Grimes unp3 = sotounpcb(so3); 700df8bae1dSRodney W. Grimes if (unp2->unp_addr) 70157bf258eSGarrett Wollman unp3->unp_addr = (struct sockaddr_un *) 70257bf258eSGarrett Wollman dup_sockaddr((struct sockaddr *) 70357bf258eSGarrett Wollman unp2->unp_addr, 1); 7040c1bb4fbSDima Dorfman 7050c1bb4fbSDima Dorfman /* 7060c1bb4fbSDima Dorfman * unp_peercred management: 7070c1bb4fbSDima Dorfman * 7080c1bb4fbSDima Dorfman * The connecter's (client's) credentials are copied 7090c1bb4fbSDima Dorfman * from its process structure at the time of connect() 7100c1bb4fbSDima Dorfman * (which is now). 7110c1bb4fbSDima Dorfman */ 7120c1bb4fbSDima Dorfman memset(&unp3->unp_peercred, '\0', sizeof(unp3->unp_peercred)); 7130c1bb4fbSDima Dorfman unp3->unp_peercred.cr_uid = p->p_ucred->cr_uid; 7140c1bb4fbSDima Dorfman unp3->unp_peercred.cr_ngroups = p->p_ucred->cr_ngroups; 7150c1bb4fbSDima Dorfman memcpy(unp3->unp_peercred.cr_groups, p->p_ucred->cr_groups, 7160c1bb4fbSDima Dorfman sizeof(unp3->unp_peercred.cr_groups)); 7170c1bb4fbSDima Dorfman unp3->unp_flags |= UNP_HAVEPC; 7180c1bb4fbSDima Dorfman /* 7190c1bb4fbSDima Dorfman * The receiver's (server's) credentials are copied 7200c1bb4fbSDima Dorfman * from the unp_peercred member of socket on which the 7210c1bb4fbSDima Dorfman * former called listen(); unp_listen() cached that 7220c1bb4fbSDima Dorfman * process's credentials at that time so we can use 7230c1bb4fbSDima Dorfman * them now. 7240c1bb4fbSDima Dorfman */ 7250c1bb4fbSDima Dorfman KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 7260c1bb4fbSDima Dorfman ("unp_connect: listener without cached peercred")); 7270c1bb4fbSDima Dorfman memcpy(&unp->unp_peercred, &unp2->unp_peercred, 7280c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 7290c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPC; 7300c1bb4fbSDima Dorfman 731df8bae1dSRodney W. Grimes so2 = so3; 732df8bae1dSRodney W. Grimes } 733df8bae1dSRodney W. Grimes error = unp_connect2(so, so2); 734df8bae1dSRodney W. Grimes bad: 735df8bae1dSRodney W. Grimes vput(vp); 736df8bae1dSRodney W. Grimes return (error); 737df8bae1dSRodney W. Grimes } 738df8bae1dSRodney W. Grimes 73926f9a767SRodney W. Grimes int 740df8bae1dSRodney W. Grimes unp_connect2(so, so2) 741df8bae1dSRodney W. Grimes register struct socket *so; 742df8bae1dSRodney W. Grimes register struct socket *so2; 743df8bae1dSRodney W. Grimes { 744df8bae1dSRodney W. Grimes register struct unpcb *unp = sotounpcb(so); 745df8bae1dSRodney W. Grimes register struct unpcb *unp2; 746df8bae1dSRodney W. Grimes 747df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 748df8bae1dSRodney W. Grimes return (EPROTOTYPE); 749df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 750df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 751df8bae1dSRodney W. Grimes switch (so->so_type) { 752df8bae1dSRodney W. Grimes 753df8bae1dSRodney W. Grimes case SOCK_DGRAM: 75498271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 755df8bae1dSRodney W. Grimes soisconnected(so); 756df8bae1dSRodney W. Grimes break; 757df8bae1dSRodney W. Grimes 758df8bae1dSRodney W. Grimes case SOCK_STREAM: 759df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 760df8bae1dSRodney W. Grimes soisconnected(so); 761df8bae1dSRodney W. Grimes soisconnected(so2); 762df8bae1dSRodney W. Grimes break; 763df8bae1dSRodney W. Grimes 764df8bae1dSRodney W. Grimes default: 765df8bae1dSRodney W. Grimes panic("unp_connect2"); 766df8bae1dSRodney W. Grimes } 767df8bae1dSRodney W. Grimes return (0); 768df8bae1dSRodney W. Grimes } 769df8bae1dSRodney W. Grimes 770f708ef1bSPoul-Henning Kamp static void 771df8bae1dSRodney W. Grimes unp_disconnect(unp) 772df8bae1dSRodney W. Grimes struct unpcb *unp; 773df8bae1dSRodney W. Grimes { 774df8bae1dSRodney W. Grimes register struct unpcb *unp2 = unp->unp_conn; 775df8bae1dSRodney W. Grimes 776df8bae1dSRodney W. Grimes if (unp2 == 0) 777df8bae1dSRodney W. Grimes return; 778df8bae1dSRodney W. Grimes unp->unp_conn = 0; 779df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 780df8bae1dSRodney W. Grimes 781df8bae1dSRodney W. Grimes case SOCK_DGRAM: 78298271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 783df8bae1dSRodney W. Grimes unp->unp_socket->so_state &= ~SS_ISCONNECTED; 784df8bae1dSRodney W. Grimes break; 785df8bae1dSRodney W. Grimes 786df8bae1dSRodney W. Grimes case SOCK_STREAM: 787df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 788df8bae1dSRodney W. Grimes unp2->unp_conn = 0; 789df8bae1dSRodney W. Grimes soisdisconnected(unp2->unp_socket); 790df8bae1dSRodney W. Grimes break; 791df8bae1dSRodney W. Grimes } 792df8bae1dSRodney W. Grimes } 793df8bae1dSRodney W. Grimes 794df8bae1dSRodney W. Grimes #ifdef notdef 79526f9a767SRodney W. Grimes void 796df8bae1dSRodney W. Grimes unp_abort(unp) 797df8bae1dSRodney W. Grimes struct unpcb *unp; 798df8bae1dSRodney W. Grimes { 799df8bae1dSRodney W. Grimes 800df8bae1dSRodney W. Grimes unp_detach(unp); 801df8bae1dSRodney W. Grimes } 802df8bae1dSRodney W. Grimes #endif 803df8bae1dSRodney W. Grimes 80498271db4SGarrett Wollman static int 80575c13541SPoul-Henning Kamp prison_unpcb(struct proc *p, struct unpcb *unp) 80675c13541SPoul-Henning Kamp { 80791421ba2SRobert Watson if (!jailed(p->p_ucred)) 80875c13541SPoul-Henning Kamp return (0); 80975c13541SPoul-Henning Kamp if (p->p_fd->fd_rdir == unp->unp_rvnode) 81075c13541SPoul-Henning Kamp return (0); 81175c13541SPoul-Henning Kamp return (1); 81275c13541SPoul-Henning Kamp } 81375c13541SPoul-Henning Kamp 81475c13541SPoul-Henning Kamp static int 81582d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 81698271db4SGarrett Wollman { 817f5ef029eSPoul-Henning Kamp int error, i, n; 81898271db4SGarrett Wollman struct unpcb *unp, **unp_list; 81998271db4SGarrett Wollman unp_gen_t gencnt; 8208f364875SJulian Elischer struct xunpgen *xug; 82198271db4SGarrett Wollman struct unp_head *head; 8228f364875SJulian Elischer struct xunpcb *xu; 82398271db4SGarrett Wollman 824a23d65bfSBruce Evans head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 82598271db4SGarrett Wollman 82698271db4SGarrett Wollman /* 82798271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 82898271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 82998271db4SGarrett Wollman */ 83098271db4SGarrett Wollman if (req->oldptr == 0) { 83198271db4SGarrett Wollman n = unp_count; 8328f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug) 83398271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 83498271db4SGarrett Wollman return 0; 83598271db4SGarrett Wollman } 83698271db4SGarrett Wollman 83798271db4SGarrett Wollman if (req->newptr != 0) 83898271db4SGarrett Wollman return EPERM; 83998271db4SGarrett Wollman 84098271db4SGarrett Wollman /* 84198271db4SGarrett Wollman * OK, now we're committed to doing something. 84298271db4SGarrett Wollman */ 8438f364875SJulian Elischer xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 84498271db4SGarrett Wollman gencnt = unp_gencnt; 84598271db4SGarrett Wollman n = unp_count; 84698271db4SGarrett Wollman 8478f364875SJulian Elischer xug->xug_len = sizeof *xug; 8488f364875SJulian Elischer xug->xug_count = n; 8498f364875SJulian Elischer xug->xug_gen = gencnt; 8508f364875SJulian Elischer xug->xug_sogen = so_gencnt; 8518f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 8528f364875SJulian Elischer if (error) { 8538f364875SJulian Elischer free(xug, M_TEMP); 85498271db4SGarrett Wollman return error; 8558f364875SJulian Elischer } 85698271db4SGarrett Wollman 85798271db4SGarrett Wollman unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 85898271db4SGarrett Wollman if (unp_list == 0) 85998271db4SGarrett Wollman return ENOMEM; 86098271db4SGarrett Wollman 8612e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 8622e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 86375c13541SPoul-Henning Kamp if (unp->unp_gencnt <= gencnt && !prison_unpcb(req->p, unp)) 86498271db4SGarrett Wollman unp_list[i++] = unp; 86598271db4SGarrett Wollman } 86698271db4SGarrett Wollman n = i; /* in case we lost some during malloc */ 86798271db4SGarrett Wollman 86898271db4SGarrett Wollman error = 0; 8698f364875SJulian Elischer xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK); 87098271db4SGarrett Wollman for (i = 0; i < n; i++) { 87198271db4SGarrett Wollman unp = unp_list[i]; 87298271db4SGarrett Wollman if (unp->unp_gencnt <= gencnt) { 8738f364875SJulian Elischer xu->xu_len = sizeof *xu; 8748f364875SJulian Elischer xu->xu_unpp = unp; 87598271db4SGarrett Wollman /* 87698271db4SGarrett Wollman * XXX - need more locking here to protect against 87798271db4SGarrett Wollman * connect/disconnect races for SMP. 87898271db4SGarrett Wollman */ 87998271db4SGarrett Wollman if (unp->unp_addr) 8808f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr, 88198271db4SGarrett Wollman unp->unp_addr->sun_len); 88298271db4SGarrett Wollman if (unp->unp_conn && unp->unp_conn->unp_addr) 88398271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 8848f364875SJulian Elischer &xu->xu_caddr, 88598271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 8868f364875SJulian Elischer bcopy(unp, &xu->xu_unp, sizeof *unp); 8878f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket); 8888f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu); 88998271db4SGarrett Wollman } 89098271db4SGarrett Wollman } 8918f364875SJulian Elischer free(xu, M_TEMP); 89298271db4SGarrett Wollman if (!error) { 89398271db4SGarrett Wollman /* 89498271db4SGarrett Wollman * Give the user an updated idea of our state. 89598271db4SGarrett Wollman * If the generation differs from what we told 89698271db4SGarrett Wollman * her before, she knows that something happened 89798271db4SGarrett Wollman * while we were processing this request, and it 89898271db4SGarrett Wollman * might be necessary to retry. 89998271db4SGarrett Wollman */ 9008f364875SJulian Elischer xug->xug_gen = unp_gencnt; 9018f364875SJulian Elischer xug->xug_sogen = so_gencnt; 9028f364875SJulian Elischer xug->xug_count = unp_count; 9038f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 90498271db4SGarrett Wollman } 90598271db4SGarrett Wollman free(unp_list, M_TEMP); 9068f364875SJulian Elischer free(xug, M_TEMP); 90798271db4SGarrett Wollman return error; 90898271db4SGarrett Wollman } 90998271db4SGarrett Wollman 91098271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 91198271db4SGarrett Wollman (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 91298271db4SGarrett Wollman "List of active local datagram sockets"); 91398271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 91498271db4SGarrett Wollman (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 91598271db4SGarrett Wollman "List of active local stream sockets"); 91698271db4SGarrett Wollman 917f708ef1bSPoul-Henning Kamp static void 918df8bae1dSRodney W. Grimes unp_shutdown(unp) 919df8bae1dSRodney W. Grimes struct unpcb *unp; 920df8bae1dSRodney W. Grimes { 921df8bae1dSRodney W. Grimes struct socket *so; 922df8bae1dSRodney W. Grimes 923df8bae1dSRodney W. Grimes if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 924df8bae1dSRodney W. Grimes (so = unp->unp_conn->unp_socket)) 925df8bae1dSRodney W. Grimes socantrcvmore(so); 926df8bae1dSRodney W. Grimes } 927df8bae1dSRodney W. Grimes 928f708ef1bSPoul-Henning Kamp static void 929df8bae1dSRodney W. Grimes unp_drop(unp, errno) 930df8bae1dSRodney W. Grimes struct unpcb *unp; 931df8bae1dSRodney W. Grimes int errno; 932df8bae1dSRodney W. Grimes { 933df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 934df8bae1dSRodney W. Grimes 935df8bae1dSRodney W. Grimes so->so_error = errno; 936df8bae1dSRodney W. Grimes unp_disconnect(unp); 937df8bae1dSRodney W. Grimes if (so->so_head) { 93898271db4SGarrett Wollman LIST_REMOVE(unp, unp_link); 93998271db4SGarrett Wollman unp->unp_gencnt = ++unp_gencnt; 94098271db4SGarrett Wollman unp_count--; 941df8bae1dSRodney W. Grimes so->so_pcb = (caddr_t) 0; 94257bf258eSGarrett Wollman if (unp->unp_addr) 94357bf258eSGarrett Wollman FREE(unp->unp_addr, M_SONAME); 94498271db4SGarrett Wollman zfree(unp_zone, unp); 945df8bae1dSRodney W. Grimes sofree(so); 946df8bae1dSRodney W. Grimes } 947df8bae1dSRodney W. Grimes } 948df8bae1dSRodney W. Grimes 949df8bae1dSRodney W. Grimes #ifdef notdef 95026f9a767SRodney W. Grimes void 951df8bae1dSRodney W. Grimes unp_drain() 952df8bae1dSRodney W. Grimes { 953df8bae1dSRodney W. Grimes 954df8bae1dSRodney W. Grimes } 955df8bae1dSRodney W. Grimes #endif 956df8bae1dSRodney W. Grimes 95726f9a767SRodney W. Grimes int 958df8bae1dSRodney W. Grimes unp_externalize(rights) 959df8bae1dSRodney W. Grimes struct mbuf *rights; 960df8bae1dSRodney W. Grimes { 961df8bae1dSRodney W. Grimes struct proc *p = curproc; /* XXX */ 962df8bae1dSRodney W. Grimes register int i; 963df8bae1dSRodney W. Grimes register struct cmsghdr *cm = mtod(rights, struct cmsghdr *); 9648692c025SYoshinobu Inoue register int *fdp; 9658692c025SYoshinobu Inoue register struct file **rp; 966df8bae1dSRodney W. Grimes register struct file *fp; 9678692c025SYoshinobu Inoue int newfds = (cm->cmsg_len - (CMSG_DATA(cm) - (u_char *)cm)) 9688692c025SYoshinobu Inoue / sizeof (struct file *); 969df8bae1dSRodney W. Grimes int f; 970df8bae1dSRodney W. Grimes 971ed5b7817SJulian Elischer /* 972ed5b7817SJulian Elischer * if the new FD's will not fit, then we free them all 973ed5b7817SJulian Elischer */ 974df8bae1dSRodney W. Grimes if (!fdavail(p, newfds)) { 9758692c025SYoshinobu Inoue rp = (struct file **)CMSG_DATA(cm); 976df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 977df8bae1dSRodney W. Grimes fp = *rp; 9788692c025SYoshinobu Inoue /* 9798692c025SYoshinobu Inoue * zero the pointer before calling unp_discard, 9808692c025SYoshinobu Inoue * since it may end up in unp_gc().. 9818692c025SYoshinobu Inoue */ 982df8bae1dSRodney W. Grimes *rp++ = 0; 9838692c025SYoshinobu Inoue unp_discard(fp); 984df8bae1dSRodney W. Grimes } 985df8bae1dSRodney W. Grimes return (EMSGSIZE); 986df8bae1dSRodney W. Grimes } 987ed5b7817SJulian Elischer /* 988ed5b7817SJulian Elischer * now change each pointer to an fd in the global table to 989ed5b7817SJulian Elischer * an integer that is the index to the local fd table entry 990ed5b7817SJulian Elischer * that we set up to point to the global one we are transferring. 9918692c025SYoshinobu Inoue * If sizeof (struct file *) is bigger than or equal to sizeof int, 9928692c025SYoshinobu Inoue * then do it in forward order. In that case, an integer will 9938692c025SYoshinobu Inoue * always come in the same place or before its corresponding 9948692c025SYoshinobu Inoue * struct file pointer. 9958692c025SYoshinobu Inoue * If sizeof (struct file *) is smaller than sizeof int, then 9968692c025SYoshinobu Inoue * do it in reverse order. 997ed5b7817SJulian Elischer */ 9988692c025SYoshinobu Inoue if (sizeof (struct file *) >= sizeof (int)) { 9998692c025SYoshinobu Inoue fdp = (int *)(cm + 1); 10008692c025SYoshinobu Inoue rp = (struct file **)CMSG_DATA(cm); 1001df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 1002df8bae1dSRodney W. Grimes if (fdalloc(p, 0, &f)) 1003df8bae1dSRodney W. Grimes panic("unp_externalize"); 10048692c025SYoshinobu Inoue fp = *rp++; 1005df8bae1dSRodney W. Grimes p->p_fd->fd_ofiles[f] = fp; 1006df8bae1dSRodney W. Grimes fp->f_msgcount--; 1007df8bae1dSRodney W. Grimes unp_rights--; 10088692c025SYoshinobu Inoue *fdp++ = f; 1009df8bae1dSRodney W. Grimes } 10108692c025SYoshinobu Inoue } else { 10118692c025SYoshinobu Inoue fdp = (int *)(cm + 1) + newfds - 1; 10128692c025SYoshinobu Inoue rp = (struct file **)CMSG_DATA(cm) + newfds - 1; 10138692c025SYoshinobu Inoue for (i = 0; i < newfds; i++) { 10148692c025SYoshinobu Inoue if (fdalloc(p, 0, &f)) 10158692c025SYoshinobu Inoue panic("unp_externalize"); 10168692c025SYoshinobu Inoue fp = *rp--; 10178692c025SYoshinobu Inoue p->p_fd->fd_ofiles[f] = fp; 10188692c025SYoshinobu Inoue fp->f_msgcount--; 10198692c025SYoshinobu Inoue unp_rights--; 10208692c025SYoshinobu Inoue *fdp-- = f; 10218692c025SYoshinobu Inoue } 10228692c025SYoshinobu Inoue } 10238692c025SYoshinobu Inoue 10248692c025SYoshinobu Inoue /* 10258692c025SYoshinobu Inoue * Adjust length, in case sizeof(struct file *) and sizeof(int) 10268692c025SYoshinobu Inoue * differs. 10278692c025SYoshinobu Inoue */ 10288692c025SYoshinobu Inoue cm->cmsg_len = CMSG_LEN(newfds * sizeof(int)); 10298692c025SYoshinobu Inoue rights->m_len = cm->cmsg_len; 1030df8bae1dSRodney W. Grimes return (0); 1031df8bae1dSRodney W. Grimes } 1032df8bae1dSRodney W. Grimes 103398271db4SGarrett Wollman void 103498271db4SGarrett Wollman unp_init(void) 103598271db4SGarrett Wollman { 103698271db4SGarrett Wollman unp_zone = zinit("unpcb", sizeof(struct unpcb), nmbclusters, 0, 0); 103798271db4SGarrett Wollman if (unp_zone == 0) 103898271db4SGarrett Wollman panic("unp_init"); 103998271db4SGarrett Wollman LIST_INIT(&unp_dhead); 104098271db4SGarrett Wollman LIST_INIT(&unp_shead); 104198271db4SGarrett Wollman } 104298271db4SGarrett Wollman 10430b788fa1SBill Paul #ifndef MIN 10440b788fa1SBill Paul #define MIN(a,b) (((a)<(b))?(a):(b)) 10450b788fa1SBill Paul #endif 10460b788fa1SBill Paul 1047f708ef1bSPoul-Henning Kamp static int 1048df8bae1dSRodney W. Grimes unp_internalize(control, p) 1049df8bae1dSRodney W. Grimes struct mbuf *control; 1050df8bae1dSRodney W. Grimes struct proc *p; 1051df8bae1dSRodney W. Grimes { 10528692c025SYoshinobu Inoue struct filedesc *fdescp = p->p_fd; 1053df8bae1dSRodney W. Grimes register struct cmsghdr *cm = mtod(control, struct cmsghdr *); 1054df8bae1dSRodney W. Grimes register struct file **rp; 1055df8bae1dSRodney W. Grimes register struct file *fp; 10568692c025SYoshinobu Inoue register int i, fd, *fdp; 10570b788fa1SBill Paul register struct cmsgcred *cmcred; 1058df8bae1dSRodney W. Grimes int oldfds; 10598692c025SYoshinobu Inoue u_int newlen; 1060df8bae1dSRodney W. Grimes 10610b788fa1SBill Paul if ((cm->cmsg_type != SCM_RIGHTS && cm->cmsg_type != SCM_CREDS) || 10620b788fa1SBill Paul cm->cmsg_level != SOL_SOCKET || cm->cmsg_len != control->m_len) 1063df8bae1dSRodney W. Grimes return (EINVAL); 10640b788fa1SBill Paul 10650b788fa1SBill Paul /* 10660b788fa1SBill Paul * Fill in credential information. 10670b788fa1SBill Paul */ 10680b788fa1SBill Paul if (cm->cmsg_type == SCM_CREDS) { 10690b788fa1SBill Paul cmcred = (struct cmsgcred *)(cm + 1); 10700b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 1071b1fc0ec1SRobert Watson cmcred->cmcred_uid = p->p_ucred->cr_ruid; 1072b1fc0ec1SRobert Watson cmcred->cmcred_gid = p->p_ucred->cr_rgid; 10730b788fa1SBill Paul cmcred->cmcred_euid = p->p_ucred->cr_uid; 10740b788fa1SBill Paul cmcred->cmcred_ngroups = MIN(p->p_ucred->cr_ngroups, 10750b788fa1SBill Paul CMGROUP_MAX); 10760b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 10770b788fa1SBill Paul cmcred->cmcred_groups[i] = p->p_ucred->cr_groups[i]; 10780b788fa1SBill Paul return(0); 10790b788fa1SBill Paul } 10800b788fa1SBill Paul 1081df8bae1dSRodney W. Grimes oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int); 1082ed5b7817SJulian Elischer /* 1083ed5b7817SJulian Elischer * check that all the FDs passed in refer to legal OPEN files 1084ed5b7817SJulian Elischer * If not, reject the entire operation. 1085ed5b7817SJulian Elischer */ 10868692c025SYoshinobu Inoue fdp = (int *)(cm + 1); 1087df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 10888692c025SYoshinobu Inoue fd = *fdp++; 10898692c025SYoshinobu Inoue if ((unsigned)fd >= fdescp->fd_nfiles || 10908692c025SYoshinobu Inoue fdescp->fd_ofiles[fd] == NULL) 1091df8bae1dSRodney W. Grimes return (EBADF); 1092df8bae1dSRodney W. Grimes } 1093ed5b7817SJulian Elischer /* 1094ed5b7817SJulian Elischer * Now replace the integer FDs with pointers to 1095ed5b7817SJulian Elischer * the associated global file table entry.. 10968692c025SYoshinobu Inoue * Allocate a bigger buffer as necessary. But if an cluster is not 10978692c025SYoshinobu Inoue * enough, return E2BIG. 1098ed5b7817SJulian Elischer */ 10998692c025SYoshinobu Inoue newlen = CMSG_LEN(oldfds * sizeof(struct file *)); 11008692c025SYoshinobu Inoue if (newlen > MCLBYTES) 11018692c025SYoshinobu Inoue return (E2BIG); 11028692c025SYoshinobu Inoue if (newlen - control->m_len > M_TRAILINGSPACE(control)) { 11038692c025SYoshinobu Inoue if (control->m_flags & M_EXT) 11048692c025SYoshinobu Inoue return (E2BIG); 11052a0c503eSBosko Milekic MCLGET(control, M_TRYWAIT); 11068692c025SYoshinobu Inoue if ((control->m_flags & M_EXT) == 0) 11078692c025SYoshinobu Inoue return (ENOBUFS); 11088692c025SYoshinobu Inoue 11098692c025SYoshinobu Inoue /* copy the data to the cluster */ 11108692c025SYoshinobu Inoue memcpy(mtod(control, char *), cm, cm->cmsg_len); 11118692c025SYoshinobu Inoue cm = mtod(control, struct cmsghdr *); 11128692c025SYoshinobu Inoue } 11138692c025SYoshinobu Inoue 11148692c025SYoshinobu Inoue /* 11158692c025SYoshinobu Inoue * Adjust length, in case sizeof(struct file *) and sizeof(int) 11168692c025SYoshinobu Inoue * differs. 11178692c025SYoshinobu Inoue */ 11188692c025SYoshinobu Inoue control->m_len = cm->cmsg_len = newlen; 11198692c025SYoshinobu Inoue 11208692c025SYoshinobu Inoue /* 11218692c025SYoshinobu Inoue * Transform the file descriptors into struct file pointers. 11228692c025SYoshinobu Inoue * If sizeof (struct file *) is bigger than or equal to sizeof int, 11238692c025SYoshinobu Inoue * then do it in reverse order so that the int won't get until 11248692c025SYoshinobu Inoue * we're done. 11258692c025SYoshinobu Inoue * If sizeof (struct file *) is smaller than sizeof int, then 11268692c025SYoshinobu Inoue * do it in forward order. 11278692c025SYoshinobu Inoue */ 11288692c025SYoshinobu Inoue if (sizeof (struct file *) >= sizeof (int)) { 11298692c025SYoshinobu Inoue fdp = (int *)(cm + 1) + oldfds - 1; 11308692c025SYoshinobu Inoue rp = (struct file **)CMSG_DATA(cm) + oldfds - 1; 1131df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 11328692c025SYoshinobu Inoue fp = fdescp->fd_ofiles[*fdp--]; 11338692c025SYoshinobu Inoue *rp-- = fp; 11348692c025SYoshinobu Inoue fp->f_count++; 11358692c025SYoshinobu Inoue fp->f_msgcount++; 11368692c025SYoshinobu Inoue unp_rights++; 11378692c025SYoshinobu Inoue } 11388692c025SYoshinobu Inoue } else { 11398692c025SYoshinobu Inoue fdp = (int *)(cm + 1); 11408692c025SYoshinobu Inoue rp = (struct file **)CMSG_DATA(cm); 11418692c025SYoshinobu Inoue for (i = 0; i < oldfds; i++) { 11428692c025SYoshinobu Inoue fp = fdescp->fd_ofiles[*fdp++]; 1143df8bae1dSRodney W. Grimes *rp++ = fp; 1144df8bae1dSRodney W. Grimes fp->f_count++; 1145df8bae1dSRodney W. Grimes fp->f_msgcount++; 1146df8bae1dSRodney W. Grimes unp_rights++; 1147df8bae1dSRodney W. Grimes } 11488692c025SYoshinobu Inoue } 1149df8bae1dSRodney W. Grimes return (0); 1150df8bae1dSRodney W. Grimes } 1151df8bae1dSRodney W. Grimes 1152f708ef1bSPoul-Henning Kamp static int unp_defer, unp_gcing; 1153df8bae1dSRodney W. Grimes 1154f708ef1bSPoul-Henning Kamp static void 1155df8bae1dSRodney W. Grimes unp_gc() 1156df8bae1dSRodney W. Grimes { 1157df8bae1dSRodney W. Grimes register struct file *fp, *nextfp; 1158df8bae1dSRodney W. Grimes register struct socket *so; 1159df8bae1dSRodney W. Grimes struct file **extra_ref, **fpp; 1160df8bae1dSRodney W. Grimes int nunref, i; 1161df8bae1dSRodney W. Grimes 1162df8bae1dSRodney W. Grimes if (unp_gcing) 1163df8bae1dSRodney W. Grimes return; 1164df8bae1dSRodney W. Grimes unp_gcing = 1; 1165df8bae1dSRodney W. Grimes unp_defer = 0; 1166ed5b7817SJulian Elischer /* 1167ed5b7817SJulian Elischer * before going through all this, set all FDs to 1168ed5b7817SJulian Elischer * be NOT defered and NOT externally accessible 1169ed5b7817SJulian Elischer */ 11702e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) 1171df8bae1dSRodney W. Grimes fp->f_flag &= ~(FMARK|FDEFER); 1172df8bae1dSRodney W. Grimes do { 11732e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) { 1174ed5b7817SJulian Elischer /* 1175ed5b7817SJulian Elischer * If the file is not open, skip it 1176ed5b7817SJulian Elischer */ 1177df8bae1dSRodney W. Grimes if (fp->f_count == 0) 1178df8bae1dSRodney W. Grimes continue; 1179ed5b7817SJulian Elischer /* 1180ed5b7817SJulian Elischer * If we already marked it as 'defer' in a 1181ed5b7817SJulian Elischer * previous pass, then try process it this time 1182ed5b7817SJulian Elischer * and un-mark it 1183ed5b7817SJulian Elischer */ 1184df8bae1dSRodney W. Grimes if (fp->f_flag & FDEFER) { 1185df8bae1dSRodney W. Grimes fp->f_flag &= ~FDEFER; 1186df8bae1dSRodney W. Grimes unp_defer--; 1187df8bae1dSRodney W. Grimes } else { 1188ed5b7817SJulian Elischer /* 1189ed5b7817SJulian Elischer * if it's not defered, then check if it's 1190ed5b7817SJulian Elischer * already marked.. if so skip it 1191ed5b7817SJulian Elischer */ 1192df8bae1dSRodney W. Grimes if (fp->f_flag & FMARK) 1193df8bae1dSRodney W. Grimes continue; 1194ed5b7817SJulian Elischer /* 1195ed5b7817SJulian Elischer * If all references are from messages 1196ed5b7817SJulian Elischer * in transit, then skip it. it's not 1197ed5b7817SJulian Elischer * externally accessible. 1198ed5b7817SJulian Elischer */ 1199df8bae1dSRodney W. Grimes if (fp->f_count == fp->f_msgcount) 1200df8bae1dSRodney W. Grimes continue; 1201ed5b7817SJulian Elischer /* 1202ed5b7817SJulian Elischer * If it got this far then it must be 1203ed5b7817SJulian Elischer * externally accessible. 1204ed5b7817SJulian Elischer */ 1205df8bae1dSRodney W. Grimes fp->f_flag |= FMARK; 1206df8bae1dSRodney W. Grimes } 1207ed5b7817SJulian Elischer /* 1208ed5b7817SJulian Elischer * either it was defered, or it is externally 1209ed5b7817SJulian Elischer * accessible and not already marked so. 1210ed5b7817SJulian Elischer * Now check if it is possibly one of OUR sockets. 1211ed5b7817SJulian Elischer */ 1212df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_SOCKET || 1213df8bae1dSRodney W. Grimes (so = (struct socket *)fp->f_data) == 0) 1214df8bae1dSRodney W. Grimes continue; 1215748e0b0aSGarrett Wollman if (so->so_proto->pr_domain != &localdomain || 1216df8bae1dSRodney W. Grimes (so->so_proto->pr_flags&PR_RIGHTS) == 0) 1217df8bae1dSRodney W. Grimes continue; 1218df8bae1dSRodney W. Grimes #ifdef notdef 1219df8bae1dSRodney W. Grimes if (so->so_rcv.sb_flags & SB_LOCK) { 1220df8bae1dSRodney W. Grimes /* 1221df8bae1dSRodney W. Grimes * This is problematical; it's not clear 1222df8bae1dSRodney W. Grimes * we need to wait for the sockbuf to be 1223df8bae1dSRodney W. Grimes * unlocked (on a uniprocessor, at least), 1224df8bae1dSRodney W. Grimes * and it's also not clear what to do 1225df8bae1dSRodney W. Grimes * if sbwait returns an error due to receipt 1226df8bae1dSRodney W. Grimes * of a signal. If sbwait does return 1227df8bae1dSRodney W. Grimes * an error, we'll go into an infinite 1228df8bae1dSRodney W. Grimes * loop. Delete all of this for now. 1229df8bae1dSRodney W. Grimes */ 1230df8bae1dSRodney W. Grimes (void) sbwait(&so->so_rcv); 1231df8bae1dSRodney W. Grimes goto restart; 1232df8bae1dSRodney W. Grimes } 1233df8bae1dSRodney W. Grimes #endif 1234ed5b7817SJulian Elischer /* 1235ed5b7817SJulian Elischer * So, Ok, it's one of our sockets and it IS externally 1236ed5b7817SJulian Elischer * accessible (or was defered). Now we look 1237dc733423SDag-Erling Smørgrav * to see if we hold any file descriptors in its 1238ed5b7817SJulian Elischer * message buffers. Follow those links and mark them 1239ed5b7817SJulian Elischer * as accessible too. 1240ed5b7817SJulian Elischer */ 1241df8bae1dSRodney W. Grimes unp_scan(so->so_rcv.sb_mb, unp_mark); 1242df8bae1dSRodney W. Grimes } 1243df8bae1dSRodney W. Grimes } while (unp_defer); 1244df8bae1dSRodney W. Grimes /* 1245df8bae1dSRodney W. Grimes * We grab an extra reference to each of the file table entries 1246df8bae1dSRodney W. Grimes * that are not otherwise accessible and then free the rights 1247df8bae1dSRodney W. Grimes * that are stored in messages on them. 1248df8bae1dSRodney W. Grimes * 1249df8bae1dSRodney W. Grimes * The bug in the orginal code is a little tricky, so I'll describe 1250df8bae1dSRodney W. Grimes * what's wrong with it here. 1251df8bae1dSRodney W. Grimes * 1252df8bae1dSRodney W. Grimes * It is incorrect to simply unp_discard each entry for f_msgcount 1253df8bae1dSRodney W. Grimes * times -- consider the case of sockets A and B that contain 1254df8bae1dSRodney W. Grimes * references to each other. On a last close of some other socket, 1255df8bae1dSRodney W. Grimes * we trigger a gc since the number of outstanding rights (unp_rights) 1256df8bae1dSRodney W. Grimes * is non-zero. If during the sweep phase the gc code un_discards, 1257df8bae1dSRodney W. Grimes * we end up doing a (full) closef on the descriptor. A closef on A 1258df8bae1dSRodney W. Grimes * results in the following chain. Closef calls soo_close, which 1259df8bae1dSRodney W. Grimes * calls soclose. Soclose calls first (through the switch 1260df8bae1dSRodney W. Grimes * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 1261df8bae1dSRodney W. Grimes * returns because the previous instance had set unp_gcing, and 1262df8bae1dSRodney W. Grimes * we return all the way back to soclose, which marks the socket 1263df8bae1dSRodney W. Grimes * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush 1264df8bae1dSRodney W. Grimes * to free up the rights that are queued in messages on the socket A, 1265df8bae1dSRodney W. Grimes * i.e., the reference on B. The sorflush calls via the dom_dispose 1266df8bae1dSRodney W. Grimes * switch unp_dispose, which unp_scans with unp_discard. This second 1267df8bae1dSRodney W. Grimes * instance of unp_discard just calls closef on B. 1268df8bae1dSRodney W. Grimes * 1269df8bae1dSRodney W. Grimes * Well, a similar chain occurs on B, resulting in a sorflush on B, 1270df8bae1dSRodney W. Grimes * which results in another closef on A. Unfortunately, A is already 1271df8bae1dSRodney W. Grimes * being closed, and the descriptor has already been marked with 1272df8bae1dSRodney W. Grimes * SS_NOFDREF, and soclose panics at this point. 1273df8bae1dSRodney W. Grimes * 1274df8bae1dSRodney W. Grimes * Here, we first take an extra reference to each inaccessible 1275df8bae1dSRodney W. Grimes * descriptor. Then, we call sorflush ourself, since we know 1276df8bae1dSRodney W. Grimes * it is a Unix domain socket anyhow. After we destroy all the 1277df8bae1dSRodney W. Grimes * rights carried in messages, we do a last closef to get rid 1278df8bae1dSRodney W. Grimes * of our extra reference. This is the last close, and the 1279df8bae1dSRodney W. Grimes * unp_detach etc will shut down the socket. 1280df8bae1dSRodney W. Grimes * 1281df8bae1dSRodney W. Grimes * 91/09/19, bsy@cs.cmu.edu 1282df8bae1dSRodney W. Grimes */ 1283df8bae1dSRodney W. Grimes extra_ref = malloc(nfiles * sizeof(struct file *), M_FILE, M_WAITOK); 12842e3c8fcbSPoul-Henning Kamp for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; fp != 0; 1285bc6f0e79SJeffrey Hsu fp = nextfp) { 12862e3c8fcbSPoul-Henning Kamp nextfp = LIST_NEXT(fp, f_list); 1287ed5b7817SJulian Elischer /* 1288ed5b7817SJulian Elischer * If it's not open, skip it 1289ed5b7817SJulian Elischer */ 1290df8bae1dSRodney W. Grimes if (fp->f_count == 0) 1291df8bae1dSRodney W. Grimes continue; 1292ed5b7817SJulian Elischer /* 1293ed5b7817SJulian Elischer * If all refs are from msgs, and it's not marked accessible 1294ed5b7817SJulian Elischer * then it must be referenced from some unreachable cycle 1295ed5b7817SJulian Elischer * of (shut-down) FDs, so include it in our 1296ed5b7817SJulian Elischer * list of FDs to remove 1297ed5b7817SJulian Elischer */ 1298df8bae1dSRodney W. Grimes if (fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) { 1299df8bae1dSRodney W. Grimes *fpp++ = fp; 1300df8bae1dSRodney W. Grimes nunref++; 1301df8bae1dSRodney W. Grimes fp->f_count++; 1302df8bae1dSRodney W. Grimes } 1303df8bae1dSRodney W. Grimes } 1304ed5b7817SJulian Elischer /* 1305ed5b7817SJulian Elischer * for each FD on our hit list, do the following two things 1306ed5b7817SJulian Elischer */ 13071c7c3c6aSMatthew Dillon for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 13081c7c3c6aSMatthew Dillon struct file *tfp = *fpp; 13091c7c3c6aSMatthew Dillon if (tfp->f_type == DTYPE_SOCKET && tfp->f_data != NULL) 13101c7c3c6aSMatthew Dillon sorflush((struct socket *)(tfp->f_data)); 13111c7c3c6aSMatthew Dillon } 1312df8bae1dSRodney W. Grimes for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) 131392cbac68SPoul-Henning Kamp closef(*fpp, (struct proc *) NULL); 1314df8bae1dSRodney W. Grimes free((caddr_t)extra_ref, M_FILE); 1315df8bae1dSRodney W. Grimes unp_gcing = 0; 1316df8bae1dSRodney W. Grimes } 1317df8bae1dSRodney W. Grimes 131826f9a767SRodney W. Grimes void 1319df8bae1dSRodney W. Grimes unp_dispose(m) 1320df8bae1dSRodney W. Grimes struct mbuf *m; 1321df8bae1dSRodney W. Grimes { 1322996c772fSJohn Dyson 1323df8bae1dSRodney W. Grimes if (m) 1324df8bae1dSRodney W. Grimes unp_scan(m, unp_discard); 1325df8bae1dSRodney W. Grimes } 1326df8bae1dSRodney W. Grimes 13270c1bb4fbSDima Dorfman static int 13280c1bb4fbSDima Dorfman unp_listen(unp, p) 13290c1bb4fbSDima Dorfman struct unpcb *unp; 13300c1bb4fbSDima Dorfman struct proc *p; 13310c1bb4fbSDima Dorfman { 13320c1bb4fbSDima Dorfman 13330c1bb4fbSDima Dorfman bzero(&unp->unp_peercred, sizeof(unp->unp_peercred)); 13340c1bb4fbSDima Dorfman unp->unp_peercred.cr_uid = p->p_ucred->cr_uid; 13350c1bb4fbSDima Dorfman unp->unp_peercred.cr_ngroups = p->p_ucred->cr_ngroups; 13360c1bb4fbSDima Dorfman bcopy(p->p_ucred->cr_groups, unp->unp_peercred.cr_groups, 13370c1bb4fbSDima Dorfman sizeof(unp->unp_peercred.cr_groups)); 13380c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPCCACHED; 13390c1bb4fbSDima Dorfman return (0); 13400c1bb4fbSDima Dorfman } 13410c1bb4fbSDima Dorfman 1342f708ef1bSPoul-Henning Kamp static void 1343df8bae1dSRodney W. Grimes unp_scan(m0, op) 1344df8bae1dSRodney W. Grimes register struct mbuf *m0; 1345996c772fSJohn Dyson void (*op) __P((struct file *)); 1346df8bae1dSRodney W. Grimes { 1347df8bae1dSRodney W. Grimes register struct mbuf *m; 1348df8bae1dSRodney W. Grimes register struct file **rp; 1349df8bae1dSRodney W. Grimes register struct cmsghdr *cm; 1350df8bae1dSRodney W. Grimes register int i; 1351df8bae1dSRodney W. Grimes int qfds; 1352df8bae1dSRodney W. Grimes 1353df8bae1dSRodney W. Grimes while (m0) { 1354df8bae1dSRodney W. Grimes for (m = m0; m; m = m->m_next) 1355df8bae1dSRodney W. Grimes if (m->m_type == MT_CONTROL && 1356df8bae1dSRodney W. Grimes m->m_len >= sizeof(*cm)) { 1357df8bae1dSRodney W. Grimes cm = mtod(m, struct cmsghdr *); 1358df8bae1dSRodney W. Grimes if (cm->cmsg_level != SOL_SOCKET || 1359df8bae1dSRodney W. Grimes cm->cmsg_type != SCM_RIGHTS) 1360df8bae1dSRodney W. Grimes continue; 13618692c025SYoshinobu Inoue qfds = (cm->cmsg_len - 13628692c025SYoshinobu Inoue (CMSG_DATA(cm) - (u_char *)cm)) 1363df8bae1dSRodney W. Grimes / sizeof (struct file *); 13648692c025SYoshinobu Inoue rp = (struct file **)CMSG_DATA(cm); 1365df8bae1dSRodney W. Grimes for (i = 0; i < qfds; i++) 1366df8bae1dSRodney W. Grimes (*op)(*rp++); 1367df8bae1dSRodney W. Grimes break; /* XXX, but saves time */ 1368df8bae1dSRodney W. Grimes } 1369df8bae1dSRodney W. Grimes m0 = m0->m_act; 1370df8bae1dSRodney W. Grimes } 1371df8bae1dSRodney W. Grimes } 1372df8bae1dSRodney W. Grimes 1373f708ef1bSPoul-Henning Kamp static void 1374df8bae1dSRodney W. Grimes unp_mark(fp) 1375df8bae1dSRodney W. Grimes struct file *fp; 1376df8bae1dSRodney W. Grimes { 1377df8bae1dSRodney W. Grimes 1378df8bae1dSRodney W. Grimes if (fp->f_flag & FMARK) 1379df8bae1dSRodney W. Grimes return; 1380df8bae1dSRodney W. Grimes unp_defer++; 1381df8bae1dSRodney W. Grimes fp->f_flag |= (FMARK|FDEFER); 1382df8bae1dSRodney W. Grimes } 1383df8bae1dSRodney W. Grimes 1384f708ef1bSPoul-Henning Kamp static void 1385df8bae1dSRodney W. Grimes unp_discard(fp) 1386df8bae1dSRodney W. Grimes struct file *fp; 1387df8bae1dSRodney W. Grimes { 1388df8bae1dSRodney W. Grimes 1389df8bae1dSRodney W. Grimes fp->f_msgcount--; 1390df8bae1dSRodney W. Grimes unp_rights--; 1391df8bae1dSRodney W. Grimes (void) closef(fp, (struct proc *)NULL); 1392df8bae1dSRodney W. Grimes } 1393