2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94
34 * $FreeBSD: src/sys/kern/uipc_usrreq.c,v 1.54.2.10 2003/03/04 17:28:09 nectar Exp $
35 * $DragonFly: src/sys/kern/uipc_usrreq.c,v 1.44 2008/09/06 05:44:58 dillon Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/domain.h>
42 #include <sys/fcntl.h>
43 #include <sys/malloc.h> /* XXX must be before <sys/file.h> */
46 #include <sys/filedesc.h>
48 #include <sys/nlookup.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/resourcevar.h>
54 #include <sys/mount.h>
55 #include <sys/sysctl.h>
57 #include <sys/unpcb.h>
58 #include <sys/vnode.h>
59 #include <sys/file2.h>
60 #include <sys/spinlock2.h>
63 static MALLOC_DEFINE(M_UNPCB
, "unpcb", "unpcb struct");
64 static unp_gen_t unp_gencnt
;
65 static u_int unp_count
;
67 static struct unp_head unp_shead
, unp_dhead
;
70 * Unix communications domain.
74 * rethink name space problems
75 * need a proper out-of-band
78 static struct sockaddr sun_noname
= { sizeof(sun_noname
), AF_LOCAL
};
79 static ino_t unp_ino
; /* prototype for fake inode numbers */
81 static int unp_attach (struct socket
*, struct pru_attach_info
*);
82 static void unp_detach (struct unpcb
*);
83 static int unp_bind (struct unpcb
*,struct sockaddr
*, struct thread
*);
84 static int unp_connect (struct socket
*,struct sockaddr
*,
86 static void unp_disconnect (struct unpcb
*);
87 static void unp_shutdown (struct unpcb
*);
88 static void unp_drop (struct unpcb
*, int);
89 static void unp_gc (void);
90 static int unp_gc_clearmarks(struct file
*, void *);
91 static int unp_gc_checkmarks(struct file
*, void *);
92 static int unp_gc_checkrefs(struct file
*, void *);
93 static void unp_scan (struct mbuf
*, void (*)(struct file
*, void *),
95 static void unp_mark (struct file
*, void *data
);
96 static void unp_discard (struct file
*, void *);
97 static int unp_internalize (struct mbuf
*, struct thread
*);
98 static int unp_listen (struct unpcb
*, struct thread
*);
101 uipc_abort(struct socket
*so
)
103 struct unpcb
*unp
= so
->so_pcb
;
107 unp_drop(unp
, ECONNABORTED
);
114 uipc_accept(struct socket
*so
, struct sockaddr
**nam
)
116 struct unpcb
*unp
= so
->so_pcb
;
122 * Pass back name of connected socket,
123 * if it was bound and we are still connected
124 * (our peer may have closed already!).
126 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
) {
127 *nam
= dup_sockaddr((struct sockaddr
*)unp
->unp_conn
->unp_addr
);
129 *nam
= dup_sockaddr((struct sockaddr
*)&sun_noname
);
135 uipc_attach(struct socket
*so
, int proto
, struct pru_attach_info
*ai
)
137 struct unpcb
*unp
= so
->so_pcb
;
141 return unp_attach(so
, ai
);
145 uipc_bind(struct socket
*so
, struct sockaddr
*nam
, struct thread
*td
)
147 struct unpcb
*unp
= so
->so_pcb
;
151 return unp_bind(unp
, nam
, td
);
155 uipc_connect(struct socket
*so
, struct sockaddr
*nam
, struct thread
*td
)
157 struct unpcb
*unp
= so
->so_pcb
;
161 return unp_connect(so
, nam
, td
);
165 uipc_connect2(struct socket
*so1
, struct socket
*so2
)
167 struct unpcb
*unp
= so1
->so_pcb
;
172 return unp_connect2(so1
, so2
);
175 /* control is EOPNOTSUPP */
178 uipc_detach(struct socket
*so
)
180 struct unpcb
*unp
= so
->so_pcb
;
190 uipc_disconnect(struct socket
*so
)
192 struct unpcb
*unp
= so
->so_pcb
;
201 uipc_listen(struct socket
*so
, struct thread
*td
)
203 struct unpcb
*unp
= so
->so_pcb
;
205 if (unp
== NULL
|| unp
->unp_vnode
== NULL
)
207 return unp_listen(unp
, td
);
211 uipc_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
213 struct unpcb
*unp
= so
->so_pcb
;
217 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
)
218 *nam
= dup_sockaddr((struct sockaddr
*)unp
->unp_conn
->unp_addr
);
221 * XXX: It seems that this test always fails even when
222 * connection is established. So, this else clause is
223 * added as workaround to return PF_LOCAL sockaddr.
225 *nam
= dup_sockaddr((struct sockaddr
*)&sun_noname
);
231 uipc_rcvd(struct socket
*so
, int flags
)
233 struct unpcb
*unp
= so
->so_pcb
;
238 switch (so
->so_type
) {
240 panic("uipc_rcvd DGRAM?");
245 if (unp
->unp_conn
== NULL
)
248 * Because we are transfering mbufs directly to the
249 * peer socket we have to use SSB_STOP on the sender
250 * to prevent it from building up infinite mbufs.
252 so2
= unp
->unp_conn
->unp_socket
;
253 if (so
->so_rcv
.ssb_cc
< so2
->so_snd
.ssb_hiwat
&&
254 so
->so_rcv
.ssb_mbcnt
< so2
->so_snd
.ssb_mbmax
256 so2
->so_snd
.ssb_flags
&= ~SSB_STOP
;
262 panic("uipc_rcvd unknown socktype");
267 /* pru_rcvoob is EOPNOTSUPP */
270 uipc_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
271 struct mbuf
*control
, struct thread
*td
)
274 struct unpcb
*unp
= so
->so_pcb
;
281 if (flags
& PRUS_OOB
) {
286 if (control
&& (error
= unp_internalize(control
, td
)))
289 switch (so
->so_type
) {
292 struct sockaddr
*from
;
299 error
= unp_connect(so
, nam
, td
);
303 if (unp
->unp_conn
== NULL
) {
308 so2
= unp
->unp_conn
->unp_socket
;
310 from
= (struct sockaddr
*)unp
->unp_addr
;
313 if (ssb_appendaddr(&so2
->so_rcv
, from
, m
, control
)) {
327 /* Connect if not connected yet. */
329 * Note: A better implementation would complain
330 * if not equal to the peer's address.
332 if (!(so
->so_state
& SS_ISCONNECTED
)) {
334 error
= unp_connect(so
, nam
, td
);
343 if (so
->so_state
& SS_CANTSENDMORE
) {
347 if (unp
->unp_conn
== NULL
)
348 panic("uipc_send connected but no connection?");
349 so2
= unp
->unp_conn
->unp_socket
;
351 * Send to paired receive port, and then reduce
352 * send buffer hiwater marks to maintain backpressure.
356 if (ssb_appendcontrol(&so2
->so_rcv
, m
, control
)) {
360 } else if (so
->so_type
== SOCK_SEQPACKET
) {
361 sbappendrecord(&so2
->so_rcv
.sb
, m
);
364 sbappend(&so2
->so_rcv
.sb
, m
);
369 * Because we are transfering mbufs directly to the
370 * peer socket we have to use SSB_STOP on the sender
371 * to prevent it from building up infinite mbufs.
373 if (so2
->so_rcv
.ssb_cc
>= so
->so_snd
.ssb_hiwat
||
374 so2
->so_rcv
.ssb_mbcnt
>= so
->so_snd
.ssb_mbmax
376 so
->so_snd
.ssb_flags
|= SSB_STOP
;
382 panic("uipc_send unknown socktype");
386 * SEND_EOF is equivalent to a SEND followed by a SHUTDOWN.
388 if (flags
& PRUS_EOF
) {
393 if (control
&& error
!= 0)
394 unp_dispose(control
);
405 uipc_sense(struct socket
*so
, struct stat
*sb
)
407 struct unpcb
*unp
= so
->so_pcb
;
411 sb
->st_blksize
= so
->so_snd
.ssb_hiwat
;
413 if (unp
->unp_ino
== 0) /* make up a non-zero inode number */
414 unp
->unp_ino
= (++unp_ino
== 0) ? ++unp_ino
: unp_ino
;
415 sb
->st_ino
= unp
->unp_ino
;
420 uipc_shutdown(struct socket
*so
)
422 struct unpcb
*unp
= so
->so_pcb
;
432 uipc_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
434 struct unpcb
*unp
= so
->so_pcb
;
439 *nam
= dup_sockaddr((struct sockaddr
*)unp
->unp_addr
);
443 struct pr_usrreqs uipc_usrreqs
= {
444 .pru_abort
= uipc_abort
,
445 .pru_accept
= uipc_accept
,
446 .pru_attach
= uipc_attach
,
447 .pru_bind
= uipc_bind
,
448 .pru_connect
= uipc_connect
,
449 .pru_connect2
= uipc_connect2
,
450 .pru_control
= pru_control_notsupp
,
451 .pru_detach
= uipc_detach
,
452 .pru_disconnect
= uipc_disconnect
,
453 .pru_listen
= uipc_listen
,
454 .pru_peeraddr
= uipc_peeraddr
,
455 .pru_rcvd
= uipc_rcvd
,
456 .pru_rcvoob
= pru_rcvoob_notsupp
,
457 .pru_send
= uipc_send
,
458 .pru_sense
= uipc_sense
,
459 .pru_shutdown
= uipc_shutdown
,
460 .pru_sockaddr
= uipc_sockaddr
,
461 .pru_sosend
= sosend
,
462 .pru_soreceive
= soreceive
,
467 uipc_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
469 struct unpcb
*unp
= so
->so_pcb
;
472 switch (sopt
->sopt_dir
) {
474 switch (sopt
->sopt_name
) {
476 if (unp
->unp_flags
& UNP_HAVEPC
)
477 soopt_from_kbuf(sopt
, &unp
->unp_peercred
,
478 sizeof(unp
->unp_peercred
));
480 if (so
->so_type
== SOCK_STREAM
)
482 else if (so
->so_type
== SOCK_SEQPACKET
)
502 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
503 * for stream sockets, although the total for sender and receiver is
504 * actually only PIPSIZ.
506 * Datagram sockets really use the sendspace as the maximum datagram size,
507 * and don't really want to reserve the sendspace. Their recvspace should
508 * be large enough for at least one max-size datagram plus address.
510 * We want the local send/recv space to be significant larger then lo0's
516 static u_long unpst_sendspace
= PIPSIZ
;
517 static u_long unpst_recvspace
= PIPSIZ
;
518 static u_long unpdg_sendspace
= 2*1024; /* really max datagram size */
519 static u_long unpdg_recvspace
= 4*1024;
521 static int unp_rights
; /* file descriptors in flight */
522 static struct spinlock unp_spin
= SPINLOCK_INITIALIZER(&unp_spin
);
524 SYSCTL_DECL(_net_local_seqpacket
);
525 SYSCTL_DECL(_net_local_stream
);
526 SYSCTL_INT(_net_local_stream
, OID_AUTO
, sendspace
, CTLFLAG_RW
,
527 &unpst_sendspace
, 0, "");
528 SYSCTL_INT(_net_local_stream
, OID_AUTO
, recvspace
, CTLFLAG_RW
,
529 &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, "");
537 SYSCTL_DECL(_net_local
);
538 SYSCTL_INT(_net_local
, OID_AUTO
, inflight
, CTLFLAG_RD
, &unp_rights
, 0, "");
541 unp_attach(struct socket
*so
, struct pru_attach_info
*ai
)
546 if (so
->so_snd
.ssb_hiwat
== 0 || so
->so_rcv
.ssb_hiwat
== 0) {
547 switch (so
->so_type
) {
551 error
= soreserve(so
, unpst_sendspace
, unpst_recvspace
,
556 error
= soreserve(so
, unpdg_sendspace
, unpdg_recvspace
,
566 unp
= kmalloc(sizeof(*unp
), M_UNPCB
, M_NOWAIT
|M_ZERO
);
569 unp
->unp_gencnt
= ++unp_gencnt
;
571 LIST_INIT(&unp
->unp_refs
);
572 unp
->unp_socket
= so
;
573 unp
->unp_rvnode
= ai
->fd_rdir
; /* jail cruft XXX JH */
574 LIST_INSERT_HEAD(so
->so_type
== SOCK_DGRAM
? &unp_dhead
575 : &unp_shead
, unp
, unp_link
);
576 so
->so_pcb
= (caddr_t
)unp
;
581 unp_detach(struct unpcb
*unp
)
583 LIST_REMOVE(unp
, unp_link
);
584 unp
->unp_gencnt
= ++unp_gencnt
;
586 if (unp
->unp_vnode
) {
587 unp
->unp_vnode
->v_socket
= NULL
;
588 vrele(unp
->unp_vnode
);
589 unp
->unp_vnode
= NULL
;
593 while (!LIST_EMPTY(&unp
->unp_refs
))
594 unp_drop(LIST_FIRST(&unp
->unp_refs
), ECONNRESET
);
595 soisdisconnected(unp
->unp_socket
);
596 unp
->unp_socket
->so_pcb
= NULL
;
599 * Normally the receive buffer is flushed later,
600 * in sofree, but if our receive buffer holds references
601 * to descriptors that are now garbage, we will dispose
602 * of those descriptor references after the garbage collector
603 * gets them (resulting in a "panic: closef: count < 0").
605 sorflush(unp
->unp_socket
);
609 kfree(unp
->unp_addr
, M_SONAME
);
614 unp_bind(struct unpcb
*unp
, struct sockaddr
*nam
, struct thread
*td
)
616 struct proc
*p
= td
->td_proc
;
617 struct sockaddr_un
*soun
= (struct sockaddr_un
*)nam
;
621 struct nlookupdata nd
;
622 char buf
[SOCK_MAXADDRLEN
];
624 if (unp
->unp_vnode
!= NULL
)
626 namelen
= soun
->sun_len
- offsetof(struct sockaddr_un
, sun_path
);
629 strncpy(buf
, soun
->sun_path
, namelen
);
630 buf
[namelen
] = 0; /* null-terminate the string */
631 error
= nlookup_init(&nd
, buf
, UIO_SYSSPACE
,
632 NLC_LOCKVP
| NLC_CREATE
| NLC_REFDVP
);
634 error
= nlookup(&nd
);
635 if (error
== 0 && nd
.nl_nch
.ncp
->nc_vp
!= NULL
)
641 vattr
.va_type
= VSOCK
;
642 vattr
.va_mode
= (ACCESSPERMS
& ~p
->p_fd
->fd_cmask
);
643 error
= VOP_NCREATE(&nd
.nl_nch
, nd
.nl_dvp
, &vp
, nd
.nl_cred
, &vattr
);
645 vp
->v_socket
= unp
->unp_socket
;
647 unp
->unp_addr
= (struct sockaddr_un
*)dup_sockaddr(nam
);
656 unp_connect(struct socket
*so
, struct sockaddr
*nam
, struct thread
*td
)
658 struct proc
*p
= td
->td_proc
;
659 struct sockaddr_un
*soun
= (struct sockaddr_un
*)nam
;
661 struct socket
*so2
, *so3
;
662 struct unpcb
*unp
, *unp2
, *unp3
;
664 struct nlookupdata nd
;
665 char buf
[SOCK_MAXADDRLEN
];
669 len
= nam
->sa_len
- offsetof(struct sockaddr_un
, sun_path
);
672 strncpy(buf
, soun
->sun_path
, len
);
676 error
= nlookup_init(&nd
, buf
, UIO_SYSSPACE
, NLC_FOLLOW
);
678 error
= nlookup(&nd
);
680 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
685 if (vp
->v_type
!= VSOCK
) {
689 error
= VOP_ACCESS(vp
, VWRITE
, p
->p_ucred
);
694 error
= ECONNREFUSED
;
697 if (so
->so_type
!= so2
->so_type
) {
701 if (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) {
702 if (!(so2
->so_options
& SO_ACCEPTCONN
) ||
703 (so3
= sonewconn(so2
, 0)) == NULL
) {
704 error
= ECONNREFUSED
;
711 unp3
->unp_addr
= (struct sockaddr_un
*)
712 dup_sockaddr((struct sockaddr
*)unp2
->unp_addr
);
715 * unp_peercred management:
717 * The connecter's (client's) credentials are copied
718 * from its process structure at the time of connect()
721 cru2x(p
->p_ucred
, &unp3
->unp_peercred
);
722 unp3
->unp_flags
|= UNP_HAVEPC
;
724 * The receiver's (server's) credentials are copied
725 * from the unp_peercred member of socket on which the
726 * former called listen(); unp_listen() cached that
727 * process's credentials at that time so we can use
730 KASSERT(unp2
->unp_flags
& UNP_HAVEPCCACHED
,
731 ("unp_connect: listener without cached peercred"));
732 memcpy(&unp
->unp_peercred
, &unp2
->unp_peercred
,
733 sizeof(unp
->unp_peercred
));
734 unp
->unp_flags
|= UNP_HAVEPC
;
738 error
= unp_connect2(so
, so2
);
745 unp_connect2(struct socket
*so
, struct socket
*so2
)
747 struct unpcb
*unp
= so
->so_pcb
;
750 if (so2
->so_type
!= so
->so_type
)
753 unp
->unp_conn
= unp2
;
754 switch (so
->so_type
) {
757 LIST_INSERT_HEAD(&unp2
->unp_refs
, unp
, unp_reflink
);
763 unp2
->unp_conn
= unp
;
769 panic("unp_connect2");
775 unp_disconnect(struct unpcb
*unp
)
777 struct unpcb
*unp2
= unp
->unp_conn
;
782 unp
->unp_conn
= NULL
;
784 switch (unp
->unp_socket
->so_type
) {
786 LIST_REMOVE(unp
, unp_reflink
);
787 unp
->unp_socket
->so_state
&= ~SS_ISCONNECTED
;
791 soisdisconnected(unp
->unp_socket
);
792 unp2
->unp_conn
= NULL
;
793 soisdisconnected(unp2
->unp_socket
);
800 unp_abort(struct unpcb
*unp
)
808 prison_unpcb(struct thread
*td
, struct unpcb
*unp
)
814 if ((p
= td
->td_proc
) == NULL
)
816 if (!p
->p_ucred
->cr_prison
)
818 if (p
->p_fd
->fd_rdir
== unp
->unp_rvnode
)
824 unp_pcblist(SYSCTL_HANDLER_ARGS
)
827 struct unpcb
*unp
, **unp_list
;
829 struct unp_head
*head
;
831 head
= ((intptr_t)arg1
== SOCK_DGRAM
? &unp_dhead
: &unp_shead
);
833 KKASSERT(curproc
!= NULL
);
836 * The process of preparing the PCB list is too time-consuming and
837 * resource-intensive to repeat twice on every request.
839 if (req
->oldptr
== NULL
) {
841 req
->oldidx
= (n
+ n
/8) * sizeof(struct xunpcb
);
845 if (req
->newptr
!= NULL
)
849 * OK, now we're committed to doing something.
854 unp_list
= kmalloc(n
* sizeof *unp_list
, M_TEMP
, M_WAITOK
);
856 for (unp
= LIST_FIRST(head
), i
= 0; unp
&& i
< n
;
857 unp
= LIST_NEXT(unp
, unp_link
)) {
858 if (unp
->unp_gencnt
<= gencnt
&& !prison_unpcb(req
->td
, unp
))
861 n
= i
; /* in case we lost some during malloc */
864 for (i
= 0; i
< n
; i
++) {
866 if (unp
->unp_gencnt
<= gencnt
) {
868 xu
.xu_len
= sizeof xu
;
871 * XXX - need more locking here to protect against
872 * connect/disconnect races for SMP.
875 bcopy(unp
->unp_addr
, &xu
.xu_addr
,
876 unp
->unp_addr
->sun_len
);
877 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
)
878 bcopy(unp
->unp_conn
->unp_addr
,
880 unp
->unp_conn
->unp_addr
->sun_len
);
881 bcopy(unp
, &xu
.xu_unp
, sizeof *unp
);
882 sotoxsocket(unp
->unp_socket
, &xu
.xu_socket
);
883 error
= SYSCTL_OUT(req
, &xu
, sizeof xu
);
886 kfree(unp_list
, M_TEMP
);
890 SYSCTL_PROC(_net_local_dgram
, OID_AUTO
, pcblist
, CTLFLAG_RD
,
891 (caddr_t
)(long)SOCK_DGRAM
, 0, unp_pcblist
, "S,xunpcb",
892 "List of active local datagram sockets");
893 SYSCTL_PROC(_net_local_stream
, OID_AUTO
, pcblist
, CTLFLAG_RD
,
894 (caddr_t
)(long)SOCK_STREAM
, 0, unp_pcblist
, "S,xunpcb",
895 "List of active local stream sockets");
896 SYSCTL_PROC(_net_local_seqpacket
, OID_AUTO
, pcblist
, CTLFLAG_RD
,
897 (caddr_t
)(long)SOCK_SEQPACKET
, 0, unp_pcblist
, "S,xunpcb",
898 "List of active local seqpacket stream sockets");
901 unp_shutdown(struct unpcb
*unp
)
905 if ((unp
->unp_socket
->so_type
== SOCK_STREAM
||
906 unp
->unp_socket
->so_type
== SOCK_SEQPACKET
) &&
907 unp
->unp_conn
!= NULL
&& (so
= unp
->unp_conn
->unp_socket
)) {
913 unp_drop(struct unpcb
*unp
, int err
)
915 struct socket
*so
= unp
->unp_socket
;
930 unp_externalize(struct mbuf
*rights
)
932 struct proc
*p
= curproc
; /* XXX */
934 struct cmsghdr
*cm
= mtod(rights
, struct cmsghdr
*);
938 int newfds
= (cm
->cmsg_len
- (CMSG_DATA(cm
) - (u_char
*)cm
))
939 / sizeof (struct file
*);
943 * if the new FD's will not fit, then we free them all
945 if (!fdavail(p
, newfds
)) {
946 rp
= (struct file
**)CMSG_DATA(cm
);
947 for (i
= 0; i
< newfds
; i
++) {
950 * zero the pointer before calling unp_discard,
951 * since it may end up in unp_gc()..
954 unp_discard(fp
, NULL
);
959 * now change each pointer to an fd in the global table to
960 * an integer that is the index to the local fd table entry
961 * that we set up to point to the global one we are transferring.
962 * If sizeof (struct file *) is bigger than or equal to sizeof int,
963 * then do it in forward order. In that case, an integer will
964 * always come in the same place or before its corresponding
965 * struct file pointer.
966 * If sizeof (struct file *) is smaller than sizeof int, then
967 * do it in reverse order.
969 if (sizeof (struct file
*) >= sizeof (int)) {
970 fdp
= (int *)(cm
+ 1);
971 rp
= (struct file
**)CMSG_DATA(cm
);
972 for (i
= 0; i
< newfds
; i
++) {
973 if (fdalloc(p
, 0, &f
))
974 panic("unp_externalize");
978 spin_lock_wr(&unp_spin
);
981 spin_unlock_wr(&unp_spin
);
985 fdp
= (int *)(cm
+ 1) + newfds
- 1;
986 rp
= (struct file
**)CMSG_DATA(cm
) + newfds
- 1;
987 for (i
= 0; i
< newfds
; i
++) {
988 if (fdalloc(p
, 0, &f
))
989 panic("unp_externalize");
993 spin_lock_wr(&unp_spin
);
996 spin_unlock_wr(&unp_spin
);
1002 * Adjust length, in case sizeof(struct file *) and sizeof(int)
1005 cm
->cmsg_len
= CMSG_LEN(newfds
* sizeof(int));
1006 rights
->m_len
= cm
->cmsg_len
;
1013 LIST_INIT(&unp_dhead
);
1014 LIST_INIT(&unp_shead
);
1015 spin_init(&unp_spin
);
1019 unp_internalize(struct mbuf
*control
, struct thread
*td
)
1021 struct proc
*p
= td
->td_proc
;
1022 struct filedesc
*fdescp
;
1023 struct cmsghdr
*cm
= mtod(control
, struct cmsghdr
*);
1027 struct cmsgcred
*cmcred
;
1033 if ((cm
->cmsg_type
!= SCM_RIGHTS
&& cm
->cmsg_type
!= SCM_CREDS
) ||
1034 cm
->cmsg_level
!= SOL_SOCKET
|| cm
->cmsg_len
!= control
->m_len
)
1038 * Fill in credential information.
1040 if (cm
->cmsg_type
== SCM_CREDS
) {
1041 cmcred
= (struct cmsgcred
*)(cm
+ 1);
1042 cmcred
->cmcred_pid
= p
->p_pid
;
1043 cmcred
->cmcred_uid
= p
->p_ucred
->cr_ruid
;
1044 cmcred
->cmcred_gid
= p
->p_ucred
->cr_rgid
;
1045 cmcred
->cmcred_euid
= p
->p_ucred
->cr_uid
;
1046 cmcred
->cmcred_ngroups
= MIN(p
->p_ucred
->cr_ngroups
,
1048 for (i
= 0; i
< cmcred
->cmcred_ngroups
; i
++)
1049 cmcred
->cmcred_groups
[i
] = p
->p_ucred
->cr_groups
[i
];
1053 oldfds
= (cm
->cmsg_len
- sizeof (*cm
)) / sizeof (int);
1055 * check that all the FDs passed in refer to legal OPEN files
1056 * If not, reject the entire operation.
1058 fdp
= (int *)(cm
+ 1);
1059 for (i
= 0; i
< oldfds
; i
++) {
1061 if ((unsigned)fd
>= fdescp
->fd_nfiles
||
1062 fdescp
->fd_files
[fd
].fp
== NULL
)
1064 if (fdescp
->fd_files
[fd
].fp
->f_type
== DTYPE_KQUEUE
)
1065 return (EOPNOTSUPP
);
1068 * Now replace the integer FDs with pointers to
1069 * the associated global file table entry..
1070 * Allocate a bigger buffer as necessary. But if an cluster is not
1071 * enough, return E2BIG.
1073 newlen
= CMSG_LEN(oldfds
* sizeof(struct file
*));
1074 if (newlen
> MCLBYTES
)
1076 if (newlen
- control
->m_len
> M_TRAILINGSPACE(control
)) {
1077 if (control
->m_flags
& M_EXT
)
1079 MCLGET(control
, MB_WAIT
);
1080 if (!(control
->m_flags
& M_EXT
))
1083 /* copy the data to the cluster */
1084 memcpy(mtod(control
, char *), cm
, cm
->cmsg_len
);
1085 cm
= mtod(control
, struct cmsghdr
*);
1089 * Adjust length, in case sizeof(struct file *) and sizeof(int)
1092 control
->m_len
= cm
->cmsg_len
= newlen
;
1095 * Transform the file descriptors into struct file pointers.
1096 * If sizeof (struct file *) is bigger than or equal to sizeof int,
1097 * then do it in reverse order so that the int won't get until
1099 * If sizeof (struct file *) is smaller than sizeof int, then
1100 * do it in forward order.
1102 if (sizeof (struct file
*) >= sizeof (int)) {
1103 fdp
= (int *)(cm
+ 1) + oldfds
- 1;
1104 rp
= (struct file
**)CMSG_DATA(cm
) + oldfds
- 1;
1105 for (i
= 0; i
< oldfds
; i
++) {
1106 fp
= fdescp
->fd_files
[*fdp
--].fp
;
1109 spin_lock_wr(&unp_spin
);
1112 spin_unlock_wr(&unp_spin
);
1115 fdp
= (int *)(cm
+ 1);
1116 rp
= (struct file
**)CMSG_DATA(cm
);
1117 for (i
= 0; i
< oldfds
; i
++) {
1118 fp
= fdescp
->fd_files
[*fdp
++].fp
;
1121 spin_lock_wr(&unp_spin
);
1124 spin_unlock_wr(&unp_spin
);
1131 * Garbage collect in-transit file descriptors that get lost due to
1132 * loops (i.e. when a socket is sent to another process over itself,
1133 * and more complex situations).
1135 * NOT MPSAFE - TODO socket flush code and maybe closef. Rest is MPSAFE.
1138 struct unp_gc_info
{
1139 struct file
**extra_ref
;
1140 struct file
*locked_fp
;
1149 struct unp_gc_info info
;
1150 static boolean_t unp_gcing
;
1154 spin_lock_wr(&unp_spin
);
1156 spin_unlock_wr(&unp_spin
);
1160 spin_unlock_wr(&unp_spin
);
1163 * before going through all this, set all FDs to
1164 * be NOT defered and NOT externally accessible
1167 allfiles_scan_exclusive(unp_gc_clearmarks
, NULL
);
1169 allfiles_scan_exclusive(unp_gc_checkmarks
, &info
);
1170 } while (info
.defer
);
1173 * We grab an extra reference to each of the file table entries
1174 * that are not otherwise accessible and then free the rights
1175 * that are stored in messages on them.
1177 * The bug in the orginal code is a little tricky, so I'll describe
1178 * what's wrong with it here.
1180 * It is incorrect to simply unp_discard each entry for f_msgcount
1181 * times -- consider the case of sockets A and B that contain
1182 * references to each other. On a last close of some other socket,
1183 * we trigger a gc since the number of outstanding rights (unp_rights)
1184 * is non-zero. If during the sweep phase the gc code un_discards,
1185 * we end up doing a (full) closef on the descriptor. A closef on A
1186 * results in the following chain. Closef calls soo_close, which
1187 * calls soclose. Soclose calls first (through the switch
1188 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply
1189 * returns because the previous instance had set unp_gcing, and
1190 * we return all the way back to soclose, which marks the socket
1191 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush
1192 * to free up the rights that are queued in messages on the socket A,
1193 * i.e., the reference on B. The sorflush calls via the dom_dispose
1194 * switch unp_dispose, which unp_scans with unp_discard. This second
1195 * instance of unp_discard just calls closef on B.
1197 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1198 * which results in another closef on A. Unfortunately, A is already
1199 * being closed, and the descriptor has already been marked with
1200 * SS_NOFDREF, and soclose panics at this point.
1202 * Here, we first take an extra reference to each inaccessible
1203 * descriptor. Then, we call sorflush ourself, since we know
1204 * it is a Unix domain socket anyhow. After we destroy all the
1205 * rights carried in messages, we do a last closef to get rid
1206 * of our extra reference. This is the last close, and the
1207 * unp_detach etc will shut down the socket.
1209 * 91/09/19, bsy@cs.cmu.edu
1211 info
.extra_ref
= kmalloc(256 * sizeof(struct file
*), M_FILE
, M_WAITOK
);
1212 info
.maxindex
= 256;
1219 allfiles_scan_exclusive(unp_gc_checkrefs
, &info
);
1222 * For each FD on our hit list, do the following two things
1224 for (i
= info
.index
, fpp
= info
.extra_ref
; --i
>= 0; ++fpp
) {
1225 struct file
*tfp
= *fpp
;
1226 if (tfp
->f_type
== DTYPE_SOCKET
&& tfp
->f_data
!= NULL
)
1227 sorflush((struct socket
*)(tfp
->f_data
));
1229 for (i
= info
.index
, fpp
= info
.extra_ref
; --i
>= 0; ++fpp
)
1231 } while (info
.index
== info
.maxindex
);
1232 kfree((caddr_t
)info
.extra_ref
, M_FILE
);
1237 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1240 unp_gc_checkrefs(struct file
*fp
, void *data
)
1242 struct unp_gc_info
*info
= data
;
1244 if (fp
->f_count
== 0)
1246 if (info
->index
== info
->maxindex
)
1250 * If all refs are from msgs, and it's not marked accessible
1251 * then it must be referenced from some unreachable cycle
1252 * of (shut-down) FDs, so include it in our
1253 * list of FDs to remove
1255 if (fp
->f_count
== fp
->f_msgcount
&& !(fp
->f_flag
& FMARK
)) {
1256 info
->extra_ref
[info
->index
++] = fp
;
1263 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1266 unp_gc_clearmarks(struct file
*fp
, void *data __unused
)
1268 fp
->f_flag
&= ~(FMARK
|FDEFER
);
1273 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1276 unp_gc_checkmarks(struct file
*fp
, void *data
)
1278 struct unp_gc_info
*info
= data
;
1282 * If the file is not open, skip it
1284 if (fp
->f_count
== 0)
1287 * If we already marked it as 'defer' in a
1288 * previous pass, then try process it this time
1291 if (fp
->f_flag
& FDEFER
) {
1292 fp
->f_flag
&= ~FDEFER
;
1296 * if it's not defered, then check if it's
1297 * already marked.. if so skip it
1299 if (fp
->f_flag
& FMARK
)
1302 * If all references are from messages
1303 * in transit, then skip it. it's not
1304 * externally accessible.
1306 if (fp
->f_count
== fp
->f_msgcount
)
1309 * If it got this far then it must be
1310 * externally accessible.
1312 fp
->f_flag
|= FMARK
;
1315 * either it was defered, or it is externally
1316 * accessible and not already marked so.
1317 * Now check if it is possibly one of OUR sockets.
1319 if (fp
->f_type
!= DTYPE_SOCKET
||
1320 (so
= (struct socket
*)fp
->f_data
) == NULL
)
1322 if (so
->so_proto
->pr_domain
!= &localdomain
||
1323 !(so
->so_proto
->pr_flags
& PR_RIGHTS
))
1326 XXX note
: exclusive fp
->f_spin lock held
1327 if (so
->so_rcv
.sb_flags
& SB_LOCK
) {
1329 * This is problematical; it's not clear
1330 * we need to wait for the sockbuf to be
1331 * unlocked (on a uniprocessor, at least),
1332 * and it's also not clear what to do
1333 * if sbwait returns an error due to receipt
1334 * of a signal. If sbwait does return
1335 * an error, we'll go into an infinite
1336 * loop. Delete all of this for now.
1338 sbwait(&so
->so_rcv
);
1343 * So, Ok, it's one of our sockets and it IS externally
1344 * accessible (or was defered). Now we look
1345 * to see if we hold any file descriptors in its
1346 * message buffers. Follow those links and mark them
1347 * as accessible too.
1349 info
->locked_fp
= fp
;
1350 /* spin_lock_wr(&so->so_rcv.sb_spin); */
1351 unp_scan(so
->so_rcv
.ssb_mb
, unp_mark
, info
);
1352 /* spin_unlock_wr(&so->so_rcv.sb_spin);*/
1357 unp_dispose(struct mbuf
*m
)
1360 unp_scan(m
, unp_discard
, NULL
);
1364 unp_listen(struct unpcb
*unp
, struct thread
*td
)
1366 struct proc
*p
= td
->td_proc
;
1369 cru2x(p
->p_ucred
, &unp
->unp_peercred
);
1370 unp
->unp_flags
|= UNP_HAVEPCCACHED
;
1375 unp_scan(struct mbuf
*m0
, void (*op
)(struct file
*, void *), void *data
)
1384 for (m
= m0
; m
; m
= m
->m_next
) {
1385 if (m
->m_type
== MT_CONTROL
&&
1386 m
->m_len
>= sizeof(*cm
)) {
1387 cm
= mtod(m
, struct cmsghdr
*);
1388 if (cm
->cmsg_level
!= SOL_SOCKET
||
1389 cm
->cmsg_type
!= SCM_RIGHTS
)
1391 qfds
= (cm
->cmsg_len
-
1392 (CMSG_DATA(cm
) - (u_char
*)cm
))
1393 / sizeof (struct file
*);
1394 rp
= (struct file
**)CMSG_DATA(cm
);
1395 for (i
= 0; i
< qfds
; i
++)
1397 break; /* XXX, but saves time */
1405 unp_mark(struct file
*fp
, void *data
)
1407 struct unp_gc_info
*info
= data
;
1409 if (info
->locked_fp
!= fp
)
1410 spin_lock_wr(&fp
->f_spin
);
1411 if ((fp
->f_flag
& FMARK
) == 0) {
1413 fp
->f_flag
|= (FMARK
|FDEFER
);
1415 if (info
->locked_fp
!= fp
)
1416 spin_unlock_wr(&fp
->f_spin
);
1420 unp_discard(struct file
*fp
, void *data __unused
)
1422 spin_lock_wr(&unp_spin
);
1425 spin_unlock_wr(&unp_spin
);