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>
61 #include <vm/vm_zone.h>
63 static struct vm_zone *unp_zone;
64 static unp_gen_t unp_gencnt;
65 static u_int unp_count;
67 static struct unp_head unp_shead, unp_dhead;
70 * Unix communications domain.
74 * rethink name space problems
75 * need a proper out-of-band
78 static struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL };
79 static ino_t unp_ino; /* prototype for fake inode numbers */
81 static int unp_attach __P((struct socket *));
82 static void unp_detach __P((struct unpcb *));
83 static int unp_bind __P((struct unpcb *,struct sockaddr *, struct thread *));
84 static int unp_connect __P((struct socket *,struct sockaddr *,
86 static void unp_disconnect __P((struct unpcb *));
87 static void unp_shutdown __P((struct unpcb *));
88 static void unp_drop __P((struct unpcb *, int));
89 static void unp_gc __P((void));
90 static void unp_scan __P((struct mbuf *, void (*)(struct file *)));
91 static void unp_mark __P((struct file *));
92 static void unp_discard __P((struct file *));
93 static void unp_freerights __P((struct file **, int));
94 static int unp_internalize __P((struct mbuf **, struct thread *));
95 static int unp_listen __P((struct unpcb *, struct proc *));
98 uipc_abort(struct socket *so)
100 struct unpcb *unp = sotounpcb(so);
104 unp_drop(unp, ECONNABORTED);
109 uipc_accept(struct socket *so, struct sockaddr **nam)
111 struct unpcb *unp = sotounpcb(so);
117 * Pass back name of connected socket,
118 * if it was bound and we are still connected
119 * (our peer may have closed already!).
121 if (unp->unp_conn && unp->unp_conn->unp_addr) {
122 *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr,
125 *nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1);
131 uipc_attach(struct socket *so, int proto, struct thread *td)
133 struct unpcb *unp = sotounpcb(so);
137 return unp_attach(so);
141 uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
143 struct unpcb *unp = sotounpcb(so);
148 return unp_bind(unp, nam, td);
152 uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
154 struct unpcb *unp = sotounpcb(so);
158 return unp_connect(so, nam, curthread);
162 uipc_connect2(struct socket *so1, struct socket *so2)
164 struct unpcb *unp = sotounpcb(so1);
169 return unp_connect2(so1, so2);
172 /* control is EOPNOTSUPP */
175 uipc_detach(struct socket *so)
177 struct unpcb *unp = sotounpcb(so);
187 uipc_disconnect(struct socket *so)
189 struct unpcb *unp = sotounpcb(so);
198 uipc_listen(struct socket *so, struct thread *td)
200 struct unpcb *unp = sotounpcb(so);
202 if (unp == 0 || unp->unp_vnode == 0)
204 return unp_listen(unp, td->td_proc);
208 uipc_peeraddr(struct socket *so, struct sockaddr **nam)
210 struct unpcb *unp = sotounpcb(so);
214 if (unp->unp_conn && unp->unp_conn->unp_addr)
215 *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr,
221 uipc_rcvd(struct socket *so, int flags)
223 struct unpcb *unp = sotounpcb(so);
229 switch (so->so_type) {
231 panic("uipc_rcvd DGRAM?");
235 if (unp->unp_conn == 0)
237 so2 = unp->unp_conn->unp_socket;
239 * Adjust backpressure on sender
240 * and wakeup any waiting to write.
242 so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt;
243 unp->unp_mbcnt = so->so_rcv.sb_mbcnt;
244 newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc -
246 (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat,
247 newhiwat, RLIM_INFINITY);
248 unp->unp_cc = so->so_rcv.sb_cc;
253 panic("uipc_rcvd unknown socktype");
258 /* pru_rcvoob is EOPNOTSUPP */
261 uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
262 struct mbuf *control, struct thread *td)
265 struct unpcb *unp = sotounpcb(so);
273 if (flags & PRUS_OOB) {
278 if (control && (error = unp_internalize(&control, td)))
281 switch (so->so_type) {
284 struct sockaddr *from;
291 error = unp_connect(so, nam, td);
295 if (unp->unp_conn == 0) {
300 so2 = unp->unp_conn->unp_socket;
302 from = (struct sockaddr *)unp->unp_addr;
305 if (sbappendaddr(&so2->so_rcv, from, m, control)) {
317 /* Connect if not connected yet. */
319 * Note: A better implementation would complain
320 * if not equal to the peer's address.
322 if ((so->so_state & SS_ISCONNECTED) == 0) {
324 error = unp_connect(so, nam, td);
333 if (so->so_state & SS_CANTSENDMORE) {
337 if (unp->unp_conn == 0)
338 panic("uipc_send connected but no connection?");
339 so2 = unp->unp_conn->unp_socket;
341 * Send to paired receive port, and then reduce
342 * send buffer hiwater marks to maintain backpressure.
346 if (sbappendcontrol(&so2->so_rcv, m, control))
349 sbappend(&so2->so_rcv, m);
350 so->so_snd.sb_mbmax -=
351 so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt;
352 unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt;
353 newhiwat = so->so_snd.sb_hiwat -
354 (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc);
355 (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat,
356 newhiwat, RLIM_INFINITY);
357 unp->unp_conn->unp_cc = so2->so_rcv.sb_cc;
363 panic("uipc_send unknown socktype");
367 * SEND_EOF is equivalent to a SEND followed by
370 if (flags & PRUS_EOF) {
375 if (control && error != 0)
376 unp_dispose(control);
387 uipc_sense(struct socket *so, struct stat *sb)
389 struct unpcb *unp = sotounpcb(so);
394 sb->st_blksize = so->so_snd.sb_hiwat;
395 if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
396 so2 = unp->unp_conn->unp_socket;
397 sb->st_blksize += so2->so_rcv.sb_cc;
400 if (unp->unp_ino == 0)
401 unp->unp_ino = unp_ino++;
402 sb->st_ino = unp->unp_ino;
407 uipc_shutdown(struct socket *so)
409 struct unpcb *unp = sotounpcb(so);
419 uipc_sockaddr(struct socket *so, struct sockaddr **nam)
421 struct unpcb *unp = sotounpcb(so);
426 *nam = dup_sockaddr((struct sockaddr *)unp->unp_addr, 1);
428 *nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1);
432 struct pr_usrreqs uipc_usrreqs = {
433 uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect,
434 uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect,
435 uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp,
436 uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr,
437 sosend, soreceive, sopoll
441 uipc_ctloutput(so, sopt)
443 struct sockopt *sopt;
445 struct unpcb *unp = sotounpcb(so);
448 switch (sopt->sopt_dir) {
450 switch (sopt->sopt_name) {
452 if (unp->unp_flags & UNP_HAVEPC)
453 error = sooptcopyout(sopt, &unp->unp_peercred,
454 sizeof(unp->unp_peercred));
456 if (so->so_type == SOCK_STREAM)
476 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
477 * for stream sockets, although the total for sender and receiver is
478 * actually only PIPSIZ.
479 * Datagram sockets really use the sendspace as the maximum datagram size,
480 * and don't really want to reserve the sendspace. Their recvspace should
481 * be large enough for at least one max-size datagram plus address.
486 static u_long unpst_sendspace = PIPSIZ;
487 static u_long unpst_recvspace = PIPSIZ;
488 static u_long unpdg_sendspace = 2*1024; /* really max datagram size */
489 static u_long unpdg_recvspace = 4*1024;
491 static int unp_rights; /* file descriptors in flight */
493 SYSCTL_DECL(_net_local_stream);
494 SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
495 &unpst_sendspace, 0, "");
496 SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
497 &unpst_recvspace, 0, "");
498 SYSCTL_DECL(_net_local_dgram);
499 SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
500 &unpdg_sendspace, 0, "");
501 SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
502 &unpdg_recvspace, 0, "");
503 SYSCTL_DECL(_net_local);
504 SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, "");
510 register struct unpcb *unp;
513 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
514 switch (so->so_type) {
517 error = soreserve(so, unpst_sendspace, unpst_recvspace);
521 error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
530 unp = zalloc(unp_zone);
533 bzero(unp, sizeof *unp);
534 unp->unp_gencnt = ++unp_gencnt;
536 LIST_INIT(&unp->unp_refs);
537 unp->unp_socket = so;
538 unp->unp_rvnode = curthread->td_proc->p_fd->fd_rdir;
539 LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead
540 : &unp_shead, unp, unp_link);
541 so->so_pcb = (caddr_t)unp;
547 register struct unpcb *unp;
549 LIST_REMOVE(unp, unp_link);
550 unp->unp_gencnt = ++unp_gencnt;
552 if (unp->unp_vnode) {
553 unp->unp_vnode->v_socket = 0;
554 vrele(unp->unp_vnode);
559 while (!LIST_EMPTY(&unp->unp_refs))
560 unp_drop(LIST_FIRST(&unp->unp_refs), ECONNRESET);
561 soisdisconnected(unp->unp_socket);
562 unp->unp_socket->so_pcb = 0;
565 * Normally the receive buffer is flushed later,
566 * in sofree, but if our receive buffer holds references
567 * to descriptors that are now garbage, we will dispose
568 * of those descriptor references after the garbage collector
569 * gets them (resulting in a "panic: closef: count < 0").
571 sorflush(unp->unp_socket);
575 FREE(unp->unp_addr, M_SONAME);
576 zfree(unp_zone, unp);
580 unp_bind(unp, nam, td)
582 struct sockaddr *nam;
585 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
593 if (unp->unp_vnode != NULL)
595 namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
598 buf = malloc(SOCK_MAXADDRLEN, M_TEMP, M_WAITOK);
599 strncpy(buf, soun->sun_path, namelen);
600 buf[namelen] = 0; /* null-terminate the string */
602 NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT, UIO_SYSSPACE,
604 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
611 if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
612 NDFREE(&nd, NDF_ONLY_PNBUF);
622 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
630 vattr.va_type = VSOCK;
631 vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask);
632 VOP_LEASE(nd.ni_dvp, td, td->td_proc->p_ucred, LEASE_WRITE);
633 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
634 NDFREE(&nd, NDF_ONLY_PNBUF);
641 vp->v_socket = unp->unp_socket;
643 unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam, 1);
644 VOP_UNLOCK(vp, 0, td);
645 vn_finished_write(mp);
651 unp_connect(so, nam, td)
653 struct sockaddr *nam;
656 register struct sockaddr_un *soun = (struct sockaddr_un *)nam;
657 register struct vnode *vp;
658 register struct socket *so2, *so3;
659 struct unpcb *unp, *unp2, *unp3;
662 char buf[SOCK_MAXADDRLEN];
664 len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
667 strncpy(buf, soun->sun_path, len);
670 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td);
675 NDFREE(&nd, NDF_ONLY_PNBUF);
676 if (vp->v_type != VSOCK) {
680 error = VOP_ACCESS(vp, VWRITE, td->td_proc->p_ucred, td);
685 error = ECONNREFUSED;
688 if (so->so_type != so2->so_type) {
692 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
693 if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
694 (so3 = sonewconn3(so2, 0, td)) == 0) {
695 error = ECONNREFUSED;
699 unp2 = sotounpcb(so2);
700 unp3 = sotounpcb(so3);
702 unp3->unp_addr = (struct sockaddr_un *)
703 dup_sockaddr((struct sockaddr *)
707 * unp_peercred management:
709 * The connecter's (client's) credentials are copied
710 * from its process structure at the time of connect()
713 memset(&unp3->unp_peercred, '\0', sizeof(unp3->unp_peercred));
714 unp3->unp_peercred.cr_uid = td->td_proc->p_ucred->cr_uid;
715 unp3->unp_peercred.cr_ngroups = td->td_proc->p_ucred->cr_ngroups;
716 memcpy(unp3->unp_peercred.cr_groups, td->td_proc->p_ucred->cr_groups,
717 sizeof(unp3->unp_peercred.cr_groups));
718 unp3->unp_flags |= UNP_HAVEPC;
720 * The receiver's (server's) credentials are copied
721 * from the unp_peercred member of socket on which the
722 * former called listen(); unp_listen() cached that
723 * process's credentials at that time so we can use
726 KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED,
727 ("unp_connect: listener without cached peercred"));
728 memcpy(&unp->unp_peercred, &unp2->unp_peercred,
729 sizeof(unp->unp_peercred));
730 unp->unp_flags |= UNP_HAVEPC;
734 error = unp_connect2(so, so2);
741 unp_connect2(so, so2)
742 register struct socket *so;
743 register struct socket *so2;
745 register struct unpcb *unp = sotounpcb(so);
746 register struct unpcb *unp2;
748 if (so2->so_type != so->so_type)
750 unp2 = sotounpcb(so2);
751 unp->unp_conn = unp2;
752 switch (so->so_type) {
755 LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
760 unp2->unp_conn = unp;
766 panic("unp_connect2");
775 register struct unpcb *unp2 = unp->unp_conn;
780 switch (unp->unp_socket->so_type) {
783 LIST_REMOVE(unp, unp_reflink);
784 unp->unp_socket->so_state &= ~SS_ISCONNECTED;
788 soisdisconnected(unp->unp_socket);
790 soisdisconnected(unp2->unp_socket);
806 unp_pcblist(SYSCTL_HANDLER_ARGS)
809 struct unpcb *unp, **unp_list;
812 struct unp_head *head;
815 head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead);
818 * The process of preparing the PCB list is too time-consuming and
819 * resource-intensive to repeat twice on every request.
821 if (req->oldptr == 0) {
823 req->oldidx = 2 * (sizeof *xug)
824 + (n + n/8) * sizeof(struct xunpcb);
828 if (req->newptr != 0)
832 * OK, now we're committed to doing something.
834 xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK);
838 xug->xug_len = sizeof *xug;
840 xug->xug_gen = gencnt;
841 xug->xug_sogen = so_gencnt;
842 error = SYSCTL_OUT(req, xug, sizeof *xug);
848 unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
850 for (unp = LIST_FIRST(head), i = 0; unp && i < n;
851 unp = LIST_NEXT(unp, unp_link)) {
852 if (unp->unp_gencnt <= gencnt) {
853 if (cr_cansee(req->td->td_proc->p_ucred,
854 unp->unp_socket->so_cred))
859 n = i; /* in case we lost some during malloc */
862 xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK);
863 for (i = 0; i < n; i++) {
865 if (unp->unp_gencnt <= gencnt) {
866 xu->xu_len = sizeof *xu;
869 * XXX - need more locking here to protect against
870 * connect/disconnect races for SMP.
873 bcopy(unp->unp_addr, &xu->xu_addr,
874 unp->unp_addr->sun_len);
875 if (unp->unp_conn && unp->unp_conn->unp_addr)
876 bcopy(unp->unp_conn->unp_addr,
878 unp->unp_conn->unp_addr->sun_len);
879 bcopy(unp, &xu->xu_unp, sizeof *unp);
880 sotoxsocket(unp->unp_socket, &xu->xu_socket);
881 error = SYSCTL_OUT(req, xu, sizeof *xu);
887 * Give the user an updated idea of our state.
888 * If the generation differs from what we told
889 * her before, she knows that something happened
890 * while we were processing this request, and it
891 * might be necessary to retry.
893 xug->xug_gen = unp_gencnt;
894 xug->xug_sogen = so_gencnt;
895 xug->xug_count = unp_count;
896 error = SYSCTL_OUT(req, xug, sizeof *xug);
898 free(unp_list, M_TEMP);
903 SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD,
904 (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
905 "List of active local datagram sockets");
906 SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD,
907 (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
908 "List of active local stream sockets");
916 if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
917 (so = unp->unp_conn->unp_socket))
926 struct socket *so = unp->unp_socket;
928 so->so_error = errno;
931 LIST_REMOVE(unp, unp_link);
932 unp->unp_gencnt = ++unp_gencnt;
934 so->so_pcb = (caddr_t) 0;
936 FREE(unp->unp_addr, M_SONAME);
937 zfree(unp_zone, unp);
951 unp_freerights(rp, fdcount)
958 for (i = 0; i < fdcount; i++) {
961 * zero the pointer before calling
962 * unp_discard since it may end up
971 unp_externalize(control, controlp)
972 struct mbuf *control, **controlp;
974 struct thread *td = curthread; /* XXX */
975 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
981 socklen_t clen = control->m_len, datalen;
987 if (controlp != NULL) /* controlp == NULL => free control messages */
991 if (sizeof(*cm) > clen || cm->cmsg_len > clen) {
996 data = CMSG_DATA(cm);
997 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
999 if (cm->cmsg_level == SOL_SOCKET
1000 && cm->cmsg_type == SCM_RIGHTS) {
1001 newfds = datalen / sizeof(struct file *);
1004 /* If we're not outputting the discriptors free them. */
1005 if (error || controlp == NULL) {
1006 unp_freerights(rp, newfds);
1009 /* if the new FD's will not fit free them. */
1010 if (!fdavail(td, newfds)) {
1012 unp_freerights(rp, newfds);
1016 * now change each pointer to an fd in the global
1017 * table to an integer that is the index to the
1018 * local fd table entry that we set up to point
1019 * to the global one we are transferring.
1021 newlen = newfds * sizeof(int);
1022 *controlp = sbcreatecontrol(NULL, newlen,
1023 SCM_RIGHTS, SOL_SOCKET);
1024 if (*controlp == NULL) {
1026 unp_freerights(rp, newfds);
1031 CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1032 for (i = 0; i < newfds; i++) {
1033 if (fdalloc(td, 0, &f))
1034 panic("unp_externalize fdalloc failed");
1036 td->td_proc->p_fd->fd_ofiles[f] = fp;
1041 } else { /* We can just copy anything else across */
1042 if (error || controlp == NULL)
1044 *controlp = sbcreatecontrol(NULL, datalen,
1045 cm->cmsg_type, cm->cmsg_level);
1046 if (*controlp == NULL) {
1051 CMSG_DATA(mtod(*controlp, struct cmsghdr *)),
1055 controlp = &(*controlp)->m_next;
1058 if (CMSG_SPACE(datalen) < clen) {
1059 clen -= CMSG_SPACE(datalen);
1060 cm = (struct cmsghdr *)
1061 ((caddr_t)cm + CMSG_SPACE(datalen));
1076 unp_zone = zinit("unpcb", sizeof(struct unpcb), nmbclusters, 0, 0);
1079 LIST_INIT(&unp_dhead);
1080 LIST_INIT(&unp_shead);
1084 #define MIN(a,b) (((a)<(b))?(a):(b))
1088 unp_internalize(controlp, td)
1089 struct mbuf **controlp;
1092 struct mbuf *control = *controlp;
1093 struct proc *p = td->td_proc;
1094 struct filedesc *fdescp = p->p_fd;
1095 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1096 struct cmsgcred *cmcred;
1102 socklen_t clen = control->m_len, datalen;
1109 while (cm != NULL) {
1110 if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET
1111 || cm->cmsg_len > clen) {
1116 data = CMSG_DATA(cm);
1117 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1119 switch (cm->cmsg_type) {
1121 * Fill in credential information.
1124 *controlp = sbcreatecontrol(NULL, sizeof(*cmcred),
1125 SCM_CREDS, SOL_SOCKET);
1126 if (*controlp == NULL) {
1131 cmcred = (struct cmsgcred *)
1132 CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1133 cmcred->cmcred_pid = p->p_pid;
1134 cmcred->cmcred_uid = p->p_ucred->cr_ruid;
1135 cmcred->cmcred_gid = p->p_ucred->cr_rgid;
1136 cmcred->cmcred_euid = p->p_ucred->cr_uid;
1137 cmcred->cmcred_ngroups = MIN(p->p_ucred->cr_ngroups,
1139 for (i = 0; i < cmcred->cmcred_ngroups; i++)
1140 cmcred->cmcred_groups[i] =
1141 p->p_ucred->cr_groups[i];
1145 oldfds = datalen / sizeof (int);
1147 * check that all the FDs passed in refer to legal files
1148 * If not, reject the entire operation.
1151 for (i = 0; i < oldfds; i++) {
1153 if ((unsigned)fd >= fdescp->fd_nfiles ||
1154 fdescp->fd_ofiles[fd] == NULL) {
1160 * Now replace the integer FDs with pointers to
1161 * the associated global file table entry..
1163 newlen = oldfds * sizeof(struct file *);
1164 *controlp = sbcreatecontrol(NULL, newlen,
1165 SCM_RIGHTS, SOL_SOCKET);
1166 if (*controlp == NULL) {
1172 rp = (struct file **)
1173 CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1174 for (i = 0; i < oldfds; i++) {
1175 fp = fdescp->fd_ofiles[*fdp++];
1184 *controlp = sbcreatecontrol(NULL, sizeof(*tv),
1185 SCM_TIMESTAMP, SOL_SOCKET);
1186 if (*controlp == NULL) {
1190 tv = (struct timeval *)
1191 CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1200 controlp = &(*controlp)->m_next;
1202 if (CMSG_SPACE(datalen) < clen) {
1203 clen -= CMSG_SPACE(datalen);
1204 cm = (struct cmsghdr *)
1205 ((caddr_t)cm + CMSG_SPACE(datalen));
1218 static int unp_defer, unp_gcing;
1223 register struct file *fp, *nextfp;
1224 register struct socket *so;
1225 struct file **extra_ref, **fpp;
1233 * before going through all this, set all FDs to
1234 * be NOT defered and NOT externally accessible
1236 LIST_FOREACH(fp, &filehead, f_list)
1237 fp->f_flag &= ~(FMARK|FDEFER);
1239 LIST_FOREACH(fp, &filehead, f_list) {
1241 * If the file is not open, skip it
1243 if (fp->f_count == 0)
1246 * If we already marked it as 'defer' in a
1247 * previous pass, then try process it this time
1250 if (fp->f_flag & FDEFER) {
1251 fp->f_flag &= ~FDEFER;
1255 * if it's not defered, then check if it's
1256 * already marked.. if so skip it
1258 if (fp->f_flag & FMARK)
1261 * If all references are from messages
1262 * in transit, then skip it. it's not
1263 * externally accessible.
1265 if (fp->f_count == fp->f_msgcount)
1268 * If it got this far then it must be
1269 * externally accessible.
1271 fp->f_flag |= FMARK;
1274 * either it was defered, or it is externally
1275 * accessible and not already marked so.
1276 * Now check if it is possibly one of OUR sockets.
1278 if (fp->f_type != DTYPE_SOCKET ||
1279 (so = (struct socket *)fp->f_data) == 0)
1281 if (so->so_proto->pr_domain != &localdomain ||
1282 (so->so_proto->pr_flags&PR_RIGHTS) == 0)
1285 if (so->so_rcv.sb_flags & SB_LOCK) {
1287 * This is problematical; it's not clear
1288 * we need to wait for the sockbuf to be
1289 * unlocked (on a uniprocessor, at least),
1290 * and it's also not clear what to do
1291 * if sbwait returns an error due to receipt
1292 * of a signal. If sbwait does return
1293 * an error, we'll go into an infinite
1294 * loop. Delete all of this for now.
1296 (void) sbwait(&so->so_rcv);
1301 * So, Ok, it's one of our sockets and it IS externally
1302 * accessible (or was defered). Now we look
1303 * to see if we hold any file descriptors in its
1304 * message buffers. Follow those links and mark them
1305 * as accessible too.
1307 unp_scan(so->so_rcv.sb_mb, unp_mark);
1309 } while (unp_defer);
1311 * We grab an extra reference to each of the file table entries
1312 * that are not otherwise accessible and then free the rights
1313 * that are stored in messages on them.
1315 * The bug in the orginal code is a little tricky, so I'll describe
1316 * what's wrong with it here.
1318 * It is incorrect to simply unp_discard each entry for f_msgcount
1319 * times -- consider the case of sockets A and B that contain
1320 * references to each other. On a last close of some other socket,
1321 * we trigger a gc since the number of outstanding rights (unp_rights)
1322 * is non-zero. If during the sweep phase the gc code un_discards,
1323 * we end up doing a (full) closef on the descriptor. A closef on A
1324 * results in the following chain. Closef calls soo_close, which
1325 * calls soclose. Soclose calls first (through the switch
1326 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply
1327 * returns because the previous instance had set unp_gcing, and
1328 * we return all the way back to soclose, which marks the socket
1329 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush
1330 * to free up the rights that are queued in messages on the socket A,
1331 * i.e., the reference on B. The sorflush calls via the dom_dispose
1332 * switch unp_dispose, which unp_scans with unp_discard. This second
1333 * instance of unp_discard just calls closef on B.
1335 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1336 * which results in another closef on A. Unfortunately, A is already
1337 * being closed, and the descriptor has already been marked with
1338 * SS_NOFDREF, and soclose panics at this point.
1340 * Here, we first take an extra reference to each inaccessible
1341 * descriptor. Then, we call sorflush ourself, since we know
1342 * it is a Unix domain socket anyhow. After we destroy all the
1343 * rights carried in messages, we do a last closef to get rid
1344 * of our extra reference. This is the last close, and the
1345 * unp_detach etc will shut down the socket.
1347 * 91/09/19, bsy@cs.cmu.edu
1349 extra_ref = malloc(nfiles * sizeof(struct file *), M_FILE, M_WAITOK);
1350 for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; fp != 0;
1352 nextfp = LIST_NEXT(fp, f_list);
1354 * If it's not open, skip it
1356 if (fp->f_count == 0)
1359 * If all refs are from msgs, and it's not marked accessible
1360 * then it must be referenced from some unreachable cycle
1361 * of (shut-down) FDs, so include it in our
1362 * list of FDs to remove
1364 if (fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) {
1371 * for each FD on our hit list, do the following two things
1373 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
1374 struct file *tfp = *fpp;
1375 if (tfp->f_type == DTYPE_SOCKET && tfp->f_data != NULL)
1376 sorflush((struct socket *)(tfp->f_data));
1378 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp)
1379 closef(*fpp, (struct thread *) NULL);
1380 free((caddr_t)extra_ref, M_FILE);
1390 unp_scan(m, unp_discard);
1399 bzero(&unp->unp_peercred, sizeof(unp->unp_peercred));
1400 unp->unp_peercred.cr_uid = p->p_ucred->cr_uid;
1401 unp->unp_peercred.cr_ngroups = p->p_ucred->cr_ngroups;
1402 bcopy(p->p_ucred->cr_groups, unp->unp_peercred.cr_groups,
1403 sizeof(unp->unp_peercred.cr_groups));
1404 unp->unp_flags |= UNP_HAVEPCCACHED;
1410 register struct mbuf *m0;
1411 void (*op) __P((struct file *));
1418 socklen_t clen, datalen;
1422 for (m = m0; m; m = m->m_next) {
1423 if (m->m_type != MT_CONTROL)
1426 cm = mtod(m, struct cmsghdr *);
1429 while (cm != NULL) {
1430 if (sizeof(*cm) > clen || cm->cmsg_len > clen)
1433 data = CMSG_DATA(cm);
1434 datalen = (caddr_t)cm + cm->cmsg_len
1437 if (cm->cmsg_level == SOL_SOCKET &&
1438 cm->cmsg_type == SCM_RIGHTS) {
1439 qfds = datalen / sizeof (struct file *);
1441 for (i = 0; i < qfds; i++)
1445 if (CMSG_SPACE(datalen) < clen) {
1446 clen -= CMSG_SPACE(datalen);
1447 cm = (struct cmsghdr *)
1448 ((caddr_t)cm + CMSG_SPACE(datalen));
1464 if (fp->f_flag & FMARK)
1467 fp->f_flag |= (FMARK|FDEFER);
1477 (void) closef(fp, (struct thread *)NULL);