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
39 #include <sys/param.h>
40 #include <sys/domain.h>
41 #include <sys/fcntl.h>
42 #include <sys/malloc.h> /* XXX must be before <sys/file.h> */
44 #include <sys/filedesc.h>
46 #include <sys/kernel.h>
49 #include <sys/mutex.h>
50 #include <sys/namei.h>
52 #include <sys/protosw.h>
53 #include <sys/resourcevar.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/signalvar.h>
59 #include <sys/sysctl.h>
60 #include <sys/systm.h>
62 #include <sys/unpcb.h>
63 #include <sys/vnode.h>
67 static uma_zone_t unp_zone;
68 static unp_gen_t unp_gencnt;
69 static u_int unp_count;
71 static struct unp_head unp_shead, unp_dhead;
74 * Unix communications domain.
78 * rethink name space problems
79 * need a proper out-of-band
82 static struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL };
83 static ino_t unp_ino; /* prototype for fake inode numbers */
85 static int unp_attach(struct socket *);
86 static void unp_detach(struct unpcb *);
87 static int unp_bind(struct unpcb *,struct sockaddr *, struct thread *);
88 static int unp_connect(struct socket *,struct sockaddr *, struct thread *);
89 static void unp_disconnect(struct unpcb *);
90 static void unp_shutdown(struct unpcb *);
91 static void unp_drop(struct unpcb *, int);
92 static void unp_gc(void);
93 static void unp_scan(struct mbuf *, void (*)(struct file *));
94 static void unp_mark(struct file *);
95 static void unp_discard(struct file *);
96 static void unp_freerights(struct file **, int);
97 static int unp_internalize(struct mbuf **, struct thread *);
98 static int unp_listen(struct unpcb *, struct thread *);
101 uipc_abort(struct socket *so)
103 struct unpcb *unp = sotounpcb(so);
107 unp_drop(unp, ECONNABORTED);
114 uipc_accept(struct socket *so, struct sockaddr **nam)
116 struct unpcb *unp = sotounpcb(so);
122 * Pass back name of connected socket,
123 * if it was bound and we are still connected
124 * (our peer may have closed already!).
126 if (unp->unp_conn && unp->unp_conn->unp_addr) {
127 *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr,
130 *nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1);
136 uipc_attach(struct socket *so, int proto, struct thread *td)
138 struct unpcb *unp = sotounpcb(so);
142 return unp_attach(so);
146 uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
148 struct unpcb *unp = sotounpcb(so);
153 return unp_bind(unp, nam, td);
157 uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
159 struct unpcb *unp = sotounpcb(so);
163 return unp_connect(so, nam, curthread);
167 uipc_connect2(struct socket *so1, struct socket *so2)
169 struct unpcb *unp = sotounpcb(so1);
174 return unp_connect2(so1, so2);
177 /* control is EOPNOTSUPP */
180 uipc_detach(struct socket *so)
182 struct unpcb *unp = sotounpcb(so);
192 uipc_disconnect(struct socket *so)
194 struct unpcb *unp = sotounpcb(so);
203 uipc_listen(struct socket *so, struct thread *td)
205 struct unpcb *unp = sotounpcb(so);
207 if (unp == 0 || unp->unp_vnode == 0)
209 return unp_listen(unp, td);
213 uipc_peeraddr(struct socket *so, struct sockaddr **nam)
215 struct unpcb *unp = sotounpcb(so);
219 if (unp->unp_conn && unp->unp_conn->unp_addr)
220 *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr,
226 uipc_rcvd(struct socket *so, int flags)
228 struct unpcb *unp = sotounpcb(so);
234 switch (so->so_type) {
236 panic("uipc_rcvd DGRAM?");
240 if (unp->unp_conn == 0)
242 so2 = unp->unp_conn->unp_socket;
244 * Adjust backpressure on sender
245 * and wakeup any waiting to write.
247 so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt;
248 unp->unp_mbcnt = so->so_rcv.sb_mbcnt;
249 newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc -
251 (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat,
252 newhiwat, RLIM_INFINITY);
253 unp->unp_cc = so->so_rcv.sb_cc;
258 panic("uipc_rcvd unknown socktype");
263 /* pru_rcvoob is EOPNOTSUPP */
266 uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
267 struct mbuf *control, struct thread *td)
270 struct unpcb *unp = sotounpcb(so);
278 if (flags & PRUS_OOB) {
283 if (control && (error = unp_internalize(&control, td)))
286 switch (so->so_type) {
289 struct sockaddr *from;
296 error = unp_connect(so, nam, td);
300 if (unp->unp_conn == 0) {
305 so2 = unp->unp_conn->unp_socket;
307 from = (struct sockaddr *)unp->unp_addr;
310 if (sbappendaddr(&so2->so_rcv, from, m, control)) {
322 /* Connect if not connected yet. */
324 * Note: A better implementation would complain
325 * if not equal to the peer's address.
327 if ((so->so_state & SS_ISCONNECTED) == 0) {
329 error = unp_connect(so, nam, td);
338 if (so->so_state & SS_CANTSENDMORE) {
342 if (unp->unp_conn == 0)
343 panic("uipc_send connected but no connection?");
344 so2 = unp->unp_conn->unp_socket;
346 * Send to paired receive port, and then reduce
347 * send buffer hiwater marks to maintain backpressure.
351 if (sbappendcontrol(&so2->so_rcv, m, control))
354 sbappend(&so2->so_rcv, m);
355 so->so_snd.sb_mbmax -=
356 so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt;
357 unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt;
358 newhiwat = so->so_snd.sb_hiwat -
359 (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc);
360 (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat,
361 newhiwat, RLIM_INFINITY);
362 unp->unp_conn->unp_cc = so2->so_rcv.sb_cc;
368 panic("uipc_send unknown socktype");
372 * SEND_EOF is equivalent to a SEND followed by
375 if (flags & PRUS_EOF) {
380 if (control && error != 0)
381 unp_dispose(control);
392 uipc_sense(struct socket *so, struct stat *sb)
394 struct unpcb *unp = sotounpcb(so);
399 sb->st_blksize = so->so_snd.sb_hiwat;
400 if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
401 so2 = unp->unp_conn->unp_socket;
402 sb->st_blksize += so2->so_rcv.sb_cc;
405 if (unp->unp_ino == 0)
406 unp->unp_ino = unp_ino++;
407 sb->st_ino = unp->unp_ino;
412 uipc_shutdown(struct socket *so)
414 struct unpcb *unp = sotounpcb(so);
424 uipc_sockaddr(struct socket *so, struct sockaddr **nam)
426 struct unpcb *unp = sotounpcb(so);
431 *nam = dup_sockaddr((struct sockaddr *)unp->unp_addr, 1);
433 *nam = dup_sockaddr((struct sockaddr *)&sun_noname, 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 = uma_zalloc(unp_zone, M_WAITOK);
538 bzero(unp, sizeof *unp);
539 unp->unp_gencnt = ++unp_gencnt;
541 LIST_INIT(&unp->unp_refs);
542 unp->unp_socket = so;
543 FILEDESC_LOCK(curproc->p_fd);
544 unp->unp_rvnode = curthread->td_proc->p_fd->fd_rdir;
545 FILEDESC_UNLOCK(curproc->p_fd);
546 LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead
547 : &unp_shead, unp, unp_link);
554 register struct unpcb *unp;
556 LIST_REMOVE(unp, unp_link);
557 unp->unp_gencnt = ++unp_gencnt;
559 if (unp->unp_vnode) {
560 unp->unp_vnode->v_socket = 0;
561 vrele(unp->unp_vnode);
566 while (!LIST_EMPTY(&unp->unp_refs))
567 unp_drop(LIST_FIRST(&unp->unp_refs), ECONNRESET);
568 soisdisconnected(unp->unp_socket);
569 unp->unp_socket->so_pcb = 0;
572 * Normally the receive buffer is flushed later,
573 * in sofree, but if our receive buffer holds references
574 * to descriptors that are now garbage, we will dispose
575 * of those descriptor references after the garbage collector
576 * gets them (resulting in a "panic: closef: count < 0").
578 sorflush(unp->unp_socket);
582 FREE(unp->unp_addr, M_SONAME);
583 uma_zfree(unp_zone, unp);
587 unp_bind(unp, nam, td)
589 struct sockaddr *nam;
592 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
600 if (unp->unp_vnode != NULL)
602 namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
605 buf = malloc(SOCK_MAXADDRLEN, M_TEMP, M_WAITOK);
606 strncpy(buf, soun->sun_path, namelen);
607 buf[namelen] = 0; /* null-terminate the string */
609 NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT, UIO_SYSSPACE,
611 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
618 if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
619 NDFREE(&nd, NDF_ONLY_PNBUF);
629 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
637 vattr.va_type = VSOCK;
638 FILEDESC_LOCK(td->td_proc->p_fd);
639 vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask);
640 FILEDESC_UNLOCK(td->td_proc->p_fd);
641 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
642 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
643 NDFREE(&nd, NDF_ONLY_PNBUF);
650 vp->v_socket = unp->unp_socket;
652 unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam, 1);
653 VOP_UNLOCK(vp, 0, td);
654 vn_finished_write(mp);
660 unp_connect(so, nam, td)
662 struct sockaddr *nam;
665 register struct sockaddr_un *soun = (struct sockaddr_un *)nam;
666 register struct vnode *vp;
667 register struct socket *so2, *so3;
668 struct unpcb *unp, *unp2, *unp3;
671 char buf[SOCK_MAXADDRLEN];
673 len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
676 strncpy(buf, soun->sun_path, len);
679 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td);
684 NDFREE(&nd, NDF_ONLY_PNBUF);
685 if (vp->v_type != VSOCK) {
689 error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td);
694 error = ECONNREFUSED;
697 if (so->so_type != so2->so_type) {
701 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
702 if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
703 (so3 = sonewconn(so2, 0)) == 0) {
704 error = ECONNREFUSED;
708 unp2 = sotounpcb(so2);
709 unp3 = sotounpcb(so3);
711 unp3->unp_addr = (struct sockaddr_un *)
712 dup_sockaddr((struct sockaddr *)
716 * unp_peercred management:
718 * The connecter's (client's) credentials are copied
719 * from its process structure at the time of connect()
722 cru2x(td->td_ucred, &unp3->unp_peercred);
723 unp3->unp_flags |= UNP_HAVEPC;
725 * The receiver's (server's) credentials are copied
726 * from the unp_peercred member of socket on which the
727 * former called listen(); unp_listen() cached that
728 * process's credentials at that time so we can use
731 KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED,
732 ("unp_connect: listener without cached peercred"));
733 memcpy(&unp->unp_peercred, &unp2->unp_peercred,
734 sizeof(unp->unp_peercred));
735 unp->unp_flags |= UNP_HAVEPC;
737 mac_set_socket_peer_from_socket(so, so3);
738 mac_set_socket_peer_from_socket(so3, so);
743 error = unp_connect2(so, so2);
750 unp_connect2(so, so2)
751 register struct socket *so;
752 register struct socket *so2;
754 register struct unpcb *unp = sotounpcb(so);
755 register struct unpcb *unp2;
757 if (so2->so_type != so->so_type)
759 unp2 = sotounpcb(so2);
760 unp->unp_conn = unp2;
761 switch (so->so_type) {
764 LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
769 unp2->unp_conn = unp;
775 panic("unp_connect2");
784 register struct unpcb *unp2 = unp->unp_conn;
789 switch (unp->unp_socket->so_type) {
792 LIST_REMOVE(unp, unp_reflink);
793 unp->unp_socket->so_state &= ~SS_ISCONNECTED;
797 soisdisconnected(unp->unp_socket);
799 soisdisconnected(unp2->unp_socket);
815 unp_pcblist(SYSCTL_HANDLER_ARGS)
818 struct unpcb *unp, **unp_list;
821 struct unp_head *head;
824 head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead);
827 * The process of preparing the PCB list is too time-consuming and
828 * resource-intensive to repeat twice on every request.
830 if (req->oldptr == 0) {
832 req->oldidx = 2 * (sizeof *xug)
833 + (n + n/8) * sizeof(struct xunpcb);
837 if (req->newptr != 0)
841 * OK, now we're committed to doing something.
843 xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK);
847 xug->xug_len = sizeof *xug;
849 xug->xug_gen = gencnt;
850 xug->xug_sogen = so_gencnt;
851 error = SYSCTL_OUT(req, xug, sizeof *xug);
857 unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
859 for (unp = LIST_FIRST(head), i = 0; unp && i < n;
860 unp = LIST_NEXT(unp, unp_link)) {
861 if (unp->unp_gencnt <= gencnt) {
862 if (cr_cansee(req->td->td_ucred,
863 unp->unp_socket->so_cred))
868 n = i; /* in case we lost some during malloc */
871 xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK);
872 for (i = 0; i < n; i++) {
874 if (unp->unp_gencnt <= gencnt) {
875 xu->xu_len = sizeof *xu;
878 * XXX - need more locking here to protect against
879 * connect/disconnect races for SMP.
882 bcopy(unp->unp_addr, &xu->xu_addr,
883 unp->unp_addr->sun_len);
884 if (unp->unp_conn && unp->unp_conn->unp_addr)
885 bcopy(unp->unp_conn->unp_addr,
887 unp->unp_conn->unp_addr->sun_len);
888 bcopy(unp, &xu->xu_unp, sizeof *unp);
889 sotoxsocket(unp->unp_socket, &xu->xu_socket);
890 error = SYSCTL_OUT(req, xu, sizeof *xu);
896 * Give the user an updated idea of our state.
897 * If the generation differs from what we told
898 * her before, she knows that something happened
899 * while we were processing this request, and it
900 * might be necessary to retry.
902 xug->xug_gen = unp_gencnt;
903 xug->xug_sogen = so_gencnt;
904 xug->xug_count = unp_count;
905 error = SYSCTL_OUT(req, xug, sizeof *xug);
907 free(unp_list, M_TEMP);
912 SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD,
913 (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
914 "List of active local datagram sockets");
915 SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD,
916 (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
917 "List of active local stream sockets");
925 if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
926 (so = unp->unp_conn->unp_socket))
935 struct socket *so = unp->unp_socket;
937 so->so_error = errno;
950 unp_freerights(rp, fdcount)
957 for (i = 0; i < fdcount; i++) {
960 * zero the pointer before calling
961 * unp_discard since it may end up
970 unp_externalize(control, controlp)
971 struct mbuf *control, **controlp;
973 struct thread *td = curthread; /* XXX */
974 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
980 socklen_t clen = control->m_len, datalen;
986 if (controlp != NULL) /* controlp == NULL => free control messages */
990 if (sizeof(*cm) > clen || cm->cmsg_len > clen) {
995 data = CMSG_DATA(cm);
996 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
998 if (cm->cmsg_level == SOL_SOCKET
999 && cm->cmsg_type == SCM_RIGHTS) {
1000 newfds = datalen / sizeof(struct file *);
1003 /* If we're not outputting the discriptors free them. */
1004 if (error || controlp == NULL) {
1005 unp_freerights(rp, newfds);
1008 FILEDESC_LOCK(td->td_proc->p_fd);
1009 /* if the new FD's will not fit free them. */
1010 if (!fdavail(td, newfds)) {
1011 FILEDESC_UNLOCK(td->td_proc->p_fd);
1013 unp_freerights(rp, newfds);
1017 * now change each pointer to an fd in the global
1018 * table to an integer that is the index to the
1019 * local fd table entry that we set up to point
1020 * to the global one we are transferring.
1022 newlen = newfds * sizeof(int);
1023 *controlp = sbcreatecontrol(NULL, newlen,
1024 SCM_RIGHTS, SOL_SOCKET);
1025 if (*controlp == NULL) {
1026 FILEDESC_UNLOCK(td->td_proc->p_fd);
1028 unp_freerights(rp, newfds);
1033 CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1034 for (i = 0; i < newfds; i++) {
1035 if (fdalloc(td, 0, &f))
1036 panic("unp_externalize fdalloc failed");
1038 td->td_proc->p_fd->fd_ofiles[f] = fp;
1045 FILEDESC_UNLOCK(td->td_proc->p_fd);
1046 } else { /* We can just copy anything else across */
1047 if (error || controlp == NULL)
1049 *controlp = sbcreatecontrol(NULL, datalen,
1050 cm->cmsg_type, cm->cmsg_level);
1051 if (*controlp == NULL) {
1056 CMSG_DATA(mtod(*controlp, struct cmsghdr *)),
1060 controlp = &(*controlp)->m_next;
1063 if (CMSG_SPACE(datalen) < clen) {
1064 clen -= CMSG_SPACE(datalen);
1065 cm = (struct cmsghdr *)
1066 ((caddr_t)cm + CMSG_SPACE(datalen));
1081 unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL,
1082 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1083 uma_zone_set_max(unp_zone, nmbclusters);
1086 LIST_INIT(&unp_dhead);
1087 LIST_INIT(&unp_shead);
1091 #define MIN(a,b) (((a)<(b))?(a):(b))
1095 unp_internalize(controlp, td)
1096 struct mbuf **controlp;
1099 struct mbuf *control = *controlp;
1100 struct proc *p = td->td_proc;
1101 struct filedesc *fdescp = p->p_fd;
1102 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1103 struct cmsgcred *cmcred;
1109 socklen_t clen = control->m_len, datalen;
1116 while (cm != NULL) {
1117 if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET
1118 || cm->cmsg_len > clen) {
1123 data = CMSG_DATA(cm);
1124 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1126 switch (cm->cmsg_type) {
1128 * Fill in credential information.
1131 *controlp = sbcreatecontrol(NULL, sizeof(*cmcred),
1132 SCM_CREDS, SOL_SOCKET);
1133 if (*controlp == NULL) {
1138 cmcred = (struct cmsgcred *)
1139 CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1140 cmcred->cmcred_pid = p->p_pid;
1141 cmcred->cmcred_uid = td->td_ucred->cr_ruid;
1142 cmcred->cmcred_gid = td->td_ucred->cr_rgid;
1143 cmcred->cmcred_euid = td->td_ucred->cr_uid;
1144 cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups,
1146 for (i = 0; i < cmcred->cmcred_ngroups; i++)
1147 cmcred->cmcred_groups[i] =
1148 td->td_ucred->cr_groups[i];
1152 oldfds = datalen / sizeof (int);
1154 * check that all the FDs passed in refer to legal files
1155 * If not, reject the entire operation.
1158 FILEDESC_LOCK(fdescp);
1159 for (i = 0; i < oldfds; i++) {
1161 if ((unsigned)fd >= fdescp->fd_nfiles ||
1162 fdescp->fd_ofiles[fd] == NULL) {
1163 FILEDESC_UNLOCK(fdescp);
1169 * Now replace the integer FDs with pointers to
1170 * the associated global file table entry..
1172 newlen = oldfds * sizeof(struct file *);
1173 *controlp = sbcreatecontrol(NULL, newlen,
1174 SCM_RIGHTS, SOL_SOCKET);
1175 if (*controlp == NULL) {
1176 FILEDESC_UNLOCK(fdescp);
1182 rp = (struct file **)
1183 CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1184 for (i = 0; i < oldfds; i++) {
1185 fp = fdescp->fd_ofiles[*fdp++];
1193 FILEDESC_UNLOCK(fdescp);
1197 *controlp = sbcreatecontrol(NULL, sizeof(*tv),
1198 SCM_TIMESTAMP, SOL_SOCKET);
1199 if (*controlp == NULL) {
1203 tv = (struct timeval *)
1204 CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1213 controlp = &(*controlp)->m_next;
1215 if (CMSG_SPACE(datalen) < clen) {
1216 clen -= CMSG_SPACE(datalen);
1217 cm = (struct cmsghdr *)
1218 ((caddr_t)cm + CMSG_SPACE(datalen));
1231 static int unp_defer, unp_gcing;
1236 register struct file *fp, *nextfp;
1237 register struct socket *so;
1238 struct file **extra_ref, **fpp;
1246 * before going through all this, set all FDs to
1247 * be NOT defered and NOT externally accessible
1249 sx_slock(&filelist_lock);
1250 LIST_FOREACH(fp, &filehead, f_list)
1251 fp->f_gcflag &= ~(FMARK|FDEFER);
1253 LIST_FOREACH(fp, &filehead, f_list) {
1256 * If the file is not open, skip it
1258 if (fp->f_count == 0) {
1263 * If we already marked it as 'defer' in a
1264 * previous pass, then try process it this time
1267 if (fp->f_gcflag & FDEFER) {
1268 fp->f_gcflag &= ~FDEFER;
1272 * if it's not defered, then check if it's
1273 * already marked.. if so skip it
1275 if (fp->f_gcflag & FMARK) {
1280 * If all references are from messages
1281 * in transit, then skip it. it's not
1282 * externally accessible.
1284 if (fp->f_count == fp->f_msgcount) {
1289 * If it got this far then it must be
1290 * externally accessible.
1292 fp->f_gcflag |= FMARK;
1295 * either it was defered, or it is externally
1296 * accessible and not already marked so.
1297 * Now check if it is possibly one of OUR sockets.
1299 if (fp->f_type != DTYPE_SOCKET ||
1300 (so = (struct socket *)fp->f_data) == 0) {
1305 if (so->so_proto->pr_domain != &localdomain ||
1306 (so->so_proto->pr_flags&PR_RIGHTS) == 0)
1309 if (so->so_rcv.sb_flags & SB_LOCK) {
1311 * This is problematical; it's not clear
1312 * we need to wait for the sockbuf to be
1313 * unlocked (on a uniprocessor, at least),
1314 * and it's also not clear what to do
1315 * if sbwait returns an error due to receipt
1316 * of a signal. If sbwait does return
1317 * an error, we'll go into an infinite
1318 * loop. Delete all of this for now.
1320 (void) sbwait(&so->so_rcv);
1325 * So, Ok, it's one of our sockets and it IS externally
1326 * accessible (or was defered). Now we look
1327 * to see if we hold any file descriptors in its
1328 * message buffers. Follow those links and mark them
1329 * as accessible too.
1331 unp_scan(so->so_rcv.sb_mb, unp_mark);
1333 } while (unp_defer);
1334 sx_sunlock(&filelist_lock);
1336 * We grab an extra reference to each of the file table entries
1337 * that are not otherwise accessible and then free the rights
1338 * that are stored in messages on them.
1340 * The bug in the orginal code is a little tricky, so I'll describe
1341 * what's wrong with it here.
1343 * It is incorrect to simply unp_discard each entry for f_msgcount
1344 * times -- consider the case of sockets A and B that contain
1345 * references to each other. On a last close of some other socket,
1346 * we trigger a gc since the number of outstanding rights (unp_rights)
1347 * is non-zero. If during the sweep phase the gc code un_discards,
1348 * we end up doing a (full) closef on the descriptor. A closef on A
1349 * results in the following chain. Closef calls soo_close, which
1350 * calls soclose. Soclose calls first (through the switch
1351 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply
1352 * returns because the previous instance had set unp_gcing, and
1353 * we return all the way back to soclose, which marks the socket
1354 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush
1355 * to free up the rights that are queued in messages on the socket A,
1356 * i.e., the reference on B. The sorflush calls via the dom_dispose
1357 * switch unp_dispose, which unp_scans with unp_discard. This second
1358 * instance of unp_discard just calls closef on B.
1360 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1361 * which results in another closef on A. Unfortunately, A is already
1362 * being closed, and the descriptor has already been marked with
1363 * SS_NOFDREF, and soclose panics at this point.
1365 * Here, we first take an extra reference to each inaccessible
1366 * descriptor. Then, we call sorflush ourself, since we know
1367 * it is a Unix domain socket anyhow. After we destroy all the
1368 * rights carried in messages, we do a last closef to get rid
1369 * of our extra reference. This is the last close, and the
1370 * unp_detach etc will shut down the socket.
1372 * 91/09/19, bsy@cs.cmu.edu
1374 extra_ref = malloc(nfiles * sizeof(struct file *), M_TEMP, M_WAITOK);
1375 sx_slock(&filelist_lock);
1376 for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; fp != 0;
1378 nextfp = LIST_NEXT(fp, f_list);
1381 * If it's not open, skip it
1383 if (fp->f_count == 0) {
1388 * If all refs are from msgs, and it's not marked accessible
1389 * then it must be referenced from some unreachable cycle
1390 * of (shut-down) FDs, so include it in our
1391 * list of FDs to remove
1393 if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) {
1400 sx_sunlock(&filelist_lock);
1402 * for each FD on our hit list, do the following two things
1404 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
1405 struct file *tfp = *fpp;
1407 if (tfp->f_type == DTYPE_SOCKET && tfp->f_data != NULL) {
1409 sorflush((struct socket *)(tfp->f_data));
1413 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp)
1414 closef(*fpp, (struct thread *) NULL);
1415 free(extra_ref, M_TEMP);
1425 unp_scan(m, unp_discard);
1434 cru2x(td->td_ucred, &unp->unp_peercred);
1435 unp->unp_flags |= UNP_HAVEPCCACHED;
1441 register struct mbuf *m0;
1442 void (*op)(struct file *);
1449 socklen_t clen, datalen;
1453 for (m = m0; m; m = m->m_next) {
1454 if (m->m_type != MT_CONTROL)
1457 cm = mtod(m, struct cmsghdr *);
1460 while (cm != NULL) {
1461 if (sizeof(*cm) > clen || cm->cmsg_len > clen)
1464 data = CMSG_DATA(cm);
1465 datalen = (caddr_t)cm + cm->cmsg_len
1468 if (cm->cmsg_level == SOL_SOCKET &&
1469 cm->cmsg_type == SCM_RIGHTS) {
1470 qfds = datalen / sizeof (struct file *);
1472 for (i = 0; i < qfds; i++)
1476 if (CMSG_SPACE(datalen) < clen) {
1477 clen -= CMSG_SPACE(datalen);
1478 cm = (struct cmsghdr *)
1479 ((caddr_t)cm + CMSG_SPACE(datalen));
1494 if (fp->f_gcflag & FMARK)
1497 fp->f_gcflag |= (FMARK|FDEFER);
1508 (void) closef(fp, (struct thread *)NULL);