2  * Copyright (c) 2000-2015 Apple Inc. All rights reserved. 
   4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 
   6  * This file contains Original Code and/or Modifications of Original Code 
   7  * as defined in and that are subject to the Apple Public Source License 
   8  * Version 2.0 (the 'License'). You may not use this file except in 
   9  * compliance with the License. The rights granted to you under the License 
  10  * may not be used to create, or enable the creation or redistribution of, 
  11  * unlawful or unlicensed copies of an Apple operating system, or to 
  12  * circumvent, violate, or enable the circumvention or violation of, any 
  13  * terms of an Apple operating system software license agreement. 
  15  * Please obtain a copy of the License at 
  16  * http://www.opensource.apple.com/apsl/ and read it before using this file. 
  18  * The Original Code and all software distributed under the License are 
  19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
  20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
  21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 
  22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
  23  * Please see the License for the specific language governing rights and 
  24  * limitations under the License. 
  26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 
  29  * Copyright (c) 1982, 1986, 1989, 1991, 1993 
  30  *      The Regents of the University of California.  All rights reserved. 
  32  * Redistribution and use in source and binary forms, with or without 
  33  * modification, are permitted provided that the following conditions 
  35  * 1. Redistributions of source code must retain the above copyright 
  36  *    notice, this list of conditions and the following disclaimer. 
  37  * 2. Redistributions in binary form must reproduce the above copyright 
  38  *    notice, this list of conditions and the following disclaimer in the 
  39  *    documentation and/or other materials provided with the distribution. 
  40  * 3. All advertising materials mentioning features or use of this software 
  41  *    must display the following acknowledgement: 
  42  *      This product includes software developed by the University of 
  43  *      California, Berkeley and its contributors. 
  44  * 4. Neither the name of the University nor the names of its contributors 
  45  *    may be used to endorse or promote products derived from this software 
  46  *    without specific prior written permission. 
  48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 
  49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
  50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
  51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 
  52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
  53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
  54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
  56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
  57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
  60  *      From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94 
  63  * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce 
  64  * support for mandatory and extensible security protections.  This notice 
  65  * is included in support of clause 2.2 (b) of the Apple Public License, 
  69 #include <sys/param.h> 
  70 #include <sys/systm.h> 
  71 #include <sys/kernel.h> 
  72 #include <sys/domain.h> 
  73 #include <sys/fcntl.h> 
  74 #include <sys/malloc.h>         /* XXX must be before <sys/file.h> */ 
  75 #include <sys/file_internal.h> 
  76 #include <sys/guarded.h> 
  77 #include <sys/filedesc.h> 
  80 #include <sys/namei.h> 
  81 #include <sys/proc_internal.h> 
  82 #include <sys/kauth.h> 
  83 #include <sys/protosw.h> 
  84 #include <sys/socket.h> 
  85 #include <sys/socketvar.h> 
  87 #include <sys/sysctl.h> 
  89 #include <sys/unpcb.h> 
  90 #include <sys/vnode_internal.h> 
  91 #include <sys/kdebug.h> 
  92 #include <sys/mcache.h> 
  94 #include <kern/zalloc.h> 
  95 #include <kern/locks.h> 
  98 #include <security/mac_framework.h> 
  99 #endif /* CONFIG_MACF */ 
 101 #include <mach/vm_param.h> 
 104  * Maximum number of FDs that can be passed in an mbuf 
 106 #define UIPC_MAX_CMSG_FD        512 
 108 #define f_msgcount f_fglob->fg_msgcount 
 109 #define f_cred f_fglob->fg_cred 
 110 #define f_ops f_fglob->fg_ops 
 111 #define f_offset f_fglob->fg_offset 
 112 #define f_data f_fglob->fg_data 
 113 struct  zone 
*unp_zone
; 
 114 static  unp_gen_t unp_gencnt
; 
 115 static  u_int unp_count
; 
 117 static  lck_attr_t              
*unp_mtx_attr
; 
 118 static  lck_grp_t               
*unp_mtx_grp
; 
 119 static  lck_grp_attr_t          
*unp_mtx_grp_attr
; 
 120 static  lck_rw_t                
*unp_list_mtx
; 
 122 static  lck_mtx_t               
*unp_disconnect_lock
; 
 123 static  lck_mtx_t               
*unp_connect_lock
; 
 124 static  u_int                   disconnect_in_progress
; 
 126 extern lck_mtx_t 
*uipc_lock
; 
 127 static  struct unp_head unp_shead
, unp_dhead
; 
 130  * mDNSResponder tracing.  When enabled, endpoints connected to 
 131  * /var/run/mDNSResponder will be traced; during each send on 
 132  * the traced socket, we log the PID and process name of the 
 133  * sending process.  We also print out a bit of info related 
 134  * to the data itself; this assumes ipc_msg_hdr in dnssd_ipc.h 
 135  * of mDNSResponder stays the same. 
 137 #define MDNSRESPONDER_PATH      "/var/run/mDNSResponder" 
 139 static int unpst_tracemdns
;     /* enable tracing */ 
 141 #define MDNS_IPC_MSG_HDR_VERSION_1      1 
 143 struct mdns_ipc_msg_hdr 
{ 
 151         } __attribute__((packed
)); 
 153 } __attribute__((packed
)); 
 156  * Unix communications domain. 
 160  *      rethink name space problems 
 161  *      need a proper out-of-band 
 164 static struct   sockaddr sun_noname 
= { sizeof (sun_noname
), AF_LOCAL
, { 0 } }; 
 165 static ino_t    unp_ino
;                /* prototype for fake inode numbers */ 
 167 static int      unp_attach(struct socket 
*); 
 168 static void     unp_detach(struct unpcb 
*); 
 169 static int      unp_bind(struct unpcb 
*, struct sockaddr 
*, proc_t
); 
 170 static int      unp_connect(struct socket 
*, struct sockaddr 
*, proc_t
); 
 171 static void     unp_disconnect(struct unpcb 
*); 
 172 static void     unp_shutdown(struct unpcb 
*); 
 173 static void     unp_drop(struct unpcb 
*, int); 
 174 __private_extern__ 
void unp_gc(void); 
 175 static void     unp_scan(struct mbuf 
*, void (*)(struct fileglob 
*, void *arg
), void *arg
); 
 176 static void     unp_mark(struct fileglob 
*, __unused 
void *); 
 177 static void     unp_discard(struct fileglob 
*, void *); 
 178 static int      unp_internalize(struct mbuf 
*, proc_t
); 
 179 static int      unp_listen(struct unpcb 
*, proc_t
); 
 180 static void     unpcb_to_compat(struct unpcb 
*, struct unpcb_compat 
*); 
 181 static void     unp_get_locks_in_order(struct socket 
*so
, struct socket 
*conn_so
); 
 184 unp_get_locks_in_order(struct socket 
*so
, struct socket 
*conn_so
)  
 187                 socket_lock(conn_so
, 1); 
 189                 struct unpcb 
*unp 
= sotounpcb(so
); 
 190                 unp
->unp_flags 
|= UNP_DONTDISCONNECT
; 
 192                 socket_unlock(so
, 0); 
 194                 /* Get the locks in the correct order */ 
 195                 socket_lock(conn_so
, 1); 
 198                 if (unp
->rw_thrcount 
== 0) { 
 199                         unp
->unp_flags 
&= ~UNP_DONTDISCONNECT
; 
 206 uipc_abort(struct socket 
*so
) 
 208         struct unpcb 
*unp 
= sotounpcb(so
); 
 212         unp_drop(unp
, ECONNABORTED
); 
 219 uipc_accept(struct socket 
*so
, struct sockaddr 
**nam
) 
 221         struct unpcb 
*unp 
= sotounpcb(so
); 
 227          * Pass back name of connected socket, 
 228          * if it was bound and we are still connected 
 229          * (our peer may have closed already!). 
 231         if (unp
->unp_conn 
&& unp
->unp_conn
->unp_addr
) { 
 232                 *nam 
= dup_sockaddr((struct sockaddr 
*) 
 233                     unp
->unp_conn
->unp_addr
, 1); 
 235                 *nam 
= dup_sockaddr((struct sockaddr 
*)&sun_noname
, 1); 
 246 uipc_attach(struct socket 
*so
, __unused 
int proto
, __unused proc_t p
) 
 248         struct unpcb 
*unp 
= sotounpcb(so
); 
 252         return (unp_attach(so
)); 
 256 uipc_bind(struct socket 
*so
, struct sockaddr 
*nam
, proc_t p
) 
 258         struct unpcb 
*unp 
= sotounpcb(so
); 
 263         return (unp_bind(unp
, nam
, p
)); 
 269  *      unp_connect:???                 [See elsewhere in this file] 
 272 uipc_connect(struct socket 
*so
, struct sockaddr 
*nam
, proc_t p
) 
 274         struct unpcb 
*unp 
= sotounpcb(so
); 
 278         return (unp_connect(so
, nam
, p
)); 
 284  *      unp_connect2:EPROTOTYPE         Protocol wrong type for socket 
 285  *      unp_connect2:EINVAL             Invalid argument 
 288 uipc_connect2(struct socket 
*so1
, struct socket 
*so2
) 
 290         struct unpcb 
*unp 
= sotounpcb(so1
); 
 295         return (unp_connect2(so1
, so2
)); 
 298 /* control is EOPNOTSUPP */ 
 301 uipc_detach(struct socket 
*so
) 
 303         struct unpcb 
*unp 
= sotounpcb(so
); 
 308         LCK_MTX_ASSERT(&unp
->unp_mtx
, LCK_MTX_ASSERT_OWNED
); 
 314 uipc_disconnect(struct socket 
*so
) 
 316         struct unpcb 
*unp 
= sotounpcb(so
); 
 329 uipc_listen(struct socket 
*so
, __unused proc_t p
) 
 331         struct unpcb 
*unp 
= sotounpcb(so
); 
 333         if (unp 
== 0 || unp
->unp_vnode 
== 0) 
 335         return (unp_listen(unp
, p
)); 
 339 uipc_peeraddr(struct socket 
*so
, struct sockaddr 
**nam
) 
 341         struct unpcb 
*unp 
= sotounpcb(so
); 
 345         if (unp
->unp_conn 
!= NULL 
&& unp
->unp_conn
->unp_addr 
!= NULL
) { 
 346                 *nam 
= dup_sockaddr((struct sockaddr 
*) 
 347                     unp
->unp_conn
->unp_addr
, 1); 
 349                 *nam 
= dup_sockaddr((struct sockaddr 
*)&sun_noname
, 1); 
 355 uipc_rcvd(struct socket 
*so
, __unused 
int flags
) 
 357         struct unpcb 
*unp 
= sotounpcb(so
); 
 362         switch (so
->so_type
) { 
 364                 panic("uipc_rcvd DGRAM?"); 
 368 #define rcv (&so->so_rcv) 
 369 #define snd (&so2->so_snd) 
 370                 if (unp
->unp_conn 
== 0) 
 373                 so2 
= unp
->unp_conn
->unp_socket
; 
 374                 unp_get_locks_in_order(so
, so2
); 
 376                  * Adjust backpressure on sender 
 377                  * and wakeup any waiting to write. 
 379                 snd
->sb_mbmax 
+= unp
->unp_mbcnt 
- rcv
->sb_mbcnt
; 
 380                 unp
->unp_mbcnt 
= rcv
->sb_mbcnt
; 
 381                 snd
->sb_hiwat 
+= unp
->unp_cc 
- rcv
->sb_cc
; 
 382                 unp
->unp_cc 
= rcv
->sb_cc
; 
 385                 socket_unlock(so2
, 1); 
 392                 panic("uipc_rcvd unknown socktype"); 
 397 /* pru_rcvoob is EOPNOTSUPP */ 
 406  *      unp_internalize:EINVAL 
 407  *      unp_internalize:EBADF 
 408  *      unp_connect:EAFNOSUPPORT        Address family not supported 
 409  *      unp_connect:EINVAL              Invalid argument 
 410  *      unp_connect:ENOTSOCK            Not a socket 
 411  *      unp_connect:ECONNREFUSED        Connection refused 
 412  *      unp_connect:EISCONN             Socket is connected 
 413  *      unp_connect:EPROTOTYPE          Protocol wrong type for socket 
 415  *      sbappendaddr:ENOBUFS            [5th argument, contents modified] 
 416  *      sbappendaddr:???                [whatever a filter author chooses] 
 419 uipc_send(struct socket 
*so
, int flags
, struct mbuf 
*m
, struct sockaddr 
*nam
, 
 420     struct mbuf 
*control
, proc_t p
) 
 423         struct unpcb 
*unp 
= sotounpcb(so
); 
 430         if (flags 
& PRUS_OOB
) { 
 436                 /* release lock to avoid deadlock (4436174) */ 
 437                 socket_unlock(so
, 0); 
 438                 error 
= unp_internalize(control
, p
); 
 444         switch (so
->so_type
) { 
 447                 struct sockaddr 
*from
; 
 454                         error 
= unp_connect(so
, nam
, p
); 
 458                         if (unp
->unp_conn 
== 0) { 
 464                 so2 
= unp
->unp_conn
->unp_socket
; 
 466                         unp_get_locks_in_order(so
, so2
); 
 469                         from 
= (struct sockaddr 
*)unp
->unp_addr
; 
 473                  * sbappendaddr() will fail when the receiver runs out of 
 474                  * space; in contrast to SOCK_STREAM, we will lose messages 
 475                  * for the SOCK_DGRAM case when the receiver's queue overflows. 
 476                  * SB_UNIX on the socket buffer implies that the callee will 
 477                  * not free the control message, if any, because we would need 
 478                  * to call unp_dispose() on it. 
 480                 if (sbappendaddr(&so2
->so_rcv
, from
, m
, control
, &error
)) { 
 483                 } else if (control 
!= NULL 
&& error 
== 0) { 
 484                         /* A socket filter took control; don't touch it */ 
 489                         socket_unlock(so2
, 1); 
 499 #define rcv (&so2->so_rcv) 
 500 #define snd (&so->so_snd) 
 501                 /* Connect if not connected yet. */ 
 503                  * Note: A better implementation would complain 
 504                  * if not equal to the peer's address. 
 506                 if ((so
->so_state 
& SS_ISCONNECTED
) == 0) { 
 508                                 error 
= unp_connect(so
, nam
, p
); 
 517                 if (so
->so_state 
& SS_CANTSENDMORE
) { 
 521                 if (unp
->unp_conn 
== 0) 
 522                         panic("uipc_send connected but no connection?"); 
 524                 so2 
= unp
->unp_conn
->unp_socket
; 
 525                 unp_get_locks_in_order(so
, so2
); 
 527                 /* Check socket state again as we might have unlocked the socket  
 528                  * while trying to get the locks in order 
 531                 if ((so
->so_state 
& SS_CANTSENDMORE
)) { 
 533                         socket_unlock(so2
, 1); 
 537                 if (unp
->unp_flags 
& UNP_TRACE_MDNS
) { 
 538                         struct mdns_ipc_msg_hdr hdr
; 
 540                         if (mbuf_copydata(m
, 0, sizeof (hdr
), &hdr
) == 0 && 
 541                             hdr
.version  
== ntohl(MDNS_IPC_MSG_HDR_VERSION_1
)) { 
 542                                 printf("%s[mDNSResponder] pid=%d (%s): op=0x%x\n", 
 543                                     __func__
, p
->p_pid
, p
->p_comm
, ntohl(hdr
.op
)); 
 548                  * Send to paired receive port, and then reduce send buffer 
 549                  * hiwater marks to maintain backpressure.  Wake up readers. 
 550                  * SB_UNIX flag will allow new record to be appended to the 
 551                  * receiver's queue even when it is already full.  It is 
 552                  * possible, however, that append might fail.  In that case, 
 553                  * we will need to call unp_dispose() on the control message; 
 554                  * the callee will not free it since SB_UNIX is set. 
 556                 didreceive 
= control 
? 
 557                     sbappendcontrol(rcv
, m
, control
, &error
) : sbappend(rcv
, m
); 
 559                 snd
->sb_mbmax 
-= rcv
->sb_mbcnt 
- unp
->unp_conn
->unp_mbcnt
; 
 560                 unp
->unp_conn
->unp_mbcnt 
= rcv
->sb_mbcnt
; 
 561                 if ((int32_t)snd
->sb_hiwat 
>=  
 562                     (int32_t)(rcv
->sb_cc 
- unp
->unp_conn
->unp_cc
)) { 
 563                         snd
->sb_hiwat 
-= rcv
->sb_cc 
- unp
->unp_conn
->unp_cc
; 
 567                 unp
->unp_conn
->unp_cc 
= rcv
->sb_cc
; 
 571                 } else if (control 
!= NULL 
&& error 
== 0) { 
 572                         /* A socket filter took control; don't touch it */ 
 576                 socket_unlock(so2
, 1); 
 584                 panic("uipc_send unknown socktype"); 
 588          * SEND_EOF is equivalent to a SEND followed by 
 591         if (flags 
& PRUS_EOF
) { 
 596         if (control 
&& error 
!= 0) { 
 597                 socket_unlock(so
, 0); 
 598                 unp_dispose(control
); 
 611 uipc_sense(struct socket 
*so
, void *ub
, int isstat64
) 
 613         struct unpcb 
*unp 
= sotounpcb(so
); 
 620         blksize 
= so
->so_snd
.sb_hiwat
; 
 621         if (so
->so_type 
== SOCK_STREAM 
&& unp
->unp_conn 
!= 0) { 
 622                 so2 
= unp
->unp_conn
->unp_socket
; 
 623                 blksize 
+= so2
->so_rcv
.sb_cc
; 
 625         if (unp
->unp_ino 
== 0) 
 626                 unp
->unp_ino 
= unp_ino
++; 
 631                 sb64 
= (struct stat64 
*)ub
; 
 632                 sb64
->st_blksize 
= blksize
; 
 633                 sb64
->st_dev 
= NODEV
; 
 634                 sb64
->st_ino 
= (ino64_t
)unp
->unp_ino
; 
 638                 sb 
= (struct stat 
*)ub
; 
 639                 sb
->st_blksize 
= blksize
; 
 641                 sb
->st_ino 
= (ino_t
)(uintptr_t)unp
->unp_ino
; 
 651  * Notes:       This is not strictly correct, as unp_shutdown() also calls 
 652  *              socantrcvmore().  These should maybe both be conditionalized 
 653  *              on the 'how' argument in soshutdown() as called from the 
 654  *              shutdown() system call. 
 657 uipc_shutdown(struct socket 
*so
) 
 659         struct unpcb 
*unp 
= sotounpcb(so
); 
 670  *              EINVAL                  Invalid argument 
 673 uipc_sockaddr(struct socket 
*so
, struct sockaddr 
**nam
) 
 675         struct unpcb 
*unp 
= sotounpcb(so
); 
 679         if (unp
->unp_addr 
!= NULL
) { 
 680                 *nam 
= dup_sockaddr((struct sockaddr 
*)unp
->unp_addr
, 1); 
 682                 *nam 
= dup_sockaddr((struct sockaddr 
*)&sun_noname
, 1); 
 687 struct pr_usrreqs uipc_usrreqs 
= { 
 688         .pru_abort 
=            uipc_abort
, 
 689         .pru_accept 
=           uipc_accept
, 
 690         .pru_attach 
=           uipc_attach
, 
 691         .pru_bind 
=             uipc_bind
, 
 692         .pru_connect 
=          uipc_connect
, 
 693         .pru_connect2 
=         uipc_connect2
, 
 694         .pru_detach 
=           uipc_detach
, 
 695         .pru_disconnect 
=       uipc_disconnect
, 
 696         .pru_listen 
=           uipc_listen
, 
 697         .pru_peeraddr 
=         uipc_peeraddr
, 
 698         .pru_rcvd 
=             uipc_rcvd
, 
 699         .pru_send 
=             uipc_send
, 
 700         .pru_sense 
=            uipc_sense
, 
 701         .pru_shutdown 
=         uipc_shutdown
, 
 702         .pru_sockaddr 
=         uipc_sockaddr
, 
 703         .pru_sosend 
=           sosend
, 
 704         .pru_soreceive 
=        soreceive
, 
 708 uipc_ctloutput(struct socket 
*so
, struct sockopt 
*sopt
) 
 710         struct unpcb 
*unp 
= sotounpcb(so
); 
 713         struct socket 
*peerso
; 
 715         switch (sopt
->sopt_dir
) { 
 717                 switch (sopt
->sopt_name
) { 
 719                         if (unp
->unp_flags 
& UNP_HAVEPC
) { 
 720                                 error 
= sooptcopyout(sopt
, &unp
->unp_peercred
, 
 721                                     sizeof (unp
->unp_peercred
)); 
 723                                 if (so
->so_type 
== SOCK_STREAM
) 
 731                         if (unp
->unp_conn 
== NULL
) { 
 735                         peerso 
= unp
->unp_conn
->unp_socket
; 
 737                                 panic("peer is connected but has no socket?"); 
 738                         unp_get_locks_in_order(so
, peerso
); 
 739                         if (sopt
->sopt_name 
== LOCAL_PEEREPID 
&& 
 740                             peerso
->so_flags 
& SOF_DELEGATED
) 
 741                                 peerpid 
= peerso
->e_pid
; 
 743                                 peerpid 
= peerso
->last_pid
; 
 744                         socket_unlock(peerso
, 1); 
 745                         error 
= sooptcopyout(sopt
, &peerpid
, sizeof (peerpid
)); 
 748                 case LOCAL_PEEREUUID
: 
 749                         if (unp
->unp_conn 
== NULL
) { 
 753                         peerso 
= unp
->unp_conn
->unp_socket
; 
 755                                 panic("peer is connected but has no socket?"); 
 756                         unp_get_locks_in_order(so
, peerso
); 
 757                         if (sopt
->sopt_name 
== LOCAL_PEEREUUID 
&& 
 758                             peerso
->so_flags 
& SOF_DELEGATED
) 
 759                                 error 
= sooptcopyout(sopt
, &peerso
->e_uuid
, 
 760                                     sizeof (peerso
->e_uuid
)); 
 762                                 error 
= sooptcopyout(sopt
, &peerso
->last_uuid
, 
 763                                     sizeof (peerso
->last_uuid
)); 
 764                         socket_unlock(peerso
, 1); 
 781  * Both send and receive buffers are allocated PIPSIZ bytes of buffering 
 782  * for stream sockets, although the total for sender and receiver is 
 783  * actually only PIPSIZ. 
 784  * Datagram sockets really use the sendspace as the maximum datagram size, 
 785  * and don't really want to reserve the sendspace.  Their recvspace should 
 786  * be large enough for at least one max-size datagram plus address. 
 791 static u_int32_t        unpst_sendspace 
= PIPSIZ
; 
 792 static u_int32_t        unpst_recvspace 
= PIPSIZ
; 
 793 static u_int32_t        unpdg_sendspace 
= 2*1024;       /* really max datagram size */ 
 794 static u_int32_t        unpdg_recvspace 
= 4*1024; 
 796 static int      unp_rights
;                     /* file descriptors in flight */ 
 797 static int      unp_disposed
;                   /* discarded file descriptors */ 
 799 SYSCTL_DECL(_net_local_stream
); 
 800 SYSCTL_INT(_net_local_stream
, OID_AUTO
, sendspace
, CTLFLAG_RW 
| CTLFLAG_LOCKED
, 
 801    &unpst_sendspace
, 0, ""); 
 802 SYSCTL_INT(_net_local_stream
, OID_AUTO
, recvspace
, CTLFLAG_RW 
| CTLFLAG_LOCKED
, 
 803    &unpst_recvspace
, 0, ""); 
 804 SYSCTL_INT(_net_local_stream
, OID_AUTO
, tracemdns
, CTLFLAG_RW 
| CTLFLAG_LOCKED
, 
 805    &unpst_tracemdns
, 0, ""); 
 806 SYSCTL_DECL(_net_local_dgram
); 
 807 SYSCTL_INT(_net_local_dgram
, OID_AUTO
, maxdgram
, CTLFLAG_RW 
| CTLFLAG_LOCKED
, 
 808    &unpdg_sendspace
, 0, ""); 
 809 SYSCTL_INT(_net_local_dgram
, OID_AUTO
, recvspace
, CTLFLAG_RW 
| CTLFLAG_LOCKED
, 
 810    &unpdg_recvspace
, 0, ""); 
 811 SYSCTL_DECL(_net_local
); 
 812 SYSCTL_INT(_net_local
, OID_AUTO
, inflight
, CTLFLAG_RD 
| CTLFLAG_LOCKED
, &unp_rights
, 0, ""); 
 820 unp_attach(struct socket 
*so
) 
 825         if (so
->so_snd
.sb_hiwat 
== 0 || so
->so_rcv
.sb_hiwat 
== 0) { 
 826                 switch (so
->so_type
) { 
 829                         error 
= soreserve(so
, unpst_sendspace
, unpst_recvspace
); 
 833                         error 
= soreserve(so
, unpdg_sendspace
, unpdg_recvspace
); 
 842         unp 
= (struct unpcb 
*)zalloc(unp_zone
); 
 845         bzero(unp
, sizeof (*unp
)); 
 847         lck_mtx_init(&unp
->unp_mtx
,  
 848                 unp_mtx_grp
, unp_mtx_attr
); 
 850         lck_rw_lock_exclusive(unp_list_mtx
); 
 851         LIST_INIT(&unp
->unp_refs
); 
 852         unp
->unp_socket 
= so
; 
 853         unp
->unp_gencnt 
= ++unp_gencnt
; 
 855         LIST_INSERT_HEAD(so
->so_type 
== SOCK_DGRAM 
? 
 856             &unp_dhead 
: &unp_shead
, unp
, unp_link
); 
 857         lck_rw_done(unp_list_mtx
); 
 858         so
->so_pcb 
= (caddr_t
)unp
; 
 860          * Mark AF_UNIX socket buffers accordingly so that: 
 862          * a. In the SOCK_STREAM case, socket buffer append won't fail due to 
 863          *    the lack of space; this essentially loosens the sbspace() check, 
 864          *    since there is disconnect between sosend() and uipc_send() with 
 865          *    respect to flow control that might result in our dropping the 
 866          *    data in uipc_send().  By setting this, we allow for slightly 
 867          *    more records to be appended to the receiving socket to avoid 
 868          *    losing data (which we can't afford in the SOCK_STREAM case). 
 869          *    Flow control still takes place since we adjust the sender's 
 870          *    hiwat during each send.  This doesn't affect the SOCK_DGRAM 
 871          *    case and append would still fail when the queue overflows. 
 873          * b. In the presence of control messages containing internalized 
 874          *    file descriptors, the append routines will not free them since 
 875          *    we'd need to undo the work first via unp_dispose(). 
 877         so
->so_rcv
.sb_flags 
|= SB_UNIX
; 
 878         so
->so_snd
.sb_flags 
|= SB_UNIX
; 
 883 unp_detach(struct unpcb 
*unp
) 
 887         lck_rw_lock_exclusive(unp_list_mtx
); 
 888         LIST_REMOVE(unp
, unp_link
); 
 891         lck_rw_done(unp_list_mtx
); 
 892         if (unp
->unp_vnode
) { 
 893                 struct vnode 
*tvp 
= NULL
; 
 894                 socket_unlock(unp
->unp_socket
, 0); 
 896                 /* Holding unp_connect_lock will avoid a race between 
 897                  * a thread closing the listening socket and a thread 
 900                 lck_mtx_lock(unp_connect_lock
); 
 901                 socket_lock(unp
->unp_socket
, 0); 
 902                 if (unp
->unp_vnode
) { 
 903                         tvp 
= unp
->unp_vnode
; 
 904                         unp
->unp_vnode
->v_socket 
= NULL
; 
 905                         unp
->unp_vnode 
= NULL
; 
 907                 lck_mtx_unlock(unp_connect_lock
); 
 909                         vnode_rele(tvp
);                /* drop the usecount */ 
 913         while (unp
->unp_refs
.lh_first
) { 
 914                 struct unpcb 
*unp2 
= NULL
; 
 916                 /* This datagram socket is connected to one or more 
 917                  * sockets. In order to avoid a race condition between removing 
 918                  * this reference and closing the connected socket, we need  
 919                  * to check disconnect_in_progress 
 921                 if (so_locked 
== 1) { 
 922                         socket_unlock(unp
->unp_socket
, 0); 
 925                 lck_mtx_lock(unp_disconnect_lock
); 
 926                 while (disconnect_in_progress 
!= 0) { 
 927                         (void)msleep((caddr_t
)&disconnect_in_progress
, unp_disconnect_lock
, 
 928                                 PSOCK
, "disconnect", NULL
); 
 930                 disconnect_in_progress 
= 1; 
 931                 lck_mtx_unlock(unp_disconnect_lock
); 
 933                 /* Now we are sure that any unpcb socket disconnect is not happening */ 
 934                 if (unp
->unp_refs
.lh_first 
!= NULL
) { 
 935                         unp2 
= unp
->unp_refs
.lh_first
; 
 936                         socket_lock(unp2
->unp_socket
, 1); 
 939                 lck_mtx_lock(unp_disconnect_lock
); 
 940                 disconnect_in_progress 
= 0; 
 941                 wakeup(&disconnect_in_progress
); 
 942                 lck_mtx_unlock(unp_disconnect_lock
); 
 945                         /* We already locked this socket and have a reference on it */ 
 946                         unp_drop(unp2
, ECONNRESET
); 
 947                         socket_unlock(unp2
->unp_socket
, 1); 
 951         if (so_locked 
== 0) { 
 952                 socket_lock(unp
->unp_socket
, 0); 
 955         soisdisconnected(unp
->unp_socket
); 
 956         /* makes sure we're getting dealloced */ 
 957         unp
->unp_socket
->so_flags 
|= SOF_PCBCLEARING
; 
 965  *              namei:???               [anything namei can return] 
 966  *              vnode_authorize:???     [anything vnode_authorize can return] 
 968  * Notes:       p at this point is the current process, as this function is 
 969  *              only called by sobind(). 
 974         struct sockaddr 
*nam
, 
 977         struct sockaddr_un 
*soun 
= (struct sockaddr_un 
*)nam
; 
 978         struct vnode 
*vp
, *dvp
; 
 979         struct vnode_attr va
; 
 980         vfs_context_t ctx 
= vfs_context_current(); 
 983         struct socket 
*so 
= unp
->unp_socket
; 
 984         char buf
[SOCK_MAXADDRLEN
]; 
 986         if (nam
->sa_family 
!= 0 && nam
->sa_family 
!= AF_UNIX
) { 
 987                 return (EAFNOSUPPORT
); 
 991          * Check if the socket is already bound to an address 
 993         if (unp
->unp_vnode 
!= NULL
) 
 996          * Check if the socket may have been shut down 
 998         if ((so
->so_state 
& (SS_CANTRCVMORE 
| SS_CANTSENDMORE
)) == 
 999             (SS_CANTRCVMORE 
| SS_CANTSENDMORE
)) 
1002         namelen 
= soun
->sun_len 
- offsetof(struct sockaddr_un
, sun_path
); 
1006          * Note: sun_path is not a zero terminated "C" string 
1008         ASSERT(namelen 
< SOCK_MAXADDRLEN
); 
1009         bcopy(soun
->sun_path
, buf
, namelen
); 
1012         socket_unlock(so
, 0); 
1014         NDINIT(&nd
, CREATE
, OP_MKFIFO
, FOLLOW 
| LOCKPARENT
, UIO_SYSSPACE
, 
1015             CAST_USER_ADDR_T(buf
), ctx
); 
1016         /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 
1027                  * need to do this before the vnode_put of dvp 
1028                  * since we may have to release an fs_nodelock 
1036                 return (EADDRINUSE
); 
1040         VATTR_SET(&va
, va_type
, VSOCK
); 
1041         VATTR_SET(&va
, va_mode
, (ACCESSPERMS 
& ~p
->p_fd
->fd_cmask
)); 
1044         error 
= mac_vnode_check_create(ctx
, 
1045             nd
.ni_dvp
, &nd
.ni_cnd
, &va
); 
1048 #endif /* CONFIG_MACF */ 
1049 #if CONFIG_MACF_SOCKET_SUBSET 
1050         error 
= mac_vnode_check_uipc_bind(ctx
, 
1051             nd
.ni_dvp
, &nd
.ni_cnd
, &va
); 
1054 #endif /* MAC_SOCKET_SUBSET */ 
1055         /* authorize before creating */ 
1056         error 
= vnode_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, ctx
); 
1059                 /* create the socket */ 
1060                 error 
= vn_create(dvp
, &vp
, &nd
, &va
, 0, 0, NULL
, ctx
); 
1070         vnode_ref(vp
);  /* gain a longterm reference */ 
1072         vp
->v_socket 
= unp
->unp_socket
; 
1073         unp
->unp_vnode 
= vp
; 
1074         unp
->unp_addr 
= (struct sockaddr_un 
*)dup_sockaddr(nam
, 1); 
1075         vnode_put(vp
);          /* drop the iocount */ 
1082  * Returns:     0                       Success 
1083  *              EAFNOSUPPORT            Address family not supported 
1084  *              EINVAL                  Invalid argument 
1085  *              ENOTSOCK                Not a socket 
1086  *              ECONNREFUSED            Connection refused 
1087  *              EPROTOTYPE              Protocol wrong type for socket 
1088  *              EISCONN                 Socket is connected 
1089  *      unp_connect2:EPROTOTYPE         Protocol wrong type for socket 
1090  *      unp_connect2:EINVAL             Invalid argument 
1091  *      namei:???                       [anything namei can return] 
1092  *      vnode_authorize:????            [anything vnode_authorize can return] 
1094  * Notes:       p at this point is the current process, as this function is 
1095  *              only called by sosend(), sendfile(), and soconnectlock(). 
1098 unp_connect(struct socket 
*so
, struct sockaddr 
*nam
, __unused proc_t p
) 
1100         struct sockaddr_un 
*soun 
= (struct sockaddr_un 
*)nam
; 
1102         struct socket 
*so2
, *so3
, *list_so
=NULL
; 
1103         struct unpcb 
*unp
, *unp2
, *unp3
; 
1104         vfs_context_t ctx 
= vfs_context_current(); 
1106         struct nameidata nd
; 
1107         char buf
[SOCK_MAXADDRLEN
]; 
1109         if (nam
->sa_family 
!= 0 && nam
->sa_family 
!= AF_UNIX
) { 
1110                 return (EAFNOSUPPORT
); 
1113         unp 
= sotounpcb(so
); 
1116         len 
= nam
->sa_len 
- offsetof(struct sockaddr_un
, sun_path
); 
1120          * Note: sun_path is not a zero terminated "C" string 
1122         ASSERT(len 
< SOCK_MAXADDRLEN
); 
1123         bcopy(soun
->sun_path
, buf
, len
); 
1126         socket_unlock(so
, 0); 
1128         NDINIT(&nd
, LOOKUP
, OP_LOOKUP
, FOLLOW 
| LOCKLEAF
, UIO_SYSSPACE
, 
1129             CAST_USER_ADDR_T(buf
), ctx
); 
1137         if (vp
->v_type 
!= VSOCK
) { 
1143 #if CONFIG_MACF_SOCKET_SUBSET 
1144         error 
= mac_vnode_check_uipc_connect(ctx
, vp
, so
); 
1149 #endif /* MAC_SOCKET_SUBSET */ 
1151         error 
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_WRITE_DATA
, ctx
); 
1157         lck_mtx_lock(unp_connect_lock
); 
1159         if (vp
->v_socket 
== 0) { 
1160                 lck_mtx_unlock(unp_connect_lock
); 
1161                 error 
= ECONNREFUSED
; 
1166         socket_lock(vp
->v_socket
, 1); /* Get a reference on the listening socket */ 
1168         lck_mtx_unlock(unp_connect_lock
); 
1171         if (so2
->so_pcb 
== NULL
) { 
1172                 error 
= ECONNREFUSED
; 
1174                         socket_unlock(so2
, 1); 
1177                         /* Release the reference held for the listen socket */ 
1178                         VERIFY(so2
->so_usecount 
> 0); 
1185                 socket_unlock(so2
, 0); 
1187                 socket_lock(so2
, 0); 
1188         } else if (so 
> so2
) { 
1192          * Check if socket was connected while we were trying to 
1193          * get the socket locks in order. 
1194          * XXX - probably shouldn't return an error for SOCK_DGRAM 
1196         if ((so
->so_state 
& SS_ISCONNECTED
) != 0) { 
1201         if (so
->so_type 
!= so2
->so_type
) { 
1206         if (so
->so_proto
->pr_flags 
& PR_CONNREQUIRED
) { 
1207                 /* Release the incoming socket but keep a reference */ 
1208                 socket_unlock(so
, 0); 
1210                 if ((so2
->so_options 
& SO_ACCEPTCONN
) == 0 || 
1211                     (so3 
= sonewconn(so2
, 0, nam
)) == 0) { 
1212                         error 
= ECONNREFUSED
; 
1214                                 socket_unlock(so2
, 1); 
1218                                 /* Release the reference held for 
1221                                 VERIFY(so2
->so_usecount 
> 0); 
1226                 unp2 
= sotounpcb(so2
); 
1227                 unp3 
= sotounpcb(so3
); 
1229                         unp3
->unp_addr 
= (struct sockaddr_un 
*) 
1230                             dup_sockaddr((struct sockaddr 
*)unp2
->unp_addr
, 1); 
1233                  * unp_peercred management: 
1235                  * The connecter's (client's) credentials are copied 
1236                  * from its process structure at the time of connect() 
1239                 cru2x(vfs_context_ucred(ctx
), &unp3
->unp_peercred
); 
1240                 unp3
->unp_flags 
|= UNP_HAVEPC
; 
1242                  * The receiver's (server's) credentials are copied 
1243                  * from the unp_peercred member of socket on which the 
1244                  * former called listen(); unp_listen() cached that 
1245                  * process's credentials at that time so we can use 
1248                 KASSERT(unp2
->unp_flags 
& UNP_HAVEPCCACHED
, 
1249                     ("unp_connect: listener without cached peercred")); 
1251                 /* Here we need to have both so and so2 locks and so2 
1252                  * is already locked. Lock ordering is required. 
1255                         socket_unlock(so2
, 0); 
1257                         socket_lock(so2
, 0); 
1262                 /* Check again if the socket state changed when its lock was released */ 
1263                 if ((so
->so_state 
& SS_ISCONNECTED
) != 0) { 
1265                         socket_unlock(so2
, 1); 
1266                         socket_lock(so3
, 0); 
1267                         sofreelastref(so3
, 1); 
1270                 memcpy(&unp
->unp_peercred
, &unp2
->unp_peercred
, 
1271                     sizeof (unp
->unp_peercred
)); 
1272                 unp
->unp_flags 
|= UNP_HAVEPC
; 
1274 #if CONFIG_MACF_SOCKET 
1275                 /* XXXMAC: recursive lock: SOCK_LOCK(so); */ 
1276                 mac_socketpeer_label_associate_socket(so
, so3
); 
1277                 mac_socketpeer_label_associate_socket(so3
, so
); 
1278                 /* XXXMAC: SOCK_UNLOCK(so); */ 
1279 #endif /* MAC_SOCKET */ 
1281                 /* Hold the reference on listening socket until the end */ 
1282                 socket_unlock(so2
, 0); 
1285                 /* Lock ordering doesn't matter because so3 was just created */ 
1286                 socket_lock(so3
, 1); 
1290                  * Enable tracing for mDNSResponder endpoints.  (The use 
1291                  * of sizeof instead of strlen below takes the null 
1292                  * terminating character into account.) 
1294                 if (unpst_tracemdns 
&& 
1295                     !strncmp(soun
->sun_path
, MDNSRESPONDER_PATH
, 
1296                     sizeof (MDNSRESPONDER_PATH
))) { 
1297                         unp
->unp_flags 
|= UNP_TRACE_MDNS
; 
1298                         unp2
->unp_flags 
|= UNP_TRACE_MDNS
; 
1302         error 
= unp_connect2(so
, so2
); 
1307                         socket_unlock(so2
, 1); 
1309                         /* Release the extra reference held for the listen socket. 
1310                          * This is possible only for SOCK_DGRAM sockets. We refuse 
1311                          * connecting to the same socket for SOCK_STREAM sockets. 
1313                         VERIFY(so2
->so_usecount 
> 0); 
1318         if (list_so 
!= NULL
) { 
1319                 socket_lock(list_so
, 0); 
1320                 socket_unlock(list_so
, 1); 
1324         LCK_MTX_ASSERT(&unp
->unp_mtx
, LCK_MTX_ASSERT_OWNED
); 
1330  * Returns:     0                       Success 
1331  *              EPROTOTYPE              Protocol wrong type for socket 
1332  *              EINVAL                  Invalid argument 
1335 unp_connect2(struct socket 
*so
, struct socket 
*so2
) 
1337         struct unpcb 
*unp 
= sotounpcb(so
); 
1340         if (so2
->so_type 
!= so
->so_type
) 
1341                 return (EPROTOTYPE
); 
1343         unp2 
= sotounpcb(so2
); 
1345         LCK_MTX_ASSERT(&unp
->unp_mtx
, LCK_MTX_ASSERT_OWNED
); 
1346         LCK_MTX_ASSERT(&unp2
->unp_mtx
, LCK_MTX_ASSERT_OWNED
); 
1348         /* Verify both sockets are still opened */ 
1349         if (unp 
== 0 || unp2 
== 0) 
1352         unp
->unp_conn 
= unp2
; 
1355         switch (so
->so_type
) { 
1358                 LIST_INSERT_HEAD(&unp2
->unp_refs
, unp
, unp_reflink
); 
1361                         /* Avoid lock order reversals due to drop/acquire in soisconnected. */ 
1362                         /* Keep an extra reference on so2 that will be dropped 
1363                          * soon after getting the locks in order  
1365                         socket_unlock(so2
, 0); 
1367                         unp_get_locks_in_order(so
, so2
); 
1368                         VERIFY(so2
->so_usecount 
> 0); 
1377                 /* This takes care of socketpair */ 
1378                 if (!(unp
->unp_flags 
& UNP_HAVEPC
) && 
1379                     !(unp2
->unp_flags 
& UNP_HAVEPC
)) { 
1380                         cru2x(kauth_cred_get(), &unp
->unp_peercred
); 
1381                         unp
->unp_flags 
|= UNP_HAVEPC
; 
1383                         cru2x(kauth_cred_get(), &unp2
->unp_peercred
); 
1384                         unp2
->unp_flags 
|= UNP_HAVEPC
; 
1386                 unp2
->unp_conn 
= unp
; 
1389                 /* Avoid lock order reversals due to drop/acquire in soisconnected. */ 
1390                 socket_unlock(so
, 0); 
1393                 /* Keep an extra reference on so2, that will be dropped soon after 
1394                  * getting the locks in order again. 
1396                 socket_unlock(so2
, 0); 
1401                 unp_get_locks_in_order(so
, so2
); 
1402                 /* Decrement the extra reference left before */ 
1403                 VERIFY(so2
->so_usecount 
> 0); 
1408                 panic("unknown socket type %d in unp_connect2", so
->so_type
); 
1410         LCK_MTX_ASSERT(&unp
->unp_mtx
, LCK_MTX_ASSERT_OWNED
); 
1411         LCK_MTX_ASSERT(&unp2
->unp_mtx
, LCK_MTX_ASSERT_OWNED
); 
1416 unp_disconnect(struct unpcb 
*unp
) 
1418         struct unpcb 
*unp2 
= NULL
; 
1419         struct socket 
*so2 
= NULL
, *so
; 
1420         struct socket 
*waitso
; 
1421         int so_locked 
= 1, strdisconn 
= 0; 
1423         so 
= unp
->unp_socket
; 
1424         if (unp
->unp_conn 
== NULL
) { 
1427         lck_mtx_lock(unp_disconnect_lock
); 
1428         while (disconnect_in_progress 
!= 0) { 
1429                 if (so_locked 
== 1) { 
1430                         socket_unlock(so
, 0); 
1433                 (void)msleep((caddr_t
)&disconnect_in_progress
, unp_disconnect_lock
, 
1434                         PSOCK
, "disconnect", NULL
); 
1436         disconnect_in_progress 
= 1; 
1437         lck_mtx_unlock(unp_disconnect_lock
); 
1439         if (so_locked 
== 0) { 
1444         unp2 
= unp
->unp_conn
; 
1446         if (unp2 
== 0 || unp2
->unp_socket 
== NULL
) { 
1449         so2 
= unp2
->unp_socket
; 
1453                 if (so_locked 
== 0) { 
1457         } else if (so 
< so2
) { 
1458                 if (so_locked 
== 0) { 
1461                 socket_lock(so2
, 1); 
1464                 if (so_locked 
== 1) {  
1465                         socket_unlock(so
, 0); 
1467                 socket_lock(so2
, 1); 
1473         LCK_MTX_ASSERT(&unp
->unp_mtx
, LCK_MTX_ASSERT_OWNED
); 
1474         LCK_MTX_ASSERT(&unp2
->unp_mtx
, LCK_MTX_ASSERT_OWNED
); 
1476         /* Check for the UNP_DONTDISCONNECT flag, if it 
1477          * is set, release both sockets and go to sleep 
1480         if ((((struct unpcb 
*)waitso
->so_pcb
)->unp_flags 
& UNP_DONTDISCONNECT
) != 0) { 
1482                         socket_unlock(so2
, 1); 
1486                 (void)msleep(waitso
->so_pcb
, &unp
->unp_mtx
,  
1487                         PSOCK 
| PDROP
, "unpdisconnect", NULL
); 
1491         if (unp
->unp_conn 
== NULL
) { 
1492                 panic("unp_conn became NULL after sleep"); 
1495         unp
->unp_conn 
= NULL
; 
1496         VERIFY(so2
->so_usecount 
> 0); 
1499         if (unp
->unp_flags 
& UNP_TRACE_MDNS
) 
1500                 unp
->unp_flags 
&= ~UNP_TRACE_MDNS
; 
1502         switch (unp
->unp_socket
->so_type
) { 
1505                 LIST_REMOVE(unp
, unp_reflink
); 
1506                 unp
->unp_socket
->so_state 
&= ~SS_ISCONNECTED
; 
1508                         socket_unlock(so2
, 1); 
1512                 unp2
->unp_conn 
= NULL
; 
1513                 VERIFY(so
->so_usecount 
> 0); 
1516                 /* Set the socket state correctly but do a wakeup later when 
1517                  * we release all locks except the socket lock, this will avoid 
1520                 unp
->unp_socket
->so_state 
&= ~(SS_ISCONNECTING
|SS_ISCONNECTED
|SS_ISDISCONNECTING
); 
1521                 unp
->unp_socket
->so_state 
|= (SS_CANTRCVMORE
|SS_CANTSENDMORE
|SS_ISDISCONNECTED
); 
1523                 unp2
->unp_socket
->so_state 
&= ~(SS_ISCONNECTING
|SS_ISCONNECTED
|SS_ISDISCONNECTING
); 
1524                 unp
->unp_socket
->so_state 
|= (SS_CANTRCVMORE
|SS_CANTSENDMORE
|SS_ISDISCONNECTED
); 
1526                 if (unp2
->unp_flags 
& UNP_TRACE_MDNS
) 
1527                         unp2
->unp_flags 
&= ~UNP_TRACE_MDNS
; 
1532                 panic("unknown socket type %d", so
->so_type
); 
1535         lck_mtx_lock(unp_disconnect_lock
); 
1536         disconnect_in_progress 
= 0; 
1537         wakeup(&disconnect_in_progress
); 
1538         lck_mtx_unlock(unp_disconnect_lock
); 
1541                 socket_unlock(so
, 0); 
1542                 soisdisconnected(so2
); 
1543                 socket_unlock(so2
, 1); 
1546                 soisdisconnected(so
); 
1548         LCK_MTX_ASSERT(&unp
->unp_mtx
, LCK_MTX_ASSERT_OWNED
); 
1553  * unpcb_to_compat copies specific bits of a unpcb to a unpcb_compat format. 
1554  * The unpcb_compat data structure is passed to user space and must not change. 
1557 unpcb_to_compat(struct unpcb 
*up
, struct unpcb_compat 
*cp
) 
1559 #if defined(__LP64__) 
1560         cp
->unp_link
.le_next 
= (u_int32_t
) 
1561             VM_KERNEL_ADDRPERM(up
->unp_link
.le_next
); 
1562         cp
->unp_link
.le_prev 
= (u_int32_t
) 
1563             VM_KERNEL_ADDRPERM(up
->unp_link
.le_prev
); 
1565         cp
->unp_link
.le_next 
= (struct unpcb_compat 
*) 
1566             VM_KERNEL_ADDRPERM(up
->unp_link
.le_next
); 
1567         cp
->unp_link
.le_prev 
= (struct unpcb_compat 
**) 
1568             VM_KERNEL_ADDRPERM(up
->unp_link
.le_prev
); 
1570         cp
->unp_socket 
= (_UNPCB_PTR(struct socket 
*)) 
1571             VM_KERNEL_ADDRPERM(up
->unp_socket
); 
1572         cp
->unp_vnode 
= (_UNPCB_PTR(struct vnode 
*)) 
1573             VM_KERNEL_ADDRPERM(up
->unp_vnode
); 
1574         cp
->unp_ino 
= up
->unp_ino
; 
1575         cp
->unp_conn 
= (_UNPCB_PTR(struct unpcb_compat 
*)) 
1576             VM_KERNEL_ADDRPERM(up
->unp_conn
); 
1577         cp
->unp_refs 
= (u_int32_t
)VM_KERNEL_ADDRPERM(up
->unp_refs
.lh_first
); 
1578 #if defined(__LP64__) 
1579         cp
->unp_reflink
.le_next 
= 
1580             (u_int32_t
)VM_KERNEL_ADDRPERM(up
->unp_reflink
.le_next
); 
1581         cp
->unp_reflink
.le_prev 
= 
1582             (u_int32_t
)VM_KERNEL_ADDRPERM(up
->unp_reflink
.le_prev
); 
1584         cp
->unp_reflink
.le_next 
= 
1585             (struct unpcb_compat 
*)VM_KERNEL_ADDRPERM(up
->unp_reflink
.le_next
); 
1586         cp
->unp_reflink
.le_prev 
= 
1587             (struct unpcb_compat 
**)VM_KERNEL_ADDRPERM(up
->unp_reflink
.le_prev
); 
1589         cp
->unp_addr 
= (_UNPCB_PTR(struct sockaddr_un 
*)) 
1590             VM_KERNEL_ADDRPERM(up
->unp_addr
); 
1591         cp
->unp_cc 
= up
->unp_cc
; 
1592         cp
->unp_mbcnt 
= up
->unp_mbcnt
; 
1593         cp
->unp_gencnt 
= up
->unp_gencnt
; 
1597 unp_pcblist SYSCTL_HANDLER_ARGS
 
1599 #pragma unused(oidp,arg2) 
1601         struct unpcb 
*unp
, **unp_list
; 
1604         struct unp_head 
*head
; 
1606         lck_rw_lock_shared(unp_list_mtx
); 
1607         head 
= ((intptr_t)arg1 
== SOCK_DGRAM 
? &unp_dhead 
: &unp_shead
); 
1610          * The process of preparing the PCB list is too time-consuming and 
1611          * resource-intensive to repeat twice on every request. 
1613         if (req
->oldptr 
== USER_ADDR_NULL
) { 
1615                 req
->oldidx 
= 2 * sizeof (xug
) + (n 
+ n 
/ 8) * 
1616                     sizeof (struct xunpcb
); 
1617                 lck_rw_done(unp_list_mtx
); 
1621         if (req
->newptr 
!= USER_ADDR_NULL
) { 
1622                 lck_rw_done(unp_list_mtx
); 
1627          * OK, now we're committed to doing something. 
1629         gencnt 
= unp_gencnt
; 
1632         bzero(&xug
, sizeof (xug
)); 
1633         xug
.xug_len 
= sizeof (xug
); 
1635         xug
.xug_gen 
= gencnt
; 
1636         xug
.xug_sogen 
= so_gencnt
; 
1637         error 
= SYSCTL_OUT(req
, &xug
, sizeof (xug
)); 
1639                 lck_rw_done(unp_list_mtx
); 
1644          * We are done if there is no pcb 
1647                 lck_rw_done(unp_list_mtx
); 
1651         MALLOC(unp_list
, struct unpcb 
**, n 
* sizeof (*unp_list
), 
1653         if (unp_list 
== 0) { 
1654                 lck_rw_done(unp_list_mtx
); 
1658         for (unp 
= head
->lh_first
, i 
= 0; unp 
&& i 
< n
; 
1659             unp 
= unp
->unp_link
.le_next
) { 
1660                 if (unp
->unp_gencnt 
<= gencnt
) 
1661                         unp_list
[i
++] = unp
; 
1663         n 
= i
;                  /* in case we lost some during malloc */ 
1666         for (i 
= 0; i 
< n
; i
++) { 
1668                 if (unp
->unp_gencnt 
<= gencnt
) { 
1671                         bzero(&xu
, sizeof (xu
)); 
1672                         xu
.xu_len 
= sizeof (xu
); 
1673                         xu
.xu_unpp 
= (_UNPCB_PTR(struct unpcb_compat 
*)) 
1674                             VM_KERNEL_ADDRPERM(unp
); 
1676                          * XXX - need more locking here to protect against 
1677                          * connect/disconnect races for SMP. 
1680                                 bcopy(unp
->unp_addr
, &xu
.xu_addr
, 
1681                                     unp
->unp_addr
->sun_len
); 
1682                         if (unp
->unp_conn 
&& unp
->unp_conn
->unp_addr
) 
1683                                 bcopy(unp
->unp_conn
->unp_addr
, 
1685                                     unp
->unp_conn
->unp_addr
->sun_len
); 
1686                         unpcb_to_compat(unp
, &xu
.xu_unp
); 
1687                         sotoxsocket(unp
->unp_socket
, &xu
.xu_socket
); 
1688                         error 
= SYSCTL_OUT(req
, &xu
, sizeof (xu
)); 
1693                  * Give the user an updated idea of our state. 
1694                  * If the generation differs from what we told 
1695                  * her before, she knows that something happened 
1696                  * while we were processing this request, and it 
1697                  * might be necessary to retry. 
1699                 bzero(&xug
, sizeof (xug
)); 
1700                 xug
.xug_len 
= sizeof (xug
); 
1701                 xug
.xug_gen 
= unp_gencnt
; 
1702                 xug
.xug_sogen 
= so_gencnt
; 
1703                 xug
.xug_count 
= unp_count
; 
1704                 error 
= SYSCTL_OUT(req
, &xug
, sizeof (xug
)); 
1706         FREE(unp_list
, M_TEMP
); 
1707         lck_rw_done(unp_list_mtx
); 
1711 SYSCTL_PROC(_net_local_dgram
, OID_AUTO
, pcblist
, 
1712             CTLTYPE_STRUCT 
| CTLFLAG_RD 
| CTLFLAG_LOCKED
, 
1713             (caddr_t
)(long)SOCK_DGRAM
, 0, unp_pcblist
, "S,xunpcb", 
1714             "List of active local datagram sockets"); 
1715 SYSCTL_PROC(_net_local_stream
, OID_AUTO
, pcblist
, 
1716             CTLTYPE_STRUCT 
| CTLFLAG_RD 
| CTLFLAG_LOCKED
, 
1717             (caddr_t
)(long)SOCK_STREAM
, 0, unp_pcblist
, "S,xunpcb", 
1718             "List of active local stream sockets"); 
1720 #if !CONFIG_EMBEDDED 
1723 unp_pcblist64 SYSCTL_HANDLER_ARGS
 
1725 #pragma unused(oidp,arg2) 
1727         struct unpcb 
*unp
, **unp_list
; 
1730         struct unp_head 
*head
; 
1732         lck_rw_lock_shared(unp_list_mtx
); 
1733         head 
= ((intptr_t)arg1 
== SOCK_DGRAM 
? &unp_dhead 
: &unp_shead
); 
1736          * The process of preparing the PCB list is too time-consuming and 
1737          * resource-intensive to repeat twice on every request. 
1739         if (req
->oldptr 
== USER_ADDR_NULL
) { 
1741                 req
->oldidx 
= 2 * sizeof (xug
) + (n 
+ n 
/ 8) * 
1742                     (sizeof (struct xunpcb64
));  
1743                 lck_rw_done(unp_list_mtx
); 
1747         if (req
->newptr 
!= USER_ADDR_NULL
) { 
1748                 lck_rw_done(unp_list_mtx
); 
1753          * OK, now we're committed to doing something. 
1755         gencnt 
= unp_gencnt
; 
1758         bzero(&xug
, sizeof (xug
)); 
1759         xug
.xug_len 
= sizeof (xug
); 
1761         xug
.xug_gen 
= gencnt
; 
1762         xug
.xug_sogen 
= so_gencnt
; 
1763         error 
= SYSCTL_OUT(req
, &xug
, sizeof (xug
)); 
1765                 lck_rw_done(unp_list_mtx
); 
1770          * We are done if there is no pcb 
1773                 lck_rw_done(unp_list_mtx
); 
1777         MALLOC(unp_list
, struct unpcb 
**, n 
* sizeof (*unp_list
), 
1779         if (unp_list 
== 0) { 
1780                 lck_rw_done(unp_list_mtx
); 
1784         for (unp 
= head
->lh_first
, i 
= 0; unp 
&& i 
< n
; 
1785             unp 
= unp
->unp_link
.le_next
) { 
1786                 if (unp
->unp_gencnt 
<= gencnt
) 
1787                         unp_list
[i
++] = unp
; 
1789         n 
= i
;                  /* in case we lost some during malloc */ 
1792         for (i 
= 0; i 
< n
; i
++) { 
1794                 if (unp
->unp_gencnt 
<= gencnt
) { 
1796                         size_t          xu_len 
= sizeof(struct xunpcb64
); 
1800                         xu
.xu_unpp 
= (u_int64_t
)VM_KERNEL_ADDRPERM(unp
); 
1801                         xu
.xunp_link
.le_next 
= (u_int64_t
) 
1802                             VM_KERNEL_ADDRPERM(unp
->unp_link
.le_next
); 
1803                         xu
.xunp_link
.le_prev 
= (u_int64_t
) 
1804                             VM_KERNEL_ADDRPERM(unp
->unp_link
.le_prev
); 
1805                         xu
.xunp_socket 
= (u_int64_t
) 
1806                             VM_KERNEL_ADDRPERM(unp
->unp_socket
); 
1807                         xu
.xunp_vnode 
= (u_int64_t
) 
1808                             VM_KERNEL_ADDRPERM(unp
->unp_vnode
); 
1809                         xu
.xunp_ino 
= unp
->unp_ino
; 
1810                         xu
.xunp_conn 
= (u_int64_t
) 
1811                             VM_KERNEL_ADDRPERM(unp
->unp_conn
); 
1812                         xu
.xunp_refs 
= (u_int64_t
) 
1813                             VM_KERNEL_ADDRPERM(unp
->unp_refs
.lh_first
); 
1814                         xu
.xunp_reflink
.le_next 
= (u_int64_t
) 
1815                             VM_KERNEL_ADDRPERM(unp
->unp_reflink
.le_next
); 
1816                         xu
.xunp_reflink
.le_prev 
= (u_int64_t
) 
1817                             VM_KERNEL_ADDRPERM(unp
->unp_reflink
.le_prev
); 
1818                         xu
.xunp_cc 
= unp
->unp_cc
; 
1819                         xu
.xunp_mbcnt 
= unp
->unp_mbcnt
; 
1820                         xu
.xunp_gencnt 
= unp
->unp_gencnt
; 
1822                         if (unp
->unp_socket
) 
1823                                 sotoxsocket64(unp
->unp_socket
, &xu
.xu_socket
); 
1826                          * XXX - need more locking here to protect against 
1827                          * connect/disconnect races for SMP. 
1830                                 bcopy(unp
->unp_addr
, &xu
.xunp_addr
, 
1831                                     unp
->unp_addr
->sun_len
); 
1832                         if (unp
->unp_conn 
&& unp
->unp_conn
->unp_addr
) 
1833                                 bcopy(unp
->unp_conn
->unp_addr
, 
1835                                     unp
->unp_conn
->unp_addr
->sun_len
); 
1837                         error 
= SYSCTL_OUT(req
, &xu
, xu_len
); 
1842                  * Give the user an updated idea of our state. 
1843                  * If the generation differs from what we told 
1844                  * her before, she knows that something happened 
1845                  * while we were processing this request, and it 
1846                  * might be necessary to retry. 
1848                 bzero(&xug
, sizeof (xug
)); 
1849                 xug
.xug_len 
= sizeof (xug
); 
1850                 xug
.xug_gen 
= unp_gencnt
; 
1851                 xug
.xug_sogen 
= so_gencnt
; 
1852                 xug
.xug_count 
= unp_count
; 
1853                 error 
= SYSCTL_OUT(req
, &xug
, sizeof (xug
)); 
1855         FREE(unp_list
, M_TEMP
); 
1856         lck_rw_done(unp_list_mtx
); 
1860 SYSCTL_PROC(_net_local_dgram
, OID_AUTO
, pcblist64
, 
1861             CTLTYPE_STRUCT 
| CTLFLAG_RD 
| CTLFLAG_LOCKED
, 
1862             (caddr_t
)(long)SOCK_DGRAM
, 0, unp_pcblist64
, "S,xunpcb64", 
1863             "List of active local datagram sockets 64 bit"); 
1864 SYSCTL_PROC(_net_local_stream
, OID_AUTO
, pcblist64
, 
1865             CTLTYPE_STRUCT 
| CTLFLAG_RD 
| CTLFLAG_LOCKED
, 
1866             (caddr_t
)(long)SOCK_STREAM
, 0, unp_pcblist64
, "S,xunpcb64", 
1867             "List of active local stream sockets 64 bit"); 
1869 #endif /* !CONFIG_EMBEDDED */ 
1872 unp_shutdown(struct unpcb 
*unp
) 
1874         struct socket 
*so 
= unp
->unp_socket
; 
1876         if (unp
->unp_socket
->so_type 
== SOCK_STREAM 
&& unp
->unp_conn
) { 
1877                 so2 
= unp
->unp_conn
->unp_socket
; 
1878                 unp_get_locks_in_order(so
, so2
); 
1880                 socket_unlock(so2
, 1); 
1885 unp_drop(struct unpcb 
*unp
, int errno
) 
1887         struct socket 
*so 
= unp
->unp_socket
; 
1889         so
->so_error 
= errno
; 
1890         unp_disconnect(unp
); 
1894  * Returns:     0                       Success 
1895  *              EMSGSIZE                The new fd's will not fit 
1896  *              ENOBUFS                 Cannot alloc struct fileproc 
1899 unp_externalize(struct mbuf 
*rights
) 
1901         proc_t p 
= current_proc();              /* XXX */ 
1903         struct cmsghdr 
*cm 
= mtod(rights
, struct cmsghdr 
*); 
1904         struct fileglob 
**rp 
= (struct fileglob 
**)(cm 
+ 1); 
1905         int *fds 
= (int *)(cm 
+ 1); 
1906         struct fileproc 
*fp
; 
1907         struct fileproc 
**fileproc_l
; 
1908         int newfds 
= (cm
->cmsg_len 
- sizeof (*cm
)) / sizeof (int); 
1911         MALLOC(fileproc_l
, struct fileproc 
**, 
1912             newfds 
* sizeof (struct fileproc 
*), M_TEMP
, M_WAITOK
); 
1913         if (fileproc_l 
== NULL
) { 
1921          * if the new FD's will not fit, then we free them all 
1923         if (!fdavail(p
, newfds
)) { 
1929          * now change each pointer to an fd in the global table to 
1930          * an integer that is the index to the local fd table entry 
1931          * that we set up to point to the global one we are transferring. 
1932          * XXX (1) this assumes a pointer and int are the same size,  
1933          * XXX     or the mbuf can hold the expansion 
1934          * XXX (2) allocation failures should be non-fatal 
1936         for (i 
= 0; i 
< newfds
; i
++) { 
1937 #if CONFIG_MACF_SOCKET 
1939                  * If receive access is denied, don't pass along 
1940                  * and error message, just discard the descriptor. 
1942                 if (mac_file_check_receive(kauth_cred_get(), rp
[i
])) { 
1944                         unp_discard(rp
[i
], p
); 
1950                 if (fdalloc(p
, 0, &f
)) 
1951                         panic("unp_externalize:fdalloc"); 
1952                 fp 
= fileproc_alloc_init(NULL
); 
1954                         panic("unp_externalize: MALLOC_ZONE"); 
1956                 fp
->f_fglob 
= rp
[i
]; 
1957                 if (fg_removeuipc_mark(rp
[i
])) { 
1960                          * Take an iocount on the fp for completing the 
1961                          * removal from the global msg queue 
1966                         fileproc_l
[i
] = NULL
; 
1968                 procfdtbl_releasefd(p
, f
, fp
); 
1973         for (i 
= 0; i 
< newfds
; i
++) { 
1974                 if (fileproc_l
[i
] != NULL
) { 
1975                         VERIFY(fileproc_l
[i
]->f_fglob 
!= NULL 
&& 
1976                             (fileproc_l
[i
]->f_fglob
->fg_lflags 
& FG_RMMSGQ
)); 
1978                         fg_removeuipc(fileproc_l
[i
]->f_fglob
); 
1980                         /* Drop the iocount */ 
1981                         fp_drop(p
, fds
[i
], fileproc_l
[i
], 0); 
1982                         fileproc_l
[i
] = NULL
; 
1985                         (void) OSAddAtomic(-1, &unp_rights
); 
1989         if (fileproc_l 
!= NULL
) 
1990                 FREE(fileproc_l
, M_TEMP
); 
1992                 for (i 
= 0; i 
< newfds
; i
++) { 
1993                         unp_discard(*rp
, p
); 
2003         _CASSERT(UIPC_MAX_CMSG_FD 
>= (MCLBYTES 
/ sizeof(int))); 
2004         unp_zone 
= zinit(sizeof (struct unpcb
), 
2005             (nmbclusters 
* sizeof (struct unpcb
)), 4096, "unpzone"); 
2009         LIST_INIT(&unp_dhead
); 
2010         LIST_INIT(&unp_shead
); 
2013          * allocate lock group attribute and group for udp pcb mutexes 
2015         unp_mtx_grp_attr 
= lck_grp_attr_alloc_init(); 
2017         unp_mtx_grp 
= lck_grp_alloc_init("unp_list", unp_mtx_grp_attr
); 
2019         unp_mtx_attr 
= lck_attr_alloc_init(); 
2021         if ((unp_list_mtx 
= lck_rw_alloc_init(unp_mtx_grp
, 
2022             unp_mtx_attr
)) == NULL
) 
2023                 return; /* pretty much dead if this fails... */ 
2025         if ((unp_disconnect_lock 
= lck_mtx_alloc_init(unp_mtx_grp
, 
2026                 unp_mtx_attr
)) == NULL
) 
2029         if ((unp_connect_lock 
= lck_mtx_alloc_init(unp_mtx_grp
, 
2030                 unp_mtx_attr
)) == NULL
) 
2035 #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 
2039  * Returns:     0                       Success 
2041  *      fdgetf_noref:EBADF 
2044 unp_internalize(struct mbuf 
*control
, proc_t p
) 
2046         struct cmsghdr 
*cm 
= mtod(control
, struct cmsghdr 
*); 
2048         struct fileglob 
**rp
; 
2049         struct fileproc 
*fp
; 
2052         uint8_t fg_ins
[UIPC_MAX_CMSG_FD 
/ 8]; 
2054         /* 64bit: cmsg_len is 'uint32_t', m_len is 'long' */ 
2055         if (cm
->cmsg_type 
!= SCM_RIGHTS 
|| cm
->cmsg_level 
!= SOL_SOCKET 
|| 
2056             (socklen_t
)cm
->cmsg_len 
!= (socklen_t
)control
->m_len
) { 
2059         oldfds 
= (cm
->cmsg_len 
- sizeof (*cm
)) / sizeof (int); 
2060         bzero(fg_ins
, sizeof(fg_ins
)); 
2063         fds 
= (int *)(cm 
+ 1); 
2065         for (i 
= 0; i 
< oldfds
; i
++) { 
2066                 struct fileproc 
*tmpfp
; 
2067                 if (((error 
= fdgetf_noref(p
, fds
[i
], &tmpfp
)) != 0)) { 
2070                 } else if (!file_issendable(p
, tmpfp
)) { 
2073                 } else if (FP_ISGUARDED(tmpfp
, GUARD_SOCKET_IPC
)) { 
2074                         error 
= fp_guard_exception(p
, 
2075                                 fds
[i
], tmpfp
, kGUARD_EXC_SOCKET_IPC
); 
2080         rp 
= (struct fileglob 
**)(cm 
+ 1); 
2082         /* On K64 we need to walk backwards because a fileglob * is twice the size of an fd  
2083          * and doing them in-order would result in stomping over unprocessed fd's 
2085         for (i 
= (oldfds 
- 1); i 
>= 0; i
--) { 
2086                 (void) fdgetf_noref(p
, fds
[i
], &fp
); 
2087                 if (fg_insertuipc_mark(fp
->f_fglob
)) 
2088                         fg_ins
[i 
/ 8] |= 0x80 >> (i 
% 8); 
2089                 rp
[i
] = fp
->f_fglob
; 
2093         for (i 
= 0; i 
< oldfds
; i
++) { 
2094                 if (fg_ins
[i 
/ 8] & (0x80 >> (i 
% 8))) { 
2095                         VERIFY(rp
[i
]->fg_lflags 
& FG_INSMSGQ
); 
2096                         fg_insertuipc(rp
[i
]); 
2098                 (void) OSAddAtomic(1, &unp_rights
); 
2104 static int      unp_defer
, unp_gcing
, unp_gcwait
; 
2105 static thread_t unp_gcthread 
= NULL
; 
2107 /* always called under uipc_lock */ 
2111         if (unp_gcthread 
== current_thread()) 
2114         while (unp_gcing 
!= 0) { 
2116                 msleep(&unp_gcing
, uipc_lock
, 0 , "unp_gc_wait", NULL
); 
2121 __private_extern__ 
void 
2124         struct fileglob 
*fg
, *nextfg
; 
2126         static struct fileglob 
**extra_ref
; 
2127         struct fileglob 
**fpp
; 
2129         int need_gcwakeup 
= 0; 
2131         lck_mtx_lock(uipc_lock
); 
2133                 lck_mtx_unlock(uipc_lock
); 
2138         unp_gcthread 
= current_thread(); 
2139         lck_mtx_unlock(uipc_lock
); 
2141          * before going through all this, set all FDs to 
2142          * be NOT defered and NOT externally accessible 
2144         for (fg 
= fmsghead
.lh_first
; fg 
!= 0; fg 
= fg
->f_msglist
.le_next
) { 
2145                 lck_mtx_lock(&fg
->fg_lock
); 
2146                 fg
->fg_flag 
&= ~(FMARK
|FDEFER
); 
2147                 lck_mtx_unlock(&fg
->fg_lock
); 
2150                 for (fg 
= fmsghead
.lh_first
; fg 
!= 0; 
2151                     fg 
= fg
->f_msglist
.le_next
) { 
2152                         lck_mtx_lock(&fg
->fg_lock
); 
2154                          * If the file is not open, skip it 
2156                         if (fg
->fg_count 
== 0) { 
2157                                 lck_mtx_unlock(&fg
->fg_lock
); 
2161                          * If we already marked it as 'defer'  in a 
2162                          * previous pass, then try process it this time 
2165                         if (fg
->fg_flag 
& FDEFER
) { 
2166                                 fg
->fg_flag 
&= ~FDEFER
; 
2170                                  * if it's not defered, then check if it's 
2171                                  * already marked.. if so skip it 
2173                                 if (fg
->fg_flag 
& FMARK
) { 
2174                                         lck_mtx_unlock(&fg
->fg_lock
); 
2178                                  * If all references are from messages 
2179                                  * in transit, then skip it. it's not 
2180                                  * externally accessible. 
2182                                 if (fg
->fg_count 
== fg
->fg_msgcount
) { 
2183                                         lck_mtx_unlock(&fg
->fg_lock
); 
2187                                  * If it got this far then it must be 
2188                                  * externally accessible. 
2190                                 fg
->fg_flag 
|= FMARK
; 
2193                          * either it was defered, or it is externally 
2194                          * accessible and not already marked so. 
2195                          * Now check if it is possibly one of OUR sockets. 
2197                         if (FILEGLOB_DTYPE(fg
) != DTYPE_SOCKET 
|| 
2198                             (so 
= (struct socket 
*)fg
->fg_data
) == 0) { 
2199                                 lck_mtx_unlock(&fg
->fg_lock
); 
2202                         if (so
->so_proto
->pr_domain 
!= localdomain 
|| 
2203                             (so
->so_proto
->pr_flags
&PR_RIGHTS
) == 0) { 
2204                                 lck_mtx_unlock(&fg
->fg_lock
); 
2208                         if (so
->so_rcv
.sb_flags 
& SB_LOCK
) { 
2210                                  * This is problematical; it's not clear 
2211                                  * we need to wait for the sockbuf to be 
2212                                  * unlocked (on a uniprocessor, at least), 
2213                                  * and it's also not clear what to do 
2214                                  * if sbwait returns an error due to receipt 
2215                                  * of a signal.  If sbwait does return 
2216                                  * an error, we'll go into an infinite 
2217                                  * loop.  Delete all of this for now. 
2219                                 (void) sbwait(&so
->so_rcv
); 
2224                          * So, Ok, it's one of our sockets and it IS externally 
2225                          * accessible (or was defered). Now we look 
2226                          * to see if we hold any file descriptors in its 
2227                          * message buffers. Follow those links and mark them 
2228                          * as accessible too. 
2230                          * In case a file is passed onto itself we need to  
2231                          * release the file lock. 
2233                         lck_mtx_unlock(&fg
->fg_lock
); 
2235                         unp_scan(so
->so_rcv
.sb_mb
, unp_mark
, 0); 
2237         } while (unp_defer
); 
2239          * We grab an extra reference to each of the file table entries 
2240          * that are not otherwise accessible and then free the rights 
2241          * that are stored in messages on them. 
2243          * The bug in the orginal code is a little tricky, so I'll describe 
2244          * what's wrong with it here. 
2246          * It is incorrect to simply unp_discard each entry for f_msgcount 
2247          * times -- consider the case of sockets A and B that contain 
2248          * references to each other.  On a last close of some other socket, 
2249          * we trigger a gc since the number of outstanding rights (unp_rights) 
2250          * is non-zero.  If during the sweep phase the gc code un_discards, 
2251          * we end up doing a (full) closef on the descriptor.  A closef on A 
2252          * results in the following chain.  Closef calls soo_close, which 
2253          * calls soclose.   Soclose calls first (through the switch 
2254          * uipc_usrreq) unp_detach, which re-invokes unp_gc.  Unp_gc simply 
2255          * returns because the previous instance had set unp_gcing, and 
2256          * we return all the way back to soclose, which marks the socket 
2257          * with SS_NOFDREF, and then calls sofree.  Sofree calls sorflush 
2258          * to free up the rights that are queued in messages on the socket A, 
2259          * i.e., the reference on B.  The sorflush calls via the dom_dispose 
2260          * switch unp_dispose, which unp_scans with unp_discard.  This second 
2261          * instance of unp_discard just calls closef on B. 
2263          * Well, a similar chain occurs on B, resulting in a sorflush on B, 
2264          * which results in another closef on A.  Unfortunately, A is already 
2265          * being closed, and the descriptor has already been marked with 
2266          * SS_NOFDREF, and soclose panics at this point. 
2268          * Here, we first take an extra reference to each inaccessible 
2269          * descriptor.  Then, we call sorflush ourself, since we know 
2270          * it is a Unix domain socket anyhow.  After we destroy all the 
2271          * rights carried in messages, we do a last closef to get rid 
2272          * of our extra reference.  This is the last close, and the 
2273          * unp_detach etc will shut down the socket. 
2275          * 91/09/19, bsy@cs.cmu.edu 
2277         extra_ref 
= _MALLOC(nfiles 
* sizeof (struct fileglob 
*), 
2278             M_FILEGLOB
, M_WAITOK
); 
2279         if (extra_ref 
== NULL
) 
2281         for (nunref 
= 0, fg 
= fmsghead
.lh_first
, fpp 
= extra_ref
; fg 
!= 0; 
2283                 lck_mtx_lock(&fg
->fg_lock
); 
2285                 nextfg 
= fg
->f_msglist
.le_next
; 
2287                  * If it's not open, skip it 
2289                 if (fg
->fg_count 
== 0) { 
2290                         lck_mtx_unlock(&fg
->fg_lock
); 
2294                  * If all refs are from msgs, and it's not marked accessible 
2295                  * then it must be referenced from some unreachable cycle 
2296                  * of (shut-down) FDs, so include it in our 
2297                  * list of FDs to remove 
2299                 if (fg
->fg_count 
== fg
->fg_msgcount 
&& !(fg
->fg_flag 
& FMARK
)) { 
2304                 lck_mtx_unlock(&fg
->fg_lock
); 
2307          * for each FD on our hit list, do the following two things 
2309         for (i 
= nunref
, fpp 
= extra_ref
; --i 
>= 0; ++fpp
) { 
2310                 struct fileglob 
*tfg
; 
2314                 if (FILEGLOB_DTYPE(tfg
) == DTYPE_SOCKET 
&& 
2315                     tfg
->fg_data 
!= NULL
) { 
2316                         so 
= (struct socket 
*)(tfg
->fg_data
); 
2322                         socket_unlock(so
, 0); 
2325         for (i 
= nunref
, fpp 
= extra_ref
; --i 
>= 0; ++fpp
) 
2326                 closef_locked((struct fileproc 
*)0, *fpp
, (proc_t
)NULL
); 
2328         FREE((caddr_t
)extra_ref
, M_FILEGLOB
); 
2330         lck_mtx_lock(uipc_lock
); 
2332         unp_gcthread 
= NULL
; 
2334         if (unp_gcwait 
!= 0) { 
2338         lck_mtx_unlock(uipc_lock
); 
2340         if (need_gcwakeup 
!= 0) 
2345 unp_dispose(struct mbuf 
*m
) 
2348                 unp_scan(m
, unp_discard
, NULL
); 
2353  * Returns:     0                       Success 
2356 unp_listen(struct unpcb 
*unp
, proc_t p
) 
2358         kauth_cred_t safecred 
= kauth_cred_proc_ref(p
); 
2359         cru2x(safecred
, &unp
->unp_peercred
); 
2360         kauth_cred_unref(&safecred
); 
2361         unp
->unp_flags 
|= UNP_HAVEPCCACHED
; 
2366 unp_scan(struct mbuf 
*m0
, void (*op
)(struct fileglob 
*, void *arg
), void *arg
) 
2369         struct fileglob 
**rp
; 
2375                 for (m 
= m0
; m
; m 
= m
->m_next
) 
2376                         if (m
->m_type 
== MT_CONTROL 
&& 
2377                             (size_t)m
->m_len 
>= sizeof (*cm
)) { 
2378                                 cm 
= mtod(m
, struct cmsghdr 
*); 
2379                                 if (cm
->cmsg_level 
!= SOL_SOCKET 
|| 
2380                                     cm
->cmsg_type 
!= SCM_RIGHTS
) 
2382                                 qfds 
= (cm
->cmsg_len 
- sizeof (*cm
)) / 
2384                                 rp 
= (struct fileglob 
**)(cm 
+ 1); 
2385                                 for (i 
= 0; i 
< qfds
; i
++) 
2387                                 break;          /* XXX, but saves time */ 
2394 unp_mark(struct fileglob 
*fg
, __unused 
void *arg
) 
2396         lck_mtx_lock(&fg
->fg_lock
); 
2398         if (fg
->fg_flag 
& FMARK
) { 
2399                 lck_mtx_unlock(&fg
->fg_lock
); 
2402         fg
->fg_flag 
|= (FMARK
|FDEFER
); 
2404         lck_mtx_unlock(&fg
->fg_lock
); 
2410 unp_discard(struct fileglob 
*fg
, void *p
) 
2413                 p 
= current_proc();             /* XXX */ 
2415         (void) OSAddAtomic(1, &unp_disposed
); 
2416         if (fg_removeuipc_mark(fg
)) { 
2417                 VERIFY(fg
->fg_lflags 
& FG_RMMSGQ
); 
2420         (void) OSAddAtomic(-1, &unp_rights
); 
2423         (void) closef_locked((struct fileproc 
*)0, fg
, p
); 
2428 unp_lock(struct socket 
*so
, int refcount
, void * lr
) 
2432                 lr_saved 
= (void *)  __builtin_return_address(0); 
2436                 lck_mtx_lock(&((struct unpcb 
*)so
->so_pcb
)->unp_mtx
); 
2438                 panic("unp_lock: so=%p NO PCB! lr=%p ref=0x%x\n",  
2439                         so
, lr_saved
, so
->so_usecount
); 
2442         if (so
->so_usecount 
< 0) 
2443                 panic("unp_lock: so=%p so_pcb=%p lr=%p ref=0x%x\n", 
2444                 so
, so
->so_pcb
, lr_saved
, so
->so_usecount
); 
2447                 VERIFY(so
->so_usecount 
> 0); 
2450         so
->lock_lr
[so
->next_lock_lr
] = lr_saved
; 
2451         so
->next_lock_lr 
= (so
->next_lock_lr
+1) % SO_LCKDBG_MAX
; 
2456 unp_unlock(struct socket 
*so
, int refcount
, void * lr
) 
2459         lck_mtx_t 
* mutex_held 
= NULL
; 
2460         struct unpcb 
*unp 
= sotounpcb(so
); 
2463                 lr_saved 
= (void *) __builtin_return_address(0); 
2469         if (so
->so_usecount 
< 0) 
2470                 panic("unp_unlock: so=%p usecount=%x\n", so
, so
->so_usecount
); 
2471         if (so
->so_pcb 
== NULL
) { 
2472                 panic("unp_unlock: so=%p NO PCB usecount=%x\n", so
, so
->so_usecount
); 
2474                 mutex_held 
= &((struct unpcb 
*)so
->so_pcb
)->unp_mtx
; 
2476         LCK_MTX_ASSERT(mutex_held
, LCK_MTX_ASSERT_OWNED
); 
2477         so
->unlock_lr
[so
->next_unlock_lr
] = lr_saved
; 
2478         so
->next_unlock_lr 
= (so
->next_unlock_lr
+1) % SO_LCKDBG_MAX
; 
2480         if (so
->so_usecount 
== 0 && (so
->so_flags 
& SOF_PCBCLEARING
)) { 
2481                 sofreelastref(so
, 1); 
2484                         FREE(unp
->unp_addr
, M_SONAME
); 
2486                 lck_mtx_unlock(mutex_held
); 
2488                 lck_mtx_destroy(&unp
->unp_mtx
, unp_mtx_grp
); 
2489                 zfree(unp_zone
, unp
); 
2493                 lck_mtx_unlock(mutex_held
); 
2500 unp_getlock(struct socket 
*so
, __unused 
int flags
) 
2502         struct unpcb 
*unp 
= (struct unpcb 
*)so
->so_pcb
; 
2506                 if (so
->so_usecount 
< 0) 
2507                         panic("unp_getlock: so=%p usecount=%x\n", so
, so
->so_usecount
); 
2508                 return(&unp
->unp_mtx
); 
2510                 panic("unp_getlock: so=%p NULL so_pcb\n", so
); 
2511                 return (so
->so_proto
->pr_domain
->dom_mtx
);