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.4 2003/06/25 03:55:57 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/namei.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/resourcevar.h>
54 #include <sys/sysctl.h>
56 #include <sys/unpcb.h>
57 #include <sys/vnode.h>
58 #include <sys/file2.h>
60 #include <vm/vm_zone.h>
62 static struct vm_zone *unp_zone;
63 static unp_gen_t unp_gencnt;
64 static u_int unp_count;
66 static struct unp_head unp_shead, unp_dhead;
69 * Unix communications domain.
73 * rethink name space problems
74 * need a proper out-of-band
77 static struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL };
78 static ino_t unp_ino; /* prototype for fake inode numbers */
80 static int unp_attach __P((struct socket *));
81 static void unp_detach __P((struct unpcb *));
82 static int unp_bind __P((struct unpcb *,struct sockaddr *, struct thread *));
83 static int unp_connect __P((struct socket *,struct sockaddr *,
85 static void unp_disconnect __P((struct unpcb *));
86 static void unp_shutdown __P((struct unpcb *));
87 static void unp_drop __P((struct unpcb *, int));
88 static void unp_gc __P((void));
89 static void unp_scan __P((struct mbuf *, void (*)(struct file *)));
90 static void unp_mark __P((struct file *));
91 static void unp_discard __P((struct file *));
92 static int unp_internalize __P((struct mbuf *, struct thread *));
93 static int unp_listen __P((struct unpcb *, struct thread *));
96 uipc_abort(struct socket *so)
98 struct unpcb *unp = sotounpcb(so);
102 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);
147 return unp_bind(unp, nam, td);
151 uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
153 struct unpcb *unp = sotounpcb(so);
157 return unp_connect(so, nam, td);
161 uipc_connect2(struct socket *so1, struct socket *so2)
163 struct unpcb *unp = sotounpcb(so1);
168 return unp_connect2(so1, so2);
171 /* control is EOPNOTSUPP */
174 uipc_detach(struct socket *so)
176 struct unpcb *unp = sotounpcb(so);
186 uipc_disconnect(struct socket *so)
188 struct unpcb *unp = sotounpcb(so);
197 uipc_listen(struct socket *so, struct thread *td)
199 struct unpcb *unp = sotounpcb(so);
201 if (unp == 0 || unp->unp_vnode == 0)
203 return unp_listen(unp, td);
207 uipc_peeraddr(struct socket *so, struct sockaddr **nam)
209 struct unpcb *unp = sotounpcb(so);
213 if (unp->unp_conn && unp->unp_conn->unp_addr)
214 *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr,
218 * XXX: It seems that this test always fails even when
219 * connection is established. So, this else clause is
220 * added as workaround to return PF_LOCAL sockaddr.
222 *nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1);
228 uipc_rcvd(struct socket *so, int flags)
230 struct unpcb *unp = sotounpcb(so);
236 switch (so->so_type) {
238 panic("uipc_rcvd DGRAM?");
242 if (unp->unp_conn == 0)
244 so2 = unp->unp_conn->unp_socket;
246 * Adjust backpressure on sender
247 * and wakeup any waiting to write.
249 so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt;
250 unp->unp_mbcnt = so->so_rcv.sb_mbcnt;
251 newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc -
253 (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat,
254 newhiwat, RLIM_INFINITY);
255 unp->unp_cc = so->so_rcv.sb_cc;
260 panic("uipc_rcvd unknown socktype");
265 /* pru_rcvoob is EOPNOTSUPP */
268 uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
269 struct mbuf *control, struct thread *td)
272 struct unpcb *unp = sotounpcb(so);
280 if (flags & PRUS_OOB) {
285 if (control && (error = unp_internalize(control, td)))
288 switch (so->so_type) {
291 struct sockaddr *from;
298 error = unp_connect(so, nam, td);
302 if (unp->unp_conn == 0) {
307 so2 = unp->unp_conn->unp_socket;
309 from = (struct sockaddr *)unp->unp_addr;
312 if (sbappendaddr(&so2->so_rcv, from, m, control)) {
324 /* Connect if not connected yet. */
326 * Note: A better implementation would complain
327 * if not equal to the peer's address.
329 if ((so->so_state & SS_ISCONNECTED) == 0) {
331 error = unp_connect(so, nam, td);
340 if (so->so_state & SS_CANTSENDMORE) {
344 if (unp->unp_conn == 0)
345 panic("uipc_send connected but no connection?");
346 so2 = unp->unp_conn->unp_socket;
348 * Send to paired receive port, and then reduce
349 * send buffer hiwater marks to maintain backpressure.
353 if (sbappendcontrol(&so2->so_rcv, m, control))
356 sbappend(&so2->so_rcv, m);
357 so->so_snd.sb_mbmax -=
358 so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt;
359 unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt;
360 newhiwat = so->so_snd.sb_hiwat -
361 (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc);
362 (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat,
363 newhiwat, RLIM_INFINITY);
364 unp->unp_conn->unp_cc = so2->so_rcv.sb_cc;
370 panic("uipc_send unknown socktype");
374 * SEND_EOF is equivalent to a SEND followed by
377 if (flags & PRUS_EOF) {
382 if (control && error != 0)
383 unp_dispose(control);
394 uipc_sense(struct socket *so, struct stat *sb)
396 struct unpcb *unp = sotounpcb(so);
401 sb->st_blksize = so->so_snd.sb_hiwat;
402 if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
403 so2 = unp->unp_conn->unp_socket;
404 sb->st_blksize += so2->so_rcv.sb_cc;
407 if (unp->unp_ino == 0) /* make up a non-zero inode number */
408 unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino;
409 sb->st_ino = unp->unp_ino;
414 uipc_shutdown(struct socket *so)
416 struct unpcb *unp = sotounpcb(so);
426 uipc_sockaddr(struct socket *so, struct sockaddr **nam)
428 struct unpcb *unp = sotounpcb(so);
433 *nam = dup_sockaddr((struct sockaddr *)unp->unp_addr, 1);
437 struct pr_usrreqs uipc_usrreqs = {
438 uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect,
439 uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect,
440 uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp,
441 uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr,
442 sosend, soreceive, sopoll
446 uipc_ctloutput(so, sopt)
448 struct sockopt *sopt;
450 struct unpcb *unp = sotounpcb(so);
453 switch (sopt->sopt_dir) {
455 switch (sopt->sopt_name) {
457 if (unp->unp_flags & UNP_HAVEPC)
458 error = sooptcopyout(sopt, &unp->unp_peercred,
459 sizeof(unp->unp_peercred));
461 if (so->so_type == SOCK_STREAM)
481 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
482 * for stream sockets, although the total for sender and receiver is
483 * actually only PIPSIZ.
484 * Datagram sockets really use the sendspace as the maximum datagram size,
485 * and don't really want to reserve the sendspace. Their recvspace should
486 * be large enough for at least one max-size datagram plus address.
491 static u_long unpst_sendspace = PIPSIZ;
492 static u_long unpst_recvspace = PIPSIZ;
493 static u_long unpdg_sendspace = 2*1024; /* really max datagram size */
494 static u_long unpdg_recvspace = 4*1024;
496 static int unp_rights; /* file descriptors in flight */
498 SYSCTL_DECL(_net_local_stream);
499 SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
500 &unpst_sendspace, 0, "");
501 SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
502 &unpst_recvspace, 0, "");
503 SYSCTL_DECL(_net_local_dgram);
504 SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
505 &unpdg_sendspace, 0, "");
506 SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
507 &unpdg_recvspace, 0, "");
508 SYSCTL_DECL(_net_local);
509 SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, "");
515 register struct unpcb *unp;
518 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
519 switch (so->so_type) {
522 error = soreserve(so, unpst_sendspace, unpst_recvspace);
526 error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
535 unp = zalloc(unp_zone);
538 bzero(unp, sizeof *unp);
539 unp->unp_gencnt = ++unp_gencnt;
541 LIST_INIT(&unp->unp_refs);
542 unp->unp_socket = so;
543 unp->unp_rvnode = curproc->p_fd->fd_rdir;
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(struct unpcb *unp, struct sockaddr *nam, struct thread *td)
587 struct proc *p = td->td_proc;
588 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
593 char buf[SOCK_MAXADDRLEN];
595 if (unp->unp_vnode != NULL)
597 namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
600 strncpy(buf, soun->sun_path, namelen);
601 buf[namelen] = 0; /* null-terminate the string */
602 NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT, UIO_SYSSPACE, buf, td);
603 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
609 NDFREE(&nd, NDF_ONLY_PNBUF);
618 vattr.va_type = VSOCK;
619 vattr.va_mode = (ACCESSPERMS & ~p->p_fd->fd_cmask);
620 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
621 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
622 NDFREE(&nd, NDF_ONLY_PNBUF);
627 vp->v_socket = unp->unp_socket;
629 unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam, 1);
630 VOP_UNLOCK(vp, 0, td);
635 unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
637 struct proc *p = td->td_proc;
638 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
640 struct socket *so2, *so3;
641 struct unpcb *unp, *unp2, *unp3;
644 char buf[SOCK_MAXADDRLEN];
648 len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
651 strncpy(buf, soun->sun_path, len);
654 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td);
659 NDFREE(&nd, NDF_ONLY_PNBUF);
660 if (vp->v_type != VSOCK) {
664 error = VOP_ACCESS(vp, VWRITE, p->p_ucred, td);
669 error = ECONNREFUSED;
672 if (so->so_type != so2->so_type) {
676 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
677 if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
678 (so3 = sonewconn(so2, 0)) == 0) {
679 error = ECONNREFUSED;
683 unp2 = sotounpcb(so2);
684 unp3 = sotounpcb(so3);
686 unp3->unp_addr = (struct sockaddr_un *)
687 dup_sockaddr((struct sockaddr *)
691 * unp_peercred management:
693 * The connecter's (client's) credentials are copied
694 * from its process structure at the time of connect()
697 cru2x(p->p_ucred, &unp3->unp_peercred);
698 unp3->unp_flags |= UNP_HAVEPC;
700 * The receiver's (server's) credentials are copied
701 * from the unp_peercred member of socket on which the
702 * former called listen(); unp_listen() cached that
703 * process's credentials at that time so we can use
706 KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED,
707 ("unp_connect: listener without cached peercred"));
708 memcpy(&unp->unp_peercred, &unp2->unp_peercred,
709 sizeof(unp->unp_peercred));
710 unp->unp_flags |= UNP_HAVEPC;
714 error = unp_connect2(so, so2);
721 unp_connect2(so, so2)
722 register struct socket *so;
723 register struct socket *so2;
725 register struct unpcb *unp = sotounpcb(so);
726 register struct unpcb *unp2;
728 if (so2->so_type != so->so_type)
730 unp2 = sotounpcb(so2);
731 unp->unp_conn = unp2;
732 switch (so->so_type) {
735 LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
740 unp2->unp_conn = unp;
746 panic("unp_connect2");
755 register struct unpcb *unp2 = unp->unp_conn;
760 switch (unp->unp_socket->so_type) {
763 LIST_REMOVE(unp, unp_reflink);
764 unp->unp_socket->so_state &= ~SS_ISCONNECTED;
768 soisdisconnected(unp->unp_socket);
770 soisdisconnected(unp2->unp_socket);
786 prison_unpcb(struct thread *td, struct unpcb *unp)
792 if ((p = td->td_proc) == NULL)
794 if (!p->p_ucred->cr_prison)
796 if (p->p_fd->fd_rdir == unp->unp_rvnode)
802 unp_pcblist(SYSCTL_HANDLER_ARGS)
805 struct unpcb *unp, **unp_list;
808 struct unp_head *head;
810 head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead);
812 KKASSERT(curproc != NULL);
815 * The process of preparing the PCB list is too time-consuming and
816 * resource-intensive to repeat twice on every request.
818 if (req->oldptr == 0) {
820 req->oldidx = 2 * (sizeof xug)
821 + (n + n/8) * sizeof(struct xunpcb);
825 if (req->newptr != 0)
829 * OK, now we're committed to doing something.
834 xug.xug_len = sizeof xug;
836 xug.xug_gen = gencnt;
837 xug.xug_sogen = so_gencnt;
838 error = SYSCTL_OUT(req, &xug, sizeof xug);
842 unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
846 for (unp = LIST_FIRST(head), i = 0; unp && i < n;
847 unp = LIST_NEXT(unp, unp_link)) {
848 if (unp->unp_gencnt <= gencnt && !prison_unpcb(req->td, unp))
851 n = i; /* in case we lost some during malloc */
854 for (i = 0; i < n; i++) {
856 if (unp->unp_gencnt <= gencnt) {
858 xu.xu_len = sizeof xu;
861 * XXX - need more locking here to protect against
862 * connect/disconnect races for SMP.
865 bcopy(unp->unp_addr, &xu.xu_addr,
866 unp->unp_addr->sun_len);
867 if (unp->unp_conn && unp->unp_conn->unp_addr)
868 bcopy(unp->unp_conn->unp_addr,
870 unp->unp_conn->unp_addr->sun_len);
871 bcopy(unp, &xu.xu_unp, sizeof *unp);
872 sotoxsocket(unp->unp_socket, &xu.xu_socket);
873 error = SYSCTL_OUT(req, &xu, sizeof xu);
878 * Give the user an updated idea of our state.
879 * If the generation differs from what we told
880 * her before, she knows that something happened
881 * while we were processing this request, and it
882 * might be necessary to retry.
884 xug.xug_gen = unp_gencnt;
885 xug.xug_sogen = so_gencnt;
886 xug.xug_count = unp_count;
887 error = SYSCTL_OUT(req, &xug, sizeof xug);
889 free(unp_list, M_TEMP);
893 SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD,
894 (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
895 "List of active local datagram sockets");
896 SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD,
897 (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
898 "List of active local stream sockets");
906 if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
907 (so = unp->unp_conn->unp_socket))
916 struct socket *so = unp->unp_socket;
918 so->so_error = errno;
931 unp_externalize(struct mbuf *rights)
933 struct proc *p = curproc; /* XXX */
935 struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
939 int newfds = (cm->cmsg_len - (CMSG_DATA(cm) - (u_char *)cm))
940 / sizeof (struct file *);
944 * if the new FD's will not fit, then we free them all
946 if (!fdavail(p, newfds)) {
947 rp = (struct file **)CMSG_DATA(cm);
948 for (i = 0; i < newfds; i++) {
951 * zero the pointer before calling unp_discard,
952 * since it may end up in unp_gc()..
960 * now change each pointer to an fd in the global table to
961 * an integer that is the index to the local fd table entry
962 * that we set up to point to the global one we are transferring.
963 * If sizeof (struct file *) is bigger than or equal to sizeof int,
964 * then do it in forward order. In that case, an integer will
965 * always come in the same place or before its corresponding
966 * struct file pointer.
967 * If sizeof (struct file *) is smaller than sizeof int, then
968 * do it in reverse order.
970 if (sizeof (struct file *) >= sizeof (int)) {
971 fdp = (int *)(cm + 1);
972 rp = (struct file **)CMSG_DATA(cm);
973 for (i = 0; i < newfds; i++) {
974 if (fdalloc(p, 0, &f))
975 panic("unp_externalize");
977 p->p_fd->fd_ofiles[f] = fp;
983 fdp = (int *)(cm + 1) + newfds - 1;
984 rp = (struct file **)CMSG_DATA(cm) + newfds - 1;
985 for (i = 0; i < newfds; i++) {
986 if (fdalloc(p, 0, &f))
987 panic("unp_externalize");
989 p->p_fd->fd_ofiles[f] = fp;
997 * Adjust length, in case sizeof(struct file *) and sizeof(int)
1000 cm->cmsg_len = CMSG_LEN(newfds * sizeof(int));
1001 rights->m_len = cm->cmsg_len;
1008 unp_zone = zinit("unpcb", sizeof(struct unpcb), nmbclusters, 0, 0);
1011 LIST_INIT(&unp_dhead);
1012 LIST_INIT(&unp_shead);
1016 #define MIN(a,b) (((a)<(b))?(a):(b))
1020 unp_internalize(struct mbuf *control, struct thread *td)
1022 struct proc *p = td->td_proc;
1023 struct filedesc *fdescp;
1024 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1028 struct cmsgcred *cmcred;
1034 if ((cm->cmsg_type != SCM_RIGHTS && cm->cmsg_type != SCM_CREDS) ||
1035 cm->cmsg_level != SOL_SOCKET || cm->cmsg_len != control->m_len)
1039 * Fill in credential information.
1041 if (cm->cmsg_type == SCM_CREDS) {
1042 cmcred = (struct cmsgcred *)(cm + 1);
1043 cmcred->cmcred_pid = p->p_pid;
1044 cmcred->cmcred_uid = p->p_ucred->cr_ruid;
1045 cmcred->cmcred_gid = p->p_ucred->cr_rgid;
1046 cmcred->cmcred_euid = p->p_ucred->cr_uid;
1047 cmcred->cmcred_ngroups = MIN(p->p_ucred->cr_ngroups,
1049 for (i = 0; i < cmcred->cmcred_ngroups; i++)
1050 cmcred->cmcred_groups[i] = p->p_ucred->cr_groups[i];
1054 oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int);
1056 * check that all the FDs passed in refer to legal OPEN files
1057 * If not, reject the entire operation.
1059 fdp = (int *)(cm + 1);
1060 for (i = 0; i < oldfds; i++) {
1062 if ((unsigned)fd >= fdescp->fd_nfiles ||
1063 fdescp->fd_ofiles[fd] == NULL)
1065 if (fdescp->fd_ofiles[fd]->f_type == DTYPE_KQUEUE)
1066 return (EOPNOTSUPP);
1069 * Now replace the integer FDs with pointers to
1070 * the associated global file table entry..
1071 * Allocate a bigger buffer as necessary. But if an cluster is not
1072 * enough, return E2BIG.
1074 newlen = CMSG_LEN(oldfds * sizeof(struct file *));
1075 if (newlen > MCLBYTES)
1077 if (newlen - control->m_len > M_TRAILINGSPACE(control)) {
1078 if (control->m_flags & M_EXT)
1080 MCLGET(control, M_WAIT);
1081 if ((control->m_flags & M_EXT) == 0)
1084 /* copy the data to the cluster */
1085 memcpy(mtod(control, char *), cm, cm->cmsg_len);
1086 cm = mtod(control, struct cmsghdr *);
1090 * Adjust length, in case sizeof(struct file *) and sizeof(int)
1093 control->m_len = cm->cmsg_len = newlen;
1096 * Transform the file descriptors into struct file pointers.
1097 * If sizeof (struct file *) is bigger than or equal to sizeof int,
1098 * then do it in reverse order so that the int won't get until
1100 * If sizeof (struct file *) is smaller than sizeof int, then
1101 * do it in forward order.
1103 if (sizeof (struct file *) >= sizeof (int)) {
1104 fdp = (int *)(cm + 1) + oldfds - 1;
1105 rp = (struct file **)CMSG_DATA(cm) + oldfds - 1;
1106 for (i = 0; i < oldfds; i++) {
1107 fp = fdescp->fd_ofiles[*fdp--];
1114 fdp = (int *)(cm + 1);
1115 rp = (struct file **)CMSG_DATA(cm);
1116 for (i = 0; i < oldfds; i++) {
1117 fp = fdescp->fd_ofiles[*fdp++];
1127 static int unp_defer, unp_gcing;
1132 register struct file *fp, *nextfp;
1133 register struct socket *so;
1134 struct file **extra_ref, **fpp;
1142 * before going through all this, set all FDs to
1143 * be NOT defered and NOT externally accessible
1145 LIST_FOREACH(fp, &filehead, f_list)
1146 fp->f_flag &= ~(FMARK|FDEFER);
1148 LIST_FOREACH(fp, &filehead, f_list) {
1150 * If the file is not open, skip it
1152 if (fp->f_count == 0)
1155 * If we already marked it as 'defer' in a
1156 * previous pass, then try process it this time
1159 if (fp->f_flag & FDEFER) {
1160 fp->f_flag &= ~FDEFER;
1164 * if it's not defered, then check if it's
1165 * already marked.. if so skip it
1167 if (fp->f_flag & FMARK)
1170 * If all references are from messages
1171 * in transit, then skip it. it's not
1172 * externally accessible.
1174 if (fp->f_count == fp->f_msgcount)
1177 * If it got this far then it must be
1178 * externally accessible.
1180 fp->f_flag |= FMARK;
1183 * either it was defered, or it is externally
1184 * accessible and not already marked so.
1185 * Now check if it is possibly one of OUR sockets.
1187 if (fp->f_type != DTYPE_SOCKET ||
1188 (so = (struct socket *)fp->f_data) == 0)
1190 if (so->so_proto->pr_domain != &localdomain ||
1191 (so->so_proto->pr_flags&PR_RIGHTS) == 0)
1194 if (so->so_rcv.sb_flags & SB_LOCK) {
1196 * This is problematical; it's not clear
1197 * we need to wait for the sockbuf to be
1198 * unlocked (on a uniprocessor, at least),
1199 * and it's also not clear what to do
1200 * if sbwait returns an error due to receipt
1201 * of a signal. If sbwait does return
1202 * an error, we'll go into an infinite
1203 * loop. Delete all of this for now.
1205 (void) sbwait(&so->so_rcv);
1210 * So, Ok, it's one of our sockets and it IS externally
1211 * accessible (or was defered). Now we look
1212 * to see if we hold any file descriptors in its
1213 * message buffers. Follow those links and mark them
1214 * as accessible too.
1216 unp_scan(so->so_rcv.sb_mb, unp_mark);
1218 } while (unp_defer);
1220 * We grab an extra reference to each of the file table entries
1221 * that are not otherwise accessible and then free the rights
1222 * that are stored in messages on them.
1224 * The bug in the orginal code is a little tricky, so I'll describe
1225 * what's wrong with it here.
1227 * It is incorrect to simply unp_discard each entry for f_msgcount
1228 * times -- consider the case of sockets A and B that contain
1229 * references to each other. On a last close of some other socket,
1230 * we trigger a gc since the number of outstanding rights (unp_rights)
1231 * is non-zero. If during the sweep phase the gc code un_discards,
1232 * we end up doing a (full) closef on the descriptor. A closef on A
1233 * results in the following chain. Closef calls soo_close, which
1234 * calls soclose. Soclose calls first (through the switch
1235 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply
1236 * returns because the previous instance had set unp_gcing, and
1237 * we return all the way back to soclose, which marks the socket
1238 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush
1239 * to free up the rights that are queued in messages on the socket A,
1240 * i.e., the reference on B. The sorflush calls via the dom_dispose
1241 * switch unp_dispose, which unp_scans with unp_discard. This second
1242 * instance of unp_discard just calls closef on B.
1244 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1245 * which results in another closef on A. Unfortunately, A is already
1246 * being closed, and the descriptor has already been marked with
1247 * SS_NOFDREF, and soclose panics at this point.
1249 * Here, we first take an extra reference to each inaccessible
1250 * descriptor. Then, we call sorflush ourself, since we know
1251 * it is a Unix domain socket anyhow. After we destroy all the
1252 * rights carried in messages, we do a last closef to get rid
1253 * of our extra reference. This is the last close, and the
1254 * unp_detach etc will shut down the socket.
1256 * 91/09/19, bsy@cs.cmu.edu
1258 extra_ref = malloc(nfiles * sizeof(struct file *), M_FILE, M_WAITOK);
1259 for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; fp != 0;
1261 nextfp = LIST_NEXT(fp, f_list);
1263 * If it's not open, skip it
1265 if (fp->f_count == 0)
1268 * If all refs are from msgs, and it's not marked accessible
1269 * then it must be referenced from some unreachable cycle
1270 * of (shut-down) FDs, so include it in our
1271 * list of FDs to remove
1273 if (fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) {
1280 * for each FD on our hit list, do the following two things
1282 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
1283 struct file *tfp = *fpp;
1284 if (tfp->f_type == DTYPE_SOCKET && tfp->f_data != NULL)
1285 sorflush((struct socket *)(tfp->f_data));
1287 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp)
1289 free((caddr_t)extra_ref, M_FILE);
1294 unp_dispose(struct mbuf *m)
1297 unp_scan(m, unp_discard);
1301 unp_listen(struct unpcb *unp, struct thread *td)
1303 struct proc *p = td->td_proc;
1306 cru2x(p->p_ucred, &unp->unp_peercred);
1307 unp->unp_flags |= UNP_HAVEPCCACHED;
1314 void (*op) __P((struct file *));
1323 for (m = m0; m; m = m->m_next)
1324 if (m->m_type == MT_CONTROL &&
1325 m->m_len >= sizeof(*cm)) {
1326 cm = mtod(m, struct cmsghdr *);
1327 if (cm->cmsg_level != SOL_SOCKET ||
1328 cm->cmsg_type != SCM_RIGHTS)
1330 qfds = (cm->cmsg_len -
1331 (CMSG_DATA(cm) - (u_char *)cm))
1332 / sizeof (struct file *);
1333 rp = (struct file **)CMSG_DATA(cm);
1334 for (i = 0; i < qfds; i++)
1336 break; /* XXX, but saves time */
1347 if (fp->f_flag & FMARK)
1350 fp->f_flag |= (FMARK|FDEFER);
1360 (void) closef(fp, NULL);