2 * Copyright (c) 2000-2004 Apple Computer, 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 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/domain.h>
67 #include <sys/fcntl.h>
68 #include <sys/malloc.h> /* XXX must be before <sys/file.h> */
69 #include <sys/file_internal.h>
70 #include <sys/filedesc.h>
73 #include <sys/namei.h>
74 #include <sys/proc_internal.h>
75 #include <sys/kauth.h>
76 #include <sys/protosw.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
80 #include <sys/sysctl.h>
82 #include <sys/unpcb.h>
83 #include <sys/vnode_internal.h>
84 #include <sys/kdebug.h>
86 #include <kern/zalloc.h>
87 #include <kern/locks.h>
89 #define f_msgcount f_fglob->fg_msgcount
90 #define f_cred f_fglob->fg_cred
91 #define f_ops f_fglob->fg_ops
92 #define f_offset f_fglob->fg_offset
93 #define f_data f_fglob->fg_data
94 struct zone
*unp_zone
;
95 static unp_gen_t unp_gencnt
;
96 static u_int unp_count
;
98 static lck_attr_t
*unp_mtx_attr
;
99 static lck_grp_t
*unp_mtx_grp
;
100 static lck_grp_attr_t
*unp_mtx_grp_attr
;
101 static lck_rw_t
*unp_list_mtx
;
103 extern lck_mtx_t
* uipc_lock
;
104 static struct unp_head unp_shead
, unp_dhead
;
107 * Unix communications domain.
111 * rethink name space problems
112 * need a proper out-of-band
115 static struct sockaddr sun_noname
= { sizeof(sun_noname
), AF_LOCAL
, { 0 } };
116 static ino_t unp_ino
; /* prototype for fake inode numbers */
118 static int unp_attach(struct socket
*);
119 static void unp_detach(struct unpcb
*);
120 static int unp_bind(struct unpcb
*,struct sockaddr
*, struct proc
*);
121 static int unp_connect(struct socket
*,struct sockaddr
*, struct proc
*);
122 static void unp_disconnect(struct unpcb
*);
123 static void unp_shutdown(struct unpcb
*);
124 static void unp_drop(struct unpcb
*, int);
125 static void unp_gc(void);
126 static void unp_scan(struct mbuf
*, void (*)(struct fileglob
*));
127 static void unp_mark(struct fileglob
*);
128 static void unp_discard(struct fileglob
*);
129 static void unp_discard_fdlocked(struct fileglob
*, struct proc
*);
130 static int unp_internalize(struct mbuf
*, struct proc
*);
131 static int unp_listen(struct unpcb
*, struct proc
*);
135 uipc_abort(struct socket
*so
)
137 struct unpcb
*unp
= sotounpcb(so
);
141 unp_drop(unp
, ECONNABORTED
);
148 uipc_accept(struct socket
*so
, struct sockaddr
**nam
)
150 struct unpcb
*unp
= sotounpcb(so
);
156 * Pass back name of connected socket,
157 * if it was bound and we are still connected
158 * (our peer may have closed already!).
160 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
) {
161 *nam
= dup_sockaddr((struct sockaddr
*)unp
->unp_conn
->unp_addr
,
164 *nam
= dup_sockaddr((struct sockaddr
*)&sun_noname
, 1);
170 uipc_attach(struct socket
*so
, __unused
int proto
, __unused
struct proc
*p
)
172 struct unpcb
*unp
= sotounpcb(so
);
176 return unp_attach(so
);
180 uipc_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
182 struct unpcb
*unp
= sotounpcb(so
);
187 return unp_bind(unp
, nam
, p
);
191 uipc_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
193 struct unpcb
*unp
= sotounpcb(so
);
197 return unp_connect(so
, nam
, p
);
201 uipc_connect2(struct socket
*so1
, struct socket
*so2
)
203 struct unpcb
*unp
= sotounpcb(so1
);
208 return unp_connect2(so1
, so2
);
211 /* control is EOPNOTSUPP */
214 uipc_detach(struct socket
*so
)
216 struct unpcb
*unp
= sotounpcb(so
);
226 uipc_disconnect(struct socket
*so
)
228 struct unpcb
*unp
= sotounpcb(so
);
237 uipc_listen(struct socket
*so
, __unused
struct proc
*p
)
239 struct unpcb
*unp
= sotounpcb(so
);
241 if (unp
== 0 || unp
->unp_vnode
== 0)
243 return unp_listen(unp
, p
);
247 uipc_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
249 struct unpcb
*unp
= sotounpcb(so
);
253 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
)
254 *nam
= dup_sockaddr((struct sockaddr
*)unp
->unp_conn
->unp_addr
,
260 uipc_rcvd(struct socket
*so
, __unused
int flags
)
262 struct unpcb
*unp
= sotounpcb(so
);
267 switch (so
->so_type
) {
269 panic("uipc_rcvd DGRAM?");
273 #define rcv (&so->so_rcv)
274 #define snd (&so2->so_snd)
275 if (unp
->unp_conn
== 0)
277 so2
= unp
->unp_conn
->unp_socket
;
279 * Adjust backpressure on sender
280 * and wakeup any waiting to write.
282 snd
->sb_mbmax
+= unp
->unp_mbcnt
- rcv
->sb_mbcnt
;
283 unp
->unp_mbcnt
= rcv
->sb_mbcnt
;
284 snd
->sb_hiwat
+= unp
->unp_cc
- rcv
->sb_cc
;
285 unp
->unp_cc
= rcv
->sb_cc
;
292 panic("uipc_rcvd unknown socktype");
297 /* pru_rcvoob is EOPNOTSUPP */
300 uipc_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
301 struct mbuf
*control
, struct proc
*p
)
304 struct unpcb
*unp
= sotounpcb(so
);
311 if (flags
& PRUS_OOB
) {
317 socket_unlock(so
, 0); /* release global lock to avoid deadlock (4436174) */
318 error
= unp_internalize(control
, p
);
324 switch (so
->so_type
) {
327 struct sockaddr
*from
;
334 error
= unp_connect(so
, nam
, p
);
338 if (unp
->unp_conn
== 0) {
343 so2
= unp
->unp_conn
->unp_socket
;
345 from
= (struct sockaddr
*)unp
->unp_addr
;
348 if (sbappendaddr(&so2
->so_rcv
, from
, m
, control
, &error
)) {
360 #define rcv (&so2->so_rcv)
361 #define snd (&so->so_snd)
362 /* Connect if not connected yet. */
364 * Note: A better implementation would complain
365 * if not equal to the peer's address.
367 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
369 error
= unp_connect(so
, nam
, p
);
378 if (so
->so_state
& SS_CANTSENDMORE
) {
382 if (unp
->unp_conn
== 0)
383 panic("uipc_send connected but no connection?");
384 so2
= unp
->unp_conn
->unp_socket
;
386 * Send to paired receive port, and then reduce
387 * send buffer hiwater marks to maintain backpressure.
390 if ((control
&& sbappendcontrol(rcv
, m
, control
, NULL
)) ||
395 rcv
->sb_mbcnt
- unp
->unp_conn
->unp_mbcnt
;
396 unp
->unp_conn
->unp_mbcnt
= rcv
->sb_mbcnt
;
397 snd
->sb_hiwat
-= rcv
->sb_cc
- unp
->unp_conn
->unp_cc
;
398 unp
->unp_conn
->unp_cc
= rcv
->sb_cc
;
409 panic("uipc_send unknown socktype");
413 * SEND_EOF is equivalent to a SEND followed by
416 if (flags
& PRUS_EOF
) {
421 if (control
&& error
!= 0)
422 unp_dispose(control
);
433 uipc_sense(struct socket
*so
, struct stat
*sb
)
435 struct unpcb
*unp
= sotounpcb(so
);
440 sb
->st_blksize
= so
->so_snd
.sb_hiwat
;
441 if (so
->so_type
== SOCK_STREAM
&& unp
->unp_conn
!= 0) {
442 so2
= unp
->unp_conn
->unp_socket
;
443 sb
->st_blksize
+= so2
->so_rcv
.sb_cc
;
446 if (unp
->unp_ino
== 0)
447 unp
->unp_ino
= unp_ino
++;
448 sb
->st_ino
= unp
->unp_ino
;
453 uipc_shutdown(struct socket
*so
)
455 struct unpcb
*unp
= sotounpcb(so
);
465 uipc_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
467 struct unpcb
*unp
= sotounpcb(so
);
472 *nam
= dup_sockaddr((struct sockaddr
*)unp
->unp_addr
, 1);
476 struct pr_usrreqs uipc_usrreqs
= {
477 uipc_abort
, uipc_accept
, uipc_attach
, uipc_bind
, uipc_connect
,
478 uipc_connect2
, pru_control_notsupp
, uipc_detach
, uipc_disconnect
,
479 uipc_listen
, uipc_peeraddr
, uipc_rcvd
, pru_rcvoob_notsupp
,
480 uipc_send
, uipc_sense
, uipc_shutdown
, uipc_sockaddr
,
481 sosend
, soreceive
, pru_sopoll_notsupp
487 struct sockopt
*sopt
)
489 struct unpcb
*unp
= sotounpcb(so
);
492 switch (sopt
->sopt_dir
) {
494 switch (sopt
->sopt_name
) {
496 if (unp
->unp_flags
& UNP_HAVEPC
)
497 error
= sooptcopyout(sopt
, &unp
->unp_peercred
,
498 sizeof(unp
->unp_peercred
));
500 if (so
->so_type
== SOCK_STREAM
)
520 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
521 * for stream sockets, although the total for sender and receiver is
522 * actually only PIPSIZ.
523 * Datagram sockets really use the sendspace as the maximum datagram size,
524 * and don't really want to reserve the sendspace. Their recvspace should
525 * be large enough for at least one max-size datagram plus address.
530 static u_long unpst_sendspace
= PIPSIZ
;
531 static u_long unpst_recvspace
= PIPSIZ
;
532 static u_long unpdg_sendspace
= 2*1024; /* really max datagram size */
533 static u_long unpdg_recvspace
= 4*1024;
535 static int unp_rights
; /* file descriptors in flight */
537 SYSCTL_DECL(_net_local_stream
);
538 SYSCTL_INT(_net_local_stream
, OID_AUTO
, sendspace
, CTLFLAG_RW
,
539 &unpst_sendspace
, 0, "");
540 SYSCTL_INT(_net_local_stream
, OID_AUTO
, recvspace
, CTLFLAG_RW
,
541 &unpst_recvspace
, 0, "");
542 SYSCTL_DECL(_net_local_dgram
);
543 SYSCTL_INT(_net_local_dgram
, OID_AUTO
, maxdgram
, CTLFLAG_RW
,
544 &unpdg_sendspace
, 0, "");
545 SYSCTL_INT(_net_local_dgram
, OID_AUTO
, recvspace
, CTLFLAG_RW
,
546 &unpdg_recvspace
, 0, "");
547 SYSCTL_DECL(_net_local
);
548 SYSCTL_INT(_net_local
, OID_AUTO
, inflight
, CTLFLAG_RD
, &unp_rights
, 0, "");
551 unp_attach(struct socket
*so
)
556 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
557 switch (so
->so_type
) {
560 error
= soreserve(so
, unpst_sendspace
, unpst_recvspace
);
564 error
= soreserve(so
, unpdg_sendspace
, unpdg_recvspace
);
573 unp
= (struct unpcb
*)zalloc(unp_zone
);
576 bzero(unp
, sizeof *unp
);
577 lck_rw_lock_exclusive(unp_list_mtx
);
578 LIST_INIT(&unp
->unp_refs
);
579 unp
->unp_socket
= so
;
580 unp
->unp_gencnt
= ++unp_gencnt
;
582 LIST_INSERT_HEAD(so
->so_type
== SOCK_DGRAM
? &unp_dhead
583 : &unp_shead
, unp
, unp_link
);
584 so
->so_pcb
= (caddr_t
)unp
;
585 lck_rw_done(unp_list_mtx
);
590 unp_detach(struct unpcb
*unp
)
592 lck_rw_lock_exclusive(unp_list_mtx
);
593 LIST_REMOVE(unp
, unp_link
);
594 unp
->unp_gencnt
= ++unp_gencnt
;
595 lck_rw_done(unp_list_mtx
);
597 if (unp
->unp_vnode
) {
598 struct vnode
*tvp
= unp
->unp_vnode
;
599 unp
->unp_vnode
->v_socket
= 0;
601 vnode_rele(tvp
); /* drop the usecount */
605 while (unp
->unp_refs
.lh_first
)
606 unp_drop(unp
->unp_refs
.lh_first
, ECONNRESET
);
607 soisdisconnected(unp
->unp_socket
);
608 unp
->unp_socket
->so_flags
|= SOF_PCBCLEARING
; /* makes sure we're getting dealloced */
609 unp
->unp_socket
->so_pcb
= 0;
612 * Normally the receive buffer is flushed later,
613 * in sofree, but if our receive buffer holds references
614 * to descriptors that are now garbage, we will dispose
615 * of those descriptor references after the garbage collector
616 * gets them (resulting in a "panic: closef: count < 0").
618 sorflush(unp
->unp_socket
);
622 FREE(unp
->unp_addr
, M_SONAME
);
623 zfree(unp_zone
, unp
);
629 struct sockaddr
*nam
,
632 struct sockaddr_un
*soun
= (struct sockaddr_un
*)nam
;
633 struct vnode
*vp
, *dvp
;
634 struct vnode_attr va
;
635 struct vfs_context context
;
638 char buf
[SOCK_MAXADDRLEN
];
641 context
.vc_ucred
= p
->p_ucred
; /* XXX kauth_cred_get() ??? proxy */
643 if (unp
->unp_vnode
!= NULL
)
645 namelen
= soun
->sun_len
- offsetof(struct sockaddr_un
, sun_path
);
648 strncpy(buf
, soun
->sun_path
, namelen
);
649 buf
[namelen
] = 0; /* null-terminate the string */
650 NDINIT(&nd
, CREATE
, FOLLOW
| LOCKPARENT
, UIO_SYSSPACE32
,
651 CAST_USER_ADDR_T(buf
), &context
);
652 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
662 * need to do this before the vnode_put of dvp
663 * since we may have to release an fs_nodelock
673 /* authorize before creating */
674 error
= vnode_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, &context
);
678 VATTR_SET(&va
, va_type
, VSOCK
);
679 VATTR_SET(&va
, va_mode
, (ACCESSPERMS
& ~p
->p_fd
->fd_cmask
));
681 /* create the socket */
682 error
= vn_create(dvp
, &vp
, &nd
.ni_cnd
, &va
, 0, &context
);
691 vnode_ref(vp
); /* gain a longterm reference */
692 vp
->v_socket
= unp
->unp_socket
;
694 unp
->unp_addr
= (struct sockaddr_un
*)dup_sockaddr(nam
, 1);
695 vnode_put(vp
); /* drop the iocount */
703 struct sockaddr
*nam
,
706 struct sockaddr_un
*soun
= (struct sockaddr_un
*)nam
;
708 struct socket
*so2
, *so3
;
709 struct unpcb
*unp
, *unp2
, *unp3
;
710 struct vfs_context context
;
713 char buf
[SOCK_MAXADDRLEN
];
716 context
.vc_ucred
= p
->p_ucred
; /* XXX kauth_cred_get() ??? proxy */
719 len
= nam
->sa_len
- offsetof(struct sockaddr_un
, sun_path
);
722 strncpy(buf
, soun
->sun_path
, len
);
725 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE32
, CAST_USER_ADDR_T(buf
), &context
);
732 if (vp
->v_type
!= VSOCK
) {
737 error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_WRITE_DATA
, &context
);
741 if (so2
== 0 || so2
->so_pcb
== NULL
) {
742 error
= ECONNREFUSED
;
746 /* make sure the socket can't go away while we're connecting */
749 if (so
->so_type
!= so2
->so_type
) {
755 * Check if socket was connected while we were trying to
756 * acquire the funnel.
757 * XXX - probably shouldn't return an error for SOCK_DGRAM
759 if ((so
->so_state
& SS_ISCONNECTED
) != 0) {
764 if (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) {
765 if ((so2
->so_options
& SO_ACCEPTCONN
) == 0 ||
766 (so3
= sonewconn(so2
, 0, nam
)) == 0) {
767 error
= ECONNREFUSED
;
771 unp2
= sotounpcb(so2
);
772 unp3
= sotounpcb(so3
);
774 unp3
->unp_addr
= (struct sockaddr_un
*)
775 dup_sockaddr((struct sockaddr
*)
779 * unp_peercred management:
781 * The connecter's (client's) credentials are copied
782 * from its process structure at the time of connect()
785 cru2x(p
->p_ucred
, &unp3
->unp_peercred
);
786 unp3
->unp_flags
|= UNP_HAVEPC
;
788 * The receiver's (server's) credentials are copied
789 * from the unp_peercred member of socket on which the
790 * former called listen(); unp_listen() cached that
791 * process's credentials at that time so we can use
794 KASSERT(unp2
->unp_flags
& UNP_HAVEPCCACHED
,
795 ("unp_connect: listener without cached peercred"));
796 memcpy(&unp
->unp_peercred
, &unp2
->unp_peercred
,
797 sizeof(unp
->unp_peercred
));
798 unp
->unp_flags
|= UNP_HAVEPC
;
800 so2
->so_usecount
--; /* drop reference taken on so2 */
802 so3
->so_usecount
++; /* make sure we keep it around */
804 error
= unp_connect2(so
, so2
);
808 so2
->so_usecount
--; /* release count on socket */
819 struct unpcb
*unp
= sotounpcb(so
);
822 if (so2
->so_type
!= so
->so_type
)
824 unp2
= sotounpcb(so2
);
826 /* Verify both sockets are still opened */
827 if (unp
== 0 || unp2
== 0)
830 unp
->unp_conn
= unp2
;
831 switch (so
->so_type
) {
834 LIST_INSERT_HEAD(&unp2
->unp_refs
, unp
, unp_reflink
);
839 /* This takes care of socketpair */
840 if (!(unp
->unp_flags
& UNP_HAVEPC
) && !(unp2
->unp_flags
& UNP_HAVEPC
)) {
841 cru2x(kauth_cred_get(), &unp
->unp_peercred
);
842 unp
->unp_flags
|= UNP_HAVEPC
;
844 cru2x(kauth_cred_get(), &unp2
->unp_peercred
);
845 unp2
->unp_flags
|= UNP_HAVEPC
;
847 unp2
->unp_conn
= unp
;
853 panic("unp_connect2");
859 unp_disconnect(struct unpcb
*unp
)
861 struct unpcb
*unp2
= unp
->unp_conn
;
866 switch (unp
->unp_socket
->so_type
) {
869 lck_rw_lock_exclusive(unp_list_mtx
);
870 LIST_REMOVE(unp
, unp_reflink
);
871 lck_rw_done(unp_list_mtx
);
872 unp
->unp_socket
->so_state
&= ~SS_ISCONNECTED
;
876 soisdisconnected(unp
->unp_socket
);
878 soisdisconnected(unp2
->unp_socket
);
885 unp_abort(struct unpcb
*unp
)
893 unp_pcblist SYSCTL_HANDLER_ARGS
896 struct unpcb
*unp
, **unp_list
;
899 struct unp_head
*head
;
901 lck_rw_lock_shared(unp_list_mtx
);
902 head
= ((intptr_t)arg1
== SOCK_DGRAM
? &unp_dhead
: &unp_shead
);
905 * The process of preparing the PCB list is too time-consuming and
906 * resource-intensive to repeat twice on every request.
908 if (req
->oldptr
== USER_ADDR_NULL
) {
910 req
->oldidx
= 2 * (sizeof xug
)
911 + (n
+ n
/8) * sizeof(struct xunpcb
);
912 lck_rw_done(unp_list_mtx
);
916 if (req
->newptr
!= USER_ADDR_NULL
) {
917 lck_rw_done(unp_list_mtx
);
922 * OK, now we're committed to doing something.
927 bzero(&xug
, sizeof(xug
));
928 xug
.xug_len
= sizeof xug
;
930 xug
.xug_gen
= gencnt
;
931 xug
.xug_sogen
= so_gencnt
;
932 error
= SYSCTL_OUT(req
, &xug
, sizeof xug
);
934 lck_rw_done(unp_list_mtx
);
939 * We are done if there is no pcb
942 lck_rw_done(unp_list_mtx
);
946 MALLOC(unp_list
, struct unpcb
**, n
* sizeof *unp_list
, M_TEMP
, M_WAITOK
);
948 lck_rw_done(unp_list_mtx
);
952 for (unp
= head
->lh_first
, i
= 0; unp
&& i
< n
;
953 unp
= unp
->unp_link
.le_next
) {
954 if (unp
->unp_gencnt
<= gencnt
)
957 n
= i
; /* in case we lost some during malloc */
960 for (i
= 0; i
< n
; i
++) {
962 if (unp
->unp_gencnt
<= gencnt
) {
965 bzero(&xu
, sizeof(xu
));
966 xu
.xu_len
= sizeof xu
;
967 xu
.xu_unpp
= (struct unpcb_compat
*)unp
;
969 * XXX - need more locking here to protect against
970 * connect/disconnect races for SMP.
973 bcopy(unp
->unp_addr
, &xu
.xu_addr
,
974 unp
->unp_addr
->sun_len
);
975 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
)
976 bcopy(unp
->unp_conn
->unp_addr
,
978 unp
->unp_conn
->unp_addr
->sun_len
);
979 bcopy(unp
, &xu
.xu_unp
, sizeof(xu
.xu_unp
));
980 sotoxsocket(unp
->unp_socket
, &xu
.xu_socket
);
981 error
= SYSCTL_OUT(req
, &xu
, sizeof xu
);
986 * Give the user an updated idea of our state.
987 * If the generation differs from what we told
988 * her before, she knows that something happened
989 * while we were processing this request, and it
990 * might be necessary to retry.
992 bzero(&xug
, sizeof(xug
));
993 xug
.xug_len
= sizeof xug
;
994 xug
.xug_gen
= unp_gencnt
;
995 xug
.xug_sogen
= so_gencnt
;
996 xug
.xug_count
= unp_count
;
997 error
= SYSCTL_OUT(req
, &xug
, sizeof xug
);
999 FREE(unp_list
, M_TEMP
);
1000 lck_rw_done(unp_list_mtx
);
1004 SYSCTL_PROC(_net_local_dgram
, OID_AUTO
, pcblist
, CTLFLAG_RD
,
1005 (caddr_t
)(long)SOCK_DGRAM
, 0, unp_pcblist
, "S,xunpcb",
1006 "List of active local datagram sockets");
1007 SYSCTL_PROC(_net_local_stream
, OID_AUTO
, pcblist
, CTLFLAG_RD
,
1008 (caddr_t
)(long)SOCK_STREAM
, 0, unp_pcblist
, "S,xunpcb",
1009 "List of active local stream sockets");
1012 unp_shutdown(struct unpcb
*unp
)
1016 if (unp
->unp_socket
->so_type
== SOCK_STREAM
&& unp
->unp_conn
&&
1017 (so
= unp
->unp_conn
->unp_socket
))
1026 struct socket
*so
= unp
->unp_socket
;
1028 so
->so_error
= errno
;
1029 unp_disconnect(unp
);
1041 unp_externalize(struct mbuf
*rights
)
1043 struct proc
*p
= current_proc(); /* XXX */
1045 struct cmsghdr
*cm
= mtod(rights
, struct cmsghdr
*);
1046 struct fileglob
**rp
= (struct fileglob
**)(cm
+ 1);
1047 struct fileproc
*fp
;
1048 struct fileglob
*fg
;
1049 int newfds
= (cm
->cmsg_len
- sizeof(*cm
)) / sizeof (int);
1055 * if the new FD's will not fit, then we free them all
1057 if (!fdavail(p
, newfds
)) {
1058 for (i
= 0; i
< newfds
; i
++) {
1060 unp_discard_fdlocked(fg
, p
);
1068 * now change each pointer to an fd in the global table to
1069 * an integer that is the index to the local fd table entry
1070 * that we set up to point to the global one we are transferring.
1071 * XXX this assumes a pointer and int are the same size...!
1073 for (i
= 0; i
< newfds
; i
++) {
1074 if (fdalloc(p
, 0, &f
))
1075 panic("unp_externalize");
1077 MALLOC_ZONE(fp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
1078 bzero(fp
, sizeof(struct fileproc
));
1081 p
->p_fd
->fd_ofiles
[f
] = fp
;
1083 *fdflags(p
, f
) &= ~UF_RESERVED
;
1095 unp_zone
= zinit(sizeof(struct unpcb
),
1096 (nmbclusters
* sizeof(struct unpcb
)),
1100 LIST_INIT(&unp_dhead
);
1101 LIST_INIT(&unp_shead
);
1104 * allocate lock group attribute and group for udp pcb mutexes
1106 unp_mtx_grp_attr
= lck_grp_attr_alloc_init();
1108 unp_mtx_grp
= lck_grp_alloc_init("unp_list", unp_mtx_grp_attr
);
1110 unp_mtx_attr
= lck_attr_alloc_init();
1112 if ((unp_list_mtx
= lck_rw_alloc_init(unp_mtx_grp
, unp_mtx_attr
)) == NULL
)
1113 return; /* pretty much dead if this fails... */
1118 #define MIN(a,b) (((a)<(b))?(a):(b))
1123 struct mbuf
*control
,
1126 struct cmsghdr
*cm
= mtod(control
, struct cmsghdr
*);
1127 struct fileglob
**rp
;
1128 struct fileproc
*fp
;
1129 register int i
, error
;
1131 int fdgetf_noref(proc_t
, struct fileglob
**, struct fileproc
**);
1133 if (cm
->cmsg_type
!= SCM_RIGHTS
|| cm
->cmsg_level
!= SOL_SOCKET
||
1134 cm
->cmsg_len
!= control
->m_len
) {
1137 oldfds
= (cm
->cmsg_len
- sizeof (*cm
)) / sizeof (int);
1140 rp
= (struct fileglob
**)(cm
+ 1);
1142 for (i
= 0; i
< oldfds
; i
++) {
1143 if (error
= fdgetf_noref(p
, *(int *)rp
++, (struct fileglob
**)0)) {
1148 rp
= (struct fileglob
**)(cm
+ 1);
1150 for (i
= 0; i
< oldfds
; i
++) {
1151 (void) fdgetf_noref(p
, *(int *)rp
, &fp
);
1152 fg_insertuipc(fp
->f_fglob
);
1153 *rp
++ = fp
->f_fglob
;
1161 static int unp_defer
, unp_gcing
;
1166 register struct fileglob
*fg
, *nextfg
;
1167 register struct socket
*so
;
1168 struct fileglob
**extra_ref
, **fpp
;
1171 lck_mtx_lock(uipc_lock
);
1173 lck_mtx_unlock(uipc_lock
);
1178 lck_mtx_unlock(uipc_lock
);
1180 * before going through all this, set all FDs to
1181 * be NOT defered and NOT externally accessible
1183 for (fg
= fmsghead
.lh_first
; fg
!= 0; fg
= fg
->f_msglist
.le_next
) {
1184 lck_mtx_lock(&fg
->fg_lock
);
1185 fg
->fg_flag
&= ~(FMARK
|FDEFER
);
1186 lck_mtx_unlock(&fg
->fg_lock
);
1189 for (fg
= fmsghead
.lh_first
; fg
!= 0; fg
= fg
->f_msglist
.le_next
) {
1190 lck_mtx_lock(&fg
->fg_lock
);
1192 * If the file is not open, skip it
1194 if (fg
->fg_count
== 0) {
1195 lck_mtx_unlock(&fg
->fg_lock
);
1199 * If we already marked it as 'defer' in a
1200 * previous pass, then try process it this time
1203 if (fg
->fg_flag
& FDEFER
) {
1204 fg
->fg_flag
&= ~FDEFER
;
1208 * if it's not defered, then check if it's
1209 * already marked.. if so skip it
1211 if (fg
->fg_flag
& FMARK
){
1212 lck_mtx_unlock(&fg
->fg_lock
);
1216 * If all references are from messages
1217 * in transit, then skip it. it's not
1218 * externally accessible.
1220 if (fg
->fg_count
== fg
->fg_msgcount
) {
1221 lck_mtx_unlock(&fg
->fg_lock
);
1225 * If it got this far then it must be
1226 * externally accessible.
1228 fg
->fg_flag
|= FMARK
;
1231 * either it was defered, or it is externally
1232 * accessible and not already marked so.
1233 * Now check if it is possibly one of OUR sockets.
1235 if (fg
->fg_type
!= DTYPE_SOCKET
||
1236 (so
= (struct socket
*)fg
->fg_data
) == 0) {
1237 lck_mtx_unlock(&fg
->fg_lock
);
1240 if (so
->so_proto
->pr_domain
!= &localdomain
||
1241 (so
->so_proto
->pr_flags
&PR_RIGHTS
) == 0) {
1242 lck_mtx_unlock(&fg
->fg_lock
);
1246 /* if this code is enabled need to run under network funnel */
1247 if (so
->so_rcv
.sb_flags
& SB_LOCK
) {
1249 * This is problematical; it's not clear
1250 * we need to wait for the sockbuf to be
1251 * unlocked (on a uniprocessor, at least),
1252 * and it's also not clear what to do
1253 * if sbwait returns an error due to receipt
1254 * of a signal. If sbwait does return
1255 * an error, we'll go into an infinite
1256 * loop. Delete all of this for now.
1258 (void) sbwait(&so
->so_rcv
);
1263 * So, Ok, it's one of our sockets and it IS externally
1264 * accessible (or was defered). Now we look
1265 * to see if we hold any file descriptors in its
1266 * message buffers. Follow those links and mark them
1267 * as accessible too.
1269 unp_scan(so
->so_rcv
.sb_mb
, unp_mark
);
1270 lck_mtx_unlock(&fg
->fg_lock
);
1272 } while (unp_defer
);
1274 * We grab an extra reference to each of the file table entries
1275 * that are not otherwise accessible and then free the rights
1276 * that are stored in messages on them.
1278 * The bug in the orginal code is a little tricky, so I'll describe
1279 * what's wrong with it here.
1281 * It is incorrect to simply unp_discard each entry for f_msgcount
1282 * times -- consider the case of sockets A and B that contain
1283 * references to each other. On a last close of some other socket,
1284 * we trigger a gc since the number of outstanding rights (unp_rights)
1285 * is non-zero. If during the sweep phase the gc code un_discards,
1286 * we end up doing a (full) closef on the descriptor. A closef on A
1287 * results in the following chain. Closef calls soo_close, which
1288 * calls soclose. Soclose calls first (through the switch
1289 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply
1290 * returns because the previous instance had set unp_gcing, and
1291 * we return all the way back to soclose, which marks the socket
1292 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush
1293 * to free up the rights that are queued in messages on the socket A,
1294 * i.e., the reference on B. The sorflush calls via the dom_dispose
1295 * switch unp_dispose, which unp_scans with unp_discard. This second
1296 * instance of unp_discard just calls closef on B.
1298 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1299 * which results in another closef on A. Unfortunately, A is already
1300 * being closed, and the descriptor has already been marked with
1301 * SS_NOFDREF, and soclose panics at this point.
1303 * Here, we first take an extra reference to each inaccessible
1304 * descriptor. Then, we call sorflush ourself, since we know
1305 * it is a Unix domain socket anyhow. After we destroy all the
1306 * rights carried in messages, we do a last closef to get rid
1307 * of our extra reference. This is the last close, and the
1308 * unp_detach etc will shut down the socket.
1310 * 91/09/19, bsy@cs.cmu.edu
1312 extra_ref
= _MALLOC(nfiles
* sizeof(struct fileglob
*), M_FILEGLOB
, M_WAITOK
);
1313 for (nunref
= 0, fg
= fmsghead
.lh_first
, fpp
= extra_ref
; fg
!= 0;
1315 lck_mtx_lock(&fg
->fg_lock
);
1317 nextfg
= fg
->f_msglist
.le_next
;
1319 * If it's not open, skip it
1321 if (fg
->fg_count
== 0) {
1322 lck_mtx_unlock(&fg
->fg_lock
);
1326 * If all refs are from msgs, and it's not marked accessible
1327 * then it must be referenced from some unreachable cycle
1328 * of (shut-down) FDs, so include it in our
1329 * list of FDs to remove
1331 if (fg
->fg_count
== fg
->fg_msgcount
&& !(fg
->fg_flag
& FMARK
)) {
1336 lck_mtx_unlock(&fg
->fg_lock
);
1339 * for each FD on our hit list, do the following two things
1341 for (i
= nunref
, fpp
= extra_ref
; --i
>= 0; ++fpp
) {
1342 struct fileglob
*tfg
;
1346 if (tfg
->fg_type
== DTYPE_SOCKET
&& tfg
->fg_data
!= NULL
) {
1347 sorflush((struct socket
*)(tfg
->fg_data
));
1350 for (i
= nunref
, fpp
= extra_ref
; --i
>= 0; ++fpp
)
1351 closef_locked((struct fileproc
*)0, *fpp
, (struct proc
*) NULL
);
1353 FREE((caddr_t
)extra_ref
, M_FILEGLOB
);
1358 unp_dispose(struct mbuf
*m
)
1362 unp_scan(m
, unp_discard
);
1372 cru2x(p
->p_ucred
, &unp
->unp_peercred
);
1373 unp
->unp_flags
|= UNP_HAVEPCCACHED
;
1377 /* should run under kernel funnel */
1381 void (*op
)(struct fileglob
*))
1384 struct fileglob
**rp
;
1390 for (m
= m0
; m
; m
= m
->m_next
)
1391 if (m
->m_type
== MT_CONTROL
&&
1392 (size_t) m
->m_len
>= sizeof(*cm
)) {
1393 cm
= mtod(m
, struct cmsghdr
*);
1394 if (cm
->cmsg_level
!= SOL_SOCKET
||
1395 cm
->cmsg_type
!= SCM_RIGHTS
)
1397 qfds
= (cm
->cmsg_len
- sizeof *cm
)
1398 / sizeof (struct fileglob
*);
1399 rp
= (struct fileglob
**)(cm
+ 1);
1400 for (i
= 0; i
< qfds
; i
++)
1402 break; /* XXX, but saves time */
1408 /* should run under kernel funnel */
1410 unp_mark(struct fileglob
*fg
)
1412 lck_mtx_lock(&fg
->fg_lock
);
1414 if (fg
->fg_flag
& FMARK
) {
1415 lck_mtx_unlock(&fg
->fg_lock
);
1418 fg
->fg_flag
|= (FMARK
|FDEFER
);
1420 lck_mtx_unlock(&fg
->fg_lock
);
1425 /* should run under kernel funnel */
1428 struct fileglob
*fg
;
1430 struct proc
*p
= current_proc(); /* XXX */
1433 unp_discard_fdlocked(fg
, p
);
1437 unp_discard_fdlocked(fg
, p
)
1438 struct fileglob
*fg
;
1445 (void) closef_locked((struct fileproc
*)0, fg
, p
);