2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1989, 1991, 1993
5 * The Regents of the University of California. All Rights Reserved.
6 * Copyright (c) 2004-2009 Robert N. M. Watson All Rights Reserved.
7 * Copyright (c) 2018 Matthew Macy
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94
37 * UNIX Domain (Local) Sockets
39 * This is an implementation of UNIX (local) domain sockets. Each socket has
40 * an associated struct unpcb (UNIX protocol control block). Stream sockets
41 * may be connected to 0 or 1 other socket. Datagram sockets may be
42 * connected to 0, 1, or many other sockets. Sockets may be created and
43 * connected in pairs (socketpair(2)), or bound/connected to using the file
44 * system name space. For most purposes, only the receive socket buffer is
45 * used, as sending on one socket delivers directly to the receive socket
46 * buffer of a second socket.
48 * The implementation is substantially complicated by the fact that
49 * "ancillary data", such as file descriptors or credentials, may be passed
50 * across UNIX domain sockets. The potential for passing UNIX domain sockets
51 * over other UNIX domain sockets requires the implementation of a simple
52 * garbage collector to find and tear down cycles of disconnected sockets.
56 * rethink name space problems
57 * need a proper out-of-band
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
65 #include <sys/param.h>
66 #include <sys/capsicum.h>
67 #include <sys/domain.h>
68 #include <sys/fcntl.h>
69 #include <sys/malloc.h> /* XXX must be before <sys/file.h> */
70 #include <sys/eventhandler.h>
72 #include <sys/filedesc.h>
73 #include <sys/kernel.h>
76 #include <sys/mount.h>
77 #include <sys/mutex.h>
78 #include <sys/namei.h>
80 #include <sys/protosw.h>
81 #include <sys/queue.h>
82 #include <sys/resourcevar.h>
83 #include <sys/rwlock.h>
84 #include <sys/socket.h>
85 #include <sys/socketvar.h>
86 #include <sys/signalvar.h>
89 #include <sys/sysctl.h>
90 #include <sys/systm.h>
91 #include <sys/taskqueue.h>
93 #include <sys/unpcb.h>
94 #include <sys/vnode.h>
102 #include <security/mac/mac_framework.h>
106 MALLOC_DECLARE(M_FILECAPS);
110 * (l) Locked using list lock
111 * (g) Locked using linkage lock
114 static uma_zone_t unp_zone;
115 static unp_gen_t unp_gencnt; /* (l) */
116 static u_int unp_count; /* (l) Count of local sockets. */
117 static ino_t unp_ino; /* Prototype for fake inode numbers. */
118 static int unp_rights; /* (g) File descriptors in flight. */
119 static struct unp_head unp_shead; /* (l) List of stream sockets. */
120 static struct unp_head unp_dhead; /* (l) List of datagram sockets. */
121 static struct unp_head unp_sphead; /* (l) List of seqpacket sockets. */
124 SLIST_ENTRY(unp_defer) ud_link;
127 static SLIST_HEAD(, unp_defer) unp_defers;
128 static int unp_defers_count;
130 static const struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL };
133 * Garbage collection of cyclic file descriptor/socket references occurs
134 * asynchronously in a taskqueue context in order to avoid recursion and
135 * reentrance in the UNIX domain socket, file descriptor, and socket layer
136 * code. See unp_gc() for a full description.
138 static struct timeout_task unp_gc_task;
141 * The close of unix domain sockets attached as SCM_RIGHTS is
142 * postponed to the taskqueue, to avoid arbitrary recursion depth.
143 * The attached sockets might have another sockets attached.
145 static struct task unp_defer_task;
148 * Both send and receive buffers are allocated PIPSIZ bytes of buffering for
149 * stream sockets, although the total for sender and receiver is actually
152 * Datagram sockets really use the sendspace as the maximum datagram size,
153 * and don't really want to reserve the sendspace. Their recvspace should be
154 * large enough for at least one max-size datagram plus address.
159 static u_long unpst_sendspace = PIPSIZ;
160 static u_long unpst_recvspace = PIPSIZ;
161 static u_long unpdg_sendspace = 2*1024; /* really max datagram size */
162 static u_long unpdg_recvspace = 4*1024;
163 static u_long unpsp_sendspace = PIPSIZ; /* really max datagram size */
164 static u_long unpsp_recvspace = PIPSIZ;
166 static SYSCTL_NODE(_net, PF_LOCAL, local, CTLFLAG_RW, 0, "Local domain");
167 static SYSCTL_NODE(_net_local, SOCK_STREAM, stream, CTLFLAG_RW, 0,
169 static SYSCTL_NODE(_net_local, SOCK_DGRAM, dgram, CTLFLAG_RW, 0, "SOCK_DGRAM");
170 static SYSCTL_NODE(_net_local, SOCK_SEQPACKET, seqpacket, CTLFLAG_RW, 0,
173 SYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
174 &unpst_sendspace, 0, "Default stream send space.");
175 SYSCTL_ULONG(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
176 &unpst_recvspace, 0, "Default stream receive space.");
177 SYSCTL_ULONG(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
178 &unpdg_sendspace, 0, "Default datagram send space.");
179 SYSCTL_ULONG(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
180 &unpdg_recvspace, 0, "Default datagram receive space.");
181 SYSCTL_ULONG(_net_local_seqpacket, OID_AUTO, maxseqpacket, CTLFLAG_RW,
182 &unpsp_sendspace, 0, "Default seqpacket send space.");
183 SYSCTL_ULONG(_net_local_seqpacket, OID_AUTO, recvspace, CTLFLAG_RW,
184 &unpsp_recvspace, 0, "Default seqpacket receive space.");
185 SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0,
186 "File descriptors in flight.");
187 SYSCTL_INT(_net_local, OID_AUTO, deferred, CTLFLAG_RD,
188 &unp_defers_count, 0,
189 "File descriptors deferred to taskqueue for close.");
192 * Locking and synchronization:
194 * Three types of locks exist in the local domain socket implementation: a
195 * a global linkage rwlock, the mtxpool lock, and per-unpcb mutexes.
196 * The linkage lock protects the socket count, global generation number,
197 * and stream/datagram global lists.
199 * The mtxpool lock protects the vnode from being modified while referenced.
200 * Lock ordering requires that it be acquired before any unpcb locks.
202 * The unpcb lock (unp_mtx) protects all fields in the unpcb. Of particular
203 * note is that this includes the unp_conn field. So long as the unpcb lock
204 * is held the reference to the unpcb pointed to by unp_conn is valid. If we
205 * require that the unpcb pointed to by unp_conn remain live in cases where
206 * we need to drop the unp_mtx as when we need to acquire the lock for a
207 * second unpcb the caller must first acquire an additional reference on the
208 * second unpcb and then revalidate any state (typically check that unp_conn
209 * is non-NULL) upon requiring the initial unpcb lock. The lock ordering
210 * between unpcbs is the conventional ascending address order. Two helper
211 * routines exist for this:
213 * - unp_pcb_lock2(unp, unp2) - which just acquires the two locks in the
216 * - unp_pcb_owned_lock2(unp, unp2, freed) - the lock for unp is held
217 * when called. If unp is unlocked and unp2 is subsequently freed
218 * freed will be set to 1.
220 * The helper routines for references are:
222 * - unp_pcb_hold(unp): Can be called any time we currently hold a valid
225 * - unp_pcb_rele(unp): The caller must hold the unp lock. If we are
226 * releasing the last reference, detach must have been called thus
227 * unp->unp_socket be NULL.
229 * UNIX domain sockets each have an unpcb hung off of their so_pcb pointer,
230 * allocated in pru_attach() and freed in pru_detach(). The validity of that
231 * pointer is an invariant, so no lock is required to dereference the so_pcb
232 * pointer if a valid socket reference is held by the caller. In practice,
233 * this is always true during operations performed on a socket. Each unpcb
234 * has a back-pointer to its socket, unp_socket, which will be stable under
235 * the same circumstances.
237 * This pointer may only be safely dereferenced as long as a valid reference
238 * to the unpcb is held. Typically, this reference will be from the socket,
239 * or from another unpcb when the referring unpcb's lock is held (in order
240 * that the reference not be invalidated during use). For example, to follow
241 * unp->unp_conn->unp_socket, you need to hold a lock on unp_conn to guarantee
242 * that detach is not run clearing unp_socket.
244 * Blocking with UNIX domain sockets is a tricky issue: unlike most network
245 * protocols, bind() is a non-atomic operation, and connect() requires
246 * potential sleeping in the protocol, due to potentially waiting on local or
247 * distributed file systems. We try to separate "lookup" operations, which
248 * may sleep, and the IPC operations themselves, which typically can occur
249 * with relative atomicity as locks can be held over the entire operation.
251 * Another tricky issue is simultaneous multi-threaded or multi-process
252 * access to a single UNIX domain socket. These are handled by the flags
253 * UNP_CONNECTING and UNP_BINDING, which prevent concurrent connecting or
254 * binding, both of which involve dropping UNIX domain socket locks in order
255 * to perform namei() and other file system operations.
257 static struct rwlock unp_link_rwlock;
258 static struct mtx unp_defers_lock;
260 #define UNP_LINK_LOCK_INIT() rw_init(&unp_link_rwlock, \
263 #define UNP_LINK_LOCK_ASSERT() rw_assert(&unp_link_rwlock, \
265 #define UNP_LINK_UNLOCK_ASSERT() rw_assert(&unp_link_rwlock, \
268 #define UNP_LINK_RLOCK() rw_rlock(&unp_link_rwlock)
269 #define UNP_LINK_RUNLOCK() rw_runlock(&unp_link_rwlock)
270 #define UNP_LINK_WLOCK() rw_wlock(&unp_link_rwlock)
271 #define UNP_LINK_WUNLOCK() rw_wunlock(&unp_link_rwlock)
272 #define UNP_LINK_WLOCK_ASSERT() rw_assert(&unp_link_rwlock, \
274 #define UNP_LINK_WOWNED() rw_wowned(&unp_link_rwlock)
276 #define UNP_DEFERRED_LOCK_INIT() mtx_init(&unp_defers_lock, \
277 "unp_defer", NULL, MTX_DEF)
278 #define UNP_DEFERRED_LOCK() mtx_lock(&unp_defers_lock)
279 #define UNP_DEFERRED_UNLOCK() mtx_unlock(&unp_defers_lock)
281 #define UNP_REF_LIST_LOCK() UNP_DEFERRED_LOCK();
282 #define UNP_REF_LIST_UNLOCK() UNP_DEFERRED_UNLOCK();
284 #define UNP_PCB_LOCK_INIT(unp) mtx_init(&(unp)->unp_mtx, \
287 #define UNP_PCB_LOCK_DESTROY(unp) mtx_destroy(&(unp)->unp_mtx)
288 #define UNP_PCB_LOCK(unp) mtx_lock(&(unp)->unp_mtx)
289 #define UNP_PCB_TRYLOCK(unp) mtx_trylock(&(unp)->unp_mtx)
290 #define UNP_PCB_UNLOCK(unp) mtx_unlock(&(unp)->unp_mtx)
291 #define UNP_PCB_OWNED(unp) mtx_owned(&(unp)->unp_mtx)
292 #define UNP_PCB_LOCK_ASSERT(unp) mtx_assert(&(unp)->unp_mtx, MA_OWNED)
293 #define UNP_PCB_UNLOCK_ASSERT(unp) mtx_assert(&(unp)->unp_mtx, MA_NOTOWNED)
295 static int uipc_connect2(struct socket *, struct socket *);
296 static int uipc_ctloutput(struct socket *, struct sockopt *);
297 static int unp_connect(struct socket *, struct sockaddr *,
299 static int unp_connectat(int, struct socket *, struct sockaddr *,
301 static int unp_connect2(struct socket *so, struct socket *so2, int);
302 static void unp_disconnect(struct unpcb *unp, struct unpcb *unp2);
303 static void unp_dispose(struct socket *so);
304 static void unp_dispose_mbuf(struct mbuf *);
305 static void unp_shutdown(struct unpcb *);
306 static void unp_drop(struct unpcb *);
307 static void unp_gc(__unused void *, int);
308 static void unp_scan(struct mbuf *, void (*)(struct filedescent **, int));
309 static void unp_discard(struct file *);
310 static void unp_freerights(struct filedescent **, int);
311 static void unp_init(void);
312 static int unp_internalize(struct mbuf **, struct thread *);
313 static void unp_internalize_fp(struct file *);
314 static int unp_externalize(struct mbuf *, struct mbuf **, int);
315 static int unp_externalize_fp(struct file *);
316 static struct mbuf *unp_addsockcred(struct thread *, struct mbuf *);
317 static void unp_process_defers(void * __unused, int);
321 unp_pcb_hold(struct unpcb *unp)
323 MPASS(unp->unp_refcount);
324 refcount_acquire(&unp->unp_refcount);
328 unp_pcb_rele(struct unpcb *unp)
332 UNP_PCB_LOCK_ASSERT(unp);
333 MPASS(unp->unp_refcount);
334 if ((freed = refcount_release(&unp->unp_refcount))) {
335 /* we got here with having detached? */
336 MPASS(unp->unp_socket == NULL);
338 UNP_PCB_LOCK_DESTROY(unp);
339 uma_zfree(unp_zone, unp);
345 unp_pcb_lock2(struct unpcb *unp, struct unpcb *unp2)
348 UNP_PCB_UNLOCK_ASSERT(unp);
349 UNP_PCB_UNLOCK_ASSERT(unp2);
350 if ((uintptr_t)unp2 > (uintptr_t)unp) {
359 static __noinline void
360 unp_pcb_owned_lock2_slowpath(struct unpcb *unp, struct unpcb **unp2p, int *freed)
366 unp_pcb_hold((unp2));
367 UNP_PCB_UNLOCK((unp));
368 UNP_PCB_LOCK((unp2));
370 *freed = unp_pcb_rele((unp2));
375 #define unp_pcb_owned_lock2(unp, unp2, freed) do { \
377 UNP_PCB_LOCK_ASSERT((unp)); \
378 UNP_PCB_UNLOCK_ASSERT((unp2)); \
379 MPASS(unp != unp2); \
380 if (__predict_true(UNP_PCB_TRYLOCK((unp2)))) \
382 else if ((uintptr_t)(unp2) > (uintptr_t)(unp)) \
383 UNP_PCB_LOCK((unp2)); \
385 unp_pcb_owned_lock2_slowpath((unp), &(unp2), &freed); \
391 * Definitions of protocols supported in the LOCAL domain.
393 static struct domain localdomain;
394 static struct pr_usrreqs uipc_usrreqs_dgram, uipc_usrreqs_stream;
395 static struct pr_usrreqs uipc_usrreqs_seqpacket;
396 static struct protosw localsw[] = {
398 .pr_type = SOCK_STREAM,
399 .pr_domain = &localdomain,
400 .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_RIGHTS,
401 .pr_ctloutput = &uipc_ctloutput,
402 .pr_usrreqs = &uipc_usrreqs_stream
405 .pr_type = SOCK_DGRAM,
406 .pr_domain = &localdomain,
407 .pr_flags = PR_ATOMIC|PR_ADDR|PR_RIGHTS,
408 .pr_ctloutput = &uipc_ctloutput,
409 .pr_usrreqs = &uipc_usrreqs_dgram
412 .pr_type = SOCK_SEQPACKET,
413 .pr_domain = &localdomain,
416 * XXXRW: For now, PR_ADDR because soreceive will bump into them
417 * due to our use of sbappendaddr. A new sbappend variants is needed
418 * that supports both atomic record writes and control data.
420 .pr_flags = PR_ADDR|PR_ATOMIC|PR_CONNREQUIRED|PR_WANTRCVD|
422 .pr_ctloutput = &uipc_ctloutput,
423 .pr_usrreqs = &uipc_usrreqs_seqpacket,
427 static struct domain localdomain = {
428 .dom_family = AF_LOCAL,
430 .dom_init = unp_init,
431 .dom_externalize = unp_externalize,
432 .dom_dispose = unp_dispose,
433 .dom_protosw = localsw,
434 .dom_protoswNPROTOSW = &localsw[nitems(localsw)]
439 uipc_abort(struct socket *so)
441 struct unpcb *unp, *unp2;
444 KASSERT(unp != NULL, ("uipc_abort: unp == NULL"));
445 UNP_PCB_UNLOCK_ASSERT(unp);
448 unp2 = unp->unp_conn;
458 uipc_accept(struct socket *so, struct sockaddr **nam)
460 struct unpcb *unp, *unp2;
461 const struct sockaddr *sa;
464 * Pass back name of connected socket, if it was bound and we are
465 * still connected (our peer may have closed already!).
468 KASSERT(unp != NULL, ("uipc_accept: unp == NULL"));
470 *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
472 unp2 = unp->unp_conn;
473 if (unp2 != NULL && unp2->unp_addr != NULL) {
475 sa = (struct sockaddr *) unp2->unp_addr;
476 bcopy(sa, *nam, sa->sa_len);
477 UNP_PCB_UNLOCK(unp2);
480 bcopy(sa, *nam, sa->sa_len);
487 uipc_attach(struct socket *so, int proto, struct thread *td)
489 u_long sendspace, recvspace;
494 KASSERT(so->so_pcb == NULL, ("uipc_attach: so_pcb != NULL"));
495 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
496 switch (so->so_type) {
498 sendspace = unpst_sendspace;
499 recvspace = unpst_recvspace;
503 sendspace = unpdg_sendspace;
504 recvspace = unpdg_recvspace;
508 sendspace = unpsp_sendspace;
509 recvspace = unpsp_recvspace;
513 panic("uipc_attach");
515 error = soreserve(so, sendspace, recvspace);
519 unp = uma_zalloc(unp_zone, M_NOWAIT | M_ZERO);
522 LIST_INIT(&unp->unp_refs);
523 UNP_PCB_LOCK_INIT(unp);
524 unp->unp_socket = so;
526 unp->unp_refcount = 1;
527 if (so->so_listen != NULL)
528 unp->unp_flags |= UNP_NASCENT;
530 if ((locked = UNP_LINK_WOWNED()) == false)
533 unp->unp_gencnt = ++unp_gencnt;
535 switch (so->so_type) {
537 LIST_INSERT_HEAD(&unp_shead, unp, unp_link);
541 LIST_INSERT_HEAD(&unp_dhead, unp, unp_link);
545 LIST_INSERT_HEAD(&unp_sphead, unp, unp_link);
549 panic("uipc_attach");
559 uipc_bindat(int fd, struct socket *so, struct sockaddr *nam, struct thread *td)
561 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
571 if (nam->sa_family != AF_UNIX)
572 return (EAFNOSUPPORT);
575 KASSERT(unp != NULL, ("uipc_bind: unp == NULL"));
577 if (soun->sun_len > sizeof(struct sockaddr_un))
579 namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
584 * We don't allow simultaneous bind() calls on a single UNIX domain
585 * socket, so flag in-progress operations, and return an error if an
586 * operation is already in progress.
588 * Historically, we have not allowed a socket to be rebound, so this
589 * also returns an error. Not allowing re-binding simplifies the
590 * implementation and avoids a great many possible failure modes.
593 if (unp->unp_vnode != NULL) {
597 if (unp->unp_flags & UNP_BINDING) {
601 unp->unp_flags |= UNP_BINDING;
604 buf = malloc(namelen + 1, M_TEMP, M_WAITOK);
605 bcopy(soun->sun_path, buf, namelen);
609 NDINIT_ATRIGHTS(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME | NOCACHE,
610 UIO_SYSSPACE, buf, fd, cap_rights_init(&rights, CAP_BINDAT), td);
611 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
616 if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
617 NDFREE(&nd, NDF_ONLY_PNBUF);
627 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
633 vattr.va_type = VSOCK;
634 vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask);
636 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
640 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
641 NDFREE(&nd, NDF_ONLY_PNBUF);
644 vn_finished_write(mp);
648 ASSERT_VOP_ELOCKED(vp, "uipc_bind");
649 soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK);
652 VOP_UNP_BIND(vp, unp);
654 unp->unp_addr = soun;
655 unp->unp_flags &= ~UNP_BINDING;
658 vn_finished_write(mp);
664 unp->unp_flags &= ~UNP_BINDING;
671 uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
674 return (uipc_bindat(AT_FDCWD, so, nam, td));
678 uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
682 KASSERT(td == curthread, ("uipc_connect: td != curthread"));
683 error = unp_connect(so, nam, td);
688 uipc_connectat(int fd, struct socket *so, struct sockaddr *nam,
693 KASSERT(td == curthread, ("uipc_connectat: td != curthread"));
694 error = unp_connectat(fd, so, nam, td);
699 uipc_close(struct socket *so)
701 struct unpcb *unp, *unp2;
702 struct vnode *vp = NULL;
706 KASSERT(unp != NULL, ("uipc_close: unp == NULL"));
710 if ((vp = unp->unp_vnode) != NULL) {
711 vplock = mtx_pool_find(mtxpool_sleep, vp);
715 if (vp && unp->unp_vnode == NULL) {
721 unp->unp_vnode = NULL;
723 unp2 = unp->unp_conn;
725 if (__predict_false(unp == unp2)) {
726 unp_disconnect(unp, unp2);
727 } else if (unp2 != NULL) {
729 unp_pcb_owned_lock2(unp, unp2, freed);
730 unp_disconnect(unp, unp2);
731 if (unp_pcb_rele(unp2) == 0)
732 UNP_PCB_UNLOCK(unp2);
734 if (unp_pcb_rele(unp) == 0)
743 uipc_connect2(struct socket *so1, struct socket *so2)
745 struct unpcb *unp, *unp2;
749 KASSERT(unp != NULL, ("uipc_connect2: unp == NULL"));
751 KASSERT(unp2 != NULL, ("uipc_connect2: unp2 == NULL"));
753 unp_pcb_lock2(unp, unp2);
756 error = unp_connect2(so1, so2, PRU_CONNECT2);
758 UNP_PCB_UNLOCK(unp2);
764 uipc_detach(struct socket *so)
766 struct unpcb *unp, *unp2;
768 struct sockaddr_un *saved_unp_addr;
770 int freeunp, local_unp_rights;
773 KASSERT(unp != NULL, ("uipc_detach: unp == NULL"));
777 local_unp_rights = 0;
780 LIST_REMOVE(unp, unp_link);
781 unp->unp_gencnt = ++unp_gencnt;
785 UNP_PCB_UNLOCK_ASSERT(unp);
787 if ((vp = unp->unp_vnode) != NULL) {
788 vplock = mtx_pool_find(mtxpool_sleep, vp);
792 if (unp->unp_vnode != vp &&
793 unp->unp_vnode != NULL) {
799 if ((unp->unp_flags & UNP_NASCENT) != 0) {
802 if ((vp = unp->unp_vnode) != NULL) {
804 unp->unp_vnode = NULL;
806 if (__predict_false(unp == unp->unp_conn)) {
807 unp_disconnect(unp, unp);
811 if ((unp2 = unp->unp_conn) != NULL) {
812 unp_pcb_owned_lock2(unp, unp2, freeunp);
819 unp_disconnect(unp, unp2);
820 if (unp_pcb_rele(unp2) == 0)
821 UNP_PCB_UNLOCK(unp2);
826 while (!LIST_EMPTY(&unp->unp_refs)) {
827 struct unpcb *ref = LIST_FIRST(&unp->unp_refs);
830 UNP_REF_LIST_UNLOCK();
833 UNP_PCB_UNLOCK_ASSERT(ref);
838 UNP_REF_LIST_UNLOCK();
840 freeunp = unp_pcb_rele(unp);
842 local_unp_rights = unp_rights;
844 unp->unp_socket->so_pcb = NULL;
845 saved_unp_addr = unp->unp_addr;
846 unp->unp_addr = NULL;
847 unp->unp_socket = NULL;
848 freeunp = unp_pcb_rele(unp);
849 if (saved_unp_addr != NULL)
850 free(saved_unp_addr, M_SONAME);
857 if (local_unp_rights)
858 taskqueue_enqueue_timeout(taskqueue_thread, &unp_gc_task, -1);
862 uipc_disconnect(struct socket *so)
864 struct unpcb *unp, *unp2;
868 KASSERT(unp != NULL, ("uipc_disconnect: unp == NULL"));
871 if ((unp2 = unp->unp_conn) == NULL) {
875 if (__predict_true(unp != unp2)) {
876 unp_pcb_owned_lock2(unp, unp2, freed);
877 if (__predict_false(freed)) {
884 unp_disconnect(unp, unp2);
885 if (unp_pcb_rele(unp) == 0)
887 if ((unp != unp2) && unp_pcb_rele(unp2) == 0)
888 UNP_PCB_UNLOCK(unp2);
893 uipc_listen(struct socket *so, int backlog, struct thread *td)
898 if (so->so_type != SOCK_STREAM && so->so_type != SOCK_SEQPACKET)
902 KASSERT(unp != NULL, ("uipc_listen: unp == NULL"));
905 if (unp->unp_vnode == NULL) {
906 /* Already connected or not bound to an address. */
907 error = unp->unp_conn != NULL ? EINVAL : EDESTADDRREQ;
913 error = solisten_proto_check(so);
915 cru2x(td->td_ucred, &unp->unp_peercred);
916 solisten_proto(so, backlog);
924 uipc_peeraddr(struct socket *so, struct sockaddr **nam)
926 struct unpcb *unp, *unp2;
927 const struct sockaddr *sa;
930 KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL"));
932 *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
935 * XXX: It seems that this test always fails even when connection is
936 * established. So, this else clause is added as workaround to
937 * return PF_LOCAL sockaddr.
939 unp2 = unp->unp_conn;
942 if (unp2->unp_addr != NULL)
943 sa = (struct sockaddr *) unp2->unp_addr;
946 bcopy(sa, *nam, sa->sa_len);
947 UNP_PCB_UNLOCK(unp2);
950 bcopy(sa, *nam, sa->sa_len);
957 uipc_rcvd(struct socket *so, int flags)
959 struct unpcb *unp, *unp2;
964 KASSERT(unp != NULL, ("%s: unp == NULL", __func__));
965 KASSERT(so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET,
966 ("%s: socktype %d", __func__, so->so_type));
969 * Adjust backpressure on sender and wakeup any waiting to write.
971 * The unp lock is acquired to maintain the validity of the unp_conn
972 * pointer; no lock on unp2 is required as unp2->unp_socket will be
973 * static as long as we don't permit unp2 to disconnect from unp,
974 * which is prevented by the lock on unp. We cache values from
975 * so_rcv to avoid holding the so_rcv lock over the entire
976 * transaction on the remote so_snd.
978 SOCKBUF_LOCK(&so->so_rcv);
979 mbcnt = so->so_rcv.sb_mbcnt;
980 sbcc = sbavail(&so->so_rcv);
981 SOCKBUF_UNLOCK(&so->so_rcv);
983 * There is a benign race condition at this point. If we're planning to
984 * clear SB_STOP, but uipc_send is called on the connected socket at
985 * this instant, it might add data to the sockbuf and set SB_STOP. Then
986 * we would erroneously clear SB_STOP below, even though the sockbuf is
987 * full. The race is benign because the only ill effect is to allow the
988 * sockbuf to exceed its size limit, and the size limits are not
989 * strictly guaranteed anyway.
992 unp2 = unp->unp_conn;
997 so2 = unp2->unp_socket;
998 SOCKBUF_LOCK(&so2->so_snd);
999 if (sbcc < so2->so_snd.sb_hiwat && mbcnt < so2->so_snd.sb_mbmax)
1000 so2->so_snd.sb_flags &= ~SB_STOP;
1001 sowwakeup_locked(so2);
1002 UNP_PCB_UNLOCK(unp);
1007 connect_internal(struct socket *so, struct sockaddr *nam, struct thread *td)
1013 if (unp->unp_conn != NULL)
1015 error = unp_connect(so, nam, td);
1019 if (unp->unp_conn == NULL) {
1020 UNP_PCB_UNLOCK(unp);
1029 uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
1030 struct mbuf *control, struct thread *td)
1032 struct unpcb *unp, *unp2;
1037 unp = sotounpcb(so);
1038 KASSERT(unp != NULL, ("%s: unp == NULL", __func__));
1039 KASSERT(so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM ||
1040 so->so_type == SOCK_SEQPACKET,
1041 ("%s: socktype %d", __func__, so->so_type));
1044 if (flags & PRUS_OOB) {
1048 if (control != NULL && (error = unp_internalize(&control, td)))
1052 switch (so->so_type) {
1055 const struct sockaddr *from;
1059 * We return with UNP_PCB_LOCK_HELD so we know that
1060 * the reference is live if the pointer is valid.
1062 if ((error = connect_internal(so, nam, td)))
1064 MPASS(unp->unp_conn != NULL);
1065 unp2 = unp->unp_conn;
1070 * Because connect() and send() are non-atomic in a sendto()
1071 * with a target address, it's possible that the socket will
1072 * have disconnected before the send() can run. In that case
1073 * return the slightly counter-intuitive but otherwise
1074 * correct error that the socket is not connected.
1076 if ((unp2 = unp->unp_conn) == NULL) {
1077 UNP_PCB_UNLOCK(unp);
1082 if (__predict_false(unp == unp2)) {
1083 if (unp->unp_socket == NULL) {
1089 unp_pcb_owned_lock2(unp, unp2, freed);
1090 if (__predict_false(freed)) {
1091 UNP_PCB_UNLOCK(unp);
1096 * The socket referencing unp2 may have been closed
1097 * or unp may have been disconnected if the unp lock
1098 * was dropped to acquire unp2.
1100 if (__predict_false(unp->unp_conn == NULL) ||
1101 unp2->unp_socket == NULL) {
1102 UNP_PCB_UNLOCK(unp);
1103 if (unp_pcb_rele(unp2) == 0)
1104 UNP_PCB_UNLOCK(unp2);
1109 if (unp2->unp_flags & UNP_WANTCRED)
1110 control = unp_addsockcred(td, control);
1111 if (unp->unp_addr != NULL)
1112 from = (struct sockaddr *)unp->unp_addr;
1115 so2 = unp2->unp_socket;
1116 SOCKBUF_LOCK(&so2->so_rcv);
1117 if (sbappendaddr_locked(&so2->so_rcv, from, m,
1119 sorwakeup_locked(so2);
1123 SOCKBUF_UNLOCK(&so2->so_rcv);
1127 unp_disconnect(unp, unp2);
1128 if (__predict_true(unp != unp2))
1129 UNP_PCB_UNLOCK(unp2);
1130 UNP_PCB_UNLOCK(unp);
1134 case SOCK_SEQPACKET:
1136 if ((so->so_state & SS_ISCONNECTED) == 0) {
1138 if ((error = connect_internal(so, nam, td)))
1144 } else if ((unp2 = unp->unp_conn) == NULL) {
1147 } else if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1152 if ((unp2 = unp->unp_conn) == NULL) {
1153 UNP_PCB_UNLOCK(unp);
1158 unp_pcb_owned_lock2(unp, unp2, freed);
1159 UNP_PCB_UNLOCK(unp);
1160 if (__predict_false(freed)) {
1164 if ((so2 = unp2->unp_socket) == NULL) {
1165 UNP_PCB_UNLOCK(unp2);
1169 SOCKBUF_LOCK(&so2->so_rcv);
1170 if (unp2->unp_flags & UNP_WANTCRED) {
1172 * Credentials are passed only once on SOCK_STREAM
1173 * and SOCK_SEQPACKET.
1175 unp2->unp_flags &= ~UNP_WANTCRED;
1176 control = unp_addsockcred(td, control);
1179 * Send to paired receive port, and then reduce send buffer
1180 * hiwater marks to maintain backpressure. Wake up readers.
1182 switch (so->so_type) {
1184 if (control != NULL) {
1185 if (sbappendcontrol_locked(&so2->so_rcv, m,
1189 sbappend_locked(&so2->so_rcv, m, flags);
1192 case SOCK_SEQPACKET: {
1193 const struct sockaddr *from;
1197 * Don't check for space available in so2->so_rcv.
1198 * Unix domain sockets only check for space in the
1199 * sending sockbuf, and that check is performed one
1200 * level up the stack.
1202 if (sbappendaddr_nospacecheck_locked(&so2->so_rcv,
1209 mbcnt = so2->so_rcv.sb_mbcnt;
1210 sbcc = sbavail(&so2->so_rcv);
1212 sorwakeup_locked(so2);
1214 SOCKBUF_UNLOCK(&so2->so_rcv);
1217 * The PCB lock on unp2 protects the SB_STOP flag. Without it,
1218 * it would be possible for uipc_rcvd to be called at this
1219 * point, drain the receiving sockbuf, clear SB_STOP, and then
1220 * we would set SB_STOP below. That could lead to an empty
1221 * sockbuf having SB_STOP set
1223 SOCKBUF_LOCK(&so->so_snd);
1224 if (sbcc >= so->so_snd.sb_hiwat || mbcnt >= so->so_snd.sb_mbmax)
1225 so->so_snd.sb_flags |= SB_STOP;
1226 SOCKBUF_UNLOCK(&so->so_snd);
1227 UNP_PCB_UNLOCK(unp2);
1233 * PRUS_EOF is equivalent to pru_send followed by pru_shutdown.
1235 if (flags & PRUS_EOF) {
1239 UNP_PCB_UNLOCK(unp);
1241 if (control != NULL && error != 0)
1242 unp_dispose_mbuf(control);
1245 if (control != NULL)
1248 * In case of PRUS_NOTREADY, uipc_ready() is responsible
1249 * for freeing memory.
1251 if (m != NULL && (flags & PRUS_NOTREADY) == 0)
1257 uipc_ready(struct socket *so, struct mbuf *m, int count)
1259 struct unpcb *unp, *unp2;
1263 unp = sotounpcb(so);
1266 if ((unp2 = unp->unp_conn) == NULL) {
1267 UNP_PCB_UNLOCK(unp);
1271 if (UNP_PCB_TRYLOCK(unp2) == 0) {
1273 UNP_PCB_UNLOCK(unp);
1275 if (unp_pcb_rele(unp2))
1278 UNP_PCB_UNLOCK(unp);
1280 so2 = unp2->unp_socket;
1282 SOCKBUF_LOCK(&so2->so_rcv);
1283 if ((error = sbready(&so2->so_rcv, m, count)) == 0)
1284 sorwakeup_locked(so2);
1286 SOCKBUF_UNLOCK(&so2->so_rcv);
1288 UNP_PCB_UNLOCK(unp2);
1292 for (int i = 0; i < count; i++)
1294 return (ECONNRESET);
1298 uipc_sense(struct socket *so, struct stat *sb)
1302 unp = sotounpcb(so);
1303 KASSERT(unp != NULL, ("uipc_sense: unp == NULL"));
1305 sb->st_blksize = so->so_snd.sb_hiwat;
1308 if (unp->unp_ino == 0)
1309 unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino;
1310 sb->st_ino = unp->unp_ino;
1311 UNP_PCB_UNLOCK(unp);
1316 uipc_shutdown(struct socket *so)
1320 unp = sotounpcb(so);
1321 KASSERT(unp != NULL, ("uipc_shutdown: unp == NULL"));
1326 UNP_PCB_UNLOCK(unp);
1331 uipc_sockaddr(struct socket *so, struct sockaddr **nam)
1334 const struct sockaddr *sa;
1336 unp = sotounpcb(so);
1337 KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL"));
1339 *nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
1341 if (unp->unp_addr != NULL)
1342 sa = (struct sockaddr *) unp->unp_addr;
1345 bcopy(sa, *nam, sa->sa_len);
1346 UNP_PCB_UNLOCK(unp);
1350 static struct pr_usrreqs uipc_usrreqs_dgram = {
1351 .pru_abort = uipc_abort,
1352 .pru_accept = uipc_accept,
1353 .pru_attach = uipc_attach,
1354 .pru_bind = uipc_bind,
1355 .pru_bindat = uipc_bindat,
1356 .pru_connect = uipc_connect,
1357 .pru_connectat = uipc_connectat,
1358 .pru_connect2 = uipc_connect2,
1359 .pru_detach = uipc_detach,
1360 .pru_disconnect = uipc_disconnect,
1361 .pru_listen = uipc_listen,
1362 .pru_peeraddr = uipc_peeraddr,
1363 .pru_rcvd = uipc_rcvd,
1364 .pru_send = uipc_send,
1365 .pru_sense = uipc_sense,
1366 .pru_shutdown = uipc_shutdown,
1367 .pru_sockaddr = uipc_sockaddr,
1368 .pru_soreceive = soreceive_dgram,
1369 .pru_close = uipc_close,
1372 static struct pr_usrreqs uipc_usrreqs_seqpacket = {
1373 .pru_abort = uipc_abort,
1374 .pru_accept = uipc_accept,
1375 .pru_attach = uipc_attach,
1376 .pru_bind = uipc_bind,
1377 .pru_bindat = uipc_bindat,
1378 .pru_connect = uipc_connect,
1379 .pru_connectat = uipc_connectat,
1380 .pru_connect2 = uipc_connect2,
1381 .pru_detach = uipc_detach,
1382 .pru_disconnect = uipc_disconnect,
1383 .pru_listen = uipc_listen,
1384 .pru_peeraddr = uipc_peeraddr,
1385 .pru_rcvd = uipc_rcvd,
1386 .pru_send = uipc_send,
1387 .pru_sense = uipc_sense,
1388 .pru_shutdown = uipc_shutdown,
1389 .pru_sockaddr = uipc_sockaddr,
1390 .pru_soreceive = soreceive_generic, /* XXX: or...? */
1391 .pru_close = uipc_close,
1394 static struct pr_usrreqs uipc_usrreqs_stream = {
1395 .pru_abort = uipc_abort,
1396 .pru_accept = uipc_accept,
1397 .pru_attach = uipc_attach,
1398 .pru_bind = uipc_bind,
1399 .pru_bindat = uipc_bindat,
1400 .pru_connect = uipc_connect,
1401 .pru_connectat = uipc_connectat,
1402 .pru_connect2 = uipc_connect2,
1403 .pru_detach = uipc_detach,
1404 .pru_disconnect = uipc_disconnect,
1405 .pru_listen = uipc_listen,
1406 .pru_peeraddr = uipc_peeraddr,
1407 .pru_rcvd = uipc_rcvd,
1408 .pru_send = uipc_send,
1409 .pru_ready = uipc_ready,
1410 .pru_sense = uipc_sense,
1411 .pru_shutdown = uipc_shutdown,
1412 .pru_sockaddr = uipc_sockaddr,
1413 .pru_soreceive = soreceive_generic,
1414 .pru_close = uipc_close,
1418 uipc_ctloutput(struct socket *so, struct sockopt *sopt)
1424 if (sopt->sopt_level != 0)
1427 unp = sotounpcb(so);
1428 KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL"));
1430 switch (sopt->sopt_dir) {
1432 switch (sopt->sopt_name) {
1433 case LOCAL_PEERCRED:
1435 if (unp->unp_flags & UNP_HAVEPC)
1436 xu = unp->unp_peercred;
1438 if (so->so_type == SOCK_STREAM)
1443 UNP_PCB_UNLOCK(unp);
1445 error = sooptcopyout(sopt, &xu, sizeof(xu));
1449 /* Unlocked read. */
1450 optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0;
1451 error = sooptcopyout(sopt, &optval, sizeof(optval));
1454 case LOCAL_CONNWAIT:
1455 /* Unlocked read. */
1456 optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0;
1457 error = sooptcopyout(sopt, &optval, sizeof(optval));
1467 switch (sopt->sopt_name) {
1469 case LOCAL_CONNWAIT:
1470 error = sooptcopyin(sopt, &optval, sizeof(optval),
1475 #define OPTSET(bit) do { \
1476 UNP_PCB_LOCK(unp); \
1478 unp->unp_flags |= bit; \
1480 unp->unp_flags &= ~bit; \
1481 UNP_PCB_UNLOCK(unp); \
1484 switch (sopt->sopt_name) {
1486 OPTSET(UNP_WANTCRED);
1489 case LOCAL_CONNWAIT:
1490 OPTSET(UNP_CONNWAIT);
1499 error = ENOPROTOOPT;
1512 unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1515 return (unp_connectat(AT_FDCWD, so, nam, td));
1519 unp_connectat(int fd, struct socket *so, struct sockaddr *nam,
1522 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
1525 struct unpcb *unp, *unp2, *unp3;
1526 struct nameidata nd;
1527 char buf[SOCK_MAXADDRLEN];
1528 struct sockaddr *sa;
1529 cap_rights_t rights;
1530 int error, len, freed;
1533 if (nam->sa_family != AF_UNIX)
1534 return (EAFNOSUPPORT);
1535 if (nam->sa_len > sizeof(struct sockaddr_un))
1537 len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
1540 bcopy(soun->sun_path, buf, len);
1543 unp = sotounpcb(so);
1545 if (unp->unp_flags & UNP_CONNECTING) {
1546 UNP_PCB_UNLOCK(unp);
1549 unp->unp_flags |= UNP_CONNECTING;
1550 UNP_PCB_UNLOCK(unp);
1552 sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
1553 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
1554 UIO_SYSSPACE, buf, fd, cap_rights_init(&rights, CAP_CONNECTAT), td);
1560 ASSERT_VOP_LOCKED(vp, "unp_connect");
1561 NDFREE(&nd, NDF_ONLY_PNBUF);
1565 if (vp->v_type != VSOCK) {
1570 error = mac_vnode_check_open(td->td_ucred, vp, VWRITE | VREAD);
1574 error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td);
1578 unp = sotounpcb(so);
1579 KASSERT(unp != NULL, ("unp_connect: unp == NULL"));
1581 vplock = mtx_pool_find(mtxpool_sleep, vp);
1583 VOP_UNP_CONNECT(vp, &unp2);
1585 error = ECONNREFUSED;
1588 so2 = unp2->unp_socket;
1589 if (so->so_type != so2->so_type) {
1593 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
1594 if (so2->so_options & SO_ACCEPTCONN) {
1595 CURVNET_SET(so2->so_vnet);
1596 so2 = sonewconn(so2, 0);
1601 error = ECONNREFUSED;
1604 unp3 = sotounpcb(so2);
1605 unp_pcb_lock2(unp2, unp3);
1606 if (unp2->unp_addr != NULL) {
1607 bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len);
1608 unp3->unp_addr = (struct sockaddr_un *) sa;
1613 * The connector's (client's) credentials are copied from its
1614 * process structure at the time of connect() (which is now).
1616 cru2x(td->td_ucred, &unp3->unp_peercred);
1617 unp3->unp_flags |= UNP_HAVEPC;
1620 * The receiver's (server's) credentials are copied from the
1621 * unp_peercred member of socket on which the former called
1622 * listen(); uipc_listen() cached that process's credentials
1623 * at that time so we can use them now.
1625 memcpy(&unp->unp_peercred, &unp2->unp_peercred,
1626 sizeof(unp->unp_peercred));
1627 unp->unp_flags |= UNP_HAVEPC;
1628 if (unp2->unp_flags & UNP_WANTCRED)
1629 unp3->unp_flags |= UNP_WANTCRED;
1630 UNP_PCB_UNLOCK(unp2);
1632 unp_pcb_owned_lock2(unp2, unp, freed);
1633 if (__predict_false(freed)) {
1634 UNP_PCB_UNLOCK(unp2);
1635 error = ECONNREFUSED;
1639 mac_socketpeer_set_from_socket(so, so2);
1640 mac_socketpeer_set_from_socket(so2, so);
1646 unp_pcb_lock2(unp, unp2);
1648 KASSERT(unp2 != NULL && so2 != NULL && unp2->unp_socket == so2 &&
1649 sotounpcb(so2) == unp2,
1650 ("%s: unp2 %p so2 %p", __func__, unp2, so2));
1651 error = unp_connect2(so, so2, PRU_CONNECT);
1653 UNP_PCB_UNLOCK(unp2);
1654 UNP_PCB_UNLOCK(unp);
1663 unp->unp_flags &= ~UNP_CONNECTING;
1664 UNP_PCB_UNLOCK(unp);
1669 unp_connect2(struct socket *so, struct socket *so2, int req)
1674 unp = sotounpcb(so);
1675 KASSERT(unp != NULL, ("unp_connect2: unp == NULL"));
1676 unp2 = sotounpcb(so2);
1677 KASSERT(unp2 != NULL, ("unp_connect2: unp2 == NULL"));
1679 UNP_PCB_LOCK_ASSERT(unp);
1680 UNP_PCB_LOCK_ASSERT(unp2);
1682 if (so2->so_type != so->so_type)
1683 return (EPROTOTYPE);
1684 unp2->unp_flags &= ~UNP_NASCENT;
1685 unp->unp_conn = unp2;
1688 switch (so->so_type) {
1690 UNP_REF_LIST_LOCK();
1691 LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
1692 UNP_REF_LIST_UNLOCK();
1697 case SOCK_SEQPACKET:
1698 unp2->unp_conn = unp;
1699 if (req == PRU_CONNECT &&
1700 ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT))
1708 panic("unp_connect2");
1714 unp_disconnect(struct unpcb *unp, struct unpcb *unp2)
1716 struct socket *so, *so2;
1719 KASSERT(unp2 != NULL, ("unp_disconnect: unp2 == NULL"));
1721 UNP_PCB_LOCK_ASSERT(unp);
1722 UNP_PCB_LOCK_ASSERT(unp2);
1724 if (unp->unp_conn == NULL && unp2->unp_conn == NULL)
1727 MPASS(unp->unp_conn == unp2);
1728 unp->unp_conn = NULL;
1729 so = unp->unp_socket;
1730 so2 = unp2->unp_socket;
1731 switch (unp->unp_socket->so_type) {
1733 UNP_REF_LIST_LOCK();
1734 LIST_REMOVE(unp, unp_reflink);
1735 UNP_REF_LIST_UNLOCK();
1738 so->so_state &= ~SS_ISCONNECTED;
1744 case SOCK_SEQPACKET:
1746 soisdisconnected(so);
1747 MPASS(unp2->unp_conn == unp);
1748 unp2->unp_conn = NULL;
1750 soisdisconnected(so2);
1753 freed = unp_pcb_rele(unp);
1755 freed = unp_pcb_rele(unp2);
1760 * unp_pcblist() walks the global list of struct unpcb's to generate a
1761 * pointer list, bumping the refcount on each unpcb. It then copies them out
1762 * sequentially, validating the generation number on each to see if it has
1763 * been detached. All of this is necessary because copyout() may sleep on
1767 unp_pcblist(SYSCTL_HANDLER_ARGS)
1769 struct unpcb *unp, **unp_list;
1771 struct xunpgen *xug;
1772 struct unp_head *head;
1775 int error, freeunp, n;
1777 switch ((intptr_t)arg1) {
1786 case SOCK_SEQPACKET:
1791 panic("unp_pcblist: arg1 %d", (int)(intptr_t)arg1);
1795 * The process of preparing the PCB list is too time-consuming and
1796 * resource-intensive to repeat twice on every request.
1798 if (req->oldptr == NULL) {
1800 req->oldidx = 2 * (sizeof *xug)
1801 + (n + n/8) * sizeof(struct xunpcb);
1805 if (req->newptr != NULL)
1809 * OK, now we're committed to doing something.
1811 xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK);
1813 gencnt = unp_gencnt;
1817 xug->xug_len = sizeof *xug;
1819 xug->xug_gen = gencnt;
1820 xug->xug_sogen = so_gencnt;
1821 error = SYSCTL_OUT(req, xug, sizeof *xug);
1827 unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
1830 for (unp = LIST_FIRST(head), i = 0; unp && i < n;
1831 unp = LIST_NEXT(unp, unp_link)) {
1833 if (unp->unp_gencnt <= gencnt) {
1834 if (cr_cansee(req->td->td_ucred,
1835 unp->unp_socket->so_cred)) {
1836 UNP_PCB_UNLOCK(unp);
1839 unp_list[i++] = unp;
1842 UNP_PCB_UNLOCK(unp);
1845 n = i; /* In case we lost some during malloc. */
1848 xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO);
1849 for (i = 0; i < n; i++) {
1852 freeunp = unp_pcb_rele(unp);
1854 if (freeunp == 0 && unp->unp_gencnt <= gencnt) {
1855 xu->xu_len = sizeof *xu;
1856 xu->xu_unpp = (uintptr_t)unp;
1858 * XXX - need more locking here to protect against
1859 * connect/disconnect races for SMP.
1861 if (unp->unp_addr != NULL)
1862 bcopy(unp->unp_addr, &xu->xu_addr,
1863 unp->unp_addr->sun_len);
1865 bzero(&xu->xu_addr, sizeof(xu->xu_addr));
1866 if (unp->unp_conn != NULL &&
1867 unp->unp_conn->unp_addr != NULL)
1868 bcopy(unp->unp_conn->unp_addr,
1870 unp->unp_conn->unp_addr->sun_len);
1872 bzero(&xu->xu_caddr, sizeof(xu->xu_caddr));
1873 xu->unp_vnode = (uintptr_t)unp->unp_vnode;
1874 xu->unp_conn = (uintptr_t)unp->unp_conn;
1875 xu->xu_firstref = (uintptr_t)LIST_FIRST(&unp->unp_refs);
1876 xu->xu_nextref = (uintptr_t)LIST_NEXT(unp, unp_reflink);
1877 xu->unp_gencnt = unp->unp_gencnt;
1878 sotoxsocket(unp->unp_socket, &xu->xu_socket);
1879 UNP_PCB_UNLOCK(unp);
1880 error = SYSCTL_OUT(req, xu, sizeof *xu);
1881 } else if (freeunp == 0)
1882 UNP_PCB_UNLOCK(unp);
1887 * Give the user an updated idea of our state. If the
1888 * generation differs from what we told her before, she knows
1889 * that something happened while we were processing this
1890 * request, and it might be necessary to retry.
1892 xug->xug_gen = unp_gencnt;
1893 xug->xug_sogen = so_gencnt;
1894 xug->xug_count = unp_count;
1895 error = SYSCTL_OUT(req, xug, sizeof *xug);
1897 free(unp_list, M_TEMP);
1902 SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLTYPE_OPAQUE | CTLFLAG_RD,
1903 (void *)(intptr_t)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
1904 "List of active local datagram sockets");
1905 SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLTYPE_OPAQUE | CTLFLAG_RD,
1906 (void *)(intptr_t)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
1907 "List of active local stream sockets");
1908 SYSCTL_PROC(_net_local_seqpacket, OID_AUTO, pcblist,
1909 CTLTYPE_OPAQUE | CTLFLAG_RD,
1910 (void *)(intptr_t)SOCK_SEQPACKET, 0, unp_pcblist, "S,xunpcb",
1911 "List of active local seqpacket sockets");
1914 unp_shutdown(struct unpcb *unp)
1919 UNP_PCB_LOCK_ASSERT(unp);
1921 unp2 = unp->unp_conn;
1922 if ((unp->unp_socket->so_type == SOCK_STREAM ||
1923 (unp->unp_socket->so_type == SOCK_SEQPACKET)) && unp2 != NULL) {
1924 so = unp2->unp_socket;
1931 unp_drop(struct unpcb *unp)
1933 struct socket *so = unp->unp_socket;
1938 * Regardless of whether the socket's peer dropped the connection
1939 * with this socket by aborting or disconnecting, POSIX requires
1940 * that ECONNRESET is returned.
1942 /* acquire a reference so that unp isn't freed from underneath us */
1946 so->so_error = ECONNRESET;
1947 unp2 = unp->unp_conn;
1949 unp_disconnect(unp, unp2);
1950 } else if (unp2 != NULL) {
1952 unp_pcb_owned_lock2(unp, unp2, freed);
1953 unp_disconnect(unp, unp2);
1954 if (unp_pcb_rele(unp2) == 0)
1955 UNP_PCB_UNLOCK(unp2);
1957 if (unp_pcb_rele(unp) == 0)
1958 UNP_PCB_UNLOCK(unp);
1962 unp_freerights(struct filedescent **fdep, int fdcount)
1967 KASSERT(fdcount > 0, ("%s: fdcount %d", __func__, fdcount));
1969 for (i = 0; i < fdcount; i++) {
1970 fp = fdep[i]->fde_file;
1971 filecaps_free(&fdep[i]->fde_caps);
1974 free(fdep[0], M_FILECAPS);
1978 unp_externalize(struct mbuf *control, struct mbuf **controlp, int flags)
1980 struct thread *td = curthread; /* XXX */
1981 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1984 struct filedesc *fdesc = td->td_proc->p_fd;
1985 struct filedescent **fdep;
1987 socklen_t clen = control->m_len, datalen;
1991 UNP_LINK_UNLOCK_ASSERT();
1994 if (controlp != NULL) /* controlp == NULL => free control messages */
1996 while (cm != NULL) {
1997 if (sizeof(*cm) > clen || cm->cmsg_len > clen) {
2001 data = CMSG_DATA(cm);
2002 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
2003 if (cm->cmsg_level == SOL_SOCKET
2004 && cm->cmsg_type == SCM_RIGHTS) {
2005 newfds = datalen / sizeof(*fdep);
2010 /* If we're not outputting the descriptors free them. */
2011 if (error || controlp == NULL) {
2012 unp_freerights(fdep, newfds);
2015 FILEDESC_XLOCK(fdesc);
2018 * Now change each pointer to an fd in the global
2019 * table to an integer that is the index to the local
2020 * fd table entry that we set up to point to the
2021 * global one we are transferring.
2023 newlen = newfds * sizeof(int);
2024 *controlp = sbcreatecontrol(NULL, newlen,
2025 SCM_RIGHTS, SOL_SOCKET);
2026 if (*controlp == NULL) {
2027 FILEDESC_XUNLOCK(fdesc);
2029 unp_freerights(fdep, newfds);
2034 CMSG_DATA(mtod(*controlp, struct cmsghdr *));
2035 if (fdallocn(td, 0, fdp, newfds) != 0) {
2036 FILEDESC_XUNLOCK(fdesc);
2038 unp_freerights(fdep, newfds);
2043 for (i = 0; i < newfds; i++, fdp++) {
2044 _finstall(fdesc, fdep[i]->fde_file, *fdp,
2045 (flags & MSG_CMSG_CLOEXEC) != 0 ? UF_EXCLOSE : 0,
2046 &fdep[i]->fde_caps);
2047 unp_externalize_fp(fdep[i]->fde_file);
2049 FILEDESC_XUNLOCK(fdesc);
2050 free(fdep[0], M_FILECAPS);
2052 /* We can just copy anything else across. */
2053 if (error || controlp == NULL)
2055 *controlp = sbcreatecontrol(NULL, datalen,
2056 cm->cmsg_type, cm->cmsg_level);
2057 if (*controlp == NULL) {
2062 CMSG_DATA(mtod(*controlp, struct cmsghdr *)),
2065 controlp = &(*controlp)->m_next;
2068 if (CMSG_SPACE(datalen) < clen) {
2069 clen -= CMSG_SPACE(datalen);
2070 cm = (struct cmsghdr *)
2071 ((caddr_t)cm + CMSG_SPACE(datalen));
2083 unp_zone_change(void *tag)
2086 uma_zone_set_max(unp_zone, maxsockets);
2094 if (!IS_DEFAULT_VNET(curvnet))
2097 unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL,
2098 NULL, NULL, UMA_ALIGN_CACHE, 0);
2099 if (unp_zone == NULL)
2101 uma_zone_set_max(unp_zone, maxsockets);
2102 uma_zone_set_warning(unp_zone, "kern.ipc.maxsockets limit reached");
2103 EVENTHANDLER_REGISTER(maxsockets_change, unp_zone_change,
2104 NULL, EVENTHANDLER_PRI_ANY);
2105 LIST_INIT(&unp_dhead);
2106 LIST_INIT(&unp_shead);
2107 LIST_INIT(&unp_sphead);
2108 SLIST_INIT(&unp_defers);
2109 TIMEOUT_TASK_INIT(taskqueue_thread, &unp_gc_task, 0, unp_gc, NULL);
2110 TASK_INIT(&unp_defer_task, 0, unp_process_defers, NULL);
2111 UNP_LINK_LOCK_INIT();
2112 UNP_DEFERRED_LOCK_INIT();
2116 unp_internalize(struct mbuf **controlp, struct thread *td)
2118 struct mbuf *control = *controlp;
2119 struct proc *p = td->td_proc;
2120 struct filedesc *fdesc = p->p_fd;
2122 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
2123 struct cmsgcred *cmcred;
2124 struct filedescent *fde, **fdep, *fdev;
2127 struct timespec *ts;
2130 socklen_t clen = control->m_len, datalen;
2134 UNP_LINK_UNLOCK_ASSERT();
2138 while (cm != NULL) {
2139 if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET
2140 || cm->cmsg_len > clen || cm->cmsg_len < sizeof(*cm)) {
2144 data = CMSG_DATA(cm);
2145 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
2147 switch (cm->cmsg_type) {
2149 * Fill in credential information.
2152 *controlp = sbcreatecontrol(NULL, sizeof(*cmcred),
2153 SCM_CREDS, SOL_SOCKET);
2154 if (*controlp == NULL) {
2158 cmcred = (struct cmsgcred *)
2159 CMSG_DATA(mtod(*controlp, struct cmsghdr *));
2160 cmcred->cmcred_pid = p->p_pid;
2161 cmcred->cmcred_uid = td->td_ucred->cr_ruid;
2162 cmcred->cmcred_gid = td->td_ucred->cr_rgid;
2163 cmcred->cmcred_euid = td->td_ucred->cr_uid;
2164 cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups,
2166 for (i = 0; i < cmcred->cmcred_ngroups; i++)
2167 cmcred->cmcred_groups[i] =
2168 td->td_ucred->cr_groups[i];
2172 oldfds = datalen / sizeof (int);
2176 * Check that all the FDs passed in refer to legal
2177 * files. If not, reject the entire operation.
2180 FILEDESC_SLOCK(fdesc);
2181 for (i = 0; i < oldfds; i++, fdp++) {
2182 fp = fget_locked(fdesc, *fdp);
2184 FILEDESC_SUNLOCK(fdesc);
2188 if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) {
2189 FILEDESC_SUNLOCK(fdesc);
2197 * Now replace the integer FDs with pointers to the
2198 * file structure and capability rights.
2200 newlen = oldfds * sizeof(fdep[0]);
2201 *controlp = sbcreatecontrol(NULL, newlen,
2202 SCM_RIGHTS, SOL_SOCKET);
2203 if (*controlp == NULL) {
2204 FILEDESC_SUNLOCK(fdesc);
2209 fdep = (struct filedescent **)
2210 CMSG_DATA(mtod(*controlp, struct cmsghdr *));
2211 fdev = malloc(sizeof(*fdev) * oldfds, M_FILECAPS,
2213 for (i = 0; i < oldfds; i++, fdev++, fdp++) {
2214 fde = &fdesc->fd_ofiles[*fdp];
2216 fdep[i]->fde_file = fde->fde_file;
2217 filecaps_copy(&fde->fde_caps,
2218 &fdep[i]->fde_caps, true);
2219 unp_internalize_fp(fdep[i]->fde_file);
2221 FILEDESC_SUNLOCK(fdesc);
2225 *controlp = sbcreatecontrol(NULL, sizeof(*tv),
2226 SCM_TIMESTAMP, SOL_SOCKET);
2227 if (*controlp == NULL) {
2231 tv = (struct timeval *)
2232 CMSG_DATA(mtod(*controlp, struct cmsghdr *));
2237 *controlp = sbcreatecontrol(NULL, sizeof(*bt),
2238 SCM_BINTIME, SOL_SOCKET);
2239 if (*controlp == NULL) {
2243 bt = (struct bintime *)
2244 CMSG_DATA(mtod(*controlp, struct cmsghdr *));
2249 *controlp = sbcreatecontrol(NULL, sizeof(*ts),
2250 SCM_REALTIME, SOL_SOCKET);
2251 if (*controlp == NULL) {
2255 ts = (struct timespec *)
2256 CMSG_DATA(mtod(*controlp, struct cmsghdr *));
2261 *controlp = sbcreatecontrol(NULL, sizeof(*ts),
2262 SCM_MONOTONIC, SOL_SOCKET);
2263 if (*controlp == NULL) {
2267 ts = (struct timespec *)
2268 CMSG_DATA(mtod(*controlp, struct cmsghdr *));
2277 controlp = &(*controlp)->m_next;
2278 if (CMSG_SPACE(datalen) < clen) {
2279 clen -= CMSG_SPACE(datalen);
2280 cm = (struct cmsghdr *)
2281 ((caddr_t)cm + CMSG_SPACE(datalen));
2293 static struct mbuf *
2294 unp_addsockcred(struct thread *td, struct mbuf *control)
2296 struct mbuf *m, *n, *n_prev;
2297 struct sockcred *sc;
2298 const struct cmsghdr *cm;
2302 ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX);
2303 m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET);
2307 sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *));
2308 sc->sc_uid = td->td_ucred->cr_ruid;
2309 sc->sc_euid = td->td_ucred->cr_uid;
2310 sc->sc_gid = td->td_ucred->cr_rgid;
2311 sc->sc_egid = td->td_ucred->cr_gid;
2312 sc->sc_ngroups = ngroups;
2313 for (i = 0; i < sc->sc_ngroups; i++)
2314 sc->sc_groups[i] = td->td_ucred->cr_groups[i];
2317 * Unlink SCM_CREDS control messages (struct cmsgcred), since just
2318 * created SCM_CREDS control message (struct sockcred) has another
2321 if (control != NULL)
2322 for (n = control, n_prev = NULL; n != NULL;) {
2323 cm = mtod(n, struct cmsghdr *);
2324 if (cm->cmsg_level == SOL_SOCKET &&
2325 cm->cmsg_type == SCM_CREDS) {
2327 control = n->m_next;
2329 n_prev->m_next = n->m_next;
2337 /* Prepend it to the head. */
2338 m->m_next = control;
2342 static struct unpcb *
2343 fptounp(struct file *fp)
2347 if (fp->f_type != DTYPE_SOCKET)
2349 if ((so = fp->f_data) == NULL)
2351 if (so->so_proto->pr_domain != &localdomain)
2353 return sotounpcb(so);
2357 unp_discard(struct file *fp)
2359 struct unp_defer *dr;
2361 if (unp_externalize_fp(fp)) {
2362 dr = malloc(sizeof(*dr), M_TEMP, M_WAITOK);
2364 UNP_DEFERRED_LOCK();
2365 SLIST_INSERT_HEAD(&unp_defers, dr, ud_link);
2366 UNP_DEFERRED_UNLOCK();
2367 atomic_add_int(&unp_defers_count, 1);
2368 taskqueue_enqueue(taskqueue_thread, &unp_defer_task);
2370 (void) closef(fp, (struct thread *)NULL);
2374 unp_process_defers(void *arg __unused, int pending)
2376 struct unp_defer *dr;
2377 SLIST_HEAD(, unp_defer) drl;
2382 UNP_DEFERRED_LOCK();
2383 if (SLIST_FIRST(&unp_defers) == NULL) {
2384 UNP_DEFERRED_UNLOCK();
2387 SLIST_SWAP(&unp_defers, &drl, unp_defer);
2388 UNP_DEFERRED_UNLOCK();
2390 while ((dr = SLIST_FIRST(&drl)) != NULL) {
2391 SLIST_REMOVE_HEAD(&drl, ud_link);
2392 closef(dr->ud_fp, NULL);
2396 atomic_add_int(&unp_defers_count, -count);
2401 unp_internalize_fp(struct file *fp)
2406 if ((unp = fptounp(fp)) != NULL) {
2408 unp->unp_msgcount++;
2416 unp_externalize_fp(struct file *fp)
2422 if ((unp = fptounp(fp)) != NULL) {
2423 unp->unp_msgcount--;
2433 * unp_defer indicates whether additional work has been defered for a future
2434 * pass through unp_gc(). It is thread local and does not require explicit
2437 static int unp_marked;
2438 static int unp_unreachable;
2441 unp_accessable(struct filedescent **fdep, int fdcount)
2447 for (i = 0; i < fdcount; i++) {
2448 fp = fdep[i]->fde_file;
2449 if ((unp = fptounp(fp)) == NULL)
2451 if (unp->unp_gcflag & UNPGC_REF)
2453 unp->unp_gcflag &= ~UNPGC_DEAD;
2454 unp->unp_gcflag |= UNPGC_REF;
2460 unp_gc_process(struct unpcb *unp)
2462 struct socket *so, *soa;
2465 /* Already processed. */
2466 if (unp->unp_gcflag & UNPGC_SCANNED)
2471 * Check for a socket potentially in a cycle. It must be in a
2472 * queue as indicated by msgcount, and this must equal the file
2473 * reference count. Note that when msgcount is 0 the file is NULL.
2475 if ((unp->unp_gcflag & UNPGC_REF) == 0 && fp &&
2476 unp->unp_msgcount != 0 && fp->f_count == unp->unp_msgcount) {
2477 unp->unp_gcflag |= UNPGC_DEAD;
2482 so = unp->unp_socket;
2484 if (SOLISTENING(so)) {
2486 * Mark all sockets in our accept queue.
2488 TAILQ_FOREACH(soa, &so->sol_comp, so_list) {
2489 if (sotounpcb(soa)->unp_gcflag & UNPGC_IGNORE_RIGHTS)
2491 SOCKBUF_LOCK(&soa->so_rcv);
2492 unp_scan(soa->so_rcv.sb_mb, unp_accessable);
2493 SOCKBUF_UNLOCK(&soa->so_rcv);
2497 * Mark all sockets we reference with RIGHTS.
2499 if ((unp->unp_gcflag & UNPGC_IGNORE_RIGHTS) == 0) {
2500 SOCKBUF_LOCK(&so->so_rcv);
2501 unp_scan(so->so_rcv.sb_mb, unp_accessable);
2502 SOCKBUF_UNLOCK(&so->so_rcv);
2506 unp->unp_gcflag |= UNPGC_SCANNED;
2509 static int unp_recycled;
2510 SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0,
2511 "Number of unreachable sockets claimed by the garbage collector.");
2513 static int unp_taskcount;
2514 SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0,
2515 "Number of times the garbage collector has run.");
2518 unp_gc(__unused void *arg, int pending)
2520 struct unp_head *heads[] = { &unp_dhead, &unp_shead, &unp_sphead,
2522 struct unp_head **head;
2523 struct file *f, **unref;
2530 * First clear all gc flags from previous runs, apart from
2531 * UNPGC_IGNORE_RIGHTS.
2533 for (head = heads; *head != NULL; head++)
2534 LIST_FOREACH(unp, *head, unp_link)
2536 (unp->unp_gcflag & UNPGC_IGNORE_RIGHTS);
2539 * Scan marking all reachable sockets with UNPGC_REF. Once a socket
2540 * is reachable all of the sockets it references are reachable.
2541 * Stop the scan once we do a complete loop without discovering
2542 * a new reachable socket.
2545 unp_unreachable = 0;
2547 for (head = heads; *head != NULL; head++)
2548 LIST_FOREACH(unp, *head, unp_link)
2549 unp_gc_process(unp);
2550 } while (unp_marked);
2552 if (unp_unreachable == 0)
2556 * Allocate space for a local list of dead unpcbs.
2558 unref = malloc(unp_unreachable * sizeof(struct file *),
2562 * Iterate looking for sockets which have been specifically marked
2563 * as as unreachable and store them locally.
2566 for (total = 0, head = heads; *head != NULL; head++)
2567 LIST_FOREACH(unp, *head, unp_link)
2568 if ((unp->unp_gcflag & UNPGC_DEAD) != 0) {
2570 if (unp->unp_msgcount == 0 || f == NULL ||
2571 f->f_count != unp->unp_msgcount)
2575 KASSERT(total <= unp_unreachable,
2576 ("unp_gc: incorrect unreachable count."));
2581 * Now flush all sockets, free'ing rights. This will free the
2582 * struct files associated with these sockets but leave each socket
2583 * with one remaining ref.
2585 for (i = 0; i < total; i++) {
2588 so = unref[i]->f_data;
2589 CURVNET_SET(so->so_vnet);
2595 * And finally release the sockets so they can be reclaimed.
2597 for (i = 0; i < total; i++)
2598 fdrop(unref[i], NULL);
2599 unp_recycled += total;
2600 free(unref, M_TEMP);
2604 unp_dispose_mbuf(struct mbuf *m)
2608 unp_scan(m, unp_freerights);
2612 * Synchronize against unp_gc, which can trip over data as we are freeing it.
2615 unp_dispose(struct socket *so)
2619 unp = sotounpcb(so);
2621 unp->unp_gcflag |= UNPGC_IGNORE_RIGHTS;
2623 if (!SOLISTENING(so))
2624 unp_dispose_mbuf(so->so_rcv.sb_mb);
2628 unp_scan(struct mbuf *m0, void (*op)(struct filedescent **, int))
2633 socklen_t clen, datalen;
2635 while (m0 != NULL) {
2636 for (m = m0; m; m = m->m_next) {
2637 if (m->m_type != MT_CONTROL)
2640 cm = mtod(m, struct cmsghdr *);
2643 while (cm != NULL) {
2644 if (sizeof(*cm) > clen || cm->cmsg_len > clen)
2647 data = CMSG_DATA(cm);
2648 datalen = (caddr_t)cm + cm->cmsg_len
2651 if (cm->cmsg_level == SOL_SOCKET &&
2652 cm->cmsg_type == SCM_RIGHTS) {
2653 (*op)(data, datalen /
2654 sizeof(struct filedescent *));
2657 if (CMSG_SPACE(datalen) < clen) {
2658 clen -= CMSG_SPACE(datalen);
2659 cm = (struct cmsghdr *)
2660 ((caddr_t)cm + CMSG_SPACE(datalen));
2672 * A helper function called by VFS before socket-type vnode reclamation.
2673 * For an active vnode it clears unp_vnode pointer and decrements unp_vnode
2677 vfs_unp_reclaim(struct vnode *vp)
2683 ASSERT_VOP_ELOCKED(vp, "vfs_unp_reclaim");
2684 KASSERT(vp->v_type == VSOCK,
2685 ("vfs_unp_reclaim: vp->v_type != VSOCK"));
2688 vplock = mtx_pool_find(mtxpool_sleep, vp);
2690 VOP_UNP_CONNECT(vp, &unp);
2694 if (unp->unp_vnode == vp) {
2696 unp->unp_vnode = NULL;
2699 UNP_PCB_UNLOCK(unp);
2708 db_print_indent(int indent)
2712 for (i = 0; i < indent; i++)
2717 db_print_unpflags(int unp_flags)
2722 if (unp_flags & UNP_HAVEPC) {
2723 db_printf("%sUNP_HAVEPC", comma ? ", " : "");
2726 if (unp_flags & UNP_WANTCRED) {
2727 db_printf("%sUNP_WANTCRED", comma ? ", " : "");
2730 if (unp_flags & UNP_CONNWAIT) {
2731 db_printf("%sUNP_CONNWAIT", comma ? ", " : "");
2734 if (unp_flags & UNP_CONNECTING) {
2735 db_printf("%sUNP_CONNECTING", comma ? ", " : "");
2738 if (unp_flags & UNP_BINDING) {
2739 db_printf("%sUNP_BINDING", comma ? ", " : "");
2745 db_print_xucred(int indent, struct xucred *xu)
2749 db_print_indent(indent);
2750 db_printf("cr_version: %u cr_uid: %u cr_ngroups: %d\n",
2751 xu->cr_version, xu->cr_uid, xu->cr_ngroups);
2752 db_print_indent(indent);
2753 db_printf("cr_groups: ");
2755 for (i = 0; i < xu->cr_ngroups; i++) {
2756 db_printf("%s%u", comma ? ", " : "", xu->cr_groups[i]);
2763 db_print_unprefs(int indent, struct unp_head *uh)
2769 LIST_FOREACH(unp, uh, unp_reflink) {
2770 if (counter % 4 == 0)
2771 db_print_indent(indent);
2772 db_printf("%p ", unp);
2773 if (counter % 4 == 3)
2777 if (counter != 0 && counter % 4 != 0)
2781 DB_SHOW_COMMAND(unpcb, db_show_unpcb)
2786 db_printf("usage: show unpcb <addr>\n");
2789 unp = (struct unpcb *)addr;
2791 db_printf("unp_socket: %p unp_vnode: %p\n", unp->unp_socket,
2794 db_printf("unp_ino: %ju unp_conn: %p\n", (uintmax_t)unp->unp_ino,
2797 db_printf("unp_refs:\n");
2798 db_print_unprefs(2, &unp->unp_refs);
2800 /* XXXRW: Would be nice to print the full address, if any. */
2801 db_printf("unp_addr: %p\n", unp->unp_addr);
2803 db_printf("unp_gencnt: %llu\n",
2804 (unsigned long long)unp->unp_gencnt);
2806 db_printf("unp_flags: %x (", unp->unp_flags);
2807 db_print_unpflags(unp->unp_flags);
2810 db_printf("unp_peercred:\n");
2811 db_print_xucred(2, &unp->unp_peercred);
2813 db_printf("unp_refcount: %u\n", unp->unp_refcount);