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 typedef struct unp_defdiscard {
66 struct unp_defdiscard *next;
70 static MALLOC_DEFINE(M_UNPCB, "unpcb", "unpcb struct");
71 static unp_gen_t unp_gencnt;
72 static u_int unp_count;
74 static struct unp_head unp_shead, unp_dhead;
76 static struct lwkt_token unp_token = LWKT_TOKEN_INITIALIZER(unp_token);
77 static int unp_defdiscard_nest;
78 static unp_defdiscard_t unp_defdiscard_base;
81 * Unix communications domain.
85 * rethink name space problems
86 * need a proper out-of-band
89 static struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL };
90 static ino_t unp_ino = 1; /* prototype for fake inode numbers */
91 static struct spinlock unp_ino_spin = SPINLOCK_INITIALIZER(&unp_ino_spin);
93 static int unp_attach (struct socket *, struct pru_attach_info *);
94 static void unp_detach (struct unpcb *);
95 static int unp_bind (struct unpcb *,struct sockaddr *, struct thread *);
96 static int unp_connect (struct socket *,struct sockaddr *,
98 static void unp_disconnect (struct unpcb *);
99 static void unp_shutdown (struct unpcb *);
100 static void unp_drop (struct unpcb *, int);
101 static void unp_gc (void);
102 static int unp_gc_clearmarks(struct file *, void *);
103 static int unp_gc_checkmarks(struct file *, void *);
104 static int unp_gc_checkrefs(struct file *, void *);
105 static int unp_revoke_gc_check(struct file *, void *);
106 static void unp_scan (struct mbuf *, void (*)(struct file *, void *),
108 static void unp_mark (struct file *, void *data);
109 static void unp_discard (struct file *, void *);
110 static int unp_internalize (struct mbuf *, struct thread *);
111 static int unp_listen (struct unpcb *, struct thread *);
112 static void unp_fp_externalize(struct lwp *lp, struct file *fp, int fd);
115 * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
116 * will sofree() it when we return.
119 uipc_abort(netmsg_t msg)
124 lwkt_gettoken(&unp_token);
125 unp = msg->base.nm_so->so_pcb;
127 unp_drop(unp, ECONNABORTED);
133 lwkt_reltoken(&unp_token);
135 lwkt_replymsg(&msg->lmsg, error);
139 uipc_accept(netmsg_t msg)
144 lwkt_gettoken(&unp_token);
145 unp = msg->base.nm_so->so_pcb;
150 * Pass back name of connected socket,
151 * if it was bound and we are still connected
152 * (our peer may have closed already!).
154 if (unp->unp_conn && unp->unp_conn->unp_addr) {
155 *msg->accept.nm_nam = dup_sockaddr(
156 (struct sockaddr *)unp->unp_conn->unp_addr);
158 *msg->accept.nm_nam = dup_sockaddr(
159 (struct sockaddr *)&sun_noname);
163 lwkt_reltoken(&unp_token);
164 lwkt_replymsg(&msg->lmsg, error);
168 uipc_attach(netmsg_t msg)
173 lwkt_gettoken(&unp_token);
174 unp = msg->base.nm_so->so_pcb;
178 error = unp_attach(msg->base.nm_so, msg->attach.nm_ai);
179 lwkt_reltoken(&unp_token);
180 lwkt_replymsg(&msg->lmsg, error);
184 uipc_bind(netmsg_t msg)
189 lwkt_gettoken(&unp_token);
190 unp = msg->base.nm_so->so_pcb;
192 error = unp_bind(unp, msg->bind.nm_nam, msg->bind.nm_td);
195 lwkt_reltoken(&unp_token);
196 lwkt_replymsg(&msg->lmsg, error);
200 uipc_connect(netmsg_t msg)
205 lwkt_gettoken(&unp_token);
206 unp = msg->base.nm_so->so_pcb;
208 error = unp_connect(msg->base.nm_so,
214 lwkt_reltoken(&unp_token);
215 lwkt_replymsg(&msg->lmsg, error);
219 uipc_connect2(netmsg_t msg)
224 lwkt_gettoken(&unp_token);
225 unp = msg->connect2.nm_so1->so_pcb;
227 error = unp_connect2(msg->connect2.nm_so1,
228 msg->connect2.nm_so2);
232 lwkt_reltoken(&unp_token);
233 lwkt_replymsg(&msg->lmsg, error);
236 /* control is EOPNOTSUPP */
239 uipc_detach(netmsg_t msg)
244 lwkt_gettoken(&unp_token);
245 unp = msg->base.nm_so->so_pcb;
252 lwkt_reltoken(&unp_token);
253 lwkt_replymsg(&msg->lmsg, error);
257 uipc_disconnect(netmsg_t msg)
262 lwkt_gettoken(&unp_token);
263 unp = msg->base.nm_so->so_pcb;
270 lwkt_reltoken(&unp_token);
271 lwkt_replymsg(&msg->lmsg, error);
275 uipc_listen(netmsg_t msg)
280 lwkt_gettoken(&unp_token);
281 unp = msg->base.nm_so->so_pcb;
282 if (unp == NULL || unp->unp_vnode == NULL)
285 error = unp_listen(unp, msg->listen.nm_td);
286 lwkt_reltoken(&unp_token);
287 lwkt_replymsg(&msg->lmsg, error);
291 uipc_peeraddr(netmsg_t msg)
296 lwkt_gettoken(&unp_token);
297 unp = msg->base.nm_so->so_pcb;
300 } else if (unp->unp_conn && unp->unp_conn->unp_addr) {
301 *msg->peeraddr.nm_nam = dup_sockaddr(
302 (struct sockaddr *)unp->unp_conn->unp_addr);
306 * XXX: It seems that this test always fails even when
307 * connection is established. So, this else clause is
308 * added as workaround to return PF_LOCAL sockaddr.
310 *msg->peeraddr.nm_nam = dup_sockaddr(
311 (struct sockaddr *)&sun_noname);
314 lwkt_reltoken(&unp_token);
315 lwkt_replymsg(&msg->lmsg, error);
319 uipc_rcvd(netmsg_t msg)
326 lwkt_gettoken(&unp_token);
327 so = msg->base.nm_so;
334 switch (so->so_type) {
336 panic("uipc_rcvd DGRAM?");
340 if (unp->unp_conn == NULL)
343 * Because we are transfering mbufs directly to the
344 * peer socket we have to use SSB_STOP on the sender
345 * to prevent it from building up infinite mbufs.
347 so2 = unp->unp_conn->unp_socket;
348 if (so->so_rcv.ssb_cc < so2->so_snd.ssb_hiwat &&
349 so->so_rcv.ssb_mbcnt < so2->so_snd.ssb_mbmax
351 atomic_clear_int(&so2->so_snd.ssb_flags, SSB_STOP);
356 panic("uipc_rcvd unknown socktype");
361 lwkt_reltoken(&unp_token);
362 lwkt_replymsg(&msg->lmsg, error);
365 /* pru_rcvoob is EOPNOTSUPP */
368 uipc_send(netmsg_t msg)
373 struct mbuf *control;
377 lwkt_gettoken(&unp_token);
378 so = msg->base.nm_so;
379 control = msg->send.nm_control;
387 if (msg->send.nm_flags & PRUS_OOB) {
392 if (control && (error = unp_internalize(control, msg->send.nm_td)))
395 switch (so->so_type) {
398 struct sockaddr *from;
400 if (msg->send.nm_addr) {
405 error = unp_connect(so,
411 if (unp->unp_conn == NULL) {
416 so2 = unp->unp_conn->unp_socket;
418 from = (struct sockaddr *)unp->unp_addr;
422 lwkt_gettoken(&so2->so_rcv.ssb_token);
423 if (ssb_appendaddr(&so2->so_rcv, from, m, control)) {
430 if (msg->send.nm_addr)
432 lwkt_reltoken(&so2->so_rcv.ssb_token);
438 /* Connect if not connected yet. */
440 * Note: A better implementation would complain
441 * if not equal to the peer's address.
443 if (!(so->so_state & SS_ISCONNECTED)) {
444 if (msg->send.nm_addr) {
445 error = unp_connect(so,
456 if (so->so_state & SS_CANTSENDMORE) {
460 if (unp->unp_conn == NULL)
461 panic("uipc_send connected but no connection?");
462 so2 = unp->unp_conn->unp_socket;
464 * Send to paired receive port, and then reduce
465 * send buffer hiwater marks to maintain backpressure.
468 lwkt_gettoken(&so2->so_rcv.ssb_token);
470 if (ssb_appendcontrol(&so2->so_rcv, m, control)) {
474 } else if (so->so_type == SOCK_SEQPACKET) {
475 sbappendrecord(&so2->so_rcv.sb, m);
478 sbappend(&so2->so_rcv.sb, m);
483 * Because we are transfering mbufs directly to the
484 * peer socket we have to use SSB_STOP on the sender
485 * to prevent it from building up infinite mbufs.
487 if (so2->so_rcv.ssb_cc >= so->so_snd.ssb_hiwat ||
488 so2->so_rcv.ssb_mbcnt >= so->so_snd.ssb_mbmax
490 atomic_set_int(&so->so_snd.ssb_flags, SSB_STOP);
492 lwkt_reltoken(&so2->so_rcv.ssb_token);
497 panic("uipc_send unknown socktype");
501 * SEND_EOF is equivalent to a SEND followed by a SHUTDOWN.
503 if (msg->send.nm_flags & PRUS_EOF) {
508 if (control && error != 0)
509 unp_dispose(control);
512 lwkt_reltoken(&unp_token);
518 lwkt_replymsg(&msg->lmsg, error);
525 uipc_sense(netmsg_t msg)
532 lwkt_gettoken(&unp_token);
533 so = msg->base.nm_so;
534 sb = msg->sense.nm_stat;
540 sb->st_blksize = so->so_snd.ssb_hiwat;
542 if (unp->unp_ino == 0) { /* make up a non-zero inode number */
543 spin_lock(&unp_ino_spin);
544 unp->unp_ino = unp_ino++;
545 spin_unlock(&unp_ino_spin);
547 sb->st_ino = unp->unp_ino;
550 lwkt_reltoken(&unp_token);
551 lwkt_replymsg(&msg->lmsg, error);
555 uipc_shutdown(netmsg_t msg)
561 lwkt_gettoken(&unp_token);
562 so = msg->base.nm_so;
571 lwkt_reltoken(&unp_token);
572 lwkt_replymsg(&msg->lmsg, error);
576 uipc_sockaddr(netmsg_t msg)
581 lwkt_gettoken(&unp_token);
582 unp = msg->base.nm_so->so_pcb;
585 *msg->sockaddr.nm_nam =
586 dup_sockaddr((struct sockaddr *)unp->unp_addr);
592 lwkt_reltoken(&unp_token);
593 lwkt_replymsg(&msg->lmsg, error);
596 struct pr_usrreqs uipc_usrreqs = {
597 .pru_abort = uipc_abort,
598 .pru_accept = uipc_accept,
599 .pru_attach = uipc_attach,
600 .pru_bind = uipc_bind,
601 .pru_connect = uipc_connect,
602 .pru_connect2 = uipc_connect2,
603 .pru_control = pr_generic_notsupp,
604 .pru_detach = uipc_detach,
605 .pru_disconnect = uipc_disconnect,
606 .pru_listen = uipc_listen,
607 .pru_peeraddr = uipc_peeraddr,
608 .pru_rcvd = uipc_rcvd,
609 .pru_rcvoob = pr_generic_notsupp,
610 .pru_send = uipc_send,
611 .pru_sense = uipc_sense,
612 .pru_shutdown = uipc_shutdown,
613 .pru_sockaddr = uipc_sockaddr,
614 .pru_sosend = sosend,
615 .pru_soreceive = soreceive
619 uipc_ctloutput(netmsg_t msg)
622 struct sockopt *sopt;
626 lwkt_gettoken(&unp_token);
627 so = msg->base.nm_so;
628 sopt = msg->ctloutput.nm_sopt;
631 switch (sopt->sopt_dir) {
633 switch (sopt->sopt_name) {
635 if (unp->unp_flags & UNP_HAVEPC)
636 soopt_from_kbuf(sopt, &unp->unp_peercred,
637 sizeof(unp->unp_peercred));
639 if (so->so_type == SOCK_STREAM)
641 else if (so->so_type == SOCK_SEQPACKET)
657 lwkt_reltoken(&unp_token);
658 lwkt_replymsg(&msg->lmsg, error);
662 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
663 * for stream sockets, although the total for sender and receiver is
664 * actually only PIPSIZ.
666 * Datagram sockets really use the sendspace as the maximum datagram size,
667 * and don't really want to reserve the sendspace. Their recvspace should
668 * be large enough for at least one max-size datagram plus address.
670 * We want the local send/recv space to be significant larger then lo0's
676 static u_long unpst_sendspace = PIPSIZ;
677 static u_long unpst_recvspace = PIPSIZ;
678 static u_long unpdg_sendspace = 2*1024; /* really max datagram size */
679 static u_long unpdg_recvspace = 4*1024;
681 static int unp_rights; /* file descriptors in flight */
682 static struct spinlock unp_spin = SPINLOCK_INITIALIZER(&unp_spin);
684 SYSCTL_DECL(_net_local_seqpacket);
685 SYSCTL_DECL(_net_local_stream);
686 SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
687 &unpst_sendspace, 0, "Size of stream socket send buffer");
688 SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
689 &unpst_recvspace, 0, "Size of stream socket receive buffer");
691 SYSCTL_DECL(_net_local_dgram);
692 SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
693 &unpdg_sendspace, 0, "Max datagram socket size");
694 SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
695 &unpdg_recvspace, 0, "Size of datagram socket receive buffer");
697 SYSCTL_DECL(_net_local);
698 SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0,
699 "File descriptors in flight");
702 unp_attach(struct socket *so, struct pru_attach_info *ai)
707 lwkt_gettoken(&unp_token);
709 if (so->so_snd.ssb_hiwat == 0 || so->so_rcv.ssb_hiwat == 0) {
710 switch (so->so_type) {
714 error = soreserve(so, unpst_sendspace, unpst_recvspace,
719 error = soreserve(so, unpdg_sendspace, unpdg_recvspace,
729 unp = kmalloc(sizeof(*unp), M_UNPCB, M_WAITOK | M_ZERO | M_NULLOK);
734 unp->unp_gencnt = ++unp_gencnt;
736 LIST_INIT(&unp->unp_refs);
737 unp->unp_socket = so;
738 unp->unp_rvnode = ai->fd_rdir; /* jail cruft XXX JH */
739 LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead
740 : &unp_shead, unp, unp_link);
741 so->so_pcb = (caddr_t)unp;
745 lwkt_reltoken(&unp_token);
750 unp_detach(struct unpcb *unp)
754 lwkt_gettoken(&unp_token);
756 LIST_REMOVE(unp, unp_link);
757 unp->unp_gencnt = ++unp_gencnt;
759 if (unp->unp_vnode) {
760 unp->unp_vnode->v_socket = NULL;
761 vrele(unp->unp_vnode);
762 unp->unp_vnode = NULL;
766 while (!LIST_EMPTY(&unp->unp_refs))
767 unp_drop(LIST_FIRST(&unp->unp_refs), ECONNRESET);
768 soisdisconnected(unp->unp_socket);
769 so = unp->unp_socket;
770 soreference(so); /* for delayed sorflush */
772 unp->unp_socket = NULL;
773 sofree(so); /* remove pcb ref */
777 * Normally the receive buffer is flushed later,
778 * in sofree, but if our receive buffer holds references
779 * to descriptors that are now garbage, we will dispose
780 * of those descriptor references after the garbage collector
781 * gets them (resulting in a "panic: closef: count < 0").
787 lwkt_reltoken(&unp_token);
790 kfree(unp->unp_addr, M_SONAME);
795 unp_bind(struct unpcb *unp, struct sockaddr *nam, struct thread *td)
797 struct proc *p = td->td_proc;
798 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
802 struct nlookupdata nd;
803 char buf[SOCK_MAXADDRLEN];
805 lwkt_gettoken(&unp_token);
806 if (unp->unp_vnode != NULL) {
810 namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
815 strncpy(buf, soun->sun_path, namelen);
816 buf[namelen] = 0; /* null-terminate the string */
817 error = nlookup_init(&nd, buf, UIO_SYSSPACE,
818 NLC_LOCKVP | NLC_CREATE | NLC_REFDVP);
820 error = nlookup(&nd);
821 if (error == 0 && nd.nl_nch.ncp->nc_vp != NULL)
827 vattr.va_type = VSOCK;
828 vattr.va_mode = (ACCESSPERMS & ~p->p_fd->fd_cmask);
829 error = VOP_NCREATE(&nd.nl_nch, nd.nl_dvp, &vp, nd.nl_cred, &vattr);
831 vp->v_socket = unp->unp_socket;
833 unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam);
839 lwkt_reltoken(&unp_token);
844 unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
846 struct proc *p = td->td_proc;
847 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
849 struct socket *so2, *so3;
850 struct unpcb *unp, *unp2, *unp3;
852 struct nlookupdata nd;
853 char buf[SOCK_MAXADDRLEN];
855 lwkt_gettoken(&unp_token);
857 len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
862 strncpy(buf, soun->sun_path, len);
866 error = nlookup_init(&nd, buf, UIO_SYSSPACE, NLC_FOLLOW);
868 error = nlookup(&nd);
870 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
875 if (vp->v_type != VSOCK) {
879 error = VOP_EACCESS(vp, VWRITE, p->p_ucred);
884 error = ECONNREFUSED;
887 if (so->so_type != so2->so_type) {
891 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
892 if (!(so2->so_options & SO_ACCEPTCONN) ||
893 (so3 = sonewconn(so2, 0)) == NULL) {
894 error = ECONNREFUSED;
901 unp3->unp_addr = (struct sockaddr_un *)
902 dup_sockaddr((struct sockaddr *)unp2->unp_addr);
905 * unp_peercred management:
907 * The connecter's (client's) credentials are copied
908 * from its process structure at the time of connect()
911 cru2x(p->p_ucred, &unp3->unp_peercred);
912 unp3->unp_flags |= UNP_HAVEPC;
914 * The receiver's (server's) credentials are copied
915 * from the unp_peercred member of socket on which the
916 * former called listen(); unp_listen() cached that
917 * process's credentials at that time so we can use
920 KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED,
921 ("unp_connect: listener without cached peercred"));
922 memcpy(&unp->unp_peercred, &unp2->unp_peercred,
923 sizeof(unp->unp_peercred));
924 unp->unp_flags |= UNP_HAVEPC;
928 error = unp_connect2(so, so2);
932 lwkt_reltoken(&unp_token);
937 unp_connect2(struct socket *so, struct socket *so2)
942 lwkt_gettoken(&unp_token);
944 if (so2->so_type != so->so_type) {
945 lwkt_reltoken(&unp_token);
949 unp->unp_conn = unp2;
951 switch (so->so_type) {
953 LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
959 unp2->unp_conn = unp;
965 panic("unp_connect2");
967 lwkt_reltoken(&unp_token);
972 unp_disconnect(struct unpcb *unp)
976 lwkt_gettoken(&unp_token);
978 unp2 = unp->unp_conn;
980 lwkt_reltoken(&unp_token);
984 unp->unp_conn = NULL;
986 switch (unp->unp_socket->so_type) {
988 LIST_REMOVE(unp, unp_reflink);
989 soclrstate(unp->unp_socket, SS_ISCONNECTED);
993 soisdisconnected(unp->unp_socket);
994 unp2->unp_conn = NULL;
995 soisdisconnected(unp2->unp_socket);
998 lwkt_reltoken(&unp_token);
1003 unp_abort(struct unpcb *unp)
1005 lwkt_gettoken(&unp_token);
1007 lwkt_reltoken(&unp_token);
1012 prison_unpcb(struct thread *td, struct unpcb *unp)
1018 if ((p = td->td_proc) == NULL)
1020 if (!p->p_ucred->cr_prison)
1022 if (p->p_fd->fd_rdir == unp->unp_rvnode)
1028 unp_pcblist(SYSCTL_HANDLER_ARGS)
1031 struct unpcb *unp, **unp_list;
1033 struct unp_head *head;
1035 head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead);
1037 KKASSERT(curproc != NULL);
1040 * The process of preparing the PCB list is too time-consuming and
1041 * resource-intensive to repeat twice on every request.
1043 if (req->oldptr == NULL) {
1045 req->oldidx = (n + n/8) * sizeof(struct xunpcb);
1049 if (req->newptr != NULL)
1052 lwkt_gettoken(&unp_token);
1055 * OK, now we're committed to doing something.
1057 gencnt = unp_gencnt;
1060 unp_list = kmalloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
1062 for (unp = LIST_FIRST(head), i = 0; unp && i < n;
1063 unp = LIST_NEXT(unp, unp_link)) {
1064 if (unp->unp_gencnt <= gencnt && !prison_unpcb(req->td, unp))
1065 unp_list[i++] = unp;
1067 n = i; /* in case we lost some during malloc */
1070 for (i = 0; i < n; i++) {
1072 if (unp->unp_gencnt <= gencnt) {
1074 xu.xu_len = sizeof xu;
1077 * XXX - need more locking here to protect against
1078 * connect/disconnect races for SMP.
1081 bcopy(unp->unp_addr, &xu.xu_addr,
1082 unp->unp_addr->sun_len);
1083 if (unp->unp_conn && unp->unp_conn->unp_addr)
1084 bcopy(unp->unp_conn->unp_addr,
1086 unp->unp_conn->unp_addr->sun_len);
1087 bcopy(unp, &xu.xu_unp, sizeof *unp);
1088 sotoxsocket(unp->unp_socket, &xu.xu_socket);
1089 error = SYSCTL_OUT(req, &xu, sizeof xu);
1092 lwkt_reltoken(&unp_token);
1093 kfree(unp_list, M_TEMP);
1098 SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD,
1099 (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
1100 "List of active local datagram sockets");
1101 SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD,
1102 (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
1103 "List of active local stream sockets");
1104 SYSCTL_PROC(_net_local_seqpacket, OID_AUTO, pcblist, CTLFLAG_RD,
1105 (caddr_t)(long)SOCK_SEQPACKET, 0, unp_pcblist, "S,xunpcb",
1106 "List of active local seqpacket stream sockets");
1109 unp_shutdown(struct unpcb *unp)
1113 if ((unp->unp_socket->so_type == SOCK_STREAM ||
1114 unp->unp_socket->so_type == SOCK_SEQPACKET) &&
1115 unp->unp_conn != NULL && (so = unp->unp_conn->unp_socket)) {
1121 unp_drop(struct unpcb *unp, int err)
1123 struct socket *so = unp->unp_socket;
1126 unp_disconnect(unp);
1133 lwkt_gettoken(&unp_token);
1134 lwkt_reltoken(&unp_token);
1139 unp_externalize(struct mbuf *rights)
1141 struct thread *td = curthread;
1142 struct proc *p = td->td_proc; /* XXX */
1143 struct lwp *lp = td->td_lwp;
1144 struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
1149 int newfds = (cm->cmsg_len - (CMSG_DATA(cm) - (u_char *)cm))
1150 / sizeof (struct file *);
1153 lwkt_gettoken(&unp_token);
1156 * if the new FD's will not fit, then we free them all
1158 if (!fdavail(p, newfds)) {
1159 rp = (struct file **)CMSG_DATA(cm);
1160 for (i = 0; i < newfds; i++) {
1163 * zero the pointer before calling unp_discard,
1164 * since it may end up in unp_gc()..
1167 unp_discard(fp, NULL);
1169 lwkt_reltoken(&unp_token);
1174 * now change each pointer to an fd in the global table to
1175 * an integer that is the index to the local fd table entry
1176 * that we set up to point to the global one we are transferring.
1177 * If sizeof (struct file *) is bigger than or equal to sizeof int,
1178 * then do it in forward order. In that case, an integer will
1179 * always come in the same place or before its corresponding
1180 * struct file pointer.
1181 * If sizeof (struct file *) is smaller than sizeof int, then
1182 * do it in reverse order.
1184 if (sizeof (struct file *) >= sizeof (int)) {
1185 fdp = (int *)CMSG_DATA(cm);
1186 rp = (struct file **)CMSG_DATA(cm);
1187 for (i = 0; i < newfds; i++) {
1188 if (fdalloc(p, 0, &f))
1189 panic("unp_externalize");
1191 unp_fp_externalize(lp, fp, f);
1195 fdp = (int *)CMSG_DATA(cm) + newfds - 1;
1196 rp = (struct file **)CMSG_DATA(cm) + newfds - 1;
1197 for (i = 0; i < newfds; i++) {
1198 if (fdalloc(p, 0, &f))
1199 panic("unp_externalize");
1201 unp_fp_externalize(lp, fp, f);
1207 * Adjust length, in case sizeof(struct file *) and sizeof(int)
1210 cm->cmsg_len = CMSG_LEN(newfds * sizeof(int));
1211 rights->m_len = cm->cmsg_len;
1213 lwkt_reltoken(&unp_token);
1218 unp_fp_externalize(struct lwp *lp, struct file *fp, int fd)
1223 lwkt_gettoken(&unp_token);
1227 if (fp->f_flag & FREVOKED) {
1228 kprintf("Warning: revoked fp exiting unix socket\n");
1230 error = falloc(lp, &fx, NULL);
1232 fsetfd(lp->lwp_proc->p_fd, fx, fd);
1234 fsetfd(lp->lwp_proc->p_fd, NULL, fd);
1237 fsetfd(lp->lwp_proc->p_fd, fp, fd);
1240 spin_lock(&unp_spin);
1243 spin_unlock(&unp_spin);
1246 lwkt_reltoken(&unp_token);
1253 LIST_INIT(&unp_dhead);
1254 LIST_INIT(&unp_shead);
1255 spin_init(&unp_spin);
1259 unp_internalize(struct mbuf *control, struct thread *td)
1261 struct proc *p = td->td_proc;
1262 struct filedesc *fdescp;
1263 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1267 struct cmsgcred *cmcred;
1273 lwkt_gettoken(&unp_token);
1276 if ((cm->cmsg_type != SCM_RIGHTS && cm->cmsg_type != SCM_CREDS) ||
1277 cm->cmsg_level != SOL_SOCKET ||
1278 CMSG_ALIGN(cm->cmsg_len) != control->m_len) {
1284 * Fill in credential information.
1286 if (cm->cmsg_type == SCM_CREDS) {
1287 cmcred = (struct cmsgcred *)CMSG_DATA(cm);
1288 cmcred->cmcred_pid = p->p_pid;
1289 cmcred->cmcred_uid = p->p_ucred->cr_ruid;
1290 cmcred->cmcred_gid = p->p_ucred->cr_rgid;
1291 cmcred->cmcred_euid = p->p_ucred->cr_uid;
1292 cmcred->cmcred_ngroups = MIN(p->p_ucred->cr_ngroups,
1294 for (i = 0; i < cmcred->cmcred_ngroups; i++)
1295 cmcred->cmcred_groups[i] = p->p_ucred->cr_groups[i];
1301 * cmsghdr may not be aligned, do not allow calculation(s) to
1304 if (cm->cmsg_len < CMSG_LEN(0)) {
1309 oldfds = (cm->cmsg_len - CMSG_LEN(0)) / sizeof (int);
1312 * check that all the FDs passed in refer to legal OPEN files
1313 * If not, reject the entire operation.
1315 fdp = (int *)CMSG_DATA(cm);
1316 for (i = 0; i < oldfds; i++) {
1318 if ((unsigned)fd >= fdescp->fd_nfiles ||
1319 fdescp->fd_files[fd].fp == NULL) {
1323 if (fdescp->fd_files[fd].fp->f_type == DTYPE_KQUEUE) {
1329 * Now replace the integer FDs with pointers to
1330 * the associated global file table entry..
1331 * Allocate a bigger buffer as necessary. But if an cluster is not
1332 * enough, return E2BIG.
1334 newlen = CMSG_LEN(oldfds * sizeof(struct file *));
1335 if (newlen > MCLBYTES) {
1339 if (newlen - control->m_len > M_TRAILINGSPACE(control)) {
1340 if (control->m_flags & M_EXT) {
1344 MCLGET(control, MB_WAIT);
1345 if (!(control->m_flags & M_EXT)) {
1350 /* copy the data to the cluster */
1351 memcpy(mtod(control, char *), cm, cm->cmsg_len);
1352 cm = mtod(control, struct cmsghdr *);
1356 * Adjust length, in case sizeof(struct file *) and sizeof(int)
1359 cm->cmsg_len = newlen;
1360 control->m_len = CMSG_ALIGN(newlen);
1363 * Transform the file descriptors into struct file pointers.
1364 * If sizeof (struct file *) is bigger than or equal to sizeof int,
1365 * then do it in reverse order so that the int won't get until
1367 * If sizeof (struct file *) is smaller than sizeof int, then
1368 * do it in forward order.
1370 if (sizeof (struct file *) >= sizeof (int)) {
1371 fdp = (int *)CMSG_DATA(cm) + oldfds - 1;
1372 rp = (struct file **)CMSG_DATA(cm) + oldfds - 1;
1373 for (i = 0; i < oldfds; i++) {
1374 fp = fdescp->fd_files[*fdp--].fp;
1377 spin_lock(&unp_spin);
1380 spin_unlock(&unp_spin);
1383 fdp = (int *)CMSG_DATA(cm);
1384 rp = (struct file **)CMSG_DATA(cm);
1385 for (i = 0; i < oldfds; i++) {
1386 fp = fdescp->fd_files[*fdp++].fp;
1389 spin_lock(&unp_spin);
1392 spin_unlock(&unp_spin);
1397 lwkt_reltoken(&unp_token);
1402 * Garbage collect in-transit file descriptors that get lost due to
1403 * loops (i.e. when a socket is sent to another process over itself,
1404 * and more complex situations).
1406 * NOT MPSAFE - TODO socket flush code and maybe closef. Rest is MPSAFE.
1409 struct unp_gc_info {
1410 struct file **extra_ref;
1411 struct file *locked_fp;
1420 struct unp_gc_info info;
1421 static boolean_t unp_gcing;
1426 * Only one gc can be in-progress at any given moment
1428 spin_lock(&unp_spin);
1430 spin_unlock(&unp_spin);
1434 spin_unlock(&unp_spin);
1436 lwkt_gettoken(&unp_token);
1439 * Before going through all this, set all FDs to be NOT defered
1440 * and NOT externally accessible (not marked). During the scan
1441 * a fd can be marked externally accessible but we may or may not
1442 * be able to immediately process it (controlled by FDEFER).
1444 * If we loop sleep a bit. The complexity of the topology can cause
1445 * multiple loops. Also failure to acquire the socket's so_rcv
1446 * token can cause us to loop.
1448 allfiles_scan_exclusive(unp_gc_clearmarks, NULL);
1451 allfiles_scan_exclusive(unp_gc_checkmarks, &info);
1453 tsleep(&info, 0, "gcagain", 1);
1454 } while (info.defer);
1457 * We grab an extra reference to each of the file table entries
1458 * that are not otherwise accessible and then free the rights
1459 * that are stored in messages on them.
1461 * The bug in the orginal code is a little tricky, so I'll describe
1462 * what's wrong with it here.
1464 * It is incorrect to simply unp_discard each entry for f_msgcount
1465 * times -- consider the case of sockets A and B that contain
1466 * references to each other. On a last close of some other socket,
1467 * we trigger a gc since the number of outstanding rights (unp_rights)
1468 * is non-zero. If during the sweep phase the gc code un_discards,
1469 * we end up doing a (full) closef on the descriptor. A closef on A
1470 * results in the following chain. Closef calls soo_close, which
1471 * calls soclose. Soclose calls first (through the switch
1472 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply
1473 * returns because the previous instance had set unp_gcing, and
1474 * we return all the way back to soclose, which marks the socket
1475 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush
1476 * to free up the rights that are queued in messages on the socket A,
1477 * i.e., the reference on B. The sorflush calls via the dom_dispose
1478 * switch unp_dispose, which unp_scans with unp_discard. This second
1479 * instance of unp_discard just calls closef on B.
1481 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1482 * which results in another closef on A. Unfortunately, A is already
1483 * being closed, and the descriptor has already been marked with
1484 * SS_NOFDREF, and soclose panics at this point.
1486 * Here, we first take an extra reference to each inaccessible
1487 * descriptor. Then, we call sorflush ourself, since we know
1488 * it is a Unix domain socket anyhow. After we destroy all the
1489 * rights carried in messages, we do a last closef to get rid
1490 * of our extra reference. This is the last close, and the
1491 * unp_detach etc will shut down the socket.
1493 * 91/09/19, bsy@cs.cmu.edu
1495 info.extra_ref = kmalloc(256 * sizeof(struct file *), M_FILE, M_WAITOK);
1496 info.maxindex = 256;
1503 allfiles_scan_exclusive(unp_gc_checkrefs, &info);
1506 * For each FD on our hit list, do the following two things
1508 for (i = info.index, fpp = info.extra_ref; --i >= 0; ++fpp) {
1509 struct file *tfp = *fpp;
1510 if (tfp->f_type == DTYPE_SOCKET && tfp->f_data != NULL)
1511 sorflush((struct socket *)(tfp->f_data));
1513 for (i = info.index, fpp = info.extra_ref; --i >= 0; ++fpp)
1515 } while (info.index == info.maxindex);
1517 lwkt_reltoken(&unp_token);
1519 kfree((caddr_t)info.extra_ref, M_FILE);
1524 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1527 unp_gc_checkrefs(struct file *fp, void *data)
1529 struct unp_gc_info *info = data;
1531 if (fp->f_count == 0)
1533 if (info->index == info->maxindex)
1537 * If all refs are from msgs, and it's not marked accessible
1538 * then it must be referenced from some unreachable cycle
1539 * of (shut-down) FDs, so include it in our
1540 * list of FDs to remove
1542 if (fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) {
1543 info->extra_ref[info->index++] = fp;
1550 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1553 unp_gc_clearmarks(struct file *fp, void *data __unused)
1555 atomic_clear_int(&fp->f_flag, FMARK | FDEFER);
1560 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1563 unp_gc_checkmarks(struct file *fp, void *data)
1565 struct unp_gc_info *info = data;
1569 * If the file is not open, skip it. Make sure it isn't marked
1570 * defered or we could loop forever, in case we somehow race
1573 if (fp->f_count == 0) {
1574 if (fp->f_flag & FDEFER)
1575 atomic_clear_int(&fp->f_flag, FDEFER);
1579 * If we already marked it as 'defer' in a
1580 * previous pass, then try process it this time
1583 if (fp->f_flag & FDEFER) {
1584 atomic_clear_int(&fp->f_flag, FDEFER);
1587 * if it's not defered, then check if it's
1588 * already marked.. if so skip it
1590 if (fp->f_flag & FMARK)
1593 * If all references are from messages
1594 * in transit, then skip it. it's not
1595 * externally accessible.
1597 if (fp->f_count == fp->f_msgcount)
1600 * If it got this far then it must be
1601 * externally accessible.
1603 atomic_set_int(&fp->f_flag, FMARK);
1607 * either it was defered, or it is externally
1608 * accessible and not already marked so.
1609 * Now check if it is possibly one of OUR sockets.
1611 if (fp->f_type != DTYPE_SOCKET ||
1612 (so = (struct socket *)fp->f_data) == NULL) {
1615 if (so->so_proto->pr_domain != &localdomain ||
1616 !(so->so_proto->pr_flags & PR_RIGHTS)) {
1621 * So, Ok, it's one of our sockets and it IS externally accessible
1622 * (or was defered). Now we look to see if we hold any file
1623 * descriptors in its message buffers. Follow those links and mark
1624 * them as accessible too.
1626 * We are holding multiple spinlocks here, if we cannot get the
1627 * token non-blocking defer until the next loop.
1629 info->locked_fp = fp;
1630 if (lwkt_trytoken(&so->so_rcv.ssb_token)) {
1631 unp_scan(so->so_rcv.ssb_mb, unp_mark, info);
1632 lwkt_reltoken(&so->so_rcv.ssb_token);
1634 atomic_set_int(&fp->f_flag, FDEFER);
1641 * Scan all unix domain sockets and replace any revoked file pointers
1642 * found with the dummy file pointer fx. We don't worry about races
1643 * against file pointers being read out as those are handled in the
1647 #define REVOKE_GC_MAXFILES 32
1649 struct unp_revoke_gc_info {
1651 struct file *fary[REVOKE_GC_MAXFILES];
1656 unp_revoke_gc(struct file *fx)
1658 struct unp_revoke_gc_info info;
1661 lwkt_gettoken(&unp_token);
1665 allfiles_scan_exclusive(unp_revoke_gc_check, &info);
1666 for (i = 0; i < info.fcount; ++i)
1667 unp_fp_externalize(NULL, info.fary[i], -1);
1668 } while (info.fcount == REVOKE_GC_MAXFILES);
1669 lwkt_reltoken(&unp_token);
1673 * Check for and replace revoked descriptors.
1675 * WARNING: This routine is not allowed to block.
1678 unp_revoke_gc_check(struct file *fps, void *vinfo)
1680 struct unp_revoke_gc_info *info = vinfo;
1691 * Is this a unix domain socket with rights-passing abilities?
1693 if (fps->f_type != DTYPE_SOCKET)
1695 if ((so = (struct socket *)fps->f_data) == NULL)
1697 if (so->so_proto->pr_domain != &localdomain)
1699 if ((so->so_proto->pr_flags & PR_RIGHTS) == 0)
1703 * Scan the mbufs for control messages and replace any revoked
1704 * descriptors we find.
1706 lwkt_gettoken(&so->so_rcv.ssb_token);
1707 m0 = so->so_rcv.ssb_mb;
1709 for (m = m0; m; m = m->m_next) {
1710 if (m->m_type != MT_CONTROL)
1712 if (m->m_len < sizeof(*cm))
1714 cm = mtod(m, struct cmsghdr *);
1715 if (cm->cmsg_level != SOL_SOCKET ||
1716 cm->cmsg_type != SCM_RIGHTS) {
1719 qfds = (cm->cmsg_len - CMSG_LEN(0)) / sizeof(void *);
1720 rp = (struct file **)CMSG_DATA(cm);
1721 for (i = 0; i < qfds; i++) {
1723 if (fp->f_flag & FREVOKED) {
1724 kprintf("Warning: Removing revoked fp from unix domain socket queue\n");
1726 info->fx->f_msgcount++;
1729 info->fary[info->fcount++] = fp;
1731 if (info->fcount == REVOKE_GC_MAXFILES)
1734 if (info->fcount == REVOKE_GC_MAXFILES)
1738 if (info->fcount == REVOKE_GC_MAXFILES)
1741 lwkt_reltoken(&so->so_rcv.ssb_token);
1744 * Stop the scan if we filled up our array.
1746 if (info->fcount == REVOKE_GC_MAXFILES)
1752 * Dispose of the fp's stored in a mbuf.
1754 * The dds loop can cause additional fps to be entered onto the
1755 * list while it is running, flattening out the operation and avoiding
1756 * a deep kernel stack recursion.
1759 unp_dispose(struct mbuf *m)
1761 unp_defdiscard_t dds;
1763 lwkt_gettoken(&unp_token);
1764 ++unp_defdiscard_nest;
1766 unp_scan(m, unp_discard, NULL);
1768 if (unp_defdiscard_nest == 1) {
1769 while ((dds = unp_defdiscard_base) != NULL) {
1770 unp_defdiscard_base = dds->next;
1771 closef(dds->fp, NULL);
1772 kfree(dds, M_UNPCB);
1775 --unp_defdiscard_nest;
1776 lwkt_reltoken(&unp_token);
1780 unp_listen(struct unpcb *unp, struct thread *td)
1782 struct proc *p = td->td_proc;
1785 lwkt_gettoken(&unp_token);
1786 cru2x(p->p_ucred, &unp->unp_peercred);
1787 unp->unp_flags |= UNP_HAVEPCCACHED;
1788 lwkt_reltoken(&unp_token);
1793 unp_scan(struct mbuf *m0, void (*op)(struct file *, void *), void *data)
1802 for (m = m0; m; m = m->m_next) {
1803 if (m->m_type == MT_CONTROL &&
1804 m->m_len >= sizeof(*cm)) {
1805 cm = mtod(m, struct cmsghdr *);
1806 if (cm->cmsg_level != SOL_SOCKET ||
1807 cm->cmsg_type != SCM_RIGHTS)
1809 qfds = (cm->cmsg_len - CMSG_LEN(0)) /
1811 rp = (struct file **)CMSG_DATA(cm);
1812 for (i = 0; i < qfds; i++)
1814 break; /* XXX, but saves time */
1822 * Mark visibility. info->defer is recalculated on every pass.
1825 unp_mark(struct file *fp, void *data)
1827 struct unp_gc_info *info = data;
1829 if ((fp->f_flag & FMARK) == 0) {
1831 atomic_set_int(&fp->f_flag, FMARK | FDEFER);
1832 } else if (fp->f_flag & FDEFER) {
1838 * Discard a fp previously held in a unix domain socket mbuf. To
1839 * avoid blowing out the kernel stack due to contrived chain-reactions
1840 * we may have to defer the operation to a higher procedural level.
1842 * Caller holds unp_token
1845 unp_discard(struct file *fp, void *data __unused)
1847 unp_defdiscard_t dds;
1849 spin_lock(&unp_spin);
1852 spin_unlock(&unp_spin);
1854 if (unp_defdiscard_nest) {
1855 dds = kmalloc(sizeof(*dds), M_UNPCB, M_WAITOK|M_ZERO);
1857 dds->next = unp_defdiscard_base;
1858 unp_defdiscard_base = dds;