2 * Copyright (c) 2000-2008 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 * Unix communications domain.
125 * rethink name space problems
126 * need a proper out-of-band
129 static struct sockaddr sun_noname
= { sizeof (sun_noname
), AF_LOCAL
, { 0 } };
130 static ino_t unp_ino
; /* prototype for fake inode numbers */
132 static int unp_attach(struct socket
*);
133 static void unp_detach(struct unpcb
*);
134 static int unp_bind(struct unpcb
*, struct sockaddr
*, proc_t
);
135 static int unp_connect(struct socket
*, struct sockaddr
*, proc_t
);
136 static void unp_disconnect(struct unpcb
*);
137 static void unp_shutdown(struct unpcb
*);
138 static void unp_drop(struct unpcb
*, int);
139 __private_extern__
void unp_gc(void);
140 static void unp_scan(struct mbuf
*, void (*)(struct fileglob
*));
141 static void unp_mark(struct fileglob
*);
142 static void unp_discard(struct fileglob
*);
143 static void unp_discard_fdlocked(struct fileglob
*, proc_t
);
144 static int unp_internalize(struct mbuf
*, proc_t
);
145 static int unp_listen(struct unpcb
*, proc_t
);
146 static void unpcb_to_compat(struct unpcb
*, struct unpcb_compat
*);
147 static void unp_get_locks_in_order(struct socket
*so
, struct socket
*conn_so
);
150 unp_get_locks_in_order(struct socket
*so
, struct socket
*conn_so
)
153 socket_lock(conn_so
, 1);
155 struct unpcb
*unp
= sotounpcb(so
);
156 unp
->unp_flags
|= UNP_DONTDISCONNECT
;
158 socket_unlock(so
, 0);
160 /* Get the locks in the correct order */
161 socket_lock(conn_so
, 1);
164 if (unp
->rw_thrcount
== 0) {
165 unp
->unp_flags
&= ~UNP_DONTDISCONNECT
;
172 uipc_abort(struct socket
*so
)
174 struct unpcb
*unp
= sotounpcb(so
);
178 unp_drop(unp
, ECONNABORTED
);
185 uipc_accept(struct socket
*so
, struct sockaddr
**nam
)
187 struct unpcb
*unp
= sotounpcb(so
);
193 * Pass back name of connected socket,
194 * if it was bound and we are still connected
195 * (our peer may have closed already!).
197 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
) {
198 *nam
= dup_sockaddr((struct sockaddr
*)
199 unp
->unp_conn
->unp_addr
, 1);
201 *nam
= dup_sockaddr((struct sockaddr
*)&sun_noname
, 1);
212 uipc_attach(struct socket
*so
, __unused
int proto
, __unused proc_t p
)
214 struct unpcb
*unp
= sotounpcb(so
);
218 return (unp_attach(so
));
222 uipc_bind(struct socket
*so
, struct sockaddr
*nam
, proc_t p
)
224 struct unpcb
*unp
= sotounpcb(so
);
229 return (unp_bind(unp
, nam
, p
));
235 * unp_connect:??? [See elsewhere in this file]
238 uipc_connect(struct socket
*so
, struct sockaddr
*nam
, proc_t p
)
240 struct unpcb
*unp
= sotounpcb(so
);
244 return (unp_connect(so
, nam
, p
));
250 * unp_connect2:EPROTOTYPE Protocol wrong type for socket
251 * unp_connect2:EINVAL Invalid argument
254 uipc_connect2(struct socket
*so1
, struct socket
*so2
)
256 struct unpcb
*unp
= sotounpcb(so1
);
261 return (unp_connect2(so1
, so2
));
264 /* control is EOPNOTSUPP */
267 uipc_detach(struct socket
*so
)
269 struct unpcb
*unp
= sotounpcb(so
);
274 lck_mtx_assert(unp
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
280 uipc_disconnect(struct socket
*so
)
282 struct unpcb
*unp
= sotounpcb(so
);
295 uipc_listen(struct socket
*so
, __unused proc_t p
)
297 struct unpcb
*unp
= sotounpcb(so
);
299 if (unp
== 0 || unp
->unp_vnode
== 0)
301 return (unp_listen(unp
, p
));
305 uipc_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
307 struct unpcb
*unp
= sotounpcb(so
);
311 if (unp
->unp_conn
!= NULL
&& unp
->unp_conn
->unp_addr
!= NULL
) {
312 *nam
= dup_sockaddr((struct sockaddr
*)
313 unp
->unp_conn
->unp_addr
, 1);
315 *nam
= dup_sockaddr((struct sockaddr
*)&sun_noname
, 1);
321 uipc_rcvd(struct socket
*so
, __unused
int flags
)
323 struct unpcb
*unp
= sotounpcb(so
);
328 switch (so
->so_type
) {
330 panic("uipc_rcvd DGRAM?");
334 #define rcv (&so->so_rcv)
335 #define snd (&so2->so_snd)
336 if (unp
->unp_conn
== 0)
339 so2
= unp
->unp_conn
->unp_socket
;
340 unp_get_locks_in_order(so
, so2
);
342 * Adjust backpressure on sender
343 * and wakeup any waiting to write.
345 snd
->sb_mbmax
+= unp
->unp_mbcnt
- rcv
->sb_mbcnt
;
346 unp
->unp_mbcnt
= rcv
->sb_mbcnt
;
347 snd
->sb_hiwat
+= unp
->unp_cc
- rcv
->sb_cc
;
348 unp
->unp_cc
= rcv
->sb_cc
;
351 socket_unlock(so2
, 1);
358 panic("uipc_rcvd unknown socktype");
363 /* pru_rcvoob is EOPNOTSUPP */
372 * unp_internalize:EINVAL
373 * unp_internalize:EBADF
374 * unp_connect:EAFNOSUPPORT Address family not supported
375 * unp_connect:EINVAL Invalid argument
376 * unp_connect:ENOTSOCK Not a socket
377 * unp_connect:ECONNREFUSED Connection refused
378 * unp_connect:EISCONN Socket is connected
379 * unp_connect:EPROTOTYPE Protocol wrong type for socket
381 * sbappendaddr:ENOBUFS [5th argument, contents modified]
382 * sbappendaddr:??? [whatever a filter author chooses]
385 uipc_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
386 struct mbuf
*control
, proc_t p
)
389 struct unpcb
*unp
= sotounpcb(so
);
396 if (flags
& PRUS_OOB
) {
402 /* release lock to avoid deadlock (4436174) */
403 socket_unlock(so
, 0);
404 error
= unp_internalize(control
, p
);
410 switch (so
->so_type
) {
413 struct sockaddr
*from
;
420 error
= unp_connect(so
, nam
, p
);
424 if (unp
->unp_conn
== 0) {
430 so2
= unp
->unp_conn
->unp_socket
;
431 unp_get_locks_in_order(so
, so2
);
434 from
= (struct sockaddr
*)unp
->unp_addr
;
438 * sbappendaddr() will fail when the receiver runs out of
439 * space; in contrast to SOCK_STREAM, we will lose messages
440 * for the SOCK_DGRAM case when the receiver's queue overflows.
441 * SB_UNIX on the socket buffer implies that the callee will
442 * not free the control message, if any, because we would need
443 * to call unp_dispose() on it.
445 if (sbappendaddr(&so2
->so_rcv
, from
, m
, control
, &error
)) {
448 } else if (control
!= NULL
&& error
== 0) {
449 /* A socket filter took control; don't touch it */
453 socket_unlock(so2
, 1);
463 #define rcv (&so2->so_rcv)
464 #define snd (&so->so_snd)
465 /* Connect if not connected yet. */
467 * Note: A better implementation would complain
468 * if not equal to the peer's address.
470 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
472 error
= unp_connect(so
, nam
, p
);
481 if (so
->so_state
& SS_CANTSENDMORE
) {
485 if (unp
->unp_conn
== 0)
486 panic("uipc_send connected but no connection?");
488 so2
= unp
->unp_conn
->unp_socket
;
489 unp_get_locks_in_order(so
, so2
);
491 /* Check socket state again as we might have unlocked the socket
492 * while trying to get the locks in order
495 if ((so
->so_state
& SS_CANTSENDMORE
)) {
497 socket_unlock(so2
, 1);
502 * Send to paired receive port, and then reduce send buffer
503 * hiwater marks to maintain backpressure. Wake up readers.
504 * SB_UNIX flag will allow new record to be appended to the
505 * receiver's queue even when it is already full. It is
506 * possible, however, that append might fail. In that case,
507 * we will need to call unp_dispose() on the control message;
508 * the callee will not free it since SB_UNIX is set.
510 didreceive
= control
?
511 sbappendcontrol(rcv
, m
, control
, &error
) : sbappend(rcv
, m
);
513 snd
->sb_mbmax
-= rcv
->sb_mbcnt
- unp
->unp_conn
->unp_mbcnt
;
514 unp
->unp_conn
->unp_mbcnt
= rcv
->sb_mbcnt
;
515 snd
->sb_hiwat
-= rcv
->sb_cc
- unp
->unp_conn
->unp_cc
;
516 unp
->unp_conn
->unp_cc
= rcv
->sb_cc
;
520 } else if (control
!= NULL
&& error
== 0) {
521 /* A socket filter took control; don't touch it */
525 socket_unlock(so2
, 1);
533 panic("uipc_send unknown socktype");
537 * SEND_EOF is equivalent to a SEND followed by
540 if (flags
& PRUS_EOF
) {
545 if (control
&& error
!= 0) {
546 socket_unlock(so
, 0);
547 unp_dispose(control
);
560 uipc_sense(struct socket
*so
, void *ub
, int isstat64
)
562 struct unpcb
*unp
= sotounpcb(so
);
569 blksize
= so
->so_snd
.sb_hiwat
;
570 if (so
->so_type
== SOCK_STREAM
&& unp
->unp_conn
!= 0) {
571 so2
= unp
->unp_conn
->unp_socket
;
572 blksize
+= so2
->so_rcv
.sb_cc
;
574 if (unp
->unp_ino
== 0)
575 unp
->unp_ino
= unp_ino
++;
580 sb64
= (struct stat64
*)ub
;
581 sb64
->st_blksize
= blksize
;
582 sb64
->st_dev
= NODEV
;
583 sb64
->st_ino
= (ino64_t
)unp
->unp_ino
;
587 sb
= (struct stat
*)ub
;
588 sb
->st_blksize
= blksize
;
590 sb
->st_ino
= (ino_t
)(uintptr_t)unp
->unp_ino
;
600 * Notes: This is not strictly correct, as unp_shutdown() also calls
601 * socantrcvmore(). These should maybe both be conditionalized
602 * on the 'how' argument in soshutdown() as called from the
603 * shutdown() system call.
606 uipc_shutdown(struct socket
*so
)
608 struct unpcb
*unp
= sotounpcb(so
);
619 * EINVAL Invalid argument
622 uipc_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
624 struct unpcb
*unp
= sotounpcb(so
);
628 if (unp
->unp_addr
!= NULL
) {
629 *nam
= dup_sockaddr((struct sockaddr
*)unp
->unp_addr
, 1);
631 *nam
= dup_sockaddr((struct sockaddr
*)&sun_noname
, 1);
636 struct pr_usrreqs uipc_usrreqs
= {
637 uipc_abort
, uipc_accept
, uipc_attach
, uipc_bind
, uipc_connect
,
638 uipc_connect2
, pru_control_notsupp
, uipc_detach
, uipc_disconnect
,
639 uipc_listen
, uipc_peeraddr
, uipc_rcvd
, pru_rcvoob_notsupp
,
640 uipc_send
, uipc_sense
, uipc_shutdown
, uipc_sockaddr
,
641 sosend
, soreceive
, pru_sopoll_notsupp
645 uipc_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
647 struct unpcb
*unp
= sotounpcb(so
);
650 switch (sopt
->sopt_dir
) {
652 switch (sopt
->sopt_name
) {
654 if (unp
->unp_flags
& UNP_HAVEPC
) {
655 error
= sooptcopyout(sopt
, &unp
->unp_peercred
,
656 sizeof (unp
->unp_peercred
));
658 if (so
->so_type
== SOCK_STREAM
)
678 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
679 * for stream sockets, although the total for sender and receiver is
680 * actually only PIPSIZ.
681 * Datagram sockets really use the sendspace as the maximum datagram size,
682 * and don't really want to reserve the sendspace. Their recvspace should
683 * be large enough for at least one max-size datagram plus address.
688 static u_int32_t unpst_sendspace
= PIPSIZ
;
689 static u_int32_t unpst_recvspace
= PIPSIZ
;
690 static u_int32_t unpdg_sendspace
= 2*1024; /* really max datagram size */
691 static u_int32_t unpdg_recvspace
= 4*1024;
693 static int unp_rights
; /* file descriptors in flight */
694 static int unp_disposed
; /* discarded file descriptors */
696 SYSCTL_DECL(_net_local_stream
);
697 SYSCTL_INT(_net_local_stream
, OID_AUTO
, sendspace
, CTLFLAG_RW
,
698 &unpst_sendspace
, 0, "");
699 SYSCTL_INT(_net_local_stream
, OID_AUTO
, recvspace
, CTLFLAG_RW
,
700 &unpst_recvspace
, 0, "");
701 SYSCTL_DECL(_net_local_dgram
);
702 SYSCTL_INT(_net_local_dgram
, OID_AUTO
, maxdgram
, CTLFLAG_RW
,
703 &unpdg_sendspace
, 0, "");
704 SYSCTL_INT(_net_local_dgram
, OID_AUTO
, recvspace
, CTLFLAG_RW
,
705 &unpdg_recvspace
, 0, "");
706 SYSCTL_DECL(_net_local
);
707 SYSCTL_INT(_net_local
, OID_AUTO
, inflight
, CTLFLAG_RD
, &unp_rights
, 0, "");
715 unp_attach(struct socket
*so
)
720 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
721 switch (so
->so_type
) {
724 error
= soreserve(so
, unpst_sendspace
, unpst_recvspace
);
728 error
= soreserve(so
, unpdg_sendspace
, unpdg_recvspace
);
737 unp
= (struct unpcb
*)zalloc(unp_zone
);
740 bzero(unp
, sizeof (*unp
));
742 unp
->unp_mtx
= lck_mtx_alloc_init(unp_mtx_grp
, unp_mtx_attr
);
743 if (unp
->unp_mtx
== NULL
) {
744 zfree(unp_zone
, unp
);
748 lck_rw_lock_exclusive(unp_list_mtx
);
749 LIST_INIT(&unp
->unp_refs
);
750 unp
->unp_socket
= so
;
751 unp
->unp_gencnt
= ++unp_gencnt
;
753 LIST_INSERT_HEAD(so
->so_type
== SOCK_DGRAM
?
754 &unp_dhead
: &unp_shead
, unp
, unp_link
);
755 lck_rw_done(unp_list_mtx
);
756 so
->so_pcb
= (caddr_t
)unp
;
758 * Mark AF_UNIX socket buffers accordingly so that:
760 * a. In the SOCK_STREAM case, socket buffer append won't fail due to
761 * the lack of space; this essentially loosens the sbspace() check,
762 * since there is disconnect between sosend() and uipc_send() with
763 * respect to flow control that might result in our dropping the
764 * data in uipc_send(). By setting this, we allow for slightly
765 * more records to be appended to the receiving socket to avoid
766 * losing data (which we can't afford in the SOCK_STREAM case).
767 * Flow control still takes place since we adjust the sender's
768 * hiwat during each send. This doesn't affect the SOCK_DGRAM
769 * case and append would still fail when the queue overflows.
771 * b. In the presence of control messages containing internalized
772 * file descriptors, the append routines will not free them since
773 * we'd need to undo the work first via unp_dispose().
775 so
->so_rcv
.sb_flags
|= SB_UNIX
;
776 so
->so_snd
.sb_flags
|= SB_UNIX
;
781 unp_detach(struct unpcb
*unp
)
783 lck_rw_lock_exclusive(unp_list_mtx
);
784 LIST_REMOVE(unp
, unp_link
);
785 lck_rw_done(unp_list_mtx
);
786 if (unp
->unp_vnode
) {
787 struct vnode
*tvp
= NULL
;
788 socket_unlock(unp
->unp_socket
, 0);
790 /* Holding unp_connect_lock will avoid a race between
791 * a thread closing the listening socket and a thread
794 lck_mtx_lock(unp_connect_lock
);
795 socket_lock(unp
->unp_socket
, 0);
796 if (unp
->unp_vnode
) {
797 tvp
= unp
->unp_vnode
;
798 unp
->unp_vnode
->v_socket
= NULL
;
799 unp
->unp_vnode
= NULL
;
801 lck_mtx_unlock(unp_connect_lock
);
803 vnode_rele(tvp
); /* drop the usecount */
807 while (unp
->unp_refs
.lh_first
) {
808 struct unpcb
*unp2
= unp
->unp_refs
.lh_first
;
809 socket_unlock(unp
->unp_socket
, 0);
811 socket_lock(unp2
->unp_socket
, 1);
812 unp_drop(unp2
, ECONNRESET
);
813 socket_unlock(unp2
->unp_socket
, 1);
814 socket_lock(unp
->unp_socket
, 0);
816 soisdisconnected(unp
->unp_socket
);
817 /* makes sure we're getting dealloced */
818 unp
->unp_socket
->so_flags
|= SOF_PCBCLEARING
;
826 * namei:??? [anything namei can return]
827 * vnode_authorize:??? [anything vnode_authorize can return]
829 * Notes: p at this point is the current process, as this function is
830 * only called by sobind().
835 struct sockaddr
*nam
,
838 struct sockaddr_un
*soun
= (struct sockaddr_un
*)nam
;
839 struct vnode
*vp
, *dvp
;
840 struct vnode_attr va
;
841 vfs_context_t ctx
= vfs_context_current();
844 struct socket
*so
= unp
->unp_socket
;
845 char buf
[SOCK_MAXADDRLEN
];
847 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_UNIX
) {
848 return (EAFNOSUPPORT
);
851 if (unp
->unp_vnode
!= NULL
)
853 namelen
= soun
->sun_len
- offsetof(struct sockaddr_un
, sun_path
);
857 socket_unlock(so
, 0);
859 strlcpy(buf
, soun
->sun_path
, namelen
+1);
860 NDINIT(&nd
, CREATE
, FOLLOW
| LOCKPARENT
, UIO_SYSSPACE
,
861 CAST_USER_ADDR_T(buf
), ctx
);
862 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
873 * need to do this before the vnode_put of dvp
874 * since we may have to release an fs_nodelock
886 VATTR_SET(&va
, va_type
, VSOCK
);
887 VATTR_SET(&va
, va_mode
, (ACCESSPERMS
& ~p
->p_fd
->fd_cmask
));
890 error
= mac_vnode_check_create(ctx
,
891 nd
.ni_dvp
, &nd
.ni_cnd
, &va
);
894 #endif /* CONFIG_MACF */
895 #if CONFIG_MACF_SOCKET_SUBSET
896 error
= mac_vnode_check_uipc_bind(ctx
,
897 nd
.ni_dvp
, &nd
.ni_cnd
, &va
);
900 #endif /* MAC_SOCKET_SUBSET */
901 /* authorize before creating */
902 error
= vnode_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, ctx
);
905 /* create the socket */
906 error
= vn_create(dvp
, &vp
, &nd
.ni_cnd
, &va
, 0, ctx
);
916 vnode_ref(vp
); /* gain a longterm reference */
918 vp
->v_socket
= unp
->unp_socket
;
920 unp
->unp_addr
= (struct sockaddr_un
*)dup_sockaddr(nam
, 1);
921 vnode_put(vp
); /* drop the iocount */
929 * EAFNOSUPPORT Address family not supported
930 * EINVAL Invalid argument
931 * ENOTSOCK Not a socket
932 * ECONNREFUSED Connection refused
933 * EPROTOTYPE Protocol wrong type for socket
934 * EISCONN Socket is connected
935 * unp_connect2:EPROTOTYPE Protocol wrong type for socket
936 * unp_connect2:EINVAL Invalid argument
937 * namei:??? [anything namei can return]
938 * vnode_authorize:???? [anything vnode_authorize can return]
940 * Notes: p at this point is the current process, as this function is
941 * only called by sosend(), sendfile(), and soconnectlock().
944 unp_connect(struct socket
*so
, struct sockaddr
*nam
, __unused proc_t p
)
946 struct sockaddr_un
*soun
= (struct sockaddr_un
*)nam
;
948 struct socket
*so2
, *so3
, *list_so
=NULL
;
949 struct unpcb
*unp
, *unp2
, *unp3
;
950 vfs_context_t ctx
= vfs_context_current();
953 char buf
[SOCK_MAXADDRLEN
];
955 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_UNIX
) {
956 return (EAFNOSUPPORT
);
962 len
= nam
->sa_len
- offsetof(struct sockaddr_un
, sun_path
);
966 strlcpy(buf
, soun
->sun_path
, len
+1);
967 socket_unlock(so
, 0);
969 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
,
970 CAST_USER_ADDR_T(buf
), ctx
);
978 if (vp
->v_type
!= VSOCK
) {
984 #if CONFIG_MACF_SOCKET_SUBSET
985 error
= mac_vnode_check_uipc_connect(ctx
, vp
);
990 #endif /* MAC_SOCKET_SUBSET */
992 error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_WRITE_DATA
, ctx
);
998 lck_mtx_lock(unp_connect_lock
);
1000 if (vp
->v_socket
== 0) {
1001 lck_mtx_unlock(unp_connect_lock
);
1002 error
= ECONNREFUSED
;
1007 socket_lock(vp
->v_socket
, 1); /* Get a reference on the listening socket */
1009 lck_mtx_unlock(unp_connect_lock
);
1012 if (so2
->so_pcb
== NULL
) {
1013 error
= ECONNREFUSED
;
1014 socket_unlock(so2
, 1);
1020 socket_unlock(so2
, 0);
1022 socket_lock(so2
, 0);
1027 * Check if socket was connected while we were trying to
1028 * get the socket locks in order.
1029 * XXX - probably shouldn't return an error for SOCK_DGRAM
1031 if ((so
->so_state
& SS_ISCONNECTED
) != 0) {
1032 socket_unlock(so2
, 1);
1037 if (so
->so_type
!= so2
->so_type
) {
1038 socket_unlock(so2
, 1);
1043 if (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) {
1044 /* Release the incoming socket but keep a reference */
1045 socket_unlock(so
, 0);
1047 if ((so2
->so_options
& SO_ACCEPTCONN
) == 0 ||
1048 (so3
= sonewconn(so2
, 0, nam
)) == 0) {
1049 error
= ECONNREFUSED
;
1050 socket_unlock(so2
, 1);
1054 unp2
= sotounpcb(so2
);
1055 unp3
= sotounpcb(so3
);
1057 unp3
->unp_addr
= (struct sockaddr_un
*)
1058 dup_sockaddr((struct sockaddr
*)unp2
->unp_addr
, 1);
1061 * unp_peercred management:
1063 * The connecter's (client's) credentials are copied
1064 * from its process structure at the time of connect()
1067 cru2x(vfs_context_ucred(ctx
), &unp3
->unp_peercred
);
1068 unp3
->unp_flags
|= UNP_HAVEPC
;
1070 * The receiver's (server's) credentials are copied
1071 * from the unp_peercred member of socket on which the
1072 * former called listen(); unp_listen() cached that
1073 * process's credentials at that time so we can use
1076 KASSERT(unp2
->unp_flags
& UNP_HAVEPCCACHED
,
1077 ("unp_connect: listener without cached peercred"));
1079 /* Here we need to have both so and so2 locks and so2
1080 * is already locked. Lock ordering is required.
1083 socket_unlock(so2
, 0);
1085 socket_lock(so2
, 0);
1090 /* Check again if the socket state changed when its lock was released */
1091 if ((so
->so_state
& SS_ISCONNECTED
) != 0) {
1093 socket_unlock(so2
, 1);
1094 socket_lock(so3
, 0);
1095 sofreelastref(so3
, 1);
1098 memcpy(&unp
->unp_peercred
, &unp2
->unp_peercred
,
1099 sizeof (unp
->unp_peercred
));
1100 unp
->unp_flags
|= UNP_HAVEPC
;
1102 #if CONFIG_MACF_SOCKET
1103 /* XXXMAC: recursive lock: SOCK_LOCK(so); */
1104 mac_socketpeer_label_associate_socket(so
, so3
);
1105 mac_socketpeer_label_associate_socket(so3
, so
);
1106 /* XXXMAC: SOCK_UNLOCK(so); */
1107 #endif /* MAC_SOCKET */
1109 /* Hold the reference on listening socket until the end */
1110 socket_unlock(so2
, 0);
1113 /* Lock ordering doesn't matter because so3 was just created */
1114 socket_lock(so3
, 1);
1119 error
= unp_connect2(so
, so2
);
1121 socket_unlock(so2
, 1);
1124 if (list_so
!= NULL
) {
1125 socket_lock(list_so
, 0);
1126 socket_unlock(list_so
, 1);
1129 lck_mtx_assert(unp
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
1135 * Returns: 0 Success
1136 * EPROTOTYPE Protocol wrong type for socket
1137 * EINVAL Invalid argument
1140 unp_connect2(struct socket
*so
, struct socket
*so2
)
1142 struct unpcb
*unp
= sotounpcb(so
);
1145 if (so2
->so_type
!= so
->so_type
)
1146 return (EPROTOTYPE
);
1148 unp2
= sotounpcb(so2
);
1150 lck_mtx_assert(unp
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
1151 lck_mtx_assert(unp2
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
1153 /* Verify both sockets are still opened */
1154 if (unp
== 0 || unp2
== 0)
1157 unp
->unp_conn
= unp2
;
1160 switch (so
->so_type
) {
1163 lck_rw_lock_exclusive(unp_list_mtx
);
1164 LIST_INSERT_HEAD(&unp2
->unp_refs
, unp
, unp_reflink
);
1165 lck_rw_done(unp_list_mtx
);
1168 /* Avoid lock order reversals due to drop/acquire in soisconnected. */
1169 /* Keep an extra reference on so2 that will be dropped
1170 * soon after getting the locks in order
1172 socket_unlock(so2
, 0);
1174 unp_get_locks_in_order(so
, so2
);
1180 /* This takes care of socketpair */
1181 if (!(unp
->unp_flags
& UNP_HAVEPC
) &&
1182 !(unp2
->unp_flags
& UNP_HAVEPC
)) {
1183 cru2x(kauth_cred_get(), &unp
->unp_peercred
);
1184 unp
->unp_flags
|= UNP_HAVEPC
;
1186 cru2x(kauth_cred_get(), &unp2
->unp_peercred
);
1187 unp2
->unp_flags
|= UNP_HAVEPC
;
1189 unp2
->unp_conn
= unp
;
1192 /* Avoid lock order reversals due to drop/acquire in soisconnected. */
1193 socket_unlock(so
, 0);
1196 /* Keep an extra reference on so2, that will be dropped soon after
1197 * getting the locks in order again.
1199 socket_unlock(so2
, 0);
1204 unp_get_locks_in_order(so
, so2
);
1205 /* Decrement the extra reference left before */
1210 panic("unknown socket type %d in unp_connect2", so
->so_type
);
1212 lck_mtx_assert(unp
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
1213 lck_mtx_assert(unp2
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
1218 unp_disconnect(struct unpcb
*unp
)
1220 struct unpcb
*unp2
= NULL
;
1221 struct socket
*so2
= NULL
, *so
;
1222 struct socket
*waitso
;
1223 int so_locked
= 1, strdisconn
= 0;
1225 so
= unp
->unp_socket
;
1226 if (unp
->unp_conn
== NULL
) {
1229 lck_mtx_lock(unp_disconnect_lock
);
1230 while (disconnect_in_progress
!= 0) {
1231 if (so_locked
== 1) {
1232 socket_unlock(so
, 0);
1235 (void)msleep((caddr_t
)&disconnect_in_progress
, unp_disconnect_lock
,
1236 PSOCK
, "disconnect", NULL
);
1238 disconnect_in_progress
= 1;
1239 lck_mtx_unlock(unp_disconnect_lock
);
1241 if (so_locked
== 0) {
1246 unp2
= unp
->unp_conn
;
1248 if (unp2
== 0 || unp2
->unp_socket
== NULL
) {
1251 so2
= unp2
->unp_socket
;
1255 if (so_locked
== 0) {
1258 socket_lock(so2
, 1);
1261 if (so_locked
== 1) {
1262 socket_unlock(so
, 0);
1264 socket_lock(so2
, 1);
1269 lck_mtx_assert(unp
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
1270 lck_mtx_assert(unp2
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
1272 /* Check for the UNP_DONTDISCONNECT flag, if it
1273 * is set, release both sockets and go to sleep
1276 if ((((struct unpcb
*)waitso
->so_pcb
)->unp_flags
& UNP_DONTDISCONNECT
) != 0) {
1277 socket_unlock(so2
, 1);
1280 (void)msleep(waitso
->so_pcb
, unp
->unp_mtx
,
1281 PSOCK
| PDROP
, "unpdisconnect", NULL
);
1285 if (unp
->unp_conn
== NULL
) {
1286 panic("unp_conn became NULL after sleep");
1289 unp
->unp_conn
= NULL
;
1292 switch (unp
->unp_socket
->so_type
) {
1295 lck_rw_lock_exclusive(unp_list_mtx
);
1296 LIST_REMOVE(unp
, unp_reflink
);
1297 lck_rw_done(unp_list_mtx
);
1298 unp
->unp_socket
->so_state
&= ~SS_ISCONNECTED
;
1299 socket_unlock(so2
, 1);
1303 unp2
->unp_conn
= NULL
;
1306 /* Set the socket state correctly but do a wakeup later when
1307 * we release all locks except the socket lock, this will avoid
1310 unp
->unp_socket
->so_state
&= ~(SS_ISCONNECTING
|SS_ISCONNECTED
|SS_ISDISCONNECTING
);
1311 unp
->unp_socket
->so_state
|= (SS_CANTRCVMORE
|SS_CANTSENDMORE
|SS_ISDISCONNECTED
);
1313 unp2
->unp_socket
->so_state
&= ~(SS_ISCONNECTING
|SS_ISCONNECTED
|SS_ISDISCONNECTING
);
1314 unp
->unp_socket
->so_state
|= (SS_CANTRCVMORE
|SS_CANTSENDMORE
|SS_ISDISCONNECTED
);
1318 panic("unknown socket type %d", so
->so_type
);
1321 lck_mtx_lock(unp_disconnect_lock
);
1322 disconnect_in_progress
= 0;
1323 wakeup(&disconnect_in_progress
);
1324 lck_mtx_unlock(unp_disconnect_lock
);
1327 socket_unlock(so
, 0);
1328 soisdisconnected(so2
);
1329 socket_unlock(so2
, 1);
1332 soisdisconnected(so
);
1334 lck_mtx_assert(unp
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
1339 * unpcb_to_compat copies specific bits of a unpcb to a unpcb_compat format.
1340 * The unpcb_compat data structure is passed to user space and must not change.
1343 unpcb_to_compat(struct unpcb
*up
, struct unpcb_compat
*cp
)
1345 #if defined(__LP64__)
1346 cp
->unp_link
.le_next
= (u_int32_t
)(uintptr_t)up
->unp_link
.le_next
;
1347 cp
->unp_link
.le_prev
= (u_int32_t
)(uintptr_t)up
->unp_link
.le_prev
;
1349 cp
->unp_link
.le_next
= (struct unpcb_compat
*)up
->unp_link
.le_next
;
1350 cp
->unp_link
.le_prev
= (struct unpcb_compat
**)up
->unp_link
.le_prev
;
1352 cp
->unp_socket
= (_UNPCB_PTR(struct socket
*))(uintptr_t)up
->unp_socket
;
1353 cp
->unp_vnode
= (_UNPCB_PTR(struct vnode
*))(uintptr_t)up
->unp_vnode
;
1354 cp
->unp_ino
= up
->unp_ino
;
1355 cp
->unp_conn
= (_UNPCB_PTR(struct unpcb_compat
*))
1356 (uintptr_t)up
->unp_conn
;
1357 cp
->unp_refs
= (u_int32_t
)(uintptr_t)up
->unp_refs
.lh_first
;
1358 #if defined(__LP64__)
1359 cp
->unp_reflink
.le_next
=
1360 (u_int32_t
)(uintptr_t)up
->unp_reflink
.le_next
;
1361 cp
->unp_reflink
.le_prev
=
1362 (u_int32_t
)(uintptr_t)up
->unp_reflink
.le_prev
;
1364 cp
->unp_reflink
.le_next
=
1365 (struct unpcb_compat
*)up
->unp_reflink
.le_next
;
1366 cp
->unp_reflink
.le_prev
=
1367 (struct unpcb_compat
**)up
->unp_reflink
.le_prev
;
1369 cp
->unp_addr
= (_UNPCB_PTR(struct sockaddr_un
*))
1370 (uintptr_t)up
->unp_addr
;
1371 cp
->unp_cc
= up
->unp_cc
;
1372 cp
->unp_mbcnt
= up
->unp_mbcnt
;
1373 cp
->unp_gencnt
= up
->unp_gencnt
;
1377 unp_pcblist SYSCTL_HANDLER_ARGS
1379 #pragma unused(oidp,arg2)
1381 struct unpcb
*unp
, **unp_list
;
1384 struct unp_head
*head
;
1386 lck_rw_lock_shared(unp_list_mtx
);
1387 head
= ((intptr_t)arg1
== SOCK_DGRAM
? &unp_dhead
: &unp_shead
);
1390 * The process of preparing the PCB list is too time-consuming and
1391 * resource-intensive to repeat twice on every request.
1393 if (req
->oldptr
== USER_ADDR_NULL
) {
1395 req
->oldidx
= 2 * sizeof (xug
) + (n
+ n
/ 8) *
1396 sizeof (struct xunpcb
);
1397 lck_rw_done(unp_list_mtx
);
1401 if (req
->newptr
!= USER_ADDR_NULL
) {
1402 lck_rw_done(unp_list_mtx
);
1407 * OK, now we're committed to doing something.
1409 gencnt
= unp_gencnt
;
1412 bzero(&xug
, sizeof (xug
));
1413 xug
.xug_len
= sizeof (xug
);
1415 xug
.xug_gen
= gencnt
;
1416 xug
.xug_sogen
= so_gencnt
;
1417 error
= SYSCTL_OUT(req
, &xug
, sizeof (xug
));
1419 lck_rw_done(unp_list_mtx
);
1424 * We are done if there is no pcb
1427 lck_rw_done(unp_list_mtx
);
1431 MALLOC(unp_list
, struct unpcb
**, n
* sizeof (*unp_list
),
1433 if (unp_list
== 0) {
1434 lck_rw_done(unp_list_mtx
);
1438 for (unp
= head
->lh_first
, i
= 0; unp
&& i
< n
;
1439 unp
= unp
->unp_link
.le_next
) {
1440 if (unp
->unp_gencnt
<= gencnt
)
1441 unp_list
[i
++] = unp
;
1443 n
= i
; /* in case we lost some during malloc */
1446 for (i
= 0; i
< n
; i
++) {
1448 if (unp
->unp_gencnt
<= gencnt
) {
1451 bzero(&xu
, sizeof (xu
));
1452 xu
.xu_len
= sizeof (xu
);
1453 xu
.xu_unpp
= (_UNPCB_PTR(struct unpcb_compat
*))
1456 * XXX - need more locking here to protect against
1457 * connect/disconnect races for SMP.
1460 bcopy(unp
->unp_addr
, &xu
.xu_addr
,
1461 unp
->unp_addr
->sun_len
);
1462 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
)
1463 bcopy(unp
->unp_conn
->unp_addr
,
1465 unp
->unp_conn
->unp_addr
->sun_len
);
1466 unpcb_to_compat(unp
, &xu
.xu_unp
);
1467 sotoxsocket(unp
->unp_socket
, &xu
.xu_socket
);
1468 error
= SYSCTL_OUT(req
, &xu
, sizeof (xu
));
1473 * Give the user an updated idea of our state.
1474 * If the generation differs from what we told
1475 * her before, she knows that something happened
1476 * while we were processing this request, and it
1477 * might be necessary to retry.
1479 bzero(&xug
, sizeof (xug
));
1480 xug
.xug_len
= sizeof (xug
);
1481 xug
.xug_gen
= unp_gencnt
;
1482 xug
.xug_sogen
= so_gencnt
;
1483 xug
.xug_count
= unp_count
;
1484 error
= SYSCTL_OUT(req
, &xug
, sizeof (xug
));
1486 FREE(unp_list
, M_TEMP
);
1487 lck_rw_done(unp_list_mtx
);
1491 SYSCTL_PROC(_net_local_dgram
, OID_AUTO
, pcblist
, CTLFLAG_RD
,
1492 (caddr_t
)(long)SOCK_DGRAM
, 0, unp_pcblist
, "S,xunpcb",
1493 "List of active local datagram sockets");
1494 SYSCTL_PROC(_net_local_stream
, OID_AUTO
, pcblist
, CTLFLAG_RD
,
1495 (caddr_t
)(long)SOCK_STREAM
, 0, unp_pcblist
, "S,xunpcb",
1496 "List of active local stream sockets");
1498 #if !CONFIG_EMBEDDED
1501 unp_pcblist64 SYSCTL_HANDLER_ARGS
1503 #pragma unused(oidp,arg2)
1505 struct unpcb
*unp
, **unp_list
;
1508 struct unp_head
*head
;
1510 lck_rw_lock_shared(unp_list_mtx
);
1511 head
= ((intptr_t)arg1
== SOCK_DGRAM
? &unp_dhead
: &unp_shead
);
1514 * The process of preparing the PCB list is too time-consuming and
1515 * resource-intensive to repeat twice on every request.
1517 if (req
->oldptr
== USER_ADDR_NULL
) {
1519 req
->oldidx
= 2 * sizeof (xug
) + (n
+ n
/ 8) *
1520 (sizeof (struct xunpcb64
));
1521 lck_rw_done(unp_list_mtx
);
1525 if (req
->newptr
!= USER_ADDR_NULL
) {
1526 lck_rw_done(unp_list_mtx
);
1531 * OK, now we're committed to doing something.
1533 gencnt
= unp_gencnt
;
1536 bzero(&xug
, sizeof (xug
));
1537 xug
.xug_len
= sizeof (xug
);
1539 xug
.xug_gen
= gencnt
;
1540 xug
.xug_sogen
= so_gencnt
;
1541 error
= SYSCTL_OUT(req
, &xug
, sizeof (xug
));
1543 lck_rw_done(unp_list_mtx
);
1548 * We are done if there is no pcb
1551 lck_rw_done(unp_list_mtx
);
1555 MALLOC(unp_list
, struct unpcb
**, n
* sizeof (*unp_list
),
1557 if (unp_list
== 0) {
1558 lck_rw_done(unp_list_mtx
);
1562 for (unp
= head
->lh_first
, i
= 0; unp
&& i
< n
;
1563 unp
= unp
->unp_link
.le_next
) {
1564 if (unp
->unp_gencnt
<= gencnt
)
1565 unp_list
[i
++] = unp
;
1567 n
= i
; /* in case we lost some during malloc */
1570 for (i
= 0; i
< n
; i
++) {
1572 if (unp
->unp_gencnt
<= gencnt
) {
1574 size_t xu_len
= sizeof(struct xunpcb64
);
1578 xu
.xu_unpp
= (u_int64_t
)(uintptr_t)unp
;
1579 xu
.xunp_link
.le_next
=
1580 (u_int64_t
)(uintptr_t)unp
->unp_link
.le_next
;
1581 xu
.xunp_link
.le_prev
=
1582 (u_int64_t
)(uintptr_t)unp
->unp_link
.le_prev
;
1583 xu
.xunp_socket
= (u_int64_t
)(uintptr_t)unp
->unp_socket
;
1584 xu
.xunp_vnode
= (u_int64_t
)(uintptr_t)unp
->unp_vnode
;
1585 xu
.xunp_ino
= unp
->unp_ino
;
1586 xu
.xunp_conn
= (u_int64_t
)(uintptr_t)unp
->unp_conn
;
1587 xu
.xunp_refs
= (u_int64_t
)(uintptr_t)unp
->unp_refs
.lh_first
;
1588 xu
.xunp_reflink
.le_next
=
1589 (u_int64_t
)(uintptr_t)unp
->unp_reflink
.le_next
;
1590 xu
.xunp_reflink
.le_prev
=
1591 (u_int64_t
)(uintptr_t)unp
->unp_reflink
.le_prev
;
1592 xu
.xunp_cc
= unp
->unp_cc
;
1593 xu
.xunp_mbcnt
= unp
->unp_mbcnt
;
1594 xu
.xunp_gencnt
= unp
->unp_gencnt
;
1596 if (unp
->unp_socket
)
1597 sotoxsocket64(unp
->unp_socket
, &xu
.xu_socket
);
1600 * XXX - need more locking here to protect against
1601 * connect/disconnect races for SMP.
1604 bcopy(unp
->unp_addr
, &xu
.xunp_addr
,
1605 unp
->unp_addr
->sun_len
);
1606 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
)
1607 bcopy(unp
->unp_conn
->unp_addr
,
1609 unp
->unp_conn
->unp_addr
->sun_len
);
1611 error
= SYSCTL_OUT(req
, &xu
, xu_len
);
1616 * Give the user an updated idea of our state.
1617 * If the generation differs from what we told
1618 * her before, she knows that something happened
1619 * while we were processing this request, and it
1620 * might be necessary to retry.
1622 bzero(&xug
, sizeof (xug
));
1623 xug
.xug_len
= sizeof (xug
);
1624 xug
.xug_gen
= unp_gencnt
;
1625 xug
.xug_sogen
= so_gencnt
;
1626 xug
.xug_count
= unp_count
;
1627 error
= SYSCTL_OUT(req
, &xug
, sizeof (xug
));
1629 FREE(unp_list
, M_TEMP
);
1630 lck_rw_done(unp_list_mtx
);
1634 SYSCTL_PROC(_net_local_dgram
, OID_AUTO
, pcblist64
, CTLFLAG_RD
,
1635 (caddr_t
)(long)SOCK_DGRAM
, 0, unp_pcblist64
, "S,xunpcb64",
1636 "List of active local datagram sockets 64 bit");
1637 SYSCTL_PROC(_net_local_stream
, OID_AUTO
, pcblist64
, CTLFLAG_RD
,
1638 (caddr_t
)(long)SOCK_STREAM
, 0, unp_pcblist64
, "S,xunpcb64",
1639 "List of active local stream sockets 64 bit");
1641 #endif /* !CONFIG_EMBEDDED */
1644 unp_shutdown(struct unpcb
*unp
)
1646 struct socket
*so
= unp
->unp_socket
;
1648 if (unp
->unp_socket
->so_type
== SOCK_STREAM
&& unp
->unp_conn
) {
1649 so2
= unp
->unp_conn
->unp_socket
;
1650 unp_get_locks_in_order(so
, so2
);
1652 socket_unlock(so2
, 1);
1657 unp_drop(struct unpcb
*unp
, int errno
)
1659 struct socket
*so
= unp
->unp_socket
;
1661 so
->so_error
= errno
;
1662 unp_disconnect(unp
);
1666 * Returns: 0 Success
1667 * EMSGSIZE The new fd's will not fit
1668 * ENOBUFS Cannot alloc struct fileproc
1671 unp_externalize(struct mbuf
*rights
)
1673 proc_t p
= current_proc(); /* XXX */
1675 struct cmsghdr
*cm
= mtod(rights
, struct cmsghdr
*);
1676 struct fileglob
**rp
= (struct fileglob
**)(cm
+ 1);
1677 int *fds
= (int *)(cm
+ 1);
1678 struct fileproc
*fp
;
1679 struct fileglob
*fg
;
1680 int newfds
= (cm
->cmsg_len
- sizeof (*cm
)) / sizeof (int);
1686 * if the new FD's will not fit, then we free them all
1688 if (!fdavail(p
, newfds
)) {
1689 for (i
= 0; i
< newfds
; i
++) {
1691 unp_discard_fdlocked(fg
, p
);
1699 * now change each pointer to an fd in the global table to
1700 * an integer that is the index to the local fd table entry
1701 * that we set up to point to the global one we are transferring.
1702 * XXX (1) this assumes a pointer and int are the same size,
1703 * XXX or the mbuf can hold the expansion
1704 * XXX (2) allocation failures should be non-fatal
1706 for (i
= 0; i
< newfds
; i
++) {
1707 #if CONFIG_MACF_SOCKET
1709 * If receive access is denied, don't pass along
1710 * and error message, just discard the descriptor.
1712 if (mac_file_check_receive(kauth_cred_get(), *rp
)) {
1715 unp_discard_fdlocked(fg
, p
);
1719 if (fdalloc(p
, 0, &f
))
1720 panic("unp_externalize:fdalloc");
1722 MALLOC_ZONE(fp
, struct fileproc
*, sizeof (struct fileproc
),
1723 M_FILEPROC
, M_WAITOK
);
1725 panic("unp_externalize: MALLOC_ZONE");
1726 bzero(fp
, sizeof (struct fileproc
));
1730 procfdtbl_releasefd(p
, f
, fp
);
1731 (void) OSAddAtomic(-1, &unp_rights
);
1742 unp_zone
= zinit(sizeof (struct unpcb
),
1743 (nmbclusters
* sizeof (struct unpcb
)), 4096, "unpzone");
1747 LIST_INIT(&unp_dhead
);
1748 LIST_INIT(&unp_shead
);
1751 * allocate lock group attribute and group for udp pcb mutexes
1753 unp_mtx_grp_attr
= lck_grp_attr_alloc_init();
1755 unp_mtx_grp
= lck_grp_alloc_init("unp_list", unp_mtx_grp_attr
);
1757 unp_mtx_attr
= lck_attr_alloc_init();
1759 if ((unp_list_mtx
= lck_rw_alloc_init(unp_mtx_grp
,
1760 unp_mtx_attr
)) == NULL
)
1761 return; /* pretty much dead if this fails... */
1763 if ((unp_disconnect_lock
= lck_mtx_alloc_init(unp_mtx_grp
,
1764 unp_mtx_attr
)) == NULL
)
1767 if ((unp_connect_lock
= lck_mtx_alloc_init(unp_mtx_grp
,
1768 unp_mtx_attr
)) == NULL
)
1773 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
1777 * Returns: 0 Success
1779 * fdgetf_noref:EBADF
1782 unp_internalize(struct mbuf
*control
, proc_t p
)
1784 struct cmsghdr
*cm
= mtod(control
, struct cmsghdr
*);
1786 struct fileglob
**rp
;
1787 struct fileproc
*fp
;
1791 /* 64bit: cmsg_len is 'uint32_t', m_len is 'long' */
1792 if (cm
->cmsg_type
!= SCM_RIGHTS
|| cm
->cmsg_level
!= SOL_SOCKET
||
1793 (socklen_t
)cm
->cmsg_len
!= (socklen_t
)control
->m_len
) {
1796 oldfds
= (cm
->cmsg_len
- sizeof (*cm
)) / sizeof (int);
1799 fds
= (int *)(cm
+ 1);
1801 for (i
= 0; i
< oldfds
; i
++) {
1802 struct fileproc
*tmpfp
;
1803 if (((error
= fdgetf_noref(p
, fds
[i
], &tmpfp
)) != 0)) {
1806 } else if (!filetype_issendable(tmpfp
->f_fglob
->fg_type
)) {
1811 rp
= (struct fileglob
**)(cm
+ 1);
1813 /* On K64 we need to walk backwards because a fileglob * is twice the size of an fd
1814 * and doing them in-order would result in stomping over unprocessed fd's
1816 for (i
= (oldfds
- 1); i
>= 0; i
--) {
1817 (void) fdgetf_noref(p
, fds
[i
], &fp
);
1818 fg_insertuipc(fp
->f_fglob
);
1819 rp
[i
] = fp
->f_fglob
;
1820 (void) OSAddAtomic(1, &unp_rights
);
1827 static int unp_defer
, unp_gcing
, unp_gcwait
;
1828 static thread_t unp_gcthread
= NULL
;
1830 /* always called under uipc_lock */
1834 if (unp_gcthread
== current_thread())
1837 while (unp_gcing
!= 0) {
1839 msleep(&unp_gcing
, uipc_lock
, 0 , "unp_gc_wait", NULL
);
1844 __private_extern__
void
1847 struct fileglob
*fg
, *nextfg
;
1849 static struct fileglob
**extra_ref
;
1850 struct fileglob
**fpp
;
1852 int need_gcwakeup
= 0;
1854 lck_mtx_lock(uipc_lock
);
1856 lck_mtx_unlock(uipc_lock
);
1861 unp_gcthread
= current_thread();
1862 lck_mtx_unlock(uipc_lock
);
1864 * before going through all this, set all FDs to
1865 * be NOT defered and NOT externally accessible
1867 for (fg
= fmsghead
.lh_first
; fg
!= 0; fg
= fg
->f_msglist
.le_next
) {
1868 lck_mtx_lock(&fg
->fg_lock
);
1869 fg
->fg_flag
&= ~(FMARK
|FDEFER
);
1870 lck_mtx_unlock(&fg
->fg_lock
);
1873 for (fg
= fmsghead
.lh_first
; fg
!= 0;
1874 fg
= fg
->f_msglist
.le_next
) {
1875 lck_mtx_lock(&fg
->fg_lock
);
1877 * If the file is not open, skip it
1879 if (fg
->fg_count
== 0) {
1880 lck_mtx_unlock(&fg
->fg_lock
);
1884 * If we already marked it as 'defer' in a
1885 * previous pass, then try process it this time
1888 if (fg
->fg_flag
& FDEFER
) {
1889 fg
->fg_flag
&= ~FDEFER
;
1893 * if it's not defered, then check if it's
1894 * already marked.. if so skip it
1896 if (fg
->fg_flag
& FMARK
) {
1897 lck_mtx_unlock(&fg
->fg_lock
);
1901 * If all references are from messages
1902 * in transit, then skip it. it's not
1903 * externally accessible.
1905 if (fg
->fg_count
== fg
->fg_msgcount
) {
1906 lck_mtx_unlock(&fg
->fg_lock
);
1910 * If it got this far then it must be
1911 * externally accessible.
1913 fg
->fg_flag
|= FMARK
;
1916 * either it was defered, or it is externally
1917 * accessible and not already marked so.
1918 * Now check if it is possibly one of OUR sockets.
1920 if (fg
->fg_type
!= DTYPE_SOCKET
||
1921 (so
= (struct socket
*)fg
->fg_data
) == 0) {
1922 lck_mtx_unlock(&fg
->fg_lock
);
1925 if (so
->so_proto
->pr_domain
!= &localdomain
||
1926 (so
->so_proto
->pr_flags
&PR_RIGHTS
) == 0) {
1927 lck_mtx_unlock(&fg
->fg_lock
);
1932 * if this code is enabled need to run
1933 * under network funnel
1935 if (so
->so_rcv
.sb_flags
& SB_LOCK
) {
1937 * This is problematical; it's not clear
1938 * we need to wait for the sockbuf to be
1939 * unlocked (on a uniprocessor, at least),
1940 * and it's also not clear what to do
1941 * if sbwait returns an error due to receipt
1942 * of a signal. If sbwait does return
1943 * an error, we'll go into an infinite
1944 * loop. Delete all of this for now.
1946 (void) sbwait(&so
->so_rcv
);
1951 * So, Ok, it's one of our sockets and it IS externally
1952 * accessible (or was defered). Now we look
1953 * to see if we hold any file descriptors in its
1954 * message buffers. Follow those links and mark them
1955 * as accessible too.
1957 * In case a file is passed onto itself we need to
1958 * release the file lock.
1960 lck_mtx_unlock(&fg
->fg_lock
);
1962 unp_scan(so
->so_rcv
.sb_mb
, unp_mark
);
1964 } while (unp_defer
);
1966 * We grab an extra reference to each of the file table entries
1967 * that are not otherwise accessible and then free the rights
1968 * that are stored in messages on them.
1970 * The bug in the orginal code is a little tricky, so I'll describe
1971 * what's wrong with it here.
1973 * It is incorrect to simply unp_discard each entry for f_msgcount
1974 * times -- consider the case of sockets A and B that contain
1975 * references to each other. On a last close of some other socket,
1976 * we trigger a gc since the number of outstanding rights (unp_rights)
1977 * is non-zero. If during the sweep phase the gc code un_discards,
1978 * we end up doing a (full) closef on the descriptor. A closef on A
1979 * results in the following chain. Closef calls soo_close, which
1980 * calls soclose. Soclose calls first (through the switch
1981 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply
1982 * returns because the previous instance had set unp_gcing, and
1983 * we return all the way back to soclose, which marks the socket
1984 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush
1985 * to free up the rights that are queued in messages on the socket A,
1986 * i.e., the reference on B. The sorflush calls via the dom_dispose
1987 * switch unp_dispose, which unp_scans with unp_discard. This second
1988 * instance of unp_discard just calls closef on B.
1990 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1991 * which results in another closef on A. Unfortunately, A is already
1992 * being closed, and the descriptor has already been marked with
1993 * SS_NOFDREF, and soclose panics at this point.
1995 * Here, we first take an extra reference to each inaccessible
1996 * descriptor. Then, we call sorflush ourself, since we know
1997 * it is a Unix domain socket anyhow. After we destroy all the
1998 * rights carried in messages, we do a last closef to get rid
1999 * of our extra reference. This is the last close, and the
2000 * unp_detach etc will shut down the socket.
2002 * 91/09/19, bsy@cs.cmu.edu
2004 extra_ref
= _MALLOC(nfiles
* sizeof (struct fileglob
*),
2005 M_FILEGLOB
, M_WAITOK
);
2006 if (extra_ref
== NULL
)
2008 for (nunref
= 0, fg
= fmsghead
.lh_first
, fpp
= extra_ref
; fg
!= 0;
2010 lck_mtx_lock(&fg
->fg_lock
);
2012 nextfg
= fg
->f_msglist
.le_next
;
2014 * If it's not open, skip it
2016 if (fg
->fg_count
== 0) {
2017 lck_mtx_unlock(&fg
->fg_lock
);
2021 * If all refs are from msgs, and it's not marked accessible
2022 * then it must be referenced from some unreachable cycle
2023 * of (shut-down) FDs, so include it in our
2024 * list of FDs to remove
2026 if (fg
->fg_count
== fg
->fg_msgcount
&& !(fg
->fg_flag
& FMARK
)) {
2031 lck_mtx_unlock(&fg
->fg_lock
);
2034 * for each FD on our hit list, do the following two things
2036 for (i
= nunref
, fpp
= extra_ref
; --i
>= 0; ++fpp
) {
2037 struct fileglob
*tfg
;
2041 if (tfg
->fg_type
== DTYPE_SOCKET
&& tfg
->fg_data
!= NULL
) {
2042 so
= (struct socket
*)(tfg
->fg_data
);
2048 socket_unlock(so
, 0);
2051 for (i
= nunref
, fpp
= extra_ref
; --i
>= 0; ++fpp
)
2052 closef_locked((struct fileproc
*)0, *fpp
, (proc_t
)NULL
);
2054 FREE((caddr_t
)extra_ref
, M_FILEGLOB
);
2056 lck_mtx_lock(uipc_lock
);
2058 unp_gcthread
= NULL
;
2060 if (unp_gcwait
!= 0) {
2064 lck_mtx_unlock(uipc_lock
);
2066 if (need_gcwakeup
!= 0)
2071 unp_dispose(struct mbuf
*m
)
2074 unp_scan(m
, unp_discard
);
2079 * Returns: 0 Success
2082 unp_listen(struct unpcb
*unp
, proc_t p
)
2084 kauth_cred_t safecred
= kauth_cred_proc_ref(p
);
2085 cru2x(safecred
, &unp
->unp_peercred
);
2086 kauth_cred_unref(&safecred
);
2087 unp
->unp_flags
|= UNP_HAVEPCCACHED
;
2091 /* should run under kernel funnel */
2093 unp_scan(struct mbuf
*m0
, void (*op
)(struct fileglob
*))
2096 struct fileglob
**rp
;
2102 for (m
= m0
; m
; m
= m
->m_next
)
2103 if (m
->m_type
== MT_CONTROL
&&
2104 (size_t)m
->m_len
>= sizeof (*cm
)) {
2105 cm
= mtod(m
, struct cmsghdr
*);
2106 if (cm
->cmsg_level
!= SOL_SOCKET
||
2107 cm
->cmsg_type
!= SCM_RIGHTS
)
2109 qfds
= (cm
->cmsg_len
- sizeof (*cm
)) /
2111 rp
= (struct fileglob
**)(cm
+ 1);
2112 for (i
= 0; i
< qfds
; i
++)
2114 break; /* XXX, but saves time */
2120 /* should run under kernel funnel */
2122 unp_mark(struct fileglob
*fg
)
2124 lck_mtx_lock(&fg
->fg_lock
);
2126 if (fg
->fg_flag
& FMARK
) {
2127 lck_mtx_unlock(&fg
->fg_lock
);
2130 fg
->fg_flag
|= (FMARK
|FDEFER
);
2132 lck_mtx_unlock(&fg
->fg_lock
);
2137 /* should run under kernel funnel */
2139 unp_discard(struct fileglob
*fg
)
2141 proc_t p
= current_proc(); /* XXX */
2143 (void) OSAddAtomic(1, &unp_disposed
);
2146 unp_discard_fdlocked(fg
, p
);
2150 unp_discard_fdlocked(struct fileglob
*fg
, proc_t p
)
2154 (void) OSAddAtomic(-1, &unp_rights
);
2155 (void) closef_locked((struct fileproc
*)0, fg
, p
);
2159 unp_lock(struct socket
*so
, int refcount
, void * lr
)
2163 lr_saved
= (void *) __builtin_return_address(0);
2167 lck_mtx_lock(((struct unpcb
*)so
->so_pcb
)->unp_mtx
);
2169 panic("unp_lock: so=%p NO PCB! lr=%p ref=0x%x\n",
2170 so
, lr_saved
, so
->so_usecount
);
2173 if (so
->so_usecount
< 0)
2174 panic("unp_lock: so=%p so_pcb=%p lr=%p ref=0x%x\n",
2175 so
, so
->so_pcb
, lr_saved
, so
->so_usecount
);
2180 so
->lock_lr
[so
->next_lock_lr
] = lr_saved
;
2181 so
->next_lock_lr
= (so
->next_lock_lr
+1) % SO_LCKDBG_MAX
;
2186 unp_unlock(struct socket
*so
, int refcount
, void * lr
)
2189 lck_mtx_t
* mutex_held
= NULL
;
2190 struct unpcb
*unp
= sotounpcb(so
);
2193 lr_saved
= (void *) __builtin_return_address(0);
2199 if (so
->so_usecount
< 0)
2200 panic("unp_unlock: so=%p usecount=%x\n", so
, so
->so_usecount
);
2201 if (so
->so_pcb
== NULL
) {
2202 panic("unp_unlock: so=%p NO PCB usecount=%x\n", so
, so
->so_usecount
);
2204 mutex_held
= ((struct unpcb
*)so
->so_pcb
)->unp_mtx
;
2206 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
2207 so
->unlock_lr
[so
->next_unlock_lr
] = lr_saved
;
2208 so
->next_unlock_lr
= (so
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
2210 if (so
->so_usecount
== 0 && (so
->so_flags
& SOF_PCBCLEARING
)) {
2211 sofreelastref(so
, 1);
2214 FREE(unp
->unp_addr
, M_SONAME
);
2216 lck_mtx_unlock(mutex_held
);
2218 lck_mtx_free(unp
->unp_mtx
, unp_mtx_grp
);
2220 unp
->unp_gencnt
= ++unp_gencnt
;
2221 zfree(unp_zone
, unp
);
2226 lck_mtx_unlock(mutex_held
);
2233 unp_getlock(struct socket
*so
, __unused
int locktype
)
2235 struct unpcb
*unp
= (struct unpcb
*)so
->so_pcb
;
2239 if (so
->so_usecount
< 0)
2240 panic("unp_getlock: so=%p usecount=%x\n", so
, so
->so_usecount
);
2241 return(unp
->unp_mtx
);
2243 panic("unp_getlock: so=%p NULL so_pcb\n", so
);
2244 return (so
->so_proto
->pr_domain
->dom_mtx
);