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.39 2008/05/09 17:52:17 dillon Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/domain.h>
42 #include <sys/fcntl.h>
43 #include <sys/malloc.h> /* XXX must be before <sys/file.h> */
46 #include <sys/filedesc.h>
48 #include <sys/nlookup.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/resourcevar.h>
54 #include <sys/mount.h>
55 #include <sys/sysctl.h>
57 #include <sys/unpcb.h>
58 #include <sys/vnode.h>
59 #include <sys/file2.h>
60 #include <sys/spinlock2.h>
62 #include <vm/vm_zone.h>
64 static struct vm_zone *unp_zone;
65 static unp_gen_t unp_gencnt;
66 static u_int unp_count;
68 static struct unp_head unp_shead, unp_dhead;
71 * Unix communications domain.
75 * rethink name space problems
76 * need a proper out-of-band
79 static struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL };
80 static ino_t unp_ino; /* prototype for fake inode numbers */
82 static int unp_attach (struct socket *, struct pru_attach_info *);
83 static void unp_detach (struct unpcb *);
84 static int unp_bind (struct unpcb *,struct sockaddr *, struct thread *);
85 static int unp_connect (struct socket *,struct sockaddr *,
87 static void unp_disconnect (struct unpcb *);
88 static void unp_shutdown (struct unpcb *);
89 static void unp_drop (struct unpcb *, int);
90 static void unp_gc (void);
91 static int unp_gc_clearmarks(struct file *, void *);
92 static int unp_gc_checkmarks(struct file *, void *);
93 static int unp_gc_checkrefs(struct file *, void *);
94 static void unp_scan (struct mbuf *, void (*)(struct file *, void *),
96 static void unp_mark (struct file *, void *data);
97 static void unp_discard (struct file *, void *);
98 static int unp_internalize (struct mbuf *, struct thread *);
99 static int unp_listen (struct unpcb *, struct thread *);
102 uipc_abort(struct socket *so)
104 struct unpcb *unp = so->so_pcb;
108 unp_drop(unp, ECONNABORTED);
115 uipc_accept(struct socket *so, struct sockaddr **nam)
117 struct unpcb *unp = so->so_pcb;
123 * Pass back name of connected socket,
124 * if it was bound and we are still connected
125 * (our peer may have closed already!).
127 if (unp->unp_conn && unp->unp_conn->unp_addr) {
128 *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr);
130 *nam = dup_sockaddr((struct sockaddr *)&sun_noname);
136 uipc_attach(struct socket *so, int proto, struct pru_attach_info *ai)
138 struct unpcb *unp = so->so_pcb;
142 return unp_attach(so, ai);
146 uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
148 struct unpcb *unp = so->so_pcb;
152 return unp_bind(unp, nam, td);
156 uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
158 struct unpcb *unp = so->so_pcb;
162 return unp_connect(so, nam, td);
166 uipc_connect2(struct socket *so1, struct socket *so2)
168 struct unpcb *unp = so1->so_pcb;
173 return unp_connect2(so1, so2);
176 /* control is EOPNOTSUPP */
179 uipc_detach(struct socket *so)
181 struct unpcb *unp = so->so_pcb;
191 uipc_disconnect(struct socket *so)
193 struct unpcb *unp = so->so_pcb;
202 uipc_listen(struct socket *so, struct thread *td)
204 struct unpcb *unp = so->so_pcb;
206 if (unp == NULL || unp->unp_vnode == NULL)
208 return unp_listen(unp, td);
212 uipc_peeraddr(struct socket *so, struct sockaddr **nam)
214 struct unpcb *unp = so->so_pcb;
218 if (unp->unp_conn && unp->unp_conn->unp_addr)
219 *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr);
222 * XXX: It seems that this test always fails even when
223 * connection is established. So, this else clause is
224 * added as workaround to return PF_LOCAL sockaddr.
226 *nam = dup_sockaddr((struct sockaddr *)&sun_noname);
232 uipc_rcvd(struct socket *so, int flags)
234 struct unpcb *unp = so->so_pcb;
240 switch (so->so_type) {
242 panic("uipc_rcvd DGRAM?");
246 if (unp->unp_conn == NULL)
248 so2 = unp->unp_conn->unp_socket;
250 * Adjust backpressure on sender
251 * and wakeup any waiting to write.
253 so2->so_snd.ssb_mbmax += unp->unp_mbcnt - so->so_rcv.ssb_mbcnt;
254 unp->unp_mbcnt = so->so_rcv.ssb_mbcnt;
256 so2->so_snd.ssb_hiwat + unp->unp_cc - so->so_rcv.ssb_cc;
257 chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.ssb_hiwat,
258 newhiwat, RLIM_INFINITY);
259 unp->unp_cc = so->so_rcv.ssb_cc;
264 panic("uipc_rcvd unknown socktype");
269 /* pru_rcvoob is EOPNOTSUPP */
272 uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
273 struct mbuf *control, struct thread *td)
276 struct unpcb *unp = so->so_pcb;
284 if (flags & PRUS_OOB) {
289 if (control && (error = unp_internalize(control, td)))
292 switch (so->so_type) {
295 struct sockaddr *from;
302 error = unp_connect(so, nam, td);
306 if (unp->unp_conn == NULL) {
311 so2 = unp->unp_conn->unp_socket;
313 from = (struct sockaddr *)unp->unp_addr;
316 if (ssb_appendaddr(&so2->so_rcv, from, m, control)) {
329 /* Connect if not connected yet. */
331 * Note: A better implementation would complain
332 * if not equal to the peer's address.
334 if (!(so->so_state & SS_ISCONNECTED)) {
336 error = unp_connect(so, nam, td);
345 if (so->so_state & SS_CANTSENDMORE) {
349 if (unp->unp_conn == NULL)
350 panic("uipc_send connected but no connection?");
351 so2 = unp->unp_conn->unp_socket;
353 * Send to paired receive port, and then reduce
354 * send buffer hiwater marks to maintain backpressure.
358 if (ssb_appendcontrol(&so2->so_rcv, m, control)) {
363 sbappend(&so2->so_rcv.sb, m);
366 so->so_snd.ssb_mbmax -=
367 so2->so_rcv.ssb_mbcnt - unp->unp_conn->unp_mbcnt;
368 unp->unp_conn->unp_mbcnt = so2->so_rcv.ssb_mbcnt;
369 newhiwat = so->so_snd.ssb_hiwat -
370 (so2->so_rcv.ssb_cc - unp->unp_conn->unp_cc);
371 chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.ssb_hiwat,
372 newhiwat, RLIM_INFINITY);
373 unp->unp_conn->unp_cc = so2->so_rcv.ssb_cc;
378 panic("uipc_send unknown socktype");
382 * SEND_EOF is equivalent to a SEND followed by a SHUTDOWN.
384 if (flags & PRUS_EOF) {
389 if (control && error != 0)
390 unp_dispose(control);
401 uipc_sense(struct socket *so, struct stat *sb)
403 struct unpcb *unp = so->so_pcb;
408 sb->st_blksize = so->so_snd.ssb_hiwat;
409 if (so->so_type == SOCK_STREAM && unp->unp_conn != NULL) {
410 so2 = unp->unp_conn->unp_socket;
411 sb->st_blksize += so2->so_rcv.ssb_cc;
414 if (unp->unp_ino == 0) /* make up a non-zero inode number */
415 unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino;
416 sb->st_ino = unp->unp_ino;
421 uipc_shutdown(struct socket *so)
423 struct unpcb *unp = so->so_pcb;
433 uipc_sockaddr(struct socket *so, struct sockaddr **nam)
435 struct unpcb *unp = so->so_pcb;
440 *nam = dup_sockaddr((struct sockaddr *)unp->unp_addr);
444 struct pr_usrreqs uipc_usrreqs = {
445 .pru_abort = uipc_abort,
446 .pru_accept = uipc_accept,
447 .pru_attach = uipc_attach,
448 .pru_bind = uipc_bind,
449 .pru_connect = uipc_connect,
450 .pru_connect2 = uipc_connect2,
451 .pru_control = pru_control_notsupp,
452 .pru_detach = uipc_detach,
453 .pru_disconnect = uipc_disconnect,
454 .pru_listen = uipc_listen,
455 .pru_peeraddr = uipc_peeraddr,
456 .pru_rcvd = uipc_rcvd,
457 .pru_rcvoob = pru_rcvoob_notsupp,
458 .pru_send = uipc_send,
459 .pru_sense = uipc_sense,
460 .pru_shutdown = uipc_shutdown,
461 .pru_sockaddr = uipc_sockaddr,
462 .pru_sosend = sosend,
463 .pru_soreceive = soreceive,
468 uipc_ctloutput(struct socket *so, struct sockopt *sopt)
470 struct unpcb *unp = so->so_pcb;
473 switch (sopt->sopt_dir) {
475 switch (sopt->sopt_name) {
477 if (unp->unp_flags & UNP_HAVEPC)
478 error = sooptcopyout(sopt, &unp->unp_peercred,
479 sizeof(unp->unp_peercred));
481 if (so->so_type == SOCK_STREAM)
501 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
502 * for stream sockets, although the total for sender and receiver is
503 * actually only PIPSIZ.
504 * Datagram sockets really use the sendspace as the maximum datagram size,
505 * and don't really want to reserve the sendspace. Their recvspace should
506 * be large enough for at least one max-size datagram plus address.
511 static u_long unpst_sendspace = PIPSIZ;
512 static u_long unpst_recvspace = PIPSIZ;
513 static u_long unpdg_sendspace = 2*1024; /* really max datagram size */
514 static u_long unpdg_recvspace = 4*1024;
516 static int unp_rights; /* file descriptors in flight */
517 static struct spinlock unp_spin = SPINLOCK_INITIALIZER(&unp_spin);
519 SYSCTL_DECL(_net_local_stream);
520 SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
521 &unpst_sendspace, 0, "");
522 SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
523 &unpst_recvspace, 0, "");
525 SYSCTL_DECL(_net_local_dgram);
526 SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
527 &unpdg_sendspace, 0, "");
528 SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
529 &unpdg_recvspace, 0, "");
531 SYSCTL_DECL(_net_local);
532 SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, "");
535 unp_attach(struct socket *so, struct pru_attach_info *ai)
540 if (so->so_snd.ssb_hiwat == 0 || so->so_rcv.ssb_hiwat == 0) {
541 switch (so->so_type) {
544 error = soreserve(so, unpst_sendspace, unpst_recvspace,
549 error = soreserve(so, unpdg_sendspace, unpdg_recvspace,
559 unp = zalloc(unp_zone);
562 bzero(unp, sizeof *unp);
563 unp->unp_gencnt = ++unp_gencnt;
565 LIST_INIT(&unp->unp_refs);
566 unp->unp_socket = so;
567 unp->unp_rvnode = ai->fd_rdir; /* jail cruft XXX JH */
568 LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead
569 : &unp_shead, unp, unp_link);
570 so->so_pcb = (caddr_t)unp;
575 unp_detach(struct unpcb *unp)
577 LIST_REMOVE(unp, unp_link);
578 unp->unp_gencnt = ++unp_gencnt;
580 if (unp->unp_vnode) {
581 unp->unp_vnode->v_socket = NULL;
582 vrele(unp->unp_vnode);
583 unp->unp_vnode = NULL;
587 while (!LIST_EMPTY(&unp->unp_refs))
588 unp_drop(LIST_FIRST(&unp->unp_refs), ECONNRESET);
589 soisdisconnected(unp->unp_socket);
590 unp->unp_socket->so_pcb = NULL;
593 * Normally the receive buffer is flushed later,
594 * in sofree, but if our receive buffer holds references
595 * to descriptors that are now garbage, we will dispose
596 * of those descriptor references after the garbage collector
597 * gets them (resulting in a "panic: closef: count < 0").
599 sorflush(unp->unp_socket);
603 kfree(unp->unp_addr, M_SONAME);
604 zfree(unp_zone, unp);
608 unp_bind(struct unpcb *unp, struct sockaddr *nam, struct thread *td)
610 struct proc *p = td->td_proc;
611 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
615 struct nlookupdata nd;
616 char buf[SOCK_MAXADDRLEN];
618 if (unp->unp_vnode != NULL)
620 namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
623 strncpy(buf, soun->sun_path, namelen);
624 buf[namelen] = 0; /* null-terminate the string */
625 error = nlookup_init(&nd, buf, UIO_SYSSPACE,
626 NLC_LOCKVP | NLC_CREATE | NLC_REFDVP);
628 error = nlookup(&nd);
629 if (error == 0 && nd.nl_nch.ncp->nc_vp != NULL)
635 vattr.va_type = VSOCK;
636 vattr.va_mode = (ACCESSPERMS & ~p->p_fd->fd_cmask);
637 error = VOP_NCREATE(&nd.nl_nch, nd.nl_dvp, &vp, nd.nl_cred, &vattr);
639 vp->v_socket = unp->unp_socket;
641 unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam);
650 unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
652 struct proc *p = td->td_proc;
653 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
655 struct socket *so2, *so3;
656 struct unpcb *unp, *unp2, *unp3;
658 struct nlookupdata nd;
659 char buf[SOCK_MAXADDRLEN];
663 len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
666 strncpy(buf, soun->sun_path, len);
670 error = nlookup_init(&nd, buf, UIO_SYSSPACE, NLC_FOLLOW);
672 error = nlookup(&nd);
674 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
679 if (vp->v_type != VSOCK) {
683 error = VOP_ACCESS(vp, VWRITE, p->p_ucred);
688 error = ECONNREFUSED;
691 if (so->so_type != so2->so_type) {
695 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
696 if (!(so2->so_options & SO_ACCEPTCONN) ||
697 (so3 = sonewconn(so2, 0)) == NULL) {
698 error = ECONNREFUSED;
705 unp3->unp_addr = (struct sockaddr_un *)
706 dup_sockaddr((struct sockaddr *)unp2->unp_addr);
709 * unp_peercred management:
711 * The connecter's (client's) credentials are copied
712 * from its process structure at the time of connect()
715 cru2x(p->p_ucred, &unp3->unp_peercred);
716 unp3->unp_flags |= UNP_HAVEPC;
718 * The receiver's (server's) credentials are copied
719 * from the unp_peercred member of socket on which the
720 * former called listen(); unp_listen() cached that
721 * process's credentials at that time so we can use
724 KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED,
725 ("unp_connect: listener without cached peercred"));
726 memcpy(&unp->unp_peercred, &unp2->unp_peercred,
727 sizeof(unp->unp_peercred));
728 unp->unp_flags |= UNP_HAVEPC;
732 error = unp_connect2(so, so2);
739 unp_connect2(struct socket *so, struct socket *so2)
741 struct unpcb *unp = so->so_pcb;
744 if (so2->so_type != so->so_type)
747 unp->unp_conn = unp2;
748 switch (so->so_type) {
751 LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
756 unp2->unp_conn = unp;
762 panic("unp_connect2");
768 unp_disconnect(struct unpcb *unp)
770 struct unpcb *unp2 = unp->unp_conn;
775 unp->unp_conn = NULL;
777 switch (unp->unp_socket->so_type) {
779 LIST_REMOVE(unp, unp_reflink);
780 unp->unp_socket->so_state &= ~SS_ISCONNECTED;
783 soisdisconnected(unp->unp_socket);
784 unp2->unp_conn = NULL;
785 soisdisconnected(unp2->unp_socket);
792 unp_abort(struct unpcb *unp)
800 prison_unpcb(struct thread *td, struct unpcb *unp)
806 if ((p = td->td_proc) == NULL)
808 if (!p->p_ucred->cr_prison)
810 if (p->p_fd->fd_rdir == unp->unp_rvnode)
816 unp_pcblist(SYSCTL_HANDLER_ARGS)
819 struct unpcb *unp, **unp_list;
821 struct unp_head *head;
823 head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead);
825 KKASSERT(curproc != NULL);
828 * The process of preparing the PCB list is too time-consuming and
829 * resource-intensive to repeat twice on every request.
831 if (req->oldptr == NULL) {
833 req->oldidx = (n + n/8) * sizeof(struct xunpcb);
837 if (req->newptr != NULL)
841 * OK, now we're committed to doing something.
846 unp_list = kmalloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
848 for (unp = LIST_FIRST(head), i = 0; unp && i < n;
849 unp = LIST_NEXT(unp, unp_link)) {
850 if (unp->unp_gencnt <= gencnt && !prison_unpcb(req->td, unp))
853 n = i; /* in case we lost some during malloc */
856 for (i = 0; i < n; i++) {
858 if (unp->unp_gencnt <= gencnt) {
860 xu.xu_len = sizeof xu;
863 * XXX - need more locking here to protect against
864 * connect/disconnect races for SMP.
867 bcopy(unp->unp_addr, &xu.xu_addr,
868 unp->unp_addr->sun_len);
869 if (unp->unp_conn && unp->unp_conn->unp_addr)
870 bcopy(unp->unp_conn->unp_addr,
872 unp->unp_conn->unp_addr->sun_len);
873 bcopy(unp, &xu.xu_unp, sizeof *unp);
874 sotoxsocket(unp->unp_socket, &xu.xu_socket);
875 error = SYSCTL_OUT(req, &xu, sizeof xu);
878 kfree(unp_list, M_TEMP);
882 SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD,
883 (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
884 "List of active local datagram sockets");
885 SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD,
886 (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
887 "List of active local stream sockets");
890 unp_shutdown(struct unpcb *unp)
894 if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn != NULL &&
895 (so = unp->unp_conn->unp_socket))
900 unp_drop(struct unpcb *unp, int err)
902 struct socket *so = unp->unp_socket;
917 unp_externalize(struct mbuf *rights)
919 struct proc *p = curproc; /* XXX */
921 struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
925 int newfds = (cm->cmsg_len - (CMSG_DATA(cm) - (u_char *)cm))
926 / sizeof (struct file *);
930 * if the new FD's will not fit, then we free them all
932 if (!fdavail(p, newfds)) {
933 rp = (struct file **)CMSG_DATA(cm);
934 for (i = 0; i < newfds; i++) {
937 * zero the pointer before calling unp_discard,
938 * since it may end up in unp_gc()..
941 unp_discard(fp, NULL);
946 * now change each pointer to an fd in the global table to
947 * an integer that is the index to the local fd table entry
948 * that we set up to point to the global one we are transferring.
949 * If sizeof (struct file *) is bigger than or equal to sizeof int,
950 * then do it in forward order. In that case, an integer will
951 * always come in the same place or before its corresponding
952 * struct file pointer.
953 * If sizeof (struct file *) is smaller than sizeof int, then
954 * do it in reverse order.
956 if (sizeof (struct file *) >= sizeof (int)) {
957 fdp = (int *)(cm + 1);
958 rp = (struct file **)CMSG_DATA(cm);
959 for (i = 0; i < newfds; i++) {
960 if (fdalloc(p, 0, &f))
961 panic("unp_externalize");
965 spin_lock_wr(&unp_spin);
968 spin_unlock_wr(&unp_spin);
972 fdp = (int *)(cm + 1) + newfds - 1;
973 rp = (struct file **)CMSG_DATA(cm) + newfds - 1;
974 for (i = 0; i < newfds; i++) {
975 if (fdalloc(p, 0, &f))
976 panic("unp_externalize");
980 spin_lock_wr(&unp_spin);
983 spin_unlock_wr(&unp_spin);
989 * Adjust length, in case sizeof(struct file *) and sizeof(int)
992 cm->cmsg_len = CMSG_LEN(newfds * sizeof(int));
993 rights->m_len = cm->cmsg_len;
1000 unp_zone = zinit("unpcb", sizeof(struct unpcb), nmbclusters, 0, 0);
1001 if (unp_zone == NULL)
1003 LIST_INIT(&unp_dhead);
1004 LIST_INIT(&unp_shead);
1005 spin_init(&unp_spin);
1009 unp_internalize(struct mbuf *control, struct thread *td)
1011 struct proc *p = td->td_proc;
1012 struct filedesc *fdescp;
1013 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1017 struct cmsgcred *cmcred;
1023 if ((cm->cmsg_type != SCM_RIGHTS && cm->cmsg_type != SCM_CREDS) ||
1024 cm->cmsg_level != SOL_SOCKET || cm->cmsg_len != control->m_len)
1028 * Fill in credential information.
1030 if (cm->cmsg_type == SCM_CREDS) {
1031 cmcred = (struct cmsgcred *)(cm + 1);
1032 cmcred->cmcred_pid = p->p_pid;
1033 cmcred->cmcred_uid = p->p_ucred->cr_ruid;
1034 cmcred->cmcred_gid = p->p_ucred->cr_rgid;
1035 cmcred->cmcred_euid = p->p_ucred->cr_uid;
1036 cmcred->cmcred_ngroups = MIN(p->p_ucred->cr_ngroups,
1038 for (i = 0; i < cmcred->cmcred_ngroups; i++)
1039 cmcred->cmcred_groups[i] = p->p_ucred->cr_groups[i];
1043 oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int);
1045 * check that all the FDs passed in refer to legal OPEN files
1046 * If not, reject the entire operation.
1048 fdp = (int *)(cm + 1);
1049 for (i = 0; i < oldfds; i++) {
1051 if ((unsigned)fd >= fdescp->fd_nfiles ||
1052 fdescp->fd_files[fd].fp == NULL)
1054 if (fdescp->fd_files[fd].fp->f_type == DTYPE_KQUEUE)
1055 return (EOPNOTSUPP);
1058 * Now replace the integer FDs with pointers to
1059 * the associated global file table entry..
1060 * Allocate a bigger buffer as necessary. But if an cluster is not
1061 * enough, return E2BIG.
1063 newlen = CMSG_LEN(oldfds * sizeof(struct file *));
1064 if (newlen > MCLBYTES)
1066 if (newlen - control->m_len > M_TRAILINGSPACE(control)) {
1067 if (control->m_flags & M_EXT)
1069 MCLGET(control, MB_WAIT);
1070 if (!(control->m_flags & M_EXT))
1073 /* copy the data to the cluster */
1074 memcpy(mtod(control, char *), cm, cm->cmsg_len);
1075 cm = mtod(control, struct cmsghdr *);
1079 * Adjust length, in case sizeof(struct file *) and sizeof(int)
1082 control->m_len = cm->cmsg_len = newlen;
1085 * Transform the file descriptors into struct file pointers.
1086 * If sizeof (struct file *) is bigger than or equal to sizeof int,
1087 * then do it in reverse order so that the int won't get until
1089 * If sizeof (struct file *) is smaller than sizeof int, then
1090 * do it in forward order.
1092 if (sizeof (struct file *) >= sizeof (int)) {
1093 fdp = (int *)(cm + 1) + oldfds - 1;
1094 rp = (struct file **)CMSG_DATA(cm) + oldfds - 1;
1095 for (i = 0; i < oldfds; i++) {
1096 fp = fdescp->fd_files[*fdp--].fp;
1099 spin_lock_wr(&unp_spin);
1102 spin_unlock_wr(&unp_spin);
1105 fdp = (int *)(cm + 1);
1106 rp = (struct file **)CMSG_DATA(cm);
1107 for (i = 0; i < oldfds; i++) {
1108 fp = fdescp->fd_files[*fdp++].fp;
1111 spin_lock_wr(&unp_spin);
1114 spin_unlock_wr(&unp_spin);
1121 * Garbage collect in-transit file descriptors that get lost due to
1122 * loops (i.e. when a socket is sent to another process over itself,
1123 * and more complex situations).
1125 * NOT MPSAFE - TODO socket flush code and maybe closef. Rest is MPSAFE.
1128 struct unp_gc_info {
1129 struct file **extra_ref;
1130 struct file *locked_fp;
1139 struct unp_gc_info info;
1140 static boolean_t unp_gcing;
1144 spin_lock_wr(&unp_spin);
1146 spin_unlock_wr(&unp_spin);
1150 spin_unlock_wr(&unp_spin);
1153 * before going through all this, set all FDs to
1154 * be NOT defered and NOT externally accessible
1157 allfiles_scan_exclusive(unp_gc_clearmarks, NULL);
1159 allfiles_scan_exclusive(unp_gc_checkmarks, &info);
1160 } while (info.defer);
1163 * We grab an extra reference to each of the file table entries
1164 * that are not otherwise accessible and then free the rights
1165 * that are stored in messages on them.
1167 * The bug in the orginal code is a little tricky, so I'll describe
1168 * what's wrong with it here.
1170 * It is incorrect to simply unp_discard each entry for f_msgcount
1171 * times -- consider the case of sockets A and B that contain
1172 * references to each other. On a last close of some other socket,
1173 * we trigger a gc since the number of outstanding rights (unp_rights)
1174 * is non-zero. If during the sweep phase the gc code un_discards,
1175 * we end up doing a (full) closef on the descriptor. A closef on A
1176 * results in the following chain. Closef calls soo_close, which
1177 * calls soclose. Soclose calls first (through the switch
1178 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply
1179 * returns because the previous instance had set unp_gcing, and
1180 * we return all the way back to soclose, which marks the socket
1181 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush
1182 * to free up the rights that are queued in messages on the socket A,
1183 * i.e., the reference on B. The sorflush calls via the dom_dispose
1184 * switch unp_dispose, which unp_scans with unp_discard. This second
1185 * instance of unp_discard just calls closef on B.
1187 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1188 * which results in another closef on A. Unfortunately, A is already
1189 * being closed, and the descriptor has already been marked with
1190 * SS_NOFDREF, and soclose panics at this point.
1192 * Here, we first take an extra reference to each inaccessible
1193 * descriptor. Then, we call sorflush ourself, since we know
1194 * it is a Unix domain socket anyhow. After we destroy all the
1195 * rights carried in messages, we do a last closef to get rid
1196 * of our extra reference. This is the last close, and the
1197 * unp_detach etc will shut down the socket.
1199 * 91/09/19, bsy@cs.cmu.edu
1201 info.extra_ref = kmalloc(256 * sizeof(struct file *), M_FILE, M_WAITOK);
1202 info.maxindex = 256;
1209 allfiles_scan_exclusive(unp_gc_checkrefs, &info);
1212 * For each FD on our hit list, do the following two things
1214 for (i = info.index, fpp = info.extra_ref; --i >= 0; ++fpp) {
1215 struct file *tfp = *fpp;
1216 if (tfp->f_type == DTYPE_SOCKET && tfp->f_data != NULL)
1217 sorflush((struct socket *)(tfp->f_data));
1219 for (i = info.index, fpp = info.extra_ref; --i >= 0; ++fpp)
1221 } while (info.index == info.maxindex);
1222 kfree((caddr_t)info.extra_ref, M_FILE);
1227 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1230 unp_gc_checkrefs(struct file *fp, void *data)
1232 struct unp_gc_info *info = data;
1234 if (fp->f_count == 0)
1236 if (info->index == info->maxindex)
1240 * If all refs are from msgs, and it's not marked accessible
1241 * then it must be referenced from some unreachable cycle
1242 * of (shut-down) FDs, so include it in our
1243 * list of FDs to remove
1245 if (fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) {
1246 info->extra_ref[info->index++] = fp;
1253 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1256 unp_gc_clearmarks(struct file *fp, void *data __unused)
1258 fp->f_flag &= ~(FMARK|FDEFER);
1263 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1266 unp_gc_checkmarks(struct file *fp, void *data)
1268 struct unp_gc_info *info = data;
1272 * If the file is not open, skip it
1274 if (fp->f_count == 0)
1277 * If we already marked it as 'defer' in a
1278 * previous pass, then try process it this time
1281 if (fp->f_flag & FDEFER) {
1282 fp->f_flag &= ~FDEFER;
1286 * if it's not defered, then check if it's
1287 * already marked.. if so skip it
1289 if (fp->f_flag & FMARK)
1292 * If all references are from messages
1293 * in transit, then skip it. it's not
1294 * externally accessible.
1296 if (fp->f_count == fp->f_msgcount)
1299 * If it got this far then it must be
1300 * externally accessible.
1302 fp->f_flag |= FMARK;
1305 * either it was defered, or it is externally
1306 * accessible and not already marked so.
1307 * Now check if it is possibly one of OUR sockets.
1309 if (fp->f_type != DTYPE_SOCKET ||
1310 (so = (struct socket *)fp->f_data) == NULL)
1312 if (so->so_proto->pr_domain != &localdomain ||
1313 !(so->so_proto->pr_flags & PR_RIGHTS))
1316 XXX note: exclusive fp->f_spin lock held
1317 if (so->so_rcv.sb_flags & SB_LOCK) {
1319 * This is problematical; it's not clear
1320 * we need to wait for the sockbuf to be
1321 * unlocked (on a uniprocessor, at least),
1322 * and it's also not clear what to do
1323 * if sbwait returns an error due to receipt
1324 * of a signal. If sbwait does return
1325 * an error, we'll go into an infinite
1326 * loop. Delete all of this for now.
1328 sbwait(&so->so_rcv);
1333 * So, Ok, it's one of our sockets and it IS externally
1334 * accessible (or was defered). Now we look
1335 * to see if we hold any file descriptors in its
1336 * message buffers. Follow those links and mark them
1337 * as accessible too.
1339 info->locked_fp = fp;
1340 /* spin_lock_wr(&so->so_rcv.sb_spin); */
1341 unp_scan(so->so_rcv.ssb_mb, unp_mark, info);
1342 /* spin_unlock_wr(&so->so_rcv.sb_spin);*/
1347 unp_dispose(struct mbuf *m)
1350 unp_scan(m, unp_discard, NULL);
1354 unp_listen(struct unpcb *unp, struct thread *td)
1356 struct proc *p = td->td_proc;
1359 cru2x(p->p_ucred, &unp->unp_peercred);
1360 unp->unp_flags |= UNP_HAVEPCCACHED;
1365 unp_scan(struct mbuf *m0, void (*op)(struct file *, void *), void *data)
1374 for (m = m0; m; m = m->m_next) {
1375 if (m->m_type == MT_CONTROL &&
1376 m->m_len >= sizeof(*cm)) {
1377 cm = mtod(m, struct cmsghdr *);
1378 if (cm->cmsg_level != SOL_SOCKET ||
1379 cm->cmsg_type != SCM_RIGHTS)
1381 qfds = (cm->cmsg_len -
1382 (CMSG_DATA(cm) - (u_char *)cm))
1383 / sizeof (struct file *);
1384 rp = (struct file **)CMSG_DATA(cm);
1385 for (i = 0; i < qfds; i++)
1387 break; /* XXX, but saves time */
1395 unp_mark(struct file *fp, void *data)
1397 struct unp_gc_info *info = data;
1399 if (info->locked_fp != fp)
1400 spin_lock_wr(&fp->f_spin);
1401 if ((fp->f_flag & FMARK) == 0) {
1403 fp->f_flag |= (FMARK|FDEFER);
1405 if (info->locked_fp != fp)
1406 spin_unlock_wr(&fp->f_spin);
1410 unp_discard(struct file *fp, void *data __unused)
1412 spin_lock_wr(&unp_spin);
1415 spin_unlock_wr(&unp_spin);