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 $
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/domain.h>
41 #include <sys/fcntl.h>
42 #include <sys/malloc.h> /* XXX must be before <sys/file.h> */
45 #include <sys/filedesc.h>
47 #include <sys/nlookup.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/resourcevar.h>
53 #include <sys/mount.h>
54 #include <sys/sysctl.h>
56 #include <sys/unpcb.h>
57 #include <sys/vnode.h>
59 #include <sys/file2.h>
60 #include <sys/spinlock2.h>
61 #include <sys/socketvar2.h>
62 #include <sys/msgport2.h>
64 typedef struct unp_defdiscard
{
65 struct unp_defdiscard
*next
;
69 static MALLOC_DEFINE(M_UNPCB
, "unpcb", "unpcb struct");
70 static unp_gen_t unp_gencnt
;
71 static u_int unp_count
;
73 static struct unp_head unp_shead
, unp_dhead
;
75 static struct lwkt_token unp_token
= LWKT_TOKEN_INITIALIZER(unp_token
);
76 static int unp_defdiscard_nest
;
77 static unp_defdiscard_t unp_defdiscard_base
;
80 * Unix communications domain.
84 * rethink name space problems
85 * need a proper out-of-band
88 static struct sockaddr sun_noname
= { sizeof(sun_noname
), AF_LOCAL
};
89 static ino_t unp_ino
= 1; /* prototype for fake inode numbers */
90 static struct spinlock unp_ino_spin
= SPINLOCK_INITIALIZER(&unp_ino_spin
);
92 static int unp_attach (struct socket
*, struct pru_attach_info
*);
93 static void unp_detach (struct unpcb
*);
94 static int unp_bind (struct unpcb
*,struct sockaddr
*, struct thread
*);
95 static int unp_connect (struct socket
*,struct sockaddr
*,
97 static void unp_disconnect (struct unpcb
*);
98 static void unp_shutdown (struct unpcb
*);
99 static void unp_drop (struct unpcb
*, int);
100 static void unp_gc (void);
101 static int unp_gc_clearmarks(struct file
*, void *);
102 static int unp_gc_checkmarks(struct file
*, void *);
103 static int unp_gc_checkrefs(struct file
*, void *);
104 static int unp_revoke_gc_check(struct file
*, void *);
105 static void unp_scan (struct mbuf
*, void (*)(struct file
*, void *),
107 static void unp_mark (struct file
*, void *data
);
108 static void unp_discard (struct file
*, void *);
109 static int unp_internalize (struct mbuf
*, struct thread
*);
110 static int unp_listen (struct unpcb
*, struct thread
*);
111 static void unp_fp_externalize(struct lwp
*lp
, struct file
*fp
, int fd
);
114 * SMP Considerations:
116 * Since unp_token will be automaticly released upon execution of
117 * blocking code, we need to reference unp_conn before any possible
118 * blocking code to prevent it from being ripped behind our back.
120 * Any adjustment to unp->unp_conn requires both the global unp_token
121 * AND the per-unp token (lwkt_token_pool_lookup(unp)) to be held.
123 * Any access to so_pcb to obtain unp requires the pool token for
127 /* NOTE: unp_token MUST be held */
129 unp_reference(struct unpcb
*unp
)
131 atomic_add_int(&unp
->unp_refcnt
, 1);
134 /* NOTE: unp_token MUST be held */
136 unp_free(struct unpcb
*unp
)
138 KKASSERT(unp
->unp_refcnt
> 0);
139 if (atomic_fetchadd_int(&unp
->unp_refcnt
, -1) == 1)
144 * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
145 * will sofree() it when we return.
148 uipc_abort(netmsg_t msg
)
153 lwkt_gettoken(&unp_token
);
154 unp
= msg
->base
.nm_so
->so_pcb
;
156 unp_drop(unp
, ECONNABORTED
);
162 lwkt_reltoken(&unp_token
);
164 lwkt_replymsg(&msg
->lmsg
, error
);
168 uipc_accept(netmsg_t msg
)
173 lwkt_gettoken(&unp_token
);
174 unp
= msg
->base
.nm_so
->so_pcb
;
178 struct unpcb
*unp2
= unp
->unp_conn
;
181 * Pass back name of connected socket,
182 * if it was bound and we are still connected
183 * (our peer may have closed already!).
185 if (unp2
&& unp2
->unp_addr
) {
187 *msg
->accept
.nm_nam
= dup_sockaddr(
188 (struct sockaddr
*)unp2
->unp_addr
);
191 *msg
->accept
.nm_nam
= dup_sockaddr(&sun_noname
);
195 lwkt_reltoken(&unp_token
);
196 lwkt_replymsg(&msg
->lmsg
, error
);
200 uipc_attach(netmsg_t msg
)
205 lwkt_gettoken(&unp_token
);
206 unp
= msg
->base
.nm_so
->so_pcb
;
210 error
= unp_attach(msg
->base
.nm_so
, msg
->attach
.nm_ai
);
211 lwkt_reltoken(&unp_token
);
212 lwkt_replymsg(&msg
->lmsg
, error
);
216 uipc_bind(netmsg_t msg
)
221 lwkt_gettoken(&unp_token
);
222 unp
= msg
->base
.nm_so
->so_pcb
;
224 error
= unp_bind(unp
, msg
->bind
.nm_nam
, msg
->bind
.nm_td
);
227 lwkt_reltoken(&unp_token
);
228 lwkt_replymsg(&msg
->lmsg
, error
);
232 uipc_connect(netmsg_t msg
)
237 unp
= msg
->base
.nm_so
->so_pcb
;
239 error
= unp_connect(msg
->base
.nm_so
,
245 lwkt_replymsg(&msg
->lmsg
, error
);
249 uipc_connect2(netmsg_t msg
)
254 unp
= msg
->connect2
.nm_so1
->so_pcb
;
256 error
= unp_connect2(msg
->connect2
.nm_so1
,
257 msg
->connect2
.nm_so2
);
261 lwkt_replymsg(&msg
->lmsg
, error
);
264 /* control is EOPNOTSUPP */
267 uipc_detach(netmsg_t msg
)
272 lwkt_gettoken(&unp_token
);
273 unp
= msg
->base
.nm_so
->so_pcb
;
280 lwkt_reltoken(&unp_token
);
281 lwkt_replymsg(&msg
->lmsg
, error
);
285 uipc_disconnect(netmsg_t msg
)
290 lwkt_gettoken(&unp_token
);
291 unp
= msg
->base
.nm_so
->so_pcb
;
298 lwkt_reltoken(&unp_token
);
299 lwkt_replymsg(&msg
->lmsg
, error
);
303 uipc_listen(netmsg_t msg
)
308 lwkt_gettoken(&unp_token
);
309 unp
= msg
->base
.nm_so
->so_pcb
;
310 if (unp
== NULL
|| unp
->unp_vnode
== NULL
)
313 error
= unp_listen(unp
, msg
->listen
.nm_td
);
314 lwkt_reltoken(&unp_token
);
315 lwkt_replymsg(&msg
->lmsg
, error
);
319 uipc_peeraddr(netmsg_t msg
)
324 lwkt_gettoken(&unp_token
);
325 unp
= msg
->base
.nm_so
->so_pcb
;
328 } else if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
) {
329 struct unpcb
*unp2
= unp
->unp_conn
;
332 *msg
->peeraddr
.nm_nam
= dup_sockaddr(
333 (struct sockaddr
*)unp2
->unp_addr
);
338 * XXX: It seems that this test always fails even when
339 * connection is established. So, this else clause is
340 * added as workaround to return PF_LOCAL sockaddr.
342 *msg
->peeraddr
.nm_nam
= dup_sockaddr(&sun_noname
);
345 lwkt_reltoken(&unp_token
);
346 lwkt_replymsg(&msg
->lmsg
, error
);
350 uipc_rcvd(netmsg_t msg
)
352 struct unpcb
*unp
, *unp2
;
358 * so_pcb is only modified with both the global and the unp
359 * pool token held. The unp pointer is invalid until we verify
360 * that it is good by re-checking so_pcb AFTER obtaining the token.
362 so
= msg
->base
.nm_so
;
363 while ((unp
= so
->so_pcb
) != NULL
) {
364 lwkt_getpooltoken(unp
);
365 if (unp
== so
->so_pcb
)
367 lwkt_relpooltoken(unp
);
373 /* pool token held */
375 switch (so
->so_type
) {
377 panic("uipc_rcvd DGRAM?");
381 if (unp
->unp_conn
== NULL
)
383 unp2
= unp
->unp_conn
; /* protected by pool token */
386 * Because we are transfering mbufs directly to the
387 * peer socket we have to use SSB_STOP on the sender
388 * to prevent it from building up infinite mbufs.
390 * As in several places in this module w ehave to ref unp2
391 * to ensure that it does not get ripped out from under us
392 * if we block on the so2 token or in sowwakeup().
394 so2
= unp2
->unp_socket
;
396 lwkt_gettoken(&so2
->so_rcv
.ssb_token
);
397 if (so
->so_rcv
.ssb_cc
< so2
->so_snd
.ssb_hiwat
&&
398 so
->so_rcv
.ssb_mbcnt
< so2
->so_snd
.ssb_mbmax
400 atomic_clear_int(&so2
->so_snd
.ssb_flags
, SSB_STOP
);
404 lwkt_reltoken(&so2
->so_rcv
.ssb_token
);
408 panic("uipc_rcvd unknown socktype");
412 lwkt_relpooltoken(unp
);
414 lwkt_replymsg(&msg
->lmsg
, error
);
417 /* pru_rcvoob is EOPNOTSUPP */
420 uipc_send(netmsg_t msg
)
422 struct unpcb
*unp
, *unp2
;
425 struct mbuf
*control
;
429 so
= msg
->base
.nm_so
;
430 control
= msg
->send
.nm_control
;
434 * so_pcb is only modified with both the global and the unp
435 * pool token held. The unp pointer is invalid until we verify
436 * that it is good by re-checking so_pcb AFTER obtaining the token.
438 so
= msg
->base
.nm_so
;
439 while ((unp
= so
->so_pcb
) != NULL
) {
440 lwkt_getpooltoken(unp
);
441 if (unp
== so
->so_pcb
)
443 lwkt_relpooltoken(unp
);
449 /* pool token held */
451 if (msg
->send
.nm_flags
& PRUS_OOB
) {
456 wakeup_start_delayed();
458 if (control
&& (error
= unp_internalize(control
, msg
->send
.nm_td
)))
461 switch (so
->so_type
) {
464 struct sockaddr
*from
;
466 if (msg
->send
.nm_addr
) {
471 error
= unp_connect(so
,
477 if (unp
->unp_conn
== NULL
) {
482 unp2
= unp
->unp_conn
;
483 so2
= unp2
->unp_socket
;
485 from
= (struct sockaddr
*)unp
->unp_addr
;
491 lwkt_gettoken(&so2
->so_rcv
.ssb_token
);
492 if (ssb_appendaddr(&so2
->so_rcv
, from
, m
, control
)) {
499 if (msg
->send
.nm_addr
)
501 lwkt_reltoken(&so2
->so_rcv
.ssb_token
);
509 /* Connect if not connected yet. */
511 * Note: A better implementation would complain
512 * if not equal to the peer's address.
514 if (!(so
->so_state
& SS_ISCONNECTED
)) {
515 if (msg
->send
.nm_addr
) {
516 error
= unp_connect(so
,
527 if (so
->so_state
& SS_CANTSENDMORE
) {
531 if (unp
->unp_conn
== NULL
)
532 panic("uipc_send connected but no connection?");
533 unp2
= unp
->unp_conn
;
534 so2
= unp2
->unp_socket
;
539 * Send to paired receive port, and then reduce
540 * send buffer hiwater marks to maintain backpressure.
543 lwkt_gettoken(&so2
->so_rcv
.ssb_token
);
545 if (ssb_appendcontrol(&so2
->so_rcv
, m
, control
)) {
549 } else if (so
->so_type
== SOCK_SEQPACKET
) {
550 sbappendrecord(&so2
->so_rcv
.sb
, m
);
553 sbappend(&so2
->so_rcv
.sb
, m
);
558 * Because we are transfering mbufs directly to the
559 * peer socket we have to use SSB_STOP on the sender
560 * to prevent it from building up infinite mbufs.
562 if (so2
->so_rcv
.ssb_cc
>= so
->so_snd
.ssb_hiwat
||
563 so2
->so_rcv
.ssb_mbcnt
>= so
->so_snd
.ssb_mbmax
565 atomic_set_int(&so
->so_snd
.ssb_flags
, SSB_STOP
);
567 lwkt_reltoken(&so2
->so_rcv
.ssb_token
);
574 panic("uipc_send unknown socktype");
578 * SEND_EOF is equivalent to a SEND followed by a SHUTDOWN.
580 if (msg
->send
.nm_flags
& PRUS_EOF
) {
585 if (control
&& error
!= 0)
586 unp_dispose(control
);
588 lwkt_relpooltoken(unp
);
589 wakeup_end_delayed();
596 lwkt_replymsg(&msg
->lmsg
, error
);
603 uipc_sense(netmsg_t msg
)
610 so
= msg
->base
.nm_so
;
611 sb
= msg
->sense
.nm_stat
;
614 * so_pcb is only modified with both the global and the unp
615 * pool token held. The unp pointer is invalid until we verify
616 * that it is good by re-checking so_pcb AFTER obtaining the token.
618 while ((unp
= so
->so_pcb
) != NULL
) {
619 lwkt_getpooltoken(unp
);
620 if (unp
== so
->so_pcb
)
622 lwkt_relpooltoken(unp
);
628 /* pool token held */
630 sb
->st_blksize
= so
->so_snd
.ssb_hiwat
;
632 if (unp
->unp_ino
== 0) { /* make up a non-zero inode number */
633 spin_lock(&unp_ino_spin
);
634 unp
->unp_ino
= unp_ino
++;
635 spin_unlock(&unp_ino_spin
);
637 sb
->st_ino
= unp
->unp_ino
;
639 lwkt_relpooltoken(unp
);
641 lwkt_replymsg(&msg
->lmsg
, error
);
645 uipc_shutdown(netmsg_t msg
)
652 * so_pcb is only modified with both the global and the unp
653 * pool token held. The unp pointer is invalid until we verify
654 * that it is good by re-checking so_pcb AFTER obtaining the token.
656 so
= msg
->base
.nm_so
;
657 while ((unp
= so
->so_pcb
) != NULL
) {
658 lwkt_getpooltoken(unp
);
659 if (unp
== so
->so_pcb
)
661 lwkt_relpooltoken(unp
);
664 /* pool token held */
667 lwkt_relpooltoken(unp
);
672 lwkt_replymsg(&msg
->lmsg
, error
);
676 uipc_sockaddr(netmsg_t msg
)
683 * so_pcb is only modified with both the global and the unp
684 * pool token held. The unp pointer is invalid until we verify
685 * that it is good by re-checking so_pcb AFTER obtaining the token.
687 so
= msg
->base
.nm_so
;
688 while ((unp
= so
->so_pcb
) != NULL
) {
689 lwkt_getpooltoken(unp
);
690 if (unp
== so
->so_pcb
)
692 lwkt_relpooltoken(unp
);
695 /* pool token held */
697 *msg
->sockaddr
.nm_nam
=
698 dup_sockaddr((struct sockaddr
*)unp
->unp_addr
);
700 lwkt_relpooltoken(unp
);
705 lwkt_replymsg(&msg
->lmsg
, error
);
708 struct pr_usrreqs uipc_usrreqs
= {
709 .pru_abort
= uipc_abort
,
710 .pru_accept
= uipc_accept
,
711 .pru_attach
= uipc_attach
,
712 .pru_bind
= uipc_bind
,
713 .pru_connect
= uipc_connect
,
714 .pru_connect2
= uipc_connect2
,
715 .pru_control
= pr_generic_notsupp
,
716 .pru_detach
= uipc_detach
,
717 .pru_disconnect
= uipc_disconnect
,
718 .pru_listen
= uipc_listen
,
719 .pru_peeraddr
= uipc_peeraddr
,
720 .pru_rcvd
= uipc_rcvd
,
721 .pru_rcvoob
= pr_generic_notsupp
,
722 .pru_send
= uipc_send
,
723 .pru_sense
= uipc_sense
,
724 .pru_shutdown
= uipc_shutdown
,
725 .pru_sockaddr
= uipc_sockaddr
,
726 .pru_sosend
= sosend
,
727 .pru_soreceive
= soreceive
731 uipc_ctloutput(netmsg_t msg
)
734 struct sockopt
*sopt
;
738 lwkt_gettoken(&unp_token
);
739 so
= msg
->base
.nm_so
;
740 sopt
= msg
->ctloutput
.nm_sopt
;
743 switch (sopt
->sopt_dir
) {
745 switch (sopt
->sopt_name
) {
747 if (unp
->unp_flags
& UNP_HAVEPC
)
748 soopt_from_kbuf(sopt
, &unp
->unp_peercred
,
749 sizeof(unp
->unp_peercred
));
751 if (so
->so_type
== SOCK_STREAM
)
753 else if (so
->so_type
== SOCK_SEQPACKET
)
769 lwkt_reltoken(&unp_token
);
770 lwkt_replymsg(&msg
->lmsg
, error
);
774 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
775 * for stream sockets, although the total for sender and receiver is
776 * actually only PIPSIZ.
778 * Datagram sockets really use the sendspace as the maximum datagram size,
779 * and don't really want to reserve the sendspace. Their recvspace should
780 * be large enough for at least one max-size datagram plus address.
782 * We want the local send/recv space to be significant larger then lo0's
788 static u_long unpst_sendspace
= PIPSIZ
;
789 static u_long unpst_recvspace
= PIPSIZ
;
790 static u_long unpdg_sendspace
= 2*1024; /* really max datagram size */
791 static u_long unpdg_recvspace
= 4*1024;
793 static int unp_rights
; /* file descriptors in flight */
794 static struct spinlock unp_spin
= SPINLOCK_INITIALIZER(&unp_spin
);
796 SYSCTL_DECL(_net_local_seqpacket
);
797 SYSCTL_DECL(_net_local_stream
);
798 SYSCTL_INT(_net_local_stream
, OID_AUTO
, sendspace
, CTLFLAG_RW
,
799 &unpst_sendspace
, 0, "Size of stream socket send buffer");
800 SYSCTL_INT(_net_local_stream
, OID_AUTO
, recvspace
, CTLFLAG_RW
,
801 &unpst_recvspace
, 0, "Size of stream socket receive buffer");
803 SYSCTL_DECL(_net_local_dgram
);
804 SYSCTL_INT(_net_local_dgram
, OID_AUTO
, maxdgram
, CTLFLAG_RW
,
805 &unpdg_sendspace
, 0, "Max datagram socket size");
806 SYSCTL_INT(_net_local_dgram
, OID_AUTO
, recvspace
, CTLFLAG_RW
,
807 &unpdg_recvspace
, 0, "Size of datagram socket receive buffer");
809 SYSCTL_DECL(_net_local
);
810 SYSCTL_INT(_net_local
, OID_AUTO
, inflight
, CTLFLAG_RD
, &unp_rights
, 0,
811 "File descriptors in flight");
814 unp_attach(struct socket
*so
, struct pru_attach_info
*ai
)
819 lwkt_gettoken(&unp_token
);
821 if (so
->so_snd
.ssb_hiwat
== 0 || so
->so_rcv
.ssb_hiwat
== 0) {
822 switch (so
->so_type
) {
826 error
= soreserve(so
, unpst_sendspace
, unpst_recvspace
,
831 error
= soreserve(so
, unpdg_sendspace
, unpdg_recvspace
,
841 unp
= kmalloc(sizeof(*unp
), M_UNPCB
, M_WAITOK
| M_ZERO
| M_NULLOK
);
847 unp
->unp_gencnt
= ++unp_gencnt
;
849 LIST_INIT(&unp
->unp_refs
);
850 unp
->unp_socket
= so
;
851 unp
->unp_rvnode
= ai
->fd_rdir
; /* jail cruft XXX JH */
852 LIST_INSERT_HEAD(so
->so_type
== SOCK_DGRAM
? &unp_dhead
853 : &unp_shead
, unp
, unp_link
);
854 so
->so_pcb
= (caddr_t
)unp
;
858 lwkt_reltoken(&unp_token
);
863 unp_detach(struct unpcb
*unp
)
867 lwkt_gettoken(&unp_token
);
868 lwkt_getpooltoken(unp
);
870 LIST_REMOVE(unp
, unp_link
); /* both tokens required */
871 unp
->unp_gencnt
= ++unp_gencnt
;
873 if (unp
->unp_vnode
) {
874 unp
->unp_vnode
->v_socket
= NULL
;
875 vrele(unp
->unp_vnode
);
876 unp
->unp_vnode
= NULL
;
880 while (!LIST_EMPTY(&unp
->unp_refs
))
881 unp_drop(LIST_FIRST(&unp
->unp_refs
), ECONNRESET
);
882 soisdisconnected(unp
->unp_socket
);
883 so
= unp
->unp_socket
;
884 soreference(so
); /* for delayed sorflush */
885 KKASSERT(so
->so_pcb
== unp
);
886 so
->so_pcb
= NULL
; /* both tokens required */
887 unp
->unp_socket
= NULL
;
888 sofree(so
); /* remove pcb ref */
892 * Normally the receive buffer is flushed later,
893 * in sofree, but if our receive buffer holds references
894 * to descriptors that are now garbage, we will dispose
895 * of those descriptor references after the garbage collector
896 * gets them (resulting in a "panic: closef: count < 0").
902 lwkt_relpooltoken(unp
);
903 lwkt_reltoken(&unp_token
);
906 kfree(unp
->unp_addr
, M_SONAME
);
911 unp_bind(struct unpcb
*unp
, struct sockaddr
*nam
, struct thread
*td
)
913 struct proc
*p
= td
->td_proc
;
914 struct sockaddr_un
*soun
= (struct sockaddr_un
*)nam
;
918 struct nlookupdata nd
;
919 char buf
[SOCK_MAXADDRLEN
];
921 lwkt_gettoken(&unp_token
);
922 if (unp
->unp_vnode
!= NULL
) {
926 namelen
= soun
->sun_len
- offsetof(struct sockaddr_un
, sun_path
);
931 strncpy(buf
, soun
->sun_path
, namelen
);
932 buf
[namelen
] = 0; /* null-terminate the string */
933 error
= nlookup_init(&nd
, buf
, UIO_SYSSPACE
,
934 NLC_LOCKVP
| NLC_CREATE
| NLC_REFDVP
);
936 error
= nlookup(&nd
);
937 if (error
== 0 && nd
.nl_nch
.ncp
->nc_vp
!= NULL
)
943 vattr
.va_type
= VSOCK
;
944 vattr
.va_mode
= (ACCESSPERMS
& ~p
->p_fd
->fd_cmask
);
945 error
= VOP_NCREATE(&nd
.nl_nch
, nd
.nl_dvp
, &vp
, nd
.nl_cred
, &vattr
);
947 if (unp
->unp_vnode
== NULL
) {
948 vp
->v_socket
= unp
->unp_socket
;
950 unp
->unp_addr
= (struct sockaddr_un
*)dup_sockaddr(nam
);
953 vput(vp
); /* late race */
960 lwkt_reltoken(&unp_token
);
965 unp_connect(struct socket
*so
, struct sockaddr
*nam
, struct thread
*td
)
967 struct proc
*p
= td
->td_proc
;
968 struct sockaddr_un
*soun
= (struct sockaddr_un
*)nam
;
970 struct socket
*so2
, *so3
;
971 struct unpcb
*unp
, *unp2
, *unp3
;
973 struct nlookupdata nd
;
974 char buf
[SOCK_MAXADDRLEN
];
976 lwkt_gettoken(&unp_token
);
978 len
= nam
->sa_len
- offsetof(struct sockaddr_un
, sun_path
);
983 strncpy(buf
, soun
->sun_path
, len
);
987 error
= nlookup_init(&nd
, buf
, UIO_SYSSPACE
, NLC_FOLLOW
);
989 error
= nlookup(&nd
);
991 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
996 if (vp
->v_type
!= VSOCK
) {
1000 error
= VOP_EACCESS(vp
, VWRITE
, p
->p_ucred
);
1005 error
= ECONNREFUSED
;
1008 if (so
->so_type
!= so2
->so_type
) {
1012 if (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) {
1013 if (!(so2
->so_options
& SO_ACCEPTCONN
) ||
1014 (so3
= sonewconn(so2
, 0)) == NULL
) {
1015 error
= ECONNREFUSED
;
1019 if (unp
->unp_conn
) { /* race, already connected! */
1027 unp3
->unp_addr
= (struct sockaddr_un
*)
1028 dup_sockaddr((struct sockaddr
*)unp2
->unp_addr
);
1031 * unp_peercred management:
1033 * The connecter's (client's) credentials are copied
1034 * from its process structure at the time of connect()
1037 cru2x(p
->p_ucred
, &unp3
->unp_peercred
);
1038 unp3
->unp_flags
|= UNP_HAVEPC
;
1040 * The receiver's (server's) credentials are copied
1041 * from the unp_peercred member of socket on which the
1042 * former called listen(); unp_listen() cached that
1043 * process's credentials at that time so we can use
1046 KASSERT(unp2
->unp_flags
& UNP_HAVEPCCACHED
,
1047 ("unp_connect: listener without cached peercred"));
1048 memcpy(&unp
->unp_peercred
, &unp2
->unp_peercred
,
1049 sizeof(unp
->unp_peercred
));
1050 unp
->unp_flags
|= UNP_HAVEPC
;
1054 error
= unp_connect2(so
, so2
);
1058 lwkt_reltoken(&unp_token
);
1063 * Connect two unix domain sockets together.
1065 * NOTE: Semantics for any change to unp_conn requires that the per-unp
1066 * pool token also be held.
1069 unp_connect2(struct socket
*so
, struct socket
*so2
)
1074 lwkt_gettoken(&unp_token
);
1076 if (so2
->so_type
!= so
->so_type
) {
1077 lwkt_reltoken(&unp_token
);
1078 return (EPROTOTYPE
);
1081 lwkt_getpooltoken(unp
);
1082 lwkt_getpooltoken(unp2
);
1084 unp
->unp_conn
= unp2
;
1086 switch (so
->so_type
) {
1088 LIST_INSERT_HEAD(&unp2
->unp_refs
, unp
, unp_reflink
);
1093 case SOCK_SEQPACKET
:
1094 unp2
->unp_conn
= unp
;
1100 panic("unp_connect2");
1102 lwkt_relpooltoken(unp2
);
1103 lwkt_relpooltoken(unp
);
1104 lwkt_reltoken(&unp_token
);
1109 * Disconnect a unix domain socket pair.
1111 * NOTE: Semantics for any change to unp_conn requires that the per-unp
1112 * pool token also be held.
1115 unp_disconnect(struct unpcb
*unp
)
1119 lwkt_gettoken(&unp_token
);
1120 lwkt_getpooltoken(unp
);
1122 while ((unp2
= unp
->unp_conn
) != NULL
) {
1123 lwkt_getpooltoken(unp2
);
1124 if (unp2
== unp
->unp_conn
)
1126 lwkt_relpooltoken(unp2
);
1131 unp
->unp_conn
= NULL
;
1133 switch (unp
->unp_socket
->so_type
) {
1135 LIST_REMOVE(unp
, unp_reflink
);
1136 soclrstate(unp
->unp_socket
, SS_ISCONNECTED
);
1140 case SOCK_SEQPACKET
:
1141 unp_reference(unp2
);
1142 unp2
->unp_conn
= NULL
;
1144 soisdisconnected(unp
->unp_socket
);
1145 soisdisconnected(unp2
->unp_socket
);
1150 lwkt_relpooltoken(unp2
);
1152 lwkt_relpooltoken(unp
);
1153 lwkt_reltoken(&unp_token
);
1158 unp_abort(struct unpcb
*unp
)
1160 lwkt_gettoken(&unp_token
);
1162 lwkt_reltoken(&unp_token
);
1167 prison_unpcb(struct thread
*td
, struct unpcb
*unp
)
1173 if ((p
= td
->td_proc
) == NULL
)
1175 if (!p
->p_ucred
->cr_prison
)
1177 if (p
->p_fd
->fd_rdir
== unp
->unp_rvnode
)
1183 unp_pcblist(SYSCTL_HANDLER_ARGS
)
1186 struct unpcb
*unp
, **unp_list
;
1188 struct unp_head
*head
;
1190 head
= ((intptr_t)arg1
== SOCK_DGRAM
? &unp_dhead
: &unp_shead
);
1192 KKASSERT(curproc
!= NULL
);
1195 * The process of preparing the PCB list is too time-consuming and
1196 * resource-intensive to repeat twice on every request.
1198 if (req
->oldptr
== NULL
) {
1200 req
->oldidx
= (n
+ n
/8) * sizeof(struct xunpcb
);
1204 if (req
->newptr
!= NULL
)
1207 lwkt_gettoken(&unp_token
);
1210 * OK, now we're committed to doing something.
1212 gencnt
= unp_gencnt
;
1215 unp_list
= kmalloc(n
* sizeof *unp_list
, M_TEMP
, M_WAITOK
);
1217 for (unp
= LIST_FIRST(head
), i
= 0; unp
&& i
< n
;
1218 unp
= LIST_NEXT(unp
, unp_link
)) {
1219 if (unp
->unp_gencnt
<= gencnt
&& !prison_unpcb(req
->td
, unp
))
1220 unp_list
[i
++] = unp
;
1222 n
= i
; /* in case we lost some during malloc */
1225 for (i
= 0; i
< n
; i
++) {
1227 if (unp
->unp_gencnt
<= gencnt
) {
1229 xu
.xu_len
= sizeof xu
;
1232 * XXX - need more locking here to protect against
1233 * connect/disconnect races for SMP.
1236 bcopy(unp
->unp_addr
, &xu
.xu_addr
,
1237 unp
->unp_addr
->sun_len
);
1238 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
)
1239 bcopy(unp
->unp_conn
->unp_addr
,
1241 unp
->unp_conn
->unp_addr
->sun_len
);
1242 bcopy(unp
, &xu
.xu_unp
, sizeof *unp
);
1243 sotoxsocket(unp
->unp_socket
, &xu
.xu_socket
);
1244 error
= SYSCTL_OUT(req
, &xu
, sizeof xu
);
1247 lwkt_reltoken(&unp_token
);
1248 kfree(unp_list
, M_TEMP
);
1253 SYSCTL_PROC(_net_local_dgram
, OID_AUTO
, pcblist
, CTLFLAG_RD
,
1254 (caddr_t
)(long)SOCK_DGRAM
, 0, unp_pcblist
, "S,xunpcb",
1255 "List of active local datagram sockets");
1256 SYSCTL_PROC(_net_local_stream
, OID_AUTO
, pcblist
, CTLFLAG_RD
,
1257 (caddr_t
)(long)SOCK_STREAM
, 0, unp_pcblist
, "S,xunpcb",
1258 "List of active local stream sockets");
1259 SYSCTL_PROC(_net_local_seqpacket
, OID_AUTO
, pcblist
, CTLFLAG_RD
,
1260 (caddr_t
)(long)SOCK_SEQPACKET
, 0, unp_pcblist
, "S,xunpcb",
1261 "List of active local seqpacket stream sockets");
1264 unp_shutdown(struct unpcb
*unp
)
1268 if ((unp
->unp_socket
->so_type
== SOCK_STREAM
||
1269 unp
->unp_socket
->so_type
== SOCK_SEQPACKET
) &&
1270 unp
->unp_conn
!= NULL
&& (so
= unp
->unp_conn
->unp_socket
)) {
1276 unp_drop(struct unpcb
*unp
, int err
)
1278 struct socket
*so
= unp
->unp_socket
;
1281 unp_disconnect(unp
);
1288 lwkt_gettoken(&unp_token
);
1289 lwkt_reltoken(&unp_token
);
1294 unp_externalize(struct mbuf
*rights
)
1296 struct thread
*td
= curthread
;
1297 struct proc
*p
= td
->td_proc
; /* XXX */
1298 struct lwp
*lp
= td
->td_lwp
;
1299 struct cmsghdr
*cm
= mtod(rights
, struct cmsghdr
*);
1304 int newfds
= (cm
->cmsg_len
- (CMSG_DATA(cm
) - (u_char
*)cm
))
1305 / sizeof (struct file
*);
1308 lwkt_gettoken(&unp_token
);
1311 * if the new FD's will not fit, then we free them all
1313 if (!fdavail(p
, newfds
)) {
1314 rp
= (struct file
**)CMSG_DATA(cm
);
1315 for (i
= 0; i
< newfds
; i
++) {
1318 * zero the pointer before calling unp_discard,
1319 * since it may end up in unp_gc()..
1322 unp_discard(fp
, NULL
);
1324 lwkt_reltoken(&unp_token
);
1329 * now change each pointer to an fd in the global table to
1330 * an integer that is the index to the local fd table entry
1331 * that we set up to point to the global one we are transferring.
1332 * If sizeof (struct file *) is bigger than or equal to sizeof int,
1333 * then do it in forward order. In that case, an integer will
1334 * always come in the same place or before its corresponding
1335 * struct file pointer.
1336 * If sizeof (struct file *) is smaller than sizeof int, then
1337 * do it in reverse order.
1339 if (sizeof (struct file
*) >= sizeof (int)) {
1340 fdp
= (int *)CMSG_DATA(cm
);
1341 rp
= (struct file
**)CMSG_DATA(cm
);
1342 for (i
= 0; i
< newfds
; i
++) {
1343 if (fdalloc(p
, 0, &f
))
1344 panic("unp_externalize");
1346 unp_fp_externalize(lp
, fp
, f
);
1350 fdp
= (int *)CMSG_DATA(cm
) + newfds
- 1;
1351 rp
= (struct file
**)CMSG_DATA(cm
) + newfds
- 1;
1352 for (i
= 0; i
< newfds
; i
++) {
1353 if (fdalloc(p
, 0, &f
))
1354 panic("unp_externalize");
1356 unp_fp_externalize(lp
, fp
, f
);
1362 * Adjust length, in case sizeof(struct file *) and sizeof(int)
1365 cm
->cmsg_len
= CMSG_LEN(newfds
* sizeof(int));
1366 rights
->m_len
= cm
->cmsg_len
;
1368 lwkt_reltoken(&unp_token
);
1373 unp_fp_externalize(struct lwp
*lp
, struct file
*fp
, int fd
)
1378 lwkt_gettoken(&unp_token
);
1382 if (fp
->f_flag
& FREVOKED
) {
1383 kprintf("Warning: revoked fp exiting unix socket\n");
1385 error
= falloc(lp
, &fx
, NULL
);
1387 fsetfd(lp
->lwp_proc
->p_fd
, fx
, fd
);
1389 fsetfd(lp
->lwp_proc
->p_fd
, NULL
, fd
);
1392 fsetfd(lp
->lwp_proc
->p_fd
, fp
, fd
);
1395 spin_lock(&unp_spin
);
1398 spin_unlock(&unp_spin
);
1401 lwkt_reltoken(&unp_token
);
1408 LIST_INIT(&unp_dhead
);
1409 LIST_INIT(&unp_shead
);
1410 spin_init(&unp_spin
);
1414 unp_internalize(struct mbuf
*control
, struct thread
*td
)
1416 struct proc
*p
= td
->td_proc
;
1417 struct filedesc
*fdescp
;
1418 struct cmsghdr
*cm
= mtod(control
, struct cmsghdr
*);
1422 struct cmsgcred
*cmcred
;
1428 lwkt_gettoken(&unp_token
);
1431 if ((cm
->cmsg_type
!= SCM_RIGHTS
&& cm
->cmsg_type
!= SCM_CREDS
) ||
1432 cm
->cmsg_level
!= SOL_SOCKET
||
1433 CMSG_ALIGN(cm
->cmsg_len
) != control
->m_len
) {
1439 * Fill in credential information.
1441 if (cm
->cmsg_type
== SCM_CREDS
) {
1442 cmcred
= (struct cmsgcred
*)CMSG_DATA(cm
);
1443 cmcred
->cmcred_pid
= p
->p_pid
;
1444 cmcred
->cmcred_uid
= p
->p_ucred
->cr_ruid
;
1445 cmcred
->cmcred_gid
= p
->p_ucred
->cr_rgid
;
1446 cmcred
->cmcred_euid
= p
->p_ucred
->cr_uid
;
1447 cmcred
->cmcred_ngroups
= MIN(p
->p_ucred
->cr_ngroups
,
1449 for (i
= 0; i
< cmcred
->cmcred_ngroups
; i
++)
1450 cmcred
->cmcred_groups
[i
] = p
->p_ucred
->cr_groups
[i
];
1456 * cmsghdr may not be aligned, do not allow calculation(s) to
1459 if (cm
->cmsg_len
< CMSG_LEN(0)) {
1464 oldfds
= (cm
->cmsg_len
- CMSG_LEN(0)) / sizeof (int);
1467 * check that all the FDs passed in refer to legal OPEN files
1468 * If not, reject the entire operation.
1470 fdp
= (int *)CMSG_DATA(cm
);
1471 for (i
= 0; i
< oldfds
; i
++) {
1473 if ((unsigned)fd
>= fdescp
->fd_nfiles
||
1474 fdescp
->fd_files
[fd
].fp
== NULL
) {
1478 if (fdescp
->fd_files
[fd
].fp
->f_type
== DTYPE_KQUEUE
) {
1484 * Now replace the integer FDs with pointers to
1485 * the associated global file table entry..
1486 * Allocate a bigger buffer as necessary. But if an cluster is not
1487 * enough, return E2BIG.
1489 newlen
= CMSG_LEN(oldfds
* sizeof(struct file
*));
1490 if (newlen
> MCLBYTES
) {
1494 if (newlen
- control
->m_len
> M_TRAILINGSPACE(control
)) {
1495 if (control
->m_flags
& M_EXT
) {
1499 MCLGET(control
, MB_WAIT
);
1500 if (!(control
->m_flags
& M_EXT
)) {
1505 /* copy the data to the cluster */
1506 memcpy(mtod(control
, char *), cm
, cm
->cmsg_len
);
1507 cm
= mtod(control
, struct cmsghdr
*);
1511 * Adjust length, in case sizeof(struct file *) and sizeof(int)
1514 cm
->cmsg_len
= newlen
;
1515 control
->m_len
= CMSG_ALIGN(newlen
);
1518 * Transform the file descriptors into struct file pointers.
1519 * If sizeof (struct file *) is bigger than or equal to sizeof int,
1520 * then do it in reverse order so that the int won't get until
1522 * If sizeof (struct file *) is smaller than sizeof int, then
1523 * do it in forward order.
1525 if (sizeof (struct file
*) >= sizeof (int)) {
1526 fdp
= (int *)CMSG_DATA(cm
) + oldfds
- 1;
1527 rp
= (struct file
**)CMSG_DATA(cm
) + oldfds
- 1;
1528 for (i
= 0; i
< oldfds
; i
++) {
1529 fp
= fdescp
->fd_files
[*fdp
--].fp
;
1532 spin_lock(&unp_spin
);
1535 spin_unlock(&unp_spin
);
1538 fdp
= (int *)CMSG_DATA(cm
);
1539 rp
= (struct file
**)CMSG_DATA(cm
);
1540 for (i
= 0; i
< oldfds
; i
++) {
1541 fp
= fdescp
->fd_files
[*fdp
++].fp
;
1544 spin_lock(&unp_spin
);
1547 spin_unlock(&unp_spin
);
1552 lwkt_reltoken(&unp_token
);
1557 * Garbage collect in-transit file descriptors that get lost due to
1558 * loops (i.e. when a socket is sent to another process over itself,
1559 * and more complex situations).
1561 * NOT MPSAFE - TODO socket flush code and maybe closef. Rest is MPSAFE.
1564 struct unp_gc_info
{
1565 struct file
**extra_ref
;
1566 struct file
*locked_fp
;
1575 struct unp_gc_info info
;
1576 static boolean_t unp_gcing
;
1581 * Only one gc can be in-progress at any given moment
1583 spin_lock(&unp_spin
);
1585 spin_unlock(&unp_spin
);
1589 spin_unlock(&unp_spin
);
1591 lwkt_gettoken(&unp_token
);
1594 * Before going through all this, set all FDs to be NOT defered
1595 * and NOT externally accessible (not marked). During the scan
1596 * a fd can be marked externally accessible but we may or may not
1597 * be able to immediately process it (controlled by FDEFER).
1599 * If we loop sleep a bit. The complexity of the topology can cause
1600 * multiple loops. Also failure to acquire the socket's so_rcv
1601 * token can cause us to loop.
1603 allfiles_scan_exclusive(unp_gc_clearmarks
, NULL
);
1606 allfiles_scan_exclusive(unp_gc_checkmarks
, &info
);
1608 tsleep(&info
, 0, "gcagain", 1);
1609 } while (info
.defer
);
1612 * We grab an extra reference to each of the file table entries
1613 * that are not otherwise accessible and then free the rights
1614 * that are stored in messages on them.
1616 * The bug in the orginal code is a little tricky, so I'll describe
1617 * what's wrong with it here.
1619 * It is incorrect to simply unp_discard each entry for f_msgcount
1620 * times -- consider the case of sockets A and B that contain
1621 * references to each other. On a last close of some other socket,
1622 * we trigger a gc since the number of outstanding rights (unp_rights)
1623 * is non-zero. If during the sweep phase the gc code un_discards,
1624 * we end up doing a (full) closef on the descriptor. A closef on A
1625 * results in the following chain. Closef calls soo_close, which
1626 * calls soclose. Soclose calls first (through the switch
1627 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply
1628 * returns because the previous instance had set unp_gcing, and
1629 * we return all the way back to soclose, which marks the socket
1630 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush
1631 * to free up the rights that are queued in messages on the socket A,
1632 * i.e., the reference on B. The sorflush calls via the dom_dispose
1633 * switch unp_dispose, which unp_scans with unp_discard. This second
1634 * instance of unp_discard just calls closef on B.
1636 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1637 * which results in another closef on A. Unfortunately, A is already
1638 * being closed, and the descriptor has already been marked with
1639 * SS_NOFDREF, and soclose panics at this point.
1641 * Here, we first take an extra reference to each inaccessible
1642 * descriptor. Then, we call sorflush ourself, since we know
1643 * it is a Unix domain socket anyhow. After we destroy all the
1644 * rights carried in messages, we do a last closef to get rid
1645 * of our extra reference. This is the last close, and the
1646 * unp_detach etc will shut down the socket.
1648 * 91/09/19, bsy@cs.cmu.edu
1650 info
.extra_ref
= kmalloc(256 * sizeof(struct file
*), M_FILE
, M_WAITOK
);
1651 info
.maxindex
= 256;
1658 allfiles_scan_exclusive(unp_gc_checkrefs
, &info
);
1661 * For each FD on our hit list, do the following two things
1663 for (i
= info
.index
, fpp
= info
.extra_ref
; --i
>= 0; ++fpp
) {
1664 struct file
*tfp
= *fpp
;
1665 if (tfp
->f_type
== DTYPE_SOCKET
&& tfp
->f_data
!= NULL
)
1666 sorflush((struct socket
*)(tfp
->f_data
));
1668 for (i
= info
.index
, fpp
= info
.extra_ref
; --i
>= 0; ++fpp
)
1670 } while (info
.index
== info
.maxindex
);
1672 lwkt_reltoken(&unp_token
);
1674 kfree((caddr_t
)info
.extra_ref
, M_FILE
);
1679 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1682 unp_gc_checkrefs(struct file
*fp
, void *data
)
1684 struct unp_gc_info
*info
= data
;
1686 if (fp
->f_count
== 0)
1688 if (info
->index
== info
->maxindex
)
1692 * If all refs are from msgs, and it's not marked accessible
1693 * then it must be referenced from some unreachable cycle
1694 * of (shut-down) FDs, so include it in our
1695 * list of FDs to remove
1697 if (fp
->f_count
== fp
->f_msgcount
&& !(fp
->f_flag
& FMARK
)) {
1698 info
->extra_ref
[info
->index
++] = fp
;
1705 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1708 unp_gc_clearmarks(struct file
*fp
, void *data __unused
)
1710 atomic_clear_int(&fp
->f_flag
, FMARK
| FDEFER
);
1715 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1718 unp_gc_checkmarks(struct file
*fp
, void *data
)
1720 struct unp_gc_info
*info
= data
;
1724 * If the file is not open, skip it. Make sure it isn't marked
1725 * defered or we could loop forever, in case we somehow race
1728 if (fp
->f_count
== 0) {
1729 if (fp
->f_flag
& FDEFER
)
1730 atomic_clear_int(&fp
->f_flag
, FDEFER
);
1734 * If we already marked it as 'defer' in a
1735 * previous pass, then try process it this time
1738 if (fp
->f_flag
& FDEFER
) {
1739 atomic_clear_int(&fp
->f_flag
, FDEFER
);
1742 * if it's not defered, then check if it's
1743 * already marked.. if so skip it
1745 if (fp
->f_flag
& FMARK
)
1748 * If all references are from messages
1749 * in transit, then skip it. it's not
1750 * externally accessible.
1752 if (fp
->f_count
== fp
->f_msgcount
)
1755 * If it got this far then it must be
1756 * externally accessible.
1758 atomic_set_int(&fp
->f_flag
, FMARK
);
1762 * either it was defered, or it is externally
1763 * accessible and not already marked so.
1764 * Now check if it is possibly one of OUR sockets.
1766 if (fp
->f_type
!= DTYPE_SOCKET
||
1767 (so
= (struct socket
*)fp
->f_data
) == NULL
) {
1770 if (so
->so_proto
->pr_domain
!= &localdomain
||
1771 !(so
->so_proto
->pr_flags
& PR_RIGHTS
)) {
1776 * So, Ok, it's one of our sockets and it IS externally accessible
1777 * (or was defered). Now we look to see if we hold any file
1778 * descriptors in its message buffers. Follow those links and mark
1779 * them as accessible too.
1781 * We are holding multiple spinlocks here, if we cannot get the
1782 * token non-blocking defer until the next loop.
1784 info
->locked_fp
= fp
;
1785 if (lwkt_trytoken(&so
->so_rcv
.ssb_token
)) {
1786 unp_scan(so
->so_rcv
.ssb_mb
, unp_mark
, info
);
1787 lwkt_reltoken(&so
->so_rcv
.ssb_token
);
1789 atomic_set_int(&fp
->f_flag
, FDEFER
);
1796 * Scan all unix domain sockets and replace any revoked file pointers
1797 * found with the dummy file pointer fx. We don't worry about races
1798 * against file pointers being read out as those are handled in the
1802 #define REVOKE_GC_MAXFILES 32
1804 struct unp_revoke_gc_info
{
1806 struct file
*fary
[REVOKE_GC_MAXFILES
];
1811 unp_revoke_gc(struct file
*fx
)
1813 struct unp_revoke_gc_info info
;
1816 lwkt_gettoken(&unp_token
);
1820 allfiles_scan_exclusive(unp_revoke_gc_check
, &info
);
1821 for (i
= 0; i
< info
.fcount
; ++i
)
1822 unp_fp_externalize(NULL
, info
.fary
[i
], -1);
1823 } while (info
.fcount
== REVOKE_GC_MAXFILES
);
1824 lwkt_reltoken(&unp_token
);
1828 * Check for and replace revoked descriptors.
1830 * WARNING: This routine is not allowed to block.
1833 unp_revoke_gc_check(struct file
*fps
, void *vinfo
)
1835 struct unp_revoke_gc_info
*info
= vinfo
;
1846 * Is this a unix domain socket with rights-passing abilities?
1848 if (fps
->f_type
!= DTYPE_SOCKET
)
1850 if ((so
= (struct socket
*)fps
->f_data
) == NULL
)
1852 if (so
->so_proto
->pr_domain
!= &localdomain
)
1854 if ((so
->so_proto
->pr_flags
& PR_RIGHTS
) == 0)
1858 * Scan the mbufs for control messages and replace any revoked
1859 * descriptors we find.
1861 lwkt_gettoken(&so
->so_rcv
.ssb_token
);
1862 m0
= so
->so_rcv
.ssb_mb
;
1864 for (m
= m0
; m
; m
= m
->m_next
) {
1865 if (m
->m_type
!= MT_CONTROL
)
1867 if (m
->m_len
< sizeof(*cm
))
1869 cm
= mtod(m
, struct cmsghdr
*);
1870 if (cm
->cmsg_level
!= SOL_SOCKET
||
1871 cm
->cmsg_type
!= SCM_RIGHTS
) {
1874 qfds
= (cm
->cmsg_len
- CMSG_LEN(0)) / sizeof(void *);
1875 rp
= (struct file
**)CMSG_DATA(cm
);
1876 for (i
= 0; i
< qfds
; i
++) {
1878 if (fp
->f_flag
& FREVOKED
) {
1879 kprintf("Warning: Removing revoked fp from unix domain socket queue\n");
1881 info
->fx
->f_msgcount
++;
1884 info
->fary
[info
->fcount
++] = fp
;
1886 if (info
->fcount
== REVOKE_GC_MAXFILES
)
1889 if (info
->fcount
== REVOKE_GC_MAXFILES
)
1893 if (info
->fcount
== REVOKE_GC_MAXFILES
)
1896 lwkt_reltoken(&so
->so_rcv
.ssb_token
);
1899 * Stop the scan if we filled up our array.
1901 if (info
->fcount
== REVOKE_GC_MAXFILES
)
1907 * Dispose of the fp's stored in a mbuf.
1909 * The dds loop can cause additional fps to be entered onto the
1910 * list while it is running, flattening out the operation and avoiding
1911 * a deep kernel stack recursion.
1914 unp_dispose(struct mbuf
*m
)
1916 unp_defdiscard_t dds
;
1918 lwkt_gettoken(&unp_token
);
1919 ++unp_defdiscard_nest
;
1921 unp_scan(m
, unp_discard
, NULL
);
1923 if (unp_defdiscard_nest
== 1) {
1924 while ((dds
= unp_defdiscard_base
) != NULL
) {
1925 unp_defdiscard_base
= dds
->next
;
1926 closef(dds
->fp
, NULL
);
1927 kfree(dds
, M_UNPCB
);
1930 --unp_defdiscard_nest
;
1931 lwkt_reltoken(&unp_token
);
1935 unp_listen(struct unpcb
*unp
, struct thread
*td
)
1937 struct proc
*p
= td
->td_proc
;
1940 lwkt_gettoken(&unp_token
);
1941 cru2x(p
->p_ucred
, &unp
->unp_peercred
);
1942 unp
->unp_flags
|= UNP_HAVEPCCACHED
;
1943 lwkt_reltoken(&unp_token
);
1948 unp_scan(struct mbuf
*m0
, void (*op
)(struct file
*, void *), void *data
)
1957 for (m
= m0
; m
; m
= m
->m_next
) {
1958 if (m
->m_type
== MT_CONTROL
&&
1959 m
->m_len
>= sizeof(*cm
)) {
1960 cm
= mtod(m
, struct cmsghdr
*);
1961 if (cm
->cmsg_level
!= SOL_SOCKET
||
1962 cm
->cmsg_type
!= SCM_RIGHTS
)
1964 qfds
= (cm
->cmsg_len
- CMSG_LEN(0)) /
1966 rp
= (struct file
**)CMSG_DATA(cm
);
1967 for (i
= 0; i
< qfds
; i
++)
1969 break; /* XXX, but saves time */
1977 * Mark visibility. info->defer is recalculated on every pass.
1980 unp_mark(struct file
*fp
, void *data
)
1982 struct unp_gc_info
*info
= data
;
1984 if ((fp
->f_flag
& FMARK
) == 0) {
1986 atomic_set_int(&fp
->f_flag
, FMARK
| FDEFER
);
1987 } else if (fp
->f_flag
& FDEFER
) {
1993 * Discard a fp previously held in a unix domain socket mbuf. To
1994 * avoid blowing out the kernel stack due to contrived chain-reactions
1995 * we may have to defer the operation to a higher procedural level.
1997 * Caller holds unp_token
2000 unp_discard(struct file
*fp
, void *data __unused
)
2002 unp_defdiscard_t dds
;
2004 spin_lock(&unp_spin
);
2007 spin_unlock(&unp_spin
);
2009 if (unp_defdiscard_nest
) {
2010 dds
= kmalloc(sizeof(*dds
), M_UNPCB
, M_WAITOK
|M_ZERO
);
2012 dds
->next
= unp_defdiscard_base
;
2013 unp_defdiscard_base
= dds
;