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 37335654d7SRobert Watson #include "opt_mac.h" 38335654d7SRobert Watson 39df8bae1dSRodney W. Grimes #include <sys/param.h> 40fb919e4dSMark Murray #include <sys/domain.h> 41960ed29cSSeigo Tanimura #include <sys/fcntl.h> 42d826c479SBruce Evans #include <sys/malloc.h> /* XXX must be before <sys/file.h> */ 43639acc13SGarrett Wollman #include <sys/file.h> 44960ed29cSSeigo Tanimura #include <sys/filedesc.h> 45960ed29cSSeigo Tanimura #include <sys/jail.h> 46960ed29cSSeigo Tanimura #include <sys/kernel.h> 47960ed29cSSeigo Tanimura #include <sys/lock.h> 486ea48a90SRobert Watson #include <sys/mac.h> 49639acc13SGarrett Wollman #include <sys/mbuf.h> 50960ed29cSSeigo Tanimura #include <sys/mutex.h> 51639acc13SGarrett Wollman #include <sys/namei.h> 52639acc13SGarrett Wollman #include <sys/proc.h> 53df8bae1dSRodney W. Grimes #include <sys/protosw.h> 54960ed29cSSeigo Tanimura #include <sys/resourcevar.h> 55df8bae1dSRodney W. Grimes #include <sys/socket.h> 56df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 57960ed29cSSeigo Tanimura #include <sys/signalvar.h> 58df8bae1dSRodney W. Grimes #include <sys/stat.h> 59960ed29cSSeigo Tanimura #include <sys/sx.h> 60639acc13SGarrett Wollman #include <sys/sysctl.h> 61960ed29cSSeigo Tanimura #include <sys/systm.h> 62639acc13SGarrett Wollman #include <sys/un.h> 6398271db4SGarrett Wollman #include <sys/unpcb.h> 64639acc13SGarrett Wollman #include <sys/vnode.h> 65df8bae1dSRodney W. Grimes 669e9d298aSJeff Roberson #include <vm/uma.h> 6798271db4SGarrett Wollman 689e9d298aSJeff Roberson static uma_zone_t unp_zone; 6998271db4SGarrett Wollman static unp_gen_t unp_gencnt; 7098271db4SGarrett Wollman static u_int unp_count; 7198271db4SGarrett Wollman 7298271db4SGarrett Wollman static struct unp_head unp_shead, unp_dhead; 7398271db4SGarrett Wollman 74df8bae1dSRodney W. Grimes /* 75df8bae1dSRodney W. Grimes * Unix communications domain. 76df8bae1dSRodney W. Grimes * 77df8bae1dSRodney W. Grimes * TODO: 78df8bae1dSRodney W. Grimes * SEQPACKET, RDM 79df8bae1dSRodney W. Grimes * rethink name space problems 80df8bae1dSRodney W. Grimes * need a proper out-of-band 8198271db4SGarrett Wollman * lock pushdown 82df8bae1dSRodney W. Grimes */ 83f708ef1bSPoul-Henning Kamp static struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL }; 84f708ef1bSPoul-Henning Kamp static ino_t unp_ino; /* prototype for fake inode numbers */ 85f708ef1bSPoul-Henning Kamp 864d77a549SAlfred Perlstein static int unp_attach(struct socket *); 874d77a549SAlfred Perlstein static void unp_detach(struct unpcb *); 884d77a549SAlfred Perlstein static int unp_bind(struct unpcb *,struct sockaddr *, struct thread *); 8970f52b48SBruce Evans static int unp_connect(struct socket *,struct sockaddr *, struct thread *); 904d77a549SAlfred Perlstein static void unp_disconnect(struct unpcb *); 914d77a549SAlfred Perlstein static void unp_shutdown(struct unpcb *); 924d77a549SAlfred Perlstein static void unp_drop(struct unpcb *, int); 934d77a549SAlfred Perlstein static void unp_gc(void); 944d77a549SAlfred Perlstein static void unp_scan(struct mbuf *, void (*)(struct file *)); 954d77a549SAlfred Perlstein static void unp_mark(struct file *); 964d77a549SAlfred Perlstein static void unp_discard(struct file *); 974d77a549SAlfred Perlstein static void unp_freerights(struct file **, int); 984d77a549SAlfred Perlstein static int unp_internalize(struct mbuf **, struct thread *); 994d77a549SAlfred Perlstein static int unp_listen(struct unpcb *, struct thread *); 100f708ef1bSPoul-Henning Kamp 101a29f300eSGarrett Wollman static int 102a29f300eSGarrett Wollman uipc_abort(struct socket *so) 103df8bae1dSRodney W. Grimes { 104df8bae1dSRodney W. Grimes struct unpcb *unp = sotounpcb(so); 105df8bae1dSRodney W. Grimes 106a29f300eSGarrett Wollman if (unp == 0) 107a29f300eSGarrett Wollman return EINVAL; 108a29f300eSGarrett Wollman unp_drop(unp, ECONNABORTED); 109ddb7d629SIan Dowse unp_detach(unp); 110ddb7d629SIan Dowse sotryfree(so); 111a29f300eSGarrett Wollman return 0; 112df8bae1dSRodney W. Grimes } 113df8bae1dSRodney W. Grimes 114a29f300eSGarrett Wollman static int 11557bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam) 116a29f300eSGarrett Wollman { 117a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 118df8bae1dSRodney W. Grimes 119a29f300eSGarrett Wollman if (unp == 0) 120a29f300eSGarrett Wollman return EINVAL; 121df8bae1dSRodney W. Grimes 122df8bae1dSRodney W. Grimes /* 123df8bae1dSRodney W. Grimes * Pass back name of connected socket, 124df8bae1dSRodney W. Grimes * if it was bound and we are still connected 125df8bae1dSRodney W. Grimes * (our peer may have closed already!). 126df8bae1dSRodney W. Grimes */ 127df8bae1dSRodney W. Grimes if (unp->unp_conn && unp->unp_conn->unp_addr) { 12857bf258eSGarrett Wollman *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr, 12957bf258eSGarrett Wollman 1); 130df8bae1dSRodney W. Grimes } else { 13157bf258eSGarrett Wollman *nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1); 132df8bae1dSRodney W. Grimes } 133a29f300eSGarrett Wollman return 0; 134a29f300eSGarrett Wollman } 135df8bae1dSRodney W. Grimes 136a29f300eSGarrett Wollman static int 137b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td) 138a29f300eSGarrett Wollman { 139a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 140df8bae1dSRodney W. Grimes 141a29f300eSGarrett Wollman if (unp != 0) 142a29f300eSGarrett Wollman return EISCONN; 143a29f300eSGarrett Wollman return unp_attach(so); 144a29f300eSGarrett Wollman } 145a29f300eSGarrett Wollman 146a29f300eSGarrett Wollman static int 147b40ce416SJulian Elischer uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 148a29f300eSGarrett Wollman { 149a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 150a29f300eSGarrett Wollman 151a29f300eSGarrett Wollman if (unp == 0) 152a29f300eSGarrett Wollman return EINVAL; 153a29f300eSGarrett Wollman 154b40ce416SJulian Elischer return unp_bind(unp, nam, td); 155a29f300eSGarrett Wollman } 156a29f300eSGarrett Wollman 157a29f300eSGarrett Wollman static int 158b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 159a29f300eSGarrett Wollman { 160a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 161a29f300eSGarrett Wollman 162a29f300eSGarrett Wollman if (unp == 0) 163a29f300eSGarrett Wollman return EINVAL; 164b40ce416SJulian Elischer return unp_connect(so, nam, curthread); 165a29f300eSGarrett Wollman } 166a29f300eSGarrett Wollman 167a29f300eSGarrett Wollman static int 168a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2) 169a29f300eSGarrett Wollman { 170a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so1); 171a29f300eSGarrett Wollman 172a29f300eSGarrett Wollman if (unp == 0) 173a29f300eSGarrett Wollman return EINVAL; 174a29f300eSGarrett Wollman 175a29f300eSGarrett Wollman return unp_connect2(so1, so2); 176a29f300eSGarrett Wollman } 177a29f300eSGarrett Wollman 178a29f300eSGarrett Wollman /* control is EOPNOTSUPP */ 179a29f300eSGarrett Wollman 180a29f300eSGarrett Wollman static int 181a29f300eSGarrett Wollman uipc_detach(struct socket *so) 182a29f300eSGarrett Wollman { 183a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 184a29f300eSGarrett Wollman 185a29f300eSGarrett Wollman if (unp == 0) 186a29f300eSGarrett Wollman return EINVAL; 187a29f300eSGarrett Wollman 188a29f300eSGarrett Wollman unp_detach(unp); 189a29f300eSGarrett Wollman return 0; 190a29f300eSGarrett Wollman } 191a29f300eSGarrett Wollman 192a29f300eSGarrett Wollman static int 193a29f300eSGarrett Wollman uipc_disconnect(struct socket *so) 194a29f300eSGarrett Wollman { 195a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 196a29f300eSGarrett Wollman 197a29f300eSGarrett Wollman if (unp == 0) 198a29f300eSGarrett Wollman return EINVAL; 199a29f300eSGarrett Wollman unp_disconnect(unp); 200a29f300eSGarrett Wollman return 0; 201a29f300eSGarrett Wollman } 202a29f300eSGarrett Wollman 203a29f300eSGarrett Wollman static int 204b40ce416SJulian Elischer uipc_listen(struct socket *so, struct thread *td) 205a29f300eSGarrett Wollman { 206a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 207a29f300eSGarrett Wollman 208a29f300eSGarrett Wollman if (unp == 0 || unp->unp_vnode == 0) 209a29f300eSGarrett Wollman return EINVAL; 2106f105b34SJohn Baldwin return unp_listen(unp, td); 211a29f300eSGarrett Wollman } 212a29f300eSGarrett Wollman 213a29f300eSGarrett Wollman static int 21457bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam) 215a29f300eSGarrett Wollman { 216a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 217a29f300eSGarrett Wollman 218a29f300eSGarrett Wollman if (unp == 0) 219a29f300eSGarrett Wollman return EINVAL; 22057bf258eSGarrett Wollman if (unp->unp_conn && unp->unp_conn->unp_addr) 22157bf258eSGarrett Wollman *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr, 22257bf258eSGarrett Wollman 1); 223bdc5f6a3SHajimu UMEMOTO else { 224bdc5f6a3SHajimu UMEMOTO /* 225bdc5f6a3SHajimu UMEMOTO * XXX: It seems that this test always fails even when 226bdc5f6a3SHajimu UMEMOTO * connection is established. So, this else clause is 227bdc5f6a3SHajimu UMEMOTO * added as workaround to return PF_LOCAL sockaddr. 228bdc5f6a3SHajimu UMEMOTO */ 22956b3905fSHajimu UMEMOTO *nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1); 230bdc5f6a3SHajimu UMEMOTO } 231a29f300eSGarrett Wollman return 0; 232a29f300eSGarrett Wollman } 233a29f300eSGarrett Wollman 234a29f300eSGarrett Wollman static int 235a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags) 236a29f300eSGarrett Wollman { 237a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 238a29f300eSGarrett Wollman struct socket *so2; 2396aef685fSBrian Feldman u_long newhiwat; 240a29f300eSGarrett Wollman 241a29f300eSGarrett Wollman if (unp == 0) 242a29f300eSGarrett Wollman return EINVAL; 243df8bae1dSRodney W. Grimes switch (so->so_type) { 244df8bae1dSRodney W. Grimes case SOCK_DGRAM: 245a29f300eSGarrett Wollman panic("uipc_rcvd DGRAM?"); 246df8bae1dSRodney W. Grimes /*NOTREACHED*/ 247df8bae1dSRodney W. Grimes 248df8bae1dSRodney W. Grimes case SOCK_STREAM: 249df8bae1dSRodney W. Grimes if (unp->unp_conn == 0) 250df8bae1dSRodney W. Grimes break; 251df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 252df8bae1dSRodney W. Grimes /* 253df8bae1dSRodney W. Grimes * Adjust backpressure on sender 254df8bae1dSRodney W. Grimes * and wakeup any waiting to write. 255df8bae1dSRodney W. Grimes */ 256ff8b0106SBrian Feldman so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt; 257ff8b0106SBrian Feldman unp->unp_mbcnt = so->so_rcv.sb_mbcnt; 2586aef685fSBrian Feldman newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - 2596aef685fSBrian Feldman so->so_rcv.sb_cc; 260f535380cSDon Lewis (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat, 2616aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 262ff8b0106SBrian Feldman unp->unp_cc = so->so_rcv.sb_cc; 263df8bae1dSRodney W. Grimes sowwakeup(so2); 264df8bae1dSRodney W. Grimes break; 265df8bae1dSRodney W. Grimes 266df8bae1dSRodney W. Grimes default: 267a29f300eSGarrett Wollman panic("uipc_rcvd unknown socktype"); 268df8bae1dSRodney W. Grimes } 269a29f300eSGarrett Wollman return 0; 270a29f300eSGarrett Wollman } 271df8bae1dSRodney W. Grimes 272a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */ 273a29f300eSGarrett Wollman 274a29f300eSGarrett Wollman static int 27557bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 276b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 277a29f300eSGarrett Wollman { 278a29f300eSGarrett Wollman int error = 0; 279a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 280a29f300eSGarrett Wollman struct socket *so2; 2816aef685fSBrian Feldman u_long newhiwat; 282a29f300eSGarrett Wollman 283a29f300eSGarrett Wollman if (unp == 0) { 284a29f300eSGarrett Wollman error = EINVAL; 285a29f300eSGarrett Wollman goto release; 286a29f300eSGarrett Wollman } 287a29f300eSGarrett Wollman if (flags & PRUS_OOB) { 288a29f300eSGarrett Wollman error = EOPNOTSUPP; 289a29f300eSGarrett Wollman goto release; 290a29f300eSGarrett Wollman } 291a29f300eSGarrett Wollman 2922bc21ed9SDavid Malone if (control && (error = unp_internalize(&control, td))) 293a29f300eSGarrett Wollman goto release; 294df8bae1dSRodney W. Grimes 295a29f300eSGarrett Wollman switch (so->so_type) { 296a29f300eSGarrett Wollman case SOCK_DGRAM: 297a29f300eSGarrett Wollman { 298df8bae1dSRodney W. Grimes struct sockaddr *from; 299df8bae1dSRodney W. Grimes 300df8bae1dSRodney W. Grimes if (nam) { 301df8bae1dSRodney W. Grimes if (unp->unp_conn) { 302df8bae1dSRodney W. Grimes error = EISCONN; 303df8bae1dSRodney W. Grimes break; 304df8bae1dSRodney W. Grimes } 305b40ce416SJulian Elischer error = unp_connect(so, nam, td); 306df8bae1dSRodney W. Grimes if (error) 307df8bae1dSRodney W. Grimes break; 308df8bae1dSRodney W. Grimes } else { 309df8bae1dSRodney W. Grimes if (unp->unp_conn == 0) { 310df8bae1dSRodney W. Grimes error = ENOTCONN; 311df8bae1dSRodney W. Grimes break; 312df8bae1dSRodney W. Grimes } 313df8bae1dSRodney W. Grimes } 314df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 315df8bae1dSRodney W. Grimes if (unp->unp_addr) 31657bf258eSGarrett Wollman from = (struct sockaddr *)unp->unp_addr; 317df8bae1dSRodney W. Grimes else 318df8bae1dSRodney W. Grimes from = &sun_noname; 319df8bae1dSRodney W. Grimes if (sbappendaddr(&so2->so_rcv, from, m, control)) { 320df8bae1dSRodney W. Grimes sorwakeup(so2); 321df8bae1dSRodney W. Grimes m = 0; 322df8bae1dSRodney W. Grimes control = 0; 323df8bae1dSRodney W. Grimes } else 324df8bae1dSRodney W. Grimes error = ENOBUFS; 325df8bae1dSRodney W. Grimes if (nam) 326df8bae1dSRodney W. Grimes unp_disconnect(unp); 327df8bae1dSRodney W. Grimes break; 328df8bae1dSRodney W. Grimes } 329df8bae1dSRodney W. Grimes 330df8bae1dSRodney W. Grimes case SOCK_STREAM: 3316b8fda4dSGarrett Wollman /* Connect if not connected yet. */ 3326b8fda4dSGarrett Wollman /* 3336b8fda4dSGarrett Wollman * Note: A better implementation would complain 334402cc72dSDavid Greenman * if not equal to the peer's address. 3356b8fda4dSGarrett Wollman */ 336402cc72dSDavid Greenman if ((so->so_state & SS_ISCONNECTED) == 0) { 337402cc72dSDavid Greenman if (nam) { 338b40ce416SJulian Elischer error = unp_connect(so, nam, td); 339402cc72dSDavid Greenman if (error) 3406b8fda4dSGarrett Wollman break; /* XXX */ 341402cc72dSDavid Greenman } else { 342402cc72dSDavid Greenman error = ENOTCONN; 343402cc72dSDavid Greenman break; 344402cc72dSDavid Greenman } 345402cc72dSDavid Greenman } 346402cc72dSDavid Greenman 347df8bae1dSRodney W. Grimes if (so->so_state & SS_CANTSENDMORE) { 348df8bae1dSRodney W. Grimes error = EPIPE; 349df8bae1dSRodney W. Grimes break; 350df8bae1dSRodney W. Grimes } 351df8bae1dSRodney W. Grimes if (unp->unp_conn == 0) 352a29f300eSGarrett Wollman panic("uipc_send connected but no connection?"); 353df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 354df8bae1dSRodney W. Grimes /* 355df8bae1dSRodney W. Grimes * Send to paired receive port, and then reduce 356df8bae1dSRodney W. Grimes * send buffer hiwater marks to maintain backpressure. 357df8bae1dSRodney W. Grimes * Wake up readers. 358df8bae1dSRodney W. Grimes */ 359df8bae1dSRodney W. Grimes if (control) { 360ff8b0106SBrian Feldman if (sbappendcontrol(&so2->so_rcv, m, control)) 361df8bae1dSRodney W. Grimes control = 0; 362df8bae1dSRodney W. Grimes } else 363ff8b0106SBrian Feldman sbappend(&so2->so_rcv, m); 364ff8b0106SBrian Feldman so->so_snd.sb_mbmax -= 365ff8b0106SBrian Feldman so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt; 366ff8b0106SBrian Feldman unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt; 3676aef685fSBrian Feldman newhiwat = so->so_snd.sb_hiwat - 3686aef685fSBrian Feldman (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc); 369f535380cSDon Lewis (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat, 3706aef685fSBrian Feldman newhiwat, RLIM_INFINITY); 371ff8b0106SBrian Feldman unp->unp_conn->unp_cc = so2->so_rcv.sb_cc; 372df8bae1dSRodney W. Grimes sorwakeup(so2); 373df8bae1dSRodney W. Grimes m = 0; 374df8bae1dSRodney W. Grimes break; 375df8bae1dSRodney W. Grimes 376df8bae1dSRodney W. Grimes default: 377a29f300eSGarrett Wollman panic("uipc_send unknown socktype"); 378df8bae1dSRodney W. Grimes } 379a29f300eSGarrett Wollman 3806b8fda4dSGarrett Wollman /* 3816b8fda4dSGarrett Wollman * SEND_EOF is equivalent to a SEND followed by 3826b8fda4dSGarrett Wollman * a SHUTDOWN. 3836b8fda4dSGarrett Wollman */ 384a29f300eSGarrett Wollman if (flags & PRUS_EOF) { 3856b8fda4dSGarrett Wollman socantsendmore(so); 3866b8fda4dSGarrett Wollman unp_shutdown(unp); 3876b8fda4dSGarrett Wollman } 388df8bae1dSRodney W. Grimes 389bd508d39SDon Lewis if (control && error != 0) 390bd508d39SDon Lewis unp_dispose(control); 391bd508d39SDon Lewis 392a29f300eSGarrett Wollman release: 393a29f300eSGarrett Wollman if (control) 394a29f300eSGarrett Wollman m_freem(control); 395a29f300eSGarrett Wollman if (m) 396a29f300eSGarrett Wollman m_freem(m); 397a29f300eSGarrett Wollman return error; 398a29f300eSGarrett Wollman } 399df8bae1dSRodney W. Grimes 400a29f300eSGarrett Wollman static int 401a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb) 402a29f300eSGarrett Wollman { 403a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 404a29f300eSGarrett Wollman struct socket *so2; 405a29f300eSGarrett Wollman 406a29f300eSGarrett Wollman if (unp == 0) 407a29f300eSGarrett Wollman return EINVAL; 408a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 409df8bae1dSRodney W. Grimes if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) { 410df8bae1dSRodney W. Grimes so2 = unp->unp_conn->unp_socket; 411a29f300eSGarrett Wollman sb->st_blksize += so2->so_rcv.sb_cc; 412df8bae1dSRodney W. Grimes } 413bfbb9ce6SPoul-Henning Kamp sb->st_dev = NOUDEV; 414df8bae1dSRodney W. Grimes if (unp->unp_ino == 0) 4156f782c46SJeffrey Hsu unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino; 416a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino; 417df8bae1dSRodney W. Grimes return (0); 418a29f300eSGarrett Wollman } 419df8bae1dSRodney W. Grimes 420a29f300eSGarrett Wollman static int 421a29f300eSGarrett Wollman uipc_shutdown(struct socket *so) 422a29f300eSGarrett Wollman { 423a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 424df8bae1dSRodney W. Grimes 425a29f300eSGarrett Wollman if (unp == 0) 426a29f300eSGarrett Wollman return EINVAL; 427a29f300eSGarrett Wollman socantsendmore(so); 428a29f300eSGarrett Wollman unp_shutdown(unp); 429a29f300eSGarrett Wollman return 0; 430a29f300eSGarrett Wollman } 431df8bae1dSRodney W. Grimes 432a29f300eSGarrett Wollman static int 43357bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam) 434a29f300eSGarrett Wollman { 435a29f300eSGarrett Wollman struct unpcb *unp = sotounpcb(so); 436a29f300eSGarrett Wollman 437a29f300eSGarrett Wollman if (unp == 0) 438a29f300eSGarrett Wollman return EINVAL; 43957bf258eSGarrett Wollman if (unp->unp_addr) 44057bf258eSGarrett Wollman *nam = dup_sockaddr((struct sockaddr *)unp->unp_addr, 1); 44183f3198bSThomas Moestl else 44283f3198bSThomas Moestl *nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1); 443a29f300eSGarrett Wollman return 0; 444df8bae1dSRodney W. Grimes } 445a29f300eSGarrett Wollman 446a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = { 447a29f300eSGarrett Wollman uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect, 448a29f300eSGarrett Wollman uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect, 449a29f300eSGarrett Wollman uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp, 450a29f300eSGarrett Wollman uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr, 45151338ea8SPeter Wemm sosend, soreceive, sopoll 452a29f300eSGarrett Wollman }; 453df8bae1dSRodney W. Grimes 4540c1bb4fbSDima Dorfman int 4550c1bb4fbSDima Dorfman uipc_ctloutput(so, sopt) 4560c1bb4fbSDima Dorfman struct socket *so; 4570c1bb4fbSDima Dorfman struct sockopt *sopt; 4580c1bb4fbSDima Dorfman { 4590c1bb4fbSDima Dorfman struct unpcb *unp = sotounpcb(so); 4600c1bb4fbSDima Dorfman int error; 4610c1bb4fbSDima Dorfman 4620c1bb4fbSDima Dorfman switch (sopt->sopt_dir) { 4630c1bb4fbSDima Dorfman case SOPT_GET: 4640c1bb4fbSDima Dorfman switch (sopt->sopt_name) { 4650c1bb4fbSDima Dorfman case LOCAL_PEERCRED: 4660c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC) 4670c1bb4fbSDima Dorfman error = sooptcopyout(sopt, &unp->unp_peercred, 4680c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 4690c1bb4fbSDima Dorfman else { 4700c1bb4fbSDima Dorfman if (so->so_type == SOCK_STREAM) 4710c1bb4fbSDima Dorfman error = ENOTCONN; 4720c1bb4fbSDima Dorfman else 4730c1bb4fbSDima Dorfman error = EINVAL; 4740c1bb4fbSDima Dorfman } 4750c1bb4fbSDima Dorfman break; 4760c1bb4fbSDima Dorfman default: 4770c1bb4fbSDima Dorfman error = EOPNOTSUPP; 4780c1bb4fbSDima Dorfman break; 4790c1bb4fbSDima Dorfman } 4800c1bb4fbSDima Dorfman break; 4810c1bb4fbSDima Dorfman case SOPT_SET: 4820c1bb4fbSDima Dorfman default: 4830c1bb4fbSDima Dorfman error = EOPNOTSUPP; 4840c1bb4fbSDima Dorfman break; 4850c1bb4fbSDima Dorfman } 4860c1bb4fbSDima Dorfman return (error); 4870c1bb4fbSDima Dorfman } 4880c1bb4fbSDima Dorfman 489df8bae1dSRodney W. Grimes /* 490df8bae1dSRodney W. Grimes * Both send and receive buffers are allocated PIPSIZ bytes of buffering 491df8bae1dSRodney W. Grimes * for stream sockets, although the total for sender and receiver is 492df8bae1dSRodney W. Grimes * actually only PIPSIZ. 493df8bae1dSRodney W. Grimes * Datagram sockets really use the sendspace as the maximum datagram size, 494df8bae1dSRodney W. Grimes * and don't really want to reserve the sendspace. Their recvspace should 495df8bae1dSRodney W. Grimes * be large enough for at least one max-size datagram plus address. 496df8bae1dSRodney W. Grimes */ 4975dce41c5SJohn Dyson #ifndef PIPSIZ 4985dce41c5SJohn Dyson #define PIPSIZ 8192 4995dce41c5SJohn Dyson #endif 500f708ef1bSPoul-Henning Kamp static u_long unpst_sendspace = PIPSIZ; 501f708ef1bSPoul-Henning Kamp static u_long unpst_recvspace = PIPSIZ; 502f708ef1bSPoul-Henning Kamp static u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 503f708ef1bSPoul-Henning Kamp static u_long unpdg_recvspace = 4*1024; 504df8bae1dSRodney W. Grimes 505f708ef1bSPoul-Henning Kamp static int unp_rights; /* file descriptors in flight */ 506df8bae1dSRodney W. Grimes 507ce02431fSDoug Rabson SYSCTL_DECL(_net_local_stream); 508639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW, 509639acc13SGarrett Wollman &unpst_sendspace, 0, ""); 510639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW, 511639acc13SGarrett Wollman &unpst_recvspace, 0, ""); 512ce02431fSDoug Rabson SYSCTL_DECL(_net_local_dgram); 513639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW, 514639acc13SGarrett Wollman &unpdg_sendspace, 0, ""); 515639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW, 516639acc13SGarrett Wollman &unpdg_recvspace, 0, ""); 517ce02431fSDoug Rabson SYSCTL_DECL(_net_local); 518639acc13SGarrett Wollman SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, ""); 519639acc13SGarrett Wollman 520f708ef1bSPoul-Henning Kamp static int 521df8bae1dSRodney W. Grimes unp_attach(so) 522df8bae1dSRodney W. Grimes struct socket *so; 523df8bae1dSRodney W. Grimes { 524df8bae1dSRodney W. Grimes register struct unpcb *unp; 525df8bae1dSRodney W. Grimes int error; 526df8bae1dSRodney W. Grimes 527df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 528df8bae1dSRodney W. Grimes switch (so->so_type) { 529df8bae1dSRodney W. Grimes 530df8bae1dSRodney W. Grimes case SOCK_STREAM: 531df8bae1dSRodney W. Grimes error = soreserve(so, unpst_sendspace, unpst_recvspace); 532df8bae1dSRodney W. Grimes break; 533df8bae1dSRodney W. Grimes 534df8bae1dSRodney W. Grimes case SOCK_DGRAM: 535df8bae1dSRodney W. Grimes error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 536df8bae1dSRodney W. Grimes break; 537df8bae1dSRodney W. Grimes 538df8bae1dSRodney W. Grimes default: 539df8bae1dSRodney W. Grimes panic("unp_attach"); 540df8bae1dSRodney W. Grimes } 541df8bae1dSRodney W. Grimes if (error) 542df8bae1dSRodney W. Grimes return (error); 543df8bae1dSRodney W. Grimes } 544a163d034SWarner Losh unp = uma_zalloc(unp_zone, M_WAITOK); 54557bf258eSGarrett Wollman if (unp == NULL) 546df8bae1dSRodney W. Grimes return (ENOBUFS); 54757bf258eSGarrett Wollman bzero(unp, sizeof *unp); 54898271db4SGarrett Wollman unp->unp_gencnt = ++unp_gencnt; 54998271db4SGarrett Wollman unp_count++; 55098271db4SGarrett Wollman LIST_INIT(&unp->unp_refs); 551df8bae1dSRodney W. Grimes unp->unp_socket = so; 55298271db4SGarrett Wollman LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead 55398271db4SGarrett Wollman : &unp_shead, unp, unp_link); 554210a5a71SAlfred Perlstein so->so_pcb = unp; 555df8bae1dSRodney W. Grimes return (0); 556df8bae1dSRodney W. Grimes } 557df8bae1dSRodney W. Grimes 558f708ef1bSPoul-Henning Kamp static void 559df8bae1dSRodney W. Grimes unp_detach(unp) 560df8bae1dSRodney W. Grimes register struct unpcb *unp; 561df8bae1dSRodney W. Grimes { 56298271db4SGarrett Wollman LIST_REMOVE(unp, unp_link); 56398271db4SGarrett Wollman unp->unp_gencnt = ++unp_gencnt; 56498271db4SGarrett Wollman --unp_count; 565df8bae1dSRodney W. Grimes if (unp->unp_vnode) { 566df8bae1dSRodney W. Grimes unp->unp_vnode->v_socket = 0; 567df8bae1dSRodney W. Grimes vrele(unp->unp_vnode); 568df8bae1dSRodney W. Grimes unp->unp_vnode = 0; 569df8bae1dSRodney W. Grimes } 570df8bae1dSRodney W. Grimes if (unp->unp_conn) 571df8bae1dSRodney W. Grimes unp_disconnect(unp); 5722e3c8fcbSPoul-Henning Kamp while (!LIST_EMPTY(&unp->unp_refs)) 5732e3c8fcbSPoul-Henning Kamp unp_drop(LIST_FIRST(&unp->unp_refs), ECONNRESET); 574df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 575df8bae1dSRodney W. Grimes unp->unp_socket->so_pcb = 0; 576df8bae1dSRodney W. Grimes if (unp_rights) { 577df8bae1dSRodney W. Grimes /* 578df8bae1dSRodney W. Grimes * Normally the receive buffer is flushed later, 579df8bae1dSRodney W. Grimes * in sofree, but if our receive buffer holds references 580df8bae1dSRodney W. Grimes * to descriptors that are now garbage, we will dispose 581df8bae1dSRodney W. Grimes * of those descriptor references after the garbage collector 582df8bae1dSRodney W. Grimes * gets them (resulting in a "panic: closef: count < 0"). 583df8bae1dSRodney W. Grimes */ 584df8bae1dSRodney W. Grimes sorflush(unp->unp_socket); 585df8bae1dSRodney W. Grimes unp_gc(); 586df8bae1dSRodney W. Grimes } 58757bf258eSGarrett Wollman if (unp->unp_addr) 58857bf258eSGarrett Wollman FREE(unp->unp_addr, M_SONAME); 5899e9d298aSJeff Roberson uma_zfree(unp_zone, unp); 590df8bae1dSRodney W. Grimes } 591df8bae1dSRodney W. Grimes 592f708ef1bSPoul-Henning Kamp static int 593b40ce416SJulian Elischer unp_bind(unp, nam, td) 594df8bae1dSRodney W. Grimes struct unpcb *unp; 59557bf258eSGarrett Wollman struct sockaddr *nam; 596b40ce416SJulian Elischer struct thread *td; 597df8bae1dSRodney W. Grimes { 59857bf258eSGarrett Wollman struct sockaddr_un *soun = (struct sockaddr_un *)nam; 599f2a2857bSKirk McKusick struct vnode *vp; 600f2a2857bSKirk McKusick struct mount *mp; 601df8bae1dSRodney W. Grimes struct vattr vattr; 60257bf258eSGarrett Wollman int error, namelen; 603df8bae1dSRodney W. Grimes struct nameidata nd; 6048f364875SJulian Elischer char *buf; 605df8bae1dSRodney W. Grimes 606df8bae1dSRodney W. Grimes if (unp->unp_vnode != NULL) 607df8bae1dSRodney W. Grimes return (EINVAL); 60855c85568SRobert Drehmel 60957bf258eSGarrett Wollman namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path); 61057bf258eSGarrett Wollman if (namelen <= 0) 61157bf258eSGarrett Wollman return EINVAL; 61255c85568SRobert Drehmel 613a163d034SWarner Losh buf = malloc(namelen + 1, M_TEMP, M_WAITOK); 61455c85568SRobert Drehmel strlcpy(buf, soun->sun_path, namelen + 1); 61555c85568SRobert Drehmel 616f2a2857bSKirk McKusick restart: 617b65f6f6bSRobert Watson NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME, UIO_SYSSPACE, 618b40ce416SJulian Elischer buf, td); 619df8bae1dSRodney W. Grimes /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 620797f2d22SPoul-Henning Kamp error = namei(&nd); 6218f364875SJulian Elischer if (error) { 6228f364875SJulian Elischer free(buf, M_TEMP); 623ad4ff090SJulian Elischer return (error); 6248f364875SJulian Elischer } 625df8bae1dSRodney W. Grimes vp = nd.ni_vp; 626f2a2857bSKirk McKusick if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 627762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 628df8bae1dSRodney W. Grimes if (nd.ni_dvp == vp) 629df8bae1dSRodney W. Grimes vrele(nd.ni_dvp); 630df8bae1dSRodney W. Grimes else 631df8bae1dSRodney W. Grimes vput(nd.ni_dvp); 632f2a2857bSKirk McKusick if (vp != NULL) { 633df8bae1dSRodney W. Grimes vrele(vp); 6348f364875SJulian Elischer free(buf, M_TEMP); 635df8bae1dSRodney W. Grimes return (EADDRINUSE); 636df8bae1dSRodney W. Grimes } 6378f364875SJulian Elischer error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 6388f364875SJulian Elischer if (error) { 6398f364875SJulian Elischer free(buf, M_TEMP); 640f2a2857bSKirk McKusick return (error); 6418f364875SJulian Elischer } 642f2a2857bSKirk McKusick goto restart; 643f2a2857bSKirk McKusick } 644df8bae1dSRodney W. Grimes VATTR_NULL(&vattr); 645df8bae1dSRodney W. Grimes vattr.va_type = VSOCK; 646b40ce416SJulian Elischer vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask); 6476ea48a90SRobert Watson #ifdef MAC 6486ea48a90SRobert Watson error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 6496ea48a90SRobert Watson &vattr); 6506151efaaSRobert Watson #endif 6516ea48a90SRobert Watson if (error == 0) { 652a854ed98SJohn Baldwin VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE); 6537be2d300SMike Smith error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 6546ea48a90SRobert Watson } 655762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 6567be2d300SMike Smith vput(nd.ni_dvp); 6578f364875SJulian Elischer if (error) { 6588f364875SJulian Elischer free(buf, M_TEMP); 659df8bae1dSRodney W. Grimes return (error); 6608f364875SJulian Elischer } 661df8bae1dSRodney W. Grimes vp = nd.ni_vp; 662df8bae1dSRodney W. Grimes vp->v_socket = unp->unp_socket; 663df8bae1dSRodney W. Grimes unp->unp_vnode = vp; 66457bf258eSGarrett Wollman unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam, 1); 665b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 666f2a2857bSKirk McKusick vn_finished_write(mp); 6678f364875SJulian Elischer free(buf, M_TEMP); 668df8bae1dSRodney W. Grimes return (0); 669df8bae1dSRodney W. Grimes } 670df8bae1dSRodney W. Grimes 671f708ef1bSPoul-Henning Kamp static int 672b40ce416SJulian Elischer unp_connect(so, nam, td) 673df8bae1dSRodney W. Grimes struct socket *so; 67457bf258eSGarrett Wollman struct sockaddr *nam; 675b40ce416SJulian Elischer struct thread *td; 676df8bae1dSRodney W. Grimes { 67757bf258eSGarrett Wollman register struct sockaddr_un *soun = (struct sockaddr_un *)nam; 678df8bae1dSRodney W. Grimes register struct vnode *vp; 679df8bae1dSRodney W. Grimes register struct socket *so2, *so3; 6800c1bb4fbSDima Dorfman struct unpcb *unp, *unp2, *unp3; 68157bf258eSGarrett Wollman int error, len; 682df8bae1dSRodney W. Grimes struct nameidata nd; 68357bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN]; 684df8bae1dSRodney W. Grimes 68557bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path); 68657bf258eSGarrett Wollman if (len <= 0) 68757bf258eSGarrett Wollman return EINVAL; 68855c85568SRobert Drehmel strlcpy(buf, soun->sun_path, len + 1); 68957bf258eSGarrett Wollman 690b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td); 691797f2d22SPoul-Henning Kamp error = namei(&nd); 692797f2d22SPoul-Henning Kamp if (error) 693df8bae1dSRodney W. Grimes return (error); 694df8bae1dSRodney W. Grimes vp = nd.ni_vp; 695762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 696df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) { 697df8bae1dSRodney W. Grimes error = ENOTSOCK; 698df8bae1dSRodney W. Grimes goto bad; 699df8bae1dSRodney W. Grimes } 700a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 701797f2d22SPoul-Henning Kamp if (error) 702df8bae1dSRodney W. Grimes goto bad; 703df8bae1dSRodney W. Grimes so2 = vp->v_socket; 704df8bae1dSRodney W. Grimes if (so2 == 0) { 705df8bae1dSRodney W. Grimes error = ECONNREFUSED; 706df8bae1dSRodney W. Grimes goto bad; 707df8bae1dSRodney W. Grimes } 708df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) { 709df8bae1dSRodney W. Grimes error = EPROTOTYPE; 710df8bae1dSRodney W. Grimes goto bad; 711df8bae1dSRodney W. Grimes } 712df8bae1dSRodney W. Grimes if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 7134cc20ab1SSeigo Tanimura if ((so2->so_options & SO_ACCEPTCONN) == 0 || 7144cc20ab1SSeigo Tanimura (so3 = sonewconn(so2, 0)) == 0) { 715df8bae1dSRodney W. Grimes error = ECONNREFUSED; 716df8bae1dSRodney W. Grimes goto bad; 717df8bae1dSRodney W. Grimes } 7180c1bb4fbSDima Dorfman unp = sotounpcb(so); 719df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 720df8bae1dSRodney W. Grimes unp3 = sotounpcb(so3); 721df8bae1dSRodney W. Grimes if (unp2->unp_addr) 72257bf258eSGarrett Wollman unp3->unp_addr = (struct sockaddr_un *) 72357bf258eSGarrett Wollman dup_sockaddr((struct sockaddr *) 72457bf258eSGarrett Wollman unp2->unp_addr, 1); 7250c1bb4fbSDima Dorfman 7260c1bb4fbSDima Dorfman /* 7270c1bb4fbSDima Dorfman * unp_peercred management: 7280c1bb4fbSDima Dorfman * 7290c1bb4fbSDima Dorfman * The connecter's (client's) credentials are copied 7300c1bb4fbSDima Dorfman * from its process structure at the time of connect() 7310c1bb4fbSDima Dorfman * (which is now). 7320c1bb4fbSDima Dorfman */ 733a854ed98SJohn Baldwin cru2x(td->td_ucred, &unp3->unp_peercred); 7340c1bb4fbSDima Dorfman unp3->unp_flags |= UNP_HAVEPC; 7350c1bb4fbSDima Dorfman /* 7360c1bb4fbSDima Dorfman * The receiver's (server's) credentials are copied 7370c1bb4fbSDima Dorfman * from the unp_peercred member of socket on which the 7380c1bb4fbSDima Dorfman * former called listen(); unp_listen() cached that 7390c1bb4fbSDima Dorfman * process's credentials at that time so we can use 7400c1bb4fbSDima Dorfman * them now. 7410c1bb4fbSDima Dorfman */ 7420c1bb4fbSDima Dorfman KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED, 7430c1bb4fbSDima Dorfman ("unp_connect: listener without cached peercred")); 7440c1bb4fbSDima Dorfman memcpy(&unp->unp_peercred, &unp2->unp_peercred, 7450c1bb4fbSDima Dorfman sizeof(unp->unp_peercred)); 7460c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPC; 747335654d7SRobert Watson #ifdef MAC 748335654d7SRobert Watson mac_set_socket_peer_from_socket(so, so3); 749335654d7SRobert Watson mac_set_socket_peer_from_socket(so3, so); 750335654d7SRobert Watson #endif 7510c1bb4fbSDima Dorfman 752df8bae1dSRodney W. Grimes so2 = so3; 753df8bae1dSRodney W. Grimes } 754df8bae1dSRodney W. Grimes error = unp_connect2(so, so2); 755df8bae1dSRodney W. Grimes bad: 756df8bae1dSRodney W. Grimes vput(vp); 757df8bae1dSRodney W. Grimes return (error); 758df8bae1dSRodney W. Grimes } 759df8bae1dSRodney W. Grimes 76026f9a767SRodney W. Grimes int 761df8bae1dSRodney W. Grimes unp_connect2(so, so2) 762df8bae1dSRodney W. Grimes register struct socket *so; 763df8bae1dSRodney W. Grimes register struct socket *so2; 764df8bae1dSRodney W. Grimes { 765df8bae1dSRodney W. Grimes register struct unpcb *unp = sotounpcb(so); 766df8bae1dSRodney W. Grimes register struct unpcb *unp2; 767df8bae1dSRodney W. Grimes 768df8bae1dSRodney W. Grimes if (so2->so_type != so->so_type) 769df8bae1dSRodney W. Grimes return (EPROTOTYPE); 770df8bae1dSRodney W. Grimes unp2 = sotounpcb(so2); 771df8bae1dSRodney W. Grimes unp->unp_conn = unp2; 772df8bae1dSRodney W. Grimes switch (so->so_type) { 773df8bae1dSRodney W. Grimes 774df8bae1dSRodney W. Grimes case SOCK_DGRAM: 77598271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink); 776df8bae1dSRodney W. Grimes soisconnected(so); 777df8bae1dSRodney W. Grimes break; 778df8bae1dSRodney W. Grimes 779df8bae1dSRodney W. Grimes case SOCK_STREAM: 780df8bae1dSRodney W. Grimes unp2->unp_conn = unp; 781df8bae1dSRodney W. Grimes soisconnected(so); 782df8bae1dSRodney W. Grimes soisconnected(so2); 783df8bae1dSRodney W. Grimes break; 784df8bae1dSRodney W. Grimes 785df8bae1dSRodney W. Grimes default: 786df8bae1dSRodney W. Grimes panic("unp_connect2"); 787df8bae1dSRodney W. Grimes } 788df8bae1dSRodney W. Grimes return (0); 789df8bae1dSRodney W. Grimes } 790df8bae1dSRodney W. Grimes 791f708ef1bSPoul-Henning Kamp static void 792df8bae1dSRodney W. Grimes unp_disconnect(unp) 793df8bae1dSRodney W. Grimes struct unpcb *unp; 794df8bae1dSRodney W. Grimes { 795df8bae1dSRodney W. Grimes register struct unpcb *unp2 = unp->unp_conn; 796df8bae1dSRodney W. Grimes 797df8bae1dSRodney W. Grimes if (unp2 == 0) 798df8bae1dSRodney W. Grimes return; 799df8bae1dSRodney W. Grimes unp->unp_conn = 0; 800df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) { 801df8bae1dSRodney W. Grimes 802df8bae1dSRodney W. Grimes case SOCK_DGRAM: 80398271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink); 804df8bae1dSRodney W. Grimes unp->unp_socket->so_state &= ~SS_ISCONNECTED; 805df8bae1dSRodney W. Grimes break; 806df8bae1dSRodney W. Grimes 807df8bae1dSRodney W. Grimes case SOCK_STREAM: 808df8bae1dSRodney W. Grimes soisdisconnected(unp->unp_socket); 809df8bae1dSRodney W. Grimes unp2->unp_conn = 0; 810df8bae1dSRodney W. Grimes soisdisconnected(unp2->unp_socket); 811df8bae1dSRodney W. Grimes break; 812df8bae1dSRodney W. Grimes } 813df8bae1dSRodney W. Grimes } 814df8bae1dSRodney W. Grimes 815df8bae1dSRodney W. Grimes #ifdef notdef 81626f9a767SRodney W. Grimes void 817df8bae1dSRodney W. Grimes unp_abort(unp) 818df8bae1dSRodney W. Grimes struct unpcb *unp; 819df8bae1dSRodney W. Grimes { 820df8bae1dSRodney W. Grimes 821df8bae1dSRodney W. Grimes unp_detach(unp); 822df8bae1dSRodney W. Grimes } 823df8bae1dSRodney W. Grimes #endif 824df8bae1dSRodney W. Grimes 82598271db4SGarrett Wollman static int 82682d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS) 82798271db4SGarrett Wollman { 828f5ef029eSPoul-Henning Kamp int error, i, n; 82998271db4SGarrett Wollman struct unpcb *unp, **unp_list; 83098271db4SGarrett Wollman unp_gen_t gencnt; 8318f364875SJulian Elischer struct xunpgen *xug; 83298271db4SGarrett Wollman struct unp_head *head; 8338f364875SJulian Elischer struct xunpcb *xu; 83498271db4SGarrett Wollman 835a23d65bfSBruce Evans head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead); 83698271db4SGarrett Wollman 83798271db4SGarrett Wollman /* 83898271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and 83998271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 84098271db4SGarrett Wollman */ 84198271db4SGarrett Wollman if (req->oldptr == 0) { 84298271db4SGarrett Wollman n = unp_count; 8438f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug) 84498271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb); 84598271db4SGarrett Wollman return 0; 84698271db4SGarrett Wollman } 84798271db4SGarrett Wollman 84898271db4SGarrett Wollman if (req->newptr != 0) 84998271db4SGarrett Wollman return EPERM; 85098271db4SGarrett Wollman 85198271db4SGarrett Wollman /* 85298271db4SGarrett Wollman * OK, now we're committed to doing something. 85398271db4SGarrett Wollman */ 854a163d034SWarner Losh xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK); 85598271db4SGarrett Wollman gencnt = unp_gencnt; 85698271db4SGarrett Wollman n = unp_count; 85798271db4SGarrett Wollman 8588f364875SJulian Elischer xug->xug_len = sizeof *xug; 8598f364875SJulian Elischer xug->xug_count = n; 8608f364875SJulian Elischer xug->xug_gen = gencnt; 8618f364875SJulian Elischer xug->xug_sogen = so_gencnt; 8628f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 8638f364875SJulian Elischer if (error) { 8648f364875SJulian Elischer free(xug, M_TEMP); 86598271db4SGarrett Wollman return error; 8668f364875SJulian Elischer } 86798271db4SGarrett Wollman 868a163d034SWarner Losh unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK); 86998271db4SGarrett Wollman 8702e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n; 8712e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) { 8728a7d8cc6SRobert Watson if (unp->unp_gencnt <= gencnt) { 873a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred, 8748a7d8cc6SRobert Watson unp->unp_socket->so_cred)) 8754787fd37SPaul Saab continue; 87698271db4SGarrett Wollman unp_list[i++] = unp; 87798271db4SGarrett Wollman } 8784787fd37SPaul Saab } 87998271db4SGarrett Wollman n = i; /* in case we lost some during malloc */ 88098271db4SGarrett Wollman 88198271db4SGarrett Wollman error = 0; 882a163d034SWarner Losh xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK); 88398271db4SGarrett Wollman for (i = 0; i < n; i++) { 88498271db4SGarrett Wollman unp = unp_list[i]; 88598271db4SGarrett Wollman if (unp->unp_gencnt <= gencnt) { 8868f364875SJulian Elischer xu->xu_len = sizeof *xu; 8878f364875SJulian Elischer xu->xu_unpp = unp; 88898271db4SGarrett Wollman /* 88998271db4SGarrett Wollman * XXX - need more locking here to protect against 89098271db4SGarrett Wollman * connect/disconnect races for SMP. 89198271db4SGarrett Wollman */ 89298271db4SGarrett Wollman if (unp->unp_addr) 8938f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr, 89498271db4SGarrett Wollman unp->unp_addr->sun_len); 89598271db4SGarrett Wollman if (unp->unp_conn && unp->unp_conn->unp_addr) 89698271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr, 8978f364875SJulian Elischer &xu->xu_caddr, 89898271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len); 8998f364875SJulian Elischer bcopy(unp, &xu->xu_unp, sizeof *unp); 9008f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket); 9018f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu); 90298271db4SGarrett Wollman } 90398271db4SGarrett Wollman } 9048f364875SJulian Elischer free(xu, M_TEMP); 90598271db4SGarrett Wollman if (!error) { 90698271db4SGarrett Wollman /* 90798271db4SGarrett Wollman * Give the user an updated idea of our state. 90898271db4SGarrett Wollman * If the generation differs from what we told 90998271db4SGarrett Wollman * her before, she knows that something happened 91098271db4SGarrett Wollman * while we were processing this request, and it 91198271db4SGarrett Wollman * might be necessary to retry. 91298271db4SGarrett Wollman */ 9138f364875SJulian Elischer xug->xug_gen = unp_gencnt; 9148f364875SJulian Elischer xug->xug_sogen = so_gencnt; 9158f364875SJulian Elischer xug->xug_count = unp_count; 9168f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug); 91798271db4SGarrett Wollman } 91898271db4SGarrett Wollman free(unp_list, M_TEMP); 9198f364875SJulian Elischer free(xug, M_TEMP); 92098271db4SGarrett Wollman return error; 92198271db4SGarrett Wollman } 92298271db4SGarrett Wollman 92398271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD, 92498271db4SGarrett Wollman (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb", 92598271db4SGarrett Wollman "List of active local datagram sockets"); 92698271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD, 92798271db4SGarrett Wollman (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb", 92898271db4SGarrett Wollman "List of active local stream sockets"); 92998271db4SGarrett Wollman 930f708ef1bSPoul-Henning Kamp static void 931df8bae1dSRodney W. Grimes unp_shutdown(unp) 932df8bae1dSRodney W. Grimes struct unpcb *unp; 933df8bae1dSRodney W. Grimes { 934df8bae1dSRodney W. Grimes struct socket *so; 935df8bae1dSRodney W. Grimes 936df8bae1dSRodney W. Grimes if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 937df8bae1dSRodney W. Grimes (so = unp->unp_conn->unp_socket)) 938df8bae1dSRodney W. Grimes socantrcvmore(so); 939df8bae1dSRodney W. Grimes } 940df8bae1dSRodney W. Grimes 941f708ef1bSPoul-Henning Kamp static void 942df8bae1dSRodney W. Grimes unp_drop(unp, errno) 943df8bae1dSRodney W. Grimes struct unpcb *unp; 944df8bae1dSRodney W. Grimes int errno; 945df8bae1dSRodney W. Grimes { 946df8bae1dSRodney W. Grimes struct socket *so = unp->unp_socket; 947df8bae1dSRodney W. Grimes 948df8bae1dSRodney W. Grimes so->so_error = errno; 949df8bae1dSRodney W. Grimes unp_disconnect(unp); 950df8bae1dSRodney W. Grimes } 951df8bae1dSRodney W. Grimes 952df8bae1dSRodney W. Grimes #ifdef notdef 95326f9a767SRodney W. Grimes void 954df8bae1dSRodney W. Grimes unp_drain() 955df8bae1dSRodney W. Grimes { 956df8bae1dSRodney W. Grimes 957df8bae1dSRodney W. Grimes } 958df8bae1dSRodney W. Grimes #endif 959df8bae1dSRodney W. Grimes 9602bc21ed9SDavid Malone static void 9612bc21ed9SDavid Malone unp_freerights(rp, fdcount) 9622bc21ed9SDavid Malone struct file **rp; 9632bc21ed9SDavid Malone int fdcount; 964df8bae1dSRodney W. Grimes { 9652bc21ed9SDavid Malone int i; 9662bc21ed9SDavid Malone struct file *fp; 967df8bae1dSRodney W. Grimes 9682bc21ed9SDavid Malone for (i = 0; i < fdcount; i++) { 969df8bae1dSRodney W. Grimes fp = *rp; 9708692c025SYoshinobu Inoue /* 9712bc21ed9SDavid Malone * zero the pointer before calling 9722bc21ed9SDavid Malone * unp_discard since it may end up 9732bc21ed9SDavid Malone * in unp_gc().. 9748692c025SYoshinobu Inoue */ 975df8bae1dSRodney W. Grimes *rp++ = 0; 9768692c025SYoshinobu Inoue unp_discard(fp); 977df8bae1dSRodney W. Grimes } 9782bc21ed9SDavid Malone } 9792bc21ed9SDavid Malone 9802bc21ed9SDavid Malone int 9812bc21ed9SDavid Malone unp_externalize(control, controlp) 9822bc21ed9SDavid Malone struct mbuf *control, **controlp; 9832bc21ed9SDavid Malone { 9842bc21ed9SDavid Malone struct thread *td = curthread; /* XXX */ 9852bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 9862bc21ed9SDavid Malone int i; 9872bc21ed9SDavid Malone int *fdp; 9882bc21ed9SDavid Malone struct file **rp; 9892bc21ed9SDavid Malone struct file *fp; 9902bc21ed9SDavid Malone void *data; 9912bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 9922bc21ed9SDavid Malone int error, newfds; 9932bc21ed9SDavid Malone int f; 9942bc21ed9SDavid Malone u_int newlen; 9952bc21ed9SDavid Malone 9962bc21ed9SDavid Malone error = 0; 9972bc21ed9SDavid Malone if (controlp != NULL) /* controlp == NULL => free control messages */ 9982bc21ed9SDavid Malone *controlp = NULL; 9992bc21ed9SDavid Malone 10002bc21ed9SDavid Malone while (cm != NULL) { 10012bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) { 10022bc21ed9SDavid Malone error = EINVAL; 10032bc21ed9SDavid Malone break; 10042bc21ed9SDavid Malone } 10052bc21ed9SDavid Malone 10062bc21ed9SDavid Malone data = CMSG_DATA(cm); 10072bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 10082bc21ed9SDavid Malone 10092bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET 10102bc21ed9SDavid Malone && cm->cmsg_type == SCM_RIGHTS) { 10112bc21ed9SDavid Malone newfds = datalen / sizeof(struct file *); 10122bc21ed9SDavid Malone rp = data; 10132bc21ed9SDavid Malone 10142bc21ed9SDavid Malone /* If we're not outputting the discriptors free them. */ 10152bc21ed9SDavid Malone if (error || controlp == NULL) { 10162bc21ed9SDavid Malone unp_freerights(rp, newfds); 10172bc21ed9SDavid Malone goto next; 10182bc21ed9SDavid Malone } 1019426da3bcSAlfred Perlstein FILEDESC_LOCK(td->td_proc->p_fd); 10202bc21ed9SDavid Malone /* if the new FD's will not fit free them. */ 10212bc21ed9SDavid Malone if (!fdavail(td, newfds)) { 1022426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 10232bc21ed9SDavid Malone error = EMSGSIZE; 10242bc21ed9SDavid Malone unp_freerights(rp, newfds); 10252bc21ed9SDavid Malone goto next; 1026df8bae1dSRodney W. Grimes } 1027ed5b7817SJulian Elischer /* 10282bc21ed9SDavid Malone * now change each pointer to an fd in the global 10292bc21ed9SDavid Malone * table to an integer that is the index to the 10302bc21ed9SDavid Malone * local fd table entry that we set up to point 10312bc21ed9SDavid Malone * to the global one we are transferring. 1032ed5b7817SJulian Elischer */ 10332bc21ed9SDavid Malone newlen = newfds * sizeof(int); 10342bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 10352bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 10362bc21ed9SDavid Malone if (*controlp == NULL) { 1037426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 10382bc21ed9SDavid Malone error = E2BIG; 10392bc21ed9SDavid Malone unp_freerights(rp, newfds); 10402bc21ed9SDavid Malone goto next; 10412bc21ed9SDavid Malone } 10422bc21ed9SDavid Malone 10432bc21ed9SDavid Malone fdp = (int *) 10442bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 1045df8bae1dSRodney W. Grimes for (i = 0; i < newfds; i++) { 1046b40ce416SJulian Elischer if (fdalloc(td, 0, &f)) 10472bc21ed9SDavid Malone panic("unp_externalize fdalloc failed"); 10488692c025SYoshinobu Inoue fp = *rp++; 1049b40ce416SJulian Elischer td->td_proc->p_fd->fd_ofiles[f] = fp; 1050426da3bcSAlfred Perlstein FILE_LOCK(fp); 1051df8bae1dSRodney W. Grimes fp->f_msgcount--; 1052426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1053df8bae1dSRodney W. Grimes unp_rights--; 10548692c025SYoshinobu Inoue *fdp++ = f; 1055df8bae1dSRodney W. Grimes } 1056426da3bcSAlfred Perlstein FILEDESC_UNLOCK(td->td_proc->p_fd); 10572bc21ed9SDavid Malone } else { /* We can just copy anything else across */ 10582bc21ed9SDavid Malone if (error || controlp == NULL) 10592bc21ed9SDavid Malone goto next; 10602bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, datalen, 10612bc21ed9SDavid Malone cm->cmsg_type, cm->cmsg_level); 10622bc21ed9SDavid Malone if (*controlp == NULL) { 10632bc21ed9SDavid Malone error = ENOBUFS; 10642bc21ed9SDavid Malone goto next; 10652bc21ed9SDavid Malone } 10662bc21ed9SDavid Malone bcopy(data, 10672bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)), 10682bc21ed9SDavid Malone datalen); 10692bc21ed9SDavid Malone } 10702bc21ed9SDavid Malone 10712bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 10722bc21ed9SDavid Malone 10732bc21ed9SDavid Malone next: 10742bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 10752bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 10762bc21ed9SDavid Malone cm = (struct cmsghdr *) 10772bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 10788692c025SYoshinobu Inoue } else { 10792bc21ed9SDavid Malone clen = 0; 10802bc21ed9SDavid Malone cm = NULL; 10818692c025SYoshinobu Inoue } 10828692c025SYoshinobu Inoue } 10838692c025SYoshinobu Inoue 10842bc21ed9SDavid Malone m_freem(control); 10852bc21ed9SDavid Malone 10862bc21ed9SDavid Malone return (error); 1087df8bae1dSRodney W. Grimes } 1088df8bae1dSRodney W. Grimes 108998271db4SGarrett Wollman void 109098271db4SGarrett Wollman unp_init(void) 109198271db4SGarrett Wollman { 10929e9d298aSJeff Roberson unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL, 10939e9d298aSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 1094586c8b6bSJeff Roberson uma_zone_set_max(unp_zone, nmbclusters); 109598271db4SGarrett Wollman if (unp_zone == 0) 109698271db4SGarrett Wollman panic("unp_init"); 109798271db4SGarrett Wollman LIST_INIT(&unp_dhead); 109898271db4SGarrett Wollman LIST_INIT(&unp_shead); 109998271db4SGarrett Wollman } 110098271db4SGarrett Wollman 1101f708ef1bSPoul-Henning Kamp static int 11022bc21ed9SDavid Malone unp_internalize(controlp, td) 11032bc21ed9SDavid Malone struct mbuf **controlp; 1104b40ce416SJulian Elischer struct thread *td; 1105df8bae1dSRodney W. Grimes { 11062bc21ed9SDavid Malone struct mbuf *control = *controlp; 1107b40ce416SJulian Elischer struct proc *p = td->td_proc; 11088692c025SYoshinobu Inoue struct filedesc *fdescp = p->p_fd; 11092bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *); 11102bc21ed9SDavid Malone struct cmsgcred *cmcred; 11112bc21ed9SDavid Malone struct file **rp; 11122bc21ed9SDavid Malone struct file *fp; 11132bc21ed9SDavid Malone struct timeval *tv; 11142bc21ed9SDavid Malone int i, fd, *fdp; 11152bc21ed9SDavid Malone void *data; 11162bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen; 11172bc21ed9SDavid Malone int error, oldfds; 11188692c025SYoshinobu Inoue u_int newlen; 1119df8bae1dSRodney W. Grimes 11202bc21ed9SDavid Malone error = 0; 11212bc21ed9SDavid Malone *controlp = NULL; 11220b788fa1SBill Paul 11232bc21ed9SDavid Malone while (cm != NULL) { 11242bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET 11252bc21ed9SDavid Malone || cm->cmsg_len > clen) { 11262bc21ed9SDavid Malone error = EINVAL; 11272bc21ed9SDavid Malone goto out; 11282bc21ed9SDavid Malone } 11292bc21ed9SDavid Malone 11302bc21ed9SDavid Malone data = CMSG_DATA(cm); 11312bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 11322bc21ed9SDavid Malone 11332bc21ed9SDavid Malone switch (cm->cmsg_type) { 11340b788fa1SBill Paul /* 11350b788fa1SBill Paul * Fill in credential information. 11360b788fa1SBill Paul */ 11372bc21ed9SDavid Malone case SCM_CREDS: 11382bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*cmcred), 11392bc21ed9SDavid Malone SCM_CREDS, SOL_SOCKET); 11402bc21ed9SDavid Malone if (*controlp == NULL) { 11412bc21ed9SDavid Malone error = ENOBUFS; 11422bc21ed9SDavid Malone goto out; 11432bc21ed9SDavid Malone } 11442bc21ed9SDavid Malone 11452bc21ed9SDavid Malone cmcred = (struct cmsgcred *) 11462bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 11470b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid; 1148a854ed98SJohn Baldwin cmcred->cmcred_uid = td->td_ucred->cr_ruid; 1149a854ed98SJohn Baldwin cmcred->cmcred_gid = td->td_ucred->cr_rgid; 1150a854ed98SJohn Baldwin cmcred->cmcred_euid = td->td_ucred->cr_uid; 1151a854ed98SJohn Baldwin cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups, 11520b788fa1SBill Paul CMGROUP_MAX); 11530b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++) 11542bc21ed9SDavid Malone cmcred->cmcred_groups[i] = 1155a854ed98SJohn Baldwin td->td_ucred->cr_groups[i]; 11562bc21ed9SDavid Malone break; 11570b788fa1SBill Paul 11582bc21ed9SDavid Malone case SCM_RIGHTS: 11592bc21ed9SDavid Malone oldfds = datalen / sizeof (int); 1160ed5b7817SJulian Elischer /* 11612bc21ed9SDavid Malone * check that all the FDs passed in refer to legal files 1162ed5b7817SJulian Elischer * If not, reject the entire operation. 1163ed5b7817SJulian Elischer */ 11642bc21ed9SDavid Malone fdp = data; 1165426da3bcSAlfred Perlstein FILEDESC_LOCK(fdescp); 1166df8bae1dSRodney W. Grimes for (i = 0; i < oldfds; i++) { 11678692c025SYoshinobu Inoue fd = *fdp++; 11688692c025SYoshinobu Inoue if ((unsigned)fd >= fdescp->fd_nfiles || 11692bc21ed9SDavid Malone fdescp->fd_ofiles[fd] == NULL) { 1170426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 11712bc21ed9SDavid Malone error = EBADF; 11722bc21ed9SDavid Malone goto out; 11732bc21ed9SDavid Malone } 1174e7d6662fSAlfred Perlstein fp = fdescp->fd_ofiles[fd]; 1175e7d6662fSAlfred Perlstein if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) { 1176e7d6662fSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 1177e7d6662fSAlfred Perlstein error = EOPNOTSUPP; 1178e7d6662fSAlfred Perlstein goto out; 1179e7d6662fSAlfred Perlstein } 1180e7d6662fSAlfred Perlstein 1181df8bae1dSRodney W. Grimes } 1182ed5b7817SJulian Elischer /* 1183ed5b7817SJulian Elischer * Now replace the integer FDs with pointers to 1184ed5b7817SJulian Elischer * the associated global file table entry.. 1185ed5b7817SJulian Elischer */ 11862bc21ed9SDavid Malone newlen = oldfds * sizeof(struct file *); 11872bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen, 11882bc21ed9SDavid Malone SCM_RIGHTS, SOL_SOCKET); 11892bc21ed9SDavid Malone if (*controlp == NULL) { 1190426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 11912bc21ed9SDavid Malone error = E2BIG; 11922bc21ed9SDavid Malone goto out; 11938692c025SYoshinobu Inoue } 11948692c025SYoshinobu Inoue 11952bc21ed9SDavid Malone fdp = data; 11962bc21ed9SDavid Malone rp = (struct file **) 11972bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 11988692c025SYoshinobu Inoue for (i = 0; i < oldfds; i++) { 11998692c025SYoshinobu Inoue fp = fdescp->fd_ofiles[*fdp++]; 1200df8bae1dSRodney W. Grimes *rp++ = fp; 1201426da3bcSAlfred Perlstein FILE_LOCK(fp); 1202df8bae1dSRodney W. Grimes fp->f_count++; 1203df8bae1dSRodney W. Grimes fp->f_msgcount++; 1204426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1205df8bae1dSRodney W. Grimes unp_rights++; 1206df8bae1dSRodney W. Grimes } 1207426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdescp); 12082bc21ed9SDavid Malone break; 12092bc21ed9SDavid Malone 12102bc21ed9SDavid Malone case SCM_TIMESTAMP: 12112bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, sizeof(*tv), 12122bc21ed9SDavid Malone SCM_TIMESTAMP, SOL_SOCKET); 12132bc21ed9SDavid Malone if (*controlp == NULL) { 12142bc21ed9SDavid Malone error = ENOBUFS; 12152bc21ed9SDavid Malone goto out; 12168692c025SYoshinobu Inoue } 12172bc21ed9SDavid Malone tv = (struct timeval *) 12182bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)); 12192bc21ed9SDavid Malone microtime(tv); 12202bc21ed9SDavid Malone break; 12212bc21ed9SDavid Malone 12222bc21ed9SDavid Malone default: 12232bc21ed9SDavid Malone error = EINVAL; 12242bc21ed9SDavid Malone goto out; 12252bc21ed9SDavid Malone } 12262bc21ed9SDavid Malone 12272bc21ed9SDavid Malone controlp = &(*controlp)->m_next; 12282bc21ed9SDavid Malone 12292bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 12302bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 12312bc21ed9SDavid Malone cm = (struct cmsghdr *) 12322bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 12332bc21ed9SDavid Malone } else { 12342bc21ed9SDavid Malone clen = 0; 12352bc21ed9SDavid Malone cm = NULL; 12362bc21ed9SDavid Malone } 12372bc21ed9SDavid Malone } 12382bc21ed9SDavid Malone 12392bc21ed9SDavid Malone out: 12402bc21ed9SDavid Malone m_freem(control); 12412bc21ed9SDavid Malone 12422bc21ed9SDavid Malone return (error); 1243df8bae1dSRodney W. Grimes } 1244df8bae1dSRodney W. Grimes 1245f708ef1bSPoul-Henning Kamp static int unp_defer, unp_gcing; 1246df8bae1dSRodney W. Grimes 1247f708ef1bSPoul-Henning Kamp static void 1248df8bae1dSRodney W. Grimes unp_gc() 1249df8bae1dSRodney W. Grimes { 1250df8bae1dSRodney W. Grimes register struct file *fp, *nextfp; 1251df8bae1dSRodney W. Grimes register struct socket *so; 1252df8bae1dSRodney W. Grimes struct file **extra_ref, **fpp; 1253df8bae1dSRodney W. Grimes int nunref, i; 1254df8bae1dSRodney W. Grimes 1255df8bae1dSRodney W. Grimes if (unp_gcing) 1256df8bae1dSRodney W. Grimes return; 1257df8bae1dSRodney W. Grimes unp_gcing = 1; 1258df8bae1dSRodney W. Grimes unp_defer = 0; 1259ed5b7817SJulian Elischer /* 1260ed5b7817SJulian Elischer * before going through all this, set all FDs to 1261ed5b7817SJulian Elischer * be NOT defered and NOT externally accessible 1262ed5b7817SJulian Elischer */ 1263426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 12642e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) 1265426da3bcSAlfred Perlstein fp->f_gcflag &= ~(FMARK|FDEFER); 1266df8bae1dSRodney W. Grimes do { 12672e3c8fcbSPoul-Henning Kamp LIST_FOREACH(fp, &filehead, f_list) { 1268426da3bcSAlfred Perlstein FILE_LOCK(fp); 1269ed5b7817SJulian Elischer /* 1270ed5b7817SJulian Elischer * If the file is not open, skip it 1271ed5b7817SJulian Elischer */ 1272426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1273426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1274df8bae1dSRodney W. Grimes continue; 1275426da3bcSAlfred Perlstein } 1276ed5b7817SJulian Elischer /* 1277ed5b7817SJulian Elischer * If we already marked it as 'defer' in a 1278ed5b7817SJulian Elischer * previous pass, then try process it this time 1279ed5b7817SJulian Elischer * and un-mark it 1280ed5b7817SJulian Elischer */ 1281426da3bcSAlfred Perlstein if (fp->f_gcflag & FDEFER) { 1282426da3bcSAlfred Perlstein fp->f_gcflag &= ~FDEFER; 1283df8bae1dSRodney W. Grimes unp_defer--; 1284df8bae1dSRodney W. Grimes } else { 1285ed5b7817SJulian Elischer /* 1286ed5b7817SJulian Elischer * if it's not defered, then check if it's 1287ed5b7817SJulian Elischer * already marked.. if so skip it 1288ed5b7817SJulian Elischer */ 1289426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) { 1290426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1291df8bae1dSRodney W. Grimes continue; 1292426da3bcSAlfred Perlstein } 1293ed5b7817SJulian Elischer /* 1294ed5b7817SJulian Elischer * If all references are from messages 1295ed5b7817SJulian Elischer * in transit, then skip it. it's not 1296ed5b7817SJulian Elischer * externally accessible. 1297ed5b7817SJulian Elischer */ 1298426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount) { 1299426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1300df8bae1dSRodney W. Grimes continue; 1301426da3bcSAlfred Perlstein } 1302ed5b7817SJulian Elischer /* 1303ed5b7817SJulian Elischer * If it got this far then it must be 1304ed5b7817SJulian Elischer * externally accessible. 1305ed5b7817SJulian Elischer */ 1306426da3bcSAlfred Perlstein fp->f_gcflag |= FMARK; 1307df8bae1dSRodney W. Grimes } 1308ed5b7817SJulian Elischer /* 1309ed5b7817SJulian Elischer * either it was defered, or it is externally 1310ed5b7817SJulian Elischer * accessible and not already marked so. 1311ed5b7817SJulian Elischer * Now check if it is possibly one of OUR sockets. 1312ed5b7817SJulian Elischer */ 1313df8bae1dSRodney W. Grimes if (fp->f_type != DTYPE_SOCKET || 131448e3128bSMatthew Dillon (so = fp->f_data) == NULL) { 1315426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1316df8bae1dSRodney W. Grimes continue; 1317426da3bcSAlfred Perlstein } 1318426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1319748e0b0aSGarrett Wollman if (so->so_proto->pr_domain != &localdomain || 1320df8bae1dSRodney W. Grimes (so->so_proto->pr_flags&PR_RIGHTS) == 0) 1321df8bae1dSRodney W. Grimes continue; 1322df8bae1dSRodney W. Grimes #ifdef notdef 1323df8bae1dSRodney W. Grimes if (so->so_rcv.sb_flags & SB_LOCK) { 1324df8bae1dSRodney W. Grimes /* 1325df8bae1dSRodney W. Grimes * This is problematical; it's not clear 1326df8bae1dSRodney W. Grimes * we need to wait for the sockbuf to be 1327df8bae1dSRodney W. Grimes * unlocked (on a uniprocessor, at least), 1328df8bae1dSRodney W. Grimes * and it's also not clear what to do 1329df8bae1dSRodney W. Grimes * if sbwait returns an error due to receipt 1330df8bae1dSRodney W. Grimes * of a signal. If sbwait does return 1331df8bae1dSRodney W. Grimes * an error, we'll go into an infinite 1332df8bae1dSRodney W. Grimes * loop. Delete all of this for now. 1333df8bae1dSRodney W. Grimes */ 1334df8bae1dSRodney W. Grimes (void) sbwait(&so->so_rcv); 1335df8bae1dSRodney W. Grimes goto restart; 1336df8bae1dSRodney W. Grimes } 1337df8bae1dSRodney W. Grimes #endif 1338ed5b7817SJulian Elischer /* 1339ed5b7817SJulian Elischer * So, Ok, it's one of our sockets and it IS externally 1340ed5b7817SJulian Elischer * accessible (or was defered). Now we look 1341dc733423SDag-Erling Smørgrav * to see if we hold any file descriptors in its 1342ed5b7817SJulian Elischer * message buffers. Follow those links and mark them 1343ed5b7817SJulian Elischer * as accessible too. 1344ed5b7817SJulian Elischer */ 1345df8bae1dSRodney W. Grimes unp_scan(so->so_rcv.sb_mb, unp_mark); 1346df8bae1dSRodney W. Grimes } 1347df8bae1dSRodney W. Grimes } while (unp_defer); 1348426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1349df8bae1dSRodney W. Grimes /* 1350df8bae1dSRodney W. Grimes * We grab an extra reference to each of the file table entries 1351df8bae1dSRodney W. Grimes * that are not otherwise accessible and then free the rights 1352df8bae1dSRodney W. Grimes * that are stored in messages on them. 1353df8bae1dSRodney W. Grimes * 1354df8bae1dSRodney W. Grimes * The bug in the orginal code is a little tricky, so I'll describe 1355df8bae1dSRodney W. Grimes * what's wrong with it here. 1356df8bae1dSRodney W. Grimes * 1357df8bae1dSRodney W. Grimes * It is incorrect to simply unp_discard each entry for f_msgcount 1358df8bae1dSRodney W. Grimes * times -- consider the case of sockets A and B that contain 1359df8bae1dSRodney W. Grimes * references to each other. On a last close of some other socket, 1360df8bae1dSRodney W. Grimes * we trigger a gc since the number of outstanding rights (unp_rights) 1361df8bae1dSRodney W. Grimes * is non-zero. If during the sweep phase the gc code un_discards, 1362df8bae1dSRodney W. Grimes * we end up doing a (full) closef on the descriptor. A closef on A 1363df8bae1dSRodney W. Grimes * results in the following chain. Closef calls soo_close, which 1364df8bae1dSRodney W. Grimes * calls soclose. Soclose calls first (through the switch 1365df8bae1dSRodney W. Grimes * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply 1366df8bae1dSRodney W. Grimes * returns because the previous instance had set unp_gcing, and 1367df8bae1dSRodney W. Grimes * we return all the way back to soclose, which marks the socket 1368df8bae1dSRodney W. Grimes * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush 1369df8bae1dSRodney W. Grimes * to free up the rights that are queued in messages on the socket A, 1370df8bae1dSRodney W. Grimes * i.e., the reference on B. The sorflush calls via the dom_dispose 1371df8bae1dSRodney W. Grimes * switch unp_dispose, which unp_scans with unp_discard. This second 1372df8bae1dSRodney W. Grimes * instance of unp_discard just calls closef on B. 1373df8bae1dSRodney W. Grimes * 1374df8bae1dSRodney W. Grimes * Well, a similar chain occurs on B, resulting in a sorflush on B, 1375df8bae1dSRodney W. Grimes * which results in another closef on A. Unfortunately, A is already 1376df8bae1dSRodney W. Grimes * being closed, and the descriptor has already been marked with 1377df8bae1dSRodney W. Grimes * SS_NOFDREF, and soclose panics at this point. 1378df8bae1dSRodney W. Grimes * 1379df8bae1dSRodney W. Grimes * Here, we first take an extra reference to each inaccessible 1380df8bae1dSRodney W. Grimes * descriptor. Then, we call sorflush ourself, since we know 1381df8bae1dSRodney W. Grimes * it is a Unix domain socket anyhow. After we destroy all the 1382df8bae1dSRodney W. Grimes * rights carried in messages, we do a last closef to get rid 1383df8bae1dSRodney W. Grimes * of our extra reference. This is the last close, and the 1384df8bae1dSRodney W. Grimes * unp_detach etc will shut down the socket. 1385df8bae1dSRodney W. Grimes * 1386df8bae1dSRodney W. Grimes * 91/09/19, bsy@cs.cmu.edu 1387df8bae1dSRodney W. Grimes */ 1388a163d034SWarner Losh extra_ref = malloc(nfiles * sizeof(struct file *), M_TEMP, M_WAITOK); 1389426da3bcSAlfred Perlstein sx_slock(&filelist_lock); 13902e3c8fcbSPoul-Henning Kamp for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; fp != 0; 1391bc6f0e79SJeffrey Hsu fp = nextfp) { 13922e3c8fcbSPoul-Henning Kamp nextfp = LIST_NEXT(fp, f_list); 1393426da3bcSAlfred Perlstein FILE_LOCK(fp); 1394ed5b7817SJulian Elischer /* 1395ed5b7817SJulian Elischer * If it's not open, skip it 1396ed5b7817SJulian Elischer */ 1397426da3bcSAlfred Perlstein if (fp->f_count == 0) { 1398426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1399df8bae1dSRodney W. Grimes continue; 1400426da3bcSAlfred Perlstein } 1401ed5b7817SJulian Elischer /* 1402ed5b7817SJulian Elischer * If all refs are from msgs, and it's not marked accessible 1403ed5b7817SJulian Elischer * then it must be referenced from some unreachable cycle 1404ed5b7817SJulian Elischer * of (shut-down) FDs, so include it in our 1405ed5b7817SJulian Elischer * list of FDs to remove 1406ed5b7817SJulian Elischer */ 1407426da3bcSAlfred Perlstein if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) { 1408df8bae1dSRodney W. Grimes *fpp++ = fp; 1409df8bae1dSRodney W. Grimes nunref++; 1410df8bae1dSRodney W. Grimes fp->f_count++; 1411df8bae1dSRodney W. Grimes } 1412426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1413df8bae1dSRodney W. Grimes } 1414426da3bcSAlfred Perlstein sx_sunlock(&filelist_lock); 1415ed5b7817SJulian Elischer /* 1416ed5b7817SJulian Elischer * for each FD on our hit list, do the following two things 1417ed5b7817SJulian Elischer */ 14181c7c3c6aSMatthew Dillon for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) { 14191c7c3c6aSMatthew Dillon struct file *tfp = *fpp; 1420426da3bcSAlfred Perlstein FILE_LOCK(tfp); 1421cd72f218SMatthew Dillon if (tfp->f_type == DTYPE_SOCKET && 142248e3128bSMatthew Dillon tfp->f_data != NULL) { 1423426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 142448e3128bSMatthew Dillon sorflush(tfp->f_data); 1425426da3bcSAlfred Perlstein } else 1426426da3bcSAlfred Perlstein FILE_UNLOCK(tfp); 14271c7c3c6aSMatthew Dillon } 1428df8bae1dSRodney W. Grimes for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) 1429b40ce416SJulian Elischer closef(*fpp, (struct thread *) NULL); 1430210a5a71SAlfred Perlstein free(extra_ref, M_TEMP); 1431df8bae1dSRodney W. Grimes unp_gcing = 0; 1432df8bae1dSRodney W. Grimes } 1433df8bae1dSRodney W. Grimes 143426f9a767SRodney W. Grimes void 1435df8bae1dSRodney W. Grimes unp_dispose(m) 1436df8bae1dSRodney W. Grimes struct mbuf *m; 1437df8bae1dSRodney W. Grimes { 1438996c772fSJohn Dyson 1439df8bae1dSRodney W. Grimes if (m) 1440df8bae1dSRodney W. Grimes unp_scan(m, unp_discard); 1441df8bae1dSRodney W. Grimes } 1442df8bae1dSRodney W. Grimes 14430c1bb4fbSDima Dorfman static int 14446f105b34SJohn Baldwin unp_listen(unp, td) 14450c1bb4fbSDima Dorfman struct unpcb *unp; 14466f105b34SJohn Baldwin struct thread *td; 14470c1bb4fbSDima Dorfman { 14480c1bb4fbSDima Dorfman 14496f105b34SJohn Baldwin cru2x(td->td_ucred, &unp->unp_peercred); 14500c1bb4fbSDima Dorfman unp->unp_flags |= UNP_HAVEPCCACHED; 14510c1bb4fbSDima Dorfman return (0); 14520c1bb4fbSDima Dorfman } 14530c1bb4fbSDima Dorfman 1454f708ef1bSPoul-Henning Kamp static void 1455df8bae1dSRodney W. Grimes unp_scan(m0, op) 1456df8bae1dSRodney W. Grimes register struct mbuf *m0; 14574d77a549SAlfred Perlstein void (*op)(struct file *); 1458df8bae1dSRodney W. Grimes { 14592bc21ed9SDavid Malone struct mbuf *m; 14602bc21ed9SDavid Malone struct file **rp; 14612bc21ed9SDavid Malone struct cmsghdr *cm; 14622bc21ed9SDavid Malone void *data; 14632bc21ed9SDavid Malone int i; 14642bc21ed9SDavid Malone socklen_t clen, datalen; 1465df8bae1dSRodney W. Grimes int qfds; 1466df8bae1dSRodney W. Grimes 1467df8bae1dSRodney W. Grimes while (m0) { 14682bc21ed9SDavid Malone for (m = m0; m; m = m->m_next) { 146912396bdcSDavid Malone if (m->m_type != MT_CONTROL) 1470df8bae1dSRodney W. Grimes continue; 14712bc21ed9SDavid Malone 14722bc21ed9SDavid Malone cm = mtod(m, struct cmsghdr *); 14732bc21ed9SDavid Malone clen = m->m_len; 14742bc21ed9SDavid Malone 14752bc21ed9SDavid Malone while (cm != NULL) { 14762bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen) 14772bc21ed9SDavid Malone break; 14782bc21ed9SDavid Malone 14792bc21ed9SDavid Malone data = CMSG_DATA(cm); 14802bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len 14812bc21ed9SDavid Malone - (caddr_t)data; 14822bc21ed9SDavid Malone 14832bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET && 14842bc21ed9SDavid Malone cm->cmsg_type == SCM_RIGHTS) { 14852bc21ed9SDavid Malone qfds = datalen / sizeof (struct file *); 14862bc21ed9SDavid Malone rp = data; 1487df8bae1dSRodney W. Grimes for (i = 0; i < qfds; i++) 1488df8bae1dSRodney W. Grimes (*op)(*rp++); 14892bc21ed9SDavid Malone } 14902bc21ed9SDavid Malone 14912bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) { 14922bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen); 14932bc21ed9SDavid Malone cm = (struct cmsghdr *) 14942bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen)); 14952bc21ed9SDavid Malone } else { 14962bc21ed9SDavid Malone clen = 0; 14972bc21ed9SDavid Malone cm = NULL; 14982bc21ed9SDavid Malone } 14992bc21ed9SDavid Malone } 1500df8bae1dSRodney W. Grimes } 1501df8bae1dSRodney W. Grimes m0 = m0->m_act; 1502df8bae1dSRodney W. Grimes } 1503df8bae1dSRodney W. Grimes } 1504df8bae1dSRodney W. Grimes 1505f708ef1bSPoul-Henning Kamp static void 1506df8bae1dSRodney W. Grimes unp_mark(fp) 1507df8bae1dSRodney W. Grimes struct file *fp; 1508df8bae1dSRodney W. Grimes { 1509426da3bcSAlfred Perlstein if (fp->f_gcflag & FMARK) 1510df8bae1dSRodney W. Grimes return; 1511df8bae1dSRodney W. Grimes unp_defer++; 1512426da3bcSAlfred Perlstein fp->f_gcflag |= (FMARK|FDEFER); 1513df8bae1dSRodney W. Grimes } 1514df8bae1dSRodney W. Grimes 1515f708ef1bSPoul-Henning Kamp static void 1516df8bae1dSRodney W. Grimes unp_discard(fp) 1517df8bae1dSRodney W. Grimes struct file *fp; 1518df8bae1dSRodney W. Grimes { 1519426da3bcSAlfred Perlstein FILE_LOCK(fp); 1520df8bae1dSRodney W. Grimes fp->f_msgcount--; 1521df8bae1dSRodney W. Grimes unp_rights--; 1522426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 1523b40ce416SJulian Elischer (void) closef(fp, (struct thread *)NULL); 1524df8bae1dSRodney W. Grimes } 1525