2 * Copyright (c) 2000 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> */
64 #include <sys/filedesc.h>
67 #include <sys/namei.h>
69 #include <sys/protosw.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
73 #include <sys/sysctl.h>
75 #include <sys/unpcb.h>
76 #include <sys/vnode.h>
78 #include <kern/zalloc.h>
80 struct zone
*unp_zone
;
81 static unp_gen_t unp_gencnt
;
82 static u_int unp_count
;
84 static struct unp_head unp_shead
, unp_dhead
;
87 * Unix communications domain.
91 * rethink name space problems
92 * need a proper out-of-band
95 static struct sockaddr sun_noname
= { sizeof(sun_noname
), AF_LOCAL
};
96 static ino_t unp_ino
; /* prototype for fake inode numbers */
98 static int unp_attach
__P((struct socket
*));
99 static void unp_detach
__P((struct unpcb
*));
100 static int unp_bind
__P((struct unpcb
*,struct sockaddr
*, struct proc
*));
101 static int unp_connect
__P((struct socket
*,struct sockaddr
*,
103 static void unp_disconnect
__P((struct unpcb
*));
104 static void unp_shutdown
__P((struct unpcb
*));
105 static void unp_drop
__P((struct unpcb
*, int));
106 static void unp_gc
__P((void));
107 static void unp_scan
__P((struct mbuf
*, void (*)(struct file
*)));
108 static void unp_mark
__P((struct file
*));
109 static void unp_discard
__P((struct file
*));
110 static int unp_internalize
__P((struct mbuf
*, struct proc
*));
113 uipc_abort(struct socket
*so
)
115 struct unpcb
*unp
= sotounpcb(so
);
119 unp_drop(unp
, ECONNABORTED
);
124 uipc_accept(struct socket
*so
, struct sockaddr
**nam
)
126 struct unpcb
*unp
= sotounpcb(so
);
132 * Pass back name of connected socket,
133 * if it was bound and we are still connected
134 * (our peer may have closed already!).
136 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
) {
137 *nam
= dup_sockaddr((struct sockaddr
*)unp
->unp_conn
->unp_addr
,
140 *nam
= dup_sockaddr((struct sockaddr
*)&sun_noname
, 1);
146 uipc_attach(struct socket
*so
, int proto
, struct proc
*p
)
148 struct unpcb
*unp
= sotounpcb(so
);
152 return unp_attach(so
);
156 uipc_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
158 struct unpcb
*unp
= sotounpcb(so
);
163 return unp_bind(unp
, nam
, p
);
167 uipc_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
169 struct unpcb
*unp
= sotounpcb(so
);
173 return unp_connect(so
, nam
, p
);
177 uipc_connect2(struct socket
*so1
, struct socket
*so2
)
179 struct unpcb
*unp
= sotounpcb(so1
);
184 return unp_connect2(so1
, so2
);
187 /* control is EOPNOTSUPP */
190 uipc_detach(struct socket
*so
)
192 struct unpcb
*unp
= sotounpcb(so
);
202 uipc_disconnect(struct socket
*so
)
204 struct unpcb
*unp
= sotounpcb(so
);
213 uipc_listen(struct socket
*so
, struct proc
*p
)
215 struct unpcb
*unp
= sotounpcb(so
);
217 if (unp
== 0 || unp
->unp_vnode
== 0)
223 uipc_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
225 struct unpcb
*unp
= sotounpcb(so
);
229 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
)
230 *nam
= dup_sockaddr((struct sockaddr
*)unp
->unp_conn
->unp_addr
,
236 uipc_rcvd(struct socket
*so
, int flags
)
238 struct unpcb
*unp
= sotounpcb(so
);
243 switch (so
->so_type
) {
245 panic("uipc_rcvd DGRAM?");
249 #define rcv (&so->so_rcv)
250 #define snd (&so2->so_snd)
251 if (unp
->unp_conn
== 0)
253 so2
= unp
->unp_conn
->unp_socket
;
255 * Adjust backpressure on sender
256 * and wakeup any waiting to write.
258 snd
->sb_mbmax
+= unp
->unp_mbcnt
- rcv
->sb_mbcnt
;
259 unp
->unp_mbcnt
= rcv
->sb_mbcnt
;
260 snd
->sb_hiwat
+= unp
->unp_cc
- rcv
->sb_cc
;
261 unp
->unp_cc
= rcv
->sb_cc
;
268 panic("uipc_rcvd unknown socktype");
273 /* pru_rcvoob is EOPNOTSUPP */
276 uipc_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
277 struct mbuf
*control
, struct proc
*p
)
280 struct unpcb
*unp
= sotounpcb(so
);
287 if (flags
& PRUS_OOB
) {
292 if (control
&& (error
= unp_internalize(control
, p
)))
295 switch (so
->so_type
) {
298 struct sockaddr
*from
;
305 error
= unp_connect(so
, nam
, p
);
309 if (unp
->unp_conn
== 0) {
314 so2
= unp
->unp_conn
->unp_socket
;
316 from
= (struct sockaddr
*)unp
->unp_addr
;
319 if (sbappendaddr(&so2
->so_rcv
, from
, m
, control
)) {
331 #define rcv (&so2->so_rcv)
332 #define snd (&so->so_snd)
333 /* Connect if not connected yet. */
335 * Note: A better implementation would complain
336 * if not equal to the peer's address.
338 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
340 error
= unp_connect(so
, nam
, p
);
349 if (so
->so_state
& SS_CANTSENDMORE
) {
353 if (unp
->unp_conn
== 0)
354 panic("uipc_send connected but no connection?");
355 so2
= unp
->unp_conn
->unp_socket
;
357 * Send to paired receive port, and then reduce
358 * send buffer hiwater marks to maintain backpressure.
362 if (sbappendcontrol(rcv
, m
, control
))
367 rcv
->sb_mbcnt
- unp
->unp_conn
->unp_mbcnt
;
368 unp
->unp_conn
->unp_mbcnt
= rcv
->sb_mbcnt
;
369 snd
->sb_hiwat
-= rcv
->sb_cc
- unp
->unp_conn
->unp_cc
;
370 unp
->unp_conn
->unp_cc
= rcv
->sb_cc
;
378 panic("uipc_send unknown socktype");
382 * SEND_EOF is equivalent to a SEND followed by
385 if (flags
& PRUS_EOF
) {
399 uipc_sense(struct socket
*so
, struct stat
*sb
)
401 struct unpcb
*unp
= sotounpcb(so
);
406 sb
->st_blksize
= so
->so_snd
.sb_hiwat
;
407 if (so
->so_type
== SOCK_STREAM
&& unp
->unp_conn
!= 0) {
408 so2
= unp
->unp_conn
->unp_socket
;
409 sb
->st_blksize
+= so2
->so_rcv
.sb_cc
;
412 if (unp
->unp_ino
== 0)
413 unp
->unp_ino
= unp_ino
++;
414 sb
->st_ino
= unp
->unp_ino
;
419 uipc_shutdown(struct socket
*so
)
421 struct unpcb
*unp
= sotounpcb(so
);
431 uipc_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
433 struct unpcb
*unp
= sotounpcb(so
);
438 *nam
= dup_sockaddr((struct sockaddr
*)unp
->unp_addr
, 1);
442 struct pr_usrreqs uipc_usrreqs
= {
443 uipc_abort
, uipc_accept
, uipc_attach
, uipc_bind
, uipc_connect
,
444 uipc_connect2
, pru_control_notsupp
, uipc_detach
, uipc_disconnect
,
445 uipc_listen
, uipc_peeraddr
, uipc_rcvd
, pru_rcvoob_notsupp
,
446 uipc_send
, uipc_sense
, uipc_shutdown
, uipc_sockaddr
,
447 sosend
, soreceive
, sopoll
451 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
452 * for stream sockets, although the total for sender and receiver is
453 * actually only PIPSIZ.
454 * Datagram sockets really use the sendspace as the maximum datagram size,
455 * and don't really want to reserve the sendspace. Their recvspace should
456 * be large enough for at least one max-size datagram plus address.
461 static u_long unpst_sendspace
= PIPSIZ
;
462 static u_long unpst_recvspace
= PIPSIZ
;
463 static u_long unpdg_sendspace
= 2*1024; /* really max datagram size */
464 static u_long unpdg_recvspace
= 4*1024;
466 static int unp_rights
; /* file descriptors in flight */
468 SYSCTL_DECL(_net_local_stream
);
469 SYSCTL_INT(_net_local_stream
, OID_AUTO
, sendspace
, CTLFLAG_RW
,
470 &unpst_sendspace
, 0, "");
471 SYSCTL_INT(_net_local_stream
, OID_AUTO
, recvspace
, CTLFLAG_RW
,
472 &unpst_recvspace
, 0, "");
473 SYSCTL_DECL(_net_local_dgram
);
474 SYSCTL_INT(_net_local_dgram
, OID_AUTO
, maxdgram
, CTLFLAG_RW
,
475 &unpdg_sendspace
, 0, "");
476 SYSCTL_INT(_net_local_dgram
, OID_AUTO
, recvspace
, CTLFLAG_RW
,
477 &unpdg_recvspace
, 0, "");
478 SYSCTL_DECL(_net_local
);
479 SYSCTL_INT(_net_local
, OID_AUTO
, inflight
, CTLFLAG_RD
, &unp_rights
, 0, "");
485 register struct unpcb
*unp
;
488 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
489 switch (so
->so_type
) {
492 error
= soreserve(so
, unpst_sendspace
, unpst_recvspace
);
496 error
= soreserve(so
, unpdg_sendspace
, unpdg_recvspace
);
505 unp
= zalloc(unp_zone
);
508 bzero(unp
, sizeof *unp
);
509 unp
->unp_gencnt
= ++unp_gencnt
;
511 LIST_INIT(&unp
->unp_refs
);
512 unp
->unp_socket
= so
;
513 LIST_INSERT_HEAD(so
->so_type
== SOCK_DGRAM
? &unp_dhead
514 : &unp_shead
, unp
, unp_link
);
515 so
->so_pcb
= (caddr_t
)unp
;
521 register struct unpcb
*unp
;
523 LIST_REMOVE(unp
, unp_link
);
524 unp
->unp_gencnt
= ++unp_gencnt
;
526 if (unp
->unp_vnode
) {
527 unp
->unp_vnode
->v_socket
= 0;
528 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
529 vrele(unp
->unp_vnode
);
530 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
535 while (unp
->unp_refs
.lh_first
)
536 unp_drop(unp
->unp_refs
.lh_first
, ECONNRESET
);
537 soisdisconnected(unp
->unp_socket
);
538 unp
->unp_socket
->so_pcb
= 0;
541 * Normally the receive buffer is flushed later,
542 * in sofree, but if our receive buffer holds references
543 * to descriptors that are now garbage, we will dispose
544 * of those descriptor references after the garbage collector
545 * gets them (resulting in a "panic: closef: count < 0").
547 sorflush(unp
->unp_socket
);
551 FREE(unp
->unp_addr
, M_SONAME
);
552 zfree(unp_zone
, unp
);
556 unp_bind(unp
, nam
, p
)
558 struct sockaddr
*nam
;
561 struct sockaddr_un
*soun
= (struct sockaddr_un
*)nam
;
562 register struct vnode
*vp
;
566 char buf
[SOCK_MAXADDRLEN
];
568 if (unp
->unp_vnode
!= NULL
)
570 #define offsetof(s, e) ((char *)&((s *)0)->e - (char *)((s *)0))
571 namelen
= soun
->sun_len
- offsetof(struct sockaddr_un
, sun_path
);
574 strncpy(buf
, soun
->sun_path
, namelen
);
575 buf
[namelen
] = 0; /* null-terminate the string */
576 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
577 NDINIT(&nd
, CREATE
, FOLLOW
| LOCKPARENT
, UIO_SYSSPACE
,
579 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
582 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
587 VOP_ABORTOP(nd
.ni_dvp
, &nd
.ni_cnd
);
593 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
597 vattr
.va_type
= VSOCK
;
598 vattr
.va_mode
= (ACCESSPERMS
& ~p
->p_fd
->fd_cmask
);
599 VOP_LEASE(nd
.ni_dvp
, p
, p
->p_ucred
, LEASE_WRITE
);
600 error
= VOP_CREATE(nd
.ni_dvp
, &nd
.ni_vp
, &nd
.ni_cnd
, &vattr
);
602 /* In FreeBSD create leave s parent held ; not here */
606 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
610 vp
->v_socket
= unp
->unp_socket
;
612 unp
->unp_addr
= (struct sockaddr_un
*)dup_sockaddr(nam
, 1);
613 VOP_UNLOCK(vp
, 0, p
);
614 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
619 unp_connect(so
, nam
, p
)
621 struct sockaddr
*nam
;
624 register struct sockaddr_un
*soun
= (struct sockaddr_un
*)nam
;
625 register struct vnode
*vp
;
626 register struct socket
*so2
, *so3
;
627 struct unpcb
*unp2
, *unp3
;
630 char buf
[SOCK_MAXADDRLEN
];
632 len
= nam
->sa_len
- offsetof(struct sockaddr_un
, sun_path
);
635 strncpy(buf
, soun
->sun_path
, len
);
638 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
639 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
, buf
, p
);
642 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
646 if (vp
->v_type
!= VSOCK
) {
650 error
= VOP_ACCESS(vp
, VWRITE
, p
->p_ucred
, p
);
655 error
= ECONNREFUSED
;
658 if (so
->so_type
!= so2
->so_type
) {
662 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
663 if (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) {
664 if ((so2
->so_options
& SO_ACCEPTCONN
) == 0 ||
665 (so3
= sonewconn(so2
, 0)) == 0) {
666 error
= ECONNREFUSED
;
667 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
670 unp2
= sotounpcb(so2
);
671 unp3
= sotounpcb(so3
);
673 unp3
->unp_addr
= (struct sockaddr_un
*)
674 dup_sockaddr((struct sockaddr
*)
678 error
= unp_connect2(so
, so2
);
679 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
682 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
687 unp_connect2(so
, so2
)
688 register struct socket
*so
;
689 register struct socket
*so2
;
691 register struct unpcb
*unp
= sotounpcb(so
);
692 register struct unpcb
*unp2
;
694 if (so2
->so_type
!= so
->so_type
)
696 unp2
= sotounpcb(so2
);
698 /* Verify both sockets are still opened */
699 if (unp
== 0 || unp2
== 0)
702 unp
->unp_conn
= unp2
;
703 switch (so
->so_type
) {
706 LIST_INSERT_HEAD(&unp2
->unp_refs
, unp
, unp_reflink
);
711 unp2
->unp_conn
= unp
;
717 panic("unp_connect2");
726 register struct unpcb
*unp2
= unp
->unp_conn
;
731 switch (unp
->unp_socket
->so_type
) {
734 LIST_REMOVE(unp
, unp_reflink
);
735 unp
->unp_socket
->so_state
&= ~SS_ISCONNECTED
;
739 soisdisconnected(unp
->unp_socket
);
741 soisdisconnected(unp2
->unp_socket
);
757 unp_pcblist SYSCTL_HANDLER_ARGS
760 struct unpcb
*unp
, **unp_list
;
763 struct unp_head
*head
;
765 head
= ((intptr_t)arg1
== SOCK_DGRAM
? &unp_dhead
: &unp_shead
);
768 * The process of preparing the PCB list is too time-consuming and
769 * resource-intensive to repeat twice on every request.
771 if (req
->oldptr
== 0) {
773 req
->oldidx
= 2 * (sizeof xug
)
774 + (n
+ n
/8) * sizeof(struct xunpcb
);
778 if (req
->newptr
!= 0)
782 * OK, now we're committed to doing something.
787 xug
.xug_len
= sizeof xug
;
789 xug
.xug_gen
= gencnt
;
790 xug
.xug_sogen
= so_gencnt
;
791 error
= SYSCTL_OUT(req
, &xug
, sizeof xug
);
796 * We are done if there is no pcb
801 unp_list
= _MALLOC(n
* sizeof *unp_list
, M_TEMP
, M_WAITOK
);
805 for (unp
= head
->lh_first
, i
= 0; unp
&& i
< n
;
806 unp
= unp
->unp_link
.le_next
) {
807 if (unp
->unp_gencnt
<= gencnt
)
810 n
= i
; /* in case we lost some during malloc */
813 for (i
= 0; i
< n
; i
++) {
815 if (unp
->unp_gencnt
<= gencnt
) {
817 xu
.xu_len
= sizeof xu
;
820 * XXX - need more locking here to protect against
821 * connect/disconnect races for SMP.
824 bcopy(unp
->unp_addr
, &xu
.xu_addr
,
825 unp
->unp_addr
->sun_len
);
826 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
)
827 bcopy(unp
->unp_conn
->unp_addr
,
829 unp
->unp_conn
->unp_addr
->sun_len
);
830 bcopy(unp
, &xu
.xu_unp
, sizeof *unp
);
831 sotoxsocket(unp
->unp_socket
, &xu
.xu_socket
);
832 error
= SYSCTL_OUT(req
, &xu
, sizeof xu
);
837 * Give the user an updated idea of our state.
838 * If the generation differs from what we told
839 * her before, she knows that something happened
840 * while we were processing this request, and it
841 * might be necessary to retry.
843 xug
.xug_gen
= unp_gencnt
;
844 xug
.xug_sogen
= so_gencnt
;
845 xug
.xug_count
= unp_count
;
846 error
= SYSCTL_OUT(req
, &xug
, sizeof xug
);
848 FREE(unp_list
, M_TEMP
);
852 SYSCTL_PROC(_net_local_dgram
, OID_AUTO
, pcblist
, CTLFLAG_RD
,
853 (caddr_t
)(long)SOCK_DGRAM
, 0, unp_pcblist
, "S,xunpcb",
854 "List of active local datagram sockets");
855 SYSCTL_PROC(_net_local_stream
, OID_AUTO
, pcblist
, CTLFLAG_RD
,
856 (caddr_t
)(long)SOCK_STREAM
, 0, unp_pcblist
, "S,xunpcb",
857 "List of active local stream sockets");
865 if (unp
->unp_socket
->so_type
== SOCK_STREAM
&& unp
->unp_conn
&&
866 (so
= unp
->unp_conn
->unp_socket
))
875 struct socket
*so
= unp
->unp_socket
;
877 so
->so_error
= errno
;
880 LIST_REMOVE(unp
, unp_link
);
881 unp
->unp_gencnt
= ++unp_gencnt
;
883 so
->so_pcb
= (caddr_t
) 0;
885 FREE(unp
->unp_addr
, M_SONAME
);
886 zfree(unp_zone
, unp
);
900 unp_externalize(rights
)
903 struct proc
*p
= current_proc(); /* XXX */
905 register struct cmsghdr
*cm
= mtod(rights
, struct cmsghdr
*);
906 register struct file
**rp
= (struct file
**)(cm
+ 1);
907 register struct file
*fp
;
908 int newfds
= (cm
->cmsg_len
- sizeof(*cm
)) / sizeof (int);
912 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
915 * if the new FD's will not fit, then we free them all
917 if (!fdavail(p
, newfds
)) {
918 for (i
= 0; i
< newfds
; i
++) {
924 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
928 * now change each pointer to an fd in the global table to
929 * an integer that is the index to the local fd table entry
930 * that we set up to point to the global one we are transferring.
931 * XXX this assumes a pointer and int are the same size...!
933 for (i
= 0; i
< newfds
; i
++) {
934 if (fdalloc(p
, 0, &f
))
935 panic("unp_externalize");
937 p
->p_fd
->fd_ofiles
[f
] = fp
;
938 *fdflags(p
, f
) &= ~UF_RESERVED
;
944 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
951 unp_zone
= zinit(sizeof(struct unpcb
),
952 (nmbclusters
* sizeof(struct unpcb
)),
956 LIST_INIT(&unp_dhead
);
957 LIST_INIT(&unp_shead
);
961 #define MIN(a,b) (((a)<(b))?(a):(b))
965 unp_internalize(control
, p
)
966 struct mbuf
*control
;
969 register struct cmsghdr
*cm
= mtod(control
, struct cmsghdr
*);
970 register struct file
**rp
;
972 register int i
, error
;
975 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
976 if (cm
->cmsg_type
!= SCM_RIGHTS
|| cm
->cmsg_level
!= SOL_SOCKET
||
977 cm
->cmsg_len
!= control
->m_len
) {
978 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
982 oldfds
= (cm
->cmsg_len
- sizeof (*cm
)) / sizeof (int);
983 rp
= (struct file
**)(cm
+ 1);
984 for (i
= 0; i
< oldfds
; i
++)
985 if (error
= fdgetf(p
, *(int *)rp
++, 0)) {
987 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
991 rp
= (struct file
**)(cm
+ 1);
992 for (i
= 0; i
< oldfds
; i
++) {
993 (void) fdgetf(p
, *(int *)rp
, &fp
);
1000 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1004 static int unp_defer
, unp_gcing
;
1009 register struct file
*fp
, *nextfp
;
1010 register struct socket
*so
;
1011 struct file
**extra_ref
, **fpp
;
1019 * before going through all this, set all FDs to
1020 * be NOT defered and NOT externally accessible
1022 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1023 for (fp
= filehead
.lh_first
; fp
!= 0; fp
= fp
->f_list
.le_next
)
1024 fp
->f_flag
&= ~(FMARK
|FDEFER
);
1026 for (fp
= filehead
.lh_first
; fp
!= 0; fp
= fp
->f_list
.le_next
) {
1028 * If the file is not open, skip it
1030 if (fcount(fp
) == 0)
1033 * If we already marked it as 'defer' in a
1034 * previous pass, then try process it this time
1037 if (fp
->f_flag
& FDEFER
) {
1038 fp
->f_flag
&= ~FDEFER
;
1042 * if it's not defered, then check if it's
1043 * already marked.. if so skip it
1045 if (fp
->f_flag
& FMARK
)
1048 * If all references are from messages
1049 * in transit, then skip it. it's not
1050 * externally accessible.
1052 if (fcount(fp
) == fp
->f_msgcount
)
1055 * If it got this far then it must be
1056 * externally accessible.
1058 fp
->f_flag
|= FMARK
;
1061 * either it was defered, or it is externally
1062 * accessible and not already marked so.
1063 * Now check if it is possibly one of OUR sockets.
1065 if (fp
->f_type
!= DTYPE_SOCKET
||
1066 (so
= (struct socket
*)fp
->f_data
) == 0)
1068 if (so
->so_proto
->pr_domain
!= &localdomain
||
1069 (so
->so_proto
->pr_flags
&PR_RIGHTS
) == 0)
1072 /* if this code is enabled need to run under network funnel */
1073 if (so
->so_rcv
.sb_flags
& SB_LOCK
) {
1075 * This is problematical; it's not clear
1076 * we need to wait for the sockbuf to be
1077 * unlocked (on a uniprocessor, at least),
1078 * and it's also not clear what to do
1079 * if sbwait returns an error due to receipt
1080 * of a signal. If sbwait does return
1081 * an error, we'll go into an infinite
1082 * loop. Delete all of this for now.
1084 (void) sbwait(&so
->so_rcv
);
1089 * So, Ok, it's one of our sockets and it IS externally
1090 * accessible (or was defered). Now we look
1091 * to see if we hold any file descriptors in its
1092 * message buffers. Follow those links and mark them
1093 * as accessible too.
1095 unp_scan(so
->so_rcv
.sb_mb
, unp_mark
);
1097 } while (unp_defer
);
1099 * We grab an extra reference to each of the file table entries
1100 * that are not otherwise accessible and then free the rights
1101 * that are stored in messages on them.
1103 * The bug in the orginal code is a little tricky, so I'll describe
1104 * what's wrong with it here.
1106 * It is incorrect to simply unp_discard each entry for f_msgcount
1107 * times -- consider the case of sockets A and B that contain
1108 * references to each other. On a last close of some other socket,
1109 * we trigger a gc since the number of outstanding rights (unp_rights)
1110 * is non-zero. If during the sweep phase the gc code un_discards,
1111 * we end up doing a (full) closef on the descriptor. A closef on A
1112 * results in the following chain. Closef calls soo_close, which
1113 * calls soclose. Soclose calls first (through the switch
1114 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply
1115 * returns because the previous instance had set unp_gcing, and
1116 * we return all the way back to soclose, which marks the socket
1117 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush
1118 * to free up the rights that are queued in messages on the socket A,
1119 * i.e., the reference on B. The sorflush calls via the dom_dispose
1120 * switch unp_dispose, which unp_scans with unp_discard. This second
1121 * instance of unp_discard just calls closef on B.
1123 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1124 * which results in another closef on A. Unfortunately, A is already
1125 * being closed, and the descriptor has already been marked with
1126 * SS_NOFDREF, and soclose panics at this point.
1128 * Here, we first take an extra reference to each inaccessible
1129 * descriptor. Then, we call sorflush ourself, since we know
1130 * it is a Unix domain socket anyhow. After we destroy all the
1131 * rights carried in messages, we do a last closef to get rid
1132 * of our extra reference. This is the last close, and the
1133 * unp_detach etc will shut down the socket.
1135 * 91/09/19, bsy@cs.cmu.edu
1137 extra_ref
= _MALLOC(nfiles
* sizeof(struct file
*), M_FILE
, M_WAITOK
);
1138 for (nunref
= 0, fp
= filehead
.lh_first
, fpp
= extra_ref
; fp
!= 0;
1140 nextfp
= fp
->f_list
.le_next
;
1142 * If it's not open, skip it
1144 if (fcount(fp
) == 0)
1147 * If all refs are from msgs, and it's not marked accessible
1148 * then it must be referenced from some unreachable cycle
1149 * of (shut-down) FDs, so include it in our
1150 * list of FDs to remove
1152 if (fcount(fp
) == fp
->f_msgcount
&& !(fp
->f_flag
& FMARK
)) {
1159 * for each FD on our hit list, do the following two things
1161 for (i
= nunref
, fpp
= extra_ref
; --i
>= 0; ++fpp
) {
1162 struct file
*tfp
= *fpp
;
1163 if (tfp
->f_type
== DTYPE_SOCKET
&& tfp
->f_data
!= NULL
) {
1164 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1165 sorflush((struct socket
*)(tfp
->f_data
));
1166 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1171 for (i
= nunref
, fpp
= extra_ref
; --i
>= 0; ++fpp
)
1172 closef(*fpp
, (struct proc
*) NULL
);
1173 FREE((caddr_t
)extra_ref
, M_FILE
);
1174 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1185 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1186 unp_scan(m
, unp_discard
);
1187 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1191 /* should run under kernel funnel */
1194 register struct mbuf
*m0
;
1195 void (*op
) __P((struct file
*));
1197 register struct mbuf
*m
;
1198 register struct file
**rp
;
1199 register struct cmsghdr
*cm
;
1204 for (m
= m0
; m
; m
= m
->m_next
)
1205 if (m
->m_type
== MT_CONTROL
&&
1206 m
->m_len
>= sizeof(*cm
)) {
1207 cm
= mtod(m
, struct cmsghdr
*);
1208 if (cm
->cmsg_level
!= SOL_SOCKET
||
1209 cm
->cmsg_type
!= SCM_RIGHTS
)
1211 qfds
= (cm
->cmsg_len
- sizeof *cm
)
1212 / sizeof (struct file
*);
1213 rp
= (struct file
**)(cm
+ 1);
1214 for (i
= 0; i
< qfds
; i
++)
1216 break; /* XXX, but saves time */
1222 /* should run under kernel funnel */
1228 if (fp
->f_flag
& FMARK
)
1231 fp
->f_flag
|= (FMARK
|FDEFER
);
1234 /* should run under kernel funnel */
1242 (void) closef(fp
, (struct proc
*)NULL
);