2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1982, 1986, 1989, 1991, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/kernel.h>
60 #include <sys/domain.h>
61 #include <sys/fcntl.h>
62 #include <sys/malloc.h> /* XXX must be before <sys/file.h> */
63 #include <sys/file_internal.h>
64 #include <sys/filedesc.h>
67 #include <sys/namei.h>
68 #include <sys/proc_internal.h>
69 #include <sys/kauth.h>
70 #include <sys/protosw.h>
71 #include <sys/socket.h>
72 #include <sys/socketvar.h>
74 #include <sys/sysctl.h>
76 #include <sys/unpcb.h>
77 #include <sys/vnode_internal.h>
78 #include <sys/kdebug.h>
80 #include <kern/zalloc.h>
81 #include <kern/locks.h>
83 #define f_msgcount f_fglob->fg_msgcount
84 #define f_cred f_fglob->fg_cred
85 #define f_ops f_fglob->fg_ops
86 #define f_offset f_fglob->fg_offset
87 #define f_data f_fglob->fg_data
88 struct zone
*unp_zone
;
89 static unp_gen_t unp_gencnt
;
90 static u_int unp_count
;
92 static lck_attr_t
*unp_mtx_attr
;
93 static lck_grp_t
*unp_mtx_grp
;
94 static lck_grp_attr_t
*unp_mtx_grp_attr
;
95 static lck_rw_t
*unp_list_mtx
;
97 extern lck_mtx_t
* uipc_lock
;
98 static struct unp_head unp_shead
, unp_dhead
;
101 * Unix communications domain.
105 * rethink name space problems
106 * need a proper out-of-band
109 static struct sockaddr sun_noname
= { sizeof(sun_noname
), AF_LOCAL
, { 0 } };
110 static ino_t unp_ino
; /* prototype for fake inode numbers */
112 static int unp_attach(struct socket
*);
113 static void unp_detach(struct unpcb
*);
114 static int unp_bind(struct unpcb
*,struct sockaddr
*, struct proc
*);
115 static int unp_connect(struct socket
*,struct sockaddr
*, struct proc
*);
116 static void unp_disconnect(struct unpcb
*);
117 static void unp_shutdown(struct unpcb
*);
118 static void unp_drop(struct unpcb
*, int);
119 static void unp_gc(void);
120 static void unp_scan(struct mbuf
*, void (*)(struct fileglob
*));
121 static void unp_mark(struct fileglob
*);
122 static void unp_discard(struct fileglob
*);
123 static void unp_discard_fdlocked(struct fileglob
*, struct proc
*);
124 static int unp_internalize(struct mbuf
*, struct proc
*);
125 static int unp_listen(struct unpcb
*, struct proc
*);
129 uipc_abort(struct socket
*so
)
131 struct unpcb
*unp
= sotounpcb(so
);
135 unp_drop(unp
, ECONNABORTED
);
142 uipc_accept(struct socket
*so
, struct sockaddr
**nam
)
144 struct unpcb
*unp
= sotounpcb(so
);
150 * Pass back name of connected socket,
151 * if it was bound and we are still connected
152 * (our peer may have closed already!).
154 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
) {
155 *nam
= dup_sockaddr((struct sockaddr
*)unp
->unp_conn
->unp_addr
,
158 *nam
= dup_sockaddr((struct sockaddr
*)&sun_noname
, 1);
164 uipc_attach(struct socket
*so
, __unused
int proto
, __unused
struct proc
*p
)
166 struct unpcb
*unp
= sotounpcb(so
);
170 return unp_attach(so
);
174 uipc_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
176 struct unpcb
*unp
= sotounpcb(so
);
181 return unp_bind(unp
, nam
, p
);
185 uipc_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
187 struct unpcb
*unp
= sotounpcb(so
);
191 return unp_connect(so
, nam
, p
);
195 uipc_connect2(struct socket
*so1
, struct socket
*so2
)
197 struct unpcb
*unp
= sotounpcb(so1
);
202 return unp_connect2(so1
, so2
);
205 /* control is EOPNOTSUPP */
208 uipc_detach(struct socket
*so
)
210 struct unpcb
*unp
= sotounpcb(so
);
220 uipc_disconnect(struct socket
*so
)
222 struct unpcb
*unp
= sotounpcb(so
);
231 uipc_listen(struct socket
*so
, __unused
struct proc
*p
)
233 struct unpcb
*unp
= sotounpcb(so
);
235 if (unp
== 0 || unp
->unp_vnode
== 0)
237 return unp_listen(unp
, p
);
241 uipc_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
243 struct unpcb
*unp
= sotounpcb(so
);
247 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
)
248 *nam
= dup_sockaddr((struct sockaddr
*)unp
->unp_conn
->unp_addr
,
254 uipc_rcvd(struct socket
*so
, __unused
int flags
)
256 struct unpcb
*unp
= sotounpcb(so
);
261 switch (so
->so_type
) {
263 panic("uipc_rcvd DGRAM?");
267 #define rcv (&so->so_rcv)
268 #define snd (&so2->so_snd)
269 if (unp
->unp_conn
== 0)
271 so2
= unp
->unp_conn
->unp_socket
;
273 * Adjust backpressure on sender
274 * and wakeup any waiting to write.
276 snd
->sb_mbmax
+= unp
->unp_mbcnt
- rcv
->sb_mbcnt
;
277 unp
->unp_mbcnt
= rcv
->sb_mbcnt
;
278 snd
->sb_hiwat
+= unp
->unp_cc
- rcv
->sb_cc
;
279 unp
->unp_cc
= rcv
->sb_cc
;
286 panic("uipc_rcvd unknown socktype");
291 /* pru_rcvoob is EOPNOTSUPP */
294 uipc_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
295 struct mbuf
*control
, struct proc
*p
)
298 struct unpcb
*unp
= sotounpcb(so
);
305 if (flags
& PRUS_OOB
) {
311 socket_unlock(so
, 0); /* release global lock to avoid deadlock (4436174) */
312 error
= unp_internalize(control
, p
);
318 switch (so
->so_type
) {
321 struct sockaddr
*from
;
328 error
= unp_connect(so
, nam
, p
);
332 if (unp
->unp_conn
== 0) {
337 so2
= unp
->unp_conn
->unp_socket
;
339 from
= (struct sockaddr
*)unp
->unp_addr
;
342 if (sbappendaddr(&so2
->so_rcv
, from
, m
, control
, &error
)) {
354 #define rcv (&so2->so_rcv)
355 #define snd (&so->so_snd)
356 /* Connect if not connected yet. */
358 * Note: A better implementation would complain
359 * if not equal to the peer's address.
361 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
363 error
= unp_connect(so
, nam
, p
);
372 if (so
->so_state
& SS_CANTSENDMORE
) {
376 if (unp
->unp_conn
== 0)
377 panic("uipc_send connected but no connection?");
378 so2
= unp
->unp_conn
->unp_socket
;
380 * Send to paired receive port, and then reduce
381 * send buffer hiwater marks to maintain backpressure.
384 if ((control
&& sbappendcontrol(rcv
, m
, control
, NULL
)) ||
389 rcv
->sb_mbcnt
- unp
->unp_conn
->unp_mbcnt
;
390 unp
->unp_conn
->unp_mbcnt
= rcv
->sb_mbcnt
;
391 snd
->sb_hiwat
-= rcv
->sb_cc
- unp
->unp_conn
->unp_cc
;
392 unp
->unp_conn
->unp_cc
= rcv
->sb_cc
;
403 panic("uipc_send unknown socktype");
407 * SEND_EOF is equivalent to a SEND followed by
410 if (flags
& PRUS_EOF
) {
415 if (control
&& error
!= 0)
416 unp_dispose(control
);
427 uipc_sense(struct socket
*so
, struct stat
*sb
)
429 struct unpcb
*unp
= sotounpcb(so
);
434 sb
->st_blksize
= so
->so_snd
.sb_hiwat
;
435 if (so
->so_type
== SOCK_STREAM
&& unp
->unp_conn
!= 0) {
436 so2
= unp
->unp_conn
->unp_socket
;
437 sb
->st_blksize
+= so2
->so_rcv
.sb_cc
;
440 if (unp
->unp_ino
== 0)
441 unp
->unp_ino
= unp_ino
++;
442 sb
->st_ino
= unp
->unp_ino
;
447 uipc_shutdown(struct socket
*so
)
449 struct unpcb
*unp
= sotounpcb(so
);
459 uipc_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
461 struct unpcb
*unp
= sotounpcb(so
);
466 *nam
= dup_sockaddr((struct sockaddr
*)unp
->unp_addr
, 1);
470 struct pr_usrreqs uipc_usrreqs
= {
471 uipc_abort
, uipc_accept
, uipc_attach
, uipc_bind
, uipc_connect
,
472 uipc_connect2
, pru_control_notsupp
, uipc_detach
, uipc_disconnect
,
473 uipc_listen
, uipc_peeraddr
, uipc_rcvd
, pru_rcvoob_notsupp
,
474 uipc_send
, uipc_sense
, uipc_shutdown
, uipc_sockaddr
,
475 sosend
, soreceive
, pru_sopoll_notsupp
481 struct sockopt
*sopt
)
483 struct unpcb
*unp
= sotounpcb(so
);
486 switch (sopt
->sopt_dir
) {
488 switch (sopt
->sopt_name
) {
490 if (unp
->unp_flags
& UNP_HAVEPC
)
491 error
= sooptcopyout(sopt
, &unp
->unp_peercred
,
492 sizeof(unp
->unp_peercred
));
494 if (so
->so_type
== SOCK_STREAM
)
514 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
515 * for stream sockets, although the total for sender and receiver is
516 * actually only PIPSIZ.
517 * Datagram sockets really use the sendspace as the maximum datagram size,
518 * and don't really want to reserve the sendspace. Their recvspace should
519 * be large enough for at least one max-size datagram plus address.
524 static u_long unpst_sendspace
= PIPSIZ
;
525 static u_long unpst_recvspace
= PIPSIZ
;
526 static u_long unpdg_sendspace
= 2*1024; /* really max datagram size */
527 static u_long unpdg_recvspace
= 4*1024;
529 static int unp_rights
; /* file descriptors in flight */
531 SYSCTL_DECL(_net_local_stream
);
532 SYSCTL_INT(_net_local_stream
, OID_AUTO
, sendspace
, CTLFLAG_RW
,
533 &unpst_sendspace
, 0, "");
534 SYSCTL_INT(_net_local_stream
, OID_AUTO
, recvspace
, CTLFLAG_RW
,
535 &unpst_recvspace
, 0, "");
536 SYSCTL_DECL(_net_local_dgram
);
537 SYSCTL_INT(_net_local_dgram
, OID_AUTO
, maxdgram
, CTLFLAG_RW
,
538 &unpdg_sendspace
, 0, "");
539 SYSCTL_INT(_net_local_dgram
, OID_AUTO
, recvspace
, CTLFLAG_RW
,
540 &unpdg_recvspace
, 0, "");
541 SYSCTL_DECL(_net_local
);
542 SYSCTL_INT(_net_local
, OID_AUTO
, inflight
, CTLFLAG_RD
, &unp_rights
, 0, "");
545 unp_attach(struct socket
*so
)
550 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
551 switch (so
->so_type
) {
554 error
= soreserve(so
, unpst_sendspace
, unpst_recvspace
);
558 error
= soreserve(so
, unpdg_sendspace
, unpdg_recvspace
);
567 unp
= (struct unpcb
*)zalloc(unp_zone
);
570 bzero(unp
, sizeof *unp
);
571 lck_rw_lock_exclusive(unp_list_mtx
);
572 LIST_INIT(&unp
->unp_refs
);
573 unp
->unp_socket
= so
;
574 unp
->unp_gencnt
= ++unp_gencnt
;
576 LIST_INSERT_HEAD(so
->so_type
== SOCK_DGRAM
? &unp_dhead
577 : &unp_shead
, unp
, unp_link
);
578 so
->so_pcb
= (caddr_t
)unp
;
579 lck_rw_done(unp_list_mtx
);
584 unp_detach(struct unpcb
*unp
)
586 lck_rw_lock_exclusive(unp_list_mtx
);
587 LIST_REMOVE(unp
, unp_link
);
588 unp
->unp_gencnt
= ++unp_gencnt
;
589 lck_rw_done(unp_list_mtx
);
591 if (unp
->unp_vnode
) {
592 struct vnode
*tvp
= unp
->unp_vnode
;
593 unp
->unp_vnode
->v_socket
= 0;
595 vnode_rele(tvp
); /* drop the usecount */
599 while (unp
->unp_refs
.lh_first
)
600 unp_drop(unp
->unp_refs
.lh_first
, ECONNRESET
);
601 soisdisconnected(unp
->unp_socket
);
602 unp
->unp_socket
->so_flags
|= SOF_PCBCLEARING
; /* makes sure we're getting dealloced */
603 unp
->unp_socket
->so_pcb
= 0;
606 * Normally the receive buffer is flushed later,
607 * in sofree, but if our receive buffer holds references
608 * to descriptors that are now garbage, we will dispose
609 * of those descriptor references after the garbage collector
610 * gets them (resulting in a "panic: closef: count < 0").
612 sorflush(unp
->unp_socket
);
616 FREE(unp
->unp_addr
, M_SONAME
);
617 zfree(unp_zone
, unp
);
623 struct sockaddr
*nam
,
626 struct sockaddr_un
*soun
= (struct sockaddr_un
*)nam
;
627 struct vnode
*vp
, *dvp
;
628 struct vnode_attr va
;
629 struct vfs_context context
;
632 char buf
[SOCK_MAXADDRLEN
];
635 context
.vc_ucred
= p
->p_ucred
; /* XXX kauth_cred_get() ??? proxy */
637 if (unp
->unp_vnode
!= NULL
)
639 namelen
= soun
->sun_len
- offsetof(struct sockaddr_un
, sun_path
);
642 strncpy(buf
, soun
->sun_path
, namelen
);
643 buf
[namelen
] = 0; /* null-terminate the string */
644 NDINIT(&nd
, CREATE
, FOLLOW
| LOCKPARENT
, UIO_SYSSPACE32
,
645 CAST_USER_ADDR_T(buf
), &context
);
646 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
656 * need to do this before the vnode_put of dvp
657 * since we may have to release an fs_nodelock
667 /* authorize before creating */
668 error
= vnode_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, &context
);
672 VATTR_SET(&va
, va_type
, VSOCK
);
673 VATTR_SET(&va
, va_mode
, (ACCESSPERMS
& ~p
->p_fd
->fd_cmask
));
675 /* create the socket */
676 error
= vn_create(dvp
, &vp
, &nd
.ni_cnd
, &va
, 0, &context
);
685 vnode_ref(vp
); /* gain a longterm reference */
686 vp
->v_socket
= unp
->unp_socket
;
688 unp
->unp_addr
= (struct sockaddr_un
*)dup_sockaddr(nam
, 1);
689 vnode_put(vp
); /* drop the iocount */
697 struct sockaddr
*nam
,
700 struct sockaddr_un
*soun
= (struct sockaddr_un
*)nam
;
702 struct socket
*so2
, *so3
;
703 struct unpcb
*unp
, *unp2
, *unp3
;
704 struct vfs_context context
;
707 char buf
[SOCK_MAXADDRLEN
];
710 context
.vc_ucred
= p
->p_ucred
; /* XXX kauth_cred_get() ??? proxy */
713 len
= nam
->sa_len
- offsetof(struct sockaddr_un
, sun_path
);
716 strncpy(buf
, soun
->sun_path
, len
);
719 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE32
, CAST_USER_ADDR_T(buf
), &context
);
726 if (vp
->v_type
!= VSOCK
) {
731 error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_WRITE_DATA
, &context
);
735 if (so2
== 0 || so2
->so_pcb
== NULL
) {
736 error
= ECONNREFUSED
;
740 /* make sure the socket can't go away while we're connecting */
743 if (so
->so_type
!= so2
->so_type
) {
749 * Check if socket was connected while we were trying to
750 * acquire the funnel.
751 * XXX - probably shouldn't return an error for SOCK_DGRAM
753 if ((so
->so_state
& SS_ISCONNECTED
) != 0) {
758 if (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) {
759 if ((so2
->so_options
& SO_ACCEPTCONN
) == 0 ||
760 (so3
= sonewconn(so2
, 0, nam
)) == 0) {
761 error
= ECONNREFUSED
;
765 unp2
= sotounpcb(so2
);
766 unp3
= sotounpcb(so3
);
768 unp3
->unp_addr
= (struct sockaddr_un
*)
769 dup_sockaddr((struct sockaddr
*)
773 * unp_peercred management:
775 * The connecter's (client's) credentials are copied
776 * from its process structure at the time of connect()
779 cru2x(p
->p_ucred
, &unp3
->unp_peercred
);
780 unp3
->unp_flags
|= UNP_HAVEPC
;
782 * The receiver's (server's) credentials are copied
783 * from the unp_peercred member of socket on which the
784 * former called listen(); unp_listen() cached that
785 * process's credentials at that time so we can use
788 KASSERT(unp2
->unp_flags
& UNP_HAVEPCCACHED
,
789 ("unp_connect: listener without cached peercred"));
790 memcpy(&unp
->unp_peercred
, &unp2
->unp_peercred
,
791 sizeof(unp
->unp_peercred
));
792 unp
->unp_flags
|= UNP_HAVEPC
;
794 so2
->so_usecount
--; /* drop reference taken on so2 */
796 so3
->so_usecount
++; /* make sure we keep it around */
798 error
= unp_connect2(so
, so2
);
802 so2
->so_usecount
--; /* release count on socket */
813 struct unpcb
*unp
= sotounpcb(so
);
816 if (so2
->so_type
!= so
->so_type
)
818 unp2
= sotounpcb(so2
);
820 /* Verify both sockets are still opened */
821 if (unp
== 0 || unp2
== 0)
824 unp
->unp_conn
= unp2
;
825 switch (so
->so_type
) {
828 LIST_INSERT_HEAD(&unp2
->unp_refs
, unp
, unp_reflink
);
833 /* This takes care of socketpair */
834 if (!(unp
->unp_flags
& UNP_HAVEPC
) && !(unp2
->unp_flags
& UNP_HAVEPC
)) {
835 cru2x(kauth_cred_get(), &unp
->unp_peercred
);
836 unp
->unp_flags
|= UNP_HAVEPC
;
838 cru2x(kauth_cred_get(), &unp2
->unp_peercred
);
839 unp2
->unp_flags
|= UNP_HAVEPC
;
841 unp2
->unp_conn
= unp
;
847 panic("unp_connect2");
853 unp_disconnect(struct unpcb
*unp
)
855 struct unpcb
*unp2
= unp
->unp_conn
;
860 switch (unp
->unp_socket
->so_type
) {
863 lck_rw_lock_exclusive(unp_list_mtx
);
864 LIST_REMOVE(unp
, unp_reflink
);
865 lck_rw_done(unp_list_mtx
);
866 unp
->unp_socket
->so_state
&= ~SS_ISCONNECTED
;
870 soisdisconnected(unp
->unp_socket
);
872 soisdisconnected(unp2
->unp_socket
);
879 unp_abort(struct unpcb
*unp
)
887 unp_pcblist SYSCTL_HANDLER_ARGS
890 struct unpcb
*unp
, **unp_list
;
893 struct unp_head
*head
;
895 lck_rw_lock_shared(unp_list_mtx
);
896 head
= ((intptr_t)arg1
== SOCK_DGRAM
? &unp_dhead
: &unp_shead
);
899 * The process of preparing the PCB list is too time-consuming and
900 * resource-intensive to repeat twice on every request.
902 if (req
->oldptr
== USER_ADDR_NULL
) {
904 req
->oldidx
= 2 * (sizeof xug
)
905 + (n
+ n
/8) * sizeof(struct xunpcb
);
906 lck_rw_done(unp_list_mtx
);
910 if (req
->newptr
!= USER_ADDR_NULL
) {
911 lck_rw_done(unp_list_mtx
);
916 * OK, now we're committed to doing something.
921 bzero(&xug
, sizeof(xug
));
922 xug
.xug_len
= sizeof xug
;
924 xug
.xug_gen
= gencnt
;
925 xug
.xug_sogen
= so_gencnt
;
926 error
= SYSCTL_OUT(req
, &xug
, sizeof xug
);
928 lck_rw_done(unp_list_mtx
);
933 * We are done if there is no pcb
936 lck_rw_done(unp_list_mtx
);
940 MALLOC(unp_list
, struct unpcb
**, n
* sizeof *unp_list
, M_TEMP
, M_WAITOK
);
942 lck_rw_done(unp_list_mtx
);
946 for (unp
= head
->lh_first
, i
= 0; unp
&& i
< n
;
947 unp
= unp
->unp_link
.le_next
) {
948 if (unp
->unp_gencnt
<= gencnt
)
951 n
= i
; /* in case we lost some during malloc */
954 for (i
= 0; i
< n
; i
++) {
956 if (unp
->unp_gencnt
<= gencnt
) {
959 bzero(&xu
, sizeof(xu
));
960 xu
.xu_len
= sizeof xu
;
961 xu
.xu_unpp
= (struct unpcb_compat
*)unp
;
963 * XXX - need more locking here to protect against
964 * connect/disconnect races for SMP.
967 bcopy(unp
->unp_addr
, &xu
.xu_addr
,
968 unp
->unp_addr
->sun_len
);
969 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
)
970 bcopy(unp
->unp_conn
->unp_addr
,
972 unp
->unp_conn
->unp_addr
->sun_len
);
973 bcopy(unp
, &xu
.xu_unp
, sizeof(xu
.xu_unp
));
974 sotoxsocket(unp
->unp_socket
, &xu
.xu_socket
);
975 error
= SYSCTL_OUT(req
, &xu
, sizeof xu
);
980 * Give the user an updated idea of our state.
981 * If the generation differs from what we told
982 * her before, she knows that something happened
983 * while we were processing this request, and it
984 * might be necessary to retry.
986 bzero(&xug
, sizeof(xug
));
987 xug
.xug_len
= sizeof xug
;
988 xug
.xug_gen
= unp_gencnt
;
989 xug
.xug_sogen
= so_gencnt
;
990 xug
.xug_count
= unp_count
;
991 error
= SYSCTL_OUT(req
, &xug
, sizeof xug
);
993 FREE(unp_list
, M_TEMP
);
994 lck_rw_done(unp_list_mtx
);
998 SYSCTL_PROC(_net_local_dgram
, OID_AUTO
, pcblist
, CTLFLAG_RD
,
999 (caddr_t
)(long)SOCK_DGRAM
, 0, unp_pcblist
, "S,xunpcb",
1000 "List of active local datagram sockets");
1001 SYSCTL_PROC(_net_local_stream
, OID_AUTO
, pcblist
, CTLFLAG_RD
,
1002 (caddr_t
)(long)SOCK_STREAM
, 0, unp_pcblist
, "S,xunpcb",
1003 "List of active local stream sockets");
1006 unp_shutdown(struct unpcb
*unp
)
1010 if (unp
->unp_socket
->so_type
== SOCK_STREAM
&& unp
->unp_conn
&&
1011 (so
= unp
->unp_conn
->unp_socket
))
1020 struct socket
*so
= unp
->unp_socket
;
1022 so
->so_error
= errno
;
1023 unp_disconnect(unp
);
1035 unp_externalize(struct mbuf
*rights
)
1037 struct proc
*p
= current_proc(); /* XXX */
1039 struct cmsghdr
*cm
= mtod(rights
, struct cmsghdr
*);
1040 struct fileglob
**rp
= (struct fileglob
**)(cm
+ 1);
1041 struct fileproc
*fp
;
1042 struct fileglob
*fg
;
1043 int newfds
= (cm
->cmsg_len
- sizeof(*cm
)) / sizeof (int);
1049 * if the new FD's will not fit, then we free them all
1051 if (!fdavail(p
, newfds
)) {
1052 for (i
= 0; i
< newfds
; i
++) {
1054 unp_discard_fdlocked(fg
, p
);
1062 * now change each pointer to an fd in the global table to
1063 * an integer that is the index to the local fd table entry
1064 * that we set up to point to the global one we are transferring.
1065 * XXX this assumes a pointer and int are the same size...!
1067 for (i
= 0; i
< newfds
; i
++) {
1068 if (fdalloc(p
, 0, &f
))
1069 panic("unp_externalize");
1071 MALLOC_ZONE(fp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
1072 bzero(fp
, sizeof(struct fileproc
));
1076 procfdtbl_releasefd(p
, f
, fp
);
1088 unp_zone
= zinit(sizeof(struct unpcb
),
1089 (nmbclusters
* sizeof(struct unpcb
)),
1093 LIST_INIT(&unp_dhead
);
1094 LIST_INIT(&unp_shead
);
1097 * allocate lock group attribute and group for udp pcb mutexes
1099 unp_mtx_grp_attr
= lck_grp_attr_alloc_init();
1101 unp_mtx_grp
= lck_grp_alloc_init("unp_list", unp_mtx_grp_attr
);
1103 unp_mtx_attr
= lck_attr_alloc_init();
1105 if ((unp_list_mtx
= lck_rw_alloc_init(unp_mtx_grp
, unp_mtx_attr
)) == NULL
)
1106 return; /* pretty much dead if this fails... */
1111 #define MIN(a,b) (((a)<(b))?(a):(b))
1116 struct mbuf
*control
,
1119 struct cmsghdr
*cm
= mtod(control
, struct cmsghdr
*);
1120 struct fileglob
**rp
;
1121 struct fileproc
*fp
;
1122 register int i
, error
;
1124 int fdgetf_noref(proc_t
, struct fileglob
**, struct fileproc
**);
1126 if (cm
->cmsg_type
!= SCM_RIGHTS
|| cm
->cmsg_level
!= SOL_SOCKET
||
1127 cm
->cmsg_len
!= control
->m_len
) {
1130 oldfds
= (cm
->cmsg_len
- sizeof (*cm
)) / sizeof (int);
1133 rp
= (struct fileglob
**)(cm
+ 1);
1135 for (i
= 0; i
< oldfds
; i
++) {
1136 if (error
= fdgetf_noref(p
, *(int *)rp
++, (struct fileglob
**)0)) {
1141 rp
= (struct fileglob
**)(cm
+ 1);
1143 for (i
= 0; i
< oldfds
; i
++) {
1144 (void) fdgetf_noref(p
, *(int *)rp
, &fp
);
1145 fg_insertuipc(fp
->f_fglob
);
1146 *rp
++ = fp
->f_fglob
;
1154 static int unp_defer
, unp_gcing
, unp_gcwait
;
1155 /* always called under uipc_lock */
1159 while (unp_gcing
!= 0) {
1161 msleep(&unp_gcing
, uipc_lock
, 0 , "unp_gc_wait", NULL
);
1168 register struct fileglob
*fg
, *nextfg
;
1169 register struct socket
*so
;
1170 struct fileglob
**extra_ref
, **fpp
;
1172 int need_gcwakeup
= 0;
1174 lck_mtx_lock(uipc_lock
);
1176 lck_mtx_unlock(uipc_lock
);
1181 lck_mtx_unlock(uipc_lock
);
1183 * before going through all this, set all FDs to
1184 * be NOT defered and NOT externally accessible
1186 for (fg
= fmsghead
.lh_first
; fg
!= 0; fg
= fg
->f_msglist
.le_next
) {
1187 lck_mtx_lock(&fg
->fg_lock
);
1188 fg
->fg_flag
&= ~(FMARK
|FDEFER
);
1189 lck_mtx_unlock(&fg
->fg_lock
);
1192 for (fg
= fmsghead
.lh_first
; fg
!= 0; fg
= fg
->f_msglist
.le_next
) {
1193 lck_mtx_lock(&fg
->fg_lock
);
1195 * If the file is not open, skip it
1197 if (fg
->fg_count
== 0) {
1198 lck_mtx_unlock(&fg
->fg_lock
);
1202 * If we already marked it as 'defer' in a
1203 * previous pass, then try process it this time
1206 if (fg
->fg_flag
& FDEFER
) {
1207 fg
->fg_flag
&= ~FDEFER
;
1211 * if it's not defered, then check if it's
1212 * already marked.. if so skip it
1214 if (fg
->fg_flag
& FMARK
){
1215 lck_mtx_unlock(&fg
->fg_lock
);
1219 * If all references are from messages
1220 * in transit, then skip it. it's not
1221 * externally accessible.
1223 if (fg
->fg_count
== fg
->fg_msgcount
) {
1224 lck_mtx_unlock(&fg
->fg_lock
);
1228 * If it got this far then it must be
1229 * externally accessible.
1231 fg
->fg_flag
|= FMARK
;
1234 * either it was defered, or it is externally
1235 * accessible and not already marked so.
1236 * Now check if it is possibly one of OUR sockets.
1238 if (fg
->fg_type
!= DTYPE_SOCKET
||
1239 (so
= (struct socket
*)fg
->fg_data
) == 0) {
1240 lck_mtx_unlock(&fg
->fg_lock
);
1243 if (so
->so_proto
->pr_domain
!= &localdomain
||
1244 (so
->so_proto
->pr_flags
&PR_RIGHTS
) == 0) {
1245 lck_mtx_unlock(&fg
->fg_lock
);
1249 /* if this code is enabled need to run under network funnel */
1250 if (so
->so_rcv
.sb_flags
& SB_LOCK
) {
1252 * This is problematical; it's not clear
1253 * we need to wait for the sockbuf to be
1254 * unlocked (on a uniprocessor, at least),
1255 * and it's also not clear what to do
1256 * if sbwait returns an error due to receipt
1257 * of a signal. If sbwait does return
1258 * an error, we'll go into an infinite
1259 * loop. Delete all of this for now.
1261 (void) sbwait(&so
->so_rcv
);
1266 * So, Ok, it's one of our sockets and it IS externally
1267 * accessible (or was defered). Now we look
1268 * to see if we hold any file descriptors in its
1269 * message buffers. Follow those links and mark them
1270 * as accessible too.
1272 unp_scan(so
->so_rcv
.sb_mb
, unp_mark
);
1273 lck_mtx_unlock(&fg
->fg_lock
);
1275 } while (unp_defer
);
1277 * We grab an extra reference to each of the file table entries
1278 * that are not otherwise accessible and then free the rights
1279 * that are stored in messages on them.
1281 * The bug in the orginal code is a little tricky, so I'll describe
1282 * what's wrong with it here.
1284 * It is incorrect to simply unp_discard each entry for f_msgcount
1285 * times -- consider the case of sockets A and B that contain
1286 * references to each other. On a last close of some other socket,
1287 * we trigger a gc since the number of outstanding rights (unp_rights)
1288 * is non-zero. If during the sweep phase the gc code un_discards,
1289 * we end up doing a (full) closef on the descriptor. A closef on A
1290 * results in the following chain. Closef calls soo_close, which
1291 * calls soclose. Soclose calls first (through the switch
1292 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply
1293 * returns because the previous instance had set unp_gcing, and
1294 * we return all the way back to soclose, which marks the socket
1295 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush
1296 * to free up the rights that are queued in messages on the socket A,
1297 * i.e., the reference on B. The sorflush calls via the dom_dispose
1298 * switch unp_dispose, which unp_scans with unp_discard. This second
1299 * instance of unp_discard just calls closef on B.
1301 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1302 * which results in another closef on A. Unfortunately, A is already
1303 * being closed, and the descriptor has already been marked with
1304 * SS_NOFDREF, and soclose panics at this point.
1306 * Here, we first take an extra reference to each inaccessible
1307 * descriptor. Then, we call sorflush ourself, since we know
1308 * it is a Unix domain socket anyhow. After we destroy all the
1309 * rights carried in messages, we do a last closef to get rid
1310 * of our extra reference. This is the last close, and the
1311 * unp_detach etc will shut down the socket.
1313 * 91/09/19, bsy@cs.cmu.edu
1315 extra_ref
= _MALLOC(nfiles
* sizeof(struct fileglob
*), M_FILEGLOB
, M_WAITOK
);
1316 for (nunref
= 0, fg
= fmsghead
.lh_first
, fpp
= extra_ref
; fg
!= 0;
1318 lck_mtx_lock(&fg
->fg_lock
);
1320 nextfg
= fg
->f_msglist
.le_next
;
1322 * If it's not open, skip it
1324 if (fg
->fg_count
== 0) {
1325 lck_mtx_unlock(&fg
->fg_lock
);
1329 * If all refs are from msgs, and it's not marked accessible
1330 * then it must be referenced from some unreachable cycle
1331 * of (shut-down) FDs, so include it in our
1332 * list of FDs to remove
1334 if (fg
->fg_count
== fg
->fg_msgcount
&& !(fg
->fg_flag
& FMARK
)) {
1339 lck_mtx_unlock(&fg
->fg_lock
);
1342 * for each FD on our hit list, do the following two things
1344 for (i
= nunref
, fpp
= extra_ref
; --i
>= 0; ++fpp
) {
1345 struct fileglob
*tfg
;
1349 if (tfg
->fg_type
== DTYPE_SOCKET
&& tfg
->fg_data
!= NULL
) {
1350 sorflush((struct socket
*)(tfg
->fg_data
));
1353 for (i
= nunref
, fpp
= extra_ref
; --i
>= 0; ++fpp
)
1354 closef_locked((struct fileproc
*)0, *fpp
, (struct proc
*) NULL
);
1355 lck_mtx_lock(uipc_lock
);
1358 if (unp_gcwait
!= 0) {
1362 lck_mtx_unlock(uipc_lock
);
1364 if (need_gcwakeup
!= 0)
1366 FREE((caddr_t
)extra_ref
, M_FILEGLOB
);
1371 unp_dispose(struct mbuf
*m
)
1375 unp_scan(m
, unp_discard
);
1385 cru2x(p
->p_ucred
, &unp
->unp_peercred
);
1386 unp
->unp_flags
|= UNP_HAVEPCCACHED
;
1390 /* should run under kernel funnel */
1394 void (*op
)(struct fileglob
*))
1397 struct fileglob
**rp
;
1403 for (m
= m0
; m
; m
= m
->m_next
)
1404 if (m
->m_type
== MT_CONTROL
&&
1405 (size_t) m
->m_len
>= sizeof(*cm
)) {
1406 cm
= mtod(m
, struct cmsghdr
*);
1407 if (cm
->cmsg_level
!= SOL_SOCKET
||
1408 cm
->cmsg_type
!= SCM_RIGHTS
)
1410 qfds
= (cm
->cmsg_len
- sizeof *cm
)
1411 / sizeof (struct fileglob
*);
1412 rp
= (struct fileglob
**)(cm
+ 1);
1413 for (i
= 0; i
< qfds
; i
++)
1415 break; /* XXX, but saves time */
1421 /* should run under kernel funnel */
1423 unp_mark(struct fileglob
*fg
)
1425 lck_mtx_lock(&fg
->fg_lock
);
1427 if (fg
->fg_flag
& FMARK
) {
1428 lck_mtx_unlock(&fg
->fg_lock
);
1431 fg
->fg_flag
|= (FMARK
|FDEFER
);
1433 lck_mtx_unlock(&fg
->fg_lock
);
1438 /* should run under kernel funnel */
1441 struct fileglob
*fg
;
1443 struct proc
*p
= current_proc(); /* XXX */
1446 unp_discard_fdlocked(fg
, p
);
1450 unp_discard_fdlocked(fg
, p
)
1451 struct fileglob
*fg
;
1458 (void) closef_locked((struct fileproc
*)0, fg
, p
);