2 * Copyright (c) 2000-2010 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/filedesc.h>
79 #include <sys/namei.h>
80 #include <sys/proc_internal.h>
81 #include <sys/kauth.h>
82 #include <sys/protosw.h>
83 #include <sys/socket.h>
84 #include <sys/socketvar.h>
86 #include <sys/sysctl.h>
88 #include <sys/unpcb.h>
89 #include <sys/vnode_internal.h>
90 #include <sys/kdebug.h>
92 #include <kern/zalloc.h>
93 #include <kern/locks.h>
96 #include <security/mac_framework.h>
97 #endif /* CONFIG_MACF */
99 #define f_msgcount f_fglob->fg_msgcount
100 #define f_cred f_fglob->fg_cred
101 #define f_ops f_fglob->fg_ops
102 #define f_offset f_fglob->fg_offset
103 #define f_data f_fglob->fg_data
104 struct zone
*unp_zone
;
105 static unp_gen_t unp_gencnt
;
106 static u_int unp_count
;
108 static lck_attr_t
*unp_mtx_attr
;
109 static lck_grp_t
*unp_mtx_grp
;
110 static lck_grp_attr_t
*unp_mtx_grp_attr
;
111 static lck_rw_t
*unp_list_mtx
;
113 static lck_mtx_t
*unp_disconnect_lock
;
114 static lck_mtx_t
*unp_connect_lock
;
115 static u_int disconnect_in_progress
;
117 extern lck_mtx_t
*uipc_lock
;
118 static struct unp_head unp_shead
, unp_dhead
;
121 * mDNSResponder tracing. When enabled, endpoints connected to
122 * /var/run/mDNSResponder will be traced; during each send on
123 * the traced socket, we log the PID and process name of the
124 * sending process. We also print out a bit of info related
125 * to the data itself; this assumes ipc_msg_hdr in dnssd_ipc.h
126 * of mDNSResponder stays the same.
128 #define MDNSRESPONDER_PATH "/var/run/mDNSResponder"
130 static int unpst_tracemdns
; /* enable tracing */
132 #define MDNS_IPC_MSG_HDR_VERSION_1 1
134 struct mdns_ipc_msg_hdr
{
142 } __attribute__((packed
));
144 } __attribute__((packed
));
147 * Unix communications domain.
151 * rethink name space problems
152 * need a proper out-of-band
155 static struct sockaddr sun_noname
= { sizeof (sun_noname
), AF_LOCAL
, { 0 } };
156 static ino_t unp_ino
; /* prototype for fake inode numbers */
158 static int unp_attach(struct socket
*);
159 static void unp_detach(struct unpcb
*);
160 static int unp_bind(struct unpcb
*, struct sockaddr
*, proc_t
);
161 static int unp_connect(struct socket
*, struct sockaddr
*, proc_t
);
162 static void unp_disconnect(struct unpcb
*);
163 static void unp_shutdown(struct unpcb
*);
164 static void unp_drop(struct unpcb
*, int);
165 __private_extern__
void unp_gc(void);
166 static void unp_scan(struct mbuf
*, void (*)(struct fileglob
*));
167 static void unp_mark(struct fileglob
*);
168 static void unp_discard(struct fileglob
*);
169 static void unp_discard_fdlocked(struct fileglob
*, proc_t
);
170 static int unp_internalize(struct mbuf
*, proc_t
);
171 static int unp_listen(struct unpcb
*, proc_t
);
172 static void unpcb_to_compat(struct unpcb
*, struct unpcb_compat
*);
173 static void unp_get_locks_in_order(struct socket
*so
, struct socket
*conn_so
);
176 unp_get_locks_in_order(struct socket
*so
, struct socket
*conn_so
)
179 socket_lock(conn_so
, 1);
181 struct unpcb
*unp
= sotounpcb(so
);
182 unp
->unp_flags
|= UNP_DONTDISCONNECT
;
184 socket_unlock(so
, 0);
186 /* Get the locks in the correct order */
187 socket_lock(conn_so
, 1);
190 if (unp
->rw_thrcount
== 0) {
191 unp
->unp_flags
&= ~UNP_DONTDISCONNECT
;
198 uipc_abort(struct socket
*so
)
200 struct unpcb
*unp
= sotounpcb(so
);
204 unp_drop(unp
, ECONNABORTED
);
211 uipc_accept(struct socket
*so
, struct sockaddr
**nam
)
213 struct unpcb
*unp
= sotounpcb(so
);
219 * Pass back name of connected socket,
220 * if it was bound and we are still connected
221 * (our peer may have closed already!).
223 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
) {
224 *nam
= dup_sockaddr((struct sockaddr
*)
225 unp
->unp_conn
->unp_addr
, 1);
227 *nam
= dup_sockaddr((struct sockaddr
*)&sun_noname
, 1);
238 uipc_attach(struct socket
*so
, __unused
int proto
, __unused proc_t p
)
240 struct unpcb
*unp
= sotounpcb(so
);
244 return (unp_attach(so
));
248 uipc_bind(struct socket
*so
, struct sockaddr
*nam
, proc_t p
)
250 struct unpcb
*unp
= sotounpcb(so
);
255 return (unp_bind(unp
, nam
, p
));
261 * unp_connect:??? [See elsewhere in this file]
264 uipc_connect(struct socket
*so
, struct sockaddr
*nam
, proc_t p
)
266 struct unpcb
*unp
= sotounpcb(so
);
270 return (unp_connect(so
, nam
, p
));
276 * unp_connect2:EPROTOTYPE Protocol wrong type for socket
277 * unp_connect2:EINVAL Invalid argument
280 uipc_connect2(struct socket
*so1
, struct socket
*so2
)
282 struct unpcb
*unp
= sotounpcb(so1
);
287 return (unp_connect2(so1
, so2
));
290 /* control is EOPNOTSUPP */
293 uipc_detach(struct socket
*so
)
295 struct unpcb
*unp
= sotounpcb(so
);
300 lck_mtx_assert(&unp
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
306 uipc_disconnect(struct socket
*so
)
308 struct unpcb
*unp
= sotounpcb(so
);
321 uipc_listen(struct socket
*so
, __unused proc_t p
)
323 struct unpcb
*unp
= sotounpcb(so
);
325 if (unp
== 0 || unp
->unp_vnode
== 0)
327 return (unp_listen(unp
, p
));
331 uipc_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
333 struct unpcb
*unp
= sotounpcb(so
);
337 if (unp
->unp_conn
!= NULL
&& unp
->unp_conn
->unp_addr
!= NULL
) {
338 *nam
= dup_sockaddr((struct sockaddr
*)
339 unp
->unp_conn
->unp_addr
, 1);
341 *nam
= dup_sockaddr((struct sockaddr
*)&sun_noname
, 1);
347 uipc_rcvd(struct socket
*so
, __unused
int flags
)
349 struct unpcb
*unp
= sotounpcb(so
);
354 switch (so
->so_type
) {
356 panic("uipc_rcvd DGRAM?");
360 #define rcv (&so->so_rcv)
361 #define snd (&so2->so_snd)
362 if (unp
->unp_conn
== 0)
365 so2
= unp
->unp_conn
->unp_socket
;
366 unp_get_locks_in_order(so
, so2
);
368 * Adjust backpressure on sender
369 * and wakeup any waiting to write.
371 snd
->sb_mbmax
+= unp
->unp_mbcnt
- rcv
->sb_mbcnt
;
372 unp
->unp_mbcnt
= rcv
->sb_mbcnt
;
373 snd
->sb_hiwat
+= unp
->unp_cc
- rcv
->sb_cc
;
374 unp
->unp_cc
= rcv
->sb_cc
;
377 socket_unlock(so2
, 1);
384 panic("uipc_rcvd unknown socktype");
389 /* pru_rcvoob is EOPNOTSUPP */
398 * unp_internalize:EINVAL
399 * unp_internalize:EBADF
400 * unp_connect:EAFNOSUPPORT Address family not supported
401 * unp_connect:EINVAL Invalid argument
402 * unp_connect:ENOTSOCK Not a socket
403 * unp_connect:ECONNREFUSED Connection refused
404 * unp_connect:EISCONN Socket is connected
405 * unp_connect:EPROTOTYPE Protocol wrong type for socket
407 * sbappendaddr:ENOBUFS [5th argument, contents modified]
408 * sbappendaddr:??? [whatever a filter author chooses]
411 uipc_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
412 struct mbuf
*control
, proc_t p
)
415 struct unpcb
*unp
= sotounpcb(so
);
422 if (flags
& PRUS_OOB
) {
428 /* release lock to avoid deadlock (4436174) */
429 socket_unlock(so
, 0);
430 error
= unp_internalize(control
, p
);
436 switch (so
->so_type
) {
439 struct sockaddr
*from
;
446 error
= unp_connect(so
, nam
, p
);
450 if (unp
->unp_conn
== 0) {
456 so2
= unp
->unp_conn
->unp_socket
;
458 unp_get_locks_in_order(so
, so2
);
461 from
= (struct sockaddr
*)unp
->unp_addr
;
465 * sbappendaddr() will fail when the receiver runs out of
466 * space; in contrast to SOCK_STREAM, we will lose messages
467 * for the SOCK_DGRAM case when the receiver's queue overflows.
468 * SB_UNIX on the socket buffer implies that the callee will
469 * not free the control message, if any, because we would need
470 * to call unp_dispose() on it.
472 if (sbappendaddr(&so2
->so_rcv
, from
, m
, control
, &error
)) {
475 } else if (control
!= NULL
&& error
== 0) {
476 /* A socket filter took control; don't touch it */
481 socket_unlock(so2
, 1);
491 #define rcv (&so2->so_rcv)
492 #define snd (&so->so_snd)
493 /* Connect if not connected yet. */
495 * Note: A better implementation would complain
496 * if not equal to the peer's address.
498 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
500 error
= unp_connect(so
, nam
, p
);
509 if (so
->so_state
& SS_CANTSENDMORE
) {
513 if (unp
->unp_conn
== 0)
514 panic("uipc_send connected but no connection?");
516 so2
= unp
->unp_conn
->unp_socket
;
517 unp_get_locks_in_order(so
, so2
);
519 /* Check socket state again as we might have unlocked the socket
520 * while trying to get the locks in order
523 if ((so
->so_state
& SS_CANTSENDMORE
)) {
525 socket_unlock(so2
, 1);
529 if (unp
->unp_flags
& UNP_TRACE_MDNS
) {
530 struct mdns_ipc_msg_hdr hdr
;
532 if (mbuf_copydata(m
, 0, sizeof (hdr
), &hdr
) == 0 &&
533 hdr
.version
== ntohl(MDNS_IPC_MSG_HDR_VERSION_1
)) {
534 printf("%s[mDNSResponder] pid=%d (%s): op=0x%x\n",
535 __func__
, p
->p_pid
, p
->p_comm
, ntohl(hdr
.op
));
540 * Send to paired receive port, and then reduce send buffer
541 * hiwater marks to maintain backpressure. Wake up readers.
542 * SB_UNIX flag will allow new record to be appended to the
543 * receiver's queue even when it is already full. It is
544 * possible, however, that append might fail. In that case,
545 * we will need to call unp_dispose() on the control message;
546 * the callee will not free it since SB_UNIX is set.
548 didreceive
= control
?
549 sbappendcontrol(rcv
, m
, control
, &error
) : sbappend(rcv
, m
);
551 snd
->sb_mbmax
-= rcv
->sb_mbcnt
- unp
->unp_conn
->unp_mbcnt
;
552 unp
->unp_conn
->unp_mbcnt
= rcv
->sb_mbcnt
;
553 snd
->sb_hiwat
-= rcv
->sb_cc
- unp
->unp_conn
->unp_cc
;
554 unp
->unp_conn
->unp_cc
= rcv
->sb_cc
;
558 } else if (control
!= NULL
&& error
== 0) {
559 /* A socket filter took control; don't touch it */
563 socket_unlock(so2
, 1);
571 panic("uipc_send unknown socktype");
575 * SEND_EOF is equivalent to a SEND followed by
578 if (flags
& PRUS_EOF
) {
583 if (control
&& error
!= 0) {
584 socket_unlock(so
, 0);
585 unp_dispose(control
);
598 uipc_sense(struct socket
*so
, void *ub
, int isstat64
)
600 struct unpcb
*unp
= sotounpcb(so
);
607 blksize
= so
->so_snd
.sb_hiwat
;
608 if (so
->so_type
== SOCK_STREAM
&& unp
->unp_conn
!= 0) {
609 so2
= unp
->unp_conn
->unp_socket
;
610 blksize
+= so2
->so_rcv
.sb_cc
;
612 if (unp
->unp_ino
== 0)
613 unp
->unp_ino
= unp_ino
++;
618 sb64
= (struct stat64
*)ub
;
619 sb64
->st_blksize
= blksize
;
620 sb64
->st_dev
= NODEV
;
621 sb64
->st_ino
= (ino64_t
)unp
->unp_ino
;
625 sb
= (struct stat
*)ub
;
626 sb
->st_blksize
= blksize
;
628 sb
->st_ino
= (ino_t
)(uintptr_t)unp
->unp_ino
;
638 * Notes: This is not strictly correct, as unp_shutdown() also calls
639 * socantrcvmore(). These should maybe both be conditionalized
640 * on the 'how' argument in soshutdown() as called from the
641 * shutdown() system call.
644 uipc_shutdown(struct socket
*so
)
646 struct unpcb
*unp
= sotounpcb(so
);
657 * EINVAL Invalid argument
660 uipc_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
662 struct unpcb
*unp
= sotounpcb(so
);
666 if (unp
->unp_addr
!= NULL
) {
667 *nam
= dup_sockaddr((struct sockaddr
*)unp
->unp_addr
, 1);
669 *nam
= dup_sockaddr((struct sockaddr
*)&sun_noname
, 1);
674 struct pr_usrreqs uipc_usrreqs
= {
675 uipc_abort
, uipc_accept
, uipc_attach
, uipc_bind
, uipc_connect
,
676 uipc_connect2
, pru_control_notsupp
, uipc_detach
, uipc_disconnect
,
677 uipc_listen
, uipc_peeraddr
, uipc_rcvd
, pru_rcvoob_notsupp
,
678 uipc_send
, uipc_sense
, uipc_shutdown
, uipc_sockaddr
,
679 sosend
, soreceive
, pru_sopoll_notsupp
683 uipc_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
685 struct unpcb
*unp
= sotounpcb(so
);
688 switch (sopt
->sopt_dir
) {
690 switch (sopt
->sopt_name
) {
692 if (unp
->unp_flags
& UNP_HAVEPC
) {
693 error
= sooptcopyout(sopt
, &unp
->unp_peercred
,
694 sizeof (unp
->unp_peercred
));
696 if (so
->so_type
== SOCK_STREAM
)
716 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
717 * for stream sockets, although the total for sender and receiver is
718 * actually only PIPSIZ.
719 * Datagram sockets really use the sendspace as the maximum datagram size,
720 * and don't really want to reserve the sendspace. Their recvspace should
721 * be large enough for at least one max-size datagram plus address.
726 static u_int32_t unpst_sendspace
= PIPSIZ
;
727 static u_int32_t unpst_recvspace
= PIPSIZ
;
728 static u_int32_t unpdg_sendspace
= 2*1024; /* really max datagram size */
729 static u_int32_t unpdg_recvspace
= 4*1024;
731 static int unp_rights
; /* file descriptors in flight */
732 static int unp_disposed
; /* discarded file descriptors */
734 SYSCTL_DECL(_net_local_stream
);
735 SYSCTL_INT(_net_local_stream
, OID_AUTO
, sendspace
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
736 &unpst_sendspace
, 0, "");
737 SYSCTL_INT(_net_local_stream
, OID_AUTO
, recvspace
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
738 &unpst_recvspace
, 0, "");
739 SYSCTL_INT(_net_local_stream
, OID_AUTO
, tracemdns
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
740 &unpst_tracemdns
, 0, "");
741 SYSCTL_DECL(_net_local_dgram
);
742 SYSCTL_INT(_net_local_dgram
, OID_AUTO
, maxdgram
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
743 &unpdg_sendspace
, 0, "");
744 SYSCTL_INT(_net_local_dgram
, OID_AUTO
, recvspace
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
745 &unpdg_recvspace
, 0, "");
746 SYSCTL_DECL(_net_local
);
747 SYSCTL_INT(_net_local
, OID_AUTO
, inflight
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &unp_rights
, 0, "");
755 unp_attach(struct socket
*so
)
760 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
761 switch (so
->so_type
) {
764 error
= soreserve(so
, unpst_sendspace
, unpst_recvspace
);
768 error
= soreserve(so
, unpdg_sendspace
, unpdg_recvspace
);
777 unp
= (struct unpcb
*)zalloc(unp_zone
);
780 bzero(unp
, sizeof (*unp
));
782 lck_mtx_init(&unp
->unp_mtx
,
783 unp_mtx_grp
, unp_mtx_attr
);
785 lck_rw_lock_exclusive(unp_list_mtx
);
786 LIST_INIT(&unp
->unp_refs
);
787 unp
->unp_socket
= so
;
788 unp
->unp_gencnt
= ++unp_gencnt
;
790 LIST_INSERT_HEAD(so
->so_type
== SOCK_DGRAM
?
791 &unp_dhead
: &unp_shead
, unp
, unp_link
);
792 lck_rw_done(unp_list_mtx
);
793 so
->so_pcb
= (caddr_t
)unp
;
795 * Mark AF_UNIX socket buffers accordingly so that:
797 * a. In the SOCK_STREAM case, socket buffer append won't fail due to
798 * the lack of space; this essentially loosens the sbspace() check,
799 * since there is disconnect between sosend() and uipc_send() with
800 * respect to flow control that might result in our dropping the
801 * data in uipc_send(). By setting this, we allow for slightly
802 * more records to be appended to the receiving socket to avoid
803 * losing data (which we can't afford in the SOCK_STREAM case).
804 * Flow control still takes place since we adjust the sender's
805 * hiwat during each send. This doesn't affect the SOCK_DGRAM
806 * case and append would still fail when the queue overflows.
808 * b. In the presence of control messages containing internalized
809 * file descriptors, the append routines will not free them since
810 * we'd need to undo the work first via unp_dispose().
812 so
->so_rcv
.sb_flags
|= SB_UNIX
;
813 so
->so_snd
.sb_flags
|= SB_UNIX
;
818 unp_detach(struct unpcb
*unp
)
822 lck_rw_lock_exclusive(unp_list_mtx
);
823 LIST_REMOVE(unp
, unp_link
);
824 lck_rw_done(unp_list_mtx
);
825 if (unp
->unp_vnode
) {
826 struct vnode
*tvp
= NULL
;
827 socket_unlock(unp
->unp_socket
, 0);
829 /* Holding unp_connect_lock will avoid a race between
830 * a thread closing the listening socket and a thread
833 lck_mtx_lock(unp_connect_lock
);
834 socket_lock(unp
->unp_socket
, 0);
835 if (unp
->unp_vnode
) {
836 tvp
= unp
->unp_vnode
;
837 unp
->unp_vnode
->v_socket
= NULL
;
838 unp
->unp_vnode
= NULL
;
840 lck_mtx_unlock(unp_connect_lock
);
842 vnode_rele(tvp
); /* drop the usecount */
846 while (unp
->unp_refs
.lh_first
) {
847 struct unpcb
*unp2
= NULL
;
849 /* This datagram socket is connected to one or more
850 * sockets. In order to avoid a race condition between removing
851 * this reference and closing the connected socket, we need
852 * to check disconnect_in_progress
854 if (so_locked
== 1) {
855 socket_unlock(unp
->unp_socket
, 0);
858 lck_mtx_lock(unp_disconnect_lock
);
859 while (disconnect_in_progress
!= 0) {
860 (void)msleep((caddr_t
)&disconnect_in_progress
, unp_disconnect_lock
,
861 PSOCK
, "disconnect", NULL
);
863 disconnect_in_progress
= 1;
864 lck_mtx_unlock(unp_disconnect_lock
);
866 /* Now we are sure that any unpcb socket disconnect is not happening */
867 if (unp
->unp_refs
.lh_first
!= NULL
) {
868 unp2
= unp
->unp_refs
.lh_first
;
869 socket_lock(unp2
->unp_socket
, 1);
872 lck_mtx_lock(unp_disconnect_lock
);
873 disconnect_in_progress
= 0;
874 wakeup(&disconnect_in_progress
);
875 lck_mtx_unlock(unp_disconnect_lock
);
878 /* We already locked this socket and have a reference on it */
879 unp_drop(unp2
, ECONNRESET
);
880 socket_unlock(unp2
->unp_socket
, 1);
884 if (so_locked
== 0) {
885 socket_lock(unp
->unp_socket
, 0);
888 soisdisconnected(unp
->unp_socket
);
889 /* makes sure we're getting dealloced */
890 unp
->unp_socket
->so_flags
|= SOF_PCBCLEARING
;
898 * namei:??? [anything namei can return]
899 * vnode_authorize:??? [anything vnode_authorize can return]
901 * Notes: p at this point is the current process, as this function is
902 * only called by sobind().
907 struct sockaddr
*nam
,
910 struct sockaddr_un
*soun
= (struct sockaddr_un
*)nam
;
911 struct vnode
*vp
, *dvp
;
912 struct vnode_attr va
;
913 vfs_context_t ctx
= vfs_context_current();
916 struct socket
*so
= unp
->unp_socket
;
917 char buf
[SOCK_MAXADDRLEN
];
919 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_UNIX
) {
920 return (EAFNOSUPPORT
);
923 if (unp
->unp_vnode
!= NULL
)
925 namelen
= soun
->sun_len
- offsetof(struct sockaddr_un
, sun_path
);
929 socket_unlock(so
, 0);
931 strlcpy(buf
, soun
->sun_path
, namelen
+1);
932 NDINIT(&nd
, CREATE
, OP_MKFIFO
, FOLLOW
| LOCKPARENT
, UIO_SYSSPACE
,
933 CAST_USER_ADDR_T(buf
), ctx
);
934 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
945 * need to do this before the vnode_put of dvp
946 * since we may have to release an fs_nodelock
958 VATTR_SET(&va
, va_type
, VSOCK
);
959 VATTR_SET(&va
, va_mode
, (ACCESSPERMS
& ~p
->p_fd
->fd_cmask
));
962 error
= mac_vnode_check_create(ctx
,
963 nd
.ni_dvp
, &nd
.ni_cnd
, &va
);
966 #endif /* CONFIG_MACF */
967 #if CONFIG_MACF_SOCKET_SUBSET
968 error
= mac_vnode_check_uipc_bind(ctx
,
969 nd
.ni_dvp
, &nd
.ni_cnd
, &va
);
972 #endif /* MAC_SOCKET_SUBSET */
973 /* authorize before creating */
974 error
= vnode_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, ctx
);
977 /* create the socket */
978 error
= vn_create(dvp
, &vp
, &nd
, &va
, 0, 0, NULL
, ctx
);
988 vnode_ref(vp
); /* gain a longterm reference */
990 vp
->v_socket
= unp
->unp_socket
;
992 unp
->unp_addr
= (struct sockaddr_un
*)dup_sockaddr(nam
, 1);
993 vnode_put(vp
); /* drop the iocount */
1000 * Returns: 0 Success
1001 * EAFNOSUPPORT Address family not supported
1002 * EINVAL Invalid argument
1003 * ENOTSOCK Not a socket
1004 * ECONNREFUSED Connection refused
1005 * EPROTOTYPE Protocol wrong type for socket
1006 * EISCONN Socket is connected
1007 * unp_connect2:EPROTOTYPE Protocol wrong type for socket
1008 * unp_connect2:EINVAL Invalid argument
1009 * namei:??? [anything namei can return]
1010 * vnode_authorize:???? [anything vnode_authorize can return]
1012 * Notes: p at this point is the current process, as this function is
1013 * only called by sosend(), sendfile(), and soconnectlock().
1016 unp_connect(struct socket
*so
, struct sockaddr
*nam
, __unused proc_t p
)
1018 struct sockaddr_un
*soun
= (struct sockaddr_un
*)nam
;
1020 struct socket
*so2
, *so3
, *list_so
=NULL
;
1021 struct unpcb
*unp
, *unp2
, *unp3
;
1022 vfs_context_t ctx
= vfs_context_current();
1024 struct nameidata nd
;
1025 char buf
[SOCK_MAXADDRLEN
];
1027 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_UNIX
) {
1028 return (EAFNOSUPPORT
);
1031 unp
= sotounpcb(so
);
1034 len
= nam
->sa_len
- offsetof(struct sockaddr_un
, sun_path
);
1038 strlcpy(buf
, soun
->sun_path
, len
+1);
1039 socket_unlock(so
, 0);
1041 NDINIT(&nd
, LOOKUP
, OP_LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
,
1042 CAST_USER_ADDR_T(buf
), ctx
);
1050 if (vp
->v_type
!= VSOCK
) {
1056 #if CONFIG_MACF_SOCKET_SUBSET
1057 error
= mac_vnode_check_uipc_connect(ctx
, vp
);
1062 #endif /* MAC_SOCKET_SUBSET */
1064 error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_WRITE_DATA
, ctx
);
1070 lck_mtx_lock(unp_connect_lock
);
1072 if (vp
->v_socket
== 0) {
1073 lck_mtx_unlock(unp_connect_lock
);
1074 error
= ECONNREFUSED
;
1079 socket_lock(vp
->v_socket
, 1); /* Get a reference on the listening socket */
1081 lck_mtx_unlock(unp_connect_lock
);
1084 if (so2
->so_pcb
== NULL
) {
1085 error
= ECONNREFUSED
;
1087 socket_unlock(so2
, 1);
1090 /* Release the reference held for the listen socket */
1097 socket_unlock(so2
, 0);
1099 socket_lock(so2
, 0);
1100 } else if (so
> so2
) {
1104 * Check if socket was connected while we were trying to
1105 * get the socket locks in order.
1106 * XXX - probably shouldn't return an error for SOCK_DGRAM
1108 if ((so
->so_state
& SS_ISCONNECTED
) != 0) {
1113 if (so
->so_type
!= so2
->so_type
) {
1118 if (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) {
1119 /* Release the incoming socket but keep a reference */
1120 socket_unlock(so
, 0);
1122 if ((so2
->so_options
& SO_ACCEPTCONN
) == 0 ||
1123 (so3
= sonewconn(so2
, 0, nam
)) == 0) {
1124 error
= ECONNREFUSED
;
1125 socket_unlock(so2
, 1);
1129 unp2
= sotounpcb(so2
);
1130 unp3
= sotounpcb(so3
);
1132 unp3
->unp_addr
= (struct sockaddr_un
*)
1133 dup_sockaddr((struct sockaddr
*)unp2
->unp_addr
, 1);
1136 * unp_peercred management:
1138 * The connecter's (client's) credentials are copied
1139 * from its process structure at the time of connect()
1142 cru2x(vfs_context_ucred(ctx
), &unp3
->unp_peercred
);
1143 unp3
->unp_flags
|= UNP_HAVEPC
;
1145 * The receiver's (server's) credentials are copied
1146 * from the unp_peercred member of socket on which the
1147 * former called listen(); unp_listen() cached that
1148 * process's credentials at that time so we can use
1151 KASSERT(unp2
->unp_flags
& UNP_HAVEPCCACHED
,
1152 ("unp_connect: listener without cached peercred"));
1154 /* Here we need to have both so and so2 locks and so2
1155 * is already locked. Lock ordering is required.
1158 socket_unlock(so2
, 0);
1160 socket_lock(so2
, 0);
1165 /* Check again if the socket state changed when its lock was released */
1166 if ((so
->so_state
& SS_ISCONNECTED
) != 0) {
1168 socket_unlock(so2
, 1);
1169 socket_lock(so3
, 0);
1170 sofreelastref(so3
, 1);
1173 memcpy(&unp
->unp_peercred
, &unp2
->unp_peercred
,
1174 sizeof (unp
->unp_peercred
));
1175 unp
->unp_flags
|= UNP_HAVEPC
;
1177 #if CONFIG_MACF_SOCKET
1178 /* XXXMAC: recursive lock: SOCK_LOCK(so); */
1179 mac_socketpeer_label_associate_socket(so
, so3
);
1180 mac_socketpeer_label_associate_socket(so3
, so
);
1181 /* XXXMAC: SOCK_UNLOCK(so); */
1182 #endif /* MAC_SOCKET */
1184 /* Hold the reference on listening socket until the end */
1185 socket_unlock(so2
, 0);
1188 /* Lock ordering doesn't matter because so3 was just created */
1189 socket_lock(so3
, 1);
1193 * Enable tracing for mDNSResponder endpoints. (The use
1194 * of sizeof instead of strlen below takes the null
1195 * terminating character into account.)
1197 if (unpst_tracemdns
&&
1198 !strncmp(soun
->sun_path
, MDNSRESPONDER_PATH
,
1199 sizeof (MDNSRESPONDER_PATH
))) {
1200 unp
->unp_flags
|= UNP_TRACE_MDNS
;
1201 unp2
->unp_flags
|= UNP_TRACE_MDNS
;
1205 error
= unp_connect2(so
, so2
);
1210 socket_unlock(so2
, 1);
1212 /* Release the extra reference held for the listen socket.
1213 * This is possible only for SOCK_DGRAM sockets. We refuse
1214 * connecting to the same socket for SOCK_STREAM sockets.
1220 if (list_so
!= NULL
) {
1221 socket_lock(list_so
, 0);
1222 socket_unlock(list_so
, 1);
1226 lck_mtx_assert(&unp
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
1232 * Returns: 0 Success
1233 * EPROTOTYPE Protocol wrong type for socket
1234 * EINVAL Invalid argument
1237 unp_connect2(struct socket
*so
, struct socket
*so2
)
1239 struct unpcb
*unp
= sotounpcb(so
);
1242 if (so2
->so_type
!= so
->so_type
)
1243 return (EPROTOTYPE
);
1245 unp2
= sotounpcb(so2
);
1247 lck_mtx_assert(&unp
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
1248 lck_mtx_assert(&unp2
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
1250 /* Verify both sockets are still opened */
1251 if (unp
== 0 || unp2
== 0)
1254 unp
->unp_conn
= unp2
;
1257 switch (so
->so_type
) {
1260 LIST_INSERT_HEAD(&unp2
->unp_refs
, unp
, unp_reflink
);
1263 /* Avoid lock order reversals due to drop/acquire in soisconnected. */
1264 /* Keep an extra reference on so2 that will be dropped
1265 * soon after getting the locks in order
1267 socket_unlock(so2
, 0);
1269 unp_get_locks_in_order(so
, so2
);
1278 /* This takes care of socketpair */
1279 if (!(unp
->unp_flags
& UNP_HAVEPC
) &&
1280 !(unp2
->unp_flags
& UNP_HAVEPC
)) {
1281 cru2x(kauth_cred_get(), &unp
->unp_peercred
);
1282 unp
->unp_flags
|= UNP_HAVEPC
;
1284 cru2x(kauth_cred_get(), &unp2
->unp_peercred
);
1285 unp2
->unp_flags
|= UNP_HAVEPC
;
1287 unp2
->unp_conn
= unp
;
1290 /* Avoid lock order reversals due to drop/acquire in soisconnected. */
1291 socket_unlock(so
, 0);
1294 /* Keep an extra reference on so2, that will be dropped soon after
1295 * getting the locks in order again.
1297 socket_unlock(so2
, 0);
1302 unp_get_locks_in_order(so
, so2
);
1303 /* Decrement the extra reference left before */
1308 panic("unknown socket type %d in unp_connect2", so
->so_type
);
1310 lck_mtx_assert(&unp
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
1311 lck_mtx_assert(&unp2
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
1316 unp_disconnect(struct unpcb
*unp
)
1318 struct unpcb
*unp2
= NULL
;
1319 struct socket
*so2
= NULL
, *so
;
1320 struct socket
*waitso
;
1321 int so_locked
= 1, strdisconn
= 0;
1323 so
= unp
->unp_socket
;
1324 if (unp
->unp_conn
== NULL
) {
1327 lck_mtx_lock(unp_disconnect_lock
);
1328 while (disconnect_in_progress
!= 0) {
1329 if (so_locked
== 1) {
1330 socket_unlock(so
, 0);
1333 (void)msleep((caddr_t
)&disconnect_in_progress
, unp_disconnect_lock
,
1334 PSOCK
, "disconnect", NULL
);
1336 disconnect_in_progress
= 1;
1337 lck_mtx_unlock(unp_disconnect_lock
);
1339 if (so_locked
== 0) {
1344 unp2
= unp
->unp_conn
;
1346 if (unp2
== 0 || unp2
->unp_socket
== NULL
) {
1349 so2
= unp2
->unp_socket
;
1353 if (so_locked
== 0) {
1357 } else if (so
< so2
) {
1358 if (so_locked
== 0) {
1361 socket_lock(so2
, 1);
1364 if (so_locked
== 1) {
1365 socket_unlock(so
, 0);
1367 socket_lock(so2
, 1);
1373 lck_mtx_assert(&unp
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
1374 lck_mtx_assert(&unp2
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
1376 /* Check for the UNP_DONTDISCONNECT flag, if it
1377 * is set, release both sockets and go to sleep
1380 if ((((struct unpcb
*)waitso
->so_pcb
)->unp_flags
& UNP_DONTDISCONNECT
) != 0) {
1382 socket_unlock(so2
, 1);
1386 (void)msleep(waitso
->so_pcb
, &unp
->unp_mtx
,
1387 PSOCK
| PDROP
, "unpdisconnect", NULL
);
1391 if (unp
->unp_conn
== NULL
) {
1392 panic("unp_conn became NULL after sleep");
1395 unp
->unp_conn
= NULL
;
1398 if (unp
->unp_flags
& UNP_TRACE_MDNS
)
1399 unp
->unp_flags
&= ~UNP_TRACE_MDNS
;
1401 switch (unp
->unp_socket
->so_type
) {
1404 LIST_REMOVE(unp
, unp_reflink
);
1405 unp
->unp_socket
->so_state
&= ~SS_ISCONNECTED
;
1407 socket_unlock(so2
, 1);
1411 unp2
->unp_conn
= NULL
;
1414 /* Set the socket state correctly but do a wakeup later when
1415 * we release all locks except the socket lock, this will avoid
1418 unp
->unp_socket
->so_state
&= ~(SS_ISCONNECTING
|SS_ISCONNECTED
|SS_ISDISCONNECTING
);
1419 unp
->unp_socket
->so_state
|= (SS_CANTRCVMORE
|SS_CANTSENDMORE
|SS_ISDISCONNECTED
);
1421 unp2
->unp_socket
->so_state
&= ~(SS_ISCONNECTING
|SS_ISCONNECTED
|SS_ISDISCONNECTING
);
1422 unp
->unp_socket
->so_state
|= (SS_CANTRCVMORE
|SS_CANTSENDMORE
|SS_ISDISCONNECTED
);
1424 if (unp2
->unp_flags
& UNP_TRACE_MDNS
)
1425 unp2
->unp_flags
&= ~UNP_TRACE_MDNS
;
1430 panic("unknown socket type %d", so
->so_type
);
1433 lck_mtx_lock(unp_disconnect_lock
);
1434 disconnect_in_progress
= 0;
1435 wakeup(&disconnect_in_progress
);
1436 lck_mtx_unlock(unp_disconnect_lock
);
1439 socket_unlock(so
, 0);
1440 soisdisconnected(so2
);
1441 socket_unlock(so2
, 1);
1444 soisdisconnected(so
);
1446 lck_mtx_assert(&unp
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
1451 * unpcb_to_compat copies specific bits of a unpcb to a unpcb_compat format.
1452 * The unpcb_compat data structure is passed to user space and must not change.
1455 unpcb_to_compat(struct unpcb
*up
, struct unpcb_compat
*cp
)
1457 #if defined(__LP64__)
1458 cp
->unp_link
.le_next
= (u_int32_t
)(uintptr_t)up
->unp_link
.le_next
;
1459 cp
->unp_link
.le_prev
= (u_int32_t
)(uintptr_t)up
->unp_link
.le_prev
;
1461 cp
->unp_link
.le_next
= (struct unpcb_compat
*)up
->unp_link
.le_next
;
1462 cp
->unp_link
.le_prev
= (struct unpcb_compat
**)up
->unp_link
.le_prev
;
1464 cp
->unp_socket
= (_UNPCB_PTR(struct socket
*))(uintptr_t)up
->unp_socket
;
1465 cp
->unp_vnode
= (_UNPCB_PTR(struct vnode
*))(uintptr_t)up
->unp_vnode
;
1466 cp
->unp_ino
= up
->unp_ino
;
1467 cp
->unp_conn
= (_UNPCB_PTR(struct unpcb_compat
*))
1468 (uintptr_t)up
->unp_conn
;
1469 cp
->unp_refs
= (u_int32_t
)(uintptr_t)up
->unp_refs
.lh_first
;
1470 #if defined(__LP64__)
1471 cp
->unp_reflink
.le_next
=
1472 (u_int32_t
)(uintptr_t)up
->unp_reflink
.le_next
;
1473 cp
->unp_reflink
.le_prev
=
1474 (u_int32_t
)(uintptr_t)up
->unp_reflink
.le_prev
;
1476 cp
->unp_reflink
.le_next
=
1477 (struct unpcb_compat
*)up
->unp_reflink
.le_next
;
1478 cp
->unp_reflink
.le_prev
=
1479 (struct unpcb_compat
**)up
->unp_reflink
.le_prev
;
1481 cp
->unp_addr
= (_UNPCB_PTR(struct sockaddr_un
*))
1482 (uintptr_t)up
->unp_addr
;
1483 cp
->unp_cc
= up
->unp_cc
;
1484 cp
->unp_mbcnt
= up
->unp_mbcnt
;
1485 cp
->unp_gencnt
= up
->unp_gencnt
;
1489 unp_pcblist SYSCTL_HANDLER_ARGS
1491 #pragma unused(oidp,arg2)
1493 struct unpcb
*unp
, **unp_list
;
1496 struct unp_head
*head
;
1498 lck_rw_lock_shared(unp_list_mtx
);
1499 head
= ((intptr_t)arg1
== SOCK_DGRAM
? &unp_dhead
: &unp_shead
);
1502 * The process of preparing the PCB list is too time-consuming and
1503 * resource-intensive to repeat twice on every request.
1505 if (req
->oldptr
== USER_ADDR_NULL
) {
1507 req
->oldidx
= 2 * sizeof (xug
) + (n
+ n
/ 8) *
1508 sizeof (struct xunpcb
);
1509 lck_rw_done(unp_list_mtx
);
1513 if (req
->newptr
!= USER_ADDR_NULL
) {
1514 lck_rw_done(unp_list_mtx
);
1519 * OK, now we're committed to doing something.
1521 gencnt
= unp_gencnt
;
1524 bzero(&xug
, sizeof (xug
));
1525 xug
.xug_len
= sizeof (xug
);
1527 xug
.xug_gen
= gencnt
;
1528 xug
.xug_sogen
= so_gencnt
;
1529 error
= SYSCTL_OUT(req
, &xug
, sizeof (xug
));
1531 lck_rw_done(unp_list_mtx
);
1536 * We are done if there is no pcb
1539 lck_rw_done(unp_list_mtx
);
1543 MALLOC(unp_list
, struct unpcb
**, n
* sizeof (*unp_list
),
1545 if (unp_list
== 0) {
1546 lck_rw_done(unp_list_mtx
);
1550 for (unp
= head
->lh_first
, i
= 0; unp
&& i
< n
;
1551 unp
= unp
->unp_link
.le_next
) {
1552 if (unp
->unp_gencnt
<= gencnt
)
1553 unp_list
[i
++] = unp
;
1555 n
= i
; /* in case we lost some during malloc */
1558 for (i
= 0; i
< n
; i
++) {
1560 if (unp
->unp_gencnt
<= gencnt
) {
1563 bzero(&xu
, sizeof (xu
));
1564 xu
.xu_len
= sizeof (xu
);
1565 xu
.xu_unpp
= (_UNPCB_PTR(struct unpcb_compat
*))
1568 * XXX - need more locking here to protect against
1569 * connect/disconnect races for SMP.
1572 bcopy(unp
->unp_addr
, &xu
.xu_addr
,
1573 unp
->unp_addr
->sun_len
);
1574 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
)
1575 bcopy(unp
->unp_conn
->unp_addr
,
1577 unp
->unp_conn
->unp_addr
->sun_len
);
1578 unpcb_to_compat(unp
, &xu
.xu_unp
);
1579 sotoxsocket(unp
->unp_socket
, &xu
.xu_socket
);
1580 error
= SYSCTL_OUT(req
, &xu
, sizeof (xu
));
1585 * Give the user an updated idea of our state.
1586 * If the generation differs from what we told
1587 * her before, she knows that something happened
1588 * while we were processing this request, and it
1589 * might be necessary to retry.
1591 bzero(&xug
, sizeof (xug
));
1592 xug
.xug_len
= sizeof (xug
);
1593 xug
.xug_gen
= unp_gencnt
;
1594 xug
.xug_sogen
= so_gencnt
;
1595 xug
.xug_count
= unp_count
;
1596 error
= SYSCTL_OUT(req
, &xug
, sizeof (xug
));
1598 FREE(unp_list
, M_TEMP
);
1599 lck_rw_done(unp_list_mtx
);
1603 SYSCTL_PROC(_net_local_dgram
, OID_AUTO
, pcblist
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
1604 (caddr_t
)(long)SOCK_DGRAM
, 0, unp_pcblist
, "S,xunpcb",
1605 "List of active local datagram sockets");
1606 SYSCTL_PROC(_net_local_stream
, OID_AUTO
, pcblist
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
1607 (caddr_t
)(long)SOCK_STREAM
, 0, unp_pcblist
, "S,xunpcb",
1608 "List of active local stream sockets");
1610 #if !CONFIG_EMBEDDED
1613 unp_pcblist64 SYSCTL_HANDLER_ARGS
1615 #pragma unused(oidp,arg2)
1617 struct unpcb
*unp
, **unp_list
;
1620 struct unp_head
*head
;
1622 lck_rw_lock_shared(unp_list_mtx
);
1623 head
= ((intptr_t)arg1
== SOCK_DGRAM
? &unp_dhead
: &unp_shead
);
1626 * The process of preparing the PCB list is too time-consuming and
1627 * resource-intensive to repeat twice on every request.
1629 if (req
->oldptr
== USER_ADDR_NULL
) {
1631 req
->oldidx
= 2 * sizeof (xug
) + (n
+ n
/ 8) *
1632 (sizeof (struct xunpcb64
));
1633 lck_rw_done(unp_list_mtx
);
1637 if (req
->newptr
!= USER_ADDR_NULL
) {
1638 lck_rw_done(unp_list_mtx
);
1643 * OK, now we're committed to doing something.
1645 gencnt
= unp_gencnt
;
1648 bzero(&xug
, sizeof (xug
));
1649 xug
.xug_len
= sizeof (xug
);
1651 xug
.xug_gen
= gencnt
;
1652 xug
.xug_sogen
= so_gencnt
;
1653 error
= SYSCTL_OUT(req
, &xug
, sizeof (xug
));
1655 lck_rw_done(unp_list_mtx
);
1660 * We are done if there is no pcb
1663 lck_rw_done(unp_list_mtx
);
1667 MALLOC(unp_list
, struct unpcb
**, n
* sizeof (*unp_list
),
1669 if (unp_list
== 0) {
1670 lck_rw_done(unp_list_mtx
);
1674 for (unp
= head
->lh_first
, i
= 0; unp
&& i
< n
;
1675 unp
= unp
->unp_link
.le_next
) {
1676 if (unp
->unp_gencnt
<= gencnt
)
1677 unp_list
[i
++] = unp
;
1679 n
= i
; /* in case we lost some during malloc */
1682 for (i
= 0; i
< n
; i
++) {
1684 if (unp
->unp_gencnt
<= gencnt
) {
1686 size_t xu_len
= sizeof(struct xunpcb64
);
1690 xu
.xu_unpp
= (u_int64_t
)(uintptr_t)unp
;
1691 xu
.xunp_link
.le_next
=
1692 (u_int64_t
)(uintptr_t)unp
->unp_link
.le_next
;
1693 xu
.xunp_link
.le_prev
=
1694 (u_int64_t
)(uintptr_t)unp
->unp_link
.le_prev
;
1695 xu
.xunp_socket
= (u_int64_t
)(uintptr_t)unp
->unp_socket
;
1696 xu
.xunp_vnode
= (u_int64_t
)(uintptr_t)unp
->unp_vnode
;
1697 xu
.xunp_ino
= unp
->unp_ino
;
1698 xu
.xunp_conn
= (u_int64_t
)(uintptr_t)unp
->unp_conn
;
1699 xu
.xunp_refs
= (u_int64_t
)(uintptr_t)unp
->unp_refs
.lh_first
;
1700 xu
.xunp_reflink
.le_next
=
1701 (u_int64_t
)(uintptr_t)unp
->unp_reflink
.le_next
;
1702 xu
.xunp_reflink
.le_prev
=
1703 (u_int64_t
)(uintptr_t)unp
->unp_reflink
.le_prev
;
1704 xu
.xunp_cc
= unp
->unp_cc
;
1705 xu
.xunp_mbcnt
= unp
->unp_mbcnt
;
1706 xu
.xunp_gencnt
= unp
->unp_gencnt
;
1708 if (unp
->unp_socket
)
1709 sotoxsocket64(unp
->unp_socket
, &xu
.xu_socket
);
1712 * XXX - need more locking here to protect against
1713 * connect/disconnect races for SMP.
1716 bcopy(unp
->unp_addr
, &xu
.xunp_addr
,
1717 unp
->unp_addr
->sun_len
);
1718 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
)
1719 bcopy(unp
->unp_conn
->unp_addr
,
1721 unp
->unp_conn
->unp_addr
->sun_len
);
1723 error
= SYSCTL_OUT(req
, &xu
, xu_len
);
1728 * Give the user an updated idea of our state.
1729 * If the generation differs from what we told
1730 * her before, she knows that something happened
1731 * while we were processing this request, and it
1732 * might be necessary to retry.
1734 bzero(&xug
, sizeof (xug
));
1735 xug
.xug_len
= sizeof (xug
);
1736 xug
.xug_gen
= unp_gencnt
;
1737 xug
.xug_sogen
= so_gencnt
;
1738 xug
.xug_count
= unp_count
;
1739 error
= SYSCTL_OUT(req
, &xug
, sizeof (xug
));
1741 FREE(unp_list
, M_TEMP
);
1742 lck_rw_done(unp_list_mtx
);
1746 SYSCTL_PROC(_net_local_dgram
, OID_AUTO
, pcblist64
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
1747 (caddr_t
)(long)SOCK_DGRAM
, 0, unp_pcblist64
, "S,xunpcb64",
1748 "List of active local datagram sockets 64 bit");
1749 SYSCTL_PROC(_net_local_stream
, OID_AUTO
, pcblist64
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
1750 (caddr_t
)(long)SOCK_STREAM
, 0, unp_pcblist64
, "S,xunpcb64",
1751 "List of active local stream sockets 64 bit");
1753 #endif /* !CONFIG_EMBEDDED */
1756 unp_shutdown(struct unpcb
*unp
)
1758 struct socket
*so
= unp
->unp_socket
;
1760 if (unp
->unp_socket
->so_type
== SOCK_STREAM
&& unp
->unp_conn
) {
1761 so2
= unp
->unp_conn
->unp_socket
;
1762 unp_get_locks_in_order(so
, so2
);
1764 socket_unlock(so2
, 1);
1769 unp_drop(struct unpcb
*unp
, int errno
)
1771 struct socket
*so
= unp
->unp_socket
;
1773 so
->so_error
= errno
;
1774 unp_disconnect(unp
);
1778 * Returns: 0 Success
1779 * EMSGSIZE The new fd's will not fit
1780 * ENOBUFS Cannot alloc struct fileproc
1783 unp_externalize(struct mbuf
*rights
)
1785 proc_t p
= current_proc(); /* XXX */
1787 struct cmsghdr
*cm
= mtod(rights
, struct cmsghdr
*);
1788 struct fileglob
**rp
= (struct fileglob
**)(cm
+ 1);
1789 int *fds
= (int *)(cm
+ 1);
1790 struct fileproc
*fp
;
1791 struct fileglob
*fg
;
1792 int newfds
= (cm
->cmsg_len
- sizeof (*cm
)) / sizeof (int);
1798 * if the new FD's will not fit, then we free them all
1800 if (!fdavail(p
, newfds
)) {
1801 for (i
= 0; i
< newfds
; i
++) {
1803 unp_discard_fdlocked(fg
, p
);
1811 * now change each pointer to an fd in the global table to
1812 * an integer that is the index to the local fd table entry
1813 * that we set up to point to the global one we are transferring.
1814 * XXX (1) this assumes a pointer and int are the same size,
1815 * XXX or the mbuf can hold the expansion
1816 * XXX (2) allocation failures should be non-fatal
1818 for (i
= 0; i
< newfds
; i
++) {
1819 #if CONFIG_MACF_SOCKET
1821 * If receive access is denied, don't pass along
1822 * and error message, just discard the descriptor.
1824 if (mac_file_check_receive(kauth_cred_get(), *rp
)) {
1827 unp_discard_fdlocked(fg
, p
);
1831 if (fdalloc(p
, 0, &f
))
1832 panic("unp_externalize:fdalloc");
1834 MALLOC_ZONE(fp
, struct fileproc
*, sizeof (struct fileproc
),
1835 M_FILEPROC
, M_WAITOK
);
1837 panic("unp_externalize: MALLOC_ZONE");
1838 bzero(fp
, sizeof (struct fileproc
));
1842 procfdtbl_releasefd(p
, f
, fp
);
1843 (void) OSAddAtomic(-1, &unp_rights
);
1854 unp_zone
= zinit(sizeof (struct unpcb
),
1855 (nmbclusters
* sizeof (struct unpcb
)), 4096, "unpzone");
1859 LIST_INIT(&unp_dhead
);
1860 LIST_INIT(&unp_shead
);
1863 * allocate lock group attribute and group for udp pcb mutexes
1865 unp_mtx_grp_attr
= lck_grp_attr_alloc_init();
1867 unp_mtx_grp
= lck_grp_alloc_init("unp_list", unp_mtx_grp_attr
);
1869 unp_mtx_attr
= lck_attr_alloc_init();
1871 if ((unp_list_mtx
= lck_rw_alloc_init(unp_mtx_grp
,
1872 unp_mtx_attr
)) == NULL
)
1873 return; /* pretty much dead if this fails... */
1875 if ((unp_disconnect_lock
= lck_mtx_alloc_init(unp_mtx_grp
,
1876 unp_mtx_attr
)) == NULL
)
1879 if ((unp_connect_lock
= lck_mtx_alloc_init(unp_mtx_grp
,
1880 unp_mtx_attr
)) == NULL
)
1885 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
1889 * Returns: 0 Success
1891 * fdgetf_noref:EBADF
1894 unp_internalize(struct mbuf
*control
, proc_t p
)
1896 struct cmsghdr
*cm
= mtod(control
, struct cmsghdr
*);
1898 struct fileglob
**rp
;
1899 struct fileproc
*fp
;
1903 /* 64bit: cmsg_len is 'uint32_t', m_len is 'long' */
1904 if (cm
->cmsg_type
!= SCM_RIGHTS
|| cm
->cmsg_level
!= SOL_SOCKET
||
1905 (socklen_t
)cm
->cmsg_len
!= (socklen_t
)control
->m_len
) {
1908 oldfds
= (cm
->cmsg_len
- sizeof (*cm
)) / sizeof (int);
1911 fds
= (int *)(cm
+ 1);
1913 for (i
= 0; i
< oldfds
; i
++) {
1914 struct fileproc
*tmpfp
;
1915 if (((error
= fdgetf_noref(p
, fds
[i
], &tmpfp
)) != 0)) {
1918 } else if (!filetype_issendable(tmpfp
->f_fglob
->fg_type
)) {
1923 rp
= (struct fileglob
**)(cm
+ 1);
1925 /* On K64 we need to walk backwards because a fileglob * is twice the size of an fd
1926 * and doing them in-order would result in stomping over unprocessed fd's
1928 for (i
= (oldfds
- 1); i
>= 0; i
--) {
1929 (void) fdgetf_noref(p
, fds
[i
], &fp
);
1930 fg_insertuipc(fp
->f_fglob
);
1931 rp
[i
] = fp
->f_fglob
;
1932 (void) OSAddAtomic(1, &unp_rights
);
1939 static int unp_defer
, unp_gcing
, unp_gcwait
;
1940 static thread_t unp_gcthread
= NULL
;
1942 /* always called under uipc_lock */
1946 if (unp_gcthread
== current_thread())
1949 while (unp_gcing
!= 0) {
1951 msleep(&unp_gcing
, uipc_lock
, 0 , "unp_gc_wait", NULL
);
1956 __private_extern__
void
1959 struct fileglob
*fg
, *nextfg
;
1961 static struct fileglob
**extra_ref
;
1962 struct fileglob
**fpp
;
1964 int need_gcwakeup
= 0;
1966 lck_mtx_lock(uipc_lock
);
1968 lck_mtx_unlock(uipc_lock
);
1973 unp_gcthread
= current_thread();
1974 lck_mtx_unlock(uipc_lock
);
1976 * before going through all this, set all FDs to
1977 * be NOT defered and NOT externally accessible
1979 for (fg
= fmsghead
.lh_first
; fg
!= 0; fg
= fg
->f_msglist
.le_next
) {
1980 lck_mtx_lock(&fg
->fg_lock
);
1981 fg
->fg_flag
&= ~(FMARK
|FDEFER
);
1982 lck_mtx_unlock(&fg
->fg_lock
);
1985 for (fg
= fmsghead
.lh_first
; fg
!= 0;
1986 fg
= fg
->f_msglist
.le_next
) {
1987 lck_mtx_lock(&fg
->fg_lock
);
1989 * If the file is not open, skip it
1991 if (fg
->fg_count
== 0) {
1992 lck_mtx_unlock(&fg
->fg_lock
);
1996 * If we already marked it as 'defer' in a
1997 * previous pass, then try process it this time
2000 if (fg
->fg_flag
& FDEFER
) {
2001 fg
->fg_flag
&= ~FDEFER
;
2005 * if it's not defered, then check if it's
2006 * already marked.. if so skip it
2008 if (fg
->fg_flag
& FMARK
) {
2009 lck_mtx_unlock(&fg
->fg_lock
);
2013 * If all references are from messages
2014 * in transit, then skip it. it's not
2015 * externally accessible.
2017 if (fg
->fg_count
== fg
->fg_msgcount
) {
2018 lck_mtx_unlock(&fg
->fg_lock
);
2022 * If it got this far then it must be
2023 * externally accessible.
2025 fg
->fg_flag
|= FMARK
;
2028 * either it was defered, or it is externally
2029 * accessible and not already marked so.
2030 * Now check if it is possibly one of OUR sockets.
2032 if (fg
->fg_type
!= DTYPE_SOCKET
||
2033 (so
= (struct socket
*)fg
->fg_data
) == 0) {
2034 lck_mtx_unlock(&fg
->fg_lock
);
2037 if (so
->so_proto
->pr_domain
!= &localdomain
||
2038 (so
->so_proto
->pr_flags
&PR_RIGHTS
) == 0) {
2039 lck_mtx_unlock(&fg
->fg_lock
);
2044 * if this code is enabled need to run
2045 * under network funnel
2047 if (so
->so_rcv
.sb_flags
& SB_LOCK
) {
2049 * This is problematical; it's not clear
2050 * we need to wait for the sockbuf to be
2051 * unlocked (on a uniprocessor, at least),
2052 * and it's also not clear what to do
2053 * if sbwait returns an error due to receipt
2054 * of a signal. If sbwait does return
2055 * an error, we'll go into an infinite
2056 * loop. Delete all of this for now.
2058 (void) sbwait(&so
->so_rcv
);
2063 * So, Ok, it's one of our sockets and it IS externally
2064 * accessible (or was defered). Now we look
2065 * to see if we hold any file descriptors in its
2066 * message buffers. Follow those links and mark them
2067 * as accessible too.
2069 * In case a file is passed onto itself we need to
2070 * release the file lock.
2072 lck_mtx_unlock(&fg
->fg_lock
);
2074 unp_scan(so
->so_rcv
.sb_mb
, unp_mark
);
2076 } while (unp_defer
);
2078 * We grab an extra reference to each of the file table entries
2079 * that are not otherwise accessible and then free the rights
2080 * that are stored in messages on them.
2082 * The bug in the orginal code is a little tricky, so I'll describe
2083 * what's wrong with it here.
2085 * It is incorrect to simply unp_discard each entry for f_msgcount
2086 * times -- consider the case of sockets A and B that contain
2087 * references to each other. On a last close of some other socket,
2088 * we trigger a gc since the number of outstanding rights (unp_rights)
2089 * is non-zero. If during the sweep phase the gc code un_discards,
2090 * we end up doing a (full) closef on the descriptor. A closef on A
2091 * results in the following chain. Closef calls soo_close, which
2092 * calls soclose. Soclose calls first (through the switch
2093 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply
2094 * returns because the previous instance had set unp_gcing, and
2095 * we return all the way back to soclose, which marks the socket
2096 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush
2097 * to free up the rights that are queued in messages on the socket A,
2098 * i.e., the reference on B. The sorflush calls via the dom_dispose
2099 * switch unp_dispose, which unp_scans with unp_discard. This second
2100 * instance of unp_discard just calls closef on B.
2102 * Well, a similar chain occurs on B, resulting in a sorflush on B,
2103 * which results in another closef on A. Unfortunately, A is already
2104 * being closed, and the descriptor has already been marked with
2105 * SS_NOFDREF, and soclose panics at this point.
2107 * Here, we first take an extra reference to each inaccessible
2108 * descriptor. Then, we call sorflush ourself, since we know
2109 * it is a Unix domain socket anyhow. After we destroy all the
2110 * rights carried in messages, we do a last closef to get rid
2111 * of our extra reference. This is the last close, and the
2112 * unp_detach etc will shut down the socket.
2114 * 91/09/19, bsy@cs.cmu.edu
2116 extra_ref
= _MALLOC(nfiles
* sizeof (struct fileglob
*),
2117 M_FILEGLOB
, M_WAITOK
);
2118 if (extra_ref
== NULL
)
2120 for (nunref
= 0, fg
= fmsghead
.lh_first
, fpp
= extra_ref
; fg
!= 0;
2122 lck_mtx_lock(&fg
->fg_lock
);
2124 nextfg
= fg
->f_msglist
.le_next
;
2126 * If it's not open, skip it
2128 if (fg
->fg_count
== 0) {
2129 lck_mtx_unlock(&fg
->fg_lock
);
2133 * If all refs are from msgs, and it's not marked accessible
2134 * then it must be referenced from some unreachable cycle
2135 * of (shut-down) FDs, so include it in our
2136 * list of FDs to remove
2138 if (fg
->fg_count
== fg
->fg_msgcount
&& !(fg
->fg_flag
& FMARK
)) {
2143 lck_mtx_unlock(&fg
->fg_lock
);
2146 * for each FD on our hit list, do the following two things
2148 for (i
= nunref
, fpp
= extra_ref
; --i
>= 0; ++fpp
) {
2149 struct fileglob
*tfg
;
2153 if (tfg
->fg_type
== DTYPE_SOCKET
&& tfg
->fg_data
!= NULL
) {
2154 so
= (struct socket
*)(tfg
->fg_data
);
2160 socket_unlock(so
, 0);
2163 for (i
= nunref
, fpp
= extra_ref
; --i
>= 0; ++fpp
)
2164 closef_locked((struct fileproc
*)0, *fpp
, (proc_t
)NULL
);
2166 FREE((caddr_t
)extra_ref
, M_FILEGLOB
);
2168 lck_mtx_lock(uipc_lock
);
2170 unp_gcthread
= NULL
;
2172 if (unp_gcwait
!= 0) {
2176 lck_mtx_unlock(uipc_lock
);
2178 if (need_gcwakeup
!= 0)
2183 unp_dispose(struct mbuf
*m
)
2186 unp_scan(m
, unp_discard
);
2191 * Returns: 0 Success
2194 unp_listen(struct unpcb
*unp
, proc_t p
)
2196 kauth_cred_t safecred
= kauth_cred_proc_ref(p
);
2197 cru2x(safecred
, &unp
->unp_peercred
);
2198 kauth_cred_unref(&safecred
);
2199 unp
->unp_flags
|= UNP_HAVEPCCACHED
;
2203 /* should run under kernel funnel */
2205 unp_scan(struct mbuf
*m0
, void (*op
)(struct fileglob
*))
2208 struct fileglob
**rp
;
2214 for (m
= m0
; m
; m
= m
->m_next
)
2215 if (m
->m_type
== MT_CONTROL
&&
2216 (size_t)m
->m_len
>= sizeof (*cm
)) {
2217 cm
= mtod(m
, struct cmsghdr
*);
2218 if (cm
->cmsg_level
!= SOL_SOCKET
||
2219 cm
->cmsg_type
!= SCM_RIGHTS
)
2221 qfds
= (cm
->cmsg_len
- sizeof (*cm
)) /
2223 rp
= (struct fileglob
**)(cm
+ 1);
2224 for (i
= 0; i
< qfds
; i
++)
2226 break; /* XXX, but saves time */
2232 /* should run under kernel funnel */
2234 unp_mark(struct fileglob
*fg
)
2236 lck_mtx_lock(&fg
->fg_lock
);
2238 if (fg
->fg_flag
& FMARK
) {
2239 lck_mtx_unlock(&fg
->fg_lock
);
2242 fg
->fg_flag
|= (FMARK
|FDEFER
);
2244 lck_mtx_unlock(&fg
->fg_lock
);
2249 /* should run under kernel funnel */
2251 unp_discard(struct fileglob
*fg
)
2253 proc_t p
= current_proc(); /* XXX */
2255 (void) OSAddAtomic(1, &unp_disposed
);
2258 unp_discard_fdlocked(fg
, p
);
2262 unp_discard_fdlocked(struct fileglob
*fg
, proc_t p
)
2266 (void) OSAddAtomic(-1, &unp_rights
);
2267 (void) closef_locked((struct fileproc
*)0, fg
, p
);
2271 unp_lock(struct socket
*so
, int refcount
, void * lr
)
2275 lr_saved
= (void *) __builtin_return_address(0);
2279 lck_mtx_lock(&((struct unpcb
*)so
->so_pcb
)->unp_mtx
);
2281 panic("unp_lock: so=%p NO PCB! lr=%p ref=0x%x\n",
2282 so
, lr_saved
, so
->so_usecount
);
2285 if (so
->so_usecount
< 0)
2286 panic("unp_lock: so=%p so_pcb=%p lr=%p ref=0x%x\n",
2287 so
, so
->so_pcb
, lr_saved
, so
->so_usecount
);
2292 so
->lock_lr
[so
->next_lock_lr
] = lr_saved
;
2293 so
->next_lock_lr
= (so
->next_lock_lr
+1) % SO_LCKDBG_MAX
;
2298 unp_unlock(struct socket
*so
, int refcount
, void * lr
)
2301 lck_mtx_t
* mutex_held
= NULL
;
2302 struct unpcb
*unp
= sotounpcb(so
);
2305 lr_saved
= (void *) __builtin_return_address(0);
2311 if (so
->so_usecount
< 0)
2312 panic("unp_unlock: so=%p usecount=%x\n", so
, so
->so_usecount
);
2313 if (so
->so_pcb
== NULL
) {
2314 panic("unp_unlock: so=%p NO PCB usecount=%x\n", so
, so
->so_usecount
);
2316 mutex_held
= &((struct unpcb
*)so
->so_pcb
)->unp_mtx
;
2318 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
2319 so
->unlock_lr
[so
->next_unlock_lr
] = lr_saved
;
2320 so
->next_unlock_lr
= (so
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
2322 if (so
->so_usecount
== 0 && (so
->so_flags
& SOF_PCBCLEARING
)) {
2323 sofreelastref(so
, 1);
2326 FREE(unp
->unp_addr
, M_SONAME
);
2328 lck_mtx_unlock(mutex_held
);
2330 unp
->unp_gencnt
= ++unp_gencnt
;
2331 zfree(unp_zone
, unp
);
2336 lck_mtx_unlock(mutex_held
);
2343 unp_getlock(struct socket
*so
, __unused
int locktype
)
2345 struct unpcb
*unp
= (struct unpcb
*)so
->so_pcb
;
2349 if (so
->so_usecount
< 0)
2350 panic("unp_getlock: so=%p usecount=%x\n", so
, so
->so_usecount
);
2351 return(&unp
->unp_mtx
);
2353 panic("unp_getlock: so=%p NULL so_pcb\n", so
);
2354 return (so
->so_proto
->pr_domain
->dom_mtx
);