2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1982, 1986, 1989, 1991, 1993
32 * The Regents of the University of California. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/kernel.h>
68 #include <sys/domain.h>
69 #include <sys/fcntl.h>
70 #include <sys/malloc.h> /* XXX must be before <sys/file.h> */
71 #include <sys/file_internal.h>
72 #include <sys/filedesc.h>
75 #include <sys/namei.h>
76 #include <sys/proc_internal.h>
77 #include <sys/kauth.h>
78 #include <sys/protosw.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
82 #include <sys/sysctl.h>
84 #include <sys/unpcb.h>
85 #include <sys/vnode_internal.h>
86 #include <sys/kdebug.h>
88 #include <kern/zalloc.h>
89 #include <kern/locks.h>
91 #define f_msgcount f_fglob->fg_msgcount
92 #define f_cred f_fglob->fg_cred
93 #define f_ops f_fglob->fg_ops
94 #define f_offset f_fglob->fg_offset
95 #define f_data f_fglob->fg_data
96 struct zone
*unp_zone
;
97 static unp_gen_t unp_gencnt
;
98 static u_int unp_count
;
100 static lck_attr_t
*unp_mtx_attr
;
101 static lck_grp_t
*unp_mtx_grp
;
102 static lck_grp_attr_t
*unp_mtx_grp_attr
;
103 static lck_rw_t
*unp_list_mtx
;
105 extern lck_mtx_t
* uipc_lock
;
106 static struct unp_head unp_shead
, unp_dhead
;
109 * Unix communications domain.
113 * rethink name space problems
114 * need a proper out-of-band
117 static struct sockaddr sun_noname
= { sizeof(sun_noname
), AF_LOCAL
, { 0 } };
118 static ino_t unp_ino
; /* prototype for fake inode numbers */
120 static int unp_attach(struct socket
*);
121 static void unp_detach(struct unpcb
*);
122 static int unp_bind(struct unpcb
*,struct sockaddr
*, struct proc
*);
123 static int unp_connect(struct socket
*,struct sockaddr
*, struct proc
*);
124 static void unp_disconnect(struct unpcb
*);
125 static void unp_shutdown(struct unpcb
*);
126 static void unp_drop(struct unpcb
*, int);
127 static void unp_gc(void);
128 static void unp_scan(struct mbuf
*, void (*)(struct fileglob
*));
129 static void unp_mark(struct fileglob
*);
130 static void unp_discard(struct fileglob
*);
131 static void unp_discard_fdlocked(struct fileglob
*, struct proc
*);
132 static int unp_internalize(struct mbuf
*, struct proc
*);
133 static int unp_listen(struct unpcb
*, struct proc
*);
137 uipc_abort(struct socket
*so
)
139 struct unpcb
*unp
= sotounpcb(so
);
143 unp_drop(unp
, ECONNABORTED
);
150 uipc_accept(struct socket
*so
, struct sockaddr
**nam
)
152 struct unpcb
*unp
= sotounpcb(so
);
158 * Pass back name of connected socket,
159 * if it was bound and we are still connected
160 * (our peer may have closed already!).
162 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
) {
163 *nam
= dup_sockaddr((struct sockaddr
*)unp
->unp_conn
->unp_addr
,
166 *nam
= dup_sockaddr((struct sockaddr
*)&sun_noname
, 1);
172 uipc_attach(struct socket
*so
, __unused
int proto
, __unused
struct proc
*p
)
174 struct unpcb
*unp
= sotounpcb(so
);
178 return unp_attach(so
);
182 uipc_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
184 struct unpcb
*unp
= sotounpcb(so
);
189 return unp_bind(unp
, nam
, p
);
193 uipc_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
195 struct unpcb
*unp
= sotounpcb(so
);
199 return unp_connect(so
, nam
, p
);
203 uipc_connect2(struct socket
*so1
, struct socket
*so2
)
205 struct unpcb
*unp
= sotounpcb(so1
);
210 return unp_connect2(so1
, so2
);
213 /* control is EOPNOTSUPP */
216 uipc_detach(struct socket
*so
)
218 struct unpcb
*unp
= sotounpcb(so
);
228 uipc_disconnect(struct socket
*so
)
230 struct unpcb
*unp
= sotounpcb(so
);
239 uipc_listen(struct socket
*so
, __unused
struct proc
*p
)
241 struct unpcb
*unp
= sotounpcb(so
);
243 if (unp
== 0 || unp
->unp_vnode
== 0)
245 return unp_listen(unp
, p
);
249 uipc_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
251 struct unpcb
*unp
= sotounpcb(so
);
255 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
)
256 *nam
= dup_sockaddr((struct sockaddr
*)unp
->unp_conn
->unp_addr
,
262 uipc_rcvd(struct socket
*so
, __unused
int flags
)
264 struct unpcb
*unp
= sotounpcb(so
);
269 switch (so
->so_type
) {
271 panic("uipc_rcvd DGRAM?");
275 #define rcv (&so->so_rcv)
276 #define snd (&so2->so_snd)
277 if (unp
->unp_conn
== 0)
279 so2
= unp
->unp_conn
->unp_socket
;
281 * Adjust backpressure on sender
282 * and wakeup any waiting to write.
284 snd
->sb_mbmax
+= unp
->unp_mbcnt
- rcv
->sb_mbcnt
;
285 unp
->unp_mbcnt
= rcv
->sb_mbcnt
;
286 snd
->sb_hiwat
+= unp
->unp_cc
- rcv
->sb_cc
;
287 unp
->unp_cc
= rcv
->sb_cc
;
294 panic("uipc_rcvd unknown socktype");
299 /* pru_rcvoob is EOPNOTSUPP */
302 uipc_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
303 struct mbuf
*control
, struct proc
*p
)
306 struct unpcb
*unp
= sotounpcb(so
);
313 if (flags
& PRUS_OOB
) {
319 socket_unlock(so
, 0); /* release global lock to avoid deadlock (4436174) */
320 error
= unp_internalize(control
, p
);
326 switch (so
->so_type
) {
329 struct sockaddr
*from
;
336 error
= unp_connect(so
, nam
, p
);
340 if (unp
->unp_conn
== 0) {
345 so2
= unp
->unp_conn
->unp_socket
;
347 from
= (struct sockaddr
*)unp
->unp_addr
;
350 if (sbappendaddr(&so2
->so_rcv
, from
, m
, control
, &error
)) {
362 #define rcv (&so2->so_rcv)
363 #define snd (&so->so_snd)
364 /* Connect if not connected yet. */
366 * Note: A better implementation would complain
367 * if not equal to the peer's address.
369 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
371 error
= unp_connect(so
, nam
, p
);
380 if (so
->so_state
& SS_CANTSENDMORE
) {
384 if (unp
->unp_conn
== 0)
385 panic("uipc_send connected but no connection?");
386 so2
= unp
->unp_conn
->unp_socket
;
388 * Send to paired receive port, and then reduce
389 * send buffer hiwater marks to maintain backpressure.
392 if ((control
&& sbappendcontrol(rcv
, m
, control
, NULL
)) ||
397 rcv
->sb_mbcnt
- unp
->unp_conn
->unp_mbcnt
;
398 unp
->unp_conn
->unp_mbcnt
= rcv
->sb_mbcnt
;
399 snd
->sb_hiwat
-= rcv
->sb_cc
- unp
->unp_conn
->unp_cc
;
400 unp
->unp_conn
->unp_cc
= rcv
->sb_cc
;
411 panic("uipc_send unknown socktype");
415 * SEND_EOF is equivalent to a SEND followed by
418 if (flags
& PRUS_EOF
) {
423 if (control
&& error
!= 0)
424 unp_dispose(control
);
435 uipc_sense(struct socket
*so
, struct stat
*sb
)
437 struct unpcb
*unp
= sotounpcb(so
);
442 sb
->st_blksize
= so
->so_snd
.sb_hiwat
;
443 if (so
->so_type
== SOCK_STREAM
&& unp
->unp_conn
!= 0) {
444 so2
= unp
->unp_conn
->unp_socket
;
445 sb
->st_blksize
+= so2
->so_rcv
.sb_cc
;
448 if (unp
->unp_ino
== 0)
449 unp
->unp_ino
= unp_ino
++;
450 sb
->st_ino
= unp
->unp_ino
;
455 uipc_shutdown(struct socket
*so
)
457 struct unpcb
*unp
= sotounpcb(so
);
467 uipc_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
469 struct unpcb
*unp
= sotounpcb(so
);
474 *nam
= dup_sockaddr((struct sockaddr
*)unp
->unp_addr
, 1);
478 struct pr_usrreqs uipc_usrreqs
= {
479 uipc_abort
, uipc_accept
, uipc_attach
, uipc_bind
, uipc_connect
,
480 uipc_connect2
, pru_control_notsupp
, uipc_detach
, uipc_disconnect
,
481 uipc_listen
, uipc_peeraddr
, uipc_rcvd
, pru_rcvoob_notsupp
,
482 uipc_send
, uipc_sense
, uipc_shutdown
, uipc_sockaddr
,
483 sosend
, soreceive
, pru_sopoll_notsupp
489 struct sockopt
*sopt
)
491 struct unpcb
*unp
= sotounpcb(so
);
494 switch (sopt
->sopt_dir
) {
496 switch (sopt
->sopt_name
) {
498 if (unp
->unp_flags
& UNP_HAVEPC
)
499 error
= sooptcopyout(sopt
, &unp
->unp_peercred
,
500 sizeof(unp
->unp_peercred
));
502 if (so
->so_type
== SOCK_STREAM
)
522 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
523 * for stream sockets, although the total for sender and receiver is
524 * actually only PIPSIZ.
525 * Datagram sockets really use the sendspace as the maximum datagram size,
526 * and don't really want to reserve the sendspace. Their recvspace should
527 * be large enough for at least one max-size datagram plus address.
532 static u_long unpst_sendspace
= PIPSIZ
;
533 static u_long unpst_recvspace
= PIPSIZ
;
534 static u_long unpdg_sendspace
= 2*1024; /* really max datagram size */
535 static u_long unpdg_recvspace
= 4*1024;
537 static int unp_rights
; /* file descriptors in flight */
539 SYSCTL_DECL(_net_local_stream
);
540 SYSCTL_INT(_net_local_stream
, OID_AUTO
, sendspace
, CTLFLAG_RW
,
541 &unpst_sendspace
, 0, "");
542 SYSCTL_INT(_net_local_stream
, OID_AUTO
, recvspace
, CTLFLAG_RW
,
543 &unpst_recvspace
, 0, "");
544 SYSCTL_DECL(_net_local_dgram
);
545 SYSCTL_INT(_net_local_dgram
, OID_AUTO
, maxdgram
, CTLFLAG_RW
,
546 &unpdg_sendspace
, 0, "");
547 SYSCTL_INT(_net_local_dgram
, OID_AUTO
, recvspace
, CTLFLAG_RW
,
548 &unpdg_recvspace
, 0, "");
549 SYSCTL_DECL(_net_local
);
550 SYSCTL_INT(_net_local
, OID_AUTO
, inflight
, CTLFLAG_RD
, &unp_rights
, 0, "");
553 unp_attach(struct socket
*so
)
558 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
559 switch (so
->so_type
) {
562 error
= soreserve(so
, unpst_sendspace
, unpst_recvspace
);
566 error
= soreserve(so
, unpdg_sendspace
, unpdg_recvspace
);
575 unp
= (struct unpcb
*)zalloc(unp_zone
);
578 bzero(unp
, sizeof *unp
);
579 lck_rw_lock_exclusive(unp_list_mtx
);
580 LIST_INIT(&unp
->unp_refs
);
581 unp
->unp_socket
= so
;
582 unp
->unp_gencnt
= ++unp_gencnt
;
584 LIST_INSERT_HEAD(so
->so_type
== SOCK_DGRAM
? &unp_dhead
585 : &unp_shead
, unp
, unp_link
);
586 so
->so_pcb
= (caddr_t
)unp
;
587 lck_rw_done(unp_list_mtx
);
592 unp_detach(struct unpcb
*unp
)
594 lck_rw_lock_exclusive(unp_list_mtx
);
595 LIST_REMOVE(unp
, unp_link
);
596 unp
->unp_gencnt
= ++unp_gencnt
;
597 lck_rw_done(unp_list_mtx
);
599 if (unp
->unp_vnode
) {
600 struct vnode
*tvp
= unp
->unp_vnode
;
601 unp
->unp_vnode
->v_socket
= 0;
603 vnode_rele(tvp
); /* drop the usecount */
607 while (unp
->unp_refs
.lh_first
)
608 unp_drop(unp
->unp_refs
.lh_first
, ECONNRESET
);
609 soisdisconnected(unp
->unp_socket
);
610 unp
->unp_socket
->so_flags
|= SOF_PCBCLEARING
; /* makes sure we're getting dealloced */
611 unp
->unp_socket
->so_pcb
= 0;
614 * Normally the receive buffer is flushed later,
615 * in sofree, but if our receive buffer holds references
616 * to descriptors that are now garbage, we will dispose
617 * of those descriptor references after the garbage collector
618 * gets them (resulting in a "panic: closef: count < 0").
620 sorflush(unp
->unp_socket
);
624 FREE(unp
->unp_addr
, M_SONAME
);
625 zfree(unp_zone
, unp
);
631 struct sockaddr
*nam
,
634 struct sockaddr_un
*soun
= (struct sockaddr_un
*)nam
;
635 struct vnode
*vp
, *dvp
;
636 struct vnode_attr va
;
637 struct vfs_context context
;
640 char buf
[SOCK_MAXADDRLEN
];
643 context
.vc_ucred
= p
->p_ucred
; /* XXX kauth_cred_get() ??? proxy */
645 if (unp
->unp_vnode
!= NULL
)
647 namelen
= soun
->sun_len
- offsetof(struct sockaddr_un
, sun_path
);
650 strncpy(buf
, soun
->sun_path
, namelen
);
651 buf
[namelen
] = 0; /* null-terminate the string */
652 NDINIT(&nd
, CREATE
, FOLLOW
| LOCKPARENT
, UIO_SYSSPACE32
,
653 CAST_USER_ADDR_T(buf
), &context
);
654 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
664 * need to do this before the vnode_put of dvp
665 * since we may have to release an fs_nodelock
675 /* authorize before creating */
676 error
= vnode_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, &context
);
680 VATTR_SET(&va
, va_type
, VSOCK
);
681 VATTR_SET(&va
, va_mode
, (ACCESSPERMS
& ~p
->p_fd
->fd_cmask
));
683 /* create the socket */
684 error
= vn_create(dvp
, &vp
, &nd
.ni_cnd
, &va
, 0, &context
);
693 vnode_ref(vp
); /* gain a longterm reference */
694 vp
->v_socket
= unp
->unp_socket
;
696 unp
->unp_addr
= (struct sockaddr_un
*)dup_sockaddr(nam
, 1);
697 vnode_put(vp
); /* drop the iocount */
705 struct sockaddr
*nam
,
708 struct sockaddr_un
*soun
= (struct sockaddr_un
*)nam
;
710 struct socket
*so2
, *so3
;
711 struct unpcb
*unp
, *unp2
, *unp3
;
712 struct vfs_context context
;
715 char buf
[SOCK_MAXADDRLEN
];
718 context
.vc_ucred
= p
->p_ucred
; /* XXX kauth_cred_get() ??? proxy */
721 len
= nam
->sa_len
- offsetof(struct sockaddr_un
, sun_path
);
724 strncpy(buf
, soun
->sun_path
, len
);
727 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE32
, CAST_USER_ADDR_T(buf
), &context
);
734 if (vp
->v_type
!= VSOCK
) {
739 error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_WRITE_DATA
, &context
);
743 if (so2
== 0 || so2
->so_pcb
== NULL
) {
744 error
= ECONNREFUSED
;
748 /* make sure the socket can't go away while we're connecting */
751 if (so
->so_type
!= so2
->so_type
) {
757 * Check if socket was connected while we were trying to
758 * acquire the funnel.
759 * XXX - probably shouldn't return an error for SOCK_DGRAM
761 if ((so
->so_state
& SS_ISCONNECTED
) != 0) {
766 if (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) {
767 if ((so2
->so_options
& SO_ACCEPTCONN
) == 0 ||
768 (so3
= sonewconn(so2
, 0, nam
)) == 0) {
769 error
= ECONNREFUSED
;
773 unp2
= sotounpcb(so2
);
774 unp3
= sotounpcb(so3
);
776 unp3
->unp_addr
= (struct sockaddr_un
*)
777 dup_sockaddr((struct sockaddr
*)
781 * unp_peercred management:
783 * The connecter's (client's) credentials are copied
784 * from its process structure at the time of connect()
787 cru2x(p
->p_ucred
, &unp3
->unp_peercred
);
788 unp3
->unp_flags
|= UNP_HAVEPC
;
790 * The receiver's (server's) credentials are copied
791 * from the unp_peercred member of socket on which the
792 * former called listen(); unp_listen() cached that
793 * process's credentials at that time so we can use
796 KASSERT(unp2
->unp_flags
& UNP_HAVEPCCACHED
,
797 ("unp_connect: listener without cached peercred"));
798 memcpy(&unp
->unp_peercred
, &unp2
->unp_peercred
,
799 sizeof(unp
->unp_peercred
));
800 unp
->unp_flags
|= UNP_HAVEPC
;
802 so2
->so_usecount
--; /* drop reference taken on so2 */
804 so3
->so_usecount
++; /* make sure we keep it around */
806 error
= unp_connect2(so
, so2
);
810 so2
->so_usecount
--; /* release count on socket */
821 struct unpcb
*unp
= sotounpcb(so
);
824 if (so2
->so_type
!= so
->so_type
)
826 unp2
= sotounpcb(so2
);
828 /* Verify both sockets are still opened */
829 if (unp
== 0 || unp2
== 0)
832 unp
->unp_conn
= unp2
;
833 switch (so
->so_type
) {
836 LIST_INSERT_HEAD(&unp2
->unp_refs
, unp
, unp_reflink
);
841 /* This takes care of socketpair */
842 if (!(unp
->unp_flags
& UNP_HAVEPC
) && !(unp2
->unp_flags
& UNP_HAVEPC
)) {
843 cru2x(kauth_cred_get(), &unp
->unp_peercred
);
844 unp
->unp_flags
|= UNP_HAVEPC
;
846 cru2x(kauth_cred_get(), &unp2
->unp_peercred
);
847 unp2
->unp_flags
|= UNP_HAVEPC
;
849 unp2
->unp_conn
= unp
;
855 panic("unp_connect2");
861 unp_disconnect(struct unpcb
*unp
)
863 struct unpcb
*unp2
= unp
->unp_conn
;
868 switch (unp
->unp_socket
->so_type
) {
871 lck_rw_lock_exclusive(unp_list_mtx
);
872 LIST_REMOVE(unp
, unp_reflink
);
873 lck_rw_done(unp_list_mtx
);
874 unp
->unp_socket
->so_state
&= ~SS_ISCONNECTED
;
878 soisdisconnected(unp
->unp_socket
);
880 soisdisconnected(unp2
->unp_socket
);
887 unp_abort(struct unpcb
*unp
)
895 unp_pcblist SYSCTL_HANDLER_ARGS
898 struct unpcb
*unp
, **unp_list
;
901 struct unp_head
*head
;
903 lck_rw_lock_shared(unp_list_mtx
);
904 head
= ((intptr_t)arg1
== SOCK_DGRAM
? &unp_dhead
: &unp_shead
);
907 * The process of preparing the PCB list is too time-consuming and
908 * resource-intensive to repeat twice on every request.
910 if (req
->oldptr
== USER_ADDR_NULL
) {
912 req
->oldidx
= 2 * (sizeof xug
)
913 + (n
+ n
/8) * sizeof(struct xunpcb
);
914 lck_rw_done(unp_list_mtx
);
918 if (req
->newptr
!= USER_ADDR_NULL
) {
919 lck_rw_done(unp_list_mtx
);
924 * OK, now we're committed to doing something.
929 bzero(&xug
, sizeof(xug
));
930 xug
.xug_len
= sizeof xug
;
932 xug
.xug_gen
= gencnt
;
933 xug
.xug_sogen
= so_gencnt
;
934 error
= SYSCTL_OUT(req
, &xug
, sizeof xug
);
936 lck_rw_done(unp_list_mtx
);
941 * We are done if there is no pcb
944 lck_rw_done(unp_list_mtx
);
948 MALLOC(unp_list
, struct unpcb
**, n
* sizeof *unp_list
, M_TEMP
, M_WAITOK
);
950 lck_rw_done(unp_list_mtx
);
954 for (unp
= head
->lh_first
, i
= 0; unp
&& i
< n
;
955 unp
= unp
->unp_link
.le_next
) {
956 if (unp
->unp_gencnt
<= gencnt
)
959 n
= i
; /* in case we lost some during malloc */
962 for (i
= 0; i
< n
; i
++) {
964 if (unp
->unp_gencnt
<= gencnt
) {
967 bzero(&xu
, sizeof(xu
));
968 xu
.xu_len
= sizeof xu
;
969 xu
.xu_unpp
= (struct unpcb_compat
*)unp
;
971 * XXX - need more locking here to protect against
972 * connect/disconnect races for SMP.
975 bcopy(unp
->unp_addr
, &xu
.xu_addr
,
976 unp
->unp_addr
->sun_len
);
977 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
)
978 bcopy(unp
->unp_conn
->unp_addr
,
980 unp
->unp_conn
->unp_addr
->sun_len
);
981 bcopy(unp
, &xu
.xu_unp
, sizeof(xu
.xu_unp
));
982 sotoxsocket(unp
->unp_socket
, &xu
.xu_socket
);
983 error
= SYSCTL_OUT(req
, &xu
, sizeof xu
);
988 * Give the user an updated idea of our state.
989 * If the generation differs from what we told
990 * her before, she knows that something happened
991 * while we were processing this request, and it
992 * might be necessary to retry.
994 bzero(&xug
, sizeof(xug
));
995 xug
.xug_len
= sizeof xug
;
996 xug
.xug_gen
= unp_gencnt
;
997 xug
.xug_sogen
= so_gencnt
;
998 xug
.xug_count
= unp_count
;
999 error
= SYSCTL_OUT(req
, &xug
, sizeof xug
);
1001 FREE(unp_list
, M_TEMP
);
1002 lck_rw_done(unp_list_mtx
);
1006 SYSCTL_PROC(_net_local_dgram
, OID_AUTO
, pcblist
, CTLFLAG_RD
,
1007 (caddr_t
)(long)SOCK_DGRAM
, 0, unp_pcblist
, "S,xunpcb",
1008 "List of active local datagram sockets");
1009 SYSCTL_PROC(_net_local_stream
, OID_AUTO
, pcblist
, CTLFLAG_RD
,
1010 (caddr_t
)(long)SOCK_STREAM
, 0, unp_pcblist
, "S,xunpcb",
1011 "List of active local stream sockets");
1014 unp_shutdown(struct unpcb
*unp
)
1018 if (unp
->unp_socket
->so_type
== SOCK_STREAM
&& unp
->unp_conn
&&
1019 (so
= unp
->unp_conn
->unp_socket
))
1028 struct socket
*so
= unp
->unp_socket
;
1030 so
->so_error
= errno
;
1031 unp_disconnect(unp
);
1043 unp_externalize(struct mbuf
*rights
)
1045 struct proc
*p
= current_proc(); /* XXX */
1047 struct cmsghdr
*cm
= mtod(rights
, struct cmsghdr
*);
1048 struct fileglob
**rp
= (struct fileglob
**)(cm
+ 1);
1049 struct fileproc
*fp
;
1050 struct fileglob
*fg
;
1051 int newfds
= (cm
->cmsg_len
- sizeof(*cm
)) / sizeof (int);
1057 * if the new FD's will not fit, then we free them all
1059 if (!fdavail(p
, newfds
)) {
1060 for (i
= 0; i
< newfds
; i
++) {
1062 unp_discard_fdlocked(fg
, p
);
1070 * now change each pointer to an fd in the global table to
1071 * an integer that is the index to the local fd table entry
1072 * that we set up to point to the global one we are transferring.
1073 * XXX this assumes a pointer and int are the same size...!
1075 for (i
= 0; i
< newfds
; i
++) {
1076 if (fdalloc(p
, 0, &f
))
1077 panic("unp_externalize");
1079 MALLOC_ZONE(fp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
1080 bzero(fp
, sizeof(struct fileproc
));
1083 p
->p_fd
->fd_ofiles
[f
] = fp
;
1085 *fdflags(p
, f
) &= ~UF_RESERVED
;
1097 unp_zone
= zinit(sizeof(struct unpcb
),
1098 (nmbclusters
* sizeof(struct unpcb
)),
1102 LIST_INIT(&unp_dhead
);
1103 LIST_INIT(&unp_shead
);
1106 * allocate lock group attribute and group for udp pcb mutexes
1108 unp_mtx_grp_attr
= lck_grp_attr_alloc_init();
1110 unp_mtx_grp
= lck_grp_alloc_init("unp_list", unp_mtx_grp_attr
);
1112 unp_mtx_attr
= lck_attr_alloc_init();
1114 if ((unp_list_mtx
= lck_rw_alloc_init(unp_mtx_grp
, unp_mtx_attr
)) == NULL
)
1115 return; /* pretty much dead if this fails... */
1120 #define MIN(a,b) (((a)<(b))?(a):(b))
1125 struct mbuf
*control
,
1128 struct cmsghdr
*cm
= mtod(control
, struct cmsghdr
*);
1129 struct fileglob
**rp
;
1130 struct fileproc
*fp
;
1131 register int i
, error
;
1133 int fdgetf_noref(proc_t
, struct fileglob
**, struct fileproc
**);
1135 if (cm
->cmsg_type
!= SCM_RIGHTS
|| cm
->cmsg_level
!= SOL_SOCKET
||
1136 cm
->cmsg_len
!= control
->m_len
) {
1139 oldfds
= (cm
->cmsg_len
- sizeof (*cm
)) / sizeof (int);
1142 rp
= (struct fileglob
**)(cm
+ 1);
1144 for (i
= 0; i
< oldfds
; i
++) {
1145 if (error
= fdgetf_noref(p
, *(int *)rp
++, (struct fileglob
**)0)) {
1150 rp
= (struct fileglob
**)(cm
+ 1);
1152 for (i
= 0; i
< oldfds
; i
++) {
1153 (void) fdgetf_noref(p
, *(int *)rp
, &fp
);
1154 fg_insertuipc(fp
->f_fglob
);
1155 *rp
++ = fp
->f_fglob
;
1163 static int unp_defer
, unp_gcing
;
1168 register struct fileglob
*fg
, *nextfg
;
1169 register struct socket
*so
;
1170 struct fileglob
**extra_ref
, **fpp
;
1173 lck_mtx_lock(uipc_lock
);
1175 lck_mtx_unlock(uipc_lock
);
1180 lck_mtx_unlock(uipc_lock
);
1182 * before going through all this, set all FDs to
1183 * be NOT defered and NOT externally accessible
1185 for (fg
= fmsghead
.lh_first
; fg
!= 0; fg
= fg
->f_msglist
.le_next
) {
1186 lck_mtx_lock(&fg
->fg_lock
);
1187 fg
->fg_flag
&= ~(FMARK
|FDEFER
);
1188 lck_mtx_unlock(&fg
->fg_lock
);
1191 for (fg
= fmsghead
.lh_first
; fg
!= 0; fg
= fg
->f_msglist
.le_next
) {
1192 lck_mtx_lock(&fg
->fg_lock
);
1194 * If the file is not open, skip it
1196 if (fg
->fg_count
== 0) {
1197 lck_mtx_unlock(&fg
->fg_lock
);
1201 * If we already marked it as 'defer' in a
1202 * previous pass, then try process it this time
1205 if (fg
->fg_flag
& FDEFER
) {
1206 fg
->fg_flag
&= ~FDEFER
;
1210 * if it's not defered, then check if it's
1211 * already marked.. if so skip it
1213 if (fg
->fg_flag
& FMARK
){
1214 lck_mtx_unlock(&fg
->fg_lock
);
1218 * If all references are from messages
1219 * in transit, then skip it. it's not
1220 * externally accessible.
1222 if (fg
->fg_count
== fg
->fg_msgcount
) {
1223 lck_mtx_unlock(&fg
->fg_lock
);
1227 * If it got this far then it must be
1228 * externally accessible.
1230 fg
->fg_flag
|= FMARK
;
1233 * either it was defered, or it is externally
1234 * accessible and not already marked so.
1235 * Now check if it is possibly one of OUR sockets.
1237 if (fg
->fg_type
!= DTYPE_SOCKET
||
1238 (so
= (struct socket
*)fg
->fg_data
) == 0) {
1239 lck_mtx_unlock(&fg
->fg_lock
);
1242 if (so
->so_proto
->pr_domain
!= &localdomain
||
1243 (so
->so_proto
->pr_flags
&PR_RIGHTS
) == 0) {
1244 lck_mtx_unlock(&fg
->fg_lock
);
1248 /* if this code is enabled need to run under network funnel */
1249 if (so
->so_rcv
.sb_flags
& SB_LOCK
) {
1251 * This is problematical; it's not clear
1252 * we need to wait for the sockbuf to be
1253 * unlocked (on a uniprocessor, at least),
1254 * and it's also not clear what to do
1255 * if sbwait returns an error due to receipt
1256 * of a signal. If sbwait does return
1257 * an error, we'll go into an infinite
1258 * loop. Delete all of this for now.
1260 (void) sbwait(&so
->so_rcv
);
1265 * So, Ok, it's one of our sockets and it IS externally
1266 * accessible (or was defered). Now we look
1267 * to see if we hold any file descriptors in its
1268 * message buffers. Follow those links and mark them
1269 * as accessible too.
1271 unp_scan(so
->so_rcv
.sb_mb
, unp_mark
);
1272 lck_mtx_unlock(&fg
->fg_lock
);
1274 } while (unp_defer
);
1276 * We grab an extra reference to each of the file table entries
1277 * that are not otherwise accessible and then free the rights
1278 * that are stored in messages on them.
1280 * The bug in the orginal code is a little tricky, so I'll describe
1281 * what's wrong with it here.
1283 * It is incorrect to simply unp_discard each entry for f_msgcount
1284 * times -- consider the case of sockets A and B that contain
1285 * references to each other. On a last close of some other socket,
1286 * we trigger a gc since the number of outstanding rights (unp_rights)
1287 * is non-zero. If during the sweep phase the gc code un_discards,
1288 * we end up doing a (full) closef on the descriptor. A closef on A
1289 * results in the following chain. Closef calls soo_close, which
1290 * calls soclose. Soclose calls first (through the switch
1291 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply
1292 * returns because the previous instance had set unp_gcing, and
1293 * we return all the way back to soclose, which marks the socket
1294 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush
1295 * to free up the rights that are queued in messages on the socket A,
1296 * i.e., the reference on B. The sorflush calls via the dom_dispose
1297 * switch unp_dispose, which unp_scans with unp_discard. This second
1298 * instance of unp_discard just calls closef on B.
1300 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1301 * which results in another closef on A. Unfortunately, A is already
1302 * being closed, and the descriptor has already been marked with
1303 * SS_NOFDREF, and soclose panics at this point.
1305 * Here, we first take an extra reference to each inaccessible
1306 * descriptor. Then, we call sorflush ourself, since we know
1307 * it is a Unix domain socket anyhow. After we destroy all the
1308 * rights carried in messages, we do a last closef to get rid
1309 * of our extra reference. This is the last close, and the
1310 * unp_detach etc will shut down the socket.
1312 * 91/09/19, bsy@cs.cmu.edu
1314 extra_ref
= _MALLOC(nfiles
* sizeof(struct fileglob
*), M_FILEGLOB
, M_WAITOK
);
1315 for (nunref
= 0, fg
= fmsghead
.lh_first
, fpp
= extra_ref
; fg
!= 0;
1317 lck_mtx_lock(&fg
->fg_lock
);
1319 nextfg
= fg
->f_msglist
.le_next
;
1321 * If it's not open, skip it
1323 if (fg
->fg_count
== 0) {
1324 lck_mtx_unlock(&fg
->fg_lock
);
1328 * If all refs are from msgs, and it's not marked accessible
1329 * then it must be referenced from some unreachable cycle
1330 * of (shut-down) FDs, so include it in our
1331 * list of FDs to remove
1333 if (fg
->fg_count
== fg
->fg_msgcount
&& !(fg
->fg_flag
& FMARK
)) {
1338 lck_mtx_unlock(&fg
->fg_lock
);
1341 * for each FD on our hit list, do the following two things
1343 for (i
= nunref
, fpp
= extra_ref
; --i
>= 0; ++fpp
) {
1344 struct fileglob
*tfg
;
1348 if (tfg
->fg_type
== DTYPE_SOCKET
&& tfg
->fg_data
!= NULL
) {
1349 sorflush((struct socket
*)(tfg
->fg_data
));
1352 for (i
= nunref
, fpp
= extra_ref
; --i
>= 0; ++fpp
)
1353 closef_locked((struct fileproc
*)0, *fpp
, (struct proc
*) NULL
);
1355 FREE((caddr_t
)extra_ref
, M_FILEGLOB
);
1360 unp_dispose(struct mbuf
*m
)
1364 unp_scan(m
, unp_discard
);
1374 cru2x(p
->p_ucred
, &unp
->unp_peercred
);
1375 unp
->unp_flags
|= UNP_HAVEPCCACHED
;
1379 /* should run under kernel funnel */
1383 void (*op
)(struct fileglob
*))
1386 struct fileglob
**rp
;
1392 for (m
= m0
; m
; m
= m
->m_next
)
1393 if (m
->m_type
== MT_CONTROL
&&
1394 (size_t) m
->m_len
>= sizeof(*cm
)) {
1395 cm
= mtod(m
, struct cmsghdr
*);
1396 if (cm
->cmsg_level
!= SOL_SOCKET
||
1397 cm
->cmsg_type
!= SCM_RIGHTS
)
1399 qfds
= (cm
->cmsg_len
- sizeof *cm
)
1400 / sizeof (struct fileglob
*);
1401 rp
= (struct fileglob
**)(cm
+ 1);
1402 for (i
= 0; i
< qfds
; i
++)
1404 break; /* XXX, but saves time */
1410 /* should run under kernel funnel */
1412 unp_mark(struct fileglob
*fg
)
1414 lck_mtx_lock(&fg
->fg_lock
);
1416 if (fg
->fg_flag
& FMARK
) {
1417 lck_mtx_unlock(&fg
->fg_lock
);
1420 fg
->fg_flag
|= (FMARK
|FDEFER
);
1422 lck_mtx_unlock(&fg
->fg_lock
);
1427 /* should run under kernel funnel */
1430 struct fileglob
*fg
;
1432 struct proc
*p
= current_proc(); /* XXX */
1435 unp_discard_fdlocked(fg
, p
);
1439 unp_discard_fdlocked(fg
, p
)
1440 struct fileglob
*fg
;
1447 (void) closef_locked((struct fileproc
*)0, fg
, p
);