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>
60 #include <sys/file2.h>
61 #include <sys/spinlock2.h>
62 #include <sys/socketvar2.h>
64 static MALLOC_DEFINE(M_UNPCB, "unpcb", "unpcb struct");
65 static unp_gen_t unp_gencnt;
66 static u_int unp_count;
68 static struct unp_head unp_shead, unp_dhead;
70 static struct lwkt_token unp_token = LWKT_TOKEN_MP_INITIALIZER(unp_token);
73 * Unix communications domain.
77 * rethink name space problems
78 * need a proper out-of-band
81 static struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL };
82 static ino_t unp_ino = 1; /* prototype for fake inode numbers */
83 static struct spinlock unp_ino_spin = SPINLOCK_INITIALIZER(&unp_ino_spin);
85 static int unp_attach (struct socket *, struct pru_attach_info *);
86 static void unp_detach (struct unpcb *);
87 static int unp_bind (struct unpcb *,struct sockaddr *, struct thread *);
88 static int unp_connect (struct socket *,struct sockaddr *,
90 static void unp_disconnect (struct unpcb *);
91 static void unp_shutdown (struct unpcb *);
92 static void unp_drop (struct unpcb *, int);
93 static void unp_gc (void);
94 static int unp_gc_clearmarks(struct file *, void *);
95 static int unp_gc_checkmarks(struct file *, void *);
96 static int unp_gc_checkrefs(struct file *, void *);
97 static int unp_revoke_gc_check(struct file *, void *);
98 static void unp_scan (struct mbuf *, void (*)(struct file *, void *),
100 static void unp_mark (struct file *, void *data);
101 static void unp_discard (struct file *, void *);
102 static int unp_internalize (struct mbuf *, struct thread *);
103 static int unp_listen (struct unpcb *, struct thread *);
104 static void unp_fp_externalize(struct lwp *lp, struct file *fp, int fd);
107 * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
108 * will sofree() it when we return.
111 uipc_abort(struct socket *so)
116 lwkt_gettoken(&unp_token);
119 unp_drop(unp, ECONNABORTED);
125 lwkt_reltoken(&unp_token);
131 uipc_accept(struct socket *so, struct sockaddr **nam)
135 lwkt_gettoken(&unp_token);
138 lwkt_reltoken(&unp_token);
143 * Pass back name of connected socket,
144 * if it was bound and we are still connected
145 * (our peer may have closed already!).
147 if (unp->unp_conn && unp->unp_conn->unp_addr) {
148 *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr);
150 *nam = dup_sockaddr((struct sockaddr *)&sun_noname);
152 lwkt_reltoken(&unp_token);
157 uipc_attach(struct socket *so, int proto, struct pru_attach_info *ai)
162 lwkt_gettoken(&unp_token);
167 error = unp_attach(so, ai);
168 lwkt_reltoken(&unp_token);
174 uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
179 lwkt_gettoken(&unp_token);
182 error = unp_bind(unp, nam, td);
185 lwkt_reltoken(&unp_token);
191 uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
196 lwkt_gettoken(&unp_token);
199 error = unp_connect(so, nam, td);
202 lwkt_reltoken(&unp_token);
208 uipc_connect2(struct socket *so1, struct socket *so2)
213 lwkt_gettoken(&unp_token);
216 error = unp_connect2(so1, so2);
219 lwkt_reltoken(&unp_token);
224 /* control is EOPNOTSUPP */
227 uipc_detach(struct socket *so)
232 lwkt_gettoken(&unp_token);
240 lwkt_reltoken(&unp_token);
246 uipc_disconnect(struct socket *so)
251 lwkt_gettoken(&unp_token);
259 lwkt_reltoken(&unp_token);
265 uipc_listen(struct socket *so, struct thread *td)
270 lwkt_gettoken(&unp_token);
272 if (unp == NULL || unp->unp_vnode == NULL)
275 error = unp_listen(unp, td);
276 lwkt_reltoken(&unp_token);
282 uipc_peeraddr(struct socket *so, struct sockaddr **nam)
287 lwkt_gettoken(&unp_token);
291 } else if (unp->unp_conn && unp->unp_conn->unp_addr) {
292 *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr);
296 * XXX: It seems that this test always fails even when
297 * connection is established. So, this else clause is
298 * added as workaround to return PF_LOCAL sockaddr.
300 *nam = dup_sockaddr((struct sockaddr *)&sun_noname);
303 lwkt_reltoken(&unp_token);
309 uipc_rcvd(struct socket *so, int flags)
314 lwkt_gettoken(&unp_token);
317 lwkt_reltoken(&unp_token);
321 switch (so->so_type) {
323 panic("uipc_rcvd DGRAM?");
327 if (unp->unp_conn == NULL)
330 * Because we are transfering mbufs directly to the
331 * peer socket we have to use SSB_STOP on the sender
332 * to prevent it from building up infinite mbufs.
334 so2 = unp->unp_conn->unp_socket;
335 if (so->so_rcv.ssb_cc < so2->so_snd.ssb_hiwat &&
336 so->so_rcv.ssb_mbcnt < so2->so_snd.ssb_mbmax
338 atomic_clear_int(&so2->so_snd.ssb_flags, SSB_STOP);
343 panic("uipc_rcvd unknown socktype");
346 lwkt_reltoken(&unp_token);
351 /* pru_rcvoob is EOPNOTSUPP */
354 uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
355 struct mbuf *control, struct thread *td)
361 lwkt_gettoken(&unp_token);
368 if (flags & PRUS_OOB) {
373 if (control && (error = unp_internalize(control, td)))
376 switch (so->so_type) {
379 struct sockaddr *from;
386 error = unp_connect(so, nam, td);
390 if (unp->unp_conn == NULL) {
395 so2 = unp->unp_conn->unp_socket;
397 from = (struct sockaddr *)unp->unp_addr;
400 if (ssb_appendaddr(&so2->so_rcv, from, m, control)) {
414 /* Connect if not connected yet. */
416 * Note: A better implementation would complain
417 * if not equal to the peer's address.
419 if (!(so->so_state & SS_ISCONNECTED)) {
421 error = unp_connect(so, nam, td);
430 if (so->so_state & SS_CANTSENDMORE) {
434 if (unp->unp_conn == NULL)
435 panic("uipc_send connected but no connection?");
436 so2 = unp->unp_conn->unp_socket;
438 * Send to paired receive port, and then reduce
439 * send buffer hiwater marks to maintain backpressure.
443 if (ssb_appendcontrol(&so2->so_rcv, m, control)) {
447 } else if (so->so_type == SOCK_SEQPACKET) {
448 sbappendrecord(&so2->so_rcv.sb, m);
451 sbappend(&so2->so_rcv.sb, m);
456 * Because we are transfering mbufs directly to the
457 * peer socket we have to use SSB_STOP on the sender
458 * to prevent it from building up infinite mbufs.
460 if (so2->so_rcv.ssb_cc >= so->so_snd.ssb_hiwat ||
461 so2->so_rcv.ssb_mbcnt >= so->so_snd.ssb_mbmax
463 atomic_set_int(&so->so_snd.ssb_flags, SSB_STOP);
469 panic("uipc_send unknown socktype");
473 * SEND_EOF is equivalent to a SEND followed by a SHUTDOWN.
475 if (flags & PRUS_EOF) {
480 if (control && error != 0)
481 unp_dispose(control);
484 lwkt_reltoken(&unp_token);
497 uipc_sense(struct socket *so, struct stat *sb)
501 lwkt_gettoken(&unp_token);
504 lwkt_reltoken(&unp_token);
507 sb->st_blksize = so->so_snd.ssb_hiwat;
509 if (unp->unp_ino == 0) { /* make up a non-zero inode number */
510 spin_lock(&unp_ino_spin);
511 unp->unp_ino = unp_ino++;
512 spin_unlock(&unp_ino_spin);
514 sb->st_ino = unp->unp_ino;
515 lwkt_reltoken(&unp_token);
521 uipc_shutdown(struct socket *so)
526 lwkt_gettoken(&unp_token);
535 lwkt_reltoken(&unp_token);
541 uipc_sockaddr(struct socket *so, struct sockaddr **nam)
546 lwkt_gettoken(&unp_token);
550 *nam = dup_sockaddr((struct sockaddr *)unp->unp_addr);
555 lwkt_reltoken(&unp_token);
560 struct pr_usrreqs uipc_usrreqs = {
561 .pru_abort = uipc_abort,
562 .pru_accept = uipc_accept,
563 .pru_attach = uipc_attach,
564 .pru_bind = uipc_bind,
565 .pru_connect = uipc_connect,
566 .pru_connect2 = uipc_connect2,
567 .pru_control = pru_control_notsupp,
568 .pru_detach = uipc_detach,
569 .pru_disconnect = uipc_disconnect,
570 .pru_listen = uipc_listen,
571 .pru_peeraddr = uipc_peeraddr,
572 .pru_rcvd = uipc_rcvd,
573 .pru_rcvoob = pru_rcvoob_notsupp,
574 .pru_send = uipc_send,
575 .pru_sense = uipc_sense,
576 .pru_shutdown = uipc_shutdown,
577 .pru_sockaddr = uipc_sockaddr,
578 .pru_sosend = sosend,
579 .pru_soreceive = soreceive
583 uipc_ctloutput(struct socket *so, struct sockopt *sopt)
588 lwkt_gettoken(&unp_token);
591 switch (sopt->sopt_dir) {
593 switch (sopt->sopt_name) {
595 if (unp->unp_flags & UNP_HAVEPC)
596 soopt_from_kbuf(sopt, &unp->unp_peercred,
597 sizeof(unp->unp_peercred));
599 if (so->so_type == SOCK_STREAM)
601 else if (so->so_type == SOCK_SEQPACKET)
617 lwkt_reltoken(&unp_token);
623 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
624 * for stream sockets, although the total for sender and receiver is
625 * actually only PIPSIZ.
627 * Datagram sockets really use the sendspace as the maximum datagram size,
628 * and don't really want to reserve the sendspace. Their recvspace should
629 * be large enough for at least one max-size datagram plus address.
631 * We want the local send/recv space to be significant larger then lo0's
637 static u_long unpst_sendspace = PIPSIZ;
638 static u_long unpst_recvspace = PIPSIZ;
639 static u_long unpdg_sendspace = 2*1024; /* really max datagram size */
640 static u_long unpdg_recvspace = 4*1024;
642 static int unp_rights; /* file descriptors in flight */
643 static struct spinlock unp_spin = SPINLOCK_INITIALIZER(&unp_spin);
645 SYSCTL_DECL(_net_local_seqpacket);
646 SYSCTL_DECL(_net_local_stream);
647 SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
648 &unpst_sendspace, 0, "");
649 SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
650 &unpst_recvspace, 0, "");
652 SYSCTL_DECL(_net_local_dgram);
653 SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
654 &unpdg_sendspace, 0, "");
655 SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
656 &unpdg_recvspace, 0, "");
658 SYSCTL_DECL(_net_local);
659 SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, "");
662 unp_attach(struct socket *so, struct pru_attach_info *ai)
667 lwkt_gettoken(&unp_token);
668 if (so->so_snd.ssb_hiwat == 0 || so->so_rcv.ssb_hiwat == 0) {
669 switch (so->so_type) {
673 error = soreserve(so, unpst_sendspace, unpst_recvspace,
678 error = soreserve(so, unpdg_sendspace, unpdg_recvspace,
688 unp = kmalloc(sizeof(*unp), M_UNPCB, M_NOWAIT|M_ZERO);
693 unp->unp_gencnt = ++unp_gencnt;
695 LIST_INIT(&unp->unp_refs);
696 unp->unp_socket = so;
697 unp->unp_rvnode = ai->fd_rdir; /* jail cruft XXX JH */
698 LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead
699 : &unp_shead, unp, unp_link);
700 so->so_pcb = (caddr_t)unp;
702 so->so_port = sync_soport(so, NULL, NULL);
705 lwkt_reltoken(&unp_token);
710 unp_detach(struct unpcb *unp)
714 lwkt_gettoken(&unp_token);
716 LIST_REMOVE(unp, unp_link);
717 unp->unp_gencnt = ++unp_gencnt;
719 if (unp->unp_vnode) {
720 unp->unp_vnode->v_socket = NULL;
721 vrele(unp->unp_vnode);
722 unp->unp_vnode = NULL;
726 while (!LIST_EMPTY(&unp->unp_refs))
727 unp_drop(LIST_FIRST(&unp->unp_refs), ECONNRESET);
728 soisdisconnected(unp->unp_socket);
729 so = unp->unp_socket;
730 soreference(so); /* for delayed sorflush */
732 unp->unp_socket = NULL;
733 sofree(so); /* remove pcb ref */
737 * Normally the receive buffer is flushed later,
738 * in sofree, but if our receive buffer holds references
739 * to descriptors that are now garbage, we will dispose
740 * of those descriptor references after the garbage collector
741 * gets them (resulting in a "panic: closef: count < 0").
747 lwkt_reltoken(&unp_token);
750 kfree(unp->unp_addr, M_SONAME);
755 unp_bind(struct unpcb *unp, struct sockaddr *nam, struct thread *td)
757 struct proc *p = td->td_proc;
758 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
762 struct nlookupdata nd;
763 char buf[SOCK_MAXADDRLEN];
765 lwkt_gettoken(&unp_token);
766 if (unp->unp_vnode != NULL) {
770 namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
775 strncpy(buf, soun->sun_path, namelen);
776 buf[namelen] = 0; /* null-terminate the string */
777 error = nlookup_init(&nd, buf, UIO_SYSSPACE,
778 NLC_LOCKVP | NLC_CREATE | NLC_REFDVP);
780 error = nlookup(&nd);
781 if (error == 0 && nd.nl_nch.ncp->nc_vp != NULL)
787 vattr.va_type = VSOCK;
788 vattr.va_mode = (ACCESSPERMS & ~p->p_fd->fd_cmask);
789 error = VOP_NCREATE(&nd.nl_nch, nd.nl_dvp, &vp, nd.nl_cred, &vattr);
791 vp->v_socket = unp->unp_socket;
793 unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam);
799 lwkt_reltoken(&unp_token);
804 unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
806 struct proc *p = td->td_proc;
807 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
809 struct socket *so2, *so3;
810 struct unpcb *unp, *unp2, *unp3;
812 struct nlookupdata nd;
813 char buf[SOCK_MAXADDRLEN];
815 lwkt_gettoken(&unp_token);
817 len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
822 strncpy(buf, soun->sun_path, len);
826 error = nlookup_init(&nd, buf, UIO_SYSSPACE, NLC_FOLLOW);
828 error = nlookup(&nd);
830 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
835 if (vp->v_type != VSOCK) {
839 error = VOP_ACCESS(vp, VWRITE, p->p_ucred);
844 error = ECONNREFUSED;
847 if (so->so_type != so2->so_type) {
851 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
852 if (!(so2->so_options & SO_ACCEPTCONN) ||
853 (so3 = sonewconn(so2, 0)) == NULL) {
854 error = ECONNREFUSED;
861 unp3->unp_addr = (struct sockaddr_un *)
862 dup_sockaddr((struct sockaddr *)unp2->unp_addr);
865 * unp_peercred management:
867 * The connecter's (client's) credentials are copied
868 * from its process structure at the time of connect()
871 cru2x(p->p_ucred, &unp3->unp_peercred);
872 unp3->unp_flags |= UNP_HAVEPC;
874 * The receiver's (server's) credentials are copied
875 * from the unp_peercred member of socket on which the
876 * former called listen(); unp_listen() cached that
877 * process's credentials at that time so we can use
880 KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED,
881 ("unp_connect: listener without cached peercred"));
882 memcpy(&unp->unp_peercred, &unp2->unp_peercred,
883 sizeof(unp->unp_peercred));
884 unp->unp_flags |= UNP_HAVEPC;
888 error = unp_connect2(so, so2);
892 lwkt_reltoken(&unp_token);
897 unp_connect2(struct socket *so, struct socket *so2)
902 lwkt_gettoken(&unp_token);
904 if (so2->so_type != so->so_type) {
905 lwkt_reltoken(&unp_token);
909 unp->unp_conn = unp2;
911 switch (so->so_type) {
913 LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
919 unp2->unp_conn = unp;
925 panic("unp_connect2");
927 lwkt_reltoken(&unp_token);
932 unp_disconnect(struct unpcb *unp)
936 lwkt_gettoken(&unp_token);
938 unp2 = unp->unp_conn;
940 lwkt_reltoken(&unp_token);
944 unp->unp_conn = NULL;
946 switch (unp->unp_socket->so_type) {
948 LIST_REMOVE(unp, unp_reflink);
949 soclrstate(unp->unp_socket, SS_ISCONNECTED);
953 soisdisconnected(unp->unp_socket);
954 unp2->unp_conn = NULL;
955 soisdisconnected(unp2->unp_socket);
958 lwkt_reltoken(&unp_token);
963 unp_abort(struct unpcb *unp)
965 lwkt_gettoken(&unp_token);
967 lwkt_reltoken(&unp_token);
972 prison_unpcb(struct thread *td, struct unpcb *unp)
978 if ((p = td->td_proc) == NULL)
980 if (!p->p_ucred->cr_prison)
982 if (p->p_fd->fd_rdir == unp->unp_rvnode)
988 unp_pcblist(SYSCTL_HANDLER_ARGS)
991 struct unpcb *unp, **unp_list;
993 struct unp_head *head;
995 head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead);
997 KKASSERT(curproc != NULL);
1000 * The process of preparing the PCB list is too time-consuming and
1001 * resource-intensive to repeat twice on every request.
1003 if (req->oldptr == NULL) {
1005 req->oldidx = (n + n/8) * sizeof(struct xunpcb);
1009 if (req->newptr != NULL)
1012 lwkt_gettoken(&unp_token);
1015 * OK, now we're committed to doing something.
1017 gencnt = unp_gencnt;
1020 unp_list = kmalloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
1022 for (unp = LIST_FIRST(head), i = 0; unp && i < n;
1023 unp = LIST_NEXT(unp, unp_link)) {
1024 if (unp->unp_gencnt <= gencnt && !prison_unpcb(req->td, unp))
1025 unp_list[i++] = unp;
1027 n = i; /* in case we lost some during malloc */
1030 for (i = 0; i < n; i++) {
1032 if (unp->unp_gencnt <= gencnt) {
1034 xu.xu_len = sizeof xu;
1037 * XXX - need more locking here to protect against
1038 * connect/disconnect races for SMP.
1041 bcopy(unp->unp_addr, &xu.xu_addr,
1042 unp->unp_addr->sun_len);
1043 if (unp->unp_conn && unp->unp_conn->unp_addr)
1044 bcopy(unp->unp_conn->unp_addr,
1046 unp->unp_conn->unp_addr->sun_len);
1047 bcopy(unp, &xu.xu_unp, sizeof *unp);
1048 sotoxsocket(unp->unp_socket, &xu.xu_socket);
1049 error = SYSCTL_OUT(req, &xu, sizeof xu);
1052 lwkt_reltoken(&unp_token);
1053 kfree(unp_list, M_TEMP);
1058 SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD,
1059 (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
1060 "List of active local datagram sockets");
1061 SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD,
1062 (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
1063 "List of active local stream sockets");
1064 SYSCTL_PROC(_net_local_seqpacket, OID_AUTO, pcblist, CTLFLAG_RD,
1065 (caddr_t)(long)SOCK_SEQPACKET, 0, unp_pcblist, "S,xunpcb",
1066 "List of active local seqpacket stream sockets");
1069 unp_shutdown(struct unpcb *unp)
1073 if ((unp->unp_socket->so_type == SOCK_STREAM ||
1074 unp->unp_socket->so_type == SOCK_SEQPACKET) &&
1075 unp->unp_conn != NULL && (so = unp->unp_conn->unp_socket)) {
1081 unp_drop(struct unpcb *unp, int err)
1083 struct socket *so = unp->unp_socket;
1086 unp_disconnect(unp);
1093 lwkt_gettoken(&unp_token);
1094 lwkt_reltoken(&unp_token);
1099 unp_externalize(struct mbuf *rights)
1101 struct thread *td = curthread;
1102 struct proc *p = td->td_proc; /* XXX */
1103 struct lwp *lp = td->td_lwp;
1104 struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
1109 int newfds = (cm->cmsg_len - (CMSG_DATA(cm) - (u_char *)cm))
1110 / sizeof (struct file *);
1113 lwkt_gettoken(&unp_token);
1116 * if the new FD's will not fit, then we free them all
1118 if (!fdavail(p, newfds)) {
1119 rp = (struct file **)CMSG_DATA(cm);
1120 for (i = 0; i < newfds; i++) {
1123 * zero the pointer before calling unp_discard,
1124 * since it may end up in unp_gc()..
1127 unp_discard(fp, NULL);
1129 lwkt_reltoken(&unp_token);
1134 * now change each pointer to an fd in the global table to
1135 * an integer that is the index to the local fd table entry
1136 * that we set up to point to the global one we are transferring.
1137 * If sizeof (struct file *) is bigger than or equal to sizeof int,
1138 * then do it in forward order. In that case, an integer will
1139 * always come in the same place or before its corresponding
1140 * struct file pointer.
1141 * If sizeof (struct file *) is smaller than sizeof int, then
1142 * do it in reverse order.
1144 if (sizeof (struct file *) >= sizeof (int)) {
1145 fdp = (int *)CMSG_DATA(cm);
1146 rp = (struct file **)CMSG_DATA(cm);
1147 for (i = 0; i < newfds; i++) {
1148 if (fdalloc(p, 0, &f))
1149 panic("unp_externalize");
1151 unp_fp_externalize(lp, fp, f);
1155 fdp = (int *)CMSG_DATA(cm) + newfds - 1;
1156 rp = (struct file **)CMSG_DATA(cm) + newfds - 1;
1157 for (i = 0; i < newfds; i++) {
1158 if (fdalloc(p, 0, &f))
1159 panic("unp_externalize");
1161 unp_fp_externalize(lp, fp, f);
1167 * Adjust length, in case sizeof(struct file *) and sizeof(int)
1170 cm->cmsg_len = CMSG_LEN(newfds * sizeof(int));
1171 rights->m_len = cm->cmsg_len;
1173 lwkt_reltoken(&unp_token);
1178 unp_fp_externalize(struct lwp *lp, struct file *fp, int fd)
1183 lwkt_gettoken(&unp_token);
1187 if (fp->f_flag & FREVOKED) {
1188 kprintf("Warning: revoked fp exiting unix socket\n");
1190 error = falloc(lp, &fx, NULL);
1192 fsetfd(lp->lwp_proc->p_fd, fx, fd);
1194 fsetfd(lp->lwp_proc->p_fd, NULL, fd);
1197 fsetfd(lp->lwp_proc->p_fd, fp, fd);
1200 spin_lock(&unp_spin);
1203 spin_unlock(&unp_spin);
1206 lwkt_reltoken(&unp_token);
1213 LIST_INIT(&unp_dhead);
1214 LIST_INIT(&unp_shead);
1215 spin_init(&unp_spin);
1219 unp_internalize(struct mbuf *control, struct thread *td)
1221 struct proc *p = td->td_proc;
1222 struct filedesc *fdescp;
1223 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1227 struct cmsgcred *cmcred;
1233 lwkt_gettoken(&unp_token);
1236 if ((cm->cmsg_type != SCM_RIGHTS && cm->cmsg_type != SCM_CREDS) ||
1237 cm->cmsg_level != SOL_SOCKET ||
1238 CMSG_ALIGN(cm->cmsg_len) != control->m_len) {
1244 * Fill in credential information.
1246 if (cm->cmsg_type == SCM_CREDS) {
1247 cmcred = (struct cmsgcred *)CMSG_DATA(cm);
1248 cmcred->cmcred_pid = p->p_pid;
1249 cmcred->cmcred_uid = p->p_ucred->cr_ruid;
1250 cmcred->cmcred_gid = p->p_ucred->cr_rgid;
1251 cmcred->cmcred_euid = p->p_ucred->cr_uid;
1252 cmcred->cmcred_ngroups = MIN(p->p_ucred->cr_ngroups,
1254 for (i = 0; i < cmcred->cmcred_ngroups; i++)
1255 cmcred->cmcred_groups[i] = p->p_ucred->cr_groups[i];
1261 * cmsghdr may not be aligned, do not allow calculation(s) to
1264 if (cm->cmsg_len < CMSG_LEN(0)) {
1269 oldfds = (cm->cmsg_len - CMSG_LEN(0)) / sizeof (int);
1272 * check that all the FDs passed in refer to legal OPEN files
1273 * If not, reject the entire operation.
1275 fdp = (int *)CMSG_DATA(cm);
1276 for (i = 0; i < oldfds; i++) {
1278 if ((unsigned)fd >= fdescp->fd_nfiles ||
1279 fdescp->fd_files[fd].fp == NULL) {
1283 if (fdescp->fd_files[fd].fp->f_type == DTYPE_KQUEUE) {
1289 * Now replace the integer FDs with pointers to
1290 * the associated global file table entry..
1291 * Allocate a bigger buffer as necessary. But if an cluster is not
1292 * enough, return E2BIG.
1294 newlen = CMSG_LEN(oldfds * sizeof(struct file *));
1295 if (newlen > MCLBYTES) {
1299 if (newlen - control->m_len > M_TRAILINGSPACE(control)) {
1300 if (control->m_flags & M_EXT) {
1304 MCLGET(control, MB_WAIT);
1305 if (!(control->m_flags & M_EXT)) {
1310 /* copy the data to the cluster */
1311 memcpy(mtod(control, char *), cm, cm->cmsg_len);
1312 cm = mtod(control, struct cmsghdr *);
1316 * Adjust length, in case sizeof(struct file *) and sizeof(int)
1319 cm->cmsg_len = newlen;
1320 control->m_len = CMSG_ALIGN(newlen);
1323 * Transform the file descriptors into struct file pointers.
1324 * If sizeof (struct file *) is bigger than or equal to sizeof int,
1325 * then do it in reverse order so that the int won't get until
1327 * If sizeof (struct file *) is smaller than sizeof int, then
1328 * do it in forward order.
1330 if (sizeof (struct file *) >= sizeof (int)) {
1331 fdp = (int *)CMSG_DATA(cm) + oldfds - 1;
1332 rp = (struct file **)CMSG_DATA(cm) + oldfds - 1;
1333 for (i = 0; i < oldfds; i++) {
1334 fp = fdescp->fd_files[*fdp--].fp;
1337 spin_lock(&unp_spin);
1340 spin_unlock(&unp_spin);
1343 fdp = (int *)CMSG_DATA(cm);
1344 rp = (struct file **)CMSG_DATA(cm);
1345 for (i = 0; i < oldfds; i++) {
1346 fp = fdescp->fd_files[*fdp++].fp;
1349 spin_lock(&unp_spin);
1352 spin_unlock(&unp_spin);
1357 lwkt_reltoken(&unp_token);
1362 * Garbage collect in-transit file descriptors that get lost due to
1363 * loops (i.e. when a socket is sent to another process over itself,
1364 * and more complex situations).
1366 * NOT MPSAFE - TODO socket flush code and maybe closef. Rest is MPSAFE.
1369 struct unp_gc_info {
1370 struct file **extra_ref;
1371 struct file *locked_fp;
1380 struct unp_gc_info info;
1381 static boolean_t unp_gcing;
1385 spin_lock(&unp_spin);
1387 spin_unlock(&unp_spin);
1391 spin_unlock(&unp_spin);
1393 lwkt_gettoken(&unp_token);
1396 * before going through all this, set all FDs to
1397 * be NOT defered and NOT externally accessible
1400 allfiles_scan_exclusive(unp_gc_clearmarks, NULL);
1402 allfiles_scan_exclusive(unp_gc_checkmarks, &info);
1403 } while (info.defer);
1406 * We grab an extra reference to each of the file table entries
1407 * that are not otherwise accessible and then free the rights
1408 * that are stored in messages on them.
1410 * The bug in the orginal code is a little tricky, so I'll describe
1411 * what's wrong with it here.
1413 * It is incorrect to simply unp_discard each entry for f_msgcount
1414 * times -- consider the case of sockets A and B that contain
1415 * references to each other. On a last close of some other socket,
1416 * we trigger a gc since the number of outstanding rights (unp_rights)
1417 * is non-zero. If during the sweep phase the gc code un_discards,
1418 * we end up doing a (full) closef on the descriptor. A closef on A
1419 * results in the following chain. Closef calls soo_close, which
1420 * calls soclose. Soclose calls first (through the switch
1421 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply
1422 * returns because the previous instance had set unp_gcing, and
1423 * we return all the way back to soclose, which marks the socket
1424 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush
1425 * to free up the rights that are queued in messages on the socket A,
1426 * i.e., the reference on B. The sorflush calls via the dom_dispose
1427 * switch unp_dispose, which unp_scans with unp_discard. This second
1428 * instance of unp_discard just calls closef on B.
1430 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1431 * which results in another closef on A. Unfortunately, A is already
1432 * being closed, and the descriptor has already been marked with
1433 * SS_NOFDREF, and soclose panics at this point.
1435 * Here, we first take an extra reference to each inaccessible
1436 * descriptor. Then, we call sorflush ourself, since we know
1437 * it is a Unix domain socket anyhow. After we destroy all the
1438 * rights carried in messages, we do a last closef to get rid
1439 * of our extra reference. This is the last close, and the
1440 * unp_detach etc will shut down the socket.
1442 * 91/09/19, bsy@cs.cmu.edu
1444 info.extra_ref = kmalloc(256 * sizeof(struct file *), M_FILE, M_WAITOK);
1445 info.maxindex = 256;
1452 allfiles_scan_exclusive(unp_gc_checkrefs, &info);
1455 * For each FD on our hit list, do the following two things
1457 for (i = info.index, fpp = info.extra_ref; --i >= 0; ++fpp) {
1458 struct file *tfp = *fpp;
1459 if (tfp->f_type == DTYPE_SOCKET && tfp->f_data != NULL)
1460 sorflush((struct socket *)(tfp->f_data));
1462 for (i = info.index, fpp = info.extra_ref; --i >= 0; ++fpp)
1464 } while (info.index == info.maxindex);
1466 lwkt_reltoken(&unp_token);
1468 kfree((caddr_t)info.extra_ref, M_FILE);
1473 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1476 unp_gc_checkrefs(struct file *fp, void *data)
1478 struct unp_gc_info *info = data;
1480 if (fp->f_count == 0)
1482 if (info->index == info->maxindex)
1486 * If all refs are from msgs, and it's not marked accessible
1487 * then it must be referenced from some unreachable cycle
1488 * of (shut-down) FDs, so include it in our
1489 * list of FDs to remove
1491 if (fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) {
1492 info->extra_ref[info->index++] = fp;
1499 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1502 unp_gc_clearmarks(struct file *fp, void *data __unused)
1504 atomic_clear_int(&fp->f_flag, FMARK | FDEFER);
1509 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1512 unp_gc_checkmarks(struct file *fp, void *data)
1514 struct unp_gc_info *info = data;
1518 * If the file is not open, skip it
1520 if (fp->f_count == 0)
1523 * If we already marked it as 'defer' in a
1524 * previous pass, then try process it this time
1527 if (fp->f_flag & FDEFER) {
1528 atomic_clear_int(&fp->f_flag, FDEFER);
1532 * if it's not defered, then check if it's
1533 * already marked.. if so skip it
1535 if (fp->f_flag & FMARK)
1538 * If all references are from messages
1539 * in transit, then skip it. it's not
1540 * externally accessible.
1542 if (fp->f_count == fp->f_msgcount)
1545 * If it got this far then it must be
1546 * externally accessible.
1548 atomic_set_int(&fp->f_flag, FMARK);
1552 * either it was defered, or it is externally
1553 * accessible and not already marked so.
1554 * Now check if it is possibly one of OUR sockets.
1556 if (fp->f_type != DTYPE_SOCKET ||
1557 (so = (struct socket *)fp->f_data) == NULL)
1559 if (so->so_proto->pr_domain != &localdomain ||
1560 !(so->so_proto->pr_flags & PR_RIGHTS))
1563 if (so->so_rcv.ssb_flags & SSB_LOCK) {
1565 * This is problematical; it's not clear
1566 * we need to wait for the sockbuf to be
1567 * unlocked (on a uniprocessor, at least),
1568 * and it's also not clear what to do
1569 * if sbwait returns an error due to receipt
1570 * of a signal. If sbwait does return
1571 * an error, we'll go into an infinite
1572 * loop. Delete all of this for now.
1574 sbwait(&so->so_rcv);
1579 * So, Ok, it's one of our sockets and it IS externally
1580 * accessible (or was defered). Now we look
1581 * to see if we hold any file descriptors in its
1582 * message buffers. Follow those links and mark them
1583 * as accessible too.
1585 info->locked_fp = fp;
1586 /* spin_lock_wr(&so->so_rcv.sb_spin); */
1587 unp_scan(so->so_rcv.ssb_mb, unp_mark, info);
1588 /* spin_unlock_wr(&so->so_rcv.sb_spin);*/
1593 * Scan all unix domain sockets and replace any revoked file pointers
1594 * found with the dummy file pointer fx. We don't worry about races
1595 * against file pointers being read out as those are handled in the
1599 #define REVOKE_GC_MAXFILES 32
1601 struct unp_revoke_gc_info {
1603 struct file *fary[REVOKE_GC_MAXFILES];
1608 unp_revoke_gc(struct file *fx)
1610 struct unp_revoke_gc_info info;
1613 lwkt_gettoken(&unp_token);
1617 allfiles_scan_exclusive(unp_revoke_gc_check, &info);
1618 for (i = 0; i < info.fcount; ++i)
1619 unp_fp_externalize(NULL, info.fary[i], -1);
1620 } while (info.fcount == REVOKE_GC_MAXFILES);
1621 lwkt_reltoken(&unp_token);
1625 * Check for and replace revoked descriptors.
1627 * WARNING: This routine is not allowed to block.
1630 unp_revoke_gc_check(struct file *fps, void *vinfo)
1632 struct unp_revoke_gc_info *info = vinfo;
1643 * Is this a unix domain socket with rights-passing abilities?
1645 if (fps->f_type != DTYPE_SOCKET)
1647 if ((so = (struct socket *)fps->f_data) == NULL)
1649 if (so->so_proto->pr_domain != &localdomain)
1651 if ((so->so_proto->pr_flags & PR_RIGHTS) == 0)
1655 * Scan the mbufs for control messages and replace any revoked
1656 * descriptors we find.
1658 m0 = so->so_rcv.ssb_mb;
1660 for (m = m0; m; m = m->m_next) {
1661 if (m->m_type != MT_CONTROL)
1663 if (m->m_len < sizeof(*cm))
1665 cm = mtod(m, struct cmsghdr *);
1666 if (cm->cmsg_level != SOL_SOCKET ||
1667 cm->cmsg_type != SCM_RIGHTS) {
1670 qfds = (cm->cmsg_len - CMSG_LEN(0)) / sizeof(void *);
1671 rp = (struct file **)CMSG_DATA(cm);
1672 for (i = 0; i < qfds; i++) {
1674 if (fp->f_flag & FREVOKED) {
1675 kprintf("Warning: Removing revoked fp from unix domain socket queue\n");
1677 info->fx->f_msgcount++;
1680 info->fary[info->fcount++] = fp;
1682 if (info->fcount == REVOKE_GC_MAXFILES)
1685 if (info->fcount == REVOKE_GC_MAXFILES)
1689 if (info->fcount == REVOKE_GC_MAXFILES)
1694 * Stop the scan if we filled up our array.
1696 if (info->fcount == REVOKE_GC_MAXFILES)
1702 unp_dispose(struct mbuf *m)
1704 lwkt_gettoken(&unp_token);
1706 unp_scan(m, unp_discard, NULL);
1707 lwkt_reltoken(&unp_token);
1711 unp_listen(struct unpcb *unp, struct thread *td)
1713 struct proc *p = td->td_proc;
1716 lwkt_gettoken(&unp_token);
1717 cru2x(p->p_ucred, &unp->unp_peercred);
1718 unp->unp_flags |= UNP_HAVEPCCACHED;
1719 lwkt_reltoken(&unp_token);
1724 unp_scan(struct mbuf *m0, void (*op)(struct file *, void *), void *data)
1733 for (m = m0; m; m = m->m_next) {
1734 if (m->m_type == MT_CONTROL &&
1735 m->m_len >= sizeof(*cm)) {
1736 cm = mtod(m, struct cmsghdr *);
1737 if (cm->cmsg_level != SOL_SOCKET ||
1738 cm->cmsg_type != SCM_RIGHTS)
1740 qfds = (cm->cmsg_len - CMSG_LEN(0)) /
1742 rp = (struct file **)CMSG_DATA(cm);
1743 for (i = 0; i < qfds; i++)
1745 break; /* XXX, but saves time */
1753 unp_mark(struct file *fp, void *data)
1755 struct unp_gc_info *info = data;
1757 if ((fp->f_flag & FMARK) == 0) {
1759 atomic_set_int(&fp->f_flag, FMARK | FDEFER);
1764 unp_discard(struct file *fp, void *data __unused)
1766 spin_lock(&unp_spin);
1769 spin_unlock(&unp_spin);