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 34ed5b7817SJulian Elischer * $Id: uipc_usrreq.c,v 1.15 1996/03/11 15:12:47 davidg Exp $ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37df8bae1dSRodney W. Grimes #include <sys/param.h> 382ee45d7dSDavid Greenman #include <sys/queue.h> 39df8bae1dSRodney W. Grimes #include <sys/systm.h> 40df8bae1dSRodney W. Grimes #include <sys/proc.h> 41df8bae1dSRodney W. Grimes #include <sys/filedesc.h> 42df8bae1dSRodney W. Grimes #include <sys/domain.h> 43df8bae1dSRodney W. Grimes #include <sys/protosw.h> 44797f2d22SPoul-Henning Kamp #include <sys/stat.h> 45df8bae1dSRodney W. Grimes #include <sys/socket.h> 46df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 47df8bae1dSRodney W. Grimes #include <sys/unpcb.h> 48df8bae1dSRodney W. Grimes #include <sys/un.h> 49df8bae1dSRodney W. Grimes #include <sys/namei.h> 50df8bae1dSRodney W. Grimes #include <sys/vnode.h> 51df8bae1dSRodney W. Grimes #include <sys/file.h> 52df8bae1dSRodney W. Grimes #include <sys/stat.h> 53df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 54df8bae1dSRodney W. Grimes 55df8bae1dSRodney W. Grimes /* 56df8bae1dSRodney W. Grimes * Unix communications domain. 57df8bae1dSRodney W. Grimes * 58df8bae1dSRodney W. Grimes * TODO: 59df8bae1dSRodney W. Grimes * SEQPACKET, RDM 60df8bae1dSRodney W. Grimes * rethink name space problems 61df8bae1dSRodney W. Grimes * need a proper out-of-band 62df8bae1dSRodney W. Grimes */ 63f708ef1bSPoul-Henning Kamp static struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 64f708ef1bSPoul-Henning Kamp static ino_t unp_ino; /* prototype for fake inode numbers */ 65f708ef1bSPoul-Henning Kamp 66f708ef1bSPoul-Henning Kamp static int unp_attach __P((struct socket *)); 67f708ef1bSPoul-Henning Kamp static void unp_detach __P((struct unpcb *)); 68f708ef1bSPoul-Henning Kamp static int unp_bind __P((struct unpcb *,struct mbuf *, struct proc *)); 69f708ef1bSPoul-Henning Kamp static int unp_connect __P((struct socket *,struct mbuf *, struct proc *)); 70f708ef1bSPoul-Henning Kamp static void unp_disconnect __P((struct unpcb *)); 71f708ef1bSPoul-Henning Kamp static void unp_shutdown __P((struct unpcb *)); 72f708ef1bSPoul-Henning Kamp static void unp_drop __P((struct unpcb *, int)); 73f708ef1bSPoul-Henning Kamp static void unp_gc __P((void)); 74f708ef1bSPoul-Henning Kamp static void unp_scan __P((struct mbuf *, void (*)(struct file *))); 75f708ef1bSPoul-Henning Kamp static void unp_mark __P((struct file *)); 76f708ef1bSPoul-Henning Kamp static void unp_discard __P((struct file *)); 77f708ef1bSPoul-Henning Kamp static int unp_internalize __P((struct mbuf *, struct proc *)); 78f708ef1bSPoul-Henning Kamp 79df8bae1dSRodney W. Grimes 80df8bae1dSRodney W. Grimes /*ARGSUSED*/ 8126f9a767SRodney W. Grimes int 82df8bae1dSRodney W. Grimes uipc_usrreq(so, req, m, nam, control) 83df8bae1dSRodney W. Grimes struct socket *so; 84df8bae1dSRodney W. Grimes int req; 85df8bae1dSRodney W. Grimes struct mbuf *m, *nam, *control; 86df8bae1dSRodney W. Grimes { 87df8bae1dSRodney W. Grimes struct unpcb *unp = sotounpcb(so); 88df8bae1dSRodney W. Grimes register struct socket *so2; 89df8bae1dSRodney W. Grimes register int error = 0; 90df8bae1dSRodney W. Grimes struct proc *p = curproc; /* XXX */ 91df8bae1dSRodney W. Grimes 92df8bae1dSRodney W. Grimes if (req == PRU_CONTROL) 93df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 94df8bae1dSRodney W. Grimes if (req != PRU_SEND && control && control->m_len) { 95df8bae1dSRodney W. Grimes error = EOPNOTSUPP; 96df8bae1dSRodney W. Grimes goto release; 97df8bae1dSRodney W. Grimes } 98df8bae1dSRodney W. Grimes if (unp == 0 && req != PRU_ATTACH) { 99df8bae1dSRodney W. Grimes error = EINVAL; 100df8bae1dSRodney W. Grimes goto release; 101df8bae1dSRodney W. Grimes } 102df8bae1dSRodney W. Grimes switch (req) { 103df8bae1dSRodney W. Grimes 104df8bae1dSRodney W. Grimes case PRU_ATTACH: 105df8bae1dSRodney W. Grimes if (unp) { 106df8bae1dSRodney W. Grimes error = EISCONN; 107df8bae1dSRodney W. Grimes break; 108df8bae1dSRodney W. Grimes } 109df8bae1dSRodney W. Grimes error = unp_attach(so); 110df8bae1dSRodney W. Grimes break; 111df8bae1dSRodney W. Grimes 112df8bae1dSRodney W. Grimes case PRU_DETACH: 113df8bae1dSRodney W. Grimes unp_detach(unp); 114df8bae1dSRodney W. Grimes break; 115df8bae1dSRodney W. Grimes 116df8bae1dSRodney W. Grimes case PRU_BIND: 117df8bae1dSRodney W. Grimes error = unp_bind(unp, nam, p); 118df8bae1dSRodney W. Grimes break; 119df8bae1dSRodney W. Grimes 120df8bae1dSRodney W. Grimes case PRU_LISTEN: 121df8bae1dSRodney W. Grimes if (unp->unp_vnode == 0) 122df8bae1dSRodney W. Grimes error = EINVAL; 123df8bae1dSRodney W. Grimes break; 124df8bae1dSRodney W. Grimes 125df8bae1dSRodney W. Grimes case PRU_CONNECT: 126df8bae1dSRodney W. Grimes error = unp_connect(so, nam, p); 127df8bae1dSRodney W. Grimes break; 128df8bae1dSRodney W. Grimes 129df8bae1dSRodney W. Grimes case PRU_CONNECT2: 130df8bae1dSRodney W. Grimes error = unp_connect2(so, (struct socket *)nam); 131df8bae1dSRodney W. Grimes break; 132df8bae1dSRodney W. Grimes 133df8bae1dSRodney W. Grimes case PRU_DISCONNECT: 134df8bae1dSRodney W. Grimes unp_disconnect(unp); 135df8bae1dSRodney W. Grimes break; 136df8bae1dSRodney W. Grimes 137df8bae1dSRodney W. Grimes case PRU_ACCEPT: 138df8bae1dSRodney W. Grimes /* 139df8bae1dSRodney W. Grimes * Pass back name of connected socket, 140df8bae1dSRodney W. Grimes * if it was bound and we are still connected 141df8bae1dSRodney W. Grimes * (our peer may have closed already!). 142df8bae1dSRodney W. Grimes */ 143df8bae1dSRodney W. Grimes if (unp->unp_conn && unp->unp_conn->unp_addr) { 144df8bae1dSRodney W. Grimes nam->m_len = unp->unp_conn->unp_addr->m_len; 145df8bae1dSRodney W. Grimes bcopy(mtod(unp->unp_conn->unp_addr, caddr_t), 146df8bae1dSRodney W. Grimes mtod(nam, caddr_t), (unsigned)nam->m_len); 147df8bae1dSRodney W. Grimes } else { 148df8bae1dSRodney W. Grimes nam->m_len = sizeof(sun_noname); 149df8bae1dSRodney W. Grimes *(mtod(nam, struct sockaddr *)) = sun_noname; 150df8bae1dSRodney W. Grimes } 151df8bae1dSRodney W. Grimes break; 152df8bae1dSRodney W. Grimes 153df8bae1dSRodney W. Grimes case PRU_SHUTDOWN: 154df8bae1dSRodney W. Grimes socantsendmore(so); 155df8bae1dSRodney W. Grimes unp_shutdown(unp); 156df8bae1dSRodney W. Grimes break; 157df8bae1dSRodney W. Grimes 158df8bae1dSRodney W. Grimes case PRU_RCVD: 159df8bae1dSRodney W. Grimes switch (so->so_type) { 160df8bae1dSRodney W. Grimes 161df8bae1dSRodney W. Grimes case SOCK_DGRAM: 162df8bae1dSRodney W. Grimes panic("uipc 1"); 163df8bae1dSRodney W. Grimes /*NOTREACHED*/ 164df8bae1dSRodney W. Grimes 165df8bae1dSRodney W. Grimes case SOCK_STREAM: 166df8bae1dSRodney W. Grimes #define rcv (&so->so_rcv) 167df8bae1dSRodney W. Grimes #define snd (&so2->so_snd) 168df8bae1dSRodney W. Grimes if (unp->unp_conn == 0) 169df8bae1dSRodney W. Grimes break; 170df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 171df8bae1dSRodney W. Grimes /* 172df8bae1dSRodney W. Grimes * Adjust backpressure on sender 173df8bae1dSRodney W. Grimes * and wakeup any waiting to write. 174df8bae1dSRodney W. Grimes */ 175df8bae1dSRodney W. Grimes snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt; 176df8bae1dSRodney W. Grimes unp->unp_mbcnt = rcv->sb_mbcnt; 177df8bae1dSRodney W. Grimes snd->sb_hiwat += unp->unp_cc - rcv->sb_cc; 178df8bae1dSRodney W. Grimes unp->unp_cc = rcv->sb_cc; 179df8bae1dSRodney W. Grimes sowwakeup(so2); 180df8bae1dSRodney W. Grimes #undef snd 181df8bae1dSRodney W. Grimes #undef rcv 182df8bae1dSRodney W. Grimes break; 183df8bae1dSRodney W. Grimes 184df8bae1dSRodney W. Grimes default: 185df8bae1dSRodney W. Grimes panic("uipc 2"); 186df8bae1dSRodney W. Grimes } 187df8bae1dSRodney W. Grimes break; 188df8bae1dSRodney W. Grimes 189df8bae1dSRodney W. Grimes case PRU_SEND: 1906b8fda4dSGarrett Wollman case PRU_SEND_EOF: 191df8bae1dSRodney W. Grimes if (control && (error = unp_internalize(control, p))) 192df8bae1dSRodney W. Grimes break; 193df8bae1dSRodney W. Grimes switch (so->so_type) { 194df8bae1dSRodney W. Grimes 195df8bae1dSRodney W. Grimes case SOCK_DGRAM: { 196df8bae1dSRodney W. Grimes struct sockaddr *from; 197df8bae1dSRodney W. Grimes 198df8bae1dSRodney W. Grimes if (nam) { 199df8bae1dSRodney W. Grimes if (unp->unp_conn) { 200df8bae1dSRodney W. Grimes error = EISCONN; 201df8bae1dSRodney W. Grimes break; 202df8bae1dSRodney W. Grimes } 203df8bae1dSRodney W. Grimes error = unp_connect(so, nam, p); 204df8bae1dSRodney W. Grimes if (error) 205df8bae1dSRodney W. Grimes break; 206df8bae1dSRodney W. Grimes } else { 207df8bae1dSRodney W. Grimes if (unp->unp_conn == 0) { 208df8bae1dSRodney W. Grimes error = ENOTCONN; 209df8bae1dSRodney W. Grimes break; 210df8bae1dSRodney W. Grimes } 211df8bae1dSRodney W. Grimes } 212df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 213df8bae1dSRodney W. Grimes if (unp->unp_addr) 214df8bae1dSRodney W. Grimes from = mtod(unp->unp_addr, struct sockaddr *); 215df8bae1dSRodney W. Grimes else 216df8bae1dSRodney W. Grimes from = &sun_noname; 217df8bae1dSRodney W. Grimes if (sbappendaddr(&so2->so_rcv, from, m, control)) { 218df8bae1dSRodney W. Grimes sorwakeup(so2); 219df8bae1dSRodney W. Grimes m = 0; 220df8bae1dSRodney W. Grimes control = 0; 221df8bae1dSRodney W. Grimes } else 222df8bae1dSRodney W. Grimes error = ENOBUFS; 223df8bae1dSRodney W. Grimes if (nam) 224df8bae1dSRodney W. Grimes unp_disconnect(unp); 225df8bae1dSRodney W. Grimes break; 226df8bae1dSRodney W. Grimes } 227df8bae1dSRodney W. Grimes 228df8bae1dSRodney W. Grimes case SOCK_STREAM: 229df8bae1dSRodney W. Grimes #define rcv (&so2->so_rcv) 230df8bae1dSRodney W. Grimes #define snd (&so->so_snd) 2316b8fda4dSGarrett Wollman /* Connect if not connected yet. */ 2326b8fda4dSGarrett Wollman /* 2336b8fda4dSGarrett Wollman * Note: A better implementation would complain 234402cc72dSDavid Greenman * if not equal to the peer's address. 2356b8fda4dSGarrett Wollman */ 236402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 237402cc72dSDavid Greenman if (nam) { 238402cc72dSDavid Greenman error = unp_connect(so, nam, p); 239402cc72dSDavid Greenman if (error) 2406b8fda4dSGarrett Wollman break; /* XXX */ 241402cc72dSDavid Greenman } else { 242402cc72dSDavid Greenman error = ENOTCONN; 243402cc72dSDavid Greenman break; 244402cc72dSDavid Greenman } 245402cc72dSDavid Greenman } 246402cc72dSDavid Greenman 247df8bae1dSRodney W. Grimes if (so->so_state & SS_CANTSENDMORE) { 248df8bae1dSRodney W. Grimes error = EPIPE; 249df8bae1dSRodney W. Grimes break; 250df8bae1dSRodney W. Grimes } 251df8bae1dSRodney W. Grimes if (unp->unp_conn == 0) 252df8bae1dSRodney W. Grimes panic("uipc 3"); 253df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 254df8bae1dSRodney W. Grimes /* 255df8bae1dSRodney W. Grimes * Send to paired receive port, and then reduce 256df8bae1dSRodney W. Grimes * send buffer hiwater marks to maintain backpressure. 257df8bae1dSRodney W. Grimes * Wake up readers. 258df8bae1dSRodney W. Grimes */ 259df8bae1dSRodney W. Grimes if (control) { 260df8bae1dSRodney W. Grimes if (sbappendcontrol(rcv, m, control)) 261df8bae1dSRodney W. Grimes control = 0; 262df8bae1dSRodney W. Grimes } else 263df8bae1dSRodney W. Grimes sbappend(rcv, m); 264df8bae1dSRodney W. Grimes snd->sb_mbmax -= 265df8bae1dSRodney W. Grimes rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt; 266df8bae1dSRodney W. Grimes unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt; 267df8bae1dSRodney W. Grimes snd->sb_hiwat -= rcv->sb_cc - unp->unp_conn->unp_cc; 268df8bae1dSRodney W. Grimes unp->unp_conn->unp_cc = rcv->sb_cc; 269df8bae1dSRodney W. Grimes sorwakeup(so2); 270df8bae1dSRodney W. Grimes m = 0; 271df8bae1dSRodney W. Grimes #undef snd 272df8bae1dSRodney W. Grimes #undef rcv 273df8bae1dSRodney W. Grimes break; 274df8bae1dSRodney W. Grimes 275df8bae1dSRodney W. Grimes default: 276df8bae1dSRodney W. Grimes panic("uipc 4"); 277df8bae1dSRodney W. Grimes } 2786b8fda4dSGarrett Wollman /* 2796b8fda4dSGarrett Wollman * SEND_EOF is equivalent to a SEND followed by 2806b8fda4dSGarrett Wollman * a SHUTDOWN. 2816b8fda4dSGarrett Wollman */ 2826b8fda4dSGarrett Wollman if (req == PRU_SEND_EOF) { 2836b8fda4dSGarrett Wollman socantsendmore(so); 2846b8fda4dSGarrett Wollman unp_shutdown(unp); 2856b8fda4dSGarrett Wollman } 286df8bae1dSRodney W. Grimes break; 287df8bae1dSRodney W. Grimes 288df8bae1dSRodney W. Grimes case PRU_ABORT: 289df8bae1dSRodney W. Grimes unp_drop(unp, ECONNABORTED); 290df8bae1dSRodney W. Grimes break; 291df8bae1dSRodney W. Grimes 292df8bae1dSRodney W. Grimes case PRU_SENSE: 293df8bae1dSRodney W. Grimes ((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat; 294df8bae1dSRodney W. Grimes if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) { 295df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 296df8bae1dSRodney W. Grimes ((struct stat *) m)->st_blksize += so2->so_rcv.sb_cc; 297df8bae1dSRodney W. Grimes } 298df8bae1dSRodney W. Grimes ((struct stat *) m)->st_dev = NODEV; 299df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 300df8bae1dSRodney W. Grimes unp->unp_ino = unp_ino++; 301df8bae1dSRodney W. Grimes ((struct stat *) m)->st_ino = unp->unp_ino; 302df8bae1dSRodney W. Grimes return (0); 303df8bae1dSRodney W. Grimes 304df8bae1dSRodney W. Grimes case PRU_RCVOOB: 305df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 306df8bae1dSRodney W. Grimes 307df8bae1dSRodney W. Grimes case PRU_SENDOOB: 308df8bae1dSRodney W. Grimes error = EOPNOTSUPP; 309df8bae1dSRodney W. Grimes break; 310df8bae1dSRodney W. Grimes 311df8bae1dSRodney W. Grimes case PRU_SOCKADDR: 312df8bae1dSRodney W. Grimes if (unp->unp_addr) { 313df8bae1dSRodney W. Grimes nam->m_len = unp->unp_addr->m_len; 314df8bae1dSRodney W. Grimes bcopy(mtod(unp->unp_addr, caddr_t), 315df8bae1dSRodney W. Grimes mtod(nam, caddr_t), (unsigned)nam->m_len); 316df8bae1dSRodney W. Grimes } else 317df8bae1dSRodney W. Grimes nam->m_len = 0; 318df8bae1dSRodney W. Grimes break; 319df8bae1dSRodney W. Grimes 320df8bae1dSRodney W. Grimes case PRU_PEERADDR: 321df8bae1dSRodney W. Grimes if (unp->unp_conn && unp->unp_conn->unp_addr) { 322df8bae1dSRodney W. Grimes nam->m_len = unp->unp_conn->unp_addr->m_len; 323df8bae1dSRodney W. Grimes bcopy(mtod(unp->unp_conn->unp_addr, caddr_t), 324df8bae1dSRodney W. Grimes mtod(nam, caddr_t), (unsigned)nam->m_len); 325df8bae1dSRodney W. Grimes } else 326df8bae1dSRodney W. Grimes nam->m_len = 0; 327df8bae1dSRodney W. Grimes break; 328df8bae1dSRodney W. Grimes 329df8bae1dSRodney W. Grimes case PRU_SLOWTIMO: 330df8bae1dSRodney W. Grimes break; 331df8bae1dSRodney W. Grimes 332df8bae1dSRodney W. Grimes default: 333df8bae1dSRodney W. Grimes panic("piusrreq"); 334df8bae1dSRodney W. Grimes } 335df8bae1dSRodney W. Grimes release: 336df8bae1dSRodney W. Grimes if (control) 337df8bae1dSRodney W. Grimes m_freem(control); 338df8bae1dSRodney W. Grimes if (m) 339df8bae1dSRodney W. Grimes m_freem(m); 340df8bae1dSRodney W. Grimes return (error); 341df8bae1dSRodney W. Grimes } 342df8bae1dSRodney W. Grimes 343df8bae1dSRodney W. Grimes /* 344df8bae1dSRodney W. Grimes * Both send and receive buffers are allocated PIPSIZ bytes of buffering 345df8bae1dSRodney W. Grimes * for stream sockets, although the total for sender and receiver is 346df8bae1dSRodney W. Grimes * actually only PIPSIZ. 347df8bae1dSRodney W. Grimes * Datagram sockets really use the sendspace as the maximum datagram size, 348df8bae1dSRodney W. Grimes * and don't really want to reserve the sendspace. Their recvspace should 349df8bae1dSRodney W. Grimes * be large enough for at least one max-size datagram plus address. 350df8bae1dSRodney W. Grimes */ 3515dce41c5SJohn Dyson #ifndef PIPSIZ 3525dce41c5SJohn Dyson #define PIPSIZ 8192 3535dce41c5SJohn Dyson #endif 354f708ef1bSPoul-Henning Kamp static u_long unpst_sendspace = PIPSIZ; 355f708ef1bSPoul-Henning Kamp static u_long unpst_recvspace = PIPSIZ; 356f708ef1bSPoul-Henning Kamp static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 357f708ef1bSPoul-Henning Kamp static u_long unpdg_recvspace = 4*1024; 358df8bae1dSRodney W. Grimes 359f708ef1bSPoul-Henning Kamp static int unp_rights; /* file descriptors in flight */ 360df8bae1dSRodney W. Grimes 361f708ef1bSPoul-Henning Kamp static int 362df8bae1dSRodney W. Grimes unp_attach(so) 363df8bae1dSRodney W. Grimes struct socket *so; 364df8bae1dSRodney W. Grimes { 365df8bae1dSRodney W. Grimes register struct mbuf *m; 366df8bae1dSRodney W. Grimes register struct unpcb *unp; 367df8bae1dSRodney W. Grimes int error; 368df8bae1dSRodney W. Grimes 369df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 370df8bae1dSRodney W. Grimes switch (so->so_type) { 371df8bae1dSRodney W. Grimes 372df8bae1dSRodney W. Grimes case SOCK_STREAM: 373df8bae1dSRodney W. Grimes error = soreserve(so, unpst_sendspace, unpst_recvspace); 374df8bae1dSRodney W. Grimes break; 375df8bae1dSRodney W. Grimes 376df8bae1dSRodney W. Grimes case SOCK_DGRAM: 377df8bae1dSRodney W. Grimes error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 378df8bae1dSRodney W. Grimes break; 379df8bae1dSRodney W. Grimes 380df8bae1dSRodney W. Grimes default: 381df8bae1dSRodney W. Grimes panic("unp_attach"); 382df8bae1dSRodney W. Grimes } 383df8bae1dSRodney W. Grimes if (error) 384df8bae1dSRodney W. Grimes return (error); 385df8bae1dSRodney W. Grimes } 386df8bae1dSRodney W. Grimes m = m_getclr(M_DONTWAIT, MT_PCB); 387df8bae1dSRodney W. Grimes if (m == NULL) 388df8bae1dSRodney W. Grimes return (ENOBUFS); 389df8bae1dSRodney W. Grimes unp = mtod(m, struct unpcb *); 390df8bae1dSRodney W. Grimes so->so_pcb = (caddr_t)unp; 391df8bae1dSRodney W. Grimes unp->unp_socket = so; 392df8bae1dSRodney W. Grimes return (0); 393df8bae1dSRodney W. Grimes } 394df8bae1dSRodney W. Grimes 395f708ef1bSPoul-Henning Kamp static void 396df8bae1dSRodney W. Grimes unp_detach(unp) 397df8bae1dSRodney W. Grimes register struct unpcb *unp; 398df8bae1dSRodney W. Grimes { 399df8bae1dSRodney W. Grimes 400df8bae1dSRodney W. Grimes if (unp->unp_vnode) { 401df8bae1dSRodney W. Grimes unp->unp_vnode->v_socket = 0; 402df8bae1dSRodney W. Grimes vrele(unp->unp_vnode); 403df8bae1dSRodney W. Grimes unp->unp_vnode = 0; 404df8bae1dSRodney W. Grimes } 405df8bae1dSRodney W. Grimes if (unp->unp_conn) 406df8bae1dSRodney W. Grimes unp_disconnect(unp); 407df8bae1dSRodney W. Grimes while (unp->unp_refs) 408df8bae1dSRodney W. Grimes unp_drop(unp->unp_refs, ECONNRESET); 409df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 410df8bae1dSRodney W. Grimes unp->unp_socket->so_pcb = 0; 411df8bae1dSRodney W. Grimes if (unp_rights) { 412df8bae1dSRodney W. Grimes /* 413df8bae1dSRodney W. Grimes * Normally the receive buffer is flushed later, 414df8bae1dSRodney W. Grimes * in sofree, but if our receive buffer holds references 415df8bae1dSRodney W. Grimes * to descriptors that are now garbage, we will dispose 416df8bae1dSRodney W. Grimes * of those descriptor references after the garbage collector 417df8bae1dSRodney W. Grimes * gets them (resulting in a "panic: closef: count < 0"). 418df8bae1dSRodney W. Grimes */ 419df8bae1dSRodney W. Grimes sorflush(unp->unp_socket); 420df8bae1dSRodney W. Grimes unp_gc(); 421df8bae1dSRodney W. Grimes } 422b4c19655SDavid Greenman m_freem(unp->unp_addr); 423b4c19655SDavid Greenman (void) m_free(dtom(unp)); 424df8bae1dSRodney W. Grimes } 425df8bae1dSRodney W. Grimes 426f708ef1bSPoul-Henning Kamp static int 427df8bae1dSRodney W. Grimes unp_bind(unp, nam, p) 428df8bae1dSRodney W. Grimes struct unpcb *unp; 429df8bae1dSRodney W. Grimes struct mbuf *nam; 430df8bae1dSRodney W. Grimes struct proc *p; 431df8bae1dSRodney W. Grimes { 432df8bae1dSRodney W. Grimes struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *); 433df8bae1dSRodney W. Grimes register struct vnode *vp; 434df8bae1dSRodney W. Grimes struct vattr vattr; 435df8bae1dSRodney W. Grimes int error; 436df8bae1dSRodney W. Grimes struct nameidata nd; 437df8bae1dSRodney W. Grimes 438df8bae1dSRodney W. Grimes NDINIT(&nd, CREATE, FOLLOW | LOCKPARENT, UIO_SYSSPACE, 439df8bae1dSRodney W. Grimes soun->sun_path, p); 440df8bae1dSRodney W. Grimes if (unp->unp_vnode != NULL) 441df8bae1dSRodney W. Grimes return (EINVAL); 442df8bae1dSRodney W. Grimes if (nam->m_len == MLEN) { 443df8bae1dSRodney W. Grimes if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0) 444df8bae1dSRodney W. Grimes return (EINVAL); 445df8bae1dSRodney W. Grimes } else 446df8bae1dSRodney W. Grimes *(mtod(nam, caddr_t) + nam->m_len) = 0; 447df8bae1dSRodney W. Grimes /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 448797f2d22SPoul-Henning Kamp error = namei(&nd); 449797f2d22SPoul-Henning Kamp if (error) 450df8bae1dSRodney W. Grimes return (error); 451df8bae1dSRodney W. Grimes vp = nd.ni_vp; 452df8bae1dSRodney W. Grimes if (vp != NULL) { 453df8bae1dSRodney W. Grimes VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 454df8bae1dSRodney W. Grimes if (nd.ni_dvp == vp) 455df8bae1dSRodney W. Grimes vrele(nd.ni_dvp); 456df8bae1dSRodney W. Grimes else 457df8bae1dSRodney W. Grimes vput(nd.ni_dvp); 458df8bae1dSRodney W. Grimes vrele(vp); 459df8bae1dSRodney W. Grimes return (EADDRINUSE); 460df8bae1dSRodney W. Grimes } 461df8bae1dSRodney W. Grimes VATTR_NULL(&vattr); 462df8bae1dSRodney W. Grimes vattr.va_type = VSOCK; 463df8bae1dSRodney W. Grimes vattr.va_mode = ACCESSPERMS; 464df8bae1dSRodney W. Grimes LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); 465797f2d22SPoul-Henning Kamp error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 466797f2d22SPoul-Henning Kamp if (error) 467df8bae1dSRodney W. Grimes return (error); 468df8bae1dSRodney W. Grimes vp = nd.ni_vp; 469df8bae1dSRodney W. Grimes vp->v_socket = unp->unp_socket; 470df8bae1dSRodney W. Grimes unp->unp_vnode = vp; 471df8bae1dSRodney W. Grimes unp->unp_addr = m_copy(nam, 0, (int)M_COPYALL); 472df8bae1dSRodney W. Grimes VOP_UNLOCK(vp); 473df8bae1dSRodney W. Grimes return (0); 474df8bae1dSRodney W. Grimes } 475df8bae1dSRodney W. Grimes 476f708ef1bSPoul-Henning Kamp static int 477df8bae1dSRodney W. Grimes unp_connect(so, nam, p) 478df8bae1dSRodney W. Grimes struct socket *so; 479df8bae1dSRodney W. Grimes struct mbuf *nam; 480df8bae1dSRodney W. Grimes struct proc *p; 481df8bae1dSRodney W. Grimes { 482df8bae1dSRodney W. Grimes register struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *); 483df8bae1dSRodney W. Grimes register struct vnode *vp; 484df8bae1dSRodney W. Grimes register struct socket *so2, *so3; 485df8bae1dSRodney W. Grimes struct unpcb *unp2, *unp3; 486df8bae1dSRodney W. Grimes int error; 487df8bae1dSRodney W. Grimes struct nameidata nd; 488df8bae1dSRodney W. Grimes 489df8bae1dSRodney W. Grimes NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, soun->sun_path, p); 490df8bae1dSRodney W. Grimes if (nam->m_data + nam->m_len == &nam->m_dat[MLEN]) { /* XXX */ 491df8bae1dSRodney W. Grimes if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0) 492df8bae1dSRodney W. Grimes return (EMSGSIZE); 493df8bae1dSRodney W. Grimes } else 494df8bae1dSRodney W. Grimes *(mtod(nam, caddr_t) + nam->m_len) = 0; 495797f2d22SPoul-Henning Kamp error = namei(&nd); 496797f2d22SPoul-Henning Kamp if (error) 497df8bae1dSRodney W. Grimes return (error); 498df8bae1dSRodney W. Grimes vp = nd.ni_vp; 499df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 500df8bae1dSRodney W. Grimes error = ENOTSOCK; 501df8bae1dSRodney W. Grimes goto bad; 502df8bae1dSRodney W. Grimes } 503797f2d22SPoul-Henning Kamp error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p); 504797f2d22SPoul-Henning Kamp if (error) 505df8bae1dSRodney W. Grimes goto bad; 506df8bae1dSRodney W. Grimes so2 = vp->v_socket; 507df8bae1dSRodney W. Grimes if (so2 == 0) { 508df8bae1dSRodney W. Grimes error = ECONNREFUSED; 509df8bae1dSRodney W. Grimes goto bad; 510df8bae1dSRodney W. Grimes } 511df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 512df8bae1dSRodney W. Grimes error = EPROTOTYPE; 513df8bae1dSRodney W. Grimes goto bad; 514df8bae1dSRodney W. Grimes } 515df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 516df8bae1dSRodney W. Grimes if ((so2->so_options & SO_ACCEPTCONN) == 0 || 517df8bae1dSRodney W. Grimes (so3 = sonewconn(so2, 0)) == 0) { 518df8bae1dSRodney W. Grimes error = ECONNREFUSED; 519df8bae1dSRodney W. Grimes goto bad; 520df8bae1dSRodney W. Grimes } 521df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 522df8bae1dSRodney W. Grimes unp3 = sotounpcb(so3); 523df8bae1dSRodney W. Grimes if (unp2->unp_addr) 524df8bae1dSRodney W. Grimes unp3->unp_addr = 525df8bae1dSRodney W. Grimes m_copy(unp2->unp_addr, 0, (int)M_COPYALL); 526df8bae1dSRodney W. Grimes so2 = so3; 527df8bae1dSRodney W. Grimes } 528df8bae1dSRodney W. Grimes error = unp_connect2(so, so2); 529df8bae1dSRodney W. Grimes bad: 530df8bae1dSRodney W. Grimes vput(vp); 531df8bae1dSRodney W. Grimes return (error); 532df8bae1dSRodney W. Grimes } 533df8bae1dSRodney W. Grimes 53426f9a767SRodney W. Grimes int 535df8bae1dSRodney W. Grimes unp_connect2(so, so2) 536df8bae1dSRodney W. Grimes register struct socket *so; 537df8bae1dSRodney W. Grimes register struct socket *so2; 538df8bae1dSRodney W. Grimes { 539df8bae1dSRodney W. Grimes register struct unpcb *unp = sotounpcb(so); 540df8bae1dSRodney W. Grimes register struct unpcb *unp2; 541df8bae1dSRodney W. Grimes 542df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 543df8bae1dSRodney W. Grimes return (EPROTOTYPE); 544df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 545df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 546df8bae1dSRodney W. Grimes switch (so->so_type) { 547df8bae1dSRodney W. Grimes 548df8bae1dSRodney W. Grimes case SOCK_DGRAM: 549df8bae1dSRodney W. Grimes unp->unp_nextref = unp2->unp_refs; 550df8bae1dSRodney W. Grimes unp2->unp_refs = unp; 551df8bae1dSRodney W. Grimes soisconnected(so); 552df8bae1dSRodney W. Grimes break; 553df8bae1dSRodney W. Grimes 554df8bae1dSRodney W. Grimes case SOCK_STREAM: 555df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 556df8bae1dSRodney W. Grimes soisconnected(so); 557df8bae1dSRodney W. Grimes soisconnected(so2); 558df8bae1dSRodney W. Grimes break; 559df8bae1dSRodney W. Grimes 560df8bae1dSRodney W. Grimes default: 561df8bae1dSRodney W. Grimes panic("unp_connect2"); 562df8bae1dSRodney W. Grimes } 563df8bae1dSRodney W. Grimes return (0); 564df8bae1dSRodney W. Grimes } 565df8bae1dSRodney W. Grimes 566f708ef1bSPoul-Henning Kamp static void 567df8bae1dSRodney W. Grimes unp_disconnect(unp) 568df8bae1dSRodney W. Grimes struct unpcb *unp; 569df8bae1dSRodney W. Grimes { 570df8bae1dSRodney W. Grimes register struct unpcb *unp2 = unp->unp_conn; 571df8bae1dSRodney W. Grimes 572df8bae1dSRodney W. Grimes if (unp2 == 0) 573df8bae1dSRodney W. Grimes return; 574df8bae1dSRodney W. Grimes unp->unp_conn = 0; 575df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 576df8bae1dSRodney W. Grimes 577df8bae1dSRodney W. Grimes case SOCK_DGRAM: 578df8bae1dSRodney W. Grimes if (unp2->unp_refs == unp) 579df8bae1dSRodney W. Grimes unp2->unp_refs = unp->unp_nextref; 580df8bae1dSRodney W. Grimes else { 581df8bae1dSRodney W. Grimes unp2 = unp2->unp_refs; 582df8bae1dSRodney W. Grimes for (;;) { 583df8bae1dSRodney W. Grimes if (unp2 == 0) 584df8bae1dSRodney W. Grimes panic("unp_disconnect"); 585df8bae1dSRodney W. Grimes if (unp2->unp_nextref == unp) 586df8bae1dSRodney W. Grimes break; 587df8bae1dSRodney W. Grimes unp2 = unp2->unp_nextref; 588df8bae1dSRodney W. Grimes } 589df8bae1dSRodney W. Grimes unp2->unp_nextref = unp->unp_nextref; 590df8bae1dSRodney W. Grimes } 591df8bae1dSRodney W. Grimes unp->unp_nextref = 0; 592df8bae1dSRodney W. Grimes unp->unp_socket->so_state &= ~SS_ISCONNECTED; 593df8bae1dSRodney W. Grimes break; 594df8bae1dSRodney W. Grimes 595df8bae1dSRodney W. Grimes case SOCK_STREAM: 596df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 597df8bae1dSRodney W. Grimes unp2->unp_conn = 0; 598df8bae1dSRodney W. Grimes soisdisconnected(unp2->unp_socket); 599df8bae1dSRodney W. Grimes break; 600df8bae1dSRodney W. Grimes } 601df8bae1dSRodney W. Grimes } 602df8bae1dSRodney W. Grimes 603df8bae1dSRodney W. Grimes #ifdef notdef 60426f9a767SRodney W. Grimes void 605df8bae1dSRodney W. Grimes unp_abort(unp) 606df8bae1dSRodney W. Grimes struct unpcb *unp; 607df8bae1dSRodney W. Grimes { 608df8bae1dSRodney W. Grimes 609df8bae1dSRodney W. Grimes unp_detach(unp); 610df8bae1dSRodney W. Grimes } 611df8bae1dSRodney W. Grimes #endif 612df8bae1dSRodney W. Grimes 613f708ef1bSPoul-Henning Kamp static void 614df8bae1dSRodney W. Grimes unp_shutdown(unp) 615df8bae1dSRodney W. Grimes struct unpcb *unp; 616df8bae1dSRodney W. Grimes { 617df8bae1dSRodney W. Grimes struct socket *so; 618df8bae1dSRodney W. Grimes 619df8bae1dSRodney W. Grimes if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 620df8bae1dSRodney W. Grimes (so = unp->unp_conn->unp_socket)) 621df8bae1dSRodney W. Grimes socantrcvmore(so); 622df8bae1dSRodney W. Grimes } 623df8bae1dSRodney W. Grimes 624f708ef1bSPoul-Henning Kamp static void 625df8bae1dSRodney W. Grimes unp_drop(unp, errno) 626df8bae1dSRodney W. Grimes struct unpcb *unp; 627df8bae1dSRodney W. Grimes int errno; 628df8bae1dSRodney W. Grimes { 629df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 630df8bae1dSRodney W. Grimes 631df8bae1dSRodney W. Grimes so->so_error = errno; 632df8bae1dSRodney W. Grimes unp_disconnect(unp); 633df8bae1dSRodney W. Grimes if (so->so_head) { 634df8bae1dSRodney W. Grimes so->so_pcb = (caddr_t) 0; 635df8bae1dSRodney W. Grimes m_freem(unp->unp_addr); 636df8bae1dSRodney W. Grimes (void) m_free(dtom(unp)); 637df8bae1dSRodney W. Grimes sofree(so); 638df8bae1dSRodney W. Grimes } 639df8bae1dSRodney W. Grimes } 640df8bae1dSRodney W. Grimes 641df8bae1dSRodney W. Grimes #ifdef notdef 64226f9a767SRodney W. Grimes void 643df8bae1dSRodney W. Grimes unp_drain() 644df8bae1dSRodney W. Grimes { 645df8bae1dSRodney W. Grimes 646df8bae1dSRodney W. Grimes } 647df8bae1dSRodney W. Grimes #endif 648df8bae1dSRodney W. Grimes 64926f9a767SRodney W. Grimes int 650df8bae1dSRodney W. Grimes unp_externalize(rights) 651df8bae1dSRodney W. Grimes struct mbuf *rights; 652df8bae1dSRodney W. Grimes { 653df8bae1dSRodney W. Grimes struct proc *p = curproc; /* XXX */ 654df8bae1dSRodney W. Grimes register int i; 655df8bae1dSRodney W. Grimes register struct cmsghdr *cm = mtod(rights, struct cmsghdr *); 656df8bae1dSRodney W. Grimes register struct file **rp = (struct file **)(cm + 1); 657df8bae1dSRodney W. Grimes register struct file *fp; 658df8bae1dSRodney W. Grimes int newfds = (cm->cmsg_len - sizeof(*cm)) / sizeof (int); 659df8bae1dSRodney W. Grimes int f; 660df8bae1dSRodney W. Grimes 661ed5b7817SJulian Elischer /* 662ed5b7817SJulian Elischer * if the new FD's will not fit, then we free them all 663ed5b7817SJulian Elischer */ 664df8bae1dSRodney W. Grimes if (!fdavail(p, newfds)) { 665df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 666df8bae1dSRodney W. Grimes fp = *rp; 667df8bae1dSRodney W. Grimes unp_discard(fp); 668df8bae1dSRodney W. Grimes *rp++ = 0; 669df8bae1dSRodney W. Grimes } 670df8bae1dSRodney W. Grimes return (EMSGSIZE); 671df8bae1dSRodney W. Grimes } 672ed5b7817SJulian Elischer /* 673ed5b7817SJulian Elischer * now change each pointer to an fd in the global table to 674ed5b7817SJulian Elischer * an integer that is the index to the local fd table entry 675ed5b7817SJulian Elischer * that we set up to point to the global one we are transferring. 676ed5b7817SJulian Elischer * XXX this assumes a pointer and int are the same size...! 677ed5b7817SJulian Elischer */ 678df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 679df8bae1dSRodney W. Grimes if (fdalloc(p, 0, &f)) 680df8bae1dSRodney W. Grimes panic("unp_externalize"); 681df8bae1dSRodney W. Grimes fp = *rp; 682df8bae1dSRodney W. Grimes p->p_fd->fd_ofiles[f] = fp; 683df8bae1dSRodney W. Grimes fp->f_msgcount--; 684df8bae1dSRodney W. Grimes unp_rights--; 685df8bae1dSRodney W. Grimes *(int *)rp++ = f; 686df8bae1dSRodney W. Grimes } 687df8bae1dSRodney W. Grimes return (0); 688df8bae1dSRodney W. Grimes } 689df8bae1dSRodney W. Grimes 690f708ef1bSPoul-Henning Kamp static int 691df8bae1dSRodney W. Grimes unp_internalize(control, p) 692df8bae1dSRodney W. Grimes struct mbuf *control; 693df8bae1dSRodney W. Grimes struct proc *p; 694df8bae1dSRodney W. Grimes { 695df8bae1dSRodney W. Grimes struct filedesc *fdp = p->p_fd; 696df8bae1dSRodney W. Grimes register struct cmsghdr *cm = mtod(control, struct cmsghdr *); 697df8bae1dSRodney W. Grimes register struct file **rp; 698df8bae1dSRodney W. Grimes register struct file *fp; 699df8bae1dSRodney W. Grimes register int i, fd; 700df8bae1dSRodney W. Grimes int oldfds; 701df8bae1dSRodney W. Grimes 702df8bae1dSRodney W. Grimes if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET || 703df8bae1dSRodney W. Grimes cm->cmsg_len != control->m_len) 704df8bae1dSRodney W. Grimes return (EINVAL); 705df8bae1dSRodney W. Grimes oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int); 706ed5b7817SJulian Elischer /* 707ed5b7817SJulian Elischer * check that all the FDs passed in refer to legal OPEN files 708ed5b7817SJulian Elischer * If not, reject the entire operation. 709ed5b7817SJulian Elischer */ 710df8bae1dSRodney W. Grimes rp = (struct file **)(cm + 1); 711df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 712df8bae1dSRodney W. Grimes fd = *(int *)rp++; 713df8bae1dSRodney W. Grimes if ((unsigned)fd >= fdp->fd_nfiles || 714df8bae1dSRodney W. Grimes fdp->fd_ofiles[fd] == NULL) 715df8bae1dSRodney W. Grimes return (EBADF); 716df8bae1dSRodney W. Grimes } 717ed5b7817SJulian Elischer /* 718ed5b7817SJulian Elischer * Now replace the integer FDs with pointers to 719ed5b7817SJulian Elischer * the associated global file table entry.. 720ed5b7817SJulian Elischer * XXX this assumes a pointer and an int are the same size! 721ed5b7817SJulian Elischer */ 722df8bae1dSRodney W. Grimes rp = (struct file **)(cm + 1); 723df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 724df8bae1dSRodney W. Grimes fp = fdp->fd_ofiles[*(int *)rp]; 725df8bae1dSRodney W. Grimes *rp++ = fp; 726df8bae1dSRodney W. Grimes fp->f_count++; 727df8bae1dSRodney W. Grimes fp->f_msgcount++; 728df8bae1dSRodney W. Grimes unp_rights++; 729df8bae1dSRodney W. Grimes } 730df8bae1dSRodney W. Grimes return (0); 731df8bae1dSRodney W. Grimes } 732df8bae1dSRodney W. Grimes 733f708ef1bSPoul-Henning Kamp static int unp_defer, unp_gcing; 734df8bae1dSRodney W. Grimes 735f708ef1bSPoul-Henning Kamp static void 736df8bae1dSRodney W. Grimes unp_gc() 737df8bae1dSRodney W. Grimes { 738df8bae1dSRodney W. Grimes register struct file *fp, *nextfp; 739df8bae1dSRodney W. Grimes register struct socket *so; 740df8bae1dSRodney W. Grimes struct file **extra_ref, **fpp; 741df8bae1dSRodney W. Grimes int nunref, i; 742df8bae1dSRodney W. Grimes 743df8bae1dSRodney W. Grimes if (unp_gcing) 744df8bae1dSRodney W. Grimes return; 745df8bae1dSRodney W. Grimes unp_gcing = 1; 746df8bae1dSRodney W. Grimes unp_defer = 0; 747ed5b7817SJulian Elischer /* 748ed5b7817SJulian Elischer * before going through all this, set all FDs to 749ed5b7817SJulian Elischer * be NOT defered and NOT externally accessible 750ed5b7817SJulian Elischer */ 751bc6f0e79SJeffrey Hsu for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) 752df8bae1dSRodney W. Grimes fp->f_flag &= ~(FMARK|FDEFER); 753df8bae1dSRodney W. Grimes do { 754bc6f0e79SJeffrey Hsu for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) { 755ed5b7817SJulian Elischer /* 756ed5b7817SJulian Elischer * If the file is not open, skip it 757ed5b7817SJulian Elischer */ 758df8bae1dSRodney W. Grimes if (fp->f_count == 0) 759df8bae1dSRodney W. Grimes continue; 760ed5b7817SJulian Elischer /* 761ed5b7817SJulian Elischer * If we already marked it as 'defer' in a 762ed5b7817SJulian Elischer * previous pass, then try process it this time 763ed5b7817SJulian Elischer * and un-mark it 764ed5b7817SJulian Elischer */ 765df8bae1dSRodney W. Grimes if (fp->f_flag & FDEFER) { 766df8bae1dSRodney W. Grimes fp->f_flag &= ~FDEFER; 767df8bae1dSRodney W. Grimes unp_defer--; 768df8bae1dSRodney W. Grimes } else { 769ed5b7817SJulian Elischer /* 770ed5b7817SJulian Elischer * if it's not defered, then check if it's 771ed5b7817SJulian Elischer * already marked.. if so skip it 772ed5b7817SJulian Elischer */ 773df8bae1dSRodney W. Grimes if (fp->f_flag & FMARK) 774df8bae1dSRodney W. Grimes continue; 775ed5b7817SJulian Elischer /* 776ed5b7817SJulian Elischer * If all references are from messages 777ed5b7817SJulian Elischer * in transit, then skip it. it's not 778ed5b7817SJulian Elischer * externally accessible. 779ed5b7817SJulian Elischer */ 780df8bae1dSRodney W. Grimes if (fp->f_count == fp->f_msgcount) 781df8bae1dSRodney W. Grimes continue; 782ed5b7817SJulian Elischer /* 783ed5b7817SJulian Elischer * If it got this far then it must be 784ed5b7817SJulian Elischer * externally accessible. 785ed5b7817SJulian Elischer */ 786df8bae1dSRodney W. Grimes fp->f_flag |= FMARK; 787df8bae1dSRodney W. Grimes } 788ed5b7817SJulian Elischer /* 789ed5b7817SJulian Elischer * either it was defered, or it is externally 790ed5b7817SJulian Elischer * accessible and not already marked so. 791ed5b7817SJulian Elischer * Now check if it is possibly one of OUR sockets. 792ed5b7817SJulian Elischer */ 793df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_SOCKET || 794df8bae1dSRodney W. Grimes (so = (struct socket *)fp->f_data) == 0) 795df8bae1dSRodney W. Grimes continue; 796748e0b0aSGarrett Wollman if (so->so_proto->pr_domain != &localdomain || 797df8bae1dSRodney W. Grimes (so->so_proto->pr_flags&PR_RIGHTS) == 0) 798df8bae1dSRodney W. Grimes continue; 799df8bae1dSRodney W. Grimes #ifdef notdef 800df8bae1dSRodney W. Grimes if (so->so_rcv.sb_flags & SB_LOCK) { 801df8bae1dSRodney W. Grimes /* 802df8bae1dSRodney W. Grimes * This is problematical; it's not clear 803df8bae1dSRodney W. Grimes * we need to wait for the sockbuf to be 804df8bae1dSRodney W. Grimes * unlocked (on a uniprocessor, at least), 805df8bae1dSRodney W. Grimes * and it's also not clear what to do 806df8bae1dSRodney W. Grimes * if sbwait returns an error due to receipt 807df8bae1dSRodney W. Grimes * of a signal. If sbwait does return 808df8bae1dSRodney W. Grimes * an error, we'll go into an infinite 809df8bae1dSRodney W. Grimes * loop. Delete all of this for now. 810df8bae1dSRodney W. Grimes */ 811df8bae1dSRodney W. Grimes (void) sbwait(&so->so_rcv); 812df8bae1dSRodney W. Grimes goto restart; 813df8bae1dSRodney W. Grimes } 814df8bae1dSRodney W. Grimes #endif 815ed5b7817SJulian Elischer /* 816ed5b7817SJulian Elischer * So, Ok, it's one of our sockets and it IS externally 817ed5b7817SJulian Elischer * accessible (or was defered). Now we look 818ed5b7817SJulian Elischer * to see if we hold any file descriptors in it's 819ed5b7817SJulian Elischer * message buffers. Follow those links and mark them 820ed5b7817SJulian Elischer * as accessible too. 821ed5b7817SJulian Elischer */ 822df8bae1dSRodney W. Grimes unp_scan(so->so_rcv.sb_mb, unp_mark); 823df8bae1dSRodney W. Grimes } 824df8bae1dSRodney W. Grimes } while (unp_defer); 825df8bae1dSRodney W. Grimes /* 826df8bae1dSRodney W. Grimes * We grab an extra reference to each of the file table entries 827df8bae1dSRodney W. Grimes * that are not otherwise accessible and then free the rights 828df8bae1dSRodney W. Grimes * that are stored in messages on them. 829df8bae1dSRodney W. Grimes * 830df8bae1dSRodney W. Grimes * The bug in the orginal code is a little tricky, so I'll describe 831df8bae1dSRodney W. Grimes * what's wrong with it here. 832df8bae1dSRodney W. Grimes * 833df8bae1dSRodney W. Grimes * It is incorrect to simply unp_discard each entry for f_msgcount 834df8bae1dSRodney W. Grimes * times -- consider the case of sockets A and B that contain 835df8bae1dSRodney W. Grimes * references to each other. On a last close of some other socket, 836df8bae1dSRodney W. Grimes * we trigger a gc since the number of outstanding rights (unp_rights) 837df8bae1dSRodney W. Grimes * is non-zero. If during the sweep phase the gc code un_discards, 838df8bae1dSRodney W. Grimes * we end up doing a (full) closef on the descriptor. A closef on A 839df8bae1dSRodney W. Grimes * results in the following chain. Closef calls soo_close, which 840df8bae1dSRodney W. Grimes * calls soclose. Soclose calls first (through the switch 841df8bae1dSRodney W. Grimes * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 842df8bae1dSRodney W. Grimes * returns because the previous instance had set unp_gcing, and 843df8bae1dSRodney W. Grimes * we return all the way back to soclose, which marks the socket 844df8bae1dSRodney W. Grimes * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush 845df8bae1dSRodney W. Grimes * to free up the rights that are queued in messages on the socket A, 846df8bae1dSRodney W. Grimes * i.e., the reference on B. The sorflush calls via the dom_dispose 847df8bae1dSRodney W. Grimes * switch unp_dispose, which unp_scans with unp_discard. This second 848df8bae1dSRodney W. Grimes * instance of unp_discard just calls closef on B. 849df8bae1dSRodney W. Grimes * 850df8bae1dSRodney W. Grimes * Well, a similar chain occurs on B, resulting in a sorflush on B, 851df8bae1dSRodney W. Grimes * which results in another closef on A. Unfortunately, A is already 852df8bae1dSRodney W. Grimes * being closed, and the descriptor has already been marked with 853df8bae1dSRodney W. Grimes * SS_NOFDREF, and soclose panics at this point. 854df8bae1dSRodney W. Grimes * 855df8bae1dSRodney W. Grimes * Here, we first take an extra reference to each inaccessible 856df8bae1dSRodney W. Grimes * descriptor. Then, we call sorflush ourself, since we know 857df8bae1dSRodney W. Grimes * it is a Unix domain socket anyhow. After we destroy all the 858df8bae1dSRodney W. Grimes * rights carried in messages, we do a last closef to get rid 859df8bae1dSRodney W. Grimes * of our extra reference. This is the last close, and the 860df8bae1dSRodney W. Grimes * unp_detach etc will shut down the socket. 861df8bae1dSRodney W. Grimes * 862df8bae1dSRodney W. Grimes * 91/09/19, bsy@cs.cmu.edu 863df8bae1dSRodney W. Grimes */ 864df8bae1dSRodney W. Grimes extra_ref = malloc(nfiles * sizeof(struct file *), M_FILE, M_WAITOK); 865bc6f0e79SJeffrey Hsu for (nunref = 0, fp = filehead.lh_first, fpp = extra_ref; fp != 0; 866bc6f0e79SJeffrey Hsu fp = nextfp) { 867bc6f0e79SJeffrey Hsu nextfp = fp->f_list.le_next; 868ed5b7817SJulian Elischer /* 869ed5b7817SJulian Elischer * If it's not open, skip it 870ed5b7817SJulian Elischer */ 871df8bae1dSRodney W. Grimes if (fp->f_count == 0) 872df8bae1dSRodney W. Grimes continue; 873ed5b7817SJulian Elischer /* 874ed5b7817SJulian Elischer * If all refs are from msgs, and it's not marked accessible 875ed5b7817SJulian Elischer * then it must be referenced from some unreachable cycle 876ed5b7817SJulian Elischer * of (shut-down) FDs, so include it in our 877ed5b7817SJulian Elischer * list of FDs to remove 878ed5b7817SJulian Elischer */ 879df8bae1dSRodney W. Grimes if (fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) { 880df8bae1dSRodney W. Grimes *fpp++ = fp; 881df8bae1dSRodney W. Grimes nunref++; 882df8bae1dSRodney W. Grimes fp->f_count++; 883df8bae1dSRodney W. Grimes } 884df8bae1dSRodney W. Grimes } 885ed5b7817SJulian Elischer /* 886ed5b7817SJulian Elischer * for each FD on our hit list, do the following two things 887ed5b7817SJulian Elischer */ 888df8bae1dSRodney W. Grimes for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) 889df8bae1dSRodney W. Grimes sorflush((struct socket *)(*fpp)->f_data); 890df8bae1dSRodney W. Grimes for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) 89192cbac68SPoul-Henning Kamp closef(*fpp,(struct proc*) NULL); 892df8bae1dSRodney W. Grimes free((caddr_t)extra_ref, M_FILE); 893df8bae1dSRodney W. Grimes unp_gcing = 0; 894df8bae1dSRodney W. Grimes } 895df8bae1dSRodney W. Grimes 89626f9a767SRodney W. Grimes void 897df8bae1dSRodney W. Grimes unp_dispose(m) 898df8bae1dSRodney W. Grimes struct mbuf *m; 899df8bae1dSRodney W. Grimes { 900df8bae1dSRodney W. Grimes if (m) 901df8bae1dSRodney W. Grimes unp_scan(m, unp_discard); 902df8bae1dSRodney W. Grimes } 903df8bae1dSRodney W. Grimes 904f708ef1bSPoul-Henning Kamp static void 905df8bae1dSRodney W. Grimes unp_scan(m0, op) 906df8bae1dSRodney W. Grimes register struct mbuf *m0; 90726f9a767SRodney W. Grimes void (*op)(struct file *); 908df8bae1dSRodney W. Grimes { 909df8bae1dSRodney W. Grimes register struct mbuf *m; 910df8bae1dSRodney W. Grimes register struct file **rp; 911df8bae1dSRodney W. Grimes register struct cmsghdr *cm; 912df8bae1dSRodney W. Grimes register int i; 913df8bae1dSRodney W. Grimes int qfds; 914df8bae1dSRodney W. Grimes 915df8bae1dSRodney W. Grimes while (m0) { 916df8bae1dSRodney W. Grimes for (m = m0; m; m = m->m_next) 917df8bae1dSRodney W. Grimes if (m->m_type == MT_CONTROL && 918df8bae1dSRodney W. Grimes m->m_len >= sizeof(*cm)) { 919df8bae1dSRodney W. Grimes cm = mtod(m, struct cmsghdr *); 920df8bae1dSRodney W. Grimes if (cm->cmsg_level != SOL_SOCKET || 921df8bae1dSRodney W. Grimes cm->cmsg_type != SCM_RIGHTS) 922df8bae1dSRodney W. Grimes continue; 923df8bae1dSRodney W. Grimes qfds = (cm->cmsg_len - sizeof *cm) 924df8bae1dSRodney W. Grimes / sizeof (struct file *); 925df8bae1dSRodney W. Grimes rp = (struct file **)(cm + 1); 926df8bae1dSRodney W. Grimes for (i = 0; i < qfds; i++) 927df8bae1dSRodney W. Grimes (*op)(*rp++); 928df8bae1dSRodney W. Grimes break; /* XXX, but saves time */ 929df8bae1dSRodney W. Grimes } 930df8bae1dSRodney W. Grimes m0 = m0->m_act; 931df8bae1dSRodney W. Grimes } 932df8bae1dSRodney W. Grimes } 933df8bae1dSRodney W. Grimes 934f708ef1bSPoul-Henning Kamp static void 935df8bae1dSRodney W. Grimes unp_mark(fp) 936df8bae1dSRodney W. Grimes struct file *fp; 937df8bae1dSRodney W. Grimes { 938df8bae1dSRodney W. Grimes 939df8bae1dSRodney W. Grimes if (fp->f_flag & FMARK) 940df8bae1dSRodney W. Grimes return; 941df8bae1dSRodney W. Grimes unp_defer++; 942df8bae1dSRodney W. Grimes fp->f_flag |= (FMARK|FDEFER); 943df8bae1dSRodney W. Grimes } 944df8bae1dSRodney W. Grimes 945f708ef1bSPoul-Henning Kamp static void 946df8bae1dSRodney W. Grimes unp_discard(fp) 947df8bae1dSRodney W. Grimes struct file *fp; 948df8bae1dSRodney W. Grimes { 949df8bae1dSRodney W. Grimes 950df8bae1dSRodney W. Grimes fp->f_msgcount--; 951df8bae1dSRodney W. Grimes unp_rights--; 952df8bae1dSRodney W. Grimes (void) closef(fp, (struct proc *)NULL); 953df8bae1dSRodney W. Grimes } 954