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; 59057bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 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; 59757bf258eSGarrett Wollman strncpy(buf, soun->sun_path, namelen); 59857bf258eSGarrett Wollman buf[namelen] = 0; /* null-terminate the string */ 599f2a2857bSKirk McKusick restart: 600974784e8SGuido van Rooij NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT, UIO_SYSSPACE, 60157bf258eSGarrett Wollman buf, p); 602df8bae1dSRodney W. Grimes /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 603797f2d22SPoul-Henning Kamp error = namei(&nd); 604797f2d22SPoul-Henning Kamp if (error) 605df8bae1dSRodney W. Grimes return (error); 606df8bae1dSRodney W. Grimes vp = nd.ni_vp; 607f2a2857bSKirk McKusick if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 608762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 609df8bae1dSRodney W. Grimes if (nd.ni_dvp == vp) 610df8bae1dSRodney W. Grimes vrele(nd.ni_dvp); 611df8bae1dSRodney W. Grimes else 612df8bae1dSRodney W. Grimes vput(nd.ni_dvp); 613f2a2857bSKirk McKusick if (vp != NULL) { 614df8bae1dSRodney W. Grimes vrele(vp); 615df8bae1dSRodney W. Grimes return (EADDRINUSE); 616df8bae1dSRodney W. Grimes } 617f2a2857bSKirk McKusick if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 618f2a2857bSKirk McKusick return (error); 619f2a2857bSKirk McKusick goto restart; 620f2a2857bSKirk McKusick } 621df8bae1dSRodney W. Grimes VATTR_NULL(&vattr); 622df8bae1dSRodney W. Grimes vattr.va_type = VSOCK; 623a29f300eSGarrett Wollman vattr.va_mode = (ACCESSPERMS & ~p->p_fd->fd_cmask); 624996c772fSJohn Dyson VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); 6257be2d300SMike Smith error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 626762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 6277be2d300SMike Smith vput(nd.ni_dvp); 6287be2d300SMike Smith if (error) 629df8bae1dSRodney W. Grimes return (error); 630df8bae1dSRodney W. Grimes vp = nd.ni_vp; 631df8bae1dSRodney W. Grimes vp->v_socket = unp->unp_socket; 632df8bae1dSRodney W. Grimes unp->unp_vnode = vp; 63357bf258eSGarrett Wollman unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam, 1); 634996c772fSJohn Dyson VOP_UNLOCK(vp, 0, p); 635f2a2857bSKirk McKusick vn_finished_write(mp); 636df8bae1dSRodney W. Grimes return (0); 637df8bae1dSRodney W. Grimes } 638df8bae1dSRodney W. Grimes 639f708ef1bSPoul-Henning Kamp static int 640df8bae1dSRodney W. Grimes unp_connect(so, nam, p) 641df8bae1dSRodney W. Grimes struct socket *so; 64257bf258eSGarrett Wollman struct sockaddr *nam; 643df8bae1dSRodney W. Grimes struct proc *p; 644df8bae1dSRodney W. Grimes { 64557bf258eSGarrett Wollman register struct sockaddr_un *soun = (struct sockaddr_un *)nam; 646df8bae1dSRodney W. Grimes register struct vnode *vp; 647df8bae1dSRodney W. Grimes register struct socket *so2, *so3; 6480c1bb4fbSDima Dorfman struct unpcb *unp, *unp2, *unp3; 64957bf258eSGarrett Wollman int error, len; 650df8bae1dSRodney W. Grimes struct nameidata nd; 65157bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 652df8bae1dSRodney W. Grimes 65357bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 65457bf258eSGarrett Wollman if (len <= 0) 65557bf258eSGarrett Wollman return EINVAL; 65657bf258eSGarrett Wollman strncpy(buf, soun->sun_path, len); 65757bf258eSGarrett Wollman buf[len] = 0; 65857bf258eSGarrett Wollman 65957bf258eSGarrett Wollman NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, p); 660797f2d22SPoul-Henning Kamp error = namei(&nd); 661797f2d22SPoul-Henning Kamp if (error) 662df8bae1dSRodney W. Grimes return (error); 663df8bae1dSRodney W. Grimes vp = nd.ni_vp; 664762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 665df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 666df8bae1dSRodney W. Grimes error = ENOTSOCK; 667df8bae1dSRodney W. Grimes goto bad; 668df8bae1dSRodney W. Grimes } 669797f2d22SPoul-Henning Kamp error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p); 670797f2d22SPoul-Henning Kamp if (error) 671df8bae1dSRodney W. Grimes goto bad; 672df8bae1dSRodney W. Grimes so2 = vp->v_socket; 673df8bae1dSRodney W. Grimes if (so2 == 0) { 674df8bae1dSRodney W. Grimes error = ECONNREFUSED; 675df8bae1dSRodney W. Grimes goto bad; 676df8bae1dSRodney W. Grimes } 677df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 678df8bae1dSRodney W. Grimes error = EPROTOTYPE; 679df8bae1dSRodney W. Grimes goto bad; 680df8bae1dSRodney W. Grimes } 681df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 682df8bae1dSRodney W. Grimes if ((so2->so_options & SO_ACCEPTCONN) == 0 || 6832f9a2132SBrian Feldman (so3 = sonewconn3(so2, 0, p)) == 0) { 684df8bae1dSRodney W. Grimes error = ECONNREFUSED; 685df8bae1dSRodney W. Grimes goto bad; 686df8bae1dSRodney W. Grimes } 6870c1bb4fbSDima Dorfman unp = sotounpcb(so); 688df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 689df8bae1dSRodney W. Grimes unp3 = sotounpcb(so3); 690df8bae1dSRodney W. Grimes if (unp2->unp_addr) 69157bf258eSGarrett Wollman unp3->unp_addr = (struct sockaddr_un *) 69257bf258eSGarrett Wollman dup_sockaddr((struct sockaddr *) 69357bf258eSGarrett Wollman unp2->unp_addr, 1); 6940c1bb4fbSDima Dorfman 6950c1bb4fbSDima Dorfman /* 6960c1bb4fbSDima Dorfman * unp_peercred management: 6970c1bb4fbSDima Dorfman * 6980c1bb4fbSDima Dorfman * The connecter's (client's) credentials are copied 6990c1bb4fbSDima Dorfman * from its process structure at the time of connect() 7000c1bb4fbSDima Dorfman * (which is now). 7010c1bb4fbSDima Dorfman */ 7020c1bb4fbSDima Dorfman memset(&unp3->unp_peercred, '\0', sizeof(unp3->unp_peercred)); 7030c1bb4fbSDima Dorfman unp3->unp_peercred.cr_uid = p->p_ucred->cr_uid; 7040c1bb4fbSDima Dorfman unp3->unp_peercred.cr_ngroups = p->p_ucred->cr_ngroups; 7050c1bb4fbSDima Dorfman memcpy(unp3->unp_peercred.cr_groups, p->p_ucred->cr_groups, 7060c1bb4fbSDima Dorfman sizeof(unp3->unp_peercred.cr_groups)); 7070c1bb4fbSDima Dorfman unp3->unp_flags |= UNP_HAVEPC; 7080c1bb4fbSDima Dorfman /* 7090c1bb4fbSDima Dorfman * The receiver's (server's) credentials are copied 7100c1bb4fbSDima Dorfman * from the unp_peercred member of socket on which the 7110c1bb4fbSDima Dorfman * former called listen(); unp_listen() cached that 7120c1bb4fbSDima Dorfman * process's credentials at that time so we can use 7130c1bb4fbSDima Dorfman * them now. 7140c1bb4fbSDima Dorfman */ 7150c1bb4fbSDima Dorfman KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 7160c1bb4fbSDima Dorfman ("unp_connect: listener without cached peercred")); 7170c1bb4fbSDima Dorfman memcpy(&unp->unp_peercred, &unp2->unp_peercred, 7180c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 7190c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPC; 7200c1bb4fbSDima Dorfman 721df8bae1dSRodney W. Grimes so2 = so3; 722df8bae1dSRodney W. Grimes } 723df8bae1dSRodney W. Grimes error = unp_connect2(so, so2); 724df8bae1dSRodney W. Grimes bad: 725df8bae1dSRodney W. Grimes vput(vp); 726df8bae1dSRodney W. Grimes return (error); 727df8bae1dSRodney W. Grimes } 728df8bae1dSRodney W. Grimes 72926f9a767SRodney W. Grimes int 730df8bae1dSRodney W. Grimes unp_connect2(so, so2) 731df8bae1dSRodney W. Grimes register struct socket *so; 732df8bae1dSRodney W. Grimes register struct socket *so2; 733df8bae1dSRodney W. Grimes { 734df8bae1dSRodney W. Grimes register struct unpcb *unp = sotounpcb(so); 735df8bae1dSRodney W. Grimes register struct unpcb *unp2; 736df8bae1dSRodney W. Grimes 737df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 738df8bae1dSRodney W. Grimes return (EPROTOTYPE); 739df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 740df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 741df8bae1dSRodney W. Grimes switch (so->so_type) { 742df8bae1dSRodney W. Grimes 743df8bae1dSRodney W. Grimes case SOCK_DGRAM: 74498271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 745df8bae1dSRodney W. Grimes soisconnected(so); 746df8bae1dSRodney W. Grimes break; 747df8bae1dSRodney W. Grimes 748df8bae1dSRodney W. Grimes case SOCK_STREAM: 749df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 750df8bae1dSRodney W. Grimes soisconnected(so); 751df8bae1dSRodney W. Grimes soisconnected(so2); 752df8bae1dSRodney W. Grimes break; 753df8bae1dSRodney W. Grimes 754df8bae1dSRodney W. Grimes default: 755df8bae1dSRodney W. Grimes panic("unp_connect2"); 756df8bae1dSRodney W. Grimes } 757df8bae1dSRodney W. Grimes return (0); 758df8bae1dSRodney W. Grimes } 759df8bae1dSRodney W. Grimes 760f708ef1bSPoul-Henning Kamp static void 761df8bae1dSRodney W. Grimes unp_disconnect(unp) 762df8bae1dSRodney W. Grimes struct unpcb *unp; 763df8bae1dSRodney W. Grimes { 764df8bae1dSRodney W. Grimes register struct unpcb *unp2 = unp->unp_conn; 765df8bae1dSRodney W. Grimes 766df8bae1dSRodney W. Grimes if (unp2 == 0) 767df8bae1dSRodney W. Grimes return; 768df8bae1dSRodney W. Grimes unp->unp_conn = 0; 769df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 770df8bae1dSRodney W. Grimes 771df8bae1dSRodney W. Grimes case SOCK_DGRAM: 77298271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 773df8bae1dSRodney W. Grimes unp->unp_socket->so_state &= ~SS_ISCONNECTED; 774df8bae1dSRodney W. Grimes break; 775df8bae1dSRodney W. Grimes 776df8bae1dSRodney W. Grimes case SOCK_STREAM: 777df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 778df8bae1dSRodney W. Grimes unp2->unp_conn = 0; 779df8bae1dSRodney W. Grimes soisdisconnected(unp2->unp_socket); 780df8bae1dSRodney W. Grimes break; 781df8bae1dSRodney W. Grimes } 782df8bae1dSRodney W. Grimes } 783df8bae1dSRodney W. Grimes 784df8bae1dSRodney W. Grimes #ifdef notdef 78526f9a767SRodney W. Grimes void 786df8bae1dSRodney W. Grimes unp_abort(unp) 787df8bae1dSRodney W. Grimes struct unpcb *unp; 788df8bae1dSRodney W. Grimes { 789df8bae1dSRodney W. Grimes 790df8bae1dSRodney W. Grimes unp_detach(unp); 791df8bae1dSRodney W. Grimes } 792df8bae1dSRodney W. Grimes #endif 793df8bae1dSRodney W. Grimes 79498271db4SGarrett Wollman static int 79575c13541SPoul-Henning Kamp prison_unpcb(struct proc *p, struct unpcb *unp) 79675c13541SPoul-Henning Kamp { 79791421ba2SRobert Watson if (!jailed(p->p_ucred)) 79875c13541SPoul-Henning Kamp return (0); 79975c13541SPoul-Henning Kamp if (p->p_fd->fd_rdir == unp->unp_rvnode) 80075c13541SPoul-Henning Kamp return (0); 80175c13541SPoul-Henning Kamp return (1); 80275c13541SPoul-Henning Kamp } 80375c13541SPoul-Henning Kamp 80475c13541SPoul-Henning Kamp static int 80582d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 80698271db4SGarrett Wollman { 807f5ef029eSPoul-Henning Kamp int error, i, n; 80898271db4SGarrett Wollman struct unpcb *unp, **unp_list; 80998271db4SGarrett Wollman unp_gen_t gencnt; 81098271db4SGarrett Wollman struct xunpgen xug; 81198271db4SGarrett Wollman struct unp_head *head; 81298271db4SGarrett Wollman 813a23d65bfSBruce Evans head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 81498271db4SGarrett Wollman 81598271db4SGarrett Wollman /* 81698271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 81798271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 81898271db4SGarrett Wollman */ 81998271db4SGarrett Wollman if (req->oldptr == 0) { 82098271db4SGarrett Wollman n = unp_count; 82198271db4SGarrett Wollman req->oldidx = 2 * (sizeof xug) 82298271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 82398271db4SGarrett Wollman return 0; 82498271db4SGarrett Wollman } 82598271db4SGarrett Wollman 82698271db4SGarrett Wollman if (req->newptr != 0) 82798271db4SGarrett Wollman return EPERM; 82898271db4SGarrett Wollman 82998271db4SGarrett Wollman /* 83098271db4SGarrett Wollman * OK, now we're committed to doing something. 83198271db4SGarrett Wollman */ 83298271db4SGarrett Wollman gencnt = unp_gencnt; 83398271db4SGarrett Wollman n = unp_count; 83498271db4SGarrett Wollman 83598271db4SGarrett Wollman xug.xug_len = sizeof xug; 83698271db4SGarrett Wollman xug.xug_count = n; 83798271db4SGarrett Wollman xug.xug_gen = gencnt; 83898271db4SGarrett Wollman xug.xug_sogen = so_gencnt; 83998271db4SGarrett Wollman error = SYSCTL_OUT(req, &xug, sizeof xug); 84098271db4SGarrett Wollman if (error) 84198271db4SGarrett Wollman return error; 84298271db4SGarrett Wollman 84398271db4SGarrett Wollman unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 84498271db4SGarrett Wollman if (unp_list == 0) 84598271db4SGarrett Wollman return ENOMEM; 84698271db4SGarrett Wollman 8472e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 8482e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 84975c13541SPoul-Henning Kamp if (unp->unp_gencnt <= gencnt && !prison_unpcb(req->p, unp)) 85098271db4SGarrett Wollman unp_list[i++] = unp; 85198271db4SGarrett Wollman } 85298271db4SGarrett Wollman n = i; /* in case we lost some during malloc */ 85398271db4SGarrett Wollman 85498271db4SGarrett Wollman error = 0; 85598271db4SGarrett Wollman for (i = 0; i < n; i++) { 85698271db4SGarrett Wollman unp = unp_list[i]; 85798271db4SGarrett Wollman if (unp->unp_gencnt <= gencnt) { 85898271db4SGarrett Wollman struct xunpcb xu; 85998271db4SGarrett Wollman xu.xu_len = sizeof xu; 86098271db4SGarrett Wollman xu.xu_unpp = unp; 86198271db4SGarrett Wollman /* 86298271db4SGarrett Wollman * XXX - need more locking here to protect against 86398271db4SGarrett Wollman * connect/disconnect races for SMP. 86498271db4SGarrett Wollman */ 86598271db4SGarrett Wollman if (unp->unp_addr) 86698271db4SGarrett Wollman bcopy(unp->unp_addr, &xu.xu_addr, 86798271db4SGarrett Wollman unp->unp_addr->sun_len); 86898271db4SGarrett Wollman if (unp->unp_conn && unp->unp_conn->unp_addr) 86998271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 87098271db4SGarrett Wollman &xu.xu_caddr, 87198271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 87298271db4SGarrett Wollman bcopy(unp, &xu.xu_unp, sizeof *unp); 87398271db4SGarrett Wollman sotoxsocket(unp->unp_socket, &xu.xu_socket); 87498271db4SGarrett Wollman error = SYSCTL_OUT(req, &xu, sizeof xu); 87598271db4SGarrett Wollman } 87698271db4SGarrett Wollman } 87798271db4SGarrett Wollman if (!error) { 87898271db4SGarrett Wollman /* 87998271db4SGarrett Wollman * Give the user an updated idea of our state. 88098271db4SGarrett Wollman * If the generation differs from what we told 88198271db4SGarrett Wollman * her before, she knows that something happened 88298271db4SGarrett Wollman * while we were processing this request, and it 88398271db4SGarrett Wollman * might be necessary to retry. 88498271db4SGarrett Wollman */ 88598271db4SGarrett Wollman xug.xug_gen = unp_gencnt; 88698271db4SGarrett Wollman xug.xug_sogen = so_gencnt; 88798271db4SGarrett Wollman xug.xug_count = unp_count; 88898271db4SGarrett Wollman error = SYSCTL_OUT(req, &xug, sizeof xug); 88998271db4SGarrett Wollman } 89098271db4SGarrett Wollman free(unp_list, M_TEMP); 89198271db4SGarrett Wollman return error; 89298271db4SGarrett Wollman } 89398271db4SGarrett Wollman 89498271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 89598271db4SGarrett Wollman (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 89698271db4SGarrett Wollman "List of active local datagram sockets"); 89798271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 89898271db4SGarrett Wollman (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 89998271db4SGarrett Wollman "List of active local stream sockets"); 90098271db4SGarrett Wollman 901f708ef1bSPoul-Henning Kamp static void 902df8bae1dSRodney W. Grimes unp_shutdown(unp) 903df8bae1dSRodney W. Grimes struct unpcb *unp; 904df8bae1dSRodney W. Grimes { 905df8bae1dSRodney W. Grimes struct socket *so; 906df8bae1dSRodney W. Grimes 907df8bae1dSRodney W. Grimes if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 908df8bae1dSRodney W. Grimes (so = unp->unp_conn->unp_socket)) 909df8bae1dSRodney W. Grimes socantrcvmore(so); 910df8bae1dSRodney W. Grimes } 911df8bae1dSRodney W. Grimes 912f708ef1bSPoul-Henning Kamp static void 913df8bae1dSRodney W. Grimes unp_drop(unp, errno) 914df8bae1dSRodney W. Grimes struct unpcb *unp; 915df8bae1dSRodney W. Grimes int errno; 916df8bae1dSRodney W. Grimes { 917df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 918df8bae1dSRodney W. Grimes 919df8bae1dSRodney W. Grimes so->so_error = errno; 920df8bae1dSRodney W. Grimes unp_disconnect(unp); 921df8bae1dSRodney W. Grimes if (so->so_head) { 92298271db4SGarrett Wollman LIST_REMOVE(unp, unp_link); 92398271db4SGarrett Wollman unp->unp_gencnt = ++unp_gencnt; 92498271db4SGarrett Wollman unp_count--; 925df8bae1dSRodney W. Grimes so->so_pcb = (caddr_t) 0; 92657bf258eSGarrett Wollman if (unp->unp_addr) 92757bf258eSGarrett Wollman FREE(unp->unp_addr, M_SONAME); 92898271db4SGarrett Wollman zfree(unp_zone, unp); 929df8bae1dSRodney W. Grimes sofree(so); 930df8bae1dSRodney W. Grimes } 931df8bae1dSRodney W. Grimes } 932df8bae1dSRodney W. Grimes 933df8bae1dSRodney W. Grimes #ifdef notdef 93426f9a767SRodney W. Grimes void 935df8bae1dSRodney W. Grimes unp_drain() 936df8bae1dSRodney W. Grimes { 937df8bae1dSRodney W. Grimes 938df8bae1dSRodney W. Grimes } 939df8bae1dSRodney W. Grimes #endif 940df8bae1dSRodney W. Grimes 94126f9a767SRodney W. Grimes int 942df8bae1dSRodney W. Grimes unp_externalize(rights) 943df8bae1dSRodney W. Grimes struct mbuf *rights; 944df8bae1dSRodney W. Grimes { 945df8bae1dSRodney W. Grimes struct proc *p = curproc; /* XXX */ 946df8bae1dSRodney W. Grimes register int i; 947df8bae1dSRodney W. Grimes register struct cmsghdr *cm = mtod(rights, struct cmsghdr *); 9488692c025SYoshinobu Inoue register int *fdp; 9498692c025SYoshinobu Inoue register struct file **rp; 950df8bae1dSRodney W. Grimes register struct file *fp; 9518692c025SYoshinobu Inoue int newfds = (cm->cmsg_len - (CMSG_DATA(cm) - (u_char *)cm)) 9528692c025SYoshinobu Inoue / sizeof (struct file *); 953df8bae1dSRodney W. Grimes int f; 954df8bae1dSRodney W. Grimes 955ed5b7817SJulian Elischer /* 956ed5b7817SJulian Elischer * if the new FD's will not fit, then we free them all 957ed5b7817SJulian Elischer */ 958df8bae1dSRodney W. Grimes if (!fdavail(p, newfds)) { 9598692c025SYoshinobu Inoue rp = (struct file **)CMSG_DATA(cm); 960df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 961df8bae1dSRodney W. Grimes fp = *rp; 9628692c025SYoshinobu Inoue /* 9638692c025SYoshinobu Inoue * zero the pointer before calling unp_discard, 9648692c025SYoshinobu Inoue * since it may end up in unp_gc().. 9658692c025SYoshinobu Inoue */ 966df8bae1dSRodney W. Grimes *rp++ = 0; 9678692c025SYoshinobu Inoue unp_discard(fp); 968df8bae1dSRodney W. Grimes } 969df8bae1dSRodney W. Grimes return (EMSGSIZE); 970df8bae1dSRodney W. Grimes } 971ed5b7817SJulian Elischer /* 972ed5b7817SJulian Elischer * now change each pointer to an fd in the global table to 973ed5b7817SJulian Elischer * an integer that is the index to the local fd table entry 974ed5b7817SJulian Elischer * that we set up to point to the global one we are transferring. 9758692c025SYoshinobu Inoue * If sizeof (struct file *) is bigger than or equal to sizeof int, 9768692c025SYoshinobu Inoue * then do it in forward order. In that case, an integer will 9778692c025SYoshinobu Inoue * always come in the same place or before its corresponding 9788692c025SYoshinobu Inoue * struct file pointer. 9798692c025SYoshinobu Inoue * If sizeof (struct file *) is smaller than sizeof int, then 9808692c025SYoshinobu Inoue * do it in reverse order. 981ed5b7817SJulian Elischer */ 9828692c025SYoshinobu Inoue if (sizeof (struct file *) >= sizeof (int)) { 9838692c025SYoshinobu Inoue fdp = (int *)(cm + 1); 9848692c025SYoshinobu Inoue rp = (struct file **)CMSG_DATA(cm); 985df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 986df8bae1dSRodney W. Grimes if (fdalloc(p, 0, &f)) 987df8bae1dSRodney W. Grimes panic("unp_externalize"); 9888692c025SYoshinobu Inoue fp = *rp++; 989df8bae1dSRodney W. Grimes p->p_fd->fd_ofiles[f] = fp; 990df8bae1dSRodney W. Grimes fp->f_msgcount--; 991df8bae1dSRodney W. Grimes unp_rights--; 9928692c025SYoshinobu Inoue *fdp++ = f; 993df8bae1dSRodney W. Grimes } 9948692c025SYoshinobu Inoue } else { 9958692c025SYoshinobu Inoue fdp = (int *)(cm + 1) + newfds - 1; 9968692c025SYoshinobu Inoue rp = (struct file **)CMSG_DATA(cm) + newfds - 1; 9978692c025SYoshinobu Inoue for (i = 0; i < newfds; i++) { 9988692c025SYoshinobu Inoue if (fdalloc(p, 0, &f)) 9998692c025SYoshinobu Inoue panic("unp_externalize"); 10008692c025SYoshinobu Inoue fp = *rp--; 10018692c025SYoshinobu Inoue p->p_fd->fd_ofiles[f] = fp; 10028692c025SYoshinobu Inoue fp->f_msgcount--; 10038692c025SYoshinobu Inoue unp_rights--; 10048692c025SYoshinobu Inoue *fdp-- = f; 10058692c025SYoshinobu Inoue } 10068692c025SYoshinobu Inoue } 10078692c025SYoshinobu Inoue 10088692c025SYoshinobu Inoue /* 10098692c025SYoshinobu Inoue * Adjust length, in case sizeof(struct file *) and sizeof(int) 10108692c025SYoshinobu Inoue * differs. 10118692c025SYoshinobu Inoue */ 10128692c025SYoshinobu Inoue cm->cmsg_len = CMSG_LEN(newfds * sizeof(int)); 10138692c025SYoshinobu Inoue rights->m_len = cm->cmsg_len; 1014df8bae1dSRodney W. Grimes return (0); 1015df8bae1dSRodney W. Grimes } 1016df8bae1dSRodney W. Grimes 101798271db4SGarrett Wollman void 101898271db4SGarrett Wollman unp_init(void) 101998271db4SGarrett Wollman { 102098271db4SGarrett Wollman unp_zone = zinit("unpcb", sizeof(struct unpcb), nmbclusters, 0, 0); 102198271db4SGarrett Wollman if (unp_zone == 0) 102298271db4SGarrett Wollman panic("unp_init"); 102398271db4SGarrett Wollman LIST_INIT(&unp_dhead); 102498271db4SGarrett Wollman LIST_INIT(&unp_shead); 102598271db4SGarrett Wollman } 102698271db4SGarrett Wollman 10270b788fa1SBill Paul #ifndef MIN 10280b788fa1SBill Paul #define MIN(a,b) (((a)<(b))?(a):(b)) 10290b788fa1SBill Paul #endif 10300b788fa1SBill Paul 1031f708ef1bSPoul-Henning Kamp static int 1032df8bae1dSRodney W. Grimes unp_internalize(control, p) 1033df8bae1dSRodney W. Grimes struct mbuf *control; 1034df8bae1dSRodney W. Grimes struct proc *p; 1035df8bae1dSRodney W. Grimes { 10368692c025SYoshinobu Inoue struct filedesc *fdescp = p->p_fd; 1037df8bae1dSRodney W. Grimes register struct cmsghdr *cm = mtod(control, struct cmsghdr *); 1038df8bae1dSRodney W. Grimes register struct file **rp; 1039df8bae1dSRodney W. Grimes register struct file *fp; 10408692c025SYoshinobu Inoue register int i, fd, *fdp; 10410b788fa1SBill Paul register struct cmsgcred *cmcred; 1042df8bae1dSRodney W. Grimes int oldfds; 10438692c025SYoshinobu Inoue u_int newlen; 1044df8bae1dSRodney W. Grimes 10450b788fa1SBill Paul if ((cm->cmsg_type != SCM_RIGHTS && cm->cmsg_type != SCM_CREDS) || 10460b788fa1SBill Paul cm->cmsg_level != SOL_SOCKET || cm->cmsg_len != control->m_len) 1047df8bae1dSRodney W. Grimes return (EINVAL); 10480b788fa1SBill Paul 10490b788fa1SBill Paul /* 10500b788fa1SBill Paul * Fill in credential information. 10510b788fa1SBill Paul */ 10520b788fa1SBill Paul if (cm->cmsg_type == SCM_CREDS) { 10530b788fa1SBill Paul cmcred = (struct cmsgcred *)(cm + 1); 10540b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 1055b1fc0ec1SRobert Watson cmcred->cmcred_uid = p->p_ucred->cr_ruid; 1056b1fc0ec1SRobert Watson cmcred->cmcred_gid = p->p_ucred->cr_rgid; 10570b788fa1SBill Paul cmcred->cmcred_euid = p->p_ucred->cr_uid; 10580b788fa1SBill Paul cmcred->cmcred_ngroups = MIN(p->p_ucred->cr_ngroups, 10590b788fa1SBill Paul CMGROUP_MAX); 10600b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 10610b788fa1SBill Paul cmcred->cmcred_groups[i] = p->p_ucred->cr_groups[i]; 10620b788fa1SBill Paul return(0); 10630b788fa1SBill Paul } 10640b788fa1SBill Paul 1065df8bae1dSRodney W. Grimes oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int); 1066ed5b7817SJulian Elischer /* 1067ed5b7817SJulian Elischer * check that all the FDs passed in refer to legal OPEN files 1068ed5b7817SJulian Elischer * If not, reject the entire operation. 1069ed5b7817SJulian Elischer */ 10708692c025SYoshinobu Inoue fdp = (int *)(cm + 1); 1071df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 10728692c025SYoshinobu Inoue fd = *fdp++; 10738692c025SYoshinobu Inoue if ((unsigned)fd >= fdescp->fd_nfiles || 10748692c025SYoshinobu Inoue fdescp->fd_ofiles[fd] == NULL) 1075df8bae1dSRodney W. Grimes return (EBADF); 1076df8bae1dSRodney W. Grimes } 1077ed5b7817SJulian Elischer /* 1078ed5b7817SJulian Elischer * Now replace the integer FDs with pointers to 1079ed5b7817SJulian Elischer * the associated global file table entry.. 10808692c025SYoshinobu Inoue * Allocate a bigger buffer as necessary. But if an cluster is not 10818692c025SYoshinobu Inoue * enough, return E2BIG. 1082ed5b7817SJulian Elischer */ 10838692c025SYoshinobu Inoue newlen = CMSG_LEN(oldfds * sizeof(struct file *)); 10848692c025SYoshinobu Inoue if (newlen > MCLBYTES) 10858692c025SYoshinobu Inoue return (E2BIG); 10868692c025SYoshinobu Inoue if (newlen - control->m_len > M_TRAILINGSPACE(control)) { 10878692c025SYoshinobu Inoue if (control->m_flags & M_EXT) 10888692c025SYoshinobu Inoue return (E2BIG); 10892a0c503eSBosko Milekic MCLGET(control, M_TRYWAIT); 10908692c025SYoshinobu Inoue if ((control->m_flags & M_EXT) == 0) 10918692c025SYoshinobu Inoue return (ENOBUFS); 10928692c025SYoshinobu Inoue 10938692c025SYoshinobu Inoue /* copy the data to the cluster */ 10948692c025SYoshinobu Inoue memcpy(mtod(control, char *), cm, cm->cmsg_len); 10958692c025SYoshinobu Inoue cm = mtod(control, struct cmsghdr *); 10968692c025SYoshinobu Inoue } 10978692c025SYoshinobu Inoue 10988692c025SYoshinobu Inoue /* 10998692c025SYoshinobu Inoue * Adjust length, in case sizeof(struct file *) and sizeof(int) 11008692c025SYoshinobu Inoue * differs. 11018692c025SYoshinobu Inoue */ 11028692c025SYoshinobu Inoue control->m_len = cm->cmsg_len = newlen; 11038692c025SYoshinobu Inoue 11048692c025SYoshinobu Inoue /* 11058692c025SYoshinobu Inoue * Transform the file descriptors into struct file pointers. 11068692c025SYoshinobu Inoue * If sizeof (struct file *) is bigger than or equal to sizeof int, 11078692c025SYoshinobu Inoue * then do it in reverse order so that the int won't get until 11088692c025SYoshinobu Inoue * we're done. 11098692c025SYoshinobu Inoue * If sizeof (struct file *) is smaller than sizeof int, then 11108692c025SYoshinobu Inoue * do it in forward order. 11118692c025SYoshinobu Inoue */ 11128692c025SYoshinobu Inoue if (sizeof (struct file *) >= sizeof (int)) { 11138692c025SYoshinobu Inoue fdp = (int *)(cm + 1) + oldfds - 1; 11148692c025SYoshinobu Inoue rp = (struct file **)CMSG_DATA(cm) + oldfds - 1; 1115df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 11168692c025SYoshinobu Inoue fp = fdescp->fd_ofiles[*fdp--]; 11178692c025SYoshinobu Inoue *rp-- = fp; 11188692c025SYoshinobu Inoue fp->f_count++; 11198692c025SYoshinobu Inoue fp->f_msgcount++; 11208692c025SYoshinobu Inoue unp_rights++; 11218692c025SYoshinobu Inoue } 11228692c025SYoshinobu Inoue } else { 11238692c025SYoshinobu Inoue fdp = (int *)(cm + 1); 11248692c025SYoshinobu Inoue rp = (struct file **)CMSG_DATA(cm); 11258692c025SYoshinobu Inoue for (i = 0; i < oldfds; i++) { 11268692c025SYoshinobu Inoue fp = fdescp->fd_ofiles[*fdp++]; 1127df8bae1dSRodney W. Grimes *rp++ = fp; 1128df8bae1dSRodney W. Grimes fp->f_count++; 1129df8bae1dSRodney W. Grimes fp->f_msgcount++; 1130df8bae1dSRodney W. Grimes unp_rights++; 1131df8bae1dSRodney W. Grimes } 11328692c025SYoshinobu Inoue } 1133df8bae1dSRodney W. Grimes return (0); 1134df8bae1dSRodney W. Grimes } 1135df8bae1dSRodney W. Grimes 1136f708ef1bSPoul-Henning Kamp static int unp_defer, unp_gcing; 1137df8bae1dSRodney W. Grimes 1138f708ef1bSPoul-Henning Kamp static void 1139df8bae1dSRodney W. Grimes unp_gc() 1140df8bae1dSRodney W. Grimes { 1141df8bae1dSRodney W. Grimes register struct file *fp, *nextfp; 1142df8bae1dSRodney W. Grimes register struct socket *so; 1143df8bae1dSRodney W. Grimes struct file **extra_ref, **fpp; 1144df8bae1dSRodney W. Grimes int nunref, i; 1145df8bae1dSRodney W. Grimes 1146df8bae1dSRodney W. Grimes if (unp_gcing) 1147df8bae1dSRodney W. Grimes return; 1148df8bae1dSRodney W. Grimes unp_gcing = 1; 1149df8bae1dSRodney W. Grimes unp_defer = 0; 1150ed5b7817SJulian Elischer /* 1151ed5b7817SJulian Elischer * before going through all this, set all FDs to 1152ed5b7817SJulian Elischer * be NOT defered and NOT externally accessible 1153ed5b7817SJulian Elischer */ 11542e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) 1155df8bae1dSRodney W. Grimes fp->f_flag &= ~(FMARK|FDEFER); 1156df8bae1dSRodney W. Grimes do { 11572e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) { 1158ed5b7817SJulian Elischer /* 1159ed5b7817SJulian Elischer * If the file is not open, skip it 1160ed5b7817SJulian Elischer */ 1161df8bae1dSRodney W. Grimes if (fp->f_count == 0) 1162df8bae1dSRodney W. Grimes continue; 1163ed5b7817SJulian Elischer /* 1164ed5b7817SJulian Elischer * If we already marked it as 'defer' in a 1165ed5b7817SJulian Elischer * previous pass, then try process it this time 1166ed5b7817SJulian Elischer * and un-mark it 1167ed5b7817SJulian Elischer */ 1168df8bae1dSRodney W. Grimes if (fp->f_flag & FDEFER) { 1169df8bae1dSRodney W. Grimes fp->f_flag &= ~FDEFER; 1170df8bae1dSRodney W. Grimes unp_defer--; 1171df8bae1dSRodney W. Grimes } else { 1172ed5b7817SJulian Elischer /* 1173ed5b7817SJulian Elischer * if it's not defered, then check if it's 1174ed5b7817SJulian Elischer * already marked.. if so skip it 1175ed5b7817SJulian Elischer */ 1176df8bae1dSRodney W. Grimes if (fp->f_flag & FMARK) 1177df8bae1dSRodney W. Grimes continue; 1178ed5b7817SJulian Elischer /* 1179ed5b7817SJulian Elischer * If all references are from messages 1180ed5b7817SJulian Elischer * in transit, then skip it. it's not 1181ed5b7817SJulian Elischer * externally accessible. 1182ed5b7817SJulian Elischer */ 1183df8bae1dSRodney W. Grimes if (fp->f_count == fp->f_msgcount) 1184df8bae1dSRodney W. Grimes continue; 1185ed5b7817SJulian Elischer /* 1186ed5b7817SJulian Elischer * If it got this far then it must be 1187ed5b7817SJulian Elischer * externally accessible. 1188ed5b7817SJulian Elischer */ 1189df8bae1dSRodney W. Grimes fp->f_flag |= FMARK; 1190df8bae1dSRodney W. Grimes } 1191ed5b7817SJulian Elischer /* 1192ed5b7817SJulian Elischer * either it was defered, or it is externally 1193ed5b7817SJulian Elischer * accessible and not already marked so. 1194ed5b7817SJulian Elischer * Now check if it is possibly one of OUR sockets. 1195ed5b7817SJulian Elischer */ 1196df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_SOCKET || 1197df8bae1dSRodney W. Grimes (so = (struct socket *)fp->f_data) == 0) 1198df8bae1dSRodney W. Grimes continue; 1199748e0b0aSGarrett Wollman if (so->so_proto->pr_domain != &localdomain || 1200df8bae1dSRodney W. Grimes (so->so_proto->pr_flags&PR_RIGHTS) == 0) 1201df8bae1dSRodney W. Grimes continue; 1202df8bae1dSRodney W. Grimes #ifdef notdef 1203df8bae1dSRodney W. Grimes if (so->so_rcv.sb_flags & SB_LOCK) { 1204df8bae1dSRodney W. Grimes /* 1205df8bae1dSRodney W. Grimes * This is problematical; it's not clear 1206df8bae1dSRodney W. Grimes * we need to wait for the sockbuf to be 1207df8bae1dSRodney W. Grimes * unlocked (on a uniprocessor, at least), 1208df8bae1dSRodney W. Grimes * and it's also not clear what to do 1209df8bae1dSRodney W. Grimes * if sbwait returns an error due to receipt 1210df8bae1dSRodney W. Grimes * of a signal. If sbwait does return 1211df8bae1dSRodney W. Grimes * an error, we'll go into an infinite 1212df8bae1dSRodney W. Grimes * loop. Delete all of this for now. 1213df8bae1dSRodney W. Grimes */ 1214df8bae1dSRodney W. Grimes (void) sbwait(&so->so_rcv); 1215df8bae1dSRodney W. Grimes goto restart; 1216df8bae1dSRodney W. Grimes } 1217df8bae1dSRodney W. Grimes #endif 1218ed5b7817SJulian Elischer /* 1219ed5b7817SJulian Elischer * So, Ok, it's one of our sockets and it IS externally 1220ed5b7817SJulian Elischer * accessible (or was defered). Now we look 1221dc733423SDag-Erling Smørgrav * to see if we hold any file descriptors in its 1222ed5b7817SJulian Elischer * message buffers. Follow those links and mark them 1223ed5b7817SJulian Elischer * as accessible too. 1224ed5b7817SJulian Elischer */ 1225df8bae1dSRodney W. Grimes unp_scan(so->so_rcv.sb_mb, unp_mark); 1226df8bae1dSRodney W. Grimes } 1227df8bae1dSRodney W. Grimes } while (unp_defer); 1228df8bae1dSRodney W. Grimes /* 1229df8bae1dSRodney W. Grimes * We grab an extra reference to each of the file table entries 1230df8bae1dSRodney W. Grimes * that are not otherwise accessible and then free the rights 1231df8bae1dSRodney W. Grimes * that are stored in messages on them. 1232df8bae1dSRodney W. Grimes * 1233df8bae1dSRodney W. Grimes * The bug in the orginal code is a little tricky, so I'll describe 1234df8bae1dSRodney W. Grimes * what's wrong with it here. 1235df8bae1dSRodney W. Grimes * 1236df8bae1dSRodney W. Grimes * It is incorrect to simply unp_discard each entry for f_msgcount 1237df8bae1dSRodney W. Grimes * times -- consider the case of sockets A and B that contain 1238df8bae1dSRodney W. Grimes * references to each other. On a last close of some other socket, 1239df8bae1dSRodney W. Grimes * we trigger a gc since the number of outstanding rights (unp_rights) 1240df8bae1dSRodney W. Grimes * is non-zero. If during the sweep phase the gc code un_discards, 1241df8bae1dSRodney W. Grimes * we end up doing a (full) closef on the descriptor. A closef on A 1242df8bae1dSRodney W. Grimes * results in the following chain. Closef calls soo_close, which 1243df8bae1dSRodney W. Grimes * calls soclose. Soclose calls first (through the switch 1244df8bae1dSRodney W. Grimes * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 1245df8bae1dSRodney W. Grimes * returns because the previous instance had set unp_gcing, and 1246df8bae1dSRodney W. Grimes * we return all the way back to soclose, which marks the socket 1247df8bae1dSRodney W. Grimes * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush 1248df8bae1dSRodney W. Grimes * to free up the rights that are queued in messages on the socket A, 1249df8bae1dSRodney W. Grimes * i.e., the reference on B. The sorflush calls via the dom_dispose 1250df8bae1dSRodney W. Grimes * switch unp_dispose, which unp_scans with unp_discard. This second 1251df8bae1dSRodney W. Grimes * instance of unp_discard just calls closef on B. 1252df8bae1dSRodney W. Grimes * 1253df8bae1dSRodney W. Grimes * Well, a similar chain occurs on B, resulting in a sorflush on B, 1254df8bae1dSRodney W. Grimes * which results in another closef on A. Unfortunately, A is already 1255df8bae1dSRodney W. Grimes * being closed, and the descriptor has already been marked with 1256df8bae1dSRodney W. Grimes * SS_NOFDREF, and soclose panics at this point. 1257df8bae1dSRodney W. Grimes * 1258df8bae1dSRodney W. Grimes * Here, we first take an extra reference to each inaccessible 1259df8bae1dSRodney W. Grimes * descriptor. Then, we call sorflush ourself, since we know 1260df8bae1dSRodney W. Grimes * it is a Unix domain socket anyhow. After we destroy all the 1261df8bae1dSRodney W. Grimes * rights carried in messages, we do a last closef to get rid 1262df8bae1dSRodney W. Grimes * of our extra reference. This is the last close, and the 1263df8bae1dSRodney W. Grimes * unp_detach etc will shut down the socket. 1264df8bae1dSRodney W. Grimes * 1265df8bae1dSRodney W. Grimes * 91/09/19, bsy@cs.cmu.edu 1266df8bae1dSRodney W. Grimes */ 1267df8bae1dSRodney W. Grimes extra_ref = malloc(nfiles * sizeof(struct file *), M_FILE, M_WAITOK); 12682e3c8fcbSPoul-Henning Kamp for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; fp != 0; 1269bc6f0e79SJeffrey Hsu fp = nextfp) { 12702e3c8fcbSPoul-Henning Kamp nextfp = LIST_NEXT(fp, f_list); 1271ed5b7817SJulian Elischer /* 1272ed5b7817SJulian Elischer * If it's not open, skip it 1273ed5b7817SJulian Elischer */ 1274df8bae1dSRodney W. Grimes if (fp->f_count == 0) 1275df8bae1dSRodney W. Grimes continue; 1276ed5b7817SJulian Elischer /* 1277ed5b7817SJulian Elischer * If all refs are from msgs, and it's not marked accessible 1278ed5b7817SJulian Elischer * then it must be referenced from some unreachable cycle 1279ed5b7817SJulian Elischer * of (shut-down) FDs, so include it in our 1280ed5b7817SJulian Elischer * list of FDs to remove 1281ed5b7817SJulian Elischer */ 1282df8bae1dSRodney W. Grimes if (fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) { 1283df8bae1dSRodney W. Grimes *fpp++ = fp; 1284df8bae1dSRodney W. Grimes nunref++; 1285df8bae1dSRodney W. Grimes fp->f_count++; 1286df8bae1dSRodney W. Grimes } 1287df8bae1dSRodney W. Grimes } 1288ed5b7817SJulian Elischer /* 1289ed5b7817SJulian Elischer * for each FD on our hit list, do the following two things 1290ed5b7817SJulian Elischer */ 12911c7c3c6aSMatthew Dillon for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 12921c7c3c6aSMatthew Dillon struct file *tfp = *fpp; 12931c7c3c6aSMatthew Dillon if (tfp->f_type == DTYPE_SOCKET && tfp->f_data != NULL) 12941c7c3c6aSMatthew Dillon sorflush((struct socket *)(tfp->f_data)); 12951c7c3c6aSMatthew Dillon } 1296df8bae1dSRodney W. Grimes for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) 129792cbac68SPoul-Henning Kamp closef(*fpp, (struct proc *) NULL); 1298df8bae1dSRodney W. Grimes free((caddr_t)extra_ref, M_FILE); 1299df8bae1dSRodney W. Grimes unp_gcing = 0; 1300df8bae1dSRodney W. Grimes } 1301df8bae1dSRodney W. Grimes 130226f9a767SRodney W. Grimes void 1303df8bae1dSRodney W. Grimes unp_dispose(m) 1304df8bae1dSRodney W. Grimes struct mbuf *m; 1305df8bae1dSRodney W. Grimes { 1306996c772fSJohn Dyson 1307df8bae1dSRodney W. Grimes if (m) 1308df8bae1dSRodney W. Grimes unp_scan(m, unp_discard); 1309df8bae1dSRodney W. Grimes } 1310df8bae1dSRodney W. Grimes 13110c1bb4fbSDima Dorfman static int 13120c1bb4fbSDima Dorfman unp_listen(unp, p) 13130c1bb4fbSDima Dorfman struct unpcb *unp; 13140c1bb4fbSDima Dorfman struct proc *p; 13150c1bb4fbSDima Dorfman { 13160c1bb4fbSDima Dorfman 13170c1bb4fbSDima Dorfman bzero(&unp->unp_peercred, sizeof(unp->unp_peercred)); 13180c1bb4fbSDima Dorfman unp->unp_peercred.cr_uid = p->p_ucred->cr_uid; 13190c1bb4fbSDima Dorfman unp->unp_peercred.cr_ngroups = p->p_ucred->cr_ngroups; 13200c1bb4fbSDima Dorfman bcopy(p->p_ucred->cr_groups, unp->unp_peercred.cr_groups, 13210c1bb4fbSDima Dorfman sizeof(unp->unp_peercred.cr_groups)); 13220c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPCCACHED; 13230c1bb4fbSDima Dorfman return (0); 13240c1bb4fbSDima Dorfman } 13250c1bb4fbSDima Dorfman 1326f708ef1bSPoul-Henning Kamp static void 1327df8bae1dSRodney W. Grimes unp_scan(m0, op) 1328df8bae1dSRodney W. Grimes register struct mbuf *m0; 1329996c772fSJohn Dyson void (*op) __P((struct file *)); 1330df8bae1dSRodney W. Grimes { 1331df8bae1dSRodney W. Grimes register struct mbuf *m; 1332df8bae1dSRodney W. Grimes register struct file **rp; 1333df8bae1dSRodney W. Grimes register struct cmsghdr *cm; 1334df8bae1dSRodney W. Grimes register int i; 1335df8bae1dSRodney W. Grimes int qfds; 1336df8bae1dSRodney W. Grimes 1337df8bae1dSRodney W. Grimes while (m0) { 1338df8bae1dSRodney W. Grimes for (m = m0; m; m = m->m_next) 1339df8bae1dSRodney W. Grimes if (m->m_type == MT_CONTROL && 1340df8bae1dSRodney W. Grimes m->m_len >= sizeof(*cm)) { 1341df8bae1dSRodney W. Grimes cm = mtod(m, struct cmsghdr *); 1342df8bae1dSRodney W. Grimes if (cm->cmsg_level != SOL_SOCKET || 1343df8bae1dSRodney W. Grimes cm->cmsg_type != SCM_RIGHTS) 1344df8bae1dSRodney W. Grimes continue; 13458692c025SYoshinobu Inoue qfds = (cm->cmsg_len - 13468692c025SYoshinobu Inoue (CMSG_DATA(cm) - (u_char *)cm)) 1347df8bae1dSRodney W. Grimes / sizeof (struct file *); 13488692c025SYoshinobu Inoue rp = (struct file **)CMSG_DATA(cm); 1349df8bae1dSRodney W. Grimes for (i = 0; i < qfds; i++) 1350df8bae1dSRodney W. Grimes (*op)(*rp++); 1351df8bae1dSRodney W. Grimes break; /* XXX, but saves time */ 1352df8bae1dSRodney W. Grimes } 1353df8bae1dSRodney W. Grimes m0 = m0->m_act; 1354df8bae1dSRodney W. Grimes } 1355df8bae1dSRodney W. Grimes } 1356df8bae1dSRodney W. Grimes 1357f708ef1bSPoul-Henning Kamp static void 1358df8bae1dSRodney W. Grimes unp_mark(fp) 1359df8bae1dSRodney W. Grimes struct file *fp; 1360df8bae1dSRodney W. Grimes { 1361df8bae1dSRodney W. Grimes 1362df8bae1dSRodney W. Grimes if (fp->f_flag & FMARK) 1363df8bae1dSRodney W. Grimes return; 1364df8bae1dSRodney W. Grimes unp_defer++; 1365df8bae1dSRodney W. Grimes fp->f_flag |= (FMARK|FDEFER); 1366df8bae1dSRodney W. Grimes } 1367df8bae1dSRodney W. Grimes 1368f708ef1bSPoul-Henning Kamp static void 1369df8bae1dSRodney W. Grimes unp_discard(fp) 1370df8bae1dSRodney W. Grimes struct file *fp; 1371df8bae1dSRodney W. Grimes { 1372df8bae1dSRodney W. Grimes 1373df8bae1dSRodney W. Grimes fp->f_msgcount--; 1374df8bae1dSRodney W. Grimes unp_rights--; 1375df8bae1dSRodney W. Grimes (void) closef(fp, (struct proc *)NULL); 1376df8bae1dSRodney W. Grimes } 1377