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
)
785 lck_rw_lock_exclusive(unp_list_mtx
);
786 LIST_REMOVE(unp
, unp_link
);
787 lck_rw_done(unp_list_mtx
);
788 if (unp
->unp_vnode
) {
789 struct vnode
*tvp
= NULL
;
790 socket_unlock(unp
->unp_socket
, 0);
792 /* Holding unp_connect_lock will avoid a race between
793 * a thread closing the listening socket and a thread
796 lck_mtx_lock(unp_connect_lock
);
797 socket_lock(unp
->unp_socket
, 0);
798 if (unp
->unp_vnode
) {
799 tvp
= unp
->unp_vnode
;
800 unp
->unp_vnode
->v_socket
= NULL
;
801 unp
->unp_vnode
= NULL
;
803 lck_mtx_unlock(unp_connect_lock
);
805 vnode_rele(tvp
); /* drop the usecount */
809 while (unp
->unp_refs
.lh_first
) {
810 struct unpcb
*unp2
= NULL
;
812 /* This datagram socket is connected to one or more
813 * sockets. In order to avoid a race condition between removing
814 * this reference and closing the connected socket, we need
815 * to check disconnect_in_progress
817 if (so_locked
== 1) {
818 socket_unlock(unp
->unp_socket
, 0);
821 lck_mtx_lock(unp_disconnect_lock
);
822 while (disconnect_in_progress
!= 0) {
823 (void)msleep((caddr_t
)&disconnect_in_progress
, unp_disconnect_lock
,
824 PSOCK
, "disconnect", NULL
);
826 disconnect_in_progress
= 1;
827 lck_mtx_unlock(unp_disconnect_lock
);
829 /* Now we are sure that any unpcb socket disconnect is not happening */
830 if (unp
->unp_refs
.lh_first
!= NULL
) {
831 unp2
= unp
->unp_refs
.lh_first
;
832 socket_lock(unp2
->unp_socket
, 1);
835 lck_mtx_lock(unp_disconnect_lock
);
836 disconnect_in_progress
= 0;
837 wakeup(&disconnect_in_progress
);
838 lck_mtx_unlock(unp_disconnect_lock
);
841 /* We already locked this socket and have a reference on it */
842 unp_drop(unp2
, ECONNRESET
);
843 socket_unlock(unp2
->unp_socket
, 1);
847 if (so_locked
== 0) {
848 socket_lock(unp
->unp_socket
, 0);
851 soisdisconnected(unp
->unp_socket
);
852 /* makes sure we're getting dealloced */
853 unp
->unp_socket
->so_flags
|= SOF_PCBCLEARING
;
861 * namei:??? [anything namei can return]
862 * vnode_authorize:??? [anything vnode_authorize can return]
864 * Notes: p at this point is the current process, as this function is
865 * only called by sobind().
870 struct sockaddr
*nam
,
873 struct sockaddr_un
*soun
= (struct sockaddr_un
*)nam
;
874 struct vnode
*vp
, *dvp
;
875 struct vnode_attr va
;
876 vfs_context_t ctx
= vfs_context_current();
879 struct socket
*so
= unp
->unp_socket
;
880 char buf
[SOCK_MAXADDRLEN
];
882 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_UNIX
) {
883 return (EAFNOSUPPORT
);
886 if (unp
->unp_vnode
!= NULL
)
888 namelen
= soun
->sun_len
- offsetof(struct sockaddr_un
, sun_path
);
892 socket_unlock(so
, 0);
894 strlcpy(buf
, soun
->sun_path
, namelen
+1);
895 NDINIT(&nd
, CREATE
, FOLLOW
| LOCKPARENT
, UIO_SYSSPACE
,
896 CAST_USER_ADDR_T(buf
), ctx
);
897 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
908 * need to do this before the vnode_put of dvp
909 * since we may have to release an fs_nodelock
921 VATTR_SET(&va
, va_type
, VSOCK
);
922 VATTR_SET(&va
, va_mode
, (ACCESSPERMS
& ~p
->p_fd
->fd_cmask
));
925 error
= mac_vnode_check_create(ctx
,
926 nd
.ni_dvp
, &nd
.ni_cnd
, &va
);
929 #endif /* CONFIG_MACF */
930 #if CONFIG_MACF_SOCKET_SUBSET
931 error
= mac_vnode_check_uipc_bind(ctx
,
932 nd
.ni_dvp
, &nd
.ni_cnd
, &va
);
935 #endif /* MAC_SOCKET_SUBSET */
936 /* authorize before creating */
937 error
= vnode_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, ctx
);
940 /* create the socket */
941 error
= vn_create(dvp
, &vp
, &nd
.ni_cnd
, &va
, 0, ctx
);
951 vnode_ref(vp
); /* gain a longterm reference */
953 vp
->v_socket
= unp
->unp_socket
;
955 unp
->unp_addr
= (struct sockaddr_un
*)dup_sockaddr(nam
, 1);
956 vnode_put(vp
); /* drop the iocount */
964 * EAFNOSUPPORT Address family not supported
965 * EINVAL Invalid argument
966 * ENOTSOCK Not a socket
967 * ECONNREFUSED Connection refused
968 * EPROTOTYPE Protocol wrong type for socket
969 * EISCONN Socket is connected
970 * unp_connect2:EPROTOTYPE Protocol wrong type for socket
971 * unp_connect2:EINVAL Invalid argument
972 * namei:??? [anything namei can return]
973 * vnode_authorize:???? [anything vnode_authorize can return]
975 * Notes: p at this point is the current process, as this function is
976 * only called by sosend(), sendfile(), and soconnectlock().
979 unp_connect(struct socket
*so
, struct sockaddr
*nam
, __unused proc_t p
)
981 struct sockaddr_un
*soun
= (struct sockaddr_un
*)nam
;
983 struct socket
*so2
, *so3
, *list_so
=NULL
;
984 struct unpcb
*unp
, *unp2
, *unp3
;
985 vfs_context_t ctx
= vfs_context_current();
988 char buf
[SOCK_MAXADDRLEN
];
990 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_UNIX
) {
991 return (EAFNOSUPPORT
);
997 len
= nam
->sa_len
- offsetof(struct sockaddr_un
, sun_path
);
1001 strlcpy(buf
, soun
->sun_path
, len
+1);
1002 socket_unlock(so
, 0);
1004 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
,
1005 CAST_USER_ADDR_T(buf
), ctx
);
1013 if (vp
->v_type
!= VSOCK
) {
1019 #if CONFIG_MACF_SOCKET_SUBSET
1020 error
= mac_vnode_check_uipc_connect(ctx
, vp
);
1025 #endif /* MAC_SOCKET_SUBSET */
1027 error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_WRITE_DATA
, ctx
);
1033 lck_mtx_lock(unp_connect_lock
);
1035 if (vp
->v_socket
== 0) {
1036 lck_mtx_unlock(unp_connect_lock
);
1037 error
= ECONNREFUSED
;
1042 socket_lock(vp
->v_socket
, 1); /* Get a reference on the listening socket */
1044 lck_mtx_unlock(unp_connect_lock
);
1047 if (so2
->so_pcb
== NULL
) {
1048 error
= ECONNREFUSED
;
1049 socket_unlock(so2
, 1);
1055 socket_unlock(so2
, 0);
1057 socket_lock(so2
, 0);
1062 * Check if socket was connected while we were trying to
1063 * get the socket locks in order.
1064 * XXX - probably shouldn't return an error for SOCK_DGRAM
1066 if ((so
->so_state
& SS_ISCONNECTED
) != 0) {
1067 socket_unlock(so2
, 1);
1072 if (so
->so_type
!= so2
->so_type
) {
1073 socket_unlock(so2
, 1);
1078 if (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) {
1079 /* Release the incoming socket but keep a reference */
1080 socket_unlock(so
, 0);
1082 if ((so2
->so_options
& SO_ACCEPTCONN
) == 0 ||
1083 (so3
= sonewconn(so2
, 0, nam
)) == 0) {
1084 error
= ECONNREFUSED
;
1085 socket_unlock(so2
, 1);
1089 unp2
= sotounpcb(so2
);
1090 unp3
= sotounpcb(so3
);
1092 unp3
->unp_addr
= (struct sockaddr_un
*)
1093 dup_sockaddr((struct sockaddr
*)unp2
->unp_addr
, 1);
1096 * unp_peercred management:
1098 * The connecter's (client's) credentials are copied
1099 * from its process structure at the time of connect()
1102 cru2x(vfs_context_ucred(ctx
), &unp3
->unp_peercred
);
1103 unp3
->unp_flags
|= UNP_HAVEPC
;
1105 * The receiver's (server's) credentials are copied
1106 * from the unp_peercred member of socket on which the
1107 * former called listen(); unp_listen() cached that
1108 * process's credentials at that time so we can use
1111 KASSERT(unp2
->unp_flags
& UNP_HAVEPCCACHED
,
1112 ("unp_connect: listener without cached peercred"));
1114 /* Here we need to have both so and so2 locks and so2
1115 * is already locked. Lock ordering is required.
1118 socket_unlock(so2
, 0);
1120 socket_lock(so2
, 0);
1125 /* Check again if the socket state changed when its lock was released */
1126 if ((so
->so_state
& SS_ISCONNECTED
) != 0) {
1128 socket_unlock(so2
, 1);
1129 socket_lock(so3
, 0);
1130 sofreelastref(so3
, 1);
1133 memcpy(&unp
->unp_peercred
, &unp2
->unp_peercred
,
1134 sizeof (unp
->unp_peercred
));
1135 unp
->unp_flags
|= UNP_HAVEPC
;
1137 #if CONFIG_MACF_SOCKET
1138 /* XXXMAC: recursive lock: SOCK_LOCK(so); */
1139 mac_socketpeer_label_associate_socket(so
, so3
);
1140 mac_socketpeer_label_associate_socket(so3
, so
);
1141 /* XXXMAC: SOCK_UNLOCK(so); */
1142 #endif /* MAC_SOCKET */
1144 /* Hold the reference on listening socket until the end */
1145 socket_unlock(so2
, 0);
1148 /* Lock ordering doesn't matter because so3 was just created */
1149 socket_lock(so3
, 1);
1154 error
= unp_connect2(so
, so2
);
1156 socket_unlock(so2
, 1);
1159 if (list_so
!= NULL
) {
1160 socket_lock(list_so
, 0);
1161 socket_unlock(list_so
, 1);
1164 lck_mtx_assert(unp
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
1170 * Returns: 0 Success
1171 * EPROTOTYPE Protocol wrong type for socket
1172 * EINVAL Invalid argument
1175 unp_connect2(struct socket
*so
, struct socket
*so2
)
1177 struct unpcb
*unp
= sotounpcb(so
);
1180 if (so2
->so_type
!= so
->so_type
)
1181 return (EPROTOTYPE
);
1183 unp2
= sotounpcb(so2
);
1185 lck_mtx_assert(unp
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
1186 lck_mtx_assert(unp2
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
1188 /* Verify both sockets are still opened */
1189 if (unp
== 0 || unp2
== 0)
1192 unp
->unp_conn
= unp2
;
1195 switch (so
->so_type
) {
1198 LIST_INSERT_HEAD(&unp2
->unp_refs
, unp
, unp_reflink
);
1201 /* Avoid lock order reversals due to drop/acquire in soisconnected. */
1202 /* Keep an extra reference on so2 that will be dropped
1203 * soon after getting the locks in order
1205 socket_unlock(so2
, 0);
1207 unp_get_locks_in_order(so
, so2
);
1213 /* This takes care of socketpair */
1214 if (!(unp
->unp_flags
& UNP_HAVEPC
) &&
1215 !(unp2
->unp_flags
& UNP_HAVEPC
)) {
1216 cru2x(kauth_cred_get(), &unp
->unp_peercred
);
1217 unp
->unp_flags
|= UNP_HAVEPC
;
1219 cru2x(kauth_cred_get(), &unp2
->unp_peercred
);
1220 unp2
->unp_flags
|= UNP_HAVEPC
;
1222 unp2
->unp_conn
= unp
;
1225 /* Avoid lock order reversals due to drop/acquire in soisconnected. */
1226 socket_unlock(so
, 0);
1229 /* Keep an extra reference on so2, that will be dropped soon after
1230 * getting the locks in order again.
1232 socket_unlock(so2
, 0);
1237 unp_get_locks_in_order(so
, so2
);
1238 /* Decrement the extra reference left before */
1243 panic("unknown socket type %d in unp_connect2", so
->so_type
);
1245 lck_mtx_assert(unp
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
1246 lck_mtx_assert(unp2
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
1251 unp_disconnect(struct unpcb
*unp
)
1253 struct unpcb
*unp2
= NULL
;
1254 struct socket
*so2
= NULL
, *so
;
1255 struct socket
*waitso
;
1256 int so_locked
= 1, strdisconn
= 0;
1258 so
= unp
->unp_socket
;
1259 if (unp
->unp_conn
== NULL
) {
1262 lck_mtx_lock(unp_disconnect_lock
);
1263 while (disconnect_in_progress
!= 0) {
1264 if (so_locked
== 1) {
1265 socket_unlock(so
, 0);
1268 (void)msleep((caddr_t
)&disconnect_in_progress
, unp_disconnect_lock
,
1269 PSOCK
, "disconnect", NULL
);
1271 disconnect_in_progress
= 1;
1272 lck_mtx_unlock(unp_disconnect_lock
);
1274 if (so_locked
== 0) {
1279 unp2
= unp
->unp_conn
;
1281 if (unp2
== 0 || unp2
->unp_socket
== NULL
) {
1284 so2
= unp2
->unp_socket
;
1288 if (so_locked
== 0) {
1291 socket_lock(so2
, 1);
1294 if (so_locked
== 1) {
1295 socket_unlock(so
, 0);
1297 socket_lock(so2
, 1);
1302 lck_mtx_assert(unp
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
1303 lck_mtx_assert(unp2
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
1305 /* Check for the UNP_DONTDISCONNECT flag, if it
1306 * is set, release both sockets and go to sleep
1309 if ((((struct unpcb
*)waitso
->so_pcb
)->unp_flags
& UNP_DONTDISCONNECT
) != 0) {
1310 socket_unlock(so2
, 1);
1313 (void)msleep(waitso
->so_pcb
, unp
->unp_mtx
,
1314 PSOCK
| PDROP
, "unpdisconnect", NULL
);
1318 if (unp
->unp_conn
== NULL
) {
1319 panic("unp_conn became NULL after sleep");
1322 unp
->unp_conn
= NULL
;
1325 switch (unp
->unp_socket
->so_type
) {
1328 LIST_REMOVE(unp
, unp_reflink
);
1329 unp
->unp_socket
->so_state
&= ~SS_ISCONNECTED
;
1330 socket_unlock(so2
, 1);
1334 unp2
->unp_conn
= NULL
;
1337 /* Set the socket state correctly but do a wakeup later when
1338 * we release all locks except the socket lock, this will avoid
1341 unp
->unp_socket
->so_state
&= ~(SS_ISCONNECTING
|SS_ISCONNECTED
|SS_ISDISCONNECTING
);
1342 unp
->unp_socket
->so_state
|= (SS_CANTRCVMORE
|SS_CANTSENDMORE
|SS_ISDISCONNECTED
);
1344 unp2
->unp_socket
->so_state
&= ~(SS_ISCONNECTING
|SS_ISCONNECTED
|SS_ISDISCONNECTING
);
1345 unp
->unp_socket
->so_state
|= (SS_CANTRCVMORE
|SS_CANTSENDMORE
|SS_ISDISCONNECTED
);
1349 panic("unknown socket type %d", so
->so_type
);
1352 lck_mtx_lock(unp_disconnect_lock
);
1353 disconnect_in_progress
= 0;
1354 wakeup(&disconnect_in_progress
);
1355 lck_mtx_unlock(unp_disconnect_lock
);
1358 socket_unlock(so
, 0);
1359 soisdisconnected(so2
);
1360 socket_unlock(so2
, 1);
1363 soisdisconnected(so
);
1365 lck_mtx_assert(unp
->unp_mtx
, LCK_MTX_ASSERT_OWNED
);
1370 * unpcb_to_compat copies specific bits of a unpcb to a unpcb_compat format.
1371 * The unpcb_compat data structure is passed to user space and must not change.
1374 unpcb_to_compat(struct unpcb
*up
, struct unpcb_compat
*cp
)
1376 #if defined(__LP64__)
1377 cp
->unp_link
.le_next
= (u_int32_t
)(uintptr_t)up
->unp_link
.le_next
;
1378 cp
->unp_link
.le_prev
= (u_int32_t
)(uintptr_t)up
->unp_link
.le_prev
;
1380 cp
->unp_link
.le_next
= (struct unpcb_compat
*)up
->unp_link
.le_next
;
1381 cp
->unp_link
.le_prev
= (struct unpcb_compat
**)up
->unp_link
.le_prev
;
1383 cp
->unp_socket
= (_UNPCB_PTR(struct socket
*))(uintptr_t)up
->unp_socket
;
1384 cp
->unp_vnode
= (_UNPCB_PTR(struct vnode
*))(uintptr_t)up
->unp_vnode
;
1385 cp
->unp_ino
= up
->unp_ino
;
1386 cp
->unp_conn
= (_UNPCB_PTR(struct unpcb_compat
*))
1387 (uintptr_t)up
->unp_conn
;
1388 cp
->unp_refs
= (u_int32_t
)(uintptr_t)up
->unp_refs
.lh_first
;
1389 #if defined(__LP64__)
1390 cp
->unp_reflink
.le_next
=
1391 (u_int32_t
)(uintptr_t)up
->unp_reflink
.le_next
;
1392 cp
->unp_reflink
.le_prev
=
1393 (u_int32_t
)(uintptr_t)up
->unp_reflink
.le_prev
;
1395 cp
->unp_reflink
.le_next
=
1396 (struct unpcb_compat
*)up
->unp_reflink
.le_next
;
1397 cp
->unp_reflink
.le_prev
=
1398 (struct unpcb_compat
**)up
->unp_reflink
.le_prev
;
1400 cp
->unp_addr
= (_UNPCB_PTR(struct sockaddr_un
*))
1401 (uintptr_t)up
->unp_addr
;
1402 cp
->unp_cc
= up
->unp_cc
;
1403 cp
->unp_mbcnt
= up
->unp_mbcnt
;
1404 cp
->unp_gencnt
= up
->unp_gencnt
;
1408 unp_pcblist SYSCTL_HANDLER_ARGS
1410 #pragma unused(oidp,arg2)
1412 struct unpcb
*unp
, **unp_list
;
1415 struct unp_head
*head
;
1417 lck_rw_lock_shared(unp_list_mtx
);
1418 head
= ((intptr_t)arg1
== SOCK_DGRAM
? &unp_dhead
: &unp_shead
);
1421 * The process of preparing the PCB list is too time-consuming and
1422 * resource-intensive to repeat twice on every request.
1424 if (req
->oldptr
== USER_ADDR_NULL
) {
1426 req
->oldidx
= 2 * sizeof (xug
) + (n
+ n
/ 8) *
1427 sizeof (struct xunpcb
);
1428 lck_rw_done(unp_list_mtx
);
1432 if (req
->newptr
!= USER_ADDR_NULL
) {
1433 lck_rw_done(unp_list_mtx
);
1438 * OK, now we're committed to doing something.
1440 gencnt
= unp_gencnt
;
1443 bzero(&xug
, sizeof (xug
));
1444 xug
.xug_len
= sizeof (xug
);
1446 xug
.xug_gen
= gencnt
;
1447 xug
.xug_sogen
= so_gencnt
;
1448 error
= SYSCTL_OUT(req
, &xug
, sizeof (xug
));
1450 lck_rw_done(unp_list_mtx
);
1455 * We are done if there is no pcb
1458 lck_rw_done(unp_list_mtx
);
1462 MALLOC(unp_list
, struct unpcb
**, n
* sizeof (*unp_list
),
1464 if (unp_list
== 0) {
1465 lck_rw_done(unp_list_mtx
);
1469 for (unp
= head
->lh_first
, i
= 0; unp
&& i
< n
;
1470 unp
= unp
->unp_link
.le_next
) {
1471 if (unp
->unp_gencnt
<= gencnt
)
1472 unp_list
[i
++] = unp
;
1474 n
= i
; /* in case we lost some during malloc */
1477 for (i
= 0; i
< n
; i
++) {
1479 if (unp
->unp_gencnt
<= gencnt
) {
1482 bzero(&xu
, sizeof (xu
));
1483 xu
.xu_len
= sizeof (xu
);
1484 xu
.xu_unpp
= (_UNPCB_PTR(struct unpcb_compat
*))
1487 * XXX - need more locking here to protect against
1488 * connect/disconnect races for SMP.
1491 bcopy(unp
->unp_addr
, &xu
.xu_addr
,
1492 unp
->unp_addr
->sun_len
);
1493 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
)
1494 bcopy(unp
->unp_conn
->unp_addr
,
1496 unp
->unp_conn
->unp_addr
->sun_len
);
1497 unpcb_to_compat(unp
, &xu
.xu_unp
);
1498 sotoxsocket(unp
->unp_socket
, &xu
.xu_socket
);
1499 error
= SYSCTL_OUT(req
, &xu
, sizeof (xu
));
1504 * Give the user an updated idea of our state.
1505 * If the generation differs from what we told
1506 * her before, she knows that something happened
1507 * while we were processing this request, and it
1508 * might be necessary to retry.
1510 bzero(&xug
, sizeof (xug
));
1511 xug
.xug_len
= sizeof (xug
);
1512 xug
.xug_gen
= unp_gencnt
;
1513 xug
.xug_sogen
= so_gencnt
;
1514 xug
.xug_count
= unp_count
;
1515 error
= SYSCTL_OUT(req
, &xug
, sizeof (xug
));
1517 FREE(unp_list
, M_TEMP
);
1518 lck_rw_done(unp_list_mtx
);
1522 SYSCTL_PROC(_net_local_dgram
, OID_AUTO
, pcblist
, CTLFLAG_RD
,
1523 (caddr_t
)(long)SOCK_DGRAM
, 0, unp_pcblist
, "S,xunpcb",
1524 "List of active local datagram sockets");
1525 SYSCTL_PROC(_net_local_stream
, OID_AUTO
, pcblist
, CTLFLAG_RD
,
1526 (caddr_t
)(long)SOCK_STREAM
, 0, unp_pcblist
, "S,xunpcb",
1527 "List of active local stream sockets");
1529 #if !CONFIG_EMBEDDED
1532 unp_pcblist64 SYSCTL_HANDLER_ARGS
1534 #pragma unused(oidp,arg2)
1536 struct unpcb
*unp
, **unp_list
;
1539 struct unp_head
*head
;
1541 lck_rw_lock_shared(unp_list_mtx
);
1542 head
= ((intptr_t)arg1
== SOCK_DGRAM
? &unp_dhead
: &unp_shead
);
1545 * The process of preparing the PCB list is too time-consuming and
1546 * resource-intensive to repeat twice on every request.
1548 if (req
->oldptr
== USER_ADDR_NULL
) {
1550 req
->oldidx
= 2 * sizeof (xug
) + (n
+ n
/ 8) *
1551 (sizeof (struct xunpcb64
));
1552 lck_rw_done(unp_list_mtx
);
1556 if (req
->newptr
!= USER_ADDR_NULL
) {
1557 lck_rw_done(unp_list_mtx
);
1562 * OK, now we're committed to doing something.
1564 gencnt
= unp_gencnt
;
1567 bzero(&xug
, sizeof (xug
));
1568 xug
.xug_len
= sizeof (xug
);
1570 xug
.xug_gen
= gencnt
;
1571 xug
.xug_sogen
= so_gencnt
;
1572 error
= SYSCTL_OUT(req
, &xug
, sizeof (xug
));
1574 lck_rw_done(unp_list_mtx
);
1579 * We are done if there is no pcb
1582 lck_rw_done(unp_list_mtx
);
1586 MALLOC(unp_list
, struct unpcb
**, n
* sizeof (*unp_list
),
1588 if (unp_list
== 0) {
1589 lck_rw_done(unp_list_mtx
);
1593 for (unp
= head
->lh_first
, i
= 0; unp
&& i
< n
;
1594 unp
= unp
->unp_link
.le_next
) {
1595 if (unp
->unp_gencnt
<= gencnt
)
1596 unp_list
[i
++] = unp
;
1598 n
= i
; /* in case we lost some during malloc */
1601 for (i
= 0; i
< n
; i
++) {
1603 if (unp
->unp_gencnt
<= gencnt
) {
1605 size_t xu_len
= sizeof(struct xunpcb64
);
1609 xu
.xu_unpp
= (u_int64_t
)(uintptr_t)unp
;
1610 xu
.xunp_link
.le_next
=
1611 (u_int64_t
)(uintptr_t)unp
->unp_link
.le_next
;
1612 xu
.xunp_link
.le_prev
=
1613 (u_int64_t
)(uintptr_t)unp
->unp_link
.le_prev
;
1614 xu
.xunp_socket
= (u_int64_t
)(uintptr_t)unp
->unp_socket
;
1615 xu
.xunp_vnode
= (u_int64_t
)(uintptr_t)unp
->unp_vnode
;
1616 xu
.xunp_ino
= unp
->unp_ino
;
1617 xu
.xunp_conn
= (u_int64_t
)(uintptr_t)unp
->unp_conn
;
1618 xu
.xunp_refs
= (u_int64_t
)(uintptr_t)unp
->unp_refs
.lh_first
;
1619 xu
.xunp_reflink
.le_next
=
1620 (u_int64_t
)(uintptr_t)unp
->unp_reflink
.le_next
;
1621 xu
.xunp_reflink
.le_prev
=
1622 (u_int64_t
)(uintptr_t)unp
->unp_reflink
.le_prev
;
1623 xu
.xunp_cc
= unp
->unp_cc
;
1624 xu
.xunp_mbcnt
= unp
->unp_mbcnt
;
1625 xu
.xunp_gencnt
= unp
->unp_gencnt
;
1627 if (unp
->unp_socket
)
1628 sotoxsocket64(unp
->unp_socket
, &xu
.xu_socket
);
1631 * XXX - need more locking here to protect against
1632 * connect/disconnect races for SMP.
1635 bcopy(unp
->unp_addr
, &xu
.xunp_addr
,
1636 unp
->unp_addr
->sun_len
);
1637 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
)
1638 bcopy(unp
->unp_conn
->unp_addr
,
1640 unp
->unp_conn
->unp_addr
->sun_len
);
1642 error
= SYSCTL_OUT(req
, &xu
, xu_len
);
1647 * Give the user an updated idea of our state.
1648 * If the generation differs from what we told
1649 * her before, she knows that something happened
1650 * while we were processing this request, and it
1651 * might be necessary to retry.
1653 bzero(&xug
, sizeof (xug
));
1654 xug
.xug_len
= sizeof (xug
);
1655 xug
.xug_gen
= unp_gencnt
;
1656 xug
.xug_sogen
= so_gencnt
;
1657 xug
.xug_count
= unp_count
;
1658 error
= SYSCTL_OUT(req
, &xug
, sizeof (xug
));
1660 FREE(unp_list
, M_TEMP
);
1661 lck_rw_done(unp_list_mtx
);
1665 SYSCTL_PROC(_net_local_dgram
, OID_AUTO
, pcblist64
, CTLFLAG_RD
,
1666 (caddr_t
)(long)SOCK_DGRAM
, 0, unp_pcblist64
, "S,xunpcb64",
1667 "List of active local datagram sockets 64 bit");
1668 SYSCTL_PROC(_net_local_stream
, OID_AUTO
, pcblist64
, CTLFLAG_RD
,
1669 (caddr_t
)(long)SOCK_STREAM
, 0, unp_pcblist64
, "S,xunpcb64",
1670 "List of active local stream sockets 64 bit");
1672 #endif /* !CONFIG_EMBEDDED */
1675 unp_shutdown(struct unpcb
*unp
)
1677 struct socket
*so
= unp
->unp_socket
;
1679 if (unp
->unp_socket
->so_type
== SOCK_STREAM
&& unp
->unp_conn
) {
1680 so2
= unp
->unp_conn
->unp_socket
;
1681 unp_get_locks_in_order(so
, so2
);
1683 socket_unlock(so2
, 1);
1688 unp_drop(struct unpcb
*unp
, int errno
)
1690 struct socket
*so
= unp
->unp_socket
;
1692 so
->so_error
= errno
;
1693 unp_disconnect(unp
);
1697 * Returns: 0 Success
1698 * EMSGSIZE The new fd's will not fit
1699 * ENOBUFS Cannot alloc struct fileproc
1702 unp_externalize(struct mbuf
*rights
)
1704 proc_t p
= current_proc(); /* XXX */
1706 struct cmsghdr
*cm
= mtod(rights
, struct cmsghdr
*);
1707 struct fileglob
**rp
= (struct fileglob
**)(cm
+ 1);
1708 int *fds
= (int *)(cm
+ 1);
1709 struct fileproc
*fp
;
1710 struct fileglob
*fg
;
1711 int newfds
= (cm
->cmsg_len
- sizeof (*cm
)) / sizeof (int);
1717 * if the new FD's will not fit, then we free them all
1719 if (!fdavail(p
, newfds
)) {
1720 for (i
= 0; i
< newfds
; i
++) {
1722 unp_discard_fdlocked(fg
, p
);
1730 * now change each pointer to an fd in the global table to
1731 * an integer that is the index to the local fd table entry
1732 * that we set up to point to the global one we are transferring.
1733 * XXX (1) this assumes a pointer and int are the same size,
1734 * XXX or the mbuf can hold the expansion
1735 * XXX (2) allocation failures should be non-fatal
1737 for (i
= 0; i
< newfds
; i
++) {
1738 #if CONFIG_MACF_SOCKET
1740 * If receive access is denied, don't pass along
1741 * and error message, just discard the descriptor.
1743 if (mac_file_check_receive(kauth_cred_get(), *rp
)) {
1746 unp_discard_fdlocked(fg
, p
);
1750 if (fdalloc(p
, 0, &f
))
1751 panic("unp_externalize:fdalloc");
1753 MALLOC_ZONE(fp
, struct fileproc
*, sizeof (struct fileproc
),
1754 M_FILEPROC
, M_WAITOK
);
1756 panic("unp_externalize: MALLOC_ZONE");
1757 bzero(fp
, sizeof (struct fileproc
));
1761 procfdtbl_releasefd(p
, f
, fp
);
1762 (void) OSAddAtomic(-1, &unp_rights
);
1773 unp_zone
= zinit(sizeof (struct unpcb
),
1774 (nmbclusters
* sizeof (struct unpcb
)), 4096, "unpzone");
1778 LIST_INIT(&unp_dhead
);
1779 LIST_INIT(&unp_shead
);
1782 * allocate lock group attribute and group for udp pcb mutexes
1784 unp_mtx_grp_attr
= lck_grp_attr_alloc_init();
1786 unp_mtx_grp
= lck_grp_alloc_init("unp_list", unp_mtx_grp_attr
);
1788 unp_mtx_attr
= lck_attr_alloc_init();
1790 if ((unp_list_mtx
= lck_rw_alloc_init(unp_mtx_grp
,
1791 unp_mtx_attr
)) == NULL
)
1792 return; /* pretty much dead if this fails... */
1794 if ((unp_disconnect_lock
= lck_mtx_alloc_init(unp_mtx_grp
,
1795 unp_mtx_attr
)) == NULL
)
1798 if ((unp_connect_lock
= lck_mtx_alloc_init(unp_mtx_grp
,
1799 unp_mtx_attr
)) == NULL
)
1804 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
1808 * Returns: 0 Success
1810 * fdgetf_noref:EBADF
1813 unp_internalize(struct mbuf
*control
, proc_t p
)
1815 struct cmsghdr
*cm
= mtod(control
, struct cmsghdr
*);
1817 struct fileglob
**rp
;
1818 struct fileproc
*fp
;
1822 /* 64bit: cmsg_len is 'uint32_t', m_len is 'long' */
1823 if (cm
->cmsg_type
!= SCM_RIGHTS
|| cm
->cmsg_level
!= SOL_SOCKET
||
1824 (socklen_t
)cm
->cmsg_len
!= (socklen_t
)control
->m_len
) {
1827 oldfds
= (cm
->cmsg_len
- sizeof (*cm
)) / sizeof (int);
1830 fds
= (int *)(cm
+ 1);
1832 for (i
= 0; i
< oldfds
; i
++) {
1833 struct fileproc
*tmpfp
;
1834 if (((error
= fdgetf_noref(p
, fds
[i
], &tmpfp
)) != 0)) {
1837 } else if (!filetype_issendable(tmpfp
->f_fglob
->fg_type
)) {
1842 rp
= (struct fileglob
**)(cm
+ 1);
1844 /* On K64 we need to walk backwards because a fileglob * is twice the size of an fd
1845 * and doing them in-order would result in stomping over unprocessed fd's
1847 for (i
= (oldfds
- 1); i
>= 0; i
--) {
1848 (void) fdgetf_noref(p
, fds
[i
], &fp
);
1849 fg_insertuipc(fp
->f_fglob
);
1850 rp
[i
] = fp
->f_fglob
;
1851 (void) OSAddAtomic(1, &unp_rights
);
1858 static int unp_defer
, unp_gcing
, unp_gcwait
;
1859 static thread_t unp_gcthread
= NULL
;
1861 /* always called under uipc_lock */
1865 if (unp_gcthread
== current_thread())
1868 while (unp_gcing
!= 0) {
1870 msleep(&unp_gcing
, uipc_lock
, 0 , "unp_gc_wait", NULL
);
1875 __private_extern__
void
1878 struct fileglob
*fg
, *nextfg
;
1880 static struct fileglob
**extra_ref
;
1881 struct fileglob
**fpp
;
1883 int need_gcwakeup
= 0;
1885 lck_mtx_lock(uipc_lock
);
1887 lck_mtx_unlock(uipc_lock
);
1892 unp_gcthread
= current_thread();
1893 lck_mtx_unlock(uipc_lock
);
1895 * before going through all this, set all FDs to
1896 * be NOT defered and NOT externally accessible
1898 for (fg
= fmsghead
.lh_first
; fg
!= 0; fg
= fg
->f_msglist
.le_next
) {
1899 lck_mtx_lock(&fg
->fg_lock
);
1900 fg
->fg_flag
&= ~(FMARK
|FDEFER
);
1901 lck_mtx_unlock(&fg
->fg_lock
);
1904 for (fg
= fmsghead
.lh_first
; fg
!= 0;
1905 fg
= fg
->f_msglist
.le_next
) {
1906 lck_mtx_lock(&fg
->fg_lock
);
1908 * If the file is not open, skip it
1910 if (fg
->fg_count
== 0) {
1911 lck_mtx_unlock(&fg
->fg_lock
);
1915 * If we already marked it as 'defer' in a
1916 * previous pass, then try process it this time
1919 if (fg
->fg_flag
& FDEFER
) {
1920 fg
->fg_flag
&= ~FDEFER
;
1924 * if it's not defered, then check if it's
1925 * already marked.. if so skip it
1927 if (fg
->fg_flag
& FMARK
) {
1928 lck_mtx_unlock(&fg
->fg_lock
);
1932 * If all references are from messages
1933 * in transit, then skip it. it's not
1934 * externally accessible.
1936 if (fg
->fg_count
== fg
->fg_msgcount
) {
1937 lck_mtx_unlock(&fg
->fg_lock
);
1941 * If it got this far then it must be
1942 * externally accessible.
1944 fg
->fg_flag
|= FMARK
;
1947 * either it was defered, or it is externally
1948 * accessible and not already marked so.
1949 * Now check if it is possibly one of OUR sockets.
1951 if (fg
->fg_type
!= DTYPE_SOCKET
||
1952 (so
= (struct socket
*)fg
->fg_data
) == 0) {
1953 lck_mtx_unlock(&fg
->fg_lock
);
1956 if (so
->so_proto
->pr_domain
!= &localdomain
||
1957 (so
->so_proto
->pr_flags
&PR_RIGHTS
) == 0) {
1958 lck_mtx_unlock(&fg
->fg_lock
);
1963 * if this code is enabled need to run
1964 * under network funnel
1966 if (so
->so_rcv
.sb_flags
& SB_LOCK
) {
1968 * This is problematical; it's not clear
1969 * we need to wait for the sockbuf to be
1970 * unlocked (on a uniprocessor, at least),
1971 * and it's also not clear what to do
1972 * if sbwait returns an error due to receipt
1973 * of a signal. If sbwait does return
1974 * an error, we'll go into an infinite
1975 * loop. Delete all of this for now.
1977 (void) sbwait(&so
->so_rcv
);
1982 * So, Ok, it's one of our sockets and it IS externally
1983 * accessible (or was defered). Now we look
1984 * to see if we hold any file descriptors in its
1985 * message buffers. Follow those links and mark them
1986 * as accessible too.
1988 * In case a file is passed onto itself we need to
1989 * release the file lock.
1991 lck_mtx_unlock(&fg
->fg_lock
);
1993 unp_scan(so
->so_rcv
.sb_mb
, unp_mark
);
1995 } while (unp_defer
);
1997 * We grab an extra reference to each of the file table entries
1998 * that are not otherwise accessible and then free the rights
1999 * that are stored in messages on them.
2001 * The bug in the orginal code is a little tricky, so I'll describe
2002 * what's wrong with it here.
2004 * It is incorrect to simply unp_discard each entry for f_msgcount
2005 * times -- consider the case of sockets A and B that contain
2006 * references to each other. On a last close of some other socket,
2007 * we trigger a gc since the number of outstanding rights (unp_rights)
2008 * is non-zero. If during the sweep phase the gc code un_discards,
2009 * we end up doing a (full) closef on the descriptor. A closef on A
2010 * results in the following chain. Closef calls soo_close, which
2011 * calls soclose. Soclose calls first (through the switch
2012 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply
2013 * returns because the previous instance had set unp_gcing, and
2014 * we return all the way back to soclose, which marks the socket
2015 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush
2016 * to free up the rights that are queued in messages on the socket A,
2017 * i.e., the reference on B. The sorflush calls via the dom_dispose
2018 * switch unp_dispose, which unp_scans with unp_discard. This second
2019 * instance of unp_discard just calls closef on B.
2021 * Well, a similar chain occurs on B, resulting in a sorflush on B,
2022 * which results in another closef on A. Unfortunately, A is already
2023 * being closed, and the descriptor has already been marked with
2024 * SS_NOFDREF, and soclose panics at this point.
2026 * Here, we first take an extra reference to each inaccessible
2027 * descriptor. Then, we call sorflush ourself, since we know
2028 * it is a Unix domain socket anyhow. After we destroy all the
2029 * rights carried in messages, we do a last closef to get rid
2030 * of our extra reference. This is the last close, and the
2031 * unp_detach etc will shut down the socket.
2033 * 91/09/19, bsy@cs.cmu.edu
2035 extra_ref
= _MALLOC(nfiles
* sizeof (struct fileglob
*),
2036 M_FILEGLOB
, M_WAITOK
);
2037 if (extra_ref
== NULL
)
2039 for (nunref
= 0, fg
= fmsghead
.lh_first
, fpp
= extra_ref
; fg
!= 0;
2041 lck_mtx_lock(&fg
->fg_lock
);
2043 nextfg
= fg
->f_msglist
.le_next
;
2045 * If it's not open, skip it
2047 if (fg
->fg_count
== 0) {
2048 lck_mtx_unlock(&fg
->fg_lock
);
2052 * If all refs are from msgs, and it's not marked accessible
2053 * then it must be referenced from some unreachable cycle
2054 * of (shut-down) FDs, so include it in our
2055 * list of FDs to remove
2057 if (fg
->fg_count
== fg
->fg_msgcount
&& !(fg
->fg_flag
& FMARK
)) {
2062 lck_mtx_unlock(&fg
->fg_lock
);
2065 * for each FD on our hit list, do the following two things
2067 for (i
= nunref
, fpp
= extra_ref
; --i
>= 0; ++fpp
) {
2068 struct fileglob
*tfg
;
2072 if (tfg
->fg_type
== DTYPE_SOCKET
&& tfg
->fg_data
!= NULL
) {
2073 so
= (struct socket
*)(tfg
->fg_data
);
2079 socket_unlock(so
, 0);
2082 for (i
= nunref
, fpp
= extra_ref
; --i
>= 0; ++fpp
)
2083 closef_locked((struct fileproc
*)0, *fpp
, (proc_t
)NULL
);
2085 FREE((caddr_t
)extra_ref
, M_FILEGLOB
);
2087 lck_mtx_lock(uipc_lock
);
2089 unp_gcthread
= NULL
;
2091 if (unp_gcwait
!= 0) {
2095 lck_mtx_unlock(uipc_lock
);
2097 if (need_gcwakeup
!= 0)
2102 unp_dispose(struct mbuf
*m
)
2105 unp_scan(m
, unp_discard
);
2110 * Returns: 0 Success
2113 unp_listen(struct unpcb
*unp
, proc_t p
)
2115 kauth_cred_t safecred
= kauth_cred_proc_ref(p
);
2116 cru2x(safecred
, &unp
->unp_peercred
);
2117 kauth_cred_unref(&safecred
);
2118 unp
->unp_flags
|= UNP_HAVEPCCACHED
;
2122 /* should run under kernel funnel */
2124 unp_scan(struct mbuf
*m0
, void (*op
)(struct fileglob
*))
2127 struct fileglob
**rp
;
2133 for (m
= m0
; m
; m
= m
->m_next
)
2134 if (m
->m_type
== MT_CONTROL
&&
2135 (size_t)m
->m_len
>= sizeof (*cm
)) {
2136 cm
= mtod(m
, struct cmsghdr
*);
2137 if (cm
->cmsg_level
!= SOL_SOCKET
||
2138 cm
->cmsg_type
!= SCM_RIGHTS
)
2140 qfds
= (cm
->cmsg_len
- sizeof (*cm
)) /
2142 rp
= (struct fileglob
**)(cm
+ 1);
2143 for (i
= 0; i
< qfds
; i
++)
2145 break; /* XXX, but saves time */
2151 /* should run under kernel funnel */
2153 unp_mark(struct fileglob
*fg
)
2155 lck_mtx_lock(&fg
->fg_lock
);
2157 if (fg
->fg_flag
& FMARK
) {
2158 lck_mtx_unlock(&fg
->fg_lock
);
2161 fg
->fg_flag
|= (FMARK
|FDEFER
);
2163 lck_mtx_unlock(&fg
->fg_lock
);
2168 /* should run under kernel funnel */
2170 unp_discard(struct fileglob
*fg
)
2172 proc_t p
= current_proc(); /* XXX */
2174 (void) OSAddAtomic(1, &unp_disposed
);
2177 unp_discard_fdlocked(fg
, p
);
2181 unp_discard_fdlocked(struct fileglob
*fg
, proc_t p
)
2185 (void) OSAddAtomic(-1, &unp_rights
);
2186 (void) closef_locked((struct fileproc
*)0, fg
, p
);
2190 unp_lock(struct socket
*so
, int refcount
, void * lr
)
2194 lr_saved
= (void *) __builtin_return_address(0);
2198 lck_mtx_lock(((struct unpcb
*)so
->so_pcb
)->unp_mtx
);
2200 panic("unp_lock: so=%p NO PCB! lr=%p ref=0x%x\n",
2201 so
, lr_saved
, so
->so_usecount
);
2204 if (so
->so_usecount
< 0)
2205 panic("unp_lock: so=%p so_pcb=%p lr=%p ref=0x%x\n",
2206 so
, so
->so_pcb
, lr_saved
, so
->so_usecount
);
2211 so
->lock_lr
[so
->next_lock_lr
] = lr_saved
;
2212 so
->next_lock_lr
= (so
->next_lock_lr
+1) % SO_LCKDBG_MAX
;
2217 unp_unlock(struct socket
*so
, int refcount
, void * lr
)
2220 lck_mtx_t
* mutex_held
= NULL
;
2221 struct unpcb
*unp
= sotounpcb(so
);
2224 lr_saved
= (void *) __builtin_return_address(0);
2230 if (so
->so_usecount
< 0)
2231 panic("unp_unlock: so=%p usecount=%x\n", so
, so
->so_usecount
);
2232 if (so
->so_pcb
== NULL
) {
2233 panic("unp_unlock: so=%p NO PCB usecount=%x\n", so
, so
->so_usecount
);
2235 mutex_held
= ((struct unpcb
*)so
->so_pcb
)->unp_mtx
;
2237 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
2238 so
->unlock_lr
[so
->next_unlock_lr
] = lr_saved
;
2239 so
->next_unlock_lr
= (so
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
2241 if (so
->so_usecount
== 0 && (so
->so_flags
& SOF_PCBCLEARING
)) {
2242 sofreelastref(so
, 1);
2245 FREE(unp
->unp_addr
, M_SONAME
);
2247 lck_mtx_unlock(mutex_held
);
2249 lck_mtx_free(unp
->unp_mtx
, unp_mtx_grp
);
2251 unp
->unp_gencnt
= ++unp_gencnt
;
2252 zfree(unp_zone
, unp
);
2257 lck_mtx_unlock(mutex_held
);
2264 unp_getlock(struct socket
*so
, __unused
int locktype
)
2266 struct unpcb
*unp
= (struct unpcb
*)so
->so_pcb
;
2270 if (so
->so_usecount
< 0)
2271 panic("unp_getlock: so=%p usecount=%x\n", so
, so
->so_usecount
);
2272 return(unp
->unp_mtx
);
2274 panic("unp_getlock: so=%p NULL so_pcb\n", so
);
2275 return (so
->so_proto
->pr_domain
->dom_mtx
);