2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California.
4 * Copyright (c) 2004-2009 Robert N. M. Watson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94
35 * UNIX Domain (Local) Sockets
37 * This is an implementation of UNIX (local) domain sockets. Each socket has
38 * an associated struct unpcb (UNIX protocol control block). Stream sockets
39 * may be connected to 0 or 1 other socket. Datagram sockets may be
40 * connected to 0, 1, or many other sockets. Sockets may be created and
41 * connected in pairs (socketpair(2)), or bound/connected to using the file
42 * system name space. For most purposes, only the receive socket buffer is
43 * used, as sending on one socket delivers directly to the receive socket
44 * buffer of a second socket.
46 * The implementation is substantially complicated by the fact that
47 * "ancillary data", such as file descriptors or credentials, may be passed
48 * across UNIX domain sockets. The potential for passing UNIX domain sockets
49 * over other UNIX domain sockets requires the implementation of a simple
50 * garbage collector to find and tear down cycles of disconnected sockets.
54 * distinguish datagram size limits from flow control limits in SEQPACKET
55 * rethink name space problems
56 * need a proper out-of-band
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
64 #include <sys/param.h>
65 #include <sys/domain.h>
66 #include <sys/fcntl.h>
67 #include <sys/malloc.h> /* XXX must be before <sys/file.h> */
68 #include <sys/eventhandler.h>
70 #include <sys/filedesc.h>
71 #include <sys/kernel.h>
74 #include <sys/mount.h>
75 #include <sys/mutex.h>
76 #include <sys/namei.h>
78 #include <sys/protosw.h>
79 #include <sys/queue.h>
80 #include <sys/resourcevar.h>
81 #include <sys/rwlock.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
84 #include <sys/signalvar.h>
87 #include <sys/sysctl.h>
88 #include <sys/systm.h>
89 #include <sys/taskqueue.h>
91 #include <sys/unpcb.h>
92 #include <sys/vnode.h>
100 #include <security/mac/mac_framework.h>
106 * (l) Locked using list lock
107 * (g) Locked using linkage lock
110 static uma_zone_t unp_zone;
111 static unp_gen_t unp_gencnt; /* (l) */
112 static u_int unp_count; /* (l) Count of local sockets. */
113 static ino_t unp_ino; /* Prototype for fake inode numbers. */
114 static int unp_rights; /* (g) File descriptors in flight. */
115 static struct unp_head unp_shead; /* (l) List of stream sockets. */
116 static struct unp_head unp_dhead; /* (l) List of datagram sockets. */
117 static struct unp_head unp_sphead; /* (l) List of seqpacket sockets. */
120 SLIST_ENTRY(unp_defer) ud_link;
123 static SLIST_HEAD(, unp_defer) unp_defers;
124 static int unp_defers_count;
126 static const struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL };
129 * Garbage collection of cyclic file descriptor/socket references occurs
130 * asynchronously in a taskqueue context in order to avoid recursion and
131 * reentrance in the UNIX domain socket, file descriptor, and socket layer
132 * code. See unp_gc() for a full description.
134 static struct timeout_task unp_gc_task;
137 * The close of unix domain sockets attached as SCM_RIGHTS is
138 * postponed to the taskqueue, to avoid arbitrary recursion depth.
139 * The attached sockets might have another sockets attached.
141 static struct task unp_defer_task;
144 * Both send and receive buffers are allocated PIPSIZ bytes of buffering for
145 * stream sockets, although the total for sender and receiver is actually
148 * Datagram sockets really use the sendspace as the maximum datagram size,
149 * and don't really want to reserve the sendspace. Their recvspace should be
150 * large enough for at least one max-size datagram plus address.
155 static u_long unpst_sendspace = PIPSIZ;
156 static u_long unpst_recvspace = PIPSIZ;
157 static u_long unpdg_sendspace = 2*1024; /* really max datagram size */
158 static u_long unpdg_recvspace = 4*1024;
159 static u_long unpsp_sendspace = PIPSIZ; /* really max datagram size */
160 static u_long unpsp_recvspace = PIPSIZ;
162 static SYSCTL_NODE(_net, PF_LOCAL, local, CTLFLAG_RW, 0, "Local domain");
163 static SYSCTL_NODE(_net_local, SOCK_STREAM, stream, CTLFLAG_RW, 0,
165 static SYSCTL_NODE(_net_local, SOCK_DGRAM, dgram, CTLFLAG_RW, 0, "SOCK_DGRAM");
166 static SYSCTL_NODE(_net_local, SOCK_SEQPACKET, seqpacket, CTLFLAG_RW, 0,
169 SYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
170 &unpst_sendspace, 0, "Default stream send space.");
171 SYSCTL_ULONG(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
172 &unpst_recvspace, 0, "Default stream receive space.");
173 SYSCTL_ULONG(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
174 &unpdg_sendspace, 0, "Default datagram send space.");
175 SYSCTL_ULONG(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
176 &unpdg_recvspace, 0, "Default datagram receive space.");
177 SYSCTL_ULONG(_net_local_seqpacket, OID_AUTO, maxseqpacket, CTLFLAG_RW,
178 &unpsp_sendspace, 0, "Default seqpacket send space.");
179 SYSCTL_ULONG(_net_local_seqpacket, OID_AUTO, recvspace, CTLFLAG_RW,
180 &unpsp_recvspace, 0, "Default seqpacket receive space.");
181 SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0,
182 "File descriptors in flight.");
183 SYSCTL_INT(_net_local, OID_AUTO, deferred, CTLFLAG_RD,
184 &unp_defers_count, 0,
185 "File descriptors deferred to taskqueue for close.");
188 * Locking and synchronization:
190 * Three types of locks exit in the local domain socket implementation: a
191 * global list mutex, a global linkage rwlock, and per-unpcb mutexes. Of the
192 * global locks, the list lock protects the socket count, global generation
193 * number, and stream/datagram global lists. The linkage lock protects the
194 * interconnection of unpcbs, the v_socket and unp_vnode pointers, and can be
195 * held exclusively over the acquisition of multiple unpcb locks to prevent
198 * UNIX domain sockets each have an unpcb hung off of their so_pcb pointer,
199 * allocated in pru_attach() and freed in pru_detach(). The validity of that
200 * pointer is an invariant, so no lock is required to dereference the so_pcb
201 * pointer if a valid socket reference is held by the caller. In practice,
202 * this is always true during operations performed on a socket. Each unpcb
203 * has a back-pointer to its socket, unp_socket, which will be stable under
204 * the same circumstances.
206 * This pointer may only be safely dereferenced as long as a valid reference
207 * to the unpcb is held. Typically, this reference will be from the socket,
208 * or from another unpcb when the referring unpcb's lock is held (in order
209 * that the reference not be invalidated during use). For example, to follow
210 * unp->unp_conn->unp_socket, you need unlock the lock on unp, not unp_conn,
211 * as unp_socket remains valid as long as the reference to unp_conn is valid.
213 * Fields of unpcbss are locked using a per-unpcb lock, unp_mtx. Individual
214 * atomic reads without the lock may be performed "lockless", but more
215 * complex reads and read-modify-writes require the mutex to be held. No
216 * lock order is defined between unpcb locks -- multiple unpcb locks may be
217 * acquired at the same time only when holding the linkage rwlock
218 * exclusively, which prevents deadlocks.
220 * Blocking with UNIX domain sockets is a tricky issue: unlike most network
221 * protocols, bind() is a non-atomic operation, and connect() requires
222 * potential sleeping in the protocol, due to potentially waiting on local or
223 * distributed file systems. We try to separate "lookup" operations, which
224 * may sleep, and the IPC operations themselves, which typically can occur
225 * with relative atomicity as locks can be held over the entire operation.
227 * Another tricky issue is simultaneous multi-threaded or multi-process
228 * access to a single UNIX domain socket. These are handled by the flags
229 * UNP_CONNECTING and UNP_BINDING, which prevent concurrent connecting or
230 * binding, both of which involve dropping UNIX domain socket locks in order
231 * to perform namei() and other file system operations.
233 static struct rwlock unp_link_rwlock;
234 static struct mtx unp_list_lock;
235 static struct mtx unp_defers_lock;
237 #define UNP_LINK_LOCK_INIT() rw_init(&unp_link_rwlock, \
240 #define UNP_LINK_LOCK_ASSERT() rw_assert(&unp_link_rwlock, \
242 #define UNP_LINK_UNLOCK_ASSERT() rw_assert(&unp_link_rwlock, \
245 #define UNP_LINK_RLOCK() rw_rlock(&unp_link_rwlock)
246 #define UNP_LINK_RUNLOCK() rw_runlock(&unp_link_rwlock)
247 #define UNP_LINK_WLOCK() rw_wlock(&unp_link_rwlock)
248 #define UNP_LINK_WUNLOCK() rw_wunlock(&unp_link_rwlock)
249 #define UNP_LINK_WLOCK_ASSERT() rw_assert(&unp_link_rwlock, \
252 #define UNP_LIST_LOCK_INIT() mtx_init(&unp_list_lock, \
253 "unp_list_lock", NULL, MTX_DEF)
254 #define UNP_LIST_LOCK() mtx_lock(&unp_list_lock)
255 #define UNP_LIST_UNLOCK() mtx_unlock(&unp_list_lock)
257 #define UNP_DEFERRED_LOCK_INIT() mtx_init(&unp_defers_lock, \
258 "unp_defer", NULL, MTX_DEF)
259 #define UNP_DEFERRED_LOCK() mtx_lock(&unp_defers_lock)
260 #define UNP_DEFERRED_UNLOCK() mtx_unlock(&unp_defers_lock)
262 #define UNP_PCB_LOCK_INIT(unp) mtx_init(&(unp)->unp_mtx, \
263 "unp_mtx", "unp_mtx", \
264 MTX_DUPOK|MTX_DEF|MTX_RECURSE)
265 #define UNP_PCB_LOCK_DESTROY(unp) mtx_destroy(&(unp)->unp_mtx)
266 #define UNP_PCB_LOCK(unp) mtx_lock(&(unp)->unp_mtx)
267 #define UNP_PCB_UNLOCK(unp) mtx_unlock(&(unp)->unp_mtx)
268 #define UNP_PCB_LOCK_ASSERT(unp) mtx_assert(&(unp)->unp_mtx, MA_OWNED)
270 static int uipc_connect2(struct socket *, struct socket *);
271 static int uipc_ctloutput(struct socket *, struct sockopt *);
272 static int unp_connect(struct socket *, struct sockaddr *,
274 static int unp_connect2(struct socket *so, struct socket *so2, int);
275 static void unp_disconnect(struct unpcb *unp, struct unpcb *unp2);
276 static void unp_dispose(struct mbuf *);
277 static void unp_shutdown(struct unpcb *);
278 static void unp_drop(struct unpcb *, int);
279 static void unp_gc(__unused void *, int);
280 static void unp_scan(struct mbuf *, void (*)(struct file *));
281 static void unp_discard(struct file *);
282 static void unp_freerights(struct file **, int);
283 static void unp_init(void);
284 static int unp_internalize(struct mbuf **, struct thread *);
285 static void unp_internalize_fp(struct file *);
286 static int unp_externalize(struct mbuf *, struct mbuf **);
287 static int unp_externalize_fp(struct file *);
288 static struct mbuf *unp_addsockcred(struct thread *, struct mbuf *);
289 static void unp_process_defers(void * __unused, int);
292 * Definitions of protocols supported in the LOCAL domain.
294 static struct domain localdomain;
295 static struct pr_usrreqs uipc_usrreqs_dgram, uipc_usrreqs_stream;
296 static struct pr_usrreqs uipc_usrreqs_seqpacket;
297 static struct protosw localsw[] = {
299 .pr_type = SOCK_STREAM,
300 .pr_domain = &localdomain,
301 .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_RIGHTS,
302 .pr_ctloutput = &uipc_ctloutput,
303 .pr_usrreqs = &uipc_usrreqs_stream
306 .pr_type = SOCK_DGRAM,
307 .pr_domain = &localdomain,
308 .pr_flags = PR_ATOMIC|PR_ADDR|PR_RIGHTS,
309 .pr_ctloutput = &uipc_ctloutput,
310 .pr_usrreqs = &uipc_usrreqs_dgram
313 .pr_type = SOCK_SEQPACKET,
314 .pr_domain = &localdomain,
317 * XXXRW: For now, PR_ADDR because soreceive will bump into them
318 * due to our use of sbappendaddr. A new sbappend variants is needed
319 * that supports both atomic record writes and control data.
321 .pr_flags = PR_ADDR|PR_ATOMIC|PR_CONNREQUIRED|PR_WANTRCVD|
323 .pr_usrreqs = &uipc_usrreqs_seqpacket,
327 static struct domain localdomain = {
328 .dom_family = AF_LOCAL,
330 .dom_init = unp_init,
331 .dom_externalize = unp_externalize,
332 .dom_dispose = unp_dispose,
333 .dom_protosw = localsw,
334 .dom_protoswNPROTOSW = &localsw[sizeof(localsw)/sizeof(localsw[0])]
339 uipc_abort(struct socket *so)
341 struct unpcb *unp, *unp2;
344 KASSERT(unp != NULL, ("uipc_abort: unp == NULL"));
348 unp2 = unp->unp_conn;
351 unp_drop(unp2, ECONNABORTED);
352 UNP_PCB_UNLOCK(unp2);
359 uipc_accept(struct socket *so, struct sockaddr **nam)
361 struct unpcb *unp, *unp2;
362 const struct sockaddr *sa;
365 * Pass back name of connected socket, if it was bound and we are
366 * still connected (our peer may have closed already!).
369 KASSERT(unp != NULL, ("uipc_accept: unp == NULL"));
371 *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
373 unp2 = unp->unp_conn;
374 if (unp2 != NULL && unp2->unp_addr != NULL) {
376 sa = (struct sockaddr *) unp2->unp_addr;
377 bcopy(sa, *nam, sa->sa_len);
378 UNP_PCB_UNLOCK(unp2);
381 bcopy(sa, *nam, sa->sa_len);
388 uipc_attach(struct socket *so, int proto, struct thread *td)
390 u_long sendspace, recvspace;
394 KASSERT(so->so_pcb == NULL, ("uipc_attach: so_pcb != NULL"));
395 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
396 switch (so->so_type) {
398 sendspace = unpst_sendspace;
399 recvspace = unpst_recvspace;
403 sendspace = unpdg_sendspace;
404 recvspace = unpdg_recvspace;
408 sendspace = unpsp_sendspace;
409 recvspace = unpsp_recvspace;
413 panic("uipc_attach");
415 error = soreserve(so, sendspace, recvspace);
419 unp = uma_zalloc(unp_zone, M_NOWAIT | M_ZERO);
422 LIST_INIT(&unp->unp_refs);
423 UNP_PCB_LOCK_INIT(unp);
424 unp->unp_socket = so;
426 unp->unp_refcount = 1;
429 unp->unp_gencnt = ++unp_gencnt;
431 switch (so->so_type) {
433 LIST_INSERT_HEAD(&unp_shead, unp, unp_link);
437 LIST_INSERT_HEAD(&unp_dhead, unp, unp_link);
441 LIST_INSERT_HEAD(&unp_sphead, unp, unp_link);
445 panic("uipc_attach");
453 uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
455 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
465 KASSERT(unp != NULL, ("uipc_bind: unp == NULL"));
467 if (soun->sun_len > sizeof(struct sockaddr_un))
469 namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
474 * We don't allow simultaneous bind() calls on a single UNIX domain
475 * socket, so flag in-progress operations, and return an error if an
476 * operation is already in progress.
478 * Historically, we have not allowed a socket to be rebound, so this
479 * also returns an error. Not allowing re-binding simplifies the
480 * implementation and avoids a great many possible failure modes.
483 if (unp->unp_vnode != NULL) {
487 if (unp->unp_flags & UNP_BINDING) {
491 unp->unp_flags |= UNP_BINDING;
494 buf = malloc(namelen + 1, M_TEMP, M_WAITOK);
495 bcopy(soun->sun_path, buf, namelen);
499 NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME,
500 UIO_SYSSPACE, buf, td);
501 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
506 if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
507 NDFREE(&nd, NDF_ONLY_PNBUF);
517 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
523 vattr.va_type = VSOCK;
524 vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask);
526 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
530 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
531 NDFREE(&nd, NDF_ONLY_PNBUF);
534 vn_finished_write(mp);
538 ASSERT_VOP_ELOCKED(vp, "uipc_bind");
539 soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK);
543 VOP_UNP_BIND(vp, unp->unp_socket);
545 unp->unp_addr = soun;
546 unp->unp_flags &= ~UNP_BINDING;
550 vn_finished_write(mp);
556 unp->unp_flags &= ~UNP_BINDING;
563 uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
567 KASSERT(td == curthread, ("uipc_connect: td != curthread"));
569 error = unp_connect(so, nam, td);
575 uipc_close(struct socket *so)
577 struct unpcb *unp, *unp2;
580 KASSERT(unp != NULL, ("uipc_close: unp == NULL"));
584 unp2 = unp->unp_conn;
587 unp_disconnect(unp, unp2);
588 UNP_PCB_UNLOCK(unp2);
595 uipc_connect2(struct socket *so1, struct socket *so2)
597 struct unpcb *unp, *unp2;
602 KASSERT(unp != NULL, ("uipc_connect2: unp == NULL"));
605 KASSERT(unp2 != NULL, ("uipc_connect2: unp2 == NULL"));
607 error = unp_connect2(so1, so2, PRU_CONNECT2);
608 UNP_PCB_UNLOCK(unp2);
615 uipc_detach(struct socket *so)
617 struct unpcb *unp, *unp2;
618 struct sockaddr_un *saved_unp_addr;
620 int freeunp, local_unp_rights;
623 KASSERT(unp != NULL, ("uipc_detach: unp == NULL"));
628 LIST_REMOVE(unp, unp_link);
629 unp->unp_gencnt = ++unp_gencnt;
634 * XXXRW: Should assert vp->v_socket == so.
636 if ((vp = unp->unp_vnode) != NULL) {
638 unp->unp_vnode = NULL;
640 unp2 = unp->unp_conn;
643 unp_disconnect(unp, unp2);
644 UNP_PCB_UNLOCK(unp2);
648 * We hold the linkage lock exclusively, so it's OK to acquire
649 * multiple pcb locks at a time.
651 while (!LIST_EMPTY(&unp->unp_refs)) {
652 struct unpcb *ref = LIST_FIRST(&unp->unp_refs);
655 unp_drop(ref, ECONNRESET);
658 local_unp_rights = unp_rights;
660 unp->unp_socket->so_pcb = NULL;
661 saved_unp_addr = unp->unp_addr;
662 unp->unp_addr = NULL;
664 freeunp = (unp->unp_refcount == 0);
665 if (saved_unp_addr != NULL)
666 free(saved_unp_addr, M_SONAME);
668 UNP_PCB_LOCK_DESTROY(unp);
669 uma_zfree(unp_zone, unp);
674 if (local_unp_rights)
675 taskqueue_enqueue_timeout(taskqueue_thread, &unp_gc_task, -1);
679 uipc_disconnect(struct socket *so)
681 struct unpcb *unp, *unp2;
684 KASSERT(unp != NULL, ("uipc_disconnect: unp == NULL"));
688 unp2 = unp->unp_conn;
691 unp_disconnect(unp, unp2);
692 UNP_PCB_UNLOCK(unp2);
700 uipc_listen(struct socket *so, int backlog, struct thread *td)
706 KASSERT(unp != NULL, ("uipc_listen: unp == NULL"));
709 if (unp->unp_vnode == NULL) {
715 error = solisten_proto_check(so);
717 cru2x(td->td_ucred, &unp->unp_peercred);
718 unp->unp_flags |= UNP_HAVEPCCACHED;
719 solisten_proto(so, backlog);
727 uipc_peeraddr(struct socket *so, struct sockaddr **nam)
729 struct unpcb *unp, *unp2;
730 const struct sockaddr *sa;
733 KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL"));
735 *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
738 * XXX: It seems that this test always fails even when connection is
739 * established. So, this else clause is added as workaround to
740 * return PF_LOCAL sockaddr.
742 unp2 = unp->unp_conn;
745 if (unp2->unp_addr != NULL)
746 sa = (struct sockaddr *) unp2->unp_addr;
749 bcopy(sa, *nam, sa->sa_len);
750 UNP_PCB_UNLOCK(unp2);
753 bcopy(sa, *nam, sa->sa_len);
760 uipc_rcvd(struct socket *so, int flags)
762 struct unpcb *unp, *unp2;
768 KASSERT(unp != NULL, ("uipc_rcvd: unp == NULL"));
770 if (so->so_type != SOCK_STREAM && so->so_type != SOCK_SEQPACKET)
771 panic("uipc_rcvd socktype %d", so->so_type);
774 * Adjust backpressure on sender and wakeup any waiting to write.
776 * The unp lock is acquired to maintain the validity of the unp_conn
777 * pointer; no lock on unp2 is required as unp2->unp_socket will be
778 * static as long as we don't permit unp2 to disconnect from unp,
779 * which is prevented by the lock on unp. We cache values from
780 * so_rcv to avoid holding the so_rcv lock over the entire
781 * transaction on the remote so_snd.
783 SOCKBUF_LOCK(&so->so_rcv);
784 mbcnt = so->so_rcv.sb_mbcnt;
785 sbcc = so->so_rcv.sb_cc;
786 SOCKBUF_UNLOCK(&so->so_rcv);
788 unp2 = unp->unp_conn;
793 so2 = unp2->unp_socket;
794 SOCKBUF_LOCK(&so2->so_snd);
795 so2->so_snd.sb_mbmax += unp->unp_mbcnt - mbcnt;
796 newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - sbcc;
797 (void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat,
798 newhiwat, RLIM_INFINITY);
799 sowwakeup_locked(so2);
800 unp->unp_mbcnt = mbcnt;
807 uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
808 struct mbuf *control, struct thread *td)
810 struct unpcb *unp, *unp2;
812 u_int mbcnt_delta, sbcc;
817 KASSERT(unp != NULL, ("uipc_send: unp == NULL"));
819 if (flags & PRUS_OOB) {
823 if (control != NULL && (error = unp_internalize(&control, td)))
825 if ((nam != NULL) || (flags & PRUS_EOF))
829 switch (so->so_type) {
832 const struct sockaddr *from;
834 unp2 = unp->unp_conn;
836 UNP_LINK_WLOCK_ASSERT();
841 error = unp_connect(so, nam, td);
844 unp2 = unp->unp_conn;
848 * Because connect() and send() are non-atomic in a sendto()
849 * with a target address, it's possible that the socket will
850 * have disconnected before the send() can run. In that case
851 * return the slightly counter-intuitive but otherwise
852 * correct error that the socket is not connected.
859 if (unp2->unp_flags & UNP_WANTCRED)
860 control = unp_addsockcred(td, control);
862 if (unp->unp_addr != NULL)
863 from = (struct sockaddr *)unp->unp_addr;
866 so2 = unp2->unp_socket;
867 SOCKBUF_LOCK(&so2->so_rcv);
868 if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) {
869 sorwakeup_locked(so2);
873 SOCKBUF_UNLOCK(&so2->so_rcv);
877 UNP_LINK_WLOCK_ASSERT();
879 unp_disconnect(unp, unp2);
880 UNP_PCB_UNLOCK(unp2);
888 if ((so->so_state & SS_ISCONNECTED) == 0) {
890 UNP_LINK_WLOCK_ASSERT();
891 error = unp_connect(so, nam, td);
901 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
907 * Because connect() and send() are non-atomic in a sendto()
908 * with a target address, it's possible that the socket will
909 * have disconnected before the send() can run. In that case
910 * return the slightly counter-intuitive but otherwise
911 * correct error that the socket is not connected.
913 * Locking here must be done carefully: the linkage lock
914 * prevents interconnections between unpcbs from changing, so
915 * we can traverse from unp to unp2 without acquiring unp's
916 * lock. Socket buffer locks follow unpcb locks, so we can
917 * acquire both remote and lock socket buffer locks.
919 unp2 = unp->unp_conn;
924 so2 = unp2->unp_socket;
926 SOCKBUF_LOCK(&so2->so_rcv);
927 if (unp2->unp_flags & UNP_WANTCRED) {
929 * Credentials are passed only once on SOCK_STREAM
930 * and SOCK_SEQPACKET.
932 unp2->unp_flags &= ~UNP_WANTCRED;
933 control = unp_addsockcred(td, control);
936 * Send to paired receive port, and then reduce send buffer
937 * hiwater marks to maintain backpressure. Wake up readers.
939 switch (so->so_type) {
941 if (control != NULL) {
942 if (sbappendcontrol_locked(&so2->so_rcv, m,
946 sbappend_locked(&so2->so_rcv, m);
949 case SOCK_SEQPACKET: {
950 const struct sockaddr *from;
953 if (sbappendaddr_locked(&so2->so_rcv, from, m,
961 * XXXRW: While fine for SOCK_STREAM, this conflates maximum
962 * datagram size and back-pressure for SOCK_SEQPACKET, which
963 * can lead to undesired return of EMSGSIZE on send instead
964 * of more desirable blocking.
966 mbcnt_delta = so2->so_rcv.sb_mbcnt - unp2->unp_mbcnt;
967 unp2->unp_mbcnt = so2->so_rcv.sb_mbcnt;
968 sbcc = so2->so_rcv.sb_cc;
969 sorwakeup_locked(so2);
971 SOCKBUF_LOCK(&so->so_snd);
972 if ((int)so->so_snd.sb_hiwat >= (int)(sbcc - unp2->unp_cc))
973 newhiwat = so->so_snd.sb_hiwat - (sbcc - unp2->unp_cc);
976 (void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat,
977 newhiwat, RLIM_INFINITY);
978 so->so_snd.sb_mbmax -= mbcnt_delta;
979 SOCKBUF_UNLOCK(&so->so_snd);
981 UNP_PCB_UNLOCK(unp2);
986 panic("uipc_send unknown socktype");
990 * PRUS_EOF is equivalent to pru_send followed by pru_shutdown.
992 if (flags & PRUS_EOF) {
999 if ((nam != NULL) || (flags & PRUS_EOF))
1004 if (control != NULL && error != 0)
1005 unp_dispose(control);
1008 if (control != NULL)
1016 uipc_sense(struct socket *so, struct stat *sb)
1018 struct unpcb *unp, *unp2;
1021 unp = sotounpcb(so);
1022 KASSERT(unp != NULL, ("uipc_sense: unp == NULL"));
1024 sb->st_blksize = so->so_snd.sb_hiwat;
1027 unp2 = unp->unp_conn;
1028 if ((so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET) &&
1030 so2 = unp2->unp_socket;
1031 sb->st_blksize += so2->so_rcv.sb_cc;
1034 if (unp->unp_ino == 0)
1035 unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino;
1036 sb->st_ino = unp->unp_ino;
1037 UNP_PCB_UNLOCK(unp);
1043 uipc_shutdown(struct socket *so)
1047 unp = sotounpcb(so);
1048 KASSERT(unp != NULL, ("uipc_shutdown: unp == NULL"));
1054 UNP_PCB_UNLOCK(unp);
1060 uipc_sockaddr(struct socket *so, struct sockaddr **nam)
1063 const struct sockaddr *sa;
1065 unp = sotounpcb(so);
1066 KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL"));
1068 *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
1070 if (unp->unp_addr != NULL)
1071 sa = (struct sockaddr *) unp->unp_addr;
1074 bcopy(sa, *nam, sa->sa_len);
1075 UNP_PCB_UNLOCK(unp);
1079 static struct pr_usrreqs uipc_usrreqs_dgram = {
1080 .pru_abort = uipc_abort,
1081 .pru_accept = uipc_accept,
1082 .pru_attach = uipc_attach,
1083 .pru_bind = uipc_bind,
1084 .pru_connect = uipc_connect,
1085 .pru_connect2 = uipc_connect2,
1086 .pru_detach = uipc_detach,
1087 .pru_disconnect = uipc_disconnect,
1088 .pru_listen = uipc_listen,
1089 .pru_peeraddr = uipc_peeraddr,
1090 .pru_rcvd = uipc_rcvd,
1091 .pru_send = uipc_send,
1092 .pru_sense = uipc_sense,
1093 .pru_shutdown = uipc_shutdown,
1094 .pru_sockaddr = uipc_sockaddr,
1095 .pru_soreceive = soreceive_dgram,
1096 .pru_close = uipc_close,
1099 static struct pr_usrreqs uipc_usrreqs_seqpacket = {
1100 .pru_abort = uipc_abort,
1101 .pru_accept = uipc_accept,
1102 .pru_attach = uipc_attach,
1103 .pru_bind = uipc_bind,
1104 .pru_connect = uipc_connect,
1105 .pru_connect2 = uipc_connect2,
1106 .pru_detach = uipc_detach,
1107 .pru_disconnect = uipc_disconnect,
1108 .pru_listen = uipc_listen,
1109 .pru_peeraddr = uipc_peeraddr,
1110 .pru_rcvd = uipc_rcvd,
1111 .pru_send = uipc_send,
1112 .pru_sense = uipc_sense,
1113 .pru_shutdown = uipc_shutdown,
1114 .pru_sockaddr = uipc_sockaddr,
1115 .pru_soreceive = soreceive_generic, /* XXX: or...? */
1116 .pru_close = uipc_close,
1119 static struct pr_usrreqs uipc_usrreqs_stream = {
1120 .pru_abort = uipc_abort,
1121 .pru_accept = uipc_accept,
1122 .pru_attach = uipc_attach,
1123 .pru_bind = uipc_bind,
1124 .pru_connect = uipc_connect,
1125 .pru_connect2 = uipc_connect2,
1126 .pru_detach = uipc_detach,
1127 .pru_disconnect = uipc_disconnect,
1128 .pru_listen = uipc_listen,
1129 .pru_peeraddr = uipc_peeraddr,
1130 .pru_rcvd = uipc_rcvd,
1131 .pru_send = uipc_send,
1132 .pru_sense = uipc_sense,
1133 .pru_shutdown = uipc_shutdown,
1134 .pru_sockaddr = uipc_sockaddr,
1135 .pru_soreceive = soreceive_generic,
1136 .pru_close = uipc_close,
1140 uipc_ctloutput(struct socket *so, struct sockopt *sopt)
1146 if (sopt->sopt_level != 0)
1149 unp = sotounpcb(so);
1150 KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL"));
1152 switch (sopt->sopt_dir) {
1154 switch (sopt->sopt_name) {
1155 case LOCAL_PEERCRED:
1157 if (unp->unp_flags & UNP_HAVEPC)
1158 xu = unp->unp_peercred;
1160 if (so->so_type == SOCK_STREAM)
1165 UNP_PCB_UNLOCK(unp);
1167 error = sooptcopyout(sopt, &xu, sizeof(xu));
1171 /* Unlocked read. */
1172 optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0;
1173 error = sooptcopyout(sopt, &optval, sizeof(optval));
1176 case LOCAL_CONNWAIT:
1177 /* Unlocked read. */
1178 optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0;
1179 error = sooptcopyout(sopt, &optval, sizeof(optval));
1189 switch (sopt->sopt_name) {
1191 case LOCAL_CONNWAIT:
1192 error = sooptcopyin(sopt, &optval, sizeof(optval),
1197 #define OPTSET(bit) do { \
1198 UNP_PCB_LOCK(unp); \
1200 unp->unp_flags |= bit; \
1202 unp->unp_flags &= ~bit; \
1203 UNP_PCB_UNLOCK(unp); \
1206 switch (sopt->sopt_name) {
1208 OPTSET(UNP_WANTCRED);
1211 case LOCAL_CONNWAIT:
1212 OPTSET(UNP_CONNWAIT);
1221 error = ENOPROTOOPT;
1234 unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1236 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
1238 struct socket *so2, *so3;
1239 struct unpcb *unp, *unp2, *unp3;
1241 struct nameidata nd;
1242 char buf[SOCK_MAXADDRLEN];
1243 struct sockaddr *sa;
1245 UNP_LINK_WLOCK_ASSERT();
1247 unp = sotounpcb(so);
1248 KASSERT(unp != NULL, ("unp_connect: unp == NULL"));
1250 if (nam->sa_len > sizeof(struct sockaddr_un))
1252 len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
1255 bcopy(soun->sun_path, buf, len);
1259 if (unp->unp_flags & UNP_CONNECTING) {
1260 UNP_PCB_UNLOCK(unp);
1264 unp->unp_flags |= UNP_CONNECTING;
1265 UNP_PCB_UNLOCK(unp);
1267 sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
1268 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
1269 UIO_SYSSPACE, buf, td);
1275 ASSERT_VOP_LOCKED(vp, "unp_connect");
1276 NDFREE(&nd, NDF_ONLY_PNBUF);
1280 if (vp->v_type != VSOCK) {
1285 error = mac_vnode_check_open(td->td_ucred, vp, VWRITE | VREAD);
1289 error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td);
1293 unp = sotounpcb(so);
1294 KASSERT(unp != NULL, ("unp_connect: unp == NULL"));
1297 * Lock linkage lock for two reasons: make sure v_socket is stable,
1298 * and to protect simultaneous locking of multiple pcbs.
1301 VOP_UNP_CONNECT(vp, &so2);
1303 error = ECONNREFUSED;
1306 if (so->so_type != so2->so_type) {
1310 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
1311 if (so2->so_options & SO_ACCEPTCONN) {
1312 CURVNET_SET(so2->so_vnet);
1313 so3 = sonewconn(so2, 0);
1318 error = ECONNREFUSED;
1321 unp = sotounpcb(so);
1322 unp2 = sotounpcb(so2);
1323 unp3 = sotounpcb(so3);
1327 if (unp2->unp_addr != NULL) {
1328 bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len);
1329 unp3->unp_addr = (struct sockaddr_un *) sa;
1334 * The connecter's (client's) credentials are copied from its
1335 * process structure at the time of connect() (which is now).
1337 cru2x(td->td_ucred, &unp3->unp_peercred);
1338 unp3->unp_flags |= UNP_HAVEPC;
1341 * The receiver's (server's) credentials are copied from the
1342 * unp_peercred member of socket on which the former called
1343 * listen(); uipc_listen() cached that process's credentials
1344 * at that time so we can use them now.
1346 KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED,
1347 ("unp_connect: listener without cached peercred"));
1348 memcpy(&unp->unp_peercred, &unp2->unp_peercred,
1349 sizeof(unp->unp_peercred));
1350 unp->unp_flags |= UNP_HAVEPC;
1351 if (unp2->unp_flags & UNP_WANTCRED)
1352 unp3->unp_flags |= UNP_WANTCRED;
1353 UNP_PCB_UNLOCK(unp3);
1354 UNP_PCB_UNLOCK(unp2);
1355 UNP_PCB_UNLOCK(unp);
1357 mac_socketpeer_set_from_socket(so, so3);
1358 mac_socketpeer_set_from_socket(so3, so);
1363 unp = sotounpcb(so);
1364 KASSERT(unp != NULL, ("unp_connect: unp == NULL"));
1365 unp2 = sotounpcb(so2);
1366 KASSERT(unp2 != NULL, ("unp_connect: unp2 == NULL"));
1369 error = unp_connect2(so, so2, PRU_CONNECT);
1370 UNP_PCB_UNLOCK(unp2);
1371 UNP_PCB_UNLOCK(unp);
1380 unp->unp_flags &= ~UNP_CONNECTING;
1381 UNP_PCB_UNLOCK(unp);
1386 unp_connect2(struct socket *so, struct socket *so2, int req)
1391 unp = sotounpcb(so);
1392 KASSERT(unp != NULL, ("unp_connect2: unp == NULL"));
1393 unp2 = sotounpcb(so2);
1394 KASSERT(unp2 != NULL, ("unp_connect2: unp2 == NULL"));
1396 UNP_LINK_WLOCK_ASSERT();
1397 UNP_PCB_LOCK_ASSERT(unp);
1398 UNP_PCB_LOCK_ASSERT(unp2);
1400 if (so2->so_type != so->so_type)
1401 return (EPROTOTYPE);
1402 unp->unp_conn = unp2;
1404 switch (so->so_type) {
1406 LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
1411 case SOCK_SEQPACKET:
1412 unp2->unp_conn = unp;
1413 if (req == PRU_CONNECT &&
1414 ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT))
1422 panic("unp_connect2");
1428 unp_disconnect(struct unpcb *unp, struct unpcb *unp2)
1432 KASSERT(unp2 != NULL, ("unp_disconnect: unp2 == NULL"));
1434 UNP_LINK_WLOCK_ASSERT();
1435 UNP_PCB_LOCK_ASSERT(unp);
1436 UNP_PCB_LOCK_ASSERT(unp2);
1438 unp->unp_conn = NULL;
1439 switch (unp->unp_socket->so_type) {
1441 LIST_REMOVE(unp, unp_reflink);
1442 so = unp->unp_socket;
1444 so->so_state &= ~SS_ISCONNECTED;
1449 case SOCK_SEQPACKET:
1450 soisdisconnected(unp->unp_socket);
1451 unp2->unp_conn = NULL;
1452 soisdisconnected(unp2->unp_socket);
1458 * unp_pcblist() walks the global list of struct unpcb's to generate a
1459 * pointer list, bumping the refcount on each unpcb. It then copies them out
1460 * sequentially, validating the generation number on each to see if it has
1461 * been detached. All of this is necessary because copyout() may sleep on
1465 unp_pcblist(SYSCTL_HANDLER_ARGS)
1469 struct unpcb *unp, **unp_list;
1471 struct xunpgen *xug;
1472 struct unp_head *head;
1475 switch ((intptr_t)arg1) {
1484 case SOCK_SEQPACKET:
1489 panic("unp_pcblist: arg1 %d", (int)(intptr_t)arg1);
1493 * The process of preparing the PCB list is too time-consuming and
1494 * resource-intensive to repeat twice on every request.
1496 if (req->oldptr == NULL) {
1498 req->oldidx = 2 * (sizeof *xug)
1499 + (n + n/8) * sizeof(struct xunpcb);
1503 if (req->newptr != NULL)
1507 * OK, now we're committed to doing something.
1509 xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK);
1511 gencnt = unp_gencnt;
1515 xug->xug_len = sizeof *xug;
1517 xug->xug_gen = gencnt;
1518 xug->xug_sogen = so_gencnt;
1519 error = SYSCTL_OUT(req, xug, sizeof *xug);
1525 unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
1528 for (unp = LIST_FIRST(head), i = 0; unp && i < n;
1529 unp = LIST_NEXT(unp, unp_link)) {
1531 if (unp->unp_gencnt <= gencnt) {
1532 if (cr_cansee(req->td->td_ucred,
1533 unp->unp_socket->so_cred)) {
1534 UNP_PCB_UNLOCK(unp);
1537 unp_list[i++] = unp;
1538 unp->unp_refcount++;
1540 UNP_PCB_UNLOCK(unp);
1543 n = i; /* In case we lost some during malloc. */
1546 xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO);
1547 for (i = 0; i < n; i++) {
1550 unp->unp_refcount--;
1551 if (unp->unp_refcount != 0 && unp->unp_gencnt <= gencnt) {
1552 xu->xu_len = sizeof *xu;
1555 * XXX - need more locking here to protect against
1556 * connect/disconnect races for SMP.
1558 if (unp->unp_addr != NULL)
1559 bcopy(unp->unp_addr, &xu->xu_addr,
1560 unp->unp_addr->sun_len);
1561 if (unp->unp_conn != NULL &&
1562 unp->unp_conn->unp_addr != NULL)
1563 bcopy(unp->unp_conn->unp_addr,
1565 unp->unp_conn->unp_addr->sun_len);
1566 bcopy(unp, &xu->xu_unp, sizeof *unp);
1567 sotoxsocket(unp->unp_socket, &xu->xu_socket);
1568 UNP_PCB_UNLOCK(unp);
1569 error = SYSCTL_OUT(req, xu, sizeof *xu);
1571 freeunp = (unp->unp_refcount == 0);
1572 UNP_PCB_UNLOCK(unp);
1574 UNP_PCB_LOCK_DESTROY(unp);
1575 uma_zfree(unp_zone, unp);
1582 * Give the user an updated idea of our state. If the
1583 * generation differs from what we told her before, she knows
1584 * that something happened while we were processing this
1585 * request, and it might be necessary to retry.
1587 xug->xug_gen = unp_gencnt;
1588 xug->xug_sogen = so_gencnt;
1589 xug->xug_count = unp_count;
1590 error = SYSCTL_OUT(req, xug, sizeof *xug);
1592 free(unp_list, M_TEMP);
1597 SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLTYPE_OPAQUE | CTLFLAG_RD,
1598 (void *)(intptr_t)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
1599 "List of active local datagram sockets");
1600 SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLTYPE_OPAQUE | CTLFLAG_RD,
1601 (void *)(intptr_t)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
1602 "List of active local stream sockets");
1603 SYSCTL_PROC(_net_local_seqpacket, OID_AUTO, pcblist,
1604 CTLTYPE_OPAQUE | CTLFLAG_RD,
1605 (void *)(intptr_t)SOCK_SEQPACKET, 0, unp_pcblist, "S,xunpcb",
1606 "List of active local seqpacket sockets");
1609 unp_shutdown(struct unpcb *unp)
1614 UNP_LINK_WLOCK_ASSERT();
1615 UNP_PCB_LOCK_ASSERT(unp);
1617 unp2 = unp->unp_conn;
1618 if ((unp->unp_socket->so_type == SOCK_STREAM ||
1619 (unp->unp_socket->so_type == SOCK_SEQPACKET)) && unp2 != NULL) {
1620 so = unp2->unp_socket;
1627 unp_drop(struct unpcb *unp, int errno)
1629 struct socket *so = unp->unp_socket;
1632 UNP_LINK_WLOCK_ASSERT();
1633 UNP_PCB_LOCK_ASSERT(unp);
1635 so->so_error = errno;
1636 unp2 = unp->unp_conn;
1640 unp_disconnect(unp, unp2);
1641 UNP_PCB_UNLOCK(unp2);
1645 unp_freerights(struct file **rp, int fdcount)
1650 for (i = 0; i < fdcount; i++) {
1658 unp_externalize(struct mbuf *control, struct mbuf **controlp)
1660 struct thread *td = curthread; /* XXX */
1661 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1667 socklen_t clen = control->m_len, datalen;
1672 UNP_LINK_UNLOCK_ASSERT();
1675 if (controlp != NULL) /* controlp == NULL => free control messages */
1677 while (cm != NULL) {
1678 if (sizeof(*cm) > clen || cm->cmsg_len > clen) {
1682 data = CMSG_DATA(cm);
1683 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1684 if (cm->cmsg_level == SOL_SOCKET
1685 && cm->cmsg_type == SCM_RIGHTS) {
1686 newfds = datalen / sizeof(struct file *);
1689 /* If we're not outputting the descriptors free them. */
1690 if (error || controlp == NULL) {
1691 unp_freerights(rp, newfds);
1694 FILEDESC_XLOCK(td->td_proc->p_fd);
1695 /* if the new FD's will not fit free them. */
1696 if (!fdavail(td, newfds)) {
1697 FILEDESC_XUNLOCK(td->td_proc->p_fd);
1699 unp_freerights(rp, newfds);
1704 * Now change each pointer to an fd in the global
1705 * table to an integer that is the index to the local
1706 * fd table entry that we set up to point to the
1707 * global one we are transferring.
1709 newlen = newfds * sizeof(int);
1710 *controlp = sbcreatecontrol(NULL, newlen,
1711 SCM_RIGHTS, SOL_SOCKET);
1712 if (*controlp == NULL) {
1713 FILEDESC_XUNLOCK(td->td_proc->p_fd);
1715 unp_freerights(rp, newfds);
1720 CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1721 for (i = 0; i < newfds; i++) {
1722 if (fdalloc(td, 0, &f))
1723 panic("unp_externalize fdalloc failed");
1725 td->td_proc->p_fd->fd_ofiles[f] = fp;
1726 unp_externalize_fp(fp);
1729 FILEDESC_XUNLOCK(td->td_proc->p_fd);
1731 /* We can just copy anything else across. */
1732 if (error || controlp == NULL)
1734 *controlp = sbcreatecontrol(NULL, datalen,
1735 cm->cmsg_type, cm->cmsg_level);
1736 if (*controlp == NULL) {
1741 CMSG_DATA(mtod(*controlp, struct cmsghdr *)),
1744 controlp = &(*controlp)->m_next;
1747 if (CMSG_SPACE(datalen) < clen) {
1748 clen -= CMSG_SPACE(datalen);
1749 cm = (struct cmsghdr *)
1750 ((caddr_t)cm + CMSG_SPACE(datalen));
1762 unp_zone_change(void *tag)
1765 uma_zone_set_max(unp_zone, maxsockets);
1773 if (!IS_DEFAULT_VNET(curvnet))
1776 unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL,
1777 NULL, NULL, UMA_ALIGN_PTR, 0);
1778 if (unp_zone == NULL)
1780 uma_zone_set_max(unp_zone, maxsockets);
1781 EVENTHANDLER_REGISTER(maxsockets_change, unp_zone_change,
1782 NULL, EVENTHANDLER_PRI_ANY);
1783 LIST_INIT(&unp_dhead);
1784 LIST_INIT(&unp_shead);
1785 LIST_INIT(&unp_sphead);
1786 SLIST_INIT(&unp_defers);
1787 TIMEOUT_TASK_INIT(taskqueue_thread, &unp_gc_task, 0, unp_gc, NULL);
1788 TASK_INIT(&unp_defer_task, 0, unp_process_defers, NULL);
1789 UNP_LINK_LOCK_INIT();
1790 UNP_LIST_LOCK_INIT();
1791 UNP_DEFERRED_LOCK_INIT();
1795 unp_internalize(struct mbuf **controlp, struct thread *td)
1797 struct mbuf *control = *controlp;
1798 struct proc *p = td->td_proc;
1799 struct filedesc *fdescp = p->p_fd;
1800 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1801 struct cmsgcred *cmcred;
1807 socklen_t clen = control->m_len, datalen;
1811 UNP_LINK_UNLOCK_ASSERT();
1815 while (cm != NULL) {
1816 if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET
1817 || cm->cmsg_len > clen) {
1821 data = CMSG_DATA(cm);
1822 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1824 switch (cm->cmsg_type) {
1826 * Fill in credential information.
1829 *controlp = sbcreatecontrol(NULL, sizeof(*cmcred),
1830 SCM_CREDS, SOL_SOCKET);
1831 if (*controlp == NULL) {
1835 cmcred = (struct cmsgcred *)
1836 CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1837 cmcred->cmcred_pid = p->p_pid;
1838 cmcred->cmcred_uid = td->td_ucred->cr_ruid;
1839 cmcred->cmcred_gid = td->td_ucred->cr_rgid;
1840 cmcred->cmcred_euid = td->td_ucred->cr_uid;
1841 cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups,
1843 for (i = 0; i < cmcred->cmcred_ngroups; i++)
1844 cmcred->cmcred_groups[i] =
1845 td->td_ucred->cr_groups[i];
1849 oldfds = datalen / sizeof (int);
1851 * Check that all the FDs passed in refer to legal
1852 * files. If not, reject the entire operation.
1855 FILEDESC_SLOCK(fdescp);
1856 for (i = 0; i < oldfds; i++) {
1858 if (fd < 0 || fd >= fdescp->fd_nfiles ||
1859 fdescp->fd_ofiles[fd] == NULL) {
1860 FILEDESC_SUNLOCK(fdescp);
1864 fp = fdescp->fd_ofiles[fd];
1865 if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) {
1866 FILEDESC_SUNLOCK(fdescp);
1874 * Now replace the integer FDs with pointers to the
1875 * associated global file table entry..
1877 newlen = oldfds * sizeof(struct file *);
1878 *controlp = sbcreatecontrol(NULL, newlen,
1879 SCM_RIGHTS, SOL_SOCKET);
1880 if (*controlp == NULL) {
1881 FILEDESC_SUNLOCK(fdescp);
1886 rp = (struct file **)
1887 CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1888 for (i = 0; i < oldfds; i++) {
1889 fp = fdescp->fd_ofiles[*fdp++];
1891 unp_internalize_fp(fp);
1893 FILEDESC_SUNLOCK(fdescp);
1897 *controlp = sbcreatecontrol(NULL, sizeof(*tv),
1898 SCM_TIMESTAMP, SOL_SOCKET);
1899 if (*controlp == NULL) {
1903 tv = (struct timeval *)
1904 CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1913 controlp = &(*controlp)->m_next;
1914 if (CMSG_SPACE(datalen) < clen) {
1915 clen -= CMSG_SPACE(datalen);
1916 cm = (struct cmsghdr *)
1917 ((caddr_t)cm + CMSG_SPACE(datalen));
1929 static struct mbuf *
1930 unp_addsockcred(struct thread *td, struct mbuf *control)
1932 struct mbuf *m, *n, *n_prev;
1933 struct sockcred *sc;
1934 const struct cmsghdr *cm;
1938 ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX);
1939 m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET);
1943 sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *));
1944 sc->sc_uid = td->td_ucred->cr_ruid;
1945 sc->sc_euid = td->td_ucred->cr_uid;
1946 sc->sc_gid = td->td_ucred->cr_rgid;
1947 sc->sc_egid = td->td_ucred->cr_gid;
1948 sc->sc_ngroups = ngroups;
1949 for (i = 0; i < sc->sc_ngroups; i++)
1950 sc->sc_groups[i] = td->td_ucred->cr_groups[i];
1953 * Unlink SCM_CREDS control messages (struct cmsgcred), since just
1954 * created SCM_CREDS control message (struct sockcred) has another
1957 if (control != NULL)
1958 for (n = control, n_prev = NULL; n != NULL;) {
1959 cm = mtod(n, struct cmsghdr *);
1960 if (cm->cmsg_level == SOL_SOCKET &&
1961 cm->cmsg_type == SCM_CREDS) {
1963 control = n->m_next;
1965 n_prev->m_next = n->m_next;
1973 /* Prepend it to the head. */
1974 m->m_next = control;
1978 static struct unpcb *
1979 fptounp(struct file *fp)
1983 if (fp->f_type != DTYPE_SOCKET)
1985 if ((so = fp->f_data) == NULL)
1987 if (so->so_proto->pr_domain != &localdomain)
1989 return sotounpcb(so);
1993 unp_discard(struct file *fp)
1995 struct unp_defer *dr;
1997 if (unp_externalize_fp(fp)) {
1998 dr = malloc(sizeof(*dr), M_TEMP, M_WAITOK);
2000 UNP_DEFERRED_LOCK();
2001 SLIST_INSERT_HEAD(&unp_defers, dr, ud_link);
2002 UNP_DEFERRED_UNLOCK();
2003 atomic_add_int(&unp_defers_count, 1);
2004 taskqueue_enqueue(taskqueue_thread, &unp_defer_task);
2006 (void) closef(fp, (struct thread *)NULL);
2010 unp_process_defers(void *arg __unused, int pending)
2012 struct unp_defer *dr;
2013 SLIST_HEAD(, unp_defer) drl;
2018 UNP_DEFERRED_LOCK();
2019 if (SLIST_FIRST(&unp_defers) == NULL) {
2020 UNP_DEFERRED_UNLOCK();
2023 SLIST_SWAP(&unp_defers, &drl, unp_defer);
2024 UNP_DEFERRED_UNLOCK();
2026 while ((dr = SLIST_FIRST(&drl)) != NULL) {
2027 SLIST_REMOVE_HEAD(&drl, ud_link);
2028 closef(dr->ud_fp, NULL);
2032 atomic_add_int(&unp_defers_count, -count);
2037 unp_internalize_fp(struct file *fp)
2042 if ((unp = fptounp(fp)) != NULL) {
2044 unp->unp_msgcount++;
2052 unp_externalize_fp(struct file *fp)
2058 if ((unp = fptounp(fp)) != NULL) {
2059 unp->unp_msgcount--;
2069 * unp_defer indicates whether additional work has been defered for a future
2070 * pass through unp_gc(). It is thread local and does not require explicit
2073 static int unp_marked;
2074 static int unp_unreachable;
2077 unp_accessable(struct file *fp)
2081 if ((unp = fptounp(fp)) == NULL)
2083 if (unp->unp_gcflag & UNPGC_REF)
2085 unp->unp_gcflag &= ~UNPGC_DEAD;
2086 unp->unp_gcflag |= UNPGC_REF;
2091 unp_gc_process(struct unpcb *unp)
2097 /* Already processed. */
2098 if (unp->unp_gcflag & UNPGC_SCANNED)
2103 * Check for a socket potentially in a cycle. It must be in a
2104 * queue as indicated by msgcount, and this must equal the file
2105 * reference count. Note that when msgcount is 0 the file is NULL.
2107 if ((unp->unp_gcflag & UNPGC_REF) == 0 && fp &&
2108 unp->unp_msgcount != 0 && fp->f_count == unp->unp_msgcount) {
2109 unp->unp_gcflag |= UNPGC_DEAD;
2115 * Mark all sockets we reference with RIGHTS.
2117 so = unp->unp_socket;
2118 SOCKBUF_LOCK(&so->so_rcv);
2119 unp_scan(so->so_rcv.sb_mb, unp_accessable);
2120 SOCKBUF_UNLOCK(&so->so_rcv);
2123 * Mark all sockets in our accept queue.
2126 TAILQ_FOREACH(soa, &so->so_comp, so_list) {
2127 SOCKBUF_LOCK(&soa->so_rcv);
2128 unp_scan(soa->so_rcv.sb_mb, unp_accessable);
2129 SOCKBUF_UNLOCK(&soa->so_rcv);
2132 unp->unp_gcflag |= UNPGC_SCANNED;
2135 static int unp_recycled;
2136 SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0,
2137 "Number of unreachable sockets claimed by the garbage collector.");
2139 static int unp_taskcount;
2140 SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0,
2141 "Number of times the garbage collector has run.");
2144 unp_gc(__unused void *arg, int pending)
2146 struct unp_head *heads[] = { &unp_dhead, &unp_shead, &unp_sphead,
2148 struct unp_head **head;
2149 struct file *f, **unref;
2156 * First clear all gc flags from previous runs.
2158 for (head = heads; *head != NULL; head++)
2159 LIST_FOREACH(unp, *head, unp_link)
2160 unp->unp_gcflag = 0;
2163 * Scan marking all reachable sockets with UNPGC_REF. Once a socket
2164 * is reachable all of the sockets it references are reachable.
2165 * Stop the scan once we do a complete loop without discovering
2166 * a new reachable socket.
2169 unp_unreachable = 0;
2171 for (head = heads; *head != NULL; head++)
2172 LIST_FOREACH(unp, *head, unp_link)
2173 unp_gc_process(unp);
2174 } while (unp_marked);
2176 if (unp_unreachable == 0)
2180 * Allocate space for a local list of dead unpcbs.
2182 unref = malloc(unp_unreachable * sizeof(struct file *),
2186 * Iterate looking for sockets which have been specifically marked
2187 * as as unreachable and store them locally.
2191 for (total = 0, head = heads; *head != NULL; head++)
2192 LIST_FOREACH(unp, *head, unp_link)
2193 if ((unp->unp_gcflag & UNPGC_DEAD) != 0) {
2195 if (unp->unp_msgcount == 0 || f == NULL ||
2196 f->f_count != unp->unp_msgcount)
2200 KASSERT(total <= unp_unreachable,
2201 ("unp_gc: incorrect unreachable count."));
2207 * Now flush all sockets, free'ing rights. This will free the
2208 * struct files associated with these sockets but leave each socket
2209 * with one remaining ref.
2211 for (i = 0; i < total; i++) {
2214 so = unref[i]->f_data;
2215 CURVNET_SET(so->so_vnet);
2221 * And finally release the sockets so they can be reclaimed.
2223 for (i = 0; i < total; i++)
2224 fdrop(unref[i], NULL);
2225 unp_recycled += total;
2226 free(unref, M_TEMP);
2230 unp_dispose(struct mbuf *m)
2234 unp_scan(m, unp_discard);
2238 unp_scan(struct mbuf *m0, void (*op)(struct file *))
2245 socklen_t clen, datalen;
2248 while (m0 != NULL) {
2249 for (m = m0; m; m = m->m_next) {
2250 if (m->m_type != MT_CONTROL)
2253 cm = mtod(m, struct cmsghdr *);
2256 while (cm != NULL) {
2257 if (sizeof(*cm) > clen || cm->cmsg_len > clen)
2260 data = CMSG_DATA(cm);
2261 datalen = (caddr_t)cm + cm->cmsg_len
2264 if (cm->cmsg_level == SOL_SOCKET &&
2265 cm->cmsg_type == SCM_RIGHTS) {
2266 qfds = datalen / sizeof (struct file *);
2268 for (i = 0; i < qfds; i++)
2272 if (CMSG_SPACE(datalen) < clen) {
2273 clen -= CMSG_SPACE(datalen);
2274 cm = (struct cmsghdr *)
2275 ((caddr_t)cm + CMSG_SPACE(datalen));
2287 * A helper function called by VFS before socket-type vnode reclamation.
2288 * For an active vnode it clears unp_vnode pointer and decrements unp_vnode
2292 vfs_unp_reclaim(struct vnode *vp)
2298 ASSERT_VOP_ELOCKED(vp, "vfs_unp_reclaim");
2299 KASSERT(vp->v_type == VSOCK,
2300 ("vfs_unp_reclaim: vp->v_type != VSOCK"));
2304 VOP_UNP_CONNECT(vp, &so);
2307 unp = sotounpcb(so);
2311 if (unp->unp_vnode == vp) {
2313 unp->unp_vnode = NULL;
2316 UNP_PCB_UNLOCK(unp);
2325 db_print_indent(int indent)
2329 for (i = 0; i < indent; i++)
2334 db_print_unpflags(int unp_flags)
2339 if (unp_flags & UNP_HAVEPC) {
2340 db_printf("%sUNP_HAVEPC", comma ? ", " : "");
2343 if (unp_flags & UNP_HAVEPCCACHED) {
2344 db_printf("%sUNP_HAVEPCCACHED", comma ? ", " : "");
2347 if (unp_flags & UNP_WANTCRED) {
2348 db_printf("%sUNP_WANTCRED", comma ? ", " : "");
2351 if (unp_flags & UNP_CONNWAIT) {
2352 db_printf("%sUNP_CONNWAIT", comma ? ", " : "");
2355 if (unp_flags & UNP_CONNECTING) {
2356 db_printf("%sUNP_CONNECTING", comma ? ", " : "");
2359 if (unp_flags & UNP_BINDING) {
2360 db_printf("%sUNP_BINDING", comma ? ", " : "");
2366 db_print_xucred(int indent, struct xucred *xu)
2370 db_print_indent(indent);
2371 db_printf("cr_version: %u cr_uid: %u cr_ngroups: %d\n",
2372 xu->cr_version, xu->cr_uid, xu->cr_ngroups);
2373 db_print_indent(indent);
2374 db_printf("cr_groups: ");
2376 for (i = 0; i < xu->cr_ngroups; i++) {
2377 db_printf("%s%u", comma ? ", " : "", xu->cr_groups[i]);
2384 db_print_unprefs(int indent, struct unp_head *uh)
2390 LIST_FOREACH(unp, uh, unp_reflink) {
2391 if (counter % 4 == 0)
2392 db_print_indent(indent);
2393 db_printf("%p ", unp);
2394 if (counter % 4 == 3)
2398 if (counter != 0 && counter % 4 != 0)
2402 DB_SHOW_COMMAND(unpcb, db_show_unpcb)
2407 db_printf("usage: show unpcb <addr>\n");
2410 unp = (struct unpcb *)addr;
2412 db_printf("unp_socket: %p unp_vnode: %p\n", unp->unp_socket,
2415 db_printf("unp_ino: %ju unp_conn: %p\n", (uintmax_t)unp->unp_ino,
2418 db_printf("unp_refs:\n");
2419 db_print_unprefs(2, &unp->unp_refs);
2421 /* XXXRW: Would be nice to print the full address, if any. */
2422 db_printf("unp_addr: %p\n", unp->unp_addr);
2424 db_printf("unp_cc: %d unp_mbcnt: %d unp_gencnt: %llu\n",
2425 unp->unp_cc, unp->unp_mbcnt,
2426 (unsigned long long)unp->unp_gencnt);
2428 db_printf("unp_flags: %x (", unp->unp_flags);
2429 db_print_unpflags(unp->unp_flags);
2432 db_printf("unp_peercred:\n");
2433 db_print_xucred(2, &unp->unp_peercred);
2435 db_printf("unp_refcount: %u\n", unp->unp_refcount);