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
) {
310 if (control
&& (error
= unp_internalize(control
, p
)))
313 switch (so
->so_type
) {
316 struct sockaddr
*from
;
323 error
= unp_connect(so
, nam
, p
);
327 if (unp
->unp_conn
== 0) {
332 so2
= unp
->unp_conn
->unp_socket
;
334 from
= (struct sockaddr
*)unp
->unp_addr
;
337 if (sbappendaddr(&so2
->so_rcv
, from
, m
, control
, &error
)) {
349 #define rcv (&so2->so_rcv)
350 #define snd (&so->so_snd)
351 /* Connect if not connected yet. */
353 * Note: A better implementation would complain
354 * if not equal to the peer's address.
356 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
358 error
= unp_connect(so
, nam
, p
);
367 if (so
->so_state
& SS_CANTSENDMORE
) {
371 if (unp
->unp_conn
== 0)
372 panic("uipc_send connected but no connection?");
373 so2
= unp
->unp_conn
->unp_socket
;
375 * Send to paired receive port, and then reduce
376 * send buffer hiwater marks to maintain backpressure.
379 if ((control
&& sbappendcontrol(rcv
, m
, control
, NULL
)) ||
384 rcv
->sb_mbcnt
- unp
->unp_conn
->unp_mbcnt
;
385 unp
->unp_conn
->unp_mbcnt
= rcv
->sb_mbcnt
;
386 snd
->sb_hiwat
-= rcv
->sb_cc
- unp
->unp_conn
->unp_cc
;
387 unp
->unp_conn
->unp_cc
= rcv
->sb_cc
;
398 panic("uipc_send unknown socktype");
402 * SEND_EOF is equivalent to a SEND followed by
405 if (flags
& PRUS_EOF
) {
410 if (control
&& error
!= 0)
411 unp_dispose(control
);
422 uipc_sense(struct socket
*so
, struct stat
*sb
)
424 struct unpcb
*unp
= sotounpcb(so
);
429 sb
->st_blksize
= so
->so_snd
.sb_hiwat
;
430 if (so
->so_type
== SOCK_STREAM
&& unp
->unp_conn
!= 0) {
431 so2
= unp
->unp_conn
->unp_socket
;
432 sb
->st_blksize
+= so2
->so_rcv
.sb_cc
;
435 if (unp
->unp_ino
== 0)
436 unp
->unp_ino
= unp_ino
++;
437 sb
->st_ino
= unp
->unp_ino
;
442 uipc_shutdown(struct socket
*so
)
444 struct unpcb
*unp
= sotounpcb(so
);
454 uipc_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
456 struct unpcb
*unp
= sotounpcb(so
);
461 *nam
= dup_sockaddr((struct sockaddr
*)unp
->unp_addr
, 1);
465 struct pr_usrreqs uipc_usrreqs
= {
466 uipc_abort
, uipc_accept
, uipc_attach
, uipc_bind
, uipc_connect
,
467 uipc_connect2
, pru_control_notsupp
, uipc_detach
, uipc_disconnect
,
468 uipc_listen
, uipc_peeraddr
, uipc_rcvd
, pru_rcvoob_notsupp
,
469 uipc_send
, uipc_sense
, uipc_shutdown
, uipc_sockaddr
,
470 sosend
, soreceive
, pru_sopoll_notsupp
476 struct sockopt
*sopt
)
478 struct unpcb
*unp
= sotounpcb(so
);
481 switch (sopt
->sopt_dir
) {
483 switch (sopt
->sopt_name
) {
485 if (unp
->unp_flags
& UNP_HAVEPC
)
486 error
= sooptcopyout(sopt
, &unp
->unp_peercred
,
487 sizeof(unp
->unp_peercred
));
489 if (so
->so_type
== SOCK_STREAM
)
509 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
510 * for stream sockets, although the total for sender and receiver is
511 * actually only PIPSIZ.
512 * Datagram sockets really use the sendspace as the maximum datagram size,
513 * and don't really want to reserve the sendspace. Their recvspace should
514 * be large enough for at least one max-size datagram plus address.
519 static u_long unpst_sendspace
= PIPSIZ
;
520 static u_long unpst_recvspace
= PIPSIZ
;
521 static u_long unpdg_sendspace
= 2*1024; /* really max datagram size */
522 static u_long unpdg_recvspace
= 4*1024;
524 static int unp_rights
; /* file descriptors in flight */
526 SYSCTL_DECL(_net_local_stream
);
527 SYSCTL_INT(_net_local_stream
, OID_AUTO
, sendspace
, CTLFLAG_RW
,
528 &unpst_sendspace
, 0, "");
529 SYSCTL_INT(_net_local_stream
, OID_AUTO
, recvspace
, CTLFLAG_RW
,
530 &unpst_recvspace
, 0, "");
531 SYSCTL_DECL(_net_local_dgram
);
532 SYSCTL_INT(_net_local_dgram
, OID_AUTO
, maxdgram
, CTLFLAG_RW
,
533 &unpdg_sendspace
, 0, "");
534 SYSCTL_INT(_net_local_dgram
, OID_AUTO
, recvspace
, CTLFLAG_RW
,
535 &unpdg_recvspace
, 0, "");
536 SYSCTL_DECL(_net_local
);
537 SYSCTL_INT(_net_local
, OID_AUTO
, inflight
, CTLFLAG_RD
, &unp_rights
, 0, "");
540 unp_attach(struct socket
*so
)
545 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
546 switch (so
->so_type
) {
549 error
= soreserve(so
, unpst_sendspace
, unpst_recvspace
);
553 error
= soreserve(so
, unpdg_sendspace
, unpdg_recvspace
);
562 unp
= (struct unpcb
*)zalloc(unp_zone
);
565 bzero(unp
, sizeof *unp
);
566 lck_rw_lock_exclusive(unp_list_mtx
);
567 LIST_INIT(&unp
->unp_refs
);
568 unp
->unp_socket
= so
;
569 unp
->unp_gencnt
= ++unp_gencnt
;
571 LIST_INSERT_HEAD(so
->so_type
== SOCK_DGRAM
? &unp_dhead
572 : &unp_shead
, unp
, unp_link
);
573 so
->so_pcb
= (caddr_t
)unp
;
574 lck_rw_done(unp_list_mtx
);
579 unp_detach(struct unpcb
*unp
)
581 lck_rw_lock_exclusive(unp_list_mtx
);
582 LIST_REMOVE(unp
, unp_link
);
583 unp
->unp_gencnt
= ++unp_gencnt
;
584 lck_rw_done(unp_list_mtx
);
586 if (unp
->unp_vnode
) {
587 struct vnode
*tvp
= unp
->unp_vnode
;
588 unp
->unp_vnode
->v_socket
= 0;
590 vnode_rele(tvp
); /* drop the usecount */
594 while (unp
->unp_refs
.lh_first
)
595 unp_drop(unp
->unp_refs
.lh_first
, ECONNRESET
);
596 soisdisconnected(unp
->unp_socket
);
597 unp
->unp_socket
->so_flags
|= SOF_PCBCLEARING
; /* makes sure we're getting dealloced */
598 unp
->unp_socket
->so_pcb
= 0;
601 * Normally the receive buffer is flushed later,
602 * in sofree, but if our receive buffer holds references
603 * to descriptors that are now garbage, we will dispose
604 * of those descriptor references after the garbage collector
605 * gets them (resulting in a "panic: closef: count < 0").
607 sorflush(unp
->unp_socket
);
611 FREE(unp
->unp_addr
, M_SONAME
);
612 zfree(unp_zone
, unp
);
618 struct sockaddr
*nam
,
621 struct sockaddr_un
*soun
= (struct sockaddr_un
*)nam
;
622 struct vnode
*vp
, *dvp
;
623 struct vnode_attr va
;
624 struct vfs_context context
;
627 char buf
[SOCK_MAXADDRLEN
];
630 context
.vc_ucred
= p
->p_ucred
; /* XXX kauth_cred_get() ??? proxy */
632 if (unp
->unp_vnode
!= NULL
)
634 namelen
= soun
->sun_len
- offsetof(struct sockaddr_un
, sun_path
);
637 strncpy(buf
, soun
->sun_path
, namelen
);
638 buf
[namelen
] = 0; /* null-terminate the string */
639 NDINIT(&nd
, CREATE
, FOLLOW
| LOCKPARENT
, UIO_SYSSPACE32
,
640 CAST_USER_ADDR_T(buf
), &context
);
641 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
651 * need to do this before the vnode_put of dvp
652 * since we may have to release an fs_nodelock
662 /* authorize before creating */
663 error
= vnode_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, &context
);
667 VATTR_SET(&va
, va_type
, VSOCK
);
668 VATTR_SET(&va
, va_mode
, (ACCESSPERMS
& ~p
->p_fd
->fd_cmask
));
670 /* create the socket */
671 error
= vn_create(dvp
, &vp
, &nd
.ni_cnd
, &va
, 0, &context
);
680 vnode_ref(vp
); /* gain a longterm reference */
681 vp
->v_socket
= unp
->unp_socket
;
683 unp
->unp_addr
= (struct sockaddr_un
*)dup_sockaddr(nam
, 1);
684 vnode_put(vp
); /* drop the iocount */
692 struct sockaddr
*nam
,
695 struct sockaddr_un
*soun
= (struct sockaddr_un
*)nam
;
697 struct socket
*so2
, *so3
;
698 struct unpcb
*unp
, *unp2
, *unp3
;
699 struct vfs_context context
;
702 char buf
[SOCK_MAXADDRLEN
];
705 context
.vc_ucred
= p
->p_ucred
; /* XXX kauth_cred_get() ??? proxy */
708 len
= nam
->sa_len
- offsetof(struct sockaddr_un
, sun_path
);
711 strncpy(buf
, soun
->sun_path
, len
);
714 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE32
, CAST_USER_ADDR_T(buf
), &context
);
721 if (vp
->v_type
!= VSOCK
) {
726 error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_WRITE_DATA
, &context
);
730 if (so2
== 0 || so2
->so_pcb
== NULL
) {
731 error
= ECONNREFUSED
;
735 /* make sure the socket can't go away while we're connecting */
738 if (so
->so_type
!= so2
->so_type
) {
744 * Check if socket was connected while we were trying to
745 * acquire the funnel.
746 * XXX - probably shouldn't return an error for SOCK_DGRAM
748 if ((so
->so_state
& SS_ISCONNECTED
) != 0) {
753 if (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) {
754 if ((so2
->so_options
& SO_ACCEPTCONN
) == 0 ||
755 (so3
= sonewconn(so2
, 0, nam
)) == 0) {
756 error
= ECONNREFUSED
;
760 unp2
= sotounpcb(so2
);
761 unp3
= sotounpcb(so3
);
763 unp3
->unp_addr
= (struct sockaddr_un
*)
764 dup_sockaddr((struct sockaddr
*)
768 * unp_peercred management:
770 * The connecter's (client's) credentials are copied
771 * from its process structure at the time of connect()
774 cru2x(p
->p_ucred
, &unp3
->unp_peercred
);
775 unp3
->unp_flags
|= UNP_HAVEPC
;
777 * The receiver's (server's) credentials are copied
778 * from the unp_peercred member of socket on which the
779 * former called listen(); unp_listen() cached that
780 * process's credentials at that time so we can use
783 KASSERT(unp2
->unp_flags
& UNP_HAVEPCCACHED
,
784 ("unp_connect: listener without cached peercred"));
785 memcpy(&unp
->unp_peercred
, &unp2
->unp_peercred
,
786 sizeof(unp
->unp_peercred
));
787 unp
->unp_flags
|= UNP_HAVEPC
;
789 so2
->so_usecount
--; /* drop reference taken on so2 */
791 so3
->so_usecount
++; /* make sure we keep it around */
793 error
= unp_connect2(so
, so2
);
797 so2
->so_usecount
--; /* release count on socket */
808 struct unpcb
*unp
= sotounpcb(so
);
811 if (so2
->so_type
!= so
->so_type
)
813 unp2
= sotounpcb(so2
);
815 /* Verify both sockets are still opened */
816 if (unp
== 0 || unp2
== 0)
819 unp
->unp_conn
= unp2
;
820 switch (so
->so_type
) {
823 LIST_INSERT_HEAD(&unp2
->unp_refs
, unp
, unp_reflink
);
828 /* This takes care of socketpair */
829 if (!(unp
->unp_flags
& UNP_HAVEPC
) && !(unp2
->unp_flags
& UNP_HAVEPC
)) {
830 cru2x(kauth_cred_get(), &unp
->unp_peercred
);
831 unp
->unp_flags
|= UNP_HAVEPC
;
833 cru2x(kauth_cred_get(), &unp2
->unp_peercred
);
834 unp2
->unp_flags
|= UNP_HAVEPC
;
836 unp2
->unp_conn
= unp
;
842 panic("unp_connect2");
848 unp_disconnect(struct unpcb
*unp
)
850 struct unpcb
*unp2
= unp
->unp_conn
;
855 switch (unp
->unp_socket
->so_type
) {
858 lck_rw_lock_exclusive(unp_list_mtx
);
859 LIST_REMOVE(unp
, unp_reflink
);
860 lck_rw_done(unp_list_mtx
);
861 unp
->unp_socket
->so_state
&= ~SS_ISCONNECTED
;
865 soisdisconnected(unp
->unp_socket
);
867 soisdisconnected(unp2
->unp_socket
);
874 unp_abort(struct unpcb
*unp
)
882 unp_pcblist SYSCTL_HANDLER_ARGS
885 struct unpcb
*unp
, **unp_list
;
888 struct unp_head
*head
;
890 lck_rw_lock_shared(unp_list_mtx
);
891 head
= ((intptr_t)arg1
== SOCK_DGRAM
? &unp_dhead
: &unp_shead
);
894 * The process of preparing the PCB list is too time-consuming and
895 * resource-intensive to repeat twice on every request.
897 if (req
->oldptr
== USER_ADDR_NULL
) {
899 req
->oldidx
= 2 * (sizeof xug
)
900 + (n
+ n
/8) * sizeof(struct xunpcb
);
901 lck_rw_done(unp_list_mtx
);
905 if (req
->newptr
!= USER_ADDR_NULL
) {
906 lck_rw_done(unp_list_mtx
);
911 * OK, now we're committed to doing something.
916 bzero(&xug
, sizeof(xug
));
917 xug
.xug_len
= sizeof xug
;
919 xug
.xug_gen
= gencnt
;
920 xug
.xug_sogen
= so_gencnt
;
921 error
= SYSCTL_OUT(req
, &xug
, sizeof xug
);
923 lck_rw_done(unp_list_mtx
);
928 * We are done if there is no pcb
931 lck_rw_done(unp_list_mtx
);
935 MALLOC(unp_list
, struct unpcb
**, n
* sizeof *unp_list
, M_TEMP
, M_WAITOK
);
937 lck_rw_done(unp_list_mtx
);
941 for (unp
= head
->lh_first
, i
= 0; unp
&& i
< n
;
942 unp
= unp
->unp_link
.le_next
) {
943 if (unp
->unp_gencnt
<= gencnt
)
946 n
= i
; /* in case we lost some during malloc */
949 for (i
= 0; i
< n
; i
++) {
951 if (unp
->unp_gencnt
<= gencnt
) {
954 bzero(&xu
, sizeof(xu
));
955 xu
.xu_len
= sizeof xu
;
956 xu
.xu_unpp
= (struct unpcb_compat
*)unp
;
958 * XXX - need more locking here to protect against
959 * connect/disconnect races for SMP.
962 bcopy(unp
->unp_addr
, &xu
.xu_addr
,
963 unp
->unp_addr
->sun_len
);
964 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
)
965 bcopy(unp
->unp_conn
->unp_addr
,
967 unp
->unp_conn
->unp_addr
->sun_len
);
968 bcopy(unp
, &xu
.xu_unp
, sizeof(xu
.xu_unp
));
969 sotoxsocket(unp
->unp_socket
, &xu
.xu_socket
);
970 error
= SYSCTL_OUT(req
, &xu
, sizeof xu
);
975 * Give the user an updated idea of our state.
976 * If the generation differs from what we told
977 * her before, she knows that something happened
978 * while we were processing this request, and it
979 * might be necessary to retry.
981 bzero(&xug
, sizeof(xug
));
982 xug
.xug_len
= sizeof xug
;
983 xug
.xug_gen
= unp_gencnt
;
984 xug
.xug_sogen
= so_gencnt
;
985 xug
.xug_count
= unp_count
;
986 error
= SYSCTL_OUT(req
, &xug
, sizeof xug
);
988 FREE(unp_list
, M_TEMP
);
989 lck_rw_done(unp_list_mtx
);
993 SYSCTL_PROC(_net_local_dgram
, OID_AUTO
, pcblist
, CTLFLAG_RD
,
994 (caddr_t
)(long)SOCK_DGRAM
, 0, unp_pcblist
, "S,xunpcb",
995 "List of active local datagram sockets");
996 SYSCTL_PROC(_net_local_stream
, OID_AUTO
, pcblist
, CTLFLAG_RD
,
997 (caddr_t
)(long)SOCK_STREAM
, 0, unp_pcblist
, "S,xunpcb",
998 "List of active local stream sockets");
1001 unp_shutdown(struct unpcb
*unp
)
1005 if (unp
->unp_socket
->so_type
== SOCK_STREAM
&& unp
->unp_conn
&&
1006 (so
= unp
->unp_conn
->unp_socket
))
1015 struct socket
*so
= unp
->unp_socket
;
1017 so
->so_error
= errno
;
1018 unp_disconnect(unp
);
1030 unp_externalize(struct mbuf
*rights
)
1032 struct proc
*p
= current_proc(); /* XXX */
1034 struct cmsghdr
*cm
= mtod(rights
, struct cmsghdr
*);
1035 struct fileglob
**rp
= (struct fileglob
**)(cm
+ 1);
1036 struct fileproc
*fp
;
1037 struct fileglob
*fg
;
1038 int newfds
= (cm
->cmsg_len
- sizeof(*cm
)) / sizeof (int);
1044 * if the new FD's will not fit, then we free them all
1046 if (!fdavail(p
, newfds
)) {
1047 for (i
= 0; i
< newfds
; i
++) {
1049 unp_discard_fdlocked(fg
, p
);
1057 * now change each pointer to an fd in the global table to
1058 * an integer that is the index to the local fd table entry
1059 * that we set up to point to the global one we are transferring.
1060 * XXX this assumes a pointer and int are the same size...!
1062 for (i
= 0; i
< newfds
; i
++) {
1063 if (fdalloc(p
, 0, &f
))
1064 panic("unp_externalize");
1066 MALLOC_ZONE(fp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
1067 bzero(fp
, sizeof(struct fileproc
));
1070 p
->p_fd
->fd_ofiles
[f
] = fp
;
1072 *fdflags(p
, f
) &= ~UF_RESERVED
;
1084 unp_zone
= zinit(sizeof(struct unpcb
),
1085 (nmbclusters
* sizeof(struct unpcb
)),
1089 LIST_INIT(&unp_dhead
);
1090 LIST_INIT(&unp_shead
);
1093 * allocate lock group attribute and group for udp pcb mutexes
1095 unp_mtx_grp_attr
= lck_grp_attr_alloc_init();
1097 unp_mtx_grp
= lck_grp_alloc_init("unp_list", unp_mtx_grp_attr
);
1099 unp_mtx_attr
= lck_attr_alloc_init();
1101 if ((unp_list_mtx
= lck_rw_alloc_init(unp_mtx_grp
, unp_mtx_attr
)) == NULL
)
1102 return; /* pretty much dead if this fails... */
1107 #define MIN(a,b) (((a)<(b))?(a):(b))
1112 struct mbuf
*control
,
1115 struct cmsghdr
*cm
= mtod(control
, struct cmsghdr
*);
1116 struct fileglob
**rp
;
1117 struct fileproc
*fp
;
1118 register int i
, error
;
1120 int fdgetf_noref(proc_t
, struct fileglob
**, struct fileproc
**);
1122 if (cm
->cmsg_type
!= SCM_RIGHTS
|| cm
->cmsg_level
!= SOL_SOCKET
||
1123 cm
->cmsg_len
!= control
->m_len
) {
1126 oldfds
= (cm
->cmsg_len
- sizeof (*cm
)) / sizeof (int);
1129 rp
= (struct fileglob
**)(cm
+ 1);
1131 for (i
= 0; i
< oldfds
; i
++) {
1132 if (error
= fdgetf_noref(p
, *(int *)rp
++, (struct fileglob
**)0)) {
1137 rp
= (struct fileglob
**)(cm
+ 1);
1139 for (i
= 0; i
< oldfds
; i
++) {
1140 (void) fdgetf_noref(p
, *(int *)rp
, &fp
);
1141 fg_insertuipc(fp
->f_fglob
);
1142 *rp
++ = fp
->f_fglob
;
1150 static int unp_defer
, unp_gcing
;
1155 register struct fileglob
*fg
, *nextfg
;
1156 register struct socket
*so
;
1157 struct fileglob
**extra_ref
, **fpp
;
1160 lck_mtx_lock(uipc_lock
);
1162 lck_mtx_unlock(uipc_lock
);
1167 lck_mtx_unlock(uipc_lock
);
1169 * before going through all this, set all FDs to
1170 * be NOT defered and NOT externally accessible
1172 for (fg
= fmsghead
.lh_first
; fg
!= 0; fg
= fg
->f_msglist
.le_next
) {
1173 lck_mtx_lock(&fg
->fg_lock
);
1174 fg
->fg_flag
&= ~(FMARK
|FDEFER
);
1175 lck_mtx_unlock(&fg
->fg_lock
);
1178 for (fg
= fmsghead
.lh_first
; fg
!= 0; fg
= fg
->f_msglist
.le_next
) {
1179 lck_mtx_lock(&fg
->fg_lock
);
1181 * If the file is not open, skip it
1183 if (fg
->fg_count
== 0) {
1184 lck_mtx_unlock(&fg
->fg_lock
);
1188 * If we already marked it as 'defer' in a
1189 * previous pass, then try process it this time
1192 if (fg
->fg_flag
& FDEFER
) {
1193 fg
->fg_flag
&= ~FDEFER
;
1197 * if it's not defered, then check if it's
1198 * already marked.. if so skip it
1200 if (fg
->fg_flag
& FMARK
){
1201 lck_mtx_unlock(&fg
->fg_lock
);
1205 * If all references are from messages
1206 * in transit, then skip it. it's not
1207 * externally accessible.
1209 if (fg
->fg_count
== fg
->fg_msgcount
) {
1210 lck_mtx_unlock(&fg
->fg_lock
);
1214 * If it got this far then it must be
1215 * externally accessible.
1217 fg
->fg_flag
|= FMARK
;
1220 * either it was defered, or it is externally
1221 * accessible and not already marked so.
1222 * Now check if it is possibly one of OUR sockets.
1224 if (fg
->fg_type
!= DTYPE_SOCKET
||
1225 (so
= (struct socket
*)fg
->fg_data
) == 0) {
1226 lck_mtx_unlock(&fg
->fg_lock
);
1229 if (so
->so_proto
->pr_domain
!= &localdomain
||
1230 (so
->so_proto
->pr_flags
&PR_RIGHTS
) == 0) {
1231 lck_mtx_unlock(&fg
->fg_lock
);
1235 /* if this code is enabled need to run under network funnel */
1236 if (so
->so_rcv
.sb_flags
& SB_LOCK
) {
1238 * This is problematical; it's not clear
1239 * we need to wait for the sockbuf to be
1240 * unlocked (on a uniprocessor, at least),
1241 * and it's also not clear what to do
1242 * if sbwait returns an error due to receipt
1243 * of a signal. If sbwait does return
1244 * an error, we'll go into an infinite
1245 * loop. Delete all of this for now.
1247 (void) sbwait(&so
->so_rcv
);
1252 * So, Ok, it's one of our sockets and it IS externally
1253 * accessible (or was defered). Now we look
1254 * to see if we hold any file descriptors in its
1255 * message buffers. Follow those links and mark them
1256 * as accessible too.
1258 unp_scan(so
->so_rcv
.sb_mb
, unp_mark
);
1259 lck_mtx_unlock(&fg
->fg_lock
);
1261 } while (unp_defer
);
1263 * We grab an extra reference to each of the file table entries
1264 * that are not otherwise accessible and then free the rights
1265 * that are stored in messages on them.
1267 * The bug in the orginal code is a little tricky, so I'll describe
1268 * what's wrong with it here.
1270 * It is incorrect to simply unp_discard each entry for f_msgcount
1271 * times -- consider the case of sockets A and B that contain
1272 * references to each other. On a last close of some other socket,
1273 * we trigger a gc since the number of outstanding rights (unp_rights)
1274 * is non-zero. If during the sweep phase the gc code un_discards,
1275 * we end up doing a (full) closef on the descriptor. A closef on A
1276 * results in the following chain. Closef calls soo_close, which
1277 * calls soclose. Soclose calls first (through the switch
1278 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply
1279 * returns because the previous instance had set unp_gcing, and
1280 * we return all the way back to soclose, which marks the socket
1281 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush
1282 * to free up the rights that are queued in messages on the socket A,
1283 * i.e., the reference on B. The sorflush calls via the dom_dispose
1284 * switch unp_dispose, which unp_scans with unp_discard. This second
1285 * instance of unp_discard just calls closef on B.
1287 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1288 * which results in another closef on A. Unfortunately, A is already
1289 * being closed, and the descriptor has already been marked with
1290 * SS_NOFDREF, and soclose panics at this point.
1292 * Here, we first take an extra reference to each inaccessible
1293 * descriptor. Then, we call sorflush ourself, since we know
1294 * it is a Unix domain socket anyhow. After we destroy all the
1295 * rights carried in messages, we do a last closef to get rid
1296 * of our extra reference. This is the last close, and the
1297 * unp_detach etc will shut down the socket.
1299 * 91/09/19, bsy@cs.cmu.edu
1301 extra_ref
= _MALLOC(nfiles
* sizeof(struct fileglob
*), M_FILEGLOB
, M_WAITOK
);
1302 for (nunref
= 0, fg
= fmsghead
.lh_first
, fpp
= extra_ref
; fg
!= 0;
1304 lck_mtx_lock(&fg
->fg_lock
);
1306 nextfg
= fg
->f_msglist
.le_next
;
1308 * If it's not open, skip it
1310 if (fg
->fg_count
== 0) {
1311 lck_mtx_unlock(&fg
->fg_lock
);
1315 * If all refs are from msgs, and it's not marked accessible
1316 * then it must be referenced from some unreachable cycle
1317 * of (shut-down) FDs, so include it in our
1318 * list of FDs to remove
1320 if (fg
->fg_count
== fg
->fg_msgcount
&& !(fg
->fg_flag
& FMARK
)) {
1325 lck_mtx_unlock(&fg
->fg_lock
);
1328 * for each FD on our hit list, do the following two things
1330 for (i
= nunref
, fpp
= extra_ref
; --i
>= 0; ++fpp
) {
1331 struct fileglob
*tfg
;
1335 if (tfg
->fg_type
== DTYPE_SOCKET
&& tfg
->fg_data
!= NULL
) {
1336 sorflush((struct socket
*)(tfg
->fg_data
));
1339 for (i
= nunref
, fpp
= extra_ref
; --i
>= 0; ++fpp
)
1340 closef_locked((struct fileproc
*)0, *fpp
, (struct proc
*) NULL
);
1342 FREE((caddr_t
)extra_ref
, M_FILEGLOB
);
1347 unp_dispose(struct mbuf
*m
)
1351 unp_scan(m
, unp_discard
);
1361 cru2x(p
->p_ucred
, &unp
->unp_peercred
);
1362 unp
->unp_flags
|= UNP_HAVEPCCACHED
;
1366 /* should run under kernel funnel */
1370 void (*op
)(struct fileglob
*))
1373 struct fileglob
**rp
;
1379 for (m
= m0
; m
; m
= m
->m_next
)
1380 if (m
->m_type
== MT_CONTROL
&&
1381 (size_t) m
->m_len
>= sizeof(*cm
)) {
1382 cm
= mtod(m
, struct cmsghdr
*);
1383 if (cm
->cmsg_level
!= SOL_SOCKET
||
1384 cm
->cmsg_type
!= SCM_RIGHTS
)
1386 qfds
= (cm
->cmsg_len
- sizeof *cm
)
1387 / sizeof (struct fileglob
*);
1388 rp
= (struct fileglob
**)(cm
+ 1);
1389 for (i
= 0; i
< qfds
; i
++)
1391 break; /* XXX, but saves time */
1397 /* should run under kernel funnel */
1399 unp_mark(struct fileglob
*fg
)
1401 lck_mtx_lock(&fg
->fg_lock
);
1403 if (fg
->fg_flag
& FMARK
) {
1404 lck_mtx_unlock(&fg
->fg_lock
);
1407 fg
->fg_flag
|= (FMARK
|FDEFER
);
1409 lck_mtx_unlock(&fg
->fg_lock
);
1414 /* should run under kernel funnel */
1417 struct fileglob
*fg
;
1419 struct proc
*p
= current_proc(); /* XXX */
1422 unp_discard_fdlocked(fg
, p
);
1426 unp_discard_fdlocked(fg
, p
)
1427 struct fileglob
*fg
;
1434 (void) closef_locked((struct fileproc
*)0, fg
, p
);