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>
63 #include <sys/msgport2.h>
65 static MALLOC_DEFINE(M_UNPCB, "unpcb", "unpcb struct");
66 static unp_gen_t unp_gencnt;
67 static u_int unp_count;
69 static struct unp_head unp_shead, unp_dhead;
71 static struct lwkt_token unp_token = LWKT_TOKEN_MP_INITIALIZER(unp_token);
74 * Unix communications domain.
78 * rethink name space problems
79 * need a proper out-of-band
82 static struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL };
83 static ino_t unp_ino = 1; /* prototype for fake inode numbers */
84 static struct spinlock unp_ino_spin = SPINLOCK_INITIALIZER(&unp_ino_spin);
86 static int unp_attach (struct socket *, struct pru_attach_info *);
87 static void unp_detach (struct unpcb *);
88 static int unp_bind (struct unpcb *,struct sockaddr *, struct thread *);
89 static int unp_connect (struct socket *,struct sockaddr *,
91 static void unp_disconnect (struct unpcb *);
92 static void unp_shutdown (struct unpcb *);
93 static void unp_drop (struct unpcb *, int);
94 static void unp_gc (void);
95 static int unp_gc_clearmarks(struct file *, void *);
96 static int unp_gc_checkmarks(struct file *, void *);
97 static int unp_gc_checkrefs(struct file *, void *);
98 static int unp_revoke_gc_check(struct file *, void *);
99 static void unp_scan (struct mbuf *, void (*)(struct file *, void *),
101 static void unp_mark (struct file *, void *data);
102 static void unp_discard (struct file *, void *);
103 static int unp_internalize (struct mbuf *, struct thread *);
104 static int unp_listen (struct unpcb *, struct thread *);
105 static void unp_fp_externalize(struct lwp *lp, struct file *fp, int fd);
108 * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
109 * will sofree() it when we return.
112 uipc_abort(netmsg_t msg)
117 lwkt_gettoken(&unp_token);
118 unp = msg->base.nm_so->so_pcb;
120 unp_drop(unp, ECONNABORTED);
126 lwkt_reltoken(&unp_token);
128 lwkt_replymsg(&msg->lmsg, error);
132 uipc_accept(netmsg_t msg)
137 lwkt_gettoken(&unp_token);
138 unp = msg->base.nm_so->so_pcb;
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 *msg->accept.nm_nam = dup_sockaddr(
149 (struct sockaddr *)unp->unp_conn->unp_addr);
151 *msg->accept.nm_nam = dup_sockaddr(
152 (struct sockaddr *)&sun_noname);
156 lwkt_reltoken(&unp_token);
157 lwkt_replymsg(&msg->lmsg, error);
161 uipc_attach(netmsg_t msg)
166 lwkt_gettoken(&unp_token);
167 unp = msg->base.nm_so->so_pcb;
171 error = unp_attach(msg->base.nm_so, msg->attach.nm_ai);
172 lwkt_reltoken(&unp_token);
173 lwkt_replymsg(&msg->lmsg, error);
177 uipc_bind(netmsg_t msg)
182 lwkt_gettoken(&unp_token);
183 unp = msg->base.nm_so->so_pcb;
185 error = unp_bind(unp, msg->bind.nm_nam, msg->bind.nm_td);
188 lwkt_reltoken(&unp_token);
189 lwkt_replymsg(&msg->lmsg, error);
193 uipc_connect(netmsg_t msg)
198 lwkt_gettoken(&unp_token);
199 unp = msg->base.nm_so->so_pcb;
201 error = unp_connect(msg->base.nm_so,
207 lwkt_reltoken(&unp_token);
208 lwkt_replymsg(&msg->lmsg, error);
212 uipc_connect2(netmsg_t msg)
217 lwkt_gettoken(&unp_token);
218 unp = msg->connect2.nm_so1->so_pcb;
220 error = unp_connect2(msg->connect2.nm_so1,
221 msg->connect2.nm_so2);
225 lwkt_reltoken(&unp_token);
226 lwkt_replymsg(&msg->lmsg, error);
229 /* control is EOPNOTSUPP */
232 uipc_detach(netmsg_t msg)
237 lwkt_gettoken(&unp_token);
238 unp = msg->base.nm_so->so_pcb;
245 lwkt_reltoken(&unp_token);
246 lwkt_replymsg(&msg->lmsg, error);
250 uipc_disconnect(netmsg_t msg)
255 lwkt_gettoken(&unp_token);
256 unp = msg->base.nm_so->so_pcb;
263 lwkt_reltoken(&unp_token);
264 lwkt_replymsg(&msg->lmsg, error);
268 uipc_listen(netmsg_t msg)
273 lwkt_gettoken(&unp_token);
274 unp = msg->base.nm_so->so_pcb;
275 if (unp == NULL || unp->unp_vnode == NULL)
278 error = unp_listen(unp, msg->listen.nm_td);
279 lwkt_reltoken(&unp_token);
280 lwkt_replymsg(&msg->lmsg, error);
284 uipc_peeraddr(netmsg_t msg)
289 lwkt_gettoken(&unp_token);
290 unp = msg->base.nm_so->so_pcb;
293 } else if (unp->unp_conn && unp->unp_conn->unp_addr) {
294 *msg->peeraddr.nm_nam = dup_sockaddr(
295 (struct sockaddr *)unp->unp_conn->unp_addr);
299 * XXX: It seems that this test always fails even when
300 * connection is established. So, this else clause is
301 * added as workaround to return PF_LOCAL sockaddr.
303 *msg->peeraddr.nm_nam = dup_sockaddr(
304 (struct sockaddr *)&sun_noname);
307 lwkt_reltoken(&unp_token);
308 lwkt_replymsg(&msg->lmsg, error);
312 uipc_rcvd(netmsg_t msg)
319 lwkt_gettoken(&unp_token);
320 so = msg->base.nm_so;
327 switch (so->so_type) {
329 panic("uipc_rcvd DGRAM?");
333 if (unp->unp_conn == NULL)
336 * Because we are transfering mbufs directly to the
337 * peer socket we have to use SSB_STOP on the sender
338 * to prevent it from building up infinite mbufs.
340 so2 = unp->unp_conn->unp_socket;
341 if (so->so_rcv.ssb_cc < so2->so_snd.ssb_hiwat &&
342 so->so_rcv.ssb_mbcnt < so2->so_snd.ssb_mbmax
344 atomic_clear_int(&so2->so_snd.ssb_flags, SSB_STOP);
349 panic("uipc_rcvd unknown socktype");
354 lwkt_reltoken(&unp_token);
355 lwkt_replymsg(&msg->lmsg, error);
358 /* pru_rcvoob is EOPNOTSUPP */
361 uipc_send(netmsg_t msg)
366 struct mbuf *control;
370 lwkt_gettoken(&unp_token);
371 so = msg->base.nm_so;
372 control = msg->send.nm_control;
380 if (msg->send.nm_flags & PRUS_OOB) {
385 if (control && (error = unp_internalize(control, msg->send.nm_td)))
388 switch (so->so_type) {
391 struct sockaddr *from;
393 if (msg->send.nm_addr) {
398 error = unp_connect(so,
404 if (unp->unp_conn == NULL) {
409 so2 = unp->unp_conn->unp_socket;
411 from = (struct sockaddr *)unp->unp_addr;
415 lwkt_gettoken(&so2->so_rcv.ssb_token);
416 if (ssb_appendaddr(&so2->so_rcv, from, m, control)) {
423 if (msg->send.nm_addr)
425 lwkt_reltoken(&so2->so_rcv.ssb_token);
431 /* Connect if not connected yet. */
433 * Note: A better implementation would complain
434 * if not equal to the peer's address.
436 if (!(so->so_state & SS_ISCONNECTED)) {
437 if (msg->send.nm_addr) {
438 error = unp_connect(so,
449 if (so->so_state & SS_CANTSENDMORE) {
453 if (unp->unp_conn == NULL)
454 panic("uipc_send connected but no connection?");
455 so2 = unp->unp_conn->unp_socket;
457 * Send to paired receive port, and then reduce
458 * send buffer hiwater marks to maintain backpressure.
461 lwkt_gettoken(&so2->so_rcv.ssb_token);
463 if (ssb_appendcontrol(&so2->so_rcv, m, control)) {
467 } else if (so->so_type == SOCK_SEQPACKET) {
468 sbappendrecord(&so2->so_rcv.sb, m);
471 sbappend(&so2->so_rcv.sb, m);
476 * Because we are transfering mbufs directly to the
477 * peer socket we have to use SSB_STOP on the sender
478 * to prevent it from building up infinite mbufs.
480 if (so2->so_rcv.ssb_cc >= so->so_snd.ssb_hiwat ||
481 so2->so_rcv.ssb_mbcnt >= so->so_snd.ssb_mbmax
483 atomic_set_int(&so->so_snd.ssb_flags, SSB_STOP);
485 lwkt_reltoken(&so2->so_rcv.ssb_token);
490 panic("uipc_send unknown socktype");
494 * SEND_EOF is equivalent to a SEND followed by a SHUTDOWN.
496 if (msg->send.nm_flags & PRUS_EOF) {
501 if (control && error != 0)
502 unp_dispose(control);
505 lwkt_reltoken(&unp_token);
511 lwkt_replymsg(&msg->lmsg, error);
518 uipc_sense(netmsg_t msg)
525 lwkt_gettoken(&unp_token);
526 so = msg->base.nm_so;
527 sb = msg->sense.nm_stat;
533 sb->st_blksize = so->so_snd.ssb_hiwat;
535 if (unp->unp_ino == 0) { /* make up a non-zero inode number */
536 spin_lock(&unp_ino_spin);
537 unp->unp_ino = unp_ino++;
538 spin_unlock(&unp_ino_spin);
540 sb->st_ino = unp->unp_ino;
543 lwkt_reltoken(&unp_token);
544 lwkt_replymsg(&msg->lmsg, error);
548 uipc_shutdown(netmsg_t msg)
554 lwkt_gettoken(&unp_token);
555 so = msg->base.nm_so;
564 lwkt_reltoken(&unp_token);
565 lwkt_replymsg(&msg->lmsg, error);
569 uipc_sockaddr(netmsg_t msg)
574 lwkt_gettoken(&unp_token);
575 unp = msg->base.nm_so->so_pcb;
578 *msg->sockaddr.nm_nam =
579 dup_sockaddr((struct sockaddr *)unp->unp_addr);
585 lwkt_reltoken(&unp_token);
586 lwkt_replymsg(&msg->lmsg, error);
589 struct pr_usrreqs uipc_usrreqs = {
590 .pru_abort = uipc_abort,
591 .pru_accept = uipc_accept,
592 .pru_attach = uipc_attach,
593 .pru_bind = uipc_bind,
594 .pru_connect = uipc_connect,
595 .pru_connect2 = uipc_connect2,
596 .pru_control = pr_generic_notsupp,
597 .pru_detach = uipc_detach,
598 .pru_disconnect = uipc_disconnect,
599 .pru_listen = uipc_listen,
600 .pru_peeraddr = uipc_peeraddr,
601 .pru_rcvd = uipc_rcvd,
602 .pru_rcvoob = pr_generic_notsupp,
603 .pru_send = uipc_send,
604 .pru_sense = uipc_sense,
605 .pru_shutdown = uipc_shutdown,
606 .pru_sockaddr = uipc_sockaddr,
607 .pru_sosend = sosend,
608 .pru_soreceive = soreceive
612 uipc_ctloutput(netmsg_t msg)
615 struct sockopt *sopt;
619 lwkt_gettoken(&unp_token);
620 so = msg->base.nm_so;
621 sopt = msg->ctloutput.nm_sopt;
624 switch (sopt->sopt_dir) {
626 switch (sopt->sopt_name) {
628 if (unp->unp_flags & UNP_HAVEPC)
629 soopt_from_kbuf(sopt, &unp->unp_peercred,
630 sizeof(unp->unp_peercred));
632 if (so->so_type == SOCK_STREAM)
634 else if (so->so_type == SOCK_SEQPACKET)
650 lwkt_reltoken(&unp_token);
651 lwkt_replymsg(&msg->lmsg, error);
655 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
656 * for stream sockets, although the total for sender and receiver is
657 * actually only PIPSIZ.
659 * Datagram sockets really use the sendspace as the maximum datagram size,
660 * and don't really want to reserve the sendspace. Their recvspace should
661 * be large enough for at least one max-size datagram plus address.
663 * We want the local send/recv space to be significant larger then lo0's
669 static u_long unpst_sendspace = PIPSIZ;
670 static u_long unpst_recvspace = PIPSIZ;
671 static u_long unpdg_sendspace = 2*1024; /* really max datagram size */
672 static u_long unpdg_recvspace = 4*1024;
674 static int unp_rights; /* file descriptors in flight */
675 static struct spinlock unp_spin = SPINLOCK_INITIALIZER(&unp_spin);
677 SYSCTL_DECL(_net_local_seqpacket);
678 SYSCTL_DECL(_net_local_stream);
679 SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
680 &unpst_sendspace, 0, "");
681 SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
682 &unpst_recvspace, 0, "");
684 SYSCTL_DECL(_net_local_dgram);
685 SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
686 &unpdg_sendspace, 0, "");
687 SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
688 &unpdg_recvspace, 0, "");
690 SYSCTL_DECL(_net_local);
691 SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, "");
694 unp_attach(struct socket *so, struct pru_attach_info *ai)
699 lwkt_gettoken(&unp_token);
701 if (so->so_snd.ssb_hiwat == 0 || so->so_rcv.ssb_hiwat == 0) {
702 switch (so->so_type) {
706 error = soreserve(so, unpst_sendspace, unpst_recvspace,
711 error = soreserve(so, unpdg_sendspace, unpdg_recvspace,
721 unp = kmalloc(sizeof(*unp), M_UNPCB, M_NOWAIT|M_ZERO);
726 unp->unp_gencnt = ++unp_gencnt;
728 LIST_INIT(&unp->unp_refs);
729 unp->unp_socket = so;
730 unp->unp_rvnode = ai->fd_rdir; /* jail cruft XXX JH */
731 LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead
732 : &unp_shead, unp, unp_link);
733 so->so_pcb = (caddr_t)unp;
737 lwkt_reltoken(&unp_token);
742 unp_detach(struct unpcb *unp)
746 lwkt_gettoken(&unp_token);
748 LIST_REMOVE(unp, unp_link);
749 unp->unp_gencnt = ++unp_gencnt;
751 if (unp->unp_vnode) {
752 unp->unp_vnode->v_socket = NULL;
753 vrele(unp->unp_vnode);
754 unp->unp_vnode = NULL;
758 while (!LIST_EMPTY(&unp->unp_refs))
759 unp_drop(LIST_FIRST(&unp->unp_refs), ECONNRESET);
760 soisdisconnected(unp->unp_socket);
761 so = unp->unp_socket;
762 soreference(so); /* for delayed sorflush */
764 unp->unp_socket = NULL;
765 sofree(so); /* remove pcb ref */
769 * Normally the receive buffer is flushed later,
770 * in sofree, but if our receive buffer holds references
771 * to descriptors that are now garbage, we will dispose
772 * of those descriptor references after the garbage collector
773 * gets them (resulting in a "panic: closef: count < 0").
779 lwkt_reltoken(&unp_token);
782 kfree(unp->unp_addr, M_SONAME);
787 unp_bind(struct unpcb *unp, struct sockaddr *nam, struct thread *td)
789 struct proc *p = td->td_proc;
790 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
794 struct nlookupdata nd;
795 char buf[SOCK_MAXADDRLEN];
797 lwkt_gettoken(&unp_token);
798 if (unp->unp_vnode != NULL) {
802 namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
807 strncpy(buf, soun->sun_path, namelen);
808 buf[namelen] = 0; /* null-terminate the string */
809 error = nlookup_init(&nd, buf, UIO_SYSSPACE,
810 NLC_LOCKVP | NLC_CREATE | NLC_REFDVP);
812 error = nlookup(&nd);
813 if (error == 0 && nd.nl_nch.ncp->nc_vp != NULL)
819 vattr.va_type = VSOCK;
820 vattr.va_mode = (ACCESSPERMS & ~p->p_fd->fd_cmask);
821 error = VOP_NCREATE(&nd.nl_nch, nd.nl_dvp, &vp, nd.nl_cred, &vattr);
823 vp->v_socket = unp->unp_socket;
825 unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam);
831 lwkt_reltoken(&unp_token);
836 unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
838 struct proc *p = td->td_proc;
839 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
841 struct socket *so2, *so3;
842 struct unpcb *unp, *unp2, *unp3;
844 struct nlookupdata nd;
845 char buf[SOCK_MAXADDRLEN];
847 lwkt_gettoken(&unp_token);
849 len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
854 strncpy(buf, soun->sun_path, len);
858 error = nlookup_init(&nd, buf, UIO_SYSSPACE, NLC_FOLLOW);
860 error = nlookup(&nd);
862 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
867 if (vp->v_type != VSOCK) {
871 error = VOP_ACCESS(vp, VWRITE, p->p_ucred);
876 error = ECONNREFUSED;
879 if (so->so_type != so2->so_type) {
883 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
884 if (!(so2->so_options & SO_ACCEPTCONN) ||
885 (so3 = sonewconn(so2, 0)) == NULL) {
886 error = ECONNREFUSED;
893 unp3->unp_addr = (struct sockaddr_un *)
894 dup_sockaddr((struct sockaddr *)unp2->unp_addr);
897 * unp_peercred management:
899 * The connecter's (client's) credentials are copied
900 * from its process structure at the time of connect()
903 cru2x(p->p_ucred, &unp3->unp_peercred);
904 unp3->unp_flags |= UNP_HAVEPC;
906 * The receiver's (server's) credentials are copied
907 * from the unp_peercred member of socket on which the
908 * former called listen(); unp_listen() cached that
909 * process's credentials at that time so we can use
912 KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED,
913 ("unp_connect: listener without cached peercred"));
914 memcpy(&unp->unp_peercred, &unp2->unp_peercred,
915 sizeof(unp->unp_peercred));
916 unp->unp_flags |= UNP_HAVEPC;
920 error = unp_connect2(so, so2);
924 lwkt_reltoken(&unp_token);
929 unp_connect2(struct socket *so, struct socket *so2)
934 lwkt_gettoken(&unp_token);
936 if (so2->so_type != so->so_type) {
937 lwkt_reltoken(&unp_token);
941 unp->unp_conn = unp2;
943 switch (so->so_type) {
945 LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
951 unp2->unp_conn = unp;
957 panic("unp_connect2");
959 lwkt_reltoken(&unp_token);
964 unp_disconnect(struct unpcb *unp)
968 lwkt_gettoken(&unp_token);
970 unp2 = unp->unp_conn;
972 lwkt_reltoken(&unp_token);
976 unp->unp_conn = NULL;
978 switch (unp->unp_socket->so_type) {
980 LIST_REMOVE(unp, unp_reflink);
981 soclrstate(unp->unp_socket, SS_ISCONNECTED);
985 soisdisconnected(unp->unp_socket);
986 unp2->unp_conn = NULL;
987 soisdisconnected(unp2->unp_socket);
990 lwkt_reltoken(&unp_token);
995 unp_abort(struct unpcb *unp)
997 lwkt_gettoken(&unp_token);
999 lwkt_reltoken(&unp_token);
1004 prison_unpcb(struct thread *td, struct unpcb *unp)
1010 if ((p = td->td_proc) == NULL)
1012 if (!p->p_ucred->cr_prison)
1014 if (p->p_fd->fd_rdir == unp->unp_rvnode)
1020 unp_pcblist(SYSCTL_HANDLER_ARGS)
1023 struct unpcb *unp, **unp_list;
1025 struct unp_head *head;
1027 head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead);
1029 KKASSERT(curproc != NULL);
1032 * The process of preparing the PCB list is too time-consuming and
1033 * resource-intensive to repeat twice on every request.
1035 if (req->oldptr == NULL) {
1037 req->oldidx = (n + n/8) * sizeof(struct xunpcb);
1041 if (req->newptr != NULL)
1044 lwkt_gettoken(&unp_token);
1047 * OK, now we're committed to doing something.
1049 gencnt = unp_gencnt;
1052 unp_list = kmalloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
1054 for (unp = LIST_FIRST(head), i = 0; unp && i < n;
1055 unp = LIST_NEXT(unp, unp_link)) {
1056 if (unp->unp_gencnt <= gencnt && !prison_unpcb(req->td, unp))
1057 unp_list[i++] = unp;
1059 n = i; /* in case we lost some during malloc */
1062 for (i = 0; i < n; i++) {
1064 if (unp->unp_gencnt <= gencnt) {
1066 xu.xu_len = sizeof xu;
1069 * XXX - need more locking here to protect against
1070 * connect/disconnect races for SMP.
1073 bcopy(unp->unp_addr, &xu.xu_addr,
1074 unp->unp_addr->sun_len);
1075 if (unp->unp_conn && unp->unp_conn->unp_addr)
1076 bcopy(unp->unp_conn->unp_addr,
1078 unp->unp_conn->unp_addr->sun_len);
1079 bcopy(unp, &xu.xu_unp, sizeof *unp);
1080 sotoxsocket(unp->unp_socket, &xu.xu_socket);
1081 error = SYSCTL_OUT(req, &xu, sizeof xu);
1084 lwkt_reltoken(&unp_token);
1085 kfree(unp_list, M_TEMP);
1090 SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD,
1091 (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
1092 "List of active local datagram sockets");
1093 SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD,
1094 (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
1095 "List of active local stream sockets");
1096 SYSCTL_PROC(_net_local_seqpacket, OID_AUTO, pcblist, CTLFLAG_RD,
1097 (caddr_t)(long)SOCK_SEQPACKET, 0, unp_pcblist, "S,xunpcb",
1098 "List of active local seqpacket stream sockets");
1101 unp_shutdown(struct unpcb *unp)
1105 if ((unp->unp_socket->so_type == SOCK_STREAM ||
1106 unp->unp_socket->so_type == SOCK_SEQPACKET) &&
1107 unp->unp_conn != NULL && (so = unp->unp_conn->unp_socket)) {
1113 unp_drop(struct unpcb *unp, int err)
1115 struct socket *so = unp->unp_socket;
1118 unp_disconnect(unp);
1125 lwkt_gettoken(&unp_token);
1126 lwkt_reltoken(&unp_token);
1131 unp_externalize(struct mbuf *rights)
1133 struct thread *td = curthread;
1134 struct proc *p = td->td_proc; /* XXX */
1135 struct lwp *lp = td->td_lwp;
1136 struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
1141 int newfds = (cm->cmsg_len - (CMSG_DATA(cm) - (u_char *)cm))
1142 / sizeof (struct file *);
1145 lwkt_gettoken(&unp_token);
1148 * if the new FD's will not fit, then we free them all
1150 if (!fdavail(p, newfds)) {
1151 rp = (struct file **)CMSG_DATA(cm);
1152 for (i = 0; i < newfds; i++) {
1155 * zero the pointer before calling unp_discard,
1156 * since it may end up in unp_gc()..
1159 unp_discard(fp, NULL);
1161 lwkt_reltoken(&unp_token);
1166 * now change each pointer to an fd in the global table to
1167 * an integer that is the index to the local fd table entry
1168 * that we set up to point to the global one we are transferring.
1169 * If sizeof (struct file *) is bigger than or equal to sizeof int,
1170 * then do it in forward order. In that case, an integer will
1171 * always come in the same place or before its corresponding
1172 * struct file pointer.
1173 * If sizeof (struct file *) is smaller than sizeof int, then
1174 * do it in reverse order.
1176 if (sizeof (struct file *) >= sizeof (int)) {
1177 fdp = (int *)CMSG_DATA(cm);
1178 rp = (struct file **)CMSG_DATA(cm);
1179 for (i = 0; i < newfds; i++) {
1180 if (fdalloc(p, 0, &f))
1181 panic("unp_externalize");
1183 unp_fp_externalize(lp, fp, f);
1187 fdp = (int *)CMSG_DATA(cm) + newfds - 1;
1188 rp = (struct file **)CMSG_DATA(cm) + newfds - 1;
1189 for (i = 0; i < newfds; i++) {
1190 if (fdalloc(p, 0, &f))
1191 panic("unp_externalize");
1193 unp_fp_externalize(lp, fp, f);
1199 * Adjust length, in case sizeof(struct file *) and sizeof(int)
1202 cm->cmsg_len = CMSG_LEN(newfds * sizeof(int));
1203 rights->m_len = cm->cmsg_len;
1205 lwkt_reltoken(&unp_token);
1210 unp_fp_externalize(struct lwp *lp, struct file *fp, int fd)
1215 lwkt_gettoken(&unp_token);
1219 if (fp->f_flag & FREVOKED) {
1220 kprintf("Warning: revoked fp exiting unix socket\n");
1222 error = falloc(lp, &fx, NULL);
1224 fsetfd(lp->lwp_proc->p_fd, fx, fd);
1226 fsetfd(lp->lwp_proc->p_fd, NULL, fd);
1229 fsetfd(lp->lwp_proc->p_fd, fp, fd);
1232 spin_lock(&unp_spin);
1235 spin_unlock(&unp_spin);
1238 lwkt_reltoken(&unp_token);
1245 LIST_INIT(&unp_dhead);
1246 LIST_INIT(&unp_shead);
1247 spin_init(&unp_spin);
1251 unp_internalize(struct mbuf *control, struct thread *td)
1253 struct proc *p = td->td_proc;
1254 struct filedesc *fdescp;
1255 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1259 struct cmsgcred *cmcred;
1265 lwkt_gettoken(&unp_token);
1268 if ((cm->cmsg_type != SCM_RIGHTS && cm->cmsg_type != SCM_CREDS) ||
1269 cm->cmsg_level != SOL_SOCKET ||
1270 CMSG_ALIGN(cm->cmsg_len) != control->m_len) {
1276 * Fill in credential information.
1278 if (cm->cmsg_type == SCM_CREDS) {
1279 cmcred = (struct cmsgcred *)CMSG_DATA(cm);
1280 cmcred->cmcred_pid = p->p_pid;
1281 cmcred->cmcred_uid = p->p_ucred->cr_ruid;
1282 cmcred->cmcred_gid = p->p_ucred->cr_rgid;
1283 cmcred->cmcred_euid = p->p_ucred->cr_uid;
1284 cmcred->cmcred_ngroups = MIN(p->p_ucred->cr_ngroups,
1286 for (i = 0; i < cmcred->cmcred_ngroups; i++)
1287 cmcred->cmcred_groups[i] = p->p_ucred->cr_groups[i];
1293 * cmsghdr may not be aligned, do not allow calculation(s) to
1296 if (cm->cmsg_len < CMSG_LEN(0)) {
1301 oldfds = (cm->cmsg_len - CMSG_LEN(0)) / sizeof (int);
1304 * check that all the FDs passed in refer to legal OPEN files
1305 * If not, reject the entire operation.
1307 fdp = (int *)CMSG_DATA(cm);
1308 for (i = 0; i < oldfds; i++) {
1310 if ((unsigned)fd >= fdescp->fd_nfiles ||
1311 fdescp->fd_files[fd].fp == NULL) {
1315 if (fdescp->fd_files[fd].fp->f_type == DTYPE_KQUEUE) {
1321 * Now replace the integer FDs with pointers to
1322 * the associated global file table entry..
1323 * Allocate a bigger buffer as necessary. But if an cluster is not
1324 * enough, return E2BIG.
1326 newlen = CMSG_LEN(oldfds * sizeof(struct file *));
1327 if (newlen > MCLBYTES) {
1331 if (newlen - control->m_len > M_TRAILINGSPACE(control)) {
1332 if (control->m_flags & M_EXT) {
1336 MCLGET(control, MB_WAIT);
1337 if (!(control->m_flags & M_EXT)) {
1342 /* copy the data to the cluster */
1343 memcpy(mtod(control, char *), cm, cm->cmsg_len);
1344 cm = mtod(control, struct cmsghdr *);
1348 * Adjust length, in case sizeof(struct file *) and sizeof(int)
1351 cm->cmsg_len = newlen;
1352 control->m_len = CMSG_ALIGN(newlen);
1355 * Transform the file descriptors into struct file pointers.
1356 * If sizeof (struct file *) is bigger than or equal to sizeof int,
1357 * then do it in reverse order so that the int won't get until
1359 * If sizeof (struct file *) is smaller than sizeof int, then
1360 * do it in forward order.
1362 if (sizeof (struct file *) >= sizeof (int)) {
1363 fdp = (int *)CMSG_DATA(cm) + oldfds - 1;
1364 rp = (struct file **)CMSG_DATA(cm) + oldfds - 1;
1365 for (i = 0; i < oldfds; i++) {
1366 fp = fdescp->fd_files[*fdp--].fp;
1369 spin_lock(&unp_spin);
1372 spin_unlock(&unp_spin);
1375 fdp = (int *)CMSG_DATA(cm);
1376 rp = (struct file **)CMSG_DATA(cm);
1377 for (i = 0; i < oldfds; i++) {
1378 fp = fdescp->fd_files[*fdp++].fp;
1381 spin_lock(&unp_spin);
1384 spin_unlock(&unp_spin);
1389 lwkt_reltoken(&unp_token);
1394 * Garbage collect in-transit file descriptors that get lost due to
1395 * loops (i.e. when a socket is sent to another process over itself,
1396 * and more complex situations).
1398 * NOT MPSAFE - TODO socket flush code and maybe closef. Rest is MPSAFE.
1401 struct unp_gc_info {
1402 struct file **extra_ref;
1403 struct file *locked_fp;
1412 struct unp_gc_info info;
1413 static boolean_t unp_gcing;
1417 spin_lock(&unp_spin);
1419 spin_unlock(&unp_spin);
1423 spin_unlock(&unp_spin);
1425 lwkt_gettoken(&unp_token);
1428 * before going through all this, set all FDs to
1429 * be NOT defered and NOT externally accessible
1432 allfiles_scan_exclusive(unp_gc_clearmarks, NULL);
1434 allfiles_scan_exclusive(unp_gc_checkmarks, &info);
1435 } while (info.defer);
1438 * We grab an extra reference to each of the file table entries
1439 * that are not otherwise accessible and then free the rights
1440 * that are stored in messages on them.
1442 * The bug in the orginal code is a little tricky, so I'll describe
1443 * what's wrong with it here.
1445 * It is incorrect to simply unp_discard each entry for f_msgcount
1446 * times -- consider the case of sockets A and B that contain
1447 * references to each other. On a last close of some other socket,
1448 * we trigger a gc since the number of outstanding rights (unp_rights)
1449 * is non-zero. If during the sweep phase the gc code un_discards,
1450 * we end up doing a (full) closef on the descriptor. A closef on A
1451 * results in the following chain. Closef calls soo_close, which
1452 * calls soclose. Soclose calls first (through the switch
1453 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply
1454 * returns because the previous instance had set unp_gcing, and
1455 * we return all the way back to soclose, which marks the socket
1456 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush
1457 * to free up the rights that are queued in messages on the socket A,
1458 * i.e., the reference on B. The sorflush calls via the dom_dispose
1459 * switch unp_dispose, which unp_scans with unp_discard. This second
1460 * instance of unp_discard just calls closef on B.
1462 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1463 * which results in another closef on A. Unfortunately, A is already
1464 * being closed, and the descriptor has already been marked with
1465 * SS_NOFDREF, and soclose panics at this point.
1467 * Here, we first take an extra reference to each inaccessible
1468 * descriptor. Then, we call sorflush ourself, since we know
1469 * it is a Unix domain socket anyhow. After we destroy all the
1470 * rights carried in messages, we do a last closef to get rid
1471 * of our extra reference. This is the last close, and the
1472 * unp_detach etc will shut down the socket.
1474 * 91/09/19, bsy@cs.cmu.edu
1476 info.extra_ref = kmalloc(256 * sizeof(struct file *), M_FILE, M_WAITOK);
1477 info.maxindex = 256;
1484 allfiles_scan_exclusive(unp_gc_checkrefs, &info);
1487 * For each FD on our hit list, do the following two things
1489 for (i = info.index, fpp = info.extra_ref; --i >= 0; ++fpp) {
1490 struct file *tfp = *fpp;
1491 if (tfp->f_type == DTYPE_SOCKET && tfp->f_data != NULL)
1492 sorflush((struct socket *)(tfp->f_data));
1494 for (i = info.index, fpp = info.extra_ref; --i >= 0; ++fpp)
1496 } while (info.index == info.maxindex);
1498 lwkt_reltoken(&unp_token);
1500 kfree((caddr_t)info.extra_ref, M_FILE);
1505 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1508 unp_gc_checkrefs(struct file *fp, void *data)
1510 struct unp_gc_info *info = data;
1512 if (fp->f_count == 0)
1514 if (info->index == info->maxindex)
1518 * If all refs are from msgs, and it's not marked accessible
1519 * then it must be referenced from some unreachable cycle
1520 * of (shut-down) FDs, so include it in our
1521 * list of FDs to remove
1523 if (fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) {
1524 info->extra_ref[info->index++] = fp;
1531 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1534 unp_gc_clearmarks(struct file *fp, void *data __unused)
1536 atomic_clear_int(&fp->f_flag, FMARK | FDEFER);
1541 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1544 unp_gc_checkmarks(struct file *fp, void *data)
1546 struct unp_gc_info *info = data;
1550 * If the file is not open, skip it
1552 if (fp->f_count == 0)
1555 * If we already marked it as 'defer' in a
1556 * previous pass, then try process it this time
1559 if (fp->f_flag & FDEFER) {
1560 atomic_clear_int(&fp->f_flag, FDEFER);
1564 * if it's not defered, then check if it's
1565 * already marked.. if so skip it
1567 if (fp->f_flag & FMARK)
1570 * If all references are from messages
1571 * in transit, then skip it. it's not
1572 * externally accessible.
1574 if (fp->f_count == fp->f_msgcount)
1577 * If it got this far then it must be
1578 * externally accessible.
1580 atomic_set_int(&fp->f_flag, FMARK);
1584 * either it was defered, or it is externally
1585 * accessible and not already marked so.
1586 * Now check if it is possibly one of OUR sockets.
1588 if (fp->f_type != DTYPE_SOCKET ||
1589 (so = (struct socket *)fp->f_data) == NULL)
1591 if (so->so_proto->pr_domain != &localdomain ||
1592 !(so->so_proto->pr_flags & PR_RIGHTS))
1595 if (so->so_rcv.ssb_flags & SSB_LOCK) {
1597 * This is problematical; it's not clear
1598 * we need to wait for the sockbuf to be
1599 * unlocked (on a uniprocessor, at least),
1600 * and it's also not clear what to do
1601 * if sbwait returns an error due to receipt
1602 * of a signal. If sbwait does return
1603 * an error, we'll go into an infinite
1604 * loop. Delete all of this for now.
1606 sbwait(&so->so_rcv);
1611 * So, Ok, it's one of our sockets and it IS externally
1612 * accessible (or was defered). Now we look
1613 * to see if we hold any file descriptors in its
1614 * message buffers. Follow those links and mark them
1615 * as accessible too.
1617 info->locked_fp = fp;
1618 lwkt_gettoken(&so->so_rcv.ssb_token);
1619 /* spin_lock_wr(&so->so_rcv.sb_spin); */
1620 unp_scan(so->so_rcv.ssb_mb, unp_mark, info);
1621 /* spin_unlock_wr(&so->so_rcv.sb_spin);*/
1622 lwkt_reltoken(&so->so_rcv.ssb_token);
1627 * Scan all unix domain sockets and replace any revoked file pointers
1628 * found with the dummy file pointer fx. We don't worry about races
1629 * against file pointers being read out as those are handled in the
1633 #define REVOKE_GC_MAXFILES 32
1635 struct unp_revoke_gc_info {
1637 struct file *fary[REVOKE_GC_MAXFILES];
1642 unp_revoke_gc(struct file *fx)
1644 struct unp_revoke_gc_info info;
1647 lwkt_gettoken(&unp_token);
1651 allfiles_scan_exclusive(unp_revoke_gc_check, &info);
1652 for (i = 0; i < info.fcount; ++i)
1653 unp_fp_externalize(NULL, info.fary[i], -1);
1654 } while (info.fcount == REVOKE_GC_MAXFILES);
1655 lwkt_reltoken(&unp_token);
1659 * Check for and replace revoked descriptors.
1661 * WARNING: This routine is not allowed to block.
1664 unp_revoke_gc_check(struct file *fps, void *vinfo)
1666 struct unp_revoke_gc_info *info = vinfo;
1677 * Is this a unix domain socket with rights-passing abilities?
1679 if (fps->f_type != DTYPE_SOCKET)
1681 if ((so = (struct socket *)fps->f_data) == NULL)
1683 if (so->so_proto->pr_domain != &localdomain)
1685 if ((so->so_proto->pr_flags & PR_RIGHTS) == 0)
1689 * Scan the mbufs for control messages and replace any revoked
1690 * descriptors we find.
1692 lwkt_gettoken(&so->so_rcv.ssb_token);
1693 m0 = so->so_rcv.ssb_mb;
1695 for (m = m0; m; m = m->m_next) {
1696 if (m->m_type != MT_CONTROL)
1698 if (m->m_len < sizeof(*cm))
1700 cm = mtod(m, struct cmsghdr *);
1701 if (cm->cmsg_level != SOL_SOCKET ||
1702 cm->cmsg_type != SCM_RIGHTS) {
1705 qfds = (cm->cmsg_len - CMSG_LEN(0)) / sizeof(void *);
1706 rp = (struct file **)CMSG_DATA(cm);
1707 for (i = 0; i < qfds; i++) {
1709 if (fp->f_flag & FREVOKED) {
1710 kprintf("Warning: Removing revoked fp from unix domain socket queue\n");
1712 info->fx->f_msgcount++;
1715 info->fary[info->fcount++] = fp;
1717 if (info->fcount == REVOKE_GC_MAXFILES)
1720 if (info->fcount == REVOKE_GC_MAXFILES)
1724 if (info->fcount == REVOKE_GC_MAXFILES)
1727 lwkt_reltoken(&so->so_rcv.ssb_token);
1730 * Stop the scan if we filled up our array.
1732 if (info->fcount == REVOKE_GC_MAXFILES)
1738 unp_dispose(struct mbuf *m)
1740 lwkt_gettoken(&unp_token);
1742 unp_scan(m, unp_discard, NULL);
1743 lwkt_reltoken(&unp_token);
1747 unp_listen(struct unpcb *unp, struct thread *td)
1749 struct proc *p = td->td_proc;
1752 lwkt_gettoken(&unp_token);
1753 cru2x(p->p_ucred, &unp->unp_peercred);
1754 unp->unp_flags |= UNP_HAVEPCCACHED;
1755 lwkt_reltoken(&unp_token);
1760 unp_scan(struct mbuf *m0, void (*op)(struct file *, void *), void *data)
1769 for (m = m0; m; m = m->m_next) {
1770 if (m->m_type == MT_CONTROL &&
1771 m->m_len >= sizeof(*cm)) {
1772 cm = mtod(m, struct cmsghdr *);
1773 if (cm->cmsg_level != SOL_SOCKET ||
1774 cm->cmsg_type != SCM_RIGHTS)
1776 qfds = (cm->cmsg_len - CMSG_LEN(0)) /
1778 rp = (struct file **)CMSG_DATA(cm);
1779 for (i = 0; i < qfds; i++)
1781 break; /* XXX, but saves time */
1789 unp_mark(struct file *fp, void *data)
1791 struct unp_gc_info *info = data;
1793 if ((fp->f_flag & FMARK) == 0) {
1795 atomic_set_int(&fp->f_flag, FMARK | FDEFER);
1800 unp_discard(struct file *fp, void *data __unused)
1802 spin_lock(&unp_spin);
1805 spin_unlock(&unp_spin);