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
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/fcntl.h>
41 #include <sys/domain.h>
42 #include <sys/filedesc.h>
44 #include <sys/malloc.h> /* XXX must be before <sys/file.h> */
46 #include <sys/mutex.h>
48 #include <sys/namei.h>
50 #include <sys/protosw.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/resourcevar.h>
55 #include <sys/sysctl.h>
57 #include <sys/unpcb.h>
58 #include <sys/vnode.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 __P((struct socket *));
83 static void unp_detach __P((struct unpcb *));
84 static int unp_bind __P((struct unpcb *,struct sockaddr *, struct thread *));
85 static int unp_connect __P((struct socket *,struct sockaddr *,
87 static void unp_disconnect __P((struct unpcb *));
88 static void unp_shutdown __P((struct unpcb *));
89 static void unp_drop __P((struct unpcb *, int));
90 static void unp_gc __P((void));
91 static void unp_scan __P((struct mbuf *, void (*)(struct file *)));
92 static void unp_mark __P((struct file *));
93 static void unp_discard __P((struct file *));
94 static void unp_freerights __P((struct file **, int));
95 static int unp_internalize __P((struct mbuf **, struct thread *));
96 static int unp_listen __P((struct unpcb *, struct thread *));
99 uipc_abort(struct socket *so)
101 struct unpcb *unp = sotounpcb(so);
105 unp_drop(unp, ECONNABORTED);
112 uipc_accept(struct socket *so, struct sockaddr **nam)
114 struct unpcb *unp = sotounpcb(so);
120 * Pass back name of connected socket,
121 * if it was bound and we are still connected
122 * (our peer may have closed already!).
124 if (unp->unp_conn && unp->unp_conn->unp_addr) {
125 *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr,
128 *nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1);
134 uipc_attach(struct socket *so, int proto, struct thread *td)
136 struct unpcb *unp = sotounpcb(so);
140 return unp_attach(so);
144 uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
146 struct unpcb *unp = sotounpcb(so);
151 return unp_bind(unp, nam, td);
155 uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
157 struct unpcb *unp = sotounpcb(so);
161 return unp_connect(so, nam, curthread);
165 uipc_connect2(struct socket *so1, struct socket *so2)
167 struct unpcb *unp = sotounpcb(so1);
172 return unp_connect2(so1, so2);
175 /* control is EOPNOTSUPP */
178 uipc_detach(struct socket *so)
180 struct unpcb *unp = sotounpcb(so);
190 uipc_disconnect(struct socket *so)
192 struct unpcb *unp = sotounpcb(so);
201 uipc_listen(struct socket *so, struct thread *td)
203 struct unpcb *unp = sotounpcb(so);
205 if (unp == 0 || unp->unp_vnode == 0)
207 return unp_listen(unp, td);
211 uipc_peeraddr(struct socket *so, struct sockaddr **nam)
213 struct unpcb *unp = sotounpcb(so);
217 if (unp->unp_conn && unp->unp_conn->unp_addr)
218 *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr,
224 uipc_rcvd(struct socket *so, int flags)
226 struct unpcb *unp = sotounpcb(so);
232 switch (so->so_type) {
234 panic("uipc_rcvd DGRAM?");
238 if (unp->unp_conn == 0)
240 so2 = unp->unp_conn->unp_socket;
242 * Adjust backpressure on sender
243 * and wakeup any waiting to write.
245 so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt;
246 unp->unp_mbcnt = so->so_rcv.sb_mbcnt;
247 newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc -
249 (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat,
250 newhiwat, RLIM_INFINITY);
251 unp->unp_cc = so->so_rcv.sb_cc;
256 panic("uipc_rcvd unknown socktype");
261 /* pru_rcvoob is EOPNOTSUPP */
264 uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
265 struct mbuf *control, struct thread *td)
268 struct unpcb *unp = sotounpcb(so);
276 if (flags & PRUS_OOB) {
281 if (control && (error = unp_internalize(&control, td)))
284 switch (so->so_type) {
287 struct sockaddr *from;
294 error = unp_connect(so, nam, td);
298 if (unp->unp_conn == 0) {
303 so2 = unp->unp_conn->unp_socket;
305 from = (struct sockaddr *)unp->unp_addr;
308 if (sbappendaddr(&so2->so_rcv, from, m, control)) {
320 /* Connect if not connected yet. */
322 * Note: A better implementation would complain
323 * if not equal to the peer's address.
325 if ((so->so_state & SS_ISCONNECTED) == 0) {
327 error = unp_connect(so, nam, td);
336 if (so->so_state & SS_CANTSENDMORE) {
340 if (unp->unp_conn == 0)
341 panic("uipc_send connected but no connection?");
342 so2 = unp->unp_conn->unp_socket;
344 * Send to paired receive port, and then reduce
345 * send buffer hiwater marks to maintain backpressure.
349 if (sbappendcontrol(&so2->so_rcv, m, control))
352 sbappend(&so2->so_rcv, m);
353 so->so_snd.sb_mbmax -=
354 so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt;
355 unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt;
356 newhiwat = so->so_snd.sb_hiwat -
357 (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc);
358 (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat,
359 newhiwat, RLIM_INFINITY);
360 unp->unp_conn->unp_cc = so2->so_rcv.sb_cc;
366 panic("uipc_send unknown socktype");
370 * SEND_EOF is equivalent to a SEND followed by
373 if (flags & PRUS_EOF) {
378 if (control && error != 0)
379 unp_dispose(control);
390 uipc_sense(struct socket *so, struct stat *sb)
392 struct unpcb *unp = sotounpcb(so);
397 sb->st_blksize = so->so_snd.sb_hiwat;
398 if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
399 so2 = unp->unp_conn->unp_socket;
400 sb->st_blksize += so2->so_rcv.sb_cc;
403 if (unp->unp_ino == 0)
404 unp->unp_ino = unp_ino++;
405 sb->st_ino = unp->unp_ino;
410 uipc_shutdown(struct socket *so)
412 struct unpcb *unp = sotounpcb(so);
422 uipc_sockaddr(struct socket *so, struct sockaddr **nam)
424 struct unpcb *unp = sotounpcb(so);
429 *nam = dup_sockaddr((struct sockaddr *)unp->unp_addr, 1);
431 *nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1);
435 struct pr_usrreqs uipc_usrreqs = {
436 uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect,
437 uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect,
438 uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp,
439 uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr,
440 sosend, soreceive, sopoll
444 uipc_ctloutput(so, sopt)
446 struct sockopt *sopt;
448 struct unpcb *unp = sotounpcb(so);
451 switch (sopt->sopt_dir) {
453 switch (sopt->sopt_name) {
455 if (unp->unp_flags & UNP_HAVEPC)
456 error = sooptcopyout(sopt, &unp->unp_peercred,
457 sizeof(unp->unp_peercred));
459 if (so->so_type == SOCK_STREAM)
479 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
480 * for stream sockets, although the total for sender and receiver is
481 * actually only PIPSIZ.
482 * Datagram sockets really use the sendspace as the maximum datagram size,
483 * and don't really want to reserve the sendspace. Their recvspace should
484 * be large enough for at least one max-size datagram plus address.
489 static u_long unpst_sendspace = PIPSIZ;
490 static u_long unpst_recvspace = PIPSIZ;
491 static u_long unpdg_sendspace = 2*1024; /* really max datagram size */
492 static u_long unpdg_recvspace = 4*1024;
494 static int unp_rights; /* file descriptors in flight */
496 SYSCTL_DECL(_net_local_stream);
497 SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
498 &unpst_sendspace, 0, "");
499 SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
500 &unpst_recvspace, 0, "");
501 SYSCTL_DECL(_net_local_dgram);
502 SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
503 &unpdg_sendspace, 0, "");
504 SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
505 &unpdg_recvspace, 0, "");
506 SYSCTL_DECL(_net_local);
507 SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, "");
513 register struct unpcb *unp;
516 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
517 switch (so->so_type) {
520 error = soreserve(so, unpst_sendspace, unpst_recvspace);
524 error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
533 unp = zalloc(unp_zone);
536 bzero(unp, sizeof *unp);
537 unp->unp_gencnt = ++unp_gencnt;
539 LIST_INIT(&unp->unp_refs);
540 unp->unp_socket = so;
541 FILEDESC_LOCK(curproc->p_fd);
542 unp->unp_rvnode = curthread->td_proc->p_fd->fd_rdir;
543 FILEDESC_UNLOCK(curproc->p_fd);
544 LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead
545 : &unp_shead, unp, unp_link);
546 so->so_pcb = (caddr_t)unp;
552 register struct unpcb *unp;
554 LIST_REMOVE(unp, unp_link);
555 unp->unp_gencnt = ++unp_gencnt;
557 if (unp->unp_vnode) {
558 unp->unp_vnode->v_socket = 0;
559 vrele(unp->unp_vnode);
564 while (!LIST_EMPTY(&unp->unp_refs))
565 unp_drop(LIST_FIRST(&unp->unp_refs), ECONNRESET);
566 soisdisconnected(unp->unp_socket);
567 unp->unp_socket->so_pcb = 0;
570 * Normally the receive buffer is flushed later,
571 * in sofree, but if our receive buffer holds references
572 * to descriptors that are now garbage, we will dispose
573 * of those descriptor references after the garbage collector
574 * gets them (resulting in a "panic: closef: count < 0").
576 sorflush(unp->unp_socket);
580 FREE(unp->unp_addr, M_SONAME);
581 zfree(unp_zone, unp);
585 unp_bind(unp, nam, td)
587 struct sockaddr *nam;
590 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
598 if (unp->unp_vnode != NULL)
600 namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
603 buf = malloc(SOCK_MAXADDRLEN, M_TEMP, M_WAITOK);
604 strncpy(buf, soun->sun_path, namelen);
605 buf[namelen] = 0; /* null-terminate the string */
607 NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT, UIO_SYSSPACE,
609 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
616 if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
617 NDFREE(&nd, NDF_ONLY_PNBUF);
627 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
635 vattr.va_type = VSOCK;
636 FILEDESC_LOCK(td->td_proc->p_fd);
637 vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask);
638 FILEDESC_UNLOCK(td->td_proc->p_fd);
639 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
640 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
641 NDFREE(&nd, NDF_ONLY_PNBUF);
648 vp->v_socket = unp->unp_socket;
650 unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam, 1);
651 VOP_UNLOCK(vp, 0, td);
652 vn_finished_write(mp);
658 unp_connect(so, nam, td)
660 struct sockaddr *nam;
663 register struct sockaddr_un *soun = (struct sockaddr_un *)nam;
664 register struct vnode *vp;
665 register struct socket *so2, *so3;
666 struct unpcb *unp, *unp2, *unp3;
669 char buf[SOCK_MAXADDRLEN];
671 len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
674 strncpy(buf, soun->sun_path, len);
677 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td);
682 NDFREE(&nd, NDF_ONLY_PNBUF);
683 if (vp->v_type != VSOCK) {
687 error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td);
692 error = ECONNREFUSED;
695 if (so->so_type != so2->so_type) {
699 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
700 if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
701 (so3 = sonewconn(so2, 0)) == 0) {
702 error = ECONNREFUSED;
706 unp2 = sotounpcb(so2);
707 unp3 = sotounpcb(so3);
709 unp3->unp_addr = (struct sockaddr_un *)
710 dup_sockaddr((struct sockaddr *)
714 * unp_peercred management:
716 * The connecter's (client's) credentials are copied
717 * from its process structure at the time of connect()
720 cru2x(td->td_ucred, &unp3->unp_peercred);
721 unp3->unp_flags |= UNP_HAVEPC;
723 * The receiver's (server's) credentials are copied
724 * from the unp_peercred member of socket on which the
725 * former called listen(); unp_listen() cached that
726 * process's credentials at that time so we can use
729 KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED,
730 ("unp_connect: listener without cached peercred"));
731 memcpy(&unp->unp_peercred, &unp2->unp_peercred,
732 sizeof(unp->unp_peercred));
733 unp->unp_flags |= UNP_HAVEPC;
737 error = unp_connect2(so, so2);
744 unp_connect2(so, so2)
745 register struct socket *so;
746 register struct socket *so2;
748 register struct unpcb *unp = sotounpcb(so);
749 register struct unpcb *unp2;
751 if (so2->so_type != so->so_type)
753 unp2 = sotounpcb(so2);
754 unp->unp_conn = unp2;
755 switch (so->so_type) {
758 LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
763 unp2->unp_conn = unp;
769 panic("unp_connect2");
778 register struct unpcb *unp2 = unp->unp_conn;
783 switch (unp->unp_socket->so_type) {
786 LIST_REMOVE(unp, unp_reflink);
787 unp->unp_socket->so_state &= ~SS_ISCONNECTED;
791 soisdisconnected(unp->unp_socket);
793 soisdisconnected(unp2->unp_socket);
809 unp_pcblist(SYSCTL_HANDLER_ARGS)
812 struct unpcb *unp, **unp_list;
815 struct unp_head *head;
818 head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead);
821 * The process of preparing the PCB list is too time-consuming and
822 * resource-intensive to repeat twice on every request.
824 if (req->oldptr == 0) {
826 req->oldidx = 2 * (sizeof *xug)
827 + (n + n/8) * sizeof(struct xunpcb);
831 if (req->newptr != 0)
835 * OK, now we're committed to doing something.
837 xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK);
841 xug->xug_len = sizeof *xug;
843 xug->xug_gen = gencnt;
844 xug->xug_sogen = so_gencnt;
845 error = SYSCTL_OUT(req, xug, sizeof *xug);
851 unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
853 for (unp = LIST_FIRST(head), i = 0; unp && i < n;
854 unp = LIST_NEXT(unp, unp_link)) {
855 if (unp->unp_gencnt <= gencnt) {
856 if (cr_cansee(req->td->td_ucred,
857 unp->unp_socket->so_cred))
862 n = i; /* in case we lost some during malloc */
865 xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK);
866 for (i = 0; i < n; i++) {
868 if (unp->unp_gencnt <= gencnt) {
869 xu->xu_len = sizeof *xu;
872 * XXX - need more locking here to protect against
873 * connect/disconnect races for SMP.
876 bcopy(unp->unp_addr, &xu->xu_addr,
877 unp->unp_addr->sun_len);
878 if (unp->unp_conn && unp->unp_conn->unp_addr)
879 bcopy(unp->unp_conn->unp_addr,
881 unp->unp_conn->unp_addr->sun_len);
882 bcopy(unp, &xu->xu_unp, sizeof *unp);
883 sotoxsocket(unp->unp_socket, &xu->xu_socket);
884 error = SYSCTL_OUT(req, xu, sizeof *xu);
890 * Give the user an updated idea of our state.
891 * If the generation differs from what we told
892 * her before, she knows that something happened
893 * while we were processing this request, and it
894 * might be necessary to retry.
896 xug->xug_gen = unp_gencnt;
897 xug->xug_sogen = so_gencnt;
898 xug->xug_count = unp_count;
899 error = SYSCTL_OUT(req, xug, sizeof *xug);
901 free(unp_list, M_TEMP);
906 SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD,
907 (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
908 "List of active local datagram sockets");
909 SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD,
910 (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
911 "List of active local stream sockets");
919 if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
920 (so = unp->unp_conn->unp_socket))
929 struct socket *so = unp->unp_socket;
931 so->so_error = errno;
944 unp_freerights(rp, fdcount)
951 for (i = 0; i < fdcount; i++) {
954 * zero the pointer before calling
955 * unp_discard since it may end up
964 unp_externalize(control, controlp)
965 struct mbuf *control, **controlp;
967 struct thread *td = curthread; /* XXX */
968 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
974 socklen_t clen = control->m_len, datalen;
980 if (controlp != NULL) /* controlp == NULL => free control messages */
984 if (sizeof(*cm) > clen || cm->cmsg_len > clen) {
989 data = CMSG_DATA(cm);
990 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
992 if (cm->cmsg_level == SOL_SOCKET
993 && cm->cmsg_type == SCM_RIGHTS) {
994 newfds = datalen / sizeof(struct file *);
997 /* If we're not outputting the discriptors free them. */
998 if (error || controlp == NULL) {
999 unp_freerights(rp, newfds);
1002 FILEDESC_LOCK(td->td_proc->p_fd);
1003 /* if the new FD's will not fit free them. */
1004 if (!fdavail(td, newfds)) {
1005 FILEDESC_UNLOCK(td->td_proc->p_fd);
1007 unp_freerights(rp, newfds);
1011 * now change each pointer to an fd in the global
1012 * table to an integer that is the index to the
1013 * local fd table entry that we set up to point
1014 * to the global one we are transferring.
1016 newlen = newfds * sizeof(int);
1017 *controlp = sbcreatecontrol(NULL, newlen,
1018 SCM_RIGHTS, SOL_SOCKET);
1019 if (*controlp == NULL) {
1020 FILEDESC_UNLOCK(td->td_proc->p_fd);
1022 unp_freerights(rp, newfds);
1027 CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1028 for (i = 0; i < newfds; i++) {
1029 if (fdalloc(td, 0, &f))
1030 panic("unp_externalize fdalloc failed");
1032 td->td_proc->p_fd->fd_ofiles[f] = fp;
1039 FILEDESC_UNLOCK(td->td_proc->p_fd);
1040 } else { /* We can just copy anything else across */
1041 if (error || controlp == NULL)
1043 *controlp = sbcreatecontrol(NULL, datalen,
1044 cm->cmsg_type, cm->cmsg_level);
1045 if (*controlp == NULL) {
1050 CMSG_DATA(mtod(*controlp, struct cmsghdr *)),
1054 controlp = &(*controlp)->m_next;
1057 if (CMSG_SPACE(datalen) < clen) {
1058 clen -= CMSG_SPACE(datalen);
1059 cm = (struct cmsghdr *)
1060 ((caddr_t)cm + CMSG_SPACE(datalen));
1075 unp_zone = zinit("unpcb", sizeof(struct unpcb), nmbclusters, 0, 0);
1078 LIST_INIT(&unp_dhead);
1079 LIST_INIT(&unp_shead);
1083 #define MIN(a,b) (((a)<(b))?(a):(b))
1087 unp_internalize(controlp, td)
1088 struct mbuf **controlp;
1091 struct mbuf *control = *controlp;
1092 struct proc *p = td->td_proc;
1093 struct filedesc *fdescp = p->p_fd;
1094 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1095 struct cmsgcred *cmcred;
1101 socklen_t clen = control->m_len, datalen;
1108 while (cm != NULL) {
1109 if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET
1110 || cm->cmsg_len > clen) {
1115 data = CMSG_DATA(cm);
1116 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1118 switch (cm->cmsg_type) {
1120 * Fill in credential information.
1123 *controlp = sbcreatecontrol(NULL, sizeof(*cmcred),
1124 SCM_CREDS, SOL_SOCKET);
1125 if (*controlp == NULL) {
1130 cmcred = (struct cmsgcred *)
1131 CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1132 cmcred->cmcred_pid = p->p_pid;
1133 cmcred->cmcred_uid = td->td_ucred->cr_ruid;
1134 cmcred->cmcred_gid = td->td_ucred->cr_rgid;
1135 cmcred->cmcred_euid = td->td_ucred->cr_uid;
1136 cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups,
1138 for (i = 0; i < cmcred->cmcred_ngroups; i++)
1139 cmcred->cmcred_groups[i] =
1140 td->td_ucred->cr_groups[i];
1144 oldfds = datalen / sizeof (int);
1146 * check that all the FDs passed in refer to legal files
1147 * If not, reject the entire operation.
1150 FILEDESC_LOCK(fdescp);
1151 for (i = 0; i < oldfds; i++) {
1153 if ((unsigned)fd >= fdescp->fd_nfiles ||
1154 fdescp->fd_ofiles[fd] == NULL) {
1155 FILEDESC_UNLOCK(fdescp);
1161 * Now replace the integer FDs with pointers to
1162 * the associated global file table entry..
1164 newlen = oldfds * sizeof(struct file *);
1165 *controlp = sbcreatecontrol(NULL, newlen,
1166 SCM_RIGHTS, SOL_SOCKET);
1167 if (*controlp == NULL) {
1168 FILEDESC_UNLOCK(fdescp);
1174 rp = (struct file **)
1175 CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1176 for (i = 0; i < oldfds; i++) {
1177 fp = fdescp->fd_ofiles[*fdp++];
1185 FILEDESC_UNLOCK(fdescp);
1189 *controlp = sbcreatecontrol(NULL, sizeof(*tv),
1190 SCM_TIMESTAMP, SOL_SOCKET);
1191 if (*controlp == NULL) {
1195 tv = (struct timeval *)
1196 CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1205 controlp = &(*controlp)->m_next;
1207 if (CMSG_SPACE(datalen) < clen) {
1208 clen -= CMSG_SPACE(datalen);
1209 cm = (struct cmsghdr *)
1210 ((caddr_t)cm + CMSG_SPACE(datalen));
1223 static int unp_defer, unp_gcing;
1228 register struct file *fp, *nextfp;
1229 register struct socket *so;
1230 struct file **extra_ref, **fpp;
1238 * before going through all this, set all FDs to
1239 * be NOT defered and NOT externally accessible
1241 sx_slock(&filelist_lock);
1242 LIST_FOREACH(fp, &filehead, f_list)
1243 fp->f_gcflag &= ~(FMARK|FDEFER);
1245 LIST_FOREACH(fp, &filehead, f_list) {
1248 * If the file is not open, skip it
1250 if (fp->f_count == 0) {
1255 * If we already marked it as 'defer' in a
1256 * previous pass, then try process it this time
1259 if (fp->f_gcflag & FDEFER) {
1260 fp->f_gcflag &= ~FDEFER;
1264 * if it's not defered, then check if it's
1265 * already marked.. if so skip it
1267 if (fp->f_gcflag & FMARK) {
1272 * If all references are from messages
1273 * in transit, then skip it. it's not
1274 * externally accessible.
1276 if (fp->f_count == fp->f_msgcount) {
1281 * If it got this far then it must be
1282 * externally accessible.
1284 fp->f_gcflag |= FMARK;
1287 * either it was defered, or it is externally
1288 * accessible and not already marked so.
1289 * Now check if it is possibly one of OUR sockets.
1291 if (fp->f_type != DTYPE_SOCKET ||
1292 (so = (struct socket *)fp->f_data) == 0) {
1297 if (so->so_proto->pr_domain != &localdomain ||
1298 (so->so_proto->pr_flags&PR_RIGHTS) == 0)
1301 if (so->so_rcv.sb_flags & SB_LOCK) {
1303 * This is problematical; it's not clear
1304 * we need to wait for the sockbuf to be
1305 * unlocked (on a uniprocessor, at least),
1306 * and it's also not clear what to do
1307 * if sbwait returns an error due to receipt
1308 * of a signal. If sbwait does return
1309 * an error, we'll go into an infinite
1310 * loop. Delete all of this for now.
1312 (void) sbwait(&so->so_rcv);
1317 * So, Ok, it's one of our sockets and it IS externally
1318 * accessible (or was defered). Now we look
1319 * to see if we hold any file descriptors in its
1320 * message buffers. Follow those links and mark them
1321 * as accessible too.
1323 unp_scan(so->so_rcv.sb_mb, unp_mark);
1325 } while (unp_defer);
1326 sx_sunlock(&filelist_lock);
1328 * We grab an extra reference to each of the file table entries
1329 * that are not otherwise accessible and then free the rights
1330 * that are stored in messages on them.
1332 * The bug in the orginal code is a little tricky, so I'll describe
1333 * what's wrong with it here.
1335 * It is incorrect to simply unp_discard each entry for f_msgcount
1336 * times -- consider the case of sockets A and B that contain
1337 * references to each other. On a last close of some other socket,
1338 * we trigger a gc since the number of outstanding rights (unp_rights)
1339 * is non-zero. If during the sweep phase the gc code un_discards,
1340 * we end up doing a (full) closef on the descriptor. A closef on A
1341 * results in the following chain. Closef calls soo_close, which
1342 * calls soclose. Soclose calls first (through the switch
1343 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply
1344 * returns because the previous instance had set unp_gcing, and
1345 * we return all the way back to soclose, which marks the socket
1346 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush
1347 * to free up the rights that are queued in messages on the socket A,
1348 * i.e., the reference on B. The sorflush calls via the dom_dispose
1349 * switch unp_dispose, which unp_scans with unp_discard. This second
1350 * instance of unp_discard just calls closef on B.
1352 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1353 * which results in another closef on A. Unfortunately, A is already
1354 * being closed, and the descriptor has already been marked with
1355 * SS_NOFDREF, and soclose panics at this point.
1357 * Here, we first take an extra reference to each inaccessible
1358 * descriptor. Then, we call sorflush ourself, since we know
1359 * it is a Unix domain socket anyhow. After we destroy all the
1360 * rights carried in messages, we do a last closef to get rid
1361 * of our extra reference. This is the last close, and the
1362 * unp_detach etc will shut down the socket.
1364 * 91/09/19, bsy@cs.cmu.edu
1366 extra_ref = malloc(nfiles * sizeof(struct file *), M_FILE, M_WAITOK);
1367 sx_slock(&filelist_lock);
1368 for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; fp != 0;
1370 nextfp = LIST_NEXT(fp, f_list);
1373 * If it's not open, skip it
1375 if (fp->f_count == 0) {
1380 * If all refs are from msgs, and it's not marked accessible
1381 * then it must be referenced from some unreachable cycle
1382 * of (shut-down) FDs, so include it in our
1383 * list of FDs to remove
1385 if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) {
1392 sx_sunlock(&filelist_lock);
1394 * for each FD on our hit list, do the following two things
1396 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
1397 struct file *tfp = *fpp;
1399 if (tfp->f_type == DTYPE_SOCKET && tfp->f_data != NULL) {
1401 sorflush((struct socket *)(tfp->f_data));
1405 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp)
1406 closef(*fpp, (struct thread *) NULL);
1407 free((caddr_t)extra_ref, M_FILE);
1417 unp_scan(m, unp_discard);
1426 cru2x(td->td_ucred, &unp->unp_peercred);
1427 unp->unp_flags |= UNP_HAVEPCCACHED;
1433 register struct mbuf *m0;
1434 void (*op) __P((struct file *));
1441 socklen_t clen, datalen;
1445 for (m = m0; m; m = m->m_next) {
1446 if (m->m_type != MT_CONTROL)
1449 cm = mtod(m, struct cmsghdr *);
1452 while (cm != NULL) {
1453 if (sizeof(*cm) > clen || cm->cmsg_len > clen)
1456 data = CMSG_DATA(cm);
1457 datalen = (caddr_t)cm + cm->cmsg_len
1460 if (cm->cmsg_level == SOL_SOCKET &&
1461 cm->cmsg_type == SCM_RIGHTS) {
1462 qfds = datalen / sizeof (struct file *);
1464 for (i = 0; i < qfds; i++)
1468 if (CMSG_SPACE(datalen) < clen) {
1469 clen -= CMSG_SPACE(datalen);
1470 cm = (struct cmsghdr *)
1471 ((caddr_t)cm + CMSG_SPACE(datalen));
1486 if (fp->f_gcflag & FMARK)
1489 fp->f_gcflag |= (FMARK|FDEFER);
1500 (void) closef(fp, (struct thread *)NULL);